qmlprofilerruncontrol.cpp 9.46 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Kai Koehne's avatar
Kai Koehne committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
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
** 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.
Kai Koehne's avatar
Kai Koehne committed
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
Kai Koehne's avatar
Kai Koehne committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Kai Koehne's avatar
Kai Koehne committed
30

31
#include "qmlprofilerruncontrol.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
32

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

Christiaan Janssen's avatar
Christiaan Janssen committed
35
#include <analyzerbase/analyzermanager.h>
36
#include <coreplugin/icore.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 <projectexplorer/environmentaspect.h>
43
#include <projectexplorer/localapplicationruncontrol.h>
44
#include <projectexplorer/localapplicationrunconfiguration.h>
45
#include <qtsupport/qtsupportconstants.h>
46
#include <qmldebug/qmloutputparser.h>
47

48 49 50
#include <QMainWindow>
#include <QMessageBox>
#include <QTimer>
51
#include <QTcpServer>
52 53 54
#include <QApplication>
#include <QMessageBox>
#include <QPushButton>
Christiaan Janssen's avatar
Christiaan Janssen committed
55

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

hjk's avatar
hjk committed
60
namespace QmlProfiler {
61 62

//
63
// QmlProfilerRunControlPrivate
64 65
//

66
class QmlProfilerRunControl::QmlProfilerRunControlPrivate
hjk's avatar
hjk committed
67 68
{
public:
69
    QmlProfilerStateManager *m_profilerState = 0;
70
    QTimer m_noDebugOutputTimer;
71
    QmlDebug::QmlOutputParser m_outputParser;
72
    bool m_running = false;
hjk's avatar
hjk committed
73 74
};

75
//
76
// QmlProfilerRunControl
77 78
//

79
QmlProfilerRunControl::QmlProfilerRunControl(const AnalyzerStartParameters &sp,
80 81 82
                                             RunConfiguration *runConfiguration)
    : AnalyzerRunControl(sp, runConfiguration, ProjectExplorer::Constants::QML_PROFILER_RUN_MODE)
    , d(new QmlProfilerRunControlPrivate)
Christiaan Janssen's avatar
Christiaan Janssen committed
83
{
Christiaan Janssen's avatar
Christiaan Janssen committed
84
    // Only wait 4 seconds for the 'Waiting for connection' on application output, then just try to connect
85 86 87
    // (application output might be redirected / blocked)
    d->m_noDebugOutputTimer.setSingleShot(true);
    d->m_noDebugOutputTimer.setInterval(4000);
88 89
    connect(&d->m_noDebugOutputTimer, &QTimer::timeout,
            this, [this](){processIsRunning(0);});
90 91

    d->m_outputParser.setNoOutputText(ApplicationLauncher::msgWinCannotRetrieveDebuggingOutput());
92 93 94 95
    connect(&d->m_outputParser, &QmlDebug::QmlOutputParser::waitingForConnectionOnPort,
            this, &QmlProfilerRunControl::processIsRunning);
    connect(&d->m_outputParser, &QmlDebug::QmlOutputParser::noOutputMessage,
            this, [this](){processIsRunning(0);});
96 97
    connect(&d->m_outputParser, &QmlDebug::QmlOutputParser::connectingToSocketMessage,
            this, [this](){processIsRunning(0);});
98 99
    connect(&d->m_outputParser, &QmlDebug::QmlOutputParser::errorMessage,
            this, &QmlProfilerRunControl::wrongSetupMessageBox);
Christiaan Janssen's avatar
Christiaan Janssen committed
100 101
}

102
QmlProfilerRunControl::~QmlProfilerRunControl()
Christiaan Janssen's avatar
Christiaan Janssen committed
103
{
104
    if (d->m_profilerState)
105
        stopEngine();
Christiaan Janssen's avatar
Christiaan Janssen committed
106 107 108
    delete d;
}

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

113
    if (startParameters().analyzerPort != 0)
114
        emit processRunning(startParameters().analyzerPort);
115
    else if (startParameters().analyzerSocket.isEmpty())
116
        d->m_noDebugOutputTimer.start();
117

Christiaan Janssen's avatar
Christiaan Janssen committed
118
    d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppRunning);
119
    emit starting(this);
120
    return true;
Christiaan Janssen's avatar
Christiaan Janssen committed
121 122
}

123
void QmlProfilerRunControl::stopEngine()
Christiaan Janssen's avatar
Christiaan Janssen committed
124
{
Christiaan Janssen's avatar
Christiaan Janssen committed
125 126 127
    QTC_ASSERT(d->m_profilerState, return);

    switch (d->m_profilerState->currentState()) {
128
    case QmlProfilerStateManager::AppRunning:
Christiaan Janssen's avatar
Christiaan Janssen committed
129 130
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppStopRequested);
        break;
131 132 133 134
    case QmlProfilerStateManager::AppStopRequested:
        // Pressed "stop" a second time. Kill the application without collecting data
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::Idle);
        break;
135 136
    case QmlProfilerStateManager::Idle:
    case QmlProfilerStateManager::AppDying:
137
        // valid, but no further action is needed
Christiaan Janssen's avatar
Christiaan Janssen committed
138
        break;
Friedemann Kleint's avatar
Friedemann Kleint committed
139 140 141 142 143
    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
144
        break;
145
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
146 147
}

