Back to index

apport  2.3
Functions | Variables
apport_python_hook Namespace Reference

Functions

def enabled
def apport_excepthook
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 34 of file apport_python_hook.py.

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         # org.freedesktop.DBus.Error.NoReply is an useless crash, needs actual
00058         # crash from D-BUS backend (LP# 914220)
00059         if 'org.freedesktop.DBus.Error.NoReply' in repr(exc_obj):
00060             return
00061 
00062         try:
00063             from cStringIO import StringIO
00064             StringIO  # pyflakes
00065         except ImportError:
00066             from io import StringIO
00067 
00068         import re, traceback
00069         from apport.fileutils import likely_packaged, get_recent_crashes
00070 
00071         # apport will look up the package from the executable path.
00072         try:
00073             binary = os.path.realpath(os.path.join(os.getcwd(), sys.argv[0]))
00074         except (TypeError, AttributeError, IndexError):
00075             # the module has mutated sys.argv, plan B
00076             try:
00077                 binary = os.readlink('/proc/%i/exe' % os.getpid())
00078             except OSError:
00079                 return
00080 
00081         # for interactive python sessions, sys.argv[0] == ''; catch that and
00082         # other irregularities
00083         if not os.access(binary, os.X_OK) or not os.path.isfile(binary):
00084             return
00085 
00086         # filter out binaries in user accessible paths
00087         if not likely_packaged(binary):
00088             return
00089 
00090         import apport.report
00091 
00092         pr = apport.report.Report()
00093         # append a basic traceback. In future we may want to include
00094         # additional data such as the local variables, loaded modules etc.
00095         tb_file = StringIO()
00096         traceback.print_exception(exc_type, exc_obj, exc_tb, file=tb_file)
00097         pr['Traceback'] = tb_file.getvalue().strip()
00098         pr.add_proc_info()
00099         pr.add_user_info()
00100         # override the ExecutablePath with the script that was actually running
00101         pr['ExecutablePath'] = binary
00102         try:
00103             pr['PythonArgs'] = '%r' % sys.argv
00104         except AttributeError:
00105             pass
00106         if pr.check_ignored():
00107             return
00108         mangled_program = re.sub('/', '_', binary)
00109         # get the uid for now, user name later
00110         user = os.getuid()
00111         pr_filename = '%s/%s.%i.crash' % (os.environ.get(
00112             'APPORT_REPORT_DIR', '/var/crash'), mangled_program, user)
00113         crash_counter = 0
00114         if os.path.exists(pr_filename):
00115             if apport.fileutils.seen_report(pr_filename):
00116                 # flood protection
00117                 with open(pr_filename, 'rb') as f:
00118                     crash_counter = get_recent_crashes(f) + 1
00119                 if crash_counter > 1:
00120                     return
00121 
00122                 # remove the old file, so that we can create the new one with
00123                 # os.O_CREAT|os.O_EXCL
00124                 os.unlink(pr_filename)
00125             else:
00126                 # don't clobber existing report
00127                 return
00128 
00129         if crash_counter:
00130             pr['CrashCounter'] = str(crash_counter)
00131         with os.fdopen(os.open(pr_filename,
00132                                os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o640), 'wb') as f:
00133             pr.write(f)
00134 
00135     finally:
00136         # resume original processing to get the default behaviour,
00137         # but do not trigger an AttributeError on interpreter shutdown.
00138         if sys:
00139             sys.__excepthook__(exc_type, exc_obj, exc_tb)
00140 

Here is the call graph for this function:

Return whether Apport should generate crash reports.

Definition at line 19 of file apport_python_hook.py.

00019 
00020 def enabled():
00021     '''Return whether Apport should generate crash reports.'''
00022 
00023     # This doesn't use apport.packaging.enabled() because it is too heavyweight
00024     # See LP: #528355
00025     import re
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 

Here is the caller graph for this function:

Install the python apport hook.

Definition at line 141 of file apport_python_hook.py.

00141 
00142 def install():
00143     '''Install the python apport hook.'''
00144 
00145     sys.excepthook = apport_excepthook

Variable Documentation

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

Definition at line 16 of file apport_python_hook.py.