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