5

I am debugging a C++ shared library (DLL) of an R library that uses the Rcpp framework.

I am on Windows and use gdb for debugging (the version installed via the Rtools).

I start debugging via the recommended command:

gdb Rgui.exe --silent

My problem: It seems that RGui captures all output created by Rcpp and R "print" functions in gdb to stdout and shows it in the RGui R console instead of the gdb console (terminal):

(gdb) call dbg_print(df)

enter image description here

How can I "redirect" the output to the gdb console?

PS 1: I could not find a setting for this in the RGui preferences

PS 2: Here is the code of the dbg_print function:

void dbg_print(DataFrame df) {
  Rcpp::print(df);
}

// Calls R's print function:
// inline void print(SEXP s) {
//    Rf_PrintValue(s);           // defined in Rinternals.h
// }

See also the source code for Rf_PrintValue in core R:

Edit 1: Rgui.exe does not offer an obvious switch for output redirection:

enter image description here

Edit 2: I am quite sure that R prints to stdout

All printing in R is done via the functions Rprintf and REprintf [...]

Rprintf writes to standard output. It is redirected by the sink() function, and is suitable for ordinary output.

REprintf writes to standard error and is useful for error messages and warnings. It is not redirected by sink().

Edit 3: @duckmayr has pointed me to an R callback function (in C) called R_WriteConsole that could be used to register my own printing routine to redirect R output to the gdb console (stdout). I am looking for more example code like this...

R Yoda
  • 8,358
  • 2
  • 50
  • 87
  • Although this will not solve your problem directly, sections 1.3.1, 1.6, 6.5, and 8.1.2 of [Writing R Extensions](https://cran.r-project.org/doc/manuals/r-release/R-exts.html) may provide some helpful information – duckmayr Dec 09 '19 at 16:04
  • 1
    @duckmayr Interesting! I see two possible way: `sinks` (*[...] Rprintf [...] is used in exactly the same way as printf, but is guaranteed to write to R’s output (which might be a GUI console rather than a file, and can be re-directed by sink)*) and [registering a callback](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Setting-R-callbacks) to capture the written output (eg. `R_WriteConsoleEx`) and redirect it to `gdb` (but ensuring safe unregistering to avoid dangling pointers could be difficult) – R Yoda Dec 09 '19 at 18:16
  • @duckmayr OK, on Windows I see no way to change the callback of `R_WriteConsole[Ex]` (I have analyzed the Rgui source code and [Writing R Extensions](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Setting-R-callbacks) says: *For **Unix-alikes** there is a public header file Rinterface.h that makes it possible to change the standard callbacks*). Any more ideas? – R Yoda Dec 10 '19 at 07:12

0 Answers0