简体   繁体   English

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

[英]How to use GDB to find what function a memory address corresponds to

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? 如何确定这些内存地址对应的代码功能/行?

Use info symbol gdb command. 使用info symbol gdb命令。 16 Examining the Symbol Table . 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中,gdb将打印最近的符号及其偏移量:

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

This is the opposite of the info address command. 这与info address命令相反。 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

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 line and shouldn't be used for info symbol . *info line所必需的,不应用于info symbol

You can also use the disassemble command with its /m flag: 您还可以使用带有/m标志的disassemble命令:

(gdb) disassemble /m 0xfde09edc

... though it's rather verbose and info line gives exactly what was requested. ...虽然它相当冗长,但info line给出了所要求的内容。

Assuming your binary has debug information g++ -g you may be able to use x/ to get the info, I know that works for vtables. 假设您的二进制文件具有调试信息g++ -g您可以使用x/来获取信息,我知道这适用于vtables。

x/<num>xw to 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将在左侧注释有关该地址的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM