NIF debugging with GDB/Valgrind

Hello there!

I’m currently trying to find a memory leak in a NIF I have written in C.

I have found some resources as to how to run erlang NIFs with valgrind. I already compiled the Erlang VM and I can now run cerl with -valgrind and -gdb.

Are there any resources on Elixir specific NIF debugging that I’m missing? Is there a setup that has worked for you? Any guide, manual, book, video, etc. would be very helpful.


1 Like

This topic is above my pay grade, but I just finished watching this talk and he mentions a library that might be useful to you.
I don’t know the name of the library though. Maybe it can help you.

Use a debug emulator. Besides that it is just normal C debugging tactics.


I do have a debug emulator, and tested that valgrind and gdb are working. But I’m not sure how I can run something like iex -S mix with the debug emulator.

Is there a way I can run $ERL_TOP/bin/cerl -rvalgrind and load my mix project? Once I have the ebin folder with the BEAM files is it possible to do that?

I don’t know how iex works, so you will have to find someone that does.

I would try to look and see what arguments iex passes to erl when it starts and then pass those to cerl instead.

1 Like

Last I tried, I had to modify elixir script.

  • update erl path here to use your compiled cerl
  • pass -valgrind flag here
set -- "$ERTS_BIN$ERL_EXEC" -valgrind -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL "$@"