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