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