remotegdbserveradapter.cpp 18.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

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

32 33
#include "gdbprocess.h"

34 35 36 37 38
#include <debugger/debuggeractions.h>
#include <debugger/debuggercore.h>
#include <debugger/debuggerprotocol.h>
#include <debugger/debuggerstartparameters.h>
#include <debugger/debuggerstringutils.h>
39

40
#include <utils/hostosinfo.h>
41
#include <utils/qtcassert.h>
42
#include <utils/qtcprocess.h>
43

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

namespace Debugger {
namespace Internal {

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

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

60 61
GdbRemoteServerEngine::GdbRemoteServerEngine(const DebuggerStartParameters &startParameters)
    : GdbEngine(startParameters)
62
{
hjk's avatar
hjk committed
63 64
    m_isMulti = false;
    m_targetPid = -1;
Eike Ziller's avatar
Eike Ziller committed
65 66
    if (Utils::HostOsInfo::isWindowsHost())
        m_gdbProc->setUseCtrlCStub(startParameters.useCtrlCStub); // This is only set for QNX/BlackBerry
67
    connect(&m_uploadProc, SIGNAL(error(QProcess::ProcessError)),
hjk's avatar
hjk committed
68
        SLOT(uploadProcError(QProcess::ProcessError)));
69
    connect(&m_uploadProc, SIGNAL(readyReadStandardOutput()),
hjk's avatar
hjk committed
70
        SLOT(readUploadStandardOutput()));
71
    connect(&m_uploadProc, SIGNAL(readyReadStandardError()),
hjk's avatar
hjk committed
72 73
        SLOT(readUploadStandardError()));
    connect(&m_uploadProc, SIGNAL(finished(int)),
ck's avatar
ck committed
74
        SLOT(uploadProcFinished()));
75 76
}

77
void GdbRemoteServerEngine::setupEngine()
78
{
hjk's avatar
hjk committed
79
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
80
    showMessage(_("TRYING TO START ADAPTER"));
81
    if (!startParameters().serverStartScript.isEmpty()) {
82 83 84 85 86 87 88 89

        // Provide script information about the environment
        QString arglist;
        Utils::QtcProcess::addArg(&arglist, startParameters().serverStartScript);
        Utils::QtcProcess::addArg(&arglist, startParameters().executable);
        Utils::QtcProcess::addArg(&arglist, startParameters().remoteChannel);

        m_uploadProc.start(_("/bin/sh ") + arglist);
90
        m_uploadProc.waitForStarted();
hjk's avatar
hjk committed
91
    }
92
    if (!startParameters().workingDirectory.isEmpty())
93
        m_gdbProc->setWorkingDirectory(startParameters().workingDirectory);
94
    if (startParameters().environment.size())
95
        m_gdbProc->setEnvironment(startParameters().environment.toStringList());
96

97
    if (startParameters().remoteSetupNeeded)
98
        notifyEngineRequestRemoteSetup();
99
    else
100
        startGdb();
101 102
}

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

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

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

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

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

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

172 173
    //const QByteArray sysroot = sp.sysroot.toLocal8Bit();
    //const QByteArray remoteArch = sp.remoteArchitecture.toLatin1();
174
    const QString args = isMasterEngine() ? startParameters().processArgs : masterEngine()->startParameters().processArgs;
175 176 177

//    if (!remoteArch.isEmpty())
//        postCommand("set architecture " + remoteArch);
178
    const QString solibSearchPath
hjk's avatar
hjk committed
179
            = sp.solibSearchPath.join(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,[...]
hjk's avatar
hjk committed
205
    if (boolSetting(TargetAsync))
206
        postCommand("set target-async on", CB(handleSetTargetAsync));
207

208
    if (executableFileName.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
    if (!executableFileName.isEmpty()) {
        postCommand("-file-exec-and-symbols \"" + executableFileName.toLocal8Bit() + '"',
            CB(handleFileExecAndSymbols));
    }
218 219
}

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

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

247
void GdbRemoteServerEngine::callTargetRemote()
248
{
hjk's avatar
hjk committed
249 250
    QByteArray rawChannel = startParameters().remoteChannel.toLatin1();
    QByteArray channel = rawChannel;
251 252 253

    // Don't touch channels with explicitly set protocols.
    if (!channel.startsWith("tcp:") && !channel.startsWith("udp:")
254 255
            && !channel.startsWith("file:") && channel.contains(':')
            && !channel.startsWith('|'))
256 257 258 259 260 261 262
    {
        // "Fix" the IPv6 case with host names without '['...']'
        if (!channel.startsWith('[') && channel.count(':') >= 2) {
            channel.insert(0, '[');
            channel.insert(channel.lastIndexOf(':'), ']');
        }
        channel = "tcp:" + channel;
263
    }
264

265 266
    if (m_isQnxGdb)
        postCommand("target qnx " + channel, CB(handleTargetQnx));
hjk's avatar
hjk committed
267 268
    else if (m_isMulti)
        postCommand("target extended-remote " + m_serverChannel, CB(handleTargetExtendedRemote));
269
    else
hjk's avatar
hjk committed
270
        postCommand("target remote " + channel, CB(handleTargetRemote), 10);
271 272
}

hjk's avatar
hjk committed
273
void GdbRemoteServerEngine::handleTargetRemote(const GdbResponse &response)
274
{
hjk's avatar
hjk committed
275
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
hjk's avatar
hjk committed
276
    if (response.resultClass == GdbResultDone) {
277
        // gdb server will stop the remote application itself.
278 279
        showMessage(_("INFERIOR STARTED"));
        showMessage(msgAttachedToStoppedInferior(), StatusBar);
hjk's avatar
hjk committed
280
        QString postAttachCommands = stringSetting(GdbPostAttachCommands);
281 282 283 284
        if (!postAttachCommands.isEmpty()) {
            foreach (const QString &cmd, postAttachCommands.split(QLatin1Char('\n')))
                postCommand(cmd.toLatin1());
        }
285
        handleInferiorPrepared();
286
    } else {
287
        // 16^error,msg="hd:5555: Connection timed out."
288
        QString msg = msgConnectRemoteServerFailed(
289
            QString::fromLocal8Bit(response.data["msg"].data()));
hjk's avatar
hjk committed
290 291 292 293 294 295 296 297 298 299
        notifyInferiorSetupFailed(msg);
    }
}

void GdbRemoteServerEngine::handleTargetExtendedRemote(const GdbResponse &response)
{
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
    if (response.resultClass == GdbResultDone) {
        showMessage(_("ATTACHED TO GDB SERVER STARTED"));
        showMessage(msgAttachedToStoppedInferior(), StatusBar);
hjk's avatar
hjk committed
300
        QString postAttachCommands = stringSetting(GdbPostAttachCommands);
hjk's avatar
hjk committed
301 302 303 304
        if (!postAttachCommands.isEmpty()) {
            foreach (const QString &cmd, postAttachCommands.split(QLatin1Char('\n')))
                postCommand(cmd.toLatin1());
        }
305 306 307 308 309 310 311
        if (m_targetPid > 0) { // attach to pid if valid
            // gdb server will stop the remote application itself.
            postCommand("attach " + QByteArray::number(m_targetPid), CB(handleTargetExtendedAttach));
        } else {
            postCommand("-gdb-set remote exec-file " + startParameters().remoteExecutable.toLatin1(),
                        CB(handleTargetExtendedAttach));
        }
hjk's avatar
hjk committed
312 313
    } else {
        QString msg = msgConnectRemoteServerFailed(
314
            QString::fromLocal8Bit(response.data["msg"].data()));
315
        notifyInferiorSetupFailed(msg);
316 317 318
    }
}

hjk's avatar
hjk committed
319 320 321 322 323 324 325 326
void GdbRemoteServerEngine::handleTargetExtendedAttach(const GdbResponse &response)
{
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
    if (response.resultClass == GdbResultDone) {
        // gdb server will stop the remote application itself.
        handleInferiorPrepared();
    } else {
        QString msg = msgConnectRemoteServerFailed(
327
            QString::fromLocal8Bit(response.data["msg"].data()));
328
        notifyInferiorSetupFailed(msg);
329 330 331
    }
}

hjk's avatar
hjk committed
332 333 334 335 336 337
void GdbRemoteServerEngine::notifyInferiorSetupOk()
{
    emit aboutToNotifyInferiorSetupOk();
    GdbEngine::notifyInferiorSetupOk();
}

338
void GdbRemoteServerEngine::handleTargetQnx(const GdbResponse &response)
339
{
340
    QTC_ASSERT(m_isQnxGdb, qDebug() << m_isQnxGdb);
341 342 343 344 345 346
    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
347
        const qint64 pid = isMasterEngine() ? startParameters().attachPID : masterEngine()->startParameters().attachPID;
348
        const QString remoteExecutable = isMasterEngine() ? startParameters().remoteExecutable : masterEngine()->startParameters().remoteExecutable;
349
        if (pid > -1)
350
            postCommand("attach " + QByteArray::number(pid), CB(handleAttach));
351 352
        else if (!remoteExecutable.isEmpty())
            postCommand("set nto-executable " + remoteExecutable.toLatin1(), CB(handleSetNtoExecutable));
353
        else
354
            handleInferiorPrepared();
355 356 357
    } else {
        // 16^error,msg="hd:5555: Connection timed out."
        QString msg = msgConnectRemoteServerFailed(
358
            QString::fromLocal8Bit(response.data["msg"].data()));
359
        notifyInferiorSetupFailed(msg);
360 361 362
    }
}

363
void GdbRemoteServerEngine::handleAttach(const GdbResponse &response)
364 365 366 367 368 369 370
{
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
    switch (response.resultClass) {
    case GdbResultDone:
    case GdbResultRunning: {
        showMessage(_("INFERIOR ATTACHED"));
        showMessage(msgAttachedToStoppedInferior(), StatusBar);
371
        handleInferiorPrepared();
372 373 374
        break;
    }
    case GdbResultError:
375
        if (response.data["msg"].data() == "ptrace: Operation not permitted.") {
376
            notifyInferiorSetupFailed(msgPtraceError(startParameters().startMode));
377 378 379 380
            break;
        }
        // if msg != "ptrace: ..." fall through
    default:
381
        QString msg = QString::fromLocal8Bit(response.data["msg"].data());
382
        notifyInferiorSetupFailed(msg);
383 384 385
    }
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
void GdbRemoteServerEngine::handleSetNtoExecutable(const GdbResponse &response)
{
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
    switch (response.resultClass) {
    case GdbResultDone:
    case GdbResultRunning: {
        showMessage(_("EXECUTABLE SET"));
        showMessage(msgAttachedToStoppedInferior(), StatusBar);
        handleInferiorPrepared();
        break;
    }
    case GdbResultError:
    default:
        QString msg = QString::fromLocal8Bit(response.data["msg"].data());
        notifyInferiorSetupFailed(msg);
    }

}

405
void GdbRemoteServerEngine::runEngine()
406
{
407
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
Tobias Nätterlund's avatar
Tobias Nätterlund committed
408

409
    const QString remoteExecutable = startParameters().remoteExecutable;
Tobias Nätterlund's avatar
Tobias Nätterlund committed
410
    if (!remoteExecutable.isEmpty()) {
411
        postCommand("-exec-run", GdbEngine::RunRequest, CB(handleExecRun));
Tobias Nätterlund's avatar
Tobias Nätterlund committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425
    } 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 {
426
        QString msg = QString::fromLocal8Bit(response.data["msg"].data());
Tobias Nätterlund's avatar
Tobias Nätterlund committed
427 428 429
        showMessage(msg);
        notifyEngineRunFailed();
    }
430 431
}

432
void GdbRemoteServerEngine::interruptInferior2()
433
{
434
    QTC_ASSERT(state() == InferiorStopRequested, qDebug() << state());
hjk's avatar
hjk committed
435
    if (boolSetting(TargetAsync)) {
436
        postCommand("-exec-interrupt", GdbEngine::Immediate,
437
            CB(handleInterruptInferior));
438
    } else if (m_isQnxGdb && Utils::HostOsInfo::isWindowsHost()) {
439
        m_gdbProc->winInterruptByCtrlC();
440
    } else {
441
        bool ok = m_gdbProc->interrupt();
442 443
        if (!ok) {
            // FIXME: Extra state needed?
444 445 446
            showMessage(_("NOTE: INFERIOR STOP NOT POSSIBLE"));
            showStatusMessage(tr("Interrupting not possible"));
            notifyInferiorRunOk();
447
        }
448
    }
449 450
}

451
void GdbRemoteServerEngine::handleInterruptInferior(const GdbResponse &response)
452 453 454 455 456 457 458
{
    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."
459
        notifyInferiorStopOk();
460
    }
461 462
}

463
void GdbRemoteServerEngine::shutdownEngine()
hjk's avatar
hjk committed
464
{
465
    notifyAdapterShutdownOk();
466 467
}

hjk's avatar
hjk committed
468 469 470 471 472 473 474 475 476 477
void GdbRemoteServerEngine::notifyEngineRemoteServerRunning
    (const QByteArray &serverChannel, int inferiorPid)
{
    showMessage(_("NOTE: REMOTE SERVER RUNNING IN MULTIMODE"));
    m_isMulti = true;
    m_targetPid = inferiorPid;
    m_serverChannel = serverChannel;
    startGdb();
}

478
void GdbRemoteServerEngine::notifyEngineRemoteSetupDone(int gdbServerPort, int qmlPort)
ck's avatar
ck committed
479 480
{
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
481
    DebuggerEngine::notifyEngineRemoteSetupDone(gdbServerPort, qmlPort);
ck's avatar
ck committed
482

hjk's avatar
hjk committed
483 484 485 486 487 488 489 490 491 492 493 494
    if (m_isMulti) {
        // Has been done in notifyEngineRemoteServerRunning
    } else {
        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));
            }
495
        }
hjk's avatar
hjk committed
496
        startGdb();
497
    }
498 499
}

500
void GdbRemoteServerEngine::notifyEngineRemoteSetupFailed(const QString &reason)
ck's avatar
ck committed
501 502
{
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
503
    DebuggerEngine::notifyEngineRemoteSetupFailed(reason);
504
    handleAdapterStartFailed(reason);
ck's avatar
ck committed
505 506
}

507 508
} // namespace Internal
} // namespace Debugger