blob: 5df3b417894c802a645ca14f2bc6f5451271209e [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#
7# USAGE: opensnoop [-h] [-t] [-x] [-p PID]
8#
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.
Brendan Greggbedd1502015-09-17 21:52:52 -070013
14from __future__ import print_function
15from bcc import BPF
16import argparse
17
18# arguments
19examples = """examples:
20 ./opensnoop # trace all open() syscalls
21 ./opensnoop -t # include timestamps
22 ./opensnoop -x # only show failed opens
23 ./opensnoop -p 181 # only trace PID 181
24"""
25parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080026 description="Trace open() syscalls",
27 formatter_class=argparse.RawDescriptionHelpFormatter,
28 epilog=examples)
Brendan Greggbedd1502015-09-17 21:52:52 -070029parser.add_argument("-t", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080030 help="include timestamp on output")
Brendan Greggbedd1502015-09-17 21:52:52 -070031parser.add_argument("-x", "--failed", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080032 help="only show failed opens")
Brendan Greggbedd1502015-09-17 21:52:52 -070033parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080034 help="trace this PID only")
Brendan Greggbedd1502015-09-17 21:52:52 -070035args = parser.parse_args()
36debug = 0
37
38# define BPF program
39bpf_text = """
40#include <uapi/linux/ptrace.h>
41
42BPF_HASH(args_filename, u32, const char *);
43
44int kprobe__sys_open(struct pt_regs *ctx, const char __user *filename)
45{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080046 u32 pid = bpf_get_current_pid_tgid();
Brendan Greggbedd1502015-09-17 21:52:52 -070047
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080048 FILTER
49 args_filename.update(&pid, &filename);
Brendan Greggbedd1502015-09-17 21:52:52 -070050
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080051 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -070052};
53
54int kretprobe__sys_open(struct pt_regs *ctx)
55{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080056 const char **filenamep;
57 int ret = ctx->ax;
58 u32 pid = bpf_get_current_pid_tgid();
Brendan Greggbedd1502015-09-17 21:52:52 -070059
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080060 filenamep = args_filename.lookup(&pid);
61 if (filenamep == 0) {
62 // missed entry
63 return 0;
64 }
Brendan Greggbedd1502015-09-17 21:52:52 -070065
Brendan Greggee31f612016-02-10 22:32:03 -080066 bpf_trace_printk("%d %s\\n", ret, *filenamep);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080067 args_filename.delete(&pid);
Brendan Greggbedd1502015-09-17 21:52:52 -070068
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080069 return 0;
Brendan Greggbedd1502015-09-17 21:52:52 -070070}
71"""
72if args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080073 bpf_text = bpf_text.replace('FILTER',
74 'if (pid != %s) { return 0; }' % args.pid)
Brendan Greggbedd1502015-09-17 21:52:52 -070075else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080076 bpf_text = bpf_text.replace('FILTER', '')
Brendan Greggbedd1502015-09-17 21:52:52 -070077if debug:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080078 print(bpf_text)
Brendan Greggbedd1502015-09-17 21:52:52 -070079
80# initialize BPF
81b = BPF(text=bpf_text)
82
83# header
84if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080085 print("%-14s" % ("TIME(s)"), end="")
Brendan Greggbedd1502015-09-17 21:52:52 -070086print("%-6s %-16s %4s %3s %s" % ("PID", "COMM", "FD", "ERR", "PATH"))
87
88start_ts = 0
89
90# format output
91while 1:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080092 (task, pid, cpu, flags, ts, msg) = b.trace_fields()
Brendan Greggee31f612016-02-10 22:32:03 -080093 (ret_s, filename) = msg.split(" ", 1)
Brendan Greggbedd1502015-09-17 21:52:52 -070094
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080095 ret = int(ret_s)
96 if (args.failed and (ret >= 0)):
97 continue
Brendan Greggbedd1502015-09-17 21:52:52 -070098
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080099 # split return value into FD and errno columns
100 if ret >= 0:
101 fd_s = ret
102 err = 0
103 else:
104 fd_s = "-1"
105 err = - ret
Brendan Greggbedd1502015-09-17 21:52:52 -0700106
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800107 # print columns
108 if args.timestamp:
109 if start_ts == 0:
110 start_ts = ts
111 print("%-14.9f" % (ts - start_ts), end="")
112 print("%-6d %-16s %4s %3s %s" % (pid, task, fd_s, err, filename))