How to trace error of OCaml programs?

空扰寡人 提交于 2019-12-12 12:35:08

问题


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?

By the way, I am using Emacs as editor.


回答1:


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).

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.



来源:https://stackoverflow.com/questions/6396738/how-to-trace-error-of-ocaml-programs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!