Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode on Windows: gdb doesn't break on 'throw' but breaks on regular exceptions

While debugging my code, gdb catches regular exceptions (such as dividing by zero) but not custom ones, thrown with throw.

I'd expect vscode to jump where the exception is thrown, so i can investigate. Instead, the exception causes the application to terminate and the debugger to close.

I tested it with some dummy code:

compiled with: g++ -g main.cpp -o test.exe

#include <stdexcept>
#include <iostream>

void test()
{
    throw std::invalid_argument( "received negative value" );
}

int main(int argc, char const *argv[]) {
    std::cin.get();

    // int c = 1 / 0;
    test();

    std::cout << "v";

    std::cin.get();
    return 0;
}

The output:

terminate called after throwing an instance of 'std::invalid_argument'
  what():  received negative value

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

My environment:

  • Windows 7 64bit
  • VSCode 1.40.2
  • MinGW

launch.json

        {
            "type": "cppdbg",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceRoot}/test.exe",
            "cwd": "${workspaceRoot}",
            "miDebuggerPath": "C:/MinGW/bin/gdb.exe",
            "MIMode": "gdb",
            "preLaunchTask": "Compile",
            "externalConsole": true
        }
like image 449
Sewbacca Avatar asked Dec 10 '19 22:12

Sewbacca


People also ask

Can you use GDB in VS Code?

Visual Studio Code supports the following debuggers for C/C++ depending on the operating system you are using: Linux: GDB. macOS: LLDB or GDB.

How do I enable breakpoints in VS Code?

Breakpoints can be toggled by clicking on the editor margin or using F9 on the current line. Finer breakpoint control (enable/disable/reapply) can be done in the Run and Debug view's BREAKPOINTS section.

How do you Debug using breakpoints in Visual Studio code?

Set breakpoints in source code To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.


1 Answers

For more info about catch see https://sourceware.org/gdb/onlinedocs/gdb/Set-Catchpoints.html#Set-Catchpoints

add to launch.json:

{
    ...
    "setupCommands": [
        /*{
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        },*/
        {
            "description": "Enable break on all exceptions",
            "text": "catch throw",
            "ignoreFailures": true
        }
    ],
    ...
}
like image 78
OwnageIsMagic Avatar answered Nov 14 '22 21:11

OwnageIsMagic