ipcenginehost.cpp 17.1 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "ipcenginehost.h"
#include "ipcengineguest.h"
#include "breakhandler.h"
#include "breakpoint.h"
#include "moduleshandler.h"
#include "registerhandler.h"
#include "stackhandler.h"
#include "watchhandler.h"
#include "watchutils.h"
#include "threadshandler.h"
#include "debuggeragents.h"
#include "debuggerstreamops.h"

#include <QSysInfo>
#include <QDebug>
#include <QFileInfo>
#include <QTimer>
#include <utils/qtcassert.h>

#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
#define SET_NATIVE_BYTE_ORDER(x) x.setByteOrder(QDataStream::LittleEndian)
#else
#define SET_NATIVE_BYTE_ORDER(x) x.setByteOrder(QDataStream::BigEndian)
#endif

namespace Debugger {
namespace Internal {

IPCEngineHost::IPCEngineHost (const DebuggerStartParameters &startParameters)
    : DebuggerEngine(startParameters)
    , m_local_guest(0)
    , m_nextMessagePayloadSize(0)
    , m_cookie(1)
    , m_device(0)
{
hjk's avatar
hjk committed
65
    connect(this, SIGNAL(stateChanged(DebuggerState)), this, SLOT(m_stateChanged(DebuggerState)));
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
}

IPCEngineHost::~IPCEngineHost()
{
    delete m_device;
}

void IPCEngineHost::setLocalGuest(IPCEngineGuest *g)
{
    m_local_guest = g;
}

void IPCEngineHost::setGuestDevice(QIODevice *d)
{
    if (m_device) {
        disconnect(m_device, SIGNAL(readyRead()), this, SLOT(readyRead()));
        delete m_device;
    }
    m_device = d;
    if (m_device)
        connect(m_device, SIGNAL(readyRead()), this, SLOT(readyRead()));
}

void IPCEngineHost::setupEngine()
{
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
    rpcCall(SetupEngine);
}

void IPCEngineHost::setupInferior()
{
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << QFileInfo(startParameters().executable).absoluteFilePath();
        s << startParameters().processArgs;
        s << startParameters().environment;
    }
    rpcCall(SetupInferior, p);
}

void IPCEngineHost::runEngine()
{
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
    rpcCall(RunEngine);
}

void IPCEngineHost::shutdownInferior()
{
    QTC_ASSERT(state() == InferiorShutdownRequested, qDebug() << state());
    rpcCall(ShutdownInferior);
}

void IPCEngineHost::shutdownEngine()
{
    rpcCall(ShutdownEngine);
}

void IPCEngineHost::detachDebugger()
{
    rpcCall(DetachDebugger);
}

void IPCEngineHost::executeStep()
{
    rpcCall(ExecuteStep);
}

void IPCEngineHost::executeStepOut()
{
    rpcCall(ExecuteStepOut);
}

void IPCEngineHost::executeNext()
{
    rpcCall(ExecuteNext);
}

void IPCEngineHost::executeStepI()
{
    rpcCall(ExecuteStepI);
}

void IPCEngineHost::executeNextI()
{
    rpcCall(ExecuteNextI);
}

void IPCEngineHost::continueInferior()
{
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
    resetLocation();
    rpcCall(ContinueInferior);
}

void IPCEngineHost::interruptInferior()
{
    QTC_ASSERT(state() == InferiorStopRequested, qDebug() << state());
    rpcCall(InterruptInferior);
}

void IPCEngineHost::executeRunToLine(const QString &fileName, int lineNumber)
{
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << fileName;
        s << quint64(lineNumber);
    }
    rpcCall(ExecuteRunToLine, p);
}

void IPCEngineHost::executeRunToFunction(const QString &functionName)
{
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << functionName;
    }
    rpcCall(ExecuteRunToFunction, p);
}

void IPCEngineHost::executeJumpToLine(const QString &fileName, int lineNumber)
{
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << fileName;
        s << quint64(lineNumber);
    }
    rpcCall(ExecuteJumpToLine, p);
}


void IPCEngineHost::activateFrame(int index)
{
    resetLocation();
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << quint64(index);
    }
    rpcCall(ActivateFrame, p);
}

void IPCEngineHost::selectThread(int index)
{
    resetLocation();
    Threads threads = threadsHandler()->threads();
    QTC_ASSERT(index < threads.size(), return);
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << quint64(threads.at(index).id);
    }
    rpcCall(SelectThread, p);
}

231
void IPCEngineHost::fetchDisassembler(DisassemblerViewAgent *v)
232
233
234
235
236
237
238
239
240
241
242
243
244
{
    quint64 address = v->frame().address;
    m_frameToDisassemblerAgent.insert(address, v);
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << address;
    }
    rpcCall(Disassemble, p);
}


245
void IPCEngineHost::addBreakpoint(const BreakpointParameters &bp)
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << bp;
    }
    rpcCall(AddBreakpoint, p);
}

