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!)开发者_高级运维
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
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.
record DOES run on a Hello World program. Heck I use record to debug gdb itself!
精彩评论