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 "Utils.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Utility/Instrumentation.h"
13 #include "lldb/Utility/Status.h"
14 
15 #include <cstdarg>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 SBError::SBError() { LLDB_INSTRUMENT_VA(this); }
21 
22 SBError::SBError(const SBError &rhs) {
23   LLDB_INSTRUMENT_VA(this, rhs);
24 
25   m_opaque_up = clone(rhs.m_opaque_up);
26 }
27 
28 SBError::SBError(const char *message) {
29   LLDB_INSTRUMENT_VA(this, message);
30 
31   SetErrorString(message);
32 }
33 
34 SBError::SBError(const lldb_private::Status &status)
35     : m_opaque_up(new Status(status)) {
36   LLDB_INSTRUMENT_VA(this, status);
37 }
38 
39 SBError::~SBError() = default;
40 
41 const SBError &SBError::operator=(const SBError &rhs) {
42   LLDB_INSTRUMENT_VA(this, rhs);
43 
44   if (this != &rhs)
45     m_opaque_up = clone(rhs.m_opaque_up);
46   return *this;
47 }
48 
49 const char *SBError::GetCString() const {
50   LLDB_INSTRUMENT_VA(this);
51 
52   if (m_opaque_up)
53     return m_opaque_up->AsCString();
54   return nullptr;
55 }
56 
57 void SBError::Clear() {
58   LLDB_INSTRUMENT_VA(this);
59 
60   if (m_opaque_up)
61     m_opaque_up->Clear();
62 }
63 
64 bool SBError::Fail() const {
65   LLDB_INSTRUMENT_VA(this);
66 
67   bool ret_value = false;
68   if (m_opaque_up)
69     ret_value = m_opaque_up->Fail();
70 
71 
72   return ret_value;
73 }
74 
75 bool SBError::Success() const {
76   LLDB_INSTRUMENT_VA(this);
77 
78   bool ret_value = true;
79   if (m_opaque_up)
80     ret_value = m_opaque_up->Success();
81 
82   return ret_value;
83 }
84 
85 uint32_t SBError::GetError() const {
86   LLDB_INSTRUMENT_VA(this);
87 
88   uint32_t err = 0;
89   if (m_opaque_up)
90     err = m_opaque_up->GetError();
91 
92 
93   return err;
94 }
95 
96 ErrorType SBError::GetType() const {
97   LLDB_INSTRUMENT_VA(this);
98 
99   ErrorType err_type = eErrorTypeInvalid;
100   if (m_opaque_up)
101     err_type = m_opaque_up->GetType();
102 
103   return err_type;
104 }
105 
106 void SBError::SetError(uint32_t err, ErrorType type) {
107   LLDB_INSTRUMENT_VA(this, err, type);
108 
109   CreateIfNeeded();
110   m_opaque_up->SetError(err, type);
111 }
112 
113 void SBError::SetError(const Status &lldb_error) {
114   CreateIfNeeded();
115   *m_opaque_up = lldb_error;
116 }
117 
118 void SBError::SetErrorToErrno() {
119   LLDB_INSTRUMENT_VA(this);
120 
121   CreateIfNeeded();
122   m_opaque_up->SetErrorToErrno();
123 }
124 
125 void SBError::SetErrorToGenericError() {
126   LLDB_INSTRUMENT_VA(this);
127 
128   CreateIfNeeded();
129   m_opaque_up->SetErrorToGenericError();
130 }
131 
132 void SBError::SetErrorString(const char *err_str) {
133   LLDB_INSTRUMENT_VA(this, err_str);
134 
135   CreateIfNeeded();
136   m_opaque_up->SetErrorString(err_str);
137 }
138 
139 int SBError::SetErrorStringWithFormat(const char *format, ...) {
140   CreateIfNeeded();
141   va_list args;
142   va_start(args, format);
143   int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);
144   va_end(args);
145   return num_chars;
146 }
147 
148 bool SBError::IsValid() const {
149   LLDB_INSTRUMENT_VA(this);
150   return this->operator bool();
151 }
152 SBError::operator bool() const {
153   LLDB_INSTRUMENT_VA(this);
154 
155   return m_opaque_up != nullptr;
156 }
157 
158 void SBError::CreateIfNeeded() {
159   if (m_opaque_up == nullptr)
160     m_opaque_up = std::make_unique<Status>();
161 }
162 
163 lldb_private::Status *SBError::operator->() { return m_opaque_up.get(); }
164 
165 lldb_private::Status *SBError::get() { return m_opaque_up.get(); }
166 
167 lldb_private::Status &SBError::ref() {
168   CreateIfNeeded();
169   return *m_opaque_up;
170 }
171 
172 const lldb_private::Status &SBError::operator*() const {
173   // Be sure to call "IsValid()" before calling this function or it will crash
174   return *m_opaque_up;
175 }
176 
177 bool SBError::GetDescription(SBStream &description) {
178   LLDB_INSTRUMENT_VA(this, description);
179 
180   if (m_opaque_up) {
181     if (m_opaque_up->Success())
182       description.Printf("success");
183     else {
184       const char *err_string = GetCString();
185       description.Printf("error: %s",
186                          (err_string != nullptr ? err_string : ""));
187     }
188   } else
189     description.Printf("error: <NULL>");
190 
191   return true;
192 }
193