gdb (MinGW) doesn't break on failed asserts (VSCode config) - c++

I'm trying to debug a program, in VSCode, which violates an assert, but doesn't break and doesn't allow me to inspect the callstack or anything. Instead the program just exits with exitcode 3 and prints out the following text:
Assertion failed!
Program: C:\Users\Sewbacca\Projects\Practice\CppTest\build\Test.exe
File: C:\Users\Sewbacca\Projects\Practice\CppTest\src\main.cpp, Line 6
Expression: false
I tried to add the following commands to "setupCommands" in .vscode/launch.json with no success:
{
"text": "break _assert (const char *_Message, const char *_File, unsigned _Line)"
},
{
"text": "break abort"
},
{
"text": "break exit"
},
Sidenote: I'm not experienced with gdb and I don't know exactly what setupCommands does change. I would have expected that vscode send these to gdb directly.
My only workarround is to set a breakpoint before main() and type -exec break abort into the debug console. Then it will break on any failed asserts.
Edit:
Adding the following config to "setupCommands":
{
"text": "-exec break abort"
},
Resulted in following error message:
[Window Title]
Visual Studio Code
[Content]
Unable to start debugging. Unexpected GDB output from command "-exec break abort". Undefined MI command: exec
[Open 'launch.json'] [Cancel]
End of Edit
Is there a way to automate this or is there a proper way to tell gdb (especially in VSCode) to break on failed asserts, rather than just exit the program?
Edit:
There was nothing wrong with my configuration. It seems like my gdb version was buggy. Sometimes it exited randomly when I told gdb to break before entering main, which led me to this issue. As stated there, gdb 8.1 for x86_64-w64-mingw32 has this bug. Since there is no newer version available in the installer, I downgraded to 7.2 which solved this issue for me. However, after using winlibs version 11.1.0, the issue still persists.
End of edit
Thanks in advance!
Setup:
src/main.cpp
#include <cassert>
int main()
{
assert(false);
}
CMakeLists.txt
project(Test)
add_executable(${PROJECT_NAME} src/main.cpp)
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
// add the directory where our target was built to the PATHs
// it gets resolved by CMake Tools:
"name": "PATH",
"value": "${env:PATH}:${command:cmake.getLaunchTargetDirectory}"
}
],
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"text": "break _assert (const char *_Message, const char *_File, unsigned _Line)"
},
{
"text": "break abort"
},
{
"text": "break exit"
},
]
}
],
"compounds": []
}
My Environment:
Platform: Windows 10
Toolchain: mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0
Build system: CMake
IDE: VSCode with extension CMake Tools

I found a solution:
gdb breaks on failed asserts, when adding the following code to setupCommands in launch.json:
"setupCommands": {
...
{
"text": "set breakpoint pending on",
"description": "Ensures that a breakpoint for abort will be set!",
"ignoreFailures": true
},
{
"text": "break abort",
"description": "Breaks on failed asserts",
"ignoreFailures": true
},
{
"text": "set breakpoint pending auto",
"description": "Setting back to default behaviour",
"ignoreFailures": true
}
...
}
Explaination:
To break on assert failures, I found this Stack Overflow Post, which worked, when I tested it manually, but not when I used it in launch.json.
I don't understand why it doesn't break on its own, but I do understand why break abort didn't worked, at least I have a guess: The symbols haven't been loaded yet and break abort asks you in gdb, if a pending breakpoint should be created, which defaults to no. So setting pending breakpoints to on, solved this issue for me and now I can finally debug my program.

Related

VSCode GDB debugging Internal error while converting character sets

While debugging like normally (before I didn't have this kind of problem) GDB returned message :
Internal error while converting character sets: No error.
Only for viewing string or char kind of variables.
I have tried to disable Windows beta UTF-8 engine, tried additional commands from here StackOverflow
Unfortunately nothing works.
Adding additional command for GDB logging I receive the same message.
1: (394137) ->1059^error,msg="Internal error while converting character sets: No error."
EDIT
As #rainbow.gekota requested, I added some more informations.
Current OS : Windows 10 21H2 (Compilation: 19044:2006)
VSCode ver. : 1.72.0 x64 -> 64bbfbf67ada9953918d72e1df2f4d8e537d340e
GDB ver. : 12.1 for MinGW-W64 x86_64, built by Brecht Sanders
GDB installed from MSYS2 repos.
Here's my launch.json with which I was trying to fix this error with set charset UTF-8
{
"version": "0.2.0",
"configurations": [
{
"name": "Start debugging",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\main.exe",
"args": ["arg1", "arg2", "arg3"],
"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": "Fix pretty-printing for gdb",
"text": "set charset UTF-8"
}
],
"preLaunchTask": "Build program",
"logging": { "engineLogging": true }
}
]
}
I don't have any more idea how to reproduce this error. It was working fine until one day.
Unfortunately I couldn't find any logical answer to my question. The only thing that helped was deleting MSYS2 with gdb and gcc installed and installing it once again.
Now I can't reproduce this issues so maybe in the future I'll comeback with the same error. Will see.

