~bzr-pqm/bzr/bzr.dev

5752.2.9 by John Arbash Meinel
Clean up an accidental module change
1
# Copyright (C) 2009-2011 Canonical Ltd
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
18
"""Handling and reporting crashes.
4634.128.17 by Martin Pool
Add a docstring about the general approach to apport
19
20
A crash is an exception propagated up almost to the top level of Bazaar.
21
22
If we have apport <https://launchpad.net/apport/>, we store a report of the
5448.2.1 by Martin
Fix some "its" vs. "it's" spelling confusion in bzrlib code... also, ahem, a name in the NEWS file
23
crash using apport into its /var/crash spool directory, from where the user
4634.128.17 by Martin Pool
Add a docstring about the general approach to apport
24
can either manually send it to Launchpad.  In some cases (at least Ubuntu
25
development releases), Apport may pop up a window asking if they want
26
to send it.
27
28
Without apport, we just write a crash report to stderr and the user can report
29
this manually if the wish.
30
31
We never send crash data across the network without user opt-in.
32
33
In principle apport can run on any platform though as of Feb 2010 there seem
34
to be some portability bugs.
4634.128.21 by Martin Pool
Better check for APPORT_DISABLE; review tweaks from Robert
35
5129.2.1 by Gary van der Merwe
Fix spelling of APPORT_DISABLE in crash doc string.
36
To force this off in bzr turn set APPORT_DISABLE in the environment or 
4634.128.21 by Martin Pool
Better check for APPORT_DISABLE; review tweaks from Robert
37
-Dno_apport.
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
38
"""
39
4584.3.17 by Martin Pool
Better message in apport crash
40
# for interactive testing, try the 'bzr assert-fail' command 
41
# or see http://code.launchpad.net/~mbp/bzr/bzr-fail
4634.128.11 by Martin Pool
comment
42
#
43
# to test with apport it's useful to set
44
# export APPORT_IGNORE_OBSOLETE_PACKAGES=1
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
45
46
import os
4584.3.30 by Martin Pool
Fix call to platform() when apport not present
47
import platform
4584.3.13 by Martin Pool
Refactor _format_plugin_list and include list of loaded modules in apport
48
import pprint
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
49
import sys
50
import time
4584.3.22 by Martin Pool
further tweaks to and tests of bzr apport reporting
51
from StringIO import StringIO
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
52
53
import bzrlib
54
from bzrlib import (
55
    config,
4584.3.16 by Martin Pool
Add -Dno_apport and fallback if apport fails
56
    debug,
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
57
    osutils,
58
    plugin,
59
    trace,
60
    )
61
62
63
def report_bug(exc_info, stderr):
4634.128.21 by Martin Pool
Better check for APPORT_DISABLE; review tweaks from Robert
64
    if ('no_apport' in debug.debug_flags) or \
65
        os.environ.get('APPORT_DISABLE', None):
4634.128.19 by Martin Pool
Add APPORT_DISABLE to turn it off
66
        return report_bug_legacy(exc_info, stderr)
67
    try:
4634.128.21 by Martin Pool
Better check for APPORT_DISABLE; review tweaks from Robert
68
        if report_bug_to_apport(exc_info, stderr):
69
            # wrote a file; if None then report the old way
70
            return
4634.128.19 by Martin Pool
Add APPORT_DISABLE to turn it off
71
    except ImportError, e:
72
        trace.mutter("couldn't find apport bug-reporting library: %s" % e)
73
    except Exception, e:
74
        # this should only happen if apport is installed but it didn't
75
        # work, eg because of an io error writing the crash file
6220.2.4 by Jonathan Riddell
don't format apport error now it's going to bzr.log
76
        trace.mutter("bzr: failed to report crash using apport: %r" % e)
6220.2.6 by Jonathan Riddell
trace.log_exception_quietly() nicer
77
        trace.log_exception_quietly()
4634.128.19 by Martin Pool
Add APPORT_DISABLE to turn it off
78
    return report_bug_legacy(exc_info, stderr)
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
79
80
81
def report_bug_legacy(exc_info, err_file):
82
    """Report a bug by just printing a message to the user."""
