# Assert failure does not print full stack trace

**URL:** <https://discuss.ocaml.org/t/assert-failure-does-not-print-full-stack-trace/10964>\
**Category:** Community\
**Tags:** debugging, dune\
**Created:** [December 15, 2022, 8:10pm UTC](https://discuss.ocaml.org/t/assert-failure-does-not-print-full-stack-trace/10964 "2022-12-15T20:10:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aqjune](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/aqjune/32/4090_2.png) [@aqjune](https://discuss.ocaml.org/u/aqjune)\
**Post date:** [December 15, 2022, 8:10pm UTC](https://discuss.ocaml.org/t/assert-failure-does-not-print-full-stack-trace/10964/1 "2022-12-15T20:10:52Z")

</div>

Hello all,

It seems when assertion failure is hit a portion of stack trace is optimized away.

```auto
ubuntu:~/ocaml-playground$ dune test                            
File "lib/Instr.ml", line 137, characters 2-71: <<true>> threw "Assert_failure lib/Util.ml:97:2".
  Raised at file "src/exn.ml", line 71, characters 4-114                                         
  Called from file "runtime-lib/runtime.ml", line 356, characters 15-52                          
  Called from file "runtime-lib/runtime.ml", line 444, characters 52-83                          
  in TEST_MODULE at file "lib/intro.ml", line 136, characters 0-110                              

```

The printed stack trace misses a few function calls.

Is there a way to fully print it?

Thanks!

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [December 16, 2022, 9:55am UTC](https://discuss.ocaml.org/t/assert-failure-does-not-print-full-stack-trace/10964/2 "2022-12-16T09:55:19Z")

</div>

Missing stack frames can be due to various causes. It’s hard to pin the right one without seeing your code, but here are a few possible reasons:

- catching an exception using `try...with` and then raising that exception again erases the first backtrace;
- if a function ends in a tail call, its stack frame is reused and thus it will not appear in the stack trace;
- C functions in general do not appear in OCaml backtraces.

To see a more complete backtrace, you can use a debugger, insert a breakpoint at the location of your assertion, and when it ~~is reached~~ fails, ask a backtrace from the debugger.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [December 16, 2022, 10:08am UTC](https://discuss.ocaml.org/t/assert-failure-does-not-print-full-stack-trace/10964/3 "2022-12-16T10:08:26Z")

</div>

> [@otini](#):
>
> - catching an exception using `try...with` and then raising that exception again erases the first backtrace;

This is not always so. The compiler tries to detect when an exception is re-raised in an exception handler and it will preserve the stack trace in that case. But the heuristics are not perfect so it won’t work every time. On the other hand, it is possible to always preserve the existing backtrace by declaring

```auto
external reraise : exn -> 'a = "%reraise"

```

and using it to re-throw an exception in an exception handler. See also `Printexc.reraise_with_backtrace`.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [December 16, 2022, 10:17am UTC](https://discuss.ocaml.org/t/assert-failure-does-not-print-full-stack-trace/10964/4 "2022-12-16T10:17:20Z")

</div>

I no longer remembered about these techniques. Thanks!

---

<div class="post-metadata">

**Author:** ![aqjune](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/aqjune/32/4090_2.png) [@aqjune](https://discuss.ocaml.org/u/aqjune)\
**Post date:** [December 19, 2022, 3:47pm UTC](https://discuss.ocaml.org/t/assert-failure-does-not-print-full-stack-trace/10964/5 "2022-12-19T15:47:50Z")

</div>

Thank you all for comments! The reason was an infinitely recursive call. I could catch this by opening `dune utop` and rerunning the problematic unit test, which finally printed the full stack trace. 🙂
