no symbols (just question marks) in stack trace in remote gdb - c

I have a relatively simple application (that links in another simple library) that refuses to let me remotely debug it with gdb. I've checked that gdb and gdbserver versions match. It's actually even the same OS (ubuntu) on both machines. It seems to be happily loading symbols from the executable. So I'm at a bit of a loss for what could be wrong. Any suggestions appreciated. Here is the transcript from gdb:
dev:/fast/git/archive/foo$ gdb /fast/git/foo
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 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 /fast/git/foo...done.
(gdb) target remote test1:5000
Remote debugging using test1:5000
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
Reading symbols from target:/lib64/ld-linux-x86-64.so.2...Reading /lib64/ld-2.23.so from remote target...
Reading /lib64/.debug/ld-2.23.so from remote target...
(no debugging symbols found)...done.
0x00007ffff7dd7cc0 in ?? () from target:/lib64/ld-linux-x86-64.so.2
(gdb) bt
#0 0x00007ffff7dd7cc0 in ?? () from target:/lib64/ld-linux-x86-64.so.2
#1 0x0000000000000003 in ?? ()
#2 0x00007fffffffce02 in ?? ()
#3 0x00007fffffffce2f in ?? ()
#4 0x00007fffffffce32 in ?? ()
#5 0x0000000000000000 in ?? ()
Ah, so interesting. I'm still not sure why, but it only prints that (bogus stack trace) on connect. If I then "continue," it will happily print the right symbols if I trigger a break.
Odd...maybe this is partly user error, but I expected it to start running and break on the start on main when I connected.

I'm at a bit of a loss for what could be wrong
It could be that there is nothing wrong at all.
Here is what I get on a local machine:
gdbserver :0 /bin/date
Process /bin/date created; pid = 132826
Listening on port 57966
Remote debugging from host 127.0.0.1
In a separate window:
gdb -q /bin/date
(gdb) target remote localhost:57966
Remote debugging using localhost:57966
Loading symbols for shared libraries.
Loading symbols for shared libraries.
0x00007ffff7ddb2d0 in _start () at rtld.c:871
871 rtld.c: No such file or directory.
(gdb) bt
#0 0x00007ffff7ddb2d0 in _start () at rtld.c:871
#1 0x0000000000000001 in ?? ()
#2 0x00007fffffffe157 in ?? ()
#3 0x0000000000000000 in ?? ()
What's happening here is that gdbserver stopped the inferior (being debugged) process very early on. Before the loader zeroed out stack, and before entering main.
The symbols for main executable should already be loaded, and you should be able to set breakpoints on them. Set a breakpoint on main, and just continue from there, and you should soon see your breakpoint getting hit.
Update:
I expected it to start running and break on the start on main when I connected.
Your expectation is incorrect.
In a typical dynamically-linked binary, there are 1000s of instructions between _start and getting to main. Sometimes these instructions are the ones where a crash happens. If GDB automatically continued to main without giving you a chance to set breakpoints or watchpoints, then debugging such crashes would be much more difficult than it is now.

A few ideas:
Make sure the remote program and shared libraries are compiled with -g to add debug symbols. The (no debugging symbols found) message may mean that the debug symbols are missing from the whole executable.
Make sure the local and remote program are the same image. You can do a 'sum' on each. This may be the problem as the #1 0x0000000000000003 in ?? () line looks to be corrupt.
Are you able to manually debug the target on the remote box? If so, check for symbols to see if it something about the remote/local debug session. You can check for debug symbols by doing a list <function>
The address ranges are often mapped the same on local system versus remote. You can disassemble an address on a local debug session and see what function/method it maps to. This can be helpful if the remote program is crashing, at least you can get an idea of where the trouble is.

When debugging remote, gdb client does not know where to load symbols from. You have two options:
1. specify executable when starting gdb
gdb <executable>
(gdb) target remote <IP>:<port>
(gdb) load <executable>
gdb should know symbols now
(gdb) b main
(gdb) mon reset
(gdb) contnue
it should break at main
(gdb) bt
2. use file command to tell about the symbols.
gdb
(gdb) target remote <IP>:<port>
(gdb) load <executable>
(gdb) file <executable>
gdb should know symbols now
(gdb) b main
(gdb) mon reset
(gdb) contnue
it should break at main
(gdb) bt

Related

gdb failed with sigsev, don't know why?