void IPCEngineHost::removeBreakpoint(quint64 id)
{
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << id;
    }
    rpcCall(RemoveBreakpoint, p);
}

267
void IPCEngineHost::changeBreakpoint(const BreakpointParameters &bp)
268
269
270
271
272
273
274
275
276
277
{
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << bp;
    }
    rpcCall(RemoveBreakpoint, p);
}

278
279
void IPCEngineHost::updateWatchData(const WatchData &data,
            const WatchUpdateFlags &flags)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
{
    Q_UNUSED(flags);
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << data;
    }
    rpcCall(RequestUpdateWatchData, p);
}

void IPCEngineHost::rpcCallback(quint64 f, QByteArray payload)
{
    switch (f) {
        default:
            showMessage(QLatin1String("IPC Error: unhandled id in guest to host call"));
            showMessage(tr("Fatal engine shutdown. Incompatible binary or ipc error."), LogError);
            showStatusMessage(tr("Fatal engine shutdown. Incompatible binary or ipc error."));
            notifyEngineSpontaneousShutdown();
            break;
        case IPCEngineGuest::NotifyEngineSetupOk:
            notifyEngineSetupOk();
            break;
        case IPCEngineGuest::NotifyEngineSetupFailed:
            notifyEngineSetupFailed();
            break;
        case IPCEngineGuest::NotifyEngineRunFailed:
            notifyEngineRunFailed();
            break;
        case IPCEngineGuest::NotifyInferiorSetupOk:
            attemptBreakpointSynchronization();
            notifyInferiorSetupOk();
            break;
        case IPCEngineGuest::NotifyInferiorSetupFailed:
            notifyInferiorSetupFailed();
            break;
        case IPCEngineGuest::NotifyEngineRunAndInferiorRunOk:
            notifyEngineRunAndInferiorRunOk();
            break;
        case IPCEngineGuest::NotifyEngineRunAndInferiorStopOk:
            notifyEngineRunAndInferiorStopOk();
            break;
        case IPCEngineGuest::NotifyInferiorRunRequested:
            notifyInferiorRunRequested();
            break;
        case IPCEngineGuest::NotifyInferiorRunOk:
            notifyInferiorRunOk();
            break;
        case IPCEngineGuest::NotifyInferiorRunFailed:
            notifyInferiorRunFailed();
            break;
        case IPCEngineGuest::NotifyInferiorStopOk:
            notifyInferiorStopOk();
            break;
        case IPCEngineGuest::NotifyInferiorSpontaneousStop:
            notifyInferiorSpontaneousStop();
            break;
        case IPCEngineGuest::NotifyInferiorStopFailed:
            notifyInferiorStopFailed();
            break;
        case IPCEngineGuest::NotifyInferiorExited:
            notifyInferiorExited();
            break;
        case IPCEngineGuest::NotifyInferiorShutdownOk:
            notifyInferiorShutdownOk();
            break;
        case IPCEngineGuest::NotifyInferiorShutdownFailed:
            notifyInferiorShutdownFailed();
            break;
        case IPCEngineGuest::NotifyEngineSpontaneousShutdown:
            notifyEngineSpontaneousShutdown();
            break;
        case IPCEngineGuest::NotifyEngineShutdownOk:
            notifyEngineShutdownOk();
            break;
        case IPCEngineGuest::NotifyEngineShutdownFailed:
            notifyEngineShutdownFailed();
            break;
        case IPCEngineGuest::NotifyInferiorIll:
            notifyInferiorIll();
            break;
        case IPCEngineGuest::NotifyEngineIll:
            notifyEngineIll();
            break;
        case IPCEngineGuest::NotifyInferiorPid:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
                quint64 pid;
                s >> pid;
                notifyInferiorPid(pid);
            }
            break;
        case IPCEngineGuest::ShowStatusMessage:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
                QString msg;
                qint64 timeout;
                s >> msg;
                s >> timeout;
                showStatusMessage(msg, timeout);
            }
            break;
        case IPCEngineGuest::ShowMessage:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
                QString msg;
                qint16 channel;
                qint64 timeout;
                s >> msg;
                s >> channel;
                s >> timeout;
                showMessage(msg, channel, timeout);
            }
            break;
        case IPCEngineGuest::CurrentFrameChanged:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
                quint64 token;
                s >> token;

                resetLocation();
                StackHandler *sh = stackHandler();
                sh->setCurrentIndex(token);
                gotoLocation(sh->currentFrame(), true);
            }
            break;
        case IPCEngineGuest::CurrentThreadChanged:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
                quint64 token;
                s >> token;
                threadsHandler()->setCurrentThreadId(token);
            }
            break;
        case IPCEngineGuest::ListFrames:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
                StackFrames frames;
                s >> frames;
                stackHandler()->setFrames(frames);
            }
            break;
        case IPCEngineGuest::ListThreads:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
                Threads threads;
                s >> threads;
                threadsHandler()->setThreads(threads);
            }
            break;
        case IPCEngineGuest::Disassembled:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
                quint64 pc;
                QString da;
                s >> pc;
                s >> da;
