qmlengine.cpp 94.6 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30 31

#include "qmlengine.h"
hjk's avatar
hjk committed
32 33 34

#include "interactiveinterpreter.h"
#include "qmlinspectoradapter.h"
35
#include "qmlinspectoragent.h"
hjk's avatar
hjk committed
36 37
#include "qmlv8debuggerclientconstants.h"
#include "qmlengineutils.h"
38

hjk's avatar
hjk committed
39
#include <debugger/breakhandler.h>
40 41 42
#include <debugger/debuggeractions.h>
#include <debugger/debuggercore.h>
#include <debugger/debuggerinternalconstants.h>
43
#include <debugger/debuggerruncontrol.h>
44 45
#include <debugger/debuggerstringutils.h>
#include <debugger/debuggertooltipmanager.h>
hjk's avatar
hjk committed
46 47
#include <debugger/sourcefileshandler.h>
#include <debugger/stackhandler.h>
Christian Kandeler's avatar
Christian Kandeler committed
48
#include <debugger/threaddata.h>
hjk's avatar
hjk committed
49
#include <debugger/watchhandler.h>
50
#include <debugger/watchwindow.h>
51

hjk's avatar
hjk committed
52 53 54 55 56
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/helpmanager.h>
#include <coreplugin/icore.h>

#include <projectexplorer/applicationlauncher.h>
57

58
#include <qmljseditor/qmljseditorconstants.h>
59
#include <qmljs/qmljsmodelmanagerinterface.h>
60
#include <qmljs/consolemanagerinterface.h>
61

62 63
#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
hjk's avatar
hjk committed
64 65

#include <utils/qtcassert.h>
66

67 68
#include <QDebug>
#include <QDir>
69
#include <QDockWidget>
hjk's avatar
hjk committed
70 71 72 73
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
74 75
#include <QMessageBox>
#include <QPlainTextEdit>
hjk's avatar
hjk committed
76
#include <QTimer>
77

hjk's avatar
hjk committed
78
#define DEBUG_QML 0
79 80 81 82 83 84 85
#if DEBUG_QML
#   define SDEBUG(s) qDebug() << s
#else
#   define SDEBUG(s)
#endif
# define XSDEBUG(s) qDebug() << s

hjk's avatar
hjk committed
86 87 88
using namespace Core;
using namespace ProjectExplorer;
using namespace QmlDebug;
89
using namespace QmlJS;
hjk's avatar
hjk committed
90
using namespace TextEditor;
hjk's avatar
hjk committed
91

92 93 94
namespace Debugger {
namespace Internal {

hjk's avatar
hjk committed
95
enum Exceptions
96
{
hjk's avatar
hjk committed
97 98 99 100
    NoExceptions,
    UncaughtExceptions,
    AllExceptions
};
101

hjk's avatar
hjk committed
102
enum StepAction
103
{
hjk's avatar
hjk committed
104 105 106 107 108
    Continue,
    StepIn,
    StepOut,
    Next
};
109

hjk's avatar
hjk committed
110 111 112 113 114 115 116 117
struct QmlV8ObjectData
{
    int handle;
    QByteArray name;
    QByteArray type;
    QVariant value;
    QVariantList properties;
};
118

hjk's avatar
hjk committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
class QmlEnginePrivate : QmlDebugClient
{
public:
    QmlEnginePrivate(QmlEngine *engine_, QmlDebugConnection *connection_)
        : QmlDebugClient(QLatin1String("V8Debugger"), connection_),
          engine(engine_),
          inspectorAdapter(engine, connection_),
          connection(connection_)
    {}

    void sendMessage(const QByteArray &msg);
    void messageReceived(const QByteArray &data);
    void stateChanged(State state);

    void continueDebugging(StepAction stepAction);

    void evaluate(const QString expr, bool global = false, bool disableBreak = false,
                  int frame = -1, bool addContext = false);
    void lookup(const QList<int> handles, bool includeSource = false);
    void backtrace(int fromFrame = -1, int toFrame = -1, bool bottom = false);
139 140
    void frame(int number);
    void scope(int number, int frameNumber = -1);
hjk's avatar
hjk committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154
    void scripts(int types = 4, const QList<int> ids = QList<int>(),
                 bool includeSource = false, const QVariant filter = QVariant());

    void setBreakpoint(const QString type, const QString target,
                       bool enabled = true,int line = 0, int column = 0,
                       const QString condition = QString(), int ignoreCount = -1);
    void clearBreakpoint(int breakpoint);
    void setExceptionBreak(Exceptions type, bool enabled = false);

    void clearCache();

