Skip to content

Instantly share code, notes, and snippets.

@icewall icewall/alloc_tracer.py
Last active Sep 30, 2016

Embed
What would you like to do?
Very very limited imitation of windbg !heap -p -a addr for gdb based on gdb python.
#!/usr/bin/env python
allocations = {}
class FunctionFinishBreakpoint(gdb.FinishBreakpoint):
def __init__ (self,allocation):
gdb.FinishBreakpoint.__init__(self,gdb.newest_frame(), internal=True)
self.silent = True
self.allocation = allocation
def stop(self):
global allocations
self.allocation["address"] = int(self.return_value)
allocations[ self.allocation["address"] ] = self.allocation
return False
class MallocBreakpoint(gdb.Breakpoint):
def stop(self):
global allocations
allocation_size = gdb.parse_and_eval("bytes")
allocation_size = int(allocation_size)
stack = gdb.execute("bt", to_string = True)
allocation = {"size": allocation_size, "stack" : stack}
FunctionFinishBreakpoint(allocation)
return False
print ("Installing malloc breakpoint")
MallocBreakpoint("__GI___libc_malloc")
@icewall

This comment has been minimized.

Copy link
Owner Author

commented May 20, 2016

Example of usage:
b main
start
source alloc_tracer.py
Installing malloc breakpoint
c
(...)
*** SIGSAV SOMEWHERE ***

print(allocations['0x1058010']["stack"])

0 __GI___libc_malloc (bytes=0x7ffd75ee9b10) at malloc.c:2876

1 0x00007f0bbccc9dad in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6

2 0x00007f0bbccc9ea9 in operator new[](unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6

3 0x0000000000400714 in Test::Test() ()

4 0x000000000040079f in main ()

5 0x00007f0bbc8c7ec5 in __libc_start_main (main=0x400784 , argc=0x2, argv=0x7ffd75ee9c28, init=, fini=, rtld_fini=, stack_end=0x7ffd75ee9c18) at libc-start.c:287

6 0x0000000000400639 in _start ()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.