[Lldb-commits] [lldb] 6b26e1d - [lldb/crashlog] Refactor CrashLogParser into a Factory pattern

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Thu Jan 12 19:21:13 PST 2023


Author: Med Ismail Bennani
Date: 2023-01-12T19:20:51-08:00
New Revision: 6b26e1dad9608ec188446dcdc0734a7b41cf145b

URL: https://github.com/llvm/llvm-project/commit/6b26e1dad9608ec188446dcdc0734a7b41cf145b
DIFF: https://github.com/llvm/llvm-project/commit/6b26e1dad9608ec188446dcdc0734a7b41cf145b.diff

LOG: [lldb/crashlog] Refactor CrashLogParser into a Factory pattern

This patch should fix an undefined behaviour that's happening when
parsing a crash report from an IDE. In the previous implementation, the
CrashLogParser base class would use the `__new__` static class method to
create the right parser instance depending on the crash report type.

For some reasons, the derived parser initializer wouldn't be called when
running the command from an IDE, so this patch refactors the
CrashLogParser code to replace the use of the `__new__` method with a
factory `create` static method.

rdar://100527640

Differential Revision: https://reviews.llvm.org/D139951

Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>

Added: 
    

Modified: 
    lldb/examples/python/crashlog.py
    lldb/examples/python/scripted_process/crashlog_scripted_process.py

Removed: 
    


################################################################################
diff  --git a/lldb/examples/python/crashlog.py b/lldb/examples/python/crashlog.py
index b62ebd758f22..d9e037057e67 100755
--- a/lldb/examples/python/crashlog.py
+++ b/lldb/examples/python/crashlog.py
@@ -417,15 +417,15 @@ class InteractiveCrashLogException(Exception):
     pass
 
 class CrashLogParser:
-    "CrashLog parser base class and factory."
-    def __new__(cls, debugger, path, verbose):
+    @staticmethod
+    def create(debugger, path, verbose):
         data = JSONCrashLogParser.is_valid_json(path)
         if data:
-            self = object.__new__(JSONCrashLogParser)
-            self.data = data
-            return self
+            parser = JSONCrashLogParser(debugger, path, verbose)
+            parser.data = data
+            return parser
         else:
-            return object.__new__(TextCrashLogParser)
+            return TextCrashLogParser(debugger, path, verbose)
 
     def __init__(self, debugger, path, verbose):
         self.path = os.path.expanduser(path)
@@ -1076,7 +1076,7 @@ def load_crashlog_in_scripted_process(debugger, crash_log_file, options, result)
     if not os.path.exists(crashlog_path):
         raise InteractiveCrashLogException("crashlog file %s does not exist" % crashlog_path)
 
-    crashlog = CrashLogParser(debugger, crashlog_path, False).parse()
+    crashlog = CrashLogParser.create(debugger, crashlog_path, False).parse()
 
     target = lldb.SBTarget()
     # 1. Try to use the user-provided target
@@ -1332,7 +1332,7 @@ def should_run_in_interactive_mode(options, ci):
                 except InteractiveCrashLogException as e:
                     result.SetError(str(e))
             else:
-                crash_log = CrashLogParser(debugger, crash_log_file, options.verbose).parse()
+                crash_log = CrashLogParser.create(debugger, crash_log_file, options.verbose).parse()
                 SymbolicateCrashLog(crash_log, options)
 
 if __name__ == '__main__':

diff  --git a/lldb/examples/python/scripted_process/crashlog_scripted_process.py b/lldb/examples/python/scripted_process/crashlog_scripted_process.py
index eddf96786d9e..dfb32aae6d3f 100644
--- a/lldb/examples/python/scripted_process/crashlog_scripted_process.py
+++ b/lldb/examples/python/scripted_process/crashlog_scripted_process.py
@@ -10,7 +10,7 @@
 
 class CrashLogScriptedProcess(ScriptedProcess):
     def parse_crashlog(self):
-        crashlog_parser = CrashLogParser(self.dbg, self.crashlog_path, False)
+        crashlog_parser = CrashLogParser.create(self.dbg, self.crashlog_path, False)
         crash_log = crashlog_parser.parse()
 
         self.pid = crash_log.process_id


        


More information about the lldb-commits mailing list