-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduced a script to capture leaks from malloc / free #44
base: master
Are you sure you want to change the base?
Conversation
addr = frame.read_register("rdi") | ||
for alloc in copy.copy(all_allocs): | ||
if alloc.addr == addr: | ||
all_allocs.remove(alloc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a set and then you can just do .remove(alloc)
. This requires MemAlloc
to be hashable and not modifiable so using a frozen data class as I suggested above will make it work.
@staticmethod | ||
def invoke(arg, from_tty): | ||
gdb.Breakpoint(ALLOC_FN) | ||
gdb.Breakpoint(FREE_FN) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should remove the breakpoints you create after you are done. If you follow my suggestions from above you will have:
allocations: set[MemAlloc] = set()
try:
alloc_bp = AllocBreakpoint()
free_bp = FreeBreakpoint()
[... rest of the function ...]
finally:
alloc_bp.delete()
free_bp.delete()
gdb.execute("continue") | ||
print("Calls to allocator fn that don't have a corresponding free") | ||
for alloc in all_allocs: | ||
print(f"{hex(alloc.addr)} - {hex(alloc.size)} - {alloc.bbcount}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you use a set then the results will be in an arbitrary order. You can do this:
for alloc in sorted(allocations, key=lambda ma: ma.bbcount):
...
gdb.execute("continue") | ||
print("Calls to allocator fn that don't have a corresponding free") | ||
for alloc in all_allocs: | ||
print(f"{hex(alloc.addr)} - {hex(alloc.size)} - {alloc.bbcount}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few possible improvements here:
- Show the bbcount as first element as it's what you are sorting on
- Show a range of addresses, not just the start
- Don't use an hex for the size
- Use commas for long bbcounts and sizes
- Pad numbers so they aligned
Maybe something like this:
print(f"{alloc.bbcount:18,}: {alloc.addr:#018x} - {alloc.addr + alloc.size:#018x} (size={alloc.size:,})")
The various bits after the :
characters mean:
18
: pad to 18 characters with spaces018
: pad to 18 characters with zeroes,
: format numbers with commas every 3 digitsx
: use hexadecimal#
: add0x
This is a simple script that matches malloc and free calls. It can be expanded / adapted to track more sophisticated allocators.