Skip to content

Add new API in SBTarget for loading core from SBFile #71769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
26 changes: 26 additions & 0 deletions lldb/source/API/SBTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Comment on lines +264 to +287
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a lot simpler and easier to read with early returns, with the added advantage that the error message is closer to the condition it corresponds to. The function below is a good example of that.


SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
const char *working_directory) {
LLDB_INSTRUMENT_VA(this, argv, envp, working_directory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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()
Expand Down