Search code examples
debuggingemacsocamltraceback

How to realize error traceback in Emacs?


I am writing a compiler in Ocaml. The tracback works well when I compile and test it with make in a terminal, for instance:

export OCAMLRUNPARAM=b
./Simpler-Basic test.sib
Fatal error: exception Match_failure("interp.ml", 45, 21)
Called from file "interp.ml", line 97, characters 72-86
Called from file "list.ml", line 74, characters 24-34
Called from file "interp.ml", line 108, characters 9-35
Called from file "main.ml", line 54, characters 4-17
make: *** [all] Error 2

But when I compile and test it in my Emacs by Meta-x compile followed by make, it does not show the traceback part in the buffer:

make
export OCAMLRUNPARAM=b
./Simpler-Basic test.sib
Fatal error: exception Match_failure("interp.ml", 45, 21)
make: *** [all] Error 2

Compilation exited abnormally with code 2 at Sat Jun 18 19:03:04

There is a part in my .emacs to do traceback that I copied from a friend: http://paste.ubuntu.com/628838/

Could anyone tell me how to amend my .emacs so that it shows traceback as in a terminal? Thank you very much


Solution

  • Where did you write export OCAMLRUNPARAM=b?

    If you wrote this in a makefile (↹ stands for a tab):

    ↹export OCAMLRUNPARAM=b
    ↹./Simpler-Basic test.sib
    

    then it doesn't work because each makefile command is executed in a separate shell, so the environment variable assignment vanishes after the first line completes. You can combine the two lines in a single logical line instead:

    ↹export OCAMLRUNPARAM=b; \
    ↹./Simpler-Basic test.sib
    

    If you always want backtraces when running an Ocaml program from within Emacs, set the environment variable in your .emacs:

    (setenv "OCAMLRUNPARAM" "b")
    

    In order for Emacs to recognize the backtrace messages as error messages with a location, you need to register them in compilation-regexp-alist. Put something like this in your .emacs (untested):

    (eval-after-load "caml"
      (add-to-list 'compilation-regexp-alist
                   '("\\(^Raised at\\|Called from\\) file \"\\([^"\n]+\\)\", line \\([0-9]+\\)"
                     2 3)))