Skip to content

Commit 6acac80

Browse files
authored
Add Ctype global state debug printers (#130)
* Add debug printers for some of Ctype's global state This produces output like: (ocd) p Ctype.global_state Ctype.global_state: Ctype.global_state = { current_level = 2; nongen_level = 2; global_level = 2; }
1 parent bc32037 commit 6acac80

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

HACKING.jst.adoc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,14 @@ where the test file or test dir are specified with respect to the
5656

5757
## Debugging
5858

59-
OCaml 4.14 makes `type_expr` abstract, and thus normal debug printing
60-
of types no longer works. However, there is now an installable printer
61-
for types, which we can use to see the types. Here are the instructions:
59+
We make several custom printers available so that we can print more values in
60+
`ocamldebug`. Notable examples:
61+
62+
* OCaml 4.14 makes `type_expr` abstract, and thus normal debug printing
63+
of types no longer works without a custom printer.
64+
* The debug printer for `Ctypes.global_state` lets you see the global mutable state maintained within the `Ctypes` module.
65+
66+
Here's how to install the custom printers for a run of `ocamldebug`:
6267

6368
1. Use the old `Makefile`, not the new `Makefile.jst`. This is an infelicity
6469
we hope to fix.
@@ -69,4 +74,4 @@ we hope to fix.
6974
the debugger to load the compiler code, required for the next
7075
step.
7176

72-
4. Execute `source tools/debug_printers` to install the printers.
77+
4. From your debugging session, run `source tools/debug_printers` to install the printers.

tools/debug_printers

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ install_printer Debug_printers.type_expr
33
install_printer Debug_printers.row_field
44
install_printer Debug_printers.ident
55
install_printer Debug_printers.path
6+
install_printer Debug_printers.ctype_global_state

tools/debug_printers.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ let type_expr = Printtyp.raw_type_expr
33
let row_field = Printtyp.raw_field
44
let ident = Ident.print_with_scope
55
let path = Path.print
6-
6+
let ctype_global_state = Ctype.print_global_state

typing/ctype.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5697,3 +5697,25 @@ let immediacy env typ =
56975697
else
56985698
Type_immediacy.Always
56995699
| _ -> Type_immediacy.Unknown
5700+
5701+
(* For use with ocamldebug *)
5702+
type global_state =
5703+
{ current_level : int ref;
5704+
nongen_level : int ref;
5705+
global_level : int ref;
5706+
}
5707+
5708+
let global_state : global_state =
5709+
{ current_level;
5710+
nongen_level;
5711+
global_level;
5712+
}
5713+
5714+
let print_global_state fmt global_state =
5715+
let print_field fmt s r = Format.fprintf fmt "%s = %d;@;" s !r in
5716+
let print_fields fmt { current_level; nongen_level; global_level; } =
5717+
print_field fmt "current_level" current_level;
5718+
print_field fmt "nongen_level" nongen_level;
5719+
print_field fmt "global_level" global_level;
5720+
in
5721+
Format.fprintf fmt "@[<1>{@;%a}@]" print_fields global_state

typing/ctype.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,8 @@ val package_subtype :
450450

451451
(* Raises [Incompatible] *)
452452
val mcomp : Env.t -> type_expr -> type_expr -> unit
453+
454+
(* For use with ocamldebug *)
455+
type global_state
456+
val global_state : global_state
457+
val print_global_state : Format.formatter -> global_state -> unit

0 commit comments

Comments
 (0)