maemodebugsupport.cpp 13.3 KB
Newer Older
ck's avatar
ck committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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 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, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "maemodebugsupport.h"

#include "maemodeployables.h"
#include "maemodeploystep.h"
#include "maemoglobal.h"
#include "maemosshrunner.h"
41
#include "maemousedportsgatherer.h"
ck's avatar
ck committed
42 43 44 45

#include <coreplugin/ssh/sftpchannel.h>
#include <debugger/debuggerplugin.h>
#include <debugger/debuggerrunner.h>
46 47
#include <debugger/debuggerengine.h>

48
#include <projectexplorer/toolchaintype.h>
ck's avatar
ck committed
49

50
#include <QtCore/QDir>
ck's avatar
ck committed
51 52
#include <QtCore/QFileInfo>

53 54
#define ASSERT_STATE(state) ASSERT_STATE_GENERIC(State, state, m_state)

ck's avatar
ck committed
55 56 57 58 59 60 61 62 63 64 65 66
using namespace Core;
using namespace Debugger;
using namespace Debugger::Internal;
using namespace ProjectExplorer;

namespace Qt4ProjectManager {
namespace Internal {

RunControl *MaemoDebugSupport::createDebugRunControl(MaemoRunConfiguration *runConfig)
{
    DebuggerStartParameters params;
    const MaemoDeviceConfig &devConf = runConfig->deviceConfig();
67

68 69 70
    const MaemoRunConfiguration::DebuggingType debuggingType
        = runConfig->debuggingType();
    if (debuggingType != MaemoRunConfiguration::DebugCppOnly) {
71
        params.qmlServerAddress = runConfig->deviceConfig().server.host;
72
        params.qmlServerPort = -1;
73
    }
74
    if (debuggingType != MaemoRunConfiguration::DebugQmlOnly) {
75 76
        params.processArgs = runConfig->arguments();
        params.sysRoot = runConfig->sysRoot();
77
        params.toolChainType = ProjectExplorer::ToolChain_GCC_MAEMO;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        params.dumperLibrary = runConfig->dumperLib();
        params.remoteDumperLib = uploadDir(devConf).toUtf8() + '/'
            + QFileInfo(runConfig->dumperLib()).fileName().toUtf8();
        if (runConfig->useRemoteGdb()) {
            params.startMode = StartRemoteGdb;
            params.executable = runConfig->remoteExecutableFilePath();
            params.debuggerCommand
                = MaemoGlobal::remoteCommandPrefix(runConfig->remoteExecutableFilePath())
                    + environment(runConfig) + QLatin1String(" /usr/bin/gdb");
            params.connParams = devConf.server;
            params.localMountDir = runConfig->localDirToMountForRemoteGdb();
            params.remoteMountPoint = MaemoGlobal::remoteProjectSourcesMountPoint();
            const QString execDirAbs
                = QDir::fromNativeSeparators(QFileInfo(runConfig->localExecutableFilePath()).path());
            const QString execDirRel
                = QDir(params.localMountDir).relativeFilePath(execDirAbs);
            params.remoteSourcesDir = QString(params.remoteMountPoint
                + QLatin1Char('/') + execDirRel).toUtf8();
        } else {
            params.startMode = AttachToRemote;
            params.executable = runConfig->localExecutableFilePath();
            params.debuggerCommand = runConfig->gdbCmd();
100
            params.remoteChannel = devConf.server.host + QLatin1String(":-1");
101 102
            params.useServerStartScript = true;
            params.remoteArchitecture = QLatin1String("arm");
103
            params.gnuTarget = QLatin1String("arm-none-linux-gnueabi");
104
        }
105 106 107
    } else {
        params.startMode = AttachToRemote;
    }
108

ck's avatar
ck committed
109 110 111 112 113 114 115 116 117 118
    DebuggerRunControl * const debuggerRunControl
        = DebuggerPlugin::createDebugger(params, runConfig);
    new MaemoDebugSupport(runConfig, debuggerRunControl);
    return debuggerRunControl;
}

MaemoDebugSupport::MaemoDebugSupport(MaemoRunConfiguration *runConfig,
    DebuggerRunControl *runControl)
    : QObject(runControl), m_runControl(runControl), m_runConfig(runConfig),
      m_deviceConfig(m_runConfig->deviceConfig()),
119
      m_runner(new MaemoSshRunner(this, m_runConfig, true)),
120 121
      m_debuggingType(m_runConfig->debuggingType()),
      m_state(Inactive), m_gdbServerPort(-1), m_qmlPort(-1)
ck's avatar
ck committed
122
{
123
    connect(m_runControl, SIGNAL(engineRequestSetup()), this,
124
        SLOT(handleAdapterSetupRequested()));
ck's avatar
ck committed
125 126 127 128
    connect(m_runControl, SIGNAL(finished()), this,
        SLOT(handleDebuggingFinished()));
}

129 130
MaemoDebugSupport::~MaemoDebugSupport()
{
131
    setState(Inactive);
132
}
ck's avatar
ck committed
133 134 135

void MaemoDebugSupport::handleAdapterSetupRequested()
{
136 137
    ASSERT_STATE(Inactive);

ck's avatar
ck committed
138 139 140 141
    if (!m_deviceConfig.isValid()) {
        handleAdapterSetupFailed(tr("No device configuration set for run configuration."));
        return;
    }
142
    setState(StartingRunner);
ck's avatar
ck committed
143 144 145 146 147 148
    m_runControl->showMessage(tr("Preparing remote side ..."), AppStuff);
    disconnect(m_runner, 0, this, 0);
    connect(m_runner, SIGNAL(error(QString)), this,
        SLOT(handleSshError(QString)));
    connect(m_runner, SIGNAL(readyForExecution()), this,
        SLOT(startExecution()));
149 150
    connect(m_runner, SIGNAL(reportProgress(QString)), this,
        SLOT(handleProgressReport(QString)));
ck's avatar
ck committed
151 152 153 154 155
    m_runner->start();
}

void MaemoDebugSupport::handleSshError(const QString &error)
{
156 157 158 159
    if (m_state == Debugging) {
        m_runControl->showMessage(tr("SSH connection error: %1").arg(error),
            AppError);
    } else if (m_state != Inactive) {
ck's avatar
ck committed
160
        handleAdapterSetupFailed(error);
161
    }
ck's avatar
ck committed
162 163 164 165
}

void MaemoDebugSupport::startExecution()
{
166
    if (m_state == Inactive)
ck's avatar
ck committed
167 168
        return;

169 170
    ASSERT_STATE(StartingRunner);

171 172 173 174 175 176 177 178 179
    if (!useGdb() && m_debuggingType != MaemoRunConfiguration::DebugQmlOnly) {
        if (!setPort(m_gdbServerPort))
            return;
    }
    if (m_debuggingType != MaemoRunConfiguration::DebugCppOnly) {
        if (!setPort(m_qmlPort))
            return;
    }

ck's avatar
ck committed
180
    const QString &dumperLib = m_runConfig->dumperLib();
181 182
    if (m_debuggingType != MaemoRunConfiguration::DebugQmlOnly
            && !dumperLib.isEmpty()
ck's avatar
ck committed
183 184
        && m_runConfig->deployStep()->currentlyNeedsDeployment(m_deviceConfig.server.host,
               MaemoDeployable(dumperLib, uploadDir(m_deviceConfig)))) {
185
        setState(InitializingUploader);
ck's avatar
ck committed
186 187 188 189 190 191 192 193 194
        m_uploader = m_runner->connection()->createSftpChannel();
        connect(m_uploader.data(), SIGNAL(initialized()), this,
                SLOT(handleSftpChannelInitialized()));
        connect(m_uploader.data(), SIGNAL(initializationFailed(QString)), this,
                SLOT(handleSftpChannelInitializationFailed(QString)));
        connect(m_uploader.data(), SIGNAL(finished(Core::SftpJobId, QString)),
                this, SLOT(handleSftpJobFinished(Core::SftpJobId, QString)));
        m_uploader->initialize();
    } else {
195
        setState(DumpersUploaded);
ck's avatar
ck committed
196 197 198 199 200 201
        startDebugging();
    }
}

void MaemoDebugSupport::handleSftpChannelInitialized()
{
202
    if (m_state == Inactive)
ck's avatar
ck committed
203 204
        return;

205 206
    ASSERT_STATE(InitializingUploader);

ck's avatar
ck committed
207 208 209 210 211 212 213 214 215
    const QString dumperLib = m_runConfig->dumperLib();
    const QString fileName = QFileInfo(dumperLib).fileName();
    const QString remoteFilePath = uploadDir(m_deviceConfig) + '/' + fileName;
    m_uploadJob = m_uploader->uploadFile(dumperLib, remoteFilePath,
        SftpOverwriteExisting);
    if (m_uploadJob == SftpInvalidJob) {
        handleAdapterSetupFailed(tr("Upload failed: Could not open file '%1'")
            .arg(dumperLib));
    } else {
216
        setState(UploadingDumpers);
ck's avatar
ck committed
217 218 219 220 221 222 223
        m_runControl->showMessage(tr("Started uploading debugging helpers ('%1').")
            .arg(dumperLib), AppStuff);
    }
}

void MaemoDebugSupport::handleSftpChannelInitializationFailed(const QString &error)
{
224
    if (m_state == Inactive)
ck's avatar
ck committed
225
        return;
226
    ASSERT_STATE(InitializingUploader);
ck's avatar
ck committed
227 228 229 230 231 232
    handleAdapterSetupFailed(error);
}

void MaemoDebugSupport::handleSftpJobFinished(Core::SftpJobId job,
    const QString &error)
{
233
    if (m_state == Inactive)
ck's avatar
ck committed
234 235
        return;

236 237
    ASSERT_STATE(UploadingDumpers);

ck's avatar
ck committed
238 239 240 241 242 243 244 245 246
    if (job != m_uploadJob) {
        qWarning("Warning: Unknown debugging helpers upload job %d finished.", job);
        return;
    }

    if (!error.isEmpty()) {
        handleAdapterSetupFailed(tr("Could not upload debugging helpers: %1.")
            .arg(error));
    } else {
247
        setState(DumpersUploaded);
ck's avatar
ck committed
248 249 250 251 252 253 254 255 256 257
        m_runConfig->deployStep()->setDeployed(m_deviceConfig.server.host,
            MaemoDeployable(m_runConfig->dumperLib(), uploadDir(m_deviceConfig)));
        m_runControl->showMessage(tr("Finished uploading debugging helpers."), AppStuff);
        startDebugging();
    }
    m_uploadJob = SftpInvalidJob;
}

void MaemoDebugSupport::startDebugging()
{
258 259
    ASSERT_STATE(DumpersUploaded);

260 261 262
    if (useGdb()) {
        handleAdapterSetupDone();
    } else {
263
        setState(StartingRemoteProcess);
264
        m_gdbserverOutput.clear();
265 266 267 268 269
        connect(m_runner, SIGNAL(remoteErrorOutput(QByteArray)), this,
            SLOT(handleRemoteErrorOutput(QByteArray)));
        connect(m_runner, SIGNAL(remoteOutput(QByteArray)), this,
            SLOT(handleRemoteOutput(QByteArray)));
        const QString &remoteExe = m_runConfig->remoteExecutableFilePath();
270 271 272
        const QString cmdPrefix = MaemoGlobal::remoteCommandPrefix(remoteExe);
        const QString env = environment(m_runConfig);
        const QString args = m_runConfig->arguments().join(QLatin1String(" "));
273 274 275 276 277 278 279
        const QString remoteCommandLine
            = m_debuggingType == MaemoRunConfiguration::DebugQmlOnly
                ? QString::fromLocal8Bit("%1 %2 %3 %4").arg(cmdPrefix).arg(env)
                      .arg(remoteExe).arg(args)
                : QString::fromLocal8Bit("%1 %2 gdbserver :%3 %4 %5")
                      .arg(cmdPrefix).arg(env).arg(m_gdbServerPort)
                      .arg(remoteExe).arg(args);
280
        m_runner->startExecution(remoteCommandLine.toUtf8());
281
    }
ck's avatar
ck committed
282 283 284 285
}

void MaemoDebugSupport::handleDebuggingFinished()
{
286
    setState(Inactive);
ck's avatar
ck committed
287 288 289 290
}

void MaemoDebugSupport::handleRemoteOutput(const QByteArray &output)
{
291 292 293
    ASSERT_STATE(QList<State>() << Inactive << Debugging);
    if (m_runControl)
        m_runControl->showMessage(QString::fromUtf8(output), AppOutput);
ck's avatar
ck committed
294 295 296 297
}

void MaemoDebugSupport::handleRemoteErrorOutput(const QByteArray &output)
{
298 299 300 301 302
    ASSERT_STATE(QList<State>() << Inactive << StartingRemoteProcess << Debugging);

    if (!m_runControl)
        return;

ck's avatar
ck committed
303
    m_runControl->showMessage(QString::fromUtf8(output), AppOutput);
304 305
    if (m_state == StartingRemoteProcess
            && m_debuggingType != MaemoRunConfiguration::DebugQmlOnly) {
306 307 308 309 310 311
        m_gdbserverOutput += output;
        if (m_gdbserverOutput.contains("Listening on port")) {
            handleAdapterSetupDone();
            m_gdbserverOutput.clear();
        }
    }
ck's avatar
ck committed
312 313
}

314 315
void MaemoDebugSupport::handleProgressReport(const QString &progressOutput)
{
316 317
    if (m_runControl)
        m_runControl->showMessage(progressOutput, AppStuff);
ck's avatar
ck committed
318 319 320 321
}

void MaemoDebugSupport::handleAdapterSetupFailed(const QString &error)
{
322
    setState(Inactive);
323
    m_runControl->handleRemoteSetupFailed(tr("Initial setup failed: %1").arg(error));
ck's avatar
ck committed
324 325 326 327
}

void MaemoDebugSupport::handleAdapterSetupDone()
{
328
    setState(Debugging);
329
    m_runControl->handleRemoteSetupDone(m_gdbServerPort, m_qmlPort);
ck's avatar
ck committed
330 331
}

332 333 334 335 336 337 338 339 340 341 342 343 344 345
void MaemoDebugSupport::setState(State newState)
{
    if (m_state == newState)
        return;
    m_state = newState;
    if (m_state == Inactive) {
        if (m_uploader) {
            disconnect(m_uploader.data(), 0, this, 0);
            m_uploader->closeChannel();
        }
        m_runner->stop();
    }
}

346 347
QString MaemoDebugSupport::environment(const MaemoRunConfiguration *rc)
{
348
    QList<Utils::EnvironmentItem> env = rc->userEnvironmentChanges();
349
    // FIXME: this must use command line argument instead: -qmljsdebugger=port:1234.
350
    if (rc->debuggingType() != MaemoRunConfiguration::DebugCppOnly) {
351
//        env << Utils::EnvironmentItem(QLatin1String(Debugger::Constants::E_QML_DEBUG_SERVER_PORT),
352
//            QString::number(qmlServerPort(rc)));
353 354 355 356
    }
    return MaemoGlobal::remoteEnvironment(env);
}

ck's avatar
ck committed
357 358 359 360 361
QString MaemoDebugSupport::uploadDir(const MaemoDeviceConfig &devConf)
{
    return MaemoGlobal::homeDirOnDevice(devConf.server.uname);
}

362 363
bool MaemoDebugSupport::useGdb() const
{
364
    return m_runControl->engine()->startParameters().startMode == StartRemoteGdb
365 366 367 368 369 370 371 372 373 374 375
        && m_debuggingType != MaemoRunConfiguration::DebugQmlOnly;
}

bool MaemoDebugSupport::setPort(int &port)
{
    port = m_runner->usedPortsGatherer()->getNextFreePort(m_runner->freePorts());
    if (port == -1) {
        handleAdapterSetupFailed(tr("Not enough free ports on device for debugging."));
        return false;
    }
    return true;
376 377
}

ck's avatar
ck committed
378 379
} // namespace Internal
} // namespace Qt4ProjectManager