Skip to content

Commit f8c0c22

Browse files
authored
Merge pull request #8137 from jasonmolenda/add-sbprocess-get-corefile-filespec
Add a new SBProcess:: GetCoreFile() API (llvm#80767)
2 parents 5bbdc64 + fd770ea commit f8c0c22

File tree

17 files changed

+105
-23
lines changed

17 files changed

+105
-23
lines changed

lldb/bindings/python/static-binding/LLDBWrapPython.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51986,6 +51986,33 @@ SWIGINTERN PyObject *_wrap_SBProcess_GetProcessInfo(PyObject *self, PyObject *ar
5198651986
}
5198751987

5198851988

51989+
SWIGINTERN PyObject *_wrap_SBProcess_GetCoreFile(PyObject *self, PyObject *args) {
51990+
PyObject *resultobj = 0;
51991+
lldb::SBProcess *arg1 = (lldb::SBProcess *) 0 ;
51992+
void *argp1 = 0 ;
51993+
int res1 = 0 ;
51994+
PyObject *swig_obj[1] ;
51995+
lldb::SBFileSpec result;
51996+
51997+
if (!args) SWIG_fail;
51998+
swig_obj[0] = args;
51999+
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_lldb__SBProcess, 0 | 0 );
52000+
if (!SWIG_IsOK(res1)) {
52001+
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "SBProcess_GetCoreFile" "', argument " "1"" of type '" "lldb::SBProcess *""'");
52002+
}
52003+
arg1 = reinterpret_cast< lldb::SBProcess * >(argp1);
52004+
{
52005+
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
52006+
result = (arg1)->GetCoreFile();
52007+
SWIG_PYTHON_THREAD_END_ALLOW;
52008+
}
52009+
resultobj = SWIG_NewPointerObj((new lldb::SBFileSpec(result)), SWIGTYPE_p_lldb__SBFileSpec, SWIG_POINTER_OWN | 0 );
52010+
return resultobj;
52011+
fail:
52012+
return NULL;
52013+
}
52014+
52015+
5198952016
SWIGINTERN PyObject *_wrap_SBProcess_AllocateMemory(PyObject *self, PyObject *args) {
5199052017
PyObject *resultobj = 0;
5199152018
lldb::SBProcess *arg1 = (lldb::SBProcess *) 0 ;
@@ -90781,6 +90808,7 @@ static PyMethodDef SwigMethods[] = {
9078190808
" if process_info.IsValid():\n"
9078290809
" process_info.GetProcessID()\n"
9078390810
""},
90811+
{ "SBProcess_GetCoreFile", _wrap_SBProcess_GetCoreFile, METH_O, "SBProcess_GetCoreFile(SBProcess self) -> SBFileSpec"},
9078490812
{ "SBProcess_AllocateMemory", _wrap_SBProcess_AllocateMemory, METH_VARARGS, "\n"
9078590813
"\n"
9078690814
"Allocates a block of memory within the process, with size and\n"

lldb/bindings/python/static-binding/lldb.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8960,6 +8960,10 @@ def GetProcessInfo(self):
89608960
"""
89618961
return _lldb.SBProcess_GetProcessInfo(self)
89628962

8963+
def GetCoreFile(self):
8964+
r"""GetCoreFile(SBProcess self) -> SBFileSpec"""
8965+
return _lldb.SBProcess_GetCoreFile(self)
8966+
89638967
def AllocateMemory(self, size, permissions, error):
89648968
r"""
89658969

lldb/include/lldb/API/SBProcess.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,15 @@ class LLDB_API SBProcess {
398398
/// valid.
399399
lldb::SBProcessInfo GetProcessInfo();
400400

401+
/// Get the file specification for the core file that is currently being used
402+
/// for the process. If the process is not loaded from a core file, then an
403+
/// invalid file specification will be returned.
404+
///
405+
/// \return
406+
/// The path to the core file for this target or an invalid file spec if
407+
/// the process isn't loaded from a core file.
408+
lldb::SBFileSpec GetCoreFile();
409+
401410
/// Allocate memory within the process.
402411
///
403412
/// This function will allocate memory in the process's address space.

lldb/include/lldb/Target/PostMortemProcess.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ class PostMortemProcess : public Process {
2424
using Process::Process;
2525

2626
public:
27+
PostMortemProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
28+
const FileSpec &core_file)
29+
: Process(target_sp, listener_sp), m_core_file(core_file) {}
30+
2731
bool IsLiveDebugSession() const override { return false; }
32+
33+
FileSpec GetCoreFile() const override { return m_core_file; }
34+
35+
protected:
36+
FileSpec m_core_file;
2837
};
2938

3039
} // namespace lldb_private

lldb/include/lldb/Target/Process.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,13 @@ class Process : public std::enable_shared_from_this<Process>,
14621462

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

1465+
/// Provide a way to retrieve the core dump file that is loaded for debugging.
1466+
/// Only available if IsLiveDebugSession() returns true.
1467+
///
1468+
/// \return
1469+
/// File path to the core file.
1470+
virtual FileSpec GetCoreFile() const { return {}; }
1471+
14651472
/// Before lldb detaches from a process, it warns the user that they are
14661473
/// about to lose their debug session. In some cases, this warning doesn't
14671474
/// need to be emitted -- for instance, with core file debugging where the

lldb/include/lldb/Target/ProcessTrace.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class ProcessTrace : public PostMortemProcess {
2727

2828
static llvm::StringRef GetPluginDescriptionStatic();
2929

30-
ProcessTrace(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);
30+
ProcessTrace(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
31+
const FileSpec &core_file);
3132

3233
~ProcessTrace() override;
3334

lldb/source/API/SBProcess.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,17 @@ lldb::SBProcessInfo SBProcess::GetProcessInfo() {
12441244
return sb_proc_info;
12451245
}
12461246

1247+
lldb::SBFileSpec SBProcess::GetCoreFile() {
1248+
LLDB_INSTRUMENT_VA(this);
1249+
1250+
ProcessSP process_sp(GetSP());
1251+
FileSpec core_file;
1252+
if (process_sp) {
1253+
core_file = process_sp->GetCoreFile();
1254+
}
1255+
return SBFileSpec(core_file);
1256+
}
1257+
12471258
lldb::addr_t SBProcess::AllocateMemory(size_t size, uint32_t permissions,
12481259
lldb::SBError &sb_error) {
12491260
LLDB_INSTRUMENT_VA(this, size, permissions, sb_error);

lldb/source/Plugins/Process/FreeBSDKernel/ProcessFreeBSDKernel.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace {
3232
class ProcessFreeBSDKernelFVC : public ProcessFreeBSDKernel {
3333
public:
3434
ProcessFreeBSDKernelFVC(lldb::TargetSP target_sp, lldb::ListenerSP listener,
35-
fvc_t *fvc);
35+
fvc_t *fvc, const FileSpec &core_file);
3636

3737
~ProcessFreeBSDKernelFVC();
3838

@@ -67,8 +67,9 @@ class ProcessFreeBSDKernelKVM : public ProcessFreeBSDKernel {
6767
} // namespace
6868

6969
ProcessFreeBSDKernel::ProcessFreeBSDKernel(lldb::TargetSP target_sp,
70-
ListenerSP listener_sp)
71-
: PostMortemProcess(target_sp, listener_sp) {}
70+
ListenerSP listener_sp,
71+
const FileSpec &core_file)
72+
: PostMortemProcess(target_sp, listener_sp, core_file) {}
7273

7374
lldb::ProcessSP ProcessFreeBSDKernel::CreateInstance(lldb::TargetSP target_sp,
7475
ListenerSP listener_sp,
@@ -82,7 +83,7 @@ lldb::ProcessSP ProcessFreeBSDKernel::CreateInstance(lldb::TargetSP target_sp,
8283
crash_file->GetPath().c_str(), nullptr, nullptr, nullptr);
8384
if (fvc)
8485
return std::make_shared<ProcessFreeBSDKernelFVC>(target_sp, listener_sp,
85-
fvc);
86+
fvc, *crash_file);
8687
#endif
8788

8889
#if defined(__FreeBSD__)
@@ -91,7 +92,7 @@ lldb::ProcessSP ProcessFreeBSDKernel::CreateInstance(lldb::TargetSP target_sp,
9192
crash_file->GetPath().c_str(), O_RDONLY, nullptr, nullptr);
9293
if (kvm)
9394
return std::make_shared<ProcessFreeBSDKernelKVM>(target_sp, listener_sp,
94-
kvm);
95+
kvm, *crash_file);
9596
#endif
9697
}
9798
return nullptr;
@@ -276,8 +277,9 @@ lldb::addr_t ProcessFreeBSDKernel::FindSymbol(const char *name) {
276277

277278
ProcessFreeBSDKernelFVC::ProcessFreeBSDKernelFVC(lldb::TargetSP target_sp,
278279
ListenerSP listener_sp,
279-
fvc_t *fvc)
280-
: ProcessFreeBSDKernel(target_sp, listener_sp), m_fvc(fvc) {}
280+
fvc_t *fvc,
281+
const FileSpec &core_file)
282+
: ProcessFreeBSDKernel(target_sp, listener_sp, crash_file), m_fvc(fvc) {}
281283

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

304306
ProcessFreeBSDKernelKVM::ProcessFreeBSDKernelKVM(lldb::TargetSP target_sp,
305307
ListenerSP listener_sp,
306-
kvm_t *fvc)
307-
: ProcessFreeBSDKernel(target_sp, listener_sp), m_kvm(fvc) {}
308+
kvm_t *fvc,
309+
const FileSpec &core_file)
310+
: ProcessFreeBSDKernel(target_sp, listener_sp, core_file), m_kvm(fvc) {}
308311

