1 //===-- MIDriver.h ----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 // Third party headers
15 #include "MICmdData.h"
16 #include "MICmnBase.h"
17 #include "MICmnConfig.h"
18 #include "MICmnStreamStdin.h"
19 #include "MIDriverBase.h"
20 #include "MIDriverMgr.h"
21 #include "MIUtilSingletonBase.h"
24 class CMICmnLLDBDebugger;
25 class CMICmnStreamStdout;
28 //============================================================================
29 // Details: MI driver implementation class. A singleton class derived from
30 // LLDB SBBroadcaster class. Register the instance of *this class with
31 // the CMIDriverMgr. The CMIDriverMgr sets the driver(s) of to start
32 // work depending on the one selected to work. A driver can if not able
33 // to handle an instruction or 'command' can pass that command onto
34 // another driver object registered with the Driver Manager.
36 class CMIDriver : public CMICmnBase,
37 public CMIDriverMgr::IDriver,
39 public MI::ISingleton<CMIDriver> {
40 friend class MI::ISingleton<CMIDriver>;
44 //++ ----------------------------------------------------------------------
45 // Details: The MI Driver has a running state which is used to help determine
46 // which specific action(s) it should take or not allow.
47 // The driver when operational and not shutting down alternates
48 // between eDriverState_RunningNotDebugging and
49 // eDriverState_RunningDebugging. eDriverState_RunningNotDebugging
50 // is normally set when a breakpoint is hit or halted.
51 // eDriverState_RunningDebugging is normally set when "exec-continue"
52 // or "exec-run" is issued.
55 eDriverState_NotRunning = 0, // The MI Driver is not operating
56 eDriverState_Initialising, // The MI Driver is setting itself up
57 eDriverState_RunningNotDebugging, // The MI Driver is operational acting on
58 // any MI commands sent to it
59 eDriverState_RunningDebugging, // The MI Driver is currently overseeing an
60 // inferior program that is running
61 eDriverState_ShuttingDown, // The MI Driver is tearing down resources and
63 eDriverState_count // Always last
69 bool Initialize() override;
70 bool Shutdown() override;
73 bool GetExitApplicationFlag() const;
74 DriverState_e GetCurrentDriverState() const;
75 bool SetDriverStateRunningNotDebugging();
76 bool SetDriverStateRunningDebugging();
77 void SetDriverDebuggingArgExecutable();
78 bool IsDriverDebuggingArgExecutable() const;
80 // MI information about itself
81 const CMIUtilString &GetAppNameShort() const;
82 const CMIUtilString &GetAppNameLong() const;
83 const CMIUtilString &GetVersionDescription() const;
86 bool WriteMessageToLog(const CMIUtilString &vMessage);
87 bool SetEnableFallThru(const bool vbYes);
88 bool GetEnableFallThru() const;
89 bool HaveExecutableFileNamePathOnCmdLine() const;
90 const CMIUtilString &GetExecutableFileNamePathOnCmdLine() const;
94 // From CMIDriverMgr::IDriver
95 bool DoInitialize() override;
96 bool DoShutdown() override;
97 bool DoMainLoop() override;
98 lldb::SBError DoParseArgs(const int argc, const char *argv[], FILE *vpStdOut,
99 bool &vwbExiting) override;
100 CMIUtilString GetError() const override;
101 const CMIUtilString &GetName() const override;
102 lldb::SBDebugger &GetTheDebugger() override;
103 bool GetDriverIsGDBMICompatibleDriver() const override;
104 bool SetId(const CMIUtilString &vId) override;
105 const CMIUtilString &GetId() const override;
106 // From CMIDriverBase
107 void SetExitApplicationFlag(const bool vbForceExit) override;
108 bool DoFallThruToAnotherDriver(const CMIUtilString &vCmd,
109 CMIUtilString &vwErrMsg) override;
110 bool SetDriverToFallThruTo(const CMIDriverBase &vrOtherDriver) override;
111 FILE *GetStdin() const override;
112 FILE *GetStdout() const override;
113 FILE *GetStderr() const override;
114 const CMIUtilString &GetDriverName() const override;
115 const CMIUtilString &GetDriverId() const override;
116 void DeliverSignal(int signal) override;
120 typedef std::queue<CMIUtilString> QueueStdinLine_t;
124 /* ctor */ CMIDriver();
125 /* ctor */ CMIDriver(const CMIDriver &);
126 void operator=(const CMIDriver &);
128 lldb::SBError ParseArgs(const int argc, const char *argv[], FILE *vpStdOut,
131 bool InterpretCommand(const CMIUtilString &vTextLine);
132 bool InterpretCommandThisDriver(const CMIUtilString &vTextLine,
133 bool &vwbCmdYesValid);
135 WrapCLICommandIntoMICommand(const CMIUtilString &vTextLine) const;
136 bool InterpretCommandFallThruDriver(const CMIUtilString &vTextLine,
137 bool &vwbCmdYesValid);
138 bool ExecuteCommand(const SMICmdData &vCmdData);
139 bool StartWorkerThreads();
140 bool StopWorkerThreads();
141 bool InitClientIDEToMIDriver() const;
142 bool InitClientIDEEclipse() const;
143 bool LocalDebugSessionStartupExecuteCommands();
144 bool ExecuteCommandFile(const bool vbAsyncMode);
149 /* dtor */ ~CMIDriver() override;
153 static const CMIUtilString ms_constAppNameShort;
154 static const CMIUtilString ms_constAppNameLong;
155 static const CMIUtilString ms_constMIVersion;
157 bool m_bFallThruToOtherDriverEnabled; // True = yes fall through, false = do
158 // not pass on command
159 CMIUtilThreadMutex m_threadMutex;
160 bool m_bDriverIsExiting; // True = yes, driver told to quit, false = continue
162 void *m_handleMainThread; // *this driver is run by the main thread
163 CMICmnStreamStdin &m_rStdin;
164 CMICmnLLDBDebugger &m_rLldbDebugger;
165 CMICmnStreamStdout &m_rStdOut;
166 DriverState_e m_eCurrentDriverState;
167 bool m_bHaveExecutableFileNamePathOnCmdLine; // True = yes, executable given
168 // as one of the parameters to
169 // the MI Driver, false = not
171 CMIUtilString m_strCmdLineArgExecuteableFileNamePath;
172 bool m_bDriverDebuggingArgExecutable; // True = the MI Driver (MI mode) is
173 // debugging executable passed as
175 // false = running via a client (e.g. Eclipse)
176 bool m_bHaveCommandFileNamePathOnCmdLine; // True = file with initial commands
177 // given as one of the parameters to
178 // the MI Driver, false = not found
179 CMIUtilString m_strCmdLineArgCommandFileNamePath;