blob: 3b5094d64b75a082d6878eb7f4fbf71c837a844e [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.
103 if (::FormatMessageA(
104 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
105 FORMAT_MESSAGE_MAX_WIDTH_MASK,
106 NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
107 (LPSTR)&buffer, 0, NULL)) {
108 message.assign(buffer);
109 ::LocalFree(buffer);
110 }
111 return message;
112}
113#endif
114
Adrian Prantl05097242018-04-30 16:49:04 +0000115// Get the error value as a NULL C string. The error string will be fetched and
116// cached on demand. The cached error string value will remain until the error
117// value is changed or cleared.
Zachary Turner97206d52017-05-12 04:51:55 +0000118const char *Status::AsCString(const char *default_error_str) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 if (Success())
120 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 if (m_string.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 switch (m_type) {
124 case eErrorTypeMachKernel:
125#if defined(__APPLE__)
Pavel Labath10c41f32017-06-06 14:06:17 +0000126 if (const char *s = ::mach_error_string(m_code))
127 m_string.assign(s);
Eli Friedman6eb685c2010-06-10 23:45:58 +0000128#endif
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 case eErrorTypePOSIX:
Pavel Labath10c41f32017-06-06 14:06:17 +0000132 m_string = llvm::sys::StrError(m_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134
Aaron Smithc3d447f2018-10-19 18:58:24 +0000135 case eErrorTypeWin32:
136#if defined(_WIN32)
137 m_string = RetrieveWin32ErrorString(m_code);
138#endif
139 break;
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 default:
142 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 }
145 if (m_string.empty()) {
146 if (default_error_str)
147 m_string.assign(default_error_str);
148 else
149 return nullptr; // User wanted a nullptr string back...
150 }
151 return m_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152}
153
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154// Clear the error and any cached error string that it might contain.
Zachary Turner97206d52017-05-12 04:51:55 +0000155void Status::Clear() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 m_code = 0;
157 m_type = eErrorTypeInvalid;
158 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159}
160
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161// Access the error value.
Zachary Turner97206d52017-05-12 04:51:55 +0000162Status::ValueType Status::GetError() const { return m_code; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164// Access the error type.
Zachary Turner97206d52017-05-12 04:51:55 +0000165ErrorType Status::GetType() const { return m_type; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166
Adrian Prantl05097242018-04-30 16:49:04 +0000167// Returns true if this object contains a value that describes an error or
168// otherwise non-success result.
Zachary Turner97206d52017-05-12 04:51:55 +0000169bool Status::Fail() const { return m_code != 0; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170
Bruce Mitchener4ebdee02018-05-29 09:10:46 +0000171// Set accessor for the error value to "err" and the type to
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172// "eErrorTypeMachKernel"
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
Bruce Mitchener4ebdee02018-05-29 09:10:46 +0000203// Set accessor for the error value and type.
Zachary Turner97206d52017-05-12 04:51:55 +0000204void Status::SetError(ValueType err, ErrorType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 m_code = err;
206 m_type = type;
207 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208}
209
Adrian Prantl05097242018-04-30 16:49:04 +0000210// Update the error value to be "errno" and update the type to be "POSIX".
Zachary Turner97206d52017-05-12 04:51:55 +0000211void Status::SetErrorToErrno() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 m_code = errno;
213 m_type = eErrorTypePOSIX;
214 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215}
216
Adrian Prantl05097242018-04-30 16:49:04 +0000217// Update the error value to be LLDB_GENERIC_ERROR and update the type to be
218// "Generic".
Zachary Turner97206d52017-05-12 04:51:55 +0000219void Status::SetErrorToGenericError() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 m_code = LLDB_GENERIC_ERROR;
221 m_type = eErrorTypeGeneric;
222 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223}
224
Adrian Prantl05097242018-04-30 16:49:04 +0000225// Set accessor for the error string value for a specific error. This allows
226// any string to be supplied as an error explanation. The error string value
227// will remain until the error value is cleared or a new error value/type is
228// assigned.
Zachary Turner97206d52017-05-12 04:51:55 +0000229void Status::SetErrorString(llvm::StringRef err_str) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000230 if (!err_str.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000231 // If we have an error string, we should always at least have an error set
232 // to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 if (Success())
234 SetErrorToGenericError();
Zachary Turnerc1564272016-11-16 21:15:24 +0000235 }
236 m_string = err_str;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237}
238
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239/// Set the current error string to a formatted error string.
240///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000241/// \param format
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242/// A printf style format string
Zachary Turner97206d52017-05-12 04:51:55 +0000243int Status::SetErrorStringWithFormat(const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if (format != nullptr && format[0]) {
245 va_list args;
246 va_start(args, format);
247 int length = SetErrorStringWithVarArg(format, args);
248 va_end(args);
249 return length;
250 } else {
251 m_string.clear();
252 }
253 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254}
255
Zachary Turner97206d52017-05-12 04:51:55 +0000256int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 if (format != nullptr && format[0]) {
Adrian Prantl05097242018-04-30 16:49:04 +0000258 // If we have an error string, we should always at least have an error set
259 // to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 if (Success())
261 SetErrorToGenericError();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262
Zachary Turner24ae6292017-02-16 19:38:21 +0000263 llvm::SmallString<1024> buf;
264 VASprintf(buf, format, args);
Pavel Labatha272fa82017-02-17 10:19:46 +0000265 m_string = buf.str();
Zachary Turner24ae6292017-02-16 19:38:21 +0000266 return buf.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 } else {
268 m_string.clear();
269 }
270 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271}
272
Adrian Prantl05097242018-04-30 16:49:04 +0000273// Returns true if the error code in this object is considered a successful
274// return value.
Zachary Turner97206d52017-05-12 04:51:55 +0000275bool Status::Success() const { return m_code == 0; }
Jim Ingham4e5c8212013-06-07 22:09:53 +0000276
Zachary Turner97206d52017-05-12 04:51:55 +0000277bool Status::WasInterrupted() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 return (m_type == eErrorTypePOSIX && m_code == EINTR);
Jim Ingham4e5c8212013-06-07 22:09:53 +0000279}
Zachary Turner33aba3c2017-02-06 18:31:44 +0000280
Zachary Turner97206d52017-05-12 04:51:55 +0000281void llvm::format_provider<lldb_private::Status>::format(
282 const lldb_private::Status &error, llvm::raw_ostream &OS,
Zachary Turner33aba3c2017-02-06 18:31:44 +0000283 llvm::StringRef Options) {
284 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
285 Options);
286}