Next: LATEXResources
Up: STAT:7400 (22S:248) Computer Intensive
Previous: Windows Notes
One approach to debugging is to include print statements at various
places to make sure your code has reached those points and to show
current values of variables. Often this is sufficient, but sometimes
more sophisticated tools are useful.
- Debugging C programs.
- You will need to compile and link with debugging enables; on
Linux this is done using the -g flag.
- The basic debugger on Linux is
gdb.
- A graphical front-ends available on the Linux systems is
ddd.
- A debugger allows you to
- set break points
- single step though code
- examine current variable values
- and lots more
- A very sophisticated tool that may be useful is
valgrind.
valgrind is available on the Linux workstations.
- Debugging R programs
- R includes several functions useful for debugging:
- traceback() will print the call stack for the most
recent error. This can help you narrow down where the error was
raised.
- debug(fun) and undebug(fun) turn debugging
of fun on and off. When debugging is on, each call to
fun enters the debugger.
- trace(fun) and untrace(fun) turn tracing
of fun on and off. When a function is traced, some
information is printed on each call. An experimental more
advanced form of trace can do more things.
- browser() calls can be placed in a function to
enter the debugger at selected points.
- fix(fun) provides a quick way to edit a function.
Using a separate text editor may be more convenient.
- The expression
options(error=recover)
will cause R to enter the debugger when an error is signaled.
- The debug package available from CRAN may also be useful.
- Debugging dynamically loaded C code in R.
- On Linux you can start R with your favorite debugger using the
-d flag:
R -d gdb
will use gdb, for example.
- After loading your code you can enter the C debugger by
sending an interrupt signal with Control-C.
- Once in the debugger you can set break points and continue to
return to R.
Sometimes it is necessary to signal or trap errors in R.
- The stop function is used to signal errors in R code.
- C code signals errors using the error function.
- Default handling of errors is controlled by the error
option; details are given in the help for options.
- Errors within an expression can be trapped with try.
- A more sophisticated error handling mechanism is provided by
tryCatch and signalCondition.
Once code is running you may want to see if you can make it run
faster. Profiling can help.
- C programs can be profiled using the approach described on the
gprof man page.
- valgrind can also be used for profiling.
- R programs can be profiled using the R function Rprof.
Next: LATEXResources
Up: STAT:7400 (22S:248) Computer Intensive
Previous: Windows Notes
Luke Tierney
2016-01-22