-
Notifications
You must be signed in to change notification settings - Fork 21
Debugging C code with Valgrind
The best way of ensuring that an application compiled from C code does not have memory leaks and does not access invalid memory addresses is to run it through Valgrind. In case of errors, Valgrind will show the exact lines of bugs if the applications were compiled with debugging symbols. Hence, before debugging nvimcom
or nvimserver
code, we have to recompile them without optimization and with debugging symbols, and a script will make this task easier (adjust the values of R_INCLUDE_DIR
, R_LIBRARY_DIR
, and RNVIM_PATH
):
make_nvimcom_for_valgrind.sh
:
#!/bin/sh
R_INCLUDE_DIR="/usr/share/R/include"
R_LIBRARY_DIR="$HOME/R/x86_64-pc-linux-gnu-library/4.3"
RNVIM_PATH="$HOME/.local/share/nvim/lazy/R.nvim"
cd "$RNVIM_PATH/nvimcom/src"
gcc -I"$R_INCLUDE_DIR" -DNDEBUG -fpic -g -c nvimcom.c -o nvimcom.o
gcc -I"$R_INCLUDE_DIR" -DNDEBUG -fpic -g -c rd2md.c -o rd2md.o
gcc -I"$R_INCLUDE_DIR" -DNDEBUG -fpic -g -c common.c -o common.o
gcc -shared -o "$R_LIBRARY_DIR/nvimcom/libs/nvimcom.so" nvimcom.o rd2md.o common.o
rm -f nvimcom.o rd2md.o common.o
cd apps
gcc -pthread -std=gnu99 -g -Wall complete.c data_structures.c logging.c obbr.c \
rnvimserver.c tcp.c utilities.c ../common.c \
-o "$R_LIBRARY_DIR/nvimcom/bin/rnvimserver"
Then, to run the rnvimserver
through Valgrind, change the file R.nvim/lua/r/server.lua
and comment out the code that runs the rnvimserver
directly and uncomment the code that runs it through Valgrind:
require("r.job").start( "Server", {
"valgrind",
"--leak-check=full",
"--log-file=/tmp/rnvimserver_valgrind_log",
rns_path, }, rns_opts)
-- require("r.job").start("Server", { rns_path }, rns_opts)
Similarly, to run R (and, consequently, nvimcom
) through Valgrind, put in your R.nvim
config:
R_args = {"-d", "'valgrind --leak-check=full --log-file=/dev/shm/R_valgrind_log'"},
Finally, run R.nvim
as usual and inspect the log files at the end of the session or use tail
to follow the live output of Valgrind:
tail -f /tmp/rnvimserver_valgrind_log