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 range of simple fields. |range_start| is the id of the first field
47 // in range, |range_len| the number of fields in the range.
48 // AddSimpleFieldRange(N,1) is semantically equivalent to AddSimpleField(N).
AddSimpleFieldRange(uint32_t range_start,uint32_t range_len)49 void FilterBytecodeGenerator::AddSimpleFieldRange(uint32_t range_start,
50 uint32_t range_len) {
51 PERFETTO_CHECK(range_start > last_field_id_);
52 PERFETTO_CHECK(range_len > 0);
53 bytecode_.push_back(range_start << 3 | kFilterOpcode_SimpleFieldRange);
54 bytecode_.push_back(range_len);
55 last_field_id_ = range_start + range_len - 1;
56 endmessage_called_ = false;
57 }
58
59 // Adds a nested field. |message_index| is the index of the message that the
60 // parser must recurse into. This implies that at least |message_index| + 1
61 // calls to EndMessage() will be made.
62 // The Serialize() method will fail if any field points to an out of range
63 // index.
AddNestedField(uint32_t field_id,uint32_t message_index)64 void FilterBytecodeGenerator::AddNestedField(uint32_t field_id,
65 uint32_t message_index) {
66 PERFETTO_CHECK(field_id > last_field_id_);
67 bytecode_.push_back(field_id << 3 | kFilterOpcode_NestedField);
68 bytecode_.push_back(message_index);
69 last_field_id_ = field_id;
70 max_msg_index_ = std::max(max_msg_index_, message_index);
71 endmessage_called_ = false;
72 }
73
74 // Returns the bytes that can be used into TraceConfig.trace_filter.bytecode.
75 // The returned bytecode is a binary buffer which consists of a sequence of
76 // varints (the opcodes) and a checksum.
77 // The returned string can be passed as-is to FilterBytecodeParser.Load().
Serialize()78 std::string FilterBytecodeGenerator::Serialize() {
79 PERFETTO_CHECK(endmessage_called_);
80 PERFETTO_CHECK(max_msg_index_ < num_messages_);
81 protozero::PackedVarInt words;
82 perfetto::base::Hash hasher;
83 for (uint32_t word : bytecode_) {
84 words.Append(word);
85 hasher.Update(word);
86 }
87 words.Append(static_cast<uint32_t>(hasher.digest()));
88 return std::string(reinterpret_cast<const char*>(words.data()), words.size());
89 }
90
91 } // namespace protozero
92