Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug a C Child Process Using VS Code

I am using VScode with the following version details:

Version: 1.73.0 (user setup) Commit: 8fa188b2b301d36553cbc9ce1b0a146ccb93351f Date: 2022-11-01T15:34:06.111Z Electron: 19.0.17 Chromium: 102.0.5005.167 Node.js: 16.14.2 V8: 10.2.154.15-electron.0 OS: Windows_NT x64 10.0.22000 Sandboxed: No

I am unable to debug a child process when I use fork().

I tried looking for a way to do so, and heard about this extension for Visual Studio: https://marketplace.visualstudio.com/items?itemName=vsdbgplat.MicrosoftChildProcessDebuggingPowerTool2022&ssr=false#overview

I tried checking in the extensions section of VSCode but didn't find it. So, I decided to manually install is by downloading the vsix file. When I tried installing it by using the "Install from VSIX" option in the extension manager, I get this error:

extension/package.json not found inside zip.

I did some more Googling and saw something about Visual Studio being different from Visual Studio Code, and that might be the cause of the error in installation.

So, is there any way/alternative around this? How do I debug a child process in Visual Studio Code?

like image 574
Olamide Olanrewaju Avatar asked Sep 21 '26 06:09

Olamide Olanrewaju


1 Answers

This is something outside of the main guides from VS Code, although they vaguely mentioned about supporting this.

You can refer to the GDB's fork debug page for more info, here is what you can add into your .vscode/launch.json:

{
    "configurations": [
        {

            // ...

            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    // https://sourceware.org/gdb/onlinedocs/gdb/Forks.html
                    "description": "Fork follows Child process",
                    "text": "set follow-fork-mode child",
                    "ignoreFailures": true
                },
                {
                    // https://sourceware.org/gdb/onlinedocs/gdb/Forks.html
                    "description": "Fork will keep the other process attached to debugger",
                    "text": "set detach-on-fork off",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

If you're using LLDB or codeLLDB, here is the LLDB command that mirrors the above except the detach feature:

{
    "configurations": [
        {

            // ...

            "initCommands": [
                "settings set target.process.follow-fork-mode child"
            ]
        }
    ]
}

Edit

I haven't used MSVC personally, but if you're using MSVC debugger, you can try to use .childdbg 1 as a startup command, here is the reference page from Microsoft.

like image 72
Jonny Soe Avatar answered Sep 22 '26 20:09

Jonny Soe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!