Debian Codegraph @

testsuite/systemtap.examples/general/whythefail.stp

raw
1 #! /usr/bin/env stap
2
3 /* Print a trace of the the last few statements (file:line numbers,
4 along with selected local variables) that were executed within a
5 given function, if it ends up returning with a given condition. (No
6 trace is shown for functions that return with the condition false,
7 demonstrating use of speculation tapset.)
8
9 Usage:
10 stap whythefail.stp PROC_OR_KERNEL FUNCTION_NAME CONDITION_EXPRESSION [$VARIABLE$]
11
12 PROC_OR_KERNEL: the first part of the probe point, e.g.:
13 'kernel' 'module("foo")' or 'process("bar")'
14 FUNCTION_NAME: the function name (assumed non-recursive)
15 CONDITION: expression to evaluate at function return, e.g.:
16 '$return != 0' or '1' or 'randint(100)<20'
17 VARIABLE: optional context-variable string to print at each statement, e.g.:
18 '$a_var$' or '@var("global")$' or (default) '$$vars'
19
20 If the script is invoked with -c CMD or -x PID, probes are filtered
21 for that process target-set (it and its descendants).
22
23 e.g.:
24 stap whythefail.stp kernel do_mlock '$return < 0'
25 */
26
27
28 global specs%
29 global counts
30
31 probe $1 . function(@2) . call {
32 if (target() && !target_set_pid(pid())) next;
33 specs[tid()] = speculation()
34 counts["entry"] <<< 1
35 }
36
37 probe $1 . statement(@2 "@*:*") {
38 if (target() && !target_set_pid(pid())) next;
39 tokenize(pp(),"@");
40 fileline=tokenize("","@");
41 file=tokenize(fileline,":\"");
42 line=tokenize("",":\"");
43 speculate(specs[tid()], sprintf("%s[%d] %s:%s %s\n",
44 execname(), tid(), file, line,
45 %( $# >= 4
46 %? @4.": ".@choose_defined($4,"?")
47 %: "$$vars: ".$$vars %) ))
48 counts["statement"] <<< 1
49 }
50
51 probe $1 . function(@2) . return {
52 if (target() && !target_set_pid(pid())) next;
53 if ($3) {
54 printf("\n%s[%d] %s function(%s) $return: %s\n", execname(), tid(), @1, @2, $return$)
55 commit(specs[tid()])
56 counts["exit-hit"] <<< 1
57 } else {
58 discard(specs[tid()])
59 counts["exit-miss"] <<< 1
60 }
61 }
62
63 probe end,error {
64 printf("\nstatistics:\n")
65 foreach (reason+ in counts) {
66 printf("%s count: %d\n", reason, @count(counts[reason]))
67 }
68 }