Back to index

apport  2.4
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 import re
00016 from glob import glob
00017 
00018 CONFIG = '/etc/default/apport'
00019 
00020 
00021 def enabled():
00022     '''Return whether Apport should generate crash reports.'''
00023 
00024     # This doesn't use apport.packaging.enabled() because it is too heavyweight
00025     # See LP: #528355
00026     try:
00027         with open(CONFIG) as f:
00028             conf = f.read()
00029         return re.search('^\s*enabled\s*=\s*0\s*$', conf, re.M) is None
00030     except IOError:
00031         # if the file does not exist, assume it's enabled
00032         return True
00033 
00034 
00035 def apport_excepthook(exc_type, exc_obj, exc_tb):
00036     '''Catch an uncaught exception and make a traceback.'''
00037 
00038     # create and save a problem report. Note that exceptions in this code
00039     # are bad, and we probably need a per-thread reentrancy guard to
00040     # prevent that happening. However, on Ubuntu there should never be
00041     # a reason for an exception here, other than [say] a read only var
00042     # or some such. So what we do is use a try - finally to ensure that
00043     # the original excepthook is invoked, and until we get bug reports
00044     # ignore the other issues.
00045 
00046     # import locally here so that there is no routine overhead on python
00047     # startup time - only when a traceback occurs will this trigger.
00048     try:
00049         # ignore 'safe' exit types.
00050         if exc_type in (KeyboardInterrupt, ):
00051             return
00052 
00053         # do not do anything if apport was disabled
00054         if not enabled():
00055             return
00056 
00057         try:
00058             from cStringIO import StringIO
00059             StringIO  # pyflakes
00060         except ImportError:
00061             from io import StringIO
00062 
00063         import re, traceback
00064         from apport.fileutils import likely_packaged, get_recent_crashes
00065 
00066         # apport will look up the package from the executable path.
00067         try:
00068             binary = os.path.realpath(os.path.join(os.getcwd(), sys.argv[0]))
00069         except (TypeError, AttributeError, IndexError):
00070             # the module has mutated sys.argv, plan B
00071             try:
00072                 binary = os.readlink('/proc/%i/exe' % os.getpid())
00073             except OSError:
00074                 return
00075 
00076         # for interactive python sessions, sys.argv[0] == ''; catch that and
00077         # other irregularities
00078         if not os.access(binary, os.X_OK) or not os.path.isfile(binary):
00079             return
00080 
00081         # filter out binaries in user accessible paths
00082         if not likely_packaged(binary):
00083             return
00084 
00085         import apport.report
00086 
00087         pr = apport.report.Report()
00088 
00089         # special handling of dbus-python exceptions
00090         if hasattr(exc_obj, 'get_dbus_name'):
00091             if exc_obj.get_dbus_name() == 'org.freedesktop.DBus.Error.NoReply':
00092                 # NoReply is an useless crash, we do not even get the method it
00093                 # was trying to call; needs actual crash from D-BUS backend (LP #914220)
00094                 return
00095             if exc_obj.get_dbus_name() == 'org.freedesktop.DBus.Error.ServiceUnknown':
00096                 dbus_service_unknown_analysis(exc_obj, pr)
00097 
00098         # append a basic traceback. In future we may want to include
00099         # additional data such as the local variables, loaded modules etc.
00100         tb_file = StringIO()
00101         traceback.print_exception(exc_type, exc_obj, exc_tb, file=tb_file)
00102         pr['Traceback'] = tb_file.getvalue().strip()
00103         pr.add_proc_info()
00104         pr.add_user_info()
00105         # override the ExecutablePath with the script that was actually running
00106         pr['ExecutablePath'] = binary
00107         try:
00108             pr['PythonArgs'] = '%r' % sys.argv
00109         except AttributeError:
00110             pass
00111         if pr.check_ignored():
00112             return
00113         mangled_program = re.sub('/', '_', binary)
00114         # get the uid for now, user name later
00115         user = os.getuid()
00116         pr_filename = '%s/%s.%i.crash' % (os.environ.get(
00117             'APPORT_REPORT_DIR', '/var/crash'), mangled_program, user)
00118         crash_counter = 0
00119         if os.path.exists(pr_filename):
00120             if apport.fileutils.seen_report(pr_filename):
00121                 # flood protection
00122                 with open(pr_filename, 'rb') as f:
00123                     crash_counter = get_recent_crashes(f) + 1
00124                 if crash_counter > 1:
00125                     return
00126 
00127                 # remove the old file, so that we can create the new one with
00128                 # os.O_CREAT|os.O_EXCL
00129                 os.unlink(pr_filename)
00130             else:
00131                 # don't clobber existing report
00132                 return
00133 
00134         if crash_counter:
00135             pr['CrashCounter'] = str(crash_counter)
00136         with os.fdopen(os.open(pr_filename,
00137                                os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o640), 'wb') as f:
00138             pr.write(f)
00139 
00140     finally:
00141         # resume original processing to get the default behaviour,
00142         # but do not trigger an AttributeError on interpreter shutdown.
00143         if sys:
00144             sys.__excepthook__(exc_type, exc_obj, exc_tb)
00145 
00146 
00147 def dbus_service_unknown_analysis(exc_obj, report):
00148     import subprocess
00149     try:
00150         from configparser import ConfigParser, NoSectionError, NoOptionError
00151         (ConfigParser, NoSectionError, NoOptionError)  # pyflakes
00152     except ImportError:
00153         # Python 2
00154         from ConfigParser import ConfigParser, NoSectionError, NoOptionError
00155 
00156     # determine D-BUS name
00157     m = re.search('name\s+(\S+)\s+was not provided by any .service',
00158                   exc_obj.get_dbus_message())
00159     if not m:
00160         if sys.stderr:
00161             sys.stderr.write('Error: cannot parse D-BUS name from exception: '
00162                              + exc_obj.get_dbus_message())
00163             return
00164 
00165     dbus_name = m.group(1)
00166 
00167     # determine .service file and Exec name for the D-BUS name
00168     services = []  # tuples of (service file, exe name, running)
00169     for f in glob('/usr/share/dbus-1/*services/*.service'):
00170         cp = ConfigParser(interpolation=None)
00171         cp.read(f, encoding='UTF-8')
00172         try:
00173             if cp.get('D-BUS Service', 'Name') == dbus_name:
00174                 exe = cp.get('D-BUS Service', 'Exec')
00175                 running = (subprocess.call(['pidof', '-sx', exe], stdout=subprocess.PIPE) == 0)
00176                 services.append((f, exe, running))
00177         except (NoSectionError, NoOptionError):
00178             if sys.stderr:
00179                 sys.stderr.write('Invalid D-BUS .service file %s: %s' % (
00180                     f, exc_obj.get_dbus_message()))
00181             continue
00182 
00183     if not services:
00184         report['DbusErrorAnalysis'] = 'no service file providing ' + dbus_name
00185     else:
00186         report['DbusErrorAnalysis'] = 'provided by'
00187         for (service, exe, running) in services:
00188             report['DbusErrorAnalysis'] += ' %s (%s is %srunning)' % (
00189                 service, exe, ('' if running else 'not '))
00190 
00191 
00192 def install():
00193     '''Install the python apport hook.'''
00194 
00195     sys.excepthook = apport_excepthook