summaryrefslogtreecommitdiff
path: root/tools/lldb-mi/MICmdBase.cpp
blob: 8bb2107fd02d9ce1c68a7b3cdf0f5d077d40113b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//===-- MICmdBase.cpp -------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// In-house headers:
#include "MICmdBase.h"
#include "MICmdArgValConsume.h"
#include "MICmdArgValOptionLong.h"
#include "MICmnLLDBDebugSessionInfo.h"
#include "MICmnMIValueConst.h"

//++
//------------------------------------------------------------------------------------
// Details: CMICmdBase constructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdBase::CMICmdBase()
    : m_pSelfCreatorFn(nullptr),
      m_rLLDBDebugSessionInfo(CMICmnLLDBDebugSessionInfo::Instance()),
      m_bHasResultRecordExtra(false), m_constStrArgThreadGroup("thread-group"),
      m_constStrArgThread("thread"), m_constStrArgFrame("frame"),
      m_constStrArgConsume("--"), m_ThreadGrpArgMandatory(false),
      m_ThreadArgMandatory(false), m_FrameArgMandatory(false) {}

//++
//------------------------------------------------------------------------------------
// Details: CMICmdBase destructor.
// Type:    Overrideable.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdBase::~CMICmdBase() {}

//++
//------------------------------------------------------------------------------------
// Details: The invoker requires this function.
// Type:    Overridden.
// Args:    None.
// Return:  SMICmdData & -  *this command's present status/data/information.
// Throws:  None.
//--
const SMICmdData &CMICmdBase::GetCmdData() const { return m_cmdData; }

//++
//------------------------------------------------------------------------------------
// Details: The invoker requires this function.
// Type:    Overridden.
// Args:    None.
// Return:  CMIUtilString & -   *this command's current error description.
//                              Empty string indicates command status ok.
// Throws:  None.
//--
const CMIUtilString &CMICmdBase::GetErrorDescription() const {
  return m_strCurrentErrDescription;
}

//++
//------------------------------------------------------------------------------------
// Details: The CMICmdFactory requires this function. Retrieve the command and
// argument
//          options description string.
// Type:    Overridden.
// Args:    None.
// Return:  CMIUtilString & -   Command description.
// Throws:  None.
//--
const CMIUtilString &CMICmdBase::GetMiCmd() const { return m_strMiCmd; }

//++
//------------------------------------------------------------------------------------
// Details: Help parse the arguments that are common to all commands.
// Args:    None.
// Return:  None
// Throws:  None.
//--
void CMICmdBase::AddCommonArgs() {
  m_setCmdArgs.Add(new CMICmdArgValOptionLong(
      m_constStrArgThreadGroup, m_ThreadGrpArgMandatory, true,
      CMICmdArgValListBase::eArgValType_ThreadGrp, 1));
  m_setCmdArgs.Add(new CMICmdArgValOptionLong(
      m_constStrArgThread, m_ThreadArgMandatory, true,
      CMICmdArgValListBase::eArgValType_Number, 1));
  m_setCmdArgs.Add(
      new CMICmdArgValOptionLong(m_constStrArgFrame, m_FrameArgMandatory, true,
                                 CMICmdArgValListBase::eArgValType_Number, 1));
  m_setCmdArgs.Add(new CMICmdArgValConsume(m_constStrArgConsume, false));
}

//++
//------------------------------------------------------------------------------------
// Details: The invoker requires this function. A command must be given working
// data and
//          provide data about its status or provide information to other
//          objects.
// Type:    Overridden.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
void CMICmdBase::SetCmdData(const SMICmdData &vCmdData) {
  m_cmdData = vCmdData;
}

//++
//------------------------------------------------------------------------------------
// Details: The command factory requires this function. The factory calls this
// function
//          so it can obtain *this command's creation function.
// Type:    Overridden.
// Args:    None.
// Return:  CMICmdFactory::CmdCreatorFnPtr - Function pointer.
// Throws:  None.
//--
CMICmdFactory::CmdCreatorFnPtr CMICmdBase::GetCmdCreatorFn() const {
  return m_pSelfCreatorFn;
}

//++
//------------------------------------------------------------------------------------
// Details: If a command is an event type (has callbacks registered with
// SBListener) it
//          needs to inform the Invoker that it has finished its work so that
//          the
//          Invoker can tidy up and call the commands Acknowledge function (yes
//          the
//          command itself could call the Acknowledge itself but not doing that
//          way).
// Type:    Overridden.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
void CMICmdBase::CmdFinishedTellInvoker() const {
  CMICmdInvoker::Instance().CmdExecuteFinished(const_cast<CMICmdBase &>(*this));
}

//++
//------------------------------------------------------------------------------------
// Details: Returns the final version of the MI result record built up in the
// command's
//          Acknowledge function. The one line text of MI result.
// Type:    Overridden.
// Args:    None.
// Return:  CMIUtilString & - MI text version of the MI result record.
// Throws:  None.
//--
const CMIUtilString &CMICmdBase::GetMIResultRecord() const {
  return m_miResultRecord.GetString();
}

//++
//------------------------------------------------------------------------------------
// Details: Retrieve from the command additional MI result to its 1 line
// response.
//          Because of using LLDB additional 'fake'/hack output is sometimes
//          required to
//          help the driver client operate i.e. Eclipse.
// Type:    Overridden.
// Args:    None.
// Return:  CMIUtilString & - MI text version of the MI result record.
// Throws:  None.
//--
const CMIUtilString &CMICmdBase::GetMIResultRecordExtra() const {
  return m_miResultRecordExtra;
}

