trace2: refactor Windows process ancestry trace2 event

In 353d3d77f4 (trace2: collect Windows-specific process information, 2019-02-22) we added process ancestry information for Windows to TRACE2 via a data_json event. It was only later in 2f732bf15e (tr2: log parent process name, 2021-07-21) that the specific cmd_ancestry event was added to TRACE2. In a future commit we will emit the ancestry information with the newer cmd_ancestry TRACE2 event. Right now, we rework this implementation of trace2_collect_process_info to separate the calculation of ancestors from building and emiting the JSON array via a data_json event. Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthew John Cheetham committed Feb 13, 2026 at 19:54 UTC c2a473b2b1b3b79e308a7cbed1eee46fb67aacd8
1 file changed +25 -25
compat/win32/trace2_win32_process_info.c
+25 -25
@@ -3,6 +3,7 @@
3 #include "../../git-compat-util.h"
4 #include "../../json-writer.h"
5 #include "../../repository.h"
6 +#include "../../strvec.h"
7 #include "../../trace2.h"
8 #include "lazyload.h"
9 #include <psapi.h>
@@ -32,12 +33,7 @@ static int find_pid(DWORD pid, HANDLE hSnapshot, PROCESSENTRY32 *pe32)
33 }
34
35 /*
35 - * Accumulate JSON array of our parent processes:
36 - * [
37 - * exe-name-parent,
38 - * exe-name-grand-parent,
39 - * ...
40 - * ]
36 + * Accumulate array of our parent process names.
37 *
38 * Note: we only report the filename of the process executable; the
39 * only way to get its full pathname is to use OpenProcess()
@@ -73,7 +69,7 @@ static int find_pid(DWORD pid, HANDLE hSnapshot, PROCESSENTRY32 *pe32)
69 * simple and avoid the alloc/realloc overhead. It is OK if we
70 * truncate the search and return a partial answer.
71 */
76 -static void get_processes(struct json_writer *jw, HANDLE hSnapshot)
72 +static void get_processes(struct strvec *names, HANDLE hSnapshot)
73 {
74 PROCESSENTRY32 pe32;
75 DWORD pid;
@@ -82,19 +78,19 @@ static void get_processes(struct json_writer *jw, HANDLE hSnapshot)
78
79 pid = GetCurrentProcessId();
80 while (find_pid(pid, hSnapshot, &pe32)) {
85 - /* Only report parents. Omit self from the JSON output. */
81 + /* Only report parents. Omit self from the output. */
82 if (nr_pids)
87 - jw_array_string(jw, pe32.szExeFile);
83 + strvec_push(names, pe32.szExeFile);
84
85 /* Check for cycle in snapshot. (Yes, it happened.) */
86 for (k = 0; k < nr_pids; k++)
87 if (pid == pid_list[k]) {
92 - jw_array_string(jw, "(cycle)");
88 + strvec_push(names, "(cycle)");
89 return;
90 }
91
92 if (nr_pids == NR_PIDS_LIMIT) {
97 - jw_array_string(jw, "(truncated)");
93 + strvec_push(names, "(truncated)");
94 return;
95 }
96
@@ -105,24 +101,14 @@ static void get_processes(struct json_writer *jw, HANDLE hSnapshot)
101 }
102
103 /*
108 - * Emit JSON data for the current and parent processes. Individual
109 - * trace2 targets can decide how to actually print it.
104 + * Collect the list of parent process names.
105 */
111 -static void get_ancestry(void)
106 +static void get_ancestry(struct strvec *names)
107 {
108 HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
109
110 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);
111 + get_processes(names, hSnapshot);
112 CloseHandle(hSnapshot);
113 }
114 }
@@ -176,13 +162,27 @@ static void get_peak_memory_info(void)
162
163 void trace2_collect_process_info(enum trace2_process_info_reason reason)
164 {
165 + struct strvec names = STRVEC_INIT;
166 +
167 if (!trace2_is_enabled())
168 return;
169
170 switch (reason) {
171 case TRACE2_PROCESS_INFO_STARTUP:
172 get_is_being_debugged();
185 - get_ancestry();
173 + get_ancestry(&names);
174 + if (names.nr) {
175 + struct json_writer jw = JSON_WRITER_INIT;
176 + jw_array_begin(&jw, 0);
177 + for (size_t i = 0; i < names.nr; i++)
178 + jw_array_string(&jw, names.v[i]);
179 + jw_end(&jw);
180 + trace2_data_json("process", the_repository,
181 + "windows/ancestry", &jw);
182 + jw_release(&jw);
183 + }
184 +
185 + strvec_clear(&names);
186 return;
187
188 case TRACE2_PROCESS_INFO_EXIT: