Coverage for /Syzygy/grinder/grinders/coverage_grinder.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
73.6%64870.C++source

Line-by-line coverage:

   1    :  // Copyright 2012 Google Inc. All Rights Reserved.
   2    :  //
   3    :  // Licensed under the Apache License, Version 2.0 (the "License");
   4    :  // you may not use this file except in compliance with the License.
   5    :  // You may obtain a copy of the License at
   6    :  //
   7    :  //     http://www.apache.org/licenses/LICENSE-2.0
   8    :  //
   9    :  // Unless required by applicable law or agreed to in writing, software
  10    :  // distributed under the License is distributed on an "AS IS" BASIS,
  11    :  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12    :  // See the License for the specific language governing permissions and
  13    :  // limitations under the License.
  14    :  
  15    :  #include "syzygy/grinder/grinders/coverage_grinder.h"
  16    :  
  17    :  #include "base/string_util.h"
  18    :  #include "base/files/file_path.h"
  19    :  #include "syzygy/common/indexed_frequency_data.h"
  20    :  #include "syzygy/grinder/cache_grind_writer.h"
  21    :  #include "syzygy/grinder/lcov_writer.h"
  22    :  #include "syzygy/pdb/pdb_reader.h"
  23    :  #include "syzygy/pdb/pdb_util.h"
  24    :  #include "syzygy/pe/find.h"
  25    :  
  26    :  namespace grinder {
  27    :  namespace grinders {
  28    :  
  29    :  using basic_block_util::ModuleInformation;
  30    :  using basic_block_util::RelativeAddressRange;
  31    :  using basic_block_util::GetFrequency;
  32    :  using basic_block_util::LoadPdbInfo;
  33    :  using basic_block_util::IsValidFrequencySize;
  34    :  using basic_block_util::PdbInfo;
  35    :  using basic_block_util::PdbInfoMap;
  36    :  using trace::parser::AbsoluteAddress64;
  37    :  
  38    :  CoverageGrinder::CoverageGrinder()
  39    :      : parser_(NULL),
  40    :        event_handler_errored_(false),
  41  E :        output_format_(kLcovFormat) {
  42  E :  }
  43    :  
  44  E :  CoverageGrinder::~CoverageGrinder() {
  45  E :  }
  46    :  
  47  E :  bool CoverageGrinder::ParseCommandLine(const CommandLine* command_line) {
  48    :    // If the switch isn't present we have nothing to do!
  49  E :    const char kOutputFormat[] = "output-format";
  50  E :    if (!command_line->HasSwitch(kOutputFormat))
  51  E :      return true;
  52    :  
  53  E :    std::string format = command_line->GetSwitchValueASCII(kOutputFormat);
  54  E :    if (LowerCaseEqualsASCII(format, "lcov")) {
  55  E :      output_format_ = kLcovFormat;
  56  E :    } else if (LowerCaseEqualsASCII(format, "cachegrind")) {
  57  E :      output_format_ = kCacheGrindFormat;
  58  E :    } else {
  59  E :      LOG(ERROR) << "Unknown output format: " << format << ".";
  60  E :      return false;
  61    :    }
  62  E :    return true;
  63  E :  }
  64    :  
  65  E :  void CoverageGrinder::SetParser(Parser* parser) {
  66  E :    DCHECK(parser != NULL);
  67  E :    parser_ = parser;
  68  E :  }
  69    :  
  70  E :  bool CoverageGrinder::Grind() {
  71  E :    if (event_handler_errored_) {
  72  i :      LOG(WARNING) << "Failed to handle all basic block frequency data events, "
  73    :                   << "coverage results will be partial.";
  74    :    }
  75    :  
  76  E :    if (pdb_info_cache_.empty()) {
  77  E :      LOG(ERROR) << "No coverage data was encountered.";
  78  E :      return false;
  79    :    }
  80    :  
  81  E :    PdbInfoMap::const_iterator it = pdb_info_cache_.begin();
  82  E :    for (; it != pdb_info_cache_.end(); ++it) {
  83  E :      if (!coverage_data_.Add(it->second.line_info)) {
  84  i :        LOG(ERROR) << "Failed to aggregate line information from PDB: "
  85    :                   << it->first.image_file_name;
  86  i :        return false;
  87    :      }
  88  E :    }
  89  E :    DCHECK(!coverage_data_.source_file_coverage_data_map().empty());
  90    :  
  91  E :    return true;
  92  E :  }
  93    :  
  94  E :  bool CoverageGrinder::OutputData(FILE* file) {
  95  E :    DCHECK(file != NULL);
  96  E :    DCHECK(!coverage_data_.source_file_coverage_data_map().empty());
  97    :  
  98    :    // These functions log verbosely for us.
  99  E :    switch (output_format_) {
 100    :      case kLcovFormat: {
 101  E :        if (!WriteLcovCoverageFile(coverage_data_, file))
 102  i :          return false;
 103  E :        break;
 104    :      }
 105    :  
 106    :      case kCacheGrindFormat: {
 107  E :        if (!WriteCacheGrindCoverageFile(coverage_data_, file))
 108  i :          return false;
 109  E :        break;
 110    :      }
 111    :  
 112  i :      default: NOTREACHED() << "Unknown OutputFormat.";
 113    :    }
 114    :  
 115  E :    return true;
 116  E :  }
 117    :  
 118    :  void CoverageGrinder::OnIndexedFrequency(
 119    :      base::Time time,
 120    :      DWORD process_id,
 121    :      DWORD thread_id,
 122  E :      const TraceIndexedFrequencyData* data) {
 123  E :    DCHECK(data != NULL);
 124  E :    DCHECK(parser_ != NULL);
 125    :  
 126    :    if (data->data_type != common::IndexedFrequencyData::COVERAGE &&
 127  E :        data->data_type != common::IndexedFrequencyData::BASIC_BLOCK_ENTRY)
 128  i :      return;
 129    :  
 130  E :    if (data->num_entries == 0) {
 131  i :      LOG(INFO) << "Skipping empty basic block frequency data.";
 132  i :      return;
 133    :    }
 134    :  
 135  E :    if (!IsValidFrequencySize(data->frequency_size)) {
 136  i :      LOG(ERROR) << "Basic block frequency data has invalid frequency_size ("
 137    :                 << data->frequency_size << ").";
 138  i :      event_handler_errored_ = true;
 139  i :      return;
 140    :    }
 141    :  
 142    :    // Get the module information for which this BB frequency data belongs.
 143    :    const ModuleInformation* module_info = parser_->GetModuleInformation(
 144  E :        process_id, AbsoluteAddress64(data->module_base_addr));
 145  E :    if (module_info == NULL) {
 146  i :      LOG(ERROR) << "Failed to find module information for basic block frequency"
 147    :                 << " data.";
 148  i :      event_handler_errored_ = true;
 149  i :      return;
 150    :    }
 151    :  
 152    :    // TODO(chrisha): Validate that the PE file itself is instrumented as
 153    :    //     expected? This isn't strictly necessary but would add another level of
 154    :    //     safety checking.
 155    :  
 156    :    // Get the PDB info. This loads the line information and the basic-block
 157    :    // ranges if not already done, otherwise it returns the cached version.
 158  E :    PdbInfo* pdb_info = NULL;
 159  E :    if (!LoadPdbInfo(&pdb_info_cache_, *module_info, &pdb_info)) {
 160  i :      event_handler_errored_ = true;
 161  i :      return;
 162    :    }
 163    :  
 164  E :    DCHECK(pdb_info != NULL);
 165    :  
 166    :    // Sanity check the contents.
 167  E :    if (data->num_entries != pdb_info->bb_ranges.size()) {
 168  i :      LOG(ERROR) << "Mismatch between trace data BB count and PDB BB count.";
 169  i :      event_handler_errored_ = true;
 170  i :      return;
 171    :    }
 172    :  
 173    :    // Run over the BB frequency data and mark non-zero frequency BBs as having
 174    :    // been visited.
 175  E :    for (size_t bb_index = 0; bb_index < data->num_entries; ++bb_index) {
 176  E :      uint32 bb_freq = GetFrequency(data, bb_index);
 177    :  
 178  E :      if (bb_freq == 0)
 179  E :        continue;
 180    :  
 181    :      // Mark this basic-block as visited.
 182  E :      const RelativeAddressRange& bb_range = pdb_info->bb_ranges[bb_index];
 183    :      if (!pdb_info->line_info.Visit(bb_range.start(),
 184    :                                     bb_range.size(),
 185  E :                                     bb_freq)) {
 186  i :        LOG(ERROR) << "Failed to visit BB at " << bb_range << ".";
 187  i :        event_handler_errored_ = true;
 188  i :        return;
 189    :      }
 190  E :    }
 191  E :  }
 192    :  
 193    :  }  // namespace grinders
 194    :  }  // namespace grinder

Coverage information generated Thu Jul 04 09:34:53 2013.