{"thread":{"id":"52800","subject":"[PATCH] mingw: add a helper function to attach GDB to the current process","startedAt":"2020-02-13T21:49:59Z","lastAt":"2020-02-13T21:49:59Z","messageCount":1,"participants":["Johannes Schindelin via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"391702","messageId":"pull.150.git.1581630593304.gitgitgadget@gmail.com","threadId":"52800","inReplyTo":null,"subject":"[PATCH] mingw: add a helper function to attach GDB to the current process","fromName":"Johannes Schindelin via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-02-13T21:49:53Z","receivedAt":"2020-02-13T21:49:59Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"From: Johannes Schindelin <johannes.schindelin@gmx.de>\n\nWhen debugging Git, the criss-cross spawning of processes can make\nthings quite a bit difficult, especially when a Unix shell script is\nthrown in the mix that calls a `git.exe` that then segfaults.\n\nTo help debugging such things, we introduce the `open_in_gdb()` function\nwhich can be called at a code location where the segfault happens (or as\nclose as one can get); This will open a new MinTTY window with a GDB\nthat already attached to the current process.\n\nInspired by Derrick Stolee.\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n    win32: make it easy to start the GNU debugger at a certain source line\n    \n    In particular when trying to debug issues in processes with redirected\n    stdin and/or stdout, it can be quite a challenge to use an interactive\n    debugger.\n    \n    Help this, by introducing a function that is purely meant for debugging\n    purposes: open_in_gdb(). It will open a new terminal window, attach gdb\n    to the current process in it, and sleep. That way, the above-mentioned\n    debugging problem can be solved simply by inserting the statement \n    open_in_gdb(); at a specific source code line, then recompiling Git and\n    then running e.g. the regression test that failed.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-150%2Fdscho%2Fopen-in-gdb-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-150/dscho/open-in-gdb-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/150\n\n compat/mingw.c | 13 +++++++++++++\n compat/mingw.h | 10 ++++++++++\n 2 files changed, 23 insertions(+)\n\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex 8141f77189..615e9b64aa 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -13,6 +13,19 @@\n \n static const int delay[] = { 0, 1, 10, 20, 40 };\n \n+void open_in_gdb(void)\n+{\n+\tstatic struct child_process cp = CHILD_PROCESS_INIT;\n+\textern char *_pgmptr;\n+\n+\targv_array_pushl(&cp.args, \"mintty\", \"gdb\", NULL);\n+\targv_array_pushf(&cp.args, \"--pid=%d\", getpid());\n+\tcp.clean_on_exit = 1;\n+\tif (start_command(&cp) < 0)\n+\t\tdie_errno(\"Could not start gdb\");\n+\tsleep(1);\n+}\n+\n int err_win_to_posix(DWORD winerr)\n {\n \tint error = ENOSYS;\ndiff --git a/compat/mingw.h b/compat/mingw.h\nindex 30d9fb3e36..26d3296d56 100644\n--- a/compat/mingw.h\n+++ b/compat/mingw.h\n@@ -584,6 +584,16 @@ int main(int argc, const char **argv) \\\n } \\\n static int mingw_main(c,v)\n \n+/*\n+ * For debugging: if a problem occurs, say, in a Git process that is spawned\n+ * from another Git process which in turn is spawned from yet another Git\n+ * process, it can be quite daunting to figure out what is going on.\n+ *\n+ * Call this function to open a new MinTTY (this assumes you are in Git for\n+ * Windows' SDK) with a GDB that attaches to the current process right away.\n+ */\n+extern void open_in_gdb(void);\n+\n /*\n  * Used by Pthread API implementation for Windows\n  */\n\nbase-commit: 8104ec994ea3849a968b4667d072fedd1e688642\n-- \ngitgitgadget\n"}]}