Alexey Ivanov | 777e802 | 2019-01-03 13:46:38 -0800 | [diff] [blame] | 1 | #!/usr/bin/env 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 |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 22 | import ctypes as ct |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 23 | from datetime import datetime, timedelta |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 24 | import os |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 25 | |
| 26 | # arguments |
| 27 | examples = """examples: |
| 28 | ./opensnoop # trace all open() syscalls |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 29 | ./opensnoop -T # include timestamps |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 30 | ./opensnoop -U # include UID |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 31 | ./opensnoop -x # only show failed opens |
| 32 | ./opensnoop -p 181 # only trace PID 181 |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 33 | ./opensnoop -t 123 # only trace TID 123 |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 34 | ./opensnoop -u 1000 # only trace UID 1000 |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 35 | ./opensnoop -d 10 # trace for 10 seconds only |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 36 | ./opensnoop -n main # only print process names containing "main" |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 37 | ./opensnoop -e # show extended fields |
| 38 | ./opensnoop -f O_WRONLY -f O_RDWR # only print calls for writing |
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") |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 54 | parser.add_argument("-u", "--uid", |
| 55 | help="trace this UID only") |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 56 | parser.add_argument("-d", "--duration", |
| 57 | help="total duration of trace in seconds") |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 58 | parser.add_argument("-n", "--name", |
Gary Lin | 40fd669 | 2018-02-12 16:51:14 +0800 | [diff] [blame] | 59 | type=ArgString, |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 60 | help="only print process names containing this name") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 61 | parser.add_argument("--ebpf", action="store_true", |
| 62 | help=argparse.SUPPRESS) |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 63 | parser.add_argument("-e", "--extended_fields", action="store_true", |
| 64 | help="show extended fields") |
| 65 | parser.add_argument("-f", "--flag_filter", action="append", |
| 66 | help="filter on flags argument (e.g., O_WRONLY)") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 67 | args = parser.parse_args() |
| 68 | debug = 0 |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 69 | if args.duration: |
| 70 | args.duration = timedelta(seconds=int(args.duration)) |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 71 | flag_filter_mask = 0 |
| 72 | for flag in args.flag_filter or []: |
| 73 | if not flag.startswith('O_'): |
| 74 | exit("Bad flag: %s" % flag) |
| 75 | try: |
| 76 | flag_filter_mask |= getattr(os, flag) |
| 77 | except AttributeError: |
| 78 | exit("Bad flag: %s" % flag) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 79 | |
| 80 | # define BPF program |
| 81 | bpf_text = """ |
| 82 | #include <uapi/linux/ptrace.h> |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 83 | #include <uapi/linux/limits.h> |
| 84 | #include <linux/sched.h> |
| 85 | |
| 86 | struct val_t { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 87 | u64 id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 88 | char comm[TASK_COMM_LEN]; |
| 89 | const char *fname; |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 90 | int flags; // EXTENDED_STRUCT_MEMBER |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct data_t { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 94 | u64 id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 95 | u64 ts; |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 96 | u32 uid; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 97 | int ret; |
| 98 | char comm[TASK_COMM_LEN]; |
| 99 | char fname[NAME_MAX]; |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 100 | int flags; // EXTENDED_STRUCT_MEMBER |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 101 | }; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 102 | |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 103 | BPF_HASH(infotmp, u64, struct val_t); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 104 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 105 | |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 106 | 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] | 107 | { |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 108 | struct val_t val = {}; |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 109 | u64 id = bpf_get_current_pid_tgid(); |
| 110 | u32 pid = id >> 32; // PID is higher part |
| 111 | u32 tid = id; // Cast and get the lower part |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 112 | u32 uid = bpf_get_current_uid_gid(); |
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 | PID_TID_FILTER |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 115 | UID_FILTER |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 116 | FLAGS_FILTER |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 117 | if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 118 | val.id = id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 119 | val.fname = filename; |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 120 | val.flags = flags; // EXTENDED_STRUCT_MEMBER |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 121 | infotmp.update(&id, &val); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 122 | } |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 123 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 124 | return 0; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 127 | int trace_return(struct pt_regs *ctx) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 128 | { |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 129 | u64 id = bpf_get_current_pid_tgid(); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 130 | struct val_t *valp; |
| 131 | struct data_t data = {}; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 132 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 133 | u64 tsp = bpf_ktime_get_ns(); |
| 134 | |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 135 | valp = infotmp.lookup(&id); |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 136 | if (valp == 0) { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 137 | // missed entry |
| 138 | return 0; |
| 139 | } |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 140 | bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm); |
| 141 | bpf_probe_read(&data.fname, sizeof(data.fname), (void *)valp->fname); |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 142 | data.id = valp->id; |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 143 | data.ts = tsp / 1000; |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 144 | data.uid = bpf_get_current_uid_gid(); |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 145 | data.flags = valp->flags; // EXTENDED_STRUCT_MEMBER |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 146 | data.ret = PT_REGS_RC(ctx); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 147 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 148 | events.perf_submit(ctx, &data, sizeof(data)); |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 149 | infotmp.delete(&id); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 150 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 151 | return 0; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 152 | } |
| 153 | """ |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 154 | if args.tid: # TID trumps PID |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 155 | bpf_text = bpf_text.replace('PID_TID_FILTER', |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 156 | 'if (tid != %s) { return 0; }' % args.tid) |
| 157 | elif args.pid: |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 158 | bpf_text = bpf_text.replace('PID_TID_FILTER', |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 159 | 'if (pid != %s) { return 0; }' % args.pid) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 160 | else: |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 161 | bpf_text = bpf_text.replace('PID_TID_FILTER', '') |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 162 | if args.uid: |
| 163 | bpf_text = bpf_text.replace('UID_FILTER', |
| 164 | 'if (uid != %s) { return 0; }' % args.uid) |
| 165 | else: |
| 166 | bpf_text = bpf_text.replace('UID_FILTER', '') |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 167 | if args.flag_filter: |
| 168 | bpf_text = bpf_text.replace('FLAGS_FILTER', |
| 169 | 'if (!(flags & %d)) { return 0; }' % flag_filter_mask) |
| 170 | else: |
| 171 | bpf_text = bpf_text.replace('FLAGS_FILTER', '') |
| 172 | if not (args.extended_fields or args.flag_filter): |
| 173 | bpf_text = '\n'.join(x for x in bpf_text.split('\n') |
| 174 | if 'EXTENDED_STRUCT_MEMBER' not in x) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 175 | if debug or args.ebpf: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 176 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 177 | if args.ebpf: |
| 178 | exit() |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 179 | |
| 180 | # initialize BPF |
| 181 | b = BPF(text=bpf_text) |
Joel Fernandes | 9af548f | 2018-01-07 11:59:09 -0800 | [diff] [blame] | 182 | b.attach_kprobe(event="do_sys_open", fn_name="trace_entry") |
| 183 | b.attach_kretprobe(event="do_sys_open", fn_name="trace_return") |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 184 | |
| 185 | TASK_COMM_LEN = 16 # linux/sched.h |
| 186 | NAME_MAX = 255 # linux/limits.h |
| 187 | |
| 188 | class Data(ct.Structure): |
| 189 | _fields_ = [ |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 190 | ("id", ct.c_ulonglong), |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 191 | ("ts", ct.c_ulonglong), |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 192 | ("uid", ct.c_uint32), |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 193 | ("ret", ct.c_int), |
| 194 | ("comm", ct.c_char * TASK_COMM_LEN), |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 195 | ("fname", ct.c_char * NAME_MAX), |
| 196 | ("flags", ct.c_int), |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 197 | ] |
| 198 | |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 199 | initial_ts = 0 |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 200 | |
| 201 | # header |
| 202 | if args.timestamp: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 203 | print("%-14s" % ("TIME(s)"), end="") |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 204 | if args.print_uid: |
| 205 | print("%-6s" % ("UID"), end="") |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 206 | print("%-6s %-16s %4s %3s " % |
| 207 | ("TID" if args.tid else "PID", "COMM", "FD", "ERR"), end="") |
| 208 | if args.extended_fields: |
| 209 | print("%-9s" % ("FLAGS"), end="") |
| 210 | print("PATH") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 211 | |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 212 | # process event |
| 213 | def print_event(cpu, data, size): |
| 214 | event = ct.cast(data, ct.POINTER(Data)).contents |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 215 | global initial_ts |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 216 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 217 | # split return value into FD and errno columns |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 218 | if event.ret >= 0: |
| 219 | fd_s = event.ret |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 220 | err = 0 |
| 221 | else: |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 222 | fd_s = -1 |
| 223 | err = - event.ret |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 224 | |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 225 | if not initial_ts: |
| 226 | initial_ts = event.ts |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 227 | |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 228 | if args.failed and (event.ret >= 0): |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 229 | return |
| 230 | |
Gary Lin | 40fd669 | 2018-02-12 16:51:14 +0800 | [diff] [blame] | 231 | if args.name and bytes(args.name) not in event.comm: |
KarimAllah Ahmed | 765dfe2 | 2016-09-10 12:01:07 +0200 | [diff] [blame] | 232 | return |
| 233 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 234 | if args.timestamp: |
KarimAllah Ahmed | a17d1e8 | 2016-09-10 12:00:32 +0200 | [diff] [blame] | 235 | delta = event.ts - initial_ts |
| 236 | print("%-14.9f" % (float(delta) / 1000000), end="") |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 237 | |
takumakume | f899037 | 2019-01-02 17:12:14 +0900 | [diff] [blame] | 238 | if args.print_uid: |
| 239 | print("%-6d" % event.uid, end="") |
| 240 | |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 241 | print("%-6d %-16s %4d %3d " % |
Dina Goldshtein | 99a3bc8 | 2016-10-10 21:37:36 +0300 | [diff] [blame] | 242 | (event.id & 0xffffffff if args.tid else event.id >> 32, |
Tim Douglas | d3583a8 | 2018-12-30 13:18:54 -0500 | [diff] [blame] | 243 | event.comm.decode('utf-8', 'replace'), fd_s, err), end="") |
| 244 | |
| 245 | if args.extended_fields: |
| 246 | print("%08o " % event.flags, end="") |
| 247 | |
Yonghong Song | ebe1951 | 2019-01-10 14:54:16 -0800 | [diff] [blame] | 248 | printb(b'%s' % event.fname) |
mcaleavya | 3c446c7 | 2016-04-29 13:38:51 +0100 | [diff] [blame] | 249 | |
| 250 | # loop with callback to print_event |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 251 | b["events"].open_perf_buffer(print_event, page_cnt=64) |
Paul Chaignon | 702de38 | 2018-01-28 13:41:35 +0100 | [diff] [blame] | 252 | start_time = datetime.now() |
| 253 | while not args.duration or datetime.now() - start_time < args.duration: |
Jerome Marchand | 5167127 | 2018-12-19 01:57:24 +0100 | [diff] [blame] | 254 | try: |
| 255 | b.perf_buffer_poll() |
| 256 | except KeyboardInterrupt: |
| 257 | exit() |