Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* Re: process attaching gdb to itself
@ 2003-09-28 22:35 Michael Elizabeth Chastain
  2003-09-28 22:45 ` Andrew Cagney
  0 siblings, 1 reply; 8+ messages in thread
From: Michael Elizabeth Chastain @ 2003-09-28 22:35 UTC (permalink / raw)
  To: aurelien.chanudet, gdb

Hello,

  char cmd [256];
  sprintf (cmd, "gdb attach %d", getpid ());
  system (cmd);

'system' waits for the program that it calls to finish.
Thus, your program is waiting for gdb to finish before it
does anything.

Try the appended program which uses raw fork/exec.
It works for me on red hat linux 8, native i686-pc-linux-gnu.

If you try to do this in production code then you are likely
to run into a blizzard of race conditions, error cases,
and signal handling problems.  If you are doing this as a learning
experience, that's great -- read up on 'man fork' and
'man execlp', and check out a book on Linux systems programming.

Michael C
GDB QA Guy

===

  #include <stdio.h>

  int main ()
  {
    int pid = fork ();
    if (pid == -1)
    {
      /* error */
      fprintf (stderr, "fork error\n");
      exit (2);
    }

    if (pid != 0)
    {
      /* parent process */
      char spid [256];
      sprintf (spid, "%d", pid);
      execlp ("gdb", "gdb", "a.out", spid, NULL);
      /* execlp returns only if error */
      fprintf (stderr, "execlp error\n");
      _exit (2);
    }

    /* child process */
    sleep (5);
    printf ("hello hacker\n");
    return 0;
  }


^ permalink raw reply	[flat|nested] 8+ messages in thread
* process attaching gdb to itself
@ 2003-09-28 20:45 Aurelien Chanudet
  0 siblings, 0 replies; 8+ messages in thread
From: Aurelien Chanudet @ 2003-09-28 20:45 UTC (permalink / raw)
  To: gdb

Hi all,

How can I have a running process gracefully attach gdb to itself ? I've 
tried something like :

char cmd [256];
sprintf (cmd, "gdb attach %d", getpid ());
system (cmd);

which has the desired effect. However, once in gdb, the debugger is 
stuck in wait4(2) and I cannot  find a way to resume execution.

Thanks,
Aurelien


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2003-09-29 14:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-28 22:35 process attaching gdb to itself Michael Elizabeth Chastain
2003-09-28 22:45 ` Andrew Cagney
2003-09-29  2:21   ` Daniel Jacobowitz
2003-09-29 13:23     ` Andrew Cagney
2003-09-29 13:30       ` Daniel Jacobowitz
2003-09-29 14:54         ` Andrew Cagney
2003-09-29 14:59           ` Daniel Jacobowitz
  -- strict thread matches above, loose matches on Subject: below --
2003-09-28 20:45 Aurelien Chanudet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox