xref: /aosp_15_r20/external/perfetto/src/trace_redaction/redact_ftrace_events.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_REDACTION_REDACT_FTRACE_EVENTS_H_
18 #define SRC_TRACE_REDACTION_REDACT_FTRACE_EVENTS_H_
19 
20 #include <string>
21 
22 #include "perfetto/protozero/field.h"
23 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
24 #include "src/trace_redaction/redact_sched_events.h"
25 #include "src/trace_redaction/trace_redaction_framework.h"
26 
27 namespace perfetto::trace_redaction {
28 
29 class FilterFtraceUsingSuspendResume : public FtraceEventFilter {
30  public:
31   bool Includes(const Context& context, protozero::Field event) const override;
32 };
33 
34 // Discard all rss events not belonging to the target package.
35 class FilterRss : public FtraceEventFilter {
36  public:
37   bool Includes(const Context& context, protozero::Field event) const override;
38 };
39 
40 // Filters ftrace events and modifies remaining events before writing them to
41 // the packet. Only one filter and/or writer can be assigned to provide finer
42 // grain control.
43 class RedactFtraceEvents : public TransformPrimitive {
44  public:
45   base::Status Transform(const Context& context,
46                          std::string* packet) const override;
47 
48   // Selects which ftrace events should be redacted. All non-ftrace events are
49   // appended to the new packet.
50   template <typename Filter>
emplace_ftrace_filter()51   void emplace_ftrace_filter() {
52     filter_ = std::make_unique<Filter>();
53   }
54 
55   // For ftrace events that pass the filter, they go through this modifier which
56   // will optionally modify the event before adding it to the event bundle (or
57   // even drop it).
58   template <typename Modifier>
emplace_post_filter_modifier()59   void emplace_post_filter_modifier() {
60     modifier_ = std::make_unique<Modifier>();
61   }
62 
63  private:
64   base::Status OnFtraceEvents(const Context& context,
65                               protozero::Field ftrace_events,
66                               protos::pbzero::FtraceEventBundle* message) const;
67 
68   void OnFtraceEvent(const Context& context,
69                      const protos::pbzero::FtraceEventBundle::Decoder& bundle,
70                      protozero::Field event,
71                      protos::pbzero::FtraceEventBundle* parent_message) const;
72 
73   std::unique_ptr<FtraceEventFilter> filter_;
74   std::unique_ptr<PidCommModifier> modifier_;
75 };
76 
77 }  // namespace perfetto::trace_redaction
78 
79 #endif  // SRC_TRACE_REDACTION_REDACT_FTRACE_EVENTS_H_
80