Sophie

Sophie

distrib > Fedora > 13 > i386 > by-pkgid > 95299258dbdf9a86cefd89b97c0d81e5 > files > 119

systemtap-1.2-1.fc13.i686.rpm

<?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>3.3. Basic SystemTap Handler Constructs</title><link rel="stylesheet" href="./Common_Content/css/default.css" type="text/css" /><meta name="generator" content="publican 1.6" /><meta name="package" content="Systemtap-SystemTap_Beginners_Guide-1.0-en-US-2.0-2" /><link rel="home" href="index.html" title="SystemTap Beginners Guide" /><link rel="up" href="understanding-how-systemtap-works.html" title="Chapter 3. Understanding How SystemTap Works" /><link rel="prev" href="systemtapscript-handler.html" title="3.2.2. Systemtap Handler/Body" /><link rel="next" href="handlerconditionalstatements.html" title="3.3.2. Conditional Statements" /></head><body class=""><p id="title"><a class="left" href="http://www.fedoraproject.org"><img src="Common_Content/images/image_left.png" alt="Product Site" /></a><a class="right" href="http://docs.fedoraproject.org"><img src="Common_Content/images/image_right.png" alt="Documentation Site" /></a></p><ul class="docnav"><li class="previous"><a accesskey="p" href="systemtapscript-handler.html"><strong>Prev</strong></a></li><li class="next"><a accesskey="n" href="handlerconditionalstatements.html"><strong>Next</strong></a></li></ul><div xml:lang="en-US" class="section" title="3.3. Basic SystemTap Handler Constructs" lang="en-US"><div class="titlepage"><div><div><h2 class="title" id="scriptconstructions">3.3. Basic SystemTap Handler Constructs</h2></div></div></div><a id="id2897368" class="indexterm"></a><a id="id3070858" class="indexterm"></a><a id="id2828725" class="indexterm"></a><a id="id2796924" class="indexterm"></a><a id="id4374099" class="indexterm"></a><div class="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 <code class="command">awk</code> 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.
	</div><div class="section" title="3.3.1. Variables"><div class="titlepage"><div><div><h3 class="title" id="variablesconstructs">3.3.1. Variables</h3></div></div></div><a id="id2804478" class="indexterm"></a><a id="id2819582" class="indexterm"></a><a id="id3035684" class="indexterm"></a><a id="id2918600" class="indexterm"></a><div class="para">
			Variables can be used freely throughout a handler; simply choose a name, assign a value from a function or expression to it, and use it in an expression. SystemTap automatically identifies whether a variable should be typed as a string or integer, based on the type of the values assigned to it. For instance, if you use set the variable <code class="command">foo</code> to <code class="command">gettimeofday_s()</code> (as in <code class="command">foo = gettimeofday_s()</code>), then <code class="command">foo</code> is typed as an number and can be printed in a <code class="command">printf()</code> with the integer format specifier (<code class="command">%d</code>).
		</div><a id="id2889869" class="indexterm"></a><a id="id2843878" class="indexterm"></a><a id="id3086074" class="indexterm"></a><div class="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 using <code class="command">global</code> outside of the probes. Consider the following example:
		</div><div class="example" id="timerjiffies"><div class="example-contents"><pre class="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 =&gt; CONFIG_HZ=%d\n",
    count_jiffies, count_ms, hz)
  exit ()
}
</pre></div><h6>Example 3.8. timer-jiffies.stp</h6></div><br class="example-break" /><a id="id4345364" class="indexterm"></a><div class="para">
			<a class="xref" href="scriptconstructions.html#timerjiffies" title="Example 3.8. timer-jiffies.stp">Example 3.8, “timer-jiffies.stp”</a> computes the <code class="command">CONFIG_HZ</code> setting of the kernel using timers that count jiffies and milliseconds, then computing accordingly. The <code class="command">global</code> statement allows the script to use the variables <code class="command">count_jiffies</code> and <code class="command">count_ms</code> (set in their own respective probes) to be shared with <code class="command">probe timer.ms(12345)</code>.
		</div><div class="note"><h2>Note</h2><div class="para">
				The <code class="command">++</code> notation in <a class="xref" href="scriptconstructions.html#timerjiffies" title="Example 3.8. timer-jiffies.stp">Example 3.8, “timer-jiffies.stp”</a> (i.e. <code class="command">count_jiffies ++</code> and <code class="command">count_ms ++</code>) is used to increment the value of a variable by 1. In the following probe, <code class="command">count_jiffies</code> is incremented by 1 every 100 jiffies:
			</div><pre class="screen">
probe timer.jiffies(100) { count_jiffies ++ }
</pre><div class="para">
				In this instance, SystemTap understands that <code class="command">count_jiffies</code> is an integer. Because no initial value was assigned to <code class="command">count_jiffies</code>, its initial value is zero by default.
			</div></div></div></div><ul class="docnav"><li class="previous"><a accesskey="p" href="systemtapscript-handler.html"><strong>Prev</strong>3.2.2. Systemtap Handler/Body</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="handlerconditionalstatements.html"><strong>Next</strong>3.3.2. Conditional Statements</a></li></ul></body></html>