vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

[GDB] follow-fork-mode child

Open ApoorvaJ opened this issue 9 years ago • 11 comments

GDB has a command called set follow-fork-mode child that instructs GDB to follow the forked process. I couldn't find a way to do this with this plugin.

I tried adding various permutations of "-exec set follow-fork-mode child" to the setup commands in the launch.json, but wasn't able to get it to work.

Is it possible to set this up from launch.json?

ApoorvaJ avatar Feb 21 '17 05:02 ApoorvaJ

@ApoorvaJ Do you have a simple code example I can try out for this?

pieandcakes avatar Feb 22 '17 18:02 pieandcakes

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    pid_t pid = fork();
    if (pid != 0) {
        printf("Main process\n");
    } else {
        printf("Forked process\n");
    }

    return 0;
}

Compile using the gcc -g fork.c option on command line on Linux to enable debug symbols. Open in gdb using gdb ./a.out Set breakpoints on both printfs. Try running. You'll only ever hit the main process breakpoint.

To hit the other one, type in set follow-fork-mode child into gdb, and then run again. You'll now only hit the forked process breakpoint.

I'd like to control this behaviour in the launch.json file while debugging via VS Code.

ApoorvaJ avatar Feb 22 '17 18:02 ApoorvaJ

@ApoorvaJ Thanks. I'll take a look and let you know.

pieandcakes avatar Feb 22 '17 19:02 pieandcakes

@ApoorvaJ We use gdb's MI command structure. This is the block you need to add to your launch.json section:

            "setupCommands": [
                {"text": "-gdb-set follow-fork-mode child"}
            ]

Note: There is a bug in the current release version that crashes and makes it so that this doesn't work. I have fixed it (and referenced the Pull Request) and it will be in our 0.10.2 release.

pieandcakes avatar Mar 01 '17 23:03 pieandcakes

@ApoorvaJ 0.10.2 is live. Closing this issue because it is resolved.

pieandcakes avatar Mar 02 '17 00:03 pieandcakes

can't switch to child process in VS code version 1.34.0

z7z8th avatar Jun 12 '19 00:06 z7z8th

Also, can't switch to child process in VS code version 1.35.0

z7z8th avatar Jun 12 '19 00:06 z7z8th

Version 1.37.1 I'm unable to even start the process. I get a write EPIPE error. I have ensured that I'm in fact loading the correct child source code. It runs fine in stand-alone node.

curtiskeisler avatar Sep 13 '19 15:09 curtiskeisler

@curtiskeisler yours sounds like a different issue . Please file a new issue with logging information.

pieandcakes avatar Sep 16 '19 17:09 pieandcakes

This is the block you need to add to your launch.json section:

            "setupCommands": [
                {"text": "-gdb-set follow-fork-mode child"}
            ]

Is this in the docs?

Trass3r avatar Oct 13 '20 17:10 Trass3r

How do you do this on modern VS Code? Where do I put the setupCommands option? Putting it in launch.json at the root of the GDB configuration object doesn't seem to work.

Edit: Nevermind, you just have to use "type": "cppdbg" instead of "type": "gdb".

jack-jjm avatar Jun 05 '22 13:06 jack-jjm