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 "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBError.h"
13 #include "lldb/API/SBFile.h"
14 #include "lldb/API/SBStream.h"
17 #include "lldb/Utility/Status.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
23 public:
25  : m_ptr(new CommandReturnObject()), m_owned(true) {}
27  : m_ptr(&ref), m_owned(false) {}
29  : m_ptr(new CommandReturnObject(*rhs.m_ptr)), m_owned(rhs.m_owned) {}
31  SBCommandReturnObjectImpl copy(rhs);
32  std::swap(*this, copy);
33  return *this;
34  }
35  // rvalue ctor+assignment are not used by SBCommandReturnObject.
37  if (m_owned)
38  delete m_ptr;
39  }
40 
41  CommandReturnObject &operator*() const { return *m_ptr; }
42 
43 private:
45  bool m_owned;
46 };
47 
49  : m_opaque_up(new SBCommandReturnObjectImpl()) {
51 }
52 
57 }
58 
60  : m_opaque_up() {
62  (const lldb::SBCommandReturnObject &), rhs);
63 
65 }
66 
72  rhs);
73 
74  if (this != &rhs)
76  return LLDB_RECORD_RESULT(*this);
77 }
78 
80 
83  return this->operator bool();
84 }
85 SBCommandReturnObject::operator bool() const {
87 
88  // This method is not useful but it needs to stay to keep SB API stable.
89  return true;
90 }
91 
94 
95  ConstString output(ref().GetOutputData());
96  return output.AsCString(/*value_if_empty*/ "");
97 }
98 
101 
102  ConstString output(ref().GetErrorData());
103  return output.AsCString(/*value_if_empty*/ "");
104 }
105 
108 
109  return ref().GetOutputData().size();
110 }
111 
114 
115  return ref().GetErrorData().size();
116 }
117 
119  LLDB_RECORD_DUMMY(size_t, SBCommandReturnObject, PutOutput, (FILE *), fh);
120  if (fh) {
121  size_t num_bytes = GetOutputSize();
122  if (num_bytes)
123  return ::fprintf(fh, "%s", GetOutput());
124  }
125  return 0;
126 }
127 
128 size_t SBCommandReturnObject::PutOutput(FileSP file_sp) {
130  file_sp);
131  if (!file_sp)
132  return 0;
133  return file_sp->Printf("%s", GetOutput());
134 }
135 
138  if (!file.m_opaque_sp)
139  return 0;
140  return file.m_opaque_sp->Printf("%s", GetOutput());
141 }
142 
144  LLDB_RECORD_DUMMY(size_t, SBCommandReturnObject, PutError, (FILE *), fh);
145  if (fh) {
146  size_t num_bytes = GetErrorSize();
147  if (num_bytes)
148  return ::fprintf(fh, "%s", GetError());
149  }
150  return 0;
151 }
152 
153 size_t SBCommandReturnObject::PutError(FileSP file_sp) {
155  file_sp);
156  if (!file_sp)
157  return 0;
158  return file_sp->Printf("%s", GetError());
159 }
160 
163  if (!file.m_opaque_sp)
164  return 0;
165  return file.m_opaque_sp->Printf("%s", GetError());
166 }
167 
170 
171  ref().Clear();
172 }
173 
176  GetStatus);
177 
178  return ref().GetStatus();
179 }
180 
183  (lldb::ReturnStatus), status);
184 
185  ref().SetStatus(status);
186 }
187 
190 
191  return ref().Succeeded();
192 }
193 
196 
197  return ref().HasResult();
198 }
199 
200 void SBCommandReturnObject::AppendMessage(const char *message) {
202  message);
203 
204  ref().AppendMessage(message);
205 }
206 
207 void SBCommandReturnObject::AppendWarning(const char *message) {
209  message);
210 
211  ref().AppendWarning(message);
212 }
213 
215  return &**m_opaque_up;
216 }
217 
219  return &**m_opaque_up;
220 }
221 
223  return **m_opaque_up;
224 }
225 
227  return **m_opaque_up;
228 }
229 
232  (lldb::SBStream &), description);
233 
234  Stream &strm = description.ref();
235 
236  description.Printf("Error: ");
237  lldb::ReturnStatus status = ref().GetStatus();
238  if (status == lldb::eReturnStatusStarted)
239  strm.PutCString("Started");
240  else if (status == lldb::eReturnStatusInvalid)
241  strm.PutCString("Invalid");
242  else if (ref().Succeeded())
243  strm.PutCString("Success");
244  else
245  strm.PutCString("Fail");
246 
247  if (GetOutputSize() > 0)
248  strm.Printf("\nOutput Message:\n%s", GetOutput());
249 
250  if (GetErrorSize() > 0)
251  strm.Printf("\nError Message:\n%s", GetError());
252 
253  return true;
254 }
255 
258  (FILE *), fh);
259 
260  SetImmediateOutputFile(fh, false);
261 }
262 
265  (FILE *), fh);
266 
267  SetImmediateErrorFile(fh, false);
268 }
269 
271  bool transfer_ownership) {
273  (FILE *, bool), fh, transfer_ownership);
274  FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
275  ref().SetImmediateOutputFile(file);
276 }
277 
279  bool transfer_ownership) {
281  (FILE *, bool), fh, transfer_ownership);
282  FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
283  ref().SetImmediateErrorFile(file);
284 }
285 
288  (SBFile), file);
290 }
291 
294  (SBFile), file);
296 }
297 
300  (FileSP), file_sp);
301  SetImmediateOutputFile(SBFile(file_sp));
302 }
303 
306  (FileSP), file_sp);
307  SetImmediateErrorFile(SBFile(file_sp));
308 }
309 
310 void SBCommandReturnObject::PutCString(const char *string, int len) {
312  (const char *, int), string, len);
313 
314  if (len == 0 || string == nullptr || *string == 0) {
315  return;
316  } else if (len > 0) {
317  std::string buffer(string, len);
318  ref().AppendMessage(buffer.c_str());
319  } else
320  ref().AppendMessage(string);
321 }
322 
323 const char *SBCommandReturnObject::GetOutput(bool only_if_no_immediate) {
324  LLDB_RECORD_METHOD(const char *, SBCommandReturnObject, GetOutput, (bool),
325  only_if_no_immediate);
326 
327  if (!only_if_no_immediate ||
328  ref().GetImmediateOutputStream().get() == nullptr)
329  return GetOutput();
330  return nullptr;
331 }
332 
333 const char *SBCommandReturnObject::GetError(bool only_if_no_immediate) {
334  LLDB_RECORD_METHOD(const char *, SBCommandReturnObject, GetError, (bool),
335  only_if_no_immediate);
336 
337  if (!only_if_no_immediate || ref().GetImmediateErrorStream().get() == nullptr)
338  return GetError();
339  return nullptr;
340 }
341 
342 size_t SBCommandReturnObject::Printf(const char *format, ...) {
343  va_list args;
344  va_start(args, format);
345  size_t result = ref().GetOutputStream().PrintfVarArg(format, args);
346  va_end(args);
347  return result;
348 }
349 
351  const char *fallback_error_cstr) {
353  (lldb::SBError &, const char *), error,
354  fallback_error_cstr);
355 
356  if (error.IsValid())
357  ref().SetError(error.ref(), fallback_error_cstr);
358  else if (fallback_error_cstr)
359  ref().SetError(Status(), fallback_error_cstr);
360 }
361 
362 void SBCommandReturnObject::SetError(const char *error_cstr) {
363  LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetError, (const char *),
364  error_cstr);
365 
366  if (error_cstr)
367  ref().SetError(error_cstr);
368 }
369 
370 namespace lldb_private {
371 namespace repro {
372 
373 template <>
379  (const lldb::SBCommandReturnObject &));
384  LLDB_REGISTER_METHOD_CONST(bool, SBCommandReturnObject, operator bool, ());
397  ());
403  (const char *));
405  (const char *));
407  (lldb::SBStream &));
409  (FILE *));
411  (FILE *));
413  (SBFile));
415  (SBFile));
417  (FileSP));
419  (FileSP));
421  (FILE *, bool));
423  (FILE *, bool));
425  (const char *, int));
427  (bool));
428  LLDB_REGISTER_METHOD(const char *, SBCommandReturnObject, GetError, (bool));
430  (lldb::SBError &, const char *));
431  LLDB_REGISTER_METHOD(void, SBCommandReturnObject, SetError, (const char *));
432 }
433 
434 }
435 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:62
The registry contains a unique mapping between functions and their ID.
A class that represents a running process on the host machine.
void RegisterMethods< SBCommandReturnObject >(Registry &R)
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:223
SBCommandReturnObjectImpl & operator=(const SBCommandReturnObjectImpl &rhs)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
lldb_private::CommandReturnObject * operator->() const
void SetImmediateErrorFile(lldb::FileSP file_sp)
lldb_private::CommandReturnObject & operator*() const
#define LLDB_RECORD_DUMMY(Result, Class, Method, Signature,...)
The LLDB_RECORD_DUMMY macro is special because it doesn&#39;t actually record anything.
std::unique_ptr< lldb_private::SBCommandReturnObjectImpl > m_opaque_up
SBCommandReturnObjectImpl(const SBCommandReturnObjectImpl &rhs)
void swap(AdaptedConstIterator< C, E, A > &lhs, AdaptedConstIterator< C, E, A > &rhs)
Definition: Iterable.h:147
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition: Stream.cpp:115
void SetImmediateOutputFile(lldb::FileSP file_sp)
lldb::SBCommandReturnObject & operator=(const lldb::SBCommandReturnObject &rhs)
bool IsValid() const
Definition: SBError.cpp:140
void SetError(lldb::SBError &error, const char *fallback_error_cstr=nullptr)
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
class LLDB_API SBFile
Definition: SBDefines.h:45
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
void AppendWarning(llvm::StringRef in_string)
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
bool GetDescription(lldb::SBStream &description)
lldb_private::Status & ref()
Definition: SBError.cpp:159
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:69
void PutCString(const char *string, int len=-1)
void AppendMessage(llvm::StringRef in_string)
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
void SetError(const Status &error, const char *fallback_error_cstr=nullptr)
void SetStatus(lldb::ReturnStatus status)
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
size_t Printf(const char *format,...) __attribute__((format(printf
lldb_private::Stream & ref()
Definition: SBStream.cpp:178
void AppendMessage(const char *message)
lldb_private::CommandReturnObject & ref() const
FileSP m_opaque_sp
Definition: SBFile.h:45
A uniqued constant string class.
Definition: ConstString.h:40
Definition: SBAddress.h:15
ReturnStatus
Command Return Status Types.
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
lldb_private::CommandReturnObject * get() const
SBCommandReturnObjectImpl(CommandReturnObject &ref)
void AppendWarning(const char *message)
#define LLDB_RECORD_RESULT(Result)
void SetStatus(lldb::ReturnStatus status)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
An error handling class.
Definition: Status.h:44