Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # stacksnoop Trace a kernel function and print all kernel stack traces. |
| 4 | # For Linux, uses BCC, eBPF, and currently x86_64 only. Inline C. |
| 5 | # |
| 6 | # USAGE: stacksnoop [-h] [-p PID] [-s] [-v] function |
| 7 | # |
| 8 | # The current implementation uses an unrolled loop for x86_64, and was written |
| 9 | # as a proof of concept. This implementation should be replaced in the future |
| 10 | # with an appropriate bpf_ call, when available. |
| 11 | # |
| 12 | # The stack depth is limited to 10 (+1 for the current instruction pointer). |
| 13 | # This could be tunable in a future version. |
| 14 | # |
| 15 | # Copyright 2016 Netflix, Inc. |
| 16 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 17 | # |
| 18 | # 12-Jan-2016 Brendan Gregg Created this. |
| 19 | |
| 20 | from __future__ import print_function |
| 21 | from bcc import BPF |
| 22 | import argparse |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame^] | 23 | import re |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 24 | |
| 25 | # arguments |
| 26 | examples = """examples: |
| 27 | ./stacksnoop ext4_sync_fs # print kernel stack traces for ext4_sync_fs |
| 28 | ./stacksnoop -s ext4_sync_fs # ... also show symbol offsets |
| 29 | ./stacksnoop -v ext4_sync_fs # ... show extra columns |
| 30 | ./stacksnoop -p 185 ext4_sync_fs # ... only when PID 185 is on-CPU |
| 31 | """ |
| 32 | parser = argparse.ArgumentParser( |
| 33 | description="Trace and print kernel stack traces for a kernel function", |
| 34 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 35 | epilog=examples) |
| 36 | parser.add_argument("-p", "--pid", |
| 37 | help="trace this PID only") |
| 38 | parser.add_argument("-s", "--offset", action="store_true", |
| 39 | help="show address offsets") |
| 40 | parser.add_argument("-v", "--verbose", action="store_true", |
| 41 | help="print more fields") |
| 42 | parser.add_argument("function", |
| 43 | help="kernel function name") |
| 44 | args = parser.parse_args() |
| 45 | function = args.function |
| 46 | offset = args.offset |
| 47 | verbose = args.verbose |
| 48 | debug = 0 |
| 49 | |
| 50 | # define BPF program |
| 51 | bpf_text = """ |
| 52 | #include <uapi/linux/ptrace.h> |
| 53 | |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame^] | 54 | BPF_STACK_TRACE(stack_traces, 128) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 55 | |
| 56 | void trace_stack(struct pt_regs *ctx) { |
| 57 | FILTER |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame^] | 58 | int stack_id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID); |
| 59 | if (stack_id >= 0) |
| 60 | bpf_trace_printk("stack_id=%d\\n", stack_id); |
| 61 | } |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 62 | """ |
| 63 | if args.pid: |
| 64 | bpf_text = bpf_text.replace('FILTER', |
| 65 | ('u32 pid; pid = bpf_get_current_pid_tgid(); ' + |
Mark Drayton | ab133d2 | 2016-03-04 00:51:20 -0800 | [diff] [blame] | 66 | 'if (pid != %s) { return; }') % (args.pid)) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 67 | else: |
| 68 | bpf_text = bpf_text.replace('FILTER', '') |
| 69 | if debug: |
| 70 | print(bpf_text) |
| 71 | |
| 72 | # initialize BPF |
| 73 | b = BPF(text=bpf_text) |
| 74 | b.attach_kprobe(event=function, fn_name="trace_stack") |
| 75 | matched = b.num_open_kprobes() |
| 76 | if matched == 0: |
| 77 | print("Function \"%s\" not found. Exiting." % function) |
| 78 | exit() |
| 79 | |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame^] | 80 | stack_traces = b.get_table("stack_traces") |
| 81 | msg_regexp = re.compile("stack_id=(\d+)") |
| 82 | |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 83 | # header |
| 84 | if verbose: |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame^] | 85 | print("%-18s %-12s %-6s %-3s %s" % ("TIME(s)", "COMM", "PID", "CPU", "SYSCALL")) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 86 | else: |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame^] | 87 | print("%-18s %s" % ("TIME(s)", "SYSCALL")) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 88 | |
| 89 | # format output |
| 90 | while 1: |
| 91 | (task, pid, cpu, flags, ts, msg) = b.trace_fields() |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame^] | 92 | m = msg_regexp.match(msg) |
| 93 | if m: |
| 94 | if verbose: |
| 95 | print("%-18.9f %-12.12s %-6d %-3d %s" % (ts, task, pid, cpu, function)) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 96 | else: |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame^] | 97 | print("%-18.9f %s" % (ts, function)) |
| 98 | |
| 99 | stack_id = int(m.group(1)) |
| 100 | for addr in stack_traces.walk(stack_id): |
| 101 | sym = b.ksymaddr(addr) if offset else b.ksym(addr) |
| 102 | print("\t%016x %s" % (addr, sym)) |
| 103 | |
| 104 | print() |