blob: afbb0208ecd3b0de37d699b4fa97c6d443815eb3 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -04002# 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
8This script need only be used to upload release builds symbols or to debug
9crashes on non-release builds (in which case try to only upload the symbols
Mike Frysinger02e1e072013-11-10 22:11:34 -050010for those executables involved).
11"""
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040012
Mike Frysingera4fa1e82014-01-15 01:45:56 -050013from __future__ import print_function
14
Don Garretta28be6d2016-06-16 18:09:35 -070015import itertools
Mike Nichols90f7c152019-04-09 15:14:08 -060016import json
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040017import os
Mike Frysingerfd355652014-01-23 02:57:48 -050018import socket
Mike Frysingerc5597f22014-11-27 15:39:15 -050019import sys
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040020import textwrap
21import tempfile
22import time
23
Mike Frysinger6db648e2018-07-24 19:57:58 -040024import requests
Mike Frysinger3d465162019-08-28 00:40:23 -040025from six.moves import http_client as httplib
Mike Frysinger3dcacee2019-08-23 17:09:11 -040026from six.moves import urllib
Mike Frysinger6db648e2018-07-24 19:57:58 -040027
Mike Frysingerd41938e2014-02-10 06:37:55 -050028from chromite.lib import cache
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040029from chromite.lib import commandline
Mike Frysinger38002522019-10-13 23:34:35 -040030from chromite.lib import constants
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040031from chromite.lib import cros_build_lib
Ralph Nathan5a582ff2015-03-20 18:18:30 -070032from chromite.lib import cros_logging as logging
Mike Frysingerd41938e2014-02-10 06:37:55 -050033from chromite.lib import gs
34from chromite.lib import osutils
Gilad Arnold83233ed2015-05-08 12:12:13 -070035from chromite.lib import path_util
Don Garrette1f47e92016-10-13 16:04:56 -070036from chromite.lib import retry_stats
Mike Frysinger69cb41d2013-08-11 20:08:19 -040037from chromite.scripts import cros_generate_breakpad_symbols
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040038
Mike Frysinger0c0efa22014-02-09 23:32:23 -050039# Needs to be after chromite imports.
Mike Frysingerc5597f22014-11-27 15:39:15 -050040# 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.
42sys.modules['keyring'] = None
Mike Frysinger0c0efa22014-02-09 23:32:23 -050043
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040044
45# URLs used for uploading symbols.
Mike Nichols90f7c152019-04-09 15:14:08 -060046OFFICIAL_UPLOAD_URL = 'https://prod-crashsymbolcollector-pa.googleapis.com/v1'
47STAGING_UPLOAD_URL = 'https://staging-crashsymbolcollector-pa.googleapis.com/v1'
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040048
49# The crash server rejects files that are this big.
Ryo Hashimotof864c0e2017-02-14 12:46:08 +090050CRASH_SERVER_FILE_LIMIT = 700 * 1024 * 1024
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040051# Give ourselves a little breathing room from what the server expects.
52DEFAULT_FILE_LIMIT = CRASH_SERVER_FILE_LIMIT - (10 * 1024 * 1024)
53
54
Mike Frysinger0c0efa22014-02-09 23:32:23 -050055# 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 Frysinger0c0efa22014-02-09 23:32:23 -050059DEDUPE_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.
64DEDUPE_TIMEOUT = DEDUPE_LIMIT
65
Don Garretta28be6d2016-06-16 18:09:35 -070066# How long to wait for the notification to finish (in seconds).
67DEDUPE_NOTIFY_TIMEOUT = 240
Mike Frysinger4dd462e2014-04-30 16:21:51 -040068
Mike Frysinger71046662014-09-12 18:15:15 -070069# 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 Frysinger93e8ffa2019-07-03 20:24:18 -040072UPLOAD_MIN_RATE = CRASH_SERVER_FILE_LIMIT // (30 * 60)
Mike Frysinger71046662014-09-12 18:15:15 -070073
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 Hashimotoc0049372017-02-16 18:50:00 +090077UPLOAD_MIN_TIMEOUT = 5 * 60
Mike Frysingercd78a082013-06-26 17:13:04 -040078
79
Don Garrett7a793092016-07-06 16:50:27 -070080# Sleep for 500ms in between uploads to avoid DoS'ing symbol server.
81SLEEP_DELAY = 0.5
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040082
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.
86INITIAL_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).
90MAX_RETRIES = 6
91
Mike Frysingereb753bf2013-11-22 16:05:35 -050092# Number of total errors, before uploads are no longer attempted.
93# This is used to avoid lots of errors causing unreasonable delays.
Mike Frysingereb753bf2013-11-22 16:05:35 -050094MAX_TOTAL_ERRORS_FOR_RETRY = 30
95
Don Garrette1f47e92016-10-13 16:04:56 -070096# Category to use for collection upload retry stats.
97UPLOAD_STATS = 'UPLOAD'
98
Don Garretta28be6d2016-06-16 18:09:35 -070099
Don Garretta28be6d2016-06-16 18:09:35 -0700100def BatchGenerator(iterator, batch_size):
101 """Given an iterator, break into lists of size batch_size.
Fang Dengba680462015-08-16 20:34:11 -0700102
Don Garretta28be6d2016-06-16 18:09:35 -0700103 The result is a generator, that will only read in as many inputs as needed for
104 the current batch. The final result can be smaller than batch_size.
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700105 """
Don Garretta28be6d2016-06-16 18:09:35 -0700106 batch = []
107 for i in iterator:
108 batch.append(i)
109 if len(batch) >= batch_size:
110 yield batch
111 batch = []
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700112
Don Garretta28be6d2016-06-16 18:09:35 -0700113 if batch:
114 # if there was anything left in the final batch, yield it.
115 yield batch
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500116
117
Mike Frysingerd41938e2014-02-10 06:37:55 -0500118def IsTarball(path):
119 """Guess if this is a tarball based on the filename."""
120 parts = path.split('.')
121 if len(parts) <= 1:
122 return False
123
124 if parts[-1] == 'tar':
125 return True
126
127 if parts[-2] == 'tar':
128 return parts[-1] in ('bz2', 'gz', 'xz')
129
130 return parts[-1] in ('tbz2', 'tbz', 'tgz', 'txz')
131
132
Don Garretta28be6d2016-06-16 18:09:35 -0700133class SymbolFile(object):
134 """This class represents the state of a symbol file during processing.
135
Mike Frysinger309f6ab2019-09-22 13:33:54 -0400136 Attributes:
Don Garretta28be6d2016-06-16 18:09:35 -0700137 display_path: Name of symbol file that should be consistent between builds.
138 file_name: Transient path of the symbol file.
139 header: ReadSymsHeader output. Dict with assorted meta-data.
140 status: INITIAL, DUPLICATE, or UPLOADED based on status of processing.
141 dedupe_item: None or instance of DedupeItem for this symbol file.
142 dedupe_push_state: Opaque value to return to dedupe code for file.
143 display_name: Read only friendly (short) file name for logging.
144 file_size: Read only size of the symbol file.
145 """
146 INITIAL = 'initial'
147 DUPLICATE = 'duplicate'
148 UPLOADED = 'uploaded'
Don Garrettdeb2e032016-07-06 16:44:14 -0700149 ERROR = 'error'
Don Garretta28be6d2016-06-16 18:09:35 -0700150
151 def __init__(self, display_path, file_name):
152 """An instance of this class represents a symbol file over time.
153
154 Args:
155 display_path: A unique/persistent between builds name to present to the
156 crash server. It is the file name, relative to where it
157 came from (tarball, breakpad dir, etc).
158 file_name: A the current location of the symbol file.
159 """
160 self.display_path = display_path
161 self.file_name = file_name
162 self.header = cros_generate_breakpad_symbols.ReadSymsHeader(file_name)
163 self.status = SymbolFile.INITIAL
Don Garretta28be6d2016-06-16 18:09:35 -0700164
165 @property
166 def display_name(self):
167 return os.path.basename(self.display_path)
168
169 def FileSize(self):
170 return os.path.getsize(self.file_name)
171
172
Don Garretta28be6d2016-06-16 18:09:35 -0700173def FindSymbolFiles(tempdir, paths):
Mike Frysinger9b2ff5c2013-11-22 10:01:12 -0500174 """Locate symbol files in |paths|
175
Don Garretta28be6d2016-06-16 18:09:35 -0700176 This returns SymbolFile objects that contain file references which are valid
177 after this exits. Those files may exist externally, or be created in the
178 tempdir (say, when expanding tarballs). The caller must not consider
179 SymbolFile's valid after tempdir is cleaned up.
180
Mike Frysinger9b2ff5c2013-11-22 10:01:12 -0500181 Args:
Don Garretta28be6d2016-06-16 18:09:35 -0700182 tempdir: Path to use for temporary files.
Mike Frysinger9b2ff5c2013-11-22 10:01:12 -0500183 paths: A list of input paths to walk. Files are returned w/out any checks.
Mike Frysingerd41938e2014-02-10 06:37:55 -0500184 Dirs are searched for files that end in ".sym". Urls are fetched and then
185 processed. Tarballs are unpacked and walked.
Mike Frysinger1a736a82013-12-12 01:50:59 -0500186
Don Garretta28be6d2016-06-16 18:09:35 -0700187 Yields:
188 A SymbolFile for every symbol file found in paths.
Mike Frysinger9b2ff5c2013-11-22 10:01:12 -0500189 """
Gilad Arnold83233ed2015-05-08 12:12:13 -0700190 cache_dir = path_util.GetCacheDir()
Mike Frysingere847efd2015-01-08 03:57:24 -0500191 common_path = os.path.join(cache_dir, constants.COMMON_CACHE)
192 tar_cache = cache.TarballCache(common_path)
193
Mike Frysinger9b2ff5c2013-11-22 10:01:12 -0500194 for p in paths:
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400195 o = urllib.parse.urlparse(p)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400196 if o.scheme:
Mike Frysingerd41938e2014-02-10 06:37:55 -0500197 # Support globs of filenames.
198 ctx = gs.GSContext()
Mike Frysinger8ab15bb2019-09-18 17:24:36 -0400199 for gspath in ctx.LS(p):
200 logging.info('processing files inside %s', gspath)
201 o = urllib.parse.urlparse(gspath)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400202 key = ('%s%s' % (o.netloc, o.path)).split('/')
Mike Frysingerd41938e2014-02-10 06:37:55 -0500203 # The common cache will not be LRU, removing the need to hold a read
204 # lock on the cached gsutil.
205 ref = tar_cache.Lookup(key)
206 try:
Mike Frysinger8ab15bb2019-09-18 17:24:36 -0400207 ref.SetDefault(gspath)
Mike Frysingerd41938e2014-02-10 06:37:55 -0500208 except cros_build_lib.RunCommandError as e:
Mike Frysinger8ab15bb2019-09-18 17:24:36 -0400209 logging.warning('ignoring %s\n%s', gspath, e)
Mike Frysingerd41938e2014-02-10 06:37:55 -0500210 continue
Mike Frysinger8ab15bb2019-09-18 17:24:36 -0400211 for sym in FindSymbolFiles(tempdir, [ref.path]):
212 yield sym
Mike Frysingerd41938e2014-02-10 06:37:55 -0500213
214 elif os.path.isdir(p):
Mike Frysinger9b2ff5c2013-11-22 10:01:12 -0500215 for root, _, files in os.walk(p):
216 for f in files:
217 if f.endswith('.sym'):
Don Garretta28be6d2016-06-16 18:09:35 -0700218 # If p is '/tmp/foo' and filename is '/tmp/foo/bar/bar.sym',
219 # display_path = 'bar/bar.sym'
220 filename = os.path.join(root, f)
221 yield SymbolFile(display_path=filename[len(p):].lstrip('/'),
222 file_name=filename)
Mike Frysingerd41938e2014-02-10 06:37:55 -0500223
224 elif IsTarball(p):
Ralph Nathan03047282015-03-23 11:09:32 -0700225 logging.info('processing files inside %s', p)
Mike Frysingerd41938e2014-02-10 06:37:55 -0500226 tardir = tempfile.mkdtemp(dir=tempdir)
227 cache.Untar(os.path.realpath(p), tardir)
Mike Frysinger8ab15bb2019-09-18 17:24:36 -0400228 for sym in FindSymbolFiles(tardir, [tardir]):
229 yield sym
Mike Frysingerd41938e2014-02-10 06:37:55 -0500230
Mike Frysinger9b2ff5c2013-11-22 10:01:12 -0500231 else:
Don Garretta28be6d2016-06-16 18:09:35 -0700232 yield SymbolFile(display_path=p, file_name=p)
Mike Frysinger9b2ff5c2013-11-22 10:01:12 -0500233
234
Don Garretta28be6d2016-06-16 18:09:35 -0700235def AdjustSymbolFileSize(symbol, tempdir, file_limit):
236 """Examine symbols files for size problems, and reduce if needed.
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500237
Don Garretta28be6d2016-06-16 18:09:35 -0700238 If the symbols size is too big, strip out the call frame info. The CFI
239 is unnecessary for 32bit x86 targets where the frame pointer is used (as
240 all of ours have) and it accounts for over half the size of the symbols
241 uploaded.
242
243 Stripped files will be created inside tempdir, and will be the callers
244 responsibility to clean up.
245
246 We also warn, if a symbols file is still too large after stripping.
Mike Frysinger5e6dd712014-03-07 22:21:17 -0500247
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500248 Args:
Don Garretta28be6d2016-06-16 18:09:35 -0700249 symbol: SymbolFile instance to be examined and modified as needed..
250 tempdir: A temporary directory we can create files in that the caller will
251 clean up.
252 file_limit: We only strip files which are larger than this limit.
253
254 Returns:
255 SymbolFile instance (original or modified as needed)
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500256 """
Don Garretta28be6d2016-06-16 18:09:35 -0700257 file_size = symbol.FileSize()
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500258
Don Garretta28be6d2016-06-16 18:09:35 -0700259 if file_limit and symbol.FileSize() > file_limit:
Mike Frysinger59babdb2019-09-06 06:25:50 -0400260 with cros_build_lib.UnbufferedNamedTemporaryFile(
261 prefix='upload_symbols',
Don Garretta28be6d2016-06-16 18:09:35 -0700262 dir=tempdir, delete=False) as temp_sym_file:
263
264 temp_sym_file.writelines(
265 [x for x in open(symbol.file_name, 'rb').readlines()
266 if not x.startswith('STACK CFI')]
267 )
268
269 original_file_size = file_size
270 symbol.file_name = temp_sym_file.name
271 file_size = symbol.FileSize()
272
273 logging.warning('stripped CFI for %s reducing size %s > %s',
274 symbol.display_name, original_file_size, file_size)
275
276 # Hopefully the crash server will let it through. But it probably won't.
277 # Not sure what the best answer is in this case.
278 if file_size >= CRASH_SERVER_FILE_LIMIT:
279 logging.PrintBuildbotStepWarnings()
280 logging.warning('upload file %s is awfully large, risking rejection by '
281 'the symbol server (%s > %s)', symbol.display_path,
282 file_size, CRASH_SERVER_FILE_LIMIT)
283
284 return symbol
285
Don Garretta28be6d2016-06-16 18:09:35 -0700286
287def GetUploadTimeout(symbol):
288 """How long to wait for a specific file to upload to the crash server.
289
290 This is a function largely to make unittesting easier.
291
292 Args:
293 symbol: A SymbolFile instance.
294
295 Returns:
296 Timeout length (in seconds)
297 """
298 # Scale the timeout based on the filesize.
Mike Frysinger93e8ffa2019-07-03 20:24:18 -0400299 return max(symbol.FileSize() // UPLOAD_MIN_RATE, UPLOAD_MIN_TIMEOUT)
Don Garretta28be6d2016-06-16 18:09:35 -0700300
301
Mike Nichols90f7c152019-04-09 15:14:08 -0600302def ExecRequest(operator, url, timeout, api_key, **kwargs):
303 """Makes a web request with default timeout, returning the json result.
Don Garretta28be6d2016-06-16 18:09:35 -0700304
Mike Nichols90f7c152019-04-09 15:14:08 -0600305 This method will raise a requests.exceptions.HTTPError if the status
306 code is not 4XX, 5XX
307
308 Note: If you are using verbose logging it is entirely possible that the
309 subsystem will write your api key to the logs!
310
311 Args:
Mike Nichols649e6a82019-04-23 11:44:48 -0600312 operator: HTTP method.
313 url: Endpoint URL.
314 timeout: HTTP timeout for request.
315 api_key: Authentication key.
Mike Nichols90f7c152019-04-09 15:14:08 -0600316
317 Returns:
318 HTTP response content
319 """
320 resp = requests.request(operator, url,
321 params={'key': api_key},
322 headers={'User-agent': 'chromite.upload_symbols'},
323 timeout=timeout, **kwargs)
324 # Make sure we don't leak secret keys by accident.
325 if resp.status_code > 399:
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400326 resp.url = resp.url.replace(urllib.parse.quote(api_key), 'XX-HIDDEN-XX')
Mike Nicholscf5b7a92019-04-25 12:01:28 -0600327 logging.warning('Url: %s, Status: %s, response: "%s", in: %s',
328 resp.url, resp.status_code, resp.text, resp.elapsed)
329 elif resp.content:
Mike Nichols90f7c152019-04-09 15:14:08 -0600330 return resp.json()
Mike Nicholscf5b7a92019-04-25 12:01:28 -0600331
Mike Nichols90f7c152019-04-09 15:14:08 -0600332 return {}
333
334
Mike Nichols137e82d2019-05-15 18:40:34 -0600335def FindDuplicates(symbols, status_url, api_key, timeout=DEDUPE_TIMEOUT):
336 """Check whether the symbol files have already been uploaded.
Mike Nichols649e6a82019-04-23 11:44:48 -0600337
338 Args:
Mike Nichols137e82d2019-05-15 18:40:34 -0600339 symbols: A iterable of SymbolFiles to be uploaded
Mike Nichols649e6a82019-04-23 11:44:48 -0600340 status_url: The crash URL to validate the file existence.
Mike Nichols649e6a82019-04-23 11:44:48 -0600341 api_key: Authentication key.
Mike Nichols137e82d2019-05-15 18:40:34 -0600342 timeout: HTTP timeout for request.
Mike Nichols649e6a82019-04-23 11:44:48 -0600343
344 Yields:
Mike Nichols137e82d2019-05-15 18:40:34 -0600345 All SymbolFiles from symbols, but duplicates have status updated to
346 DUPLICATE.
Mike Nichols649e6a82019-04-23 11:44:48 -0600347 """
Mike Nichols137e82d2019-05-15 18:40:34 -0600348 for batch in BatchGenerator(symbols, DEDUPE_LIMIT):
349 items = []
350 result = {}
351 for x in batch:
352 items.append({'debug_file': x.header.name,
353 'debug_id': x.header.id.replace('-', '')})
354 symbol_data = {'symbol_ids': items}
355 try:
356 result = ExecRequest('post', '%s/symbols:checkStatuses' %
357 status_url,
358 timeout,
359 api_key=api_key,
360 data=json.dumps(symbol_data))
Mike Frysingerc7d164a2019-11-06 17:09:45 -0500361 except requests.exceptions.RequestException as e:
Mike Nichols137e82d2019-05-15 18:40:34 -0600362 logging.warning('could not identify duplicates: HTTP error: %s', e)
363 for b in batch:
364 b.status = SymbolFile.INITIAL
365 set_match = {'debugId': b.header.id.replace('-', ''),
366 'debugFile': b.header.name}
367 for cs_result in result.get('pairs', []):
Mike Frysingerca227ec2019-07-02 17:59:21 -0400368 if set_match == cs_result.get('symbolId'):
Mike Nichols137e82d2019-05-15 18:40:34 -0600369 if cs_result.get('status') == 'FOUND':
370 logging.debug('Found duplicate: %s', b.display_name)
371 b.status = SymbolFile.DUPLICATE
372 break
373 yield b
374
Mike Nichols649e6a82019-04-23 11:44:48 -0600375
Mike Nichols90f7c152019-04-09 15:14:08 -0600376def UploadSymbolFile(upload_url, symbol, api_key):
Mike Frysinger80de5012019-08-01 14:10:53 -0400377 """Upload a symbol file to the crash server, returning the status result.
Don Garretta28be6d2016-06-16 18:09:35 -0700378
379 Args:
380 upload_url: The crash URL to POST the |sym_file| to
381 symbol: A SymbolFile instance.
Mike Nichols90f7c152019-04-09 15:14:08 -0600382 api_key: Authentication key
Mike Frysinger80de5012019-08-01 14:10:53 -0400383 """
Mike Nichols90f7c152019-04-09 15:14:08 -0600384 timeout = GetUploadTimeout(symbol)
Mike Nichols137e82d2019-05-15 18:40:34 -0600385 upload = ExecRequest('post',
386 '%s/uploads:create' % upload_url, timeout, api_key)
Mike Nichols649e6a82019-04-23 11:44:48 -0600387
Mike Nichols137e82d2019-05-15 18:40:34 -0600388 if upload and 'uploadUrl' in upload.keys():
389 symbol_data = {'symbol_id':
390 {'debug_file': symbol.header.name,
391 'debug_id': symbol.header.id.replace('-', '')}
392 }
393 ExecRequest('put',
394 upload['uploadUrl'], timeout,
395 api_key=api_key,
396 data=open(symbol.file_name, 'r'))
397 ExecRequest('post',
398 '%s/uploads/%s:complete' % (
399 upload_url, upload['uploadKey']),
400 timeout, api_key=api_key,
401 # TODO(mikenichols): Validate product_name once it is added
402 # to the proto; currently unsupported.
403 data=json.dumps(symbol_data))
404 else:
405 raise requests.exceptions.HTTPError
Don Garretta28be6d2016-06-16 18:09:35 -0700406
407
Mike Nichols90f7c152019-04-09 15:14:08 -0600408def PerformSymbolsFileUpload(symbols, upload_url, api_key):
Don Garretta28be6d2016-06-16 18:09:35 -0700409 """Upload the symbols to the crash server
410
411 Args:
Don Garrettdeb2e032016-07-06 16:44:14 -0700412 symbols: An iterable of SymbolFiles to be uploaded.
Don Garretta28be6d2016-06-16 18:09:35 -0700413 upload_url: URL of crash server to upload too.
Mike Nichols90f7c152019-04-09 15:14:08 -0600414 api_key: Authentication key.
Don Garretta28be6d2016-06-16 18:09:35 -0700415 failures: Tracker for total upload failures.
Don Garretta28be6d2016-06-16 18:09:35 -0700416
Don Garrettdeb2e032016-07-06 16:44:14 -0700417 Yields:
418 Each symbol from symbols, perhaps modified.
Don Garretta28be6d2016-06-16 18:09:35 -0700419 """
Don Garrettdeb2e032016-07-06 16:44:14 -0700420 failures = 0
Don Garretta28be6d2016-06-16 18:09:35 -0700421
Don Garrettdeb2e032016-07-06 16:44:14 -0700422 for s in symbols:
423 if (failures < MAX_TOTAL_ERRORS_FOR_RETRY and
424 s.status in (SymbolFile.INITIAL, SymbolFile.ERROR)):
425 # Keeps us from DoS-ing the symbol server.
426 time.sleep(SLEEP_DELAY)
427 logging.info('Uploading symbol_file: %s', s.display_path)
428 try:
429 # This command retries the upload multiple times with growing delays. We
430 # only consider the upload a failure if these retries fail.
Don Garrette1f47e92016-10-13 16:04:56 -0700431 def ShouldRetryUpload(exception):
Mike Nichols0abb8d32019-04-26 14:55:08 -0600432 if isinstance(exception, (requests.exceptions.RequestException,
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400433 IOError,
Mike Nichols0abb8d32019-04-26 14:55:08 -0600434 httplib.HTTPException, socket.error)):
435 logging.info('Request failed, retrying: %s', exception)
436 return True
437 return False
Don Garrette1f47e92016-10-13 16:04:56 -0700438
Don Garrett440944e2016-10-03 16:33:31 -0700439 with cros_build_lib.TimedSection() as timer:
Don Garrette1f47e92016-10-13 16:04:56 -0700440 retry_stats.RetryWithStats(
441 UPLOAD_STATS, ShouldRetryUpload, MAX_RETRIES,
Don Garrett440944e2016-10-03 16:33:31 -0700442 UploadSymbolFile,
Mike Nichols90f7c152019-04-09 15:14:08 -0600443 upload_url, s, api_key,
Luigi Semenzato5104f3c2016-10-12 12:37:42 -0700444 sleep=INITIAL_RETRY_DELAY,
445 log_all_retries=True)
Mike Nicholsb03b2c62019-05-02 18:46:04 -0600446 if s.status != SymbolFile.DUPLICATE:
447 logging.info('upload of %10i bytes took %s', s.FileSize(),
448 timer.delta)
449 s.status = SymbolFile.UPLOADED
Mike Frysingerc7d164a2019-11-06 17:09:45 -0500450 except requests.exceptions.RequestException as e:
Mike Nichols90f7c152019-04-09 15:14:08 -0600451 logging.warning('could not upload: %s: HTTP error: %s',
452 s.display_name, e)
Don Garrettdeb2e032016-07-06 16:44:14 -0700453 s.status = SymbolFile.ERROR
454 failures += 1
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400455 except (httplib.HTTPException, IOError, socket.error) as e:
Ryo Hashimoto45273f02017-03-16 17:45:56 +0900456 logging.warning('could not upload: %s: %s %s', s.display_name,
457 type(e).__name__, e)
Don Garrettdeb2e032016-07-06 16:44:14 -0700458 s.status = SymbolFile.ERROR
459 failures += 1
460
461 # We pass the symbol along, on both success and failure.
462 yield s
Don Garretta28be6d2016-06-16 18:09:35 -0700463
464
Don Garrettdeb2e032016-07-06 16:44:14 -0700465def ReportResults(symbols, failed_list):
Don Garretta28be6d2016-06-16 18:09:35 -0700466 """Log a summary of the symbol uploading.
467
468 This has the side effect of fully consuming the symbols iterator.
469
470 Args:
471 symbols: An iterator of SymbolFiles to be uploaded.
Don Garretta28be6d2016-06-16 18:09:35 -0700472 failed_list: A filename at which to write out a list of our failed uploads.
Don Garrettdeb2e032016-07-06 16:44:14 -0700473
474 Returns:
475 The number of symbols not uploaded.
Don Garretta28be6d2016-06-16 18:09:35 -0700476 """
477 upload_failures = []
478 result_counts = {
479 SymbolFile.INITIAL: 0,
480 SymbolFile.UPLOADED: 0,
481 SymbolFile.DUPLICATE: 0,
Don Garrettdeb2e032016-07-06 16:44:14 -0700482 SymbolFile.ERROR: 0,
Don Garretta28be6d2016-06-16 18:09:35 -0700483 }
484
485 for s in symbols:
486 result_counts[s.status] += 1
Don Garrettdeb2e032016-07-06 16:44:14 -0700487 if s.status in [SymbolFile.INITIAL, SymbolFile.ERROR]:
Don Garretta28be6d2016-06-16 18:09:35 -0700488 upload_failures.append(s)
489
Don Garrette1f47e92016-10-13 16:04:56 -0700490 # Report retry numbers.
491 _, _, retries = retry_stats.CategoryStats(UPLOAD_STATS)
492 if retries:
493 logging.warning('%d upload retries performed.', retries)
494
Don Garretta28be6d2016-06-16 18:09:35 -0700495 logging.info('Uploaded %(uploaded)d, Skipped %(duplicate)d duplicates.',
496 result_counts)
497
Chris Ching91908032016-09-27 16:55:33 -0600498 if result_counts[SymbolFile.ERROR]:
Don Garretta28be6d2016-06-16 18:09:35 -0700499 logging.PrintBuildbotStepWarnings()
Chris Ching91908032016-09-27 16:55:33 -0600500 logging.warning('%d non-recoverable upload errors',
501 result_counts[SymbolFile.ERROR])
502
503 if result_counts[SymbolFile.INITIAL]:
504 logging.PrintBuildbotStepWarnings()
505 logging.warning('%d upload(s) were skipped because of excessive errors',
Don Garrettdeb2e032016-07-06 16:44:14 -0700506 result_counts[SymbolFile.INITIAL])
Don Garretta28be6d2016-06-16 18:09:35 -0700507
508 if failed_list is not None:
509 with open(failed_list, 'w') as fl:
510 for s in upload_failures:
511 fl.write('%s\n' % s.display_path)
512
Don Garrettdeb2e032016-07-06 16:44:14 -0700513 return result_counts[SymbolFile.INITIAL] + result_counts[SymbolFile.ERROR]
514
Don Garretta28be6d2016-06-16 18:09:35 -0700515
Mike Nichols649e6a82019-04-23 11:44:48 -0600516def UploadSymbols(sym_paths, upload_url, failed_list=None,
Mike Nichols137e82d2019-05-15 18:40:34 -0600517 upload_limit=None, strip_cfi=None, timeout=None,
Mike Nichols90f7c152019-04-09 15:14:08 -0600518 api_key=None):
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400519 """Upload all the generated symbols for |board| to the crash server
520
521 Args:
Mike Frysinger9b2ff5c2013-11-22 10:01:12 -0500522 sym_paths: Specific symbol files (or dirs of sym files) to upload,
523 otherwise search |breakpad_dir|
Don Garretta28be6d2016-06-16 18:09:35 -0700524 upload_url: URL of crash server to upload too.
Don Garretta28be6d2016-06-16 18:09:35 -0700525 failed_list: A filename at which to write out a list of our failed uploads.
526 upload_limit: Integer listing how many files to upload. None for no limit.
527 strip_cfi: File size at which we strip out CFI data. None for no limit.
Mike Nichols137e82d2019-05-15 18:40:34 -0600528 timeout: HTTP timeout for request.
Mike Nichols90f7c152019-04-09 15:14:08 -0600529 api_key: A string based authentication key
Mike Frysinger1a736a82013-12-12 01:50:59 -0500530
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400531 Returns:
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400532 The number of errors that were encountered.
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400533 """
Don Garrette1f47e92016-10-13 16:04:56 -0700534 retry_stats.SetupStats()
535
Don Garretta28be6d2016-06-16 18:09:35 -0700536 # Note: This method looks like each step of processing is performed
537 # sequentially for all SymbolFiles, but instead each step is a generator that
538 # produces the next iteration only when it's read. This means that (except for
539 # some batching) each SymbolFile goes through all of these steps before the
540 # next one is processed at all.
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400541
Don Garretta28be6d2016-06-16 18:09:35 -0700542 # This is used to hold striped
543 with osutils.TempDir(prefix='upload_symbols.') as tempdir:
544 symbols = FindSymbolFiles(tempdir, sym_paths)
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500545
Don Garrett1bc1e102016-07-06 17:06:10 -0700546 # Sort all of our symbols so the largest ones (probably the most important)
547 # are processed first.
548 symbols = list(symbols)
549 symbols.sort(key=lambda s: s.FileSize(), reverse=True)
550
Don Garretta28be6d2016-06-16 18:09:35 -0700551 if upload_limit is not None:
552 # Restrict symbols processed to the limit.
553 symbols = itertools.islice(symbols, None, upload_limit)
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400554
Don Garretta28be6d2016-06-16 18:09:35 -0700555 # Strip CFI, if needed.
556 symbols = (AdjustSymbolFileSize(s, tempdir, strip_cfi) for s in symbols)
Mike Frysinger8ec8c502014-02-10 00:19:13 -0500557
Mike Nichols137e82d2019-05-15 18:40:34 -0600558 # Find duplicates via batch API
559 symbols = FindDuplicates(symbols, upload_url, api_key, timeout)
560
Don Garretta28be6d2016-06-16 18:09:35 -0700561 # Perform uploads
Mike Nichols90f7c152019-04-09 15:14:08 -0600562 symbols = PerformSymbolsFileUpload(symbols, upload_url, api_key)
Mike Frysingerd42e5f02014-03-14 11:19:37 -0400563
Don Garretta28be6d2016-06-16 18:09:35 -0700564 # Log the final results, and consume the symbols generator fully.
Don Garrettdeb2e032016-07-06 16:44:14 -0700565 failures = ReportResults(symbols, failed_list)
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500566
Don Garrettdeb2e032016-07-06 16:44:14 -0700567 return failures
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400568
569
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400570def main(argv):
571 parser = commandline.ArgumentParser(description=__doc__)
572
Don Garretta28be6d2016-06-16 18:09:35 -0700573 # TODO: Make sym_paths, breakpad_root, and root exclusive.
574
Mike Frysingerd41938e2014-02-10 06:37:55 -0500575 parser.add_argument('sym_paths', type='path_or_uri', nargs='*', default=None,
576 help='symbol file or directory or URL or tarball')
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400577 parser.add_argument('--board', default=None,
Don Garrett747cc4b2015-10-07 14:48:48 -0700578 help='Used to find default breakpad_root.')
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400579 parser.add_argument('--breakpad_root', type='path', default=None,
Mike Frysingerc5597f22014-11-27 15:39:15 -0500580 help='full path to the breakpad symbol directory')
581 parser.add_argument('--root', type='path', default=None,
582 help='full path to the chroot dir')
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400583 parser.add_argument('--official_build', action='store_true', default=False,
584 help='point to official symbol server')
Mike Frysinger38647542014-09-12 18:15:39 -0700585 parser.add_argument('--server', type=str, default=None,
586 help='URI for custom symbol server')
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400587 parser.add_argument('--regenerate', action='store_true', default=False,
588 help='regenerate all symbols')
Don Garretta28be6d2016-06-16 18:09:35 -0700589 parser.add_argument('--upload-limit', type=int,
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400590 help='only upload # number of symbols')
591 parser.add_argument('--strip_cfi', type=int,
Don Garretta28be6d2016-06-16 18:09:35 -0700592 default=DEFAULT_FILE_LIMIT,
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400593 help='strip CFI data for files above this size')
Mike Frysinger7f9be142014-01-15 02:16:42 -0500594 parser.add_argument('--failed-list', type='path',
595 help='where to save a list of failed symbols')
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500596 parser.add_argument('--dedupe', action='store_true', default=False,
597 help='use the swarming service to avoid re-uploading')
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400598 parser.add_argument('--yes', action='store_true', default=False,
599 help='answer yes to all prompts')
Don Garrett747cc4b2015-10-07 14:48:48 -0700600 parser.add_argument('--product_name', type=str, default='ChromeOS',
601 help='Produce Name for breakpad stats.')
Mike Nichols90f7c152019-04-09 15:14:08 -0600602 parser.add_argument('--api_key', type=str, default=None,
603 help='full path to the API key file')
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400604
605 opts = parser.parse_args(argv)
Mike Frysinger90e49ca2014-01-14 14:42:07 -0500606 opts.Freeze()
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400607
Don Garretta28be6d2016-06-16 18:09:35 -0700608 # Figure out the symbol files/directories to upload.
609 if opts.sym_paths:
610 sym_paths = opts.sym_paths
611 elif opts.breakpad_root:
612 sym_paths = [opts.breakpad_root]
613 elif opts.root:
614 if not opts.board:
Mike Frysinger8390dec2017-08-11 15:11:38 -0400615 cros_build_lib.Die('--board must be set if --root is used.')
Don Garretta28be6d2016-06-16 18:09:35 -0700616 breakpad_dir = cros_generate_breakpad_symbols.FindBreakpadDir(opts.board)
617 sym_paths = [os.path.join(opts.root, breakpad_dir.lstrip('/'))]
618 else:
Mike Frysinger8390dec2017-08-11 15:11:38 -0400619 cros_build_lib.Die('--sym_paths, --breakpad_root, or --root must be set.')
Don Garretta28be6d2016-06-16 18:09:35 -0700620
Don Garrett747cc4b2015-10-07 14:48:48 -0700621 if opts.sym_paths or opts.breakpad_root:
Mike Frysinger9dcf9ae2013-08-10 15:17:09 -0400622 if opts.regenerate:
Don Garrett747cc4b2015-10-07 14:48:48 -0700623 cros_build_lib.Die('--regenerate may not be used with specific files, '
624 'or breakpad_root')
Mike Frysinger9dcf9ae2013-08-10 15:17:09 -0400625 else:
626 if opts.board is None:
627 cros_build_lib.Die('--board is required')
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400628
Don Garretta28be6d2016-06-16 18:09:35 -0700629 # Figure out which crash server to upload too.
630 upload_url = opts.server
631 if not upload_url:
632 if opts.official_build:
633 upload_url = OFFICIAL_UPLOAD_URL
634 else:
635 logging.warning('unofficial builds upload to the staging server')
636 upload_url = STAGING_UPLOAD_URL
637
Mike Nichols90f7c152019-04-09 15:14:08 -0600638 # Set up the API key needed to authenticate to Crash server.
639 # Allow for a local key file for testing purposes.
640 if opts.api_key:
641 api_key_file = opts.api_key
642 else:
643 api_key_file = constants.CRASH_API_KEY
644
645 api_key = osutils.ReadFile(api_key_file)
646
Don Garretta28be6d2016-06-16 18:09:35 -0700647 # Confirm we really want the long upload.
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400648 if not opts.yes:
Mike Frysingerc5de9602014-02-09 02:42:36 -0500649 prolog = '\n'.join(textwrap.wrap(textwrap.dedent("""
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400650 Uploading symbols for an entire Chromium OS build is really only
651 necessary for release builds and in a few cases for developers
652 to debug problems. It will take considerable time to run. For
653 developer debugging purposes, consider instead passing specific
654 files to upload.
Mike Frysingerc5de9602014-02-09 02:42:36 -0500655 """), 80)).strip()
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400656 if not cros_build_lib.BooleanPrompt(
657 prompt='Are you sure you want to upload all build symbols',
Mike Frysingerc5de9602014-02-09 02:42:36 -0500658 default=False, prolog=prolog):
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400659 cros_build_lib.Die('better safe than sorry')
660
661 ret = 0
Don Garretta28be6d2016-06-16 18:09:35 -0700662
663 # Regenerate symbols from binaries.
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400664 if opts.regenerate:
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400665 ret += cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
666 opts.board, breakpad_dir=opts.breakpad_root)
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400667
Don Garretta28be6d2016-06-16 18:09:35 -0700668 # Do the upload.
669 ret += UploadSymbols(
670 sym_paths=sym_paths,
671 upload_url=upload_url,
Don Garretta28be6d2016-06-16 18:09:35 -0700672 failed_list=opts.failed_list,
673 upload_limit=opts.upload_limit,
Mike Nichols90f7c152019-04-09 15:14:08 -0600674 strip_cfi=opts.strip_cfi,
675 api_key=api_key)
Don Garretta28be6d2016-06-16 18:09:35 -0700676
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400677 if ret:
Ralph Nathan59900422015-03-24 10:41:17 -0700678 logging.error('encountered %i problem(s)', ret)
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400679 # Since exit(status) gets masked, clamp it to 1 so we don't inadvertently
680 # return 0 in case we are a multiple of the mask.
Don Garretta28be6d2016-06-16 18:09:35 -0700681 return 1