Sasha Goldshtein | dda4769 | 2016-02-08 03:10:13 -0800 | [diff] [blame^] | 1 | Demonstrations of memleak. |
| 2 | |
| 3 | |
| 4 | memleak traces and matches memory allocation and deallocation requests, and |
| 5 | collects call stacks for each allocation. memleak can then print a summary |
| 6 | of which call stacks performed allocations that weren't subsequently freed. |
| 7 | For example: |
| 8 | |
| 9 | # ./memleak.py -p $(pidof allocs) |
| 10 | Attaching to malloc and free in pid 5193, Ctrl+C to quit. |
| 11 | *** Outstanding allocations: |
| 12 | 80 bytes in 5 allocations from stack |
| 13 | main+0x6d [/home/vagrant/allocs] (400862) |
| 14 | __libc_start_main+0xf0 [/usr/lib64/libc-2.21.so] (7fd460ac2790) |
| 15 | |
| 16 | *** Outstanding allocations: |
| 17 | 160 bytes in 10 allocations from stack |
| 18 | main+0x6d [/home/vagrant/allocs] (400862) |
| 19 | __libc_start_main+0xf0 [/usr/lib64/libc-2.21.so] (7fd460ac2790) |
| 20 | |
| 21 | |
| 22 | When using the -p switch, memleak traces the allocations of a particular |
| 23 | process. Without this switch, kernel allocations (kmalloc) are traced instead. |
| 24 | For example: |
| 25 | |
| 26 | # ./memleak.py |
| 27 | Attaching to kmalloc and kfree, Ctrl+C to quit. |
| 28 | ... |
| 29 | 248 bytes in 4 allocations from stack |
| 30 | bpf_prog_load [kernel] (ffffffff8118c471) |
| 31 | sys_bpf [kernel] (ffffffff8118c8b5) |
| 32 | |
| 33 | 328 bytes in 1 allocations from stack |
| 34 | perf_mmap [kernel] (ffffffff811990fd) |
| 35 | mmap_region [kernel] (ffffffff811df5d4) |
| 36 | do_mmap [kernel] (ffffffff811dfb83) |
| 37 | vm_mmap_pgoff [kernel] (ffffffff811c494f) |
| 38 | sys_mmap_pgoff [kernel] (ffffffff811ddf02) |
| 39 | sys_mmap [kernel] (ffffffff8101b0ab) |
| 40 | |
| 41 | 464 bytes in 1 allocations from stack |
| 42 | traceprobe_command [kernel] (ffffffff81187cf2) |
| 43 | traceprobe_probes_write [kernel] (ffffffff81187d86) |
| 44 | probes_write [kernel] (ffffffff81181580) |
| 45 | __vfs_write [kernel] (ffffffff812237b7) |
| 46 | vfs_write [kernel] (ffffffff81223ec6) |
| 47 | sys_write [kernel] (ffffffff81224b85) |
| 48 | entry_SYSCALL_64_fastpath [kernel] (ffffffff8178182e) |
| 49 | |
| 50 | 8192 bytes in 1 allocations from stack |
| 51 | alloc_and_copy_ftrace_hash.constprop.59 [kernel] (ffffffff8115d17e) |
| 52 | ftrace_set_hash [kernel] (ffffffff8115e767) |
| 53 | ftrace_set_filter_ip [kernel] (ffffffff8115e9a8) |
| 54 | arm_kprobe [kernel] (ffffffff81148600) |
| 55 | enable_kprobe [kernel] (ffffffff811486f6) |
| 56 | kprobe_register [kernel] (ffffffff81182399) |
| 57 | perf_trace_init [kernel] (ffffffff8117c4e0) |
| 58 | perf_tp_event_init [kernel] (ffffffff81192479) |
| 59 | |
| 60 | |
| 61 | memleak stores each allocated block along with its size, timestamp, and the |
| 62 | stack that allocated it. When the block is deleted, this information is freed |
| 63 | to reduce the memory overhead. |
| 64 | |
| 65 | To avoid false positives, allocations younger than a certain age (500ms by |
| 66 | default) are not printed. To change this threshold, use the -o switch. |
| 67 | |
| 68 | By default, memleak prints its output every 5 seconds. To change this |
| 69 | interval, use the -i switch. |
| 70 | |
| 71 | |
| 72 | USAGE message: |
| 73 | |
| 74 | # ./memleak.py -h |
| 75 | usage: memleak.py [-h] [-p PID] [-t] [-i INTERVAL] [-a] [-o OLDER] |
| 76 | [-c COMMAND] |
| 77 | |
| 78 | Trace outstanding memory allocations that weren't freed. |
| 79 | Supports both user-mode allocations made with malloc/free and kernel-mode |
| 80 | allocations made with kmalloc/kfree. |
| 81 | |
| 82 | optional arguments: |
| 83 | -h, --help show this help message and exit |
| 84 | -p PID, --pid PID the PID to trace; if not specified, trace kernel |
| 85 | allocs |
| 86 | -t, --trace print trace messages for each alloc/free call |
| 87 | -i INTERVAL, --interval INTERVAL |
| 88 | interval in seconds to print outstanding allocations |
| 89 | -a, --show-allocs show allocation addresses and sizes as well as call |
| 90 | stacks |
| 91 | -o OLDER, --older OLDER |
| 92 | prune allocations younger than this age in |
| 93 | milliseconds |
| 94 | -c COMMAND, --command COMMAND |
| 95 | execute and trace the specified command |
| 96 | |
| 97 | EXAMPLES: |
| 98 | |
| 99 | ./memleak.py -p $(pidof allocs) |
| 100 | Trace allocations and display a summary of "leaked" (outstanding) |
| 101 | allocations every 5 seconds |
| 102 | ./memleak.py -p $(pidof allocs) -t |
| 103 | Trace allocations and display each individual call to malloc/free |
| 104 | ./memleak.py -p $(pidof allocs) -a -i 10 Trace allocations and display allocated addresses, sizes, and stacks every 10 seconds for outstanding allocations ./memleak.py -c "./allocs" Run the specified command and trace its allocations ./memleak.py Trace allocations in kernel mode and display a summary of outstanding allocations every 5 seconds ./memleak.py -o 60000 Trace allocations in kernel mode and display a summary of outstanding allocations that are at least one minute (60 seconds) old |
| 105 | |