Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

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.

leak_detector/README.md Show resolved Hide resolved
leak_detector/README.md Show resolved Hide resolved
leak_detector/README.md Show resolved Hide resolved
leak_detector/leak_detector.py Show resolved Hide resolved
leak_detector/leak_detector.py Show resolved Hide resolved
leak_detector/leak_detector.py Show resolved Hide resolved
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