source: rtems-tools/tester/covoar/covoar.cc @ 99c90b3

Last change on this file since 99c90b3 was 99c90b3, checked in by Chris Johns <chrisj@…>, on Aug 5, 2018 at 11:41:08 PM

tester/covoar: Integrate DWARF function data.

Use DAWRF function data to create the executable coverage
maps. Integrate the existing objdump processing with this
data.

  • Refactor CoverageMapBase? to have the address ranges and address info as separate objects. Move the to address info into a vector. Add support for multiple address ranges.
  • DesiredSymbols? is only interested in function symbols.
  • ExecutableInfo? creates coverage maps from DWARF function data.
  • Add warning flags to the covoar build.
  • Varous C++11 refactoring.
  • Property mode set to 100644
File size: 17.4 KB
Line 
1#include <iostream>
2#include <iomanip>
3
4#include <cxxabi.h>
5#include <ctype.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <limits.h>
9#include <signal.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <unistd.h>
16
17#include <list>
18
19#include <rld.h>
20#include <rld-process.h>
21
22#include "app_common.h"
23#include "CoverageFactory.h"
24#include "CoverageMap.h"
25#include "DesiredSymbols.h"
26#include "ExecutableInfo.h"
27#include "Explanations.h"
28#include "ObjdumpProcessor.h"
29#include "ReportsBase.h"
30#include "TargetFactory.h"
31#include "GcovData.h"
32
33#if defined(_WIN32) || defined(__CYGWIN__)
34  #define kill(p,s) raise(s)
35#endif
36
37typedef std::list<std::string> CoverageNames;
38typedef std::list<Coverage::ExecutableInfo*> Executables;
39typedef std::string option_error;
40
41/*
42 * Create a build path from the executable paths. Also extract the build prefix
43 * and BSP names.
44 */
45static void createBuildPath(Executables& executablesToAnalyze,
46                            std::string& buildPath,
47                            std::string& buildPrefix,
48                            std::string& buildBSP)
49{
50  for (const auto& exe : executablesToAnalyze) {
51    rld::strings eparts;
52    rld::split(eparts, rld::path::path_abs(exe->getFileName()), RLD_PATH_SEPARATOR);
53    std::string fail; // empty means all is OK else an error string
54    for (rld::path::paths::reverse_iterator pri = eparts.rbegin();
55         pri != eparts.rend();
56         ++pri) {
57      if (*pri == "testsuites") {
58        ++pri;
59        if (pri == eparts.rend()) {
60          fail = "invalid executable path, no BSP";
61          break;
62        }
63        if (buildBSP.empty()) {
64          buildBSP = *pri;
65        } else {
66          if (buildBSP != *pri) {
67            fail = "executable BSP does not match: " + buildBSP;
68            break;
69          }
70        }
71        ++pri;
72        if (pri == eparts.rend() || *pri != "c") {
73          fail = "invalid executable path, no 'c'";
74          break;
75        }
76        ++pri;
77        if (pri == eparts.rend()) {
78          fail = "invalid executable path, no arch prefix";
79          break;
80        }
81        if (buildPrefix.empty()) {
82          buildPrefix = *pri;
83        } else {
84          if (buildPrefix != *pri) {
85            fail = "executable build prefix does not match: " + buildPrefix;
86            break;
87          }
88        }
89        ++pri;
90        if (pri == eparts.rend()) {
91          fail = "invalid executable path, no build top";
92          break;
93        }
94        //
95        // The remaining parts of the path is the build path. Iterator over them
96        // and collect into a new paths variable to join to make a path.
97        //
98        rld::path::paths bparts;
99        for (; pri != eparts.rend(); ++pri)
100          bparts.insert(bparts.begin(), *pri);
101        std::string thisBuildPath;
102        rld::path::path_join(thisBuildPath, bparts, thisBuildPath);
103        if (buildPath.empty()) {
104          buildPath = thisBuildPath;
105        } else {
106          if (buildPath != thisBuildPath) {
107            fail = "executable build path does not match: " + buildPath;
108          }
109        }
110        break;
111      }
112    }
113    if (!fail.empty()) {
114      throw rld::error( fail, "createBuildPath" );
115    }
116  }
117}
118
119/*
120 *  Print program usage message
121 */
122void usage(const std::string& progname)
123{
124  std::cerr <<"Usage: " << progname
125            <<" [-v] -T TARGET -f FORMAT [-E EXPLANATIONS] -1 EXECUTABLE coverage1 ... coverageN" << std::endl
126            << "--OR--" << std::endl
127            << "Usage: " << progname
128            << " [-v] -T TARGET -f FORMAT [-E EXPLANATIONS] -e EXE_EXTENSION -c COVERAGEFILE_EXTENSION EXECUTABLE1 ... EXECUTABLE2" << std::endl
129            << std::endl
130            << "  -v                        - verbose at initialization" << std::endl
131            << "  -T TARGET                 - target name" << std::endl
132            << "  -f FORMAT                 - coverage file format (RTEMS, QEMU, TSIM or Skyeye)" << std::endl
133            << "  -E EXPLANATIONS           - name of file with explanations" << std::endl
134            << "  -S SYMBOL_SET_FILE        - path to the INI format symbol sets" << std::endl
135            << "  -1 EXECUTABLE             - name of executable to get symbols from" << std::endl
136            << "  -e EXE_EXTENSION          - extension of the executables to analyze" << std::endl
137            << "  -c COVERAGEFILE_EXTENSION - extension of the coverage files to analyze" << std::endl
138            << "  -g GCNOS_LIST             - name of file with list of *.gcno files" << std::endl
139            << "  -p PROJECT_NAME           - name of the project" << std::endl
140            << "  -C ConfigurationFileName  - name of configuration file" << std::endl
141            << "  -O Output_Directory       - name of output directory (default=." << std::endl
142            << "  -d debug                  - disable cleaning of tempfile" << std::endl
143            << std::endl;
144}
145
146int covoar(
147  int    argc,
148  char** argv
149)
150{
151  CoverageNames                 coverageFileNames;
152  std::string                   coverageFileName;
153  Executables                   executablesToAnalyze;
154  Coverage::ExecutableInfo*     executableInfo = NULL;
155  std::string                   executableExtension = "exe";
156  std::string                   coverageExtension = "cov";
157  Coverage::CoverageFormats_t   coverageFormat = Coverage::COVERAGE_FORMAT_QEMU;
158  Coverage::CoverageReaderBase* coverageReader = NULL;
159  char*                         executable = NULL;
160  const char*                   explanations = NULL;
161  const char*                   gcnosFileName = NULL;
162  char                          gcnoFileName[FILE_NAME_LENGTH];
163  char                          gcdaFileName[FILE_NAME_LENGTH];
164  char                          gcovBashCommand[256];
165  std::string                   target;
166  const char*                   format = "html";
167  FILE*                         gcnosFile = NULL;
168  Gcov::GcovData*               gcovFile;
169  const char*                   singleExecutable = NULL;
170  rld::process::tempfile        objdumpFile( ".dmp" );
171  rld::process::tempfile        err( ".err" );
172  rld::process::tempfile        syms( ".syms" );
173  bool                          debug = false;
174  std::string                   symbolSet;
175  std::string                   option;
176  int                           opt;
177
178  //
179  // Process command line options.
180  //
181
182  while ((opt = getopt(argc, argv, "1:L:e:c:g:E:f:s:S:T:O:p:vd")) != -1) {
183    switch (opt) {
184      case '1': singleExecutable    = optarg; break;
185      case 'L': dynamicLibrary      = optarg; break;
186      case 'e': executableExtension = optarg; break;
187      case 'c': coverageExtension   = optarg; break;
188      case 'g': gcnosFileName       = optarg; break;
189      case 'E': explanations        = optarg; break;
190      case 'f': format              = optarg; break;
191      case 'S': symbolSet           = optarg; break;
192      case 'T': target              = optarg; break;
193      case 'O': outputDirectory     = optarg; break;
194      case 'v': Verbose             = true;
195                rld::verbose_inc ();          break;
196      case 'p': projectName         = optarg; break;
197      case 'd': debug               = true;   break;
198      default: /* '?' */
199        throw option_error( "unknown option" );
200    }
201  }
202
203  /*
204   * Validate inputs.
205   */
206
207  /*
208   * Validate that we have a symbols of interest file.
209   */
210  if ( symbolSet.empty() )
211    throw option_error( "symbol set file -S" );
212
213  /*
214   * Has path to explanations.txt been specified.
215   */
216  if ( !explanations )
217    throw option_error( "explanations -E" );
218
219  /*
220   * Check for project name.
221   */
222  if ( !projectName )
223    throw option_error( "project name -p" );
224
225  // If a single executable was specified, process the remaining
226  // arguments as coverage file names.
227  if (singleExecutable) {
228
229    // Ensure that the executable is readable.
230    if (!FileIsReadable( singleExecutable )) {
231      std::cerr << "warning: Unable to read executable: " << singleExecutable
232                << std::endl;
233    } else {
234
235      for (int i = optind; i < argc; i++) {
236        // Ensure that the coverage file is readable.
237        if (!FileIsReadable( argv[i] )) {
238          std::cerr << "warning: Unable to read coverage file: " << argv[i]
239                    << std::endl;
240        } else {
241          coverageFileNames.push_back( argv[i] );
242        }
243      }
244
245      // If there was at least one coverage file, create the
246      // executable information.
247      if (!coverageFileNames.empty()) {
248        if (dynamicLibrary) {
249          executableInfo = new Coverage::ExecutableInfo(
250            singleExecutable, dynamicLibrary, Verbose
251          );
252        } else {
253          executableInfo = new Coverage::ExecutableInfo(
254            singleExecutable, nullptr, Verbose
255          );
256        }
257
258        executablesToAnalyze.push_back( executableInfo );
259      }
260    }
261  }
262  else {
263    // If not invoked with a single executable, process the remaining
264    // arguments as executables and derive the coverage file names.
265    for (int i = optind; i < argc; i++) {
266      // Ensure that the executable is readable.
267      if (!FileIsReadable( argv[i] )) {
268        std::cerr << "warning: Unable to read executable: " << argv[i] << std::endl;
269      } else {
270        coverageFileName = argv[i];
271        coverageFileName.append( "." + coverageExtension );
272
273        if (!FileIsReadable( coverageFileName.c_str() )) {
274          std::cerr << "warning: Unable to read coverage file: " << coverageFileName
275                    << std::endl;
276        } else {
277          executableInfo = new Coverage::ExecutableInfo(
278            argv[i], nullptr, Verbose
279          );
280          executablesToAnalyze.push_back( executableInfo );
281          coverageFileNames.push_back( coverageFileName );
282        }
283      }
284    }
285  }
286
287  // Ensure that there is at least one executable to process.
288  if (executablesToAnalyze.empty())
289    throw rld::error( "No information to analyze", "covoar" );
290
291  // The executablesToAnalyze and coverageFileNames containers need
292  // to be the name size of some of the code below breaks. Lets
293  // check and make sure.
294  if (executablesToAnalyze.size() != coverageFileNames.size())
295    throw rld::error( "executables and coverage name size mismatch", "covoar" );
296
297  //
298  // Find the top of the BSP's build tree and if we have found the top
299  // check the executable is under the same path and BSP.
300  //
301  std::string buildPath;
302  std::string buildTarget;
303  std::string buildBSP;
304  createBuildPath(executablesToAnalyze,
305                  buildPath,
306                  buildTarget,
307                  buildBSP);
308
309  //
310  // Use a command line target if provided.
311  //
312  if (!target.empty()) {
313    buildTarget = target;
314  }
315
316  if (Verbose) {
317    if (singleExecutable) {
318      std::cerr << "Processing a single executable and multiple coverage files"
319                << std::endl;
320    } else {
321      std::cerr << "Processing multiple executable/coverage file pairs" << std::endl;
322    }
323    std::cerr << "Coverage Format : " << format << std::endl
324              << "Target          : " << buildTarget.c_str() << std::endl
325              << std::endl;
326
327    // Process each executable/coverage file pair.
328    Executables::iterator eitr = executablesToAnalyze.begin();
329    for (const auto& cname : coverageFileNames) {
330      std::cerr << "Coverage file " << cname
331                << " for executable: " << (*eitr)->getFileName() << std::endl;
332      if (!singleExecutable)
333        eitr++;
334    }
335  }
336
337  //
338  // Create data to support analysis.
339  //
340
341  // Create data based on target.
342  TargetInfo = Target::TargetFactory( buildTarget );
343
344  // Create the set of desired symbols.
345  SymbolsToAnalyze = new Coverage::DesiredSymbols();
346
347  //
348  // Read symbol configuration file and load needed symbols.
349  //
350  SymbolsToAnalyze->load( symbolSet, buildTarget, buildBSP, Verbose );
351
352  if ( Verbose )
353    std::cerr << "Analyzing " << SymbolsToAnalyze->set.size()
354              << " symbols" << std::endl;
355
356  // Create explanations.
357  AllExplanations = new Coverage::Explanations();
358  if ( explanations )
359    AllExplanations->load( explanations );
360
361  // Create coverage map reader.
362  coverageReader = Coverage::CreateCoverageReader(coverageFormat);
363  if (!coverageReader)
364    throw rld::error( "Unable to create coverage file reader", "covoar" );
365
366  // Create the objdump processor.
367  objdumpProcessor = new Coverage::ObjdumpProcessor();
368
369  // Prepare each executable for analysis.
370  for (auto& exe : executablesToAnalyze) {
371    if (Verbose)
372      std::cerr << "Extracting information from: " << exe->getFileName()
373                << std::endl;
374
375    // If a dynamic library was specified, determine the load address.
376    if (dynamicLibrary) {
377      exe->setLoadAddress( objdumpProcessor->determineLoadAddress( exe ) );
378    }
379
380    // Load the objdump for the symbols in this executable.
381    objdumpProcessor->load( exe, objdumpFile, err );
382  }
383
384  //
385  // Analyze the coverage data.
386  //
387
388  // Process each executable/coverage file pair.
389  Executables::iterator eitr = executablesToAnalyze.begin();
390  for (const auto& cname : coverageFileNames) {
391    Coverage::ExecutableInfo* exe = *eitr;
392    if (Verbose)
393      std::cerr << "Processing coverage file " << cname
394                << " for executable " << exe->getFileName()
395                << std::endl;
396
397    // Process its coverage file.
398    coverageReader->processFile( cname.c_str(), exe );
399
400    // Merge each symbols coverage map into a unified coverage map.
401    exe->mergeCoverage();
402
403    // DEBUG Print ExecutableInfo content
404    //exe->dumpExecutableInfo();
405
406    if (!singleExecutable) {
407      eitr++;
408    }
409  }
410
411  // Do necessary preprocessing of uncovered ranges and branches
412  if (Verbose)
413    std::cerr << "Preprocess uncovered ranges and branches" << std::endl;
414
415  SymbolsToAnalyze->preprocess();
416
417  //
418  // Generate Gcov reports
419  //
420  if (gcnosFileName) {
421    if (Verbose)
422      std::cerr << "Generating Gcov reports..." << std::endl;
423
424    gcnosFile = fopen ( gcnosFileName , "r" );
425
426    if ( !gcnosFile )
427      std::cerr << "Unable to open " << gcnosFileName << std::endl;
428    else {
429      while ( fscanf( gcnosFile, "%s", inputBuffer ) != EOF) {
430        gcovFile = new Gcov::GcovData();
431        strcpy( gcnoFileName, inputBuffer );
432
433        if ( Verbose )
434          std::cerr << "Processing file: " << gcnoFileName << std::endl;
435
436        if ( gcovFile->readGcnoFile( gcnoFileName ) ) {
437          // Those need to be in this order
438          gcovFile->processCounters();
439          gcovFile->writeReportFile();
440          gcovFile->writeGcdaFile();
441          gcovFile->writeGcovFile();
442        }
443
444        delete gcovFile;
445      }
446      fclose( gcnosFile );
447    }
448  }
449
450  // Determine the uncovered ranges and branches.
451  if (Verbose)
452    std::cerr << "Computing uncovered ranges and branches" << std::endl;
453
454  SymbolsToAnalyze->computeUncovered();
455
456  // Calculate remainder of statistics.
457  if (Verbose)
458    std::cerr << "Calculate statistics" << std::endl;
459
460  SymbolsToAnalyze->calculateStatistics();
461
462  // Look up the source lines for any uncovered ranges and branches.
463  if (Verbose)
464    std::cerr << "Looking up source lines for uncovered ranges and branches"
465              << std::endl;
466
467  SymbolsToAnalyze->findSourceForUncovered();
468
469  //
470  // Report the coverage data.
471  //
472  if (Verbose)
473    std::cerr << "Generate Reports" << std::endl;
474
475  Coverage::GenerateReports();
476
477  // Write explanations that were not found.
478  if ( explanations ) {
479    std::string notFound;
480
481    notFound = outputDirectory;
482    notFound += "/";
483    notFound += "ExplanationsNotFound.txt";
484
485    if (Verbose)
486      std::cerr << "Writing Not Found Report (" << notFound<< ')' << std::endl;
487
488    AllExplanations->writeNotFound( notFound.c_str() );
489  }
490
491  //Leave tempfiles around if debug flag (-d) is enabled.
492  if ( debug ) {
493    objdumpFile.override( "objdump_file" );
494    objdumpFile.keep();
495    err.override( "objdump_exec_log" );
496    err.keep();
497    syms.override( "symbols_list" );
498    syms.keep();
499  }
500
501  return 0;
502}
503
504#define PrintableString(_s) \
505((!(_s)) ? "NOT SET" : (_s))
506
507static void
508fatal_signal( int signum )
509{
510  signal( signum, SIG_DFL );
511
512  rld::process::temporaries_clean_up();
513
514  /*
515   * Get the same signal again, this time not handled, so its normal effect
516   * occurs.
517   */
518  kill( getpid(), signum );
519}
520
521static void
522setup_signals( void )
523{
524  if ( signal( SIGINT, SIG_IGN ) != SIG_IGN )
525    signal( SIGINT, fatal_signal );
526#ifdef SIGHUP
527  if ( signal( SIGHUP, SIG_IGN ) != SIG_IGN )
528    signal( SIGHUP, fatal_signal );
529#endif
530  if ( signal( SIGTERM, SIG_IGN ) != SIG_IGN )
531    signal( SIGTERM, fatal_signal );
532#ifdef SIGPIPE
533  if ( signal( SIGPIPE, SIG_IGN ) != SIG_IGN )
534    signal( SIGPIPE, fatal_signal );
535#endif
536#ifdef SIGCHLD
537  signal( SIGCHLD, SIG_DFL );
538#endif
539}
540
541void
542unhandled_exception (void)
543{
544  std::cerr << "error: exception handling error, please report" << std::endl;
545  exit (1);
546}
547
548int main(
549  int    argc,
550  char** argv
551)
552{
553  std::string progname( argv[0] );
554  int         ec = 0;
555
556  setup_signals();
557
558  std::set_terminate(unhandled_exception);
559
560  try
561  {
562    progname = rld::path::basename(argv[0]);
563    covoar( argc, argv );
564  }
565  catch ( option_error oe )
566  {
567    std::cerr << "error: missing option: " + oe << std::endl;
568    usage(progname);
569    ec = EXIT_FAILURE;
570  }
571  catch (rld::error re)
572  {
573    std::cerr << "error: "
574              << re.where << ": " << re.what
575              << std::endl;
576    ec = 10;
577  }
578  catch (std::exception e)
579  {
580    rld::output_std_exception(e, std::cerr);
581    ec = 11;
582  }
583  catch (...)
584  {
585    /*
586     * Helps to know if this happens.
587     */
588    std::cerr << "error: unhandled exception" << std::endl;
589    ec = 12;
590  }
591
592  return ec;
593}
Note: See TracBrowser for help on using the repository browser.