Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [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. |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 15 | # 28-Dec-2018 Tim Douglas Print flags argument, enable filtering |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 16 | # 06-Jan-2019 Takuma Kume Support filtering by UID |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 17 | |
| 18 | from __future__ import print_function |
Gary Lin | 40fd669 | 2018-02-12 16:51:14 +0800 | [diff] [blame] | 19 | from bcc import ArgString, BPF |
japroc | aed9b1e | 2019-01-04 20:21:46 +0300 | [diff] [blame] | 20 | from bcc.utils import printb |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 21 | import argparse |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 22 | from datetime import datetime, timedelta |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 23 | import os |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 24 | |
| 25 | # arguments |
| 26 | examples = """examples: |
| 27 | ./opensnoop # trace all open() syscalls |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 28 | ./opensnoop -T # include timestamps |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 29 | ./opensnoop -U # include UID |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 30 | ./opensnoop -x # only show failed opens |
| 31 | ./opensnoop -p 181 # only trace PID 181 |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 32 | ./opensnoop -t 123 # only trace TID 123 |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 33 | ./opensnoop -u 1000 # only trace UID 1000 |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 34 | ./opensnoop -d 10 # trace for 10 seconds only |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 35 | ./opensnoop -n main # only print process names containing "main" |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 36 | ./opensnoop -e # show extended fields |
| 37 | ./opensnoop -f O_WRONLY -f O_RDWR # only print calls for writing |
Alban Crequy | b2aa29f | 2019-12-16 10:54:18 +0100 | [diff] [blame] | 38 | ./opensnoop --cgroupmap ./mappath # only trace cgroups in this BPF map |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 39 | """ |
| 40 | parser = argparse.ArgumentParser( |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 41 | description="Trace open() syscalls", |
| 42 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 43 | epilog=examples) |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 44 | parser.add_argument("-T", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 45 | help="include timestamp on output") |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 46 | parser.add_argument("-U", "--print-uid", action="store_true", |
| 47 | help="print UID column") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 48 | parser.add_argument("-x", "--failed", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 49 | help="only show failed opens") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 50 | parser.add_argument("-p", "--pid", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 51 | help="trace this PID only") |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 52 | parser.add_argument("-t", "--tid", |
| 53 | help="trace this TID only") |
Alban Crequy | b2aa29f | 2019-12-16 10:54:18 +0100 | [diff] [blame] | 54 | parser.add_argument("--cgroupmap", |
| 55 | help="trace cgroups in this BPF map only") |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 56 | parser.add_argument("-u", "--uid", |
| 57 | help="trace this UID only") |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 58 | parser.add_argument("-d", "--duration", |
| 59 | help="total duration of trace in seconds") |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 60 | parser.add_argument("-n", "--name", |
Gary Lin | 40fd669 | 2018-02-12 16:51:14 +0800 | [diff] [blame] | 61 | type=ArgString, |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 62 | help="only print process names containing this name") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 63 | parser.add_argument("--ebpf", action="store_true", |
| 64 | help=argparse.SUPPRESS) |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 65 | parser.add_argument("-e", "--extended_fields", action="store_true", |
| 66 | help="show extended fields") |
| 67 | parser.add_argument("-f", "--flag_filter", action="append", |
| 68 | help="filter on flags argument (e.g., O_WRONLY)") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 69 | args = parser.parse_args() |
| 70 | debug = 0 |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 71 | if args.duration: |
| 72 | args.duration = timedelta(seconds=int(args.duration)) |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 73 | flag_filter_mask = 0 |
| 74 | for flag in args.flag_filter or []: |
| 75 | if not flag.startswith('O_'): |
| 76 | exit("Bad flag: %s" % flag) |
| 77 | try: |
| 78 | flag_filter_mask |= getattr(os, flag) |
| 79 | except AttributeError: |
| 80 | exit("Bad flag: %s" % flag) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 81 | |
| 82 | # define BPF program |
| 83 | bpf_text = """ |
| 84 | #include <uapi/linux/ptrace.h> |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 85 | #include <uapi/linux/limits.h> |
| 86 | #include <linux/sched.h> |
| 87 | |
| 88 | struct val_t { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 89 | u64 id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 90 | char comm[TASK_COMM_LEN]; |
| 91 | const char *fname; |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 92 | int flags; // EXTENDED_STRUCT_MEMBER |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | struct data_t { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 96 | u64 id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 97 | u64 ts; |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 98 | u32 uid; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 99 | int ret; |
| 100 | char comm[TASK_COMM_LEN]; |
| 101 | char fname[NAME_MAX]; |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 102 | int flags; // EXTENDED_STRUCT_MEMBER |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 103 | }; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 104 | |
Alban Crequy | b2aa29f | 2019-12-16 10:54:18 +0100 | [diff] [blame] | 105 | #if CGROUPSET |
| 106 | BPF_TABLE_PINNED("hash", u64, u64, cgroupset, 1024, "CGROUPPATH"); |
| 107 | #endif |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 108 | BPF_PERF_OUTPUT(events); |
Jiri Olsa | c347fe6 | 2019-12-22 15:52:39 +0100 | [diff] [blame] | 109 | """ |
| 110 | |
| 111 | bpf_text_kprobe = """ |
| 112 | BPF_HASH(infotmp, u64, struct val_t); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 113 | |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 114 | int trace_entry(struct pt_regs *ctx, int dfd, const char __user *filename, int flags) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 115 | { |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 116 | struct val_t val = {}; |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 117 | u64 id = bpf_get_current_pid_tgid(); |
| 118 | u32 pid = id >> 32; // PID is higher part |
| 119 | u32 tid = id; // Cast and get the lower part |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 120 | u32 uid = bpf_get_current_uid_gid(); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 121 | |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 122 | PID_TID_FILTER |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 123 | UID_FILTER |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 124 | FLAGS_FILTER |
Alban Crequy | b2aa29f | 2019-12-16 10:54:18 +0100 | [diff] [blame] | 125 | #if CGROUPSET |
| 126 | u64 cgroupid = bpf_get_current_cgroup_id(); |
| 127 | if (cgroupset.lookup(&cgroupid) == NULL) { |
| 128 | return 0; |
| 129 | } |
| 130 | #endif |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 131 | if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 132 | val.id = id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 133 | val.fname = filename; |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 134 | val.flags = flags; // EXTENDED_STRUCT_MEMBER |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 135 | infotmp.update(&id, &val); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 136 | } |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 137 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 138 | return 0; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 141 | int trace_return(struct pt_regs *ctx) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 142 | { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 143 | u64 id = bpf_get_current_pid_tgid(); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 144 | struct val_t *valp; |
| 145 | struct data_t data = {}; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 146 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 147 | u64 tsp = bpf_ktime_get_ns(); |
| 148 | |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 149 | valp = infotmp.lookup(&id); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 150 | if (valp == 0) { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 151 | // missed entry |
| 152 | return 0; |
| 153 | } |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 154 | bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm); |
Sumanth Korikkar | 023154c | 2020-04-20 05:54:57 -0500 | [diff] [blame] | 155 | bpf_probe_read_user(&data.fname, sizeof(data.fname), (void *)valp->fname); |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 156 | data.id = valp->id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 157 | data.ts = tsp / 1000; |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 158 | data.uid = bpf_get_current_uid_gid(); |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 159 | data.flags = valp->flags; // EXTENDED_STRUCT_MEMBER |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 160 | data.ret = PT_REGS_RC(ctx); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 161 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 162 | events.perf_submit(ctx, &data, sizeof(data)); |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 163 | infotmp.delete(&id); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 164 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 165 | return 0; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 166 | } |
| 167 | """ |
Jiri Olsa | c347fe6 | 2019-12-22 15:52:39 +0100 | [diff] [blame] | 168 | |
| 169 | bpf_text_kfunc= """ |
Sumanth Korikkar | 023154c | 2020-04-20 05:54:57 -0500 | [diff] [blame] | 170 | KRETFUNC_PROBE(do_sys_open, int dfd, const char __user *filename, int flags, int mode, int ret) |
Jiri Olsa | c347fe6 | 2019-12-22 15:52:39 +0100 | [diff] [blame] | 171 | { |
| 172 | u64 id = bpf_get_current_pid_tgid(); |
| 173 | u32 pid = id >> 32; // PID is higher part |
| 174 | u32 tid = id; // Cast and get the lower part |
| 175 | u32 uid = bpf_get_current_uid_gid(); |
| 176 | |
| 177 | PID_TID_FILTER |
| 178 | UID_FILTER |
| 179 | FLAGS_FILTER |
Alban Crequy | 510fc74 | 2020-03-19 14:07:38 +0100 | [diff] [blame] | 180 | #if CGROUPSET |
| 181 | u64 cgroupid = bpf_get_current_cgroup_id(); |
| 182 | if (cgroupset.lookup(&cgroupid) == NULL) { |
| 183 | return 0; |
| 184 | } |
| 185 | #endif |
Jiri Olsa | c347fe6 | 2019-12-22 15:52:39 +0100 | [diff] [blame] | 186 | |
| 187 | struct data_t data = {}; |
| 188 | bpf_get_current_comm(&data.comm, sizeof(data.comm)); |
| 189 | |
| 190 | u64 tsp = bpf_ktime_get_ns(); |
| 191 | |
Sumanth Korikkar | 023154c | 2020-04-20 05:54:57 -0500 | [diff] [blame] | 192 | bpf_probe_read_user(&data.fname, sizeof(data.fname), (void *)filename); |
Jiri Olsa | c347fe6 | 2019-12-22 15:52:39 +0100 | [diff] [blame] | 193 | data.id = id; |
| 194 | data.ts = tsp / 1000; |
| 195 | data.uid = bpf_get_current_uid_gid(); |
| 196 | data.flags = flags; // EXTENDED_STRUCT_MEMBER |
| 197 | data.ret = ret; |
| 198 | |
| 199 | events.perf_submit(ctx, &data, sizeof(data)); |
Mauricio Vásquez | 44e0f43 | 2020-05-21 11:50:52 -0500 | [diff] [blame^] | 200 | |
| 201 | return 0: |
Jiri Olsa | c347fe6 | 2019-12-22 15:52:39 +0100 | [diff] [blame] | 202 | } |
| 203 | """ |
| 204 | |
| 205 | is_support_kfunc = BPF.support_kfunc() |
| 206 | if is_support_kfunc: |
| 207 | bpf_text += bpf_text_kfunc |
| 208 | else: |
| 209 | bpf_text += bpf_text_kprobe |
| 210 | |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 211 | if args.tid: # TID trumps PID |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 212 | bpf_text = bpf_text.replace('PID_TID_FILTER', |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 213 | 'if (tid != %s) { return 0; }' % args.tid) |
| 214 | elif args.pid: |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 215 | bpf_text = bpf_text.replace('PID_TID_FILTER', |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 216 | 'if (pid != %s) { return 0; }' % args.pid) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 217 | else: |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 218 | bpf_text = bpf_text.replace('PID_TID_FILTER', '') |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 219 | if args.uid: |
| 220 | bpf_text = bpf_text.replace('UID_FILTER', |
| 221 | 'if (uid != %s) { return 0; }' % args.uid) |
| 222 | else: |
| 223 | bpf_text = bpf_text.replace('UID_FILTER', '') |
Alban Crequy | b2aa29f | 2019-12-16 10:54:18 +0100 | [diff] [blame] | 224 | if args.cgroupmap: |
| 225 | bpf_text = bpf_text.replace('CGROUPSET', '1') |
| 226 | bpf_text = bpf_text.replace('CGROUPPATH', args.cgroupmap) |
| 227 | else: |
| 228 | bpf_text = bpf_text.replace('CGROUPSET', '0') |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 229 | if args.flag_filter: |
| 230 | bpf_text = bpf_text.replace('FLAGS_FILTER', |
| 231 | 'if (!(flags & %d)) { return 0; }' % flag_filter_mask) |
| 232 | else: |
| 233 | bpf_text = bpf_text.replace('FLAGS_FILTER', '') |
| 234 | if not (args.extended_fields or args.flag_filter): |
| 235 | bpf_text = '\n'.join(x for x in bpf_text.split('\n') |
| 236 | if 'EXTENDED_STRUCT_MEMBER' not in x) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 237 | if debug or args.ebpf: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 238 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 239 | if args.ebpf: |
| 240 | exit() |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 241 | |
| 242 | # initialize BPF |
| 243 | b = BPF(text=bpf_text) |
Jiri Olsa | c347fe6 | 2019-12-22 15:52:39 +0100 | [diff] [blame] | 244 | if not is_support_kfunc: |
| 245 | b.attach_kprobe(event="do_sys_open", fn_name="trace_entry") |
| 246 | b.attach_kretprobe(event="do_sys_open", fn_name="trace_return") |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 247 | |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 248 | initial_ts = 0 |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 249 | |
| 250 | # header |
| 251 | if args.timestamp: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 252 | print("%-14s" % ("TIME(s)"), end="") |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 253 | if args.print_uid: |
| 254 | print("%-6s" % ("UID"), end="") |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 255 | print("%-6s %-16s %4s %3s " % |
| 256 | ("TID" if args.tid else "PID", "COMM", "FD", "ERR"), end="") |
| 257 | if args.extended_fields: |
| 258 | print("%-9s" % ("FLAGS"), end="") |
| 259 | print("PATH") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 260 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 261 | # process event |
| 262 | def print_event(cpu, data, size): |
Xiaozhou Liu | 51d62d3 | 2019-02-15 13:03:05 +0800 | [diff] [blame] | 263 | event = b["events"].event(data) |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 264 | global initial_ts |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 265 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 266 | # split return value into FD and errno columns |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 267 | if event.ret >= 0: |
| 268 | fd_s = event.ret |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 269 | err = 0 |
| 270 | else: |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 271 | fd_s = -1 |
| 272 | err = - event.ret |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 273 | |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 274 | if not initial_ts: |
| 275 | initial_ts = event.ts |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 276 | |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 277 | if args.failed and (event.ret >= 0): |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 278 | return |
| 279 | |
Gary Lin | 40fd669 | 2018-02-12 16:51:14 +0800 | [diff] [blame] | 280 | if args.name and bytes(args.name) not in event.comm: |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 281 | return |
| 282 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 283 | if args.timestamp: |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 284 | delta = event.ts - initial_ts |
Gary Lin | 6c79331 | 2019-04-18 15:17:56 +0800 | [diff] [blame] | 285 | printb(b"%-14.9f" % (float(delta) / 1000000), nl="") |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 286 | |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 287 | if args.print_uid: |
Gary Lin | 6c79331 | 2019-04-18 15:17:56 +0800 | [diff] [blame] | 288 | printb(b"%-6d" % event.uid, nl="") |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 289 | |
Gary Lin | 6c79331 | 2019-04-18 15:17:56 +0800 | [diff] [blame] | 290 | printb(b"%-6d %-16s %4d %3d " % |
| 291 | (event.id & 0xffffffff if args.tid else event.id >> 32, |
| 292 | event.comm, fd_s, err), nl="") |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 293 | |
| 294 | if args.extended_fields: |
Gary Lin | 6c79331 | 2019-04-18 15:17:56 +0800 | [diff] [blame] | 295 | printb(b"%08o " % event.flags, nl="") |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 296 | |
Yonghong Song | ebe1951 | 2019-01-10 14:54:16 -0800 | [diff] [blame] | 297 | printb(b'%s' % event.fname) |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 298 | |
| 299 | # loop with callback to print_event |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 300 | b["events"].open_perf_buffer(print_event, page_cnt=64) |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 301 | start_time = datetime.now() |
| 302 | while not args.duration or datetime.now() - start_time < args.duration: |
Jerome Marchand | 5167127 | 2018-12-19 01:57:24 +0100 | [diff] [blame] | 303 | try: |
| 304 | b.perf_buffer_poll() |
| 305 | except KeyboardInterrupt: |
| 306 | exit() |