此内容没有您所选择的语言版本。
3.3.2. 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
- 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