调试R中加载的Rcpp编译代码的有效方法是什么(在OS X Mavericks上)? [英] What are productive ways to debug Rcpp compiled code loaded in R (on OS X Mavericks)?

查看:134
本文介绍了调试R中加载的Rcpp编译代码的有效方法是什么(在OS X Mavericks上)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特别是在OS X Mavericks上,调试加载到R中的共享对象的最有效,最快的方法是什么?我主要是对调试Rcpp代码进行调试感兴趣。

What is the most productive and quickest way to debug shared objects that are loaded into R, in particular on OS X Mavericks? I'm primarily interested in debugging compiled Rcpp code.

我已阅读了有关调试已编译代码的R外部代码( http://cran.r-project.org/doc/manuals/R-exts.html#Debugging-编译代码),该代码支持使用gdb,但Mavericks并未正式支持gdb。但是,看来lldb是可行的选择吗?我从Dirk对本文的回复中找到了最有用的资源,用于找出如何在R中调试编译的代码(谢谢Dirk!)(在Windows下调试(逐行)Rcpp生成的DLL )。

I have read the R externals on debugging compiled code (http://cran.r-project.org/doc/manuals/R-exts.html#Debugging-compiled-code) which favours using gdb, but gdb isn't officially supported on Mavericks. However, it seems that lldb is a viable alternative? I found the most useful resource for working out how to debug compiled code in R from Dirk's response to this post (Thanks Dirk!) (Debugging (line by line) of Rcpp-generated DLL under Windows).

我可以按照下面明确概述的步骤来成功调试已编译的Rcpp代码,其他Rcpp新手可能会发现这很有用(对长度表示歉意,但是我想清楚比省略和模糊更好)。但是这个开发过程有点乏味且耗时。

I can successfully debug compiled Rcpp code by following the steps which I outline below explicitly step by step, which other Rcpp novices may find this useful (apologies for the length, but I figure it's better to be clear than to omit and be vague). But this development process is a bit tedious and time consuming.

问题:


  1. 与下面的操作相比,有没有更快和/或更容易的方法来调试已编译的Rcpp代码?

  1. is there a faster and/or easier way to debug compiled Rcpp code, compared to what I do below?

我是一个超级粉丝是Rstudio的产品,并且很乐意合并通过该IDE创建的调试共享对象,因此,如果有人知道该怎么做,我想知道吗? Rstudio似乎使用属性,并且似乎下面的第4步需要修改,因为编译后临时目录中似乎没有.cpp文件(在下面的示例中,没有 file5156292c0b48.cpp文件)

I'm a big fan of Rstudio, and would love to incorporate debugging shared objects created from that IDE, so if anyone knows how to do this, I would be interested to know? Rstudio seems to use attributes, and it appears that step 4 below needs modification, because there doesn't seem to be a .cpp file in the temporary directory after compiling (in my example below, there is no "file5156292c0b48.cpp" file)

步骤:

1)(一次性关闭) )转到目录〜/ .R(带。的隐藏目录)。创建一个名为 Makevars的新文件,并在其中添加 CXXFLAGS = -g -O0 -Wall 行。

1) (One off) Go to the directory ~/.R (a hidden directory with the .). Create a new file called "Makevars" and in it add the line CXXFLAGS=-g -O0 -Wall.

2)在终端中,键入 R -d lldb 启动R。lldb现在将启动。

2) In the terminal, type R -d lldb to launch R. lldb will now start.

3)在lldb命令行中键入 run

3) Type run at the lldb command line. This will start R.

4)编译Rcpp代码并找到编译对象的位置。德克对上述帖子的回应显示了一种实现此目的的方法。这是我将在此处使用的示例。在R中运行以下命令:

4) Compile the Rcpp code and find the location of the compiled objects. Dirk's response to the above mentioned post shows one way to do this. Here is an example I'll use here. Run the following commands in R:

library(inline)

fun <- cxxfunction(signature(), plugin="Rcpp", verbose=TRUE, body='
int theAnswer = 1;
int theAnswer2 = 2;
int theAnswer3 = 3;
double theAnswer4 = 4.5;
return wrap(theAnswer4);
')

编译的共享库和其他文件,可以通过在R中运行setwd(tempdir())和list.files()来找到。会有一个.cpp文件,例如 file5156292c0b48.cpp和一个.so文件,例如 file5156292c0b48.so

This creates a compiled shared object and other files which can be found by running setwd(tempdir()) and list.files() in R. There will be a .cpp file, like "file5156292c0b48.cpp" and .so file like "file5156292c0b48.so"

5)通过在以下位置运行 dyn.load( file5156292c0b48.so)将共享库加载到R中R命令行

5) Load the shared object into R by running dyn.load("file5156292c0b48.so") at the R command line

6)现在,我们想调试此.so对象中的C ++代码。点击 ctrl + c 返回lldb。现在,我想在文件5156292c0b48.cpp文件中的特定行设置断点。我通过打开另一个终端并查看file5156292c0b48.cpp中感兴趣的行号来找到正确的行号。假设它是第31行,对应于ins Answer = 1行;在我的示例中然后,我在lldb命令行中输入: breakpoint set -f file5156292c0b48.cpp -l 31 。调试器会打印出已经设置了一个断点以及其他一些信息……

6) Now we want to debug the C++ code in this .so object. Go back to lldb by hitting ctrl + c. Now I want to set a break point at a specific line in the file5156292c0b48.cpp file. I find the correct line number by opening another terminal and looking at the line number of interest in file5156292c0b48.cpp. Say it's line 31, which corresponds to the line int theAnswer = 1; above in my example. I then type at the lldb command line: breakpoint set -f file5156292c0b48.cpp -l 31. The debugger prints back that a break point has been set and some other stuff...

7)通过运行 cont返回R。 lldb中的code>(在我按Enter键之前,R提示不会自动出现在我身上)并调用该函数。在R命令行中运行 fun()。现在,我正在调试共享对象(单击n转到下一行,使用p [对象名称]打印变量等)...。

7) Go back to R by running cont in lldb (the R prompt doesn't automatically appear for me until I hit enter) and call the function. Run fun() at the R command line. Now I am debugging the shared object (hit n to go to next line, p [object name] to print variables etc)....

推荐答案

要调试像这样的简单Rcpp脚本,您可以做的一件事是创建一个嵌入R的 .cpp 应用程序(带有主程序)。直接使用Xcode进行调试,这将为您带来良好的调试体验。

To debug simple Rcpp scripts like this, one thing you can do is create a .cpp application (with a main) that embeds R. This way you can debug it directly with Xcode which will give you a good debugging experience.

开始调试软件包时,它变得更加复杂。

It gets more complicate when you start to want to debug packages.

这篇关于调试R中加载的Rcpp编译代码的有效方法是什么(在OS X Mavericks上)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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