qmlprofilerengine.cpp 10 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30 31
#include "qmlprofilerengine.h"

32
#include "localqmlprofilerrunner.h"
33

34
#include <analyzerbase/analyzermanager.h>
35
#include <coreplugin/icore.h>
36
#include <utils/qtcassert.h>
37
#include <coreplugin/helpmanager.h>
38 39 40
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/target.h>
41
#include <projectexplorer/environmentaspect.h>
42
#include <projectexplorer/localapplicationruncontrol.h>
43
#include <projectexplorer/localapplicationrunconfiguration.h>
44
#include <qtsupport/qtsupportconstants.h>
45
#include <qmldebug/qmloutputparser.h>
46

47 48 49
#include <QMainWindow>
#include <QMessageBox>
#include <QTimer>
50
#include <QTcpServer>
51 52 53
#include <QApplication>
#include <QMessageBox>
#include <QPushButton>
54

55
using namespace Analyzer;
56
using namespace Core;
57
using namespace ProjectExplorer;
58

hjk's avatar
hjk committed
59 60
namespace QmlProfiler {
namespace Internal {
61 62 63 64 65

//
// QmlProfilerEnginePrivate
//

66
class QmlProfilerRunControl::QmlProfilerEnginePrivate
hjk's avatar
hjk committed
67 68
{
public:
69
    QmlProfilerEnginePrivate() : m_running(false) {}
hjk's avatar
hjk committed
70

Christiaan Janssen's avatar
Christiaan Janssen committed
71
    QmlProfilerStateManager *m_profilerState;
72
    QTimer m_noDebugOutputTimer;
73
    QmlDebug::QmlOutputParser m_outputParser;
74
    bool m_running;
hjk's avatar
hjk committed
75 76
};

77 78 79 80
//
// QmlProfilerEngine
//

81 82
QmlProfilerRunControl::QmlProfilerRunControl(const AnalyzerStartParameters &sp,
                                     RunConfiguration *runConfiguration)
83 84
    : AnalyzerRunControl(sp, runConfiguration)
    , d(new QmlProfilerEnginePrivate)
85
{
Christiaan Janssen's avatar
Christiaan Janssen committed
86
    d->m_profilerState = 0;
87

Christiaan Janssen's avatar
Christiaan Janssen committed
88
    // Only wait 4 seconds for the 'Waiting for connection' on application output, then just try to connect
89 90 91 92
    // (application output might be redirected / blocked)
    d->m_noDebugOutputTimer.setSingleShot(true);
    d->m_noDebugOutputTimer.setInterval(4000);
    connect(&d->m_noDebugOutputTimer, SIGNAL(timeout()), this, SLOT(processIsRunning()));
93 94

    d->m_outputParser.setNoOutputText(ApplicationLauncher::msgWinCannotRetrieveDebuggingOutput());
95 96
    connect(&d->m_outputParser, SIGNAL(waitingForConnectionOnPort(quint16)),
            this, SLOT(processIsRunning(quint16)));
97 98 99 100
    connect(&d->m_outputParser, SIGNAL(noOutputMessage()),
            this, SLOT(processIsRunning()));
    connect(&d->m_outputParser, SIGNAL(errorMessage(QString)),
            this, SLOT(wrongSetupMessageBox(QString)));
101 102
}

103
QmlProfilerRunControl::~QmlProfilerRunControl()
104
{
Christiaan Janssen's avatar
Christiaan Janssen committed
105
    if (d->m_profilerState && d->m_profilerState->currentState() == QmlProfilerStateManager::AppRunning)
106
        stopEngine();
107 108 109
    delete d;
}

110
bool QmlProfilerRunControl::startEngine()
111
{
Christiaan Janssen's avatar
Christiaan Janssen committed
112 113 114 115
    QTC_ASSERT(d->m_profilerState, return false);

    d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppStarting);

Orgad Shaneh's avatar
Orgad Shaneh committed
116
    if (startParameters().startMode == StartLocal)
Aurindam Jana's avatar
Aurindam Jana committed
117
        d->m_noDebugOutputTimer.start();
Orgad Shaneh's avatar
Orgad Shaneh committed
118
    else
119
        emit processRunning(startParameters().analyzerPort);
120

Christiaan Janssen's avatar
Christiaan Janssen committed
121
    d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppRunning);
122
    engineStarted();
123
    return true;
124 125
}

