blob: e3c4284a8e8a5a5bc482485a7177ed63b4b6bfbb [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- Status.cpp --------------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turner97206d52017-05-12 04:51:55 +00009#include "lldb/Utility/Status.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000010
11#include "lldb/Utility/VASPrintf.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000012#include "lldb/lldb-defines.h"
13#include "lldb/lldb-enumerations.h"
14#include "llvm/ADT/SmallString.h"
15#include "llvm/ADT/StringRef.h"
Pavel Labath10c41f32017-06-06 14:06:17 +000016#include "llvm/Support/Errno.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000017#include "llvm/Support/FormatProviders.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000018
19#include <cerrno>
20#include <cstdarg>
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000021#include <string>
Zachary Turner4479ac12017-04-06 18:12:24 +000022#include <system_error>
23
Charles Davis322fc842013-08-27 06:13:56 +000024#ifdef __APPLE__
Charles Davis510938e2013-08-27 05:04:57 +000025#include <mach/mach.h>
Charles Davis322fc842013-08-27 06:13:56 +000026#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Aaron Smithc3d447f2018-10-19 18:58:24 +000028#ifdef _WIN32
29#include <windows.h>
30#endif
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000031#include <stdint.h>
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000032
Zachary Turner4479ac12017-04-06 18:12:24 +000033namespace llvm {
34class raw_ostream;
35}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037using namespace lldb;
38using namespace lldb_private;
39
Zachary Turner97206d52017-05-12 04:51:55 +000040Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
Greg Claytonca512b32011-01-14 04:54:56 +000041
Zachary Turner97206d52017-05-12 04:51:55 +000042Status::Status(ValueType err, ErrorType type)
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 : m_code(err), m_type(type), m_string() {}
Greg Claytonca512b32011-01-14 04:54:56 +000044
Pavel Labathf512b972020-04-23 15:55:39 +020045// This logic is confusing because c++ calls the traditional (posix) errno codes
46// "generic errors", while we use the term "generic" to mean completely
47// arbitrary (text-based) errors.
Zachary Turner97206d52017-05-12 04:51:55 +000048Status::Status(std::error_code EC)
Pavel Labathf512b972020-04-23 15:55:39 +020049 : m_code(EC.value()),
50 m_type(EC.category() == std::generic_category() ? eErrorTypePOSIX
51 : eErrorTypeGeneric),
Zachary Turner7d86ee52017-03-08 17:56:08 +000052 m_string(EC.message()) {}
53
Zachary Turner97206d52017-05-12 04:51:55 +000054Status::Status(const char *format, ...)
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 : m_code(0), m_type(eErrorTypeInvalid), m_string() {
56 va_list args;
57 va_start(args, format);
58 SetErrorToGenericError();
59 SetErrorStringWithVarArg(format, args);
60 va_end(args);
Enrico Granataf2bbf712011-07-15 02:26:42 +000061}
62
Pavel Labath3adc4082017-06-15 11:23:26 +000063const Status &Status::operator=(llvm::Error error) {
64 if (!error) {
65 Clear();
66 return *this;
67 }
Pavel Labatha24a3a32017-05-18 12:46:50 +000068
69 // if the error happens to be a errno error, preserve the error code
70 error = llvm::handleErrors(
71 std::move(error), [&](std::unique_ptr<llvm::ECError> e) -> llvm::Error {
72 std::error_code ec = e->convertToErrorCode();
73 if (ec.category() == std::generic_category()) {
74 m_code = ec.value();
75 m_type = ErrorType::eErrorTypePOSIX;
76 return llvm::Error::success();
77 }
78 return llvm::Error(std::move(e));
79 });
80
81 // Otherwise, just preserve the message
Pavel Labath3adc4082017-06-15 11:23:26 +000082 if (error) {
83 SetErrorToGenericError();
Pavel Labatha24a3a32017-05-18 12:46:50 +000084 SetErrorString(llvm::toString(std::move(error)));
Pavel Labath3adc4082017-06-15 11:23:26 +000085 }
86
87 return *this;
Pavel Labatha24a3a32017-05-18 12:46:50 +000088}
89
90llvm::Error Status::ToError() const {
91 if (Success())
92 return llvm::Error::success();
93 if (m_type == ErrorType::eErrorTypePOSIX)
Aaron Smithc3d447f2018-10-19 18:58:24 +000094 return llvm::errorCodeToError(
95 std::error_code(m_code, std::generic_category()));
Pavel Labatha24a3a32017-05-18 12:46:50 +000096 return llvm::make_error<llvm::StringError>(AsCString(),
97 llvm::inconvertibleErrorCode());
98}
99
Zachary Turner97206d52017-05-12 04:51:55 +0000100Status::~Status() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101
Aaron Smithc3d447f2018-10-19 18:58:24 +0000102#ifdef _WIN32
103static std::string RetrieveWin32ErrorString(uint32_t error_code) {
104 char *buffer = nullptr;
105 std::string message;
106 // Retrieve win32 system error.
Alexandre Ganeabdad3ec2019-11-28 14:15:13 -0500107 // First, attempt to load a en-US message
Aaron Smithc3d447f2018-10-19 18:58:24 +0000108 if (::FormatMessageA(
109 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
110 FORMAT_MESSAGE_MAX_WIDTH_MASK,
Alexandre Ganeabdad3ec2019-11-28 14:15:13 -0500111 NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
Aaron Smithc3d447f2018-10-19 18:58:24 +0000112 (LPSTR)&buffer, 0, NULL)) {
113 message.assign(buffer);
114 ::LocalFree(buffer);
115 }
Alexandre Ganeabdad3ec2019-11-28 14:15:13 -0500116 // If the previous didn't work, use the default OS language
117 else if (::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
118 FORMAT_MESSAGE_FROM_SYSTEM |
119 FORMAT_MESSAGE_MAX_WIDTH_MASK,
120 NULL, error_code, 0, (LPSTR)&buffer, 0, NULL)) {
121 message.assign(buffer);
122 ::LocalFree(buffer);
123 }
Aaron Smithc3d447f2018-10-19 18:58:24 +0000124 return message;
125}
126#endif
127
Adrian Prantl05097242018-04-30 16:49:04 +0000128// Get the error value as a NULL C string. The error string will be fetched and
129// cached on demand. The cached error string value will remain until the error
130// value is changed or cleared.
Zachary Turner97206d52017-05-12 04:51:55 +0000131const char *Status::AsCString(const char *default_error_str) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 if (Success())
133 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 if (m_string.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 switch (m_type) {
137 case eErrorTypeMachKernel:
138#if defined(__APPLE__)
Pavel Labath10c41f32017-06-06 14:06:17 +0000139 if (const char *s = ::mach_error_string(m_code))
140 m_string.assign(s);
Eli Friedman6eb685c2010-06-10 23:45:58 +0000141#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 case eErrorTypePOSIX:
Pavel Labath10c41f32017-06-06 14:06:17 +0000145 m_string = llvm::sys::StrError(m_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
Aaron Smithc3d447f2018-10-19 18:58:24 +0000148 case eErrorTypeWin32:
149#if defined(_WIN32)
150 m_string = RetrieveWin32ErrorString(m_code);
151#endif
152 break;
153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 default:
155 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 }
158 if (m_string.empty()) {
159 if (default_error_str)
160 m_string.assign(default_error_str);
161 else
162 return nullptr; // User wanted a nullptr string back...
163 }
164 return m_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167// Clear the error and any cached error string that it might contain.
Zachary Turner97206d52017-05-12 04:51:55 +0000168void Status::Clear() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 m_code = 0;
170 m_type = eErrorTypeInvalid;
171 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172}
173
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174// Access the error value.
Zachary Turner97206d52017-05-12 04:51:55 +0000175Status::ValueType Status::GetError() const { return m_code; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177// Access the error type.
Zachary Turner97206d52017-05-12 04:51:55 +0000178ErrorType Status::GetType() const { return m_type; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179
Adrian Prantl05097242018-04-30 16:49:04 +0000180// Returns true if this object contains a value that describes an error or
181// otherwise non-success result.
Zachary Turner97206d52017-05-12 04:51:55 +0000182bool Status::Fail() const { return m_code != 0; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183
Bruce Mitchener4ebdee02018-05-29 09:10:46 +0000184// Set accessor for the error value to "err" and the type to
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185// "eErrorTypeMachKernel"
Zachary Turner97206d52017-05-12 04:51:55 +0000186void Status::SetMachError(uint32_t err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 m_code = err;
188 m_type = eErrorTypeMachKernel;
189 m_string.clear();
190}
191
Zachary Turner97206d52017-05-12 04:51:55 +0000192void Status::SetExpressionError(lldb::ExpressionResults result,
193 const char *mssg) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 m_code = result;
195 m_type = eErrorTypeExpression;
196 m_string = mssg;
197}
198
Zachary Turner97206d52017-05-12 04:51:55 +0000199int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
200 const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 int length = 0;
202
203 if (format != nullptr && format[0]) {
204 va_list args;
205 va_start(args, format);
206 length = SetErrorStringWithVarArg(format, args);
207 va_end(args);
208 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 m_string.clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 }
211 m_code = result;
212 m_type = eErrorTypeExpression;
213 return length;
Jim Ingham1624a2d2014-05-05 02:26:40 +0000214}
215
Bruce Mitchener4ebdee02018-05-29 09:10:46 +0000216// Set accessor for the error value and type.
Zachary Turner97206d52017-05-12 04:51:55 +0000217void Status::SetError(ValueType err, ErrorType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 m_code = err;
219 m_type = type;
220 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221}
222
Adrian Prantl05097242018-04-30 16:49:04 +0000223// Update the error value to be "errno" and update the type to be "POSIX".
Zachary Turner97206d52017-05-12 04:51:55 +0000224void Status::SetErrorToErrno() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 m_code = errno;
226 m_type = eErrorTypePOSIX;
227 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228}
229
Adrian Prantl05097242018-04-30 16:49:04 +0000230// Update the error value to be LLDB_GENERIC_ERROR and update the type to be
231// "Generic".
Zachary Turner97206d52017-05-12 04:51:55 +0000232void Status::SetErrorToGenericError() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 m_code = LLDB_GENERIC_ERROR;
234 m_type = eErrorTypeGeneric;
235 m_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236}
237
Adrian Prantl05097242018-04-30 16:49:04 +0000238// Set accessor for the error string value for a specific error. This allows
239// any string to be supplied as an error explanation. The error string value
240// will remain until the error value is cleared or a new error value/type is
241// assigned.
Zachary Turner97206d52017-05-12 04:51:55 +0000242void Status::SetErrorString(llvm::StringRef err_str) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000243 if (!err_str.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000244 // If we have an error string, we should always at least have an error set
245 // to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 if (Success())
247 SetErrorToGenericError();
Zachary Turnerc1564272016-11-16 21:15:24 +0000248 }
Benjamin Krameradcd0262020-01-28 20:23:46 +0100249 m_string = std::string(err_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250}
251
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252/// Set the current error string to a formatted error string.
253///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000254/// \param format
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255/// A printf style format string
Zachary Turner97206d52017-05-12 04:51:55 +0000256int Status::SetErrorStringWithFormat(const char *format, ...) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 if (format != nullptr && format[0]) {
258 va_list args;
259 va_start(args, format);
260 int length = SetErrorStringWithVarArg(format, args);
261 va_end(args);
262 return length;
263 } else {
264 m_string.clear();
265 }
266 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267}
268
Zachary Turner97206d52017-05-12 04:51:55 +0000269int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 if (format != nullptr && format[0]) {
Adrian Prantl05097242018-04-30 16:49:04 +0000271 // If we have an error string, we should always at least have an error set
272 // to a generic value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 if (Success())
274 SetErrorToGenericError();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275
Zachary Turner24ae6292017-02-16 19:38:21 +0000276 llvm::SmallString<1024> buf;
277 VASprintf(buf, format, args);
Benjamin Krameradcd0262020-01-28 20:23:46 +0100278 m_string = std::string(buf.str());
Zachary Turner24ae6292017-02-16 19:38:21 +0000279 return buf.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 } else {
281 m_string.clear();
282 }
283 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284}
285
Adrian Prantl05097242018-04-30 16:49:04 +0000286// Returns true if the error code in this object is considered a successful
287// return value.
Zachary Turner97206d52017-05-12 04:51:55 +0000288bool Status::Success() const { return m_code == 0; }
Jim Ingham4e5c8212013-06-07 22:09:53 +0000289
Zachary Turner97206d52017-05-12 04:51:55 +0000290bool Status::WasInterrupted() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 return (m_type == eErrorTypePOSIX && m_code == EINTR);
Jim Ingham4e5c8212013-06-07 22:09:53 +0000292}
Zachary Turner33aba3c2017-02-06 18:31:44 +0000293
Zachary Turner97206d52017-05-12 04:51:55 +0000294void llvm::format_provider<lldb_private::Status>::format(
295 const lldb_private::Status &error, llvm::raw_ostream &OS,
Zachary Turner33aba3c2017-02-06 18:31:44 +0000296 llvm::StringRef Options) {
297 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
298 Options);
299}