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"
33 static OneTimeFreeAllocator symbol_name_allocator;
35 Symbol::Symbol(const std::string& name, uint64_t addr, uint64_t len)
38 name_(symbol_name_allocator.AllocateString(name)),
39 demangled_name_(nullptr) {
42 const char* Symbol::DemangledName() const {
43 if (demangled_name_ == nullptr) {
44 const std::string s = Dso::Demangle(name_);
46 demangled_name_ = name_;
48 demangled_name_ = symbol_name_allocator.AllocateString(s);
51 return demangled_name_;
54 bool Dso::demangle_ = true;
55 std::string Dso::symfs_dir_;
56 std::string Dso::vmlinux_;
57 std::unordered_map<std::string, BuildId> Dso::build_id_map_;
58 size_t Dso::dso_count_;
60 void Dso::SetDemangle(bool demangle) {
64 extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n, int* status);
66 std::string Dso::Demangle(const std::string& name) {
71 bool is_linker_symbol = (name.find(linker_prefix) == 0);
72 const char* mangled_str = name.c_str();
73 if (is_linker_symbol) {
74 mangled_str += linker_prefix.size();
76 std::string result = name;
77 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
79 if (is_linker_symbol) {
80 result = std::string("[linker]") + demangled_name;
82 result = demangled_name;
85 } else if (is_linker_symbol) {
86 result = std::string("[linker]") + mangled_str;
91 bool Dso::SetSymFsDir(const std::string& symfs_dir) {
92 std::string dirname = symfs_dir;
93 if (!dirname.empty()) {
94 if (dirname.back() != '/') {
95 dirname.push_back('/');
97 std::vector<std::string> files;
98 std::vector<std::string> subdirs;
99 GetEntriesInDir(symfs_dir, &files, &subdirs);
100 if (files.empty() && subdirs.empty()) {
101 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'";
105 symfs_dir_ = dirname;
109 void Dso::SetVmlinux(const std::string& vmlinux) {
113 void Dso::SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids) {
114 std::unordered_map<std::string, BuildId> map;
115 for (auto& pair : build_ids) {
116 LOG(DEBUG) << "build_id_map: " << pair.first << ", " << pair.second.ToString();
119 build_id_map_ = std::move(map);
122 BuildId Dso::GetExpectedBuildId(const std::string& filename) {
123 auto it = build_id_map_.find(filename);
124 if (it != build_id_map_.end()) {
130 std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path) {
131 std::string path = dso_path;
132 if (dso_type == DSO_KERNEL) {
133 path = "[kernel.kallsyms]";
135 return std::unique_ptr<Dso>(new Dso(dso_type, path));
138 Dso::Dso(DsoType type, const std::string& path)
139 : type_(type), path_(path), min_vaddr_(std::numeric_limits<uint64_t>::max()), is_loaded_(false) {
144 if (--dso_count_ == 0) {
145 symbol_name_allocator.Clear();
149 struct SymbolComparator {
150 bool operator()(const Symbol& symbol1, const Symbol& symbol2) {
151 return symbol1.addr < symbol2.addr;
155 std::string Dso::GetAccessiblePath() const {
156 return symfs_dir_ + path_;
159 const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
163 LOG(DEBUG) << "failed to load dso: " << path_;
168 auto it = std::upper_bound(symbols_.begin(), symbols_.end(), Symbol("", vaddr_in_dso, 0),
170 if (it != symbols_.begin()) {
172 if (it->addr <= vaddr_in_dso && it->addr + it->len > vaddr_in_dso) {
179 uint64_t Dso::MinVirtualAddress() {
180 if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
182 if (type_ == DSO_ELF_FILE) {
183 BuildId build_id = GetExpectedBuildId(GetAccessiblePath());
186 if (ReadMinExecutableVirtualAddressFromElfFile(GetAccessiblePath(), build_id, &addr)) {
198 result = LoadKernel();
200 case DSO_KERNEL_MODULE:
201 result = LoadKernelModule();
204 if (std::get<0>(SplitUrlInApk(path_))) {
205 result = LoadEmbeddedElfFile();
207 result = LoadElfFile();
213 std::sort(symbols_.begin(), symbols_.end(), SymbolComparator());
219 static bool IsKernelFunctionSymbol(const KernelSymbol& symbol) {
220 return (symbol.type == 'T' || symbol.type == 't' || symbol.type == 'W' || symbol.type == 'w');
223 bool Dso::KernelSymbolCallback(const KernelSymbol& kernel_symbol, Dso* dso) {
224 if (IsKernelFunctionSymbol(kernel_symbol)) {
225 dso->InsertSymbol(Symbol(kernel_symbol.name, kernel_symbol.addr, 0));
230 void Dso::VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso) {
231 if (elf_symbol.is_func) {
232 dso->InsertSymbol(Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
236 bool Dso::LoadKernel() {
237 BuildId build_id = GetExpectedBuildId(DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID);
238 if (!vmlinux_.empty()) {
239 ParseSymbolsFromElfFile(vmlinux_, build_id,
240 std::bind(VmlinuxSymbolCallback, std::placeholders::_1, this));
242 if (!build_id.IsEmpty()) {
243 BuildId real_build_id;
244 GetKernelBuildId(&real_build_id);
245 bool match = (build_id == real_build_id);
246 LOG(DEBUG) << "check kernel build id (" << (match ? "match" : "mismatch") << "): expected "
247 << build_id.ToString() << ", real " << real_build_id.ToString();
253 ProcessKernelSymbols("/proc/kallsyms",
254 std::bind(&KernelSymbolCallback, std::placeholders::_1, this));
256 for (auto& symbol : symbols_) {
257 if (symbol.addr != 0) {
263 LOG(WARNING) << "Symbol addresses in /proc/kallsyms are all zero. Check "
264 "/proc/sys/kernel/kptr_restrict if possible.";
272 void Dso::ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso,
273 bool (*filter)(const ElfFileSymbol&)) {
274 if (filter(elf_symbol)) {
275 dso->InsertSymbol(Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
279 static bool SymbolFilterForKernelModule(const ElfFileSymbol& elf_symbol) {
280 // TODO: Parse symbol outside of .text section.
281 return (elf_symbol.is_func && elf_symbol.is_in_text_section);
284 bool Dso::LoadKernelModule() {
285 BuildId build_id = GetExpectedBuildId(path_);
286 ParseSymbolsFromElfFile(
287 symfs_dir_ + path_, build_id,
288 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this, SymbolFilterForKernelModule));
292 static bool SymbolFilterForDso(const ElfFileSymbol& elf_symbol) {
293 return elf_symbol.is_func || (elf_symbol.is_label && elf_symbol.is_in_text_section);
296 bool Dso::LoadElfFile() {
298 BuildId build_id = GetExpectedBuildId(GetAccessiblePath());
300 if (symfs_dir_.empty()) {
301 // Linux host can store debug shared libraries in /usr/lib/debug.
302 loaded = ParseSymbolsFromElfFile(
303 "/usr/lib/debug" + path_, build_id,
304 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this, SymbolFilterForDso));
307 loaded = ParseSymbolsFromElfFile(
308 GetAccessiblePath(), build_id,
309 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this, SymbolFilterForDso));
314 bool Dso::LoadEmbeddedElfFile() {
315 std::string path = GetAccessiblePath();
316 BuildId build_id = GetExpectedBuildId(path);
317 auto tuple = SplitUrlInApk(path);
318 CHECK(std::get<0>(tuple));
319 return ParseSymbolsFromApkFile(std::get<1>(tuple), std::get<2>(tuple), build_id,
320 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
321 this, SymbolFilterForDso));
324 void Dso::InsertSymbol(const Symbol& symbol) {
325 symbols_.push_back(symbol);
328 void Dso::FixupSymbolLength() {
329 Symbol* prev_symbol = nullptr;
330 for (auto& symbol : symbols_) {
331 if (prev_symbol != nullptr && prev_symbol->len == 0) {
332 prev_symbol->len = symbol.addr - prev_symbol->addr;
334 prev_symbol = &symbol;
336 if (prev_symbol != nullptr && prev_symbol->len == 0) {
337 prev_symbol->len = std::numeric_limits<unsigned long long>::max() - prev_symbol->addr;