Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Breakpad for Python. | 5 """Breakpad for Python. |
| 6 | 6 |
| 7 Sends a notification when a process stops on an exception. | 7 Sends a notification when a process stops on an exception. |
| 8 | 8 |
| 9 It is only enabled when all these conditions are met: | 9 It is only enabled when all these conditions are met: |
| 10 1. hostname finishes with '.google.com' | 10 1. hostname finishes with '.google.com' |
| 11 2. main module name doesn't contain the word 'test' | 11 2. main module name doesn't contain the word 'test' |
| 12 3. no NO_BREAKPAD environment variable is defined | 12 3. no NO_BREAKPAD environment variable is defined |
| 13 """ | 13 """ |
| 14 | 14 |
| 15 import atexit | 15 import atexit |
| 16 import time | |
| 16 import getpass | 17 import getpass |
| 17 import os | 18 import os |
| 18 import urllib | 19 import urllib |
| 20 import urllib2 | |
| 19 import traceback | 21 import traceback |
| 20 import socket | 22 import socket |
| 21 import sys | 23 import sys |
|
cmp
2011/04/28 02:00:24
reorder these alphabetically
| |
| 22 | 24 |
| 23 | 25 |
| 24 # Configure these values. | 26 # Configure these values. |
| 25 DEFAULT_URL = 'https://chromium-status.appspot.com/breakpad' | 27 DEFAULT_URL = 'https://chromium-status.appspot.com' |
| 26 | 28 |
| 27 _REGISTERED = False | 29 _REGISTERED = False |
| 28 | 30 |
| 31 _TIME_STARTED = time.time() | |
| 32 | |
| 33 | |
| 34 def post(url, params): | |
| 35 """HTTP POST with timeout when it's supported.""" | |
| 36 kwargs = {} | |
| 37 if (sys.version_info[0] * 10 + sys.version_info[1]) >= 26: | |
|
cmp
2011/04/28 02:00:24
remove extra space after )
| |
| 38 kwargs['timeout'] = 4 | |
| 39 request = urllib2.urlopen(url, urllib.urlencode(params), **kwargs) | |
| 40 out = request.read() | |
| 41 request.close() | |
| 42 return out | |
| 43 | |
| 29 | 44 |
| 30 def FormatException(e): | 45 def FormatException(e): |
| 31 """Returns a human readable form of an exception. | 46 """Returns a human readable form of an exception. |
| 32 | 47 |
| 33 Adds the maximum number of interesting information in the safest way.""" | 48 Adds the maximum number of interesting information in the safest way.""" |
| 34 try: | 49 try: |
| 35 out = repr(e) | 50 out = repr(e) |
| 36 except Exception: | 51 except Exception: |
| 37 out = '' | 52 out = '' |
| 38 try: | 53 try: |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 51 if hasattr(e, 'info') and callable(e.info): | 66 if hasattr(e, 'info') and callable(e.info): |
| 52 out += '\ninfo(): %s' % e.info() | 67 out += '\ninfo(): %s' % e.info() |
| 53 except Exception: | 68 except Exception: |
| 54 pass | 69 pass |
| 55 return out | 70 return out |
| 56 | 71 |
| 57 | 72 |
| 58 def SendStack(last_tb, stack, url=None, maxlen=50): | 73 def SendStack(last_tb, stack, url=None, maxlen=50): |
| 59 """Sends the stack trace to the breakpad server.""" | 74 """Sends the stack trace to the breakpad server.""" |
| 60 if not url: | 75 if not url: |
| 61 url = DEFAULT_URL | 76 url = DEFAULT_URL + '/breakpad' |
| 62 print 'Sending crash report ...' | 77 print 'Sending crash report ...' |
| 63 try: | 78 try: |
| 64 params = { | 79 params = { |
| 65 'args': sys.argv, | 80 'args': sys.argv, |
| 66 'stack': stack[0:4096], | 81 'stack': stack[0:4096], |
| 67 'user': getpass.getuser(), | 82 'user': getpass.getuser(), |
| 68 'exception': FormatException(last_tb), | 83 'exception': FormatException(last_tb), |
| 69 'host': socket.getfqdn(), | 84 'host': socket.getfqdn(), |
| 70 'cwd': os.getcwd(), | 85 'cwd': os.getcwd(), |
| 71 'version': sys.version, | 86 'version': sys.version, |
| 72 } | 87 } |
| 73 # pylint: disable=W0702 | 88 # pylint: disable=W0702 |
| 74 print('\n'.join(' %s: %s' % (k, v[0:maxlen]) | 89 print('\n'.join(' %s: %s' % (k, v[0:maxlen]) |
| 75 for k, v in params.iteritems())) | 90 for k, v in params.iteritems())) |
| 76 request = urllib.urlopen(url, urllib.urlencode(params)) | 91 print(post(url, params)) |
| 77 print(request.read()) | |
| 78 request.close() | |
| 79 except IOError: | 92 except IOError: |
| 80 print('There was a failure while trying to send the stack trace. Too bad.') | 93 print('There was a failure while trying to send the stack trace. Too bad.') |
| 81 | 94 |
| 82 | 95 |
| 96 def SendProfiling(url=None): | |
| 97 try: | |
| 98 if not url: | |
| 99 url = DEFAULT_URL + '/profiling' | |
| 100 params = { | |
| 101 'argv': ' '.join(sys.argv), | |
| 102 'platform': sys.platform, | |
| 103 'duration': time.time() - _TIME_STARTED, | |
|
cmp
2011/04/28 02:00:24
move line 103 above line 102
| |
| 104 } | |
| 105 post(url, params) | |
| 106 except IOError: | |
| 107 pass | |
| 108 | |
| 109 | |
| 83 def CheckForException(): | 110 def CheckForException(): |
| 84 """Runs at exit. Look if there was an exception active.""" | 111 """Runs at exit. Look if there was an exception active.""" |
| 85 last_value = getattr(sys, 'last_value', None) | 112 last_value = getattr(sys, 'last_value', None) |
| 86 if last_value and not isinstance(last_value, KeyboardInterrupt): | 113 if last_value: |
| 87 last_tb = getattr(sys, 'last_traceback', None) | 114 if not isinstance(last_value, KeyboardInterrupt): |
| 88 if last_tb: | 115 last_tb = getattr(sys, 'last_traceback', None) |
| 89 SendStack(last_value, ''.join(traceback.format_tb(last_tb))) | 116 if last_tb: |
| 117 SendStack(last_value, ''.join(traceback.format_tb(last_tb))) | |
| 118 else: | |
| 119 SendProfiling() | |
| 90 | 120 |
| 91 | 121 |
| 92 def Register(): | 122 def Register(): |
| 93 """Registers the callback at exit. Calling it multiple times is no-op.""" | 123 """Registers the callback at exit. Calling it multiple times is no-op.""" |
| 94 global _REGISTERED | 124 global _REGISTERED |
| 95 if _REGISTERED: | 125 if _REGISTERED: |
| 96 return | 126 return |
| 97 _REGISTERED = True | 127 _REGISTERED = True |
| 98 atexit.register(CheckForException) | 128 atexit.register(CheckForException) |
| 99 | 129 |
| 100 | 130 |
| 101 # Skip unit tests and we don't want anything from non-googler. | 131 # Skip unit tests and we don't want anything from non-googler. |
| 102 if (not 'test' in sys.modules['__main__'].__file__ and | 132 if (not 'test' in sys.modules['__main__'].__file__ and |
| 103 not 'NO_BREAKPAD' in os.environ and | 133 not 'NO_BREAKPAD' in os.environ and |
| 104 (socket.getfqdn().endswith('.google.com') or | 134 (socket.getfqdn().endswith('.google.com') or |
| 105 socket.getfqdn().endswith('.chromium.org'))): | 135 socket.getfqdn().endswith('.chromium.org'))): |
| 106 Register() | 136 Register() |
| 107 | 137 |
| 108 # Uncomment this line if you want to test it out. | 138 # Uncomment this line if you want to test it out. |
| 109 #Register() | 139 #Register() |
| OLD | NEW |