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

H_J*_*H_J 4 c memory debugging gdb

我正在调试C中的内存问题.我正在访问的内存是偶然的free():d由其他人的模块.有没有办法在gdb一段内存时得到通知free():d?

nne*_*neo 9

假设你的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
Run Code Online (Sandbox Code Playgroud)

现在,每当有人释放任何东西时,你都会得到一点打印输出(c如果你想让它在每次free发生时都停止,你可以省略):

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
Run Code Online (Sandbox Code Playgroud)

或者,如果您已经知道自己感兴趣的内存地址,请cond在有人试图访问free该地址时使用:

(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) 
Run Code Online (Sandbox Code Playgroud)