如何跟踪OCaml程序的错误? [英] How to trace error of OCaml programs?

查看:132
本文介绍了如何跟踪OCaml程序的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用OCaml编写的编译器。有时当执行错误时,它显示错误行,但它不显示上下文,例如,如何调用函数,使用哪个值...

I am writing a compiler written in OCaml. Sometimes when there is an error of execution, it shows the line of error, but it does not show the context, for instance, how the function is called, with which values...

为了帮助调试,有没有人知道一种方法来显示执行的步骤,直到错误与相关变量的真实价值?

In order to help debugging, does anyone know a way to show the steps of execution till the error with real value of the relevant variables?

顺便说一下,我使用Emacs作为编辑器。

By the way, I am using Emacs as editor.

推荐答案

Ocaml已编译。你似乎习惯于解释语言,运行时系统可以访问完整的程序源代码。使用编译的程序,运行时系统不能访问很多信息。例如,变量名在编译时消失,除非正常程序执行需要,否则没有任何东西会跟踪传递给每个函数的参数。这样会产生很多开销。

Ocaml is compiled. You seem to be used to interpreted languages, where the run-time system has access to the full program source code. With a compiled program, the run-time system doesn't have access to much information. For example, variable names disappear at compile time, and nothing will keep track of the arguments passed to every function except as needed for the normal program execution (doing that would incur a lot of overhead).

如果使用调试符号编译程序(将 -g 选项传递给编译器),则可以获得堆栈跟踪未捕获的异常。您将获得函数名称和一些程序位置,但不会获取详细的内存内容。使用调试信息进行编译会产生更大的可执行文件,但不会更改运行时性能。您需要设置 OCAMLRUNPARAM 环境变量

If you compile your program with debugging symbols (pass the -g option to the compiler), you can get a stack trace if your program dies of an uncaught exception. You'll get function names and some program locations, but not detailed memory contents. Compiling with debugging information results in a bigger executable, but doesn't change the run-time performance. You need to set the OCAMLRUNPARAM environment variable to contain b when running the program.

ocamlc -g -o foo foo.ml
export OCAMLRUNPARAM=b
./foo

如果需要更多信息,您需要在调试器中运行程序。

If you want more information, you need to run your program inside a debugger.

这篇关于如何跟踪OCaml程序的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