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 <stdio.h> |
---|
13 | #include <sys/stat.h> |
---|
14 | |
---|
15 | #include <rld.h> |
---|
16 | |
---|
17 | #include "app_common.h" |
---|
18 | #include "CoverageReaderQEMU.h" |
---|
19 | #include "CoverageMap.h" |
---|
20 | #include "ExecutableInfo.h" |
---|
21 | |
---|
22 | #include "qemu-traces.h" |
---|
23 | |
---|
24 | #if HAVE_OPEN64 |
---|
25 | #define OPEN fopen64 |
---|
26 | #else |
---|
27 | #define OPEN fopen |
---|
28 | #endif |
---|
29 | |
---|
30 | namespace Coverage { |
---|
31 | |
---|
32 | CoverageReaderQEMU::CoverageReaderQEMU() |
---|
33 | { |
---|
34 | BranchInfoAvailable = true; |
---|
35 | } |
---|
36 | |
---|
37 | CoverageReaderQEMU::~CoverageReaderQEMU() |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | void CoverageReaderQEMU::processFile( |
---|
42 | const char* const file, |
---|
43 | ExecutableInfo* const executableInformation |
---|
44 | ) |
---|
45 | { |
---|
46 | struct trace_header header; |
---|
47 | uintptr_t i; |
---|
48 | int status; |
---|
49 | FILE* traceFile; |
---|
50 | uint8_t taken; |
---|
51 | uint8_t notTaken; |
---|
52 | uint8_t branchInfo; |
---|
53 | |
---|
54 | taken = TargetInfo->qemuTakenBit(); |
---|
55 | notTaken = TargetInfo->qemuNotTakenBit(); |
---|
56 | branchInfo = taken | notTaken; |
---|
57 | |
---|
58 | // |
---|
59 | // Open the coverage file and read the header. |
---|
60 | // |
---|
61 | traceFile = ::OPEN( file, "r" ); |
---|
62 | if (!traceFile) { |
---|
63 | std::ostringstream what; |
---|
64 | what << "Unable to open " << file; |
---|
65 | throw rld::error( what, "CoverageReaderQEMU::processFile" ); |
---|
66 | } |
---|
67 | |
---|
68 | status = ::fread( &header, sizeof(trace_header), 1, traceFile ); |
---|
69 | if (status != 1) { |
---|
70 | ::fclose( traceFile ); |
---|
71 | std::ostringstream what; |
---|
72 | what << "Unable to read header from " << file; |
---|
73 | throw rld::error( what, "CoverageReaderQEMU::processFile" ); |
---|
74 | } |
---|
75 | |
---|
76 | // |
---|
77 | // Read ENTRIES number of trace entries. |
---|
78 | // |
---|
79 | #define ENTRIES 1024 |
---|
80 | while (true) { |
---|
81 | CoverageMapBase *aCoverageMap = NULL; |
---|
82 | struct trace_entry entries[ENTRIES]; |
---|
83 | struct trace_entry *entry; |
---|
84 | int num_entries; |
---|
85 | |
---|
86 | |
---|
87 | // Read and process each line of the coverage file. |
---|
88 | num_entries = ::fread( |
---|
89 | entries, |
---|
90 | sizeof(struct trace_entry), |
---|
91 | ENTRIES, |
---|
92 | traceFile |
---|
93 | ); |
---|
94 | if (num_entries == 0) |
---|
95 | break; |
---|
96 | |
---|
97 | // Get the coverage map for each entry. Note that the map is |
---|
98 | // the same for each entry in the coverage map |
---|
99 | for (int count=0; count<num_entries; count++) { |
---|
100 | |
---|
101 | entry = &entries[count]; |
---|
102 | |
---|
103 | // Mark block as fully executed. |
---|
104 | // Obtain the coverage map containing the specified address. |
---|
105 | aCoverageMap = executableInformation->getCoverageMap( entry->pc ); |
---|
106 | |
---|
107 | // Ensure that coverage map exists. |
---|
108 | if (!aCoverageMap) |
---|
109 | continue; |
---|
110 | |
---|
111 | // Set was executed for each TRACE_OP_BLOCK |
---|
112 | if (entry->op & TRACE_OP_BLOCK) { |
---|
113 | for (i=0; i<entry->size; i++) { |
---|
114 | aCoverageMap->setWasExecuted( entry->pc + i ); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | // Determine if additional branch information is available. |
---|
119 | if ( (entry->op & branchInfo) != 0 ) { |
---|
120 | uint32_t a = entry->pc + entry->size - 1; |
---|
121 | while (!aCoverageMap->isStartOfInstruction(a)) |
---|
122 | a--; |
---|
123 | if (entry->op & taken) { |
---|
124 | aCoverageMap->setWasTaken( a ); |
---|
125 | } else if (entry->op & notTaken) { |
---|
126 | aCoverageMap->setWasNotTaken( a ); |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | ::fclose( traceFile ); |
---|
132 | } |
---|
133 | } |
---|