STAPEX(3stap)
๐ STAPEX ๐
okay bestie, so this is stapex โ yes, the systemtap examples page, from section 3stap of the systemtap manuals. it's basically the starter pack of little scripts that show you how systemtap actually looks and feels when you write it. no cap, this is the vibe check you run before you write anything real. ๐ธ
โจ LANGUAGE BASICS โจ
these examples give you a feel for basic systemtap syntax and control structures. lowkey the whole language in one screenshotted group chat:
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])
}
}
and this prints:
odds[1] = 1
odds[3] = 5
odds[4] = 7
odds[5] = 9
evens[5] = 8
evens[4] = 6
evens[2] = 2
evens[1] = 0
note that all variables types are inferred, and that all locals and globals are automatically initialized. the language just knows. it's giving mind reading, fr fr. ๐ฎ
this next script prints the primes between 0 and 49. it ate, honestly:
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()
}
and this script demonstrates recursive functions. functions calling functions, main character energy only ๐ซถ
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 ()
}
โ ๏ธ any larger number may exceed theMAXACTIONorMAXNESTINGlimits, and result in an error. so likeโฆ stay in your lane with the recursion, bestie, the runtime will clock you. ๐
๐ PROBING ๐
to trace entry and exit from a function, use a pair of probes. one for the door in, one for the door out:
probe kernel.function("sys_mkdir") { println ("enter") }
probe kernel.function("sys_mkdir").return { println ("exit") }
to list the probeable functions in the kernel, use the listings mode:
% stap -l 'kernel.function("*")'
to list the probeable functions and local variables in the kernel, use another listings mode. that's the capital-L glow-up โจ:
% stap -L 'kernel.function("*")'
๐ฆ MORE EXAMPLES ๐ฆ
the directory to find more examples can be found in the stappaths (7) manual page, and online at http://sourceware.org/systemtap/examples/ โ go wild, there's so much more where this came from ๐
๐ SEE ALSO ๐
stap(1)stapprobes(3stap)stappaths(7)
and that's the whole page, periodt ๐
it came to you straight from STAPEX(3stap) โ section 3stap, systemtap's very own manual shelf, exactly where this little legend lives. ily, go write a probe ๐ง๐
๐ฆ where this page comes from
- arch rolling: systemtap 5.6-1 ยท GPL-2.0-or-later
- centos-stream 10: systemtap-client 5.6-3.el10 ยท GPL-2.0-or-later AND GPL-2.0-only AND BSD-3-Clause AND LGPL-2.1-only AND GFDL-1.2-or-later AND BSD-2-Clause
- centos-stream 9: systemtap-client 5.6-3.el9 ยท GPL-2.0-or-later AND GPL-2.0-only AND BSD-3-Clause AND LGPL-2.1-only AND GFDL-1.2-or-later AND BSD-2-Clause
- debian bookworm: systemtap-doc 4.8-2
- debian trixie: systemtap-doc 5.1-5
- fedora 43: systemtap-client 5.3-4.fc43 ยท GPL-2.0-or-later AND GPL-2.0-only AND BSD-3-Clause AND LGPL-2.1-only AND GFDL-1.2-or-later AND BSD-2-Clause
- fedora 43: systemtap-client 5.6-1.fc43 ยท GPL-2.0-or-later AND GPL-2.0-only AND BSD-3-Clause AND LGPL-2.1-only AND GFDL-1.2-or-later AND BSD-2-Clause
- fedora 44: systemtap-client 5.4-3.fc44 ยท GPL-2.0-or-later AND GPL-2.0-only AND BSD-3-Clause AND LGPL-2.1-only AND GFDL-1.2-or-later AND BSD-2-Clause
- fedora 44: systemtap-client 5.6-1.fc44 ยท GPL-2.0-or-later AND GPL-2.0-only AND BSD-3-Clause AND LGPL-2.1-only AND GFDL-1.2-or-later AND BSD-2-Clause
- nixos 26.05: systemtap 5.4
- nixpkgs unstable: systemtap 5.5
- opensuse-leap 16.0: systemtap-docs 5.2-160000.2.2 ยท GPL-2.0-or-later
- ubuntu jammy: systemtap-doc 4.6-2
- ubuntu noble: systemtap-doc 5.0-2ubuntu1
- ubuntu questing: systemtap-doc 5.1-5
- ubuntu resolute: systemtap-doc 5.3-2
- ubuntu stonking: systemtap-doc 5.5-1
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms