4

Is there some nice feature of the format or library for going from some part of the bytecode to the line of code it originally came from? This would obviously be useful for debugging and error messages.

In particular, I'm looking at how hard it would be to add support for source maps to js_of_ocaml.

Thomash
  • 6,339
  • 1
  • 30
  • 50
Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214

2 Answers2

7

When compiled with debug information enabled (option -g), the bytecode carries so-called "event" structures marking for example the function entry and return point, which provide source location and typing information.

As a proof of concept of how to inspect this information, I have created a small branch of the ocamlpp tool (a small utility by Benoît Vaugon to inspect bytecode files) that prints this debug information alongside the bytecode instructions.

I have no idea whether js_of_ocaml takes the necessary steps to preserve this location information throughout the compilation process. You should probably contact the maintainer, Jérôme Vouillon, to ask for more information.

gasche
  • 31,259
  • 3
  • 78
  • 100
  • Regardind `js_of_ocaml` is what you mention related to backtraces ? Because I tried to implement `__FILE__` `__LINE__` via backtraces but it didn't work in `js_of_ocaml` and had to resort to [this](https://github.com/dbuenzli/vg/blob/master/devel/db-locs) very ugly script instead. – Daniel Bünzli Jun 16 '13 at 10:31
  • Yes, I believe event structures are also what is used to report location information out of exception backtraces. But I think that their presence is orthogonal to whether the `js_of_ocaml` runtime properly implements backtraces. In any case, I understood the question in the context of modifying the `js_of_ocaml` compiler, rather than using exactly what's available today. For `__FILE__` and `__LINE__`, I suppose the most convenient solution today is to use Camlp4 (or `-ppx`). – gasche Jun 16 '13 at 14:11
3

js_of_ocaml -debuginfo uses debug_event in a bytecode to write the line of code in comment.

zakki
  • 2,283
  • 14
  • 20
  • Awesome. One important note: I had to use the `-g` flag with `ocamlc` to get the debug output in the bytecode before running it through `js_of_ocaml`. It took me a while to figure that out, and without it you don't get any comments in the generated js. – Tikhon Jelvis Jun 18 '13 at 19:09