Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

3.3.2. 条件付き (conditional) ステートメント

場合によっては、SystemTap スクリプトの出力が大きすぎることがあるかもしれません。これに対処するには、スクリプトの論理を細分化して、出力をプローブに関連するもしくは有用なものにする必要があります。
これはハンドラーで 条件 を使うことで実行できます。SystemTap は以下のタイプの条件付きステートメントを受け付けます。
If/Else ステートメント
書式は以下のようになります。
if (condition)
  statement1
else
  statement2
condition 式がゼロ以外の場合、statement1 が実行されます。condition 式がゼロの場合、statement2 が実行されます。else 節 (else statement2) はオプションです。statement1statement2 は両方とも、ステートメントブロックになります。

例3.9 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)
}
例3.9「ifelse.stp」 は、5 秒間にシステムが実行する仮想ファイルシステムの読み込み (vfs_read) と書き込み (vfs_write) 回数をカウントするスクリプトです。この実行時には、プローブした関数の名前が (条件 if (probefunc()=="vfs_read") の) vfs_read と一致する場合、スクリプトは変数 countread の値を 1 つ増やします。一致しない場合は、countnonread (else {countnonread ++}) を増やします。
While ループ
書式は以下のようになります。
while (condition)
  statement
condition がゼロ以外であれば、statement 内のステートメントのブロックは実行されます。statement はステートメントブロックであることが多く、condition が最終的にゼロとなるように値を変更する必要があります。
For ループ
書式は以下のようになります。
for (initialization; conditional; increment) statement
for ループは、単に while ループの短縮形です。以下が同等の while ループになります。
initialization
while (conditional) {
   statement
   increment
}
条件演算子

等号 == のほかに、以下の演算子も条件付きステートメントに使用できます。

>=
より大か等しい
<=
より小か等しい
!=
等しくない