0

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

Writing R extensions explains how I can start R with GDB attached for debugging via this shell command:

$ R -d gdb --vanilla

I want to use QtCreator instead of gdb to have debugging GUI, but this doesn't work (just opens a hex dump window and I cannot run R):

$ R -d qtcreator --vanilla

How can I use QtCreator for debugging?

Edit 1:

Attaching the QtCreator debugger to a running R session does not work: If I start R, get the process ID in R via Sys.getpid() and choose Debug > Start Debugging > Attach to running application I get an error:

enter image description here

Edit 2: The ptrace error is caused by kernel hardening that allows only debugging of child processes. I could solve this (temporarily) via

sudo su -
echo 0 > /proc/sys/kernel/yama/ptrace_scope
exit

Still an open issue: The QtCreator debugger shows the assembly code (not the C/C++ code)

R Yoda
  • 8,358
  • 2
  • 50
  • 87
  • 3
    To my knowledge QtCreator is just an IDE for Qt, and not a debugger. I would assume that the `-d gdb` flag is necessary to create debugging information, regardless of the tool that is used to analyze the code. Not sure if `ddd`, a lightweight GUI for the gnu dbg, works in combination with Rcpp. – RHertel Nov 01 '19 at 10:37
  • I have already tried `R -d ddd --vanilla` + `run` in the ddd/gdb console but a strange `DDD: Selection` is popping up which I do not understand – R Yoda Nov 01 '19 at 10:40
  • @RHertel The qtcreator IDE has an integrated debugger as plugin: `qtcreator --help` shows `qtcreator -debug` as option but I don't know to pass this switch to qtcreator (instead of R). This doesnt work: `R -d qtcreator -debug --vanilla` – R Yoda Nov 01 '19 at 10:42
  • There are many such C++ IDEs (CodeBlocks, eclipse CDT...) which seem to contain a debugger, but in the end they are just invoking the standard debugger like gdb, unless specified otherwise. I suspect that this is also the case for QtCreator. https://doc.qt.io/qtcreator/creator-debugger-engines.html . – RHertel Nov 01 '19 at 10:49
  • @RHertel Yes, gdb is quite often the heart of visual debuggers. All I want is a debugging GUI for R to work faster (I always forget the gdb command names) – R Yoda Nov 01 '19 at 10:52
  • 1
    Can you attach to a running process using QtCreator? That‘s what I am using with the gdb front end provided by Emacs. – Ralf Stubner Nov 01 '19 at 12:38
  • @RalfStubner Is this a "one-liner" to start debugging with Emacs (long long time ago I have used this editor ;-) – R Yoda Nov 01 '19 at 12:43
  • 1
    It is not that simple. I might be able to add it asan answer later. – Ralf Stubner Nov 01 '19 at 12:52
  • @RalfStubner THX - I have just found a question for that: https://stackoverflow.com/q/4980595 – R Yoda Nov 01 '19 at 13:24

1 Answers1

2

Here the steps it takes me to do this using the gdb front end provided by Emacs. As an example, I am debugging a package that uses Rcpp:

  1. Open the root directory of the package in Emacs.
  2. Start an R session with ESS via M-x R.
  3. Get the pid of the R session with Sys.getpid().
  4. Start gdb front end with M-x gdb adding -p <pid> to the suggested command line.
  5. In gdb add some break point in the package to be loaded. This will be marked as "pending".
  6. In gdb continue the R process.
  7. In R load the package with devtools::load_all().
  8. In R call a function that will reach the break point.
  9. In gdb you now have the normal interface but with the Source code being displayed in another buffer. Typically it takes one next step for the source buffer to show.

Here a screen shoot showing the *R* and *gdb* buffers together with the source code. Not the break-point indicated by the red dot and the cursor indicating where processing stands:

enter image description here

If you load the package some other way, e.g. with library(..., lib.loc = "..."), you should open the src directory of the package in the first step. That way Emacs can find the corresponding source file. I expect that this can be transferred to QtCreator as well.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • THX a lot for giving insight into "neighbour's garden" :-) When I open a source file manually in QtCreator I can set breakpoints too... – R Yoda Nov 01 '19 at 15:51
  • 1
    The R FAQ contains a small section about [Debugging R from within Emacs](https://cran.r-project.org/doc/FAQ/R-FAQ.html#Debugging-R-from-within-Emacs) – R Yoda Nov 01 '19 at 22:06