Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 2 | # |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 3 | # memleak Trace and display outstanding allocations to detect |
| 4 | # memory leaks in user-mode processes and the kernel. |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 5 | # |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 6 | # USAGE: memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND] |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 7 | # [-s SAMPLE_RATE] [-d STACK_DEPTH] [-T TOP] [-z MIN_SIZE] |
| 8 | # [-Z MAX_SIZE] |
| 9 | # [interval] [count] |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 10 | # |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 11 | # Licensed under the Apache License, Version 2.0 (the "License") |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 12 | # Copyright (C) 2016 Sasha Goldshtein. |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 13 | |
Sasha Goldshtein | 49df994 | 2017-02-08 23:22:06 -0500 | [diff] [blame] | 14 | from bcc import BPF |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 15 | from time import sleep |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 16 | from datetime import datetime |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 17 | import argparse |
| 18 | import subprocess |
Sasha Goldshtein | cfce311 | 2016-02-07 11:09:36 -0800 | [diff] [blame] | 19 | import os |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 20 | |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 21 | class Allocation(object): |
| 22 | def __init__(self, stack, size): |
| 23 | self.stack = stack |
| 24 | self.count = 1 |
| 25 | self.size = size |
| 26 | |
| 27 | def update(self, size): |
| 28 | self.count += 1 |
| 29 | self.size += size |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 30 | |
Sasha Goldshtein | 751fce5 | 2016-02-08 02:57:02 -0800 | [diff] [blame] | 31 | def run_command_get_output(command): |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 32 | p = subprocess.Popen(command.split(), |
| 33 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 34 | return iter(p.stdout.readline, b'') |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 35 | |
Sasha Goldshtein | 751fce5 | 2016-02-08 02:57:02 -0800 | [diff] [blame] | 36 | def run_command_get_pid(command): |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 37 | p = subprocess.Popen(command.split()) |
| 38 | return p.pid |
Sasha Goldshtein | 751fce5 | 2016-02-08 02:57:02 -0800 | [diff] [blame] | 39 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 40 | examples = """ |
| 41 | EXAMPLES: |
| 42 | |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 43 | ./memleak -p $(pidof allocs) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 44 | Trace allocations and display a summary of "leaked" (outstanding) |
| 45 | allocations every 5 seconds |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 46 | ./memleak -p $(pidof allocs) -t |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 47 | Trace allocations and display each individual call to malloc/free |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 48 | ./memleak -ap $(pidof allocs) 10 |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 49 | Trace allocations and display allocated addresses, sizes, and stacks |
| 50 | every 10 seconds for outstanding allocations |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 51 | ./memleak -c "./allocs" |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 52 | Run the specified command and trace its allocations |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 53 | ./memleak |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 54 | Trace allocations in kernel mode and display a summary of outstanding |
| 55 | allocations every 5 seconds |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 56 | ./memleak -o 60000 |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 57 | Trace allocations in kernel mode and display a summary of outstanding |
| 58 | allocations that are at least one minute (60 seconds) old |
Sasha Goldshtein | 29e37d9 | 2016-02-14 06:56:07 -0800 | [diff] [blame] | 59 | ./memleak -s 5 |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 60 | Trace roughly every 5th allocation, to reduce overhead |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 61 | """ |
| 62 | |
| 63 | description = """ |
| 64 | Trace outstanding memory allocations that weren't freed. |
| 65 | Supports both user-mode allocations made with malloc/free and kernel-mode |
| 66 | allocations made with kmalloc/kfree. |
| 67 | """ |
| 68 | |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 69 | parser = argparse.ArgumentParser(description=description, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 70 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 71 | epilog=examples) |
Sasha Goldshtein | d2241f4 | 2016-02-09 06:23:10 -0800 | [diff] [blame] | 72 | parser.add_argument("-p", "--pid", type=int, default=-1, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 73 | help="the PID to trace; if not specified, trace kernel allocs") |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 74 | parser.add_argument("-t", "--trace", action="store_true", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 75 | help="print trace messages for each alloc/free call") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 76 | parser.add_argument("interval", nargs="?", default=5, type=int, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 77 | help="interval in seconds to print outstanding allocations") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 78 | parser.add_argument("count", nargs="?", type=int, |
| 79 | help="number of times to print the report before exiting") |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 80 | parser.add_argument("-a", "--show-allocs", default=False, action="store_true", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 81 | help="show allocation addresses and sizes as well as call stacks") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 82 | parser.add_argument("-o", "--older", default=500, type=int, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 83 | help="prune allocations younger than this age in milliseconds") |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 84 | parser.add_argument("-c", "--command", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 85 | help="execute and trace the specified command") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 86 | parser.add_argument("-s", "--sample-rate", default=1, type=int, |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 87 | help="sample every N-th allocation to decrease the overhead") |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 88 | parser.add_argument("-T", "--top", type=int, default=10, |
| 89 | help="display only this many top allocating stacks (by size)") |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 90 | parser.add_argument("-z", "--min-size", type=int, |
| 91 | help="capture only allocations larger than this size") |
| 92 | parser.add_argument("-Z", "--max-size", type=int, |
| 93 | help="capture only allocations smaller than this size") |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 94 | parser.add_argument("-O", "--obj", type=str, default="c", |
| 95 | help="attach to malloc & free in the specified object") |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 96 | |
| 97 | args = parser.parse_args() |
| 98 | |
Sasha Goldshtein | d2241f4 | 2016-02-09 06:23:10 -0800 | [diff] [blame] | 99 | pid = args.pid |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 100 | command = args.command |
| 101 | kernel_trace = (pid == -1 and command is None) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 102 | trace_all = args.trace |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 103 | interval = args.interval |
| 104 | min_age_ns = 1e6 * args.older |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 105 | sample_every_n = args.sample_rate |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 106 | num_prints = args.count |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 107 | top_stacks = args.top |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 108 | min_size = args.min_size |
| 109 | max_size = args.max_size |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 110 | obj = args.obj |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 111 | |
| 112 | if min_size is not None and max_size is not None and min_size > max_size: |
| 113 | print("min_size (-z) can't be greater than max_size (-Z)") |
| 114 | exit(1) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 115 | |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 116 | if command is not None: |
| 117 | print("Executing '%s' and tracing the resulting process." % command) |
| 118 | pid = run_command_get_pid(command) |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 119 | |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 120 | bpf_source = """ |
| 121 | #include <uapi/linux/ptrace.h> |
| 122 | |
| 123 | struct alloc_info_t { |
| 124 | u64 size; |
| 125 | u64 timestamp_ns; |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 126 | int stack_id; |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | BPF_HASH(sizes, u64); |
| 130 | BPF_HASH(allocs, u64, struct alloc_info_t); |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 131 | BPF_STACK_TRACE(stack_traces, 1024) |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 132 | |
| 133 | int alloc_enter(struct pt_regs *ctx, size_t size) |
| 134 | { |
| 135 | SIZE_FILTER |
| 136 | if (SAMPLE_EVERY_N > 1) { |
| 137 | u64 ts = bpf_ktime_get_ns(); |
| 138 | if (ts % SAMPLE_EVERY_N != 0) |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | u64 pid = bpf_get_current_pid_tgid(); |
| 143 | u64 size64 = size; |
| 144 | sizes.update(&pid, &size64); |
| 145 | |
| 146 | if (SHOULD_PRINT) |
| 147 | bpf_trace_printk("alloc entered, size = %u\\n", size); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int alloc_exit(struct pt_regs *ctx) |
| 152 | { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 153 | u64 address = PT_REGS_RC(ctx); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 154 | u64 pid = bpf_get_current_pid_tgid(); |
| 155 | u64* size64 = sizes.lookup(&pid); |
| 156 | struct alloc_info_t info = {0}; |
| 157 | |
| 158 | if (size64 == 0) |
| 159 | return 0; // missed alloc entry |
| 160 | |
| 161 | info.size = *size64; |
| 162 | sizes.delete(&pid); |
| 163 | |
| 164 | info.timestamp_ns = bpf_ktime_get_ns(); |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 165 | info.stack_id = stack_traces.get_stackid(ctx, STACK_FLAGS); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 166 | allocs.update(&address, &info); |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 167 | |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 168 | if (SHOULD_PRINT) { |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 169 | bpf_trace_printk("alloc exited, size = %lu, result = %lx\\n", |
| 170 | info.size, address); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int free_enter(struct pt_regs *ctx, void *address) |
| 176 | { |
| 177 | u64 addr = (u64)address; |
| 178 | struct alloc_info_t *info = allocs.lookup(&addr); |
| 179 | if (info == 0) |
| 180 | return 0; |
| 181 | |
| 182 | allocs.delete(&addr); |
| 183 | |
| 184 | if (SHOULD_PRINT) { |
| 185 | bpf_trace_printk("free entered, address = %lx, size = %lu\\n", |
| 186 | address, info->size); |
| 187 | } |
| 188 | return 0; |
| 189 | } |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 190 | """ |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 191 | bpf_source = bpf_source.replace("SHOULD_PRINT", "1" if trace_all else "0") |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 192 | bpf_source = bpf_source.replace("SAMPLE_EVERY_N", str(sample_every_n)) |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 193 | |
| 194 | size_filter = "" |
| 195 | if min_size is not None and max_size is not None: |
| 196 | size_filter = "if (size < %d || size > %d) return 0;" % \ |
| 197 | (min_size, max_size) |
| 198 | elif min_size is not None: |
| 199 | size_filter = "if (size < %d) return 0;" % min_size |
| 200 | elif max_size is not None: |
| 201 | size_filter = "if (size > %d) return 0;" % max_size |
| 202 | bpf_source = bpf_source.replace("SIZE_FILTER", size_filter) |
| 203 | |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 204 | stack_flags = "BPF_F_REUSE_STACKID" |
| 205 | if not kernel_trace: |
| 206 | stack_flags += "|BPF_F_USER_STACK" |
| 207 | bpf_source = bpf_source.replace("STACK_FLAGS", stack_flags) |
| 208 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 209 | bpf_program = BPF(text=bpf_source) |
| 210 | |
| 211 | if not kernel_trace: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 212 | print("Attaching to malloc and free in pid %d, Ctrl+C to quit." % pid) |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 213 | bpf_program.attach_uprobe(name=obj, sym="malloc", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 214 | fn_name="alloc_enter", pid=pid) |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 215 | bpf_program.attach_uretprobe(name=obj, sym="malloc", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 216 | fn_name="alloc_exit", pid=pid) |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 217 | bpf_program.attach_uprobe(name=obj, sym="free", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 218 | fn_name="free_enter", pid=pid) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 219 | else: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 220 | print("Attaching to kmalloc and kfree, Ctrl+C to quit.") |
| 221 | bpf_program.attach_kprobe(event="__kmalloc", fn_name="alloc_enter") |
| 222 | bpf_program.attach_kretprobe(event="__kmalloc", fn_name="alloc_exit") |
| 223 | bpf_program.attach_kprobe(event="kfree", fn_name="free_enter") |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 224 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 225 | def print_outstanding(): |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 226 | print("[%s] Top %d stacks with outstanding allocations:" % |
| 227 | (datetime.now().strftime("%H:%M:%S"), top_stacks)) |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 228 | alloc_info = {} |
| 229 | allocs = bpf_program["allocs"] |
| 230 | stack_traces = bpf_program["stack_traces"] |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 231 | for address, info in sorted(allocs.items(), key=lambda a: a[1].size): |
Sasha Goldshtein | 60c4192 | 2017-02-09 04:19:53 -0500 | [diff] [blame] | 232 | if BPF.monotonic_time() - min_age_ns < info.timestamp_ns: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 233 | continue |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 234 | if info.stack_id < 0: |
| 235 | continue |
| 236 | if info.stack_id in alloc_info: |
| 237 | alloc_info[info.stack_id].update(info.size) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 238 | else: |
Sasha Goldshtein | 49df994 | 2017-02-08 23:22:06 -0500 | [diff] [blame] | 239 | stack = list(stack_traces.walk(info.stack_id)) |
| 240 | combined = [] |
| 241 | for addr in stack: |
| 242 | combined.append(bpf_program.sym(addr, pid, |
| 243 | show_module=True, show_address=True)) |
| 244 | alloc_info[info.stack_id] = Allocation(combined, |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 245 | info.size) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 246 | if args.show_allocs: |
| 247 | print("\taddr = %x size = %s" % |
| 248 | (address.value, info.size)) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 249 | to_show = sorted(alloc_info.values(), |
| 250 | key=lambda a: a.size)[-top_stacks:] |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 251 | for alloc in to_show: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 252 | print("\t%d bytes in %d allocations from stack\n\t\t%s" % |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 253 | (alloc.size, alloc.count, "\n\t\t".join(alloc.stack))) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 254 | |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 255 | count_so_far = 0 |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 256 | while True: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 257 | if trace_all: |
Brenden Blanco | c94ab7a | 2016-03-11 15:34:29 -0800 | [diff] [blame] | 258 | print(bpf_program.trace_fields()) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 259 | else: |
| 260 | try: |
| 261 | sleep(interval) |
| 262 | except KeyboardInterrupt: |
| 263 | exit() |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 264 | print_outstanding() |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 265 | count_so_far += 1 |
| 266 | if num_prints is not None and count_so_far >= num_prints: |
| 267 | exit() |