mib created this revision.
mib added a reviewer: labath.
mib added a project: LLDB.
Herald added a subscriber: lldb-commits.

This patch moves the SB API method GetExtendedCrashInformation from
SBTarget to SBProcess since it only makes sense to call this method on a
sane process which might not be the case on a SBTarget object.

It also addresses some feedbacks received in D74657 
<https://reviews.llvm.org/D74657>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75049

Files:
  lldb/bindings/interface/SBProcess.i
  lldb/bindings/interface/SBTarget.i
  lldb/include/lldb/API/SBProcess.h
  lldb/include/lldb/API/SBStructuredData.h
  lldb/include/lldb/API/SBTarget.h
  lldb/include/lldb/Target/Platform.h
  lldb/source/API/SBProcess.cpp
  lldb/source/API/SBTarget.cpp
  lldb/source/Commands/CommandObjectProcess.cpp
  lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
  lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
  lldb/test/API/functionalities/process_crash_info/TestProcessCrashInfo.py
  lldb/test/API/functionalities/process_crash_info/main.c

Index: lldb/test/API/functionalities/process_crash_info/main.c
===================================================================
--- lldb/test/API/functionalities/process_crash_info/main.c
+++ lldb/test/API/functionalities/process_crash_info/main.c
@@ -1,6 +1,7 @@
 #include <stdlib.h>
+
 int main() {
-  int *var = malloc(sizeof(int));
+  int *var = malloc(sizeof(int)); // break here
   free(var);
   free(var);
   return 0;
Index: lldb/test/API/functionalities/process_crash_info/TestProcessCrashInfo.py
===================================================================
--- lldb/test/API/functionalities/process_crash_info/TestProcessCrashInfo.py
+++ lldb/test/API/functionalities/process_crash_info/TestProcessCrashInfo.py
@@ -17,7 +17,7 @@
         TestBase.setUp(self)
         self.runCmd("settings set auto-confirm true")
         self.source = "main.c"
-        self.line = 3
+        self.line = lldbtest.line_number(self.source, '// break here')
 
     def tearDown(self):
         self.runCmd("settings clear auto-confirm")
@@ -52,7 +52,10 @@
         stream = lldb.SBStream()
         self.assertTrue(stream)
 
-        crash_info = target.GetExtendedCrashInformation()
+        process = target.GetProcess()
+        self.assertTrue(process)
+
+        crash_info = process.GetExtendedCrashInformation()
 
         error = crash_info.GetAsJSON(stream)
 
@@ -62,24 +65,6 @@
 
         self.assertIn("pointer being freed was not allocated", stream.GetData())
 
-    @skipUnlessDarwin
-    def test_before_launch(self):
-        """Test that lldb doesn't fetch the extended crash information
-        dictionnary from if the process wasn't launched yet."""
-        self.build()
-        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
-        self.assertTrue(target, VALID_TARGET)
-
-        stream = lldb.SBStream()
-        self.assertTrue(stream)
-
-        crash_info = target.GetExtendedCrashInformation()
-
-        error = crash_info.GetAsJSON(stream)
-        self.assertFalse(error.Success())
-        self.assertIn("No structured data.", error.GetCString())
-
-    @skipUnlessDarwin
     def test_on_sane_process(self):
         """Test that lldb doesn't fetch the extended crash information
         dictionnary from a 'sane' stopped process."""
@@ -90,7 +75,10 @@
         stream = lldb.SBStream()
         self.assertTrue(stream)
 
-        crash_info = target.GetExtendedCrashInformation()
+        process = target.GetProcess()
+        self.assertTrue(process)
+
+        crash_info = process.GetExtendedCrashInformation()
 
         error = crash_info.GetAsJSON(stream)
         self.assertFalse(error.Success())
Index: lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
===================================================================
--- lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
+++ lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
@@ -86,7 +86,7 @@
   };
 
   llvm::Expected<lldb_private::StructuredData::DictionarySP>
-  FetchExtendedCrashInformation(lldb_private::Target &target) override;
+  FetchExtendedCrashInformation(lldb_private::Process& process) override;
 
 protected:
   struct CrashInfoAnnotations {
@@ -107,15 +107,15 @@
   /// extract the section to gather the messages annotations and the abort
   /// cause.
   ///
-  /// \param[in] target
-  ///     The target running the crashed process.
+  /// \param[in] process
+  ///     The crashed process.
   ///
   /// \return
   ///     A  structured data array containing at each entry in each entry, the
   ///     module spec, its UUID, the crash messages and the abort cause.
   ///     \b nullptr if process has no crash information annotations.
   lldb_private::StructuredData::ArraySP
-  ExtractCrashInfoAnnotations(lldb_private::Target &target);
+  ExtractCrashInfoAnnotations(lldb_private::Process& process);
 
   void ReadLibdispatchOffsetsAddress(lldb_private::Process *process);
 
Index: lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
===================================================================
--- lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -1503,10 +1503,10 @@
 }
 
 llvm::Expected<StructuredData::DictionarySP>
