From 45f8845bf3f3a4a01dbc9c6b9ff6b25276518f58 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 6 Nov 2018 18:01:55 +0100 Subject: [PATCH] mingw: add a helper function to attach GDB to the current process When debugging Git, the criss-cross spawning of processes can make things quite a bit difficult, especially when a Unix shell script is thrown in the mix that calls a `git.exe` that then segfaults. To help debugging such things, we introduce the `open_in_gdb()` function which can be called at a code location where the segfault happens (or as close as one can get); This will open a new MinTTY window with a GDB that already attached to the current process. Inspired by Derrick Stolee. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 13 +++++++++++++ compat/mingw.h | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 981d6c0dd4674a..7bf204d38fafeb 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -14,6 +14,19 @@ #define HCAST(type, handle) ((type)(intptr_t)handle) +void open_in_gdb(void) +{ + static struct child_process cp = CHILD_PROCESS_INIT; + extern char *_pgmptr; + + argv_array_pushl(&cp.args, "mintty", "gdb", NULL); + argv_array_pushf(&cp.args, "--pid=%d", getpid()); + cp.clean_on_exit = 1; + if (start_command(&cp) < 0) + die_errno("Could not start gdb"); + sleep(1); +} + int err_win_to_posix(DWORD winerr) { int error = ENOSYS; diff --git a/compat/mingw.h b/compat/mingw.h index 0614b4c81fa3dc..d792fd4eac1f53 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -733,6 +733,16 @@ static int mingw_main(c,v) #endif +/* + * For debugging: if a problem occurs, say, in a Git process that is spawned + * from another Git process which in turn is spawned from yet another Git + * process, it can be quite daunting to figure out what is going on. + * + * Call this function to open a new MinTTY (this assumes you are in Git for + * Windows' SDK) with a GDB that attaches to the current process right away. + */ +extern void open_in_gdb(void); + /* * Used by Pthread API implementation for Windows */