OSDN Git Service

Remove verify_boot_signature.
[android-x86/system-extras.git] / simpleperf / dso.cpp
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 <vector>
25
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
28
29 #include "environment.h"
30 #include "read_apk.h"
31 #include "read_elf.h"
32 #include "utils.h"
33
34 static OneTimeFreeAllocator symbol_name_allocator;
35
36 Symbol::Symbol(const std::string& name, uint64_t addr, uint64_t len)
37     : addr(addr),
38       len(len),
39       name_(symbol_name_allocator.AllocateString(name)),
40       demangled_name_(nullptr),
41       dump_id_(UINT_MAX) {}
42
43 const char* Symbol::DemangledName() const {
44   if (demangled_name_ == nullptr) {
45     const std::string s = Dso::Demangle(name_);
46     if (s == name_) {
47       demangled_name_ = name_;
48     } else {
49       demangled_name_ = symbol_name_allocator.AllocateString(s);
50     }
51   }
52   return demangled_name_;
53 }
54
55 bool Dso::demangle_ = true;
56 std::string Dso::symfs_dir_;
57 std::string Dso::vmlinux_;
58 std::string Dso::kallsyms_;
59 bool Dso::read_kernel_symbols_from_proc_;
60 std::unordered_map<std::string, BuildId> Dso::build_id_map_;
61 size_t Dso::dso_count_;
62 uint32_t Dso::g_dump_id_;
63 std::unique_ptr<TemporaryFile> Dso::vdso_64bit_;
64 std::unique_ptr<TemporaryFile> Dso::vdso_32bit_;
65
66 void Dso::SetDemangle(bool demangle) { demangle_ = demangle; }
67
68 extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n,
69                                 int* status);
70
71 std::string Dso::Demangle(const std::string& name) {
72   if (!demangle_) {
73     return name;
74   }
75   int status;
76   bool is_linker_symbol = (name.find(linker_prefix) == 0);
77   const char* mangled_str = name.c_str();
78   if (is_linker_symbol) {
79     mangled_str += linker_prefix.size();
80   }
81   std::string result = name;
82   char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
83   if (status == 0) {
84     if (is_linker_symbol) {
85       result = std::string("[linker]") + demangled_name;
86     } else {
87       result = demangled_name;
88     }
89     free(demangled_name);
90   } else if (is_linker_symbol) {
91     result = std::string("[linker]") + mangled_str;
92   }
93   return result;
94 }
95
96 bool Dso::SetSymFsDir(const std::string& symfs_dir) {
97   std::string dirname = symfs_dir;
98   if (!dirname.empty()) {
99     if (dirname.back() != '/') {
100       dirname.push_back('/');
101     }
102     if (!IsDir(symfs_dir)) {
103       LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'";
104       return false;
105     }
106   }
107   symfs_dir_ = dirname;
108   return true;
109 }
110
111 void Dso::SetVmlinux(const std::string& vmlinux) { vmlinux_ = vmlinux; }
112
113 void Dso::SetBuildIds(
114     const std::vector<std::pair<std::string, BuildId>>& build_ids) {
115   std::unordered_map<std::string, BuildId> map;
116   for (auto& pair : build_ids) {
117     LOG(DEBUG) << "build_id_map: " << pair.first << ", "
118                << pair.second.ToString();
119     map.insert(pair);
120   }
121   build_id_map_ = std::move(map);
122 }
123
124 void Dso::SetVdsoFile(std::unique_ptr<TemporaryFile> vdso_file, bool is_64bit) {
125   if (is_64bit) {
126     vdso_64bit_ = std::move(vdso_file);
127   } else {
128     vdso_32bit_ = std::move(vdso_file);
129   }
130 }
131
132 BuildId Dso::FindExpectedBuildIdForPath(const std::string& path) {
133   auto it = build_id_map_.find(path);
134   if (it != build_id_map_.end()) {
135     return it->second;
136   }
137   return BuildId();
138 }
139
140 BuildId Dso::GetExpectedBuildId() {
141   return FindExpectedBuildIdForPath(path_);
142 }
143
144 std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path,
145                                     bool force_64bit) {
146   return std::unique_ptr<Dso>(new Dso(dso_type, dso_path, force_64bit));
147 }
148
149 Dso::Dso(DsoType type, const std::string& path, bool force_64bit)
150     : type_(type),
151       path_(path),
152       debug_file_path_(path),
153       min_vaddr_(std::numeric_limits<uint64_t>::max()),
154       is_loaded_(false),
155       dump_id_(UINT_MAX),
156       symbol_dump_id_(0),
157       symbol_warning_loglevel_(android::base::WARNING) {
158   if (type_ == DSO_KERNEL) {
159     min_vaddr_ = 0;
160   }
161   // Check if file matching path_ exists in symfs directory before using it as
162   // debug_file_path_.
163   if (!symfs_dir_.empty()) {
164     std::string path_in_symfs = symfs_dir_ + path_;
165     std::tuple<bool, std::string, std::string> tuple =
166         SplitUrlInApk(path_in_symfs);
167     std::string file_path =
168         std::get<0>(tuple) ? std::get<1>(tuple) : path_in_symfs;
169     if (IsRegularFile(file_path)) {
170       debug_file_path_ = path_in_symfs;
171     }
172   } else if (path == "[vdso]") {
173     if (force_64bit && vdso_64bit_ != nullptr) {
174       debug_file_path_ = vdso_64bit_->path;
175     } else if (!force_64bit && vdso_32bit_ != nullptr) {
176       debug_file_path_ = vdso_32bit_->path;
177     }
178   }
179   size_t pos = path.find_last_of("/\\");
180   if (pos != std::string::npos) {
181     file_name_ = path.substr(pos + 1);
182   } else {
183     file_name_ = path;
184   }
185   dso_count_++;
186 }
187
188 Dso::~Dso() {
189   if (--dso_count_ == 0) {
190     // Clean up global variables when no longer used.
191     symbol_name_allocator.Clear();
192     demangle_ = true;
193     symfs_dir_.clear();
194     vmlinux_.clear();
195     kallsyms_.clear();
196     read_kernel_symbols_from_proc_ = false;
197     build_id_map_.clear();
198     g_dump_id_ = 0;
199     vdso_64bit_ = nullptr;
200     vdso_32bit_ = nullptr;
201   }
202 }
203
204 uint32_t Dso::CreateDumpId() {
205   CHECK(!HasDumpId());
206   return dump_id_ = g_dump_id_++;
207 }
208
209 uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
210   CHECK(!symbol->HasDumpId());
211   symbol->dump_id_ = symbol_dump_id_++;
212   return symbol->dump_id_;
213 }
214
215 const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
216   if (!is_loaded_) {
217     Load();
218   }
219   if (!symbols_.empty()) {
220     auto it = std::upper_bound(symbols_.begin(), symbols_.end(),
221                                Symbol("", vaddr_in_dso, 0),
222                                Symbol::CompareValueByAddr);
223     if (it != symbols_.begin()) {
224       --it;
225       if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
226         return &*it;
227       }
228     }
229   }
230   if (!unknown_symbols_.empty()) {
231     auto it = unknown_symbols_.find(vaddr_in_dso);
232     if (it != unknown_symbols_.end()) {
233       return &it->second;
234     }
235   }
236   return nullptr;
237 }
238
239 const std::vector<Symbol>& Dso::GetSymbols() {
240   if (!is_loaded_) {
241     Load();
242   }
243   return symbols_;
244 }
245
246 void Dso::SetSymbols(std::vector<Symbol>* symbols) {
247   symbols_ = std::move(*symbols);
248   symbols->clear();
249 }
250
251 void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) {
252   unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1)));
253 }
254
255 uint64_t Dso::MinVirtualAddress() {
256   if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
257     min_vaddr_ = 0;
258     if (type_ == DSO_ELF_FILE) {
259       BuildId build_id = GetExpectedBuildId();
260
261       uint64_t addr;
262       ElfStatus result = ReadMinExecutableVirtualAddressFromElfFile(
263           GetDebugFilePath(), build_id, &addr);
264       if (result != ElfStatus::NO_ERROR) {
265         LOG(WARNING) << "failed to read min virtual address of "
266                      << GetDebugFilePath() << ": " << result;
267       } else {
268         min_vaddr_ = addr;
269       }
270     }
271   }
272   return min_vaddr_;
273 }
274
275 static std::vector<Symbol> MergeSortedSymbols(const std::vector<Symbol>& s1,
276                                               const std::vector<Symbol>& s2) {
277   std::vector<Symbol> result;
278   std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(result),
279                  Symbol::CompareValueByAddr);
280   return result;
281 }
282
283 void Dso::Load() {
284   is_loaded_ = true;
285   std::vector<Symbol> dumped_symbols;
286   if (!symbols_.empty()) {
287     // If symbols has been read from file feature section of perf.data, move it to
288     // dumped_symbols,  so later we can merge them with symbols read from file system.
289     dumped_symbols = std::move(symbols_);
290     symbols_.clear();
291     // Don't warn missing symbol table if we have dumped symbols in perf.data.
292     symbol_warning_loglevel_ = android::base::DEBUG;
293   }
294   bool result = false;
295   switch (type_) {
296     case DSO_KERNEL:
297       result = LoadKernel();
298       break;
299     case DSO_KERNEL_MODULE:
300       result = LoadKernelModule();
301       break;
302     case DSO_ELF_FILE: {
303       if (std::get<0>(SplitUrlInApk(path_))) {
304         result = LoadEmbeddedElfFile();
305       } else {
306         result = LoadElfFile();
307       }
308       break;
309     }
310   }
311   if (result) {
312     std::sort(symbols_.begin(), symbols_.end(), Symbol::CompareValueByAddr);
313     FixupSymbolLength();
314   } else {
315     symbols_.clear();
316   }
317
318   if (symbols_.empty()) {
319     symbols_ = std::move(dumped_symbols);
320   } else if (!dumped_symbols.empty()) {
321     symbols_ = MergeSortedSymbols(symbols_, dumped_symbols);
322   }
323
324   if (symbols_.empty()) {
325     LOG(DEBUG) << "failed to load dso: " << path_;
326   }
327 }
328
329 static bool IsKernelFunctionSymbol(const KernelSymbol& symbol) {
330   return (symbol.type == 'T' || symbol.type == 't' || symbol.type == 'W' ||
331           symbol.type == 'w');
332 }
333
334 static bool KernelSymbolCallback(const KernelSymbol& kernel_symbol,
335                                  std::vector<Symbol>* symbols) {
336   if (IsKernelFunctionSymbol(kernel_symbol)) {
337     symbols->emplace_back(Symbol(kernel_symbol.name, kernel_symbol.addr, 0));
338   }
339   return false;
340 }
341
342 static void VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol,
343                                   std::vector<Symbol>* symbols) {
344   if (elf_symbol.is_func) {
345     symbols->emplace_back(
346         Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
347   }
348 }
349
350 bool Dso::CheckReadSymbolResult(ElfStatus result, const std::string& filename) {
351   if (result == ElfStatus::NO_ERROR) {
352     LOG(VERBOSE) << "Read symbols from " << filename << " successfully";
353     return true;
354   } else if (result == ElfStatus::NO_SYMBOL_TABLE) {
355     if (path_ == "[vdso]") {
356       // Vdso only contains dynamic symbol table, and we can't change that.
357       return true;
358     }
359     // Lacking symbol table isn't considered as an error but worth reporting.
360     LOG(symbol_warning_loglevel_) << filename << " doesn't contain symbol table";
361     return true;
362   } else {
363     LOG(symbol_warning_loglevel_) << "failed to read symbols from " << filename << ": " << result;
364     return false;
365   }
366 }
367
368 bool Dso::LoadKernel() {
369   BuildId build_id = GetExpectedBuildId();
370   if (!vmlinux_.empty()) {
371     ElfStatus result = ParseSymbolsFromElfFile(vmlinux_, build_id,
372         std::bind(VmlinuxSymbolCallback, std::placeholders::_1, &symbols_));
373     return CheckReadSymbolResult(result, vmlinux_);
374   } else if (!kallsyms_.empty()) {
375     ProcessKernelSymbols(kallsyms_, std::bind(&KernelSymbolCallback,
376                                               std::placeholders::_1, &symbols_));
377     bool all_zero = true;
378     for (const auto& symbol : symbols_) {
379       if (symbol.addr != 0) {
380         all_zero = false;
381         break;
382       }
383     }
384     if (all_zero) {
385       LOG(symbol_warning_loglevel_)
386           << "Symbol addresses in /proc/kallsyms on device are all zero. "
387              "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
388       symbols_.clear();
389       return false;
390     }
391   } else if (read_kernel_symbols_from_proc_ || !build_id.IsEmpty()) {
392     // Try /proc/kallsyms only when asked to do so, or when build id matches.
393     // Otherwise, it is likely to use /proc/kallsyms on host for perf.data recorded on device.
394     if (!build_id.IsEmpty()) {
395       BuildId real_build_id;
396       if (!GetKernelBuildId(&real_build_id)) {
397         return false;
398       }
399       bool match = (build_id == real_build_id);
400       if (!match) {
401         LOG(symbol_warning_loglevel_) << "failed to read symbols from /proc/kallsyms: Build id "
402                                       << "mismatch";
403         return false;
404       }
405     }
406
407     std::string kallsyms;
408     if (!android::base::ReadFileToString("/proc/kallsyms", &kallsyms)) {
409       LOG(DEBUG) << "failed to read /proc/kallsyms";
410       return false;
411     }
412     ProcessKernelSymbols(kallsyms, std::bind(&KernelSymbolCallback,
413                                              std::placeholders::_1, &symbols_));
414     bool all_zero = true;
415     for (const auto& symbol : symbols_) {
416       if (symbol.addr != 0) {
417         all_zero = false;
418         break;
419       }
420     }
421     if (all_zero) {
422       LOG(symbol_warning_loglevel_) << "Symbol addresses in /proc/kallsyms are all zero. "
423                                        "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
424       symbols_.clear();
425       return false;
426     }
427   }
428   return true;
429 }
430
431 static void ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol,
432                                   bool (*filter)(const ElfFileSymbol&),
433                                   std::vector<Symbol>* symbols) {
434   if (filter(elf_symbol)) {
435     symbols->emplace_back(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len);
436   }
437 }
438
439 static bool SymbolFilterForKernelModule(const ElfFileSymbol& elf_symbol) {
440   // TODO: Parse symbol outside of .text section.
441   return (elf_symbol.is_func && elf_symbol.is_in_text_section);
442 }
443
444 bool Dso::LoadKernelModule() {
445   BuildId build_id = GetExpectedBuildId();
446   ElfStatus result = ParseSymbolsFromElfFile(GetDebugFilePath(), build_id,
447       std::bind(ElfFileSymbolCallback, std::placeholders::_1,
448                 SymbolFilterForKernelModule, &symbols_));
449   return CheckReadSymbolResult(result, GetDebugFilePath());
450 }
451
452 static bool SymbolFilterForDso(const ElfFileSymbol& elf_symbol) {
453   return elf_symbol.is_func ||
454          (elf_symbol.is_label && elf_symbol.is_in_text_section);
455 }
456
457 bool Dso::LoadElfFile() {
458   BuildId build_id = GetExpectedBuildId();
459
460   if (symfs_dir_.empty()) {
461     // Linux host can store debug shared libraries in /usr/lib/debug.
462     ElfStatus result = ParseSymbolsFromElfFile(
463         "/usr/lib/debug" + path_, build_id,
464         std::bind(ElfFileSymbolCallback, std::placeholders::_1,
465                   SymbolFilterForDso, &symbols_));
466     if (result == ElfStatus::NO_ERROR) {
467       return CheckReadSymbolResult(result, "/usr/lib/debug" + path_);
468     }
469   }
470   // TODO: load std::vector<Symbol> directly from ParseSymbolsFromElfFile
471   // instead of needing to call a callback function for each symbol.
472   ElfStatus result = ParseSymbolsFromElfFile(
473       GetDebugFilePath(), build_id,
474       std::bind(ElfFileSymbolCallback, std::placeholders::_1,
475                 SymbolFilterForDso, &symbols_));
476   return CheckReadSymbolResult(result, GetDebugFilePath());
477 }
478
479 bool Dso::LoadEmbeddedElfFile() {
480   BuildId build_id = GetExpectedBuildId();
481   auto tuple = SplitUrlInApk(GetDebugFilePath());
482   CHECK(std::get<0>(tuple));
483   ElfStatus result = ParseSymbolsFromApkFile(
484       std::get<1>(tuple), std::get<2>(tuple), build_id,
485       std::bind(ElfFileSymbolCallback, std::placeholders::_1,
486                 SymbolFilterForDso, &symbols_));
487   return CheckReadSymbolResult(result, GetDebugFilePath());
488 }
489
490 void Dso::FixupSymbolLength() {
491   Symbol* prev_symbol = nullptr;
492   for (auto& symbol : symbols_) {
493     if (prev_symbol != nullptr && prev_symbol->len == 0) {
494       prev_symbol->len = symbol.addr - prev_symbol->addr;
495     }
496     prev_symbol = &symbol;
497   }
498   if (prev_symbol != nullptr && prev_symbol->len == 0) {
499     prev_symbol->len = std::numeric_limits<uint64_t>::max() - prev_symbol->addr;
500   }
501 }
502
503 const char* DsoTypeToString(DsoType dso_type) {
504   switch (dso_type) {
505     case DSO_KERNEL:
506       return "dso_kernel";
507     case DSO_KERNEL_MODULE:
508       return "dso_kernel_module";
509     case DSO_ELF_FILE:
510       return "dso_elf_file";
511     default:
512       return "unknown";
513   }
514 }