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