Debian Codegraph @

doc/SystemTap_Beginners_Guide/en-US/Scripts.xml

raw
1 <?xml version='1.0'?>
2 <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
3 ]>
4
5 <section id="scripts">
6 <title>SystemTap Scripts</title>
7 <indexterm>
8 <primary>scripts</primary>
9 <secondary>introduction</secondary>
10 </indexterm>
11 <indexterm>
12 <primary>SystemTap scripts</primary>
13 <secondary>introduction</secondary>
14 </indexterm>
15
16
17
18 <para>
19 For the most part, SystemTap scripts are the foundation of each SystemTap
20 session. SystemTap scripts instruct SystemTap on what type of information to
21 collect, and what to do once that information is collected.
22 </para>
23 <indexterm>
24 <primary>scripts</primary>
25 <secondary>introduction</secondary>
26 <tertiary>components</tertiary>
27 </indexterm>
28 <indexterm>
29 <primary>SystemTap scripts</primary>
30 <secondary>introduction</secondary>
31 <tertiary>components</tertiary>
32 </indexterm>
33
34 <indexterm>
35 <primary>components</primary>
36 <secondary>SystemTap scripts</secondary>
37 <tertiary>introduction</tertiary>
38 </indexterm>
39
40 <indexterm>
41 <primary>scripts</primary>
42 <secondary>introduction</secondary>
43 <tertiary>events and handlers</tertiary>
44 </indexterm>
45 <indexterm>
46 <primary>SystemTap scripts</primary>
47 <secondary>introduction</secondary>
48 <tertiary>events and handlers</tertiary>
49 </indexterm>
50 <indexterm>
51 <primary>handlers and events</primary>
52 <secondary>SystemTap scripts</secondary>
53 <tertiary>introduction</tertiary>
54 </indexterm>
55 <para>
56 As stated in <xref linkend="understanding-how-systemtap-works"/>, SystemTap
57 scripts are made up of two components: <emphasis>events</emphasis> and
58 <emphasis>handlers</emphasis>. Once a SystemTap session is underway,
59 SystemTap monitors the operating system for the specified events and
60 executes the handlers as they occur.
61 </para>
62
63 <note>
64 <title>Note</title>
65 <indexterm>
66 <primary>scripts</primary>
67 <secondary>introduction</secondary>
68 <tertiary>probes</tertiary>
69 </indexterm>
70 <indexterm>
71 <primary>SystemTap scripts</primary>
72 <secondary>introduction</secondary>
73 <tertiary>probes</tertiary>
74 </indexterm>
75 <indexterm>
76 <primary>probes</primary>
77 <secondary>SystemTap scripts</secondary>
78 <tertiary>introduction</tertiary>
79 </indexterm>
80 <para>
81 An event and its corresponding handler is collectively called a
82 <emphasis>probe</emphasis>. A SystemTap script can have multiple probes.
83 </para>
84
85 <para>
86 A probe's handler is commonly referred to as a <emphasis>probe
87 body</emphasis>.
88 </para>
89 </note>
90
91 <para>
92 In terms of application development, using events and handlers is similar to
93 instrumenting the code by inserting diagnostic print statements in a
94 program's sequence of commands. These diagnostic print statements allow you
95 to view a history of commands executed once the program is run.
96 </para>
97 <!-- <para>
98 In terms of application development, using events and handlers is similar to inserting <command>print</command> statements in a program's sequence of commands. These <command>print</command> statements allow you to view a history of commands executed once the program is run.
99 </para> -->
100
101 <para>
102 SystemTap scripts allow insertion of the instrumentation code without
103 recompilation of the code and allows more flexibility with regard to
104 handlers. Events serve as the triggers for handlers to run; handlers can be
105 specified to record specified data and print it in a certain manner.
106 </para>
107
108 <formalpara id="scriptformats">
109 <title>Format</title>
110 <indexterm>
111 <primary>scripts</primary>
112 <secondary>introduction</secondary>
113 <tertiary>format and syntax</tertiary>
114 </indexterm>
115 <indexterm>
116 <primary>SystemTap scripts</primary>
117 <secondary>introduction</secondary>
118 <tertiary>format and syntax</tertiary>
119 </indexterm>
120 <indexterm>
121 <primary>format and syntax</primary>
122 <secondary>SystemTap scripts</secondary>
123 <tertiary>introduction</tertiary>
124 </indexterm>
125 <indexterm>
126 <primary>syntax and format</primary>
127 <secondary>SystemTap scripts</secondary>
128 <tertiary>introduction</tertiary>
129 </indexterm>
130 <para>
131 SystemTap scripts use the file extension <filename>.stp</filename>, and
132 contains probes written in the following format:
133 </para>
134 </formalpara>
135 <screen>probe <replaceable>event</replaceable> {<replaceable>statements</replaceable>}</screen>
136
137 <para>
138 SystemTap supports multiple events per probe; multiple events are delimited
139 by a comma (<command>,</command>). If multiple events are specified in a
140 single probe, SystemTap will execute the handler when any of the specified
141 events occur.
142 </para>
143 <indexterm>
144 <primary>scripts</primary>
145 <secondary>introduction</secondary>
146 <tertiary>statement blocks</tertiary>
147 </indexterm>
148 <indexterm>
149 <primary>SystemTap scripts</primary>
150 <secondary>introduction</secondary>
151 <tertiary>statement blocks</tertiary>
152 </indexterm>
153 <indexterm>
154 <primary>statement blocks</primary>
155 <secondary>SystemTap scripts</secondary>
156 <tertiary>introduction</tertiary>
157 </indexterm>
158 <para>
159 Each probe has a corresponding <firstterm>statement block</firstterm>. This statement block is
160 enclosed in braces (<command>{ }</command>) and contains the statements to be executed per event.
161 SystemTap executes these statements in sequence; special separators or
162 terminators are generally not necessary between multiple statements.
163 </para>
164
165 <note>
166 <title>Note</title>
167 <para>
168 Statement blocks in SystemTap scripts follow the same syntax and semantics as the C
169 programming language. A statement block can be nested within another statement block.
170 </para>
171 </note>
172 <indexterm>
173 <primary>scripts</primary>
174 <secondary>introduction</secondary>
175 <tertiary>functions</tertiary>
176 </indexterm>
177 <indexterm>
178 <primary>SystemTap scripts</primary>
179 <secondary>introduction</secondary>
180 <tertiary>functions</tertiary>
181 </indexterm>
182 <indexterm>
183 <primary>functions</primary>
184 <secondary>SystemTap scripts</secondary>
185 <tertiary>introduction</tertiary>
186 </indexterm>
187 <para>
188 SystemTap allows you to write functions to factor out code to be used by a
189 number of probes. Thus, rather than repeatedly writing the same
190 series of statements in multiple probes, you can just place the instructions
191 in a <firstterm>function</firstterm>, as in:
192 </para>
193
194 <screen>function <replaceable>function_name</replaceable>(<replaceable>arguments</replaceable>) {<replaceable>statements</replaceable>}
195 probe <replaceable>event</replaceable> {<replaceable>function_name</replaceable>(<replaceable>arguments</replaceable>)}</screen>
196
197 <para>
198 The <command><replaceable>statements</replaceable></command> in
199 <replaceable>function_name</replaceable> are executed when the probe for
200 <replaceable>event</replaceable> executes. The
201 <replaceable>arguments</replaceable> are optional values passed into the
202 function.
203 </para>
204
205 <!--
206 <para>The <replaceable>exit()</replaceable> condition is optional; this condition safely terminates the session once the script successfully collects the required information the first time.</para>
207 -->
208 <important>
209 <title>Important</title>
210 <para>
211 <xref linkend="scripts"/> is designed to introduce readers to the basics
212 of SystemTap scripts. To understand SystemTap scripts better, it is
213 advisable that you refer to <xref linkend="useful-systemtap-scripts"/>;
214 each section therein provides a detailed explanation of the script, its
215 events, handlers, and expected output.
216 </para>
217 </important>
218
219 <section id="systemtapscript-events">
220 <title>Event</title>
221 <indexterm>
222 <primary>Events</primary>
223 <secondary>introduction</secondary>
224 </indexterm>
225 <para>
226 SystemTap events can be broadly classified into two types:
227 <firstterm>synchronous</firstterm> and
228 <firstterm>asynchronous</firstterm>.
229 </para>
230
231 <formalpara>
232 <title>Synchronous Events</title>
233 <indexterm>
234 <primary>Events</primary>
235 <secondary>synchronous events</secondary>
236 </indexterm>
237
238 <indexterm>
239 <primary>synchronous events</primary>
240 <secondary>Events</secondary>
241 </indexterm>
242 <para>
243 A <firstterm>synchronous</firstterm> event occurs when any process
244 executes an instruction at a particular location in kernel
245 code. This gives other events a reference point from which more
246 contextual data may be available.
247 </para>
248 </formalpara>
249
250 <!--<para>A <firstterm>synchronous</firstterm> event occurs when any processor executes an instruction matched by the specification. This gives other events a reference point (or instruction address) from which more contextual data may be available.</para>-->
251
252 <!--<para>Synchronous events reference particular locations in kernel code. As a result, when synchronous events are used SystemTap can determine contextual information regarding the location (such as function parameters).</para>-->
253 <indexterm>
254 <primary>Events</primary>
255 <secondary>examples of synchronous and asynchronous events</secondary>
256 </indexterm>
257
258 <indexterm>
259 <primary>examples of synchronous and asynchronous events</primary>
260 <secondary>Events</secondary>
261 </indexterm>
262 <para>Examples of synchronous events include:</para>
263
264 <variablelist>
265
266 <varlistentry>
267 <term>syscall.<replaceable>system_call</replaceable></term>
268 <listitem>
269 <indexterm>
270 <primary>Events</primary>
271 <secondary><command>syscall.<replaceable>system_call</replaceable></command></secondary>
272 </indexterm>
273
274 <indexterm>
275 <primary><command>syscall.<replaceable>system_call</replaceable></command></primary>
276 <secondary>Events</secondary>
277 </indexterm>
278
279 <para>
280 The entry to the system call
281 <replaceable>system_call</replaceable>. If the exit from a syscall
282 is desired, appending a <command>.return</command> to the event
283 monitor the exit of the system call instead. For example, to specify
284 the entry and exit of the system call <command>close</command>, use
285 <command>syscall.close</command> and
286 <command>syscall.close.return</command> respectively.
287 </para>
288 </listitem>
289 </varlistentry>
290
291 <varlistentry>
292 <term>vfs.<replaceable>file_operation</replaceable></term>
293 <listitem>
294 <indexterm>
295 <primary>Events</primary>
296 <secondary><command>vfs.<replaceable>file_operation</replaceable></command></secondary>
297 </indexterm>
298
299 <indexterm>
300 <primary><command>vfs.<replaceable>file_operation</replaceable></command></primary>
301 <secondary>Events</secondary>
302 </indexterm>
303
304 <para>
305 The entry to the <replaceable>file_operation</replaceable> event for
306 Virtual File System (VFS). Similar to <command>syscall</command>
307 event, appending a <command>.return</command> to the event monitors
308 the exit of the <replaceable>file_operation</replaceable> operation.
309 </para>
310 </listitem>
311 </varlistentry>
312
313 <varlistentry>
314 <term>kernel.function("<replaceable>function</replaceable>")</term>
315 <listitem>
316 <indexterm>
317 <primary>Events</primary>
318 <secondary><command>kernel.function("<replaceable>function</replaceable>")</command></secondary>
319 </indexterm>
320
321 <indexterm>
322 <primary><command>kernel.function("<replaceable>function</replaceable>")</command></primary>
323 <secondary>Events</secondary>
324 </indexterm>
325 <para>
326 The entry to the kernel function
327 <replaceable>function</replaceable>. For example,
328 <command>kernel.function("sys_open")</command> refers to the "event"
329 that occurs when the kernel function <command>sys_open</command> is
330 called by any thread in the system. To specify the
331 <emphasis>return</emphasis> of the kernel function
332 <command>sys_open</command>, append the <command>return</command>
333 string to the event statement;
334 that is, <command>kernel.function("sys_open").return</command>.
335 </para>
336 <indexterm>
337 <primary>Events</primary>
338 <secondary>wildcards</secondary>
339 </indexterm>
340
341 <indexterm>
342 <primary>wildcards in events</primary>
343 </indexterm>
344 <indexterm>
345 <primary>events wildcards</primary>
346 </indexterm>
347 <para>
348 When defining probe events, you can use asterisk (<literal>*</literal>)
349 for wildcards. You can also trace the entry or exit of a function in
350 a kernel source file. Consider the following example:
351 </para>
352
353 <example id="wildcards">
354 <title>wildcards.stp</title>
355 <programlisting>probe kernel.function("*@net/socket.c") { }
356 probe kernel.function("*@net/socket.c").return { }</programlisting>
357 </example>
358
359 <remark>Wild cards also work for other types of events, for example syscall.*</remark>
360
361 <para>
362 In the previous example, the first probe's event specifies the entry
363 of ALL functions in the kernel source file
364 <filename>net/socket.c</filename>. The second probe specifies the
365 exit of all those functions. Note that in this example,
366 there are no statements in the handler;
367 as such, no information will be collected or displayed.
368 </para>
369 </listitem>
370
371 </varlistentry>
372
373 <varlistentry>
374 <term>kernel.trace("<replaceable>tracepoint</replaceable>")</term>
375 <listitem>
376 <indexterm><primary>tracepoint</primary></indexterm>
377 <indexterm>
378 <primary>Events</primary>
379 <secondary><command>kernel.trace("<replaceable>tracepoint</replaceable>")</command></secondary>
380 </indexterm>
381
382 <indexterm>
383 <primary><command>kernel.trace("<replaceable>tracepoint</replaceable>")</command></primary>
384 <secondary>Events</secondary>
385 </indexterm>
386 <para>
387 The static probe for <replaceable>tracepoint</replaceable>.
388 Recent kernels (2.6.30 and newer)
389 include instrumentation for specific events in the kernel. These
390 events are statically marked with tracepoints. One example of a
391 tracepoint available in systemtap is
392 <command>kernel.trace("kfree_skb")</command> which indicates each
393 time a network buffer is freed in the kernel.
394 </para>
395 </listitem>
396
397 </varlistentry>
398
399 <varlistentry>
400 <term>module("<replaceable>module</replaceable>").function("<replaceable>function</replaceable>")</term>
401 <listitem>
402 <indexterm>
403 <primary>Events</primary>
404 <secondary><command>module("<replaceable>module</replaceable>")</command></secondary>
405 </indexterm>
406
407 <indexterm>
408 <primary><command>module("<replaceable>module</replaceable>")</command></primary>
409 <secondary>Events</secondary>
410 </indexterm>
411 <para>Allows you to probe functions within modules. For example:</para>
412
413 <example id="eventsmodules"><title>moduleprobe.stp</title>
414 <programlisting>probe module("ext3").function("*") { }
415 probe module("ext3").function("*").return { }</programlisting>
416 </example>
417
418 <para>
419 The first probe in <xref linkend="eventsmodules"/>
420 points to the entry of <emphasis>all</emphasis> functions for
421 the <filename>ext3</filename> module. The second probe points to
422 the exits of all functions for that same module; the use of the
423 <command>.return</command> suffix is similar to
424 <command>kernel.function()</command>. Note that the probes in
425 <xref linkend="eventsmodules"/> do not contain statements
426 in the probe handlers, and as such will not print any useful
427 data (as in <xref linkend="wildcards"/>).
428 </para>
429
430 <para>
431 A system's kernel modules are typically located in <filename>/lib/modules/<replaceable>kernel_version</replaceable></filename>, where <replaceable>kernel_version</replaceable> refers to the currently loaded kernel version. Modules use the file name extension <filename>.ko</filename>.
432 </para>
433
434 </listitem>
435 </varlistentry>
436 </variablelist>
437
438 <formalpara>
439 <title>Asynchronous Events</title>
440 <indexterm>
441 <primary>Events</primary>
442 <secondary>asynchronous events</secondary>
443 </indexterm>
444
445 <indexterm>
446 <primary>asynchronous events</primary>
447 <secondary>Events</secondary>
448 </indexterm>
449
450 <para>
451 <firstterm>Asynchronous</firstterm> events are not tied to a particular
452 instruction or location in code. This family of probe points consists
453 mainly of counters, timers, and similar constructs.
454 </para>
455 <!-- <para><firstterm>Asynchronous</firstterm> events, on the other hand, do not point to any reference point. This family of probe points consists mainly of counters, timers, and similar constructs.</para>-->
456 </formalpara>
457
458 <para>Examples of asynchronous events include:</para>
459
460 <variablelist>
461
462 <varlistentry>
463 <term>begin</term>
464 <listitem>
465 <indexterm>
466 <primary>Events</primary>
467 <secondary><command>begin</command></secondary>
468 </indexterm>
469
470 <indexterm>
471 <primary><command>begin</command></primary>
472 <secondary>Events</secondary>
473 </indexterm>
474 <para>
475 The startup of a SystemTap session; that is, as soon as the SystemTap
476 script is run.
477 </para>
478 </listitem>
479 </varlistentry>
480
481 <varlistentry>
482 <term>end</term>
483 <listitem>
484 <indexterm>
485 <primary>Events</primary>
486 <secondary><command>end</command></secondary>
487 </indexterm>
488
489 <indexterm>
490 <primary><command>end</command></primary>
491 <secondary>Events</secondary>
492 </indexterm>
493 <para>The end of a SystemTap session.</para>
494 </listitem>
495 </varlistentry>
496 <varlistentry>
497 <term>timer events</term>
498 <listitem>
499 <indexterm>
500 <primary>Events</primary>
501 <secondary>timer events</secondary>
502 </indexterm>
503
504 <indexterm>
505 <primary>timer events</primary>
506 <secondary>Events</secondary>
507 </indexterm>
508
509 <para>
510 An event that specifies a handler to be executed periodically.
511 For example:
512 </para>
513
514 <example id="timer"><title>timer-s.stp</title>
515 <programlisting>probe timer.s(4)
516 {
517 printf("hello world\n")
518 }</programlisting>
519 </example>
520
521 <para>
522 <xref linkend="timer"/> is an example of a probe that prints
523 <command>hello world</command> every 4 seconds. It is also possible
524 to use the following timer events:
525 </para>
526
527 <itemizedlist>
528 <listitem><para><command>timer.ms(<replaceable>milliseconds</replaceable>)</command></para></listitem>
529
530 <listitem><para><command>timer.us(<replaceable>microseconds</replaceable>)</command></para></listitem>
531
532 <listitem><para><command>timer.ns(<replaceable>nanoseconds</replaceable>)</command></para></listitem>
533
534 <listitem><para><command>timer.hz(<replaceable>hertz</replaceable>)</command></para></listitem>
535
536 <listitem><para><command>timer.jiffies(<replaceable>jiffies</replaceable>)</command></para></listitem>
537 </itemizedlist>
538
539 <para>
540 When used in conjunction with other probes that collect information,
541 timer events allows you to print out get periodic updates and see
542 how that information changes over time.
543 </para>
544
545 </listitem>
546 </varlistentry>
547
548 </variablelist>
549
550 <important>
551 <title>Important</title>
552 <para>
553 SystemTap supports the use of a large collection of probe events. For
554 more information about supported events, refer to <command>man
555 stapprobes</command>. The <citetitle>SEE ALSO</citetitle> section of
556 <command>man stapprobes</command> also contains links to other
557 <command>man</command> pages that discuss supported events for specific
558 subsystems and components.
559 </para>
560 </important>
561
562 <remark>is reference appropriate? too advanced for readers (it seems so to me)? please advise.</remark>
563
564 </section>
565 <!-- stophere -->
566 <section id="systemtapscript-handler">
567 <title>SystemTap Handler/Body</title>
568 <indexterm>
569 <primary>handlers</primary>
570 <secondary>introduction</secondary>
571 </indexterm>
572 <para> Consider the following sample script: </para>
573
574 <example id="helloworld"><title>helloworld.stp</title>
575 <programlisting>probe begin
576 {
577 printf ("hello world\n")
578 exit ()
579 }</programlisting>
580 </example>
581
582 <para>
583 In <xref linkend="helloworld"/>, the event <command>begin</command>
584 (that is, the start of the session) triggers the handler enclosed in
585 <command>{ }</command>, which simply prints <command>hello
586 world</command> followed by a new-line, then exits.
587 </para>
588
589 <note>
590 <title>Note</title>
591 <indexterm>
592 <primary>functions (used in handlers)</primary>
593 <secondary><command>exit()</command></secondary>
594 </indexterm>
595
596 <indexterm>
597 <primary><command>exit()</command></primary>
598 <secondary>functions</secondary>
599 </indexterm>
600 <para>
601 SystemTap scripts continue to run until the
602 <command>exit()</command> function executes. If the users wants to stop
603 the execution of the script, it can interrupted manually with
604 <keycombo><keycap>Ctrl</keycap><keycap>C</keycap></keycombo>.
605 </para>
606 </note>
607
608 <formalpara id="printf">
609 <title>printf ( ) Statements</title>
610 <indexterm>
611 <primary><command>printf()</command></primary>
612 <secondary>format strings</secondary>
613 </indexterm>
614
615 <para>
616 The <command>printf ()</command> statement is one of the simplest
617 functions for printing data. <command>printf ()</command> can also be
618 used to display data using a wide variety of SystemTap functions in the
619 following format:
620 </para>
621 </formalpara>
622
623
624 <programlisting> printf ("<replaceable>format string</replaceable>\n", <replaceable>arguments</replaceable>)
625 </programlisting>
626 <indexterm>
627 <primary><command>printf()</command></primary>
628 <secondary>format strings</secondary>
629 </indexterm>
630
631 <indexterm>
632 <primary>format strings</primary>
633 <secondary><command>printf()</command></secondary>
634 </indexterm>
635 <para>
636 The <replaceable>format string</replaceable> specifies how
637 <replaceable>arguments</replaceable> should be printed. The format string
638 of <xref linkend="helloworld"/> instructs SystemTap to print
639 <command>hello world</command>, and contains no format specifiers.
640 </para>
641 <indexterm>
642 <primary><command>printf()</command></primary>
643 <secondary>format specifiers</secondary>
644 </indexterm>
645
646 <indexterm>
647 <primary>format specifiers</primary>
648 <secondary><command>printf()</command></secondary>
649 </indexterm>
650 <para>
651 You can use the format specifiers <command>%s</command> (for strings)
652 and <command>%d</command> (for numbers) in format strings, depending on
653 your list of arguments. Format strings can have multiple format
654 specifiers, each matching a corresponding argument; multiple arguments
655 are delimited by a comma (<command>,</command>).
656 </para>
657
658 <note>
659 <title>Note</title>
660 <indexterm>
661 <primary><command>printf()</command></primary>
662 <secondary>syntax and format</secondary>
663 </indexterm>
664
665 <indexterm>
666 <primary>syntax and format</primary>
667 <secondary><command>printf()</command></secondary>
668 </indexterm>
669 <indexterm>
670 <primary>format and syntax</primary>
671 <secondary><command>printf()</command></secondary>
672 </indexterm>
673 <para>Semantically, the SystemTap <command>printf</command> function is
674 very similar to its C language counterpart. The aforementioned syntax
675 and format for SystemTap's <command>printf</command> function is
676 identical to that of the C-style <command>printf</command>.
677 </para>
678 </note>
679
680 <para> To illustrate this, consider the following probe example: </para>
681
682 <example id="syscall-open">
683 <title>variables-in-printf-statements.stp</title>
684 <programlisting>probe syscall.open
685 {
686 printf ("%s(%d) open\n", execname(), pid())
687 }</programlisting>
688 </example>
689
690 <para>
691 <xref linkend="syscall-open"/> instructs SystemTap to probe all entries to
692 the system call <command>open</command>; for each event, it prints the
693 current <command>execname()</command> (a string with the executable name) and
694 <command>pid()</command> (the current process ID number), followed by the word
695 <command>open</command>. A snippet of this probe's output would look like:
696 </para>
697
698 <remark>editorial review: does a clarification that "format specifier1" is
699 to "argument1", "format specifier2" is to "argument2", or is this clear
700 enough? </remark>
701
702 <screen>vmware-guestd(2206) open
703 hald(2360) open
704 hald(2360) open
705 hald(2360) open
706 df(3433) open
707 df(3433) open
708 df(3433) open
709 hald(2360) open</screen>
710
711 <formalpara id="systemtapscript-functions">
712 <title>SystemTap Functions</title>
713 <indexterm>
714 <primary>functions</primary>
715 </indexterm>
716
717 <indexterm>
718 <primary>SystemTap script functions</primary>
719 </indexterm>
720
721 <indexterm>
722 <primary>handler functions</primary>
723 </indexterm>
724
725 <para>
726 SystemTap supports a wide variety of functions that can be used as
727 <command>printf ()</command> arguments. <xref linkend="syscall-open"/>
728 uses the SystemTap functions <command>execname()</command> (name of the
729 process that called a kernel function/performed a system call) and
730 <command>pid()</command> (current process ID).
731 </para>
732 </formalpara>
733
734 <remark>is "handler function" an appropriate term? wcohen: use "SystemTap functions" to match up language in man pages</remark>
735
736 <para>The following is a list of commonly-used SystemTap functions:</para>
737 <variablelist>
738
739 <varlistentry>
740 <term>tid()</term>
741 <listitem>
742 <indexterm>
743 <primary>functions</primary>
744 <secondary><command>tid()</command></secondary>
745 <!-- <tertiary><command>tid()</command></tertiary> -->
746 </indexterm>
747
748 <indexterm>
749 <primary>functions</primary>
750 <secondary><command>tid()</command></secondary>
751 <!-- <tertiary></tertiary> -->
752 </indexterm>
753
754 <indexterm>
755 <primary><command>tid()</command></primary>
756 <secondary>functions</secondary>
757 <!-- <tertiary>handler </tertiary> -->
758 </indexterm>
759
760 <para>The ID of the current thread.</para>
761 </listitem>
762 </varlistentry>
763
764 <varlistentry>
765 <term>uid()</term>
766 <listitem>
767 <indexterm>
768 <primary>functions</primary>
769 <!--<secondary>handler functions</secondary>-->
770 <secondary><command>uid()</command></secondary>
771 </indexterm>
772 <!--
773 <indexterm>
774 <primary>handler functions</primary>
775 <secondary>Handlers</secondary>
776 <tertiary><command>uid()</command></tertiary>
777 </indexterm>
778 -->
779 <indexterm>
780 <primary><command>uid()</command></primary>
781 <secondary>functions</secondary>
782 <!-- <tertiary>handler functions</tertiary> -->
783 </indexterm>
784 <para>The ID of the current user.</para>
785 </listitem>
786 </varlistentry>
787
788 <varlistentry>
789 <term>cpu()</term>
790 <listitem>
791 <indexterm>
792 <primary>functions</primary>
793 <!--<secondary>handler functions</secondary>-->
794 <secondary><command>cpu()</command></secondary>
795 </indexterm>
796 <!--
797 <indexterm>
798 <primary>handler functions</primary>
799 <secondary>Handlers</secondary>
800 <tertiary><command>cpu()</command></tertiary>
801 </indexterm>
802 -->
803 <indexterm>
804 <primary><command>cpu()</command></primary>
805 <secondary>functions</secondary>
806 <!-- <tertiary>handler functions</tertiary> -->
807 </indexterm>
808 <para>The current CPU number.</para>
809 </listitem>
810 </varlistentry>
811
812 <varlistentry>
813 <term>gettimeofday_s()</term>
814 <listitem>
815 <indexterm>
816 <primary>functions</primary>
817 <!--<secondary>handler functions</secondary>-->
818 <secondary><command>gettimeofday_s()</command></secondary>
819 </indexterm>
820 <!--
821 <indexterm>
822 <primary>handler functions</primary>
823 <secondary>Handlers</secondary>
824 <tertiary><command>gettimeofday_s()</command></tertiary>
825 </indexterm>
826 -->
827 <indexterm>
828 <primary><command>gettimeofday_s()</command></primary>
829 <secondary>functions</secondary>
830 <!-- <tertiary>handler functions</tertiary> -->
831 </indexterm>
832
833 <para>The number of seconds since UNIX epoch (January 1, 1970).</para>
834 </listitem>
835 </varlistentry>
836
837 <varlistentry>
838 <term>ctime()</term>
839 <listitem>
840 <indexterm>
841 <primary>functions</primary>
842 <!--<secondary>handler functions</secondary>-->
843 <secondary><command>ctime()</command></secondary>
844 </indexterm>
845 <!--
846 <indexterm>
847 <primary>handler functions</primary>
848 <secondary>Handlers</secondary>
849 <tertiary><command>ctime()</command></tertiary>
850 </indexterm>
851 -->
852 <indexterm>
853 <primary><command>ctime()</command></primary>
854 <secondary>functions</secondary>
855 <!-- <tertiary>handler functions</tertiary> -->
856 </indexterm>
857 <para>
858 Convert number of seconds since UNIX epoch to date.
859 </para>
860 </listitem>
861 </varlistentry>
862
863 <!--
864 <varlistentry>
865 <term>get_cycles()</term>
866 <listitem>
867 <para>A snapshot of the hardware cycle counter.</para>
868 </listitem>
869 </varlistentry>
870 -->
871
872 <varlistentry>
873 <term>pp()</term>
874 <listitem>
875 <indexterm>
876 <primary>functions</primary>
877 <!--<secondary>handler functions</secondary>-->
878 <secondary><command>pp()</command></secondary>
879 </indexterm>
880 <!--
881 <indexterm>
882 <primary>handler functions</primary>
883 <secondary>Handlers</secondary>
884 <tertiary><command>pp()</command></tertiary>
885 </indexterm>
886 -->
887 <indexterm>
888 <primary><command>pp()</command></primary>
889 <secondary>functions</secondary>
890 <!-- <tertiary>handler functions</tertiary> -->
891 </indexterm>
892 <para>A string describing the probe point currently being handled.</para>
893 </listitem>
894 </varlistentry>
895 <!-- removed, doesnt work as expected anymore
896 <varlistentry>
897 <term>probefunc()</term>
898 <listitem>
899 <para>If known, the name of the function in which the probe was placed.</para>
900 </listitem>
901 </varlistentry>
902 -->
903
904 <varlistentry>
905 <term>thread_indent()</term>
906 <listitem>
907 <indexterm>
908 <primary>functions</primary>
909 <!--<secondary>handler functions</secondary>-->
910 <secondary><command>thread_indent()</command></secondary>
911 </indexterm>
912 <!--
913 <indexterm>
914 <primary>handler functions</primary>
915 <secondary>Handlers</secondary>
916 <tertiary><command>thread_indent()</command></tertiary>
917 </indexterm>
918 -->
919 <indexterm>
920 <primary><command>thread_indent()</command></primary>
921 <secondary>functions</secondary>
922 <!-- <tertiary>handler functions</tertiary> -->
923 </indexterm>
924 <para>
925 This particular function is quite useful in providing you with a way
926 to better organize your print results. The function takes one
927 argument, an indentation delta, which indicates how many
928 spaces to add or remove from a thread's "indentation counter".
929 It then returns a
930 string with some generic trace data along with an appropriate number
931 of indentation spaces.
932 </para>
933
934 <para>
935 The generic data included in the returned string includes a
936 timestamp (number of microseconds since the
937 first call to <command>thread_indent()</command> by the thread),
938 a process name, and the thread ID. This allows you to
939 identify what functions were called, who called them, and the
940 duration of each function call.
941 </para>
942
943 <para>
944 If call entries and exits immediately precede each other, it is easy
945 to match them. However, in most cases, after a first function call
946 entry is made several other call entries and exits may be made
947 before the first call exits. The indentation counter helps you match
948 an entry with its corresponding exit by indenting the next function
949 call if it is not the exit of the previous one.
950 </para>
951
952 <para>
953 Consider the following example on the use of
954 <command>thread_indent()</command>:
955 </para>
956
957 <example id="thread_indent"><title>thread_indent.stp</title>
958 <programlisting>probe kernel.function("*@net/socket.c").call
959 {
960 printf ("%s -> %s\n", thread_indent(1), probefunc())
961 }
962 probe kernel.function("*@net/socket.c").return
963 {
964 printf ("%s &lt;- %s\n", thread_indent(-1), probefunc())
965 }</programlisting>
966 </example>
967
968 <para>
969 <xref linkend="thread_indent"/> prints out the
970 <command>thread_indent()</command> and probe functions at each event
971 in the following format:</para>
972
973 <screen>0 ftp(7223): -&gt; sys_socketcall
974 1159 ftp(7223): -&gt; sys_socket
975 2173 ftp(7223): -&gt; __sock_create
976 2286 ftp(7223): -&gt; sock_alloc_inode
977 2737 ftp(7223): &lt;- sock_alloc_inode
978 3349 ftp(7223): -&gt; sock_alloc
979 3389 ftp(7223): &lt;- sock_alloc
980 3417 ftp(7223): &lt;- __sock_create
981 4117 ftp(7223): -&gt; sock_create
982 4160 ftp(7223): &lt;- sock_create
983 4301 ftp(7223): -&gt; sock_map_fd
984 4644 ftp(7223): -&gt; sock_map_file
985 4699 ftp(7223): &lt;- sock_map_file
986 4715 ftp(7223): &lt;- sock_map_fd
987 4732 ftp(7223): &lt;- sys_socket
988 4775 ftp(7223): &lt;- sys_socketcall</screen>
989
990 <para>This sample output contains the following information:</para>
991
992 <itemizedlist>
993 <listitem><para>The time (in microseconds) since the initial <command>thread_indent()</command> call for the thread (included in the string from <command>thread_indent()</command>).</para></listitem>
994
995 <listitem><para>The process name (and its corresponding ID) that made the function call (included in the string from <command>thread_indent()</command>).</para></listitem>
996
997 <listitem><para>An arrow signifying whether the call was an entry (<computeroutput>&lt;-</computeroutput>) or an exit (<computeroutput>-></computeroutput>); the indentations help you match specific function call entries with their corresponding exits.</para></listitem>
998
999 <listitem><para>The name of the function called by the process.</para></listitem>
1000 </itemizedlist>
1001
1002 <remark>remember to add a reference later to "tapsets" from here, to clarify
1003 that thread_indent is defined in tapsets as a special function of sorts</remark>
1004
1005 </listitem>
1006 </varlistentry>
1007
1008 <varlistentry>
1009 <term>name</term>
1010 <listitem>
1011 <indexterm>
1012 <primary>local variables</primary>
1013 <secondary>name</secondary>
1014 </indexterm>
1015
1016 <indexterm>
1017 <primary>variables (local)</primary>
1018 <secondary>name</secondary>
1019 </indexterm>
1020
1021 <indexterm>
1022 <primary>name</primary>
1023 <secondary>local variables</secondary>
1024 </indexterm>
1025 <!-- <indexterm>
1026 <primary>functions</primary>
1027 <secondary>handler functions</secondary>
1028 <tertiary><command>name</command></tertiary>
1029 </indexterm>
1030
1031 <indexterm>
1032 <primary>handler functions</primary>
1033 <secondary>Handlers</secondary>
1034 <tertiary><command>name</command></tertiary>
1035 </indexterm>
1036
1037 <indexterm>
1038 <primary><command>name</command></primary>
1039 <secondary>Handlers</secondary>
1040 <tertiary>handler functions</tertiary>
1041 </indexterm>-->
1042 <para>Identifies the name of a specific system call. This variable can
1043 only be used in probes that use the event
1044 <command>syscall.<replaceable>system_call</replaceable></command>.
1045 </para>
1046 </listitem>
1047 </varlistentry>
1048
1049 <varlistentry>
1050 <term>target()</term>
1051 <listitem>
1052
1053 <indexterm>
1054 <primary>functions</primary>
1055 <!-- <secondary>handler functions</secondary> -->
1056 <secondary><command>target()</command></secondary>
1057 </indexterm>
1058 <!--
1059 <indexterm>
1060 <primary>handler functions</primary>
1061 <secondary>Handlers</secondary>
1062 <tertiary><command>target()</command></tertiary>
1063 </indexterm>
1064 -->
1065 <indexterm>
1066 <primary><command>target()</command></primary>
1067 <secondary>functions</secondary>
1068 <!-- <tertiary>handler functions</tertiary> -->
1069 </indexterm>
1070 <para>
1071 Used in conjunction with <command>stap
1072 <replaceable>script</replaceable> -x <replaceable>process
1073 ID</replaceable></command> or <command>stap
1074 <replaceable>script</replaceable> -c
1075 <replaceable>command</replaceable></command>. If you want to specify
1076 a script to take an argument of a process ID or command, use
1077 <command>target()</command> as the variable in the script to refer
1078 to it. For example:
1079 </para>
1080
1081 <example id="targetexample">
1082 <title>targetexample.stp</title>
1083 <programlisting>probe syscall.* {
1084 if (pid() == target())
1085 printf("%s\n", name)
1086 } </programlisting>
1087 </example>
1088
1089 <para>
1090 When <xref linkend="targetexample"/> is run with the argument
1091 <command>-x <replaceable>process ID</replaceable></command>, it
1092 watches all system calls (as specified by the event
1093 <command>syscall.*</command>) and prints out the name of all system
1094 calls made by the specified process.
1095 </para>
1096
1097 <para>
1098 This has the same effect as specifying <command>if (pid() ==
1099 <replaceable>process ID</replaceable>)</command> each time you wish
1100 to target a specific process. However, using
1101 <command>target()</command> makes it easier for you to re-use the
1102 script, giving you the ability to pass a process ID as an
1103 argument each time you wish to run the script (that is, <command>stap
1104 targetexample.stp -x <replaceable>process ID</replaceable></command>).
1105 </para>
1106 <!--
1107 <note>
1108 <title>Note</title>
1109 <para>In <xref linkend="targetexample"/>, <command>name</command> instructs SystemTap to capture the name of the process</para>
1110 </note> -->
1111
1112 </listitem>
1113 </varlistentry>
1114
1115 <!--
1116 <varlistentry>
1117 <term></term>
1118 <listitem>
1119 <para></para>
1120 </listitem>
1121 </varlistentry>
1122 -->
1123 </variablelist>
1124
1125 <para>For more information about supported SystemTap functions, refer to
1126 <command>man stapfuncs</command>.
1127 </para>
1128
1129 <remark>will need a complete listing of supported handler functions? also, SystemTap function descriptions seem ambiguous, please advise.</remark>
1130
1131 <!--
1132 <para>
1133 <replaceable>variable</replaceable> can be either <command>%s</command> for strings, or <command>%d</command> for numbers, depending on the <replaceable>handler function</replaceable> used. Each <command>printf ()</command> statement can contain multiple <replaceable>variable</replaceable>s, with each one corresponding to a matching <replaceable>handler function</replaceable>. Multiple <replaceable>handler function</replaceable>s are delimited by comma (<command>,</command>).
1134 </para>
1135
1136 <command>printf ()</command> is a SystemTap-supported C statement, and can also trap data using a wide variety
1137
1138 SystemTap supports a wide variety of handler functions that can trap data when triggered by events. One way to display these functions is to use the <command>print()</command>
1139 </para>
1140
1141
1142 <para>
1143 <xref linkend="wildcards"/> illustrates an example of a SystemTap script that contains no handlers. SystemTap will still be able to run the script, but no information will be displayed.
1144 </para>
1145 -->
1146
1147 </section>
1148 </section>