VS code is ignoring the breakpoint in c++ debugging - c++

I am debugging a c++ code in VS Code but it doesn't stop on breakpoint and visualize the variables, watch and call stack, which it was supposed to do. Instead of this, it prints this in debug console:
Breakpoint 1, 0x000000000040074a in main ()
[Inferior 1 (process 9445) exited normally]
The program '/home/hashir/x/a.out' has exited with code 0 (0x00000000)
here is launch.json file:
{
// 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": "/home/hashir/x/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "/home/hashir/x/",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

Compile the program using the -g tag along with g++/clang++.

This problem literally ruined my day. It turned out that all I had to do is just Terminal > Run Build Task or Ctrl+Shift + B and then start debugging.

I've figured out that if your source-code file name contains white spaces, like "Binary Search.cpp", VSCode will ignore the breakpoints regardless of "stop at entry" configuration. Removing the white spaces from my source files' names worked, although their paths contain white spaces. For example "C++ Exercises/BinarySearch.cpp" seems to be valid.
In this issue opened on GitHub, it is suggested that non-ascii characters might cause such problems.

The default launch.json file has this line:
"program": "${workspaceFolder}/a.out",
But this will run a.out to start debugging, and if you configured tasks.json file with only one cpp file, it will have lines like:
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
this will create an executable with the same name with your current file.
Either change the tasks.json to create an a.out file or change launch.json file with the no extension name of your current file.
launch.json:
"program": "${workspaceFolder}/<stripped current file name>",
OR
tasks.json:
"-o",
"${fileDirname}/a.out"

It seem something wrong with environment vars, try open vscode from "developer command prompt for VScode", then on prompt, type: code

Related

Debugging unable to open source files

Environment
Ubuntu 20.04.
VS Code 1.69.0 with extensions
VS Code launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Test debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/bazel-bin/app/mp_test",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
}
]
}
Note: I use bazel to build mediapipe and my app. Do not know if it matters or not.
bazel build --copt="-g" --strip="never" --define MEDIAPIPE_DISABLE_GPU=1 //app:mp_test
Problem description
I have a code:
mediapipe::CalculatorGraph graph;
graph.Initialize(config);
I set breakpoints on the second line. Start debugging. After F11 (step into) execution cursor stands on the line. I repeated several time F11 and at the end it goes into some standard library code (unique_ptr exactly). I tried to use Stack Trace and double click on one of its entries. Look at the screenshot. So, debugger cann't open the source file, because it looks into /proc/self/cwd/external folder which doesn't exists. Meanwhile IntelliSence can find the definition of the mediapipe::CalculatorGraph::Initialize in mediapipe project directory and my app was built successfully, means that compiler (linker) found everything. Why? How can I fix it?
Note: Folder /proc/self/cwd/ contains files from my app folder (BUILD, main.cpp, ...).

Unable to start debugging. Not implemented. Error on VS code

Hi i recently started using VS code for my C++ course. I am trying to get the debugger to work but even with hours of trying to figure out what's wrong i still get the following error "Unable to start debugging. Not implemented." I am a bit unsure on what that means let alone how to solve it. My launch.json file looks like this. Thank you in advance for the help.
{
// 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": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "Assigment_1.C++",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal"
}
]
}
I don't have enough Karma to post in the comment thread but I wanted to say that I don't think it matters that you're using vs code instead of visual studio. It says in the debug console that the C/C++ extionsion is compatible with both, and this is the .json that is automatically generated by vs code. I'm having the same issue trying to run C. I've reached out to microsoft support. Let me know if you've figured it out, thanks.
Alternatively, it seems to work if you just download the code runner extension and then right click and choose run code instead. At least it did for me using C. Still trying to figure out how to use it with the debugger however.
I had the same problem and I have solved this problem using these steps.
First create your C++ folder (mine is cpp test) and create the main.cpp file.
Then after making the main method just click f5 and the vs code will pop up a box asking whether C++(GDB) or C++(Windows).
refer the photo
Now click on C++(GDB/LLDB).
Now click on g++.exe (Build and Debug active file).
Now the vs code will generate the lanch.json file.
Then go to the main.cpp file, Just click f5 and it will run without any problem. It worked for me.
(Please make sure that you have installed cygwin64 on your computer, mingw is also possible)
The json file below worked for me
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]

