Skip to content

Commit aceba7c

Browse files
Jlalondyuxuanchen1997
authored andcommitted
[LLDB][SaveCore] Add SBSaveCoreOptions Object, and SBProcess::SaveCore() overload (#98403)
Summary: This PR adds `SBSaveCoreOptions`, which is a container class for options when LLDB is taking coredumps. For this first iteration this container just keeps parity with the extant API of `file, style, plugin`. In the future this options object can be extended to allow users to take a subset of their core dumps. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251387
1 parent 9e3048d commit aceba7c

30 files changed

+412
-62
lines changed

lldb/bindings/headers.swig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "lldb/API/SBCommandReturnObject.h"
2222
#include "lldb/API/SBCommunication.h"
2323
#include "lldb/API/SBCompileUnit.h"
24+
#include "lldb/API/SBSaveCoreOptions.h"
2425
#include "lldb/API/SBData.h"
2526
#include "lldb/API/SBDebugger.h"
2627
#include "lldb/API/SBDeclaration.h"

lldb/bindings/interface/SBSaveCoreOptionsDocstrings.i

Whitespace-only changes.

lldb/bindings/interfaces.swig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
%include "./interface/SBCommandReturnObjectDocstrings.i"
2626
%include "./interface/SBCommunicationDocstrings.i"
2727
%include "./interface/SBCompileUnitDocstrings.i"
28+
%include "./interface/SBSaveCoreOptionsDocstrings.i"
2829
%include "./interface/SBDataDocstrings.i"
2930
%include "./interface/SBDebuggerDocstrings.i"
3031
%include "./interface/SBDeclarationDocstrings.i"
@@ -101,6 +102,7 @@
101102
%include "lldb/API/SBCommandReturnObject.h"
102103
%include "lldb/API/SBCommunication.h"
103104
%include "lldb/API/SBCompileUnit.h"
105+
%include "lldb/API/SBSaveCoreOptions.h"
104106
%include "lldb/API/SBData.h"
105107
%include "lldb/API/SBDebugger.h"
106108
%include "lldb/API/SBDeclaration.h"

lldb/include/lldb/API/LLDB.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "lldb/API/SBQueue.h"
5858
#include "lldb/API/SBQueueItem.h"
5959
#include "lldb/API/SBReproducer.h"
60+
#include "lldb/API/SBSaveCoreOptions.h"
6061
#include "lldb/API/SBSection.h"
6162
#include "lldb/API/SBSourceManager.h"
6263
#include "lldb/API/SBStatisticsOptions.h"

lldb/include/lldb/API/SBDefines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class LLDB_API SBCommandPluginInterface;
6161
class LLDB_API SBCommandReturnObject;
6262
class LLDB_API SBCommunication;
6363
class LLDB_API SBCompileUnit;
64+
class LLDB_API SBSaveCoreOptions;
6465
class LLDB_API SBData;
6566
class LLDB_API SBDebugger;
6667
class LLDB_API SBDeclaration;

lldb/include/lldb/API/SBError.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class LLDB_API SBError {
7777
friend class SBBreakpointName;
7878
friend class SBCommandReturnObject;
7979
friend class SBCommunication;
80+
friend class SBSaveCoreOptions;
8081
friend class SBData;
8182
friend class SBDebugger;
8283
friend class SBFile;

lldb/include/lldb/API/SBFileSpec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class LLDB_API SBFileSpec {
7878
friend class SBTarget;
7979
friend class SBThread;
8080
friend class SBTrace;
81+
friend class SBSaveCoreOptions;
8182

8283
SBFileSpec(const lldb_private::FileSpec &fspec);
8384

lldb/include/lldb/API/SBProcess.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ class LLDB_API SBProcess {
378378
/// \param[in] file_name - The name of the file to save the core file to.
379379
lldb::SBError SaveCore(const char *file_name);
380380

381+
/// Save the state of the process with the desired settings
382+
/// as defined in the options object.
383+
///
384+
/// \param[in] options - The options to use when saving the core file.
385+
lldb::SBError SaveCore(SBSaveCoreOptions &options);
386+
381387
/// Query the address load_addr and store the details of the memory
382388
/// region that contains it in the supplied SBMemoryRegionInfo object.
383389
/// To iterate over all memory regions use GetMemoryRegionList.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===-- SBSaveCoreOptions.h -------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_API_SBSAVECOREOPTIONS_H
10+
#define LLDB_API_SBSAVECOREOPTIONS_H
11+
12+
#include "lldb/API/SBDefines.h"
13+
#include "lldb/Symbol/SaveCoreOptions.h"
14+
15+
namespace lldb {
16+
17+
class LLDB_API SBSaveCoreOptions {
18+
public:
19+
SBSaveCoreOptions();
20+
SBSaveCoreOptions(const lldb::SBSaveCoreOptions &rhs);
21+
~SBSaveCoreOptions() = default;
22+
23+
const SBSaveCoreOptions &operator=(const lldb::SBSaveCoreOptions &rhs);
24+
25+
/// Set the plugin name. Supplying null or empty string will reset
26+
/// the option.
27+
///
28+
/// \param plugin Name of the object file plugin.
29+
SBError SetPluginName(const char *plugin);
30+
31+
/// Get the Core dump plugin name, if set.
32+
///
33+
/// \return The name of the plugin, or null if not set.
34+
const char *GetPluginName() const;
35+
36+
/// Set the Core dump style.
37+
///
38+
/// \param style The style of the core dump.
39+
void SetStyle(lldb::SaveCoreStyle style);
40+
41+
/// Get the Core dump style, if set.
42+
///
43+
/// \return The core dump style, or undefined if not set.
44+
lldb::SaveCoreStyle GetStyle() const;
45+
46+
/// Set the output file path
47+
///
48+
/// \param output_file a
49+
/// \class SBFileSpec object that describes the output file.
50+
void SetOutputFile(SBFileSpec output_file);
51+
52+
/// Get the output file spec
53+
///
54+
/// \return The output file spec.
55+
SBFileSpec GetOutputFile() const;
56+
57+
/// Reset all options.
58+
void Clear();
59+
60+
protected:
61+
friend class SBProcess;
62+
lldb_private::SaveCoreOptions &ref() const;
63+
64+
private:
65+
std::unique_ptr<lldb_private::SaveCoreOptions> m_opaque_up;
66+
}; // SBSaveCoreOptions
67+
} // namespace lldb
68+
69+
#endif // LLDB_API_SBSAVECOREOPTIONS_H

lldb/include/lldb/Core/PluginManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ class PluginManager {
178178

179179
static bool UnregisterPlugin(ObjectFileCreateInstance create_callback);
180180

181+
static bool IsRegisteredObjectFilePluginName(llvm::StringRef name);
182+
181183
static ObjectFileCreateInstance
182184
GetObjectFileCreateCallbackAtIndex(uint32_t idx);
183185

@@ -191,9 +193,7 @@ class PluginManager {
191193
GetObjectFileCreateMemoryCallbackForPluginName(llvm::StringRef name);
192194

193195
static Status SaveCore(const lldb::ProcessSP &process_sp,
194-
const FileSpec &outfile,
195-
lldb::SaveCoreStyle &core_style,
196-
llvm::StringRef plugin_name);
196+
const lldb_private::SaveCoreOptions &core_options);
197197

198198
// ObjectContainer
199199
static bool RegisterPlugin(
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- SaveCoreOptions.h ---------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H
10+
#define LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H
11+
12+
#include "lldb/Utility/FileSpec.h"
13+
#include "lldb/lldb-forward.h"
14+
#include "lldb/lldb-types.h"
15+
16+
#include <optional>
17+
#include <string>
18+
19+
namespace lldb_private {
20+
21+
class SaveCoreOptions {
22+
public:
23+
SaveCoreOptions(){};
24+
~SaveCoreOptions() = default;
25+
26+
lldb_private::Status SetPluginName(const char *name);
27+
std::optional<std::string> GetPluginName() const;
28+
29+
void SetStyle(lldb::SaveCoreStyle style);
30+
lldb::SaveCoreStyle GetStyle() const;
31+
32+
void SetOutputFile(lldb_private::FileSpec file);
33+
const std::optional<lldb_private::FileSpec> GetOutputFile() const;
34+
35+
void Clear();
36+
37+
private:
38+
std::optional<std::string> m_plugin_name;
39+
std::optional<lldb_private::FileSpec> m_file;
40+
std::optional<lldb::SaveCoreStyle> m_style;
41+
};
42+
} // namespace lldb_private
43+
44+
#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_LLDB_PRIVATE_INTERFACES_H
1010
#define LLDB_LLDB_PRIVATE_INTERFACES_H
1111

12+
#include "lldb/Symbol/SaveCoreOptions.h"
1213
#include "lldb/lldb-enumerations.h"
1314
#include "lldb/lldb-forward.h"
1415
#include "lldb/lldb-private-enumerations.h"
@@ -55,8 +56,7 @@ typedef ObjectFile *(*ObjectFileCreateMemoryInstance)(
5556
const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
5657
const lldb::ProcessSP &process_sp, lldb::addr_t offset);
5758
typedef bool (*ObjectFileSaveCore)(const lldb::ProcessSP &process_sp,
58-
const FileSpec &outfile,
59-
lldb::SaveCoreStyle &core_style,
59+
const lldb_private::SaveCoreOptions &options,
6060
Status &error);
6161
typedef EmulateInstruction *(*EmulateInstructionCreateInstance)(
6262
const ArchSpec &arch, InstructionType inst_type);

lldb/source/API/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ add_lldb_library(liblldb SHARED ${option_framework}
5656
SBCommandReturnObject.cpp
5757
SBCommunication.cpp
5858
SBCompileUnit.cpp
59+
SBSaveCoreOptions.cpp
5960
SBData.cpp
6061
SBDebugger.cpp
6162
SBDeclaration.cpp

lldb/source/API/SBProcess.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "lldb/API/SBFileSpec.h"
4141
#include "lldb/API/SBMemoryRegionInfo.h"
4242
#include "lldb/API/SBMemoryRegionInfoList.h"
43+
#include "lldb/API/SBSaveCoreOptions.h"
4344
#include "lldb/API/SBScriptObject.h"
4445
#include "lldb/API/SBStream.h"
4546
#include "lldb/API/SBStringList.h"
@@ -1216,13 +1217,28 @@ bool SBProcess::IsInstrumentationRuntimePresent(
12161217

12171218
lldb::SBError SBProcess::SaveCore(const char *file_name) {
12181219
LLDB_INSTRUMENT_VA(this, file_name);
1219-
return SaveCore(file_name, "", SaveCoreStyle::eSaveCoreFull);
1220+
SBSaveCoreOptions options;
1221+
options.SetOutputFile(SBFileSpec(file_name));
1222+
options.SetStyle(SaveCoreStyle::eSaveCoreFull);
1223+
return SaveCore(options);
12201224
}
12211225

12221226
lldb::SBError SBProcess::SaveCore(const char *file_name,
12231227
const char *flavor,
12241228
SaveCoreStyle core_style) {
12251229
LLDB_INSTRUMENT_VA(this, file_name, flavor, core_style);
1230+
SBSaveCoreOptions options;
1231+
options.SetOutputFile(SBFileSpec(file_name));
1232+
options.SetStyle(core_style);
1233+
SBError error = options.SetPluginName(flavor);
1234+
if (error.Fail())
1235+
return error;
1236+
return SaveCore(options);
1237+
}
1238+
1239+
lldb::SBError SBProcess::SaveCore(SBSaveCoreOptions &options) {
1240+
1241+
LLDB_INSTRUMENT_VA(this, options);
12261242

12271243
lldb::SBError error;
12281244
ProcessSP process_sp(GetSP());
@@ -1239,10 +1255,7 @@ lldb::SBError SBProcess::SaveCore(const char *file_name,
12391255
return error;
12401256
}
12411257

1242-
FileSpec core_file(file_name);
1243-
FileSystem::Instance().Resolve(core_file);
1244-
error.ref() = PluginManager::SaveCore(process_sp, core_file, core_style,
1245-
flavor);
1258+
error.ref() = PluginManager::SaveCore(process_sp, options.ref());
12461259

12471260
return error;
12481261
}

lldb/source/API/SBSaveCoreOptions.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===-- SBSaveCoreOptions.cpp -----------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lldb/API/SBSaveCoreOptions.h"
10+
#include "lldb/API/SBError.h"
11+
#include "lldb/API/SBFileSpec.h"
12+
#include "lldb/Host/FileSystem.h"
13+
#include "lldb/Symbol/SaveCoreOptions.h"
14+
#include "lldb/Utility/Instrumentation.h"
15+
16+
#include "Utils.h"
17+
18+
using namespace lldb;
19+
20+
SBSaveCoreOptions::SBSaveCoreOptions() {
21+
LLDB_INSTRUMENT_VA(this)
22+
23+
m_opaque_up = std::make_unique<lldb_private::SaveCoreOptions>();
24+
}
25+
26+
SBSaveCoreOptions::SBSaveCoreOptions(const SBSaveCoreOptions &rhs) {
27+
LLDB_INSTRUMENT_VA(this, rhs);
28+
29+
m_opaque_up = clone(rhs.m_opaque_up);
30+
}
31+
32+
const SBSaveCoreOptions &
33+
SBSaveCoreOptions::operator=(const SBSaveCoreOptions &rhs) {
34+
LLDB_INSTRUMENT_VA(this, rhs);
35+
36+
if (this != &rhs)
37+
m_opaque_up = clone(rhs.m_opaque_up);
38+
return *this;
39+
}
40+
41+
SBError SBSaveCoreOptions::SetPluginName(const char *name) {
42+
LLDB_INSTRUMENT_VA(this, name);
43+
lldb_private::Status error = m_opaque_up->SetPluginName(name);
44+
return SBError(error);
45+
}
46+
47+
void SBSaveCoreOptions::SetStyle(lldb::SaveCoreStyle style) {
48+
LLDB_INSTRUMENT_VA(this, style);
49+
m_opaque_up->SetStyle(style);
50+
}
51+
52+
void SBSaveCoreOptions::SetOutputFile(lldb::SBFileSpec file_spec) {
53+
LLDB_INSTRUMENT_VA(this, file_spec);
54+
m_opaque_up->SetOutputFile(file_spec.ref());
55+
}
56+
57+
const char *SBSaveCoreOptions::GetPluginName() const {
58+
LLDB_INSTRUMENT_VA(this);
59+
const auto name = m_opaque_up->GetPluginName();
60+
if (!name)
61+
return nullptr;
62+
return lldb_private::ConstString(name.value()).GetCString();
63+
}
64+
65+
SBFileSpec SBSaveCoreOptions::GetOutputFile() const {
66+
LLDB_INSTRUMENT_VA(this);
67+
const auto file_spec = m_opaque_up->GetOutputFile();
68+
if (file_spec)
69+
return SBFileSpec(file_spec.value());
70+
return SBFileSpec();
71+
}
72+
73+
lldb::SaveCoreStyle SBSaveCoreOptions::GetStyle() const {
74+
LLDB_INSTRUMENT_VA(this);
75+
return m_opaque_up->GetStyle();
76+
}
77+
78+
void SBSaveCoreOptions::Clear() {
79+
LLDB_INSTRUMENT_VA(this);
80+
m_opaque_up->Clear();
81+
}
82+
83+
lldb_private::SaveCoreOptions &SBSaveCoreOptions::ref() const {
84+
return *m_opaque_up.get();
85+
}

0 commit comments

Comments
 (0)