445
                DisassemblerViewAgent *view = m_frameToDisassemblerAgent.take(pc);
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
                if (view)
                    view->setContents(da);
            }
            break;
        case IPCEngineGuest::UpdateWatchData:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
                bool fullCycle;
                qint64 count;
                QList<WatchData> wd;
                s >> fullCycle;
                s >> count;
                for (qint64 i = 0; i < count; i++) {
                    WatchData d;
                    s >> d;
                    wd.append(d);
                }
                WatchHandler *wh = watchHandler();
                if (!wh)
                    break;
                wh->beginCycle(fullCycle);
                wh->insertBulkData(wd);
                wh->endCycle(fullCycle);
            }
            break;
        case IPCEngineGuest::NotifyAddBreakpointOk:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
476
                BreakpointId id;
477
                s >> id;
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
478
                breakHandler()->notifyBreakpointInsertOk(id);
479
480
481
482
483
            }
        case IPCEngineGuest::NotifyAddBreakpointFailed:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
484
                BreakpointId id;
485
                s >> id;
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
486
                breakHandler()->notifyBreakpointInsertFailed(id);
487
488
489
490
491
            }
        case IPCEngineGuest::NotifyRemoveBreakpointOk:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
492
                BreakpointId id;
493
                s >> id;
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
494
                breakHandler()->notifyBreakpointRemoveOk(id);
495
496
497
498
499
            }
        case IPCEngineGuest::NotifyRemoveBreakpointFailed:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
500
                BreakpointId id;
501
                s >> id;
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
502
                breakHandler()->notifyBreakpointRemoveFailed(id);
503
504
505
506
507
            }
        case IPCEngineGuest::NotifyChangeBreakpointOk:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
508
                BreakpointId id;
509
                s >> id;
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
510
                breakHandler()->notifyBreakpointChangeOk(id);
511
512
513
514
515
            }
        case IPCEngineGuest::NotifyChangeBreakpointFailed:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
516
                BreakpointId id;
517
                s >> id;
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
518
                breakHandler()->notifyBreakpointChangeFailed(id);
519
520
521
522
523
            }
        case IPCEngineGuest::NotifyBreakpointAdjusted:
            {
                QDataStream s(payload);
                SET_NATIVE_BYTE_ORDER(s);
524
525
526
                BreakpointId id;
                BreakpointParameters d;
                s >> id >> d;
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
527
                breakHandler()->notifyBreakpointAdjusted(id, d);
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
            }
    }
}

void IPCEngineHost::m_stateChanged(const DebuggerState &state)
{
    QByteArray p;
    {
        QDataStream s(&p, QIODevice::WriteOnly);
        SET_NATIVE_BYTE_ORDER(s);
        s << (qint64)state;
    }
    rpcCall(StateChanged, p);

}

void IPCEngineHost::rpcCall(Function f, QByteArray payload)
{
    if (m_local_guest) {
        QMetaObject::invokeMethod(m_local_guest,
                "rpcCallback",
                Qt::QueuedConnection,
                Q_ARG(quint64, f),
                Q_ARG(QByteArray, payload));
    } else if (m_device) {
        QByteArray header;
        {
            QDataStream s(&header, QIODevice::WriteOnly);
            SET_NATIVE_BYTE_ORDER(s);
            s << m_cookie++;
            s << (quint64) f;
            s << (quint64) payload.size();
        }
        m_device->write(header);
        m_device->write(payload);
        m_device->putChar('T');
    }
}

void IPCEngineHost::readyRead()
{
    QDataStream s(m_device);
    SET_NATIVE_BYTE_ORDER(s);
    if (!m_nextMessagePayloadSize) {
        if (quint64(m_device->bytesAvailable ()) < (sizeof(quint64) * 3))
            return;
        s >> m_nextMessageCookie;
        s >> m_nextMessageFunction;
        s >> m_nextMessagePayloadSize;
        m_nextMessagePayloadSize += 1; // terminator and "got header" marker
    }

    quint64 ba = m_device->bytesAvailable();
    if (ba < m_nextMessagePayloadSize)
        return;

    QByteArray payload = m_device->read(m_nextMessagePayloadSize - 1);

    char terminator;
    m_device->getChar(&terminator);
    if (terminator != 'T') {
        showStatusMessage(tr("Fatal engine shutdown. Incompatible binary or ipc error."));
        showMessage(QLatin1String("IPC Error: terminator missing"));
        notifyEngineSpontaneousShutdown();
        return;
    }
    rpcCallback(m_nextMessageFunction, payload);
    m_nextMessagePayloadSize = 0;
    if (quint64(m_device->bytesAvailable()) >= (sizeof(quint64) * 3))
        QTimer::singleShot(0, this, SLOT(readyRead()));
}


} // namespace Internal
} // namespace Debugger