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