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