使用gdb使用Rcpp调试R软件包的C ++代码无法使用R_PV(未知返回类型)打印变量值 [英] Debugging C++ code of an R package with Rcpp using gdb cannot print variable value with R_PV (unknown return type)

查看:126
本文介绍了使用gdb使用Rcpp调试R软件包的C ++代码无法使用R_PV(未知返回类型)打印变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Ubuntu 18.04上使用RStudio通过Rcpp创建了一个hello world R包,如

 (gdb)whatis x 
type = Rcpp :: CharacterVector

我的调试会话:

  R -d gdb --vanilla 
(gdb)运行
库(RcppTestPkg )
#键入Strg + X进入gdb以设置断点
(gdb)break rcpp_hello_world.cpp:8
(gdb)cont
rcpp_hello_world()
断点1,rcpp_hello_world.cpp上的rcpp_hello_world():8
8 NumericVector y = NumericVector :: create(0.0,1.0);
(gdb)n
9列表z =列表:: create(x,y);
(gdb)n
11返回z;
(gdb)info locals
x = {< Rcpp :: PreserveStorage< Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {{
data = 0x5555562c4360},< Rcpp :: SlotProxyPolicy< Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: AttributeProxyPolicy< Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: NamesProxyPolicy< Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: RObjectMethods< Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: VectorBase< 16,是,Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {< Rcpp :: traits :: expands_to_logical__impl<> 16; = {<无数据字段>},<无数据字段>},缓存= {
p = 0x7fffffffba10}}
y = {< Rcpp :: PreserveStorage< Rcpp :: Vector< 14,Rcpp :: PreserveStorage> >> = {{
data = 0x5555562c43d0},< Rcpp :: SlotProxyPolicy< Rcpp :: Vector< 14,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: AttributeProxyPolicy< Rcpp :: Vector< 14,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: NamesProxyPolicy< Rcpp :: Vector< 14,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: RObjectMethods< Rcpp :: Vector< 14,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: VectorBase< 14,是,Rcpp :: Vector< 14,Rcpp :: PreserveStorage> >> = {< Rcpp :: traits :: expands_to_logical__impl< 14> = {<无数据字段>},<无数据字段>},缓存= {
start = 0x5555562c43f8}}
z = {< Rcpp :: PreserveStorage< Rcpp :: Vector< 19, Rcpp :: PreserveStorage> >> = {{
data = 0x5555562c4440},< Rcpp :: SlotProxyPolicy< Rcpp :: Vector< 19,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: AttributeProxyPolicy< Rcpp :: Vector< 19,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: NamesProxyPolicy< Rcpp :: Vector< 19,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: RObjectMethods< Rcpp :: Vector< 19,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: VectorBase< 19,是,Rcpp :: Vector< 19,Rcpp :: PreserveStorage> >> = {< Rcpp :: traits :: expands_to_logical__impl< 19> = {<无数据字段>},<无数据字段>},缓存= {
p = 0x7fffffffbab0}}
(gdb)px
$ 3 = {< Rcpp :: PreserveStorage< ; Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {{
data = 0x5555562c4360},< Rcpp :: SlotProxyPolicy< Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: AttributeProxyPolicy< Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: NamesProxyPolicy< Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: RObjectMethods< Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {<没有数据字段>},< Rcpp :: VectorBase< 16,是,Rcpp :: Vector< 16,Rcpp :: PreserveStorage> >> = {< Rcpp :: traits :: expands_to_logical__impl<> 16; = {<无数据字段>},<无数据字段>},缓存= {
p = 0x7fffffffba10}}

(gdb)p R_PV(x)
' R_PV'的返回类型未知;将调用强制转换为其声明的返回类型

(gdb)p x-> data
$ 5 =(SEXP)0x5555566d2308
(gdb)p R_PV(x-> data )
'R_PV'具有未知的返回类型;将调用转换为声明的返回类型


编辑:这是该函数的源代码:

  #include< Rcpp.h> 
使用命名空间Rcpp;

// [[Rcpp :: export]]
清单rcpp_hello_world(){

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

返回z;
}


解决方案


(gdb)p R_PV(x)


在我的 R 源, R_PV 是返回 void 的函数。改为尝试以下操作:

 (gdb)调用R_PV(x)

正如Dirk Eddelbuettel所述,您仍然需要将正确的类型传递给 R_PV ,所以也许正确的命令是:

 (gdb)调用R_PV(Rcpp :: wrap(& x))


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 ;
}

解决方案

(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))

这篇关于使用gdb使用Rcpp调试R软件包的C ++代码无法使用R_PV(未知返回类型)打印变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