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/process_track_translation_table.h"
20 #include "src/trace_processor/importers/common/slice_translation_table.h"
21 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
22
23 #include "protos/perfetto/trace/trace_packet.pbzero.h"
24 #include "protos/perfetto/trace/translation/translation_table.pbzero.h"
25
26 namespace perfetto {
27 namespace trace_processor {
28
29 using perfetto::protos::pbzero::TracePacket;
30
TranslationTableModule(TraceProcessorContext * context)31 TranslationTableModule::TranslationTableModule(TraceProcessorContext* context)
32 : context_(context) {
33 RegisterForField(TracePacket::kTranslationTableFieldNumber, context);
34 }
35
36 TranslationTableModule::~TranslationTableModule() = default;
37
TokenizePacket(const protos::pbzero::TracePacket_Decoder & decoder,TraceBlobView *,int64_t,RefPtr<PacketSequenceStateGeneration>,uint32_t field_id)38 ModuleResult TranslationTableModule::TokenizePacket(
39 const protos::pbzero::TracePacket_Decoder& decoder,
40 TraceBlobView* /*packet*/,
41 int64_t /*packet_timestamp*/,
42 RefPtr<PacketSequenceStateGeneration> /*state*/,
43 uint32_t field_id) {
44 if (field_id != TracePacket::kTranslationTableFieldNumber) {
45 return ModuleResult::Ignored();
46 }
47 const auto translation_table =
48 protos::pbzero::TranslationTable::Decoder(decoder.translation_table());
49 if (translation_table.has_chrome_histogram()) {
50 ParseChromeHistogramRules(translation_table.chrome_histogram());
51 } else if (translation_table.has_chrome_user_event()) {
52 ParseChromeUserEventRules(translation_table.chrome_user_event());
53 } else if (translation_table.has_chrome_performance_mark()) {
54 ParseChromePerformanceMarkRules(
55 translation_table.chrome_performance_mark());
56 } else if (translation_table.has_slice_name()) {
57 ParseSliceNameRules(translation_table.slice_name());
58 } else if (translation_table.has_process_track_name()) {
59 ParseProcessTrackNameRules(translation_table.process_track_name());
60 } else if (translation_table.has_chrome_study()) {
61 ParseChromeStudyRules(translation_table.chrome_study());
62 }
63 return ModuleResult::Handled();
64 }
65
ParseChromeHistogramRules(protozero::ConstBytes bytes)66 void TranslationTableModule::ParseChromeHistogramRules(
67 protozero::ConstBytes bytes) {
68 const auto chrome_histogram =
69 protos::pbzero::ChromeHistorgramTranslationTable::Decoder(bytes);
70 for (auto it = chrome_histogram.hash_to_name(); it; ++it) {
71 protos::pbzero::ChromeHistorgramTranslationTable::HashToNameEntry::Decoder
72 entry(*it);
73 context_->args_translation_table->AddChromeHistogramTranslationRule(
74 entry.key(), entry.value());
75 }
76 }
77
ParseChromeUserEventRules(protozero::ConstBytes bytes)78 void TranslationTableModule::ParseChromeUserEventRules(
79 protozero::ConstBytes bytes) {
80 const auto chrome_user_event =
81 protos::pbzero::ChromeUserEventTranslationTable::Decoder(bytes);
82 for (auto it = chrome_user_event.action_hash_to_name(); it; ++it) {
83 protos::pbzero::ChromeUserEventTranslationTable::ActionHashToNameEntry::
84 Decoder entry(*it);
85 context_->args_translation_table->AddChromeUserEventTranslationRule(
86 entry.key(), entry.value());
87 }
88 }
89
ParseChromePerformanceMarkRules(protozero::ConstBytes bytes)90 void TranslationTableModule::ParseChromePerformanceMarkRules(
91 protozero::ConstBytes bytes) {
92 const auto chrome_performance_mark =
93 protos::pbzero::ChromePerformanceMarkTranslationTable::Decoder(bytes);
94 for (auto it = chrome_performance_mark.site_hash_to_name(); it; ++it) {
95 protos::pbzero::ChromePerformanceMarkTranslationTable::SiteHashToNameEntry::
96 Decoder entry(*it);
97 context_->args_translation_table
98 ->AddChromePerformanceMarkSiteTranslationRule(entry.key(),
99 entry.value());
100 }
101 for (auto it = chrome_performance_mark.mark_hash_to_name(); it; ++it) {
102 protos::pbzero::ChromePerformanceMarkTranslationTable::MarkHashToNameEntry::
103 Decoder entry(*it);
104 context_->args_translation_table
105 ->AddChromePerformanceMarkMarkTranslationRule(entry.key(),
106 entry.value());
107 }
108 }
109
ParseSliceNameRules(protozero::ConstBytes bytes)110 void TranslationTableModule::ParseSliceNameRules(protozero::ConstBytes bytes) {
111 const auto slice_name =
112 protos::pbzero::SliceNameTranslationTable::Decoder(bytes);
113 for (auto it = slice_name.raw_to_deobfuscated_name(); it; ++it) {
114 protos::pbzero::SliceNameTranslationTable::RawToDeobfuscatedNameEntry::
115 Decoder entry(*it);
116 context_->slice_translation_table->AddNameTranslationRule(entry.key(),
117 entry.value());
118 }
119 }
120
ParseProcessTrackNameRules(protozero::ConstBytes bytes)121 void TranslationTableModule::ParseProcessTrackNameRules(
122 protozero::ConstBytes bytes) {
123 const auto process_track_name =
124 protos::pbzero::ProcessTrackNameTranslationTable::Decoder(bytes);
125 for (auto it = process_track_name.raw_to_deobfuscated_name(); it; ++it) {
126 protos::pbzero::ProcessTrackNameTranslationTable::
127 RawToDeobfuscatedNameEntry::Decoder entry(*it);
128 context_->process_track_translation_table->AddNameTranslationRule(
129 entry.key(), entry.value());
130 }
131 }
132
ParseChromeStudyRules(protozero::ConstBytes bytes)133 void TranslationTableModule::ParseChromeStudyRules(
134 protozero::ConstBytes bytes) {
135 const auto chrome_study =
136 protos::pbzero::ChromeStudyTranslationTable::Decoder(bytes);
137 for (auto it = chrome_study.hash_to_name(); it; ++it) {
138 protos::pbzero::ChromeStudyTranslationTable::HashToNameEntry::Decoder entry(
139 *it);
140 context_->args_translation_table->AddChromeStudyTranslationRule(
141 entry.key(), entry.value());
142 }
143 }
144
145 } // namespace trace_processor
146 } // namespace perfetto
147