I am trying to use gdb to debug compiled code in an R package in the same way as specified in the video: https://vimeo.com/11937905 and Writing R Extensions section 4.4.
These sources say to use the command
R -d gdb
when starting R. However, whenever I try to do this I get a message saying WARNING: unknown option '-d'
and R is started under normal conditions.
Why isn't R recognizing the -d flag? I am using Windows 10.
As @MatthewLueder has found out himself finally debugging on Windows does not work as on Linux.
A how-to is described in the R for Windows FAQ
The main reason for the missing -d
argument in R
seems to be that Windows cannot send a signal to a process to interrupt the execution and pass the control to the (gdb
) debugger:
Therefore R on Windows offers a work-around using RGui
instead of R
:
gdb /path/to/R-3.x.x/bin/x64/Rgui.exe
(gdb) run
After starting the RGui
you are in an R shell and can load your packages
that contain the DLLs to be debugged.
To set breakpoints for debugging you can interrupt R to break into the debugger via a menu item that is only visible if RGui
was started with gdb
:
Now you can set breakpoints in your code via b a_function_name
, enter c
to continue R, call the function in R and voilà: gdb
shows the breakpoint hit and
you can debug (stepping through the code and printing variable values).
PS: I am currently developing an R package to improve the debugging of C++ code in R packages since it is quite difficult to view the current values of R variables or Rcpp
data types in gdb
: https://github.com/aryoda/R_CppDebugHelper
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With