forked from bpftrace/bpftrace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopensnoop.bt
More file actions
160 lines (138 loc) · 4.19 KB
/
Copy pathopensnoop.bt
File metadata and controls
160 lines (138 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env bpftrace
// opensnoop Trace open() syscalls.
// For Linux, uses bpftrace and eBPF.
//
// USAGE: opensnoop.bt -- [--depth=<N>] [--errname]
//
// Example of usage:
//
// # ./opensnoop.bt
// Attached 8 probes
// Tracing open syscalls... Hit Ctrl-C to end.
// PID COMM FD ERR PATH
// 37421 bpftrace 33 0 /sys/kernel/debug/tracing/events/syscalls/sys_exit_openat2/id
// 3108 cgroupify 5 0 /Git/bpftrace/bpftrace/rongtao/.
// 1400 systemd-oomd 13 0 /sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/app.slice/app-gnome\x2dsession\x2dmanager.slice/memory.pressure
// 3108 cgroupify 5 0 /Git/bpftrace/bpftrace/rongtao/3122/cgroup.procs
// 1615 abrt-dump-journ 4 0 /var/log/journal/c27675f9250c4420a7479dd37bd7e8e2/system.journal
// 1614 abrt-dump-journ 4 0 /var/log/journal/c27675f9250c4420a7479dd37bd7e8e2/system.journal
// 1613 abrt-dump-journ 4 0 /var/log/journal/c27675f9250c4420a7479dd37bd7e8e2/system.journal
// 3108 cgroupify 5 0 /Git/bpftrace/bpftrace/rongtao/3150/cgroup.procs
// 3108 cgroupify 5 0 /Git/bpftrace/bpftrace/rongtao/17355/cgroup.procs
// 2440 snmp-pass 4 0 /proc/cpuinfo
// 2440 snmp-pass 4 0 /proc/stat
//
// While tracing, at "ls" command was launched: the libraries it uses can be seen
// as they were opened.
//
// opensnoop can be useful for discovering configuration and log files, if used
// during application startup.
//
// This is a bpftrace version of the bcc tool of the same name.
// The bcc version provides command line options to customize the output.
//
// Copyright 2018 Netflix, Inc.
//
// 08-Sep-2018 Brendan Gregg Created this.
// 16-Sep-2025 Rong Tao Support fullpath of file.
config = {
missing_probes = warn;
}
macro max_path_depth()
{
getopt("depth", 35, "Maximum depth of full path")
}
macro errname()
{
getopt("errname", false, "Show error message instead of errno")
}
BEGIN
{
printf("Tracing open syscalls... Hit Ctrl-C to end.\n");
printf("%-6s %-16s %4s", "PID", "COMM", "FD");
if errname {
printf(" %16s", "ERR");
} else {
printf(" %4s", "ERR");
}
printf(" %s\n", "PATH");
}
tracepoint:syscalls:sys_enter_open,
tracepoint:syscalls:sys_enter_openat,
tracepoint:syscalls:sys_enter_openat2
{
@filename[tid] = args.filename;
}
macro getcwd(@paths)
{
$dentry = curtask.fs.pwd.dentry;
$vfsmnt = curtask.fs.pwd.mnt;
$mnt = (struct mount *)(((uint64)$vfsmnt) - offsetof(struct mount, mnt));
$mnt_root = $vfsmnt.mnt_root;
for $j : ((uint64)0)..((uint64)max_path_depth) {
@paths[tid,$j] = str($dentry.d_name.name);
$parent_dentry = $dentry.d_parent;
if ($dentry == $parent_dentry || $dentry == $mnt_root) {
$mnt_parent = (struct mount *)$mnt.mnt_parent;
// Real root directory
if ($mnt == $mnt_parent) {
break;
}
$dentry = $mnt.mnt_mountpoint;
$mnt = $mnt_parent;
$mnt_root = $mnt.mnt.mnt_root;
continue;
}
$dentry = $parent_dentry;
}
}
macro printcwd(@paths)
{
for $j : ((uint64)0)..((uint64)max_path_depth) {
let $key = (tid,(uint64)((max_path_depth() - $j) - 1));
let $name = @paths[$key];
// If it is a mount point, there will be a '/', because
// the '/' will be added below, so just skip this '/'.
//
// And, if @paths don't have the $key, just skip the
// empty string.
if ($name == "/" || $name == "") {
continue;
}
printf("/%s", $name);
delete(@paths, $key);
}
printf("/");
}
macro sys_exit(ret, @filename, @paths)
{
$ret = ret;
$fd = ($ret >= 0) ? $ret : (-1);
$errno = ($ret >= 0) ? 0 : (-$ret);
getcwd(@paths);
$path = str(@filename[tid]);
printf("%-6d %-16s %4d", pid, comm, $fd);
if errname {
printf(" %16s ", strerror($errno));
} else {
printf(" %3d ", $errno);
}
// Skip display cwd if path start with '/'.
if ($path[0] != "/"[0]) {
printcwd(@paths);
}
printf("%s\n", str(@filename[tid]));
delete(@filename, tid);
}
tracepoint:syscalls:sys_exit_open,
tracepoint:syscalls:sys_exit_openat,
tracepoint:syscalls:sys_exit_openat2
/@filename[tid]/
{
sys_exit(args.ret, @filename, @paths);
}
END
{
clear(@filename);
clear(@paths);
}