How to debug C++ application in docker with VSCode

I am trying to debug a C++ application running inside a docker container with VSCode. I followed the below-mentioned steps to do that
Installed C/C++ and Remote-Containers extension in VSCode
Connected VSCode to the docker container
Added the below C/C++: (gdb) Attach configuration in launch.json
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "/usr/local/bin/myfeature",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"additionalSOLibSearchPath": "/usr/local/bin/myfeature.dbg",
"sourceFileMap":{
"<source-path>": "/usr/local/src/test/av/"
},
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
When I start debugging the breakpoints in the file myclass.cc are disabled and show status as "Module containing this breakpoint has not been loaded yet...."
I am not very familiar with gdb but when I run this command from the command line
gdb /usr/local/bin/myfeature
I get this
Reading symbols from /usr/local/bin/myfeature...
Reading symbols from /usr/local/bin/myfeature.dbg...
Then I am able to add breakpoint using
break myclass.cc:15
If gdb is able to read symbols and add breakpoint then why is VSCode not able to do that? What am I doing wrong here?
Thank you

Can't pause or set breakpoint when running in vscode

I'm currently developing a (simple) kernel using Qemu for i368.
I was using a normal makefile and had debugging via GDB into Qemu working perfeclty
The I moved to CMake and for some reason I can only hit breakpoints when my code is already in a breakpoint.
I also can't pause, the feedback I get from the debug window is the pause was succesful
When setting a breakpoint it tells me Attempting to bind the breakpoint
A hint may be that when I stop the debug session, I get a popup from vscode saying timeout
My launch.json
{
// 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": "Launch with GDB",
"type": "cppdbg",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"cwd": "${workspaceRoot}",
"args": [],
"targetArchitecture": "x86",
"MIMode": "gdb",
"miDebuggerPath": "/bin/gdb",
"miDebuggerArgs": "",
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
},
"customLaunchSetupCommands": [
{
"text": "target remote localhost:1234",
"description": "Connect to QEMU remote debugger"
}
],
"setupCommands": [
{
"text": "file ${command:cmake.launchTargetPath}",
"description": "Load binary."
},
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
}
]
}
Pause response
<-- C (pause-67): {"command":"pause","arguments":{"threadId":1},"type":"request","seq":67}
--> R (pause-67): {"type":"response","request_seq":67,"success":true,"command":"pause","body":{},"seq":1321}
Response when setting a breakpoint:
--> E (output): {"type":"event","event":"output","body":{"category":"console","output":"1: (340258) Send Event AD7BreakpointErrorEvent\n"},"seq":1327}
1: (340258) Send Event AD7BreakpointErrorEvent
--> R (setBreakpoints-69): {"type":"response","request_seq":69,"success":true,"command":"setBreakpoints","body":{"breakpoints":[{"id":4,"verified":true,"line":73}]},"seq":1329}
--> E (breakpoint): {"type":"event","event":"breakpoint","body":{"reason":"changed","breakpoint":{"id":4,"verified":false,"message":"Attempting to bind the breakpoint....","line":73}},"seq":1331}
If I type stop in Qemu terminal then vscode stops and I can set breakpoints again.
I have the same issue and still haven't solved yet.
But I found something maybe useful for you.
according to their discussion under this issue: https://github.com/Microsoft/vscode-cpptools/issues/833
you can try
set target-async to off for your debugger:
"setupCommands": [
{
"text": "set target-async off"
}
send SIGINT to your local debugger process
-exec -interpreter-exec console "signal 2"
to see if it can pause or not.
I meet the similar issue, try to connect remote target with option ""MIDebuggerServerAddress": "targetIp:port", not command in customLaunchSetupCommands.

Pretty-printer is not working when remote debugging an arm board via vs code

I'm trying to remote debug a c++ app from a VM ubuntu 16.04 amd64 host to a debian armbian target of cubietruck board (ARM® Cortex™-A7 Dual-Core) via vs code.
I have follwed this guide https://medium.com/#spe_/debugging-c-c-programs-remotely-using-visual-studio-code-and-gdbserver-559d3434fb78 with the only addition of the field
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
so the whole launch.json has become
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"miDebuggerPath": "/home/user/ARM/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"program": "/home/user/myproject/bin/app",
"miDebuggerServerAddress": "localhost:9091",
"args": [],
"stopAtEntry": false,
"cwd": "/home/user/myproject",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb"
},
"osx": {
"MIMode": "gdb"
},
"windows": {
"MIMode": "gdb"
}
}
]
}
in launch.json file so as to support pretty printer.
In the host I'm using the linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf gdb while the gdb in arm board has the native 8.0.1 gdb.
Everything works fine except from the pretty printer since the strings are not shown correctly. Whenever I hover over a string the fields npos and _M_dataplus pop up and one should open the _M_dataplus filed to see the actual string.
In host the linaro gdb supports pretty-printer since the command info pretty-printer gives the output :
builtin
mpx_bound128
However when I give the same command in target gdb 8.0.1, I get:
Undefined info command: "pretty-printer". Try "help info".
I also created a .gdbinit file in my host home folder that contains the enable pretty-printer command. I saw in the debug console that it was executed successfully but the result was erroneous as well.
Since I am pretty novice in remote debugging, what should I do to get the pretty-printer working. Should I install pretty-printer in the remote board as well or am I doing something else wrong?
1.make sure your bin is NOT built with -static-libstdc++
2.if you have to use -static-libstdc++, please add the following in your ~/.gdbinit:
python sys.path.append('/usr/share/gdb/python') <--provided by gdb `show configuration: --with-gdb-datadir=`
python sys.path.append("/usr/share/gcc-8/python/") <--corresponding to your gcc version
source /usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25-gdb.py <--this script is for std::string/list/map/etc pretty-printing.
3.edit your launch.json:
"setupCommands": [
{ "text": "-interpreter-exec console \"set sysroot /\"", "ignoreFailures": true },
{ "text": "-enable-pretty-printing", "ignoreFailures": true },
],
This is what I did to resolve the same issue occured to me. Hope this would resolve your issue.
ref1: http://tomszilagyi.github.io/2018/03/Remote-gdb-with-stl-pp
ref2: http://transit.iut2.upmf-grenoble.fr/doc/gdb-doc/html/gdb/Writing-a-Pretty_002dPrinter.html
ref3: Import Error: No module name libstdcxx

