blob: b5c6ce9232492ed72f6aa1e0a8955177edef5fff [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Error.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// C Includes
Charles Davis322fc842013-08-27 06:13:56 +000011#ifdef __APPLE__
Charles Davis510938e2013-08-27 05:04:57 +000012#include <mach/mach.h>
Charles Davis322fc842013-08-27 06:13:56 +000013#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15// C++ Includes
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000016#include <cerrno>
17#include <cstdarg>
18
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019// Other libraries and framework includes
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000020#include "llvm/ADT/SmallVector.h"
21
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Log.h"
Zachary Turnerf343968f2016-08-09 23:06:08 +000024#include "lldb/Host/PosixApi.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000025#include "lldb/Utility/Error.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027using namespace lldb;
28using namespace lldb_private;
29
Kate Stoneb9c1b512016-09-06 20:57:50 +000030Error::Error() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
Greg Claytonca512b32011-01-14 04:54:56 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032Error::Error(ValueType err, ErrorType type)
33 : m_code(err), m_type(type), m_string() {}
Greg Claytonca512b32011-01-14 04:54:56 +000034
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000035Error::Error(const Error &rhs) = default;
Greg Claytonca512b32011-01-14 04:54:56 +000036
Kate Stoneb9c1b512016-09-06 20:57:50 +000037Error::Error(const char *format, ...)
38 : m_code(0), m_type(eErrorTypeInvalid), m_string() {
39 va_list args;
40 va_start(args, format);
41 SetErrorToGenericError();
42 SetErrorStringWithVarArg(format, args);
43 va_end(args);
Enrico Granataf2bbf712011-07-15 02:26:42 +000044}
45
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046//----------------------------------------------------------------------
47// Assignment operator
48//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000049const Error &Error::operator=(const Error &rhs) {
50 if (this != &rhs) {
51 m_code = rhs.m_code;
52 m_type = rhs.m_type;
53 m_string = rhs.m_string;
54 }
55 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058//----------------------------------------------------------------------
59// Assignment operator
60//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000061const Error &Error::operator=(uint32_t err) {
62 m_code = err;
63 m_type = eErrorTypeMachKernel;
64 m_string.clear();
65 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066}
67
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000068Error::~Error() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069
70//----------------------------------------------------------------------
71// Get the error value as a NULL C string. The error string will be
72// fetched and cached on demand. The cached error string value will
73// remain until the error value is changed or cleared.
74//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000075const char *Error::AsCString(const char *default_error_str) const {
76 if (Success())
77 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 if (m_string.empty()) {
80 const char *s = nullptr;
81 switch (m_type) {
82 case eErrorTypeMachKernel:
83#if defined(__APPLE__)
84 s = ::mach_error_string(m_code);
Eli Friedman6eb685c2010-06-10 23:45:58 +000085#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 case eErrorTypePOSIX:
89 s = ::strerror(m_code);
90 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 default:
93 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 if (s != nullptr)
96 m_string.assign(s);
97 }
98 if (m_string.empty()) {
99 if (default_error_str)
100 m_string.assign(default_error_str);
101 else
102 return nullptr; // User wanted a nullptr string back...
103 }
104 return m_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105}
106
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107//----------------------------------------------------------------------
108// Clear the error and any cached error string that it might contain.
109//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110void Error::Clear() {
111 m_code = 0;
112 m_type = eErrorTypeInvalid;
113 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114}
115
116//----------------------------------------------------------------------
117// Access the error value.
118//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119Error::ValueType Error::GetError() const { return m_code; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120
121//----------------------------------------------------------------------
122// Access the error type.
123//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124ErrorType Error::GetType() const { return m_type; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125
126//----------------------------------------------------------------------
Ed Maste75500e72016-07-19 15:28:02 +0000127// Returns true if this object contains a value that describes an
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128// error or otherwise non-success result.
129//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130bool Error::Fail() const { return m_code != 0; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131
132//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133// Set accesssor for the error value to "err" and the type to
134// "eErrorTypeMachKernel"
135//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136void Error::SetMachError(uint32_t err) {
137 m_code = err;
138 m_type = eErrorTypeMachKernel;
139 m_string.clear();
140}
141
142void Error::SetExpressionError(lldb::ExpressionResults result,
143 const char *mssg) {
144 m_code = result;
145 m_type = eErrorTypeExpression;
146 m_string = mssg;
147}
148
149int Error::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
150 const char *format, ...) {
151 int length = 0;
152
153 if (format != nullptr && format[0]) {
154 va_list args;
155 va_start(args, format);
156 length = SetErrorStringWithVarArg(format, args);
157 va_end(args);
158 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159 m_string.clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 }
161 m_code = result;
162 m_type = eErrorTypeExpression;
163 return length;
Jim Ingham1624a2d2014-05-05 02:26:40 +0000164}
165
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166//----------------------------------------------------------------------
167// Set accesssor for the error value and type.
168//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169void Error::SetError(ValueType err, ErrorType type) {
170 m_code = err;
171 m_type = type;
172 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173}
174
175//----------------------------------------------------------------------
176// Update the error value to be "errno" and update the type to
177// be "POSIX".
178//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179void Error::SetErrorToErrno() {
180 m_code = errno;
181 m_type = eErrorTypePOSIX;
182 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183}
184
185//----------------------------------------------------------------------
186// Update the error value to be LLDB_GENERIC_ERROR and update the type
187// to be "Generic".
188//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189void Error::SetErrorToGenericError() {
190 m_code = LLDB_GENERIC_ERROR;
191 m_type = eErrorTypeGeneric;
192 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193}
194
195//----------------------------------------------------------------------
196// Set accessor for the error string value for a specific error.
197// This allows any string to be supplied as an error explanation.
198// The error string value will remain until the error value is
199// cleared or a new error value/type is assigned.
200//----------------------------------------------------------------------
Zachary Turnerc1564272016-11-16 21:15:24 +0000201void Error::SetErrorString(llvm::StringRef err_str) {
202 if (!err_str.empty()) {
203 // If we have an error string, we should always at least have an error
204 // set to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 if (Success())
206 SetErrorToGenericError();
Zachary Turnerc1564272016-11-16 21:15:24 +0000207 }
208 m_string = err_str;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209}
210
211//------------------------------------------------------------------
212/// Set the current error string to a formatted error string.
213///
214/// @param format
215/// A printf style format string
216//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217int Error::SetErrorStringWithFormat(const char *format, ...) {
218 if (format != nullptr && format[0]) {
219 va_list args;
220 va_start(args, format);
221 int length = SetErrorStringWithVarArg(format, args);
222 va_end(args);
223 return length;
224 } else {
225 m_string.clear();
226 }
227 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228}
229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230int Error::SetErrorStringWithVarArg(const char *format, va_list args) {
231 if (format != nullptr && format[0]) {
232 // If we have an error string, we should always at least have
233 // an error set to a generic value.
234 if (Success())
235 SetErrorToGenericError();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 // Try and fit our error into a 1024 byte buffer first...
238 llvm::SmallVector<char, 1024> buf;
239 buf.resize(1024);
240 // Copy in case our first call to vsnprintf doesn't fit into our
241 // allocated buffer above
242 va_list copy_args;
243 va_copy(copy_args, args);
244 unsigned length = ::vsnprintf(buf.data(), buf.size(), format, args);
245 if (length >= buf.size()) {
246 // The error formatted string didn't fit into our buffer, resize it
247 // to the exact needed size, and retry
248 buf.resize(length + 1);
249 length = ::vsnprintf(buf.data(), buf.size(), format, copy_args);
250 va_end(copy_args);
251 assert(length < buf.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 m_string.assign(buf.data(), length);
254 va_end(args);
255 return length;
256 } else {
257 m_string.clear();
258 }
259 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260}
261
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262//----------------------------------------------------------------------
263// Returns true if the error code in this object is considered a
264// successful return value.
265//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266bool Error::Success() const { return m_code == 0; }
Jim Ingham4e5c8212013-06-07 22:09:53 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268bool Error::WasInterrupted() const {
269 return (m_type == eErrorTypePOSIX && m_code == EINTR);
Jim Ingham4e5c8212013-06-07 22:09:53 +0000270}
Zachary Turner33aba3c2017-02-06 18:31:44 +0000271
272void llvm::format_provider<lldb_private::Error>::format(
273 const lldb_private::Error &error, llvm::raw_ostream &OS,
274 llvm::StringRef Options) {
275 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
276 Options);
277}