qmlprofilerengine.cpp 10.6 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Kai Koehne's avatar
Kai Koehne committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Kai Koehne's avatar
Kai Koehne committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Kai Koehne's avatar
Kai Koehne committed
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.
Kai Koehne's avatar
Kai Koehne committed
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
Kai Koehne's avatar
Kai Koehne committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Kai Koehne's avatar
Kai Koehne committed
29

Christiaan Janssen's avatar
Christiaan Janssen committed
30
31
#include "qmlprofilerengine.h"

32
#include "localqmlprofilerrunner.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
33

Christiaan Janssen's avatar
Christiaan Janssen committed
34
#include <analyzerbase/analyzermanager.h>
35
#include <coreplugin/icore.h>
36
#include <debugger/debuggerrunconfigurationaspect.h>
37
#include <utils/qtcassert.h>
38
#include <coreplugin/helpmanager.h>
39
40
41
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/target.h>
42
#include <qmlprojectmanager/qmlprojectrunconfiguration.h>
43
#include <qmlprojectmanager/qmlprojectplugin.h>
44
#include <projectexplorer/environmentaspect.h>
45
#include <projectexplorer/localapplicationruncontrol.h>
46
#include <projectexplorer/localapplicationrunconfiguration.h>
47
#include <qmldebug/qmloutputparser.h>
48

49
50
51
#include <QMainWindow>
#include <QMessageBox>
#include <QTimer>
52
#include <QTcpServer>
Christiaan Janssen's avatar
Christiaan Janssen committed
53

54
using namespace Analyzer;
55
using namespace ProjectExplorer;
56

hjk's avatar
hjk committed
57
58
namespace QmlProfiler {
namespace Internal {
59
60
61
62
63

//
// QmlProfilerEnginePrivate
//

hjk's avatar
hjk committed
64
65
66
class QmlProfilerEngine::QmlProfilerEnginePrivate
{
public:
67
    QmlProfilerEnginePrivate(const AnalyzerStartParameters &sp) : sp(sp), m_running(false) {}
hjk's avatar
hjk committed
68

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

76
77
78
79
//
// QmlProfilerEngine
//

80
QmlProfilerEngine::QmlProfilerEngine(IAnalyzerTool *tool,
81
82
83
                                     const Analyzer::AnalyzerStartParameters &sp,
                                     ProjectExplorer::RunConfiguration *runConfiguration)
    : IAnalyzerEngine(tool, sp, runConfiguration)
84
    , d(new QmlProfilerEnginePrivate(sp))
Christiaan Janssen's avatar
Christiaan Janssen committed
85
{
Christiaan Janssen's avatar
Christiaan Janssen committed
86
    d->m_profilerState = 0;
87
88
89
90
91
92

    // Only wait 4 seconds for the 'Waiting for connection' on application ouput, then just try to connect
    // (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)));
Christiaan Janssen's avatar
Christiaan Janssen committed
101
102
103
104
}

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

110
bool QmlProfilerEngine::start()
Christiaan Janssen's avatar
Christiaan Janssen committed
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);

116
117
118
119
    if (QmlProjectManager::QmlProjectRunConfiguration *rc =
            qobject_cast<QmlProjectManager::QmlProjectRunConfiguration *>(runConfiguration())) {
        if (rc->observerPath().isEmpty()) {
            QmlProjectManager::QmlProjectPlugin::showQmlObserverToolWarning();
Christiaan Janssen's avatar
Christiaan Janssen committed
120
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::Idle);
121
            AnalyzerManager::stopTool();
122
            return false;
123
124
125
        }
    }

126
    if (d->sp.startMode == StartQmlRemote || d->sp.startMode == StartLocal) {
Aurindam Jana's avatar
Aurindam Jana committed
127
        d->m_noDebugOutputTimer.start();
128
    } else {
129
        emit processRunning(startParameters().analyzerPort);
130
    }
131

Christiaan Janssen's avatar
Christiaan Janssen committed
132
    d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppRunning);
133
    engineStarted();
134
    return true;
Christiaan Janssen's avatar
Christiaan Janssen committed
135
136
137
138
}

void QmlProfilerEngine::stop()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
139
140
141
142
143
144
145
146
147
148
149
    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;
    }
150
151
    case QmlProfilerStateManager::AppDying :
        // valid, but no further action is needed
Christiaan Janssen's avatar
Christiaan Janssen committed
152
        break;
Friedemann Kleint's avatar
Friedemann Kleint committed
153
154
155
156
157
    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
158
        break;
159
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
160
161
}

162
void QmlProfilerEngine::notifyRemoteFinished(bool success)
Christiaan Janssen's avatar
Christiaan Janssen committed
163
{
Christiaan Janssen's avatar
Christiaan Janssen committed
164
    QTC_ASSERT(d->m_profilerState, return);
165

Christiaan Janssen's avatar
Christiaan Janssen committed
166
167
    switch (d->m_profilerState->currentState()) {
    case QmlProfilerStateManager::AppRunning : {
168
169
170
171
        if (success)
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
        else
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppKilled);
Christiaan Janssen's avatar
Christiaan Janssen committed
172
        AnalyzerManager::stopTool();
Christiaan Janssen's avatar
Christiaan Janssen committed
173

174
        engineFinished();
Christiaan Janssen's avatar
Christiaan Janssen committed
175
176
177
        break;
    }
    case QmlProfilerStateManager::AppStopped :
178
    case QmlProfilerStateManager::AppKilled :
Christiaan Janssen's avatar
Christiaan Janssen committed
179
180
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::Idle);
        break;
Friedemann Kleint's avatar
Friedemann Kleint committed
181
182
183
184
185
    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
186
187
        break;
    }
188
}
189

Christiaan Janssen's avatar
Christiaan Janssen committed
190
void QmlProfilerEngine::cancelProcess()
hjk's avatar
hjk committed
191
{
Christiaan Janssen's avatar
Christiaan Janssen committed
192
    QTC_ASSERT(d->m_profilerState, return);
193

Christiaan Janssen's avatar
Christiaan Janssen committed
194
195
196
197
198
199
    switch (d->m_profilerState->currentState()) {
    case QmlProfilerStateManager::AppReadyToStop : {
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppStopped);
        break;
    }
    case QmlProfilerStateManager::AppRunning : {
200
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
Christiaan Janssen's avatar
Christiaan Janssen committed
201
202
203
        break;
    }
    default: {
Friedemann Kleint's avatar
Friedemann Kleint committed
204
205
206
        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
207
208
        return;
    }
209
    }
210
    engineFinished();
Christiaan Janssen's avatar
Christiaan Janssen committed
211
212
}

213
void QmlProfilerEngine::logApplicationMessage(const QString &msg, Utils::OutputFormat format)
214
{
215
216
    emit outputReceived(msg, format);
    d->m_outputParser.processOutput(msg);
217
218
}

219
void QmlProfilerEngine::wrongSetupMessageBox(const QString &errorMessage)
220
{
hjk's avatar
hjk committed
221
    QMessageBox *infoBox = new QMessageBox(Core::ICore::mainWindow());
222
223
224
225
226
227
228
229
230
231
232
233
234
    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();
235

Christiaan Janssen's avatar
Christiaan Janssen committed
236
    // KILL
237
    d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
238
    AnalyzerManager::stopTool();
239
    engineFinished();
240
}
hjk's avatar
hjk committed
241

242
243
244
245
void QmlProfilerEngine::wrongSetupMessageBoxFinished(int button)
{
    if (button == QMessageBox::Help) {
        Core::HelpManager *helpManager = Core::HelpManager::instance();
246
        helpManager->handleHelpRequest(QLatin1String("qthelp://org.qt-project.qtcreator/doc/creator-debugging-qml.html"
247
                               "#setting-up-qml-debugging"));
248
249
250
    }
}

251
void QmlProfilerEngine::showNonmodalWarning(const QString &warningMsg)
252
{
hjk's avatar
hjk committed
253
    QMessageBox *noExecWarning = new QMessageBox(Core::ICore::mainWindow());
254
255
    noExecWarning->setIcon(QMessageBox::Warning);
    noExecWarning->setWindowTitle(tr("QML Profiler"));
256
    noExecWarning->setText(warningMsg);
257
258
259
260
261
262
    noExecWarning->setStandardButtons(QMessageBox::Ok);
    noExecWarning->setDefaultButton(QMessageBox::Ok);
    noExecWarning->setModal(false);
    noExecWarning->show();
}

Aurindam Jana's avatar
Aurindam Jana committed
263
void QmlProfilerEngine::notifyRemoteSetupDone(quint16 port)
264
265
{
    d->m_noDebugOutputTimer.stop();
Aurindam Jana's avatar
Aurindam Jana committed
266
267
    emit processRunning(port);
}
268

Aurindam Jana's avatar
Aurindam Jana committed
269
270
271
void QmlProfilerEngine::processIsRunning(quint16 port)
{
    d->m_noDebugOutputTimer.stop();
272

273
    if (port > 0 && mode() != StartQmlRemote)
274
        emit processRunning(port);
275
276
277
278
279
280
281
282
283
284
285
286
}

void QmlProfilerEngine::engineStarted()
{
    d->m_running = true;
    emit starting(this);
}

void QmlProfilerEngine::engineFinished()
{
    d->m_running = false;
    emit finished();
287
288
}

Christiaan Janssen's avatar
Christiaan Janssen committed
289
290
291
292
293
////////////////////////////////////////////////////////////////
// Profiler State
void QmlProfilerEngine::registerProfilerStateManager( QmlProfilerStateManager *profilerState )
{
    // disconnect old
294
    if (d->m_profilerState)
Christiaan Janssen's avatar
Christiaan Janssen committed
295
296
297
298
299
        disconnect(d->m_profilerState, SIGNAL(stateChanged()), this, SLOT(profilerStateChanged()));

    d->m_profilerState = profilerState;

    // connect
300
    if (d->m_profilerState)
Christiaan Janssen's avatar
Christiaan Janssen committed
301
302
303
304
305
306
307
        connect(d->m_profilerState, SIGNAL(stateChanged()), this, SLOT(profilerStateChanged()));
}

void QmlProfilerEngine::profilerStateChanged()
{
    switch (d->m_profilerState->currentState()) {
    case QmlProfilerStateManager::AppReadyToStop : {
308
309
        if (d->m_running)
            cancelProcess();
Christiaan Janssen's avatar
Christiaan Janssen committed
310
311
312
        break;
    }
    case QmlProfilerStateManager::Idle : {
313
        d->m_noDebugOutputTimer.stop();
Christiaan Janssen's avatar
Christiaan Janssen committed
314
315
316
317
318
319
320
        break;
    }
    default:
        break;
    }
}

hjk's avatar
hjk committed
321
322
} // namespace Internal
} // namespace QmlProfiler