SystemTap Scripts
For the most part, SystemTap scripts are the foundation of each SystemTap session. SystemTap scripts instruct SystemTap on what type of information to trap, and what to do once that information is trapped.
As stated in , SystemTap scripts are made up of two components: events and handlers. Once a SystemTap session is underway, SystemTap monitors the operating system for the specified events and executes the handlers as they occur.
Note
An event and its corresponding handler is collectively called a probe. A SystemTap script can have multiple probes.
In terms of application development, using events and handlers is similar to inserting print statements in a program's sequence of commands. These print statements allow you to view a history of commands executed once the program is run.
SystemTap scripts go one step further by allowing you more flexibility with regard to handlers. Events serve as the triggers for handlers to run; handlers can be specified to trap specified data and print it in a certain manner.
Format
SystemTap scripts use the file extension .stp, and are written in the following format:
probe [event],
[another event]
{
[handler]
exit()
}
The exit() condition is optional, but it is recommended since it safely terminates the session once the script successfully traps the required information.
Important
is designed to introduce readers to the basics of SystemTap scripts. To understand SystemTap scripts better, it is advisable that you refer to ; each section therein provides a detailed explanation of the script, its events, handlers, and expected output.
Events
SystemTap supports multiple events per probe; as shown in , multiple events are delimited by a comma (,). Sample [event]s include:
begin
The startup of a SystemTap session; i.e. as soon as the SystemTap script is run.
end
The end of a SystemTap session.
kernel.function("[function]")
The entry to the kernel function function. For example, kernel.function("sys_open") refers to the "event" that the kernel function sys_open is used. To specify the return of the kernel function sys_open, append the return string to the event statement; i.e. kernel.function("sys_open").return.
When defining functions, you can use asterisk (*) for wildcards. You can also trace the entry/exit of a function in a kernel source file. Consider the following example:
Wildcards and Kernel Source Files in an Event
probe kernel.function("*@net/socket.c") { }
probe kernel.function("*@net/socket.c").return { }
In the previous example, the first probe's event specifies the entry of ALL functions in the kernel source file net/socket.c. The second probe specifies the exit of all those functions. Note that in this example, no handler was specified; as such, no information will be displayed.
syscall.[system_call]
The entry to the system call [system_call]. Similar to kerne.function, appending a return to the statement specifies the exit of the system call. For example, to specify the entry of the system call close, use syscall.close.return.
To identify what system calls are made by a specific program/command, use strace command.
Handlers
SystemTap supports a wide variety of functions that can trap data when triggered by events. One way to display these functions is to use the print()