1 | |
---|
2 | /*! @file CoverageMapBase.cc |
---|
3 | * @brief CoverageMapBase Implementation |
---|
4 | * |
---|
5 | * This file contains the implementation of the functions |
---|
6 | * which provide a base level of functionality of a CoverageMap. |
---|
7 | */ |
---|
8 | |
---|
9 | #include <libgen.h> |
---|
10 | #include <limits.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include <stdlib.h> |
---|
13 | |
---|
14 | #include "CoverageMapBase.h" |
---|
15 | |
---|
16 | namespace Coverage { |
---|
17 | |
---|
18 | CoverageMapBase::CoverageMapBase( |
---|
19 | const std::string& exefileName, |
---|
20 | uint32_t low, |
---|
21 | uint32_t high |
---|
22 | ) |
---|
23 | { |
---|
24 | uint32_t a; |
---|
25 | AddressRange_t range; |
---|
26 | |
---|
27 | range.fileName = exefileName; |
---|
28 | range.lowAddress = low; |
---|
29 | range.highAddress = high; |
---|
30 | RangeList.push_back( range ); |
---|
31 | |
---|
32 | Size = high - low + 1; |
---|
33 | |
---|
34 | Info = new perAddressInfo_t[ Size ]; |
---|
35 | |
---|
36 | for (a=0; a<Size; a++) { |
---|
37 | |
---|
38 | perAddressInfo_t *i = &Info[ a ]; |
---|
39 | |
---|
40 | i->isStartOfInstruction = false; |
---|
41 | i->wasExecuted = 0; |
---|
42 | i->isBranch = false; |
---|
43 | i->isNop = false; |
---|
44 | i->wasTaken = 0; |
---|
45 | i->wasNotTaken = 0; |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | CoverageMapBase::~CoverageMapBase() |
---|
50 | { |
---|
51 | if (Info) |
---|
52 | delete Info; |
---|
53 | } |
---|
54 | |
---|
55 | void CoverageMapBase::Add( uint32_t low, uint32_t high ) |
---|
56 | { |
---|
57 | AddressRange_t range; |
---|
58 | |
---|
59 | range.lowAddress = low; |
---|
60 | range.highAddress = high; |
---|
61 | RangeList.push_back( range ); |
---|
62 | } |
---|
63 | |
---|
64 | bool CoverageMapBase::determineOffset( |
---|
65 | uint32_t address, |
---|
66 | uint32_t *offset |
---|
67 | )const |
---|
68 | { |
---|
69 | AddressRange::const_iterator itr; |
---|
70 | |
---|
71 | for ( itr = RangeList.begin(); itr != RangeList.end(); itr++ ) { |
---|
72 | if ((address >= itr->lowAddress) && (address <= itr->highAddress)){ |
---|
73 | *offset = address - itr->lowAddress; |
---|
74 | return true; |
---|
75 | } |
---|
76 | } |
---|
77 | *offset = 0; |
---|
78 | return false; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | void CoverageMapBase::dump( void ) const { |
---|
83 | |
---|
84 | uint32_t a; |
---|
85 | perAddressInfo_t* entry; |
---|
86 | |
---|
87 | fprintf( stderr, "Coverage Map Contents:\n" ); |
---|
88 | |
---|
89 | /* |
---|
90 | * XXX - Dump is only marking the first Address Range. |
---|
91 | */ |
---|
92 | |
---|
93 | for (a = 0; a < Size; a++) { |
---|
94 | |
---|
95 | entry = &Info[ a ]; |
---|
96 | |
---|
97 | fprintf( |
---|
98 | stderr, |
---|
99 | "0x%x - isStartOfInstruction = %s, wasExecuted = %s\n", |
---|
100 | a + RangeList.front().lowAddress, |
---|
101 | entry->isStartOfInstruction ? "TRUE" : "FALSE", |
---|
102 | entry->wasExecuted ? "TRUE" : "FALSE" |
---|
103 | ); |
---|
104 | fprintf( |
---|
105 | stderr, |
---|
106 | " isBranch = %s, wasTaken = %s, wasNotTaken = %s\n", |
---|
107 | entry->isBranch ? "TRUE" : "FALSE", |
---|
108 | entry->wasTaken ? "TRUE" : "FALSE", |
---|
109 | entry->wasNotTaken ? "TRUE" : "FALSE" |
---|
110 | ); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | bool CoverageMapBase::getBeginningOfInstruction( |
---|
115 | uint32_t address, |
---|
116 | uint32_t* beginning |
---|
117 | ) const |
---|
118 | { |
---|
119 | bool status = false; |
---|
120 | uint32_t start; |
---|
121 | AddressRange_t range; |
---|
122 | |
---|
123 | |
---|
124 | status = getRange( address, &range ); |
---|
125 | if ( status != true ) |
---|
126 | return status; |
---|
127 | |
---|
128 | start = address; |
---|
129 | |
---|
130 | while (start >= range.lowAddress ) { |
---|
131 | if (Info[ start - range.lowAddress ].isStartOfInstruction) { |
---|
132 | *beginning = start; |
---|
133 | status = true; |
---|
134 | break; |
---|
135 | } |
---|
136 | else |
---|
137 | start--; |
---|
138 | } |
---|
139 | |
---|
140 | return status; |
---|
141 | } |
---|
142 | |
---|
143 | int32_t CoverageMapBase::getFirstLowAddress() const |
---|
144 | { |
---|
145 | return RangeList.front().lowAddress; |
---|
146 | } |
---|
147 | |
---|
148 | bool CoverageMapBase::getRange( uint32_t address, AddressRange_t *range ) const |
---|
149 | { |
---|
150 | AddressRange::const_iterator itr; |
---|
151 | |
---|
152 | for ( itr = RangeList.begin(); itr != RangeList.end(); itr++ ) { |
---|
153 | if ((address >= itr->lowAddress) && (address <= itr->highAddress)){ |
---|
154 | range->lowAddress = itr->lowAddress; |
---|
155 | range->highAddress = itr->highAddress; |
---|
156 | return true; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | range->lowAddress = 0; |
---|
161 | range->highAddress = 0; |
---|
162 | |
---|
163 | return false; |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | uint32_t CoverageMapBase::getSize() const |
---|
168 | { |
---|
169 | return Size; |
---|
170 | } |
---|
171 | |
---|
172 | void CoverageMapBase::setIsStartOfInstruction( |
---|
173 | uint32_t address |
---|
174 | ) |
---|
175 | { |
---|
176 | uint32_t offset; |
---|
177 | |
---|
178 | if (determineOffset( address, &offset ) != true) |
---|
179 | return; |
---|
180 | |
---|
181 | Info[ offset ].isStartOfInstruction = true; |
---|
182 | } |
---|
183 | |
---|
184 | bool CoverageMapBase::isStartOfInstruction( uint32_t address ) const |
---|
185 | { |
---|
186 | uint32_t offset; |
---|
187 | |
---|
188 | if (determineOffset( address, &offset ) != true) |
---|
189 | return false; |
---|
190 | |
---|
191 | return Info[ offset ].isStartOfInstruction; |
---|
192 | } |
---|
193 | |
---|
194 | void CoverageMapBase::setWasExecuted( uint32_t address ) |
---|
195 | { |
---|
196 | uint32_t offset; |
---|
197 | |
---|
198 | if (determineOffset( address, &offset ) != true) |
---|
199 | return; |
---|
200 | |
---|
201 | Info[ offset ].wasExecuted += 1; |
---|
202 | } |
---|
203 | |
---|
204 | void CoverageMapBase::sumWasExecuted( uint32_t address, uint32_t addition) |
---|
205 | { |
---|
206 | uint32_t offset; |
---|
207 | |
---|
208 | if (determineOffset( address, &offset ) != true) |
---|
209 | return; |
---|
210 | |
---|
211 | Info[ offset ].wasExecuted += addition; |
---|
212 | } |
---|
213 | |
---|
214 | bool CoverageMapBase::wasExecuted( uint32_t address ) const |
---|
215 | { |
---|
216 | uint32_t offset; |
---|
217 | bool result; |
---|
218 | |
---|
219 | result = true; |
---|
220 | |
---|
221 | if (determineOffset( address, &offset ) != true) |
---|
222 | result = false; |
---|
223 | |
---|
224 | if (Info[ offset ].wasExecuted <= 0) |
---|
225 | result = false; |
---|
226 | |
---|
227 | return result; |
---|
228 | } |
---|
229 | |
---|
230 | uint32_t CoverageMapBase::getWasExecuted( uint32_t address ) const |
---|
231 | { |
---|
232 | uint32_t offset; |
---|
233 | |
---|
234 | if (determineOffset( address, &offset ) != true) |
---|
235 | return 0; |
---|
236 | |
---|
237 | return Info[ offset ].wasExecuted; |
---|
238 | } |
---|
239 | |
---|
240 | void CoverageMapBase::setIsBranch( |
---|
241 | uint32_t address |
---|
242 | ) |
---|
243 | { |
---|
244 | uint32_t offset; |
---|
245 | |
---|
246 | if (determineOffset( address, &offset ) != true) |
---|
247 | return; |
---|
248 | |
---|
249 | Info[ offset ].isBranch = true; |
---|
250 | } |
---|
251 | |
---|
252 | bool CoverageMapBase::isNop( uint32_t address ) const |
---|
253 | { |
---|
254 | uint32_t offset; |
---|
255 | |
---|
256 | if (determineOffset( address, &offset ) != true) |
---|
257 | return false; |
---|
258 | |
---|
259 | return Info[ offset ].isNop; |
---|
260 | } |
---|
261 | |
---|
262 | void CoverageMapBase::setIsNop( |
---|
263 | uint32_t address |
---|
264 | ) |
---|
265 | { |
---|
266 | uint32_t offset; |
---|
267 | |
---|
268 | if (determineOffset( address, &offset ) != true) |
---|
269 | return; |
---|
270 | |
---|
271 | Info[ offset ].isNop = true; |
---|
272 | } |
---|
273 | |
---|
274 | bool CoverageMapBase::isBranch( uint32_t address ) const |
---|
275 | { |
---|
276 | uint32_t offset; |
---|
277 | |
---|
278 | if (determineOffset( address, &offset ) != true) |
---|
279 | return false; |
---|
280 | |
---|
281 | return Info[ offset ].isBranch; |
---|
282 | } |
---|
283 | |
---|
284 | void CoverageMapBase::setWasTaken( |
---|
285 | uint32_t address |
---|
286 | ) |
---|
287 | { |
---|
288 | uint32_t offset; |
---|
289 | |
---|
290 | if (determineOffset( address, &offset ) != true) |
---|
291 | return; |
---|
292 | |
---|
293 | Info[ offset ].wasTaken += 1; |
---|
294 | } |
---|
295 | |
---|
296 | void CoverageMapBase::setWasNotTaken( |
---|
297 | uint32_t address |
---|
298 | ) |
---|
299 | { |
---|
300 | uint32_t offset; |
---|
301 | |
---|
302 | if (determineOffset( address, &offset ) != true) |
---|
303 | return; |
---|
304 | |
---|
305 | Info[ offset ].wasNotTaken += 1; |
---|
306 | } |
---|
307 | |
---|
308 | bool CoverageMapBase::wasAlwaysTaken( uint32_t address ) const |
---|
309 | { |
---|
310 | uint32_t offset; |
---|
311 | |
---|
312 | if (determineOffset( address, &offset ) != true) |
---|
313 | return false; |
---|
314 | |
---|
315 | return (Info[ offset ].wasTaken && |
---|
316 | !Info[ offset ].wasNotTaken); |
---|
317 | } |
---|
318 | |
---|
319 | bool CoverageMapBase::wasNeverTaken( uint32_t address ) const |
---|
320 | { |
---|
321 | uint32_t offset; |
---|
322 | |
---|
323 | if (determineOffset( address, &offset ) != true) |
---|
324 | return false; |
---|
325 | |
---|
326 | return (!Info[ offset ].wasTaken && |
---|
327 | Info[ offset ].wasNotTaken); |
---|
328 | } |
---|
329 | |
---|
330 | bool CoverageMapBase::wasNotTaken( uint32_t address ) const |
---|
331 | { |
---|
332 | uint32_t offset; |
---|
333 | bool result; |
---|
334 | |
---|
335 | result = true; |
---|
336 | |
---|
337 | if (determineOffset( address, &offset ) != true) |
---|
338 | result = false; |
---|
339 | |
---|
340 | if (Info[ offset ].wasNotTaken <= 0) |
---|
341 | result = false; |
---|
342 | |
---|
343 | return result; |
---|
344 | } |
---|
345 | |
---|
346 | void CoverageMapBase::sumWasNotTaken( uint32_t address, uint32_t addition) |
---|
347 | { |
---|
348 | uint32_t offset; |
---|
349 | |
---|
350 | if (determineOffset( address, &offset ) != true) |
---|
351 | return; |
---|
352 | |
---|
353 | Info[ offset ].wasNotTaken += addition; |
---|
354 | } |
---|
355 | |
---|
356 | uint32_t CoverageMapBase::getWasNotTaken( uint32_t address ) const |
---|
357 | { |
---|
358 | uint32_t offset; |
---|
359 | |
---|
360 | if (determineOffset( address, &offset ) != true) |
---|
361 | return 0; |
---|
362 | |
---|
363 | return Info[ offset ].wasNotTaken; |
---|
364 | } |
---|
365 | |
---|
366 | bool CoverageMapBase::wasTaken( uint32_t address ) const |
---|
367 | { |
---|
368 | uint32_t offset; |
---|
369 | bool result; |
---|
370 | |
---|
371 | result = true; |
---|
372 | |
---|
373 | if (determineOffset( address, &offset ) != true) |
---|
374 | result = false; |
---|
375 | |
---|
376 | if (Info[ offset ].wasTaken <= 0) |
---|
377 | result = false; |
---|
378 | |
---|
379 | return result; |
---|
380 | } |
---|
381 | |
---|
382 | void CoverageMapBase::sumWasTaken( uint32_t address, uint32_t addition) |
---|
383 | { |
---|
384 | uint32_t offset; |
---|
385 | |
---|
386 | if (determineOffset( address, &offset ) != true) |
---|
387 | return; |
---|
388 | |
---|
389 | Info[ offset ].wasTaken += addition; |
---|
390 | } |
---|
391 | |
---|
392 | uint32_t CoverageMapBase::getWasTaken( uint32_t address ) const |
---|
393 | { |
---|
394 | uint32_t offset; |
---|
395 | |
---|
396 | if (determineOffset( address, &offset ) != true) |
---|
397 | return 0; |
---|
398 | |
---|
399 | return Info[ offset ].wasTaken; |
---|
400 | } |
---|
401 | } |
---|