LLDB  mainline
Status.cpp
Go to the documentation of this file.
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 
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 Status &rhs) = default;
51 
52 Status::Status(const char *format, ...)
54  va_list args;
55  va_start(args, format);
57  SetErrorStringWithVarArg(format, args);
58  va_end(args);
59 }
60 
62  if (!error) {
63  Clear();
64  return *this;
65  }
66 
67  // if the error happens to be a errno error, preserve the error code
68  error = llvm::handleErrors(
69  std::move(error), [&](std::unique_ptr<llvm::ECError> e) -> llvm::Error {
70  std::error_code ec = e->convertToErrorCode();
71  if (ec.category() == std::generic_category()) {
72  m_code = ec.value();
74  return llvm::Error::success();
75  }
76  return llvm::Error(std::move(e));
77  });
78 
79  // Otherwise, just preserve the message
80  if (error) {
82  SetErrorString(llvm::toString(std::move(error)));
83  }
84 
85  return *this;
86 }
87 
89  if (Success())
90  return llvm::Error::success();
92  return llvm::errorCodeToError(
93  std::error_code(m_code, std::generic_category()));
94  return llvm::make_error<llvm::StringError>(AsCString(),
95  llvm::inconvertibleErrorCode());
96 }
97 
98 // Assignment operator
99 const Status &Status::operator=(const Status &rhs) {
100  if (this != &rhs) {
101  m_code = rhs.m_code;
102  m_type = rhs.m_type;
103  m_string = rhs.m_string;
104  }
105  return *this;
106 }
107 
108 Status::~Status() = default;
109 
110 #ifdef _WIN32
111 static std::string RetrieveWin32ErrorString(uint32_t error_code) {
112  char *buffer = nullptr;
113  std::string message;
114  // Retrieve win32 system error.
115  if (::FormatMessageA(
116  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
117  FORMAT_MESSAGE_MAX_WIDTH_MASK,
118  NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
119  (LPSTR)&buffer, 0, NULL)) {
120  message.assign(buffer);
121  ::LocalFree(buffer);
122  }
123  return message;
124 }
125 #endif
126 
127 // Get the error value as a NULL C string. The error string will be fetched and
128 // cached on demand. The cached error string value will remain until the error
129 // value is changed or cleared.
130 const char *Status::AsCString(const char *default_error_str) const {
131  if (Success())
132  return nullptr;
133 
134  if (m_string.empty()) {
135  switch (m_type) {
137 #if defined(__APPLE__)
138  if (const char *s = ::mach_error_string(m_code))
139  m_string.assign(s);
140 #endif
141  break;
142 
143  case eErrorTypePOSIX:
144  m_string = llvm::sys::StrError(m_code);
145  break;
146 
147  case eErrorTypeWin32:
148 #if defined(_WIN32)
149  m_string = RetrieveWin32ErrorString(m_code);
150 #endif
151  break;
152 
153  default:
154  break;
155  }
156  }
157  if (m_string.empty()) {
158  if (default_error_str)
159  m_string.assign(default_error_str);
160  else
161  return nullptr; // User wanted a nullptr string back...
162  }
163  return m_string.c_str();
164 }
165 
166 // Clear the error and any cached error string that it might contain.
168  m_code = 0;
170  m_string.clear();
171 }
172 
173 // Access the error value.
175 
176 // Access the error type.
177 ErrorType Status::GetType() const { return m_type; }
178 
179 // Returns true if this object contains a value that describes an error or
180 // otherwise non-success result.
181 bool Status::Fail() const { return m_code != 0; }
182 
183 // Set accessor for the error value to "err" and the type to
184 // "eErrorTypeMachKernel"
186  m_code = err;
188  m_string.clear();
189 }
190 
192  const char *mssg) {
193  m_code = result;
195  m_string = mssg;
196 }
197 
199  const char *format, ...) {
200  int length = 0;
201 
202  if (format != nullptr && format[0]) {
203  va_list args;
204  va_start(args, format);
205  length = SetErrorStringWithVarArg(format, args);
206  va_end(args);
207  } else {
208  m_string.clear();
209  }
210  m_code = result;
212  return length;
213 }
214 
215 // Set accessor for the error value and type.
217  m_code = err;
218  m_type = type;
219  m_string.clear();
220 }
221 
222 // Update the error value to be "errno" and update the type to be "POSIX".
224  m_code = errno;
226  m_string.clear();
227 }
228 
229 // Update the error value to be LLDB_GENERIC_ERROR and update the type to be
230 // "Generic".
234  m_string.clear();
235 }
236 
237 // Set accessor for the error string value for a specific error. This allows
238 // any string to be supplied as an error explanation. The error string value
239 // will remain until the error value is cleared or a new error value/type is
240 // assigned.
241 void Status::SetErrorString(llvm::StringRef err_str) {
242  if (!err_str.empty()) {
243  // If we have an error string, we should always at least have an error set
244  // to a generic value.
245  if (Success())
247  }
248  m_string = err_str;
249 }
250 
251 /// Set the current error string to a formatted error string.
252 ///
253 /// \param format
254 /// A printf style format string
255 int Status::SetErrorStringWithFormat(const char *format, ...) {
256  if (format != nullptr && format[0]) {
257  va_list args;
258  va_start(args, format);
259  int length = SetErrorStringWithVarArg(format, args);
260  va_end(args);
261  return length;
262  } else {
263  m_string.clear();
264  }
265  return 0;
266 }
267 
268 int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
269  if (format != nullptr && format[0]) {
270  // If we have an error string, we should always at least have an error set
271  // to a generic value.
272  if (Success())
274 
275  llvm::SmallString<1024> buf;
276  VASprintf(buf, format, args);
277  m_string = buf.str();
278  return buf.size();
279  } else {
280  m_string.clear();
281  }
282  return 0;
283 }
284 
285 // Returns true if the error code in this object is considered a successful
286 // return value.
287 bool Status::Success() const { return m_code == 0; }
288 
290  return (m_type == eErrorTypePOSIX && m_code == EINTR);
291 }
292 
294  const lldb_private::Status &error, llvm::raw_ostream &OS,
295  llvm::StringRef Options) {
296  llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
297  Options);
298 }
llvm::Error ToError() const
Definition: Status.cpp:88
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
int int SetErrorStringWithVarArg(const char *format, va_list args)
Definition: Status.cpp:268
Definition: Debugger.h:71
These are from the ExpressionResults enum.
bool WasInterrupted() const
Test for a failure due to a generic interrupt.
Definition: Status.cpp:289
void SetMachError(uint32_t err)
Set accessor from a kern_return_t.
Definition: Status.cpp:185
int SetExpressionErrorWithFormat(lldb::ExpressionResults, const char *format,...) __attribute__((format(printf
Definition: Status.cpp:198
llvm::Error Error
std::string m_string
A string representation of the error code.
Definition: Status.h:213
POSIX error codes.
void SetExpressionError(lldb::ExpressionResults, const char *mssg)
Definition: Status.cpp:191
void SetErrorToErrno()
Set the current error to errno.
Definition: Status.cpp:223
Standard Win32 error codes.
llvm::StringRef toString(Record::Kind K)
void SetErrorToGenericError()
Set the current error to a generic error.
Definition: Status.cpp:231
void Clear()
Clear the object state.
Definition: Status.cpp:167
Mach kernel error codes.
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
Status()
Default constructor.
Definition: Status.cpp:41
bool Success() const
Test for success condition.
Definition: Status.cpp:287
Generic errors that can be any value.
bool VASprintf(llvm::SmallVectorImpl< char > &buf, const char *fmt, va_list args)
Definition: VASprintf.cpp:19
const Status & operator=(const Status &rhs)
Assignment operator.
Definition: Status.cpp:99
static void format(const lldb_private::Status &error, llvm::raw_ostream &OS, llvm::StringRef Options)
Definition: Status.cpp:293
lldb::ErrorType GetType() const
Access the error type.
Definition: Status.cpp:177
A command line option parsing protocol class.
Definition: Options.h:62
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
int void SetError(ValueType err, lldb::ErrorType type)
Set accesssor with an error value and type.
Definition: Status.cpp:216
Definition: SBAddress.h:15
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
ValueType GetError() const
Access the error value.
Definition: Status.cpp:174
#define LLDB_GENERIC_ERROR
Definition: lldb-defines.h:46
lldb::ErrorType m_type
The type of the above error code.
Definition: Status.h:212
An error handling class.
Definition: Status.h:44
ValueType m_code
Member variables.
Definition: Status.h:211