0

I have a C program foo which I am compiling and debugging with GDB just fine. (I'm using QtCreator as an IDE if relevant).

When I call function bar(), in library libbar.so.1, in debian package libbar1, I don't get the expected result so I'd like to step into it.

  • apt source libbar1 gets sources for libbar.so.1 and I've found the implementation of bar() in /tmp/libbar-1.0/src/bar.c.
  • apt install libbar1-dbgsym installs the debugging symbols to /usr/lib/debug/.build-id/{22,fb}/*.debug

How can I debug foo and step into the implementation of bar() in GDB?


I suspect I must have to add startup commands to GDB with the location of debug symbols and sources.

I've tried adding this to gdb startup commands:

> directory /tmp/libbar-1.0/src/

Next, I'm thinking it might be related to set substitute-path from to, but I'm not sure whether /tmp/libbar-1.0/src goes in from or to and what the other one would be.

Stewart
  • 4,356
  • 2
  • 27
  • 59
  • 1
    My answer in [GDB complaining about missing raise.c](https://stackoverflow.com/questions/48278881/gdb-complaining-about-missing-raise-c), which shows how to tell GDB where it should look for the glibc sources, may help here. So (1) place a breakpoint at `bar` and run until it stops there (2) type `info source` to see the compilation-dir (3) type `set substitute-path compilation-dir dir-with-sources` – Mark Plotnick Jun 03 '21 at 22:00
  • Please don't use `libbar` -- show the _actual_ library you are having trouble with. – Employed Russian Jun 04 '21 at 04:02
  • @MarkPlotnick The IDE I'm using doesn't make it easy for me to "type `info source`". But your other answer really helped. Now that I've gone through the whole process and re-read your comment, you were spot on. It just took me some time to understand it. – Stewart Jun 04 '21 at 08:16

1 Answers1

0

GDB complaining about missing raise.c gave me a start to solving this. This is the full solution where I don't have the luxury of halting on a signal during runtime:

We need the following from the Debian archive:

  1. sudo apt install libbar1: This deploys /usr/lib/x86_64-linux-gnu/libbar.so.1. It's already installed if your program is running.
  2. sudo apt install libbar1-dbgsym: This deploys /usr/lib/debug/.build-id/22/*.debug
  3. apt source libbar1: This deploys ./libbar-1.0/src/bar.c

Now we need to open the library in gdb and look for the original source path:

$ gdb /usr/lib/x86_64-linux-gnu/libbar.so.1
...
Reading symbols from /usr/lib/x86_64-linux-gnu/libbar.so.1...Reading symbols from /usr/lib/debug/.build-id/22/8b32043be712018feacacb319a48ebf5400ebf.debug...done
(gdb) info sources 
./obj-x86_64-linux-gnu/./src/bar.c

The ./obj-x86_64-linux-gnu/. is important. This was the original location where the sources were built. We need to substitute that with the location of our sources so GDB can find them and add that new path to our list of searchable sources:

(gdb) set substitute-path ./obj-x86_64-linux-gnu/. /tmp/libbar-1.0
(gdb) directory /tmp/libbar-1.0
Source directories searched: /tmp/libbar-1.0:$cdir:$cwd
(gdb) info sources 
/tmp/libbar-1.0/src/bar.c
(gdb) list bar
2       int bar(int x) {
3         return x+1;
4       }

You're using QtCreator to drive gdb. In that case under "Tools > Options > Debugger > GDB" there is a text box labeled: "Additional Startup Commands". Add the following to that text box and you can debug!

set substitute-path ./obj-x86_64-linux-gnu/. /tmp/libbar-1.0
directory /tmp/libbar-1.0
Stewart
  • 4,356
  • 2
  • 27
  • 59