Skip to content

Further refine our debugging infrastructure #1650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ _coverage/

# Debugger support
ocamlc
ocamlopt
.ocamldebug
1 change: 0 additions & 1 deletion .ocamldebug

This file was deleted.

13 changes: 9 additions & 4 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,23 @@ something like:

## Using the OCaml debugger to debug the compiler

First, run `make debug`. This completes three steps:
First, run `make debug`. This completes four steps:

1. `make install`
2. Sets up the `ocaml/tools/debug_printers` script so that you can `source
ocaml/tools/debug_printers` during a debugging session to see
otherwise-abstract variable values. This script is automatically loaded by
the debugger due to the `.ocamldebug` file at the root of the compiler repo.
otherwise-abstract variable values.
3. Symlinks `./ocamlc` and `./ocamlopt` to point to the bytecode versions of
those compilers. This is convenient for emacs integration, because emacs
looks for sources starting in the directory containing the executable.
4. Creates a `.ocamldebug` file to automatically load the right search path
and the `debug_printers` set up above.

Then it's time to run the debugger itself. The recommended workflow is to add
the elisp below to your emacs init file, and then use the command
`ocamldebug-ocamlc` to debug `ocamlc` or the command `ocamldebug-ocamlopt` to
debug `ocamlopt`.
debug `ocamlopt`. Running your built `ocamldebug` file on `ocamlc` or `ocamlopt`
should also work, if you wish to work outside emacs.

```
;; directly inspired by the ocamldebug implementation in ocamldebug.el
Expand All @@ -243,6 +245,9 @@ debug `ocamlopt`.
(setq ocamldebug-debuggee-args
(read-from-minibuffer (format "Args for ocamlc: ")
ocamldebug-debuggee-args))
;; In addition to the directories in .ocamldebug, use 'find' to
;; see also list directories with -I; this finds any new cmo directories
;; since the last 'make debug'
(let* ((cmo-top-dir (file-name-concat ocaml-dir "_build/main"))
(find-cmo-cmd (concat "find "
cmo-top-dir
Expand Down
11 changes: 4 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,14 @@ coverage: boot-runtest

.PHONY: debug
.NOTPARALLEL: debug
debug: install ocaml/tools/debug_printers ocamlc ocamlopt
debug: install debug-printers ocamlc ocamlopt .ocamldebug

ocamlc:
ln -s $(prefix)/bin/ocamlc.byte ocamlc

ocamlopt:
ln -s $(prefix)/bin/ocamlopt.byte ocamlopt

ocaml/tools/debug_printers: ocaml/tools/debug_printers.ml ocaml/tools/debug_printers.cmo
echo 'load_printer "ocaml/tools/debug_printers.cmo"' > $@
awk '{ print "install_printer Debug_printers." $$2 }' < $< >> $@

ocaml/tools/debug_printers.cmo: ocaml/tools/debug_printers.ml _build/install/main/bin/ocamlc.byte
_build/install/main/bin/ocamlc.byte -c -I _build/main/ocaml/.ocamlcommon.objs/byte ocaml/tools/debug_printers.ml
.ocamldebug: install
find _build/main -name '*.cmo' -type f -printf 'directory %h\n' | sort -u > .ocamldebug
echo "source ocaml/tools/debug_printers" >> .ocamldebug
12 changes: 12 additions & 0 deletions ocaml/tools/dune
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,16 @@
(libraries ocamlcommon)
(modules debug_printers))

(rule
(target debug_printers)
(deps debug_printers.ml %{cmo:debug_printers})
(action
(with-stdout-to %{target}
(progn
; Resorting to Bash instead of the built-in [echo] action because I
; couldn't find a better way to get an absolute path out of Dune
(bash "echo load_printer \\\"$(realpath %{cmo:debug_printers})\\\"")
(with-stdin-from debug_printers.ml
(run awk "{ print \"install_printer Debug_printers.\" $2 }"))))))

; ocamlcp, ocamloptp and ocamlprof are not currently supported.