remotegdbserveradapter.cpp 14.2 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
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** 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.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30 31 32
**
**************************************************************************/

ck's avatar
ck committed
33
#include "remotegdbserveradapter.h"
hjk's avatar
hjk committed
34

35
#include "debuggeractions.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
36
#include "debuggerstartparameters.h"
hjk's avatar
hjk committed
37
#include "debuggercore.h"
38
#include "debuggerstringutils.h"
39
#include "gdbengine.h"
hjk's avatar
hjk committed
40
#include "gdbmi.h"
41 42

#include <utils/qtcassert.h>
43
#include <utils/fancymainwindow.h>
44
#include <projectexplorer/abi.h>
45

46 47
#include <QFileInfo>
#include <QMessageBox>
48 49 50 51 52

namespace Debugger {
namespace Internal {

#define CB(callback) \
53
    static_cast<GdbEngine::GdbCommandCallback>(&GdbRemoteServerEngine::callback), \
54 55 56 57 58 59 60 61
    STRINGIFY(callback)

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

62 63 64
GdbRemoteServerEngine::GdbRemoteServerEngine(const DebuggerStartParameters &startParameters,
    DebuggerEngine *masterEngine)
    : GdbEngine(startParameters, masterEngine)
65
{
66
    connect(&m_uploadProc, SIGNAL(error(QProcess::ProcessError)),
hjk's avatar
hjk committed
67
        SLOT(uploadProcError(QProcess::ProcessError)));
68
    connect(&m_uploadProc, SIGNAL(readyReadStandardOutput()),
hjk's avatar
hjk committed
69
        SLOT(readUploadStandardOutput()));
70
    connect(&m_uploadProc, SIGNAL(readyReadStandardError()),
hjk's avatar
hjk committed
71 72
        SLOT(readUploadStandardError()));
    connect(&m_uploadProc, SIGNAL(finished(int)),
ck's avatar
ck committed
73
        SLOT(uploadProcFinished()));
74 75
}

76
GdbEngine::DumperHandling GdbRemoteServerEngine::dumperHandling() const
77
{
hjk's avatar
hjk committed
78 79 80 81 82
    using namespace ProjectExplorer;
    const Abi abi = startParameters().toolChainAbi;
    if (abi.os() == Abi::SymbianOS
            || abi.os() == Abi::WindowsOS
            || abi.binaryFormat() == Abi::ElfFormat)
83 84 85 86
        return DumperLoadedByGdb;
    return DumperLoadedByGdbPreload;
}

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

105
void GdbRemoteServerEngine::uploadProcError(QProcess::ProcessError error)
106 107 108 109
{
    QString msg;
    switch (error) {
        case QProcess::FailedToStart:
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
110
            msg = tr("The upload process failed to start. Shell missing?");
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            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().");
    }

135
    showMessage(msg, StatusBar);
hjk's avatar
hjk committed
136
    showMessageBox(QMessageBox::Critical, tr("Error"), msg);
137 138
}

139
void GdbRemoteServerEngine::readUploadStandardOutput()
140
{
141 142 143 144
    const QByteArray ba = m_uploadProc.readAllStandardOutput();
    const QString msg = QString::fromLocal8Bit(ba, ba.length());
    showMessage(msg, LogOutput);
    showMessage(msg, AppOutput);
145 146
}

147
void GdbRemoteServerEngine::readUploadStandardError()
148
{
149 150 151 152
    const QByteArray ba = m_uploadProc.readAllStandardError();
    const QString msg = QString::fromLocal8Bit(ba, ba.length());
    showMessage(msg, LogOutput);
    showMessage(msg, AppError);
153 154
}

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

164
void GdbRemoteServerEngine::setupInferior()
165
{
hjk's avatar
hjk committed
166
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
hjk's avatar
hjk committed
167
    const DebuggerStartParameters &sp = startParameters();
BogDan Vatra's avatar
BogDan Vatra committed
168 169 170 171 172
#ifdef Q_OS_WIN
    #define PATHSEP ";"
#else
    #define PATHSEP ":"
#endif
173
    QString fileName;
hjk's avatar
hjk committed
174 175
    if (!sp.executable.isEmpty()) {
        QFileInfo fi(sp.executable);
176 177
        fileName = fi.absoluteFilePath();
    }
178 179 180
    //const QByteArray sysroot = sp.sysroot.toLocal8Bit();
    //const QByteArray remoteArch = sp.remoteArchitecture.toLatin1();
    //const QByteArray gnuTarget = sp.gnuTarget.toLatin1();
hjk's avatar
hjk committed
181
    const QString args = sp.processArgs;
182 183 184 185 186 187

//    if (!remoteArch.isEmpty())
//        postCommand("set architecture " + remoteArch);
//    if (!gnuTarget.isEmpty())
//        postCommand("set gnutarget " + gnuTarget);
    const QString solibSearchPath = sp.solibSearchPath.join(QLatin1String(PATHSEP));
BogDan Vatra's avatar
BogDan Vatra committed
188
    if (!solibSearchPath.isEmpty())
189
        postCommand("set solib-search-path " + solibSearchPath.toLocal8Bit());
BogDan Vatra's avatar
BogDan Vatra committed
190

191
    if (!args.isEmpty())
192
        postCommand("-exec-arguments " + args.toLocal8Bit());
193

194 195
    // 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
196
    // loaded.  Use the "file" command.' as gdb tries to set the
197 198 199 200 201 202 203 204 205 206 207
    // 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..
hjk's avatar
hjk committed
208 209


hjk's avatar
hjk committed
210
    // gdb/mi/mi-main.c:1958: internal-error:
hjk's avatar
hjk committed
211 212
    // mi_execute_async_cli_command: Assertion `is_running (inferior_ptid)'
    // failed.\nA problem internal to GDB has been detected,[...]
213
    if (debuggerCore()->boolSetting(TargetAsync))
214
        postCommand("set target-async on", CB(handleSetTargetAsync));
215 216 217 218 219 220 221

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

222
    postCommand("-file-exec-and-symbols \"" + fileName.toLocal8Bit() + '"',
hjk's avatar
hjk committed
223
        CB(handleFileExecAndSymbols));
224 225
}

226
void GdbRemoteServerEngine::handleSetTargetAsync(const GdbResponse &response)
227
{
hjk's avatar
hjk committed
228
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
229 230
    if (response.resultClass == GdbResultError)
        qDebug() << "Adapter too old: does not support asynchronous mode.";
231 232
}

233
void GdbRemoteServerEngine::handleFileExecAndSymbols(const GdbResponse &response)
234
{
hjk's avatar
hjk committed
235
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
236
    if (response.resultClass == GdbResultDone) {
237
        callTargetRemote();
238
    } else {
239
        QByteArray reason = response.data.findChild("msg").data();
240
        QString msg = tr("Reading debug information failed:\n");
241 242 243 244 245 246
        msg += QString::fromLocal8Bit(reason);
        if (reason.endsWith("No such file or directory.")) {
            showMessage(_("INFERIOR STARTUP: BINARY NOT FOUND"));
            showMessage(msg, StatusBar);
            callTargetRemote(); // Proceed nevertheless.
        } else {
247
            notifyInferiorSetupFailed(msg);
248
        }
249 250 251
    }
}

252
void GdbRemoteServerEngine::callTargetRemote()
253 254 255 256 257 258 259
{
    //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)
260 261 262 263 264 265 266 267 268 269 270 271
    QByteArray channel = startParameters().remoteChannel.toLatin1();

    // Don't touch channels with explicitly set protocols.
    if (!channel.startsWith("tcp:") && !channel.startsWith("udp:")
            && !channel.startsWith("file:") && channel.contains(':'))
    {
        // "Fix" the IPv6 case with host names without '['...']'
        if (!channel.startsWith('[') && channel.count(':') >= 2) {
            channel.insert(0, '[');
            channel.insert(channel.lastIndexOf(':'), ']');
        }
        channel = "tcp:" + channel;
272
    }
273

274 275
    if (m_isQnxGdb)
        postCommand("target qnx " + channel, CB(handleTargetQnx));
276
    else
277
        postCommand("target remote " + channel, CB(handleTargetRemote));
278 279
}

280
void GdbRemoteServerEngine::handleTargetRemote(const GdbResponse &record)
281
{
hjk's avatar
hjk committed
282
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
283
    if (record.resultClass == GdbResultDone) {
284
        // gdb server will stop the remote application itself.
285 286
        showMessage(_("INFERIOR STARTED"));
        showMessage(msgAttachedToStoppedInferior(), StatusBar);
287
        handleInferiorPrepared();
288
    } else {
289
        // 16^error,msg="hd:5555: Connection timed out."
290 291
        QString msg = msgConnectRemoteServerFailed(
            QString::fromLocal8Bit(record.data.findChild("msg").data()));
292
        notifyInferiorSetupFailed(msg);
293 294 295
    }
}

296
void GdbRemoteServerEngine::handleTargetQnx(const GdbResponse &response)
297
{
298
    QTC_ASSERT(m_isQnxGdb, qDebug() << m_isQnxGdb);
299 300 301 302 303 304 305 306
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
    if (response.resultClass == GdbResultDone) {
        // gdb server will stop the remote application itself.
        showMessage(_("INFERIOR STARTED"));
        showMessage(msgAttachedToStoppedInferior(), StatusBar);

        const qint64 pid = startParameters().attachPID;
        if (pid > -1) {
307
            postCommand("attach " + QByteArray::number(pid), CB(handleAttach));
308
        } else {
309
            handleInferiorPrepared();
310 311 312 313 314
        }
    } else {
        // 16^error,msg="hd:5555: Connection timed out."
        QString msg = msgConnectRemoteServerFailed(
            QString::fromLocal8Bit(response.data.findChild("msg").data()));
315
        notifyInferiorSetupFailed(msg);
316 317 318
    }
}

319
void GdbRemoteServerEngine::handleAttach(const GdbResponse &response)
320 321 322 323 324 325 326
{
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
    switch (response.resultClass) {
    case GdbResultDone:
    case GdbResultRunning: {
        showMessage(_("INFERIOR ATTACHED"));
        showMessage(msgAttachedToStoppedInferior(), StatusBar);
327
        handleInferiorPrepared();
328 329 330 331
        break;
    }
    case GdbResultError:
        if (response.data.findChild("msg").data() == "ptrace: Operation not permitted.") {
332
            notifyInferiorSetupFailed(DumperHelper::msgPtraceError(startParameters().startMode));
333 334 335 336 337
            break;
        }
        // if msg != "ptrace: ..." fall through
    default:
        QString msg = QString::fromLocal8Bit(response.data.findChild("msg").data());
338
        notifyInferiorSetupFailed(msg);
339 340 341
    }
}

342
void GdbRemoteServerEngine::runEngine()
343
{
344
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
345 346
    notifyEngineRunAndInferiorStopOk();
    continueInferiorInternal();
347 348
}

349
void GdbRemoteServerEngine::interruptInferior2()
350
{
351
    QTC_ASSERT(state() == InferiorStopRequested, qDebug() << state());
352
    if (debuggerCore()->boolSetting(TargetAsync)) {
353
        postCommand("-exec-interrupt", GdbEngine::Immediate,
354 355 356 357 358
            CB(handleInterruptInferior));
    } else {
        bool ok = m_gdbProc.interrupt();
        if (!ok) {
            // FIXME: Extra state needed?
359 360 361
            showMessage(_("NOTE: INFERIOR STOP NOT POSSIBLE"));
            showStatusMessage(tr("Interrupting not possible"));
            notifyInferiorRunOk();
362
        }
363
    }
364 365
}

366
void GdbRemoteServerEngine::handleInterruptInferior(const GdbResponse &response)
367 368 369 370 371 372 373
{
    if (response.resultClass == GdbResultDone) {
        // The gdb server will trigger extra output that we will pick up
        // to do a proper state transition.
    } else {
        // FIXME: On some gdb versions like git 170ffa5d7dd this produces
        // >810^error,msg="mi_cmd_exec_interrupt: Inferior not executing."
374
        notifyInferiorStopOk();
375
    }
376 377
}

378
void GdbRemoteServerEngine::shutdownEngine()
hjk's avatar
hjk committed
379
{
380
    notifyAdapterShutdownOk();
381 382
}

383
void GdbRemoteServerEngine::handleRemoteSetupDone(int gdbServerPort, int qmlPort)
ck's avatar
ck committed
384
{
385
    notifyEngineRemoteSetupDone();
ck's avatar
ck committed
386 387
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());

388 389 390 391 392 393 394 395 396 397
    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));
        }
    }
398
    startGdb();
399 400
}

401
void GdbRemoteServerEngine::handleRemoteSetupFailed(const QString &reason)
ck's avatar
ck committed
402 403
{
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
404
    handleAdapterStartFailed(reason);
ck's avatar
ck committed
405 406
}

407 408
} // namespace Internal
} // namespace Debugger