| Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 1 | /* |
| 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 Cui | b378355 | 2015-06-11 11:15:42 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 20 | #include <string.h> |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 21 | |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 22 | #include <algorithm> |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 23 | #include <limits> |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 24 | #include <memory> |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 25 | #include <optional> |
| Yabin Cui | 9ba4d94 | 2020-09-08 16:12:46 -0700 | [diff] [blame] | 26 | #include <string_view> |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 27 | #include <vector> |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 28 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
| Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 30 | #include <android-base/logging.h> |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 31 | #include <android-base/strings.h> |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 32 | |
| Yabin Cui | 075dd18 | 2020-08-05 19:51:36 +0000 | [diff] [blame] | 33 | #include "JITDebugReader.h" |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 34 | #include "environment.h" |
| Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 35 | #include "kallsyms.h" |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 36 | #include "read_apk.h" |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 37 | #include "read_dex_file.h" |
| Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 38 | #include "read_elf.h" |
| Yabin Cui | b378355 | 2015-06-11 11:15:42 -0700 | [diff] [blame] | 39 | #include "utils.h" |
| Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 40 | |
| Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 41 | namespace simpleperf { |
| 42 | |
| Yabin Cui | 075dd18 | 2020-08-05 19:51:36 +0000 | [diff] [blame] | 43 | using android::base::EndsWith; |
| Yabin Cui | 9ba4d94 | 2020-09-08 16:12:46 -0700 | [diff] [blame] | 44 | using android::base::StartsWith; |
| Yabin Cui | 3a88045 | 2020-06-29 16:37:31 -0700 | [diff] [blame] | 45 | |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 46 | namespace simpleperf_dso_impl { |
| 47 | |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 48 | std::string RemovePathSeparatorSuffix(const std::string& path) { |
| 49 | // Don't remove path separator suffix for '/'. |
| Yabin Cui | 075dd18 | 2020-08-05 19:51:36 +0000 | [diff] [blame] | 50 | if (EndsWith(path, OS_PATH_SEPARATOR) && path.size() > 1u) { |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 51 | return path.substr(0, path.size() - 1); |
| 52 | } |
| 53 | return path; |
| 54 | } |
| 55 | |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 56 | void DebugElfFileFinder::Reset() { |
| 57 | vdso_64bit_.clear(); |
| 58 | vdso_32bit_.clear(); |
| 59 | symfs_dir_.clear(); |
| 60 | build_id_to_file_map_.clear(); |
| 61 | } |
| 62 | |
| 63 | bool DebugElfFileFinder::SetSymFsDir(const std::string& symfs_dir) { |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 64 | symfs_dir_ = RemovePathSeparatorSuffix(symfs_dir); |
| 65 | if (!IsDir(symfs_dir_)) { |
| 66 | LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir_ << "'"; |
| 67 | return false; |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 68 | } |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 69 | std::string build_id_list_file = symfs_dir_ + OS_PATH_SEPARATOR + "build_id_list"; |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 70 | 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 Cui | 2969a9e | 2018-04-19 17:06:24 -0700 | [diff] [blame] | 73 | std::vector<std::string> items = android::base::Split(line, "="); |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 74 | if (items.size() == 2u) { |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 75 | build_id_to_file_map_[items[0]] = symfs_dir_ + OS_PATH_SEPARATOR + items[1]; |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 82 | bool 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 Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 87 | std::string dir = RemovePathSeparatorSuffix(symbol_dir); |
| Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 88 | CollectBuildIdInDir(dir); |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | void DebugElfFileFinder::CollectBuildIdInDir(const std::string& dir) { |
| 93 | for (const std::string& entry : GetEntriesInDir(dir)) { |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 94 | std::string path = dir + OS_PATH_SEPARATOR + entry; |
| Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 95 | if (IsDir(path)) { |
| 96 | CollectBuildIdInDir(path); |
| 97 | } else { |
| 98 | BuildId build_id; |
| Yabin Cui | 3a88045 | 2020-06-29 16:37:31 -0700 | [diff] [blame] | 99 | ElfStatus status; |
| 100 | auto elf = ElfFile::Open(path, &status); |
| 101 | if (status == ElfStatus::NO_ERROR && elf->GetBuildId(&build_id) == ElfStatus::NO_ERROR) { |
| Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 102 | build_id_to_file_map_[build_id.ToString()] = path; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 108 | void 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 Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 116 | static 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 Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 139 | std::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 Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 147 | } |
| Yabin Cui | d347bb4 | 2019-11-14 15:24:07 -0800 | [diff] [blame] | 148 | 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 Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 152 | |
| Yabin Cui | 5d269c7 | 2019-05-31 15:30:17 -0700 | [diff] [blame] | 153 | // 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 Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 157 | if (it != build_id_to_file_map_.end() && CheckDebugFilePath(it->second, build_id, false)) { |
| Yabin Cui | 5d269c7 | 2019-05-31 15:30:17 -0700 | [diff] [blame] | 158 | return it->second; |
| 159 | } |
| 160 | } |
| 161 | } |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 162 | if (!symfs_dir_.empty()) { |
| Yabin Cui | a4496ad | 2019-11-18 16:40:28 -0800 | [diff] [blame] | 163 | // 2. Try concatenating symfs_dir and dso_path. |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 164 | std::string path = GetPathInSymFsDir(dso_path); |
| Yabin Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 165 | if (CheckDebugFilePath(path, build_id, true)) { |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 166 | return path; |
| 167 | } |
| Yabin Cui | a4496ad | 2019-11-18 16:40:28 -0800 | [diff] [blame] | 168 | // 3. Try concatenating symfs_dir and basename of dso_path. |
| 169 | path = symfs_dir_ + OS_PATH_SEPARATOR + android::base::Basename(dso_path); |
| Yabin Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 170 | if (CheckDebugFilePath(path, build_id, false)) { |
| Yabin Cui | a4496ad | 2019-11-18 16:40:28 -0800 | [diff] [blame] | 171 | return path; |
| 172 | } |
| Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 173 | } |
| Yabin Cui | a4496ad | 2019-11-18 16:40:28 -0800 | [diff] [blame] | 174 | // 4. Try concatenating /usr/lib/debug and dso_path. |
| Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 175 | // Linux host can store debug shared libraries in /usr/lib/debug. |
| Yabin Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 176 | if (CheckDebugFilePath("/usr/lib/debug" + dso_path, build_id, false)) { |
| Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 177 | return "/usr/lib/debug" + dso_path; |
| 178 | } |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 179 | return dso_path; |
| 180 | } |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 181 | |
| 182 | std::string DebugElfFileFinder::GetPathInSymFsDir(const std::string& path) { |
| 183 | auto add_symfs_prefix = [&](const std::string& path) { |
| Yabin Cui | 9ba4d94 | 2020-09-08 16:12:46 -0700 | [diff] [blame] | 184 | if (StartsWith(path, OS_PATH_SEPARATOR)) { |
| Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 185 | return symfs_dir_ + path; |
| 186 | } |
| 187 | return symfs_dir_ + OS_PATH_SEPARATOR + path; |
| 188 | }; |
| 189 | if (OS_PATH_SEPARATOR == '/') { |
| 190 | return add_symfs_prefix(path); |
| 191 | } |
| 192 | // Paths in recorded perf.data uses '/' as path separator. When reporting on Windows, it needs |
| 193 | // to be converted to '\\'. |
| 194 | auto tuple = SplitUrlInApk(path); |
| 195 | if (std::get<0>(tuple)) { |
| 196 | std::string apk_path = std::get<1>(tuple); |
| 197 | std::string entry_path = std::get<2>(tuple); |
| 198 | std::replace(apk_path.begin(), apk_path.end(), '/', OS_PATH_SEPARATOR); |
| 199 | return GetUrlInApk(add_symfs_prefix(apk_path), entry_path); |
| 200 | } |
| 201 | std::string elf_path = path; |
| 202 | std::replace(elf_path.begin(), elf_path.end(), '/', OS_PATH_SEPARATOR); |
| 203 | return add_symfs_prefix(elf_path); |
| 204 | } |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 205 | } // namespace simpleperf_dso_impl |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 206 | |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 207 | static OneTimeFreeAllocator symbol_name_allocator; |
| 208 | |
| Martin Stjernholm | 7c27cc2 | 2018-11-28 00:46:00 +0000 | [diff] [blame] | 209 | Symbol::Symbol(std::string_view name, uint64_t addr, uint64_t len) |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 210 | : addr(addr), |
| 211 | len(len), |
| 212 | name_(symbol_name_allocator.AllocateString(name)), |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 213 | demangled_name_(nullptr), |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 214 | dump_id_(UINT_MAX) {} |
| Yabin Cui | b10a8fb | 2015-08-18 16:32:18 -0700 | [diff] [blame] | 215 | |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 216 | const char* Symbol::DemangledName() const { |
| 217 | if (demangled_name_ == nullptr) { |
| 218 | const std::string s = Dso::Demangle(name_); |
| 219 | if (s == name_) { |
| 220 | demangled_name_ = name_; |
| 221 | } else { |
| 222 | demangled_name_ = symbol_name_allocator.AllocateString(s); |
| 223 | } |
| 224 | } |
| 225 | return demangled_name_; |
| Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 228 | static bool CompareSymbolToAddr(const Symbol& s, uint64_t addr) { |
| 229 | return s.addr < addr; |
| 230 | } |
| 231 | |
| 232 | static bool CompareAddrToSymbol(uint64_t addr, const Symbol& s) { |
| 233 | return addr < s.addr; |
| 234 | } |
| 235 | |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 236 | bool Dso::demangle_ = true; |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 237 | std::string Dso::vmlinux_; |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 238 | std::string Dso::kallsyms_; |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 239 | std::unordered_map<std::string, BuildId> Dso::build_id_map_; |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 240 | size_t Dso::dso_count_; |
| Yabin Cui | 16501ff | 2016-10-19 15:06:29 -0700 | [diff] [blame] | 241 | uint32_t Dso::g_dump_id_; |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 242 | simpleperf_dso_impl::DebugElfFileFinder Dso::debug_elf_file_finder_; |
| Yabin Cui | ba50c4b | 2015-07-21 11:24:48 -0700 | [diff] [blame] | 243 | |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 244 | void Dso::SetDemangle(bool demangle) { |
| 245 | demangle_ = demangle; |
| 246 | } |
| Yabin Cui | b378355 | 2015-06-11 11:15:42 -0700 | [diff] [blame] | 247 | |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 248 | extern "C" char* __cxa_demangle(const char* mangled_name, char* buf, size_t* n, int* status); |
| Yabin Cui | b10a8fb | 2015-08-18 16:32:18 -0700 | [diff] [blame] | 249 | |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 250 | std::string Dso::Demangle(const std::string& name) { |
| Yabin Cui | b10a8fb | 2015-08-18 16:32:18 -0700 | [diff] [blame] | 251 | if (!demangle_) { |
| 252 | return name; |
| 253 | } |
| 254 | int status; |
| 255 | bool is_linker_symbol = (name.find(linker_prefix) == 0); |
| 256 | const char* mangled_str = name.c_str(); |
| 257 | if (is_linker_symbol) { |
| 258 | mangled_str += linker_prefix.size(); |
| 259 | } |
| 260 | std::string result = name; |
| 261 | char* demangled_name = __cxa_demangle(mangled_str, nullptr, nullptr, &status); |
| 262 | if (status == 0) { |
| 263 | if (is_linker_symbol) { |
| 264 | result = std::string("[linker]") + demangled_name; |
| 265 | } else { |
| 266 | result = demangled_name; |
| 267 | } |
| 268 | free(demangled_name); |
| 269 | } else if (is_linker_symbol) { |
| 270 | result = std::string("[linker]") + mangled_str; |
| 271 | } |
| 272 | return result; |
| 273 | } |
| 274 | |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 275 | bool Dso::SetSymFsDir(const std::string& symfs_dir) { |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 276 | return debug_elf_file_finder_.SetSymFsDir(symfs_dir); |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 279 | bool Dso::AddSymbolDir(const std::string& symbol_dir) { |
| 280 | return debug_elf_file_finder_.AddSymbolDir(symbol_dir); |
| 281 | } |
| 282 | |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 283 | void Dso::SetVmlinux(const std::string& vmlinux) { |
| 284 | vmlinux_ = vmlinux; |
| 285 | } |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 286 | |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 287 | void Dso::SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids) { |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 288 | std::unordered_map<std::string, BuildId> map; |
| 289 | for (auto& pair : build_ids) { |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 290 | LOG(DEBUG) << "build_id_map: " << pair.first << ", " << pair.second.ToString(); |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 291 | map.insert(pair); |
| 292 | } |
| 293 | build_id_map_ = std::move(map); |
| 294 | } |
| 295 | |
| Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 296 | void Dso::SetVdsoFile(const std::string& vdso_file, bool is_64bit) { |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 297 | debug_elf_file_finder_.SetVdsoFile(vdso_file, is_64bit); |
| Yabin Cui | 63a1c3d | 2017-05-19 12:57:44 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| Yabin Cui | 52c6369 | 2016-11-28 17:28:08 -0800 | [diff] [blame] | 300 | BuildId Dso::FindExpectedBuildIdForPath(const std::string& path) { |
| 301 | auto it = build_id_map_.find(path); |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 302 | if (it != build_id_map_.end()) { |
| 303 | return it->second; |
| 304 | } |
| 305 | return BuildId(); |
| 306 | } |
| 307 | |
| Yabin Cui | 52c6369 | 2016-11-28 17:28:08 -0800 | [diff] [blame] | 308 | BuildId Dso::GetExpectedBuildId() { |
| 309 | return FindExpectedBuildIdForPath(path_); |
| 310 | } |
| 311 | |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 312 | Dso::Dso(DsoType type, const std::string& path, const std::string& debug_file_path) |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 313 | : type_(type), |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 314 | path_(path), |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 315 | debug_file_path_(debug_file_path), |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 316 | is_loaded_(false), |
| Yabin Cui | 16501ff | 2016-10-19 15:06:29 -0700 | [diff] [blame] | 317 | dump_id_(UINT_MAX), |
| Yabin Cui | e466d4d | 2017-08-11 17:03:07 -0700 | [diff] [blame] | 318 | symbol_dump_id_(0), |
| 319 | symbol_warning_loglevel_(android::base::WARNING) { |
| Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 320 | size_t pos = path.find_last_of("/\\"); |
| 321 | if (pos != std::string::npos) { |
| 322 | file_name_ = path.substr(pos + 1); |
| 323 | } else { |
| 324 | file_name_ = path; |
| 325 | } |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 326 | dso_count_++; |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 329 | Dso::~Dso() { |
| 330 | if (--dso_count_ == 0) { |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 331 | // Clean up global variables when no longer used. |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 332 | symbol_name_allocator.Clear(); |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 333 | demangle_ = true; |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 334 | vmlinux_.clear(); |
| 335 | kallsyms_.clear(); |
| 336 | build_id_map_.clear(); |
| Yabin Cui | 16501ff | 2016-10-19 15:06:29 -0700 | [diff] [blame] | 337 | g_dump_id_ = 0; |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 338 | debug_elf_file_finder_.Reset(); |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| Yabin Cui | 16501ff | 2016-10-19 15:06:29 -0700 | [diff] [blame] | 342 | uint32_t Dso::CreateDumpId() { |
| 343 | CHECK(!HasDumpId()); |
| 344 | return dump_id_ = g_dump_id_++; |
| 345 | } |
| 346 | |
| 347 | uint32_t Dso::CreateSymbolDumpId(const Symbol* symbol) { |
| 348 | CHECK(!symbol->HasDumpId()); |
| 349 | symbol->dump_id_ = symbol_dump_id_++; |
| 350 | return symbol->dump_id_; |
| 351 | } |
| 352 | |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 353 | std::optional<uint64_t> Dso::IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) { |
| 354 | return ip - map_start + map_pgoff; |
| 355 | } |
| 356 | |
| Yabin Cui | 547c60e | 2015-10-12 16:56:05 -0700 | [diff] [blame] | 357 | const Symbol* Dso::FindSymbol(uint64_t vaddr_in_dso) { |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 358 | if (!is_loaded_) { |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 359 | LoadSymbols(); |
| Yabin Cui | c5b4a31 | 2016-10-24 13:38:38 -0700 | [diff] [blame] | 360 | } |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 361 | auto it = std::upper_bound(symbols_.begin(), symbols_.end(), vaddr_in_dso, CompareAddrToSymbol); |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 362 | if (it != symbols_.begin()) { |
| 363 | --it; |
| 364 | if (it->addr <= vaddr_in_dso && (it->addr + it->len > vaddr_in_dso)) { |
| 365 | return &*it; |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| Yabin Cui | c5b4a31 | 2016-10-24 13:38:38 -0700 | [diff] [blame] | 368 | if (!unknown_symbols_.empty()) { |
| 369 | auto it = unknown_symbols_.find(vaddr_in_dso); |
| 370 | if (it != unknown_symbols_.end()) { |
| 371 | return &it->second; |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | return nullptr; |
| 375 | } |
| 376 | |
| Yabin Cui | c5b4a31 | 2016-10-24 13:38:38 -0700 | [diff] [blame] | 377 | void Dso::SetSymbols(std::vector<Symbol>* symbols) { |
| 378 | symbols_ = std::move(*symbols); |
| 379 | symbols->clear(); |
| 380 | } |
| 381 | |
| 382 | void Dso::AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name) { |
| 383 | unknown_symbols_.insert(std::make_pair(vaddr_in_dso, Symbol(name, vaddr_in_dso, 1))); |
| 384 | } |
| 385 | |
| Yabin Cui | ac4b249 | 2020-12-09 16:27:57 -0800 | [diff] [blame] | 386 | bool Dso::IsForJavaMethod() const { |
| Yabin Cui | 10bbd84 | 2018-08-13 17:42:25 -0700 | [diff] [blame] | 387 | if (type_ == DSO_DEX_FILE) { |
| 388 | return true; |
| 389 | } |
| 390 | if (type_ == DSO_ELF_FILE) { |
| Yabin Cui | 9ba4d94 | 2020-09-08 16:12:46 -0700 | [diff] [blame] | 391 | if (JITDebugReader::IsPathInJITSymFile(path_)) { |
| Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 392 | return true; |
| 393 | } |
| Yabin Cui | 9ba4d94 | 2020-09-08 16:12:46 -0700 | [diff] [blame] | 394 | // JITDebugReader in old versions generates symfiles in 'TemporaryFile-XXXXXX'. |
| 395 | size_t pos = path_.rfind('/'); |
| 396 | pos = (pos == std::string::npos) ? 0 : pos + 1; |
| 397 | return StartsWith(std::string_view(&path_[pos], path_.size() - pos), "TemporaryFile"); |
| Yabin Cui | 10bbd84 | 2018-08-13 17:42:25 -0700 | [diff] [blame] | 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 402 | void Dso::LoadSymbols() { |
| 403 | if (!is_loaded_) { |
| 404 | is_loaded_ = true; |
| 405 | std::vector<Symbol> symbols = LoadSymbolsImpl(); |
| 406 | if (symbols_.empty()) { |
| 407 | symbols_ = std::move(symbols); |
| 408 | } else { |
| 409 | std::vector<Symbol> merged_symbols; |
| 410 | std::set_union(symbols_.begin(), symbols_.end(), symbols.begin(), symbols.end(), |
| 411 | std::back_inserter(merged_symbols), Symbol::CompareValueByAddr); |
| 412 | symbols_ = std::move(merged_symbols); |
| 413 | } |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 414 | } |
| Yabin Cui | ba50c4b | 2015-07-21 11:24:48 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 417 | static void ReportReadElfSymbolResult( |
| 418 | ElfStatus result, const std::string& path, const std::string& debug_file_path, |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 419 | android::base::LogSeverity warning_loglevel = android::base::WARNING) { |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 420 | if (result == ElfStatus::NO_ERROR) { |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 421 | LOG(VERBOSE) << "Read symbols from " << debug_file_path << " successfully"; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 422 | } else if (result == ElfStatus::NO_SYMBOL_TABLE) { |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 423 | if (path == "[vdso]") { |
| Yabin Cui | 63a1c3d | 2017-05-19 12:57:44 -0700 | [diff] [blame] | 424 | // Vdso only contains dynamic symbol table, and we can't change that. |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 425 | return; |
| Yabin Cui | 63a1c3d | 2017-05-19 12:57:44 -0700 | [diff] [blame] | 426 | } |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 427 | // Lacking symbol table isn't considered as an error but worth reporting. |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 428 | LOG(warning_loglevel) << debug_file_path << " doesn't contain symbol table"; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 429 | } else { |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 430 | LOG(warning_loglevel) << "failed to read symbols from " << debug_file_path << ": " << result; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 434 | static void SortAndFixSymbols(std::vector<Symbol>& symbols) { |
| 435 | std::sort(symbols.begin(), symbols.end(), Symbol::CompareValueByAddr); |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 436 | Symbol* prev_symbol = nullptr; |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 437 | for (auto& symbol : symbols) { |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 438 | if (prev_symbol != nullptr && prev_symbol->len == 0) { |
| Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 439 | prev_symbol->len = symbol.addr - prev_symbol->addr; |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 440 | } |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 441 | prev_symbol = &symbol; |
| Yabin Cui | 638c558 | 2015-07-01 16:16:57 -0700 | [diff] [blame] | 442 | } |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 445 | class DexFileDso : public Dso { |
| 446 | public: |
| 447 | DexFileDso(const std::string& path, const std::string& debug_file_path) |
| 448 | : Dso(DSO_DEX_FILE, path, debug_file_path) {} |
| 449 | |
| 450 | void AddDexFileOffset(uint64_t dex_file_offset) override { |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 451 | auto it = std::lower_bound(dex_file_offsets_.begin(), dex_file_offsets_.end(), dex_file_offset); |
| Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 452 | if (it != dex_file_offsets_.end() && *it == dex_file_offset) { |
| 453 | return; |
| 454 | } |
| 455 | dex_file_offsets_.insert(it, dex_file_offset); |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 458 | const std::vector<uint64_t>* DexFileOffsets() override { return &dex_file_offsets_; } |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 459 | |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 460 | uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override { |
| 461 | return ip - map_start + map_pgoff; |
| 462 | } |
| 463 | |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 464 | std::vector<Symbol> LoadSymbolsImpl() override { |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 465 | std::vector<Symbol> symbols; |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 466 | auto tuple = SplitUrlInApk(debug_file_path_); |
| 467 | bool status = false; |
| Yabin Cui | 710f372 | 2021-03-23 17:45:39 -0700 | [diff] [blame^] | 468 | auto symbol_callback = [&](DexFileSymbol* dex_symbol) { |
| 469 | symbols.emplace_back(std::string_view(dex_symbol->name, dex_symbol->name_size), |
| 470 | dex_symbol->addr, dex_symbol->size); |
| 471 | }; |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 472 | if (std::get<0>(tuple)) { |
| 473 | std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(std::get<1>(tuple)); |
| 474 | ZipEntry entry; |
| 475 | std::vector<uint8_t> data; |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 476 | if (ahelper && ahelper->FindEntry(std::get<2>(tuple), &entry) && |
| 477 | ahelper->GetEntryData(entry, &data)) { |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 478 | status = ReadSymbolsFromDexFileInMemory(data.data(), data.size(), dex_file_offsets_, |
| Yabin Cui | 710f372 | 2021-03-23 17:45:39 -0700 | [diff] [blame^] | 479 | symbol_callback); |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 480 | } |
| 481 | } else { |
| Yabin Cui | 710f372 | 2021-03-23 17:45:39 -0700 | [diff] [blame^] | 482 | status = ReadSymbolsFromDexFile(debug_file_path_, dex_file_offsets_, symbol_callback); |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 483 | } |
| 484 | if (!status) { |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 485 | android::base::LogSeverity level = |
| 486 | symbols_.empty() ? android::base::WARNING : android::base::DEBUG; |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 487 | LOG(level) << "Failed to read symbols from " << debug_file_path_; |
| 488 | return symbols; |
| 489 | } |
| 490 | LOG(VERBOSE) << "Read symbols from " << debug_file_path_ << " successfully"; |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 491 | SortAndFixSymbols(symbols); |
| 492 | return symbols; |
| 493 | } |
| 494 | |
| 495 | private: |
| 496 | std::vector<uint64_t> dex_file_offsets_; |
| 497 | }; |
| 498 | |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 499 | class ElfDso : public Dso { |
| 500 | public: |
| 501 | ElfDso(const std::string& path, const std::string& debug_file_path) |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 502 | : Dso(DSO_ELF_FILE, path, debug_file_path) {} |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 503 | |
| Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 504 | std::string_view GetReportPath() const override { |
| Yabin Cui | 9ba4d94 | 2020-09-08 16:12:46 -0700 | [diff] [blame] | 505 | if (JITDebugReader::IsPathInJITSymFile(path_)) { |
| 506 | if (path_.find(kJITAppCacheFile) != path_.npos) { |
| Yabin Cui | 075dd18 | 2020-08-05 19:51:36 +0000 | [diff] [blame] | 507 | return "[JIT app cache]"; |
| 508 | } |
| Yabin Cui | 9ba4d94 | 2020-09-08 16:12:46 -0700 | [diff] [blame] | 509 | return "[JIT zygote cache]"; |
| Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 510 | } |
| 511 | return path_; |
| 512 | } |
| 513 | |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 514 | void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t file_offset) override { |
| 515 | min_vaddr_ = min_vaddr; |
| 516 | file_offset_of_min_vaddr_ = file_offset; |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 517 | } |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 518 | |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 519 | void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* file_offset) override { |
| 520 | if (type_ == DSO_DEX_FILE) { |
| 521 | return dex_file_dso_->GetMinExecutableVaddr(min_vaddr, file_offset); |
| 522 | } |
| 523 | if (min_vaddr_ == uninitialized_value) { |
| 524 | min_vaddr_ = 0; |
| 525 | BuildId build_id = GetExpectedBuildId(); |
| Yabin Cui | 90c3b30 | 2020-07-01 10:09:16 -0700 | [diff] [blame] | 526 | |
| 527 | ElfStatus status; |
| 528 | auto elf = ElfFile::Open(debug_file_path_, &build_id, &status); |
| 529 | if (elf) { |
| 530 | min_vaddr_ = elf->ReadMinExecutableVaddr(&file_offset_of_min_vaddr_); |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 531 | } else { |
| Yabin Cui | 90c3b30 | 2020-07-01 10:09:16 -0700 | [diff] [blame] | 532 | LOG(WARNING) << "failed to read min virtual address of " << debug_file_path_ << ": " |
| 533 | << status; |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | *min_vaddr = min_vaddr_; |
| 537 | *file_offset = file_offset_of_min_vaddr_; |
| 538 | } |
| 539 | |
| 540 | uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t map_pgoff) override { |
| 541 | if (type_ == DSO_DEX_FILE) { |
| 542 | return dex_file_dso_->IpToVaddrInFile(ip, map_start, map_pgoff); |
| 543 | } |
| 544 | uint64_t min_vaddr; |
| 545 | uint64_t file_offset_of_min_vaddr; |
| 546 | GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr); |
| 547 | if (file_offset_of_min_vaddr == uninitialized_value) { |
| 548 | return ip - map_start + min_vaddr; |
| 549 | } |
| 550 | // Apps may make part of the executable segment of a shared library writeable, which can |
| 551 | // generate multiple executable segments at runtime. So use map_pgoff to calculate |
| 552 | // vaddr_in_file. |
| 553 | return ip - map_start + map_pgoff - file_offset_of_min_vaddr + min_vaddr; |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 556 | void AddDexFileOffset(uint64_t dex_file_offset) override { |
| 557 | if (type_ == DSO_ELF_FILE) { |
| 558 | // When simpleperf does unwinding while recording, it processes mmap records before reading |
| 559 | // dex file linked list (via JITDebugReader). To process mmap records, it creates Dso |
| 560 | // objects of type ELF_FILE. Then after reading dex file linked list, it realizes some |
| 561 | // ELF_FILE Dso objects should actually be DEX_FILE, because they have dex file offsets. |
| 562 | // So here converts ELF_FILE Dso into DEX_FILE Dso. |
| 563 | type_ = DSO_DEX_FILE; |
| 564 | dex_file_dso_.reset(new DexFileDso(path_, path_)); |
| 565 | } |
| 566 | dex_file_dso_->AddDexFileOffset(dex_file_offset); |
| 567 | } |
| 568 | |
| 569 | const std::vector<uint64_t>* DexFileOffsets() override { |
| 570 | return dex_file_dso_ ? dex_file_dso_->DexFileOffsets() : nullptr; |
| 571 | } |
| 572 | |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 573 | protected: |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 574 | std::vector<Symbol> LoadSymbolsImpl() override { |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 575 | if (dex_file_dso_) { |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 576 | return dex_file_dso_->LoadSymbolsImpl(); |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 577 | } |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 578 | std::vector<Symbol> symbols; |
| 579 | BuildId build_id = GetExpectedBuildId(); |
| 580 | auto symbol_callback = [&](const ElfFileSymbol& symbol) { |
| 581 | if (symbol.is_func || (symbol.is_label && symbol.is_in_text_section)) { |
| 582 | symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len); |
| 583 | } |
| 584 | }; |
| 585 | ElfStatus status; |
| Yabin Cui | 0194703 | 2020-06-30 14:36:46 -0700 | [diff] [blame] | 586 | auto elf = ElfFile::Open(debug_file_path_, &build_id, &status); |
| 587 | if (elf) { |
| 588 | status = elf->ParseSymbols(symbol_callback); |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 589 | } |
| 590 | ReportReadElfSymbolResult(status, path_, debug_file_path_, |
| 591 | symbols_.empty() ? android::base::WARNING : android::base::DEBUG); |
| 592 | SortAndFixSymbols(symbols); |
| 593 | return symbols; |
| 594 | } |
| 595 | |
| 596 | private: |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 597 | static constexpr uint64_t uninitialized_value = std::numeric_limits<uint64_t>::max(); |
| 598 | |
| 599 | uint64_t min_vaddr_ = uninitialized_value; |
| 600 | uint64_t file_offset_of_min_vaddr_ = uninitialized_value; |
| Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 601 | std::unique_ptr<DexFileDso> dex_file_dso_; |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 602 | }; |
| 603 | |
| 604 | class KernelDso : public Dso { |
| 605 | public: |
| 606 | KernelDso(const std::string& path, const std::string& debug_file_path) |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 607 | : Dso(DSO_KERNEL, path, debug_file_path) { |
| 608 | if (!vmlinux_.empty()) { |
| 609 | // Use vmlinux as the kernel debug file. |
| 610 | BuildId build_id = GetExpectedBuildId(); |
| 611 | ElfStatus status; |
| 612 | if (ElfFile::Open(vmlinux_, &build_id, &status)) { |
| 613 | debug_file_path_ = vmlinux_; |
| 614 | has_debug_file_ = true; |
| 615 | } |
| 616 | } else if (IsRegularFile(debug_file_path_)) { |
| 617 | has_debug_file_ = true; |
| 618 | } |
| 619 | } |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 620 | |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 621 | // IpToVaddrInFile() and LoadSymbols() must be consistent in fixing addresses changed by kernel |
| 622 | // address space layout randomization. |
| 623 | uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override { |
| 624 | if (map_start != 0 && GetKernelStartAddr() != 0) { |
| 625 | // Fix kernel addresses changed by kernel address randomization. |
| 626 | fix_kernel_address_randomization_ = true; |
| 627 | return ip - map_start + GetKernelStartAddr(); |
| 628 | } |
| 629 | return ip; |
| 630 | } |
| 631 | |
| 632 | std::optional<uint64_t> IpToFileOffset(uint64_t ip, uint64_t map_start, uint64_t) override { |
| 633 | if (map_start != 0 && GetKernelStartOffset() != 0) { |
| 634 | return ip - map_start + GetKernelStartOffset(); |
| 635 | } |
| 636 | return std::nullopt; |
| 637 | } |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 638 | |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 639 | protected: |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 640 | std::vector<Symbol> LoadSymbolsImpl() override { |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 641 | std::vector<Symbol> symbols; |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 642 | if (has_debug_file_) { |
| 643 | ReadSymbolsFromDebugFile(&symbols); |
| 644 | } |
| Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 645 | |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 646 | if (symbols.empty() && !kallsyms_.empty()) { |
| 647 | ReadSymbolsFromKallsyms(kallsyms_, &symbols); |
| 648 | } |
| Yabin Cui | 36b57d9 | 2020-12-17 17:06:27 -0800 | [diff] [blame] | 649 | #if defined(__linux__) |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 650 | if (symbols.empty()) { |
| 651 | ReadSymbolsFromProc(&symbols); |
| 652 | } |
| Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 653 | #endif // defined(__linux__) |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 654 | SortAndFixSymbols(symbols); |
| 655 | if (!symbols.empty()) { |
| 656 | symbols.back().len = std::numeric_limits<uint64_t>::max() - symbols.back().addr; |
| 657 | } |
| 658 | return symbols; |
| 659 | } |
| 660 | |
| 661 | private: |
| 662 | void ReadSymbolsFromDebugFile(std::vector<Symbol>* symbols) { |
| 663 | if (!fix_kernel_address_randomization_) { |
| 664 | LOG(WARNING) << "Don't know how to fix addresses changed by kernel address randomization. So " |
| 665 | "symbols in " |
| 666 | << debug_file_path_ << " are not used"; |
| 667 | return; |
| 668 | } |
| 669 | // symbols_ are kernel symbols got from /proc/kallsyms while recording. Those symbols are |
| 670 | // not fixed for kernel address randomization. So clear them to avoid mixing them with |
| 671 | // symbols in debug_file_path. |
| 672 | symbols_.clear(); |
| 673 | |
| 674 | auto symbol_callback = [&](const ElfFileSymbol& symbol) { |
| 675 | if (symbol.is_func) { |
| 676 | symbols->emplace_back(symbol.name, symbol.vaddr, symbol.len); |
| Yabin Cui | 0194703 | 2020-06-30 14:36:46 -0700 | [diff] [blame] | 677 | } |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 678 | }; |
| 679 | ElfStatus status; |
| 680 | if (auto elf = ElfFile::Open(debug_file_path_, &status); elf) { |
| 681 | status = elf->ParseSymbols(symbol_callback); |
| 682 | } |
| 683 | ReportReadElfSymbolResult(status, path_, debug_file_path_); |
| 684 | } |
| 685 | |
| 686 | void ReadSymbolsFromKallsyms(std::string& kallsyms, std::vector<Symbol>* symbols) { |
| 687 | auto symbol_callback = [&](const KernelSymbol& symbol) { |
| 688 | if (strchr("TtWw", symbol.type) && symbol.addr != 0u) { |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 689 | if (symbol.module == nullptr) { |
| 690 | symbols->emplace_back(symbol.name, symbol.addr, 0); |
| 691 | } else { |
| 692 | std::string name = std::string(symbol.name) + " [" + symbol.module + "]"; |
| 693 | symbols->emplace_back(name, symbol.addr, 0); |
| 694 | } |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 695 | } |
| 696 | return false; |
| 697 | }; |
| 698 | ProcessKernelSymbols(kallsyms, symbol_callback); |
| 699 | if (symbols->empty()) { |
| 700 | LOG(WARNING) << "Symbol addresses in /proc/kallsyms on device are all zero. " |
| 701 | "`echo 0 >/proc/sys/kernel/kptr_restrict` if possible."; |
| 702 | } |
| 703 | } |
| 704 | |
| Yabin Cui | 36b57d9 | 2020-12-17 17:06:27 -0800 | [diff] [blame] | 705 | #if defined(__linux__) |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 706 | void ReadSymbolsFromProc(std::vector<Symbol>* symbols) { |
| 707 | BuildId build_id = GetExpectedBuildId(); |
| Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 708 | if (!build_id.IsEmpty()) { |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 709 | // Try /proc/kallsyms only when asked to do so, or when build id matches. |
| 710 | // Otherwise, it is likely to use /proc/kallsyms on host for perf.data recorded on device. |
| 711 | bool can_read_kallsyms = true; |
| 712 | if (!build_id.IsEmpty()) { |
| 713 | BuildId real_build_id; |
| 714 | if (!GetKernelBuildId(&real_build_id) || build_id != real_build_id) { |
| 715 | LOG(DEBUG) << "failed to read symbols from /proc/kallsyms: Build id mismatch"; |
| 716 | can_read_kallsyms = false; |
| 717 | } |
| 718 | } |
| 719 | if (can_read_kallsyms) { |
| 720 | std::string kallsyms; |
| Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 721 | if (LoadKernelSymbols(&kallsyms)) { |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 722 | ReadSymbolsFromKallsyms(kallsyms, symbols); |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | } |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 726 | } |
| Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 727 | #endif // defined(__linux__) |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 728 | |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 729 | uint64_t GetKernelStartAddr() { |
| 730 | if (!kernel_start_addr_) { |
| 731 | ParseKernelStartAddr(); |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 732 | } |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 733 | return kernel_start_addr_.value(); |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 734 | } |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 735 | |
| 736 | uint64_t GetKernelStartOffset() { |
| 737 | if (!kernel_start_file_offset_) { |
| 738 | ParseKernelStartAddr(); |
| 739 | } |
| 740 | return kernel_start_file_offset_.value(); |
| 741 | } |
| 742 | |
| 743 | void ParseKernelStartAddr() { |
| 744 | kernel_start_addr_ = 0; |
| 745 | kernel_start_file_offset_ = 0; |
| 746 | if (has_debug_file_) { |
| 747 | ElfStatus status; |
| 748 | if (auto elf = ElfFile::Open(debug_file_path_, &status); elf) { |
| 749 | for (const auto& section : elf->GetSectionHeader()) { |
| 750 | if (section.name == ".text") { |
| 751 | kernel_start_addr_ = section.vaddr; |
| 752 | kernel_start_file_offset_ = section.file_offset; |
| 753 | break; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | bool has_debug_file_ = false; |
| 761 | bool fix_kernel_address_randomization_ = false; |
| 762 | std::optional<uint64_t> kernel_start_addr_; |
| 763 | std::optional<uint64_t> kernel_start_file_offset_; |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | class KernelModuleDso : public Dso { |
| 767 | public: |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 768 | KernelModuleDso(const std::string& path, const std::string& debug_file_path, |
| 769 | uint64_t memory_start, uint64_t memory_end, Dso* kernel_dso) |
| 770 | : Dso(DSO_KERNEL_MODULE, path, debug_file_path), |
| 771 | memory_start_(memory_start), |
| 772 | memory_end_(memory_end), |
| 773 | kernel_dso_(kernel_dso) {} |
| 774 | |
| 775 | void SetMinExecutableVaddr(uint64_t min_vaddr, uint64_t memory_offset) override { |
| 776 | min_vaddr_ = min_vaddr; |
| 777 | memory_offset_of_min_vaddr_ = memory_offset; |
| 778 | } |
| 779 | |
| 780 | void GetMinExecutableVaddr(uint64_t* min_vaddr, uint64_t* memory_offset) override { |
| 781 | if (!min_vaddr_) { |
| 782 | CalculateMinVaddr(); |
| 783 | } |
| 784 | *min_vaddr = min_vaddr_.value(); |
| 785 | *memory_offset = memory_offset_of_min_vaddr_.value(); |
| 786 | } |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 787 | |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 788 | uint64_t IpToVaddrInFile(uint64_t ip, uint64_t map_start, uint64_t) override { |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 789 | uint64_t min_vaddr; |
| 790 | uint64_t memory_offset; |
| 791 | GetMinExecutableVaddr(&min_vaddr, &memory_offset); |
| 792 | return ip - map_start - memory_offset + min_vaddr; |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 795 | protected: |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 796 | std::vector<Symbol> LoadSymbolsImpl() override { |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 797 | std::vector<Symbol> symbols; |
| 798 | BuildId build_id = GetExpectedBuildId(); |
| 799 | auto symbol_callback = [&](const ElfFileSymbol& symbol) { |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 800 | // We only know how to map ip addrs to symbols in text section. |
| 801 | if (symbol.is_in_text_section && (symbol.is_label || symbol.is_func)) { |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 802 | symbols.emplace_back(symbol.name, symbol.vaddr, symbol.len); |
| 803 | } |
| 804 | }; |
| Yabin Cui | 0194703 | 2020-06-30 14:36:46 -0700 | [diff] [blame] | 805 | ElfStatus status; |
| 806 | auto elf = ElfFile::Open(debug_file_path_, &build_id, &status); |
| 807 | if (elf) { |
| 808 | status = elf->ParseSymbols(symbol_callback); |
| 809 | } |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 810 | ReportReadElfSymbolResult(status, path_, debug_file_path_, |
| 811 | symbols_.empty() ? android::base::WARNING : android::base::DEBUG); |
| 812 | SortAndFixSymbols(symbols); |
| 813 | return symbols; |
| 814 | } |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 815 | |
| 816 | private: |
| 817 | void CalculateMinVaddr() { |
| 818 | min_vaddr_ = 0; |
| 819 | memory_offset_of_min_vaddr_ = 0; |
| 820 | |
| 821 | // min_vaddr and memory_offset are used to convert an ip addr of a kernel module to its |
| 822 | // vaddr_in_file, as shown in IpToVaddrInFile(). When the kernel loads a kernel module, it |
| 823 | // puts ALLOC sections (like .plt, .text.ftrace_trampoline, .text) in memory in order. The |
| 824 | // text section may not be at the start of the module memory. To do address conversion, we |
| 825 | // need to know its relative position in the module memory. There are two ways: |
| 826 | // 1. Read the kernel module file to calculate the relative position of .text section. It |
| 827 | // is relatively complex and depends on both PLT entries and the kernel version. |
| 828 | // 2. Find a module symbol in .text section, get its address in memory from /proc/kallsyms, and |
| 829 | // its vaddr_in_file from the kernel module file. Then other symbols in .text section can be |
| 830 | // mapped in the same way. |
| 831 | // Below we use the second method. |
| 832 | |
| 833 | // 1. Select a module symbol in /proc/kallsyms. |
| 834 | kernel_dso_->LoadSymbols(); |
| 835 | const auto& kernel_symbols = kernel_dso_->GetSymbols(); |
| 836 | auto it = std::lower_bound(kernel_symbols.begin(), kernel_symbols.end(), memory_start_, |
| 837 | CompareSymbolToAddr); |
| 838 | const Symbol* kernel_symbol = nullptr; |
| 839 | while (it != kernel_symbols.end() && it->addr < memory_end_) { |
| 840 | if (strlen(it->Name()) > 0 && it->Name()[0] != '$') { |
| 841 | kernel_symbol = &*it; |
| 842 | break; |
| 843 | } |
| 844 | ++it; |
| 845 | } |
| 846 | if (kernel_symbol == nullptr) { |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | // 2. Find the symbol in .ko file. |
| 851 | std::string symbol_name = kernel_symbol->Name(); |
| 852 | if (auto pos = symbol_name.rfind(' '); pos != std::string::npos) { |
| 853 | symbol_name.resize(pos); |
| 854 | } |
| 855 | LoadSymbols(); |
| 856 | for (const auto& symbol : symbols_) { |
| 857 | if (symbol_name == symbol.Name()) { |
| 858 | min_vaddr_ = symbol.addr; |
| 859 | memory_offset_of_min_vaddr_ = kernel_symbol->addr - memory_start_; |
| 860 | return; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | uint64_t memory_start_; |
| 866 | uint64_t memory_end_; |
| 867 | Dso* kernel_dso_; |
| 868 | std::optional<uint64_t> min_vaddr_; |
| 869 | std::optional<uint64_t> memory_offset_of_min_vaddr_; |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 870 | }; |
| 871 | |
| Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 872 | class SymbolMapFileDso : public Dso { |
| 873 | public: |
| 874 | SymbolMapFileDso(const std::string& path) : Dso(DSO_SYMBOL_MAP_FILE, path, path) {} |
| 875 | |
| 876 | uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; } |
| 877 | |
| 878 | protected: |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 879 | std::vector<Symbol> LoadSymbolsImpl() override { return {}; } |
| Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 880 | }; |
| 881 | |
| Yabin Cui | c36ea8b | 2018-04-16 18:21:40 -0700 | [diff] [blame] | 882 | class UnknownDso : public Dso { |
| 883 | public: |
| 884 | UnknownDso(const std::string& path) : Dso(DSO_UNKNOWN_FILE, path, path) {} |
| 885 | |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 886 | uint64_t IpToVaddrInFile(uint64_t ip, uint64_t, uint64_t) override { return ip; } |
| Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 887 | |
| Yabin Cui | c36ea8b | 2018-04-16 18:21:40 -0700 | [diff] [blame] | 888 | protected: |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 889 | std::vector<Symbol> LoadSymbolsImpl() override { return std::vector<Symbol>(); } |
| Yabin Cui | c36ea8b | 2018-04-16 18:21:40 -0700 | [diff] [blame] | 890 | }; |
| 891 | |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 892 | std::unique_ptr<Dso> Dso::CreateDso(DsoType dso_type, const std::string& dso_path, |
| 893 | bool force_64bit) { |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 894 | BuildId build_id = FindExpectedBuildIdForPath(dso_path); |
| 895 | std::string debug_path = debug_elf_file_finder_.FindDebugFile(dso_path, force_64bit, build_id); |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 896 | switch (dso_type) { |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 897 | case DSO_ELF_FILE: |
| 898 | return std::unique_ptr<Dso>(new ElfDso(dso_path, debug_path)); |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 899 | case DSO_KERNEL: |
| Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 900 | return std::unique_ptr<Dso>(new KernelDso(dso_path, debug_path)); |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 901 | case DSO_DEX_FILE: |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 902 | return std::unique_ptr<Dso>(new DexFileDso(dso_path, dso_path)); |
| Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 903 | case DSO_SYMBOL_MAP_FILE: |
| 904 | return std::unique_ptr<Dso>(new SymbolMapFileDso(dso_path)); |
| Yabin Cui | c36ea8b | 2018-04-16 18:21:40 -0700 | [diff] [blame] | 905 | case DSO_UNKNOWN_FILE: |
| 906 | return std::unique_ptr<Dso>(new UnknownDso(dso_path)); |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 907 | default: |
| 908 | LOG(FATAL) << "Unexpected dso_type " << static_cast<int>(dso_type); |
| 909 | } |
| 910 | return nullptr; |
| 911 | } |
| 912 | |
| Yabin Cui | 4ad10fb | 2020-04-01 15:45:48 -0700 | [diff] [blame] | 913 | std::unique_ptr<Dso> Dso::CreateElfDsoWithBuildId(const std::string& dso_path, BuildId& build_id) { |
| 914 | return std::unique_ptr<Dso>( |
| 915 | new ElfDso(dso_path, debug_elf_file_finder_.FindDebugFile(dso_path, false, build_id))); |
| 916 | } |
| 917 | |
| Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 918 | std::unique_ptr<Dso> Dso::CreateKernelModuleDso(const std::string& dso_path, uint64_t memory_start, |
| 919 | uint64_t memory_end, Dso* kernel_dso) { |
| 920 | BuildId build_id = FindExpectedBuildIdForPath(dso_path); |
| 921 | std::string debug_path = debug_elf_file_finder_.FindDebugFile(dso_path, false, build_id); |
| 922 | return std::unique_ptr<Dso>( |
| 923 | new KernelModuleDso(dso_path, debug_path, memory_start, memory_end, kernel_dso)); |
| 924 | } |
| 925 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 926 | const char* DsoTypeToString(DsoType dso_type) { |
| 927 | switch (dso_type) { |
| 928 | case DSO_KERNEL: |
| 929 | return "dso_kernel"; |
| 930 | case DSO_KERNEL_MODULE: |
| 931 | return "dso_kernel_module"; |
| 932 | case DSO_ELF_FILE: |
| 933 | return "dso_elf_file"; |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 934 | case DSO_DEX_FILE: |
| 935 | return "dso_dex_file"; |
| Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 936 | case DSO_SYMBOL_MAP_FILE: |
| 937 | return "dso_symbol_map_file"; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 938 | default: |
| 939 | return "unknown"; |
| 940 | } |
| 941 | } |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 942 | |
| 943 | bool GetBuildIdFromDsoPath(const std::string& dso_path, BuildId* build_id) { |
| Yabin Cui | 3a88045 | 2020-06-29 16:37:31 -0700 | [diff] [blame] | 944 | ElfStatus status; |
| 945 | auto elf = ElfFile::Open(dso_path, &status); |
| 946 | if (status == ElfStatus::NO_ERROR && elf->GetBuildId(build_id) == ElfStatus::NO_ERROR) { |
| 947 | return true; |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 948 | } |
| Yabin Cui | 3a88045 | 2020-06-29 16:37:31 -0700 | [diff] [blame] | 949 | return false; |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 950 | } |
| Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 951 | |
| 952 | } // namespace simpleperf |