aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Vaage <vaage@google.com>2024-04-02 10:23:07 -0700
committerAaron Vaage <vaage@google.com>2024-04-08 15:33:27 +0000
commita97a0429c32086e5f19d49a2eebd5aabb373df0b (patch)
tree5e691b1e8a43c1a81edabcecf98776edebc51b84
parent2b1117bf60971ab044e3daea63adc2b6a2b35bc9 (diff)
downloadperfetto-a97a0429c32086e5f19d49a2eebd5aabb373df0b.tar.gz
Trace Redaction - Filter print ftrace events
Print events are used as many different ways. The most common usecase is logging the begin and end events used by perfetto to display slices. Some system processes use print events to log important events (e.g. LMK). Because print statments are freeform strings but have important usecases redaction can't completely leave them as-is and can't completely remove them. The happy medium is to drop all print statements outside of the target package. Bug: 332547909 Change-Id: I7df5f1e215d4c0605302eb497cbd543aa94ff640
-rw-r--r--Android.bp1
-rw-r--r--src/trace_redaction/BUILD.gn2
-rw-r--r--src/trace_redaction/filter_ftrace_using_allowlist_integrationtest.cc2
-rw-r--r--src/trace_redaction/filter_print_events.cc63
-rw-r--r--src/trace_redaction/filter_print_events.h46
-rw-r--r--src/trace_redaction/main.cc2
-rw-r--r--src/trace_redaction/populate_allow_lists.cc1
7 files changed, 116 insertions, 1 deletions
diff --git a/Android.bp b/Android.bp
index 0c8ab4294..c3e26636a 100644
--- a/Android.bp
+++ b/Android.bp
@@ -12851,6 +12851,7 @@ filegroup {
srcs: [
"src/trace_redaction/build_timeline.cc",
"src/trace_redaction/filter_ftrace_using_allowlist.cc",
+ "src/trace_redaction/filter_print_events.cc",
"src/trace_redaction/filter_sched_waking_events.cc",
"src/trace_redaction/find_package_uid.cc",
"src/trace_redaction/optimize_timeline.cc",
diff --git a/src/trace_redaction/BUILD.gn b/src/trace_redaction/BUILD.gn
index 1f088ea41..81675b0aa 100644
--- a/src/trace_redaction/BUILD.gn
+++ b/src/trace_redaction/BUILD.gn
@@ -32,6 +32,8 @@ source_set("trace_redaction") {
"build_timeline.h",
"filter_ftrace_using_allowlist.cc",
"filter_ftrace_using_allowlist.h",
+ "filter_print_events.cc",
+ "filter_print_events.h",
"filter_sched_waking_events.cc",
"filter_sched_waking_events.h",
"find_package_uid.cc",
diff --git a/src/trace_redaction/filter_ftrace_using_allowlist_integrationtest.cc b/src/trace_redaction/filter_ftrace_using_allowlist_integrationtest.cc
index c0abf3f42..89946ae00 100644
--- a/src/trace_redaction/filter_ftrace_using_allowlist_integrationtest.cc
+++ b/src/trace_redaction/filter_ftrace_using_allowlist_integrationtest.cc
@@ -170,6 +170,7 @@ TEST_F(FilterFtraceUsingAllowlistTest, RetainsAllowedEvents) {
ASSERT_TRUE(events.count(protos::pbzero::FtraceEvent::kTimestampFieldNumber));
// These are events.
+ ASSERT_TRUE(events.count(protos::pbzero::FtraceEvent::kPrintFieldNumber));
ASSERT_TRUE(
events.count(protos::pbzero::FtraceEvent::kCpuFrequencyFieldNumber));
ASSERT_TRUE(events.count(protos::pbzero::FtraceEvent::kCpuIdleFieldNumber));
@@ -197,7 +198,6 @@ TEST_F(FilterFtraceUsingAllowlistTest, RemovesNotAllowedEvents) {
// These are events.
ASSERT_FALSE(
events.count(protos::pbzero::FtraceEvent::kOomScoreAdjUpdateFieldNumber));
- ASSERT_FALSE(events.count(protos::pbzero::FtraceEvent::kPrintFieldNumber));
ASSERT_FALSE(
events.count(protos::pbzero::FtraceEvent::kSchedProcessExitFieldNumber));
ASSERT_FALSE(
diff --git a/src/trace_redaction/filter_print_events.cc b/src/trace_redaction/filter_print_events.cc
new file mode 100644
index 000000000..de0c95268
--- /dev/null
+++ b/src/trace_redaction/filter_print_events.cc
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "src/trace_redaction/filter_print_events.h"
+
+#include "perfetto/base/logging.h"
+#include "perfetto/base/status.h"
+
+#include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
+#include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
+
+namespace perfetto::trace_redaction {
+
+base::Status FilterPrintEvents::VerifyContext(const Context& context) const {
+ if (!context.package_uid.has_value()) {
+ return base::ErrStatus("FilterPrintEvents: missing packet uid.");
+ }
+
+ if (!context.timeline) {
+ return base::ErrStatus("FilterPrintEvents: missing timeline.");
+ }
+
+ return base::OkStatus();
+}
+
+bool FilterPrintEvents::KeepEvent(const Context& context,
+ protozero::ConstBytes bytes) const {
+ PERFETTO_DCHECK(context.timeline);
+ PERFETTO_DCHECK(context.package_uid.has_value());
+
+ const auto* timeline = context.timeline.get();
+ auto package_uid = context.package_uid;
+
+ protozero::ProtoDecoder event(bytes);
+
+ // This is not a print packet. Keep the packet.
+ if (event.FindField(protos::pbzero::FtraceEvent::kPrintFieldNumber).valid()) {
+ return true;
+ }
+
+ auto time =
+ event.FindField(protos::pbzero::FtraceEvent::kTimestampFieldNumber);
+ auto pid = event.FindField(protos::pbzero::FtraceEvent::kPidFieldNumber);
+
+ // Pid + Time --> UID, if the uid matches the target package, keep the event.
+ return pid.valid() && time.valid() &&
+ timeline->Search(time.as_uint64(), pid.as_int32()).uid == package_uid;
+}
+
+} // namespace perfetto::trace_redaction
diff --git a/src/trace_redaction/filter_print_events.h b/src/trace_redaction/filter_print_events.h
new file mode 100644
index 000000000..36ef92be5
--- /dev/null
+++ b/src/trace_redaction/filter_print_events.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_TRACE_REDACTION_FILTER_PRINT_EVENTS_H_
+#define SRC_TRACE_REDACTION_FILTER_PRINT_EVENTS_H_
+
+#include "perfetto/protozero/field.h"
+#include "src/trace_redaction/scrub_ftrace_events.h"
+#include "src/trace_redaction/trace_redaction_framework.h"
+
+namespace perfetto::trace_redaction {
+
+// event {
+// timestamp: 6702093749982230
+// pid: 7947 <-- target
+// print {
+// buf: "B|7105|virtual void
+// swappy::ChoreographerThread::onChoreographer()\n"
+// }
+// }
+//
+// If the target pid doesn't belong to the target package (context.package_uid),
+// then the event will be marked as "don't keep".
+class FilterPrintEvents : public FtraceEventFilter {
+ public:
+ base::Status VerifyContext(const Context& context) const override;
+ bool KeepEvent(const Context& context,
+ protozero::ConstBytes bytes) const override;
+};
+
+} // namespace perfetto::trace_redaction
+
+#endif // SRC_TRACE_REDACTION_FILTER_PRINT_EVENTS_H_
diff --git a/src/trace_redaction/main.cc b/src/trace_redaction/main.cc
index c0061b603..c81b8bcf9 100644
--- a/src/trace_redaction/main.cc
+++ b/src/trace_redaction/main.cc
@@ -18,6 +18,7 @@
#include "perfetto/base/status.h"
#include "src/trace_redaction/build_timeline.h"
#include "src/trace_redaction/filter_ftrace_using_allowlist.h"
+#include "src/trace_redaction/filter_print_events.h"
#include "src/trace_redaction/filter_sched_waking_events.h"
#include "src/trace_redaction/find_package_uid.h"
#include "src/trace_redaction/optimize_timeline.h"
@@ -55,6 +56,7 @@ static base::Status Main(std::string_view input,
// number of events they need to iterate over.
auto scrub_ftrace_events = redactor.emplace_transform<ScrubFtraceEvents>();
scrub_ftrace_events->emplace_back<FilterFtraceUsingAllowlist>();
+ scrub_ftrace_events->emplace_back<FilterPrintEvents>();
scrub_ftrace_events->emplace_back<FilterSchedWakingEvents>();
redactor.emplace_transform<ScrubProcessTrees>();
diff --git a/src/trace_redaction/populate_allow_lists.cc b/src/trace_redaction/populate_allow_lists.cc
index f17fdcd51..18d562862 100644
--- a/src/trace_redaction/populate_allow_lists.cc
+++ b/src/trace_redaction/populate_allow_lists.cc
@@ -69,6 +69,7 @@ base::Status PopulateAllowlists::Build(Context* context) const {
protos::pbzero::FtraceEvent::kIonBufferDestroyFieldNumber,
protos::pbzero::FtraceEvent::kDmaHeapStatFieldNumber,
protos::pbzero::FtraceEvent::kRssStatThrottledFieldNumber,
+ protos::pbzero::FtraceEvent::kPrintFieldNumber,
};
// TODO: Some ftrace fields should be retained, but they carry too much risk