cvisual-studio-code

crtexe.c - Issue about debugging C/C++ files in Visual Studio Code


Consider:

Enter image description here

While debugging C/C++ files with Visual Studio Code, the program flow enters to crtexe.c when pressing the 'step over' or 'step into' key from the line with '}' of the main() function.

Enter image description here

However, when continue debugging in crtexe.c, the flow just stops at if (!managedapp) line and doesn't go back to the main() function.

If I press 'step out' key in the line '}' of main(), then an error message "Unable to step out. Operation failed with error code 0x80004004" comes out.

I tested same code in another IDE, like CLion or NetBeans, but these issues didn't occur.

I'm using MinGW-w64 GCC and GDB. How do I resolve this issue?

Sample code (file main.c)

#include <stdio.h>

int main()
{
    printf("Call main()\n");

    int num1 = 1;
    int num2 = 20;
    int num3 = num1 + num2;

    printf("%d\n", num3);

    return 0;
}

File launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "GDB Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "C:/Users/.../main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "C:/Users/...",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/msys2/mingw64/bin/gdb.exe",
            "sourceFileMap": {
                "/c/": "C:/"
            }
        }
    ]
}

Solution

  • From the image I see the call before the condition:

    mainret = main(argc, argv);
    

    You declared the main function:

    int main();
    

    Since the program was successfully linked the program is in C language. Thus the stack frame is corrupted after main exited. On Windows it is a default call convention that callee function is responsible for cleaning up function arguments from the stack and caller gets invalid stack frame (stack register) after main. You should declare main function properly:

    int main(int argc, char** argv);