1STAPEX(3stap) STAPEX(3stap)
2
3
4
6 stapex - systemtap examples
7
8
9
11 These examples give a feel for basic systemtap syntax and control
12 structures.
13
14 global odds, evens
15
16 probe begin {
17 # "no" and "ne" are local integers
18 for (i=0; i<10; i++) {
19 if (i % 2) odds [no++] = i
20 else evens [ne++] = i
21 }
22 delete odds[2]
23 delete evens[3]
24 exit ()
25 }
26
27 probe end {
28 foreach (x+ in odds) {
29 printf ("odds[%d] = %d0, x, odds[x])
30 }
31 foreach (x in evens-) {
32 printf ("evens[%d] = %d0, x, evens[x])
33 }
34 }
35 This prints:
36 odds[1] = 1
37 odds[3] = 5
38 odds[4] = 7
39 odds[5] = 9
40 evens[5] = 8
41 evens[4] = 6
42 evens[2] = 2
43 evens[1] = 0
44 Note that all variables types are inferred, and that all locals and
45 globals are automatically initialized.
46
47
48 This script prints the primes between 0 and 49.
49 function isprime (x) {
50 if (x < 2) return 0
51 for (i=2; i<x; i++) {
52 if (x % i == 0) return 0
53 if (i * i > x) break
54 }
55 return 1
56 }
57 probe begin {
58 for (i=0; i<50; i++)
59 if (isprime (i)) printf("%d0, i)
60 exit()
61 }
62
63
64 This script demonstrates recursive functions.
65 function fibonacci(i) {
66 if (i < 1) error ("bad number")
67 if (i == 1) return 1
68 if (i == 2) return 2
69 return fibonacci (i-1) + fibonacci (i-2)
70 }
71 probe begin {
72 printf ("11th fibonacci number: %d0, fibonacci (11))
73 exit ()
74 }
75 Any larger number may exceed the MAXACTION or MAXNESTING limits, and
76 result in an error.
77
78
79
81 To trace entry and exit from a function, use a pair of probes:
82 probe kernel.function("sys_mkdir") { println ("enter") }
83 probe kernel.function("sys_mkdir").return { println ("exit") }
84
85 To list the probeable functions in the kernel, use the listings mode.
86 % stap -l 'kernel.function("*")'
87
88 To list the probeable functions and local variables in the kernel, use
89 another listings mode.
90 % stap -L 'kernel.function("*")'
91
92
94 The directory to find more examples can be found in the stappaths (7)
95 manual page.
96
97
99 stap(1) stapprobes(3stap) stapfuncs(3stap) stappaths(7)
100
101
102
103 STAPEX(3stap)