Most systemtap scripts include conditionals, to limit tracing or other logic to those processes or users or whatever of interest. The syntax is simple:
if (EXPR) STATEMENT [else STATEMENT] |
if/else statement |
while (EXPR) STATEMENT |
while loop |
for (A; B; C) STATEMENT |
for loop |
Scripts may use break/continue as in C.
Probe handlers can return early using next as in awk.
Blocks of statements are enclosed in { and }. In
systemtap, the semicolon (;) is accepted as a null statement
rather than as a statement terminator, so is only rarely1necessary. Shell-style (#), C-style (/* */), and
C++-style (//) comments are all accepted.
Expressions look like C or awk, and support the usual
operators, precedences, and numeric literals. Strings are treated as
atomic values rather than arrays of characters. String concatenation
is done with the dot ("a" . "b"). Some examples:
(uid() > 100) |
probably an ordinary user |
(execname() == "sed") |
current process is sed |
(cpu() == 0 && gettimeofday_s() > 1140498000) |
after Feb. 21, 2006, on CPU 0 |
"hello" . " " . "world" |
a string in three easy pieces |
Variables may be used as well. Just pick a name, assign to it, and use it in expressions. They are automatically initialized and declared. The type of each identifier - string vs. number - is automatically inferred by systemtap from the kinds of operators and literals used on it. Any inconsistencies will be reported as errors. Conversion between string and number types is done through explicit function calls.
foo = gettimeofday_s() |
foo is a number |
bar = "/usr/bin/" . execname() |
bar is a string |
c++ |
c is a number |
s = sprint(2345) |
s becomes the string "2345" |
By default, variables are local to the probe they are used in. That is, they are initialized, used, and disposed of at each probe handler invocation. To share variables between probes, declare them global anywhere in the script. Because of possible concurrency (multiple probe handlers running on different CPUs), each global variable used by a probe is automatically read- or write-locked while the handler is running.