xref: /openbsd/gnu/llvm/lldb/source/Utility/Status.cpp (revision f6aab3d8)
1dda28197Spatrick //===-- Status.cpp --------------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Utility/Status.h"
10061da546Spatrick 
11061da546Spatrick #include "lldb/Utility/VASPrintf.h"
12061da546Spatrick #include "lldb/lldb-defines.h"
13061da546Spatrick #include "lldb/lldb-enumerations.h"
14061da546Spatrick #include "llvm/ADT/SmallString.h"
15061da546Spatrick #include "llvm/ADT/StringRef.h"
16061da546Spatrick #include "llvm/Support/Errno.h"
17061da546Spatrick #include "llvm/Support/FormatProviders.h"
18061da546Spatrick 
19061da546Spatrick #include <cerrno>
20061da546Spatrick #include <cstdarg>
21061da546Spatrick #include <string>
22061da546Spatrick #include <system_error>
23061da546Spatrick 
24061da546Spatrick #ifdef __APPLE__
25061da546Spatrick #include <mach/mach.h>
26061da546Spatrick #endif
27061da546Spatrick 
28061da546Spatrick #ifdef _WIN32
29061da546Spatrick #include <windows.h>
30061da546Spatrick #endif
31be691f3bSpatrick #include <cstdint>
32061da546Spatrick 
33061da546Spatrick namespace llvm {
34061da546Spatrick class raw_ostream;
35061da546Spatrick }
36061da546Spatrick 
37061da546Spatrick using namespace lldb;
38061da546Spatrick using namespace lldb_private;
39061da546Spatrick 
Status()40be691f3bSpatrick Status::Status() : m_string() {}
41061da546Spatrick 
Status(ValueType err,ErrorType type)42061da546Spatrick Status::Status(ValueType err, ErrorType type)
43061da546Spatrick     : m_code(err), m_type(type), m_string() {}
44061da546Spatrick 
45dda28197Spatrick // This logic is confusing because c++ calls the traditional (posix) errno codes
46dda28197Spatrick // "generic errors", while we use the term "generic" to mean completely
47dda28197Spatrick // arbitrary (text-based) errors.
Status(std::error_code EC)48061da546Spatrick Status::Status(std::error_code EC)
49dda28197Spatrick     : m_code(EC.value()),
50dda28197Spatrick       m_type(EC.category() == std::generic_category() ? eErrorTypePOSIX
51dda28197Spatrick                                                       : eErrorTypeGeneric),
52061da546Spatrick       m_string(EC.message()) {}
53061da546Spatrick 
Status(const char * format,...)54*f6aab3d8Srobert Status::Status(const char *format, ...) : m_string() {
55061da546Spatrick   va_list args;
56061da546Spatrick   va_start(args, format);
57061da546Spatrick   SetErrorToGenericError();
58061da546Spatrick   SetErrorStringWithVarArg(format, args);
59061da546Spatrick   va_end(args);
60061da546Spatrick }
61061da546Spatrick 
operator =(llvm::Error error)62061da546Spatrick const Status &Status::operator=(llvm::Error error) {
63061da546Spatrick   if (!error) {
64061da546Spatrick     Clear();
65061da546Spatrick     return *this;
66061da546Spatrick   }
67061da546Spatrick 
68061da546Spatrick   // if the error happens to be a errno error, preserve the error code
69061da546Spatrick   error = llvm::handleErrors(
70061da546Spatrick       std::move(error), [&](std::unique_ptr<llvm::ECError> e) -> llvm::Error {
71061da546Spatrick         std::error_code ec = e->convertToErrorCode();
72061da546Spatrick         if (ec.category() == std::generic_category()) {
73061da546Spatrick           m_code = ec.value();
74061da546Spatrick           m_type = ErrorType::eErrorTypePOSIX;
75061da546Spatrick           return llvm::Error::success();
76061da546Spatrick         }
77061da546Spatrick         return llvm::Error(std::move(e));
78061da546Spatrick       });
79061da546Spatrick 
80061da546Spatrick   // Otherwise, just preserve the message
81061da546Spatrick   if (error) {
82061da546Spatrick     SetErrorToGenericError();
83061da546Spatrick     SetErrorString(llvm::toString(std::move(error)));
84061da546Spatrick   }
85061da546Spatrick 
86061da546Spatrick   return *this;
87061da546Spatrick }
88061da546Spatrick 
ToError() const89061da546Spatrick llvm::Error Status::ToError() const {
90061da546Spatrick   if (Success())
91061da546Spatrick     return llvm::Error::success();
92061da546Spatrick   if (m_type == ErrorType::eErrorTypePOSIX)
93061da546Spatrick     return llvm::errorCodeToError(
94061da546Spatrick         std::error_code(m_code, std::generic_category()));
95061da546Spatrick   return llvm::make_error<llvm::StringError>(AsCString(),
96061da546Spatrick                                              llvm::inconvertibleErrorCode());
97061da546Spatrick }
98061da546Spatrick 
99061da546Spatrick Status::~Status() = default;
100061da546Spatrick 
101061da546Spatrick #ifdef _WIN32
RetrieveWin32ErrorString(uint32_t error_code)102061da546Spatrick static std::string RetrieveWin32ErrorString(uint32_t error_code) {
103061da546Spatrick   char *buffer = nullptr;
104061da546Spatrick   std::string message;
105061da546Spatrick   // Retrieve win32 system error.
106061da546Spatrick   // First, attempt to load a en-US message
107061da546Spatrick   if (::FormatMessageA(
108061da546Spatrick           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
109061da546Spatrick               FORMAT_MESSAGE_MAX_WIDTH_MASK,
110061da546Spatrick           NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
111061da546Spatrick           (LPSTR)&buffer, 0, NULL)) {
112061da546Spatrick     message.assign(buffer);
113061da546Spatrick     ::LocalFree(buffer);
114061da546Spatrick   }
115061da546Spatrick   // If the previous didn't work, use the default OS language
116061da546Spatrick   else if (::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
117061da546Spatrick                                 FORMAT_MESSAGE_FROM_SYSTEM |
118061da546Spatrick                                 FORMAT_MESSAGE_MAX_WIDTH_MASK,
119061da546Spatrick                             NULL, error_code, 0, (LPSTR)&buffer, 0, NULL)) {
120061da546Spatrick     message.assign(buffer);
121061da546Spatrick     ::LocalFree(buffer);
122061da546Spatrick   }
123061da546Spatrick   return message;
124061da546Spatrick }
125061da546Spatrick #endif
126061da546Spatrick 
127061da546Spatrick // Get the error value as a NULL C string. The error string will be fetched and
128061da546Spatrick // cached on demand. The cached error string value will remain until the error
129061da546Spatrick // value is changed or cleared.
AsCString(const char * default_error_str) const130061da546Spatrick const char *Status::AsCString(const char *default_error_str) const {
131061da546Spatrick   if (Success())
132061da546Spatrick     return nullptr;
133061da546Spatrick 
134061da546Spatrick   if (m_string.empty()) {
135061da546Spatrick     switch (m_type) {
136061da546Spatrick     case eErrorTypeMachKernel:
137061da546Spatrick #if defined(__APPLE__)
138061da546Spatrick       if (const char *s = ::mach_error_string(m_code))
139061da546Spatrick         m_string.assign(s);
140061da546Spatrick #endif
141061da546Spatrick       break;
142061da546Spatrick 
143061da546Spatrick     case eErrorTypePOSIX:
144061da546Spatrick       m_string = llvm::sys::StrError(m_code);
145061da546Spatrick       break;
146061da546Spatrick 
147061da546Spatrick     case eErrorTypeWin32:
148061da546Spatrick #if defined(_WIN32)
149061da546Spatrick       m_string = RetrieveWin32ErrorString(m_code);
150061da546Spatrick #endif
151061da546Spatrick       break;
152061da546Spatrick 
153061da546Spatrick     default:
154061da546Spatrick       break;
155061da546Spatrick     }
156061da546Spatrick   }
157061da546Spatrick   if (m_string.empty()) {
158061da546Spatrick     if (default_error_str)
159061da546Spatrick       m_string.assign(default_error_str);
160061da546Spatrick     else
161061da546Spatrick       return nullptr; // User wanted a nullptr string back...
162061da546Spatrick   }
163061da546Spatrick   return m_string.c_str();
164061da546Spatrick }
165061da546Spatrick 
166061da546Spatrick // Clear the error and any cached error string that it might contain.
Clear()167061da546Spatrick void Status::Clear() {
168061da546Spatrick   m_code = 0;
169061da546Spatrick   m_type = eErrorTypeInvalid;
170061da546Spatrick   m_string.clear();
171061da546Spatrick }
172061da546Spatrick 
173061da546Spatrick // Access the error value.
GetError() const174061da546Spatrick Status::ValueType Status::GetError() const { return m_code; }
175061da546Spatrick 
176061da546Spatrick // Access the error type.
GetType() const177061da546Spatrick ErrorType Status::GetType() const { return m_type; }
178061da546Spatrick 
179061da546Spatrick // Returns true if this object contains a value that describes an error or
180061da546Spatrick // otherwise non-success result.
Fail() const181061da546Spatrick bool Status::Fail() const { return m_code != 0; }
182061da546Spatrick 
183061da546Spatrick // Set accessor for the error value to "err" and the type to
184061da546Spatrick // "eErrorTypeMachKernel"
SetMachError(uint32_t err)185061da546Spatrick void Status::SetMachError(uint32_t err) {
186061da546Spatrick   m_code = err;
187061da546Spatrick   m_type = eErrorTypeMachKernel;
188061da546Spatrick   m_string.clear();
189061da546Spatrick }
190061da546Spatrick 
SetExpressionError(lldb::ExpressionResults result,const char * mssg)191061da546Spatrick void Status::SetExpressionError(lldb::ExpressionResults result,
192061da546Spatrick                                 const char *mssg) {
193061da546Spatrick   m_code = result;
194061da546Spatrick   m_type = eErrorTypeExpression;
195061da546Spatrick   m_string = mssg;
196061da546Spatrick }
197061da546Spatrick 
SetExpressionErrorWithFormat(lldb::ExpressionResults result,const char * format,...)198061da546Spatrick int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
199061da546Spatrick                                          const char *format, ...) {
200061da546Spatrick   int length = 0;
201061da546Spatrick 
202061da546Spatrick   if (format != nullptr && format[0]) {
203061da546Spatrick     va_list args;
204061da546Spatrick     va_start(args, format);
205061da546Spatrick     length = SetErrorStringWithVarArg(format, args);
206061da546Spatrick     va_end(args);
207061da546Spatrick   } else {
208061da546Spatrick     m_string.clear();
209061da546Spatrick   }
210061da546Spatrick   m_code = result;
211061da546Spatrick   m_type = eErrorTypeExpression;
212061da546Spatrick   return length;
213061da546Spatrick }
214061da546Spatrick 
215061da546Spatrick // Set accessor for the error value and type.
SetError(ValueType err,ErrorType type)216061da546Spatrick void Status::SetError(ValueType err, ErrorType type) {
217061da546Spatrick   m_code = err;
218061da546Spatrick   m_type = type;
219061da546Spatrick   m_string.clear();
220061da546Spatrick }
221061da546Spatrick 
222061da546Spatrick // Update the error value to be "errno" and update the type to be "POSIX".
SetErrorToErrno()223061da546Spatrick void Status::SetErrorToErrno() {
224061da546Spatrick   m_code = errno;
225061da546Spatrick   m_type = eErrorTypePOSIX;
226061da546Spatrick   m_string.clear();
227061da546Spatrick }
228061da546Spatrick 
229061da546Spatrick // Update the error value to be LLDB_GENERIC_ERROR and update the type to be
230061da546Spatrick // "Generic".
SetErrorToGenericError()231061da546Spatrick void Status::SetErrorToGenericError() {
232061da546Spatrick   m_code = LLDB_GENERIC_ERROR;
233061da546Spatrick   m_type = eErrorTypeGeneric;
234061da546Spatrick   m_string.clear();
235061da546Spatrick }
236061da546Spatrick 
237061da546Spatrick // Set accessor for the error string value for a specific error. This allows
238061da546Spatrick // any string to be supplied as an error explanation. The error string value
239061da546Spatrick // will remain until the error value is cleared or a new error value/type is
240061da546Spatrick // assigned.
SetErrorString(llvm::StringRef err_str)241061da546Spatrick void Status::SetErrorString(llvm::StringRef err_str) {
242061da546Spatrick   if (!err_str.empty()) {
243061da546Spatrick     // If we have an error string, we should always at least have an error set
244061da546Spatrick     // to a generic value.
245061da546Spatrick     if (Success())
246061da546Spatrick       SetErrorToGenericError();
247061da546Spatrick   }
248dda28197Spatrick   m_string = std::string(err_str);
249061da546Spatrick }
250061da546Spatrick 
251061da546Spatrick /// Set the current error string to a formatted error string.
252061da546Spatrick ///
253061da546Spatrick /// \param format
254061da546Spatrick ///     A printf style format string
SetErrorStringWithFormat(const char * format,...)255061da546Spatrick int Status::SetErrorStringWithFormat(const char *format, ...) {
256061da546Spatrick   if (format != nullptr && format[0]) {
257061da546Spatrick     va_list args;
258061da546Spatrick     va_start(args, format);
259061da546Spatrick     int length = SetErrorStringWithVarArg(format, args);
260061da546Spatrick     va_end(args);
261061da546Spatrick     return length;
262061da546Spatrick   } else {
263061da546Spatrick     m_string.clear();
264061da546Spatrick   }
265061da546Spatrick   return 0;
266061da546Spatrick }
267061da546Spatrick 
SetErrorStringWithVarArg(const char * format,va_list args)268061da546Spatrick int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
269061da546Spatrick   if (format != nullptr && format[0]) {
270061da546Spatrick     // If we have an error string, we should always at least have an error set
271061da546Spatrick     // to a generic value.
272061da546Spatrick     if (Success())
273061da546Spatrick       SetErrorToGenericError();
274061da546Spatrick 
275061da546Spatrick     llvm::SmallString<1024> buf;
276061da546Spatrick     VASprintf(buf, format, args);
277dda28197Spatrick     m_string = std::string(buf.str());
278061da546Spatrick     return buf.size();
279061da546Spatrick   } else {
280061da546Spatrick     m_string.clear();
281061da546Spatrick   }
282061da546Spatrick   return 0;
283061da546Spatrick }
284061da546Spatrick 
285061da546Spatrick // Returns true if the error code in this object is considered a successful
286061da546Spatrick // return value.
Success() const287061da546Spatrick bool Status::Success() const { return m_code == 0; }
288061da546Spatrick 
format(const lldb_private::Status & error,llvm::raw_ostream & OS,llvm::StringRef Options)289061da546Spatrick void llvm::format_provider<lldb_private::Status>::format(
290061da546Spatrick     const lldb_private::Status &error, llvm::raw_ostream &OS,
291061da546Spatrick     llvm::StringRef Options) {
292061da546Spatrick   llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
293061da546Spatrick                                                  Options);
294061da546Spatrick }
295