Back to index

apport  2.4
Functions | Variables
apport_python_hook Namespace Reference

Functions

def enabled
def apport_excepthook
def dbus_service_unknown_analysis
def install

Variables

string CONFIG = '/etc/default/apport'

Detailed Description

Python sys.excepthook hook to generate apport crash dumps.

Function Documentation

def apport_python_hook.apport_excepthook (   exc_type,
  exc_obj,
  exc_tb 
)
Catch an uncaught exception and make a traceback.

Definition at line 35 of file apport_python_hook.py.

00035 
00036 def apport_excepthook(exc_type, exc_obj, exc_tb):
00037     '''Catch an uncaught exception and make a traceback.'''
00038 
00039     # create and save a problem report. Note that exceptions in this code
00040     # are bad, and we probably need a per-thread reentrancy guard to
00041     # prevent that happening. However, on Ubuntu there should never be
00042     # a reason for an exception here, other than [say] a read only var
00043     # or some such. So what we do is use a try - finally to ensure that
00044     # the original excepthook is invoked, and until we get bug reports
00045     # ignore the other issues.
00046 
00047     # import locally here so that there is no routine overhead on python
00048     # startup time - only when a traceback occurs will this trigger.
00049     try:
00050         # ignore 'safe' exit types.
00051         if exc_type in (KeyboardInterrupt, ):
00052             return
00053 
00054         # do not do anything if apport was disabled
00055         if not enabled():
00056             return
00057 
00058         try:
00059             from cStringIO import StringIO
00060             StringIO  # pyflakes
00061         except ImportError:
00062             from io import StringIO
00063 
00064         import re, traceback
00065         from apport.fileutils import likely_packaged, get_recent_crashes
00066 
00067         # apport will look up the package from the executable path.
00068         try:
00069             binary = os.path.realpath(os.path.join(os.getcwd(), sys.argv[0]))
00070         except (TypeError, AttributeError, IndexError):
00071             # the module has mutated sys.argv, plan B
00072             try:
00073                 binary = os.readlink('/proc/%i/exe' % os.getpid())
00074             except OSError:
00075                 return
00076 
00077         # for interactive python sessions, sys.argv[0] == ''; catch that and
00078         # other irregularities
00079         if not os.access(binary, os.X_OK) or not os.path.isfile(binary):
00080             return
00081 
00082         # filter out binaries in user accessible paths
00083         if not likely_packaged(binary):
00084             return
00085 
00086         import apport.report
00087 
00088         pr = apport.report.Report()
00089 
00090         # special handling of dbus-python exceptions
00091         if hasattr(exc_obj, 'get_dbus_name'):
00092             if exc_obj.get_dbus_name() == 'org.freedesktop.DBus.Error.NoReply':
00093                 # NoReply is an useless crash, we do not even get the method it
00094                 # was trying to call; needs actual crash from D-BUS backend (LP #914220)
00095                 return
00096             if exc_obj.get_dbus_name() == 'org.freedesktop.DBus.Error.ServiceUnknown':
00097                 dbus_service_unknown_analysis(exc_obj, pr)
00098 
00099         # append a basic traceback. In future we may want to include
00100         # additional data such as the local variables, loaded modules etc.
00101         tb_file = StringIO()
00102         traceback.print_exception(exc_type, exc_obj, exc_tb, file=tb_file)
00103         pr['Traceback'] = tb_file.getvalue().strip()
00104         pr.add_proc_info()
00105         pr.add_user_info()
00106         # override the ExecutablePath with the script that was actually running
00107         pr['ExecutablePath'] = binary
00108         try:
00109             pr['PythonArgs'] = '%r' % sys.argv
00110         except AttributeError:
00111             pass
00112         if pr.check_ignored():
00113             return
00114         mangled_program = re.sub('/', '_', binary)
00115         # get the uid for now, user name later
00116         user = os.getuid()
00117         pr_filename = '%s/%s.%i.crash' % (os.environ.get(
00118             'APPORT_REPORT_DIR', '/var/crash'), mangled_program, user)
00119         crash_counter = 0
00120         if os.path.exists(pr_filename):
00121             if apport.fileutils.seen_report(pr_filename):
00122                 # flood protection
00123                 with open(pr_filename, 'rb') as f:
00124                     crash_counter = get_recent_crashes(f) + 1
00125                 if crash_counter > 1:
00126                     return
00127 
00128                 # remove the old file, so that we can create the new one with
00129                 # os.O_CREAT|os.O_EXCL
00130                 os.unlink(pr_filename)
00131             else:
00132                 # don't clobber existing report
00133                 return
00134 
00135         if crash_counter:
00136             pr['CrashCounter'] = str(crash_counter)
00137         with os.fdopen(os.open(pr_filename,
00138                                os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o640), 'wb') as f:
00139             pr.write(f)
00140 
00141     finally:
00142         # resume original processing to get the default behaviour,
00143         # but do not trigger an AttributeError on interpreter shutdown.
00144         if sys:
00145             sys.__excepthook__(exc_type, exc_obj, exc_tb)
00146 

