Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # opensnoop Trace open() syscalls. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 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 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 12 | # 17-Sep-2015 Brendan Gregg Created this. |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 13 | |
| 14 | from __future__ import print_function |
| 15 | from bcc import BPF |
| 16 | import argparse |
| 17 | |
| 18 | # arguments |
| 19 | examples = """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 | """ |
| 25 | parser = argparse.ArgumentParser( |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 26 | description="Trace open() syscalls", |
| 27 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 28 | epilog=examples) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 29 | parser.add_argument("-t", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 30 | help="include timestamp on output") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 31 | parser.add_argument("-x", "--failed", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 32 | help="only show failed opens") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 33 | parser.add_argument("-p", "--pid", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 34 | help="trace this PID only") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 35 | args = parser.parse_args() |
| 36 | debug = 0 |
| 37 | |
| 38 | # define BPF program |
| 39 | bpf_text = """ |
| 40 | #include <uapi/linux/ptrace.h> |
| 41 | |
| 42 | BPF_HASH(args_filename, u32, const char *); |
| 43 | |
| 44 | int kprobe__sys_open(struct pt_regs *ctx, const char __user *filename) |
| 45 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 46 | u32 pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 47 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 48 | FILTER |
| 49 | args_filename.update(&pid, &filename); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 50 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 51 | return 0; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | int kretprobe__sys_open(struct pt_regs *ctx) |
| 55 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 56 | const char **filenamep; |
| 57 | int ret = ctx->ax; |
| 58 | u32 pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 59 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 60 | filenamep = args_filename.lookup(&pid); |
| 61 | if (filenamep == 0) { |
| 62 | // missed entry |
| 63 | return 0; |
| 64 | } |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 65 | |
Brendan Gregg | ee31f61 | 2016-02-10 22:32:03 -0800 | [diff] [blame] | 66 | bpf_trace_printk("%d %s\\n", ret, *filenamep); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 67 | args_filename.delete(&pid); |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 68 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 69 | return 0; |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 70 | } |
| 71 | """ |
| 72 | if args.pid: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 73 | bpf_text = bpf_text.replace('FILTER', |
| 74 | 'if (pid != %s) { return 0; }' % args.pid) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 75 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 76 | bpf_text = bpf_text.replace('FILTER', '') |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 77 | if debug: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 78 | print(bpf_text) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 79 | |
| 80 | # initialize BPF |
| 81 | b = BPF(text=bpf_text) |
| 82 | |
| 83 | # header |
| 84 | if args.timestamp: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 85 | print("%-14s" % ("TIME(s)"), end="") |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 86 | print("%-6s %-16s %4s %3s %s" % ("PID", "COMM", "FD", "ERR", "PATH")) |
| 87 | |
| 88 | start_ts = 0 |
| 89 | |
| 90 | # format output |
| 91 | while 1: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 92 | (task, pid, cpu, flags, ts, msg) = b.trace_fields() |
Brendan Gregg | ee31f61 | 2016-02-10 22:32:03 -0800 | [diff] [blame] | 93 | (ret_s, filename) = msg.split(" ", 1) |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 94 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 95 | ret = int(ret_s) |
| 96 | if (args.failed and (ret >= 0)): |
| 97 | continue |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 98 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 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 |
Brendan Gregg | bedd150 | 2015-09-17 21:52:52 -0700 | [diff] [blame] | 106 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 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)) |