📚 R Programming Tutorial

R Programming

Learn R programming concepts with simple explanations, examples, techniques and practical notes.

← Back to R Programming

Running GDB on R Itself

You may have written some C code to interface to R and found it to be buggy. In order to run GDB on that C function, you must first run R itself through GDB.

Although you can invoke R through GDB from a shell command line, for our purposes here, I suggest using separate windows for R and GDB. Here is the procedure:

  1. Start R in one window, as usual.
  2. 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
  3. In that second window, submit GDB's attach command with the R process number.
  4. 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.

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.