Skip to content

Commit c54b59a

Browse files
authored
adds the --show-knowledge option to bap mc (#1304)
This option will dump the final state of the knowledge base. Mostly useful for debugging and writing lifters.
1 parent 2ef02d6 commit c54b59a

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/bap_primus/bap_primus_lisp_semantics.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ let program =
4646
~empty:Program.empty
4747
~equal:Program.equal
4848
~join:(fun x y -> Ok (Program.merge x y))
49-
~inspect:(fun p ->
50-
let r = Format.asprintf "%a" Program.pp p in
51-
Sexp.Atom r)
5249

5350
type program = {
5451
prog : Program.t;

plugins/mc/mc_main.ml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ type output = [
119119
| `invalid
120120
| `addr
121121
| `memory
122+
| `knowledge
122123
] [@@deriving compare, enumerate]
123124
124125
type target =
@@ -196,22 +197,24 @@ module Spec = struct
196197
| `size -> "size"
197198
| `addr -> "addr"
198199
| `memory -> "memory"
200+
| `knowledge -> "knowledge"
199201
| `invalid -> "invalid" in
200202
201203
let as_flag = function
202204
| `insn | `bil | `bir -> ["pretty"]
203205
| `sema -> ["all-slots"]
204-
| `kinds | `size | `invalid | `memory | `addr -> [enabled] in
206+
| `kinds | `size | `invalid | `memory | `addr | `knowledge -> [enabled] in
205207
206208
let doc = function
207209
| `insn -> "Print the decoded instruction."
208210
| `bil -> "Print the BIL code."
209211
| `bir -> "Print the IR."
210-
| `sema -> "Print the knowledge base."
212+
| `sema -> "Print the full semantics of the instruction."
211213
| `kinds -> "Print semantics tags associated with instruction."
212214
| `size -> "Print the instruction size."
213215
| `addr -> "Print the instruction address"
214216
| `memory -> "Print the instruction memory representation"
217+
| `knowledge -> "Print the knowledge base."
215218
| `invalid -> "Print invalid instructions." in
216219
217220
let name s = "show-" ^ name_of_output s in
@@ -339,6 +342,10 @@ let print_insn_memory formats mem =
339342
List.iter formats ~f:(fun _enabled ->
340343
printf "%a@\n" Memory.pp mem)
341344
345+
let print_knowledge formats =
346+
List.iter formats ~f:(fun _ ->
347+
printf "%a" KB.pp_state (Toplevel.current ()))
348+
342349
let print_insn insn_formats insn =
343350
List.iter insn_formats ~f:(fun fmt ->
344351
Insn.with_printer fmt (fun () ->
@@ -383,7 +390,8 @@ let print arch mem code formats =
383390
print_bil (formats `bil) insn;
384391
print_bir (formats `bir) insn;
385392
print_sema (formats `sema) insn;
386-
print_kinds (formats `kinds) code
393+
print_kinds (formats `kinds) code;
394+
print_knowledge (formats `knowledge)
387395
388396
let bits = function
389397
| Target {name=t} -> Theory.Target.bits t
@@ -428,13 +436,14 @@ let validate_formats formats =
428436
List.map formats ~f:(function
429437
| (`insn|`bil|`bir) as kind,fmts ->
430438
validate_module kind fmts
431-
| (`kinds|`size|`invalid|`addr|`memory),[] -> Ok ()
432-
| (`kinds|`size|`invalid|`addr|`memory),[opt]
439+
| (`kinds|`size|`invalid|`addr|`memory|`knowledge),[] -> Ok ()
440+
| (`kinds|`size|`invalid|`addr|`memory|`knowledge),[opt]
433441
when String.equal enabled opt -> Ok ()
434442
| `kinds,_ -> Error (No_formats_expected "kinds")
435443
| `size,_ -> Error (No_formats_expected "size")
436444
| `addr,_ -> Error (No_formats_expected "addr")
437445
| `memory,_ -> Error (No_formats_expected "memory")
446+
| `knowledge,_ -> Error (No_formats_expected "knowledge")
438447
| `invalid,_ -> Error (No_formats_expected "invalid")
439448
| `sema,_ ->
440449
(* no validation right now, since the knowledge introspection

0 commit comments

Comments
 (0)