@matheusbsbs / matheus-bagliano / commits / 08809c09aa
Fork 0

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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Feb 13, 2020 at 21:49 UTC 08809c09aa1351b603e9c55734105cd2e3c24c41
2 files changed +23
compat/mingw.c
+13
@@ -13,6 +13,19 @@
13
14 static const int delay[] = { 0, 1, 10, 20, 40 };
15
16 +void open_in_gdb(void)
17 +{
18 + static struct child_process cp = CHILD_PROCESS_INIT;
19 + extern char *_pgmptr;
20 +
21 + argv_array_pushl(&cp.args, "mintty", "gdb", NULL);
22 + argv_array_pushf(&cp.args, "--pid=%d", getpid());
23 + cp.clean_on_exit = 1;
24 + if (start_command(&cp) < 0)
25 + die_errno("Could not start gdb");
26 + sleep(1);
27 +}
28 +
29 int err_win_to_posix(DWORD winerr)
30 {
31 int error = ENOSYS;
compat/mingw.h
+10
@@ -591,6 +591,16 @@ extern CRITICAL_SECTION pinfo_cs;
591 int wmain(int argc, const wchar_t **w_argv);
592 int main(int argc, const char **argv);
593
594 +/*
595 + * For debugging: if a problem occurs, say, in a Git process that is spawned
596 + * from another Git process which in turn is spawned from yet another Git
597 + * process, it can be quite daunting to figure out what is going on.
598 + *
599 + * Call this function to open a new MinTTY (this assumes you are in Git for
600 + * Windows' SDK) with a GDB that attaches to the current process right away.
601 + */
602 +extern void open_in_gdb(void);
603 +
604 /*
605 * Used by Pthread API implementation for Windows
606 */