next up previous contents index
Next: 7 Associative arrays Up: SystemTap Language Reference Previous: 5 Language elements   Contents   Index

Subsections


6 Statement types

Statements enable procedural control flow within functions and probe handlers. The total number of statements executed in response to any single probe event is limited to MAXACTION, which defaults to 1000. See Section [*].


6.1 break and continue

Use break or continue to exit or iterate the innermost nesting loop statement, such as within a while, for, or foreach statement. The syntax and semantics are the same as those used in C.


6.2 try/catch

Use try/catch to handle most kinds of run-time errors within the script instead of aborting the probe handler in progress. The semantics are similar to C++ in that try/catch blocks may be nested. The error string may be captured by optionally naming a variable which is to receive it.

try { 
   /* do something */
   /* trigger error like kread(0), or divide by zero, or error("foo") */
} catch (msg) { /* omit (msg) entirely if not interested */
   /* println("caught error ", msg) */
   /* handle error */
}
/* execution continues */


6.3 delete

delete removes an element.

The following statement removes from ARRAY the element specified by the index tuple. The value will no longer be available, and subsequent iterations will not report the element. It is not an error to delete an element that does not exist.

delete ARRAY[INDEX1, INDEX2, ...]
The following syntax removes all elements from ARRAY:

delete ARRAY
The following statement removes the value of SCALAR. Integers and strings are cleared to zero and null ("") respectively, while statistics are reset to their initial empty state.

delete SCALAR


6.4 EXP (expression)

An expression executes a string- or integer-valued expression and discards the value.


6.5 for

General syntax:
for (EXP1; EXP2; EXP3) STMT
The for statement is similar to the for statement in C. The for expression executes EXP1 as initialization. While EXP2 is non-zero, it executes STMT, then the iteration expression EXP3.


6.6 foreach

General syntax:
foreach (VAR in ARRAY) STMT
The foreach statement loops over each element of a named global array, assigning the current key to VAR. The array must not be modified within the statement. If you add a single plus (+) or minus (-) operator after the VAR or the ARRAY identifier, the iteration order will be sorted by the ascending or descending index or value.

The following statement behaves the same as the first example, except it is used when an array is indexed with a tuple of keys. Use a sorting suffix on at most one VAR or ARRAY identifier.

foreach ([VAR1, VAR2, ...] in ARRAY) STMT

You can combine the first and second syntax to capture both the full tuple and the keys at the same time as follows.

foreach (VAR = [VAR1, VAR2, ...] in ARRAY) STMT

The following statement is the same as the first example, except that the limit keyword limits the number of loop iterations to EXP times. EXP is evaluated once at the beginning of the loop.

foreach (VAR in ARRAY limit EXP) STMT


6.7 if

General syntax:

if (EXP) STMT1 [ else STMT2 ]
The if statement compares an integer-valued EXP to zero. It executes the first STMT if non-zero, or the second STMT if zero.

The if command has the same syntax and semantics as used in C.


6.8 next

The next statement returns immediately from the enclosing probe handler.


6.9 ; (null statement)

General syntax:

statement1
;
statement2
The semicolon represents the null statement, or do nothing. It is useful as an optional separator between statements to improve syntax error detection and to handle certain grammar ambiguities.


6.10 return

General syntax:

return EXP
The return statement returns the EXP value from the enclosing function. If the value of the function is not returned, then a return statement is not needed, and the function will have a special unknown type with no return value.


6.11 { } (statement block)

This is the statement block with zero or more statements enclosed within brackets. The following is the general syntax:

{ STMT1 STMT2 ... }
The statement block executes each statement in sequence in the block. Separators or terminators are generally not necessary between statements. The statement block uses the same syntax and semantics as in C.


6.12 while

General syntax:

while (EXP) STMT
The while statement uses the same syntax and semantics as in C. In the statement above, while the integer-valued EXP evaluates to non-zero, the parser will execute STMT.


next up previous contents index
Next: 7 Associative arrays Up: SystemTap Language Reference Previous: 5 Language elements   Contents   Index