xref: /aosp_15_r20/external/perfetto/src/protozero/filtering/filter_bytecode_generator.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #include "src/protozero/filtering/filter_bytecode_generator.h"
18 
19 #include "perfetto/base/logging.h"
20 #include "perfetto/ext/base/hash.h"
21 #include "perfetto/protozero/packed_repeated_fields.h"
22 #include "perfetto/protozero/proto_utils.h"
23 #include "perfetto/protozero/scattered_heap_buffer.h"
24 #include "src/protozero/filtering/filter_bytecode_common.h"
25 
26 namespace protozero {
27 
28 FilterBytecodeGenerator::FilterBytecodeGenerator() = default;
29 FilterBytecodeGenerator::~FilterBytecodeGenerator() = default;
30 
EndMessage()31 void FilterBytecodeGenerator::EndMessage() {
32   endmessage_called_ = true;
33   bytecode_.push_back(kFilterOpcode_EndOfMessage);
34   last_field_id_ = 0;
35   ++num_messages_;
36 }
37 
38 // Allows a simple field (varint, fixed32/64, string or bytes).
AddSimpleField(uint32_t field_id)39 void FilterBytecodeGenerator::AddSimpleField(uint32_t field_id) {
40   PERFETTO_CHECK(field_id > last_field_id_);
41   bytecode_.push_back(field_id << 3 | kFilterOpcode_SimpleField);
42   last_field_id_ = field_id;
43   endmessage_called_ = false;
44 }
45 
46 // Allows a string field which needs to be rewritten using the given chain.
AddFilterStringField(uint32_t field_id)47 void FilterBytecodeGenerator::AddFilterStringField(uint32_t field_id) {
48   PERFETTO_CHECK(field_id > last_field_id_);
49   bytecode_.push_back(field_id << 3 | kFilterOpcode_FilterString);
50   last_field_id_ = field_id;
51   endmessage_called_ = false;
52 }
53 
54 // Allows a range of simple fields. |range_start| is the id of the first field
55 // in range, |range_len| the number of fields in the range.
56 // AddSimpleFieldRange(N,1) is semantically equivalent to AddSimpleField(N).
AddSimpleFieldRange(uint32_t range_start,uint32_t range_len)57 void FilterBytecodeGenerator::AddSimpleFieldRange(uint32_t range_start,
58                                                   uint32_t range_len) {
59   PERFETTO_CHECK(range_start > last_field_id_);
60   PERFETTO_CHECK(range_len > 0);
61   bytecode_.push_back(range_start << 3 | kFilterOpcode_SimpleFieldRange);
62   bytecode_.push_back(range_len);
63   last_field_id_ = range_start + range_len - 1;
64   endmessage_called_ = false;
65 }
66 
67 // Adds a nested field. |message_index| is the index of the message that the
68 // parser must recurse into. This implies that at least |message_index| + 1
69 // calls to EndMessage() will be made.
70 // The Serialize() method will fail if any field points to an out of range
71 // index.
AddNestedField(uint32_t field_id,uint32_t message_index)72 void FilterBytecodeGenerator::AddNestedField(uint32_t field_id,
73                                              uint32_t message_index) {
74   PERFETTO_CHECK(field_id > last_field_id_);
75   bytecode_.push_back(field_id << 3 | kFilterOpcode_NestedField);
76   bytecode_.push_back(message_index);
77   last_field_id_ = field_id;
78   max_msg_index_ = std::max(max_msg_index_, message_index);
79   endmessage_called_ = false;
80 }
81 
82 // Returns the bytes that can be used into TraceConfig.trace_filter.bytecode.
83 // The returned bytecode is a binary buffer which consists of a sequence of
84 // varints (the opcodes) and a checksum.
85 // The returned string can be passed as-is to FilterBytecodeParser.Load().
Serialize()86 std::string FilterBytecodeGenerator::Serialize() {
87   PERFETTO_CHECK(endmessage_called_);
88   PERFETTO_CHECK(max_msg_index_ < num_messages_);
89   protozero::PackedVarInt words;
90   perfetto::base::Hasher hasher;
91   for (uint32_t word : bytecode_) {
92     words.Append(word);
93     hasher.Update(word);
94   }
95   words.Append(static_cast<uint32_t>(hasher.digest()));
96   return std::string(reinterpret_cast<const char*>(words.data()), words.size());
97 }
98 
99 }  // namespace protozero
100