Skip to content

Commit

Permalink
[LLDB][SaveCore] Add SBSaveCoreOptions Object, and SBProcess::SaveCor…
Browse files Browse the repository at this point in the history
…e() overload (#98403)

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.
  • Loading branch information
Jlalond authored Jul 19, 2024
1 parent 996d31c commit 4120570
Show file tree
Hide file tree
Showing 30 changed files with 412 additions and 62 deletions.
1 change: 1 addition & 0 deletions lldb/bindings/headers.swig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "lldb/API/SBCommandReturnObject.h"
#include "lldb/API/SBCommunication.h"
#include "lldb/API/SBCompileUnit.h"
#include "lldb/API/SBSaveCoreOptions.h"
#include "lldb/API/SBData.h"
#include "lldb/API/SBDebugger.h"
#include "lldb/API/SBDeclaration.h"
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions lldb/bindings/interfaces.swig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
%include "./interface/SBCommandReturnObjectDocstrings.i"
%include "./interface/SBCommunicationDocstrings.i"
%include "./interface/SBCompileUnitDocstrings.i"
%include "./interface/SBSaveCoreOptionsDocstrings.i"
%include "./interface/SBDataDocstrings.i"
%include "./interface/SBDebuggerDocstrings.i"
%include "./interface/SBDeclarationDocstrings.i"
Expand Down Expand Up @@ -101,6 +102,7 @@
%include "lldb/API/SBCommandReturnObject.h"
%include "lldb/API/SBCommunication.h"
%include "lldb/API/SBCompileUnit.h"
%include "lldb/API/SBSaveCoreOptions.h"
%include "lldb/API/SBData.h"
%include "lldb/API/SBDebugger.h"
%include "lldb/API/SBDeclaration.h"
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/LLDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "lldb/API/SBQueue.h"
#include "lldb/API/SBQueueItem.h"
#include "lldb/API/SBReproducer.h"
#include "lldb/API/SBSaveCoreOptions.h"
#include "lldb/API/SBSection.h"
#include "lldb/API/SBSourceManager.h"
#include "lldb/API/SBStatisticsOptions.h"
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class LLDB_API SBCommandPluginInterface;
class LLDB_API SBCommandReturnObject;
class LLDB_API SBCommunication;
class LLDB_API SBCompileUnit;
class LLDB_API SBSaveCoreOptions;
class LLDB_API SBData;
class LLDB_API SBDebugger;
class LLDB_API SBDeclaration;
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBError.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class LLDB_API SBError {
friend class SBBreakpointName;
friend class SBCommandReturnObject;
friend class SBCommunication;
friend class SBSaveCoreOptions;
friend class SBData;
friend class SBDebugger;
friend class SBFile;
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBFileSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class LLDB_API SBFileSpec {
friend class SBTarget;
friend class SBThread;
friend class SBTrace;
friend class SBSaveCoreOptions;

SBFileSpec(const lldb_private::FileSpec &fspec);

Expand Down
6 changes: 6 additions & 0 deletions lldb/include/lldb/API/SBProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ class LLDB_API SBProcess {
/// \param[in] file_name - The name of the file to save the core file to.
lldb::SBError SaveCore(const char *file_name);

/// Save the state of the process with the desired settings
/// as defined in the options object.
///
/// \param[in] options - The options to use when saving the core file.
lldb::SBError SaveCore(SBSaveCoreOptions &options);

/// Query the address load_addr and store the details of the memory
/// region that contains it in the supplied SBMemoryRegionInfo object.
/// To iterate over all memory regions use GetMemoryRegionList.
Expand Down
69 changes: 69 additions & 0 deletions lldb/include/lldb/API/SBSaveCoreOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//===-- SBSaveCoreOptions.h -------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_API_SBSAVECOREOPTIONS_H
#define LLDB_API_SBSAVECOREOPTIONS_H

#include "lldb/API/SBDefines.h"
#include "lldb/Symbol/SaveCoreOptions.h"

namespace lldb {

class LLDB_API SBSaveCoreOptions {
public:
SBSaveCoreOptions();
SBSaveCoreOptions(const lldb::SBSaveCoreOptions &rhs);
~SBSaveCoreOptions() = default;

const SBSaveCoreOptions &operator=(const lldb::SBSaveCoreOptions &rhs);

/// Set the plugin name. Supplying null or empty string will reset
/// the option.
///
/// \param plugin Name of the object file plugin.
SBError SetPluginName(const char *plugin);

/// Get the Core dump plugin name, if set.
///
/// \return The name of the plugin, or null if not set.
const char *GetPluginName() const;

/// Set the Core dump style.
///
/// \param style The style of the core dump.
void SetStyle(lldb::SaveCoreStyle style);

/// Get the Core dump style, if set.
///
/// \return The core dump style, or undefined if not set.
lldb::SaveCoreStyle GetStyle() const;

/// Set the output file path
///
/// \param output_file a
/// \class SBFileSpec object that describes the output file.
void SetOutputFile(SBFileSpec output_file);

/// Get the output file spec
///
/// \return The output file spec.
SBFileSpec GetOutputFile() const;

/// Reset all options.
void Clear();

protected:
friend class SBProcess;
lldb_private::SaveCoreOptions &ref() const;

private:
std::unique_ptr<lldb_private::SaveCoreOptions> m_opaque_up;
}; // SBSaveCoreOptions
} // namespace lldb

#endif // LLDB_API_SBSAVECOREOPTIONS_H
6 changes: 3 additions & 3 deletions lldb/include/lldb/Core/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ class PluginManager {

static bool UnregisterPlugin(ObjectFileCreateInstance create_callback);

static bool IsRegisteredObjectFilePluginName(llvm::StringRef name);

static ObjectFileCreateInstance
GetObjectFileCreateCallbackAtIndex(uint32_t idx);

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

static Status SaveCore(const lldb::ProcessSP &process_sp,
const FileSpec &outfile,
lldb::SaveCoreStyle &core_style,
llvm::StringRef plugin_name);
const lldb_private::SaveCoreOptions &core_options);

// ObjectContainer
static bool RegisterPlugin(
Expand Down
44 changes: 44 additions & 0 deletions lldb/include/lldb/Symbol/SaveCoreOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===-- SaveCoreOptions.h ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H
#define LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H

#include "lldb/Utility/FileSpec.h"
#include "lldb/lldb-forward.h"
#include "lldb/lldb-types.h"

#include <optional>
#include <string>

namespace lldb_private {

class SaveCoreOptions {
public:
SaveCoreOptions(){};
~SaveCoreOptions() = default;

lldb_private::Status SetPluginName(const char *name);
std::optional<std::string> GetPluginName() const;

void SetStyle(lldb::SaveCoreStyle style);
lldb::SaveCoreStyle GetStyle() const;

void SetOutputFile(lldb_private::FileSpec file);
const std::optional<lldb_private::FileSpec> GetOutputFile() const;

void Clear();

private:
std::optional<std::string> m_plugin_name;
std::optional<lldb_private::FileSpec> m_file;
std::optional<lldb::SaveCoreStyle> m_style;
};
} // namespace lldb_private

#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H
4 changes: 2 additions & 2 deletions lldb/include/lldb/lldb-private-interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLDB_LLDB_PRIVATE_INTERFACES_H
#define LLDB_LLDB_PRIVATE_INTERFACES_H

#include "lldb/Symbol/SaveCoreOptions.h"
#include "lldb/lldb-enumerations.h"
#include "lldb/lldb-forward.h"
#include "lldb/lldb-private-enumerations.h"
Expand Down Expand Up @@ -55,8 +56,7 @@ typedef ObjectFile *(*ObjectFileCreateMemoryInstance)(
const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
const lldb::ProcessSP &process_sp, lldb::addr_t offset);
typedef bool (*ObjectFileSaveCore)(const lldb::ProcessSP &process_sp,
const FileSpec &outfile,
lldb::SaveCoreStyle &core_style,
const lldb_private::SaveCoreOptions &options,
Status &error);
typedef EmulateInstruction *(*EmulateInstructionCreateInstance)(
const ArchSpec &arch, InstructionType inst_type);
Expand Down
1 change: 1 addition & 0 deletions lldb/source/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ add_lldb_library(liblldb SHARED ${option_framework}
SBCommandReturnObject.cpp
SBCommunication.cpp
SBCompileUnit.cpp
SBSaveCoreOptions.cpp
SBData.cpp
SBDebugger.cpp
SBDeclaration.cpp
Expand Down
23 changes: 18 additions & 5 deletions lldb/source/API/SBProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "lldb/API/SBFileSpec.h"
#include "lldb/API/SBMemoryRegionInfo.h"
#include "lldb/API/SBMemoryRegionInfoList.h"
#include "lldb/API/SBSaveCoreOptions.h"
#include "lldb/API/SBScriptObject.h"
#include "lldb/API/SBStream.h"
#include "lldb/API/SBStringList.h"
Expand Down Expand Up @@ -1216,13 +1217,28 @@ bool SBProcess::IsInstrumentationRuntimePresent(

lldb::SBError SBProcess::SaveCore(const char *file_name) {
LLDB_INSTRUMENT_VA(this, file_name);
return SaveCore(file_name, "", SaveCoreStyle::eSaveCoreFull);
SBSaveCoreOptions options;
options.SetOutputFile(SBFileSpec(file_name));
options.SetStyle(SaveCoreStyle::eSaveCoreFull);
return SaveCore(options);
}

lldb::SBError SBProcess::SaveCore(const char *file_name,
const char *flavor,
SaveCoreStyle core_style) {
LLDB_INSTRUMENT_VA(this, file_name, flavor, core_style);
SBSaveCoreOptions options;
options.SetOutputFile(SBFileSpec(file_name));
options.SetStyle(core_style);
SBError error = options.SetPluginName(flavor);
if (error.Fail())
return error;
return SaveCore(options);
}

lldb::SBError SBProcess::SaveCore(SBSaveCoreOptions &options) {

LLDB_INSTRUMENT_VA(this, options);

lldb::SBError error;
ProcessSP process_sp(GetSP());
Expand All @@ -1239,10 +1255,7 @@ lldb::SBError SBProcess::SaveCore(const char *file_name,
return error;
}

FileSpec core_file(file_name);
FileSystem::Instance().Resolve(core_file);
error.ref() = PluginManager::SaveCore(process_sp, core_file, core_style,
flavor);
error.ref() = PluginManager::SaveCore(process_sp, options.ref());

return error;
}
Expand Down
85 changes: 85 additions & 0 deletions lldb/source/API/SBSaveCoreOptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===-- SBSaveCoreOptions.cpp -----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/API/SBSaveCoreOptions.h"
#include "lldb/API/SBError.h"
#include "lldb/API/SBFileSpec.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Symbol/SaveCoreOptions.h"
#include "lldb/Utility/Instrumentation.h"

#include "Utils.h"

using namespace lldb;

SBSaveCoreOptions::SBSaveCoreOptions() {
LLDB_INSTRUMENT_VA(this)

m_opaque_up = std::make_unique<lldb_private::SaveCoreOptions>();
}

SBSaveCoreOptions::SBSaveCoreOptions(const SBSaveCoreOptions &rhs) {
LLDB_INSTRUMENT_VA(this, rhs);

m_opaque_up = clone(rhs.m_opaque_up);
}

const SBSaveCoreOptions &
SBSaveCoreOptions::operator=(const SBSaveCoreOptions &rhs) {
LLDB_INSTRUMENT_VA(this, rhs);

if (this != &rhs)
m_opaque_up = clone(rhs.m_opaque_up);
return *this;
}

SBError SBSaveCoreOptions::SetPluginName(const char *name) {
LLDB_INSTRUMENT_VA(this, name);
lldb_private::Status error = m_opaque_up->SetPluginName(name);
return SBError(error);
}

void SBSaveCoreOptions::SetStyle(lldb::SaveCoreStyle style) {
LLDB_INSTRUMENT_VA(this, style);
m_opaque_up->SetStyle(style);
}

void SBSaveCoreOptions::SetOutputFile(lldb::SBFileSpec file_spec) {
LLDB_INSTRUMENT_VA(this, file_spec);
m_opaque_up->SetOutputFile(file_spec.ref());
}

const char *SBSaveCoreOptions::GetPluginName() const {
LLDB_INSTRUMENT_VA(this);
const auto name = m_opaque_up->GetPluginName();
if (!name)
return nullptr;
return lldb_private::ConstString(name.value()).GetCString();
}

SBFileSpec SBSaveCoreOptions::GetOutputFile() const {
LLDB_INSTRUMENT_VA(this);
const auto file_spec = m_opaque_up->GetOutputFile();
if (file_spec)
return SBFileSpec(file_spec.value());
return SBFileSpec();
}

lldb::SaveCoreStyle SBSaveCoreOptions::GetStyle() const {
LLDB_INSTRUMENT_VA(this);
return m_opaque_up->GetStyle();
}

void SBSaveCoreOptions::Clear() {
LLDB_INSTRUMENT_VA(this);
m_opaque_up->Clear();
}

lldb_private::SaveCoreOptions &SBSaveCoreOptions::ref() const {
return *m_opaque_up.get();
}
Loading

0 comments on commit 4120570

Please sign in to comment.