# Backtraces in script run by ocaml

**URL:** <https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970>\
**Category:** Learning\
**Tags:** script, debugging, ocaml\
**Created:** [June 14, 2020, 10:53am UTC](https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970 "2020-06-14T10:53:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serpent7776](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/serpent7776/32/2245_2.png) [@Serpent7776](https://discuss.ocaml.org/u/Serpent7776)\
**Post date:** [June 14, 2020, 10:53am UTC](https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970/1 "2020-06-14T10:53:04Z")

</div>

How do I get proper backtraces when running script with ocaml?

For example I have `test.ml`

```
let f () = failwith "test"
let proc () = f ()
let () = proc ()

```

When I run ocaml with `env OCAMLRUNPARAM=b ocaml test.ml` I get

```
Exception: Failure "test".
Raised at file "stdlib.ml", line 29, characters 22-33
Called from unknown location
Called from file "toplevel/toploop.ml", line 212, characters 17-27

```

How do I get proper location in my script?

---

<div class="post-metadata">

**Author:** ![Serpent7776](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/serpent7776/32/2245_2.png) [@Serpent7776](https://discuss.ocaml.org/u/Serpent7776)\
**Post date:** [June 14, 2020, 11:42am UTC](https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970/2 "2020-06-14T11:42:59Z")

</div>

I noticed that I get proper backtrace for my actual script when I `#use script.ml` in toplevel. That doesn’t work for the test script above though. My actual script rises `Exception: Invalid_argument "index out of bounds"`

Here’s the link to my original script [https://godbolt.org/z/nhcPby](https://godbolt.org/z/nhcPby)

I’m using `ocaml-base-compiler.4.10.0` in local switch

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [June 15, 2020, 3:34am UTC](https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970/3 "2020-06-15T03:34:54Z")

</div>

See [https://ocaml.org/learn/tutorials/error\_handling.html#Stacktraces](https://ocaml.org/learn/tutorials/error_handling.html#Stacktraces)

---

<div class="post-metadata">

**Author:** ![Serpent7776](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/serpent7776/32/2245_2.png) [@Serpent7776](https://discuss.ocaml.org/u/Serpent7776)\
**Post date:** [June 15, 2020, 4:44pm UTC](https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970/4 "2020-06-15T16:44:27Z")

</div>

Tried both `OCAMLRUNPARAM=b` and `OCAMLRUNPARAM=b`. These do enable collecting backtraces, but I get `Called from unknown location` instead of proper location in my script.

---

<div class="post-metadata">

**Author:** ![ahem](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ahem/32/1490_2.png) [@ahem](https://discuss.ocaml.org/u/ahem)\
**Post date:** [June 21, 2020, 11:29pm UTC](https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970/5 "2020-06-21T23:29:21Z")

</div>

@Serpent7776 backtraces are only available when debug symbols are available.

[https://caml.inria.fr/pub/docs/manual-ocaml/runtime.html](https://caml.inria.fr/pub/docs/manual-ocaml/runtime.html)

If you compile your `test.ml` with `ocamlc -g test.ml`, and set `OCAMLRUNPARAM=b`, you should get a backtrace.

---

<div class="post-metadata">

**Author:** ![gasche](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gasche/32/4_2.png) [@gasche](https://discuss.ocaml.org/u/gasche)\
**Post date:** [June 22, 2020, 1:22pm UTC](https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970/6 "2020-06-22T13:22:13Z")

</div>

This appears to be a regression in OCaml 4.08. Older OCaml versions (starting from 4.03) would correctly show backtraces for script files run through `ocaml`. @Serpent7776, would you care to open an issue on the OCaml bugtracker, or would you prefer if I did it myself?

---

<div class="post-metadata">

**Author:** ![Serpent7776](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/serpent7776/32/2245_2.png) [@Serpent7776](https://discuss.ocaml.org/u/Serpent7776)\
**Post date:** [June 23, 2020, 10:35am UTC](https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970/7 "2020-06-23T10:35:21Z")

</div>

@ahem`ocamlc` seems to work fine, but I don’t want compilation step  
@gasche Opened a bug here [https://github.com/ocaml/ocaml/issues/9701](https://github.com/ocaml/ocaml/issues/9701)

---

<div class="post-metadata">

**Author:** ![renatoalencar](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/renatoalencar/32/3028_2.png) [@renatoalencar](https://discuss.ocaml.org/u/renatoalencar)\
**Post date:** [December 1, 2021, 1:19pm UTC](https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970/9 "2021-12-01T13:19:47Z")

</div>

The issue is that the bytecode is released before the backtrace is retrieved and printed, as a result no debugging information is availbable.

Documented everything on the issue.

> <https://github.com/ocaml/ocaml/issues/9701#issuecomment-983631220>
>
> I'm trying to get backtraces in simple test code:
> \`\`\`
> let f () = failwith "tes…t"
> let proc () = f ()
> let () = proc ()
> \`\`\`
> Running \`env OCAMLRUNPARAM=b ocaml test.ml\`:
> \`\`\`
> ocaml --version
> The OCaml toplevel, version 4.10.0
> \`\`\`
> \`\`\`
> Exception: Failure "test".
> Raised at file "stdlib.ml", line 29, characters 22-33
> Called from unknown location
> Called from file "toplevel/toploop.ml", line 212, characters 17-27
> \`\`\`
> Notice the "unknown location"
> 
> But for older version location in source file was provided
> \`\`\`
> ocaml --version
> The OCaml toplevel, version 4.05.0
> \`\`\`
> \`\`\`
> Exception: Failure "test".
> Raised at file "pervasives.ml", line 32, characters 22-33
> Called from file "./test.ml", line 3, characters 9-16
> Called from file "toplevel/toploop.ml", line 180, characters 17-56
> \`\`\`
> 
> discuss thread for reference: https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970
