remotegdbserveradapter.cpp 15.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
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
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.
**
28 29 30
**
**************************************************************************/

ck's avatar
ck committed
31
#include "remotegdbserveradapter.h"
hjk's avatar
hjk committed
32

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

40
#include <utils/hostosinfo.h>
41
#include <utils/qtcassert.h>
42
#include <utils/fancymainwindow.h>
43
#include <projectexplorer/abi.h>
44

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

namespace Debugger {
namespace Internal {

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

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

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

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

84
void GdbRemoteServerEngine::setupEngine()
85
{
hjk's avatar
hjk committed
86
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
87
    showMessage(_("TRYING TO START ADAPTER"));
88 89 90
    if (!startParameters().serverStartScript.isEmpty()) {
        m_uploadProc.start(_("/bin/sh ") + startParameters().serverStartScript);
        m_uploadProc.waitForStarted();
hjk's avatar
hjk committed
91
    }
92
    if (startParameters().remoteSetupNeeded)
93
        notifyEngineRequestRemoteSetup();
94
    else
95
        startGdb();
96 97
}

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

128
    showMessage(msg, StatusBar);
hjk's avatar
hjk committed
129
    showMessageBox(QMessageBox::Critical, tr("Error"), msg);
130 131
}

132
void GdbRemoteServerEngine::readUploadStandardOutput()
133
{
134 135 136 137
    const QByteArray ba = m_uploadProc.readAllStandardOutput();
    const QString msg = QString::fromLocal8Bit(ba, ba.length());
    showMessage(msg, LogOutput);
    showMessage(msg, AppOutput);
138 139
}

140
void GdbRemoteServerEngine::readUploadStandardError()
141
{
142 143 144 145
    const QByteArray ba = m_uploadProc.readAllStandardError();
    const QString msg = QString::fromLocal8Bit(ba, ba.length());
    showMessage(msg, LogOutput);
    showMessage(msg, AppError);
146 147
}

148
void GdbRemoteServerEngine::uploadProcFinished()
ck's avatar
ck committed
149 150 151
{
    if (m_uploadProc.exitStatus() == QProcess::NormalExit
        && m_uploadProc.exitCode() == 0)
152
        startGdb();
ck's avatar
ck committed
153
    else
154
        notifyEngineRemoteSetupFailed(m_uploadProc.errorString());
ck's avatar
ck committed
155 156
}

157
void GdbRemoteServerEngine::setupInferior()
158
{
hjk's avatar
hjk committed
159
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
hjk's avatar
hjk committed
160
    const DebuggerStartParameters &sp = startParameters();
Tobias Nätterlund's avatar
Tobias Nätterlund committed
161
    QString executableFileName;
hjk's avatar
hjk committed
162 163
    if (!sp.executable.isEmpty()) {
        QFileInfo fi(sp.executable);
Tobias Nätterlund's avatar
Tobias Nätterlund committed
164
        executableFileName = fi.absoluteFilePath();
165
    }
Tobias Nätterlund's avatar
Tobias Nätterlund committed
166 167 168 169 170 171
    QString symbolFileName;
    if (!sp.symbolFileName.isEmpty()) {
        QFileInfo fi(sp.symbolFileName);
        symbolFileName = fi.absoluteFilePath();
    }

172 173
    //const QByteArray sysroot = sp.sysroot.toLocal8Bit();
    //const QByteArray remoteArch = sp.remoteArchitecture.toLatin1();
hjk's avatar
hjk committed
174
    const QString args = sp.processArgs;
175 176 177

//    if (!remoteArch.isEmpty())
//        postCommand("set architecture " + remoteArch);
178 179
    const QString solibSearchPath
            = sp.solibSearchPath.join(QString(Utils::HostOsInfo::pathListSeparator()));
BogDan Vatra's avatar
BogDan Vatra committed
180
    if (!solibSearchPath.isEmpty())
181
        postCommand("set solib-search-path " + solibSearchPath.toLocal8Bit());
BogDan Vatra's avatar
BogDan Vatra committed
182

183
    if (!args.isEmpty())
184
        postCommand("-exec-arguments " + args.toLocal8Bit());
185

186 187
    // 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
188
    // loaded.  Use the "file" command.' as gdb tries to set the
189 190 191 192 193 194 195 196 197 198 199
    // 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
200 201


hjk's avatar
hjk committed
202
    // gdb/mi/mi-main.c:1958: internal-error:
hjk's avatar
hjk committed
203 204
    // mi_execute_async_cli_command: Assertion `is_running (inferior_ptid)'
    // failed.\nA problem internal to GDB has been detected,[...]
205
    if (debuggerCore()->boolSetting(TargetAsync))
206
        postCommand("set target-async on", CB(handleSetTargetAsync));
207

Tobias Nätterlund's avatar
Tobias Nätterlund committed
208
    if (executableFileName.isEmpty() && symbolFileName.isEmpty()) {
209 210 211 212 213
        showMessage(tr("No symbol file given."), StatusBar);
        callTargetRemote();
        return;
    }

Tobias Nätterlund's avatar
Tobias Nätterlund committed
214 215 216 217 218 219 220 221 222
    if (!symbolFileName.isEmpty()) {
        postCommand("-file-symbol-file \""
                    + symbolFileName.toLocal8Bit() + '"',
                    CB(handleFileExecAndSymbols));
    }
    if (!executableFileName.isEmpty()) {
        postCommand("-file-exec-and-symbols \"" + executableFileName.toLocal8Bit() + '"',
            CB(handleFileExecAndSymbols));
    }
223 224
}

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

232
void GdbRemoteServerEngine::handleFileExecAndSymbols(const GdbResponse &response)
233
{
hjk's avatar
hjk committed
234
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
235
    if (response.resultClass == GdbResultDone) {
236
        callTargetRemote();
237
    } else {
238
        QByteArray reason = response.data.findChild("msg").data();
239
        QString msg = tr("Reading debug information failed:\n");
240 241 242 243 244 245
        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 {
246
            notifyInferiorSetupFailed(msg);
247
        }
248 249 250
    }
}

251
void GdbRemoteServerEngine::callTargetRemote()
252 253 254 255 256 257 258
{
    //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)
259 260 261 262 263 264 265 266 267 268 269 270
    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;
271
    }
272

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

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

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

Tobias Nätterlund's avatar
Tobias Nätterlund committed
304
        const qint64 pid = isMasterEngine() ? startParameters().attachPID : masterEngine()->startParameters().attachPID;
305
        if (pid > -1) {
306
            postCommand("attach " + QByteArray::number(pid), CB(handleAttach));
307
        } else {
308
            handleInferiorPrepared();
309 310 311 312 313
        }
    } else {
        // 16^error,msg="hd:5555: Connection timed out."
        QString msg = msgConnectRemoteServerFailed(
            QString::fromLocal8Bit(response.data.findChild("msg").data()));
314
        notifyInferiorSetupFailed(msg);
315 316 317
    }
}

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

