strboul blog

Debugging R package with LLDB


This post gives you a quick walkthrough how to debug an R package calling native C/C++ code with LLDB1.

General notes

Examine R objects

In order to examine R objects, you need to cast the return value of the variables into a base C type so lldb can know what they exactly return. Otherwise, you receive an error like ...has unknown return type; cast the call to its declared return type.

Print and inspect R structures interactively, assuming that you want to inspect a R numeric vector named x:

(lldb) p (double)Rf_PrintValue(x)
(lldb) p (bool)Rf_isReal(x)
(lldb) p (double*)REAL(x) # returns the address
(lldb) p ((double*)REAL(x))[2] # returns the value

Print the whole data.frame (casting doesn't matter so can do any e.g. char, double etc.):

(lldb) p (char)Rf_PrintValue(df)

To set temporary variables within lldb. For R objects:

(lldb) p REAL(VECTOR_ELT(df, 1))
(double *) $0 = 0x0e30..

(lldb) p REAL(VECTOR_ELT(df, 1))[0]
(double) $1 = 3.1415..

(lldb) e double $var=REAL(VECTOR_ELT(df, 1))[0]
(lldb) p $var
(double) $var = 3.1415

Resources


  1. Although this content is mainly for LLDB, there is a lot of overlapping stuff with GDB