Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > 21b744155c03a240ef0ea458aef35c9d > files > 755

systemtap-client-2.2.1-1.fc18.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" type="text/css" href="Common_Content/css/default.css" /><link rel="stylesheet" media="print" href="Common_Content/css/print.css" type="text/css" /><meta name="generator" content="publican 2.8" /><meta name="package" content="Systemtap-SystemTap_Beginners_Guide-2.2.1-en-US-2.2.1-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="targetvariables.html" title="3.3.2. Target Variables" /></head><body><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="targetvariables.html"><strong>Next</strong></a></li></ul><div xml:lang="en-US" class="section" id="scriptconstructions" lang="en-US"><div class="titlepage"><div><div keep-together.within-column="always"><h2 class="title">3.3. Basic SystemTap Handler Constructs</h2></div></div></div><a id="idp184918076" class="indexterm"></a><a id="idp180048620" class="indexterm"></a><a id="idp180043524" class="indexterm"></a><a id="idp192297852" class="indexterm"></a><a id="idp178679444" 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" id="variablesconstructs"><div class="titlepage"><div><div keep-together.within-column="always"><h3 class="title">3.3.1. Variables</h3></div></div></div><a id="idp181844172" class="indexterm"></a><a id="idp226881164" class="indexterm"></a><a id="idp210079380" class="indexterm"></a><a id="idp205524932" 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 a 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="idp181141572" class="indexterm"></a><a id="idp212351948" class="indexterm"></a><a id="idp188272724" 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"><h6>Example 3.8. timer-jiffies.stp</h6><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></div><br class="example-break" /><a id="idp180415188" class="indexterm"></a><div class="para">
			<a class="xref" href="scriptconstructions.html#timerjiffies">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"><div class="admonition_header"><h2>Note</h2></div><div class="admonition"><div class="para">
				The <code class="command">++</code> notation in <a class="xref" href="scriptconstructions.html#timerjiffies">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></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="targetvariables.html"><strong>Next</strong>3.3.2. Target Variables</a></li></ul></body></html>