Git fork
at reftables-rust 195 lines 5.4 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2 3#include "../../git-compat-util.h" 4#include "../../json-writer.h" 5#include "../../repository.h" 6#include "../../trace2.h" 7#include "lazyload.h" 8#include <psapi.h> 9#include <tlhelp32.h> 10 11/* 12 * An arbitrarily chosen value to limit the size of the ancestor 13 * array built in git_processes(). 14 */ 15#define NR_PIDS_LIMIT 10 16 17/* 18 * Find the process data for the given PID in the given snapshot 19 * and update the PROCESSENTRY32 data. 20 */ 21static int find_pid(DWORD pid, HANDLE hSnapshot, PROCESSENTRY32 *pe32) 22{ 23 pe32->dwSize = sizeof(PROCESSENTRY32); 24 25 if (Process32First(hSnapshot, pe32)) { 26 do { 27 if (pe32->th32ProcessID == pid) 28 return 1; 29 } while (Process32Next(hSnapshot, pe32)); 30 } 31 return 0; 32} 33 34/* 35 * Accumulate JSON array of our parent processes: 36 * [ 37 * exe-name-parent, 38 * exe-name-grand-parent, 39 * ... 40 * ] 41 * 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). 47 * 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 52 * process exited). 53 * 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. 57 * 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. 62 * 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. 68 * 69 * Therefore, we keep an array of the visited PPIDs to guard against 70 * cycles. 71 * 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. 75 */ 76static void get_processes(struct json_writer *jw, HANDLE hSnapshot) 77{ 78 PROCESSENTRY32 pe32; 79 DWORD pid; 80 DWORD pid_list[NR_PIDS_LIMIT]; 81 int k, nr_pids = 0; 82 83 pid = GetCurrentProcessId(); 84 while (find_pid(pid, hSnapshot, &pe32)) { 85 /* Only report parents. Omit self from the JSON output. */ 86 if (nr_pids) 87 jw_array_string(jw, pe32.szExeFile); 88 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)"); 93 return; 94 } 95 96 if (nr_pids == NR_PIDS_LIMIT) { 97 jw_array_string(jw, "(truncated)"); 98 return; 99 } 100 101 pid_list[nr_pids++] = pid; 102 103 pid = pe32.th32ParentProcessID; 104 } 105} 106 107/* 108 * Emit JSON data for the current and parent processes. Individual 109 * trace2 targets can decide how to actually print it. 110 */ 111static void get_ancestry(void) 112{ 113 HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 114 115 if (hSnapshot != INVALID_HANDLE_VALUE) { 116 struct json_writer jw = JSON_WRITER_INIT; 117 118 jw_array_begin(&jw, 0); 119 get_processes(&jw, hSnapshot); 120 jw_end(&jw); 121 122 trace2_data_json("process", the_repository, "windows/ancestry", 123 &jw); 124 125 jw_release(&jw); 126 CloseHandle(hSnapshot); 127 } 128} 129 130/* 131 * Is a debugger attached to the current process? 132 * 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. 137 */ 138static void get_is_being_debugged(void) 139{ 140 if (IsDebuggerPresent()) 141 trace2_data_intmax("process", the_repository, 142 "windows/debugger_present", 1); 143} 144 145/* 146 * Emit JSON data with the peak memory usage of the current process. 147 */ 148static void get_peak_memory_info(void) 149{ 150 DECLARE_PROC_ADDR(psapi.dll, BOOL, WINAPI, GetProcessMemoryInfo, 151 HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD); 152 153 if (INIT_PROC_ADDR(GetProcessMemoryInfo)) { 154 PROCESS_MEMORY_COUNTERS pmc; 155 156 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, 157 sizeof(pmc))) { 158 struct json_writer jw = JSON_WRITER_INIT; 159 160 jw_object_begin(&jw, 0); 161 162#define KV(kv) #kv, (intmax_t)pmc.kv 163 164 jw_object_intmax(&jw, KV(PageFaultCount)); 165 jw_object_intmax(&jw, KV(PeakWorkingSetSize)); 166 jw_object_intmax(&jw, KV(PeakPagefileUsage)); 167 168 jw_end(&jw); 169 170 trace2_data_json("process", the_repository, 171 "windows/memory", &jw); 172 jw_release(&jw); 173 } 174 } 175} 176 177void trace2_collect_process_info(enum trace2_process_info_reason reason) 178{ 179 if (!trace2_is_enabled()) 180 return; 181 182 switch (reason) { 183 case TRACE2_PROCESS_INFO_STARTUP: 184 get_is_being_debugged(); 185 get_ancestry(); 186 return; 187 188 case TRACE2_PROCESS_INFO_EXIT: 189 get_peak_memory_info(); 190 return; 191 192 default: 193 BUG("trace2_collect_process_info: unknown reason '%d'", reason); 194 } 195}