//++
//------------------------------------------------------------------------------------
// Details: Hss *this command got additional MI result to its 1 line response.
//          Because of using LLDB additional 'fake'/hack output is sometimes
//          required to
//          help the driver client operate i.e. Eclipse.
// Type:    Overridden.
// Args:    None.
// Return:  bool    - True = Yes have additional MI output, false = no nothing
// extra.
// Throws:  None.
//--
bool CMICmdBase::HasMIResultRecordExtra() const {
  return m_bHasResultRecordExtra;
}

//++
//------------------------------------------------------------------------------------
// Details: Short cut function to enter error information into the command's
// metadata
//          object and set the command's error status.
// Type:    Method.
// Args:    rErrMsg - (R) Status description.
// Return:  None.
// Throws:  None.
//--
void CMICmdBase::SetError(const CMIUtilString &rErrMsg) {
  m_cmdData.bCmdValid = false;
  m_cmdData.strErrorDescription = rErrMsg;
  m_cmdData.bCmdExecutedSuccessfully = false;

  const CMICmnMIValueResult valueResult("msg", CMICmnMIValueConst(rErrMsg));
  const CMICmnMIResultRecord miResultRecord(
      m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Error,
      valueResult);
  m_miResultRecord = miResultRecord;
  m_cmdData.strMiCmdResultRecord = miResultRecord.GetString();
}

//++
//------------------------------------------------------------------------------------
// Details: Short cut function to check MI command's execute status and
//          set an error in case of failure.
// Type:    Method.
// Args:    error - (R) Error description object.
//          successHandler - (R) function describing actions to execute
//          in case of success state of passed SBError object.
//          errorHandler - (R) function describing actions to execute
//          in case of fail status of passed SBError object.
// Return:  bool.
// Throws:  None.
//--
bool CMICmdBase::HandleSBError(const lldb::SBError &error,
                               const std::function<bool()> &successHandler,
                               const std::function<void()> &errorHandler) {
  if (error.Success())
    return successHandler();

  SetError(error.GetCString());
  errorHandler();
  return MIstatus::failure;
}

//++
//------------------------------------------------------------------------------------
// Details: Short cut function to check MI command's execute status and
//          call specified handler function for success case.
// Type:    Method.
// Args:    error - (R) Error description object.
//          successHandler - (R) function describing actions to execute
//          in case of success state of passed SBError object.
// Return:  bool.
// Throws:  None.
//--
bool CMICmdBase::HandleSBErrorWithSuccess(
    const lldb::SBError &error,
    const std::function<bool()> &successHandler) {
  return HandleSBError(error, successHandler);
}

//++
//------------------------------------------------------------------------------------
// Details: Short cut function to check MI command's execute status and
//          call specified handler function for error case.
// Type:    Method.
// Args:    error - (R) Error description object.
//          errorHandler - (R) function describing actions to execute
//          in case of fail status of passed SBError object.
// Return:  bool.
// Throws:  None.
//--
bool CMICmdBase::HandleSBErrorWithFailure(
    const lldb::SBError &error,
    const std::function<void()> &errorHandler) {
  return HandleSBError(error, [] { return MIstatus::success; }, errorHandler);
}

//++
//------------------------------------------------------------------------------------
// Details: Ask a command to provide its unique identifier.
// Type:    Method.
// Args:    A unique identifier for this command class.
// Return:  None.
// Throws:  None.
//--
MIuint CMICmdBase::GetGUID() {
  MIuint64 vptr = reinterpret_cast<MIuint64>(this);
  MIuint id = (vptr)&0xFFFFFFFF;
  id ^= (vptr >> 32) & 0xFFFFFFFF;

  return id;
}

//++
//------------------------------------------------------------------------------------
// Details: The invoker requires this function. The parses the command line
// options
//          arguments to extract values for each of those arguments.
// Type:    Overridden.
// Args:    None.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool CMICmdBase::ParseArgs() {
  // Do nothing - override to implement

  return MIstatus::success;
}

//++
//------------------------------------------------------------------------------------
// Details: Having previously given CMICmdArgSet m_setCmdArgs all the argument
// or option
//          definitions for the command to handle proceed to parse and validate
//          the
//          command's options text for those arguments and extract the values
//          for each if
//          any.
// Type:    Method.
// Args:    None.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool CMICmdBase::ParseValidateCmdOptions() {
  CMICmdArgContext argCntxt(m_cmdData.strMiCmdOption);
  if (m_setCmdArgs.Validate(m_cmdData.strMiCmd, argCntxt))
    return MIstatus::success;

  SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_ARGS),
                                 m_cmdData.strMiCmd.c_str(),
                                 m_setCmdArgs.GetErrorDescription().c_str()));

  return MIstatus::failure;
}

//++
//------------------------------------------------------------------------------------
// Details: If the MI Driver is not operating via a client i.e. Eclipse but say
// operating
//          on a executable passed in as a argument to the drive then what
//          should the driver
//          do on a command failing? Either continue operating or exit the
//          application.
//          Override this function where a command failure cannot allow the
//          driver to
//          continue operating.
// Type:    Overrideable.
// Args:    None.
// Return:  bool - True = Fatal if command fails, false = can continue if
// command fails.
// Throws:  None.
//--
bool CMICmdBase::GetExitAppOnCommandFailure() const { return false; }