如何在Emacs中跟踪“failwith”错误?

时间:2012-01-27 17:52:39

标签: debugging emacs ocaml

我在Emacs下编写OCaml。我已经配置了Emacs,以便Meta-x compilemake -k发出带有超链接的警告。但是对于failwith引发的错误,它不能给出超链接,例如:

analyzing (ZONE)...
Fatal error: exception Failure("to do")
Raised at file "pervasives.ml", line 22, characters 22-33
Called from file "list.ml", line 69, characters 12-15
make: *** [all] Error 2

Compilation exited abnormally with code 2 at Fri Jan 27 18:44:10

我的代码中有很多failwith "to do",需要知道哪一个引发了错误,是否有人知道如何让Emacs找到这种错误?

2 个答案:

答案 0 :(得分:3)

请参阅following bug report以获取要添加到.emacs的某些elisp,以便编译主模式知道如何解析OCaml回溯报告。

以下是建议的代码(加上tuareg-mode hooks):

(defun caml-change-error-alist-for-backtraces ()
  "Hook to change the compilation-error-regexp-alist variable, to
   search the ocaml backtraces for error locations"
  (interactive)
  (progn
    (setq compilation-error-regexp-alist-alist
          (append
           '((caml-backtrace
"^ *\\(?:Raised at\\|Called from\\) file \\(\"?\\)\\([^,\" \n\t<>]+\\)\\1,\
 lines? \\([0-9]+\\)-?\\([0-9]+\\)?\\(?:$\\|,\
\\(?: characters? \\([0-9]+\\)-?\\([0-9]+\\)?:?\\)?\\)"
              2 (3 . 4) (5 . 6)))
           compilation-error-regexp-alist-alist))
    (setq compilation-error-regexp-alist
          (append compilation-error-regexp-alist '(caml-backtrace)))))

(add-hook 'caml-mode-hook 'caml-change-error-alist-for-backtraces)
(add-hook 'tuareg-mode-hook 'caml-change-error-alist-for-backtraces)


(defun caml-change-error-alist-for-assert-failure ()
  "Hook to change the compilation-error-regexp-alist variable, to
   search the assert failure messages for error locations"
  (interactive)
  (progn
    (setq compilation-error-regexp-alist-alist
          (append
           '((caml-assert-failure
              "Assert_failure(\"\\([^,\" \n\t<>]+\\)\", \\([0-9]+\\), \\([0-9]+\\))"
              1 2 3))
           compilation-error-regexp-alist-alist))
    (setq compilation-error-regexp-alist
          (append compilation-error-regexp-alist '(caml-assert-failure)))))

(add-hook 'caml-mode-hook 'caml-change-error-alist-for-assert-failure)
(add-hook 'tuareg-mode-hook 'caml-change-error-alist-for-assert-failure)

答案 1 :(得分:2)

有时编译为字节码会为您提供更精确的堆栈跟踪。

  1. 确保使用-g选项进行编译(或使用带有ocamlbuild的debug标记)
  2. 编译为字节码,堆栈跟踪更精确。
  3. 确保$OCAMLRUNPARAM设置了b选项(请参阅here
  4. M-x next-error 允许您遵循堆栈跟踪(如果您使用分发中的caml-mode并且至少使用OCaml 3.11)。