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