All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH] mingw: add a helper function to attach GDB to the current process
Date: Thu, 13 Feb 2020 21:49:53 +0000	[thread overview]
Message-ID: <pull.150.git.1581630593304.gitgitgadget@gmail.com> (raw)

From: Johannes Schindelin <johannes.schindelin@gmx.de>

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>
---
    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(+)

diff --git a/compat/mingw.c b/compat/mingw.c
index 8141f77189..615e9b64aa 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -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;
diff --git a/compat/mingw.h b/compat/mingw.h
index 30d9fb3e36..26d3296d56 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -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
-- 
gitgitgadget

                 reply	other threads:[~2020-02-13 21:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.150.git.1581630593304.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.