Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: tools/traffic_annotation/auditor/traffic_annotation_auditor.cc

Issue 2448133006: Tool added to extract network traffic annotations. (Closed)
Patch Set: Comments addressed More Windows Corrections. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // See README.md for information and build instructions.
6
7 #include "base/command_line.h"
8 #include "base/files/file_enumerator.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/process/launch.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "net/traffic_annotation/network_traffic_annotation.h"
20 #include "third_party/protobuf/src/google/protobuf/text_format.h"
21
22 #include "tools/traffic_annotation/traffic_annotation.pb.h"
23
24 namespace {
25
26 // Holds an instance of network traffic annotation.
27 struct AnnotationInstance {
28 AnnotationInstance() : state(STATE_OK) {}
29
30 // Protobuf of the annotation.
31 traffic_annotation::NetworkTrafficAnnotation proto;
32 // Unique id of the annotation.
33 std::string unique_id;
34 // State of this annotation.
35 enum State { STATE_OK, STATE_DUPLICATE, STATE_UNDEFINED } state;
36 };
37
38 // Runs clang tool, returns true if all steps are successful. Returns annotation
39 // extractor clang tool's exit code in |clang_tool_exit_code|.
40 bool RunClangTool(const base::FilePath& src_dir,
41 const base::FilePath& build_dir,
42 const base::FilePath& output_dir,
43 const base::CommandLine::StringVector& path_filters,
44 int* clang_tool_exit_code) {
45 for (auto& path : path_filters) {
46 base::CommandLine cmdline(
47 base::FilePath(FILE_PATH_LITERAL("tools"))
48 .Append(FILE_PATH_LITERAL("clang"))
49 .Append(FILE_PATH_LITERAL("scripts"))
50 .Append(FILE_PATH_LITERAL("run_tool.py")));
51 cmdline.AppendSwitch("generate-compdb");
52 cmdline.AppendArgNative(FILE_PATH_LITERAL("traffic_annotation_extractor"));
53 cmdline.AppendArgPath(build_dir);
54 cmdline.AppendArgNative(path);
55 std::string tool_args = base::StringPrintf(
56 "--output-directory=%s", output_dir.MaybeAsASCII().c_str());
57
58 #ifdef _WIN32
59 cmdline.AppendSwitchNative("tool-args", base::ASCIIToUTF16(tool_args));
60 cmdline.PrependWrapper(L"python");
61 #else
62 cmdline.AppendSwitchNative("tool-args", tool_args);
63 #endif
64
65 base::Process process = base::LaunchProcess(cmdline, base::LaunchOptions());
66 if (!process.IsValid() || !process.WaitForExit(clang_tool_exit_code)) {
67 LOG(ERROR) << "Executing clang tool failed.\n";
68 return false;
69 }
70 }
71 return true;
72 }
73
74 // Reads an extracted annotation file and returns it in the output variables.
75 // The file starts with four |header_lines| with the following meaning:
76 // 0- File path.
77 // 1- Name of the function including this position.
78 // 2- Line number.
79 // 3- Unique id of annotation.
80 // The rest of the file is the protobuf text (|msg_text|).
81 bool ReadFile(const base::FilePath& file_path,
82 std::vector<std::string>* header_lines,
83 std::string* msg_text) {
84 std::string file_content;
85 if (!base::ReadFileToString(file_path, &file_content))
86 return false;
87
88 header_lines->clear();
89 msg_text->clear();
90
91 std::vector<std::string> tokens = base::SplitString(
92 file_content, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
93
94 // If enough data is extracted, populate outputs, otherwise leave them blank.
95 if (tokens.size() > 4) {
96 for (int i = 0; i < 4; i++)
97 header_lines->push_back(tokens[i]);
98 for (size_t i = 4; i < tokens.size(); i++)
99 *msg_text += tokens[i] + "\n";
100 }
101
102 return true;
103 }
104
105 // Reads all extracted txt files from given input folder and populates instances
106 // and errors. Errors include not finding the file, incorrect content, or error
107 // passed from clang tool.
108 void ReadExtractedFiles(const base::FilePath& folder_name,
109 std::vector<AnnotationInstance>* instances,
110 std::vector<std::string>* errors) {
111 base::FileEnumerator file_iter(folder_name, false,
112 base::FileEnumerator::FILES,
113 FILE_PATH_LITERAL("*.txt"));
114 while (!file_iter.Next().empty()) {
115 std::string file_name = file_iter.GetInfo().GetName().AsUTF8Unsafe();
116 LOG(INFO) << "Reading " << file_name.c_str() << "...\n";
117
118 std::vector<std::string> header_lines;
119 std::string msg_text;
120 if (!ReadFile(folder_name.Append(file_iter.GetInfo().GetName()),
121 &header_lines, &msg_text)) {
122 errors->push_back(
123 base::StringPrintf("Could not open file '%s'\n.", file_name.c_str()));
124 continue;
125 }
126 if (header_lines.size() < 4) {
127 errors->push_back(base::StringPrintf(
128 "Header lines are not complete for file '%s'\n.", file_name.c_str()));
129 continue;
130 }
131
132 // If annotation unique id is 'unittest', just ignore it.
133 if (base::ToLowerASCII(header_lines[3]) == "unittest")
134 continue;
135
136 AnnotationInstance new_instance;
137
138 if (header_lines[3] == "\"Undefined\"") {
139 new_instance.state = AnnotationInstance::STATE_UNDEFINED;
140 } else if (!google::protobuf::TextFormat::ParseFromString(
141 msg_text, (google::protobuf::Message*)&new_instance)) {
142 errors->push_back(base::StringPrintf(
143 "Could not parse protobuf for file '%s'\n.", file_name.c_str()));
144 continue;
145 }
146
147 // Add header data to new instance.
148 traffic_annotation::NetworkTrafficAnnotation_TrafficSource* src =
149 new_instance.proto.mutable_source();
150 src->set_file(header_lines[0]);
151 src->set_function(header_lines[1]);
152 int line;
153 base::StringToInt(header_lines[2], &line);
154 src->set_line(line);
155 new_instance.unique_id = header_lines[3];
156 instances->push_back(new_instance);
157 }
158 LOG(INFO) << instances->size() << " annotation instance(s) read.\n";
159 }
160
161 // Checks to see if unique ids are really unique and marks the ones which are
162 // not.
163 void MarkRepeatedUniqueIds(std::vector<AnnotationInstance>* instances) {
164 std::map<std::string, AnnotationInstance*> unique_ids;
165 for (auto& instance : *instances) {
166 if (instance.state == AnnotationInstance::STATE_OK) {
167 auto match = unique_ids.find(instance.unique_id);
168 if (match != unique_ids.end()) {
169 instance.state = match->second->state =
170 AnnotationInstance::STATE_DUPLICATE;
171 } else {
172 unique_ids.insert(
173 std::make_pair(std::string(instance.unique_id), &instance));
174 }
175 }
176 }
177 }
178
179 // Writes summary of annotation instances to a file. Returns true if successful.
180 bool WriteSummaryFile(int clang_tool_exit_code,
181 const std::vector<AnnotationInstance>& instances,
182 const std::vector<std::string>& errors,
183 const base::FilePath& file_path) {
184 std::string report = "";
185
186 if (errors.size() || clang_tool_exit_code) {
187 report = "[Errors]\n";
188
189 if (clang_tool_exit_code)
190 report += base::StringPrintf("Clang tool returned error: %i\n",
191 clang_tool_exit_code);
192
193 for (const auto& error : errors)
194 report += error + "\n";
195 }
196
197 report += "[Annotations]\n";
198
199 for (const auto& instance : instances) {
200 report +=
201 "------------------------------------------------------------"
202 "--------------------\n";
203 report += base::StringPrintf("Unique ID: %s\n", instance.unique_id.c_str());
204 if (instance.state == AnnotationInstance::STATE_UNDEFINED)
205 report += base::StringPrintf("WARNING: Undefined annotation.\n");
206 else if (instance.state == AnnotationInstance::STATE_DUPLICATE)
207 report += base::StringPrintf("WARNING: Duplicate unique id.\n");
208 std::string temp;
209 google::protobuf::TextFormat::PrintToString(instance.proto, &temp);
210 report += base::StringPrintf("Content:\n%s\n", temp.c_str());
211 }
212
213 if (base::WriteFile(file_path, report.c_str(), report.length()) == -1) {
214 LOG(INFO) << " Could not create output file: "
215 << file_path.MaybeAsASCII().c_str() << ".\n";
216 return false;
217 }
218
219 LOG(INFO) << "Output file " << file_path.MaybeAsASCII().c_str()
220 << " written for " << instances.size() << " instances.\n";
221 return true;
222 }
223
224 } // namespace
225
226 #ifdef _WIN32
227 int wmain(int argc, wchar_t* argv[]) {
228 #else
229 int main(int argc, char* argv[]) {
230 #endif
231 // Parse switches.
232 base::CommandLine command_line = base::CommandLine(argc, argv);
233 if (command_line.HasSwitch("help") || command_line.HasSwitch("h") ||
234 argc == 1) {
235 LOG(INFO)
236 << "Usage: traffic_annotation_auditor [OPTION]... [path_filter]...\n"
237 "Extracts network traffic annotations from source files. If path "
238 "filter(s) are specified, only those directories of the source "
239 "will be analyzed.\n"
240 "Options:\n"
241 " -h, --help Shows help.\n"
242 " --build-dir Path to the build directory from which "
243 "the\n"
244 " annotations will be extracted.\n"
245 " --extractor-output-dir Path to the directory that extracted\n"
246 " partial files will be written to. "
247 "Will\n"
248 " be automatically generated and "
249 "deleted\n"
250 " if not specified.\n"
251 " --extracted-input-dir Path to a directory where extracted\n"
252 " partial annotations are already "
253 "stored.\n"
254 " If specified, build directory will be\n"
255 " ignored.\n"
256 " --summary-file Path to an output file with summary of\n"
257 " extracted annotations.\n"
258 "Example:\n"
259 " traffic_annotation_auditor --build-dir=out/Debug\n"
260 " --summary-file=report.txt\n";
261 return 1;
262 }
263 base::FilePath extracted_files_dir =
264 command_line.GetSwitchValuePath("extracted-input-dir");
265 base::FilePath build_dir = command_line.GetSwitchValuePath("build-dir");
266 base::FilePath extractor_output_dir =
267 command_line.GetSwitchValuePath("extractor-output-dir");
268 base::FilePath summary_file = command_line.GetSwitchValuePath("summary-file");
269 base::ScopedTempDir temp_dir;
270
271 if (summary_file.empty())
272 LOG(ERROR) << "WARNING: Output file not specified.\n";
273
274 int clang_tool_exit_code = 0;
275
276 // Extract annotations.
277 if (extracted_files_dir.empty()) {
278 // Get build directory, if it is empty issue an error.
279 if (build_dir.empty()) {
280 LOG(ERROR)
281 << "You must either specify the build directory to run the clang "
282 "tool and extract annotations, or specify the input directory "
283 "where extracted annotation files already exist.\n";
284 return 1;
285 }
286 // If output directory is not provided, create a temporary one.
287 if (extractor_output_dir.empty()) {
288 if (!temp_dir.CreateUniqueTempDirUnderPath(build_dir)) {
289 LOG(ERROR) << "Could not create temporary directory in "
290 << build_dir.MaybeAsASCII().c_str() << ".\n";
291 return 1;
292 }
293 extractor_output_dir = temp_dir.GetPath();
294 } else {
295 // Ensure given directory is empty.
296 if (!base::FileEnumerator(extractor_output_dir, false,
297 base::FileEnumerator::FILES)
298 .Next()
299 .empty()) {
300 LOG(ERROR) << "Output directory "
301 << extractor_output_dir.MaybeAsASCII().c_str()
302 << "should be empty .\n";
303 return 1;
304 }
305 }
306
307 // Get path filters, if none is provided, just add all.
308 base::CommandLine::StringVector path_filters = command_line.GetArgs();
309 if (!path_filters.size()) {
310 base::FilePath temp;
311 path_filters.push_back(temp.AppendASCII("./").value().c_str());
312 }
313
314 // Eexcutable is usually in out/[Build Dir], so the path to source is
315 // extracted by moving two directories up.
316 if (!RunClangTool(command_line.GetProgram().DirName().AppendASCII("../.."),
317 build_dir, extractor_output_dir, path_filters,
318 &clang_tool_exit_code)) {
319 return 1;
320 }
321
322 extracted_files_dir = extractor_output_dir;
323 }
324
325 // Read all extracted files.
326 std::vector<AnnotationInstance> instances;
327 std::vector<std::string> errors;
328 ReadExtractedFiles(extracted_files_dir, &instances, &errors);
329
330 if (instances.empty()) {
331 LOG(ERROR) << "Could not read any file.\n";
332 return 1;
333 } else {
334 MarkRepeatedUniqueIds(&instances);
335 }
336
337 // Create Summary file if requested.
338 if (!summary_file.empty() &&
339 !WriteSummaryFile(clang_tool_exit_code, instances, errors, summary_file))
340 return 1;
341
342 return 0;
343 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698