This section may be of interest to you even if you are not trying to fix a bug in R. For example, you may have written some C code to interface to R (covered in Chapter 15) and found it to be buggy. In order to run GDB on that C function, you must first run R itself through GDB.
Or, you may be interested in the internals of R, say to determine how you can write efficient R code, and wish to explore the internals by stepping through the R source code with a debugging tool such as GDB.
Although you can invoke R through GDB from a shell command line (see Section 15.1.4), for our purposes here, I suggest using separate windows for R and GDB. Here’s the procedure:
Start R in one window, as usual.
In another window, determine the ID number of your R process. In UNIX family systems, for instance, this is obtained by something like ps -a
.
In that second window, submit GDB’s attach
command with the R process number.
Submit the continue
command to GDB.
You can set breakpoints in the R source code either before continuing or by interrupting GDB later with ctrl-C. See Section 15.1.4 for details for debugging C code called from R. If, on the other hand, you wish to use GDB to explore the R source code, note the following.
The R source code is dominated by S expression pointers (SEXPs), which are pointers to C structs that contain an R variable’s value, type, and so on. You can use the R internal function Rf_PrintValue(s)
to inspect SEXP values. For example, if the SEXP is named s
, then in GDB, type this:
call Rf_PrintValue(s)
This prints the value.