blob: 2248e35c71837d85608f5615e1649cf6fea907fd [file] [log] [blame]
[email protected]18878422012-01-10 16:45:371# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ada4c652009-12-03 15:32:012# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Breakpad for Python.
6
[email protected]fa877022010-09-09 20:01:097Sends a notification when a process stops on an exception.
8
9It is only enabled when all these conditions are met:
[email protected]28924252011-04-30 14:23:4810 1. hostname finishes with '.google.com' or 'chromium.org'
[email protected]fa877022010-09-09 20:01:0911 2. main module name doesn't contain the word 'test'
12 3. no NO_BREAKPAD environment variable is defined
13"""
[email protected]ada4c652009-12-03 15:32:0114
15import atexit
16import getpass
[email protected]fa877022010-09-09 20:01:0917import os
[email protected]7aca9e82009-12-03 19:06:0918import socket
[email protected]ada4c652009-12-03 15:32:0119import sys
[email protected]7ca049b2011-04-29 12:58:2320import time
21import traceback
22import urllib
23import urllib2
[email protected]ada4c652009-12-03 15:32:0124
[email protected]ce921b22010-05-28 01:44:5825
[email protected]7aeac3e2010-05-13 19:53:2526# Configure these values.
[email protected]7ca049b2011-04-29 12:58:2327DEFAULT_URL = 'https://chromium-status.appspot.com'
[email protected]ce921b22010-05-28 01:44:5828
[email protected]28924252011-04-30 14:23:4829# Global variable to prevent double registration.
[email protected]ce921b22010-05-28 01:44:5830_REGISTERED = False
31
[email protected]7ca049b2011-04-29 12:58:2332_TIME_STARTED = time.time()
33
[email protected]28924252011-04-30 14:23:4834_HOST_NAME = socket.getfqdn()
35
[email protected]18878422012-01-10 16:45:3736# Skip unit tests and we don't want anything from non-googler.
37IS_ENABLED = (
38 not 'test' in getattr(sys.modules['__main__'], '__file__', '') and
39 not 'NO_BREAKPAD' in os.environ and
40 _HOST_NAME.endswith(('.google.com', '.chromium.org')))
41
[email protected]7ca049b2011-04-29 12:58:2342
43def post(url, params):
44 """HTTP POST with timeout when it's supported."""
[email protected]2e72bb12012-01-17 15:18:3545 if not IS_ENABLED:
46 # Make sure to not send anything for non googler.
47 return
[email protected]7ca049b2011-04-29 12:58:2348 kwargs = {}
49 if (sys.version_info[0] * 10 + sys.version_info[1]) >= 26:
50 kwargs['timeout'] = 4
[email protected]2e72bb12012-01-17 15:18:3551 try:
52 request = urllib2.urlopen(url, urllib.urlencode(params), **kwargs)
53 out = request.read()
54 request.close()
55 return out
56 except IOError:
57 return 'There was a failure while trying to send the stack trace. Too bad.'
[email protected]7ca049b2011-04-29 12:58:2358
[email protected]ada4c652009-12-03 15:32:0159
[email protected]1bd1ed32010-11-24 20:50:5660def FormatException(e):
61 """Returns a human readable form of an exception.
62
63 Adds the maximum number of interesting information in the safest way."""
64 try:
65 out = repr(e)
66 except Exception:
67 out = ''
68 try:
69 out = str(e)
70 if isinstance(e, Exception):
71 # urllib exceptions, usually the HTTP headers.
72 if hasattr(e, 'headers'):
73 out += '\nHeaders: %s' % e.headers
74 if hasattr(e, 'url'):
75 out += '\nUrl: %s' % e.url
76 if hasattr(e, 'msg'):
77 out += '\nMsg: %s' % e.msg
78 # The web page in some urllib exceptions.
79 if hasattr(e, 'read') and callable(e.read):
80 out += '\nread(): %s' % e.read()
81 if hasattr(e, 'info') and callable(e.info):
82 out += '\ninfo(): %s' % e.info()
83 except Exception:
84 pass
85 return out
86
87
[email protected]2e72bb12012-01-17 15:18:3588def SendStack(last_tb, stack, url=None, maxlen=50, verbose=True):
[email protected]ce921b22010-05-28 01:44:5889 """Sends the stack trace to the breakpad server."""
[email protected]18878422012-01-10 16:45:3790 if not IS_ENABLED:
[email protected]18878422012-01-10 16:45:3791 return
[email protected]2e72bb12012-01-17 15:18:3592 def p(o):
93 if verbose:
94 print(o)
95 p('Sending crash report ...')
96 params = {
97 'args': sys.argv,
98 'cwd': os.getcwd(),
99 'exception': FormatException(last_tb),
100 'host': _HOST_NAME,
101 'stack': stack[0:4096],
102 'user': getpass.getuser(),
103 'version': sys.version,
104 }
105 p('\n'.join(' %s: %s' % (k, params[k][0:maxlen]) for k in sorted(params)))
106 p(post(url or DEFAULT_URL + '/breakpad', params))
[email protected]7aca9e82009-12-03 19:06:09107
108
[email protected]43b99102012-11-28 19:08:14109def SendProfiling(duration, url=None):
[email protected]2e72bb12012-01-17 15:18:35110 params = {
111 'argv': ' '.join(sys.argv),
112 # Strip the hostname.
113 'domain': _HOST_NAME.split('.', 1)[-1],
[email protected]43b99102012-11-28 19:08:14114 'duration': duration,
[email protected]2e72bb12012-01-17 15:18:35115 'platform': sys.platform,
116 }
117 post(url or DEFAULT_URL + '/profiling', params)
[email protected]7ca049b2011-04-29 12:58:23118
119
[email protected]7aca9e82009-12-03 19:06:09120def CheckForException():
[email protected]ce921b22010-05-28 01:44:58121 """Runs at exit. Look if there was an exception active."""
[email protected]7aeac3e2010-05-13 19:53:25122 last_value = getattr(sys, 'last_value', None)
[email protected]7ca049b2011-04-29 12:58:23123 if last_value:
124 if not isinstance(last_value, KeyboardInterrupt):
125 last_tb = getattr(sys, 'last_traceback', None)
126 if last_tb:
127 SendStack(last_value, ''.join(traceback.format_tb(last_tb)))
128 else:
[email protected]43b99102012-11-28 19:08:14129 duration = time.time() - _TIME_STARTED
130 if duration > 90:
131 SendProfiling(duration)
[email protected]7aca9e82009-12-03 19:06:09132
133
[email protected]ce921b22010-05-28 01:44:58134def Register():
135 """Registers the callback at exit. Calling it multiple times is no-op."""
136 global _REGISTERED
137 if _REGISTERED:
138 return
139 _REGISTERED = True
140 atexit.register(CheckForException)
141
142
[email protected]18878422012-01-10 16:45:37143if IS_ENABLED:
[email protected]ce921b22010-05-28 01:44:58144 Register()
145
146# Uncomment this line if you want to test it out.
147#Register()