How can I get a complete, unrelated stack trace in OCaml after?

OCaml stack traces for truncated; for example, the following program creates a stack trace, shown below:

let rec f0 () = 1 + f1 ()
    and f1 () = 1 + f2 ()
    and f2 () = 1 + f3 ()
    and f3 () = 1 + f4 ()
    and f4 () = 1 + f5 ()
    and f5 () = 1 + f5 ()

let _ =
  Printexc.record_backtrace true;
  f0 ()

Fatal error: exception Stack overflow
Raised by primitive operation at file "stackoverflow.ml", line 6, characters 20-25
Called from file "stackoverflow.ml", line 6, characters 20-25
Called from file "stackoverflow.ml", line 6, characters 20-25

Contrast with stack trace when the error is not stack overflow (change final f5 ()to failwith "Oops":

Fatal error: exception Failure("Oops")
Raised at file "pervasives.ml", line 30, characters 22-33
Called from file "stackoverflow.ml", line 6, characters 20-35
Called from file "stackoverflow.ml", line 5, characters 20-25
Called from file "stackoverflow.ml", line 4, characters 20-25
Called from file "stackoverflow.ml", line 3, characters 20-25
Called from file "stackoverflow.ml", line 2, characters 20-25
Called from file "stackoverflow.ml", line 1, characters 20-25
Called from file "stackoverflow.ml", line 10, characters 2-7

How can I prevent OCaml stack trimming?

+4
source share
3 answers

You can use ocamldebug to get the stack trace:

ocamlc stackoverflow.ml -o stackoverflow
ocamldebug stackoverflow

then in ocamldebug:

(ocd) run
 [...] 
 Uncaught exception: Stack_overflow
(ocd) backstep
(ocd) bt

You can add a debug flag to ocamlc (-g) to perform additional actions in the debugger.

+2
source

, 1024 , .

, , backtrace 1024 byterun/caml/backtrace_prim.h:

#define BACKTRACE_BUFFER_SIZE 1024

, , .

ocamlc, 262 000 ( ).

( OCaml 4.04.)

+4

You can restrict the stack so that it does not overflow the backtrace buffer, for example. (checked with bytecode)

$ env OCAMLRUNPARAM=b,l=1000 ./test
[...]
Called from file "test.ml", line 6, characters 20-25
Called from file "test.ml", line 6, characters 20-25
Called from file "test.ml", line 5, characters 20-25
Called from file "test.ml", line 4, characters 20-25
Called from file "test.ml", line 3, characters 20-25
Called from file "test.ml", line 2, characters 20-25
Called from file "test.ml", line 1, characters 20-25
Called from file "test.ml", line 10, characters 2-7
+2
source

Source: https://habr.com/ru/post/1674052/


All Articles