2

我有一段代码如下:

try
   raise Exit (* body *)
with
| e ->
  Printexc.record_backtrace true;
  printf "Unexpected exception : %s\n" (Printexc.to_string e);
  let x = Printexc.get_backtrace () in
  print_string x;
  Printexc.print_backtrace stdout

中的代码body确实引发了异常,它显示Unexpected exception : Pervasives.Exit,但是它不打印任何回溯。

-g我用and编译文件export OCAMLRUNPARAM=b,有人知道回溯无法打印的原因吗?

4

1 回答 1

1

我在您的代码中没有看到很多函数,因此很可能没有要打印的堆栈跟踪。在此处查看上一个答案:打印堆栈跟踪

今天让我感到震惊的是,一个可能的问题是 OCAMLRUNPARAM 实际上并未在您的流程中设置。让环境变量通过 make(1) 命令向下传递可能很棘手。一个原因是 Makefile 中的每一行都由不同的 shell 执行。

影响回溯的另一件事是内联。如果您的功能很复杂,这可能不会影响您。但是你可以关闭几乎所有的内联-inline 0。这不太可能在无堆栈跟踪和堆栈跟踪之间产生差异。它可能会在较短和较长的堆栈跟踪之间产生差异。

这是一个内联产生影响的实验:

$ cat m.ml
try
    let f () : int = raise Exit
    in let g () = f () + 2
    in let h () = g () + 3
    in let main () = Printf.printf "%d\n" (h () + 4)
    in main ()
with
    e -> Printf.printf "%s" (Printexc.get_backtrace ())
$ ocamlopt -inline 10 -g -o m m.ml
$ OCAMLRUNPARAM=b m
Raised by primitive operation at file "m.ml", line 3, characters 18-22
$ ocamlopt -inline 0 -g -o m m.ml
$ OCAMLRUNPARAM=b m
Raised at file "m.ml", line 2, characters 27-31
Called from file "m.ml", line 3, characters 18-22
Called from file "m.ml", line 4, characters 18-22
Called from file "m.ml", line 5, characters 43-47
Called from file "m.ml", line 6, characters 7-14
于 2013-08-24T16:53:12.440 回答