21. August 2020

Common mistake why GDB debugger does not work in Visual Studio Code

Visual Studio Code has great C/C++ support and it’s possible to debug a project with it.

Visual Studio Code can use gdb as a debugger.

If you have your project with a binary and you press F5 to launch the debugger, then Visual Studio will prompt you to configure GDB launcher in launch.json.

Here is sample launch.json for binary my_application on Linux:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/my_application",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }  
    ]
}

Just define a path to your binary, save the file and press F5.

Still, it might happen that debugger does not stop at your break point. Why?

There is one very common mistake. People often forget to add -g for gcc parameter when building the project.

GCC without parameter -g will build a release version of the project without debug information. To fix the problem just add -g to your Makefile, clean, and rebuild the project.

23. June 2014

How to debug Gradle script

First of all: do not use daemon mode for debugging.

IntelliJ Idea is automatically spawning daemon when you start any Gradle task. You have to attach to remote process.

I wrote¬†small “How to debug” based on info from forums.gradle.org.

You’ll need to set GRADLE_OPTS environment variable to:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005

Here is example in PowerShell:

01-command-line-options

$env:GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

Configure Remote debugging in Idea:

02-configure-remote

You do not need to change anything in default configuration.

Now return to command line with your project and invoke task. Gradle will automatically stop and it waits for debugger.

03-start-process

Attach debugger

04-attach-to-process

Here we go :-)

05-break-point

There is small limitation. You can stop code execution outside DSL e.g. in class method (line 3). You won’t be able to stop execution in Gradle DSL line 10.

You can download sample build script from Github.