blob: 1c59e606295074dce55eb57fb98ab360d486368e [file] [log] [blame]
Yabin Cuiec12ed92015-06-08 10:38:10 -07001/*
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
Yabin Cuib3783552015-06-11 11:15:42 -070019#include <stdlib.h>
Yabin Cuicc2e59e2015-08-21 14:23:43 -070020#include <string.h>
Yabin Cuic8485602015-08-20 15:04:39 -070021
Yabin Cuicc2e59e2015-08-21 14:23:43 -070022#include <algorithm>
Yabin Cuic8485602015-08-20 15:04:39 -070023#include <limits>
Yabin Cuicc2e59e2015-08-21 14:23:43 -070024#include <vector>
Yabin Cuic8485602015-08-20 15:04:39 -070025
Yabin Cuib4212972016-05-25 14:08:05 -070026#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080027#include <android-base/logging.h>
Yabin Cuic8485602015-08-20 15:04:39 -070028
Yabin Cuiec12ed92015-06-08 10:38:10 -070029#include "environment.h"
Yabin Cuib1a885b2016-02-14 19:18:02 -080030#include "read_apk.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070031#include "read_elf.h"
Yabin Cuib3783552015-06-11 11:15:42 -070032#include "utils.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070033
Yabin Cuicc2e59e2015-08-21 14:23:43 -070034static OneTimeFreeAllocator symbol_name_allocator;
35
36Symbol::Symbol(const std::string& name, uint64_t addr, uint64_t len)
37 : addr(addr),
38 len(len),
39 name_(symbol_name_allocator.AllocateString(name)),
Yabin Cui767dd172016-06-02 21:02:43 -070040 demangled_name_(nullptr),
Yabin Cui16501ff2016-10-19 15:06:29 -070041 dump_id_(UINT_MAX) {}
Yabin Cuib10a8fb2015-08-18 16:32:18 -070042
Yabin Cuicc2e59e2015-08-21 14:23:43 -070043const 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_;
Yabin Cuiec12ed92015-06-08 10:38:10 -070053}
54
Yabin Cuic8485602015-08-20 15:04:39 -070055bool Dso::demangle_ = true;
56std::string Dso::symfs_dir_;
57std::string Dso::vmlinux_;
Yabin Cuib4212972016-05-25 14:08:05 -070058std::string Dso::kallsyms_;
Yabin Cuic8485602015-08-20 15:04:39 -070059std::unordered_map<std::string, BuildId> Dso::build_id_map_;
Yabin Cuicc2e59e2015-08-21 14:23:43 -070060size_t Dso::dso_count_;
Yabin Cui16501ff2016-10-19 15:06:29 -070061uint32_t Dso::g_dump_id_;
Yabin Cuiba50c4b2015-07-21 11:24:48 -070062
Yabin Cui767dd172016-06-02 21:02:43 -070063void Dso::SetDemangle(bool demangle) { demangle_ = demangle; }
Yabin Cuib3783552015-06-11 11:15:42 -070064
Yabin Cui767dd172016-06-02 21:02:43 -070065extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n,
66 int* status);
Yabin Cuib10a8fb2015-08-18 16:32:18 -070067
Yabin Cuic8485602015-08-20 15:04:39 -070068std::string Dso::Demangle(const std::string& name) {
Yabin Cuib10a8fb2015-08-18 16:32:18 -070069 if (!demangle_) {
70 return name;
71 }
72 int status;
73 bool is_linker_symbol = (name.find(linker_prefix) == 0);
74 const char* mangled_str = name.c_str();
75 if (is_linker_symbol) {
76 mangled_str += linker_prefix.size();
77 }
78 std::string result = name;
79 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
80 if (status == 0) {
81 if (is_linker_symbol) {
82 result = std::string("[linker]") + demangled_name;
83 } else {
84 result = demangled_name;
85 }
86 free(demangled_name);
87 } else if (is_linker_symbol) {
88 result = std::string("[linker]") + mangled_str;
89 }
90 return result;
91}
92
Yabin Cuic8485602015-08-20 15:04:39 -070093bool Dso::SetSymFsDir(const std::string& symfs_dir) {
94 std::string dirname = symfs_dir;
95 if (!dirname.empty()) {
96 if (dirname.back() != '/') {
97 dirname.push_back('/');
98 }
Yabin Cui07865862016-08-22 13:39:19 -070099 if (GetEntriesInDir(symfs_dir).empty()) {
Yabin Cuic8485602015-08-20 15:04:39 -0700100 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'";
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700101 return false;
Yabin Cuic8485602015-08-20 15:04:39 -0700102 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700103 }
Yabin Cuic8485602015-08-20 15:04:39 -0700104 symfs_dir_ = dirname;
105 return true;
106}
107
Yabin Cui767dd172016-06-02 21:02:43 -0700108void Dso::SetVmlinux(const std::string& vmlinux) { vmlinux_ = vmlinux; }
Yabin Cuic8485602015-08-20 15:04:39 -0700109
Yabin Cui767dd172016-06-02 21:02:43 -0700110void Dso::SetBuildIds(
111 const std::vector<std::pair<std::string, BuildId>>& build_ids) {
Yabin Cuic8485602015-08-20 15:04:39 -0700112 std::unordered_map<std::string, BuildId> map;
113 for (auto& pair : build_ids) {
Yabin Cui767dd172016-06-02 21:02:43 -0700114 LOG(DEBUG) << "build_id_map: " << pair.first << ", "
115 << pair.second.ToString();
Yabin Cuic8485602015-08-20 15:04:39 -0700116 map.insert(pair);
117 }
118 build_id_map_ = std::move(map);
119}
120
Yabin Cuieec606c2016-07-07 13:53:33 -0700121BuildId Dso::GetExpectedBuildId() {
122 auto it = build_id_map_.find(path_);
Yabin Cuic8485602015-08-20 15:04:39 -0700123 if (it != build_id_map_.end()) {
124 return it->second;
125 }
126 return BuildId();
127}
128
Yabin Cui767dd172016-06-02 21:02:43 -0700129std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type,
130 const std::string& dso_path) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700131 return std::unique_ptr<Dso>(new Dso(dso_type, dso_path));
Yabin Cuic8485602015-08-20 15:04:39 -0700132}
133
Yabin Cuic5b4a312016-10-24 13:38:38 -0700134Dso::Dso(DsoType type, const std::string& path)
Yabin Cui767dd172016-06-02 21:02:43 -0700135 : type_(type),
Yabin Cui767dd172016-06-02 21:02:43 -0700136 path_(path),
Yabin Cuieec606c2016-07-07 13:53:33 -0700137 debug_file_path_(path),
Yabin Cui767dd172016-06-02 21:02:43 -0700138 min_vaddr_(std::numeric_limits<uint64_t>::max()),
139 is_loaded_(false),
Yabin Cui16501ff2016-10-19 15:06:29 -0700140 dump_id_(UINT_MAX),
141 symbol_dump_id_(0) {
142 if (type_ == DSO_KERNEL) {
143 min_vaddr_ = 0;
144 }
Yabin Cuieec606c2016-07-07 13:53:33 -0700145 // Check if file matching path_ exists in symfs directory before using it as
146 // debug_file_path_.
147 if (!symfs_dir_.empty()) {
148 std::string path_in_symfs = symfs_dir_ + path_;
149 std::tuple<bool, std::string, std::string> tuple =
150 SplitUrlInApk(path_in_symfs);
151 std::string file_path =
152 std::get<0>(tuple) ? std::get<1>(tuple) : path_in_symfs;
153 if (IsRegularFile(file_path)) {
154 debug_file_path_ = path_in_symfs;
155 }
156 }
Yabin Cui15475e62016-07-14 13:26:19 -0700157 size_t pos = path.find_last_of("/\\");
158 if (pos != std::string::npos) {
159 file_name_ = path.substr(pos + 1);
160 } else {
161 file_name_ = path;
162 }
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700163 dso_count_++;
Yabin Cuic8485602015-08-20 15:04:39 -0700164}
165
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700166Dso::~Dso() {
167 if (--dso_count_ == 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700168 // Clean up global variables when no longer used.
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700169 symbol_name_allocator.Clear();
Yabin Cuib4212972016-05-25 14:08:05 -0700170 demangle_ = true;
171 symfs_dir_.clear();
172 vmlinux_.clear();
173 kallsyms_.clear();
174 build_id_map_.clear();
Yabin Cui16501ff2016-10-19 15:06:29 -0700175 g_dump_id_ = 0;
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700176 }
177}
178
Yabin Cuic5b4a312016-10-24 13:38:38 -0700179static bool CompareSymbol(const Symbol& symbol1, const Symbol& symbol2) {
180 return symbol1.addr < symbol2.addr;
181}
182
Yabin Cui16501ff2016-10-19 15:06:29 -0700183uint32_t Dso::CreateDumpId() {
184 CHECK(!HasDumpId());
185 return dump_id_ = g_dump_id_++;
186}
187
188uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
189 CHECK(!symbol->HasDumpId());
190 symbol->dump_id_ = symbol_dump_id_++;
191 return symbol->dump_id_;
192}
193
Yabin Cui547c60e2015-10-12 16:56:05 -0700194const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
Yabin Cuic8485602015-08-20 15:04:39 -0700195 if (!is_loaded_) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700196 Load();
197 }
198 if (!symbols_.empty()) {
199 auto it = std::upper_bound(symbols_.begin(), symbols_.end(), Symbol("", vaddr_in_dso, 0), CompareSymbol);
200 if (it != symbols_.begin()) {
201 --it;
202 if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
203 return &*it;
Yabin Cui767dd172016-06-02 21:02:43 -0700204 }
Yabin Cuic8485602015-08-20 15:04:39 -0700205 }
206 }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700207 if (!unknown_symbols_.empty()) {
208 auto it = unknown_symbols_.find(vaddr_in_dso);
209 if (it != unknown_symbols_.end()) {
210 return &it->second;
Yabin Cuic8485602015-08-20 15:04:39 -0700211 }
212 }
213 return nullptr;
214}
215
Yabin Cuic5b4a312016-10-24 13:38:38 -0700216const std::vector<Symbol>& Dso::GetSymbols() {
217 if (!is_loaded_) {
218 Load();
219 }
220 return symbols_;
221}
222
223void Dso::SetSymbols(std::vector<Symbol>* symbols) {
224 symbols_ = std::move(*symbols);
225 symbols->clear();
226}
227
228void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) {
229 unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1)));
230}
231
Yabin Cui547c60e2015-10-12 16:56:05 -0700232uint64_t Dso::MinVirtualAddress() {
233 if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
234 min_vaddr_ = 0;
235 if (type_ == DSO_ELF_FILE) {
Yabin Cuieec606c2016-07-07 13:53:33 -0700236 BuildId build_id = GetExpectedBuildId();
Yabin Cui547c60e2015-10-12 16:56:05 -0700237
238 uint64_t addr;
Yabin Cuidec43c12016-07-29 16:40:40 -0700239 ElfStatus result = ReadMinExecutableVirtualAddressFromElfFile(
240 GetDebugFilePath(), build_id, &addr);
241 if (result != ElfStatus::NO_ERROR) {
242 LOG(WARNING) << "failed to read min virtual address of "
243 << GetDebugFilePath() << ": " << result;
244 } else {
Yabin Cui547c60e2015-10-12 16:56:05 -0700245 min_vaddr_ = addr;
246 }
247 }
248 }
249 return min_vaddr_;
250}
251
Yabin Cuic5b4a312016-10-24 13:38:38 -0700252void Dso::Load() {
253 is_loaded_ = true;
254 if (!symbols_.empty()) {
255 // If symbols has been read from file feature section of perf.data, no
256 // need to load them from file system.
257 // TODO: combine symbols in file feature section and in file system.
258 return;
259 }
Yabin Cuic8485602015-08-20 15:04:39 -0700260 bool result = false;
261 switch (type_) {
262 case DSO_KERNEL:
263 result = LoadKernel();
264 break;
265 case DSO_KERNEL_MODULE:
266 result = LoadKernelModule();
267 break;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800268 case DSO_ELF_FILE: {
269 if (std::get<0>(SplitUrlInApk(path_))) {
270 result = LoadEmbeddedElfFile();
271 } else {
272 result = LoadElfFile();
273 }
Yabin Cuic8485602015-08-20 15:04:39 -0700274 break;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800275 }
Yabin Cuic8485602015-08-20 15:04:39 -0700276 }
277 if (result) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700278 std::sort(symbols_.begin(), symbols_.end(), CompareSymbol);
Yabin Cuic8485602015-08-20 15:04:39 -0700279 FixupSymbolLength();
Yabin Cuidec43c12016-07-29 16:40:40 -0700280 } else {
281 symbols_.clear();
Yabin Cuic5b4a312016-10-24 13:38:38 -0700282 LOG(DEBUG) << "failed to load dso: " << path_;
Yabin Cuic8485602015-08-20 15:04:39 -0700283 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700284}
285
Yabin Cuiec12ed92015-06-08 10:38:10 -0700286static bool IsKernelFunctionSymbol(const KernelSymbol& symbol) {
Yabin Cui767dd172016-06-02 21:02:43 -0700287 return (symbol.type == 'T' || symbol.type == 't' || symbol.type == 'W' ||
288 symbol.type == 'w');
Yabin Cuiec12ed92015-06-08 10:38:10 -0700289}
290
Yabin Cuic5b4a312016-10-24 13:38:38 -0700291static bool KernelSymbolCallback(const KernelSymbol& kernel_symbol,
292 std::vector<Symbol>* symbols) {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700293 if (IsKernelFunctionSymbol(kernel_symbol)) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700294 symbols->emplace_back(Symbol(kernel_symbol.name, kernel_symbol.addr, 0));
Yabin Cuiec12ed92015-06-08 10:38:10 -0700295 }
296 return false;
297}
298
Yabin Cuic5b4a312016-10-24 13:38:38 -0700299static void VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol,
300 std::vector<Symbol>* symbols) {
Yabin Cui39d3cae2015-07-13 16:23:13 -0700301 if (elf_symbol.is_func) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700302 symbols->emplace_back(
Yabin Cui767dd172016-06-02 21:02:43 -0700303 Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
Yabin Cui39d3cae2015-07-13 16:23:13 -0700304 }
305}
306
Yabin Cuidec43c12016-07-29 16:40:40 -0700307bool CheckReadSymbolResult(ElfStatus result, const std::string& filename) {
308 if (result == ElfStatus::NO_ERROR) {
Yabin Cuicb6c9012016-08-25 10:46:40 -0700309 LOG(VERBOSE) << "Read symbols from " << filename << " successfully";
Yabin Cuidec43c12016-07-29 16:40:40 -0700310 return true;
311 } else if (result == ElfStatus::NO_SYMBOL_TABLE) {
312 // Lacking symbol table isn't considered as an error but worth reporting.
313 LOG(WARNING) << filename << " doesn't contain symbol table";
314 return true;
315 } else {
316 LOG(WARNING) << "failed to read symbols from " << filename
317 << ": " << result;
318 return false;
319 }
320}
321
Yabin Cuic8485602015-08-20 15:04:39 -0700322bool Dso::LoadKernel() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700323 BuildId build_id = GetExpectedBuildId();
Yabin Cui39d3cae2015-07-13 16:23:13 -0700324 if (!vmlinux_.empty()) {
Yabin Cuidec43c12016-07-29 16:40:40 -0700325 ElfStatus result = ParseSymbolsFromElfFile(vmlinux_, build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700326 std::bind(VmlinuxSymbolCallback, std::placeholders::_1, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700327 return CheckReadSymbolResult(result, vmlinux_);
Yabin Cuib4212972016-05-25 14:08:05 -0700328 } else if (!kallsyms_.empty()) {
Yabin Cui767dd172016-06-02 21:02:43 -0700329 ProcessKernelSymbols(kallsyms_, std::bind(&KernelSymbolCallback,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700330 std::placeholders::_1, &symbols_));
Yabin Cuib4212972016-05-25 14:08:05 -0700331 bool all_zero = true;
332 for (const auto& symbol : symbols_) {
333 if (symbol.addr != 0) {
334 all_zero = false;
335 break;
336 }
337 }
338 if (all_zero) {
Yabin Cui767dd172016-06-02 21:02:43 -0700339 LOG(WARNING)
340 << "Symbol addresses in /proc/kallsyms on device are all zero. "
Yabin Cui6965d422016-06-15 11:41:42 -0700341 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
Yabin Cuib4212972016-05-25 14:08:05 -0700342 symbols_.clear();
343 return false;
344 }
Yabin Cui39d3cae2015-07-13 16:23:13 -0700345 } else {
Yabin Cui04c70a62015-08-04 14:48:39 -0700346 if (!build_id.IsEmpty()) {
347 BuildId real_build_id;
Yabin Cuidec43c12016-07-29 16:40:40 -0700348 if (!GetKernelBuildId(&real_build_id)) {
349 return false;
350 }
Yabin Cui04c70a62015-08-04 14:48:39 -0700351 bool match = (build_id == real_build_id);
Yabin Cui04c70a62015-08-04 14:48:39 -0700352 if (!match) {
Yabin Cuidec43c12016-07-29 16:40:40 -0700353 LOG(WARNING) << "failed to read symbols from /proc/kallsyms: Build id "
354 << "mismatch";
Yabin Cui04c70a62015-08-04 14:48:39 -0700355 return false;
356 }
Yabin Cui39d3cae2015-07-13 16:23:13 -0700357 }
Yabin Cui8a52e972015-10-01 11:32:44 -0700358
Yabin Cuib4212972016-05-25 14:08:05 -0700359 std::string kallsyms;
360 if (!android::base::ReadFileToString("/proc/kallsyms", &kallsyms)) {
361 LOG(DEBUG) << "failed to read /proc/kallsyms";
362 return false;
363 }
Yabin Cui767dd172016-06-02 21:02:43 -0700364 ProcessKernelSymbols(kallsyms, std::bind(&KernelSymbolCallback,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700365 std::placeholders::_1, &symbols_));
Yabin Cuib4212972016-05-25 14:08:05 -0700366 bool all_zero = true;
367 for (const auto& symbol : symbols_) {
Yabin Cui8a52e972015-10-01 11:32:44 -0700368 if (symbol.addr != 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700369 all_zero = false;
Yabin Cui8a52e972015-10-01 11:32:44 -0700370 break;
371 }
372 }
Yabin Cuib4212972016-05-25 14:08:05 -0700373 if (all_zero) {
374 LOG(WARNING) << "Symbol addresses in /proc/kallsyms are all zero. "
Yabin Cui6965d422016-06-15 11:41:42 -0700375 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
Yabin Cui8a52e972015-10-01 11:32:44 -0700376 symbols_.clear();
377 return false;
378 }
Yabin Cui638c5582015-07-01 16:16:57 -0700379 }
Yabin Cui04c70a62015-08-04 14:48:39 -0700380 return true;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700381}
382
Yabin Cuic5b4a312016-10-24 13:38:38 -0700383static void ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol,
384 bool (*filter)(const ElfFileSymbol&),
385 std::vector<Symbol>* symbols) {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700386 if (filter(elf_symbol)) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700387 symbols->emplace_back(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len);
Yabin Cuiec12ed92015-06-08 10:38:10 -0700388 }
389}
390
391static bool SymbolFilterForKernelModule(const ElfFileSymbol& elf_symbol) {
392 // TODO: Parse symbol outside of .text section.
393 return (elf_symbol.is_func && elf_symbol.is_in_text_section);
394}
395
Yabin Cuic8485602015-08-20 15:04:39 -0700396bool Dso::LoadKernelModule() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700397 BuildId build_id = GetExpectedBuildId();
Yabin Cuidec43c12016-07-29 16:40:40 -0700398 ElfStatus result = ParseSymbolsFromElfFile(GetDebugFilePath(), build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700399 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
400 SymbolFilterForKernelModule, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700401 return CheckReadSymbolResult(result, GetDebugFilePath());
Yabin Cuiec12ed92015-06-08 10:38:10 -0700402}
403
404static bool SymbolFilterForDso(const ElfFileSymbol& elf_symbol) {
Yabin Cui767dd172016-06-02 21:02:43 -0700405 return elf_symbol.is_func ||
406 (elf_symbol.is_label && elf_symbol.is_in_text_section);
Yabin Cuib3783552015-06-11 11:15:42 -0700407}
408
Yabin Cuic8485602015-08-20 15:04:39 -0700409bool Dso::LoadElfFile() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700410 BuildId build_id = GetExpectedBuildId();
Yabin Cui547c60e2015-10-12 16:56:05 -0700411
412 if (symfs_dir_.empty()) {
413 // Linux host can store debug shared libraries in /usr/lib/debug.
Yabin Cuidec43c12016-07-29 16:40:40 -0700414 ElfStatus result = ParseSymbolsFromElfFile(
Yabin Cui547c60e2015-10-12 16:56:05 -0700415 "/usr/lib/debug" + path_, build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700416 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
417 SymbolFilterForDso, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700418 if (result == ElfStatus::NO_ERROR) {
Yabin Cuicb6c9012016-08-25 10:46:40 -0700419 return CheckReadSymbolResult(result, "/usr/lib/debug" + path_);
Yabin Cuidec43c12016-07-29 16:40:40 -0700420 }
Yabin Cui547c60e2015-10-12 16:56:05 -0700421 }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700422 // TODO: load std::vector<Symbol> directly from ParseSymbolsFromElfFile
423 // instead of needing to call a callback function for each symbol.
Yabin Cuidec43c12016-07-29 16:40:40 -0700424 ElfStatus result = ParseSymbolsFromElfFile(
425 GetDebugFilePath(), build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700426 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
427 SymbolFilterForDso, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700428 return CheckReadSymbolResult(result, GetDebugFilePath());
Yabin Cuiec12ed92015-06-08 10:38:10 -0700429}
Yabin Cui638c5582015-07-01 16:16:57 -0700430
Yabin Cuib1a885b2016-02-14 19:18:02 -0800431bool Dso::LoadEmbeddedElfFile() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700432 BuildId build_id = GetExpectedBuildId();
Yabin Cuidec43c12016-07-29 16:40:40 -0700433 auto tuple = SplitUrlInApk(GetDebugFilePath());
Yabin Cuib1a885b2016-02-14 19:18:02 -0800434 CHECK(std::get<0>(tuple));
Yabin Cuidec43c12016-07-29 16:40:40 -0700435 ElfStatus result = ParseSymbolsFromApkFile(
Yabin Cui767dd172016-06-02 21:02:43 -0700436 std::get<1>(tuple), std::get<2>(tuple), build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700437 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
438 SymbolFilterForDso, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700439 return CheckReadSymbolResult(result, GetDebugFilePath());
Yabin Cuib1a885b2016-02-14 19:18:02 -0800440}
441
Yabin Cuic8485602015-08-20 15:04:39 -0700442void Dso::FixupSymbolLength() {
443 Symbol* prev_symbol = nullptr;
444 for (auto& symbol : symbols_) {
445 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700446 prev_symbol->len = symbol.addr - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700447 }
Yabin Cui767dd172016-06-02 21:02:43 -0700448 prev_symbol = const_cast<Symbol*>(&symbol);
Yabin Cui638c5582015-07-01 16:16:57 -0700449 }
Yabin Cuic8485602015-08-20 15:04:39 -0700450 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Chih-Hung Hsieh7d6c8ab2016-04-15 16:12:03 -0700451 prev_symbol->len = std::numeric_limits<uint64_t>::max() - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700452 }
Yabin Cui638c5582015-07-01 16:16:57 -0700453}
Yabin Cui767dd172016-06-02 21:02:43 -0700454
455const char* DsoTypeToString(DsoType dso_type) {
456 switch (dso_type) {
457 case DSO_KERNEL:
458 return "dso_kernel";
459 case DSO_KERNEL_MODULE:
460 return "dso_kernel_module";
461 case DSO_ELF_FILE:
462 return "dso_elf_file";
463 default:
464 return "unknown";
465 }
466}