blob: d3b7ffba351b2cfaa244bd06da880988f1b2e895 [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#include "dso.h"
18
Yabin Cuib3783552015-06-11 11:15:42 -070019#include <stdlib.h>
Yabin Cuicc2e59e2015-08-21 14:23:43 -070020#include <string.h>
Yabin Cuic8485602015-08-20 15:04:39 -070021
Yabin Cuicc2e59e2015-08-21 14:23:43 -070022#include <algorithm>
Yabin Cuic8485602015-08-20 15:04:39 -070023#include <limits>
Yabin Cuidd401b32018-04-11 11:17:06 -070024#include <memory>
Yabin Cui7078c672020-11-10 16:24:12 -080025#include <optional>
Yabin Cui9ba4d942020-09-08 16:12:46 -070026#include <string_view>
Yabin Cuicc2e59e2015-08-21 14:23:43 -070027#include <vector>
Yabin Cuic8485602015-08-20 15:04:39 -070028
Yabin Cuib4212972016-05-25 14:08:05 -070029#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080030#include <android-base/logging.h>
Yabin Cui40b70ff2018-04-09 14:06:08 -070031#include <android-base/strings.h>
Yabin Cuic8485602015-08-20 15:04:39 -070032
Yabin Cui075dd182020-08-05 19:51:36 +000033#include "JITDebugReader.h"
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020034#include "environment.h"
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +010035#include "kallsyms.h"
Yabin Cuib1a885b2016-02-14 19:18:02 -080036#include "read_apk.h"
Yabin Cui516a87c2018-03-26 17:34:00 -070037#include "read_dex_file.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070038#include "read_elf.h"
Yabin Cuib3783552015-06-11 11:15:42 -070039#include "utils.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070040
Yabin Cuifaa7b922021-01-11 17:35:57 -080041namespace simpleperf {
42
Yabin Cui075dd182020-08-05 19:51:36 +000043using android::base::EndsWith;
Yabin Cui9ba4d942020-09-08 16:12:46 -070044using android::base::StartsWith;
Yabin Cui3a880452020-06-29 16:37:31 -070045
Yabin Cui40b70ff2018-04-09 14:06:08 -070046namespace simpleperf_dso_impl {
47
Yabin Cui1b9b1c12018-10-29 14:23:48 -070048std::string RemovePathSeparatorSuffix(const std::string& path) {
49 // Don't remove path separator suffix for '/'.
Yabin Cui075dd182020-08-05 19:51:36 +000050 if (EndsWith(path, OS_PATH_SEPARATOR) && path.size() > 1u) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -070051 return path.substr(0, path.size() - 1);
52 }
53 return path;
54}
55
Yabin Cui40b70ff2018-04-09 14:06:08 -070056void DebugElfFileFinder::Reset() {
57 vdso_64bit_.clear();
58 vdso_32bit_.clear();
59 symfs_dir_.clear();
60 build_id_to_file_map_.clear();
61}
62
63bool DebugElfFileFinder::SetSymFsDir(const std::string& symfs_dir) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -070064 symfs_dir_ = RemovePathSeparatorSuffix(symfs_dir);
65 if (!IsDir(symfs_dir_)) {
66 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir_ << "'";
67 return false;
Yabin Cui40b70ff2018-04-09 14:06:08 -070068 }
Yabin Cui1b9b1c12018-10-29 14:23:48 -070069 std::string build_id_list_file = symfs_dir_ + OS_PATH_SEPARATOR + "build_id_list";
Yabin Cui40b70ff2018-04-09 14:06:08 -070070 std::string build_id_list;
71 if (android::base::ReadFileToString(build_id_list_file, &build_id_list)) {
72 for (auto& line : android::base::Split(build_id_list, "\n")) {
Yabin Cui2969a9e2018-04-19 17:06:24 -070073 std::vector<std::string> items = android::base::Split(line, "=");
Yabin Cui40b70ff2018-04-09 14:06:08 -070074 if (items.size() == 2u) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -070075 build_id_to_file_map_[items[0]] = symfs_dir_ + OS_PATH_SEPARATOR + items[1];
Yabin Cui40b70ff2018-04-09 14:06:08 -070076 }
77 }
78 }
79 return true;
80}
81
Yabin Cui3939b9d2018-07-20 17:12:13 -070082bool DebugElfFileFinder::AddSymbolDir(const std::string& symbol_dir) {
83 if (!IsDir(symbol_dir)) {
84 LOG(ERROR) << "Invalid symbol dir " << symbol_dir;
85 return false;
86 }
Yabin Cui1b9b1c12018-10-29 14:23:48 -070087 std::string dir = RemovePathSeparatorSuffix(symbol_dir);
Yabin Cui3939b9d2018-07-20 17:12:13 -070088 CollectBuildIdInDir(dir);
89 return true;
90}
91
92void DebugElfFileFinder::CollectBuildIdInDir(const std::string& dir) {
93 for (const std::string& entry : GetEntriesInDir(dir)) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -070094 std::string path = dir + OS_PATH_SEPARATOR + entry;
Yabin Cui3939b9d2018-07-20 17:12:13 -070095 if (IsDir(path)) {
96 CollectBuildIdInDir(path);
97 } else {
98 BuildId build_id;
Yabin Cui3a880452020-06-29 16:37:31 -070099 ElfStatus status;
100 auto elf = ElfFile::Open(path, &status);
101 if (status == ElfStatus::NO_ERROR && elf->GetBuildId(&build_id) == ElfStatus::NO_ERROR) {
Yabin Cui3939b9d2018-07-20 17:12:13 -0700102 build_id_to_file_map_[build_id.ToString()] = path;
103 }
104 }
105 }
106}
107
Yabin Cui40b70ff2018-04-09 14:06:08 -0700108void DebugElfFileFinder::SetVdsoFile(const std::string& vdso_file, bool is_64bit) {
109 if (is_64bit) {
110 vdso_64bit_ = vdso_file;
111 } else {
112 vdso_32bit_ = vdso_file;
113 }
114}
115
Yabin Cui991477b2020-07-17 16:12:15 -0700116static bool CheckDebugFilePath(const std::string& path, BuildId& build_id,
117 bool report_build_id_mismatch) {
118 ElfStatus status;
119 auto elf = ElfFile::Open(path, &status);
120 if (!elf) {
121 return false;
122 }
123 BuildId debug_build_id;
124 status = elf->GetBuildId(&debug_build_id);
125 if (status != ElfStatus::NO_ERROR && status != ElfStatus::NO_BUILD_ID) {
126 return false;
127 }
128
129 // Native libraries in apks and kernel modules may not have build ids.
130 // So build_id and debug_build_id can either be empty, or have the same value.
131 bool match = build_id == debug_build_id;
132 if (!match && report_build_id_mismatch) {
133 LOG(WARNING) << path << " isn't used because of build id mismatch: expected " << build_id
134 << ", real " << debug_build_id;
135 }
136 return match;
137}
138
Yabin Cui40b70ff2018-04-09 14:06:08 -0700139std::string DebugElfFileFinder::FindDebugFile(const std::string& dso_path, bool force_64bit,
140 BuildId& build_id) {
141 if (dso_path == "[vdso]") {
142 if (force_64bit && !vdso_64bit_.empty()) {
143 return vdso_64bit_;
144 } else if (!force_64bit && !vdso_32bit_.empty()) {
145 return vdso_32bit_;
146 }
Yabin Cui3939b9d2018-07-20 17:12:13 -0700147 }
Yabin Cuid347bb42019-11-14 15:24:07 -0800148 if (build_id.IsEmpty()) {
149 // Try reading build id from file if we don't already have one.
150 GetBuildIdFromDsoPath(dso_path, &build_id);
151 }
Yabin Cui3939b9d2018-07-20 17:12:13 -0700152
Yabin Cui5d269c72019-05-31 15:30:17 -0700153 // 1. Try build_id_to_file_map.
154 if (!build_id_to_file_map_.empty()) {
155 if (!build_id.IsEmpty() || GetBuildIdFromDsoPath(dso_path, &build_id)) {
156 auto it = build_id_to_file_map_.find(build_id.ToString());
Yabin Cui991477b2020-07-17 16:12:15 -0700157 if (it != build_id_to_file_map_.end() && CheckDebugFilePath(it->second, build_id, false)) {
Yabin Cui5d269c72019-05-31 15:30:17 -0700158 return it->second;
159 }
160 }
161 }
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700162 if (!symfs_dir_.empty()) {
Yabin Cuia4496ad2019-11-18 16:40:28 -0800163 // 2. Try concatenating symfs_dir and dso_path.
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700164 std::string path = GetPathInSymFsDir(dso_path);
Yabin Cui991477b2020-07-17 16:12:15 -0700165 if (CheckDebugFilePath(path, build_id, true)) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700166 return path;
167 }
Yabin Cuia4496ad2019-11-18 16:40:28 -0800168 // 3. Try concatenating symfs_dir and basename of dso_path.
169 path = symfs_dir_ + OS_PATH_SEPARATOR + android::base::Basename(dso_path);
Yabin Cui991477b2020-07-17 16:12:15 -0700170 if (CheckDebugFilePath(path, build_id, false)) {
Yabin Cuia4496ad2019-11-18 16:40:28 -0800171 return path;
172 }
Yabin Cui3939b9d2018-07-20 17:12:13 -0700173 }
Yabin Cuia4496ad2019-11-18 16:40:28 -0800174 // 4. Try concatenating /usr/lib/debug and dso_path.
Yabin Cui3939b9d2018-07-20 17:12:13 -0700175 // Linux host can store debug shared libraries in /usr/lib/debug.
Yabin Cui991477b2020-07-17 16:12:15 -0700176 if (CheckDebugFilePath("/usr/lib/debug" + dso_path, build_id, false)) {
Yabin Cui3939b9d2018-07-20 17:12:13 -0700177 return "/usr/lib/debug" + dso_path;
178 }
Yabin Cui40b70ff2018-04-09 14:06:08 -0700179 return dso_path;
180}
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700181
182std::string DebugElfFileFinder::GetPathInSymFsDir(const std::string& path) {
183 auto add_symfs_prefix = [&](const std::string& path) {
Yabin Cui9ba4d942020-09-08 16:12:46 -0700184 if (StartsWith(path, OS_PATH_SEPARATOR)) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700185 return symfs_dir_ + path;
186 }
187 return symfs_dir_ + OS_PATH_SEPARATOR + path;
188 };
189 if (OS_PATH_SEPARATOR == '/') {
190 return add_symfs_prefix(path);
191 }
192 // Paths in recorded perf.data uses '/' as path separator. When reporting on Windows, it needs
193 // to be converted to '\\'.
194 auto tuple = SplitUrlInApk(path);
195 if (std::get<0>(tuple)) {
196 std::string apk_path = std::get<1>(tuple);
197 std::string entry_path = std::get<2>(tuple);
198 std::replace(apk_path.begin(), apk_path.end(), '/', OS_PATH_SEPARATOR);
199 return GetUrlInApk(add_symfs_prefix(apk_path), entry_path);
200 }
201 std::string elf_path = path;
202 std::replace(elf_path.begin(), elf_path.end(), '/', OS_PATH_SEPARATOR);
203 return add_symfs_prefix(elf_path);
204}
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200205} // namespace simpleperf_dso_impl
Yabin Cui40b70ff2018-04-09 14:06:08 -0700206
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700207static OneTimeFreeAllocator symbol_name_allocator;
208
Martin Stjernholm7c27cc22018-11-28 00:46:00 +0000209Symbol::Symbol(std::string_view name, uint64_t addr, uint64_t len)
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700210 : addr(addr),
211 len(len),
212 name_(symbol_name_allocator.AllocateString(name)),
Yabin Cui767dd172016-06-02 21:02:43 -0700213 demangled_name_(nullptr),
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200214 dump_id_(UINT_MAX) {}
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700215
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700216const char* Symbol::DemangledName() const {
217 if (demangled_name_ == nullptr) {
218 const std::string s = Dso::Demangle(name_);
219 if (s == name_) {
220 demangled_name_ = name_;
221 } else {
222 demangled_name_ = symbol_name_allocator.AllocateString(s);
223 }
224 }
225 return demangled_name_;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700226}
227
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800228static bool CompareSymbolToAddr(const Symbol& s, uint64_t addr) {
229 return s.addr < addr;
230}
231
232static bool CompareAddrToSymbol(uint64_t addr, const Symbol& s) {
233 return addr < s.addr;
234}
235
Yabin Cuic8485602015-08-20 15:04:39 -0700236bool Dso::demangle_ = true;
Yabin Cuic8485602015-08-20 15:04:39 -0700237std::string Dso::vmlinux_;
Yabin Cuib4212972016-05-25 14:08:05 -0700238std::string Dso::kallsyms_;
Yabin Cuic8485602015-08-20 15:04:39 -0700239std::unordered_map<std::string, BuildId> Dso::build_id_map_;
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700240size_t Dso::dso_count_;
Yabin Cui16501ff2016-10-19 15:06:29 -0700241uint32_t Dso::g_dump_id_;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700242simpleperf_dso_impl::DebugElfFileFinder Dso::debug_elf_file_finder_;
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700243
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200244void Dso::SetDemangle(bool demangle) {
245 demangle_ = demangle;
246}
Yabin Cuib3783552015-06-11 11:15:42 -0700247
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200248extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n, int* status);
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700249
Yabin Cuic8485602015-08-20 15:04:39 -0700250std::string Dso::Demangle(const std::string& name) {
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700251 if (!demangle_) {
252 return name;
253 }
254 int status;
255 bool is_linker_symbol = (name.find(linker_prefix) == 0);
256 const char* mangled_str = name.c_str();
257 if (is_linker_symbol) {
258 mangled_str += linker_prefix.size();
259 }
260 std::string result = name;
261 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
262 if (status == 0) {
263 if (is_linker_symbol) {
264 result = std::string("[linker]") + demangled_name;
265 } else {
266 result = demangled_name;
267 }
268 free(demangled_name);
269 } else if (is_linker_symbol) {
270 result = std::string("[linker]") + mangled_str;
271 }
272 return result;
273}
274
Yabin Cuic8485602015-08-20 15:04:39 -0700275bool Dso::SetSymFsDir(const std::string& symfs_dir) {
Yabin Cui40b70ff2018-04-09 14:06:08 -0700276 return debug_elf_file_finder_.SetSymFsDir(symfs_dir);
Yabin Cuic8485602015-08-20 15:04:39 -0700277}
278
Yabin Cui3939b9d2018-07-20 17:12:13 -0700279bool Dso::AddSymbolDir(const std::string& symbol_dir) {
280 return debug_elf_file_finder_.AddSymbolDir(symbol_dir);
281}
282
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200283void Dso::SetVmlinux(const std::string& vmlinux) {
284 vmlinux_ = vmlinux;
285}
Yabin Cuic8485602015-08-20 15:04:39 -0700286
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200287void Dso::SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids) {
Yabin Cuic8485602015-08-20 15:04:39 -0700288 std::unordered_map<std::string, BuildId> map;
289 for (auto& pair : build_ids) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200290 LOG(DEBUG) << "build_id_map: " << pair.first << ", " << pair.second.ToString();
Yabin Cuic8485602015-08-20 15:04:39 -0700291 map.insert(pair);
292 }
293 build_id_map_ = std::move(map);
294}
295
Yabin Cuic68e66d2018-03-07 15:47:15 -0800296void Dso::SetVdsoFile(const std::string& vdso_file, bool is_64bit) {
Yabin Cui40b70ff2018-04-09 14:06:08 -0700297 debug_elf_file_finder_.SetVdsoFile(vdso_file, is_64bit);
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700298}
299
Yabin Cui52c63692016-11-28 17:28:08 -0800300BuildId Dso::FindExpectedBuildIdForPath(const std::string& path) {
301 auto it = build_id_map_.find(path);
Yabin Cuic8485602015-08-20 15:04:39 -0700302 if (it != build_id_map_.end()) {
303 return it->second;
304 }
305 return BuildId();
306}
307
Yabin Cui52c63692016-11-28 17:28:08 -0800308BuildId Dso::GetExpectedBuildId() {
309 return FindExpectedBuildIdForPath(path_);
310}
311
Yabin Cui516a87c2018-03-26 17:34:00 -0700312Dso::Dso(DsoType type, const std::string& path, const std::string& debug_file_path)
Yabin Cui767dd172016-06-02 21:02:43 -0700313 : type_(type),
Yabin Cui767dd172016-06-02 21:02:43 -0700314 path_(path),
Yabin Cui516a87c2018-03-26 17:34:00 -0700315 debug_file_path_(debug_file_path),
Yabin Cui767dd172016-06-02 21:02:43 -0700316 is_loaded_(false),
Yabin Cui16501ff2016-10-19 15:06:29 -0700317 dump_id_(UINT_MAX),
Yabin Cuie466d4d2017-08-11 17:03:07 -0700318 symbol_dump_id_(0),
319 symbol_warning_loglevel_(android::base::WARNING) {
Yabin Cui15475e62016-07-14 13:26:19 -0700320 size_t pos = path.find_last_of("/\\");
321 if (pos != std::string::npos) {
322 file_name_ = path.substr(pos + 1);
323 } else {
324 file_name_ = path;
325 }
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700326 dso_count_++;
Yabin Cuic8485602015-08-20 15:04:39 -0700327}
328
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700329Dso::~Dso() {
330 if (--dso_count_ == 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700331 // Clean up global variables when no longer used.
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700332 symbol_name_allocator.Clear();
Yabin Cuib4212972016-05-25 14:08:05 -0700333 demangle_ = true;
Yabin Cuib4212972016-05-25 14:08:05 -0700334 vmlinux_.clear();
335 kallsyms_.clear();
336 build_id_map_.clear();
Yabin Cui16501ff2016-10-19 15:06:29 -0700337 g_dump_id_ = 0;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700338 debug_elf_file_finder_.Reset();
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700339 }
340}
341
Yabin Cui16501ff2016-10-19 15:06:29 -0700342uint32_t Dso::CreateDumpId() {
343 CHECK(!HasDumpId());
344 return dump_id_ = g_dump_id_++;
345}
346
347uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
348 CHECK(!symbol->HasDumpId());
349 symbol->dump_id_ = symbol_dump_id_++;
350 return symbol->dump_id_;
351}
352
Yabin Cui7078c672020-11-10 16:24:12 -0800353std::optional<uint64_t> Dso::IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) {
354 return ip - map_start + map_pgoff;
355}
356
Yabin Cui547c60e2015-10-12 16:56:05 -0700357const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
Yabin Cuic8485602015-08-20 15:04:39 -0700358 if (!is_loaded_) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800359 LoadSymbols();
Yabin Cuic5b4a312016-10-24 13:38:38 -0700360 }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800361 auto it = std::upper_bound(symbols_.begin(), symbols_.end(), vaddr_in_dso, CompareAddrToSymbol);
Yabin Cui516a87c2018-03-26 17:34:00 -0700362 if (it != symbols_.begin()) {
363 --it;
364 if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
365 return &*it;
Yabin Cuic8485602015-08-20 15:04:39 -0700366 }
367 }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700368 if (!unknown_symbols_.empty()) {
369 auto it = unknown_symbols_.find(vaddr_in_dso);
370 if (it != unknown_symbols_.end()) {
371 return &it->second;
Yabin Cuic8485602015-08-20 15:04:39 -0700372 }
373 }
374 return nullptr;
375}
376
Yabin Cuic5b4a312016-10-24 13:38:38 -0700377void Dso::SetSymbols(std::vector<Symbol>* symbols) {
378 symbols_ = std::move(*symbols);
379 symbols->clear();
380}
381
382void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) {
383 unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1)));
384}
385
Yabin Cuiac4b2492020-12-09 16:27:57 -0800386bool Dso::IsForJavaMethod() const {
Yabin Cui10bbd842018-08-13 17:42:25 -0700387 if (type_ == DSO_DEX_FILE) {
388 return true;
389 }
390 if (type_ == DSO_ELF_FILE) {
Yabin Cui9ba4d942020-09-08 16:12:46 -0700391 if (JITDebugReader::IsPathInJITSymFile(path_)) {
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700392 return true;
393 }
Yabin Cui9ba4d942020-09-08 16:12:46 -0700394 // JITDebugReader in old versions generates symfiles in 'TemporaryFile-XXXXXX'.
395 size_t pos = path_.rfind('/');
396 pos = (pos == std::string::npos) ? 0 : pos + 1;
397 return StartsWith(std::string_view(&path_[pos], path_.size() - pos), "TemporaryFile");
Yabin Cui10bbd842018-08-13 17:42:25 -0700398 }
399 return false;
400}
401
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800402void Dso::LoadSymbols() {
403 if (!is_loaded_) {
404 is_loaded_ = true;
405 std::vector<Symbol> symbols = LoadSymbolsImpl();
406 if (symbols_.empty()) {
407 symbols_ = std::move(symbols);
408 } else {
409 std::vector<Symbol> merged_symbols;
410 std::set_union(symbols_.begin(), symbols_.end(), symbols.begin(), symbols.end(),
411 std::back_inserter(merged_symbols), Symbol::CompareValueByAddr);
412 symbols_ = std::move(merged_symbols);
413 }
Yabin Cuic8485602015-08-20 15:04:39 -0700414 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700415}
416
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200417static void ReportReadElfSymbolResult(
418 ElfStatus result, const std::string& path, const std::string& debug_file_path,
Yabin Cui516a87c2018-03-26 17:34:00 -0700419 android::base::LogSeverity warning_loglevel = android::base::WARNING) {
Yabin Cuidec43c12016-07-29 16:40:40 -0700420 if (result == ElfStatus::NO_ERROR) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700421 LOG(VERBOSE) << "Read symbols from " << debug_file_path << " successfully";
Yabin Cuidec43c12016-07-29 16:40:40 -0700422 } else if (result == ElfStatus::NO_SYMBOL_TABLE) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700423 if (path == "[vdso]") {
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700424 // Vdso only contains dynamic symbol table, and we can't change that.
Yabin Cui516a87c2018-03-26 17:34:00 -0700425 return;
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700426 }
Yabin Cuidec43c12016-07-29 16:40:40 -0700427 // Lacking symbol table isn't considered as an error but worth reporting.
Yabin Cui516a87c2018-03-26 17:34:00 -0700428 LOG(warning_loglevel) << debug_file_path << " doesn't contain symbol table";
Yabin Cuidec43c12016-07-29 16:40:40 -0700429 } else {
Yabin Cui516a87c2018-03-26 17:34:00 -0700430 LOG(warning_loglevel) << "failed to read symbols from " << debug_file_path << ": " << result;
Yabin Cuidec43c12016-07-29 16:40:40 -0700431 }
432}
433
Yabin Cui516a87c2018-03-26 17:34:00 -0700434static void SortAndFixSymbols(std::vector<Symbol>& symbols) {
435 std::sort(symbols.begin(), symbols.end(), Symbol::CompareValueByAddr);
Yabin Cuic8485602015-08-20 15:04:39 -0700436 Symbol* prev_symbol = nullptr;
Yabin Cui516a87c2018-03-26 17:34:00 -0700437 for (auto& symbol : symbols) {
Yabin Cuic8485602015-08-20 15:04:39 -0700438 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700439 prev_symbol->len = symbol.addr - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700440 }
Yabin Cui3d4aa262017-11-01 15:58:55 -0700441 prev_symbol = &symbol;
Yabin Cui638c5582015-07-01 16:16:57 -0700442 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700443}
444
Yabin Cuidd401b32018-04-11 11:17:06 -0700445class DexFileDso : public Dso {
446 public:
447 DexFileDso(const std::string& path, const std::string& debug_file_path)
448 : Dso(DSO_DEX_FILE, path, debug_file_path) {}
449
450 void AddDexFileOffset(uint64_t dex_file_offset) override {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200451 auto it = std::lower_bound(dex_file_offsets_.begin(), dex_file_offsets_.end(), dex_file_offset);
Yabin Cuic8571d42018-06-06 11:20:39 -0700452 if (it != dex_file_offsets_.end() && *it == dex_file_offset) {
453 return;
454 }
455 dex_file_offsets_.insert(it, dex_file_offset);
Yabin Cuidd401b32018-04-11 11:17:06 -0700456 }
457
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200458 const std::vector<uint64_t>* DexFileOffsets() override { return &dex_file_offsets_; }
Yabin Cuidd401b32018-04-11 11:17:06 -0700459
Yabin Cuidb2c4932019-02-07 15:06:42 -0800460 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override {
461 return ip - map_start + map_pgoff;
462 }
463
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800464 std::vector<Symbol> LoadSymbolsImpl() override {
Yabin Cuidd401b32018-04-11 11:17:06 -0700465 std::vector<Symbol> symbols;
Yabin Cui2a53ff32018-05-21 17:37:00 -0700466 auto tuple = SplitUrlInApk(debug_file_path_);
467 bool status = false;
Yabin Cui710f3722021-03-23 17:45:39 -0700468 auto symbol_callback = [&](DexFileSymbol* dex_symbol) {
469 symbols.emplace_back(std::string_view(dex_symbol->name, dex_symbol->name_size),
470 dex_symbol->addr, dex_symbol->size);
471 };
Yabin Cui2a53ff32018-05-21 17:37:00 -0700472 if (std::get<0>(tuple)) {
473 std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(std::get<1>(tuple));
474 ZipEntry entry;
475 std::vector<uint8_t> data;
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200476 if (ahelper && ahelper->FindEntry(std::get<2>(tuple), &entry) &&
477 ahelper->GetEntryData(entry, &data)) {
Yabin Cui2a53ff32018-05-21 17:37:00 -0700478 status = ReadSymbolsFromDexFileInMemory(data.data(), data.size(), dex_file_offsets_,
Yabin Cui710f3722021-03-23 17:45:39 -0700479 symbol_callback);
Yabin Cui2a53ff32018-05-21 17:37:00 -0700480 }
481 } else {
Yabin Cui710f3722021-03-23 17:45:39 -0700482 status = ReadSymbolsFromDexFile(debug_file_path_, dex_file_offsets_, symbol_callback);
Yabin Cui2a53ff32018-05-21 17:37:00 -0700483 }
484 if (!status) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200485 android::base::LogSeverity level =
486 symbols_.empty() ? android::base::WARNING : android::base::DEBUG;
Yabin Cuidd401b32018-04-11 11:17:06 -0700487 LOG(level) << "Failed to read symbols from " << debug_file_path_;
488 return symbols;
489 }
490 LOG(VERBOSE) << "Read symbols from " << debug_file_path_ << " successfully";
Yabin Cuidd401b32018-04-11 11:17:06 -0700491 SortAndFixSymbols(symbols);
492 return symbols;
493 }
494
495 private:
496 std::vector<uint64_t> dex_file_offsets_;
497};
498
Yabin Cui516a87c2018-03-26 17:34:00 -0700499class ElfDso : public Dso {
500 public:
501 ElfDso(const std::string& path, const std::string& debug_file_path)
Yabin Cuidb2c4932019-02-07 15:06:42 -0800502 : Dso(DSO_ELF_FILE, path, debug_file_path) {}
Yabin Cui516a87c2018-03-26 17:34:00 -0700503
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700504 std::string_view GetReportPath() const override {
Yabin Cui9ba4d942020-09-08 16:12:46 -0700505 if (JITDebugReader::IsPathInJITSymFile(path_)) {
506 if (path_.find(kJITAppCacheFile) != path_.npos) {
Yabin Cui075dd182020-08-05 19:51:36 +0000507 return "[JIT app cache]";
508 }
Yabin Cui9ba4d942020-09-08 16:12:46 -0700509 return "[JIT zygote cache]";
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700510 }
511 return path_;
512 }
513
Yabin Cuidb2c4932019-02-07 15:06:42 -0800514 void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t file_offset) override {
515 min_vaddr_ = min_vaddr;
516 file_offset_of_min_vaddr_ = file_offset;
Yabin Cuic8485602015-08-20 15:04:39 -0700517 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700518
Yabin Cuidb2c4932019-02-07 15:06:42 -0800519 void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* file_offset) override {
520 if (type_ == DSO_DEX_FILE) {
521 return dex_file_dso_->GetMinExecutableVaddr(min_vaddr, file_offset);
522 }
523 if (min_vaddr_ == uninitialized_value) {
524 min_vaddr_ = 0;
525 BuildId build_id = GetExpectedBuildId();
Yabin Cui90c3b302020-07-01 10:09:16 -0700526
527 ElfStatus status;
528 auto elf = ElfFile::Open(debug_file_path_, &build_id, &status);
529 if (elf) {
530 min_vaddr_ = elf->ReadMinExecutableVaddr(&file_offset_of_min_vaddr_);
Yabin Cuidb2c4932019-02-07 15:06:42 -0800531 } else {
Yabin Cui90c3b302020-07-01 10:09:16 -0700532 LOG(WARNING) << "failed to read min virtual address of " << debug_file_path_ << ": "
533 << status;
Yabin Cuidb2c4932019-02-07 15:06:42 -0800534 }
535 }
536 *min_vaddr = min_vaddr_;
537 *file_offset = file_offset_of_min_vaddr_;
538 }
539
540 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override {
541 if (type_ == DSO_DEX_FILE) {
542 return dex_file_dso_->IpToVaddrInFile(ip, map_start, map_pgoff);
543 }
544 uint64_t min_vaddr;
545 uint64_t file_offset_of_min_vaddr;
546 GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
547 if (file_offset_of_min_vaddr == uninitialized_value) {
548 return ip - map_start + min_vaddr;
549 }
550 // Apps may make part of the executable segment of a shared library writeable, which can
551 // generate multiple executable segments at runtime. So use map_pgoff to calculate
552 // vaddr_in_file.
553 return ip - map_start + map_pgoff - file_offset_of_min_vaddr + min_vaddr;
Yabin Cui516a87c2018-03-26 17:34:00 -0700554 }
555
Yabin Cuidd401b32018-04-11 11:17:06 -0700556 void AddDexFileOffset(uint64_t dex_file_offset) override {
557 if (type_ == DSO_ELF_FILE) {
558 // When simpleperf does unwinding while recording, it processes mmap records before reading
559 // dex file linked list (via JITDebugReader). To process mmap records, it creates Dso
560 // objects of type ELF_FILE. Then after reading dex file linked list, it realizes some
561 // ELF_FILE Dso objects should actually be DEX_FILE, because they have dex file offsets.
562 // So here converts ELF_FILE Dso into DEX_FILE Dso.
563 type_ = DSO_DEX_FILE;
564 dex_file_dso_.reset(new DexFileDso(path_, path_));
565 }
566 dex_file_dso_->AddDexFileOffset(dex_file_offset);
567 }
568
569 const std::vector<uint64_t>* DexFileOffsets() override {
570 return dex_file_dso_ ? dex_file_dso_->DexFileOffsets() : nullptr;
571 }
572
Yabin Cui516a87c2018-03-26 17:34:00 -0700573 protected:
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800574 std::vector<Symbol> LoadSymbolsImpl() override {
Yabin Cuidd401b32018-04-11 11:17:06 -0700575 if (dex_file_dso_) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800576 return dex_file_dso_->LoadSymbolsImpl();
Yabin Cuidd401b32018-04-11 11:17:06 -0700577 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700578 std::vector<Symbol> symbols;
579 BuildId build_id = GetExpectedBuildId();
580 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
581 if (symbol.is_func || (symbol.is_label && symbol.is_in_text_section)) {
582 symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
583 }
584 };
585 ElfStatus status;
Yabin Cui01947032020-06-30 14:36:46 -0700586 auto elf = ElfFile::Open(debug_file_path_, &build_id, &status);
587 if (elf) {
588 status = elf->ParseSymbols(symbol_callback);
Yabin Cui516a87c2018-03-26 17:34:00 -0700589 }
590 ReportReadElfSymbolResult(status, path_, debug_file_path_,
591 symbols_.empty() ? android::base::WARNING : android::base::DEBUG);
592 SortAndFixSymbols(symbols);
593 return symbols;
594 }
595
596 private:
Yabin Cuidb2c4932019-02-07 15:06:42 -0800597 static constexpr uint64_t uninitialized_value = std::numeric_limits<uint64_t>::max();
598
599 uint64_t min_vaddr_ = uninitialized_value;
600 uint64_t file_offset_of_min_vaddr_ = uninitialized_value;
Yabin Cuidd401b32018-04-11 11:17:06 -0700601 std::unique_ptr<DexFileDso> dex_file_dso_;
Yabin Cui516a87c2018-03-26 17:34:00 -0700602};
603
604class KernelDso : public Dso {
605 public:
606 KernelDso(const std::string& path, const std::string& debug_file_path)
Yabin Cui7078c672020-11-10 16:24:12 -0800607 : Dso(DSO_KERNEL, path, debug_file_path) {
608 if (!vmlinux_.empty()) {
609 // Use vmlinux as the kernel debug file.
610 BuildId build_id = GetExpectedBuildId();
611 ElfStatus status;
612 if (ElfFile::Open(vmlinux_, &build_id, &status)) {
613 debug_file_path_ = vmlinux_;
614 has_debug_file_ = true;
615 }
616 } else if (IsRegularFile(debug_file_path_)) {
617 has_debug_file_ = true;
618 }
619 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700620
Yabin Cui7078c672020-11-10 16:24:12 -0800621 // IpToVaddrInFile() and LoadSymbols() must be consistent in fixing addresses changed by kernel
622 // address space layout randomization.
623 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override {
624 if (map_start != 0 && GetKernelStartAddr() != 0) {
625 // Fix kernel addresses changed by kernel address randomization.
626 fix_kernel_address_randomization_ = true;
627 return ip - map_start + GetKernelStartAddr();
628 }
629 return ip;
630 }
631
632 std::optional<uint64_t> IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t) override {
633 if (map_start != 0 && GetKernelStartOffset() != 0) {
634 return ip - map_start + GetKernelStartOffset();
635 }
636 return std::nullopt;
637 }
Yabin Cuidb2c4932019-02-07 15:06:42 -0800638
Yabin Cui516a87c2018-03-26 17:34:00 -0700639 protected:
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800640 std::vector<Symbol> LoadSymbolsImpl() override {
Yabin Cui516a87c2018-03-26 17:34:00 -0700641 std::vector<Symbol> symbols;
Yabin Cui7078c672020-11-10 16:24:12 -0800642 if (has_debug_file_) {
643 ReadSymbolsFromDebugFile(&symbols);
644 }
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100645
Yabin Cui7078c672020-11-10 16:24:12 -0800646 if (symbols.empty() && !kallsyms_.empty()) {
647 ReadSymbolsFromKallsyms(kallsyms_, &symbols);
648 }
Yabin Cui36b57d92020-12-17 17:06:27 -0800649#if defined(__linux__)
Yabin Cui7078c672020-11-10 16:24:12 -0800650 if (symbols.empty()) {
651 ReadSymbolsFromProc(&symbols);
652 }
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100653#endif // defined(__linux__)
Yabin Cui7078c672020-11-10 16:24:12 -0800654 SortAndFixSymbols(symbols);
655 if (!symbols.empty()) {
656 symbols.back().len = std::numeric_limits<uint64_t>::max() - symbols.back().addr;
657 }
658 return symbols;
659 }
660
661 private:
662 void ReadSymbolsFromDebugFile(std::vector<Symbol>* symbols) {
663 if (!fix_kernel_address_randomization_) {
664 LOG(WARNING) << "Don't know how to fix addresses changed by kernel address randomization. So "
665 "symbols in "
666 << debug_file_path_ << " are not used";
667 return;
668 }
669 // symbols_ are kernel symbols got from /proc/kallsyms while recording. Those symbols are
670 // not fixed for kernel address randomization. So clear them to avoid mixing them with
671 // symbols in debug_file_path.
672 symbols_.clear();
673
674 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
675 if (symbol.is_func) {
676 symbols->emplace_back(symbol.name, symbol.vaddr, symbol.len);
Yabin Cui01947032020-06-30 14:36:46 -0700677 }
Yabin Cui7078c672020-11-10 16:24:12 -0800678 };
679 ElfStatus status;
680 if (auto elf = ElfFile::Open(debug_file_path_, &status); elf) {
681 status = elf->ParseSymbols(symbol_callback);
682 }
683 ReportReadElfSymbolResult(status, path_, debug_file_path_);
684 }
685
686 void ReadSymbolsFromKallsyms(std::string& kallsyms, std::vector<Symbol>* symbols) {
687 auto symbol_callback = [&](const KernelSymbol& symbol) {
688 if (strchr("TtWw", symbol.type) && symbol.addr != 0u) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800689 if (symbol.module == nullptr) {
690 symbols->emplace_back(symbol.name, symbol.addr, 0);
691 } else {
692 std::string name = std::string(symbol.name) + " [" + symbol.module + "]";
693 symbols->emplace_back(name, symbol.addr, 0);
694 }
Yabin Cui7078c672020-11-10 16:24:12 -0800695 }
696 return false;
697 };
698 ProcessKernelSymbols(kallsyms, symbol_callback);
699 if (symbols->empty()) {
700 LOG(WARNING) << "Symbol addresses in /proc/kallsyms on device are all zero. "
701 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
702 }
703 }
704
Yabin Cui36b57d92020-12-17 17:06:27 -0800705#if defined(__linux__)
Yabin Cui7078c672020-11-10 16:24:12 -0800706 void ReadSymbolsFromProc(std::vector<Symbol>* symbols) {
707 BuildId build_id = GetExpectedBuildId();
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100708 if (!build_id.IsEmpty()) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700709 // Try /proc/kallsyms only when asked to do so, or when build id matches.
710 // Otherwise, it is likely to use /proc/kallsyms on host for perf.data recorded on device.
711 bool can_read_kallsyms = true;
712 if (!build_id.IsEmpty()) {
713 BuildId real_build_id;
714 if (!GetKernelBuildId(&real_build_id) || build_id != real_build_id) {
715 LOG(DEBUG) << "failed to read symbols from /proc/kallsyms: Build id mismatch";
716 can_read_kallsyms = false;
717 }
718 }
719 if (can_read_kallsyms) {
720 std::string kallsyms;
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100721 if (LoadKernelSymbols(&kallsyms)) {
Yabin Cui7078c672020-11-10 16:24:12 -0800722 ReadSymbolsFromKallsyms(kallsyms, symbols);
Yabin Cui516a87c2018-03-26 17:34:00 -0700723 }
724 }
725 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700726 }
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100727#endif // defined(__linux__)
Yabin Cui516a87c2018-03-26 17:34:00 -0700728
Yabin Cui7078c672020-11-10 16:24:12 -0800729 uint64_t GetKernelStartAddr() {
730 if (!kernel_start_addr_) {
731 ParseKernelStartAddr();
Yabin Cui516a87c2018-03-26 17:34:00 -0700732 }
Yabin Cui7078c672020-11-10 16:24:12 -0800733 return kernel_start_addr_.value();
Yabin Cui516a87c2018-03-26 17:34:00 -0700734 }
Yabin Cui7078c672020-11-10 16:24:12 -0800735
736 uint64_t GetKernelStartOffset() {
737 if (!kernel_start_file_offset_) {
738 ParseKernelStartAddr();
739 }
740 return kernel_start_file_offset_.value();
741 }
742
743 void ParseKernelStartAddr() {
744 kernel_start_addr_ = 0;
745 kernel_start_file_offset_ = 0;
746 if (has_debug_file_) {
747 ElfStatus status;
748 if (auto elf = ElfFile::Open(debug_file_path_, &status); elf) {
749 for (const auto& section : elf->GetSectionHeader()) {
750 if (section.name == ".text") {
751 kernel_start_addr_ = section.vaddr;
752 kernel_start_file_offset_ = section.file_offset;
753 break;
754 }
755 }
756 }
757 }
758 }
759
760 bool has_debug_file_ = false;
761 bool fix_kernel_address_randomization_ = false;
762 std::optional<uint64_t> kernel_start_addr_;
763 std::optional<uint64_t> kernel_start_file_offset_;
Yabin Cui516a87c2018-03-26 17:34:00 -0700764};
765
766class KernelModuleDso : public Dso {
767 public:
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800768 KernelModuleDso(const std::string& path, const std::string& debug_file_path,
769 uint64_t memory_start, uint64_t memory_end, Dso* kernel_dso)
770 : Dso(DSO_KERNEL_MODULE, path, debug_file_path),
771 memory_start_(memory_start),
772 memory_end_(memory_end),
773 kernel_dso_(kernel_dso) {}
774
775 void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t memory_offset) override {
776 min_vaddr_ = min_vaddr;
777 memory_offset_of_min_vaddr_ = memory_offset;
778 }
779
780 void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* memory_offset) override {
781 if (!min_vaddr_) {
782 CalculateMinVaddr();
783 }
784 *min_vaddr = min_vaddr_.value();
785 *memory_offset = memory_offset_of_min_vaddr_.value();
786 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700787
Yabin Cuidb2c4932019-02-07 15:06:42 -0800788 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800789 uint64_t min_vaddr;
790 uint64_t memory_offset;
791 GetMinExecutableVaddr(&min_vaddr, &memory_offset);
792 return ip - map_start - memory_offset + min_vaddr;
Yabin Cuidb2c4932019-02-07 15:06:42 -0800793 }
794
Yabin Cui516a87c2018-03-26 17:34:00 -0700795 protected:
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800796 std::vector<Symbol> LoadSymbolsImpl() override {
Yabin Cui516a87c2018-03-26 17:34:00 -0700797 std::vector<Symbol> symbols;
798 BuildId build_id = GetExpectedBuildId();
799 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800800 // We only know how to map ip addrs to symbols in text section.
801 if (symbol.is_in_text_section && (symbol.is_label || symbol.is_func)) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700802 symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
803 }
804 };
Yabin Cui01947032020-06-30 14:36:46 -0700805 ElfStatus status;
806 auto elf = ElfFile::Open(debug_file_path_, &build_id, &status);
807 if (elf) {
808 status = elf->ParseSymbols(symbol_callback);
809 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700810 ReportReadElfSymbolResult(status, path_, debug_file_path_,
811 symbols_.empty() ? android::base::WARNING : android::base::DEBUG);
812 SortAndFixSymbols(symbols);
813 return symbols;
814 }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800815
816 private:
817 void CalculateMinVaddr() {
818 min_vaddr_ = 0;
819 memory_offset_of_min_vaddr_ = 0;
820
821 // min_vaddr and memory_offset are used to convert an ip addr of a kernel module to its
822 // vaddr_in_file, as shown in IpToVaddrInFile(). When the kernel loads a kernel module, it
823 // puts ALLOC sections (like .plt, .text.ftrace_trampoline, .text) in memory in order. The
824 // text section may not be at the start of the module memory. To do address conversion, we
825 // need to know its relative position in the module memory. There are two ways:
826 // 1. Read the kernel module file to calculate the relative position of .text section. It
827 // is relatively complex and depends on both PLT entries and the kernel version.
828 // 2. Find a module symbol in .text section, get its address in memory from /proc/kallsyms, and
829 // its vaddr_in_file from the kernel module file. Then other symbols in .text section can be
830 // mapped in the same way.
831 // Below we use the second method.
832
833 // 1. Select a module symbol in /proc/kallsyms.
834 kernel_dso_->LoadSymbols();
835 const auto& kernel_symbols = kernel_dso_->GetSymbols();
836 auto it = std::lower_bound(kernel_symbols.begin(), kernel_symbols.end(), memory_start_,
837 CompareSymbolToAddr);
838 const Symbol* kernel_symbol = nullptr;
839 while (it != kernel_symbols.end() && it->addr < memory_end_) {
840 if (strlen(it->Name()) > 0 && it->Name()[0] != '$') {
841 kernel_symbol = &*it;
842 break;
843 }
844 ++it;
845 }
846 if (kernel_symbol == nullptr) {
847 return;
848 }
849
850 // 2. Find the symbol in .ko file.
851 std::string symbol_name = kernel_symbol->Name();
852 if (auto pos = symbol_name.rfind(' '); pos != std::string::npos) {
853 symbol_name.resize(pos);
854 }
855 LoadSymbols();
856 for (const auto& symbol : symbols_) {
857 if (symbol_name == symbol.Name()) {
858 min_vaddr_ = symbol.addr;
859 memory_offset_of_min_vaddr_ = kernel_symbol->addr - memory_start_;
860 return;
861 }
862 }
863 }
864
865 uint64_t memory_start_;
866 uint64_t memory_end_;
867 Dso* kernel_dso_;
868 std::optional<uint64_t> min_vaddr_;
869 std::optional<uint64_t> memory_offset_of_min_vaddr_;
Yabin Cui516a87c2018-03-26 17:34:00 -0700870};
871
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200872class SymbolMapFileDso : public Dso {
873 public:
874 SymbolMapFileDso(const std::string& path) : Dso(DSO_SYMBOL_MAP_FILE, path, path) {}
875
876 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; }
877
878 protected:
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800879 std::vector<Symbol> LoadSymbolsImpl() override { return {}; }
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200880};
881
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700882class UnknownDso : public Dso {
883 public:
884 UnknownDso(const std::string& path) : Dso(DSO_UNKNOWN_FILE, path, path) {}
885
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200886 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; }
Yabin Cuidb2c4932019-02-07 15:06:42 -0800887
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700888 protected:
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800889 std::vector<Symbol> LoadSymbolsImpl() override { return std::vector<Symbol>(); }
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700890};
891
Yabin Cui516a87c2018-03-26 17:34:00 -0700892std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path,
893 bool force_64bit) {
Yabin Cui7078c672020-11-10 16:24:12 -0800894 BuildId build_id = FindExpectedBuildIdForPath(dso_path);
895 std::string debug_path = debug_elf_file_finder_.FindDebugFile(dso_path, force_64bit, build_id);
Yabin Cui516a87c2018-03-26 17:34:00 -0700896 switch (dso_type) {
Yabin Cui7078c672020-11-10 16:24:12 -0800897 case DSO_ELF_FILE:
898 return std::unique_ptr<Dso>(new ElfDso(dso_path, debug_path));
Yabin Cui516a87c2018-03-26 17:34:00 -0700899 case DSO_KERNEL:
Yabin Cui7078c672020-11-10 16:24:12 -0800900 return std::unique_ptr<Dso>(new KernelDso(dso_path, debug_path));
Yabin Cui516a87c2018-03-26 17:34:00 -0700901 case DSO_DEX_FILE:
Yabin Cui40b70ff2018-04-09 14:06:08 -0700902 return std::unique_ptr<Dso>(new DexFileDso(dso_path, dso_path));
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200903 case DSO_SYMBOL_MAP_FILE:
904 return std::unique_ptr<Dso>(new SymbolMapFileDso(dso_path));
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700905 case DSO_UNKNOWN_FILE:
906 return std::unique_ptr<Dso>(new UnknownDso(dso_path));
Yabin Cui516a87c2018-03-26 17:34:00 -0700907 default:
908 LOG(FATAL) << "Unexpected dso_type " << static_cast<int>(dso_type);
909 }
910 return nullptr;
911}
912
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700913std::unique_ptr<Dso> Dso::CreateElfDsoWithBuildId(const std::string& dso_path, BuildId& build_id) {
914 return std::unique_ptr<Dso>(
915 new ElfDso(dso_path, debug_elf_file_finder_.FindDebugFile(dso_path, false, build_id)));
916}
917
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800918std::unique_ptr<Dso> Dso::CreateKernelModuleDso(const std::string& dso_path, uint64_t memory_start,
919 uint64_t memory_end, Dso* kernel_dso) {
920 BuildId build_id = FindExpectedBuildIdForPath(dso_path);
921 std::string debug_path = debug_elf_file_finder_.FindDebugFile(dso_path, false, build_id);
922 return std::unique_ptr<Dso>(
923 new KernelModuleDso(dso_path, debug_path, memory_start, memory_end, kernel_dso));
924}
925
Yabin Cui767dd172016-06-02 21:02:43 -0700926const char* DsoTypeToString(DsoType dso_type) {
927 switch (dso_type) {
928 case DSO_KERNEL:
929 return "dso_kernel";
930 case DSO_KERNEL_MODULE:
931 return "dso_kernel_module";
932 case DSO_ELF_FILE:
933 return "dso_elf_file";
Yabin Cui516a87c2018-03-26 17:34:00 -0700934 case DSO_DEX_FILE:
935 return "dso_dex_file";
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200936 case DSO_SYMBOL_MAP_FILE:
937 return "dso_symbol_map_file";
Yabin Cui767dd172016-06-02 21:02:43 -0700938 default:
939 return "unknown";
940 }
941}
Yabin Cui40b70ff2018-04-09 14:06:08 -0700942
943bool GetBuildIdFromDsoPath(const std::string& dso_path, BuildId* build_id) {
Yabin Cui3a880452020-06-29 16:37:31 -0700944 ElfStatus status;
945 auto elf = ElfFile::Open(dso_path, &status);
946 if (status == ElfStatus::NO_ERROR && elf->GetBuildId(build_id) == ElfStatus::NO_ERROR) {
947 return true;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700948 }
Yabin Cui3a880452020-06-29 16:37:31 -0700949 return false;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700950}
Yabin Cuifaa7b922021-01-11 17:35:57 -0800951
952} // namespace simpleperf