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/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 :
28 : using basic_block_util::ModuleInformation;
29 : using basic_block_util::RelativeAddressRange;
30 : using basic_block_util::GetFrequency;
31 : using basic_block_util::LoadPdbInfo;
32 : using basic_block_util::IsValidFrequencySize;
33 : using basic_block_util::PdbInfo;
34 : using basic_block_util::PdbInfoMap;
35 : using trace::parser::AbsoluteAddress64;
36 :
37 : CoverageGrinder::CoverageGrinder()
38 : : parser_(NULL),
39 : event_handler_errored_(false),
40 E : output_format_(kLcovFormat) {
41 E : }
42 :
43 E : CoverageGrinder::~CoverageGrinder() {
44 E : }
45 :
46 E : bool CoverageGrinder::ParseCommandLine(const CommandLine* command_line) {
47 : // If the switch isn't present we have nothing to do!
48 E : const char kOutputFormat[] = "output-format";
49 E : if (!command_line->HasSwitch(kOutputFormat))
50 E : return true;
51 :
52 E : std::string format = command_line->GetSwitchValueASCII(kOutputFormat);
53 E : if (LowerCaseEqualsASCII(format, "lcov")) {
54 E : output_format_ = kLcovFormat;
55 E : } else if (LowerCaseEqualsASCII(format, "cachegrind")) {
56 E : output_format_ = kCacheGrindFormat;
57 E : } else {
58 E : LOG(ERROR) << "Unknown output format: " << format << ".";
59 E : return false;
60 : }
61 E : return true;
62 E : }
63 :
64 E : void CoverageGrinder::SetParser(Parser* parser) {
65 E : DCHECK(parser != NULL);
66 E : parser_ = parser;
67 E : }
68 :
69 E : bool CoverageGrinder::Grind() {
70 E : if (event_handler_errored_) {
71 i : LOG(WARNING) << "Failed to handle all basic block frequency data events, "
72 : << "coverage results will be partial.";
73 : }
74 :
75 E : if (pdb_info_cache_.empty()) {
76 E : LOG(ERROR) << "No coverage data was encountered.";
77 E : return false;
78 : }
79 :
80 E : PdbInfoMap::const_iterator it = pdb_info_cache_.begin();
81 E : for (; it != pdb_info_cache_.end(); ++it) {
82 E : if (!coverage_data_.Add(it->second.line_info)) {
83 i : LOG(ERROR) << "Failed to aggregate line information from PDB: "
84 : << it->first.image_file_name;
85 i : return false;
86 : }
87 E : }
88 E : DCHECK(!coverage_data_.source_file_coverage_data_map().empty());
89 :
90 E : return true;
91 E : }
92 :
93 E : bool CoverageGrinder::OutputData(FILE* file) {
94 E : DCHECK(file != NULL);
95 E : DCHECK(!coverage_data_.source_file_coverage_data_map().empty());
96 :
97 : // These functions log verbosely for us.
98 E : switch (output_format_) {
99 : case kLcovFormat: {
100 E : if (!WriteLcovCoverageFile(coverage_data_, file))
101 i : return false;
102 E : break;
103 : }
104 :
105 : case kCacheGrindFormat: {
106 E : if (!WriteCacheGrindCoverageFile(coverage_data_, file))
107 i : return false;
108 E : break;
109 : }
110 :
111 i : default: NOTREACHED() << "Unknown OutputFormat.";
112 : }
113 :
114 E : return true;
115 E : }
116 :
117 : void CoverageGrinder::OnIndexedFrequency(
118 : base::Time time,
119 : DWORD process_id,
120 : DWORD thread_id,
121 E : const TraceIndexedFrequencyData* data) {
122 E : DCHECK(data != NULL);
123 E : DCHECK(parser_ != NULL);
124 :
125 E : if (data->data_type != TraceIndexedFrequencyData::BASIC_BLOCK)
126 i : return;
127 :
128 E : if (data->num_entries == 0) {
129 i : LOG(INFO) << "Skipping empty basic block frequency data.";
130 i : return;
131 : }
132 :
133 E : if (!IsValidFrequencySize(data->frequency_size)) {
134 i : LOG(ERROR) << "Basic block frequency data has invalid frequency_size ("
135 : << data->frequency_size << ").";
136 i : event_handler_errored_ = true;
137 i : return;
138 : }
139 :
140 : // Get the module information for which this BB frequency data belongs.
141 : const ModuleInformation* module_info = parser_->GetModuleInformation(
142 E : process_id, AbsoluteAddress64(data->module_base_addr));
143 E : if (module_info == NULL) {
144 i : LOG(ERROR) << "Failed to find module information for basic block frequency"
145 : << " data.";
146 i : event_handler_errored_ = true;
147 i : return;
148 : }
149 :
150 : // TODO(chrisha): Validate that the PE file itself is instrumented as
151 : // expected? This isn't strictly necessary but would add another level of
152 : // safety checking.
153 :
154 : // Get the PDB info. This loads the line information and the basic-block
155 : // ranges if not already done, otherwise it returns the cached version.
156 E : PdbInfo* pdb_info = NULL;
157 E : if (!LoadPdbInfo(&pdb_info_cache_, *module_info, &pdb_info)) {
158 i : event_handler_errored_ = true;
159 i : return;
160 : }
161 :
162 E : DCHECK(pdb_info != NULL);
163 :
164 : // Sanity check the contents.
165 E : if (data->num_entries != pdb_info->bb_ranges.size()) {
166 i : LOG(ERROR) << "Mismatch between trace data BB count and PDB BB count.";
167 i : event_handler_errored_ = true;
168 i : return;
169 : }
170 :
171 : // Run over the BB frequency data and mark non-zero frequency BBs as having
172 : // been visited.
173 E : for (size_t bb_index = 0; bb_index < data->num_entries; ++bb_index) {
174 E : uint32 bb_freq = GetFrequency(data, bb_index);
175 :
176 E : if (bb_freq == 0)
177 E : continue;
178 :
179 : // Mark this basic-block as visited.
180 E : const RelativeAddressRange& bb_range = pdb_info->bb_ranges[bb_index];
181 : if (!pdb_info->line_info.Visit(bb_range.start(),
182 : bb_range.size(),
183 E : bb_freq)) {
184 i : LOG(ERROR) << "Failed to visit BB at " << bb_range << ".";
185 i : event_handler_errored_ = true;
186 i : return;
187 : }
188 E : }
189 E : }
190 :
191 : } // namespace grinder
|