Ahlam Javed
Ahlam Javed

Reputation: 21

Debugging C/C++ Language Not Hitting Breakpoints

I have the following file overflow.c that I'm trying to debug through breakpoints in Visual Studio Code macOS:

#include <stdio.h>

int main(void) {
    int n = 1;
    for (int i = 0; i < 64; i++)
    {
        printf("%i\n", n);
        n = n * 2;
    }
    return 0;
}

I've built it by typing make overflow in the terminal, which returns

cc   overflow.o   -o overflow

And I can do ./overflow in the terminal to run it, which works. I have the C/C++ extension by Microsoft installed. My launch.json looks like the following:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C Run",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/overflow",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

When I debug using the "C Run" configuration, it runs my entire code without hitting any of my breakpoints (found here)

The "C Attach" is for attaching to an already-running app, which isn't applicable here. I've added the following to my PATH:

PATH="/Applications/Xcode.app/Contents/Developer/usr/bin:${PATH}"

My debug console after debugging "C Run" config loads bunch of symbols, returns output from my print statements, and ends with

The program '/Users/ahlam/Downloads/workspace/overflow' has exited with code 0 (0x00000000).

EDIT: I've also tried it with C++ and it has the same behavior. Made a hello.cpp, built using g++ hello.cpp and debugging just ran the entire code without hitting any breakpoints.

Any help is appreciated.

Upvotes: 1

Views: 1173

Answers (1)

azizj
azizj

Reputation: 3777

You need to generate source-level debug information, which you can do by using the -g flag in clang:

clang -g overflow.c -o overflow

Do that instead of make overflow. You'll see a folder called overflow.dSYM in your directory. Debugging should now work.

Upvotes: 3

Related Questions