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