Aaron Vaage | 14b255d | 2024-02-28 09:11:00 -0800 | [diff] [blame] | 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 | |
Aaron Vaage | 7424539 | 2024-02-28 13:32:39 -0800 | [diff] [blame] | 17 | #include <string> |
| 18 | |
Aaron Vaage | 14b255d | 2024-02-28 09:11:00 -0800 | [diff] [blame] | 19 | #include "src/trace_redaction/scrub_trace_packet.h" |
| 20 | |
Aaron Vaage | 7424539 | 2024-02-28 13:32:39 -0800 | [diff] [blame] | 21 | #include "perfetto/base/status.h" |
Aaron Vaage | be92761 | 2024-04-22 10:35:27 -0700 | [diff] [blame] | 22 | #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 Vaage | 7424539 | 2024-02-28 13:32:39 -0800 | [diff] [blame] | 25 | |
Aaron Vaage | 14b255d | 2024-02-28 09:11:00 -0800 | [diff] [blame] | 26 | namespace perfetto::trace_redaction { |
Aaron Vaage | b1676f9 | 2024-04-10 11:50:36 -0700 | [diff] [blame] | 27 | |
| 28 | TracePacketFilter::~TracePacketFilter() = default; |
| 29 | |
Aaron Vaage | da5ae11 | 2024-04-23 10:33:46 -0700 | [diff] [blame] | 30 | base::Status TracePacketFilter::VerifyContext(const Context&) const { |
| 31 | return base::OkStatus(); |
| 32 | } |
| 33 | |
Aaron Vaage | 14b255d | 2024-02-28 09:11:00 -0800 | [diff] [blame] | 34 | base::Status ScrubTracePacket::Transform(const Context& context, |
| 35 | std::string* packet) const { |
| 36 | if (packet == nullptr || packet->empty()) { |
Aaron Vaage | 4351eb9 | 2024-04-12 11:58:22 -0700 | [diff] [blame] | 37 | return base::ErrStatus("ScrubTracePacket: null or empty packet."); |
Aaron Vaage | 14b255d | 2024-02-28 09:11:00 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Aaron Vaage | b1676f9 | 2024-04-10 11:50:36 -0700 | [diff] [blame] | 40 | for (const auto& filter : filters_) { |
Aaron Vaage | be92761 | 2024-04-22 10:35:27 -0700 | [diff] [blame] | 41 | RETURN_IF_ERROR(filter->VerifyContext(context)); |
| 42 | } |
Aaron Vaage | 14b255d | 2024-02-28 09:11:00 -0800 | [diff] [blame] | 43 | |
Aaron Vaage | be92761 | 2024-04-22 10:35:27 -0700 | [diff] [blame] | 44 | 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 Vaage | b1676f9 | 2024-04-10 11:50:36 -0700 | [diff] [blame] | 52 | } |
Aaron Vaage | 14b255d | 2024-02-28 09:11:00 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Aaron Vaage | be92761 | 2024-04-22 10:35:27 -0700 | [diff] [blame] | 55 | packet->assign(new_packet.SerializeAsString()); |
Aaron Vaage | 14b255d | 2024-02-28 09:11:00 -0800 | [diff] [blame] | 56 | return base::OkStatus(); |
| 57 | } |
| 58 | |
Aaron Vaage | be92761 | 2024-04-22 10:35:27 -0700 | [diff] [blame] | 59 | // Logical AND all filters. |
Aaron Vaage | b1676f9 | 2024-04-10 11:50:36 -0700 | [diff] [blame] | 60 | bool ScrubTracePacket::KeepEvent(const Context& context, |
Aaron Vaage | be92761 | 2024-04-22 10:35:27 -0700 | [diff] [blame] | 61 | const protozero::Field& field) const { |
Aaron Vaage | b1676f9 | 2024-04-10 11:50:36 -0700 | [diff] [blame] | 62 | for (const auto& filter : filters_) { |
Aaron Vaage | be92761 | 2024-04-22 10:35:27 -0700 | [diff] [blame] | 63 | if (!filter->KeepField(context, field)) { |
Aaron Vaage | b1676f9 | 2024-04-10 11:50:36 -0700 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
Aaron Vaage | 14b255d | 2024-02-28 09:11:00 -0800 | [diff] [blame] | 71 | } // namespace perfetto::trace_redaction |