blob: 6ecc7717620b96d2a1202f1e6978a1efc642decb [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 Labatha24a3a32017-05-18 12:46:50 +000059Status::Status(llvm::Error error)
60 : m_code(0), m_type(ErrorType::eErrorTypeGeneric) {
61 if (!error)
62 return;
63
64 // if the error happens to be a errno error, preserve the error code
65 error = llvm::handleErrors(
66 std::move(error), [&](std::unique_ptr<llvm::ECError> e) -> llvm::Error {
67 std::error_code ec = e->convertToErrorCode();
68 if (ec.category() == std::generic_category()) {
69 m_code = ec.value();
70 m_type = ErrorType::eErrorTypePOSIX;
71 return llvm::Error::success();
72 }
73 return llvm::Error(std::move(e));
74 });
75
76 // Otherwise, just preserve the message
77 if (error)
78 SetErrorString(llvm::toString(std::move(error)));
79}
80
81llvm::Error Status::ToError() const {
82 if (Success())
83 return llvm::Error::success();
84 if (m_type == ErrorType::eErrorTypePOSIX)
85 return llvm::errorCodeToError(std::error_code(m_code, std::generic_category()));
86 return llvm::make_error<llvm::StringError>(AsCString(),
87 llvm::inconvertibleErrorCode());
88}
89
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090//----------------------------------------------------------------------
91// Assignment operator
92//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +000093const Status &Status::operator=(const Status &rhs) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 if (this != &rhs) {
95 m_code = rhs.m_code;
96 m_type = rhs.m_type;
97 m_string = rhs.m_string;
98 }
99 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100}
101
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102//----------------------------------------------------------------------
103// Assignment operator
104//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000105const Status &Status::operator=(uint32_t err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 m_code = err;
107 m_type = eErrorTypeMachKernel;
108 m_string.clear();
109 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
Zachary Turner97206d52017-05-12 04:51:55 +0000112Status::~Status() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113
114//----------------------------------------------------------------------
115// Get the error value as a NULL C string. The error string will be
116// fetched and cached on demand. The cached error string value will
117// remain until the error value is changed or cleared.
118//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000119const char *Status::AsCString(const char *default_error_str) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 if (Success())
121 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 if (m_string.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 switch (m_type) {
125 case eErrorTypeMachKernel:
126#if defined(__APPLE__)
Pavel Labath10c41f32017-06-06 14:06:17 +0000127 if (const char *s = ::mach_error_string(m_code))
128 m_string.assign(s);
Eli Friedman6eb685c2010-06-10 23:45:58 +0000129#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 case eErrorTypePOSIX:
Pavel Labath10c41f32017-06-06 14:06:17 +0000133 m_string = llvm::sys::StrError(m_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 default:
137 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 }
140 if (m_string.empty()) {
141 if (default_error_str)
142 m_string.assign(default_error_str);
143 else
144 return nullptr; // User wanted a nullptr string back...
145 }
146 return m_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149//----------------------------------------------------------------------
150// Clear the error and any cached error string that it might contain.
151//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000152void Status::Clear() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 m_code = 0;
154 m_type = eErrorTypeInvalid;
155 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156}
157
158//----------------------------------------------------------------------
159// Access the error value.
160//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000161Status::ValueType Status::GetError() const { return m_code; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162
163//----------------------------------------------------------------------
164// Access the error type.
165//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000166ErrorType Status::GetType() const { return m_type; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167
168//----------------------------------------------------------------------
Ed Maste75500e72016-07-19 15:28:02 +0000169// Returns true if this object contains a value that describes an
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170// error or otherwise non-success result.
171//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000172bool Status::Fail() const { return m_code != 0; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173
174//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175// Set accesssor for the error value to "err" and the type to
176// "eErrorTypeMachKernel"
177//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000178void Status::SetMachError(uint32_t err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 m_code = err;
180 m_type = eErrorTypeMachKernel;
181 m_string.clear();
182}
183
Zachary Turner97206d52017-05-12 04:51:55 +0000184void Status::SetExpressionError(lldb::ExpressionResults result,
185 const char *mssg) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 m_code = result;
187 m_type = eErrorTypeExpression;
188 m_string = mssg;
189}
190
Zachary Turner97206d52017-05-12 04:51:55 +0000191int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
192 const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 int length = 0;
194
195 if (format != nullptr && format[0]) {
196 va_list args;
197 va_start(args, format);
198 length = SetErrorStringWithVarArg(format, args);
199 va_end(args);
200 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 m_string.clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 }
203 m_code = result;
204 m_type = eErrorTypeExpression;
205 return length;
Jim Ingham1624a2d2014-05-05 02:26:40 +0000206}
207
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208//----------------------------------------------------------------------
209// Set accesssor for the error value and type.
210//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000211void Status::SetError(ValueType err, ErrorType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 m_code = err;
213 m_type = type;
214 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215}
216
217//----------------------------------------------------------------------
218// Update the error value to be "errno" and update the type to
219// be "POSIX".
220//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000221void Status::SetErrorToErrno() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 m_code = errno;
223 m_type = eErrorTypePOSIX;
224 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225}
226
227//----------------------------------------------------------------------
228// Update the error value to be LLDB_GENERIC_ERROR and update the type
229// to be "Generic".
230//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000231void Status::SetErrorToGenericError() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 m_code = LLDB_GENERIC_ERROR;
233 m_type = eErrorTypeGeneric;
234 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235}
236
237//----------------------------------------------------------------------
238// Set accessor for the error string value for a specific error.
239// This allows any string to be supplied as an error explanation.
240// The error string value will remain until the error value is
241// cleared or a new error value/type is assigned.
242//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000243void Status::SetErrorString(llvm::StringRef err_str) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000244 if (!err_str.empty()) {
245 // If we have an error string, we should always at least have an error
246 // set to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 if (Success())
248 SetErrorToGenericError();
Zachary Turnerc1564272016-11-16 21:15:24 +0000249 }
250 m_string = err_str;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251}
252
253//------------------------------------------------------------------
254/// Set the current error string to a formatted error string.
255///
256/// @param format
257/// A printf style format string
258//------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000259int Status::SetErrorStringWithFormat(const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 if (format != nullptr && format[0]) {
261 va_list args;
262 va_start(args, format);
263 int length = SetErrorStringWithVarArg(format, args);
264 va_end(args);
265 return length;
266 } else {
267 m_string.clear();
268 }
269 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270}
271
Zachary Turner97206d52017-05-12 04:51:55 +0000272int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 if (format != nullptr && format[0]) {
274 // If we have an error string, we should always at least have
275 // an error set to a generic value.
276 if (Success())
277 SetErrorToGenericError();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278
Zachary Turner24ae6292017-02-16 19:38:21 +0000279 llvm::SmallString<1024> buf;
280 VASprintf(buf, format, args);
Pavel Labatha272fa82017-02-17 10:19:46 +0000281 m_string = buf.str();
Zachary Turner24ae6292017-02-16 19:38:21 +0000282 return buf.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 } else {
284 m_string.clear();
285 }
286 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287}
288
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289//----------------------------------------------------------------------
290// Returns true if the error code in this object is considered a
291// successful return value.
292//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000293bool Status::Success() const { return m_code == 0; }
Jim Ingham4e5c8212013-06-07 22:09:53 +0000294
Zachary Turner97206d52017-05-12 04:51:55 +0000295bool Status::WasInterrupted() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296 return (m_type == eErrorTypePOSIX && m_code == EINTR);
Jim Ingham4e5c8212013-06-07 22:09:53 +0000297}
Zachary Turner33aba3c2017-02-06 18:31:44 +0000298
Zachary Turner97206d52017-05-12 04:51:55 +0000299void llvm::format_provider<lldb_private::Status>::format(
300 const lldb_private::Status &error, llvm::raw_ostream &OS,
Zachary Turner33aba3c2017-02-06 18:31:44 +0000301 llvm::StringRef Options) {
302 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
303 Options);
304}