summaryrefslogtreecommitdiffstats
path: root/stapex.5.in
blob: 86e1c87ba6dd3a6b44daf84e5ffddbc7a72f0b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
.\" -*- nroff -*-
.TH STAPEX 5 @DATE@ "Red Hat"
.SH NAME
stapex \- systemtap examples

.\" macros
.de SAMPLE
.br
.RS
.nf
.nh
..
.de ESAMPLE
.hy
.fi
.RE
..

.SH LANGUAGE BASICS
These examples give a feel for basic systemtap syntax and
control structures.

.SAMPLE
global odds, evens

probe begin {
  # "no" and "ne" are local integers
  for (i=0; i<10; i++) {
    if (i % 2) odds [no++] = i
    else evens [ne++] = i 
  }
  delete odds[2]
  delete evens[3]
  exit ()
}

probe end {
  foreach (x+ in odds) {
    printf ("odds[%d] = %d\n", x, odds[x])
  }
  foreach (x in evens\-) {
    printf ("evens[%d] = %d\n", x, evens[x])
  }
}
.ESAMPLE
This prints:
.SAMPLE
odds[1] = 1
odds[3] = 5
odds[4] = 7
odds[5] = 9
evens[5] = 8
evens[4] = 6
evens[2] = 2
evens[1] = 0
.ESAMPLE
Note that all variables types are inferred, and that all locals
and globals are automatically initialized.

.PP
This script prints the primes between 0 and 49.
.SAMPLE
function isprime (x) {
  if (x < 2) return 0
  for (i=2; i<x; i++) {
    if (x % i == 0) return 0
    if (i * i > x) break
  }
  return 1
}
probe begin {
  for (i=0; i<50; i++)
    if (isprime (i)) printf("%d\n", i)
  exit()
}
.ESAMPLE

.PP
This script demonstrates recursive functions.
.SAMPLE
function fibonacci(i) {
  if (i < 1) error ("bad number")
  if (i == 1) return 1
  if (i == 2) return 2
  return fibonacci (i\-1) + fibonacci (i\-2)
}
probe begin {
  printf ("11th fibonacci number: %d\n", fibonacci (11))
  exit ()
}
.ESAMPLE
Any larger number may exceed the MAXACTION or MAXNESTING
limits, and result in an error.


.SH PROBING

To trace entry and exit from a function, use a pair of probes:
.SAMPLE
probe kernel.function("sys_mkdir") { println ("enter") }
probe kernel.function("sys_mkdir").return { println ("exit") }
.ESAMPLE

To list the probeable functions in the kernel, use the last-pass
option to the translator.  That output needs to be filtered because
each inlined function instance is listed separately.
.SAMPLE
% stap \-p2 \-e \[aq]probe kernel.function("*") {}\[aq] | sort | uniq
.ESAMPLE

.SH SEE ALSO
.BR @prefix@/doc/systemtap*/examples
.IR stap (1)
.IR stapprobes (5)
.IR stapfuncs (5)