No client URI found in getBuildTasks() - VSCode - C++

On trying to debug a C++ code on VSCode, I am getting the error, "No client URI found in getBuildTasks()"
I am an absolute beginner and am just learning C++ and just set up my VSCode from a tutorial to run C++ on VSCode with minGW-w64. But the debug button just won't work like it should have and is instead throwing that error.
This is what my launch.json looks like:
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"preLaunchTask": "echo",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
And these are the options that show up when I press Run and Debug with a.cpp opened in the folder with the .vscode subfolder containing the aforementioned launch.json file along with c_cpp_properties.json and tasks.json files.
Any and all help will be appreciated.
You need to open the Folder that you're working in, into the Workspace.
To do this, select Explorer from the left side bar and click on the big blue Open Folder button over there and open the Folder that you're working on and containing the .vscode subfolder.
.
.
The Desktop is where I build most of my smaller, quicker programs at. I removed the Desktop from the WorkSpace open, in order to stop the Source Control from suggesting me to commit changes to GitHub and hence VSCode had no directory to find the .vscode folder from. This never was a problem when I would debug and run python files but for C++ it was.
It was an absolute nooby dumbo mistake of mine but it literally took me 2 days to figure out and I've written the answer down, hoping it helps some other noob. :)

Debug GNU using VS Code without GDB

Over the weekend I had installed a gcc through the MSYS2 bash. I set it up in VS code and have it working properly. I even had the GDB working (yes I know this is a debugger). But, my main question is, is it possible to use the debugging function in VS code to debug rather then GDB. Pressing F5 it pulls up the launch.json file and gives me launch: program 'enter program name, for example c:\School\a.exe' does not exist .After some research I see you give it a file to the args to allow it run in debugger. When I do this though I can't seem to either give it the right file or make it work overall. I am also using a.exe rather than a.out. I'm unsure if this has effect.
{
// 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": ["C:\\School\\CSE340\\project2\\main.cpp"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
VS Code does not have an internal debugger (see here). You need to use GDB or the visual studio debugger (if you have the latter).
In your launch.json you need to modify the entries:
"program": this is the path to the program you want to debug, i.e. your compiled program (can be a relative path to your project folder)
"miDebuggerPath": this is the path to GDB
"args": these are arguments, you want to pass to your programm for debugging purposes, i.e. you can leave this blank
So the launch.json file for you would look something like this:
{
// 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": "${workspaceFolder}\\CSE340\\project2\\main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", // Path where your gdb.exe is located
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
${workspaceFolder} is a path variable to your workspace and seems to point to C:\\School\\, so maybe you'll need to modify the value of "program" to point to the application that you want to debug. You could also specify the absolute path to your program.
Also, do not forget to compile your code with debug-flags (-g), these are needed by GDB in order to step through the code. For example:
g++ -g main.cpp -o main.exe

Visual Studio Code use input text file on debug

I am trying to follow the direction from this post
Visual Studio Code redirect input on debug
but when I add the console config to the launch.json file
"console": "integratedTerminal"
it throws a "Property console is not allowed". and when I debug the program it still waits on input and never reach break point like I would if I start in shell like
"./a.out 1 test1.txt"
"./a.out 1 <test1.txt"
Full config
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
//"program": "${workspaceRoot}/a.out.dSYM/Contents/Resources/DWARF/a.out",
"program": "${workspaceRoot}/a.out",
"args": ["1", "<","test1.txt"],
"stopAtEntry": false,
"cwd": "${workspaceRoot}/",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
//"miDebuggerPath": "C:\\mingw\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"console": "integratedTerminal"
//"preLaunchTask": //"build test1"
}
]
}
I use GDB instead of lldb but still encountered the same issue. It waited for an input when I typed arguments in the "launch.json" file this way:
"args": ["<", "test1.txt"],
But it started working properly when I had rewritten it in the following manner:
"args": ["<", "${workspaceFolder}/test1.txt"],
I think that one should add a parent folder even though the input file is in the workspace folder or simply use the full path.
If you use the integrated console, the < doesn't get interpreted by a shell. Usually, using externalConsole: true fixes the problem since this uses a shell. But if the external console doesn't work on your system for whatever reason and you're forced to use externalConsole: false, the workaround is to let GDB create the shell: miDebuggerArgs: "'-ex' 'run < /path/to/test1.txt'"