此内容没有您所选择的语言版本。
3.3. Basic SystemTap Handler Constructs
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.
3.3.1. Variables
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 a number and can be printed in a printf()
with the integer format specifier (%d
).
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:
Example 3.8. 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 () }
Example 3.8, “timer-jiffies.stp” 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 Example 3.8, “timer-jiffies.stp” (that is 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.