How to get notification in gdb when freeing memory?

I am debugging a memory problem in C. The parts of the memory that I refer to were accidentally free() : d by another module. Is there a way in gdb to get notified when a piece of memory is free() : d?

+4
source share
2 answers

Suppose your libc free argument is called mem .

Then you can print everything that has been released:

 (gdb) break __GI___libc_free # this is what my libc 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 someone releases something, you get a small printout (you can omit c if you want it to stop free each time):

 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 which memory address you are interested in, use cond to break when someone tries to specify this address 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) 
+9
source

The following tools will be useful to get information about memory leaks.

And it doesn't take long to get used to working with them - definitely worth a try.

Or using a hardware watchpoint to track specific addresses can help - the debugger gets control whenever a read or write occurs with the addresses you are viewing - but I'm not sure if this offers an exact solution to your problem.

+2
source

Source: https://habr.com/ru/post/1438133/


All Articles