Debugging C++ code of an R package with Rcpp using gdb cannot print variable value with R_PV (unknown return type)

送分小仙女□ 提交于 2019-12-11 16:21:50

问题


I have used RStudio on Ubuntu 18.04 to create an hello world R package using Rcpp like described here in Rcpp Package Development:

My ~/.R/Makevars contain only the line

CXXFLAGS=-g -O0 -Wall

and during pkg build I can see that these flags are applied.

How can print the current value of an R vector (C++ classes CharacterVector or NumericVector in gdb after hitting a breakpoint?

(gdb) p R_PV(x) (as explained in Writing R Extensions) shows an error (perhaps because the SEXP is wrapped?):

(gdb) whatis x
type = Rcpp::CharacterVector

My debug session:

R -d gdb --vanilla
(gdb) run
library(RcppTestPkg)
# type Strg + X to break into gdb to set a breakpoint
(gdb) break rcpp_hello_world.cpp:8
(gdb) cont
rcpp_hello_world()
Breakpoint 1, rcpp_hello_world () at rcpp_hello_world.cpp:8
8       NumericVector y   = NumericVector::create( 0.0, 1.0 ) ;
(gdb) n
9       List z            = List::create( x, y ) ;
(gdb) n
11      return z ;
(gdb) info locals
x = {<Rcpp::PreserveStorage<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {
    data = 0x5555562c4360}, <Rcpp::SlotProxyPolicy<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::AttributeProxyPolicy<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::NamesProxyPolicy<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::RObjectMethods<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::VectorBase<16, true, Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<Rcpp::traits::expands_to_logical__impl<16>> = {<No data fields>}, <No data fields>}, cache = {
    p = 0x7fffffffba10}}
y = {<Rcpp::PreserveStorage<Rcpp::Vector<14, Rcpp::PreserveStorage> >> = {
    data = 0x5555562c43d0}, <Rcpp::SlotProxyPolicy<Rcpp::Vector<14, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::AttributeProxyPolicy<Rcpp::Vector<14, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::NamesProxyPolicy<Rcpp::Vector<14, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::RObjectMethods<Rcpp::Vector<14, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::VectorBase<14, true, Rcpp::Vector<14, Rcpp::PreserveStorage> >> = {<Rcpp::traits::expands_to_logical__impl<14>> = {<No data fields>}, <No data fields>}, cache = {
    start = 0x5555562c43f8}}
z = {<Rcpp::PreserveStorage<Rcpp::Vector<19, Rcpp::PreserveStorage> >> = {
    data = 0x5555562c4440}, <Rcpp::SlotProxyPolicy<Rcpp::Vector<19, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::AttributeProxyPolicy<Rcpp::Vector<19, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::NamesProxyPolicy<Rcpp::Vector<19, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::RObjectMethods<Rcpp::Vector<19, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::VectorBase<19, true, Rcpp::Vector<19, Rcpp::PreserveStorage> >> = {<Rcpp::traits::expands_to_logical__impl<19>> = {<No data fields>}, <No data fields>}, cache = {
    p = 0x7fffffffbab0}}
(gdb) p x
$3 = {<Rcpp::PreserveStorage<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {
    data = 0x5555562c4360}, <Rcpp::SlotProxyPolicy<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::AttributeProxyPolicy<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::NamesProxyPolicy<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::RObjectMethods<Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<No data fields>}, <Rcpp::VectorBase<16, true, Rcpp::Vector<16, Rcpp::PreserveStorage> >> = {<Rcpp::traits::expands_to_logical__impl<16>> = {<No data fields>}, <No data fields>}, cache = {
    p = 0x7fffffffba10}}

(gdb) p R_PV(x)
'R_PV' has unknown return type; cast the call to its declared return type

(gdb) p x->data
$5 = (SEXP) 0x5555566d2308
(gdb) p R_PV(x->data)
'R_PV' has unknown return type; cast the call to its declared return type


Edit: Here`s the source code of the function:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
List rcpp_hello_world() {

    CharacterVector x = CharacterVector::create( "foo", "bar" )  ;
    NumericVector y   = NumericVector::create( 0.0, 1.0 ) ;
    List z            = List::create( x, y ) ;

    return z ;
}

回答1:


(gdb) p R_PV(x)

In my R source, R_PV is a function returning void. Try this instead:

(gdb) call R_PV(x)

As Dirk Eddelbuettel noted, you still need to pass the right type to R_PV, so perhaps the correct command is:

(gdb) call R_PV(Rcpp::wrap(&x))


来源:https://stackoverflow.com/questions/58652434/debugging-c-code-of-an-r-package-with-rcpp-using-gdb-cannot-print-variable-val

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!