Reputation: 2533
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?
Upvotes: 12
Views: 7094
Reputation: 13
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.
Upvotes: 0
Reputation: 4677
Upvotes: 1
Reputation: 9476
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.
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.
Upvotes: 15
Reputation: 21
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.
Upvotes: 1