<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>Fun with ones and zeros - bugs</title>
<description><![CDATA[Barry's notes on computer software and hardware]]></description>
<link>/blog/tags/bugs</link>
<lastBuildDate>Thu, 28 May 2026 06:52:48 -0700</lastBuildDate>
<item>
<title>Simple debugging output in C</title>
<link>/blog/entries/simple-debugging-output-c</link>
<pubDate>Sun, 03 Apr 2011 18:35:29 -0700</pubDate>
<author>bp@barryp.org (Barry Pederson)</author>
<description><![CDATA[<p>I don't do a whole lot of C programming, but when I do it tends to be in difficult environments like Apache modules or Samba VFS modules, where you can't just do simple printfs to get some output from your program.<br />
</p>
<p>I've come up with this small chunk of code I can plop in a C file to allow for optionally writing out useful information to a file somewhere on the disk.</p>
<div class="source"><pre><span class="cp">#ifdef DEBUG_FILENAME</span>
<span class="cp">    #include &lt;stdarg.h&gt;</span>
<span class="cp">    #include &lt;stdio.h&gt;</span>
<span class="cp">    #include &lt;time.h&gt;</span>
<span class="cp">    #define QUOTE(name) #name</span>
<span class="cp">    #define STR(macro) QUOTE(macro)</span>

    <span class="k">static</span> <span class="kt">void</span> <span class="nf">debug_log</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">msg</span><span class="p">,</span> <span class="p">...)</span> <span class="p">{</span>
        <span class="kt">char</span> <span class="n">timestamp</span><span class="p">[</span><span class="mi">32</span><span class="p">];</span> <span class="c1">// really only need 21 bytes here</span>
        <span class="kt">time_t</span> <span class="n">now</span><span class="p">;</span>
        <span class="kt">va_list</span> <span class="n">ap</span><span class="p">;</span>
        <span class="kt">FILE</span> <span class="o">*</span><span class="n">f</span><span class="p">;</span>

        <span class="n">now</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">);</span>
        <span class="n">strftime</span><span class="p">(</span><span class="n">timestamp</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">timestamp</span><span class="p">),</span> <span class="s">&quot;%Y-%m-%d %H:%M:%S &quot;</span><span class="p">,</span> <span class="n">localtime</span><span class="p">(</span><span class="o">&amp;</span><span class="n">now</span><span class="p">));</span>

        <span class="n">f</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="n">STR</span><span class="p">(</span><span class="n">DEBUG_FILENAME</span><span class="p">),</span> <span class="s">&quot;a&quot;</span><span class="p">);</span>

        <span class="n">fputs</span><span class="p">(</span><span class="n">timestamp</span><span class="p">,</span> <span class="n">f</span><span class="p">);</span>

        <span class="n">va_start</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="n">msg</span><span class="p">);</span>
        <span class="n">vfprintf</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">ap</span><span class="p">);</span>
        <span class="n">va_end</span><span class="p">(</span><span class="n">ap</span><span class="p">);</span>

        <span class="n">fputc</span><span class="p">(</span><span class="sc">&#39;\n&#39;</span><span class="p">,</span> <span class="n">f</span><span class="p">);</span>
        <span class="n">fclose</span><span class="p">(</span><span class="n">f</span><span class="p">);</span>
    <span class="p">}</span>
<span class="cp">#else</span>
<span class="cp">    #define debug_log</span>
<span class="cp">#endif</span>
</pre></div>

<p>Within your program, you'd just sprinkle in calls to <code>debug_log()</code> with a format string and optional arguments, such as:</p>
<div class="source"><pre><span class="n">x</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
<span class="n">y</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="n">debug_log</span><span class="p">(</span><span class="s">&quot;Currently, x=%d, y=%d&quot;</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span>
</pre></div>

<p>The code can then be enabled and configured to output to <code>/tmp/foo.log</code> (for example), by adding either</p>
<div class="source"><pre><span class="cp">#define DEBUG_FILENAME /tmp/foo.log</span>
</pre></div>

<p>to the top of your source file, or even more slickly for some things, from the commandline with</p>
<pre><code>cc -DDEBUG_FILENAME=/tmp/foo.log myprogram.c
</code></pre>
<p>When the program is run, in your <code>/tmp/foo.log</code> file you'd find something like:</p>
<pre><code>2011-04-03 20:30:05 Currently, x=5, y=10
</code></pre>
<p>If you don't define <code>DEBUG_FILENAME</code>, the code basically goes away, shouldn't take up any space in your binary at all.</p>]]></description>
</item>
<item>
<title>RabbitMQ FreeBSD port </title>
<link>/blog/entries/rabbitmq-freebsd-port</link>
<pubDate>Mon, 01 Sep 2008 17:29:06 -0700</pubDate>
<author>bp@barryp.org (Barry Pederson)</author>
<description><![CDATA[
<p>I was happy to see a FreeBSD port added for RabbitMQ, <a href="http://www.freshports.org/net/rabbitmq">net/rabbitmq</a>,  although I found a couple problems with it:  it doesn't start automatically when your machine or jail boots, and when building the <a href="http://hopper.squarespace.com/blog/2008/1/12/introducing-the-erlang-amqp-client.html">rabbitmq-erlang-client</a>, it errors out with:
</p>
<pre><code>src/amqp_channel.erl:28: can't find include lib &quot;rabbitmq_server/include/rabbit.hrl&quot;
src/amqp_channel.erl:29: can't find include lib &quot;rabbitmq_server/include/rabbit_framing.hrl&quot;
</code></pre><p>I worked on the port a bit, and submitted a bug report and patch, <a href="http://www.freebsd.org/cgi/query-pr.cgi?pr=127033">ports/127033</a>, that fixes these problems.
</p>


]]></description>
</item>
<item>
<title>Full Text Searching with SQLite</title>
<link>/blog/entries/full-text-searching-sqlite</link>
<pubDate>Sun, 03 Dec 2006 16:20:17 -0800</pubDate>
<author>bp@barryp.org (Barry Pederson)</author>
<description><![CDATA[
<p>I'd like to add a search feature back on this site.  Previously, I had an arrangement setup with PyBlosxom and ht://Dig, but now that it's Django-powered, I'd like to do something that worked directly from the database instead of crawling the site like ht://Dig did.
</p>
<p>After looking a while at various text search engines, I remembered seeing that <a href="http://sqlite.org">SQLite</a> just added an <a href="http://www.sqlite.org/cvstrac/wiki?p=FtsOne">FTS1</a> module in version 3.3.8, which sounds pretty easy to use.  Unfortunately the FreeBSD port <code>databases/sqlite3</code> doesn't build with that feature.
</p>
<p>After poking around a bit, I got it to build with FTS manually, and after a whole bunch more messing around, came up with a patch to add an option to the port to build sqlite3 with FTS.   The patch has been submitted to the FreeBSD bug tracker as <a href="http://www.freebsd.org/cgi/query-pr.cgi?pr=ports/106281">ports/106281</a>.  Hopefully I have all my ducks lined up on that.
</p>
<hr />
<p>Anyhow, in testing the FTS a bit, I found one thing they only hint at on the SQLite website.  There's a <a href="http://www.tartarus.org/~martin/PorterStemmer/index.html">Porter stemmer</a> built in, even though the wiki says: &quot;The module does not perform stemming of any sort.&quot;  You activate it by adding <code>tokenize porter</code> to the table declaration, for example (adapted from their example).
</p>
<div class="source"><pre><span class="k">CREATE</span> <span class="n">VIRTUAL</span> <span class="k">TABLE</span> <span class="n">recipe</span> <span class="k">USING</span> <span class="n">FTS1</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">ingredients</span><span class="p">,</span> <span class="n">tokenize</span> <span class="n">porter</span><span class="p">);</span>
</pre></div>
<p>once you've done that, and inserted some sample data:
</p>
<div class="source"><pre><span class="k">INSERT</span> <span class="k">INTO</span> <span class="n">recipe</span> <span class="k">VALUES</span><span class="p">(</span><span class="s1">&#39;broccoli stew&#39;</span><span class="p">,</span> <span class="s1">&#39;broccoli peppers cheese tomatoes&#39;</span><span class="p">);</span>
</pre></div>
<p>the searches don't have to be quite as exact, for example:
</p>
<div class="source"><pre><span class="k">SELECT</span> <span class="n">name</span> <span class="k">FROM</span> <span class="n">recipe</span> <span class="k">WHERE</span> <span class="n">ingredients</span> <span class="k">MATCH</span> <span class="s1">&#39;pepper&#39;</span>
</pre></div>
<p>hits the 'broccoli stew' recipe even through it has 'peppers' and you searched for 'pepper'.
</p>
<p>Not sure why the Porter stemmer isn't documented in the SQLite wiki, perhaps it's still a work in progress or being changed for <a href="http://www.sqlite.org/cvstrac/wiki?p=FtsTwo">FTS2</a>.
</p>
]]></description>
</item>
<item>
<title>Django, SCGI, and AJP</title>
<link>/blog/entries/django-scgi-and-ajp</link>
<pubDate>Mon, 20 Nov 2006 18:33:44 -0800</pubDate>
<author>bp@barryp.org (Barry Pederson)</author>
<description><![CDATA[
<p>I've been doing a lot with Django lately, and initially set it up using mod_python as the Django docs recommend, but still have some <a href="/blog/entries/backend_protocols/">reservations</a> about that kind of arrangement.  I'd like to go back to running it under SCGI or something similar.   <br />
</p>
<p>Django has support builtin for FastCGI, but after trying to install mod_fastcgi in my Apache 2.0.x setup, decided it was a PITA.  mod_scgi is quite easy to setup  in Apache (even though the documentation is mostly nonexistent).  After finding where Django implements its FastCGI support using the <a href="http://www.saddi.com/software/flup/">flup</a> module, I saw that with just a few minor tweaks Django could be made to support all of flup's protocols, including SCGI and AJP (Apache Jserv Protocol).
</p>
<p>AJP turns out to be very interesting because it's included standard with Apache 2.2 as <a href="http://httpd.apache.org/docs/2.2/mod/mod_proxy_ajp.html">mod_proxy_ajp</a>, and can work with <a href="http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html">mod_proxy_balancer</a> - meaning you could setup multiple Django instances and have Apache share the load between them.
</p>
<p>After testing a bit, I <a href="http://code.djangoproject.com/ticket/3047">submitted a patch</a>, and will probably switch to running my Django sites as AJP servers managed by daemontools, and frontended by Apache 2.2
</p>


]]></description>
</item>
<item>
<title>mod_python segfault fixed
</title>
<link>/blog/entries/mod_python_segfault_3</link>
<pubDate>Sun, 12 Feb 2006 19:40:49 -0800</pubDate>
<author>bp@barryp.org (Barry Pederson)</author>
<description><![CDATA[
<p>
Just as a followup, it seems the segfault in mod_python on FreeBSD I <a href="/blog/bugs/mod_python_segfault.html">mentioned before</a>
was <a href="http://www.mail-archive.com/python-dev@httpd.apache.org/msg01062.html">found</a> and fixed.
Turns out to not be any kind of pointer/memory corruption like I thought, but rather a mishandled
return code from an APR (Apache Portable Runtime) function.  Oh well, I got to play with gdb, ddd, and valgrind a bit, which 
is good stuff to be familiar with.
</p>



]]></description>
</item>
</channel>
</rss>