blob: 5996be1e4e0598df3d352af17c1197ca11999717 [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"
14#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/FormatProviders.h" // for format_provider
19
20#include <cerrno>
21#include <cstdarg>
22#include <string> // for string
23#include <system_error>
24
Charles Davis322fc842013-08-27 06:13:56 +000025#ifdef __APPLE__
Charles Davis510938e2013-08-27 05:04:57 +000026#include <mach/mach.h>
Charles Davis322fc842013-08-27 06:13:56 +000027#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
Zachary Turner4479ac12017-04-06 18:12:24 +000029#include <stdint.h> // for uint32_t
30#include <string.h> // for strerror
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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059//----------------------------------------------------------------------
60// Assignment operator
61//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +000062const Status &Status::operator=(const Status &rhs) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 if (this != &rhs) {
64 m_code = rhs.m_code;
65 m_type = rhs.m_type;
66 m_string = rhs.m_string;
67 }
68 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069}
70
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071//----------------------------------------------------------------------
72// Assignment operator
73//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +000074const Status &Status::operator=(uint32_t err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 m_code = err;
76 m_type = eErrorTypeMachKernel;
77 m_string.clear();
78 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079}
80
Zachary Turner97206d52017-05-12 04:51:55 +000081Status::~Status() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082
83//----------------------------------------------------------------------
84// Get the error value as a NULL C string. The error string will be
85// fetched and cached on demand. The cached error string value will
86// remain until the error value is changed or cleared.
87//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +000088const char *Status::AsCString(const char *default_error_str) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (Success())
90 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 if (m_string.empty()) {
93 const char *s = nullptr;
94 switch (m_type) {
95 case eErrorTypeMachKernel:
96#if defined(__APPLE__)
97 s = ::mach_error_string(m_code);
Eli Friedman6eb685c2010-06-10 23:45:58 +000098#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 case eErrorTypePOSIX:
102 s = ::strerror(m_code);
103 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 default:
106 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 if (s != nullptr)
109 m_string.assign(s);
110 }
111 if (m_string.empty()) {
112 if (default_error_str)
113 m_string.assign(default_error_str);
114 else
115 return nullptr; // User wanted a nullptr string back...
116 }
117 return m_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118}
119
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120//----------------------------------------------------------------------
121// Clear the error and any cached error string that it might contain.
122//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000123void Status::Clear() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 m_code = 0;
125 m_type = eErrorTypeInvalid;
126 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127}
128
129//----------------------------------------------------------------------
130// Access the error value.
131//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000132Status::ValueType Status::GetError() const { return m_code; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133
134//----------------------------------------------------------------------
135// Access the error type.
136//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000137ErrorType Status::GetType() const { return m_type; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138
139//----------------------------------------------------------------------
Ed Maste75500e72016-07-19 15:28:02 +0000140// Returns true if this object contains a value that describes an
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141// error or otherwise non-success result.
142//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000143bool Status::Fail() const { return m_code != 0; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144
145//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146// Set accesssor for the error value to "err" and the type to
147// "eErrorTypeMachKernel"
148//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000149void Status::SetMachError(uint32_t err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 m_code = err;
151 m_type = eErrorTypeMachKernel;
152 m_string.clear();
153}
154
Zachary Turner97206d52017-05-12 04:51:55 +0000155void Status::SetExpressionError(lldb::ExpressionResults result,
156 const char *mssg) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 m_code = result;
158 m_type = eErrorTypeExpression;
159 m_string = mssg;
160}
161
Zachary Turner97206d52017-05-12 04:51:55 +0000162int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
163 const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 int length = 0;
165
166 if (format != nullptr && format[0]) {
167 va_list args;
168 va_start(args, format);
169 length = SetErrorStringWithVarArg(format, args);
170 va_end(args);
171 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172 m_string.clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 }
174 m_code = result;
175 m_type = eErrorTypeExpression;
176 return length;
Jim Ingham1624a2d2014-05-05 02:26:40 +0000177}
178
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179//----------------------------------------------------------------------
180// Set accesssor for the error value and type.
181//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000182void Status::SetError(ValueType err, ErrorType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 m_code = err;
184 m_type = type;
185 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186}
187
188//----------------------------------------------------------------------
189// Update the error value to be "errno" and update the type to
190// be "POSIX".
191//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000192void Status::SetErrorToErrno() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 m_code = errno;
194 m_type = eErrorTypePOSIX;
195 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196}
197
198//----------------------------------------------------------------------
199// Update the error value to be LLDB_GENERIC_ERROR and update the type
200// to be "Generic".
201//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000202void Status::SetErrorToGenericError() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 m_code = LLDB_GENERIC_ERROR;
204 m_type = eErrorTypeGeneric;
205 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
208//----------------------------------------------------------------------
209// Set accessor for the error string value for a specific error.
210// This allows any string to be supplied as an error explanation.
211// The error string value will remain until the error value is
212// cleared or a new error value/type is assigned.
213//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000214void Status::SetErrorString(llvm::StringRef err_str) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000215 if (!err_str.empty()) {
216 // If we have an error string, we should always at least have an error
217 // set to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 if (Success())
219 SetErrorToGenericError();
Zachary Turnerc1564272016-11-16 21:15:24 +0000220 }
221 m_string = err_str;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222}
223
224//------------------------------------------------------------------
225/// Set the current error string to a formatted error string.
226///
227/// @param format
228/// A printf style format string
229//------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000230int Status::SetErrorStringWithFormat(const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 if (format != nullptr && format[0]) {
232 va_list args;
233 va_start(args, format);
234 int length = SetErrorStringWithVarArg(format, args);
235 va_end(args);
236 return length;
237 } else {
238 m_string.clear();
239 }
240 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241}
242
Zachary Turner97206d52017-05-12 04:51:55 +0000243int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if (format != nullptr && format[0]) {
245 // If we have an error string, we should always at least have
246 // an error set to a generic value.
247 if (Success())
248 SetErrorToGenericError();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249
Zachary Turner24ae6292017-02-16 19:38:21 +0000250 llvm::SmallString<1024> buf;
251 VASprintf(buf, format, args);
Pavel Labatha272fa82017-02-17 10:19:46 +0000252 m_string = buf.str();
Zachary Turner24ae6292017-02-16 19:38:21 +0000253 return buf.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 } else {
255 m_string.clear();
256 }
257 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258}
259
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260//----------------------------------------------------------------------
261// Returns true if the error code in this object is considered a
262// successful return value.
263//----------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000264bool Status::Success() const { return m_code == 0; }
Jim Ingham4e5c8212013-06-07 22:09:53 +0000265
Zachary Turner97206d52017-05-12 04:51:55 +0000266bool Status::WasInterrupted() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 return (m_type == eErrorTypePOSIX && m_code == EINTR);
Jim Ingham4e5c8212013-06-07 22:09:53 +0000268}
Zachary Turner33aba3c2017-02-06 18:31:44 +0000269
Zachary Turner97206d52017-05-12 04:51:55 +0000270void llvm::format_provider<lldb_private::Status>::format(
271 const lldb_private::Status &error, llvm::raw_ostream &OS,
Zachary Turner33aba3c2017-02-06 18:31:44 +0000272 llvm::StringRef Options) {
273 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
274 Options);
275}