Commit 9389ab48 authored by Maria Kacik's avatar Maria Kacik

[memleak] add parameter for specifying object to load malloc/free from

parent 3cc4c0a2
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
memleak \- Print a summary of outstanding allocations and their call stacks to detect memory leaks. Uses Linux eBPF/bcc. memleak \- Print a summary of outstanding allocations and their call stacks to detect memory leaks. Uses Linux eBPF/bcc.
.SH SYNOPSIS .SH SYNOPSIS
.B memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND] [-s SAMPLE_RATE] .B memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND] [-s SAMPLE_RATE]
[-T TOP] [-z MIN_SIZE] [-Z MAX_SIZE] [INTERVAL] [COUNT] [-T TOP] [-z MIN_SIZE] [-Z MAX_SIZE] [-O OBJ] [INTERVAL] [COUNT]
.SH DESCRIPTION .SH DESCRIPTION
memleak traces and matches memory allocation and deallocation requests, and memleak traces and matches memory allocation and deallocation requests, and
collects call stacks for each allocation. memleak can then print a summary collects call stacks for each allocation. memleak can then print a summary
...@@ -53,6 +53,9 @@ Capture only allocations that are larger than or equal to MIN_SIZE bytes. ...@@ -53,6 +53,9 @@ Capture only allocations that are larger than or equal to MIN_SIZE bytes.
\-Z MAX_SIZE \-Z MAX_SIZE
Capture only allocations that are smaller than or equal to MAX_SIZE bytes. Capture only allocations that are smaller than or equal to MAX_SIZE bytes.
.TP .TP
\-O OBJ
Attach to malloc and free in specified object instead of resolving libc. Ignored when kernel allocations are profiled.
.TP
INTERVAL INTERVAL
Print a summary of oustanding allocations and their call stacks every INTERVAL seconds. Print a summary of oustanding allocations and their call stacks every INTERVAL seconds.
The default interval is 5 seconds. The default interval is 5 seconds.
......
...@@ -135,6 +135,8 @@ parser.add_argument("-z", "--min-size", type=int, ...@@ -135,6 +135,8 @@ parser.add_argument("-z", "--min-size", type=int,
help="capture only allocations larger than this size") help="capture only allocations larger than this size")
parser.add_argument("-Z", "--max-size", type=int, parser.add_argument("-Z", "--max-size", type=int,
help="capture only allocations smaller than this size") help="capture only allocations smaller than this size")
parser.add_argument("-O", "--obj", type=str, default="c",
help="attach to malloc & free in the specified object")
args = parser.parse_args() args = parser.parse_args()
...@@ -149,6 +151,7 @@ num_prints = args.count ...@@ -149,6 +151,7 @@ num_prints = args.count
top_stacks = args.top top_stacks = args.top
min_size = args.min_size min_size = args.min_size
max_size = args.max_size max_size = args.max_size
obj = args.obj
if min_size is not None and max_size is not None and min_size > max_size: if min_size is not None and max_size is not None and min_size > max_size:
print("min_size (-z) can't be greater than max_size (-Z)") print("min_size (-z) can't be greater than max_size (-Z)")
...@@ -251,11 +254,11 @@ bpf_program = BPF(text=bpf_source) ...@@ -251,11 +254,11 @@ bpf_program = BPF(text=bpf_source)
if not kernel_trace: if not kernel_trace:
print("Attaching to malloc and free in pid %d, Ctrl+C to quit." % pid) print("Attaching to malloc and free in pid %d, Ctrl+C to quit." % pid)
bpf_program.attach_uprobe(name="c", sym="malloc", bpf_program.attach_uprobe(name=obj, sym="malloc",
fn_name="alloc_enter", pid=pid) fn_name="alloc_enter", pid=pid)
bpf_program.attach_uretprobe(name="c", sym="malloc", bpf_program.attach_uretprobe(name=obj, sym="malloc",
fn_name="alloc_exit", pid=pid) fn_name="alloc_exit", pid=pid)
bpf_program.attach_uprobe(name="c", sym="free", bpf_program.attach_uprobe(name=obj, sym="free",
fn_name="free_enter", pid=pid) fn_name="free_enter", pid=pid)
else: else:
print("Attaching to kmalloc and kfree, Ctrl+C to quit.") print("Attaching to kmalloc and kfree, Ctrl+C to quit.")
......
...@@ -150,14 +150,16 @@ of the sampling rate applied. ...@@ -150,14 +150,16 @@ of the sampling rate applied.
USAGE message: USAGE message:
# ./memleak -h # ./memleak -h
usage: memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND] usage: memleak.py [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND]
[-s SAMPLE_RATE] [-d STACK_DEPTH] [-T TOP] [-s SAMPLE_RATE] [-T TOP] [-z MIN_SIZE] [-Z MAX_SIZE]
[-O OBJ]
[interval] [count] [interval] [count]
Trace outstanding memory allocations that weren't freed. Trace outstanding memory allocations that weren't freed.
Supports both user-mode allocations made with malloc/free and kernel-mode Supports both user-mode allocations made with malloc/free and kernel-mode
allocations made with kmalloc/kfree. allocations made with kmalloc/kfree.
positional arguments:
interval interval in seconds to print outstanding allocations interval interval in seconds to print outstanding allocations
count number of times to print the report before exiting count number of times to print the report before exiting
...@@ -175,13 +177,12 @@ optional arguments: ...@@ -175,13 +177,12 @@ optional arguments:
execute and trace the specified command execute and trace the specified command
-s SAMPLE_RATE, --sample-rate SAMPLE_RATE -s SAMPLE_RATE, --sample-rate SAMPLE_RATE
sample every N-th allocation to decrease the overhead sample every N-th allocation to decrease the overhead
-d STACK_DEPTH, --stack_depth STACK_DEPTH
maximum stack depth to capture
-T TOP, --top TOP display only this many top allocating stacks (by size) -T TOP, --top TOP display only this many top allocating stacks (by size)
-z MIN_SIZE, --min-size MIN_SIZE -z MIN_SIZE, --min-size MIN_SIZE
capture only allocations larger than this size capture only allocations larger than this size
-Z MAX_SIZE, --max-size MAX_SIZE -Z MAX_SIZE, --max-size MAX_SIZE
capture only allocations smaller than this size capture only allocations smaller than this size
-O OBJ, --obj OBJ attach to malloc & free in the specified object
EXAMPLES: EXAMPLES:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment