Introduced a script to capture leaks from malloc / free#44
Open
chimicus wants to merge 4 commits intoundoio:masterfrom
Open
Introduced a script to capture leaks from malloc / free#44chimicus wants to merge 4 commits intoundoio:masterfrom
chimicus wants to merge 4 commits intoundoio:masterfrom
Conversation
barisione
requested changes
Dec 7, 2022
| addr = frame.read_register("rdi") | ||
| for alloc in copy.copy(all_allocs): | ||
| if alloc.addr == addr: | ||
| all_allocs.remove(alloc) |
Collaborator
There was a problem hiding this comment.
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) |
Collaborator
There was a problem hiding this comment.
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}") |
Collaborator
There was a problem hiding this comment.
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}") |
Collaborator
There was a problem hiding this comment.
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a simple script that matches malloc and free calls. It can be expanded / adapted to track more sophisticated allocators.