maemodebugsupport.cpp 10.7 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
41
42
43
44
45
46
47
48
49
50
/****************************************************************************
**
** 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 "maemorunconfiguration.h"
#include "maemosshrunner.h"

#include <coreplugin/ssh/sftpchannel.h>
#include <debugger/debuggerengine.h>
#include <debugger/debuggerplugin.h>
#include <debugger/debuggerrunner.h>
#include <debugger/gdb/remotegdbserveradapter.h>
#include <debugger/gdb/remoteplaingdbadapter.h>
#include <projectexplorer/toolchain.h>

51
#include <QtCore/QDir>
ck's avatar
ck committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <QtCore/QFileInfo>

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();
66
67
68
69
70
71
72
    if (runConfig->useRemoteGdb()) {
        params.startMode = StartRemoteGdb;
        params.executable = runConfig->remoteExecutableFilePath();
        params.debuggerCommand
            = MaemoGlobal::remoteCommandPrefix(runConfig->remoteExecutableFilePath())
                + QLatin1String(" /usr/bin/gdb");
        params.connParams = devConf.server;
73
74
        params.localMountDir = runConfig->localDirToMountForRemoteGdb();
        params.remoteMountPoint = MaemoGlobal::remoteProjectSourcesMountPoint();
75
76
77
        const QString execDirAbs
            = QDir::fromNativeSeparators(QFileInfo(runConfig->localExecutableFilePath()).path());
        const QString execDirRel
78
79
80
            = QDir(params.localMountDir).relativeFilePath(execDirAbs);
        params.remoteSourcesDir = QString(params.remoteMountPoint
            + QLatin1Char('/') + execDirRel).toUtf8();
81
82
83
84
85
86
87
88
    } else {
        params.startMode = AttachToRemote;
        params.executable = runConfig->localExecutableFilePath();
        params.debuggerCommand = runConfig->gdbCmd();
        params.remoteChannel = devConf.server.host + QLatin1Char(':')
            + gdbServerPort(runConfig, devConf);
        params.remoteArchitecture = QLatin1String("arm");
    }
ck's avatar
ck committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    params.processArgs = runConfig->arguments();
    params.sysRoot = runConfig->sysRoot();
    params.toolChainType = ToolChain::GCC_MAEMO;
    params.dumperLibrary = runConfig->dumperLib();
    params.remoteDumperLib = uploadDir(devConf).toUtf8() + '/'
        + QFileInfo(runConfig->dumperLib()).fileName().toUtf8();

    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()),
106
      m_runner(new MaemoSshRunner(this, m_runConfig, true))
ck's avatar
ck committed
107
108
109
{
    GdbEngine *engine = qobject_cast<GdbEngine *>(m_runControl->engine());
    Q_ASSERT(engine);
110
    m_gdbAdapter = engine->gdbAdapter();
ck's avatar
ck committed
111
112
113
114
115
116
117
    Q_ASSERT(m_gdbAdapter);
    connect(m_gdbAdapter, SIGNAL(requestSetup()), this,
        SLOT(handleAdapterSetupRequested()));
    connect(m_runControl, SIGNAL(finished()), this,
        SLOT(handleDebuggingFinished()));
}

118
119
120
121
MaemoDebugSupport::~MaemoDebugSupport()
{
    stopSsh();
}
ck's avatar
ck committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

void MaemoDebugSupport::handleAdapterSetupRequested()
{
    if (!m_deviceConfig.isValid()) {
        handleAdapterSetupFailed(tr("No device configuration set for run configuration."));
        return;
    }
    m_adapterStarted = false;
    m_stopped = false;
    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()));
    m_runner->start();
}

void MaemoDebugSupport::handleSshError(const QString &error)
{
    if (!m_stopped && !m_adapterStarted)
        handleAdapterSetupFailed(error);
}

void MaemoDebugSupport::startExecution()
{
    if (m_stopped)
        return;

    const QString &dumperLib = m_runConfig->dumperLib();
    if (!dumperLib.isEmpty()
        && m_runConfig->deployStep()->currentlyNeedsDeployment(m_deviceConfig.server.host,
               MaemoDeployable(dumperLib, uploadDir(m_deviceConfig)))) {
        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 {
        startDebugging();
    }
}

void MaemoDebugSupport::handleSftpChannelInitialized()
{
    if (m_stopped)
        return;

    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 {
        m_runControl->showMessage(tr("Started uploading debugging helpers ('%1').")
            .arg(dumperLib), AppStuff);
    }
}

void MaemoDebugSupport::handleSftpChannelInitializationFailed(const QString &error)
{
    if (m_stopped)
        return;

    handleAdapterSetupFailed(error);
}

void MaemoDebugSupport::handleSftpJobFinished(Core::SftpJobId job,
    const QString &error)
{
    if (m_stopped)
        return;

    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 {
        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()
{
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
    if (useGdb()) {
        handleAdapterSetupDone();
    } else {
        connect(m_runner, SIGNAL(remoteErrorOutput(QByteArray)), this,
            SLOT(handleRemoteErrorOutput(QByteArray)));
        connect(m_runner, SIGNAL(remoteOutput(QByteArray)), this,
            SLOT(handleRemoteOutput(QByteArray)));
        connect(m_runner, SIGNAL(remoteProcessStarted()), this,
            SLOT(handleRemoteProcessStarted()));
        const QString &remoteExe = m_runConfig->remoteExecutableFilePath();
        m_runner->startExecution(QString::fromLocal8Bit("%1 gdbserver :%2 %3 %4")
            .arg(MaemoGlobal::remoteCommandPrefix(remoteExe))
            .arg(gdbServerPort(m_runConfig, m_deviceConfig))
            .arg(remoteExe).arg(m_runConfig->arguments()
            .join(QLatin1String(" "))).toUtf8());
    }
ck's avatar
ck committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
}

void MaemoDebugSupport::handleRemoteProcessStarted()
{
    handleAdapterSetupDone();
}

void MaemoDebugSupport::handleDebuggingFinished()
{
    m_stopped = true;
    stopSsh();
}

void MaemoDebugSupport::handleRemoteOutput(const QByteArray &output)
{
    m_runControl->showMessage(QString::fromUtf8(output), AppOutput);
}

void MaemoDebugSupport::handleRemoteErrorOutput(const QByteArray &output)
{
    m_runControl->showMessage(QString::fromUtf8(output), AppOutput);
}

void MaemoDebugSupport::stopSsh()
{
    disconnect(m_runner, 0, this, 0);
    if (m_uploader) {
        disconnect(m_uploader.data(), 0, this, 0);
        m_uploader->closeChannel();
    }
    m_runner->stop();
}

void MaemoDebugSupport::handleAdapterSetupFailed(const QString &error)
{
271
272
273
274
275
    const QString msg = tr("Initial setup failed: %1").arg(error);
    if (useGdb())
        qobject_cast<RemotePlainGdbAdapter *>(m_gdbAdapter)->handleSetupFailed(msg);
    else
        qobject_cast<RemoteGdbServerAdapter*>(m_gdbAdapter)->handleSetupFailed(msg);
ck's avatar
ck committed
276
277
278
279
280
281
282
    m_stopped = true;
    stopSsh();
}

void MaemoDebugSupport::handleAdapterSetupDone()
{
    m_adapterStarted = true;
283
284
285
286
    if (useGdb())
        qobject_cast<RemotePlainGdbAdapter *>(m_gdbAdapter)->handleSetupDone();
    else
        qobject_cast<RemoteGdbServerAdapter*>(m_gdbAdapter)->handleSetupDone();
ck's avatar
ck committed
287
288
289
290
291
292
293
294
295
}

QString MaemoDebugSupport::gdbServerPort(const MaemoRunConfiguration *rc,
    const MaemoDeviceConfig &devConf)
{
    // During configuration we don't know which port to use, so we display
    // something in the config dialog, but we will make sure we use
    // the right port from the information file.
    return devConf.type == MaemoDeviceConfig::Physical
296
        ? QString::number(devConf.debuggingPort)
ck's avatar
ck committed
297
298
299
300
301
302
303
304
        : rc->runtimeGdbServerPort();
}

QString MaemoDebugSupport::uploadDir(const MaemoDeviceConfig &devConf)
{
    return MaemoGlobal::homeDirOnDevice(devConf.server.uname);
}

305
306
307
308
309
bool MaemoDebugSupport::useGdb() const
{
    return m_runControl->engine()->startParameters().startMode == StartRemoteGdb;
}

ck's avatar
ck committed
310
311
} // namespace Internal
} // namespace Qt4ProjectManager