5

I am trying to use gdb to debug a simple C++ subroutine that uses Rcpp features. I build my subroutine foo.cpp using the options -O0 -g. I start R using R -d gdb I load the subroutine using dyn.load("foo.so"), and I call the subroutine with .Call("foo",X). There are no problems. When I enter the subroutine in gdb, I can print standard C++ types, e.g, "print i" where i is an int. However, I cannot print the elements of an Rcpp::NumericMatrix xout. I have tried

print xout(1,1)

as well as

print R_PV(xout(1,1))

Does anyone know how to do this?

My C++ code is:

#include <iostream>
#include <Rcpp.h>

RcppExport SEXP foo(SEXP x){
    using namespace Rcpp;
    NumericMatrix xin = NumericMatrix(x);
    int nrow = xin.nrow();
    int ncol = xin.ncol();
    NumericMatrix xout(nrow, ncol);
    for(int j = 0; j<ncol; j++) {
      xout(0,j) = xin(0,j);
      for (int i=1; i<nrow; i++) {
          if (ISNA(xin(i,j)))
           xout(i,j) = xout(i-1,j);
          else
           xout(i,j) = xin(i,j);
      }
    }

    return(xout);
}

From the command line, I run:

R CMD SHLIB foo.cpp

Then I start R using

R -d gdb

I add a breakpoint at foo

b foo

Then I start R:

R

In R, I type

dyn.load("foo.so")
X =matrix(rnorm(9),3)
.Call("foo",X)

From the gdb command line I type

print xin

and get:

$1 = {<Rcpp::Vector<14>> = {<Rcpp::RObject> = {_vptr.RObject = 0x7fff8df4b410, 
      m_sexp = 0x2567f20}, <Rcpp::VectorBase<14, true, Rcpp::Vector<14> >> = {<Rcpp::traits::expands_to_logical__impl<14>> = {<No data fields>}, <No data fields>}, <Rcpp::internal::eval_methods<14>> = {<No data fields>}, cache = {
      start = 0x31e03c39b0}}, <Rcpp::MatrixBase<14, true, Rcpp::Matrix<14> >> = {<Rcpp::traits::expands_to_logical__impl<14>> = {<No data fields>}, <No data fields>}, nrows = -537925942}

I would like to be able to access xin(1,1) from gdb, e.g.,

print xin(1,1)

gtcuser
  • 61
  • 2
  • It's a bit of a wart. We don't have native print member functions. With Armadillo types you can. See [this SO question](http://stackoverflow.com/questions/15163261/debugging-c-programs-with-gdb) for some background. – Dirk Eddelbuettel Jan 21 '15 at 19:53
  • A question with a similar problem is here: https://stackoverflow.com/q/58652434 – R Yoda Nov 02 '19 at 20:44

0 Answers0