blob: 4f202bcdd0eb14ad3da9b5a78bdf9581f9c405b4 [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),
156 symbol_dump_id_(0) {
157 if (type_ == DSO_KERNEL) {
158 min_vaddr_ = 0;
159 }
Yabin Cuieec606c2016-07-07 13:53:33 -0700160 // Check if file matching path_ exists in symfs directory before using it as
161 // debug_file_path_.
162 if (!symfs_dir_.empty()) {
163 std::string path_in_symfs = symfs_dir_ + path_;
164 std::tuple<bool, std::string, std::string> tuple =
165 SplitUrlInApk(path_in_symfs);
166 std::string file_path =
167 std::get<0>(tuple) ? std::get<1>(tuple) : path_in_symfs;
168 if (IsRegularFile(file_path)) {
169 debug_file_path_ = path_in_symfs;
170 }
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700171 } else if (path == "[vdso]") {
172 if (force_64bit && vdso_64bit_ != nullptr) {
173 debug_file_path_ = vdso_64bit_->path;
174 } else if (!force_64bit && vdso_32bit_ != nullptr) {
175 debug_file_path_ = vdso_32bit_->path;
176 }
Yabin Cuieec606c2016-07-07 13:53:33 -0700177 }
Yabin Cui15475e62016-07-14 13:26:19 -0700178 size_t pos = path.find_last_of("/\\");
179 if (pos != std::string::npos) {
180 file_name_ = path.substr(pos + 1);
181 } else {
182 file_name_ = path;
183 }
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700184 dso_count_++;
Yabin Cuic8485602015-08-20 15:04:39 -0700185}
186
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700187Dso::~Dso() {
188 if (--dso_count_ == 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700189 // Clean up global variables when no longer used.
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700190 symbol_name_allocator.Clear();
Yabin Cuib4212972016-05-25 14:08:05 -0700191 demangle_ = true;
192 symfs_dir_.clear();
193 vmlinux_.clear();
194 kallsyms_.clear();
Yabin Cuia9392452017-01-12 18:07:27 -0800195 read_kernel_symbols_from_proc_ = false;
Yabin Cuib4212972016-05-25 14:08:05 -0700196 build_id_map_.clear();
Yabin Cui16501ff2016-10-19 15:06:29 -0700197 g_dump_id_ = 0;
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700198 vdso_64bit_ = nullptr;
199 vdso_32bit_ = nullptr;
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700200 }
201}
202
Yabin Cui16501ff2016-10-19 15:06:29 -0700203uint32_t Dso::CreateDumpId() {
204 CHECK(!HasDumpId());
205 return dump_id_ = g_dump_id_++;
206}
207
208uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
209 CHECK(!symbol->HasDumpId());
210 symbol->dump_id_ = symbol_dump_id_++;
211 return symbol->dump_id_;
212}
213
Yabin Cui547c60e2015-10-12 16:56:05 -0700214const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
Yabin Cuic8485602015-08-20 15:04:39 -0700215 if (!is_loaded_) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700216 Load();
217 }
218 if (!symbols_.empty()) {
Yabin Cuiaba7e292016-11-11 14:53:52 -0800219 auto it = std::upper_bound(symbols_.begin(), symbols_.end(),
220 Symbol("", vaddr_in_dso, 0),
221 Symbol::CompareValueByAddr);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700222 if (it != symbols_.begin()) {
223 --it;
224 if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
225 return &*it;
Yabin Cui767dd172016-06-02 21:02:43 -0700226 }
Yabin Cuic8485602015-08-20 15:04:39 -0700227 }
228 }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700229 if (!unknown_symbols_.empty()) {
230 auto it = unknown_symbols_.find(vaddr_in_dso);
231 if (it != unknown_symbols_.end()) {
232 return &it->second;
Yabin Cuic8485602015-08-20 15:04:39 -0700233 }
234 }
235 return nullptr;
236}
237
Yabin Cuic5b4a312016-10-24 13:38:38 -0700238const std::vector<Symbol>& Dso::GetSymbols() {
239 if (!is_loaded_) {
240 Load();
241 }
242 return symbols_;
243}
244
245void Dso::SetSymbols(std::vector<Symbol>* symbols) {
246 symbols_ = std::move(*symbols);
247 symbols->clear();
248}
249
250void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) {
251 unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1)));
252}
253
Yabin Cui547c60e2015-10-12 16:56:05 -0700254uint64_t Dso::MinVirtualAddress() {
255 if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
256 min_vaddr_ = 0;
257 if (type_ == DSO_ELF_FILE) {
Yabin Cuieec606c2016-07-07 13:53:33 -0700258 BuildId build_id = GetExpectedBuildId();
Yabin Cui547c60e2015-10-12 16:56:05 -0700259
260 uint64_t addr;
Yabin Cuidec43c12016-07-29 16:40:40 -0700261 ElfStatus result = ReadMinExecutableVirtualAddressFromElfFile(
262 GetDebugFilePath(), build_id, &addr);
263 if (result != ElfStatus::NO_ERROR) {
264 LOG(WARNING) << "failed to read min virtual address of "
265 << GetDebugFilePath() << ": " << result;
266 } else {
Yabin Cui547c60e2015-10-12 16:56:05 -0700267 min_vaddr_ = addr;
268 }
269 }
270 }
271 return min_vaddr_;
272}
273
Yabin Cuie2f10782016-12-15 12:00:44 -0800274static std::vector<Symbol> MergeSortedSymbols(const std::vector<Symbol>& s1,
275 const std::vector<Symbol>& s2) {
276 std::vector<Symbol> result;
277 std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(result),
278 Symbol::CompareValueByAddr);
279 return result;
280}
281
Yabin Cuic5b4a312016-10-24 13:38:38 -0700282void Dso::Load() {
283 is_loaded_ = true;
Yabin Cuie2f10782016-12-15 12:00:44 -0800284 std::vector<Symbol> dumped_symbols;
Yabin Cuic5b4a312016-10-24 13:38:38 -0700285 if (!symbols_.empty()) {
Yabin Cuie2f10782016-12-15 12:00:44 -0800286 // If symbols has been read from file feature section of perf.data, move it to
287 // dumped_symbols, so later we can merge them with symbols read from file system.
288 dumped_symbols = std::move(symbols_);
289 symbols_.clear();
Yabin Cuic5b4a312016-10-24 13:38:38 -0700290 }
Yabin Cuic8485602015-08-20 15:04:39 -0700291 bool result = false;
292 switch (type_) {
293 case DSO_KERNEL:
294 result = LoadKernel();
295 break;
296 case DSO_KERNEL_MODULE:
297 result = LoadKernelModule();
298 break;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800299 case DSO_ELF_FILE: {
300 if (std::get<0>(SplitUrlInApk(path_))) {
301 result = LoadEmbeddedElfFile();
302 } else {
303 result = LoadElfFile();
304 }
Yabin Cuic8485602015-08-20 15:04:39 -0700305 break;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800306 }
Yabin Cuic8485602015-08-20 15:04:39 -0700307 }
308 if (result) {
Yabin Cuiaba7e292016-11-11 14:53:52 -0800309 std::sort(symbols_.begin(), symbols_.end(), Symbol::CompareValueByAddr);
Yabin Cuic8485602015-08-20 15:04:39 -0700310 FixupSymbolLength();
Yabin Cuidec43c12016-07-29 16:40:40 -0700311 } else {
312 symbols_.clear();
Yabin Cuie2f10782016-12-15 12:00:44 -0800313 }
314
315 if (symbols_.empty()) {
316 symbols_ = std::move(dumped_symbols);
317 } else if (!dumped_symbols.empty()) {
318 symbols_ = MergeSortedSymbols(symbols_, dumped_symbols);
319 }
320
321 if (symbols_.empty()) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700322 LOG(DEBUG) << "failed to load dso: " << path_;
Yabin Cuic8485602015-08-20 15:04:39 -0700323 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700324}
325
Yabin Cuiec12ed92015-06-08 10:38:10 -0700326static bool IsKernelFunctionSymbol(const KernelSymbol& symbol) {
Yabin Cui767dd172016-06-02 21:02:43 -0700327 return (symbol.type == 'T' || symbol.type == 't' || symbol.type == 'W' ||
328 symbol.type == 'w');
Yabin Cuiec12ed92015-06-08 10:38:10 -0700329}
330
Yabin Cuic5b4a312016-10-24 13:38:38 -0700331static bool KernelSymbolCallback(const KernelSymbol& kernel_symbol,
332 std::vector<Symbol>* symbols) {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700333 if (IsKernelFunctionSymbol(kernel_symbol)) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700334 symbols->emplace_back(Symbol(kernel_symbol.name, kernel_symbol.addr, 0));
Yabin Cuiec12ed92015-06-08 10:38:10 -0700335 }
336 return false;
337}
338
Yabin Cuic5b4a312016-10-24 13:38:38 -0700339static void VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol,
340 std::vector<Symbol>* symbols) {
Yabin Cui39d3cae2015-07-13 16:23:13 -0700341 if (elf_symbol.is_func) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700342 symbols->emplace_back(
Yabin Cui767dd172016-06-02 21:02:43 -0700343 Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
Yabin Cui39d3cae2015-07-13 16:23:13 -0700344 }
345}
346
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700347bool Dso::CheckReadSymbolResult(ElfStatus result, const std::string& filename) {
Yabin Cuidec43c12016-07-29 16:40:40 -0700348 if (result == ElfStatus::NO_ERROR) {
Yabin Cuicb6c9012016-08-25 10:46:40 -0700349 LOG(VERBOSE) << "Read symbols from " << filename << " successfully";
Yabin Cuidec43c12016-07-29 16:40:40 -0700350 return true;
351 } else if (result == ElfStatus::NO_SYMBOL_TABLE) {
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700352 if (path_ == "[vdso]") {
353 // Vdso only contains dynamic symbol table, and we can't change that.
354 return true;
355 }
Yabin Cuidec43c12016-07-29 16:40:40 -0700356 // Lacking symbol table isn't considered as an error but worth reporting.
357 LOG(WARNING) << filename << " doesn't contain symbol table";
358 return true;
359 } else {
360 LOG(WARNING) << "failed to read symbols from " << filename
361 << ": " << result;
362 return false;
363 }
364}
365
Yabin Cuic8485602015-08-20 15:04:39 -0700366bool Dso::LoadKernel() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700367 BuildId build_id = GetExpectedBuildId();
Yabin Cui39d3cae2015-07-13 16:23:13 -0700368 if (!vmlinux_.empty()) {
Yabin Cuidec43c12016-07-29 16:40:40 -0700369 ElfStatus result = ParseSymbolsFromElfFile(vmlinux_, build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700370 std::bind(VmlinuxSymbolCallback, std::placeholders::_1, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700371 return CheckReadSymbolResult(result, vmlinux_);
Yabin Cuib4212972016-05-25 14:08:05 -0700372 } else if (!kallsyms_.empty()) {
Yabin Cui767dd172016-06-02 21:02:43 -0700373 ProcessKernelSymbols(kallsyms_, std::bind(&KernelSymbolCallback,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700374 std::placeholders::_1, &symbols_));
Yabin Cuib4212972016-05-25 14:08:05 -0700375 bool all_zero = true;
376 for (const auto& symbol : symbols_) {
377 if (symbol.addr != 0) {
378 all_zero = false;
379 break;
380 }
381 }
382 if (all_zero) {
Yabin Cui767dd172016-06-02 21:02:43 -0700383 LOG(WARNING)
384 << "Symbol addresses in /proc/kallsyms on device are all zero. "
Yabin Cui6965d422016-06-15 11:41:42 -0700385 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
Yabin Cuib4212972016-05-25 14:08:05 -0700386 symbols_.clear();
387 return false;
388 }
Yabin Cuia9392452017-01-12 18:07:27 -0800389 } else if (read_kernel_symbols_from_proc_ || !build_id.IsEmpty()) {
390 // Try /proc/kallsyms only when asked to do so, or when build id matches.
391 // Otherwise, it is likely to use /proc/kallsyms on host for perf.data recorded on device.
392 if (!build_id.IsEmpty()) {
393 BuildId real_build_id;
394 if (!GetKernelBuildId(&real_build_id)) {
395 return false;
396 }
397 bool match = (build_id == real_build_id);
398 if (!match) {
399 LOG(WARNING) << "failed to read symbols from /proc/kallsyms: Build id "
400 << "mismatch";
401 return false;
402 }
Yabin Cui39d3cae2015-07-13 16:23:13 -0700403 }
Yabin Cui8a52e972015-10-01 11:32:44 -0700404
Yabin Cuib4212972016-05-25 14:08:05 -0700405 std::string kallsyms;
406 if (!android::base::ReadFileToString("/proc/kallsyms", &kallsyms)) {
407 LOG(DEBUG) << "failed to read /proc/kallsyms";
408 return false;
409 }
Yabin Cui767dd172016-06-02 21:02:43 -0700410 ProcessKernelSymbols(kallsyms, std::bind(&KernelSymbolCallback,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700411 std::placeholders::_1, &symbols_));
Yabin Cuib4212972016-05-25 14:08:05 -0700412 bool all_zero = true;
413 for (const auto& symbol : symbols_) {
Yabin Cui8a52e972015-10-01 11:32:44 -0700414 if (symbol.addr != 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700415 all_zero = false;
Yabin Cui8a52e972015-10-01 11:32:44 -0700416 break;
417 }
418 }
Yabin Cuib4212972016-05-25 14:08:05 -0700419 if (all_zero) {
420 LOG(WARNING) << "Symbol addresses in /proc/kallsyms are all zero. "
Yabin Cui6965d422016-06-15 11:41:42 -0700421 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
Yabin Cui8a52e972015-10-01 11:32:44 -0700422 symbols_.clear();
423 return false;
424 }
Yabin Cui638c5582015-07-01 16:16:57 -0700425 }
Yabin Cui04c70a62015-08-04 14:48:39 -0700426 return true;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700427}
428
Yabin Cuic5b4a312016-10-24 13:38:38 -0700429static void ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol,
430 bool (*filter)(const ElfFileSymbol&),
431 std::vector<Symbol>* symbols) {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700432 if (filter(elf_symbol)) {
Yabin Cuic5b4a312016-10-24 13:38:38 -0700433 symbols->emplace_back(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len);
Yabin Cuiec12ed92015-06-08 10:38:10 -0700434 }
435}
436
437static bool SymbolFilterForKernelModule(const ElfFileSymbol& elf_symbol) {
438 // TODO: Parse symbol outside of .text section.
439 return (elf_symbol.is_func && elf_symbol.is_in_text_section);
440}
441
Yabin Cuic8485602015-08-20 15:04:39 -0700442bool Dso::LoadKernelModule() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700443 BuildId build_id = GetExpectedBuildId();
Yabin Cuidec43c12016-07-29 16:40:40 -0700444 ElfStatus result = ParseSymbolsFromElfFile(GetDebugFilePath(), build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700445 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
446 SymbolFilterForKernelModule, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700447 return CheckReadSymbolResult(result, GetDebugFilePath());
Yabin Cuiec12ed92015-06-08 10:38:10 -0700448}
449
450static bool SymbolFilterForDso(const ElfFileSymbol& elf_symbol) {
Yabin Cui767dd172016-06-02 21:02:43 -0700451 return elf_symbol.is_func ||
452 (elf_symbol.is_label && elf_symbol.is_in_text_section);
Yabin Cuib3783552015-06-11 11:15:42 -0700453}
454
Yabin Cuic8485602015-08-20 15:04:39 -0700455bool Dso::LoadElfFile() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700456 BuildId build_id = GetExpectedBuildId();
Yabin Cui547c60e2015-10-12 16:56:05 -0700457
458 if (symfs_dir_.empty()) {
459 // Linux host can store debug shared libraries in /usr/lib/debug.
Yabin Cuidec43c12016-07-29 16:40:40 -0700460 ElfStatus result = ParseSymbolsFromElfFile(
Yabin Cui547c60e2015-10-12 16:56:05 -0700461 "/usr/lib/debug" + path_, build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700462 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
463 SymbolFilterForDso, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700464 if (result == ElfStatus::NO_ERROR) {
Yabin Cuicb6c9012016-08-25 10:46:40 -0700465 return CheckReadSymbolResult(result, "/usr/lib/debug" + path_);
Yabin Cuidec43c12016-07-29 16:40:40 -0700466 }
Yabin Cui547c60e2015-10-12 16:56:05 -0700467 }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700468 // TODO: load std::vector<Symbol> directly from ParseSymbolsFromElfFile
469 // instead of needing to call a callback function for each symbol.
Yabin Cuidec43c12016-07-29 16:40:40 -0700470 ElfStatus result = ParseSymbolsFromElfFile(
471 GetDebugFilePath(), build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700472 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
473 SymbolFilterForDso, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700474 return CheckReadSymbolResult(result, GetDebugFilePath());
Yabin Cuiec12ed92015-06-08 10:38:10 -0700475}
Yabin Cui638c5582015-07-01 16:16:57 -0700476
Yabin Cuib1a885b2016-02-14 19:18:02 -0800477bool Dso::LoadEmbeddedElfFile() {
Yabin Cuieec606c2016-07-07 13:53:33 -0700478 BuildId build_id = GetExpectedBuildId();
Yabin Cuidec43c12016-07-29 16:40:40 -0700479 auto tuple = SplitUrlInApk(GetDebugFilePath());
Yabin Cuib1a885b2016-02-14 19:18:02 -0800480 CHECK(std::get<0>(tuple));
Yabin Cuidec43c12016-07-29 16:40:40 -0700481 ElfStatus result = ParseSymbolsFromApkFile(
Yabin Cui767dd172016-06-02 21:02:43 -0700482 std::get<1>(tuple), std::get<2>(tuple), build_id,
Yabin Cuic5b4a312016-10-24 13:38:38 -0700483 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
484 SymbolFilterForDso, &symbols_));
Yabin Cuidec43c12016-07-29 16:40:40 -0700485 return CheckReadSymbolResult(result, GetDebugFilePath());
Yabin Cuib1a885b2016-02-14 19:18:02 -0800486}
487
Yabin Cuic8485602015-08-20 15:04:39 -0700488void Dso::FixupSymbolLength() {
489 Symbol* prev_symbol = nullptr;
490 for (auto& symbol : symbols_) {
491 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700492 prev_symbol->len = symbol.addr - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700493 }
Yabin Cui767dd172016-06-02 21:02:43 -0700494 prev_symbol = const_cast<Symbol*>(&symbol);
Yabin Cui638c5582015-07-01 16:16:57 -0700495 }
Yabin Cuic8485602015-08-20 15:04:39 -0700496 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Chih-Hung Hsieh7d6c8ab2016-04-15 16:12:03 -0700497 prev_symbol->len = std::numeric_limits<uint64_t>::max() - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700498 }
Yabin Cui638c5582015-07-01 16:16:57 -0700499}
Yabin Cui767dd172016-06-02 21:02:43 -0700500
501const char* DsoTypeToString(DsoType dso_type) {
502 switch (dso_type) {
503 case DSO_KERNEL:
504 return "dso_kernel";
505 case DSO_KERNEL_MODULE:
506 return "dso_kernel_module";
507 case DSO_ELF_FILE:
508 return "dso_elf_file";
509 default:
510 return "unknown";
511 }
512}