debuggingocamldune

How to debug OCaml dune test program?


Let's say I:

dune init proj foobar
cd foobar

Then modify test/test_foobar.ml to be:

let () = print_endline "First"
let () = print_endline "Second"
let () = print_endline "Third"

I can execute the program with dune test.
How can I run the program and step each line individually in a debugger with source available? Either ocamldebug or gdb is probably fine.

I can do gdb --tui --args ./_build/default/test/test_foobar.exe but it does NOT show the source.

I don't see any suitable files to launch with ocamldebug e.g.:

$ ocamldebug ./_build/default/test/test_foobar.ml
    OCaml Debugger version 5.2.0

(ocd) r
Loading program... < shortened >/foobar/./_build/default/test/test_foobar.ml is not a bytecode file.

Working with OCaml 5.2.0 and dune 3.15.3


Solution

  • Add (modes byte exe) to your test/dune file to cause a byte-code version to be built also e.g. _build/default/test/test_foobar.bc which can be run in ocamldebug.

    The test/dune file:

    (test
     (name test_foobar)
     (modes byte exe)
    )
    

    ---

    gdb does run to the end of the program while showing "No Source Available" the whole time.
    If you set a breakpoint and hit it though, the source will appear.

    e.g.

    (gdb) break test/test_foobar.ml:1