blob: 8e641a89ff33116324d0ed23512a45e55bb65864 [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 Cuidd401b32018-04-11 11:17:06 -070024#include <memory>
Yabin Cuicc2e59e2015-08-21 14:23:43 -070025#include <vector>
Yabin Cuic8485602015-08-20 15:04:39 -070026
Yabin Cuib4212972016-05-25 14:08:05 -070027#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080028#include <android-base/logging.h>
Yabin Cui40b70ff2018-04-09 14:06:08 -070029#include <android-base/strings.h>
Yabin Cuic8485602015-08-20 15:04:39 -070030
Yabin Cuiec12ed92015-06-08 10:38:10 -070031#include "environment.h"
Yabin Cuib1a885b2016-02-14 19:18:02 -080032#include "read_apk.h"
Yabin Cui516a87c2018-03-26 17:34:00 -070033#include "read_dex_file.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070034#include "read_elf.h"
Yabin Cuib3783552015-06-11 11:15:42 -070035#include "utils.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070036
Yabin Cui40b70ff2018-04-09 14:06:08 -070037namespace simpleperf_dso_impl {
38
39void DebugElfFileFinder::Reset() {
40 vdso_64bit_.clear();
41 vdso_32bit_.clear();
42 symfs_dir_.clear();
43 build_id_to_file_map_.clear();
44}
45
46bool DebugElfFileFinder::SetSymFsDir(const std::string& symfs_dir) {
47 std::string dirname = symfs_dir;
48 if (!dirname.empty()) {
49 if (dirname.back() != '/') {
50 dirname.push_back('/');
51 }
52 if (!IsDir(symfs_dir)) {
53 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'";
54 return false;
55 }
56 }
57 symfs_dir_ = dirname;
Yabin Cui40b70ff2018-04-09 14:06:08 -070058 std::string build_id_list_file = symfs_dir_ + "build_id_list";
59 std::string build_id_list;
60 if (android::base::ReadFileToString(build_id_list_file, &build_id_list)) {
61 for (auto& line : android::base::Split(build_id_list, "\n")) {
Yabin Cui2969a9e2018-04-19 17:06:24 -070062 std::vector<std::string> items = android::base::Split(line, "=");
Yabin Cui40b70ff2018-04-09 14:06:08 -070063 if (items.size() == 2u) {
Yabin Cui3939b9d2018-07-20 17:12:13 -070064 build_id_to_file_map_[items[0]] = symfs_dir_ + items[1];
Yabin Cui40b70ff2018-04-09 14:06:08 -070065 }
66 }
67 }
68 return true;
69}
70
Yabin Cui3939b9d2018-07-20 17:12:13 -070071bool DebugElfFileFinder::AddSymbolDir(const std::string& symbol_dir) {
72 if (!IsDir(symbol_dir)) {
73 LOG(ERROR) << "Invalid symbol dir " << symbol_dir;
74 return false;
75 }
76 std::string dir = symbol_dir;
77 if (dir.size() > 1 && dir.back() == '/') {
78 dir.pop_back();
79 }
80 CollectBuildIdInDir(dir);
81 return true;
82}
83
84void DebugElfFileFinder::CollectBuildIdInDir(const std::string& dir) {
85 for (const std::string& entry : GetEntriesInDir(dir)) {
86 std::string path = dir + "/" + entry;
87 if (IsDir(path)) {
88 CollectBuildIdInDir(path);
89 } else {
90 BuildId build_id;
91 if (GetBuildIdFromElfFile(path, &build_id) == ElfStatus::NO_ERROR) {
92 build_id_to_file_map_[build_id.ToString()] = path;
93 }
94 }
95 }
96}
97
Yabin Cui40b70ff2018-04-09 14:06:08 -070098void DebugElfFileFinder::SetVdsoFile(const std::string& vdso_file, bool is_64bit) {
99 if (is_64bit) {
100 vdso_64bit_ = vdso_file;
101 } else {
102 vdso_32bit_ = vdso_file;
103 }
104}
105
106std::string DebugElfFileFinder::FindDebugFile(const std::string& dso_path, bool force_64bit,
107 BuildId& build_id) {
108 if (dso_path == "[vdso]") {
109 if (force_64bit && !vdso_64bit_.empty()) {
110 return vdso_64bit_;
111 } else if (!force_64bit && !vdso_32bit_.empty()) {
112 return vdso_32bit_;
113 }
Yabin Cui3939b9d2018-07-20 17:12:13 -0700114 }
115 // 1. Try build_id_to_file_map.
116 if (!build_id_to_file_map_.empty()) {
Yabin Cui40b70ff2018-04-09 14:06:08 -0700117 if (!build_id.IsEmpty() || GetBuildIdFromDsoPath(dso_path, &build_id)) {
Yabin Cui40b70ff2018-04-09 14:06:08 -0700118 auto it = build_id_to_file_map_.find(build_id.ToString());
119 if (it != build_id_to_file_map_.end()) {
Yabin Cui3939b9d2018-07-20 17:12:13 -0700120 return it->second;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700121 }
122 }
123 }
Yabin Cui3939b9d2018-07-20 17:12:13 -0700124 auto check_path = [&](const std::string& path) {
125 BuildId debug_build_id;
126 if (GetBuildIdFromDsoPath(path, &debug_build_id)) {
127 if (!build_id.IsEmpty() || GetBuildIdFromDsoPath(dso_path, &build_id)) {
128 if (build_id == debug_build_id) {
129 return true;
130 }
131 }
132 }
133 return false;
134 };
135
136 // 2. Try concatenating symfs_dir and dso_path.
137 if (!symfs_dir_.empty() && check_path(symfs_dir_ + dso_path)) {
138 return symfs_dir_ + dso_path;
139 }
140 // 3. Try concatenating /usr/lib/debug and dso_path.
141 // Linux host can store debug shared libraries in /usr/lib/debug.
142 if (check_path("/usr/lib/debug" + dso_path)) {
143 return "/usr/lib/debug" + dso_path;
144 }
Yabin Cui40b70ff2018-04-09 14:06:08 -0700145 return dso_path;
146}
147} // namespace simpleperf_dso_imp
148
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700149static OneTimeFreeAllocator symbol_name_allocator;
150
151Symbol::Symbol(const std::string& name, uint64_t addr, uint64_t len)
152 : addr(addr),
153 len(len),
154 name_(symbol_name_allocator.AllocateString(name)),
Yabin Cui767dd172016-06-02 21:02:43 -0700155 demangled_name_(nullptr),
Yabin Cui516a87c2018-03-26 17:34:00 -0700156 dump_id_(UINT_MAX) {
157}
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700158
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700159const char* Symbol::DemangledName() const {
160 if (demangled_name_ == nullptr) {
161 const std::string s = Dso::Demangle(name_);
162 if (s == name_) {
163 demangled_name_ = name_;
164 } else {
165 demangled_name_ = symbol_name_allocator.AllocateString(s);
166 }
167 }
168 return demangled_name_;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700169}
170
Yabin Cuic8485602015-08-20 15:04:39 -0700171bool Dso::demangle_ = true;
Yabin Cuic8485602015-08-20 15:04:39 -0700172std::string Dso::vmlinux_;
Yabin Cuib4212972016-05-25 14:08:05 -0700173std::string Dso::kallsyms_;
Yabin Cuia9392452017-01-12 18:07:27 -0800174bool Dso::read_kernel_symbols_from_proc_;
Yabin Cuic8485602015-08-20 15:04:39 -0700175std::unordered_map<std::string, BuildId> Dso::build_id_map_;
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700176size_t Dso::dso_count_;
Yabin Cui16501ff2016-10-19 15:06:29 -0700177uint32_t Dso::g_dump_id_;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700178simpleperf_dso_impl::DebugElfFileFinder Dso::debug_elf_file_finder_;
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700179
Yabin Cui767dd172016-06-02 21:02:43 -0700180void Dso::SetDemangle(bool demangle) { demangle_ = demangle; }
Yabin Cuib3783552015-06-11 11:15:42 -0700181
Yabin Cui767dd172016-06-02 21:02:43 -0700182extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n,
183 int* status);
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700184
Yabin Cuic8485602015-08-20 15:04:39 -0700185std::string Dso::Demangle(const std::string& name) {
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700186 if (!demangle_) {
187 return name;
188 }
189 int status;
190 bool is_linker_symbol = (name.find(linker_prefix) == 0);
191 const char* mangled_str = name.c_str();
192 if (is_linker_symbol) {
193 mangled_str += linker_prefix.size();
194 }
195 std::string result = name;
196 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
197 if (status == 0) {
198 if (is_linker_symbol) {
199 result = std::string("[linker]") + demangled_name;
200 } else {
201 result = demangled_name;
202 }
203 free(demangled_name);
204 } else if (is_linker_symbol) {
205 result = std::string("[linker]") + mangled_str;
206 }
207 return result;
208}
209
Yabin Cuic8485602015-08-20 15:04:39 -0700210bool Dso::SetSymFsDir(const std::string& symfs_dir) {
Yabin Cui40b70ff2018-04-09 14:06:08 -0700211 return debug_elf_file_finder_.SetSymFsDir(symfs_dir);
Yabin Cuic8485602015-08-20 15:04:39 -0700212}
213
Yabin Cui3939b9d2018-07-20 17:12:13 -0700214bool Dso::AddSymbolDir(const std::string& symbol_dir) {
215 return debug_elf_file_finder_.AddSymbolDir(symbol_dir);
216}
217
Yabin Cui767dd172016-06-02 21:02:43 -0700218void Dso::SetVmlinux(const std::string& vmlinux) { vmlinux_ = vmlinux; }
Yabin Cuic8485602015-08-20 15:04:39 -0700219
Yabin Cui767dd172016-06-02 21:02:43 -0700220void Dso::SetBuildIds(
221 const std::vector<std::pair<std::string, BuildId>>& build_ids) {
Yabin Cuic8485602015-08-20 15:04:39 -0700222 std::unordered_map<std::string, BuildId> map;
223 for (auto& pair : build_ids) {
Yabin Cui767dd172016-06-02 21:02:43 -0700224 LOG(DEBUG) << "build_id_map: " << pair.first << ", "
225 << pair.second.ToString();
Yabin Cuic8485602015-08-20 15:04:39 -0700226 map.insert(pair);
227 }
228 build_id_map_ = std::move(map);
229}
230
Yabin Cuic68e66d2018-03-07 15:47:15 -0800231void Dso::SetVdsoFile(const std::string& vdso_file, bool is_64bit) {
Yabin Cui40b70ff2018-04-09 14:06:08 -0700232 debug_elf_file_finder_.SetVdsoFile(vdso_file, is_64bit);
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700233}
234
Yabin Cui52c63692016-11-28 17:28:08 -0800235BuildId Dso::FindExpectedBuildIdForPath(const std::string& path) {
236 auto it = build_id_map_.find(path);
Yabin Cuic8485602015-08-20 15:04:39 -0700237 if (it != build_id_map_.end()) {
238 return it->second;
239 }
240 return BuildId();
241}
242
Yabin Cui52c63692016-11-28 17:28:08 -0800243BuildId Dso::GetExpectedBuildId() {
244 return FindExpectedBuildIdForPath(path_);
245}
246
Yabin Cui516a87c2018-03-26 17:34:00 -0700247Dso::Dso(DsoType type, const std::string& path, const std::string& debug_file_path)
Yabin Cui767dd172016-06-02 21:02:43 -0700248 : type_(type),
Yabin Cui767dd172016-06-02 21:02:43 -0700249 path_(path),
Yabin Cui516a87c2018-03-26 17:34:00 -0700250 debug_file_path_(debug_file_path),
Yabin Cui767dd172016-06-02 21:02:43 -0700251 is_loaded_(false),
Yabin Cui16501ff2016-10-19 15:06:29 -0700252 dump_id_(UINT_MAX),
Yabin Cuie466d4d2017-08-11 17:03:07 -0700253 symbol_dump_id_(0),
254 symbol_warning_loglevel_(android::base::WARNING) {
Yabin Cui15475e62016-07-14 13:26:19 -0700255 size_t pos = path.find_last_of("/\\");
256 if (pos != std::string::npos) {
257 file_name_ = path.substr(pos + 1);
258 } else {
259 file_name_ = path;
260 }
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700261 dso_count_++;
Yabin Cuic8485602015-08-20 15:04:39 -0700262}
263
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700264Dso::~Dso() {
265 if (--dso_count_ == 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700266 // Clean up global variables when no longer used.
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700267 symbol_name_allocator.Clear();
Yabin Cuib4212972016-05-25 14:08:05 -0700268 demangle_ = true;
Yabin Cuib4212972016-05-25 14:08:05 -0700269 vmlinux_.clear();
270 kallsyms_.clear();
Yabin Cuia9392452017-01-12 18:07:27 -0800271 read_kernel_symbols_from_proc_ = false;
Yabin Cuib4212972016-05-25 14:08:05 -0700272 build_id_map_.clear();
Yabin Cui16501ff2016-10-19 15:06:29 -0700273 g_dump_id_ = 0;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700274 debug_elf_file_finder_.Reset();
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700275 }
276}
277
Yabin Cui16501ff2016-10-19 15:06:29 -0700278uint32_t Dso::CreateDumpId() {
279 CHECK(!HasDumpId());
280 return dump_id_ = g_dump_id_++;
281}
282
283uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
284 CHECK(!symbol->HasDumpId());
285 symbol->dump_id_ = symbol_dump_id_++;
286 return symbol->dump_id_;
287}
288
Yabin Cui547c60e2015-10-12 16:56:05 -0700289const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
Yabin Cuic8485602015-08-20 15:04:39 -0700290 if (!is_loaded_) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700291 Load();
292 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700293 auto it = std::upper_bound(symbols_.begin(), symbols_.end(),
294 Symbol("", vaddr_in_dso, 0),
295 Symbol::CompareValueByAddr);
296 if (it != symbols_.begin()) {
297 --it;
298 if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
299 return &*it;
Yabin Cuic8485602015-08-20 15:04:39 -0700300 }
301 }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700302 if (!unknown_symbols_.empty()) {
303 auto it = unknown_symbols_.find(vaddr_in_dso);
304 if (it != unknown_symbols_.end()) {
305 return &it->second;
Yabin Cuic8485602015-08-20 15:04:39 -0700306 }
307 }
308 return nullptr;
309}
310
Yabin Cuic5b4a312016-10-24 13:38:38 -0700311void Dso::SetSymbols(std::vector<Symbol>* symbols) {
312 symbols_ = std::move(*symbols);
313 symbols->clear();
314}
315
316void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) {
317 unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1)));
318}
319
Yabin Cuic5b4a312016-10-24 13:38:38 -0700320void Dso::Load() {
321 is_loaded_ = true;
Yabin Cui516a87c2018-03-26 17:34:00 -0700322 std::vector<Symbol> symbols = LoadSymbols();
323 if (symbols_.empty()) {
324 symbols_ = std::move(symbols);
Yabin Cuidec43c12016-07-29 16:40:40 -0700325 } else {
Yabin Cui516a87c2018-03-26 17:34:00 -0700326 std::vector<Symbol> merged_symbols;
327 std::set_union(symbols_.begin(), symbols_.end(), symbols.begin(), symbols.end(),
328 std::back_inserter(merged_symbols), Symbol::CompareValueByAddr);
329 symbols_ = std::move(merged_symbols);
Yabin Cuic8485602015-08-20 15:04:39 -0700330 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700331}
332
Yabin Cui516a87c2018-03-26 17:34:00 -0700333static void ReportReadElfSymbolResult(ElfStatus result, const std::string& path,
334 const std::string& debug_file_path,
335 android::base::LogSeverity warning_loglevel = android::base::WARNING) {
Yabin Cuidec43c12016-07-29 16:40:40 -0700336 if (result == ElfStatus::NO_ERROR) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700337 LOG(VERBOSE) << "Read symbols from " << debug_file_path << " successfully";
Yabin Cuidec43c12016-07-29 16:40:40 -0700338 } else if (result == ElfStatus::NO_SYMBOL_TABLE) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700339 if (path == "[vdso]") {
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700340 // Vdso only contains dynamic symbol table, and we can't change that.
Yabin Cui516a87c2018-03-26 17:34:00 -0700341 return;
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700342 }
Yabin Cuidec43c12016-07-29 16:40:40 -0700343 // Lacking symbol table isn't considered as an error but worth reporting.
Yabin Cui516a87c2018-03-26 17:34:00 -0700344 LOG(warning_loglevel) << debug_file_path << " doesn't contain symbol table";
Yabin Cuidec43c12016-07-29 16:40:40 -0700345 } else {
Yabin Cui516a87c2018-03-26 17:34:00 -0700346 LOG(warning_loglevel) << "failed to read symbols from " << debug_file_path << ": " << result;
Yabin Cuidec43c12016-07-29 16:40:40 -0700347 }
348}
349
Yabin Cui516a87c2018-03-26 17:34:00 -0700350static void SortAndFixSymbols(std::vector<Symbol>& symbols) {
351 std::sort(symbols.begin(), symbols.end(), Symbol::CompareValueByAddr);
Yabin Cuic8485602015-08-20 15:04:39 -0700352 Symbol* prev_symbol = nullptr;
Yabin Cui516a87c2018-03-26 17:34:00 -0700353 for (auto& symbol : symbols) {
Yabin Cuic8485602015-08-20 15:04:39 -0700354 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700355 prev_symbol->len = symbol.addr - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700356 }
Yabin Cui3d4aa262017-11-01 15:58:55 -0700357 prev_symbol = &symbol;
Yabin Cui638c5582015-07-01 16:16:57 -0700358 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700359}
360
Yabin Cuidd401b32018-04-11 11:17:06 -0700361class DexFileDso : public Dso {
362 public:
363 DexFileDso(const std::string& path, const std::string& debug_file_path)
364 : Dso(DSO_DEX_FILE, path, debug_file_path) {}
365
366 void AddDexFileOffset(uint64_t dex_file_offset) override {
Yabin Cuic8571d42018-06-06 11:20:39 -0700367 auto it = std::lower_bound(dex_file_offsets_.begin(), dex_file_offsets_.end(),
368 dex_file_offset);
369 if (it != dex_file_offsets_.end() && *it == dex_file_offset) {
370 return;
371 }
372 dex_file_offsets_.insert(it, dex_file_offset);
Yabin Cuidd401b32018-04-11 11:17:06 -0700373 }
374
375 const std::vector<uint64_t>* DexFileOffsets() override {
376 return &dex_file_offsets_;
377 }
378
379 std::vector<Symbol> LoadSymbols() override {
380 std::vector<Symbol> symbols;
381 std::vector<DexFileSymbol> dex_file_symbols;
Yabin Cui2a53ff32018-05-21 17:37:00 -0700382 auto tuple = SplitUrlInApk(debug_file_path_);
383 bool status = false;
384 if (std::get<0>(tuple)) {
385 std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(std::get<1>(tuple));
386 ZipEntry entry;
387 std::vector<uint8_t> data;
Yabin Cui15749e02018-05-30 16:37:06 -0700388 if (ahelper &&
389 ahelper->FindEntry(std::get<2>(tuple), &entry) && ahelper->GetEntryData(entry, &data)) {
Yabin Cui2a53ff32018-05-21 17:37:00 -0700390 status = ReadSymbolsFromDexFileInMemory(data.data(), data.size(), dex_file_offsets_,
391 &dex_file_symbols);
392 }
393 } else {
394 status = ReadSymbolsFromDexFile(debug_file_path_, dex_file_offsets_, &dex_file_symbols);
395 }
396 if (!status) {
Yabin Cuidd401b32018-04-11 11:17:06 -0700397 android::base::LogSeverity level = symbols_.empty() ? android::base::WARNING
398 : android::base::DEBUG;
399 LOG(level) << "Failed to read symbols from " << debug_file_path_;
400 return symbols;
401 }
402 LOG(VERBOSE) << "Read symbols from " << debug_file_path_ << " successfully";
403 for (auto& symbol : dex_file_symbols) {
404 symbols.emplace_back(symbol.name, symbol.offset, symbol.len);
405 }
406 SortAndFixSymbols(symbols);
407 return symbols;
408 }
409
410 private:
411 std::vector<uint64_t> dex_file_offsets_;
412};
413
Yabin Cui516a87c2018-03-26 17:34:00 -0700414class ElfDso : public Dso {
415 public:
416 ElfDso(const std::string& path, const std::string& debug_file_path)
417 : Dso(DSO_ELF_FILE, path, debug_file_path),
418 min_vaddr_(std::numeric_limits<uint64_t>::max()) {}
419
420 uint64_t MinVirtualAddress() override {
421 if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
422 min_vaddr_ = 0;
423 if (type_ == DSO_ELF_FILE) {
424 BuildId build_id = GetExpectedBuildId();
425
426 uint64_t addr;
Yabin Cui8422f342018-05-09 17:27:27 -0700427 ElfStatus result;
428 auto tuple = SplitUrlInApk(debug_file_path_);
429 if (std::get<0>(tuple)) {
430 EmbeddedElf* elf = ApkInspector::FindElfInApkByName(std::get<1>(tuple),
431 std::get<2>(tuple));
432 if (elf == nullptr) {
433 result = ElfStatus::FILE_NOT_FOUND;
434 } else {
435 result = ReadMinExecutableVirtualAddressFromEmbeddedElfFile(
436 elf->filepath(), elf->entry_offset(), elf->entry_size(), build_id, &addr);
437 }
438 } else {
439 result = ReadMinExecutableVirtualAddressFromElfFile(debug_file_path_, build_id, &addr);
440 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700441 if (result != ElfStatus::NO_ERROR) {
442 LOG(WARNING) << "failed to read min virtual address of "
443 << GetDebugFilePath() << ": " << result;
444 } else {
445 min_vaddr_ = addr;
446 }
447 }
448 }
449 return min_vaddr_;
Yabin Cuic8485602015-08-20 15:04:39 -0700450 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700451
452 void SetMinVirtualAddress(uint64_t min_vaddr) override {
453 min_vaddr_ = min_vaddr;
454 }
455
Yabin Cuidd401b32018-04-11 11:17:06 -0700456 void AddDexFileOffset(uint64_t dex_file_offset) override {
457 if (type_ == DSO_ELF_FILE) {
458 // When simpleperf does unwinding while recording, it processes mmap records before reading
459 // dex file linked list (via JITDebugReader). To process mmap records, it creates Dso
460 // objects of type ELF_FILE. Then after reading dex file linked list, it realizes some
461 // ELF_FILE Dso objects should actually be DEX_FILE, because they have dex file offsets.
462 // So here converts ELF_FILE Dso into DEX_FILE Dso.
463 type_ = DSO_DEX_FILE;
464 dex_file_dso_.reset(new DexFileDso(path_, path_));
465 }
466 dex_file_dso_->AddDexFileOffset(dex_file_offset);
467 }
468
469 const std::vector<uint64_t>* DexFileOffsets() override {
470 return dex_file_dso_ ? dex_file_dso_->DexFileOffsets() : nullptr;
471 }
472
Yabin Cui516a87c2018-03-26 17:34:00 -0700473 protected:
474 std::vector<Symbol> LoadSymbols() override {
Yabin Cuidd401b32018-04-11 11:17:06 -0700475 if (dex_file_dso_) {
476 return dex_file_dso_->LoadSymbols();
477 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700478 std::vector<Symbol> symbols;
479 BuildId build_id = GetExpectedBuildId();
480 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
481 if (symbol.is_func || (symbol.is_label && symbol.is_in_text_section)) {
482 symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
483 }
484 };
485 ElfStatus status;
486 std::tuple<bool, std::string, std::string> tuple = SplitUrlInApk(debug_file_path_);
487 if (std::get<0>(tuple)) {
Yabin Cui8422f342018-05-09 17:27:27 -0700488 EmbeddedElf* elf = ApkInspector::FindElfInApkByName(std::get<1>(tuple), std::get<2>(tuple));
489 if (elf == nullptr) {
490 status = ElfStatus::FILE_NOT_FOUND;
491 } else {
492 status = ParseSymbolsFromEmbeddedElfFile(elf->filepath(), elf->entry_offset(),
493 elf->entry_size(), build_id, symbol_callback);
494 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700495 } else {
496 status = ParseSymbolsFromElfFile(debug_file_path_, build_id, symbol_callback);
497 }
498 ReportReadElfSymbolResult(status, path_, debug_file_path_,
499 symbols_.empty() ? android::base::WARNING : android::base::DEBUG);
500 SortAndFixSymbols(symbols);
501 return symbols;
502 }
503
504 private:
505 uint64_t min_vaddr_;
Yabin Cuidd401b32018-04-11 11:17:06 -0700506 std::unique_ptr<DexFileDso> dex_file_dso_;
Yabin Cui516a87c2018-03-26 17:34:00 -0700507};
508
509class KernelDso : public Dso {
510 public:
511 KernelDso(const std::string& path, const std::string& debug_file_path)
512 : Dso(DSO_KERNEL, path, debug_file_path) {}
513
514 protected:
515 std::vector<Symbol> LoadSymbols() override {
516 std::vector<Symbol> symbols;
517 BuildId build_id = GetExpectedBuildId();
518 if (!vmlinux_.empty()) {
519 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
520 if (symbol.is_func) {
521 symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
522 }
523 };
524 ElfStatus status = ParseSymbolsFromElfFile(vmlinux_, build_id, symbol_callback);
525 ReportReadElfSymbolResult(status, path_, vmlinux_);
526 } else if (!kallsyms_.empty()) {
527 symbols = ReadSymbolsFromKallsyms(kallsyms_);
528 } else if (read_kernel_symbols_from_proc_ || !build_id.IsEmpty()) {
529 // Try /proc/kallsyms only when asked to do so, or when build id matches.
530 // Otherwise, it is likely to use /proc/kallsyms on host for perf.data recorded on device.
531 bool can_read_kallsyms = true;
532 if (!build_id.IsEmpty()) {
533 BuildId real_build_id;
534 if (!GetKernelBuildId(&real_build_id) || build_id != real_build_id) {
535 LOG(DEBUG) << "failed to read symbols from /proc/kallsyms: Build id mismatch";
536 can_read_kallsyms = false;
537 }
538 }
539 if (can_read_kallsyms) {
540 std::string kallsyms;
541 if (!android::base::ReadFileToString("/proc/kallsyms", &kallsyms)) {
542 LOG(DEBUG) << "failed to read /proc/kallsyms";
543 } else {
544 symbols = ReadSymbolsFromKallsyms(kallsyms);
545 }
546 }
547 }
548 SortAndFixSymbols(symbols);
549 if (!symbols.empty()) {
550 symbols.back().len = std::numeric_limits<uint64_t>::max() - symbols.back().addr;
551 }
552 return symbols;
553 }
554
555 private:
556 std::vector<Symbol> ReadSymbolsFromKallsyms(std::string& kallsyms) {
557 std::vector<Symbol> symbols;
558 auto symbol_callback = [&](const KernelSymbol& symbol) {
559 if (strchr("TtWw", symbol.type) && symbol.addr != 0u) {
560 symbols.emplace_back(symbol.name, symbol.addr, 0);
561 }
562 return false;
563 };
564 ProcessKernelSymbols(kallsyms, symbol_callback);
565 if (symbols.empty()) {
566 LOG(WARNING) << "Symbol addresses in /proc/kallsyms on device are all zero. "
567 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
568 }
569 return symbols;
570 }
571};
572
573class KernelModuleDso : public Dso {
574 public:
575 KernelModuleDso(const std::string& path, const std::string& debug_file_path)
576 : Dso(DSO_KERNEL_MODULE, path, debug_file_path) {}
577
578 protected:
579 std::vector<Symbol> LoadSymbols() override {
580 std::vector<Symbol> symbols;
581 BuildId build_id = GetExpectedBuildId();
582 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
583 if (symbol.is_func || symbol.is_in_text_section) {
584 symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
585 }
586 };
587 ElfStatus status = ParseSymbolsFromElfFile(debug_file_path_, build_id, symbol_callback);
588 ReportReadElfSymbolResult(status, path_, debug_file_path_,
589 symbols_.empty() ? android::base::WARNING : android::base::DEBUG);
590 SortAndFixSymbols(symbols);
591 return symbols;
592 }
593};
594
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700595class UnknownDso : public Dso {
596 public:
597 UnknownDso(const std::string& path) : Dso(DSO_UNKNOWN_FILE, path, path) {}
598
599 protected:
600 std::vector<Symbol> LoadSymbols() override {
601 return std::vector<Symbol>();
602 }
603};
604
Yabin Cui516a87c2018-03-26 17:34:00 -0700605std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path,
606 bool force_64bit) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700607 switch (dso_type) {
Yabin Cui40b70ff2018-04-09 14:06:08 -0700608 case DSO_ELF_FILE: {
609 BuildId build_id = FindExpectedBuildIdForPath(dso_path);
610 return std::unique_ptr<Dso>(new ElfDso(dso_path,
611 debug_elf_file_finder_.FindDebugFile(dso_path, force_64bit, build_id)));
612 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700613 case DSO_KERNEL:
614 return std::unique_ptr<Dso>(new KernelDso(dso_path, dso_path));
615 case DSO_KERNEL_MODULE:
Yabin Cui40b70ff2018-04-09 14:06:08 -0700616 return std::unique_ptr<Dso>(new KernelModuleDso(dso_path, dso_path));
Yabin Cui516a87c2018-03-26 17:34:00 -0700617 case DSO_DEX_FILE:
Yabin Cui40b70ff2018-04-09 14:06:08 -0700618 return std::unique_ptr<Dso>(new DexFileDso(dso_path, dso_path));
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700619 case DSO_UNKNOWN_FILE:
620 return std::unique_ptr<Dso>(new UnknownDso(dso_path));
Yabin Cui516a87c2018-03-26 17:34:00 -0700621 default:
622 LOG(FATAL) << "Unexpected dso_type " << static_cast<int>(dso_type);
623 }
624 return nullptr;
625}
626
Yabin Cui767dd172016-06-02 21:02:43 -0700627const char* DsoTypeToString(DsoType dso_type) {
628 switch (dso_type) {
629 case DSO_KERNEL:
630 return "dso_kernel";
631 case DSO_KERNEL_MODULE:
632 return "dso_kernel_module";
633 case DSO_ELF_FILE:
634 return "dso_elf_file";
Yabin Cui516a87c2018-03-26 17:34:00 -0700635 case DSO_DEX_FILE:
636 return "dso_dex_file";
Yabin Cui767dd172016-06-02 21:02:43 -0700637 default:
638 return "unknown";
639 }
640}
Yabin Cui40b70ff2018-04-09 14:06:08 -0700641
642bool GetBuildIdFromDsoPath(const std::string& dso_path, BuildId* build_id) {
643 auto tuple = SplitUrlInApk(dso_path);
644 ElfStatus result;
645 if (std::get<0>(tuple)) {
Yabin Cui8422f342018-05-09 17:27:27 -0700646 EmbeddedElf* elf = ApkInspector::FindElfInApkByName(std::get<1>(tuple), std::get<2>(tuple));
647 if (elf == nullptr) {
648 result = ElfStatus::FILE_NOT_FOUND;
649 } else {
650 result = GetBuildIdFromEmbeddedElfFile(elf->filepath(), elf->entry_offset(),
651 elf->entry_size(), build_id);
652 }
Yabin Cui40b70ff2018-04-09 14:06:08 -0700653 } else {
654 result = GetBuildIdFromElfFile(dso_path, build_id);
655 }
656 return result == ElfStatus::NO_ERROR;
657}