83
    trace.print_exception(exc_info, err_file)
84
    err_file.write('\n')
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
85
    import textwrap
86
    def print_wrapped(l):
87
        err_file.write(textwrap.fill(l,
88
            width=78, subsequent_indent='    ') + '\n')
89
    print_wrapped('bzr %s on python %s (%s)\n' % \
90
        (bzrlib.__version__,
91
        bzrlib._format_version_tuple(sys.version_info),
92
        platform.platform(aliased=1)))
93
    print_wrapped('arguments: %r\n' % sys.argv)
94
    print_wrapped(textwrap.fill(
95
        'plugins: ' + plugin.format_concise_plugin_list(),
96
        width=78,
97
        subsequent_indent='    ',
98
        ) + '\n')
99
    print_wrapped(
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
100
        'encoding: %r, fsenc: %r, lang: %r\n' % (
101
            osutils.get_user_encoding(), sys.getfilesystemencoding(),
102
            os.environ.get('LANG')))
5609.23.3 by Martin Pool
Don't print full list of plugins in non-apport crash
103
    # We used to show all the plugins here, but it's too verbose.
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
104
    err_file.write(
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
105
        "\n"
4584.3.19 by Martin Pool
Tweak crash message and use the same one with apport or without.
106
        "*** Bazaar has encountered an internal error.  This probably indicates a\n"
4584.3.22 by Martin Pool
further tweaks to and tests of bzr apport reporting
107
        "    bug in Bazaar.  You can help us fix it by filing a bug report at\n"
108
        "        https://bugs.launchpad.net/bzr/+filebug\n"
109
        "    including this traceback and a description of the problem.\n"
4584.3.19 by Martin Pool
Tweak crash message and use the same one with apport or without.
110
        )
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
111
112
113
def report_bug_to_apport(exc_info, stderr):
114
    """Report a bug to apport for optional automatic filing.
4634.128.21 by Martin Pool
Better check for APPORT_DISABLE; review tweaks from Robert
115
116
    :returns: The name of the crash file, or None if we didn't write one.
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
117
    """
4634.128.6 by Martin Pool
Don't write crash reports if apport is configured to ignore them
118
    # this function is based on apport_package_hook.py, but omitting some of the
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
119
    # Ubuntu-specific policy about what to report and when
4584.3.25 by Martin Pool
Better handling of ImportError from apport
120
5904.1.2 by Martin Pool
Various pyflakes import fixes.
121
    # This import is apparently not used, but we're doing it so that if the
122
    # import fails, the exception will be caught at a higher level and we'll
123
    # report the error by other means.
4584.3.25 by Martin Pool
Better handling of ImportError from apport
124
    import apport
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
125
4634.128.6 by Martin Pool
Don't write crash reports if apport is configured to ignore them
126
    crash_filename = _write_apport_report_to_file(exc_info)
4584.3.21 by Martin Pool
Start adding tests for apport
127
4634.128.6 by Martin Pool
Don't write crash reports if apport is configured to ignore them
128
    if crash_filename is None:
129
        stderr.write("\n"
130
            "apport is set to ignore crashes in this version of bzr.\n"
131
            )
132
    else:
4634.128.21 by Martin Pool
Better check for APPORT_DISABLE; review tweaks from Robert
133
        trace.print_exception(exc_info, stderr)
4634.128.6 by Martin Pool
Don't write crash reports if apport is configured to ignore them
134
        stderr.write("\n"
135
            "You can report this problem to Bazaar's developers by running\n"
136
            "    apport-bug %s\n"
137
            "if a bug-reporting window does not automatically appear.\n"
138
            % (crash_filename))
4634.128.7 by Martin Pool
comment
139
        # XXX: on Windows, Mac, and other platforms where we might have the
140
        # apport libraries but not have an apport always running, we could
141
        # synchronously file now
4634.128.6 by Martin Pool
Don't write crash reports if apport is configured to ignore them
142
4634.128.18 by Martin Pool
Update apport crash tests
143
    return crash_filename
144
4634.128.6 by Martin Pool
Don't write crash reports if apport is configured to ignore them
145
146
def _write_apport_report_to_file(exc_info):
4584.3.22 by Martin Pool
further tweaks to and tests of bzr apport reporting
147
    import traceback