Visual Studio Code - Activate Env before Debug

my current setup involves a boost-python module, that I wrote. To debug this module, I wrote a standalone C++ program that calls the python-script from within the C++ program.
int main()
{
try
{
PyImport_AppendInittab("oum_export", INIT_MODULE);
Py_Initialize();
PyObject *obj = Py_BuildValue("s", "/media/thomas/Data/seafile/Seafile/TUG/ILearnHeart/Anisotropic\\ Diffusion/python/oum_cpp_opt.py");
FILE *file = _Py_fopen_obj(obj, "r+");
if (file != NULL)
PyRun_SimpleFile(file, "D:/seafile/Seafile/TUG/ILearnHeart/Anisotropic Diffusion/python/oum_cpp_opt.py");
else
cout << "Script file to execute not found" << endl;
}
catch( p::error_already_set )
{
PyErr_Print();
}
Py_Finalize();
}
This should allow me to easily debug the callbacks made to the Python-modules, written in in C++. On invoking the vscode-debugger, the program crashes with the error
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
which is due to the fact that I'm not in the correct anaconda environment. How can I tell visual-studio code to enter the correct environment, before launching the gdb (i.e.: "source activate aniso_diff && gdb oum_export_test")?
Here's my current launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "oum_export_test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build_linux",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build oum standalone"
}
]
}
I tried incorporating the activate command into the build preLaunchTask, but it seems vscode invokes a fresh shell for gdb.
This is probably a no-brainer for most of you, but I just figured out that the simplest solution is to simply activate your desired environment before invoking vscode on the same shell.