blob: 304277661bbebbf138f9425d3212eed86e7b24c3 [file] [log] [blame]
Yabin Cuiec12ed92015-06-08 10:38:10 -07001/*
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>
Yabin Cui7078c672020-11-10 16:24:12 -080021#include <optional>
Yabin Cuiec12ed92015-06-08 10:38:10 -070022#include <string>
Yabin Cuie32ed2b2020-07-23 15:30:14 -070023#include <string_view>
Yabin Cui638c5582015-07-01 16:16:57 -070024#include <unordered_map>
25#include <vector>
26
Mark Salyzyn499a0172018-11-12 12:58:06 -080027#include <android-base/file.h>
Yabin Cuie466d4d2017-08-11 17:03:07 -070028#include <android-base/logging.h>
Yabin Cui63a1c3d2017-05-19 12:57:44 -070029
Yabin Cui638c5582015-07-01 16:16:57 -070030#include "build_id.h"
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +010031#include "kallsyms.h"
Yabin Cui63a1c3d2017-05-19 12:57:44 -070032#include "read_elf.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070033
Yabin Cuifaa7b922021-01-11 17:35:57 -080034namespace simpleperf {
Yabin Cui40b70ff2018-04-09 14:06:08 -070035namespace simpleperf_dso_impl {
36
37// Find elf files with symbol table and debug information.
38class DebugElfFileFinder {
39 public:
40 void Reset();
41 bool SetSymFsDir(const std::string& symfs_dir);
Yabin Cui3939b9d2018-07-20 17:12:13 -070042 bool AddSymbolDir(const std::string& symbol_dir);
Yabin Cui40b70ff2018-04-09 14:06:08 -070043 void SetVdsoFile(const std::string& vdso_file, bool is_64bit);
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020044 std::string FindDebugFile(const std::string& dso_path, bool force_64bit, BuildId& build_id);
Yabin Cui1b9b1c12018-10-29 14:23:48 -070045 // Only for testing
46 std::string GetPathInSymFsDir(const std::string& path);
Yabin Cui40b70ff2018-04-09 14:06:08 -070047
48 private:
Yabin Cui3939b9d2018-07-20 17:12:13 -070049 void CollectBuildIdInDir(const std::string& dir);
50
Yabin Cui40b70ff2018-04-09 14:06:08 -070051 std::string vdso_64bit_;
52 std::string vdso_32bit_;
53 std::string symfs_dir_;
54 std::unordered_map<std::string, std::string> build_id_to_file_map_;
55};
56
57} // namespace simpleperf_dso_impl
58
Yabin Cuic8485602015-08-20 15:04:39 -070059struct Symbol {
Yabin Cuiec12ed92015-06-08 10:38:10 -070060 uint64_t addr;
Yabin Cui16501ff2016-10-19 15:06:29 -070061 // TODO: make len uint32_t.
Yabin Cuiec12ed92015-06-08 10:38:10 -070062 uint64_t len;
Yabin Cuib10a8fb2015-08-18 16:32:18 -070063
Martin Stjernholm7c27cc22018-11-28 00:46:00 +000064 Symbol(std::string_view name, uint64_t addr, uint64_t len);
Yabin Cui767dd172016-06-02 21:02:43 -070065 const char* Name() const { return name_; }
Yabin Cuib10a8fb2015-08-18 16:32:18 -070066
Yabin Cuicc2e59e2015-08-21 14:23:43 -070067 const char* DemangledName() const;
Yabin Cui40eef9e2021-04-13 13:08:31 -070068 void SetDemangledName(std::string_view name) const;
Yabin Cui1e16b202021-08-16 13:37:35 -070069 // Return function name without signature.
Yabin Cuifef95142021-08-19 10:51:00 -070070 std::string_view FunctionName() const;
Yabin Cuib10a8fb2015-08-18 16:32:18 -070071
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020072 bool HasDumpId() const { return dump_id_ != UINT_MAX; }
Yabin Cui767dd172016-06-02 21:02:43 -070073
Yabin Cui16501ff2016-10-19 15:06:29 -070074 bool GetDumpId(uint32_t* pdump_id) const {
75 if (!HasDumpId()) {
76 return false;
77 }
78 *pdump_id = dump_id_;
79 return true;
80 }
Yabin Cui767dd172016-06-02 21:02:43 -070081
Yabin Cuiaba7e292016-11-11 14:53:52 -080082 static bool CompareByDumpId(const Symbol* s1, const Symbol* s2) {
83 uint32_t id1 = UINT_MAX;
84 s1->GetDumpId(&id1);
85 uint32_t id2 = UINT_MAX;
86 s2->GetDumpId(&id2);
87 return id1 < id2;
88 }
89
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020090 static bool CompareByAddr(const Symbol* s1, const Symbol* s2) { return s1->addr < s2->addr; }
Yabin Cuiaba7e292016-11-11 14:53:52 -080091
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020092 static bool CompareValueByAddr(const Symbol& s1, const Symbol& s2) { return s1.addr < s2.addr; }
Yabin Cuiaba7e292016-11-11 14:53:52 -080093
Yabin Cuib10a8fb2015-08-18 16:32:18 -070094 private:
Yabin Cuicc2e59e2015-08-21 14:23:43 -070095 const char* name_;
96 mutable const char* demangled_name_;
Yabin Cui16501ff2016-10-19 15:06:29 -070097 mutable uint32_t dump_id_;
98
99 friend class Dso;
Yabin Cui767dd172016-06-02 21:02:43 -0700100};
101
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700102enum DsoType {
103 DSO_KERNEL,
104 DSO_KERNEL_MODULE,
105 DSO_ELF_FILE,
Yabin Cui516a87c2018-03-26 17:34:00 -0700106 DSO_DEX_FILE, // For files containing dex files, like .vdex files.
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200107 DSO_SYMBOL_MAP_FILE,
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700108 DSO_UNKNOWN_FILE,
Yabin Cui8d005de2020-10-21 13:48:53 -0700109 // DSO_UNKNOWN_FILE is written to the file feature section in recording files. Changing its value
110 // may cause compatibility issue. So put new DsoTypes below.
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700111};
112
Yabin Cui16501ff2016-10-19 15:06:29 -0700113class Dso {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700114 public:
Yabin Cuic8485602015-08-20 15:04:39 -0700115 static void SetDemangle(bool demangle);
116 static std::string Demangle(const std::string& name);
Yabin Cui3939b9d2018-07-20 17:12:13 -0700117 // SymFsDir is used to provide an alternative root directory looking for files with symbols.
118 // For example, if we are searching symbols for /system/lib/libc.so and SymFsDir is /data/symbols,
119 // then we will also search file /data/symbols/system/lib/libc.so.
Yabin Cuic8485602015-08-20 15:04:39 -0700120 static bool SetSymFsDir(const std::string& symfs_dir);
Yabin Cui3939b9d2018-07-20 17:12:13 -0700121 // SymbolDir is used to add a directory containing files with symbols. Each file under it will
122 // be searched recursively to build a build_id_map.
123 static bool AddSymbolDir(const std::string& symbol_dir);
Yabin Cuic8485602015-08-20 15:04:39 -0700124 static void SetVmlinux(const std::string& vmlinux);
Yabin Cuib4212972016-05-25 14:08:05 -0700125 static void SetKallsyms(std::string kallsyms) {
126 if (!kallsyms.empty()) {
127 kallsyms_ = std::move(kallsyms);
128 }
129 }
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200130 static void SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids);
Yabin Cui52c63692016-11-28 17:28:08 -0800131 static BuildId FindExpectedBuildIdForPath(const std::string& path);
Yabin Cuic68e66d2018-03-07 15:47:15 -0800132 static void SetVdsoFile(const std::string& vdso_file, bool is_64bit);
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700133
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700134 static std::unique_ptr<Dso> CreateDso(DsoType dso_type, const std::string& dso_path,
135 bool force_64bit = false);
Yabin Cui16f41ff2021-03-23 14:58:25 -0700136 static std::unique_ptr<Dso> CreateDsoWithBuildId(DsoType dso_type, const std::string& dso_path,
137 BuildId& build_id);
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800138 static std::unique_ptr<Dso> CreateKernelModuleDso(const std::string& dso_path,
139 uint64_t memory_start, uint64_t memory_end,
140 Dso* kernel_dso);
Yabin Cui516a87c2018-03-26 17:34:00 -0700141 virtual ~Dso();
Yabin Cui3c8c2132015-08-13 20:30:20 -0700142
Yabin Cui767dd172016-06-02 21:02:43 -0700143 DsoType type() const { return type_; }
144
Yabin Cui3c8c2132015-08-13 20:30:20 -0700145 // Return the path recorded in perf.data.
Yabin Cui767dd172016-06-02 21:02:43 -0700146 const std::string& Path() const { return path_; }
Yabin Cuieec606c2016-07-07 13:53:33 -0700147 // Return the path containing symbol table and debug information.
Yabin Cui11424c42022-03-10 16:04:04 -0800148 const std::string& GetDebugFilePath() const {
149 if (!debug_file_path_.has_value()) {
150 debug_file_path_ = FindDebugFilePath();
151 }
152 return debug_file_path_.value();
153 }
154
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700155 // Return the path beautified for reporting.
156 virtual std::string_view GetReportPath() const { return Path(); }
Yabin Cui15475e62016-07-14 13:26:19 -0700157 // Return the file name without directory info.
158 const std::string& FileName() const { return file_name_; }
Yabin Cui767dd172016-06-02 21:02:43 -0700159
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200160 bool HasDumpId() { return dump_id_ != UINT_MAX; }
Yabin Cui16501ff2016-10-19 15:06:29 -0700161
162 bool GetDumpId(uint32_t* pdump_id) {
163 if (!HasDumpId()) {
164 return false;
165 }
166 *pdump_id = dump_id_;
167 return true;
168 }
169
170 uint32_t CreateDumpId();
171 uint32_t CreateSymbolDumpId(const Symbol* symbol);
Yabin Cui78fddd12016-10-24 14:09:26 -0700172
Yabin Cuidb2c4932019-02-07 15:06:42 -0800173 virtual void SetMinExecutableVaddr(uint64_t, uint64_t) {}
174 virtual void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* file_offset) {
175 *min_vaddr = 0;
176 *file_offset = 0;
177 }
Yabin Cuidd401b32018-04-11 11:17:06 -0700178 virtual void AddDexFileOffset(uint64_t) {}
179 virtual const std::vector<uint64_t>* DexFileOffsets() { return nullptr; }
Yabin Cui547c60e2015-10-12 16:56:05 -0700180
Yabin Cuidb2c4932019-02-07 15:06:42 -0800181 virtual uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) = 0;
Yabin Cui7078c672020-11-10 16:24:12 -0800182 virtual std::optional<uint64_t> IpToFileOffset(uint64_t ip, uint64_t map_start,
183 uint64_t map_pgoff);
Yabin Cuidb2c4932019-02-07 15:06:42 -0800184
Yabin Cui547c60e2015-10-12 16:56:05 -0700185 const Symbol* FindSymbol(uint64_t vaddr_in_dso);
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800186 void LoadSymbols();
Yabin Cuiac4b2492020-12-09 16:27:57 -0800187 const std::vector<Symbol>& GetSymbols() const { return symbols_; }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700188 void SetSymbols(std::vector<Symbol>* symbols);
189
190 // Create a symbol for a virtual address which can't find a corresponding
191 // symbol in symbol table.
192 void AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name);
Yabin Cuiac4b2492020-12-09 16:27:57 -0800193 bool IsForJavaMethod() const;
Yabin Cuib3783552015-06-11 11:15:42 -0700194
Yabin Cui516a87c2018-03-26 17:34:00 -0700195 protected:
Yabin Cuic8485602015-08-20 15:04:39 -0700196 static bool demangle_;
Yabin Cuic8485602015-08-20 15:04:39 -0700197 static std::string vmlinux_;
Yabin Cuib4212972016-05-25 14:08:05 -0700198 static std::string kallsyms_;
Yabin Cuic8485602015-08-20 15:04:39 -0700199 static std::unordered_map<std::string, BuildId> build_id_map_;
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700200 static size_t dso_count_;
Yabin Cui16501ff2016-10-19 15:06:29 -0700201 static uint32_t g_dump_id_;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700202 static simpleperf_dso_impl::DebugElfFileFinder debug_elf_file_finder_;
Yabin Cuic8485602015-08-20 15:04:39 -0700203
Yabin Cui11424c42022-03-10 16:04:04 -0800204 Dso(DsoType type, const std::string& path);
205 BuildId GetExpectedBuildId() const;
Yabin Cui516a87c2018-03-26 17:34:00 -0700206
Yabin Cui11424c42022-03-10 16:04:04 -0800207 virtual std::string FindDebugFilePath() const { return path_; }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800208 virtual std::vector<Symbol> LoadSymbolsImpl() = 0;
Yabin Cuic8485602015-08-20 15:04:39 -0700209
Yabin Cuidd401b32018-04-11 11:17:06 -0700210 DsoType type_;
Yabin Cuieec606c2016-07-07 13:53:33 -0700211 // path of the shared library used by the profiled program
Yabin Cuic8485602015-08-20 15:04:39 -0700212 const std::string path_;
Yabin Cuieec606c2016-07-07 13:53:33 -0700213 // path of the shared library having symbol table and debug information
214 // It is the same as path_, or has the same build id as path_.
Yabin Cui11424c42022-03-10 16:04:04 -0800215 mutable std::optional<std::string> debug_file_path_;
Yabin Cui15475e62016-07-14 13:26:19 -0700216 // File name of the shared library, got by removing directories in path_.
217 std::string file_name_;
Yabin Cuic5b4a312016-10-24 13:38:38 -0700218 std::vector<Symbol> symbols_;
219 // unknown symbols are like [libc.so+0x1234].
220 std::unordered_map<uint64_t, Symbol> unknown_symbols_;
Yabin Cuic8485602015-08-20 15:04:39 -0700221 bool is_loaded_;
Yabin Cui16501ff2016-10-19 15:06:29 -0700222 // Used to identify current dso if it needs to be dumped.
223 uint32_t dump_id_;
224 // Used to assign dump_id for symbols in current dso.
225 uint32_t symbol_dump_id_;
Yabin Cuie466d4d2017-08-11 17:03:07 -0700226 android::base::LogSeverity symbol_warning_loglevel_;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700227};
228
Yabin Cui767dd172016-06-02 21:02:43 -0700229const char* DsoTypeToString(DsoType dso_type);
Yabin Cui40b70ff2018-04-09 14:06:08 -0700230bool GetBuildIdFromDsoPath(const std::string& dso_path, BuildId* build_id);
Yabin Cui767dd172016-06-02 21:02:43 -0700231
Yabin Cuifaa7b922021-01-11 17:35:57 -0800232} // namespace simpleperf
233
Yabin Cuiec12ed92015-06-08 10:38:10 -0700234#endif // SIMPLE_PERF_DSO_H_