blob: f31d4c4b5d71c1a99a3c2b6a19f216f52bdde424 [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),
41 has_dumped_(false) {}
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 Cuiba50c4b2015-07-21 11:24:48 -070061
Yabin Cui767dd172016-06-02 21:02:43 -070062void Dso::SetDemangle(bool demangle) { demangle_ = demangle; }
Yabin Cuib3783552015-06-11 11:15:42 -070063
Yabin Cui767dd172016-06-02 21:02:43 -070064extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n,
65 int* status);
Yabin Cuib10a8fb2015-08-18 16:32:18 -070066
Yabin Cuic8485602015-08-20 15:04:39 -070067std::string Dso::Demangle(const std::string& name) {
Yabin Cuib10a8fb2015-08-18 16:32:18 -070068 if (!demangle_) {
69 return name;
70 }
71 int status;
72 bool is_linker_symbol = (name.find(linker_prefix) == 0);
73 const char* mangled_str = name.c_str();
74 if (is_linker_symbol) {
75 mangled_str += linker_prefix.size();
76 }
77 std::string result = name;
78 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
79 if (status == 0) {
80 if (is_linker_symbol) {
81 result = std::string("[linker]") + demangled_name;
82 } else {
83 result = demangled_name;
84 }
85 free(demangled_name);
86 } else if (is_linker_symbol) {
87 result = std::string("[linker]") + mangled_str;
88 }
89 return result;
90}
91
Yabin Cuic8485602015-08-20 15:04:39 -070092bool Dso::SetSymFsDir(const std::string& symfs_dir) {
93 std::string dirname = symfs_dir;
94 if (!dirname.empty()) {
95 if (dirname.back() != '/') {
96 dirname.push_back('/');
97 }
98 std::vector<std::string> files;
99 std::vector<std::string> subdirs;
100 GetEntriesInDir(symfs_dir, &files, &subdirs);
101 if (files.empty() && subdirs.empty()) {
102 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'";
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700103 return false;
Yabin Cuic8485602015-08-20 15:04:39 -0700104 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700105 }
Yabin Cuic8485602015-08-20 15:04:39 -0700106 symfs_dir_ = dirname;
107 return true;
108}
109
Yabin Cui767dd172016-06-02 21:02:43 -0700110void Dso::SetVmlinux(const std::string& vmlinux) { vmlinux_ = vmlinux; }
Yabin Cuic8485602015-08-20 15:04:39 -0700111
Yabin Cui767dd172016-06-02 21:02:43 -0700112void Dso::SetBuildIds(
113 const std::vector<std::pair<std::string, BuildId>>& build_ids) {
Yabin Cuic8485602015-08-20 15:04:39 -0700114 std::unordered_map<std::string, BuildId> map;
115 for (auto& pair : build_ids) {
Yabin Cui767dd172016-06-02 21:02:43 -0700116 LOG(DEBUG) << "build_id_map: " << pair.first << ", "
117 << pair.second.ToString();
Yabin Cuic8485602015-08-20 15:04:39 -0700118 map.insert(pair);
119 }
120 build_id_map_ = std::move(map);
121}
122
Yabin Cuieec606c2016-07-07 13:53:33 -0700123BuildId Dso::GetExpectedBuildId() {
124 auto it = build_id_map_.find(path_);
Yabin Cuic8485602015-08-20 15:04:39 -0700125 if (it != build_id_map_.end()) {
126 return it->second;
127 }
128 return BuildId();
129}
130
Yabin Cui767dd172016-06-02 21:02:43 -0700131std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type,
132 const std::string& dso_path) {
133 static uint64_t id = 0;
134 return std::unique_ptr<Dso>(new Dso(dso_type, ++id, dso_path));
Yabin Cuic8485602015-08-20 15:04:39 -0700135}
136
Yabin Cui767dd172016-06-02 21:02:43 -0700137Dso::Dso(DsoType type, uint64_t id, const std::string& path)
138 : type_(type),
139 id_(id),
140 path_(path),
Yabin Cuieec606c2016-07-07 13:53:33 -0700141 debug_file_path_(path),
Yabin Cui767dd172016-06-02 21:02:43 -0700142 min_vaddr_(std::numeric_limits<uint64_t>::max()),
143 is_loaded_(false),
144 has_dumped_(false) {
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 Cuicc2e59e2015-08-21 14:23:43 -0700175 }
176}
177
Yabin Cui547c60e2015-10-12 16:56:05 -0700178const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
Yabin Cuic8485602015-08-20 15:04:39 -0700179 if (!is_loaded_) {
180 is_loaded_ = true;
Yabin Cui767dd172016-06-02 21:02:43 -0700181 // If symbols has been read from SymbolRecords, no need to load them from
182 // dso.
183 if (symbols_.empty()) {
184 if (!Load()) {
185 LOG(DEBUG) << "failed to load dso: " << path_;
186 return nullptr;
187 }
Yabin Cuic8485602015-08-20 15:04:39 -0700188 }
189 }
Yabin Cui767dd172016-06-02 21:02:43 -0700190 if (symbols_.empty()) {
191 return nullptr;
192 }
Yabin Cuic8485602015-08-20 15:04:39 -0700193
Yabin Cui767dd172016-06-02 21:02:43 -0700194 auto it = symbols_.upper_bound(Symbol("", vaddr_in_dso, 0));
Yabin Cuic8485602015-08-20 15:04:39 -0700195 if (it != symbols_.begin()) {
196 --it;
Yabin Cui547c60e2015-10-12 16:56:05 -0700197 if (it->addr <= vaddr_in_dso && it->addr + it->len > vaddr_in_dso) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700198 return &*it;
Yabin Cuic8485602015-08-20 15:04:39 -0700199 }
200 }
201 return nullptr;
202}
203
Yabin Cui547c60e2015-10-12 16:56:05 -0700204uint64_t Dso::MinVirtualAddress() {
205 if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
206 min_vaddr_ = 0;
207 if (type_ == DSO_ELF_FILE) {
Yabin Cuieec606c2016-07-07 13:53:33 -0700208 BuildId build_id = GetExpectedBuildId();
Yabin Cui547c60e2015-10-12 16:56:05 -0700209
210 uint64_t addr;
Yabin Cuieec606c2016-07-07 13:53:33 -0700211 if (ReadMinExecutableVirtualAddressFromElfFile(GetDebugFilePath(),
Yabin Cui767dd172016-06-02 21:02:43 -0700212 build_id, &addr)) {
Yabin Cui547c60e2015-10-12 16:56:05 -0700213 min_vaddr_ = addr;
214 }
215 }
216 }
217 return min_vaddr_;
218}
219
Yabin Cuic8485602015-08-20 15:04:39 -0700220bool Dso::Load() {
221 bool result = false;
222 switch (type_) {
223 case DSO_KERNEL:
224 result = LoadKernel();
225 break;
226 case DSO_KERNEL_MODULE:
227 result = LoadKernelModule();
228 break;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800229 case DSO_ELF_FILE: {
230 if (std::get<0>(SplitUrlInApk(path_))) {
231 result = LoadEmbeddedElfFile();
232 } else {
233 result = LoadElfFile();
234 }
Yabin Cuic8485602015-08-20 15:04:39 -0700235 break;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800236 }
Yabin Cuic8485602015-08-20 15:04:39 -0700237 }
238 if (result) {
239 FixupSymbolLength();
240 }
241 return result;
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700242}
243
Yabin Cuiec12ed92015-06-08 10:38:10 -0700244static bool IsKernelFunctionSymbol(const KernelSymbol& symbol) {
Yabin Cui767dd172016-06-02 21:02:43 -0700245 return (symbol.type == 'T' || symbol.type == 't' || symbol.type == 'W' ||
246 symbol.type == 'w');
Yabin Cuiec12ed92015-06-08 10:38:10 -0700247}
248
Yabin Cuieec606c2016-07-07 13:53:33 -0700249static bool KernelSymbolCallback(const KernelSymbol& kernel_symbol, Dso* dso) {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700250 if (IsKernelFunctionSymbol(kernel_symbol)) {
Yabin Cuic8485602015-08-20 15:04:39 -0700251 dso->InsertSymbol(Symbol(kernel_symbol.name, kernel_symbol.addr, 0));
Yabin Cuiec12ed92015-06-08 10:38:10 -0700252 }
253 return false;
254}
255
Yabin Cuieec606c2016-07-07 13:53:33 -0700256static void VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso) {
Yabin Cui39d3cae2015-07-13 16:23:13 -0700257 if (elf_symbol.is_func) {
Yabin Cui767dd172016-06-02 21:02:43 -0700258 dso->InsertSymbol(
259 Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
Yabin Cui39d3cae2015-07-13 16:23:13 -0700260 }
261}
262
Yabin Cuic8485602015-08-20 15:04:39 -0700263bool Dso::LoadKernel() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700264 BuildId build_id = GetExpectedBuildId();
Yabin Cui39d3cae2015-07-13 16:23:13 -0700265 if (!vmlinux_.empty()) {
Yabin Cui767dd172016-06-02 21:02:43 -0700266 ParseSymbolsFromElfFile(
267 vmlinux_, build_id,
268 std::bind(VmlinuxSymbolCallback, std::placeholders::_1, this));
Yabin Cuib4212972016-05-25 14:08:05 -0700269 } else if (!kallsyms_.empty()) {
Yabin Cui767dd172016-06-02 21:02:43 -0700270 ProcessKernelSymbols(kallsyms_, std::bind(&KernelSymbolCallback,
271 std::placeholders::_1, this));
Yabin Cuib4212972016-05-25 14:08:05 -0700272 bool all_zero = true;
273 for (const auto& symbol : symbols_) {
274 if (symbol.addr != 0) {
275 all_zero = false;
276 break;
277 }
278 }
279 if (all_zero) {
Yabin Cui767dd172016-06-02 21:02:43 -0700280 LOG(WARNING)
281 << "Symbol addresses in /proc/kallsyms on device are all zero. "
Yabin Cui6965d422016-06-15 11:41:42 -0700282 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
Yabin Cuib4212972016-05-25 14:08:05 -0700283 symbols_.clear();
284 return false;
285 }
Yabin Cui39d3cae2015-07-13 16:23:13 -0700286 } else {
Yabin Cui04c70a62015-08-04 14:48:39 -0700287 if (!build_id.IsEmpty()) {
288 BuildId real_build_id;
289 GetKernelBuildId(&real_build_id);
290 bool match = (build_id == real_build_id);
Yabin Cuieec606c2016-07-07 13:53:33 -0700291 LOG(WARNING) << "check kernel build id ("
292 << (match ? "match" : "mismatch") << "): expected "
293 << build_id.ToString() << ", real "
294 << real_build_id.ToString();
Yabin Cui04c70a62015-08-04 14:48:39 -0700295 if (!match) {
296 return false;
297 }
Yabin Cui39d3cae2015-07-13 16:23:13 -0700298 }
Yabin Cui8a52e972015-10-01 11:32:44 -0700299
Yabin Cuib4212972016-05-25 14:08:05 -0700300 std::string kallsyms;
301 if (!android::base::ReadFileToString("/proc/kallsyms", &kallsyms)) {
302 LOG(DEBUG) << "failed to read /proc/kallsyms";
303 return false;
304 }
Yabin Cui767dd172016-06-02 21:02:43 -0700305 ProcessKernelSymbols(kallsyms, std::bind(&KernelSymbolCallback,
306 std::placeholders::_1, this));
Yabin Cuib4212972016-05-25 14:08:05 -0700307 bool all_zero = true;
308 for (const auto& symbol : symbols_) {
Yabin Cui8a52e972015-10-01 11:32:44 -0700309 if (symbol.addr != 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700310 all_zero = false;
Yabin Cui8a52e972015-10-01 11:32:44 -0700311 break;
312 }
313 }
Yabin Cuib4212972016-05-25 14:08:05 -0700314 if (all_zero) {
315 LOG(WARNING) << "Symbol addresses in /proc/kallsyms are all zero. "
Yabin Cui6965d422016-06-15 11:41:42 -0700316 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
Yabin Cui8a52e972015-10-01 11:32:44 -0700317 symbols_.clear();
318 return false;
319 }
Yabin Cui638c5582015-07-01 16:16:57 -0700320 }
Yabin Cui04c70a62015-08-04 14:48:39 -0700321 return true;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700322}
323
Yabin Cuieec606c2016-07-07 13:53:33 -0700324static void ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso,
325 bool (*filter)(const ElfFileSymbol&)) {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700326 if (filter(elf_symbol)) {
Yabin Cui767dd172016-06-02 21:02:43 -0700327 dso->InsertSymbol(
328 Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
Yabin Cuiec12ed92015-06-08 10:38:10 -0700329 }
330}
331
332static bool SymbolFilterForKernelModule(const ElfFileSymbol& elf_symbol) {
333 // TODO: Parse symbol outside of .text section.
334 return (elf_symbol.is_func && elf_symbol.is_in_text_section);
335}
336
Yabin Cuic8485602015-08-20 15:04:39 -0700337bool Dso::LoadKernelModule() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700338 BuildId build_id = GetExpectedBuildId();
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700339 ParseSymbolsFromElfFile(
Yabin Cuic8485602015-08-20 15:04:39 -0700340 symfs_dir_ + path_, build_id,
Yabin Cui767dd172016-06-02 21:02:43 -0700341 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this,
342 SymbolFilterForKernelModule));
Yabin Cui04c70a62015-08-04 14:48:39 -0700343 return true;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700344}
345
346static bool SymbolFilterForDso(const ElfFileSymbol& elf_symbol) {
Yabin Cui767dd172016-06-02 21:02:43 -0700347 return elf_symbol.is_func ||
348 (elf_symbol.is_label && elf_symbol.is_in_text_section);
Yabin Cuib3783552015-06-11 11:15:42 -0700349}
350
Yabin Cuic8485602015-08-20 15:04:39 -0700351bool Dso::LoadElfFile() {
Yabin Cui547c60e2015-10-12 16:56:05 -0700352 bool loaded = false;
Yabin Cuieec606c2016-07-07 13:53:33 -0700353 BuildId build_id = GetExpectedBuildId();
Yabin Cui547c60e2015-10-12 16:56:05 -0700354
355 if (symfs_dir_.empty()) {
356 // Linux host can store debug shared libraries in /usr/lib/debug.
357 loaded = ParseSymbolsFromElfFile(
358 "/usr/lib/debug" + path_, build_id,
Yabin Cui767dd172016-06-02 21:02:43 -0700359 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this,
360 SymbolFilterForDso));
Yabin Cui547c60e2015-10-12 16:56:05 -0700361 }
362 if (!loaded) {
363 loaded = ParseSymbolsFromElfFile(
Yabin Cuieec606c2016-07-07 13:53:33 -0700364 GetDebugFilePath(), build_id,
Yabin Cui767dd172016-06-02 21:02:43 -0700365 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this,
366 SymbolFilterForDso));
Yabin Cui547c60e2015-10-12 16:56:05 -0700367 }
368 return loaded;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700369}
Yabin Cui638c5582015-07-01 16:16:57 -0700370
Yabin Cuib1a885b2016-02-14 19:18:02 -0800371bool Dso::LoadEmbeddedElfFile() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700372 std::string path = GetDebugFilePath();
373 BuildId build_id = GetExpectedBuildId();
Yabin Cuib1a885b2016-02-14 19:18:02 -0800374 auto tuple = SplitUrlInApk(path);
375 CHECK(std::get<0>(tuple));
Yabin Cui767dd172016-06-02 21:02:43 -0700376 return ParseSymbolsFromApkFile(
377 std::get<1>(tuple), std::get<2>(tuple), build_id,
378 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this,
379 SymbolFilterForDso));
Yabin Cuib1a885b2016-02-14 19:18:02 -0800380}
381
Yabin Cui767dd172016-06-02 21:02:43 -0700382void Dso::InsertSymbol(const Symbol& symbol) { symbols_.insert(symbol); }
Yabin Cuic8485602015-08-20 15:04:39 -0700383
384void Dso::FixupSymbolLength() {
385 Symbol* prev_symbol = nullptr;
386 for (auto& symbol : symbols_) {
387 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700388 prev_symbol->len = symbol.addr - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700389 }
Yabin Cui767dd172016-06-02 21:02:43 -0700390 prev_symbol = const_cast<Symbol*>(&symbol);
Yabin Cui638c5582015-07-01 16:16:57 -0700391 }
Yabin Cuic8485602015-08-20 15:04:39 -0700392 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Chih-Hung Hsieh7d6c8ab2016-04-15 16:12:03 -0700393 prev_symbol->len = std::numeric_limits<uint64_t>::max() - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700394 }
Yabin Cui638c5582015-07-01 16:16:57 -0700395}
Yabin Cui767dd172016-06-02 21:02:43 -0700396
397const char* DsoTypeToString(DsoType dso_type) {
398 switch (dso_type) {
399 case DSO_KERNEL:
400 return "dso_kernel";
401 case DSO_KERNEL_MODULE:
402 return "dso_kernel_module";
403 case DSO_ELF_FILE:
404 return "dso_elf_file";
405 default:
406 return "unknown";
407 }
408}