341
void GdbRemoteServerEngine::runEngine()
342
{
343
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
Tobias Nätterlund's avatar
Tobias Nätterlund committed
344 345 346

    const QString remoteExecutable = startParameters().remoteExecutable;
    if (!remoteExecutable.isEmpty()) {
347 348 349
        // Cannot use -exec-run for QNX gdb as it does not support path parameter for the MI call
        const QByteArray command = m_isQnxGdb ? "run" : "-exec-run";
        postCommand(command + " " + remoteExecutable.toLocal8Bit(), GdbEngine::RunRequest, CB(handleExecRun));
Tobias Nätterlund's avatar
Tobias Nätterlund committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    } else {
        notifyEngineRunAndInferiorStopOk();
        continueInferiorInternal();
    }
}

void GdbRemoteServerEngine::handleExecRun(const GdbResponse &response)
{
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
    if (response.resultClass == GdbResultRunning) {
        notifyEngineRunAndInferiorRunOk();
        showMessage(_("INFERIOR STARTED"));
        showMessage(msgInferiorSetupOk(), StatusBar);
    } else {
        QString msg = QString::fromLocal8Bit(response.data.findChild("msg").data());
        showMessage(msg);
        notifyEngineRunFailed();
    }
368 369
}

370
void GdbRemoteServerEngine::interruptInferior2()
371
{
372
    QTC_ASSERT(state() == InferiorStopRequested, qDebug() << state());
373
    if (debuggerCore()->boolSetting(TargetAsync)) {
374
        postCommand("-exec-interrupt", GdbEngine::Immediate,
375 376 377 378 379
            CB(handleInterruptInferior));
    } else {
        bool ok = m_gdbProc.interrupt();
        if (!ok) {
            // FIXME: Extra state needed?
380 381 382
            showMessage(_("NOTE: INFERIOR STOP NOT POSSIBLE"));
            showStatusMessage(tr("Interrupting not possible"));
            notifyInferiorRunOk();
383
        }
384
    }
385 386
}

387
void GdbRemoteServerEngine::handleInterruptInferior(const GdbResponse &response)
388 389 390 391 392 393 394
{
    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."
395
        notifyInferiorStopOk();
396
    }
397 398
}

399
void GdbRemoteServerEngine::shutdownEngine()
hjk's avatar
hjk committed
400
{
401
    notifyAdapterShutdownOk();
402 403
}

404
void GdbRemoteServerEngine::notifyEngineRemoteSetupDone(int gdbServerPort, int qmlPort)
ck's avatar
ck committed
405 406
{
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
407
    DebuggerEngine::notifyEngineRemoteSetupDone(gdbServerPort, qmlPort);
ck's avatar
ck committed
408

409 410 411 412 413 414 415 416 417 418
    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));
        }
    }
419
    startGdb();
420 421
}

422
void GdbRemoteServerEngine::notifyEngineRemoteSetupFailed(const QString &reason)
ck's avatar
ck committed
423 424
{
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
425
    DebuggerEngine::notifyEngineRemoteSetupFailed(reason);
426
    handleAdapterStartFailed(reason);
ck's avatar
ck committed
427 428
}

429 430
} // namespace Internal
} // namespace Debugger