]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdMgr.cpp
Merge ^/head r275478 through r275622.
[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 CMICmdMgr::Initialize( void )
68 {
69         m_clientUsageRefCnt++;
70
71         if( m_bInitialized )
72                 return MIstatus::success;
73
74         bool bOk = MIstatus::success;
75         CMIUtilString errMsg;
76  
77         // Note initialization order is important here as some resources depend on previous
78         MI::ModuleInit< CMICmnLog >      ( IDS_MI_INIT_ERR_LOG      , bOk, errMsg );
79         MI::ModuleInit< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
80         if( bOk && !m_interpretor.Initialize() )
81         {
82                 bOk = false;
83                 errMsg = CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_CMDINTERPRETER ), m_interpretor.GetErrorDescription().c_str() );
84         }
85         if( bOk && !m_factory.Initialize() )
86         {
87                 bOk = false;
88                 errMsg = CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_CMDFACTORY ), m_factory.GetErrorDescription().c_str() );
89         }
90         if( bOk && !m_invoker.Initialize() )
91         {
92                 bOk = false;
93                 errMsg = CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_CMDINVOKER ), m_invoker.GetErrorDescription().c_str() );
94         }
95         m_bInitialized = bOk;
96
97         if( !bOk )
98         {
99                 CMIUtilString strInitError( CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_CMDMGR ), errMsg.c_str() ) );
100                 SetErrorDescription( strInitError );
101                 return MIstatus::failure;
102         }
103
104         return  MIstatus::success;
105 }
106
107 //++ ------------------------------------------------------------------------------------
108 // Details:     Release resources for *this Command Manager.
109 // Type:        Method.
110 // Args:        None.
111 // Return:      MIstatus::success - Functionality succeeded.
112 //                      MIstatus::failure - Functionality failed.
113 // Throws:      None.
114 //--
115 bool CMICmdMgr::Shutdown( void )
116 {
117         if( --m_clientUsageRefCnt > 0 )
118                 return MIstatus::success;
119         
120         if( !m_bInitialized )
121                 return MIstatus::success;
122
123         m_bInitialized = false;
124
125         ClrErrorDescription();
126
127         bool bOk = MIstatus::success;
128         CMIUtilString errMsg;
129
130         // Tidy up
131         m_setCmdDeleteCallback.clear();
132
133         // Note shutdown order is important here 
134         if( !m_invoker.Shutdown() )
135         {
136                 bOk = false;
137                 errMsg += CMIUtilString::Format( MIRSRC( IDS_MI_SHTDWN_ERR_CMDINVOKER ), m_invoker.GetErrorDescription().c_str() );
138         }
139         if( !m_factory.Shutdown() )
140         {
141                 bOk = false;
142                 if( !errMsg.empty() ) errMsg += ", ";
143                 errMsg += CMIUtilString::Format( MIRSRC( IDS_MI_SHTDWN_ERR_CMDFACTORY ), m_factory.GetErrorDescription().c_str() );
144         }
145         if( !m_interpretor.Shutdown() )
146         {
147                 bOk = false;
148                 if( !errMsg.empty() ) errMsg += ", ";
149                 errMsg += CMIUtilString::Format( MIRSRC( IDS_MI_SHTDWN_ERR_CMDINTERPRETER ), m_interpretor.GetErrorDescription().c_str() );
150         }
151         MI::ModuleShutdown< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
152         MI::ModuleShutdown< CMICmnLog >( IDS_MI_INIT_ERR_LOG            , bOk, errMsg );
153
154         if( !bOk )
155         {
156                 SetErrorDescriptionn( MIRSRC( IDS_MI_SHUTDOWN_ERR ), errMsg.c_str() );
157         }
158
159         return MIstatus::success;
160 }       
161
162 //++ ------------------------------------------------------------------------------------
163 // Details:     Establish whether the text data is an MI format type command.
164 // Type:        Method.
165 // Args:        vTextLine                               - (R) Text data to interpret.
166 //                      vwbYesValid                             - (W) True = MI type command, false = not recognised.
167 //                      vwbCmdNotInCmdFactor    - (W) True = MI command not found in the command factor, false = recognised.
168 // Return:      MIstatus::success - Functionality succeeded.
169 //                      MIstatus::failure - Functionality failed.
170 // Throws:      None.
171 //--
172 bool CMICmdMgr::CmdInterpret( const CMIUtilString & vTextLine, bool & vwbYesValid, bool & vwbCmdNotInCmdFactor, SMICmdData & rwCmdData )
173 {
174         return m_interpretor.ValidateIsMi( vTextLine, vwbYesValid, vwbCmdNotInCmdFactor, rwCmdData );
175 }
176
177 //++ ------------------------------------------------------------------------------------
178 // Details:     Having previously had the potential command validated and found valid now
179 //                      get the command executed.
180 //                      If the Functionalityity returns MIstatus::failure call GetErrorDescription().
181 //                      This function is used by the application's main thread.
182 // Type:        Method.
183 // Args:        vCmdData        - (RW) Command meta data.
184 // Return:      MIstatus::success - Functionality succeeded.
185 //                      MIstatus::failure - Functionality failed.
186 // Throws:      None.
187 //--
188 bool CMICmdMgr::CmdExecute( const SMICmdData & vCmdData )
189 {
190         bool bOk = MIstatus::success;
191         
192         // Pass the command's meta data structure to the command 
193         // so it can update it if required. (Need to copy it out of the
194         // command before the command is deleted)
195         CMICmdBase * pCmd = nullptr;
196         bOk = m_factory.CmdCreate( vCmdData.strMiCmd, vCmdData, pCmd );
197         if( !bOk )
198         {
199                 const CMIUtilString errMsg( CMIUtilString::Format( MIRSRC( IDS_CMDMGR_ERR_CMD_FAILED_CREATE ), m_factory.GetErrorDescription().c_str() ) );
200                 SetErrorDescription( errMsg );
201                 return MIstatus::failure;
202         }
203
204         bOk = m_invoker.CmdExecute( *pCmd );
205         if( !bOk )
206         {
207                 const CMIUtilString errMsg( CMIUtilString::Format( MIRSRC( IDS_CMDMGR_ERR_CMD_INVOKER ), m_invoker.GetErrorDescription().c_str() ) );
208                 SetErrorDescription( errMsg );
209                 return MIstatus::failure;
210         }
211                         
212         return bOk;
213 }
214
215 //++ ------------------------------------------------------------------------------------
216 // Details:     Iterate all interested clients and tell them a command is being deleted.
217 // Type:        Method.
218 // Args:        vCmdData        - (RW) The command to be deleted. 
219 // Return:      MIstatus::success - Functional succeeded.
220 //                      MIstatus::failure - Functional failed.
221 // Throws:      None.
222 //--
223 bool CMICmdMgr::CmdDelete( SMICmdData vCmdData )
224 {
225         // Note vCmdData is a copy! The command holding its copy will be deleted soon
226         // we still need to iterate callback clients after a command object is deleted
227
228         m_setCmdDeleteCallback.Delete( vCmdData );
229
230         return MIstatus::success;
231 }
232
233 //++ ------------------------------------------------------------------------------------
234 // Details:     Register an object to be called when a command object is deleted.
235 // Type:        Method.
236 // Args:        vObject - (R) A new interested client.
237 // Return:      MIstatus::success - Functional succeeded.
238 //                      MIstatus::failure - Functional failed.
239 // Throws:      None.
240 //--
241 bool CMICmdMgr::CmdRegisterForDeleteNotification( CMICmdMgrSetCmdDeleteCallback::ICallback & vObject )
242 {
243         return m_setCmdDeleteCallback.Register( vObject );
244 }
245
246 //++ ------------------------------------------------------------------------------------
247 // Details:     Unregister an object from being called when a command object is deleted.
248 // Type:        Method.
249 // Args:        vObject - (R) The was interested client.
250 // Return:      MIstatus::success - Functional succeeded.
251 //                      MIstatus::failure - Functional failed.
252 // Throws:      None.
253 //--
254 bool CMICmdMgr::CmdUnregisterForDeleteNotification( CMICmdMgrSetCmdDeleteCallback::ICallback & vObject )
255 {
256         return m_setCmdDeleteCallback.Unregister( vObject );
257 }