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

发布于 2024-12-08 00:39:58 字数 2399 浏览 0 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽清风入怀 2024-12-15 00:39:58

使用信息符号 gdb 命令。 16 检查符号表

info symbol addr

打印存储在地址 addr 处的符号名称。如果没有符号准确地存储在 addr 处,gdb 将打印最近的符号以及距该符号的偏移量:

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

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

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

(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

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

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:

(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.

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
与酒说心事 2024-12-15 00:39:58

最初的问题询问如何在 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>

* 对于 info line 是必需的,不应该用于 info symbol

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

(gdb) disassemble /m 0xfde09edc

...尽管它相当冗长,并且 info line 准确地给出了所请求的内容。

The original question asked how to do this in 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.

You can also use the disassemble command with its /m flag:

(gdb) disassemble /m 0xfde09edc

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

樱娆 2024-12-15 00:39:58

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文