1 /*
2 * Copyright (C) 2022 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 #include "src/trace_processor/importers/proto/translation_table_module.h"
17
18 #include "src/trace_processor/importers/common/args_translation_table.h"
19 #include "src/trace_processor/importers/common/slice_translation_table.h"
20
21 #include "protos/perfetto/trace/trace_packet.pbzero.h"
22 #include "protos/perfetto/trace/translation/translation_table.pbzero.h"
23
24 namespace perfetto {
25 namespace trace_processor {
26
27 using perfetto::protos::pbzero::TracePacket;
28
TranslationTableModule(TraceProcessorContext * context)29 TranslationTableModule::TranslationTableModule(TraceProcessorContext* context)
30 : context_(context) {
31 RegisterForField(TracePacket::kTranslationTableFieldNumber, context);
32 }
33
34 TranslationTableModule::~TranslationTableModule() = default;
35
TokenizePacket(const protos::pbzero::TracePacket_Decoder & decoder,TraceBlobView *,int64_t,PacketSequenceState *,uint32_t field_id)36 ModuleResult TranslationTableModule::TokenizePacket(
37 const protos::pbzero::TracePacket_Decoder& decoder,
38 TraceBlobView* /*packet*/,
39 int64_t /*packet_timestamp*/,
40 PacketSequenceState* /*state*/,
41 uint32_t field_id) {
42 if (field_id != TracePacket::kTranslationTableFieldNumber) {
43 return ModuleResult::Ignored();
44 }
45 const auto translation_table =
46 protos::pbzero::TranslationTable::Decoder(decoder.translation_table());
47 if (translation_table.has_chrome_histogram()) {
48 ParseChromeHistogramRules(translation_table.chrome_histogram());
49 } else if (translation_table.has_chrome_user_event()) {
50 ParseChromeUserEventRules(translation_table.chrome_user_event());
51 } else if (translation_table.has_chrome_performance_mark()) {
52 ParseChromePerformanceMarkRules(
53 translation_table.chrome_performance_mark());
54 } else if (translation_table.has_slice_name()) {
55 ParseSliceNameRules(translation_table.slice_name());
56 }
57 return ModuleResult::Handled();
58 }
59
ParseChromeHistogramRules(protozero::ConstBytes bytes)60 void TranslationTableModule::ParseChromeHistogramRules(
61 protozero::ConstBytes bytes) {
62 const auto chrome_histogram =
63 protos::pbzero::ChromeHistorgramTranslationTable::Decoder(bytes);
64 for (auto it = chrome_histogram.hash_to_name(); it; ++it) {
65 protos::pbzero::ChromeHistorgramTranslationTable::HashToNameEntry::Decoder
66 entry(*it);
67 context_->args_translation_table->AddChromeHistogramTranslationRule(
68 entry.key(), entry.value());
69 }
70 }
71
ParseChromeUserEventRules(protozero::ConstBytes bytes)72 void TranslationTableModule::ParseChromeUserEventRules(
73 protozero::ConstBytes bytes) {
74 const auto chrome_user_event =
75 protos::pbzero::ChromeUserEventTranslationTable::Decoder(bytes);
76 for (auto it = chrome_user_event.action_hash_to_name(); it; ++it) {
77 protos::pbzero::ChromeUserEventTranslationTable::ActionHashToNameEntry::
78 Decoder entry(*it);
79 context_->args_translation_table->AddChromeUserEventTranslationRule(
80 entry.key(), entry.value());
81 }
82 }
83
ParseChromePerformanceMarkRules(protozero::ConstBytes bytes)84 void TranslationTableModule::ParseChromePerformanceMarkRules(
85 protozero::ConstBytes bytes) {
86 const auto chrome_performance_mark =
87 protos::pbzero::ChromePerformanceMarkTranslationTable::Decoder(bytes);
88 for (auto it = chrome_performance_mark.site_hash_to_name(); it; ++it) {
89 protos::pbzero::ChromePerformanceMarkTranslationTable::SiteHashToNameEntry::
90 Decoder entry(*it);
91 context_->args_translation_table
92 ->AddChromePerformanceMarkSiteTranslationRule(entry.key(),
93 entry.value());
94 }
95 for (auto it = chrome_performance_mark.mark_hash_to_name(); it; ++it) {
96 protos::pbzero::ChromePerformanceMarkTranslationTable::MarkHashToNameEntry::
97 Decoder entry(*it);
98 context_->args_translation_table
99 ->AddChromePerformanceMarkMarkTranslationRule(entry.key(),
100 entry.value());
101 }
102 }
103
ParseSliceNameRules(protozero::ConstBytes bytes)104 void TranslationTableModule::ParseSliceNameRules(protozero::ConstBytes bytes) {
105 const auto slice_name =
106 protos::pbzero::SliceNameTranslationTable::Decoder(bytes);
107 for (auto it = slice_name.raw_to_deobfuscated_name(); it; ++it) {
108 protos::pbzero::SliceNameTranslationTable::RawToDeobfuscatedNameEntry::
109 Decoder entry(*it);
110 context_->slice_translation_table->AddNameTranslationRule(entry.key(),
111 entry.value());
112 }
113 }
114
115 } // namespace trace_processor
116 } // namespace perfetto
117