Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a forked child process using CLion

Tags:

I was debugging a Linux C program with CLion's internal debugger (which is gdb on Linux). The program forked a child process that was supposed to get suspended at the break point I set. But the debugger didn't stop there even if I had entered set follow-fork-mode child inside the gdb panel. So how can I make that happen with CLion?

like image 958
DYS Avatar asked Mar 25 '16 13:03

DYS


People also ask

How do I debug a forked process?

There is an alternative way of debugging the child process. After fork() is executed, put a sleep() call in the code where the child executes, get the PID of the child using the ps utility, then attach the PID. Now, you can debug the child process, like any other process.

How do you attach a process to CLion?

Press Ctrl+Alt+F5 or choose Run | Attach to Process from the main menu. CLion will show the list of the running local processes. Select the process to attach to.


2 Answers

I followed answer posted by @MarkusParker, but instead of set auto-load safe-path / I used set detach-on-fork off to prevent disconnect from child process. This instruction works for me:

  1. Set a break point at the beginning of your program (ie. the parent program, not the child program).

  2. Start the program in the debugger.

  3. Go to the debugger console (tab with the label gdb) in clion and enter set follow-fork-mode child and set detach-on-fork off.

  4. Continue debugging.

like image 60
annstriganova Avatar answered Sep 16 '22 15:09

annstriganova


Use the GDB tab of the debugger:

enter set follow-fork-mode child and set detach-on-fork off enter image description here

like image 36
user1095332 Avatar answered Sep 17 '22 15:09

user1095332