In my project, I build three applications, in my Linux host machine.
Output binaries are,
main_process
update_data
db_process
Questions:
gdb handles only one executables to debug at one time?
All these three executables can't be run once in gdb?
When I run,
gdb ./main_process
Error message from gdb:
(gdb) r
Starting program: /home/iir/bin/
warning: `/lib/ld-linux.so.2': Shared library architecture i386 is not compatible with target architecture i386:x86-64.
warning: `/lib/ld-linux.so.2': Shared library architecture i386 is not compatible with target architecture i386:x86-64.
Failed to read a valid object file image from memory.
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7dda402 in ?? () from /lib/ld-linux.so.2
(gdb) bt
#0 0x00007ffff7dda402 in ?? () from /lib/ld-linux.so.2
Backtrace stopped: Cannot access memory at address 0x10074
EDIT: This question got downvotes, I want to mention here, the segfault caused because of running arm binaries in GDB. I've noticed that its bug in our build system where it didn't build actually for the arm arch instead x86.
Error message from gdb
(gdb) r
Starting program: /home/iir/bin/
The error message says that you are trying to execute /home/iir/bin/ which appears to be a directory. It is very unlikely that this is the actual result you observed after running gdb ./main_process.
Conclusion: you are not telling us what you actually did and what you observe, and trying to guess what that might be is a waste of time.
This question got downvotes, I want to mention here, the segfault caused because of running arm binaries in host GDB.
I've noticed that its bug in our build system where it didn't build actually for the arm arch instead x86.
Questions:
gdb handles only one executables to debug at one time?
Answer: Yes and I can able to pass another executable in as an argument using run.
All these three executables can't be run once in gdb?
Answer: No.

GDB Cannot Set a Breakpoint on Shared Library

I am trying to debug a program which loads a shared library. I can debug the main program parts perfectly but in the shared library, I run into some problems.
gdb -p 70876
GNU gdb (GDB) 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-apple-darwin15.5.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".
Attaching to process 70876
Reading symbols from /usr/local/pgsql/bin/postgres...done.
0x00007fff9496a2a2 in poll () from /usr/lib/system/libsystem_kernel.dylib
(gdb) b multi_ProcessUtility
Breakpoint 1 at 0xdaa9: multi_ProcessUtility. (2 locations)
(gdb) b multi_utility.c:129
Cannot access memory at address 0xdaa9
(gdb)
So, the problem is that, I can set a breakpoint with the method name itself and the program stops at the specified point. However, I cannot set the breakpoint with the filename and line number. (The specified filename and line number also addresses the same function)
When the program stops in the shared library function, running info source command result with No current source file. message
At the time I have attached to the process, the shared library is loaded and running.
I am on Mac OS X El Capitan, using GNU GCC 6 and GDB 7.11.1 from homebrew. I have compiled both the main program and the shared library with "-Og -ggdb -g3" flags.
I have solved my problem by downgrading to gdb 6.3.50.
Basically, I have installed gdb-apple from macports and it is based on gdb 6.3.50. I don't know whether it is related to gdb version or gdb-apple port has some tweaks that make it work.

How to set breakpoint in gdb when attach to another process

I have a C program which a really complicated script is written to run it. I need to debug this program using gdb. I have tried to run the script and attach gdb to it's process, but then I'm not able to set breakpoints that I need:
$ gdb median.o 27944
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 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 "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 median.o...done.
Attaching to program: median.o, process 27944
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.19.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007fc376e9cbfa in ?? ()
(gdb) break median.c:10
Cannot access memory at address 0x40059d
I also tried this:
$gdb -p 28303
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 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 "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".
Attaching to process 28303
Reading symbols from /bin/dash...(no debugging symbols found)...done.
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.19.so...done.
done.
Loaded symbols for /lib/x86_64-linux-gnu/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.19.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007fe918e50bfa in wait4 () at ../sysdeps/unix/syscall-template.S:81
81 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) break median.c:10
No source file named median.c.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (median.c:10) pending.
(gdb) continue
Continuing.
Please enter 3 numbers separated by spaces > 6 is the median
[Inferior 1 (process 28303) exited normally]
So it continues without stopping at the breakpoint. It is worth mentioning that if I call run median.o < input from gdb itself, it works.
How can I set breakpoints on a C program which is being run by a script?
This is a classic problem that people have with gdb. It's so common that you'd think it would have a handy name!
There are a few solutions to the problem, some time-tested and some relatively more experimental.
If the program to debug (in gdb lingo, "the inferior") is long-running -- for example, a GUI or a server of some kind -- then the simplest way is to just run the script, wait for the inferior to start, and then attach to it. You can attach using the PID, either with gdb -p PID or using attach PID at the gdb prompt.
If the program is short-lived, then another classic approach is to add a call to sleep early in the program's startup; say as the first line of main. Then, continue with the attach plan.
Those are the classic ways. But now let's talk about the more fun stuff.
gdb has a multi-inferior mode, where it can debug multiple processes at once. This mode, IME, is still a bit fragile, but I've had some success with it.
First you put gdb into the correct mode:
set detach-on-fork off
set non-stop off
set pagination off
(If you have an older gdb you will also need set target-async on).
Now you can debug the shell, something like:
$ gdb --args /bin/sh /path/to/my/script
(gdb) [... set the mode as above ...]
(gdb) break some_function_in_my_inferior
Now run should start the script, and automatically attach gdb to each child process that's created; eventually stopping at the breakpoint.
There's still one more way. A long time ago, there was a kernel patch to add "global breakpoints", plus a gdb patch to work with this feature. As far as I know, none of this was ever merged. But, I wrote a variant in my gdb helpers project.
There is a new command there called preattach. What it does is use SystemTap to watch for an exec of a specified program; then it pauses this program while gdb attaches to it.

