blob: 6d559770907e7d83134f925624aeef5cc5a9c1cf [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 Cui7078c672020-11-10 16:24:12 -080025#include <optional>
Yabin Cui9ba4d942020-09-08 16:12:46 -070026#include <string_view>
Yabin Cuicc2e59e2015-08-21 14:23:43 -070027#include <vector>
Yabin Cuic8485602015-08-20 15:04:39 -070028
Yabin Cuib4212972016-05-25 14:08:05 -070029#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080030#include <android-base/logging.h>
Yabin Cui40b70ff2018-04-09 14:06:08 -070031#include <android-base/strings.h>
Yabin Cuic8485602015-08-20 15:04:39 -070032
Yabin Cui075dd182020-08-05 19:51:36 +000033#include "JITDebugReader.h"
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020034#include "environment.h"
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +010035#include "kallsyms.h"
Yabin Cuib1a885b2016-02-14 19:18:02 -080036#include "read_apk.h"
Yabin Cui516a87c2018-03-26 17:34:00 -070037#include "read_dex_file.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070038#include "read_elf.h"
Yabin Cuib3783552015-06-11 11:15:42 -070039#include "utils.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070040
Yabin Cuifaa7b922021-01-11 17:35:57 -080041namespace simpleperf {
42
Yabin Cui075dd182020-08-05 19:51:36 +000043using android::base::EndsWith;
Yabin Cui9ba4d942020-09-08 16:12:46 -070044using android::base::StartsWith;
Yabin Cui3a880452020-06-29 16:37:31 -070045
Yabin Cui40b70ff2018-04-09 14:06:08 -070046namespace simpleperf_dso_impl {
47
Yabin Cui1b9b1c12018-10-29 14:23:48 -070048std::string RemovePathSeparatorSuffix(const std::string& path) {
49 // Don't remove path separator suffix for '/'.
Yabin Cui075dd182020-08-05 19:51:36 +000050 if (EndsWith(path, OS_PATH_SEPARATOR) && path.size() > 1u) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -070051 return path.substr(0, path.size() - 1);
52 }
53 return path;
54}
55
Yabin Cui40b70ff2018-04-09 14:06:08 -070056void DebugElfFileFinder::Reset() {
57 vdso_64bit_.clear();
58 vdso_32bit_.clear();
59 symfs_dir_.clear();
60 build_id_to_file_map_.clear();
61}
62
63bool DebugElfFileFinder::SetSymFsDir(const std::string& symfs_dir) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -070064 symfs_dir_ = RemovePathSeparatorSuffix(symfs_dir);
65 if (!IsDir(symfs_dir_)) {
66 LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir_ << "'";
67 return false;
Yabin Cui40b70ff2018-04-09 14:06:08 -070068 }
Yabin Cui1b9b1c12018-10-29 14:23:48 -070069 std::string build_id_list_file = symfs_dir_ + OS_PATH_SEPARATOR + "build_id_list";
Yabin Cui40b70ff2018-04-09 14:06:08 -070070 std::string build_id_list;
71 if (android::base::ReadFileToString(build_id_list_file, &build_id_list)) {
72 for (auto& line : android::base::Split(build_id_list, "\n")) {
Yabin Cui2969a9e2018-04-19 17:06:24 -070073 std::vector<std::string> items = android::base::Split(line, "=");
Yabin Cui40b70ff2018-04-09 14:06:08 -070074 if (items.size() == 2u) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -070075 build_id_to_file_map_[items[0]] = symfs_dir_ + OS_PATH_SEPARATOR + items[1];
Yabin Cui40b70ff2018-04-09 14:06:08 -070076 }
77 }
78 }
79 return true;
80}
81
Yabin Cui3939b9d2018-07-20 17:12:13 -070082bool DebugElfFileFinder::AddSymbolDir(const std::string& symbol_dir) {
83 if (!IsDir(symbol_dir)) {
84 LOG(ERROR) << "Invalid symbol dir " << symbol_dir;
85 return false;
86 }
Yabin Cui1b9b1c12018-10-29 14:23:48 -070087 std::string dir = RemovePathSeparatorSuffix(symbol_dir);
Yabin Cui3939b9d2018-07-20 17:12:13 -070088 CollectBuildIdInDir(dir);
89 return true;
90}
91
92void DebugElfFileFinder::CollectBuildIdInDir(const std::string& dir) {
93 for (const std::string& entry : GetEntriesInDir(dir)) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -070094 std::string path = dir + OS_PATH_SEPARATOR + entry;
Yabin Cui3939b9d2018-07-20 17:12:13 -070095 if (IsDir(path)) {
96 CollectBuildIdInDir(path);
97 } else {
98 BuildId build_id;
Yabin Cui3a880452020-06-29 16:37:31 -070099 ElfStatus status;
100 auto elf = ElfFile::Open(path, &status);
101 if (status == ElfStatus::NO_ERROR && elf->GetBuildId(&build_id) == ElfStatus::NO_ERROR) {
Yabin Cui3939b9d2018-07-20 17:12:13 -0700102 build_id_to_file_map_[build_id.ToString()] = path;
103 }
104 }
105 }
106}
107
Yabin Cui40b70ff2018-04-09 14:06:08 -0700108void DebugElfFileFinder::SetVdsoFile(const std::string& vdso_file, bool is_64bit) {
109 if (is_64bit) {
110 vdso_64bit_ = vdso_file;
111 } else {
112 vdso_32bit_ = vdso_file;
113 }
114}
115
Yabin Cui991477b2020-07-17 16:12:15 -0700116static bool CheckDebugFilePath(const std::string& path, BuildId& build_id,
117 bool report_build_id_mismatch) {
118 ElfStatus status;
119 auto elf = ElfFile::Open(path, &status);
120 if (!elf) {
121 return false;
122 }
123 BuildId debug_build_id;
124 status = elf->GetBuildId(&debug_build_id);
125 if (status != ElfStatus::NO_ERROR && status != ElfStatus::NO_BUILD_ID) {
126 return false;
127 }
128
129 // Native libraries in apks and kernel modules may not have build ids.
130 // So build_id and debug_build_id can either be empty, or have the same value.
131 bool match = build_id == debug_build_id;
132 if (!match && report_build_id_mismatch) {
133 LOG(WARNING) << path << " isn't used because of build id mismatch: expected " << build_id
134 << ", real " << debug_build_id;
135 }
136 return match;
137}
138
Yabin Cui40b70ff2018-04-09 14:06:08 -0700139std::string DebugElfFileFinder::FindDebugFile(const std::string& dso_path, bool force_64bit,
140 BuildId& build_id) {
141 if (dso_path == "[vdso]") {
142 if (force_64bit && !vdso_64bit_.empty()) {
143 return vdso_64bit_;
144 } else if (!force_64bit && !vdso_32bit_.empty()) {
145 return vdso_32bit_;
146 }
Yabin Cui3939b9d2018-07-20 17:12:13 -0700147 }
Yabin Cuid347bb42019-11-14 15:24:07 -0800148 if (build_id.IsEmpty()) {
149 // Try reading build id from file if we don't already have one.
150 GetBuildIdFromDsoPath(dso_path, &build_id);
151 }
Yabin Cui3939b9d2018-07-20 17:12:13 -0700152
Yabin Cui5d269c72019-05-31 15:30:17 -0700153 // 1. Try build_id_to_file_map.
154 if (!build_id_to_file_map_.empty()) {
155 if (!build_id.IsEmpty() || GetBuildIdFromDsoPath(dso_path, &build_id)) {
156 auto it = build_id_to_file_map_.find(build_id.ToString());
Yabin Cui991477b2020-07-17 16:12:15 -0700157 if (it != build_id_to_file_map_.end() && CheckDebugFilePath(it->second, build_id, false)) {
Yabin Cui5d269c72019-05-31 15:30:17 -0700158 return it->second;
159 }
160 }
161 }
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700162 if (!symfs_dir_.empty()) {
Yabin Cuia4496ad2019-11-18 16:40:28 -0800163 // 2. Try concatenating symfs_dir and dso_path.
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700164 std::string path = GetPathInSymFsDir(dso_path);
Yabin Cui991477b2020-07-17 16:12:15 -0700165 if (CheckDebugFilePath(path, build_id, true)) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700166 return path;
167 }
Christopher Ferris56a3fa12021-10-12 17:23:30 -0700168 if (EndsWith(dso_path, ".apk") && IsRegularFile(path)) {
169 return path;
170 }
Yabin Cuia4496ad2019-11-18 16:40:28 -0800171 // 3. Try concatenating symfs_dir and basename of dso_path.
172 path = symfs_dir_ + OS_PATH_SEPARATOR + android::base::Basename(dso_path);
Yabin Cui991477b2020-07-17 16:12:15 -0700173 if (CheckDebugFilePath(path, build_id, false)) {
Yabin Cuia4496ad2019-11-18 16:40:28 -0800174 return path;
175 }
Yabin Cui3939b9d2018-07-20 17:12:13 -0700176 }
Yabin Cuia4496ad2019-11-18 16:40:28 -0800177 // 4. Try concatenating /usr/lib/debug and dso_path.
Yabin Cui3939b9d2018-07-20 17:12:13 -0700178 // Linux host can store debug shared libraries in /usr/lib/debug.
Yabin Cui991477b2020-07-17 16:12:15 -0700179 if (CheckDebugFilePath("/usr/lib/debug" + dso_path, build_id, false)) {
Yabin Cui3939b9d2018-07-20 17:12:13 -0700180 return "/usr/lib/debug" + dso_path;
181 }
Yabin Cui40b70ff2018-04-09 14:06:08 -0700182 return dso_path;
183}
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700184
185std::string DebugElfFileFinder::GetPathInSymFsDir(const std::string& path) {
186 auto add_symfs_prefix = [&](const std::string& path) {
Yabin Cui9ba4d942020-09-08 16:12:46 -0700187 if (StartsWith(path, OS_PATH_SEPARATOR)) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700188 return symfs_dir_ + path;
189 }
190 return symfs_dir_ + OS_PATH_SEPARATOR + path;
191 };
192 if (OS_PATH_SEPARATOR == '/') {
193 return add_symfs_prefix(path);
194 }
195 // Paths in recorded perf.data uses '/' as path separator. When reporting on Windows, it needs
196 // to be converted to '\\'.
197 auto tuple = SplitUrlInApk(path);
198 if (std::get<0>(tuple)) {
199 std::string apk_path = std::get<1>(tuple);
200 std::string entry_path = std::get<2>(tuple);
201 std::replace(apk_path.begin(), apk_path.end(), '/', OS_PATH_SEPARATOR);
202 return GetUrlInApk(add_symfs_prefix(apk_path), entry_path);
203 }
204 std::string elf_path = path;
205 std::replace(elf_path.begin(), elf_path.end(), '/', OS_PATH_SEPARATOR);
206 return add_symfs_prefix(elf_path);
207}
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200208} // namespace simpleperf_dso_impl
Yabin Cui40b70ff2018-04-09 14:06:08 -0700209
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700210static OneTimeFreeAllocator symbol_name_allocator;
211
Martin Stjernholm7c27cc22018-11-28 00:46:00 +0000212Symbol::Symbol(std::string_view name, uint64_t addr, uint64_t len)
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700213 : addr(addr),
214 len(len),
215 name_(symbol_name_allocator.AllocateString(name)),
Yabin Cui767dd172016-06-02 21:02:43 -0700216 demangled_name_(nullptr),
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200217 dump_id_(UINT_MAX) {}
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700218
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700219const char* Symbol::DemangledName() const {
220 if (demangled_name_ == nullptr) {
221 const std::string s = Dso::Demangle(name_);
Yabin Cui40eef9e2021-04-13 13:08:31 -0700222 SetDemangledName(s);
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700223 }
224 return demangled_name_;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700225}
226
Yabin Cui40eef9e2021-04-13 13:08:31 -0700227void Symbol::SetDemangledName(std::string_view name) const {
228 if (name == name_) {
229 demangled_name_ = name_;
230 } else {
231 demangled_name_ = symbol_name_allocator.AllocateString(name);
232 }
233}
234
Yabin Cuifef95142021-08-19 10:51:00 -0700235std::string_view Symbol::FunctionName() const {
Yabin Cui1e16b202021-08-16 13:37:35 -0700236 // Name with signature is like "void ctep.v(cteo, ctgc, ctbn)".
237 std::string_view name = DemangledName();
238 auto brace_pos = name.find('(');
239 if (brace_pos != name.npos) {
240 name = name.substr(0, brace_pos);
241 auto space_pos = name.rfind(' ');
242 if (space_pos != name.npos) {
243 name = name.substr(space_pos + 1);
244 }
245 }
246 return name;
247}
248
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800249static bool CompareSymbolToAddr(const Symbol& s, uint64_t addr) {
250 return s.addr < addr;
251}
252
253static bool CompareAddrToSymbol(uint64_t addr, const Symbol& s) {
254 return addr < s.addr;
255}
256
Yabin Cuic8485602015-08-20 15:04:39 -0700257bool Dso::demangle_ = true;
Yabin Cuic8485602015-08-20 15:04:39 -0700258std::string Dso::vmlinux_;
Yabin Cuib4212972016-05-25 14:08:05 -0700259std::string Dso::kallsyms_;
Yabin Cuic8485602015-08-20 15:04:39 -0700260std::unordered_map<std::string, BuildId> Dso::build_id_map_;
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700261size_t Dso::dso_count_;
Yabin Cui16501ff2016-10-19 15:06:29 -0700262uint32_t Dso::g_dump_id_;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700263simpleperf_dso_impl::DebugElfFileFinder Dso::debug_elf_file_finder_;
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700264
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200265void Dso::SetDemangle(bool demangle) {
266 demangle_ = demangle;
267}
Yabin Cuib3783552015-06-11 11:15:42 -0700268
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200269extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n, int* status);
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700270
Yabin Cuic8485602015-08-20 15:04:39 -0700271std::string Dso::Demangle(const std::string& name) {
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700272 if (!demangle_) {
273 return name;
274 }
275 int status;
276 bool is_linker_symbol = (name.find(linker_prefix) == 0);
277 const char* mangled_str = name.c_str();
278 if (is_linker_symbol) {
279 mangled_str += linker_prefix.size();
280 }
281 std::string result = name;
282 char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status);
283 if (status == 0) {
284 if (is_linker_symbol) {
285 result = std::string("[linker]") + demangled_name;
286 } else {
287 result = demangled_name;
288 }
289 free(demangled_name);
290 } else if (is_linker_symbol) {
291 result = std::string("[linker]") + mangled_str;
292 }
293 return result;
294}
295
Yabin Cuic8485602015-08-20 15:04:39 -0700296bool Dso::SetSymFsDir(const std::string& symfs_dir) {
Yabin Cui40b70ff2018-04-09 14:06:08 -0700297 return debug_elf_file_finder_.SetSymFsDir(symfs_dir);
Yabin Cuic8485602015-08-20 15:04:39 -0700298}
299
Yabin Cui3939b9d2018-07-20 17:12:13 -0700300bool Dso::AddSymbolDir(const std::string& symbol_dir) {
301 return debug_elf_file_finder_.AddSymbolDir(symbol_dir);
302}
303
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200304void Dso::SetVmlinux(const std::string& vmlinux) {
305 vmlinux_ = vmlinux;
306}
Yabin Cuic8485602015-08-20 15:04:39 -0700307
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200308void Dso::SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids) {
Yabin Cuic8485602015-08-20 15:04:39 -0700309 std::unordered_map<std::string, BuildId> map;
310 for (auto& pair : build_ids) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200311 LOG(DEBUG) << "build_id_map: " << pair.first << ", " << pair.second.ToString();
Yabin Cuic8485602015-08-20 15:04:39 -0700312 map.insert(pair);
313 }
314 build_id_map_ = std::move(map);
315}
316
Yabin Cuic68e66d2018-03-07 15:47:15 -0800317void Dso::SetVdsoFile(const std::string& vdso_file, bool is_64bit) {
Yabin Cui40b70ff2018-04-09 14:06:08 -0700318 debug_elf_file_finder_.SetVdsoFile(vdso_file, is_64bit);
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700319}
320
Yabin Cui52c63692016-11-28 17:28:08 -0800321BuildId Dso::FindExpectedBuildIdForPath(const std::string& path) {
322 auto it = build_id_map_.find(path);
Yabin Cuic8485602015-08-20 15:04:39 -0700323 if (it != build_id_map_.end()) {
324 return it->second;
325 }
326 return BuildId();
327}
328
Yabin Cui11424c42022-03-10 16:04:04 -0800329BuildId Dso::GetExpectedBuildId() const {
Yabin Cui52c63692016-11-28 17:28:08 -0800330 return FindExpectedBuildIdForPath(path_);
331}
332
Yabin Cui11424c42022-03-10 16:04:04 -0800333Dso::Dso(DsoType type, const std::string& path)
Yabin Cui767dd172016-06-02 21:02:43 -0700334 : type_(type),
Yabin Cui767dd172016-06-02 21:02:43 -0700335 path_(path),
Yabin Cui767dd172016-06-02 21:02:43 -0700336 is_loaded_(false),
Yabin Cui16501ff2016-10-19 15:06:29 -0700337 dump_id_(UINT_MAX),
Yabin Cuie466d4d2017-08-11 17:03:07 -0700338 symbol_dump_id_(0),
339 symbol_warning_loglevel_(android::base::WARNING) {
Yabin Cui15475e62016-07-14 13:26:19 -0700340 size_t pos = path.find_last_of("/\\");
341 if (pos != std::string::npos) {
342 file_name_ = path.substr(pos + 1);
343 } else {
344 file_name_ = path;
345 }
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700346 dso_count_++;
Yabin Cuic8485602015-08-20 15:04:39 -0700347}
348
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700349Dso::~Dso() {
350 if (--dso_count_ == 0) {
Yabin Cuib4212972016-05-25 14:08:05 -0700351 // Clean up global variables when no longer used.
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700352 symbol_name_allocator.Clear();
Yabin Cuib4212972016-05-25 14:08:05 -0700353 demangle_ = true;
Yabin Cuib4212972016-05-25 14:08:05 -0700354 vmlinux_.clear();
355 kallsyms_.clear();
356 build_id_map_.clear();
Yabin Cui16501ff2016-10-19 15:06:29 -0700357 g_dump_id_ = 0;
Yabin Cui40b70ff2018-04-09 14:06:08 -0700358 debug_elf_file_finder_.Reset();
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700359 }
360}
361
Yabin Cui16501ff2016-10-19 15:06:29 -0700362uint32_t Dso::CreateDumpId() {
363 CHECK(!HasDumpId());
364 return dump_id_ = g_dump_id_++;
365}
366
367uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) {
368 CHECK(!symbol->HasDumpId());
369 symbol->dump_id_ = symbol_dump_id_++;
370 return symbol->dump_id_;
371}
372
Yabin Cui7078c672020-11-10 16:24:12 -0800373std::optional<uint64_t> Dso::IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) {
374 return ip - map_start + map_pgoff;
375}
376
Yabin Cui547c60e2015-10-12 16:56:05 -0700377const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) {
Yabin Cuic8485602015-08-20 15:04:39 -0700378 if (!is_loaded_) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800379 LoadSymbols();
Yabin Cuic5b4a312016-10-24 13:38:38 -0700380 }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800381 auto it = std::upper_bound(symbols_.begin(), symbols_.end(), vaddr_in_dso, CompareAddrToSymbol);
Yabin Cui516a87c2018-03-26 17:34:00 -0700382 if (it != symbols_.begin()) {
383 --it;
384 if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) {
385 return &*it;
Yabin Cuic8485602015-08-20 15:04:39 -0700386 }
387 }
Yabin Cuic5b4a312016-10-24 13:38:38 -0700388 if (!unknown_symbols_.empty()) {
389 auto it = unknown_symbols_.find(vaddr_in_dso);
390 if (it != unknown_symbols_.end()) {
391 return &it->second;
Yabin Cuic8485602015-08-20 15:04:39 -0700392 }
393 }
394 return nullptr;
395}
396
Yabin Cuic5b4a312016-10-24 13:38:38 -0700397void Dso::SetSymbols(std::vector<Symbol>* symbols) {
398 symbols_ = std::move(*symbols);
399 symbols->clear();
400}
401
402void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) {
403 unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1)));
404}
405
Yabin Cuiac4b2492020-12-09 16:27:57 -0800406bool Dso::IsForJavaMethod() const {
Yabin Cui10bbd842018-08-13 17:42:25 -0700407 if (type_ == DSO_DEX_FILE) {
408 return true;
409 }
410 if (type_ == DSO_ELF_FILE) {
Yabin Cui9ba4d942020-09-08 16:12:46 -0700411 if (JITDebugReader::IsPathInJITSymFile(path_)) {
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700412 return true;
413 }
Yabin Cui9ba4d942020-09-08 16:12:46 -0700414 // JITDebugReader in old versions generates symfiles in 'TemporaryFile-XXXXXX'.
415 size_t pos = path_.rfind('/');
416 pos = (pos == std::string::npos) ? 0 : pos + 1;
417 return StartsWith(std::string_view(&path_[pos], path_.size() - pos), "TemporaryFile");
Yabin Cui10bbd842018-08-13 17:42:25 -0700418 }
419 return false;
420}
421
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800422void Dso::LoadSymbols() {
423 if (!is_loaded_) {
424 is_loaded_ = true;
425 std::vector<Symbol> symbols = LoadSymbolsImpl();
426 if (symbols_.empty()) {
427 symbols_ = std::move(symbols);
428 } else {
429 std::vector<Symbol> merged_symbols;
430 std::set_union(symbols_.begin(), symbols_.end(), symbols.begin(), symbols.end(),
431 std::back_inserter(merged_symbols), Symbol::CompareValueByAddr);
432 symbols_ = std::move(merged_symbols);
433 }
Yabin Cuic8485602015-08-20 15:04:39 -0700434 }
Yabin Cuiba50c4b2015-07-21 11:24:48 -0700435}
436
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200437static void ReportReadElfSymbolResult(
438 ElfStatus result, const std::string& path, const std::string& debug_file_path,
Yabin Cui516a87c2018-03-26 17:34:00 -0700439 android::base::LogSeverity warning_loglevel = android::base::WARNING) {
Yabin Cuidec43c12016-07-29 16:40:40 -0700440 if (result == ElfStatus::NO_ERROR) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700441 LOG(VERBOSE) << "Read symbols from " << debug_file_path << " successfully";
Yabin Cuidec43c12016-07-29 16:40:40 -0700442 } else if (result == ElfStatus::NO_SYMBOL_TABLE) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700443 if (path == "[vdso]") {
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700444 // Vdso only contains dynamic symbol table, and we can't change that.
Yabin Cui516a87c2018-03-26 17:34:00 -0700445 return;
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700446 }
Yabin Cuidec43c12016-07-29 16:40:40 -0700447 // Lacking symbol table isn't considered as an error but worth reporting.
Yabin Cui516a87c2018-03-26 17:34:00 -0700448 LOG(warning_loglevel) << debug_file_path << " doesn't contain symbol table";
Yabin Cuidec43c12016-07-29 16:40:40 -0700449 } else {
Yabin Cui516a87c2018-03-26 17:34:00 -0700450 LOG(warning_loglevel) << "failed to read symbols from " << debug_file_path << ": " << result;
Yabin Cuidec43c12016-07-29 16:40:40 -0700451 }
452}
453
Yabin Cui516a87c2018-03-26 17:34:00 -0700454static void SortAndFixSymbols(std::vector<Symbol>& symbols) {
455 std::sort(symbols.begin(), symbols.end(), Symbol::CompareValueByAddr);
Yabin Cuic8485602015-08-20 15:04:39 -0700456 Symbol* prev_symbol = nullptr;
Yabin Cui516a87c2018-03-26 17:34:00 -0700457 for (auto& symbol : symbols) {
Yabin Cuic8485602015-08-20 15:04:39 -0700458 if (prev_symbol != nullptr && prev_symbol->len == 0) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700459 prev_symbol->len = symbol.addr - prev_symbol->addr;
Yabin Cuic8485602015-08-20 15:04:39 -0700460 }
Yabin Cui3d4aa262017-11-01 15:58:55 -0700461 prev_symbol = &symbol;
Yabin Cui638c5582015-07-01 16:16:57 -0700462 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700463}
464
Yabin Cuidd401b32018-04-11 11:17:06 -0700465class DexFileDso : public Dso {
466 public:
Yabin Cui11424c42022-03-10 16:04:04 -0800467 DexFileDso(const std::string& path) : Dso(DSO_DEX_FILE, path) {}
Yabin Cuidd401b32018-04-11 11:17:06 -0700468
469 void AddDexFileOffset(uint64_t dex_file_offset) override {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200470 auto it = std::lower_bound(dex_file_offsets_.begin(), dex_file_offsets_.end(), dex_file_offset);
Yabin Cuic8571d42018-06-06 11:20:39 -0700471 if (it != dex_file_offsets_.end() && *it == dex_file_offset) {
472 return;
473 }
474 dex_file_offsets_.insert(it, dex_file_offset);
Yabin Cuidd401b32018-04-11 11:17:06 -0700475 }
476
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200477 const std::vector<uint64_t>* DexFileOffsets() override { return &dex_file_offsets_; }
Yabin Cuidd401b32018-04-11 11:17:06 -0700478
Yabin Cuidb2c4932019-02-07 15:06:42 -0800479 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override {
480 return ip - map_start + map_pgoff;
481 }
482
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800483 std::vector<Symbol> LoadSymbolsImpl() override {
Yabin Cuidd401b32018-04-11 11:17:06 -0700484 std::vector<Symbol> symbols;
Yabin Cui11424c42022-03-10 16:04:04 -0800485 const std::string& debug_file_path = GetDebugFilePath();
486 auto tuple = SplitUrlInApk(debug_file_path);
Yabin Cui83b0e122021-11-03 14:10:10 -0700487 // Symbols of dex files are collected on device. If the dex file doesn't exist, probably
488 // we are reporting on host, and there is no need to report warning of missing dex files.
Yabin Cui11424c42022-03-10 16:04:04 -0800489 if (!IsRegularFile(std::get<0>(tuple) ? std::get<1>(tuple) : debug_file_path)) {
490 LOG(DEBUG) << "skip reading symbols from non-exist dex_file " << debug_file_path;
Yabin Cui83b0e122021-11-03 14:10:10 -0700491 return symbols;
492 }
Yabin Cui2a53ff32018-05-21 17:37:00 -0700493 bool status = false;
David Srbecky6a296b62021-04-15 20:52:22 +0100494 auto symbol_callback = [&](DexFileSymbol* symbol) {
495 symbols.emplace_back(symbol->name, symbol->addr, symbol->size);
Yabin Cui710f3722021-03-23 17:45:39 -0700496 };
Yabin Cui2a53ff32018-05-21 17:37:00 -0700497 if (std::get<0>(tuple)) {
498 std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(std::get<1>(tuple));
499 ZipEntry entry;
500 std::vector<uint8_t> data;
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200501 if (ahelper && ahelper->FindEntry(std::get<2>(tuple), &entry) &&
502 ahelper->GetEntryData(entry, &data)) {
Yabin Cui11424c42022-03-10 16:04:04 -0800503 status = ReadSymbolsFromDexFileInMemory(data.data(), data.size(), debug_file_path,
Yabin Cui17769f22021-07-13 16:39:16 -0700504 dex_file_offsets_, symbol_callback);
Yabin Cui2a53ff32018-05-21 17:37:00 -0700505 }
506 } else {
Yabin Cui11424c42022-03-10 16:04:04 -0800507 status = ReadSymbolsFromDexFile(debug_file_path, dex_file_offsets_, symbol_callback);
Yabin Cui2a53ff32018-05-21 17:37:00 -0700508 }
509 if (!status) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200510 android::base::LogSeverity level =
511 symbols_.empty() ? android::base::WARNING : android::base::DEBUG;
Yabin Cui11424c42022-03-10 16:04:04 -0800512 LOG(level) << "Failed to read symbols from dex_file " << debug_file_path;
Yabin Cuidd401b32018-04-11 11:17:06 -0700513 return symbols;
514 }
Yabin Cui11424c42022-03-10 16:04:04 -0800515 LOG(VERBOSE) << "Read symbols from dex_file " << debug_file_path << " successfully";
Yabin Cuidd401b32018-04-11 11:17:06 -0700516 SortAndFixSymbols(symbols);
517 return symbols;
518 }
519
520 private:
521 std::vector<uint64_t> dex_file_offsets_;
522};
523
Yabin Cui516a87c2018-03-26 17:34:00 -0700524class ElfDso : public Dso {
525 public:
Yabin Cui11424c42022-03-10 16:04:04 -0800526 ElfDso(const std::string& path, bool force_64bit)
527 : Dso(DSO_ELF_FILE, path), force_64bit_(force_64bit) {}
Yabin Cui516a87c2018-03-26 17:34:00 -0700528
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700529 std::string_view GetReportPath() const override {
Yabin Cui9ba4d942020-09-08 16:12:46 -0700530 if (JITDebugReader::IsPathInJITSymFile(path_)) {
531 if (path_.find(kJITAppCacheFile) != path_.npos) {
Yabin Cui075dd182020-08-05 19:51:36 +0000532 return "[JIT app cache]";
533 }
Yabin Cui9ba4d942020-09-08 16:12:46 -0700534 return "[JIT zygote cache]";
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700535 }
536 return path_;
537 }
538
Yabin Cuidb2c4932019-02-07 15:06:42 -0800539 void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t file_offset) override {
540 min_vaddr_ = min_vaddr;
541 file_offset_of_min_vaddr_ = file_offset;
Yabin Cuic8485602015-08-20 15:04:39 -0700542 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700543
Yabin Cuidb2c4932019-02-07 15:06:42 -0800544 void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* file_offset) override {
545 if (type_ == DSO_DEX_FILE) {
546 return dex_file_dso_->GetMinExecutableVaddr(min_vaddr, file_offset);
547 }
548 if (min_vaddr_ == uninitialized_value) {
549 min_vaddr_ = 0;
550 BuildId build_id = GetExpectedBuildId();
Yabin Cui90c3b302020-07-01 10:09:16 -0700551
552 ElfStatus status;
Yabin Cui11424c42022-03-10 16:04:04 -0800553 auto elf = ElfFile::Open(GetDebugFilePath(), &build_id, &status);
Yabin Cui90c3b302020-07-01 10:09:16 -0700554 if (elf) {
555 min_vaddr_ = elf->ReadMinExecutableVaddr(&file_offset_of_min_vaddr_);
Yabin Cuidb2c4932019-02-07 15:06:42 -0800556 } else {
Yabin Cui11424c42022-03-10 16:04:04 -0800557 LOG(WARNING) << "failed to read min virtual address of " << GetDebugFilePath() << ": "
Yabin Cui90c3b302020-07-01 10:09:16 -0700558 << status;
Yabin Cuidb2c4932019-02-07 15:06:42 -0800559 }
560 }
561 *min_vaddr = min_vaddr_;
562 *file_offset = file_offset_of_min_vaddr_;
563 }
564
565 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override {
566 if (type_ == DSO_DEX_FILE) {
567 return dex_file_dso_->IpToVaddrInFile(ip, map_start, map_pgoff);
568 }
569 uint64_t min_vaddr;
570 uint64_t file_offset_of_min_vaddr;
571 GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
572 if (file_offset_of_min_vaddr == uninitialized_value) {
573 return ip - map_start + min_vaddr;
574 }
575 // Apps may make part of the executable segment of a shared library writeable, which can
576 // generate multiple executable segments at runtime. So use map_pgoff to calculate
577 // vaddr_in_file.
578 return ip - map_start + map_pgoff - file_offset_of_min_vaddr + min_vaddr;
Yabin Cui516a87c2018-03-26 17:34:00 -0700579 }
580
Yabin Cuidd401b32018-04-11 11:17:06 -0700581 void AddDexFileOffset(uint64_t dex_file_offset) override {
582 if (type_ == DSO_ELF_FILE) {
583 // When simpleperf does unwinding while recording, it processes mmap records before reading
584 // dex file linked list (via JITDebugReader). To process mmap records, it creates Dso
585 // objects of type ELF_FILE. Then after reading dex file linked list, it realizes some
586 // ELF_FILE Dso objects should actually be DEX_FILE, because they have dex file offsets.
587 // So here converts ELF_FILE Dso into DEX_FILE Dso.
588 type_ = DSO_DEX_FILE;
Yabin Cui11424c42022-03-10 16:04:04 -0800589 dex_file_dso_.reset(new DexFileDso(path_));
Yabin Cuidd401b32018-04-11 11:17:06 -0700590 }
591 dex_file_dso_->AddDexFileOffset(dex_file_offset);
592 }
593
594 const std::vector<uint64_t>* DexFileOffsets() override {
595 return dex_file_dso_ ? dex_file_dso_->DexFileOffsets() : nullptr;
596 }
597
Yabin Cui516a87c2018-03-26 17:34:00 -0700598 protected:
Yabin Cui11424c42022-03-10 16:04:04 -0800599 std::string FindDebugFilePath() const override {
600 BuildId build_id = GetExpectedBuildId();
601 return debug_elf_file_finder_.FindDebugFile(path_, force_64bit_, build_id);
602 }
603
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800604 std::vector<Symbol> LoadSymbolsImpl() override {
Yabin Cuidd401b32018-04-11 11:17:06 -0700605 if (dex_file_dso_) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800606 return dex_file_dso_->LoadSymbolsImpl();
Yabin Cuidd401b32018-04-11 11:17:06 -0700607 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700608 std::vector<Symbol> symbols;
609 BuildId build_id = GetExpectedBuildId();
610 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
611 if (symbol.is_func || (symbol.is_label && symbol.is_in_text_section)) {
612 symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
613 }
614 };
615 ElfStatus status;
Yabin Cui11424c42022-03-10 16:04:04 -0800616 auto elf = ElfFile::Open(GetDebugFilePath(), &build_id, &status);
Yabin Cui01947032020-06-30 14:36:46 -0700617 if (elf) {
618 status = elf->ParseSymbols(symbol_callback);
Yabin Cui516a87c2018-03-26 17:34:00 -0700619 }
Yabin Cui11424c42022-03-10 16:04:04 -0800620 ReportReadElfSymbolResult(status, path_, GetDebugFilePath(),
Yabin Cui516a87c2018-03-26 17:34:00 -0700621 symbols_.empty() ? android::base::WARNING : android::base::DEBUG);
622 SortAndFixSymbols(symbols);
623 return symbols;
624 }
625
626 private:
Yabin Cuidb2c4932019-02-07 15:06:42 -0800627 static constexpr uint64_t uninitialized_value = std::numeric_limits<uint64_t>::max();
628
Yabin Cui11424c42022-03-10 16:04:04 -0800629 bool force_64bit_;
Yabin Cuidb2c4932019-02-07 15:06:42 -0800630 uint64_t min_vaddr_ = uninitialized_value;
631 uint64_t file_offset_of_min_vaddr_ = uninitialized_value;
Yabin Cuidd401b32018-04-11 11:17:06 -0700632 std::unique_ptr<DexFileDso> dex_file_dso_;
Yabin Cui516a87c2018-03-26 17:34:00 -0700633};
634
635class KernelDso : public Dso {
636 public:
Yabin Cui11424c42022-03-10 16:04:04 -0800637 KernelDso(const std::string& path) : Dso(DSO_KERNEL, path) {
638 debug_file_path_ = FindDebugFilePath();
Yabin Cui7078c672020-11-10 16:24:12 -0800639 if (!vmlinux_.empty()) {
640 // Use vmlinux as the kernel debug file.
641 BuildId build_id = GetExpectedBuildId();
642 ElfStatus status;
643 if (ElfFile::Open(vmlinux_, &build_id, &status)) {
644 debug_file_path_ = vmlinux_;
645 has_debug_file_ = true;
646 }
Yabin Cui11424c42022-03-10 16:04:04 -0800647 } else if (IsRegularFile(GetDebugFilePath())) {
Yabin Cui7078c672020-11-10 16:24:12 -0800648 has_debug_file_ = true;
649 }
650 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700651
Yabin Cui7078c672020-11-10 16:24:12 -0800652 // IpToVaddrInFile() and LoadSymbols() must be consistent in fixing addresses changed by kernel
653 // address space layout randomization.
654 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override {
655 if (map_start != 0 && GetKernelStartAddr() != 0) {
656 // Fix kernel addresses changed by kernel address randomization.
657 fix_kernel_address_randomization_ = true;
658 return ip - map_start + GetKernelStartAddr();
659 }
660 return ip;
661 }
662
663 std::optional<uint64_t> IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t) override {
664 if (map_start != 0 && GetKernelStartOffset() != 0) {
665 return ip - map_start + GetKernelStartOffset();
666 }
667 return std::nullopt;
668 }
Yabin Cuidb2c4932019-02-07 15:06:42 -0800669
Yabin Cui516a87c2018-03-26 17:34:00 -0700670 protected:
Yabin Cui11424c42022-03-10 16:04:04 -0800671 std::string FindDebugFilePath() const override {
672 BuildId build_id = GetExpectedBuildId();
673 return debug_elf_file_finder_.FindDebugFile(path_, false, build_id);
674 }
675
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800676 std::vector<Symbol> LoadSymbolsImpl() override {
Yabin Cui516a87c2018-03-26 17:34:00 -0700677 std::vector<Symbol> symbols;
Yabin Cui7078c672020-11-10 16:24:12 -0800678 if (has_debug_file_) {
679 ReadSymbolsFromDebugFile(&symbols);
680 }
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100681
Yabin Cui7078c672020-11-10 16:24:12 -0800682 if (symbols.empty() && !kallsyms_.empty()) {
683 ReadSymbolsFromKallsyms(kallsyms_, &symbols);
684 }
Yabin Cui36b57d92020-12-17 17:06:27 -0800685#if defined(__linux__)
Yabin Cui7078c672020-11-10 16:24:12 -0800686 if (symbols.empty()) {
687 ReadSymbolsFromProc(&symbols);
688 }
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100689#endif // defined(__linux__)
Yabin Cui7078c672020-11-10 16:24:12 -0800690 SortAndFixSymbols(symbols);
691 if (!symbols.empty()) {
692 symbols.back().len = std::numeric_limits<uint64_t>::max() - symbols.back().addr;
693 }
694 return symbols;
695 }
696
697 private:
698 void ReadSymbolsFromDebugFile(std::vector<Symbol>* symbols) {
699 if (!fix_kernel_address_randomization_) {
700 LOG(WARNING) << "Don't know how to fix addresses changed by kernel address randomization. So "
701 "symbols in "
Yabin Cui11424c42022-03-10 16:04:04 -0800702 << GetDebugFilePath() << " are not used";
Yabin Cui7078c672020-11-10 16:24:12 -0800703 return;
704 }
705 // symbols_ are kernel symbols got from /proc/kallsyms while recording. Those symbols are
706 // not fixed for kernel address randomization. So clear them to avoid mixing them with
707 // symbols in debug_file_path.
708 symbols_.clear();
709
710 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
711 if (symbol.is_func) {
712 symbols->emplace_back(symbol.name, symbol.vaddr, symbol.len);
Yabin Cui01947032020-06-30 14:36:46 -0700713 }
Yabin Cui7078c672020-11-10 16:24:12 -0800714 };
715 ElfStatus status;
Yabin Cui11424c42022-03-10 16:04:04 -0800716 if (auto elf = ElfFile::Open(GetDebugFilePath(), &status); elf) {
Yabin Cui7078c672020-11-10 16:24:12 -0800717 status = elf->ParseSymbols(symbol_callback);
718 }
Yabin Cui11424c42022-03-10 16:04:04 -0800719 ReportReadElfSymbolResult(status, path_, GetDebugFilePath());
Yabin Cui7078c672020-11-10 16:24:12 -0800720 }
721
722 void ReadSymbolsFromKallsyms(std::string& kallsyms, std::vector<Symbol>* symbols) {
723 auto symbol_callback = [&](const KernelSymbol& symbol) {
724 if (strchr("TtWw", symbol.type) && symbol.addr != 0u) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800725 if (symbol.module == nullptr) {
726 symbols->emplace_back(symbol.name, symbol.addr, 0);
727 } else {
728 std::string name = std::string(symbol.name) + " [" + symbol.module + "]";
729 symbols->emplace_back(name, symbol.addr, 0);
730 }
Yabin Cui7078c672020-11-10 16:24:12 -0800731 }
732 return false;
733 };
734 ProcessKernelSymbols(kallsyms, symbol_callback);
735 if (symbols->empty()) {
736 LOG(WARNING) << "Symbol addresses in /proc/kallsyms on device are all zero. "
737 "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible.";
738 }
739 }
740
Yabin Cui36b57d92020-12-17 17:06:27 -0800741#if defined(__linux__)
Yabin Cui7078c672020-11-10 16:24:12 -0800742 void ReadSymbolsFromProc(std::vector<Symbol>* symbols) {
743 BuildId build_id = GetExpectedBuildId();
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100744 if (!build_id.IsEmpty()) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700745 // Try /proc/kallsyms only when asked to do so, or when build id matches.
746 // Otherwise, it is likely to use /proc/kallsyms on host for perf.data recorded on device.
747 bool can_read_kallsyms = true;
748 if (!build_id.IsEmpty()) {
749 BuildId real_build_id;
750 if (!GetKernelBuildId(&real_build_id) || build_id != real_build_id) {
751 LOG(DEBUG) << "failed to read symbols from /proc/kallsyms: Build id mismatch";
752 can_read_kallsyms = false;
753 }
754 }
755 if (can_read_kallsyms) {
756 std::string kallsyms;
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100757 if (LoadKernelSymbols(&kallsyms)) {
Yabin Cui7078c672020-11-10 16:24:12 -0800758 ReadSymbolsFromKallsyms(kallsyms, symbols);
Yabin Cui516a87c2018-03-26 17:34:00 -0700759 }
760 }
761 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700762 }
Thiébaud Weksteene7e750e2020-11-19 15:07:46 +0100763#endif // defined(__linux__)
Yabin Cui516a87c2018-03-26 17:34:00 -0700764
Yabin Cui7078c672020-11-10 16:24:12 -0800765 uint64_t GetKernelStartAddr() {
766 if (!kernel_start_addr_) {
767 ParseKernelStartAddr();
Yabin Cui516a87c2018-03-26 17:34:00 -0700768 }
Yabin Cui7078c672020-11-10 16:24:12 -0800769 return kernel_start_addr_.value();
Yabin Cui516a87c2018-03-26 17:34:00 -0700770 }
Yabin Cui7078c672020-11-10 16:24:12 -0800771
772 uint64_t GetKernelStartOffset() {
773 if (!kernel_start_file_offset_) {
774 ParseKernelStartAddr();
775 }
776 return kernel_start_file_offset_.value();
777 }
778
779 void ParseKernelStartAddr() {
780 kernel_start_addr_ = 0;
781 kernel_start_file_offset_ = 0;
782 if (has_debug_file_) {
783 ElfStatus status;
Yabin Cui11424c42022-03-10 16:04:04 -0800784 if (auto elf = ElfFile::Open(GetDebugFilePath(), &status); elf) {
Yabin Cui7078c672020-11-10 16:24:12 -0800785 for (const auto& section : elf->GetSectionHeader()) {
786 if (section.name == ".text") {
787 kernel_start_addr_ = section.vaddr;
788 kernel_start_file_offset_ = section.file_offset;
789 break;
790 }
791 }
792 }
793 }
794 }
795
796 bool has_debug_file_ = false;
797 bool fix_kernel_address_randomization_ = false;
798 std::optional<uint64_t> kernel_start_addr_;
799 std::optional<uint64_t> kernel_start_file_offset_;
Yabin Cui516a87c2018-03-26 17:34:00 -0700800};
801
802class KernelModuleDso : public Dso {
803 public:
Yabin Cui11424c42022-03-10 16:04:04 -0800804 KernelModuleDso(const std::string& path, uint64_t memory_start, uint64_t memory_end,
805 Dso* kernel_dso)
806 : Dso(DSO_KERNEL_MODULE, path),
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800807 memory_start_(memory_start),
808 memory_end_(memory_end),
809 kernel_dso_(kernel_dso) {}
810
811 void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t memory_offset) override {
812 min_vaddr_ = min_vaddr;
813 memory_offset_of_min_vaddr_ = memory_offset;
814 }
815
816 void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* memory_offset) override {
817 if (!min_vaddr_) {
818 CalculateMinVaddr();
819 }
820 *min_vaddr = min_vaddr_.value();
821 *memory_offset = memory_offset_of_min_vaddr_.value();
822 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700823
Yabin Cuidb2c4932019-02-07 15:06:42 -0800824 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800825 uint64_t min_vaddr;
826 uint64_t memory_offset;
827 GetMinExecutableVaddr(&min_vaddr, &memory_offset);
828 return ip - map_start - memory_offset + min_vaddr;
Yabin Cuidb2c4932019-02-07 15:06:42 -0800829 }
830
Yabin Cui516a87c2018-03-26 17:34:00 -0700831 protected:
Yabin Cui11424c42022-03-10 16:04:04 -0800832 std::string FindDebugFilePath() const override {
833 BuildId build_id = GetExpectedBuildId();
834 return debug_elf_file_finder_.FindDebugFile(path_, false, build_id);
835 }
836
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800837 std::vector<Symbol> LoadSymbolsImpl() override {
Yabin Cui516a87c2018-03-26 17:34:00 -0700838 std::vector<Symbol> symbols;
839 BuildId build_id = GetExpectedBuildId();
840 auto symbol_callback = [&](const ElfFileSymbol& symbol) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800841 // We only know how to map ip addrs to symbols in text section.
842 if (symbol.is_in_text_section && (symbol.is_label || symbol.is_func)) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700843 symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len);
844 }
845 };
Yabin Cui01947032020-06-30 14:36:46 -0700846 ElfStatus status;
Yabin Cui11424c42022-03-10 16:04:04 -0800847 auto elf = ElfFile::Open(GetDebugFilePath(), &build_id, &status);
Yabin Cui01947032020-06-30 14:36:46 -0700848 if (elf) {
849 status = elf->ParseSymbols(symbol_callback);
850 }
Yabin Cui11424c42022-03-10 16:04:04 -0800851 ReportReadElfSymbolResult(status, path_, GetDebugFilePath(),
Yabin Cui516a87c2018-03-26 17:34:00 -0700852 symbols_.empty() ? android::base::WARNING : android::base::DEBUG);
853 SortAndFixSymbols(symbols);
854 return symbols;
855 }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800856
857 private:
858 void CalculateMinVaddr() {
859 min_vaddr_ = 0;
860 memory_offset_of_min_vaddr_ = 0;
861
862 // min_vaddr and memory_offset are used to convert an ip addr of a kernel module to its
863 // vaddr_in_file, as shown in IpToVaddrInFile(). When the kernel loads a kernel module, it
864 // puts ALLOC sections (like .plt, .text.ftrace_trampoline, .text) in memory in order. The
865 // text section may not be at the start of the module memory. To do address conversion, we
866 // need to know its relative position in the module memory. There are two ways:
867 // 1. Read the kernel module file to calculate the relative position of .text section. It
868 // is relatively complex and depends on both PLT entries and the kernel version.
869 // 2. Find a module symbol in .text section, get its address in memory from /proc/kallsyms, and
870 // its vaddr_in_file from the kernel module file. Then other symbols in .text section can be
871 // mapped in the same way.
872 // Below we use the second method.
873
874 // 1. Select a module symbol in /proc/kallsyms.
875 kernel_dso_->LoadSymbols();
876 const auto& kernel_symbols = kernel_dso_->GetSymbols();
877 auto it = std::lower_bound(kernel_symbols.begin(), kernel_symbols.end(), memory_start_,
878 CompareSymbolToAddr);
879 const Symbol* kernel_symbol = nullptr;
880 while (it != kernel_symbols.end() && it->addr < memory_end_) {
881 if (strlen(it->Name()) > 0 && it->Name()[0] != '$') {
882 kernel_symbol = &*it;
883 break;
884 }
885 ++it;
886 }
887 if (kernel_symbol == nullptr) {
888 return;
889 }
890
891 // 2. Find the symbol in .ko file.
892 std::string symbol_name = kernel_symbol->Name();
893 if (auto pos = symbol_name.rfind(' '); pos != std::string::npos) {
894 symbol_name.resize(pos);
895 }
896 LoadSymbols();
897 for (const auto& symbol : symbols_) {
898 if (symbol_name == symbol.Name()) {
899 min_vaddr_ = symbol.addr;
900 memory_offset_of_min_vaddr_ = kernel_symbol->addr - memory_start_;
901 return;
902 }
903 }
904 }
905
906 uint64_t memory_start_;
907 uint64_t memory_end_;
908 Dso* kernel_dso_;
909 std::optional<uint64_t> min_vaddr_;
910 std::optional<uint64_t> memory_offset_of_min_vaddr_;
Yabin Cui516a87c2018-03-26 17:34:00 -0700911};
912
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200913class SymbolMapFileDso : public Dso {
914 public:
Yabin Cui11424c42022-03-10 16:04:04 -0800915 SymbolMapFileDso(const std::string& path) : Dso(DSO_SYMBOL_MAP_FILE, path) {}
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200916
917 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; }
918
919 protected:
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800920 std::vector<Symbol> LoadSymbolsImpl() override { return {}; }
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200921};
922
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700923class UnknownDso : public Dso {
924 public:
Yabin Cui11424c42022-03-10 16:04:04 -0800925 UnknownDso(const std::string& path) : Dso(DSO_UNKNOWN_FILE, path) {}
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700926
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200927 uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; }
Yabin Cuidb2c4932019-02-07 15:06:42 -0800928
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700929 protected:
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800930 std::vector<Symbol> LoadSymbolsImpl() override { return std::vector<Symbol>(); }
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700931};
932
Yabin Cui516a87c2018-03-26 17:34:00 -0700933std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path,
934 bool force_64bit) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700935 switch (dso_type) {
Yabin Cui7078c672020-11-10 16:24:12 -0800936 case DSO_ELF_FILE:
Yabin Cui11424c42022-03-10 16:04:04 -0800937 return std::unique_ptr<Dso>(new ElfDso(dso_path, force_64bit));
Yabin Cui516a87c2018-03-26 17:34:00 -0700938 case DSO_KERNEL:
Yabin Cui11424c42022-03-10 16:04:04 -0800939 return std::unique_ptr<Dso>(new KernelDso(dso_path));
Yabin Cui516a87c2018-03-26 17:34:00 -0700940 case DSO_DEX_FILE:
Yabin Cui11424c42022-03-10 16:04:04 -0800941 return std::unique_ptr<Dso>(new DexFileDso(dso_path));
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200942 case DSO_SYMBOL_MAP_FILE:
943 return std::unique_ptr<Dso>(new SymbolMapFileDso(dso_path));
Yabin Cuic36ea8b2018-04-16 18:21:40 -0700944 case DSO_UNKNOWN_FILE:
945 return std::unique_ptr<Dso>(new UnknownDso(dso_path));
Yabin Cui516a87c2018-03-26 17:34:00 -0700946 default:
947 LOG(FATAL) << "Unexpected dso_type " << static_cast<int>(dso_type);
948 }
949 return nullptr;
950}
951
Yabin Cui16f41ff2021-03-23 14:58:25 -0700952std::unique_ptr<Dso> Dso::CreateDsoWithBuildId(DsoType dso_type, const std::string& dso_path,
953 BuildId& build_id) {
Yabin Cui11424c42022-03-10 16:04:04 -0800954 std::unique_ptr<Dso> dso;
Yabin Cui16f41ff2021-03-23 14:58:25 -0700955 switch (dso_type) {
956 case DSO_ELF_FILE:
Yabin Cui11424c42022-03-10 16:04:04 -0800957 dso.reset(new ElfDso(dso_path, false));
958 break;
Yabin Cui16f41ff2021-03-23 14:58:25 -0700959 case DSO_KERNEL:
Yabin Cui11424c42022-03-10 16:04:04 -0800960 dso.reset(new KernelDso(dso_path));
961 break;
Yabin Cui16f41ff2021-03-23 14:58:25 -0700962 case DSO_KERNEL_MODULE:
Yabin Cui11424c42022-03-10 16:04:04 -0800963 dso.reset(new KernelModuleDso(dso_path, 0, 0, nullptr));
964 break;
Yabin Cui16f41ff2021-03-23 14:58:25 -0700965 default:
966 LOG(FATAL) << "Unexpected dso_type " << static_cast<int>(dso_type);
Yabin Cui11424c42022-03-10 16:04:04 -0800967 return nullptr;
Yabin Cui16f41ff2021-03-23 14:58:25 -0700968 }
Yabin Cui11424c42022-03-10 16:04:04 -0800969 dso->debug_file_path_ = debug_elf_file_finder_.FindDebugFile(dso_path, false, build_id);
970 return dso;
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700971}
972
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800973std::unique_ptr<Dso> Dso::CreateKernelModuleDso(const std::string& dso_path, uint64_t memory_start,
974 uint64_t memory_end, Dso* kernel_dso) {
Yabin Cui11424c42022-03-10 16:04:04 -0800975 return std::unique_ptr<Dso>(new KernelModuleDso(dso_path, memory_start, memory_end, kernel_dso));
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800976}
977
Yabin Cui767dd172016-06-02 21:02:43 -0700978const char* DsoTypeToString(DsoType dso_type) {
979 switch (dso_type) {
980 case DSO_KERNEL:
981 return "dso_kernel";
982 case DSO_KERNEL_MODULE:
983 return "dso_kernel_module";
984 case DSO_ELF_FILE:
985 return "dso_elf_file";
Yabin Cui516a87c2018-03-26 17:34:00 -0700986 case DSO_DEX_FILE:
987 return "dso_dex_file";
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200988 case DSO_SYMBOL_MAP_FILE:
989 return "dso_symbol_map_file";
Yabin Cui767dd172016-06-02 21:02:43 -0700990 default:
991 return "unknown";
992 }
993}
Yabin Cui40b70ff2018-04-09 14:06:08 -0700994
995bool GetBuildIdFromDsoPath(const std::string& dso_path, BuildId* build_id) {
Yabin Cui3a880452020-06-29 16:37:31 -0700996 ElfStatus status;
997 auto elf = ElfFile::Open(dso_path, &status);
998 if (status == ElfStatus::NO_ERROR && elf->GetBuildId(build_id) == ElfStatus::NO_ERROR) {
999 return true;
Yabin Cui40b70ff2018-04-09 14:06:08 -07001000 }
Yabin Cui3a880452020-06-29 16:37:31 -07001001 return false;
Yabin Cui40b70ff2018-04-09 14:06:08 -07001002}
Yabin Cuifaa7b922021-01-11 17:35:57 -08001003
1004} // namespace simpleperf