]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdMgr.cpp
Update compiler-rt to trunk r230183. This has some of our patches
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdMgr.cpp
1 //===-- MICmdMgr.cpp --------------------------------------------*- 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 //++
11 // File:        MICmdMgr.cpp
12 //
13 // Overview:    CMICmdMgr implementation.
14 //
15 // Environment: Compilers:  Visual C++ 12.
16 //                          gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 //              Libraries:  See MIReadmetxt.
18 //
19 // Copyright:   None.
20 //--
21
22 // In-house headers:
23 #include "MICmdMgr.h"
24 #include "MICmnResources.h"
25 #include "MICmnLog.h"
26 #include "MICmdInterpreter.h"
27 #include "MICmdFactory.h"
28 #include "MICmdInvoker.h"
29 #include "MICmdBase.h"
30 #include "MIUtilSingletonBase.h"
31 #include "MIUtilSingletonHelper.h"
32
33 //++ ------------------------------------------------------------------------------------
34 // Details: CMICmdMgr constructor.
35 // Type:    Method.
36 // Args:    None.
37 // Return:  None.
38 // Throws:  None.
39 //--
40 CMICmdMgr::CMICmdMgr(void)
41     : m_interpretor(CMICmdInterpreter::Instance())
42     , m_factory(CMICmdFactory::Instance())
43     , m_invoker(CMICmdInvoker::Instance())
44 {
45 }
46
47 //++ ------------------------------------------------------------------------------------
48 // Details: CMICmdMgr destructor.
49 // Type:    Overridable.
50 // Args:    None.
51 // Return:  None.
52 // Throws:  None.
53 //--
54 CMICmdMgr::~CMICmdMgr(void)
55 {
56     Shutdown();
57 }
58
59 //++ ------------------------------------------------------------------------------------
60 // Details: Initialize resources for *this Command Manager.
61 // Type:    Method.
62 // Args:    None.
63 // Return:  MIstatus::success - Functionality succeeded.
64 //          MIstatus::failure - Functionality failed.
65 // Throws:  None.
66 //--
67 bool
68 CMICmdMgr::Initialize(void)
69 {
70     m_clientUsageRefCnt++;
71
72     if (m_bInitialized)
73         return MIstatus::success;
74
75     bool bOk = MIstatus::success;
76     CMIUtilString errMsg;
77
78     // Note initialization order is important here as some resources depend on previous
79     MI::ModuleInit<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
80     MI::ModuleInit<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);
81     if (bOk && !m_interpretor.Initialize())
82     {
83         bOk = false;
84         errMsg = CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDINTERPRETER), m_interpretor.GetErrorDescription().c_str());
85     }
86     if (bOk && !m_factory.Initialize())
87     {
88         bOk = false;
89         errMsg = CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDFACTORY), m_factory.GetErrorDescription().c_str());
90     }
91     if (bOk && !m_invoker.Initialize())
92     {
93         bOk = false;
94         errMsg = CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDINVOKER), m_invoker.GetErrorDescription().c_str());
95     }
96     m_bInitialized = bOk;
97
98     if (!bOk)
99     {
100         CMIUtilString strInitError(CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDMGR), errMsg.c_str()));
101         SetErrorDescription(strInitError);
102         return MIstatus::failure;
103     }
104
105     return MIstatus::success;
106 }
107
108 //++ ------------------------------------------------------------------------------------
109 // Details: Release resources for *this Command Manager.
110 // Type:    Method.
111 // Args:    None.
112 // Return:  MIstatus::success - Functionality succeeded.
113 //          MIstatus::failure - Functionality failed.
114 // Throws:  None.
115 //--
116 bool
117 CMICmdMgr::Shutdown(void)
118 {
119     if (--m_clientUsageRefCnt > 0)
120         return MIstatus::success;
121
122     if (!m_bInitialized)
123         return MIstatus::success;
124
125     m_bInitialized = false;
126
127     ClrErrorDescription();
128
129     bool bOk = MIstatus::success;
130     CMIUtilString errMsg;
131
132     // Tidy up
133     m_setCmdDeleteCallback.clear();
134
135     // Note shutdown order is important here
136     if (!m_invoker.Shutdown())
137     {
138         bOk = false;
139         errMsg += CMIUtilString::Format(MIRSRC(IDS_MI_SHTDWN_ERR_CMDINVOKER), m_invoker.GetErrorDescription().c_str());
140     }
141     if (!m_factory.Shutdown())
142     {
143         bOk = false;
144         if (!errMsg.empty())
145             errMsg += ", ";
146         errMsg += CMIUtilString::Format(MIRSRC(IDS_MI_SHTDWN_ERR_CMDFACTORY), m_factory.GetErrorDescription().c_str());
147     }
148     if (!m_interpretor.Shutdown())
149     {
150         bOk = false;
151         if (!errMsg.empty())
152             errMsg += ", ";
153         errMsg += CMIUtilString::Format(MIRSRC(IDS_MI_SHTDWN_ERR_CMDINTERPRETER), m_interpretor.GetErrorDescription().c_str());
154     }
155     MI::ModuleShutdown<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);
156     MI::ModuleShutdown<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
157
158     if (!bOk)
159     {
160         SetErrorDescriptionn(MIRSRC(IDS_MI_SHUTDOWN_ERR), errMsg.c_str());
161     }
162
163     return MIstatus::success;
164 }
165
166 //++ ------------------------------------------------------------------------------------
167 // Details: Establish whether the text data is an MI format type command.
168 // Type:    Method.
169 // Args:    vTextLine               - (R) Text data to interpret.
170 //          vwbYesValid             - (W) True = MI type command, false = not recognised.
171 //          vwbCmdNotInCmdFactor    - (W) True = MI command not found in the command factor, false = recognised.
172 // Return:  MIstatus::success - Functionality succeeded.
173 //          MIstatus::failure - Functionality failed.
174 // Throws:  None.
175 //--
176 bool
177 CMICmdMgr::CmdInterpret(const CMIUtilString &vTextLine, bool &vwbYesValid, bool &vwbCmdNotInCmdFactor, SMICmdData &rwCmdData)
178 {
179     return m_interpretor.ValidateIsMi(vTextLine, vwbYesValid, vwbCmdNotInCmdFactor, rwCmdData);
180 }
181
182 //++ ------------------------------------------------------------------------------------
183 // Details: Having previously had the potential command validated and found valid now
184 //          get the command executed.
185 //          If the Functionalityity returns MIstatus::failure call GetErrorDescription().
186 //          This function is used by the application's main thread.
187 // Type:    Method.
188 // Args:    vCmdData    - (RW) Command meta data.
189 // Return:  MIstatus::success - Functionality succeeded.
190 //          MIstatus::failure - Functionality failed.
191 // Throws:  None.
192 //--
193 bool
194 CMICmdMgr::CmdExecute(const SMICmdData &vCmdData)
195 {
196     bool bOk = MIstatus::success;
197
198     // Pass the command's meta data structure to the command
199     // so it can update it if required. (Need to copy it out of the
200     // command before the command is deleted)
201     CMICmdBase *pCmd = nullptr;
202     bOk = m_factory.CmdCreate(vCmdData.strMiCmd, vCmdData, pCmd);
203     if (!bOk)
204     {
205         const CMIUtilString errMsg(
206             CMIUtilString::Format(MIRSRC(IDS_CMDMGR_ERR_CMD_FAILED_CREATE), m_factory.GetErrorDescription().c_str()));
207         SetErrorDescription(errMsg);
208         return MIstatus::failure;
209     }
210
211     bOk = m_invoker.CmdExecute(*pCmd);
212     if (!bOk)
213     {
214         const CMIUtilString errMsg(CMIUtilString::Format(MIRSRC(IDS_CMDMGR_ERR_CMD_INVOKER), m_invoker.GetErrorDescription().c_str()));
215         SetErrorDescription(errMsg);
216         return MIstatus::failure;
217     }
218
219     return bOk;
220 }
221
222 //++ ------------------------------------------------------------------------------------
223 // Details: Iterate all interested clients and tell them a command is being deleted.
224 // Type:    Method.
225 // Args:    vCmdData    - (RW) The command to be deleted.
226 // Return:  MIstatus::success - Functional succeeded.
227 //          MIstatus::failure - Functional failed.
228 // Throws:  None.
229 //--
230 bool
231 CMICmdMgr::CmdDelete(SMICmdData vCmdData)
232 {
233     // Note vCmdData is a copy! The command holding its copy will be deleted soon
234     // we still need to iterate callback clients after a command object is deleted
235
236     m_setCmdDeleteCallback.Delete(vCmdData);
237
238     return MIstatus::success;
239 }
240
241 //++ ------------------------------------------------------------------------------------
242 // Details: Register an object to be called when a command object is deleted.
243 // Type:    Method.
244 // Args:    vObject - (R) A new interested client.
245 // Return:  MIstatus::success - Functional succeeded.
246 //          MIstatus::failure - Functional failed.
247 // Throws:  None.
248 //--
249 bool
250 CMICmdMgr::CmdRegisterForDeleteNotification(CMICmdMgrSetCmdDeleteCallback::ICallback &vObject)
251 {
252     return m_setCmdDeleteCallback.Register(vObject);
253 }
254
255 //++ ------------------------------------------------------------------------------------
256 // Details: Unregister an object from being called when a command object is deleted.
257 // Type:    Method.
258 // Args:    vObject - (R) The was interested client.
259 // Return:  MIstatus::success - Functional succeeded.
260 //          MIstatus::failure - Functional failed.
261 // Throws:  None.
262 //--
263 bool
264 CMICmdMgr::CmdUnregisterForDeleteNotification(CMICmdMgrSetCmdDeleteCallback::ICallback &vObject)
265 {
266     return m_setCmdDeleteCallback.Unregister(vObject);
267 }