blob: cd913723762f7737421f823a6d1c8c6abba7041c [file] [log] [blame]
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -08001#!/usr/bin/env python
Sasha Goldshtein50459642016-02-10 08:35:20 -08002#
Sasha Goldshtein0e856f42016-03-21 07:26:52 -07003# memleak Trace and display outstanding allocations to detect
4# memory leaks in user-mode processes and the kernel.
Sasha Goldshtein50459642016-02-10 08:35:20 -08005#
Sasha Goldshtein29e37d92016-02-14 06:56:07 -08006# USAGE: memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND]
Sasha Goldshtein0e856f42016-03-21 07:26:52 -07007# [-s SAMPLE_RATE] [-d STACK_DEPTH] [-T TOP] [-z MIN_SIZE]
8# [-Z MAX_SIZE]
9# [interval] [count]
Sasha Goldshtein50459642016-02-10 08:35:20 -080010#
Sasha Goldshtein43fa0412016-02-10 22:17:26 -080011# Licensed under the Apache License, Version 2.0 (the "License")
Sasha Goldshtein50459642016-02-10 08:35:20 -080012# Copyright (C) 2016 Sasha Goldshtein.
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080013
14from bcc import BPF
15from time import sleep
Sasha Goldshteinc8148c82016-02-09 11:15:41 -080016from datetime import datetime
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080017import argparse
18import subprocess
Sasha Goldshteincfce3112016-02-07 11:09:36 -080019import ctypes
20import os
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080021
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -080022class Time(object):
Sasha Goldshtein33522d72016-02-08 03:39:44 -080023 # BPF timestamps come from the monotonic clock. To be able to filter
24 # and compare them from Python, we need to invoke clock_gettime.
25 # Adapted from http://stackoverflow.com/a/1205762
26 CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -080027
Sasha Goldshtein33522d72016-02-08 03:39:44 -080028 class timespec(ctypes.Structure):
29 _fields_ = [
30 ('tv_sec', ctypes.c_long),
31 ('tv_nsec', ctypes.c_long)
32 ]
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -080033
Sasha Goldshtein33522d72016-02-08 03:39:44 -080034 librt = ctypes.CDLL('librt.so.1', use_errno=True)
35 clock_gettime = librt.clock_gettime
36 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -080037
Sasha Goldshtein33522d72016-02-08 03:39:44 -080038 @staticmethod
39 def monotonic_time():
40 t = Time.timespec()
41 if Time.clock_gettime(
42 Time.CLOCK_MONOTONIC_RAW, ctypes.pointer(t)) != 0:
43 errno_ = ctypes.get_errno()
44 raise OSError(errno_, os.strerror(errno_))
45 return t.tv_sec * 1e9 + t.tv_nsec
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -080046
Sasha Goldshtein29228612016-02-07 12:20:19 -080047class StackDecoder(object):
Sasha Goldshtein0e856f42016-03-21 07:26:52 -070048 def __init__(self, pid):
Sasha Goldshtein33522d72016-02-08 03:39:44 -080049 self.pid = pid
Sasha Goldshtein0e856f42016-03-21 07:26:52 -070050 if pid != -1:
51 self.proc_sym = BPF.ProcessSymbols(pid)
Sasha Goldshtein29228612016-02-07 12:20:19 -080052
Sasha Goldshtein0e856f42016-03-21 07:26:52 -070053 def refresh(self):
54 if self.pid != -1:
55 self.proc_sym.refresh_code_ranges()
Sasha Goldshtein33522d72016-02-08 03:39:44 -080056
57 def decode_stack(self, info, is_kernel_trace):
58 stack = ""
59 if info.num_frames <= 0:
60 return "???"
61 for i in range(0, info.num_frames):
62 addr = info.callstack[i]
63 if is_kernel_trace:
64 stack += " %s [kernel] (%x) ;" % \
Sasha Goldshtein0e856f42016-03-21 07:26:52 -070065 (BPF.ksym(addr), addr)
Sasha Goldshtein33522d72016-02-08 03:39:44 -080066 else:
Sasha Goldshtein33522d72016-02-08 03:39:44 -080067 stack += " %s (%x) ;" % \
Sasha Goldshtein0e856f42016-03-21 07:26:52 -070068 (self.proc_sym.decode_addr(addr), addr)
Sasha Goldshtein33522d72016-02-08 03:39:44 -080069 return stack
Sasha Goldshtein29228612016-02-07 12:20:19 -080070
Sasha Goldshtein751fce52016-02-08 02:57:02 -080071def run_command_get_output(command):
Sasha Goldshtein33522d72016-02-08 03:39:44 -080072 p = subprocess.Popen(command.split(),
73 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
74 return iter(p.stdout.readline, b'')
Sasha Goldshtein29228612016-02-07 12:20:19 -080075
Sasha Goldshtein751fce52016-02-08 02:57:02 -080076def run_command_get_pid(command):
Sasha Goldshtein33522d72016-02-08 03:39:44 -080077 p = subprocess.Popen(command.split())
78 return p.pid
Sasha Goldshtein751fce52016-02-08 02:57:02 -080079
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -080080examples = """
81EXAMPLES:
82
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080083./memleak -p $(pidof allocs)
Sasha Goldshtein33522d72016-02-08 03:39:44 -080084 Trace allocations and display a summary of "leaked" (outstanding)
85 allocations every 5 seconds
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080086./memleak -p $(pidof allocs) -t
Sasha Goldshtein33522d72016-02-08 03:39:44 -080087 Trace allocations and display each individual call to malloc/free
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080088./memleak -ap $(pidof allocs) 10
Sasha Goldshtein33522d72016-02-08 03:39:44 -080089 Trace allocations and display allocated addresses, sizes, and stacks
90 every 10 seconds for outstanding allocations
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080091./memleak -c "./allocs"
Sasha Goldshtein33522d72016-02-08 03:39:44 -080092 Run the specified command and trace its allocations
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080093./memleak
Sasha Goldshtein33522d72016-02-08 03:39:44 -080094 Trace allocations in kernel mode and display a summary of outstanding
95 allocations every 5 seconds
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080096./memleak -o 60000
Sasha Goldshtein33522d72016-02-08 03:39:44 -080097 Trace allocations in kernel mode and display a summary of outstanding
98 allocations that are at least one minute (60 seconds) old
Sasha Goldshtein29e37d92016-02-14 06:56:07 -080099./memleak -s 5
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -0800100 Trace roughly every 5th allocation, to reduce overhead
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800101"""
102
103description = """
104Trace outstanding memory allocations that weren't freed.
105Supports both user-mode allocations made with malloc/free and kernel-mode
106allocations made with kmalloc/kfree.
107"""
108
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -0800109parser = argparse.ArgumentParser(description=description,
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800110 formatter_class=argparse.RawDescriptionHelpFormatter,
111 epilog=examples)
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800112parser.add_argument("-p", "--pid", type=int, default=-1,
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800113 help="the PID to trace; if not specified, trace kernel allocs")
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -0800114parser.add_argument("-t", "--trace", action="store_true",
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800115 help="print trace messages for each alloc/free call")
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800116parser.add_argument("interval", nargs="?", default=5, type=int,
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800117 help="interval in seconds to print outstanding allocations")
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800118parser.add_argument("count", nargs="?", type=int,
119 help="number of times to print the report before exiting")
Sasha Goldshteina7cc6c22016-02-07 12:03:54 -0800120parser.add_argument("-a", "--show-allocs", default=False, action="store_true",
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800121 help="show allocation addresses and sizes as well as call stacks")
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800122parser.add_argument("-o", "--older", default=500, type=int,
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800123 help="prune allocations younger than this age in milliseconds")
Sasha Goldshtein29228612016-02-07 12:20:19 -0800124parser.add_argument("-c", "--command",
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800125 help="execute and trace the specified command")
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800126parser.add_argument("-s", "--sample-rate", default=1, type=int,
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -0800127 help="sample every N-th allocation to decrease the overhead")
Sasha Goldshteindcee30d2016-02-09 06:24:33 -0800128parser.add_argument("-d", "--stack-depth", default=10, type=int,
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800129 help="maximum stack depth to capture")
Sasha Goldshteinc8148c82016-02-09 11:15:41 -0800130parser.add_argument("-T", "--top", type=int, default=10,
131 help="display only this many top allocating stacks (by size)")
Sasha Goldshtein50459642016-02-10 08:35:20 -0800132parser.add_argument("-z", "--min-size", type=int,
133 help="capture only allocations larger than this size")
134parser.add_argument("-Z", "--max-size", type=int,
135 help="capture only allocations smaller than this size")
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800136
137args = parser.parse_args()
138
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800139pid = args.pid
Sasha Goldshtein29228612016-02-07 12:20:19 -0800140command = args.command
141kernel_trace = (pid == -1 and command is None)
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800142trace_all = args.trace
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800143interval = args.interval
144min_age_ns = 1e6 * args.older
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -0800145sample_every_n = args.sample_rate
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800146num_prints = args.count
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800147max_stack_size = args.stack_depth + 2
Sasha Goldshteinc8148c82016-02-09 11:15:41 -0800148top_stacks = args.top
Sasha Goldshtein50459642016-02-10 08:35:20 -0800149min_size = args.min_size
150max_size = args.max_size
151
152if min_size is not None and max_size is not None and min_size > max_size:
153 print("min_size (-z) can't be greater than max_size (-Z)")
154 exit(1)
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800155
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800156if command is not None:
157 print("Executing '%s' and tracing the resulting process." % command)
158 pid = run_command_get_pid(command)
Sasha Goldshtein29228612016-02-07 12:20:19 -0800159
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800160bpf_source = """
161#include <uapi/linux/ptrace.h>
162
163struct alloc_info_t {
164 u64 size;
165 u64 timestamp_ns;
166 int num_frames;
167 u64 callstack[MAX_STACK_SIZE];
168};
169
170BPF_HASH(sizes, u64);
171BPF_HASH(allocs, u64, struct alloc_info_t);
172
173// Adapted from https://github.com/iovisor/bcc/tools/offcputime.py
174static u64 get_frame(u64 *bp) {
175 if (*bp) {
176 // The following stack walker is x86_64 specific
177 u64 ret = 0;
178 if (bpf_probe_read(&ret, sizeof(ret), (void *)(*bp+8)))
179 return 0;
180 if (bpf_probe_read(bp, sizeof(*bp), (void *)*bp))
181 *bp = 0;
182 return ret;
183 }
184 return 0;
185}
186static int grab_stack(struct pt_regs *ctx, struct alloc_info_t *info)
187{
188 int depth = 0;
189 u64 bp = ctx->bp;
190 GRAB_ONE_FRAME
191 return depth;
192}
193
194int alloc_enter(struct pt_regs *ctx, size_t size)
195{
196 SIZE_FILTER
197 if (SAMPLE_EVERY_N > 1) {
198 u64 ts = bpf_ktime_get_ns();
199 if (ts % SAMPLE_EVERY_N != 0)
200 return 0;
201 }
202
203 u64 pid = bpf_get_current_pid_tgid();
204 u64 size64 = size;
205 sizes.update(&pid, &size64);
206
207 if (SHOULD_PRINT)
208 bpf_trace_printk("alloc entered, size = %u\\n", size);
209 return 0;
210}
211
212int alloc_exit(struct pt_regs *ctx)
213{
214 u64 address = ctx->ax;
215 u64 pid = bpf_get_current_pid_tgid();
216 u64* size64 = sizes.lookup(&pid);
217 struct alloc_info_t info = {0};
218
219 if (size64 == 0)
220 return 0; // missed alloc entry
221
222 info.size = *size64;
223 sizes.delete(&pid);
224
225 info.timestamp_ns = bpf_ktime_get_ns();
226 info.num_frames = grab_stack(ctx, &info) - 2;
227 allocs.update(&address, &info);
Sasha Goldshtein0e856f42016-03-21 07:26:52 -0700228
Sasha Goldshtein43fa0412016-02-10 22:17:26 -0800229 if (SHOULD_PRINT) {
230 bpf_trace_printk("alloc exited, size = %lu, result = %lx, frames = %d\\n",
231 info.size, address, info.num_frames);
232 }
233 return 0;
234}
235
236int free_enter(struct pt_regs *ctx, void *address)
237{
238 u64 addr = (u64)address;
239 struct alloc_info_t *info = allocs.lookup(&addr);
240 if (info == 0)
241 return 0;
242
243 allocs.delete(&addr);
244
245 if (SHOULD_PRINT) {
246 bpf_trace_printk("free entered, address = %lx, size = %lu\\n",
247 address, info->size);
248 }
249 return 0;
250}
Sasha Goldshtein0e856f42016-03-21 07:26:52 -0700251"""
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800252bpf_source = bpf_source.replace("SHOULD_PRINT", "1" if trace_all else "0")
Sasha Goldshtein521ab4f2016-02-08 05:48:31 -0800253bpf_source = bpf_source.replace("SAMPLE_EVERY_N", str(sample_every_n))
Sasha Goldshteind2241f42016-02-09 06:23:10 -0800254bpf_source = bpf_source.replace("GRAB_ONE_FRAME", max_stack_size *
255 "\tif (!(info->callstack[depth++] = get_frame(&bp))) return depth;\n")
256bpf_source = bpf_source.replace("MAX_STACK_SIZE", str(max_stack_size))
Sasha Goldshtein50459642016-02-10 08:35:20 -0800257
258size_filter = ""
259if min_size is not None and max_size is not None:
260 size_filter = "if (size < %d || size > %d) return 0;" % \
261 (min_size, max_size)
262elif min_size is not None:
263 size_filter = "if (size < %d) return 0;" % min_size
264elif max_size is not None:
265 size_filter = "if (size > %d) return 0;" % max_size
266bpf_source = bpf_source.replace("SIZE_FILTER", size_filter)
267
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800268bpf_program = BPF(text=bpf_source)
269
270if not kernel_trace:
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800271 print("Attaching to malloc and free in pid %d, Ctrl+C to quit." % pid)
272 bpf_program.attach_uprobe(name="c", sym="malloc",
273 fn_name="alloc_enter", pid=pid)
274 bpf_program.attach_uretprobe(name="c", sym="malloc",
275 fn_name="alloc_exit", pid=pid)
276 bpf_program.attach_uprobe(name="c", sym="free",
277 fn_name="free_enter", pid=pid)
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800278else:
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800279 print("Attaching to kmalloc and kfree, Ctrl+C to quit.")
280 bpf_program.attach_kprobe(event="__kmalloc", fn_name="alloc_enter")
281 bpf_program.attach_kretprobe(event="__kmalloc", fn_name="alloc_exit")
282 bpf_program.attach_kprobe(event="kfree", fn_name="free_enter")
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800283
Sasha Goldshtein0e856f42016-03-21 07:26:52 -0700284decoder = StackDecoder(pid)
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800285
286def print_outstanding():
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800287 stacks = {}
Sasha Goldshteinc8148c82016-02-09 11:15:41 -0800288 print("[%s] Top %d stacks with outstanding allocations:" %
289 (datetime.now().strftime("%H:%M:%S"), top_stacks))
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800290 allocs = bpf_program.get_table("allocs")
291 for address, info in sorted(allocs.items(), key=lambda a: a[1].size):
292 if Time.monotonic_time() - min_age_ns < info.timestamp_ns:
293 continue
294 stack = decoder.decode_stack(info, kernel_trace)
295 if stack in stacks:
296 stacks[stack] = (stacks[stack][0] + 1,
297 stacks[stack][1] + info.size)
298 else:
299 stacks[stack] = (1, info.size)
300 if args.show_allocs:
301 print("\taddr = %x size = %s" %
302 (address.value, info.size))
Sasha Goldshteinc8148c82016-02-09 11:15:41 -0800303 to_show = sorted(stacks.items(), key=lambda s: s[1][1])[-top_stacks:]
304 for stack, (count, size) in to_show:
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800305 print("\t%d bytes in %d allocations from stack\n\t\t%s" %
306 (size, count, stack.replace(";", "\n\t\t")))
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800307
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800308count_so_far = 0
Sasha Goldshtein4f1ea672016-02-07 01:57:42 -0800309while True:
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800310 if trace_all:
Brenden Blancoc94ab7a2016-03-11 15:34:29 -0800311 print(bpf_program.trace_fields())
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800312 else:
313 try:
314 sleep(interval)
315 except KeyboardInterrupt:
316 exit()
Sasha Goldshtein0e856f42016-03-21 07:26:52 -0700317 decoder.refresh()
Sasha Goldshtein33522d72016-02-08 03:39:44 -0800318 print_outstanding()
Sasha Goldshtein40e55ba2016-02-09 05:53:48 -0800319 count_so_far += 1
320 if num_prints is not None and count_so_far >= num_prints:
321 exit()