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 
19 using namespace lldb;
20 using namespace lldb_private;
21 
23 public:
26  : m_ptr(&ref), m_owned(false) {}
28  : m_ptr(new CommandReturnObject(*rhs.m_ptr)), m_owned(rhs.m_owned) {}
30  SBCommandReturnObjectImpl copy(rhs);
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 
42 private:
44  bool m_owned = true;
45 };
46 
48  : m_opaque_up(new SBCommandReturnObjectImpl()) {
49  LLDB_INSTRUMENT_VA(this);
50 }
51 
53  : m_opaque_up(new SBCommandReturnObjectImpl(ref)) {
54  LLDB_INSTRUMENT_VA(this, 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 
75  LLDB_INSTRUMENT_VA(this);
76  return this->operator bool();
77 }
78 SBCommandReturnObject::operator bool() const {
79  LLDB_INSTRUMENT_VA(this);
80 
81  // This method is not useful but it needs to stay to keep SB API stable.
82  return true;
83 }
84 
86  LLDB_INSTRUMENT_VA(this);
87 
88  ConstString output(ref().GetOutputData());
89  return output.AsCString(/*value_if_empty*/ "");
90 }
91 
93  LLDB_INSTRUMENT_VA(this);
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 
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 
121 size_t SBCommandReturnObject::PutOutput(FileSP file_sp) {
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 
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 
145 size_t SBCommandReturnObject::PutError(FileSP file_sp) {
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 
191 
193 }
194 
197 
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) {
256  LLDB_INSTRUMENT_VA(this, fh, transfer_ownership);
257  FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
258  ref().SetImmediateOutputFile(file);
259 }
260 
262  bool transfer_ownership) {
263  LLDB_INSTRUMENT_VA(this, fh, transfer_ownership);
264  FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
265  ref().SetImmediateErrorFile(file);
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);
280  SetImmediateOutputFile(SBFile(file_sp));
281 }
282 
284  LLDB_INSTRUMENT_VA(this, file_sp);
285  SetImmediateErrorFile(SBFile(file_sp));
286 }
287 
288 void 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 
300 const 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 
309 const 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 
317 size_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 
335 void SBCommandReturnObject::SetError(const char *error_cstr) {
336  LLDB_INSTRUMENT_VA(this, error_cstr);
337 
338  if (error_cstr)
339  ref().AppendError(error_cstr);
340 }
lldb::SBCommandReturnObject::ref
lldb_private::CommandReturnObject & ref() const
Definition: SBCommandReturnObject.cpp:213
Instrumentation.h
lldb_private::CommandReturnObject::GetStatus
lldb::ReturnStatus GetStatus() const
Definition: CommandReturnObject.cpp:129
lldb_private::CommandReturnObject::HasResult
bool HasResult() const
Definition: CommandReturnObject.cpp:135
lldb_private::SBCommandReturnObjectImpl::m_ptr
CommandReturnObject * m_ptr
Definition: SBCommandReturnObject.cpp:43
lldb::SBCommandReturnObject::PutOutput
size_t PutOutput(FILE *fh)
Definition: SBCommandReturnObject.cpp:111
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
lldb::SBFile::m_opaque_sp
FileSP m_opaque_sp
Definition: SBFile.h:45
lldb::SBCommandReturnObject::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Definition: SBCommandReturnObject.cpp:317
lldb::SBStream::Printf
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:70
lldb_private::CommandReturnObject::SetError
void SetError(const Status &error, const char *fallback_error_cstr=nullptr)
Definition: CommandReturnObject.cpp:107
lldb::SBCommandReturnObject::operator->
lldb_private::CommandReturnObject * operator->() const
Definition: SBCommandReturnObject.cpp:201
lldb::SBCommandReturnObject::m_opaque_up
std::unique_ptr< lldb_private::SBCommandReturnObjectImpl > m_opaque_up
Definition: SBCommandReturnObject.h:121
lldb::SBCommandReturnObject
Definition: SBCommandReturnObject.h:24
lldb::SBCommandReturnObject::AppendWarning
void AppendWarning(const char *message)
Definition: SBCommandReturnObject.cpp:195
lldb::SBError
Definition: SBError.h:20
lldb::SBCommandReturnObject::SBCommandReturnObject
SBCommandReturnObject()
Definition: SBCommandReturnObject.cpp:47
lldb::SBCommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: SBCommandReturnObject.cpp:171
lldb::SBCommandReturnObject::SetError
void SetError(lldb::SBError &error, const char *fallback_error_cstr=nullptr)
Definition: SBCommandReturnObject.cpp:325
lldb_private::swap
void swap(AdaptedConstIterator< C, E, A > &lhs, AdaptedConstIterator< C, E, A > &rhs)
Definition: Iterable.h:147
lldb_private::SBCommandReturnObjectImpl
Definition: SBCommandReturnObject.cpp:22
lldb_private::CommandReturnObject::GetOutputData
llvm::StringRef GetOutputData()
Definition: CommandReturnObject.h:32
lldb::SBCommandReturnObject::get
lldb_private::CommandReturnObject * get() const
Definition: SBCommandReturnObject.cpp:205
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:192
lldb_private::Stream
Definition: Stream.h:28
lldb_private::CommandReturnObject::Succeeded
bool Succeeded() const
Definition: CommandReturnObject.cpp:131
lldb::eReturnStatusInvalid
@ eReturnStatusInvalid
Definition: lldb-enumerations.h:260
lldb_private::SBCommandReturnObjectImpl::SBCommandReturnObjectImpl
SBCommandReturnObjectImpl()
Definition: SBCommandReturnObject.cpp:24
lldb::SBCommandReturnObject::GetError
const char * GetError()
Definition: SBCommandReturnObject.cpp:92
CommandReturnObject.h
lldb_private::CommandReturnObject::Clear
void Clear()
Definition: CommandReturnObject.cpp:140
lldb::SBCommandReturnObject::operator=
lldb::SBCommandReturnObject & operator=(const lldb::SBCommandReturnObject &rhs)
Definition: SBCommandReturnObject.cpp:64
lldb::SBCommandReturnObject::GetErrorSize
size_t GetErrorSize()
Definition: SBCommandReturnObject.cpp:105
lldb::SBCommandReturnObject::operator*
lldb_private::CommandReturnObject & operator*() const
Definition: SBCommandReturnObject.cpp:209
lldb::SBCommandReturnObject::SetImmediateOutputFile
void SetImmediateOutputFile(FILE *fh)
Definition: SBCommandReturnObject.cpp:242
lldb::SBCommandReturnObject::PutCString
void PutCString(const char *string, int len=-1)
Definition: SBCommandReturnObject.cpp:288
lldb_private::SBCommandReturnObjectImpl::operator*
CommandReturnObject & operator*() const
Definition: SBCommandReturnObject.cpp:40
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
SBStream.h
lldb::SBCommandReturnObject::SetImmediateErrorFile
void SetImmediateErrorFile(FILE *fh)
Definition: SBCommandReturnObject.cpp:248
lldb::SBFile
Definition: SBFile.h:16
lldb::SBCommandReturnObject::AppendMessage
void AppendMessage(const char *message)
Definition: SBCommandReturnObject.cpp:189
lldb_private::CommandReturnObject::SetImmediateOutputFile
void SetImmediateOutputFile(lldb::FileSP file_sp)
Definition: CommandReturnObject.h:66
lldb::ReturnStatus
ReturnStatus
Command Return Status Types.
Definition: lldb-enumerations.h:259
lldb_private::clone
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
Utils.h
lldb::SBCommandReturnObject::PutError
size_t PutError(FILE *fh)
Definition: SBCommandReturnObject.cpp:135
lldb_private::CommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: CommandReturnObject.cpp:127
lldb::eReturnStatusStarted
@ eReturnStatusStarted
Definition: lldb-enumerations.h:265
lldb::SBCommandReturnObject::GetStatus
lldb::ReturnStatus GetStatus()
Definition: SBCommandReturnObject.cpp:165
lldb_private::CommandReturnObject::GetOutputStream
Stream & GetOutputStream()
Definition: CommandReturnObject.h:46
lldb_private::ConstString
Definition: ConstString.h:39
lldb::SBFile
class LLDB_API SBFile
Definition: SBDefines.h:57
lldb_private::Stream::PrintfVarArg
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition: Stream.cpp:116
lldb::SBCommandReturnObject::IsValid
bool IsValid() const
Definition: SBCommandReturnObject.cpp:74
lldb::SBCommandReturnObject::Clear
void Clear()
Definition: SBCommandReturnObject.cpp:159
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::SBCommandReturnObjectImpl::operator=
SBCommandReturnObjectImpl & operator=(const SBCommandReturnObjectImpl &rhs)
Definition: SBCommandReturnObject.cpp:29
lldb_private::SBCommandReturnObjectImpl::SBCommandReturnObjectImpl
SBCommandReturnObjectImpl(CommandReturnObject &ref)
Definition: SBCommandReturnObject.cpp:25
SBFile.h
lldb_private::SBCommandReturnObjectImpl::SBCommandReturnObjectImpl
SBCommandReturnObjectImpl(const SBCommandReturnObjectImpl &rhs)
Definition: SBCommandReturnObject.cpp:27
lldb_private::Status
Definition: Status.h:44
lldb::SBCommandReturnObject::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBCommandReturnObject.cpp:217
lldb_private::CommandReturnObject
Definition: CommandReturnObject.h:26
message
message(FATAL_ERROR "invalid libipt include path provided") endif() include_directories($
Definition: Plugins/Trace/intel-pt/CMakeLists.txt:6
lldb_private::SBCommandReturnObjectImpl::~SBCommandReturnObjectImpl
~SBCommandReturnObjectImpl()
Definition: SBCommandReturnObject.cpp:35
lldb_private::CommandReturnObject::GetErrorData
llvm::StringRef GetErrorData()
Definition: CommandReturnObject.h:39
lldb::SBCommandReturnObject::Succeeded
bool Succeeded()
Definition: SBCommandReturnObject.cpp:177
lldb_private::CommandReturnObject::AppendWarning
void void AppendWarning(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:94
lldb_private::CommandReturnObject::SetImmediateErrorFile
void SetImmediateErrorFile(lldb::FileSP file_sp)
Definition: CommandReturnObject.h:73
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
SBCommandReturnObject.h
lldb::SBCommandReturnObject::GetOutput
const char * GetOutput()
Definition: SBCommandReturnObject.cpp:85
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
ConstString.h
lldb::SBCommandReturnObject::GetOutputSize
size_t GetOutputSize()
Definition: SBCommandReturnObject.cpp:99
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb::SBStream
Definition: SBStream.h:18
lldb_private::CommandReturnObject::AppendError
void void AppendError(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:100
lldb
Definition: SBAddress.h:15
lldb_private::CommandReturnObject::AppendMessage
void AppendMessage(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:88
lldb::SBCommandReturnObject::~SBCommandReturnObject
~SBCommandReturnObject()
SBError.h
lldb::SBCommandReturnObject::HasResult
bool HasResult()
Definition: SBCommandReturnObject.cpp:183