# Using ocamldebug with dune

**URL:** <https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878>\
**Category:** Ecosystem\
**Tags:** dune\
**Created:** [May 19, 2021, 10:12pm UTC](https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878 "2021-05-19T22:12:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jserot](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jserot/32/2294_2.png) [@jserot](https://discuss.ocaml.org/u/jserot)\
**Post date:** [May 19, 2021, 10:12pm UTC](https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878/1 "2021-05-19T22:12:45Z")

</div>

I have a problem trying to use `ocamldebug` on programs compiled with `dune`.  
Suppose I have the following `dune` project:

```auto
<root>/src/lib/foo.ml
              /dune
<root>/example/main.ml
              /dune 

```

where `foo.ml` defines a library and `main.ml`uses it. For ex (silly example)

```auto
(* foo.ml *)
let bar x =
  let y = x*2 in
  y+1

(* main.ml *)
let _ =
  let r1 = Foo.bar 1 in
  let r2 = Foo.bar 2 in
  print_int (r1+r2)

```

These files are, classicaly, compiled with the following `dune` stanzas, resp.:

```auto
(library
  (name foo)
  (modules foo ))

```

and

```auto
(executable
 (name main)
 (modes byte)
 (modules main)
 (libraries foo))

```

To use `ocamldebug` the resulting program (`main.bc`), i `cd` to the corresponding `_build` subdir:

```auto
cd _build/default/example

```

and run `ocamldebug` (either from the CL or Emacs) passing `main.bc` as the program name and no argument (IIUC `dune` uses the `-g` `ocamlc` argument for bytecode pgms).

I would have imagined that setting a breakpoint, say at line 2 of `foo.ml` would not work because the current directory (`_build/default/example`) does not contain the corresponding source file. Surprizingly enough, it does:

```auto
(ocd) break @ Foo 2
Loading program... done.
Breakpoint 1 at 0:10492: file src/lib/foo.ml, line 2, characters 3-25

```

It seems that `dune` has, somehow “under the hood” adjusted the `directory` variable of `ocamldebug`. This is confimed by inspecting this variable:

```auto
(ocd) directory
Reinitialize directory list ? (y or n) n
Directories: . /Users/jserot/.opam/4.10.0/lib/ocaml
  /Users/jserot/.opam/4.10.0/.opam-switch/build/ocaml-base-compiler.4.10.0/stdlib
  /Users/jserot/.opam/4.10.0/lib/ocaml
  /Users/jserot/Dev/ml/dune-debug/_build/default
  /Users/jserot/Dev/ml/dune-debug/_build/default/example
  /Users/jserot/Dev/ml/dune-debug/_build/default/example/.main.eobjs/byte
  /Users/jserot/Dev/ml/dune-debug/_build/default/src/lib
  /Users/jserot/Dev/ml/dune-debug/_build/default/src/lib/.foo.objs/byte

```

So far so good.  
The problem is that setting a breakpoint in `main.ml` does not work:

```auto
(ocd) list "main.ml"
1 let _ =
2 let r1 = Foo.bar 1 in
3 let r2 = Foo.bar 2 in
4 print_int (r1+r2)
Position out of range.
(ocd) break @ Main 2
Can't find any event there.

```

What’s going on here ? The corresponding source file

- is in directory listed in the `ocamldebug` `directory` variable
- has been compiled with `-g`

Am i missing sth ?

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [May 20, 2021, 9:28am UTC](https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878/2 "2021-05-20T09:28:33Z")

</div>

Hi,

I think you are in the same situation as this dune issue: [Byte target can't be debugged with ocamldebug · Issue #4347 · ocaml/dune · GitHub](https://github.com/ocaml/dune/issues/4347)

The solution seems to refer to the main module as `@dune __exe__ Main` and it should be better in dune 3.0.

---

<div class="post-metadata">

**Author:** ![jserot](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jserot/32/2294_2.png) [@jserot](https://discuss.ocaml.org/u/jserot)\
**Post date:** [May 20, 2021, 9:51am UTC](https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878/3 "2021-05-20T09:51:59Z")

</div>

My quick search for similar issues was apparently too quick 😉

Setting the breakpoint with

```auto
 break @ dune __exe__ Main 2

```

works.

What is specially misleading is that this explicit “demangling” is not required for other modules (such as `Foo` here)…

Another inconvenient is that breakpoints cannot be set from Emacs this way ☹

Thanks for the pointer anyway

---

<div class="post-metadata">

**Author:** ![sid](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/sid/32/1476_2.png) [@sid](https://discuss.ocaml.org/u/sid)\
**Post date:** [October 30, 2021, 10:14pm UTC](https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878/4 "2021-10-30T22:14:55Z")

</div>

Try adding `(wrapped_executables false)` stanza to the `dune-project` file. This will allow you to do `break @ Main 2`

---

<div class="post-metadata">

**Author:** ![jserot](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jserot/32/2294_2.png) [@jserot](https://discuss.ocaml.org/u/jserot)\
**Post date:** [December 26, 2021, 10:26am UTC](https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878/5 "2021-12-26T10:26:56Z")

</div>

Thanks for the tip, @sid !  
And sorry for the delay in answering 😕

---

<div class="post-metadata">

**Author:** ![sid](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/sid/32/1476_2.png) [@sid](https://discuss.ocaml.org/u/sid)\
**Post date:** [December 26, 2021, 11:19am UTC](https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878/6 "2021-12-26T11:19:52Z")

</div>

No problems! BTW did it work for you after using my suggestion?

---

<div class="post-metadata">

**Author:** ![jserot](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jserot/32/2294_2.png) [@jserot](https://discuss.ocaml.org/u/jserot)\
**Post date:** [December 26, 2021, 6:24pm UTC](https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878/7 "2021-12-26T18:24:19Z")

</div>

Yes, like a charm 🙂

---

<div class="post-metadata">

**Author:** ![jserot](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jserot/32/2294_2.png) [@jserot](https://discuss.ocaml.org/u/jserot)\
**Post date:** [January 25, 2023, 10:44am UTC](https://discuss.ocaml.org/t/using-ocamldebug-with-dune/7878/8 "2023-01-25T10:44:46Z")

</div>

Well, things seem to be broken again in `dune 3.6` 😕 :

After launching `ocamldebug` :

```auto
...
...
(ocd) break @ Main 2
Loading program... done.
No source file for Main.

```

```auto
(ocd) directory
Reinitialize directory list ? (y or n) n
Directories: . /Users/jserot/.opam/5.0.0/lib/ocaml
  /Users/jserot/.opam/5.0.0/.opam-switch/build/ocaml-base-compiler.5.0.0/stdlib
  /Users/jserot/.opam/5.0.0/lib/ocaml /workspace_root /workspace_root/example
  /workspace_root/example/.main.eobjs/byte /workspace_root/src/lib
  /workspace_root/src/lib/.foo.objs/byte

```

Seems that the `workspace_root` variable has not been resolved correctly …  
I will file an issue on the `dune` GH.

Meanwhile, the workaround seems to be get back to `dune 2.6`