309312
ProcessFreeBSDKernelKVM::~ProcessFreeBSDKernelKVM() {
310313
if (m_kvm)

lldb/source/Plugins/Process/FreeBSDKernel/ProcessFreeBSDKernel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
class ProcessFreeBSDKernel : public lldb_private::PostMortemProcess {
1515
public:
16-
ProcessFreeBSDKernel(lldb::TargetSP target_sp, lldb::ListenerSP listener);
16+
ProcessFreeBSDKernel(lldb::TargetSP target_sp, lldb::ListenerSP listener,
17+
const lldb_private::FileSpec &core_file);
1718

1819
static lldb::ProcessSP
1920
CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener,

lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ bool ProcessElfCore::CanDebug(lldb::TargetSP target_sp,
9999
ProcessElfCore::ProcessElfCore(lldb::TargetSP target_sp,
100100
lldb::ListenerSP listener_sp,
101101
const FileSpec &core_file)
102-
: PostMortemProcess(target_sp, listener_sp), m_core_file(core_file) {}
102+
: PostMortemProcess(target_sp, listener_sp, core_file) {}
103103

104104
// Destructor
105105
ProcessElfCore::~ProcessElfCore() {
@@ -261,8 +261,8 @@ Status ProcessElfCore::DoLoadCore() {
261261
exe_module_spec.GetFileSpec().SetFile(m_nt_file_entries[0].path,
262262
FileSpec::Style::native);
263263
if (exe_module_spec.GetFileSpec()) {
264-
exe_module_sp = GetTarget().GetOrCreateModule(exe_module_spec,
265-
true /* notify */);
264+
exe_module_sp =
265+
GetTarget().GetOrCreateModule(exe_module_spec, true /* notify */);
266266
if (exe_module_sp)
267267
GetTarget().SetExecutableModule(exe_module_sp, eLoadDependentsNo);
268268
}

lldb/source/Plugins/Process/elf-core/ProcessElfCore.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ class ProcessElfCore : public lldb_private::PostMortemProcess {
127127
VMRangeToPermissions;
128128

129129
lldb::ModuleSP m_core_module_sp;
130-
lldb_private::FileSpec m_core_file;
131130
std::string m_dyld_plugin_name;
132131

133132
// True if m_thread_contexts contains valid entries

lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ bool ProcessMachCore::CanDebug(lldb::TargetSP target_sp,
112112
ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp,
113113
ListenerSP listener_sp,
114114
const FileSpec &core_file)
115-
: PostMortemProcess(target_sp, listener_sp), m_core_aranges(),
116-
m_core_range_infos(), m_core_module_sp(), m_core_file(core_file),
115+
: PostMortemProcess(target_sp, listener_sp, core_file), m_core_aranges(),
116+
m_core_range_infos(), m_core_module_sp(),
117117
m_dyld_addr(LLDB_INVALID_ADDRESS),
118118
m_mach_kernel_addr(LLDB_INVALID_ADDRESS) {}
119119

lldb/source/Plugins/Process/mach-core/ProcessMachCore.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ class ProcessMachCore : public lldb_private::PostMortemProcess {
130130
VMRangeToFileOffset m_core_aranges;
131131
VMRangeToPermissions m_core_range_infos;
132132
lldb::ModuleSP m_core_module_sp;
133-
lldb_private::FileSpec m_core_file;
134133
lldb::addr_t m_dyld_addr;
135134
lldb::addr_t m_mach_kernel_addr;
136135
llvm::StringRef m_dyld_plugin_name;

lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ ProcessMinidump::ProcessMinidump(lldb::TargetSP target_sp,
156156
lldb::ListenerSP listener_sp,
157157
const FileSpec &core_file,
158158
DataBufferSP core_data)
159-
: PostMortemProcess(target_sp, listener_sp), m_core_file(core_file),
159+
: PostMortemProcess(target_sp, listener_sp, core_file),
160160
m_core_data(std::move(core_data)), m_active_exception(nullptr),
161161
m_is_wow64(false) {}
162162

lldb/source/Plugins/Process/minidump/ProcessMinidump.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ class ProcessMinidump : public PostMortemProcess {
107107
JITLoaderList &GetJITLoaders() override;
108108

109109
private:
110-
FileSpec m_core_file;
111110
lldb::DataBufferSP m_core_data;
112111
llvm::ArrayRef<minidump::Thread> m_thread_list;
113112
const minidump::ExceptionStream *m_active_exception;

lldb/source/Target/ProcessTrace.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,
3434
bool can_connect) {
3535
if (can_connect)
3636
return nullptr;
37-
return std::make_shared<ProcessTrace>(target_sp, listener_sp);
37+
return std::make_shared<ProcessTrace>(target_sp, listener_sp, *crash_file);
3838
}
3939

4040
bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
4141
return plugin_specified_by_name;
4242
}
4343

44-
ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp)
45-
: PostMortemProcess(target_sp, listener_sp) {}
44+
ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp,
45+
const FileSpec &core_file)
46+
: PostMortemProcess(target_sp, listener_sp, core_file) {}
4647

4748
ProcessTrace::~ProcessTrace() {
4849
Clear();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,17 @@ def test_arm_core(self):
624624

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

627+
def test_get_core_file_api(self):
628+
"""
629+
Test SBProcess::GetCoreFile() API can successfully get the core file.
630+
"""
631+
core_file_name = "linux-x86_64.core"
632+
target = self.dbg.CreateTarget("linux-x86_64.out")
633+
process = target.LoadCore(core_file_name)
634+
self.assertTrue(process, PROCESS_IS_VALID)
635+
self.assertEqual(process.GetCoreFile().GetFilename(), core_file_name)
636+
self.dbg.DeleteTarget(target)
637+
627638
def check_memory_regions(self, process, region_count):
628639
region_list = process.GetMemoryRegions()
629640
self.assertEqual(region_list.GetSize(), region_count)

0 commit comments

Comments
 (0)