blob: 6d1388b3c1f40ca14ff7bdef3c78ef401651fd78 [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Greggbedd1502015-09-17 21:52:52 -07003#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08004# opensnoop Trace open() syscalls.
5# For Linux, uses BCC, eBPF. Embedded C.
Brendan Greggbedd1502015-09-17 21:52:52 -07006#
Paul Chaignon702de382018-01-28 13:41:35 +01007# USAGE: opensnoop [-h] [-T] [-x] [-p PID] [-d DURATION] [-t TID] [-n NAME]
Brendan Greggbedd1502015-09-17 21:52:52 -07008#
9# Copyright (c) 2015 Brendan Gregg.
10# Licensed under the Apache License, Version 2.0 (the "License")
11#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080012# 17-Sep-2015 Brendan Gregg Created this.
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030013# 29-Apr-2016 Allan McAleavy Updated for BPF_PERF_OUTPUT.
14# 08-Oct-2016 Dina Goldshtein Support filtering by PID and TID.
Tim Douglasd3583a82018-12-30 13:18:54 -050015# 28-Dec-2018 Tim Douglas Print flags argument, enable filtering
takumakumef8990372019-01-02 17:12:14 +090016# 06-Jan-2019 Takuma Kume Support filtering by UID
Brendan Greggbedd1502015-09-17 21:52:52 -070017
18from __future__ import print_function
Gary Lin40fd6692018-02-12 16:51:14 +080019from bcc import ArgString, BPF
japrocaed9b1e2019-01-04 20:21:46 +030020from bcc.utils import printb
Brendan Greggbedd1502015-09-17 21:52:52 -070021import argparse
Paul Chaignon702de382018-01-28 13:41:35 +010022from datetime import datetime, timedelta
Tim Douglasd3583a82018-12-30 13:18:54 -050023import os
Brendan Greggbedd1502015-09-17 21:52:52 -070024
25# arguments
26examples = """examples:
27 ./opensnoop # trace all open() syscalls
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030028 ./opensnoop -T # include timestamps
takumakumef8990372019-01-02 17:12:14 +090029 ./opensnoop -U # include UID
Brendan Greggbedd1502015-09-17 21:52:52 -070030 ./opensnoop -x # only show failed opens
31 ./opensnoop -p 181 # only trace PID 181
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030032 ./opensnoop -t 123 # only trace TID 123
takumakumef8990372019-01-02 17:12:14 +090033 ./opensnoop -u 1000 # only trace UID 1000
Paul Chaignon702de382018-01-28 13:41:35 +010034 ./opensnoop -d 10 # trace for 10 seconds only
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020035 ./opensnoop -n main # only print process names containing "main"
Tim Douglasd3583a82018-12-30 13:18:54 -050036 ./opensnoop -e # show extended fields
37 ./opensnoop -f O_WRONLY -f O_RDWR # only print calls for writing
Alban Crequyb2aa29f2019-12-16 10:54:18 +010038 ./opensnoop --cgroupmap ./mappath # only trace cgroups in this BPF map
Brendan Greggbedd1502015-09-17 21:52:52 -070039"""
40parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080041 description="Trace open() syscalls",
42 formatter_class=argparse.RawDescriptionHelpFormatter,
43 epilog=examples)
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030044parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080045 help="include timestamp on output")
takumakumef8990372019-01-02 17:12:14 +090046parser.add_argument("-U", "--print-uid", action="store_true",
47 help="print UID column")
Brendan Greggbedd1502015-09-17 21:52:52 -070048parser.add_argument("-x", "--failed", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080049 help="only show failed opens")
Brendan Greggbedd1502015-09-17 21:52:52 -070050parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080051 help="trace this PID only")
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030052parser.add_argument("-t", "--tid",
53 help="trace this TID only")
Alban Crequyb2aa29f2019-12-16 10:54:18 +010054parser.add_argument("--cgroupmap",
55 help="trace cgroups in this BPF map only")
takumakumef8990372019-01-02 17:12:14 +090056parser.add_argument("-u", "--uid",
57 help="trace this UID only")
Paul Chaignon702de382018-01-28 13:41:35 +010058parser.add_argument("-d", "--duration",
59 help="total duration of trace in seconds")
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020060parser.add_argument("-n", "--name",
Gary Lin40fd6692018-02-12 16:51:14 +080061 type=ArgString,
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020062 help="only print process names containing this name")
Nathan Scottcf0792f2018-02-02 16:56:50 +110063parser.add_argument("--ebpf", action="store_true",
64 help=argparse.SUPPRESS)
Tim Douglasd3583a82018-12-30 13:18:54 -050065parser.add_argument("-e", "--extended_fields", action="store_true",
66 help="show extended fields")
67parser.add_argument("-f", "--flag_filter", action="append",
68 help="filter on flags argument (e.g., O_WRONLY)")
Brendan Greggbedd1502015-09-17 21:52:52 -070069args = parser.parse_args()
70debug = 0
Paul Chaignon702de382018-01-28 13:41:35 +010071if args.duration:
72 args.duration = timedelta(seconds=int(args.duration))
Tim Douglasd3583a82018-12-30 13:18:54 -050073flag_filter_mask = 0
74for 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 Greggbedd1502015-09-17 21:52:52 -070081
82# define BPF program
83bpf_text = """
84#include <uapi/linux/ptrace.h>
mcaleavya3c446c72016-04-29 13:38:51 +010085#include <uapi/linux/limits.h>
86#include <linux/sched.h>
87
88struct val_t {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030089 u64 id;
mcaleavya3c446c72016-04-29 13:38:51 +010090 char comm[TASK_COMM_LEN];
91 const char *fname;
Tim Douglasd3583a82018-12-30 13:18:54 -050092 int flags; // EXTENDED_STRUCT_MEMBER
mcaleavya3c446c72016-04-29 13:38:51 +010093};
94
95struct data_t {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030096 u64 id;
mcaleavya3c446c72016-04-29 13:38:51 +010097 u64 ts;
takumakumef8990372019-01-02 17:12:14 +090098 u32 uid;
mcaleavya3c446c72016-04-29 13:38:51 +010099 int ret;
100 char comm[TASK_COMM_LEN];
101 char fname[NAME_MAX];
Tim Douglasd3583a82018-12-30 13:18:54 -0500102 int flags; // EXTENDED_STRUCT_MEMBER
mcaleavya3c446c72016-04-29 13:38:51 +0100103};
Brendan Greggbedd1502015-09-17 21:52:52 -0700104
Alban Crequyb2aa29f2019-12-16 10:54:18 +0100105#if CGROUPSET
106BPF_TABLE_PINNED("hash", u64, u64, cgroupset, 1024, "CGROUPPATH");
107#endif
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300108BPF_HASH(infotmp, u64, struct val_t);
mcaleavya3c446c72016-04-29 13:38:51 +0100109BPF_PERF_OUTPUT(events);
Brendan Greggbedd1502015-09-17 21:52:52 -0700110
Tim Douglasd3583a82018-12-30 13:18:54 -0500111int trace_entry(struct pt_regs *ctx, int dfd, const char __user *filename, int flags)
Brendan Greggbedd1502015-09-17 21:52:52 -0700112{
mcaleavya3c446c72016-04-29 13:38:51 +0100113 struct val_t val = {};
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300114 u64 id = bpf_get_current_pid_tgid();
115 u32 pid = id >> 32; // PID is higher part
116 u32 tid = id; // Cast and get the lower part
takumakumef8990372019-01-02 17:12:14 +0900117 u32 uid = bpf_get_current_uid_gid();
Brendan Greggbedd1502015-09-17 21:52:52 -0700118
Tim Douglasd3583a82018-12-30 13:18:54 -0500119 PID_TID_FILTER
takumakumef8990372019-01-02 17:12:14 +0900120 UID_FILTER
Tim Douglasd3583a82018-12-30 13:18:54 -0500121 FLAGS_FILTER
Alban Crequyb2aa29f2019-12-16 10:54:18 +0100122#if CGROUPSET
123 u64 cgroupid = bpf_get_current_cgroup_id();
124 if (cgroupset.lookup(&cgroupid) == NULL) {
125 return 0;
126 }
127#endif
mcaleavya3c446c72016-04-29 13:38:51 +0100128 if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300129 val.id = id;
mcaleavya3c446c72016-04-29 13:38:51 +0100130 val.fname = filename;
Tim Douglasd3583a82018-12-30 13:18:54 -0500131 val.flags = flags; // EXTENDED_STRUCT_MEMBER
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300132 infotmp.update(&id, &val);
mcaleavya3c446c72016-04-29 13:38:51 +0100133 }
Brendan Greggbedd1502015-09-17 21:52:52 -0700134
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800135 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -0700136};
137
mcaleavya3c446c72016-04-29 13:38:51 +0100138int trace_return(struct pt_regs *ctx)
Brendan Greggbedd1502015-09-17 21:52:52 -0700139{
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300140 u64 id = bpf_get_current_pid_tgid();
mcaleavya3c446c72016-04-29 13:38:51 +0100141 struct val_t *valp;
142 struct data_t data = {};
Brendan Greggbedd1502015-09-17 21:52:52 -0700143
mcaleavya3c446c72016-04-29 13:38:51 +0100144 u64 tsp = bpf_ktime_get_ns();
145
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300146 valp = infotmp.lookup(&id);
mcaleavya3c446c72016-04-29 13:38:51 +0100147 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800148 // missed entry
149 return 0;
150 }
mcaleavya3c446c72016-04-29 13:38:51 +0100151 bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
152 bpf_probe_read(&data.fname, sizeof(data.fname), (void *)valp->fname);
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300153 data.id = valp->id;
mcaleavya3c446c72016-04-29 13:38:51 +0100154 data.ts = tsp / 1000;
takumakumef8990372019-01-02 17:12:14 +0900155 data.uid = bpf_get_current_uid_gid();
Tim Douglasd3583a82018-12-30 13:18:54 -0500156 data.flags = valp->flags; // EXTENDED_STRUCT_MEMBER
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530157 data.ret = PT_REGS_RC(ctx);
Brendan Greggbedd1502015-09-17 21:52:52 -0700158
mcaleavya3c446c72016-04-29 13:38:51 +0100159 events.perf_submit(ctx, &data, sizeof(data));
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300160 infotmp.delete(&id);
Brendan Greggbedd1502015-09-17 21:52:52 -0700161
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800162 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -0700163}
164"""
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300165if args.tid: # TID trumps PID
Tim Douglasd3583a82018-12-30 13:18:54 -0500166 bpf_text = bpf_text.replace('PID_TID_FILTER',
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300167 'if (tid != %s) { return 0; }' % args.tid)
168elif args.pid:
Tim Douglasd3583a82018-12-30 13:18:54 -0500169 bpf_text = bpf_text.replace('PID_TID_FILTER',
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800170 'if (pid != %s) { return 0; }' % args.pid)
Brendan Greggbedd1502015-09-17 21:52:52 -0700171else:
Tim Douglasd3583a82018-12-30 13:18:54 -0500172 bpf_text = bpf_text.replace('PID_TID_FILTER', '')
takumakumef8990372019-01-02 17:12:14 +0900173if args.uid:
174 bpf_text = bpf_text.replace('UID_FILTER',
175 'if (uid != %s) { return 0; }' % args.uid)
176else:
177 bpf_text = bpf_text.replace('UID_FILTER', '')
Alban Crequyb2aa29f2019-12-16 10:54:18 +0100178if args.cgroupmap:
179 bpf_text = bpf_text.replace('CGROUPSET', '1')
180 bpf_text = bpf_text.replace('CGROUPPATH', args.cgroupmap)
181else:
182 bpf_text = bpf_text.replace('CGROUPSET', '0')
Tim Douglasd3583a82018-12-30 13:18:54 -0500183if args.flag_filter:
184 bpf_text = bpf_text.replace('FLAGS_FILTER',
185 'if (!(flags & %d)) { return 0; }' % flag_filter_mask)
186else:
187 bpf_text = bpf_text.replace('FLAGS_FILTER', '')
188if not (args.extended_fields or args.flag_filter):
189 bpf_text = '\n'.join(x for x in bpf_text.split('\n')
190 if 'EXTENDED_STRUCT_MEMBER' not in x)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100191if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800192 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100193 if args.ebpf:
194 exit()
Brendan Greggbedd1502015-09-17 21:52:52 -0700195
196# initialize BPF
197b = BPF(text=bpf_text)
Joel Fernandes9af548f2018-01-07 11:59:09 -0800198b.attach_kprobe(event="do_sys_open", fn_name="trace_entry")
199b.attach_kretprobe(event="do_sys_open", fn_name="trace_return")
mcaleavya3c446c72016-04-29 13:38:51 +0100200
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200201initial_ts = 0
Brendan Greggbedd1502015-09-17 21:52:52 -0700202
203# header
204if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800205 print("%-14s" % ("TIME(s)"), end="")
takumakumef8990372019-01-02 17:12:14 +0900206if args.print_uid:
207 print("%-6s" % ("UID"), end="")
Tim Douglasd3583a82018-12-30 13:18:54 -0500208print("%-6s %-16s %4s %3s " %
209 ("TID" if args.tid else "PID", "COMM", "FD", "ERR"), end="")
210if args.extended_fields:
211 print("%-9s" % ("FLAGS"), end="")
212print("PATH")
Brendan Greggbedd1502015-09-17 21:52:52 -0700213
mcaleavya3c446c72016-04-29 13:38:51 +0100214# process event
215def print_event(cpu, data, size):
Xiaozhou Liu51d62d32019-02-15 13:03:05 +0800216 event = b["events"].event(data)
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200217 global initial_ts
Brendan Greggbedd1502015-09-17 21:52:52 -0700218
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800219 # split return value into FD and errno columns
mcaleavya3c446c72016-04-29 13:38:51 +0100220 if event.ret >= 0:
221 fd_s = event.ret
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800222 err = 0
223 else:
mcaleavya3c446c72016-04-29 13:38:51 +0100224 fd_s = -1
225 err = - event.ret
Brendan Greggbedd1502015-09-17 21:52:52 -0700226
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200227 if not initial_ts:
228 initial_ts = event.ts
mcaleavya3c446c72016-04-29 13:38:51 +0100229
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200230 if args.failed and (event.ret >= 0):
mcaleavya3c446c72016-04-29 13:38:51 +0100231 return
232
Gary Lin40fd6692018-02-12 16:51:14 +0800233 if args.name and bytes(args.name) not in event.comm:
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +0200234 return
235
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800236 if args.timestamp:
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200237 delta = event.ts - initial_ts
Gary Lin6c793312019-04-18 15:17:56 +0800238 printb(b"%-14.9f" % (float(delta) / 1000000), nl="")
mcaleavya3c446c72016-04-29 13:38:51 +0100239
takumakumef8990372019-01-02 17:12:14 +0900240 if args.print_uid:
Gary Lin6c793312019-04-18 15:17:56 +0800241 printb(b"%-6d" % event.uid, nl="")
takumakumef8990372019-01-02 17:12:14 +0900242
Gary Lin6c793312019-04-18 15:17:56 +0800243 printb(b"%-6d %-16s %4d %3d " %
244 (event.id & 0xffffffff if args.tid else event.id >> 32,
245 event.comm, fd_s, err), nl="")
Tim Douglasd3583a82018-12-30 13:18:54 -0500246
247 if args.extended_fields:
Gary Lin6c793312019-04-18 15:17:56 +0800248 printb(b"%08o " % event.flags, nl="")
Tim Douglasd3583a82018-12-30 13:18:54 -0500249
Yonghong Songebe19512019-01-10 14:54:16 -0800250 printb(b'%s' % event.fname)
mcaleavya3c446c72016-04-29 13:38:51 +0100251
252# loop with callback to print_event
Mark Drayton5f5687e2017-02-20 18:13:03 +0000253b["events"].open_perf_buffer(print_event, page_cnt=64)
Paul Chaignon702de382018-01-28 13:41:35 +0100254start_time = datetime.now()
255while not args.duration or datetime.now() - start_time < args.duration:
Jerome Marchand51671272018-12-19 01:57:24 +0100256 try:
257 b.perf_buffer_poll()
258 except KeyboardInterrupt:
259 exit()