Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 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 |
| 21 | import logging |
| 22 | import multiprocessing |
| 23 | import os |
| 24 | import tempfile |
| 25 | |
| 26 | from chromite.lib import commandline |
| 27 | from chromite.lib import cros_build_lib |
| 28 | from chromite.lib import osutils |
| 29 | from chromite.lib import parallel |
| 30 | |
| 31 | |
| 32 | SymbolHeader = collections.namedtuple('SymbolHeader', |
| 33 | ('cpu', 'id', 'name', 'os',)) |
| 34 | |
| 35 | |
| 36 | def ReadSymsHeader(sym_file): |
| 37 | """Parse the header of the symbol file |
| 38 | |
| 39 | The first line of the syms file will read like: |
| 40 | MODULE Linux arm F4F6FA6CCBDEF455039C8DE869C8A2F40 blkid |
| 41 | |
| 42 | https://code.google.com/p/google-breakpad/wiki/SymbolFiles |
| 43 | |
| 44 | Args: |
| 45 | sym_file: The symbol file to parse |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 46 | |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 47 | Returns: |
| 48 | A SymbolHeader object |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 49 | |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 50 | Raises: |
| 51 | ValueError if the first line of |sym_file| is invalid |
| 52 | """ |
| 53 | read_it = lambda x: x.readline().split() |
| 54 | if isinstance(sym_file, basestring): |
| 55 | with open(sym_file, 'r') as f: |
| 56 | header = read_it(f) |
| 57 | else: |
| 58 | header = read_it(sym_file) |
| 59 | |
| 60 | if header[0] != 'MODULE' or len(header) != 5: |
| 61 | raise ValueError('header of sym file is invalid') |
| 62 | return SymbolHeader(os=header[1], cpu=header[2], id=header[3], name=header[4]) |
| 63 | |
| 64 | |
| 65 | def GenerateBreakpadSymbol(elf_file, debug_file=None, breakpad_dir=None, |
| 66 | board=None, strip_cfi=False, num_errors=None): |
| 67 | """Generate the symbols for |elf_file| using |debug_file| |
| 68 | |
| 69 | Args: |
| 70 | elf_file: The file to dump symbols for |
| 71 | debug_file: Split debug file to use for symbol information |
| 72 | breakpad_dir: The dir to store the output symbol file in |
| 73 | board: If |breakpad_dir| is not specified, use |board| to find it |
| 74 | strip_cfi: Do not generate CFI data |
| 75 | num_errors: An object to update with the error count (needs a .value member) |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 76 | |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 77 | Returns: |
| 78 | The number of errors that were encountered. |
| 79 | """ |
| 80 | if breakpad_dir is None: |
| 81 | breakpad_dir = FindBreakpadDir(board) |
| 82 | if num_errors is None: |
| 83 | num_errors = ctypes.c_int() |
| 84 | |
| 85 | cmd_base = ['dump_syms'] |
| 86 | if strip_cfi: |
| 87 | cmd_base += ['-c'] |
| 88 | # Some files will not be readable by non-root (e.g. set*id /bin/su). |
| 89 | needs_sudo = not os.access(elf_file, os.R_OK) |
| 90 | |
| 91 | def _DumpIt(cmd_args): |
| 92 | if needs_sudo: |
| 93 | run_command = cros_build_lib.SudoRunCommand |
| 94 | else: |
| 95 | run_command = cros_build_lib.RunCommand |
| 96 | return run_command( |
| 97 | cmd_base + cmd_args, redirect_stderr=True, log_stdout_to_file=temp.name, |
| 98 | error_code_ok=True, debug_level=logging.DEBUG) |
| 99 | |
| 100 | def _CrashCheck(ret, msg): |
| 101 | if ret < 0: |
| 102 | cros_build_lib.PrintBuildbotStepWarnings() |
| 103 | cros_build_lib.Warning('dump_syms crashed with %s; %s', |
| 104 | osutils.StrSignal(-ret), msg) |
| 105 | |
| 106 | osutils.SafeMakedirs(breakpad_dir) |
| 107 | with tempfile.NamedTemporaryFile(dir=breakpad_dir, bufsize=0) as temp: |
| 108 | if debug_file: |
| 109 | # Try to dump the symbols using the debug file like normal. |
| 110 | cmd_args = [elf_file, os.path.dirname(debug_file)] |
| 111 | result = _DumpIt(cmd_args) |
| 112 | |
| 113 | if result.returncode: |
| 114 | # Sometimes dump_syms can crash because there's too much info. |
| 115 | # Try dumping and stripping the extended stuff out. At least |
| 116 | # this way we'll get the extended symbols. http://crbug.com/266064 |
| 117 | _CrashCheck(result.returncode, 'retrying w/out CFI') |
| 118 | cmd_args = ['-c', '-r'] + cmd_args |
| 119 | result = _DumpIt(cmd_args) |
| 120 | _CrashCheck(result.returncode, 'retrying w/out debug') |
| 121 | |
| 122 | basic_dump = result.returncode |
| 123 | else: |
| 124 | basic_dump = True |
| 125 | |
| 126 | if basic_dump: |
| 127 | # If that didn't work (no debug, or dump_syms still failed), try |
| 128 | # dumping just the file itself directly. |
| 129 | result = _DumpIt([elf_file]) |
| 130 | if result.returncode: |
| 131 | # A lot of files (like kernel files) contain no debug information, |
| 132 | # do not consider such occurrences as errors. |
| 133 | cros_build_lib.PrintBuildbotStepWarnings() |
| 134 | _CrashCheck(result.returncode, 'giving up entirely') |
| 135 | if 'file contains no debugging information' in result.error: |
| 136 | cros_build_lib.Warning('no symbols found for %s', elf_file) |
| 137 | else: |
| 138 | num_errors.value += 1 |
| 139 | cros_build_lib.Error('dumping symbols for %s failed:\n%s', |
| 140 | elf_file, result.error) |
| 141 | return num_errors.value |
| 142 | |
| 143 | # Move the dumped symbol file to the right place: |
| 144 | # /build/$BOARD/usr/lib/debug/breakpad/<module-name>/<id>/<module-name>.sym |
| 145 | header = ReadSymsHeader(temp) |
| 146 | cros_build_lib.Info('Dumped %s as %s : %s', elf_file, header.name, |
| 147 | header.id) |
| 148 | sym_file = os.path.join(breakpad_dir, header.name, header.id, |
| 149 | header.name + '.sym') |
| 150 | osutils.SafeMakedirs(os.path.dirname(sym_file)) |
| 151 | os.rename(temp.name, sym_file) |
Mike Frysinger | 60ec101 | 2013-10-21 00:11:10 -0400 | [diff] [blame] | 152 | os.chmod(sym_file, 0o644) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 153 | temp.delete = False |
| 154 | |
| 155 | return num_errors.value |
| 156 | |
| 157 | |
| 158 | def GenerateBreakpadSymbols(board, breakpad_dir=None, strip_cfi=False, |
Mike Frysinger | ef9ab2f | 2013-08-26 22:16:00 -0400 | [diff] [blame] | 159 | generate_count=None, sysroot=None, |
Shawn Nematbakhsh | 2c169cb | 2013-10-29 16:23:58 -0700 | [diff] [blame] | 160 | num_processes=None, clean_breakpad=False, |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 161 | exclude_dirs=(), file_list=None): |
| 162 | """Generate symbols for this board. |
| 163 | |
| 164 | If |file_list| is None, symbols are generated for all executables, otherwise |
| 165 | only for the files included in |file_list|. |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 166 | |
| 167 | TODO(build): |
| 168 | This should be merged with buildbot_commands.GenerateBreakpadSymbols() |
| 169 | once we rewrite cros_generate_breakpad_symbols in python. |
| 170 | |
| 171 | Args: |
| 172 | board: The board whose symbols we wish to generate |
| 173 | breakpad_dir: The full path to the breakpad directory where symbols live |
| 174 | strip_cfi: Do not generate CFI data |
| 175 | generate_count: If set, only generate this many symbols (meant for testing) |
| 176 | sysroot: The root where to find the corresponding ELFs |
Mike Frysinger | ef9ab2f | 2013-08-26 22:16:00 -0400 | [diff] [blame] | 177 | num_processes: Number of jobs to run in parallel |
Mike Frysinger | 9a628bb | 2013-10-24 15:51:37 -0400 | [diff] [blame] | 178 | clean_breakpad: Should we `rm -rf` the breakpad output dir first; note: we |
| 179 | do not do any locking, so do not run more than one in parallel when True |
Shawn Nematbakhsh | 2c169cb | 2013-10-29 16:23:58 -0700 | [diff] [blame] | 180 | exclude_dirs: List of dirs (relative to |sysroot|) to not search |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 181 | file_list: Only generate symbols for files in this list. Each file must be a |
| 182 | full path (including |sysroot| prefix). |
| 183 | TODO(build): Support paths w/o |sysroot|. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 184 | |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 185 | Returns: |
| 186 | The number of errors that were encountered. |
| 187 | """ |
| 188 | if breakpad_dir is None: |
| 189 | breakpad_dir = FindBreakpadDir(board) |
| 190 | if sysroot is None: |
Yu-Ju Hong | dd9bb2b | 2014-01-03 17:08:26 -0800 | [diff] [blame] | 191 | sysroot = cros_build_lib.GetSysroot(board=board) |
Mike Frysinger | 9a628bb | 2013-10-24 15:51:37 -0400 | [diff] [blame] | 192 | if clean_breakpad: |
| 193 | cros_build_lib.Info('cleaning out %s first', breakpad_dir) |
| 194 | osutils.RmDir(breakpad_dir, ignore_missing=True, sudo=True) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 195 | # Make sure non-root can write out symbols as needed. |
| 196 | osutils.SafeMakedirs(breakpad_dir, sudo=True) |
| 197 | if not os.access(breakpad_dir, os.W_OK): |
| 198 | cros_build_lib.SudoRunCommand(['chown', '-R', str(os.getuid()), |
| 199 | breakpad_dir]) |
| 200 | debug_dir = FindDebugDir(board) |
Shawn Nematbakhsh | 2c169cb | 2013-10-29 16:23:58 -0700 | [diff] [blame] | 201 | exclude_paths = [os.path.join(debug_dir, x) for x in exclude_dirs] |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 202 | if file_list is None: |
| 203 | file_list = [] |
| 204 | file_filter = dict.fromkeys([os.path.normpath(x) for x in file_list], False) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 205 | |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 206 | cros_build_lib.Info('generating breakpad symbols using %s', debug_dir) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 207 | |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 208 | # Let's locate all the debug_files and elfs first along with the debug file |
| 209 | # sizes. This way we can start processing the largest files first in parallel |
| 210 | # with the small ones. |
| 211 | # If |file_list| was given, ignore all other files. |
| 212 | targets = [] |
Shawn Nematbakhsh | 2c169cb | 2013-10-29 16:23:58 -0700 | [diff] [blame] | 213 | for root, dirs, files in os.walk(debug_dir): |
| 214 | if root in exclude_paths: |
| 215 | cros_build_lib.Info('Skipping excluded dir %s', root) |
| 216 | del dirs[:] |
| 217 | continue |
| 218 | |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 219 | for debug_file in files: |
| 220 | debug_file = os.path.join(root, debug_file) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 221 | # Turn /build/$BOARD/usr/lib/debug/sbin/foo.debug into |
| 222 | # /build/$BOARD/sbin/foo. |
| 223 | elf_file = os.path.join(sysroot, debug_file[len(debug_dir) + 1:-6]) |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 224 | |
| 225 | if file_filter: |
| 226 | if elf_file in file_filter: |
| 227 | file_filter[elf_file] = True |
| 228 | elif debug_file in file_filter: |
| 229 | file_filter[debug_file] = True |
| 230 | else: |
| 231 | continue |
| 232 | |
| 233 | # Filter out files based on common issues with the debug file. |
| 234 | if not debug_file.endswith('.debug'): |
| 235 | continue |
| 236 | |
| 237 | elif debug_file.endswith('.ko.debug'): |
| 238 | cros_build_lib.Debug('Skipping kernel module %s', debug_file) |
| 239 | continue |
| 240 | |
| 241 | elif os.path.islink(debug_file): |
| 242 | # The build-id stuff is common enough to filter out by default. |
| 243 | if '/.build-id/' in debug_file: |
| 244 | msg = cros_build_lib.Debug |
| 245 | else: |
| 246 | msg = cros_build_lib.Warning |
| 247 | msg('Skipping symbolic link %s', debug_file) |
| 248 | continue |
| 249 | |
| 250 | # Filter out files based on common issues with the elf file. |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 251 | if not os.path.exists(elf_file): |
| 252 | # Sometimes we filter out programs from /usr/bin but leave behind |
| 253 | # the .debug file. |
| 254 | cros_build_lib.Warning('Skipping missing %s', elf_file) |
| 255 | continue |
| 256 | |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 257 | targets.append((os.path.getsize(debug_file), elf_file, debug_file)) |
| 258 | |
| 259 | bg_errors = multiprocessing.Value('i') |
| 260 | if file_filter: |
| 261 | files_not_found = [x for x, found in file_filter.iteritems() if not found] |
| 262 | bg_errors.value += len(files_not_found) |
| 263 | if files_not_found: |
| 264 | cros_build_lib.Error('Failed to find requested files: %s', |
| 265 | files_not_found) |
| 266 | |
| 267 | # Now start generating symbols for the discovered elfs. |
| 268 | with parallel.BackgroundTaskRunner(GenerateBreakpadSymbol, |
| 269 | breakpad_dir=breakpad_dir, board=board, |
| 270 | strip_cfi=strip_cfi, |
| 271 | num_errors=bg_errors, |
| 272 | processes=num_processes) as queue: |
| 273 | for _, elf_file, debug_file in sorted(targets, reverse=True): |
| 274 | if generate_count == 0: |
| 275 | break |
| 276 | |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 277 | queue.put([elf_file, debug_file]) |
| 278 | if generate_count is not None: |
| 279 | generate_count -= 1 |
| 280 | if generate_count == 0: |
| 281 | break |
| 282 | |
| 283 | return bg_errors.value |
| 284 | |
| 285 | |
| 286 | def FindDebugDir(board): |
| 287 | """Given a |board|, return the path to the split debug dir for it""" |
Yu-Ju Hong | dd9bb2b | 2014-01-03 17:08:26 -0800 | [diff] [blame] | 288 | sysroot = cros_build_lib.GetSysroot(board=board) |
| 289 | return os.path.join(sysroot, 'usr', 'lib', 'debug') |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 290 | |
| 291 | |
| 292 | def FindBreakpadDir(board): |
| 293 | """Given a |board|, return the path to the breakpad dir for it""" |
| 294 | return os.path.join(FindDebugDir(board), 'breakpad') |
| 295 | |
| 296 | |
| 297 | def main(argv): |
| 298 | parser = commandline.ArgumentParser(description=__doc__) |
| 299 | |
| 300 | parser.add_argument('--board', default=None, |
| 301 | help='board to generate symbols for') |
| 302 | parser.add_argument('--breakpad_root', type='path', default=None, |
| 303 | help='root directory for breakpad symbols') |
Shawn Nematbakhsh | 2c169cb | 2013-10-29 16:23:58 -0700 | [diff] [blame] | 304 | parser.add_argument('--exclude-dir', type=str, action='append', |
| 305 | default=[], |
| 306 | help='directory (relative to |board| root) to not search') |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 307 | parser.add_argument('--generate-count', type=int, default=None, |
| 308 | help='only generate # number of symbols') |
Mike Frysinger | 9a628bb | 2013-10-24 15:51:37 -0400 | [diff] [blame] | 309 | parser.add_argument('--noclean', dest='clean', action='store_false', |
| 310 | default=True, |
| 311 | help='do not clean out breakpad dir before running') |
Mike Frysinger | ef9ab2f | 2013-08-26 22:16:00 -0400 | [diff] [blame] | 312 | parser.add_argument('--jobs', type=int, default=None, |
| 313 | help='limit number of parallel jobs') |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 314 | parser.add_argument('--strip_cfi', action='store_true', default=False, |
| 315 | help='do not generate CFI data (pass -c to dump_syms)') |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 316 | parser.add_argument('file_list', nargs='*', default=None, |
| 317 | help='generate symbols for only these files ' |
| 318 | '(e.g. /build/$BOARD/usr/bin/foo)') |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 319 | |
| 320 | opts = parser.parse_args(argv) |
Mike Frysinger | 90e49ca | 2014-01-14 14:42:07 -0500 | [diff] [blame] | 321 | opts.Freeze() |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 322 | |
| 323 | if opts.board is None: |
| 324 | cros_build_lib.Die('--board is required') |
| 325 | |
| 326 | ret = GenerateBreakpadSymbols(opts.board, breakpad_dir=opts.breakpad_root, |
| 327 | strip_cfi=opts.strip_cfi, |
Mike Frysinger | ef9ab2f | 2013-08-26 22:16:00 -0400 | [diff] [blame] | 328 | generate_count=opts.generate_count, |
Mike Frysinger | 9a628bb | 2013-10-24 15:51:37 -0400 | [diff] [blame] | 329 | num_processes=opts.jobs, |
Shawn Nematbakhsh | 2c169cb | 2013-10-29 16:23:58 -0700 | [diff] [blame] | 330 | clean_breakpad=opts.clean, |
Prathmesh Prabhu | 9995e9b | 2013-10-31 16:43:55 -0700 | [diff] [blame] | 331 | exclude_dirs=opts.exclude_dir, |
| 332 | file_list=opts.file_list) |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 333 | if ret: |
| 334 | cros_build_lib.Error('encountered %i problem(s)', ret) |
| 335 | # Since exit(status) gets masked, clamp it to 1 so we don't inadvertently |
| 336 | # return 0 in case we are a multiple of the mask. |
| 337 | ret = 1 |
| 338 | |
| 339 | return ret |