LLDB  mainline
SBError.cpp
Go to the documentation of this file.
1 //===-- SBError.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 
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 <stdarg.h>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
21 
22 SBError::SBError(const SBError &rhs) : m_opaque_up() {
24 
25  m_opaque_up = clone(rhs.m_opaque_up);
26 }
27 
29 
30 const SBError &SBError::operator=(const SBError &rhs) {
32  SBError, operator=,(const lldb::SBError &), rhs);
33 
34  if (this != &rhs)
35  m_opaque_up = clone(rhs.m_opaque_up);
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 NULL;
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->SetErrorToErrno();
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 != NULL;
148 }
149 
150 void SBError::CreateIfNeeded() {
151  if (m_opaque_up == NULL)
152  m_opaque_up.reset(new Status());
153 }
154 
155 lldb_private::Status *SBError::operator->() { return m_opaque_up.get(); }
156 
157 lldb_private::Status *SBError::get() { return m_opaque_up.get(); }
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", (err_string != NULL ? err_string : ""));
179  }
180  } else
181  description.Printf("error: <NULL>");
182 
183  return true;
184 }
185 
186 namespace lldb_private {
187 namespace repro {
188 
189 template <>
194  SBError, operator=,(const lldb::SBError &));
195  LLDB_REGISTER_METHOD_CONST(const char *, SBError, GetCString, ());
196  LLDB_REGISTER_METHOD(void, SBError, Clear, ());
204  LLDB_REGISTER_METHOD(void, SBError, SetErrorString, (const char *));
206  LLDB_REGISTER_METHOD_CONST(bool, SBError, operator bool, ());
208 }
209 
210 }
211 }
lldb_private::Status * operator->()
Definition: SBError.cpp:155
The registry contains a unique mapping between functions and their ID.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
uint32_t GetError() const
Definition: SBError.cpp:75
void SetErrorToGenericError()
Definition: SBError.cpp:117
bool Success() const
Definition: SBError.cpp:65
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: SBError.cpp:131
void Clear()
Definition: SBError.cpp:47
bool Fail() const
Definition: SBError.cpp:54
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:97
lldb_private::Status * get()
Definition: SBError.cpp:157
void SetErrorString(const char *err_str)
Definition: SBError.cpp:124
bool IsValid() const
Definition: SBError.cpp:140
const SBError & operator=(const lldb::SBError &rhs)
Definition: SBError.cpp:30
#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
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
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
bool GetDescription(lldb::SBStream &description)
Definition: SBError.cpp:169
const lldb_private::Status & operator*() const
Definition: SBError.cpp:164
const char * GetCString() const
Definition: SBError.cpp:39
Definition: SBAddress.h:15
lldb::ErrorType GetType() const
Definition: SBError.cpp:87
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
void RegisterMethods< SBError >(Registry &R)
Definition: SBError.cpp:190
void SetErrorToErrno()
Definition: SBError.cpp:110
#define LLDB_RECORD_RESULT(Result)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
An error handling class.
Definition: Status.h:44