mingw: short-circuit the conversion of `/dev/null` to UTF-16
[git/debian.git] / compat / win32 / trace2_win32_process_info.c
blob8ccbd1c2c6f82d68e0c39f8ec77aa2bd4a899f2e
1 #include "../../cache.h"
2 #include "../../json-writer.h"
3 #include "lazyload.h"
4 #include <Psapi.h>
5 #include <tlHelp32.h>
7 /*
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)) {
22 do {
23 if (pe32->th32ProcessID == pid)
24 return 1;
25 } while (Process32Next(hSnapshot, pe32));
27 return 0;
31 * Accumulate JSON array of our parent processes:
32 * [
33 * exe-name-parent,
34 * exe-name-grand-parent,
35 * ...
36 * ]
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
48 * process exited).
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
66 * cycles.
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)
74 PROCESSENTRY32 pe32;
75 DWORD pid;
76 DWORD pid_list[NR_PIDS_LIMIT];
77 int k, nr_pids = 0;
79 pid = GetCurrentProcessId();
80 while (find_pid(pid, hSnapshot, &pe32)) {
81 /* Only report parents. Omit self from the JSON output. */
82 if (nr_pids)
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)");
89 return;
92 if (nr_pids == NR_PIDS_LIMIT) {
93 jw_array_string(jw, "(truncated)");
94 return;
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);
116 jw_end(&jw);
118 trace2_data_json("process", the_repository, "windows/ancestry",
119 &jw);
121 jw_release(&jw);
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,
153 sizeof(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));
164 jw_end(&jw);
166 trace2_data_json("process", the_repository,
167 "windows/memory", &jw);
168 jw_release(&jw);
173 void trace2_collect_process_info(enum trace2_process_info_reason reason)
175 if (!trace2_is_enabled())
176 return;
178 switch (reason) {
179 case TRACE2_PROCESS_INFO_STARTUP:
180 get_is_being_debugged();
181 get_ancestry();
182 return;
184 case TRACE2_PROCESS_INFO_EXIT:
185 get_peak_memory_info();
186 return;
188 default:
189 BUG("trace2_collect_process_info: unknown reason '%d'", reason);