Skip to content

Commit

Permalink
opensnoop: -d option for duration
Browse files Browse the repository at this point in the history
  • Loading branch information
pchaigno committed Jan 28, 2018
1 parent 44463d5 commit 702de38
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
9 changes: 8 additions & 1 deletion man/man8/opensnoop.8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.SH NAME
opensnoop \- Trace open() syscalls. Uses Linux eBPF/bcc.
.SH SYNOPSIS
.B opensnoop [\-h] [\-T] [\-x] [\-p PID] [\-t TID] [\-n name]
.B opensnoop [\-h] [\-T] [\-x] [\-p PID] [\-t TID] [\-d DURATION] [\-n name]
.SH DESCRIPTION
opensnoop traces the open() syscall, showing which processes are attempting
to open which files. This can be useful for determining the location of config
Expand Down Expand Up @@ -36,6 +36,9 @@ Trace this process ID only (filtered in-kernel).
\-t TID
Trace this thread ID only (filtered in-kernel).
.TP
\-d DURATION
Total duration of trace in seconds.
.TP
\-n name
Only print processes where its name partially matches 'name'
.SH EXAMPLES
Expand All @@ -44,6 +47,10 @@ Trace all open() syscalls:
#
.B opensnoop
.TP
Trace all open() syscalls, for 10 seconds only:
#
.B opensnoop -d 10
.TP
Trace all open() syscalls, and include timestamps:
#
.B opensnoop \-T
Expand Down
11 changes: 9 additions & 2 deletions tools/opensnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# opensnoop Trace open() syscalls.
# For Linux, uses BCC, eBPF. Embedded C.
#
# USAGE: opensnoop [-h] [-T] [-x] [-p PID] [-t TID] [-n NAME]
# USAGE: opensnoop [-h] [-T] [-x] [-p PID] [-d DURATION] [-t TID] [-n NAME]
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
Expand All @@ -17,6 +17,7 @@
from bcc import BPF
import argparse
import ctypes as ct
from datetime import datetime, timedelta

# arguments
examples = """examples:
Expand All @@ -25,6 +26,7 @@
./opensnoop -x # only show failed opens
./opensnoop -p 181 # only trace PID 181
./opensnoop -t 123 # only trace TID 123
./opensnoop -d 10 # trace for 10 seconds only
./opensnoop -n main # only print process names containing "main"
"""
parser = argparse.ArgumentParser(
Expand All @@ -39,10 +41,14 @@
help="trace this PID only")
parser.add_argument("-t", "--tid",
help="trace this TID only")
parser.add_argument("-d", "--duration",
help="total duration of trace in seconds")
parser.add_argument("-n", "--name",
help="only print process names containing this name")
args = parser.parse_args()
debug = 0
if args.duration:
args.duration = timedelta(seconds=int(args.duration))

# define BPF program
bpf_text = """
Expand Down Expand Up @@ -179,5 +185,6 @@ def print_event(cpu, data, size):

# loop with callback to print_event
b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1:
start_time = datetime.now()
while not args.duration or datetime.now() - start_time < args.duration:
b.kprobe_poll()
17 changes: 16 additions & 1 deletion tools/opensnoop_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ The ERR column is the system error number. Error number 2 is ENOENT: no such
file or directory.


A maximum tracing duration can be set with the -d option. For example, to trace
for 2 seconds:

# ./opensnoop -d 2
PID COMM FD ERR PATH
2191 indicator-multi 11 0 /sys/block
2191 indicator-multi 11 0 /sys/block
2191 indicator-multi 11 0 /sys/block
2191 indicator-multi 11 0 /sys/block
2191 indicator-multi 11 0 /sys/block


The -n option can be used to filter on process name using partial matches:

# ./opensnoop -n ed
Expand Down Expand Up @@ -123,7 +135,7 @@ to the '-n' option.
USAGE message:

# ./opensnoop -h
usage: opensnoop [-h] [-T] [-x] [-p PID] [-t TID] [-n NAME]
usage: opensnoop [-h] [-T] [-x] [-p PID] [-t TID] [-d DURATION] [-n NAME]

Trace open() syscalls

Expand All @@ -133,6 +145,8 @@ optional arguments:
-x, --failed only show failed opens
-p PID, --pid PID trace this PID only
-t TID, --tid TID trace this TID only
-d DURATION, --duration DURATION
total duration of trace in seconds
-n NAME, --name NAME only print process names containing this name

examples:
Expand All @@ -141,4 +155,5 @@ examples:
./opensnoop -x # only show failed opens
./opensnoop -p 181 # only trace PID 181
./opensnoop -t 123 # only trace TID 123
./opensnoop -d 10 # trace for 10 seconds only
./opensnoop -n main # only print process names containing "main"

0 comments on commit 702de38

Please sign in to comment.