How to trace “failwith” error in Emacs?

孤街浪徒 提交于 2019-12-11 03:01:44

问题


I am writing OCaml under Emacs. I have already configured Emacs so that Meta-x compile and make -k gives warnings with hyperlink. But for errors raised by failwith, it can not give a hyperlink, for instance:

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

I have many failwith "to do" in my code, and need to know which one raises the error, does anyone know how to let Emacs locate this kind of error?


回答1:


See the following bug report for some elisp to add to your .emacs, so that the compilation major mode knows how to parse OCaml backtrace reports.

Here is the suggested code (plus 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)



回答2:


Sometimes compiling to bytecode gives you more precise stack traces.

  1. Make sure you compile with the -g option (or use the debug tag with ocamlbuild)
  2. Compile to bytecode, the stack traces are more precise.
  3. Make sure $OCAMLRUNPARAM has the b option set (see here)

M-x next-error will allow you to follow the stack trace (if you use caml-mode from the distribution and at least OCaml 3.11).



来源:https://stackoverflow.com/questions/9037763/how-to-trace-failwith-error-in-emacs

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