]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdBase.cpp
Merge lldb trunk r366426, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdBase.cpp
1 //===-- MICmdBase.cpp -------------------------------------------*- C++ -*-===//
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // In-house headers:
10 #include "MICmdBase.h"
11 #include "MICmdArgValConsume.h"
12 #include "MICmdArgValOptionLong.h"
13 #include "MICmnLLDBDebugSessionInfo.h"
14 #include "MICmnMIValueConst.h"
15
16 //++
17 // Details: CMICmdBase constructor.
18 // Type:    Method.
19 // Args:    None.
20 // Return:  None.
21 // Throws:  None.
22 //--
23 CMICmdBase::CMICmdBase()
24     : m_pSelfCreatorFn(nullptr),
25       m_rLLDBDebugSessionInfo(CMICmnLLDBDebugSessionInfo::Instance()),
26       m_bHasResultRecordExtra(false), m_constStrArgThreadGroup("thread-group"),
27       m_constStrArgThread("thread"), m_constStrArgFrame("frame"),
28       m_constStrArgConsume("--"), m_ThreadGrpArgMandatory(false),
29       m_ThreadArgMandatory(false), m_FrameArgMandatory(false) {}
30
31 //++
32 // Details: CMICmdBase destructor.
33 // Type:    Overrideable.
34 // Args:    None.
35 // Return:  None.
36 // Throws:  None.
37 //--
38 CMICmdBase::~CMICmdBase() {}
39
40 //++
41 // Details: The invoker requires this function.
42 // Type:    Overridden.
43 // Args:    None.
44 // Return:  SMICmdData & -  *this command's present status/data/information.
45 // Throws:  None.
46 //--
47 const SMICmdData &CMICmdBase::GetCmdData() const { return m_cmdData; }
48
49 //++
50 // Details: The invoker requires this function.
51 // Type:    Overridden.
52 // Args:    None.
53 // Return:  CMIUtilString & -   *this command's current error description.
54 //                              Empty string indicates command status ok.
55 // Throws:  None.
56 //--
57 const CMIUtilString &CMICmdBase::GetErrorDescription() const {
58   return m_strCurrentErrDescription;
59 }
60
61 //++
62 // Details: The CMICmdFactory requires this function. Retrieve the command and
63 // argument
64 //          options description string.
65 // Type:    Overridden.
66 // Args:    None.
67 // Return:  CMIUtilString & -   Command description.
68 // Throws:  None.
69 //--
70 const CMIUtilString &CMICmdBase::GetMiCmd() const { return m_strMiCmd; }
71
72 //++
73 // Details: Help parse the arguments that are common to all commands.
74 // Args:    None.
75 // Return:  None
76 // Throws:  None.
77 //--
78 void CMICmdBase::AddCommonArgs() {
79   m_setCmdArgs.Add(new CMICmdArgValOptionLong(
80       m_constStrArgThreadGroup, m_ThreadGrpArgMandatory, true,
81       CMICmdArgValListBase::eArgValType_ThreadGrp, 1));
82   m_setCmdArgs.Add(new CMICmdArgValOptionLong(
83       m_constStrArgThread, m_ThreadArgMandatory, true,
84       CMICmdArgValListBase::eArgValType_Number, 1));
85   m_setCmdArgs.Add(
86       new CMICmdArgValOptionLong(m_constStrArgFrame, m_FrameArgMandatory, true,
87                                  CMICmdArgValListBase::eArgValType_Number, 1));
88   m_setCmdArgs.Add(new CMICmdArgValConsume(m_constStrArgConsume, false));
89 }
90
91 //++
92 // Details: The invoker requires this function. A command must be given working
93 // data and
94 //          provide data about its status or provide information to other
95 //          objects.
96 // Type:    Overridden.
97 // Args:    None.
98 // Return:  None.
99 // Throws:  None.
100 //--
101 void CMICmdBase::SetCmdData(const SMICmdData &vCmdData) {
102   m_cmdData = vCmdData;
103 }
104
105 //++
106 // Details: The command factory requires this function. The factory calls this
107 // function
108 //          so it can obtain *this command's creation function.
109 // Type:    Overridden.
110 // Args:    None.
111 // Return:  CMICmdFactory::CmdCreatorFnPtr - Function pointer.
112 // Throws:  None.
113 //--
114 CMICmdFactory::CmdCreatorFnPtr CMICmdBase::GetCmdCreatorFn() const {
115   return m_pSelfCreatorFn;
116 }
117
118 //++
119 // Details: If a command is an event type (has callbacks registered with
120 // SBListener) it
121 //          needs to inform the Invoker that it has finished its work so that
122 //          the
123 //          Invoker can tidy up and call the commands Acknowledge function (yes
124 //          the
125 //          command itself could call the Acknowledge itself but not doing that
126 //          way).
127 // Type:    Overridden.
128 // Args:    None.
129 // Return:  None.
130 // Throws:  None.
131 //--
132 void CMICmdBase::CmdFinishedTellInvoker() const {
133   CMICmdInvoker::Instance().CmdExecuteFinished(const_cast<CMICmdBase &>(*this));
134 }
135
136 //++
137 // Details: Returns the final version of the MI result record built up in the
138 // command's
139 //          Acknowledge function. The one line text of MI result.
140 // Type:    Overridden.
141 // Args:    None.
142 // Return:  CMIUtilString & - MI text version of the MI result record.
143 // Throws:  None.
144 //--
145 const CMIUtilString &CMICmdBase::GetMIResultRecord() const {
146   return m_miResultRecord.GetString();
147 }
148
149 //++
150 // Details: Retrieve from the command additional MI result to its 1 line
151 // response.
152 //          Because of using LLDB additional 'fake'/hack output is sometimes
153 //          required to
154 //          help the driver client operate i.e. Eclipse.
155 // Type:    Overridden.
156 // Args:    None.
157 // Return:  CMIUtilString & - MI text version of the MI result record.
158 // Throws:  None.
159 //--
160 const CMIUtilString &CMICmdBase::GetMIResultRecordExtra() const {
161   return m_miResultRecordExtra;
162 }
163
164 //++
165 // Details: Hss *this command got additional MI result to its 1 line response.
166 //          Because of using LLDB additional 'fake'/hack output is sometimes
167 //          required to
168 //          help the driver client operate i.e. Eclipse.
169 // Type:    Overridden.
170 // Args:    None.
171 // Return:  bool    - True = Yes have additional MI output, false = no nothing
172 // extra.
173 // Throws:  None.
174 //--
175 bool CMICmdBase::HasMIResultRecordExtra() const {
176   return m_bHasResultRecordExtra;
177 }
178
179 //++
180 // Details: Short cut function to enter error information into the command's
181 // metadata
182 //          object and set the command's error status.
183 // Type:    Method.
184 // Args:    rErrMsg - (R) Status description.
185 // Return:  None.
186 // Throws:  None.
187 //--
188 void CMICmdBase::SetError(const CMIUtilString &rErrMsg) {
189   m_cmdData.bCmdValid = false;
190   m_cmdData.strErrorDescription = rErrMsg;
191   m_cmdData.bCmdExecutedSuccessfully = false;
192
193   const CMICmnMIValueResult valueResult("msg", CMICmnMIValueConst(rErrMsg));
194   const CMICmnMIResultRecord miResultRecord(
195       m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Error,
196       valueResult);
197   m_miResultRecord = miResultRecord;
198   m_cmdData.strMiCmdResultRecord = miResultRecord.GetString();
199 }
200
201 //++
202 // Details: Short cut function to check MI command's execute status and
203 //          set an error in case of failure.
204 // Type:    Method.
205 // Args:    error - (R) Error description object.
206 //          successHandler - (R) function describing actions to execute
207 //          in case of success state of passed SBError object.
208 //          errorHandler - (R) function describing actions to execute
209 //          in case of fail status of passed SBError object.
210 // Return:  bool.
211 // Throws:  None.
212 //--
213 bool CMICmdBase::HandleSBError(const lldb::SBError &error,
214                                const std::function<bool()> &successHandler,
215                                const std::function<void()> &errorHandler) {
216   if (error.Success())
217     return successHandler();
218
219   SetError(error.GetCString());
220   errorHandler();
221   return MIstatus::failure;
222 }
223
224 //++
225 // Details: Short cut function to check MI command's execute status and
226 //          call specified handler function for success case.
227 // Type:    Method.
228 // Args:    error - (R) Error description object.
229 //          successHandler - (R) function describing actions to execute
230 //          in case of success state of passed SBError object.
231 // Return:  bool.
232 // Throws:  None.
233 //--
234 bool CMICmdBase::HandleSBErrorWithSuccess(
235     const lldb::SBError &error,
236     const std::function<bool()> &successHandler) {
237   return HandleSBError(error, successHandler);
238 }
239
240 //++
241 // Details: Short cut function to check MI command's execute status and
242 //          call specified handler function for error case.
243 // Type:    Method.
244 // Args:    error - (R) Error description object.
245 //          errorHandler - (R) function describing actions to execute
246 //          in case of fail status of passed SBError object.
247 // Return:  bool.
248 // Throws:  None.
249 //--
250 bool CMICmdBase::HandleSBErrorWithFailure(
251     const lldb::SBError &error,
252     const std::function<void()> &errorHandler) {
253   return HandleSBError(error, [] { return MIstatus::success; }, errorHandler);
254 }
255
256 //++
257 // Details: Ask a command to provide its unique identifier.
258 // Type:    Method.
259 // Args:    A unique identifier for this command class.
260 // Return:  None.
261 // Throws:  None.
262 //--
263 MIuint CMICmdBase::GetGUID() {
264   MIuint64 vptr = reinterpret_cast<MIuint64>(this);
265   MIuint id = (vptr)&0xFFFFFFFF;
266   id ^= (vptr >> 32) & 0xFFFFFFFF;
267
268   return id;
269 }
270
271 //++
272 // Details: The invoker requires this function. The parses the command line
273 // options
274 //          arguments to extract values for each of those arguments.
275 // Type:    Overridden.
276 // Args:    None.
277 // Return:  MIstatus::success - Functional succeeded.
278 //          MIstatus::failure - Functional failed.
279 // Throws:  None.
280 //--
281 bool CMICmdBase::ParseArgs() {
282   // Do nothing - override to implement
283
284   return MIstatus::success;
285 }
286
287 //++
288 // Details: Having previously given CMICmdArgSet m_setCmdArgs all the argument
289 // or option
290 //          definitions for the command to handle proceed to parse and validate
291 //          the
292 //          command's options text for those arguments and extract the values
293 //          for each if
294 //          any.
295 // Type:    Method.
296 // Args:    None.
297 // Return:  MIstatus::success - Functional succeeded.
298 //          MIstatus::failure - Functional failed.
299 // Throws:  None.
300 //--
301 bool CMICmdBase::ParseValidateCmdOptions() {
302   CMICmdArgContext argCntxt(m_cmdData.strMiCmdOption);
303   if (m_setCmdArgs.Validate(m_cmdData.strMiCmd, argCntxt))
304     return MIstatus::success;
305
306   SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_ARGS),
307                                  m_cmdData.strMiCmd.c_str(),
308                                  m_setCmdArgs.GetErrorDescription().c_str()));
309
310   return MIstatus::failure;
311 }
312
313 //++
314 // Details: If the MI Driver is not operating via a client i.e. Eclipse but say
315 // operating
316 //          on a executable passed in as a argument to the drive then what
317 //          should the driver
318 //          do on a command failing? Either continue operating or exit the
319 //          application.
320 //          Override this function where a command failure cannot allow the
321 //          driver to
322 //          continue operating.
323 // Type:    Overrideable.
324 // Args:    None.
325 // Return:  bool - True = Fatal if command fails, false = can continue if
326 // command fails.
327 // Throws:  None.
328 //--
329 bool CMICmdBase::GetExitAppOnCommandFailure() const { return false; }