remotelinuxdebugsupport.cpp 11.3 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
**
** GNU Lesser General Public License Usage
**
** 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.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
28
** Nokia at qt-info@nokia.com.
29 30 31 32
**
**************************************************************************/
#include "remotelinuxdebugsupport.h"

33
#include "linuxdeviceconfiguration.h"
34
#include "remotelinuxapplicationrunner.h"
35
#include "remotelinuxrunconfiguration.h"
36
#include "remotelinuxusedportsgatherer.h"
37 38

#include <debugger/debuggerengine.h>
39
#include <debugger/debuggerstartparameters.h>
40
#include <projectexplorer/abi.h>
41
#include <projectexplorer/project.h>
42
#include <projectexplorer/target.h>
43
#include <projectexplorer/toolchain.h>
44
#include <qt4projectmanager/qt4buildconfiguration.h>
45
#include <utils/qtcassert.h>
46

47 48
#include <QPointer>
#include <QSharedPointer>
49

50 51 52 53 54
using namespace Utils;
using namespace Debugger;
using namespace ProjectExplorer;

namespace RemoteLinux {
55 56 57 58 59 60 61 62 63 64
namespace Internal {
namespace {
enum State { Inactive, StartingRunner, StartingRemoteProcess, Debugging };
} // anonymous namespace

class AbstractRemoteLinuxDebugSupportPrivate
{
public:
    AbstractRemoteLinuxDebugSupportPrivate(RemoteLinuxRunConfiguration *runConfig,
            DebuggerEngine *engine)
65
        : engine(engine), deviceConfig(runConfig->deviceConfig()),
66
          qmlDebugging(runConfig->debuggerAspect()->useQmlDebugger()),
67
          cppDebugging(runConfig->debuggerAspect()->useCppDebugger()),
68
          state(Inactive),
69 70 71 72 73 74
          gdbServerPort(-1), qmlPort(-1)
    {
    }

    const QPointer<Debugger::DebuggerEngine> engine;
    const LinuxDeviceConfiguration::ConstPtr deviceConfig;
75 76
    bool qmlDebugging;
    bool cppDebugging;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    QByteArray gdbserverOutput;
    State state;
    int gdbServerPort;
    int qmlPort;
};

class RemoteLinuxDebugSupportPrivate
{
public:
    RemoteLinuxDebugSupportPrivate(RemoteLinuxRunConfiguration *runConfig) : runner(runConfig) {}

