Basic SystemTap Handler Constructs
handlers
SystemTap handler constructs
SystemTap handlers
SystemTap handler constructs
handlers
SystemTap handler constructs
syntax and format
SystemTap handlers
SystemTap handler constructs
syntax and format
syntax and format
SystemTap handler constructs
handlers
SystemTap supports the use of several basic constructs in handlers. The syntax for most of these handler constructs are mostly based on C and awk syntax. This section describes several of the most useful SystemTap handler constructs, which should provide you with enough information to write simple yet useful SystemTap scripts.
Variables
handlers
SystemTap handler constructs
variables
SystemTap statements
SystemTap handler constructs
variables
variables
SystemTap handler constructs
handlers
format and syntax
SystemTap handler constructs
handlers
Variables can be used freely throughout a handler; simply choose a
name, assign a value from a function or expression to it, and use it in an expression. SystemTap automatically identifies whether a variable should be typed as a string or integer, based on the type of the values assigned to it. For instance, if you use set the variable foo to gettimeofday_s() (as in foo = gettimeofday_s()), then foo is typed as an number and can be printed in a printf() with the integer format specifier (%d).
handlers
SystemTap handler constructs
global
SystemTap statements
SystemTap handler constructs
global
global
SystemTap handler constructs
handlers
Note, however, that by default variables are only local to the probe they are used in. This means that variables are initialized, used and disposed at each probe handler invocation. To share a variable between probes, declare the variable name using global outside of the probes. Consider the following example:
timer-jiffies.stp
global count_jiffies, count_ms
probe timer.jiffies(100) { count_jiffies ++ }
probe timer.ms(100) { count_ms ++ }
probe timer.ms(12345)
{
hz=(1000*count_jiffies) / count_ms
printf ("jiffies:ms ratio %d:%d => CONFIG_HZ=%d\n",
count_jiffies, count_ms, hz)
exit ()
}
CONFIG_HZ, computing for
computes the CONFIG_HZ setting of the kernel using timers that count jiffies and milliseconds, then computing accordingly. The global statement allows the script to use the variables count_jiffies and count_ms (set in their own respective probes) to be shared with probe timer.ms(12345).
Note
The ++ notation in (i.e. count_jiffies ++ and count_ms ++) is used to increment the value of a variable by 1. In the following probe, count_jiffies is incremented by 1 every 100 jiffies:
probe timer.jiffies(100) { count_jiffies ++ }
In this instance, SystemTap understands that count_jiffies is an integer. Because no initial value was assigned to count_jiffies, its initial value is zero by default.
Conditional Statements
handlers
conditional statements
SystemTap statements
conditional statements
In some cases, the output of a SystemTap script may be too big. To address this, you need to further refine the script's logic in order to delimit the output into something more relevant or useful to your probe.
You can do this by using conditionals in handlers. SystemTap accepts the following types of conditional statements:
If/Else Statements
handlers
conditional statements
if/else
SystemTap statements
conditional statements
if/else
if/else
conditional statements
handlers
Format:
if (condition)
statement1
else
statement2
The statement1 is executed if the
condition expression is
non-zero. The statement2 is
executed if the condition
expression is zero. The else clause
(else statement2)is optional. Both
statement1 and
statement2 can be statement
blocks.
ifelse.stp
global countread, countnonread
probe kernel.function("vfs_read"),kernel.function("vfs_write")
{
if (probefunc()=="vfs_read")
countread ++
else
countnonread ++
}
probe timer.s(5) { exit() }
probe end
{
printf("VFS reads total %d\n VFS writes total %d\n", countread, countnonread)
}
is a script that counts how many virtual file system reads (vfs_read) and writes (vfs_write) the system performs within a 5-second span. When run, the script increments the value of the variable countread by 1 if the name of the function it probed matches vfs_read (as noted by the condition if (probefunc()=="vfs_read")); otherwise, it increments countnonread (else {countnonread ++}).
While Loops
handlers
conditional statements
while loops
SystemTap statements
conditional statements
while loops
while loops
conditional statements
handlers
Format:
while (condition)
statement
So long as condition is non-zero
the block of statements in
statement are executed. The
statement is often a statement
block and it must change a value so
condition will eventually be zero.
For Loops
handlers
conditional statements
for loops
SystemTap statements
conditional statements
for loops
for loops
conditional statements
handlers
Format:
for (initialization; conditional; increment) statement
The for loop is simply shorthand for a while loop. The
following is the equivalent while loop:
initialization
while (conditional) {
statement
increment
}
need simple, simple examples for FOR and WHILE
Conditional Operators
handlers
conditional statements
conditional operators
SystemTap statements
conditional statements
conditional operators
conditional operators
conditional statements
handlers
Aside from == ("is equal to"), you can also use the following operators in your conditional statements:
>=
Greater than or equal to
<=
Less than or equal to
!=
Is not equal to
Command-Line Arguments
handlers
SystemTap handler constructs
command-line arguments
SystemTap statements
SystemTap handler constructs
command-line arguments
command-line arguments
SystemTap handler constructs
handlers
You can also allow a SystemTap script to accept simple command-line arguments using a $ or @ immediately
followed by the number of the argument on the command line. Use $ if you are expecting the user to enter an integer as a command-line argument, and @ if you are expecting a string.
commandlineargs.stp
probe kernel.function(@1) { }
probe kernel.function(@1).return { }
is similar to , except that it allows you to pass the kernel function to be probed as a command-line argument (as in stap commandlineargs.stp kernel function). You can also specify the script to accept multiple command-line arguments, noting them as @1, @2, and so on, in the order they are entered by the user.
handlers
SystemTap handler constructs
variable notations
SystemTap statements
SystemTap handler constructs
variable notations
variable notations
SystemTap handler constructs
handlers