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_PROCESSOR_IMPORTERS_COMMON_PROCESS_TRACK_TRANSLATION_TABLE_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_PROCESS_TRACK_TRANSLATION_TABLE_H_
19 
20 #include <cstdint>
21 
22 #include "perfetto/ext/base/flat_hash_map.h"
23 #include "perfetto/ext/base/string_view.h"
24 #include "src/trace_processor/storage/trace_storage.h"
25 
26 namespace perfetto::trace_processor {
27 
28 // Tracks and stores slice translation rules. It allows Trace Processor
29 // to for example deobfuscate slice names.
30 class ProcessTrackTranslationTable {
31  public:
32   ProcessTrackTranslationTable(TraceStorage* storage);
33 
34   // If the name is not mapped to anything, assumes that no translation is
35   // necessry, and returns the raw_name.
TranslateName(StringId raw_name)36   StringId TranslateName(StringId raw_name) const {
37     const auto* mapped_name = raw_to_deobfuscated_name_.Find(raw_name);
38     return mapped_name ? *mapped_name : raw_name;
39   }
40 
AddNameTranslationRule(base::StringView raw,base::StringView deobfuscated)41   void AddNameTranslationRule(base::StringView raw,
42                               base::StringView deobfuscated) {
43     const StringId raw_id = storage_->InternString(raw);
44     const StringId deobfuscated_id = storage_->InternString(deobfuscated);
45     raw_to_deobfuscated_name_[raw_id] = deobfuscated_id;
46   }
47 
48  private:
49   TraceStorage* storage_;
50   base::FlatHashMap<StringId, StringId> raw_to_deobfuscated_name_;
51 };
52 
53 }  // namespace perfetto::trace_processor
54 
55 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_PROCESS_TRACK_TRANSLATION_TABLE_H_
56