Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -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 | """Upload all debug symbols required for crash reporting purposes. |
| 6 | |
| 7 | This script need only be used to upload release builds symbols or to debug |
| 8 | crashes on non-release builds (in which case try to only upload the symbols |
| 9 | for those executables involved).""" |
| 10 | |
| 11 | import ctypes |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 12 | import multiprocessing |
| 13 | import os |
Mike Frysinger | 094a217 | 2013-08-14 12:54:35 -0400 | [diff] [blame] | 14 | import poster |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 15 | import random |
| 16 | import textwrap |
| 17 | import tempfile |
| 18 | import time |
Mike Frysinger | 094a217 | 2013-08-14 12:54:35 -0400 | [diff] [blame] | 19 | import urllib2 |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 20 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 21 | from chromite.lib import commandline |
| 22 | from chromite.lib import cros_build_lib |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 23 | from chromite.lib import parallel |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 24 | from chromite.scripts import cros_generate_breakpad_symbols |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 25 | |
| 26 | |
| 27 | # URLs used for uploading symbols. |
| 28 | OFFICIAL_UPLOAD_URL = 'http://clients2.google.com/cr/symbol' |
| 29 | STAGING_UPLOAD_URL = 'http://clients2.google.com/cr/staging_symbol' |
| 30 | |
| 31 | |
| 32 | # The crash server rejects files that are this big. |
| 33 | CRASH_SERVER_FILE_LIMIT = 350 * 1024 * 1024 |
| 34 | # Give ourselves a little breathing room from what the server expects. |
| 35 | DEFAULT_FILE_LIMIT = CRASH_SERVER_FILE_LIMIT - (10 * 1024 * 1024) |
| 36 | |
| 37 | |
Mike Frysinger | cd78a08 | 2013-06-26 17:13:04 -0400 | [diff] [blame] | 38 | # How long to wait (in seconds) for a single upload to complete. This has |
| 39 | # to allow for symbols that are up to CRASH_SERVER_FILE_LIMIT in size. |
| 40 | UPLOAD_TIMEOUT = 30 * 60 |
| 41 | |
| 42 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 43 | # Sleep for 200ms in between uploads to avoid DoS'ing symbol server. |
| 44 | DEFAULT_SLEEP_DELAY = 0.2 |
| 45 | |
| 46 | |
| 47 | # Number of seconds to wait before retrying an upload. The delay will double |
| 48 | # for each subsequent retry of the same symbol file. |
| 49 | INITIAL_RETRY_DELAY = 1 |
| 50 | |
| 51 | # Allow up to 7 attempts to upload a symbol file (total delay may be |
| 52 | # 1+2+4+8+16+32=63 seconds). |
| 53 | MAX_RETRIES = 6 |
| 54 | |
| 55 | # Number of total errors, TOTAL_ERROR_COUNT, before retries are no longer |
| 56 | # attempted. This is used to avoid lots of errors causing unreasonable delays. |
| 57 | MAX_TOTAL_ERRORS_FOR_RETRY = 3 |
| 58 | |
| 59 | |
| 60 | def SymUpload(sym_file, upload_url): |
Mike Frysinger | 094a217 | 2013-08-14 12:54:35 -0400 | [diff] [blame] | 61 | """Upload a symbol file to a HTTP server |
| 62 | |
| 63 | The upload is a multipart/form-data POST with the following parameters: |
| 64 | code_file: the basename of the module, e.g. "app" |
| 65 | code_identifier: the module file's identifier |
| 66 | debug_file: the basename of the debugging file, e.g. "app" |
| 67 | debug_identifier: the debug file's identifier, usually consisting of |
| 68 | the guid and age embedded in the pdb, e.g. |
| 69 | "11111111BBBB3333DDDD555555555555F" |
| 70 | version: the file version of the module, e.g. "1.2.3.4" |
| 71 | product: HTTP-friendly product name |
| 72 | os: the operating system that the module was built for |
| 73 | cpu: the CPU that the module was built for |
| 74 | symbol_file: the contents of the breakpad-format symbol file |
| 75 | |
| 76 | Args: |
| 77 | sym_file: The symbol file to upload |
| 78 | upload_url: The crash URL to POST the |sym_file| to |
| 79 | """ |
| 80 | sym_header = cros_generate_breakpad_symbols.ReadSymsHeader(sym_file) |
| 81 | |
| 82 | fields = ( |
| 83 | ('code_file', sym_header.name), |
| 84 | ('debug_file', sym_header.name), |
| 85 | ('debug_identifier', sym_header.id.replace('-', '')), |
| 86 | # Should we set these fields? They aren't critical, but it might be nice? |
| 87 | # We'd have to figure out what file this symbol is coming from and what |
| 88 | # package provides it ... |
| 89 | #('version', None), |
| 90 | #('product', 'ChromeOS'), |
| 91 | ('os', sym_header.os), |
| 92 | ('cpu', sym_header.cpu), |
| 93 | poster.encode.MultipartParam.from_file('symbol_file', sym_file), |
| 94 | ) |
| 95 | |
| 96 | data, headers = poster.encode.multipart_encode(fields) |
| 97 | request = urllib2.Request(upload_url, data, headers) |
| 98 | request.add_header('User-agent', 'chromite.upload_symbols') |
| 99 | urllib2.urlopen(request, timeout=UPLOAD_TIMEOUT) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 100 | |
| 101 | |
| 102 | def TestingSymUpload(sym_file, upload_url): |
| 103 | """A stub version of SymUpload for --testing usage""" |
| 104 | cmd = ['sym_upload', sym_file, upload_url] |
| 105 | # Randomly fail 80% of the time (the retry logic makes this 80%/3 per file). |
| 106 | returncode = random.randint(1, 100) <= 80 |
| 107 | cros_build_lib.Debug('would run (and return %i): %s', returncode, |
| 108 | ' '.join(map(repr, cmd))) |
| 109 | if returncode: |
| 110 | output = 'Failed to send the symbol file.' |
| 111 | else: |
| 112 | output = 'Successfully sent the symbol file.' |
| 113 | result = cros_build_lib.CommandResult(cmd=cmd, error=None, output=output, |
| 114 | returncode=returncode) |
| 115 | if returncode: |
Mike Frysinger | 094a217 | 2013-08-14 12:54:35 -0400 | [diff] [blame] | 116 | raise urllib2.HTTPError(upload_url, 400, 'forced test fail', {}, None) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 117 | else: |
| 118 | return result |
| 119 | |
| 120 | |
| 121 | def UploadSymbol(sym_file, upload_url, file_limit=DEFAULT_FILE_LIMIT, |
| 122 | sleep=0, num_errors=None): |
| 123 | """Upload |sym_file| to |upload_url| |
| 124 | |
| 125 | Args: |
| 126 | sym_file: The full path to the breakpad symbol to upload |
| 127 | upload_url: The crash server to upload things to |
| 128 | file_limit: The max file size of a symbol file before we try to strip it |
| 129 | sleep: Number of seconds to sleep before running |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 130 | num_errors: An object to update with the error count (needs a .value member) |
| 131 | Returns: |
| 132 | The number of errors that were encountered. |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 133 | """ |
| 134 | if num_errors is None: |
| 135 | num_errors = ctypes.c_int() |
| 136 | elif num_errors.value > MAX_TOTAL_ERRORS_FOR_RETRY: |
| 137 | # Abandon ship! It's on fire! NOoooooooooooOOOoooooo. |
| 138 | return 0 |
| 139 | |
| 140 | upload_file = sym_file |
| 141 | |
| 142 | if sleep: |
| 143 | # Keeps us from DoS-ing the symbol server. |
| 144 | time.sleep(sleep) |
| 145 | |
| 146 | cros_build_lib.Debug('uploading %s' % sym_file) |
| 147 | |
| 148 | # Ideally there'd be a tempfile.SpooledNamedTemporaryFile that we could use. |
| 149 | with tempfile.NamedTemporaryFile(prefix='upload_symbols', |
| 150 | bufsize=0) as temp_sym_file: |
| 151 | if file_limit: |
| 152 | # If the symbols size is too big, strip out the call frame info. The CFI |
| 153 | # is unnecessary for 32bit x86 targets where the frame pointer is used (as |
| 154 | # all of ours have) and it accounts for over half the size of the symbols |
| 155 | # uploaded. |
| 156 | file_size = os.path.getsize(sym_file) |
| 157 | if file_size > file_limit: |
| 158 | cros_build_lib.Warning('stripping CFI from %s due to size %s > %s', |
| 159 | sym_file, file_size, file_limit) |
| 160 | temp_sym_file.writelines([x for x in open(sym_file, 'rb').readlines() |
| 161 | if not x.startswith('STACK CFI')]) |
| 162 | upload_file = temp_sym_file.name |
| 163 | |
| 164 | # Hopefully the crash server will let it through. But it probably won't. |
| 165 | # Not sure what the best answer is in this case. |
| 166 | file_size = os.path.getsize(upload_file) |
| 167 | if file_size > CRASH_SERVER_FILE_LIMIT: |
| 168 | cros_build_lib.PrintBuildbotStepWarnings() |
| 169 | cros_build_lib.Error('upload file %s is awfully large, risking rejection ' |
| 170 | 'by symbol server (%s > %s)', sym_file, file_size, |
| 171 | CRASH_SERVER_FILE_LIMIT) |
| 172 | num_errors.value += 1 |
| 173 | |
| 174 | # Upload the symbol file. |
| 175 | try: |
Mike Frysinger | 094a217 | 2013-08-14 12:54:35 -0400 | [diff] [blame] | 176 | cros_build_lib.RetryException(urllib2.HTTPError, |
| 177 | MAX_RETRIES, SymUpload, upload_file, |
| 178 | upload_url, sleep=INITIAL_RETRY_DELAY) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 179 | cros_build_lib.Info('successfully uploaded %10i bytes: %s', file_size, |
| 180 | os.path.basename(sym_file)) |
Mike Frysinger | 094a217 | 2013-08-14 12:54:35 -0400 | [diff] [blame] | 181 | except urllib2.HTTPError as e: |
| 182 | cros_build_lib.Warning('could not upload: %s: HTTP %s: %s', |
| 183 | os.path.basename(sym_file), e.code, e.reason) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 184 | num_errors.value += 1 |
| 185 | |
| 186 | return num_errors.value |
| 187 | |
| 188 | |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 189 | def UploadSymbols(board=None, official=False, breakpad_dir=None, |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 190 | file_limit=DEFAULT_FILE_LIMIT, sleep=DEFAULT_SLEEP_DELAY, |
Mike Frysinger | 118d250 | 2013-08-19 03:36:56 -0400 | [diff] [blame^] | 191 | upload_count=None, sym_files=None, root=None): |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 192 | """Upload all the generated symbols for |board| to the crash server |
| 193 | |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 194 | You can use in a few ways: |
| 195 | * pass |board| to locate all of its symbols |
| 196 | * pass |breakpad_dir| to upload all the symbols in there |
| 197 | * pass |sym_files| to upload specific symbols |
| 198 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 199 | Args: |
| 200 | board: The board whose symbols we wish to upload |
| 201 | official: Use the official symbol server rather than the staging one |
| 202 | breakpad_dir: The full path to the breakpad directory where symbols live |
| 203 | file_limit: The max file size of a symbol file before we try to strip it |
| 204 | sleep: How long to sleep in between uploads |
| 205 | upload_count: If set, only upload this many symbols (meant for testing) |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 206 | sym_files: Specific symbol files to upload, otherwise search |breakpad_dir| |
Mike Frysinger | 118d250 | 2013-08-19 03:36:56 -0400 | [diff] [blame^] | 207 | root: The tree to prefix to |breakpad_dir| (if |breakpad_dir| is not set) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 208 | Returns: |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 209 | The number of errors that were encountered. |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 210 | """ |
| 211 | num_errors = 0 |
| 212 | |
| 213 | if official: |
| 214 | upload_url = OFFICIAL_UPLOAD_URL |
| 215 | else: |
| 216 | cros_build_lib.Warning('unofficial builds upload to the staging server') |
| 217 | upload_url = STAGING_UPLOAD_URL |
| 218 | |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 219 | if sym_files: |
| 220 | cros_build_lib.Info('uploading specified symbol files to %s', upload_url) |
| 221 | sym_file_sets = [('', '', sym_files)] |
| 222 | all_files = True |
| 223 | else: |
| 224 | if breakpad_dir is None: |
Mike Frysinger | 118d250 | 2013-08-19 03:36:56 -0400 | [diff] [blame^] | 225 | breakpad_dir = os.path.join( |
| 226 | root, |
| 227 | cros_generate_breakpad_symbols.FindBreakpadDir(board).lstrip('/')) |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 228 | cros_build_lib.Info('uploading all symbols to %s from %s', upload_url, |
| 229 | breakpad_dir) |
| 230 | sym_file_sets = os.walk(breakpad_dir) |
| 231 | all_files = False |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 232 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 233 | # We need to limit ourselves to one upload at a time to avoid the server |
| 234 | # kicking in DoS protection. See these bugs for more details: |
| 235 | # http://crbug.com/209442 |
| 236 | # http://crbug.com/212496 |
| 237 | bg_errors = multiprocessing.Value('i') |
| 238 | with parallel.BackgroundTaskRunner(UploadSymbol, file_limit=file_limit, |
| 239 | sleep=sleep, num_errors=bg_errors, |
| 240 | processes=1) as queue: |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 241 | for root, _, files in sym_file_sets: |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 242 | if upload_count == 0: |
| 243 | break |
| 244 | |
| 245 | for sym_file in files: |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 246 | if all_files or sym_file.endswith('.sym'): |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 247 | sym_file = os.path.join(root, sym_file) |
| 248 | queue.put([sym_file, upload_url]) |
| 249 | |
| 250 | if upload_count is not None: |
| 251 | upload_count -= 1 |
| 252 | if upload_count == 0: |
| 253 | break |
| 254 | num_errors += bg_errors.value |
| 255 | |
| 256 | return num_errors |
| 257 | |
| 258 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 259 | def main(argv): |
| 260 | parser = commandline.ArgumentParser(description=__doc__) |
| 261 | |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 262 | parser.add_argument('sym_files', type='path', nargs='*', default=None) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 263 | parser.add_argument('--board', default=None, |
| 264 | help='board to build packages for') |
| 265 | parser.add_argument('--breakpad_root', type='path', default=None, |
| 266 | help='root directory for breakpad symbols') |
| 267 | parser.add_argument('--official_build', action='store_true', default=False, |
| 268 | help='point to official symbol server') |
| 269 | parser.add_argument('--regenerate', action='store_true', default=False, |
| 270 | help='regenerate all symbols') |
| 271 | parser.add_argument('--upload-count', type=int, default=None, |
| 272 | help='only upload # number of symbols') |
| 273 | parser.add_argument('--strip_cfi', type=int, |
| 274 | default=CRASH_SERVER_FILE_LIMIT - (10 * 1024 * 1024), |
| 275 | help='strip CFI data for files above this size') |
| 276 | parser.add_argument('--testing', action='store_true', default=False, |
| 277 | help='run in testing mode') |
| 278 | parser.add_argument('--yes', action='store_true', default=False, |
| 279 | help='answer yes to all prompts') |
| 280 | |
| 281 | opts = parser.parse_args(argv) |
| 282 | |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 283 | if opts.sym_files: |
| 284 | if opts.regenerate: |
| 285 | cros_build_lib.Die('--regenerate may not be used with specific files') |
| 286 | else: |
| 287 | if opts.board is None: |
| 288 | cros_build_lib.Die('--board is required') |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 289 | |
| 290 | if opts.breakpad_root and opts.regenerate: |
| 291 | cros_build_lib.Die('--regenerate may not be used with --breakpad_root') |
| 292 | |
| 293 | if opts.testing: |
| 294 | # TODO(build): Kill off --testing mode once unittests are up-to-snuff. |
| 295 | cros_build_lib.Info('running in testing mode') |
| 296 | # pylint: disable=W0601,W0603 |
| 297 | global INITIAL_RETRY_DELAY, SymUpload, DEFAULT_SLEEP_DELAY |
| 298 | INITIAL_RETRY_DELAY = DEFAULT_SLEEP_DELAY = 0 |
| 299 | SymUpload = TestingSymUpload |
| 300 | |
| 301 | if not opts.yes: |
| 302 | query = textwrap.wrap(textwrap.dedent(""" |
| 303 | Uploading symbols for an entire Chromium OS build is really only |
| 304 | necessary for release builds and in a few cases for developers |
| 305 | to debug problems. It will take considerable time to run. For |
| 306 | developer debugging purposes, consider instead passing specific |
| 307 | files to upload. |
| 308 | """), 80) |
| 309 | cros_build_lib.Warning('\n%s', '\n'.join(query)) |
| 310 | if not cros_build_lib.BooleanPrompt( |
| 311 | prompt='Are you sure you want to upload all build symbols', |
| 312 | default=False): |
| 313 | cros_build_lib.Die('better safe than sorry') |
| 314 | |
| 315 | ret = 0 |
| 316 | if opts.regenerate: |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 317 | ret += cros_generate_breakpad_symbols.GenerateBreakpadSymbols( |
| 318 | opts.board, breakpad_dir=opts.breakpad_root) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 319 | |
| 320 | ret += UploadSymbols(opts.board, official=opts.official_build, |
| 321 | breakpad_dir=opts.breakpad_root, |
| 322 | file_limit=opts.strip_cfi, sleep=DEFAULT_SLEEP_DELAY, |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 323 | upload_count=opts.upload_count, sym_files=opts.sym_files) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 324 | if ret: |
| 325 | cros_build_lib.Error('encountered %i problem(s)', ret) |
| 326 | # Since exit(status) gets masked, clamp it to 1 so we don't inadvertently |
| 327 | # return 0 in case we are a multiple of the mask. |
| 328 | ret = 1 |
| 329 | |
| 330 | return ret |
Mike Frysinger | 094a217 | 2013-08-14 12:54:35 -0400 | [diff] [blame] | 331 | |
| 332 | |
| 333 | # We need this to run once per process. Do it at module import time as that |
| 334 | # will let us avoid doing it inline at function call time (see SymUpload) as |
| 335 | # that func might be called by the multiprocessing module which means we'll |
| 336 | # do the opener logic multiple times overall. Plus, if you're importing this |
| 337 | # module, it's a pretty good chance that you're going to need this. |
| 338 | poster.streaminghttp.register_openers() |