LLDB  mainline
SBError.cpp
Go to the documentation of this file.
1 //===-- SBError.cpp -------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBError.h"
10 #include "Utils.h"
11 #include "lldb/API/SBStream.h"
13 #include "lldb/Utility/Status.h"
14 
15 #include <cstdarg>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
21 
23  LLDB_INSTRUMENT_VA(this, rhs);
24 
25  m_opaque_up = clone(rhs.m_opaque_up);
26 }
27 
29  : m_opaque_up(new Status(status)) {
30  LLDB_INSTRUMENT_VA(this, status);
31 }
32 
33 SBError::~SBError() = default;
34 
35 const SBError &SBError::operator=(const SBError &rhs) {
36  LLDB_INSTRUMENT_VA(this, rhs);
37 
38  if (this != &rhs)
40  return *this;
41 }
42 
43 const char *SBError::GetCString() const {
44  LLDB_INSTRUMENT_VA(this);
45 
46  if (m_opaque_up)
47  return m_opaque_up->AsCString();
48  return nullptr;
49 }
50 
52  LLDB_INSTRUMENT_VA(this);
53 
54  if (m_opaque_up)
55  m_opaque_up->Clear();
56 }
57 
58 bool SBError::Fail() const {
59  LLDB_INSTRUMENT_VA(this);
60 
61  bool ret_value = false;
62  if (m_opaque_up)
63  ret_value = m_opaque_up->Fail();
64 
65 
66  return ret_value;
67 }
68 
69 bool SBError::Success() const {
70  LLDB_INSTRUMENT_VA(this);
71 
72  bool ret_value = true;
73  if (m_opaque_up)
74  ret_value = m_opaque_up->Success();
75 
76  return ret_value;
77 }
78 
80  LLDB_INSTRUMENT_VA(this);
81 
82  uint32_t err = 0;
83  if (m_opaque_up)
84  err = m_opaque_up->GetError();
85 
86 
87  return err;
88 }
89 
91  LLDB_INSTRUMENT_VA(this);
92 
93  ErrorType err_type = eErrorTypeInvalid;
94  if (m_opaque_up)
95  err_type = m_opaque_up->GetType();
96 
97  return err_type;
98 }
99 
101  LLDB_INSTRUMENT_VA(this, err, type);
102 
103  CreateIfNeeded();
104  m_opaque_up->SetError(err, type);
105 }
106 
107 void SBError::SetError(const Status &lldb_error) {
108  CreateIfNeeded();
109  *m_opaque_up = lldb_error;
110 }
111 
113  LLDB_INSTRUMENT_VA(this);
114 
115  CreateIfNeeded();
116  m_opaque_up->SetErrorToErrno();
117 }
118 
120  LLDB_INSTRUMENT_VA(this);
121 
122  CreateIfNeeded();
123  m_opaque_up->SetErrorToGenericError();
124 }
125 
126 void SBError::SetErrorString(const char *err_str) {
127  LLDB_INSTRUMENT_VA(this, err_str);
128 
129  CreateIfNeeded();
130  m_opaque_up->SetErrorString(err_str);
131 }
132 
133 int SBError::SetErrorStringWithFormat(const char *format, ...) {
134  CreateIfNeeded();
135  va_list args;
136  va_start(args, format);
137  int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);
138  va_end(args);
139  return num_chars;
140 }
141 
142 bool SBError::IsValid() const {
143  LLDB_INSTRUMENT_VA(this);
144  return this->operator bool();
145 }
146 SBError::operator bool() const {
147  LLDB_INSTRUMENT_VA(this);
148 
149  return m_opaque_up != nullptr;
150 }
151 
153  if (m_opaque_up == nullptr)
154  m_opaque_up = std::make_unique<Status>();
155 }
156 
158 
160 
162  CreateIfNeeded();
163  return *m_opaque_up;
164 }
165 
167  // Be sure to call "IsValid()" before calling this function or it will crash
168  return *m_opaque_up;
169 }
170 
171 bool SBError::GetDescription(SBStream &description) {
172  LLDB_INSTRUMENT_VA(this, description);
173 
174  if (m_opaque_up) {
175  if (m_opaque_up->Success())
176  description.Printf("success");
177  else {
178  const char *err_string = GetCString();
179  description.Printf("error: %s",
180  (err_string != nullptr ? err_string : ""));
181  }
182  } else
183  description.Printf("error: <NULL>");
184 
185  return true;
186 }
Instrumentation.h
lldb::SBStream::Printf
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:70
lldb::SBError::get
lldb_private::Status * get()
Definition: SBError.cpp:159
lldb::SBError::GetCString
const char * GetCString() const
Get the error string as a NULL terminated UTF8 c-string.
Definition: SBError.cpp:43
lldb::SBError
Definition: SBError.h:20
lldb::SBError::SetErrorToErrno
void SetErrorToErrno()
Definition: SBError.cpp:112
lldb::SBError::operator*
const lldb_private::Status & operator*() const
Definition: SBError.cpp:166
lldb::SBError::SetErrorToGenericError
void SetErrorToGenericError()
Definition: SBError.cpp:119
lldb::SBError::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBError.cpp:171
lldb::SBError::Success
bool Success() const
Definition: SBError.cpp:69
SBStream.h
lldb::SBError::Fail
bool Fail() const
Definition: SBError.cpp:58
lldb::SBError::Clear
void Clear()
Definition: SBError.cpp:51
lldb::SBError::SetErrorStringWithFormat
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: SBError.cpp:133
lldb_private::clone
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
Utils.h
lldb::ErrorType
ErrorType
Definition: lldb-enumerations.h:307
lldb::SBError::SBError
SBError()
Definition: SBError.cpp:20
lldb::SBError::SetErrorString
void SetErrorString(const char *err_str)
Definition: SBError.cpp:126
lldb::SBError::GetError
uint32_t GetError() const
Definition: SBError.cpp:79
lldb::SBError::CreateIfNeeded
void CreateIfNeeded()
Definition: SBError.cpp:152
lldb::SBError::SetError
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:100
lldb::SBError::IsValid
bool IsValid() const
Definition: SBError.cpp:142
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb::SBError::operator=
const SBError & operator=(const lldb::SBError &rhs)
Definition: SBError.cpp:35
lldb::eErrorTypeInvalid
@ eErrorTypeInvalid
Definition: lldb-enumerations.h:308
lldb::SBError::~SBError
~SBError()
Status.h
lldb::SBError::ref
lldb_private::Status & ref()
Definition: SBError.cpp:161
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
lldb::SBError::operator->
lldb_private::Status * operator->()
Definition: SBError.cpp:157
lldb::SBStream
Definition: SBStream.h:18
lldb::SBError::m_opaque_up
std::unique_ptr< lldb_private::Status > m_opaque_up
Definition: SBError.h:99
lldb::SBError::GetType
lldb::ErrorType GetType() const
Definition: SBError.cpp:90
lldb
Definition: SBAddress.h:15
SBError.h