Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # opensnoop Trace open() syscalls. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 6 | # |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 7 | # USAGE: opensnoop [-h] [-T] [-x] [-p PID] [-d DURATION] [-t TID] [-n NAME] |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 8 | # |
| 9 | # Copyright (c) 2015 Brendan Gregg. |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 12 | # 17-Sep-2015 Brendan Gregg Created this. |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 13 | # 29-Apr-2016 Allan McAleavy Updated for BPF_PERF_OUTPUT. |
| 14 | # 08-Oct-2016 Dina Goldshtein Support filtering by PID and TID. |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 15 | |
| 16 | from __future__ import print_function |
Gary Lin | 40fd669 | 2018-02-12 16:51:14 +0800 | [diff] [blame] | 17 | from bcc import ArgString, BPF |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 18 | import argparse |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 19 | import ctypes as ct |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 20 | from datetime import datetime, timedelta |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 21 | |
| 22 | # arguments |
| 23 | examples = """examples: |
| 24 | ./opensnoop # trace all open() syscalls |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 25 | ./opensnoop -T # include timestamps |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 26 | ./opensnoop -x # only show failed opens |
| 27 | ./opensnoop -p 181 # only trace PID 181 |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 28 | ./opensnoop -t 123 # only trace TID 123 |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 29 | ./opensnoop -d 10 # trace for 10 seconds only |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 30 | ./opensnoop -n main # only print process names containing "main" |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 31 | """ |
| 32 | parser = argparse.ArgumentParser( |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 33 | description="Trace open() syscalls", |
| 34 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 35 | epilog=examples) |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 36 | parser.add_argument("-T", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 37 | help="include timestamp on output") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 38 | parser.add_argument("-x", "--failed", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 39 | help="only show failed opens") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 40 | parser.add_argument("-p", "--pid", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 41 | help="trace this PID only") |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 42 | parser.add_argument("-t", "--tid", |
| 43 | help="trace this TID only") |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 44 | parser.add_argument("-d", "--duration", |
| 45 | help="total duration of trace in seconds") |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 46 | parser.add_argument("-n", "--name", |
Gary Lin | 40fd669 | 2018-02-12 16:51:14 +0800 | [diff] [blame] | 47 | type=ArgString, |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 48 | help="only print process names containing this name") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 49 | parser.add_argument("--ebpf", action="store_true", |
| 50 | help=argparse.SUPPRESS) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 51 | args = parser.parse_args() |
| 52 | debug = 0 |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 53 | if args.duration: |
| 54 | args.duration = timedelta(seconds=int(args.duration)) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 55 | |
| 56 | # define BPF program |
| 57 | bpf_text = """ |
| 58 | #include <uapi/linux/ptrace.h> |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 59 | #include <uapi/linux/limits.h> |
| 60 | #include <linux/sched.h> |
| 61 | |
| 62 | struct val_t { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 63 | u64 id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 64 | u64 ts; |
| 65 | char comm[TASK_COMM_LEN]; |
| 66 | const char *fname; |
| 67 | }; |
| 68 | |
| 69 | struct data_t { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 70 | u64 id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 71 | u64 ts; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 72 | int ret; |
| 73 | char comm[TASK_COMM_LEN]; |
| 74 | char fname[NAME_MAX]; |
| 75 | }; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 76 | |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 77 | BPF_HASH(infotmp, u64, struct val_t); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 78 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 79 | |
Joel Fernandes | 9af548f | 2018-01-07 11:59:09 -0800 | [diff] [blame] | 80 | int trace_entry(struct pt_regs *ctx, int dfd, const char __user *filename) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 81 | { |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 82 | struct val_t val = {}; |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 83 | u64 id = bpf_get_current_pid_tgid(); |
| 84 | u32 pid = id >> 32; // PID is higher part |
| 85 | u32 tid = id; // Cast and get the lower part |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 86 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 87 | FILTER |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 88 | if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 89 | val.id = id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 90 | val.ts = bpf_ktime_get_ns(); |
| 91 | val.fname = filename; |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 92 | infotmp.update(&id, &val); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 93 | } |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 94 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 95 | return 0; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 98 | int trace_return(struct pt_regs *ctx) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 99 | { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 100 | u64 id = bpf_get_current_pid_tgid(); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 101 | struct val_t *valp; |
| 102 | struct data_t data = {}; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 103 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 104 | u64 tsp = bpf_ktime_get_ns(); |
| 105 | |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 106 | valp = infotmp.lookup(&id); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 107 | if (valp == 0) { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 108 | // missed entry |
| 109 | return 0; |
| 110 | } |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 111 | bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm); |
| 112 | bpf_probe_read(&data.fname, sizeof(data.fname), (void *)valp->fname); |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 113 | data.id = valp->id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 114 | data.ts = tsp / 1000; |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 115 | data.ret = PT_REGS_RC(ctx); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 116 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 117 | events.perf_submit(ctx, &data, sizeof(data)); |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 118 | infotmp.delete(&id); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 119 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 120 | return 0; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 121 | } |
| 122 | """ |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 123 | if args.tid: # TID trumps PID |
| 124 | bpf_text = bpf_text.replace('FILTER', |
| 125 | 'if (tid != %s) { return 0; }' % args.tid) |
| 126 | elif args.pid: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 127 | bpf_text = bpf_text.replace('FILTER', |
| 128 | 'if (pid != %s) { return 0; }' % args.pid) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 129 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 130 | bpf_text = bpf_text.replace('FILTER', '') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 131 | if debug or args.ebpf: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 132 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 133 | if args.ebpf: |
| 134 | exit() |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 135 | |
| 136 | # initialize BPF |
| 137 | b = BPF(text=bpf_text) |
Joel Fernandes | 9af548f | 2018-01-07 11:59:09 -0800 | [diff] [blame] | 138 | b.attach_kprobe(event="do_sys_open", fn_name="trace_entry") |
| 139 | b.attach_kretprobe(event="do_sys_open", fn_name="trace_return") |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 140 | |
| 141 | TASK_COMM_LEN = 16 # linux/sched.h |
| 142 | NAME_MAX = 255 # linux/limits.h |
| 143 | |
| 144 | class Data(ct.Structure): |
| 145 | _fields_ = [ |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 146 | ("id", ct.c_ulonglong), |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 147 | ("ts", ct.c_ulonglong), |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 148 | ("ret", ct.c_int), |
| 149 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 150 | ("fname", ct.c_char * NAME_MAX) |
| 151 | ] |
| 152 | |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 153 | initial_ts = 0 |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 154 | |
| 155 | # header |
| 156 | if args.timestamp: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 157 | print("%-14s" % ("TIME(s)"), end="") |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 158 | print("%-6s %-16s %4s %3s %s" % |
| 159 | ("TID" if args.tid else "PID", "COMM", "FD", "ERR", "PATH")) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 160 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 161 | # process event |
| 162 | def print_event(cpu, data, size): |
| 163 | event = ct.cast(data, ct.POINTER(Data)).contents |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 164 | global initial_ts |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 165 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 166 | # split return value into FD and errno columns |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 167 | if event.ret >= 0: |
| 168 | fd_s = event.ret |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 169 | err = 0 |
| 170 | else: |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 171 | fd_s = -1 |
| 172 | err = - event.ret |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 173 | |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 174 | if not initial_ts: |
| 175 | initial_ts = event.ts |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 176 | |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 177 | if args.failed and (event.ret >= 0): |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 178 | return |
| 179 | |
Gary Lin | 40fd669 | 2018-02-12 16:51:14 +0800 | [diff] [blame] | 180 | if args.name and bytes(args.name) not in event.comm: |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 181 | return |
| 182 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 183 | if args.timestamp: |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 184 | delta = event.ts - initial_ts |
| 185 | print("%-14.9f" % (float(delta) / 1000000), end="") |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 186 | |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 187 | print("%-6d %-16s %4d %3d %s" % |
| 188 | (event.id & 0xffffffff if args.tid else event.id >> 32, |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 189 | event.comm.decode(), fd_s, err, event.fname.decode())) |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 190 | |
| 191 | # loop with callback to print_event |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 192 | b["events"].open_perf_buffer(print_event, page_cnt=64) |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 193 | start_time = datetime.now() |
| 194 | while not args.duration or datetime.now() - start_time < args.duration: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 195 | b.perf_buffer_poll() |