1cfca06d7SDimitry Andric //===-- SBError.cpp -------------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/API/SBError.h"
105f29bb8aSDimitry Andric #include "Utils.h"
11f034231aSEd Maste #include "lldb/API/SBStream.h"
126f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
13b76161e4SDimitry Andric #include "lldb/Utility/Status.h"
14f034231aSEd Maste
15344a3780SDimitry Andric #include <cstdarg>
16f034231aSEd Maste
17f034231aSEd Maste using namespace lldb;
18f034231aSEd Maste using namespace lldb_private;
19f034231aSEd Maste
SBError()206f8fc217SDimitry Andric SBError::SBError() { LLDB_INSTRUMENT_VA(this); }
21f034231aSEd Maste
SBError(const SBError & rhs)226f8fc217SDimitry Andric SBError::SBError(const SBError &rhs) {
236f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
245f29bb8aSDimitry Andric
255f29bb8aSDimitry Andric m_opaque_up = clone(rhs.m_opaque_up);
26f034231aSEd Maste }
27f034231aSEd Maste
SBError(const char * message)287fa27ce4SDimitry Andric SBError::SBError(const char *message) {
297fa27ce4SDimitry Andric LLDB_INSTRUMENT_VA(this, message);
307fa27ce4SDimitry Andric
317fa27ce4SDimitry Andric SetErrorString(message);
327fa27ce4SDimitry Andric }
337fa27ce4SDimitry Andric
SBError(const lldb_private::Status & status)34e3b55780SDimitry Andric SBError::SBError(const lldb_private::Status &status)
35e3b55780SDimitry Andric : m_opaque_up(new Status(status)) {
36e3b55780SDimitry Andric LLDB_INSTRUMENT_VA(this, status);
37e3b55780SDimitry Andric }
38e3b55780SDimitry Andric
39cfca06d7SDimitry Andric SBError::~SBError() = default;
40f034231aSEd Maste
operator =(const SBError & rhs)4114f1b3e8SDimitry Andric const SBError &SBError::operator=(const SBError &rhs) {
426f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
43f034231aSEd Maste
445f29bb8aSDimitry Andric if (this != &rhs)
455f29bb8aSDimitry Andric m_opaque_up = clone(rhs.m_opaque_up);
466f8fc217SDimitry Andric return *this;
47f034231aSEd Maste }
48f034231aSEd Maste
GetCString() const4914f1b3e8SDimitry Andric const char *SBError::GetCString() const {
506f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
515f29bb8aSDimitry Andric
525f29bb8aSDimitry Andric if (m_opaque_up)
535f29bb8aSDimitry Andric return m_opaque_up->AsCString();
545f29bb8aSDimitry Andric return nullptr;
55f034231aSEd Maste }
56f034231aSEd Maste
Clear()5714f1b3e8SDimitry Andric void SBError::Clear() {
586f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
595f29bb8aSDimitry Andric
605f29bb8aSDimitry Andric if (m_opaque_up)
615f29bb8aSDimitry Andric m_opaque_up->Clear();
62f034231aSEd Maste }
63f034231aSEd Maste
Fail() const6414f1b3e8SDimitry Andric bool SBError::Fail() const {
656f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
66f034231aSEd Maste
67f034231aSEd Maste bool ret_value = false;
685f29bb8aSDimitry Andric if (m_opaque_up)
695f29bb8aSDimitry Andric ret_value = m_opaque_up->Fail();
70f034231aSEd Maste
71f034231aSEd Maste
72f034231aSEd Maste return ret_value;
73f034231aSEd Maste }
74f034231aSEd Maste
Success() const7514f1b3e8SDimitry Andric bool SBError::Success() const {
766f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
77f034231aSEd Maste
785f29bb8aSDimitry Andric bool ret_value = true;
795f29bb8aSDimitry Andric if (m_opaque_up)
805f29bb8aSDimitry Andric ret_value = m_opaque_up->Success();
81f034231aSEd Maste
82f034231aSEd Maste return ret_value;
83f034231aSEd Maste }
84f034231aSEd Maste
GetError() const8514f1b3e8SDimitry Andric uint32_t SBError::GetError() const {
866f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
87f034231aSEd Maste
88f034231aSEd Maste uint32_t err = 0;
895f29bb8aSDimitry Andric if (m_opaque_up)
905f29bb8aSDimitry Andric err = m_opaque_up->GetError();
91f034231aSEd Maste
92f034231aSEd Maste
93f034231aSEd Maste return err;
94f034231aSEd Maste }
95f034231aSEd Maste
GetType() const9614f1b3e8SDimitry Andric ErrorType SBError::GetType() const {
976f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
98f034231aSEd Maste
995f29bb8aSDimitry Andric ErrorType err_type = eErrorTypeInvalid;
1005f29bb8aSDimitry Andric if (m_opaque_up)
1015f29bb8aSDimitry Andric err_type = m_opaque_up->GetType();
102f034231aSEd Maste
103f034231aSEd Maste return err_type;
104f034231aSEd Maste }
105f034231aSEd Maste
SetError(uint32_t err,ErrorType type)10614f1b3e8SDimitry Andric void SBError::SetError(uint32_t err, ErrorType type) {
1076f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, err, type);
1085f29bb8aSDimitry Andric
109f034231aSEd Maste CreateIfNeeded();
1105f29bb8aSDimitry Andric m_opaque_up->SetError(err, type);
111f034231aSEd Maste }
112f034231aSEd Maste
SetError(const Status & lldb_error)113b76161e4SDimitry Andric void SBError::SetError(const Status &lldb_error) {
114f034231aSEd Maste CreateIfNeeded();
1155f29bb8aSDimitry Andric *m_opaque_up = lldb_error;
116f034231aSEd Maste }
117f034231aSEd Maste
SetErrorToErrno()11814f1b3e8SDimitry Andric void SBError::SetErrorToErrno() {
1196f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1205f29bb8aSDimitry Andric
121f034231aSEd Maste CreateIfNeeded();
1225f29bb8aSDimitry Andric m_opaque_up->SetErrorToErrno();
123f034231aSEd Maste }
124f034231aSEd Maste
SetErrorToGenericError()12514f1b3e8SDimitry Andric void SBError::SetErrorToGenericError() {
1266f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1275f29bb8aSDimitry Andric
128f034231aSEd Maste CreateIfNeeded();
129b60736ecSDimitry Andric m_opaque_up->SetErrorToGenericError();
130f034231aSEd Maste }
131f034231aSEd Maste
SetErrorString(const char * err_str)13214f1b3e8SDimitry Andric void SBError::SetErrorString(const char *err_str) {
1336f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, err_str);
1345f29bb8aSDimitry Andric
135f034231aSEd Maste CreateIfNeeded();
1365f29bb8aSDimitry Andric m_opaque_up->SetErrorString(err_str);
137f034231aSEd Maste }
138f034231aSEd Maste
SetErrorStringWithFormat(const char * format,...)13914f1b3e8SDimitry Andric int SBError::SetErrorStringWithFormat(const char *format, ...) {
140f034231aSEd Maste CreateIfNeeded();
141f034231aSEd Maste va_list args;
142f034231aSEd Maste va_start(args, format);
1435f29bb8aSDimitry Andric int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);
144f034231aSEd Maste va_end(args);
145f034231aSEd Maste return num_chars;
146f034231aSEd Maste }
147f034231aSEd Maste
IsValid() const1485f29bb8aSDimitry Andric bool SBError::IsValid() const {
1496f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1505f29bb8aSDimitry Andric return this->operator bool();
1515f29bb8aSDimitry Andric }
operator bool() const1525f29bb8aSDimitry Andric SBError::operator bool() const {
1536f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
154f034231aSEd Maste
1555f29bb8aSDimitry Andric return m_opaque_up != nullptr;
156f034231aSEd Maste }
157f034231aSEd Maste
CreateIfNeeded()1585f29bb8aSDimitry Andric void SBError::CreateIfNeeded() {
1595f29bb8aSDimitry Andric if (m_opaque_up == nullptr)
160cfca06d7SDimitry Andric m_opaque_up = std::make_unique<Status>();
1615f29bb8aSDimitry Andric }
162f034231aSEd Maste
operator ->()1635f29bb8aSDimitry Andric lldb_private::Status *SBError::operator->() { return m_opaque_up.get(); }
1645f29bb8aSDimitry Andric
get()1655f29bb8aSDimitry Andric lldb_private::Status *SBError::get() { return m_opaque_up.get(); }
166f034231aSEd Maste
ref()167b76161e4SDimitry Andric lldb_private::Status &SBError::ref() {
168f034231aSEd Maste CreateIfNeeded();
1695f29bb8aSDimitry Andric return *m_opaque_up;
170f034231aSEd Maste }
171f034231aSEd Maste
operator *() const172b76161e4SDimitry Andric const lldb_private::Status &SBError::operator*() const {
173f034231aSEd Maste // Be sure to call "IsValid()" before calling this function or it will crash
1745f29bb8aSDimitry Andric return *m_opaque_up;
175f034231aSEd Maste }
176f034231aSEd Maste
GetDescription(SBStream & description)17714f1b3e8SDimitry Andric bool SBError::GetDescription(SBStream &description) {
1786f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, description);
1795f29bb8aSDimitry Andric
1805f29bb8aSDimitry Andric if (m_opaque_up) {
1815f29bb8aSDimitry Andric if (m_opaque_up->Success())
182f034231aSEd Maste description.Printf("success");
18314f1b3e8SDimitry Andric else {
184f034231aSEd Maste const char *err_string = GetCString();
1855f29bb8aSDimitry Andric description.Printf("error: %s",
1865f29bb8aSDimitry Andric (err_string != nullptr ? err_string : ""));
187f034231aSEd Maste }
18814f1b3e8SDimitry Andric } else
189f034231aSEd Maste description.Printf("error: <NULL>");
190f034231aSEd Maste
191f034231aSEd Maste return true;
192f034231aSEd Maste }
193