Skip to contentSkip to conversation

Adding a userspace application crash handling system to autotest

PATCH Michael Goldish 2 participants 2 messages Sep 14, 2009 – Sep 15, 2009 1 message not in the archive KVM

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 1527767282.40681252922562180.JavaMail.root@zmail05.collab.prod.int.phx2.redhat.com

#1 lore raw

I think this is a very useful feature to have.

Please see some very minor comments below.

Quoted 209 lines · ----- "Lucas Meneghel Rodrigues

This patch adds a system to watch user space segmentation faults, writing core dumps and some degree of core dump analysis report. We believe that such a system will be beneficial for autotest as a whole, since the ability to get core dumps and dump analysis for each app crashing during an autotest execution can help test engineers with richer debugging information.

The system is comprised by 2 parts:

* Modifications on test code that enable core dumps
generation, register a core handler script in the kernel and check by generated core files at the end of each test.

* A core handler script that is going to write the
core on each test debug dir in a convenient way, with a report that currently is comprised by the process that died and a gdb stacktrace of the process. As the system gets in shape, we could add more scripts that can do fancier stuff (such as handlers that use frysk to get more info such as memory maps, provided that we have frysk installed in the machine).

This is the proof of concept of the system. I am sending it to the mailing list on this early stage so I can get feedback on the feature. The system passes my basic tests:

* Run a simple long test, such as the kvm test, and
then crash an application while the test is running. I get reports generated on test.debugdir

* Run a slightly more complex control file, with 3 parallel
bonnie instances at once and crash an application while the test is running. I get reports generated on all test.debugdirs.

3rd try:
* Explicitely enable core dumps using the resource module
* Fixed a bug on the crash detection code, and factored it into a utility function.

I believe we are good to go now.

---
 client/common_lib/test.py     |   66 +++++++++++++-
 client/tools/crash_handler.py |  202
+++++++++++++++++++++++++++++++++++++++++
 2 files changed, 266 insertions(+), 2 deletions(-)
 create mode 100755 client/tools/crash_handler.py
client/common_lib/test.py +0 -0
index 362c960..65b78a3 100644
@@ -17,7 +17,7 @@
 #       tmpdir          eg. tmp/<tempname>_<testname.tag>
 
 import fcntl, os, re, sys, shutil, tarfile, tempfile, time,

traceback

client/common_lib/test.py +7 -2
-import warnings, logging
+import warnings, logging, glob, resource
 
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.bin import utils
@@ -31,7 +31,6 @@ class base_test:
         self.job = job
         self.pkgmgr = job.pkgmgr
         self.autodir = job.autodir
-
         self.outputdir = outputdir
         self.tagged_testname = os.path.basename(self.outputdir)
         self.resultsdir = os.path.join(self.outputdir, 'results')
@@ -40,6 +39,7 @@ class base_test:
         os.mkdir(self.profdir)
         self.debugdir = os.path.join(self.outputdir, 'debug')
         os.mkdir(self.debugdir)
+        self.configure_crash_handler()
         self.bindir = bindir
         if hasattr(job, 'libdir'):
             self.libdir = job.libdir
@@ -54,6 +54,66 @@ class base_test:
         self.after_iteration_hooks = []
 
 
+    def configure_crash_handler(self):
+        """
+        Configure the crash handler by:
+         * Setting up core size to unlimited
+         * Putting an appropriate crash handler on

/proc/sys/kernel/core_pattern
+ * Create files that the crash handler will use to figure
which tests

client/common_lib/test.py +4 -0
+           are active at a given moment
+
+        The crash handler will pick up the core file and write it to
+        self.debugdir, and perform analysis on it to generate a

report. The

client/common_lib/test.py +3 -0
+        program also outputs some results to syslog.
+
+        If multiple tests are running, an attempt to verify if we

still have
+ the old PID on the system process table to determine whether
it is a
+ parent of the current test execution. If we can't determine
it, the
+ core file and the report file will be copied to all test
debug dirs.

client/common_lib/test.py +7 -0
+        """
+        self.pattern_file = '/proc/sys/kernel/core_pattern'
+        try:
+            # Enable core dumps
+            resource.setrlimit(resource.RLIMIT_CORE, (-1, -1))
+            # Trying to backup core pattern and register our script
+            self.core_pattern_backup = open(self.pattern_file,

'r').read()

