1 #include "../../cache.h"
2 #include "../../json-writer.h"
8 * An arbitrarily chosen value to limit the size of the ancestor
9 * array built in git_processes().
11 #define NR_PIDS_LIMIT 10
14 * Find the process data for the given PID in the given snapshot
15 * and update the PROCESSENTRY32 data.
17 static int find_pid(DWORD pid, HANDLE hSnapshot, PROCESSENTRY32 *pe32)
19 pe32->dwSize = sizeof(PROCESSENTRY32);
21 if (Process32First(hSnapshot, pe32)) {
23 if (pe32->th32ProcessID == pid)
25 } while (Process32Next(hSnapshot, pe32));
31 * Accumulate JSON array of our parent processes:
34 * exe-name-grand-parent,
38 * Note: we only report the filename of the process executable; the
39 * only way to get its full pathname is to use OpenProcess()
40 * and GetModuleFileNameEx() or QueryfullProcessImageName()
41 * and that seems rather expensive (on top of the cost of
42 * getting the snapshot).
44 * Note: we compute the set of parent processes by walking the PPID
45 * link in each visited PROCESSENTRY32 record. This search
46 * stops when an ancestor process is not found in the snapshot
47 * (because it exited before the current or intermediate parent
50 * This search may compute an incorrect result if the PPID link
51 * refers to the PID of an exited parent and that PID has been
52 * recycled and given to a new unrelated process.
54 * Worse, it is possible for a child or descendant of the
55 * current process to be given the recycled PID and cause a
56 * PPID-cycle. This would cause an infinite loop building our
57 * parent process array.
59 * Note: for completeness, the "System Idle" process has PID=0 and
60 * PPID=0 and could cause another PPID-cycle. We don't expect
61 * Git to be a descendant of the idle process, but because of
62 * PID recycling, it might be possible to get a PPID link value
63 * of 0. This too would cause an infinite loop.
65 * Therefore, we keep an array of the visited PPIDs to guard against
68 * We use a fixed-size array rather than ALLOC_GROW to keep things
69 * simple and avoid the alloc/realloc overhead. It is OK if we
70 * truncate the search and return a partial answer.
72 static void get_processes(struct json_writer *jw, HANDLE hSnapshot)
76 DWORD pid_list[NR_PIDS_LIMIT];
79 pid = GetCurrentProcessId();
80 while (find_pid(pid, hSnapshot, &pe32)) {
81 /* Only report parents. Omit self from the JSON output. */
83 jw_array_string(jw, pe32.szExeFile);
85 /* Check for cycle in snapshot. (Yes, it happened.) */
86 for (k = 0; k < nr_pids; k++)
87 if (pid == pid_list[k]) {
88 jw_array_string(jw, "(cycle)");
92 if (nr_pids == NR_PIDS_LIMIT) {
93 jw_array_string(jw, "(truncated)");
97 pid_list[nr_pids++] = pid;
99 pid = pe32.th32ParentProcessID;
104 * Emit JSON data for the current and parent processes. Individual
105 * trace2 targets can decide how to actually print it.
107 static void get_ancestry(void)
109 HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
111 if (hSnapshot != INVALID_HANDLE_VALUE) {
112 struct json_writer jw = JSON_WRITER_INIT;
114 jw_array_begin(&jw, 0);
115 get_processes(&jw, hSnapshot);
118 trace2_data_json("process", the_repository, "windows/ancestry",
122 CloseHandle(hSnapshot);
127 * Is a debugger attached to the current process?
129 * This will catch debug runs (where the debugger started the process).
130 * This is the normal case. Since this code is called during our startup,
131 * it will not report instances where a debugger is attached dynamically
132 * to a running git process, but that is relatively rare.
134 static void get_is_being_debugged(void)
136 if (IsDebuggerPresent())
137 trace2_data_intmax("process", the_repository,
138 "windows/debugger_present", 1);
142 * Emit JSON data with the peak memory usage of the current process.
144 static void get_peak_memory_info(void)
146 DECLARE_PROC_ADDR(psapi.dll, BOOL, GetProcessMemoryInfo, HANDLE,
147 PPROCESS_MEMORY_COUNTERS, DWORD);
149 if (INIT_PROC_ADDR(GetProcessMemoryInfo)) {
150 PROCESS_MEMORY_COUNTERS pmc;
152 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc,
154 struct json_writer jw = JSON_WRITER_INIT;
156 jw_object_begin(&jw, 0);
158 #define KV(kv) #kv, (intmax_t)pmc.kv
160 jw_object_intmax(&jw, KV(PageFaultCount));
161 jw_object_intmax(&jw, KV(PeakWorkingSetSize));
162 jw_object_intmax(&jw, KV(PeakPagefileUsage));
166 trace2_data_json("process", the_repository,
167 "windows/memory", &jw);
173 void trace2_collect_process_info(enum trace2_process_info_reason reason)
175 if (!trace2_is_enabled())
179 case TRACE2_PROCESS_INFO_STARTUP:
180 get_is_being_debugged();
184 case TRACE2_PROCESS_INFO_EXIT:
185 get_peak_memory_info();
189 BUG("trace2_collect_process_info: unknown reason '%d'", reason);