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