LLDB  mainline
SBError.cpp
Go to the documentation of this file.
1 //===-- SBError.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 
9 #include "lldb/API/SBError.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Utility/Status.h"
14 
15 #include <cstdarg>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
21 
22 SBError::SBError(const SBError &rhs) : m_opaque_up() {
24 
26 }
27 
28 SBError::~SBError() = default;
29 
30 const SBError &SBError::operator=(const SBError &rhs) {
32  SBError, operator=,(const lldb::SBError &), rhs);
33 
34  if (this != &rhs)
36  return LLDB_RECORD_RESULT(*this);
37 }
38 
39 const char *SBError::GetCString() const {
41 
42  if (m_opaque_up)
43  return m_opaque_up->AsCString();
44  return nullptr;
45 }
46 
49 
50  if (m_opaque_up)
51  m_opaque_up->Clear();
52 }
53 
54 bool SBError::Fail() const {
56 
57  bool ret_value = false;
58  if (m_opaque_up)
59  ret_value = m_opaque_up->Fail();
60 
61 
62  return ret_value;
63 }
64 
65 bool SBError::Success() const {
67 
68  bool ret_value = true;
69  if (m_opaque_up)
70  ret_value = m_opaque_up->Success();
71 
72  return ret_value;
73 }
74 
77 
78 
79  uint32_t err = 0;
80  if (m_opaque_up)
81  err = m_opaque_up->GetError();
82 
83 
84  return err;
85 }
86 
89 
90  ErrorType err_type = eErrorTypeInvalid;
91  if (m_opaque_up)
92  err_type = m_opaque_up->GetType();
93 
94  return err_type;
95 }
96 
99  type);
100 
101  CreateIfNeeded();
102  m_opaque_up->SetError(err, type);
103 }
104 
105 void SBError::SetError(const Status &lldb_error) {
106  CreateIfNeeded();
107  *m_opaque_up = lldb_error;
108 }
109 
112 
113  CreateIfNeeded();
114  m_opaque_up->SetErrorToErrno();
115 }
116 
119 
120  CreateIfNeeded();
121  m_opaque_up->SetErrorToGenericError();
122 }
123 
124 void SBError::SetErrorString(const char *err_str) {
125  LLDB_RECORD_METHOD(void, SBError, SetErrorString, (const char *), err_str);
126 
127  CreateIfNeeded();
128  m_opaque_up->SetErrorString(err_str);
129 }
130 
131 int SBError::SetErrorStringWithFormat(const char *format, ...) {
132  CreateIfNeeded();
133  va_list args;
134  va_start(args, format);
135  int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);
136  va_end(args);
137  return num_chars;
138 }
139 
140 bool SBError::IsValid() const {
142  return this->operator bool();
143 }
144 SBError::operator bool() const {
145  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError, operator bool);
146 
147  return m_opaque_up != nullptr;
148 }
149 
151  if (m_opaque_up == nullptr)
152  m_opaque_up = std::make_unique<Status>();
153 }
154 
156 
158 
160  CreateIfNeeded();
161  return *m_opaque_up;
162 }
163 
165  // Be sure to call "IsValid()" before calling this function or it will crash
166  return *m_opaque_up;
167 }
168 
169 bool SBError::GetDescription(SBStream &description) {
171  description);
172 
173  if (m_opaque_up) {
174  if (m_opaque_up->Success())
175  description.Printf("success");
176  else {
177  const char *err_string = GetCString();
178  description.Printf("error: %s",
179  (err_string != nullptr ? err_string : ""));
180  }
181  } else
182  description.Printf("error: <NULL>");
183 
184  return true;
185 }
186 
187 namespace lldb_private {
188 namespace repro {
189 
190 template <>
195  SBError, operator=,(const lldb::SBError &));
196  LLDB_REGISTER_METHOD_CONST(const char *, SBError, GetCString, ());
197  LLDB_REGISTER_METHOD(void, SBError, Clear, ());
198  LLDB_REGISTER_METHOD_CONST(bool, SBError, Fail, ());
203  LLDB_REGISTER_METHOD(void, SBError, SetErrorToErrno, ());
204  LLDB_REGISTER_METHOD(void, SBError, SetErrorToGenericError, ());
205  LLDB_REGISTER_METHOD(void, SBError, SetErrorString, (const char *));
206  LLDB_REGISTER_METHOD_CONST(bool, SBError, IsValid, ());
207  LLDB_REGISTER_METHOD_CONST(bool, SBError, operator bool, ());
208  LLDB_REGISTER_METHOD(bool, SBError, GetDescription, (lldb::SBStream &));
209 }
210 
211 }
212 }
lldb::SBStream::Printf
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:69
lldb::SBError::get
lldb_private::Status * get()
Definition: SBError.cpp:157
lldb::SBError::GetCString
const char * GetCString() const
Definition: SBError.cpp:39
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
lldb::SBError
Definition: SBError.h:20
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
lldb::SBError::SetErrorToErrno
void SetErrorToErrno()
Definition: SBError.cpp:110
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
lldb::SBError::operator*
const lldb_private::Status & operator*() const
Definition: SBError.cpp:164
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
SBReproducerPrivate.h
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb::SBError::SetErrorToGenericError
void SetErrorToGenericError()
Definition: SBError.cpp:117
lldb::SBError::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBError.cpp:169
R
#define R(base)
lldb::SBError::Success
bool Success() const
Definition: SBError.cpp:65
SBStream.h
lldb::SBError::Fail
bool Fail() const
Definition: SBError.cpp:54
lldb::SBError::Clear
void Clear()
Definition: SBError.cpp:47
lldb::SBError::SetErrorStringWithFormat
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: SBError.cpp:131
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb_private::clone
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
Utils.h
lldb::ErrorType
ErrorType
Definition: lldb-enumerations.h:306
lldb::SBError::SBError
SBError()
Definition: SBError.cpp:20
lldb::SBError::SetErrorString
void SetErrorString(const char *err_str)
Definition: SBError.cpp:124
lldb_private::repro::RegisterMethods< SBError >
void RegisterMethods< SBError >(Registry &R)
Definition: SBError.cpp:191
lldb::SBError::GetError
uint32_t GetError() const
Definition: SBError.cpp:75
lldb::SBError::CreateIfNeeded
void CreateIfNeeded()
Definition: SBError.cpp:150
lldb::SBError::SetError
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:97
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
lldb::SBError::IsValid
bool IsValid() const
Definition: SBError.cpp:140
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb::SBError::operator=
const SBError & operator=(const lldb::SBError &rhs)
Definition: SBError.cpp:30
lldb::eErrorTypeInvalid
@ eErrorTypeInvalid
Definition: lldb-enumerations.h:307
lldb::SBError::~SBError
~SBError()
SetError
static void SetError(CommandReturnObject &result, Error err)
Definition: CommandObjectReproducer.cpp:140
Status.h
lldb::SBError::ref
lldb_private::Status & ref()
Definition: SBError.cpp:159
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::LineStatus::Success
@ Success
lldb::SBError::operator->
lldb_private::Status * operator->()
Definition: SBError.cpp:155
lldb::SBStream
Definition: SBStream.h:18
lldb::SBError::m_opaque_up
std::unique_ptr< lldb_private::Status > m_opaque_up
Definition: SBError.h:92
lldb::SBError::GetType
lldb::ErrorType GetType() const
Definition: SBError.cpp:87
lldb
Definition: SBAddress.h:15
SBError.h
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93