Skip to content

Conversation

@chimicus
Copy link
Contributor

@chimicus chimicus commented Dec 5, 2022

This is a simple script that matches malloc and free calls. It can be expanded / adapted to track more sophisticated allocators.

addr = frame.read_register("rdi")
for alloc in copy.copy(all_allocs):
if alloc.addr == addr:
all_allocs.remove(alloc)
Copy link
Collaborator

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)
Copy link
Collaborator

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}")
Copy link
Collaborator

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}")
Copy link
Collaborator

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 spaces
  • 018: pad to 18 characters with zeroes
  • ,: format numbers with commas every 3 digits
  • x: use hexadecimal
  • #: add 0x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants