xref: /src/contrib/llvm-project/lldb/source/Core/DebuggerEvents.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1145449b1SDimitry Andric //===-- DebuggerEvents.cpp ------------------------------------------------===//
2145449b1SDimitry Andric //
3145449b1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4145449b1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5145449b1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6145449b1SDimitry Andric //
7145449b1SDimitry Andric //===----------------------------------------------------------------------===//
8145449b1SDimitry Andric 
9145449b1SDimitry Andric #include "lldb/Core/DebuggerEvents.h"
10e3b55780SDimitry Andric #include "lldb/Core/Debugger.h"
11e3b55780SDimitry Andric #include "lldb/Core/Module.h"
12ac9a064cSDimitry Andric #include "lldb/Core/Progress.h"
13145449b1SDimitry Andric #include "llvm/Support/WithColor.h"
14145449b1SDimitry Andric 
15145449b1SDimitry Andric using namespace lldb_private;
16e3b55780SDimitry Andric using namespace lldb;
17145449b1SDimitry Andric 
18145449b1SDimitry Andric template <typename T>
GetEventDataFromEventImpl(const Event * event_ptr)19145449b1SDimitry Andric static const T *GetEventDataFromEventImpl(const Event *event_ptr) {
20145449b1SDimitry Andric   if (event_ptr)
21145449b1SDimitry Andric     if (const EventData *event_data = event_ptr->GetData())
22145449b1SDimitry Andric       if (event_data->GetFlavor() == T::GetFlavorString())
23145449b1SDimitry Andric         return static_cast<const T *>(event_ptr->GetData());
24145449b1SDimitry Andric   return nullptr;
25145449b1SDimitry Andric }
26145449b1SDimitry Andric 
GetFlavorString()277fa27ce4SDimitry Andric llvm::StringRef ProgressEventData::GetFlavorString() {
287fa27ce4SDimitry Andric   return "ProgressEventData";
29145449b1SDimitry Andric }
30145449b1SDimitry Andric 
GetFlavor() const317fa27ce4SDimitry Andric llvm::StringRef ProgressEventData::GetFlavor() const {
32145449b1SDimitry Andric   return ProgressEventData::GetFlavorString();
33145449b1SDimitry Andric }
34145449b1SDimitry Andric 
Dump(Stream * s) const35145449b1SDimitry Andric void ProgressEventData::Dump(Stream *s) const {
367fa27ce4SDimitry Andric   s->Printf(" id = %" PRIu64 ", title = \"%s\"", m_id, m_title.c_str());
377fa27ce4SDimitry Andric   if (!m_details.empty())
387fa27ce4SDimitry Andric     s->Printf(", details = \"%s\"", m_details.c_str());
39145449b1SDimitry Andric   if (m_completed == 0 || m_completed == m_total)
40145449b1SDimitry Andric     s->Printf(", type = %s", m_completed == 0 ? "start" : "end");
41145449b1SDimitry Andric   else
42145449b1SDimitry Andric     s->PutCString(", type = update");
43145449b1SDimitry Andric   // If m_total is UINT64_MAX, there is no progress to report, just "start"
44145449b1SDimitry Andric   // and "end". If it isn't we will show the completed and total amounts.
45ac9a064cSDimitry Andric   if (m_total != Progress::kNonDeterministicTotal)
46145449b1SDimitry Andric     s->Printf(", progress = %" PRIu64 " of %" PRIu64, m_completed, m_total);
47145449b1SDimitry Andric }
48145449b1SDimitry Andric 
49145449b1SDimitry Andric const ProgressEventData *
GetEventDataFromEvent(const Event * event_ptr)50145449b1SDimitry Andric ProgressEventData::GetEventDataFromEvent(const Event *event_ptr) {
51145449b1SDimitry Andric   return GetEventDataFromEventImpl<ProgressEventData>(event_ptr);
52145449b1SDimitry Andric }
53145449b1SDimitry Andric 
547fa27ce4SDimitry Andric StructuredData::DictionarySP
GetAsStructuredData(const Event * event_ptr)557fa27ce4SDimitry Andric ProgressEventData::GetAsStructuredData(const Event *event_ptr) {
567fa27ce4SDimitry Andric   const ProgressEventData *progress_data =
577fa27ce4SDimitry Andric       ProgressEventData::GetEventDataFromEvent(event_ptr);
587fa27ce4SDimitry Andric 
597fa27ce4SDimitry Andric   if (!progress_data)
607fa27ce4SDimitry Andric     return {};
617fa27ce4SDimitry Andric 
627fa27ce4SDimitry Andric   auto dictionary_sp = std::make_shared<StructuredData::Dictionary>();
637fa27ce4SDimitry Andric   dictionary_sp->AddStringItem("title", progress_data->GetTitle());
647fa27ce4SDimitry Andric   dictionary_sp->AddStringItem("details", progress_data->GetDetails());
657fa27ce4SDimitry Andric   dictionary_sp->AddStringItem("message", progress_data->GetMessage());
667fa27ce4SDimitry Andric   dictionary_sp->AddIntegerItem("progress_id", progress_data->GetID());
677fa27ce4SDimitry Andric   dictionary_sp->AddIntegerItem("completed", progress_data->GetCompleted());
687fa27ce4SDimitry Andric   dictionary_sp->AddIntegerItem("total", progress_data->GetTotal());
697fa27ce4SDimitry Andric   dictionary_sp->AddBooleanItem("debugger_specific",
707fa27ce4SDimitry Andric                                 progress_data->IsDebuggerSpecific());
717fa27ce4SDimitry Andric 
727fa27ce4SDimitry Andric   return dictionary_sp;
737fa27ce4SDimitry Andric }
747fa27ce4SDimitry Andric 
GetPrefix() const75145449b1SDimitry Andric llvm::StringRef DiagnosticEventData::GetPrefix() const {
76ac9a064cSDimitry Andric   switch (m_severity) {
77ac9a064cSDimitry Andric   case Severity::eSeverityInfo:
78e3b55780SDimitry Andric     return "info";
79ac9a064cSDimitry Andric   case Severity::eSeverityWarning:
80145449b1SDimitry Andric     return "warning";
81ac9a064cSDimitry Andric   case Severity::eSeverityError:
82145449b1SDimitry Andric     return "error";
83145449b1SDimitry Andric   }
84145449b1SDimitry Andric   llvm_unreachable("Fully covered switch above!");
85145449b1SDimitry Andric }
86145449b1SDimitry Andric 
Dump(Stream * s) const87145449b1SDimitry Andric void DiagnosticEventData::Dump(Stream *s) const {
88ac9a064cSDimitry Andric   llvm::HighlightColor color = m_severity == lldb::eSeverityWarning
89145449b1SDimitry Andric                                    ? llvm::HighlightColor::Warning
90145449b1SDimitry Andric                                    : llvm::HighlightColor::Error;
91145449b1SDimitry Andric   llvm::WithColor(s->AsRawOstream(), color, llvm::ColorMode::Enable)
92145449b1SDimitry Andric       << GetPrefix();
93145449b1SDimitry Andric   *s << ": " << GetMessage() << '\n';
94145449b1SDimitry Andric   s->Flush();
95145449b1SDimitry Andric }
96145449b1SDimitry Andric 
GetFlavorString()977fa27ce4SDimitry Andric llvm::StringRef DiagnosticEventData::GetFlavorString() {
987fa27ce4SDimitry Andric   return "DiagnosticEventData";
99145449b1SDimitry Andric }
100145449b1SDimitry Andric 
GetFlavor() const1017fa27ce4SDimitry Andric llvm::StringRef DiagnosticEventData::GetFlavor() const {
102145449b1SDimitry Andric   return DiagnosticEventData::GetFlavorString();
103145449b1SDimitry Andric }
104145449b1SDimitry Andric 
105145449b1SDimitry Andric const DiagnosticEventData *
GetEventDataFromEvent(const Event * event_ptr)106145449b1SDimitry Andric DiagnosticEventData::GetEventDataFromEvent(const Event *event_ptr) {
107145449b1SDimitry Andric   return GetEventDataFromEventImpl<DiagnosticEventData>(event_ptr);
108145449b1SDimitry Andric }
109e3b55780SDimitry Andric 
1107fa27ce4SDimitry Andric StructuredData::DictionarySP
GetAsStructuredData(const Event * event_ptr)1117fa27ce4SDimitry Andric DiagnosticEventData::GetAsStructuredData(const Event *event_ptr) {
1127fa27ce4SDimitry Andric   const DiagnosticEventData *diagnostic_data =
1137fa27ce4SDimitry Andric       DiagnosticEventData::GetEventDataFromEvent(event_ptr);
1147fa27ce4SDimitry Andric 
1157fa27ce4SDimitry Andric   if (!diagnostic_data)
1167fa27ce4SDimitry Andric     return {};
1177fa27ce4SDimitry Andric 
1187fa27ce4SDimitry Andric   auto dictionary_sp = std::make_shared<StructuredData::Dictionary>();
1197fa27ce4SDimitry Andric   dictionary_sp->AddStringItem("message", diagnostic_data->GetMessage());
1207fa27ce4SDimitry Andric   dictionary_sp->AddStringItem("type", diagnostic_data->GetPrefix());
1217fa27ce4SDimitry Andric   dictionary_sp->AddBooleanItem("debugger_specific",
1227fa27ce4SDimitry Andric                                 diagnostic_data->IsDebuggerSpecific());
1237fa27ce4SDimitry Andric   return dictionary_sp;
124e3b55780SDimitry Andric }
125e3b55780SDimitry Andric 
GetFlavorString()1267fa27ce4SDimitry Andric llvm::StringRef SymbolChangeEventData::GetFlavorString() {
1277fa27ce4SDimitry Andric   return "SymbolChangeEventData";
1287fa27ce4SDimitry Andric }
1297fa27ce4SDimitry Andric 
GetFlavor() const1307fa27ce4SDimitry Andric llvm::StringRef SymbolChangeEventData::GetFlavor() const {
131e3b55780SDimitry Andric   return SymbolChangeEventData::GetFlavorString();
132e3b55780SDimitry Andric }
133e3b55780SDimitry Andric 
134e3b55780SDimitry Andric const SymbolChangeEventData *
GetEventDataFromEvent(const Event * event_ptr)135e3b55780SDimitry Andric SymbolChangeEventData::GetEventDataFromEvent(const Event *event_ptr) {
136e3b55780SDimitry Andric   return GetEventDataFromEventImpl<SymbolChangeEventData>(event_ptr);
137e3b55780SDimitry Andric }
138e3b55780SDimitry Andric 
DoOnRemoval(Event * event_ptr)139e3b55780SDimitry Andric void SymbolChangeEventData::DoOnRemoval(Event *event_ptr) {
140e3b55780SDimitry Andric   DebuggerSP debugger_sp(m_debugger_wp.lock());
141e3b55780SDimitry Andric   if (!debugger_sp)
142e3b55780SDimitry Andric     return;
143e3b55780SDimitry Andric 
144e3b55780SDimitry Andric   for (TargetSP target_sp : debugger_sp->GetTargetList().Targets()) {
145e3b55780SDimitry Andric     if (ModuleSP module_sp =
146e3b55780SDimitry Andric             target_sp->GetImages().FindModule(m_module_spec.GetUUID())) {
147e3b55780SDimitry Andric       {
148e3b55780SDimitry Andric         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
149e3b55780SDimitry Andric         if (!module_sp->GetSymbolFileFileSpec())
150e3b55780SDimitry Andric           module_sp->SetSymbolFileFileSpec(m_module_spec.GetSymbolFileSpec());
151e3b55780SDimitry Andric       }
152e3b55780SDimitry Andric       ModuleList module_list;
153e3b55780SDimitry Andric       module_list.Append(module_sp);
154e3b55780SDimitry Andric       target_sp->SymbolsDidLoad(module_list);
155e3b55780SDimitry Andric     }
156e3b55780SDimitry Andric   }
157e3b55780SDimitry Andric }
158