Path: blob/main/contrib/llvm-project/lldb/source/API/SBEvent.cpp
39587 views
//===-- SBEvent.cpp -------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBEvent.h"9#include "lldb/API/SBBroadcaster.h"10#include "lldb/API/SBStream.h"11#include "lldb/Utility/Instrumentation.h"1213#include "lldb/Breakpoint/Breakpoint.h"14#include "lldb/Interpreter/CommandInterpreter.h"15#include "lldb/Target/Process.h"16#include "lldb/Utility/ConstString.h"17#include "lldb/Utility/Event.h"18#include "lldb/Utility/Stream.h"1920using namespace lldb;21using namespace lldb_private;2223SBEvent::SBEvent() { LLDB_INSTRUMENT_VA(this); }2425SBEvent::SBEvent(uint32_t event_type, const char *cstr, uint32_t cstr_len)26: m_event_sp(new Event(27event_type, new EventDataBytes(llvm::StringRef(cstr, cstr_len)))),28m_opaque_ptr(m_event_sp.get()) {29LLDB_INSTRUMENT_VA(this, event_type, cstr, cstr_len);30}3132SBEvent::SBEvent(EventSP &event_sp)33: m_event_sp(event_sp), m_opaque_ptr(event_sp.get()) {34LLDB_INSTRUMENT_VA(this, event_sp);35}3637SBEvent::SBEvent(Event *event_ptr) : m_opaque_ptr(event_ptr) {38LLDB_INSTRUMENT_VA(this, event_ptr);39}4041SBEvent::SBEvent(const SBEvent &rhs)42: m_event_sp(rhs.m_event_sp), m_opaque_ptr(rhs.m_opaque_ptr) {43LLDB_INSTRUMENT_VA(this, rhs);44}4546const SBEvent &SBEvent::operator=(const SBEvent &rhs) {47LLDB_INSTRUMENT_VA(this, rhs);4849if (this != &rhs) {50m_event_sp = rhs.m_event_sp;51m_opaque_ptr = rhs.m_opaque_ptr;52}53return *this;54}5556SBEvent::~SBEvent() = default;5758const char *SBEvent::GetDataFlavor() {59LLDB_INSTRUMENT_VA(this);6061Event *lldb_event = get();62if (lldb_event) {63EventData *event_data = lldb_event->GetData();64if (event_data)65return ConstString(lldb_event->GetData()->GetFlavor()).GetCString();66}67return nullptr;68}6970uint32_t SBEvent::GetType() const {71LLDB_INSTRUMENT_VA(this);7273const Event *lldb_event = get();74uint32_t event_type = 0;75if (lldb_event)76event_type = lldb_event->GetType();777879return event_type;80}8182SBBroadcaster SBEvent::GetBroadcaster() const {83LLDB_INSTRUMENT_VA(this);8485SBBroadcaster broadcaster;86const Event *lldb_event = get();87if (lldb_event)88broadcaster.reset(lldb_event->GetBroadcaster(), false);89return broadcaster;90}9192const char *SBEvent::GetBroadcasterClass() const {93LLDB_INSTRUMENT_VA(this);9495const Event *lldb_event = get();96if (lldb_event)97return ConstString(lldb_event->GetBroadcaster()->GetBroadcasterClass())98.AsCString();99else100return "unknown class";101}102103bool SBEvent::BroadcasterMatchesPtr(const SBBroadcaster *broadcaster) {104LLDB_INSTRUMENT_VA(this, broadcaster);105106if (broadcaster)107return BroadcasterMatchesRef(*broadcaster);108return false;109}110111bool SBEvent::BroadcasterMatchesRef(const SBBroadcaster &broadcaster) {112LLDB_INSTRUMENT_VA(this, broadcaster);113114Event *lldb_event = get();115bool success = false;116if (lldb_event)117success = lldb_event->BroadcasterIs(broadcaster.get());118119120return success;121}122123void SBEvent::Clear() {124LLDB_INSTRUMENT_VA(this);125126Event *lldb_event = get();127if (lldb_event)128lldb_event->Clear();129}130131EventSP &SBEvent::GetSP() const { return m_event_sp; }132133Event *SBEvent::get() const {134// There is a dangerous accessor call GetSharedPtr which can be used, so if135// we have anything valid in m_event_sp, we must use that since if it gets136// used by a function that puts something in there, then it won't update137// m_opaque_ptr...138if (m_event_sp)139m_opaque_ptr = m_event_sp.get();140141return m_opaque_ptr;142}143144void SBEvent::reset(EventSP &event_sp) {145m_event_sp = event_sp;146m_opaque_ptr = m_event_sp.get();147}148149void SBEvent::reset(Event *event_ptr) {150m_opaque_ptr = event_ptr;151m_event_sp.reset();152}153154bool SBEvent::IsValid() const {155LLDB_INSTRUMENT_VA(this);156return this->operator bool();157}158SBEvent::operator bool() const {159LLDB_INSTRUMENT_VA(this);160161// Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor.162// See comments in SBEvent::get()....163return SBEvent::get() != nullptr;164}165166const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {167LLDB_INSTRUMENT_VA(event);168169return ConstString(static_cast<const char *>(170EventDataBytes::GetBytesFromEvent(event.get())))171.GetCString();172}173174bool SBEvent::GetDescription(SBStream &description) {175LLDB_INSTRUMENT_VA(this, description);176177Stream &strm = description.ref();178179if (get()) {180m_opaque_ptr->Dump(&strm);181} else182strm.PutCString("No value");183184return true;185}186187bool SBEvent::GetDescription(SBStream &description) const {188LLDB_INSTRUMENT_VA(this, description);189190Stream &strm = description.ref();191192if (get()) {193m_opaque_ptr->Dump(&strm);194} else195strm.PutCString("No value");196197return true;198}199200201