Search code examples
clinuxdebugginggdbdebug-symbols

GDB not mapping symbol files to stripped exe


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

Solution

  • 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.