• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_ARGS_TRANSLATION_TABLE_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_ARGS_TRANSLATION_TABLE_H_
19 
20 #include <cstdint>
21 #include <optional>
22 
23 #include "perfetto/ext/base/flat_hash_map.h"
24 #include "perfetto/ext/base/string_view.h"
25 #include "src/trace_processor/importers/common/args_tracker.h"
26 #include "src/trace_processor/importers/common/deobfuscation_mapping_table.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 #include "src/trace_processor/util/proto_to_args_parser.h"
29 
30 template <>
31 struct std::hash<std::pair<perfetto::trace_processor::MappingId, uint64_t>> {
32   size_t operator()(const std::pair<perfetto::trace_processor::MappingId,
33                                     uint64_t>& p) const {
34     return std::hash<perfetto::trace_processor::MappingId>{}(p.first) ^
35            std::hash<uint64_t>{}(p.second);
36   }
37 };
38 
39 namespace perfetto {
40 namespace trace_processor {
41 
42 // Tracks and stores args translation rules. It allows Trace Processor
43 // to map for example hashes to their names.
44 class ArgsTranslationTable {
45  public:
46   using Key = util::ProtoToArgsParser::Key;
47   using NativeSymbolKey = std::pair<MappingId, uint64_t>;
48   struct SourceLocation {
49     std::string file_name;
50     std::string function_name;
51     uint32_t line_number;
52   };
53 
54   explicit ArgsTranslationTable(TraceStorage* storage);
55 
56   // Returns true if an arg with the given key and type requires translation.
57   bool NeedsTranslation(StringId flat_key_id,
58                         StringId key_id,
59                         Variadic::Type type) const;
60 
61   void TranslateArgs(const ArgsTracker::CompactArgSet& arg_set,
62                      ArgsTracker::BoundInserter& inserter) const;
63 
64   void AddChromeHistogramTranslationRule(uint64_t hash, base::StringView name) {
65     chrome_histogram_hash_to_name_.Insert(hash, name.ToStdString());
66   }
67   void AddChromeUserEventTranslationRule(uint64_t hash,
68                                          base::StringView action) {
69     chrome_user_event_hash_to_action_.Insert(hash, action.ToStdString());
70   }
71   void AddChromePerformanceMarkSiteTranslationRule(uint64_t hash,
72                                                    base::StringView name) {
73     chrome_performance_mark_site_hash_to_name_.Insert(hash, name.ToStdString());
74   }
75   void AddChromePerformanceMarkMarkTranslationRule(uint64_t hash,
76                                                    base::StringView name) {
77     chrome_performance_mark_mark_hash_to_name_.Insert(hash, name.ToStdString());
78   }
79   void AddNativeSymbolTranslationRule(MappingId mapping_id,
80                                       uint64_t rel_pc,
81                                       const SourceLocation& loc) {
82     native_symbol_to_location_.Insert(std::make_pair(mapping_id, rel_pc), loc);
83   }
84   void AddDeobfuscationMappingTable(
85       DeobfuscationMappingTable deobfuscation_mapping_table) {
86     deobfuscation_mapping_table_ = std::move(deobfuscation_mapping_table);
87   }
88 
89   std::optional<base::StringView> TranslateChromeHistogramHashForTesting(
90       uint64_t hash) const {
91     return TranslateChromeHistogramHash(hash);
92   }
93   std::optional<base::StringView> TranslateChromeUserEventHashForTesting(
94       uint64_t hash) const {
95     return TranslateChromeUserEventHash(hash);
96   }
97   std::optional<base::StringView>
98   TranslateChromePerformanceMarkSiteHashForTesting(uint64_t hash) const {
99     return TranslateChromePerformanceMarkSiteHash(hash);
100   }
101   std::optional<base::StringView>
102   TranslateChromePerformanceMarkMarkHashForTesting(uint64_t hash) const {
103     return TranslateChromePerformanceMarkMarkHash(hash);
104   }
105   std::optional<StringId> TranslateClassNameForTesting(
106       StringId obfuscated_class_name_id) const {
107     return TranslateClassName(obfuscated_class_name_id);
108   }
109 
110  private:
111   enum class KeyType {
112     kChromeHistogramHash = 0,
113     kChromeUserEventHash = 1,
114     kChromePerformanceMarkMarkHash = 2,
115     kChromePerformanceMarkSiteHash = 3,
116     kMojoMethodMappingId = 4,
117     kMojoMethodRelPc = 5,
118     kClassName = 6,
119   };
120 
121   static constexpr char kChromeHistogramHashKey[] =
122       "chrome_histogram_sample.name_hash";
123   static constexpr char kChromeHistogramNameKey[] =
124       "chrome_histogram_sample.name";
125 
126   static constexpr char kChromeUserEventHashKey[] =
127       "chrome_user_event.action_hash";
128   static constexpr char kChromeUserEventActionKey[] =
129       "chrome_user_event.action";
130 
131   static constexpr char kChromePerformanceMarkSiteHashKey[] =
132       "chrome_hashed_performance_mark.site_hash";
133   static constexpr char kChromePerformanceMarkSiteKey[] =
134       "chrome_hashed_performance_mark.site";
135 
136   static constexpr char kChromePerformanceMarkMarkHashKey[] =
137       "chrome_hashed_performance_mark.mark_hash";
138   static constexpr char kChromePerformanceMarkMarkKey[] =
139       "chrome_hashed_performance_mark.mark";
140 
141   static constexpr char kMojoMethodMappingIdKey[] =
142       "chrome_mojo_event_info.mojo_interface_method.native_symbol.mapping_id";
143   static constexpr char kMojoMethodRelPcKey[] =
144       "chrome_mojo_event_info.mojo_interface_method.native_symbol.rel_pc";
145   static constexpr char kMojoMethodNameKey[] =
146       "chrome_mojo_event_info.mojo_method_name";
147   static constexpr char kMojoIntefaceTagKey[] =
148       "chrome_mojo_event_info.mojo_interface_tag";
149 
150   static constexpr char kObfuscatedViewDumpClassNameFlatKey[] =
151       "android_view_dump.activity.view.class_name";
152 
153   TraceStorage* storage_;
154   StringId interned_chrome_histogram_hash_key_;
155   StringId interned_chrome_histogram_name_key_;
156   StringId interned_chrome_user_event_hash_key_;
157   StringId interned_chrome_user_event_action_key_;
158   StringId interned_chrome_performance_mark_site_hash_key_;
159   StringId interned_chrome_performance_mark_site_key_;
160   StringId interned_chrome_performance_mark_mark_hash_key_;
161   StringId interned_chrome_performance_mark_mark_key_;
162 
163   StringId interned_mojo_method_mapping_id_;
164   StringId interned_mojo_method_rel_pc_;
165   StringId interned_mojo_method_name_;
166   StringId interned_mojo_interface_tag_;
167 
168   // A "flat_key" of an argument from the "args" table that has to be
169   // deobfuscated. A Java class name must be contained in this argument.
170   StringId interned_obfuscated_view_dump_class_name_flat_key_;
171 
172   base::FlatHashMap<uint64_t, std::string> chrome_histogram_hash_to_name_;
173   base::FlatHashMap<uint64_t, std::string> chrome_user_event_hash_to_action_;
174   base::FlatHashMap<uint64_t, std::string>
175       chrome_performance_mark_site_hash_to_name_;
176   base::FlatHashMap<uint64_t, std::string>
177       chrome_performance_mark_mark_hash_to_name_;
178   base::FlatHashMap<NativeSymbolKey, SourceLocation> native_symbol_to_location_;
179   // A translation mapping for obfuscated Java class names and its members.
180   DeobfuscationMappingTable deobfuscation_mapping_table_;
181 
182   // Returns the corresponding SupportedKey enum if the table knows how to
183   // translate the argument with the given key and type, and std::nullopt
184   // otherwise.
185   std::optional<KeyType> KeyIdAndTypeToEnum(StringId flat_key_id,
186                                             StringId key_id,
187                                             Variadic::Type type) const;
188 
189   std::optional<base::StringView> TranslateChromeHistogramHash(
190       uint64_t hash) const;
191   std::optional<base::StringView> TranslateChromeUserEventHash(
192       uint64_t hash) const;
193   std::optional<base::StringView> TranslateChromePerformanceMarkSiteHash(
194       uint64_t hash) const;
195   std::optional<base::StringView> TranslateChromePerformanceMarkMarkHash(
196       uint64_t hash) const;
197   std::optional<SourceLocation> TranslateNativeSymbol(MappingId mapping_id,
198                                                       uint64_t rel_pc) const;
199 
200   // Returns the deobfuscated name of a Java class or std::nullopt if
201   // translation is not found.
202   std::optional<StringId> TranslateClassName(
203       StringId obfuscated_class_name_id) const;
204 
205   void EmitMojoMethodLocation(std::optional<uint64_t> mapping_id,
206                               std::optional<uint64_t> rel_pc,
207                               ArgsTracker::BoundInserter& inserter) const;
208 };
209 
210 }  // namespace trace_processor
211 }  // namespace perfetto
212 
213 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_ARGS_TRANSLATION_TABLE_H_
214