148
void QmlProfilerRunControl::notifyRemoteFinished()
Christiaan Janssen's avatar
Christiaan Janssen committed
149
{
Christiaan Janssen's avatar
Christiaan Janssen committed
150
    QTC_ASSERT(d->m_profilerState, return);
151

Christiaan Janssen's avatar
Christiaan Janssen committed
152
    switch (d->m_profilerState->currentState()) {
153
    case QmlProfilerStateManager::AppRunning:
154
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
Christiaan Janssen's avatar
Christiaan Janssen committed
155
        AnalyzerManager::stopTool();
156
        emit finished();
Christiaan Janssen's avatar
Christiaan Janssen committed
157
        break;
158
    case QmlProfilerStateManager::Idle:
Christiaan Janssen's avatar
Christiaan Janssen committed
159
        break;
160
    default:
Friedemann Kleint's avatar
Friedemann Kleint committed
161 162 163
        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
164 165
        break;
    }
166
}
167

168
void QmlProfilerRunControl::cancelProcess()
hjk's avatar
hjk committed
169
{
Christiaan Janssen's avatar
Christiaan Janssen committed
170
    QTC_ASSERT(d->m_profilerState, return);
171

Christiaan Janssen's avatar
Christiaan Janssen committed
172
    switch (d->m_profilerState->currentState()) {
173
    case QmlProfilerStateManager::Idle:
Christiaan Janssen's avatar
Christiaan Janssen committed
174
        break;
175
    case QmlProfilerStateManager::AppRunning:
176
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
Christiaan Janssen's avatar
Christiaan Janssen committed
177 178
        break;
    default: {
Friedemann Kleint's avatar
Friedemann Kleint committed
179 180 181
        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
182 183
        return;
    }
184
    }
185
    emit finished();
Christiaan Janssen's avatar
Christiaan Janssen committed
186 187
}

188
void QmlProfilerRunControl::logApplicationMessage(const QString &msg, Utils::OutputFormat format)
189
{
190
    appendMessage(msg, format);
191
    d->m_outputParser.processOutput(msg);
192 193
}

194
void QmlProfilerRunControl::wrongSetupMessageBox(const QString &errorMessage)
195
{
196
    QMessageBox *infoBox = new QMessageBox(ICore::mainWindow());
197 198 199 200 201 202 203 204 205
    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);

206 207
    connect(infoBox, &QDialog::finished,
            this, &QmlProfilerRunControl::wrongSetupMessageBoxFinished);
208 209

    infoBox->show();
210

Christiaan Janssen's avatar
Christiaan Janssen committed
211
    // KILL
212
    d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
213
    d->m_noDebugOutputTimer.stop();
214
    AnalyzerManager::stopTool();
215
    emit finished();
216
}
hjk's avatar
hjk committed
217

218
void QmlProfilerRunControl::wrongSetupMessageBoxFinished(int button)
219 220
{
    if (button == QMessageBox::Help) {
221
        HelpManager::handleHelpRequest(QLatin1String("qthelp://org.qt-project.qtcreator/doc/creator-debugging-qml.html"
222
                               "#setting-up-qml-debugging"));
223 224 225
    }
}

226
void QmlProfilerRunControl::notifyRemoteSetupDone(quint16 port)
227 228
{
    d->m_noDebugOutputTimer.stop();
Aurindam Jana's avatar
Aurindam Jana committed
229 230
    emit processRunning(port);
}
231

232
void QmlProfilerRunControl::processIsRunning(quint16 port)
Aurindam Jana's avatar
Aurindam Jana committed
233 234
{
    d->m_noDebugOutputTimer.stop();
235

236 237 238
    if (port == 0)
        port = startParameters().analyzerPort;
    if (port != 0)
239
        emit processRunning(port);
240 241
}

242
void QmlProfilerRunControl::registerProfilerStateManager( QmlProfilerStateManager *profilerState )
Christiaan Janssen's avatar
Christiaan Janssen committed
243 244
{
    // disconnect old
245
    if (d->m_profilerState)
246 247
        disconnect(d->m_profilerState, &QmlProfilerStateManager::stateChanged,
                   this, &QmlProfilerRunControl::profilerStateChanged);
Christiaan Janssen's avatar
Christiaan Janssen committed
248 249 250 251

    d->m_profilerState = profilerState;

    // connect
252
    if (d->m_profilerState)
253 254
        connect(d->m_profilerState, &QmlProfilerStateManager::stateChanged,
                this, &QmlProfilerRunControl::profilerStateChanged);
Christiaan Janssen's avatar
Christiaan Janssen committed
255 256
}

257
void QmlProfilerRunControl::profilerStateChanged()
Christiaan Janssen's avatar
Christiaan Janssen committed
258 259
{
    switch (d->m_profilerState->currentState()) {
260 261
    case QmlProfilerStateManager::Idle:
        emit finished();
262
        d->m_noDebugOutputTimer.stop();
Christiaan Janssen's avatar
Christiaan Janssen committed
263 264 265 266 267 268
        break;
    default:
        break;
    }
}

269
RunControl::StopResult QmlProfilerRunControl::stop()
270 271 272 273 274 275 276
{
    StopResult result = Analyzer::AnalyzerRunControl::stop();
    if (d->m_profilerState->currentState() != QmlProfilerStateManager::Idle)
        m_isRunning = true;
    return result;
}

hjk's avatar
hjk committed
277
} // namespace QmlProfiler