Este conteúdo não está disponível no idioma selecionado.
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 Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 set the variable
var to gettimeofday_s() (as in var = gettimeofday_s()), then var 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
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” (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 ++ }
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.
3.3.2. Conditional Statements Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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
- Format:
if (condition) statement1 else statement2
if (condition) statement1 else statement2Copy to Clipboard Copied! Toggle word wrap Toggle overflow Thestatement1is executed if theconditionexpression is non-zero. Thestatement2is executed if theconditionexpression is zero. Theelseclause (elsestatement2) is optional. Bothstatement1andstatement2can be statement blocks.Example 3.9. ifelse.stp
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example 3.9, “ifelse.stp” 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 variablecountreadby 1 if the name of the function it probed matchesvfs_read(as noted by the conditionif (probefunc()=="vfs_read")); otherwise, it incrementscountnonread(else {countnonread ++}). - While Loops
- Format:
while (condition) statement
while (condition) statementCopy to Clipboard Copied! Toggle word wrap Toggle overflow So long asconditionis non-zero the block of statements instatementare executed. Thestatementis often a statement block and it must change a value soconditionwill eventually be zero. - For Loops
- Format:
for (initialization; conditional; increment) statement
for (initialization; conditional; increment) statementCopy to Clipboard Copied! Toggle word wrap Toggle overflow Theforloop is simply shorthand for a while loop. The following is the equivalentwhileloop:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Conditional Operators
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
3.3.3. Command-Line Arguments Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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.
Example 3.10. commandlineargs.stp
probe kernel.function(@1) { }
probe kernel.function(@1).return { }
probe kernel.function(@1) { }
probe kernel.function(@1).return { }
Example 3.10, “commandlineargs.stp” is similar to Example 3.1, “wildcards.stp”, 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.