I have a C library which is created with
cc -fPIC -g -O3 -c -o obj/my_lib.o my_lib.c
g++ -shared -Wl,-soname,libmy_lib.so.1 obj/my_lib.o -o libmy_lib.so.1.8.0
This library is packaged into debian packages with dpkg-buildpackage
producing libmy_lib1-1.deb
, libmy_lib1-dev-1.deb
, and libmy_lib1-dbgsym-1.ddeb
. Installing all of those packages, I can then compile/link a simple test program that calls into the library. This works. Running the test program works.
However, when I run GDB on the test program (on the same computer), I see
gdb$ break main
Breakpoint 1 at 0x87e: file test.c, line 10.
gdb$ info sharedlibrary
No shared libraries loaded at this time.
gdb$ r
Starting program: /tmp/a.out
Breakpoint 1, main () at test.c:10
10 my_library_func();
gdb$ info sharedlibrary
From To Syms Read Shared Object Library
0x00007ffff7dd5f10 0x00007ffff7df4b20 Yes /lib64/ld-linux-x86-64.so.2
0x00007ffff7bac9a0 0x00007ffff7bad438 Yes /usr/lib/x86_64-linux-gnu/libmy_lib.so.1
0x00007ffff74532d0 0x00007ffff75cbc3c Yes /lib/x86_64-linux-gnu/libc.so.6
0x00007ffff709fa80 0x00007ffff715e2f5 Yes /lib/x86_64-linux-gnu/libm.so.6
0x00007ffff6e7eac0 0x00007ffff6e8f36d Yes /lib/x86_64-linux-gnu/libgcc_s.so.1
(*): Shared library is missing debugging information.
gdb$ s
my_library_func () at my_lib.c:299
299 my_lib.c: No such file or directory.
As you can see, GDB knows about the debug symbols for the library. However, it does not know about the source file for the library. How should I running GDB to it can resolve the C source code?
2
Answers
GDB searches a number of default directory paths to locate the specified sourcefile. You can add paths using the
directory
command: https://sourceware.org/gdb/current/onlinedocs/gdb/Source-Path.htmlYou need to also tell gdb where the source files are. Which means you also need the source files, not just the debugging symbols.
It’s important that the sources you download are the actual ones used to compile the library, because debugging information only contains filename and line number. If you give gdb a file where the line numbers don’t correspond (a different version, for example), the source lines printed by gdb will be very confusing. It has no way to know they are wrong. You should be able to use the src deb with the same version number as the library debs.
Once you have the source files, tell gdb where to look for them with
You can specify several paths. Read
help directory
inside gdb.Since you’ll need to do this often, put that line into a gdbinit file. You’ll probably want to use
.gdbinit
in your current directory, but.gdbinit
in your home directory might also be a possibility. Gdb uses both.If you’re working with a library whose source is spread over a subdirectory tree, you might find it useful to set a substitution path:
Again, more help is available with
help set substitute-path
.