4584.3.21 by Martin Pool
Start adding tests for apport
148
    from apport.report import Report
4584.3.22 by Martin Pool
further tweaks to and tests of bzr apport reporting
149
150
    exc_type, exc_object, exc_tb = exc_info
151
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
152
    pr = Report()
5616.2.1 by Martin Pool
Don't put ProcMaps in apport crash reports.
153
    # add_proc_info sets the ExecutablePath, InterpreterPath, etc.
4797.33.9 by Martin Pool
Call apport add_proc_info to make sure necessary fields are present
154
    pr.add_proc_info()
5616.2.1 by Martin Pool
Don't put ProcMaps in apport crash reports.
155
    # It also adds ProcMaps which for us is rarely useful and mostly noise, so
156
    # let's remove it.
157
    del pr['ProcMaps']
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
158
    pr.add_user_info()
4634.128.4 by Martin Pool
Prepopulate apport Package and SourcePackage
159
160
    # Package and SourcePackage are needed so that apport will report about even
161
    # non-packaged versions of bzr; also this reports on their packaged
162
    # dependencies which is useful.
163
    pr['SourcePackage'] = 'bzr'
164
    pr['Package'] = 'bzr'
165
4584.3.22 by Martin Pool
further tweaks to and tests of bzr apport reporting
166
    pr['CommandLine'] = pprint.pformat(sys.argv)
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
167
    pr['BzrVersion'] = bzrlib.__version__
168
    pr['PythonVersion'] = bzrlib._format_version_tuple(sys.version_info)
169
    pr['Platform'] = platform.platform(aliased=1)
170
    pr['UserEncoding'] = osutils.get_user_encoding()
171
    pr['FileSystemEncoding'] = sys.getfilesystemencoding()
172
    pr['Locale'] = os.environ.get('LANG')
4584.3.13 by Martin Pool
Refactor _format_plugin_list and include list of loaded modules in apport
173
    pr['BzrPlugins'] = _format_plugin_list()
174
    pr['PythonLoadedModules'] = _format_module_list()
4584.3.22 by Martin Pool
further tweaks to and tests of bzr apport reporting
175
    pr['BzrDebugFlags'] = pprint.pformat(debug.debug_flags)
176
4797.33.10 by Martin Pool
Hardcode Package and SourcePackage into apport report
177
    # actually we'd rather file directly against the upstream product, but
178
    # apport does seem to count on there being one in there; we might need to
179
    # redirect it elsewhere anyhow
180
    pr['SourcePackage'] = 'bzr'
181
    pr['Package'] = 'bzr'
182
4797.33.15 by Martin Pool
Put CrashDb=bzr directly into the crash file
183
    # tell apport to file directly against the bzr package using 
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
184
    # <https://bugs.launchpad.net/bzr/+bug/391015>
4797.33.15 by Martin Pool
Put CrashDb=bzr directly into the crash file
185
    #
186
    # XXX: unfortunately apport may crash later if the crashdb definition
187
    # file isn't present
188
    pr['CrashDb'] = 'bzr'
4797.33.10 by Martin Pool
Hardcode Package and SourcePackage into apport report
189
4584.3.22 by Martin Pool
further tweaks to and tests of bzr apport reporting
190
    tb_file = StringIO()
191
    traceback.print_exception(exc_type, exc_object, exc_tb, file=tb_file)
192
    pr['Traceback'] = tb_file.getvalue()
193
4634.128.10 by Martin Pool
Attach tail of .bzr.log to crash reports
194
    _attach_log_tail(pr)
195
4634.128.9 by Martin Pool
comment
196
    # We want to use the 'bzr' crashdb so that it gets sent directly upstream,
197
    # which is a reasonable default for most internal errors.  However, if we
198
    # set it here then apport will crash later if it doesn't know about that
199
    # crashdb.  Instead, we rely on the bzr package installing both a
200
    # source hook telling crashes to go to this crashdb, and a crashdb
201
    # configuration describing it.
202
4634.128.10 by Martin Pool
Attach tail of .bzr.log to crash reports
203
    # these may contain some sensitive info (smtp_passwords)
