1 #include "../../cache.h"
2 #include "../../json-writer.h"
7 * An arbitrarily chosen value to limit the size of the ancestor
8 * array built in git_processes().
10 #define NR_PIDS_LIMIT 10
13 * Find the process data for the given PID in the given snapshot
14 * and update the PROCESSENTRY32 data.
16 static int find_pid(DWORD pid
, HANDLE hSnapshot
, PROCESSENTRY32
*pe32
)
18 pe32
->dwSize
= sizeof(PROCESSENTRY32
);
20 if (Process32First(hSnapshot
, pe32
)) {
22 if (pe32
->th32ProcessID
== pid
)
24 } while (Process32Next(hSnapshot
, pe32
));
30 * Accumulate JSON array of our parent processes:
33 * exe-name-grand-parent,
37 * Note: we only report the filename of the process executable; the
38 * only way to get its full pathname is to use OpenProcess()
39 * and GetModuleFileNameEx() or QueryfullProcessImageName()
40 * and that seems rather expensive (on top of the cost of
41 * getting the snapshot).
43 * Note: we compute the set of parent processes by walking the PPID
44 * link in each visited PROCESSENTRY32 record. This search
45 * stops when an ancestor process is not found in the snapshot
46 * (because it exited before the current or intermediate parent
49 * This search may compute an incorrect result if the PPID link
50 * refers to the PID of an exited parent and that PID has been
51 * recycled and given to a new unrelated process.
53 * Worse, it is possible for a child or descendant of the
54 * current process to be given the recycled PID and cause a
55 * PPID-cycle. This would cause an infinite loop building our
56 * parent process array.
58 * Note: for completeness, the "System Idle" process has PID=0 and
59 * PPID=0 and could cause another PPID-cycle. We don't expect
60 * Git to be a descendant of the idle process, but because of
61 * PID recycling, it might be possible to get a PPID link value
62 * of 0. This too would cause an infinite loop.
64 * Therefore, we keep an array of the visited PPIDs to guard against
67 * We use a fixed-size array rather than ALLOC_GROW to keep things
68 * simple and avoid the alloc/realloc overhead. It is OK if we
69 * truncate the search and return a partial answer.
71 static void get_processes(struct json_writer
*jw
, HANDLE hSnapshot
)
75 DWORD pid_list
[NR_PIDS_LIMIT
];
78 pid
= GetCurrentProcessId();
79 while (find_pid(pid
, hSnapshot
, &pe32
)) {
80 /* Only report parents. Omit self from the JSON output. */
82 jw_array_string(jw
, pe32
.szExeFile
);
84 /* Check for cycle in snapshot. (Yes, it happened.) */
85 for (k
= 0; k
< nr_pids
; k
++)
86 if (pid
== pid_list
[k
]) {
87 jw_array_string(jw
, "(cycle)");
91 if (nr_pids
== NR_PIDS_LIMIT
) {
92 jw_array_string(jw
, "(truncated)");
96 pid_list
[nr_pids
++] = pid
;
98 pid
= pe32
.th32ParentProcessID
;
103 * Emit JSON data for the current and parent processes. Individual
104 * trace2 targets can decide how to actually print it.
106 static void get_ancestry(void)
108 HANDLE hSnapshot
= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS
, 0);
110 if (hSnapshot
!= INVALID_HANDLE_VALUE
) {
111 struct json_writer jw
= JSON_WRITER_INIT
;
113 jw_array_begin(&jw
, 0);
114 get_processes(&jw
, hSnapshot
);
117 trace2_data_json("process", the_repository
, "windows/ancestry",
121 CloseHandle(hSnapshot
);
126 * Is a debugger attached to the current process?
128 * This will catch debug runs (where the debugger started the process).
129 * This is the normal case. Since this code is called during our startup,
130 * it will not report instances where a debugger is attached dynamically
131 * to a running git process, but that is relatively rare.
133 static void get_is_being_debugged(void)
135 if (IsDebuggerPresent())
136 trace2_data_intmax("process", the_repository
,
137 "windows/debugger_present", 1);
140 void trace2_collect_process_info(void)
142 if (!trace2_is_enabled())
145 get_is_being_debugged();