Commit bcd29daa authored by hjk's avatar hjk
Browse files

Debugger: Consolidate QmlEngine



Create a QmlEnginePrivate. Move the QmlDebugClient there.
Merge QmlAdapter into QmlEngine Abstraction is not used anymore.

Move some helper bits to a qmlengineutils.{h,cpp}

Change-Id: I63117355d786cc12641101b7fd38c7cd208d11eb
Reviewed-by: default avatarChristian Stenger <christian.stenger@theqtcompany.com>
parent 0e0f1bab
......@@ -146,12 +146,11 @@ QtcPlugin {
prefix: "qml/"
files: [
"interactiveinterpreter.cpp", "interactiveinterpreter.h",
"qmladapter.cpp", "qmladapter.h",
"qmlcppengine.cpp", "qmlcppengine.h",
"qmlengine.cpp", "qmlengine.h",
"qmlengineutils.cpp", "qmlengineutils.h",
"qmlinspectoradapter.cpp", "qmlinspectoradapter.h",
"qmlinspectoragent.cpp", "qmlinspectoragent.h",
"qmlv8debuggerclient.cpp", "qmlv8debuggerclient.h",
"qmlv8debuggerclientconstants.h"
]
}
......
HEADERS += \
$$PWD/qmlengine.h \
$$PWD/qmladapter.h \
$$PWD/qmlengineutils.h \
$$PWD/qmlcppengine.h \
$$PWD/qmlv8debuggerclient.h \
$$PWD/interactiveinterpreter.h \
$$PWD/qmlv8debuggerclientconstants.h \
$$PWD/qmlinspectoragent.h \
......@@ -10,9 +9,8 @@ HEADERS += \
SOURCES += \
$$PWD/qmlengine.cpp \
$$PWD/qmladapter.cpp \
$$PWD/qmlengineutils.cpp \
$$PWD/qmlcppengine.cpp \
$$PWD/qmlv8debuggerclient.cpp \
$$PWD/interactiveinterpreter.cpp \
$$PWD/qmlinspectoragent.cpp \
$$PWD/qmlinspectoradapter.cpp
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** 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
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** 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 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.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "qmladapter.h"
#include <debugger/debuggerstringutils.h>
#include "qmlengine.h"
#include "qmlv8debuggerclient.h"
#include <utils/qtcassert.h>
#include <QDebug>
using namespace QmlDebug;
namespace Debugger {
namespace Internal {
/*!
QmlAdapter manages the connection & clients for QML/JS debugging.
*/
QmlAdapter::QmlAdapter(DebuggerEngine *engine, QObject *parent)
: QObject(parent)
, m_engine(engine)
, m_qmlClient(0)
, m_conn(0)
, m_msgClient(0)
{
m_connectionTimer.setInterval(4000);
m_connectionTimer.setSingleShot(true);
connect(&m_connectionTimer, &QTimer::timeout, this, &QmlAdapter::checkConnectionState);
m_conn = new QmlDebugConnection(this);
connect(m_conn, &QmlDebugConnection::stateMessage,
this, &QmlAdapter::showConnectionStateMessage);
connect(m_conn, &QmlDebugConnection::errorMessage,
this, &QmlAdapter::showConnectionErrorMessage);
connect(m_conn, &QmlDebugConnection::error,
this, &QmlAdapter::connectionErrorOccurred);
connect(m_conn, &QmlDebugConnection::opened,
&m_connectionTimer, &QTimer::stop);
connect(m_conn, &QmlDebugConnection::opened,
this, &QmlAdapter::connected);
connect(m_conn, &QmlDebugConnection::closed,
this, &QmlAdapter::disconnected);
createDebuggerClients();
m_msgClient = new QDebugMessageClient(m_conn);
connect(m_msgClient, &QDebugMessageClient::newState, this, &QmlAdapter::clientStateChanged);
}
QmlAdapter::~QmlAdapter()
{
}
void QmlAdapter::beginConnectionTcp(const QString &address, quint16 port)
{
if (m_engine.isNull() || !m_conn || m_conn->isOpen())
return;
m_conn->connectToHost(address, port);
//A timeout to check the connection state
m_connectionTimer.start();
}
void QmlAdapter::closeConnection()
{
if (m_connectionTimer.isActive()) {
m_connectionTimer.stop();
} else {
if (m_conn)
m_conn->close();
}
}
void QmlAdapter::connectionErrorOccurred(QDebugSupport::Error error)
{
// this is only an error if we are already connected and something goes wrong.
if (isConnected()) {
emit connectionError(error);
} else {
m_connectionTimer.stop();
emit connectionStartupFailed();
}
}
void QmlAdapter::clientStateChanged(QmlDebugClient::State state)
{
QString serviceName;
float version = 0;
if (QmlDebugClient *client = qobject_cast<QmlDebugClient*>(sender())) {
serviceName = client->name();
version = client->remoteVersion();
}
logServiceStateChange(serviceName, version, state);
}
void QmlAdapter::debugClientStateChanged(QmlDebugClient::State state)
{
if (state != QmlDebugClient::Enabled)
return;
QmlDebugClient *client = qobject_cast<QmlDebugClient*>(sender());
QTC_ASSERT(client, return);
m_qmlClient = qobject_cast<QmlV8DebuggerClient *>(client);
m_qmlClient->startSession();
}
void QmlAdapter::checkConnectionState()
{
if (!isConnected()) {
closeConnection();
emit connectionStartupFailed();
}
}
bool QmlAdapter::isConnected() const
{
return m_conn && m_qmlClient && m_conn->isOpen();
}
void QmlAdapter::createDebuggerClients()
{
QmlV8DebuggerClient *debugClient2 = new QmlV8DebuggerClient(m_conn);
connect(debugClient2, &QmlV8DebuggerClient::newState,
this, &QmlAdapter::clientStateChanged);
connect(debugClient2, &QmlV8DebuggerClient::newState,
this, &QmlAdapter::debugClientStateChanged);
m_debugClients.insert(debugClient2->name(),debugClient2);
debugClient2->setEngine((QmlEngine*)(m_engine.data()));
}
QmlDebugConnection *QmlAdapter::connection() const
{
return m_conn;
}
DebuggerEngine *QmlAdapter::debuggerEngine() const
{
return m_engine.data();
}
void QmlAdapter::showConnectionStateMessage(const QString &message)
{
if (!m_engine.isNull())
m_engine.data()->showMessage(_("QML Debugger: ") + message, LogStatus);
}
void QmlAdapter::showConnectionErrorMessage(const QString &message)
{
if (!m_engine.isNull())
m_engine.data()->showMessage(_("QML Debugger: ") + message, LogError);
}
QmlV8DebuggerClient *QmlAdapter::activeDebuggerClient() const
{
return m_qmlClient;
}
QHash<QString, QmlV8DebuggerClient*> QmlAdapter::debuggerClients() const
{
return m_debugClients;
}
QDebugMessageClient *QmlAdapter::messageClient() const
{
return m_msgClient;
}
void QmlAdapter::logServiceStateChange(const QString &service, float version,
QmlDebugClient::State newState)
{
switch (newState) {
case QmlDebugClient::Unavailable: {
showConnectionStateMessage(_("Status of \"%1\" Version: %2 changed to 'unavailable'.").
arg(service).arg(QString::number(version)));
break;
}
case QmlDebugClient::Enabled: {
showConnectionStateMessage(_("Status of \"%1\" Version: %2 changed to 'enabled'.").
arg(service).arg(QString::number(version)));
break;
}
case QmlDebugClient::NotConnected: {
showConnectionStateMessage(_("Status of \"%1\" Version: %2 changed to 'not connected'.").
arg(service).arg(QString::number(version)));
break;
}
}
}
void QmlAdapter::logServiceActivity(const QString &service, const QString &logMessage)
{
if (!m_engine.isNull())
m_engine.data()->showMessage(service + QLatin1Char(' ') + logMessage, LogDebug);
}
} // namespace Internal
} // namespace Debugger
This diff is collapsed.
......@@ -31,28 +31,21 @@
#ifndef QMLENGINE_H
#define QMLENGINE_H
#include "interactiveinterpreter.h"
#include "qmladapter.h"
#include "qmlinspectoradapter.h"
#include <debugger/debuggerengine.h>
#include <projectexplorer/applicationlauncher.h>
#include <qmldebug/qdebugmessageclient.h>
#include <qmldebug/qmldebugclient.h>
#include <qmldebug/qmloutputparser.h>
#include <qmljs/iscriptevaluator.h>
#include <qmljs/qmljsdocument.h>
QT_FORWARD_DECLARE_CLASS(QTextDocument)
namespace Core { class IDocument; }
namespace TextEditor { class BaseTextEditor; }
namespace Debugger {
namespace Internal {
class WatchData;
class WatchItem;
class QmlEnginePrivate;
class QmlAdapter;
class WatchTreeView;
class QmlEngine : public DebuggerEngine, QmlJS::IScriptEvaluator
{
......@@ -63,31 +56,11 @@ public:
DebuggerEngine *masterEngine = 0);
~QmlEngine();
void notifyEngineRemoteServerRunning(const QByteArray &, int pid);
void notifyEngineRemoteSetupFinished(const RemoteSetupResult &result);
bool canDisplayTooltip() const;
void showMessage(const QString &msg, int channel = LogDebug,
int timeout = -1) const;
void gotoLocation(const Internal::Location &location);
void filterApplicationMessage(const QString &msg, int channel);
void inferiorSpontaneousStop();
enum LogDirection {
LogSend,
LogReceive
};
void logMessage(const QString &service, LogDirection direction,
const QString &str);
void filterApplicationMessage(const QString &msg, int channel) const;
void setSourceFiles(const QStringList &fileNames);
void updateScriptSource(const QString &fileName, int lineOffset,
int columnOffset, const QString &source);
void insertBreakpoint(Breakpoint bp);
void logServiceStateChange(const QString &service, float version,
QmlDebug::QmlDebugClient::State newState);
void logServiceActivity(const QString &service, const QString &logMessage);
private slots:
void disconnected();
......@@ -96,23 +69,28 @@ private slots:
void errorMessageBoxFinished(int result);
void updateCurrentContext();
void appendDebugOutput(QtMsgType type, const QString &message,
const QmlDebug::QDebugContextInfo &info);
void tryToConnect(quint16 port = 0);
void beginConnection(quint16 port = 0);
void connectionEstablished();
void connectionStartupFailed();
void appStartupFailed(const QString &errorMessage);
void connectionError(QDebugSupport::Error error);
void serviceConnectionError(const QString &service);
void appendMessage(const QString &msg, Utils::OutputFormat);
void synchronizeWatchers();
private:
// DebuggerEngine implementation.
void notifyEngineRemoteServerRunning(const QByteArray &, int pid);
void notifyEngineRemoteSetupFinished(const RemoteSetupResult &result);
void showMessage(const QString &msg, int channel = LogDebug,
int timeout = -1) const;
void gotoLocation(const Internal::Location &location);
void insertBreakpoint(Breakpoint bp);
bool isSynchronous() const { return false; }
bool canDisplayTooltip() const { return false; }
void executeStep();
void executeStepOut();
void executeNext();
......@@ -153,7 +131,6 @@ private:
void reloadSourceFiles();
void reloadFullStack() {}
bool supportsThreads() const { return false; }
void updateWatchData(const QByteArray &iname);
void selectWatchData(const QByteArray &iname);
void executeDebuggerCommand(const QString &command, DebuggerLanguages languages);
......@@ -162,36 +139,21 @@ private:
bool hasCapability(unsigned) const;
void quitDebugger();
private:
void closeConnection();
void startApplicationLauncher();
void stopApplicationLauncher();
bool isShadowBuildProject() const;
QString fromShadowBuildFilename(const QString &filename) const;
QString mangleFilenamePaths(const QString &filename,
const QString &oldBasePath, const QString &newBasePath) const;
QString qmlImportPath() const;
void updateDocument(Core::IDocument *document, const QTextDocument *textDocument);
bool canEvaluateScript(const QString &script);
bool adjustBreakpointLineAndColumn(const QString &filePath, quint32 *line,
quint32 *column, bool *valid);
QmlAdapter m_adapter;
QmlInspectorAdapter m_inspectorAdapter;
ProjectExplorer::ApplicationLauncher m_applicationLauncher;
QTimer m_noDebugOutputTimer;
QmlDebug::QmlOutputParser m_outputParser;
QHash<QString, QTextDocument*> m_sourceDocuments;
QHash<QString, QWeakPointer<TextEditor::BaseTextEditor> > m_sourceEditors;
InteractiveInterpreter m_interpreter;
QHash<QString,Breakpoint> pendingBreakpoints;
QList<quint32> queryIds;
bool m_retryOnConnectFail;
bool m_automaticConnect;
void connectionErrorOccurred(QDebugSupport::Error socketError);
void clientStateChanged(QmlDebug::QmlDebugClient::State state);
void checkConnectionState();
void showConnectionStateMessage(const QString &message);
void showConnectionErrorMessage(const QString &message);
bool isConnected() const;
private:
friend class QmlCppEngine;
friend class QmlEnginePrivate;
QmlEnginePrivate *d;
};
} // namespace Internal
......
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** 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
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** 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 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.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "qmlengine.h"
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljs/consolemanagerinterface.h>
#include <coreplugin/editormanager/documentmodel.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
using namespace Core;
using namespace QmlDebug;
using namespace QmlJS;
using namespace QmlJS::AST;
using namespace TextEditor;
namespace Debugger {
namespace Internal {
class ASTWalker : public Visitor
{
public:
void operator()(Node *ast, quint32 *l, quint32 *c)
{
done = false;
line = l;
column = c;
Node::accept(ast, this);
}
bool preVisit(Node *ast)
{
return !done && ast->lastSourceLocation().startLine >= *line;
}
//Case 1: Breakpoint is between sourceStart(exclusive) and
// sourceEnd(inclusive) --> End tree walk.
//Case 2: Breakpoint is on sourceStart --> Check for the start
// of the first executable code. Set the line number and
// column number. End tree walk.
//Case 3: Breakpoint is on "unbreakable" code --> Find the next "breakable"
// code and check for Case 2. End tree walk.
//Add more types when suitable.
bool visit(UiScriptBinding *ast)
{
if (!ast->statement)
return true;
quint32 sourceStartLine = ast->firstSourceLocation().startLine;
quint32 statementStartLine;
quint32 statementColumn;
if (ast->statement->kind == Node::Kind_ExpressionStatement) {
statementStartLine = ast->statement->firstSourceLocation().startLine;
statementColumn = ast->statement->firstSourceLocation().startColumn;
} else if (ast->statement->kind == Node::Kind_Block) {
Block *block = static_cast<Block *>(ast->statement);
if (!block || !block->statements)
return true;
statementStartLine = block->statements->firstSourceLocation().startLine;
statementColumn = block->statements->firstSourceLocation().startColumn;
} else {
return true;
}
//Case 1
//Check for possible relocation within the binding statement
//Rewritten to (function <token>() { { }})
//The offset 16 is position of inner lbrace without token length.
const int offset = 16;
//Case 2
if (statementStartLine == *line) {
if (sourceStartLine == *line)
*column = offset + ast->qualifiedId->identifierToken.length;
done = true;
}
//Case 3
if (statementStartLine > *line) {
*line = statementStartLine;
if (sourceStartLine == *line)
*column = offset + ast->qualifiedId->identifierToken.length;
else
*column = statementColumn;
done = true;
}
return true;
}
bool visit(FunctionDeclaration *ast) {
quint32 sourceStartLine = ast->firstSourceLocation().startLine;
quint32 sourceStartColumn = ast->firstSourceLocation().startColumn;
quint32 statementStartLine = ast->body->firstSourceLocation().startLine;
quint32 statementColumn = ast->body->firstSourceLocation().startColumn;
//Case 1
//Check for possible relocation within the function declaration
//Case 2
if (statementStartLine == *line) {
if (sourceStartLine == *line)
*column = statementColumn - sourceStartColumn + 1;
done = true;
}
//Case 3
if (statementStartLine > *line) {
*line = statementStartLine;
if (sourceStartLine == *line)
*column = statementColumn - sourceStartColumn + 1;
else
*column = statementColumn;
done = true;
}
return true;
}
bool visit(EmptyStatement *ast)
{
*line = ast->lastSourceLocation().startLine + 1;
return true;
}
bool visit(VariableStatement *ast) { test(ast); return true; }
bool visit(VariableDeclarationList *ast) { test(ast); return true; }
bool visit(VariableDeclaration *ast) { test(ast); return true; }
bool visit(ExpressionStatement *ast) { test(ast); return true; }
bool visit(IfStatement *ast) { test(ast); return true; }
bool visit(DoWhileStatement *ast) { test(ast); return true; }
bool visit(WhileStatement *ast) { test(ast); return true; }
bool visit(ForStatement *ast) { test(ast); return true; }
bool visit(LocalForStatement *ast) { test(ast); return true; }
bool visit(ForEachStatement *ast) { test(ast); return true; }
bool visit(LocalForEachStatement *ast) { test(ast); return true; }
bool visit(ContinueStatement *ast) { test(ast); return true; }
bool visit(BreakStatement *ast) { test(ast); return true; }
bool visit(ReturnStatement *ast) { test(ast); return true; }
bool visit(WithStatement *ast) { test(ast); return true; }
bool visit(SwitchStatement *ast) { test(ast); return true; }