Skip to content

Commit 99a3bc8

Browse files
dinazil4ast
authored andcommitted
Update opensnoop to filter by PID and TID (iovisor#739)
* Use real PID instead of TID in opensnoop * Replaced -t for timestamp with -T * Support TID as well as PID * Update opensnoop example * Update man * Added missing documentation re -n option * Minor: styling
1 parent 6802f31 commit 99a3bc8

File tree

3 files changed

+51
-32
lines changed

3 files changed

+51
-32
lines changed

man/man8/opensnoop.8

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.SH NAME
33
opensnoop \- Trace open() syscalls. Uses Linux eBPF/bcc.
44
.SH SYNOPSIS
5-
.B opensnoop [\-h] [\-t] [\-x] [\-p PID] [\-n name]
5+
.B opensnoop [\-h] [\-T] [\-x] [\-p PID] [\-t TID] [\-n name]
66
.SH DESCRIPTION
77
opensnoop traces the open() syscall, showing which processes are attempting
88
to open which files. This can be useful for determining the location of config
@@ -24,7 +24,7 @@ CONFIG_BPF and bcc.
2424
\-h
2525
Print usage message.
2626
.TP
27-
\-t
27+
\-T
2828
Include a timestamp column.
2929
.TP
3030
\-x
@@ -33,6 +33,9 @@ Only print failed opens.
3333
\-p PID
3434
Trace this process ID only (filtered in-kernel).
3535
.TP
36+
\-t TID
37+
Trace this thread ID only (filtered in-kernel).
38+
.TP
3639
\-n name
3740
Only print processes where its name partially matches 'name'
3841
.SH EXAMPLES
@@ -43,7 +46,7 @@ Trace all open() syscalls:
4346
.TP
4447
Trace all open() syscalls, and include timestamps:
4548
#
46-
.B opensnoop \-t
49+
.B opensnoop \-T
4750
.TP
4851
Trace only open() syscalls that failed:
4952
#
@@ -64,6 +67,9 @@ Time of the call, in seconds.
6467
PID
6568
Process ID
6669
.TP
70+
TID
71+
Thread ID
72+
.TP
6773
COMM
6874
Process name
6975
.TP

tools/opensnoop.py

+30-21
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
# opensnoop Trace open() syscalls.
55
# For Linux, uses BCC, eBPF. Embedded C.
66
#
7-
# USAGE: opensnoop [-h] [-t] [-x] [-p PID]
7+
# USAGE: opensnoop [-h] [-T] [-x] [-p PID] [-t TID] [-n NAME]
88
#
99
# Copyright (c) 2015 Brendan Gregg.
1010
# Licensed under the Apache License, Version 2.0 (the "License")
1111
#
1212
# 17-Sep-2015 Brendan Gregg Created this.
13-
# 29-Apr-2016 Allan McAleavy updated for BPF_PERF_OUTPUT
13+
# 29-Apr-2016 Allan McAleavy Updated for BPF_PERF_OUTPUT.
14+
# 08-Oct-2016 Dina Goldshtein Support filtering by PID and TID.
1415

1516
from __future__ import print_function
1617
from bcc import BPF
@@ -20,21 +21,24 @@
2021
# arguments
2122
examples = """examples:
2223
./opensnoop # trace all open() syscalls
23-
./opensnoop -t # include timestamps
24+
./opensnoop -T # include timestamps
2425
./opensnoop -x # only show failed opens
2526
./opensnoop -p 181 # only trace PID 181
27+
./opensnoop -t 123 # only trace TID 123
2628
./opensnoop -n main # only print process names containing "main"
2729
"""
2830
parser = argparse.ArgumentParser(
2931
description="Trace open() syscalls",
3032
formatter_class=argparse.RawDescriptionHelpFormatter,
3133
epilog=examples)
32-
parser.add_argument("-t", "--timestamp", action="store_true",
34+
parser.add_argument("-T", "--timestamp", action="store_true",
3335
help="include timestamp on output")
3436
parser.add_argument("-x", "--failed", action="store_true",
3537
help="only show failed opens")
3638
parser.add_argument("-p", "--pid",
3739
help="trace this PID only")
40+
parser.add_argument("-t", "--tid",
41+
help="trace this TID only")
3842
parser.add_argument("-n", "--name",
3943
help="only print process names containing this name")
4044
args = parser.parse_args()
@@ -47,67 +51,70 @@
4751
#include <linux/sched.h>
4852
4953
struct val_t {
50-
u32 pid;
54+
u64 id;
5155
u64 ts;
5256
char comm[TASK_COMM_LEN];
5357
const char *fname;
5458
};
5559
5660
struct data_t {
57-
u32 pid;
61+
u64 id;
5862
u64 ts;
5963
int ret;
6064
char comm[TASK_COMM_LEN];
6165
char fname[NAME_MAX];
6266
};
6367
64-
BPF_HASH(args_filename, u32, const char *);
65-
BPF_HASH(infotmp, u32, struct val_t);
68+
BPF_HASH(infotmp, u64, struct val_t);
6669
BPF_PERF_OUTPUT(events);
6770
6871
int trace_entry(struct pt_regs *ctx, const char __user *filename)
6972
{
7073
struct val_t val = {};
71-
u32 pid = bpf_get_current_pid_tgid();
74+
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
7277
7378
FILTER
7479
if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
75-
val.pid = bpf_get_current_pid_tgid();
80+
val.id = id;
7681
val.ts = bpf_ktime_get_ns();
7782
val.fname = filename;
78-
infotmp.update(&pid, &val);
83+
infotmp.update(&id, &val);
7984
}
8085
8186
return 0;
8287
};
8388
8489
int trace_return(struct pt_regs *ctx)
8590
{
86-
u32 pid = bpf_get_current_pid_tgid();
91+
u64 id = bpf_get_current_pid_tgid();
8792
struct val_t *valp;
8893
struct data_t data = {};
8994
9095
u64 tsp = bpf_ktime_get_ns();
9196
92-
valp = infotmp.lookup(&pid);
97+
valp = infotmp.lookup(&id);
9398
if (valp == 0) {
9499
// missed entry
95100
return 0;
96101
}
97102
bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
98103
bpf_probe_read(&data.fname, sizeof(data.fname), (void *)valp->fname);
99-
data.pid = valp->pid;
104+
data.id = valp->id;
100105
data.ts = tsp / 1000;
101106
data.ret = PT_REGS_RC(ctx);
102107
103108
events.perf_submit(ctx, &data, sizeof(data));
104-
infotmp.delete(&pid);
105-
args_filename.delete(&pid);
109+
infotmp.delete(&id);
106110
107111
return 0;
108112
}
109113
"""
110-
if args.pid:
114+
if args.tid: # TID trumps PID
115+
bpf_text = bpf_text.replace('FILTER',
116+
'if (tid != %s) { return 0; }' % args.tid)
117+
elif args.pid:
111118
bpf_text = bpf_text.replace('FILTER',
112119
'if (pid != %s) { return 0; }' % args.pid)
113120
else:
@@ -125,7 +132,7 @@
125132

126133
class Data(ct.Structure):
127134
_fields_ = [
128-
("pid", ct.c_ulonglong),
135+
("id", ct.c_ulonglong),
129136
("ts", ct.c_ulonglong),
130137
("ret", ct.c_int),
131138
("comm", ct.c_char * TASK_COMM_LEN),
@@ -137,7 +144,8 @@ class Data(ct.Structure):
137144
# header
138145
if args.timestamp:
139146
print("%-14s" % ("TIME(s)"), end="")
140-
print("%-6s %-16s %4s %3s %s" % ("PID", "COMM", "FD", "ERR", "PATH"))
147+
print("%-6s %-16s %4s %3s %s" %
148+
("TID" if args.tid else "PID", "COMM", "FD", "ERR", "PATH"))
141149

142150
# process event
143151
def print_event(cpu, data, size):
@@ -165,8 +173,9 @@ def print_event(cpu, data, size):
165173
delta = event.ts - initial_ts
166174
print("%-14.9f" % (float(delta) / 1000000), end="")
167175

168-
print("%-6d %-16s %4d %3d %s" % (event.pid, event.comm,
169-
fd_s, err, event.fname))
176+
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))
170179

171180
# loop with callback to print_event
172181
b["events"].open_perf_buffer(print_event)

tools/opensnoop_example.txt

+12-8
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ during application startup.
4848

4949

5050
The -p option can be used to filter on a PID, which is filtered in-kernel. Here
51-
I've used it with -t to print timestamps:
51+
I've used it with -T to print timestamps:
5252

53-
./opensnoop -tp 1956
53+
./opensnoop -Tp 1956
5454
TIME(s) PID COMM FD ERR PATH
5555
0.000000000 1956 supervise 9 0 supervise/status.new
5656
0.000289999 1956 supervise 9 0 supervise/status.new
@@ -123,18 +123,22 @@ to the '-n' option.
123123
USAGE message:
124124

125125
# ./opensnoop -h
126-
usage: opensnoop [-h] [-t] [-x] [-p PID]
126+
usage: opensnoop [-h] [-T] [-x] [-p PID] [-t TID] [-n NAME]
127127

128128
Trace open() syscalls
129129

130130
optional arguments:
131-
-h, --help show this help message and exit
132-
-t, --timestamp include timestamp on output
133-
-x, --failed only show failed opens
134-
-p PID, --pid PID trace this PID only
131+
-h, --help show this help message and exit
132+
-T, --timestamp include timestamp on output
133+
-x, --failed only show failed opens
134+
-p PID, --pid PID trace this PID only
135+
-t TID, --tid TID trace this TID only
136+
-n NAME, --name NAME only print process names containing this name
135137

136138
examples:
137139
./opensnoop # trace all open() syscalls
138-
./opensnoop -t # include timestamps
140+
./opensnoop -T # include timestamps
139141
./opensnoop -x # only show failed opens
140142
./opensnoop -p 181 # only trace PID 181
143+
./opensnoop -t 123 # only trace TID 123
144+
./opensnoop -n main # only print process names containing "main"

0 commit comments

Comments
 (0)