This is the mail archive of the gdb@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: process attaching gdb to itself


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;
  }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]