blob: 8bccee9b227d448153353a56abb56989fc90f0c0 [file] [log] [blame]
Hector Dearmanbe61adf2017-10-18 15:58:46 +01001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Anna Zappone6e4e0382018-02-27 11:15:30 +000017#include <sys/stat.h>
Hector Dearmanbe61adf2017-10-18 15:58:46 +010018#include <fstream>
19#include <memory>
Isabelle Taylor6498fcf2018-02-06 11:35:05 +000020#include <regex>
21#include <set>
Hector Dearmanbe61adf2017-10-18 15:58:46 +010022#include <sstream>
23#include <string>
24
Florian Mayer74b73a92018-03-09 17:37:13 +000025#include "perfetto/base/file_utils.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000026#include "perfetto/ftrace_reader/format_parser.h"
Isabelle Taylor6498fcf2018-02-06 11:35:05 +000027#include "perfetto/trace/ftrace/ftrace_event.pbzero.h"
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010028#include "tools/ftrace_proto_gen/ftrace_proto_gen.h"
Hector Dearmanbe61adf2017-10-18 15:58:46 +010029
30int main(int argc, const char** argv) {
Isabelle Taylor6498fcf2018-02-06 11:35:05 +000031 if (argc != 4) {
32 fprintf(stderr, "Usage: ./%s whitelist_dir input_dir output_dir\n",
33 argv[0]);
Hector Dearmanbe61adf2017-10-18 15:58:46 +010034 return 1;
35 }
36
Isabelle Taylor6498fcf2018-02-06 11:35:05 +000037 const char* whitelist_path = argv[1];
38 const char* input_dir = argv[2];
39 const char* output_dir = argv[3];
Hector Dearmanbe61adf2017-10-18 15:58:46 +010040
Isabelle Taylor6498fcf2018-02-06 11:35:05 +000041 std::set<std::string> events = perfetto::GetWhitelistedEvents(whitelist_path);
42 std::vector<std::string> events_info;
43
Florian Mayer74b73a92018-03-09 17:37:13 +000044 std::string ftrace;
45 if (!perfetto::base::ReadFile(
46 "protos/perfetto/trace/ftrace/ftrace_event.proto", &ftrace)) {
Isabelle Taylor6498fcf2018-02-06 11:35:05 +000047 fprintf(stderr, "Failed to open %s\n",
48 "protos/perfetto/trace/ftrace/ftrace_event.proto");
Hector Dearmanbe61adf2017-10-18 15:58:46 +010049 return 1;
50 }
Hector Dearmanbe61adf2017-10-18 15:58:46 +010051
Anna Zappone6e4e0382018-02-27 11:15:30 +000052 std::set<std::string> new_events;
Florian Mayer26a54d32018-03-23 10:56:20 +000053 for (const auto& event : events) {
Anna Zappone6e4e0382018-02-27 11:15:30 +000054 std::string file_name =
55 event.substr(event.find('/') + 1, std::string::npos);
56 struct stat buf;
57 if (stat(("protos/perfetto/trace/ftrace/" + file_name + ".proto").c_str(),
58 &buf) == -1) {
59 new_events.insert(file_name);
60 }
61 }
62
63 if (!new_events.empty()) {
64 perfetto::PrintFtraceEventProtoAdditions(new_events);
Anna Zappone7c104622018-03-28 16:35:18 +010065 perfetto::PrintEventFormatterMain(new_events);
66 perfetto::PrintEventFormatterUsingStatements(new_events);
67 perfetto::PrintEventFormatterFunctions(new_events);
68 printf(
69 "\nAdd output to ParseInode in "
70 "tools/ftrace_proto_gen/ftrace_inode_handler.cc\n");
Anna Zappone6e4e0382018-02-27 11:15:30 +000071 }
72
Isabelle Taylor6498fcf2018-02-06 11:35:05 +000073 for (auto event : events) {
74 std::string proto_file_name =
75 event.substr(event.find('/') + 1, std::string::npos) + ".proto";
76 std::string group = event.substr(0, event.find('/'));
77 std::string input_path = input_dir + event + std::string("/format");
78 std::string output_path = output_dir + std::string("/") + proto_file_name;
79
Florian Mayer74b73a92018-03-09 17:37:13 +000080 std::string contents;
81 if (!perfetto::base::ReadFile(input_path, &contents)) {
Isabelle Taylor6498fcf2018-02-06 11:35:05 +000082 fprintf(stderr, "Failed to open %s\n", input_path.c_str());
83 return 1;
84 }
Isabelle Taylor6498fcf2018-02-06 11:35:05 +000085
86 perfetto::FtraceEvent format;
87 if (!perfetto::ParseFtraceEvent(contents, &format)) {
88 fprintf(stderr, "Could not parse file %s.\n", input_path.c_str());
89 return 1;
90 }
91
92 perfetto::Proto proto;
93 if (!perfetto::GenerateProto(format, &proto)) {
94 fprintf(stderr, "Could not generate proto for file %s\n",
95 input_path.c_str());
96 return 1;
97 }
98
99 std::smatch match;
Isabelle Taylor6498fcf2018-02-06 11:35:05 +0000100 std::regex event_regex(format.name + "\\s*=\\s*(\\d+)");
101 std::regex_search(ftrace, match, event_regex);
102 std::string proto_field_id = match[1].str().c_str();
103 if (proto_field_id == "") {
104 fprintf(stderr,
105 "Could not find proto_field_id for %s in ftrace_event.proto. "
106 "Please add it.\n",
107 format.name.c_str());
108 return 1;
109 }
110
Anna Zappone7c104622018-03-28 16:35:18 +0100111 if (!new_events.empty())
112 PrintInodeHandlerMain(format.name, proto);
113
Isabelle Taylor6498fcf2018-02-06 11:35:05 +0000114 events_info.push_back(
115 perfetto::SingleEventInfo(format, proto, group, proto_field_id));
116
117 std::ofstream fout(output_path.c_str(), std::ios::out);
118 if (!fout) {
119 fprintf(stderr, "Failed to open %s\n", output_path.c_str());
120 return 1;
121 }
122
123 fout << proto.ToString();
124 fout.close();
Hector Dearmanbe61adf2017-10-18 15:58:46 +0100125 }
126
Isabelle Taylor6498fcf2018-02-06 11:35:05 +0000127 perfetto::GenerateEventInfo(events_info);
Hector Dearmanbe61adf2017-10-18 15:58:46 +0100128}