Inspecting R in GDB (with Python)

December 21, 2016

(This article was first published on R – Random Remarks, and kindly contributed to R-bloggers)

Today I spent a few hours debugging a hang 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),
   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.

To leave a comment for the author, please follow the link and comment on their blog: R – Random Remarks. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)