blob: 418d47bc6e9d9ccf5cfbb280a30bbca54e56ac71 [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 char comm[TASK_COMM_LEN];
65 const char *fname;
66};
67
68struct data_t {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030069 u64 id;
mcaleavya3c446c72016-04-29 13:38:51 +010070 u64 ts;
mcaleavya3c446c72016-04-29 13:38:51 +010071 int ret;
72 char comm[TASK_COMM_LEN];
73 char fname[NAME_MAX];
74};
Brendan Greggbedd1502015-09-17 21:52:52 -070075
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030076BPF_HASH(infotmp, u64, struct val_t);
mcaleavya3c446c72016-04-29 13:38:51 +010077BPF_PERF_OUTPUT(events);
Brendan Greggbedd1502015-09-17 21:52:52 -070078
Joel Fernandes9af548f2018-01-07 11:59:09 -080079int trace_entry(struct pt_regs *ctx, int dfd, const char __user *filename)
Brendan Greggbedd1502015-09-17 21:52:52 -070080{
mcaleavya3c446c72016-04-29 13:38:51 +010081 struct val_t val = {};
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030082 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
Brendan Greggbedd1502015-09-17 21:52:52 -070085
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080086 FILTER
mcaleavya3c446c72016-04-29 13:38:51 +010087 if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030088 val.id = id;
mcaleavya3c446c72016-04-29 13:38:51 +010089 val.fname = filename;
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030090 infotmp.update(&id, &val);
mcaleavya3c446c72016-04-29 13:38:51 +010091 }
Brendan Greggbedd1502015-09-17 21:52:52 -070092
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080093 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -070094};
95
mcaleavya3c446c72016-04-29 13:38:51 +010096int trace_return(struct pt_regs *ctx)
Brendan Greggbedd1502015-09-17 21:52:52 -070097{
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030098 u64 id = bpf_get_current_pid_tgid();
mcaleavya3c446c72016-04-29 13:38:51 +010099 struct val_t *valp;
100 struct data_t data = {};
Brendan Greggbedd1502015-09-17 21:52:52 -0700101
mcaleavya3c446c72016-04-29 13:38:51 +0100102 u64 tsp = bpf_ktime_get_ns();
103
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300104 valp = infotmp.lookup(&id);
mcaleavya3c446c72016-04-29 13:38:51 +0100105 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800106 // missed entry
107 return 0;
108 }
mcaleavya3c446c72016-04-29 13:38:51 +0100109 bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
110 bpf_probe_read(&data.fname, sizeof(data.fname), (void *)valp->fname);
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300111 data.id = valp->id;
mcaleavya3c446c72016-04-29 13:38:51 +0100112 data.ts = tsp / 1000;
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530113 data.ret = PT_REGS_RC(ctx);
Brendan Greggbedd1502015-09-17 21:52:52 -0700114
mcaleavya3c446c72016-04-29 13:38:51 +0100115 events.perf_submit(ctx, &data, sizeof(data));
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300116 infotmp.delete(&id);
Brendan Greggbedd1502015-09-17 21:52:52 -0700117
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800118 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -0700119}
120"""
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300121if args.tid: # TID trumps PID
122 bpf_text = bpf_text.replace('FILTER',
123 'if (tid != %s) { return 0; }' % args.tid)
124elif args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800125 bpf_text = bpf_text.replace('FILTER',
126 'if (pid != %s) { return 0; }' % args.pid)
Brendan Greggbedd1502015-09-17 21:52:52 -0700127else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800128 bpf_text = bpf_text.replace('FILTER', '')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100129if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800130 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100131 if args.ebpf:
132 exit()
Brendan Greggbedd1502015-09-17 21:52:52 -0700133
134# initialize BPF
135b = BPF(text=bpf_text)
Joel Fernandes9af548f2018-01-07 11:59:09 -0800136b.attach_kprobe(event="do_sys_open", fn_name="trace_entry")
137b.attach_kretprobe(event="do_sys_open", fn_name="trace_return")
mcaleavya3c446c72016-04-29 13:38:51 +0100138
139TASK_COMM_LEN = 16 # linux/sched.h
140NAME_MAX = 255 # linux/limits.h
141
142class Data(ct.Structure):
143 _fields_ = [
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300144 ("id", ct.c_ulonglong),
mcaleavya3c446c72016-04-29 13:38:51 +0100145 ("ts", ct.c_ulonglong),
mcaleavya3c446c72016-04-29 13:38:51 +0100146 ("ret", ct.c_int),
147 ("comm", ct.c_char * TASK_COMM_LEN),
148 ("fname", ct.c_char * NAME_MAX)
149 ]
150
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200151initial_ts = 0
Brendan Greggbedd1502015-09-17 21:52:52 -0700152
153# header
154if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800155 print("%-14s" % ("TIME(s)"), end="")
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300156print("%-6s %-16s %4s %3s %s" %
157 ("TID" if args.tid else "PID", "COMM", "FD", "ERR", "PATH"))
Brendan Greggbedd1502015-09-17 21:52:52 -0700158
mcaleavya3c446c72016-04-29 13:38:51 +0100159# process event
160def print_event(cpu, data, size):
161 event = ct.cast(data, ct.POINTER(Data)).contents
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200162 global initial_ts
Brendan Greggbedd1502015-09-17 21:52:52 -0700163
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800164 # split return value into FD and errno columns
mcaleavya3c446c72016-04-29 13:38:51 +0100165 if event.ret >= 0:
166 fd_s = event.ret
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800167 err = 0
168 else:
mcaleavya3c446c72016-04-29 13:38:51 +0100169 fd_s = -1
170 err = - event.ret
Brendan Greggbedd1502015-09-17 21:52:52 -0700171
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200172 if not initial_ts:
173 initial_ts = event.ts
mcaleavya3c446c72016-04-29 13:38:51 +0100174
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200175 if args.failed and (event.ret >= 0):
mcaleavya3c446c72016-04-29 13:38:51 +0100176 return
177
Gary Lin40fd6692018-02-12 16:51:14 +0800178 if args.name and bytes(args.name) not in event.comm:
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +0200179 return
180
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800181 if args.timestamp:
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200182 delta = event.ts - initial_ts
183 print("%-14.9f" % (float(delta) / 1000000), end="")
mcaleavya3c446c72016-04-29 13:38:51 +0100184
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300185 print("%-6d %-16s %4d %3d %s" %
186 (event.id & 0xffffffff if args.tid else event.id >> 32,
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200187 event.comm.decode('utf-8', 'replace'), fd_s, err,
188 event.fname.decode('utf-8', 'replace')))
mcaleavya3c446c72016-04-29 13:38:51 +0100189
190# loop with callback to print_event
Mark Drayton5f5687e2017-02-20 18:13:03 +0000191b["events"].open_perf_buffer(print_event, page_cnt=64)
Paul Chaignon702de382018-01-28 13:41:35 +0100192start_time = datetime.now()
193while not args.duration or datetime.now() - start_time < args.duration:
Teng Qindbf00292018-02-28 21:47:50 -0800194 b.perf_buffer_poll()