[lldb/crashlog] Remove tempfile prefix from inlined symbol object file
authorMed Ismail Bennani <ismail@bennani.ma>
Mon, 22 May 2023 22:51:43 +0000 (15:51 -0700)
committerMed Ismail Bennani <ismail@bennani.ma>
Mon, 22 May 2023 23:14:00 +0000 (16:14 -0700)
This patch changes the way we generate the ObjectFileJSON files
containing the inlined symbols from the crash report to remove the
tempfile prefix from the object file name.

To do so, instead of creating a new tempfile for each module, we create a
temporary directory that contains each module object file with the same
name as the module.

This makes the backtraces only contain the module name without the
temfile prefix which makes it look like a regular stackframe.

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

Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
lldb/examples/python/crashlog.py
lldb/examples/python/scripted_process/crashlog_scripted_process.py
lldb/examples/python/symbolication.py

index d207af1..7bfa402 100755 (executable)
@@ -40,6 +40,7 @@ import shlex
 import string
 import subprocess
 import sys
+import tempfile
 import threading
 import time
 import uuid
@@ -1154,12 +1155,17 @@ def SymbolicateCrashLog(crash_log, options):
     futures = []
     loaded_images = []
     with concurrent.futures.ThreadPoolExecutor() as executor:
-        def add_module(image, target):
-            return image, image.add_module(target)
+        with tempfile.TemporaryDirectory() as obj_dir:
 
-        for image in crash_log.images:
-            futures.append(executor.submit(add_module, image=image, target=target))
+            def add_module(image, target, obj_dir):
+                return image, image.add_module(target, obj_dir)
 
+            for image in crash_log.images:
+                futures.append(
+                    executor.submit(
+                        add_module, image=image, target=target, obj_dir=obj_dir
+                    )
+                )
         for future in concurrent.futures.as_completed(futures):
             image, err = future.result()
             if err:
index 46b2816..43e307b 100644 (file)
@@ -1,4 +1,4 @@
-import os,json,struct,signal,uuid
+import os, json, struct, signal, uuid, tempfile
 
 from typing import Any, Dict
 
@@ -38,16 +38,17 @@ class CrashLogScriptedProcess(ScriptedProcess):
                         for image in self.crashlog.find_images_with_identifier(ident):
                             image.resolve = True
 
-        for image in self.crashlog.images:
-            if image not in self.loaded_images:
-                if image.uuid == uuid.UUID(int=0):
-                    continue
-                err = image.add_module(self.target)
-                if err:
-                    # Append to SBCommandReturnObject
-                    print(err)
-                else:
-                    self.loaded_images.append(image)
+        with tempfile.TemporaryDirectory() as obj_dir:
+            for image in self.crashlog.images:
+                if image not in self.loaded_images:
+                    if image.uuid == uuid.UUID(int=0):
+                        continue
+                    err = image.add_module(self.target, obj_dir)
+                    if err:
+                        # Append to SBCommandReturnObject
+                        print(err)
+                    else:
+                        self.loaded_images.append(image)
 
         for thread in self.crashlog.threads:
             if hasattr(thread, 'app_specific_backtrace') and thread.app_specific_backtrace:
index 3a42f34..74de7dd 100755 (executable)
@@ -368,7 +368,7 @@ class Image:
         else:
             return 'error: no section infos'
 
-    def add_module(self, target):
+    def add_module(self, target, obj_dir=None):
         '''Add the Image described in this object to "target" and load the sections if "load" is True.'''
         if target:
             # Try and find using UUID only first so that paths need not match
@@ -384,7 +384,7 @@ class Image:
                         resolved_path, None, uuid_str, self.symfile)
             if not self.module and self.section_infos:
                 name = os.path.basename(self.path)
-                with tempfile.NamedTemporaryFile(suffix='.' + name) as tf:
+                if obj_dir and os.path.isdir(obj_dir):
                     data = {
                         'triple': target.triple,
                         'uuid': uuid_str,
@@ -398,9 +398,10 @@ class Image:
                             'size': section.end_addr - section.start_addr
                             })
                     data['symbols'] = list(self.symbols.values())
-                    with open(tf.name, 'w') as f:
+                    obj_file = os.path.join(obj_dir, name)
+                    with open(obj_file, "w") as f:
                         f.write(json.dumps(data, indent=4))
-                    self.module = target.AddModule(tf.name, None, uuid_str)
+                    self.module = target.AddModule(obj_file, None, uuid_str)
                     if self.module:
                         # If we were able to add the module with inlined
                         # symbols, we should mark it as available so load_module