GDB doesn't work: Cannot insert breakpoint 1. Cannot access memory at address [...] - c++

I'm trying to setup my vscode to debug C++ programs using gdb from WSL. But I faced problem with gdb on my WSL 1 (Ubuntu 22.04 LTS). Whenever I try set a breakpoint and run a program, I get the following error:
Starting program: /mnt/d/Informatics/vscode/test/a.out
warning: opening /proc/PID/mem file for lwp 7674.7674 failed: No such file or directory (2)
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x10e0
The contents of the program don't matter, always getting the error. Code is compiled using the following line:
g++ -Wall -pedantic-errors -O2 -std=c++23 -ggdb3 -fdiagnostics-color=always /mnt/d/Informatics/vscode/test/*.cpp
gdb version: GNU gdb (Ubuntu 12.0.90-0ubuntu1) 12.0.90
g++ version: 12.0.1 (also tried with 11.2.0 - no difference)
On windows with g++ and gdb from MinGW the problem doesn't exist, so this should mean I'm not using the gdb debugger wrong.
Edit: tried compiling gdb 11.2 from source as suggested in comments, but this did not fix the problem. Only difference is that it's not showing /proc/PID/mem warning now.
Starting program: /mnt/d/Informatics/vscode/test/a.out
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x10e0
Edit 2: Compiled gdb 10.2 from source and it works, the problem does not appear. I don't know why it doesn't work with any version above 10.2. Is it gdb bug or WSL bug?

Related

How do you find out the cause of rare crashes that are caused by things that are not caught by try catch (access violation, divide by zero, etc.)?

I am a .NET programmer who is starting to dabble into C++. In C# I would put the root function in a try catch, this way I would catch all exceptions, save the stack trace, and this way I would know what caused the exception, significantly reducing the time spent debugging.
But in C++ some stuff(access violation, divide by zero, etc.) are not caught by try catch. How do you deal with them, how do you know which line of code caused the error?
For example let's assume we have a program that has 1 million lines of code. It's running 24/7, has no user-interaction. Once in a month it crashes because of something that is not caught by try catch. How do you find out which line of code caused the crash?
Environment: Windows 10, MSVC.
C++ is meant to be a high performance language and checks are expensive. You can't run at C++ speeds and at the same time have all sorts of checks. It is by design.
Running .Net this way is akin to running C++ in debug mode with sanitizers on. So if you want to run your application with all the information you can, turn on debug mode in your cmake build and add sanitizers, at least undefined and address sanitizers.
For Windows/MSVC it seems that address sanitizers were just added in 2021. You can check the announcement here: https://devblogs.microsoft.com/cppblog/addresssanitizer-asan-for-windows-with-msvc/
For Windows/mingw or Linux/* you can use Gcc and Clang's builtin sanitizers that have largely the same usage/syntax.
To set your build to debug mode:
cd <builddir>
cmake -DCMAKE_BUILD_TYPE=debug <sourcedir>
To enable sanitizers, add this to your compiler command line: -fsanitize=address,undefined
One way to do that is to add it to your cmake build so altogether it becomes:
cmake -DCMAKE_BUILD_TYPE=debug \
-DCMAKE_CXX_FLAGS_DEBUG_INIT="-fsanitize=address,undefined" \
<sourcedir>
Then run your application binary normally as you do. When an issue is found a meaningful message will be printed along with a very informative stack trace.
Alternatively you can set so the sanitizer breaks inside the debugger (gdb) so you can inspect it live but that only works with the undefined sanitizer. To do so, replace
-fsanitize=address,undefined
with
-fsanitize-undefined-trap-on-error -fsanitize-trap=undefined -fsanitize=address
For example, this code has a clear problem:
void doit( int* p ) {
*p = 10;
}
int main() {
int* ptr = nullptr;
doit(ptr);
}
Compile it in the optimized way and you get:
$ g++ -O3 test.cpp -o test
$ ./test
Segmentation fault (core dumped)
Not very informative. You can try to run it inside the debugger but no symbols are there to see.
$ g++ -O3 test.cpp -o test
$ gdb ./test
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
...
Reading symbols from ./test...
(No debugging symbols found in ./test)
(gdb) r
Starting program: /tmp/test
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555044 in main ()
(gdb)
That's useless so we can turn on debug symbols with
$ g++ -g3 test.cpp -o test
$ gdb ./test
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
...
Reading symbols from ./test...
(gdb) r
Starting program: /tmp/test
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
test.cpp:4:5: runtime error: store to null pointer of type 'int'
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555259 in doit (p=0x0) at test.cpp:4
4 *p = 10;
Then you can inspect inside:
(gdb) p p
$1 = (int *) 0x0
Now, turn on sanitizers to get even more messages without the debugger:
$ g++ -O0 -g3 test.cpp -fsanitize=address,undefined -o test
$ ./test
test.cpp:4:5: runtime error: store to null pointer of type 'int'
AddressSanitizer:DEADLYSIGNAL
=================================================================
==931717==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x563b7b66c259 bp 0x7fffd167c240 sp 0x7fffd167c230 T0)
==931717==The signal is caused by a WRITE memory access.
==931717==Hint: address points to the zero page.
#0 0x563b7b66c258 in doit(int*) /tmp/test.cpp:4
#1 0x563b7b66c281 in main /tmp/test.cpp:9
#2 0x7f36164a9082 in __libc_start_main ../csu/libc-start.c:308
#3 0x563b7b66c12d in _start (/tmp/test+0x112d)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /tmp/test.cpp:4 in doit(int*)
==931717==ABORTING
That is much better!

c++ / g++ - Error: Unknown pseudo-op: `. loc'

I am trying to configure my computer to learn C++.
I have errors and warnings once I run gcc from the terminal to call hello.cpp.
C:\Users\Mathieu>gcc -g hello.cpp -o hello -lm
I am using a Windows computer / windows 7 (64 bits) / IDE: Sublime text or Visual Studio Code.
What I have done so far by looking for solutions in the web:
I have added into the PATH the address of MinGW and I have added ";" before the previous PATH.
I have also been checking all the PATH by using echo %PATH% into the terminal and checking the environmental variables.
By typing g++ or gcc alone in the terminal, I get "fatal error, no input file", which means gcc and g++ are well detected by the computer.
I have checked g++ and gcc versions, there are equal.
gcc (MinGW.org GCC Build-20200227-1) 9.2.0
gcc (MinGW.org GCC Build-20200227-1) 9.2.0
I have also performed #alisonc recommandation found here by reviewing links.
g++ 4.6.1 compiler error: Error: unknown pseudo-op: `.cfi_personality'
C:\Users\Mathieu>ls 'which g++' -al
ls: which g++: No such file or directory
C:\Users\Mathieu>ls 'which gcc' -al
ls: which gcc: No such file or directory
When I have launched the program from the terminal I get a long list of errors. I am just adding 3 lines to illustrate. I am not sure if that is relevant to add everything, please, see a few ones:
C:\Users\Mathieu\AppData\Local\Temp\ccUFD66I.s:6498: Error: Unknown pseudo-op:
.secrel32
C:\Users\Mathieu\AppData\Local\Temp\ccUFD66I.s:153: Warning: Missing string
C:\Users\Mathieu\AppData\Local\Temp\ccUFD66I.s:153: Error: Rest of line ignored.
First ignored character is `3'.'''
May someone help me ?
Thank you in advance
All the best
Mathieu
Try to run:
g++ -g hello.cpp -o hello -lm

Cannot find -lubsan on using -fsanitize=undefined (mingw-w64)

I'm using mingw-w64 (gcc 7.3.0) and when I compile any C++ program using the following command:
g++ file.cpp -fsanitize=undefined
I get the following error:
...mingw32/bin/ld.exe: cannot find -lubsan
I'm able to successfully compile and run my programs if I remove the -fsanitize=undefined flag, though. After some research I found out that this means the library ubsan (Undefined Behavior Sanitizer) is missing, but I couldn't find anything about the library. How do I fix this?
This is well known issue with mingw see for instance this msys github issue. No proper solution known but there are several WAs.
Install WSL, ubuntu over WSL and you will have ubsan inside it
Build GCC under Windows from source enabling sanitizers build. They are present in GCC sources they are just not here in mingw.
Use -fsanitize=undefined -fsanitize-undefined-trap-on-error to just not use libubsan rich logging capabilities but get it trap on undefined instruction.
Hope one of this helps.

Trying to compile a C++ code with Root (Cern) parameters included

I working on trying to compile a code written in C++ and Root on my Mac. The code is from a colleague who works on a Linux laptop. In addition to the different OS's, I have both a different version of gcc and Root than her and I am not sure which difference is causing the code not to compile on my machine.
She has g++ 4.8 and root 5.(something). I have gcc 5.3.0 and root 6.06/02.
She has given to me the a line of code she uses to get her machine to compile the code
gcc -Wall -o executable_name code_name.cc `root-config --cflags --glibs`
But when I write on my machine, Terminal gives me the error
gcc: error: unrecognized command line option ‘-stdlib=libc++’
gcc: error: unrecognized command line option ‘-stdlib=libc++’
I need help generating the correct line to get gcc to compile the code.
The problem here is two-fold: You're on OSX and you are using GCC.
The problem with this is that root-config assumes that since you're on OSX you will be using the OSX-standard Clang compiler which have the -stdlib flag. Since you're not using Clang, but GCC (which doesn't have this flag) you get an error.
You have two possible solutions: Use clang++ instead of g++ to compile and build (requires you to install the compiler if it's not installed already, it comes with Xcode), or to modify the root-config script so it doesn't add -stdlib=libc++. There might be environment variables or flags that the root-config script checks that alter the behavior, but I don't know anything about the script, you have to check it for that.

Application on another system crashes on startup without error message for sudo, Segmentation Fault for non-sudo

I have written a websocket++ server on Ubuntu 13.10 and am trying to execute it on Linux Mint 16.
I have installed all dependencies, and the first line under main is a cout which never fires.
This is the compile command:
g++ -o Dgn Dgn.cpp ed25519-donna-master/ed25519.o
-Og -std=c++0x -I ~/Dgn -D_WEBSOCKETPP_CPP11_STL_ -D_WEBSOCKETPP_NO_CPP11_REGEX_
-lboost_regex -lboost_system -L/usr/lib -lssl -lcrypto -pthread -lpqxx
-lboost_thread -ljson_spirit -lgmp -lgmpxx
If I execute with sudo to use restricted ports, it fails immediately without error returning to the command line.
If I execute without sudo, is prints Segmentation Fault and fails immediately to the command line.
The directories in ~/Dgn are present on the new system.
I did a quick, simple test and checked to see if a basic websocket++ example could compile and execute normally, and it was successful.
Both systems are 64-bit. The only difference are the distros, but Linux Mint 16 is based upon Ubuntu 13.10, and all commands to setup were identical.
How can this be compiled so that it can execute on another system?
As a further test, I compiled it on the new system, and it works.
Is it not possible to compile on one system and run on another?
GDB
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7de58da in ?? () from /lib64/ld-linux-x86-64.so.2
The fact that cout line never fires (I assume it has << std::endl) means that the crash happens in a static object constructor. The most straightforward way to debug is to allow core dump (see man limits) and inspect the dump with gdb. So far that's all I can think of. More details will help.