File: fork_exec_snoop.bt

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (172 lines) | stat: -rwxr-xr-x 4,911 bytes parent folder | download | duplicates (2)
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
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bpftrace

//   Copyright 2022 Nicolas Hillegeer <nicolas@hillegeer.com>
//   Modifications Copyright 2023 Paul Wallrabe <pwallrab@redhat.com>
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//   This file is based on the work of Nicolas Hillegeer <nicolas@hillegeer.com>
//   and has been modified by Paul Wallrabe <pwallrab@redhat.com> to add support
//   for normalized timestamps and implemented a trigger to start printing to
//   console once a certain condition is reached specific to Podman.
//
// Traces processes that are created using the fork/exec model.
// Output is printed after the first command containing "podman" is executed.
//
// Usage: sudo ./fork_exec_snoop.bt
//
// TRACEPOINT: This field represents the name of the tracepoint.
// T: This field denotes the timestamp in μs when the tracepoint was triggered.
// DT: This field shows the delta time between enter and exit tracepoints in μs.
// PID: This field represents the Process ID.
// PPID: This field stands for the Parent Process ID.
// TGID: This field indicates the Thread Group ID.
// PTGID: This field denotes the Parent Thread Group ID.
// CMD: This field represents the command name (executable name) and args.
//
// Limitations: The output contains entries from all processes using fork/exec.

BEGIN {
    printf("%-25s %-12s %-12s %-8s %-8s %-8s %-8s %s\n",
        "TRACEPOINT",
        "T",
        "DT",
        "PID",
        "PPID",
        "TGID",
        "PTGID",
        "CMD"
    );

    @time_divisor = (uint64) 1000;
}

tracepoint:syscalls:sys_enter_clone
{
    $time = nsecs;
    @time_start_fork_parent[tid] = $time;
    @time_start_fork_child[tid] = $time;

    if (@time_normalized)
    {
        printf("%-25s %-12u %-12s %-8d %-8d %-8d %-8d %s\n",
            "syscalls:sys_enter_clone",
            ($time - @time_normalized) / @time_divisor,
            "",
            curtask->pid,
            curtask->parent->pid,
            curtask->tgid,
            curtask->parent->tgid,
            comm
        );
    }
}

tracepoint:syscalls:sys_exit_clone
/args->ret == 0 && @time_start_fork_child[curtask->parent->pid]/
{
    $time = nsecs;
    $ptid = curtask->parent->pid;

    if (@time_normalized)
    {
        printf("%-25s %-12u %-12u %-8d %-8d %-8d %-8d %s\n",
            "syscalls:sys_exit_clone",
            ($time - @time_normalized) / @time_divisor,
            ($time - @time_start_fork_child[$ptid]) / @time_divisor,
            curtask->pid,
            curtask->parent->pid,
            curtask->tgid,
            $ptid,
            comm
        );
    }

    delete(@time_start_fork_child[$ptid]);
}

tracepoint:syscalls:sys_exit_clone
/args->ret > 0 && @time_start_fork_parent[tid]/
{
    $time = nsecs;

    if (@time_normalized)
    {
        printf("%-25s %-12u %-12u %-8d %-8d %-8d %-8d %s\n",
            "syscalls:sys_exit_clone",
            ($time - @time_normalized) / @time_divisor,
            ($time - @time_start_fork_parent[tid]) / @time_divisor,
            curtask->pid,
            curtask->parent->pid,
            curtask->tgid,
            curtask->parent->tgid,
            comm
        );
    }

    delete(@time_start_fork_parent[tid]);
}

tracepoint:syscalls:sys_enter_exec*
{
    $time = nsecs;
    @time_start_exec[tid] = $time;

    if (@time_normalized)
    {
        printf("%-25s %-12u %-12s %-8d %-8d %-8d %-8d %s",
            "syscalls:sys_enter_exec*",
            ($time - @time_normalized) / @time_divisor,
            "",
            curtask->pid,
            curtask->parent->pid,
            curtask->tgid,
            curtask->parent->tgid,
            ""
        );
        join(args->argv);
    }
}

tracepoint:syscalls:sys_exit_exec*
/@time_start_exec[pid]/
{
    $time = nsecs;

    if (!@time_normalized && strncmp("podman", comm, 6) == 0)
    {
        @time_normalized = $time;
    }

    if (@time_normalized)
    {
        printf("%-25s %-12u %-12u %-8d %-8d %-8d %-8d %s\n",
            "syscalls:sys_exit_exec*",
            ($time - @time_normalized) / @time_divisor,
            ($time - @time_start_exec[tid]) / @time_divisor,
            curtask->pid,
            curtask->parent->pid,
            curtask->tgid,
            curtask->parent->tgid,
            comm
        );
    }

    delete(@time_start_exec[tid]);
}

END
{
    delete(@time_divisor);
    delete(@time_normalized);
}