Skip to content

Commit aa59353

Browse files
mjcheethamgitster
authored andcommitted
trace2: refactor Windows process ancestry trace2 event
In 353d3d7 (trace2: collect Windows-specific process information) we added process ancestry information for Windows to TRACE2 via a data_json event. It was only later in 2f732bf (tr2: log parent process name) 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>
1 parent 863d158 commit aa59353

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

compat/win32/trace2_win32_process_info.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../../git-compat-util.h"
44
#include "../../json-writer.h"
55
#include "../../repository.h"
6+
#include "../../strvec.h"
67
#include "../../trace2.h"
78
#include "lazyload.h"
89
#include <psapi.h>
@@ -32,12 +33,7 @@ static int find_pid(DWORD pid, HANDLE hSnapshot, PROCESSENTRY32 *pe32)
3233
}
3334

3435
/*
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.
4137
*
4238
* Note: we only report the filename of the process executable; the
4339
* 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)
7369
* simple and avoid the alloc/realloc overhead. It is OK if we
7470
* truncate the search and return a partial answer.
7571
*/
76-
static void get_processes(struct json_writer *jw, HANDLE hSnapshot)
72+
static void get_processes(struct strvec *names, HANDLE hSnapshot)
7773
{
7874
PROCESSENTRY32 pe32;
7975
DWORD pid;
@@ -82,19 +78,19 @@ static void get_processes(struct json_writer *jw, HANDLE hSnapshot)
8278

8379
pid = GetCurrentProcessId();
8480
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. */
8682
if (nr_pids)
87-
jw_array_string(jw, pe32.szExeFile);
83+
strvec_push(names, pe32.szExeFile);
8884

8985
/* Check for cycle in snapshot. (Yes, it happened.) */
9086
for (k = 0; k < nr_pids; k++)
9187
if (pid == pid_list[k]) {
92-
jw_array_string(jw, "(cycle)");
88+
strvec_push(names, "(cycle)");
9389
return;
9490
}
9591

9692
if (nr_pids == NR_PIDS_LIMIT) {
97-
jw_array_string(jw, "(truncated)");
93+
strvec_push(names, "(truncated)");
9894
return;
9995
}
10096

@@ -105,24 +101,14 @@ static void get_processes(struct json_writer *jw, HANDLE hSnapshot)
105101
}
106102

107103
/*
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.
110105
*/
111-
static void get_ancestry(void)
106+
static void get_ancestry(struct strvec *names)
112107
{
113108
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
114109

115110
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);
126112
CloseHandle(hSnapshot);
127113
}
128114
}
@@ -176,13 +162,27 @@ static void get_peak_memory_info(void)
176162

177163
void trace2_collect_process_info(enum trace2_process_info_reason reason)
178164
{
165+
struct strvec names = STRVEC_INIT;
166+
179167
if (!trace2_is_enabled())
180168
return;
181169

182170
switch (reason) {
183171
case TRACE2_PROCESS_INFO_STARTUP:
184172
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);
186186
return;
187187

188188
case TRACE2_PROCESS_INFO_EXIT:

0 commit comments

Comments
 (0)