堆栈溢出的OCaml堆栈跟踪被截断;例如,下面的程序生成如下所示的堆栈跟踪:
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当错误不是堆栈溢出时,与堆栈跟踪形成对比(将最终的f5 ()更改为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如何防止OCaml截断堆栈跟踪?
发布于 2017-04-14 23:05:29
您可以使用ocamldebug获取堆栈跟踪:
ocamlc stackoverflow.ml -o stackoverflow
ocamldebug stackoverflow然后在ocamldebug中:
(ocd) run
[...]
Uncaught exception: Stack_overflow
(ocd) backstep
(ocd) bt您可能希望将调试标志添加到ocamlc (-g)中,以便在调试器中执行额外的操作。
发布于 2017-04-05 05:02:56
当我复制你的结果时,我得到1024行回溯,这是一个可疑的数字。
实际上,我看到这个库在byterun/caml/ backtrace _prim.h中强制设置了一个硬编码的最大回溯大小为1024:
#define BACKTRACE_BUFFER_SIZE 1024如果您想要更大的回溯,您可能必须构建一个新的标准库。
为了说明它的价值,我在ocamlc上做了一些测试,当溢出发生时,我看到了大约262,000个活动堆栈帧(具有默认的堆栈大小)。
(用正确的OCaml 4.04文件名编辑)
发布于 2017-04-05 16:14:07
您可以限制堆栈,这样它就不会溢出回溯缓冲区,例如(用字节码测试)
$ 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-7https://stackoverflow.com/questions/43221830
复制相似问题