LLDB  mainline
SBCommandReturnObject.cpp
Go to the documentation of this file.
1 //===-- SBCommandReturnObject.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 
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBError.h"
13 #include "lldb/API/SBStream.h"
16 #include "lldb/Utility/Status.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
22  : m_opaque_up(new CommandReturnObject()) {
24 }
25 
27  : m_opaque_up() {
29  (const lldb::SBCommandReturnObject &), rhs);
30 
31  m_opaque_up = clone(rhs.m_opaque_up);
32 }
33 
35  : m_opaque_up(ptr) {
38 }
39 
41 
45 
46  return LLDB_RECORD_RESULT(m_opaque_up.release());
47 }
48 
54  rhs);
55 
56  if (this != &rhs)
57  m_opaque_up = clone(rhs.m_opaque_up);
58  return LLDB_RECORD_RESULT(*this);
59 }
60 
63  return this->operator bool();
64 }
65 SBCommandReturnObject::operator bool() const {
67 
68  return m_opaque_up != nullptr;
69 }
70 
73 
74  if (m_opaque_up) {
75  llvm::StringRef output = m_opaque_up->GetOutputData();
76  ConstString result(output.empty() ? llvm::StringRef("") : output);
77 
78  return result.AsCString();
79  }
80 
81  return nullptr;
82 }
83 
86 
87  if (m_opaque_up) {
88  llvm::StringRef output = m_opaque_up->GetErrorData();
89  ConstString result(output.empty() ? llvm::StringRef("") : output);
90  return result.AsCString();
91  }
92 
93  return nullptr;
94 }
95 
98 
99  return (m_opaque_up ? m_opaque_up->GetOutputData().size() : 0);
100 }
101 
104 
105  return (m_opaque_up ? m_opaque_up->GetErrorData().size() : 0);
106 }
107 
109  LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutOutput, (FILE *), fh);
110 
111  if (fh) {
112  size_t num_bytes = GetOutputSize();
113  if (num_bytes)
114  return ::fprintf(fh, "%s", GetOutput());
115  }
116  return 0;
117 }
118 
120  LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutError, (FILE *), fh);
121 
122  if (fh) {
123  size_t num_bytes = GetErrorSize();
124  if (num_bytes)
125  return ::fprintf(fh, "%s", GetError());
126  }
127  return 0;
128 }
129 
132 
133  if (m_opaque_up)
134  m_opaque_up->Clear();
135 }
136 
139  GetStatus);
140 
141  return (m_opaque_up ? m_opaque_up->GetStatus() : lldb::eReturnStatusInvalid);
142 }
143 
146  (lldb::ReturnStatus), status);
147 
148  if (m_opaque_up)
149  m_opaque_up->SetStatus(status);
150 }
151 
154 
155  return (m_opaque_up ? m_opaque_up->Succeeded() : false);
156 }
157 
160 
161  return (m_opaque_up ? m_opaque_up->HasResult() : false);
162 }
163 
164 void SBCommandReturnObject::AppendMessage(const char *message) {
166  message);
167 
168  if (m_opaque_up)
169  m_opaque_up->AppendMessage(message);
170 }
171 
172 void SBCommandReturnObject::AppendWarning(const char *message) {
174  message);
175 
176  if (m_opaque_up)
177  m_opaque_up->AppendWarning(message);
178 }
179 
181  return m_opaque_up.get();
182 }
183 
185  return m_opaque_up.get();
186 }
187 
189  assert(m_opaque_up.get());
190  return *(m_opaque_up.get());
191 }
192 
194  assert(m_opaque_up.get());
195  return *(m_opaque_up.get());
196 }
197 
199  if (m_opaque_up)
200  m_opaque_up.reset(ptr);
201 }
202 
205  (lldb::SBStream &), description);
206 
207  Stream &strm = description.ref();
208 
209  if (m_opaque_up) {
210  description.Printf("Error: ");
211  lldb::ReturnStatus status = m_opaque_up->GetStatus();
212  if (status == lldb::eReturnStatusStarted)
213  strm.PutCString("Started");
214  else if (status == lldb::eReturnStatusInvalid)
215  strm.PutCString("Invalid");
216  else if (m_opaque_up->Succeeded())
217  strm.PutCString("Success");
218  else
219  strm.PutCString("Fail");
220 
221  if (GetOutputSize() > 0)
222  strm.Printf("\nOutput Message:\n%s", GetOutput());
223 
224  if (GetErrorSize() > 0)
225  strm.Printf("\nError Message:\n%s", GetError());
226  } else
227  strm.PutCString("No value");
228 
229  return true;
230 }
231 
234  (FILE *), fh);
235 
236  SetImmediateOutputFile(fh, false);
237 }
238 
241  (FILE *), fh);
242 
243  SetImmediateErrorFile(fh, false);
244 }
245 
247  bool transfer_ownership) {
249  (FILE *, bool), fh, transfer_ownership);
250 
251  if (m_opaque_up)
252  m_opaque_up->SetImmediateOutputFile(fh, transfer_ownership);
253 }
254 
256  bool transfer_ownership) {
258  (FILE *, bool), fh, transfer_ownership);
259 
260  if (m_opaque_up)
261  m_opaque_up->SetImmediateErrorFile(fh, transfer_ownership);
262 }
263 
264 void SBCommandReturnObject::PutCString(const char *string, int len) {
266  (const char *, int), string, len);
267 
268  if (m_opaque_up) {
269  if (len == 0 || string == nullptr || *string == 0) {
270  return;
271  } else if (len > 0) {
272  std::string buffer(string, len);
273  m_opaque_up->AppendMessage(buffer.c_str());
274  } else
275  m_opaque_up->AppendMessage(string);
276  }
277 }
278 
279 const char *SBCommandReturnObject::GetOutput(bool only_if_no_immediate) {
280  LLDB_RECORD_METHOD(const char *, SBCommandReturnObject, GetOutput, (bool),
281  only_if_no_immediate);
282 
283  if (!m_opaque_up)
284  return nullptr;
285  if (!only_if_no_immediate ||
286  m_opaque_up->GetImmediateOutputStream().get() == nullptr)
287  return GetOutput();
288  return nullptr;
289 }
290 
291 const char *SBCommandReturnObject::GetError(bool only_if_no_immediate) {
292  LLDB_RECORD_METHOD(const char *, SBCommandReturnObject, GetError, (bool),
293  only_if_no_immediate);
294 
295  if (!m_opaque_up)
296  return nullptr;
297  if (!only_if_no_immediate ||
298  m_opaque_up->GetImmediateErrorStream().get() == nullptr)
299  return GetError();
300  return nullptr;
301 }
302 
303 size_t SBCommandReturnObject::Printf(const char *format, ...) {
304  if (m_opaque_up) {
305  va_list args;
306  va_start(args, format);
307  size_t result = m_opaque_up->GetOutputStream().PrintfVarArg(format, args);
308  va_end(args);
309  return result;
310  }
311  return 0;
312 }
313 
315  const char *fallback_error_cstr) {
317  (lldb::SBError &, const char *), error,
318  fallback_error_cstr);
319 
320  if (m_opaque_up) {
321  if (error.IsValid())
322  m_opaque_up->SetError(error.ref(), fallback_error_cstr);
323  else if (fallback_error_cstr)
324  m_opaque_up->SetError(Status(), fallback_error_cstr);
325  }
326 }
327 
328 void SBCommandReturnObject::SetError(const char *error_cstr) {
329  LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetError, (const char *),
330  error_cstr);
331 
332  if (m_opaque_up && error_cstr)
333  m_opaque_up->SetError(error_cstr);
334 }
335 
336 namespace lldb_private {
337 namespace repro {
338 
339 template <>
343  (const lldb::SBCommandReturnObject &));
352  LLDB_REGISTER_METHOD_CONST(bool, SBCommandReturnObject, operator bool, ());
361  ());
367  (const char *));
369  (const char *));
371  (lldb::SBStream &));
373  (FILE *));
375  (FILE *));
377  (FILE *, bool));
379  (FILE *, bool));
381  (const char *, int));
383  (bool));
384  LLDB_REGISTER_METHOD(const char *, SBCommandReturnObject, GetError, (bool));
386  (lldb::SBError &, const char *));
387  LLDB_REGISTER_METHOD(void, SBCommandReturnObject, SetError, (const char *));
388 }
389 
390 }
391 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
The registry contains a unique mapping between functions and their ID.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
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:224
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
lldb_private::CommandReturnObject * operator->() const
lldb_private::CommandReturnObject * Release()
lldb_private::CommandReturnObject & operator*() const
bool IsValid() const
Definition: SBError.cpp:140
void SetError(lldb::SBError &error, const char *fallback_error_cstr=nullptr)
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
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:62
void PutCString(const char *string, int len=-1)
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
void SetStatus(lldb::ReturnStatus status)
void SetLLDBObjectPtr(lldb_private::CommandReturnObject *ptr)
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:162
void AppendMessage(const char *message)
lldb_private::CommandReturnObject & ref() const
A uniqued constant string class.
Definition: ConstString.h:38
Definition: SBAddress.h:15
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
const lldb::SBCommandReturnObject & operator=(const lldb::SBCommandReturnObject &rhs)
lldb_private::CommandReturnObject * get() const
void AppendWarning(const char *message)
#define LLDB_RECORD_RESULT(Result)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
An error handling class.
Definition: Status.h:44