1 | /*! @file CoverageReaderQEMU.cc |
---|
2 | * @brief CoverageReaderQEMU Implementation |
---|
3 | * |
---|
4 | * This file contains the implementation of the functions supporting |
---|
5 | * reading the QEMU coverage data files. |
---|
6 | */ |
---|
7 | |
---|
8 | #include "covoar-config.h" |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <sys/stat.h> |
---|
13 | |
---|
14 | #include "app_common.h" |
---|
15 | #include "CoverageReaderQEMU.h" |
---|
16 | #include "CoverageMap.h" |
---|
17 | #include "ExecutableInfo.h" |
---|
18 | |
---|
19 | #include "qemu-traces.h" |
---|
20 | |
---|
21 | #if HAVE_STAT64 |
---|
22 | #define OPEN fopen64 |
---|
23 | #else |
---|
24 | #define OPEN fopen |
---|
25 | #endif |
---|
26 | |
---|
27 | namespace Coverage { |
---|
28 | |
---|
29 | CoverageReaderQEMU::CoverageReaderQEMU() |
---|
30 | { |
---|
31 | BranchInfoAvailable = true; |
---|
32 | } |
---|
33 | |
---|
34 | CoverageReaderQEMU::~CoverageReaderQEMU() |
---|
35 | { |
---|
36 | } |
---|
37 | |
---|
38 | void CoverageReaderQEMU::processFile( |
---|
39 | const char* const file, |
---|
40 | ExecutableInfo* const executableInformation |
---|
41 | ) |
---|
42 | { |
---|
43 | struct trace_header header; |
---|
44 | uintptr_t i; |
---|
45 | int status; |
---|
46 | FILE* traceFile; |
---|
47 | uint8_t taken; |
---|
48 | uint8_t notTaken; |
---|
49 | uint8_t branchInfo; |
---|
50 | |
---|
51 | taken = TargetInfo->qemuTakenBit(); |
---|
52 | notTaken = TargetInfo->qemuNotTakenBit(); |
---|
53 | branchInfo = taken | notTaken; |
---|
54 | |
---|
55 | // |
---|
56 | // Open the coverage file and read the header. |
---|
57 | // |
---|
58 | traceFile = OPEN( file, "r" ); |
---|
59 | if (!traceFile) { |
---|
60 | fprintf( |
---|
61 | stderr, |
---|
62 | "ERROR: CoverageReaderQEMU::processFile - Unable to open %s\n", |
---|
63 | file |
---|
64 | ); |
---|
65 | exit( -1 ); |
---|
66 | } |
---|
67 | |
---|
68 | status = fread( &header, sizeof(trace_header), 1, traceFile ); |
---|
69 | if (status != 1) { |
---|
70 | fprintf( |
---|
71 | stderr, |
---|
72 | "ERROR: CoverageReaderQEMU::processFile - " |
---|
73 | "Unable to read header from %s\n", |
---|
74 | file |
---|
75 | ); |
---|
76 | exit( -1 ); |
---|
77 | } |
---|
78 | |
---|
79 | #if 0 |
---|
80 | fprintf( |
---|
81 | stderr, |
---|
82 | "magic = %s\n" |
---|
83 | "version = %d\n" |
---|
84 | "kind = %d\n" |
---|
85 | "sizeof_target_pc = %d\n" |
---|
86 | "big_endian = %d\n" |
---|
87 | "machine = %02x:%02x\n", |
---|
88 | header.magic, |
---|
89 | header.version, |
---|
90 | header.kind, |
---|
91 | header.sizeof_target_pc, |
---|
92 | header.big_endian, |
---|
93 | header.machine[0], header.machine[1] |
---|
94 | ); |
---|
95 | #endif |
---|
96 | |
---|
97 | // |
---|
98 | // Read ENTRIES number of trace entries. |
---|
99 | // |
---|
100 | #define ENTRIES 1024 |
---|
101 | while (1) { |
---|
102 | CoverageMapBase *aCoverageMap = NULL; |
---|
103 | struct trace_entry entries[ENTRIES]; |
---|
104 | struct trace_entry *entry; |
---|
105 | int num_entries; |
---|
106 | |
---|
107 | |
---|
108 | // Read and process each line of the coverage file. |
---|
109 | num_entries = fread( |
---|
110 | entries, |
---|
111 | sizeof(struct trace_entry), |
---|
112 | ENTRIES, |
---|
113 | traceFile |
---|
114 | ); |
---|
115 | if (num_entries == 0) |
---|
116 | break; |
---|
117 | |
---|
118 | // Get the coverage map for each entry. Note that the map is |
---|
119 | // the same for each entry in the coverage map |
---|
120 | for (int count=0; count<num_entries; count++) { |
---|
121 | |
---|
122 | entry = &entries[count]; |
---|
123 | |
---|
124 | // Mark block as fully executed. |
---|
125 | // Obtain the coverage map containing the specified address. |
---|
126 | aCoverageMap = executableInformation->getCoverageMap( entry->pc ); |
---|
127 | |
---|
128 | // Ensure that coverage map exists. |
---|
129 | if (!aCoverageMap) |
---|
130 | continue; |
---|
131 | |
---|
132 | // Set was executed for each TRACE_OP_BLOCK |
---|
133 | if (entry->op & TRACE_OP_BLOCK) { |
---|
134 | for (i=0; i<entry->size; i++) { |
---|
135 | aCoverageMap->setWasExecuted( entry->pc + i ); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | // Determine if additional branch information is available. |
---|
140 | if ( (entry->op & branchInfo) != 0 ) { |
---|
141 | uint32_t a = entry->pc + entry->size - 1; |
---|
142 | while (!aCoverageMap->isStartOfInstruction(a)) |
---|
143 | a--; |
---|
144 | if (entry->op & taken) { |
---|
145 | aCoverageMap->setWasTaken( a ); |
---|
146 | } else if (entry->op & notTaken) { |
---|
147 | aCoverageMap->setWasNotTaken( a ); |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | fclose( traceFile ); |
---|
153 | } |
---|
154 | } |
---|