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!
Debugging with dune
2.3k views Asked by Quantifier At
2
There are 2 answers
0
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
.
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 useor for the given building profile, e.g., for
release
,In a general case, flags are added using
flags
,ocamlc_flags
, andocamlopt_flags
fields that are accepted byenv
,library
,executable
, andexecutables
stanzas and have the corresponding scopes. If you want your flag to be applied globally you need to add the corresponding flag field toenv
stanza, e.g.,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 withocamldebug
. You can use eithergdb
, which OCaml supports pretty well or use bytecode executables.