summaryrefslogtreecommitdiffstats
path: root/tester/covoar/DesiredSymbols.cc
blob: 89f95ea61100ab6682cfa0a27f846ef02e66392e (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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*! @file DesiredSymbols.cc
 *  @brief DesiredSymbols Implementation
 *
 *  This file contains the implementation of the functions
 *  which provide the functionality of the DesiredSymbols.
 */

#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#endif

#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <iostream>

#include "rld.h"
#include <rld-config.h>
#include "rld-symbols.h"
#include "rld-files.h"

#include "DesiredSymbols.h"
#include "app_common.h"
#include "CoverageMap.h"
#include "ObjdumpProcessor.h"

namespace Coverage {

  DesiredSymbols::DesiredSymbols()
  {
  }

  DesiredSymbols::~DesiredSymbols()
  {
  }

  bool DesiredSymbols::load(
    const std::string& symbolsSet,
    const std::string& buildTarget,
    const std::string& buildBSP,
    bool               verbose
  )
  {
    rld::files::cache cache;
    bool              r = true;

    //
    // Load the INI file looking for a top level:
    //
    //  [symbols-sets]
    //  sets = A, B, C
    //
    // For each set read the libraries from the configuration file and load.
    //
    //  [A]
    //  libraries = @BUILD-PREFIX@/c/@BSP@/A/libA.a
    //
    //  [B]
    //  libraries = @BUILD-PREFIX@/c/@BSP@/B/libB.a
    //
    try {
      cache.open();

      rld::config::config config;

      if (verbose)
        std::cerr << "Loading symbol sets: " << symbolsSet << std::endl;

      config.load (symbolsSet);

      const rld::config::section& sym_section = config.get_section("symbol-sets");

      rld::strings sets;
      rld::config::parse_items (sym_section, "sets", sets, true);

      for (const std::string set : sets) {
        if (verbose)
          std::cerr << " Symbol set: " << set << std::endl;
        const rld::config::section& set_section = config.get_section(set);
        rld::strings libs;
        rld::config::parse_items (set_section, "libraries", libs, true);
        for (std::string lib : libs) {
          lib = rld::find_replace(lib, "@BUILD-TARGET@", buildTarget);
          lib = rld::find_replace(lib, "@BSP@", buildBSP);
          if (verbose)
            std::cerr << " Loading library: " << lib << std::endl;
          cache.add(lib);
        }
      }

      rld::symbols::table symbols;

      cache.load_symbols (symbols, true);

      for (auto& kv : symbols.globals()) {
        const rld::symbols::symbol& sym = *(kv.second);
        set[sym.name()] = *(new SymbolInformation);
      }
      for (auto& kv : symbols.weaks()) {
        const rld::symbols::symbol& sym = *(kv.second);
        set[sym.name()] = *(new SymbolInformation);
      }

    } catch (rld::error re) {
      std::cerr << "error: "
                << re.where << ": " << re.what
                << std::endl;
      r = false;
    } catch (...) {
      cache.close();
      throw;
    }

    cache.close();

    return r;
  }

  void DesiredSymbols::preprocess( void )
  {
    ObjdumpProcessor::objdumpLines_t::iterator fitr;
    ObjdumpProcessor::objdumpLines_t::iterator n, p;
    DesiredSymbols::symbolSet_t::iterator      sitr;
    CoverageMapBase*                           theCoverageMap;

    // Look at each symbol.
    for (sitr = SymbolsToAnalyze->set.begin();
         sitr != SymbolsToAnalyze->set.end();
         sitr++) {

      // If the unified coverage map does not exist, the symbol was
      // never referenced by any executable.  Just skip it.
      theCoverageMap = sitr->second.unifiedCoverageMap;
      if (!theCoverageMap)
        continue;

      // Mark any branch and NOP instructions.
      for (fitr = sitr->second.instructions.begin();
           fitr != sitr->second.instructions.end();
           fitr++) {
        if (fitr->isBranch) {
           theCoverageMap->setIsBranch(
             fitr->address - sitr->second.baseAddress
           );
        }
        if (fitr->isNop) {
           theCoverageMap->setIsNop(
             fitr->address - sitr->second.baseAddress
           );
        }
      }

    }
  }

  void DesiredSymbols::calculateStatistics( void )
  {
    uint32_t                              a;
    uint32_t                              endAddress;
    DesiredSymbols::symbolSet_t::iterator sitr;
    CoverageMapBase*                      theCoverageMap;

    // Look at each symbol.
    for (sitr = SymbolsToAnalyze->set.begin();
         sitr != SymbolsToAnalyze->set.end();
         sitr++) {

      // If the unified coverage map does not exist, the symbol was
      // never referenced by any executable.  Just skip it.
      theCoverageMap = sitr->second.unifiedCoverageMap;
      if (!theCoverageMap)
        continue;

      // Increment the total sizeInBytes byt the bytes in the symbol
      stats.sizeInBytes += sitr->second.stats.sizeInBytes;

      // Now scan through the coverage map of this symbol.
      endAddress = sitr->second.stats.sizeInBytes - 1;
      a = 0;
      while (a <= endAddress) {

        // If we are at the start of instruction increment
        // instruction type counters as needed.
        if ( theCoverageMap->isStartOfInstruction( a ) ) {

          stats.sizeInInstructions++;
          sitr->second.stats.sizeInInstructions++;

          if (!theCoverageMap->wasExecuted( a ) ) {
            stats.uncoveredInstructions++;
            sitr->second.stats.uncoveredInstructions++;

            if ( theCoverageMap->isBranch( a )) {
              stats.branchesNotExecuted++;
              sitr->second.stats.branchesNotExecuted++;
             }
          } else if (theCoverageMap->isBranch( a )) {
            stats.branchesExecuted++;
            sitr->second.stats.branchesExecuted++;
          }

        }

        if (!theCoverageMap->wasExecuted( a )) {
          stats.uncoveredBytes++;
          sitr->second.stats.uncoveredBytes++;
        }
        a++;

      }
    }
  }


  void DesiredSymbols::computeUncovered( void )
  {
    uint32_t                              a, la, ha;
    uint32_t                              endAddress;
    uint32_t                              count;
    DesiredSymbols::symbolSet_t::iterator sitr;
    CoverageRanges*                       theBranches;
    CoverageMapBase*                      theCoverageMap;
    CoverageRanges*                       theRanges;

    // Look at each symbol.
    for (sitr = SymbolsToAnalyze->set.begin();
         sitr != SymbolsToAnalyze->set.end();
         sitr++) {

      // If the unified coverage map does not exist, the symbol was
      // never referenced by any executable.  Just skip it.
      theCoverageMap = sitr->second.unifiedCoverageMap;
      if (!theCoverageMap)
        continue;

      // Create containers for the symbol's uncovered ranges and branches.
      theRanges = new CoverageRanges();
      sitr->second.uncoveredRanges = theRanges;
      theBranches = new CoverageRanges();
      sitr->second.uncoveredBranches = theBranches;

      // Mark NOPs as executed
      endAddress = sitr->second.stats.sizeInBytes - 1;
      a = 0;
      while (a < endAddress) {
        if (!theCoverageMap->wasExecuted( a )) {
          a++;
          continue;
        }

	for (ha=a+1;
	     ha<=endAddress && !theCoverageMap->isStartOfInstruction( ha );
	     ha++)
	  ;
        if ( ha >= endAddress )
          break;

        if (theCoverageMap->isNop( ha ))
          do {
            theCoverageMap->setWasExecuted( ha );
	    ha++;
            if ( ha >= endAddress )
              break;
          } while ( !theCoverageMap->isStartOfInstruction( ha ) );
        a = ha;
      }

      // Now scan through the coverage map of this symbol.
      endAddress = sitr->second.stats.sizeInBytes - 1;
      a = 0;
      while (a <= endAddress) {

        // If an address was NOT executed, find consecutive unexecuted
        // addresses and add them to the uncovered ranges.
        if (!theCoverageMap->wasExecuted( a )) {

          la = a;
          count = 1;
          for (ha=a+1;
               ha<=endAddress && !theCoverageMap->wasExecuted( ha );
               ha++)
          {
            if ( theCoverageMap->isStartOfInstruction( ha ) )
              count++;
          }
          ha--;

          stats.uncoveredRanges++;
          sitr->second.stats.uncoveredRanges++;
          theRanges->add(
            sitr->second.baseAddress + la,
            sitr->second.baseAddress + ha,
            CoverageRanges::UNCOVERED_REASON_NOT_EXECUTED,
            count
          );
          a = ha + 1;
        }

        // If an address is a branch instruction, add any uncovered branches
        // to the uncoverd branches.
        else if (theCoverageMap->isBranch( a )) {
          la = a;
          for (ha=a+1;
               ha<=endAddress && !theCoverageMap->isStartOfInstruction( ha );
               ha++)
            ;
          ha--;

          if (theCoverageMap->wasAlwaysTaken( la )) {
            stats.branchesAlwaysTaken++;
            sitr->second.stats.branchesAlwaysTaken++;
            theBranches->add(
              sitr->second.baseAddress + la,
              sitr->second.baseAddress + ha,
              CoverageRanges::UNCOVERED_REASON_BRANCH_ALWAYS_TAKEN,
              1
            );
            if (Verbose)
              fprintf(
                stderr,
                "Branch always taken found in %s (0x%x - 0x%x)\n",
                (sitr->first).c_str(),
                sitr->second.baseAddress + la,
                sitr->second.baseAddress + ha
              );
          }

          else if (theCoverageMap->wasNeverTaken( la )) {
            stats.branchesNeverTaken++;
            sitr->second.stats.branchesNeverTaken++;
            theBranches->add(
              sitr->second.baseAddress + la,
              sitr->second.baseAddress + ha,
              CoverageRanges::UNCOVERED_REASON_BRANCH_NEVER_TAKEN,
              1
            );
            if (Verbose)
              fprintf(
                stderr,
                "Branch never taken found in %s (0x%x - 0x%x)\n",
                (sitr->first).c_str(),
                sitr->second.baseAddress + la,
                sitr->second.baseAddress + ha
              );
          }
          a = ha + 1;
        }
        else
          a++;
      }
    }
  }


  void DesiredSymbols::createCoverageMap(
    const std::string& exefileName,
    const std::string& symbolName,
    uint32_t           size
  )
  {
    CoverageMapBase*      aCoverageMap;
    uint32_t              highAddress;
    symbolSet_t::iterator itr;

    // Ensure that the symbol is a desired symbol.
    itr = set.find( symbolName );

    if (itr == set.end()) {

      fprintf(
        stderr,
        "ERROR: DesiredSymbols::createCoverageMap - Unable to create "
        "unified coverage map for %s because it is NOT a desired symbol\n",
        symbolName.c_str()
      );
      exit( -1 );
    }

    // If we have already created a coverage map, ...
    if (itr->second.unifiedCoverageMap) {

      // ensure that the specified size matches the existing size.
      if (itr->second.stats.sizeInBytes != size) {

        // Changed ERROR to INFO because size mismatch is not treated as
        // error anymore.
        // Set smallest size as size and continue.
        // Update value for longer byte size.
        // 2015-07-22
        fprintf(
          stderr,
          "INFO: DesiredSymbols::createCoverageMap - Attempt to create "
          "unified coverage maps for %s with different sizes (%s/%d != %s/%d)\n",

          symbolName.c_str(),
          exefileName.c_str(),
          itr->second.stats.sizeInBytes,
          itr->second.sourceFile->getFileName().c_str(),
          size
        );

        if ( itr->second.stats.sizeInBytes < size )
          itr->second.stats.sizeInBytes = size;
        else
          size = itr->second.stats.sizeInBytes;
      }
    }

    // If we don't already have a coverage map, create one.
    else {

      highAddress = size - 1;

      aCoverageMap = new CoverageMap( exefileName, 0, highAddress );
      if (!aCoverageMap) {

        fprintf(
          stderr,
          "ERROR: DesiredSymbols::createCoverageMap - Unable to allocate "
          "coverage map for %s:%s\n",
          exefileName.c_str(),
          symbolName.c_str()
        );
        exit( -1 );
      }

      if ( Verbose )
        fprintf(
          stderr,
          "Created unified coverage map for %s (0x%x - 0x%x)\n",
          symbolName.c_str(), 0, highAddress
        );
      itr->second.unifiedCoverageMap = aCoverageMap;
      itr->second.stats.sizeInBytes = size;
    }
  }

  void DesiredSymbols::determineSourceLines(
    CoverageRanges* const theRanges,
    ExecutableInfo* const theExecutable

  )
  {
    char*                              base;
    char*                              cStatus;
    char                               command[512];
    std::string                        fileName;
    CoverageRanges::ranges_t::iterator ritr;
    char                               rpath[PATH_MAX];
    FILE*                              tmpfile;

    // Open a temporary file for the uncovered ranges.
    tmpfile = fopen( "ranges1.tmp", "w" );
    if ( !tmpfile ) {
      fprintf(
        stderr,
        "ERROR: DesiredSymbols::determineSourceLines - "
        "unable to open %s\n",
        "ranges1.tmp"
      );
      exit(-1);
    }

    // Write the range addresses to the temporary file.
    for (ritr =  theRanges->set.begin();
         ritr != theRanges->set.end();
         ritr++ ) {
      fprintf(
        tmpfile,
        "0x%08x\n0x%08x\n",
        ritr->lowAddress - theExecutable->getLoadAddress(),
        ritr->highAddress - theExecutable->getLoadAddress()
      );
    }

    fclose( tmpfile );

    // Invoke addr2line to generate the source lines for each address.
    if (theExecutable->hasDynamicLibrary())
      fileName = theExecutable->getLibraryName();
    else
      fileName = theExecutable->getFileName();

    sprintf(
      command,
      "%s -Ce %s <%s | dos2unix >%s",
      TargetInfo->getAddr2line(),
      fileName.c_str(),
      "ranges1.tmp",
      "ranges2.tmp"
    );

    if (system( command )) {
      fprintf(
        stderr,
        "ERROR: DesiredSymbols::determineSourceLines - "
        "command (%s) failed\n",
        command
      );
      exit( -1 );
    }

    // Open the addr2line output file.
    tmpfile = fopen( "ranges2.tmp", "r" );
    if ( !tmpfile ) {
      fprintf(
        stderr,
        "ERROR: DesiredSymbols::determineSourceLines - "
        "unable to open %s\n",
        "ranges2.tmp"
      );
      exit(-1);
    }

    // Process the addr2line output.
    for (ritr =  theRanges->set.begin();
         ritr != theRanges->set.end();
         ritr++ ) {

      cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, tmpfile );
      if ( cStatus == NULL ) {
        fprintf(
          stderr,
          "ERROR: DesiredSymbols::determineSourceLines - "
          "Out of sync in addr2line output\n"
        );
        exit( -1 );
      }
      inputBuffer[ strlen(inputBuffer) - 1] = '\0';

      // Use only the base filename without directory path.
#ifdef _WIN32
      #define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
#endif
      realpath( inputBuffer, rpath );
      base = basename( rpath );

      ritr->lowSourceLine = std::string( base );

      cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, tmpfile );
      if ( cStatus == NULL ) {
        fprintf(
          stderr,
          "ERROR: DesiredSymbols::determineSourceLines - "
          "Out of sync in addr2line output\n"
        );
        exit( -1 );
      }
      inputBuffer[ strlen(inputBuffer) - 1] = '\0';

      // Use only the base filename without directory path.
      realpath( inputBuffer, rpath );
      base = basename( rpath );

      ritr->highSourceLine = std::string( base );
    }

    fclose( tmpfile );
    unlink( "ranges1.tmp" );
    unlink( "ranges2.tmp" );
  }

  SymbolInformation* DesiredSymbols::find(
    const std::string& symbolName
  )
  {
    if (set.find( symbolName ) == set.end())
      return NULL;
    else
      return &set[ symbolName ];
  }

  void DesiredSymbols::findSourceForUncovered( void )
  {
    DesiredSymbols::symbolSet_t::iterator ditr;
    CoverageRanges*                       theBranches;
    CoverageRanges*                       theRanges;

    // Process uncovered ranges and/or branches for each symbol.
    for (ditr = SymbolsToAnalyze->set.begin();
         ditr != SymbolsToAnalyze->set.end();
         ditr++) {

      // First the unexecuted ranges, ...
      theRanges = ditr->second.uncoveredRanges;
      if (theRanges == NULL)
        continue;

      if (!theRanges->set.empty()) {
        if (Verbose)
          fprintf(
            stderr,
            "Looking up source lines for uncovered ranges in %s\n",
            (ditr->first).c_str()
          );
        determineSourceLines(
          theRanges,
          ditr->second.sourceFile
        );
      }

      // then the uncovered branches.
      theBranches = ditr->second.uncoveredBranches;
      if (theBranches == NULL)
        continue;

      if (!theBranches->set.empty()) {
        if (Verbose)
          fprintf(
            stderr,
            "Looking up source lines for uncovered branches in %s\n",
            (ditr->first).c_str()
          );
        determineSourceLines(
          theBranches,
          ditr->second.sourceFile
        );
      }
    }
  }

  uint32_t DesiredSymbols::getNumberBranchesAlwaysTaken( void ) const {
    return stats.branchesAlwaysTaken;
  };

  uint32_t DesiredSymbols::getNumberBranchesFound( void ) const {
    return (stats.branchesNotExecuted + stats.branchesExecuted);
  };

  uint32_t DesiredSymbols::getNumberBranchesNeverTaken( void ) const {
    return stats.branchesNeverTaken;
  };

  uint32_t DesiredSymbols::getNumberUncoveredRanges( void ) const {
    return stats.uncoveredRanges;
  };

  bool DesiredSymbols::isDesired (
    const std::string& symbolName
  ) const
  {
    if (set.find( symbolName ) == set.end()) {
      #if 0
        fprintf( stderr,
          "Warning: Unable to find symbol %s\n",
          symbolName.c_str()
        );
      #endif
      return false;
    }
    return true;
  }

  void DesiredSymbols::mergeCoverageMap(
    const std::string&           symbolName,
    const CoverageMapBase* const sourceCoverageMap
  )
  {
    uint32_t              dAddress;
    CoverageMapBase*      destinationCoverageMap;
    uint32_t              dMapSize;
    symbolSet_t::iterator itr;
    uint32_t              sAddress;
    uint32_t              sBaseAddress;
    uint32_t              sMapSize;
    uint32_t              executionCount;

    // Ensure that the symbol is a desired symbol.
    itr = set.find( symbolName );

    if (itr == set.end()) {

      fprintf(
        stderr,
        "ERROR: DesiredSymbols::mergeCoverageMap - Unable to merge "
        "coverage map for %s because it is NOT a desired symbol\n",
        symbolName.c_str()
      );
      exit( -1 );
    }

    // Ensure that the source and destination coverage maps
    // are the same size.
    // Changed from ERROR msg to INFO, because size mismatch is not
    // treated as error anymore. 2015-07-20
    dMapSize = itr->second.stats.sizeInBytes;
    sBaseAddress = sourceCoverageMap->getFirstLowAddress();
    sMapSize = sourceCoverageMap->getSize();
    if (dMapSize != sMapSize) {

      fprintf(
        stderr,
        "INFO: DesiredSymbols::mergeCoverageMap - Unable to merge "
        "coverage map for %s because the sizes are different\n",
        symbolName.c_str()
      );
      return;
    }

    // Merge the data for each address.
    destinationCoverageMap = itr->second.unifiedCoverageMap;

    for (dAddress = 0; dAddress < dMapSize; dAddress++) {

      sAddress = dAddress + sBaseAddress;

      // Merge start of instruction indication.
      if (sourceCoverageMap->isStartOfInstruction( sAddress ))
        destinationCoverageMap->setIsStartOfInstruction( dAddress );

      // Merge the execution data.
      executionCount = sourceCoverageMap->getWasExecuted( sAddress );
      destinationCoverageMap->sumWasExecuted( dAddress, executionCount );

      // Merge the branch data.
      executionCount = sourceCoverageMap->getWasTaken( sAddress );
      destinationCoverageMap->sumWasTaken( dAddress, executionCount );

      executionCount = sourceCoverageMap->getWasNotTaken( sAddress );
      destinationCoverageMap->sumWasNotTaken( dAddress, executionCount );
    }
  }

}