Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In GDB, how to find out who malloc'ed an address on the heap?

I have a pointer in GDB, how can I find out where it was first allocated on the heap?

In WinDBG, this can be done by !heap -p -a <0x12345678> after turning on gflags /i <*exe> +ust

Since Valgrind can tell me where the memory is allocated (when it detects some leaks), I guess this is also possible?

(This is NOT about watchpoint. This is given the situation where I randomly break into the In GDB, application, look at a pointer and want to know "who created this piece of memory"?)


Using reverse debugging in GDB is a very novel way and probably the correct way to solve this problem. I encountered some problem with that approach with GDB 7.1 -- the latest stable version. Reverse debugging is a rather new feature in GDB so I needed to check out HEAD (7.2) to fix it.

It probably says something about the matureness of the GDB approach but I think it should definitely be used when it's more mature. (Awesome feature!)

like image 809
kizzx2 Avatar asked Jul 26 '10 09:07

kizzx2


Video Answer


2 Answers

Maybe reverse debugging will help here. Try to set watchpoint on memory address and reverse-continue until memory written.

(gdb) watch *0x12345678
(gdb) reverse-continue
like image 167
ks1322 Avatar answered Sep 21 '22 14:09

ks1322


Valgrind hijacks memory management calls, that's how heap checkers work. There's no facility in GDB itself to tell you where given address was returned by malloc(3). I suggest looking into mtrace and glibc allocation debugging.

like image 36
Nikolai Fetissov Avatar answered Sep 20 '22 14:09

Nikolai Fetissov