スクリプト変数
スクリプト変数は、自動、スレッド・ローカル、またはグローバル・クラス変数のいずれかです。
スクリプト変数は、 Vue スクリプトのコンテキスト内にのみ存在し、その値はスクリプトから割り当てられます。 また、これらの変数にアクセスしたりこれらの変数を変更したりできるのは、これらの変数を定義するスクリプト内に限られます。
通常、スクリプト変数のデータ型は宣言ステートメントを使用して明示的に宣言する必要があります。 ただし、変数への最初の参照が代入演算子の左辺に変数をもつ代入操作である場合は、コンパイラーでプログラム変数のデータ型を暗黙的に判別することができます。
整数型の暗黙的な型判別
整数型を代入するには、代入の右辺が次のいずれかの状態でなければなりません。
- 定数。
- 整数型の別の変数 (組み込み変数を含む)。 型が不明な変数からの代入はエラーになります。
- diff_time 関数のような整数型を戻す Vue 関数です。
- 右辺の式を整数型にキャストする。ただし、警告が出される場合があります。
- 前述の状態に関連した式。
変数はその型と値を右辺の式に基づいて取ります。 また、変数のクラスを変数の前に指定することで変数に代入することができます。 次のスクリプトでは、いくつかの例を示します。
/*
* File: implicit2.e
* Usage: Demonstrates implicit assignment for integer types
*/
int read(int fd, char *p, long size);
@@BEGIN
{
count = 404; /* count: int of global class */
zcount = 2 * (count - 4); /* zcount: int of global class */
llcount = 33459182089021LL; /* lcount: long long of global class */
lxcount = 0xF00000000245B20LL; /* xcount: long long of global class */
}
@@syscall:$1:read:entry
{
__auto probev_timestamp_t ts1, ts2;
int gsize;
ts1 = timestamp();
auto:dcount = llcount - lxcount; /* dcount: long long of auto class */
auto:mypid = __pid; /* mypid: pid_t (64-bit integer) of automatic class */
fd = __arg1; /* fd: int of global class */
/* The following cast will likely cause a compiler warning
* but can be ignored here
*/
global:bufaddr = (long)__arg2; /* bufaddr: long of global class */
gsize = __arg3;
thread:size = gsize + 400; /* size: int of thread-local class */
printf("count = %d, zcount = %lld\n", count, zcount);
printf("llcount = %lld, lxcount = 0x%016llx, diff = %lld\n",
llcount, lxcount, dcount);
printf("mypid = %ld, fd = %d, size = %d\n", mypid, fd, size);
printf("bufaddr = 0x%08x\n", bufaddr);
ts2 = timestamp();
auto:diff = diff_time(ts1, ts2, MICROSECONDS); /* diff: int of automatic class */
printf("Time to execute = %d microseconds\n", diff);
exit();
}
プロセス ID が 250000 のプロセスをプローブすると仮定して、次のスクリプトでは implicit2.e スクリプトの実行例を示します。
# probevue implicit2.e 250000
WRN-100: Line:29 Column:26 Incompatible cast
count = 404, zcount = 800
llcount = 33459182089021, lxcount = 0x0f00000000245b20, diff = -1080830451389212643
mypid = 250000, fd = 10, size = 4496
bufaddr = 0x20033c00
Time to execute = 11 microseconds
前述の例では、スクリプト内の $1 シンボルが "250000" に自動的に置き換えられ、プロセス ID が 250000 であるプロセスに、読み取りシステム・コールのエントリー・プローブ・ポイントが限定されます。
ストリング・タイプの暗黙タイプ決定
文字列型を代入するには、代入の右辺が次のいずれかの状態でなければなりません。
- 文字列リテラル。二重引用符で囲まれた 一連の文字列。
- string (文字列) 型の別の変数 (組み込み変数を含む)。
- et_userstring 関数のような文字列を返す Vue 関数。
- 前述の状態に関連した式。
次の例では、暗黙の文字列型の代入を示します。
/*
* File: implicit3.e
* Usage: Demonstrates implicit assignment for string types
*/
int write(int fd, char *p, long size);
@@BEGIN
{
s1 = "Write system call:\n";
}
@@syscall:$1:write:entry
{
String s2[40];
wbuf = get_userstring(__arg2, __arg3);
s2 = s1;
zbuf = s2;
pstring = zbuf + wbuf;
printf("%s\n", pstring);
}
@@syscall:$1:write:exit
{
ename = __pname;
printf("Exec name = %s\n", ename);
exit();
}
$1 シェル定位置パラメーター変数を置き換えるには、スクリプトの実行時にプロセス ID を引数としてスクリプトに渡す必要があります。
リスト・タイプの暗黙タイプ決定
リスト型を代入するには、代入の右辺が list() 関数でなければなりません。 list() 関数はどの節からでも使用できます。