Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Florian Mayer | bd88ff8 | 2019-12-18 16:36:37 +0000 | [diff] [blame] | 17 | #include "src/profiling/symbolizer/local_symbolizer.h" |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 18 | |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | |
Primiano Tucci | 58d2dc6 | 2021-06-24 16:03:24 +0100 | [diff] [blame] | 21 | #include <cinttypes> |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 22 | #include <memory> |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 23 | #include <optional> |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 24 | #include <sstream> |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "perfetto/base/build_config.h" |
| 29 | #include "perfetto/base/compiler.h" |
| 30 | #include "perfetto/base/logging.h" |
Primiano Tucci | ab293f5 | 2020-12-08 11:46:52 +0100 | [diff] [blame] | 31 | #include "perfetto/ext/base/file_utils.h" |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 32 | #include "perfetto/ext/base/scoped_file.h" |
Hector Dearman | aa02574 | 2021-01-14 13:55:22 +0000 | [diff] [blame] | 33 | #include "perfetto/ext/base/string_utils.h" |
Daniele Di Proietto | 25f8069 | 2021-09-17 10:14:41 +0000 | [diff] [blame] | 34 | #include "src/profiling/symbolizer/elf.h" |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 35 | #include "src/profiling/symbolizer/filesystem.h" |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 36 | #include "src/profiling/symbolizer/scoped_read_mmap.h" |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 37 | |
| 38 | namespace perfetto { |
| 39 | namespace profiling { |
| 40 | |
Florian Mayer | 98f7a93 | 2020-12-11 16:31:19 +0000 | [diff] [blame] | 41 | // TODO(fmayer): Fix up name. This suggests it always returns a symbolizer or |
| 42 | // dies, which isn't the case. |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 43 | std::unique_ptr<Symbolizer> LocalSymbolizerOrDie( |
| 44 | std::vector<std::string> binary_path, |
| 45 | const char* mode) { |
| 46 | std::unique_ptr<Symbolizer> symbolizer; |
| 47 | |
| 48 | if (!binary_path.empty()) { |
| 49 | #if PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER) |
| 50 | std::unique_ptr<BinaryFinder> finder; |
| 51 | if (!mode || strncmp(mode, "find", 4) == 0) |
| 52 | finder.reset(new LocalBinaryFinder(std::move(binary_path))); |
| 53 | else if (strncmp(mode, "index", 5) == 0) |
| 54 | finder.reset(new LocalBinaryIndexer(std::move(binary_path))); |
| 55 | else |
| 56 | PERFETTO_FATAL("Invalid symbolizer mode [find | index]: %s", mode); |
| 57 | symbolizer.reset(new LocalSymbolizer(std::move(finder))); |
| 58 | #else |
| 59 | base::ignore_result(mode); |
| 60 | PERFETTO_FATAL("This build does not support local symbolization."); |
| 61 | #endif |
| 62 | } |
| 63 | return symbolizer; |
| 64 | } |
| 65 | |
| 66 | } // namespace profiling |
| 67 | } // namespace perfetto |
| 68 | |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 69 | #if PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER) |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 70 | #include "perfetto/ext/base/string_splitter.h" |
Hector Dearman | 5f06938 | 2019-09-16 10:24:01 +0100 | [diff] [blame] | 71 | #include "perfetto/ext/base/string_utils.h" |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 72 | #include "perfetto/ext/base/utils.h" |
| 73 | |
Florian Mayer | 4c95570 | 2020-06-10 20:37:38 +0200 | [diff] [blame] | 74 | #include <signal.h> |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 75 | #include <sys/stat.h> |
| 76 | #include <sys/types.h> |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 77 | |
| 78 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
Florian Mayer | 79eb212 | 2020-12-10 17:09:09 +0000 | [diff] [blame] | 79 | constexpr const char* kDefaultSymbolizer = "llvm-symbolizer.exe"; |
| 80 | #else |
| 81 | constexpr const char* kDefaultSymbolizer = "llvm-symbolizer"; |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 82 | #endif |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 83 | |
| 84 | namespace perfetto { |
Florian Mayer | bd88ff8 | 2019-12-18 16:36:37 +0000 | [diff] [blame] | 85 | namespace profiling { |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 86 | |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 87 | std::vector<std::string> GetLines( |
| 88 | std::function<int64_t(char*, size_t)> fn_read) { |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 89 | std::vector<std::string> lines; |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 90 | char buffer[512]; |
| 91 | int64_t rd = 0; |
| 92 | // Cache the partial line of the previous read. |
| 93 | std::string last_line; |
| 94 | while ((rd = fn_read(buffer, sizeof(buffer))) > 0) { |
| 95 | std::string data(buffer, static_cast<size_t>(rd)); |
| 96 | // Create stream buffer of last partial line + new data |
| 97 | std::stringstream stream(last_line + data); |
| 98 | std::string line; |
| 99 | last_line = ""; |
| 100 | while (std::getline(stream, line)) { |
| 101 | // Return from reading when we read an empty line. |
| 102 | if (line.empty()) { |
| 103 | return lines; |
| 104 | } else if (stream.eof()) { |
| 105 | // Cache off the partial line when we hit end of stream. |
| 106 | last_line += line; |
| 107 | break; |
| 108 | } else { |
| 109 | lines.push_back(line); |
| 110 | } |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 111 | } |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 112 | } |
| 113 | if (rd == -1) { |
| 114 | PERFETTO_ELOG("Failed to read data from subprocess."); |
| 115 | } |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 116 | return lines; |
Florian Mayer | 3917919 | 2019-09-09 16:15:22 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 119 | namespace { |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 120 | bool InRange(const void* base, |
| 121 | size_t total_size, |
| 122 | const void* ptr, |
| 123 | size_t size) { |
| 124 | return ptr >= base && static_cast<const char*>(ptr) + size <= |
| 125 | static_cast<const char*>(base) + total_size; |
| 126 | } |
| 127 | |
| 128 | template <typename E> |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 129 | std::optional<uint64_t> GetLoadBias(void* mem, size_t size) { |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 130 | const typename E::Ehdr* ehdr = static_cast<typename E::Ehdr*>(mem); |
| 131 | if (!InRange(mem, size, ehdr, sizeof(typename E::Ehdr))) { |
| 132 | PERFETTO_ELOG("Corrupted ELF."); |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 133 | return std::nullopt; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 134 | } |
| 135 | for (size_t i = 0; i < ehdr->e_phnum; ++i) { |
| 136 | typename E::Phdr* phdr = GetPhdr<E>(mem, ehdr, i); |
| 137 | if (!InRange(mem, size, phdr, sizeof(typename E::Phdr))) { |
| 138 | PERFETTO_ELOG("Corrupted ELF."); |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 139 | return std::nullopt; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 140 | } |
| 141 | if (phdr->p_type == PT_LOAD && phdr->p_flags & PF_X) { |
| 142 | return phdr->p_vaddr - phdr->p_offset; |
| 143 | } |
| 144 | } |
| 145 | return 0u; |
| 146 | } |
| 147 | |
| 148 | template <typename E> |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 149 | std::optional<std::string> GetBuildId(void* mem, size_t size) { |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 150 | const typename E::Ehdr* ehdr = static_cast<typename E::Ehdr*>(mem); |
| 151 | if (!InRange(mem, size, ehdr, sizeof(typename E::Ehdr))) { |
| 152 | PERFETTO_ELOG("Corrupted ELF."); |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 153 | return std::nullopt; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 154 | } |
| 155 | for (size_t i = 0; i < ehdr->e_shnum; ++i) { |
| 156 | typename E::Shdr* shdr = GetShdr<E>(mem, ehdr, i); |
| 157 | if (!InRange(mem, size, shdr, sizeof(typename E::Shdr))) { |
| 158 | PERFETTO_ELOG("Corrupted ELF."); |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 159 | return std::nullopt; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | if (shdr->sh_type != SHT_NOTE) |
| 163 | continue; |
| 164 | |
| 165 | auto offset = shdr->sh_offset; |
| 166 | while (offset < shdr->sh_offset + shdr->sh_size) { |
| 167 | typename E::Nhdr* nhdr = |
| 168 | reinterpret_cast<typename E::Nhdr*>(static_cast<char*>(mem) + offset); |
| 169 | |
| 170 | if (!InRange(mem, size, nhdr, sizeof(typename E::Nhdr))) { |
| 171 | PERFETTO_ELOG("Corrupted ELF."); |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 172 | return std::nullopt; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 173 | } |
| 174 | if (nhdr->n_type == NT_GNU_BUILD_ID && nhdr->n_namesz == 4) { |
| 175 | char* name = reinterpret_cast<char*>(nhdr) + sizeof(*nhdr); |
| 176 | if (!InRange(mem, size, name, 4)) { |
| 177 | PERFETTO_ELOG("Corrupted ELF."); |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 178 | return std::nullopt; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 179 | } |
| 180 | if (memcmp(name, "GNU", 3) == 0) { |
| 181 | const char* value = reinterpret_cast<char*>(nhdr) + sizeof(*nhdr) + |
| 182 | base::AlignUp<4>(nhdr->n_namesz); |
| 183 | |
| 184 | if (!InRange(mem, size, value, nhdr->n_descsz)) { |
| 185 | PERFETTO_ELOG("Corrupted ELF."); |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 186 | return std::nullopt; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 187 | } |
| 188 | return std::string(value, nhdr->n_descsz); |
| 189 | } |
| 190 | } |
| 191 | offset += sizeof(*nhdr) + base::AlignUp<4>(nhdr->n_namesz) + |
| 192 | base::AlignUp<4>(nhdr->n_descsz); |
| 193 | } |
| 194 | } |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 195 | return std::nullopt; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Florian Mayer | 3917919 | 2019-09-09 16:15:22 +0100 | [diff] [blame] | 198 | std::string SplitBuildID(const std::string& hex_build_id) { |
| 199 | if (hex_build_id.size() < 3) { |
| 200 | PERFETTO_DFATAL_OR_ELOG("Invalid build-id (< 3 char) %s", |
| 201 | hex_build_id.c_str()); |
| 202 | return {}; |
| 203 | } |
| 204 | |
| 205 | return hex_build_id.substr(0, 2) + "/" + hex_build_id.substr(2); |
| 206 | } |
| 207 | |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 208 | bool IsElf(const char* mem, size_t size) { |
| 209 | if (size <= EI_MAG3) |
| 210 | return false; |
| 211 | return (mem[EI_MAG0] == ELFMAG0 && mem[EI_MAG1] == ELFMAG1 && |
| 212 | mem[EI_MAG2] == ELFMAG2 && mem[EI_MAG3] == ELFMAG3); |
| 213 | } |
| 214 | |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 215 | struct BuildIdAndLoadBias { |
| 216 | std::string build_id; |
| 217 | uint64_t load_bias; |
| 218 | }; |
| 219 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 220 | std::optional<BuildIdAndLoadBias> GetBuildIdAndLoadBias(const char* fname, |
| 221 | size_t size) { |
Daniele Di Proietto | ee90928 | 2021-09-20 18:26:16 +0000 | [diff] [blame] | 222 | static_assert(EI_CLASS > EI_MAG3, "mem[EI_MAG?] accesses are in range."); |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 223 | if (size <= EI_CLASS) |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 224 | return std::nullopt; |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 225 | ScopedReadMmap map(fname, size); |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 226 | if (!map.IsValid()) { |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 227 | PERFETTO_PLOG("mmap"); |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 228 | return std::nullopt; |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 229 | } |
| 230 | char* mem = static_cast<char*>(*map); |
| 231 | |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 232 | if (!IsElf(mem, size)) |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 233 | return std::nullopt; |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 234 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 235 | std::optional<std::string> build_id; |
| 236 | std::optional<uint64_t> load_bias; |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 237 | switch (mem[EI_CLASS]) { |
| 238 | case ELFCLASS32: |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 239 | build_id = GetBuildId<Elf32>(mem, size); |
| 240 | load_bias = GetLoadBias<Elf32>(mem, size); |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 241 | break; |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 242 | case ELFCLASS64: |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 243 | build_id = GetBuildId<Elf64>(mem, size); |
| 244 | load_bias = GetLoadBias<Elf64>(mem, size); |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 245 | break; |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 246 | default: |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 247 | return std::nullopt; |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 248 | } |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 249 | if (build_id && load_bias) { |
| 250 | return BuildIdAndLoadBias{*build_id, *load_bias}; |
| 251 | } |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 252 | return std::nullopt; |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 255 | std::map<std::string, FoundBinary> BuildIdIndex(std::vector<std::string> dirs) { |
| 256 | std::map<std::string, FoundBinary> result; |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 257 | WalkDirectories(std::move(dirs), [&result](const char* fname, size_t size) { |
| 258 | char magic[EI_MAG3 + 1]; |
| 259 | // Scope file access. On windows OpenFile opens an exclusive lock. |
| 260 | // This lock needs to be released before mapping the file. |
| 261 | { |
| 262 | base::ScopedFile fd(base::OpenFile(fname, O_RDONLY)); |
| 263 | if (!fd) { |
| 264 | PERFETTO_PLOG("Failed to open %s", fname); |
| 265 | return; |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 266 | } |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 267 | ssize_t rd = base::Read(*fd, &magic, sizeof(magic)); |
| 268 | if (rd != sizeof(magic)) { |
| 269 | PERFETTO_PLOG("Failed to read %s", fname); |
| 270 | return; |
| 271 | } |
| 272 | if (!IsElf(magic, static_cast<size_t>(rd))) { |
| 273 | PERFETTO_DLOG("%s not an ELF.", fname); |
| 274 | return; |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 275 | } |
| 276 | } |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 277 | std::optional<BuildIdAndLoadBias> build_id_and_load_bias = |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 278 | GetBuildIdAndLoadBias(fname, size); |
| 279 | if (build_id_and_load_bias) { |
| 280 | result.emplace(build_id_and_load_bias->build_id, |
| 281 | FoundBinary{fname, build_id_and_load_bias->load_bias}); |
| 282 | } |
| 283 | }); |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 284 | return result; |
| 285 | } |
| 286 | |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 287 | } // namespace |
| 288 | |
Florian Mayer | 4dd5a1f | 2020-07-14 13:00:51 +0100 | [diff] [blame] | 289 | bool ParseLlvmSymbolizerLine(const std::string& line, |
| 290 | std::string* file_name, |
| 291 | uint32_t* line_no) { |
| 292 | size_t col_pos = line.rfind(':'); |
| 293 | if (col_pos == std::string::npos || col_pos == 0) |
| 294 | return false; |
| 295 | size_t row_pos = line.rfind(':', col_pos - 1); |
| 296 | if (row_pos == std::string::npos || row_pos == 0) |
| 297 | return false; |
| 298 | *file_name = line.substr(0, row_pos); |
| 299 | auto line_no_str = line.substr(row_pos + 1, col_pos - row_pos - 1); |
| 300 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 301 | std::optional<int32_t> opt_parsed_line_no = base::StringToInt32(line_no_str); |
Florian Mayer | 4dd5a1f | 2020-07-14 13:00:51 +0100 | [diff] [blame] | 302 | if (!opt_parsed_line_no || *opt_parsed_line_no < 0) |
| 303 | return false; |
| 304 | *line_no = static_cast<uint32_t>(*opt_parsed_line_no); |
| 305 | return true; |
| 306 | } |
| 307 | |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 308 | BinaryFinder::~BinaryFinder() = default; |
| 309 | |
| 310 | LocalBinaryIndexer::LocalBinaryIndexer(std::vector<std::string> roots) |
| 311 | : buildid_to_file_(BuildIdIndex(std::move(roots))) {} |
| 312 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 313 | std::optional<FoundBinary> LocalBinaryIndexer::FindBinary( |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 314 | const std::string& abspath, |
| 315 | const std::string& build_id) { |
| 316 | auto it = buildid_to_file_.find(build_id); |
| 317 | if (it != buildid_to_file_.end()) |
| 318 | return it->second; |
| 319 | PERFETTO_ELOG("Could not find Build ID: %s (file %s).", |
| 320 | base::ToHex(build_id).c_str(), abspath.c_str()); |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 321 | return std::nullopt; |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | LocalBinaryIndexer::~LocalBinaryIndexer() = default; |
| 325 | |
| 326 | LocalBinaryFinder::LocalBinaryFinder(std::vector<std::string> roots) |
| 327 | : roots_(std::move(roots)) {} |
| 328 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 329 | std::optional<FoundBinary> LocalBinaryFinder::FindBinary( |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 330 | const std::string& abspath, |
| 331 | const std::string& build_id) { |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 332 | auto p = cache_.emplace(abspath, std::nullopt); |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 333 | if (!p.second) |
| 334 | return p.first->second; |
| 335 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 336 | std::optional<FoundBinary>& cache_entry = p.first->second; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 337 | |
| 338 | for (const std::string& root_str : roots_) { |
| 339 | cache_entry = FindBinaryInRoot(root_str, abspath, build_id); |
| 340 | if (cache_entry) |
| 341 | return cache_entry; |
| 342 | } |
Florian Mayer | fdc86d6 | 2019-09-20 11:23:55 +0100 | [diff] [blame] | 343 | PERFETTO_ELOG("Could not find %s (Build ID: %s).", abspath.c_str(), |
| 344 | base::ToHex(build_id).c_str()); |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 345 | return cache_entry; |
| 346 | } |
| 347 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 348 | std::optional<FoundBinary> LocalBinaryFinder::IsCorrectFile( |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 349 | const std::string& symbol_file, |
| 350 | const std::string& build_id) { |
Primiano Tucci | ab293f5 | 2020-12-08 11:46:52 +0100 | [diff] [blame] | 351 | if (!base::FileExists(symbol_file)) { |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 352 | return std::nullopt; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 353 | } |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 354 | // Openfile opens the file with an exclusive lock on windows. |
Daniele Di Proietto | b2b829c | 2024-02-19 17:23:34 +0000 | [diff] [blame^] | 355 | std::optional<size_t> size = base::GetFileSize(symbol_file); |
| 356 | if (!size.has_value()) { |
| 357 | PERFETTO_PLOG("Failed to get file size %s", symbol_file.c_str()); |
| 358 | return std::nullopt; |
| 359 | } |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 360 | |
Daniele Di Proietto | b2b829c | 2024-02-19 17:23:34 +0000 | [diff] [blame^] | 361 | if (*size == 0) { |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 362 | return std::nullopt; |
Daniele Di Proietto | cb42600 | 2023-02-16 12:14:38 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 365 | std::optional<BuildIdAndLoadBias> build_id_and_load_bias = |
Daniele Di Proietto | b2b829c | 2024-02-19 17:23:34 +0000 | [diff] [blame^] | 366 | GetBuildIdAndLoadBias(symbol_file.c_str(), *size); |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 367 | if (!build_id_and_load_bias) |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 368 | return std::nullopt; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 369 | if (build_id_and_load_bias->build_id != build_id) { |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 370 | return std::nullopt; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 371 | } |
| 372 | return FoundBinary{symbol_file, build_id_and_load_bias->load_bias}; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 375 | std::optional<FoundBinary> LocalBinaryFinder::FindBinaryInRoot( |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 376 | const std::string& root_str, |
| 377 | const std::string& abspath, |
| 378 | const std::string& build_id) { |
| 379 | constexpr char kApkPrefix[] = "base.apk!"; |
| 380 | |
| 381 | std::string filename; |
| 382 | std::string dirname; |
| 383 | |
| 384 | for (base::StringSplitter sp(abspath, '/'); sp.Next();) { |
Florian Mayer | 3917919 | 2019-09-09 16:15:22 +0100 | [diff] [blame] | 385 | if (!dirname.empty()) |
| 386 | dirname += "/"; |
| 387 | dirname += filename; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 388 | filename = sp.cur_token(); |
| 389 | } |
| 390 | |
| 391 | // Return the first match for the following options: |
| 392 | // * absolute path of library file relative to root. |
| 393 | // * absolute path of library file relative to root, but with base.apk! |
| 394 | // removed from filename. |
| 395 | // * only filename of library file relative to root. |
| 396 | // * only filename of library file relative to root, but with base.apk! |
| 397 | // removed from filename. |
Florian Mayer | 3917919 | 2019-09-09 16:15:22 +0100 | [diff] [blame] | 398 | // * in the subdirectory .build-id: the first two hex digits of the build-id |
| 399 | // as subdirectory, then the rest of the hex digits, with ".debug"appended. |
| 400 | // See |
| 401 | // https://fedoraproject.org/wiki/RolandMcGrath/BuildID#Find_files_by_build_ID |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 402 | // |
Florian Mayer | 3917919 | 2019-09-09 16:15:22 +0100 | [diff] [blame] | 403 | // For example, "/system/lib/base.apk!foo.so" with build id abcd1234, |
| 404 | // is looked for at |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 405 | // * $ROOT/system/lib/base.apk!foo.so |
| 406 | // * $ROOT/system/lib/foo.so |
| 407 | // * $ROOT/base.apk!foo.so |
| 408 | // * $ROOT/foo.so |
Florian Mayer | 3917919 | 2019-09-09 16:15:22 +0100 | [diff] [blame] | 409 | // * $ROOT/.build-id/ab/cd1234.debug |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 410 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 411 | std::optional<FoundBinary> result; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 412 | |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 413 | std::string symbol_file = root_str + "/" + dirname + "/" + filename; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 414 | result = IsCorrectFile(symbol_file, build_id); |
| 415 | if (result) { |
| 416 | return result; |
| 417 | } |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 418 | |
Hector Dearman | aa02574 | 2021-01-14 13:55:22 +0000 | [diff] [blame] | 419 | if (base::StartsWith(filename, kApkPrefix)) { |
Daniele Di Proietto | fc25f7e | 2022-02-08 20:13:39 +0000 | [diff] [blame] | 420 | symbol_file = root_str + "/" + dirname + "/" + |
| 421 | filename.substr(sizeof(kApkPrefix) - 1); |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 422 | result = IsCorrectFile(symbol_file, build_id); |
| 423 | if (result) { |
| 424 | return result; |
| 425 | } |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | symbol_file = root_str + "/" + filename; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 429 | result = IsCorrectFile(symbol_file, build_id); |
| 430 | if (result) { |
| 431 | return result; |
| 432 | } |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 433 | |
Hector Dearman | aa02574 | 2021-01-14 13:55:22 +0000 | [diff] [blame] | 434 | if (base::StartsWith(filename, kApkPrefix)) { |
Daniele Di Proietto | fc25f7e | 2022-02-08 20:13:39 +0000 | [diff] [blame] | 435 | symbol_file = root_str + "/" + filename.substr(sizeof(kApkPrefix) - 1); |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 436 | result = IsCorrectFile(symbol_file, build_id); |
| 437 | if (result) { |
| 438 | return result; |
| 439 | } |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Hector Dearman | 5f06938 | 2019-09-16 10:24:01 +0100 | [diff] [blame] | 442 | std::string hex_build_id = base::ToHex(build_id.c_str(), build_id.size()); |
Florian Mayer | 3917919 | 2019-09-09 16:15:22 +0100 | [diff] [blame] | 443 | std::string split_hex_build_id = SplitBuildID(hex_build_id); |
| 444 | if (!split_hex_build_id.empty()) { |
| 445 | symbol_file = |
| 446 | root_str + "/" + ".build-id" + "/" + split_hex_build_id + ".debug"; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 447 | result = IsCorrectFile(symbol_file, build_id); |
| 448 | if (result) { |
| 449 | return result; |
| 450 | } |
Florian Mayer | 3917919 | 2019-09-09 16:15:22 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 453 | return std::nullopt; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 456 | LocalBinaryFinder::~LocalBinaryFinder() = default; |
| 457 | |
Florian Mayer | 79eb212 | 2020-12-10 17:09:09 +0000 | [diff] [blame] | 458 | LLVMSymbolizerProcess::LLVMSymbolizerProcess(const std::string& symbolizer_path) |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 459 | : |
| 460 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
Florian Mayer | 79eb212 | 2020-12-10 17:09:09 +0000 | [diff] [blame] | 461 | subprocess_(symbolizer_path, {}) { |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 462 | } |
| 463 | #else |
Florian Mayer | 79eb212 | 2020-12-10 17:09:09 +0000 | [diff] [blame] | 464 | subprocess_(symbolizer_path, {"llvm-symbolizer"}) { |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 465 | } |
| 466 | #endif |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 467 | |
| 468 | std::vector<SymbolizedFrame> LLVMSymbolizerProcess::Symbolize( |
| 469 | const std::string& binary, |
| 470 | uint64_t address) { |
| 471 | std::vector<SymbolizedFrame> result; |
Primiano Tucci | 934f8df | 2022-11-28 18:35:00 +0000 | [diff] [blame] | 472 | base::StackString<1024> buffer("\"%s\" 0x%" PRIx64 "\n", binary.c_str(), |
| 473 | address); |
| 474 | if (subprocess_.Write(buffer.c_str(), buffer.len()) < 0) { |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 475 | PERFETTO_ELOG("Failed to write to llvm-symbolizer."); |
| 476 | return result; |
| 477 | } |
Joshua Gilpatrick | cd34994 | 2020-11-11 15:18:37 -0800 | [diff] [blame] | 478 | auto lines = GetLines([&](char* read_buffer, size_t buffer_size) { |
| 479 | return subprocess_.Read(read_buffer, buffer_size); |
| 480 | }); |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 481 | // llvm-symbolizer writes out records in the form of |
| 482 | // Foo(Bar*) |
| 483 | // foo.cc:123 |
| 484 | // This is why we should always get a multiple of two number of lines. |
| 485 | PERFETTO_DCHECK(lines.size() % 2 == 0); |
| 486 | result.resize(lines.size() / 2); |
| 487 | for (size_t i = 0; i < lines.size(); ++i) { |
Florian Mayer | 519f32e | 2019-08-01 18:18:39 +0100 | [diff] [blame] | 488 | SymbolizedFrame& cur = result[i / 2]; |
| 489 | if (i % 2 == 0) { |
| 490 | cur.function_name = lines[i]; |
| 491 | } else { |
Florian Mayer | 4dd5a1f | 2020-07-14 13:00:51 +0100 | [diff] [blame] | 492 | if (!ParseLlvmSymbolizerLine(lines[i], &cur.file_name, &cur.line)) { |
Florian Mayer | 519f32e | 2019-08-01 18:18:39 +0100 | [diff] [blame] | 493 | PERFETTO_ELOG("Failed to parse llvm-symbolizer line: %s", |
| 494 | lines[i].c_str()); |
| 495 | cur.file_name = ""; |
| 496 | cur.line = 0; |
| 497 | } |
| 498 | } |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 499 | } |
Florian Mayer | cf9eadc | 2019-08-05 11:59:35 +0100 | [diff] [blame] | 500 | |
| 501 | for (auto it = result.begin(); it != result.end();) { |
| 502 | if (it->function_name == "??") |
| 503 | it = result.erase(it); |
| 504 | else |
| 505 | ++it; |
| 506 | } |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 507 | return result; |
| 508 | } |
Florian Mayer | 04d1ace | 2019-08-01 11:59:53 +0100 | [diff] [blame] | 509 | std::vector<std::vector<SymbolizedFrame>> LocalSymbolizer::Symbolize( |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 510 | const std::string& mapping_name, |
| 511 | const std::string& build_id, |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 512 | uint64_t load_bias, |
Florian Mayer | 04d1ace | 2019-08-01 11:59:53 +0100 | [diff] [blame] | 513 | const std::vector<uint64_t>& addresses) { |
Lalit Maganti | 4e2303c | 2023-03-29 15:28:36 +0100 | [diff] [blame] | 514 | std::optional<FoundBinary> binary = |
Florian Mayer | 6d1f0ae | 2020-07-21 08:59:29 +0100 | [diff] [blame] | 515 | finder_->FindBinary(mapping_name, build_id); |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 516 | if (!binary) |
| 517 | return {}; |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 518 | uint64_t load_bias_correction = 0; |
| 519 | if (binary->load_bias > load_bias) { |
| 520 | // On Android 10, there was a bug in libunwindstack that would incorrectly |
| 521 | // calculate the load_bias, and thus the relative PC. This would end up in |
| 522 | // frames that made no sense. We can fix this up after the fact if we |
| 523 | // detect this situation. |
| 524 | load_bias_correction = binary->load_bias - load_bias; |
| 525 | PERFETTO_LOG("Correcting load bias by %" PRIu64 " for %s", |
| 526 | load_bias_correction, mapping_name.c_str()); |
| 527 | } |
Florian Mayer | 04d1ace | 2019-08-01 11:59:53 +0100 | [diff] [blame] | 528 | std::vector<std::vector<SymbolizedFrame>> result; |
| 529 | result.reserve(addresses.size()); |
| 530 | for (uint64_t address : addresses) |
Florian Mayer | e7228a7 | 2020-10-22 18:18:01 +0100 | [diff] [blame] | 531 | result.emplace_back(llvm_symbolizer_.Symbolize( |
| 532 | binary->file_name, address + load_bias_correction)); |
Florian Mayer | 04d1ace | 2019-08-01 11:59:53 +0100 | [diff] [blame] | 533 | return result; |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Florian Mayer | 79eb212 | 2020-12-10 17:09:09 +0000 | [diff] [blame] | 536 | LocalSymbolizer::LocalSymbolizer(const std::string& symbolizer_path, |
| 537 | std::unique_ptr<BinaryFinder> finder) |
| 538 | : llvm_symbolizer_(symbolizer_path), finder_(std::move(finder)) {} |
| 539 | |
| 540 | LocalSymbolizer::LocalSymbolizer(std::unique_ptr<BinaryFinder> finder) |
| 541 | : LocalSymbolizer(kDefaultSymbolizer, std::move(finder)) {} |
| 542 | |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 543 | LocalSymbolizer::~LocalSymbolizer() = default; |
| 544 | |
Florian Mayer | bd88ff8 | 2019-12-18 16:36:37 +0000 | [diff] [blame] | 545 | } // namespace profiling |
Florian Mayer | d12a6de | 2019-08-01 10:36:11 +0100 | [diff] [blame] | 546 | } // namespace perfetto |
Primiano Tucci | 41af34f | 2019-10-01 13:09:22 +0100 | [diff] [blame] | 547 | |
| 548 | #endif // PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER) |