When trying to run gdb with a program, it seg faults while reading symbols.
When I run:
gdb /home/user/path/to/program.exe
I get:
GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/user/path/to/program.exe...Segmentation fault (core dumped)
I suspect that the binary might be too large for gdb to load into memory. This error only occurs when I compile with -g (debug flag). Here's the difference in size of the binaries:
Compiled with
-release flag: 405 MB
-debug flag: 862 MB
Any ideas on other culprits of this segmentation fault? Or is there a way to increase the memory allowed for gdb? This turns out to be a very challenging problem to google.
I was having the same problem with gdb 7.9 on Ubuntu 15.04 x86_64, which I had installed simply using apt-get install gdb.
I solved the problem by compiling and installing a previous version: gdb 7.5.1.
I had to download a library (which I found out here) and I also had to run ./configure using some arguments (which I found out here). Everything else is straightforward.
Good luck.
Here are the commands:
$ cd
$ sudo apt-get install libncurses5-dev
$ wget ftp://sourceware.org/pub/gdb/releases/gdb-7.5.1.tar.gz
$ tar zxf gdb-7.5.1.tar.gz
$ cd gdb-7.5.1
$ sudo ./configure --disable-werror
$ sudo make
$ sudo make install
If you compile without the -g flag then you are not including debugging information in your executable, so when gdb loads there's much less information to load in.
If gdb segfaults during start up then this is a gdb bug, there's no executable that you should be able to pass to gdb that should cause a segfault, at worst you should get some error message.
You could try running gdb under gdb, (just do gdb --args gdb /home/user/path/to/program.exe) this will not help you much, but might give some insight into what is wrong with gdb, you could then file a gdb bug here: https://sourceware.org/bugzilla/enter_bug.cgi?product=gdb but this is only worth doing if you either have good steps to reproduce, or a backtrace from a crashed gdb.
Reinstalling gdb might help, but I wouldn't hold much hope of that solving the problem, unless you change the version of gdb you install, gdb itself is a pretty easy program to install, so pretty hard to get wrong.
You could also try building gdb from git, it's pretty easy, and the bug might have already been fixed, start from here: http://www.gnu.org/software/gdb/current/
If you extend your question with a backtrace from a crashed gdb then others might be able to offer you more of an insight into why this is crashing, but the blame is definitely with your version of gdb.
Related
it's my first post so I apologise in advance if I post anything wrong or incorrectly format.
My system: MacBook Pro running MacOS Mojave 10.14.1, Netbeans 8.2
I'm running a simple C++ program that prints hello world:
int main(int argc, char** argv) {
cout << "Hello World" << endl;
return 0;
}
So my issue is that I cannot run the debugger on my MacBook using Netbeans or Terminal commands. Every time I do, I get the following error:
not in executable format: file format not recognized
I originally had the problem where I had a missing debugger command. I followed the directions here and installed Homebrew, got gdb, and code-signed the gdb binary. After all that I started getting the error highlighted above.
I google this new problem, and I find this stack overflow post which suggests that I might be running a 32-bit gdb while building in 64-bit. However, based on the output when running gdb:
GNU gdb (GDB) 8.2
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin18.0.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
BFD: /Users/Anon/Desktop/gdb_test/gdb_test: unknown load command 0x32
BFD: /Users/Anon/Desktop/gdb_test/gdb_test: unknown load command 0x32
"/Users/Anon/Desktop/gdb_test/gdb_test": not in executable format: file format not recognized
And the configuration of the GDB:
This GDB was configured as follows:
configure --host=x86_64-apple-darwin18.0.0 --target=x86_64-apple-darwin18.0.0
--with-auto-load-dir=:${prefix}/share/auto-load
--with-auto-load-safe-path=:${prefix}/share/auto-load
--with-expat
--with-gdb-datadir=/usr/local/Cellar/gdb/8.2/share/gdb (relocatable)
--with-jit-reader-dir=/usr/local/Cellar/gdb/8.2/lib/gdb (relocatable)
--without-libunwind-ia64
--without-lzma
--without-babeltrace
--without-intel-pt
--disable-libmcheck
--without-mpfr
--with-python=/System/Library/Frameworks/Python.framework/Versions/2.7
--without-guile
--with-separate-debug-dir=/usr/local/Cellar/gdb/8.2/lib/debug (relocatable)
("Relocatable" means the directory can be moved with the GDB installation tree, and GDB will still find it.)
It seems that my GDB is indeed 64-bit so I'm guessing that's not the problem. I also found this post where the top answer suggests that gdb 8.2 is impossibly broken and I should just downgrade to 8.0.1. However, an edit says that an update by the GNU team has (supposedly) fixed the problem so I run:
brew update
and ensure that everything is up-to-date, but I still get the same error.
I'm at my wits end here, and I've put in WAY too much time into trying to resolve this issue. If it can't be fixed, can you suggest other hassle-free ways (massive emphasis on hassle-free) I can debug C/C++ programs on my Mac? Otherwise, I'll stick to my university's computer labs.
EDIT: here's how I compiled from the terminal
g++ -g main.cpp -o main
I'm compiling in Debug mode (64-bit) in Netbeans with C++14 standard using the g++ compiler.
I call gdb after compiling by writing to the terminal:
gdb main
or by simply using the GUI in Netbeans
The compilers on macOS are confusing. Apple supplies the LLVM compiler as part of Xcode, but it provides a stub at /usr/bin/g++ which leads people to believe they are using GCC, i.e. the GNU Compiler Collection. They then try to use gdb, i.e. the GNU Debugger with the LLVM-produced executables and find it doesn't work.
IMHO, you either need to use wholly Apple-supplied tools, or wholly GNU-supplied tools.
So, let's look at the first option - using Apple tools. In this case you compile with:
/usr/bin/g++ -g main.cpp -o main
and debug with:
/usr/bin/lldb ./main
and the debugger commands seem pretty similar to GDB.
The second option is using GCC and GDB which are normally best installed using homebrew. The installation commands are:
brew install gcc
brew install gdb
Then you will compile with something like:
/usr/local/bin/g++-8 -g main.cpp -o main
and debug with something like:
/usr/local/bin/gdb ./main
However, I cannot get this to work on macOS Mojave for the moment! I read here that GDB v8.1 doesn't work on macOS and you need to install v8.0.1 instead but cannot seem to do that with homebrew.
I have an app that I have developed using QT Creator. The project builds and references a couple shared libraries that are in the build tree. When I try to debug my project using the remote debugger, I can't step into or break on any of the library code.
I have tried fixing it by setting LD_LIBRARY_PATH, using set-solib-search-path, etc.
More detailed info:
Host machine is a VM running Ubuntu 16.04
Target machine is a BeagleBone Black running Jessie
Everything is compiled as debug (with the -g flag)
My executable is in /home/debian
My library files are in /home/debian/lib
ls $LD_LIBRARY_PATH shows all of my library files, as expected
Start up command for GDB:
set solib-search-path /home/debian/lib
running gdb /home/debian/lib/mylib.so.1 yields the following:
GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from lib/mylib.so.1...done.
which implies that gdb is able to read the symbols.
However, running info shared library in the debugger console in qt yields
>~"From To Syms Read Shared Object Library\n"
>~" No /home/debian/lib/mylib.so.1\n"
Running sharedlibrary /home/debian/lib/mylib.so.1 doesn't seem to do anything; it just says "done".
Running add-symbol-file /home/debian/lib/mylib.so.1 fails with
The address where /home/debian/lib/mylib.so.1 has been loaded is missing
The application output screen says "Could not load shared library symbols" when it starts, and says "Do you need "set solib-search-path" or "set sysroot"?" but show solib-search-path prints out
The search path for loading non-absolute shared library symbol files is /home/debian/lib. as expected.
How can I resolve this so that I can debug my libraries?
EDIT:
If I debug on the target device, instead of remote debugging from Qt Creator, gdb can load the symbols, step into library code, and set breakpoints just peachily. Anyone have an idea as to why I can't remote debug?
I'm using WSL(Bash on Windows = Windows Subsystem for Linux).
I wrote a simple code, a.c
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello\n");
return 0;
}
I compiled it.
$ gcc -g -o a.exe a.c
$
It worked well.
$ ./a.exe
Hello
$
And, I tried to execute a.exe in gdb. However, I got "During startup program exited normally."
$ gdb ./a.exe
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.exe...done.
(gdb) r
Starting program: /home/softgear/a.exe
During startup program exited normally.
(gdb)
How can I use gdb in WSL? Help me, please.
Looks like this is already reported and fixed WSL bug.
As a workaround you can set disable-randomization off before debugging, see https://github.com/Microsoft/WSL/issues/2870#issuecomment-359664608. Or update WSL to a latest version.
I am trying to debug a simple "hello world" C++ program on Ubuntu 16.04 but gdb is not able to recognize the executable file format. However, I am able to successfully run the executable on the command line.
Here is the code
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
I compile the program file TestProject.cpp using the command
g++ -g TestProject.cpp -o hello
Then to debug, I give the command
gdb ./hello
I get the following error message
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
"/home/<home>/./hello": not in executable format: File format not recognized
Something seems to be corrupt with the Ubuntu machine. Because I am able to debug the same program on another Ubuntu 16.04 virtual machine.
It is almost certain that ks1322's comment is correct one:
You've installed a 64-bit GCC, so your ./hello is a 64-bit binary (use file ./hello to confirm).
You've installed a 32-bit only GDB, so it doesn't know how to debug x86_64 binaries.
The fix is simple: install 64-bit GDB (which is capable of debugging both 32 and 64-bit binaries), or build hello in 32-bit mode (with g++ -m32 ...).
I had the same issue on mac os.
there is a bug in gdb: https://sourceware.org/bugzilla/show_bug.cgi?id=23746
their git repository has already had the fix. Unfortunately, the bins in homebrew have not had it yet. So, I had to git clone git://sourceware.org/git/binutils-gdb.git, compiled it and installed as it is described in the readme file.
I do believe this will fix yours on ubuntu.
P.S. it works on my machine but I have to run eclipse as root:
sudo /.../MacOS/eclipse. Otherwise, I have Launching : Configuring GDB Aborting configuring GDB. Cause I do not know how to fix it (
Running GDB 7.4-2012.02 on Ubuntu and am experiencing weird behavior that I can't duplicate on other platforms.
bash$ export LD_LIBRARY_PATH=my_path
bash$ export LD_LIBRARY_PATH2=my_path2
bash$ gdb
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>.
(gdb) shell echo $LD_LIBRARY_PATH
(gdb) shell echo $LD_LIBRARY_PATH2
my_path2
As can be seen, GDB is resetting LD_LIBRARY_PATH but none of the other exported variables. Why is it doing this? Is there some setting I don't know about which causes this? My ~/.gdbinit is empty and the behavior persists even when using gdb -n.
Additionally, after exiting back to the login shell:
bash$ echo $LD_LIBRARY_PATH
my_path
bash$ $SHELL -c 'echo $LD_LIBRARY_PATH'
my_path
bash$ $SHELL
hi from .bashrc
bash$ echo $LD_LIBRARY_PATH
my_path
So I don't think my startup scripts are the issue.
So I don't think my startup scripts are the issue.
Yes, they are the issue (I know for a fact that GDB does not mess with your LD_LIBRARY_PATH environment).
Update:
Startup scripts were not the issue after all.
After some debugging, user2601195 discovered that the following variables were all getting unset: GCONV_PATH, GETCONF_DIR, HOSTALIASES, LD_AUDIT, LD_DEBUG, LD_DEBUG_OUTPUT, LD_DYNAMIC_WEAK, LD_LIBRARY_PATH, LD_ORIGIN_PATH, LD_PRELOAD
These are environment variables that glibc considers insecure, and unsets for setuid binaries. It turned out that in fact the gdb being invoked was suid-root:
-rwsr-sr-x 1 root root 5975928 Mar 15 2012 /usr/bin/gdb
which explains the problem.