summaryrefslogtreecommitdiffstats
path: root/SystemTap_Beginners_Guide/using-usage.html
blob: f6cee6e2acdd1a80b2690410dfba49cc1b6448fa (plain) (blame)
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 
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title xmlns:d="http://docbook.org/ns/docbook">2.3. Running SystemTap Scripts</title><link rel="stylesheet" type="text/css" href="Common_Content/css/default.css" /><link rel="stylesheet" media="print" href="Common_Content/css/print.css" type="text/css" /><meta xmlns:d="http://docbook.org/ns/docbook" name="generator" content="not publican" /><meta xmlns:d="http://docbook.org/ns/docbook" name="package" content="" /><link rel="home" href="index.html" title="SystemTap Beginners Guide" /><link rel="up" href="using-systemtap.html" title="Chapter 2. Using SystemTap" /><link rel="prev" href="cross-compiling.html" title="2.2. Generating Instrumentation for Other Computers" /><link rel="next" href="understanding-how-systemtap-works.html" title="Chapter 3. Understanding How SystemTap Works" /></head><body><p id="title"></p><ul class="docnav top"><li class="previous"><a accesskey="p" href="cross-compiling.html"><strong>Prev</strong></a></li><li class="home">SystemTap Beginners Guide</li><li class="next"><a accesskey="n" href="understanding-how-systemtap-works.html"><strong>Next</strong></a></li></ul><div class="section"><div class="titlepage"><div><div><h2 class="title"><a id="using-usage"></a>2.3. Running SystemTap Scripts</h2></div></div></div><a id="id1936" class="indexterm"></a><a id="id1939" class="indexterm"></a><a id="id1942" class="indexterm"></a><a id="id1945" class="indexterm"></a><a id="id1948" class="indexterm"></a><a id="id1951" class="indexterm"></a><div class="para">	SystemTap is distributed with a number of command line tools that allow you to monitor the activities of the system. The <code class="command">stap</code> command reads probing instructions from a SystemTap script, translates these instructions into C code, builds a kernel module, and loads it into the running Linux kernel. The <code class="command">staprun</code> command runs SystemTap instrumentation, that is, a kernel module built from SystemTap scripts during a cross-instrumentation. </div><a id="id1957" class="indexterm"></a><div class="para">Running <code class="command">stap</code> and <code class="command">staprun</code> requires elevated privileges to the system. Because not all users can be granted root access just to run SystemTap, you can allow a non-privileged user to run SystemTap instrumentation on their machine by adding them to one of the following user groups:</div><div class="variablelist"><dl class="variablelist"><dt><span class="term">stapdev</span></dt><dd><a id="id1966" class="indexterm"></a><a id="id1969" class="indexterm"></a><div class="para">	Members of this group can use the <code class="command">stap</code> command to run SystemTap scripts, or <code class="command">staprun</code> to run SystemTap instrumentation modules. </div><div class="para">	Running the <code class="command">stap</code> command involves compiling SystemTap scripts into kernel modules and	loading them into the kernel. This operation requires elevated privileges to the system, which are granted	to <code class="systemitem">stapdev</code> members. Unfortunately, such privileges also grant effective root	access to <code class="systemitem">stapdev</code> members. As a consequence, only grant <code class="systemitem">stapdev</code> group membership to users whom you can trust with root access. </div></dd><dt><span class="term">stapusr</span></dt><dd><a id="id1983" class="indexterm"></a><a id="id1986" class="indexterm"></a><div class="para">	Members of this group can only use the <code class="command">staprun</code> command to run SystemTap instrumentation modules. In addition, they can only run modules from the <code class="filename">/lib/modules/<em class="replaceable">kernel_version</em>/systemtap/</code> directory. Note that this directory must be owned only by the root user, and must only be writable by the root user. </div></dd></dl></div><div class="para">	The <code class="command">stap</code> command reads a SystemTap script either from a file, or from standard input. To tell <code class="command">stap</code> to read a SystemTap script from a file, specify the file name on the command line: </div><pre class="screen"><code class="command">stap <em class="replaceable">file_name</em></code></pre><a id="id1999" class="indexterm"></a><a id="id2002" class="indexterm"></a><a id="id2005" class="indexterm"></a><div class="para">	To instruct <code class="command">stap</code> to read a SystemTap script from standard input, use the <code class="option">-</code> switch instead of the file name. Note that any command-line options you wish to use must be inserted before the <code class="option">-</code> switch. For example, to make the output of the <code class="command">stap</code> command more verbose, type: </div><pre class="screen"><code class="command">echo "probe timer.s(1) {exit()}" | stap -v -</code></pre><div class="para"> Below is a list of commonly used <code class="command">stap</code> options: </div><a id="id2016" class="indexterm"></a><a id="id2019" class="indexterm"></a><div class="variablelist"><a id="id2023" class="indexterm"></a><dl class="variablelist"><dt><span class="term">-v</span></dt><dd><div class="para">Makes the output of the SystemTap session more	verbose. You can repeat this option multiple times to provide more details on the script's execution,	for example:</div><pre class="screen"><code class="command">stap -vvv script.stp</code></pre><div class="para">This option is particularly useful if you encounter any errors in running the script.	For more information about common SystemTap script errors, refer to <a class="xref" href="errors.html">Chapter 6, <em>Understanding SystemTap Errors</em></a>.</div></dd><dt><span class="term">-o <em class="replaceable">file_name</em></span></dt><dd><div class="para">Sends the standard output to a file named <em class="replaceable">file_name</em>.</div></dd><dt><span class="term">-S <em class="replaceable">size</em>,<em class="replaceable">count</em></span></dt><dd><div class="para">Limits the maximum size of output files to <em class="replaceable">size</em> megabytes and the maximum number of stored files to <em class="replaceable">count</em>. This option implements logrotate operations for SystemTap	and the resulting file names have a sequence number suffix.</div></dd><dt><span class="term">-x <em class="replaceable">process_id</em></span></dt><dd><div class="para">Sets the SystemTap handler function <code class="function">target()</code> to the specified process ID. For more information about <code class="function">target()</code>, refer to <a class="xref" href="systemtapscript-handler.html#systemtapscript-functions">SystemTap Functions</a>.</div></dd><dt><span class="term">-c '<em class="replaceable">command</em>'</span></dt><dd><div class="para">Sets the SystemTap handler function <code class="function">target()</code> to the specified	command and runs the SystemTap instrumentation for the duration	of this command.	For more information about <code class="function">target()</code>, refer to <a class="xref" href="systemtapscript-handler.html#systemtapscript-functions">SystemTap Functions</a>.</div></dd><dt><span class="term">-e '<em class="replaceable">script</em>'</span></dt><dd><div class="para">Uses <em class="replaceable">script</em> rather than a file as input for the SystemTap translator.</div></dd><dt><span class="term">-F</span></dt><dd><div class="para">Uses SystemTap's flight recorder mode and makes the script a background process. For more information about flight recorder mode, refer to <a class="xref" href="using-usage.html#flight-recorder">Section 2.3.1, “SystemTap Flight Recorder Mode”</a>.</div></dd></dl></div><div class="para">For more information about the <code class="command">stap</code> command, refer to the <span class="citerefentry"><span class="refentrytitle">stap</span>(1)</span> man page. For more information about the <code class="command">staprun</code> command and cross-instrumentation, refer to <a class="xref" href="cross-compiling.html">Section 2.2, “Generating Instrumentation for Other Computers”</a> or the <span class="citerefentry"><span class="refentrytitle">staprun</span>(8)</span> man page.</div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="flight-recorder"></a>2.3.1. SystemTap Flight Recorder Mode</h3></div></div></div><a id="id2087" class="indexterm"></a><div class="para"> SystemTap's flight recorder mode allows you to run a SystemTap script for long periods of time and just focus on recent output. The flight recorder mode limits the amount of output generated. </div><div class="para"> There are two variations of the flight recorder mode: <em class="firstterm">in-memory</em> and <em class="firstterm">file</em> mode. In both cases, the SystemTap script runs as a background process. </div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="memory-flight-recorder"></a>2.3.1.1. In-memory Flight Recorder</h4></div></div></div><a id="id2095" class="indexterm"></a><div class="para"> When flight recorder mode is used without a file name, SystemTap uses a buffer in kernel memory to store the output of the script. Once the SystemTap instrumentation module is loaded and the probes start running, the instrumentation detaches and is put in the background. When the interesting event occurs, you can reattach to the instrumentation to see the recent output in the memory buffer and any continuing output. </div><div class="para"> To run a SystemTap script by using the flight recorder in-memory mode, run the <code class="command">stap</code> command with the <code class="option">-F</code> command line option: </div><pre class="screen"><code class="command">stap -F iotime.stp</code></pre><div class="para"> Once the script starts, <code class="command">stap</code> prints a message similar to the following to provide you with the command to reconnect to the running script: </div><pre class="screen">Disconnecting from systemtap module. To reconnect, type "staprun -A stap_5dd0073edcb1f13f7565d8c343063e68_19556"</pre><div class="para"> When the interesting event occurs, run the following command to connect to the currently running script, output the recent data in the memory buffer, and get continuing output: </div><pre class="screen"><code class="command">staprun -A stap_5dd0073edcb1f13f7565d8c343063e68_19556</code></pre><div class="para"> By default, the kernel buffer is 1MB in size. You can increase this value by using the <code class="option">-s</code> option with the size in megabytes (rounded up to the next power over 2) for the buffer. For example, <code class="option">-s2</code> on the SystemTap command line would specify 2MB for the buffer. </div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="file-flight-recorder"></a>2.3.1.2. File Flight Recorder</h4></div></div></div><a id="id2115" class="indexterm"></a><div class="para"> The flight recorder mode can also store data to files. You can control the number and size of the files kept by using the <code class="option">-S</code> option followed by two numerical arguments separated by a comma: the first argument is the maximum size in megabytes for the each output file, the second argument is the number of recent files to keep. To specify the file name, use the <code class="option">-o</code> option followed by the name. SystemTap automatically adds a number suffix to the file name to indicate the order of the files. </div><div class="para"> The following command starts SystemTap in file flight recorder mode with the output going to files named <code class="filename">/tmp/pfaults.log.<em class="replaceable">[0-9]+</em></code>, each file 1MB or smaller, and keeping latest two files: </div><pre class="screen"><code class="command">stap -F -o /tmp/pfaults.log -S 1,2 pfaults.stp</code></pre><div class="para"> The command prints the process ID to standard output. Sending a SIGTERM to the process terminates the SystemTap script and stops the data collection. For example, if the previous command listed 7590 as the process ID, the following command would stop the SystemTap script: </div><pre class="screen"><code class="command">kill -s SIGTERM 7590</code></pre><div class="para"> In this example, only the most recent two files generated by the script are kept: SystemTap automatically removes older files. As a result, the <code class="command">ls -sh /tmp/pfaults.log.*</code> command lists two files: </div><pre class="screen">1020K /tmp/pfaults.log.5 44K /tmp/pfaults.log.6</pre><div class="para"> To examine the latest data, read the file with the highest number, in this case <code class="filename">/tmp/pfaults.log.6</code>. </div></div></div></div><ul class="docnav"><li class="previous"><a accesskey="p" href="cross-compiling.html"><strong>Prev</strong>2.2. Generating Instrumentation for Other Compute...</a></li><li class="up"><a accesskey="u" href="#"><strong>Up</strong></a></li><li class="home"><a accesskey="h" href="index.html"><strong>Home</strong></a></li><li class="next"><a accesskey="n" href="understanding-how-systemtap-works.html"><strong>Next</strong>Chapter 3. Understanding How SystemTap Works</a></li></ul></body></html>