Chris Lattner | 30fdc8d | 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 | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 11 | #include "lldb/API/SBStream.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 12 | #include "lldb/Utility/Error.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 13 | #include "lldb/Utility/Log.h" |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 14 | |
Eli Friedman | 9c68da9 | 2010-06-09 08:46:23 +0000 | [diff] [blame] | 15 | #include <stdarg.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace lldb; |
| 18 | using namespace lldb_private; |
| 19 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | SBError::SBError() : m_opaque_ap() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | SBError::SBError(const SBError &rhs) : m_opaque_ap() { |
| 23 | if (rhs.IsValid()) |
| 24 | m_opaque_ap.reset(new Error(*rhs)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | SBError::~SBError() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | const SBError &SBError::operator=(const SBError &rhs) { |
| 30 | if (rhs.IsValid()) { |
| 31 | if (m_opaque_ap.get()) |
| 32 | *m_opaque_ap = *rhs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | m_opaque_ap.reset(new Error(*rhs)); |
| 35 | } else |
| 36 | m_opaque_ap.reset(); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | const char *SBError::GetCString() const { |
| 42 | if (m_opaque_ap.get()) |
| 43 | return m_opaque_ap->AsCString(); |
| 44 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | void SBError::Clear() { |
| 48 | if (m_opaque_ap.get()) |
| 49 | m_opaque_ap->Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | bool SBError::Fail() const { |
| 53 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 54 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | bool ret_value = false; |
| 56 | if (m_opaque_ap.get()) |
| 57 | ret_value = m_opaque_ap->Fail(); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | if (log) |
| 60 | log->Printf("SBError(%p)::Fail () => %i", |
| 61 | static_cast<void *>(m_opaque_ap.get()), ret_value); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | return ret_value; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | if (log) |
| 73 | log->Printf("SBError(%p)::Success () => %i", |
| 74 | static_cast<void *>(m_opaque_ap.get()), ret_value); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | return ret_value; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | uint32_t SBError::GetError() const { |
| 80 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | uint32_t err = 0; |
| 83 | if (m_opaque_ap.get()) |
| 84 | err = m_opaque_ap->GetError(); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | if (log) |
| 87 | log->Printf("SBError(%p)::GetError () => 0x%8.8x", |
| 88 | static_cast<void *>(m_opaque_ap.get()), err); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | return err; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | if (log) |
| 100 | log->Printf("SBError(%p)::GetType () => %i", |
| 101 | static_cast<void *>(m_opaque_ap.get()), err_type); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | return err_type; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | void SBError::SetError(uint32_t err, ErrorType type) { |
| 107 | CreateIfNeeded(); |
| 108 | m_opaque_ap->SetError(err, type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | void SBError::SetError(const Error &lldb_error) { |
| 112 | CreateIfNeeded(); |
| 113 | *m_opaque_ap = lldb_error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | void SBError::SetErrorToErrno() { |
| 117 | CreateIfNeeded(); |
| 118 | m_opaque_ap->SetErrorToErrno(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | void SBError::SetErrorToGenericError() { |
| 122 | CreateIfNeeded(); |
| 123 | m_opaque_ap->SetErrorToErrno(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | void SBError::SetErrorString(const char *err_str) { |
| 127 | CreateIfNeeded(); |
| 128 | m_opaque_ap->SetErrorString(err_str); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 : "")); |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 168 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | } else |
| 170 | description.Printf("error: <NULL>"); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | return true; |
| 173 | } |