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 |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 23 | import ctypes as ct |
| 24 | import time |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 25 | |
| 26 | # arguments |
| 27 | examples = """examples: |
| 28 | ./stacksnoop ext4_sync_fs # print kernel stack traces for ext4_sync_fs |
| 29 | ./stacksnoop -s ext4_sync_fs # ... also show symbol offsets |
| 30 | ./stacksnoop -v ext4_sync_fs # ... show extra columns |
| 31 | ./stacksnoop -p 185 ext4_sync_fs # ... only when PID 185 is on-CPU |
| 32 | """ |
| 33 | parser = argparse.ArgumentParser( |
| 34 | description="Trace and print kernel stack traces for a kernel function", |
| 35 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 36 | epilog=examples) |
| 37 | parser.add_argument("-p", "--pid", |
| 38 | help="trace this PID only") |
| 39 | parser.add_argument("-s", "--offset", action="store_true", |
| 40 | help="show address offsets") |
| 41 | parser.add_argument("-v", "--verbose", action="store_true", |
| 42 | help="print more fields") |
| 43 | parser.add_argument("function", |
| 44 | help="kernel function name") |
| 45 | args = parser.parse_args() |
| 46 | function = args.function |
| 47 | offset = args.offset |
| 48 | verbose = args.verbose |
| 49 | debug = 0 |
| 50 | |
| 51 | # define BPF program |
| 52 | bpf_text = """ |
| 53 | #include <uapi/linux/ptrace.h> |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 54 | #include <linux/sched.h> |
| 55 | |
| 56 | struct data_t { |
| 57 | u64 stack_id; |
| 58 | u32 pid; |
| 59 | char comm[TASK_COMM_LEN]; |
| 60 | }; |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 61 | |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame] | 62 | BPF_STACK_TRACE(stack_traces, 128) |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 63 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 64 | |
| 65 | void trace_stack(struct pt_regs *ctx) { |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 66 | u32 pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 67 | FILTER |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 68 | struct data_t data = {}; |
| 69 | data.stack_id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID), |
| 70 | data.pid = pid; |
| 71 | bpf_get_current_comm(&data.comm, sizeof(data.comm)); |
| 72 | events.perf_submit(ctx, &data, sizeof(data)); |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame] | 73 | } |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 74 | """ |
| 75 | if args.pid: |
| 76 | bpf_text = bpf_text.replace('FILTER', |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 77 | 'if (pid != %s) { return; }' % args.pid) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 78 | else: |
| 79 | bpf_text = bpf_text.replace('FILTER', '') |
| 80 | if debug: |
| 81 | print(bpf_text) |
| 82 | |
| 83 | # initialize BPF |
| 84 | b = BPF(text=bpf_text) |
| 85 | b.attach_kprobe(event=function, fn_name="trace_stack") |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 86 | |
| 87 | TASK_COMM_LEN = 16 # linux/sched.h |
| 88 | |
| 89 | class Data(ct.Structure): |
| 90 | _fields_ = [ |
| 91 | ("stack_id", ct.c_ulonglong), |
| 92 | ("pid", ct.c_uint), |
| 93 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 94 | ] |
| 95 | |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 96 | matched = b.num_open_kprobes() |
| 97 | if matched == 0: |
| 98 | print("Function \"%s\" not found. Exiting." % function) |
| 99 | exit() |
| 100 | |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame] | 101 | stack_traces = b.get_table("stack_traces") |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 102 | start_ts = time.time() |
Vicent Marti | 592414e | 2016-03-27 18:22:03 +0200 | [diff] [blame] | 103 | |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 104 | # header |
| 105 | if verbose: |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 106 | print("%-18s %-12s %-6s %-3s %s" % |
| 107 | ("TIME(s)", "COMM", "PID", "CPU", "FUNCTION")) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 108 | else: |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 109 | print("%-18s %s" % ("TIME(s)", "FUNCTION")) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 110 | |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 111 | def print_event(cpu, data, size): |
| 112 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 113 | |
| 114 | ts = time.time() - start_ts |
| 115 | |
| 116 | if verbose: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame^] | 117 | print("%-18.9f %-12.12s %-6d %-3d %s" % |
| 118 | (ts, event.comm, event.pid, cpu, function)) |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 119 | else: |
| 120 | print("%-18.9f %s" % (ts, function)) |
| 121 | |
| 122 | for addr in stack_traces.walk(event.stack_id): |
| 123 | sym = b.ksymaddr(addr) if offset else b.ksym(addr) |
| 124 | print("\t%016x %s" % (addr, sym)) |
| 125 | |
| 126 | print() |
| 127 | |
| 128 | b["events"].open_perf_buffer(print_event) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 129 | while 1: |
Mark Drayton | 266d6f6 | 2016-05-24 07:01:01 -0700 | [diff] [blame] | 130 | b.kprobe_poll() |