如何在释放内存时通知gdb?(How to get notified in gdb when memory is freed?)

我正在用C调试一个内存问题。我正在访问的那块内存被意外free() :d被其他人的模块free() 。 当一块内存free()时, gdb有没有办法得到通知free() :d?

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. Is there a way in gdb to get notified when a piece of memory is free():d?

最满意答案

假设你的libc的free参数被称为mem 。

然后,您可以打印出已释放的所有内容:

(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

现在,每当有人释放任何东西时,你都会得到一点打印输出(如果你想让它在每次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

或者,如果您已经知道您感兴趣的内存地址,请在有人试图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)

Suppose your libc's free's argument is called 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):

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:

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

更多推荐