126
void QmlProfilerRunControl::stopEngine()
127
{
Christiaan Janssen's avatar
Christiaan Janssen committed
128 129 130 131 132 133 134 135 136 137 138
    QTC_ASSERT(d->m_profilerState, return);

    switch (d->m_profilerState->currentState()) {
    case QmlProfilerStateManager::AppRunning : {
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppStopRequested);
        break;
    }
    case QmlProfilerStateManager::AppReadyToStop : {
        cancelProcess();
        break;
    }
139 140
    case QmlProfilerStateManager::AppDying :
        // valid, but no further action is needed
Christiaan Janssen's avatar
Christiaan Janssen committed
141
        break;
Friedemann Kleint's avatar
Friedemann Kleint committed
142 143 144 145 146
    default: {
        const QString message = QString::fromLatin1("Unexpected engine stop from state %1 in %2:%3")
            .arg(d->m_profilerState->currentStateAsString(), QString::fromLatin1(__FILE__), QString::number(__LINE__));
        qWarning("%s", qPrintable(message));
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
147
        break;
148
    }
149 150
}

151
void QmlProfilerRunControl::notifyRemoteFinished(bool success)
152
{
Christiaan Janssen's avatar
Christiaan Janssen committed
153
    QTC_ASSERT(d->m_profilerState, return);
154

Christiaan Janssen's avatar
Christiaan Janssen committed
155 156
    switch (d->m_profilerState->currentState()) {
    case QmlProfilerStateManager::AppRunning : {
157 158 159 160
        if (success)
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
        else
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppKilled);
Christiaan Janssen's avatar
Christiaan Janssen committed
161
        AnalyzerManager::stopTool();
162

163
        runControlFinished();
Christiaan Janssen's avatar
Christiaan Janssen committed
164 165 166
        break;
    }
    case QmlProfilerStateManager::AppStopped :
167
    case QmlProfilerStateManager::AppKilled :
Christiaan Janssen's avatar
Christiaan Janssen committed
168 169
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::Idle);
        break;
Friedemann Kleint's avatar
Friedemann Kleint committed
170 171 172 173 174
    default: {
        const QString message = QString::fromLatin1("Process died unexpectedly from state %1 in %2:%3")
            .arg(d->m_profilerState->currentStateAsString(), QString::fromLatin1(__FILE__), QString::number(__LINE__));
        qWarning("%s", qPrintable(message));
}
Christiaan Janssen's avatar
Christiaan Janssen committed
175 176
        break;
    }
177
}
178

179
void QmlProfilerRunControl::cancelProcess()
hjk's avatar
hjk committed
180
{
Christiaan Janssen's avatar
Christiaan Janssen committed
181
    QTC_ASSERT(d->m_profilerState, return);
182

Christiaan Janssen's avatar
Christiaan Janssen committed
183 184 185 186 187 188
    switch (d->m_profilerState->currentState()) {
    case QmlProfilerStateManager::AppReadyToStop : {
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppStopped);
        break;
    }
    case QmlProfilerStateManager::AppRunning : {
189
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
Christiaan Janssen's avatar
Christiaan Janssen committed
190 191 192
        break;
    }
    default: {
Friedemann Kleint's avatar
Friedemann Kleint committed
193 194 195
        const QString message = QString::fromLatin1("Unexpected process termination requested with state %1 in %2:%3")
            .arg(d->m_profilerState->currentStateAsString(), QString::fromLatin1(__FILE__), QString::number(__LINE__));
        qWarning("%s", qPrintable(message));
Christiaan Janssen's avatar
Christiaan Janssen committed
196 197
        return;
    }
198
    }
199
    runControlFinished();
200 201
}

202
void QmlProfilerRunControl::logApplicationMessage(const QString &msg, Utils::OutputFormat format)
203
{
204
    appendMessage(msg, format);
205
    d->m_outputParser.processOutput(msg);
206 207
}

