如何找到使用gdb定义的文件?

Mat*_*att 6 debugging gdb path

当我键入list mystructgdb时,我收到用于定义mystruct的代码行。我怎样才能要求gdb给我读取文件以打印这些行?从gdb python接口获取该文件将是更可取的。越容易解析越好。

谢谢!

pmo*_*mod 5

For showing definition of a type there is a command ptype:

$ ptype mystruct
...
Run Code Online (Sandbox Code Playgroud)

To know where type is defined, command info types regex:

$ info types ^mystruct$
<filename>:<line>
Run Code Online (Sandbox Code Playgroud)

And to print lines of source file, command list filename:start_line,filename:end_line:

$ list myfile.c:100,myfile.c:110
Run Code Online (Sandbox Code Playgroud)

if not enough

$ list +
Run Code Online (Sandbox Code Playgroud)

Note that there is possible several same type difinitions, 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;
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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.