    void expandObject(const QByteArray &iname, quint64 objectId);
    void flushSendBuffer();

155 156 157 158 159
    void handleBacktrace(const QVariant &bodyVal, const QVariant &refsVal);
    void handleLookup(const QVariant &bodyVal, const QVariant &refsVal);
    void handleEvaluate(int sequence, bool success, const QVariant &bodyVal, const QVariant &refsVal);
    void handleFrame(const QVariant &bodyVal, const QVariant &refsVal);
    void handleScope(const QVariant &bodyVal, const QVariant &refsVal);
hjk's avatar
hjk committed
160 161 162 163
    StackFrame extractStackFrame(const QVariant &bodyVal, const QVariant &refsVal);

    bool canEvaluateScript(const QString &script);
    void updateScriptSource(const QString &fileName, int lineOffset, int columnOffset, const QString &source);
164

165
    void runCommand(const DebuggerCommand &command);
166
    void runDirectCommand(const QByteArray &type, const QByteArray &msg = QByteArray());
167

hjk's avatar
hjk committed
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
public:
    int sequence = -1;
    QmlEngine *engine;
    QHash<BreakpointModelId, int> breakpoints;
    QHash<int, BreakpointModelId> breakpointsSync;
    QList<int> breakpointsTemp;

    QHash<int, QString> evaluatingExpression;
    QHash<int, QByteArray> localsAndWatchers;
    QList<int> updateLocalsAndWatchers;
    QList<int> debuggerCommands;

    //Cache
    QList<int> currentFrameScopes;
    QHash<int, int> stackIndexLookup;

    StepAction previousStepAction = Continue;

    QList<QByteArray> sendBuffer;

    QHash<QString, QTextDocument*> sourceDocuments;
    QHash<QString, QWeakPointer<BaseTextEditor> > sourceEditors;
    InteractiveInterpreter interpreter;
    ApplicationLauncher applicationLauncher;
    QmlInspectorAdapter inspectorAdapter;
    QmlOutputParser outputParser;

    QTimer noDebugOutputTimer;
    QHash<QString,Breakpoint> pendingBreakpoints;
    QList<quint32> queryIds;
    bool retryOnConnectFail = false;
    bool automaticConnect = false;

    QTimer connectionTimer;
    QmlDebug::QmlDebugConnection *connection;
    QmlDebug::QDebugMessageClient *msgClient = 0;
204
};
205

hjk's avatar
hjk committed
206
static void updateDocument(IDocument *document, const QTextDocument *textDocument)
207
{
hjk's avatar
hjk committed
208 209
    if (auto baseTextDocument = qobject_cast<TextDocument *>(document))
        baseTextDocument->document()->setPlainText(textDocument->toPlainText());
210 211
}

hjk's avatar
hjk committed
212

213 214 215 216 217 218
///////////////////////////////////////////////////////////////////////
//
// QmlEngine
//
///////////////////////////////////////////////////////////////////////

219
QmlEngine::QmlEngine(const DebuggerRunParameters &startParameters, DebuggerEngine *masterEngine)
hjk's avatar
hjk committed
220 221
  : DebuggerEngine(startParameters),
    d(new QmlEnginePrivate(this, new QmlDebugConnection(this)))
222
{
Friedemann Kleint's avatar
Friedemann Kleint committed
223
    setObjectName(QLatin1String("QmlEngine"));
224

Aurindam Jana's avatar
Aurindam Jana committed
225 226 227
    if (masterEngine)
        setMasterEngine(masterEngine);

hjk's avatar
hjk committed
228 229 230 231 232
    connect(stackHandler(), &StackHandler::stackChanged,
            this, &QmlEngine::updateCurrentContext);
    connect(stackHandler(), &StackHandler::currentIndexChanged,
            this, &QmlEngine::updateCurrentContext);
    connect(inspectorView(), SIGNAL(currentIndexChanged(QModelIndex)),
233
            SLOT(updateCurrentContext()));
hjk's avatar
hjk committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    connect(d->inspectorAdapter.agent(), &QmlInspectorAgent::expressionResult,
            this, &QmlEngine::expressionEvaluated);

    connect(&d->applicationLauncher, &ApplicationLauncher::processExited,
            this, &QmlEngine::disconnected);
    connect(&d->applicationLauncher, &ApplicationLauncher::appendMessage,
            this, &QmlEngine::appendMessage);
    connect(&d->applicationLauncher, &ApplicationLauncher::processStarted,
            &d->noDebugOutputTimer, static_cast<void(QTimer::*)()>(&QTimer::start));

    d->outputParser.setNoOutputText(ApplicationLauncher::msgWinCannotRetrieveDebuggingOutput());
    connect(&d->outputParser, &QmlOutputParser::waitingForConnectionOnPort,
            this, &QmlEngine::beginConnection);
    connect(&d->outputParser, &QmlOutputParser::noOutputMessage,
            this, [this] { tryToConnect(); });
    connect(&d->outputParser, &QmlOutputParser::errorMessage,
            this, &QmlEngine::appStartupFailed);
251

Kai Koehne's avatar
Kai Koehne committed
252 253
    // Only wait 8 seconds for the 'Waiting for connection' on application output,
    // then just try to connect (application output might be redirected / blocked)
hjk's avatar
hjk committed
254 255 256
    d->noDebugOutputTimer.setSingleShot(true);
    d->noDebugOutputTimer.setInterval(8000);
    connect(&d->noDebugOutputTimer, SIGNAL(timeout()), this, SLOT(tryToConnect()));
257

hjk's avatar
hjk committed
258 259 260
    if (auto mmIface = ModelManagerInterface::instance()) {
        connect(mmIface, &ModelManagerInterface::documentUpdated,
                this, &QmlEngine::documentUpdated);
261
    }
262
    // we won't get any debug output
263
    if (startParameters.useTerminal) {
hjk's avatar
hjk committed
264 265 266
        d->noDebugOutputTimer.setInterval(0);
        d->retryOnConnectFail = true;
        d->automaticConnect = true;
267
    }
hjk's avatar
hjk committed
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

    if (auto consoleManager = ConsoleManagerInterface::instance())
        consoleManager->setScriptEvaluator(this);


    d->connectionTimer.setInterval(4000);
    d->connectionTimer.setSingleShot(true);
    connect(&d->connectionTimer, &QTimer::timeout,
            this, &QmlEngine::checkConnectionState);

    connect(d->connection, &QmlDebugConnection::stateMessage,
            this, &QmlEngine::showConnectionStateMessage);
    connect(d->connection, &QmlDebugConnection::errorMessage,
            this, &QmlEngine::showConnectionErrorMessage);
    connect(d->connection, &QmlDebugConnection::error,
            this, &QmlEngine::connectionErrorOccurred);
    connect(d->connection, &QmlDebugConnection::opened,
            &d->connectionTimer, &QTimer::stop);
    connect(d->connection, &QmlDebugConnection::opened,
            this, &QmlEngine::connectionEstablished);
    connect(d->connection, &QmlDebugConnection::closed,
            this, &QmlEngine::disconnected);

    d->msgClient = new QDebugMessageClient(d->connection);
    connect(d->msgClient, &QDebugMessageClient::newState,
            this, &QmlEngine::clientStateChanged);
    connect(d->msgClient, &QDebugMessageClient::message,
            this, &appendDebugOutput);
296 297 298
}

QmlEngine::~QmlEngine()
hjk's avatar
hjk committed
299
{
hjk's avatar
hjk committed
300
    QSet<IDocument *> documentsToClose;
301

hjk's avatar
hjk committed
302 303 304
    QHash<QString, QWeakPointer<BaseTextEditor> >::iterator iter;
    for (iter = d->sourceEditors.begin(); iter != d->sourceEditors.end(); ++iter) {
        QWeakPointer<BaseTextEditor> textEditPtr = iter.value();
305
        if (textEditPtr)
306
            documentsToClose << textEditPtr.data()->document();
307
    }
hjk's avatar
hjk committed
308 309 310
    EditorManager::closeDocuments(documentsToClose.toList());

    delete d;
hjk's avatar
hjk committed
311
}
312

hjk's avatar
hjk committed
313
void QmlEngine::setupInferior()
314
{
hjk's avatar
hjk committed
315
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
316

317
    notifyInferiorSetupOk();
318

hjk's avatar
hjk committed
319
    if (d->automaticConnect)
320
        beginConnection();
hjk's avatar
hjk committed
321
}
hjk's avatar
hjk committed
322

con's avatar
con committed
323
void QmlEngine::appendMessage(const QString &msg, Utils::OutputFormat /* format */)
hjk's avatar
hjk committed
324
{
325
    showMessage(msg, AppOutput); // FIXME: Redirect to RunControl
hjk's avatar
hjk committed
326 327
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
328 329 330 331
void QmlEngine::connectionEstablished()
{
    attemptBreakpointSynchronization();

332
    if (!watchHandler()->watcherNames().isEmpty())
333
        synchronizeWatchers();
hjk's avatar
hjk committed
334 335
    connect(watchModel(), &QAbstractItemModel::layoutChanged,
            this, &QmlEngine::synchronizeWatchers);
Christiaan Janssen's avatar
Christiaan Janssen committed
336

337 338
    if (state() == EngineRunRequested)
        notifyEngineRunAndInferiorRunOk();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
339
}
340

341 342
void QmlEngine::tryToConnect(quint16 port)
{
343
    showMessage(QLatin1String("QML Debugger: No application output received in time, trying to connect ..."), LogStatus);
hjk's avatar
hjk committed
344
    d->retryOnConnectFail = true;
345 346 347
    if (state() == EngineRunRequested) {
        if (isSlaveEngine()) {
            // Probably cpp is being debugged and hence we did not get the output yet.
348
            if (!masterEngine()->isDying()) {
hjk's avatar
hjk committed
349 350
                d->noDebugOutputTimer.setInterval(4000);
                d->noDebugOutputTimer.start();
351
            }
352 353 354 355 356 357
            else
                appStartupFailed(tr("No application output received in time"));
        } else {
            beginConnection(port);
        }
    } else {
hjk's avatar
hjk committed
358
        d->automaticConnect = true;
359
    }
360 361
}

362
void QmlEngine::beginConnection(quint16 port)
363
{
hjk's avatar
hjk committed
364
    d->noDebugOutputTimer.stop();
365

hjk's avatar
hjk committed
366
    if (state() != EngineRunRequested && d->retryOnConnectFail)
367 368
        return;

369
    QTC_ASSERT(state() == EngineRunRequested, return);
370

371
    QString host =  runParameters().qmlServerAddress;
372 373 374 375
    // Use localhost as default
    if (host.isEmpty())
        host = QLatin1String("localhost");

376 377 378 379 380 381 382 383 384 385 386
    /*
     * Let plugin-specific code override the port printed by the application. This is necessary
     * in the case of port forwarding, when the port the application listens on is not the same that
     * we want to connect to.
     * NOTE: It is still necessary to wait for the output in that case, because otherwise we cannot
     * be sure that the port is already open. The usual method of trying to connect repeatedly
     * will not work, because the intermediate port is already open. So the connection
     * will be accepted on that port but the forwarding to the target port will fail and
     * the connection will be closed again (instead of returning the "connection refused"
     * error that we expect).
     */
387 388
    if (runParameters().qmlServerPort > 0)
        port = runParameters().qmlServerPort;
389

hjk's avatar
hjk committed
390 391
    if (!d->connection || d->connection->isOpen())
        return;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
392

hjk's avatar
hjk committed
393 394 395 396 397
    d->connection->connectToHost(host, port);

    //A timeout to check the connection state
    d->connectionTimer.start();
}
398

399
void QmlEngine::connectionStartupFailed()
Lasse Holmstedt's avatar
Lasse Holmstedt committed
400
{
hjk's avatar
hjk committed
401
    if (d->retryOnConnectFail) {
402 403
        // retry after 3 seconds ...
        QTimer::singleShot(3000, this, SLOT(beginConnection()));
404 405
        return;
    }
406

hjk's avatar
hjk committed
407
    QMessageBox *infoBox = new QMessageBox(ICore::mainWindow());
408 409
    infoBox->setIcon(QMessageBox::Critical);
    infoBox->setWindowTitle(tr("Qt Creator"));
410 411 412 413 414
    infoBox->setText(tr("Could not connect to the in-process QML debugger."
                        "\nDo you want to retry?"));
    infoBox->setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel |
                                QMessageBox::Help);
    infoBox->setDefaultButton(QMessageBox::Retry);
415 416
    infoBox->setModal(true);

hjk's avatar
hjk committed
417 418
    connect(infoBox, &QDialog::finished,
            this, &QmlEngine::errorMessageBoxFinished);
419 420 421 422

    infoBox->show();
}

423 424
void QmlEngine::appStartupFailed(const QString &errorMessage)
{
425 426 427 428
    QString error = tr("Could not connect to the in-process QML debugger."
                       "\n%1").arg(errorMessage);

    if (isMasterEngine()) {
hjk's avatar
hjk committed
429
        QMessageBox *infoBox = new QMessageBox(ICore::mainWindow());
430 431 432 433 434
        infoBox->setIcon(QMessageBox::Critical);
        infoBox->setWindowTitle(tr("Qt Creator"));
        infoBox->setText(error);
        infoBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Help);
        infoBox->setDefaultButton(QMessageBox::Ok);
hjk's avatar
hjk committed
435 436
        connect(infoBox, &QDialog::finished,
                this, &QmlEngine::errorMessageBoxFinished);
437 438 439 440
        infoBox->show();
    } else {
        showMessage(error, StatusBar);
    }
441 442 443 444

    notifyEngineRunFailed();
}

445
void QmlEngine::errorMessageBoxFinished(int result)
446 447
{
    switch (result) {
448
    case QMessageBox::Retry: {
449
        beginConnection();
450 451 452
        break;
    }
    case QMessageBox::Help: {
hjk's avatar
hjk committed
453
        HelpManager::handleHelpRequest(QLatin1String("qthelp://org.qt-project.qtcreator/doc/creator-debugging-qml.html"));
454
        // fall through
455 456
    }
    default:
457 458 459
        if (state() == InferiorRunOk) {
            notifyInferiorSpontaneousStop();
            notifyInferiorIll();
460
        } else if (state() == EngineRunRequested) {
461
            notifyEngineRunFailed();
462
        }
Kai Koehne's avatar
Kai Koehne committed
463
        break;
464
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
465 466
}

hjk's avatar
hjk committed
467
void QmlEngine::filterApplicationMessage(const QString &output, int /*channel*/) const
Lasse Holmstedt's avatar
Lasse Holmstedt committed
468
{
hjk's avatar
hjk committed
469
    d->outputParser.processOutput(output);
470 471 472 473
}

void QmlEngine::showMessage(const QString &msg, int channel, int timeout) const
{
474
    if (channel == AppOutput || channel == AppError)
hjk's avatar
hjk committed
475
        filterApplicationMessage(msg, channel);
476 477 478
    DebuggerEngine::showMessage(msg, channel, timeout);
}

479 480 481
void QmlEngine::gotoLocation(const Location &location)
{
    const QString fileName = location.fileName();
482
    if (QUrl(fileName).isLocalFile()) {
483
        // internal file from source files -> show generated .js
hjk's avatar
hjk committed
484
        QTC_ASSERT(d->sourceDocuments.contains(fileName), return);
485

Aurindam Jana's avatar
Aurindam Jana committed
486
        QString titlePattern = tr("JS Source for %1").arg(fileName);
487
        //Check if there are open documents with the same title
hjk's avatar
hjk committed
488
        foreach (IDocument *document, DocumentModel::openedDocuments()) {
489
            if (document->displayName() == titlePattern) {
hjk's avatar
hjk committed
490
                EditorManager::activateEditorForDocument(document);
491
                return;
Aurindam Jana's avatar
Aurindam Jana committed
492 493
            }
        }
hjk's avatar
hjk committed
494
        IEditor *editor = EditorManager::openEditorWithContents(
495 496 497
                    QmlJSEditor::Constants::C_QMLJSEDITOR_ID, &titlePattern);
        if (editor) {
            editor->document()->setProperty(Constants::OPENED_BY_DEBUGGER, true);
hjk's avatar
hjk committed
498
            if (auto plainTextEdit = qobject_cast<QPlainTextEdit *>(editor->widget()))
499
                plainTextEdit->setReadOnly(true);
hjk's avatar
hjk committed
500
            updateDocument(editor->document(), d->sourceDocuments.value(fileName));
501
        }
502 503 504 505 506
    } else {
        DebuggerEngine::gotoLocation(location);
    }
}

507 508
void QmlEngine::closeConnection()
{
hjk's avatar
hjk committed
509 510 511 512 513 514 515 516 517
    disconnect(watchModel(), &QAbstractItemModel::layoutChanged,
               this, &QmlEngine::synchronizeWatchers);

    if (d->connectionTimer.isActive()) {
        d->connectionTimer.stop();
    } else {
        if (d->connection)
            d->connection->close();
    }
518 519
}

hjk's avatar
hjk committed
520 521 522
void QmlEngine::runEngine()
{
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
523

524
    if (!isSlaveEngine()) {
525
        if (runParameters().startMode == AttachToRemoteServer)
hjk's avatar
hjk committed
526
            d->noDebugOutputTimer.start();
527
        else if (runParameters().startMode == AttachToRemoteProcess)
528 529
            beginConnection();
        else
530
            startApplicationLauncher();
531
    } else {
hjk's avatar
hjk committed
532
        d->noDebugOutputTimer.start();
533
    }
534 535 536 537
}

void QmlEngine::startApplicationLauncher()
{
hjk's avatar
hjk committed
538
    if (!d->applicationLauncher.isRunning()) {
539
        appendMessage(tr("Starting %1 %2").arg(
540 541
                          QDir::toNativeSeparators(runParameters().executable),
                          runParameters().processArgs)
542
                      + QLatin1Char('\n')
con's avatar
con committed
543
                     , Utils::NormalMessageFormat);
hjk's avatar
hjk committed
544
        d->applicationLauncher.start(ApplicationLauncher::Gui,
545 546
                                    runParameters().executable,
                                    runParameters().processArgs);
547
    }
548
}
Lasse Holmstedt's avatar
Lasse Holmstedt committed
549

550 551
void QmlEngine::stopApplicationLauncher()
{
hjk's avatar
hjk committed
552 553 554 555
    if (d->applicationLauncher.isRunning()) {
        disconnect(&d->applicationLauncher, &ApplicationLauncher::processExited,
                   this, &QmlEngine::disconnected);
        d->applicationLauncher.stop();
556
    }
557 558
}

559
void QmlEngine::notifyEngineRemoteSetupFinished(const RemoteSetupResult &result)
560
{
561
    DebuggerEngine::notifyEngineRemoteSetupFinished(result);
562

563 564
    if (result.success) {
        if (result.qmlServerPort != InvalidPort)
565
            runParameters().qmlServerPort = result.qmlServerPort;
566

567
        notifyEngineSetupOk();
568

569 570 571
        // The remote setup can take while especialy with mixed debugging.
        // Just waiting for 8 seconds is not enough. Increase the timeout
        // to 60 s
hjk's avatar
hjk committed
572 573
        // In case we get an output the d->outputParser will start the connection.
        d->noDebugOutputTimer.setInterval(60000);
574 575 576
    }
    else {
        if (isMasterEngine())
hjk's avatar
hjk committed
577
            QMessageBox::critical(ICore::dialogParent(), tr("Failed to start application"),
578 579 580
                                  tr("Application startup failed: %1").arg(result.reason));
        notifyEngineSetupFailed();
    }
581 582
}

Aurindam Jana's avatar
Aurindam Jana committed
583 584 585 586 587
void QmlEngine::notifyEngineRemoteServerRunning(const QByteArray &serverChannel, int pid)
{
    bool ok = false;
    quint16 qmlPort = serverChannel.toUInt(&ok);
    if (ok)
588
        runParameters().qmlServerPort = qmlPort;
Aurindam Jana's avatar
Aurindam Jana committed
589
    else
Friedemann Kleint's avatar
Friedemann Kleint committed
590
        qWarning() << tr("QML debugging port not set: Unable to convert %1 to unsigned int.").arg(QString::fromLatin1(serverChannel));
Aurindam Jana's avatar
Aurindam Jana committed
591 592 593 594 595

    DebuggerEngine::notifyEngineRemoteServerRunning(serverChannel, pid);
    notifyEngineSetupOk();

    // The remote setup can take a while especially with mixed debugging.
hjk's avatar
hjk committed
596 597 598
    // Just waiting for 8 seconds is not enough. Increase the timeout to 60 s.
    // In case we get an output the d->outputParser will start the connection.
    d->noDebugOutputTimer.setInterval(60000);
Aurindam Jana's avatar
Aurindam Jana committed
599 600
}

hjk's avatar
hjk committed
601
void QmlEngine::shutdownInferior()
602
{
hjk's avatar
hjk committed
603
    // End session.
604 605 606 607 608
    //    { "seq"     : <number>,
    //      "type"    : "request",
    //      "command" : "disconnect",
    //    }
    d->runCommand(DISCONNECT);
609

610
    if (isSlaveEngine())
611 612
        resetLocation();
    stopApplicationLauncher();
613
    closeConnection();
614

hjk's avatar
hjk committed
615 616 617 618 619
    notifyInferiorShutdownOk();
}

void QmlEngine::shutdownEngine()
{
hjk's avatar
hjk committed
620
    clearExceptionSelection();
621

hjk's avatar
hjk committed
622 623 624
    if (auto consoleManager = ConsoleManagerInterface::instance())
        consoleManager->setScriptEvaluator(0);
    d->noDebugOutputTimer.stop();
625 626

   // double check (ill engine?):
627
    stopApplicationLauncher();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
628

629
    notifyEngineShutdownOk();
630 631
    if (!isSlaveEngine())
        showMessage(QString(), StatusBar);
632 633
}

hjk's avatar
hjk committed
634
void QmlEngine::setupEngine()
635
{
636
    if (runParameters().remoteSetupNeeded) {
637
        // we need to get the port first
638
        notifyEngineRequestRemoteSetup();
639
    } else {
hjk's avatar
hjk committed
640 641
        d->applicationLauncher.setEnvironment(runParameters().environment);
        d->applicationLauncher.setWorkingDirectory(runParameters().workingDirectory);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
642

643
        // We can't do this in the constructore because runControl() isn't yet defined
hjk's avatar
hjk committed
644 645
        connect(&d->applicationLauncher, &ApplicationLauncher::bringToForegroundRequested,
                runControl(), &RunControl::bringApplicationToForeground,
646 647 648 649
                Qt::UniqueConnection);

        notifyEngineSetupOk();
    }
650 651
}

652 653
void QmlEngine::continueInferior()
{
hjk's avatar
hjk committed
654
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
hjk's avatar
hjk committed
655 656
    clearExceptionSelection();
    d->continueDebugging(Continue);
657
    resetLocation();
hjk's avatar
hjk committed
658 659
    notifyInferiorRunRequested();
    notifyInferiorRunOk();
660 661 662 663
}

void QmlEngine::interruptInferior()
{
hjk's avatar
hjk committed
664
    showMessage(_(INTERRUPT), LogInput);
665
    d->runDirectCommand(INTERRUPT);
666
    notifyInferiorStopOk();
667 668 669 670
}

void QmlEngine::executeStep()
{
hjk's avatar
hjk committed
671 672
    clearExceptionSelection();
    d->continueDebugging(StepIn);
hjk's avatar
hjk committed
673 674
    notifyInferiorRunRequested();
    notifyInferiorRunOk();
675 676 677 678
}

void QmlEngine::executeStepI()
{
hjk's avatar
hjk committed
679 680
    clearExceptionSelection();
    d->continueDebugging(StepIn);
hjk's avatar
hjk committed
681 682
    notifyInferiorRunRequested();
    notifyInferiorRunOk();
683 684 685 686
}

void QmlEngine::executeStepOut()
{
hjk's avatar
hjk committed
687 688
    clearExceptionSelection();
    d->continueDebugging(StepOut);
hjk's avatar
hjk committed
689 690
    notifyInferiorRunRequested();
    notifyInferiorRunOk();
691 692 693 694
}

void QmlEngine::executeNext()
{
hjk's avatar
hjk committed
695 696
    clearExceptionSelection();
    d->continueDebugging(Next);
hjk's avatar
hjk committed
697 698
    notifyInferiorRunRequested();
    notifyInferiorRunOk();
699 700 701 702
}

void QmlEngine::executeNextI()
{
703
    executeNext();
704 705
}

706
void QmlEngine::executeRunToLine(const ContextData &data)
707
{
708
    QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
Jarek Kobus's avatar
Jarek Kobus committed
709
    showStatusMessage(tr("Run to line %1 (%2) requested...").arg(data.lineNumber).arg(data.fileName), 5000);
710
    resetLocation();
711 712 713 714 715 716
    ContextData modifiedData = data;
    quint32 line = data.lineNumber;
    quint32 column;
    bool valid;
    if (adjustBreakpointLineAndColumn(data.fileName, &line, &column, &valid))
        modifiedData.lineNumber = line;
hjk's avatar
hjk committed
717 718 719 720 721
    d->setBreakpoint(QString(_(SCRIPTREGEXP)), modifiedData.fileName,
                     true, modifiedData.lineNumber);
    clearExceptionSelection();
    d->continueDebugging(Continue);

722 723
    notifyInferiorRunRequested();
    notifyInferiorRunOk();
724 725 726 727 728 729 730 731
}

void QmlEngine::executeRunToFunction(const QString &functionName)
{
    Q_UNUSED(functionName)
    XSDEBUG("FIXME:  QmlEngine::executeRunToFunction()");
}

732
void QmlEngine::executeJumpToLine(const ContextData &data)
733
{
734
    Q_UNUSED(data)
735 736 737 738 739
    XSDEBUG("FIXME:  QmlEngine::executeJumpToLine()");
}

void QmlEngine::activateFrame(int index)
{
740 741 742
    if (state() != InferiorStopOk && state() != InferiorUnrunnable)
        return;

hjk's avatar
hjk committed
743 744 745 746
    if (index != stackHandler()->currentIndex())
        d->frame(d->stackIndexLookup.value(index));

    stackHandler()->setCurrentIndex(index);
747
    gotoLocation(stackHandler()->frames().value(index));
748 749
}

hjk's avatar
hjk committed
750
void QmlEngine::selectThread(ThreadId threadId)
751
{
hjk's avatar
hjk committed
752
    Q_UNUSED(threadId)
753 754
}

755
void QmlEngine::insertBreakpoint(Breakpoint bp)
756
{
757 758 759
    BreakpointState state = bp.state();
    QTC_ASSERT(state == BreakpointInsertRequested, qDebug() << bp << this << state);
    bp.notifyBreakpointInsertProceeding();
760

761
    const BreakpointParameters &params = bp.parameters();
762 763 764 765 766 767
    quint32 line = params.lineNumber;
    quint32 column = 0;
    if (params.type == BreakpointByFileAndLine) {
        bool valid = false;
        if (!adjustBreakpointLineAndColumn(params.fileName, &line, &column,
                                           &valid)) {
hjk's avatar
hjk committed
768
            d->pendingBreakpoints.insertMulti(params.fileName, bp);
769 770 771 772 773 774
            return;
        }
        if (!valid)
            return;
    }

hjk's avatar
hjk committed
775 776 777 778 779 780 781 782 783 784 785 786
    if (params.type == BreakpointAtJavaScriptThrow) {
        bp.notifyBreakpointInsertOk();
        d->setExceptionBreak(AllExceptions, params.enabled);

    } else if (params.type == BreakpointByFileAndLine) {
        d->setBreakpoint(QString(_(SCRIPTREGEXP)), params.fileName,
                         params.enabled, line, column,
                         QLatin1String(params.condition), params.ignoreCount);

    } else if (params.type == BreakpointOnQmlSignalEmit) {
        d->setBreakpoint(QString(_(EVENT)), params.functionName, params.enabled);
        bp.notifyBreakpointInsertOk();
787
    }
hjk's avatar
hjk committed
788 789

    d->breakpointsSync.insert(d->sequence, bp.id());
790 791
}

792
void QmlEngine::removeBreakpoint(Breakpoint bp)
793
{
794
    const BreakpointParameters &params = bp.parameters();
795
    if (params.type == BreakpointByFileAndLine &&
hjk's avatar
hjk committed
796 797 798
            d->pendingBreakpoints.contains(params.fileName)) {
        auto it = d->pendingBreakpoints.find(params.fileName);
        while (it != d->pendingBreakpoints.end() && it.key() == params.fileName) {
799
            if (it.value() == bp.id()) {
hjk's avatar
hjk committed
800
                d->pendingBreakpoints.erase(it);
801 802
                return;
            }
803
            ++it;
804 805 806
        }
    }

807 808 809
    BreakpointState state = bp.state();
    QTC_ASSERT(state == BreakpointRemoveRequested, qDebug() << bp << this << state);
    bp.notifyBreakpointRemoveProceeding();
810

hjk's avatar
hjk committed
811 812 813 814 815 816 817 818 819
    int breakpoint = d->breakpoints.value(bp.id());
    d->breakpoints.remove(bp.id());

    if (params.type == BreakpointAtJavaScriptThrow)
        d->setExceptionBreak(AllExceptions);
    else if (params.type == BreakpointOnQmlSignalEmit)
        d->setBreakpoint(QString(_(EVENT)), params.functionName, false);
    else
        d->clearBreakpoint(breakpoint);
820

821 822
    if (bp.state() == BreakpointRemoveProceeding)
        bp.notifyBreakpointRemoveOk();
823 824
}

825
void QmlEngine::changeBreakpoint(Breakpoint bp)
826
{
827 828 829
    BreakpointState state = bp.state();
    QTC_ASSERT(state == BreakpointChangeRequested, qDebug() << bp << this << state);
    bp.notifyBreakpointChangeProceeding();
830

hjk's avatar
hjk committed
831 832 833 834 835 836 837 838 839 840 841
    const BreakpointParameters &params = bp.parameters();

    BreakpointResponse br = bp.response();
    if (params.type == BreakpointAtJavaScriptThrow) {
        d->setExceptionBreak(AllExceptions, params.enabled);
        br.enabled = params.enabled;
        bp.setResponse(br);
    } else if (params.type == BreakpointOnQmlSignalEmit) {
        d->setBreakpoint(QString(_(EVENT)), params.functionName, params.enabled);
        br.enabled = params.enabled;
        bp.setResponse(br);
842
    } else {
hjk's avatar
hjk committed
843 844 845 846 847 848
        //V8 supports only minimalistic changes in breakpoint
        //Remove the breakpoint and add again
        bp.notifyBreakpointChangeOk();
        bp.removeBreakpoint();
        BreakHandler *handler = d->engine->breakHandler();
        handler->appendBreakpoint(params);
849 850
    }

851 852
    if (bp.state() == BreakpointChangeProceeding)
        bp.notifyBreakpointChangeOk();
853 854
}

855 856
void QmlEngine::attemptBreakpointSynchronization()
{
857 858 859 860 861
    if (!stateAcceptsBreakpointChanges()) {
        showMessage(_("BREAKPOINT SYNCHRONIZATION NOT POSSIBLE IN CURRENT STATE"));
        return;
    }

hjk's avatar
hjk committed
862
    BreakHandler *handler = breakHandler();
863

864
    DebuggerEngine *bpOwner = isSlaveEngine() ? masterEngine() : this;
865
    foreach (Breakpoint bp, handler->unclaimedBreakpoints()) {
866
        // Take ownership of the breakpoint. Requests insertion.
867 868
        if (acceptsBreakpoint(bp))
            bp.setEngine(bpOwner);
869 870
    }

871 872
    foreach (Breakpoint bp, handler->engineBreakpoints(bpOwner)) {
        switch (bp.state()) {
873 874 875 876 877
        case BreakpointNew:
            // Should not happen once claimed.