Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code, debugging child process not working

I have this exact question: https://github.com/Microsoft/vscode-cpptools/issues/511

But the solution there does not work.

I've tried the same simple example code,

#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;
}

I compile using -g, and my launch.json file is shown below.

I click debug, I set the breakpoint to the first line. At the first stop, I enter -exec set follow-fork-mode child under the debug console, it gives me back =cmd-param-changed,param="follow-fork-mode",value="child". Then it just doesn't work. It goes to the parent process (pid shows not as 0). I've tried setting it in the launch.json file itself using {"text": "-gdb-set follow-fork-mode child"}, but that doesn't work either.

{
        "version": "0.2.0",
        "configurations": [

        { 
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "${workspaceRoot}/a.exe",
            "processId": "${command:pickProcess}",
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe"
        },
            {
                "name": "(gdb) Launch",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceRoot}/a.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceRoot}",
                "environment": [],
                "externalConsole": true,
                "MIMode": "gdb",
                "miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }
like image 407
Ch. Pla Avatar asked Oct 08 '17 05:10

Ch. Pla


1 Answers

I got the same problem and I solved putting this :


 "setupCommands": [
                    {
                        "description": "Enable funcy printing to gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    },
                    {   "description":"In this mode GDB will be attached to both processes after a call to fork() or vfork().",
                        "text": "-gdb-set detach-on-fork off",
                        "ignoreFailures": true
                    },
                    {   "description": "The new process is debugged after a fork. The parent process runs unimpeded.",
                        "text": "-gdb-set follow-fork-mode child",
                        "ignoreFailures": true
                    }
                ],

like image 192
Abellán Avatar answered Oct 17 '22 00:10

Abellán