1 //===-- DebuggerEvents.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/DebuggerEvents.h"
10 #include "lldb/Core/Debugger.h"
11 #include "lldb/Core/Module.h"
12 #include "llvm/Support/WithColor.h"
13 
14 using namespace lldb_private;
15 using namespace lldb;
16 
17 template <typename T>
18 static const T *GetEventDataFromEventImpl(const Event *event_ptr) {
19   if (event_ptr)
20     if (const EventData *event_data = event_ptr->GetData())
21       if (event_data->GetFlavor() == T::GetFlavorString())
22         return static_cast<const T *>(event_ptr->GetData());
23   return nullptr;
24 }
25 
26 llvm::StringRef ProgressEventData::GetFlavorString() {
27   return "ProgressEventData";
28 }
29 
30 llvm::StringRef ProgressEventData::GetFlavor() const {
31   return ProgressEventData::GetFlavorString();
32 }
33 
34 void ProgressEventData::Dump(Stream *s) const {
35   s->Printf(" id = %" PRIu64 ", title = \"%s\"", m_id, m_title.c_str());
36   if (!m_details.empty())
37     s->Printf(", details = \"%s\"", m_details.c_str());
38   if (m_completed == 0 || m_completed == m_total)
39     s->Printf(", type = %s", m_completed == 0 ? "start" : "end");
40   else
41     s->PutCString(", type = update");
42   // If m_total is UINT64_MAX, there is no progress to report, just "start"
43   // and "end". If it isn't we will show the completed and total amounts.
44   if (m_total != UINT64_MAX)
45     s->Printf(", progress = %" PRIu64 " of %" PRIu64, m_completed, m_total);
46 }
47 
48 const ProgressEventData *
49 ProgressEventData::GetEventDataFromEvent(const Event *event_ptr) {
50   return GetEventDataFromEventImpl<ProgressEventData>(event_ptr);
51 }
52 
53 StructuredData::DictionarySP
54 ProgressEventData::GetAsStructuredData(const Event *event_ptr) {
55   const ProgressEventData *progress_data =
56       ProgressEventData::GetEventDataFromEvent(event_ptr);
57 
58   if (!progress_data)
59     return {};
60 
61   auto dictionary_sp = std::make_shared<StructuredData::Dictionary>();
62   dictionary_sp->AddStringItem("title", progress_data->GetTitle());
63   dictionary_sp->AddStringItem("details", progress_data->GetDetails());
64   dictionary_sp->AddStringItem("message", progress_data->GetMessage());
65   dictionary_sp->AddIntegerItem("progress_id", progress_data->GetID());
66   dictionary_sp->AddIntegerItem("completed", progress_data->GetCompleted());
67   dictionary_sp->AddIntegerItem("total", progress_data->GetTotal());
68   dictionary_sp->AddBooleanItem("debugger_specific",
69                                 progress_data->IsDebuggerSpecific());
70 
71   return dictionary_sp;
72 }
73 
74 llvm::StringRef DiagnosticEventData::GetPrefix() const {
75   switch (m_type) {
76   case Type::Info:
77     return "info";
78   case Type::Warning:
79     return "warning";
80   case Type::Error:
81     return "error";
82   }
83   llvm_unreachable("Fully covered switch above!");
84 }
85 
86 void DiagnosticEventData::Dump(Stream *s) const {
87   llvm::HighlightColor color = m_type == Type::Warning
88                                    ? llvm::HighlightColor::Warning
89                                    : llvm::HighlightColor::Error;
90   llvm::WithColor(s->AsRawOstream(), color, llvm::ColorMode::Enable)
91       << GetPrefix();
92   *s << ": " << GetMessage() << '\n';
93   s->Flush();
94 }
95 
96 llvm::StringRef DiagnosticEventData::GetFlavorString() {
97   return "DiagnosticEventData";
98 }
99 
100 llvm::StringRef DiagnosticEventData::GetFlavor() const {
101   return DiagnosticEventData::GetFlavorString();
102 }
103 
104 const DiagnosticEventData *
105 DiagnosticEventData::GetEventDataFromEvent(const Event *event_ptr) {
106   return GetEventDataFromEventImpl<DiagnosticEventData>(event_ptr);
107 }
108 
109 StructuredData::DictionarySP
110 DiagnosticEventData::GetAsStructuredData(const Event *event_ptr) {
111   const DiagnosticEventData *diagnostic_data =
112       DiagnosticEventData::GetEventDataFromEvent(event_ptr);
113 
114   if (!diagnostic_data)
115     return {};
116 
117   auto dictionary_sp = std::make_shared<StructuredData::Dictionary>();
118   dictionary_sp->AddStringItem("message", diagnostic_data->GetMessage());
119   dictionary_sp->AddStringItem("type", diagnostic_data->GetPrefix());
120   dictionary_sp->AddBooleanItem("debugger_specific",
121                                 diagnostic_data->IsDebuggerSpecific());
122   return dictionary_sp;
123 }
124 
125 llvm::StringRef SymbolChangeEventData::GetFlavorString() {
126   return "SymbolChangeEventData";
127 }
128 
129 llvm::StringRef SymbolChangeEventData::GetFlavor() const {
130   return SymbolChangeEventData::GetFlavorString();
131 }
132 
133 const SymbolChangeEventData *
134 SymbolChangeEventData::GetEventDataFromEvent(const Event *event_ptr) {
135   return GetEventDataFromEventImpl<SymbolChangeEventData>(event_ptr);
136 }
137 
138 void SymbolChangeEventData::DoOnRemoval(Event *event_ptr) {
139   DebuggerSP debugger_sp(m_debugger_wp.lock());
140   if (!debugger_sp)
141     return;
142 
143   for (TargetSP target_sp : debugger_sp->GetTargetList().Targets()) {
144     if (ModuleSP module_sp =
145             target_sp->GetImages().FindModule(m_module_spec.GetUUID())) {
146       {
147         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
148         if (!module_sp->GetSymbolFileFileSpec())
149           module_sp->SetSymbolFileFileSpec(m_module_spec.GetSymbolFileSpec());
150       }
151       ModuleList module_list;
152       module_list.Append(module_sp);
153       target_sp->SymbolsDidLoad(module_list);
154     }
155   }
156 }
157