blob: 9c3366765873c57850dad1fa76469de6034db1ee [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
Elliott Hughes66dd09e2015-12-04 14:00:57 -080026#include <android-base/logging.h>
Yabin Cuic8485602015-08-20 15:04:39 -070027
Yabin Cuiec12ed92015-06-08 10:38:10 -070028#include "environment.h"
Yabin Cui72885012016-02-14 19:18:02 -080029#include "read_apk.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070030#include "read_elf.h"
Yabin Cuib3783552015-06-11 11:15:42 -070031#include "utils.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070032
Yabin Cuicc2e59e2015-08-21 14:23:43 -070033static OneTimeFreeAllocator symbol_name_allocator;
34
35Symbol::Symbol(const std::string& name, uint64_t addr, uint64_t len)
36 : addr(addr),
37 len(len),
38 name_(symbol_name_allocator.AllocateString(name)),
39 demangled_name_(nullptr) {
Yabin Cuib10a8fb2015-08-18 16:32:18 -070040}
41
Yabin Cuicc2e59e2015-08-21 14:23:43 -070042const char* Symbol::DemangledName() const {
43 if (demangled_name_ == nullptr) {
44 const std::string s = Dso::Demangle(name_);
45 if (s == name_) {
46 demangled_name_ = name_;
47 } else {
48 demangled_name_ = symbol_name_allocator.AllocateString(s);
49 }
50 }
51 return demangled_name_;
Yabin Cuiec12ed92015-06-08 10:38:10 -070052}
53
Yabin Cuic8485602015-08-20 15:04:39 -070054bool Dso::demangle_ = true;
55std::string Dso::symfs_dir_;
56std::string Dso::vmlinux_;
57std::unordered_map<std::string, BuildId> Dso::build_id_map_;
Yabin Cuicc2e59e2015-08-21 14:23:43 -070058size_t Dso::dso_count_;
Yabin Cuiba50c4b2015-07-21 11:24:48 -070059
Yabin Cuic8485602015-08-20 15:04:39 -070060void Dso::SetDemangle(bool demangle) {
Yabin Cui39d3cae2015-07-13 16:23:13 -070061 demangle_ = demangle;
62}
Yabin Cuib3783552015-06-11 11:15:42 -070063
Yabin Cuib10a8fb2015-08-18 16:32:18 -070064extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n, int* status);
65
Yabin Cuic8485602015-08-20 15:04:39 -070066std::string Dso::Demangle(const std::string& name) {
Yabin Cuib10a8fb2015-08-18 16:32:18 -070067 if (!demangle_) {
68 return name;
69 }
70 int status;
71 bool is_linker_symbol = (name.find(linker_prefix) == 0);
72 const char* mangled_str = name.c_str();
73 if (is_linker_symbol) {
74 mangled_str += linker_prefix.size();
75 }
76 std::string result = name;
77 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
78 if (status == 0) {
79 if (is_linker_symbol) {
80 result = std::string("[linker]") + demangled_name;
81 } else {
82 result = demangled_name;
83 }
84 free(demangled_name);
85 } else if (is_linker_symbol) {
86 result = std::string("[linker]") + mangled_str;
87 }
88 return result;
89}
90
Yabin Cuic8485602015-08-20 15:04:39 -070091bool Dso::SetSymFsDir(const std::string& symfs_dir) {
92 std::string dirname = symfs_dir;
93 if (!dirname.empty()) {
94 if (dirname.back() != '/') {
95 dirname.push_back('/');
96 }
97 std::vector<std::string> files;
98 std::vector<std::string> subdirs;
99 GetEntriesInDir(symfs_dir, &files, &subdirs);
100 if (files.empty() && subdirs.empty()) {
101 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'";
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700102 return false;
Yabin Cuic8485602015-08-20 15:04:39 -0700103 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700104 }
Yabin Cuic8485602015-08-20 15:04:39 -0700105 symfs_dir_ = dirname;
106 return true;
107}
108
109void Dso::SetVmlinux(const std::string& vmlinux) {
110 vmlinux_ = vmlinux;
111}
112
113void Dso::SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids) {
114 std::unordered_map<std::string, BuildId> map;
115 for (auto& pair : build_ids) {
116 LOG(DEBUG) << "build_id_map: " << pair.first << ", " << pair.second.ToString();
117 map.insert(pair);
118 }
119 build_id_map_ = std::move(map);
120}
121
122BuildId Dso::GetExpectedBuildId(const std::string& filename) {
123 auto it = build_id_map_.find(filename);
124 if (it != build_id_map_.end()) {
125 return it->second;
126 }
127 return BuildId();
128}
129
130std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path) {
131 std::string path = dso_path;
132 if (dso_type == DSO_KERNEL) {
133 path = "[kernel.kallsyms]";
134 }
135 return std::unique_ptr<Dso>(new Dso(dso_type, path));
136}
137
Yabin Cui547c60e2015-10-12 16:56:05 -0700138Dso::Dso(DsoType type, const std::string& path)
139 : type_(type), path_(path), min_vaddr_(std::numeric_limits<uint64_t>::max()), is_loaded_(false) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700140 dso_count_++;
Yabin Cuic8485602015-08-20 15:04:39 -0700141}
142
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700143Dso::~Dso() {
144 if (--dso_count_ == 0) {
145 symbol_name_allocator.Clear();
146 }
147}
148
149struct SymbolComparator {
150 bool operator()(const Symbol& symbol1, const Symbol& symbol2) {
151 return symbol1.addr < symbol2.addr;
152 }
153};
154
Yabin Cui3c8c2132015-08-13 20:30:20 -0700155std::string Dso::GetAccessiblePath() const {
156 return symfs_dir_ + path_;
157}
158
Yabin Cui547c60e2015-10-12 16:56:05 -0700159const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
Yabin Cuic8485602015-08-20 15:04:39 -0700160 if (!is_loaded_) {
161 is_loaded_ = true;
162 if (!Load()) {
163 LOG(DEBUG) << "failed to load dso: " << path_;
164 return nullptr;
165 }
166 }
Yabin Cuic8485602015-08-20 15:04:39 -0700167
Yabin Cui547c60e2015-10-12 16:56:05 -0700168 auto it = std::upper_bound(symbols_.begin(), symbols_.end(), Symbol("", vaddr_in_dso, 0),
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700169 SymbolComparator());
Yabin Cuic8485602015-08-20 15:04:39 -0700170 if (it != symbols_.begin()) {
171 --it;
Yabin Cui547c60e2015-10-12 16:56:05 -0700172 if (it->addr <= vaddr_in_dso && it->addr + it->len > vaddr_in_dso) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700173 return &*it;
Yabin Cuic8485602015-08-20 15:04:39 -0700174 }
175 }
176 return nullptr;
177}
178
Yabin Cui547c60e2015-10-12 16:56:05 -0700179uint64_t Dso::MinVirtualAddress() {
180 if (min_vaddr_ == std::numeric_limits<uint64_t>::max()) {
181 min_vaddr_ = 0;
182 if (type_ == DSO_ELF_FILE) {
183 BuildId build_id = GetExpectedBuildId(GetAccessiblePath());
184
185 uint64_t addr;
186 if (ReadMinExecutableVirtualAddressFromElfFile(GetAccessiblePath(), build_id, &addr)) {
187 min_vaddr_ = addr;
188 }
189 }
190 }
191 return min_vaddr_;
192}
193
Yabin Cuic8485602015-08-20 15:04:39 -0700194bool Dso::Load() {
195 bool result = false;
196 switch (type_) {
197 case DSO_KERNEL:
198 result = LoadKernel();
199 break;
200 case DSO_KERNEL_MODULE:
201 result = LoadKernelModule();
202 break;
Yabin Cui72885012016-02-14 19:18:02 -0800203 case DSO_ELF_FILE: {
204 if (std::get<0>(SplitUrlInApk(path_))) {
205 result = LoadEmbeddedElfFile();
206 } else {
207 result = LoadElfFile();
208 }
Yabin Cuic8485602015-08-20 15:04:39 -0700209 break;
Yabin Cui72885012016-02-14 19:18:02 -0800210 }
Yabin Cuic8485602015-08-20 15:04:39 -0700211 }
212 if (result) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700213 std::sort(symbols_.begin(), symbols_.end(), SymbolComparator());
Yabin Cuic8485602015-08-20 15:04:39 -0700214 FixupSymbolLength();
215 }
216 return result;
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700217}
218
Yabin Cuiec12ed92015-06-08 10:38:10 -0700219static bool IsKernelFunctionSymbol(const KernelSymbol& symbol) {
220 return (symbol.type == 'T' || symbol.type == 't' || symbol.type == 'W' || symbol.type == 'w');
221}
222
Yabin Cuic8485602015-08-20 15:04:39 -0700223bool Dso::KernelSymbolCallback(const KernelSymbol& kernel_symbol, Dso* dso) {
Yabin Cuiec12ed92015-06-08 10:38:10 -0700224 if (IsKernelFunctionSymbol(kernel_symbol)) {
Yabin Cuic8485602015-08-20 15:04:39 -0700225 dso->InsertSymbol(Symbol(kernel_symbol.name, kernel_symbol.addr, 0));
Yabin Cuiec12ed92015-06-08 10:38:10 -0700226 }
227 return false;
228}
229
Yabin Cuic8485602015-08-20 15:04:39 -0700230void Dso::VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso) {
Yabin Cui39d3cae2015-07-13 16:23:13 -0700231 if (elf_symbol.is_func) {
Yabin Cuic8485602015-08-20 15:04:39 -0700232 dso->InsertSymbol(Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
Yabin Cui39d3cae2015-07-13 16:23:13 -0700233 }
234}
235
Yabin Cuic8485602015-08-20 15:04:39 -0700236bool Dso::LoadKernel() {
Yabin Cui638c5582015-07-01 16:16:57 -0700237 BuildId build_id = GetExpectedBuildId(DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID);
Yabin Cui39d3cae2015-07-13 16:23:13 -0700238 if (!vmlinux_.empty()) {
239 ParseSymbolsFromElfFile(vmlinux_, build_id,
Yabin Cuic8485602015-08-20 15:04:39 -0700240 std::bind(VmlinuxSymbolCallback, std::placeholders::_1, this));
Yabin Cui39d3cae2015-07-13 16:23:13 -0700241 } else {
Yabin Cui04c70a62015-08-04 14:48:39 -0700242 if (!build_id.IsEmpty()) {
243 BuildId real_build_id;
244 GetKernelBuildId(&real_build_id);
245 bool match = (build_id == real_build_id);
246 LOG(DEBUG) << "check kernel build id (" << (match ? "match" : "mismatch") << "): expected "
247 << build_id.ToString() << ", real " << real_build_id.ToString();
248 if (!match) {
249 return false;
250 }
Yabin Cui39d3cae2015-07-13 16:23:13 -0700251 }
Yabin Cui8a52e972015-10-01 11:32:44 -0700252
Yabin Cui04c70a62015-08-04 14:48:39 -0700253 ProcessKernelSymbols("/proc/kallsyms",
Yabin Cuic8485602015-08-20 15:04:39 -0700254 std::bind(&KernelSymbolCallback, std::placeholders::_1, this));
Yabin Cui8a52e972015-10-01 11:32:44 -0700255 bool allZero = true;
256 for (auto& symbol : symbols_) {
257 if (symbol.addr != 0) {
258 allZero = false;
259 break;
260 }
261 }
262 if (allZero) {
263 LOG(WARNING) << "Symbol addresses in /proc/kallsyms are all zero. Check "
264 "/proc/sys/kernel/kptr_restrict if possible.";
265 symbols_.clear();
266 return false;
267 }
Yabin Cui638c5582015-07-01 16:16:57 -0700268 }
Yabin Cui04c70a62015-08-04 14:48:39 -0700269 return true;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700270}
271
Yabin Cuic8485602015-08-20 15:04:39 -0700272void Dso::ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso,
Yabin Cuiec12ed92015-06-08 10:38:10 -0700273 bool (*filter)(const ElfFileSymbol&)) {
274 if (filter(elf_symbol)) {
Yabin Cui547c60e2015-10-12 16:56:05 -0700275 dso->InsertSymbol(Symbol(elf_symbol.name, elf_symbol.vaddr, elf_symbol.len));
Yabin Cuiec12ed92015-06-08 10:38:10 -0700276 }
277}
278
279static bool SymbolFilterForKernelModule(const ElfFileSymbol& elf_symbol) {
280 // TODO: Parse symbol outside of .text section.
281 return (elf_symbol.is_func && elf_symbol.is_in_text_section);
282}
283
Yabin Cuic8485602015-08-20 15:04:39 -0700284bool Dso::LoadKernelModule() {
285 BuildId build_id = GetExpectedBuildId(path_);
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700286 ParseSymbolsFromElfFile(
Yabin Cuic8485602015-08-20 15:04:39 -0700287 symfs_dir_ + path_, build_id,
288 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this, SymbolFilterForKernelModule));
Yabin Cui04c70a62015-08-04 14:48:39 -0700289 return true;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700290}
291
292static bool SymbolFilterForDso(const ElfFileSymbol& elf_symbol) {
Yabin Cuib3783552015-06-11 11:15:42 -0700293 return elf_symbol.is_func || (elf_symbol.is_label && elf_symbol.is_in_text_section);
294}
295
Yabin Cuic8485602015-08-20 15:04:39 -0700296bool Dso::LoadElfFile() {
Yabin Cui547c60e2015-10-12 16:56:05 -0700297 bool loaded = false;
298 BuildId build_id = GetExpectedBuildId(GetAccessiblePath());
299
300 if (symfs_dir_.empty()) {
301 // Linux host can store debug shared libraries in /usr/lib/debug.
302 loaded = ParseSymbolsFromElfFile(
303 "/usr/lib/debug" + path_, build_id,
304 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this, SymbolFilterForDso));
305 }
306 if (!loaded) {
307 loaded = ParseSymbolsFromElfFile(
308 GetAccessiblePath(), build_id,
309 std::bind(ElfFileSymbolCallback, std::placeholders::_1, this, SymbolFilterForDso));
310 }
311 return loaded;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700312}
Yabin Cui638c5582015-07-01 16:16:57 -0700313
Yabin Cui72885012016-02-14 19:18:02 -0800314bool Dso::LoadEmbeddedElfFile() {
315 std::string path = GetAccessiblePath();
316 BuildId build_id = GetExpectedBuildId(path);
317 auto tuple = SplitUrlInApk(path);
318 CHECK(std::get<0>(tuple));
319 return ParseSymbolsFromApkFile(std::get<1>(tuple), std::get<2>(tuple), build_id,
320 std::bind(ElfFileSymbolCallback, std::placeholders::_1,
321 this, SymbolFilterForDso));
322}
323
Yabin Cuic8485602015-08-20 15:04:39 -0700324void Dso::InsertSymbol(const Symbol& symbol) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700325 symbols_.push_back(symbol);
Yabin Cuic8485602015-08-20 15:04:39 -0700326}
327
328void Dso::FixupSymbolLength() {
329 Symbol* prev_symbol = nullptr;
330 for (auto& symbol : symbols_) {
331 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700332 prev_symbol->len = symbol.addr - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700333 }
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700334 prev_symbol = &symbol;
Yabin Cui638c5582015-07-01 16:16:57 -0700335 }
Yabin Cuic8485602015-08-20 15:04:39 -0700336 if (prev_symbol != nullptr && prev_symbol->len == 0) {
337 prev_symbol->len = std::numeric_limits<unsigned long long>::max() - prev_symbol->addr;
338 }
Yabin Cui638c5582015-07-01 16:16:57 -0700339}