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 
28 SBError::~SBError() = default;
29 
30 const SBError &SBError::operator=(const SBError &rhs) {
31  LLDB_INSTRUMENT_VA(this, rhs);
32 
33  if (this != &rhs)
34  m_opaque_up = clone(rhs.m_opaque_up);
35  return *this;
36 }
37 
38 const char *SBError::GetCString() const {
39  LLDB_INSTRUMENT_VA(this);
40 
41  if (m_opaque_up)
42  return m_opaque_up->AsCString();
43  return nullptr;
44 }
45 
47  LLDB_INSTRUMENT_VA(this);
48 
49  if (m_opaque_up)
50  m_opaque_up->Clear();
51 }
52 
53 bool SBError::Fail() const {
54  LLDB_INSTRUMENT_VA(this);
55 
56  bool ret_value = false;
57  if (m_opaque_up)
58  ret_value = m_opaque_up->Fail();
59 
60 
61  return ret_value;
62 }
63 
64 bool SBError::Success() const {
65  LLDB_INSTRUMENT_VA(this);
66 
67  bool ret_value = true;
68  if (m_opaque_up)
69  ret_value = m_opaque_up->Success();
70 
71  return ret_value;
72 }
73 
75  LLDB_INSTRUMENT_VA(this);
76 
77  uint32_t err = 0;
78  if (m_opaque_up)
79  err = m_opaque_up->GetError();
80 
81 
82  return err;
83 }
84 
86  LLDB_INSTRUMENT_VA(this);
87 
88  ErrorType err_type = eErrorTypeInvalid;
89  if (m_opaque_up)
90  err_type = m_opaque_up->GetType();
91 
92  return err_type;
93 }
94 
96  LLDB_INSTRUMENT_VA(this, err, type);
97 
98  CreateIfNeeded();
99  m_opaque_up->SetError(err, type);
100 }
101 
102 void SBError::SetError(const Status &lldb_error) {
103  CreateIfNeeded();
104  *m_opaque_up = lldb_error;
105 }
106 
108  LLDB_INSTRUMENT_VA(this);
109 
110  CreateIfNeeded();
111  m_opaque_up->SetErrorToErrno();
112 }
113 
115  LLDB_INSTRUMENT_VA(this);
116 
117  CreateIfNeeded();
118  m_opaque_up->SetErrorToGenericError();
119 }
120 
121 void SBError::SetErrorString(const char *err_str) {
122  LLDB_INSTRUMENT_VA(this, err_str);
123 
124  CreateIfNeeded();
125  m_opaque_up->SetErrorString(err_str);
126 }
127 
128 int SBError::SetErrorStringWithFormat(const char *format, ...) {
129  CreateIfNeeded();
130  va_list args;
131  va_start(args, format);
132  int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);
133  va_end(args);
134  return num_chars;
135 }
136 
137 bool SBError::IsValid() const {
138  LLDB_INSTRUMENT_VA(this);
139  return this->operator bool();
140 }
141 SBError::operator bool() const {
142  LLDB_INSTRUMENT_VA(this);
143 
144  return m_opaque_up != nullptr;
145 }
146 
148  if (m_opaque_up == nullptr)
149  m_opaque_up = std::make_unique<Status>();
150 }
151 
152 lldb_private::Status *SBError::operator->() { return m_opaque_up.get(); }
153 
154 lldb_private::Status *SBError::get() { return m_opaque_up.get(); }
155 
157  CreateIfNeeded();
158  return *m_opaque_up;
159 }
160 
162  // Be sure to call "IsValid()" before calling this function or it will crash
163  return *m_opaque_up;
164 }
165 
166 bool SBError::GetDescription(SBStream &description) {
167  LLDB_INSTRUMENT_VA(this, description);
168 
169  if (m_opaque_up) {
170  if (m_opaque_up->Success())
171  description.Printf("success");
172  else {
173  const char *err_string = GetCString();
174  description.Printf("error: %s",
175  (err_string != nullptr ? err_string : ""));
176  }
177  } else
178  description.Printf("error: <NULL>");
179 
180  return true;
181 }
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:154
lldb::SBError::GetCString
const char * GetCString() const
Definition: SBError.cpp:38
lldb::SBError
Definition: SBError.h:20
lldb::SBError::SetErrorToErrno
void SetErrorToErrno()
Definition: SBError.cpp:107
lldb::SBError::operator*
const lldb_private::Status & operator*() const
Definition: SBError.cpp:161
lldb::SBError::SetErrorToGenericError
void SetErrorToGenericError()
Definition: SBError.cpp:114
lldb::SBError::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBError.cpp:166
lldb::SBError::Success
bool Success() const
Definition: SBError.cpp:64
SBStream.h
lldb::SBError::Fail
bool Fail() const
Definition: SBError.cpp:53
lldb::SBError::Clear
void Clear()
Definition: SBError.cpp:46
lldb::SBError::SetErrorStringWithFormat
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: SBError.cpp:128
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:306
lldb::SBError::SBError
SBError()
Definition: SBError.cpp:20
lldb::SBError::SetErrorString
void SetErrorString(const char *err_str)
Definition: SBError.cpp:121
lldb::SBError::GetError
uint32_t GetError() const
Definition: SBError.cpp:74
lldb::SBError::CreateIfNeeded
void CreateIfNeeded()
Definition: SBError.cpp:147
lldb::SBError::SetError
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:95
lldb::SBError::IsValid
bool IsValid() const
Definition: SBError.cpp:137
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb::SBError::operator=
const SBError & operator=(const lldb::SBError &rhs)
Definition: SBError.cpp:30
lldb::eErrorTypeInvalid
@ eErrorTypeInvalid
Definition: lldb-enumerations.h:307
lldb::SBError::~SBError
~SBError()
Status.h
lldb::SBError::ref
lldb_private::Status & ref()
Definition: SBError.cpp:156
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:152
lldb::SBStream
Definition: SBStream.h:18
lldb::SBError::m_opaque_up
std::unique_ptr< lldb_private::Status > m_opaque_up
Definition: SBError.h:92
lldb::SBError::GetType
lldb::ErrorType GetType() const
Definition: SBError.cpp:85
lldb
Definition: SBAddress.h:15
SBError.h