<p>Today I spent a few hours debugging a hang R process that left a zombie <code>sh</code> which so far suggests bug (race condition?) in R’s <code>system2()</code> call. Anyway, it soon turned out that the only way to see what’s happening with R is to use <code>gdb</code>, which I personally dread. It is so because I haven’t found a way to dump R’s variables in <code>gdb</code> without constructing lengthy expressions based on the definition of <code>SEXP</code> as can be found in <code>Rinternals.h</code>. Well, I haven’t until today.</p>
<p>First, I came across <a target="_blank" rel="nofollow" href="http://www.theresearchkitchen.com/archives/498">this (old) blog post</a>. Rory Winston shows there how one can use <code>gdb</code>‘s scripting language to add a simple pretty-printer for R. For some reason parts of it didn’t execute in my <code>gdb</code> (7.2). And then I remembered that <code>gdb</code> can be scripted in Python! Here’s a Python-beautified excerpt from <code>gdb</code>‘s backtrace…:</p>
<pre>#10 0x00007ffff787c9ac in Rf_applyClosure (
   call=g(c(&quot;a&quot;, &quot;b&quot;), 1, c(1.0, 2.0), True),
   op=&lt;closure: a=, b=, c=, d=, { Sys.sleep(3.0) }>, 
   arglist=&lt;promise: c(&quot;a&quot;, &quot;b&quot;)>,
           &lt;promise: 1>,
           &lt;promise: c(1.0, 2.0)>,
           &lt;promise: True>,
   rho=&lt;environment>,
   suppliedvars=) at ../../../src/main/eval.c:1135
</pre>
<p>…and then the same line without beautification:</p>
<pre>#10 0x00007ffff787c9ac in Rf_applyClosure (call=0x55555626bf18, 
                                           op=0x55555626b720,
                                           arglist=0x55555626c6d8,
                                           rho=0x55555626b818,
                                           suppliedvars=0x555555769b28)
    at ../../../src/main/eval.c:1135</pre>
<p>Nice, huh? So much easier to understand the internal state of R process…</p>
<p>The beautifier (aka. “pretty-printer”) is available <a target="_blank" rel="nofollow" href="https://github.com/lbartnik/rgdbprinter">here (GitHub)</a>. There are a few more examples there together with a short How-To on how to use it in a gdb session.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/randomremarksblog.wordpress.com/441/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/randomremarksblog.wordpress.com/441/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/kuzXoGYZEcE">