Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PssFreeSnapshot #115

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions metric/system/process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,39 @@ func GetInfoForPid(_ resolve.Resolver, pid int) (ProcState, error) {
}

func FetchNumThreads(pid int) (int, error) {
pHandle, err := syscall.OpenProcess(
targetProcessHandle, err := syscall.OpenProcess(
xsyswindows.PROCESS_QUERY_INFORMATION,
false,
uint32(pid))
if err != nil {
return 0, fmt.Errorf("OpenProcess failed for PID %d: %w", pid, err)
}
defer syscall.CloseHandle(pHandle)
defer syscall.CloseHandle(targetProcessHandle)

currentProcessHandle, err := syscall.GetCurrentProcess()
if err != nil {
return 0, fmt.Errorf("GetCurrentProcess failed: %w", err)
}
// The pseudo handle need not be closed when it is no longer
// needed, calling CloseHandle has no effect. Adding here to
// remind us to close any handles we open.
defer syscall.CloseHandle(currentProcessHandle)

var snapshotHandle syscall.Handle
err = PssCaptureSnapshot(pHandle, PSSCaptureThreads, 0, &snapshotHandle)
err = PssCaptureSnapshot(targetProcessHandle, PSSCaptureThreads, 0, &snapshotHandle)
if err != nil {
return 0, fmt.Errorf("PssCaptureSnapshot failed: %w", err)
}

info := PssThreadInformation{}
buffSize := unsafe.Sizeof(info)
err = PssQuerySnapshot(snapshotHandle, PssQueryThreadInformation, &info, uint32(buffSize))
if err != nil {
return 0, fmt.Errorf("PssQuerySnapshot failed: %w", err)
queryErr := PssQuerySnapshot(snapshotHandle, PssQueryThreadInformation, &info, uint32(buffSize))
freeErr := PssFreeSnapshot(currentProcessHandle, snapshotHandle)
if queryErr != nil || freeErr != nil {
//Join discards any nil errors
return 0, errors.Join(
fmt.Errorf("PssQuerySnapshot failed: %w", queryErr),
fmt.Errorf("PssFreeSnapshot failed: %w", freeErr))
}

return int(info.ThreadsCaptured), nil
Expand Down
2 changes: 2 additions & 0 deletions metric/system/process/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package process
// - https://learn.microsoft.com/en-us/previous-versions/windows/desktop/proc_snap/overview-of-process-snapshotting
// PssCaptureSnapshot docs in https://learn.microsoft.com/en-us/windows/win32/api/processsnapshot/nf-processsnapshot-psscapturesnapshot
// PssQuerySnapshot docs in https://learn.microsoft.com/en-us/windows/win32/api/processsnapshot/nf-processsnapshot-pssquerysnapshot
// PssFreeSnapshot docs in https://learn.microsoft.com/en-us/windows/win32/api/processsnapshot/nf-processsnapshot-pssfreesnapshot

// Use golang.org/x/sys/windows/mkwinsyscall instead of adriansr/mksyscall
// below once https://github.com/golang/go/issues/42373 is fixed.
Expand All @@ -29,6 +30,7 @@ package process

//sys PssCaptureSnapshot(processHandle syscall.Handle, captureFlags PSSCaptureFlags, threadContextFlags uint32, snapshotHandle *syscall.Handle) (err error) [failretval!=0] = kernel32.PssCaptureSnapshot
//sys PssQuerySnapshot(snapshotHandle syscall.Handle, informationClass uint32, buffer *PssThreadInformation, bufferLength uint32) (err error) [failretval!=0] = kernel32.PssQuerySnapshot
//sys PssFreeSnapshot(processHandle syscall.Handle, snapshotHandle syscall.Handle) (err error) [failretval!=0] = kernel32.PssFreeSnapshot

// The following constants are PssQueryInformationClass as defined on
// https://learn.microsoft.com/en-us/windows/win32/api/processsnapshot/ne-processsnapshot-pss_query_information_class
Expand Down
9 changes: 9 additions & 0 deletions metric/system/process/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.