client/common_lib/test.py +3 -0
+            pattern_file = open(self.pattern_file, 'w')
+            tools_dir = os.path.join(self.autodir, 'tools')
+            crash_handler_path = os.path.join(tools_dir,

'crash_handler.py')
+ pattern_file.write('|' + crash_handler_path + ' %p %t %u
%s %h %e')
+ # Writing the files that the crash handler is going to
use
+ self.debugdir_tmp_file = ('/tmp/autotest_results_dir.%s'
%

client/common_lib/test.py +2 -0
+                                      os.getpid())
+            utils.open_write_close(self.debugdir_tmp_file,

self.debugdir + "\n")

client/common_lib/test.py +11 -0
+        except Exception, e:
+            self.crash_handling_enabled = False
+            logging.error('Crash handling system disabled: %s' % e)
+        else:
+            self.crash_handling_enabled = True
+            logging.debug('Crash handling system enabled.')
+
+
+    def crash_handler_report(self):
+        """
+        If core dumps are found on the debugdir after the execution

of the

client/common_lib/test.py +6 -0
+        test, let the user know.
+        """
+        if self.crash_handling_enabled:
+            core_dirs = glob.glob('%s/crash.*' % self.debugdir)
+            if core_dirs:
+                logging.warning('Programs crashed during test

execution:')

client/common_lib/test.py +2 -0
+                for dir in core_dirs:
+                    logging.warning('Please verify %s for more info',

dir)

client/common_lib/test.py +10 -0
+            # Remove the debugdir info file
+            os.unlink(self.debugdir_tmp_file)
+            # Restore the core pattern backup
+            try:
+                utils.open_write_close(self.pattern_file,
+                                       self.core_pattern_backup)
+            except EnvironmentError:
+                pass
+
+
     def assert_(self, expr, msg='Assertion failed.'):
         if not expr:
             raise error.TestError(msg)
@@ -377,6 +437,7 @@ class base_test:
                         traceback.print_exc()
                         print 'Now raising the earlier %s error' %

exc_info[0]
finally:

client/common_lib/test.py +2 -0
+                    self.crash_handler_report()
                     self.job.logging.restore()
                     try:
                         raise exc_info[0], exc_info[1], exc_info[2]
@@ -389,6 +450,7 @@ class base_test:
                     if run_cleanup:
                         _cherry_pick_call(self.cleanup, *args,
 **dargs)
                 finally:
+                    self.crash_handler_report()
                     self.job.logging.restore()
         except error.AutotestError:

if self.network_destabilizing:

client/tools/crash_handler.py new file +18 -0
new file mode 100755
index 0000000..e281eb5
@@ -0,0 +1,202 @@
+#!/usr/bin/python
+"""
+Simple crash handling application for autotest
+
+@copyright Red Hat Inc 2009
+@author Lucas Meneghel Rodrigues <[email protected]>
+"""
+import sys, os, commands, glob, tempfile, shutil, syslog
+
+
+def get_parent_pid(pid):
+    """
+    Returns the parent PID for a given PID, converted to an integer.
+
+    @param pid: Process ID.
+    """
+    try:
+        stat_file_contents = open('/proc/%s/stat' % pid,
Quoted 2 lines

'r').readline()
+ ppid = int(stat_file_contents.split(" ")[3])

I think there's no need for the " " in the split() call, because split() defaults to splitting around any kind of whitespace.

The two lines can be combined into a rather short one, BTW:

ppid = int(open('/proc/%s/stat' % pid).read().split()[3])

(open() defaults to 'r' mode.)

Quoted 102 lines
client/tools/crash_handler.py new file +2 -0
+    except:
+        # It is not possible to determine the parent because the

process

client/tools/crash_handler.py new file +20 -0
+        # already left the process table.
+        ppid = 1
+
+    return ppid
+
+
+def pid_descends_from(pid_a, pid_b):
+    """
+    Check whether pid_a descends from pid_b.
+
+    @param pid_a: Process ID.
+    @param pid_b: Process ID.
+    """
+    pid_a = int(pid_a)
+    pid_b = int(pid_b)
+    current_pid = pid_a
+    while current_pid > 1:
+        if current_pid == pid_b:
+            syslog.syslog(syslog.LOG_INFO,
+                          "PID %s descends from PID %s!" % (pid_a,

pid_b))

client/tools/crash_handler.py new file +5 -0
+            return True
+        else:
+            current_pid = get_parent_pid(current_pid)
+    syslog.syslog(syslog.LOG_INFO,
+                  "PID %s does not descend from PID %s" % (pid_a,

pid_b))

client/tools/crash_handler.py new file +6 -0
+    return False
+
+
+def write_to_file(file_path, contents):
+    """
+    Write contents to a given file path specified. If not specified,

the file

client/tools/crash_handler.py new file +13 -0
+    will be created.
+
+    @param file_path: Path to a given file.
+    @param contents: File contents.
+    """
+    file_object = open(file_path, 'w')
+    file_object.write(contents)
+    file_object.close()
+
+
+def get_results_dir_list(pid, core_dir_basename):
+    """
+    Get all valid output directories for the core file and the

report. It works
+ by inspecting files created by each test on /tmp and verifying if
the
+ PID of the process that crashed is a child or grandchild of the
autotest
+ test process. If it can't find any relationship (maybe a daemon
that died
+ during a test execution), it will write the core file to the
debug dirs
+ of all tests currently being executed. If there are no active
autotest
+ tests at a particular moment, it will return a list with
['/tmp'].

client/tools/crash_handler.py new file +3 -0
+
+    @param pid: PID for the process that generated the core
+    @param core_dir_basename: Basename for the directory that will

hold both

client/tools/crash_handler.py new file +10 -0
+            the core dump and the crash report.
+    """
+    # Get all active test debugdir path files present
+    debugdir_files = glob.glob("/tmp/autotest_results_dir.*")
+    if debugdir_files:
+        pid_dir_dict = {}
+        for debugdir_file in debugdir_files:
+            a_pid = debugdir_file.split('.')[-1]
+            results_dir = open(debugdir_file, 'r').read().strip()
+            pid_dir_dict[a_pid] = os.path.join(results_dir,

core_dir_basename)

client/tools/crash_handler.py new file +9 -0
+
+        results_dir_list = []
+        found_relation = False
+        for a_pid, a_path in pid_dir_dict.iteritems():
+            if pid_descends_from(pid, a_pid):
+                results_dir_list.append(a_path)
+                found_relation = True
+
+        # If we could not find any relations between the pids in the

list with
+ # the process that crashed, we can't tell for sure which test
spawned
+ # the process (maybe it is a daemon and started even before
autotest
+ # started), so we will have to output the core file to all
active test

client/tools/crash_handler.py new file +8 -0
+        # directories.
+        if not found_relation:
+            return pid_dir_dict.values()
+        else:
+            return results_dir_list
+
+    else:
+        path_inactive_autotest = os.path.join('/tmp',
Quoted 2 lines

core_dir_basename)
+ return [path_inactive_autotest]

Here's a slightly shorter implementation of this function that doesn't need pid_descends_from():

pid_dir_dict = {}
for debugdir_file in glob.glob("/tmp/autotest_results_dir.*"):
a_pid = os.path.splitext(debugdir_file)[1]
results_dir = open(debugdir_file).read().strip() pid_dir_dict[a_pid] = os.path.join(results_dir, core_dir_basename)

results_dir_list = []
while pid > 1:
if pid_dir_dict.has_key(pid):
results_dir_list.append(pid_dir_dict[pid])
pid = get_parent_pid(pid)

return (results_dir_list or

        pid_dir_dict.values() or
        [os.path.join("/tmp", core_dir_basename)])

It's not very different from your version -- I just find it a little simpler.

Quoted 15 lines
client/tools/crash_handler.py new file +3 -0
+def get_info_from_core(path):
+    """
+    Reads a core file and extracts a dictionary with useful core

information.
+ Right now, the only information extracted is the full executable
name.

client/tools/crash_handler.py new file +4 -0
+
+    @param path: Path to core file.
+    """
+    # Here we are getting the executable full path in a very

inelegant way :(
+ # Since the 'right' solution for it is to make a library to get
information
+ # from core dump files, properly written, I'll leave this as it
is for now.

client/tools/crash_handler.py

Quoted 2 lines
client/tools/crash_handler.py new file +2 -0
+    full_exe_path = commands.getoutput('strings %s | grep "_="' %
+                                       path).strip("_=")

If you think that's unelegant you can try using regular expressions, but then it might be slightly tricky to match only printable characters. You can also use filter(lambda x: x in string.printable, str), but that would return all printable strings concatenated, without newlines separating them (unless there are newlines in the core file itself).

Quoted 1 line

+ if full_exe_path.startswith("./"):

I'm not sure, but it might be safer to use os.path.isabs(). If an exe path is relative will it always be prefixed by "./" in the core file, or can the binary name appear without any prefix?

Quoted 95 lines
client/tools/crash_handler.py new file +21 -0
+        pwd = commands.getoutput('strings %s | grep "^PWD="' %
+                                 path).strip("PWD=")
+        full_exe_path = os.path.join(pwd, full_exe_path.strip("./"))
+
+    return {'core_file': path, 'full_exe_path': full_exe_path}
+
+
+if __name__ == "__main__":
+    syslog.openlog('AutotestCrashHandler', 0, syslog.LOG_DAEMON)
+    (crashed_pid, time, uid, signal, hostname, exe) = sys.argv[1:]
+    core_name = 'core'
+    report_name = 'report'
+    core_dir_name = 'crash.%s.%s' % (exe, crashed_pid)
+    core_tmp_dir = tempfile.mkdtemp(prefix='core_', dir='/tmp')
+    core_tmp_path = os.path.join(core_tmp_dir, core_name)
+    gdb_command_path = os.path.join(core_tmp_dir, 'gdb_command')
+
+    try:
+        # Get the filtered results dir list
+        current_results_dir_list = get_results_dir_list(crashed_pid,
+                                                       

core_dir_name)

client/tools/crash_handler.py new file +12 -0
+
+        # Write the core file to the appropriate directory
+        # (we are piping it to this script)
+        core_file = sys.stdin.read()
+        write_to_file(core_tmp_path, core_file)
+
+        # Write a command file for GDB
+        gdb_command = 'bt full\n'
+        write_to_file(gdb_command_path, gdb_command)
+
+        # Get full command path
+        exe_path =

get_info_from_core(core_tmp_path)['full_exe_path']

client/tools/crash_handler.py new file +3 -0
+
+        # Take a backtrace from the running program
+        gdb_cmd = 'gdb -e %s -c %s -x %s -n -batch -quiet' %

(exe_path,
+
core_tmp_path,
+
gdb_command_path)

client/tools/crash_handler.py new file +29 -0
+        backtrace = commands.getoutput(gdb_cmd)
+        # Sanitize output before passing it to the report
+        backtrace = backtrace.decode('utf-8', 'ignore')
+
+        # Composing the format_dict
+        format_dict = {}
+        format_dict['program'] = exe_path
+        format_dict['pid'] = crashed_pid
+        format_dict['signal'] = signal
+        format_dict['hostname'] = hostname
+        format_dict['time'] = time
+        format_dict['backtrace'] = backtrace
+
+        report = """Autotest crash report
+
+Program: %(program)s
+PID: %(pid)s
+Signal: %(signal)s
+Hostname: %(hostname)s
+Time of the crash: %(time)s
+Program backtrace:
+%(backtrace)s
+""" % format_dict
+
+        syslog.syslog(syslog.LOG_INFO,
+                      "Application %s, PID %s crashed" %
+                      (exe_path, crashed_pid))
+
+        # Now, for all results dir, let's create the directory if it

doesn't

client/tools/crash_handler.py new file +15 -0
+        # exist, and write the core file and the report to it.
+        syslog.syslog(syslog.LOG_INFO,
+                      "Writing core files and reports to %s" %
+                      current_results_dir_list)
+        for result_dir in current_results_dir_list:
+            if not os.path.isdir(result_dir):
+                os.makedirs(result_dir)
+            core_path = os.path.join(result_dir, 'core')
+            write_to_file(core_path, core_file)
+            report_path = os.path.join(result_dir, 'report')
+            write_to_file(report_path, report)
+
+    finally:
+        # Cleanup temporary directories
+        shutil.rmtree(core_tmp_dir)
Signature · 6 lines
1.6.2.5

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 212 lines · Michael Goldish

I think this is a very useful feature to have.

Please see some very minor comments below.

Quoted 206 lines · ----- "Lucas Meneghel Rodrigues

This patch adds a system to watch user space segmentation faults, writing core dumps and some degree of core dump analysis report. We believe that such a system will be beneficial for autotest as a whole, since the ability to get core dumps and dump analysis for each app crashing during an autotest execution can help test engineers with richer debugging information.

The system is comprised by 2 parts:

 * Modifications on test code that enable core dumps generation, register a core handler script in the kernel and check by generated core files at the end of each test.

 * A core handler script that is going to write the core on each test debug dir in a convenient way, with a report that currently is comprised by the process that died and a gdb stacktrace of the process. As the system gets in shape, we could add more scripts that can do fancier stuff (such as handlers that use frysk to get more info such as memory maps, provided that we have frysk installed in the machine).

This is the proof of concept of the system. I am sending it to the mailing list on this early stage so I can get feedback on the feature. The system passes my basic tests:

 * Run a simple long test, such as the kvm test, and then crash an application while the test is running. I get reports generated on test.debugdir

 * Run a slightly more complex control file, with 3 parallel bonnie instances at once and crash an application while the test is running. I get reports generated on all test.debugdirs.

3rd try:
 * Explicitely enable core dumps using the resource module
 * Fixed a bug on the crash detection code, and factored it into a utility function.

I believe we are good to go now.

---
 client/common_lib/test.py     |   66 +++++++++++++- client/tools/crash_handler.py |  202
+++++++++++++++++++++++++++++++++++++++++

 2 files changed, 266 insertions(+), 2 deletions(-)

 create mode 100755 client/tools/crash_handler.py

client/common_lib/test.py +0 -0
index 362c960..65b78a3 100644
@@ -17,7 +17,7 @@
  #       tmpdir          eg. tmp/<tempname>_<testname.tag>
 
  import fcntl, os, re, sys, shutil, tarfile, tempfile, time,

traceback

client/common_lib/test.py +1 -1
-import warnings, logging
+import warnings, logging, glob, resource

 from autotest_lib.client.common_lib import error from autotest_lib.client.bin import utils

client/common_lib/test.py +0 -0
@@ -31,7 +31,6 @@ class base_test:

         self.job = job
         self.pkgmgr = job.pkgmgr
         self.autodir = job.autodir
-
         self.outputdir = outputdir self.tagged_testname = os.path.basename(self.outputdir) self.resultsdir = os.path.join(self.outputdir, 'results')

client/common_lib/test.py +1 -0
@@ -40,6 +39,7 @@ class base_test:
          os.mkdir(self.profdir)
          self.debugdir = os.path.join(self.outputdir, 'debug')
          os.mkdir(self.debugdir)
+        self.configure_crash_handler()

         self.bindir = bindir
         if hasattr(job, 'libdir'):
             self.libdir = job.libdir

client/common_lib/test.py +0 -0
@@ -54,6 +54,66 @@ class base_test:

         self.after_iteration_hooks = []

+    def configure_crash_handler(self):
+        """
+        Configure the crash handler by:
+         * Setting up core size to unlimited
+         * Putting an appropriate crash handler on /proc/sys/kernel/core_pattern
+         * Create files that the crash handler will use to figure which tests
+           are active at a given moment
+
+        The crash handler will pick up the core file and write it to
+        self.debugdir, and perform analysis on it to generate a report. The
+        program also outputs some results to syslog.
+
+        If multiple tests are running, an attempt to verify if we still have
+        the old PID on the system process table to determine whether it is a
+        parent of the current test execution. If we can't determine it, the
+        core file and the report file will be copied to all test debug dirs.
+        """
+        self.pattern_file = '/proc/sys/kernel/core_pattern'
+        try:
+            # Enable core dumps
+            resource.setrlimit(resource.RLIMIT_CORE, (-1, -1))
+            # Trying to backup core pattern and register our script
+            self.core_pattern_backup = open(self.pattern_file, 'r').read()
+            pattern_file = open(self.pattern_file, 'w')
+            tools_dir = os.path.join(self.autodir, 'tools')
+            crash_handler_path = os.path.join(tools_dir, 'crash_handler.py')
+            pattern_file.write('|' + crash_handler_path + ' %p %t %u %s %h %e')
+            # Writing the files that the crash handler is going to use
+            self.debugdir_tmp_file = ('/tmp/autotest_results_dir.%s' %
+                                      os.getpid())
+            utils.open_write_close(self.debugdir_tmp_file, self.debugdir + "\n")
+        except Exception, e:
+            self.crash_handling_enabled = False
+            logging.error('Crash handling system disabled: %s' % e)
+        else:
+            self.crash_handling_enabled = True
+            logging.debug('Crash handling system enabled.')

client/common_lib/test.py +5 -0
+
+
+    def crash_handler_report(self):
+        """
+        If core dumps are found on the debugdir after the execution

of the
+        test, let the user know.
+        """
+        if self.crash_handling_enabled:
+            core_dirs = glob.glob('%s/crash.*' % self.debugdir)
+            if core_dirs:
+                logging.warning('Programs crashed during test execution:')
+                for dir in core_dirs:
+                    logging.warning('Please verify %s for more info', dir)
+            # Remove the debugdir info file
+            os.unlink(self.debugdir_tmp_file)
+            # Restore the core pattern backup
+            try:
+                utils.open_write_close(self.pattern_file,
+                                       self.core_pattern_backup)
+            except EnvironmentError:
+                pass

client/common_lib/test.py +2 -0
+
+

     def assert_(self, expr, msg='Assertion failed.'):
         if not expr:
             raise error.TestError(msg)

client/common_lib/test.py +0 -0
@@ -377,6 +437,7 @@ class base_test:
                          traceback.print_exc()

                         print 'Now raising the earlier %s error' % exc_info[0]
                 finally:
+                    self.crash_handler_report() self.job.logging.restore()
                     try:
                         raise exc_info[0], exc_info[1], exc_info[2]

client/common_lib/test.py +0 -0
@@ -389,6 +450,7 @@ class base_test:

                     if run_cleanup:
                         _cherry_pick_call(self.cleanup, *args, **dargs)
                 finally:
+                    self.crash_handler_report() self.job.logging.restore() except error.AutotestError:
             if self.network_destabilizing:

client/tools/crash_handler.py new file +15 -0
new file mode 100755
index 0000000..e281eb5
@@ -0,0 +1,202 @@
+#!/usr/bin/python
+"""
+Simple crash handling application for autotest
+
+@copyright Red Hat Inc 2009
+@author Lucas Meneghel Rodrigues <[email protected]>
+"""
+import sys, os, commands, glob, tempfile, shutil, syslog
+
+
+def get_parent_pid(pid):
+    """
+    Returns the parent PID for a given PID, converted to an integer.
+
+    @param pid: Process ID.
Quoted 8 lines
Quoted 5 lines
client/tools/crash_handler.py new file +3 -0
+    """
+    try:
+        stat_file_contents = open('/proc/%s/stat' % pid,

'r').readline()
+        ppid = int(stat_file_contents.split(" ")[3])

I think there's no need for the " " in the split() call, because split() defaults to splitting around any kind of whitespace.

Right, fair enough!

Quoted 5 lines

The two lines can be combined into a rather short one, BTW:

ppid = int(open('/proc/%s/stat' % pid).read().split()[3])

(open() defaults to 'r' mode.)

Done. I prefered the above, it was a bit more explicit, but in the end I realized it was not harder to understand the condensed form.

Quoted 120 lines
Quoted 96 lines

+    except:
+        # It is not possible to determine the parent because the process
+        # already left the process table.
+        ppid = 1

client/tools/crash_handler.py new file +18 -0
+
+    return ppid
+
+
+def pid_descends_from(pid_a, pid_b):
+    """
+    Check whether pid_a descends from pid_b.
+
+    @param pid_a: Process ID.
+    @param pid_b: Process ID.
+    """
+    pid_a = int(pid_a)
+    pid_b = int(pid_b)
+    current_pid = pid_a
+    while current_pid > 1:
+        if current_pid == pid_b:
+            syslog.syslog(syslog.LOG_INFO,
+                          "PID %s descends from PID %s!" % (pid_a,

pid_b))
+            return True
+        else:
+            current_pid = get_parent_pid(current_pid)
+    syslog.syslog(syslog.LOG_INFO,
+                  "PID %s does not descend from PID %s" % (pid_a, pid_b))
+    return False

client/tools/crash_handler.py new file +5 -0
+
+
+def write_to_file(file_path, contents):
+    """
+    Write contents to a given file path specified. If not specified,

the file
+    will be created.

client/tools/crash_handler.py new file +12 -0
+
+    @param file_path: Path to a given file.
+    @param contents: File contents.
+    """
+    file_object = open(file_path, 'w')
+    file_object.write(contents)
+    file_object.close()
+
+
+def get_results_dir_list(pid, core_dir_basename):
+    """
+    Get all valid output directories for the core file and the

report. It works
+    by inspecting files created by each test on /tmp and verifying if the
+    PID of the process that crashed is a child or grandchild of the autotest
+    test process. If it can't find any relationship (maybe a daemon that died
+    during a test execution), it will write the core file to the debug dirs
+    of all tests currently being executed. If there are no active autotest
+    tests at a particular moment, it will return a list with ['/tmp'].
+
+    @param pid: PID for the process that generated the core
+    @param core_dir_basename: Basename for the directory that will hold both
+            the core dump and the crash report.
+    """
+    # Get all active test debugdir path files present
+    debugdir_files = glob.glob("/tmp/autotest_results_dir.*")
+    if debugdir_files:
+        pid_dir_dict = {}
+        for debugdir_file in debugdir_files:
+            a_pid = debugdir_file.split('.')[-1]
+            results_dir = open(debugdir_file, 'r').read().strip()
+            pid_dir_dict[a_pid] = os.path.join(results_dir, core_dir_basename)

client/tools/crash_handler.py new file +9 -0
+
+        results_dir_list = []
+        found_relation = False
+        for a_pid, a_path in pid_dir_dict.iteritems():
+            if pid_descends_from(pid, a_pid):
+                results_dir_list.append(a_path)
+                found_relation = True
+
+        # If we could not find any relations between the pids in the

list with
+        # the process that crashed, we can't tell for sure which test spawned
+        # the process (maybe it is a daemon and started even before autotest
+        # started), so we will have to output the core file to all active test
+        # directories.
+        if not found_relation:

Quoted 8 lines

+            return pid_dir_dict.values()
+        else:
+            return results_dir_list
+
+    else:
+        path_inactive_autotest = os.path.join('/tmp', core_dir_basename)
+        return [path_inactive_autotest]

Here's a slightly shorter implementation of this function that doesn't need pid_descends_from():

pid_dir_dict = {}
for debugdir_file in glob.glob("/tmp/autotest_results_dir.*"):
   a_pid = os.path.splitext(debugdir_file)[1]
   results_dir = open(debugdir_file).read().strip() pid_dir_dict[a_pid] = os.path.join(results_dir, core_dir_basename)

results_dir_list = []
while pid > 1:
   if pid_dir_dict.has_key(pid):
       results_dir_list.append(pid_dir_dict[pid])
   pid = get_parent_pid(pid)

Quoted 6 lines

return (results_dir_list or
       pid_dir_dict.values() or
       [os.path.join("/tmp", core_dir_basename)])

It's not very different from your version -- I just find it a little simpler.

Ok, Implemented with the comments John made.

Quoted 24 lines
Quoted 9 lines

+def get_info_from_core(path):
+    """
+    Reads a core file and extracts a dictionary with useful core information.
+    Right now, the only information extracted is the full executable name.
+
+    @param path: Path to core file.
+    """

Quoted 8 lines

+    # Here we are getting the executable full path in a very inelegant way :(
+    # Since the 'right' solution for it is to make a library to get information
+    # from core dump files, properly written, I'll leave this as it is for now.
+    full_exe_path = commands.getoutput('strings %s | grep "_="' %
+                                       path).strip("_=")

If you think that's unelegant you can try using regular expressions, but then it might be slightly tricky to match only printable characters. You can also use filter(lambda x: x in string.printable, str), but that would return all printable strings concatenated, without newlines separating them (unless there are newlines in the core file itself).

Quoted 5 lines
Quoted 1 line

+    if full_exe_path.startswith("./"):

I'm not sure, but it might be safer to use os.path.isabs(). If an exe path is relative will it always be prefixed by "./" in the core file, or can the binary name appear without any prefix?

The session marked with _= contains the command line, I am not aware of a case where there's nothing added to the prefix, so I left it the way it was. If it turns out that this is not good enough, I will change it. I was talking to Paul Muldoon, one of the frysk authors and we might try to do a library for getting info from core files...

Thanks for your comments, Michael, I've sent an updated version for evaluation!

Signature · 1 line
Lucas