Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Kevin Pouget <kevin.pouget@gmail.com>
To: gdb@sourceware.org
Subject: Re: GDB and LD_PRELOAD library-call interception
Date: Mon, 04 Apr 2011 13:35:00 -0000	[thread overview]
Message-ID: <BANLkTimUXk989FLVRpxOpyO5cXfbNSaG8Q@mail.gmail.com> (raw)
In-Reply-To: <BANLkTi=j6+-85R4B+N1Nd5kb9bkEZfsT9A@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2437 bytes --]

Here is a *prototype* patch of what I discussed earlier, which allows
to follow the N-th fork of a wrapper.

(my) `xterm' first forks /usr/libexec/utempter/utempter before
starting the program to debug, so it's skipped, then GDB follows the
forked child instead of the parent (I still need to figure out how not
to create a new inferior in this case).

C-c in the xterm is not intercepted by GDB, I don't know if that's a
bug or a feature;
however C-c in GDB terminal kills the xterm instead of stopping the
debuggee ... I guess it's a matter of terminal ownership, the signal
is just not sent to the right process (that's the opposite of Xavier's
problem :)


let me know what you think about it, I'll fix the bugs if it is seem
interesting for the community


Kevin

On Sun, Apr 3, 2011 at 12:54 PM, Xavier de Gaye <xdegaye@gmail.com> wrote:
>
> On Thu, Mar 31, 2011 at 2:16 PM, Jan Kratochvil wrote:
> >
> > It is not such straightforward, GDB expects the PID it has spawned will be
> > debugged while with xterm the process being debugged is its child.
> >
> > I guess you can write a small C helper which will:
> >  * create new pty
> >  * change its fds 0/1/2 to the slave of this pty
> >  * fork xterm -e own-helper-part pty-unique-id
> > In own-helper-part interconnect the pty master part and its fds 0/1/2.
> >
>
>
> The attached files 'xterm_wrapper.py' and 'interconnect_pty.py' are a
> raw implementation written in python of the above scheme. In gdb do:
>
>    set exec-wrapper python xterm_wrapper.py
>
> The problem with the implementation is that the debuggee cannot set
> the slave pty as its controlling terminal without forking (set
> SET_CONTROLLING_TERMINAL to True in 'xterm_wrapper.py' in order to do
> that, but then exec-wrapper cannot be used). So that it is not
> possible to interrupt the debuggee with a 'C-c' character.
>
> On the other hand the 'interconnect_pty.py' helper used by
> 'xterm_wrapper.py' can also be used by itself in stand-alone. When run
> without arguments, it creates a pty and prints the slave pty name so
> that it can be used in gdb with:
>
>    set inferior-tty /dev/pts/nn
>
> It can also be made to spawn gdb in a new xterm and set correctly the
> '-tty' gdb option with tne new pty name (kind of the reverse of the
> initial scheme):
>
>    python interconnect_pty.py --exec gdb --args '/path/to/debuggee'
>
> Xavier

[-- Attachment #2: gdb-xterm.diff --]
[-- Type: application/octet-stream, Size: 2747 bytes --]

diff --git a/gdb/fork-child.c b/gdb/fork-child.c
index bb173e7..9971c83 100644
--- a/gdb/fork-child.c
+++ b/gdb/fork-child.c
@@ -34,7 +34,7 @@
 #include "command.h" /* for dont_repeat () */
 #include "gdbcmd.h"
 #include "solib.h"
-
+#include "linux-nat.h"
 #include <signal.h>
 
 /* This just gets used as a default if we can't find SHELL.  */
@@ -421,11 +421,20 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env,
   return pid;
 }
 
+/* Follow the NFORK-th child.  */
+#define NFORK 2
+static int 
+I_want_follow_fork_child() {
+  static int fork = 0 ;
+  return (++fork % (NFORK)) == 0 ;
+}
+
 /* Accept NTRAPS traps from the inferior.  */
 
 void
 startup_inferior (int ntraps)
 {
+  int enabled = 0 ;
   int pending_execs = ntraps;
   int terminal_initted = 0;
   ptid_t resume_ptid;
@@ -451,16 +460,37 @@ startup_inferior (int ntraps)
       memset (&ws, 0, sizeof (ws));
       event_ptid = target_wait (resume_ptid, &ws, 0);
 
+      if (!enabled) {
+        linux_enable_event_reporting (inferior_ptid);
+        enabled = 1 ;
+      }
+      
       if (ws.kind == TARGET_WAITKIND_IGNORE)
 	/* The inferior didn't really stop, keep waiting.  */
 	continue;
 
       switch (ws.kind)
 	{
+          case TARGET_WAITKIND_FORKED:
+          case TARGET_WAITKIND_VFORKED: {
+            int follow_the_child = I_want_follow_fork_child() ;
+            int old_detach_fork = detach_fork ;
+            
+            /* Force GDB to detach the forks. */
+            detach_fork = 1 ;
+            inferior_thread ()->pending_follow = ws ;
+            
+            if (target_follow_fork(follow_the_child) != 0) {
+               warning("couldn't follow fork child") ;
+            } else if (follow_the_child) {
+              /* Resume the new child PID. */
+              resume_ptid = inferior_ptid ;
+            }
+            detach_fork = old_detach_fork ;
+            break ;
+          }
 	  case TARGET_WAITKIND_SPURIOUS:
 	  case TARGET_WAITKIND_LOADED:
-	  case TARGET_WAITKIND_FORKED:
-	  case TARGET_WAITKIND_VFORKED:
 	  case TARGET_WAITKIND_SYSCALL_ENTRY:
 	  case TARGET_WAITKIND_SYSCALL_RETURN:
 	    /* Ignore gracefully during startup of the inferior.  */
diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c
index 7f654af..3eddea4 100644
--- a/gdb/linux-fork.c
+++ b/gdb/linux-fork.c
@@ -628,6 +628,11 @@ checkpoint_command (char *args, int from_tty)
   pid_t retpid;
   struct cleanup *old_chain;
 
+  /* Ensure that the inferior is not multithreaded. */
+  update_thread_list () ;
+  if (thread_count () > 1)
+    error(_("checkpoint: can't checkpoint multiple threads.")) ;
+  
   /* Make the inferior fork, record its (and gdb's) state.  */
 
   if (lookup_minimal_symbol ("fork", NULL, NULL) != NULL)

  parent reply	other threads:[~2011-04-04 13:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-31  8:25 Kevin Pouget
2011-03-31  8:47 ` Jan Kratochvil
2011-03-31  9:47   ` Kevin Pouget
2011-03-31 12:16     ` Jan Kratochvil
2011-03-31 15:07       ` Tom Tromey
2011-04-03 16:54       ` Xavier de Gaye
     [not found]         ` <BANLkTi=j6+-85R4B+N1Nd5kb9bkEZfsT9A@mail.gmail.com>
2011-04-04 13:35           ` Kevin Pouget [this message]
2011-03-31 15:06     ` Tom Tromey
2011-03-31 15:55       ` Kevin Pouget

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=BANLkTimUXk989FLVRpxOpyO5cXfbNSaG8Q@mail.gmail.com \
    --to=kevin.pouget@gmail.com \
    --cc=gdb@sourceware.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox