remotegdbserveradapter.cpp 11.2 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

ck's avatar
ck committed
30
#include "remotegdbserveradapter.h"
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
31

32
#include "debuggerstringutils.h"
33 34 35
#include "gdbengine.h"

#include <utils/qtcassert.h>
36
#include <utils/fancymainwindow.h>
37
#include <projectexplorer/toolchaintype.h>
38 39 40 41 42 43 44 45

#include <QtCore/QFileInfo>
#include <QtGui/QMessageBox>

namespace Debugger {
namespace Internal {

#define CB(callback) \
ck's avatar
ck committed
46
    static_cast<GdbEngine::AdapterCallback>(&RemoteGdbServerAdapter::callback), \
47 48 49 50 51 52 53 54
    STRINGIFY(callback)

///////////////////////////////////////////////////////////////////////
//
// RemoteGdbAdapter
//
///////////////////////////////////////////////////////////////////////

ck's avatar
ck committed
55
RemoteGdbServerAdapter::RemoteGdbServerAdapter(GdbEngine *engine, int toolChainType, QObject *parent) :
56 57
    AbstractGdbAdapter(engine, parent),
    m_toolChainType(toolChainType)
58
{
59 60 61 62 63 64
    connect(&m_uploadProc, SIGNAL(error(QProcess::ProcessError)),
        this, SLOT(uploadProcError(QProcess::ProcessError)));
    connect(&m_uploadProc, SIGNAL(readyReadStandardOutput()),
        this, SLOT(readUploadStandardOutput()));
    connect(&m_uploadProc, SIGNAL(readyReadStandardError()),
        this, SLOT(readUploadStandardError()));
ck's avatar
ck committed
65 66
    connect(&m_uploadProc, SIGNAL(finished(int)), this,
        SLOT(uploadProcFinished()));
67 68
}

ck's avatar
ck committed
69
AbstractGdbAdapter::DumperHandling RemoteGdbServerAdapter::dumperHandling() const
70 71
{
    switch (m_toolChainType) {
72 73 74 75 76 77 78 79
    case ProjectExplorer::ToolChain_MinGW:
    case ProjectExplorer::ToolChain_MSVC:
    case ProjectExplorer::ToolChain_WINCE:
    case ProjectExplorer::ToolChain_WINSCW:
    case ProjectExplorer::ToolChain_GCCE:
    case ProjectExplorer::ToolChain_RVCT_ARMV5:
    case ProjectExplorer::ToolChain_RVCT_ARMV6:
    case ProjectExplorer::ToolChain_GCC_MAEMO:
80 81 82 83 84 85 86
        return DumperLoadedByGdb;
    default:
        break;
    }
    return DumperLoadedByGdbPreload;
}

ck's avatar
ck committed
87
void RemoteGdbServerAdapter::startAdapter()
88
{
hjk's avatar
hjk committed
89
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
90
    showMessage(_("TRYING TO START ADAPTER"));
hjk's avatar
hjk committed
91 92 93 94
    if (!startParameters().useServerStartScript) {
        handleSetupDone();
        return;
    }
95
    if (startParameters().serverStartScript.isEmpty()) {
ck's avatar
ck committed
96 97
        showMessage(_("No server start script given. "), StatusBar);
        emit requestSetup();
hjk's avatar
hjk committed
98
    } else {
99
        m_uploadProc.start(_("/bin/sh ") + startParameters().serverStartScript);
hjk's avatar
hjk committed
100 101
        m_uploadProc.waitForStarted();
    }
102 103
}

ck's avatar
ck committed
104
void RemoteGdbServerAdapter::uploadProcError(QProcess::ProcessError error)
105 106 107 108
{
    QString msg;
    switch (error) {
        case QProcess::FailedToStart:
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
109
            msg = tr("The upload process failed to start. Shell missing?");
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            break;
        case QProcess::Crashed:
            msg = tr("The upload process crashed some time after starting "
                "successfully.");
            break;
        case QProcess::Timedout:
            msg = tr("The last waitFor...() function timed out. "
                "The state of QProcess is unchanged, and you can try calling "
                "waitFor...() again.");
            break;
        case QProcess::WriteError:
            msg = tr("An error occurred when attempting to write "
                "to the upload process. For example, the process may not be running, "
                "or it may have closed its input channel.");
            break;
        case QProcess::ReadError:
            msg = tr("An error occurred when attempting to read from "
                "the upload process. For example, the process may not be running.");
            break;
        default:
            msg = tr("An unknown error in the upload process occurred. "
                "This is the default return value of error().");
    }

134
    showMessage(msg, StatusBar);
135
    DebuggerEngine::showMessageBox(QMessageBox::Critical, tr("Error"), msg);
136 137
}

ck's avatar
ck committed
138
void RemoteGdbServerAdapter::readUploadStandardOutput()
139
{
140 141 142 143
    const QByteArray ba = m_uploadProc.readAllStandardOutput();
    const QString msg = QString::fromLocal8Bit(ba, ba.length());
    showMessage(msg, LogOutput);
    showMessage(msg, AppOutput);
144 145
}

ck's avatar
ck committed
146
void RemoteGdbServerAdapter::readUploadStandardError()
147
{
148 149 150 151
    const QByteArray ba = m_uploadProc.readAllStandardError();
    const QString msg = QString::fromLocal8Bit(ba, ba.length());
    showMessage(msg, LogOutput);
    showMessage(msg, AppError);
152 153
}

ck's avatar
ck committed
154 155 156 157 158 159 160 161 162
void RemoteGdbServerAdapter::uploadProcFinished()
{
    if (m_uploadProc.exitStatus() == QProcess::NormalExit
        && m_uploadProc.exitCode() == 0)
        handleSetupDone();
    else
        handleSetupFailed(m_uploadProc.errorString());
}

hjk's avatar
hjk committed
163
void RemoteGdbServerAdapter::setupInferior()
164
{
hjk's avatar
hjk committed
165
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
166

167 168 169 170 171
    QString fileName;
    if (!startParameters().executable.isEmpty()) {
        QFileInfo fi(startParameters().executable);
        fileName = fi.absoluteFilePath();
    }
172 173
    const QByteArray sysRoot = startParameters().sysRoot.toLocal8Bit();
    const QByteArray remoteArch = startParameters().remoteArchitecture.toLatin1();
174
    const QByteArray gnuTarget = startParameters().gnuTarget.toLatin1();
175 176 177 178 179 180
    const QByteArray solibPath =
         QFileInfo(startParameters().dumperLibrary).path().toLocal8Bit();
    const QString args = startParameters().processArgs.join(_(" "));

    if (!remoteArch.isEmpty())
        m_engine->postCommand("set architecture " + remoteArch);
181 182
    if (!gnuTarget.isEmpty())
        m_engine->postCommand("set gnutarget " + gnuTarget);
183 184 185 186 187
    if (!sysRoot.isEmpty())
        m_engine->postCommand("set sysroot " + sysRoot);
    if (!solibPath.isEmpty())
        m_engine->postCommand("set solib-search-path " + solibPath);
    if (!args.isEmpty())
188
        m_engine->postCommand("-exec-arguments " + args.toLocal8Bit());
189

190 191
    // This has to be issued before 'target remote'. On pre-7.0 the
    // command is not present and will result in ' No symbol table is
192
    // loaded.  Use the "file" command.' as gdb tries to set the
193 194 195 196 197 198 199 200 201 202 203
    // value of a variable with name 'target-async'.
    //
    // Testing with -list-target-features which was introduced at
    // the same time would not work either, as this need an existing
    // target.
    //
    // Using it even without a target and having it fail might still
    // be better as:
    // Some external comment: '[but] "set target-async on" with a native
    // windows gdb will work, but then fail when you actually do
    // "run"/"attach", I think..
204
    m_engine->postCommand("set target-async on", CB(handleSetTargetAsync));
205 206 207 208 209 210 211

    if (fileName.isEmpty()) {
        showMessage(tr("No symbol file given."), StatusBar);
        callTargetRemote();
        return;
    }

212 213
    m_engine->postCommand("-file-exec-and-symbols \""
        + fileName.toLocal8Bit() + '"',
hjk's avatar
hjk committed
214
        CB(handleFileExecAndSymbols));
215 216
}

ck's avatar
ck committed
217
void RemoteGdbServerAdapter::handleSetTargetAsync(const GdbResponse &response)
218
{
hjk's avatar
hjk committed
219
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
220 221
    if (response.resultClass == GdbResultError)
        qDebug() << "Adapter too old: does not support asynchronous mode.";
222 223
}

ck's avatar
ck committed
224
void RemoteGdbServerAdapter::handleFileExecAndSymbols(const GdbResponse &response)
225
{
hjk's avatar
hjk committed
226
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
227
    if (response.resultClass == GdbResultDone) {
228
        callTargetRemote();
229
    } else {
230
        QString msg = tr("Reading debug information failed:\n");
231
        msg += QString::fromLocal8Bit(response.data.findChild("msg").data());
hjk's avatar
hjk committed
232
        m_engine->notifyInferiorSetupFailed(msg);
233 234 235
    }
}

236 237 238 239 240 241 242 243 244 245 246 247 248
void RemoteGdbServerAdapter::callTargetRemote()
{
    //m_breakHandler->clearBreakMarkers();

    // "target remote" does three things:
    //     (1) connects to the gdb server
    //     (2) starts the remote application
    //     (3) stops the remote application (early, e.g. in the dynamic linker)
    QString channel = startParameters().remoteChannel;
    m_engine->postCommand("target remote " + channel.toLatin1(),
        CB(handleTargetRemote));
}

ck's avatar
ck committed
249
void RemoteGdbServerAdapter::handleTargetRemote(const GdbResponse &record)
250
{
hjk's avatar
hjk committed
251
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
252
    if (record.resultClass == GdbResultDone) {
253
        // gdb server will stop the remote application itself.
254 255
        showMessage(_("INFERIOR STARTED"));
        showMessage(msgAttachedToStoppedInferior(), StatusBar);
256
        m_engine->handleInferiorPrepared();
257
    } else {
258
        // 16^error,msg="hd:5555: Connection timed out."
259 260
        QString msg = msgConnectRemoteServerFailed(
            QString::fromLocal8Bit(record.data.findChild("msg").data()));
hjk's avatar
hjk committed
261
        m_engine->notifyInferiorSetupFailed(msg);
262 263 264
    }
}

hjk's avatar
hjk committed
265
void RemoteGdbServerAdapter::runEngine()
266
{
267
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
268
    m_engine->notifyEngineRunAndInferiorStopOk();
269
    m_engine->continueInferiorInternal();
270 271
}

ck's avatar
ck committed
272
void RemoteGdbServerAdapter::interruptInferior()
273
{
274 275
    // FIXME: On some gdb versions like git 170ffa5d7dd this produces
    // >810^error,msg="mi_cmd_exec_interrupt: Inferior not executing."
276
    m_engine->postCommand("-exec-interrupt", GdbEngine::Immediate);
277 278
}

hjk's avatar
hjk committed
279
void RemoteGdbServerAdapter::shutdownInferior()
280
{
hjk's avatar
hjk committed
281 282 283 284 285 286
    m_engine->defaultInferiorShutdown("kill");
}

void RemoteGdbServerAdapter::shutdownAdapter()
{
    m_engine->notifyAdapterShutdownOk();
287 288
}

289
void RemoteGdbServerAdapter::handleSetupDone(int gdbServerPort, int qmlPort)
ck's avatar
ck committed
290 291 292
{
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    if (qmlPort != -1)
        startParameters().qmlServerPort = qmlPort;
    if (gdbServerPort != -1) {
        QString &rc = startParameters().remoteChannel;
        const int sepIndex = rc.lastIndexOf(QLatin1Char(':'));
        if (sepIndex != -1) {
            rc.replace(sepIndex + 1, rc.count() - sepIndex - 1,
                       QString::number(gdbServerPort));
        }
    }
    handleSetupDone();
}

void RemoteGdbServerAdapter::handleSetupDone()
{
ck's avatar
ck committed
308 309 310 311 312 313 314 315 316 317 318
    if (m_engine->startGdb(QStringList(), startParameters().debuggerCommand))
        m_engine->handleAdapterStarted();
}

void RemoteGdbServerAdapter::handleSetupFailed(const QString &reason)
{
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());

    m_engine->handleAdapterStartFailed(reason);
}

319 320
} // namespace Internal
} // namespace Debugger