嘿, 我是Mofei!
How to Debug Node.js C++ Addon Modules Using VS Code

As one of the most popular development tools currently, I have become reliant on VS Code since I started using it. Recently, due to project requirements, I have been working with some C++ plugins for Node.js, so I would like to document how to debug C++ modules using VS Code.

1. Setting Up the Debugging Module in VS Code

Setting up the debugging module in VS Code is very easy, and there are many ways to do it. Below are two commonly used methods:

  • Method 1: Hold down Cmd+Shift+P to open the command palette, type open launch.json in the command palette, and then select C++.
  • Method 2: Find the debugging button in the left sidebar, then click the gear icon ⚙ at the top of the opened sidebar, and select C++.

howtodebug

Both of the above methods will create a template file named launch.json that looks like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb"
        }
    ]
}

This file is mainly used to define which program you want to start debugging with. For example, if we want to debug code run in the following way node test/vtshaver.test.js (running the test/vtshaver.test.js file with Node), we just need to change the program in the configuration to the absolute path of Node (you can use which node to find the current version of Node's path). After setting up Node, we need to configure the launch argument args. Here, we are launching the test/vtshaver.test.js file, so we need to find the absolute path of this file and configure it accordingly.

Additionally, since this is a C++ module, we need to compile the C++ code before running it. We can configure this using the preLaunchTask field, setting it to npm: build:dev. This way, every time we start the debug task, VS Code will automatically execute the corresponding command.

"preLaunchTask": "npm: build:dev",

The final configuration file might look like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "preLaunchTask": "npm: build:dev",
            "program": "/Users/mapbox-mofei/.nvm/versions/node/v8.11.3/bin/node",
            "args": ["/Users/mapbox-mofei/dev/mapbox/vtshaver/test/vtshaver.test.js"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb"
        }
    ]
}

Now open the corresponding C++ file and click on the blank space next to the line numbers to add a breakpoint (you will see a small green circle appear to the left of the line number). Then return to the debug window (click the debug button in the left navigation bar), find the green launch button at the top, and click it!

howtodebug

If you can run it correctly, then congratulations! You can now use VS Code to debug C++ code. The program will automatically stop at the breakpoint, and you can hover over any relevant variable to view its contents. If you want the program to continue, just click the navigation buttons at the top.

howtodebug

THE END

More Articles You Might Be Interested In

Got any insights on the content of this post? Let me know!

avatar

Mofei's Friend (Click to edit)

What's on your mind today?

HI. I AM MOFEI!

NICE TO MEET YOU!