blob: b74db72773dd4e4df13154e8424653fd8bec39d9 [file] [log] [blame]
Zachary Turner97206d52017-05-12 04:51:55 +00001//===-- Status.cpp -----------------------------------------------*- C++
2//-*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003//
Chandler Carruth2946cd72019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007//
8//===----------------------------------------------------------------------===//
9
Zachary Turner97206d52017-05-12 04:51:55 +000010#include "lldb/Utility/Status.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000011
12#include "lldb/Utility/VASPrintf.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000013#include "lldb/lldb-defines.h"
14#include "lldb/lldb-enumerations.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/StringRef.h"
Pavel Labath10c41f32017-06-06 14:06:17 +000017#include "llvm/Support/Errno.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000018#include "llvm/Support/FormatProviders.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000019
20#include <cerrno>
21#include <cstdarg>
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000022#include <string>
Zachary Turner4479ac12017-04-06 18:12:24 +000023#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
Aaron Smithc3d447f2018-10-19 18:58:24 +000029#ifdef _WIN32
30#include <windows.h>
31#endif
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000032#include <stdint.h>
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000033
Zachary Turner4479ac12017-04-06 18:12:24 +000034namespace llvm {
35class raw_ostream;
36}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038using namespace lldb;
39using namespace lldb_private;
40
Zachary Turner97206d52017-05-12 04:51:55 +000041Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
Greg Claytonca512b32011-01-14 04:54:56 +000042
Zachary Turner97206d52017-05-12 04:51:55 +000043Status::Status(ValueType err, ErrorType type)
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 : m_code(err), m_type(type), m_string() {}
Greg Claytonca512b32011-01-14 04:54:56 +000045
Zachary Turner97206d52017-05-12 04:51:55 +000046Status::Status(std::error_code EC)
Zachary Turner7d86ee52017-03-08 17:56:08 +000047 : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric),
48 m_string(EC.message()) {}
49
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)
Aaron Smithc3d447f2018-10-19 18:58:24 +000090 return llvm::errorCodeToError(
91 std::error_code(m_code, std::generic_category()));
Pavel Labatha24a3a32017-05-18 12:46:50 +000092 return llvm::make_error<llvm::StringError>(AsCString(),
93 llvm::inconvertibleErrorCode());
94}
95
Zachary Turner97206d52017-05-12 04:51:55 +000096Status::~Status() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
Aaron Smithc3d447f2018-10-19 18:58:24 +000098#ifdef _WIN32
99static std::string RetrieveWin32ErrorString(uint32_t error_code) {
100 char *buffer = nullptr;
101 std::string message;
102 // Retrieve win32 system error.
Alexandre Ganeabdad3ec2019-11-28 14:15:13 -0500103 // First, attempt to load a en-US message
Aaron Smithc3d447f2018-10-19 18:58:24 +0000104 if (::FormatMessageA(
105 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
106 FORMAT_MESSAGE_MAX_WIDTH_MASK,
Alexandre Ganeabdad3ec2019-11-28 14:15:13 -0500107 NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
Aaron Smithc3d447f2018-10-19 18:58:24 +0000108 (LPSTR)&buffer, 0, NULL)) {
109 message.assign(buffer);
110 ::LocalFree(buffer);
111 }
Alexandre Ganeabdad3ec2019-11-28 14:15:13 -0500112 // If the previous didn't work, use the default OS language
113 else if (::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
114 FORMAT_MESSAGE_FROM_SYSTEM |
115 FORMAT_MESSAGE_MAX_WIDTH_MASK,
116 NULL, error_code, 0, (LPSTR)&buffer, 0, NULL)) {
117 message.assign(buffer);
118 ::LocalFree(buffer);
119 }
Aaron Smithc3d447f2018-10-19 18:58:24 +0000120 return message;
121}
122#endif
123
Adrian Prantl05097242018-04-30 16:49:04 +0000124// Get the error value as a NULL C string. The error string will be fetched and
125// cached on demand. The cached error string value will remain until the error
126// value is changed or cleared.
Zachary Turner97206d52017-05-12 04:51:55 +0000127const char *Status::AsCString(const char *default_error_str) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 if (Success())
129 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 if (m_string.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 switch (m_type) {
133 case eErrorTypeMachKernel:
134#if defined(__APPLE__)
Pavel Labath10c41f32017-06-06 14:06:17 +0000135 if (const char *s = ::mach_error_string(m_code))
136 m_string.assign(s);
Eli Friedman6eb685c2010-06-10 23:45:58 +0000137#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 case eErrorTypePOSIX:
Pavel Labath10c41f32017-06-06 14:06:17 +0000141 m_string = llvm::sys::StrError(m_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143
Aaron Smithc3d447f2018-10-19 18:58:24 +0000144 case eErrorTypeWin32:
145#if defined(_WIN32)
146 m_string = RetrieveWin32ErrorString(m_code);
147#endif
148 break;
149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 default:
151 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 }
154 if (m_string.empty()) {
155 if (default_error_str)
156 m_string.assign(default_error_str);
157 else
158 return nullptr; // User wanted a nullptr string back...
159 }
160 return m_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161}
162
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163// Clear the error and any cached error string that it might contain.
Zachary Turner97206d52017-05-12 04:51:55 +0000164void Status::Clear() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 m_code = 0;
166 m_type = eErrorTypeInvalid;
167 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168}
169
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170// Access the error value.
Zachary Turner97206d52017-05-12 04:51:55 +0000171Status::ValueType Status::GetError() const { return m_code; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173// Access the error type.
Zachary Turner97206d52017-05-12 04:51:55 +0000174ErrorType Status::GetType() const { return m_type; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175
Adrian Prantl05097242018-04-30 16:49:04 +0000176// Returns true if this object contains a value that describes an error or
177// otherwise non-success result.
Zachary Turner97206d52017-05-12 04:51:55 +0000178bool Status::Fail() const { return m_code != 0; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179
Bruce Mitchener4ebdee02018-05-29 09:10:46 +0000180// Set accessor for the error value to "err" and the type to
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181// "eErrorTypeMachKernel"
Zachary Turner97206d52017-05-12 04:51:55 +0000182void Status::SetMachError(uint32_t err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 m_code = err;
184 m_type = eErrorTypeMachKernel;
185 m_string.clear();
186}
187
Zachary Turner97206d52017-05-12 04:51:55 +0000188void Status::SetExpressionError(lldb::ExpressionResults result,
189 const char *mssg) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 m_code = result;
191 m_type = eErrorTypeExpression;
192 m_string = mssg;
193}
194
Zachary Turner97206d52017-05-12 04:51:55 +0000195int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
196 const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 int length = 0;
198
199 if (format != nullptr && format[0]) {
200 va_list args;
201 va_start(args, format);
202 length = SetErrorStringWithVarArg(format, args);
203 va_end(args);
204 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 m_string.clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 }
207 m_code = result;
208 m_type = eErrorTypeExpression;
209 return length;
Jim Ingham1624a2d2014-05-05 02:26:40 +0000210}
211
Bruce Mitchener4ebdee02018-05-29 09:10:46 +0000212// Set accessor for the error value and type.
Zachary Turner97206d52017-05-12 04:51:55 +0000213void Status::SetError(ValueType err, ErrorType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 m_code = err;
215 m_type = type;
216 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}
218
Adrian Prantl05097242018-04-30 16:49:04 +0000219// Update the error value to be "errno" and update the type to be "POSIX".
Zachary Turner97206d52017-05-12 04:51:55 +0000220void Status::SetErrorToErrno() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 m_code = errno;
222 m_type = eErrorTypePOSIX;
223 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224}
225
Adrian Prantl05097242018-04-30 16:49:04 +0000226// Update the error value to be LLDB_GENERIC_ERROR and update the type to be
227// "Generic".
Zachary Turner97206d52017-05-12 04:51:55 +0000228void Status::SetErrorToGenericError() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 m_code = LLDB_GENERIC_ERROR;
230 m_type = eErrorTypeGeneric;
231 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232}
233
Adrian Prantl05097242018-04-30 16:49:04 +0000234// Set accessor for the error string value for a specific error. This allows
235// any string to be supplied as an error explanation. The error string value
236// will remain until the error value is cleared or a new error value/type is
237// assigned.
Zachary Turner97206d52017-05-12 04:51:55 +0000238void Status::SetErrorString(llvm::StringRef err_str) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000239 if (!err_str.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000240 // If we have an error string, we should always at least have an error set
241 // to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 if (Success())
243 SetErrorToGenericError();
Zachary Turnerc1564272016-11-16 21:15:24 +0000244 }
245 m_string = err_str;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248/// Set the current error string to a formatted error string.
249///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000250/// \param format
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251/// A printf style format string
Zachary Turner97206d52017-05-12 04:51:55 +0000252int Status::SetErrorStringWithFormat(const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 if (format != nullptr && format[0]) {
254 va_list args;
255 va_start(args, format);
256 int length = SetErrorStringWithVarArg(format, args);
257 va_end(args);
258 return length;
259 } else {
260 m_string.clear();
261 }
262 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263}
264
Zachary Turner97206d52017-05-12 04:51:55 +0000265int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 if (format != nullptr && format[0]) {
Adrian Prantl05097242018-04-30 16:49:04 +0000267 // If we have an error string, we should always at least have an error set
268 // to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 if (Success())
270 SetErrorToGenericError();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271
Zachary Turner24ae6292017-02-16 19:38:21 +0000272 llvm::SmallString<1024> buf;
273 VASprintf(buf, format, args);
Pavel Labatha272fa82017-02-17 10:19:46 +0000274 m_string = buf.str();
Zachary Turner24ae6292017-02-16 19:38:21 +0000275 return buf.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 } else {
277 m_string.clear();
278 }
279 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280}
281
Adrian Prantl05097242018-04-30 16:49:04 +0000282// Returns true if the error code in this object is considered a successful
283// return value.
Zachary Turner97206d52017-05-12 04:51:55 +0000284bool Status::Success() const { return m_code == 0; }
Jim Ingham4e5c8212013-06-07 22:09:53 +0000285
Zachary Turner97206d52017-05-12 04:51:55 +0000286bool Status::WasInterrupted() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 return (m_type == eErrorTypePOSIX && m_code == EINTR);
Jim Ingham4e5c8212013-06-07 22:09:53 +0000288}
Zachary Turner33aba3c2017-02-06 18:31:44 +0000289
Zachary Turner97206d52017-05-12 04:51:55 +0000290void llvm::format_provider<lldb_private::Status>::format(
291 const lldb_private::Status &error, llvm::raw_ostream &OS,
Zachary Turner33aba3c2017-02-06 18:31:44 +0000292 llvm::StringRef Options) {
293 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
294 Options);
295}