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