Commit f84d44e4 authored by hjk's avatar hjk

Debugger: Merge *Adapter classes into GdbEngine

The main reason for having the adapters (complex target specific state
handling) is mostly gone now, leaving us mainly with the drawbacks
of the solution: An additional indirection, and using a hierarchy
for code sharing. So drop that, and use if/else chains instead
of virtual functions now, and start simplifying the result.

Change-Id: Idcf3a28da103c01cfa80cf9bab8ef51fe879b6d7
Reviewed-by: Christian Stenger's avatarChristian Stenger <christian.stenger@qt.io>
Reviewed-by: default avatarhjk <hjk@qt.io>
parent 991663d1
......@@ -109,14 +109,9 @@ Project {
name: "gdb"
prefix: "gdb/"
files: [
"attachgdbadapter.cpp", "attachgdbadapter.h",
"coregdbadapter.cpp", "coregdbadapter.h",
"gdbengine.cpp", "gdbengine.h",
"gdboptionspage.cpp",
"gdbplainengine.cpp", "gdbplainengine.h",
"remotegdbserveradapter.cpp", "remotegdbserveradapter.h",
"startgdbserverdialog.cpp", "startgdbserverdialog.h",
"termgdbadapter.cpp", "termgdbadapter.h"
]
}
......
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "attachgdbadapter.h"
#include <coreplugin/messagebox.h>
#include <debugger/debuggerprotocol.h>
#include <debugger/debuggerstartparameters.h>
#include <utils/qtcassert.h>
#include <utils/qtcfallthrough.h>
namespace Debugger {
namespace Internal {
GdbAttachEngine::GdbAttachEngine(bool useTerminal)
: GdbEngine(useTerminal)
{
}
void GdbAttachEngine::setupEngine()
{
QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
showMessage("TRYING TO START ADAPTER");
startGdb();
}
void GdbAttachEngine::setupInferior()
{
QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
// Task 254674 does not want to remove them
//qq->breakHandler()->removeAllBreakpoints();
handleInferiorPrepared();
}
void GdbAttachEngine::runEngine()
{
QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
const qint64 pid = runParameters().attachPID.pid();
showStatusMessage(tr("Attaching to process %1.").arg(pid));
runCommand({"attach " + QString::number(pid),
[this](const DebuggerResponse &r) { handleAttach(r); }});
// In some cases we get only output like
// "Could not attach to process. If your uid matches the uid of the target\n"
// "process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try\n"
// " again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf\n"
// " ptrace: Operation not permitted.\n"
// but no(!) ^ response. Use a second command to force *some* output
runCommand({"print 24"});
}
void GdbAttachEngine::handleAttach(const DebuggerResponse &response)
{
QTC_ASSERT(state() == EngineRunRequested || state() == InferiorStopOk,
qDebug() << state());
switch (response.resultClass) {
case ResultDone:
case ResultRunning:
showMessage("INFERIOR ATTACHED");
if (state() == EngineRunRequested) {
// Happens e.g. for "Attach to unstarted application"
// We will get a '*stopped' later that we'll interpret as 'spontaneous'
// So acknowledge the current state and put a delayed 'continue' in the pipe.
showMessage(tr("Attached to running application"), StatusBar);
notifyEngineRunAndInferiorRunOk();
} else {
// InferiorStopOk, e.g. for "Attach to running application".
// The *stopped came in between sending the 'attach' and
// receiving its '^done'.
if (runParameters().continueAfterAttach)
continueInferiorInternal();
}
break;
case ResultError:
if (response.data["msg"].data() == "ptrace: Operation not permitted.") {
QString msg = msgPtraceError(runParameters().startMode);
showStatusMessage(tr("Failed to attach to application: %1").arg(msg));
Core::AsynchronousMessageBox::warning(tr("Debugger Error"), msg);
notifyEngineIll();
break;
}
Q_FALLTHROUGH(); // if msg != "ptrace: ..."
default:
showStatusMessage(tr("Failed to attach to application: %1")
.arg(QString(response.data["msg"].data())));
notifyEngineIll();
}
}
void GdbAttachEngine::interruptInferior2()
{
interruptLocalInferior(runParameters().attachPID.pid());
}
void GdbAttachEngine::shutdownEngine()
{
notifyAdapterShutdownOk();
}
} // namespace Internal
} // namespace Debugger
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "gdbengine.h"
namespace Debugger {
namespace Internal {
///////////////////////////////////////////////////////////////////////
//
// AttachGdbAdapter
//
///////////////////////////////////////////////////////////////////////
class GdbAttachEngine : public GdbEngine
{
Q_OBJECT
public:
explicit GdbAttachEngine(bool useTerminal);
private:
void setupEngine() override;
void setupInferior() override;
void runEngine() override;
void interruptInferior2() override;
void shutdownEngine() override;
void handleAttach(const DebuggerResponse &response);
};
} // namespace Internal
} // namespace Debugger
This diff is collapsed.
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "gdbengine.h"
#include <QFile>
namespace Debugger {
namespace Internal {
class GdbCoreEngine : public GdbEngine
{
Q_OBJECT
public:
explicit GdbCoreEngine(bool useTerminal);
~GdbCoreEngine() override;
struct CoreInfo
{
QString rawStringFromCore;
QString foundExecutableName; // empty if no corresponding exec could be found
bool isCore = false;
};
static CoreInfo readExecutableNameFromCore(const ProjectExplorer::StandardRunnable &debugger,
const QString &coreFile);
private:
void setupEngine() override;
void setupInferior() override;
void runEngine() override;
void interruptInferior() override;
void shutdownEngine() override;
void handleFileExecAndSymbols(const DebuggerResponse &response);
void handleTargetCore(const DebuggerResponse &response);
void handleRoundTrip(const DebuggerResponse &response);
void unpackCoreIfNeeded();
QString coreFileName() const;
QString coreName() const;
void continueSetupEngine();
void writeCoreChunk();
private:
QString m_executable;
QString m_coreName;
QString m_tempCoreName;
QProcess *m_coreUnpackProcess = nullptr;
QFile m_tempCoreFile;
};
} // namespace Internal
} // namespace Debugger
HEADERS += \
$$PWD/gdbengine.h \
$$PWD/attachgdbadapter.h \
$$PWD/coregdbadapter.h \
$$PWD/termgdbadapter.h \
$$PWD/remotegdbserveradapter.h \
$$PWD/gdbplainengine.h \
$$PWD/startgdbserverdialog.h
SOURCES += \
$$PWD/gdbengine.cpp \
$$PWD/gdboptionspage.cpp \
$$PWD/attachgdbadapter.cpp \
$$PWD/coregdbadapter.cpp \
$$PWD/termgdbadapter.cpp \
$$PWD/remotegdbserveradapter.cpp \
$$PWD/gdbplainengine.cpp \
$$PWD/startgdbserverdialog.cpp
This diff is collapsed.
This diff is collapsed.
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "gdbplainengine.h"
#include <debugger/debuggeractions.h>
#include <debugger/debuggercore.h>
#include <debugger/debuggerprotocol.h>
#include <debugger/debuggerstartparameters.h>
#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
#include <QFileInfo>
namespace Debugger {
namespace Internal {
#define CB(callback) [this](const DebuggerResponse &r) { callback(r); }
GdbPlainEngine::GdbPlainEngine(bool useTerminal)
: GdbEngine(useTerminal)
{
// Output
connect(&m_outputCollector, &OutputCollector::byteDelivery,
this, &GdbEngine::readDebuggeeOutput);
}
void GdbPlainEngine::setupInferior()
{
QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
setEnvironmentVariables();
const DebuggerRunParameters &rp = runParameters();
if (!rp.inferior.workingDirectory.isEmpty())
runCommand({"cd " + rp.inferior.workingDirectory});
if (!rp.inferior.commandLineArguments.isEmpty()) {
QString args = rp.inferior.commandLineArguments;
runCommand({"-exec-arguments " + args});
}
QString executable = QFileInfo(runParameters().inferior.executable).absoluteFilePath();
runCommand({"-file-exec-and-symbols \"" + executable + '"',
CB(handleFileExecAndSymbols)});
}
void GdbPlainEngine::handleFileExecAndSymbols(const DebuggerResponse &response)
{
QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
if (response.resultClass == ResultDone) {
handleInferiorPrepared();
} else {
QString msg = response.data["msg"].data();
// Extend the message a bit in unknown cases.
if (!msg.endsWith("File format not recognized"))
msg = tr("Starting executable failed:") + '\n' + msg;
notifyInferiorSetupFailed(msg);
}
}
void GdbPlainEngine::runEngine()
{
if (runParameters().useContinueInsteadOfRun)
runCommand({"-exec-continue", DebuggerCommand::RunRequest, CB(handleExecuteContinue)});
else
runCommand({"-exec-run", DebuggerCommand::RunRequest, CB(handleExecRun)});
}
void GdbPlainEngine::handleExecRun(const DebuggerResponse &response)
{
QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
if (response.resultClass == ResultRunning) {
notifyEngineRunAndInferiorRunOk(); // For gdb < 7.0
//showStatusMessage(tr("Running..."));
showMessage("INFERIOR STARTED");
showMessage(msgInferiorSetupOk(), StatusBar);
// FIXME: That's the wrong place for it.
if (boolSetting(EnableReverseDebugging))
runCommand({"target record"});
} else {
QString msg = response.data["msg"].data();
//QTC_CHECK(status() == InferiorRunOk);
//interruptInferior();
showMessage(msg);
notifyEngineRunFailed();
}
}
void GdbPlainEngine::setupEngine()
{
QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
showMessage("TRYING TO START ADAPTER");
QStringList gdbArgs;
if (!m_outputCollector.listen()) {
handleAdapterStartFailed(tr("Cannot set up communication with child process: %1")
.arg(m_outputCollector.errorString()));
return;
}
gdbArgs.append("--tty=" + m_outputCollector.serverName());
startGdb(gdbArgs);
}
void GdbPlainEngine::handleGdbStartFailed()
{
m_outputCollector.shutdown();
}
void GdbPlainEngine::interruptInferior2()
{
interruptLocalInferior(inferiorPid());
}
void GdbPlainEngine::shutdownEngine()
{
showMessage(QString("PLAIN ADAPTER SHUTDOWN %1").arg(state()));
m_outputCollector.shutdown();
notifyAdapterShutdownOk();
}
} // namespace Debugger
} // namespace Internal
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "gdbengine.h"
#include <debugger/outputcollector.h>
namespace Debugger {
namespace Internal {
class GdbPlainEngine : public GdbEngine
{
// Needs tr - Context
Q_OBJECT
public:
explicit GdbPlainEngine(bool useTerminal);
private:
void handleExecRun(const DebuggerResponse &response);
void handleFileExecAndSymbols(const DebuggerResponse &response);
void setupInferior() override;
void runEngine() override;
void setupEngine() override;
void handleGdbStartFailed() override;
void interruptInferior2() override;
void shutdownEngine() override;
OutputCollector m_outputCollector;
};
} // namespace Debugger
} // namespace Internal
This diff is collapsed.
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "gdbengine.h"
namespace Debugger {
namespace Internal {
class GdbRemoteServerEngine : public GdbEngine
{
Q_OBJECT
public:
explicit GdbRemoteServerEngine(bool useTerminal);
private:
void setupEngine() override;
void setupInferior() override;
void runEngine() override;
void interruptInferior2() override;
void shutdownEngine() override;
void callTargetRemote();
void handleSetTargetAsync(const DebuggerResponse &response);
void handleFileExecAndSymbols(const DebuggerResponse &response);
void handleTargetRemote(const DebuggerResponse &response);
void handleTargetExtendedRemote(const DebuggerResponse &response);
void handleTargetExtendedAttach(const DebuggerResponse &response);
void handleTargetQnx(const DebuggerResponse &response);
void handleAttach(const DebuggerResponse &response);
void handleSetNtoExecutable(const DebuggerResponse &response);
void handleInterruptInferior(const DebuggerResponse &response);
void handleExecRun(const DebuggerResponse &response);
};
} // namespace Internal
} // namespace Debugger
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "termgdbadapter.h"
#include <debugger/debuggercore.h>
#include <debugger/debuggerprotocol.h>
#include <debugger/debuggerstartparameters.h>
#include <debugger/shared/hostutils.h>
#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
#include <coreplugin/icore.h>
#include <coreplugin/messagebox.h>
using namespace Utils;
namespace Debugger {
namespace Internal {
///////////////////////////////////////////////////////////////////////
//
// TermGdbAdapter
//
///////////////////////////////////////////////////////////////////////
GdbTermEngine::GdbTermEngine(bool useTerminal)
: GdbEngine(useTerminal)
{
if (HostOsInfo::isWindowsHost()) {
// Windows up to xp needs a workaround for attaching to freshly started processes. see proc_stub_win
if (QSysInfo::WindowsVersion >= QSysInfo::WV_VISTA)
m_stubProc.setMode(ConsoleProcess::Suspend);
else
m_stubProc.setMode(ConsoleProcess::Debug);
} else {
m_stubProc.setMode(ConsoleProcess::Debug);
m_stubProc.setSettings(Core::ICore::settings());
}
}
GdbTermEngine::~GdbTermEngine()
{