1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
<?xml version='1.0'?>
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
]>
<section id="scriptconstructions">
<title>Basic SystemTap Handler Constructs</title>
<indexterm>
<primary>handlers</primary>
<secondary>SystemTap handler constructs</secondary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>SystemTap handler constructs</secondary>
</indexterm>
<!-- next 2 indexterms for syntax and format -->
<indexterm>
<primary>handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>syntax and format</tertiary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>syntax and format</tertiary>
</indexterm>
<indexterm>
<primary>syntax and format</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<para>SystemTap supports the use of several basic constructs in handlers. The syntax for most of these handler constructs are mostly based on C and <command>awk</command> syntax. This section describes several of the most useful SystemTap handler constructs, which should provide you with enough information to write simple yet useful SystemTap scripts.</para>
<section id="variablesconstructs">
<title>Variables</title>
<!-- next 3 indexterms for variables -->
<indexterm>
<primary>handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>variables</tertiary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>variables</tertiary>
</indexterm>
<indexterm>
<primary>variables</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<indexterm>
<primary>format and syntax</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<para>Variables can be used freely throughout a handler; simply choose a name, assign it to a function, and use it in an expression. SystemTap automatically identifies whether a variable should be identified as a string or integer, based on the function it is assigned to. For instance, if you use set the variable <command>foo</command> to <command>gettimeofday_s()</command> (as in <command>foo = gettimeofday_s()</command>), then <command>foo</command> can be used as an integer argument (<command>%d</command>) in <command>printf()</command>.</para>
<!-- next 2 indexterms for <command>global</command> -->
<indexterm>
<primary>handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary><command>global</command></tertiary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary><command>global</command></tertiary>
</indexterm>
<indexterm>
<primary><command>global</command></primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<para>Note, however, that by default variables are only local to the probe they are used in. This means that variables are initialized, used and disposed at each probe handler invocation. To share a variable between probes, declare the variable name first using <command>global</command> outside of any probe. Consider the following example:</para>
<example id="timerjiffies">
<title>timer-jiffies.stp</title>
<programlisting>
global count_jiffies, count_ms
probe timer.jiffies(100) { count_jiffies ++ }
probe timer.ms(100) { count_ms ++ }
probe timer.ms(12345)
{
hz=(1000*count_jiffies) / count_ms
printf ("jiffies:ms ratio %d:%d => CONFIG_HZ=%d\n",
count_jiffies, count_ms, hz)
exit ()
}
</programlisting>
</example>
<indexterm>
<primary><command>CONFIG_HZ, computing for</command></primary>
</indexterm>
<para><xref linkend="timerjiffies"/> attempts to compute the <command>CONFIG_HZ</command> setting of the kernel using timers that count jiffies and milliseconds, then computing accordingly. The <command>global</command> statement allows the script to use the variables <command>count_jiffies</command> and <command>count_ms</command> (set in their own respective probes) to be shared with <command>probe timer.ms(12345)</command>.</para>
<note>
<title>Note</title>
<para>The <command>++</command> notation in <xref linkend="timerjiffies"/> (i.e. <command>count_jiffies ++</command> and <command>count_ms ++</command>) is used to increment the value of a variable by 1. In the following probe, <command>count_jiffies</command> is incremented by 1 every 100 jiffies:</para>
<screen>
probe timer.jiffies(100) { count_jiffies ++ }
</screen>
<para>In this instance, SystemTap understands that <command>count_jiffies</command> is an integer. Because no initial value was assigned to <command>count_jiffies</command>, its initial value is zero by default.</para>
</note>
<!--
<note>
<title>Note</title>
<para>In some cases, such as in <xref linkend="timerjiffies"/>, a variable may be declared without any specific value as yet. You need to declare such values as integers using the notation <command>++</command>.</para>
</note>
-->
</section>
<section id="handlerconditionalstatements">
<title>Conditional Statements</title>
<indexterm>
<primary>handlers</primary>
<secondary>conditional statements</secondary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>conditional statements</secondary>
</indexterm>
<para>
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.
</para>
<!-- </formalpara> -->
<para>
You can do this by using <emphasis>conditionals</emphasis> in handlers. SystemTap accepts the following types of conditional statements:
</para>
<variablelist>
<varlistentry>
<term>If/Else Statements</term>
<listitem>
<!-- next 3 indexterms for if/else -->
<indexterm>
<primary>handlers</primary>
<secondary>conditional statements</secondary>
<tertiary>if/else</tertiary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>conditional statements</secondary>
<tertiary>if/else</tertiary>
</indexterm>
<indexterm>
<primary>if/else</primary>
<secondary>conditional statements</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<para>Format:</para>
<programlisting>
if (<replaceable>condition</replaceable>)
{<replaceable>statement</replaceable>} else {<replaceable>statement</replaceable>}
</programlisting>
<example id="simpleifelseexample">
<title>ifelse.stp</title>
<programlisting>
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)
}
</programlisting>
</example>
<para><xref linkend="simpleifelseexample"/> is a script that counts how many virtual file system reads (<command>vfs_read</command>) and writes (<command>vfs_write</command>) the system performs within a 5-second span. When run, the script increments the value of the variable <command>countread</command> by 1 if the name of the function it probed matches <command>vfs_read</command> (as noted by the condition <command>if (probefunc()=="vfs_read")</command>); otherwise, it increments <command>countnonread</command> (<command>else {countnonread ++}</command>).</para>
</listitem>
</varlistentry>
<varlistentry>
<term>While Loops</term>
<listitem>
<!-- next 2 indexterms for while loops -->
<indexterm>
<primary>handlers</primary>
<secondary>conditional statements</secondary>
<tertiary>while loops</tertiary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>conditional statements</secondary>
<tertiary>while loops</tertiary>
</indexterm>
<indexterm>
<primary>while loops</primary>
<secondary>conditional statements</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<para>Format:</para>
<programlisting>
while (<replaceable>condition</replaceable>) {<replaceable>statement</replaceable>}
</programlisting>
<!--
<example id="simplewhileexample">
<title>while.stp</title>
<programlisting>
global foo
probe timer.s(1) {
foo ++
while (foo<6) {printf("hello world\n")}
printf("goodbye world\n")
</programlisting>
</example>
<para><xref linkend="simplewhileexample"/> is a script that prints <computeroutput>hello world</computeroutput> while less than 6 seconds has passed (<command>while (foo<6)</command>). Once the <command>while</command> condition no longer applies, the script prints out <computeroutput>goodbye world</computeroutput>.</para>
--></listitem>
</varlistentry>
<varlistentry>
<term>For Loops</term>
<listitem>
<!-- next 2 indexterms for for loops -->
<indexterm>
<primary>handlers</primary>
<secondary>conditional statements</secondary>
<tertiary>for loops</tertiary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>conditional statements</secondary>
<tertiary>for loops</tertiary>
</indexterm>
<indexterm>
<primary>for loops</primary>
<secondary>conditional statements</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<para>Format:</para>
<programlisting>
for (<replaceable>argument1</replaceable>; <replaceable>argument2</replaceable>; <replaceable>argument3</replaceable>) {<replaceable>statement</replaceable>}
</programlisting>
</listitem>
</varlistentry>
<!--<para>Each conditional statement must be enclosed in <command>{ }</command>.</para>-->
<!--
<varlistentry>
<term></term>
<listitem>
<para></para>
</listitem>
</varlistentry>
-->
</variablelist>
<!--
<para>These constructs are better illustrated in the different examples available in <xref linkend="useful-systemtap-scripts"/>.</para>-->
<remark>need simple, simple examples for FOR and WHILE</remark>
<formalpara>
<title>Conditional Operators</title>
<!-- next 2 indexterms for conditional operators -->
<indexterm>
<primary>handlers</primary>
<secondary>conditional statements</secondary>
<tertiary>conditional operators</tertiary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>conditional statements</secondary>
<tertiary>conditional operators</tertiary>
</indexterm>
<indexterm>
<primary>conditional operators</primary>
<secondary>conditional statements</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<para>Aside from <command>==</command> ("is equal to"), you can also use the following operators in your conditional statements:</para>
</formalpara>
<variablelist>
<varlistentry>
<term>>=</term>
<listitem>
<para>Greater than or equal to</para>
</listitem>
</varlistentry>
<varlistentry>
<term><=</term>
<listitem>
<para>Less than or equal to</para>
</listitem>
</varlistentry>
<varlistentry>
<term>!=</term>
<listitem>
<para>Is not equal to</para>
</listitem>
</varlistentry>
</variablelist>
</section>
<section id="commandlineargssect">
<title>Command-Line Arguments</title>
<indexterm>
<primary>handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>command-line arguments</tertiary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>command-line arguments</tertiary>
</indexterm>
<indexterm>
<primary>command-line arguments</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<para>You can also allow a SystemTap script to accept simple command-line arguments and declare them in the script without using <command>target()</command>. One way to do this is to use the variable notation <command>$</command> or <command>@</command>.</para>
<!-- </formalpara> -->
<example id="commandlineargs"><title>commandlineargs.stp</title>
<programlisting>
probe kernel.function(@1) { }
probe kernel.function(@1).return { }
</programlisting>
</example>
<para><xref linkend="commandlineargs"/> is similar to <xref linkend="wildcards"/>, except that it allows you to pass the kernel function to be probed as a command-line argument (as in <command>stap commandlineargs.stp <replaceable>kernel function</replaceable></command>). You can also specify the script to accept multiple command-line arguments, noting them as <command>@1</command>, <command>@2</command>, and so on, in the order they are entered by the user.</para>
<!-- next 2 indexterms for variable notations -->
<indexterm>
<primary>handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>variable notations</tertiary>
</indexterm>
<indexterm>
<primary>SystemTap handlers</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>variable notations</tertiary>
</indexterm>
<indexterm>
<primary>variable notations</primary>
<secondary>SystemTap handler constructs</secondary>
<tertiary>handlers</tertiary>
</indexterm>
<para>Both variable notations <command>$</command> and <command>@</command> also represent a specific variable type. Use <command>$</command> if you are expecting the user to enter an integer as a command-line argument, and <command>@</command> if you are expecting a string.</para>
</section>
<!-- endsection -->
</section>
|