Home > database >  GDB not mapping symbol files to stripped exe
GDB not mapping symbol files to stripped exe

Time:09-22

I have a C file, I compiled with -g option and saved the debug symbol to a different file and stripped the executable. When I try to debug this executable with set debug-file-directory /root/test, I am not able to set breakpoints. I guess the symbol file mappings are not working. Can some one give some inputs here?

#include <stdio.h>
void func1()
{
}

int main() {
FILE *fp;
fp  = fopen ("/tmp/abcdefg", "w");
func1();
}

Compiled with debug symbols;

gcc -g file.c

saved the debug symbols to a different file in the same directory.

strip --strip-debug a.out -o a.out.debug

stripped the executable;

strip a.out

The exe a.out and debug symbol file a.out.debug are in the /root/test directory.

Starting gdb session;

root@ubuntu:~/test# gdb a.out
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
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-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.out...(no debugging symbols found)...done.
(gdb) set debug-file-directory /root/test
(gdb) show debug-file-directory
The directory where separate debug symbols are searched for is "/root/test".
(gdb) b main
Function "main" not defined.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) q
You have new mail in /var/mail/root

CodePudding user response:

As the busybee said, some of your commands are incorrect.

You should checkout the gdb documentation on separate debug files for a fuller description, however, the commands you need are:

$ gcc -g file.c
$ strip --only-keep-debug a.out -o a.out.debug
$ strip --strip-debug a.out
$ objcopy --add-gnu-debuglink=a.out.debug a.out
$ gdb -q a.out
Reading symbols from a.out...
Reading symbols from /home/andrew/tmp/strip-demo/a.out.debug...
(gdb) 

The strip --only-keep-debug copies just the debug sections from a.out into a.out.debug.

Then strip --strip-debug removes just the debug sections, nothing else, from a.out.

The objcopy --add-gnu-debuglink=a.out.debug adds a small section to a.out that tells GDB the name of the file containing the debug information. GDB uses some built in rules, as well as debug-file-directory to look for the file named in the debug link.

Finally, when we start GDB we can see that it has found the external debug information.

CodePudding user response:

You have at least two errors:

strip --strip-debug a.out -o a.out.debug does not do what you think.

Instead of "exporting" the debug information, it removes this and saves the result in "a.out.debug". The result is an executable with debug information.

Look up the option --only-keep-debug as a next step.

You did not load any debug information into gdb

After launching GDB, use the symbol-file command to load the debugging symbols. I don't think that GDB can find debug information automatically, because it cannot know the name of the file.

You don't need to change the debug-file-directory for this.

  • Related