Skip to contentSkip to conversation

mingw: add a helper function to attach GDB to the current process

PATCH New Johannes Schindelin via GitGitGadget 1 participant 1 message Feb 13, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mingw: add a helper function to attach GDB to the current process

From: Johannes Schindelin <[email protected]>

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.

---
win32: make it easy to start the GNU debugger at a certain source line

In particular when trying to debug issues in processes with redirected stdin and/or stdout, it can be quite a challenge to use an interactive debugger.

Help this, by introducing a function that is purely meant for debugging
purposes: open_in_gdb(). It will open a new terminal window, attach gdb to the current process in it, and sleep. That way, the above-mentioned debugging problem can be solved simply by inserting the statement open_in_gdb(); at a specific source code line, then recompiling Git and then running e.g. the regression test that failed.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-150%2Fdscho%2Fopen-in-gdb-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-150/dscho/open-in-gdb-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/150

 compat/mingw.c | 13 +++++++++++++
 compat/mingw.h | 10 ++++++++++
 2 files changed, 23 insertions(+)
compat/mingw.c +13 -0
index 8141f77189..615e9b64aa 100644
@@ -13,6 +13,19 @@
 
 static const int delay[] = { 0, 1, 10, 20, 40 };
 
+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;
compat/mingw.h +10 -0
index 30d9fb3e36..26d3296d56 100644
@@ -584,6 +584,16 @@ int main(int argc, const char **argv) \
 } \
 static int mingw_main(c,v)
 
+/*
+ * 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
  */

base-commit: 8104ec994ea3849a968b4667d072fedd1e688642

Signature · 1 line
gitgitgadget