When I type list mystruct
into gdb, I receive the lines of code used to define mystruct. How can I ask gdb to give me the file it is reading from to print those lines? Getting that file from the gdb python interface would be preferable. The more easily parsable the better.
Thanks!
For showing definition of a type there is a command ptype:
$ ptype mystruct
...
To know where type is defined, command info types regex:
$ info types ^mystruct$
<filename>:<line>
And to print lines of source file, command list filename:start_line,filename:end_line:
$ list myfile.c:100,myfile.c:110
if not enough
$ list +
Note that there is possible several same type definitions, so info types can give several locations.
Update
Since this is a matter of compatibility between compiler (that generates debugging information, e.g. DWARF) and gdb that reads it, for some reason, it's not always possible to retrieve detailed information, e.g. line number. This can be workaround-ed by using specific tools, e.g. for DWARF there is a dwarfdump tool, that has access to all DWARF information in the file. The output for structure type
struct mystruct {
int i;
struct sample *less;
}
looks like:
$ dwarfdump -ie ./a.out
...
< 1><0x00000079> structure_type
name "mystruct"
byte_size 0x0000000c
decl_file 0x00000002 ../sample.h
decl_line 0x00000003
sibling <0x000000a8>
< 2><0x00000085> member
name "i"
decl_file 0x00000002 ../sample.h
decl_line 0x00000004
type <0x0000004f>
data_member_location 0
< 2><0x0000008f> member
name "less"
decl_file 0x00000002 ../sample.h
decl_line 0x00000005
type <0x000000a8>
data_member_location 4
Here you have information on which line not only type declaration starts, but also line number for each member.
The output format is not very convenient, and heavy - you should write your own parser. But it could be better to write your own tool using libdwarf or utilize pyelftools on python. Here is one of examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With