Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I map OCaml bytecode to its original source code location?

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.

like image 343
Tikhon Jelvis Avatar asked Dec 26 '22 01:12

Tikhon Jelvis


2 Answers

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.

like image 115
gasche Avatar answered May 12 '23 11:05

gasche


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

like image 36
zakki Avatar answered May 12 '23 10:05

zakki