Inspecting R in GDB (with Python)

Today I spent a few hours debugging a hanging R process that left a zombie sh which so far suggests bug (race condition?) in R’s system2() call. Anyway, it soon turned out that the only way to see what’s happening with R is to use gdb, which I personally dread. It is so because I haven’t found a way to dump R’s variables in gdb without constructing lengthy expressions based on the definition of SEXP as can be found in Rinternals.h. Well, I haven’t until today.

First, I came across this (old) blog post. Rory Winston shows there how one can use gdb‘s scripting language to add a simple pretty-printer for R. For some reason parts of it didn’t execute in my gdb (7.2). And then I remembered that gdb can be scripted in Python! Here’s a Python-beautified excerpt from gdb‘s backtrace…:

#10 0x00007ffff787c9ac in Rf_applyClosure (
   call=g(c("a", "b"), 1, c(1.0, 2.0), True),
   op=<closure: a=, b=, c=, d=, { Sys.sleep(3.0) }>, 
   arglist=<promise: c("a", "b")>,
           <promise: 1>,
           <promise: c(1.0, 2.0)>,
           <promise: True>,
   suppliedvars=) at ../../../src/main/eval.c:1135

…and then the same line without beautification:

#10 0x00007ffff787c9ac in Rf_applyClosure (call=0x55555626bf18, 
    at ../../../src/main/eval.c:1135

Nice, huh? So much easier to understand the internal state of R process…

The beautifier (aka. “pretty-printer”) is available here (GitHub). There are a few more examples there together with a short How-To on how to use it in a gdb session.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s