Here is the call graph for this function:

def apport_python_hook.dbus_service_unknown_analysis (   exc_obj,
  report 
)

Definition at line 147 of file apport_python_hook.py.

00147 
00148 def dbus_service_unknown_analysis(exc_obj, report):
00149     import subprocess
00150     try:
00151         from configparser import ConfigParser, NoSectionError, NoOptionError
00152         (ConfigParser, NoSectionError, NoOptionError)  # pyflakes
00153     except ImportError:
00154         # Python 2
00155         from ConfigParser import ConfigParser, NoSectionError, NoOptionError
00156 
00157     # determine D-BUS name
00158     m = re.search('name\s+(\S+)\s+was not provided by any .service',
00159                   exc_obj.get_dbus_message())
00160     if not m:
00161         if sys.stderr:
00162             sys.stderr.write('Error: cannot parse D-BUS name from exception: '
00163                              + exc_obj.get_dbus_message())
00164             return
00165 
00166     dbus_name = m.group(1)
00167 
00168     # determine .service file and Exec name for the D-BUS name
00169     services = []  # tuples of (service file, exe name, running)
00170     for f in glob('/usr/share/dbus-1/*services/*.service'):
00171         cp = ConfigParser(interpolation=None)
00172         cp.read(f, encoding='UTF-8')
00173         try:
00174             if cp.get('D-BUS Service', 'Name') == dbus_name:
00175                 exe = cp.get('D-BUS Service', 'Exec')
00176                 running = (subprocess.call(['pidof', '-sx', exe], stdout=subprocess.PIPE) == 0)
00177                 services.append((f, exe, running))
00178         except (NoSectionError, NoOptionError):
00179             if sys.stderr:
00180                 sys.stderr.write('Invalid D-BUS .service file %s: %s' % (
00181                     f, exc_obj.get_dbus_message()))
00182             continue
00183 
00184     if not services:
00185         report['DbusErrorAnalysis'] = 'no service file providing ' + dbus_name
00186     else:
00187         report['DbusErrorAnalysis'] = 'provided by'
00188         for (service, exe, running) in services:
00189             report['DbusErrorAnalysis'] += ' %s (%s is %srunning)' % (
00190                 service, exe, ('' if running else 'not '))
00191 

Here is the caller graph for this function:

Return whether Apport should generate crash reports.

Definition at line 21 of file apport_python_hook.py.

00021 
00022 def enabled():
00023     '''Return whether Apport should generate crash reports.'''
00024 
00025     # This doesn't use apport.packaging.enabled() because it is too heavyweight
00026     # See LP: #528355
00027     try:
00028         with open(CONFIG) as f:
00029             conf = f.read()
00030         return re.search('^\s*enabled\s*=\s*0\s*$', conf, re.M) is None
00031     except IOError:
00032         # if the file does not exist, assume it's enabled
00033         return True
00034 

Here is the caller graph for this function:

Install the python apport hook.

Definition at line 192 of file apport_python_hook.py.

00192 
00193 def install():
00194     '''Install the python apport hook.'''
00195 
00196     sys.excepthook = apport_excepthook

Variable Documentation

string apport_python_hook.CONFIG = '/etc/default/apport'

Definition at line 18 of file apport_python_hook.py.