LLDB mainline
SBCommandReturnObject.cpp
Go to the documentation of this file.
1//===-- SBCommandReturnObject.cpp -----------------------------------------===//
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
10#include "Utils.h"
11#include "lldb/API/SBError.h"
12#include "lldb/API/SBFile.h"
13#include "lldb/API/SBStream.h"
19#include "lldb/Utility/Status.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
25public:
28 : m_ptr(&ref), m_owned(false) {}
30 : m_ptr(new CommandReturnObject(*rhs.m_ptr)), m_owned(rhs.m_owned) {}
33 std::swap(*this, copy);
34 return *this;
35 }
36 // rvalue ctor+assignment are not used by SBCommandReturnObject.
38 if (m_owned)
39 delete m_ptr;
40 }
41
42 CommandReturnObject &operator*() const { return *m_ptr; }
43
44private:
46 bool m_owned = true;
47};
48
50 : m_opaque_up(new SBCommandReturnObjectImpl()) {
52}
53
55 : m_opaque_up(new SBCommandReturnObjectImpl(ref)) {
57}
58
60 LLDB_INSTRUMENT_VA(this, rhs);
61
63}
64
67 LLDB_INSTRUMENT_VA(this, rhs);
68
69 if (this != &rhs)
71 return *this;
72}
73
75
78 return this->operator bool();
79}
80SBCommandReturnObject::operator bool() const {
82
83 // This method is not useful but it needs to stay to keep SB API stable.
84 return true;
85}
86
89
90 ConstString output(ref().GetOutputString());
91 return output.AsCString(/*value_if_empty*/ "");
92}
93
96
97 ConstString output(ref().GetErrorString());
98 return output.AsCString(/*value_if_empty*/ "");
99}
100
102 LLDB_INSTRUMENT_VA(this);
103
105 SBStructuredData sb_data;
106 sb_data.m_impl_up->SetObjectSP(data);
107 return sb_data;
108}
109
111 LLDB_INSTRUMENT_VA(this);
112
113 return ref().GetOutputString().size();
114}
115
117 LLDB_INSTRUMENT_VA(this);
118
119 return ref().GetErrorString().size();
120}
121
122size_t SBCommandReturnObject::PutOutput(FILE *fh) {
123 LLDB_INSTRUMENT_VA(this, fh);
124 if (fh) {
125 size_t num_bytes = GetOutputSize();
126 if (num_bytes)
127 return ::fprintf(fh, "%s", GetOutput());
128 }
129 return 0;
130}
131
133 LLDB_INSTRUMENT_VA(this, file_sp);
134 if (!file_sp)
135 return 0;
136 return file_sp->Printf("%s", GetOutput());
137}
138
140 LLDB_INSTRUMENT_VA(this, file);
141 if (!file.m_opaque_sp)
142 return 0;
143 return file.m_opaque_sp->Printf("%s", GetOutput());
144}
145
146size_t SBCommandReturnObject::PutError(FILE *fh) {
147 LLDB_INSTRUMENT_VA(this, fh);
148 if (fh) {
149 size_t num_bytes = GetErrorSize();
150 if (num_bytes)
151 return ::fprintf(fh, "%s", GetError());
152 }
153 return 0;
154}
155
157 LLDB_INSTRUMENT_VA(this, file_sp);
158 if (!file_sp)
159 return 0;
160 return file_sp->Printf("%s", GetError());
161}
162
164 LLDB_INSTRUMENT_VA(this, file);
165 if (!file.m_opaque_sp)
166 return 0;
167 return file.m_opaque_sp->Printf("%s", GetError());
168}
169
171 LLDB_INSTRUMENT_VA(this);
172
173 ref().Clear();
174}
175
177 LLDB_INSTRUMENT_VA(this);
178
179 return ref().GetStatus();
180}
181
183 LLDB_INSTRUMENT_VA(this, status);
184
185 ref().SetStatus(status);
186}
187
189 LLDB_INSTRUMENT_VA(this);
190
191 return ref().Succeeded();
192}
193
195 LLDB_INSTRUMENT_VA(this);
196
197 return ref().HasResult();
198}
199
200void SBCommandReturnObject::AppendMessage(const char *message) {
201 LLDB_INSTRUMENT_VA(this, message);
202
203 ref().AppendMessage(message);
204}
205
206void SBCommandReturnObject::AppendWarning(const char *message) {
207 LLDB_INSTRUMENT_VA(this, message);
208
209 ref().AppendWarning(message);
210}
211
213 return &**m_opaque_up;
214}
215
217 return &**m_opaque_up;
218}
219
221 return **m_opaque_up;
222}
223
225 return **m_opaque_up;
226}
227
229 LLDB_INSTRUMENT_VA(this, description);
230
231 Stream &strm = description.ref();
232
233 description.Printf("Error: ");
234 lldb::ReturnStatus status = ref().GetStatus();
235 if (status == lldb::eReturnStatusStarted)
236 strm.PutCString("Started");
237 else if (status == lldb::eReturnStatusInvalid)
238 strm.PutCString("Invalid");
239 else if (ref().Succeeded())
240 strm.PutCString("Success");
241 else
242 strm.PutCString("Fail");
243
244 if (GetOutputSize() > 0)
245 strm.Printf("\nOutput Message:\n%s", GetOutput());
246
247 if (GetErrorSize() > 0)
248 strm.Printf("\nError Message:\n%s", GetError());
249
250 return true;
251}
252
254 LLDB_INSTRUMENT_VA(this, fh);
255
256 SetImmediateOutputFile(fh, false);
257}
258
260 LLDB_INSTRUMENT_VA(this, fh);
261
262 SetImmediateErrorFile(fh, false);
263}
264
266 bool transfer_ownership) {
268 FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
270}
271
273 bool transfer_ownership) {
275 FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
277}
278
280 LLDB_INSTRUMENT_VA(this, file);
282}
283
285 LLDB_INSTRUMENT_VA(this, file);
287}
288
290 LLDB_INSTRUMENT_VA(this, file_sp);
292}
293
295 LLDB_INSTRUMENT_VA(this, file_sp);
297}
298
299void SBCommandReturnObject::PutCString(const char *string, int len) {
300 LLDB_INSTRUMENT_VA(this, string, len);
301
302 if (len == 0 || string == nullptr || *string == 0) {
303 return;
304 } else if (len > 0) {
305 std::string buffer(string, len);
306 ref().AppendMessage(buffer.c_str());
307 } else
308 ref().AppendMessage(string);
309}
310
311const char *SBCommandReturnObject::GetOutput(bool only_if_no_immediate) {
312 LLDB_INSTRUMENT_VA(this, only_if_no_immediate);
313
314 if (!only_if_no_immediate ||
315 ref().GetImmediateOutputStream().get() == nullptr)
316 return GetOutput();
317 return nullptr;
318}
319
320const char *SBCommandReturnObject::GetError(bool only_if_no_immediate) {
321 LLDB_INSTRUMENT_VA(this, only_if_no_immediate);
322
323 if (!only_if_no_immediate || ref().GetImmediateErrorStream().get() == nullptr)
324 return GetError();
325 return nullptr;
326}
327
328size_t SBCommandReturnObject::Printf(const char *format, ...) {
329 va_list args;
330 va_start(args, format);
331 size_t result = ref().GetOutputStream().PrintfVarArg(format, args);
332 va_end(args);
333 return result;
334}
335
337 const char *fallback_error_cstr) {
338 LLDB_INSTRUMENT_VA(this, error, fallback_error_cstr);
339
340 if (error.IsValid() && !error.Fail())
341 ref().SetError(error.ref().Clone());
342 else if (fallback_error_cstr)
343 ref().SetError(Status::FromErrorString(fallback_error_cstr));
344}
345
346void SBCommandReturnObject::SetError(const char *error_cstr) {
347 LLDB_INSTRUMENT_VA(this, error_cstr);
348
349 if (error_cstr)
350 ref().AppendError(error_cstr);
351}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
lldb_private::CommandReturnObject * get() const
lldb_private::CommandReturnObject & operator*() const
void SetStatus(lldb::ReturnStatus status)
bool GetDescription(lldb::SBStream &description)
lldb_private::CommandReturnObject * operator->() const
lldb::SBCommandReturnObject & operator=(const lldb::SBCommandReturnObject &rhs)
void SetError(lldb::SBError &error, const char *fallback_error_cstr=nullptr)
void AppendMessage(const char *message)
lldb_private::CommandReturnObject & ref() const
void AppendWarning(const char *message)
void PutCString(const char *string, int len=-1)
std::unique_ptr< lldb_private::SBCommandReturnObjectImpl > m_opaque_up
FileSP m_opaque_sp
Definition: SBFile.h:51
lldb_private::Stream & ref()
Definition: SBStream.cpp:177
StructuredDataImplUP m_impl_up
void AppendMessage(llvm::StringRef in_string)
void SetImmediateErrorFile(lldb::FileSP file_sp)
void void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
std::string GetErrorString(bool with_diagnostics=true)
Return the errors as a string.
lldb::ReturnStatus GetStatus() const
void SetImmediateOutputFile(lldb::FileSP file_sp)
void void AppendWarning(llvm::StringRef in_string)
A uniqued constant string class.
Definition: ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
SBCommandReturnObjectImpl(const SBCommandReturnObjectImpl &rhs)
SBCommandReturnObjectImpl(CommandReturnObject &ref)
SBCommandReturnObjectImpl & operator=(const SBCommandReturnObjectImpl &rhs)
static Status FromErrorString(const char *str)
Definition: Status.h:138
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition: Stream.cpp:143
std::shared_ptr< Object > ObjectSP
A class that represents a running process on the host machine.
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
Definition: SBAddress.h:15
ReturnStatus
Command Return Status Types.
@ eReturnStatusStarted
@ eReturnStatusInvalid
std::shared_ptr< lldb_private::File > FileSP
Definition: lldb-forward.h:353