aboutsummaryrefslogtreecommitdiff
path: root/tools/lldb-mi/MICmdInvoker.cpp
blob: ef957c53bd7054cf65f115fdb1c7c89455069e1f (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
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
//===-- MICmdInvoker.cpp ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// In-house headers:
#include "MICmdInvoker.h"
#include "MICmdBase.h"
#include "MICmdMgr.h"
#include "MICmnLog.h"
#include "MICmnStreamStdout.h"
#include "MIDriver.h"

//++ ------------------------------------------------------------------------------------
// Details: CMICmdInvoker constructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdInvoker::CMICmdInvoker(void)
    : m_rStreamOut(CMICmnStreamStdout::Instance())
{
}

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

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

    if (m_bInitialized)
        return MIstatus::success;

    m_bInitialized = true;

    return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details: Release resources for *this Stdin stream.
// Type:    Method.
// Args:    None.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool
CMICmdInvoker::Shutdown(void)
{
    if (--m_clientUsageRefCnt > 0)
        return MIstatus::success;

    if (!m_bInitialized)
        return MIstatus::success;

    CmdDeleteAll();

    m_bInitialized = false;

    return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details: Empty the map of invoked commands doing work. Command objects are deleted too.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
void
CMICmdInvoker::CmdDeleteAll(void)
{
    CMICmdMgr &rMgr = CMICmdMgr::Instance();
    MapCmdIdToCmd_t::const_iterator it = m_mapCmdIdToCmd.begin();
    while (it != m_mapCmdIdToCmd.end())
    {
        const MIuint cmdId((*it).first);
        MIunused(cmdId);
        CMICmdBase *pCmd = (*it).second;
        const CMIUtilString &rCmdName(pCmd->GetCmdData().strMiCmd);
        MIunused(rCmdName);
        rMgr.CmdDelete(pCmd->GetCmdData());

        // Next
        ++it;
    }
    m_mapCmdIdToCmd.clear();
}

//++ ------------------------------------------------------------------------------------
// Details: Remove from the map of invoked commands doing work a command that has finished
//          its work. The command object is deleted too.
// Type:    Method.
// Args:    vId             - (R) Command object's unique ID.
//          vbYesDeleteCmd  - (R) True = Delete command object, false = delete via the Command Manager.
// Return:  None.
// Throws:  None.
//--
bool
CMICmdInvoker::CmdDelete(const MIuint vId, const bool vbYesDeleteCmd /*= false*/)
{
    CMICmdMgr &rMgr = CMICmdMgr::Instance();
    MapCmdIdToCmd_t::const_iterator it = m_mapCmdIdToCmd.find(vId);
    if (it != m_mapCmdIdToCmd.end())
    {
        CMICmdBase *pCmd = (*it).second;
        if (vbYesDeleteCmd)
        {
            // Via registered interest command manager callback *this object to delete the command
            m_mapCmdIdToCmd.erase(it);
            delete pCmd;
        }
        else
            // Notify other interested object of this command's pending deletion
            rMgr.CmdDelete(pCmd->GetCmdData());
    }

    if (m_mapCmdIdToCmd.empty())
        rMgr.CmdUnregisterForDeleteNotification(*this);

    return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details: Add to the map of invoked commands doing work a command that is about to
//          start to do work.
// Type:    Method.
// Args:    vCmd    - (R) Command object.
// Return:  None.
// Throws:  None.
//--
bool
CMICmdInvoker::CmdAdd(const CMICmdBase &vCmd)
{
    if (m_mapCmdIdToCmd.empty())
    {
        CMICmdMgr &rMgr = CMICmdMgr::Instance();
        rMgr.CmdRegisterForDeleteNotification(*this);
    }

    const MIuint &cmdId(vCmd.GetCmdData().id);
    MapCmdIdToCmd_t::const_iterator it = m_mapCmdIdToCmd.find(cmdId);
    if (it != m_mapCmdIdToCmd.end())
        return MIstatus::success;

    MapPairCmdIdToCmd_t pr(cmdId, const_cast<CMICmdBase *>(&vCmd));
    m_mapCmdIdToCmd.insert(pr);

    return MIstatus::success;
}

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

    if (bOk && !vCmd.ParseArgs())
    {
        // Report command execution failed
        const SMICmdData cmdData(vCmd.GetCmdData());
        CmdStdout(cmdData);
        CmdCauseAppExit(vCmd);
        CmdDelete(cmdData.id);

        // Proceed to wait or execute next command
        return MIstatus::success;
    }

    if (bOk && !vCmd.Execute())
    {
        // Report command execution failed
        const SMICmdData cmdData(vCmd.GetCmdData());
        CmdStdout(cmdData);
        CmdCauseAppExit(vCmd);
        CmdDelete(cmdData.id);

        // Proceed to wait or execute next command
        return MIstatus::success;
    }

    bOk = CmdExecuteFinished(vCmd);

    return bOk;
}

//++ ------------------------------------------------------------------------------------
// Details: Called when a command has finished its Execution() work either synchronously
//          because the command executed was the type a non event type or asynchronoulsy
//          via the command's callback (because of an SB Listener event). Needs to be called
//          so that *this invoker call do some house keeping and then proceed to call
//          the command's Acknowledge() function.
// Type:    Method.
// Args:    vCmd    - (R) Command object.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Throws:  None.
//--
bool
CMICmdInvoker::CmdExecuteFinished(CMICmdBase &vCmd)
{
    // Command finished now get the command to gather it's information and form the MI
    // Result record
    if (!vCmd.Acknowledge())
    {
        // Report command acknowledge functionality failed
        const SMICmdData cmdData(vCmd.GetCmdData());
        CmdStdout(cmdData);
        CmdCauseAppExit(vCmd);
        CmdDelete(cmdData.id);

        // Proceed to wait or execute next command
        return MIstatus::success;
    }

    // Retrieve the command's latest data/information. Needed for commands of the event type so have
    // a record of commands pending finishing execution.
    const CMIUtilString &rMIResultRecord(vCmd.GetMIResultRecord());
    SMICmdData cmdData(vCmd.GetCmdData());          // Make a copy as the command will be deleted soon
    cmdData.strMiCmdResultRecord = rMIResultRecord; // Precautionary copy as the command might forget to do this
    if (vCmd.HasMIResultRecordExtra())
    {
        cmdData.bHasResultRecordExtra = true;
        const CMIUtilString &rMIExtra(vCmd.GetMIResultRecordExtra());
        cmdData.strMiCmdResultRecordExtra = rMIExtra; // Precautionary copy as the command might forget to do this
    }

    // Send command's MI response to the client
    bool bOk = CmdStdout(cmdData);

    // Delete the command object as do not require anymore
    bOk = bOk && CmdDelete(vCmd.GetCmdData().id);

    return bOk;
}

//++ ------------------------------------------------------------------------------------
// Details: If the MI Driver is not operating via a client i.e. Eclipse check the command
//          on failure suggests the application exits. A command can be such that a
//          failure cannot the allow the application to continue operating.
// Args:    vCmd    - (R) Command object.
// Return:  None.
// Return:  None.
// Throws:  None.
//--
void
CMICmdInvoker::CmdCauseAppExit(const CMICmdBase &vCmd) const
{
    if (vCmd.GetExitAppOnCommandFailure())
    {
        CMIDriver &rDriver(CMIDriver::Instance());
        if (rDriver.IsDriverDebuggingArgExecutable())
        {
            rDriver.SetExitApplicationFlag(true);
        }
    }
}

//++ ------------------------------------------------------------------------------------
// Details: Write to stdout and the Log file the command's MI formatted result.
// Type:    vCmdData    - (R) A command's information.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Return:  None.
// Throws:  None.
//--
bool
CMICmdInvoker::CmdStdout(const SMICmdData &vCmdData) const
{
    bool bOk = m_pLog->WriteLog(vCmdData.strMiCmdAll);
    const bool bLock = bOk && m_rStreamOut.Lock();
    bOk = bOk && bLock && m_rStreamOut.WriteMIResponse(vCmdData.strMiCmdResultRecord);
    if (bOk && vCmdData.bHasResultRecordExtra)
    {
        bOk = m_rStreamOut.WriteMIResponse(vCmdData.strMiCmdResultRecordExtra);
    }
    bOk = bLock && m_rStreamOut.Unlock();

    return bOk;
}

//++ ------------------------------------------------------------------------------------
// Details: Required by the CMICmdMgr::ICmdDeleteCallback. *this object is registered
//          with the Command Manager to receive callbacks when a command is being deleted.
//          An object, *this invoker, does not delete a command object itself but calls
//          the Command Manager to delete a command object. This function is the Invoker's
//          called.
//          The Invoker owns the command objects and so can delete them but must do it
//          via the manager so other objects can be notified of the deletion.
// Type:    Method.
// Args:    vCmd    - (RW) Command.
// Return:  None.
// Throws:  None.
//--
void
CMICmdInvoker::Delete(SMICmdData &vCmd)
{
    CmdDelete(vCmd.id, true);
}