如何使用gdb找到该文件所定义的内容? [英] How do I find the file something was defined in using gdb?
问题描述
当我在gdb中输入 list mystruct
,我收到用于定义mystruct的代码行。我如何要求gdb给我正在读取的文件打印这些行?从gdb python接口获取该文件将是更可取的。
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.
谢谢!
推荐答案
显示类型的定义有一个命令 ptype :
For showing definition of a type there is a command ptype:
$ ptype mystruct
...
em> :
To know where type is defined, command info types regex:
$ info types ^mystruct$
<filename>:<line>
并打印源文件行,命令列表 filename:start_line,filename :end_line :
And to print lines of source file, command list filename:start_line,filename:end_line:
$ list myfile.c:100,myfile.c:110
如果不够
$ list +
请注意,可能有几种相同类型的差异,因此信息类型可以提供多个位置。
Note that there is possible several same type difinitions, so info types can give several locations.
更新
由于这是编译器之间的兼容性问题调试信息,例如DWARF)和 gdb ,因为某些原因,并不总是可以检索详细信息,例如电话号码。这可以通过使用特定的工具来解决。对于DWARF,有一个 dwarfdump 工具,可以访问文件中的所有DWARF信息。结构类型的输出
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;
}
看起来像:
$ 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.
输出格式不是很方便,而且很重,你应该编写自己的解析器。但是,使用 libdwarf 编写自己的工具可能会更好,或利用 pyelftools 在python上。 这里是一个例子。
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.
这篇关于如何使用gdb找到该文件所定义的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!