summaryrefslogtreecommitdiffstats
path: root/tester/covoar/GcovFunctionData.cc
blob: 7487c408561cc5103885537e97516e0eda450d92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*! @file GcovFunctionData.cc
 *  @brief GcovFunctionData Implementation
 *
 *  This file contains the implementation of the class storing information
 *  about single function.
 */

#include <cstdio>
#include <cstring>
#include <cinttypes>

#include "GcovFunctionData.h"
#include "ObjdumpProcessor.h"
#include "CoverageMapBase.h"
#include "DesiredSymbols.h"
#include "rtems-utils.h"


namespace Gcov {

  GcovFunctionData::GcovFunctionData()
  {
    id              = 0;
    checksum        = 0;
    firstLineNumber = 0;
    numberOfBlocks  = 0;
    numberOfArcs    = 0;
    coverageMap     = NULL;
    symbolInfo      = NULL;
  }

  GcovFunctionData::~GcovFunctionData()
  {
  }

  void GcovFunctionData::setChecksum( const uint32_t chk )
  {
    checksum = chk;
  }

  void GcovFunctionData::setId( const uint32_t idNumber )
  {
    id = idNumber;
  }


  void GcovFunctionData::setFirstLineNumber( const uint32_t lineNo )
  {
    firstLineNumber = lineNo;
  }

  bool GcovFunctionData::setFunctionName(
    const std::string&        fcnName,
    Coverage::DesiredSymbols& symbolsToAnalyze
  )
  {
    std::string symbolName;

    symbolName = fcnName;

    if ( fcnName.length() >= FUNCTION_NAME_LENGTH ) {
      std::cerr << "ERROR: Function name is too long to be correctly stored: "
                << fcnName.length() << std::endl;
      return false;
    }

    functionName = fcnName;

    // Tie function to its coverage map
    symbolInfo = symbolsToAnalyze.find( symbolName );
    if ( symbolInfo != NULL ) {
      coverageMap = symbolInfo->unifiedCoverageMap;
    }

#if 0
    if ( coverageMap == NULL ) {
      std::cerr << "ERROR: Could not find coverage map for: " << symbolName
                << std::endl;
    } else {
      std::cerr << "SUCCESS: Found coverage map for: " << symbolName
                << std::endl;
    }
#endif

    return true;
  }

  bool GcovFunctionData::setFileName( const std::string& fileName ) {
    if ( fileName.length() >= FILE_NAME_LENGTH ) {
      std::cerr << "ERROR: File name is too long to be correctly stored: "
                << fileName.length() << std::endl;
      return false;
    }

    sourceFileName = fileName;
    return true;
  }

  arcs_t GcovFunctionData::getArcs() const
  {
    return arcs;
  }

  uint32_t GcovFunctionData::getChecksum() const
  {
    return checksum;
  }

  uint32_t GcovFunctionData::getId() const
  {
    return id;
  }

  void GcovFunctionData::getCounters(
    uint64_t* counterValues,
    uint32_t& countersFound,
    uint64_t& countersSum,
    uint64_t& countersMax
  )
  {
    arcs_iterator_t currentArc;
    int             i;

    countersFound = 0;
    countersSum   = 0;
    countersMax   = 0;

    // Locate relevant counters and copy their values
    i = 0;
    for ( currentArc = arcs.begin(); currentArc != arcs.end(); currentArc++ ) {
      if (
        currentArc->flags == 0 ||
        currentArc->flags == 2 ||
        currentArc->flags == 4
      ) {
        countersFound++;
        countersSum += currentArc->counter;
        counterValues[i] = currentArc->counter;

        if ( countersMax <= currentArc->counter ) {
          countersMax = currentArc->counter;
        }

        i++;
      }
    }
  }

  blocks_t GcovFunctionData::getBlocks() const
  {
    return blocks;
  }

  void GcovFunctionData::addArc(
    uint32_t source,
    uint32_t destination,
    uint32_t flags
  )
  {
    gcov_arc_info arc;

    numberOfArcs++;
    arc.sourceBlock      = source;
    arc.destinationBlock = destination;
    arc.flags            = flags;
    arc.counter          = 0;
    arcs.push_back( arc );
  }

  void GcovFunctionData::addBlock(
    const uint32_t     id,
    const uint32_t     flags,
    const std::string& sourceFileName
  )
  {
    gcov_block_info block;

    numberOfBlocks++;
    block.id             = id;
    block.flags          = flags;
    block.numberOfLines  = 0;
    block.counter        = 0;
    block.sourceFileName = sourceFileName;
    blocks.push_back( block );
  }

