From: Lucas Meneghel Rodrigues Subject: Re: [Autotest] [PATCH] Adding a userspace application crash handling system to autotest Date: Tue, 15 Sep 2009 18:05:19 -0300 Message-ID: <6ac58f4f0909151405k6b23b6c6v84926e15fac8226b@mail.gmail.com> References: <1527767282.40681252922562180.JavaMail.root@zmail05.collab.prod.int.phx2.redhat.com> <594092660.42171252923823785.JavaMail.root@zmail05.collab.prod.int.phx2.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 To: Michael Goldish Return-path: Received: from mail-vw0-f195.google.com ([209.85.212.195]:41302 "EHLO mail-vw0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751691AbZIOVFQ convert rfc822-to-8bit (ORCPT ); Tue, 15 Sep 2009 17:05:16 -0400 Received: by vws33 with SMTP id 33so1380587vws.33 for ; Tue, 15 Sep 2009 14:05:19 -0700 (PDT) In-Reply-To: <594092660.42171252923823785.JavaMail.root@zmail05.collab.prod.int.phx2.redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On Mon, Sep 14, 2009 at 7:23 AM, Michael Goldish = wrote: > I think this is a very useful feature to have. > > Please see some very minor comments below. > > ----- "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. >> >> 3rd try: >> =A0* Explicitely enable core dumps using the resource module >> =A0* Fixed a bug on the crash detection code, and factored >> =A0 =A0it into a utility function. >> >> I believe we are good to go now. >> >> Signed-off-by: Lucas Meneghel Rodrigues >> --- >> =A0client/common_lib/test.py =A0 =A0 | =A0 66 +++++++++++++- >> =A0client/tools/crash_handler.py | =A0202 >> +++++++++++++++++++++++++++++++++++++++++ >> =A02 files changed, 266 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..65b78a3 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, >> traceback >> -import warnings, logging >> +import warnings, logging, glob, resource >> >> =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 =A0self.job =3D job >> =A0 =A0 =A0 =A0 =A0self.pkgmgr =3D job.pkgmgr >> =A0 =A0 =A0 =A0 =A0self.autodir =3D job.autodir >> - >> =A0 =A0 =A0 =A0 =A0self.outputdir =3D outputdir >> =A0 =A0 =A0 =A0 =A0self.tagged_testname =3D os.path.basename(self.ou= tputdir) >> =A0 =A0 =A0 =A0 =A0self.resultsdir =3D os.path.join(self.outputdir, = 'results') >> @@ -40,6 +39,7 @@ class base_test: >> =A0 =A0 =A0 =A0 =A0os.mkdir(self.profdir) >> =A0 =A0 =A0 =A0 =A0self.debugdir =3D os.path.join(self.outputdir, 'd= ebug') >> =A0 =A0 =A0 =A0 =A0os.mkdir(self.debugdir) >> + =A0 =A0 =A0 =A0self.configure_crash_handler() >> =A0 =A0 =A0 =A0 =A0self.bindir =3D bindir >> =A0 =A0 =A0 =A0 =A0if hasattr(job, 'libdir'): >> =A0 =A0 =A0 =A0 =A0 =A0 =A0self.libdir =3D job.libdir >> @@ -54,6 +54,66 @@ class base_test: >> =A0 =A0 =A0 =A0 =A0self.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 = figure >> 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 wr= ite it to >> + =A0 =A0 =A0 =A0self.debugdir, and perform analysis on it to genera= te 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 determin= e whether >> it is a >> + =A0 =A0 =A0 =A0parent of the current test execution. If we can't d= etermine >> 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_patter= n' >> + =A0 =A0 =A0 =A0try: >> + =A0 =A0 =A0 =A0 =A0 =A0# Enable core dumps >> + =A0 =A0 =A0 =A0 =A0 =A0resource.setrlimit(resource.RLIMIT_CORE, (-= 1, -1)) >> + =A0 =A0 =A0 =A0 =A0 =A0# Trying to backup core pattern and registe= r our script >> + =A0 =A0 =A0 =A0 =A0 =A0self.core_pattern_backup =3D open(self.patt= ern_file, >> 'r').read() >> + =A0 =A0 =A0 =A0 =A0 =A0pattern_file =3D open(self.pattern_file, 'w= ') >> + =A0 =A0 =A0 =A0 =A0 =A0tools_dir =3D os.path.join(self.autodir, 't= ools') >> + =A0 =A0 =A0 =A0 =A0 =A0crash_handler_path =3D os.path.join(tools_d= ir, >> 'crash_handler.py') >> + =A0 =A0 =A0 =A0 =A0 =A0pattern_file.write('|' + crash_handler_path= + ' %p %t %u >> %s %h %e') >> + =A0 =A0 =A0 =A0 =A0 =A0# Writing the files that the crash handler = is going to >> use >> + =A0 =A0 =A0 =A0 =A0 =A0self.debugdir_tmp_file =3D ('/tmp/autotest_= results_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 =A0utils.open_write_close(self.debugdir_tmp_fi= le, >> self.debugdir + "\n") >> + =A0 =A0 =A0 =A0except Exception, e: >> + =A0 =A0 =A0 =A0 =A0 =A0self.crash_handling_enabled =3D False >> + =A0 =A0 =A0 =A0 =A0 =A0logging.error('Crash handling system disabl= ed: %s' % e) >> + =A0 =A0 =A0 =A0else: >> + =A0 =A0 =A0 =A0 =A0 =A0self.crash_handling_enabled =3D True >> + =A0 =A0 =A0 =A0 =A0 =A0logging.debug('Crash handling system enable= d.') >> + >> + >> + =A0 =A0def crash_handler_report(self): >> + =A0 =A0 =A0 =A0""" >> + =A0 =A0 =A0 =A0If core dumps are found on the debugdir after the e= xecution >> of the >> + =A0 =A0 =A0 =A0test, let the user know. >> + =A0 =A0 =A0 =A0""" >> + =A0 =A0 =A0 =A0if self.crash_handling_enabled: >> + =A0 =A0 =A0 =A0 =A0 =A0core_dirs =3D glob.glob('%s/crash.*' % self= =2Edebugdir) >> + =A0 =A0 =A0 =A0 =A0 =A0if core_dirs: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logging.warning('Programs crashed d= uring 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 ver= ify %s for more info', >> dir) >> + =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 =A0utils.open_write_close(self.pattern= _file, >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 self.core_pattern_backup) >> + =A0 =A0 =A0 =A0 =A0 =A0except EnvironmentError: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0pass >> + >> + >> =A0 =A0 =A0def assert_(self, expr, msg=3D'Assertion failed.'): >> =A0 =A0 =A0 =A0 =A0if not expr: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0raise error.TestError(msg) >> @@ -377,6 +437,7 @@ class base_test: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0traceback.print_e= xc() >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0print 'Now raisin= g the earlier %s error' % >> exc_info[0] >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0finally: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0self.crash_handler_report() >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0self.job.logging.restore(= ) >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0try: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0raise exc_info[0]= , exc_info[1], exc_info[2] >> @@ -389,6 +450,7 @@ class base_test: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if run_cleanup: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0_cherry_pick_call= (self.cleanup, *args, >> **dargs) >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0finally: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0self.crash_handler_report() >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0self.job.logging.restore(= ) >> =A0 =A0 =A0 =A0 =A0except error.AutotestError: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0if self.network_destabilizing: >> diff --git a/client/tools/crash_handler.py >> b/client/tools/crash_handler.py >> new file mode 100755 >> index 0000000..e281eb5 >> --- /dev/null >> +++ b/client/tools/crash_handler.py >> @@ -0,0 +1,202 @@ >> +#!/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): >> + =A0 =A0""" >> + =A0 =A0Returns the parent PID for a given PID, converted to an int= eger. >> + >> + =A0 =A0@param pid: Process ID. >> + =A0 =A0""" >> + =A0 =A0try: >> + =A0 =A0 =A0 =A0stat_file_contents =3D open('/proc/%s/stat' % pid, >> 'r').readline() >> + =A0 =A0 =A0 =A0ppid =3D 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! > The two lines can be combined into a rather short one, BTW: > > ppid =3D 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. >> + =A0 =A0except: >> + =A0 =A0 =A0 =A0# It is not possible to determine the parent becaus= e 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 descend= s 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 P= ID %s" % (pid_a, >> pid_b)) >> + =A0 =A0return False >> + >> + >> +def write_to_file(file_path, contents): >> + =A0 =A0""" >> + =A0 =A0Write contents to a given file path specified. If not speci= fied, >> 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_object =3D open(file_path, 'w') >> + =A0 =A0file_object.write(contents) >> + =A0 =A0file_object.close() >> + >> + >> +def get_results_dir_list(pid, core_dir_basename): >> + =A0 =A0""" >> + =A0 =A0Get all valid output directories for the core file and the >> report. It works >> + =A0 =A0by inspecting files created by each test on /tmp and verify= ing 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 da= emon >> that died >> + =A0 =A0during a test execution), it will write the core file to th= e >> debug dirs >> + =A0 =A0of all tests currently being executed. If there are no acti= ve >> 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 w= ill >> hold both >> + =A0 =A0 =A0 =A0 =A0 =A0the core 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').re= ad().strip() >> + =A0 =A0 =A0 =A0 =A0 =A0pid_dir_dict[a_pid] =3D os.path.join(result= s_dir, >> core_dir_basename) >> + >> + =A0 =A0 =A0 =A0results_dir_list =3D [] >> + =A0 =A0 =A0 =A0found_relation =3D False >> + =A0 =A0 =A0 =A0for a_pid, a_path in pid_dir_dict.iteritems(): >> + =A0 =A0 =A0 =A0 =A0 =A0if pid_descends_from(pid, a_pid): >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0results_dir_list.append(a_path) >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0found_relation =3D True >> + >> + =A0 =A0 =A0 =A0# If we could not find any relations between the pi= ds in the >> list with >> + =A0 =A0 =A0 =A0# the process that crashed, we can't tell for sure = which test >> spawned >> + =A0 =A0 =A0 =A0# the process (maybe it is a daemon and started eve= n 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.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] > > Here's a slightly shorter implementation of this function that doesn'= t > need pid_descends_from(): > > pid_dir_dict =3D {} > for debugdir_file in glob.glob("/tmp/autotest_results_dir.*"): > =A0 =A0a_pid =3D os.path.splitext(debugdir_file)[1] > =A0 =A0results_dir =3D open(debugdir_file).read().strip() > =A0 =A0pid_dir_dict[a_pid] =3D os.path.join(results_dir, core_dir_bas= ename) > > results_dir_list =3D [] > while pid > 1: > =A0 =A0if pid_dir_dict.has_key(pid): > =A0 =A0 =A0 =A0results_dir_list.append(pid_dir_dict[pid]) > =A0 =A0pid =3D get_parent_pid(pid) > > return (results_dir_list or > =A0 =A0 =A0 =A0pid_dir_dict.values() or > =A0 =A0 =A0 =A0[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. >> +def get_info_from_core(path): >> + =A0 =A0""" >> + =A0 =A0Reads a core file and extracts a dictionary with useful cor= e >> information. >> + =A0 =A0Right now, the only information extracted is the full execu= table >> name. >> + >> + =A0 =A0@param path: Path to core file. >> + =A0 =A0""" >> + =A0 =A0# Here we are getting the executable full path in a very >> inelegant 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 a= s 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") > > If you think that's unelegant you can try using regular expressions, > but then it might be slightly tricky to match only printable characte= rs. > You can also use filter(lambda x: x in string.printable, str), but th= at > would return all printable strings concatenated, without newlines > separating them (unless there are newlines in the core file itself). > >> + =A0 =A0if 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 _=3D 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 eva= luation! --=20 Lucas