Search

1.4. Sample SystemTap scripts

download PDF
Following are some example scripts that illustrate the basic operation of SystemTap. For more examples, see the examples/small_demos/ directory in the source directory, the SystemTap wiki at http://sourceware.org/systemtap/wiki/HomePage, or the SystemTap War Stories at http://sourceware.org/systemtap/wiki/WarStories page.

1.4.1. Basic SystemTap syntax and control structures

The following code examples demonstrate SystemTap syntax and control structures.
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", x, odds[x])

foreach (x in evens-)
printf ("evens[%d] = %d", x, evens[x])
}
This prints:
odds[0] = 1
odds[1] = 3
odds[3] = 7
odds[4] = 9
evens[4] = 8
evens[2] = 4
evens[1] = 2
evens[0] = 0
Note that all variable types are inferred, and that all locals and globals are initialized.

1.4.2. Primes between 0 and 49

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()
}
This prints:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

1.4.3. Recursive functions

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", fibonacci (11))
exit ()
}
This prints:
11th fibonacci number: 118
Any larger number input to the function may exceed the MAXACTION or MAXNESTING limits, which will be caught at run time and result in an error. For more about limits see Section Section 1.6, “Safety and security”.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.