Skip to content

Commit c1160b5

Browse files
Jlalondadrian-prantl
authored andcommitted
[LLDB] Reapply llvm#100443 SBSaveCore Thread list (llvm#104497)
Reapply llvm#100443 and llvm#101770. These were originally reverted due to a test failure and an MSAN failure. I changed the test attribute to restrict to x86 (following the other existing tests). I could not reproduce the test or the MSAN failure and no repo steps were provided. (cherry picked from commit 572943e)
1 parent fbf0ce9 commit c1160b5

26 files changed

+458
-84
lines changed

lldb/include/lldb/API/SBProcess.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ class LLDB_API SBProcess {
586586
friend class SBBreakpointCallbackBaton;
587587
friend class SBBreakpointLocation;
588588
friend class SBCommandInterpreter;
589+
friend class SBSaveCoreOptions;
589590
friend class SBDebugger;
590591
friend class SBExecutionContext;
591592
friend class SBFunction;

lldb/include/lldb/API/SBSaveCoreOptions.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#define LLDB_API_SBSAVECOREOPTIONS_H
1111

1212
#include "lldb/API/SBDefines.h"
13+
#include "lldb/API/SBError.h"
14+
#include "lldb/API/SBFileSpec.h"
15+
#include "lldb/API/SBProcess.h"
16+
#include "lldb/API/SBThread.h"
1317

1418
namespace lldb {
1519

@@ -53,6 +57,29 @@ class LLDB_API SBSaveCoreOptions {
5357
/// \return The output file spec.
5458
SBFileSpec GetOutputFile() const;
5559

60+
/// Set the process to save, or unset if supplied with a default constructed
61+
/// process.
62+
///
63+
/// \param process The process to save.
64+
/// \return Success if process was set, otherwise an error
65+
/// \note This will clear all process specific options if a different process
66+
/// is specified than the current set process, either explicitly from this
67+
/// api, or implicitly from any function that requires a process.
68+
SBError SetProcess(lldb::SBProcess process);
69+
70+
/// Add a thread to save in the core file.
71+
///
72+
/// \param thread The thread to save.
73+
/// \note This will set the process if it is not already set, or return
74+
/// and error if the SBThread is not from the set process.
75+
SBError AddThread(lldb::SBThread thread);
76+
77+
/// Remove a thread from the list of threads to save.
78+
///
79+
/// \param thread The thread to remove.
80+
/// \return True if the thread was removed, false if it was not in the list.
81+
bool RemoveThread(lldb::SBThread thread);
82+
5683
/// Reset all options.
5784
void Clear();
5885

lldb/include/lldb/API/SBThread.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class LLDB_API SBThread {
237237
friend class SBBreakpoint;
238238
friend class SBBreakpointLocation;
239239
friend class SBBreakpointCallbackBaton;
240+
friend class SBSaveCoreOptions;
240241
friend class SBExecutionContext;
241242
friend class SBFrame;
242243
friend class SBProcess;
@@ -257,6 +258,8 @@ class LLDB_API SBThread {
257258
SBError ResumeNewPlan(lldb_private::ExecutionContext &exe_ctx,
258259
lldb_private::ThreadPlan *new_plan);
259260

261+
lldb::ThreadSP GetSP() const;
262+
260263
lldb::ExecutionContextRefSP m_opaque_sp;
261264

262265
lldb_private::Thread *operator->();

lldb/include/lldb/Core/PluginManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class PluginManager {
194194
GetObjectFileCreateMemoryCallbackForPluginName(llvm::StringRef name);
195195

196196
static Status SaveCore(const lldb::ProcessSP &process_sp,
197-
const lldb_private::SaveCoreOptions &core_options);
197+
lldb_private::SaveCoreOptions &core_options);
198198

199199
// ObjectContainer
200200
static bool RegisterPlugin(

lldb/include/lldb/Symbol/SaveCoreOptions.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <optional>
1717
#include <string>
18+
#include <unordered_set>
1819

1920
namespace lldb_private {
2021

@@ -32,13 +33,25 @@ class SaveCoreOptions {
3233
void SetOutputFile(lldb_private::FileSpec file);
3334
const std::optional<lldb_private::FileSpec> GetOutputFile() const;
3435

36+
Status SetProcess(lldb::ProcessSP process_sp);
37+
38+
Status AddThread(lldb::ThreadSP thread_sp);
39+
bool RemoveThread(lldb::ThreadSP thread_sp);
40+
bool ShouldThreadBeSaved(lldb::tid_t tid) const;
41+
42+
Status EnsureValidConfiguration(lldb::ProcessSP process_sp) const;
43+
3544
void Clear();
3645

3746
private:
47+
void ClearProcessSpecificData();
48+
3849
std::optional<std::string> m_plugin_name;
3950
std::optional<lldb_private::FileSpec> m_file;
4051
std::optional<lldb::SaveCoreStyle> m_style;
52+
lldb::ProcessSP m_process_sp;
53+
std::unordered_set<lldb::tid_t> m_threads_to_save;
4154
};
4255
} // namespace lldb_private
4356

44-
#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H
57+
#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_SAVECOREOPTIONS_H

lldb/include/lldb/Target/Process.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,15 @@ class Process : public std::enable_shared_from_this<Process>,
741741
/// Helper function for Process::SaveCore(...) that calculates the address
742742
/// ranges that should be saved. This allows all core file plug-ins to save
743743
/// consistent memory ranges given a \a core_style.
744-
Status CalculateCoreFileSaveRanges(lldb::SaveCoreStyle core_style,
744+
Status CalculateCoreFileSaveRanges(const SaveCoreOptions &core_options,
745745
CoreFileMemoryRanges &ranges);
746746

747+
/// Helper function for Process::SaveCore(...) that calculates the thread list
748+
/// based upon options set within a given \a core_options object.
749+
/// \note If there is no thread list defined, all threads will be saved.
750+
std::vector<lldb::ThreadSP>
751+
CalculateCoreFileThreadList(const SaveCoreOptions &core_options);
752+
747753
protected:
748754
virtual JITLoaderList &GetJITLoaders();
749755

lldb/include/lldb/lldb-private-interfaces.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ typedef ObjectFile *(*ObjectFileCreateMemoryInstance)(
5757
const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
5858
const lldb::ProcessSP &process_sp, lldb::addr_t offset);
5959
typedef bool (*ObjectFileSaveCore)(const lldb::ProcessSP &process_sp,
60-
const lldb_private::SaveCoreOptions &options,
60+
lldb_private::SaveCoreOptions &options,
6161
Status &error);
6262
typedef EmulateInstruction *(*EmulateInstructionCreateInstance)(
6363
const ArchSpec &arch, InstructionType inst_type);

lldb/source/API/SBSaveCoreOptions.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBSaveCoreOptions.h"
10-
#include "lldb/API/SBError.h"
11-
#include "lldb/API/SBFileSpec.h"
1210
#include "lldb/Host/FileSystem.h"
1311
#include "lldb/Symbol/SaveCoreOptions.h"
1412
#include "lldb/Utility/Instrumentation.h"
@@ -77,6 +75,21 @@ lldb::SaveCoreStyle SBSaveCoreOptions::GetStyle() const {
7775
return m_opaque_up->GetStyle();
7876
}
7977

78+
SBError SBSaveCoreOptions::SetProcess(lldb::SBProcess process) {
79+
LLDB_INSTRUMENT_VA(this, process);
80+
return m_opaque_up->SetProcess(process.GetSP());
81+
}
82+
83+
SBError SBSaveCoreOptions::AddThread(lldb::SBThread thread) {
84+
LLDB_INSTRUMENT_VA(this, thread);
85+
return m_opaque_up->AddThread(thread.GetSP());
86+
}
87+
88+
bool SBSaveCoreOptions::RemoveThread(lldb::SBThread thread) {
89+
LLDB_INSTRUMENT_VA(this, thread);
90+
return m_opaque_up->RemoveThread(thread.GetSP());
91+
}
92+
8093
void SBSaveCoreOptions::Clear() {
8194
LLDB_INSTRUMENT_VA(this);
8295
m_opaque_up->Clear();

lldb/source/API/SBThread.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,8 @@ bool SBThread::SafeToCallFunctions() {
13591359
return true;
13601360
}
13611361

1362+
lldb::ThreadSP SBThread::GetSP() const { return m_opaque_sp->GetThreadSP(); }
1363+
13621364
lldb_private::Thread *SBThread::operator->() {
13631365
return get();
13641366
}

lldb/source/Core/PluginManager.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ PluginManager::GetObjectFileCreateMemoryCallbackForPluginName(
702702
}
703703

704704
Status PluginManager::SaveCore(const lldb::ProcessSP &process_sp,
705-
const lldb_private::SaveCoreOptions &options) {
705+
lldb_private::SaveCoreOptions &options) {
706706
Status error;
707707
if (!options.GetOutputFile()) {
708708
error.SetErrorString("No output file specified");
@@ -714,6 +714,10 @@ Status PluginManager::SaveCore(const lldb::ProcessSP &process_sp,
714714
return error;
715715
}
716716

717+
error = options.EnsureValidConfiguration(process_sp);
718+
if (error.Fail())
719+
return error;
720+
717721
if (!options.GetPluginName().has_value()) {
718722
// Try saving core directly from the process plugin first.
719723
llvm::Expected<bool> ret =

0 commit comments

Comments
 (0)