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/SBMemoryRegionInfo.h"
11 #include "lldb/Host/FileSystem.h"
12 #include "lldb/Symbol/SaveCoreOptions.h"
13 #include "lldb/Target/ThreadCollection.h"
14 #include "lldb/Utility/Instrumentation.h"
15
16 #include "Utils.h"
17
18 using namespace lldb;
19
SBSaveCoreOptions()20 SBSaveCoreOptions::SBSaveCoreOptions() {
21 LLDB_INSTRUMENT_VA(this)
22
23 m_opaque_up = std::make_unique<lldb_private::SaveCoreOptions>();
24 }
25
SBSaveCoreOptions(const SBSaveCoreOptions & rhs)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 SBSaveCoreOptions::~SBSaveCoreOptions() = default;
33
34 const SBSaveCoreOptions &
operator =(const SBSaveCoreOptions & rhs)35 SBSaveCoreOptions::operator=(const SBSaveCoreOptions &rhs) {
36 LLDB_INSTRUMENT_VA(this, rhs);
37
38 if (this != &rhs)
39 m_opaque_up = clone(rhs.m_opaque_up);
40 return *this;
41 }
42
SetPluginName(const char * name)43 SBError SBSaveCoreOptions::SetPluginName(const char *name) {
44 LLDB_INSTRUMENT_VA(this, name);
45 return SBError(m_opaque_up->SetPluginName(name));
46 }
47
SetStyle(lldb::SaveCoreStyle style)48 void SBSaveCoreOptions::SetStyle(lldb::SaveCoreStyle style) {
49 LLDB_INSTRUMENT_VA(this, style);
50 m_opaque_up->SetStyle(style);
51 }
52
SetOutputFile(lldb::SBFileSpec file_spec)53 void SBSaveCoreOptions::SetOutputFile(lldb::SBFileSpec file_spec) {
54 LLDB_INSTRUMENT_VA(this, file_spec);
55 m_opaque_up->SetOutputFile(file_spec.ref());
56 }
57
GetPluginName() const58 const char *SBSaveCoreOptions::GetPluginName() const {
59 LLDB_INSTRUMENT_VA(this);
60 const auto name = m_opaque_up->GetPluginName();
61 if (!name)
62 return nullptr;
63 return lldb_private::ConstString(name.value()).GetCString();
64 }
65
GetOutputFile() const66 SBFileSpec SBSaveCoreOptions::GetOutputFile() const {
67 LLDB_INSTRUMENT_VA(this);
68 const auto file_spec = m_opaque_up->GetOutputFile();
69 if (file_spec)
70 return SBFileSpec(file_spec.value());
71 return SBFileSpec();
72 }
73
GetStyle() const74 lldb::SaveCoreStyle SBSaveCoreOptions::GetStyle() const {
75 LLDB_INSTRUMENT_VA(this);
76 return m_opaque_up->GetStyle();
77 }
78
SetProcess(lldb::SBProcess process)79 SBError SBSaveCoreOptions::SetProcess(lldb::SBProcess process) {
80 LLDB_INSTRUMENT_VA(this, process);
81 return m_opaque_up->SetProcess(process.GetSP());
82 }
83
AddThread(lldb::SBThread thread)84 SBError SBSaveCoreOptions::AddThread(lldb::SBThread thread) {
85 LLDB_INSTRUMENT_VA(this, thread);
86 return m_opaque_up->AddThread(thread.GetSP());
87 }
88
RemoveThread(lldb::SBThread thread)89 bool SBSaveCoreOptions::RemoveThread(lldb::SBThread thread) {
90 LLDB_INSTRUMENT_VA(this, thread);
91 return m_opaque_up->RemoveThread(thread.GetSP());
92 }
93
94 lldb::SBError
AddMemoryRegionToSave(const SBMemoryRegionInfo & region)95 SBSaveCoreOptions::AddMemoryRegionToSave(const SBMemoryRegionInfo ®ion) {
96 LLDB_INSTRUMENT_VA(this, region);
97 // Currently add memory region can't fail, so we always return a success
98 // SBerror, but because these API's live forever, this is the most future
99 // proof thing to do.
100 m_opaque_up->AddMemoryRegionToSave(region.ref());
101 return SBError();
102 }
103
GetThreadsToSave() const104 lldb::SBThreadCollection SBSaveCoreOptions::GetThreadsToSave() const {
105 LLDB_INSTRUMENT_VA(this);
106 lldb::ThreadCollectionSP threadcollection_sp =
107 std::make_shared<lldb_private::ThreadCollection>(
108 m_opaque_up->GetThreadsToSave());
109 return SBThreadCollection(threadcollection_sp);
110 }
111
Clear()112 void SBSaveCoreOptions::Clear() {
113 LLDB_INSTRUMENT_VA(this);
114 m_opaque_up->Clear();
115 }
116
GetCurrentSizeInBytes(SBError & error)117 uint64_t SBSaveCoreOptions::GetCurrentSizeInBytes(SBError &error) {
118 LLDB_INSTRUMENT_VA(this, error);
119 llvm::Expected<uint64_t> expected_bytes =
120 m_opaque_up->GetCurrentSizeInBytes();
121 if (!expected_bytes) {
122 error =
123 SBError(lldb_private::Status::FromError(expected_bytes.takeError()));
124 return 0;
125 }
126 // Clear the error, so if the clearer uses it we set it to success.
127 error.Clear();
128 return *expected_bytes;
129 }
130
ref() const131 lldb_private::SaveCoreOptions &SBSaveCoreOptions::ref() const {
132 return *m_opaque_up;
133 }
134