forked from AdaCore/gnatcoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg_dump.adb
2010 lines (1682 loc) · 68.5 KB
/
cfg_dump.adb
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
------------------------------------------------------------------------------
-- --
-- GNATcoverage --
-- --
-- Copyright (C) 2008-2021, AdaCore --
-- --
-- GNATcoverage is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This software is distributed in the hope that it will be useful --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with this software; see file --
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license. --
------------------------------------------------------------------------------
with Ada.Command_Line;
with Ada.Containers.Doubly_Linked_Lists;
with Ada.Containers.Ordered_Maps;
with Ada.Containers.Ordered_Sets;
with Ada.Containers.Vectors;
with Ada.Exceptions; use Ada.Exceptions;
with Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Conversion;
with Ada.Unchecked_Deallocation;
with Interfaces;
with GNAT.Expect; use GNAT.Expect;
with GNAT.OS_Lib;
with Binary_Files; use Binary_Files;
with Coverage.Source;
with Decision_Map;
with Diagnostics; use Diagnostics;
with Disassemblers;
with Elf_Disassemblers;
with Execs_Dbase;
with Hex_Images; use Hex_Images;
with Highlighting;
with Outputs; use Outputs;
with Qemu_Traces;
with SC_Obligations;
with Strings;
with Support_Files;
with Switches;
with Traces; use Traces;
with Traces_Dbase;
with Traces_Elf; use Traces_Elf;
with Traces_Files;
package body CFG_Dump is
use type Pc_Type;
function To_Unbounded_String
(S : String) return Ada.Strings.Unbounded.Unbounded_String
renames Ada.Strings.Unbounded.To_Unbounded_String;
procedure Load_SCOs (ALI_Filename : String);
-- Wrapper for SC_Obligations.Load_SCOs, assumes there is no source file to
-- ignore.
--------------------
-- Output helpers --
--------------------
-- There are two kinds of output to handle. The output to a file is
-- handled with Ada.Text_IO, and the output to a subprocess is handled
-- with GNAT.Expect. These two packages have incompatible interfaces with
-- respect to text output, so there is a need for an abstraction layer.
type Output_Kind is (File, Subprocess);
type Output_File_Access is access all File_Type;
type Output_Type is record
Kind : Output_Kind;
File : Output_File_Access;
-- Used when Kind => File
Pd : Process_Descriptor;
-- Used when Kind => Subprogram
To_Close : Boolean;
-- Whether the output must be closed. When Kind => File and File =>
-- Standard_Output, this musn't be closed.
end record;
procedure Put (Output : in out Output_Type; C : Character);
procedure Put (Output : in out Output_Type; S : String);
procedure Put_Line (Output : in out Output_Type; S : String);
procedure Close (Output : in out Output_Type);
-------------------------
-- CFG data structures --
-------------------------
type Successor_Kind is (Fallthrough, Branch, Subp_Return, Raise_Exception);
type Successor_Record is record
Kind : Successor_Kind;
Known : Boolean;
-- Whether the address of this successor instruction is known (i.e if it
-- is not the target of an indirect branch).
Address : Pc_Type;
-- Address of this successor instruction
end record;
-- Instructions are executed as sequences, and the execution flow can
-- sometimes jump. This type is used to describe the possible successors
-- of some instruction during the execution.
package Successor_Vectors is new Ada.Containers.Vectors
(Index_Type => Positive,
Element_Type => Successor_Record);
package Boolean_Vectors is new Ada.Containers.Vectors
(Index_Type => Positive,
Element_Type => Boolean);
package PC_Sets is new Ada.Containers.Ordered_Sets
(Element_Type => Pc_Type,
"<" => "<",
"=" => "=");
subtype PC_Set is PC_Sets.Set;
type Instruction_Record;
type Instruction_Access is access all Instruction_Record;
type Instruction_Record is record
Bytes : Binary_Content;
-- Location and content for this instruction
Section : Binary_Files.Section_Index;
-- Index of the section in which this instruction was found
Insn_Set : Elf_Disassemblers.Insn_Set_Type;
Selected : Boolean;
-- Whether this instruction matches selected locations
Executed : Boolean;
-- Whether this instructions was executed when producing traces
Control_Flow_Pair : Pc_Type;
-- If this instruction has control flow effects, contains the address
-- of the last instructions executed before the effects are applied.
-- On architectures with no delay slot, this will be this instruction
-- itself.
--
-- If this instruction ends a basic block because of control flow
-- effects applying to it, contains the address of the instruction these
-- effects come from. On architectures with no delay slot, this will be
-- this instruction itself.
--
-- In all other cases, contains No_PC.
Successors : Successor_Vectors.Vector;
-- Set of instructions that can be executed right after this one (with
-- fallthrough, branch, call, etc.).
Executed_Successors : Boolean_Vectors.Vector;
-- For each successor, whether the corresponding branch has been taken
end record;
function Has_Lower_Address
(Left, Right : Instruction_Access) return Boolean is
(Left.Bytes.First < Right.Bytes.First);
function Has_Same_Address
(Left, Right : Instruction_Access) return Boolean is
(Left.Bytes.First = Right.Bytes.First);
function Address (Insn : Instruction_Access) return Pc_Type is
(Insn.Bytes.First);
procedure Free is new Ada.Unchecked_Deallocation
(Instruction_Record, Instruction_Access);
package Instruction_Vectors is new Ada.Containers.Vectors
(Index_Type => Positive,
Element_Type => Instruction_Access);
subtype Instructions_Vector is Instruction_Vectors.Vector;
package Instruction_Sets is new Ada.Containers.Ordered_Sets
(Element_Type => Instruction_Access,
"<" => Has_Lower_Address,
"=" => Has_Same_Address);
subtype Instructions_Set is Instruction_Sets.Set;
subtype Basic_Block is Instructions_Vector;
-- A basic block gathers multiple instructions that are executed in
-- a sequence. The execution flow always starts executing the first
-- instruction of a basic block and cannot stop in the middle of it except
-- because of a trap. Note that basic blocks always contain at least one
-- instruction.
type Basic_Block_Access is access all Basic_Block;
procedure Free is new Ada.Unchecked_Deallocation
(Basic_Block, Basic_Block_Access);
package Basic_Block_Maps is new Ada.Containers.Ordered_Maps
(Key_Type => Pc_Type,
Element_Type => Basic_Block_Access);
subtype Basic_Blocks is Basic_Block_Maps.Map;
function Address (BB : Basic_Block) return Pc_Type is
(BB.First_Element.Bytes.First);
-- Return the address of the first instruction of a basic block
function Successors (BB : Basic_Block) return Successor_Vectors.Vector is
(BB.Last_Element.Successors);
-- Return the successors of the last instruction of a basic block
function Get_Instruction
(CFG : Basic_Blocks;
Address : Pc_Type) return Instruction_Access;
-- Return the instruction whose first byte is Address, or null if there is
-- no one.
type Context_Type is record
Exec : Exe_File_Acc;
-- Executable instructions come from
Locs : Proc_Locations;
-- Locations used to filter instructions to display
Instructions : Instructions_Vector;
-- Temporary list of instructions to display. Built before building the
-- CFG (see Collect_Instructions).
Basic_Block_Starters : PC_Set;
-- Set of instructions that have to start a basic block
Other_Outcome, Other_Income : Instructions_Set;
-- Outcome: keys are not selected instructions that branch/have
-- fallthrough to a selected instruction. Associated values are
-- these selected instructions. Income: reciprocal.
CFG : Basic_Blocks;
Group_By_Condition : Boolean;
Keep_Edges : Boolean;
Tag_Executed : Boolean;
Output : Output_Type;
-- Output file to write the CFG to
end record;
-- Group of information used to compute the CFG
type Context_Access is access all Context_Type;
procedure Collect_Instructions
(Context : Context_Access;
Section : Address_Info_Acc);
-- Complete Context.Instructions with instructions found in Section that
-- match provided locations.
-- This disassembles all of Section, not just the specified locations,
-- which is a hughe waste and means that any issue impacting disassembly
-- of any symbol in the section can potentially hinder dumping the CFG
-- for an entirely unrelated symbol, even if the offending location is
-- outside of the specified range???
procedure Build_Basic_Blocks (Context : Context_Access);
-- Group Context.Instructions into Context.Basic_Blocks. Make
-- Context.Instructions empty before returning.
procedure Clear_Basic_Blocks (Context : Context_Access);
-- Deallocate basic blocks and contained instructions
procedure Tag_Executed_Instructions
(Context : Context_Access;
Exec_Path : String;
Traces_Files_List : Inputs.Inputs_Type);
-- Extract traces from each file and use them to tag instructions if they
-- have been executed. This pass works on the CFG.
procedure Output_CFG (Context : Context_Access);
-- Actually output the CFG in the dot format to the output
function Expand_Tabs (S : String; Start : Positive) return String;
-- Expand ASCII.HT characters in S as 8-cols wide alignments. Consider
-- that S (S'First) is the Start'th character on the line.
------------
-- Colors --
------------
Hex_Color : constant Highlighting.Color_Type := "808080";
Edge_Unselected_Color : constant Highlighting.Color_Type := "808080";
Edge_Default_Color : constant Highlighting.Color_Type := "800000";
Edge_Executed_Color : constant Highlighting.Color_Type := "008000";
Unexecuted_Insn_Color : constant Highlighting.Color_Type := "800000";
Executed_Insn_Color : constant Highlighting.Color_Type := "008000";
Sloc_Color : constant Highlighting.Color_Type := "004080";
Unknown_Color : constant Highlighting.Color_Type := "a0a0a0";
True_Color : constant Highlighting.Color_Type := "008000";
False_Color : constant Highlighting.Color_Type := "800000";
---------------------
-- Get_Instruction --
---------------------
function Get_Instruction
(CFG : Basic_Blocks;
Address : Pc_Type) return Instruction_Access
is
use Basic_Block_Maps;
BB : constant Cursor := CFG.Floor (Address);
begin
if BB /= No_Element then
for Insn of Element (BB).all loop
if CFG_Dump.Address (Insn) = Address then
return Insn;
end if;
end loop;
end if;
return null;
end Get_Instruction;
--------------------------
-- Collect_Instructions --
--------------------------
procedure Collect_Instructions
(Context : Context_Access;
Section : Address_Info_Acc)
is
Code : constant Binary_Content := Section.Section_Content;
Sec_Ndx : constant Binary_Files.Section_Index := Section.Section_Sec_Idx;
I_Ranges : Elf_Disassemblers.Insn_Set_Ranges renames
Get_Insn_Set_Ranges (Context.Exec.all, Sec_Ndx).all;
Cache : Elf_Disassemblers.Insn_Set_Cache :=
Elf_Disassemblers.Empty_Cache;
Disas : access Disassemblers.Disassembler'Class;
Insn_Set : Elf_Disassemblers.Insn_Set_Type;
type Disassembly_State is
(OK,
Invalid_Insn,
Skip_Padding);
State : Disassembly_State;
-- Disassembled instructions
Disas_Error : Ada.Strings.Unbounded.Unbounded_String;
-- Hold the error message when disassembling failed
PC : Pc_Type := Code.First;
-- Address of the first byte of the instruction we are about to
-- disassemble.
Old_PC : Pc_Type;
-- Likewise for the previous instruction
Sym_Cur : Address_Info_Sets.Cursor :=
Find_Address_Info (Context.Exec.all, Symbol_Addresses, PC);
-- Cursor to the symbol that contains PC, if it exists, or to the first
-- symbol after PC. If there is no such symbol, No_Element.
Symbol : Address_Info_Acc;
-- Symbol corresponding to Sym_Cur, on null if it is No_Element
Insn, Last_Insn : Instruction_Access := null;
-- Currently analyzed instructions and the previous one. Last_Insn is
-- set to null when it has no fallthrough.
Insn_Added, Last_Insn_Added : Boolean := False;
-- Whether the current/previous instruction is referenced in the
-- database. Used to know what to deallocate.
Insn_Len : Positive;
-- Length of the current instruction
Insn_Branch : Branch_Kind;
Insn_Flag_Indir, Insn_Flag_Cond : Boolean;
Insn_Branch_Dest, Insn_FT_Dest : Disassemblers.Dest;
Insn_Has_FT : Boolean := False;
Insn_Starts_BB : Boolean := False;
type Branch_Point_Record is record
Branch_Instruction : Pc_Type;
-- Instruction this branch point comes from
Address : Pc_Type;
-- Address of the last instruction that is executed before the branch
-- actually happens.
Target : Successor_Record;
-- Describes the effect of this branch
Is_Conditional : Boolean;
-- Whether is a conditional branch
end record;
-- Holds information about the effect of a branching instruction
package Branch_Point_Lists is new Ada.Containers.Doubly_Linked_Lists
(Element_Type => Branch_Point_Record);
use Branch_Point_Lists;
Branch_Points : Branch_Point_Lists.List;
-- Stores data about the effect of analyzed branch instructions. These
-- can be postponed because of delay slots. Branch points are removed
-- from this list once the point they apply to has been reached.
Cur : Branch_Point_Lists.Cursor;
procedure Add_Branch_Point
(Address, Target, Delay_Slot : Pc_Type;
Effect : Successor_Kind;
Is_Conditional : Boolean);
-- Add a branch point to the list
----------------------
-- Add_Branch_Point --
----------------------
procedure Add_Branch_Point
(Address, Target, Delay_Slot : Pc_Type;
Effect : Successor_Kind;
Is_Conditional : Boolean) is
begin
Branch_Points.Append
((Branch_Instruction => Address,
Address =>
(if Delay_Slot = No_PC
then Address
else Delay_Slot),
Target =>
(Kind => Effect,
Known => Target /= No_PC,
Address => Target),
Is_Conditional => Is_Conditional));
end Add_Branch_Point;
-- Start of processing for Collect_Instructions
begin
while Elf_Disassemblers.Iterate_Over_Insns
(I_Ranges, Cache, Code.Last, PC, Insn_Set)
loop
Disas := Elf_Disassemblers.Disa_For_Machine
(Traces.Machine, Insn_Set);
if Insn = null or else Insn_Added then
Insn := new Instruction_Record'
(Bytes => (null, PC, No_PC),
Section => Sec_Ndx,
Insn_Set => Insn_Set,
others => <>);
Insn_Added := False;
end if;
-- Adjust Sym according to PC; Symbol as well
while Address_Info_Sets."/=" (Sym_Cur, Address_Info_Sets.No_Element)
and then Address_Info_Sets.Element (Sym_Cur).Last < PC
loop
Sym_Cur := Address_Info_Sets.Next (Sym_Cur);
end loop;
Symbol :=
(if Address_Info_Sets."=" (Sym_Cur, Address_Info_Sets.No_Element)
then null
else Address_Info_Sets.Element (Sym_Cur));
-- First decode the current instruction
begin
State := OK;
Insn_Len := Disas.Get_Insn_Length
(Slice (Code, PC, Code.Last));
Insn.Bytes := Slice
(Code, PC, PC + Pc_Type (Insn_Len) - 1);
if Symbol /= null then
if Insn.Bytes.First < Symbol.First
and then Symbol.First <= Insn.Bytes.Last
then
-- This instruction crosses a symbol's lower address: it
-- looks like we are disassembling padding bytes. Skip
-- these until this symbol.
State := Skip_Padding;
elsif Insn.Bytes.First <= Symbol.Last
and then Symbol.Last < Insn.Bytes.Last
then
-- This instruction crosses a symbol's upper address: we
-- assume instructions are not supposed to do that.
State := Invalid_Insn;
Disas_Error := To_Unbounded_String
("Insn crosses a symbol's upper address");
end if;
end if;
if State = OK then
Disas.Get_Insn_Properties
(Insn_Bin => Insn.Bytes,
Pc => PC,
Branch => Insn_Branch,
Flag_Indir => Insn_Flag_Indir,
Flag_Cond => Insn_Flag_Cond,
Branch_Dest => Insn_Branch_Dest,
FT_Dest => Insn_FT_Dest);
end if;
end;
case State is
when OK =>
Insn.Selected := Matches_Locations
(Context.Exec, Context.Locs, Address (Insn));
Insn.Executed := False;
Insn.Control_Flow_Pair := No_PC;
-- Past this point, PC contains the address of the next
-- instruction. Use Address (Insn) to get the address of
-- the current one.
Old_PC := PC;
PC := PC + Pc_Type (Insn_Len);
-- Chain the previous instruction to this one only when needed
if Last_Insn /= null then
Last_Insn.Successors.Append
((Kind => Fallthrough,
Known => True,
Address => Address (Insn)));
if not Insn.Selected and then Last_Insn.Selected then
-- This instruction is not selected, but since it's
-- directly referenced by the previous instruction,
-- we are interested in it anyway.
Context.Other_Outcome.Include (Insn);
Insn_Added := True;
elsif Insn.Selected and then not Last_Insn.Selected then
-- Likewise, reciprocally
Context.Other_Income.Insert (Last_Insn);
Last_Insn_Added := True;
end if;
end if;
if Insn.Selected then
Context.Instructions.Append (Insn);
Insn_Added := True;
if Insn_Starts_BB then
Context.Basic_Block_Starters.Include (Address (Insn));
end if;
end if;
-- If this is a control flow instruction, memorize its effect
-- to apply them after the delay slot.
case Insn_Branch is
when Br_Jmp =>
Add_Branch_Point
(Address (Insn),
Insn_Branch_Dest.Target,
Insn_Branch_Dest.Delay_Slot,
Branch,
Insn_Flag_Cond);
Insn.Control_Flow_Pair := Insn_Branch_Dest.Delay_Slot;
when Br_Ret =>
Add_Branch_Point
(Address (Insn),
No_PC,
Insn_Branch_Dest.Delay_Slot,
Subp_Return,
Insn_Flag_Cond);
when Br_Call =>
-- Calls usually have a fallthrough edge. However, calls
-- that raise an exception do have one.
if not Context.Keep_Edges then
-- Use debug information to get indirect call target, if
-- possible.
if Insn_Flag_Indir then
Insn_Branch_Dest.Target := Get_Call_Target
(Context.Exec.all,
Address (Insn),
Pc_Type (Insn_Len));
end if;
declare
Symbol : constant Address_Info_Acc := Get_Symbol
(Context.Exec.all, Insn_Branch_Dest.Target);
Symbol_Name : constant String_Access :=
(if Symbol = null
then null
else Symbol.Symbol_Name);
begin
if Symbol_Name /= null
and then
Decision_Map.Subp_Raises_Exception
(Traces_Elf.Platform_Independent_Symbol
(Symbol_Name.all, Context.Exec.all))
then
Add_Branch_Point
(Address (Insn),
No_PC,
Insn_Branch_Dest.Delay_Slot,
Raise_Exception,
Insn_Flag_Cond);
end if;
end;
end if;
when others =>
null;
end case;
when Invalid_Insn =>
Disassemblers.Abort_Disassembler_Error
(PC, Slice (Code, PC, Code.Last),
Ada.Strings.Unbounded.To_String (Disas_Error));
when Skip_Padding =>
-- If there is no symbol anymore to re-synchronize the
-- disassembling process, we consider that all remaining
-- bytes are padding.
if Symbol = null then
return;
end if;
Insn.Selected := False;
PC := Symbol.First;
end case;
-- These are for the next instruction, during the next iteration.
-- If the current instruction wasn't selected, be sure to break the
-- basic block anyway: propagate the Insn_Starts_BB flag.
Insn_Has_FT := State = OK;
Insn_Starts_BB := Insn_Starts_BB and then not Insn.Selected;
-- Apply branch points when needed
Cur := Branch_Points.First;
while Cur /= No_Element loop
declare
BP : Branch_Point_Record renames Element (Cur);
Next_Cur : constant Cursor := Next (Cur);
begin
if BP.Address < Address (Insn) then
Branch_Points.Delete (Cur);
elsif BP.Address = Address (Insn) then
Insn_Has_FT := BP.Is_Conditional;
Insn_Starts_BB := True;
if BP.Target.Known then
if Matches_Locations
(Context.Exec, Context.Locs, BP.Target.Address)
then
Context.Basic_Block_Starters.Include
(BP.Target.Address);
elsif Insn.Selected then
-- The destination of this branch is out of scope, but
-- it is referenced anyway by the graph. Since we do
-- not have the real instruction, create a dummy one
-- so that a proper node is generated for it.
declare
PC : constant Pc_Type := BP.Target.Address;
Dummy_Insn : Instruction_Access :=
new Instruction_Record'
(Bytes => (null, PC, No_PC),
Section => 0,
others => <>);
Cur : Instruction_Sets.Cursor;
Inserted : Boolean;
begin
Context.Other_Outcome.Insert
(Dummy_Insn, Cur, Inserted);
if not Inserted then
Free (Dummy_Insn);
end if;
end;
end if;
end if;
Insn.Control_Flow_Pair := BP.Branch_Instruction;
Insn.Successors.Append (BP.Target);
Branch_Points.Delete (Cur);
end if;
Cur := Next_Cur;
end;
end loop;
-- Now, prepare the next iteration
if Last_Insn /= null and then not Last_Insn_Added then
Free (Last_Insn);
end if;
if Insn_Has_FT then
Last_Insn := Insn;
Last_Insn_Added := Insn_Added;
Insn := null;
Insn_Added := False;
else
Last_Insn := null;
Last_Insn_Added := False;
end if;
-- Stop when wrapping
exit when PC < Old_PC;
end loop;
end Collect_Instructions;
------------------------
-- Build_Basic_Blocks --
------------------------
procedure Build_Basic_Blocks (Context : Context_Access) is
CFG : Basic_Blocks renames Context.CFG;
Current_BB : Basic_Block_Access := null;
procedure Finalize_Successors (BB : in out Basic_Block);
-- Remove successors of all instructions in BB except for the last
-- instruction. Such successors are useless since the only possible
-- successor for such instructions is the next one. Also elaborate
-- the "executed" flag. for successors.
-------------------------
-- Finalize_Successors --
-------------------------
procedure Finalize_Successors (BB : in out Basic_Block) is
use Instruction_Vectors;
use Successor_Vectors;
begin
for Cur in BB.Iterate loop
if Cur /= BB.Last then
Element (Cur).Successors.Clear;
else
-- By default, no successor has been reached
for Succ of BB.Last_Element.Successors loop
Element (Cur).Executed_Successors.Append (False);
end loop;
end if;
end loop;
end Finalize_Successors;
-- Start of processing for Build_Basic_Blocks
begin
for Insn of Context.Instructions loop
if Current_BB = null
or else Context.Basic_Block_Starters.Contains (Address (Insn))
then
if Current_BB /= null then
Finalize_Successors (Current_BB.all);
end if;
Current_BB := new Basic_Block;
CFG.Insert (Insn.Bytes.First, Current_BB);
end if;
Current_BB.Append (Insn);
end loop;
if Current_BB /= null then
Finalize_Successors (Current_BB.all);
end if;
Context.Instructions.Clear;
end Build_Basic_Blocks;
------------------------
-- Clear_Basic_Blocks --
------------------------
procedure Clear_Basic_Blocks (Context : Context_Access) is
begin
for BB of Context.CFG loop
for Insn of BB.all loop
Free (Insn);
end loop;
Free (BB);
end loop;
end Clear_Basic_Blocks;
-------------------------------
-- Tag_Executed_Instructions --
-------------------------------
procedure Tag_Executed_Instructions
(Context : Context_Access;
Exec_Path : String;
Traces_Files_List : Inputs.Inputs_Type)
is
use type Interfaces.Unsigned_8;
use Traces_Dbase;
use Traces_Files;
Base : Traces_Base;
procedure Import_Traces (Filename : String);
-- Import the trace from Filename into Base
procedure Process_Trace_Entry (Trace : Trace_Entry);
-- Tag as executed all the instructions in the CFG it covers
procedure Process_Trace_Op
(Insn : Instruction_Access; Op : Interfaces.Unsigned_8);
-- Tag as executed all the successors in Insn
-------------------
-- Import_Traces --
-------------------
procedure Import_Traces (Filename : String) is
Trace_File : Trace_File_Type;
Result : Read_Result;
begin
Read_Trace_File (Filename, Trace_File, Result, Base);
Success_Or_Fatal_Error (Filename, Result);
declare
Exec_Sig : constant Binary_File_Signature :=
Get_Signature (Context.Exec.all);
Trace_Sig : constant Binary_File_Signature :=
Get_Signature (Trace_File);
Mismatch_Reason : constant String :=
Match_Signatures (Exec_Sig, Trace_Sig);
begin
if Mismatch_Reason /= "" then
Warn
("ELF file " & Exec_Path
& " does not seem to match trace file "
& Filename & ": " & Mismatch_Reason);
end if;
end;
end Import_Traces;
-------------------------
-- Process_Trace_Entry --
-------------------------
procedure Process_Trace_Entry (Trace : Trace_Entry) is
use Basic_Block_Maps;
BB : Cursor;
Branch_Insn : Instruction_Access;
begin
-- Look for the last basic block that starts before this trace entry
BB := Context.CFG.Floor (Trace.First);
-- If we cannot find any, the first selected basic block may start
-- after this trace entry so give it another try. Note that the CFG
-- is supposed to contain at least one basic block, so First_Element
-- is safe.
if BB = No_Element
and then Address (Context.CFG.First_Element.all) <= Trace.Last
then
BB := Context.CFG.First;
end if;
while BB /= No_Element
and then Address (Element (BB).all) <= Trace.Last
loop
-- Tag executed instructions
for Insn of Element (BB).all loop
-- Tag executed instructions
if Address (Insn) in Trace.First .. Trace.Last then
Insn.Executed := True;
end if;
-- Tag reached successors.
if Insn.Bytes.Last = Trace.Last then
if Insn.Control_Flow_Pair /= Address (Insn) then
-- On architectures with delay slot, the "branch taken"
-- information may be present on the branch instruction
-- instead of on the instruction on which the branch
-- applies. Due to the nature of delay slots, this branch
-- instruction may appear in the middle of a basic block.
Branch_Insn := Get_Instruction
(Context.CFG, Insn.Control_Flow_Pair);
else
Branch_Insn := Insn;
end if;
if Branch_Insn /= null then
Process_Trace_Op (Branch_Insn, Trace.Op);
end if;
end if;
end loop;
declare
use Ada.Containers;
Last_Insn : constant Instruction_Access :=
Element (BB).Last_Element;
begin
if Last_Insn.Bytes.First in Trace.First .. Trace.Last
and then Last_Insn.Successors.Length = 1
then
-- This instruction ends this basic block, but appears
-- in the middle of a trace entry: this can only be a
-- non-branch instruction, and thus it should have excactly
-- one successor. Ignore when there is more than one
-- successor anyway: emulators may start a trace entry
-- with a delay-slot instruction.
Last_Insn.Executed_Successors.Replace_Element
(Last_Insn.Executed_Successors.First_Index,
Last_Insn.Executed);
end if;
end;
Next (BB);
end loop;
end Process_Trace_Entry;
----------------------
-- Process_Trace_Op --
----------------------
procedure Process_Trace_Op
(Insn : Instruction_Access; Op : Interfaces.Unsigned_8)
is
use Ada.Containers;
use Qemu_Traces;
I : Natural;
begin
I := Insn.Successors.First_Index;
for Succ of Insn.Successors loop
-- No computation is needed if this sucessor has already
-- been proved as being reached.
if not Insn.Executed_Successors.Element (I) then
Insn.Executed_Successors.Replace_Element
(I,
((Op and
(case Succ.Kind is
when Fallthrough =>
Trace_Op_Br1,
when Branch =>
-- For unconditional branches, Op_Block is
-- used instead of Op_Br*.
(if Insn.Successors.Length = 1
then Trace_Op_Block
else Trace_Op_Br0),
when Subp_Return =>
Trace_Op_Block,
when Raise_Exception =>
Trace_Op_Fault))
/= 0));
end if;
I := I + 1;
end loop;
end Process_Trace_Op;
Iter : Entry_Iterator;
Trace : Trace_Entry;
-- Start of processingr for Tag_Executed_Instructions
begin
Init_Base (Base);
-- Import all trace entries to the database, so that they are sorted by
-- address
Inputs.Iterate (Traces_Files_List, Import_Traces'Access);
-- And then, use them to tag instructions
Init (Base, Iter, Context.CFG.First_Key);
loop
Get_Next_Trace (Trace, Iter);
exit when Trace = Bad_Trace;