R Yoda
R Yoda

Reputation: 8780

Identify source file name for a symbol in gdb debugger

I am debugging an application based on multiple C++ shared libraries in gdb on Ubuntu 18.04 and want to find the source code (or at least the name of the shared library or C++ namespace) of a symbol called TRUE.

All code has been compiled with -g3 -O0 -Wall.

Edit: I have "only" the debug symbols available for shared libraries and the application code is compiled with -g3 -O0 -Wall.

Here is what I know about the symbol in gdb:

(gdb) ptype TRUE
type = enum {FALSE, TRUE}
(gdb) whatis TRUE
type = enum {...}
(gdb) info line TRUE
(gdb) 

How can I find out who defined the enum of my TRUE symbol`?

There is a similar question but the answer does not give the expected output (but empty, see log above): https://stackoverflow.com/a/20771062

Edit: I am one step further (but still the full path is missing for such a common kind of file name):

(gdb) python print(gdb.lookup_symbol("TRUE")[0].symtab)
api.cpp

Upvotes: 5

Views: 2766

Answers (2)

R Yoda
R Yoda

Reputation: 8780

I have found it finally! Python is the solution, the gdb object within Python does offer more detailed information than the pure native gdb commands:

(gdb) python print(gdb.lookup_symbol("TRUE")[0].symtab.fullname())
/media/devmount/dev/R/Rcpp/src/api.cpp

For details see the documentation: https://sourceware.org/gdb/download/onlinedocs/gdb/Symbol-Tables-In-Python.html#Symbol-Tables-In-Python

Edit: I have found a gdb command for that but it seems not to work in my case:

(gdb) set print symbol-filename on
(gdb) show print symbol-filename
Printing of source filename and line number with <symbol> is on.
(gdb) ptype TRUE
type = enum {FALSE, TRUE}

Upvotes: 6

Florian Weimer
Florian Weimer

Reputation: 33757

I'm not sure if GDB exposes this information. It is recorded in the DWARF data, so you can dump it using dwarfdump or eu-readelf -w. The latter is part of elfutils and it produces output like this:

DWARF section [11] '.debug_macro' at offset 0xac:

 Offset:             0x0
 Version:            4
 Flag:               0x2 (debug_line_offset)
 Offset length:      4
 .debug_line offset: 0x0

 #include offset 0x0
 start_file 0, [1] file.c
  start_file 31, [2] /usr/include/stdc-predef.h
   #include offset 0x0
  end_file
  #define FALSE 0, line 1 (indirect)
  #define TRUE 1, line 2 (indirect)
 end_file

Assuming that file.c contains:

#define FALSE 0
#define TRUE 1

Given the nested structure of DWARF, this is not something that can easily be processed by tools like grep, though.

Upvotes: 3

Related Questions