Skip to content

Commit 45f8845

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 5af6eeb commit 45f8845

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

compat/mingw.c

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414

1515
#define HCAST(type, handle) ((type)(intptr_t)handle)
1616

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

compat/mingw.h

+10
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,16 @@ static int mingw_main(c,v)
733733

734734
#endif
735735

736+
/*
737+
* For debugging: if a problem occurs, say, in a Git process that is spawned
738+
* from another Git process which in turn is spawned from yet another Git
739+
* process, it can be quite daunting to figure out what is going on.
740+
*
741+
* Call this function to open a new MinTTY (this assumes you are in Git for
742+
* Windows' SDK) with a GDB that attaches to the current process right away.
743+
*/
744+
extern void open_in_gdb(void);
745+
736746
/*
737747
* Used by Pthread API implementation for Windows
738748
*/

0 commit comments

Comments
 (0)