summaryrefslogtreecommitdiffstats
path: root/SystemTap_Beginners_Guide/handlerconditionalstatements.html
blob: a663569e6692ee8ac2c9e699779b54b3f202ef7d (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 
<?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">3.3.3. Conditional Statements</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="publican v4.1.3" /><meta xmlns:d="http://docbook.org/ns/docbook" name="package" content="SystemTap-SystemTap_Beginners_Guide-2.6-en-US-2.0-1" /><link rel="home" href="index.html" title="SystemTap Beginners Guide" /><link rel="up" href="scriptconstructions.html" title="3.3. Basic SystemTap Handler Constructs" /><link rel="prev" href="targetavailable.html" title="3.3.2.3. Checking Target Variable Availability" /><link rel="next" href="commandlineargssect.html" title="3.3.4. Command-Line Arguments" /></head><body><p id="title"><a class="left" href="https://fedorahosted.org/publican"><img alt="Product Site" src="Common_Content/images//image_left.png" /></a><a class="right" href="https://fedorahosted.org/publican"><img alt="Documentation Site" src="Common_Content/images//image_right.png" /></a></p><ul class="docnav top"><li class="previous"><a accesskey="p" href="targetavailable.html"><strong>Prev</strong></a></li><li class="home">SystemTap Beginners Guide</li><li class="next"><a accesskey="n" href="commandlineargssect.html"><strong>Next</strong></a></li></ul><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="handlerconditionalstatements"></a>3.3.3. Conditional Statements</h3></div></div></div><a id="idm214011954080" class="indexterm"></a><a id="idm214007213072" class="indexterm"></a><div class="para">	In some cases, the output of a SystemTap script may be too large. To address this, you need to further refine the script's logic in order to delimit the output into something more relevant or useful to your probe. </div><div class="para">	Do this by using <span class="emphasis"><em>conditionals</em></span> in handlers. SystemTap accepts the following types of conditional statements: </div><div class="variablelist"><dl class="variablelist"><dt><span class="term">If/Else Statements</span></dt><dd><a id="idm213973606704" class="indexterm"></a><a id="idm214018132832" class="indexterm"></a><a id="idm213973137472" class="indexterm"></a><div class="para">	Format: </div><pre class="programlisting">if (<em class="replaceable">condition</em>) <em class="replaceable">statement1</em> else <em class="replaceable">statement2</em></pre><div class="para">	The <code class="command"><em class="replaceable">statement1</em></code> is executed if the <code class="command"><em class="replaceable">condition</em></code> expression is non-zero. The <code class="command"><em class="replaceable">statement2</em></code> is executed if the <code class="command"><em class="replaceable">condition</em></code> expression is zero. The <code class="command">else</code> clause (<code class="command">else</code> <em class="replaceable">statement2</em>) is optional. Both <code class="command"><em class="replaceable">statement1</em></code> and <code class="command"><em class="replaceable">statement2</em></code> can be statement blocks. </div><div class="example"><a id="simpleifelseexample"></a><p class="title"><strong>Example 3.11. ifelse.stp</strong></p><div class="example-contents"><pre class="programlisting">global countread, countnonread probe kernel.function("vfs_read"),kernel.function("vfs_write") { if (probefunc()=="vfs_read") countread ++ else countnonread ++ } probe timer.s(5) { exit() } probe end { printf("VFS reads total %d\n VFS writes total %d\n", countread, countnonread) }</pre></div></div><div class="para"> <a class="xref" href="handlerconditionalstatements.html#simpleifelseexample">Example 3.11, “ifelse.stp”</a> is a script that counts how many virtual file system reads (<code class="command">vfs_read</code>) and writes (<code class="command">vfs_write</code>) the system performs within a 5-second span. When run, the script increments the value of the variable <code class="command">countread</code> by 1 if the name of the function it probed matches <code class="command">vfs_read</code> (as noted by the condition <code class="command">if (probefunc()=="vfs_read")</code>); otherwise, it increments <code class="command">countnonread</code> (<code class="command">else {countnonread ++}</code>). </div></dd><dt><span class="term">While Loops</span></dt><dd><a id="idm214017988768" class="indexterm"></a><a id="idm213970851600" class="indexterm"></a><a id="idm214018911600" class="indexterm"></a><div class="para">	Format: </div><pre class="programlisting">while (<em class="replaceable">condition</em>) <em class="replaceable">statement</em></pre><div class="para">	So long as <code class="command"><em class="replaceable">condition</em></code> is non-zero the block of statements in <code class="command"><em class="replaceable">statement</em></code> are executed. The <code class="command"><em class="replaceable">statement</em></code> is often a statement block and it must change a value so <code class="command"><em class="replaceable">condition</em></code> will eventually be zero. </div></dd><dt><span class="term">For Loops</span></dt><dd><a id="idm213969747888" class="indexterm"></a><a id="idm214010565392" class="indexterm"></a><a id="idm213978689584" class="indexterm"></a><div class="para">	Format: </div><pre class="programlisting">for (<em class="replaceable">initialization</em>; <em class="replaceable">conditional</em>; <em class="replaceable">increment</em>) <em class="replaceable">statement</em></pre><div class="para">	The <code class="command">for</code> loop is shorthand for a while loop. The following is the equivalent <code class="command">while</code> loop: </div><pre class="programlisting"><em class="replaceable">initialization</em> while (<em class="replaceable">conditional</em>) { <em class="replaceable">statement</em> <em class="replaceable">increment</em> }</pre></dd></dl></div><div class="para"><div xmlns:d="http://docbook.org/ns/docbook" class="title">Conditional Operators</div><a id="idm214010496016" class="indexterm"></a><a id="idm214009622416" class="indexterm"></a><a id="idm213972701104" class="indexterm"></a>	Aside from <code class="command">==</code> ("is equal to"), following operators can also be used in conditional statements: </div><div class="variablelist"><dl class="variablelist"><dt><span class="term">&gt;=</span></dt><dd><div class="para">	Greater than or equal to </div></dd><dt><span class="term">&lt;=</span></dt><dd><div class="para">	Less than or equal to </div></dd><dt><span class="term">!=</span></dt><dd><div class="para">	Is not equal to </div></dd></dl></div></div><ul class="docnav"><li class="previous"><a accesskey="p" href="targetavailable.html"><strong>Prev</strong>3.3.2.3. Checking Target Variable Availability</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="commandlineargssect.html"><strong>Next</strong>3.3.4. Command-Line Arguments</a></li></ul></body></html>