@@ -19,28 +19,56 @@ let state = ref (Step 1)
1919
2020let help_msg =
2121 "
22- h
23- help: print help message
24- b {algorithm name}*
25- break {algorithm name}*: add break points
26- rm {algorithm name}*
27- remove {algorithm name}*: remove break points
28- bp
29- breakpoints: print all break points
30- s {number}?
31- step {number}?: take n steps
32- si {number}?
33- stepinstr {number}?: step n AL instructions
34- c {number}?
35- continue {number}?: continue steps until meet n break points
36- al: print al context stack
37- wasm: print wasm context stack
38- store {field} {index}: print a value in store
39- lookup {variable name}: lookup the variable
40- q
41- quit: quit
22+ s[tep] {number}?
23+ Take n steps
24+ si|stepinstr {number}?
25+ Step over n AL instructions
26+ c[ontinue] {number}?
27+ Continue until the n-th break point
28+ b[reak] {algorithm name}*
29+ Add break points
30+ rm|remove {algorithm name}*
31+ Remove break points
32+ bp|breakpoints
33+ Print all break points
34+ al
35+ Print AL context stack
36+ wasm
37+ Print Wasm context stack
38+ v[ar] {variable name} {field|index}*
39+ Print a value selected from an AL variable
40+ f[rame] {field|index}*
41+ Print a value selected from the current Wasm frame
42+ l[ocal] {index} {field|index}*
43+ Print a value selected from the current Wasm frame's locals
44+ (shorthand for `frame LOCALS`)
45+ m[odule] {field} {index}
46+ Print an index from the current Wasm frame's module
47+ (shorthand for `frame MODULE`)
48+ st[ore] {field|index}*
49+ Print a value from the Wasm store
50+ z|state {field|index}*
51+ Print a value indexed from the current Wasm frame's module
52+ (shorthand for the composition of module and store lookup)
53+ q[uit]
54+ Quit
55+ h[elp]
56+ Print help message
4257 "
4358
59+ let print_path path =
60+ if int_of_string_opt path <> None then
61+ " [" ^ path ^ " ]"
62+ else
63+ " ." ^ path
64+
65+ let print_value access root paths =
66+ print_endline (
67+ root ^ String. concat " " (List. map print_path paths) ^
68+ try " = " ^ string_of_value (access paths)
69+ with _ -> " does not exist"
70+ )
71+
4472let allow_command ctx =
4573 let is_entry name il = name |> lookup_algo |> body_of_algo = il in
4674
@@ -71,77 +99,86 @@ let rec do_debug ctx =
7199 let _ = print_string " \n debugger> " in
72100 read_line ()
73101 |> String. split_on_char ' '
74- |> handle_command ctx;
102+ |> List. filter ((<> ) " " )
103+ |> handle_command ctx
104+
75105and handle_command ctx = function
76106 | ("h" | "help" ) :: _ ->
77107 print_endline help_msg;
78108 do_debug ctx
79- | ("b" | "break" ) :: t -> break_points := ! break_points @ t; do_debug ctx
80- | ("rm" | "remove" ) :: t ->
81- break_points := List. filter (fun e -> not (List. mem e t)) ! break_points;
109+ | ("b" | "break" ) :: t ->
110+ if t = [] then
111+ print_endline (String. concat " " ! break_points)
112+ else
113+ break_points := ! break_points @ t;
82114 do_debug ctx
83- | ("bp" | "breakpoints" ) :: _ ->
115+ | ("bp" | "breakpoints" ) :: [] ->
84116 print_endline (String. concat " " ! break_points);
85117 do_debug ctx
118+ | ("rm" | "remove" ) :: t ->
119+ break_points := List. filter (fun e -> not (List. mem e t)) ! break_points;
120+ do_debug ctx
86121 | ("s" | "step" ) :: t ->
87122 (match t with
88- | n :: _ when Option. is_some (int_of_string_opt n) ->
123+ | [] ->
124+ state := Step 1
125+ | [n] when int_of_string_opt n <> None ->
89126 state := Step (int_of_string n)
90127 | _ ->
91- state := Step 1
128+ handle_command ctx [ " help " ]
92129 )
93130 | ("si" | "stepinstr" ) :: t ->
94131 (match ctx with
95132 | (AlContext. Al (name, _, il, _, _) | AlContext. Enter (name, il, _)) :: _
96- when List. length il > 0 ->
133+ when List. length il > 0 ->
97134 (match t with
98- | n :: _ when Option. is_some (int_of_string_opt n) ->
135+ | [] ->
136+ state := StepInstr (name, 1 )
137+ | [n] when int_of_string_opt n <> None ->
99138 state := StepInstr (name, int_of_string n)
100139 | _ ->
101- state := StepInstr (name, 1 )
140+ handle_command ctx [ " help " ]
102141 )
103142 | _ ->
104143 handle_command ctx (" step" :: t)
105144 )
106145 | ("c" | "continue" ) :: t ->
107146 (match t with
108- | n :: _ when Option. is_some (int_of_string_opt n) ->
147+ | [] ->
148+ state := Continue 1
149+ | [n] when int_of_string_opt n <> None ->
109150 state := Continue (int_of_string n)
110151 | _ ->
111- state := Continue 1
152+ handle_command ctx [ " help " ]
112153 )
113- | "al" :: _ ->
114- ctx
115- |> List. map AlContext. string_of_context
116- |> List. iter print_endline;
154+ | "al" :: [] ->
155+ ctx |> List. map AlContext. string_of_context |> List. iter print_endline;
117156 do_debug ctx
118- | "wasm" :: _ ->
157+ | "wasm" :: [] ->
119158 WasmContext. string_of_context_stack () |> print_endline;
120159 do_debug ctx
121- | "store" :: field :: n :: _ ->
122- (try
123- let idx = int_of_string n in
124- Store. access field
125- |> unwrap_listv
126- |> (! )
127- |> (fun arr -> Array. get arr idx)
128- |> string_of_value
129- |> print_endline;
130- with _ -> ()
131- );
160+ | ("st" | "store" ) :: t ->
161+ print_value Access. access_store " store" t;
132162 do_debug ctx
133- | "lookup" :: s :: _ ->
134- (match ctx with
135- | (Al (_ , _ , _ , env , _ ) | Enter (_ , _ , env )) :: _ ->
136- lookup_env_opt s env
137- |> Option. map string_of_value
138- |> Option. iter print_endline;
139- | _ -> ()
140- );
163+ | ("f" | "frame" ) :: t ->
164+ print_value Access. access_frame " frame" t;
141165 do_debug ctx
142- | ("q" | "quit" ) :: _ -> debug := false
143- | _ -> do_debug ctx
166+ | ("l" | "local" ) :: t ->
167+ print_value Access. access_frame " frame" (" LOCALS" :: t);
168+ do_debug ctx
169+ | ("m" | "module" ) :: t ->
170+ print_value Access. access_frame " frame" (" MODULE" :: t);
171+ do_debug ctx
172+ | ("z" | "state" ) :: t ->
173+ print_value Access. access_state " state" t;
174+ do_debug ctx
175+ | ("v" | "var" ) :: s :: t ->
176+ print_value (Access. access_env ctx s) s t;
177+ do_debug ctx
178+ | ("q" | "quit" ) :: [] ->
179+ debug := false
180+ | _ ->
181+ handle_command ctx [" help" ]
144182
145183let run ctx =
146-
147184 if ! debug && allow_command ctx then do_debug ctx
0 commit comments