blob: 4cb4dbb9d8904ddd1127913272f8e4db01c3d1cd [file] [log] [blame]
Alexey Ivanov777e8022019-01-03 13:46:38 -08001#!/usr/bin/env 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
mcaleavya3c446c72016-04-29 13:38:51 +010022import ctypes as ct
Paul Chaignon702de382018-01-28 13:41:35 +010023from datetime import datetime, timedelta
Tim Douglasd3583a82018-12-30 13:18:54 -050024import os
Brendan Greggbedd1502015-09-17 21:52:52 -070025
26# arguments
27examples = """examples:
28 ./opensnoop # trace all open() syscalls
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030029 ./opensnoop -T # include timestamps
takumakumef8990372019-01-02 17:12:14 +090030 ./opensnoop -U # include UID
Brendan Greggbedd1502015-09-17 21:52:52 -070031 ./opensnoop -x # only show failed opens
32 ./opensnoop -p 181 # only trace PID 181
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030033 ./opensnoop -t 123 # only trace TID 123
takumakumef8990372019-01-02 17:12:14 +090034 ./opensnoop -u 1000 # only trace UID 1000
Paul Chaignon702de382018-01-28 13:41:35 +010035 ./opensnoop -d 10 # trace for 10 seconds only
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020036 ./opensnoop -n main # only print process names containing "main"
Tim Douglasd3583a82018-12-30 13:18:54 -050037 ./opensnoop -e # show extended fields
38 ./opensnoop -f O_WRONLY -f O_RDWR # only print calls for writing
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")
takumakumef8990372019-01-02 17:12:14 +090054parser.add_argument("-u", "--uid",
55 help="trace this UID only")
Paul Chaignon702de382018-01-28 13:41:35 +010056parser.add_argument("-d", "--duration",
57 help="total duration of trace in seconds")
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020058parser.add_argument("-n", "--name",
Gary Lin40fd6692018-02-12 16:51:14 +080059 type=ArgString,
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020060 help="only print process names containing this name")
Nathan Scottcf0792f2018-02-02 16:56:50 +110061parser.add_argument("--ebpf", action="store_true",
62 help=argparse.SUPPRESS)
Tim Douglasd3583a82018-12-30 13:18:54 -050063parser.add_argument("-e", "--extended_fields", action="store_true",
64 help="show extended fields")
65parser.add_argument("-f", "--flag_filter", action="append",
66 help="filter on flags argument (e.g., O_WRONLY)")
Brendan Greggbedd1502015-09-17 21:52:52 -070067args = parser.parse_args()
68debug = 0
Paul Chaignon702de382018-01-28 13:41:35 +010069if args.duration:
70 args.duration = timedelta(seconds=int(args.duration))
Tim Douglasd3583a82018-12-30 13:18:54 -050071flag_filter_mask = 0
72for 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 Greggbedd1502015-09-17 21:52:52 -070079
80# define BPF program
81bpf_text = """
82#include <uapi/linux/ptrace.h>
mcaleavya3c446c72016-04-29 13:38:51 +010083#include <uapi/linux/limits.h>
84#include <linux/sched.h>
85
86struct val_t {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030087 u64 id;
mcaleavya3c446c72016-04-29 13:38:51 +010088 char comm[TASK_COMM_LEN];
89 const char *fname;
Tim Douglasd3583a82018-12-30 13:18:54 -050090 int flags; // EXTENDED_STRUCT_MEMBER
mcaleavya3c446c72016-04-29 13:38:51 +010091};
92
93struct data_t {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030094 u64 id;
mcaleavya3c446c72016-04-29 13:38:51 +010095 u64 ts;
takumakumef8990372019-01-02 17:12:14 +090096 u32 uid;
mcaleavya3c446c72016-04-29 13:38:51 +010097 int ret;
98 char comm[TASK_COMM_LEN];
99 char fname[NAME_MAX];
Tim Douglasd3583a82018-12-30 13:18:54 -0500100 int flags; // EXTENDED_STRUCT_MEMBER
mcaleavya3c446c72016-04-29 13:38:51 +0100101};
Brendan Greggbedd1502015-09-17 21:52:52 -0700102
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300103BPF_HASH(infotmp, u64, struct val_t);
mcaleavya3c446c72016-04-29 13:38:51 +0100104BPF_PERF_OUTPUT(events);
Brendan Greggbedd1502015-09-17 21:52:52 -0700105
Tim Douglasd3583a82018-12-30 13:18:54 -0500106int trace_entry(struct pt_regs *ctx, int dfd, const char __user *filename, int flags)
Brendan Greggbedd1502015-09-17 21:52:52 -0700107{
mcaleavya3c446c72016-04-29 13:38:51 +0100108 struct val_t val = {};
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300109 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
takumakumef8990372019-01-02 17:12:14 +0900112 u32 uid = bpf_get_current_uid_gid();
Brendan Greggbedd1502015-09-17 21:52:52 -0700113
Tim Douglasd3583a82018-12-30 13:18:54 -0500114 PID_TID_FILTER
takumakumef8990372019-01-02 17:12:14 +0900115 UID_FILTER
Tim Douglasd3583a82018-12-30 13:18:54 -0500116 FLAGS_FILTER
mcaleavya3c446c72016-04-29 13:38:51 +0100117 if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300118 val.id = id;
mcaleavya3c446c72016-04-29 13:38:51 +0100119 val.fname = filename;
Tim Douglasd3583a82018-12-30 13:18:54 -0500120 val.flags = flags; // EXTENDED_STRUCT_MEMBER
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300121 infotmp.update(&id, &val);
mcaleavya3c446c72016-04-29 13:38:51 +0100122 }
Brendan Greggbedd1502015-09-17 21:52:52 -0700123
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800124 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -0700125};
126
mcaleavya3c446c72016-04-29 13:38:51 +0100127int trace_return(struct pt_regs *ctx)
Brendan Greggbedd1502015-09-17 21:52:52 -0700128{
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300129 u64 id = bpf_get_current_pid_tgid();
mcaleavya3c446c72016-04-29 13:38:51 +0100130 struct val_t *valp;
131 struct data_t data = {};
Brendan Greggbedd1502015-09-17 21:52:52 -0700132
mcaleavya3c446c72016-04-29 13:38:51 +0100133 u64 tsp = bpf_ktime_get_ns();
134
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300135 valp = infotmp.lookup(&id);
mcaleavya3c446c72016-04-29 13:38:51 +0100136 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800137 // missed entry
138 return 0;
139 }
mcaleavya3c446c72016-04-29 13:38:51 +0100140 bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
141 bpf_probe_read(&data.fname, sizeof(data.fname), (void *)valp->fname);
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300142 data.id = valp->id;
mcaleavya3c446c72016-04-29 13:38:51 +0100143 data.ts = tsp / 1000;
takumakumef8990372019-01-02 17:12:14 +0900144 data.uid = bpf_get_current_uid_gid();
Tim Douglasd3583a82018-12-30 13:18:54 -0500145 data.flags = valp->flags; // EXTENDED_STRUCT_MEMBER
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530146 data.ret = PT_REGS_RC(ctx);
Brendan Greggbedd1502015-09-17 21:52:52 -0700147
mcaleavya3c446c72016-04-29 13:38:51 +0100148 events.perf_submit(ctx, &data, sizeof(data));
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300149 infotmp.delete(&id);
Brendan Greggbedd1502015-09-17 21:52:52 -0700150
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800151 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -0700152}
153"""
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300154if args.tid: # TID trumps PID
Tim Douglasd3583a82018-12-30 13:18:54 -0500155 bpf_text = bpf_text.replace('PID_TID_FILTER',
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300156 'if (tid != %s) { return 0; }' % args.tid)
157elif args.pid:
Tim Douglasd3583a82018-12-30 13:18:54 -0500158 bpf_text = bpf_text.replace('PID_TID_FILTER',
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800159 'if (pid != %s) { return 0; }' % args.pid)
Brendan Greggbedd1502015-09-17 21:52:52 -0700160else:
Tim Douglasd3583a82018-12-30 13:18:54 -0500161 bpf_text = bpf_text.replace('PID_TID_FILTER', '')
takumakumef8990372019-01-02 17:12:14 +0900162if args.uid:
163 bpf_text = bpf_text.replace('UID_FILTER',
164 'if (uid != %s) { return 0; }' % args.uid)
165else:
166 bpf_text = bpf_text.replace('UID_FILTER', '')
Tim Douglasd3583a82018-12-30 13:18:54 -0500167if args.flag_filter:
168 bpf_text = bpf_text.replace('FLAGS_FILTER',
169 'if (!(flags & %d)) { return 0; }' % flag_filter_mask)
170else:
171 bpf_text = bpf_text.replace('FLAGS_FILTER', '')
172if 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 Scottcf0792f2018-02-02 16:56:50 +1100175if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800176 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100177 if args.ebpf:
178 exit()
Brendan Greggbedd1502015-09-17 21:52:52 -0700179
180# initialize BPF
181b = BPF(text=bpf_text)
Joel Fernandes9af548f2018-01-07 11:59:09 -0800182b.attach_kprobe(event="do_sys_open", fn_name="trace_entry")
183b.attach_kretprobe(event="do_sys_open", fn_name="trace_return")
mcaleavya3c446c72016-04-29 13:38:51 +0100184
185TASK_COMM_LEN = 16 # linux/sched.h
186NAME_MAX = 255 # linux/limits.h
187
188class Data(ct.Structure):
189 _fields_ = [
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300190 ("id", ct.c_ulonglong),
mcaleavya3c446c72016-04-29 13:38:51 +0100191 ("ts", ct.c_ulonglong),
takumakumef8990372019-01-02 17:12:14 +0900192 ("uid", ct.c_uint32),
mcaleavya3c446c72016-04-29 13:38:51 +0100193 ("ret", ct.c_int),
194 ("comm", ct.c_char * TASK_COMM_LEN),
Tim Douglasd3583a82018-12-30 13:18:54 -0500195 ("fname", ct.c_char * NAME_MAX),
196 ("flags", ct.c_int),
mcaleavya3c446c72016-04-29 13:38:51 +0100197 ]
198
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200199initial_ts = 0
Brendan Greggbedd1502015-09-17 21:52:52 -0700200
201# header
202if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800203 print("%-14s" % ("TIME(s)"), end="")
takumakumef8990372019-01-02 17:12:14 +0900204if args.print_uid:
205 print("%-6s" % ("UID"), end="")
Tim Douglasd3583a82018-12-30 13:18:54 -0500206print("%-6s %-16s %4s %3s " %
207 ("TID" if args.tid else "PID", "COMM", "FD", "ERR"), end="")
208if args.extended_fields:
209 print("%-9s" % ("FLAGS"), end="")
210print("PATH")
Brendan Greggbedd1502015-09-17 21:52:52 -0700211
mcaleavya3c446c72016-04-29 13:38:51 +0100212# process event
213def print_event(cpu, data, size):
214 event = ct.cast(data, ct.POINTER(Data)).contents
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200215 global initial_ts
Brendan Greggbedd1502015-09-17 21:52:52 -0700216
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800217 # split return value into FD and errno columns
mcaleavya3c446c72016-04-29 13:38:51 +0100218 if event.ret >= 0:
219 fd_s = event.ret
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800220 err = 0
221 else:
mcaleavya3c446c72016-04-29 13:38:51 +0100222 fd_s = -1
223 err = - event.ret
Brendan Greggbedd1502015-09-17 21:52:52 -0700224
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200225 if not initial_ts:
226 initial_ts = event.ts
mcaleavya3c446c72016-04-29 13:38:51 +0100227
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200228 if args.failed and (event.ret >= 0):
mcaleavya3c446c72016-04-29 13:38:51 +0100229 return
230
Gary Lin40fd6692018-02-12 16:51:14 +0800231 if args.name and bytes(args.name) not in event.comm:
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +0200232 return
233
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800234 if args.timestamp:
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200235 delta = event.ts - initial_ts
236 print("%-14.9f" % (float(delta) / 1000000), end="")
mcaleavya3c446c72016-04-29 13:38:51 +0100237
takumakumef8990372019-01-02 17:12:14 +0900238 if args.print_uid:
239 print("%-6d" % event.uid, end="")
240
Tim Douglasd3583a82018-12-30 13:18:54 -0500241 print("%-6d %-16s %4d %3d " %
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300242 (event.id & 0xffffffff if args.tid else event.id >> 32,
Tim Douglasd3583a82018-12-30 13:18:54 -0500243 event.comm.decode('utf-8', 'replace'), fd_s, err), end="")
244
245 if args.extended_fields:
246 print("%08o " % event.flags, end="")
247
Yonghong Songebe19512019-01-10 14:54:16 -0800248 printb(b'%s' % event.fname)
mcaleavya3c446c72016-04-29 13:38:51 +0100249
250# loop with callback to print_event
Mark Drayton5f5687e2017-02-20 18:13:03 +0000251b["events"].open_perf_buffer(print_event, page_cnt=64)
Paul Chaignon702de382018-01-28 13:41:35 +0100252start_time = datetime.now()
253while not args.duration or datetime.now() - start_time < args.duration:
Jerome Marchand51671272018-12-19 01:57:24 +0100254 try:
255 b.perf_buffer_poll()
256 except KeyboardInterrupt:
257 exit()