source: rtems-tools/tester/covoar/CoverageMapBase.cc @ bf8e59f

5
Last change on this file since bf8e59f was bf8e59f, checked in by Chris Johns <chrisj@…>, on 06/21/18 at 07:55:24

covoar: Covert CoverageMapBase? C to C++

Updates #3462

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