aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lldb-mi/MICmdMgr.cpp
blob: 453ffedbd33df86b529f402e2a81927be28de0da (plain) (blame)
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
//===-- MICmdMgr.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 "MICmdMgr.h"
#include "MICmdBase.h"
#include "MICmdFactory.h"
#include "MICmdInterpreter.h"
#include "MICmdInvoker.h"
#include "MICmnLog.h"
#include "MICmnResources.h"
#include "MIUtilSingletonBase.h"
#include "MIUtilSingletonHelper.h"

//++
// Details: CMICmdMgr constructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdMgr::CMICmdMgr()
    : m_interpretor(CMICmdInterpreter::Instance()),
      m_factory(CMICmdFactory::Instance()),
      m_invoker(CMICmdInvoker::Instance()) {}

//++
// Details: CMICmdMgr destructor.
// Type:    Overridable.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdMgr::~CMICmdMgr() { Shutdown(); }

//++
// Details: Initialize resources for *this Command Manager.
// Type:    Method.
// Args:    None.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Throws:  None.
//--
bool CMICmdMgr::Initialize() {
  m_clientUsageRefCnt++;

  if (m_bInitialized)
    return MIstatus::success;

  bool bOk = MIstatus::success;
  CMIUtilString errMsg;

  // Note initialization order is important here as some resources depend on
  // previous
  MI::ModuleInit<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
  MI::ModuleInit<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);
  if (bOk && !m_interpretor.Initialize()) {
    bOk = false;
    errMsg = CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDINTERPRETER),
                                   m_interpretor.GetErrorDescription().c_str());
  }
  if (bOk && !m_factory.Initialize()) {
    bOk = false;
    errMsg = CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDFACTORY),
                                   m_factory.GetErrorDescription().c_str());
  }
  if (bOk && !m_invoker.Initialize()) {
    bOk = false;
    errMsg = CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDINVOKER),
                                   m_invoker.GetErrorDescription().c_str());
  }
  m_bInitialized = bOk;

  if (!bOk) {
    CMIUtilString strInitError(
        CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDMGR), errMsg.c_str()));
    SetErrorDescription(strInitError);
    return MIstatus::failure;
  }

  return MIstatus::success;
}

//++
// Details: Release resources for *this Command Manager.
// Type:    Method.
// Args:    None.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Throws:  None.
//--
bool CMICmdMgr::Shutdown() {
  if (--m_clientUsageRefCnt > 0)
    return MIstatus::success;

  if (!m_bInitialized)
    return MIstatus::success;

  m_bInitialized = false;

  ClrErrorDescription();

  bool bOk = MIstatus::success;
  CMIUtilString errMsg;

  // Tidy up
  m_setCmdDeleteCallback.clear();

  // Note shutdown order is important here
  if (!m_invoker.Shutdown()) {
    bOk = false;
    errMsg += CMIUtilString::Format(MIRSRC(IDS_MI_SHTDWN_ERR_CMDINVOKER),
                                    m_invoker.GetErrorDescription().c_str());
  }
  if (!m_factory.Shutdown()) {
    bOk = false;
    if (!errMsg.empty())
      errMsg += ", ";
    errMsg += CMIUtilString::Format(MIRSRC(IDS_MI_SHTDWN_ERR_CMDFACTORY),
                                    m_factory.GetErrorDescription().c_str());
  }
  if (!m_interpretor.Shutdown()) {
    bOk = false;
    if (!errMsg.empty())
      errMsg += ", ";
    errMsg +=
        CMIUtilString::Format(MIRSRC(IDS_MI_SHTDWN_ERR_CMDINTERPRETER),
                              m_interpretor.GetErrorDescription().c_str());
  }
  MI::ModuleShutdown<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);
  MI::ModuleShutdown<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);

  if (!bOk) {
    SetErrorDescriptionn(MIRSRC(IDS_MI_SHUTDOWN_ERR), errMsg.c_str());
  }

  return MIstatus::success;
}

//++
// Details: Establish whether the text data is an MI format type command.
// Type:    Method.
// Args:    vTextLine               - (R) Text data to interpret.
//          vwbYesValid             - (W) True = MI type command, false = not
//          recognised.
//          vwbCmdNotInCmdFactor    - (W) True = MI command not found in the
//          command factor, false = recognised.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Throws:  None.
//--
bool CMICmdMgr::CmdInterpret(const CMIUtilString &vTextLine, bool &vwbYesValid,
                             bool &vwbCmdNotInCmdFactor,
                             SMICmdData &rwCmdData) {
  return m_interpretor.ValidateIsMi(vTextLine, vwbYesValid,
                                    vwbCmdNotInCmdFactor, rwCmdData);
}

//++
// Details: Having previously had the potential command validated and found
// valid now
//          get the command executed.
//          If the Functionality returns MIstatus::failure call
//          GetErrorDescription().
//          This function is used by the application's main thread.
// Type:    Method.
// Args:    vCmdData    - (RW) Command meta data.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Throws:  None.
//--
bool CMICmdMgr::CmdExecute(const SMICmdData &vCmdData) {
  bool bOk = MIstatus::success;

  // Pass the command's meta data structure to the command
  // so it can update it if required. (Need to copy it out of the
  // command before the command is deleted)
  CMICmdBase *pCmd = nullptr;
  bOk = m_factory.CmdCreate(vCmdData.strMiCmd, vCmdData, pCmd);
  if (!bOk) {
    const CMIUtilString errMsg(
        CMIUtilString::Format(MIRSRC(IDS_CMDMGR_ERR_CMD_FAILED_CREATE),
                              m_factory.GetErrorDescription().c_str()));
    SetErrorDescription(errMsg);
    return MIstatus::failure;
  }

  bOk = m_invoker.CmdExecute(*pCmd);
  if (!bOk) {
    const CMIUtilString errMsg(
        CMIUtilString::Format(MIRSRC(IDS_CMDMGR_ERR_CMD_INVOKER),
                              m_invoker.GetErrorDescription().c_str()));
    SetErrorDescription(errMsg);
    return MIstatus::failure;
  }

  return bOk;
}

//++
// Details: Iterate all interested clients and tell them a command is being
// deleted.
// Type:    Method.
// Args:    vCmdData    - (RW) The command to be deleted.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool CMICmdMgr::CmdDelete(SMICmdData vCmdData) {
  // Note vCmdData is a copy! The command holding its copy will be deleted soon
  // we still need to iterate callback clients after a command object is deleted

  m_setCmdDeleteCallback.Delete(vCmdData);

  return MIstatus::success;
}

//++
// Details: Register an object to be called when a command object is deleted.
// Type:    Method.
// Args:    vObject - (R) A new interested client.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool CMICmdMgr::CmdRegisterForDeleteNotification(
    CMICmdMgrSetCmdDeleteCallback::ICallback &vObject) {
  return m_setCmdDeleteCallback.Register(vObject);
}

//++
// Details: Unregister an object from being called when a command object is
// deleted.
// Type:    Method.
// Args:    vObject - (R) The was interested client.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool CMICmdMgr::CmdUnregisterForDeleteNotification(
    CMICmdMgrSetCmdDeleteCallback::ICallback &vObject) {
  return m_setCmdDeleteCallback.Unregister(vObject);
}