C++ 如何使用GDB查找内存地址对应的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7639309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:16:06  来源:igfitidea点击:

How to use GDB to find what function a memory address corresponds to

c++gdb

提问by Nathaniel Flath

I am using google's heap checker to track down a memory leak. It gives me a stack trace such as:

我正在使用谷歌的堆检查器来追踪内存泄漏。它给了我一个堆栈跟踪,例如:

Leak of 21 bytes in 1 objects allocated from:                                                                                                                                                               
    @ 0xf6088241                                                                                                                                                                                               
    @ 0xf60890d2                                                                                                                                                                                               
    @ 0xf6089246                                                                                                                                                                                               
    @ 0x8054781                                                                                                                                                                                                
    @ 0x8054862                                                                                                                                                                                                
    @ 0xf684ee76                                                                                                                                                                                               
    @ 0xf684f343                                                                                                                                                                                               
    @ 0x804be4c                                                                                                                                                                                                
    @ 0x80544f6                                                                                                                                                                                                
    @ 0xf5e52bb6                                                                                                                                                                                               
    @ 0x804b101  

How do I determine what functions/lines of code these memory addresses correspond to?

如何确定这些内存地址对应的函数/代码行?

回答by ks1322

Use info symbolgdb command. 16 Examining the Symbol Table.

使用info symbolgdb 命令。16 检查符号表

info symbol addr

Print the name of a symbol which is stored at the address addr. If no symbol is stored exactly at addr, gdb prints the nearest symbol and an offset from it:

打印存储在地址 addr 中的符号名称。如果在 addr 中没有准确存储符号,则 gdb 会打印最近的符号及其偏移量:

(gdb) info symbol 0x54320
_initialize_vx + 396 in section .text

This is the opposite of the info address command. You can use it to find out the name of a variable or a function given its address.

这与 info address 命令相反。您可以使用它来找出变量名或给定地址的函数名。

For dynamically linked executables, the name of executable or shared library containing the symbol is also printed:

对于动态链接的可执行文件,还会打印包含该符号的可执行文件或共享库的名称:

(gdb) info symbol 0x400225
_start + 5 in section .text of /tmp/a.out
(gdb) info symbol 0x2aaaac2811cf
__read_nocancel + 6 in section .text of /usr/lib64/libc.so.6

回答by Brian Vandenberg

The original question asked how to do this in GDB:

原始问题询问如何在 GDB 中执行此操作:

# NOT what you want; note the lack of '*':
(gdb) info symbol 0xfde09edc
blah() + 388 in section .text of /tmp/libblah.so

# IS what you want; note the presence of '*':
(gdb) info line *0xfde09edc
Line 91 of "blah.cc"
   starts at address 0xfde09ebc <blah()+356>
   and ends at 0xfde09ee4 <blah()+396>

The *is necessary for info lineand shouldn't be used for info symbol.

*是必要的info line,不应该被用于info symbol

You can also use the disassemblecommand with its /mflag:

您还可以使用disassemble带有/m标志的命令:

(gdb) disassemble /m 0xfde09edc

... though it's rather verbose and info linegives exactly what was requested.

...虽然它相当冗长,并info line给出了所要求的内容。

回答by Mark B

Assuming your binary has debug information g++ -gyou may be able to use x/to get the info, I know that works for vtables.

假设你的二进制文件有调试信息,g++ -g你可以x/用来获取信息,我知道这适用于 vtables。

x/<num>xwto print <num>hex words of memory, and gdb will annotate the left side with information about what's at the address.

x/<num>xw打印<num>内存的十六进制字,gdb 将用有关地址内容的信息注释左侧。