Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2017 The ChromiumOS Authors |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [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 | This script takes expanded crash symbols published by the Android build, and |
| 8 | converts them to breakpad format. |
| 9 | """ |
| 10 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 11 | import logging |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 12 | import multiprocessing |
| 13 | import os |
| 14 | import re |
| 15 | import zipfile |
| 16 | |
| 17 | from chromite.lib import commandline |
| 18 | from chromite.lib import cros_build_lib |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 19 | from chromite.lib import osutils |
| 20 | from chromite.lib import parallel |
| 21 | from chromite.scripts import cros_generate_breakpad_symbols |
| 22 | |
| 23 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 24 | RELOCATION_PACKER_BIN = "relocation_packer" |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 25 | |
| 26 | # These regexps match each type of address we have to adjust. |
| 27 | ADDRESS_REGEXPS = ( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | re.compile(r"^FUNC ([0-9a-f]+)"), |
| 29 | re.compile(r"^([0-9a-f]+)"), |
| 30 | re.compile(r"^PUBLIC ([0-9a-f]+)"), |
| 31 | re.compile(r"^STACK CFI INIT ([0-9a-f]+)"), |
| 32 | re.compile(r"^STACK CFI ([0-9a-f]+)"), |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | |
| 36 | class OffsetDiscoveryError(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | """Raised if we can't find the offset after unpacking symbols.""" |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def FindExpansionOffset(unpack_result): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | """Helper to extract symbols offset from relocation_packer output. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | This can accept and handle both successful and failed unpack command output. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | Will return 0 if no adjustment is needed. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | Args: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 48 | unpack_result: CompletedProcess from the relocation_packer command. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | Returns: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 51 | Integer offset to adjust symbols by. May be 0. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | Raises: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 54 | OffsetDiscoveryError: if the unpack succeeds, but we can't parse the |
| 55 | output. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | """ |
| 57 | if unpack_result.returncode != 0: |
| 58 | return 0 |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | # Look for the number of relocations as a confidence check that we got the |
| 61 | # expected output. Note that we don't otherwise care about this value. |
| 62 | relocations_match = re.search( |
| 63 | r"INFO: Relocations +: +(\d+) entries", unpack_result.stdout |
| 64 | ) |
| 65 | if not relocations_match: |
| 66 | raise OffsetDiscoveryError( |
| 67 | "No Relocations in: %s" % unpack_result.stdout |
| 68 | ) |
Lloyd Pique | e5f4f0f | 2019-02-12 14:17:32 -0800 | [diff] [blame] | 69 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | # An "Expansion" line is only written if the value is nonzero. |
| 71 | offset_match = re.search( |
| 72 | r"INFO: Expansion +: +(\d+) bytes", unpack_result.stdout |
| 73 | ) |
| 74 | if not offset_match: |
| 75 | return 0 |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | # Return offset as a negative number. |
| 78 | return -int(offset_match.group(1)) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def _AdjustLineSymbolOffset(line, offset): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | """Adjust the symbol offset for one line of a breakpad file. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | Args: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 85 | line: One line of the file. |
| 86 | offset: int to adjust the symbol by. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 87 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | Returns: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 89 | The adjusted line, or original line if there is no change. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | """ |
| 91 | for regexp in ADDRESS_REGEXPS: |
| 92 | m = regexp.search(line) |
| 93 | if m: |
| 94 | address = int(m.group(1), 16) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 95 | |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 96 | # We ignore 0 addresses, since the zeros are fillers for unknowns. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | if address: |
| 98 | address += offset |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 99 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | # Return the same line with address adjusted. |
| 101 | return "%s%x%s" % (line[: m.start(1)], address, line[m.end(1) :]) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | # Nothing recognized, no adjustment. |
| 104 | return line |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def _AdjustSymbolOffset(breakpad_file, offset): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | """Given a breakpad file, adjust the symbols by offset. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 109 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | Updates the file in place. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 111 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | Args: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 113 | breakpad_file: File to read and update in place. |
| 114 | offset: Integer to move symbols by. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 115 | """ |
| 116 | logging.info( |
| 117 | "Adjusting symbols in %s with offset %d.", breakpad_file, offset |
| 118 | ) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | # Keep newlines. |
| 121 | lines = osutils.ReadFile(breakpad_file).splitlines(True) |
| 122 | adjusted_lines = [_AdjustLineSymbolOffset(line, offset) for line in lines] |
| 123 | osutils.WriteFile(breakpad_file, "".join(adjusted_lines)) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 124 | |
| 125 | |
| 126 | def _UnpackGenerateBreakpad(elf_file, *args, **kwargs): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | """Unpack Android relocation symbols, and GenerateBreakpadSymbol |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | This method accepts exactly the same arguments as |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 130 | cros_generate_breakpad_symbols.GenerateBreakpadSymbol, except that it |
| 131 | requires elf_file, and fills in dump_sym_cmd. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | Args: |
Alex Klein | 361062b | 2023-04-05 09:45:28 -0600 | [diff] [blame] | 134 | elf_file: Name of the file to generate breakpad symbols for. |
| 135 | *args: See cros_generate_breakpad_symbols.GenerateBreakpadSymbol. |
| 136 | **kwargs: See cros_generate_breakpad_symbols.GenerateBreakpadSymbol. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 137 | """ |
| 138 | # We try to unpack, and just see if it works. Real failures caused by |
| 139 | # something other than a binary that's already unpacked will be logged and |
| 140 | # ignored. We'll notice them when dump_syms fails later (which it will on |
| 141 | # packed binaries.). |
| 142 | unpack_cmd = [RELOCATION_PACKER_BIN, "-u", elf_file] |
| 143 | unpack_result = cros_build_lib.run( |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 144 | unpack_cmd, stdout=True, check=False, encoding="utf-8" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | ) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 146 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | # If we unpacked, extract the offset, and remember it. |
| 148 | offset = FindExpansionOffset(unpack_result) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 149 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | if offset: |
| 151 | logging.info( |
| 152 | "Unpacked relocation symbols for %s with offset %d.", |
| 153 | elf_file, |
| 154 | offset, |
| 155 | ) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 156 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | # Now generate breakpad symbols from the binary. |
| 158 | breakpad_file = cros_generate_breakpad_symbols.GenerateBreakpadSymbol( |
| 159 | elf_file, *args, **kwargs |
| 160 | ) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 161 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 162 | if isinstance(breakpad_file, int): |
| 163 | logging.error("Unable to generate symbols for %s", elf_file) |
| 164 | elif offset: |
| 165 | _AdjustSymbolOffset(breakpad_file, offset) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 166 | |
| 167 | |
| 168 | def GenerateBreakpadSymbols(breakpad_dir, symbols_dir): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 169 | """Generate symbols for all binaries in symbols_dir. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 170 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | Args: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 172 | breakpad_dir: The full path in which to write out breakpad symbols. |
| 173 | symbols_dir: The full path to the binaries to process from. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 174 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | Returns: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 176 | The number of errors that were encountered. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 177 | """ |
| 178 | osutils.SafeMakedirs(breakpad_dir) |
| 179 | logging.info("generating breakpad symbols from %s", symbols_dir) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 180 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 181 | num_errors = parallel.WrapMultiprocessing(multiprocessing.Value, "i") |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 182 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 183 | # Now start generating symbols for the discovered elfs. |
| 184 | with parallel.BackgroundTaskRunner( |
| 185 | _UnpackGenerateBreakpad, |
| 186 | breakpad_dir=breakpad_dir, |
| 187 | num_errors=num_errors, |
| 188 | ) as queue: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 189 | for root, _, files in os.walk(symbols_dir): |
| 190 | for f in files: |
| 191 | queue.put([os.path.join(root, f)]) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 192 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | return num_errors.value |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 194 | |
| 195 | |
| 196 | def ProcessSymbolsZip(zip_archive, breakpad_dir): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 197 | """Extract, process, and upload all symbols in a symbols zip file. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 198 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 199 | Take the symbols file build artifact from an Android build, process it into |
| 200 | breakpad format, and upload the results to the ChromeOS crashreporter. |
| 201 | Significant multiprocessing is done by helper libraries, and a remote swarm |
| 202 | server is used to reduce processing of duplicate symbol files. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 203 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | The symbols files are really expected to be unstripped elf files (or |
| 205 | libraries), possibly using packed relocation tables. No other file types are |
| 206 | expected in the zip. |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 207 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 208 | Args: |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 209 | zip_archive: Name of the zip file to process. |
| 210 | breakpad_dir: Root directory for writing out breakpad files. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 211 | """ |
| 212 | with osutils.TempDir(prefix="extracted-") as extract_dir: |
| 213 | logging.info("Extracting %s into %s", zip_archive, extract_dir) |
| 214 | with zipfile.ZipFile(zip_archive, "r") as zf: |
| 215 | # We are trusting the contents from a security point of view. |
| 216 | zf.extractall(extract_dir) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 217 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | logging.info( |
| 219 | "Generate breakpad symbols from %s into %s", |
| 220 | extract_dir, |
| 221 | breakpad_dir, |
| 222 | ) |
| 223 | GenerateBreakpadSymbols(breakpad_dir, extract_dir) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 224 | |
| 225 | |
| 226 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | """Helper method mostly used for manual testing.""" |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 228 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 229 | parser = commandline.ArgumentParser(description=__doc__) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 230 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 231 | parser.add_argument( |
| 232 | "--symbols_file", type="path", required=True, help="Zip file containing" |
| 233 | ) |
| 234 | parser.add_argument( |
| 235 | "--breakpad_dir", |
| 236 | type="path", |
| 237 | default="/tmp/breakpad", |
| 238 | help="Root directory for breakpad symbol files.", |
| 239 | ) |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 240 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 241 | opts = parser.parse_args(argv) |
| 242 | opts.Freeze() |
Shuhei Takahashi | 721d5a7 | 2017-02-10 15:38:28 +0900 | [diff] [blame] | 243 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 244 | ProcessSymbolsZip(opts.symbols_file, opts.breakpad_dir) |