1
#define USE_THE_REPOSITORY_VARIABLE
3
#include "../../git-compat-util.h"
4
#include "../../json-writer.h"
5
#include "../../repository.h"
6
#include "../../trace2.h"
12
* An arbitrarily chosen value to limit the size of the ancestor
13
* array built in git_processes().
15
#define NR_PIDS_LIMIT 10
18
* Find the process data for the given PID in the given snapshot
19
* and update the PROCESSENTRY32 data.
21
static int find_pid(DWORD pid, HANDLE hSnapshot, PROCESSENTRY32 *pe32)
23
pe32->dwSize = sizeof(PROCESSENTRY32);
25
if (Process32First(hSnapshot, pe32)) {
27
if (pe32->th32ProcessID == pid)
29
} while (Process32Next(hSnapshot, pe32));
35
* Accumulate JSON array of our parent processes:
38
* exe-name-grand-parent,
42
* Note: we only report the filename of the process executable; the
43
* only way to get its full pathname is to use OpenProcess()
44
* and GetModuleFileNameEx() or QueryfullProcessImageName()
45
* and that seems rather expensive (on top of the cost of
46
* getting the snapshot).
48
* Note: we compute the set of parent processes by walking the PPID
49
* link in each visited PROCESSENTRY32 record. This search
50
* stops when an ancestor process is not found in the snapshot
51
* (because it exited before the current or intermediate parent
54
* This search may compute an incorrect result if the PPID link
55
* refers to the PID of an exited parent and that PID has been
56
* recycled and given to a new unrelated process.
58
* Worse, it is possible for a child or descendant of the
59
* current process to be given the recycled PID and cause a
60
* PPID-cycle. This would cause an infinite loop building our
61
* parent process array.
63
* Note: for completeness, the "System Idle" process has PID=0 and
64
* PPID=0 and could cause another PPID-cycle. We don't expect
65
* Git to be a descendant of the idle process, but because of
66
* PID recycling, it might be possible to get a PPID link value
67
* of 0. This too would cause an infinite loop.
69
* Therefore, we keep an array of the visited PPIDs to guard against
72
* We use a fixed-size array rather than ALLOC_GROW to keep things
73
* simple and avoid the alloc/realloc overhead. It is OK if we
74
* truncate the search and return a partial answer.
76
static void get_processes(struct json_writer *jw, HANDLE hSnapshot)
80
DWORD pid_list[NR_PIDS_LIMIT];
83
pid = GetCurrentProcessId();
84
while (find_pid(pid, hSnapshot, &pe32)) {
85
/* Only report parents. Omit self from the JSON output. */
87
jw_array_string(jw, pe32.szExeFile);
89
/* Check for cycle in snapshot. (Yes, it happened.) */
90
for (k = 0; k < nr_pids; k++)
91
if (pid == pid_list[k]) {
92
jw_array_string(jw, "(cycle)");
96
if (nr_pids == NR_PIDS_LIMIT) {
97
jw_array_string(jw, "(truncated)");
101
pid_list[nr_pids++] = pid;
103
pid = pe32.th32ParentProcessID;
108
* Emit JSON data for the current and parent processes. Individual
109
* trace2 targets can decide how to actually print it.
111
static void get_ancestry(void)
113
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
115
if (hSnapshot != INVALID_HANDLE_VALUE) {
116
struct json_writer jw = JSON_WRITER_INIT;
118
jw_array_begin(&jw, 0);
119
get_processes(&jw, hSnapshot);
122
trace2_data_json("process", the_repository, "windows/ancestry",
126
CloseHandle(hSnapshot);
131
* Is a debugger attached to the current process?
133
* This will catch debug runs (where the debugger started the process).
134
* This is the normal case. Since this code is called during our startup,
135
* it will not report instances where a debugger is attached dynamically
136
* to a running git process, but that is relatively rare.
138
static void get_is_being_debugged(void)
140
if (IsDebuggerPresent())
141
trace2_data_intmax("process", the_repository,
142
"windows/debugger_present", 1);
146
* Emit JSON data with the peak memory usage of the current process.
148
static void get_peak_memory_info(void)
150
DECLARE_PROC_ADDR(psapi.dll, BOOL, WINAPI, GetProcessMemoryInfo,
151
HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
153
if (INIT_PROC_ADDR(GetProcessMemoryInfo)) {
154
PROCESS_MEMORY_COUNTERS pmc;
156
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc,
158
struct json_writer jw = JSON_WRITER_INIT;
160
jw_object_begin(&jw, 0);
162
#define KV(kv) #kv, (intmax_t)pmc.kv
164
jw_object_intmax(&jw, KV(PageFaultCount));
165
jw_object_intmax(&jw, KV(PeakWorkingSetSize));
166
jw_object_intmax(&jw, KV(PeakPagefileUsage));
170
trace2_data_json("process", the_repository,
171
"windows/memory", &jw);
177
void trace2_collect_process_info(enum trace2_process_info_reason reason)
179
if (!trace2_is_enabled())
183
case TRACE2_PROCESS_INFO_STARTUP:
184
get_is_being_debugged();
188
case TRACE2_PROCESS_INFO_EXIT:
189
get_peak_memory_info();
193
BUG("trace2_collect_process_info: unknown reason '%d'", reason);