1 /*
2  * Copyright (C) 2020 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 #pragma once
18 
19 #include <inttypes.h>
20 
21 #include <string>
22 #include <string_view>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "dso.h"
27 #include "thread_tree.h"
28 
29 namespace simpleperf {
30 
31 enum class CallChainExecutionType {
32   NATIVE_METHOD,
33   INTERPRETED_JVM_METHOD,
34   JIT_JVM_METHOD,
35   // ART methods near interpreted/JIT JVM methods. They're shown only when RemoveArtFrame = false.
36   ART_METHOD,
37 };
38 
39 struct CallChainReportEntry {
40   uint64_t ip = 0;
41   const Symbol* symbol = nullptr;
42   Dso* dso = nullptr;
43   const char* dso_name = nullptr;
44   uint64_t vaddr_in_file = 0;
45   const MapEntry* map = nullptr;
46   CallChainExecutionType execution_type = CallChainExecutionType::NATIVE_METHOD;
47 };
48 
49 class CallChainReportBuilder {
50  public:
CallChainReportBuilder(ThreadTree & thread_tree)51   CallChainReportBuilder(ThreadTree& thread_tree) : thread_tree_(thread_tree) {}
52   // If true, remove interpreter frames both before and after a Java frame.
53   // Default is true.
SetRemoveArtFrame(bool enable)54   void SetRemoveArtFrame(bool enable) { remove_art_frame_ = enable; }
55   // If true, convert a JIT method into its corresponding interpreted Java method. So they can be
56   // merged in reports like flamegraph. Default is true.
SetConvertJITFrame(bool enable)57   void SetConvertJITFrame(bool enable) { convert_jit_frame_ = enable; }
58   // Add proguard mapping.txt to de-obfuscate minified symbols.
59   bool AddProguardMappingFile(std::string_view mapping_file);
60   std::vector<CallChainReportEntry> Build(const ThreadEntry* thread,
61                                           const std::vector<uint64_t>& ips, size_t kernel_ip_count);
62 
63  private:
64   struct JavaMethod {
65     Dso* dso;
66     const Symbol* symbol;
JavaMethodJavaMethod67     JavaMethod(Dso* dso, const Symbol* symbol) : dso(dso), symbol(symbol) {}
68   };
69 
70   struct ProguardMappingClass {
71     std::string original_classname;
72     // Map from minified method names to original method names.
73     std::unordered_map<std::string, std::string> method_map;
74   };
75 
76   void MarkArtFrame(std::vector<CallChainReportEntry>& callchain);
77   void ConvertJITFrame(std::vector<CallChainReportEntry>& callchain);
78   void CollectJavaMethods();
79   void DeObfuscateJavaMethods(std::vector<CallChainReportEntry>& callchain);
80 
81   ThreadTree& thread_tree_;
82   bool remove_art_frame_ = true;
83   bool convert_jit_frame_ = true;
84   bool java_method_initialized_ = false;
85   std::unordered_map<std::string, JavaMethod> java_method_map_;
86   // Map from minified class names to ProguardMappingClass.
87   std::unordered_map<std::string, ProguardMappingClass> proguard_class_map_;
88 };
89 
90 }  // namespace simpleperf
91