简体   繁体   English

如何释放内存时在gdb中得到通知?

[英]How to get notified in gdb when memory is freed?

I'm debugging a memory issue in C. The piece of memory I'm accessing has been accidentally free() :d by somebody else's module. 我正在调试C中的内存问题。我正在访问的内存块被意外free() :由其他人的模块执行。 Is there a way in gdb to get notified when a piece of memory is free() :d? 当一块内存free()时, gdb有没有办法得到通知free() :d?

Suppose your libc's free 's argument is called mem . 假设你的libc的free参数叫做mem

Then, you can print out everything that is freed: 然后,您可以打印出已释放的所有内容:

(gdb) break __GI___libc_free # this is what my libc's free is actually called
Breakpoint 2 at 0x7ffff7af38e0: file malloc.c, line 3698.
(gdb) commands 2
Type commands for when breakpoint 2 is hit, one per line.
End with a line saying just "end".
>print mem
>c
>end

Now, every time anyone frees anything, you will get a little printout (you can omit c if you want it to stop every time free occurs): 现在,每当有人释放任何东西时,你都会得到一点打印输出(如果你想让它在每次free时都停止,你可以省略c ):

Breakpoint 2, *__GI___libc_free (mem=0x601010) at malloc.c:3698
3698    malloc.c: No such file or directory.
    in malloc.c
$1 = (void *) 0x601010

Or, if you already know what memory address you are interested in, use cond to break when someone tries to free that address: 或者,如果您已经知道您感兴趣的内存地址,请在有人试图free该地址时使用cond中断:

(gdb) cond 2 (mem==0x601010)
(gdb) c
Breakpoint 3, *__GI___libc_free (mem=0x601010) at malloc.c:3698
3698    malloc.c: No such file or directory.
    in malloc.c
(gdb) 

In order to get info about memory leaks the following tools will be really helpful. 为了获得有关内存泄漏的信息,以下工具将非常有用。

  1. Valgrind Valgrind的

  2. Google Perf Tools 谷歌Perf工具

And it doesn't take very long to get used to working with these - definitely worth giving a try. 习惯使用它们并不需要很长时间 - 绝对值得一试。

Or using a hardware watch point to keep track of certain addresses might help - the debugger gets control whenever a read or write happens to the addresses you are watching - but I'm not sure if this offers the exact solution to your problem. 或者使用硬件监视点来跟踪某些地址可能会有所帮助 - 只要对您正在观看的地址进行读取或写入,调试器就会获得控制权 - 但我不确定这是否能为您的问题提供准确的解决方案。

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

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