Skip to content

Add a new SBProcess:: GetCoreFile() API (#80767) #8137

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
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
28 changes: 28 additions & 0 deletions lldb/bindings/python/static-binding/LLDBWrapPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51986,6 +51986,33 @@ SWIGINTERN PyObject *_wrap_SBProcess_GetProcessInfo(PyObject *self, PyObject *ar
}


SWIGINTERN PyObject *_wrap_SBProcess_GetCoreFile(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
lldb::SBProcess *arg1 = (lldb::SBProcess *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject *swig_obj[1] ;
lldb::SBFileSpec result;

if (!args) SWIG_fail;
swig_obj[0] = args;
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_lldb__SBProcess, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "SBProcess_GetCoreFile" "', argument " "1"" of type '" "lldb::SBProcess *""'");
}
arg1 = reinterpret_cast< lldb::SBProcess * >(argp1);
{
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
result = (arg1)->GetCoreFile();
SWIG_PYTHON_THREAD_END_ALLOW;
}
resultobj = SWIG_NewPointerObj((new lldb::SBFileSpec(result)), SWIGTYPE_p_lldb__SBFileSpec, SWIG_POINTER_OWN | 0 );
return resultobj;
fail:
return NULL;
}


SWIGINTERN PyObject *_wrap_SBProcess_AllocateMemory(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
lldb::SBProcess *arg1 = (lldb::SBProcess *) 0 ;
Expand Down Expand Up @@ -90781,6 +90808,7 @@ static PyMethodDef SwigMethods[] = {
" if process_info.IsValid():\n"
" process_info.GetProcessID()\n"
""},
{ "SBProcess_GetCoreFile", _wrap_SBProcess_GetCoreFile, METH_O, "SBProcess_GetCoreFile(SBProcess self) -> SBFileSpec"},
{ "SBProcess_AllocateMemory", _wrap_SBProcess_AllocateMemory, METH_VARARGS, "\n"
"\n"
"Allocates a block of memory within the process, with size and\n"
Expand Down
4 changes: 4 additions & 0 deletions lldb/bindings/python/static-binding/lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8960,6 +8960,10 @@ def GetProcessInfo(self):
"""
return _lldb.SBProcess_GetProcessInfo(self)

def GetCoreFile(self):
r"""GetCoreFile(SBProcess self) -> SBFileSpec"""
return _lldb.SBProcess_GetCoreFile(self)

def AllocateMemory(self, size, permissions, error):
r"""

Expand Down
9 changes: 9 additions & 0 deletions lldb/include/lldb/API/SBProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ class LLDB_API SBProcess {
/// valid.
lldb::SBProcessInfo GetProcessInfo();

/// Get the file specification for the core file that is currently being used
/// for the process. If the process is not loaded from a core file, then an
/// invalid file specification will be returned.
///
/// \return
/// The path to the core file for this target or an invalid file spec if
/// the process isn't loaded from a core file.
lldb::SBFileSpec GetCoreFile();

/// Allocate memory within the process.
///
/// This function will allocate memory in the process's address space.
Expand Down
9 changes: 9 additions & 0 deletions lldb/include/lldb/Target/PostMortemProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ class PostMortemProcess : public Process {
using Process::Process;

public:
PostMortemProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
const FileSpec &core_file)
: Process(target_sp, listener_sp), m_core_file(core_file) {}

bool IsLiveDebugSession() const override { return false; }

FileSpec GetCoreFile() const override { return m_core_file; }

protected:
FileSpec m_core_file;
};

} // namespace lldb_private
Expand Down
7 changes: 7 additions & 0 deletions lldb/include/lldb/Target/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,13 @@ class Process : public std::enable_shared_from_this<Process>,

virtual bool IsLiveDebugSession() const { return true; };

/// Provide a way to retrieve the core dump file that is loaded for debugging.
/// Only available if IsLiveDebugSession() returns true.
///
/// \return
/// File path to the core file.
virtual FileSpec GetCoreFile() const { return {}; }

/// Before lldb detaches from a process, it warns the user that they are
/// about to lose their debug session. In some cases, this warning doesn't
/// need to be emitted -- for instance, with core file debugging where the
Expand Down
3 changes: 2 additions & 1 deletion lldb/include/lldb/Target/ProcessTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class ProcessTrace : public PostMortemProcess {

static llvm::StringRef GetPluginDescriptionStatic();

ProcessTrace(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);
ProcessTrace(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
const FileSpec &core_file);

~ProcessTrace() override;

Expand Down
11 changes: 11 additions & 0 deletions lldb/source/API/SBProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,17 @@ lldb::SBProcessInfo SBProcess::GetProcessInfo() {
return sb_proc_info;
}

lldb::SBFileSpec SBProcess::GetCoreFile() {
LLDB_INSTRUMENT_VA(this);

ProcessSP process_sp(GetSP());
FileSpec core_file;
if (process_sp) {
core_file = process_sp->GetCoreFile();
}
return SBFileSpec(core_file);
}

lldb::addr_t SBProcess::AllocateMemory(size_t size, uint32_t permissions,
lldb::SBError &sb_error) {
LLDB_INSTRUMENT_VA(this, size, permissions, sb_error);
Expand Down
21 changes: 12 additions & 9 deletions lldb/source/Plugins/Process/FreeBSDKernel/ProcessFreeBSDKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace {
class ProcessFreeBSDKernelFVC : public ProcessFreeBSDKernel {
public:
ProcessFreeBSDKernelFVC(lldb::TargetSP target_sp, lldb::ListenerSP listener,
fvc_t *fvc);
fvc_t *fvc, const FileSpec &core_file);

~ProcessFreeBSDKernelFVC();

Expand Down Expand Up @@ -67,8 +67,9 @@ class ProcessFreeBSDKernelKVM : public ProcessFreeBSDKernel {
} // namespace

ProcessFreeBSDKernel::ProcessFreeBSDKernel(lldb::TargetSP target_sp,
ListenerSP listener_sp)
: PostMortemProcess(target_sp, listener_sp) {}
ListenerSP listener_sp,
const FileSpec &core_file)
: PostMortemProcess(target_sp, listener_sp, core_file) {}

lldb::ProcessSP ProcessFreeBSDKernel::CreateInstance(lldb::TargetSP target_sp,
ListenerSP listener_sp,
Expand All @@ -82,7 +83,7 @@ lldb::ProcessSP ProcessFreeBSDKernel::CreateInstance(lldb::TargetSP target_sp,
crash_file->GetPath().c_str(), nullptr, nullptr, nullptr);
if (fvc)
return std::make_shared<ProcessFreeBSDKernelFVC>(target_sp, listener_sp,
fvc);
fvc, *crash_file);
#endif

#if defined(__FreeBSD__)
Expand All @@ -91,7 +92,7 @@ lldb::ProcessSP ProcessFreeBSDKernel::CreateInstance(lldb::TargetSP target_sp,
crash_file->GetPath().c_str(), O_RDONLY, nullptr, nullptr);
if (kvm)
return std::make_shared<ProcessFreeBSDKernelKVM>(target_sp, listener_sp,
kvm);
kvm, *crash_file);
#endif
}
return nullptr;
Expand Down Expand Up @@ -276,8 +277,9 @@ lldb::addr_t ProcessFreeBSDKernel::FindSymbol(const char *name) {

ProcessFreeBSDKernelFVC::ProcessFreeBSDKernelFVC(lldb::TargetSP target_sp,
ListenerSP listener_sp,
fvc_t *fvc)
: ProcessFreeBSDKernel(target_sp, listener_sp), m_fvc(fvc) {}
fvc_t *fvc,
const FileSpec &core_file)
: ProcessFreeBSDKernel(target_sp, listener_sp, crash_file), m_fvc(fvc) {}

ProcessFreeBSDKernelFVC::~ProcessFreeBSDKernelFVC() {
if (m_fvc)
Expand All @@ -303,8 +305,9 @@ const char *ProcessFreeBSDKernelFVC::GetError() { return fvc_geterr(m_fvc); }

ProcessFreeBSDKernelKVM::ProcessFreeBSDKernelKVM(lldb::TargetSP target_sp,
ListenerSP listener_sp,
kvm_t *fvc)
: ProcessFreeBSDKernel(target_sp, listener_sp), m_kvm(fvc) {}
kvm_t *fvc,
const FileSpec &core_file)
: ProcessFreeBSDKernel(target_sp, listener_sp, core_file), m_kvm(fvc) {}

ProcessFreeBSDKernelKVM::~ProcessFreeBSDKernelKVM() {
if (m_kvm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

class ProcessFreeBSDKernel : public lldb_private::PostMortemProcess {
public:
ProcessFreeBSDKernel(lldb::TargetSP target_sp, lldb::ListenerSP listener);
ProcessFreeBSDKernel(lldb::TargetSP target_sp, lldb::ListenerSP listener,
const lldb_private::FileSpec &core_file);

static lldb::ProcessSP
CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener,
Expand Down
6 changes: 3 additions & 3 deletions lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ bool ProcessElfCore::CanDebug(lldb::TargetSP target_sp,
ProcessElfCore::ProcessElfCore(lldb::TargetSP target_sp,
lldb::ListenerSP listener_sp,
const FileSpec &core_file)
: PostMortemProcess(target_sp, listener_sp), m_core_file(core_file) {}
: PostMortemProcess(target_sp, listener_sp, core_file) {}

// Destructor
ProcessElfCore::~ProcessElfCore() {
Expand Down Expand Up @@ -261,8 +261,8 @@ Status ProcessElfCore::DoLoadCore() {
exe_module_spec.GetFileSpec().SetFile(m_nt_file_entries[0].path,
FileSpec::Style::native);
if (exe_module_spec.GetFileSpec()) {
exe_module_sp = GetTarget().GetOrCreateModule(exe_module_spec,
true /* notify */);
exe_module_sp =
GetTarget().GetOrCreateModule(exe_module_spec, true /* notify */);
if (exe_module_sp)
GetTarget().SetExecutableModule(exe_module_sp, eLoadDependentsNo);
}
Expand Down
1 change: 0 additions & 1 deletion lldb/source/Plugins/Process/elf-core/ProcessElfCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ class ProcessElfCore : public lldb_private::PostMortemProcess {
VMRangeToPermissions;

lldb::ModuleSP m_core_module_sp;
lldb_private::FileSpec m_core_file;
std::string m_dyld_plugin_name;

// True if m_thread_contexts contains valid entries
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ bool ProcessMachCore::CanDebug(lldb::TargetSP target_sp,
ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp,
ListenerSP listener_sp,
const FileSpec &core_file)
: PostMortemProcess(target_sp, listener_sp), m_core_aranges(),
m_core_range_infos(), m_core_module_sp(), m_core_file(core_file),
: PostMortemProcess(target_sp, listener_sp, core_file), m_core_aranges(),
m_core_range_infos(), m_core_module_sp(),
m_dyld_addr(LLDB_INVALID_ADDRESS),
m_mach_kernel_addr(LLDB_INVALID_ADDRESS) {}

Expand Down
1 change: 0 additions & 1 deletion lldb/source/Plugins/Process/mach-core/ProcessMachCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ class ProcessMachCore : public lldb_private::PostMortemProcess {
VMRangeToFileOffset m_core_aranges;
VMRangeToPermissions m_core_range_infos;
lldb::ModuleSP m_core_module_sp;
lldb_private::FileSpec m_core_file;
lldb::addr_t m_dyld_addr;
lldb::addr_t m_mach_kernel_addr;
llvm::StringRef m_dyld_plugin_name;
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ ProcessMinidump::ProcessMinidump(lldb::TargetSP target_sp,
lldb::ListenerSP listener_sp,
const FileSpec &core_file,
DataBufferSP core_data)
: PostMortemProcess(target_sp, listener_sp), m_core_file(core_file),
: PostMortemProcess(target_sp, listener_sp, core_file),
m_core_data(std::move(core_data)), m_active_exception(nullptr),
m_is_wow64(false) {}

Expand Down
1 change: 0 additions & 1 deletion lldb/source/Plugins/Process/minidump/ProcessMinidump.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class ProcessMinidump : public PostMortemProcess {
JITLoaderList &GetJITLoaders() override;

private:
FileSpec m_core_file;
lldb::DataBufferSP m_core_data;
llvm::ArrayRef<minidump::Thread> m_thread_list;
const minidump::ExceptionStream *m_active_exception;
Expand Down
7 changes: 4 additions & 3 deletions lldb/source/Target/ProcessTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,
bool can_connect) {
if (can_connect)
return nullptr;
return std::make_shared<ProcessTrace>(target_sp, listener_sp);
return std::make_shared<ProcessTrace>(target_sp, listener_sp, *crash_file);
}

bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
return plugin_specified_by_name;
}

ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp)
: PostMortemProcess(target_sp, listener_sp) {}
ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp,
const FileSpec &core_file)
: PostMortemProcess(target_sp, listener_sp, core_file) {}

ProcessTrace::~ProcessTrace() {
Clear();
Expand Down
11 changes: 11 additions & 0 deletions lldb/test/API/functionalities/postmortem/elf-core/TestLinuxCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,17 @@ def test_arm_core(self):

self.expect("register read --all")

def test_get_core_file_api(self):
"""
Test SBProcess::GetCoreFile() API can successfully get the core file.
"""
core_file_name = "linux-x86_64.core"
target = self.dbg.CreateTarget("linux-x86_64.out")
process = target.LoadCore(core_file_name)
self.assertTrue(process, PROCESS_IS_VALID)
self.assertEqual(process.GetCoreFile().GetFilename(), core_file_name)
self.dbg.DeleteTarget(target)

def check_memory_regions(self, process, region_count):
region_list = process.GetMemoryRegions()
self.assertEqual(region_list.GetSize(), region_count)
Expand Down