3

I want to set a a break-point in the C implementation for model.matrix. I tried Selva's solution in How can I view the source code for a function?:

> debug(C_modelmatrix)
Error in debug(C_modelmatrix) : object 'C_modelmatrix' not found
> debug(modelmatrix)
Error in debug(modelmatrix) : object 'modelmatrix' not found

The function I'm interested can be found here.

SEXP modelmatrix(SEXP call, SEXP op, SEXP args, SEXP rho

I'm building and running from R source code. How do I set a break-point?

Community
  • 1
  • 1
ABCD
  • 7,914
  • 9
  • 54
  • 90

2 Answers2

5

There is still-useful video tutorial by Seth Falcon on how to debug R packages with native code which shows this.

In essence, launch R with R -d gdb to invoke the gdb debugger which you then instruct to set breakpoints in the right places.

If you (or your operating system) prefers a different compiler, you obviously need to substitute it in the invocation: R -d lldb.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
2

I think Dirk's answer is perfect. Note that gdb is not supported in OS-X, we'd have to use lldb.

> /bin/R -d lldb
> b modelmatrix
> r

Now, run any one-factor ANOVA experiment to trigger the breakpoint.

ABCD
  • 7,914
  • 9
  • 54
  • 90