xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/proto/pigweed_detokenizer.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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_TRACE_PROCESSOR_IMPORTERS_PROTO_PIGWEED_DETOKENIZER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PIGWEED_DETOKENIZER_H_
19 
20 #include <string_view>
21 #include <variant>
22 
23 #include "perfetto/ext/base/flat_hash_map.h"
24 #include "perfetto/ext/base/status_or.h"
25 #include "perfetto/protozero/field.h"
26 
27 namespace perfetto::trace_processor::pigweed {
28 
29 // We only distinguish between the int types that we need to; we need
30 // to know different lengths for unsigned due to varint encoding.
31 // Strings are not supported.
32 enum ArgType { kSignedInt, kUnsigned32, kUnsigned64, kFloat };
33 
34 // Representation of an arg in a formatting string: where it is,
35 // its contents, and its type.
36 struct Arg {
37   ArgType type;
38   std::string format;
39   size_t begin;
40   size_t end;
41 };
42 
43 // A parsed format string from the database.
44 class FormatString {
45  public:
46   FormatString() = default;
47   FormatString(std::string template_str);
48 
template_str()49   const std::string& template_str() const { return template_str_; }
50 
args()51   const std::vector<Arg>& args() const { return args_; }
52 
53  private:
54   std::string template_str_;
55   std::vector<Arg> args_;
56 };
57 
58 // A string that we have detokenized, along with any information we gathered
59 // along the way.
60 class DetokenizedString {
61  public:
62   explicit DetokenizedString(const uint32_t token, FormatString str_template);
63 
64   explicit DetokenizedString(
65       const uint32_t token,
66       FormatString str_template,
67       std::vector<std::variant<int64_t, uint64_t, double>> args,
68       std::vector<std::string> args_formatted);
69 
70   // The fully formatted string.
71   std::string Format() const;
72 
73   // The printf template used to format the string.
template_str()74   const std::string& template_str() const {
75     return format_string_.template_str();
76   }
77 
78   // The ID of the template used to format the string.
token()79   uint32_t token() const { return token_; }
80 
81   // Numerical args in the string, in order.
args()82   const std::vector<std::variant<int64_t, uint64_t, double>>& args() const {
83     return args_;
84   }
85 
86  private:
87   uint32_t token_;
88   FormatString format_string_;
89   // We don't bother holding 32 bit versions, just promote them.
90   std::vector<std::variant<int64_t, uint64_t, double>> args_;
91   std::vector<std::string> args_formatted_;
92 };
93 
94 class PigweedDetokenizer {
95  public:
96   explicit PigweedDetokenizer(base::FlatHashMap<uint32_t, FormatString> tokens);
97   base::StatusOr<DetokenizedString> Detokenize(
98       const protozero::ConstBytes& bytes) const;
99 
100  private:
101   base::FlatHashMap<uint32_t, FormatString> tokens_;
102 };
103 
104 PigweedDetokenizer CreateNullDetokenizer();
105 
106 base::StatusOr<PigweedDetokenizer> CreateDetokenizer(
107     const protozero::ConstBytes& blob);
108 
109 }  // namespace perfetto::trace_processor::pigweed
110 
111 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PIGWEED_DETOKENIZER_H_
112