Skip to content

Commit 86dc798

Browse files
committed
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 <[email protected]>
1 parent 27e3e78 commit 86dc798

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

compat/mingw.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212

1313
static const int delay[] = { 0, 1, 10, 20, 40 };
1414

15+
void open_in_gdb(void)
16+
{
17+
static struct child_process cp = CHILD_PROCESS_INIT;
18+
extern char *_pgmptr;
19+
20+
argv_array_pushl(&cp.args, "mintty", "gdb", NULL);
21+
argv_array_pushf(&cp.args, "--pid=%d", getpid());
22+
cp.clean_on_exit = 1;
23+
if (start_command(&cp) < 0)
24+
die_errno("Could not start gdb");
25+
sleep(1);
26+
}
27+
1528
int err_win_to_posix(DWORD winerr)
1629
{
1730
int error = ENOSYS;

compat/mingw.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,16 @@ int main(int argc, const char **argv) \
604604
} \
605605
static int mingw_main(c,v)
606606

607+
/*
608+
* For debugging: if a problem occurs, say, in a Git process that is spawned
609+
* from another Git process which in turn is spawned from yet another Git
610+
* process, it can be quite daunting to figure out what is going on.
611+
*
612+
* Call this function to open a new MinTTY (this assumes you are in Git for
613+
* Windows' SDK) with a GDB that attaches to the current process right away.
614+
*/
615+
extern void open_in_gdb(void);
616+
607617
/*
608618
* Used by Pthread API implementation for Windows
609619
*/

0 commit comments

Comments
 (0)