[Lldb-commits] [lldb] Add new API in SBTarget for loading core from SBFile (PR #71769)

via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 16 20:59:29 PST 2023


https://github.com/GeorgeHuyubo updated https://github.com/llvm/llvm-project/pull/71769

>From 749453ae9878dbc38c49db3a3086882ae886d849 Mon Sep 17 00:00:00 2001
From: George Hu <huyubohyb at gmail.com>
Date: Thu, 16 Nov 2023 15:46:33 -0800
Subject: [PATCH] Add new API in SBTarget for loading core from SBFile

---
 lldb/include/lldb/API/SBTarget.h              |  1 +
 lldb/source/API/SBTarget.cpp                  | 26 +++++++++++++++++++
 .../postmortem/elf-core/TestLinuxCore.py      | 18 +++++++++++++
 3 files changed, 45 insertions(+)

diff --git a/lldb/include/lldb/API/SBTarget.h b/lldb/include/lldb/API/SBTarget.h
index 83087623088c5b4..8e44cd5513c5b20 100644
--- a/lldb/include/lldb/API/SBTarget.h
+++ b/lldb/include/lldb/API/SBTarget.h
@@ -184,6 +184,7 @@ class LLDB_API SBTarget {
 
   SBProcess LoadCore(const char *core_file);
   SBProcess LoadCore(const char *core_file, lldb::SBError &error);
+  SBProcess LoadCore(const SBFile &file, lldb::SBError &error);
 
   /// Launch a new process with sensible defaults.
   ///
diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp
index 2d029554492a05c..9632627e3cefc42 100644
--- a/lldb/source/API/SBTarget.cpp
+++ b/lldb/source/API/SBTarget.cpp
@@ -16,6 +16,7 @@
 #include "lldb/API/SBEnvironment.h"
 #include "lldb/API/SBEvent.h"
 #include "lldb/API/SBExpressionOptions.h"
+#include "lldb/API/SBFile.h"
 #include "lldb/API/SBFileSpec.h"
 #include "lldb/API/SBListener.h"
 #include "lldb/API/SBModule.h"
@@ -260,6 +261,31 @@ SBProcess SBTarget::LoadCore(const char *core_file, lldb::SBError &error) {
   return sb_process;
 }
 
+SBProcess SBTarget::LoadCore(const SBFile &file, lldb::SBError &error) {
+  LLDB_INSTRUMENT_VA(this, file, error);
+
+  SBProcess sb_process;
+  TargetSP target_sp(GetSP());
+  if (target_sp) {
+    FileSP file_sp = file.GetFile();
+    FileSpec filespec;
+    file_sp->GetFileSpec(filespec);
+    FileSystem::Instance().Resolve(filespec);
+    ProcessSP process_sp(target_sp->CreateProcess(
+        target_sp->GetDebugger().GetListener(), "", &filespec, false));
+    if (process_sp) {
+      error.SetError(process_sp->LoadCore());
+      if (error.Success())
+        sb_process.SetSP(process_sp);
+    } else {
+      error.SetErrorString("Failed to create the process");
+    }
+  } else {
+    error.SetErrorString("SBTarget is invalid");
+  }
+  return sb_process;
+}
+
 SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
                                  const char *working_directory) {
   LLDB_INSTRUMENT_VA(this, argv, envp, working_directory);
diff --git a/lldb/test/API/functionalities/postmortem/elf-core/TestLinuxCore.py b/lldb/test/API/functionalities/postmortem/elf-core/TestLinuxCore.py
index a083fab18eabcbc..4a638c99985c3dd 100644
--- a/lldb/test/API/functionalities/postmortem/elf-core/TestLinuxCore.py
+++ b/lldb/test/API/functionalities/postmortem/elf-core/TestLinuxCore.py
@@ -53,6 +53,11 @@ def test_x86_64(self):
         """Test that lldb can read the process information from an x86_64 linux core file."""
         self.do_test("linux-x86_64", self._x86_64_pid, self._x86_64_regions, "a.out")
 
+    @skipIfLLVMTargetMissing("X86")
+    def test_x86_64_fd(self):
+        """Test that lldb can read the process information from an x86_64 linux core file."""
+        self.do_test_fd("linux-x86_64", self._x86_64_pid, self._x86_64_regions, "a.out")
+
     @skipIfLLVMTargetMissing("SystemZ")
     def test_s390x(self):
         """Test that lldb can read the process information from an s390x linux core file."""
@@ -752,6 +757,19 @@ def do_test(self, filename, pid, region_count, thread_name):
 
         self.dbg.DeleteTarget(target)
 
+    def do_test_fd(self, filename, pid, region_count, thread_name):
+        file_object = open(filename + ".core", "r")
+        fd = file_object.fileno()
+        file = lldb.SBFile(fd, "r", True)
+        target = self.dbg.CreateTarget(filename + ".out")
+        error = lldb.SBError()
+        process = target.LoadCore(file, error)
+
+        self.check_all(process, pid, region_count, thread_name)
+
+        self.dbg.DeleteTarget(target)
+
+
 
 def replace_path(binary, replace_from, replace_to):
     src = replace_from.encode()



More information about the lldb-commits mailing list