  void GcovFunctionData::printFunctionInfo(
    std::ofstream& textFile,
    uint32_t       function_number
  )
  {
    blocks_iterator_t currentBlock;
    arcs_iterator_t   currentArc;

    textFile << std::endl << std::endl
             << "=========================="
             << "FUNCTION  " << std::setw( 3 ) << function_number
             << "=========================="
             << std::endl << std::endl
             << "Name:      " << functionName << std::endl
             << "File:      " << sourceFileName << std::endl
             << "Line:      " << firstLineNumber << std::endl
             << "Id:        " << id << std::endl
             << "Checksum:  0x" << std::hex << checksum << std::dec
             << std::endl << std::endl;

    // Print arcs info
    for ( currentArc = arcs.begin(); currentArc != arcs.end(); currentArc++ ) {
      printArcInfo( textFile, currentArc );
    }
    textFile << std::endl;

    // Print blocks info
    for (
      currentBlock = blocks.begin();
      currentBlock != blocks.end();
      currentBlock++
    ) {
      printBlockInfo( textFile, currentBlock );
    }
  }

  void GcovFunctionData::printCoverageInfo(
    std::ofstream& textFile,
    uint32_t       function_number
  )
  {
    uint32_t baseAddress = 0;
    uint32_t baseSize;
    uint32_t currentAddress;
    std::list<Coverage::objdumpLine_t>::iterator instruction;

    if ( coverageMap != NULL ) {

      for (
        instruction = symbolInfo->instructions.begin();
        instruction != symbolInfo->instructions.end();
        instruction++
      ) {
        if( instruction->isInstruction ) {
          baseAddress = instruction->address;
          break;
        }
      }

      baseSize   = coverageMap->getSize();

      textFile << std::endl << "Instructions (Base address: 0x"
               << std::setfill( '0' ) << std::setw( 8 )
               << std::hex << baseAddress << std::dec << std::setfill( ' ' )
               << ", Size: " << std::setw( 4 ) << baseSize
               << "):" << std::endl << std::endl;

      for (
        instruction = symbolInfo->instructions.begin();
        instruction != symbolInfo->instructions.end();
        instruction++
      )
      {
        if ( instruction->isInstruction ) {
          currentAddress = instruction->address - baseAddress;

          textFile << std::left << "0x" << std::setw( 70 )
                   << instruction->line.c_str() << " " << std::right
                   << "| 0x" << std::hex << std::setfill( '0' )
                   << std::setw( 8 ) << currentAddress << " "
                   << std::dec << std::setfill( ' ' )
                   << "*| exec: " << std::setw( 4 )
                   << coverageMap->getWasExecuted( currentAddress )
                   << " | taken/not: " << std::setw( 4 )
                   << coverageMap->getWasTaken( currentAddress )
                   << "/" << std::setw( 4 )
                   << coverageMap->getWasNotTaken( currentAddress )
                   << " ";

          if ( instruction->isBranch )
            textFile << "| Branch ";
          else
            textFile << "         ";

          if ( instruction->isNop )
            textFile << "| NOP(" << std::setw( 3 ) << instruction->nopSize
                     << ") " << std::endl;
          else
            textFile << "           " << std::endl;
        }
      }
    }
  }

  void GcovFunctionData::setBlockFileName(
    const blocks_iterator_t block,
    const std::string&      fileName
  )
  {
    block->sourceFileName = fileName;
  }

  void GcovFunctionData::addBlockLine(
    const blocks_iterator_t block,
    const uint32_t          line
  )
  {
    block->lines.push_back( line );
    (block->numberOfLines)++;
  }

  blocks_iterator_t GcovFunctionData::findBlockById( const uint32_t id )
  {
    blocks_iterator_t blockIterator;

    if ( !blocks.empty() ) {
      blockIterator = blocks.begin();
      while ( blockIterator != blocks.end() ) {
        if ( blockIterator->id == id) {
          break;
        }

        blockIterator++;
      }
    } else {
      std::cerr << "ERROR: GcovFunctionData::findBlockById() failed"
                << ", no blocks present" << std::endl;
    }

    return blockIterator;
  }

  void GcovFunctionData::printArcInfo(
    std::ofstream&  textFile,
    arcs_iterator_t arc
  )
  {
    textFile << " > ARC " << std::setw( 3 ) << arc->sourceBlock << " -> "
             << arc->destinationBlock << " ";

    textFile << "\tFLAGS: ";
    switch ( arc->flags ) {
      case 0:
        textFile << "( ___________ ____ _______ )";
        break;
      case 1:
        textFile << "( ___________ ____ ON_TREE )";
        break;
      case 2:
        textFile << "( ___________ FAKE _______ )";
        break;
      case 3:
        textFile << "( ___________ FAKE ON_TREE )";
        break;
      case 4:
        textFile << "( FALLTHROUGH ____ _______ )";
        break;
      case 5:
        textFile << "( FALLTHROUGH ____ ON_TREE )";
        break;
      default:
        textFile  << "( =======FLAGS_ERROR====== )";
        std::cerr << " ERROR: Unknown arc flag: 0x"
                  << std::hex << arcs.back().flags << std::endl
                  << std::dec;
        break;
    }

    textFile << "\tTaken: " << std::setw( 5 ) << arc->counter << std::endl;
  }

