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