Debian Codegraph @

tapset/linux/context.stp

raw
1 // context tapset
2 // Copyright (C) 2005-2015 Red Hat Inc.
3 // Copyright (C) 2006 Intel Corporation.
4 //
5 // This file is part of systemtap, and is free software. You can
6 // redistribute it and/or modify it under the terms of the GNU General
7 // Public License (GPL); either version 2, or (at your option) any
8 // later version.
9 //
10 // NB: tapsetdescription is in tapset/context.stp
11
12 /**
13 * sfunction execname - Returns the execname of a target process (or group of processes)
14 *
15 * Description: Returns the execname of a target process (or group of processes).
16 */
17 function execname:string ()
18 %{ /* pure */ /* unprivileged */ /* stable */
19 strlcpy (STAP_RETVALUE, current->comm, MAXSTRINGLEN);
20 %}
21
22 /**
23 * sfunction pexecname - Returns the execname of a target process's parent process
24 *
25 * Description: This function returns the execname of a target
26 * process's parent procces.
27 */
28 function pexecname:string ()
29 %{ /* pure */ /* unprivileged */ /* stable */
30 #if defined(STAPCONF_REAL_PARENT)
31 strlcpy (STAP_RETVALUE, current->real_parent->comm, MAXSTRINGLEN);
32 #else
33 strlcpy (STAP_RETVALUE, current->parent->comm, MAXSTRINGLEN);
34 #endif
35 %}
36
37 /**
38 * sfunction pid - Returns the ID of a target process
39 *
40 * Description: This function returns the ID of a target process.
41 */
42 function pid:long ()
43 {
44 return @const("current->tgid")
45 }
46
47 /**
48 * sfunction ns_pid - Returns the ID of a target process as seen in a pid namespace
49 *
50 * Description: This function returns the ID of a target process as seen in
51 * the target pid namespace.
52 */
53 function ns_pid:long ()
54 %{ /* pure */ /* unprivileged */ /* stable */
55 int64_t rc = from_target_pid_ns(current, PID);
56 if (rc < 0) STAP_ERROR ("cannot resolve id in namespace");
57 else STAP_RETURN (rc);
58 %}
59
60 /**
61 * sfunction tid - Returns the thread ID of a target process
62 *
63 * Description: This function returns the thread ID of the target process.
64 */
65 function tid:long ()
66 {
67 return @const("current->pid")
68 }
69
70 /**
71 * sfunction ns_tid - Returns the thread ID of a target process as seen in a pid namespace
72 *
73 * Description: This function returns the thread ID of a target process as seen
74 * in the target pid namespace if provided, or the stap process namespace.
75 */
76 function ns_tid:long ()
77 %{ /* pure */ /* unprivileged */ /* stable */
78 int64_t rc = from_target_pid_ns(current, TID);
79 if (rc < 0) STAP_ERROR ("cannot resolve id in namespace");
80 else STAP_RETURN (rc);
81 %}
82
83 /**
84 * sfunction ppid - Returns the process ID of a target process's parent process
85 *
86 * Description: This function return the process ID of the target proccess's parent process.
87 */
88 function ppid:long()
89 %{ /* pure */ /* unprivileged */ /* stable */
90 #if defined(STAPCONF_REAL_PARENT)
91 STAP_RETVALUE = current->real_parent->tgid;
92 #else
93 STAP_RETVALUE = current->parent->tgid;
94 #endif
95 %}
96
97 /**
98 * sfunction ns_ppid - Returns the process ID of a target process's parent process as seen in a pid namespace
99 *
100 * Description: This function return the process ID of the target proccess's parent process as seen in the target pid namespace if provided, or the stap process namespace.
101 */
102 function ns_ppid:long()
103 %{ /* pure */ /* unprivileged */ /* stable */
104 int64_t rc = from_target_pid_ns(current->parent, PID);
105 if (rc < 0) STAP_ERROR ("cannot resolve id in namespace");
106 else STAP_RETURN (rc);
107 %}
108
109 /**
110 * sfunction pgrp - Returns the process group ID of the current process
111 *
112 * Description: This function returns the process group ID of the
113 * current process.
114 */
115 function pgrp:long ()
116 %{ /* pure */ /* unprivileged */ /* stable */
117 STAP_RETVALUE = task_pgrp_nr_ns(current, &init_pid_ns);
118 %}
119
120 /**
121 * sfunction ns_pgrp - Returns the process group ID of the current process as seen in a pid namespace
122 *
123 * Description: This function returns the process group ID of the
124 * current process as seen in the target pid namespace if provided, or the stap process namespace.
125 */
126 function ns_pgrp:long ()
127 %{ /* pure */ /* unprivileged */ /* stable */
128 int64_t rc = from_target_pid_ns(current, PGRP);
129 if (rc < 0) STAP_ERROR ("cannot resolve id in namespace");
130 else STAP_RETURN (rc);
131 %}
132
133 /**
134 * sfunction sid - Returns the session ID of the current process
135 *
136 * Description: The session ID of a process is the process group ID
137 * of the session leader. Session ID is stored in the signal_struct
138 * since Kernel 2.6.0.
139 */
140 function sid:long ()
141 %{ /* pure */ /* unprivileged */ /* stable */
142 STAP_RETVALUE = task_session_nr_ns(current, &init_pid_ns);
143 %}
144
145 /**
146 * sfunction ns_sid - Returns the session ID of the current process as seen in a pid namespace
147 *
148 * Description: The namespace-aware session ID of a process is the process group ID
149 * of the session leader as seen in the target pid namespace if provided, or the stap process namespace. Session ID is stored in the signal_struct
150 * since Kernel 2.6.0.
151 */
152 function ns_sid:long ()
153 %{ /* pure */ /* unprivileged */ /* stable */
154 int64_t rc = from_target_pid_ns(current, SID);
155 if (rc < 0) STAP_ERROR ("cannot resolve id in namespace");
156 else STAP_RETURN (rc);
157 %}
158
159 /**
160 * sfunction gid - Returns the group ID of a target process
161 *
162 * Description: This function returns the group ID of a target process.
163 */
164 function gid:long ()
165 %{ /* pure */ /* unprivileged */ /* stable */
166 #ifdef STAPCONF_TASK_UID
167 STAP_RETVALUE = current->gid;
168 #else
169 #if defined(CONFIG_USER_NS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0))
170 STAP_RETVALUE = from_kgid_munged(current_user_ns(), current_gid());
171 #else
172 STAP_RETVALUE = current_gid();
173 #endif
174 #endif
175 %}
176
177 /**
178 * sfunction ns_gid - Returns the group ID of a target process as seen in a user namespace
179 *
180 * Description: This function returns the group ID of a target process as seen in the target user namespace if provided, or the stap process namespace.
181 */
182 function ns_gid:long ()
183 %{ /* pure */ /* unprivileged */ /* stable */
184 int64_t rc = from_target_user_ns(current, GID);
185 if (rc < 0) STAP_ERROR ("cannot resolve id in namespace");
186 else STAP_RETURN (rc);
187 %}
188
189 /**
190 * sfunction egid - Returns the effective gid of a target process
191 *
192 * Description: This function returns the effective gid of a target process
193 */
194 function egid:long ()
195 %{ /* pure */ /* unprivileged */ /* stable */
196 #ifdef STAPCONF_TASK_UID
197 STAP_RETVALUE = current->egid;
198 #else
199 #if defined(CONFIG_USER_NS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0))
200 STAP_RETVALUE = from_kgid_munged(current_user_ns(), current_egid());
201 #else
202 STAP_RETVALUE = current_egid();
203 #endif
204 #endif
205 %}
206
207 /**
208 * sfunction ns_egid - Returns the effective gid of a target process as seen in a user namespace
209 *
210 * Description: This function returns the effective gid of a target process as seen in the target user namespace if provided, or the stap process namespace
211 */
212 function ns_egid:long ()
213 %{ /* pure */ /* unprivileged */ /* stable */
214 int64_t rc = from_target_user_ns(current, EGID);
215 if (rc < 0) STAP_ERROR ("cannot resolve id in namespace");
216 else STAP_RETURN (rc);
217 %}
218
219 /**
220 * sfunction uid - Returns the user ID of a target process
221 *
222 * Description: This function returns the user ID of the target process.
223 */
224 function uid:long ()
225 %{ /* pure */ /* unprivileged */ /* stable */
226 #ifdef STAPCONF_TASK_UID
227 STAP_RETVALUE = current->uid;
228 #else
229 #if defined(CONFIG_USER_NS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0))
230 STAP_RETVALUE = from_kuid_munged(current_user_ns(), current_uid());
231 #else
232 STAP_RETVALUE = current_uid();
233 #endif
234 #endif
235 %}
236
237 /**
238 * sfunction ns_uid - Returns the user ID of a target process as seen in a user namespace
239 *
240 * Description: This function returns the user ID of the target process as seen in the target user namespace if provided, or the stap process namespace.
241 */
242 function ns_uid:long ()
243 %{ /* pure */ /* unprivileged */ /* stable */
244 int64_t rc = from_target_user_ns(current, UID);
245 if (rc < 0) STAP_ERROR ("cannot resolve id in namespace");
246 else STAP_RETURN (rc);
247 %}
248
249 /**
250 * sfunction euid - Return the effective uid of a target process
251 *
252 * Description: Returns the effective user ID of the target process.
253 */
254 function euid:long ()
255 %{ /* pure */ /* unprivileged */ /* stable */
256 #ifdef STAPCONF_TASK_UID
257 STAP_RETVALUE = current->euid;
258 #else
259 #if defined(CONFIG_USER_NS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0))
260 STAP_RETVALUE = from_kuid_munged(&init_user_ns, current_euid());
261 #else
262 STAP_RETVALUE = current_euid();
263 #endif
264 #endif
265 %}
266
267 /**
268 * sfunction ns_euid - Returns the effective user ID of a target process as seen in a user namespace
269 *
270 * Description: This function returns the effective user ID of the target process as seen in the target user namespace if provided, or the stap process namespace.
271 */
272 function ns_euid:long ()
273 %{ /* pure */ /* unprivileged */ /* stable */
274 int64_t rc = from_target_user_ns(current, EUID);
275 if (rc < 0) STAP_ERROR ("cannot resolve id in namespace");
276 else STAP_RETURN (rc);
277 %}
278
279 /**
280 * sfunction is_myproc - Determines if the current probe point has occurred in the user's own process
281 *
282 * Description: This function returns 1 if the current probe
283 * point has occurred in the user's own process.
284 */
285 function is_myproc:long ()
286 %{ /* pure */ /* unprivileged */ /* stable */
287 STAP_RETVALUE = is_myproc();
288 %}
289
290
291 %( systemtap_v <= "1.4" %?
292 /**
293 * sfunction cpuid - Returns the current cpu number
294 *
295 * Description: This function returns the current cpu number.
296 * Deprecated in SystemTap 1.4 and removed in SystemTap 1.5.
297 */
298 function cpuid:long ()
299 %{ /* pure */
300 STAP_RETVALUE = smp_processor_id();
301 %}
302 %)
303
304 /**
305 * sfunction cpu - Returns the current cpu number
306 *
307 * Description: This function returns the current cpu number.
308 */
309 function cpu:long ()
310 %{ /* pure */ /* unprivileged */ /* stable */
311 STAP_RETVALUE = smp_processor_id();
312 %}
313
314 /**
315 * sfunction registers_valid - Determines validity of register() and u_register() in current context
316 *
317 * Description: This function returns 1 if register() and u_register()
318 * can be used in the current context, or 0 otherwise.
319 * For example, registers_valid() returns 0
320 * when called from a begin or end probe.
321 */
322 function registers_valid:long ()
323 %{ /* pure */ /* unprivileged */ /* stable */
324 STAP_RETVALUE = (c->user_mode_p
325 ? (CONTEXT->uregs != NULL)
326 : (CONTEXT->kregs != NULL));
327 %}
328
329 /**
330 * sfunction user_mode - Determines if probe point occurs in user-mode
331 *
332 * Return 1 if the probe point occurred in user-mode.
333 */
334 function user_mode:long ()
335 %{ /* pure */ /* unprivileged */ /* stable */
336 STAP_RETVALUE = CONTEXT->user_mode_p ? 1 : 0;
337 %}
338
339 /**
340 * sfunction is_return - Whether the current probe context is a return probe
341 *
342 * Description: Returns 1 if the current probe context is a return probe,
343 * returns 0 otherwise.
344 */
345 function is_return:long ()
346 %{ /* pure */ /* stable */
347 if (CONTEXT->probe_type == stp_probe_type_kretprobe
348 || CONTEXT->probe_type == stp_probe_type_uretprobe)
349 STAP_RETVALUE = 1;
350 else
351 STAP_RETVALUE = 0;
352 %}
353
354 /**
355 * sfunction target - Return the process ID of the target process
356 *
357 * Description: This function returns the process ID of the target
358 * process. This is useful in conjunction with the -x PID or
359 * -c CMD command-line options to stap. An example of its use is
360 * to create scripts that filter on a specific process.
361 *
362 * -x <pid>
363 * target() returns the pid specified by -x
364 *
365 * -c <command>
366 * target() returns the pid for the executed command specified by -c
367 */
368 function target:long ()
369 {
370 return @const("_stp_target")
371 }
372
373 /**
374 * sfunction module_name - The module name of the current script
375 *
376 * Description: This function returns the name of the stap module.
377 * Either generated randomly (stap_[0-9a-f]+_[0-9a-f]+)
378 * or set by stap -m <module_name>.
379 */
380 function module_name:string ()
381 %{ /* pure */ /* unprivileged */ /* stable */
382 strlcpy(STAP_RETVALUE, THIS_MODULE->name, MAXSTRINGLEN);
383 %}
384
385 /**
386 * sfunction module_size - The module size of the current script
387 *
388 * Description: This function returns the sizes of various sections
389 * of the stap module.
390 */
391 function module_size:string() %{
392 int ctx = num_online_cpus() * sizeof(struct context);
393 snprintf(STAP_RETVALUE, MAXSTRINGLEN, "%ludata/%lutext/%uctx/%lunet/%lualloc kb",
394 #ifdef STAPCONF_MODULE_LAYOUT
395 (unsigned long) (THIS_MODULE->core_layout.size - THIS_MODULE->core_layout.text_size)/1024,
396 (unsigned long) (THIS_MODULE->core_layout.text_size)/1024,
397 #else
398 #ifndef STAPCONF_GRSECURITY
399 (unsigned long) (THIS_MODULE->core_size - THIS_MODULE->core_text_size)/1024,
400 (unsigned long) (THIS_MODULE->core_text_size)/1024,
401 #else
402 (unsigned long) (THIS_MODULE->core_size_rw - THIS_MODULE->core_size_rx)/1024,
403 (unsigned long) (THIS_MODULE->core_size_rx)/1024,
404 #endif
405 #endif
406 ctx/1024, _stp_allocated_net_memory/1024,
407 (_stp_allocated_memory - _stp_allocated_net_memory - ctx)/1024);
408 %}
409
410 /**
411 * sfunction stp_pid - The process id of the stapio process
412 *
413 * Description: This function returns the process id of the
414 * stapio process that launched this script. There could be
415 * other SystemTap scripts and stapio processes running on
416 * the system.
417 */
418 function stp_pid:long ()
419 %{ /* pure */ /* stable */
420 STAP_RETVALUE = _stp_pid;
421 %}
422
423
424 /**
425 * sfunction remote_id - The index of this instance in a remote execution.
426 *
427 * Description: This function returns a number 0..N, which is the unique
428 * index of this particular script execution from a swarm of
429 * "stap --remote A --remote B ..." runs, and is the same number
430 * "stap --remote-prefix" would print. The function returns -1 if the
431 * script was not launched with "stap --remote", or if the remote
432 * staprun/stapsh are older than version 1.7.
433 */
434 function remote_id:long () {
435 return @const("_stp_remote_id")
436 }
437
438
439 /**
440 * sfunction remote_uri - The name of this instance in a remote execution.
441 *
442 * Description: This function returns the remote host used to invoke
443 * this particular script execution from a swarm of "stap --remote" runs.
444 * It may not be unique among the swarm.
445 * The function returns an empty string if the script was not launched with
446 * "stap --remote".
447 */
448 function remote_uri:string () {
449 return %{ /* string */ /* pure */ /* unprivileged */ _stp_remote_uri %}
450 }
451
452
453 /**
454 * sfunction stack_size - Return the size of the kernel stack
455 *
456 * Description: This function returns the size of the kernel stack.
457 */
458 function stack_size:long ()
459 %{ /* pure */ /* stable */
460 STAP_RETVALUE = THREAD_SIZE;
461 %}
462
463 /**
464 * sfunction stack_used - Returns the amount of kernel stack used
465 *
466 * Description: This function determines how many bytes are
467 * currently used in the kernel stack.
468 */
469 function stack_used:long ()
470 %{ /* pure */ /* stable */
471 char a;
472 STAP_RETVALUE = THREAD_SIZE - ((long)&a & (THREAD_SIZE-1));
473 %}
474
475 /**
476 * sfunction stack_unused - Returns the amount of kernel stack currently available
477 *
478 * Description: This function determines how many bytes are
479 * currently available in the kernel stack.
480 */
481 function stack_unused:long ()
482 %{ /* pure */ /* stable */
483 char a;
484 STAP_RETVALUE = (long)&a & (THREAD_SIZE-1);
485 %}
486
487 /**
488 * sfunction addr - Address of the current probe point.
489 *
490 * Description: Returns the instruction pointer from the current probe's
491 * register state. Not all probe types have registers though, in which case
492 * zero is returned. The returned address is suitable for use with functions
493 * like symname() and symdata().
494 */
495 function addr:long ()
496 %{ /* pure */ /* stable */
497 if (CONTEXT->user_mode_p) {
498 STAP_RETVALUE = (intptr_t)(CONTEXT->uregs ? REG_IP(CONTEXT->uregs) : 0);
499 } else {
500 STAP_RETVALUE = (intptr_t)(CONTEXT->kregs ? REG_IP(CONTEXT->kregs) : 0);
501 }
502 %}
503
504 /**
505 * sfunction uaddr - User space address of current running task
506 *
507 * Description: Returns the address in userspace that the current
508 * task was at when the probe occurred. When the current running task
509 * isn't a user space thread, or the address cannot be found, zero
510 * is returned. Can be used to see where the current task is combined
511 * with usymname() or usymdata(). Often the task will be in the VDSO
512 * where it entered the kernel.
513 */
514 function uaddr:long ()
515 %{ /* pure */ /* myproc-unprivileged */ /* stable */
516 struct pt_regs *uregs;
517
518 if (CONTEXT->user_mode_p)
519 uregs = CONTEXT->uregs;
520 else
521 uregs = _stp_current_pt_regs();
522
523 if (uregs)
524 STAP_RETVALUE = (int64_t) REG_IP(uregs);
525 else
526 STAP_RETVALUE = 0;
527 %}
528
529
530 /**
531 * sfunction cmdline_args - Fetch command line arguments from current process
532 * @n: First argument to get (zero is normally the program itself)
533 * @m: Last argument to get (or minus one for all arguments after n)
534 * @delim: String to use to separate arguments when more than one.
535 *
536 * Description: Returns arguments from the current process starting
537 * with argument number n, up to argument m. If there are less than n
538 * arguments, or the arguments cannot be retrieved from the current
539 * process, the empty string is returned. If m is smaller than n then
540 * all arguments starting from argument n are returned. Argument zero
541 * is traditionally the command itself.
542 */
543 function cmdline_args:string(n:long, m:long, delim:string)
544 {
545 __args = "";
546 __mm = @task(task_current())->mm;
547 if (__mm == 0)
548 return "";
549
550 __arg_start = @mm(__mm)->arg_start;
551 __arg_end = @mm(__mm)->arg_end;
552 if (__arg_start == 0 || __arg_end == 0)
553 return "";
554
555 for (__nr=0;
556 (__arg_start < __arg_end) && // still string content left?
557 (m<0 || __nr<=m); // caller still interested?
558 __nr++) {
559
560 __arg = user_string(__arg_start, ""); // argv[__nr], unquoted
561 __arg_start += strlen(__arg) + 1; // skip over for next iteration
562
563 if (__nr == n) // caller starting to get interested?
564 __args = __arg;
565 else if (__nr > n) // caller still interested?
566 {
567 if (__arg == "")
568 __args .= delim . "\"\""
569 else
570 __args .= delim . __arg;
571 }
572 }
573
574 return __args;
575 }
576
577 /**
578 * sfunction cmdline_arg - Fetch a command line argument
579 * @n: Argument to get (zero is the program itself)
580 *
581 * Description: Returns argument the requested argument from the
582 * current process or the empty string when there are not that many
583 * arguments or there is a problem retrieving the argument. Argument
584 * zero is traditionally the command itself.
585 */
586 function cmdline_arg:string(n:long)
587 {
588 return cmdline_args(n, n, "");
589 }
590
591 /**
592 * sfunction cmdline_str - Fetch all command line arguments from current process
593 *
594 * Description: Returns all arguments from the current process
595 * delimited by spaces. Returns the empty string when the arguments
596 * cannot be retrieved.
597 */
598 function cmdline_str:string()
599 {
600 return cmdline_args(0, -1, " ");
601 }