1 /* 2 * Copyright (C) 2018 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 17 #ifndef TOOLS_FTRACE_PROTO_GEN_PROTO_GEN_UTILS_H_ 18 #define TOOLS_FTRACE_PROTO_GEN_PROTO_GEN_UTILS_H_ 19 20 #include <map> 21 #include <set> 22 #include <sstream> 23 #include <string> 24 #include <vector> 25 26 #include <google/protobuf/descriptor.h> 27 28 #include "src/traced/probes/ftrace/format_parser.h" 29 30 namespace perfetto { 31 32 class VerifyStream : public std::ostringstream { 33 public: 34 VerifyStream(std::string filename); 35 virtual ~VerifyStream(); 36 37 private: 38 std::string filename_; 39 std::string expected_; 40 }; 41 42 class FtraceEventName { 43 public: 44 explicit FtraceEventName(const std::string& full_name); 45 46 bool valid() const; 47 const std::string& name() const; 48 const std::string& group() const; 49 50 private: 51 bool valid_; 52 std::string name_; 53 std::string group_; 54 }; 55 56 struct ProtoType { 57 enum Type { INVALID, NUMERIC, STRING }; 58 Type type; 59 uint16_t size; 60 bool is_signed; 61 62 ProtoType GetSigned() const; 63 std::string ToString() const; 64 65 static ProtoType Invalid(); 66 static ProtoType String(); 67 static ProtoType Numeric(uint16_t size, bool is_signed); 68 static ProtoType FromDescriptor(google::protobuf::FieldDescriptor::Type type); 69 }; 70 71 struct Proto { 72 Proto() = default; 73 Proto(std::string evt_name, const google::protobuf::Descriptor& desc); 74 struct Field { 75 ProtoType type; 76 std::string name; 77 uint32_t number; 78 }; 79 std::string name; 80 std::string event_name; 81 std::map<std::string, Field> fields; 82 83 std::string ToString(); 84 void MergeFrom(const Proto& other); 85 void AddField(Proto::Field field); 86 std::vector<const Field*> SortedFields(); 87 uint32_t max_id = 0; 88 }; 89 90 std::string ToCamelCase(const std::string& s); 91 ProtoType GetCommon(ProtoType one, ProtoType other); 92 std::string ProtoHeader(); 93 bool GenerateProto(const FtraceEvent& format, Proto* proto_out); 94 ProtoType InferProtoType(const FtraceEvent::Field& field); 95 96 } // namespace perfetto 97 98 #endif // TOOLS_FTRACE_PROTO_GEN_PROTO_GEN_UTILS_H_ 99