2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
29 #include "environment.h"
34 static OneTimeFreeAllocator symbol_name_allocator;
36 Symbol::Symbol(const std::string& name, uint64_t addr, uint64_t len)
39 name_(symbol_name_allocator.AllocateString(name)),
40 demangled_name_(nullptr),
43 const char* Symbol::DemangledName() const {
44 if (demangled_name_ == nullptr) {
45 const std::string s = Dso::Demangle(name_);
47 demangled_name_ = name_;
49 demangled_name_ = symbol_name_allocator.AllocateString(s);
52 return demangled_name_;
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_;
66 void Dso::SetDemangle(bool demangle) { demangle_ = demangle; }
68 extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n,
71 std::string Dso::Demangle(const std::string& name) {
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();
81 std::string result = name;
82 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
84 if (is_linker_symbol) {
85 result = std::string("[linker]") + demangled_name;
87 result = demangled_name;
90 } else if (is_linker_symbol) {
91 result = std::string("[linker]") + mangled_str;
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('/');
102 if (!IsDir(symfs_dir)) {
103 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'";
107 symfs_dir_ = dirname;
111 void Dso::SetVmlinux(const std::string& vmlinux) { vmlinux_ = vmlinux; }
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();
121 build_id_map_ = std::move(map);
124 void Dso::SetVdsoFile(std::unique_ptr<TemporaryFile> vdso_file, bool is_64bit) {
126 vdso_64bit_ = std::move(vdso_file);
128 vdso_32bit_ = std::move(vdso_file);
132 BuildId Dso::FindExpectedBuildIdForPath(const std::string& path) {
133 auto it = build_id_map_.find(path);
134 if (it != build_id_map_.end()) {
140 BuildId Dso::GetExpectedBuildId() {
141 return FindExpectedBuildIdForPath(path_);
144 std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path,
146 return std::unique_ptr<Dso>(new Dso(dso_type, dso_path, force_64bit));
149 Dso::Dso(DsoType type, const std::string& path, bool force_64bit)
152 debug_file_path_(path),
153 min_vaddr_(std::numeric_limits<uint64_t>::max()),
157 symbol_warning_loglevel_(android::base::WARNING) {
158 if (type_ == DSO_KERNEL) {
161 // Check if file matching path_ exists in symfs directory before using it as
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;
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;
179 size_t pos = path.find_last_of("/\\");
180 if (pos != std::string::npos) {
181 file_name_ = path.substr(pos + 1);
189 if (--dso_count_ == 0) {
190 // Clean up global variables when no longer used.
191 symbol_name_allocator.Clear();
196 read_kernel_symbols_from_proc_ = false;
197 build_id_map_.clear();
199 vdso_64bit_ = nullptr;
200 vdso_32bit_ = nullptr;
204 uint32_t Dso::CreateDumpId() {
206 return dump_id_ = g_dump_id_++;
209 uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
210 CHECK(!symbol->HasDumpId());
211 symbol->dump_id_ = symbol_dump_id_++;
212 return symbol->dump_id_;
215 const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
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()) {
225 if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
230 if (!unknown_symbols_.empty()) {
231 auto it = unknown_symbols_.find(vaddr_in_dso);
232 if (it != unknown_symbols_.end()) {
239 const std::vector<Symbol>& Dso::GetSymbols() {
246 void Dso::SetSymbols(std::vector<Symbol>* symbols) {
247 symbols_ = std::move(*symbols);
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)));
255 uint64_t Dso::MinVirtualAddress() {
256 if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
258 if (type_ == DSO_ELF_FILE) {
259 BuildId build_id = GetExpectedBuildId();
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;
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);
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_);
291 // Don't warn missing symbol table if we have dumped symbols in perf.data.
292 symbol_warning_loglevel_ = android::base::DEBUG;
297 result = LoadKernel();
299 case DSO_KERNEL_MODULE:
300 result = LoadKernelModule();
303 if (std::get<0>(SplitUrlInApk(path_))) {
304 result = LoadEmbeddedElfFile();
306 result = LoadElfFile();
312 std::sort(symbols_.begin(), symbols_.end(), Symbol::CompareValueByAddr);
318 if (symbols_.empty()) {
319 symbols_ = std::move(dumped_symbols);
320 } else if (!dumped_symbols.empty()) {
321 symbols_ = MergeSortedSymbols(symbols_, dumped_symbols);
324 if (symbols_.empty()) {
325 LOG(DEBUG) << "failed to load dso: " << path_;
329 static bool IsKernelFunctionSymbol(const KernelSymbol& symbol) {
330 return (symbol.type == 'T' || symbol.type == 't' || symbol.type == 'W' ||
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));
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));
350 bool Dso::CheckReadSymbolResult(ElfStatus result, const std::string& filename) {
351 if (result == ElfStatus::NO_ERROR) {
352 LOG(VERBOSE) << "Read symbols from " << filename << " successfully";
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.
359 // Lacking symbol table isn't considered as an error but worth reporting.
360 LOG(symbol_warning_loglevel_) << filename << " doesn't contain symbol table";
363 LOG(symbol_warning_loglevel_) << "failed to read symbols from " << filename << ": " << result;
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) {
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.";
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)) {
399 bool match = (build_id == real_build_id);
401 LOG(symbol_warning_loglevel_) << "failed to read symbols from /proc/kallsyms: Build id "
407 std::string kallsyms;
408 if (!android::base::ReadFileToString("/proc/kallsyms", &kallsyms)) {
409 LOG(DEBUG) << "failed to read /proc/kallsyms";
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) {
422 LOG(symbol_warning_loglevel_) << "Symbol addresses in /proc/kallsyms are all zero. "
423 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
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);
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);
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());
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);
457 bool Dso::LoadElfFile() {
458 BuildId build_id = GetExpectedBuildId();
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_);
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());
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());
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;
496 prev_symbol = &symbol;
498 if (prev_symbol != nullptr && prev_symbol->len == 0) {
499 prev_symbol->len = std::numeric_limits<uint64_t>::max() - prev_symbol->addr;
503 const char* DsoTypeToString(DsoType dso_type) {
507 case DSO_KERNEL_MODULE:
508 return "dso_kernel_module";
510 return "dso_elf_file";