簡體   English   中英

如何釋放內存時在gdb中得到通知?

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

我正在調試C中的內存問題。我正在訪問的內存塊被意外free() :由其他人的模塊執行。 當一塊內存free()時, 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) 

為了獲得有關內存泄漏的信息,以下工具將非常有用。

  1. Valgrind的

  2. 谷歌Perf工具

習慣使用它們並不需要很長時間 - 絕對值得一試。

或者使用硬件監視點來跟蹤某些地址可能會有所幫助 - 只要對您正在觀看的地址進行讀取或寫入,調試器就會獲得控制權 - 但我不確定這是否能為您的問題提供准確的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM