From: John Admanski Subject: Re: [PATCH] Adding a userspace application crash handling system to autotest Date: Mon, 17 Aug 2009 10:18:26 -0700 Message-ID: <394b34480908171018w2a22df6ai8e45d0121113c377@mail.gmail.com> References: <1250129059-20998-1-git-send-email-lmr@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: autotest@test.kernel.org, kvm@vger.kernel.org, mtosatti@redhat.com, mbligh@google.com, showard@google.com To: Lucas Meneghel Rodrigues Return-path: Received: from smtp-out.google.com ([216.239.33.17]:45200 "EHLO smtp-out.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932284AbZHQRSw convert rfc822-to-8bit (ORCPT ); Mon, 17 Aug 2009 13:18:52 -0400 Received: from spaceape11.eur.corp.google.com (spaceape11.eur.corp.google.com [172.28.16.145]) by smtp-out.google.com with ESMTP id n7HHInbP002261 for ; Mon, 17 Aug 2009 18:18:49 +0100 Received: from rv-out-0506.google.com (rvbl9.prod.google.com [10.140.88.9]) by spaceape11.eur.corp.google.com with ESMTP id n7HHIaGD024686 for ; Mon, 17 Aug 2009 10:18:46 -0700 Received: by rv-out-0506.google.com with SMTP id l9so766722rvb.23 for ; Mon, 17 Aug 2009 10:18:46 -0700 (PDT) In-Reply-To: <1250129059-20998-1-git-send-email-lmr@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: I'll just make a few general style comments... On Wed, Aug 12, 2009 at 7:04 PM, Lucas Meneghel Rodrigues wrote: > 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: > > =A0* 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. > > =A0* 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: > > =A0* 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 > > =A0* 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. > > But surely this has a long way to go before we can consider it > for inclusion on autotest. > > Signed-off-by: Lucas Meneghel Rodrigues > --- > =A0client/common_lib/test.py =A0 =A0 | =A0 59 ++++++++++++- > =A0client/tools/crash_handler.py | =A0197 +++++++++++++++++++++++++++= ++++++++++++++ > =A02 files changed, 254 insertions(+), 2 deletions(-) > =A0create mode 100755 client/tools/crash_handler.py > > diff --git a/client/common_lib/test.py b/client/common_lib/test.py > index 362c960..f519bfe 100644 > --- a/client/common_lib/test.py > +++ b/client/common_lib/test.py > @@ -17,7 +17,7 @@ > =A0# =A0 =A0 =A0 tmpdir =A0 =A0 =A0 =A0 =A0eg. tmp/_ > > =A0import fcntl, os, re, sys, shutil, tarfile, tempfile, time, traceb= ack > -import warnings, logging > +import warnings, logging, glob > > =A0from autotest_lib.client.common_lib import error > =A0from autotest_lib.client.bin import utils > @@ -31,7 +31,6 @@ class base_test: > =A0 =A0 =A0 =A0 self.job =3D job > =A0 =A0 =A0 =A0 self.pkgmgr =3D job.pkgmgr > =A0 =A0 =A0 =A0 self.autodir =3D job.autodir > - > =A0 =A0 =A0 =A0 self.outputdir =3D outputdir > =A0 =A0 =A0 =A0 self.tagged_testname =3D os.path.basename(self.output= dir) > =A0 =A0 =A0 =A0 self.resultsdir =3D os.path.join(self.outputdir, 'res= ults') > @@ -40,6 +39,7 @@ class base_test: > =A0 =A0 =A0 =A0 os.mkdir(self.profdir) > =A0 =A0 =A0 =A0 self.debugdir =3D os.path.join(self.outputdir, 'debug= ') > =A0 =A0 =A0 =A0 os.mkdir(self.debugdir) > + =A0 =A0 =A0 =A0self.configure_crash_handler() > =A0 =A0 =A0 =A0 self.bindir =3D bindir > =A0 =A0 =A0 =A0 if hasattr(job, 'libdir'): > =A0 =A0 =A0 =A0 =A0 =A0 self.libdir =3D job.libdir > @@ -54,6 +54,45 @@ class base_test: > =A0 =A0 =A0 =A0 self.after_iteration_hooks =3D [] > > > + =A0 =A0def configure_crash_handler(self): > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0Configure the crash handler by: > + =A0 =A0 =A0 =A0 * Setting up core size to unlimited > + =A0 =A0 =A0 =A0 * Putting an appropriate crash handler on /proc/sys= /kernel/core_pattern > + =A0 =A0 =A0 =A0 * Create files that the crash handler will use to f= igure which tests > + =A0 =A0 =A0 =A0 =A0 are active at a given moment > + > + =A0 =A0 =A0 =A0The crash handler will pick up the core file and wri= te it to > + =A0 =A0 =A0 =A0self.debugdir, and perform analysis on it to generat= e a report. The > + =A0 =A0 =A0 =A0program also outputs some results to syslog. > + > + =A0 =A0 =A0 =A0If multiple tests are running, an attempt to verify = if we still have > + =A0 =A0 =A0 =A0the old PID on the system process table to determine= whether it is a > + =A0 =A0 =A0 =A0parent of the current test execution. If we can't de= termine it, the > + =A0 =A0 =A0 =A0core file and the report file will be copied to all = test debug dirs. > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0self.pattern_file =3D '/proc/sys/kernel/core_pattern= ' > + =A0 =A0 =A0 =A0try: > + =A0 =A0 =A0 =A0 =A0 =A0# Trying to backup core pattern and register= our script > + =A0 =A0 =A0 =A0 =A0 =A0self.core_pattern_backup =3D open(self.patte= rn_file, 'r').read() > + =A0 =A0 =A0 =A0 =A0 =A0pattern_fd =3D open(self.pattern_file, 'w') I don't think it's a good idea to call file objects "fd". The os module actually exposes methods for working directly with file descriptors (which we use sometimes) so it can be confusing to read code that refers to file objects as fd...part of my brain keeps thinking "why isn't he using os.write if this is a file descriptor?" > + =A0 =A0 =A0 =A0 =A0 =A0tools_dir =3D os.path.join(self.autodir, 'to= ols') > + =A0 =A0 =A0 =A0 =A0 =A0crash_handler_path =3D os.path.join(tools_di= r, 'crash_handler.py') > + =A0 =A0 =A0 =A0 =A0 =A0pattern_fd.write('|' + crash_handler_path + = ' %p %t %u %s %h %e') > + =A0 =A0 =A0 =A0 =A0 =A0# Writing the files that the crash handler i= s going to use > + =A0 =A0 =A0 =A0 =A0 =A0self.debugdir_tmp_file =3D ('/tmp/autotest_r= esults_dir.%s' % > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0os.getpid()) > + =A0 =A0 =A0 =A0 =A0 =A0debugdir_fd =3D open(self.debugdir_tmp_file,= 'w') > + =A0 =A0 =A0 =A0 =A0 =A0debugdir_fd.write(self.debugdir + "\n") > + =A0 =A0 =A0 =A0 =A0 =A0debugdir_fd.close() There's actually a utils method for doing this cleanly called open_writ= e_close. > + =A0 =A0 =A0 =A0 =A0 =A0# Now we can consider the system initialized= =2E > + =A0 =A0 =A0 =A0 =A0 =A0self.crash_handling_enabled =3D True > + =A0 =A0 =A0 =A0 =A0 =A0logging.debug('Crash handling system enabled= =2E') Maybe this little snippet should be in an else clause, instead of inside the try? Personally I think it makes it clearer to have "the try part of the block didn't fail" in an else instead of at the end of the try, and it has more symmetry with the except clause. > + =A0 =A0 =A0 =A0except Exception, e: > + =A0 =A0 =A0 =A0 =A0 =A0logging.debug('Crash handling system disable= d: %s' % e) > + =A0 =A0 =A0 =A0 =A0 =A0self.crash_handling_enabled =3D False I would set the flag before calling logging, like for the success case. Also, you should pass e in as a parameter to debug rather than doing the % formatting yourself. Maybe that should also be logging.exception? Although that will raise the visibility of the log, I suppose if you expect this to fail in "normal" cases then that wouldn't be a good idea. > + > + > =A0 =A0 def assert_(self, expr, msg=3D'Assertion failed.'): > =A0 =A0 =A0 =A0 if not expr: > =A0 =A0 =A0 =A0 =A0 =A0 raise error.TestError(msg) > @@ -403,6 +442,22 @@ class base_test: > =A0 =A0 =A0 =A0 else: > =A0 =A0 =A0 =A0 =A0 =A0 if self.network_destabilizing: > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 self.job.enable_warnings("NETWORK") > + =A0 =A0 =A0 =A0# If core dumps are found on the debugdir after the = execution of the > + =A0 =A0 =A0 =A0# test, let the user know. > + =A0 =A0 =A0 =A0if self.crash_handling_enabled: > + =A0 =A0 =A0 =A0 =A0 =A0core_dirs =3D glob.glob('%s/core.*' % self.d= ebugdir) > + =A0 =A0 =A0 =A0 =A0 =A0if core_dirs: > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logging.warning('Programs crashed du= ring test execution:') > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0for dir in core_dirs: > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logging.warning('Please veri= fy %s for more info' % dir) Again, pass in dir as a parameter to logging.warning, don't do % formatting yourself. > + =A0 =A0 =A0 =A0 =A0 =A0# Remove the debugdir info file > + =A0 =A0 =A0 =A0 =A0 =A0os.unlink(self.debugdir_tmp_file) > + =A0 =A0 =A0 =A0 =A0 =A0# Restore the core pattern backup > + =A0 =A0 =A0 =A0 =A0 =A0try: > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0pattern_fd =3D open(self.pattern_fil= e, 'w') > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0pattern_fd.write(self.core_pattern_b= ackup) > + =A0 =A0 =A0 =A0 =A0 =A0except: > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0pass I think open_write_close works here as well, although you'd still need the try-except too. Any reason you can't just go with a more specific exception type? Maybe just catching EnvironmentError? > > > =A0def _get_nonstar_args(func): > diff --git a/client/tools/crash_handler.py b/client/tools/crash_handl= er.py > new file mode 100755 > index 0000000..a0d62a7 > --- /dev/null > +++ b/client/tools/crash_handler.py > @@ -0,0 +1,197 @@ > +#!/usr/bin/python > +""" > +Simple crash handling application for autotest > + > +@copyright Red Hat Inc 2009 > +@author Lucas Meneghel Rodrigues > +""" > +import sys, os, commands, glob, tempfile, shutil, syslog > + > + > +def get_parent_pid(pid): Personally, I think using /proc/$pid/stat would be nicer than calling p= s. > + =A0 =A0""" > + =A0 =A0Returns the parent PID for a given PID, converted to an inte= ger. > + > + =A0 =A0@param pid: Process ID. > + =A0 =A0""" > + =A0 =A0try: > + =A0 =A0 =A0 =A0ppid =3D int(commands.getoutput("ps --pid=3D%s -o pp= id=3D" % pid).split()[0]) > + =A0 =A0except IndexError: > + =A0 =A0 =A0 =A0# It is not possible to determine the parent because= the process > + =A0 =A0 =A0 =A0# already left the process table. > + =A0 =A0 =A0 =A0ppid =3D 1 > + > + =A0 =A0return ppid > + > + > +def pid_descends_from(pid_a, pid_b): > + =A0 =A0""" > + =A0 =A0Check whether pid_a descends from pid_b. > + > + =A0 =A0@param pid_a: Process ID. > + =A0 =A0@param pid_b: Process ID. > + =A0 =A0""" > + =A0 =A0pid_a =3D int(pid_a) > + =A0 =A0pid_b =3D int(pid_b) > + =A0 =A0current_pid =3D pid_a > + =A0 =A0while current_pid > 1: > + =A0 =A0 =A0 =A0if current_pid =3D=3D pid_b: > + =A0 =A0 =A0 =A0 =A0 =A0syslog.syslog(syslog.LOG_INFO, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"PID %s descends= from PID %s!" % (pid_a, pid_b)) > + =A0 =A0 =A0 =A0 =A0 =A0return True > + =A0 =A0 =A0 =A0else: > + =A0 =A0 =A0 =A0 =A0 =A0current_pid =3D get_parent_pid(current_pid) > + =A0 =A0syslog.syslog(syslog.LOG_INFO, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"PID %s does not descend from PI= D %s" % (pid_a, pid_b)) > + =A0 =A0return False > + > + > +def write_to_file(file_path, contents): I would steal the open_write_close implementation from the utils. Although in practice it's not a big difference. > + =A0 =A0""" > + =A0 =A0Write contents to a given file path specified. If not specif= ied, the file > + =A0 =A0will be created. > + > + =A0 =A0@param file_path: Path to a given file. > + =A0 =A0@param contents: File contents. > + =A0 =A0""" > + =A0 =A0file_fd =3D open(file_path, 'w') > + =A0 =A0file_fd.write(contents) > + =A0 =A0file_fd.close() > + > + > +def get_results_dir_list(pid, core_dir_basename): > + =A0 =A0""" > + =A0 =A0Get all valid output directories for the core file and the r= eport. It works > + =A0 =A0by inspecting files created by each test on /tmp and verifyi= ng if the > + =A0 =A0PID of the process that crashed is a child or grandchild of = the autotest > + =A0 =A0test process. If it can't find any relationship (maybe a dae= mon that died > + =A0 =A0during a test execution), it will write the core file to the= debug dirs > + =A0 =A0of all tests currently being executed. If there are no activ= e autotest > + =A0 =A0tests at a particular moment, it will return a list with ['/= tmp']. > + > + =A0 =A0@param pid: PID for the process that generated the core > + =A0 =A0@param core_dir_basename: Basename for the directory that wi= ll hold both the > + =A0 =A0 =A0 =A0 =A0 =A0core dump and the crash report. > + =A0 =A0""" > + =A0 =A0# Get all active test debugdir path files present > + =A0 =A0debugdir_files =3D glob.glob("/tmp/autotest_results_dir.*") > + =A0 =A0if debugdir_files: > + =A0 =A0 =A0 =A0pid_dir_dict =3D {} > + =A0 =A0 =A0 =A0for debugdir_file in debugdir_files: > + =A0 =A0 =A0 =A0 =A0 =A0a_pid =3D debugdir_file.split('.')[-1] > + =A0 =A0 =A0 =A0 =A0 =A0results_dir =3D open(debugdir_file, 'r').rea= d().strip() > + =A0 =A0 =A0 =A0 =A0 =A0pid_dir_dict[a_pid] =3D os.path.join(results= _dir, core_dir_basename) > + > + =A0 =A0 =A0 =A0results_dir_list =3D [] > + =A0 =A0 =A0 =A0found_relation =3D False > + =A0 =A0 =A0 =A0for a_pid in pid_dir_dict.keys(): for a_pid, a_path in pid_dir_dict.iteritems() Then you don't need to do the lookup inside the if, you can just use a_= path. > + =A0 =A0 =A0 =A0 =A0 =A0if pid_descends_from(pid, a_pid): > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0results_dir_list.append(pid_dir_dict= [a_pid]) > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0found_relation =3D True > + > + =A0 =A0 =A0 =A0# If we could not find any relations between the pid= s in the list with > + =A0 =A0 =A0 =A0# the process that crashed, we can't tell for sure w= hich tested spawned > + =A0 =A0 =A0 =A0# the process (maybe it is a daemon and started even= before autotest > + =A0 =A0 =A0 =A0# started), so we will have to output the core file = to all active test > + =A0 =A0 =A0 =A0# directories. > + =A0 =A0 =A0 =A0if not found_relation: > + =A0 =A0 =A0 =A0 =A0 =A0return [pid_dir_dict[d] for d in pid_dir_dic= t.keys()] return pid_dir_dict.values() > + =A0 =A0 =A0 =A0else: > + =A0 =A0 =A0 =A0 =A0 =A0return results_dir_list > + > + =A0 =A0else: > + =A0 =A0 =A0 =A0path_inactive_autotest =3D os.path.join('/tmp', core= _dir_basename) > + =A0 =A0 =A0 =A0return [path_inactive_autotest] > + > + > +def get_info_from_core(path): > + =A0 =A0""" > + =A0 =A0Reads a core file and extracts a dictionary with useful core= information. > + =A0 =A0Right now, the only information extracted is the full execut= able name. > + > + =A0 =A0@param path: Path to core file. > + =A0 =A0""" > + =A0 =A0# Here we are getting the executable full path in a very ine= legant way :( > + =A0 =A0# Since the 'right' solution for it is to make a library to = get information > + =A0 =A0# from core dump files, properly written, I'll leave this as= it is for now. > + =A0 =A0full_exe_path =3D commands.getoutput('strings %s | grep "_=3D= "' % > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 path).strip("_=3D") > + =A0 =A0if full_exe_path.startswith("./"): > + =A0 =A0 =A0 =A0pwd =3D commands.getoutput('strings %s | grep "^PWD=3D= "' % > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 pat= h).strip("PWD=3D") > + =A0 =A0 =A0 =A0full_exe_path =3D os.path.join(pwd, full_exe_path.st= rip("./")) > + > + =A0 =A0return {'core_file': path, 'full_exe_path': full_exe_path} > + > + > +if __name__ =3D=3D "__main__": > + =A0 =A0syslog.openlog('AutotestCrashHandler', 0, syslog.LOG_DAEMON) > + =A0 =A0(crashed_pid, time, uid, signal, hostname, exe) =3D sys.argv= [1:] > + =A0 =A0core_name =3D 'core' > + =A0 =A0report_name =3D 'report' > + =A0 =A0core_dir_name =3D 'crash.%s.%s' % (exe, crashed_pid) > + =A0 =A0core_tmp_dir =3D tempfile.mkdtemp(prefix=3D'core_', dir=3D'/= tmp') > + =A0 =A0core_tmp_path =3D os.path.join(core_tmp_dir, core_name) > + =A0 =A0gdb_command_path =3D os.path.join(core_tmp_dir, 'gdb_command= ') > + > + =A0 =A0# Get the filtered results dir list > + =A0 =A0current_results_dir_list =3D get_results_dir_list(crashed_pi= d, core_dir_name) > + > + =A0 =A0# Write the core file to the appropriate directory > + =A0 =A0# (we are piping it to this script) > + =A0 =A0core_file =3D sys.stdin.read() > + =A0 =A0write_to_file(core_tmp_path, core_file) > + > + =A0 =A0# Write a command file for GDB > + =A0 =A0gdb_command =3D 'bt full\n' > + =A0 =A0write_to_file(gdb_command_path, gdb_command) > + > + =A0 =A0# Get full command path > + =A0 =A0exe_path =3D get_info_from_core(core_tmp_path)['full_exe_pat= h'] > + > + =A0 =A0# Take a backtrace from the running program > + =A0 =A0gdb_cmd =3D 'gdb -e %s -c %s -x %s -n -batch -quiet' % (exe_= path, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0core_tmp_path, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0gdb_command_path) > + =A0 =A0backtrace =3D commands.getoutput(gdb_cmd) > + =A0 =A0# Sanitize output before passing it to the report > + =A0 =A0backtrace =3D backtrace.decode('utf-8', 'ignore') > + > + =A0 =A0# Composing the format_dict > + =A0 =A0format_dict =3D {} > + =A0 =A0format_dict['program'] =3D exe_path > + =A0 =A0format_dict['pid'] =3D crashed_pid > + =A0 =A0format_dict['signal'] =3D signal > + =A0 =A0format_dict['hostname'] =3D hostname > + =A0 =A0format_dict['time'] =3D time > + =A0 =A0format_dict['backtrace'] =3D backtrace > + > + =A0 =A0report =3D """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 > + > + =A0 =A0syslog.syslog(syslog.LOG_INFO, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"Application %s, PID %s crashed"= % (exe_path, crashed_pid)) > + > + =A0 =A0# Now, for all results dir, let's create the directory if it= doesn't exist, > + =A0 =A0# and write the core file and the report to it. > + =A0 =A0syslog.syslog(syslog.LOG_INFO, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"Writing core files and reports = to %s" % > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0current_results_dir_list) > + =A0 =A0for result_dir in current_results_dir_list: > + =A0 =A0 =A0 =A0if not os.path.isdir(result_dir): > + =A0 =A0 =A0 =A0 =A0 =A0os.makedirs(result_dir) > + =A0 =A0 =A0 =A0core_path =3D os.path.join(result_dir, 'core') > + =A0 =A0 =A0 =A0write_to_file(core_path, core_file) > + =A0 =A0 =A0 =A0report_path =3D os.path.join(result_dir, 'report') > + =A0 =A0 =A0 =A0write_to_file(report_path, report) > + =A0 =A0# Cleanup temporary directories > + =A0 =A0shutil.rmtree(core_tmp_dir) Maybe this rmtree should be in a finally block; it's painful when tmpdirs only get cleaned up when things succeed. > + > -- > 1.6.2.5 > >