1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdeclarativedebugserver_p.h"
43 #include "qdeclarativedebugservice_p.h"
44 #include "qdeclarativedebugservice_p_p.h"
45 #include <private/qdeclarativeengine_p.h>
47 #include <QtCore/QDir>
48 #include <QtCore/QPluginLoader>
49 #include <QtCore/QStringList>
50 #include <QtCore/qwaitcondition.h>
52 #include <private/qobject_p.h>
53 #include <private/qcoreapplication_p.h>
58 QDeclarativeDebug Protocol (Version 1):
62 "QDeclarativeDebugServer" 0 version pluginNames
63 version: an int representing the highest protocol version the client knows
64 pluginNames: plugins available on client side
66 "QDeclarativeDebugClient" 0 version pluginNames pluginVersions
67 version: an int representing the highest protocol version the client & server know
68 pluginNames: plugins available on server side. plugins both in the client and server message are enabled.
69 client plugin advertisement
71 "QDeclarativeDebugServer" 1 pluginNames
72 server plugin advertisement
74 "QDeclarativeDebugClient" 1 pluginNames pluginVersions
76 Everything send with a header different to "QDeclarativeDebugServer" is sent to the appropriate plugin.
79 const int protocolVersion = 1;
81 // print detailed information about loading of plugins
82 DEFINE_BOOL_CONFIG_OPTION(qmlDebugVerbose, QML_DEBUGGER_VERBOSE)
84 class QDeclarativeDebugServerThread;
86 class QDeclarativeDebugServerPrivate : public QObjectPrivate
88 Q_DECLARE_PUBLIC(QDeclarativeDebugServer)
90 QDeclarativeDebugServerPrivate();
92 void advertisePlugins();
93 QDeclarativeDebugServerConnection *loadConnectionPlugin(const QString &pluginName);
95 QDeclarativeDebugServerConnection *connection;
96 QHash<QString, QDeclarativeDebugService *> plugins;
97 mutable QReadWriteLock pluginsLock;
98 QStringList clientPlugins;
101 QMutex messageArrivedMutex;
102 QWaitCondition messageArrivedCondition;
103 QStringList waitingForMessageNames;
104 QDeclarativeDebugServerThread *thread;
105 QPluginLoader loader;
109 void _q_sendMessages(const QList<QByteArray> &messages);
112 class QDeclarativeDebugServerThread : public QThread
115 void setPluginName(const QString &pluginName) {
116 m_pluginName = pluginName;
119 void setPort(int port, bool block) {
127 QString m_pluginName;
132 QDeclarativeDebugServerPrivate::QDeclarativeDebugServerPrivate() :
137 // used in _q_sendMessages
138 qRegisterMetaType<QList<QByteArray> >("QList<QByteArray>");
141 void QDeclarativeDebugServerPrivate::advertisePlugins()
143 Q_Q(QDeclarativeDebugServer);
150 QDataStream out(&message, QIODevice::WriteOnly);
151 QStringList pluginNames;
152 QList<float> pluginVersions;
153 foreach (QDeclarativeDebugService *service, plugins.values()) {
154 pluginNames << service->name();
155 pluginVersions << service->version();
157 out << QString(QLatin1String("QDeclarativeDebugClient")) << 1 << pluginNames << pluginVersions;
160 QMetaObject::invokeMethod(q, "_q_sendMessages", Qt::QueuedConnection, Q_ARG(QList<QByteArray>, QList<QByteArray>() << message));
163 QDeclarativeDebugServerConnection *QDeclarativeDebugServerPrivate::loadConnectionPlugin(
164 const QString &pluginName)
166 #ifndef QT_NO_LIBRARY
167 QStringList pluginCandidates;
168 const QStringList paths = QCoreApplication::libraryPaths();
169 foreach (const QString &libPath, paths) {
170 const QDir dir(libPath + QLatin1String("/qmltooling"));
172 QStringList plugins(dir.entryList(QDir::Files));
173 foreach (const QString &pluginPath, plugins) {
174 if (QFileInfo(pluginPath).fileName().contains(pluginName))
175 pluginCandidates << dir.absoluteFilePath(pluginPath);
180 foreach (const QString &pluginPath, pluginCandidates) {
181 if (qmlDebugVerbose())
182 qDebug() << "QDeclarativeDebugServer: Trying to load plugin " << pluginPath << "...";
184 loader.setFileName(pluginPath);
185 if (!loader.load()) {
186 if (qmlDebugVerbose())
187 qDebug() << "QDeclarativeDebugServer: Error while loading: " << loader.errorString();
190 if (QObject *instance = loader.instance())
191 connection = qobject_cast<QDeclarativeDebugServerConnection*>(instance);
194 if (qmlDebugVerbose())
195 qDebug() << "QDeclarativeDebugServer: Plugin successfully loaded.";
200 if (qmlDebugVerbose())
201 qDebug() << "QDeclarativeDebugServer: Plugin does not implement interface QDeclarativeDebugServerConnection.";
209 void QDeclarativeDebugServerThread::run()
211 QDeclarativeDebugServer *server = QDeclarativeDebugServer::instance();
212 QDeclarativeDebugServerConnection *connection
213 = server->d_func()->loadConnectionPlugin(m_pluginName);
215 connection->setServer(QDeclarativeDebugServer::instance());
216 connection->setPort(m_port, m_block);
218 QCoreApplicationPrivate *appD = static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(qApp));
219 qWarning() << QString::fromAscii("QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
220 "Remote debugger plugin has not been found.").arg(appD->qmljsDebugArgumentsString());
226 bool QDeclarativeDebugServer::hasDebuggingClient() const
228 Q_D(const QDeclarativeDebugServer);
230 && d->connection->isConnected()
234 static QDeclarativeDebugServer *qDeclarativeDebugServer = 0;
237 static void cleanup()
239 delete qDeclarativeDebugServer;
240 qDeclarativeDebugServer = 0;
243 QDeclarativeDebugServer *QDeclarativeDebugServer::instance()
245 static bool commandLineTested = false;
247 if (!commandLineTested) {
248 commandLineTested = true;
250 QCoreApplicationPrivate *appD = static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(qApp));
251 #ifndef QDECLARATIVE_NO_DEBUG_PROTOCOL
252 // ### remove port definition when protocol is changed
257 // format: qmljsdebugger=port:3768[,block] OR qmljsdebugger=ost[,block]
258 if (!appD->qmljsDebugArgumentsString().isEmpty()) {
259 if (!QDeclarativeEnginePrivate::qml_debugging_enabled) {
260 qWarning() << QString::fromLatin1(
261 "QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
262 "Debugging has not been enabled.").arg(
263 appD->qmljsDebugArgumentsString());
268 if (appD->qmljsDebugArgumentsString().indexOf(QLatin1String("port:")) == 0) {
269 int separatorIndex = appD->qmljsDebugArgumentsString().indexOf(QLatin1Char(','));
270 port = appD->qmljsDebugArgumentsString().mid(5, separatorIndex - 5).toInt(&ok);
271 pluginName = QLatin1String("qmldbg_tcp");
272 } else if (appD->qmljsDebugArgumentsString().contains(QLatin1String("ost"))) {
273 pluginName = QLatin1String("qmldbg_ost");
277 block = appD->qmljsDebugArgumentsString().contains(QLatin1String("block"));
280 qDeclarativeDebugServer = new QDeclarativeDebugServer();
281 QDeclarativeDebugServerThread *thread = new QDeclarativeDebugServerThread;
282 qDeclarativeDebugServer->d_func()->thread = thread;
283 qDeclarativeDebugServer->moveToThread(thread);
284 thread->setPluginName(pluginName);
285 thread->setPort(port, block);
289 QDeclarativeDebugServerPrivate *d = qDeclarativeDebugServer->d_func();
290 d->messageArrivedMutex.lock();
291 d->messageArrivedCondition.wait(&d->messageArrivedMutex);
292 d->messageArrivedMutex.unlock();
296 qWarning() << QString::fromLatin1(
297 "QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
298 "Format is -qmljsdebugger=port:<port>[,block]").arg(
299 appD->qmljsDebugArgumentsString());
303 if (!appD->qmljsDebugArgumentsString().isEmpty()) {
304 qWarning() << QString::fromLatin1(
305 "QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
306 "QtDeclarative is not configured for debugging.").arg(
307 appD->qmljsDebugArgumentsString());
312 return qDeclarativeDebugServer;
315 QDeclarativeDebugServer::QDeclarativeDebugServer()
316 : QObject(*(new QDeclarativeDebugServerPrivate))
318 qAddPostRoutine(cleanup);
321 QDeclarativeDebugServer::~QDeclarativeDebugServer()
323 Q_D(QDeclarativeDebugServer);
325 QReadLocker(&d->pluginsLock);
327 foreach (QDeclarativeDebugService *service, d->plugins.values()) {
328 service->d_func()->server = 0;
329 service->d_func()->status = QDeclarativeDebugService::NotConnected;
330 service->statusChanged(QDeclarativeDebugService::NotConnected);
336 if (!d->thread->wait(1000))
337 d->thread->terminate();
340 delete d->connection;
343 void QDeclarativeDebugServer::receiveMessage(const QByteArray &message)
345 Q_D(QDeclarativeDebugServer);
347 QDataStream in(message);
352 if (name == QLatin1String("QDeclarativeDebugServer")) {
357 in >> version >> d->clientPlugins;
359 // Send the hello answer immediately, since it needs to arrive before
360 // the plugins below start sending messages.
361 QByteArray helloAnswer;
363 QDataStream out(&helloAnswer, QIODevice::WriteOnly);
364 QStringList pluginNames;
365 QList<float> pluginVersions;
366 foreach (QDeclarativeDebugService *service, d->plugins.values()) {
367 pluginNames << service->name();
368 pluginVersions << service->version();
371 out << QString(QLatin1String("QDeclarativeDebugClient")) << 0 << protocolVersion << pluginNames << pluginVersions;
373 d->connection->send(QList<QByteArray>() << helloAnswer);
377 QReadLocker(&d->pluginsLock);
378 QHash<QString, QDeclarativeDebugService*>::ConstIterator iter = d->plugins.constBegin();
379 for (; iter != d->plugins.constEnd(); ++iter) {
380 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::Unavailable;
381 if (d->clientPlugins.contains(iter.key()))
382 newStatus = QDeclarativeDebugService::Enabled;
383 iter.value()->d_func()->status = newStatus;
384 iter.value()->statusChanged(newStatus);
387 qWarning("QDeclarativeDebugServer: Connection established");
388 d->messageArrivedCondition.wakeAll();
390 } else if (op == 1) {
393 QStringList oldClientPlugins = d->clientPlugins;
394 in >> d->clientPlugins;
396 QReadLocker(&d->pluginsLock);
397 QHash<QString, QDeclarativeDebugService*>::ConstIterator iter = d->plugins.constBegin();
398 for (; iter != d->plugins.constEnd(); ++iter) {
399 const QString pluginName = iter.key();
400 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::Unavailable;
401 if (d->clientPlugins.contains(pluginName))
402 newStatus = QDeclarativeDebugService::Enabled;
404 if (oldClientPlugins.contains(pluginName)
405 != d->clientPlugins.contains(pluginName)) {
406 iter.value()->d_func()->status = newStatus;
407 iter.value()->statusChanged(newStatus);
412 qWarning("QDeclarativeDebugServer: Invalid control message %d", op);
413 d->connection->disconnect();
422 QReadLocker(&d->pluginsLock);
423 QHash<QString, QDeclarativeDebugService *>::Iterator iter = d->plugins.find(name);
424 if (iter == d->plugins.end()) {
425 qWarning() << "QDeclarativeDebugServer: Message received for missing plugin" << name;
427 (*iter)->messageReceived(message);
429 if (d->waitingForMessageNames.removeOne(name))
430 d->messageArrivedCondition.wakeAll();
433 qWarning("QDeclarativeDebugServer: Invalid hello message");
439 void QDeclarativeDebugServerPrivate::_q_sendMessages(const QList<QByteArray> &messages)
442 connection->send(messages);
445 QList<QDeclarativeDebugService*> QDeclarativeDebugServer::services() const
447 const Q_D(QDeclarativeDebugServer);
448 QReadLocker(&d->pluginsLock);
449 return d->plugins.values();
452 QStringList QDeclarativeDebugServer::serviceNames() const
454 const Q_D(QDeclarativeDebugServer);
455 QReadLocker(&d->pluginsLock);
456 return d->plugins.keys();
459 bool QDeclarativeDebugServer::addService(QDeclarativeDebugService *service)
461 Q_D(QDeclarativeDebugServer);
463 QWriteLocker(&d->pluginsLock);
464 if (!service || d->plugins.contains(service->name()))
466 d->plugins.insert(service->name(), service);
469 QReadLocker(&d->pluginsLock);
470 d->advertisePlugins();
471 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::Unavailable;
472 if (d->clientPlugins.contains(service->name()))
473 newStatus = QDeclarativeDebugService::Enabled;
474 service->d_func()->status = newStatus;
479 bool QDeclarativeDebugServer::removeService(QDeclarativeDebugService *service)
481 Q_D(QDeclarativeDebugServer);
483 QWriteLocker(&d->pluginsLock);
484 if (!service || !d->plugins.contains(service->name()))
486 d->plugins.remove(service->name());
489 QReadLocker(&d->pluginsLock);
490 d->advertisePlugins();
491 QDeclarativeDebugService::Status newStatus = QDeclarativeDebugService::NotConnected;
492 service->d_func()->server = 0;
493 service->d_func()->status = newStatus;
494 service->statusChanged(newStatus);
500 void QDeclarativeDebugServer::sendMessages(QDeclarativeDebugService *service,
501 const QList<QByteArray> &messages)
503 QList<QByteArray> prefixedMessages;
504 foreach (const QByteArray &message, messages) {
506 QDataStream out(&prefixed, QIODevice::WriteOnly);
507 out << service->name() << message;
508 prefixedMessages << prefixed;
511 QMetaObject::invokeMethod(this, "_q_sendMessages", Qt::QueuedConnection, Q_ARG(QList<QByteArray>, prefixedMessages));
514 bool QDeclarativeDebugServer::waitForMessage(QDeclarativeDebugService *service)
516 Q_D(QDeclarativeDebugServer);
517 QReadLocker(&d->pluginsLock);
520 || !d->plugins.contains(service->name()))
523 d->messageArrivedMutex.lock();
524 d->waitingForMessageNames << service->name();
526 d->messageArrivedCondition.wait(&d->messageArrivedMutex);
527 } while (d->waitingForMessageNames.contains(service->name()));
528 d->messageArrivedMutex.unlock();
534 #include "moc_qdeclarativedebugserver_p.cpp"