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"
17#include "lldb/Utility/Status.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
23public:
26 : m_ptr(&ref), m_owned(false) {}
28 : m_ptr(new CommandReturnObject(*rhs.m_ptr)), m_owned(rhs.m_owned) {}
31 std::swap(*this, copy);
32 return *this;
33 }
34 // rvalue ctor+assignment are not used by SBCommandReturnObject.
36 if (m_owned)
37 delete m_ptr;
38 }
39
40 CommandReturnObject &operator*() const { return *m_ptr; }
41
42private:
44 bool m_owned = true;
45};
46
48 : m_opaque_up(new SBCommandReturnObjectImpl()) {
50}
51
53 : m_opaque_up(new SBCommandReturnObjectImpl(ref)) {
55}
56
58 LLDB_INSTRUMENT_VA(this, rhs);
59
61}
62
65 LLDB_INSTRUMENT_VA(this, rhs);
66
67 if (this != &rhs)
69 return *this;
70}
71
73
76 return this->operator bool();
77}
78SBCommandReturnObject::operator bool() const {
80
81 // This method is not useful but it needs to stay to keep SB API stable.
82 return true;
83}
84
87
88 ConstString output(ref().GetOutputData());
89 return output.AsCString(/*value_if_empty*/ "");
90}
91
94
95 ConstString output(ref().GetErrorData());
96 return output.AsCString(/*value_if_empty*/ "");
97}
98
100 LLDB_INSTRUMENT_VA(this);
101
102 return ref().GetOutputData().size();
103}
104
106 LLDB_INSTRUMENT_VA(this);
107
108 return ref().GetErrorData().size();
109}
110
111size_t SBCommandReturnObject::PutOutput(FILE *fh) {
112 LLDB_INSTRUMENT_VA(this, fh);
113 if (fh) {
114 size_t num_bytes = GetOutputSize();
115 if (num_bytes)
116 return ::fprintf(fh, "%s", GetOutput());
117 }
118 return 0;
119}
120
122 LLDB_INSTRUMENT_VA(this, file_sp);
123 if (!file_sp)
124 return 0;
125 return file_sp->Printf("%s", GetOutput());
126}
127
129 LLDB_INSTRUMENT_VA(this, file);
130 if (!file.m_opaque_sp)
131 return 0;
132 return file.m_opaque_sp->Printf("%s", GetOutput());
133}
134
135size_t SBCommandReturnObject::PutError(FILE *fh) {
136 LLDB_INSTRUMENT_VA(this, fh);
137 if (fh) {
138 size_t num_bytes = GetErrorSize();
139 if (num_bytes)
140 return ::fprintf(fh, "%s", GetError());
141 }
142 return 0;
143}
144
146 LLDB_INSTRUMENT_VA(this, file_sp);
147 if (!file_sp)
148 return 0;
149 return file_sp->Printf("%s", GetError());
150}
151
153 LLDB_INSTRUMENT_VA(this, file);
154 if (!file.m_opaque_sp)
155 return 0;
156 return file.m_opaque_sp->Printf("%s", GetError());
157}
158
160 LLDB_INSTRUMENT_VA(this);
161
162 ref().Clear();
163}
164
166 LLDB_INSTRUMENT_VA(this);
167
168 return ref().GetStatus();
169}
170
172 LLDB_INSTRUMENT_VA(this, status);
173
174 ref().SetStatus(status);
175}
176
178 LLDB_INSTRUMENT_VA(this);
179
180 return ref().Succeeded();
181}
182
184 LLDB_INSTRUMENT_VA(this);
185
186 return ref().HasResult();
187}
188
189void SBCommandReturnObject::AppendMessage(const char *message) {
190 LLDB_INSTRUMENT_VA(this, message);
191
192 ref().AppendMessage(message);
193}
194
195void SBCommandReturnObject::AppendWarning(const char *message) {
196 LLDB_INSTRUMENT_VA(this, message);
197
198 ref().AppendWarning(message);
199}
200
202 return &**m_opaque_up;
203}
204
206 return &**m_opaque_up;
207}
208
210 return **m_opaque_up;
211}
212
214 return **m_opaque_up;
215}
216
218 LLDB_INSTRUMENT_VA(this, description);
219
220 Stream &strm = description.ref();
221
222 description.Printf("Error: ");
223 lldb::ReturnStatus status = ref().GetStatus();
224 if (status == lldb::eReturnStatusStarted)
225 strm.PutCString("Started");
226 else if (status == lldb::eReturnStatusInvalid)
227 strm.PutCString("Invalid");
228 else if (ref().Succeeded())
229 strm.PutCString("Success");
230 else
231 strm.PutCString("Fail");
232
233 if (GetOutputSize() > 0)
234 strm.Printf("\nOutput Message:\n%s", GetOutput());
235
236 if (GetErrorSize() > 0)
237 strm.Printf("\nError Message:\n%s", GetError());
238
239 return true;
240}
241
243 LLDB_INSTRUMENT_VA(this, fh);
244
245 SetImmediateOutputFile(fh, false);
246}
247
249 LLDB_INSTRUMENT_VA(this, fh);
250
251 SetImmediateErrorFile(fh, false);
252}
253
255 bool transfer_ownership) {
257 FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
259}
260
262 bool transfer_ownership) {
264 FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
266}
267
269 LLDB_INSTRUMENT_VA(this, file);
271}
272
274 LLDB_INSTRUMENT_VA(this, file);
276}
277
279 LLDB_INSTRUMENT_VA(this, file_sp);
281}
282
284 LLDB_INSTRUMENT_VA(this, file_sp);
286}
287
288void SBCommandReturnObject::PutCString(const char *string, int len) {
289 LLDB_INSTRUMENT_VA(this, string, len);
290
291 if (len == 0 || string == nullptr || *string == 0) {
292 return;
293 } else if (len > 0) {
294 std::string buffer(string, len);
295 ref().AppendMessage(buffer.c_str());
296 } else
297 ref().AppendMessage(string);
298}
299
300const char *SBCommandReturnObject::GetOutput(bool only_if_no_immediate) {
301 LLDB_INSTRUMENT_VA(this, only_if_no_immediate);
302
303 if (!only_if_no_immediate ||
304 ref().GetImmediateOutputStream().get() == nullptr)
305 return GetOutput();
306 return nullptr;
307}
308
309const char *SBCommandReturnObject::GetError(bool only_if_no_immediate) {
310 LLDB_INSTRUMENT_VA(this, only_if_no_immediate);
311
312 if (!only_if_no_immediate || ref().GetImmediateErrorStream().get() == nullptr)
313 return GetError();
314 return nullptr;
315}
316
317size_t SBCommandReturnObject::Printf(const char *format, ...) {
318 va_list args;
319 va_start(args, format);
320 size_t result = ref().GetOutputStream().PrintfVarArg(format, args);
321 va_end(args);
322 return result;
323}
324
326 const char *fallback_error_cstr) {
327 LLDB_INSTRUMENT_VA(this, error, fallback_error_cstr);
328
329 if (error.IsValid())
330 ref().SetError(error.ref(), fallback_error_cstr);
331 else if (fallback_error_cstr)
332 ref().SetError(Status(), fallback_error_cstr);
333}
334
335void SBCommandReturnObject::SetError(const char *error_cstr) {
336 LLDB_INSTRUMENT_VA(this, error_cstr);
337
338 if (error_cstr)
339 ref().AppendError(error_cstr);
340}
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
void AppendMessage(llvm::StringRef in_string)
void SetImmediateErrorFile(lldb::FileSP file_sp)
void void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
lldb::ReturnStatus GetStatus() const
void SetImmediateOutputFile(lldb::FileSP file_sp)
void void AppendWarning(llvm::StringRef in_string)
void SetError(const Status &error, const char *fallback_error_cstr=nullptr)
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)
An error handling class.
Definition: Status.h:44
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
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:350