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 |
| 24 | import os |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 25 | |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 26 | from chromite.cbuildbot import cbuildbot_alerts |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 27 | from chromite.lib import build_target_lib |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 28 | from chromite.lib import commandline |
| 29 | from chromite.lib import cros_build_lib |
| 30 | from chromite.lib import osutils |
| 31 | from chromite.lib import parallel |
Mike Frysinger | 96ad3f2 | 2014-04-24 23:27:27 -0400 | [diff] [blame] | 32 | from chromite.lib import signals |
Alex Klein | 1809f57 | 2021-09-09 11:28:37 -0600 | [diff] [blame] | 33 | from chromite.utils import file_util |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 34 | |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [diff] [blame] | 35 | |
Stephen Boyd | fc1c803 | 2021-10-06 20:58:37 -0700 | [diff] [blame] | 36 | # Elf files that don't exist but have a split .debug file installed. |
| 37 | ALLOWED_DEBUG_ONLY_FILES = { |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | "boot/vmlinux", |
Stephen Boyd | fc1c803 | 2021-10-06 20:58:37 -0700 | [diff] [blame] | 39 | } |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 40 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 41 | # Allowlist of elf files that we know we can't symbolize in the normal way, but |
| 42 | # which we don't have an automatic way to detect. |
| 43 | EXPECTED_POOR_SYMBOLIZATION_FILES = ALLOWED_DEBUG_ONLY_FILES | { |
| 44 | # Git binaries are downloaded as binary blobs already stripped. |
| 45 | "usr/bin/git", |
| 46 | "usr/bin/git-receive-pack", |
| 47 | "usr/bin/git-upload-archive", |
| 48 | "usr/bin/git-upload-pack", |
| 49 | # Prebuild Android binary |
| 50 | "build/rootfs/opt/google/vms/android/etc/bin/XkbToKcmConverter", |
| 51 | # Pulled from |
| 52 | # https://skia.googlesource.com/buildbot/+/refs/heads/main/gold-client/, no |
| 53 | # need to resymbolize. |
| 54 | "usr/bin/goldctl", |
| 55 | # This is complete for --board=eve |
| 56 | # TODO(b/241470012): Complete for other boards. |
| 57 | } |
| 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | SymbolHeader = collections.namedtuple( |
| 60 | "SymbolHeader", |
| 61 | ( |
| 62 | "cpu", |
| 63 | "id", |
| 64 | "name", |
| 65 | "os", |
| 66 | ), |
| 67 | ) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 68 | |
| 69 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 70 | class SymbolGenerationResult(enum.Enum): |
| 71 | """Result of running dump_syms |
| 72 | |
| 73 | Return value of _DumpAllowingBasicFallback() and _DumpExpectingSymbols(). |
| 74 | """ |
| 75 | |
| 76 | SUCCESS = 1 |
| 77 | UNEXPECTED_FAILURE = 2 |
| 78 | EXPECTED_FAILURE = 3 |
| 79 | |
| 80 | |
| 81 | def _ExpectGoodSymbols(elf_file, sysroot): |
| 82 | """Determines if we expect dump_syms to create good symbols |
| 83 | |
| 84 | We know that certain types of files never generate good symbols. Distinguish |
| 85 | those from the majority of elf files which should generate good symbols. |
| 86 | |
| 87 | Args: |
| 88 | elf_file: The complete path to the file which we will pass to dump_syms |
| 89 | sysroot: If not None, the root of the build directory ('/build/eve', for |
| 90 | instance) |
| 91 | |
| 92 | Returns: |
| 93 | True if the elf file should generate good symbols, False if not. |
| 94 | """ |
| 95 | # .ko files (kernel object files) never produce good symbols. |
| 96 | if elf_file.endswith(".ko"): |
| 97 | return False |
| 98 | |
| 99 | # dump_syms doesn't understand Golang executables. |
| 100 | result = cros_build_lib.run( |
| 101 | ["/usr/bin/file", elf_file], print_cmd=False, stdout=True |
| 102 | ) |
| 103 | if b"Go BuildID" in result.stdout: |
| 104 | return False |
| 105 | |
| 106 | if sysroot is not None: |
| 107 | relative_path = os.path.relpath(elf_file, sysroot) |
| 108 | else: |
| 109 | relative_path = elf_file |
| 110 | |
| 111 | if relative_path in EXPECTED_POOR_SYMBOLIZATION_FILES: |
| 112 | return False |
| 113 | |
| 114 | return True |
| 115 | |
| 116 | |
| 117 | def ReadSymsHeader(sym_file, name_for_errors): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | """Parse the header of the symbol file |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | The first line of the syms file will read like: |
| 121 | MODULE Linux arm F4F6FA6CCBDEF455039C8DE869C8A2F40 blkid |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 122 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | https://code.google.com/p/google-breakpad/wiki/SymbolFiles |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | Args: |
| 126 | sym_file: The symbol file to parse |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 127 | name_for_errors: A name for error strings. Can be the name of the elf file |
| 128 | that generated the symbol file, or the name of the symbol file if the |
| 129 | symbol file has already been moved to a meaningful location. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | Returns: |
| 132 | A SymbolHeader object |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 133 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | Raises: |
| 135 | ValueError if the first line of |sym_file| is invalid |
| 136 | """ |
| 137 | with file_util.Open(sym_file, "rb") as f: |
| 138 | header = f.readline().decode("utf-8").split() |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 139 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 140 | if len(header) != 5 or header[0] != "MODULE": |
| 141 | raise ValueError( |
| 142 | f"header of sym file from {name_for_errors} is invalid" |
| 143 | ) |
Mike Frysinger | 50cedd3 | 2014-02-09 23:03:18 -0500 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | return SymbolHeader( |
| 146 | os=header[1], cpu=header[2], id=header[3], name=header[4] |
| 147 | ) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 148 | |
| 149 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | def GenerateBreakpadSymbol( |
| 151 | elf_file, |
| 152 | debug_file=None, |
| 153 | breakpad_dir=None, |
| 154 | strip_cfi=False, |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 155 | sysroot=None, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 156 | num_errors=None, |
| 157 | dump_syms_cmd="dump_syms", |
| 158 | ): |
| 159 | """Generate the symbols for |elf_file| using |debug_file| |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 160 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | Args: |
| 162 | elf_file: The file to dump symbols for |
| 163 | debug_file: Split debug file to use for symbol information |
| 164 | breakpad_dir: The dir to store the output symbol file in |
| 165 | strip_cfi: Do not generate CFI data |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 166 | sysroot: Path to the sysroot with the elf_file under it |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 167 | num_errors: An object to update with the error count (needs a .value member) |
| 168 | dump_syms_cmd: Command to use for dumping symbols. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 169 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | Returns: |
| 171 | The name of symbol file written out on success, or the failure count. |
| 172 | """ |
| 173 | assert breakpad_dir |
| 174 | if num_errors is None: |
| 175 | num_errors = ctypes.c_int() |
| 176 | debug_file_only = not os.path.exists(elf_file) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | cmd_base = [dump_syms_cmd, "-v"] |
| 179 | if strip_cfi: |
| 180 | cmd_base += ["-c"] |
| 181 | # Some files will not be readable by non-root (e.g. set*id /bin/su). |
| 182 | needs_sudo = not os.access(elf_file, os.R_OK) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 183 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 184 | def _DumpIt(cmd_args): |
| 185 | if needs_sudo: |
| 186 | run_command = cros_build_lib.sudo_run |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 187 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | run_command = cros_build_lib.run |
| 189 | return run_command( |
| 190 | cmd_base + cmd_args, |
| 191 | stderr=True, |
| 192 | stdout=temp.name, |
| 193 | check=False, |
| 194 | debug_level=logging.DEBUG, |
| 195 | ) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 196 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 197 | def _CrashCheck(result, file_or_files, msg): |
| 198 | if result.returncode: |
| 199 | cbuildbot_alerts.PrintBuildbotStepWarnings() |
| 200 | if result.returncode < 0: |
| 201 | logging.warning( |
| 202 | "dump_syms %s crashed with %s; %s", |
| 203 | file_or_files, |
| 204 | signals.StrSignal(-result.returncode), |
| 205 | msg, |
| 206 | ) |
| 207 | else: |
| 208 | logging.warning( |
| 209 | "dump_syms %s returned %d; %s", |
| 210 | file_or_files, |
| 211 | result.returncode, |
| 212 | msg, |
| 213 | ) |
| 214 | logging.warning("output:\n%s", result.stderr.decode("utf-8")) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 215 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 216 | def _DumpAllowingBasicFallback(): |
| 217 | """Dump symbols for a executable when we do NOT expect to get good symbols. |
| 218 | |
| 219 | Returns: |
| 220 | A SymbolGenerationResult |
| 221 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 222 | if debug_file: |
| 223 | # Try to dump the symbols using the debug file like normal. |
| 224 | if debug_file_only: |
| 225 | cmd_args = [debug_file] |
| 226 | file_or_files = debug_file |
| 227 | else: |
| 228 | cmd_args = [elf_file, os.path.dirname(debug_file)] |
| 229 | file_or_files = [elf_file, debug_file] |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 230 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 231 | result = _DumpIt(cmd_args) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 232 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 233 | if result.returncode: |
| 234 | # Sometimes dump_syms can crash because there's too much info. |
| 235 | # Try dumping and stripping the extended stuff out. At least |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 236 | # this way we'll get the extended symbols. |
| 237 | # https://crbug.com/266064 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 238 | _CrashCheck(result, file_or_files, "retrying w/out CFI") |
| 239 | cmd_args = ["-c", "-r"] + cmd_args |
| 240 | result = _DumpIt(cmd_args) |
| 241 | _CrashCheck(result, file_or_files, "retrying w/out debug") |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 242 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 243 | if not result.returncode: |
| 244 | return SymbolGenerationResult.SUCCESS |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 245 | |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 246 | # If that didn't work (no debug, or dump_syms still failed), try |
| 247 | # dumping just the file itself directly. |
| 248 | result = _DumpIt([elf_file]) |
| 249 | if result.returncode: |
| 250 | # A lot of files (like kernel files) contain no debug information, |
| 251 | # do not consider such occurrences as errors. |
| 252 | cbuildbot_alerts.PrintBuildbotStepWarnings() |
| 253 | if b"file contains no debugging information" in result.stderr: |
| 254 | logging.warning("dump_syms failed; giving up entirely.") |
| 255 | logging.warning("No symbols found for %s", elf_file) |
| 256 | return SymbolGenerationResult.EXPECTED_FAILURE |
| 257 | else: |
| 258 | _CrashCheck(result, elf_file, "counting as failure") |
| 259 | return SymbolGenerationResult.UNEXPECTED_FAILURE |
| 260 | |
| 261 | return SymbolGenerationResult.SUCCESS |
| 262 | |
| 263 | def _DumpExpectingSymbols(): |
| 264 | """Dump symbols for a executable when we expect to get good symbols. |
| 265 | |
| 266 | Returns: |
| 267 | A SymbolGenerationResult. We never expect failure, so the result |
| 268 | will always be SUCCESS or UNEXPECTED_FAILURE. |
| 269 | """ |
| 270 | if not debug_file: |
| 271 | logging.warning("%s must have debug file", elf_file) |
| 272 | return SymbolGenerationResult.UNEXPECTED_FAILURE |
| 273 | |
| 274 | cmd_args = [elf_file, os.path.dirname(debug_file)] |
| 275 | result = _DumpIt(cmd_args) |
| 276 | if result.returncode: |
| 277 | _CrashCheck(result, [elf_file, debug_file], "unexpected failure") |
| 278 | return SymbolGenerationResult.UNEXPECTED_FAILURE |
| 279 | return SymbolGenerationResult.SUCCESS |
| 280 | |
| 281 | osutils.SafeMakedirs(breakpad_dir) |
| 282 | with cros_build_lib.UnbufferedNamedTemporaryFile( |
| 283 | dir=breakpad_dir, delete=False |
| 284 | ) as temp: |
| 285 | if _ExpectGoodSymbols(elf_file, sysroot): |
| 286 | result = _DumpExpectingSymbols() |
| 287 | # Until the EXPECTED_POOR_SYMBOLIZATION_FILES allowlist is |
| 288 | # completely set up for all boards, don't fail the build if |
| 289 | # _ExpectGoodSymbols is wrong. |
| 290 | # TODO(b/241470012): Remove the call to _DumpAllowingBasicFallback() |
| 291 | # and just error out if _DumpExpectingSymbols fails. |
| 292 | if result == SymbolGenerationResult.UNEXPECTED_FAILURE: |
| 293 | result = _DumpAllowingBasicFallback() |
| 294 | else: |
| 295 | result = _DumpAllowingBasicFallback() |
| 296 | |
| 297 | if result == SymbolGenerationResult.UNEXPECTED_FAILURE: |
| 298 | num_errors.value += 1 |
| 299 | os.unlink(temp.name) |
| 300 | return num_errors.value |
| 301 | |
| 302 | if result == SymbolGenerationResult.EXPECTED_FAILURE: |
| 303 | os.unlink(temp.name) |
| 304 | return 0 |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 305 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 306 | # Move the dumped symbol file to the right place: |
| 307 | # /build/$BOARD/usr/lib/debug/breakpad/<module-name>/<id>/<module-name>.sym |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 308 | header = ReadSymsHeader(temp, elf_file) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 309 | logging.info("Dumped %s as %s : %s", elf_file, header.name, header.id) |
| 310 | sym_file = os.path.join( |
| 311 | breakpad_dir, header.name, header.id, header.name + ".sym" |
| 312 | ) |
| 313 | osutils.SafeMakedirs(os.path.dirname(sym_file)) |
| 314 | os.rename(temp.name, sym_file) |
| 315 | os.chmod(sym_file, 0o644) |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 316 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 317 | return sym_file |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 318 | |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 319 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 320 | def GenerateBreakpadSymbols( |
| 321 | board, |
| 322 | breakpad_dir=None, |
| 323 | strip_cfi=False, |
| 324 | generate_count=None, |
| 325 | sysroot=None, |
| 326 | num_processes=None, |
| 327 | clean_breakpad=False, |
| 328 | exclude_dirs=(), |
| 329 | file_list=None, |
| 330 | ): |
| 331 | """Generate symbols for this board. |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 332 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 333 | If |file_list| is None, symbols are generated for all executables, otherwise |
| 334 | only for the files included in |file_list|. |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 335 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 336 | TODO(build): |
| 337 | This should be merged with buildbot_commands.GenerateBreakpadSymbols() |
| 338 | once we rewrite cros_generate_breakpad_symbols in python. |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 339 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 340 | Args: |
| 341 | board: The board whose symbols we wish to generate |
| 342 | breakpad_dir: The full path to the breakpad directory where symbols live |
| 343 | strip_cfi: Do not generate CFI data |
| 344 | generate_count: If set, only generate this many symbols (meant for testing) |
| 345 | sysroot: The root where to find the corresponding ELFs |
| 346 | num_processes: Number of jobs to run in parallel |
| 347 | clean_breakpad: Should we `rm -rf` the breakpad output dir first; note: we |
| 348 | do not do any locking, so do not run more than one in parallel when True |
| 349 | exclude_dirs: List of dirs (relative to |sysroot|) to not search |
| 350 | file_list: Only generate symbols for files in this list. Each file must be a |
| 351 | full path (including |sysroot| prefix). |
| 352 | TODO(build): Support paths w/o |sysroot|. |
| 353 | |
| 354 | Returns: |
| 355 | The number of errors that were encountered. |
| 356 | """ |
| 357 | if sysroot is None: |
| 358 | sysroot = build_target_lib.get_default_sysroot_path(board) |
| 359 | if breakpad_dir is None: |
| 360 | breakpad_dir = FindBreakpadDir(board, sysroot=sysroot) |
| 361 | if clean_breakpad: |
| 362 | logging.info("cleaning out %s first", breakpad_dir) |
| 363 | osutils.RmDir(breakpad_dir, ignore_missing=True, sudo=True) |
| 364 | # Make sure non-root can write out symbols as needed. |
| 365 | osutils.SafeMakedirs(breakpad_dir, sudo=True) |
| 366 | if not os.access(breakpad_dir, os.W_OK): |
| 367 | cros_build_lib.sudo_run(["chown", "-R", str(os.getuid()), breakpad_dir]) |
| 368 | debug_dir = FindDebugDir(board, sysroot=sysroot) |
| 369 | exclude_paths = [os.path.join(debug_dir, x) for x in exclude_dirs] |
| 370 | if file_list is None: |
| 371 | file_list = [] |
| 372 | file_filter = dict.fromkeys([os.path.normpath(x) for x in file_list], False) |
| 373 | |
| 374 | logging.info("generating breakpad symbols using %s", debug_dir) |
| 375 | |
| 376 | # Let's locate all the debug_files and elfs first along with the debug file |
| 377 | # sizes. This way we can start processing the largest files first in parallel |
| 378 | # with the small ones. |
| 379 | # If |file_list| was given, ignore all other files. |
| 380 | targets = [] |
| 381 | for root, dirs, files in os.walk(debug_dir): |
| 382 | if root in exclude_paths: |
| 383 | logging.info("Skipping excluded dir %s", root) |
| 384 | del dirs[:] |
| 385 | continue |
| 386 | |
| 387 | for debug_file in files: |
| 388 | debug_file = os.path.join(root, debug_file) |
| 389 | # Turn /build/$BOARD/usr/lib/debug/sbin/foo.debug into |
| 390 | # /build/$BOARD/sbin/foo. |
| 391 | elf_file = os.path.join( |
| 392 | sysroot, debug_file[len(debug_dir) + 1 : -6] |
| 393 | ) |
| 394 | |
| 395 | if file_filter: |
| 396 | if elf_file in file_filter: |
| 397 | file_filter[elf_file] = True |
| 398 | elif debug_file in file_filter: |
| 399 | file_filter[debug_file] = True |
| 400 | else: |
| 401 | continue |
| 402 | |
| 403 | # Filter out files based on common issues with the debug file. |
| 404 | if not debug_file.endswith(".debug"): |
| 405 | continue |
| 406 | |
| 407 | elif os.path.islink(debug_file): |
| 408 | # The build-id stuff is common enough to filter out by default. |
| 409 | if "/.build-id/" in debug_file: |
| 410 | msg = logging.debug |
| 411 | else: |
| 412 | msg = logging.warning |
| 413 | msg("Skipping symbolic link %s", debug_file) |
| 414 | continue |
| 415 | |
| 416 | # Filter out files based on common issues with the elf file. |
| 417 | elf_path = os.path.relpath(elf_file, sysroot) |
| 418 | debug_only = elf_path in ALLOWED_DEBUG_ONLY_FILES |
| 419 | if not os.path.exists(elf_file) and not debug_only: |
| 420 | # Sometimes we filter out programs from /usr/bin but leave behind |
| 421 | # the .debug file. |
| 422 | logging.warning("Skipping missing %s", elf_file) |
| 423 | continue |
| 424 | |
| 425 | targets.append((os.path.getsize(debug_file), elf_file, debug_file)) |
| 426 | |
| 427 | bg_errors = parallel.WrapMultiprocessing(multiprocessing.Value, "i") |
| 428 | if file_filter: |
| 429 | files_not_found = [x for x, found in file_filter.items() if not found] |
| 430 | bg_errors.value += len(files_not_found) |
| 431 | if files_not_found: |
| 432 | logging.error("Failed to find requested files: %s", files_not_found) |
| 433 | |
| 434 | # Now start generating symbols for the discovered elfs. |
| 435 | with parallel.BackgroundTaskRunner( |
| 436 | GenerateBreakpadSymbol, |
| 437 | breakpad_dir=breakpad_dir, |
| 438 | strip_cfi=strip_cfi, |
| 439 | num_errors=bg_errors, |
| 440 | processes=num_processes, |
Ian Barkley-Yeung | fdaaf2e | 2023-03-02 17:30:39 -0800 | [diff] [blame] | 441 | sysroot=sysroot, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 442 | ) as queue: |
| 443 | for _, elf_file, debug_file in sorted(targets, reverse=True): |
| 444 | if generate_count == 0: |
| 445 | break |
| 446 | |
| 447 | queue.put([elf_file, debug_file]) |
| 448 | if generate_count is not None: |
| 449 | generate_count -= 1 |
| 450 | if generate_count == 0: |
| 451 | break |
| 452 | |
| 453 | return bg_errors.value |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 454 | |
| 455 | |
Mike Frysinger | 3f571af | 2016-08-31 23:56:53 -0400 | [diff] [blame] | 456 | def FindDebugDir(board, sysroot=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 457 | """Given a |board|, return the path to the split debug dir for it""" |
| 458 | if sysroot is None: |
| 459 | sysroot = build_target_lib.get_default_sysroot_path(board) |
| 460 | return os.path.join(sysroot, "usr", "lib", "debug") |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 461 | |
| 462 | |
Mike Frysinger | 3f571af | 2016-08-31 23:56:53 -0400 | [diff] [blame] | 463 | def FindBreakpadDir(board, sysroot=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 464 | """Given a |board|, return the path to the breakpad dir for it""" |
| 465 | return os.path.join(FindDebugDir(board, sysroot=sysroot), "breakpad") |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 466 | |
| 467 | |
| 468 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 469 | parser = commandline.ArgumentParser(description=__doc__) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 470 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 471 | parser.add_argument( |
| 472 | "--board", default=None, help="board to generate symbols for" |
| 473 | ) |
| 474 | parser.add_argument( |
| 475 | "--breakpad_root", |
| 476 | type="path", |
| 477 | default=None, |
| 478 | help="root output directory for breakpad symbols", |
| 479 | ) |
| 480 | parser.add_argument( |
| 481 | "--sysroot", |
| 482 | type="path", |
| 483 | default=None, |
| 484 | help="root input directory for files", |
| 485 | ) |
| 486 | parser.add_argument( |
| 487 | "--exclude-dir", |
| 488 | type=str, |
| 489 | action="append", |
| 490 | default=[], |
| 491 | help="directory (relative to |board| root) to not search", |
| 492 | ) |
| 493 | parser.add_argument( |
| 494 | "--generate-count", |
| 495 | type=int, |
| 496 | default=None, |
| 497 | help="only generate # number of symbols", |
| 498 | ) |
| 499 | parser.add_argument( |
| 500 | "--noclean", |
| 501 | dest="clean", |
| 502 | action="store_false", |
| 503 | default=True, |
| 504 | help="do not clean out breakpad dir before running", |
| 505 | ) |
| 506 | parser.add_argument( |
| 507 | "--jobs", type=int, default=None, help="limit number of parallel jobs" |
| 508 | ) |
| 509 | parser.add_argument( |
| 510 | "--strip_cfi", |
| 511 | action="store_true", |
| 512 | default=False, |
| 513 | help="do not generate CFI data (pass -c to dump_syms)", |
| 514 | ) |
| 515 | parser.add_argument( |
| 516 | "file_list", |
| 517 | nargs="*", |
| 518 | default=None, |
| 519 | help="generate symbols for only these files " |
| 520 | "(e.g. /build/$BOARD/usr/bin/foo)", |
| 521 | ) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 522 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 523 | opts = parser.parse_args(argv) |
| 524 | opts.Freeze() |
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 | if opts.board is None and opts.sysroot is None: |
| 527 | cros_build_lib.Die("--board or --sysroot is required") |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 528 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 529 | ret = GenerateBreakpadSymbols( |
| 530 | opts.board, |
| 531 | breakpad_dir=opts.breakpad_root, |
| 532 | strip_cfi=opts.strip_cfi, |
| 533 | generate_count=opts.generate_count, |
| 534 | sysroot=opts.sysroot, |
| 535 | num_processes=opts.jobs, |
| 536 | clean_breakpad=opts.clean, |
| 537 | exclude_dirs=opts.exclude_dir, |
| 538 | file_list=opts.file_list, |
| 539 | ) |
| 540 | if ret: |
| 541 | logging.error("encountered %i problem(s)", ret) |
| 542 | # Since exit(status) gets masked, clamp it to 1 so we don't inadvertently |
| 543 | # return 0 in case we are a multiple of the mask. |
| 544 | ret = 1 |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 545 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 546 | return ret |