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 #include "dso.h"
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <algorithm>
23 #include <limits>
24 #include <memory>
25 #include <optional>
26 #include <string_view>
27 #include <vector>
28
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 #include <android-base/strings.h>
32
33 #include "JITDebugReader.h"
34 #include "environment.h"
35 #include "kallsyms.h"
36 #include "read_apk.h"
37 #include "read_dex_file.h"
38 #include "read_elf.h"
39 #include "utils.h"
40
41 namespace simpleperf {
42
43 using android::base::EndsWith;
44 using android::base::StartsWith;
45
46 namespace simpleperf_dso_impl {
47
RemovePathSeparatorSuffix(const std::string & path)48 std::string RemovePathSeparatorSuffix(const std::string& path) {
49 // Don't remove path separator suffix for '/'.
50 if (EndsWith(path, OS_PATH_SEPARATOR) && path.size() > 1u) {
51 return path.substr(0, path.size() - 1);
52 }
53 return path;
54 }
55
Reset()56 void DebugElfFileFinder::Reset() {
57 vdso_64bit_.clear();
58 vdso_32bit_.clear();
59 symfs_dir_.clear();
60 build_id_to_file_map_.clear();
61 }
62
SetSymFsDir(const std::string & symfs_dir)63 bool DebugElfFileFinder::SetSymFsDir(const std::string& symfs_dir) {
64 symfs_dir_ = RemovePathSeparatorSuffix(symfs_dir);
65 if (!IsDir(symfs_dir_)) {
66 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir_ << "'";
67 return false;
68 }
69 std::string build_id_list_file = symfs_dir_ + OS_PATH_SEPARATOR + "build_id_list";
70 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")) {
73 std::vector<std::string> items = android::base::Split(line, "=");
74 if (items.size() == 2u) {
75 build_id_to_file_map_[items[0]] = symfs_dir_ + OS_PATH_SEPARATOR + items[1];
76 }
77 }
78 }
79 return true;
80 }
81
AddSymbolDir(const std::string & symbol_dir)82 bool DebugElfFileFinder::AddSymbolDir(const std::string& symbol_dir) {
83 if (!IsDir(symbol_dir)) {
84 LOG(ERROR) << "Invalid symbol dir " << symbol_dir;
85 return false;
86 }
87 std::string dir = RemovePathSeparatorSuffix(symbol_dir);
88 CollectBuildIdInDir(dir);
89 return true;
90 }
91
CollectBuildIdInDir(const std::string & dir)92 void DebugElfFileFinder::CollectBuildIdInDir(const std::string& dir) {
93 for (const std::string& entry : GetEntriesInDir(dir)) {
94 std::string path = dir + OS_PATH_SEPARATOR + entry;
95 if (IsDir(path)) {
96 CollectBuildIdInDir(path);
97 } else {
98 BuildId build_id;
99 ElfStatus status;
100 auto elf = ElfFile::Open(path, &status);
101 if (status == ElfStatus::NO_ERROR && elf->GetBuildId(&build_id) == ElfStatus::NO_ERROR) {
102 build_id_to_file_map_[build_id.ToString()] = path;
103 }
104 }
105 }
106 }
107
SetVdsoFile(const std::string & vdso_file,bool is_64bit)108 void 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
CheckDebugFilePath(const std::string & path,BuildId & build_id,bool report_build_id_mismatch)116 static 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
FindDebugFile(const std::string & dso_path,bool force_64bit,BuildId & build_id)139 std::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 }
147 }
148 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 }
152
153 // 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());
157 if (it != build_id_to_file_map_.end() && CheckDebugFilePath(it->second, build_id, false)) {
158 return it->second;
159 }
160 }
161 }
162 if (!symfs_dir_.empty()) {
163 // 2. Try concatenating symfs_dir and dso_path.
164 std::string path = GetPathInSymFsDir(dso_path);
165 if (CheckDebugFilePath(path, build_id, true)) {
166 return path;
167 }
168 if (EndsWith(dso_path, ".apk") && IsRegularFile(path)) {
169 return path;
170 }
171 // 3. Try concatenating symfs_dir and basename of dso_path.
172 path = symfs_dir_ + OS_PATH_SEPARATOR + android::base::Basename(dso_path);
173 if (CheckDebugFilePath(path, build_id, false)) {
174 return path;
175 }
176 }
177 // 4. Try concatenating /usr/lib/debug and dso_path.
178 // Linux host can store debug shared libraries in /usr/lib/debug.
179 if (CheckDebugFilePath("/usr/lib/debug" + dso_path, build_id, false)) {
180 return "/usr/lib/debug" + dso_path;
181 }
182 return dso_path;
183 }
184
GetPathInSymFsDir(const std::string & path)185 std::string DebugElfFileFinder::GetPathInSymFsDir(const std::string& path) {
186 auto add_symfs_prefix = [&](const std::string& path) {
187 if (StartsWith(path, OS_PATH_SEPARATOR)) {
188 return symfs_dir_ + path;
189 }
190 return symfs_dir_ + OS_PATH_SEPARATOR + path;
191 };
192 if (OS_PATH_SEPARATOR == '/') {
193 return add_symfs_prefix(path);
194 }
195 // Paths in recorded perf.data uses '/' as path separator. When reporting on Windows, it needs
196 // to be converted to '\\'.
197 auto tuple = SplitUrlInApk(path);
198 if (std::get<0>(tuple)) {
199 std::string apk_path = std::get<1>(tuple);
200 std::string entry_path = std::get<2>(tuple);
201 std::replace(apk_path.begin(), apk_path.end(), '/', OS_PATH_SEPARATOR);
202 return GetUrlInApk(add_symfs_prefix(apk_path), entry_path);
203 }
204 std::string elf_path = path;
205 std::replace(elf_path.begin(), elf_path.end(), '/', OS_PATH_SEPARATOR);
206 return add_symfs_prefix(elf_path);
207 }
208 } // namespace simpleperf_dso_impl
209
210 static OneTimeFreeAllocator symbol_name_allocator;
211
Symbol(std::string_view name,uint64_t addr,uint64_t len)212 Symbol::Symbol(std::string_view name, uint64_t addr, uint64_t len)
213 : addr(addr),
214 len(len),
215 name_(symbol_name_allocator.AllocateString(name)),
216 demangled_name_(nullptr),
217 dump_id_(UINT_MAX) {}
218
DemangledName() const219 const char* Symbol::DemangledName() const {
220 if (demangled_name_ == nullptr) {
221 const std::string s = Dso::Demangle(name_);
222 SetDemangledName(s);
223 }
224 return demangled_name_;
225 }
226
SetDemangledName(std::string_view name) const227 void Symbol::SetDemangledName(std::string_view name) const {
228 if (name == name_) {
229 demangled_name_ = name_;
230 } else {
231 demangled_name_ = symbol_name_allocator.AllocateString(name);
232 }
233 }
234
FunctionName() const235 std::string_view Symbol::FunctionName() const {
236 // Name with signature is like "void ctep.v(cteo, ctgc, ctbn)".
237 std::string_view name = DemangledName();
238 auto brace_pos = name.find('(');
239 if (brace_pos != name.npos) {
240 name = name.substr(0, brace_pos);
241 auto space_pos = name.rfind(' ');
242 if (space_pos != name.npos) {
243 name = name.substr(space_pos + 1);
244 }
245 }
246 return name;
247 }
248
CompareSymbolToAddr(const Symbol & s,uint64_t addr)249 static bool CompareSymbolToAddr(const Symbol& s, uint64_t addr) {
250 return s.addr < addr;
251 }
252
CompareAddrToSymbol(uint64_t addr,const Symbol & s)253 static bool CompareAddrToSymbol(uint64_t addr, const Symbol& s) {
254 return addr < s.addr;
255 }
256
257 bool Dso::demangle_ = true;
258 std::string Dso::vmlinux_;
259 std::string Dso::kallsyms_;
260 std::unordered_map<std::string, BuildId> Dso::build_id_map_;
261 size_t Dso::dso_count_;
262 uint32_t Dso::g_dump_id_;
263 simpleperf_dso_impl::DebugElfFileFinder Dso::debug_elf_file_finder_;
264
SetDemangle(bool demangle)265 void Dso::SetDemangle(bool demangle) {
266 demangle_ = demangle;
267 }
268
269 extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n, int* status);
270 #if defined(__linux__) || defined(__darwin__)
271 extern "C" char* rustc_demangle(const char* mangled, char* out, size_t* len, int* status);
272 #endif
273
Demangle(const std::string & name)274 std::string Dso::Demangle(const std::string& name) {
275 if (!demangle_) {
276 return name;
277 }
278 int status;
279 bool is_linker_symbol = (name.find(linker_prefix) == 0);
280 const char* mangled_str = name.c_str();
281 if (is_linker_symbol) {
282 mangled_str += linker_prefix.size();
283 }
284
285 if (mangled_str[0] == '_') {
286 char* demangled_name = nullptr;
287 int status = -2; // -2 means name didn't demangle.
288 if (mangled_str[1] == 'Z') {
289 demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
290 #if defined(__linux__) || defined(__darwin__)
291 } else if (mangled_str[1] == 'R') {
292 demangled_name = rustc_demangle(mangled_str, nullptr, nullptr, &status);
293 #endif
294 }
295 if (status == 0) {
296 // demangled successfully
297 std::string result;
298 if (is_linker_symbol) {
299 result = std::string("[linker]") + demangled_name;
300 } else {
301 result = demangled_name;
302 }
303 free(demangled_name);
304 return result;
305 }
306 }
307
308 // failed to demangle
309 if (is_linker_symbol) {
310 return std::string("[linker]") + mangled_str;
311 }
312 return name;
313 }
314
SetSymFsDir(const std::string & symfs_dir)315 bool Dso::SetSymFsDir(const std::string& symfs_dir) {
316 return debug_elf_file_finder_.SetSymFsDir(symfs_dir);
317 }
318
AddSymbolDir(const std::string & symbol_dir)319 bool Dso::AddSymbolDir(const std::string& symbol_dir) {
320 return debug_elf_file_finder_.AddSymbolDir(symbol_dir);
321 }
322
SetVmlinux(const std::string & vmlinux)323 void Dso::SetVmlinux(const std::string& vmlinux) {
324 vmlinux_ = vmlinux;
325 }
326
SetBuildIds(const std::vector<std::pair<std::string,BuildId>> & build_ids)327 void Dso::SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids) {
328 std::unordered_map<std::string, BuildId> map;
329 for (auto& pair : build_ids) {
330 LOG(DEBUG) << "build_id_map: " << pair.first << ", " << pair.second.ToString();
331 map.insert(pair);
332 }
333 build_id_map_ = std::move(map);
334 }
335
SetVdsoFile(const std::string & vdso_file,bool is_64bit)336 void Dso::SetVdsoFile(const std::string& vdso_file, bool is_64bit) {
337 debug_elf_file_finder_.SetVdsoFile(vdso_file, is_64bit);
338 }
339
FindExpectedBuildIdForPath(const std::string & path)340 BuildId Dso::FindExpectedBuildIdForPath(const std::string& path) {
341 auto it = build_id_map_.find(path);
342 if (it != build_id_map_.end()) {
343 return it->second;
344 }
345 return BuildId();
346 }
347
GetExpectedBuildId() const348 BuildId Dso::GetExpectedBuildId() const {
349 return FindExpectedBuildIdForPath(path_);
350 }
351
Dso(DsoType type,const std::string & path)352 Dso::Dso(DsoType type, const std::string& path)
353 : type_(type),
354 path_(path),
355 is_loaded_(false),
356 dump_id_(UINT_MAX),
357 symbol_dump_id_(0),
358 symbol_warning_loglevel_(android::base::WARNING) {
359 size_t pos = path.find_last_of("/\\");
360 if (pos != std::string::npos) {
361 file_name_ = path.substr(pos + 1);
362 } else {
363 file_name_ = path;
364 }
365 dso_count_++;
366 }
367
~Dso()368 Dso::~Dso() {
369 if (--dso_count_ == 0) {
370 // Clean up global variables when no longer used.
371 symbol_name_allocator.Clear();
372 demangle_ = true;
373 vmlinux_.clear();
374 kallsyms_.clear();
375 build_id_map_.clear();
376 g_dump_id_ = 0;
377 debug_elf_file_finder_.Reset();
378 }
379 }
380
CreateDumpId()381 uint32_t Dso::CreateDumpId() {
382 CHECK(!HasDumpId());
383 return dump_id_ = g_dump_id_++;
384 }
385
CreateSymbolDumpId(const Symbol * symbol)386 uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
387 CHECK(!symbol->HasDumpId());
388 symbol->dump_id_ = symbol_dump_id_++;
389 return symbol->dump_id_;
390 }
391
IpToFileOffset(uint64_t ip,uint64_t map_start,uint64_t map_pgoff)392 std::optional<uint64_t> Dso::IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) {
393 return ip - map_start + map_pgoff;
394 }
395
FindSymbol(uint64_t vaddr_in_dso)396 const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
397 if (!is_loaded_) {
398 LoadSymbols();
399 }
400 auto it = std::upper_bound(symbols_.begin(), symbols_.end(), vaddr_in_dso, CompareAddrToSymbol);
401 if (it != symbols_.begin()) {
402 --it;
403 if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
404 return &*it;
405 }
406 }
407 if (!unknown_symbols_.empty()) {
408 auto it = unknown_symbols_.find(vaddr_in_dso);
409 if (it != unknown_symbols_.end()) {
410 return &it->second;
411 }
412 }
413 return nullptr;
414 }
415
SetSymbols(std::vector<Symbol> * symbols)416 void Dso::SetSymbols(std::vector<Symbol>* symbols) {
417 symbols_ = std::move(*symbols);
418 symbols->clear();
419 }
420
AddUnknownSymbol(uint64_t vaddr_in_dso,const std::string & name)421 void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) {
422 unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1)));
423 }
424
IsForJavaMethod() const425 bool Dso::IsForJavaMethod() const {
426 if (type_ == DSO_DEX_FILE) {
427 return true;
428 }
429 if (type_ == DSO_ELF_FILE) {
430 if (JITDebugReader::IsPathInJITSymFile(path_)) {
431 return true;
432 }
433 // JITDebugReader in old versions generates symfiles in 'TemporaryFile-XXXXXX'.
434 size_t pos = path_.rfind('/');
435 pos = (pos == std::string::npos) ? 0 : pos + 1;
436 return StartsWith(std::string_view(&path_[pos], path_.size() - pos), "TemporaryFile");
437 }
438 return false;
439 }
440
LoadSymbols()441 void Dso::LoadSymbols() {
442 if (!is_loaded_) {
443 is_loaded_ = true;
444 std::vector<Symbol> symbols = LoadSymbolsImpl();
445 if (symbols_.empty()) {
446 symbols_ = std::move(symbols);
447 } else {
448 std::vector<Symbol> merged_symbols;
449 std::set_union(symbols_.begin(), symbols_.end(), symbols.begin(), symbols.end(),
450 std::back_inserter(merged_symbols), Symbol::CompareValueByAddr);
451 symbols_ = std::move(merged_symbols);
452 }
453 }
454 }
455
ReportReadElfSymbolResult(ElfStatus result,const std::string & path,const std::string & debug_file_path,android::base::LogSeverity warning_loglevel=android::base::WARNING)456 static void ReportReadElfSymbolResult(
457 ElfStatus result, const std::string& path, const std::string& debug_file_path,
458 android::base::LogSeverity warning_loglevel = android::base::WARNING) {
459 if (result == ElfStatus::NO_ERROR) {
460 LOG(VERBOSE) << "Read symbols from " << debug_file_path << " successfully";
461 } else if (result == ElfStatus::NO_SYMBOL_TABLE) {
462 if (path == "[vdso]") {
463 // Vdso only contains dynamic symbol table, and we can't change that.
464 return;
465 }
466 // Lacking symbol table isn't considered as an error but worth reporting.
467 LOG(warning_loglevel) << debug_file_path << " doesn't contain symbol table";
468 } else {
469 LOG(warning_loglevel) << "failed to read symbols from " << debug_file_path << ": " << result;
470 }
471 }
472
SortAndFixSymbols(std::vector<Symbol> & symbols)473 static void SortAndFixSymbols(std::vector<Symbol>& symbols) {
474 std::sort(symbols.begin(), symbols.end(), Symbol::CompareValueByAddr);
475 Symbol* prev_symbol = nullptr;
476 for (auto& symbol : symbols) {
477 if (prev_symbol != nullptr && prev_symbol->len == 0) {
478 prev_symbol->len = symbol.addr - prev_symbol->addr;
479 }
480 prev_symbol = &symbol;
481 }
482 }
483
484 class DexFileDso : public Dso {
485 public:
DexFileDso(const std::string & path)486 DexFileDso(const std::string& path) : Dso(DSO_DEX_FILE, path) {}
487
AddDexFileOffset(uint64_t dex_file_offset)488 void AddDexFileOffset(uint64_t dex_file_offset) override {
489 auto it = std::lower_bound(dex_file_offsets_.begin(), dex_file_offsets_.end(), dex_file_offset);
490 if (it != dex_file_offsets_.end() && *it == dex_file_offset) {
491 return;
492 }
493 dex_file_offsets_.insert(it, dex_file_offset);
494 }
495
DexFileOffsets()496 const std::vector<uint64_t>* DexFileOffsets() override { return &dex_file_offsets_; }
497
IpToVaddrInFile(uint64_t ip,uint64_t map_start,uint64_t map_pgoff)498 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override {
499 return ip - map_start + map_pgoff;
500 }
501
LoadSymbolsImpl()502 std::vector<Symbol> LoadSymbolsImpl() override {
503 std::vector<Symbol> symbols;
504 if (StartsWith(path_, kDexFileInMemoryPrefix)) {
505 // For dex file in memory, the symbols should already be set via SetSymbols().
506 return symbols;
507 }
508
509 const std::string& debug_file_path = GetDebugFilePath();
510 auto tuple = SplitUrlInApk(debug_file_path);
511 // Symbols of dex files are collected on device. If the dex file doesn't exist, probably
512 // we are reporting on host, and there is no need to report warning of missing dex files.
513 if (!IsRegularFile(std::get<0>(tuple) ? std::get<1>(tuple) : debug_file_path)) {
514 LOG(DEBUG) << "skip reading symbols from non-exist dex_file " << debug_file_path;
515 return symbols;
516 }
517 bool status = false;
518 auto symbol_callback = [&](DexFileSymbol* symbol) {
519 symbols.emplace_back(symbol->name, symbol->addr, symbol->size);
520 };
521 if (std::get<0>(tuple)) {
522 std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(std::get<1>(tuple));
523 ZipEntry entry;
524 std::vector<uint8_t> data;
525 if (ahelper && ahelper->FindEntry(std::get<2>(tuple), &entry) &&
526 ahelper->GetEntryData(entry, &data)) {
527 status = ReadSymbolsFromDexFileInMemory(data.data(), data.size(), debug_file_path,
528 dex_file_offsets_, symbol_callback);
529 }
530 } else {
531 status = ReadSymbolsFromDexFile(debug_file_path, dex_file_offsets_, symbol_callback);
532 }
533 if (!status) {
534 android::base::LogSeverity level =
535 symbols_.empty() ? android::base::WARNING : android::base::DEBUG;
536 LOG(level) << "Failed to read symbols from dex_file " << debug_file_path;
537 return symbols;
538 }
539 LOG(VERBOSE) << "Read symbols from dex_file " << debug_file_path << " successfully";
540 SortAndFixSymbols(symbols);
541 return symbols;
542 }
543
544 private:
545 std::vector<uint64_t> dex_file_offsets_;
546 };
547
548 class ElfDso : public Dso {
549 public:
ElfDso(const std::string & path,bool force_64bit)550 ElfDso(const std::string& path, bool force_64bit)
551 : Dso(DSO_ELF_FILE, path), force_64bit_(force_64bit) {}
552
GetReportPath() const553 std::string_view GetReportPath() const override {
554 if (JITDebugReader::IsPathInJITSymFile(path_)) {
555 if (path_.find(kJITAppCacheFile) != path_.npos) {
556 return "[JIT app cache]";
557 }
558 return "[JIT zygote cache]";
559 }
560 return path_;
561 }
562
SetMinExecutableVaddr(uint64_t min_vaddr,uint64_t file_offset)563 void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t file_offset) override {
564 min_vaddr_ = min_vaddr;
565 file_offset_of_min_vaddr_ = file_offset;
566 }
567
GetMinExecutableVaddr(uint64_t * min_vaddr,uint64_t * file_offset)568 void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* file_offset) override {
569 if (type_ == DSO_DEX_FILE) {
570 return dex_file_dso_->GetMinExecutableVaddr(min_vaddr, file_offset);
571 }
572 if (min_vaddr_ == uninitialized_value) {
573 min_vaddr_ = 0;
574 BuildId build_id = GetExpectedBuildId();
575
576 ElfStatus status;
577 auto elf = ElfFile::Open(GetDebugFilePath(), &build_id, &status);
578 if (elf) {
579 min_vaddr_ = elf->ReadMinExecutableVaddr(&file_offset_of_min_vaddr_);
580 } else {
581 // This is likely to be a file wrongly thought of as an ELF file, due to stack unwinding.
582 // No need to report it by default.
583 LOG(DEBUG) << "failed to read min virtual address of " << GetDebugFilePath() << ": "
584 << status;
585 }
586 }
587 *min_vaddr = min_vaddr_;
588 *file_offset = file_offset_of_min_vaddr_;
589 }
590
IpToVaddrInFile(uint64_t ip,uint64_t map_start,uint64_t map_pgoff)591 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override {
592 if (type_ == DSO_DEX_FILE) {
593 return dex_file_dso_->IpToVaddrInFile(ip, map_start, map_pgoff);
594 }
595 uint64_t min_vaddr;
596 uint64_t file_offset_of_min_vaddr;
597 GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
598 if (file_offset_of_min_vaddr == uninitialized_value) {
599 return ip - map_start + min_vaddr;
600 }
601 // Apps may make part of the executable segment of a shared library writeable, which can
602 // generate multiple executable segments at runtime. So use map_pgoff to calculate
603 // vaddr_in_file.
604 return ip - map_start + map_pgoff - file_offset_of_min_vaddr + min_vaddr;
605 }
606
AddDexFileOffset(uint64_t dex_file_offset)607 void AddDexFileOffset(uint64_t dex_file_offset) override {
608 if (type_ == DSO_ELF_FILE) {
609 // When simpleperf does unwinding while recording, it processes mmap records before reading
610 // dex file linked list (via JITDebugReader). To process mmap records, it creates Dso
611 // objects of type ELF_FILE. Then after reading dex file linked list, it realizes some
612 // ELF_FILE Dso objects should actually be DEX_FILE, because they have dex file offsets.
613 // So here converts ELF_FILE Dso into DEX_FILE Dso.
614 type_ = DSO_DEX_FILE;
615 dex_file_dso_.reset(new DexFileDso(path_));
616 }
617 dex_file_dso_->AddDexFileOffset(dex_file_offset);
618 }
619
DexFileOffsets()620 const std::vector<uint64_t>* DexFileOffsets() override {
621 return dex_file_dso_ ? dex_file_dso_->DexFileOffsets() : nullptr;
622 }
623
624 protected:
FindDebugFilePath() const625 std::string FindDebugFilePath() const override {
626 BuildId build_id = GetExpectedBuildId();
627 return debug_elf_file_finder_.FindDebugFile(path_, force_64bit_, build_id);
628 }
629
LoadSymbolsImpl()630 std::vector<Symbol> LoadSymbolsImpl() override {
631 if (dex_file_dso_) {
632 return dex_file_dso_->LoadSymbolsImpl();
633 }
634 std::vector<Symbol> symbols;
635 BuildId build_id = GetExpectedBuildId();
636 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
637 if (symbol.is_func || (symbol.is_label && symbol.is_in_text_section)) {
638 symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
639 }
640 };
641 ElfStatus status;
642 auto elf = ElfFile::Open(GetDebugFilePath(), &build_id, &status);
643 if (elf) {
644 status = elf->ParseSymbols(symbol_callback);
645 }
646 android::base::LogSeverity log_level = android::base::WARNING;
647 if (!symbols_.empty() || !symbols.empty()) {
648 // We already have some symbols when recording.
649 log_level = android::base::DEBUG;
650 }
651 if ((status == ElfStatus::FILE_NOT_FOUND || status == ElfStatus::FILE_MALFORMED) &&
652 build_id.IsEmpty()) {
653 // This is likely to be a file wrongly thought of as an ELF file, due to stack unwinding.
654 log_level = android::base::DEBUG;
655 }
656 ReportReadElfSymbolResult(status, path_, GetDebugFilePath(), log_level);
657 SortAndFixSymbols(symbols);
658 return symbols;
659 }
660
661 private:
662 static constexpr uint64_t uninitialized_value = std::numeric_limits<uint64_t>::max();
663
664 bool force_64bit_;
665 uint64_t min_vaddr_ = uninitialized_value;
666 uint64_t file_offset_of_min_vaddr_ = uninitialized_value;
667 std::unique_ptr<DexFileDso> dex_file_dso_;
668 };
669
670 class KernelDso : public Dso {
671 public:
KernelDso(const std::string & path)672 KernelDso(const std::string& path) : Dso(DSO_KERNEL, path) {}
673
674 // IpToVaddrInFile() and LoadSymbols() must be consistent in fixing addresses changed by kernel
675 // address space layout randomization.
IpToVaddrInFile(uint64_t ip,uint64_t map_start,uint64_t)676 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override {
677 if (map_start != 0 && GetKernelStartAddr() != 0) {
678 // Fix kernel addresses changed by kernel address randomization.
679 fix_kernel_address_randomization_ = true;
680 return ip - map_start + GetKernelStartAddr();
681 }
682 return ip;
683 }
684
IpToFileOffset(uint64_t ip,uint64_t map_start,uint64_t)685 std::optional<uint64_t> IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t) override {
686 if (map_start != 0 && GetKernelStartOffset() != 0) {
687 return ip - map_start + GetKernelStartOffset();
688 }
689 return std::nullopt;
690 }
691
692 protected:
FindDebugFilePath() const693 std::string FindDebugFilePath() const override {
694 BuildId build_id = GetExpectedBuildId();
695 if (!vmlinux_.empty()) {
696 // Use vmlinux as the kernel debug file.
697 ElfStatus status;
698 if (ElfFile::Open(vmlinux_, &build_id, &status)) {
699 return vmlinux_;
700 }
701 }
702 return debug_elf_file_finder_.FindDebugFile(path_, false, build_id);
703 }
704
LoadSymbolsImpl()705 std::vector<Symbol> LoadSymbolsImpl() override {
706 std::vector<Symbol> symbols;
707 ReadSymbolsFromDebugFile(&symbols);
708
709 if (symbols.empty() && !kallsyms_.empty()) {
710 ReadSymbolsFromKallsyms(kallsyms_, &symbols);
711 }
712 #if defined(__linux__)
713 if (symbols.empty()) {
714 ReadSymbolsFromProc(&symbols);
715 }
716 #endif // defined(__linux__)
717 SortAndFixSymbols(symbols);
718 if (!symbols.empty() && symbols.back().len == 0) {
719 symbols.back().len = std::numeric_limits<uint64_t>::max() - symbols.back().addr;
720 }
721 return symbols;
722 }
723
724 private:
ReadSymbolsFromDebugFile(std::vector<Symbol> * symbols)725 void ReadSymbolsFromDebugFile(std::vector<Symbol>* symbols) {
726 ElfStatus status;
727 auto elf = ElfFile::Open(GetDebugFilePath(), &status);
728 if (!elf) {
729 return;
730 }
731
732 if (!fix_kernel_address_randomization_) {
733 LOG(WARNING) << "Don't know how to fix addresses changed by kernel address randomization. So "
734 "symbols in "
735 << GetDebugFilePath() << " are not used";
736 return;
737 }
738 // symbols_ are kernel symbols got from /proc/kallsyms while recording. Those symbols are
739 // not fixed for kernel address randomization. So clear them to avoid mixing them with
740 // symbols in debug_file_path.
741 symbols_.clear();
742
743 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
744 if (symbol.is_func) {
745 symbols->emplace_back(symbol.name, symbol.vaddr, symbol.len);
746 }
747 };
748 status = elf->ParseSymbols(symbol_callback);
749 ReportReadElfSymbolResult(status, path_, GetDebugFilePath());
750 }
751
ReadSymbolsFromKallsyms(std::string & kallsyms,std::vector<Symbol> * symbols)752 void ReadSymbolsFromKallsyms(std::string& kallsyms, std::vector<Symbol>* symbols) {
753 auto symbol_callback = [&](const KernelSymbol& symbol) {
754 if (strchr("TtWw", symbol.type) && symbol.addr != 0u) {
755 if (symbol.module == nullptr) {
756 symbols->emplace_back(symbol.name, symbol.addr, 0);
757 } else {
758 std::string name = std::string(symbol.name) + " [" + symbol.module + "]";
759 symbols->emplace_back(name, symbol.addr, 0);
760 }
761 }
762 return false;
763 };
764 ProcessKernelSymbols(kallsyms, symbol_callback);
765 if (symbols->empty()) {
766 LOG(WARNING) << "Symbol addresses in /proc/kallsyms on device are all zero. "
767 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
768 }
769 }
770
771 #if defined(__linux__)
ReadSymbolsFromProc(std::vector<Symbol> * symbols)772 void ReadSymbolsFromProc(std::vector<Symbol>* symbols) {
773 BuildId build_id = GetExpectedBuildId();
774 if (!build_id.IsEmpty()) {
775 // Try /proc/kallsyms only when asked to do so, or when build id matches.
776 // Otherwise, it is likely to use /proc/kallsyms on host for perf.data recorded on device.
777 bool can_read_kallsyms = true;
778 if (!build_id.IsEmpty()) {
779 BuildId real_build_id;
780 if (!GetKernelBuildId(&real_build_id) || build_id != real_build_id) {
781 LOG(DEBUG) << "failed to read symbols from /proc/kallsyms: Build id mismatch";
782 can_read_kallsyms = false;
783 }
784 }
785 if (can_read_kallsyms) {
786 std::string kallsyms;
787 if (LoadKernelSymbols(&kallsyms)) {
788 ReadSymbolsFromKallsyms(kallsyms, symbols);
789 }
790 }
791 }
792 }
793 #endif // defined(__linux__)
794
GetKernelStartAddr()795 uint64_t GetKernelStartAddr() {
796 if (!kernel_start_addr_) {
797 ParseKernelStartAddr();
798 }
799 return kernel_start_addr_.value();
800 }
801
GetKernelStartOffset()802 uint64_t GetKernelStartOffset() {
803 if (!kernel_start_file_offset_) {
804 ParseKernelStartAddr();
805 }
806 return kernel_start_file_offset_.value();
807 }
808
ParseKernelStartAddr()809 void ParseKernelStartAddr() {
810 kernel_start_addr_ = 0;
811 kernel_start_file_offset_ = 0;
812 ElfStatus status;
813 if (auto elf = ElfFile::Open(GetDebugFilePath(), &status); elf) {
814 for (const auto& section : elf->GetSectionHeader()) {
815 if (section.name == ".text") {
816 kernel_start_addr_ = section.vaddr;
817 kernel_start_file_offset_ = section.file_offset;
818 break;
819 }
820 }
821 }
822 }
823
824 bool fix_kernel_address_randomization_ = false;
825 std::optional<uint64_t> kernel_start_addr_;
826 std::optional<uint64_t> kernel_start_file_offset_;
827 };
828
829 class KernelModuleDso : public Dso {
830 public:
KernelModuleDso(const std::string & path,uint64_t memory_start,uint64_t memory_end,Dso * kernel_dso)831 KernelModuleDso(const std::string& path, uint64_t memory_start, uint64_t memory_end,
832 Dso* kernel_dso)
833 : Dso(DSO_KERNEL_MODULE, path),
834 memory_start_(memory_start),
835 memory_end_(memory_end),
836 kernel_dso_(kernel_dso) {}
837
SetMinExecutableVaddr(uint64_t min_vaddr,uint64_t memory_offset)838 void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t memory_offset) override {
839 min_vaddr_ = min_vaddr;
840 memory_offset_of_min_vaddr_ = memory_offset;
841 }
842
GetMinExecutableVaddr(uint64_t * min_vaddr,uint64_t * memory_offset)843 void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* memory_offset) override {
844 if (!min_vaddr_) {
845 CalculateMinVaddr();
846 }
847 *min_vaddr = min_vaddr_.value();
848 *memory_offset = memory_offset_of_min_vaddr_.value();
849 }
850
IpToVaddrInFile(uint64_t ip,uint64_t map_start,uint64_t)851 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override {
852 uint64_t min_vaddr;
853 uint64_t memory_offset;
854 GetMinExecutableVaddr(&min_vaddr, &memory_offset);
855 return ip - map_start - memory_offset + min_vaddr;
856 }
857
858 protected:
FindDebugFilePath() const859 std::string FindDebugFilePath() const override {
860 BuildId build_id = GetExpectedBuildId();
861 return debug_elf_file_finder_.FindDebugFile(path_, false, build_id);
862 }
863
LoadSymbolsImpl()864 std::vector<Symbol> LoadSymbolsImpl() override {
865 std::vector<Symbol> symbols;
866 BuildId build_id = GetExpectedBuildId();
867 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
868 // We only know how to map ip addrs to symbols in text section.
869 if (symbol.is_in_text_section && (symbol.is_label || symbol.is_func)) {
870 symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
871 }
872 };
873 ElfStatus status;
874 auto elf = ElfFile::Open(GetDebugFilePath(), &build_id, &status);
875 if (elf) {
876 status = elf->ParseSymbols(symbol_callback);
877 }
878 // Don't warn when a kernel module is missing. As a backup, we read symbols from /proc/kallsyms.
879 ReportReadElfSymbolResult(status, path_, GetDebugFilePath(), android::base::DEBUG);
880 SortAndFixSymbols(symbols);
881 return symbols;
882 }
883
884 private:
CalculateMinVaddr()885 void CalculateMinVaddr() {
886 min_vaddr_ = 0;
887 memory_offset_of_min_vaddr_ = 0;
888
889 // min_vaddr and memory_offset are used to convert an ip addr of a kernel module to its
890 // vaddr_in_file, as shown in IpToVaddrInFile(). When the kernel loads a kernel module, it
891 // puts ALLOC sections (like .plt, .text.ftrace_trampoline, .text) in memory in order. The
892 // text section may not be at the start of the module memory. To do address conversion, we
893 // need to know its relative position in the module memory. There are two ways:
894 // 1. Read the kernel module file to calculate the relative position of .text section. It
895 // is relatively complex and depends on both PLT entries and the kernel version.
896 // 2. Find a module symbol in .text section, get its address in memory from /proc/kallsyms,
897 // and its vaddr_in_file from the kernel module file. Then other symbols in .text section can
898 // be mapped in the same way. Below we use the second method.
899
900 if (!IsRegularFile(GetDebugFilePath())) {
901 return;
902 }
903
904 // 1. Select a module symbol in /proc/kallsyms.
905 kernel_dso_->LoadSymbols();
906 const auto& kernel_symbols = kernel_dso_->GetSymbols();
907 auto it = std::lower_bound(kernel_symbols.begin(), kernel_symbols.end(), memory_start_,
908 CompareSymbolToAddr);
909 const Symbol* kernel_symbol = nullptr;
910 while (it != kernel_symbols.end() && it->addr < memory_end_) {
911 if (strlen(it->Name()) > 0 && it->Name()[0] != '$') {
912 kernel_symbol = &*it;
913 break;
914 }
915 ++it;
916 }
917 if (kernel_symbol == nullptr) {
918 return;
919 }
920
921 // 2. Find the symbol in .ko file.
922 std::string symbol_name = kernel_symbol->Name();
923 if (auto pos = symbol_name.rfind(' '); pos != std::string::npos) {
924 symbol_name.resize(pos);
925 }
926 LoadSymbols();
927 for (const auto& symbol : symbols_) {
928 if (symbol_name == symbol.Name()) {
929 min_vaddr_ = symbol.addr;
930 memory_offset_of_min_vaddr_ = kernel_symbol->addr - memory_start_;
931 return;
932 }
933 }
934 }
935
936 uint64_t memory_start_;
937 uint64_t memory_end_;
938 Dso* kernel_dso_;
939 std::optional<uint64_t> min_vaddr_;
940 std::optional<uint64_t> memory_offset_of_min_vaddr_;
941 };
942
943 class SymbolMapFileDso : public Dso {
944 public:
SymbolMapFileDso(const std::string & path)945 SymbolMapFileDso(const std::string& path) : Dso(DSO_SYMBOL_MAP_FILE, path) {}
946
IpToVaddrInFile(uint64_t ip,uint64_t,uint64_t)947 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; }
948
949 protected:
LoadSymbolsImpl()950 std::vector<Symbol> LoadSymbolsImpl() override { return {}; }
951 };
952
953 class UnknownDso : public Dso {
954 public:
UnknownDso(const std::string & path)955 UnknownDso(const std::string& path) : Dso(DSO_UNKNOWN_FILE, path) {}
956
IpToVaddrInFile(uint64_t ip,uint64_t,uint64_t)957 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; }
958
959 protected:
LoadSymbolsImpl()960 std::vector<Symbol> LoadSymbolsImpl() override { return std::vector<Symbol>(); }
961 };
962
CreateDso(DsoType dso_type,const std::string & dso_path,bool force_64bit)963 std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path,
964 bool force_64bit) {
965 switch (dso_type) {
966 case DSO_ELF_FILE:
967 return std::unique_ptr<Dso>(new ElfDso(dso_path, force_64bit));
968 case DSO_KERNEL:
969 return std::unique_ptr<Dso>(new KernelDso(dso_path));
970 case DSO_DEX_FILE:
971 return std::unique_ptr<Dso>(new DexFileDso(dso_path));
972 case DSO_SYMBOL_MAP_FILE:
973 return std::unique_ptr<Dso>(new SymbolMapFileDso(dso_path));
974 case DSO_UNKNOWN_FILE:
975 return std::unique_ptr<Dso>(new UnknownDso(dso_path));
976 default:
977 LOG(ERROR) << "Unexpected dso_type " << static_cast<int>(dso_type);
978 return nullptr;
979 }
980 }
981
CreateDsoWithBuildId(DsoType dso_type,const std::string & dso_path,BuildId & build_id)982 std::unique_ptr<Dso> Dso::CreateDsoWithBuildId(DsoType dso_type, const std::string& dso_path,
983 BuildId& build_id) {
984 std::unique_ptr<Dso> dso;
985 switch (dso_type) {
986 case DSO_ELF_FILE:
987 dso.reset(new ElfDso(dso_path, false));
988 break;
989 case DSO_KERNEL:
990 dso.reset(new KernelDso(dso_path));
991 break;
992 case DSO_KERNEL_MODULE:
993 dso.reset(new KernelModuleDso(dso_path, 0, 0, nullptr));
994 break;
995 default:
996 LOG(ERROR) << "Unexpected dso_type " << static_cast<int>(dso_type);
997 return nullptr;
998 }
999 dso->debug_file_path_ = debug_elf_file_finder_.FindDebugFile(dso_path, false, build_id);
1000 return dso;
1001 }
1002
CreateKernelModuleDso(const std::string & dso_path,uint64_t memory_start,uint64_t memory_end,Dso * kernel_dso)1003 std::unique_ptr<Dso> Dso::CreateKernelModuleDso(const std::string& dso_path, uint64_t memory_start,
1004 uint64_t memory_end, Dso* kernel_dso) {
1005 return std::unique_ptr<Dso>(new KernelModuleDso(dso_path, memory_start, memory_end, kernel_dso));
1006 }
1007
DsoTypeToString(DsoType dso_type)1008 const char* DsoTypeToString(DsoType dso_type) {
1009 switch (dso_type) {
1010 case DSO_KERNEL:
1011 return "dso_kernel";
1012 case DSO_KERNEL_MODULE:
1013 return "dso_kernel_module";
1014 case DSO_ELF_FILE:
1015 return "dso_elf_file";
1016 case DSO_DEX_FILE:
1017 return "dso_dex_file";
1018 case DSO_SYMBOL_MAP_FILE:
1019 return "dso_symbol_map_file";
1020 default:
1021 return "unknown";
1022 }
1023 }
1024
GetBuildIdFromDsoPath(const std::string & dso_path,BuildId * build_id)1025 bool GetBuildIdFromDsoPath(const std::string& dso_path, BuildId* build_id) {
1026 ElfStatus status;
1027 auto elf = ElfFile::Open(dso_path, &status);
1028 if (status == ElfStatus::NO_ERROR && elf->GetBuildId(build_id) == ElfStatus::NO_ERROR) {
1029 return true;
1030 }
1031 return false;
1032 }
1033
GetBuildId(const Dso & dso,BuildId & build_id)1034 bool GetBuildId(const Dso& dso, BuildId& build_id) {
1035 if (dso.type() == DSO_KERNEL) {
1036 if (GetKernelBuildId(&build_id)) {
1037 return true;
1038 }
1039 } else if (dso.type() == DSO_KERNEL_MODULE) {
1040 bool has_build_id = false;
1041 if (android::base::EndsWith(dso.Path(), ".ko")) {
1042 return GetBuildIdFromDsoPath(dso.Path(), &build_id);
1043 }
1044 if (const std::string& path = dso.Path();
1045 path.size() > 2 && path[0] == '[' && path.back() == ']') {
1046 // For kernel modules that we can't find the corresponding file, read build id from /sysfs.
1047 return GetModuleBuildId(path.substr(1, path.size() - 2), &build_id);
1048 }
1049 } else if (dso.type() == DSO_ELF_FILE) {
1050 if (dso.Path() == DEFAULT_EXECNAME_FOR_THREAD_MMAP || dso.IsForJavaMethod()) {
1051 return false;
1052 }
1053 if (GetBuildIdFromDsoPath(dso.Path(), &build_id)) {
1054 return true;
1055 }
1056 }
1057 return false;
1058 }
1059
1060 } // namespace simpleperf
1061