Skip to content

Commit

Permalink
trace2: collect Windows-specific process information
Browse files Browse the repository at this point in the history
Add platform-specific interface to log information about the current
process.

On Windows, this interface is used to indicate whether the git process
is running under a debugger and list names of the process ancestors.

Information for other platforms is left for a future effort.

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
jeffhostetler authored and dscho committed Feb 27, 2019
1 parent 2a4785a commit df00b65
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions common-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int main(int argc, const char **argv)

trace2_initialize();
trace2_cmd_start(argv);
trace2_collect_process_info();

git_resolve_executable_dir(argv[0]);

Expand Down
119 changes: 119 additions & 0 deletions compat/win32/trace2_win32_process_info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "../../cache.h"
#include "../../json-writer.h"
#include <Psapi.h>
#include <tlHelp32.h>

#define NR_PIDS_LIMIT 42

/*
* Find the process data for the given PID in the given snapshot
* and update the PROCESSENTRY32 data.
*/
static int find_pid(DWORD pid, HANDLE hSnapshot, PROCESSENTRY32 *pe32)
{
pe32->dwSize = sizeof(PROCESSENTRY32);

if (Process32First(hSnapshot, pe32)) {
do {
if (pe32->th32ProcessID == pid)
return 1;
} while (Process32Next(hSnapshot, pe32));
}
return 0;
}

/*
* Accumulate JSON array of our parent processes:
* [
* exe-name-parent,
* exe-name-grand-parent,
* ...
* ]
*
* We artificially limit this to NR_PIDS_LIMIT to quickly guard against cycles
* in the parent PIDs without a lot of fuss and because we just want some
* context and don't need an absolute answer.
*
* Note: we only report the filename of the process executable; the
* only way to get its full pathname is to use OpenProcess()
* and GetModuleFileNameEx() or QueryfullProcessImageName()
* and that seems rather expensive (on top of the cost of
* getting the snapshot).
*/
static void get_processes(struct json_writer *jw, HANDLE hSnapshot)
{
PROCESSENTRY32 pe32;
DWORD pid;
DWORD pid_list[NR_PIDS_LIMIT];
int k, nr_pids = 0;

pid = GetCurrentProcessId();
while (find_pid(pid, hSnapshot, &pe32)) {
/* Only report parents. Omit self from the JSON output. */
if (nr_pids)
jw_array_string(jw, pe32.szExeFile);

/* Check for cycle in snapshot. (Yes, it happened.) */
for (k = 0; k < nr_pids; k++)
if (pid == pid_list[k]) {
jw_array_string(jw, "(cycle)");
return;
}

if (nr_pids == NR_PIDS_LIMIT) {
jw_array_string(jw, "(truncated)");
return;
}

pid_list[nr_pids++] = pid;

pid = pe32.th32ParentProcessID;
}
}

/*
* Emit JSON data for the current and parent processes. Individual
* trace2 targets can decide how to actually print it.
*/
static void get_ancestry(void)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hSnapshot != INVALID_HANDLE_VALUE) {
struct json_writer jw = JSON_WRITER_INIT;

jw_array_begin(&jw, 0);
get_processes(&jw, hSnapshot);
jw_end(&jw);

trace2_data_json("process", the_repository, "windows/ancestry",
&jw);

jw_release(&jw);
CloseHandle(hSnapshot);
}
}

/*
* Is a debugger attached to the current process?
*
* This will catch debug runs (where the debugger started the process).
* This is the normal case. Since this code is called during our startup,
* it will not report instances where a debugger is attached dynamically
* to a running git process, but that is relatively rare.
*/
static void get_is_being_debugged(void)
{
if (IsDebuggerPresent())
trace2_data_intmax("process", the_repository,
"windows/debugger_present", 1);
}

void trace2_collect_process_info(void)
{
if (!trace2_is_enabled())
return;

get_is_being_debugged();
get_ancestry();
}
2 changes: 2 additions & 0 deletions config.mak.uname
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ ifeq ($(uname_S),Windows)
BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
COMPAT_OBJS = compat/msvc.o compat/winansi.o \
compat/win32/pthread.o compat/win32/syslog.o \
compat/win32/trace2_win32_process_info.o \
compat/win32/dirent.o compat/win32/fscache.o
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
Expand Down Expand Up @@ -605,6 +606,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
COMPAT_OBJS += compat/mingw.o compat/winansi.o \
compat/win32/path-utils.o \
compat/win32/trace2_win32_process_info.o \
compat/win32/pthread.o compat/win32/syslog.o \
compat/win32/dirent.o compat/win32/fscache.o
BASIC_CFLAGS += -DWIN32 -DPROTECT_NTFS_DEFAULT=1
Expand Down
14 changes: 14 additions & 0 deletions trace2.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,18 @@ void trace2_printf(const char *fmt, ...);
/* clang-format on */
#endif

/*
* Optional platform-specific code to dump information about the
* current and any parent process(es). This is intended to allow
* post-processors to know who spawned this git instance and anything
* else the platform may be able to tell us about the current process.
*/
#if defined(GIT_WINDOWS_NATIVE)
void trace2_collect_process_info(void);
#else
#define trace2_collect_process_info() \
do { \
} while (0)
#endif

#endif /* TRACE2_H */

0 comments on commit df00b65

Please sign in to comment.