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 ConstString ProgressEventData::GetFlavorString() {
27   static ConstString g_flavor("ProgressEventData");
28   return g_flavor;
29 }
30 
31 ConstString ProgressEventData::GetFlavor() const {
32   return ProgressEventData::GetFlavorString();
33 }
34 
35 void ProgressEventData::Dump(Stream *s) const {
36   s->Printf(" id = %" PRIu64 ", message = \"%s\"", m_id, m_message.c_str());
37   if (m_completed == 0 || m_completed == m_total)
38     s->Printf(", type = %s", m_completed == 0 ? "start" : "end");
39   else
40     s->PutCString(", type = update");
41   // If m_total is UINT64_MAX, there is no progress to report, just "start"
42   // and "end". If it isn't we will show the completed and total amounts.
43   if (m_total != UINT64_MAX)
44     s->Printf(", progress = %" PRIu64 " of %" PRIu64, m_completed, m_total);
45 }
46 
47 const ProgressEventData *
48 ProgressEventData::GetEventDataFromEvent(const Event *event_ptr) {
49   return GetEventDataFromEventImpl<ProgressEventData>(event_ptr);
50 }
51 
52 llvm::StringRef DiagnosticEventData::GetPrefix() const {
53   switch (m_type) {
54   case Type::Info:
55     return "info";
56   case Type::Warning:
57     return "warning";
58   case Type::Error:
59     return "error";
60   }
61   llvm_unreachable("Fully covered switch above!");
62 }
63 
64 void DiagnosticEventData::Dump(Stream *s) const {
65   llvm::HighlightColor color = m_type == Type::Warning
66                                    ? llvm::HighlightColor::Warning
67                                    : llvm::HighlightColor::Error;
68   llvm::WithColor(s->AsRawOstream(), color, llvm::ColorMode::Enable)
69       << GetPrefix();
70   *s << ": " << GetMessage() << '\n';
71   s->Flush();
72 }
73 
74 ConstString DiagnosticEventData::GetFlavorString() {
75   static ConstString g_flavor("DiagnosticEventData");
76   return g_flavor;
77 }
78 
79 ConstString DiagnosticEventData::GetFlavor() const {
80   return DiagnosticEventData::GetFlavorString();
81 }
82 
83 const DiagnosticEventData *
84 DiagnosticEventData::GetEventDataFromEvent(const Event *event_ptr) {
85   return GetEventDataFromEventImpl<DiagnosticEventData>(event_ptr);
86 }
87 
88 ConstString SymbolChangeEventData::GetFlavorString() {
89   static ConstString g_flavor("SymbolChangeEventData");
90   return g_flavor;
91 }
92 
93 ConstString SymbolChangeEventData::GetFlavor() const {
94   return SymbolChangeEventData::GetFlavorString();
95 }
96 
97 const SymbolChangeEventData *
98 SymbolChangeEventData::GetEventDataFromEvent(const Event *event_ptr) {
99   return GetEventDataFromEventImpl<SymbolChangeEventData>(event_ptr);
100 }
101 
102 void SymbolChangeEventData::DoOnRemoval(Event *event_ptr) {
103   DebuggerSP debugger_sp(m_debugger_wp.lock());
104   if (!debugger_sp)
105     return;
106 
107   for (TargetSP target_sp : debugger_sp->GetTargetList().Targets()) {
108     if (ModuleSP module_sp =
109             target_sp->GetImages().FindModule(m_module_spec.GetUUID())) {
110       {
111         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
112         if (!module_sp->GetSymbolFileFileSpec())
113           module_sp->SetSymbolFileFileSpec(m_module_spec.GetSymbolFileSpec());
114       }
115       ModuleList module_list;
116       module_list.Append(module_sp);
117       target_sp->SymbolsDidLoad(module_list);
118     }
119   }
120 }
121