kizzx2
kizzx2

Reputation: 19213

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!)

Upvotes: 8

Views: 8282

Answers (3)

ks1322
ks1322

Reputation: 35716

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

Upvotes: 7

Michael Snyder
Michael Snyder

Reputation: 5719

record DOES run on a Hello World program. Heck I use record to debug gdb itself!

Upvotes: 2

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84159

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.

Upvotes: 4

Related Questions