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/logging.h>
28 #include "environment.h"
32 static OneTimeFreeAllocator symbol_name_allocator;
34 Symbol::Symbol(const std::string& name, uint64_t addr, uint64_t len)
37 name_(symbol_name_allocator.AllocateString(name)),
38 demangled_name_(nullptr) {
41 const char* Symbol::DemangledName() const {
42 if (demangled_name_ == nullptr) {
43 const std::string s = Dso::Demangle(name_);
45 demangled_name_ = name_;
47 demangled_name_ = symbol_name_allocator.AllocateString(s);
50 return demangled_name_;
53 bool Dso::demangle_ = true;
54 std::string Dso::symfs_dir_;
55 std::string Dso::vmlinux_;
56 std::unordered_map<std::string, BuildId> Dso::build_id_map_;
57 size_t Dso::dso_count_;
59 void Dso::SetDemangle(bool demangle) {
63 extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n, int* status);
65 std::string Dso::Demangle(const std::string& name) {
70 bool is_linker_symbol = (name.find(linker_prefix) == 0);
71 const char* mangled_str = name.c_str();
72 if (is_linker_symbol) {
73 mangled_str += linker_prefix.size();
75 std::string result = name;
76 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
78 if (is_linker_symbol) {
79 result = std::string("[linker]") + demangled_name;
81 result = demangled_name;
84 } else if (is_linker_symbol) {
85 result = std::string("[linker]") + mangled_str;
90 bool Dso::SetSymFsDir(const std::string& symfs_dir) {
91 std::string dirname = symfs_dir;
92 if (!dirname.empty()) {
93 if (dirname.back() != '/') {
94 dirname.push_back('/');
96 std::vector<std::string> files;
97 std::vector<std::string> subdirs;
98 GetEntriesInDir(symfs_dir, &files, &subdirs);
99 if (files.empty() && subdirs.empty()) {
100 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'";
104 symfs_dir_ = dirname;
108 void Dso::SetVmlinux(const std::string& vmlinux) {
112 void Dso::SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids) {
113 std::unordered_map<std::string, BuildId> map;
114 for (auto& pair : build_ids) {
115 LOG(DEBUG) << "build_id_map: " << pair.first << ", " << pair.second.ToString();
118 build_id_map_ = std::move(map);
121 BuildId Dso::GetExpectedBuildId(const std::string& filename) {
122 auto it = build_id_map_.find(filename);
123 if (it != build_id_map_.end()) {
129 std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path) {
130 std::string path = dso_path;
131 if (dso_type == DSO_KERNEL) {
132 path = "[kernel.kallsyms]";
134 return std::unique_ptr<Dso>(new Dso(dso_type, path));
137 Dso::Dso(DsoType type, const std::string& path)
138 : type_(type), path_(path), min_vaddr_(std::numeric_limits<uint64_t>::max()), is_loaded_(false) {
143 if (--dso_count_ == 0) {
144 symbol_name_allocator.Clear();
148 struct SymbolComparator {
149 bool operator()(const Symbol& symbol1, const Symbol& symbol2) {
150 return symbol1.addr < symbol2.addr;
154 std::string Dso::GetAccessiblePath() const {
155 return symfs_dir_ + path_;
158 const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
162 LOG(DEBUG) << "failed to load dso: " << path_;
167 auto it = std::upper_bound(symbols_.begin(), symbols_.end(), Symbol("", vaddr_in_dso, 0),
169 if (it != symbols_.begin()) {
171 if (it->addr <= vaddr_in_dso && it->addr + it->len > vaddr_in_dso) {
178 uint64_t Dso::MinVirtualAddress() {
179 if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
181 if (type_ == DSO_ELF_FILE) {
182 BuildId build_id = GetExpectedBuildId(GetAccessiblePath());
185 if (ReadMinExecutableVirtualAddressFromElfFile(GetAccessiblePath(), build_id, &addr)) {
197 result = LoadKernel();
199 case DSO_KERNEL_MODULE:
200 result = LoadKernelModule();
203 result = LoadElfFile();
207 std::sort(symbols_.begin(), symbols_.end(), SymbolComparator());
213 static bool IsKernelFunctionSymbol(const KernelSymbol& symbol) {
214 return (symbol.type == 'T' || symbol.type == 't' || symbol.type == 'W' || symbol.type == 'w');
217 bool Dso::KernelSymbolCallback(const KernelSymbol& kernel_symbol, Dso* dso) {
218 if (IsKernelFunctionSymbol(kernel_symbol)) {
219 dso->InsertSymbol(Symbol(kernel_symbol.name, kernel_symbol.addr, 0));
224 void Dso::VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso) {
225 if (elf_symbol.is_func) {
226 dso->InsertSymbol(Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
230 bool Dso::LoadKernel() {
231 BuildId build_id = GetExpectedBuildId(DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID);
232 if (!vmlinux_.empty()) {
233 ParseSymbolsFromElfFile(vmlinux_, build_id,
234 std::bind(VmlinuxSymbolCallback, std::placeholders::_1, this));
236 if (!build_id.IsEmpty()) {
237 BuildId real_build_id;
238 GetKernelBuildId(&real_build_id);
239 bool match = (build_id == real_build_id);
240 LOG(DEBUG) << "check kernel build id (" << (match ? "match" : "mismatch") << "): expected "
241 << build_id.ToString() << ", real " << real_build_id.ToString();
247 ProcessKernelSymbols("/proc/kallsyms",
248 std::bind(&KernelSymbolCallback, std::placeholders::_1, this));
250 for (auto& symbol : symbols_) {
251 if (symbol.addr != 0) {
257 LOG(WARNING) << "Symbol addresses in /proc/kallsyms are all zero. Check "
258 "/proc/sys/kernel/kptr_restrict if possible.";
266 void Dso::ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso,
267 bool (*filter)(const ElfFileSymbol&)) {
268 if (filter(elf_symbol)) {
269 dso->InsertSymbol(Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
273 static bool SymbolFilterForKernelModule(const ElfFileSymbol& elf_symbol) {
274 // TODO: Parse symbol outside of .text section.
275 return (elf_symbol.is_func && elf_symbol.is_in_text_section);
278 bool Dso::LoadKernelModule() {
279 BuildId build_id = GetExpectedBuildId(path_);
280 ParseSymbolsFromElfFile(
281 symfs_dir_ + path_, build_id,
282 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this, SymbolFilterForKernelModule));
286 static bool SymbolFilterForDso(const ElfFileSymbol& elf_symbol) {
287 return elf_symbol.is_func || (elf_symbol.is_label && elf_symbol.is_in_text_section);
290 bool Dso::LoadElfFile() {
292 BuildId build_id = GetExpectedBuildId(GetAccessiblePath());
294 if (symfs_dir_.empty()) {
295 // Linux host can store debug shared libraries in /usr/lib/debug.
296 loaded = ParseSymbolsFromElfFile(
297 "/usr/lib/debug" + path_, build_id,
298 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this, SymbolFilterForDso));
301 loaded = ParseSymbolsFromElfFile(
302 GetAccessiblePath(), build_id,
303 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this, SymbolFilterForDso));
308 void Dso::InsertSymbol(const Symbol& symbol) {
309 symbols_.push_back(symbol);
312 void Dso::FixupSymbolLength() {
313 Symbol* prev_symbol = nullptr;
314 for (auto& symbol : symbols_) {
315 if (prev_symbol != nullptr && prev_symbol->len == 0) {
316 prev_symbol->len = symbol.addr - prev_symbol->addr;
318 prev_symbol = &symbol;
320 if (prev_symbol != nullptr && prev_symbol->len == 0) {
321 prev_symbol->len = std::numeric_limits<unsigned long long>::max() - prev_symbol->addr;