SoftTimur
SoftTimur

Reputation: 5540

How to trace "failwith" error in Emacs?

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?

Upvotes: 2

Views: 441

Answers (2)

gasche
gasche

Reputation: 31469

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)

Upvotes: 3

Daniel B&#252;nzli
Daniel B&#252;nzli

Reputation: 5167

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

Upvotes: 2

Related Questions