]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdBase.h
Import DTS files from Linux 5.0
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdBase.h
1 //===-- MICmdBase.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 #include <functional>
13
14 #include "lldb/API/SBError.h"
15
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"
24
25 // Declarations:
26 class CMICmnLLDBDebugSessionInfo;
27
28 //++
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
42 //          events set up.
43 //          A command's Execute(), Acknowledge() and event callback functions
44 //          are
45 //          carried out in the main thread.
46 //          A command may use the argument derived object classes
47 //          (CMICmdArgValBase)
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.
51 //--
52 class CMICmdBase : public CMICmnBase,
53                    public CMICmdInvoker::ICmd,
54                    public CMICmdFactory::ICmd {
55   // Methods:
56 public:
57   CMICmdBase();
58
59   // Overridden:
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;
72
73   virtual MIuint GetGUID();
74   void AddCommonArgs();
75
76   // Overrideable:
77   ~CMICmdBase() override;
78   virtual bool GetExitAppOnCommandFailure() const;
79
80   // Methods:
81 protected:
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();
93
94   // Attributes:
95   CMICmdFactory::CmdCreatorFnPtr m_pSelfCreatorFn;
96   CMIUtilString m_strCurrentErrDescription; // Reason for Execute or Acknowledge
97                                             // function failure
98   SMICmdData m_cmdData; // Holds information/status of *this command. Used by
99                         // other MI code to report or determine state of a
100                         // command.
101   bool m_bWaitForEventFromSBDebugger; // True = yes event type command wait,
102                                       // false = command calls Acknowledge()
103                                       // straight after Execute()
104                                       // no waiting
105   CMIUtilString
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
119                                                        // all command based
120                                                        // derived classes.
121   bool m_bHasResultRecordExtra; // True = Yes command produced additional MI
122                                 // output to its 1 line response, false = no
123                                 // extra MI output
124                                 // formed.
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;
131
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;
137 };
138
139 //++
140 //------------------------------------------------------------------------------------
141 // Details: Retrieve the command argument or option object pointer so that it
142 // can be
143 //          examined. If the option found and valid get the value (number,
144 //          string or list
145 //          - see CMICmdArgValBase class) from it to use with the command's
146 //          decision
147 //          making. If the argument is not found the command's error description
148 //          is set
149 //          describing the error condition.
150 // Type:    Template method.
151 // Args:    vStrOptionName  - (R)   The text name of the argument or option to
152 // search for in
153 //                                  the list of the command's possible arguments
154 //                                  or options.
155 // Return:  T * - CMICmdArgValBase derived object.
156 //              - nullptr = function has failed, unable to retrieve the
157 //              option/arg object.
158 // Throws:  None.
159 //--
160 template <class T>
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()));
167     return nullptr;
168   }
169
170   return static_cast<T *>(pPtrBase);
171 }
172
173 //++
174 //------------------------------------------------------------------------------------
175 // Details: Retrieve the command argument or option object pointer using
176 // template function
177 //          CMICmdBase::GetOption(). Should the argument (by name) not be found
178 //          the
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
185 //          the list of
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.
190 // Throws:  None.
191 //--
192 #define CMICMDBASE_GETOPTION(a, b, c)                                          \
193   CMICmdArgVal##b *a = CMICmdBase::GetOption<CMICmdArgVal##b>(c);              \
194   if (a == nullptr)                                                            \
195     return MIstatus::failure;
196 // This comment is to stop compile warning for #define