    GenericRemoteLinuxApplicationRunner runner;
};

} // namespace Internal

93 94 95 96 97 98
using namespace Internal;

DebuggerStartParameters AbstractRemoteLinuxDebugSupport::startParameters(const RemoteLinuxRunConfiguration *runConfig)
{
    DebuggerStartParameters params;
    const LinuxDeviceConfiguration::ConstPtr &devConf = runConfig->deviceConfig();
99
    if (runConfig->debuggerAspect()->useQmlDebugger()) {
100
        params.languages |= QmlLanguage;
101
        params.qmlServerAddress = runConfig->deviceConfig()->sshParameters().host;
102
        params.qmlServerPort = 0; // port is selected later on
103
    }
104
    if (runConfig->debuggerAspect()->useCppDebugger()) {
105
        params.languages |= CppLanguage;
106 107 108 109
        params.processArgs = runConfig->arguments();
        if (runConfig->activeQt4BuildConfiguration()->qtVersion())
            params.sysroot = runConfig->activeQt4BuildConfiguration()->qtVersion()->systemRoot();
        params.toolChainAbi = runConfig->abi();
110
        params.startMode = AttachToRemoteServer;
111 112
        params.executable = runConfig->localExecutableFilePath();
        params.debuggerCommand = runConfig->gdbCmd();
113 114 115 116
        QString host = devConf->sshParameters().host;
        params.remoteChannel = host.contains(QLatin1Char(':'))
                ? QString::fromLatin1("[%1]:-1").arg(host)
                : host + QLatin1String(":-1");
117 118 119 120 121 122 123 124

        // TODO: This functionality should be inside the debugger.
        const ProjectExplorer::Abi &abi = runConfig->target()
            ->activeBuildConfiguration()->toolChain()->targetAbi();
        params.remoteArchitecture = abi.toString();
        params.gnuTarget = QLatin1String(abi.architecture() == ProjectExplorer::Abi::ArmArchitecture
            ? "arm-none-linux-gnueabi": "i386-unknown-linux-gnu");
    } else {
125
        params.startMode = AttachToRemoteServer;
126
    }
127
    params.requestRemoteSetup = true;
128 129 130 131 132 133 134 135 136 137 138 139 140
    params.displayName = runConfig->displayName();

    if (const ProjectExplorer::Project *project = runConfig->target()->project()) {
        params.projectSourceDirectory = project->projectDirectory();
        if (const ProjectExplorer::BuildConfiguration *buildConfig = runConfig->target()->activeBuildConfiguration()) {
            params.projectBuildDirectory = buildConfig->buildDirectory();
        }
        params.projectSourceFiles = project->files(Project::ExcludeGeneratedFiles);
    }

    return params;
}

141 142
AbstractRemoteLinuxDebugSupport::AbstractRemoteLinuxDebugSupport(RemoteLinuxRunConfiguration *runConfig,
        DebuggerEngine *engine)
143
    : QObject(engine), d(new AbstractRemoteLinuxDebugSupportPrivate(runConfig, engine))
144
{
145
    connect(d->engine, SIGNAL(requestRemoteSetup()), this, SLOT(handleAdapterSetupRequested()));
146 147 148 149
}

AbstractRemoteLinuxDebugSupport::~AbstractRemoteLinuxDebugSupport()
{
150
    setFinished();
151
    delete d;
152 153 154 155
}

void AbstractRemoteLinuxDebugSupport::showMessage(const QString &msg, int channel)
{
156 157
    if (d->engine)
        d->engine->showMessage(msg, channel);
158 159 160 161
}

void AbstractRemoteLinuxDebugSupport::handleAdapterSetupRequested()
{
162
    QTC_ASSERT(d->state == Inactive, return);
163

164
    d->state = StartingRunner;
165 166 167 168 169 170 171 172 173 174
    showMessage(tr("Preparing remote side ...\n"), AppStuff);
    disconnect(runner(), 0, this, 0);
    connect(runner(), SIGNAL(error(QString)), this, SLOT(handleSshError(QString)));
    connect(runner(), SIGNAL(readyForExecution()), this, SLOT(startExecution()));
    connect(runner(), SIGNAL(reportProgress(QString)), this, SLOT(handleProgressReport(QString)));
    runner()->start();
}

void AbstractRemoteLinuxDebugSupport::handleSshError(const QString &error)
{
175
    if (d->state == Debugging) {
176
        showMessage(error, AppError);
177 178 179
        if (d->engine)
            d->engine->notifyInferiorIll();
    } else if (d->state != Inactive) {
180 181 182 183 184 185
        handleAdapterSetupFailed(error);
    }
}

void AbstractRemoteLinuxDebugSupport::startExecution()
{
186
    if (d->state == Inactive)
187 188
        return;

189
    QTC_ASSERT(d->state == StartingRunner, return);
190

191 192 193
    if (d->cppDebugging && !setPort(d->gdbServerPort))
        return;
    if (d->qmlDebugging && !setPort(d->qmlPort))
194 195
            return;

196 197
    d->state = StartingRemoteProcess;
    d->gdbserverOutput.clear();
198 199 200 201
    connect(runner(), SIGNAL(remoteErrorOutput(QByteArray)), this,
        SLOT(handleRemoteErrorOutput(QByteArray)));
    connect(runner(), SIGNAL(remoteOutput(QByteArray)), this,
        SLOT(handleRemoteOutput(QByteArray)));
202
    if (d->qmlDebugging && !d->cppDebugging) {
203 204 205 206 207
        connect(runner(), SIGNAL(remoteProcessStarted()),
            SLOT(handleRemoteProcessStarted()));
    }
    const QString &remoteExe = runner()->remoteExecutable();
    QString args = runner()->arguments();
208
    if (d->qmlDebugging) {
209
        args += QString::fromLatin1(" -qmljsdebugger=port:%1,block")
210
            .arg(d->qmlPort);
211 212
    }

213
    const QString remoteCommandLine = (d->qmlDebugging && !d->cppDebugging)
214
        ? QString::fromLatin1("%1 %2 %3").arg(runner()->commandPrefix()).arg(remoteExe).arg(args)
215 216 217
        : QString::fromLatin1("%1 gdbserver %5:%2 %3 %4").arg(runner()->commandPrefix())
              .arg(d->gdbServerPort).arg(remoteExe).arg(args)
              .arg(runner()->connection()->connectionInfo().peerAddress.toString());
218 219 220 221 222 223 224
    connect(runner(), SIGNAL(remoteProcessFinished(qint64)),
        SLOT(handleRemoteProcessFinished(qint64)));
    runner()->startExecution(remoteCommandLine.toUtf8());
}

void AbstractRemoteLinuxDebugSupport::handleRemoteProcessFinished(qint64 exitCode)
{
225
    if (!d->engine || d->state == Inactive)
226 227
        return;

228
    if (d->state == Debugging) {
229
        // The QML engine does not realize on its own that the application has finished.
230
        if (d->qmlDebugging && !d->cppDebugging)
231
            d->engine->quitDebugger();
232
        else if (exitCode != 0)
233
            d->engine->notifyInferiorIll();
234

235
    } else {
236
        const QString errorMsg = (d->qmlDebugging && !d->cppDebugging)
237 238
            ? tr("Remote application failed with exit code %1.").arg(exitCode)
            : tr("The gdbserver process closed unexpectedly.");
239
        d->engine->handleRemoteSetupFailed(errorMsg);
240 241 242 243 244
    }
}

void AbstractRemoteLinuxDebugSupport::handleDebuggingFinished()
{
245
    setFinished();
246 247 248 249
}

void AbstractRemoteLinuxDebugSupport::handleRemoteOutput(const QByteArray &output)
{
250
    QTC_ASSERT(d->state == Inactive || d->state == Debugging, return);
251

252 253 254 255 256
    showMessage(QString::fromUtf8(output), AppOutput);
}

void AbstractRemoteLinuxDebugSupport::handleRemoteErrorOutput(const QByteArray &output)
{
257
    QTC_ASSERT(d->state == Inactive || d->state == StartingRemoteProcess || d->state == Debugging,
258
        return);
259

260
    if (!d->engine)
261 262 263
        return;

    showMessage(QString::fromUtf8(output), AppOutput);
264
    if (d->state == StartingRemoteProcess && d->cppDebugging) {
265 266
        d->gdbserverOutput += output;
        if (d->gdbserverOutput.contains("Listening on port")) {
267
            handleAdapterSetupDone();
268
            d->gdbserverOutput.clear();
269 270 271 272 273 274 275 276 277 278 279
        }
    }
}

void AbstractRemoteLinuxDebugSupport::handleProgressReport(const QString &progressOutput)
{
    showMessage(progressOutput + QLatin1Char('\n'), AppStuff);
}

void AbstractRemoteLinuxDebugSupport::handleAdapterSetupFailed(const QString &error)
{
280
    setFinished();
281
    d->engine->handleRemoteSetupFailed(tr("Initial setup failed: %1").arg(error));
282 283 284 285
}

void AbstractRemoteLinuxDebugSupport::handleAdapterSetupDone()
{
286 287
    d->state = Debugging;
    d->engine->handleRemoteSetupDone(d->gdbServerPort, d->qmlPort);
288 289 290 291
}

void AbstractRemoteLinuxDebugSupport::handleRemoteProcessStarted()
{
292
    Q_ASSERT(d->qmlDebugging && !d->cppDebugging);
293
    QTC_ASSERT(d->state == StartingRemoteProcess, return);
294

295 296 297
    handleAdapterSetupDone();
}

298
void AbstractRemoteLinuxDebugSupport::setFinished()
299
{
300
    if (d->state == Inactive)
301
        return;
302
    d->state = Inactive;
303
    runner()->stop();
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
}

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


RemoteLinuxDebugSupport::RemoteLinuxDebugSupport(RemoteLinuxRunConfiguration *runConfig,
        DebuggerEngine *engine)
    : AbstractRemoteLinuxDebugSupport(runConfig, engine),
320
      d(new RemoteLinuxDebugSupportPrivate(runConfig))
321 322 323 324 325
{
}

RemoteLinuxDebugSupport::~RemoteLinuxDebugSupport()
{
326
    delete d;
327 328 329 330
}

AbstractRemoteLinuxApplicationRunner *RemoteLinuxDebugSupport::runner() const
{
331
    return &d->runner;
332 333 334
}

} // namespace RemoteLinux