【发布时间】:2012-10-06 06:59:40
【问题描述】:
我正在调试 C 中的内存问题。我正在访问的内存块意外地被其他人的模块 free():d 所访问。 gdb 有没有办法在内存为free():d 时得到通知?
【问题讨论】:
我正在调试 C 中的内存问题。我正在访问的内存块意外地被其他人的模块 free():d 所访问。 gdb 有没有办法在内存为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)
【讨论】:
为了获取有关内存泄漏的信息,以下工具将非常有用。
而且很快就会习惯使用这些 - 绝对值得一试。
或者使用硬件观察点来跟踪某些地址可能会有所帮助 - 每当您正在观察的地址发生读取或写入时,调试器就会获得控制 - 但我不确定这是否能准确解决您的问题.
【讨论】: