summaryrefslogtreecommitdiff
path: root/src/plugins/debugger/qml/qscriptdebuggerclient.cpp
blob: f8ad0c944f48a314a0fe22ae303e7a4a28fc1bbd (plain)
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
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
445
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
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
605
606
607
608
609
610
611
612
613
614
615
616
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "qscriptdebuggerclient.h"

#include "watchdata.h"
#include "watchhandler.h"
#include "breakpoint.h"
#include "breakhandler.h"
#include "debuggerconstants.h"
#include "qmlengine.h"
#include "stackhandler.h"
#include "debuggercore.h"
#include "debuggerstringutils.h"

#include <QTextDocument>
#include <QFileInfo>
#include <QTextStream>
#include <QMessageBox>
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>

namespace Debugger {
namespace Internal {

struct JSAgentBreakpointData
{
    QByteArray functionName;
    QByteArray fileUrl;
    qint32 lineNumber;
};

struct JSAgentStackData
{
    QByteArray functionName;
    QByteArray fileUrl;
    qint32 lineNumber;
};

uint qHash(const JSAgentBreakpointData &b)
{
    return b.lineNumber ^ qHash(b.fileUrl);
}

QDataStream &operator<<(QDataStream &s, const JSAgentBreakpointData &data)
{
    return s << data.functionName << data.fileUrl << data.lineNumber;
}

QDataStream &operator<<(QDataStream &s, const JSAgentStackData &data)
{
    return s << data.functionName << data.fileUrl << data.lineNumber;
}

QDataStream &operator>>(QDataStream &s, JSAgentBreakpointData &data)
{
    return s >> data.functionName >> data.fileUrl >> data.lineNumber;
}

QDataStream &operator>>(QDataStream &s, JSAgentStackData &data)
{
    return s >> data.functionName >> data.fileUrl >> data.lineNumber;
}

bool operator==(const JSAgentBreakpointData &b1, const JSAgentBreakpointData &b2)
{
    return b1.lineNumber == b2.lineNumber && b1.fileUrl == b2.fileUrl;
}

typedef QSet<JSAgentBreakpointData> JSAgentBreakpoints;
typedef QList<JSAgentStackData> JSAgentStackFrames;


static QDataStream &operator>>(QDataStream &s, WatchData &data)
{
    data = WatchData();
    QByteArray name;
    QByteArray value;
    QByteArray type;
    bool hasChildren = false;
    s >> data.exp >> name >> value >> type >> hasChildren >> data.id;
    data.name = QString::fromUtf8(name);
    data.setType(type, false);
    data.setValue(QString::fromUtf8(value));
    data.setHasChildren(hasChildren);
    data.setAllUnneeded();
    return s;
}

class QScriptDebuggerClientPrivate
{
public:
    explicit QScriptDebuggerClientPrivate(QScriptDebuggerClient *) :
        ping(0), engine(0)
    {

    }

    int ping;
    QmlEngine *engine;
    JSAgentBreakpoints breakpoints;

    void logSendMessage(const QString &msg) const;
    void logReceiveMessage(const QString &msg) const;
};

QScriptDebuggerClient::QScriptDebuggerClient(QmlJsDebugClient::QDeclarativeDebugConnection* client)
    : QmlDebuggerClient(client, QLatin1String("JSDebugger")),
      d(new QScriptDebuggerClientPrivate(this))
{
}

QScriptDebuggerClient::~QScriptDebuggerClient()
{
    delete d;
}

void QScriptDebuggerClient::executeStep()
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "STEPINTO";
    rs << cmd;
    d->logSendMessage(QLatin1String(cmd));
    sendMessage(reply);
}

void QScriptDebuggerClient::executeStepOut()
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "STEPOUT";
    rs << cmd;
    d->logSendMessage(QLatin1String(cmd));
    sendMessage(reply);
}

void QScriptDebuggerClient::executeNext()
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "STEPOVER";
    rs << cmd;
    d->logSendMessage(QLatin1String(cmd));
    sendMessage(reply);
}

void QScriptDebuggerClient::executeStepI()
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "STEPINTO";
    rs << cmd;
    d->logSendMessage(QLatin1String(cmd));
    sendMessage(reply);
}

void QScriptDebuggerClient::executeRunToLine(const ContextData &data)
{
    JSAgentBreakpointData bp;
    bp.fileUrl = QUrl::fromLocalFile(data.fileName).toString().toUtf8();
    bp.lineNumber = data.lineNumber;
    bp.functionName = "TEMPORARY";
    d->breakpoints.insert(bp);
    synchronizeBreakpoints();
    continueInferior();
}

void QScriptDebuggerClient::continueInferior()
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "CONTINUE";
    rs << cmd;
    d->logSendMessage(QLatin1String(cmd));
    sendMessage(reply);
}

void QScriptDebuggerClient::interruptInferior()
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "INTERRUPT";
    rs << cmd;
    d->logSendMessage(QLatin1String(cmd));
    sendMessage(reply);
}

void QScriptDebuggerClient::startSession()
{
    //Flush buffered data
    flushSendBuffer();

    //Set all breakpoints
    BreakHandler *handler = d->engine->breakHandler();
    foreach (BreakpointModelId id, handler->engineBreakpointIds(d->engine)) {
        QTC_CHECK(handler->state(id) == BreakpointInsertProceeding);
        handler->notifyBreakpointInsertOk(id);
    }
}

void QScriptDebuggerClient::endSession()
{
}

void QScriptDebuggerClient::activateFrame(int index)
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "ACTIVATE_FRAME";
    rs << cmd
       << index;
    d->logSendMessage(QLatin1String(cmd) + QLatin1Char(' ') + QString::number(index));
    sendMessage(reply);
}

void QScriptDebuggerClient::insertBreakpoint(const BreakpointModelId &id)
{
    BreakHandler *handler = d->engine->breakHandler();
    JSAgentBreakpointData bp;
    bp.fileUrl = QUrl::fromLocalFile(handler->fileName(id)).toString().toUtf8();
    bp.lineNumber = handler->lineNumber(id);
    bp.functionName = handler->functionName(id).toUtf8();
    d->breakpoints.insert(bp);
}

void QScriptDebuggerClient::removeBreakpoint(const BreakpointModelId &id)
{
    BreakHandler *handler = d->engine->breakHandler();
    JSAgentBreakpointData bp;
    bp.fileUrl = QUrl::fromLocalFile(handler->fileName(id)).toString().toUtf8();
    bp.lineNumber = handler->lineNumber(id);
    bp.functionName = handler->functionName(id).toUtf8();
    d->breakpoints.remove(bp);
}

void QScriptDebuggerClient::changeBreakpoint(const BreakpointModelId &id)
{
    BreakHandler *handler = d->engine->breakHandler();
    if (handler->isEnabled(id)) {
        insertBreakpoint(id);
    } else {
        removeBreakpoint(id);
    }
    BreakpointResponse br = handler->response(id);
    br.enabled = handler->isEnabled(id);
    handler->setResponse(id, br);
}

void QScriptDebuggerClient::synchronizeBreakpoints()
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "BREAKPOINTS";
    rs << cmd
       << d->breakpoints;

    QString logBreakpoints;
    QTextStream str(&logBreakpoints);
    str << cmd << " (";
    bool first = true;
    foreach (const JSAgentBreakpointData &bp, d->breakpoints) {
        if (first) {
            first = false;
        } else {
            str << ", ";
        }
        str << '[' << bp.functionName << ", " << bp.fileUrl << ", " << bp.lineNumber << ']';
    }
    str << ')';
    d->logSendMessage(logBreakpoints);

    sendMessage(reply);
}

void QScriptDebuggerClient::assignValueInDebugger(const QByteArray expr, const quint64 &id,
                                                  const QString &property, const QString &value)
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "SET_PROPERTY";
    rs << cmd;
    rs << expr << id << property << value;
    d->logSendMessage(QString::fromLatin1("%1 %2 %3 %4 %5").
                      arg(QLatin1String(cmd), QLatin1String(expr),
                          QString::number(id), property, value));
    sendMessage(reply);
}

void QScriptDebuggerClient::updateWatchData(const WatchData &data)
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "EXEC";
    rs << cmd;
    rs << data.iname << data.name;
    d->logSendMessage(QLatin1String(cmd) + QLatin1Char(' ') + QLatin1String(data.iname)
                      + QLatin1Char(' ') + data.name);
    sendMessage(reply);
}

void QScriptDebuggerClient::executeDebuggerCommand(const QString &command)
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "EXEC";
    QByteArray console = "console";
    rs << cmd << console << command;
    d->logSendMessage(QLatin1String(cmd) + QLatin1Char(' ') + QLatin1String(console)
                      + QLatin1Char(' ') +  command);
    sendMessage(reply);
}

void QScriptDebuggerClient::synchronizeWatchers(const QStringList &watchers)
{
    // send watchers list
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "WATCH_EXPRESSIONS";
    rs << cmd;
    d->logSendMessage(QString::fromLatin1("%1 (%2)").arg(QLatin1String(cmd),
                                                         watchers.join(QLatin1String(", "))));
    sendMessage(reply);
}

void QScriptDebuggerClient::expandObject(const QByteArray &iname, quint64 objectId)
{
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "EXPAND";
    rs << cmd;
    rs << iname << objectId;
    d->logSendMessage(QLatin1String(cmd) + QLatin1Char(' ') + QLatin1String(iname)
                      + QString::number(objectId));
    sendMessage(reply);
}

void QScriptDebuggerClient::sendPing()
{
    d->ping++;
    QByteArray reply;
    QDataStream rs(&reply, QIODevice::WriteOnly);
    QByteArray cmd = "PING";
    rs << cmd;
    rs << d->ping;
    d->logSendMessage(QLatin1String(cmd));
    sendMessage(reply);
}

void QScriptDebuggerClient::messageReceived(const QByteArray &data)
{
    QByteArray rwData = data;
    QDataStream stream(&rwData, QIODevice::ReadOnly);

    QByteArray command;
    stream >> command;

    if (command == "STOPPED") {
        d->engine->inferiorSpontaneousStop();

        QString logString = QString::fromLatin1(command);

        JSAgentStackFrames stackFrames;
        QList<WatchData> watches;
        QList<WatchData> locals;
        stream >> stackFrames >> watches >> locals;

        logString += QString::fromLatin1(" (%1 stack frames) (%2 watches)  (%3 locals)").
                arg(stackFrames.size()).arg(watches.size()).arg(locals.size());

        StackFrames ideStackFrames;
        for (int i = 0; i != stackFrames.size(); ++i) {
            StackFrame frame;
            frame.line = stackFrames.at(i).lineNumber;
            frame.function = QLatin1String(stackFrames.at(i).functionName);
            frame.file = d->engine->toFileInProject(QUrl(QLatin1String(stackFrames.at(i).fileUrl)));
            frame.usable = QFileInfo(frame.file).isReadable();
            frame.level = i + 1;
            ideStackFrames << frame;
        }

        if (ideStackFrames.size() && ideStackFrames.back().function == QLatin1String("<global>"))
            ideStackFrames.takeLast();

        d->engine->stackHandler()->setFrames(ideStackFrames);

        d->engine->watchHandler()->beginCycle();
        bool needPing = false;

        foreach (WatchData data, watches) {
            data.iname = d->engine->watchHandler()->watcherName(data.exp);
            d->engine->watchHandler()->insertData(data);

            if (d->engine->watchHandler()->expandedINames().contains(data.iname)) {
                needPing = true;
                expandObject(data.iname,data.id);
            }
        }

        foreach (WatchData data, locals) {
            data.iname = "local." + data.exp;
            d->engine->watchHandler()->insertData(data);

            if (d->engine->watchHandler()->expandedINames().contains(data.iname)) {
                needPing = true;
                expandObject(data.iname,data.id);
            }
        }

        if (needPing) {
            sendPing();
        } else {
            d->engine->watchHandler()->endCycle();
        }

        bool becauseOfException;
        stream >> becauseOfException;

        logString += becauseOfException ? QLatin1String(" exception") : QLatin1String(" no_exception");

        if (becauseOfException) {
            QString error;
            stream >> error;

            logString += QLatin1Char(' ');
            logString += error;
            d->logReceiveMessage(logString);

            QString msg = stackFrames.isEmpty()
                    ? tr("<p>An uncaught exception occurred:</p><p>%1</p>")
                      .arg(Qt::escape(error))
                    : tr("<p>An uncaught exception occurred in <i>%1</i>:</p><p>%2</p>")
                      .arg(QLatin1String(stackFrames.value(0).fileUrl), Qt::escape(error));
            showMessageBox(QMessageBox::Information, tr("Uncaught Exception"), msg);
        } else {
            //
            // Make breakpoint non-pending
            //
            QString file;
            QString function;
            int line = -1;

            if (!ideStackFrames.isEmpty()) {
                file = ideStackFrames.at(0).file;
                line = ideStackFrames.at(0).line;
                function = ideStackFrames.at(0).function;
            }

            BreakHandler *handler = d->engine->breakHandler();
            foreach (BreakpointModelId id, handler->engineBreakpointIds(d->engine)) {
                QString processedFilename = handler->fileName(id);

                if (processedFilename == file && handler->lineNumber(id) == line) {
                    if (handler->state(id) == BreakpointInsertProceeding)
                        handler->notifyBreakpointInsertOk(id);
                    QTC_CHECK(handler->state(id) == BreakpointInserted);
                    BreakpointResponse br = handler->response(id);
                    br.fileName = file;
                    br.lineNumber = line;
                    br.functionName = function;
                    handler->setResponse(id, br);
                }
            }

            QList<JSAgentBreakpointData> breakpoints(d->breakpoints.toList());
            foreach (const JSAgentBreakpointData &data, breakpoints) {
                if (data.fileUrl == QUrl::fromLocalFile(file).toString().toUtf8() &&
                        data.lineNumber == line &&
                        data.functionName == "TEMPORARY") {
                    breakpoints.removeOne(data);
                    d->breakpoints = JSAgentBreakpoints::fromList(breakpoints);
                    synchronizeBreakpoints();
                    break;
                }
            }

            d->logReceiveMessage(logString);
        }

        if (!ideStackFrames.isEmpty())
            d->engine->gotoLocation(ideStackFrames.value(0));

    } else if (command == "RESULT") {
        WatchData data;
        QByteArray iname;
        stream >> iname >> data;

        d->logReceiveMessage(QLatin1String(command) + QLatin1Char(' ')
                             +  QLatin1String(iname) + QLatin1Char(' ') + data.value);
        data.iname = iname;
        if (iname.startsWith("watch.")) {
            d->engine->watchHandler()->insertData(data);
        } else if (iname == "console") {
            d->engine->showMessage(data.value, ScriptConsoleOutput);
        } else {
            qWarning() << "QmlEngine: Unexcpected result: " << iname << data.value;
        }
    } else if (command == "EXPANDED") {
        QList<WatchData> result;
        QByteArray iname;
        stream >> iname >> result;
        d->logReceiveMessage(QString::fromLatin1("%1 %2 (%3 x watchdata)").
                             arg(QLatin1String(command), QLatin1String(iname),
                                 QString::number(result.size())));
        bool needPing = false;

        foreach (WatchData data, result) {
            data.iname = iname + '.' + data.exp;
            d->engine->watchHandler()->insertData(data);

            if (d->engine->watchHandler()->expandedINames().contains(data.iname)) {
                needPing = true;
                expandObject(data.iname, data.id);
            }
        }
        if (needPing)
            sendPing();
    } else if (command == "LOCALS") {
        QList<WatchData> locals;
        QList<WatchData> watches;
        int frameId;
        stream >> frameId >> locals;
        if (!stream.atEnd()) { // compatibility with jsdebuggeragent from 2.1, 2.2
            stream >> watches;
        }

        d->logReceiveMessage(QString::fromLatin1("%1 %2 (%3 x locals) (%4 x watchdata)").arg(
                             QLatin1String(command), QString::number(frameId),
                             QString::number(locals.size()), QString::number(watches.size())));
        d->engine->watchHandler()->beginCycle();
        bool needPing = false;
        foreach (WatchData data, watches) {
            data.iname = d->engine->watchHandler()->watcherName(data.exp);
            d->engine->watchHandler()->insertData(data);

            if (d->engine->watchHandler()->expandedINames().contains(data.iname)) {
                needPing = true;
                expandObject(data.iname, data.id);
            }
        }

        foreach (WatchData data, locals) {
            data.iname = "local." + data.exp;
            d->engine->watchHandler()->insertData(data);
            if (d->engine->watchHandler()->expandedINames().contains(data.iname)) {
                needPing = true;
                expandObject(data.iname, data.id);
            }
        }
        if (needPing)
            sendPing();
        else
            d->engine->watchHandler()->endCycle();

    } else if (command == "PONG") {
        int ping;
        stream >> ping;

        d->logReceiveMessage(QLatin1String(command) + QLatin1Char(' ') + QString::number(ping));

        if (ping == d->ping)
            d->engine->watchHandler()->endCycle();
    } else {
        qDebug() << Q_FUNC_INFO << "Unknown command: " << command;
        d->logReceiveMessage(QLatin1String(command) + QLatin1String(" UNKNOWN COMMAND!!"));
    }

}

void QScriptDebuggerClient::setEngine(QmlEngine *engine)
{
    d->engine = engine;
}

void QScriptDebuggerClientPrivate::logSendMessage(const QString &msg) const
{
    if (engine)
        engine->logMessage(QLatin1String("QScriptDebuggerClient"), QmlEngine::LogSend, msg);
}

void QScriptDebuggerClientPrivate::logReceiveMessage(const QString &msg) const
{
    if (engine)
        engine->logMessage(QLatin1String("QScriptDebuggerClient"), QmlEngine::LogReceive, msg);
}

} // Internal
} // Debugger