How to debug a forked child process using CLion
Asked Answered
F

4

34

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?

Frodin answered 25/3, 2016 at 13:38 Comment(1)
Possible duplicate of How set follow-fork-mode as child in debuger using CMakeStandin
D
25

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.

Dramaturgy answered 24/3, 2018 at 18:6 Comment(1)
Since this is a popular answer, to avoid entering these commands every time you can create this file ~/.gdbinit and add these commands inside (each in a separate line). This will probably only take effect if you are using your system's gdb.Footmark
M
11

Use the GDB tab of the debugger and enter:

set detach-on-fork off
set follow-fork-mode child

enter image description here

Melissamelisse answered 2/11, 2019 at 19:11 Comment(0)
T
2

according to this link debugger: Support multi-process targets It's still not supported in Clion. set detach-on-fork off will add a thread to the frame, but you still can't control the child process. And Clion "treats exit of one of the inferiors as end of debugging session even-though others are still running".

I think there is a same issue with vscode.

Timework answered 8/3, 2022 at 15:51 Comment(0)
T
0

Ensure that you set set follow-fork-mode child before the fork.

Tribunal answered 5/6, 2016 at 16:32 Comment(3)
I confirmed this approach before and it didn't work. One can only debug through command line interface in this way instead of inside CLion.Frodin
I'm experiencing that CLion disconnects remote debugging sessions when GDB follows the child process.Icj
@JaimeHablutzel In that case you also need to set detach-on-fork off.Influx

© 2022 - 2024 — McMap. All rights reserved.