Remi.b Remi.b - 3 months ago 11
C Question

gdb with with the .C interface (shared object)

I am calling a shared object coded in

C
from a
R
wrapper via the .C. The
C
code cause segmentation faults and I would like to use gdb to help me debug this code.

Is it possible to use
gdb
with the .C interface?

I am on MAC OSX 10.11.3

Answer

Whether you are calling compiled code via .C or .Call should not matter WRT to debugging an R session via gdb. As an example, consider the code below which intentionally causes a segfault (on my system) by dereferencing a null pointer:

#include <R.h>
#include <stdio.h>
#include <string.h>

void rtest(void)
{
    int* p = NULL;
    printf("%d\n", *p);
} 

Compile this with R CMD SHLIB rtest.c, and start R using either of

  • R -d /path/to/gdb
  • R --debugger=/path/to/gdb

Or, if gdb is on your PATH, you don't need the full file path above. At any rate, start the R session:

nathan@nathan-deb:/tmp$ R --debugger=gdb

#GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1
#Copyright (C) 2014 Free Software Foundation, Inc.
#License GPLv3+: GNU GPL version 3 or later 
#
# omitted
#
#Reading symbols from /usr/local/lib/R/bin/exec/R...done.
#
(gdb) r 

Then load the shared library, call the function, and proceed with your debugging session:

dyn.load("rtest.so")
f <- function() .C("rtest")
f()

#Program received signal SIGSEGV, Segmentation fault.
#rtest () at rtest.c:8
#8      printf("%d\n", *p);