208
void QmlProfilerRunControl::wrongSetupMessageBox(const QString &errorMessage)
209
{
210
    QMessageBox *infoBox = new QMessageBox(ICore::mainWindow());
211 212 213 214 215 216 217 218 219 220 221 222 223
    infoBox->setIcon(QMessageBox::Critical);
    infoBox->setWindowTitle(tr("Qt Creator"));
    //: %1 is detailed error message
    infoBox->setText(tr("Could not connect to the in-process QML debugger:\n%1")
                     .arg(errorMessage));
    infoBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Help);
    infoBox->setDefaultButton(QMessageBox::Ok);
    infoBox->setModal(true);

    connect(infoBox, SIGNAL(finished(int)),
            this, SLOT(wrongSetupMessageBoxFinished(int)));

    infoBox->show();
224

Christiaan Janssen's avatar
Christiaan Janssen committed
225
    // KILL
226
    d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
227
    AnalyzerManager::stopTool();
228
    runControlFinished();
229
}
hjk's avatar
hjk committed
230

231
void QmlProfilerRunControl::wrongSetupMessageBoxFinished(int button)
232 233
{
    if (button == QMessageBox::Help) {
234
        HelpManager::handleHelpRequest(QLatin1String("qthelp://org.qt-project.qtcreator/doc/creator-debugging-qml.html"
235
                               "#setting-up-qml-debugging"));
236 237 238
    }
}

239
void QmlProfilerRunControl::showNonmodalWarning(const QString &warningMsg)
240
{
241
    QMessageBox *noExecWarning = new QMessageBox(ICore::mainWindow());
242 243
    noExecWarning->setIcon(QMessageBox::Warning);
    noExecWarning->setWindowTitle(tr("QML Profiler"));
244
    noExecWarning->setText(warningMsg);
245 246 247 248 249 250
    noExecWarning->setStandardButtons(QMessageBox::Ok);
    noExecWarning->setDefaultButton(QMessageBox::Ok);
    noExecWarning->setModal(false);
    noExecWarning->show();
}

251
void QmlProfilerRunControl::notifyRemoteSetupDone(quint16 port)
252 253
{
    d->m_noDebugOutputTimer.stop();
Aurindam Jana's avatar
Aurindam Jana committed
254 255
    emit processRunning(port);
}
256

257
void QmlProfilerRunControl::processIsRunning(quint16 port)
Aurindam Jana's avatar
Aurindam Jana committed
258 259
{
    d->m_noDebugOutputTimer.stop();
260

261
    if (port > 0 && startParameters().analyzerPort != 0)
262
        emit processRunning(port);
263 264
}

265
void QmlProfilerRunControl::engineStarted()
266 267 268 269 270
{
    d->m_running = true;
    emit starting(this);
}

271
void QmlProfilerRunControl::runControlFinished()
272 273 274
{
    d->m_running = false;
    emit finished();
275 276
}

Christiaan Janssen's avatar
Christiaan Janssen committed
277 278
////////////////////////////////////////////////////////////////
// Profiler State
279
void QmlProfilerRunControl::registerProfilerStateManager( QmlProfilerStateManager *profilerState )
Christiaan Janssen's avatar
Christiaan Janssen committed
280 281
{
    // disconnect old
282
    if (d->m_profilerState)
Christiaan Janssen's avatar
Christiaan Janssen committed
283 284 285 286 287
        disconnect(d->m_profilerState, SIGNAL(stateChanged()), this, SLOT(profilerStateChanged()));

    d->m_profilerState = profilerState;

    // connect
288
    if (d->m_profilerState)
Christiaan Janssen's avatar
Christiaan Janssen committed
289 290 291
        connect(d->m_profilerState, SIGNAL(stateChanged()), this, SLOT(profilerStateChanged()));
}

292
void QmlProfilerRunControl::profilerStateChanged()
Christiaan Janssen's avatar
Christiaan Janssen committed
293 294 295
{
    switch (d->m_profilerState->currentState()) {
    case QmlProfilerStateManager::AppReadyToStop : {
296 297
        if (d->m_running)
            cancelProcess();
Christiaan Janssen's avatar
Christiaan Janssen committed
298 299 300
        break;
    }
    case QmlProfilerStateManager::Idle : {
301
        d->m_noDebugOutputTimer.stop();
Christiaan Janssen's avatar
Christiaan Janssen committed
302 303 304 305 306 307 308
        break;
    }
    default:
        break;
    }
}

hjk's avatar
hjk committed
309 310
} // namespace Internal
} // namespace QmlProfiler