• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 SRC_PROTOZERO_FILTERING_FILTER_UTIL_H_
18 #define SRC_PROTOZERO_FILTERING_FILTER_UTIL_H_
19 
20 #include <stdint.h>
21 
22 #include <list>
23 #include <map>
24 #include <string>
25 
26 // We include this intentionally instead of forward declaring to allow
27 // for an easy find/replace transformation when moving to Google3.
28 #include <google/protobuf/descriptor.h>
29 
30 namespace protozero {
31 
32 // Parses a .proto message definition, recursing into its sub-messages, and
33 // builds up a set of Messages and Field definitions.
34 // Depends on libprotobuf-full and should be used only in host tools.
35 // See the //tools/proto_filter for an executable that wraps this class with
36 // a cmdline interface.
37 class FilterUtil {
38  public:
39   FilterUtil();
40   ~FilterUtil();
41 
42   // Loads a message schema from a .proto file, recursing into nested types.
43   // Args:
44   // proto_file: path to the .proto file.
45   // root_message: fully qualified message name (e.g., perfetto.protos.Trace).
46   //     If empty, the first message in the file will be used.
47   // proto_dir_path: the root for .proto includes. If empty uses CWD.
48   bool LoadMessageDefinition(const std::string& proto_file,
49                              const std::string& root_message,
50                              const std::string& proto_dir_path);
51 
52   // Deduplicates leaf messages having the same sets of field ids.
53   // It changes the internal state and affects the behavior of next calls to
54   // GenerateFilterBytecode() and PrintAsText().
55   void Dedupe();
56 
57   // Generates the filter bytecode for the root message previously loaded by
58   // LoadMessageDefinition() using FilterBytecodeGenerator.
59   // The returned string is a binary-encoded proto message of type
60   // perfetto.protos.ProtoFilter (see proto_filter.proto).
61   std::string GenerateFilterBytecode();
62 
63   // Prints the list of messages and fields onto stdout in a diff-friendly text
64   // format. Example:
65   // PowerRails                 2 message  energy_data     PowerRails.EnergyData
66   // PowerRails.RailDescriptor  1 uint32   index
67   void PrintAsText();
68 
69   // Resolves an array of field ids into a dot-concatenated field names.
70   // E.g., [2,5,1] -> ".trace.packet.timestamp".
71   std::string LookupField(const uint32_t* field_ids, size_t num_fields);
72 
73   // Like the above but the array of field is passed as a buffer containing
74   // varints, e.g. "\x02\x05\0x01".
75   std::string LookupField(const std::string& varint_encoded_path);
76 
77  private:
78   struct Message {
79     struct Field {
80       std::string name;
81       std::string type;  // "uint32", "string", "message"
82       // Only when type == "message". Note that when using Dedupe() this can
83       // be aliased against a different submessage which happens to have the
84       // same set of field ids.
85       Message* nested_type = nullptr;
is_simpleMessage::Field86       bool is_simple() const { return nested_type == nullptr; }
87     };
88     std::string full_name;  // e.g., "perfetto.protos.Foo.Bar";
89     std::map<uint32_t /*field_id*/, Field> fields;
90 
91     // True if at least one field has a non-null |nestd_type|.
92     bool has_nested_fields = false;
93   };
94 
95   using DescriptorsByNameMap = std::map<std::string, Message*>;
96   Message* ParseProtoDescriptor(const google::protobuf::Descriptor*,
97                                 DescriptorsByNameMap*);
98 
99   // list<> because pointers need to be stable.
100   std::list<Message> descriptors_;
101 };
102 
103 }  // namespace protozero
104 
105 #endif  // SRC_PROTOZERO_FILTERING_FILTER_UTIL_H_
106