Inspecting R in GDB (with Python)

[This article was first published on R – Random Remarks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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),
   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>,
   rho=<environment>,
   suppliedvars=) at ../../../src/main/eval.c:1135

…and then the same line without beautification:

#10 0x00007ffff787c9ac in Rf_applyClosure (call=0x55555626bf18, 
                                           op=0x55555626b720,
                                           arglist=0x55555626c6d8,
                                           rho=0x55555626b818,
                                           suppliedvars=0x555555769b28)
    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.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)