brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.0 KiB · ad76b08 Raw
120 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// This file contains the declarations of the DAPSessionManager and11/// ManagedEventThread classes, which are used to multiple concurrent DAP12/// sessions in a single lldb-dap process.13///14//===----------------------------------------------------------------------===//15 16#ifndef LLDB_TOOLS_LLDB_DAP_DAPSESSIONMANAGER_H17#define LLDB_TOOLS_LLDB_DAP_DAPSESSIONMANAGER_H18 19#include "lldb/API/SBBroadcaster.h"20#include "lldb/API/SBDebugger.h"21#include "lldb/API/SBTarget.h"22#include "lldb/Host/MainLoop.h"23#include "lldb/lldb-types.h"24#include "llvm/Support/Error.h"25#include <condition_variable>26#include <map>27#include <memory>28#include <mutex>29#include <optional>30#include <thread>31#include <vector>32 33namespace lldb_dap {34 35// Forward declarations36struct DAP;37 38class ManagedEventThread {39public:40  // Constructor declaration41  ManagedEventThread(lldb::SBBroadcaster broadcaster, std::thread t);42 43  ~ManagedEventThread();44 45  ManagedEventThread(const ManagedEventThread &) = delete;46  ManagedEventThread &operator=(const ManagedEventThread &) = delete;47 48private:49  lldb::SBBroadcaster m_broadcaster;50  std::thread m_event_thread;51};52 53/// Global DAP session manager that manages multiple concurrent DAP sessions in54/// a single lldb-dap process. Handles session lifecycle tracking, coordinates55/// shared debugger event threads, and facilitates target handoff between56/// sessions for dynamically created targets.57class DAPSessionManager {58public:59  /// Get the singleton instance of the DAP session manager.60  static DAPSessionManager &GetInstance();61 62  /// Register a DAP session.63  void RegisterSession(lldb_private::MainLoop *loop, DAP *dap);64 65  /// Unregister a DAP session. Called by sessions when they complete their66  /// disconnection, which unblocks WaitForAllSessionsToDisconnect().67  void UnregisterSession(lldb_private::MainLoop *loop);68 69  /// Get all active DAP sessions.70  std::vector<DAP *> GetActiveSessions();71 72  /// Disconnect all registered sessions by calling Disconnect() on73  /// each and requesting their event loops to terminate. Used during74  /// shutdown to force all sessions to begin disconnecting.75  void DisconnectAllSessions();76 77  /// Block until all sessions disconnect and unregister. Returns an error if78  /// DisconnectAllSessions() was called and any disconnection failed.79  llvm::Error WaitForAllSessionsToDisconnect();80 81  /// Get or create event thread for a specific debugger.82  std::shared_ptr<ManagedEventThread>83  GetEventThreadForDebugger(lldb::SBDebugger debugger, DAP *requesting_dap);84 85  /// Find the DAP instance that owns the given target.86  DAP *FindDAPForTarget(lldb::SBTarget target);87 88  /// Static convenience method for FindDAPForTarget.89  static DAP *FindDAP(lldb::SBTarget target) {90    return GetInstance().FindDAPForTarget(target);91  }92 93  /// Clean up expired event threads from the collection.94  void ReleaseExpiredEventThreads();95 96private:97  DAPSessionManager() = default;98  ~DAPSessionManager() = default;99 100  // Non-copyable and non-movable.101  DAPSessionManager(const DAPSessionManager &) = delete;102  DAPSessionManager &operator=(const DAPSessionManager &) = delete;103  DAPSessionManager(DAPSessionManager &&) = delete;104  DAPSessionManager &operator=(DAPSessionManager &&) = delete;105 106  bool m_client_failed = false;107  std::mutex m_sessions_mutex;108  std::condition_variable m_sessions_condition;109  std::map<lldb_private::MainLoop *, DAP *> m_active_sessions;110 111  /// Map from debugger ID to its event thread, used when multiple DAP sessions112  /// share the same debugger instance.113  std::map<lldb::user_id_t, std::weak_ptr<ManagedEventThread>>114      m_debugger_event_threads;115};116 117} // namespace lldb_dap118 119#endif // LLDB_TOOLS_LLDB_DAP_DAPSESSIONMANAGER_H120