I have a very simple Hello World c++ program. I am running it on Mac OS Mojave 10.14.5. Apple LLVM version 10.0.1. GNU gdb (GDB) 8.3
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
printf("Hello, World!");
return 0;
}
I compile it with command g++ -g a.cpp
I run gdb as sudo gdb ./a.out.
In (gdb) prompt I type start
I get the following message, but the (gdb) prompt never returns:
Temporary breakpoint 1 at 0x100000f3f: file a.cpp, line 6.
Starting program: a.out
[New Thread 0x1203 of process 5444]
[New Thread 0xf03 of process 5444]
I cannot even close the process with control + z. I have to force a termination of the terminal to close it.
Related
I'm trying to get gdb working with C++ programs on Ubuntu 20.04. What I need is to be able to set a breakpoint (for example, break main.cpp:3 gdb command) and then run until the breakpoint, but at the moment both start and run fail because they "Cannot insert breakpoint" and "Cannot access memory". For me gdb fails even with very simple examples. This is main.cpp content:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
I found somewhere that using -no-pie might help to get gdb working (with breakpoints), so I compile the program by running g++ -ggdb3 -no-pie -o main main.cpp (I also tried -g instead of -ggdb3, and -fno-PIE in addition to -no-pie). When I try to use gdb, it complains "Cannot insert breakpoint 1":
gdb -q main
Reading symbols from main...
(gdb) start
Temporary breakpoint 1 at 0x1189: file main.cpp, line 3.
Starting program: /tmp/main
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x1189
Without -no-pie result is the same. Only thing that changes with or without -no-pie is the hexadecimal address, without -no-pie it is low like 0x1189 (as shown above), with -no-pie it can be 0x401176, but everything else exactly the same, I keep getting the "Cannot access memory at address" warning in both cases.
If I use starti instead of start, it works at first, but after a few nexti iterations it prints usual message "Cannot insteart breakpoint":
gdb -q main
Reading symbols from main...
(gdb) starti
Starting program: /tmp/main
Program stopped.
0x00007ffff7fd0100 in ?? () from /lib64/ld-linux-x86-64.so.2
(gdb) nexti
0x00007ffff7fd0103 in ?? () from /lib64/ld-linux-x86-64.so.2
...
(gdb) nexti
Warning:
Cannot insert breakpoint 0.
Cannot access memory at address 0x4
0x00007ffff7fd0119 in ?? () from /lib64/ld-linux-x86-64.so.2
(gdb) nexti
0x00007ffff7fd011c in ?? () from /lib64/ld-linux-x86-64.so.2
...
(gdb) nexti
Warning:
Cannot insert breakpoint 0.
Cannot access memory at address 0x1c
0x000055555556ca22 in ?? ()
(gdb) nexti
[Detaching after fork from child process 3829827]
...
[Detaching after fork from child process 3829840]
Hello World![Inferior 1 (process 3819010) exited normally]
So I can use nexti, but cannot use next and obviously cannot insert breakpoints.
I tried -Wl,-no-pie (by running g++ -Wl,-no-pie -ggdb3 -o main main.cpp; adding -no-pie does not change anything) but this option causes a strange linker error:
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
When I google the error, I only found advice to try -no-pie instead of -Wl,-no-pie, and no other solutions. Since debugging C++ programs is very common activity, I feel like I'm missing something obvious but I found no solution so far.
To make it easier to understand what exact commands I use and to make it clear I'm not mixing up directories and to show what versions of g++ and gdb I'm using, here is full terminal log:
$ ls
main.cpp
$ g++ --version | grep Ubuntu
g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
$ g++ -ggdb3 -no-pie -o main main.cpp
$ ls
main main.cpp
$ gdb --version | grep Ubuntu
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
$ readelf -h main | grep 'Type: .*EXEC'
Type: EXEC (Executable file)
$ gdb -q main
Reading symbols from main...
(gdb) start
Temporary breakpoint 1 at 0x401176: file main.cpp, line 3.
Starting program: /tmp/main/main
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x401176
For completeness, I tried the same without -no-pie:
$ rm main
$ g++ -ggdb3 -o main main.cpp
$ readelf -h main | grep 'Type: .*'
Type: DYN (Shared object file)
$ gdb -q main
Reading symbols from main...
(gdb) start
Temporary breakpoint 1 at 0x1189: file main.cpp, line 3.
Starting program: /tmp/main/main
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x1189
As you can see the only difference with or without -no-pie is the memory address, but the issue and warnings are the same. Without -no-pie this may be expected, but I do not understand why this is happening if I compiled with -no-pie and what else I can try to solve the issue.
This:
g++ -ggdb3 -no-pie -o main main.cpp
should produce a non-PIE executable. You should be able to verify that it non-PIE by looking at readelf -h main | grep 'Type: .*EXEC' (PIE binaries have ET_DYN type).
This:
Temporary breakpoint 1 at 0x1189: file main.cpp, line 3.
is unambiguously a PIE binary (a non-PIE binary will not have any code below 0x40000 on x86_64 Linux).
Conclusion: you are either debugging the wrong binary (e.g. you are compiling main in a different directory from the one in which you are debugging), or you are not telling is the whole story.
The context
Consider the following file
$ cat main.cpp
int main() {return 0;}
I can list all the available functions by executing
$ g++ -g main.cpp && gdb -q -batch -ex 'info functions -n' a.out
All defined functions:
File main.cpp:
1: int main();
When executing start before executing info functions more than 1000 functions are listed (see below)
g++ -g main.cpp && \
gdb -q -batch -ex 'start' -ex 'info functions -n' a.out | \
head -n 10
Temporary breakpoint 1 at 0x111d: file main.cpp, line 1.
Temporary breakpoint 1, main () at main.cpp:1
1 int main() {return 0;}
All defined functions:
File /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/allocated_ptr.h:
70: void std::__allocated_ptr<std::allocator<std::_Sp_counted_ptr_inplace<std::filesystem::__cxx11::filesystem_error::_Impl, std::allocator<std::filesystem::__cxx11::filesystem_error::_Impl>, (__gnu_cxx::_Lock_policy)2> > >::~__allocated_ptr();
70: void std::__allocated_ptr<std::allocator<std::_Sp_counted_ptr_inplace<std::filesystem::filesystem_error::_Impl, std::allocator<std::filesystem::filesystem_error::_Impl>, (__gnu_cxx::_Lock_policy)2> > >::~__allocated_ptr();
As seen below, the total number of lines printed is so, apparently, more than 1000 functions are being listed
g++ -g main.cpp && gdb -q -batch -ex 'start' -ex 'info functions -n' a.out | wc -l
4436
The question
As we can see above, the main.cpp file does not contain any function, so why is gdb listing those functions when the start command has been executed before but not when start hasn't been executed?
Additional context
As suggested in one of the comments of this question, here's the output of executing info shared after start has been executed
g++ -g main.cpp && gdb -q -batch -ex 'start' -ex 'info shared' a.out
Temporary breakpoint 1 at 0x111d: file main.cpp, line 1.
Temporary breakpoint 1, main () at main.cpp:1
1 int main() {return 0;}
From To Syms Read Shared Object Library
0x00007ffff7fd2090 0x00007ffff7ff2746 Yes (*) /lib64/ld-linux-x86-64.so.2
0x00007ffff7e4c040 0x00007ffff7f37b52 Yes /usr/lib/libstdc++.so.6
0x00007ffff7c7f3b0 0x00007ffff7d1a658 Yes (*) /usr/lib/libm.so.6
0x00007ffff7c59020 0x00007ffff7c69ca5 Yes /usr/lib/libgcc_s.so.1
0x00007ffff7ab3650 0x00007ffff7bfe6bd Yes (*) /usr/lib/libc.so.6
(*): Shared library is missing debugging information.
main.cpp file does not contain any function, so why is gdb listing those functions when the start command has been executed before but not when start hasn't been executed?
Before start, GDB reads symbols (and debug info) only for the main executable.
After start, a dynamically linked executable loads shared libraries (seen in info shared), and GDB (by default) reads symbol tables and debug info for each of them. And since these libraries contain hundreds of functions, GDB knows about all of them.
You can prevent this with set auto-solib-add off, but usually you don't want to do that. If you do, and your program crashes in e.g. abort, GDB will not know where you crashed unless you manually add the symbols back using sharedlibrary or add-symbol-file command.
I've only started getting this problem recently, and I have no idea when it started occuring/what causes it.
I have this simple test program here:
#include <iostream>
int main()
{
return 0;
}
but when I try to run it normally, it creates a stackdump.
Stack trace:
Frame Function Args
00CBC498 6101D93A (00000198, 0000EA60, 000000A4, 00CBC508)
00CBC5C8 610E2F3F (00000000, 60FC04E8, 00CBC658, 7794ABEE)
When I try to run it in GDB, however, it just plain fails to do so.
gdb: unknown target exception 0x406d1388 at 0x778edae8
Program received signal ?, Unknown signal.
0x778edae8 in RaiseException ()
from /cygdrive/c/WINDOWS/SYSTEM32/KERNELBASE.dll
(gdb) n
Single stepping until exit from function RaiseException,
which has no line number information.
[Thread 14880.0x11ac exited with code 1080890248]
[Thread 14880.0x3fd8 exited with code 1080890248]
[Thread 14880.0x3b24 exited with code 1080890248]
[Inferior 1 (process 14880) exited with code 010033211610]
This is how my compiler's set up:
g++ -g -std=c++1y -Wall -c main.cpp -o main.o main.cpp compiled...
g++ -g -std=c++1y -Wall -o a main.o Successfully compiled!
Any idea as to what I'm doing wrong here?
The platform I'm using is Windows, and I'm using Cygwin as my development environment.
OF NOTE: I still get the aforementioned GDB error no matter what I have in main.cpp.
ALSO OF NOTE: Here are the additional files in the a.exe executable:
I formerly needed cygboost_filesystem.dll to get boost_filesystem to work, and I need libgcc_s_sjlj-1.dll and libstdc++6 because it won't work without them.
Update gdb to the 7.11.1-1 experimental release version.
Details of GDB bug
I've just installed MinGW with mingw32-base and mingw32-gcc-g++ on Windows 10 and added "C:\MinGW\bin\" to the system path and I can run gcc and g++ from the command prompt, but when I try to compile a .cpp file with g++ helloworld.cpp -o helloworld.exe it creates helloworld.exe and after about 2 seconds it deletes helloworld.exe. If I try to run helloworld.exe from the prompt before it deletes it, it gives me "Access is denied.".
Here is helloworld.cpp:
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
But even if I use other code it does the same.
Consider this simple program:
#include <string>
#include <iostream>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "MyTest"
#include <boost/test/unit_test.hpp>
using namespace std;
template<char* S>
void Test()
{
BOOST_REQUIRE("Boom!" != string(S));
}
char bang[] = "Bang!";
BOOST_AUTO_TEST_CASE(Boom)
{
char boom[] = "Boom!";
Test<bang>();
}
I'm on Mac OS X v10.8.2 (Mountain Lion) and have XCode 4.5 installed.
The program works when compiled with GCC, for example,
gcc test.cpp -lboost_unit_test_framework-mt -lstdc++
but it crashes when compiled with Apple Clang 4.1 (tags/Apple/clang-421.11.65) (based on LLVM 3.1svn)
clang -std=c++11 -stdlib=libc++ -lc++ test.cpp -lboost_unit_test_framework-mt
I'm using Boost 1.51.0, installed using BREW. Recompiling Boost using Clang does not help.
Is there a solution to this mystery?
./a.out
Running 1 test case...
*** No errors detected
Segmentation fault: 11
gdb ./a.out
GNU gdb 6.3.50-20050815 (Apple version gdb-1822) (Sun Aug 5 03:00:42 UTC 2012)
...
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .... done
(gdb) r
Starting program: /private/tmp/xx/a.out
Reading symbols for shared libraries +++............................. done
Running 1 test case...
*** No errors detected
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: 13 at address: 0x0000000000000000
0x0000000100056c56 in boost::unit_test::test_unit::~test_unit ()
(gdb) where
#0 0x0000000100056c56 in boost::unit_test::test_unit::~test_unit ()
#1 0x000000010002435f in boost::unit_test::master_test_suite_t::~master_test_suite_t ()
#2 0x00000001000244dd in boost::unit_test::framework_impl::~framework_impl ()
#3 0x00007fff96179307 in __cxa_finalize ()
#4 0x00007fff9617af57 in exit ()
#5 0x00007fff944897e8 in start ()
lldb a.out
Current executable set to 'a.out' (x86_64).
(lldb) r
Process 71553 launched: '/private/tmp/xx/a.out' (x86_64)
Running 1 test case...
*** No errors detected
Process 71553 stopped
* thread #1: tid = 0x1c03, 0x0000000100056c56 libboost_unit_test_framework.dylib`boost::unit_test::test_unit::~test_unit() + 86, stop reason = EXC_BAD_ACCESS (code=13, address=0x0)
frame #0: 0x0000000100056c56 libboost_unit_test_framework.dylib`boost::unit_test::test_unit::~test_unit() + 86
libboost_unit_test_framework.dylib`boost::unit_test::test_unit::~test_unit() + 86:
-> 0x100056c56: lock
0x100056c57: xaddl %ecx, -8(%rax)
0x100056c5b: testl %ecx, %ecx
0x100056c5d: jg 0x100056c68 ; boost::unit_test::test_unit::~test_unit() + 104
clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.65) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
Recompiling Boost with the C++11 options does the trick.
./bootstrap.sh --with-toolset=clang --prefix=/usr/local
sudo ./b2 install toolset=clang cxxflags="-std=c++11 -stdlib=libc++" linkflags="-stdlib=libc++" threading=multi
I made a mistake of using just ./b2 install last time but the install step appears to build some libraries before installing them.
I had the same issue than timlukins.
I had to do cmake .. -DBoost_USE_STATIC_LIBS=YES. But as I had other issues compiling with boost_timer for instance, I found that it was my include that was not correct.
I had to replace :
#include <boost/test/included/unit_test.hpp>
with:
#include <boost/test/unit_test.hpp>
It was working fine with gcc and VS13, but not with clang.
If it can help...
The following seems to work, but you may need the other arguments.
USER (~/tmp)
$ clang++ -pedantic test.cpp -lboost_unit_test_framework-mt
USER (~/tmp)
$ ./a.out
Running 1 test case...
*** No errors detected