• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# @lint-avoid-python-3-compatibility-imports
3#
4# opensnoop Trace open() syscalls.
5#           For Linux, uses BCC, eBPF. Embedded C.
6#
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#
12# 17-Sep-2015   Brendan Gregg   Created this.
13
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(
26    description="Trace open() syscalls",
27    formatter_class=argparse.RawDescriptionHelpFormatter,
28    epilog=examples)
29parser.add_argument("-t", "--timestamp", action="store_true",
30    help="include timestamp on output")
31parser.add_argument("-x", "--failed", action="store_true",
32    help="only show failed opens")
33parser.add_argument("-p", "--pid",
34    help="trace this PID only")
35args = 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{
46    u32 pid = bpf_get_current_pid_tgid();
47
48    FILTER
49    args_filename.update(&pid, &filename);
50
51    return 0;
52};
53
54int kretprobe__sys_open(struct pt_regs *ctx)
55{
56    const char **filenamep;
57    int ret = ctx->ax;
58    u32 pid = bpf_get_current_pid_tgid();
59
60    filenamep = args_filename.lookup(&pid);
61    if (filenamep == 0) {
62        // missed entry
63        return 0;
64    }
65
66    bpf_trace_printk("%d %s\\n", ret, *filenamep);
67    args_filename.delete(&pid);
68
69    return 0;
70}
71"""
72if args.pid:
73    bpf_text = bpf_text.replace('FILTER',
74        'if (pid != %s) { return 0; }' % args.pid)
75else:
76    bpf_text = bpf_text.replace('FILTER', '')
77if debug:
78    print(bpf_text)
79
80# initialize BPF
81b = BPF(text=bpf_text)
82
83# header
84if args.timestamp:
85    print("%-14s" % ("TIME(s)"), end="")
86print("%-6s %-16s %4s %3s %s" % ("PID", "COMM", "FD", "ERR", "PATH"))
87
88start_ts = 0
89
90# format output
91while 1:
92    (task, pid, cpu, flags, ts, msg) = b.trace_fields()
93    (ret_s, filename) = msg.split(" ", 1)
94
95    ret = int(ret_s)
96    if (args.failed and (ret >= 0)):
97        continue
98
99    # 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
106
107    # 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))
113