]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Utility/Status.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Utility / Status.cpp
1 //===-- Status.cpp -----------------------------------------------*- C++
2 //-*-===//
3 //
4 // 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
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Utility/Status.h"
11
12 #include "lldb/Utility/VASPrintf.h"
13 #include "lldb/lldb-defines.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/Errno.h"
18 #include "llvm/Support/FormatProviders.h"
19
20 #include <cerrno>
21 #include <cstdarg>
22 #include <string>
23 #include <system_error>
24
25 #ifdef __APPLE__
26 #include <mach/mach.h>
27 #endif
28
29 #ifdef _WIN32
30 #include <windows.h>
31 #endif
32 #include <stdint.h>
33
34 namespace llvm {
35 class raw_ostream;
36 }
37
38 using namespace lldb;
39 using namespace lldb_private;
40
41 Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
42
43 Status::Status(ValueType err, ErrorType type)
44     : m_code(err), m_type(type), m_string() {}
45
46 Status::Status(std::error_code EC)
47     : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric),
48       m_string(EC.message()) {}
49
50 Status::Status(const char *format, ...)
51     : 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);
57 }
58
59 const Status &Status::operator=(llvm::Error error) {
60   if (!error) {
61     Clear();
62     return *this;
63   }
64
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
78   if (error) {
79     SetErrorToGenericError();
80     SetErrorString(llvm::toString(std::move(error)));
81   }
82
83   return *this;
84 }
85
86 llvm::Error Status::ToError() const {
87   if (Success())
88     return llvm::Error::success();
89   if (m_type == ErrorType::eErrorTypePOSIX)
90     return llvm::errorCodeToError(
91         std::error_code(m_code, std::generic_category()));
92   return llvm::make_error<llvm::StringError>(AsCString(),
93                                              llvm::inconvertibleErrorCode());
94 }
95
96 // Assignment operator
97 const Status &Status::operator=(const Status &rhs) {
98   if (this != &rhs) {
99     m_code = rhs.m_code;
100     m_type = rhs.m_type;
101     m_string = rhs.m_string;
102   }
103   return *this;
104 }
105
106 Status::~Status() = default;
107
108 #ifdef _WIN32
109 static std::string RetrieveWin32ErrorString(uint32_t error_code) {
110   char *buffer = nullptr;
111   std::string message;
112   // Retrieve win32 system error.
113   if (::FormatMessageA(
114           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
115               FORMAT_MESSAGE_MAX_WIDTH_MASK,
116           NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
117           (LPSTR)&buffer, 0, NULL)) {
118     message.assign(buffer);
119     ::LocalFree(buffer);
120   }
121   return message;
122 }
123 #endif
124
125 // Get the error value as a NULL C string. The error string will be fetched and
126 // cached on demand. The cached error string value will remain until the error
127 // value is changed or cleared.
128 const char *Status::AsCString(const char *default_error_str) const {
129   if (Success())
130     return nullptr;
131
132   if (m_string.empty()) {
133     switch (m_type) {
134     case eErrorTypeMachKernel:
135 #if defined(__APPLE__)
136       if (const char *s = ::mach_error_string(m_code))
137         m_string.assign(s);
138 #endif
139       break;
140
141     case eErrorTypePOSIX:
142       m_string = llvm::sys::StrError(m_code);
143       break;
144
145     case eErrorTypeWin32:
146 #if defined(_WIN32)
147       m_string = RetrieveWin32ErrorString(m_code);
148 #endif
149       break;
150
151     default:
152       break;
153     }
154   }
155   if (m_string.empty()) {
156     if (default_error_str)
157       m_string.assign(default_error_str);
158     else
159       return nullptr; // User wanted a nullptr string back...
160   }
161   return m_string.c_str();
162 }
163
164 // Clear the error and any cached error string that it might contain.
165 void Status::Clear() {
166   m_code = 0;
167   m_type = eErrorTypeInvalid;
168   m_string.clear();
169 }
170
171 // Access the error value.
172 Status::ValueType Status::GetError() const { return m_code; }
173
174 // Access the error type.
175 ErrorType Status::GetType() const { return m_type; }
176
177 // Returns true if this object contains a value that describes an error or
178 // otherwise non-success result.
179 bool Status::Fail() const { return m_code != 0; }
180
181 // Set accessor for the error value to "err" and the type to
182 // "eErrorTypeMachKernel"
183 void Status::SetMachError(uint32_t err) {
184   m_code = err;
185   m_type = eErrorTypeMachKernel;
186   m_string.clear();
187 }
188
189 void Status::SetExpressionError(lldb::ExpressionResults result,
190                                 const char *mssg) {
191   m_code = result;
192   m_type = eErrorTypeExpression;
193   m_string = mssg;
194 }
195
196 int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
197                                          const char *format, ...) {
198   int length = 0;
199
200   if (format != nullptr && format[0]) {
201     va_list args;
202     va_start(args, format);
203     length = SetErrorStringWithVarArg(format, args);
204     va_end(args);
205   } else {
206     m_string.clear();
207   }
208   m_code = result;
209   m_type = eErrorTypeExpression;
210   return length;
211 }
212
213 // Set accessor for the error value and type.
214 void Status::SetError(ValueType err, ErrorType type) {
215   m_code = err;
216   m_type = type;
217   m_string.clear();
218 }
219
220 // Update the error value to be "errno" and update the type to be "POSIX".
221 void Status::SetErrorToErrno() {
222   m_code = errno;
223   m_type = eErrorTypePOSIX;
224   m_string.clear();
225 }
226
227 // Update the error value to be LLDB_GENERIC_ERROR and update the type to be
228 // "Generic".
229 void Status::SetErrorToGenericError() {
230   m_code = LLDB_GENERIC_ERROR;
231   m_type = eErrorTypeGeneric;
232   m_string.clear();
233 }
234
235 // Set accessor for the error string value for a specific error. This allows
236 // any string to be supplied as an error explanation. The error string value
237 // will remain until the error value is cleared or a new error value/type is
238 // assigned.
239 void Status::SetErrorString(llvm::StringRef err_str) {
240   if (!err_str.empty()) {
241     // If we have an error string, we should always at least have an error set
242     // to a generic value.
243     if (Success())
244       SetErrorToGenericError();
245   }
246   m_string = err_str;
247 }
248
249 /// Set the current error string to a formatted error string.
250 ///
251 /// \param format
252 ///     A printf style format string
253 int Status::SetErrorStringWithFormat(const char *format, ...) {
254   if (format != nullptr && format[0]) {
255     va_list args;
256     va_start(args, format);
257     int length = SetErrorStringWithVarArg(format, args);
258     va_end(args);
259     return length;
260   } else {
261     m_string.clear();
262   }
263   return 0;
264 }
265
266 int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
267   if (format != nullptr && format[0]) {
268     // If we have an error string, we should always at least have an error set
269     // to a generic value.
270     if (Success())
271       SetErrorToGenericError();
272
273     llvm::SmallString<1024> buf;
274     VASprintf(buf, format, args);
275     m_string = buf.str();
276     return buf.size();
277   } else {
278     m_string.clear();
279   }
280   return 0;
281 }
282
283 // Returns true if the error code in this object is considered a successful
284 // return value.
285 bool Status::Success() const { return m_code == 0; }
286
287 bool Status::WasInterrupted() const {
288   return (m_type == eErrorTypePOSIX && m_code == EINTR);
289 }
290
291 void llvm::format_provider<lldb_private::Status>::format(
292     const lldb_private::Status &error, llvm::raw_ostream &OS,
293     llvm::StringRef Options) {
294   llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
295                                                  Options);
296 }