Path: blob/main/contrib/llvm-project/lldb/source/Utility/Event.cpp
39587 views
//===-- Event.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/Utility/Event.h"910#include "lldb/Utility/Broadcaster.h"11#include "lldb/Utility/DataExtractor.h"12#include "lldb/Utility/Endian.h"13#include "lldb/Utility/Listener.h"14#include "lldb/Utility/Stream.h"15#include "lldb/Utility/StreamString.h"16#include "lldb/lldb-enumerations.h"1718#include "llvm/ADT/StringExtras.h"1920#include <algorithm>2122#include <cctype>2324using namespace lldb;25using namespace lldb_private;2627#pragma mark -28#pragma mark Event2930// Event functions3132Event::Event(Broadcaster *broadcaster, uint32_t event_type, EventData *data)33: m_broadcaster_wp(broadcaster->GetBroadcasterImpl()), m_type(event_type),34m_data_sp(data) {}3536Event::Event(Broadcaster *broadcaster, uint32_t event_type,37const EventDataSP &event_data_sp)38: m_broadcaster_wp(broadcaster->GetBroadcasterImpl()), m_type(event_type),39m_data_sp(event_data_sp) {}4041Event::Event(uint32_t event_type, EventData *data)42: m_broadcaster_wp(), m_type(event_type), m_data_sp(data) {}4344Event::Event(uint32_t event_type, const EventDataSP &event_data_sp)45: m_broadcaster_wp(), m_type(event_type), m_data_sp(event_data_sp) {}4647Event::~Event() = default;4849void Event::Dump(Stream *s) const {50Broadcaster *broadcaster;51Broadcaster::BroadcasterImplSP broadcaster_impl_sp(m_broadcaster_wp.lock());52if (broadcaster_impl_sp)53broadcaster = broadcaster_impl_sp->GetBroadcaster();54else55broadcaster = nullptr;5657if (broadcaster) {58StreamString event_name;59if (broadcaster->GetEventNames(event_name, m_type, false))60s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x (%s), data = ",61static_cast<const void *>(this),62static_cast<void *>(broadcaster),63broadcaster->GetBroadcasterName().c_str(), m_type,64event_name.GetData());65else66s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x, data = ",67static_cast<const void *>(this),68static_cast<void *>(broadcaster),69broadcaster->GetBroadcasterName().c_str(), m_type);70} else71s->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ",72static_cast<const void *>(this), m_type);7374if (m_data_sp) {75s->PutChar('{');76m_data_sp->Dump(s);77s->PutChar('}');78} else79s->Printf("<NULL>");80}8182void Event::DoOnRemoval() {83std::lock_guard<std::mutex> guard(m_listeners_mutex);8485if (!m_data_sp)86return;8788m_data_sp->DoOnRemoval(this);8990// Now that the event has been handled by the primary event Listener, forward91// it to the other Listeners.9293EventSP me_sp = shared_from_this();94if (m_data_sp->ForwardEventToPendingListeners(this)) {95for (auto listener_sp : m_pending_listeners)96listener_sp->AddEvent(me_sp);97m_pending_listeners.clear();98}99}100101#pragma mark -102#pragma mark EventData103104// EventData functions105106EventData::EventData() = default;107108EventData::~EventData() = default;109110void EventData::Dump(Stream *s) const { s->PutCString("Generic Event Data"); }111112#pragma mark -113#pragma mark EventDataBytes114115// EventDataBytes functions116117EventDataBytes::EventDataBytes() : m_bytes() {}118119EventDataBytes::EventDataBytes(llvm::StringRef str) : m_bytes(str.str()) {}120121EventDataBytes::~EventDataBytes() = default;122123llvm::StringRef EventDataBytes::GetFlavorString() { return "EventDataBytes"; }124125llvm::StringRef EventDataBytes::GetFlavor() const {126return EventDataBytes::GetFlavorString();127}128129void EventDataBytes::Dump(Stream *s) const {130if (llvm::all_of(m_bytes, llvm::isPrint))131s->Format("\"{0}\"", m_bytes);132else133s->Format("{0:$[ ]@[x-2]}", llvm::make_range(134reinterpret_cast<const uint8_t *>(m_bytes.data()),135reinterpret_cast<const uint8_t *>(m_bytes.data() +136m_bytes.size())));137}138139const void *EventDataBytes::GetBytes() const {140return (m_bytes.empty() ? nullptr : m_bytes.data());141}142143size_t EventDataBytes::GetByteSize() const { return m_bytes.size(); }144145const void *EventDataBytes::GetBytesFromEvent(const Event *event_ptr) {146const EventDataBytes *e = GetEventDataFromEvent(event_ptr);147if (e != nullptr)148return e->GetBytes();149return nullptr;150}151152size_t EventDataBytes::GetByteSizeFromEvent(const Event *event_ptr) {153const EventDataBytes *e = GetEventDataFromEvent(event_ptr);154if (e != nullptr)155return e->GetByteSize();156return 0;157}158159const EventDataBytes *160EventDataBytes::GetEventDataFromEvent(const Event *event_ptr) {161if (event_ptr != nullptr) {162const EventData *event_data = event_ptr->GetData();163if (event_data &&164event_data->GetFlavor() == EventDataBytes::GetFlavorString())165return static_cast<const EventDataBytes *>(event_data);166}167return nullptr;168}169170llvm::StringRef EventDataReceipt::GetFlavorString() {171return "Process::ProcessEventData";172}173174#pragma mark -175#pragma mark EventStructuredData176177// EventDataStructuredData definitions178179EventDataStructuredData::EventDataStructuredData()180: EventData(), m_process_sp(), m_object_sp(), m_plugin_sp() {}181182EventDataStructuredData::EventDataStructuredData(183const ProcessSP &process_sp, const StructuredData::ObjectSP &object_sp,184const lldb::StructuredDataPluginSP &plugin_sp)185: EventData(), m_process_sp(process_sp), m_object_sp(object_sp),186m_plugin_sp(plugin_sp) {}187188EventDataStructuredData::~EventDataStructuredData() = default;189190// EventDataStructuredData member functions191192llvm::StringRef EventDataStructuredData::GetFlavor() const {193return EventDataStructuredData::GetFlavorString();194}195196void EventDataStructuredData::Dump(Stream *s) const {197if (!s)198return;199200if (m_object_sp)201m_object_sp->Dump(*s);202}203204const ProcessSP &EventDataStructuredData::GetProcess() const {205return m_process_sp;206}207208const StructuredData::ObjectSP &EventDataStructuredData::GetObject() const {209return m_object_sp;210}211212const lldb::StructuredDataPluginSP &213EventDataStructuredData::GetStructuredDataPlugin() const {214return m_plugin_sp;215}216217void EventDataStructuredData::SetProcess(const ProcessSP &process_sp) {218m_process_sp = process_sp;219}220221void EventDataStructuredData::SetObject(222const StructuredData::ObjectSP &object_sp) {223m_object_sp = object_sp;224}225226void EventDataStructuredData::SetStructuredDataPlugin(227const lldb::StructuredDataPluginSP &plugin_sp) {228m_plugin_sp = plugin_sp;229}230231// EventDataStructuredData static functions232233const EventDataStructuredData *234EventDataStructuredData::GetEventDataFromEvent(const Event *event_ptr) {235if (event_ptr == nullptr)236return nullptr;237238const EventData *event_data = event_ptr->GetData();239if (!event_data ||240event_data->GetFlavor() != EventDataStructuredData::GetFlavorString())241return nullptr;242243return static_cast<const EventDataStructuredData *>(event_data);244}245246ProcessSP EventDataStructuredData::GetProcessFromEvent(const Event *event_ptr) {247auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);248if (event_data)249return event_data->GetProcess();250else251return ProcessSP();252}253254StructuredData::ObjectSP255EventDataStructuredData::GetObjectFromEvent(const Event *event_ptr) {256auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);257if (event_data)258return event_data->GetObject();259else260return StructuredData::ObjectSP();261}262263lldb::StructuredDataPluginSP264EventDataStructuredData::GetPluginFromEvent(const Event *event_ptr) {265auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);266if (event_data)267return event_data->GetStructuredDataPlugin();268else269return StructuredDataPluginSP();270}271272llvm::StringRef EventDataStructuredData::GetFlavorString() {273return "EventDataStructuredData";274}275276277