• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Demangle(const std::string & name)271 std::string Dso::Demangle(const std::string& name) {
272   if (!demangle_) {
273     return name;
274   }
275   int status;
276   bool is_linker_symbol = (name.find(linker_prefix) == 0);
277   const char* mangled_str = name.c_str();
278   if (is_linker_symbol) {
279     mangled_str += linker_prefix.size();
280   }
281   std::string result = name;
282   char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
283   if (status == 0) {
284     if (is_linker_symbol) {
285       result = std::string("[linker]") + demangled_name;
286     } else {
287       result = demangled_name;
288     }
289     free(demangled_name);
290   } else if (is_linker_symbol) {
291     result = std::string("[linker]") + mangled_str;
292   }
293   return result;
294 }
295 
SetSymFsDir(const std::string & symfs_dir)296 bool Dso::SetSymFsDir(const std::string& symfs_dir) {
297   return debug_elf_file_finder_.SetSymFsDir(symfs_dir);
298 }
299 
AddSymbolDir(const std::string & symbol_dir)300 bool Dso::AddSymbolDir(const std::string& symbol_dir) {
301   return debug_elf_file_finder_.AddSymbolDir(symbol_dir);
302 }
303 
SetVmlinux(const std::string & vmlinux)304 void Dso::SetVmlinux(const std::string& vmlinux) {
305   vmlinux_ = vmlinux;
306 }
307 
SetBuildIds(const std::vector<std::pair<std::string,BuildId>> & build_ids)308 void Dso::SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids) {
309   std::unordered_map<std::string, BuildId> map;
310   for (auto& pair : build_ids) {
311     LOG(DEBUG) << "build_id_map: " << pair.first << ", " << pair.second.ToString();
312     map.insert(pair);
313   }
314   build_id_map_ = std::move(map);
315 }
316 
SetVdsoFile(const std::string & vdso_file,bool is_64bit)317 void Dso::SetVdsoFile(const std::string& vdso_file, bool is_64bit) {
318   debug_elf_file_finder_.SetVdsoFile(vdso_file, is_64bit);
319 }
320 
FindExpectedBuildIdForPath(const std::string & path)321 BuildId Dso::FindExpectedBuildIdForPath(const std::string& path) {
322   auto it = build_id_map_.find(path);
323   if (it != build_id_map_.end()) {
324     return it->second;
325   }
326   return BuildId();
327 }
328 
GetExpectedBuildId() const329 BuildId Dso::GetExpectedBuildId() const {
330   return FindExpectedBuildIdForPath(path_);
331 }
332 
Dso(DsoType type,const std::string & path)333 Dso::Dso(DsoType type, const std::string& path)
334     : type_(type),
335       path_(path),
336       is_loaded_(false),
337       dump_id_(UINT_MAX),
338       symbol_dump_id_(0),
339       symbol_warning_loglevel_(android::base::WARNING) {
340   size_t pos = path.find_last_of("/\\");
341   if (pos != std::string::npos) {
342     file_name_ = path.substr(pos + 1);
343   } else {
344     file_name_ = path;
345   }
346   dso_count_++;
347 }
348 
~Dso()349 Dso::~Dso() {
350   if (--dso_count_ == 0) {
351     // Clean up global variables when no longer used.
352     symbol_name_allocator.Clear();
353     demangle_ = true;
354     vmlinux_.clear();
355     kallsyms_.clear();
356     build_id_map_.clear();
357     g_dump_id_ = 0;
358     debug_elf_file_finder_.Reset();
359   }
360 }
361 
CreateDumpId()362 uint32_t Dso::CreateDumpId() {
363   CHECK(!HasDumpId());
364   return dump_id_ = g_dump_id_++;
365 }
366 
CreateSymbolDumpId(const Symbol * symbol)367 uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
368   CHECK(!symbol->HasDumpId());
369   symbol->dump_id_ = symbol_dump_id_++;
370   return symbol->dump_id_;
371 }
372 
IpToFileOffset(uint64_t ip,uint64_t map_start,uint64_t map_pgoff)373 std::optional<uint64_t> Dso::IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) {
374   return ip - map_start + map_pgoff;
375 }
376 
FindSymbol(uint64_t vaddr_in_dso)377 const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
378   if (!is_loaded_) {
379     LoadSymbols();
380   }
381   auto it = std::upper_bound(symbols_.begin(), symbols_.end(), vaddr_in_dso, CompareAddrToSymbol);
382   if (it != symbols_.begin()) {
383     --it;
384     if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
385       return &*it;
386     }
387   }
388   if (!unknown_symbols_.empty()) {
389     auto it = unknown_symbols_.find(vaddr_in_dso);
390     if (it != unknown_symbols_.end()) {
391       return &it->second;
392     }
393   }
394   return nullptr;
395 }
396 
SetSymbols(std::vector<Symbol> * symbols)397 void Dso::SetSymbols(std::vector<Symbol>* symbols) {
398   symbols_ = std::move(*symbols);
399   symbols->clear();
400 }
401 
AddUnknownSymbol(uint64_t vaddr_in_dso,const std::string & name)402 void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) {
403   unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1)));
404 }
405 
IsForJavaMethod() const406 bool Dso::IsForJavaMethod() const {
407   if (type_ == DSO_DEX_FILE) {
408     return true;
409   }
410   if (type_ == DSO_ELF_FILE) {
411     if (JITDebugReader::IsPathInJITSymFile(path_)) {
412       return true;
413     }
414     // JITDebugReader in old versions generates symfiles in 'TemporaryFile-XXXXXX'.
415     size_t pos = path_.rfind('/');
416     pos = (pos == std::string::npos) ? 0 : pos + 1;
417     return StartsWith(std::string_view(&path_[pos], path_.size() - pos), "TemporaryFile");
418   }
419   return false;
420 }
421 
LoadSymbols()422 void Dso::LoadSymbols() {
423   if (!is_loaded_) {
424     is_loaded_ = true;
425     std::vector<Symbol> symbols = LoadSymbolsImpl();
426     if (symbols_.empty()) {
427       symbols_ = std::move(symbols);
428     } else {
429       std::vector<Symbol> merged_symbols;
430       std::set_union(symbols_.begin(), symbols_.end(), symbols.begin(), symbols.end(),
431                      std::back_inserter(merged_symbols), Symbol::CompareValueByAddr);
432       symbols_ = std::move(merged_symbols);
433     }
434   }
435 }
436 
ReportReadElfSymbolResult(ElfStatus result,const std::string & path,const std::string & debug_file_path,android::base::LogSeverity warning_loglevel=android::base::WARNING)437 static void ReportReadElfSymbolResult(
438     ElfStatus result, const std::string& path, const std::string& debug_file_path,
439     android::base::LogSeverity warning_loglevel = android::base::WARNING) {
440   if (result == ElfStatus::NO_ERROR) {
441     LOG(VERBOSE) << "Read symbols from " << debug_file_path << " successfully";
442   } else if (result == ElfStatus::NO_SYMBOL_TABLE) {
443     if (path == "[vdso]") {
444       // Vdso only contains dynamic symbol table, and we can't change that.
445       return;
446     }
447     // Lacking symbol table isn't considered as an error but worth reporting.
448     LOG(warning_loglevel) << debug_file_path << " doesn't contain symbol table";
449   } else {
450     LOG(warning_loglevel) << "failed to read symbols from " << debug_file_path << ": " << result;
451   }
452 }
453 
SortAndFixSymbols(std::vector<Symbol> & symbols)454 static void SortAndFixSymbols(std::vector<Symbol>& symbols) {
455   std::sort(symbols.begin(), symbols.end(), Symbol::CompareValueByAddr);
456   Symbol* prev_symbol = nullptr;
457   for (auto& symbol : symbols) {
458     if (prev_symbol != nullptr && prev_symbol->len == 0) {
459       prev_symbol->len = symbol.addr - prev_symbol->addr;
460     }
461     prev_symbol = &symbol;
462   }
463 }
464 
465 class DexFileDso : public Dso {
466  public:
DexFileDso(const std::string & path)467   DexFileDso(const std::string& path) : Dso(DSO_DEX_FILE, path) {}
468 
AddDexFileOffset(uint64_t dex_file_offset)469   void AddDexFileOffset(uint64_t dex_file_offset) override {
470     auto it = std::lower_bound(dex_file_offsets_.begin(), dex_file_offsets_.end(), dex_file_offset);
471     if (it != dex_file_offsets_.end() && *it == dex_file_offset) {
472       return;
473     }
474     dex_file_offsets_.insert(it, dex_file_offset);
475   }
476 
DexFileOffsets()477   const std::vector<uint64_t>* DexFileOffsets() override { return &dex_file_offsets_; }
478 
IpToVaddrInFile(uint64_t ip,uint64_t map_start,uint64_t map_pgoff)479   uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override {
480     return ip - map_start + map_pgoff;
481   }
482 
LoadSymbolsImpl()483   std::vector<Symbol> LoadSymbolsImpl() override {
484     std::vector<Symbol> symbols;
485     const std::string& debug_file_path = GetDebugFilePath();
486     auto tuple = SplitUrlInApk(debug_file_path);
487     // Symbols of dex files are collected on device. If the dex file doesn't exist, probably
488     // we are reporting on host, and there is no need to report warning of missing dex files.
489     if (!IsRegularFile(std::get<0>(tuple) ? std::get<1>(tuple) : debug_file_path)) {
490       LOG(DEBUG) << "skip reading symbols from non-exist dex_file " << debug_file_path;
491       return symbols;
492     }
493     bool status = false;
494     auto symbol_callback = [&](DexFileSymbol* symbol) {
495       symbols.emplace_back(symbol->name, symbol->addr, symbol->size);
496     };
497     if (std::get<0>(tuple)) {
498       std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(std::get<1>(tuple));
499       ZipEntry entry;
500       std::vector<uint8_t> data;
501       if (ahelper && ahelper->FindEntry(std::get<2>(tuple), &entry) &&
502           ahelper->GetEntryData(entry, &data)) {
503         status = ReadSymbolsFromDexFileInMemory(data.data(), data.size(), debug_file_path,
504                                                 dex_file_offsets_, symbol_callback);
505       }
506     } else {
507       status = ReadSymbolsFromDexFile(debug_file_path, dex_file_offsets_, symbol_callback);
508     }
509     if (!status) {
510       android::base::LogSeverity level =
511           symbols_.empty() ? android::base::WARNING : android::base::DEBUG;
512       LOG(level) << "Failed to read symbols from dex_file " << debug_file_path;
513       return symbols;
514     }
515     LOG(VERBOSE) << "Read symbols from dex_file " << debug_file_path << " successfully";
516     SortAndFixSymbols(symbols);
517     return symbols;
518   }
519 
520  private:
521   std::vector<uint64_t> dex_file_offsets_;
522 };
523 
524 class ElfDso : public Dso {
525  public:
ElfDso(const std::string & path,bool force_64bit)526   ElfDso(const std::string& path, bool force_64bit)
527       : Dso(DSO_ELF_FILE, path), force_64bit_(force_64bit) {}
528 
GetReportPath() const529   std::string_view GetReportPath() const override {
530     if (JITDebugReader::IsPathInJITSymFile(path_)) {
531       if (path_.find(kJITAppCacheFile) != path_.npos) {
532         return "[JIT app cache]";
533       }
534       return "[JIT zygote cache]";
535     }
536     return path_;
537   }
538 
SetMinExecutableVaddr(uint64_t min_vaddr,uint64_t file_offset)539   void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t file_offset) override {
540     min_vaddr_ = min_vaddr;
541     file_offset_of_min_vaddr_ = file_offset;
542   }
543 
GetMinExecutableVaddr(uint64_t * min_vaddr,uint64_t * file_offset)544   void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* file_offset) override {
545     if (type_ == DSO_DEX_FILE) {
546       return dex_file_dso_->GetMinExecutableVaddr(min_vaddr, file_offset);
547     }
548     if (min_vaddr_ == uninitialized_value) {
549       min_vaddr_ = 0;
550       BuildId build_id = GetExpectedBuildId();
551 
552       ElfStatus status;
553       auto elf = ElfFile::Open(GetDebugFilePath(), &build_id, &status);
554       if (elf) {
555         min_vaddr_ = elf->ReadMinExecutableVaddr(&file_offset_of_min_vaddr_);
556       } else {
557         LOG(WARNING) << "failed to read min virtual address of " << GetDebugFilePath() << ": "
558                      << status;
559       }
560     }
561     *min_vaddr = min_vaddr_;
562     *file_offset = file_offset_of_min_vaddr_;
563   }
564 
IpToVaddrInFile(uint64_t ip,uint64_t map_start,uint64_t map_pgoff)565   uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override {
566     if (type_ == DSO_DEX_FILE) {
567       return dex_file_dso_->IpToVaddrInFile(ip, map_start, map_pgoff);
568     }
569     uint64_t min_vaddr;
570     uint64_t file_offset_of_min_vaddr;
571     GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
572     if (file_offset_of_min_vaddr == uninitialized_value) {
573       return ip - map_start + min_vaddr;
574     }
575     // Apps may make part of the executable segment of a shared library writeable, which can
576     // generate multiple executable segments at runtime. So use map_pgoff to calculate
577     // vaddr_in_file.
578     return ip - map_start + map_pgoff - file_offset_of_min_vaddr + min_vaddr;
579   }
580 
AddDexFileOffset(uint64_t dex_file_offset)581   void AddDexFileOffset(uint64_t dex_file_offset) override {
582     if (type_ == DSO_ELF_FILE) {
583       // When simpleperf does unwinding while recording, it processes mmap records before reading
584       // dex file linked list (via JITDebugReader). To process mmap records, it creates Dso
585       // objects of type ELF_FILE. Then after reading dex file linked list, it realizes some
586       // ELF_FILE Dso objects should actually be DEX_FILE, because they have dex file offsets.
587       // So here converts ELF_FILE Dso into DEX_FILE Dso.
588       type_ = DSO_DEX_FILE;
589       dex_file_dso_.reset(new DexFileDso(path_));
590     }
591     dex_file_dso_->AddDexFileOffset(dex_file_offset);
592   }
593 
DexFileOffsets()594   const std::vector<uint64_t>* DexFileOffsets() override {
595     return dex_file_dso_ ? dex_file_dso_->DexFileOffsets() : nullptr;
596   }
597 
598  protected:
FindDebugFilePath() const599   std::string FindDebugFilePath() const override {
600     BuildId build_id = GetExpectedBuildId();
601     return debug_elf_file_finder_.FindDebugFile(path_, force_64bit_, build_id);
602   }
603 
LoadSymbolsImpl()604   std::vector<Symbol> LoadSymbolsImpl() override {
605     if (dex_file_dso_) {
606       return dex_file_dso_->LoadSymbolsImpl();
607     }
608     std::vector<Symbol> symbols;
609     BuildId build_id = GetExpectedBuildId();
610     auto symbol_callback = [&](const ElfFileSymbol& symbol) {
611       if (symbol.is_func || (symbol.is_label && symbol.is_in_text_section)) {
612         symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
613       }
614     };
615     ElfStatus status;
616     auto elf = ElfFile::Open(GetDebugFilePath(), &build_id, &status);
617     if (elf) {
618       status = elf->ParseSymbols(symbol_callback);
619     }
620     ReportReadElfSymbolResult(status, path_, GetDebugFilePath(),
621                               symbols_.empty() ? android::base::WARNING : android::base::DEBUG);
622     SortAndFixSymbols(symbols);
623     return symbols;
624   }
625 
626  private:
627   static constexpr uint64_t uninitialized_value = std::numeric_limits<uint64_t>::max();
628 
629   bool force_64bit_;
630   uint64_t min_vaddr_ = uninitialized_value;
631   uint64_t file_offset_of_min_vaddr_ = uninitialized_value;
632   std::unique_ptr<DexFileDso> dex_file_dso_;
633 };
634 
635 class KernelDso : public Dso {
636  public:
KernelDso(const std::string & path)637   KernelDso(const std::string& path) : Dso(DSO_KERNEL, path) {
638     debug_file_path_ = FindDebugFilePath();
639     if (!vmlinux_.empty()) {
640       // Use vmlinux as the kernel debug file.
641       BuildId build_id = GetExpectedBuildId();
642       ElfStatus status;
643       if (ElfFile::Open(vmlinux_, &build_id, &status)) {
644         debug_file_path_ = vmlinux_;
645         has_debug_file_ = true;
646       }
647     } else if (IsRegularFile(GetDebugFilePath())) {
648       has_debug_file_ = true;
649     }
650   }
651 
652   // IpToVaddrInFile() and LoadSymbols() must be consistent in fixing addresses changed by kernel
653   // address space layout randomization.
IpToVaddrInFile(uint64_t ip,uint64_t map_start,uint64_t)654   uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override {
655     if (map_start != 0 && GetKernelStartAddr() != 0) {
656       // Fix kernel addresses changed by kernel address randomization.
657       fix_kernel_address_randomization_ = true;
658       return ip - map_start + GetKernelStartAddr();
659     }
660     return ip;
661   }
662 
IpToFileOffset(uint64_t ip,uint64_t map_start,uint64_t)663   std::optional<uint64_t> IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t) override {
664     if (map_start != 0 && GetKernelStartOffset() != 0) {
665       return ip - map_start + GetKernelStartOffset();
666     }
667     return std::nullopt;
668   }
669 
670  protected:
FindDebugFilePath() const671   std::string FindDebugFilePath() const override {
672     BuildId build_id = GetExpectedBuildId();
673     return debug_elf_file_finder_.FindDebugFile(path_, false, build_id);
674   }
675 
LoadSymbolsImpl()676   std::vector<Symbol> LoadSymbolsImpl() override {
677     std::vector<Symbol> symbols;
678     if (has_debug_file_) {
679       ReadSymbolsFromDebugFile(&symbols);
680     }
681 
682     if (symbols.empty() && !kallsyms_.empty()) {
683       ReadSymbolsFromKallsyms(kallsyms_, &symbols);
684     }
685 #if defined(__linux__)
686     if (symbols.empty()) {
687       ReadSymbolsFromProc(&symbols);
688     }
689 #endif  // defined(__linux__)
690     SortAndFixSymbols(symbols);
691     if (!symbols.empty()) {
692       symbols.back().len = std::numeric_limits<uint64_t>::max() - symbols.back().addr;
693     }
694     return symbols;
695   }
696 
697  private:
ReadSymbolsFromDebugFile(std::vector<Symbol> * symbols)698   void ReadSymbolsFromDebugFile(std::vector<Symbol>* symbols) {
699     if (!fix_kernel_address_randomization_) {
700       LOG(WARNING) << "Don't know how to fix addresses changed by kernel address randomization. So "
701                       "symbols in "
702                    << GetDebugFilePath() << " are not used";
703       return;
704     }
705     // symbols_ are kernel symbols got from /proc/kallsyms while recording. Those symbols are
706     // not fixed for kernel address randomization. So clear them to avoid mixing them with
707     // symbols in debug_file_path.
708     symbols_.clear();
709 
710     auto symbol_callback = [&](const ElfFileSymbol& symbol) {
711       if (symbol.is_func) {
712         symbols->emplace_back(symbol.name, symbol.vaddr, symbol.len);
713       }
714     };
715     ElfStatus status;
716     if (auto elf = ElfFile::Open(GetDebugFilePath(), &status); elf) {
717       status = elf->ParseSymbols(symbol_callback);
718     }
719     ReportReadElfSymbolResult(status, path_, GetDebugFilePath());
720   }
721 
ReadSymbolsFromKallsyms(std::string & kallsyms,std::vector<Symbol> * symbols)722   void ReadSymbolsFromKallsyms(std::string& kallsyms, std::vector<Symbol>* symbols) {
723     auto symbol_callback = [&](const KernelSymbol& symbol) {
724       if (strchr("TtWw", symbol.type) && symbol.addr != 0u) {
725         if (symbol.module == nullptr) {
726           symbols->emplace_back(symbol.name, symbol.addr, 0);
727         } else {
728           std::string name = std::string(symbol.name) + " [" + symbol.module + "]";
729           symbols->emplace_back(name, symbol.addr, 0);
730         }
731       }
732       return false;
733     };
734     ProcessKernelSymbols(kallsyms, symbol_callback);
735     if (symbols->empty()) {
736       LOG(WARNING) << "Symbol addresses in /proc/kallsyms on device are all zero. "
737                       "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
738     }
739   }
740 
741 #if defined(__linux__)
ReadSymbolsFromProc(std::vector<Symbol> * symbols)742   void ReadSymbolsFromProc(std::vector<Symbol>* symbols) {
743     BuildId build_id = GetExpectedBuildId();
744     if (!build_id.IsEmpty()) {
745       // Try /proc/kallsyms only when asked to do so, or when build id matches.
746       // Otherwise, it is likely to use /proc/kallsyms on host for perf.data recorded on device.
747       bool can_read_kallsyms = true;
748       if (!build_id.IsEmpty()) {
749         BuildId real_build_id;
750         if (!GetKernelBuildId(&real_build_id) || build_id != real_build_id) {
751           LOG(DEBUG) << "failed to read symbols from /proc/kallsyms: Build id mismatch";
752           can_read_kallsyms = false;
753         }
754       }
755       if (can_read_kallsyms) {
756         std::string kallsyms;
757         if (LoadKernelSymbols(&kallsyms)) {
758           ReadSymbolsFromKallsyms(kallsyms, symbols);
759         }
760       }
761     }
762   }
763 #endif  // defined(__linux__)
764 
GetKernelStartAddr()765   uint64_t GetKernelStartAddr() {
766     if (!kernel_start_addr_) {
767       ParseKernelStartAddr();
768     }
769     return kernel_start_addr_.value();
770   }
771 
GetKernelStartOffset()772   uint64_t GetKernelStartOffset() {
773     if (!kernel_start_file_offset_) {
774       ParseKernelStartAddr();
775     }
776     return kernel_start_file_offset_.value();
777   }
778 
ParseKernelStartAddr()779   void ParseKernelStartAddr() {
780     kernel_start_addr_ = 0;
781     kernel_start_file_offset_ = 0;
782     if (has_debug_file_) {
783       ElfStatus status;
784       if (auto elf = ElfFile::Open(GetDebugFilePath(), &status); elf) {
785         for (const auto& section : elf->GetSectionHeader()) {
786           if (section.name == ".text") {
787             kernel_start_addr_ = section.vaddr;
788             kernel_start_file_offset_ = section.file_offset;
789             break;
790           }
791         }
792       }
793     }
794   }
795 
796   bool has_debug_file_ = false;
797   bool fix_kernel_address_randomization_ = false;
798   std::optional<uint64_t> kernel_start_addr_;
799   std::optional<uint64_t> kernel_start_file_offset_;
800 };
801 
802 class KernelModuleDso : public Dso {
803  public:
KernelModuleDso(const std::string & path,uint64_t memory_start,uint64_t memory_end,Dso * kernel_dso)804   KernelModuleDso(const std::string& path, uint64_t memory_start, uint64_t memory_end,
805                   Dso* kernel_dso)
806       : Dso(DSO_KERNEL_MODULE, path),
807         memory_start_(memory_start),
808         memory_end_(memory_end),
809         kernel_dso_(kernel_dso) {}
810 
SetMinExecutableVaddr(uint64_t min_vaddr,uint64_t memory_offset)811   void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t memory_offset) override {
812     min_vaddr_ = min_vaddr;
813     memory_offset_of_min_vaddr_ = memory_offset;
814   }
815 
GetMinExecutableVaddr(uint64_t * min_vaddr,uint64_t * memory_offset)816   void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* memory_offset) override {
817     if (!min_vaddr_) {
818       CalculateMinVaddr();
819     }
820     *min_vaddr = min_vaddr_.value();
821     *memory_offset = memory_offset_of_min_vaddr_.value();
822   }
823 
IpToVaddrInFile(uint64_t ip,uint64_t map_start,uint64_t)824   uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override {
825     uint64_t min_vaddr;
826     uint64_t memory_offset;
827     GetMinExecutableVaddr(&min_vaddr, &memory_offset);
828     return ip - map_start - memory_offset + min_vaddr;
829   }
830 
831  protected:
FindDebugFilePath() const832   std::string FindDebugFilePath() const override {
833     BuildId build_id = GetExpectedBuildId();
834     return debug_elf_file_finder_.FindDebugFile(path_, false, build_id);
835   }
836 
LoadSymbolsImpl()837   std::vector<Symbol> LoadSymbolsImpl() override {
838     std::vector<Symbol> symbols;
839     BuildId build_id = GetExpectedBuildId();
840     auto symbol_callback = [&](const ElfFileSymbol& symbol) {
841       // We only know how to map ip addrs to symbols in text section.
842       if (symbol.is_in_text_section && (symbol.is_label || symbol.is_func)) {
843         symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
844       }
845     };
846     ElfStatus status;
847     auto elf = ElfFile::Open(GetDebugFilePath(), &build_id, &status);
848     if (elf) {
849       status = elf->ParseSymbols(symbol_callback);
850     }
851     ReportReadElfSymbolResult(status, path_, GetDebugFilePath(),
852                               symbols_.empty() ? android::base::WARNING : android::base::DEBUG);
853     SortAndFixSymbols(symbols);
854     return symbols;
855   }
856 
857  private:
CalculateMinVaddr()858   void CalculateMinVaddr() {
859     min_vaddr_ = 0;
860     memory_offset_of_min_vaddr_ = 0;
861 
862     // min_vaddr and memory_offset are used to convert an ip addr of a kernel module to its
863     // vaddr_in_file, as shown in IpToVaddrInFile(). When the kernel loads a kernel module, it
864     // puts ALLOC sections (like .plt, .text.ftrace_trampoline, .text) in memory in order. The
865     // text section may not be at the start of the module memory. To do address conversion, we
866     // need to know its relative position in the module memory. There are two ways:
867     // 1. Read the kernel module file to calculate the relative position of .text section. It
868     // is relatively complex and depends on both PLT entries and the kernel version.
869     // 2. Find a module symbol in .text section, get its address in memory from /proc/kallsyms, and
870     // its vaddr_in_file from the kernel module file. Then other symbols in .text section can be
871     // mapped in the same way.
872     // Below we use the second method.
873 
874     // 1. Select a module symbol in /proc/kallsyms.
875     kernel_dso_->LoadSymbols();
876     const auto& kernel_symbols = kernel_dso_->GetSymbols();
877     auto it = std::lower_bound(kernel_symbols.begin(), kernel_symbols.end(), memory_start_,
878                                CompareSymbolToAddr);
879     const Symbol* kernel_symbol = nullptr;
880     while (it != kernel_symbols.end() && it->addr < memory_end_) {
881       if (strlen(it->Name()) > 0 && it->Name()[0] != '$') {
882         kernel_symbol = &*it;
883         break;
884       }
885       ++it;
886     }
887     if (kernel_symbol == nullptr) {
888       return;
889     }
890 
891     // 2. Find the symbol in .ko file.
892     std::string symbol_name = kernel_symbol->Name();
893     if (auto pos = symbol_name.rfind(' '); pos != std::string::npos) {
894       symbol_name.resize(pos);
895     }
896     LoadSymbols();
897     for (const auto& symbol : symbols_) {
898       if (symbol_name == symbol.Name()) {
899         min_vaddr_ = symbol.addr;
900         memory_offset_of_min_vaddr_ = kernel_symbol->addr - memory_start_;
901         return;
902       }
903     }
904   }
905 
906   uint64_t memory_start_;
907   uint64_t memory_end_;
908   Dso* kernel_dso_;
909   std::optional<uint64_t> min_vaddr_;
910   std::optional<uint64_t> memory_offset_of_min_vaddr_;
911 };
912 
913 class SymbolMapFileDso : public Dso {
914  public:
SymbolMapFileDso(const std::string & path)915   SymbolMapFileDso(const std::string& path) : Dso(DSO_SYMBOL_MAP_FILE, path) {}
916 
IpToVaddrInFile(uint64_t ip,uint64_t,uint64_t)917   uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; }
918 
919  protected:
LoadSymbolsImpl()920   std::vector<Symbol> LoadSymbolsImpl() override { return {}; }
921 };
922 
923 class UnknownDso : public Dso {
924  public:
UnknownDso(const std::string & path)925   UnknownDso(const std::string& path) : Dso(DSO_UNKNOWN_FILE, path) {}
926 
IpToVaddrInFile(uint64_t ip,uint64_t,uint64_t)927   uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; }
928 
929  protected:
LoadSymbolsImpl()930   std::vector<Symbol> LoadSymbolsImpl() override { return std::vector<Symbol>(); }
931 };
932 
CreateDso(DsoType dso_type,const std::string & dso_path,bool force_64bit)933 std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path,
934                                     bool force_64bit) {
935   switch (dso_type) {
936     case DSO_ELF_FILE:
937       return std::unique_ptr<Dso>(new ElfDso(dso_path, force_64bit));
938     case DSO_KERNEL:
939       return std::unique_ptr<Dso>(new KernelDso(dso_path));
940     case DSO_DEX_FILE:
941       return std::unique_ptr<Dso>(new DexFileDso(dso_path));
942     case DSO_SYMBOL_MAP_FILE:
943       return std::unique_ptr<Dso>(new SymbolMapFileDso(dso_path));
944     case DSO_UNKNOWN_FILE:
945       return std::unique_ptr<Dso>(new UnknownDso(dso_path));
946     default:
947       LOG(FATAL) << "Unexpected dso_type " << static_cast<int>(dso_type);
948   }
949   return nullptr;
950 }
951 
CreateDsoWithBuildId(DsoType dso_type,const std::string & dso_path,BuildId & build_id)952 std::unique_ptr<Dso> Dso::CreateDsoWithBuildId(DsoType dso_type, const std::string& dso_path,
953                                                BuildId& build_id) {
954   std::unique_ptr<Dso> dso;
955   switch (dso_type) {
956     case DSO_ELF_FILE:
957       dso.reset(new ElfDso(dso_path, false));
958       break;
959     case DSO_KERNEL:
960       dso.reset(new KernelDso(dso_path));
961       break;
962     case DSO_KERNEL_MODULE:
963       dso.reset(new KernelModuleDso(dso_path, 0, 0, nullptr));
964       break;
965     default:
966       LOG(FATAL) << "Unexpected dso_type " << static_cast<int>(dso_type);
967       return nullptr;
968   }
969   dso->debug_file_path_ = debug_elf_file_finder_.FindDebugFile(dso_path, false, build_id);
970   return dso;
971 }
972 
CreateKernelModuleDso(const std::string & dso_path,uint64_t memory_start,uint64_t memory_end,Dso * kernel_dso)973 std::unique_ptr<Dso> Dso::CreateKernelModuleDso(const std::string& dso_path, uint64_t memory_start,
974                                                 uint64_t memory_end, Dso* kernel_dso) {
975   return std::unique_ptr<Dso>(new KernelModuleDso(dso_path, memory_start, memory_end, kernel_dso));
976 }
977 
DsoTypeToString(DsoType dso_type)978 const char* DsoTypeToString(DsoType dso_type) {
979   switch (dso_type) {
980     case DSO_KERNEL:
981       return "dso_kernel";
982     case DSO_KERNEL_MODULE:
983       return "dso_kernel_module";
984     case DSO_ELF_FILE:
985       return "dso_elf_file";
986     case DSO_DEX_FILE:
987       return "dso_dex_file";
988     case DSO_SYMBOL_MAP_FILE:
989       return "dso_symbol_map_file";
990     default:
991       return "unknown";
992   }
993 }
994 
GetBuildIdFromDsoPath(const std::string & dso_path,BuildId * build_id)995 bool GetBuildIdFromDsoPath(const std::string& dso_path, BuildId* build_id) {
996   ElfStatus status;
997   auto elf = ElfFile::Open(dso_path, &status);
998   if (status == ElfStatus::NO_ERROR && elf->GetBuildId(build_id) == ElfStatus::NO_ERROR) {
999     return true;
1000   }
1001   return false;
1002 }
1003 
1004 }  // namespace simpleperf
1005