forked from git-for-windows/git
-
Notifications
You must be signed in to change notification settings - Fork 100
fixup! trace2: collect Windows-specific process information #117
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
Merged
jeffhostetler
merged 1 commit into
microsoft:vfs-2.20.1
from
jeffhostetler:gvfs-trace2-next-fixup
Feb 11, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
#include <Psapi.h> | ||
#include <tlHelp32.h> | ||
|
||
#define NR_PIDS_LIMIT 42 | ||
|
||
/* | ||
* Find the process data for the given PID in the given snapshot | ||
* and update the PROCESSENTRY32 data. | ||
|
@@ -21,13 +23,17 @@ static int find_pid(DWORD pid, HANDLE hSnapshot, PROCESSENTRY32 *pe32) | |
} | ||
|
||
/* | ||
* Accumulate JSON array: | ||
* Accumulate JSON array of our parent processes: | ||
* [ | ||
* exe-name-parent, | ||
* exe-name-grand-parent, | ||
* ... | ||
* ] | ||
* | ||
* We artificially limit this to NR_PIDS_LIMIT to quickly guard against cycles | ||
* in the parent PIDs without a lot of fuss and because we just want some | ||
* context and don't need an absolute answer. | ||
* | ||
* Note: we only report the filename of the process executable; the | ||
* only way to get its full pathname is to use OpenProcess() | ||
* and GetModuleFileNameEx() or QueryfullProcessImageName() | ||
|
@@ -38,16 +44,28 @@ static void get_processes(struct json_writer *jw, HANDLE hSnapshot) | |
{ | ||
PROCESSENTRY32 pe32; | ||
DWORD pid; | ||
DWORD pid_list[NR_PIDS_LIMIT]; | ||
int k, nr_pids = 0; | ||
|
||
pid = GetCurrentProcessId(); | ||
while (find_pid(pid, hSnapshot, &pe32)) { | ||
/* Only report parents. Omit self from the JSON output. */ | ||
if (nr_pids) | ||
jw_array_string(jw, pe32.szExeFile); | ||
|
||
/* We only want parent processes, so skip self. */ | ||
if (!find_pid(pid, hSnapshot, &pe32)) | ||
return; | ||
pid = pe32.th32ParentProcessID; | ||
/* Check for cycle in snapshot. (Yes, it happened.) */ | ||
for (k = 0; k < nr_pids; k++) | ||
if (pid == pid_list[k]) { | ||
jw_array_string(jw, "(cycle)"); | ||
return; | ||
} | ||
|
||
while (find_pid(pid, hSnapshot, &pe32)) { | ||
jw_array_string(jw, pe32.szExeFile); | ||
if (nr_pids == NR_PIDS_LIMIT) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Paranoia nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, but we'd already have a buffer overrun at that point. |
||
jw_array_string(jw, "(truncated)"); | ||
return; | ||
} | ||
|
||
pid_list[nr_pids++] = pid; | ||
|
||
pid = pe32.th32ParentProcessID; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to check for cycles or should we just give up once we exceed NR_PIDS_LIMIT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, i could go either way here. my way lets us see "(cycle)" vs "(truncate)" in the Kusto data.
i thought that might be interesting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we check for a cycle (which we know happens), we will avoid having
NR_PIDS_LIMIT
lines of repeated entries every time it does happen...