Debug fork() in eclipse cdt
Asked Answered
T

4

12

I'm trying to debug some fork() mechanism with eclipse cdt (Juno). I wrote the program in C.

  if( -1 == (pid = fork()) ) /* error */
    goto cleanup;
  else if ( 0 == pid ) /* child */
  {
    execlp("gcc", "gcc", cFilePath, "-o" , GCC_OUTPUT_FILE_NAME, NULL);
    goto cleanup; /* Arrives here only on error! */
  }
  else if (pid > 0) /* parent - checks: correct pid returns, returns normally, with exit status = 0*/
  {
      returnedpid = wait(exitStatus);
      if( pid != returnedpid || exitStatus == NULL || !WIFEXITED(*exitStatus) || !WEXITSTATUS(*exitStatus) )
          goto cleanup;
  }

I tried to add "set follow-fork-mode child" as said here: http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2006-02/msg00435.html

1. How can I debug the code section where (0==pid)?

2. When I get to the wait statement, the debugger just return immediately, isn't wait() suppose to suspend till the child returns? Why is it return immediately?

Toon answered 17/3, 2013 at 22:43 Comment(0)
H
14

Your problems are probably due to "detach-on-fork" being set to off. DSF sets this by default (it's also gdb's default).

1) Put a breakpoint onto lines with "execlp..." and "returnedpid =...". 2) In debug configuration enable "non-stop mode" and "automatically debug forked process". 3) Start debug session. You will hit a breakpoint either in child or parent. Now see debug view.

Debug view

You will notice your binary shows two threads.

4) Click on one or the other (lines with main() in above image) to switch debug context.

Hm answered 18/3, 2013 at 13:17 Comment(3)
Hi dbrank0. I followed your example and find it working (but you need to force the "Automatically debug forked process" option) but with a problem anyway. I'm "daemonizing" my process so, following gurus indication, I'm forking twice. It seems to me that the first fork is followed properly but as soon as I try to fork the second time the whole debugger hangs and I can't follow none of the two processes. thanks anyway but... any Idea?Remission
No, sorry, but do look if any of switches explained in "debugging forks" chapter (sourceware.org/gdb/onlinedocs/gdb/Forks.html) helps.Hm
Works perfectly, even follows execvp (swapn new process) after fork. Nice.Replace
O
1

It might be because the init process reaps the child before you go to wait. Try blocking sigchld as you go into fork and then unblock the signals after your forks/execs. Maybe that should give you some idea as to what is exactly happening.

Using sigprocmask should help you.

Onyx answered 18/3, 2013 at 2:37 Comment(0)
B
1
  1. In debug configuration window -> Debugger -> Check "Automatically debug forked processes"
  2. Open breakpoint window -> View menu -> Add Event Breakpoint C/C++ -> "fork called"
  3. Now once the event breakpoint occurred, press F6 -> you'll see new process in debug window
  4. Choose new process and press F6 - You are in the child
  5. Enjoy
Beutner answered 21/5, 2014 at 16:29 Comment(1)
thanks! note that the UI is kinda bad so you may not see check boxes. Just click the text to enable the debug forked processes.Mckenzie
G
0

Just to add to the perfect answer of @dbrank0, the Debug Configuration window is located under the Run menu. You can find the mentioned configurations under the Debugger tab. enter image description here

Guttate answered 13/10, 2023 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.