-PlatformDarwin::FetchExtendedCrashInformation(lldb_private::Target &target) {
+PlatformDarwin::FetchExtendedCrashInformation(Process& process) {
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
 
-  StructuredData::ArraySP annotations = ExtractCrashInfoAnnotations(target);
+  StructuredData::ArraySP annotations = ExtractCrashInfoAnnotations(process);
 
   if (!annotations || !annotations->GetSize()) {
     LLDB_LOG(log, "Couldn't extract crash information annotations");
@@ -1522,11 +1522,11 @@
 }
 
 StructuredData::ArraySP
-PlatformDarwin::ExtractCrashInfoAnnotations(Target &target) {
+PlatformDarwin::ExtractCrashInfoAnnotations(Process& process) {
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
 
   ConstString section_name("__crash_info");
-  ProcessSP process_sp = target.GetProcessSP();
+  Target & target = process.GetTarget();
   StructuredData::ArraySP array_sp = std::make_shared<StructuredData::Array>();
 
   for (ModuleSP module : target.GetImages().Modules()) {
@@ -1562,7 +1562,7 @@
     Status error;
     CrashInfoAnnotations annotations;
     size_t expected_size = sizeof(CrashInfoAnnotations);
-    size_t bytes_read = process_sp->ReadMemoryFromInferior(
+    size_t bytes_read = process.ReadMemoryFromInferior(
         load_addr, &annotations, expected_size, error);
 
     if (expected_size != bytes_read || error.Fail()) {
@@ -1587,7 +1587,7 @@
 
     std::string message;
     bytes_read =
-        process_sp->ReadCStringFromMemory(annotations.message, message, error);
+        process.ReadCStringFromMemory(annotations.message, message, error);
 
     if (message.empty() || bytes_read != message.size() || error.Fail()) {
       LLDB_LOG(log, "Failed to read the message from memory in module {0}: {1}",
@@ -1603,7 +1603,7 @@
       LLDB_LOG(log, "No message2 available for module {0}.", module_name);
 
     std::string message2;
-    bytes_read = process_sp->ReadCStringFromMemory(annotations.message2,
+    bytes_read = process.ReadCStringFromMemory(annotations.message2,
                                                    message2, error);
 
     if (!message2.empty() && bytes_read == message2.size() && error.Success())
Index: lldb/source/Commands/CommandObjectProcess.cpp
===================================================================
--- lldb/source/Commands/CommandObjectProcess.cpp
+++ lldb/source/Commands/CommandObjectProcess.cpp
@@ -1283,7 +1283,7 @@
       }
 
       auto expected_crash_info =
-          platform_sp->FetchExtendedCrashInformation(process->GetTarget());
+          platform_sp->FetchExtendedCrashInformation(*process);
 
       if (!expected_crash_info) {
         result.AppendError(llvm::toString(expected_crash_info.takeError()));
Index: lldb/source/API/SBTarget.cpp
===================================================================
--- lldb/source/API/SBTarget.cpp
+++ lldb/source/API/SBTarget.cpp
@@ -2388,30 +2388,6 @@
     m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
 }
 
-SBStructuredData SBTarget::GetExtendedCrashInformation() {
-  LLDB_RECORD_METHOD_NO_ARGS(lldb::SBStructuredData, SBTarget,
-                             GetExtendedCrashInformation);
-  SBStructuredData data;
-  TargetSP target_sp(GetSP());
-  if (!target_sp)
-    return LLDB_RECORD_RESULT(data);
-
-  PlatformSP platform_sp = target_sp->GetPlatform();
-
-  if (!target_sp)
-    return LLDB_RECORD_RESULT(data);
-
-  auto expected_data =
-      platform_sp->FetchExtendedCrashInformation(*target_sp.get());
-
-  if (!expected_data)
-    return LLDB_RECORD_RESULT(data);
-
-  StructuredData::ObjectSP fetched_data = *expected_data;
-  data.m_impl_up->SetObjectSP(fetched_data);
-  return LLDB_RECORD_RESULT(data);
-}
-
 namespace lldb_private {
 namespace repro {
 
@@ -2654,8 +2630,6 @@
   LLDB_REGISTER_METHOD_CONST(lldb::SBLaunchInfo, SBTarget, GetLaunchInfo, ());
   LLDB_REGISTER_METHOD(void, SBTarget, SetLaunchInfo,
                        (const lldb::SBLaunchInfo &));
-  LLDB_REGISTER_METHOD(lldb::SBStructuredData, SBTarget,
-                       GetExtendedCrashInformation, ());
   LLDB_REGISTER_METHOD(
       size_t, SBTarget, ReadMemory,
       (const lldb::SBAddress, void *, size_t, lldb::SBError &));
Index: lldb/source/API/SBProcess.cpp
===================================================================
--- lldb/source/API/SBProcess.cpp
+++ lldb/source/API/SBProcess.cpp
@@ -18,6 +18,7 @@
 #include "lldb/Core/Module.h"
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Core/StreamFile.h"
+#include "lldb/Core/StructuredDataImpl.h"
 #include "lldb/Target/MemoryRegionInfo.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/RegisterContext.h"
@@ -1010,6 +1011,30 @@
   return true;
 }
 
+SBStructuredData SBProcess::GetExtendedCrashInformation() {
+  LLDB_RECORD_METHOD_NO_ARGS(lldb::SBStructuredData, SBProcess,
+                             GetExtendedCrashInformation);
+  SBStructuredData data;
+  ProcessSP process_sp(GetSP());
+  if (!process_sp)
+    return LLDB_RECORD_RESULT(data);
+
+  PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
+
+  if (!platform_sp)
+    return LLDB_RECORD_RESULT(data);
+  
+  auto expected_data =
+      platform_sp->FetchExtendedCrashInformation(*process_sp.get());
+
+  if (!expected_data)
+    return LLDB_RECORD_RESULT(data);
+
+  StructuredData::ObjectSP fetched_data = *expected_data;
+  data.m_impl_up->SetObjectSP(fetched_data);
+  return LLDB_RECORD_RESULT(data);
+}
+
 uint32_t
 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
   LLDB_RECORD_METHOD_CONST(uint32_t, SBProcess,
@@ -1385,6 +1410,8 @@
   LLDB_REGISTER_METHOD(lldb::addr_t, SBProcess, ReadPointerFromMemory,
                        (lldb::addr_t, lldb::SBError &));
   LLDB_REGISTER_METHOD(bool, SBProcess, GetDescription, (lldb::SBStream &));
+  LLDB_REGISTER_METHOD(lldb::SBStructuredData, SBProcess,
+                       GetExtendedCrashInformation, ());
   LLDB_REGISTER_METHOD_CONST(uint32_t, SBProcess,
                              GetNumSupportedHardwareWatchpoints,
                              (lldb::SBError &));
Index: lldb/include/lldb/Target/Platform.h
===================================================================
--- lldb/include/lldb/Target/Platform.h
+++ lldb/include/lldb/Target/Platform.h
@@ -831,8 +831,8 @@
   /// nullptr. This dictionnary is generic and extensible, as it contains an
   /// array for each different type of crash information.
   ///
-  /// \param[in] target
-  ///     The target running the crashed process.
+  /// \param[in] process
+  ///     The crashed process.
   ///
   /// \return
   ///     A structured data dictionnary containing at each entry, the crash
@@ -840,7 +840,7 @@
   ///     entry value. \b nullptr if not implemented or  if the process has no
   ///     crash information entry. \b error if an error occured.
   virtual llvm::Expected<StructuredData::DictionarySP>
-  FetchExtendedCrashInformation(lldb_private::Target &target) {
+  FetchExtendedCrashInformation(lldb_private::Process &process) {
     return nullptr;
   }
 
Index: lldb/include/lldb/API/SBTarget.h
===================================================================
--- lldb/include/lldb/API/SBTarget.h
+++ lldb/include/lldb/API/SBTarget.h
@@ -819,8 +819,6 @@
 
   void SetLaunchInfo(const lldb::SBLaunchInfo &launch_info);
 
-  SBStructuredData GetExtendedCrashInformation();
-
 protected:
   friend class SBAddress;
   friend class SBBlock;
Index: lldb/include/lldb/API/SBStructuredData.h
===================================================================
--- lldb/include/lldb/API/SBStructuredData.h
+++ lldb/include/lldb/API/SBStructuredData.h
@@ -91,6 +91,7 @@
   friend class SBTraceOptions;
   friend class SBDebugger;
   friend class SBTarget;
+  friend class SBProcess;
   friend class SBThread;
   friend class SBThreadPlan;
   friend class SBBreakpoint;
Index: lldb/include/lldb/API/SBProcess.h
===================================================================
--- lldb/include/lldb/API/SBProcess.h
+++ lldb/include/lldb/API/SBProcess.h
@@ -222,6 +222,8 @@
 
   bool GetDescription(lldb::SBStream &description);
 
+  SBStructuredData GetExtendedCrashInformation();
+
   /// Start Tracing with the given SBTraceOptions.
   ///
   /// \param[in] options
Index: lldb/bindings/interface/SBTarget.i
===================================================================
--- lldb/bindings/interface/SBTarget.i
+++ lldb/bindings/interface/SBTarget.i
@@ -949,12 +949,6 @@
     void
     SetLaunchInfo (const lldb::SBLaunchInfo &launch_info);
 
-    %feature("autodoc", "
-    Returns the platform's process extended crash information.") GetExtendedCrashInformation;
-    lldb::SBStructuredData
-    GetExtendedCrashInformation ();
-
-
     void SetCollectingStats(bool v);
 
     bool GetCollectingStats();
Index: lldb/bindings/interface/SBProcess.i
===================================================================
--- lldb/bindings/interface/SBProcess.i
+++ lldb/bindings/interface/SBProcess.i
@@ -346,6 +346,11 @@
     bool
     GetDescription (lldb::SBStream &description);
 
+    %feature("autodoc", "
+    Returns the platform's process extended crash information.") GetExtendedCrashInformation;
+    lldb::SBStructuredData
+    GetExtendedCrashInformation ();
+
     uint32_t
     GetNumSupportedHardwareWatchpoints (lldb::SBError &error) const;
 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
  • [Lldb-commits] [PATCH]... Med Ismail Bennani via Phabricator via lldb-commits

Reply via email to