181ad6265SDimitry Andric //===-- DebuggerEvents.cpp ------------------------------------------------===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric 
981ad6265SDimitry Andric #include "lldb/Core/DebuggerEvents.h"
10bdd1243dSDimitry Andric #include "lldb/Core/Debugger.h"
11bdd1243dSDimitry Andric #include "lldb/Core/Module.h"
1281ad6265SDimitry Andric #include "llvm/Support/WithColor.h"
1381ad6265SDimitry Andric 
1481ad6265SDimitry Andric using namespace lldb_private;
15bdd1243dSDimitry Andric using namespace lldb;
1681ad6265SDimitry Andric 
1781ad6265SDimitry Andric template <typename T>
GetEventDataFromEventImpl(const Event * event_ptr)1881ad6265SDimitry Andric static const T *GetEventDataFromEventImpl(const Event *event_ptr) {
1981ad6265SDimitry Andric   if (event_ptr)
2081ad6265SDimitry Andric     if (const EventData *event_data = event_ptr->GetData())
2181ad6265SDimitry Andric       if (event_data->GetFlavor() == T::GetFlavorString())
2281ad6265SDimitry Andric         return static_cast<const T *>(event_ptr->GetData());
2381ad6265SDimitry Andric   return nullptr;
2481ad6265SDimitry Andric }
2581ad6265SDimitry Andric 
GetFlavorString()26*fe013be4SDimitry Andric llvm::StringRef ProgressEventData::GetFlavorString() {
27*fe013be4SDimitry Andric   return "ProgressEventData";
2881ad6265SDimitry Andric }
2981ad6265SDimitry Andric 
GetFlavor() const30*fe013be4SDimitry Andric llvm::StringRef ProgressEventData::GetFlavor() const {
3181ad6265SDimitry Andric   return ProgressEventData::GetFlavorString();
3281ad6265SDimitry Andric }
3381ad6265SDimitry Andric 
Dump(Stream * s) const3481ad6265SDimitry Andric void ProgressEventData::Dump(Stream *s) const {
35*fe013be4SDimitry Andric   s->Printf(" id = %" PRIu64 ", title = \"%s\"", m_id, m_title.c_str());
36*fe013be4SDimitry Andric   if (!m_details.empty())
37*fe013be4SDimitry Andric     s->Printf(", details = \"%s\"", m_details.c_str());
3881ad6265SDimitry Andric   if (m_completed == 0 || m_completed == m_total)
3981ad6265SDimitry Andric     s->Printf(", type = %s", m_completed == 0 ? "start" : "end");
4081ad6265SDimitry Andric   else
4181ad6265SDimitry Andric     s->PutCString(", type = update");
4281ad6265SDimitry Andric   // If m_total is UINT64_MAX, there is no progress to report, just "start"
4381ad6265SDimitry Andric   // and "end". If it isn't we will show the completed and total amounts.
4481ad6265SDimitry Andric   if (m_total != UINT64_MAX)
4581ad6265SDimitry Andric     s->Printf(", progress = %" PRIu64 " of %" PRIu64, m_completed, m_total);
4681ad6265SDimitry Andric }
4781ad6265SDimitry Andric 
4881ad6265SDimitry Andric const ProgressEventData *
GetEventDataFromEvent(const Event * event_ptr)4981ad6265SDimitry Andric ProgressEventData::GetEventDataFromEvent(const Event *event_ptr) {
5081ad6265SDimitry Andric   return GetEventDataFromEventImpl<ProgressEventData>(event_ptr);
5181ad6265SDimitry Andric }
5281ad6265SDimitry Andric 
53*fe013be4SDimitry Andric StructuredData::DictionarySP
GetAsStructuredData(const Event * event_ptr)54*fe013be4SDimitry Andric ProgressEventData::GetAsStructuredData(const Event *event_ptr) {
55*fe013be4SDimitry Andric   const ProgressEventData *progress_data =
56*fe013be4SDimitry Andric       ProgressEventData::GetEventDataFromEvent(event_ptr);
57*fe013be4SDimitry Andric 
58*fe013be4SDimitry Andric   if (!progress_data)
59*fe013be4SDimitry Andric     return {};
60*fe013be4SDimitry Andric 
61*fe013be4SDimitry Andric   auto dictionary_sp = std::make_shared<StructuredData::Dictionary>();
62*fe013be4SDimitry Andric   dictionary_sp->AddStringItem("title", progress_data->GetTitle());
63*fe013be4SDimitry Andric   dictionary_sp->AddStringItem("details", progress_data->GetDetails());
64*fe013be4SDimitry Andric   dictionary_sp->AddStringItem("message", progress_data->GetMessage());
65*fe013be4SDimitry Andric   dictionary_sp->AddIntegerItem("progress_id", progress_data->GetID());
66*fe013be4SDimitry Andric   dictionary_sp->AddIntegerItem("completed", progress_data->GetCompleted());
67*fe013be4SDimitry Andric   dictionary_sp->AddIntegerItem("total", progress_data->GetTotal());
68*fe013be4SDimitry Andric   dictionary_sp->AddBooleanItem("debugger_specific",
69*fe013be4SDimitry Andric                                 progress_data->IsDebuggerSpecific());
70*fe013be4SDimitry Andric 
71*fe013be4SDimitry Andric   return dictionary_sp;
72*fe013be4SDimitry Andric }
73*fe013be4SDimitry Andric 
GetPrefix() const7481ad6265SDimitry Andric llvm::StringRef DiagnosticEventData::GetPrefix() const {
7581ad6265SDimitry Andric   switch (m_type) {
76bdd1243dSDimitry Andric   case Type::Info:
77bdd1243dSDimitry Andric     return "info";
7881ad6265SDimitry Andric   case Type::Warning:
7981ad6265SDimitry Andric     return "warning";
8081ad6265SDimitry Andric   case Type::Error:
8181ad6265SDimitry Andric     return "error";
8281ad6265SDimitry Andric   }
8381ad6265SDimitry Andric   llvm_unreachable("Fully covered switch above!");
8481ad6265SDimitry Andric }
8581ad6265SDimitry Andric 
Dump(Stream * s) const8681ad6265SDimitry Andric void DiagnosticEventData::Dump(Stream *s) const {
8781ad6265SDimitry Andric   llvm::HighlightColor color = m_type == Type::Warning
8881ad6265SDimitry Andric                                    ? llvm::HighlightColor::Warning
8981ad6265SDimitry Andric                                    : llvm::HighlightColor::Error;
9081ad6265SDimitry Andric   llvm::WithColor(s->AsRawOstream(), color, llvm::ColorMode::Enable)
9181ad6265SDimitry Andric       << GetPrefix();
9281ad6265SDimitry Andric   *s << ": " << GetMessage() << '\n';
9381ad6265SDimitry Andric   s->Flush();
9481ad6265SDimitry Andric }
9581ad6265SDimitry Andric 
GetFlavorString()96*fe013be4SDimitry Andric llvm::StringRef DiagnosticEventData::GetFlavorString() {
97*fe013be4SDimitry Andric   return "DiagnosticEventData";
9881ad6265SDimitry Andric }
9981ad6265SDimitry Andric 
GetFlavor() const100*fe013be4SDimitry Andric llvm::StringRef DiagnosticEventData::GetFlavor() const {
10181ad6265SDimitry Andric   return DiagnosticEventData::GetFlavorString();
10281ad6265SDimitry Andric }
10381ad6265SDimitry Andric 
10481ad6265SDimitry Andric const DiagnosticEventData *
GetEventDataFromEvent(const Event * event_ptr)10581ad6265SDimitry Andric DiagnosticEventData::GetEventDataFromEvent(const Event *event_ptr) {
10681ad6265SDimitry Andric   return GetEventDataFromEventImpl<DiagnosticEventData>(event_ptr);
10781ad6265SDimitry Andric }
108bdd1243dSDimitry Andric 
109*fe013be4SDimitry Andric StructuredData::DictionarySP
GetAsStructuredData(const Event * event_ptr)110*fe013be4SDimitry Andric DiagnosticEventData::GetAsStructuredData(const Event *event_ptr) {
111*fe013be4SDimitry Andric   const DiagnosticEventData *diagnostic_data =
112*fe013be4SDimitry Andric       DiagnosticEventData::GetEventDataFromEvent(event_ptr);
113*fe013be4SDimitry Andric 
114*fe013be4SDimitry Andric   if (!diagnostic_data)
115*fe013be4SDimitry Andric     return {};
116*fe013be4SDimitry Andric 
117*fe013be4SDimitry Andric   auto dictionary_sp = std::make_shared<StructuredData::Dictionary>();
118*fe013be4SDimitry Andric   dictionary_sp->AddStringItem("message", diagnostic_data->GetMessage());
119*fe013be4SDimitry Andric   dictionary_sp->AddStringItem("type", diagnostic_data->GetPrefix());
120*fe013be4SDimitry Andric   dictionary_sp->AddBooleanItem("debugger_specific",
121*fe013be4SDimitry Andric                                 diagnostic_data->IsDebuggerSpecific());
122*fe013be4SDimitry Andric   return dictionary_sp;
123bdd1243dSDimitry Andric }
124bdd1243dSDimitry Andric 
GetFlavorString()125*fe013be4SDimitry Andric llvm::StringRef SymbolChangeEventData::GetFlavorString() {
126*fe013be4SDimitry Andric   return "SymbolChangeEventData";
127*fe013be4SDimitry Andric }
128*fe013be4SDimitry Andric 
GetFlavor() const129*fe013be4SDimitry Andric llvm::StringRef SymbolChangeEventData::GetFlavor() const {
130bdd1243dSDimitry Andric   return SymbolChangeEventData::GetFlavorString();
131bdd1243dSDimitry Andric }
132bdd1243dSDimitry Andric 
133bdd1243dSDimitry Andric const SymbolChangeEventData *
GetEventDataFromEvent(const Event * event_ptr)134bdd1243dSDimitry Andric SymbolChangeEventData::GetEventDataFromEvent(const Event *event_ptr) {
135bdd1243dSDimitry Andric   return GetEventDataFromEventImpl<SymbolChangeEventData>(event_ptr);
136bdd1243dSDimitry Andric }
137bdd1243dSDimitry Andric 
DoOnRemoval(Event * event_ptr)138bdd1243dSDimitry Andric void SymbolChangeEventData::DoOnRemoval(Event *event_ptr) {
139bdd1243dSDimitry Andric   DebuggerSP debugger_sp(m_debugger_wp.lock());
140bdd1243dSDimitry Andric   if (!debugger_sp)
141bdd1243dSDimitry Andric     return;
142bdd1243dSDimitry Andric 
143bdd1243dSDimitry Andric   for (TargetSP target_sp : debugger_sp->GetTargetList().Targets()) {
144bdd1243dSDimitry Andric     if (ModuleSP module_sp =
145bdd1243dSDimitry Andric             target_sp->GetImages().FindModule(m_module_spec.GetUUID())) {
146bdd1243dSDimitry Andric       {
147bdd1243dSDimitry Andric         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
148bdd1243dSDimitry Andric         if (!module_sp->GetSymbolFileFileSpec())
149bdd1243dSDimitry Andric           module_sp->SetSymbolFileFileSpec(m_module_spec.GetSymbolFileSpec());
150bdd1243dSDimitry Andric       }
151bdd1243dSDimitry Andric       ModuleList module_list;
152bdd1243dSDimitry Andric       module_list.Append(module_sp);
153bdd1243dSDimitry Andric       target_sp->SymbolsDidLoad(module_list);
154bdd1243dSDimitry Andric     }
155bdd1243dSDimitry Andric   }
156bdd1243dSDimitry Andric }
157