Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug an R package (with C and C++ code) in Eclipse?

In Visual Studio we can use "attach to process" in order to debug the DLLs.

Is there any way to debug C/C++ code of an R package in Eclipse/StatET (i.e. something like this example of debugging external code linked to Scilab)?

like image 905
user1436187 Avatar asked Mar 24 '14 17:03

user1436187


1 Answers

The following steps work for Eclipse-CDT C/C++ (LUNA).

  1. Compile R (3.2) with debug information. Steps for downloading source using SVN can be found in R install guide.

$./configure --enable-R-shlib --with-valgrind-instrumentation=2 --with-system-valgrind-headers --CFLAGS='-g -O0 -fPIC' FFLAGS='-g -O0 -fPIC' CXXFLAGS='-g -O0 -fPIC' FCFLAGS='-g -O0 -fPIC' 
$make
$sudo make install 

This will install R under /usr/local/lib/R.

Note: -g and -O are needed to add debug symbols and to make sure that comiple optimization do not prevent debugging.

  1. Setup proper directory structure for an R-package with C source code. Use a makefile rather than relying on Eclipse-StatET for building the project. Makes the setup more easily portable to Windows.

  2. R CMD install at the command line or using the Makefile will install the package to the user's local R library.

  3. Under Eclipse (Luna) create a "New Debug Configuration" under C/C++ Application.

  4. Under 'Main' tab:

a. select a C/C++ Application:

/usr/local/lib/R/bin/exec/R

b. Project may point to the R-package project.

c. Check "Connect process input & output to a terminal".

  1. Under "Arguments" tab use:

--slave --vanilla --no-save

  1. Under 'Environment' tab add:

a. LD_LIBRARY_PATH: /use/local/lib/R/lib

b. R_HOME /usr/local/lib/R Select "Append environment to native environment"

  1. Under "Debugger" tab

a. Pick GDB debugger gdb

b. If 'GDB command set' is not displayed as an option, click 'Select other...' at the very bottom. Choose 'Legacy Create Process Launcher'. Doing so will now display options for picking GDB command set: Pick Standard with protocol mi.

  1. Under "source" tab

a. Add absolute path to the R directory with R source (optional) ~/Downloads/R

b. Add absolute path to the package src directory and any other dependent libraries.

  1. Click Debug.
like image 80
user151410 Avatar answered Sep 18 '22 13:09

user151410