-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocamldumper.ml
1638 lines (1500 loc) · 56.7 KB
/
ocamldumper.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* Mehdi Dogguy, PPS laboratory, University Paris Diderot *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* Copyright 2010 Mehdi Dogguy *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Dump info on .cmi, .cmo, .cmx, .cma, .cmxa, .cmxs files
and on bytecode executables.
This file is an enhanced version of a combination of the OCaml
dumpobj and objinfo utilities.
*)
open Printf
open Misc
open Cmo_format
open Lambda
open Format
open Opcodes
open Opnames
(* Command line options *)
let the_filename = ref ""
let the_realfile = ref ""
let print_approx_flag = ref true
let print_code_flag = ref true
let print_crc_flag = ref true
let print_debug_flag = ref true
let print_description_flag = ref true
let print_dirs_flag = ref true
let print_dlls_flag = ref true
let print_dll_paths_flag = ref true
let print_full_events_flag = ref false
let print_globals_flag = ref true
let print_imports_flag = ref true
let print_locations_flag = ref true
let print_primitives_flag = ref true
let print_reloc_info_flag = ref true
let print_source_flag = ref true
let print_stack_flag = ref true
(* If true, only show identifiers in the computational environment *)
let comp_env_filter_flag = ref true
(* String from which a regular expression is formed to filter
output to the given module. *)
let module_filter_string = ref ""
let module_filter_re: Str.regexp option ref = ref None
let is_matching_module mdle =
match !module_filter_re with
| None -> true
| Some re -> Str.string_match re mdle 0
(* Extra path to search for source files. *)
let extra_path = ref ([] : string list)
(* Source file we want to examine *)
let desired_source_filename = ref ""
(* List of directories to act as workspace roots. *)
let workspace_roots: string list ref = ref []
(* Tell if included in file filter *)
let keep_file = ref true
let workspace_marker = "/workspace_root"
let workspace_marker_slash = "/workspace_root/"
let workspace_marker_len = String.length workspace_marker
let build_re = Str.regexp {|\(.*\)/_build/.*|}
module Magic_number = Misc.Magic_number
let find_workspace_roots () =
if Str.string_match build_re !the_realfile 0 then begin
let root = Str.matched_group 1 !the_realfile in
workspace_roots := root :: !workspace_roots
end else begin
printf "Workspace root not found";
workspace_roots := ["/workspace_root"]
end
let has_workspace_marker dir_name =
(dir_name = workspace_marker) ||
Compat.starts_with ~prefix: workspace_marker_slash dir_name
(* Because we might have multiple workspace roots, a single
filename might expand into multiple filenames. *)
let expand_workspace_root dir_name =
if not (has_workspace_marker dir_name) then [dir_name]
else begin
if List.length !workspace_roots = 0 then
find_workspace_roots ();
let stem = String.sub dir_name workspace_marker_len
(String.length dir_name - workspace_marker_len) in
List.map (fun root -> root ^ stem) !workspace_roots
end
let add_expanded_dir dir_name =
let expanded = expand_workspace_root dir_name in
List.iter Load_path.add_dir expanded
let check_file fname =
if !desired_source_filename <> "" then begin
(* Only change for non-none filemames *)
if fname <> "_none_" then
keep_file := (fname = !desired_source_filename)
end
let is_desired_loc (loc: Location.t) =
let desired = !desired_source_filename in
if String.equal desired "" then true
else begin
let ls = loc.loc_start in
let filename = ls.pos_fname in
String.equal desired filename
end
let () =
Load_path.reset ()
(* Read signed and unsigned integers *)
let inputu ic =
let b1 = input_byte ic in
let b2 = input_byte ic in
let b3 = input_byte ic in
let b4 = input_byte ic in
(b4 lsl 24) + (b3 lsl 16) + (b2 lsl 8) + b1
let inputs ic =
let b1 = input_byte ic in
let b2 = input_byte ic in
let b3 = input_byte ic in
let b4 = input_byte ic in
let b4' = if b4 >= 128 then b4-256 else b4 in
(b4' lsl 24) + (b3 lsl 16) + (b2 lsl 8) + b1
type global_table_entry =
Empty
| Global of Ident.t
| Constant of Obj.t
let start = ref 0 (* Position of beg. of code *)
let reloc = ref ([] : (reloc_info * int) list) (* Relocation table *)
let globals = ref ([||] : global_table_entry array) (* Global map *)
let primitives = ref ([||] : string array) (* Table of primitives *)
let objfile = ref true (* true if dumping a .cmo *)
(* Events (indexed by PC) *)
let event_table = (Hashtbl.create 253 : (int, Instruct.debug_event) Hashtbl.t)
let relocate_event orig ev =
ev.Instruct.ev_pos <- orig + ev.Instruct.ev_pos;
match ev.ev_repr with
Event_parent repr -> repr := ev.ev_pos
| _ -> ()
let record_events orig evl =
List.iter
(fun ev ->
relocate_event orig ev;
Hashtbl.add event_table ev.ev_pos ev)
evl
(* Print a structured constant *)
let print_float f =
if String.contains f '.'
then printf "%s" f
else printf "%s." f
;;
let rec print_struct_const ppf = function
Const_base(Const_int i) -> fprintf ppf "%d" i
| Const_base(Const_float f) -> print_float f
| Const_base(Const_string (s, _, _)) -> fprintf ppf "%S" s
| Const_immstring s -> fprintf ppf "%S" s
| Const_base(Const_char c) -> fprintf ppf "%C" c
| Const_base(Const_int32 i) -> fprintf ppf "%ldl" i
| Const_base(Const_nativeint i) -> fprintf ppf "%ndn" i
| Const_base(Const_int64 i) -> fprintf ppf "%LdL" i
| Const_block(tag, args) ->
fprintf ppf "<%d>" tag;
begin match args with
[] -> ()
| [a1] ->
fprintf ppf "("; print_struct_const ppf a1; fprintf ppf ")"
| a1::al ->
fprintf ppf "("; print_struct_const ppf a1;
List.iter (fun a -> fprintf ppf ", "; print_struct_const ppf a) al;
fprintf ppf ")"
end
| Const_float_array a ->
fprintf ppf "[|";
List.iter (fun f -> print_float f; fprintf ppf "; ") a;
fprintf ppf "|]"
(* Print an obj *)
let same_custom x y =
Obj.field x 0 = Obj.field (Obj.repr y) 0
let rec print_obj ppf x =
if Obj.is_block x then begin
let tag = Obj.tag x in
if tag = Obj.string_tag then
fprintf ppf "%S" (Obj.magic x : string)
else if tag = Obj.double_tag then
fprintf ppf "%.12g" (Obj.magic x : float)
else if tag = Obj.double_array_tag then begin
let a = (Obj.magic x : floatarray) in
fprintf ppf "[|";
for i = 0 to Array.Floatarray.length a - 1 do
if i > 0 then fprintf ppf ", ";
fprintf ppf "%.12g" (Array.Floatarray.get a i)
done;
fprintf ppf "|]"
end else if tag = Obj.custom_tag && same_custom x 0l then
fprintf ppf "%ldl" (Obj.magic x : int32)
else if tag = Obj.custom_tag && same_custom x 0n then
fprintf ppf "%ndn" (Obj.magic x : nativeint)
else if tag = Obj.custom_tag && same_custom x 0L then
fprintf ppf "%LdL" (Obj.magic x : int64)
else if tag < Obj.no_scan_tag then begin
fprintf ppf "<%d>" (Obj.tag x);
match Obj.size x with
0 -> ()
| 1 ->
fprintf ppf "("; print_obj ppf (Obj.field x 0); fprintf ppf ")"
| n ->
fprintf ppf "("; print_obj ppf (Obj.field x 0);
for i = 1 to n - 1 do
fprintf ppf ", "; print_obj ppf (Obj.field x i)
done;
fprintf ppf ")"
end else
fprintf ppf "<tag %d>" tag
end else
fprintf ppf "%d" (Obj.magic x : int)
(* Current position in input file *)
let currpos ic =
pos_in ic - !start
(* Access in the relocation table *)
let rec rassoc key = function
[] -> raise Not_found
| (a,b) :: l -> if b = key then a else rassoc key l
let find_reloc ic =
rassoc (pos_in ic - !start) !reloc
(* Symbolic printing of global names, etc *)
let print_global ppf item =
match item with
Global id -> pp_print_string ppf (Ident.name id)
| Constant obj -> print_obj ppf obj
| _ -> pp_print_string ppf "???"
let print_getglobal_name ppf ic =
if !objfile then begin
begin try
match find_reloc ic with
Reloc_getglobal id -> pp_print_string ppf (Ident.name id)
| Reloc_literal sc -> print_struct_const ppf sc
| _ -> pp_print_string ppf "<wrong reloc>"
with Not_found ->
pp_print_string ppf "<no reloc>"
end;
ignore (inputu ic);
end
else begin
let n = inputu ic in
if n >= Array.length !globals || n < 0
then pp_print_string ppf "<global table overflow>"
else print_global ppf !globals.(n)
end
let print_setglobal_name ppf ic =
if !objfile then begin
begin try
match find_reloc ic with
Reloc_setglobal id -> pp_print_string ppf (Ident.name id)
| _ -> pp_print_string ppf "<wrong reloc>"
with Not_found ->
pp_print_string ppf "<no reloc>"
end;
ignore (inputu ic);
end
else begin
let n = inputu ic in
if n >= Array.length !globals || n < 0
then pp_print_string ppf "<global table overflow>"
else match !globals.(n) with
Global id -> pp_print_string ppf (Ident.name id)
| _ -> pp_print_string ppf "???"
end
let print_primitive ppf ic =
if !objfile then begin
begin try
match find_reloc ic with
Reloc_primitive s -> pp_print_string ppf s
| _ -> pp_print_string ppf "<wrong reloc>"
with Not_found ->
pp_print_string ppf "<no reloc>"
end;
ignore (inputu ic);
end
else begin
let n = inputu ic in
if n >= Array.length !primitives || n < 0
then pp_print_int ppf n
else pp_print_string ppf !primitives.(n)
end
(* Disassemble one instruction *)
let currpc ic =
currpos ic / 4
type shape =
| Nothing
| Uint
| Sint
| Uint_Uint
| Disp
| Uint_Disp
| Sint_Disp
| Getglobal
| Getglobal_Uint
| Setglobal
| Primitive
| Uint_Primitive
| Switch
| Closurerec
| Pubmet
;;
(* Shape and description of each opcode *)
let op_info = [
opACC0, (Nothing, "accu = sp[0]");
opACC1, (Nothing, "accu = sp[1]");
opACC2, (Nothing, "accu = sp[2]");
opACC3, (Nothing, "accu = sp[3]");
opACC4, (Nothing, "accu = sp[4]");
opACC5, (Nothing, "accu = sp[5]");
opACC6, (Nothing, "accu = sp[6]");
opACC7, (Nothing, "accu = sp[7]");
opACC, (Uint, "n=> accu = sp[n]");
opPUSH, (Nothing, "push accu");
opPUSHACC0, (Nothing, "push accu; accu = sp[0]");
opPUSHACC1, (Nothing, "push accu; accu = sp[1]");
opPUSHACC2, (Nothing, "push accu; accu = sp[2]");
opPUSHACC3, (Nothing, "push accu; accu = sp[3]");
opPUSHACC4, (Nothing, "push accu; accu = sp[4]");
opPUSHACC5, (Nothing, "push accu; accu = sp[5]");
opPUSHACC6, (Nothing, "push accu; accu = sp[6]");
opPUSHACC7, (Nothing, "push accu; accu = sp[7]");
opPUSHACC, (Uint, "n=> push accu; accu = sp[n]");
opPOP, (Uint, "n=> pop items from the stack");
opASSIGN, (Uint, "n=> sp[n] = accu; accu = ()");
opENVACC1, (Nothing, "accu = env(1)");
opENVACC2, (Nothing, "accu = env(2)");
opENVACC3, (Nothing, "accu = env(3)");
opENVACC4, (Nothing, "accu = env(4)");
opENVACC, (Uint, "n=> accu = env(n)");
opPUSHENVACC1, (Nothing, "push accu; accu = env(1)");
opPUSHENVACC2, (Nothing, "push accu; accu = env(2)");
opPUSHENVACC3, (Nothing, "push accu; accu = env(3)");
opPUSHENVACC4, (Nothing, "push accu; accu = env(4)");
opPUSHENVACC, (Uint, "push accu; accu = env(*pc++)");
opPUSH_RETADDR, (Disp, "n=> push pc+n; push env; push extra_args");
opAPPLY, (Uint, "n=> extra_args = n - 1; pc = Code_val(accu); env = accu;");
opAPPLY1, (Nothing, "arg1 = pop; push extra_args, env, pc, arg1; pc = Code_val(accu); env = accu; extra_args = 0");
opAPPLY2, (Nothing, "arg1,arg2 = pops; push extra_args, env, pc, arg2, arg1; pc = Code_val(accu); env = accu; extra_args = 1");
opAPPLY3, (Nothing, "arg1,arg2,arg3 = pops; push extra_args, env, pc, arg3, arg2, arg1; pc = Code_val(accu); env = accu; extra_args = 2");
opAPPTERM, (Uint_Uint, "n,s=> sp[0..s-1]=sp[0..n-1];pc = Code_val(accu); env = accu; extra_args += n-1");
opAPPTERM1, (Uint, "n=> arg1=pop;pop n-1 items; push arg1;pc = Code_val(accu); env = accu");
opAPPTERM2, (Uint, "n=> arg1,arg2=pops;pop n-2 items; push arg2,arg1;pc = Code_val(accu); env = accu;extra_args++");
opAPPTERM3, (Uint, "n=> arg1,arg2,arg3=pops;pop n-3 items; push arg3,arg2,arg1;pc = Code_val(accu); env = accu;extra_args+=2");
opRETURN, (Uint, "n=> pop n elements; if(extra_args> 0){extra_args--;pc=Code_val(accu);env = accu}else{pc,env,extra_args=pops}");
opRESTART, (Nothing, "n=size(env);push env(n-1..3);env=env(2);extra_args+=n-3");
opGRAB, (Uint, "n=> if(extra_args>=n){extra_args-=n}else{m=1+extra_args;accu=closure with m args, field2=env,code=pc-3, args popped}");
opCLOSURE, (Uint_Disp, "n,ofs=> if(n>0){push accu};accu=closure of n+1 elements;Code_val(acc)=pc+ofs;arity=0;env offset=2");
opCLOSUREREC, (Closurerec, "nvar,[f...]=> accu=closure for f0 and Infix closures for f1..., all sharing the nvars from accu and stack;push closures");
opOFFSETCLOSUREM3, (Nothing, "accu=&env[-3]");
opOFFSETCLOSURE0, (Nothing, "accu=env");
opOFFSETCLOSURE3, (Nothing, "accu=*env[3]");
opOFFSETCLOSURE, (Sint, "n=> accu=&env[n]"); (* was Uint *)
opPUSHOFFSETCLOSUREM3, (Nothing, "push accu;accu=&env[-3]");
opPUSHOFFSETCLOSURE0, (Nothing, "push accu;accu=env");
opPUSHOFFSETCLOSURE3, (Nothing, "push accu;accu=*env[3]");
opPUSHOFFSETCLOSURE, (Sint, "n=> push accu;accu=&env[n]"); (* was Nothing *)
opGETGLOBAL, (Getglobal, "n=> accu=global[n]");
opPUSHGETGLOBAL, (Getglobal, "n=> push accu; accu=global[n]");
opGETGLOBALFIELD, (Getglobal_Uint, "n,p=> accu=global[n][p]");
opPUSHGETGLOBALFIELD, (Getglobal_Uint, "n,p=> push accu;accu=global[n][p]");
opSETGLOBAL, (Setglobal, "n=> global[n]=accu; accu=()");
opATOM0, (Nothing, "accu = Atom(0)");
opATOM, (Uint, "n=> accu = Atom(n)");
opPUSHATOM0, (Nothing, "push accu; accu = Atom(0)");
opPUSHATOM, (Uint, "n=> push accu; accu = Atom(n)");
opMAKEBLOCK, (Uint_Uint, "n,t=> accu=n-element block with tag t, elements from accu and popped stack");
opMAKEBLOCK1, (Uint, "t=> accu=1-element block, tag t, element from accu");
opMAKEBLOCK2, (Uint, "t=> accu=2-element block, tag t, elements from accu and pop");
opMAKEBLOCK3, (Uint, "t=> accu=3-element block, tag t, elements from accu and popped stack");
opMAKEFLOATBLOCK, (Uint, "n=> n-element float block, elements from accu and popped stack");
opGETFIELD0, (Nothing, "accu = accu[0]");
opGETFIELD1, (Nothing, "accu = accu[1]");
opGETFIELD2, (Nothing, "accu = accu[2]");
opGETFIELD3, (Nothing, "accu = accu[3]");
opGETFIELD, (Uint, "n=> accu = accu[n]");
opGETFLOATFIELD, (Uint, "n=> accu=Double Block with DoubleField(accu, n)");
opSETFIELD0, (Nothing, "accu[0] = pop; accu=()");
opSETFIELD1, (Nothing, "accu[1] = pop; accu=()");
opSETFIELD2, (Nothing, "accu[2] = pop; accu=()");
opSETFIELD3, (Nothing, "accu[3] = pop; accu=()");
opSETFIELD, (Uint, "n=> accu[n] = pop; accu=()");
opSETFLOATFIELD, (Uint, "DoubleField(accu, n) = pop; accu=()");
opVECTLENGTH, (Nothing, "accu = size of block in accu (/2 if Double array)");
opGETVECTITEM, (Nothing, "n=pop; accu=accu[n]");
opSETVECTITEM, (Nothing, "n=pop;v=pop;accu[n]=v");
opGETSTRINGCHAR, (Nothing, "n=pop; accu = nth unsigned char of accu string");
opGETBYTESCHAR, (Nothing, "n=pop; accu = nth unsigned char of accu string");
opSETBYTESCHAR, (Nothing, "n=pop;v=pop; nth unsigned char of accu set to v");
opBRANCH, (Disp, "ofs=> pc += ofs");
opBRANCHIF, (Disp, "ofs=> if(accu != false){pc += ofs}");
opBRANCHIFNOT, (Disp, "ofs=> if(accu == false){pc += ofs}");
opSWITCH, (Switch, "n=> tab=pc;szt=n>>16;szl=n&0xffff;if(Is_block(accu)){ix=Tag_val(accu);pc += tab[szl+ix]}else{pc += tab[accu]");
opBOOLNOT, (Nothing, "accu = not accu");
opPUSHTRAP, (Disp, "ofs=> push extra_args, env, trap_sp_offset, pc+ofs");
opPOPTRAP, (Nothing, "pop;trap_sp_offset=pop;pop;pop");
opRAISE, (Nothing, "if(no frame){stop}else{sp=trapsp;pop pc,trapsp,env,extra_args}");
opCHECK_SIGNALS, (Nothing, "Handle signals, if any. accu not preserved.");
opC_CALL1, (Primitive, "p=> accu=primitive(p)(accu)");
opC_CALL2, (Primitive, "p=> accu=primitive(p)(accu, pop)");
opC_CALL3, (Primitive, "p=> accu=primitive(p)(accu, pop, pop)");
opC_CALL4, (Primitive, "p=> accu=primitive(p)(accu, pop, pop, pop)");
opC_CALL5, (Primitive, "p=> accu=primitive(p)(accu, pop, pop, pop, pop)");
opC_CALLN, (Uint_Primitive, "n,p=> accu=primitive(p)(accu, (n-1)-pops");
opCONST0, (Nothing, "accu = 0");
opCONST1, (Nothing, "accu = 1");
opCONST2, (Nothing, "accu = 2");
opCONST3, (Nothing, "accu = 3");
opCONSTINT, (Sint, "n=> accu = n");
opPUSHCONST0, (Nothing, "push accu; accu = 0");
opPUSHCONST1, (Nothing, "push accu; accu = 1");
opPUSHCONST2, (Nothing, "push accu; accu = 2");
opPUSHCONST3, (Nothing, "push accu; accu = 3");
opPUSHCONSTINT, (Sint, "n=> push accu; accu = n");
opNEGINT, (Nothing, "accu = -accu");
opADDINT, (Nothing, "accu = accu + pop");
opSUBINT, (Nothing, "accu = accu - pop");
opMULINT, (Nothing, "accu = accu * pop");
opDIVINT, (Nothing, "accu = accu / pop.");
opMODINT, (Nothing, "accu = accu % pop");
opANDINT, (Nothing, "accu = accu & pop");
opORINT, (Nothing, "accu = accu | pop");
opXORINT, (Nothing, "accu = accu ^ pop");
opLSLINT, (Nothing, "accu = accu << pop");
opLSRINT, (Nothing, "accu = (unsigned)accu >> pop");
opASRINT, (Nothing, "accu = (signed)accu >> pop ");
opEQ, (Nothing, "accu = accu == pop");
opNEQ, (Nothing, "accu = accu != pop");
opLTINT, (Nothing, "accu = accu < pop");
opLEINT, (Nothing, "accu = accu <= pop");
opGTINT, (Nothing, "accu = accu > pop");
opGEINT, (Nothing, "accu = accu >= pop");
opOFFSETINT, (Sint, "ofs=> accu += ofs");
opOFFSETREF, (Sint, "ofs=> accu[0] += ofs");
opISINT, (Nothing, "accu = Val_long(accu & 1)");
opGETMETHOD, (Nothing, "x=pop;y=x[0];accu = y[accu]");
opGETDYNMET, (Nothing, "object=sp[0];accu=method matching tag in object's method table");
opGETPUBMET, (Pubmet, "tag,cache=> object=accu;methods=object[0];push object;accu=method matching tag in methods;");
opBEQ, (Sint_Disp, "val,ofs=> val == (signed)accu){pc += ofs}");
opBNEQ, (Sint_Disp, "val,ofs=> if(val != (signed)accu){pc += ofs}");
opBLTINT, (Sint_Disp, "val,ofs=> if(val < (signed)accu){pc += ofs}");
opBLEINT, (Sint_Disp, "val,ofs=> if(val <= (signed)accu){pc += ofs}");
opBGTINT, (Sint_Disp, "val,ofs=> if(val > (signed)accu){pc += ofs}");
opBGEINT, (Sint_Disp, "val,ofs=> if(val >= (signed)accu){pc += ofs}");
opULTINT, (Nothing, "accu < (unsigned)pop");
opUGEINT, (Nothing, "accu >= (unsigned)pop");
opBULTINT, (Uint_Disp, "val,ofs=> if(val < (unsigned)accu){pc += ofs}");
opBUGEINT, (Uint_Disp, "val,ofs=> if(val >= (unsigned)accu){pc += ofs}");
opSTOP, (Nothing, "Stop interpreting, return accu");
opEVENT, (Nothing, "if(--caml_event_count==0){send event message to debugger}");
opBREAK, (Nothing, "Send break message to debugger");
opRERAISE, (Nothing, "Like Raise, but backtrace slightly different");
opRAISE_NOTRACE, (Nothing, "Raise but do not record backtrace.");
];;
(* Dump relocation info *)
let print_reloc (info, pos) =
printf " %d (%d) " pos (pos/4);
match info with
Reloc_literal sc -> print_struct_const std_formatter sc; printf "@."
| Reloc_getglobal id -> printf "require %s@." (Ident.name id)
| Reloc_setglobal id -> printf "provide %s@." (Ident.name id)
| Reloc_primitive s -> printf "prim %s@." s
let kind_string ev =
(match ev.Instruct.ev_kind with
Event_before -> "before"
| Event_after _ -> "after"
| Event_pseudo -> "pseudo")
let info_string ev =
(match ev.Instruct.ev_info with
Event_function -> "/fun"
| Event_return _ -> "/ret"
| Event_other -> "")
let repr_string ev =
(match ev.Instruct.ev_repr with
Event_none -> ""
| Event_parent _ -> "(repr)"
| Event_child repr -> Int.to_string !repr)
let get_env ev =
Envaux.env_from_summary ev.Instruct.ev_typenv
let in_comp_env (ev: Instruct.debug_event) (id: Ident.t) =
if not !comp_env_filter_flag then true else begin
let ce : Instruct.compilation_env = ev.ev_compenv in
let stack : int Ident.tbl = ce.ce_stack in
let heap : int Ident.tbl = ce.ce_heap in
let recs : int Ident.tbl = ce.ce_rec in
try ignore (Ident.find_same id stack); true;
with Not_found ->
try ignore (Ident.find_same id heap); true;
with Not_found ->
try ignore (Ident.find_same id recs); true;
with Not_found -> false
end
let print_summary_id title id =
printf " %s: " title;
Ident.print_with_scope std_formatter id;
printf ";@ "
let print_summary_string title s =
printf " %s: %s@ " title s
let next_summary summary =
match summary with
| Env.Env_empty -> None
| Env_value (s, id, vd) -> Some s
| Env_type (s, id, td) -> Some s
| Env_extension (s, id, ec) -> Some s
| Env_module (s, id, mp, md) -> Some s
| Env_modtype (s, id, md) -> Some s
| Env_class (s, id, cd) -> Some s
| Env_cltype (s, id, ctd) -> Some s
| Env_open (s, p) -> Some s
| Env_functor_arg (s, id) -> Some s
| Env_constraints (s, cstrs) -> Some s
| Env_copy_types s -> Some s
| Env_persistent (s, id) -> Some s
| Env_value_unbound (s, n, r) -> Some s
| Env_module_unbound (s, n, r) -> Some s
let print_value_desc (vd: Types.value_description) =
printf "@[{";
printf "val_type=@ ";
Xprinttyp.raw_type_expr std_formatter vd.val_type;
ignore vd.val_kind;
ignore vd.val_loc;
ignore vd.val_attributes;
ignore vd.val_uid;
printf "}@]"
(*
let ls = ev.ev_loc.loc_start in
let le = ev.ev_loc.loc_end in
printf "ev_loc={File \"%s\", line %d, characters %d-%d},@ " ls.Lexing.pos_fname
ls.Lexing.pos_lnum (ls.Lexing.pos_cnum - ls.Lexing.pos_bol)
(le.Lexing.pos_cnum - ls.Lexing.pos_bol);
*)
let myprint_loc (loc: Location.t) =
if !print_locations_flag then begin
let ls = loc.loc_start in
let filename = ls.pos_fname in
if filename <> "_none_" then begin
Location.print_loc std_formatter loc;
end
end
let print_summary_item ev summary =
match summary with
| Env.Env_empty ->
() (*print_summary_string "Env_empty" "" *)
| Env_value (s, id, vd) ->
let loc = vd.val_loc in
if not (is_desired_loc loc) then ()
else if not (in_comp_env ev id) then ()
else begin
printf "@[<hov 2>";
(* print_summary_id "Env_value" id; *)
let buf = Buffer.create 100 in
let ppf = Format.formatter_of_buffer buf in
Xprinttyp.value_description id ppf vd;
fprintf ppf "@?" ;
let bcontent = Buffer.contents buf in
printf "%s;@ " bcontent;
myprint_loc loc;
let num_atts = List.length vd.val_attributes in
if num_atts > 0 then begin
printf ",@ ";
printf "#atts=%d@ " num_atts
end;
printf "@]@;"
end
| Env_type (s, id, td) ->
let loc = td.type_loc in
if not (is_desired_loc loc) then ()
else begin
printf "@[<hov 2>";
(* print_summary_id "Env_type" id; *)
Xprinttyp.type_declaration id std_formatter td;
myprint_loc loc;
printf "@]@;";
end
| Env_extension (s, id, ec) ->
let loc = ec.ext_loc in
if not (is_desired_loc loc) then ()
else begin
printf "@[<hov 2>";
(* print_summary_id "Env_extension" id; *)
Xprinttyp.extension_constructor id std_formatter ec;
myprint_loc ec.ext_loc;
printf "@]@;";
end
| Env_module (s, id, mp, md) ->
if not !comp_env_filter_flag then
print_summary_id "Env_module" id;
| Env_modtype (s, id, md) ->
print_summary_id "Env_modtype" id;
Xprinttyp.modtype_declaration id std_formatter md
| Env_class (s, id, cd) ->
print_summary_id "Env_class" id
| Env_cltype (s, id, ctd) ->
print_summary_id "Env_cltype" id
| Env_open (s, p) ->
if not !comp_env_filter_flag then
print_summary_string "Env_open" (Path.name p)
| Env_functor_arg (s, id) ->
print_summary_id "Env_functor_arg" id
| Env_constraints (s, cstrs) ->
print_summary_string "Env_constraints" "cstrs"
| Env_copy_types s ->
print_summary_string "Env_copy_types" ""
| Env_persistent (s, id) ->
print_summary_id "Env_persistent" id
| Env.Env_value_unbound (s, n, r) ->
if not !comp_env_filter_flag then begin
print_summary_string "Env_value_unbound"
(String.concat " " [n; "reason"])
end
| Env_module_unbound (s, n, r) ->
print_summary_string "Env_module_unbound"
(String.concat " " [n; "reason"])
let rec print_summary ev summary =
print_summary_item ev summary;
match next_summary summary with
| None -> ()
| Some s -> print_summary ev s
let print_ident_tbl (title: string) (tbl: int Ident.tbl) =
if tbl = Ident.empty then begin
printf "%s = empty@ " title
end else begin
printf "@[<2>%s {@ " title;
Ident.iter (fun id idval ->
printf "%s=%d@ " (Ident.name id) idval
) tbl;
printf "}@ @]";
end
(*
type compilation_env =
{ ce_stack: int Ident.tbl; (* Positions of variables in the stack *)
ce_heap: int Ident.tbl; (* Structure of the heap-allocated env *)
ce_rec: int Ident.tbl } (* Functions bound by the same let rec *)
*)
let print_comp_env (ce : Instruct.compilation_env) =
let stack : int Ident.tbl = ce.ce_stack in
let heap : int Ident.tbl = ce.ce_heap in
let recs : int Ident.tbl = ce.ce_rec in
if stack = Ident.empty &&
heap = Ident.empty &&
recs = Ident.empty
then ()
else begin
printf "@[<2>ev_compenv{@ ";
print_ident_tbl "ce_stack" stack;
print_ident_tbl "ce_heap" heap;
print_ident_tbl "ce_rec" recs;
printf "}@ @]";
end
(* From typing/subst.ml
type type_replacement =
| Path of Path.t
| Type_function of { params : type_expr list; body : type_expr }
type t =
{ types: type_replacement Path.Map.t;
modules: Path.t Path.Map.t;
modtypes: module_type Path.Map.t;
for_saving: bool;
loc: Location.t option;
}
*)
let print_subst title (ts: Subst.t) =
if ts = Subst.identity then begin
(* printf "%s is empty substitution,@ " title *)
()
end else begin
(* Unfortunately, the substitution is not made available *)
printf "{%s is non-empty (but opaque) substitution},@ " title
end
(* If there is a non-trivial substitution, get the summary of the
substituted environment. *)
let get_substituted_summary (ev: Instruct.debug_event) =
let summary = ev.ev_typenv in
let subst = ev.ev_typsubst in
if subst = Subst.identity then
(* No substitution *)
summary
else begin
let env2 = Envaux.env_from_summary summary subst in
let summary2 = Env.summary env2 in
summary2
end
(* From instruct.mli
type debug_event =
{ mutable ev_pos: int; (* Position in bytecode *)
ev_module: string; (* Name of defining module *)
ev_loc: Location.t; (* Location in source file *)
ev_kind: debug_event_kind; (* Before/after event *)
ev_defname: string; (* Enclosing definition *)
ev_info: debug_event_info; (* Extra information *)
ev_typenv: Env.summary; (* Typing environment *)
ev_typsubst: Subst.t; (* Substitution over types *)
ev_compenv: compilation_env; (* Compilation environment *)
ev_stacksize: int; (* Size of stack frame *)
ev_repr: debug_event_repr } (* Position of the representative *)
*)
let print_stack_and_env (ev: Instruct.debug_event) =
let ce = ev.ev_compenv in
let heap : int Ident.tbl = ce.ce_heap in
(* Find number of heap elements. They start a 2 since slot 0
has the starting pc and slot 1 has the arity and env start. *)
let hmax = ref 0 in
Ident.iter (fun id idval ->
if idval > !hmax then
hmax := idval;
) heap;
let sz = ev.ev_stacksize in
if sz = 0 && !hmax = 0 then () else begin
if sz > 0 then begin
let ray = Array.make sz "" in
let stack : int Ident.tbl = ce.ce_stack in
Ident.iter (fun id idval ->
ray.(sz - idval) <- Ident.name id;
) stack;
printf "@[<2>{Stack:";
for i = 0 to sz - 1 do
printf "@ %d:%s" i ray.(i)
done;
printf "@]}@ ";
end;
if !hmax > 0 then begin
let ray = Array.make (!hmax+1) "" in
Ident.iter (fun id idval ->
ray.(idval) <- Ident.name id;
) heap;
printf "@[<2>{Heap:";
for i = 2 to !hmax do
printf "@ %d:%s" i ray.(i)
done;
printf "@]}@ ";
end;
printf "@.";
end
let print_ev (ev: Instruct.debug_event) =
if !print_source_flag then
Show_source.show_point ev true;
if !print_stack_flag then
print_stack_and_env ev;
printf "pc=%d(=4*%d),@ " ev.Instruct.ev_pos (ev.Instruct.ev_pos/4 );
printf "ev_module=%s@ " ev.Instruct.ev_module;
myprint_loc ev.ev_loc;
printf ",@ ev_kind=%s,@ " (kind_string ev);
printf "ev_defname=%s,@ " ev.ev_defname;
printf "ev_info=%s,@ " (info_string ev);
printf "ev_typenv={@;@[";
print_summary ev (get_substituted_summary ev);
printf "}@;@]@.";
print_comp_env ev.ev_compenv;
print_subst "ev_typsubst" ev.ev_typsubst;
printf "ev_stacksize=%d,@ " ev.ev_stacksize;
printf "ev_repr=%s,@ " (repr_string ev);
()
let print_event (ev: Instruct.debug_event) =
let ls = ev.ev_loc.loc_start in
let fname = ls.Lexing.pos_fname in
check_file fname;
if !keep_file && is_matching_module ev.ev_module then begin
if !print_full_events_flag then
print_ev ev
else if !print_locations_flag then
printf "Def %s, Mod %s, %s%s "
ev.ev_defname ev.ev_module
(kind_string ev) (info_string ev);
Location.print_loc std_formatter ev.ev_loc;
printf "@.";
if !print_source_flag then
Show_source.show_point ev true;
if !print_stack_flag then
print_stack_and_env ev
end
let print_ev_indexed (ev: Instruct.debug_event) j =
if is_matching_module ev.ev_module then begin
printf "@[<2>ev[%d]:{@ " j;
print_event ev;
printf "}@]@.";
()
end
let dump_eventlist evl =
if !print_debug_flag then begin
printf "{length evl=%d@." (List.length evl);
let j = ref 0 in
List.iter (fun ev ->
print_ev_indexed ev !j;
j := !j + 1;
) evl;
printf "}@.";
end
let print_instr ic =
let pos = currpos ic in
List.iter print_event (Hashtbl.find_all event_table pos);
let buf = Buffer.create 100 in
let ppf = Format.formatter_of_buffer buf in
let descr = ref "" in
fprintf ppf "%8d " (pos / 4);
let op = inputu ic in
if op >= Array.length Opnames.names_of_instructions || op < 0
then (pp_print_string ppf "*** unknown opcode : "; pp_print_int ppf op)
else pp_print_string ppf names_of_instructions.(op);
begin try
let info = List.assoc op op_info in
let shape = fst info in
descr := snd info;
begin
if shape <> Nothing then pp_print_string ppf " ";
match shape with
| Uint -> pp_print_int ppf (inputu ic)
| Sint -> pp_print_int ppf (inputs ic)
| Uint_Uint
-> pp_print_int ppf (inputu ic); pp_print_string ppf ", "; pp_print_int ppf (inputu ic)
| Disp -> let p = currpc ic in pp_print_int ppf (p + inputs ic)
| Uint_Disp
-> pp_print_int ppf (inputu ic); pp_print_string ppf ", ";
let p = currpc ic in pp_print_int ppf (p + inputs ic)
| Sint_Disp
-> pp_print_int ppf (inputs ic); pp_print_string ppf ", ";
let p = currpc ic in pp_print_int ppf (p + inputs ic)
| Getglobal -> print_getglobal_name ppf ic
| Getglobal_Uint
-> print_getglobal_name ppf ic; pp_print_string ppf ", "; pp_print_int ppf (inputu ic)
| Setglobal -> print_setglobal_name ppf ic
| Primitive -> print_primitive ppf ic
| Uint_Primitive
-> pp_print_int ppf (inputu ic); pp_print_string ppf ", "; print_primitive ppf ic
| Switch
-> let n = inputu ic in
let orig = currpc ic in
for i = 0 to (n land 0xFFFF) - 1 do
fprintf ppf "@. int "; pp_print_int ppf i; pp_print_string ppf " -> ";
pp_print_int ppf(orig + inputs ic);
done;
for i = 0 to (n lsr 16) - 1 do
fprintf ppf "@. tag "; pp_print_int ppf i; pp_print_string ppf " -> ";
pp_print_int ppf(orig + inputs ic);
done;
| Closurerec
-> let nfuncs = inputu ic in
let nvars = inputu ic in
let orig = currpc ic in
pp_print_int ppf nvars;
for _i = 0 to nfuncs - 1 do
pp_print_string ppf ", ";
pp_print_int ppf (orig + inputs ic);
done;
| Pubmet
-> let tag = inputs ic in
let _cache = inputu ic in
pp_print_int ppf tag
| Nothing -> ();
end;
with Not_found -> pp_print_string ppf " (unknown arguments)"
end;
fprintf ppf "@?";
if not !keep_file then ()
else
let line = Buffer.contents buf in
if !print_description_flag then begin
let llen = String.length line in
let tab_col = 45 in
let pad_len = max 0 (tab_col - llen) in
let padding = String.make pad_len ' ' in
printf "%s %s ;%s@." line padding !descr
end else
printf "%s@." line
(* Disassemble a block of code *)
let print_code ic len =
start := pos_in ic;
let stop = !start + len in
while pos_in ic < stop do print_instr ic done
let input_stringlist ic len =
let get_string_list sect len =
let rec fold s e acc =
if e != len then
if sect.[e] = '\000' then
fold (e+1) (e+1) (String.sub sect s (e-s) :: acc)
else fold s (e+1) acc
else acc
in fold 0 0 []
in
let sect = really_input_string ic len in
get_string_list sect len
let dummy_crc = String.make 32 '-'
let null_crc = String.make 32 '0'
let string_of_crc crc = if !print_crc_flag then Digest.to_hex crc else null_crc
let print_name_crc (name, crco) =
let crc =
match crco with
None -> dummy_crc
| Some crc -> string_of_crc crc
in
printf "\t%s\t%s@." crc name
let print_line name =
printf "\t%s@." name
let print_required_global id =
printf "\t%s@." (Ident.name id)
(* Relocation information
type reloc_info =
Reloc_literal of Lambda.structured_constant (* structured constant *)
| Reloc_getglobal of Ident.t (* reference to a global *)