]>
Commit | Line | Data |
---|---|---|
ba4a90fd | 1 | .\" -*- nroff -*- |
e97c0b29 | 2 | .TH STAPEX 3stap @DATE@ "Red Hat" |
ba4a90fd FCE |
3 | .SH NAME |
4 | stapex \- systemtap examples | |
5 | ||
6 | .\" macros | |
7 | .de SAMPLE | |
8 | .br | |
9 | .RS | |
10 | .nf | |
11 | .nh | |
12 | .. | |
13 | .de ESAMPLE | |
14 | .hy | |
15 | .fi | |
16 | .RE | |
17 | .. | |
18 | ||
19 | .SH LANGUAGE BASICS | |
20 | These examples give a feel for basic systemtap syntax and | |
21 | control structures. | |
22 | ||
23 | .SAMPLE | |
24 | global odds, evens | |
25 | ||
26 | probe begin { | |
27 | # "no" and "ne" are local integers | |
28 | for (i=0; i<10; i++) { | |
29 | if (i % 2) odds [no++] = i | |
30 | else evens [ne++] = i | |
31 | } | |
32 | delete odds[2] | |
33 | delete evens[3] | |
34 | exit () | |
35 | } | |
36 | ||
37 | probe end { | |
93484556 | 38 | foreach (x+ in odds) { |
5b3452c9 | 39 | printf ("odds[%d] = %d\n", x, odds[x]) |
ba4a90fd | 40 | } |
13d2ecdb | 41 | foreach (x in evens\-) { |
5b3452c9 | 42 | printf ("evens[%d] = %d\n", x, evens[x]) |
ba4a90fd FCE |
43 | } |
44 | } | |
45 | .ESAMPLE | |
46 | This prints: | |
47 | .SAMPLE | |
48 | odds[1] = 1 | |
49 | odds[3] = 5 | |
50 | odds[4] = 7 | |
51 | odds[5] = 9 | |
ba4a90fd | 52 | evens[5] = 8 |
93484556 FCE |
53 | evens[4] = 6 |
54 | evens[2] = 2 | |
5b3452c9 | 55 | evens[1] = 0 |
ba4a90fd | 56 | .ESAMPLE |
5b3452c9 FCE |
57 | Note that all variables types are inferred, and that all locals |
58 | and globals are automatically initialized. | |
ba4a90fd FCE |
59 | |
60 | .PP | |
61 | This script prints the primes between 0 and 49. | |
62 | .SAMPLE | |
63 | function isprime (x) { | |
64 | if (x < 2) return 0 | |
65 | for (i=2; i<x; i++) { | |
66 | if (x % i == 0) return 0 | |
67 | if (i * i > x) break | |
68 | } | |
69 | return 1 | |
70 | } | |
71 | probe begin { | |
72 | for (i=0; i<50; i++) | |
5b3452c9 | 73 | if (isprime (i)) printf("%d\n", i) |
ba4a90fd FCE |
74 | exit() |
75 | } | |
76 | .ESAMPLE | |
77 | ||
78 | .PP | |
79 | This script demonstrates recursive functions. | |
80 | .SAMPLE | |
81 | function fibonacci(i) { | |
82 | if (i < 1) error ("bad number") | |
83 | if (i == 1) return 1 | |
84 | if (i == 2) return 2 | |
13d2ecdb | 85 | return fibonacci (i\-1) + fibonacci (i\-2) |
ba4a90fd FCE |
86 | } |
87 | probe begin { | |
5b3452c9 | 88 | printf ("11th fibonacci number: %d\n", fibonacci (11)) |
ba4a90fd FCE |
89 | exit () |
90 | } | |
91 | .ESAMPLE | |
92 | Any larger number may exceed the MAXACTION or MAXNESTING | |
93 | limits, and result in an error. | |
94 | ||
95 | ||
96 | .SH PROBING | |
97 | ||
98 | To trace entry and exit from a function, use a pair of probes: | |
99 | .SAMPLE | |
163a52d9 MH |
100 | probe kernel.function("sys_mkdir") { println ("enter") } |
101 | probe kernel.function("sys_mkdir").return { println ("exit") } | |
ba4a90fd FCE |
102 | .ESAMPLE |
103 | ||
16442b90 | 104 | To list the probeable functions in the kernel, use the listings mode. |
ba4a90fd | 105 | .SAMPLE |
16442b90 | 106 | % stap \-l \[aq]kernel.function("*")\[aq] |
ba4a90fd FCE |
107 | .ESAMPLE |
108 | ||
d4e35ac8 WH |
109 | To list the probeable functions and local variables in the kernel, use another listings mode. |
110 | .SAMPLE | |
111 | % stap \-L \[aq]kernel.function("*")\[aq] | |
112 | .ESAMPLE | |
113 | ||
ff7b8b4a MW |
114 | .SH MORE EXAMPLES |
115 | ||
116 | Larger examples, demos and samples can be found in | |
117 | @prefix@/doc/systemtap*/examples, each example comes with either a .txt | |
118 | or .meta file explaining what the example, sample or demo does and how | |
119 | it is ordinarily run. | |
120 | ||
ba4a90fd | 121 | .SH SEE ALSO |
5b3452c9 | 122 | .BR @prefix@/doc/systemtap*/examples |
ba4a90fd | 123 | .IR stap (1) |
e97c0b29 WC |
124 | .IR stapprobes (3stap) |
125 | .IR stapfuncs (3stap) | |
ba4a90fd | 126 |