Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 1 | /* |
| 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 Zappone | 6e4e038 | 2018-02-27 11:15:30 +0000 | [diff] [blame] | 17 | #include <sys/stat.h> |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 18 | #include <fstream> |
| 19 | #include <memory> |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 20 | #include <regex> |
| 21 | #include <set> |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 22 | #include <sstream> |
| 23 | #include <string> |
| 24 | |
Florian Mayer | 74b73a9 | 2018-03-09 17:37:13 +0000 | [diff] [blame] | 25 | #include "perfetto/base/file_utils.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 26 | #include "perfetto/ftrace_reader/format_parser.h" |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 27 | #include "perfetto/trace/ftrace/ftrace_event.pbzero.h" |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 28 | #include "tools/ftrace_proto_gen/ftrace_proto_gen.h" |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 29 | |
| 30 | int main(int argc, const char** argv) { |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 31 | if (argc != 4) { |
| 32 | fprintf(stderr, "Usage: ./%s whitelist_dir input_dir output_dir\n", |
| 33 | argv[0]); |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 34 | return 1; |
| 35 | } |
| 36 | |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 37 | const char* whitelist_path = argv[1]; |
| 38 | const char* input_dir = argv[2]; |
| 39 | const char* output_dir = argv[3]; |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 40 | |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 41 | std::set<std::string> events = perfetto::GetWhitelistedEvents(whitelist_path); |
| 42 | std::vector<std::string> events_info; |
| 43 | |
Florian Mayer | 74b73a9 | 2018-03-09 17:37:13 +0000 | [diff] [blame] | 44 | std::string ftrace; |
| 45 | if (!perfetto::base::ReadFile( |
| 46 | "protos/perfetto/trace/ftrace/ftrace_event.proto", &ftrace)) { |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 47 | fprintf(stderr, "Failed to open %s\n", |
| 48 | "protos/perfetto/trace/ftrace/ftrace_event.proto"); |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 49 | return 1; |
| 50 | } |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 51 | |
Anna Zappone | 6e4e038 | 2018-02-27 11:15:30 +0000 | [diff] [blame] | 52 | std::set<std::string> new_events; |
Florian Mayer | 26a54d3 | 2018-03-23 10:56:20 +0000 | [diff] [blame] | 53 | for (const auto& event : events) { |
Anna Zappone | 6e4e038 | 2018-02-27 11:15:30 +0000 | [diff] [blame] | 54 | 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 Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 65 | 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 Zappone | 6e4e038 | 2018-02-27 11:15:30 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 73 | 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 Mayer | 74b73a9 | 2018-03-09 17:37:13 +0000 | [diff] [blame] | 80 | std::string contents; |
| 81 | if (!perfetto::base::ReadFile(input_path, &contents)) { |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 82 | fprintf(stderr, "Failed to open %s\n", input_path.c_str()); |
| 83 | return 1; |
| 84 | } |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 85 | |
| 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 Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 100 | 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 Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 111 | if (!new_events.empty()) |
| 112 | PrintInodeHandlerMain(format.name, proto); |
| 113 | |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 114 | 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 Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 127 | perfetto::GenerateEventInfo(events_info); |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 128 | } |