blob: 7959d4b97a25ea2ff35f0f1d675ac4e267ba678a [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 Cuia9392452017-01-12 18:07:27 -080059bool Dso::read_kernel_symbols_from_proc_;
Yabin Cuic8485602015-08-20 15:04:39 -070060std::unordered_map<std::string, BuildId> Dso::build_id_map_;
Yabin Cuicc2e59e2015-08-21 14:23:43 -070061size_t Dso::dso_count_;
Yabin Cui16501ff2016-10-19 15:06:29 -070062uint32_t Dso::g_dump_id_;
Yabin Cui63a1c3d2017-05-19 12:57:44 -070063std::unique_ptr<TemporaryFile> Dso::vdso_64bit_;
64std::unique_ptr<TemporaryFile> Dso::vdso_32bit_;
Yabin Cuiba50c4b2015-07-21 11:24:48 -070065
Yabin Cui767dd172016-06-02 21:02:43 -070066void Dso::SetDemangle(bool demangle) { demangle_ = demangle; }
Yabin Cuib3783552015-06-11 11:15:42 -070067
Yabin Cui767dd172016-06-02 21:02:43 -070068extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n,
69 int* status);
Yabin Cuib10a8fb2015-08-18 16:32:18 -070070
Yabin Cuic8485602015-08-20 15:04:39 -070071std::string Dso::Demangle(const std::string& name) {
Yabin Cuib10a8fb2015-08-18 16:32:18 -070072 if (!demangle_) {
73 return name;
74 }
75 int status;
76 bool is_linker_symbol = (name.find(linker_prefix) == 0);
77 const char* mangled_str = name.c_str();
78 if (is_linker_symbol) {
79 mangled_str += linker_prefix.size();
80 }
81 std::string result = name;
82 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
83 if (status == 0) {
84 if (is_linker_symbol) {
85 result = std::string("[linker]") + demangled_name;
86 } else {
87 result = demangled_name;
88 }
89 free(demangled_name);
90 } else if (is_linker_symbol) {
91 result = std::string("[linker]") + mangled_str;
92 }
93 return result;
94}
95
Yabin Cuic8485602015-08-20 15:04:39 -070096bool Dso::SetSymFsDir(const std::string& symfs_dir) {
97 std::string dirname = symfs_dir;
98 if (!dirname.empty()) {
99 if (dirname.back() != '/') {
100 dirname.push_back('/');
101 }
Yabin Cuif560a6f2016-12-14 17:43:26 -0800102 if (!IsDir(symfs_dir)) {
Yabin Cuic8485602015-08-20 15:04:39 -0700103 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'";
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700104 return false;
Yabin Cuic8485602015-08-20 15:04:39 -0700105 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700106 }
Yabin Cuic8485602015-08-20 15:04:39 -0700107 symfs_dir_ = dirname;
108 return true;
109}
110
Yabin Cui767dd172016-06-02 21:02:43 -0700111void Dso::SetVmlinux(const std::string& vmlinux) { vmlinux_ = vmlinux; }
Yabin Cuic8485602015-08-20 15:04:39 -0700112
Yabin Cui767dd172016-06-02 21:02:43 -0700113void Dso::SetBuildIds(
114 const std::vector<std::pair<std::string, BuildId>>& build_ids) {
Yabin Cuic8485602015-08-20 15:04:39 -0700115 std::unordered_map<std::string, BuildId> map;
116 for (auto& pair : build_ids) {
Yabin Cui767dd172016-06-02 21:02:43 -0700117 LOG(DEBUG) << "build_id_map: " << pair.first << ", "
118 << pair.second.ToString();
Yabin Cuic8485602015-08-20 15:04:39 -0700119 map.insert(pair);
120 }
121 build_id_map_ = std::move(map);
122}
123
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700124void Dso::SetVdsoFile(std::unique_ptr<TemporaryFile> vdso_file, bool is_64bit) {
125 if (is_64bit) {
126 vdso_64bit_ = std::move(vdso_file);
127 } else {
128 vdso_32bit_ = std::move(vdso_file);
129 }
130}
131
Yabin Cui52c63692016-11-28 17:28:08 -0800132BuildId Dso::FindExpectedBuildIdForPath(const std::string& path) {
133 auto it = build_id_map_.find(path);
Yabin Cuic8485602015-08-20 15:04:39 -0700134 if (it != build_id_map_.end()) {
135 return it->second;
136 }
137 return BuildId();
138}
139
Yabin Cui52c63692016-11-28 17:28:08 -0800140BuildId Dso::GetExpectedBuildId() {
141 return FindExpectedBuildIdForPath(path_);
142}
143
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700144std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path,
145 bool force_64bit) {
146 return std::unique_ptr<Dso>(new Dso(dso_type, dso_path, force_64bit));
Yabin Cuic8485602015-08-20 15:04:39 -0700147}
148
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700149Dso::Dso(DsoType type, const std::string& path, bool force_64bit)
Yabin Cui767dd172016-06-02 21:02:43 -0700150 : type_(type),
Yabin Cui767dd172016-06-02 21:02:43 -0700151 path_(path),
Yabin Cuieec606c2016-07-07 13:53:33 -0700152 debug_file_path_(path),
Yabin Cui767dd172016-06-02 21:02:43 -0700153 min_vaddr_(std::numeric_limits<uint64_t>::max()),
154 is_loaded_(false),
Yabin Cui16501ff2016-10-19 15:06:29 -0700155 dump_id_(UINT_MAX),
Yabin Cuie466d4d2017-08-11 17:03:07 -0700156 symbol_dump_id_(0),
157 symbol_warning_loglevel_(android::base::WARNING) {
Yabin Cui16501ff2016-10-19 15:06:29 -0700158 if (type_ == DSO_KERNEL) {
159 min_vaddr_ = 0;
160 }
Yabin Cuieec606c2016-07-07 13:53:33 -0700161 // Check if file matching path_ exists in symfs directory before using it as
162 // debug_file_path_.
163 if (!symfs_dir_.empty()) {
164 std::string path_in_symfs = symfs_dir_ + path_;
165 std::tuple<bool, std::string, std::string> tuple =
166 SplitUrlInApk(path_in_symfs);
167 std::string file_path =
168 std::get<0>(tuple) ? std::get<1>(tuple) : path_in_symfs;
169 if (IsRegularFile(file_path)) {
170 debug_file_path_ = path_in_symfs;
171 }
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700172 } else if (path == "[vdso]") {
173 if (force_64bit && vdso_64bit_ != nullptr) {
174 debug_file_path_ = vdso_64bit_->path;
175 } else if (!force_64bit && vdso_32bit_ != nullptr) {
176 debug_file_path_ = vdso_32bit_->path;
177 }
Yabin Cuieec606c2016-07-07 13:53:33 -0700178 }
Yabin Cui15475e62016-07-14 13:26:19 -0700179 size_t pos = path.find_last_of("/\\");
180 if (pos != std::string::npos) {
181 file_name_ = path.substr(pos + 1);
182 } else {
183 file_name_ = path;
184 }
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700185 dso_count_++;
Yabin Cuic8485602015-08-20 15:04:39 -0700186}
187
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700188Dso::~Dso() {
189 if (--dso_count_ == 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700190 // Clean up global variables when no longer used.
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700191 symbol_name_allocator.Clear();
Yabin Cuib4212972016-05-25 14:08:05 -0700192 demangle_ = true;
193 symfs_dir_.clear();
194 vmlinux_.clear();
195 kallsyms_.clear();
Yabin Cuia9392452017-01-12 18:07:27 -0800196 read_kernel_symbols_from_proc_ = false;
Yabin Cuib4212972016-05-25 14:08:05 -0700197 build_id_map_.clear();
Yabin Cui16501ff2016-10-19 15:06:29 -0700198 g_dump_id_ = 0;
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700199 vdso_64bit_ = nullptr;
200 vdso_32bit_ = nullptr;
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700201 }
202}
203
Yabin Cui16501ff2016-10-19 15:06:29 -0700204uint32_t Dso::CreateDumpId() {
205 CHECK(!HasDumpId());
206 return dump_id_ = g_dump_id_++;
207}
208
209uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
210 CHECK(!symbol->HasDumpId());
211 symbol->dump_id_ = symbol_dump_id_++;
212 return symbol->dump_id_;
213}
214
Yabin Cui547c60e2015-10-12 16:56:05 -0700215const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
Yabin Cuic8485602015-08-20 15:04:39 -0700216 if (!is_loaded_) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700217 Load();
218 }
219 if (!symbols_.empty()) {
Yabin Cuiaba7e292016-11-11 14:53:52 -0800220 auto it = std::upper_bound(symbols_.begin(), symbols_.end(),
221 Symbol("", vaddr_in_dso, 0),
222 Symbol::CompareValueByAddr);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700223 if (it != symbols_.begin()) {
224 --it;
225 if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
226 return &*it;
Yabin Cui767dd172016-06-02 21:02:43 -0700227 }
Yabin Cuic8485602015-08-20 15:04:39 -0700228 }
229 }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700230 if (!unknown_symbols_.empty()) {
231 auto it = unknown_symbols_.find(vaddr_in_dso);
232 if (it != unknown_symbols_.end()) {
233 return &it->second;
Yabin Cuic8485602015-08-20 15:04:39 -0700234 }
235 }
236 return nullptr;
237}
238
Yabin Cuic5b4a312016-10-24 13:38:38 -0700239const std::vector<Symbol>& Dso::GetSymbols() {
240 if (!is_loaded_) {
241 Load();
242 }
243 return symbols_;
244}
245
246void Dso::SetSymbols(std::vector<Symbol>* symbols) {
247 symbols_ = std::move(*symbols);
248 symbols->clear();
249}
250
251void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) {
252 unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1)));
253}
254
Yabin Cui547c60e2015-10-12 16:56:05 -0700255uint64_t Dso::MinVirtualAddress() {
256 if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
257 min_vaddr_ = 0;
258 if (type_ == DSO_ELF_FILE) {
Yabin Cuieec606c2016-07-07 13:53:33 -0700259 BuildId build_id = GetExpectedBuildId();
Yabin Cui547c60e2015-10-12 16:56:05 -0700260
261 uint64_t addr;
Yabin Cuidec43c12016-07-29 16:40:40 -0700262 ElfStatus result = ReadMinExecutableVirtualAddressFromElfFile(
263 GetDebugFilePath(), build_id, &addr);
264 if (result != ElfStatus::NO_ERROR) {
265 LOG(WARNING) << "failed to read min virtual address of "
266 << GetDebugFilePath() << ": " << result;
267 } else {
Yabin Cui547c60e2015-10-12 16:56:05 -0700268 min_vaddr_ = addr;
269 }
270 }
271 }
272 return min_vaddr_;
273}
274
Yabin Cuie2f10782016-12-15 12:00:44 -0800275static std::vector<Symbol> MergeSortedSymbols(const std::vector<Symbol>& s1,
276 const std::vector<Symbol>& s2) {
277 std::vector<Symbol> result;
278 std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(result),
279 Symbol::CompareValueByAddr);
280 return result;
281}
282
Yabin Cuic5b4a312016-10-24 13:38:38 -0700283void Dso::Load() {
284 is_loaded_ = true;
Yabin Cuie2f10782016-12-15 12:00:44 -0800285 std::vector<Symbol> dumped_symbols;
Yabin Cuic5b4a312016-10-24 13:38:38 -0700286 if (!symbols_.empty()) {
Yabin Cuie2f10782016-12-15 12:00:44 -0800287 // If symbols has been read from file feature section of perf.data, move it to
288 // dumped_symbols, so later we can merge them with symbols read from file system.
289 dumped_symbols = std::move(symbols_);
290 symbols_.clear();
Yabin Cuie466d4d2017-08-11 17:03:07 -0700291 // Don't warn missing symbol table if we have dumped symbols in perf.data.
292 symbol_warning_loglevel_ = android::base::DEBUG;
Yabin Cuic5b4a312016-10-24 13:38:38 -0700293 }
Yabin Cuic8485602015-08-20 15:04:39 -0700294 bool result = false;
295 switch (type_) {
296 case DSO_KERNEL:
297 result = LoadKernel();
298 break;
299 case DSO_KERNEL_MODULE:
300 result = LoadKernelModule();
301 break;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800302 case DSO_ELF_FILE: {
303 if (std::get<0>(SplitUrlInApk(path_))) {
304 result = LoadEmbeddedElfFile();
305 } else {
306 result = LoadElfFile();
307 }
Yabin Cuic8485602015-08-20 15:04:39 -0700308 break;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800309 }
Yabin Cuic8485602015-08-20 15:04:39 -0700310 }
311 if (result) {
Yabin Cuiaba7e292016-11-11 14:53:52 -0800312 std::sort(symbols_.begin(), symbols_.end(), Symbol::CompareValueByAddr);
Yabin Cuic8485602015-08-20 15:04:39 -0700313 FixupSymbolLength();
Yabin Cuidec43c12016-07-29 16:40:40 -0700314 } else {
315 symbols_.clear();
Yabin Cuie2f10782016-12-15 12:00:44 -0800316 }
317
318 if (symbols_.empty()) {
319 symbols_ = std::move(dumped_symbols);
320 } else if (!dumped_symbols.empty()) {
321 symbols_ = MergeSortedSymbols(symbols_, dumped_symbols);
322 }
323
324 if (symbols_.empty()) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700325 LOG(DEBUG) << "failed to load dso: " << path_;
Yabin Cuic8485602015-08-20 15:04:39 -0700326 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700327}
328
Yabin Cuiec12ed92015-06-08 10:38:10 -0700329static bool IsKernelFunctionSymbol(const KernelSymbol& symbol) {
Yabin Cui767dd172016-06-02 21:02:43 -0700330 return (symbol.type == 'T' || symbol.type == 't' || symbol.type == 'W' ||
331 symbol.type == 'w');
Yabin Cuiec12ed92015-06-08 10:38:10 -0700332}
333
Yabin Cuic5b4a312016-10-24 13:38:38 -0700334static bool KernelSymbolCallback(const KernelSymbol& kernel_symbol,
335 std::vector<Symbol>* symbols) {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700336 if (IsKernelFunctionSymbol(kernel_symbol)) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700337 symbols->emplace_back(Symbol(kernel_symbol.name, kernel_symbol.addr, 0));
Yabin Cuiec12ed92015-06-08 10:38:10 -0700338 }
339 return false;
340}
341
Yabin Cuic5b4a312016-10-24 13:38:38 -0700342static void VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol,
343 std::vector<Symbol>* symbols) {
Yabin Cui39d3cae2015-07-13 16:23:13 -0700344 if (elf_symbol.is_func) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700345 symbols->emplace_back(
Yabin Cui767dd172016-06-02 21:02:43 -0700346 Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
Yabin Cui39d3cae2015-07-13 16:23:13 -0700347 }
348}
349
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700350bool Dso::CheckReadSymbolResult(ElfStatus result, const std::string& filename) {
Yabin Cuidec43c12016-07-29 16:40:40 -0700351 if (result == ElfStatus::NO_ERROR) {
Yabin Cuicb6c9012016-08-25 10:46:40 -0700352 LOG(VERBOSE) << "Read symbols from " << filename << " successfully";
Yabin Cuidec43c12016-07-29 16:40:40 -0700353 return true;
354 } else if (result == ElfStatus::NO_SYMBOL_TABLE) {
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700355 if (path_ == "[vdso]") {
356 // Vdso only contains dynamic symbol table, and we can't change that.
357 return true;
358 }
Yabin Cuidec43c12016-07-29 16:40:40 -0700359 // Lacking symbol table isn't considered as an error but worth reporting.
Yabin Cuie466d4d2017-08-11 17:03:07 -0700360 LOG(symbol_warning_loglevel_) << filename << " doesn't contain symbol table";
Yabin Cuidec43c12016-07-29 16:40:40 -0700361 return true;
362 } else {
Yabin Cuie466d4d2017-08-11 17:03:07 -0700363 LOG(symbol_warning_loglevel_) << "failed to read symbols from " << filename << ": " << result;
Yabin Cuidec43c12016-07-29 16:40:40 -0700364 return false;
365 }
366}
367
Yabin Cuic8485602015-08-20 15:04:39 -0700368bool Dso::LoadKernel() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700369 BuildId build_id = GetExpectedBuildId();
Yabin Cui39d3cae2015-07-13 16:23:13 -0700370 if (!vmlinux_.empty()) {
Yabin Cuidec43c12016-07-29 16:40:40 -0700371 ElfStatus result = ParseSymbolsFromElfFile(vmlinux_, build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700372 std::bind(VmlinuxSymbolCallback, std::placeholders::_1, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700373 return CheckReadSymbolResult(result, vmlinux_);
Yabin Cuib4212972016-05-25 14:08:05 -0700374 } else if (!kallsyms_.empty()) {
Yabin Cui767dd172016-06-02 21:02:43 -0700375 ProcessKernelSymbols(kallsyms_, std::bind(&KernelSymbolCallback,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700376 std::placeholders::_1, &symbols_));
Yabin Cuib4212972016-05-25 14:08:05 -0700377 bool all_zero = true;
378 for (const auto& symbol : symbols_) {
379 if (symbol.addr != 0) {
380 all_zero = false;
381 break;
382 }
383 }
384 if (all_zero) {
Yabin Cuie466d4d2017-08-11 17:03:07 -0700385 LOG(symbol_warning_loglevel_)
Yabin Cui767dd172016-06-02 21:02:43 -0700386 << "Symbol addresses in /proc/kallsyms on device are all zero. "
Yabin Cui6965d422016-06-15 11:41:42 -0700387 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
Yabin Cuib4212972016-05-25 14:08:05 -0700388 symbols_.clear();
389 return false;
390 }
Yabin Cuia9392452017-01-12 18:07:27 -0800391 } else if (read_kernel_symbols_from_proc_ || !build_id.IsEmpty()) {
392 // Try /proc/kallsyms only when asked to do so, or when build id matches.
393 // Otherwise, it is likely to use /proc/kallsyms on host for perf.data recorded on device.
394 if (!build_id.IsEmpty()) {
395 BuildId real_build_id;
396 if (!GetKernelBuildId(&real_build_id)) {
397 return false;
398 }
399 bool match = (build_id == real_build_id);
400 if (!match) {
Yabin Cuie466d4d2017-08-11 17:03:07 -0700401 LOG(symbol_warning_loglevel_) << "failed to read symbols from /proc/kallsyms: Build id "
402 << "mismatch";
Yabin Cuia9392452017-01-12 18:07:27 -0800403 return false;
404 }
Yabin Cui39d3cae2015-07-13 16:23:13 -0700405 }
Yabin Cui8a52e972015-10-01 11:32:44 -0700406
Yabin Cuib4212972016-05-25 14:08:05 -0700407 std::string kallsyms;
408 if (!android::base::ReadFileToString("/proc/kallsyms", &kallsyms)) {
409 LOG(DEBUG) << "failed to read /proc/kallsyms";
410 return false;
411 }
Yabin Cui767dd172016-06-02 21:02:43 -0700412 ProcessKernelSymbols(kallsyms, std::bind(&KernelSymbolCallback,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700413 std::placeholders::_1, &symbols_));
Yabin Cuib4212972016-05-25 14:08:05 -0700414 bool all_zero = true;
415 for (const auto& symbol : symbols_) {
Yabin Cui8a52e972015-10-01 11:32:44 -0700416 if (symbol.addr != 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700417 all_zero = false;
Yabin Cui8a52e972015-10-01 11:32:44 -0700418 break;
419 }
420 }
Yabin Cuib4212972016-05-25 14:08:05 -0700421 if (all_zero) {
Yabin Cuie466d4d2017-08-11 17:03:07 -0700422 LOG(symbol_warning_loglevel_) << "Symbol addresses in /proc/kallsyms are all zero. "
423 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
Yabin Cui8a52e972015-10-01 11:32:44 -0700424 symbols_.clear();
425 return false;
426 }
Yabin Cui638c5582015-07-01 16:16:57 -0700427 }
Yabin Cui04c70a62015-08-04 14:48:39 -0700428 return true;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700429}
430
Yabin Cuic5b4a312016-10-24 13:38:38 -0700431static void ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol,
432 bool (*filter)(const ElfFileSymbol&),
433 std::vector<Symbol>* symbols) {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700434 if (filter(elf_symbol)) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700435 symbols->emplace_back(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len);
Yabin Cuiec12ed92015-06-08 10:38:10 -0700436 }
437}
438
439static bool SymbolFilterForKernelModule(const ElfFileSymbol& elf_symbol) {
440 // TODO: Parse symbol outside of .text section.
441 return (elf_symbol.is_func && elf_symbol.is_in_text_section);
442}
443
Yabin Cuic8485602015-08-20 15:04:39 -0700444bool Dso::LoadKernelModule() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700445 BuildId build_id = GetExpectedBuildId();
Yabin Cuidec43c12016-07-29 16:40:40 -0700446 ElfStatus result = ParseSymbolsFromElfFile(GetDebugFilePath(), build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700447 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
448 SymbolFilterForKernelModule, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700449 return CheckReadSymbolResult(result, GetDebugFilePath());
Yabin Cuiec12ed92015-06-08 10:38:10 -0700450}
451
452static bool SymbolFilterForDso(const ElfFileSymbol& elf_symbol) {
Yabin Cui767dd172016-06-02 21:02:43 -0700453 return elf_symbol.is_func ||
454 (elf_symbol.is_label && elf_symbol.is_in_text_section);
Yabin Cuib3783552015-06-11 11:15:42 -0700455}
456
Yabin Cuic8485602015-08-20 15:04:39 -0700457bool Dso::LoadElfFile() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700458 BuildId build_id = GetExpectedBuildId();
Yabin Cui547c60e2015-10-12 16:56:05 -0700459
460 if (symfs_dir_.empty()) {
461 // Linux host can store debug shared libraries in /usr/lib/debug.
Yabin Cuidec43c12016-07-29 16:40:40 -0700462 ElfStatus result = ParseSymbolsFromElfFile(
Yabin Cui547c60e2015-10-12 16:56:05 -0700463 "/usr/lib/debug" + path_, build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700464 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
465 SymbolFilterForDso, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700466 if (result == ElfStatus::NO_ERROR) {
Yabin Cuicb6c9012016-08-25 10:46:40 -0700467 return CheckReadSymbolResult(result, "/usr/lib/debug" + path_);
Yabin Cuidec43c12016-07-29 16:40:40 -0700468 }
Yabin Cui547c60e2015-10-12 16:56:05 -0700469 }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700470 // TODO: load std::vector<Symbol> directly from ParseSymbolsFromElfFile
471 // instead of needing to call a callback function for each symbol.
Yabin Cuidec43c12016-07-29 16:40:40 -0700472 ElfStatus result = ParseSymbolsFromElfFile(
473 GetDebugFilePath(), build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700474 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
475 SymbolFilterForDso, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700476 return CheckReadSymbolResult(result, GetDebugFilePath());
Yabin Cuiec12ed92015-06-08 10:38:10 -0700477}
Yabin Cui638c5582015-07-01 16:16:57 -0700478
Yabin Cuib1a885b2016-02-14 19:18:02 -0800479bool Dso::LoadEmbeddedElfFile() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700480 BuildId build_id = GetExpectedBuildId();
Yabin Cuidec43c12016-07-29 16:40:40 -0700481 auto tuple = SplitUrlInApk(GetDebugFilePath());
Yabin Cuib1a885b2016-02-14 19:18:02 -0800482 CHECK(std::get<0>(tuple));
Yabin Cuidec43c12016-07-29 16:40:40 -0700483 ElfStatus result = ParseSymbolsFromApkFile(
Yabin Cui767dd172016-06-02 21:02:43 -0700484 std::get<1>(tuple), std::get<2>(tuple), build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700485 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
486 SymbolFilterForDso, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700487 return CheckReadSymbolResult(result, GetDebugFilePath());
Yabin Cuib1a885b2016-02-14 19:18:02 -0800488}
489
Yabin Cuic8485602015-08-20 15:04:39 -0700490void Dso::FixupSymbolLength() {
491 Symbol* prev_symbol = nullptr;
492 for (auto& symbol : symbols_) {
493 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700494 prev_symbol->len = symbol.addr - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700495 }
Yabin Cui3d4aa262017-11-01 15:58:55 -0700496 prev_symbol = &symbol;
Yabin Cui638c5582015-07-01 16:16:57 -0700497 }
Yabin Cuic8485602015-08-20 15:04:39 -0700498 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Chih-Hung Hsieh7d6c8ab2016-04-15 16:12:03 -0700499 prev_symbol->len = std::numeric_limits<uint64_t>::max() - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700500 }
Yabin Cui638c5582015-07-01 16:16:57 -0700501}
Yabin Cui767dd172016-06-02 21:02:43 -0700502
503const char* DsoTypeToString(DsoType dso_type) {
504 switch (dso_type) {
505 case DSO_KERNEL:
506 return "dso_kernel";
507 case DSO_KERNEL_MODULE:
508 return "dso_kernel_module";
509 case DSO_ELF_FILE:
510 return "dso_elf_file";
511 default:
512 return "unknown";
513 }
514}