Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2013 The ChromiumOS Authors |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Generate minidump symbols for use by the Crash server. |
| 6 | |
| 7 | Note: This should be run inside the chroot. |
| 8 | |
| 9 | This produces files in the breakpad format required by minidump_stackwalk and |
| 10 | the crash server to dump stack information. |
| 11 | |
| 12 | Basically it scans all the split .debug files in /build/$BOARD/usr/lib/debug/ |
| 13 | and converts them over using the `dump_syms` programs. Those plain text .sym |
| 14 | files are then stored in /build/$BOARD/usr/lib/debug/breakpad/. |
| 15 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 16 | If you want to actually upload things, see upload_symbols.py. |
| 17 | """ |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 18 | |
| 19 | import collections |
| 20 | import ctypes |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 21 | import enum |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 22 | import logging |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 23 | import multiprocessing |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 24 | import multiprocessing.sharedctypes |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 25 | import os |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 26 | import re |
| 27 | from typing import Optional |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 28 | |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 29 | from chromite.cbuildbot import cbuildbot_alerts |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 30 | from chromite.lib import build_target_lib |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 31 | from chromite.lib import commandline |
| 32 | from chromite.lib import cros_build_lib |
| 33 | from chromite.lib import osutils |
| 34 | from chromite.lib import parallel |
Mike Frysinger | 96ad3f2 | 2014-04-24 23:27:27 -0400 | [diff] [blame] | 35 | from chromite.lib import signals |
Alex Klein | 1809f57 | 2021-09-09 11:28:37 -0600 | [diff] [blame] | 36 | from chromite.utils import file_util |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 37 | |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [diff] [blame] | 38 | |
Stephen Boyd | fc1c803 | 2021-10-06 20:58:37 -0700 | [diff] [blame] | 39 | # Elf files that don't exist but have a split .debug file installed. |
| 40 | ALLOWED_DEBUG_ONLY_FILES = { |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | "boot/vmlinux", |
Stephen Boyd | fc1c803 | 2021-10-06 20:58:37 -0700 | [diff] [blame] | 42 | } |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 43 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 44 | # Allowlist of elf files that we know we can't symbolize in the normal way, but |
| 45 | # which we don't have an automatic way to detect. |
| 46 | EXPECTED_POOR_SYMBOLIZATION_FILES = ALLOWED_DEBUG_ONLY_FILES | { |
| 47 | # Git binaries are downloaded as binary blobs already stripped. |
| 48 | "usr/bin/git", |
| 49 | "usr/bin/git-receive-pack", |
| 50 | "usr/bin/git-upload-archive", |
| 51 | "usr/bin/git-upload-pack", |
| 52 | # Prebuild Android binary |
| 53 | "build/rootfs/opt/google/vms/android/etc/bin/XkbToKcmConverter", |
Ian Barkley-Yeung | ab1dab1 | 2023-04-21 18:19:55 -0700 | [diff] [blame] | 54 | "build/rootfs/opt/google/containers/android/etc/bin/XkbToKcmConverter", |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 55 | # Pulled from |
| 56 | # https://skia.googlesource.com/buildbot/+/refs/heads/main/gold-client/, no |
| 57 | # need to resymbolize. |
| 58 | "usr/bin/goldctl", |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 61 | # Allowlist of patterns for ELF files that symbolize (dump_syms exits with |
| 62 | # success) but don't pass symbol file validation. Note that ELFs listed in |
| 63 | # EXPECTED_POOR_SYMBOLIZATION_FILES do not have their symbol files validated and |
| 64 | # do not need to be repeated here. |
| 65 | ALLOWLIST_NO_SYMBOL_FILE_VALIDATION = { |
| 66 | # Built in a weird way, see comments at top of |
| 67 | # https://source.chromium.org/chromium/chromium/src/+/main:native_client/src/trusted/service_runtime/linux/nacl_bootstrap.x |
| 68 | "opt/google/chrome/nacl_helper_bootstrap", |
| 69 | # TODO(b/273543528): Investigate why this doesn't have stack records on |
| 70 | # kevin builds. |
| 71 | "build/rootfs/dlc-scaled/screen-ai/package/root/libchromescreenai.so", |
Ian Barkley-Yeung | ab1dab1 | 2023-04-21 18:19:55 -0700 | [diff] [blame] | 72 | # TODO(b/279645511): Investigate why this doesn't have STACK records on |
| 73 | # jacuzzi, scarlet, kukui, etc. |
| 74 | "usr/bin/rma_reset", |
| 75 | # Virtual dynamic shared object, not expected to have STACK records. |
Trent Apted | c4f366a | 2023-05-16 15:32:48 +1000 | [diff] [blame] | 76 | ( |
| 77 | "opt/google/containers/android/ndk_translation/lib/arm/" |
| 78 | "libndk_translation_vdso.so" |
| 79 | ), |
Ian Barkley-Yeung | ab1dab1 | 2023-04-21 18:19:55 -0700 | [diff] [blame] | 80 | # TODO(b/279665879): Figure out why this ndk_translation libraries is not |
| 81 | # getting STACK records. |
| 82 | "opt/google/containers/android/ndk_translation/lib/arm/libdexfile.so", |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 83 | } |
| 84 | # Same but patterns not exact paths. |
| 85 | ALLOWLIST_NO_SYMBOL_FILE_VALIDATION_RE = tuple( |
| 86 | re.compile(x) |
| 87 | for x in ( |
| 88 | # Only has code if use.camera_feature_effects. Otherwise will have no |
| 89 | # STACK records. |
| 90 | r"usr/lib[^/]*/libcros_ml_core\.so", |
| 91 | # Prebuilt closed-source library. |
| 92 | r"usr/lib[^/]*/python[0-9]\.[0-9]/site-packages/.*/x_ignore_nofocus.so", |
| 93 | # b/273577373: Rarely used, and only by few programs that do |
| 94 | # non-standard encoding conversions |
| 95 | r"lib[^/]*/libnss_files\.so\.[0-9.]+", |
| 96 | r"lib[^/]*/libnss_dns\.so\.[0-9.]+", |
| 97 | r"usr/lib[^/]*/gconv/libISOIR165\.so", |
| 98 | r"usr/lib[^/]*/gconv/libGB\.so", |
| 99 | r"usr/lib[^/]*/gconv/libKSC\.so", |
| 100 | r"usr/lib[^/]*/gconv/libCNS\.so", |
| 101 | r"usr/lib[^/]*/gconv/libJISX0213\.so", |
| 102 | r"usr/lib[^/]*/gconv/libJIS\.so", |
| 103 | # TODO(b/273579075): Figure out why libcares.so is not getting STACK |
| 104 | # records on kevin. |
| 105 | r"usr/lib[^/]*/libcares\.so[0-9.]*", |
| 106 | # libevent-2.1.so.7.0.1 does not have executable code and thus no |
| 107 | # STACK records. See libevent-2.1.12-libevent-shrink.patch. |
| 108 | r"usr/lib[^/]*/libevent-[0-9.]+\.so[0-9.]*", |
| 109 | # TODO(b/273599604): Figure out why libdcerpc-samr.so.0.0.1 is not |
| 110 | # getting STACK records on kevin. |
| 111 | r"usr/lib[^/]*/libdcerpc-samr\.so[0-9.]*", |
| 112 | # TODO(b/272613635): Figure out why these libabsl shared libraries are |
| 113 | # not getting STACK records. |
| 114 | r"usr/lib[^/]*/libabsl_bad_variant_access\.so\.[0-9.]+", |
| 115 | r"usr/lib[^/]*/libabsl_random_internal_platform\.so\.[0-9.]+", |
| 116 | r"usr/lib[^/]*/libabsl_flags\.so\.[0-9.]+", |
| 117 | r"usr/lib[^/]*/libabsl_bad_any_cast_impl\.so\.[0-9.]+", |
| 118 | r"usr/lib[^/]*/libabsl_bad_optional_access\.so\.[0-9.]+", |
| 119 | # TODO(b/273607289): Figure out why libgrpc++_error_details.so.1.43.0 is |
| 120 | # not getting STACK records on kevin. |
| 121 | r"usr/lib[^/]*/libgrpc\+\+_error_details\.so\.[0-9.]+", |
Ian Barkley-Yeung | ab1dab1 | 2023-04-21 18:19:55 -0700 | [diff] [blame] | 122 | # linux-gate.so is system call wrappers which don't always have enough |
| 123 | # code to get STACK entries. |
| 124 | r"lib/modules/[^/]+/vdso/linux-gate\.so", |
| 125 | # TODO(b/280503615): Figure out why libGLESv2.so.2.0.0 doesn't have |
| 126 | # STACK records on amd64-generic or betty-pi-arc. |
| 127 | r"usr/lib[^/]*/libGLESv[0-9]+\.so.*", |
| 128 | # This is just a backwards compatibility stub if ENABLE_HLSL is defined, |
| 129 | # see https://github.com/KhronosGroup/glslang/blob/main/hlsl/stub.cpp |
| 130 | r"usr/lib[^/]*/libHLSL.so", |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 131 | ) |
| 132 | ) |
| 133 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | SymbolHeader = collections.namedtuple( |
| 135 | "SymbolHeader", |
| 136 | ( |
| 137 | "cpu", |
| 138 | "id", |
| 139 | "name", |
| 140 | "os", |
| 141 | ), |
| 142 | ) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 143 | |
| 144 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 145 | class SymbolGenerationResult(enum.Enum): |
| 146 | """Result of running dump_syms |
| 147 | |
| 148 | Return value of _DumpAllowingBasicFallback() and _DumpExpectingSymbols(). |
| 149 | """ |
| 150 | |
| 151 | SUCCESS = 1 |
| 152 | UNEXPECTED_FAILURE = 2 |
| 153 | EXPECTED_FAILURE = 3 |
| 154 | |
| 155 | |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 156 | class ExpectedFiles(enum.Enum): |
| 157 | """The files always expect to see dump_syms run on. |
| 158 | |
| 159 | We do extra validation on a few, semi-randomly chosen files. If we do not |
| 160 | create symbol files for these ELFs, something is very wrong. |
| 161 | """ |
| 162 | |
| 163 | ASH_CHROME = enum.auto() |
| 164 | LIBC = enum.auto() |
| 165 | CRASH_REPORTER = enum.auto() |
| 166 | LIBMETRICS = enum.auto() |
| 167 | |
| 168 | |
| 169 | ALL_EXPECTED_FILES = frozenset( |
| 170 | ( |
| 171 | ExpectedFiles.ASH_CHROME, |
| 172 | ExpectedFiles.LIBC, |
| 173 | ExpectedFiles.CRASH_REPORTER, |
| 174 | ExpectedFiles.LIBMETRICS, |
| 175 | ) |
| 176 | ) |
| 177 | |
| 178 | |
Ian Barkley-Yeung | ab1dab1 | 2023-04-21 18:19:55 -0700 | [diff] [blame] | 179 | # Regular expression for ChromeOS's libc.so. Note that some containers have |
| 180 | # their own libc.so file; we don't want to do the extra validation on those. |
| 181 | # (They are often subsets of the full libc and will not pass STACK count tests.) |
| 182 | LIBC_REGEX = re.compile(r"lib[^/]*/libc\.so\.[0-9.]+") |
| 183 | |
| 184 | |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 185 | class SymbolFileLineCounts: |
| 186 | """Counts of the various types of lines in a .sym file""" |
| 187 | |
| 188 | LINE_NUMBER_REGEX = re.compile(r"^([0-9a-f]+)") |
| 189 | |
| 190 | def __init__(self, sym_file: str, elf_file: str): |
| 191 | # https://chromium.googlesource.com/breakpad/breakpad/+/HEAD/docs/symbol_files.md |
| 192 | # explains what these line types are. |
| 193 | self.module_lines = 0 |
| 194 | self.file_lines = 0 |
| 195 | self.inline_origin_lines = 0 |
| 196 | self.func_lines = 0 |
| 197 | self.inline_lines = 0 |
| 198 | self.line_number_lines = 0 |
| 199 | self.public_lines = 0 |
| 200 | self.stack_lines = 0 |
| 201 | # Not listed in the documentation but still present. |
| 202 | self.info_lines = 0 |
| 203 | |
| 204 | with open(sym_file, mode="r", encoding="utf-8") as f: |
| 205 | for line in f: |
| 206 | words = line.split() |
| 207 | expected_words_max = None |
| 208 | if not words: |
| 209 | raise ValueError( |
| 210 | f"{elf_file}: symbol file has unexpected blank line" |
| 211 | ) |
| 212 | |
| 213 | line_type = words[0] |
| 214 | if line_type == "MODULE": |
| 215 | self.module_lines += 1 |
| 216 | expected_words_min = 5 |
| 217 | expected_words_max = 5 |
| 218 | elif line_type == "FILE": |
| 219 | self.file_lines += 1 |
| 220 | expected_words_min = 3 |
| 221 | # No max, filenames can have spaces. |
| 222 | elif line_type == "INLINE_ORIGIN": |
| 223 | self.inline_origin_lines += 1 |
| 224 | expected_words_min = 3 |
| 225 | # No max, function parameter lists can have spaces. |
| 226 | elif line_type == "FUNC": |
| 227 | self.func_lines += 1 |
| 228 | expected_words_min = 5 |
| 229 | # No max, function parameter lists can have spaces. |
| 230 | elif line_type == "INLINE": |
| 231 | self.inline_lines += 1 |
| 232 | expected_words_min = 5 |
| 233 | # No max, INLINE can have multiple address pairs. |
| 234 | elif SymbolFileLineCounts.LINE_NUMBER_REGEX.match(line_type): |
| 235 | self.line_number_lines += 1 |
| 236 | expected_words_min = 4 |
| 237 | expected_words_max = 4 |
| 238 | line_type = "line number" |
| 239 | elif line_type == "PUBLIC": |
| 240 | self.public_lines += 1 |
Ian Barkley-Yeung | ab1dab1 | 2023-04-21 18:19:55 -0700 | [diff] [blame] | 241 | # TODO(b/251003272): expected_words_min should be 4; |
| 242 | # however, dump_syms sometimes produces PUBLIC records with |
| 243 | # no symbol name. This is an error but is not affecting our |
| 244 | # ability to decode stacks. |
| 245 | expected_words_min = 3 |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 246 | # No max, function parameter lists can have spaces. |
| 247 | elif line_type == "STACK": |
| 248 | self.stack_lines += 1 |
| 249 | expected_words_min = 5 |
| 250 | # No max, expressions can be complex. |
| 251 | elif line_type == "INFO": |
| 252 | self.info_lines += 1 |
| 253 | # Not documented, so unclear what the min & max are |
| 254 | expected_words_min = None |
| 255 | else: |
| 256 | raise ValueError( |
| 257 | f"{elf_file}: symbol file has unknown line type " |
| 258 | f"{line_type}" |
| 259 | ) |
| 260 | |
| 261 | if expected_words_max is not None: |
| 262 | if not ( |
| 263 | expected_words_min <= len(words) <= expected_words_max |
| 264 | ): |
| 265 | raise ValueError( |
| 266 | f"{elf_file}: symbol file has {line_type} line " |
| 267 | f"with {len(words)} words (expected " |
| 268 | f"{expected_words_min} - {expected_words_max})" |
| 269 | ) |
| 270 | elif expected_words_min is not None: |
| 271 | if len(words) < expected_words_min: |
| 272 | raise ValueError( |
| 273 | f"{elf_file}: symbol file has {line_type} line " |
| 274 | f"with {len(words)} words (expected " |
| 275 | f"{expected_words_min} or more)" |
| 276 | ) |
| 277 | |
| 278 | |
| 279 | def ValidateSymbolFile( |
| 280 | sym_file: str, |
| 281 | elf_file: str, |
| 282 | sysroot: Optional[str], |
| 283 | found_files: Optional[multiprocessing.managers.ListProxy], |
| 284 | ) -> bool: |
| 285 | """Checks that the given sym_file has enough info for us to get good stacks. |
| 286 | |
| 287 | Validates that the given sym_file has enough information for us to get |
| 288 | good error reports -- enough STACK records to unwind the stack and enough |
| 289 | FUNC or PUBLIC records to turn the function addresses into human-readable |
| 290 | names. |
| 291 | |
| 292 | Args: |
| 293 | sym_file: The complete path to the breakpad symbol file to validate |
| 294 | elf_file: The complete path to the elf file which was the source of the |
| 295 | symbol file. |
| 296 | sysroot: If not None, the root of the build directory ('/build/eve', for |
| 297 | instance). |
| 298 | found_files: A multiprocessing.managers.ListProxy list containing |
| 299 | ExpectedFiles, representing which of the "should always be present" |
| 300 | files have been processed. |
| 301 | |
| 302 | Returns: |
| 303 | True if the symbol file passes validation. |
| 304 | """ |
| 305 | if sysroot is not None: |
| 306 | relative_path = os.path.relpath(elf_file, sysroot) |
| 307 | else: |
| 308 | relative_path = os.path.relpath(elf_file, "/") |
| 309 | |
| 310 | if relative_path in ALLOWLIST_NO_SYMBOL_FILE_VALIDATION: |
| 311 | return True |
| 312 | for regex in ALLOWLIST_NO_SYMBOL_FILE_VALIDATION_RE: |
| 313 | if regex.match(relative_path): |
| 314 | return True |
| 315 | |
| 316 | counts = SymbolFileLineCounts(sym_file, elf_file) |
| 317 | |
| 318 | errors = False |
| 319 | if counts.stack_lines == 0: |
| 320 | # Use the elf_file in error messages; sym_file is still a temporary |
| 321 | # file with a meaningless-to-humans name right now. |
| 322 | logging.warning("%s: Symbol file has no STACK records", elf_file) |
| 323 | errors = True |
| 324 | if counts.module_lines != 1: |
| 325 | logging.warning( |
| 326 | "%s: Symbol file has %d MODULE lines", elf_file, counts.module_lines |
| 327 | ) |
| 328 | errors = True |
| 329 | # Many shared object files have only PUBLIC functions. In theory, |
| 330 | # executables should always have at least one FUNC (main) and some line |
| 331 | # numbers, but for reasons I'm unclear on, C-based executables often just |
| 332 | # have PUBLIC records. dump_syms does not support line numbers after |
| 333 | # PUBLIC records, only FUNC records, so such executables will also have |
| 334 | # no line numbers. |
| 335 | if counts.public_lines == 0 and counts.func_lines == 0: |
| 336 | logging.warning( |
| 337 | "%s: Symbol file has no FUNC or PUBLIC records", elf_file |
| 338 | ) |
| 339 | errors = True |
| 340 | # However, if we get a FUNC record, we do want line numbers for it. |
| 341 | if counts.func_lines > 0 and counts.line_number_lines == 0: |
| 342 | logging.warning( |
| 343 | "%s: Symbol file has FUNC records but no line numbers", elf_file |
| 344 | ) |
| 345 | errors = True |
| 346 | |
| 347 | if counts.line_number_lines > 0 and counts.file_lines == 0: |
| 348 | logging.warning( |
| 349 | "%s: Symbol file has line number records but no FILE records", |
| 350 | elf_file, |
| 351 | ) |
| 352 | errors = True |
| 353 | if counts.inline_lines > 0 and counts.file_lines == 0: |
| 354 | logging.warning( |
| 355 | "%s: Symbol file has INLINE records but no FILE records", elf_file |
| 356 | ) |
| 357 | errors = True |
| 358 | |
| 359 | if counts.inline_lines > 0 and counts.inline_origin_lines == 0: |
| 360 | logging.warning( |
| 361 | "%s: Symbol file has INLINE records but no INLINE_ORIGIN records", |
| 362 | elf_file, |
| 363 | ) |
| 364 | errors = True |
| 365 | |
| 366 | def _AddFoundFile(files, found): |
| 367 | """Add another file to the list of expected files we've found.""" |
| 368 | if files is not None: |
| 369 | files.append(found) |
| 370 | |
| 371 | # Extra validation for a few ELF files which are special. Either these are |
| 372 | # unusually important to the system (chrome binary, which is where a large |
| 373 | # fraction of our crashes occur, and libc.so, which is in every stack), or |
| 374 | # they are some hand-chosen ELF files which stand in for "normal" platform2 |
| 375 | # binaries. Not all ELF files would pass the extra validation, so we can't |
| 376 | # run these checks on every ELF, but we want to make sure we don't end up |
| 377 | # with, say, a chrome build or a platform2 build with just one or two FUNC |
| 378 | # records on every binary. |
| 379 | if relative_path == "opt/google/chrome/chrome": |
| 380 | _AddFoundFile(found_files, ExpectedFiles.ASH_CHROME) |
| 381 | if counts.func_lines < 100000: |
| 382 | logging.warning( |
| 383 | "chrome should have at least 100,000 FUNC records, found %d", |
| 384 | counts.func_lines, |
| 385 | ) |
| 386 | errors = True |
| 387 | if counts.stack_lines < 1000000: |
| 388 | logging.warning( |
| 389 | "chrome should have at least 1,000,000 STACK records, found %d", |
| 390 | counts.stack_lines, |
| 391 | ) |
| 392 | errors = True |
| 393 | if counts.line_number_lines < 1000000: |
| 394 | logging.warning( |
| 395 | "chrome should have at least 1,000,000 line number records, " |
| 396 | "found %d", |
| 397 | counts.line_number_lines, |
| 398 | ) |
| 399 | errors = True |
| 400 | # Lacros symbol files are not generated as part of the ChromeOS build and |
| 401 | # can't be validated here. |
| 402 | # TODO(b/273836486): Add similar logic to the code that generates Lacros |
| 403 | # symbols. |
Ian Barkley-Yeung | ab1dab1 | 2023-04-21 18:19:55 -0700 | [diff] [blame] | 404 | elif LIBC_REGEX.fullmatch(relative_path): |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 405 | _AddFoundFile(found_files, ExpectedFiles.LIBC) |
| 406 | if counts.public_lines < 100: |
| 407 | logging.warning( |
| 408 | "%s should have at least 100 PUBLIC records, found %d", |
| 409 | elf_file, |
| 410 | counts.public_lines, |
| 411 | ) |
| 412 | errors = True |
| 413 | if counts.stack_lines < 10000: |
| 414 | logging.warning( |
| 415 | "%s should have at least 10000 STACK records, found %d", |
| 416 | elf_file, |
| 417 | counts.stack_lines, |
| 418 | ) |
| 419 | errors = True |
| 420 | elif relative_path == "sbin/crash_reporter": |
| 421 | # Representative platform2 executable. |
| 422 | _AddFoundFile(found_files, ExpectedFiles.CRASH_REPORTER) |
| 423 | if counts.stack_lines < 1000: |
| 424 | logging.warning( |
| 425 | "crash_reporter should have at least 1000 STACK records, " |
| 426 | "found %d", |
| 427 | counts.stack_lines, |
| 428 | ) |
| 429 | errors = True |
| 430 | if counts.func_lines < 1000: |
| 431 | logging.warning( |
| 432 | "crash_reporter should have at least 1000 FUNC records, " |
| 433 | "found %d", |
| 434 | counts.func_lines, |
| 435 | ) |
| 436 | errors = True |
| 437 | if counts.line_number_lines < 10000: |
| 438 | logging.warning( |
| 439 | "crash_reporter should have at least 10,000 line number " |
| 440 | "records, found %d", |
| 441 | counts.line_number_lines, |
| 442 | ) |
| 443 | errors = True |
| 444 | elif os.path.basename(relative_path) == "libmetrics.so": |
| 445 | # Representative platform2 shared library. |
| 446 | _AddFoundFile(found_files, ExpectedFiles.LIBMETRICS) |
| 447 | if counts.func_lines < 100: |
| 448 | logging.warning( |
| 449 | "libmetrics should have at least 100 FUNC records, found %d", |
| 450 | counts.func_lines, |
| 451 | ) |
| 452 | errors = True |
| 453 | if counts.public_lines == 0: |
| 454 | logging.warning( |
| 455 | "libmetrics should have at least 1 PUBLIC record, found %d", |
| 456 | counts.public_lines, |
| 457 | ) |
| 458 | errors = True |
| 459 | if counts.stack_lines < 1000: |
| 460 | logging.warning( |
| 461 | "libmetrics should have at least 1000 STACK records, found %d", |
| 462 | counts.stack_lines, |
| 463 | ) |
| 464 | errors = True |
| 465 | if counts.line_number_lines < 5000: |
| 466 | logging.warning( |
| 467 | "libmetrics should have at least 5000 line number records, " |
| 468 | "found %d", |
| 469 | counts.line_number_lines, |
| 470 | ) |
| 471 | errors = True |
| 472 | |
| 473 | return not errors |
| 474 | |
| 475 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 476 | def _ExpectGoodSymbols(elf_file, sysroot): |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 477 | """Determines if we expect dump_syms to create good symbols. |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 478 | |
| 479 | We know that certain types of files never generate good symbols. Distinguish |
| 480 | those from the majority of elf files which should generate good symbols. |
| 481 | |
| 482 | Args: |
| 483 | elf_file: The complete path to the file which we will pass to dump_syms |
| 484 | sysroot: If not None, the root of the build directory ('/build/eve', for |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 485 | instance) |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 486 | |
| 487 | Returns: |
| 488 | True if the elf file should generate good symbols, False if not. |
| 489 | """ |
| 490 | # .ko files (kernel object files) never produce good symbols. |
| 491 | if elf_file.endswith(".ko"): |
| 492 | return False |
| 493 | |
| 494 | # dump_syms doesn't understand Golang executables. |
| 495 | result = cros_build_lib.run( |
| 496 | ["/usr/bin/file", elf_file], print_cmd=False, stdout=True |
| 497 | ) |
| 498 | if b"Go BuildID" in result.stdout: |
| 499 | return False |
| 500 | |
| 501 | if sysroot is not None: |
| 502 | relative_path = os.path.relpath(elf_file, sysroot) |
| 503 | else: |
Ian Barkley-Yeung | e178576 | 2023-03-08 18:32:18 -0800 | [diff] [blame] | 504 | relative_path = os.path.relpath(elf_file, "/") |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 505 | |
| 506 | if relative_path in EXPECTED_POOR_SYMBOLIZATION_FILES: |
| 507 | return False |
| 508 | |
Ian Barkley-Yeung | e178576 | 2023-03-08 18:32:18 -0800 | [diff] [blame] | 509 | # Binaries in /usr/local are not actually shipped to end-users, so we |
| 510 | # don't care if they get good symbols -- we should never get crash reports |
| 511 | # for them anyways. |
| 512 | if relative_path.startswith("usr/local"): |
| 513 | return False |
| 514 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 515 | return True |
| 516 | |
| 517 | |
| 518 | def ReadSymsHeader(sym_file, name_for_errors): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 519 | """Parse the header of the symbol file |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 520 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 521 | The first line of the syms file will read like: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 522 | MODULE Linux arm F4F6FA6CCBDEF455039C8DE869C8A2F40 blkid |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 523 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 524 | https://code.google.com/p/google-breakpad/wiki/SymbolFiles |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 525 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 526 | Args: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 527 | sym_file: The symbol file to parse |
| 528 | name_for_errors: A name for error strings. Can be the name of the elf |
| 529 | file that generated the symbol file, or the name of the symbol file |
| 530 | if the symbol file has already been moved to a meaningful location. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 531 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 532 | Returns: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 533 | A SymbolHeader object |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 534 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 535 | Raises: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 536 | ValueError if the first line of |sym_file| is invalid |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 537 | """ |
| 538 | with file_util.Open(sym_file, "rb") as f: |
| 539 | header = f.readline().decode("utf-8").split() |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 540 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 541 | if len(header) != 5 or header[0] != "MODULE": |
| 542 | raise ValueError( |
| 543 | f"header of sym file from {name_for_errors} is invalid" |
| 544 | ) |
Mike Frysinger | 50cedd3 | 2014-02-09 23:03:18 -0500 | [diff] [blame] | 545 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 546 | return SymbolHeader( |
| 547 | os=header[1], cpu=header[2], id=header[3], name=header[4] |
| 548 | ) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 549 | |
| 550 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 551 | def GenerateBreakpadSymbol( |
| 552 | elf_file, |
| 553 | debug_file=None, |
| 554 | breakpad_dir=None, |
| 555 | strip_cfi=False, |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 556 | sysroot=None, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 557 | num_errors=None, |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 558 | found_files=None, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 559 | dump_syms_cmd="dump_syms", |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 560 | force_basic_fallback=False, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 561 | ): |
| 562 | """Generate the symbols for |elf_file| using |debug_file| |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 563 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 564 | Args: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 565 | elf_file: The file to dump symbols for |
| 566 | debug_file: Split debug file to use for symbol information |
| 567 | breakpad_dir: The dir to store the output symbol file in |
| 568 | strip_cfi: Do not generate CFI data |
| 569 | sysroot: Path to the sysroot with the elf_file under it |
| 570 | num_errors: An object to update with the error count (needs a .value |
| 571 | member). |
| 572 | found_files: A multiprocessing.managers.ListProxy list containing |
| 573 | ExpectedFiles, representing which of the "should always be present" |
| 574 | files have been processed. |
| 575 | dump_syms_cmd: Command to use for dumping symbols. |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 576 | force_basic_fallback: If True, always use _DumpAllowingBasicFallback() |
| 577 | instead of _DumpExpectingSymbols(). |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 578 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 579 | Returns: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 580 | The name of symbol file written out on success, or the failure count. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 581 | """ |
| 582 | assert breakpad_dir |
| 583 | if num_errors is None: |
Trent Apted | 1e2e4f3 | 2023-05-05 03:50:20 +0000 | [diff] [blame] | 584 | num_errors = ctypes.c_int(0) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 585 | debug_file_only = not os.path.exists(elf_file) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 586 | |
Peter Boström | 763baca | 2023-05-25 16:36:33 +0000 | [diff] [blame] | 587 | cmd_base = [dump_syms_cmd, "-v", "-d", "-m"] |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 588 | if strip_cfi: |
| 589 | cmd_base += ["-c"] |
| 590 | # Some files will not be readable by non-root (e.g. set*id /bin/su). |
| 591 | needs_sudo = not os.access(elf_file, os.R_OK) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 592 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 593 | def _DumpIt(cmd_args): |
| 594 | if needs_sudo: |
| 595 | run_command = cros_build_lib.sudo_run |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 596 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 597 | run_command = cros_build_lib.run |
| 598 | return run_command( |
| 599 | cmd_base + cmd_args, |
| 600 | stderr=True, |
| 601 | stdout=temp.name, |
| 602 | check=False, |
| 603 | debug_level=logging.DEBUG, |
| 604 | ) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 605 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 606 | def _CrashCheck(result, file_or_files, msg): |
| 607 | if result.returncode: |
| 608 | cbuildbot_alerts.PrintBuildbotStepWarnings() |
| 609 | if result.returncode < 0: |
| 610 | logging.warning( |
| 611 | "dump_syms %s crashed with %s; %s", |
| 612 | file_or_files, |
| 613 | signals.StrSignal(-result.returncode), |
| 614 | msg, |
| 615 | ) |
| 616 | else: |
| 617 | logging.warning( |
| 618 | "dump_syms %s returned %d; %s", |
| 619 | file_or_files, |
| 620 | result.returncode, |
| 621 | msg, |
| 622 | ) |
| 623 | logging.warning("output:\n%s", result.stderr.decode("utf-8")) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 624 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 625 | def _DumpAllowingBasicFallback(): |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 626 | """Dump symbols for an ELF when we do NOT expect to get good symbols. |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 627 | |
| 628 | Returns: |
| 629 | A SymbolGenerationResult |
| 630 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 631 | if debug_file: |
| 632 | # Try to dump the symbols using the debug file like normal. |
| 633 | if debug_file_only: |
| 634 | cmd_args = [debug_file] |
| 635 | file_or_files = debug_file |
| 636 | else: |
| 637 | cmd_args = [elf_file, os.path.dirname(debug_file)] |
| 638 | file_or_files = [elf_file, debug_file] |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 639 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 640 | result = _DumpIt(cmd_args) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 641 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 642 | if result.returncode: |
| 643 | # Sometimes dump_syms can crash because there's too much info. |
| 644 | # Try dumping and stripping the extended stuff out. At least |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 645 | # this way we'll get the extended symbols. |
| 646 | # https://crbug.com/266064 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 647 | _CrashCheck(result, file_or_files, "retrying w/out CFI") |
| 648 | cmd_args = ["-c", "-r"] + cmd_args |
| 649 | result = _DumpIt(cmd_args) |
| 650 | _CrashCheck(result, file_or_files, "retrying w/out debug") |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 651 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 652 | if not result.returncode: |
| 653 | return SymbolGenerationResult.SUCCESS |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 654 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 655 | # If that didn't work (no debug, or dump_syms still failed), try |
| 656 | # dumping just the file itself directly. |
| 657 | result = _DumpIt([elf_file]) |
| 658 | if result.returncode: |
| 659 | # A lot of files (like kernel files) contain no debug information, |
| 660 | # do not consider such occurrences as errors. |
| 661 | cbuildbot_alerts.PrintBuildbotStepWarnings() |
| 662 | if b"file contains no debugging information" in result.stderr: |
| 663 | logging.warning("dump_syms failed; giving up entirely.") |
| 664 | logging.warning("No symbols found for %s", elf_file) |
| 665 | return SymbolGenerationResult.EXPECTED_FAILURE |
| 666 | else: |
| 667 | _CrashCheck(result, elf_file, "counting as failure") |
| 668 | return SymbolGenerationResult.UNEXPECTED_FAILURE |
| 669 | |
| 670 | return SymbolGenerationResult.SUCCESS |
| 671 | |
| 672 | def _DumpExpectingSymbols(): |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 673 | """Dump symbols for an ELF when we expect to get good symbols. |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 674 | |
| 675 | Returns: |
| 676 | A SymbolGenerationResult. We never expect failure, so the result |
| 677 | will always be SUCCESS or UNEXPECTED_FAILURE. |
| 678 | """ |
| 679 | if not debug_file: |
| 680 | logging.warning("%s must have debug file", elf_file) |
| 681 | return SymbolGenerationResult.UNEXPECTED_FAILURE |
| 682 | |
| 683 | cmd_args = [elf_file, os.path.dirname(debug_file)] |
| 684 | result = _DumpIt(cmd_args) |
| 685 | if result.returncode: |
Ian Barkley-Yeung | ab1dab1 | 2023-04-21 18:19:55 -0700 | [diff] [blame] | 686 | _CrashCheck( |
| 687 | result, |
| 688 | [elf_file, debug_file], |
| 689 | "unexpected symbol generation failure", |
| 690 | ) |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 691 | return SymbolGenerationResult.UNEXPECTED_FAILURE |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 692 | |
| 693 | # TODO(b/270240549): Remove try/except, allow exceptions to just |
| 694 | # fail the script. The try/except is just here until we are sure this |
| 695 | # will not break the build. |
| 696 | try: |
| 697 | if not ValidateSymbolFile( |
| 698 | temp.name, elf_file, sysroot, found_files |
| 699 | ): |
| 700 | logging.warning("%s: symbol file failed validation", elf_file) |
| 701 | return SymbolGenerationResult.UNEXPECTED_FAILURE |
| 702 | except ValueError as e: |
| 703 | logging.warning( |
| 704 | "%s: symbol file failed validation due to exception %s", |
| 705 | elf_file, |
| 706 | e, |
| 707 | ) |
| 708 | return SymbolGenerationResult.UNEXPECTED_FAILURE |
| 709 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 710 | return SymbolGenerationResult.SUCCESS |
| 711 | |
| 712 | osutils.SafeMakedirs(breakpad_dir) |
| 713 | with cros_build_lib.UnbufferedNamedTemporaryFile( |
| 714 | dir=breakpad_dir, delete=False |
| 715 | ) as temp: |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 716 | if not force_basic_fallback and _ExpectGoodSymbols(elf_file, sysroot): |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 717 | result = _DumpExpectingSymbols() |
| 718 | # Until the EXPECTED_POOR_SYMBOLIZATION_FILES allowlist is |
| 719 | # completely set up for all boards, don't fail the build if |
| 720 | # _ExpectGoodSymbols is wrong. |
| 721 | # TODO(b/241470012): Remove the call to _DumpAllowingBasicFallback() |
| 722 | # and just error out if _DumpExpectingSymbols fails. |
| 723 | if result == SymbolGenerationResult.UNEXPECTED_FAILURE: |
| 724 | result = _DumpAllowingBasicFallback() |
| 725 | else: |
| 726 | result = _DumpAllowingBasicFallback() |
| 727 | |
| 728 | if result == SymbolGenerationResult.UNEXPECTED_FAILURE: |
| 729 | num_errors.value += 1 |
| 730 | os.unlink(temp.name) |
| 731 | return num_errors.value |
| 732 | |
| 733 | if result == SymbolGenerationResult.EXPECTED_FAILURE: |
| 734 | os.unlink(temp.name) |
| 735 | return 0 |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 736 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 737 | # Move the dumped symbol file to the right place: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 738 | # /$SYSROOT/usr/lib/debug/breakpad/<module-name>/<id>/<module-name>.sym |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 739 | header = ReadSymsHeader(temp, elf_file) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 740 | logging.info("Dumped %s as %s : %s", elf_file, header.name, header.id) |
| 741 | sym_file = os.path.join( |
| 742 | breakpad_dir, header.name, header.id, header.name + ".sym" |
| 743 | ) |
| 744 | osutils.SafeMakedirs(os.path.dirname(sym_file)) |
| 745 | os.rename(temp.name, sym_file) |
| 746 | os.chmod(sym_file, 0o644) |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 747 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 748 | return sym_file |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 749 | |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 750 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 751 | def GenerateBreakpadSymbols( |
| 752 | board, |
| 753 | breakpad_dir=None, |
| 754 | strip_cfi=False, |
| 755 | generate_count=None, |
| 756 | sysroot=None, |
| 757 | num_processes=None, |
| 758 | clean_breakpad=False, |
| 759 | exclude_dirs=(), |
| 760 | file_list=None, |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 761 | always_use_basic_fallback=False, |
| 762 | ignore_expected_files=(), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 763 | ): |
| 764 | """Generate symbols for this board. |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 765 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 766 | If |file_list| is None, symbols are generated for all executables, otherwise |
| 767 | only for the files included in |file_list|. |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 768 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 769 | TODO(build): |
| 770 | This should be merged with buildbot_commands.GenerateBreakpadSymbols() |
| 771 | once we rewrite cros_generate_breakpad_symbols in python. |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 772 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 773 | Args: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 774 | board: The board whose symbols we wish to generate |
| 775 | breakpad_dir: The full path to the breakpad directory where symbols live |
| 776 | strip_cfi: Do not generate CFI data |
| 777 | generate_count: If set, only generate this many symbols (meant for |
| 778 | testing) |
| 779 | sysroot: The root where to find the corresponding ELFs |
| 780 | num_processes: Number of jobs to run in parallel |
| 781 | clean_breakpad: Should we `rm -rf` the breakpad output dir first; note: |
| 782 | we do not do any locking, so do not run more than one in parallel |
| 783 | when True |
| 784 | exclude_dirs: List of dirs (relative to |sysroot|) to not search |
| 785 | file_list: Only generate symbols for files in this list. Each file must |
| 786 | be a full path (including |sysroot| prefix). |
| 787 | TODO(build): Support paths w/o |sysroot|. |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 788 | always_use_basic_fallback: If True, use the "basic fallback" mode for |
| 789 | all symbol files. |
| 790 | ignore_expected_files: A list of ExpectedFiles that will not be |
| 791 | considered "missing" if we do not generate symbols for them. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 792 | |
| 793 | Returns: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 794 | The number of errors that were encountered. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 795 | """ |
| 796 | if sysroot is None: |
| 797 | sysroot = build_target_lib.get_default_sysroot_path(board) |
| 798 | if breakpad_dir is None: |
| 799 | breakpad_dir = FindBreakpadDir(board, sysroot=sysroot) |
| 800 | if clean_breakpad: |
| 801 | logging.info("cleaning out %s first", breakpad_dir) |
| 802 | osutils.RmDir(breakpad_dir, ignore_missing=True, sudo=True) |
| 803 | # Make sure non-root can write out symbols as needed. |
| 804 | osutils.SafeMakedirs(breakpad_dir, sudo=True) |
| 805 | if not os.access(breakpad_dir, os.W_OK): |
| 806 | cros_build_lib.sudo_run(["chown", "-R", str(os.getuid()), breakpad_dir]) |
| 807 | debug_dir = FindDebugDir(board, sysroot=sysroot) |
| 808 | exclude_paths = [os.path.join(debug_dir, x) for x in exclude_dirs] |
| 809 | if file_list is None: |
| 810 | file_list = [] |
| 811 | file_filter = dict.fromkeys([os.path.normpath(x) for x in file_list], False) |
| 812 | |
| 813 | logging.info("generating breakpad symbols using %s", debug_dir) |
| 814 | |
| 815 | # Let's locate all the debug_files and elfs first along with the debug file |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 816 | # sizes. This way we can start processing the largest files first in |
| 817 | # parallel with the small ones. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 818 | # If |file_list| was given, ignore all other files. |
| 819 | targets = [] |
| 820 | for root, dirs, files in os.walk(debug_dir): |
| 821 | if root in exclude_paths: |
| 822 | logging.info("Skipping excluded dir %s", root) |
| 823 | del dirs[:] |
| 824 | continue |
| 825 | |
| 826 | for debug_file in files: |
| 827 | debug_file = os.path.join(root, debug_file) |
| 828 | # Turn /build/$BOARD/usr/lib/debug/sbin/foo.debug into |
| 829 | # /build/$BOARD/sbin/foo. |
| 830 | elf_file = os.path.join( |
| 831 | sysroot, debug_file[len(debug_dir) + 1 : -6] |
| 832 | ) |
| 833 | |
| 834 | if file_filter: |
| 835 | if elf_file in file_filter: |
| 836 | file_filter[elf_file] = True |
| 837 | elif debug_file in file_filter: |
| 838 | file_filter[debug_file] = True |
| 839 | else: |
| 840 | continue |
| 841 | |
| 842 | # Filter out files based on common issues with the debug file. |
| 843 | if not debug_file.endswith(".debug"): |
| 844 | continue |
| 845 | |
| 846 | elif os.path.islink(debug_file): |
| 847 | # The build-id stuff is common enough to filter out by default. |
| 848 | if "/.build-id/" in debug_file: |
| 849 | msg = logging.debug |
| 850 | else: |
| 851 | msg = logging.warning |
| 852 | msg("Skipping symbolic link %s", debug_file) |
| 853 | continue |
| 854 | |
| 855 | # Filter out files based on common issues with the elf file. |
| 856 | elf_path = os.path.relpath(elf_file, sysroot) |
| 857 | debug_only = elf_path in ALLOWED_DEBUG_ONLY_FILES |
| 858 | if not os.path.exists(elf_file) and not debug_only: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 859 | # Sometimes we filter out programs from /usr/bin but leave |
| 860 | # behind the .debug file. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 861 | logging.warning("Skipping missing %s", elf_file) |
| 862 | continue |
| 863 | |
| 864 | targets.append((os.path.getsize(debug_file), elf_file, debug_file)) |
| 865 | |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 866 | with multiprocessing.Manager() as mp_manager: |
| 867 | bg_errors = parallel.WrapMultiprocessing(multiprocessing.Value, "i") |
| 868 | found_files = parallel.WrapMultiprocessing(mp_manager.list) |
| 869 | if file_filter: |
| 870 | files_not_found = [ |
| 871 | x for x, found in file_filter.items() if not found |
| 872 | ] |
| 873 | bg_errors.value += len(files_not_found) |
| 874 | if files_not_found: |
| 875 | logging.error( |
| 876 | "Failed to find requested files: %s", files_not_found |
| 877 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 878 | |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 879 | # Now start generating symbols for the discovered elfs. |
| 880 | with parallel.BackgroundTaskRunner( |
| 881 | GenerateBreakpadSymbol, |
| 882 | breakpad_dir=breakpad_dir, |
| 883 | strip_cfi=strip_cfi, |
| 884 | num_errors=bg_errors, |
| 885 | processes=num_processes, |
| 886 | sysroot=sysroot, |
| 887 | found_files=found_files, |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 888 | force_basic_fallback=always_use_basic_fallback, |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 889 | ) as queue: |
| 890 | for _, elf_file, debug_file in sorted(targets, reverse=True): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 891 | if generate_count == 0: |
| 892 | break |
| 893 | |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 894 | queue.put([elf_file, debug_file]) |
| 895 | if generate_count is not None: |
| 896 | generate_count -= 1 |
| 897 | if generate_count == 0: |
| 898 | break |
| 899 | |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 900 | missing = ( |
| 901 | ALL_EXPECTED_FILES |
| 902 | - frozenset(found_files) |
| 903 | - frozenset(ignore_expected_files) |
| 904 | ) |
| 905 | if ( |
| 906 | missing |
| 907 | and not file_filter |
| 908 | and generate_count is None |
| 909 | and not always_use_basic_fallback |
| 910 | ): |
Ian Barkley-Yeung | c5b6f58 | 2023-03-08 18:23:07 -0800 | [diff] [blame] | 911 | logging.warning( |
| 912 | "Not all expected files were processed successfully, " |
| 913 | "missing %s", |
| 914 | missing, |
| 915 | ) |
| 916 | # TODO(b/270240549): Increment bg_errors.value here once we check |
| 917 | # that this isn't going to fail any current builds. |
| 918 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 919 | return bg_errors.value |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 920 | |
| 921 | |
Mike Frysinger | 3f571af | 2016-08-31 23:56:53 -0400 | [diff] [blame] | 922 | def FindDebugDir(board, sysroot=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 923 | """Given a |board|, return the path to the split debug dir for it""" |
| 924 | if sysroot is None: |
| 925 | sysroot = build_target_lib.get_default_sysroot_path(board) |
| 926 | return os.path.join(sysroot, "usr", "lib", "debug") |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 927 | |
| 928 | |
Mike Frysinger | 3f571af | 2016-08-31 23:56:53 -0400 | [diff] [blame] | 929 | def FindBreakpadDir(board, sysroot=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 930 | """Given a |board|, return the path to the breakpad dir for it""" |
| 931 | return os.path.join(FindDebugDir(board, sysroot=sysroot), "breakpad") |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 932 | |
| 933 | |
| 934 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 935 | parser = commandline.ArgumentParser(description=__doc__) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 936 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 937 | parser.add_argument( |
| 938 | "--board", default=None, help="board to generate symbols for" |
| 939 | ) |
| 940 | parser.add_argument( |
| 941 | "--breakpad_root", |
| 942 | type="path", |
| 943 | default=None, |
| 944 | help="root output directory for breakpad symbols", |
| 945 | ) |
| 946 | parser.add_argument( |
| 947 | "--sysroot", |
| 948 | type="path", |
| 949 | default=None, |
| 950 | help="root input directory for files", |
| 951 | ) |
| 952 | parser.add_argument( |
| 953 | "--exclude-dir", |
| 954 | type=str, |
| 955 | action="append", |
| 956 | default=[], |
| 957 | help="directory (relative to |board| root) to not search", |
| 958 | ) |
| 959 | parser.add_argument( |
| 960 | "--generate-count", |
| 961 | type=int, |
| 962 | default=None, |
| 963 | help="only generate # number of symbols", |
| 964 | ) |
| 965 | parser.add_argument( |
| 966 | "--noclean", |
| 967 | dest="clean", |
| 968 | action="store_false", |
| 969 | default=True, |
| 970 | help="do not clean out breakpad dir before running", |
| 971 | ) |
| 972 | parser.add_argument( |
| 973 | "--jobs", type=int, default=None, help="limit number of parallel jobs" |
| 974 | ) |
| 975 | parser.add_argument( |
| 976 | "--strip_cfi", |
| 977 | action="store_true", |
| 978 | default=False, |
| 979 | help="do not generate CFI data (pass -c to dump_syms)", |
| 980 | ) |
| 981 | parser.add_argument( |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 982 | "--ignore_errors", |
| 983 | action="store_true", |
| 984 | default=False, |
| 985 | help="Ignore errors from dump_syms, do not validate symbol files, " |
| 986 | "just generate symbols best effort", |
| 987 | ) |
| 988 | parser.add_argument( |
| 989 | "--ignore_expected_file", |
| 990 | type=str, |
| 991 | action="append", |
| 992 | default=[], |
| 993 | choices=[x.name for x in ExpectedFiles], |
| 994 | help="do not generate errors if symbols are not generated for these " |
| 995 | "files", |
| 996 | ) |
| 997 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 998 | "file_list", |
| 999 | nargs="*", |
| 1000 | default=None, |
Trent Apted | c4f366a | 2023-05-16 15:32:48 +1000 | [diff] [blame] | 1001 | help=( |
| 1002 | "generate symbols for only these files " |
| 1003 | "(e.g. /build/$BOARD/usr/bin/foo)" |
| 1004 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1005 | ) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 1006 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1007 | opts = parser.parse_args(argv) |
| 1008 | opts.Freeze() |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 1009 | ignore_expected_files = [ |
| 1010 | ExpectedFiles[x] for x in opts.ignore_expected_file |
| 1011 | ] |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 1012 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1013 | if opts.board is None and opts.sysroot is None: |
| 1014 | cros_build_lib.Die("--board or --sysroot is required") |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 1015 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1016 | ret = GenerateBreakpadSymbols( |
| 1017 | opts.board, |
| 1018 | breakpad_dir=opts.breakpad_root, |
| 1019 | strip_cfi=opts.strip_cfi, |
| 1020 | generate_count=opts.generate_count, |
| 1021 | sysroot=opts.sysroot, |
| 1022 | num_processes=opts.jobs, |
| 1023 | clean_breakpad=opts.clean, |
| 1024 | exclude_dirs=opts.exclude_dir, |
| 1025 | file_list=opts.file_list, |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 1026 | always_use_basic_fallback=opts.ignore_errors, |
| 1027 | ignore_expected_files=ignore_expected_files, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1028 | ) |
| 1029 | if ret: |
| 1030 | logging.error("encountered %i problem(s)", ret) |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 1031 | # Since exit(status) gets masked, clamp it to 1 so we don't |
| 1032 | # inadvertently return 0 in case we are a multiple of the mask. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1033 | ret = 1 |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 1034 | |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 1035 | if opts.ignore_errors: |
| 1036 | return 0 |
| 1037 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1038 | return ret |