Debug C codes called from R with DDD




What follows are just few quick notes hopefully useful to easily debug C functions called from R using the graphical Data Display Debugger running on top of, say, gdb.



Let's assume you have just written a C function to generate a vector of Gaussian random numbers (truncated or not) using the Ratio-Of-Uniforms (ROU) method. If you know nothing about the ROU method and are bothered by that, you can start with the original paper of Kinderman and Monahan (1977) "Computer Generation of Random Variables Using the Ratio of Uniform Deviates" ACM Transactions on Mathematical Software 3: 257-260, or with Josef Leydold (2000) Automatic Sampling with the Ratio-of-uniforms Method ACM Trans. Math. Softw. 26: 78-98. Books and monographs covering the subject include:



Fine, so you have written your functions test_ROU_gogo2 in a file called normROU.c. You now need to generate your object file with the debugging option turned on:

Then you generate a "library" so that you can call your functions from R. (If all that is brand new for you, it would be worth checking the Writing R Extensions manual.): Now you create a file normROU.R where you will keep your R functions related to this normal random variate generation with the ROU method. In this file you will have one R function rnorm2() calling your C function test_ROU_gogo2 which will look something like: It just works like the "native" R rnorm() function except you can give it some truncation boundaries as well.

You start R, load your library and call normROU2(10) but something goes wrong!... You might then want to be able to run your C function within a debugger like gdb with the DDD graphical interface.

In order to do that you will have to restart R with some debugging options:

-d ddd selects ddd. The ddd window will appear and from the program menu you will first select the Run in Execution Window option. So you should see something like:

DDD 1

Then from the same menu you will click on Run Again and NOT on run. If everything goes fine an X terminal with a more or less usual R command line appears.

DDD 2

After loading your library into R (dyn.load("rngROU.so")) and your R codes (source("normROU.R")), you select Open Source from the File menu, and then on the pop-up window your code of interest: normROU.c as shown here:

DDD 3

You can now set your break points in the usual way before calling test_gogo_ROU2 from R with something like rnormROU2(10). The execution will stop at your break point and you can enjoy DDD...

DDD 4

Have fun!



Back to C Pouzat Home Page Retour à la page titre de C Pouzat