blob: 0c2b9b524228395281fe75cdbb205e011a2cb5ea [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#
Dina Goldshtein99a3bc82016-10-10 21:37:36 +03007# USAGE: opensnoop [-h] [-T] [-x] [-p PID] [-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
17from bcc import BPF
18import argparse
mcaleavya3c446c72016-04-29 13:38:51 +010019import ctypes as ct
Brendan Greggbedd1502015-09-17 21:52:52 -070020
21# arguments
22examples = """examples:
23 ./opensnoop # trace all open() syscalls
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030024 ./opensnoop -T # include timestamps
Brendan Greggbedd1502015-09-17 21:52:52 -070025 ./opensnoop -x # only show failed opens
26 ./opensnoop -p 181 # only trace PID 181
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030027 ./opensnoop -t 123 # only trace TID 123
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020028 ./opensnoop -n main # only print process names containing "main"
Brendan Greggbedd1502015-09-17 21:52:52 -070029"""
30parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080031 description="Trace open() syscalls",
32 formatter_class=argparse.RawDescriptionHelpFormatter,
33 epilog=examples)
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030034parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080035 help="include timestamp on output")
Brendan Greggbedd1502015-09-17 21:52:52 -070036parser.add_argument("-x", "--failed", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080037 help="only show failed opens")
Brendan Greggbedd1502015-09-17 21:52:52 -070038parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080039 help="trace this PID only")
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030040parser.add_argument("-t", "--tid",
41 help="trace this TID only")
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +020042parser.add_argument("-n", "--name",
43 help="only print process names containing this name")
Brendan Greggbedd1502015-09-17 21:52:52 -070044args = parser.parse_args()
45debug = 0
46
47# define BPF program
48bpf_text = """
49#include <uapi/linux/ptrace.h>
mcaleavya3c446c72016-04-29 13:38:51 +010050#include <uapi/linux/limits.h>
51#include <linux/sched.h>
52
53struct val_t {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030054 u64 id;
mcaleavya3c446c72016-04-29 13:38:51 +010055 u64 ts;
56 char comm[TASK_COMM_LEN];
57 const char *fname;
58};
59
60struct data_t {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030061 u64 id;
mcaleavya3c446c72016-04-29 13:38:51 +010062 u64 ts;
mcaleavya3c446c72016-04-29 13:38:51 +010063 int ret;
64 char comm[TASK_COMM_LEN];
65 char fname[NAME_MAX];
66};
Brendan Greggbedd1502015-09-17 21:52:52 -070067
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030068BPF_HASH(infotmp, u64, struct val_t);
mcaleavya3c446c72016-04-29 13:38:51 +010069BPF_PERF_OUTPUT(events);
Brendan Greggbedd1502015-09-17 21:52:52 -070070
mcaleavya3c446c72016-04-29 13:38:51 +010071int trace_entry(struct pt_regs *ctx, const char __user *filename)
Brendan Greggbedd1502015-09-17 21:52:52 -070072{
mcaleavya3c446c72016-04-29 13:38:51 +010073 struct val_t val = {};
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030074 u64 id = bpf_get_current_pid_tgid();
75 u32 pid = id >> 32; // PID is higher part
76 u32 tid = id; // Cast and get the lower part
Brendan Greggbedd1502015-09-17 21:52:52 -070077
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080078 FILTER
mcaleavya3c446c72016-04-29 13:38:51 +010079 if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030080 val.id = id;
mcaleavya3c446c72016-04-29 13:38:51 +010081 val.ts = bpf_ktime_get_ns();
82 val.fname = filename;
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030083 infotmp.update(&id, &val);
mcaleavya3c446c72016-04-29 13:38:51 +010084 }
Brendan Greggbedd1502015-09-17 21:52:52 -070085
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080086 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -070087};
88
mcaleavya3c446c72016-04-29 13:38:51 +010089int trace_return(struct pt_regs *ctx)
Brendan Greggbedd1502015-09-17 21:52:52 -070090{
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030091 u64 id = bpf_get_current_pid_tgid();
mcaleavya3c446c72016-04-29 13:38:51 +010092 struct val_t *valp;
93 struct data_t data = {};
Brendan Greggbedd1502015-09-17 21:52:52 -070094
mcaleavya3c446c72016-04-29 13:38:51 +010095 u64 tsp = bpf_ktime_get_ns();
96
Dina Goldshtein99a3bc82016-10-10 21:37:36 +030097 valp = infotmp.lookup(&id);
mcaleavya3c446c72016-04-29 13:38:51 +010098 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080099 // missed entry
100 return 0;
101 }
mcaleavya3c446c72016-04-29 13:38:51 +0100102 bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
103 bpf_probe_read(&data.fname, sizeof(data.fname), (void *)valp->fname);
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300104 data.id = valp->id;
mcaleavya3c446c72016-04-29 13:38:51 +0100105 data.ts = tsp / 1000;
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530106 data.ret = PT_REGS_RC(ctx);
Brendan Greggbedd1502015-09-17 21:52:52 -0700107
mcaleavya3c446c72016-04-29 13:38:51 +0100108 events.perf_submit(ctx, &data, sizeof(data));
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300109 infotmp.delete(&id);
Brendan Greggbedd1502015-09-17 21:52:52 -0700110
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800111 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -0700112}
113"""
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300114if args.tid: # TID trumps PID
115 bpf_text = bpf_text.replace('FILTER',
116 'if (tid != %s) { return 0; }' % args.tid)
117elif args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800118 bpf_text = bpf_text.replace('FILTER',
119 'if (pid != %s) { return 0; }' % args.pid)
Brendan Greggbedd1502015-09-17 21:52:52 -0700120else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800121 bpf_text = bpf_text.replace('FILTER', '')
Brendan Greggbedd1502015-09-17 21:52:52 -0700122if debug:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800123 print(bpf_text)
Brendan Greggbedd1502015-09-17 21:52:52 -0700124
125# initialize BPF
126b = BPF(text=bpf_text)
mcaleavya3c446c72016-04-29 13:38:51 +0100127b.attach_kprobe(event="sys_open", fn_name="trace_entry")
128b.attach_kretprobe(event="sys_open", fn_name="trace_return")
129
130TASK_COMM_LEN = 16 # linux/sched.h
131NAME_MAX = 255 # linux/limits.h
132
133class Data(ct.Structure):
134 _fields_ = [
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300135 ("id", ct.c_ulonglong),
mcaleavya3c446c72016-04-29 13:38:51 +0100136 ("ts", ct.c_ulonglong),
mcaleavya3c446c72016-04-29 13:38:51 +0100137 ("ret", ct.c_int),
138 ("comm", ct.c_char * TASK_COMM_LEN),
139 ("fname", ct.c_char * NAME_MAX)
140 ]
141
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200142initial_ts = 0
Brendan Greggbedd1502015-09-17 21:52:52 -0700143
144# header
145if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800146 print("%-14s" % ("TIME(s)"), end="")
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300147print("%-6s %-16s %4s %3s %s" %
148 ("TID" if args.tid else "PID", "COMM", "FD", "ERR", "PATH"))
Brendan Greggbedd1502015-09-17 21:52:52 -0700149
mcaleavya3c446c72016-04-29 13:38:51 +0100150# process event
151def print_event(cpu, data, size):
152 event = ct.cast(data, ct.POINTER(Data)).contents
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200153 global initial_ts
Brendan Greggbedd1502015-09-17 21:52:52 -0700154
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800155 # split return value into FD and errno columns
mcaleavya3c446c72016-04-29 13:38:51 +0100156 if event.ret >= 0:
157 fd_s = event.ret
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800158 err = 0
159 else:
mcaleavya3c446c72016-04-29 13:38:51 +0100160 fd_s = -1
161 err = - event.ret
Brendan Greggbedd1502015-09-17 21:52:52 -0700162
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200163 if not initial_ts:
164 initial_ts = event.ts
mcaleavya3c446c72016-04-29 13:38:51 +0100165
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200166 if args.failed and (event.ret >= 0):
mcaleavya3c446c72016-04-29 13:38:51 +0100167 return
168
KarimAllah Ahmed765dfe22016-09-10 12:01:07 +0200169 if args.name and args.name not in event.comm:
170 return
171
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800172 if args.timestamp:
KarimAllah Ahmeda17d1e82016-09-10 12:00:32 +0200173 delta = event.ts - initial_ts
174 print("%-14.9f" % (float(delta) / 1000000), end="")
mcaleavya3c446c72016-04-29 13:38:51 +0100175
Dina Goldshtein99a3bc82016-10-10 21:37:36 +0300176 print("%-6d %-16s %4d %3d %s" %
177 (event.id & 0xffffffff if args.tid else event.id >> 32,
178 event.comm, fd_s, err, event.fname))
mcaleavya3c446c72016-04-29 13:38:51 +0100179
180# loop with callback to print_event
181b["events"].open_perf_buffer(print_event)
182while 1:
183 b.kprobe_poll()