Status.cpp revision 320041
138032Speter//===-- Status.cpp -----------------------------------------------*- C++
238032Speter//-*-===//
3261194Sgshapiro//
464562Sgshapiro//                     The LLVM Compiler Infrastructure
538032Speter//
638032Speter// This file is distributed under the University of Illinois Open Source
738032Speter// License. See LICENSE.TXT for details.
838032Speter//
938032Speter//===----------------------------------------------------------------------===//
1038032Speter
1138032Speter#include "lldb/Utility/Status.h"
1238032Speter
1338032Speter#include "lldb/Utility/VASPrintf.h"
1438032Speter#include "lldb/lldb-defines.h"      // for LLDB_GENERIC_ERROR
1538032Speter#include "lldb/lldb-enumerations.h" // for ErrorType, ErrorType::eErr...
16266527Sgshapiro#include "llvm/ADT/SmallString.h"   // for SmallString
1738032Speter#include "llvm/ADT/StringRef.h"     // for StringRef
1838032Speter#include "llvm/Support/Errno.h"
19#include "llvm/Support/FormatProviders.h" // for format_provider
20
21#include <cerrno>
22#include <cstdarg>
23#include <string> // for string
24#include <system_error>
25
26#ifdef __APPLE__
27#include <mach/mach.h>
28#endif
29
30#include <stdint.h> // for uint32_t
31
32namespace llvm {
33class raw_ostream;
34}
35
36using namespace lldb;
37using namespace lldb_private;
38
39Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
40
41Status::Status(ValueType err, ErrorType type)
42    : m_code(err), m_type(type), m_string() {}
43
44Status::Status(std::error_code EC)
45    : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric),
46      m_string(EC.message()) {}
47
48Status::Status(const Status &rhs) = default;
49
50Status::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
59const 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
86llvm::Error Status::ToError() const {
87  if (Success())
88    return llvm::Error::success();
89  if (m_type == ErrorType::eErrorTypePOSIX)
90    return llvm::errorCodeToError(std::error_code(m_code, std::generic_category()));
91  return llvm::make_error<llvm::StringError>(AsCString(),
92                                             llvm::inconvertibleErrorCode());
93}
94
95//----------------------------------------------------------------------
96// Assignment operator
97//----------------------------------------------------------------------
98const Status &Status::operator=(const Status &rhs) {
99  if (this != &rhs) {
100    m_code = rhs.m_code;
101    m_type = rhs.m_type;
102    m_string = rhs.m_string;
103  }
104  return *this;
105}
106
107//----------------------------------------------------------------------
108// Assignment operator
109//----------------------------------------------------------------------
110const Status &Status::operator=(uint32_t err) {
111  m_code = err;
112  m_type = eErrorTypeMachKernel;
113  m_string.clear();
114  return *this;
115}
116
117Status::~Status() = default;
118
119//----------------------------------------------------------------------
120// Get the error value as a NULL C string. The error string will be
121// fetched and cached on demand. The cached error string value will
122// remain until the error value is changed or cleared.
123//----------------------------------------------------------------------
124const char *Status::AsCString(const char *default_error_str) const {
125  if (Success())
126    return nullptr;
127
128  if (m_string.empty()) {
129    switch (m_type) {
130    case eErrorTypeMachKernel:
131#if defined(__APPLE__)
132      if (const char *s = ::mach_error_string(m_code))
133        m_string.assign(s);
134#endif
135      break;
136
137    case eErrorTypePOSIX:
138      m_string = llvm::sys::StrError(m_code);
139      break;
140
141    default:
142      break;
143    }
144  }
145  if (m_string.empty()) {
146    if (default_error_str)
147      m_string.assign(default_error_str);
148    else
149      return nullptr; // User wanted a nullptr string back...
150  }
151  return m_string.c_str();
152}
153
154//----------------------------------------------------------------------
155// Clear the error and any cached error string that it might contain.
156//----------------------------------------------------------------------
157void Status::Clear() {
158  m_code = 0;
159  m_type = eErrorTypeInvalid;
160  m_string.clear();
161}
162
163//----------------------------------------------------------------------
164// Access the error value.
165//----------------------------------------------------------------------
166Status::ValueType Status::GetError() const { return m_code; }
167
168//----------------------------------------------------------------------
169// Access the error type.
170//----------------------------------------------------------------------
171ErrorType Status::GetType() const { return m_type; }
172
173//----------------------------------------------------------------------
174// Returns true if this object contains a value that describes an
175// error or otherwise non-success result.
176//----------------------------------------------------------------------
177bool Status::Fail() const { return m_code != 0; }
178
179//----------------------------------------------------------------------
180// Set accesssor for the error value to "err" and the type to
181// "eErrorTypeMachKernel"
182//----------------------------------------------------------------------
183void Status::SetMachError(uint32_t err) {
184  m_code = err;
185  m_type = eErrorTypeMachKernel;
186  m_string.clear();
187}
188
189void Status::SetExpressionError(lldb::ExpressionResults result,
190                                const char *mssg) {
191  m_code = result;
192  m_type = eErrorTypeExpression;
193  m_string = mssg;
194}
195
196int 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//----------------------------------------------------------------------
214// Set accesssor for the error value and type.
215//----------------------------------------------------------------------
216void Status::SetError(ValueType err, ErrorType type) {
217  m_code = err;
218  m_type = type;
219  m_string.clear();
220}
221
222//----------------------------------------------------------------------
223// Update the error value to be "errno" and update the type to
224// be "POSIX".
225//----------------------------------------------------------------------
226void Status::SetErrorToErrno() {
227  m_code = errno;
228  m_type = eErrorTypePOSIX;
229  m_string.clear();
230}
231
232//----------------------------------------------------------------------
233// Update the error value to be LLDB_GENERIC_ERROR and update the type
234// to be "Generic".
235//----------------------------------------------------------------------
236void Status::SetErrorToGenericError() {
237  m_code = LLDB_GENERIC_ERROR;
238  m_type = eErrorTypeGeneric;
239  m_string.clear();
240}
241
242//----------------------------------------------------------------------
243// Set accessor for the error string value for a specific error.
244// This allows any string to be supplied as an error explanation.
245// The error string value will remain until the error value is
246// cleared or a new error value/type is assigned.
247//----------------------------------------------------------------------
248void Status::SetErrorString(llvm::StringRef err_str) {
249  if (!err_str.empty()) {
250    // If we have an error string, we should always at least have an error
251    // set to a generic value.
252    if (Success())
253      SetErrorToGenericError();
254  }
255  m_string = err_str;
256}
257
258//------------------------------------------------------------------
259/// Set the current error string to a formatted error string.
260///
261/// @param format
262///     A printf style format string
263//------------------------------------------------------------------
264int Status::SetErrorStringWithFormat(const char *format, ...) {
265  if (format != nullptr && format[0]) {
266    va_list args;
267    va_start(args, format);
268    int length = SetErrorStringWithVarArg(format, args);
269    va_end(args);
270    return length;
271  } else {
272    m_string.clear();
273  }
274  return 0;
275}
276
277int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
278  if (format != nullptr && format[0]) {
279    // If we have an error string, we should always at least have
280    // an error set to a generic value.
281    if (Success())
282      SetErrorToGenericError();
283
284    llvm::SmallString<1024> buf;
285    VASprintf(buf, format, args);
286    m_string = buf.str();
287    return buf.size();
288  } else {
289    m_string.clear();
290  }
291  return 0;
292}
293
294//----------------------------------------------------------------------
295// Returns true if the error code in this object is considered a
296// successful return value.
297//----------------------------------------------------------------------
298bool Status::Success() const { return m_code == 0; }
299
300bool Status::WasInterrupted() const {
301  return (m_type == eErrorTypePOSIX && m_code == EINTR);
302}
303
304void llvm::format_provider<lldb_private::Status>::format(
305    const lldb_private::Status &error, llvm::raw_ostream &OS,
306    llvm::StringRef Options) {
307  llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
308                                                 Options);
309}
310