<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>technochakra.com &#187; debugging linux</title>
	<atom:link href="http://www.technochakra.com/tag/debugging-linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.technochakra.com</link>
	<description>Wheels Of Technology</description>
	<lastBuildDate>Thu, 04 Feb 2010 11:48:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>strace Those Crashes On Linux</title>
		<link>http://www.technochakra.com/strace-those-crashes-on-linux/</link>
		<comments>http://www.technochakra.com/strace-those-crashes-on-linux/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 04:21:07 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[debugging linux]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=155</guid>
		<description><![CDATA[strace is a great tool on Linux that every developer should be aware of.  It helps trace system APIs &#8211; the APIs used, parameters passed and the error value returned.   It does not require root permissions to run.  You can generate a system call trace on a command that is executed or you can trace [...]]]></description>
			<content:encoded><![CDATA[<p>strace is a great tool on Linux that every developer should be aware of.  It helps trace system APIs &#8211; the APIs used, parameters passed and the error value returned.   It does not require root permissions to run.  You can generate a system call trace on a command that is executed or you can trace an already running process.</p>
<h3>Sample Usage</h3>
<blockquote><p>strace ./my_crashing_binary</p>
<p>strace ./my_crashing_binary 2&gt;out.txt</p>
<p>strace -p 12345 2&gt;out.txt</p></blockquote>
<h2>The strace Output</h2>
<p>A sample output of strace is given below -</p>
<blockquote><p><code><span style="color: #800000;">&lt;snip&gt;</span><br />
<span style="color: #800000;">old_mmap</span>(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fe1000<br />
<span style="color: #800000;">mprotect</span>(0x645000, 4096, PROT_READ)     = 0<br />
<span style="color: #800000;">mprotect</span>(0x51a000, 4096, PROT_READ)     = 0<br />
<span style="color: #800000;">set_thread_area</span>({entry_number:-1 -&gt; 6, base_addr:0xb7fe16c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0<br />
<span style="color: #800000;">munmap</span>(0xb7fe3000, 115297)              = 0<br />
<span style="color: #800000;">fstat64</span>(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 5), ...}) = 0<br />
<span style="color: #800000;">mmap2</span>(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fff000<br />
<span style="color: #800000;">write</span>(1, "Hello World\n", 12)           = 12<br />
<span style="color: #800000;">brk</span>(0)                                  = 0x940e000<br />
<span style="color: #800000;">brk</span>(0x942f000)                          = 0x942f000<br />
<span style="color: #993366;">open</span>("/myfile", O_RDONLY)               = -1 ENOENT (No such file or directory)<br />
<span style="color: #993366;">--- SIGSEGV (Segmentation fault) @ 0 (0) ---<br />
+++ killed by SIGSEGV (core dumped) +++</span><br />
</code></p></blockquote>
<p>In the above example each line of the strace output has the system call, the parameters passed to it and the return value.</p>
<p>Take note that just before the above program crashed, it tried to access <em>/myfile</em>. With experience it is not hard to guess that the file is being accessed even though it was not found.  Either this file is absolutely necessary for the program to run or the required error checks are missing that could have averted the crash.</p>
<h2>When Is strace Useful?</h2>
<p>strace is very useful while debugging crashes.  Some scenarios where strace can be helpful are -</p>
<ol>
<li>Debugging why an installation crashes on a machine.</li>
<li>Detecting file contention issues such as a file name clash while creating temporary files.</li>
<li>Debugging random crashes that are most probably due to the program running out of memory or due to it requesting an arbitrarily large chunk of memory.</li>
<li>Finding out how the program interacts with the file system.</li>
<li>Debugging crashes reproducibly only on one machine.</li>
<li>Debugging crashes in unfamilar code or in cases when sources are unavailable.</li>
</ol>
<h2>More Information</h2>
<p>This article is meant as a pointer to the strace utility but it is not a substitute for the man page.  Go to the man page to explore all options strace provides.</p>
<blockquote><p>man strace</p></blockquote>
<p>strace is not a substitute for a debugger but a lightweight tool which can be used to generate logs, diagnose common problems and study the usage of system calls in a program.  Like any other program in a programmer&#8217;s toolset, it can save a lot of debugging time if used at the right time.</p>
<p>
<iframe src="http://rcm.amazon.com/e/cm?t=technochakra-20&#038;o=1&#038;p=13&#038;l=st1&#038;mode=books&#038;search=linux%20debugging&#038;fc1=000000&#038;lt1=&#038;lc1=3366FF&#038;bg1=FFFFFF&#038;f=ifr" marginwidth="0" marginheight="0" width="468" height="60" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe></p>
<p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-2898169159040774";
/* 468x60, created 7/1/09 */
google_ad_slot = "5882807646";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/strace-those-crashes-on-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
