summaryrefslogtreecommitdiffstats
path: root/tester/covoar/GcovData.cc
blob: 397c20d6c35fa94843476b671117c87fa504b5fe (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
/*! @file GcovData.cc
 *  @brief GcovData Implementation
 *
 *  This file contains the implementation of the functions supporting
 *  reading *.gcno and writing *.gcda files for gcov support
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <sys/stat.h>

//#include "app_common.h"
#include "GcovData.h"
//#include "ExecutableInfo.h"
//#include "CoverageMap.h"
//#include "qemu-traces.h"

#include "rtems-utils.h"

namespace Gcov {

  GcovData::GcovData( Coverage::DesiredSymbols& symbolsToAnalyze ):
    numberOfFunctions( 0 ),
    gcnoPreamble(),
    symbolsToAnalyze_m( symbolsToAnalyze )
  {
  }

  GcovData::~GcovData()
  {
  }

  bool GcovData::readGcnoFile( const std::string& fileName )
  {
    int           status;
    std::ifstream gcovFile;
    std::string   tempString;
    std::string   tempString2;
    std::string   tempString3;
    size_t        index;

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

    gcnoFileName = fileName;
    gcdaFileName = fileName;
    textFileName = fileName;
    cFileName    = fileName;
    tempString   = gcdaFileName;
    tempString2  = textFileName;
    tempString3  = cFileName;

    index = tempString.find( ".gcno" );
    if ( index == std::string::npos ) {
      std::cerr << "ERROR: incorrect name of *.gcno file" << std::endl;
      return false;
    } else {
      // construct gcda file name
      tempString = tempString.replace( index, strlen( ".gcno" ), ".gcda" );

      // construct report file name
      tempString2 = tempString2.replace( index, strlen( ".gcno" ), ".txt" );

      // construct source file name
      tempString3 = tempString3.replace( index, strlen( ".gcno" ), ".c" );
    }

    // Debug message
    // std::cerr << "Reading file: " << gcnoFileName << std::endl;

    // Open the notes file.
    gcovFile.open( gcnoFileName );
    if ( !gcovFile ) {
      std::cerr << "Unable to open " << gcnoFileName << std::endl;
      return false;
    }

    // Read and validate the gcnoPreamble (magic, version, timestamp) from the file
    status = readFilePreamble( &gcnoPreamble, gcovFile, GCNO_MAGIC );
    if ( status <= 0 ) {
      std::cerr << "Unable to read " << gcnoFileName << std::endl;
      return false;
    }

    //Read all remaining frames from file
    while( readFrame( gcovFile ) ) {}

    return true;
  }


  bool GcovData::writeGcdaFile()
  {
    gcov_preamble           preamble;
    gcov_frame_header       header;
    std::ofstream           gcdaFile;
    functions_iterator_t    currentFunction;
    arcs_iterator_t         currentArc;
    uint32_t                buffer;
    uint32_t                countersFound;
    uint32_t                countersFoundSum;
    uint64_t                countersSum;
    uint64_t                countersMax;
    uint64_t                llBuffer[4096];    // TODO: Use common buffer
    gcov_statistics         objectStats;
    gcov_statistics         programStats;
    std::ofstream::pos_type bytes_before;

    // Debug message
    //std::cerr << "Writing file: " <<  gcdaFileName << std::endl;

    // Lets clear counters sumators
    countersSum      = 0;
    countersMax      = 0;
    countersFoundSum = 0;

    // Open the data file.
    gcdaFile.open( gcdaFileName );
    if ( !gcdaFile ) {
      std::cerr << "Unable to create " << gcdaFileName << std::endl;
      return false;
    }

    //Form preamble
    preamble.magic     = GCDA_MAGIC;
    preamble.version   = gcnoPreamble.version;
    preamble.timestamp = gcnoPreamble.timestamp;

    //Write preamble
    gcdaFile.write( (char *) &preamble , sizeof( preamble ) );
    if ( gcdaFile.fail() ) {
      std::cerr << "Error while writing gcda preamble to a file "
                << gcdaFileName << std::endl;
    }

    //Write function info and counter counts
    for (
      currentFunction = functions.begin();
      currentFunction != functions.end();
      currentFunction++
    ) {
      //Write function announcement frame header (length always equals 2)
      header.tag = GCOV_TAG_FUNCTION;
      header.length = 2;
      gcdaFile.write( (char *) &header, sizeof( header ) );
      if ( gcdaFile.fail() ) {
        std::cerr << "Error while writing function announcement to a file "
                  << gcdaFileName << std::endl;
      }

      //Write function id
      buffer = (*currentFunction).getId();
      gcdaFile.write( (char *) &buffer, sizeof( buffer ) );
      if ( gcdaFile.fail() ) {
        std::cerr << "Error while writing function id to a file "
                  << gcdaFileName << std::endl;
      }

      //Write function checksum
      buffer = (*currentFunction).getChecksum();
      gcdaFile.write( (char *) &buffer, sizeof( buffer ) );
      if ( gcdaFile.fail() ) {
        std::cerr << "Error while writing function checksum to a file "
                  << gcdaFileName << std::endl;
      }

      // Determine how many counters there are
      // and store their counts in buffer
      countersFound = 0;
      (*currentFunction).getCounters(
        llBuffer,
        countersFound,
        countersSum,
        countersMax
      );
      countersFoundSum += countersFound;

      //Write info about counters
      header.tag = GCOV_TAG_COUNTER;
      header.length = countersFound * 2;
      gcdaFile.write( (char *) &header, sizeof( header ) );
      if ( gcdaFile.fail() ) {
        std::cerr << "Error while writing counter header to a file "
                  << gcdaFileName << std::endl;
      }

      bytes_before = gcdaFile.tellp();

      gcdaFile.write( (char *) llBuffer, sizeof( uint64_t ) * countersFound );
      if ( gcdaFile.tellp() - bytes_before != countersFound ) {
        std::cerr << "Error while writing counter data to a file "
                  << gcdaFileName << std::endl;
      }
    }

    // Prepare frame with object file statistics
    header.tag = GCOV_TAG_OBJECT_SUMMARY;
    header.length = 9;
    objectStats.checksum = 0;      // TODO: have no idea hov to calculates it :)
    objectStats.counters = countersFoundSum;
    objectStats.runs = 1;        // We are lying for now, we have no means of figuring this out
    objectStats.sum = countersSum;    // Sum of all counters
    objectStats.max = countersMax;    // max value for counter on last run, we have no clue
    objectStats.sumMax = countersMax;    // we have no clue

    // Write data
    gcdaFile.write( (char *) &header, sizeof( header ) );
    if ( gcdaFile.fail() ) {
      std::cerr << "Error while writing stats header to a file "
                << gcdaFileName << std::endl;
    }

    gcdaFile.write( (char *) &objectStats, sizeof( objectStats ) );
    if ( gcdaFile.fail() ) {
      std::cerr << "Error while writing object stats to a file "
                << gcdaFileName << std::endl;
    }


    // Prepare frame with program statistics
    header.tag = GCOV_TAG_PROGRAM_SUMMARY;
    header.length = 9;
    programStats.checksum = 0;      // TODO: have no idea hov to calculate it :)
    programStats.counters = countersFoundSum;
    programStats.runs = 1;      // We are lying for now, we have no clue
    programStats.sum = countersSum;    // Sum of all counters
    programStats.max = countersMax;    // max value for counter on last run, we have no clue
    programStats.sumMax = countersMax;    // we have no clue

    // Write data
    gcdaFile.write( (char *) &header, sizeof( header ) );
    if ( gcdaFile.fail() ) {
      std::cerr << "Error while writing stats header to a file "
                << gcdaFileName << std::endl;
    }

    gcdaFile.write( (char *) &programStats, sizeof( programStats ) );
    if ( gcdaFile.fail() ) {
      std::cerr << "Error while writing program stats to a file "
                << gcdaFileName << std::endl;
    }

    return true;
  }

  bool GcovData::readFrame( std::ifstream& gcovFile )
  {
    gcov_frame_header header;
    char              buffer[512];
    char              intBuffer[16384];
    uint32_t          tempBlockId;
    blocks_iterator_t tempBlockIterator;
    int               status;

    status = readFrameHeader( &header, gcovFile );
    if ( status <= 0 ) {
      // Not printing error message because this
      // happenns at the end of each file
      return false;
    }

    switch ( header.tag ) {

      case GCOV_TAG_FUNCTION:

        {
          numberOfFunctions++;
          GcovFunctionData newFunction;

          if ( !readFunctionFrame(header, gcovFile, &newFunction) ) {
            std::cerr << "Error while reading FUNCTION from gcov file..."
                      << std::endl;
            return false;
          }

          functions.push_back( newFunction );
        }

        break;

      case GCOV_TAG_BLOCKS:

        gcovFile.read( intBuffer, header.length );
        if ( gcovFile.gcount() != (int) header.length ) {
          std::cerr << "Error while reading BLOCKS from gcov file..."
                    << std::endl
                    << "Header length is " << header.length
                    << " instead of " << gcovFile.gcount()
                    << std::endl;
          return false;
        }

        for( uint32_t i = 0; i < header.length; i++ ) {
          functions.back().addBlock( i, intBuffer[i], "" );
        }

        break;

      case GCOV_TAG_ARCS:

        gcovFile.read( intBuffer, header.length );
        if ( gcovFile.gcount() != (int) header.length ) {
          return false;
        }

        for ( int i = 1; i < (int) header.length; i += 2 ) {
          functions.back().addArc(intBuffer[0], intBuffer[i], intBuffer[i+1]);
        }

        break;

      case GCOV_TAG_LINES:

        gcovFile.read( intBuffer, 2 );
        if ( gcovFile.gcount() != 2 || intBuffer[1] != 0 ) {
          std::cerr << "Error while reading block id for LINES from gcov "
                    << "file..." << std::endl;
          return false;
        }
        tempBlockId = intBuffer[0];
        header.length -= 2;

        // Find the right block
        tempBlockIterator =functions.back().findBlockById( tempBlockId );

        header.length -= readString( buffer, gcovFile );
        functions.back().setBlockFileName( tempBlockIterator, buffer );

        gcovFile.read( intBuffer, header.length );
        if ( gcovFile.gcount() != (int) header.length ) {
          std::cerr << "Error while reading LINES from gcov file..."
                    << std::endl;
          return false;
        } else {
          for ( int i = 0; i < (int) (header.length - 2); i++ ) {
            functions.back().addBlockLine( tempBlockIterator, intBuffer[i] );
          }
        }

        break;

      default:

        std::cerr << std::endl << std::endl
                  << "ERROR - encountered unknown *.gcno tag : 0x"
                  << std::hex << header.tag << std::dec << std::endl;
        break;
      }

      return true;
  }

  int GcovData::readString( char* buffer, std::ifstream& gcovFile )
  {
    int length;

    gcovFile.read( (char *) &length, sizeof( int ) );
    if ( gcovFile.gcount() != sizeof( int ) ) {
      std::cerr << "ERROR: Unable to read string length from gcov file"
                << std::endl;
      return -1;
    }

    gcovFile.read( buffer, length * 4 );
    if ( gcovFile.gcount() != length * 4 ) {
      std::cerr << "ERROR: Unable to read string from gcov file"
                << std::endl;
      return -1;
    }

    buffer[length * 4] = '\0';

    return length +1;
  }

  int GcovData::readFrameHeader(
    gcov_frame_header* header,
    std::ifstream&     gcovFile
  )
  {
    int length;

    length = sizeof( gcov_frame_header );
    gcovFile.read( (char *) header, length );
    if ( gcovFile.gcount() != length ) {
      std::cerr << "ERROR: Unable to read frame header from gcov file"
                << std::endl;
      return -1;
    }

      return length / 4;
  }

  int GcovData::readFilePreamble(
     gcov_preamble* preamble,
     std::ifstream& gcovFile,
     uint32_t       desiredMagic
  )
  {
    rtems::utils::ostream_guard old_state( std::cerr );

    // Read the gcov preamble and make sure it is the right length and has the
    // magic number
    gcovFile.read( (char *) preamble, sizeof( gcov_preamble ) );
    if ( gcovFile.gcount() != sizeof( gcov_preamble ) ) {
      std::cerr << "Error while reading file preamble" << std::endl;
      return -1;
    }

    if ( preamble->magic != GCNO_MAGIC ) {
      std::cerr << "File is not a valid *.gcno output (magic: 0x"
                << std::hex << std::setw( 4 ) << preamble->magic
                << ")" << std::endl;
      return -1;
    }

    return sizeof( gcov_preamble ) / 4;
  }

  bool GcovData::readFunctionFrame(
    gcov_frame_header header,
    std::ifstream&    gcovFile,
    GcovFunctionData* function
  )
  {
    char buffer[512];
    char intBuffer[16384];

    gcovFile.read( (char *) intBuffer, 8 );
    if ( gcovFile.gcount() != 8 ) {
      std::cerr << "ERROR: Unable to read Function ID & checksum" << std::endl;
      return false;
    }

    header.length -= 2;
    function->setId( intBuffer[0] );
    function->setChecksum( intBuffer[1] );

    header.length -= readString( buffer, gcovFile );
    function->setFunctionName( buffer, symbolsToAnalyze_m );
    header.length -= readString( buffer, gcovFile );
    function->setFileName( buffer );
    gcovFile.read( (char*) intBuffer, 4 * header.length );
    if (gcovFile.gcount() != 4 * header.length ) {
      std::cerr << "ERROR: Unable to read Function starting line number"
                << std::endl;
      return false;
    }

    function->setFirstLineNumber( intBuffer[0] );

    return true;
  }

  bool GcovData::writeReportFile()
  {
    functions_iterator_t currentFunction;
    uint32_t             i = 1;          //iterator
    std::ofstream        textFile;

    // Debug message
    // std::cerr << "Writing file: " << textFileName << std::endl;

    // Open the data file.
    textFile.open( textFileName );
    if ( !textFile.is_open() ) {
      std::cerr << "Unable to create " << textFileName << std::endl;
      return false;
    }

    printGcnoFileInfo( textFile );

    for (
      currentFunction = functions.begin();
      currentFunction != functions.end();
      currentFunction++
    ) {
      (*currentFunction).printFunctionInfo( textFile, i );
      (*currentFunction).printCoverageInfo( textFile, i );
      i++;
    }

    return true;
  }

  void GcovData::printGcnoFileInfo( std::ofstream& textFile )
  {
    textFile << std::endl << "FILE:\t\t\t" << gcnoFileName
             << std::endl << std::hex
             << "magic:\t\t\t" << gcnoPreamble.magic << std::endl
             << "version:\t\t" << gcnoPreamble.version << std::endl
             << "timestamp:\t\t" << gcnoPreamble.timestamp << std::endl
             << std::dec
             << "functions found: \t"
             << std::endl << std::endl << gcnoPreamble.timestamp
             << std::endl << std::endl;
  }

  void GcovData::writeGcovFile()
  {
    //std::cerr << "Attempting to run gcov for: " << cFileName << std::endl;
    std::ostringstream command;

    command << "(  cd " << rld::path::dirname( cFileName )
            << " && gcov " << rld::path::basename( cFileName )
            << " &>> gcov.log)";
    //std::cerr << "> " << command << std::endl;
    system( command.str().c_str() );
  }

  bool GcovData::processCounters()
  {
    functions_iterator_t currentFunction;
    bool                 status = true;

    for (
      currentFunction = functions.begin();
      currentFunction != functions.end();
      currentFunction++
    ) {
      if ( !(*currentFunction).processFunctionCounters() ) {
        status = false;
      }
    }

    return status;
  }
}