Debian Codegraph @

tapset/bpf/context.stp

raw
1 // context tapset
2 // Copyright (C) 2016-2021 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8
9 /**
10 * sfunction execname - Returns the execname of a target process (or group of processes)
11 *
12 * Description: Returns the execname of a target process (or group of processes).
13 */
14 function execname:string ()
15 %{ /* bpf */ /* pure */ /* unprivileged */ /* stable */
16 /* buf = bpf_stk_alloc(BPF_MAXSTRINGLEN);
17 buf[0] = 0x0; // guarantee NUL byte
18 rc = get_current_comm(buf, BPF_MAXSTRINGLEN); */
19 alloc, $buf, BPF_MAXSTRINGLEN;
20 0x62, $buf, -, -, 0x0; /* stw [$buf+0], 0x0 -- guarantee NUL byte */
21 call, $rc, get_current_comm, $buf, BPF_MAXSTRINGLEN;
22
23 /* if (rc < 0) return err_msg;
24 return buf; */
25 0xc5, $rc, -, _err, 0; /* jslt $rc, 0, _err */
26 0xbf, $$, $buf, -, -; /* mov $$, $buf */
27 0x05, -, -, _done, -; /* ja _done */
28
29 label, _err;
30 0xbf, $$, "<unknown>", -, -; /* mov $$, <unknown> */
31
32 label, _done;
33 %}
34
35 // TODO: pexecname ()
36
37 /**
38 * sfunction pid - Returns the ID of a thread group
39 *
40 * Description: This function returns the userspace pid / kernel tgid
41 * of a target process.
42 */
43 function pid:long ()
44 %{ /* bpf */ /* pure */ /* unprivileged */ /* stable */
45 /* BPF_FUNC_get_current_pid_tgid returns tgid << 32 | pid */
46 0x85, 0, 0, 0, 14; /* call BPF_FUNC_get_current_pid_tgid */
47 0xbf, $$, 0, 0, 0; /* movx $$, r0 */
48 0x77, $$, 0, 0, 32 /* rshk $$, 32 */
49 %}
50
51 // TODO: ns_pid:long ()
52
53 /**
54 * sfunction tid - Returns the thread ID of a target process
55 *
56 * Description: This function returns the userspace tid / kernel pid
57 * of a target process.
58 */
59 function tid:long ()
60 %{ /* bpf */ /* pure */ /* unprivileged */ /* stable */
61 /* BPF_FUNC_get_current_pid_tgid returns tgid << 32 | pid */
62 0x85, 0, 0, 0, 14; /* call BPF_FUNC_get_current_pid_tgid */
63 0xbc, $$, 0, 0, 0 /* movwx $$, r0 */
64 %}
65
66 // TODO: ns_tid:long ()
67 // TODO: ppid:long ()
68 // TODO: ns_ppid:long ()
69 // TODO: pgrp:long ()
70 // TODO: ns_pgrp:long ()
71 // TODO: sid:long ()
72 // TODO: ns_sid:long ()
73
74 /**
75 * sfunction gid - Returns the group ID of a target process
76 *
77 * Description: This function returns the group ID of a target process.
78 */
79 function gid:long ()
80 %{ /* bpf */ /* pure */ /* unprivileged */ /* stable */
81 /* BPF_FUNC_get_current_uid_gid returns gid << 32 | uid */
82 0x85, 0, 0, 0, 15; /* call BPF_FUNC_get_current_uid_gid */
83 0xbf, $$, 0, 0, 0; /* movx $$, r0 */
84 0x77, $$, 0, 0, 32 /* rshk $$, 32 */
85 %}
86
87 // TODO: ns_gid:long ()
88 // TODO: egid:long ()
89 // TODO: ns_egid:long ()
90
91 /**
92 * sfunction uid - Returns the user ID of a target process
93 *
94 * Description: This function returns the user ID of the target process.
95 */
96 function uid:long ()
97 %{ /* bpf */ /* pure */ /* unprivileged */ /* stable */
98 /* BPF_FUNC_get_current_uid_gid returns gid << 32 | uid */
99 0x85, 0, 0, 0, 15; /* call BPF_FUNC_get_current_uid_gid */
100 0xbc, $$, 0, 0, 0 /* movwx $$, r0 */
101 %}
102
103 // TODO: ns_uid:long ()
104 // TODO: euid:long ()
105 // TODO: ns_euid:long ()
106 // XXX: is_myproc () is only relevant for unprivileged use of eBPF (still theoretical).
107
108 // TODO: Old systemtap-compat scripts should not be running on eBPF backend in the first place?
109 /**
110 * sfunction cpuid - Returns the current cpu number
111 *
112 * Description: This function returns the current cpu number.
113 * Deprecated in SystemTap 1.4 and removed in SystemTap 1.5.
114 */
115 %( systemtap_v <= "1.4" %?
116 function cpuid:long ()
117 %{ /* bpf */ /* pure */
118 0x85, 0, 0, 0, 8; /* call BPF_FUNC_get_smp_processor_id */
119 0xbf, $$, 0, 0, 0 /* movx $$, r0 */
120 %}
121 %)
122
123 /**
124 * sfunction cpu - Returns the current cpu number
125 *
126 * Description: This function returns the current cpu number.
127 */
128 function cpu:long ()
129 %{ /* bpf */ /* pure */ /* unprivileged */ /* stable */
130 0x85, 0, 0, 0, 8; /* call BPF_FUNC_get_smp_processor_id */
131 0xbf, $$, 0, 0, 0 /* movx $$, r0 */
132 %}
133
134 // TODO: registers_valid:long ()
135 // TODO: user_mode:long ()
136 // TODO: is_return:long ()
137 // TODO: target:long ()
138 // TODO: module_name:string ()
139 // XXX: module_size:string () -- not clear if this should refer to the entire .bo or to just the current eBPF routine.
140 // TODO: stp_pid:long ()
141 // XXX: remote_id:long (), remote_uri:string() -- pending an evaluation of remote eBPF execution.
142 // XXX: stack_size() -- not clear if this should be the eBPF stack size or the kernel stack size.
143 // XXX: stack_used(),stack_unused() probably a fairly ill-defined idea with the eBPF stack.
144 // TODO: Other context functions for info about things like eBPF maps.
145
146 // TODO: addr:long ()
147 // TODO: uaddr:long ()
148 // XXX: cmdline_args:string(n:long, m:long, delim:string) -- requires string concatenation & loops.
149 // TODO: cmdline_arg:string(n:long)
150 // XXX: cmdline_string:string() -- requires string concatenation & loops.
151
152 global _stp_target
153
154 /**
155 * Description: This function copies the system's target PID set with
156 * either -x or -c in stabpf. This function only works with the user-space
157 * bpf interpreter, so it runs during initialization to copy target PID value
158 * into a systemtap global which can be read from other more constrained
159 * contexts.
160 */
161 function _stp_get_target:long ()
162 %{ /* bpf */ /* pure */ /* unprivileged */ /* userspace */
163 0x85, 0, 0, 0, -5; /* call BPF_FUNC_get_target */
164 0xbf, $$, 0, 0, 0 /* movx $$, r0 */
165 %}
166
167 probe init
168 {
169 _stp_target = _stp_get_target()
170 }
171
172 /**
173 * sfunction target - Return the process ID of the target process
174 *
175 * Description: This function returns the process ID of the target
176 * process. This is useful in conjunction with the -x PID or
177 * -c CMD command-line options to stap. An example of its use is
178 * to create scripts that filter on a specific process.
179 *
180 * -x <pid>
181 * target() returns the pid specified by -x
182 *
183 * -c <command>
184 * target() returns the pid for the executed command specified by -c
185 */
186 function target:long ()
187 {
188 return _stp_target
189 }