blob: e686204f8c461c933a9bf759bd13ec2d36ae38f9 [file] [log] [blame]
Zachary Turner97206d52017-05-12 04:51:55 +00001//===-- Status.cpp -----------------------------------------------*- C++
2//-*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
Zachary Turner97206d52017-05-12 04:51:55 +000011#include "lldb/Utility/Status.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000012
13#include "lldb/Utility/VASPrintf.h"
Pavel Labath10c41f32017-06-06 14:06:17 +000014#include "lldb/lldb-defines.h" // for LLDB_GENERIC_ERROR
15#include "lldb/lldb-enumerations.h" // for ErrorType, ErrorType::eErr...
16#include "llvm/ADT/SmallString.h" // for SmallString
17#include "llvm/ADT/StringRef.h" // for StringRef
18#include "llvm/Support/Errno.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000019#include "llvm/Support/FormatProviders.h" // for format_provider
20
21#include <cerrno>
22#include <cstdarg>
23#include <string> // for string
24#include <system_error>
25
Charles Davis322fc842013-08-27 06:13:56 +000026#ifdef __APPLE__
Charles Davis510938e2013-08-27 05:04:57 +000027#include <mach/mach.h>
Charles Davis322fc842013-08-27 06:13:56 +000028#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Zachary Turner4479ac12017-04-06 18:12:24 +000030#include <stdint.h> // for uint32_t
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000031
Zachary Turner4479ac12017-04-06 18:12:24 +000032namespace llvm {
33class raw_ostream;
34}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036using namespace lldb;
37using namespace lldb_private;
38
Zachary Turner97206d52017-05-12 04:51:55 +000039Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
Greg Claytonca512b32011-01-14 04:54:56 +000040
Zachary Turner97206d52017-05-12 04:51:55 +000041Status::Status(ValueType err, ErrorType type)
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 : m_code(err), m_type(type), m_string() {}
Greg Claytonca512b32011-01-14 04:54:56 +000043
Zachary Turner97206d52017-05-12 04:51:55 +000044Status::Status(std::error_code EC)
Zachary Turner7d86ee52017-03-08 17:56:08 +000045 : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric),
46 m_string(EC.message()) {}
47
Zachary Turner97206d52017-05-12 04:51:55 +000048Status::Status(const Status &rhs) = default;
Greg Claytonca512b32011-01-14 04:54:56 +000049
Zachary Turner97206d52017-05-12 04:51:55 +000050Status::Status(const char *format, ...)
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 : m_code(0), m_type(eErrorTypeInvalid), m_string() {
52 va_list args;
53 va_start(args, format);
54 SetErrorToGenericError();
55 SetErrorStringWithVarArg(format, args);
56 va_end(args);
Enrico Granataf2bbf712011-07-15 02:26:42 +000057}
58
Pavel Labath3adc4082017-06-15 11:23:26 +000059const Status &Status::operator=(llvm::Error error) {
60 if (!error) {
61 Clear();
62 return *this;
63 }
Pavel Labatha24a3a32017-05-18 12:46:50 +000064
65 // if the error happens to be a errno error, preserve the error code
66 error = llvm::handleErrors(
67 std::move(error), [&](std::unique_ptr<llvm::ECError> e) -> llvm::Error {
68 std::error_code ec = e->convertToErrorCode();
69 if (ec.category() == std::generic_category()) {
70 m_code = ec.value();
71 m_type = ErrorType::eErrorTypePOSIX;
72 return llvm::Error::success();
73 }
74 return llvm::Error(std::move(e));
75 });
76
77 // Otherwise, just preserve the message
Pavel Labath3adc4082017-06-15 11:23:26 +000078 if (error) {
79 SetErrorToGenericError();
Pavel Labatha24a3a32017-05-18 12:46:50 +000080 SetErrorString(llvm::toString(std::move(error)));
Pavel Labath3adc4082017-06-15 11:23:26 +000081 }
82
83 return *this;
Pavel Labatha24a3a32017-05-18 12:46:50 +000084}
85
86llvm::Error Status::ToError() const {
87 if (Success())
88 return llvm::Error::success();
89 if (m_type == ErrorType::eErrorTypePOSIX)
90 return llvm::errorCodeToError(std::error_code(m_code, std::generic_category()));
91 return llvm::make_error<llvm::StringError>(AsCString(),
92 llvm::inconvertibleErrorCode());
93}
94
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095//----------------------------------------------------------------------
96// Assignment operator
97//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +000098const Status &Status::operator=(const Status &rhs) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 if (this != &rhs) {
100 m_code = rhs.m_code;
101 m_type = rhs.m_type;
102 m_string = rhs.m_string;
103 }
104 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105}
106
Zachary Turner97206d52017-05-12 04:51:55 +0000107Status::~Status() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
109//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000110// Get the error value as a NULL C string. The error string will be fetched and
111// cached on demand. The cached error string value will remain until the error
112// value is changed or cleared.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000114const char *Status::AsCString(const char *default_error_str) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 if (Success())
116 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 if (m_string.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 switch (m_type) {
120 case eErrorTypeMachKernel:
121#if defined(__APPLE__)
Pavel Labath10c41f32017-06-06 14:06:17 +0000122 if (const char *s = ::mach_error_string(m_code))
123 m_string.assign(s);
Eli Friedman6eb685c2010-06-10 23:45:58 +0000124#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 case eErrorTypePOSIX:
Pavel Labath10c41f32017-06-06 14:06:17 +0000128 m_string = llvm::sys::StrError(m_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 default:
132 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 }
135 if (m_string.empty()) {
136 if (default_error_str)
137 m_string.assign(default_error_str);
138 else
139 return nullptr; // User wanted a nullptr string back...
140 }
141 return m_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142}
143
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144//----------------------------------------------------------------------
145// Clear the error and any cached error string that it might contain.
146//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000147void Status::Clear() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 m_code = 0;
149 m_type = eErrorTypeInvalid;
150 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151}
152
153//----------------------------------------------------------------------
154// Access the error value.
155//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000156Status::ValueType Status::GetError() const { return m_code; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157
158//----------------------------------------------------------------------
159// Access the error type.
160//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000161ErrorType Status::GetType() const { return m_type; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162
163//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000164// Returns true if this object contains a value that describes an error or
165// otherwise non-success result.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000167bool Status::Fail() const { return m_code != 0; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168
169//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170// Set accesssor for the error value to "err" and the type to
171// "eErrorTypeMachKernel"
172//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000173void Status::SetMachError(uint32_t err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 m_code = err;
175 m_type = eErrorTypeMachKernel;
176 m_string.clear();
177}
178
Zachary Turner97206d52017-05-12 04:51:55 +0000179void Status::SetExpressionError(lldb::ExpressionResults result,
180 const char *mssg) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 m_code = result;
182 m_type = eErrorTypeExpression;
183 m_string = mssg;
184}
185
Zachary Turner97206d52017-05-12 04:51:55 +0000186int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
187 const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 int length = 0;
189
190 if (format != nullptr && format[0]) {
191 va_list args;
192 va_start(args, format);
193 length = SetErrorStringWithVarArg(format, args);
194 va_end(args);
195 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196 m_string.clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 }
198 m_code = result;
199 m_type = eErrorTypeExpression;
200 return length;
Jim Ingham1624a2d2014-05-05 02:26:40 +0000201}
202
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203//----------------------------------------------------------------------
204// Set accesssor for the error value and type.
205//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000206void Status::SetError(ValueType err, ErrorType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 m_code = err;
208 m_type = type;
209 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210}
211
212//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000213// Update the error value to be "errno" and update the type to be "POSIX".
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000215void Status::SetErrorToErrno() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 m_code = errno;
217 m_type = eErrorTypePOSIX;
218 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219}
220
221//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000222// Update the error value to be LLDB_GENERIC_ERROR and update the type to be
223// "Generic".
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000225void Status::SetErrorToGenericError() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 m_code = LLDB_GENERIC_ERROR;
227 m_type = eErrorTypeGeneric;
228 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229}
230
231//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000232// Set accessor for the error string value for a specific error. This allows
233// any string to be supplied as an error explanation. The error string value
234// will remain until the error value is cleared or a new error value/type is
235// assigned.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000237void Status::SetErrorString(llvm::StringRef err_str) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000238 if (!err_str.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000239 // If we have an error string, we should always at least have an error set
240 // to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 if (Success())
242 SetErrorToGenericError();
Zachary Turnerc1564272016-11-16 21:15:24 +0000243 }
244 m_string = err_str;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245}
246
247//------------------------------------------------------------------
248/// Set the current error string to a formatted error string.
249///
250/// @param format
251/// A printf style format string
252//------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000253int Status::SetErrorStringWithFormat(const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 if (format != nullptr && format[0]) {
255 va_list args;
256 va_start(args, format);
257 int length = SetErrorStringWithVarArg(format, args);
258 va_end(args);
259 return length;
260 } else {
261 m_string.clear();
262 }
263 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264}
265
Zachary Turner97206d52017-05-12 04:51:55 +0000266int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 if (format != nullptr && format[0]) {
Adrian Prantl05097242018-04-30 16:49:04 +0000268 // If we have an error string, we should always at least have an error set
269 // to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 if (Success())
271 SetErrorToGenericError();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272
Zachary Turner24ae6292017-02-16 19:38:21 +0000273 llvm::SmallString<1024> buf;
274 VASprintf(buf, format, args);
Pavel Labatha272fa82017-02-17 10:19:46 +0000275 m_string = buf.str();
Zachary Turner24ae6292017-02-16 19:38:21 +0000276 return buf.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 } else {
278 m_string.clear();
279 }
280 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281}
282
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000284// Returns true if the error code in this object is considered a successful
285// return value.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000287bool Status::Success() const { return m_code == 0; }
Jim Ingham4e5c8212013-06-07 22:09:53 +0000288
Zachary Turner97206d52017-05-12 04:51:55 +0000289bool Status::WasInterrupted() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 return (m_type == eErrorTypePOSIX && m_code == EINTR);
Jim Ingham4e5c8212013-06-07 22:09:53 +0000291}
Zachary Turner33aba3c2017-02-06 18:31:44 +0000292
Zachary Turner97206d52017-05-12 04:51:55 +0000293void llvm::format_provider<lldb_private::Status>::format(
294 const lldb_private::Status &error, llvm::raw_ostream &OS,
Zachary Turner33aba3c2017-02-06 18:31:44 +0000295 llvm::StringRef Options) {
296 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
297 Options);
298}