blob: fd29885eab0b08ca4e0c1fe69a4478b659fa6f2c [file] [log] [blame]
Aaron Vaage14b255d2024-02-28 09:11:00 -08001/*
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
Aaron Vaage74245392024-02-28 13:32:39 -080017#include <string>
18
Aaron Vaage14b255d2024-02-28 09:11:00 -080019#include "src/trace_redaction/scrub_trace_packet.h"
20
Aaron Vaage74245392024-02-28 13:32:39 -080021#include "perfetto/base/status.h"
Aaron Vaagebe927612024-04-22 10:35:27 -070022#include "perfetto/protozero/scattered_heap_buffer.h"
23#include "src/trace_processor/util/status_macros.h"
24#include "src/trace_redaction/proto_util.h"
Aaron Vaage74245392024-02-28 13:32:39 -080025
Aaron Vaage14b255d2024-02-28 09:11:00 -080026namespace perfetto::trace_redaction {
Aaron Vaageb1676f92024-04-10 11:50:36 -070027
28TracePacketFilter::~TracePacketFilter() = default;
29
Aaron Vaageda5ae112024-04-23 10:33:46 -070030base::Status TracePacketFilter::VerifyContext(const Context&) const {
31 return base::OkStatus();
32}
33
Aaron Vaage14b255d2024-02-28 09:11:00 -080034base::Status ScrubTracePacket::Transform(const Context& context,
35 std::string* packet) const {
36 if (packet == nullptr || packet->empty()) {
Aaron Vaage4351eb92024-04-12 11:58:22 -070037 return base::ErrStatus("ScrubTracePacket: null or empty packet.");
Aaron Vaage14b255d2024-02-28 09:11:00 -080038 }
39
Aaron Vaageb1676f92024-04-10 11:50:36 -070040 for (const auto& filter : filters_) {
Aaron Vaagebe927612024-04-22 10:35:27 -070041 RETURN_IF_ERROR(filter->VerifyContext(context));
42 }
Aaron Vaage14b255d2024-02-28 09:11:00 -080043
Aaron Vaagebe927612024-04-22 10:35:27 -070044 protozero::HeapBuffered<protos::pbzero::TracePacket> new_packet;
45
46 protozero::ProtoDecoder decoder(*packet);
47
48 for (auto field = decoder.ReadField(); field.valid();
49 field = decoder.ReadField()) {
50 if (KeepEvent(context, field)) {
51 proto_util::AppendField(field, new_packet.get());
Aaron Vaageb1676f92024-04-10 11:50:36 -070052 }
Aaron Vaage14b255d2024-02-28 09:11:00 -080053 }
54
Aaron Vaagebe927612024-04-22 10:35:27 -070055 packet->assign(new_packet.SerializeAsString());
Aaron Vaage14b255d2024-02-28 09:11:00 -080056 return base::OkStatus();
57}
58
Aaron Vaagebe927612024-04-22 10:35:27 -070059// Logical AND all filters.
Aaron Vaageb1676f92024-04-10 11:50:36 -070060bool ScrubTracePacket::KeepEvent(const Context& context,
Aaron Vaagebe927612024-04-22 10:35:27 -070061 const protozero::Field& field) const {
Aaron Vaageb1676f92024-04-10 11:50:36 -070062 for (const auto& filter : filters_) {
Aaron Vaagebe927612024-04-22 10:35:27 -070063 if (!filter->KeepField(context, field)) {
Aaron Vaageb1676f92024-04-10 11:50:36 -070064 return false;
65 }
66 }
67
68 return true;
69}
70
Aaron Vaage14b255d2024-02-28 09:11:00 -080071} // namespace perfetto::trace_redaction