Skip to content

Commit e2fb816

Browse files
authored
Add new API in SBTarget for loading core from SBFile (#71769)
Add a new API in SBTarget to Load Core from a SBFile. This will enable a target to load core from a file descriptor. So that in coredumper, we don't need to write core file to disk, instead we can pass the input file descriptor to lldb directly. Test: ``` (lldb) script Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D. >>> file_object = open("/home/hyubo/210hda79ms32sr0h", "r") >>> fd=file_object.fileno() >>> file = lldb.SBFile(fd,'r', True) >>> error = lldb.SBError() >>> target = lldb.debugger.CreateTarget(None) >>> target.LoadCore(file,error) SBProcess: pid = 56415, state = stopped, threads = 1 ```
1 parent 4263b2e commit e2fb816

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

lldb/include/lldb/API/SBTarget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class LLDB_API SBTarget {
184184

185185
SBProcess LoadCore(const char *core_file);
186186
SBProcess LoadCore(const char *core_file, lldb::SBError &error);
187+
SBProcess LoadCore(const SBFile &file, lldb::SBError &error);
187188

188189
/// Launch a new process with sensible defaults.
189190
///

lldb/source/API/SBTarget.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "lldb/API/SBEnvironment.h"
1717
#include "lldb/API/SBEvent.h"
1818
#include "lldb/API/SBExpressionOptions.h"
19+
#include "lldb/API/SBFile.h"
1920
#include "lldb/API/SBFileSpec.h"
2021
#include "lldb/API/SBListener.h"
2122
#include "lldb/API/SBModule.h"
@@ -260,6 +261,31 @@ SBProcess SBTarget::LoadCore(const char *core_file, lldb::SBError &error) {
260261
return sb_process;
261262
}
262263

264+
SBProcess SBTarget::LoadCore(const SBFile &file, lldb::SBError &error) {
265+
LLDB_INSTRUMENT_VA(this, file, error);
266+
267+
SBProcess sb_process;
268+
TargetSP target_sp(GetSP());
269+
if (target_sp) {
270+
FileSP file_sp = file.GetFile();
271+
FileSpec filespec;
272+
file_sp->GetFileSpec(filespec);
273+
FileSystem::Instance().Resolve(filespec);
274+
ProcessSP process_sp(target_sp->CreateProcess(
275+
target_sp->GetDebugger().GetListener(), "", &filespec, false));
276+
if (process_sp) {
277+
error.SetError(process_sp->LoadCore());
278+
if (error.Success())
279+
sb_process.SetSP(process_sp);
280+
} else {
281+
error.SetErrorString("Failed to create the process");
282+
}
283+
} else {
284+
error.SetErrorString("SBTarget is invalid");
285+
}
286+
return sb_process;
287+
}
288+
263289
SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
264290
const char *working_directory) {
265291
LLDB_INSTRUMENT_VA(this, argv, envp, working_directory);

lldb/test/API/functionalities/postmortem/elf-core/TestLinuxCore.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def test_x86_64(self):
5353
"""Test that lldb can read the process information from an x86_64 linux core file."""
5454
self.do_test("linux-x86_64", self._x86_64_pid, self._x86_64_regions, "a.out")
5555

56+
@skipIfLLVMTargetMissing("X86")
57+
def test_x86_64_fd(self):
58+
"""Test that lldb can read the process information from an x86_64 linux core file."""
59+
self.do_test_fd("linux-x86_64", self._x86_64_pid, self._x86_64_regions, "a.out")
60+
5661
@skipIfLLVMTargetMissing("SystemZ")
5762
def test_s390x(self):
5863
"""Test that lldb can read the process information from an s390x linux core file."""
@@ -757,6 +762,19 @@ def do_test(self, filename, pid, region_count, thread_name):
757762

758763
self.dbg.DeleteTarget(target)
759764

765+
def do_test_fd(self, filename, pid, region_count, thread_name):
766+
file_object = open(filename + ".core", "r")
767+
fd = file_object.fileno()
768+
file = lldb.SBFile(fd, "r", True)
769+
target = self.dbg.CreateTarget(filename + ".out")
770+
error = lldb.SBError()
771+
process = target.LoadCore(file, error)
772+
773+
self.check_all(process, pid, region_count, thread_name)
774+
775+
self.dbg.DeleteTarget(target)
776+
777+
760778

761779
def replace_path(binary, replace_from, replace_to):
762780
src = replace_from.encode()

0 commit comments

Comments
 (0)