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