1 //===-- MICmdBase.h ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include "lldb/API/SBError.h"
16 #include "MICmdArgSet.h"
17 #include "MICmdData.h"
18 #include "MICmdFactory.h"
19 #include "MICmdInvoker.h"
20 #include "MICmnBase.h"
21 #include "MICmnMIResultRecord.h"
22 #include "MICmnResources.h"
23 #include "MIUtilString.h"
26 class CMICmnLLDBDebugSessionInfo;
29 //============================================================================
30 // Details: MI command base class. MI commands derive from this base class.
31 // The Command Factory creates command objects and passes them to the
32 // Command Invoker. The Invoker takes ownership of any commands created
33 // which means it is the only object to delete them when a command is
34 // finished working. Commands do not delete themselves.
35 // There are two types of command implicitly defined by the state of
36 // the m_bWaitForEventFromSBDebugger flag. There is the event type
37 // command which registers (command fn) callbacks with the SBListener
38 // does some work then wakes up again when called back, does more work
39 // perhaps, ends, then the Invoker calls the command's Acknowledge
40 // function. The other type of command is one that just does some work,
41 // ends, then the Invoker calls the command's Acknowledge function. No
43 // A command's Execute(), Acknowledge() and event callback functions
45 // carried out in the main thread.
46 // A command may use the argument derived object classes
48 // to factor handling and parsing of different types of arguments
49 // presented to a command. A command will produce an error should it
50 // be presented with arguments or options it does not understand.
52 class CMICmdBase : public CMICmnBase,
53 public CMICmdInvoker::ICmd,
54 public CMICmdFactory::ICmd {
60 // From CMICmdInvoker::ICmd
61 const SMICmdData &GetCmdData() const override;
62 const CMIUtilString &GetErrorDescription() const override;
63 void SetCmdData(const SMICmdData &vCmdData) override;
64 void CmdFinishedTellInvoker() const override;
65 const CMIUtilString &GetMIResultRecord() const override;
66 const CMIUtilString &GetMIResultRecordExtra() const override;
67 bool HasMIResultRecordExtra() const override;
68 bool ParseArgs() override;
69 // From CMICmdFactory::ICmd
70 const CMIUtilString &GetMiCmd() const override;
71 CMICmdFactory::CmdCreatorFnPtr GetCmdCreatorFn() const override;
73 virtual MIuint GetGUID();
77 ~CMICmdBase() override;
78 virtual bool GetExitAppOnCommandFailure() const;
82 void SetError(const CMIUtilString &rErrMsg);
83 bool HandleSBError(const lldb::SBError &error,
84 const std::function<bool()> &successHandler =
85 [] { return MIstatus::success; },
86 const std::function<void()> &errorHandler = [] {});
87 bool HandleSBErrorWithSuccess(const lldb::SBError &error,
88 const std::function<bool()> &successHandler);
89 bool HandleSBErrorWithFailure(const lldb::SBError &error,
90 const std::function<void()> &errorHandler);
91 template <class T> T *GetOption(const CMIUtilString &vStrOptionName);
92 bool ParseValidateCmdOptions();
95 CMICmdFactory::CmdCreatorFnPtr m_pSelfCreatorFn;
96 CMIUtilString m_strCurrentErrDescription; // Reason for Execute or Acknowledge
98 SMICmdData m_cmdData; // Holds information/status of *this command. Used by
99 // other MI code to report or determine state of a
101 bool m_bWaitForEventFromSBDebugger; // True = yes event type command wait,
102 // false = command calls Acknowledge()
103 // straight after Execute()
106 m_strMiCmd; // The MI text identifying *this command i.e. 'break-insert'
107 CMICmnMIResultRecord m_miResultRecord; // This is completed in the
108 // Acknowledge() function and returned
109 // to the Command Invoker to proceed
110 // stdout output. Each command forms 1 response to its input.
111 CMIUtilString m_miResultRecordExtra; // This is completed in the Acknowledge()
112 // function and returned to the Command
113 // Invoker to proceed
114 // stdout output. Hack command produce more response text to help the client
115 // because of using LLDB
116 CMICmnLLDBDebugSessionInfo &m_rLLDBDebugSessionInfo; // Access to command
117 // sharing information or
118 // data across any and
121 bool m_bHasResultRecordExtra; // True = Yes command produced additional MI
122 // output to its 1 line response, false = no
125 CMICmdArgSet m_setCmdArgs; // The list of arguments *this command needs to
126 // parse from the options string to carry out work.
127 const CMIUtilString m_constStrArgThreadGroup;
128 const CMIUtilString m_constStrArgThread;
129 const CMIUtilString m_constStrArgFrame;
130 const CMIUtilString m_constStrArgConsume;
132 // These 3 members can be used by the derived classes to make any of
133 // "thread", "frame" or "thread-group" mandatory.
134 bool m_ThreadGrpArgMandatory;
135 bool m_ThreadArgMandatory;
136 bool m_FrameArgMandatory;
140 //------------------------------------------------------------------------------------
141 // Details: Retrieve the command argument or option object pointer so that it
143 // examined. If the option found and valid get the value (number,
145 // - see CMICmdArgValBase class) from it to use with the command's
147 // making. If the argument is not found the command's error description
149 // describing the error condition.
150 // Type: Template method.
151 // Args: vStrOptionName - (R) The text name of the argument or option to
153 // the list of the command's possible arguments
155 // Return: T * - CMICmdArgValBase derived object.
156 // - nullptr = function has failed, unable to retrieve the
157 // option/arg object.
161 T *CMICmdBase::GetOption(const CMIUtilString &vStrOptionName) {
162 CMICmdArgValBase *pPtrBase = nullptr;
163 if (!m_setCmdArgs.GetArg(vStrOptionName, pPtrBase)) {
164 SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_OPTION_NOT_FOUND),
165 m_cmdData.strMiCmd.c_str(),
166 vStrOptionName.c_str()));
170 return static_cast<T *>(pPtrBase);
174 //------------------------------------------------------------------------------------
175 // Details: Retrieve the command argument or option object pointer using
177 // CMICmdBase::GetOption(). Should the argument (by name) not be found
179 // command will exit with a failure (set in GetOption()).
180 // Type: Preprocessor macro.
181 // Args: a - (R) The actual variable's name.
182 // b - (R) The type of variable (appended to CMICmdArgVal i.e.
183 // CMICmdArgValString).
184 // c - (R) The text name of the argument or option to search for in
186 // the command's possible arguments or options.
187 // Return: T * - CMICmdArgValBase derived object.
188 // - nullptr = function has failed, unable to retrieve the
189 // option/arg object.
192 #define CMICMDBASE_GETOPTION(a, b, c) \
193 CMICmdArgVal##b *a = CMICmdBase::GetOption<CMICmdArgVal##b>(c); \
195 return MIstatus::failure;
196 // This comment is to stop compile warning for #define