GDB does not load symbols from libraries

I try to debug some native code on Android with GBD. The code wasn't created by me and is not in an Android project, so I can't use the ndk-gdb tool. I use gdbserver on the android machine and connect to it from my mac with the normal GDB program. I try to load all the libraries (which should have symbols according to objdump tool), but gdb tells me that it does not load the symbols (according to the gdb command “info sharedLibrary”). These are the steps I took:
start gdbserver on Android machine
start GDB with the debug version of the binary
gdb symbols/system/bin/mediaserver
the following commands are executed in gdb itself
tell gdb where to look for the libraries with symbols
(gdb) set solib-search-path symbols/system/lib
tell gdb where to find the source files
(gdb) directory /sources
connect to remote target (Android machine)
(gdb) target remote 192.168.1.10:5039
GDB connects successfully to the running binary and I can pause and continue the execution. But it does not show me any debug information like function names or line numbers. It only shows adresses. When I check the status of the used libraries, I see that gdb thinks, they don’t have any symbols:
command in gdb:
(gdb) info sharedLibrary
From To Syms Read Shared Object Library
0x00003700 0x0000ff0c Yes /symbols/system/bin/linker
No libc.so
No libstdc++.so
No libm.so
No liblog.so
No libcutils.so
No libspeexresampler.so
No libaudioutils.so
No libgccdemangle.so
No libamplayer.so
Now for example the last library. When I check with the file command (not in gdb), it tells me that it is a not stripped library (the library is located in the "symbols/system/lib" folder).
file libamplayer.so
Output:
libamplayer.so: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked, not stripped
objdump command shows a lot of symbols in it (I don’t show the output because it’s very long). So why does gdb not recognise the symbols in this libraries? I thought, at least line numbers and function names are present in not stripped versions of libraries. Or am I wrong? It would be very nice if someone could give me more insight.
Thanks!
System info:
GDB Version: 7.3.1-gg2 on Mac OS X Mavericks
The code wasn't created by me and is not in an Android project, so I can't use the ndk-gdb tool.
Your conclusion does not at all follow. ndk-gdb should be able to debug any Android program, whether created as a "project" or via other means.
I use gdbserver on the android machine and connect to it from my mac with the normal GDB program.
The normal GDB is likely not configured for cross-debugging, and thus doesn't understand ARM binaries at all. I am surprised you get as far using it as you do.

How to use tracing in GDB

In the documentation for gdb:
The tracepoint facility is currently available only for remote
targets. See section Specifying a Debugging Target. In addition, your
remote target must know how to collect trace data. This functionality
is implemented in the remote stub; however, none of the stubs
distributed with GDB support tracepoints as of this writing.
Emphasis mine. Where can I get such a stub (for C/C++ code compiled with GCC on Debian x86 or x64)? Or how do I go about making one? The documentation on writing stubs only mentions implementing functions to communicate with the serial ports. Thanks!
I don't know much about remotes but some targets in gdb now do support tracepoints
there is possibly a way to get at this using a 'normal' gdb info or show command, I could not find it. in the output below tracepoints are supported due to the 'supported=1',
this may not be limited to the gdb stub, but also the kernel the stub is running on.
$ gdbserver/gdbserver :12345 ~/tests/test &
$ gdb -quiet
(gdb) file ~/tests/test
Reading symbols from /home/ratmice/tests/test...done.
(gdb) target remote :12345
Remote debugging using :12345
Remote debugging from host 127.0.0.1
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00000035dd600b20 in _start () from /lib64/ld-linux-x86-64.so.2
Created trace state variable $trace_timestamp for target's variable 1.
Missing separate debuginfos, use: debuginfo-install glibc-2.13-2.x86_64
(gdb) interpreter-exec mi2 -trace-status
^done,supported="1",running="0",frames="0",frames-created="0",buffer-size="5242880",buffer-free="5242880",disconnected="0",circular="0"

Resources