if (condition) statement1 else statement2
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.
Example 3.11. 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) }
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 (condition) statement
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 (initialization; conditional; increment) statement
for
loop is shorthand for a while loop. The
following is the equivalent while
loop:
initialization while (conditional) { statement increment }
==
("is equal to"), following operators can also be used in conditional statements: