1 //===-- SBError.cpp ---------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/API/SBError.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Utility/Log.h"
13 #include "lldb/Utility/Status.h"
14
15 #include <stdarg.h>
16
17 using namespace lldb;
18 using namespace lldb_private;
19
SBError()20 SBError::SBError() : m_opaque_ap() {}
21
SBError(const SBError & rhs)22 SBError::SBError(const SBError &rhs) : m_opaque_ap() {
23 if (rhs.IsValid())
24 m_opaque_ap.reset(new Status(*rhs));
25 }
26
~SBError()27 SBError::~SBError() {}
28
operator =(const SBError & rhs)29 const SBError &SBError::operator=(const SBError &rhs) {
30 if (rhs.IsValid()) {
31 if (m_opaque_ap)
32 *m_opaque_ap = *rhs;
33 else
34 m_opaque_ap.reset(new Status(*rhs));
35 } else
36 m_opaque_ap.reset();
37
38 return *this;
39 }
40
GetCString() const41 const char *SBError::GetCString() const {
42 if (m_opaque_ap)
43 return m_opaque_ap->AsCString();
44 return NULL;
45 }
46
Clear()47 void SBError::Clear() {
48 if (m_opaque_ap)
49 m_opaque_ap->Clear();
50 }
51
Fail() const52 bool SBError::Fail() const {
53 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
54
55 bool ret_value = false;
56 if (m_opaque_ap)
57 ret_value = m_opaque_ap->Fail();
58
59 if (log)
60 log->Printf("SBError(%p)::Fail () => %i",
61 static_cast<void *>(m_opaque_ap.get()), ret_value);
62
63 return ret_value;
64 }
65
Success() const66 bool SBError::Success() const {
67 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
68 bool ret_value = true;
69 if (m_opaque_ap)
70 ret_value = m_opaque_ap->Success();
71
72 if (log)
73 log->Printf("SBError(%p)::Success () => %i",
74 static_cast<void *>(m_opaque_ap.get()), ret_value);
75
76 return ret_value;
77 }
78
GetError() const79 uint32_t SBError::GetError() const {
80 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
81
82 uint32_t err = 0;
83 if (m_opaque_ap)
84 err = m_opaque_ap->GetError();
85
86 if (log)
87 log->Printf("SBError(%p)::GetError () => 0x%8.8x",
88 static_cast<void *>(m_opaque_ap.get()), err);
89
90 return err;
91 }
92
GetType() const93 ErrorType SBError::GetType() const {
94 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
95 ErrorType err_type = eErrorTypeInvalid;
96 if (m_opaque_ap)
97 err_type = m_opaque_ap->GetType();
98
99 if (log)
100 log->Printf("SBError(%p)::GetType () => %i",
101 static_cast<void *>(m_opaque_ap.get()), err_type);
102
103 return err_type;
104 }
105
SetError(uint32_t err,ErrorType type)106 void SBError::SetError(uint32_t err, ErrorType type) {
107 CreateIfNeeded();
108 m_opaque_ap->SetError(err, type);
109 }
110
SetError(const Status & lldb_error)111 void SBError::SetError(const Status &lldb_error) {
112 CreateIfNeeded();
113 *m_opaque_ap = lldb_error;
114 }
115
SetErrorToErrno()116 void SBError::SetErrorToErrno() {
117 CreateIfNeeded();
118 m_opaque_ap->SetErrorToErrno();
119 }
120
SetErrorToGenericError()121 void SBError::SetErrorToGenericError() {
122 CreateIfNeeded();
123 m_opaque_ap->SetErrorToErrno();
124 }
125
SetErrorString(const char * err_str)126 void SBError::SetErrorString(const char *err_str) {
127 CreateIfNeeded();
128 m_opaque_ap->SetErrorString(err_str);
129 }
130
SetErrorStringWithFormat(const char * format,...)131 int SBError::SetErrorStringWithFormat(const char *format, ...) {
132 CreateIfNeeded();
133 va_list args;
134 va_start(args, format);
135 int num_chars = m_opaque_ap->SetErrorStringWithVarArg(format, args);
136 va_end(args);
137 return num_chars;
138 }
139
IsValid() const140 bool SBError::IsValid() const { return m_opaque_ap != NULL; }
141
CreateIfNeeded()142 void SBError::CreateIfNeeded() {
143 if (m_opaque_ap == NULL)
144 m_opaque_ap.reset(new Status());
145 }
146
operator ->()147 lldb_private::Status *SBError::operator->() { return m_opaque_ap.get(); }
148
get()149 lldb_private::Status *SBError::get() { return m_opaque_ap.get(); }
150
ref()151 lldb_private::Status &SBError::ref() {
152 CreateIfNeeded();
153 return *m_opaque_ap;
154 }
155
operator *() const156 const lldb_private::Status &SBError::operator*() const {
157 // Be sure to call "IsValid()" before calling this function or it will crash
158 return *m_opaque_ap;
159 }
160
GetDescription(SBStream & description)161 bool SBError::GetDescription(SBStream &description) {
162 if (m_opaque_ap) {
163 if (m_opaque_ap->Success())
164 description.Printf("success");
165 else {
166 const char *err_string = GetCString();
167 description.Printf("error: %s", (err_string != NULL ? err_string : ""));
168 }
169 } else
170 description.Printf("error: <NULL>");
171
172 return true;
173 }
174