Skip to content

Commit 0123cef

Browse files
jeffreytan81jeffreytan81
andauthored
Add a new SBProcess:: GetCoreFile() API (llvm#80767)
We have a Python script that needs to locate coredump path during debugging so that we can retrieve certain metadata files associated with it. Currently, there is no API for this. This patch adds a new `SBProcess::GetCoreFile()` to retrieve target dump file spec used for dump debugging. Note: this is different from the main executable module spec. To achieve this, the patch hoists m_core_file into PostMortemProcess for sharing. --------- Co-authored-by: jeffreytan81 <[email protected]>
1 parent c0cb0be commit 0123cef

File tree

15 files changed

+73
-23
lines changed

15 files changed

+73
-23
lines changed

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
@@ -1503,6 +1503,13 @@ class Process : public std::enable_shared_from_this<Process>,
15031503

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

1506+
/// Provide a way to retrieve the core dump file that is loaded for debugging.
1507+
/// Only available if IsLiveDebugSession() returns true.
1508+
///
1509+
/// \return
1510+
/// File path to the core file.
1511+
virtual FileSpec GetCoreFile() const { return {}; }
1512+
15061513
/// Before lldb detaches from a process, it warns the user that they are
15071514
/// about to lose their debug session. In some cases, this warning doesn't
15081515
/// 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
@@ -111,8 +111,8 @@ bool ProcessMachCore::CanDebug(lldb::TargetSP target_sp,
111111
ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp,
112112
ListenerSP listener_sp,
113113
const FileSpec &core_file)
114-
: PostMortemProcess(target_sp, listener_sp), m_core_aranges(),
115-
m_core_range_infos(), m_core_module_sp(), m_core_file(core_file),
114+
: PostMortemProcess(target_sp, listener_sp, core_file), m_core_aranges(),
115+
m_core_range_infos(), m_core_module_sp(),
116116
m_dyld_addr(LLDB_INVALID_ADDRESS),
117117
m_mach_kernel_addr(LLDB_INVALID_ADDRESS) {}
118118

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
@@ -36,15 +36,16 @@ ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,
3636
bool can_connect) {
3737
if (can_connect)
3838
return nullptr;
39-
return std::make_shared<ProcessTrace>(target_sp, listener_sp);
39+
return std::make_shared<ProcessTrace>(target_sp, listener_sp, *crash_file);
4040
}
4141

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

46-
ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp)
47-
: PostMortemProcess(target_sp, listener_sp) {}
46+
ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp,
47+
const FileSpec &core_file)
48+
: PostMortemProcess(target_sp, listener_sp, core_file) {}
4849

4950
ProcessTrace::~ProcessTrace() {
5051
Clear();

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

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

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

636+
def test_get_core_file_api(self):
637+
"""
638+
Test SBProcess::GetCoreFile() API can successfully get the core file.
639+
"""
640+
core_file_name = "linux-x86_64.core"
641+
target = self.dbg.CreateTarget("linux-x86_64.out")
642+
process = target.LoadCore(core_file_name)
643+
self.assertTrue(process, PROCESS_IS_VALID)
644+
self.assertEqual(process.GetCoreFile().GetFilename(), core_file_name)
645+
self.dbg.DeleteTarget(target)
646+
636647
def check_memory_regions(self, process, region_count):
637648
region_list = process.GetMemoryRegions()
638649
self.assertEqual(region_list.GetSize(), region_count)

0 commit comments

Comments
 (0)