<?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>slackito.com</title>
	<atom:link href="http://slackito.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://slackito.com</link>
	<description>slackito, ergo sum</description>
	<lastBuildDate>Thu, 03 May 2012 11:06:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Poor man&#8217;s tracepoints with gdb</title>
		<link>http://slackito.com/2011/12/09/poor-mans-tracepoints-with-gdb/</link>
		<comments>http://slackito.com/2011/12/09/poor-mans-tracepoints-with-gdb/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 11:42:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://slackito.com/?p=85</guid>
		<description><![CDATA[(This article is an adaptation of this thing in Spanish I wrote in 2008. It&#8217;s nothing groundbreaking but a good friend of mine suggested a few days ago that it was still useful to him, so I decided to put it here. The new title is a reference to the poor man&#8217;s profiler, which you [...]]]></description>
			<content:encoded><![CDATA[<p><i>(This article is an adaptation of <a href="http://slack.codemaniacs.com/blog/2008/02/10/trazas-con-gdb/">this thing in Spanish I wrote in 2008</a>. It&#8217;s nothing groundbreaking but <a href="http://pplux.com">a good friend of mine</a> suggested a few days ago that it was still useful to him, so I decided to put it here. The new title is a reference to the <a href="http://poormansprofiler.org/">poor man&#8217;s profiler</a>, which you will probably like if you like this post)</i></p>
<p>Everyone of us has resorted at some point to printf-based debugging. Traces can be very useful, either because you want to see at a glance how a given value is changing over time or because stopping a thread at a breakpoint to examine some values can vanish the same race condition you are trying to debug. Nevertheless, inserting printf statements is a boring task, which makes you recompile everytime you want to change the set of values you want to see.</p>
<p>Ideally we could use tracepoints in our favorite debugger to collect some values and later dump them to a log, and <a href="http://sourceware.org/gdb/onlinedocs/gdb/Tracepoints.html">gdb supports them</a> but as the documentation points out:</p>
<blockquote><p>
The tracepoint facility is currently available only for remote targets. See Targets. In addition, your remote target must know how to collect trace data. This functionality is implemented in the remote stub; however, none of the stubs distributed with gdb support tracepoints as of this writing.
</p></blockquote>
<p>So, if our platform doesn&#8217;t support tracepoints&#8230; are we stuck with printf()? Well, yes and no. It turns out gdb allows us to specify actions to be executed at a breakpoint, so you can tell it to, every time it passes by some point in your program, print whatever you want and continue running. It&#8217;s not as good as the real deal, because a tracepoint would be more lightweight, but it can be helpful nonetheless.</p>
<p>Let&#8217;s see how to do it. Consider the following C program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
        <span style="color: #993333;">double</span> x<span style="color: #339933;">=</span><span style="color:#800080;">2.0f</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">64</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
                x<span style="color: #339933;">*=</span><span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>if we compile it with debug info and launch it on gdb, we can log the values of x doing this:</p>
<pre>
(gdb) break 8
Breakpoint 1 at 0x400463: file a.c, line 8.
(gdb) commands 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
>silent
>printf "x=%g\n",x
>cont
>end
(gdb) r
Starting program: /home/slack/a.out
x=2
x=4
x=8
</pre>
<p>The &#8220;silent&#8221; keyword as first action means that gdb shouldn&#8217;t print the info about the current stack frame it usually prints when stopping at a breakpoint, and the rest should be pretty self-explaining. In addition to gdb&#8217;s printf function you can also use more complex gdb expresions so you could, for instance, traverse a list and log its values or stuff like that :)</p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://slackito.com/2011/12/09/poor-mans-tracepoints-with-gdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use debug information in separate files with gdb</title>
		<link>http://slackito.com/2011/08/24/separate-debug-information-with-gdb/</link>
		<comments>http://slackito.com/2011/08/24/separate-debug-information-with-gdb/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 16:55:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://slackito.com/?p=84</guid>
		<description><![CDATA[One of the things that surprised me when I started using Visual Studio, coming from a Linux programming background, was the amount of files it generates when you create and compile a project. One of these files is usually named project.pdb and contains the debug information for the final executable, along with information for incremental [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that surprised me when I started using Visual Studio, coming from a Linux programming background, was the amount of files it generates when you create and compile a project. One of these files is usually named <code>project.pdb</code> and contains the debug information for the final executable, along with information for incremental linking (Microsoft calls it the &#8220;<a title="PDB file description @ MSDN" href="http://msdn.microsoft.com/en-us/library/yd4f8bd1.aspx">program database</a>&#8220;, hence the PDB extension). It turns out having separate debug information files can be useful when you want to distribute your program in binary form, but still be able to debug it when you get crash reports.</p>
<p>I was curious if the GNU toolchain could do the same, and the answer is <strong>yes</strong>. According to the <a title="GDB Documentation -  Separate debug files" href="http://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html">gdb documentation</a> there are two ways of using debug info from another file:</p>
<ul>
<li>Adding a <em>debug link </em>to the executable file: create the binary with embedded debug information as usual, then copy it to another file, strip it out and add the debug link:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">objcopy <span style="color: #660033;">--only-keep-debug</span> program program.debug
<span style="color: #c20cb9; font-weight: bold;">strip</span> program
objcopy <span style="color: #660033;">--add-gnu-debuglink</span>=program.debug program</pre></div></div>

<p>The debug link contains the file name, and a CRC of the full contents of the debug file. When loading the executable into gdb, it will look in <code>program.debug</code> and <code>.debug/program.debug</code> (paths relative to the executable file) for a file containing debug information. This method works with every executable format that can contain a section called <code>.gnu_debuglink</code>. I tested it on cygwin.</li>
<li>Build ID: <code>ld</code>, the GNU linker, can embed a special section with a build identifier, which can be either a MD5 or SHA1 hash of the output file contents, a random number, or any given bitstring (see the <a title="ld command line options documentation" href="http://sourceware.org/binutils/docs/ld/Options.html">documentation</a> of the &#8211;build-id option). This ID will then be copied and preserved when a debug file is created or the binary is stripped. When a Build ID is present, gdb will try to load a debug file in <code>/&lt;debug-dir&gt;/xx/yyyyyyyy.debug</code>, where:- <code>&lt;debug-dir&gt;</code> is each directory specified with <code>set debug-file-directory</code>,- <code>xx</code>is the first byte of the hexadecimal Build ID bitstring, and- <code>yyyyyyyy</code> is the rest of the bitstring.
<p>This method is supported only on some platforms using ELF format for binaries and the GNU binutils.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://slackito.com/2011/08/24/separate-debug-information-with-gdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatic memoization in C++0x</title>
		<link>http://slackito.com/2011/03/17/automatic-memoization-in-cplusplus0x/</link>
		<comments>http://slackito.com/2011/03/17/automatic-memoization-in-cplusplus0x/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 00:27:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[c++0x]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[memoization]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[variadic templates]]></category>

		<guid isPermaLink="false">http://slackito.com/?p=55</guid>
		<description><![CDATA[Memoization is a pretty well-known optimization technique which consists in &#8220;remembering&#8221; (i.e.: caching) the results of previous calls to a function, so that repeated calls with the same parameters are resolved without repeating the original computation. Some days ago, while trying to show a colleague the benefits of a modern high-level language like Python over [...]]]></description>
			<content:encoded><![CDATA[<p><a href="https://secure.wikimedia.org/wikipedia/en/wiki/Memoization">Memoization</a> is a pretty well-known optimization technique which consists in &#8220;remembering&#8221; (i.e.: caching) the results of previous calls to a function, so that repeated calls with the same parameters are resolved without repeating the original computation.</p>
<p>Some days ago, while trying to show a colleague the benefits of a modern high-level language like Python over C++, I came up with the following snippet:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> memoize<span style="color: black;">&#40;</span>fn<span style="color: black;">&#41;</span>:
     cache = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
     <span style="color: #ff7700;font-weight:bold;">def</span> memoized_fn<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args<span style="color: black;">&#41;</span>:
         <span style="color: #ff7700;font-weight:bold;">if</span> args <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> cache:
             cache<span style="color: black;">&#91;</span>args<span style="color: black;">&#93;</span> = fn<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args<span style="color: black;">&#41;</span>
         <span style="color: #ff7700;font-weight:bold;">return</span> cache<span style="color: black;">&#91;</span>args<span style="color: black;">&#93;</span>
     <span style="color: #ff7700;font-weight:bold;">return</span> memoized_fn</pre></td></tr></table></div>

<p>It is a small function which takes a function as its only parameter, and returns a memoized version of that function. It is short, it shows some interesting Python features, like built-in dictionaries and tuples, or functions as first-class objects, and it should be pretty readable.</p>
<p>To make a fair comparison I needed to code a C++ version too. I was thinking about writing, just to prove my point, the classic boilerplate-filled template to create a function object, and using <a href="http://drdobbs.com/184403813">typelists</a> and compile-time recursion to allow an arbitrary number of parameters. But it would have been quite boring. Also, it turns out that, with the upcoming C++ standard supporting <a href="https://secure.wikimedia.org/wikipedia/en/wiki/C%2B%2B0x#Lambda_functions_and_expressions">lambda functions</a>, <a href="https://secure.wikimedia.org/wikipedia/en/wiki/C%2B%2B0x#Tuple_types">tuples</a> and <a href="https://secure.wikimedia.org/wikipedia/en/wiki/C%2B%2B0x#Variadic_templates">variadic templates</a>, it is possible to get rid of most of the boilerplate and use pretty much the same functional approach. Moreover, gcc 4.5 already supports these things, so I decided to give it a go:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ReturnType, <span style="color: #0000ff;">typename</span>... <span style="color: #007788;">Args</span><span style="color: #000080;">&gt;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">function</span><span style="color: #000080;">&lt;</span>ReturnType <span style="color: #008000;">&#40;</span>Args...<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span> memoize<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">function</span><span style="color: #000080;">&lt;</span>ReturnType <span style="color: #008000;">&#40;</span>Args...<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span> func<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">tuple</span><span style="color: #000080;">&lt;</span>Args...<span style="color: #000080;">&gt;</span>, ReturnType<span style="color: #000080;">&gt;</span> cache<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#91;</span><span style="color: #000080;">=</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#40;</span>Args... <span style="color: #007788;">args</span><span style="color: #008000;">&#41;</span> mutable  <span style="color: #008000;">&#123;</span>
            std<span style="color: #008080;">::</span><span style="color: #007788;">tuple</span><span style="color: #000080;">&lt;</span>Args...<span style="color: #000080;">&gt;</span> t<span style="color: #008000;">&#40;</span>args...<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>cache.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> cache.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>                
                cache<span style="color: #008000;">&#91;</span>t<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> func<span style="color: #008000;">&#40;</span>args...<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">return</span> cache<span style="color: #008000;">&#91;</span>t<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Tricky things to note about the C++ version:</p>
<ul>
<li>The new lambda syntax: the equals sign in <code>[=]</code> means &#8220;capture local variables in the surrounding scope by value&#8221;, which is needed because we are returning the lambda function, and the local variable will disappear at that moment, so we can&#8217;t hold a reference to it. As we are capturing by value and we pretend to change this captured value, the function should be marked as <code>mutable</code> (see &#8220;<a href="http://aristeia.com/Papers/appearing%20and%20disappearing%20consts.pdf"><em>Appearing and disappearing consts in C++</em></a>&#8221; by Scott Meyers)</li>
<li>Lambda functions are function objects of implementation-dependent type, so we need to use std::function as the return type from memoize() to wrap our lambda.</li>
</ul>
<p>I still like the Python version better, as it looks cleaner to me, but I&#8217;m glad the new features can help us reduce the amount of boilerplate where switching to newer languages is not possible (sometimes you just NEED the extra speed). Kudos to the C++ standards committee for the improvements and the gcc team for keeping up with them.</p>
<p><strong>EDIT (2011/03/21)</strong>: Thanks everyone for the feedback, both in the comments here and <a href="http://www.reddit.com/r/programming/comments/g5j1a/automatic_memoization_in_c0x/"> in the reddit thread</a>. Some additional notes:</p>
<ul>
<li>Here you have the <a href="http://slackito.com/blog/wp-content/uploads/2011/03/memoize.cc">complete sample file</a>. I tested it under g++ 4.5.2 on MinGW, with -std=c++0x.</li>
<li>This is a proof of concept, which I did to become familiar with the new language features. As some people pointed out, a map is not the best data structure to use as a cache (it has O(log n) lookups). You will probably do better if you use a hash map (like the new <code>std::unordered_map</code> in C++0x). I chose map just for the sake of clarity. Also, you will need to define an <code>operator&lt;</code> for any type you would like to use (or a hash function for unordered_map).</li>
<li>There were <a href="http://slackito.com/2011/03/17/automatic-memoization-in-cplusplus0x/?preview=true&#038;preview_id=55&#038;preview_nonce=18cc72daa0#comment-96">also</a> <a href="http://www.reddit.com/r/programming/comments/g5j1a/automatic_memoization_in_c0x/c1l6amw">suggestions</a> to use <code>lower_bound</code> or <code>equal_range</code> to avoid the second map lookup and use the resulting iterator also as an insertion hint. I thought about saving the result in a local variable to avoid another lookup, but I wanted it to be as close as possible to the python version, just for clarity. I also didn&#8217;t know about these functions, so thanks for the tip! :D
<li>Some people also pointed out that this example doesn&#8217;t work with recursive functions. That&#8217;s completely true. In <a href="http://stackoverflow.com/questions/5353141/c0x-weird-code/5353187#5353187">this post on stackoverflow</a> the user larsmans suggests that I&#8217;m leaving the implementation of a fixed-point combinator as an exercise to the reader. Maybe it would be a good exercise for the writer, too&#8230; if I&#8217;m able to write something like that it will surely deserve its own post ;D</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://slackito.com/2011/03/17/automatic-memoization-in-cplusplus0x/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Welcome!</title>
		<link>http://slackito.com/2009/12/31/welcome/</link>
		<comments>http://slackito.com/2009/12/31/welcome/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 05:18:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://slackito.com/?p=9</guid>
		<description><![CDATA[I&#8217;m currently moving here from my old website, so expect things to change or move around in the next days/weeks. I hope to move the music, demoscene productions and personal projects soon, but i still haven&#8217;t decided what to do with the posts in my old blog. Maybe I&#8217;ll import the posts, maybe I won&#8217;t. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently moving here from <a href="http://slack.codemaniacs.com">my old website</a>, so expect things to change or move around in the next days/weeks. I hope to move the music, demoscene productions and personal projects soon, but i still haven&#8217;t decided what to do with the posts in <a href="http://slack.codemaniacs.com/blog">my old blog</a>. Maybe I&#8217;ll import the posts, maybe I won&#8217;t.</p>
<p>In the meantime, please check out <a href="http://slackito.com/music/">the music section</a> for some of my creations :)</p>
<p>Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://slackito.com/2009/12/31/welcome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

