mib created this revision. mib added reviewers: JDevlieghere, kastiglione. mib added a project: LLDB. Herald added a project: All. mib requested review of this revision. Herald added a subscriber: lldb-commits.
This patch changes the CrashLogParser class to be both the base class and a Factory for the JSONCrashLogParser & TextCrashLogParser. That should help remove some code duplication and ensure both class have a parse method. Signed-off-by: Med Ismail Bennani <medismail.benn...@gmail.com> Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D131085 Files: lldb/examples/python/crashlog.py lldb/examples/python/scripted_process/crashlog_scripted_process.py
Index: lldb/examples/python/scripted_process/crashlog_scripted_process.py =================================================================== --- lldb/examples/python/scripted_process/crashlog_scripted_process.py +++ lldb/examples/python/scripted_process/crashlog_scripted_process.py @@ -11,13 +11,14 @@ class CrashLogScriptedProcess(ScriptedProcess): def parse_crashlog(self): try: - crash_log = CrashLogParser().parse(self.dbg, self.crashlog_path, False) + crashlog_parser = CrashLogParser(self.dbg, self.crashlog_path, False) + crashlog = crashlog_parser.parse() except Exception as e: raise e - self.pid = crash_log.process_id - self.addr_mask = crash_log.addr_mask - self.crashed_thread_idx = crash_log.crashed_thread_idx + self.pid = crashlog.process_id + self.addr_mask = crashlog.addr_mask + self.crashed_thread_idx = crashlog.crashed_thread_idx self.loaded_images = [] def load_images(self, images): @@ -35,12 +36,12 @@ else: self.loaded_images.append(image) - for thread in crash_log.threads: + for thread in crashlog.threads: if self.load_all_images: - load_images(self, crash_log.images) + load_images(self, crashlog.images) elif thread.did_crash(): for ident in thread.idents: - load_images(self, crash_log.find_images_with_identifier(ident)) + load_images(self, crashlog.find_images_with_identifier(ident)) self.threads[thread.index] = CrashLogScriptedThread(self, None, thread) Index: lldb/examples/python/crashlog.py =================================================================== --- lldb/examples/python/crashlog.py +++ lldb/examples/python/crashlog.py @@ -412,19 +412,28 @@ pass -class CrashLogParser: - def parse(self, debugger, path, verbose): +class CrashLogParser(object): + "Base class and factory." + def __new__(cls, debugger, path, verbose, *args, **kwargs): try: - return JSONCrashLogParser(debugger, path, verbose).parse() + return super(CrashLogParser, cls).__new__(JSONCrashLogParser, *args, **kwargs) except CrashLogFormatException: - return TextCrashLogParser(debugger, path, verbose).parse() + return super(CrashLogParser, cls).__new__(TextCrashLogParser, *args, **kwargs) - -class JSONCrashLogParser: def __init__(self, debugger, path, verbose): self.path = os.path.expanduser(path) self.verbose = verbose self.crashlog = CrashLog(debugger, self.path, self.verbose) + self.data = None + + @abc.abstractmethod + def parse(self): + pass + + +class JSONCrashLogParser(CrashLogParser): + def __init__(self, debugger, path, verbose): + super().__init__(debugger, path, verbose) def parse_json(self, buffer): try: @@ -592,7 +601,7 @@ INSTRS = 5 -class TextCrashLogParser: +class TextCrashLogParser(CrashLogParser): parent_process_regex = re.compile('^Parent Process:\s*(.*)\[(\d+)\]') thread_state_regex = re.compile('^Thread ([0-9]+) crashed with') thread_instrs_regex = re.compile('^Thread ([0-9]+) instruction stream') @@ -617,11 +626,9 @@ def __init__(self, debugger, path, verbose): - self.path = os.path.expanduser(path) - self.verbose = verbose + super().__init__(debugger,path,verbose) self.thread = None self.app_specific_backtrace = False - self.crashlog = CrashLog(debugger, self.path, self.verbose) self.parse_mode = CrashLogParseMode.NORMAL self.parsers = { CrashLogParseMode.NORMAL : self.parse_normal, @@ -1014,7 +1021,7 @@ if not os.path.exists(crashlog_path): raise Exception("crashlog file %s does not exist" % crashlog_path) - crashlog = CrashLogParser().parse(debugger, crashlog_path, False) + crashlog = CrashLogParser(debugger, crashlog_path, False).parse() target = lldb.SBTarget() # 1. Try to use the user-provided target @@ -1261,7 +1268,7 @@ err.SetErrorString(str(e)) result.SetError(err) else: - crash_log = CrashLogParser().parse(debugger, crash_log_file, options.verbose) + crash_log = CrashLogParser(debugger, crash_log_file, options.verbose).parse() SymbolicateCrashLog(crash_log, options, result) if __name__ == '__main__':
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits