<?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>tdistler.com &#187; debugging</title>
	<atom:link href="http://tdistler.com/tag/debugging/feed" rel="self" type="application/rss+xml" />
	<link>http://tdistler.com</link>
	<description>&#34;To err is human, but to really foul things up you need a computer.”</description>
	<lastBuildDate>Tue, 06 Dec 2011 20:46:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>CPU Profiling on Windows: Very Sleepy</title>
		<link>http://tdistler.com/2010/12/21/cpu-profiling-on-windows-very-sleepy</link>
		<comments>http://tdistler.com/2010/12/21/cpu-profiling-on-windows-very-sleepy#comments</comments>
		<pubDate>Tue, 21 Dec 2010 19:59:04 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Code Monkey]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://tdistler.com/?p=697</guid>
		<description><![CDATA[A colleague of mine found a pretty cool profiling tool for Windows called Very Sleepy. It&#8217;s simple, straight-forward, free, and it works&#8230; everything I like in a tool (this is why I like the Sysinternals stuff so much). It&#8217;s also got call-graph and 64-bit support. Definitely worth checking out.]]></description>
			<content:encoded><![CDATA[<p>A colleague of mine found a pretty cool profiling tool for Windows called <a title="Coders Notes: Very Sleepy" href="http://www.codersnotes.com/sleepy" target="_blank">Very Sleepy</a>. It&#8217;s simple, straight-forward, free, and it works&#8230; everything I like in a tool (this is why I like the <a title="Mircosoft Sysinternals" href="http://www.sysinternals.com" target="_blank">Sysinternals</a> stuff so much). It&#8217;s also got call-graph and 64-bit support. Definitely worth checking out.</p>
<p><a href="http://tdistler.com/wp-content/uploads/2011/01/very_sleepy_screenshot.jpg"><img class="aligncenter size-medium wp-image-698" title="very_sleepy_screenshot" src="http://tdistler.com/wp-content/uploads/2011/01/very_sleepy_screenshot-300x218.jpg" alt="Very Sleep Screen Shot" width="300" height="218" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://tdistler.com/2010/12/21/cpu-profiling-on-windows-very-sleepy/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ThreadNinja: Finding Rogue POSIX Threads</title>
		<link>http://tdistler.com/2010/07/08/threadninja-finding-rogue-posix-threads</link>
		<comments>http://tdistler.com/2010/07/08/threadninja-finding-rogue-posix-threads#comments</comments>
		<pubDate>Fri, 09 Jul 2010 02:10:04 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Code Monkey]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://tdistler.com/?p=277</guid>
		<description><![CDATA[What Is It? ThreadNinja is a Linux library my team created that tracks pthread_create() and pthread_join() calls in an application. It prints a stacktrace where each thread is created and where it is joined. Any rogue (unjoined) threads are reported when the application exits. ThreadNinja is unobtrusive: it does NOT have to be compiled into [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://tdistler.com/wp-content/uploads/2010/05/tux-ninja.jpg"><img class="alignright size-thumbnail wp-image-278" title="tux-ninja" src="http://tdistler.com/wp-content/uploads/2010/05/tux-ninja-150x150.jpg" alt="Tux Ninja" width="150" height="150" /></a><strong>What Is It?</strong></p>
<p>ThreadNinja is a Linux library my team created that tracks <code>pthread_create()</code> and <code>pthread_join()</code> calls in an application. It prints a stacktrace where each thread is created and where it is joined. Any rogue (unjoined) threads are reported when the application exits. ThreadNinja is <em>unobtrusive</em>: it does NOT have to be compiled into the code. This means you can use it on applications you didn&#8217;t compile.</p>
<p>We found it useful and thought we&#8217;d share it. It&#8217;s be no means production code&#8230; just a tool. Hack on it, expand it, change it&#8230; whatever. It&#8217;s pretty small, so it should be easy to dive right in. We&#8217;ve released it under the <a title="BSD License Page" href="http://www.opensource.org/licenses/bsd-license.php" target="_blank">BSD license</a>.</p>
<p><strong>Cut To The Chase</strong></p>
<p>You can checkout the source code from <a title="Thread Ninja Project on Google Code" href="http://code.google.com/p/threadninja/" target="_blank">Google Code</a>, or download the version 1.0 tarball directly (<a title="Thread Ninja Source Tarball" href="http://www.tdistler.com/media/code/threadninja.tar.gz" target="_blank">threadninja.tar.gz</a>).</p>
<p>To build ThreadNinja, simply untar it and call <code>make</code>:<br />
<code>&gt; tar -zxf threadninja.tar.gz<br />
&gt; make</code></p>
<p>Now, simply use <code>LD_PRELOAD</code> to run the application:</p>
<p><code>&gt; LD_PRELOAD=/path/to/threadninja/build/libthreadninja.so.1 TheApplication</code></p>
<p>If you don&#8217;t see function names in the stacktraces that are generated, then the application needs to be compiled with debug symbols. For my test app, I had to compile with the <code>-rdynamic</code> option:</p>
<p><code>&gt; ﻿g++ -Wall -rdynamic main.cpp -lpthread</code></p>
<p>This causes the global symbol table to be included in the executable, which contains all the application&#8217;s function names. For more info, look at the <code>--export-dynamic</code> option on the <a title="GNU Linker man page" href="http://linux.die.net/man/1/ld" target="_blank">GNU linker (ld) man page</a>.</p>
<p><strong>The Story Behind ThreadNinja</strong></p>
<p>My team was assigned to stabilize a large video application that runs as a Linux-based appliance. The application consisted of 100,000+ of lines to code that was a tangle of build warnings, circular references, and many creative hacks. Our particular task was to fix a persistent set of seg-faults and memory leaks.<span id="more-277"></span></p>
<p>One of the first things we noticed was that the application didn&#8217;t shutdown properly. The shutdown logic was something akin to: signal components to exit, sleep 2 seconds, call Release() a couple times to clean up extra ref-counts, sleep 2 seconds, and then just call exit() to get the process to terminate (bypassing the remaining clean up code). Of course, this renders <a title="Valgrind" href="http://valgrind.org/" target="_blank">Valgrind</a> useless when trying to find memory leaks, because the automatic memory cleanup code gets bypassed during the process abort.</p>
<p>As a result, our first priority was to get the app to shutdown cleanly. The first issue we ran into was that <code>pthread_join()</code> blocked indefinitely because threads were failing to terminate. We tried using GDB to track the threads, but many hundreds of threads were being created and destroyed dynamically. We needed a way to let the application run for hours, allow 1000+ threads to live and die, and still be able to track rogue threads. Hence, ThreadNinja was born.</p>
<p><strong>How It Works</strong></p>
<p>Through the magic of <code><a title="LD_PRELOAD" href="http://linux.die.net/man/8/ld.so" target="_blank">LD_PRELOAD</a></code>, ThreadNinja &#8220;injects&#8221; itself between all calls to <code>pthread_create()</code> and <code>pthread_join()</code>. This happens because <code>LD_PRELOAD</code> instructs the loader to load ThreadNinja into memory first, before other libraries (in this case, before the pthread library). The result is that ThreadNinja&#8217;s implementation of <code>pthread_create()</code> and <code>pthread_join()</code> are used by the application instead of pthread&#8217;s own implementation. What ThreadNinja does is track the calls to these methods and then pass the call on to the &#8220;real&#8221; pthread implementation. From the application&#8217;s point-of-view, the behavior of the thread methods are the same&#8230; the tracking is transparent.</p>
<p><strong>Output</strong></p>
<p>Each time <code>pthread_create()</code> is called, a stacktrace and timestamp are printed to stdout:</p>
<p><code>Thread Created: 3047947120<br />
[bt] Thu Jul  8 16:31:23 2010</code></p>
<p><code>[bt] ./a.out(StartService(unsigned long*, int))<br />
[bt] ./a.out(Initialize())<br />
[bt] ./a.out(main+0xb) [0x80489fa]<br />
[bt] /lib/libc.so.6(__libc_start_main+0xe6) [0xb5bbb6]<br />
[bt] ./a.out() [0x8048811]</code></p>
<p>The first line (&#8220;Thread Created&#8221;) gives the value of the <code>pthread_t</code> handle, so you can later track where rogue threads where created. The next line is the time when the create happened. The remaining lines are the call stack that led to the <code>pthread_create()</code> call.</p>
<p>Each time <code>pthread_join()</code> is called, similar information is printed to stdout:</p>
<p><code>Thread Joined: 3047947120<br />
[bt] Thu Jul  8 16:31:31 2010</code></p>
<p><code>[bt] ./a.out(Terminate())<br />
[bt] ./a.out(main+0x10) [0x80489ff]<br />
[bt] /lib/libc.so.6(__libc_start_main+0xe6) [0xb5bbb6]<br />
[bt] ./a.out() [0x8048811]</code></p>
<p>When the application terminates (cleanly or uncleanly), a summary of the current state of the application threads is printed:</p>
<p><code>exit_handler()<br />
[Thread Summary]<br />
Total Created: 573<br />
Total Joined: 568<br />
Total Running: 5</code></p>
<p>In this case, you&#8217;ll notice that 5 threads were never joined on.</p>
<p><strong>Limitations</strong></p>
<p>ThreadNinja only tracks calls to <code>pthread_create()</code> and <code>pthread_join(</code>). This means calls like <code>system()</code>, <code>exec()</code>, and <code>fork()</code> are not tracked. Also, calls to <code>pthread_cancel()</code> are not tracked. We had started adding code to track pthread mutexes and stuff, but it turned out we didn&#8217;t need it. Feel free to add support for all this stuff and submit changes to the Google code site.</p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://tdistler.com/2010/07/08/threadninja-finding-rogue-posix-threads/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Stop Stealing My File Descriptors!</title>
		<link>http://tdistler.com/2010/06/18/stop-stealing-my-file-descriptors</link>
		<comments>http://tdistler.com/2010/06/18/stop-stealing-my-file-descriptors#comments</comments>
		<pubDate>Fri, 18 Jun 2010 21:48:43 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Code Monkey]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://tdistler.com/?p=313</guid>
		<description><![CDATA[We ran into a weird problem the other day where our Linux video display appliance would lose audio support when the process was restarted. The audio was supposed to play through a custom joystick-keyboard that was attached via USB (the keyboard is used by security guards to PTZ cameras, control monitors, etc). The audio could [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://tdistler.com/wp-content/uploads/2010/06/sherlock_tux.jpg"><img class="alignright size-thumbnail wp-image-319" title="sherlock_tux" src="http://tdistler.com/wp-content/uploads/2010/06/sherlock_tux-150x150.jpg" alt="Sherlock Tux" width="150" height="150" /></a>We ran into a weird problem the other day where our Linux video display appliance would lose audio support when the process was restarted. The audio was supposed to play through a custom joystick-keyboard that was attached via USB (the keyboard is used by security guards to PTZ cameras, control monitors, etc). The audio could be heard just fine when the box first booted, but if the application restarted audio would be lost.</p>
<p>Looking at the logs, we found that our audio pipeline was failing to open <code>/dev/dsp</code> on the restart. We then used <code>lsof</code> to list the open file descriptors to see which process currently held <code>/dev/dsp</code>:</p>
<p><code># lsof | grep /dev/dsp<br />
ntpd   18857    root   16u    CHR     14,3    180099 /dev/dsp<br />
</code></p>
<p>What!?!?&#8230; why the heck is NTP opening the sound device and how did it steal it from us??? After some discussion we started remembering a problem in the past with <code>ntpd</code> stealing our SNMP diagnostics port. This just didn&#8217;t make any sense.</p>
<p>Digging into our appliance code, we found this line:</p>
<p><code>system( "service ntpd restart" );</code></p>
<p>This would be called each time we were notified by the security system that the NTP server address had changed (which fired once each time the process was started so we could get the initial address). But this still didn&#8217;t explain why NTP took over ownership of our file descriptors on restart.</p>
<p>Long story short: <code>system()</code> is implemented as <code>fork()</code> followed by <code>execv()</code>. By default, <code>fork()</code> gives a copy of the parent&#8217;s file descriptors to the child process (i.e. the <code>ntpd</code> child process got a copy of the <code>/dev/dsp</code> file descriptor). To prevent this, you have to set the <code>FD_CLOEXEC</code> flag on the file desciptors you don&#8217;t want copied.</p>
<p>For example:</p>
<p><code>fd = open( "/dev/dsp", O_RDWR );<br />
fcntl( fd, F_SETFD, FD_CLOEXEC );</code></p>
<p><strong>Conclusion</strong>: setting the <code>FD_CLOEXEC</code> flag on the <code>/dev/dsp</code> file descriptor fixed the problem for audio. However, most of the other file desciptors still got owned by <code>ntpd</code>. Did we go back and set the <code>FD_CLOEXEC</code> flag on all file descriptors, you ask? Nope. It turns out we had a script monitoring the NTP config file and restarting ntpd for us when the file got updated&#8230; we just had to update the config file and remove the <code>system( "service ntpd restart" )</code> call.</p>
<p>Oh, and the reason audio worked on first boot but not subsequent restarts was due to a weird race condition around when <code>/dev/dsp</code> got opened.</p>
]]></content:encoded>
			<wfw:commentRss>http://tdistler.com/2010/06/18/stop-stealing-my-file-descriptors/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>StackHash and Application Crashes on Windows</title>
		<link>http://tdistler.com/2009/04/10/stackhash-and-application-crashes-on-windows</link>
		<comments>http://tdistler.com/2009/04/10/stackhash-and-application-crashes-on-windows#comments</comments>
		<pubDate>Fri, 10 Apr 2009 15:00:50 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Code Monkey]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[funny]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://tdistler.com/?p=186</guid>
		<description><![CDATA[We got an interesting application crash yesterday with a confusing message similar to this: Fault bucket 42424242, type 1 Event Name: APPCRASH Response: None Cab Id: 0 Problem signature: P1: MyApp.exe P2: 1.42.42.42 P3: 598773cf P4: StackHash_ac62 P5: 0.0.0.0 P6: 00000000 P7: c0000007 P8: 00000000 P9: P10: We spent some time wondering if our crypto [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" src="/media/images/software_bug.jpg" alt="Software Bug" />We got an interesting application crash yesterday with a confusing message similar to this:</p>
<p class="codeblock">
Fault bucket 42424242, type 1<br />
Event Name: APPCRASH<br />
Response: None<br />
Cab Id: 0<br/><br />
Problem signature:<br />
P1: MyApp.exe<br />
P2: 1.42.42.42<br />
P3: 598773cf<br />
P4: StackHash_ac62<br />
P5: 0.0.0.0<br />
P6: 00000000<br />
P7: c0000007<br />
P8: 00000000<br />
P9:<br />
P10:
</p>
<p>We spent some time wondering if our crypto libraries were the problem (we just made some changes recently), but concluded that was unlikely. So what the heck is the &#8220;StackHash&#8221; module? Did our trashed stack cause the kernel to think we were a different module? Nope.</p>
<p>The answer is that the Windows executive couldn&#8217;t identify the module we were in when the application crashed (it uses the instruction pointer to determine what code was executing). In this case, the kernel simply takes a hash of the stack so at least we might be able to identify if we&#8217;ve seen this exact crash before. Here&#8217;s the answer summarized by an engineer from Microsoft:</p>
<blockquote><p>In the OS when I try to get a faulting module name it is possible that there is no module laoded (sic) at that address. For example in this case the EIP was zero. So in those cases where a module is not loaded and it is not also in the unloaded module list, I take a stack hash of the stack so that we can identify this crash from other crashes where also the module is not known.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://tdistler.com/2009/04/10/stackhash-and-application-crashes-on-windows/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>How to Print a Stack Backtrace Programatically in Linux</title>
		<link>http://tdistler.com/2008/11/15/how-to-print-a-stack-backtrace-programatically-in-linux</link>
		<comments>http://tdistler.com/2008/11/15/how-to-print-a-stack-backtrace-programatically-in-linux#comments</comments>
		<pubDate>Sun, 16 Nov 2008 00:36:58 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Code Monkey]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://tdistler.com/?p=149</guid>
		<description><![CDATA[So here&#8217;s a cool feature of GNU&#8217;s implementation of libc: you can get a stack backtrace (as an array of strings) dynamically in your code. This can be really useful when trying to determine the code path taken when an error occurs. Most times, it&#8217;s faster to just run the code in a debugger and [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" style="float: right;" src="/media/images/GNULogo.jpg" alt="GNU Logo" />So here&#8217;s a cool feature of <a title="GUN libc" href="http://www.gnu.org/software/libc/" target="_blank">GNU&#8217;s implementation of libc</a>: you can get a stack backtrace (as an array of strings) dynamically in your code. This can be really useful when trying to determine the code path taken when an error occurs. Most times, it&#8217;s faster to just run the code in a debugger and use it to display a backtrace, but there are instances when doing it programmatically is your best option. For example, you could get a backtrace in your application&#8217;s exception handler and use it to augment error log messages.</p>
<p>First, you need to include execinfo.h to your code:</p>
<p><code>#include &lt;execinfo.h&gt;</code></p>
<p>Next, call the <code>backtrace()</code> function to get an array of void pointers that represents the current stack (the pointers are the return addresses for each stack frame).</p>
<p><code>void* tracePtrs[100];<br />
int count = backtrace( tracePtrs, 100 );</code></p>
<p>The <code>backtrace()</code> function returns the number of entries in the array (read the man pages for more info about the array size).</p>
<p>Finally, you need to resolve the function names associated with the pointers. You have 2 options: <code>backtrace_symbols()</code> and <code>backtrace_symbols_fd()</code>. Both of these methods resolve the pointers to strings, but the difference is that <code>backtrace_symbols()</code> allocates the strings on the heap while <code>backtrace_symbols_fd()</code> writes the strings to a file descriptor that you can read. Just keep in mind that <code>backtrace_symbols()</code> won&#8217;t work if the heap has been trashed.</p>
<p>Here&#8217;s an example using <code>backtrace_symbols()</code>:</p>
<pre><code>char** funcNames = backtrace_symbols( tracePtrs, count );

// Print the stack trace
for( int ii = 0; ii &lt; count; ii++ )
    printf( "%s\n", funcNames[ii] );

// Free the string pointers
free( funcNames );</code></pre>
<p><br/>NOTE: Make sure you call <code>free()</code> on the array of strings returned from <code>backtrace_symbols()</code>.</p>
<p>For more information, <a title="Linux Journal: Stack Backtraceing Inside Your Program" href="http://www.linuxjournal.com/article/6391" target="_blank">here&#8217;s a good article from the Linux Journal</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://tdistler.com/2008/11/15/how-to-print-a-stack-backtrace-programatically-in-linux/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debugging: ACE, Windows, and Memory Leak Detection</title>
		<link>http://tdistler.com/2008/11/14/debugging-ace-windows-and-memory-leak-detection</link>
		<comments>http://tdistler.com/2008/11/14/debugging-ace-windows-and-memory-leak-detection#comments</comments>
		<pubDate>Sat, 15 Nov 2008 00:26:30 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Code Monkey]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://tdistler.com/?p=148</guid>
		<description><![CDATA[The Windows development environment provided by VisualStudio has some neat tools for detecting memory leaks in code. You simply #define _CRTDBG_MAP_ALLOC before including your headers, and #include &#60;crtdbg.h&#62; as the last header: #define _CRTDBG_MAP_ALLOC // Include other header files here #include &#60;crtdbg.h&#62; Then, you call _CrtDumpMemoryLeaks() before your application exits. If your program exits at [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" style="float: right;" src="/media/images/ACE_Logo.jpg" alt="ACE Logo" />The Windows development environment provided by VisualStudio has some neat tools for detecting memory leaks in code. You simply <code>#define _CRTDBG_MAP_ALLOC</code> before including your headers, and <code>#include &lt;crtdbg.h&gt; </code>as the last header:</p>
<p><code>#define _CRTDBG_MAP_ALLOC<br />
// Include other header files here<br />
#include &lt;crtdbg.h&gt;</code></p>
<p>Then, you call <code>_CrtDumpMemoryLeaks()</code> before your application exits. If your program exits at many points, you can alternatively call <code>_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF )</code> at the beginning of you application, which will cause the leaks to also be printed when it exits. The results are printed to the Debug Window and look like the following:</p>
<pre><code>Detected memory leaks!
Dumping objects -&gt;
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) :
{18} normal block at 0x00780E80, 64 bytes long.
Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.</code></pre>
<p><br/></p>
<p>Cool, Huh?! However, some libraries don&#8217;t play nice with this, as I explain below.</p>
<p><span id="more-148"></span><strong>Memory Leak Detection and ACE:</strong></p>
<p>I&#8217;ve mentioned <a title="Adaptive Communication Environment" href="http://www.cs.wustl.edu/~schmidt/ACE.html" target="_blank">ACE</a> before in a previous post <a title="C++ Portable Runtime Evaluation" href="http://tdistler.com/2007/11/01/c-portable-runtime-evaluation" target="_blank">evaluating portable runtime environments</a>. It&#8217;s a pretty cool set of libraries that provides a portable OS abstraction layer and communication environment. If you write your application using ACE, it will compile and run on a whole host of system types&#8230; but I digress.</p>
<p>One problem with ACE is that it conflicts with the Windows leak check code. If you define <code>_CRTDBG_MAP_ALLOC</code> before the ACE headers, you will get build errors like the following:</p>
<p><code>1&gt;..\include\ace\os_ns_unistd.h(154) : error C2059: syntax error : 'constant'</code></p>
<p>What&#8217;s happening is that the ACE headers try to enable the leak detection code themselves, which causes the overridden memory functions to conflict with the original versions. In essence, ACE is trying to define <code>malloc</code> (for example) after <code>crtdbg.h</code> has already overridden it.</p>
<p>So, how do you get your application to build with leak detection and ACE? Define <code>_CRTDBG_MAP_ALLOC</code> after the ACE headers (Remember: the ACE headers must <em>always </em>be included before other headers&#8230; another complaint I have).</p>
<p><code>#include "ace/ACE.h"<br />
#define _CRTDBG_MAP_ALLOC<br />
// Include other headers here<br />
#include &lt;crtdbg.h&gt;</code></p>
<p>Okay, your application should compile now. Run it and look at the output window. For a simple application that does nothing but return, this is what I see:</p>
<pre><code>Detected memory leaks!
Dumping objects -&gt;
{178} normal block at 0x00CDBD78, 50 bytes long.
Data: &lt;---TEST---      &gt; 2D 2D 2D 48 45 4C 4C 4F 2D 2D 2D 00 CD CD CD CD
{173} normal block at 0x002EAAC8, 8 bytes long.
Data: &lt;  .     &gt; C0 A1 2E 00 CD CD CD CD
{172} normal block at 0x002EAA78, 20 bytes long.
Data: &lt;4  Z       Z    &gt; 34 90 F3 5A 0A 01 00 00 F3 17 DA 5A 03 00 00 00
{171} normal block at 0x002EAA18, 32 bytes long.
Data: &lt;   Z  ]         &gt; C4 91 F3 5A C0 FB 5D 00 FF FF FF FF 00 00 00 00
{170} normal block at 0x002EA9B8, 32 bytes long.
Data: &lt;   Z  ]         &gt; C4 91 F3 5A 88 FB 5D 00 FF FF FF FF 00 00 00 00
{169} normal block at 0x002EA958, 32 bytes long.
Data: &lt;   ZP ]         &gt; B4 91 F3 5A 50 FB 5D 00 FF FF FF FF 00 00 00 00
{168} normal block at 0x002EA910, 8 bytes long.
Data: &lt;   Z    &gt; D4 91 F3 5A 00 00 00 00

... Truncated for sanity (30 more leaks follow) ...

Object dump complete.
The program '[14288] simple.exe: Native' has exited with code 0 (0x0).</code></pre>
<p><br/></p>
<p>These are very likely false-positives, but they are annoying. There are other problems too:</p>
<ol>
<li>There are no file/line numbers printed, so we can&#8217;t track down the code to verify if the errors are real or not.</li>
<li>File and line numbers don&#8217;t show up for errors <em>outside </em>of ACE (the first leak is mine).</li>
<li>All this output adds noise that can hide other memory leaks (I wish Microsoft would add a way to suppress errors we don&#8217;t care about&#8230; props to <a title="Valgrind" href="http://valgrind.org/" target="_blank">Valgrind</a> for doing this).</li>
</ol>
<p>Anyways, all of this is quite frustrating. I was hunting around on the ACE newsgroup for solutions and could only find <a title="ACE Newsgroup: checking application for memory leaks" href="http://groups.google.co.kr/group/comp.soft-sys.ace/browse_thread/thread/cc1c9642efaf17d7/36e97ef862bb84c0?#36e97ef862bb84c0" target="_blank">this thread</a>:</p>
<blockquote><p>&#8220;In general, we track memory issues on Windows using Purify, so you might try going that route.&#8221;<br />
Douglas Schmidt</p></blockquote>
<blockquote><p>&#8220;Correct. This memory tracking scheme and the ACE restrictions onheader file placement are at odds here. You have two choices:</p>
<p>1. Use a different memory tracking scheme such as Purify, which Doug<br />
suggested.</p>
<p>2. Develop, or sponsor, necessary changes to remove ACE&#8217;s restrictions<br />
on header file placement.&#8221;<br />
Steve Huston</p></blockquote>
<p>So there you have it: ACE and Window memory leak detection don&#8217;t play nice together. I hate encountering a problem without a viable solution&#8230; it bugs me (no pun intended). I even tried modifying the ACE headers with marginal results (it came close to compiling). Oh well&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://tdistler.com/2008/11/14/debugging-ace-windows-and-memory-leak-detection/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging: C++ Templates, Breakpoints, and GDB</title>
		<link>http://tdistler.com/2008/11/13/debugging-c-templates-brekpoints-and-gdb</link>
		<comments>http://tdistler.com/2008/11/13/debugging-c-templates-brekpoints-and-gdb#comments</comments>
		<pubDate>Fri, 14 Nov 2008 01:28:57 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Code Monkey]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://tdistler.com/?p=146</guid>
		<description><![CDATA[Debugging C++ templates is difficult. Debugging C++ templates with GDB can be an act of torture for even seasoned GDB users. I like GDB, but there are some tricks you should know when using it to debug templates. In this post, I deal with setting breakpoints. Breakpoint Basics: Setting a breakpoint in GDB is supposed [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" style="float: right;" src="/media/images/GNULogo.jpg" alt="GNU Logo" />Debugging C++ templates is difficult. Debugging C++ templates with GDB can be an act of torture for even seasoned GDB users. I like GDB, but there are some tricks you should know when using it to debug templates. In this post, I deal with setting breakpoints.</p>
<p><strong>Breakpoint Basics:</strong></p>
<p>Setting a breakpoint in GDB is supposed to be simple. Here we set a breakpoint at line 50 in file main.cpp:</p>
<p class="codeblock">(gdb) b main.cpp:50<br />
Breakpoint 1 at 0x804937a: file main.cpp, line 50.</p>
<p>We can also use the function name and GDB will attempt to find the correct location for us:</p>
<p class="codeblock">(gdb) b DoSomething<br />
Breakpoint 2 at 0&#215;8049334: file main.cpp, line 150</p>
<p>Simple, right? Just wait&#8230;</p>
<p><strong>Breakpoint Gotchas:</strong></p>
<p>GDB&#8217;s breakpoint logic is pretty handy for simple projects, but it can break down fast when things get more complicated.</p>
<p>For example, let&#8217;s say your application is plugin-driven, with each plugin being a separate library. Now assume each plugin has a Plugin.cpp file under it&#8217;s own Source directory. Try to set a breakpoint in the Initialize() method of the Plugin class:</p>
<p class="codeblock">(gdb) b Initialize<br />
Breakpoint 3 at 0&#215;8049717: file main.cpp, line 230</p>
<p>Oops! There is an Initialize() method in main.cpp and GDB thought that&#8217;s where we wanted to put it: wrong!</p>
<p><span id="more-146"></span>Okay, let&#8217;s be more specific:</p>
<p class="codeblock">(gbd) b Plugin::Initialize<br />
Breakpoint 4 at 0&#215;8046194: file Source/Plugin.cpp, line 89</p>
<p>Okay, that looks better, but <em>which </em>Plugin.cpp file did GDB put the breakpoint? How do we know it&#8217;s the file for the plugin we want?</p>
<p>If we used namespaces, then we can get more specific:</p>
<p class="codeblock">(gdb) b MY_PLUGIN_A::Plugin::Initialize<br />
Breakpoint 5 at 0&#215;8050039: file Source/Plugin.cpp, line 130</p>
<p>We can see from the address and line number that the previous breakpoint was at the wrong place. Okay, moving on&#8230;</p>
<p><strong>Setting Breakpoint in Templates:</strong></p>
<p>Templates can be much harder to set breakpoints in because we have to specify the <em>exact </em>prototype for the fully-defined template. We as programmers are used to the compiler handling the template type stuff for us, so it can be difficult to guess the correct type.</p>
<p>For example, assume we have some abstract class BarAbstract that uses the template Foo&lt;&gt; to make it concrete. Now assume we&#8217;re really clever and we want to hide this from the users of our class. We could use a typedef to hide the true type:</p>
<p class="codeblock">typedef Foo&lt;BarAbstract&gt; Bar;</p>
<p>Now, all the user needs to do is instantiate Bar without a thought to the Foo&lt;&gt; template.</p>
<p class="codeblock">int DoSomething()<br />
{<br />
Bar b;<br />
return b.Baz();<br />
}</p>
<p>So far so good? Okay, now how the heck do you set a breakpoint in the Baz() method of class Bar?</p>
<p>The quick answer: use objdump, c++filt, and grep to find the complete definition that GDB will need.</p>
<p class="codeblock">$ objdump -t libMyLib.so | c++filt | grep &#8216;BarAbstract.*Baz&#8217;<br />
0000d2d6 w F .text 0000000a     MY_PLUGIN_A::Foo&lt;MY_PLUGIN_A::BarAbstract&gt;::Baz()</p>
<p>Now, simply copy-n-paste the full method definition to GDB when setting the breakpoint:</p>
<p class="codeblock">(gdb) b MY_PLUGIN_A::Foo&lt;MY_PLUGIN_A::BarAbstract&gt;::Baz()<br />
Breakpoint 6 at 0&#215;8048890: file Source/Bar.cpp, line 355</p>
<p>That&#8217;s it! Happy debugging!</p>
]]></content:encoded>
			<wfw:commentRss>http://tdistler.com/2008/11/13/debugging-c-templates-brekpoints-and-gdb/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

