mib updated this revision to Diff 491007.
mib added a comment.

Address @JDevlieghere & @bulbazord comments

- Use D142059 <https://reviews.llvm.org/D142059>'s 
ScriptedProcess::GetCapabilities to decide whether we should force symbol 
lookup.
- Make symbol fetching parallel.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141702/new/

https://reviews.llvm.org/D141702

Files:
  lldb/examples/python/scripted_process/crashlog_scripted_process.py
  lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp

Index: lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
===================================================================
--- lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
+++ lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
@@ -11,7 +11,7 @@
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/Module.h"
 #include "lldb/Core/PluginManager.h"
-
+#include "lldb/Core/Progress.h"
 #include "lldb/Host/OptionParser.h"
 #include "lldb/Host/ThreadLauncher.h"
 #include "lldb/Interpreter/CommandInterpreter.h"
@@ -19,12 +19,15 @@
 #include "lldb/Interpreter/OptionGroupBoolean.h"
 #include "lldb/Interpreter/ScriptInterpreter.h"
 #include "lldb/Interpreter/ScriptedMetadata.h"
+#include "lldb/Symbol/LocateSymbolFile.h"
 #include "lldb/Target/MemoryRegionInfo.h"
 #include "lldb/Target/Queue.h"
 #include "lldb/Target/RegisterContext.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/State.h"
 
+#include "llvm/Support/ThreadPool.h"
+
 #include <mutex>
 
 LLDB_PLUGIN_DEFINE(ScriptedProcess)
@@ -414,19 +417,26 @@
     return ScriptedInterface::ErrorWithMessage<StructuredData::ObjectSP>(
         LLVM_PRETTY_FUNCTION, "No loaded images.", error);
 
+  size_t num_image_to_load = loaded_images_sp->GetSize();
   ModuleList module_list;
   Target &target = GetTarget();
 
-  auto reload_image = [&target, &module_list, &error_with_message](
-                          StructuredData::Object *obj) -> bool {
-    StructuredData::Dictionary *dict = obj->GetAsDictionary();
-
-    if (!dict)
-      return error_with_message("Couldn't cast image object into dictionary.");
+  StructuredData::DictionarySP capabilities_sp =
+      GetInterface().GetCapabilities();
+  if (!capabilities_sp) {
+    llvm::StringRef error_message =
+        "Couldn't fetch scripted process capabilities.\nContinuing loading "
+        "dynamic libraries.";
+    ScriptedInterface::ErrorWithMessage<StructuredData::ObjectSP>(
+        LLVM_PRETTY_FUNCTION, error_message, error);
+  }
 
-    ModuleSpec module_spec;
-    llvm::StringRef value;
+  bool force_lookup = false;
+  capabilities_sp->GetValueForKeyAsBoolean("force_symbol_lookup", force_lookup);
 
+  auto create_module_spec =
+      [&target, &error_with_message](StructuredData::Dictionary *dict,
+                                     ModuleSpec &module_spec) -> bool {
     bool has_path = dict->HasKey("path");
     bool has_uuid = dict->HasKey("uuid");
     if (!has_path && !has_uuid)
@@ -434,6 +444,7 @@
     if (!dict->HasKey("load_addr"))
       return error_with_message("Dictionary is missing key 'load_addr'");
 
+    llvm::StringRef value;
     if (has_path) {
       dict->GetValueForKeyAsString("path", value);
       module_spec.GetFileSpec().SetPath(value);
@@ -444,6 +455,43 @@
       module_spec.GetUUID().SetFromStringRef(value);
     }
     module_spec.GetArchitecture() = target.GetArchitecture();
+    return true;
+  };
+
+  Progress progress("Fetching external dependencies", num_image_to_load);
+  auto fetch_symbols =
+      [&error_with_message, &create_module_spec, &progress,
+       &force_lookup](StructuredData::Dictionary *dict) -> bool {
+    if (!dict)
+      return error_with_message("Couldn't cast image object into dictionary.");
+
+    Status error;
+    ModuleSpec module_spec;
+
+    if (!create_module_spec(dict, module_spec) ||
+        !Symbols::DownloadObjectAndSymbolFile(module_spec, error,
+                                              force_lookup) ||
+        error.Fail() ||
+        !FileSystem::Instance().Exists(module_spec.GetFileSpec()))
+      return error_with_message(error.AsCString());
+    progress.Increment();
+    return true;
+  };
+
+  auto load_modules =
+      [&target, &module_list, &create_module_spec,
+       &error_with_message](StructuredData::Object *obj) -> bool {
+    if (!obj)
+      return error_with_message("Invalid structured data object.");
+
+    StructuredData::Dictionary *dict = obj->GetAsDictionary();
+    if (!dict)
+      return error_with_message("Structured data object is not a dictionary.");
+
+    ModuleSpec module_spec;
+    if (!create_module_spec(dict, module_spec))
+      return error_with_message(
+          "Couldn't create module specificiation from dictionary.");
 
     ModuleSP module_sp =
         target.GetOrCreateModule(module_spec, true /* notify */);
@@ -451,6 +499,7 @@
     if (!module_sp)
       return error_with_message("Couldn't create or get module.");
 
+    llvm::StringRef value;
     lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
     lldb::addr_t slide = LLDB_INVALID_OFFSET;
     dict->GetValueForKeyAsInteger("load_addr", load_addr);
@@ -469,16 +518,29 @@
     if (!changed && !module_sp->GetObjectFile())
       return error_with_message("Couldn't set the load address for module.");
 
-    dict->GetValueForKeyAsString("path", value);
-    FileSpec objfile(value);
-    module_sp->SetFileSpecAndObjectName(objfile, objfile.GetFilename());
+    if (dict->HasKey("path")) {
+      dict->GetValueForKeyAsString("path", value);
+      FileSpec objfile(value);
+      module_sp->SetFileSpecAndObjectName(objfile, objfile.GetFilename());
+    }
 
     return module_list.AppendIfNeeded(module_sp);
   };
 
-  if (!loaded_images_sp->ForEach(reload_image))
-    return ScriptedInterface::ErrorWithMessage<StructuredData::ObjectSP>(
-        LLVM_PRETTY_FUNCTION, "Couldn't reload all images.", error);
+  // Share one thread pool across operations to avoid the overhead of
+  // recreating the threads.
+  llvm::ThreadPoolTaskGroup task_group(Debugger::GetThreadPool());
+
+  // Create a task runner that fetches symbols and imports dynamic libraries in
+  // separate threads.
+  StructuredData::Dictionary *item = nullptr;
+  for (size_t i = 0; i < loaded_images_sp->GetSize(); i++)
+    if (loaded_images_sp->GetItemAtIndexAsDictionary(i, item))
+      task_group.async(fetch_symbols, item);
+  task_group.wait();
+
+  if (!loaded_images_sp->ForEach(load_modules))
+    return {};
 
   target.ModulesDidLoad(module_list);
 
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
@@ -31,12 +31,10 @@
                     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)
+                        for section in image.section_infos:
+                            if section.start_addr and section.name == "__TEXT":
+                                self.loaded_images.append({"uuid": str(image.uuid),
+                                                           "load_addr": section.start_addr})
 
         for thread in crash_log.threads:
             if self.load_all_images:
@@ -93,6 +91,7 @@
         self.exception = None
         self.extended_thread_info = None
         self.parse_crashlog()
+        self.capabilities['force_symbol_lookup'] = True
 
     def get_memory_region_containing_address(self, addr: int) -> lldb.SBMemoryRegionInfo:
         return None
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to