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] |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 7 | # [--combined-only] [-s SAMPLE_RATE] [-T TOP] [-z MIN_SIZE] |
| 8 | # [-Z MAX_SIZE] [-O OBJ] |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 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 |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 47 | Trace allocations and display each individual allocator function call |
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. |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 65 | Supports both user-mode allocations made with libc functions and kernel-mode |
| 66 | allocations made with kmalloc/kmem_cache_alloc/get_free_pages and corresponding |
| 67 | memory release functions. |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 68 | """ |
| 69 | |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 70 | parser = argparse.ArgumentParser(description=description, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 71 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 72 | epilog=examples) |
Sasha Goldshtein | d2241f4 | 2016-02-09 06:23:10 -0800 | [diff] [blame] | 73 | parser.add_argument("-p", "--pid", type=int, default=-1, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 74 | help="the PID to trace; if not specified, trace kernel allocs") |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 75 | parser.add_argument("-t", "--trace", action="store_true", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 76 | help="print trace messages for each alloc/free call") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 77 | parser.add_argument("interval", nargs="?", default=5, type=int, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 78 | help="interval in seconds to print outstanding allocations") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 79 | parser.add_argument("count", nargs="?", type=int, |
| 80 | help="number of times to print the report before exiting") |
Sasha Goldshtein | a7cc6c2 | 2016-02-07 12:03:54 -0800 | [diff] [blame] | 81 | parser.add_argument("-a", "--show-allocs", default=False, action="store_true", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 82 | help="show allocation addresses and sizes as well as call stacks") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 83 | parser.add_argument("-o", "--older", default=500, type=int, |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 84 | help="prune allocations younger than this age in milliseconds") |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 85 | parser.add_argument("-c", "--command", |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 86 | help="execute and trace the specified command") |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 87 | parser.add_argument("--combined-only", default=False, action="store_true", |
| 88 | help="show combined allocation statistics only") |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 89 | parser.add_argument("-s", "--sample-rate", default=1, type=int, |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 90 | help="sample every N-th allocation to decrease the overhead") |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 91 | parser.add_argument("-T", "--top", type=int, default=10, |
| 92 | help="display only this many top allocating stacks (by size)") |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 93 | parser.add_argument("-z", "--min-size", type=int, |
| 94 | help="capture only allocations larger than this size") |
| 95 | parser.add_argument("-Z", "--max-size", type=int, |
| 96 | help="capture only allocations smaller than this size") |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 97 | parser.add_argument("-O", "--obj", type=str, default="c", |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 98 | help="attach to allocator functions in the specified object") |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 99 | |
| 100 | args = parser.parse_args() |
| 101 | |
Sasha Goldshtein | d2241f4 | 2016-02-09 06:23:10 -0800 | [diff] [blame] | 102 | pid = args.pid |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 103 | command = args.command |
| 104 | kernel_trace = (pid == -1 and command is None) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 105 | trace_all = args.trace |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 106 | interval = args.interval |
| 107 | min_age_ns = 1e6 * args.older |
Sasha Goldshtein | 521ab4f | 2016-02-08 05:48:31 -0800 | [diff] [blame] | 108 | sample_every_n = args.sample_rate |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 109 | num_prints = args.count |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 110 | top_stacks = args.top |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 111 | min_size = args.min_size |
| 112 | max_size = args.max_size |
Maria Kacik | 9389ab4 | 2017-01-18 21:43:41 -0800 | [diff] [blame] | 113 | obj = args.obj |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 114 | |
| 115 | if min_size is not None and max_size is not None and min_size > max_size: |
| 116 | print("min_size (-z) can't be greater than max_size (-Z)") |
| 117 | exit(1) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 118 | |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 119 | if command is not None: |
| 120 | print("Executing '%s' and tracing the resulting process." % command) |
| 121 | pid = run_command_get_pid(command) |
Sasha Goldshtein | 2922861 | 2016-02-07 12:20:19 -0800 | [diff] [blame] | 122 | |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 123 | bpf_source = """ |
| 124 | #include <uapi/linux/ptrace.h> |
| 125 | |
| 126 | struct alloc_info_t { |
| 127 | u64 size; |
| 128 | u64 timestamp_ns; |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 129 | int stack_id; |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 132 | struct combined_alloc_info_t { |
| 133 | u64 total_size; |
| 134 | u64 number_of_allocs; |
| 135 | }; |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 136 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 137 | BPF_HASH(sizes, u64); |
| 138 | BPF_TABLE("hash", u64, struct alloc_info_t, allocs, 1000000); |
| 139 | BPF_HASH(memptrs, u64, u64); |
| 140 | BPF_STACK_TRACE(stack_traces, 10240) |
| 141 | BPF_TABLE("hash", u64, struct combined_alloc_info_t, combined_allocs, 10240); |
| 142 | |
| 143 | static inline void update_statistics_add(u64 stack_id, u64 sz) { |
| 144 | struct combined_alloc_info_t *existing_cinfo; |
| 145 | struct combined_alloc_info_t cinfo = {0}; |
| 146 | |
| 147 | existing_cinfo = combined_allocs.lookup(&stack_id); |
| 148 | if (existing_cinfo != 0) |
| 149 | cinfo = *existing_cinfo; |
| 150 | |
| 151 | cinfo.total_size += sz; |
| 152 | cinfo.number_of_allocs += 1; |
| 153 | |
| 154 | combined_allocs.update(&stack_id, &cinfo); |
| 155 | } |
| 156 | |
| 157 | static inline void update_statistics_del(u64 stack_id, u64 sz) { |
| 158 | struct combined_alloc_info_t *existing_cinfo; |
| 159 | struct combined_alloc_info_t cinfo = {0}; |
| 160 | |
| 161 | existing_cinfo = combined_allocs.lookup(&stack_id); |
| 162 | if (existing_cinfo != 0) |
| 163 | cinfo = *existing_cinfo; |
| 164 | |
| 165 | if (sz >= cinfo.total_size) |
| 166 | cinfo.total_size = 0; |
| 167 | else |
| 168 | cinfo.total_size -= sz; |
| 169 | |
| 170 | if (cinfo.number_of_allocs > 0) |
| 171 | cinfo.number_of_allocs -= 1; |
| 172 | |
| 173 | combined_allocs.update(&stack_id, &cinfo); |
| 174 | } |
| 175 | |
| 176 | static inline int gen_alloc_enter(struct pt_regs *ctx, size_t size) { |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 177 | SIZE_FILTER |
| 178 | if (SAMPLE_EVERY_N > 1) { |
| 179 | u64 ts = bpf_ktime_get_ns(); |
| 180 | if (ts % SAMPLE_EVERY_N != 0) |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | u64 pid = bpf_get_current_pid_tgid(); |
| 185 | u64 size64 = size; |
| 186 | sizes.update(&pid, &size64); |
| 187 | |
| 188 | if (SHOULD_PRINT) |
| 189 | bpf_trace_printk("alloc entered, size = %u\\n", size); |
| 190 | return 0; |
| 191 | } |
| 192 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 193 | static inline int gen_alloc_exit2(struct pt_regs *ctx, u64 address) { |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 194 | u64 pid = bpf_get_current_pid_tgid(); |
| 195 | u64* size64 = sizes.lookup(&pid); |
| 196 | struct alloc_info_t info = {0}; |
| 197 | |
| 198 | if (size64 == 0) |
| 199 | return 0; // missed alloc entry |
| 200 | |
| 201 | info.size = *size64; |
| 202 | sizes.delete(&pid); |
| 203 | |
| 204 | info.timestamp_ns = bpf_ktime_get_ns(); |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 205 | info.stack_id = stack_traces.get_stackid(ctx, STACK_FLAGS); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 206 | allocs.update(&address, &info); |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 207 | update_statistics_add(info.stack_id, info.size); |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 208 | |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 209 | if (SHOULD_PRINT) { |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 210 | bpf_trace_printk("alloc exited, size = %lu, result = %lx\\n", |
| 211 | info.size, address); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 216 | static inline int gen_alloc_exit(struct pt_regs *ctx) { |
| 217 | return gen_alloc_exit2(ctx, PT_REGS_RC(ctx)); |
| 218 | } |
| 219 | |
| 220 | static inline int gen_free_enter(struct pt_regs *ctx, void *address) { |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 221 | u64 addr = (u64)address; |
| 222 | struct alloc_info_t *info = allocs.lookup(&addr); |
| 223 | if (info == 0) |
| 224 | return 0; |
| 225 | |
| 226 | allocs.delete(&addr); |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 227 | update_statistics_del(info->stack_id, info->size); |
Sasha Goldshtein | 43fa041 | 2016-02-10 22:17:26 -0800 | [diff] [blame] | 228 | |
| 229 | if (SHOULD_PRINT) { |
| 230 | bpf_trace_printk("free entered, address = %lx, size = %lu\\n", |
| 231 | address, info->size); |
| 232 | } |
| 233 | return 0; |
| 234 | } |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 235 | |
| 236 | int malloc_enter(struct pt_regs *ctx, size_t size) { |
| 237 | return gen_alloc_enter(ctx, size); |
| 238 | } |
| 239 | |
| 240 | int malloc_exit(struct pt_regs *ctx) { |
| 241 | return gen_alloc_exit(ctx); |
| 242 | } |
| 243 | |
| 244 | int free_enter(struct pt_regs *ctx, void *address) { |
| 245 | return gen_free_enter(ctx, address); |
| 246 | } |
| 247 | |
| 248 | int calloc_enter(struct pt_regs *ctx, size_t nmemb, size_t size) { |
| 249 | return gen_alloc_enter(ctx, nmemb * size); |
| 250 | } |
| 251 | |
| 252 | int calloc_exit(struct pt_regs *ctx) { |
| 253 | return gen_alloc_exit(ctx); |
| 254 | } |
| 255 | |
| 256 | int realloc_enter(struct pt_regs *ctx, void *ptr, size_t size) { |
| 257 | gen_free_enter(ctx, ptr); |
| 258 | return gen_alloc_enter(ctx, size); |
| 259 | } |
| 260 | |
| 261 | int realloc_exit(struct pt_regs *ctx) { |
| 262 | return gen_alloc_exit(ctx); |
| 263 | } |
| 264 | |
| 265 | int posix_memalign_enter(struct pt_regs *ctx, void **memptr, size_t alignment, |
| 266 | size_t size) { |
| 267 | u64 memptr64 = (u64)(size_t)memptr; |
| 268 | u64 pid = bpf_get_current_pid_tgid(); |
| 269 | |
| 270 | memptrs.update(&pid, &memptr64); |
| 271 | return gen_alloc_enter(ctx, size); |
| 272 | } |
| 273 | |
| 274 | int posix_memalign_exit(struct pt_regs *ctx) { |
| 275 | u64 pid = bpf_get_current_pid_tgid(); |
| 276 | u64 *memptr64 = memptrs.lookup(&pid); |
| 277 | void *addr; |
| 278 | |
| 279 | if (memptr64 == 0) |
| 280 | return 0; |
| 281 | |
| 282 | memptrs.delete(&pid); |
| 283 | |
| 284 | if (bpf_probe_read(&addr, sizeof(void*), (void*)(size_t)*memptr64) != 0) |
| 285 | return 0; |
| 286 | |
| 287 | u64 addr64 = (u64)(size_t)addr; |
| 288 | return gen_alloc_exit2(ctx, addr64); |
| 289 | } |
| 290 | |
| 291 | int aligned_alloc_enter(struct pt_regs *ctx, size_t alignment, size_t size) { |
| 292 | return gen_alloc_enter(ctx, size); |
| 293 | } |
| 294 | |
| 295 | int aligned_alloc_exit(struct pt_regs *ctx) { |
| 296 | return gen_alloc_exit(ctx); |
| 297 | } |
| 298 | |
| 299 | int valloc_enter(struct pt_regs *ctx, size_t size) { |
| 300 | return gen_alloc_enter(ctx, size); |
| 301 | } |
| 302 | |
| 303 | int valloc_exit(struct pt_regs *ctx) { |
| 304 | return gen_alloc_exit(ctx); |
| 305 | } |
| 306 | |
| 307 | int memalign_enter(struct pt_regs *ctx, size_t alignment, size_t size) { |
| 308 | return gen_alloc_enter(ctx, size); |
| 309 | } |
| 310 | |
| 311 | int memalign_exit(struct pt_regs *ctx) { |
| 312 | return gen_alloc_exit(ctx); |
| 313 | } |
| 314 | |
| 315 | int pvalloc_enter(struct pt_regs *ctx, size_t size) { |
| 316 | return gen_alloc_enter(ctx, size); |
| 317 | } |
| 318 | |
| 319 | int pvalloc_exit(struct pt_regs *ctx) { |
| 320 | return gen_alloc_exit(ctx); |
| 321 | } |
Sasha Goldshtein | 0e856f4 | 2016-03-21 07:26:52 -0700 | [diff] [blame] | 322 | """ |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 323 | |
| 324 | bpf_source_kernel = """ |
| 325 | |
| 326 | TRACEPOINT_PROBE(kmem, kmalloc) { |
| 327 | gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc); |
| 328 | return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr); |
| 329 | } |
| 330 | |
| 331 | TRACEPOINT_PROBE(kmem, kmalloc_node) { |
| 332 | gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc); |
| 333 | return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr); |
| 334 | } |
| 335 | |
| 336 | TRACEPOINT_PROBE(kmem, kfree) { |
| 337 | return gen_free_enter((struct pt_regs *)args, (void *)args->ptr); |
| 338 | } |
| 339 | |
| 340 | TRACEPOINT_PROBE(kmem, kmem_cache_alloc) { |
| 341 | gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc); |
| 342 | return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr); |
| 343 | } |
| 344 | |
| 345 | TRACEPOINT_PROBE(kmem, kmem_cache_alloc_node) { |
| 346 | gen_alloc_enter((struct pt_regs *)args, args->bytes_alloc); |
| 347 | return gen_alloc_exit2((struct pt_regs *)args, (size_t)args->ptr); |
| 348 | } |
| 349 | |
| 350 | TRACEPOINT_PROBE(kmem, kmem_cache_free) { |
| 351 | return gen_free_enter((struct pt_regs *)args, (void *)args->ptr); |
| 352 | } |
| 353 | |
| 354 | TRACEPOINT_PROBE(kmem, mm_page_alloc) { |
| 355 | gen_alloc_enter((struct pt_regs *)args, PAGE_SIZE << args->order); |
| 356 | return gen_alloc_exit2((struct pt_regs *)args, args->pfn); |
| 357 | } |
| 358 | |
| 359 | TRACEPOINT_PROBE(kmem, mm_page_free) { |
| 360 | return gen_free_enter((struct pt_regs *)args, (void *)args->pfn); |
| 361 | } |
| 362 | """ |
| 363 | |
| 364 | if kernel_trace: |
| 365 | bpf_source += bpf_source_kernel |
| 366 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 367 | 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] | 368 | bpf_source = bpf_source.replace("SAMPLE_EVERY_N", str(sample_every_n)) |
Sasha Goldshtein | 5045964 | 2016-02-10 08:35:20 -0800 | [diff] [blame] | 369 | |
| 370 | size_filter = "" |
| 371 | if min_size is not None and max_size is not None: |
| 372 | size_filter = "if (size < %d || size > %d) return 0;" % \ |
| 373 | (min_size, max_size) |
| 374 | elif min_size is not None: |
| 375 | size_filter = "if (size < %d) return 0;" % min_size |
| 376 | elif max_size is not None: |
| 377 | size_filter = "if (size > %d) return 0;" % max_size |
| 378 | bpf_source = bpf_source.replace("SIZE_FILTER", size_filter) |
| 379 | |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 380 | stack_flags = "BPF_F_REUSE_STACKID" |
| 381 | if not kernel_trace: |
| 382 | stack_flags += "|BPF_F_USER_STACK" |
| 383 | bpf_source = bpf_source.replace("STACK_FLAGS", stack_flags) |
| 384 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 385 | bpf_program = BPF(text=bpf_source) |
| 386 | |
| 387 | if not kernel_trace: |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 388 | print("Attaching to pid %d, Ctrl+C to quit." % pid) |
| 389 | |
| 390 | def attach_probes(sym, fn_prefix=None, can_fail=False): |
| 391 | if fn_prefix is None: |
| 392 | fn_prefix = sym |
| 393 | |
| 394 | try: |
| 395 | bpf_program.attach_uprobe(name=obj, sym=sym, |
| 396 | fn_name=fn_prefix+"_enter", |
| 397 | pid=pid) |
| 398 | bpf_program.attach_uretprobe(name=obj, sym=sym, |
| 399 | fn_name=fn_prefix+"_exit", |
| 400 | pid=pid) |
| 401 | except Exception: |
| 402 | if can_fail: |
| 403 | return |
| 404 | else: |
| 405 | raise |
| 406 | |
| 407 | attach_probes("malloc") |
| 408 | attach_probes("calloc") |
| 409 | attach_probes("realloc") |
| 410 | attach_probes("posix_memalign") |
| 411 | attach_probes("valloc") |
| 412 | attach_probes("memalign") |
| 413 | attach_probes("pvalloc") |
| 414 | attach_probes("aligned_alloc", can_fail=True) # added in C11 |
| 415 | bpf_program.attach_uprobe(name=obj, sym="free", fn_name="free_enter", |
| 416 | pid=pid) |
| 417 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 418 | else: |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 419 | print("Attaching to kernel allocators, Ctrl+C to quit.") |
| 420 | |
| 421 | # No probe attaching here. Allocations are counted by attaching to |
| 422 | # tracepoints. |
| 423 | # |
| 424 | # Memory allocations in Linux kernel are not limited to malloc/free |
| 425 | # equivalents. It's also common to allocate a memory page or multiple |
| 426 | # pages. Page allocator have two interfaces, one working with page frame |
| 427 | # numbers (PFN), while other working with page addresses. It's possible |
| 428 | # to allocate pages with one kind of functions, and free them with |
| 429 | # another. Code in kernel can easy convert PFNs to addresses and back, |
| 430 | # but it's hard to do the same in eBPF kprobe without fragile hacks. |
| 431 | # |
| 432 | # Fortunately, Linux exposes tracepoints for memory allocations, which |
| 433 | # can be instrumented by eBPF programs. Tracepoint for page allocations |
| 434 | # gives access to PFNs for both allocator interfaces. So there is no |
| 435 | # need to guess which allocation corresponds to which free. |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 436 | |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 437 | def print_outstanding(): |
Sasha Goldshtein | c8148c8 | 2016-02-09 11:15:41 -0800 | [diff] [blame] | 438 | print("[%s] Top %d stacks with outstanding allocations:" % |
| 439 | (datetime.now().strftime("%H:%M:%S"), top_stacks)) |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 440 | alloc_info = {} |
| 441 | allocs = bpf_program["allocs"] |
| 442 | stack_traces = bpf_program["stack_traces"] |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 443 | 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] | 444 | if BPF.monotonic_time() - min_age_ns < info.timestamp_ns: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 445 | continue |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 446 | if info.stack_id < 0: |
| 447 | continue |
| 448 | if info.stack_id in alloc_info: |
| 449 | alloc_info[info.stack_id].update(info.size) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 450 | else: |
Sasha Goldshtein | 49df994 | 2017-02-08 23:22:06 -0500 | [diff] [blame] | 451 | stack = list(stack_traces.walk(info.stack_id)) |
| 452 | combined = [] |
| 453 | for addr in stack: |
| 454 | combined.append(bpf_program.sym(addr, pid, |
Sasha Goldshtein | 0155385 | 2017-02-09 03:58:09 -0500 | [diff] [blame] | 455 | show_module=True, show_offset=True)) |
Sasha Goldshtein | 49df994 | 2017-02-08 23:22:06 -0500 | [diff] [blame] | 456 | alloc_info[info.stack_id] = Allocation(combined, |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 457 | info.size) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 458 | if args.show_allocs: |
| 459 | print("\taddr = %x size = %s" % |
| 460 | (address.value, info.size)) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 461 | to_show = sorted(alloc_info.values(), |
| 462 | key=lambda a: a.size)[-top_stacks:] |
Vicent Marti | e25ae03 | 2016-03-25 17:14:34 +0100 | [diff] [blame] | 463 | for alloc in to_show: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 464 | 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] | 465 | (alloc.size, alloc.count, "\n\t\t".join(alloc.stack))) |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 466 | |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 467 | def print_outstanding_combined(): |
| 468 | stack_traces = bpf_program["stack_traces"] |
| 469 | stacks = sorted(bpf_program["combined_allocs"].items(), |
| 470 | key=lambda a: -a[1].total_size) |
| 471 | cnt = 1 |
| 472 | entries = [] |
| 473 | for stack_id, info in stacks: |
| 474 | try: |
| 475 | trace = [] |
| 476 | for addr in stack_traces.walk(stack_id.value): |
| 477 | sym = bpf_program.sym(addr, pid, |
| 478 | show_module=True, |
| 479 | show_offset=True) |
| 480 | trace.append(sym) |
| 481 | trace = "\n\t\t".join(trace) |
| 482 | except KeyError: |
| 483 | trace = "stack information lost" |
| 484 | |
| 485 | entry = ("\t%d bytes in %d allocations from stack\n\t\t%s" % |
| 486 | (info.total_size, info.number_of_allocs, trace)) |
| 487 | entries.append(entry) |
| 488 | |
| 489 | cnt += 1 |
| 490 | if cnt > top_stacks: |
| 491 | break |
| 492 | |
| 493 | print("[%s] Top %d stacks with outstanding allocations:" % |
| 494 | (datetime.now().strftime("%H:%M:%S"), top_stacks)) |
| 495 | |
| 496 | print('\n'.join(reversed(entries))) |
| 497 | |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 498 | count_so_far = 0 |
Sasha Goldshtein | 4f1ea67 | 2016-02-07 01:57:42 -0800 | [diff] [blame] | 499 | while True: |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 500 | if trace_all: |
Brenden Blanco | c94ab7a | 2016-03-11 15:34:29 -0800 | [diff] [blame] | 501 | print(bpf_program.trace_fields()) |
Sasha Goldshtein | 33522d7 | 2016-02-08 03:39:44 -0800 | [diff] [blame] | 502 | else: |
| 503 | try: |
| 504 | sleep(interval) |
| 505 | except KeyboardInterrupt: |
| 506 | exit() |
Rinat Ibragimov | 2c1799c | 2017-07-11 21:14:08 +0300 | [diff] [blame^] | 507 | if args.combined_only: |
| 508 | print_outstanding_combined() |
| 509 | else: |
| 510 | print_outstanding() |
Sasha Goldshtein | 40e55ba | 2016-02-09 05:53:48 -0800 | [diff] [blame] | 511 | count_so_far += 1 |
| 512 | if num_prints is not None and count_so_far >= num_prints: |
| 513 | exit() |