1

What I am trying to do is fork a process, exec a new executable in the child process, let the parent terminate and attach to the child process by GDB to debug this child process. The reason I am using an initial parent process to fork a child(debuggable) process is that I want to set some environment properties for this child process which requires some system calls into the linux kernel and do not want to modify the GDB to do so. I tried and found that I can attach to the child process through GDB after the parent process has terminated but in that case the child is already running after the exec() system call. The problem is that I cannot use ptrace(PTRACE_TRACEME) for the child, which causes it to stop at first instruction because in that case GDB cannot attach to it, as it is already traced by its parent. How can I stop the child process after setting up its environment so that it is stopped at its first instruction when i do exec()?

Jina Lee
  • 119
  • 2
  • 10

1 Answers1

2

I figured out a way that fulfilled my need and I am posting it as it might be helpful to someone else. After forking, setting up the necessary environment, just before exec'ing the program to be debugged, send a stop signal to itself {kill (getpid(), SIGSTOP)}. Let the parent not wait for the child process and terminate. So that there is only one pid. Open GDB attach to it using "gdb -p pid". The stop signal will be delivered to GDB. Continue the process by giving command sig 0 at GDB prompt. The child process will continue and exec the debuggable program next and you may debug it normally now.

Jina Lee
  • 119
  • 2
  • 10