如何在 OCaml 中获取堆栈跟踪?

发布于 2024-07-07 09:24:08 字数 59 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傲性难收 2024-07-14 09:24:08

使用 -g 编译并设置环境变量 OCAMLRUNPARAM=b

Compile with -g and set environment variable OCAMLRUNPARAM=b

给我一枪 2024-07-14 09:24:08

一些 Printexc 函数允许您以编程方式执行此操作。

Some Printexc functions let you do this programmatically.

落叶缤纷 2024-07-14 09:24:08

因为看起来你只能在 unix 上获取异常的跟踪,所以你可以在第二个进程中分叉并抛出异常。 这样主流程就可以继续:

export OCAMLRUNPARAM=b
# compile with -g

flush_all(); let r = Unix.fork() in if r == 0 then raise Exit

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:

export OCAMLRUNPARAM=b
# compile with -g

flush_all(); let r = Unix.fork() in if r == 0 then raise Exit
心凉怎暖 2024-07-14 09:24:08

如果您使用 Ocamlbuild 而不是直接调用编译器,则可以使用 debug 标记。 来自 手册

使用 OCamlbuild,您只需将调试标记添加到程序的目标中,它就会决定何时插入 -g 标志。

例如,如果您正在使用包 bar 构建文件 foo.ml,那么您的 _tags 文件将包含一行:

<foo.ml>: package(bar), debug

这将插入适当的内容构建字节码/本机文件时的 -g 标志。 但是,您仍然需要使用 export OCAMLRUNPARAM=b 设置环境变量,如其他答案中所述。

If you are using Ocamlbuild instead of invoking compiler directly, you can use the debug tag. From the manual:

With OCamlbuild, you can simply add the debug tag to your program’s targets, and it will sort out when to insert the -g flag or not.

For example, if you are building a file foo.ml with package bar then your _tags file will have a line:

<foo.ml>: package(bar), debug

This will insert the appropriate -g flags while building bytecode/native files. However, you still need to set the environment variable using export OCAMLRUNPARAM=b as mentioned in the other answers.

情痴 2024-07-14 09:24:08

正如其他答案中所述,您需要使用调试信息编译项目,并使用 OCAMLRUNPARAM=b 环境变量运行它。

让 Ocamlbuild 使用调试信息编译整个项目但不编辑 _tags 文件的一种便捷方法是指定一个特殊的调试目标。 来自手册

编译适合使用 ocamldebug 进行调试的代码或使用 ocamlprof 分析本机代码的首选方法是使用适当的目标扩展,.d.byte< /code> 用于调试或 .p.native


我使用这种技术在命令行上进行快速编译运行周期。 例如,运行 foo.ml

export OCAMLRUNPARAM=b

ocamlbuild -no-links foo.d.byte && _build/foo.d.byte

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:

The preferred way of compiling code suitable for debugging with ocamldebug or profiling native code with ocamlprof is to use the appropriate target extensions, .d.byte for debugging or .p.native.

I use this technique for quick compile-run cycles on the command line. For example, to run foo.ml:

export OCAMLRUNPARAM=b

ocamlbuild -no-links foo.d.byte && _build/foo.d.byte
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文