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 SBError::SBError() : m_opaque_ap() {} 21 22 SBError::SBError(const SBError &rhs) : m_opaque_ap() { 23 if (rhs.IsValid()) 24 m_opaque_ap.reset(new Error(*rhs)); 25 } 26 27 SBError::~SBError() {} 28 29 const SBError &SBError::operator=(const SBError &rhs) { 30 if (rhs.IsValid()) { 31 if (m_opaque_ap.get()) 32 *m_opaque_ap = *rhs; 33 else 34 m_opaque_ap.reset(new Error(*rhs)); 35 } else 36 m_opaque_ap.reset(); 37 38 return *this; 39 } 40 41 const char *SBError::GetCString() const { 42 if (m_opaque_ap.get()) 43 return m_opaque_ap->AsCString(); 44 return NULL; 45 } 46 47 void SBError::Clear() { 48 if (m_opaque_ap.get()) 49 m_opaque_ap->Clear(); 50 } 51 52 bool SBError::Fail() const { 53 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 54 55 bool ret_value = false; 56 if (m_opaque_ap.get()) 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 66 bool SBError::Success() const { 67 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 68 bool ret_value = true; 69 if (m_opaque_ap.get()) 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 79 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.get()) 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 93 ErrorType SBError::GetType() const { 94 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 95 ErrorType err_type = eErrorTypeInvalid; 96 if (m_opaque_ap.get()) 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 106 void SBError::SetError(uint32_t err, ErrorType type) { 107 CreateIfNeeded(); 108 m_opaque_ap->SetError(err, type); 109 } 110 111 void SBError::SetError(const Error &lldb_error) { 112 CreateIfNeeded(); 113 *m_opaque_ap = lldb_error; 114 } 115 116 void SBError::SetErrorToErrno() { 117 CreateIfNeeded(); 118 m_opaque_ap->SetErrorToErrno(); 119 } 120 121 void SBError::SetErrorToGenericError() { 122 CreateIfNeeded(); 123 m_opaque_ap->SetErrorToErrno(); 124 } 125 126 void SBError::SetErrorString(const char *err_str) { 127 CreateIfNeeded(); 128 m_opaque_ap->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_ap->SetErrorStringWithVarArg(format, args); 136 va_end(args); 137 return num_chars; 138 } 139 140 bool SBError::IsValid() const { return m_opaque_ap.get() != NULL; } 141 142 void SBError::CreateIfNeeded() { 143 if (m_opaque_ap.get() == NULL) 144 m_opaque_ap.reset(new Error()); 145 } 146 147 lldb_private::Error *SBError::operator->() { return m_opaque_ap.get(); } 148 149 lldb_private::Error *SBError::get() { return m_opaque_ap.get(); } 150 151 lldb_private::Error &SBError::ref() { 152 CreateIfNeeded(); 153 return *m_opaque_ap; 154 } 155 156 const lldb_private::Error &SBError::operator*() const { 157 // Be sure to call "IsValid()" before calling this function or it will crash 158 return *m_opaque_ap; 159 } 160 161 bool SBError::GetDescription(SBStream &description) { 162 if (m_opaque_ap.get()) { 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