From a7d3bcd8d045fb550ae76d41365e02109d5d7b2f Mon Sep 17 00:00:00 2001
From: Laura Abbott <lauraa@codeaurora.org>
Date: Mon, 5 May 2014 15:54:30 -0700
Subject: [PATCH] linux-ramdump-parser-v2: Catch parsing exceptions

For reasons ranging from mangled dumps to bugs in code, the parser may
generate exceptions while parsing. Rather than terminate the entire
script, handle exceptions in a more graceful fashion.

Change-Id: I883ee9c0910802b00c797380af5b21e00990dce8
---
 linux-ramdump-parser-v2/print_out.py | 6 ++++++
 linux-ramdump-parser-v2/ramparse.py  | 8 ++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/linux-ramdump-parser-v2/print_out.py b/linux-ramdump-parser-v2/print_out.py
index 51370a7..9a2698e 100644
--- a/linux-ramdump-parser-v2/print_out.py
+++ b/linux-ramdump-parser-v2/print_out.py
@@ -9,6 +9,7 @@
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 import sys
+import traceback
 from contextlib import contextmanager
 
 out_file = None
@@ -30,6 +31,11 @@ def print_out_str(string):
     else:
         out_file.write((string + '\n').encode('ascii', 'ignore'))
 
+def print_out_exception():
+    if out_file is None:
+        traceback.print_exc(file=sys.stdout)
+    else:
+        traceback.print_exc(file=out_file)
 
 @contextmanager
 def print_out_section(header):
diff --git a/linux-ramdump-parser-v2/ramparse.py b/linux-ramdump-parser-v2/ramparse.py
index 7caf609..2a53efd 100755
--- a/linux-ramdump-parser-v2/ramparse.py
+++ b/linux-ramdump-parser-v2/ramparse.py
@@ -25,7 +25,7 @@ from optparse import OptionParser
 
 import parser_util
 from ramdump import RamDump
-from print_out import print_out_str, set_outfile, print_out_section
+from print_out import print_out_str, set_outfile, print_out_section, print_out_exception
 
 # Please update version when something is changed!'
 VERSION = '2.0'
@@ -306,7 +306,11 @@ if __name__ == '__main__':
         # p.cls.__name__ attribute.
         if getattr(options, p.cls.__name__) or (options.everything and not p.optional):
             with print_out_section(p.cls.__name__):
-                p.cls(dump).parse()
+                try:
+                    p.cls(dump).parse()
+                except:
+                    print_out_str('!!! Exception while running {0}'.format(p.cls.__name__))
+                    print_out_exception()
 
     if options.t32launcher or options.everything:
         dump.create_t32_launcher()
-- 
GitLab