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