Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 2 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Upload all debug symbols required for crash reporting purposes. |
| 7 | |
| 8 | This script need only be used to upload release builds symbols or to debug |
| 9 | crashes on non-release builds (in which case try to only upload the symbols |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 10 | for those executables involved). |
| 11 | """ |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 12 | |
Mike Frysinger | a4fa1e8 | 2014-01-15 01:45:56 -0500 | [diff] [blame] | 13 | from __future__ import print_function |
| 14 | |
Mike Frysinger | a4fa1e8 | 2014-01-15 01:45:56 -0500 | [diff] [blame] | 15 | import httplib |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 16 | import itertools |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 17 | import json |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 18 | import os |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 19 | import requests |
Mike Frysinger | fd35565 | 2014-01-23 02:57:48 -0500 | [diff] [blame] | 20 | import socket |
Mike Frysinger | c5597f2 | 2014-11-27 15:39:15 -0500 | [diff] [blame] | 21 | import sys |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 22 | import textwrap |
| 23 | import tempfile |
| 24 | import time |
Mike Frysinger | 094a217 | 2013-08-14 12:54:35 -0400 | [diff] [blame] | 25 | import urllib2 |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 26 | import urlparse |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 27 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 28 | from chromite.lib import constants |
Mike Frysinger | bbd1f11 | 2016-09-08 18:25:11 -0400 | [diff] [blame] | 29 | |
Mike Frysinger | 3fff4ff | 2018-07-24 19:24:58 -0400 | [diff] [blame] | 30 | # pylint: disable=ungrouped-imports |
Mike Frysinger | bbd1f11 | 2016-09-08 18:25:11 -0400 | [diff] [blame] | 31 | third_party = os.path.join(constants.CHROMITE_DIR, 'third_party') |
| 32 | while True: |
| 33 | try: |
| 34 | sys.path.remove(third_party) |
| 35 | except ValueError: |
| 36 | break |
Mike Frysinger | bbd1f11 | 2016-09-08 18:25:11 -0400 | [diff] [blame] | 37 | sys.path.insert(0, os.path.join(third_party, 'upload_symbols')) |
| 38 | del third_party |
| 39 | |
| 40 | # Has to be after sys.path manipulation above. |
| 41 | # And our sys.path muckery confuses pylint. |
| 42 | import poster # pylint: disable=import-error |
| 43 | |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 44 | from chromite.lib import cache |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 45 | from chromite.lib import commandline |
| 46 | from chromite.lib import cros_build_lib |
Ralph Nathan | 5a582ff | 2015-03-20 18:18:30 -0700 | [diff] [blame] | 47 | from chromite.lib import cros_logging as logging |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 48 | from chromite.lib import gs |
| 49 | from chromite.lib import osutils |
Gilad Arnold | 83233ed | 2015-05-08 12:12:13 -0700 | [diff] [blame] | 50 | from chromite.lib import path_util |
Don Garrett | e1f47e9 | 2016-10-13 16:04:56 -0700 | [diff] [blame] | 51 | from chromite.lib import retry_stats |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 52 | from chromite.scripts import cros_generate_breakpad_symbols |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 53 | |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 54 | # Needs to be after chromite imports. |
Mike Frysinger | c5597f2 | 2014-11-27 15:39:15 -0500 | [diff] [blame] | 55 | # We don't want to import the general keyring module as that will implicitly |
| 56 | # try to import & connect to a dbus server. That's a waste of time. |
| 57 | sys.modules['keyring'] = None |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 58 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 59 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 60 | # We need this to run once per process. Do it at module import time as that |
| 61 | # will let us avoid doing it inline at function call time (see UploadSymbolFile) |
| 62 | # as that func might be called by the multiprocessing module which means we'll |
| 63 | # do the opener logic multiple times overall. Plus, if you're importing this |
| 64 | # module, it's a pretty good chance that you're going to need this. |
| 65 | poster.streaminghttp.register_openers() |
| 66 | |
| 67 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 68 | # URLs used for uploading symbols. |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 69 | OFFICIAL_UPLOAD_URL = 'https://prod-crashsymbolcollector-pa.googleapis.com/v1' |
| 70 | STAGING_UPLOAD_URL = 'https://staging-crashsymbolcollector-pa.googleapis.com/v1' |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 71 | |
| 72 | # The crash server rejects files that are this big. |
Ryo Hashimoto | f864c0e | 2017-02-14 12:46:08 +0900 | [diff] [blame] | 73 | CRASH_SERVER_FILE_LIMIT = 700 * 1024 * 1024 |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 74 | # Give ourselves a little breathing room from what the server expects. |
| 75 | DEFAULT_FILE_LIMIT = CRASH_SERVER_FILE_LIMIT - (10 * 1024 * 1024) |
| 76 | |
| 77 | |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 78 | # The batch limit when talking to the dedup server. We avoid sending one at a |
| 79 | # time as the round trip overhead will dominate. Conversely, we avoid sending |
| 80 | # all at once so we can start uploading symbols asap -- the symbol server is a |
| 81 | # bit slow and will take longer than anything else. |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 82 | DEDUPE_LIMIT = 100 |
| 83 | |
| 84 | # How long to wait for the server to respond with the results. Note that the |
| 85 | # larger the limit above, the larger this will need to be. So we give it ~1 |
| 86 | # second per item max. |
| 87 | DEDUPE_TIMEOUT = DEDUPE_LIMIT |
| 88 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 89 | # How long to wait for the notification to finish (in seconds). |
| 90 | DEDUPE_NOTIFY_TIMEOUT = 240 |
Mike Frysinger | 4dd462e | 2014-04-30 16:21:51 -0400 | [diff] [blame] | 91 | |
Mike Frysinger | 7104666 | 2014-09-12 18:15:15 -0700 | [diff] [blame] | 92 | # The minimum average rate (in bytes per second) that we expect to maintain |
| 93 | # when uploading symbols. This has to allow for symbols that are up to |
| 94 | # CRASH_SERVER_FILE_LIMIT in size. |
| 95 | UPLOAD_MIN_RATE = CRASH_SERVER_FILE_LIMIT / (30 * 60) |
| 96 | |
| 97 | # The lowest timeout (in seconds) we'll allow. If the server is overloaded, |
| 98 | # then there might be a delay in setting up the connection, not just with the |
| 99 | # transfer. So even a small file might need a larger value. |
Ryo Hashimoto | c004937 | 2017-02-16 18:50:00 +0900 | [diff] [blame] | 100 | UPLOAD_MIN_TIMEOUT = 5 * 60 |
Mike Frysinger | cd78a08 | 2013-06-26 17:13:04 -0400 | [diff] [blame] | 101 | |
| 102 | |
Don Garrett | 7a79309 | 2016-07-06 16:50:27 -0700 | [diff] [blame] | 103 | # Sleep for 500ms in between uploads to avoid DoS'ing symbol server. |
| 104 | SLEEP_DELAY = 0.5 |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 105 | |
| 106 | |
| 107 | # Number of seconds to wait before retrying an upload. The delay will double |
| 108 | # for each subsequent retry of the same symbol file. |
| 109 | INITIAL_RETRY_DELAY = 1 |
| 110 | |
| 111 | # Allow up to 7 attempts to upload a symbol file (total delay may be |
| 112 | # 1+2+4+8+16+32=63 seconds). |
| 113 | MAX_RETRIES = 6 |
| 114 | |
Mike Frysinger | eb753bf | 2013-11-22 16:05:35 -0500 | [diff] [blame] | 115 | # Number of total errors, before uploads are no longer attempted. |
| 116 | # This is used to avoid lots of errors causing unreasonable delays. |
Mike Frysinger | eb753bf | 2013-11-22 16:05:35 -0500 | [diff] [blame] | 117 | MAX_TOTAL_ERRORS_FOR_RETRY = 30 |
| 118 | |
Don Garrett | e1f47e9 | 2016-10-13 16:04:56 -0700 | [diff] [blame] | 119 | # Category to use for collection upload retry stats. |
| 120 | UPLOAD_STATS = 'UPLOAD' |
| 121 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 122 | |
Mike Nichols | 649e6a8 | 2019-04-23 11:44:48 -0600 | [diff] [blame] | 123 | class DuplicateFile(Exception): |
| 124 | """Thrown when a symbol file already exists on the server.""" |
| 125 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 126 | def BatchGenerator(iterator, batch_size): |
| 127 | """Given an iterator, break into lists of size batch_size. |
Fang Deng | ba68046 | 2015-08-16 20:34:11 -0700 | [diff] [blame] | 128 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 129 | The result is a generator, that will only read in as many inputs as needed for |
| 130 | the current batch. The final result can be smaller than batch_size. |
Mike Frysinger | 0a2fd92 | 2014-09-12 20:23:42 -0700 | [diff] [blame] | 131 | """ |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 132 | batch = [] |
| 133 | for i in iterator: |
| 134 | batch.append(i) |
| 135 | if len(batch) >= batch_size: |
| 136 | yield batch |
| 137 | batch = [] |
Mike Frysinger | 0a2fd92 | 2014-09-12 20:23:42 -0700 | [diff] [blame] | 138 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 139 | if batch: |
| 140 | # if there was anything left in the final batch, yield it. |
| 141 | yield batch |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 142 | |
| 143 | |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 144 | def IsTarball(path): |
| 145 | """Guess if this is a tarball based on the filename.""" |
| 146 | parts = path.split('.') |
| 147 | if len(parts) <= 1: |
| 148 | return False |
| 149 | |
| 150 | if parts[-1] == 'tar': |
| 151 | return True |
| 152 | |
| 153 | if parts[-2] == 'tar': |
| 154 | return parts[-1] in ('bz2', 'gz', 'xz') |
| 155 | |
| 156 | return parts[-1] in ('tbz2', 'tbz', 'tgz', 'txz') |
| 157 | |
| 158 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 159 | class SymbolFile(object): |
| 160 | """This class represents the state of a symbol file during processing. |
| 161 | |
| 162 | Properties: |
| 163 | display_path: Name of symbol file that should be consistent between builds. |
| 164 | file_name: Transient path of the symbol file. |
| 165 | header: ReadSymsHeader output. Dict with assorted meta-data. |
| 166 | status: INITIAL, DUPLICATE, or UPLOADED based on status of processing. |
| 167 | dedupe_item: None or instance of DedupeItem for this symbol file. |
| 168 | dedupe_push_state: Opaque value to return to dedupe code for file. |
| 169 | display_name: Read only friendly (short) file name for logging. |
| 170 | file_size: Read only size of the symbol file. |
| 171 | """ |
| 172 | INITIAL = 'initial' |
| 173 | DUPLICATE = 'duplicate' |
| 174 | UPLOADED = 'uploaded' |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 175 | ERROR = 'error' |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 176 | |
| 177 | def __init__(self, display_path, file_name): |
| 178 | """An instance of this class represents a symbol file over time. |
| 179 | |
| 180 | Args: |
| 181 | display_path: A unique/persistent between builds name to present to the |
| 182 | crash server. It is the file name, relative to where it |
| 183 | came from (tarball, breakpad dir, etc). |
| 184 | file_name: A the current location of the symbol file. |
| 185 | """ |
| 186 | self.display_path = display_path |
| 187 | self.file_name = file_name |
| 188 | self.header = cros_generate_breakpad_symbols.ReadSymsHeader(file_name) |
| 189 | self.status = SymbolFile.INITIAL |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 190 | |
| 191 | @property |
| 192 | def display_name(self): |
| 193 | return os.path.basename(self.display_path) |
| 194 | |
| 195 | def FileSize(self): |
| 196 | return os.path.getsize(self.file_name) |
| 197 | |
| 198 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 199 | def FindSymbolFiles(tempdir, paths): |
Mike Frysinger | 9b2ff5c | 2013-11-22 10:01:12 -0500 | [diff] [blame] | 200 | """Locate symbol files in |paths| |
| 201 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 202 | This returns SymbolFile objects that contain file references which are valid |
| 203 | after this exits. Those files may exist externally, or be created in the |
| 204 | tempdir (say, when expanding tarballs). The caller must not consider |
| 205 | SymbolFile's valid after tempdir is cleaned up. |
| 206 | |
Mike Frysinger | 9b2ff5c | 2013-11-22 10:01:12 -0500 | [diff] [blame] | 207 | Args: |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 208 | tempdir: Path to use for temporary files. |
Mike Frysinger | 9b2ff5c | 2013-11-22 10:01:12 -0500 | [diff] [blame] | 209 | paths: A list of input paths to walk. Files are returned w/out any checks. |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 210 | Dirs are searched for files that end in ".sym". Urls are fetched and then |
| 211 | processed. Tarballs are unpacked and walked. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 212 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 213 | Yields: |
| 214 | A SymbolFile for every symbol file found in paths. |
Mike Frysinger | 9b2ff5c | 2013-11-22 10:01:12 -0500 | [diff] [blame] | 215 | """ |
Gilad Arnold | 83233ed | 2015-05-08 12:12:13 -0700 | [diff] [blame] | 216 | cache_dir = path_util.GetCacheDir() |
Mike Frysinger | e847efd | 2015-01-08 03:57:24 -0500 | [diff] [blame] | 217 | common_path = os.path.join(cache_dir, constants.COMMON_CACHE) |
| 218 | tar_cache = cache.TarballCache(common_path) |
| 219 | |
Mike Frysinger | 9b2ff5c | 2013-11-22 10:01:12 -0500 | [diff] [blame] | 220 | for p in paths: |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 221 | o = urlparse.urlparse(p) |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 222 | if o.scheme: |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 223 | # Support globs of filenames. |
| 224 | ctx = gs.GSContext() |
| 225 | for p in ctx.LS(p): |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 226 | logging.info('processing files inside %s', p) |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 227 | o = urlparse.urlparse(p) |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 228 | key = ('%s%s' % (o.netloc, o.path)).split('/') |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 229 | # The common cache will not be LRU, removing the need to hold a read |
| 230 | # lock on the cached gsutil. |
| 231 | ref = tar_cache.Lookup(key) |
| 232 | try: |
| 233 | ref.SetDefault(p) |
| 234 | except cros_build_lib.RunCommandError as e: |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 235 | logging.warning('ignoring %s\n%s', p, e) |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 236 | continue |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 237 | for p in FindSymbolFiles(tempdir, [ref.path]): |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 238 | yield p |
| 239 | |
| 240 | elif os.path.isdir(p): |
Mike Frysinger | 9b2ff5c | 2013-11-22 10:01:12 -0500 | [diff] [blame] | 241 | for root, _, files in os.walk(p): |
| 242 | for f in files: |
| 243 | if f.endswith('.sym'): |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 244 | # If p is '/tmp/foo' and filename is '/tmp/foo/bar/bar.sym', |
| 245 | # display_path = 'bar/bar.sym' |
| 246 | filename = os.path.join(root, f) |
| 247 | yield SymbolFile(display_path=filename[len(p):].lstrip('/'), |
| 248 | file_name=filename) |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 249 | |
| 250 | elif IsTarball(p): |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 251 | logging.info('processing files inside %s', p) |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 252 | tardir = tempfile.mkdtemp(dir=tempdir) |
| 253 | cache.Untar(os.path.realpath(p), tardir) |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 254 | for p in FindSymbolFiles(tardir, [tardir]): |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 255 | yield p |
| 256 | |
Mike Frysinger | 9b2ff5c | 2013-11-22 10:01:12 -0500 | [diff] [blame] | 257 | else: |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 258 | yield SymbolFile(display_path=p, file_name=p) |
Mike Frysinger | 9b2ff5c | 2013-11-22 10:01:12 -0500 | [diff] [blame] | 259 | |
| 260 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 261 | def AdjustSymbolFileSize(symbol, tempdir, file_limit): |
| 262 | """Examine symbols files for size problems, and reduce if needed. |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 263 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 264 | If the symbols size is too big, strip out the call frame info. The CFI |
| 265 | is unnecessary for 32bit x86 targets where the frame pointer is used (as |
| 266 | all of ours have) and it accounts for over half the size of the symbols |
| 267 | uploaded. |
| 268 | |
| 269 | Stripped files will be created inside tempdir, and will be the callers |
| 270 | responsibility to clean up. |
| 271 | |
| 272 | We also warn, if a symbols file is still too large after stripping. |
Mike Frysinger | 5e6dd71 | 2014-03-07 22:21:17 -0500 | [diff] [blame] | 273 | |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 274 | Args: |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 275 | symbol: SymbolFile instance to be examined and modified as needed.. |
| 276 | tempdir: A temporary directory we can create files in that the caller will |
| 277 | clean up. |
| 278 | file_limit: We only strip files which are larger than this limit. |
| 279 | |
| 280 | Returns: |
| 281 | SymbolFile instance (original or modified as needed) |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 282 | """ |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 283 | file_size = symbol.FileSize() |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 284 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 285 | if file_limit and symbol.FileSize() > file_limit: |
| 286 | with tempfile.NamedTemporaryFile( |
| 287 | prefix='upload_symbols', bufsize=0, |
| 288 | dir=tempdir, delete=False) as temp_sym_file: |
| 289 | |
| 290 | temp_sym_file.writelines( |
| 291 | [x for x in open(symbol.file_name, 'rb').readlines() |
| 292 | if not x.startswith('STACK CFI')] |
| 293 | ) |
| 294 | |
| 295 | original_file_size = file_size |
| 296 | symbol.file_name = temp_sym_file.name |
| 297 | file_size = symbol.FileSize() |
| 298 | |
| 299 | logging.warning('stripped CFI for %s reducing size %s > %s', |
| 300 | symbol.display_name, original_file_size, file_size) |
| 301 | |
| 302 | # Hopefully the crash server will let it through. But it probably won't. |
| 303 | # Not sure what the best answer is in this case. |
| 304 | if file_size >= CRASH_SERVER_FILE_LIMIT: |
| 305 | logging.PrintBuildbotStepWarnings() |
| 306 | logging.warning('upload file %s is awfully large, risking rejection by ' |
| 307 | 'the symbol server (%s > %s)', symbol.display_path, |
| 308 | file_size, CRASH_SERVER_FILE_LIMIT) |
| 309 | |
| 310 | return symbol |
| 311 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 312 | |
| 313 | def GetUploadTimeout(symbol): |
| 314 | """How long to wait for a specific file to upload to the crash server. |
| 315 | |
| 316 | This is a function largely to make unittesting easier. |
| 317 | |
| 318 | Args: |
| 319 | symbol: A SymbolFile instance. |
| 320 | |
| 321 | Returns: |
| 322 | Timeout length (in seconds) |
| 323 | """ |
| 324 | # Scale the timeout based on the filesize. |
| 325 | return max(symbol.FileSize() / UPLOAD_MIN_RATE, UPLOAD_MIN_TIMEOUT) |
| 326 | |
| 327 | |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 328 | def ExecRequest(operator, url, timeout, api_key, **kwargs): |
| 329 | """Makes a web request with default timeout, returning the json result. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 330 | |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 331 | This method will raise a requests.exceptions.HTTPError if the status |
| 332 | code is not 4XX, 5XX |
| 333 | |
| 334 | Note: If you are using verbose logging it is entirely possible that the |
| 335 | subsystem will write your api key to the logs! |
| 336 | |
| 337 | Args: |
Mike Nichols | 649e6a8 | 2019-04-23 11:44:48 -0600 | [diff] [blame] | 338 | operator: HTTP method. |
| 339 | url: Endpoint URL. |
| 340 | timeout: HTTP timeout for request. |
| 341 | api_key: Authentication key. |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 342 | |
| 343 | Returns: |
| 344 | HTTP response content |
| 345 | """ |
| 346 | resp = requests.request(operator, url, |
| 347 | params={'key': api_key}, |
| 348 | headers={'User-agent': 'chromite.upload_symbols'}, |
| 349 | timeout=timeout, **kwargs) |
| 350 | # Make sure we don't leak secret keys by accident. |
| 351 | if resp.status_code > 399: |
| 352 | resp.url = resp.url.replace(urllib2.quote(api_key), 'XX-HIDDEN-XX') |
Mike Nichols | cf5b7a9 | 2019-04-25 12:01:28 -0600 | [diff] [blame] | 353 | logging.warning('Url: %s, Status: %s, response: "%s", in: %s', |
| 354 | resp.url, resp.status_code, resp.text, resp.elapsed) |
| 355 | elif resp.content: |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 356 | return resp.json() |
Mike Nichols | cf5b7a9 | 2019-04-25 12:01:28 -0600 | [diff] [blame] | 357 | |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 358 | return {} |
| 359 | |
| 360 | |
Mike Nichols | 649e6a8 | 2019-04-23 11:44:48 -0600 | [diff] [blame] | 361 | def UploadExists(symbol, status_url, timeout, api_key): |
| 362 | """Skip a symbol files if we've already uploaded. |
| 363 | |
| 364 | Args: |
| 365 | symbol: A SymbolFile instance. |
| 366 | status_url: The crash URL to validate the file existence. |
| 367 | timeout: HTTP timeout for request. |
| 368 | api_key: Authentication key. |
| 369 | |
| 370 | Yields: |
| 371 | Boolean, true if it is available on server, false if |
| 372 | it is not. |
| 373 | """ |
| 374 | result = ExecRequest('get', '%s/symbols/%s/%s:checkStatus' % |
| 375 | (status_url, symbol.header.name, |
| 376 | symbol.header.id.replace('-', '')), |
| 377 | timeout, api_key) |
| 378 | return (result and 'status' in result.keys() |
| 379 | and result['status'] == 'FOUND') |
| 380 | |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 381 | def UploadSymbolFile(upload_url, symbol, api_key): |
| 382 | '''Upload a symbol file to the crash server, returning the status result. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 383 | |
| 384 | Args: |
| 385 | upload_url: The crash URL to POST the |sym_file| to |
| 386 | symbol: A SymbolFile instance. |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 387 | api_key: Authentication key |
| 388 | ''' |
| 389 | timeout = GetUploadTimeout(symbol) |
Mike Nichols | 649e6a8 | 2019-04-23 11:44:48 -0600 | [diff] [blame] | 390 | if UploadExists(symbol, upload_url, timeout, api_key): |
| 391 | logging.info('%s symbol file already uploaded, skipping', symbol.header.id) |
| 392 | raise DuplicateFile |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 393 | else: |
Mike Nichols | 649e6a8 | 2019-04-23 11:44:48 -0600 | [diff] [blame] | 394 | upload = ExecRequest('post', |
| 395 | '%s/uploads:create' % upload_url, timeout, api_key) |
| 396 | |
| 397 | if upload and 'uploadUrl' in upload.keys(): |
| 398 | symbol_data = {'symbol_id': |
| 399 | {'debug_file': symbol.header.name, |
| 400 | 'debug_id': symbol.header.id.replace('-', '')} |
| 401 | } |
| 402 | ExecRequest('put', |
| 403 | upload['uploadUrl'], timeout, |
| 404 | api_key=api_key, |
| 405 | data=open(symbol.file_name, 'r')) |
| 406 | ExecRequest('post', |
| 407 | '%s/uploads/%s:complete' % ( |
| 408 | upload_url, upload['uploadKey']), |
| 409 | timeout, api_key=api_key, |
| 410 | # TODO(mikenichols): Validate product_name once it is added |
| 411 | # to the proto; currently unsupported. |
| 412 | data=json.dumps(symbol_data)) |
| 413 | else: |
| 414 | raise requests.exceptions.HTTPError |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 415 | |
| 416 | |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 417 | def PerformSymbolsFileUpload(symbols, upload_url, api_key): |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 418 | """Upload the symbols to the crash server |
| 419 | |
| 420 | Args: |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 421 | symbols: An iterable of SymbolFiles to be uploaded. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 422 | upload_url: URL of crash server to upload too. |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 423 | api_key: Authentication key. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 424 | failures: Tracker for total upload failures. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 425 | |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 426 | Yields: |
| 427 | Each symbol from symbols, perhaps modified. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 428 | """ |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 429 | failures = 0 |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 430 | |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 431 | for s in symbols: |
| 432 | if (failures < MAX_TOTAL_ERRORS_FOR_RETRY and |
| 433 | s.status in (SymbolFile.INITIAL, SymbolFile.ERROR)): |
| 434 | # Keeps us from DoS-ing the symbol server. |
| 435 | time.sleep(SLEEP_DELAY) |
| 436 | logging.info('Uploading symbol_file: %s', s.display_path) |
| 437 | try: |
| 438 | # This command retries the upload multiple times with growing delays. We |
| 439 | # only consider the upload a failure if these retries fail. |
Don Garrett | e1f47e9 | 2016-10-13 16:04:56 -0700 | [diff] [blame] | 440 | def ShouldRetryUpload(exception): |
Mike Nichols | 0abb8d3 | 2019-04-26 14:55:08 -0600 | [diff] [blame] | 441 | if isinstance(exception, (requests.exceptions.RequestException, |
| 442 | urllib2.URLError, |
| 443 | httplib.HTTPException, socket.error)): |
| 444 | logging.info('Request failed, retrying: %s', exception) |
| 445 | return True |
| 446 | return False |
Don Garrett | e1f47e9 | 2016-10-13 16:04:56 -0700 | [diff] [blame] | 447 | |
Don Garrett | 440944e | 2016-10-03 16:33:31 -0700 | [diff] [blame] | 448 | with cros_build_lib.TimedSection() as timer: |
Don Garrett | e1f47e9 | 2016-10-13 16:04:56 -0700 | [diff] [blame] | 449 | retry_stats.RetryWithStats( |
| 450 | UPLOAD_STATS, ShouldRetryUpload, MAX_RETRIES, |
Don Garrett | 440944e | 2016-10-03 16:33:31 -0700 | [diff] [blame] | 451 | UploadSymbolFile, |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 452 | upload_url, s, api_key, |
Luigi Semenzato | 5104f3c | 2016-10-12 12:37:42 -0700 | [diff] [blame] | 453 | sleep=INITIAL_RETRY_DELAY, |
| 454 | log_all_retries=True) |
Don Garrett | 440944e | 2016-10-03 16:33:31 -0700 | [diff] [blame] | 455 | logging.info('upload of %10i bytes took %s', s.FileSize(), timer.delta) |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 456 | s.status = SymbolFile.UPLOADED |
Mike Nichols | 649e6a8 | 2019-04-23 11:44:48 -0600 | [diff] [blame] | 457 | except DuplicateFile: |
| 458 | s.status = SymbolFile.DUPLICATE |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 459 | except (requests.exceptions.HTTPError, |
| 460 | requests.exceptions.Timeout, |
| 461 | requests.exceptions.RequestException) as e: |
| 462 | logging.warning('could not upload: %s: HTTP error: %s', |
| 463 | s.display_name, e) |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 464 | s.status = SymbolFile.ERROR |
| 465 | failures += 1 |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 466 | except (httplib.HTTPException, urllib2.URLError, socket.error) as e: |
Ryo Hashimoto | 45273f0 | 2017-03-16 17:45:56 +0900 | [diff] [blame] | 467 | logging.warning('could not upload: %s: %s %s', s.display_name, |
| 468 | type(e).__name__, e) |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 469 | s.status = SymbolFile.ERROR |
| 470 | failures += 1 |
| 471 | |
| 472 | # We pass the symbol along, on both success and failure. |
| 473 | yield s |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 474 | |
| 475 | |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 476 | def ReportResults(symbols, failed_list): |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 477 | """Log a summary of the symbol uploading. |
| 478 | |
| 479 | This has the side effect of fully consuming the symbols iterator. |
| 480 | |
| 481 | Args: |
| 482 | symbols: An iterator of SymbolFiles to be uploaded. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 483 | failed_list: A filename at which to write out a list of our failed uploads. |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 484 | |
| 485 | Returns: |
| 486 | The number of symbols not uploaded. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 487 | """ |
| 488 | upload_failures = [] |
| 489 | result_counts = { |
| 490 | SymbolFile.INITIAL: 0, |
| 491 | SymbolFile.UPLOADED: 0, |
| 492 | SymbolFile.DUPLICATE: 0, |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 493 | SymbolFile.ERROR: 0, |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | for s in symbols: |
| 497 | result_counts[s.status] += 1 |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 498 | if s.status in [SymbolFile.INITIAL, SymbolFile.ERROR]: |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 499 | upload_failures.append(s) |
| 500 | |
Don Garrett | e1f47e9 | 2016-10-13 16:04:56 -0700 | [diff] [blame] | 501 | # Report retry numbers. |
| 502 | _, _, retries = retry_stats.CategoryStats(UPLOAD_STATS) |
| 503 | if retries: |
| 504 | logging.warning('%d upload retries performed.', retries) |
| 505 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 506 | logging.info('Uploaded %(uploaded)d, Skipped %(duplicate)d duplicates.', |
| 507 | result_counts) |
| 508 | |
Chris Ching | 9190803 | 2016-09-27 16:55:33 -0600 | [diff] [blame] | 509 | if result_counts[SymbolFile.ERROR]: |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 510 | logging.PrintBuildbotStepWarnings() |
Chris Ching | 9190803 | 2016-09-27 16:55:33 -0600 | [diff] [blame] | 511 | logging.warning('%d non-recoverable upload errors', |
| 512 | result_counts[SymbolFile.ERROR]) |
| 513 | |
| 514 | if result_counts[SymbolFile.INITIAL]: |
| 515 | logging.PrintBuildbotStepWarnings() |
| 516 | logging.warning('%d upload(s) were skipped because of excessive errors', |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 517 | result_counts[SymbolFile.INITIAL]) |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 518 | |
| 519 | if failed_list is not None: |
| 520 | with open(failed_list, 'w') as fl: |
| 521 | for s in upload_failures: |
| 522 | fl.write('%s\n' % s.display_path) |
| 523 | |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 524 | return result_counts[SymbolFile.INITIAL] + result_counts[SymbolFile.ERROR] |
| 525 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 526 | |
Mike Nichols | 649e6a8 | 2019-04-23 11:44:48 -0600 | [diff] [blame] | 527 | def UploadSymbols(sym_paths, upload_url, failed_list=None, |
| 528 | upload_limit=None, strip_cfi=None, |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 529 | api_key=None): |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 530 | """Upload all the generated symbols for |board| to the crash server |
| 531 | |
| 532 | Args: |
Mike Frysinger | 9b2ff5c | 2013-11-22 10:01:12 -0500 | [diff] [blame] | 533 | sym_paths: Specific symbol files (or dirs of sym files) to upload, |
| 534 | otherwise search |breakpad_dir| |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 535 | upload_url: URL of crash server to upload too. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 536 | failed_list: A filename at which to write out a list of our failed uploads. |
| 537 | upload_limit: Integer listing how many files to upload. None for no limit. |
| 538 | strip_cfi: File size at which we strip out CFI data. None for no limit. |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 539 | api_key: A string based authentication key |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 540 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 541 | Returns: |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 542 | The number of errors that were encountered. |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 543 | """ |
Don Garrett | e1f47e9 | 2016-10-13 16:04:56 -0700 | [diff] [blame] | 544 | retry_stats.SetupStats() |
| 545 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 546 | # Note: This method looks like each step of processing is performed |
| 547 | # sequentially for all SymbolFiles, but instead each step is a generator that |
| 548 | # produces the next iteration only when it's read. This means that (except for |
| 549 | # some batching) each SymbolFile goes through all of these steps before the |
| 550 | # next one is processed at all. |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 551 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 552 | # This is used to hold striped |
| 553 | with osutils.TempDir(prefix='upload_symbols.') as tempdir: |
| 554 | symbols = FindSymbolFiles(tempdir, sym_paths) |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 555 | |
Don Garrett | 1bc1e10 | 2016-07-06 17:06:10 -0700 | [diff] [blame] | 556 | # Sort all of our symbols so the largest ones (probably the most important) |
| 557 | # are processed first. |
| 558 | symbols = list(symbols) |
| 559 | symbols.sort(key=lambda s: s.FileSize(), reverse=True) |
| 560 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 561 | if upload_limit is not None: |
| 562 | # Restrict symbols processed to the limit. |
| 563 | symbols = itertools.islice(symbols, None, upload_limit) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 564 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 565 | # Strip CFI, if needed. |
| 566 | symbols = (AdjustSymbolFileSize(s, tempdir, strip_cfi) for s in symbols) |
Mike Frysinger | 8ec8c50 | 2014-02-10 00:19:13 -0500 | [diff] [blame] | 567 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 568 | # Perform uploads |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 569 | symbols = PerformSymbolsFileUpload(symbols, upload_url, api_key) |
Mike Frysinger | d42e5f0 | 2014-03-14 11:19:37 -0400 | [diff] [blame] | 570 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 571 | # Log the final results, and consume the symbols generator fully. |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 572 | failures = ReportResults(symbols, failed_list) |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 573 | |
Don Garrett | deb2e03 | 2016-07-06 16:44:14 -0700 | [diff] [blame] | 574 | return failures |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 575 | |
| 576 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 577 | def main(argv): |
| 578 | parser = commandline.ArgumentParser(description=__doc__) |
| 579 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 580 | # TODO: Make sym_paths, breakpad_root, and root exclusive. |
| 581 | |
Mike Frysinger | d41938e | 2014-02-10 06:37:55 -0500 | [diff] [blame] | 582 | parser.add_argument('sym_paths', type='path_or_uri', nargs='*', default=None, |
| 583 | help='symbol file or directory or URL or tarball') |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 584 | parser.add_argument('--board', default=None, |
Don Garrett | 747cc4b | 2015-10-07 14:48:48 -0700 | [diff] [blame] | 585 | help='Used to find default breakpad_root.') |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 586 | parser.add_argument('--breakpad_root', type='path', default=None, |
Mike Frysinger | c5597f2 | 2014-11-27 15:39:15 -0500 | [diff] [blame] | 587 | help='full path to the breakpad symbol directory') |
| 588 | parser.add_argument('--root', type='path', default=None, |
| 589 | help='full path to the chroot dir') |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 590 | parser.add_argument('--official_build', action='store_true', default=False, |
| 591 | help='point to official symbol server') |
Mike Frysinger | 3864754 | 2014-09-12 18:15:39 -0700 | [diff] [blame] | 592 | parser.add_argument('--server', type=str, default=None, |
| 593 | help='URI for custom symbol server') |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 594 | parser.add_argument('--regenerate', action='store_true', default=False, |
| 595 | help='regenerate all symbols') |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 596 | parser.add_argument('--upload-limit', type=int, |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 597 | help='only upload # number of symbols') |
| 598 | parser.add_argument('--strip_cfi', type=int, |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 599 | default=DEFAULT_FILE_LIMIT, |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 600 | help='strip CFI data for files above this size') |
Mike Frysinger | 7f9be14 | 2014-01-15 02:16:42 -0500 | [diff] [blame] | 601 | parser.add_argument('--failed-list', type='path', |
| 602 | help='where to save a list of failed symbols') |
Mike Frysinger | 0c0efa2 | 2014-02-09 23:32:23 -0500 | [diff] [blame] | 603 | parser.add_argument('--dedupe', action='store_true', default=False, |
| 604 | help='use the swarming service to avoid re-uploading') |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 605 | parser.add_argument('--yes', action='store_true', default=False, |
| 606 | help='answer yes to all prompts') |
Don Garrett | 747cc4b | 2015-10-07 14:48:48 -0700 | [diff] [blame] | 607 | parser.add_argument('--product_name', type=str, default='ChromeOS', |
| 608 | help='Produce Name for breakpad stats.') |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 609 | parser.add_argument('--api_key', type=str, default=None, |
| 610 | help='full path to the API key file') |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 611 | |
| 612 | opts = parser.parse_args(argv) |
Mike Frysinger | 90e49ca | 2014-01-14 14:42:07 -0500 | [diff] [blame] | 613 | opts.Freeze() |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 614 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 615 | # Figure out the symbol files/directories to upload. |
| 616 | if opts.sym_paths: |
| 617 | sym_paths = opts.sym_paths |
| 618 | elif opts.breakpad_root: |
| 619 | sym_paths = [opts.breakpad_root] |
| 620 | elif opts.root: |
| 621 | if not opts.board: |
Mike Frysinger | 8390dec | 2017-08-11 15:11:38 -0400 | [diff] [blame] | 622 | cros_build_lib.Die('--board must be set if --root is used.') |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 623 | breakpad_dir = cros_generate_breakpad_symbols.FindBreakpadDir(opts.board) |
| 624 | sym_paths = [os.path.join(opts.root, breakpad_dir.lstrip('/'))] |
| 625 | else: |
Mike Frysinger | 8390dec | 2017-08-11 15:11:38 -0400 | [diff] [blame] | 626 | cros_build_lib.Die('--sym_paths, --breakpad_root, or --root must be set.') |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 627 | |
Don Garrett | 747cc4b | 2015-10-07 14:48:48 -0700 | [diff] [blame] | 628 | if opts.sym_paths or opts.breakpad_root: |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 629 | if opts.regenerate: |
Don Garrett | 747cc4b | 2015-10-07 14:48:48 -0700 | [diff] [blame] | 630 | cros_build_lib.Die('--regenerate may not be used with specific files, ' |
| 631 | 'or breakpad_root') |
Mike Frysinger | 9dcf9ae | 2013-08-10 15:17:09 -0400 | [diff] [blame] | 632 | else: |
| 633 | if opts.board is None: |
| 634 | cros_build_lib.Die('--board is required') |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 635 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 636 | # Figure out which crash server to upload too. |
| 637 | upload_url = opts.server |
| 638 | if not upload_url: |
| 639 | if opts.official_build: |
| 640 | upload_url = OFFICIAL_UPLOAD_URL |
| 641 | else: |
| 642 | logging.warning('unofficial builds upload to the staging server') |
| 643 | upload_url = STAGING_UPLOAD_URL |
| 644 | |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 645 | # Set up the API key needed to authenticate to Crash server. |
| 646 | # Allow for a local key file for testing purposes. |
| 647 | if opts.api_key: |
| 648 | api_key_file = opts.api_key |
| 649 | else: |
| 650 | api_key_file = constants.CRASH_API_KEY |
| 651 | |
| 652 | api_key = osutils.ReadFile(api_key_file) |
| 653 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 654 | # Confirm we really want the long upload. |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 655 | if not opts.yes: |
Mike Frysinger | c5de960 | 2014-02-09 02:42:36 -0500 | [diff] [blame] | 656 | prolog = '\n'.join(textwrap.wrap(textwrap.dedent(""" |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 657 | Uploading symbols for an entire Chromium OS build is really only |
| 658 | necessary for release builds and in a few cases for developers |
| 659 | to debug problems. It will take considerable time to run. For |
| 660 | developer debugging purposes, consider instead passing specific |
| 661 | files to upload. |
Mike Frysinger | c5de960 | 2014-02-09 02:42:36 -0500 | [diff] [blame] | 662 | """), 80)).strip() |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 663 | if not cros_build_lib.BooleanPrompt( |
| 664 | prompt='Are you sure you want to upload all build symbols', |
Mike Frysinger | c5de960 | 2014-02-09 02:42:36 -0500 | [diff] [blame] | 665 | default=False, prolog=prolog): |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 666 | cros_build_lib.Die('better safe than sorry') |
| 667 | |
| 668 | ret = 0 |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 669 | |
| 670 | # Regenerate symbols from binaries. |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 671 | if opts.regenerate: |
Mike Frysinger | 69cb41d | 2013-08-11 20:08:19 -0400 | [diff] [blame] | 672 | ret += cros_generate_breakpad_symbols.GenerateBreakpadSymbols( |
| 673 | opts.board, breakpad_dir=opts.breakpad_root) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 674 | |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 675 | # Do the upload. |
| 676 | ret += UploadSymbols( |
| 677 | sym_paths=sym_paths, |
| 678 | upload_url=upload_url, |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 679 | failed_list=opts.failed_list, |
| 680 | upload_limit=opts.upload_limit, |
Mike Nichols | 90f7c15 | 2019-04-09 15:14:08 -0600 | [diff] [blame] | 681 | strip_cfi=opts.strip_cfi, |
| 682 | api_key=api_key) |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 683 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 684 | if ret: |
Ralph Nathan | 5990042 | 2015-03-24 10:41:17 -0700 | [diff] [blame] | 685 | logging.error('encountered %i problem(s)', ret) |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 686 | # Since exit(status) gets masked, clamp it to 1 so we don't inadvertently |
| 687 | # return 0 in case we are a multiple of the mask. |
Don Garrett | a28be6d | 2016-06-16 18:09:35 -0700 | [diff] [blame] | 688 | return 1 |