如何在 OCaml 中获取堆栈跟踪?
Objective Caml 语言只会在您要求正确的情况下生成堆栈跟踪 - 字节码和本机代码有什么要求?
The Objective Caml language will only produce stack traces if you ask for them just right - what are the requirements for both bytecode and native code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 -g 编译并设置环境变量 OCAMLRUNPARAM=b
Compile with -g and set environment variable OCAMLRUNPARAM=b
一些 Printexc 函数允许您以编程方式执行此操作。
Some Printexc functions let you do this programmatically.
因为看起来你只能在 unix 上获取异常的跟踪,所以你可以在第二个进程中分叉并抛出异常。 这样主流程就可以继续:
Because it looks like you can only get traces for exceptions on unix you can fork and throw the exception in the second process. This way the main process can continue:
如果您使用 Ocamlbuild 而不是直接调用编译器,则可以使用
debug
标记。 来自 手册:例如,如果您正在使用包
bar
构建文件foo.ml
,那么您的_tags
文件将包含一行:这将插入适当的内容构建字节码/本机文件时的
-g
标志。 但是,您仍然需要使用export OCAMLRUNPARAM=b
设置环境变量,如其他答案中所述。If you are using Ocamlbuild instead of invoking compiler directly, you can use the
debug
tag. From the manual:For example, if you are building a file
foo.ml
with packagebar
then your_tags
file will have a line:This will insert the appropriate
-g
flags while building bytecode/native files. However, you still need to set the environment variable usingexport OCAMLRUNPARAM=b
as mentioned in the other answers.正如其他答案中所述,您需要使用调试信息编译项目,并使用 OCAMLRUNPARAM=b 环境变量运行它。
让 Ocamlbuild 使用调试信息编译整个项目但不编辑
_tags
文件的一种便捷方法是指定一个特殊的调试目标。 来自手册:我使用这种技术在命令行上进行快速编译运行周期。 例如,运行
foo.ml
:As noted in other answers, you need to compile your project with debugging info and run it with the
OCAMLRUNPARAM=b
environment variable.A convenient way to have Ocamlbuild compile an entire project with debugging info but without editing the
_tags
file is to specify a special debug target. From the manual:I use this technique for quick compile-run cycles on the command line. For example, to run
foo.ml
: