Back to index

apport  2.3
apport_python_hook.py
Go to the documentation of this file.
00001 '''Python sys.excepthook hook to generate apport crash dumps.'''
00002 
00003 # Copyright (c) 2006 - 2009 Canonical Ltd.
00004 # Authors: Robert Collins <robert@ubuntu.com>
00005 #          Martin Pitt <martin.pitt@ubuntu.com>
00006 #
00007 # This program is free software; you can redistribute it and/or modify it
00008 # under the terms of the GNU General Public License as published by the
00009 # Free Software Foundation; either version 2 of the License, or (at your
00010 # option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
00011 # the full text of the license.
00012 
00013 import os
00014 import sys
00015 
00016 CONFIG = '/etc/default/apport'
00017 
00018 
00019 def enabled():
00020     '''Return whether Apport should generate crash reports.'''
00021 
00022     # This doesn't use apport.packaging.enabled() because it is too heavyweight
00023     # See LP: #528355
00024     import re
00025     try:
00026         with open(CONFIG) as f:
00027             conf = f.read()
00028         return re.search('^\s*enabled\s*=\s*0\s*$', conf, re.M) is None
00029     except IOError:
00030         # if the file does not exist, assume it's enabled
00031         return True
00032 
00033 
00034 def apport_excepthook(exc_type, exc_obj, exc_tb):
00035     '''Catch an uncaught exception and make a traceback.'''
00036 
00037     # create and save a problem report. Note that exceptions in this code
00038     # are bad, and we probably need a per-thread reentrancy guard to
00039     # prevent that happening. However, on Ubuntu there should never be
00040     # a reason for an exception here, other than [say] a read only var
00041     # or some such. So what we do is use a try - finally to ensure that
00042     # the original excepthook is invoked, and until we get bug reports
00043     # ignore the other issues.
00044 
00045     # import locally here so that there is no routine overhead on python
00046     # startup time - only when a traceback occurs will this trigger.
00047     try:
00048         # ignore 'safe' exit types.
00049         if exc_type in (KeyboardInterrupt, ):
00050             return
00051 
00052         # do not do anything if apport was disabled
00053         if not enabled():
00054             return
00055 
00056         # org.freedesktop.DBus.Error.NoReply is an useless crash, needs actual
00057         # crash from D-BUS backend (LP# 914220)
00058         if 'org.freedesktop.DBus.Error.NoReply' in repr(exc_obj):
00059             return
00060 
00061         try:
00062             from cStringIO import StringIO
00063             StringIO  # pyflakes
00064         except ImportError:
00065             from io import StringIO
00066 
00067         import re, traceback
00068         from apport.fileutils import likely_packaged, get_recent_crashes
00069 
00070         # apport will look up the package from the executable path.
00071         try:
00072             binary = os.path.realpath(os.path.join(os.getcwd(), sys.argv[0]))
00073         except (TypeError, AttributeError, IndexError):
00074             # the module has mutated sys.argv, plan B
00075             try:
00076                 binary = os.readlink('/proc/%i/exe' % os.getpid())
00077             except OSError:
00078                 return
00079 
00080         # for interactive python sessions, sys.argv[0] == ''; catch that and
00081         # other irregularities
00082         if not os.access(binary, os.X_OK) or not os.path.isfile(binary):
00083             return
00084 
00085         # filter out binaries in user accessible paths
00086         if not likely_packaged(binary):
00087             return
00088 
00089         import apport.report
00090 
00091         pr = apport.report.Report()
00092         # append a basic traceback. In future we may want to include
00093         # additional data such as the local variables, loaded modules etc.
00094         tb_file = StringIO()
00095         traceback.print_exception(exc_type, exc_obj, exc_tb, file=tb_file)
00096         pr['Traceback'] = tb_file.getvalue().strip()
00097         pr.add_proc_info()
00098         pr.add_user_info()
00099         # override the ExecutablePath with the script that was actually running
00100         pr['ExecutablePath'] = binary
00101         try:
00102             pr['PythonArgs'] = '%r' % sys.argv
00103         except AttributeError:
00104             pass
00105         if pr.check_ignored():
00106             return
00107         mangled_program = re.sub('/', '_', binary)
00108         # get the uid for now, user name later
00109         user = os.getuid()
00110         pr_filename = '%s/%s.%i.crash' % (os.environ.get(
00111             'APPORT_REPORT_DIR', '/var/crash'), mangled_program, user)
00112         crash_counter = 0
00113         if os.path.exists(pr_filename):
00114             if apport.fileutils.seen_report(pr_filename):
00115                 # flood protection
00116                 with open(pr_filename, 'rb') as f:
00117                     crash_counter = get_recent_crashes(f) + 1
00118                 if crash_counter > 1:
00119                     return
00120 
00121                 # remove the old file, so that we can create the new one with
00122                 # os.O_CREAT|os.O_EXCL
00123                 os.unlink(pr_filename)
00124             else:
00125                 # don't clobber existing report
00126                 return
00127 
00128         if crash_counter:
00129             pr['CrashCounter'] = str(crash_counter)
00130         with os.fdopen(os.open(pr_filename,
00131                                os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o640), 'wb') as f:
00132             pr.write(f)
00133 
00134     finally:
00135         # resume original processing to get the default behaviour,
00136         # but do not trigger an AttributeError on interpreter shutdown.
00137         if sys:
00138             sys.__excepthook__(exc_type, exc_obj, exc_tb)
00139 
00140 
00141 def install():
00142     '''Install the python apport hook.'''
00143 
00144     sys.excepthook = apport_excepthook