如何使用GDB查找一个内存地址对应什么函数

2022-01-20 00:00:00 gdb c++

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

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?

推荐答案

使用info symbol gdb 命令.16 检查符号表.

Use info symbol gdb command. 16 Examining the Symbol Table.

info symbol addr

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

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:

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

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

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.

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

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

相关文章