204
    # TODO: strip that out and attach the rest
205
    #
206
    #attach_file_if_exists(report,
4999.3.2 by Martin Pool
expand tabs
207
    #   os.path.join(dot_bzr, 'bazaar.conf', 'BzrConfig')
4634.128.10 by Martin Pool
Attach tail of .bzr.log to crash reports
208
    #attach_file_if_exists(report,
4999.3.2 by Martin Pool
expand tabs
209
    #   os.path.join(dot_bzr, 'locations.conf', 'BzrLocations')
4634.128.10 by Martin Pool
Attach tail of .bzr.log to crash reports
210
    
4634.128.8 by Martin Pool
Anonymize apport report
211
    # strip username, hostname, etc
212
    pr.anonymize()
213
4634.128.6 by Martin Pool
Don't write crash reports if apport is configured to ignore them
214
    if pr.check_ignored():
215
        # eg configured off in ~/.apport-ignore.xml
216
        return None
217
    else:
4634.128.18 by Martin Pool
Update apport crash tests
218
        crash_file_name, crash_file = _open_crash_file()
4634.128.6 by Martin Pool
Don't write crash reports if apport is configured to ignore them
219
        pr.write(crash_file)
220
        crash_file.close()
4634.128.18 by Martin Pool
Update apport crash tests
221
        return crash_file_name
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
222
223
4634.128.10 by Martin Pool
Attach tail of .bzr.log to crash reports
224
def _attach_log_tail(pr):
225
    try:
226
        bzr_log = open(trace._get_bzr_log_filename(), 'rt')
4634.128.18 by Martin Pool
Update apport crash tests
227
    except (IOError, OSError), e:
228
        pr['BzrLogTail'] = repr(e)
229
        return
230
    try:
4634.128.10 by Martin Pool
Attach tail of .bzr.log to crash reports
231
        lines = bzr_log.readlines()
232
        pr['BzrLogTail'] = ''.join(lines[-40:])
233
    finally:
234
        bzr_log.close()
235
236
4584.3.6 by Martin Pool
Move apport integration to bzrlib.crash and send output to a file.
237
def _open_crash_file():
238
    crash_dir = config.crash_dir()
239
    if not osutils.isdir(crash_dir):
4634.128.2 by Martin Pool
Write crash files into /var/crash where apport can see them.
240
        # on unix this should be /var/crash and should already exist; on
241
        # Windows or if it's manually configured it might need to be created,
242
        # and then it should be private
243
        os.makedirs(crash_dir, mode=0600)
244
    date_string = time.strftime('%Y-%m-%dT%H:%M', time.gmtime())
4634.128.14 by Martin Pool
Don't call os.getuid on win32 (thanks gzlist)
245
    # XXX: getuid doesn't work on win32, but the crash directory is per-user
246
    if sys.platform == 'win32':
247
        user_part = ''
248
    else:
249
        user_part = '.%d' % os.getuid()
4634.128.16 by Martin Pool
Open the crash file using O_EXCL etc and set the mode
250
    filename = osutils.pathjoin(
251
        crash_dir,
252
        'bzr%s.%s.crash' % (
253
            user_part,
254
            date_string))
255
    # be careful here that people can't play tmp-type symlink mischief in the
256
    # world-writable directory
4634.128.18 by Martin Pool
Update apport crash tests
257
    return filename, os.fdopen(
4634.128.16 by Martin Pool
Open the crash file using O_EXCL etc and set the mode
258
        os.open(filename, 
259
            os.O_WRONLY|os.O_CREAT|os.O_EXCL,
260
            0600),
261
        'w')
4584.3.13 by Martin Pool
Refactor _format_plugin_list and include list of loaded modules in apport
262
263
264
def _format_plugin_list():
5616.7.10 by Martin Pool
Clean up describe_plugins to sort loaded and unloaded plugins together.
265
    return ''.join(plugin.describe_plugins(show_paths=True))
4584.3.13 by Martin Pool
Refactor _format_plugin_list and include list of loaded modules in apport
266
267
268
def _format_module_list():
269
    return pprint.pformat(sys.modules)