  void GcovFunctionData::printBlockInfo(
    std::ofstream&    textFile,
    blocks_iterator_t block
  )
  {
    std::list<uint32_t>::iterator line;

    rtems::utils::ostream_guard old_state( textFile );

    textFile << " > BLOCK " << std::setw( 3 ) << block->id
             << " from " << block->sourceFileName << std::endl
             << "    -counter: " << std::setw( 5 ) << block->counter
             << std::endl
             << "    -flags: 0x" << std::hex << block->flags << std::endl
             << "    -lines: ";

    if ( !block->lines.empty() )
      for ( line = block->lines.begin(); line != block->lines.end(); line++ ) {
        textFile << *line << ", ";
      }

    textFile << std::endl;
  }

  bool GcovFunctionData::processFunctionCounters() {

    uint32_t            baseAddress = 0;
    uint32_t            currentAddress = 0;
    blocks_iterator_t   blockIterator;
    blocks_iterator_t   blockIterator2;
    arcs_iterator_t     arcIterator;
    arcs_iterator_t     arcIterator2;
    std::list<uint64_t> taken;       // List of taken counts for branches
    std::list<uint64_t> notTaken;    // List of not taken counts for branches
    std::list<Coverage::objdumpLine_t>::iterator instruction;

    //std::cerr << "DEBUG: Processing counters for file: " << sourceFileName
    //          << std::endl;
    if (
      blocks.empty()      ||
      arcs.empty()        ||
      coverageMap == NULL ||
      symbolInfo->instructions.empty()
    ) {
      //std::cerr << "DEBUG: sanity check returned false for function: "
      //          << functionName << " from file: " << sourceFileName
      //          << std::endl;
      return false;
    }

    // Reset iterators and variables
    blockIterator  = blocks.begin();
    arcIterator    = arcs.begin();
    arcIterator2   = arcIterator;
    arcIterator2++;
    instruction    = symbolInfo->instructions.begin();
    baseAddress    = coverageMap->getFirstLowAddress();      //symbolInfo->baseAddress;
    currentAddress = baseAddress;

    // Find taken/not taken values for branches
    if ( !processBranches( &taken , &notTaken ) )
    {
      //std::cerr << "ERROR: Failed to process branches for function: "
      //          << functionName << " from file: " << sourceFileName
      //          << std::endl;
      return false;
    };

    // Process the branching arcs
    while ( blockIterator != blocks.end() ) {
      //std::cerr << "DEBUG: Processing branches" << std::endl;
      while ( arcIterator->sourceBlock != blockIterator->id ) {
        if ( arcIterator == arcs.end() ) {
          //std::cerr << "ERROR: Unexpectedly runned out of arcs to analyze"
          //          << std::endl;
          return false;
        }

        arcIterator++;
        arcIterator2++;
      }

      // If no more branches break;
      if ( arcIterator2 == arcs.end() ) {
        break;
      }

      // If this is a branch without FAKE arcs process it
      if (
         ( arcIterator->sourceBlock == arcIterator2->sourceBlock ) &&
        !( arcIterator->flags & FAKE_ARC_FLAG ) &&
        !( arcIterator2->flags & FAKE_ARC_FLAG )
      ) {
        if ( taken.empty() || notTaken.empty() ) {
          std::cerr << "ERROR: Branches missing for function: "
                    << functionName << " from file: " << sourceFileName
                    << std::endl;
          return false;
        }

        //std::cerr << "DEBUG: Found true branching arc "
        //          << std::setw( 3 ) << arcIterator->sourceBlock << " -> "
        //          << std::setw( 3 ) << arcIteratior->destinationBlock
        //          << std::endl;
        if ( arcIterator->flags & FALLTHROUGH_ARC_FLAG ) {
          arcIterator->counter = notTaken.front();
          notTaken.pop_front();
          arcIterator2->counter = taken.front();
          taken.pop_front();
        } else {
          arcIterator2->counter = notTaken.front();
          notTaken.pop_front();
          arcIterator->counter = taken.front();
          taken.pop_front();
        }

        blockIterator2 = blocks.begin();
        //TODO: ADD FAILSAFE
        while ( arcIterator->destinationBlock != blockIterator2->id ) {
          blockIterator2++;
        }

        blockIterator2->counter += arcIterator->counter;

        blockIterator2 = blocks.begin();
        //TODO: ADD FAILSAFE
        while ( arcIterator2->destinationBlock != blockIterator2->id ) {
            blockIterator2++;
        }

         blockIterator2->counter += arcIterator2->counter;
      }

      blockIterator++;
    }

    // Reset iterators and variables
    blockIterator = blocks.begin();
    arcIterator   = arcs.begin();
    arcIterator2  = arcIterator;
    arcIterator2++;

    // Set the first block
    blockIterator->counter = coverageMap->getWasExecuted( currentAddress );

    // Analyze remaining arcs and blocks
    while ( blockIterator != blocks.end() ) {
      while ( arcIterator->sourceBlock != blockIterator->id ) {
        if ( arcIterator == arcs.end() ) {
          std::cerr << "ERROR: Unexpectedly runned out of arcs to analyze"
                    << std::endl;
          return false;
        }

        arcIterator++;
        arcIterator2++;
      }

      // If this is the last arc, propagate counter and exit
      if ( arcIterator2 == arcs.end() ) {
        //std::cerr << "DEBUG: Found last arc " << std::setw( 3 )
        //          << arcIterator->sourceBlock << " -> " << std::setw( 3 )
        //          << arcIterator->destinationBlock << std::endl;
        arcIterator->counter = blockIterator->counter;
        blockIterator2       =  blocks.begin();
        while ( arcIterator->destinationBlock != blockIterator2->id ) {  //TODO: ADD FAILSAFE
          blockIterator2++;
        }

        blockIterator2->counter += arcIterator->counter;

        return true;
      }

      // If this is not a branch, propagate counter and continue
      if ( arcIterator->sourceBlock != arcIterator2->sourceBlock ) {
        //std::cerr << "DEBUG: Found simple arc " << std::setw( 3 )
        //          << arcIterator->sourceBlock << " -> " << std::setw( 3 )
        //          << arcIterator->destinationBlock << std::endl;
        arcIterator->counter = blockIterator->counter;
        blockIterator2       =  blocks.begin();

        while ( arcIterator->destinationBlock != blockIterator2->id ) { //TODO: ADD FAILSAFE
          blockIterator2++;
        }

        blockIterator2->counter += arcIterator->counter;
      }

      // If this is  a branch with FAKE arc
      else if (
        ( arcIterator->sourceBlock == arcIterator2->sourceBlock ) &&
        ( arcIterator2->flags & FAKE_ARC_FLAG )
      ) {
        //std::cerr << "DEBUG: Found fake branching arc " << std::setw( 3 )
        //          << arcIterator->sourceBlock << " -> " << std::setw( 3 )
        //          << arcIterator->destinationBlock << std::endl;
        arcIterator->counter = blockIterator->counter;
        blockIterator2       = blocks.begin();

        while ( arcIterator->destinationBlock != blockIterator2->id ) { //TODO: ADD FAILSAFE
          blockIterator2++;
        }

        blockIterator2->counter += arcIterator->counter;
      }

      // If this is a legitimate branch
      blockIterator++;
    }

    return true;
  }

