1 //===-- MICmnLLDBDebugger.h -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #pragma once 11 12 // Third party headers 13 #include "lldb/API/SBDebugger.h" 14 #include "lldb/API/SBEvent.h" 15 #include "lldb/API/SBListener.h" 16 #include <condition_variable> 17 #include <map> 18 #include <mutex> 19 20 // In-house headers: 21 #include "MICmnBase.h" 22 #include "MIUtilSingletonBase.h" 23 #include "MIUtilThreadBaseStd.h" 24 25 // Declarations: 26 class CMIDriverBase; 27 class CMICmnLLDBDebuggerHandleEvents; 28 29 //++ 30 //============================================================================ 31 // Details: MI proxy/adapter for the LLDB public SBDebugger API. The CMIDriver 32 // requires *this object. Command classes make calls on *this object 33 // to facilitate their work effort. The instance runs in its own worker 34 // thread. 35 // A singleton class. 36 //-- 37 class CMICmnLLDBDebugger : public CMICmnBase, 38 public CMIUtilThreadActiveObjBase, 39 public MI::ISingleton<CMICmnLLDBDebugger> { 40 friend class MI::ISingleton<CMICmnLLDBDebugger>; 41 42 // Methods: 43 public: 44 bool Initialize() override; 45 bool Shutdown() override; 46 47 bool SetDriver(const CMIDriverBase &vClientDriver); 48 CMIDriverBase &GetDriver() const; 49 lldb::SBDebugger &GetTheDebugger(); 50 lldb::SBListener &GetTheListener(); 51 void WaitForHandleEvent(); 52 bool CheckIfNeedToRebroadcastStopEvent(); 53 void RebroadcastStopEvent(); 54 55 // MI Commands can use these functions to listen for events they require 56 bool RegisterForEvent(const CMIUtilString &vClientName, 57 const CMIUtilString &vBroadcasterClass, 58 const MIuint vEventMask); 59 bool UnregisterForEvent(const CMIUtilString &vClientName, 60 const CMIUtilString &vBroadcasterClass); 61 bool RegisterForEvent(const CMIUtilString &vClientName, 62 const lldb::SBBroadcaster &vBroadcaster, 63 const MIuint vEventMask); 64 bool UnregisterForEvent(const CMIUtilString &vClientName, 65 const lldb::SBBroadcaster &vBroadcaster); 66 67 // Overridden: 68 public: 69 // From CMIUtilThreadActiveObjBase 70 const CMIUtilString &ThreadGetName() const override; 71 72 // Overridden: 73 protected: 74 // From CMIUtilThreadActiveObjBase 75 bool ThreadRun(bool &vrIsAlive) override; 76 bool ThreadFinish() override; 77 78 // Typedefs: 79 private: 80 typedef std::map<CMIUtilString, MIuint> MapBroadcastClassNameToEventMask_t; 81 typedef std::pair<CMIUtilString, MIuint> 82 MapPairBroadcastClassNameToEventMask_t; 83 typedef std::map<CMIUtilString, MIuint> MapIdToEventMask_t; 84 typedef std::pair<CMIUtilString, MIuint> MapPairIdToEventMask_t; 85 86 // Methods: 87 private: 88 /* ctor */ CMICmnLLDBDebugger(); 89 /* ctor */ CMICmnLLDBDebugger(const CMICmnLLDBDebugger &); 90 void operator=(const CMICmnLLDBDebugger &); 91 92 bool InitSBDebugger(); 93 bool InitSBListener(); 94 bool InitStdStreams(); 95 bool MonitorSBListenerEvents(bool &vrbYesExit); 96 97 bool BroadcasterGetMask(const CMIUtilString &vBroadcasterClass, 98 MIuint &vEventMask) const; 99 bool BroadcasterRemoveMask(const CMIUtilString &vBroadcasterClass); 100 bool BroadcasterSaveMask(const CMIUtilString &vBroadcasterClass, 101 const MIuint vEventMask); 102 103 MIuint 104 ClientGetMaskForAllClients(const CMIUtilString &vBroadcasterClass) const; 105 bool ClientSaveMask(const CMIUtilString &vClientName, 106 const CMIUtilString &vBroadcasterClass, 107 const MIuint vEventMask); 108 bool ClientRemoveTheirMask(const CMIUtilString &vClientName, 109 const CMIUtilString &vBroadcasterClass); 110 bool ClientGetTheirMask(const CMIUtilString &vClientName, 111 const CMIUtilString &vBroadcasterClass, 112 MIuint &vwEventMask); 113 bool LoadMIFormatters(lldb::SBTypeCategory miCategory); 114 bool RegisterMISummaryProviders(); 115 // Overridden: 116 private: 117 // From CMICmnBase 118 /* dtor */ ~CMICmnLLDBDebugger() override; 119 120 // Attributes: 121 private: 122 CMIDriverBase 123 *m_pClientDriver; // The driver that wants to use *this LLDB debugger 124 lldb::SBDebugger m_lldbDebugger; // SBDebugger is the primordial object that 125 // creates SBTargets and provides access to 126 // them 127 lldb::SBListener m_lldbListener; // API clients can register its own listener 128 // to debugger events 129 const CMIUtilString m_constStrThisThreadId; 130 MapBroadcastClassNameToEventMask_t m_mapBroadcastClassNameToEventMask; 131 MapIdToEventMask_t m_mapIdToEventMask; 132 std::mutex m_mutexEventQueue; 133 std::condition_variable m_conditionEventQueueEmpty; 134 uint32_t m_nLastStopId; 135 }; 136