Debugger not working for C++ Visual Studio Code - c++

I am trying to debug my C++ code with gdb, but every time I set a breakpoint and press F5, it just doesn't do anything. I hover over the breakpoint and it says "Module containing this breakpoint has not yet loaded or the breakpoint address could not be obtained." even if I put a breakpoint right on main! Here is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
in addition, the little menu that pops up, the step over, step into, step out buttons are all greyed out. I can only pause, restart, or stop the debugging (that isn't happening).
I compile and run my program with this
g++ inputbuf.cc lexer.cc parser.cc -o a.out && a.out < tests/no_errors/test_01.txt > output.txt
Also, the double backslash for the gdb path "\\" vs "/" in launch.json doesn't do anything.

Related

How to set up code path when debug C++ in Visual Studio

I am following this official Visual Studio site to start developing C++ in Visual Studio: https://code.visualstudio.com/docs/languages/cpp#:~:text=C%2FC%2B%2B%20support%20for%20Visual%20Studio%20Code%20is%20provided,use%20the%20keyboard%20shortcut%20%28Ctrl%2BShift%2BX%29.%20Search%20for%20%27C%2B%2B%27. I have MinGW installed as suggested.
I can build the code and it runs correctly.
When I set a break point and run debug, the break point is ignored. The following messages appears several times in the Debug Console:
PS C:\Users\Cheng\Documents\Temp\HelloWorld> & 'c:\Users\Cheng\.vscode\extensions\ms-vscode.cpptools-1.13.9-win32-x64\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-ccgsybc5.exz' '--stdout=Microsoft-MIEngine-Out-bwaebfti.urx' '--stderr=Microsoft-MIEngine-Error-rwnjcwal.lr2' '--pid=Microsoft-MIEngine-Pid-mj2o2p2s.eil' '--dbgExe=/msys64/usr/bin/gdb.exe' '--interpreter=mi'
When I set "stopAtEntry" to true in lauch.json, I see the following log in the Debug Console indicating the correct code path:
Thread 1 "HelloWorld" hit Breakpoint 1, main () at C:\Users\Cheng\Documents\Temp\HelloWorld\HelloWorld.cpp:5
But a new file pop up, with empty content and the following path:
C:\Users\Cheng\Documents\Temp\HelloWorld\C\Users\Cheng\Documents\Temp\HelloWorld\HelloWorld.cpp
The content of the launch.json is the following:
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/msys64/usr/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
]
}
I think that it is just an issue of specifying the correct debug path.
Can anyone help me on this configuration issue? Very much appreciated your help.
CP

How to redirect the std output of a C++ program into a file in VSCode on Mac OS by "launch.json"?

I'm trying to let the output of a C++ program to go into a file, say, "image.ppm". Actually, using the following launch.json file works pretty well on my Ubuntu 20.04.
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/raytracing",
"args": [">image.ppm"],
"postDebugTask": "open_image_linux",
"stopAtEntry": false,
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"cwd": "${workspaceFolder}",
// "preLaunchTask": "C/C++: g++ build active file",
"preLaunchTask": "Build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
When I try to implement a similar procedure on Mac OS, something wried happened, here is my new launch.json file:
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/raytracing",
"args": [">image.ppm"],
"postDebugTask": "open_image_mac",
"stopAtEntry": false,
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"cwd": "${workspaceFolder}",
// "preLaunchTask": "C/C++: g++ build active file",
"preLaunchTask": "Build",
"setupCommands": [
{
"description": "Enable pretty-printing for lldb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-lldb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
As can be seen, I just changed the debugger from gdb to lldb since gdb doesn't natively support Apple Silicon right now. However, the image.ppm will not be created after the execution of the program, while the output of the program goes into the debug console, like this:
I think, intrinsically, "args": [">image.ppm"], should work, since it works smoothly on Linux. I guess the reason may be related to the different features of gdb and lldb. Since when I run the program in VSCode on Ubuntu, nothing will be shown in the debug console. But I don't know how could I solve this problem.

"program.exe doesn't exist" in VSCode when i try to use an external window

EDIT : the g++ is in my path, it isn't the problem.
I have been trying to use a launch.json to run my c++ programs in the external terminal, but every time it gives me an error program.exe doesn't exist although it works fine in the VS Code terminal.
Here is how the launch.json is configured:
{
"configurations": [
{
"name": "run and debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

What to in VScode do after launch JSON file C++

I'm learning a basic to c++ in Vscode but I'm having issues with debugging, I followed everything downloaded MinGW, edit the environment path variable value to C:\MingGW\Bin. I tried but when I try to run it opens a launch.JSON what do I do after that? When I run > start debugging I get two options, I click on C++(GDB/LLDB). It opens a JSON file it reads :
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
I don't know what do to next please help...

Unable to start debugging with GDB from VSCode because of error: Unexpected GDB output from command "-exec-run"

I keep getting Unexpected GDB output from command "-exec-run" error in the debug console while I try to run my c++ code which includes a custom class I made and takes a .txt file as an input in the same path.
I'm using the command g++ -g class.h class.cpp mycode.cpp -o mycode.exe in the terminal and just try to debug the code with F5.
I don't get this error when I'm executing a cpp file with no libraries included
Here is my launch.json file
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
I've heard downgrading gdb or g++ could help but I could not understand how can I downgrade them. If you think that's the solution a simple explanation for how to downgrade would be much appreciated.