xref: /aosp_15_r20/system/extras/simpleperf/dso.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2015 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 SIMPLE_PERF_DSO_H_
18 #define SIMPLE_PERF_DSO_H_
19 
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <string_view>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include <android-base/file.h>
28 #include <android-base/logging.h>
29 
30 #include "build_id.h"
31 #include "kallsyms.h"
32 #include "read_elf.h"
33 
34 namespace simpleperf {
35 namespace simpleperf_dso_impl {
36 
37 // Find elf files with symbol table and debug information.
38 class DebugElfFileFinder {
39  public:
40   void Reset();
AllowMismatchedBuildId()41   void AllowMismatchedBuildId() { allow_mismatched_build_id_ = true; }
42   bool SetSymFsDir(const std::string& symfs_dir);
43   bool AddSymbolDir(const std::string& symbol_dir);
44   void SetVdsoFile(const std::string& vdso_file, bool is_64bit);
45   std::string FindDebugFile(const std::string& dso_path, bool force_64bit, BuildId& build_id);
46   // Only for testing
47   std::string GetPathInSymFsDir(const std::string& path);
48 
49  private:
50   void CollectBuildIdInDir(const std::string& dir);
51   std::optional<std::string> SearchFileMapByPath(std::string_view path);
52   bool CheckDebugFilePath(const std::string& path, BuildId& build_id,
53                           bool report_build_id_mismatch);
54 
55   bool allow_mismatched_build_id_ = false;
56   std::string vdso_64bit_;
57   std::string vdso_32bit_;
58   std::string symfs_dir_;
59   std::unordered_map<std::string, std::string> build_id_to_file_map_;
60   std::vector<std::string> no_build_id_files_;
61 };
62 
63 }  // namespace simpleperf_dso_impl
64 
65 struct Symbol {
66   uint64_t addr;
67   // TODO: make len uint32_t.
68   uint64_t len;
69 
70   Symbol(std::string_view name, uint64_t addr, uint64_t len);
NameSymbol71   const char* Name() const { return name_; }
72 
73   const char* DemangledName() const;
74   void SetDemangledName(std::string_view name) const;
75   // Return function name without signature.
76   std::string_view FunctionName() const;
77 
HasDumpIdSymbol78   bool HasDumpId() const { return dump_id_ != UINT_MAX; }
79 
GetDumpIdSymbol80   bool GetDumpId(uint32_t* pdump_id) const {
81     if (!HasDumpId()) {
82       return false;
83     }
84     *pdump_id = dump_id_;
85     return true;
86   }
87 
CompareByDumpIdSymbol88   static bool CompareByDumpId(const Symbol* s1, const Symbol* s2) {
89     uint32_t id1 = UINT_MAX;
90     s1->GetDumpId(&id1);
91     uint32_t id2 = UINT_MAX;
92     s2->GetDumpId(&id2);
93     return id1 < id2;
94   }
95 
CompareByAddrSymbol96   static bool CompareByAddr(const Symbol* s1, const Symbol* s2) { return s1->addr < s2->addr; }
97 
CompareValueByAddrSymbol98   static bool CompareValueByAddr(const Symbol& s1, const Symbol& s2) { return s1.addr < s2.addr; }
99 
100  private:
101   const char* name_;
102   mutable const char* demangled_name_;
103   mutable uint32_t dump_id_;
104 
105   friend class Dso;
106 };
107 
108 enum DsoType {
109   DSO_KERNEL,
110   DSO_KERNEL_MODULE,
111   DSO_ELF_FILE,
112   DSO_DEX_FILE,  // For files containing dex files, like .vdex files.
113   DSO_SYMBOL_MAP_FILE,
114   DSO_UNKNOWN_FILE,
115   // DSO_UNKNOWN_FILE is written to the file feature section in recording files. Changing its value
116   // may cause compatibility issue. So put new DsoTypes below.
117 };
118 
119 class Dso {
120  public:
121   static void SetDemangle(bool demangle);
122   static std::string Demangle(const std::string& name);
123   // SymFsDir is used to provide an alternative root directory looking for files with symbols.
124   // For example, if we are searching symbols for /system/lib/libc.so and SymFsDir is /data/symbols,
125   // then we will also search file /data/symbols/system/lib/libc.so.
126   static bool SetSymFsDir(const std::string& symfs_dir);
127   // SymbolDir is used to add a directory containing files with symbols. Each file under it will
128   // be searched recursively to build a build_id_map.
129   static bool AddSymbolDir(const std::string& symbol_dir);
130   static void SetVmlinux(const std::string& vmlinux);
SetKallsyms(std::string kallsyms)131   static void SetKallsyms(std::string kallsyms) {
132     if (!kallsyms.empty()) {
133       kallsyms_ = std::move(kallsyms);
134     }
135   }
136   static void AllowMismatchedBuildId();
137   static void SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids);
138   static BuildId FindExpectedBuildIdForPath(const std::string& path);
139   static void SetVdsoFile(const std::string& vdso_file, bool is_64bit);
140 
141   static std::unique_ptr<Dso> CreateDso(DsoType dso_type, const std::string& dso_path,
142                                         bool force_64bit = false);
143   static std::unique_ptr<Dso> CreateDsoWithBuildId(DsoType dso_type, const std::string& dso_path,
144                                                    BuildId& build_id);
145   static std::unique_ptr<Dso> CreateKernelModuleDso(const std::string& dso_path,
146                                                     uint64_t memory_start, uint64_t memory_end,
147                                                     Dso* kernel_dso);
148   virtual ~Dso();
149 
type()150   DsoType type() const { return type_; }
151 
152   // Return the path recorded in perf.data.
Path()153   const std::string& Path() const { return path_; }
154   // Return the path containing symbol table and debug information.
GetDebugFilePath()155   const std::string& GetDebugFilePath() const {
156     if (!debug_file_path_.has_value()) {
157       debug_file_path_ = FindDebugFilePath();
158     }
159     return debug_file_path_.value();
160   }
161 
162   // Return the path beautified for reporting.
GetReportPath()163   virtual std::string_view GetReportPath() const { return Path(); }
164   // Return the file name without directory info.
FileName()165   const std::string& FileName() const { return file_name_; }
166 
HasDumpId()167   bool HasDumpId() { return dump_id_ != UINT_MAX; }
168 
GetDumpId(uint32_t * pdump_id)169   bool GetDumpId(uint32_t* pdump_id) {
170     if (!HasDumpId()) {
171       return false;
172     }
173     *pdump_id = dump_id_;
174     return true;
175   }
176 
177   uint32_t CreateDumpId();
178   uint32_t CreateSymbolDumpId(const Symbol* symbol);
179 
SetMinExecutableVaddr(uint64_t,uint64_t)180   virtual void SetMinExecutableVaddr(uint64_t, uint64_t) {}
GetMinExecutableVaddr(uint64_t * min_vaddr,uint64_t * file_offset)181   virtual void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* file_offset) {
182     *min_vaddr = 0;
183     *file_offset = 0;
184   }
AddDexFileOffset(uint64_t)185   virtual void AddDexFileOffset(uint64_t) {}
DexFileOffsets()186   virtual const std::vector<uint64_t>* DexFileOffsets() { return nullptr; }
187 
188   virtual uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) = 0;
189   virtual std::optional<uint64_t> IpToFileOffset(uint64_t ip, uint64_t map_start,
190                                                  uint64_t map_pgoff);
191 
192   const Symbol* FindSymbol(uint64_t vaddr_in_dso);
193   void LoadSymbols();
GetSymbols()194   const std::vector<Symbol>& GetSymbols() const { return symbols_; }
195   void SetSymbols(std::vector<Symbol>* symbols);
196 
197   // Create a symbol for a virtual address which can't find a corresponding
198   // symbol in symbol table.
199   void AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name);
200   bool IsForJavaMethod() const;
201 
202  protected:
203   static bool demangle_;
204   static std::string vmlinux_;
205   static std::string kallsyms_;
206   static std::unordered_map<std::string, BuildId> build_id_map_;
207   static size_t dso_count_;
208   static uint32_t g_dump_id_;
209   static simpleperf_dso_impl::DebugElfFileFinder debug_elf_file_finder_;
210 
211   Dso(DsoType type, const std::string& path);
212   BuildId GetExpectedBuildId() const;
213 
FindDebugFilePath()214   virtual std::string FindDebugFilePath() const { return path_; }
215   virtual std::vector<Symbol> LoadSymbolsImpl() = 0;
216 
217   DsoType type_;
218   // path of the shared library used by the profiled program
219   const std::string path_;
220   // path of the shared library having symbol table and debug information
221   // It is the same as path_, or has the same build id as path_.
222   mutable std::optional<std::string> debug_file_path_;
223   // File name of the shared library, got by removing directories in path_.
224   std::string file_name_;
225   std::vector<Symbol> symbols_;
226   // unknown symbols are like [libc.so+0x1234].
227   std::unordered_map<uint64_t, Symbol> unknown_symbols_;
228   bool is_loaded_;
229   // Used to identify current dso if it needs to be dumped.
230   uint32_t dump_id_;
231   // Used to assign dump_id for symbols in current dso.
232   uint32_t symbol_dump_id_;
233   android::base::LogSeverity symbol_warning_loglevel_;
234 };
235 
236 const char* DsoTypeToString(DsoType dso_type);
237 bool GetBuildIdFromDsoPath(const std::string& dso_path, BuildId* build_id);
238 bool GetBuildId(const Dso& dso, BuildId& build_id);
239 
240 }  // namespace simpleperf
241 
242 #endif  // SIMPLE_PERF_DSO_H_
243