Debugging with dune

2.3k views Asked by At

I know of the OCaml debugging tool (first compile with ocamlc -g <file>, then run ocamldebug <output>) and also the function call tracing feature in the toplevel (both covered here). However, I can't seem to find anything about debug builds with dune. Is this possible? Can someone point me in the right direction? Thank you!

2

There are 2 answers

1
ivg On BEST ANSWER

The -g flag is present by default in all build profiles, so the short answer is that you don't need to do anything. As a pro tip, if you want to see what flags are set by default use

dune printenv .

or for the given building profile, e.g., for release,

dune printenv --profile release .

In a general case, flags are added using flags, ocamlc_flags, and ocamlopt_flags fields that are accepted by env, library, executable, and executables stanzas and have the corresponding scopes. If you want your flag to be applied globally you need to add the corresponding flag field to env stanza, e.g.,

(env
 (release
  (ocamlopt_flags (:standard -O3))))

Here :standard expands to the standard set of flags.

It is also worth to know that OCaml native executables (the executables compiled to machine code using ocamlopt) do not work with ocamldebug. You can use either gdb, which OCaml supports pretty well or use bytecode executables.

0
Dav On

You can build your dune project in bytecode and execute it in ocamldebug.

In a directory of your choice, write this dune file:

;; This declares the hello_world executable implemented by hello_world.ml
;; to be build as native (.exe) or bytecode (.bc) version.
(executable
 (name hello_world)
 (modes byte exe))

This hello_world.ml file:

print_endline "Hello, world!"

And build it with:

dune build hello_world.bc # (*.bc for bytecode, *.exe for native)

The executable will be built as _build/default/hello_world.bc. The executable can then be run in ocamldebug.

dune build hello_world.bc
ocamldebug _build/default/hello_world.bc

As an important note, ocamldebug version 5.0.0 and below does not support spawning multiple domains for parallel programming.

And for an execution, the executable can be built and run in a single step with dune exec ./hello_world.bc.