  bool GcovFunctionData::processBranches(
    std::list<uint64_t>* taken ,
    std::list<uint64_t>* notTaken
  )
  {
    uint32_t baseAddress = 0;
    uint32_t currentAddress;
    std::list<Coverage::objdumpLine_t>::iterator instruction;

    if ( coverageMap == NULL ) {
      return false;
    }

    //baseAddress = coverageMap->getFirstLowAddress();      //symbolInfo->baseAddress;
    for (
      instruction = symbolInfo->instructions.begin();
      instruction != symbolInfo->instructions.end();
      instruction++
    ) {
      if( instruction->isInstruction ) {
        baseAddress = instruction->address;
        break;
      }
    }

    // std::cerr << "DEBUG: Processing instructions in search of branches"
    //           << std::endl;
    for (
      instruction = symbolInfo->instructions.begin();
      instruction != symbolInfo->instructions.end();
      instruction++
    ) {
      if ( instruction->isInstruction ) {
        currentAddress = instruction-> address - baseAddress;
        if ( instruction->isBranch ) {
          taken->push_back(
            (uint64_t) coverageMap->getWasTaken( currentAddress )
          );

          notTaken->push_back(
            (uint64_t) coverageMap->getWasNotTaken( currentAddress )
          );

          //std::cerr << "Added branch to list taken/not: " << std::setw( 4 )
          //          << coverageMap->getWasTaken( currentAddress )
          //          << "/" << std::setw( 4 )
          //          << coverageMap->getWasNotTaken( currentAddress )
          //          << std::endl;
        }
      }
    }
    return true;
  }
}