blob: e1a010879213198c585c0a4d8e6a011e499ca49a [file] [log] [blame]
Brendan Greggbedd1502015-09-17 21:52:52 -07001#!/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.
Brendan Greggbedd1502015-09-17 21:52:52 -070015
16from __future__ import print_function
Gary Lin40fd6692018-02-12 16:51:14 +080017from bcc import ArgString, BPF
Brendan Greggbedd1502015-09-17 21:52:52 -070018import argparse
mcaleavya3c446c72016-04-29 13:38:51 +010019import ctypes as ct
Paul Chaignon702de382018-01-28 13:41:35 +010020from datetime import datetime, timedelta
Brendan Greggbedd1502015-09-17 21:52:52 -070021
22# arguments
23examples = """examples:
24 ./opensnoop # trace all open() syscalls
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030025 ./opensnoop -T # include timestamps
Brendan Greggbedd1502015-09-17 21:52:52 -070026 ./opensnoop -x # only show failed opens
27 ./opensnoop -p 181 # only trace PID 181
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030028 ./opensnoop -t 123 # only trace TID 123
Paul Chaignon702de382018-01-28 13:41:35 +010029 ./opensnoop -d 10 # trace for 10 seconds only
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020030 ./opensnoop -n main # only print process names containing "main"
Brendan Greggbedd1502015-09-17 21:52:52 -070031"""
32parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080033 description="Trace open() syscalls",
34 formatter_class=argparse.RawDescriptionHelpFormatter,
35 epilog=examples)
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030036parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080037 help="include timestamp on output")
Brendan Greggbedd1502015-09-17 21:52:52 -070038parser.add_argument("-x", "--failed", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080039 help="only show failed opens")
Brendan Greggbedd1502015-09-17 21:52:52 -070040parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080041 help="trace this PID only")
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030042parser.add_argument("-t", "--tid",
43 help="trace this TID only")
Paul Chaignon702de382018-01-28 13:41:35 +010044parser.add_argument("-d", "--duration",
45 help="total duration of trace in seconds")
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020046parser.add_argument("-n", "--name",
Gary Lin40fd6692018-02-12 16:51:14 +080047 type=ArgString,
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020048 help="only print process names containing this name")
Nathan Scottcf0792f2018-02-02 16:56:50 +110049parser.add_argument("--ebpf", action="store_true",
50 help=argparse.SUPPRESS)
Brendan Greggbedd1502015-09-17 21:52:52 -070051args = parser.parse_args()
52debug = 0
Paul Chaignon702de382018-01-28 13:41:35 +010053if args.duration:
54 args.duration = timedelta(seconds=int(args.duration))
Brendan Greggbedd1502015-09-17 21:52:52 -070055
56# define BPF program
57bpf_text = """
58#include <uapi/linux/ptrace.h>
mcaleavya3c446c72016-04-29 13:38:51 +010059#include <uapi/linux/limits.h>
60#include <linux/sched.h>
61
62struct val_t {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030063 u64 id;
mcaleavya3c446c72016-04-29 13:38:51 +010064 u64 ts;
65 char comm[TASK_COMM_LEN];
66 const char *fname;
67};
68
69struct data_t {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030070 u64 id;
mcaleavya3c446c72016-04-29 13:38:51 +010071 u64 ts;
mcaleavya3c446c72016-04-29 13:38:51 +010072 int ret;
73 char comm[TASK_COMM_LEN];
74 char fname[NAME_MAX];
75};
Brendan Greggbedd1502015-09-17 21:52:52 -070076
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030077BPF_HASH(infotmp, u64, struct val_t);
mcaleavya3c446c72016-04-29 13:38:51 +010078BPF_PERF_OUTPUT(events);
Brendan Greggbedd1502015-09-17 21:52:52 -070079
Joel Fernandes9af548f2018-01-07 11:59:09 -080080int trace_entry(struct pt_regs *ctx, int dfd, const char __user *filename)
Brendan Greggbedd1502015-09-17 21:52:52 -070081{
mcaleavya3c446c72016-04-29 13:38:51 +010082 struct val_t val = {};
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030083 u64 id = bpf_get_current_pid_tgid();
84 u32 pid = id >> 32; // PID is higher part
85 u32 tid = id; // Cast and get the lower part
Brendan Greggbedd1502015-09-17 21:52:52 -070086
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080087 FILTER
mcaleavya3c446c72016-04-29 13:38:51 +010088 if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030089 val.id = id;
mcaleavya3c446c72016-04-29 13:38:51 +010090 val.ts = bpf_ktime_get_ns();
91 val.fname = filename;
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030092 infotmp.update(&id, &val);
mcaleavya3c446c72016-04-29 13:38:51 +010093 }
Brendan Greggbedd1502015-09-17 21:52:52 -070094
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080095 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -070096};
97
mcaleavya3c446c72016-04-29 13:38:51 +010098int trace_return(struct pt_regs *ctx)
Brendan Greggbedd1502015-09-17 21:52:52 -070099{
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300100 u64 id = bpf_get_current_pid_tgid();
mcaleavya3c446c72016-04-29 13:38:51 +0100101 struct val_t *valp;
102 struct data_t data = {};
Brendan Greggbedd1502015-09-17 21:52:52 -0700103
mcaleavya3c446c72016-04-29 13:38:51 +0100104 u64 tsp = bpf_ktime_get_ns();
105
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300106 valp = infotmp.lookup(&id);
mcaleavya3c446c72016-04-29 13:38:51 +0100107 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800108 // missed entry
109 return 0;
110 }
mcaleavya3c446c72016-04-29 13:38:51 +0100111 bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
112 bpf_probe_read(&data.fname, sizeof(data.fname), (void *)valp->fname);
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300113 data.id = valp->id;
mcaleavya3c446c72016-04-29 13:38:51 +0100114 data.ts = tsp / 1000;
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530115 data.ret = PT_REGS_RC(ctx);
Brendan Greggbedd1502015-09-17 21:52:52 -0700116
mcaleavya3c446c72016-04-29 13:38:51 +0100117 events.perf_submit(ctx, &data, sizeof(data));
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300118 infotmp.delete(&id);
Brendan Greggbedd1502015-09-17 21:52:52 -0700119
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800120 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -0700121}
122"""
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300123if args.tid: # TID trumps PID
124 bpf_text = bpf_text.replace('FILTER',
125 'if (tid != %s) { return 0; }' % args.tid)
126elif args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800127 bpf_text = bpf_text.replace('FILTER',
128 'if (pid != %s) { return 0; }' % args.pid)
Brendan Greggbedd1502015-09-17 21:52:52 -0700129else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800130 bpf_text = bpf_text.replace('FILTER', '')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100131if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800132 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100133 if args.ebpf:
134 exit()
Brendan Greggbedd1502015-09-17 21:52:52 -0700135
136# initialize BPF
137b = BPF(text=bpf_text)
Joel Fernandes9af548f2018-01-07 11:59:09 -0800138b.attach_kprobe(event="do_sys_open", fn_name="trace_entry")
139b.attach_kretprobe(event="do_sys_open", fn_name="trace_return")
mcaleavya3c446c72016-04-29 13:38:51 +0100140
141TASK_COMM_LEN = 16 # linux/sched.h
142NAME_MAX = 255 # linux/limits.h
143
144class Data(ct.Structure):
145 _fields_ = [
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300146 ("id", ct.c_ulonglong),
mcaleavya3c446c72016-04-29 13:38:51 +0100147 ("ts", ct.c_ulonglong),
mcaleavya3c446c72016-04-29 13:38:51 +0100148 ("ret", ct.c_int),
149 ("comm", ct.c_char * TASK_COMM_LEN),
150 ("fname", ct.c_char * NAME_MAX)
151 ]
152
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200153initial_ts = 0
Brendan Greggbedd1502015-09-17 21:52:52 -0700154
155# header
156if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800157 print("%-14s" % ("TIME(s)"), end="")
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300158print("%-6s %-16s %4s %3s %s" %
159 ("TID" if args.tid else "PID", "COMM", "FD", "ERR", "PATH"))
Brendan Greggbedd1502015-09-17 21:52:52 -0700160
mcaleavya3c446c72016-04-29 13:38:51 +0100161# process event
162def print_event(cpu, data, size):
163 event = ct.cast(data, ct.POINTER(Data)).contents
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200164 global initial_ts
Brendan Greggbedd1502015-09-17 21:52:52 -0700165
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800166 # split return value into FD and errno columns
mcaleavya3c446c72016-04-29 13:38:51 +0100167 if event.ret >= 0:
168 fd_s = event.ret
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800169 err = 0
170 else:
mcaleavya3c446c72016-04-29 13:38:51 +0100171 fd_s = -1
172 err = - event.ret
Brendan Greggbedd1502015-09-17 21:52:52 -0700173
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200174 if not initial_ts:
175 initial_ts = event.ts
mcaleavya3c446c72016-04-29 13:38:51 +0100176
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200177 if args.failed and (event.ret >= 0):
mcaleavya3c446c72016-04-29 13:38:51 +0100178 return
179
Gary Lin40fd6692018-02-12 16:51:14 +0800180 if args.name and bytes(args.name) not in event.comm:
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +0200181 return
182
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800183 if args.timestamp:
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200184 delta = event.ts - initial_ts
185 print("%-14.9f" % (float(delta) / 1000000), end="")
mcaleavya3c446c72016-04-29 13:38:51 +0100186
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300187 print("%-6d %-16s %4d %3d %s" %
188 (event.id & 0xffffffff if args.tid else event.id >> 32,
Rafael F78948e42017-03-26 14:54:25 +0200189 event.comm.decode(), fd_s, err, event.fname.decode()))
mcaleavya3c446c72016-04-29 13:38:51 +0100190
191# loop with callback to print_event
Mark Drayton5f5687e2017-02-20 18:13:03 +0000192b["events"].open_perf_buffer(print_event, page_cnt=64)
Paul Chaignon702de382018-01-28 13:41:35 +0100193start_time = datetime.now()
194while not args.duration or datetime.now() - start_time < args.duration:
Teng Qindbf00292018-02-28 21:47:50 -0800195 b.perf_buffer_poll()