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/Core/Error.h" 13 #include "lldb/Core/Log.h" 14 15 #include <stdarg.h> 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 21 SBError::SBError () : 22 m_opaque_ap () 23 { 24 } 25 26 SBError::SBError (const SBError &rhs) : 27 m_opaque_ap () 28 { 29 if (rhs.IsValid()) 30 m_opaque_ap.reset (new Error(*rhs)); 31 } 32 33 34 SBError::~SBError() 35 { 36 } 37 38 const SBError & 39 SBError::operator = (const SBError &rhs) 40 { 41 if (rhs.IsValid()) 42 { 43 if (m_opaque_ap.get()) 44 *m_opaque_ap = *rhs; 45 else 46 m_opaque_ap.reset (new Error(*rhs)); 47 } 48 else 49 m_opaque_ap.reset(); 50 51 return *this; 52 } 53 54 55 const char * 56 SBError::GetCString () const 57 { 58 if (m_opaque_ap.get()) 59 return m_opaque_ap->AsCString(); 60 return NULL; 61 } 62 63 void 64 SBError::Clear () 65 { 66 if (m_opaque_ap.get()) 67 m_opaque_ap->Clear(); 68 } 69 70 bool 71 SBError::Fail () const 72 { 73 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 74 75 bool ret_value = false; 76 if (m_opaque_ap.get()) 77 ret_value = m_opaque_ap->Fail(); 78 79 if (log) 80 log->Printf ("SBError(%p)::Fail () => %i", 81 static_cast<void*>(m_opaque_ap.get()), ret_value); 82 83 return ret_value; 84 } 85 86 bool 87 SBError::Success () const 88 { 89 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 90 bool ret_value = true; 91 if (m_opaque_ap.get()) 92 ret_value = m_opaque_ap->Success(); 93 94 if (log) 95 log->Printf ("SBError(%p)::Success () => %i", 96 static_cast<void*>(m_opaque_ap.get()), ret_value); 97 98 return ret_value; 99 } 100 101 uint32_t 102 SBError::GetError () const 103 { 104 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 105 106 uint32_t err = 0; 107 if (m_opaque_ap.get()) 108 err = m_opaque_ap->GetError(); 109 110 if (log) 111 log->Printf ("SBError(%p)::GetError () => 0x%8.8x", 112 static_cast<void*>(m_opaque_ap.get()), err); 113 114 115 return err; 116 } 117 118 ErrorType 119 SBError::GetType () const 120 { 121 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 122 ErrorType err_type = eErrorTypeInvalid; 123 if (m_opaque_ap.get()) 124 err_type = m_opaque_ap->GetType(); 125 126 if (log) 127 log->Printf ("SBError(%p)::GetType () => %i", 128 static_cast<void*>(m_opaque_ap.get()), err_type); 129 130 return err_type; 131 } 132 133 void 134 SBError::SetError (uint32_t err, ErrorType type) 135 { 136 CreateIfNeeded (); 137 m_opaque_ap->SetError (err, type); 138 } 139 140 void 141 SBError::SetError (const Error &lldb_error) 142 { 143 CreateIfNeeded (); 144 *m_opaque_ap = lldb_error; 145 } 146 147 148 void 149 SBError::SetErrorToErrno () 150 { 151 CreateIfNeeded (); 152 m_opaque_ap->SetErrorToErrno (); 153 } 154 155 void 156 SBError::SetErrorToGenericError () 157 { 158 CreateIfNeeded (); 159 m_opaque_ap->SetErrorToErrno (); 160 } 161 162 void 163 SBError::SetErrorString (const char *err_str) 164 { 165 CreateIfNeeded (); 166 m_opaque_ap->SetErrorString (err_str); 167 } 168 169 int 170 SBError::SetErrorStringWithFormat (const char *format, ...) 171 { 172 CreateIfNeeded (); 173 va_list args; 174 va_start (args, format); 175 int num_chars = m_opaque_ap->SetErrorStringWithVarArg (format, args); 176 va_end (args); 177 return num_chars; 178 } 179 180 bool 181 SBError::IsValid () const 182 { 183 return m_opaque_ap.get() != NULL; 184 } 185 186 void 187 SBError::CreateIfNeeded () 188 { 189 if (m_opaque_ap.get() == NULL) 190 m_opaque_ap.reset(new Error ()); 191 } 192 193 194 lldb_private::Error * 195 SBError::operator->() 196 { 197 return m_opaque_ap.get(); 198 } 199 200 lldb_private::Error * 201 SBError::get() 202 { 203 return m_opaque_ap.get(); 204 } 205 206 lldb_private::Error & 207 SBError::ref() 208 { 209 CreateIfNeeded(); 210 return *m_opaque_ap; 211 } 212 213 const lldb_private::Error & 214 SBError::operator*() const 215 { 216 // Be sure to call "IsValid()" before calling this function or it will crash 217 return *m_opaque_ap; 218 } 219 220 bool 221 SBError::GetDescription (SBStream &description) 222 { 223 if (m_opaque_ap.get()) 224 { 225 if (m_opaque_ap->Success()) 226 description.Printf ("success"); 227 else 228 { 229 const char * err_string = GetCString(); 230 description.Printf ("error: %s", (err_string != NULL ? err_string : "")); 231 } 232 } 233 else 234 description.Printf ("error: <NULL>"); 235 236 return true; 237 } 238