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