Commit 1d64e169 authored by hjk's avatar hjk
Browse files

debugger: rework startup logic

parent ae2cfdaa
...@@ -33,6 +33,8 @@ ...@@ -33,6 +33,8 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QProcess> #include <QtCore/QProcess>
#include "gdbengine.h"
namespace Debugger { namespace Debugger {
namespace Internal { namespace Internal {
...@@ -48,15 +50,10 @@ class AbstractGdbAdapter : public QObject ...@@ -48,15 +50,10 @@ class AbstractGdbAdapter : public QObject
Q_OBJECT Q_OBJECT
public: public:
AbstractGdbAdapter(QObject *parent = 0) : QObject(parent) {} AbstractGdbAdapter(GdbEngine *engine, QObject *parent = 0)
: QObject(parent), m_engine(engine)
virtual void setEngine(GdbEngine *engine) { m_engine = engine; } {}
virtual void start(const QString &program, const QStringList &args,
QIODevice::OpenMode mode = QIODevice::ReadWrite) = 0;
virtual void kill() = 0;
virtual void terminate() = 0;
//virtual bool waitForStarted(int msecs = 30000) = 0;
virtual bool waitForFinished(int msecs = 30000) = 0;
virtual QProcess::ProcessState state() const = 0; virtual QProcess::ProcessState state() const = 0;
virtual QString errorString() const = 0; virtual QString errorString() const = 0;
virtual QByteArray readAllStandardError() = 0; virtual QByteArray readAllStandardError() = 0;
...@@ -66,18 +63,35 @@ public: ...@@ -66,18 +63,35 @@ public:
virtual void setEnvironment(const QStringList &env) = 0; virtual void setEnvironment(const QStringList &env) = 0;
virtual bool isAdapter() const = 0; virtual bool isAdapter() const = 0;
virtual void attach() = 0; virtual void startAdapter(const DebuggerStartParametersPtr &sp) = 0;
virtual void prepareInferior() = 0;
virtual void startInferior() = 0;
virtual void shutdownInferior() = 0;
virtual void shutdownAdapter() = 0;
virtual void interruptInferior() = 0; virtual void interruptInferior() = 0;
signals: signals:
void adapterStarted();
void adapterStartFailed(const QString &msg);
void adapterShutDown();
void adapterShutdownFailed(const QString &msg);
void adapterCrashed();
void inferiorPrepared();
void inferiorPreparationFailed(const QString &msg);
void inferiorStarted();
void inferiorStartFailed(const QString &msg);
void inferiorShutDown();
void inferiorShutdownFailed(const QString &msg);
void inferiorPidChanged(qint64 pid);
void error(QProcess::ProcessError); void error(QProcess::ProcessError);
void started();
void readyReadStandardOutput(); void readyReadStandardOutput();
void readyReadStandardError(); void readyReadStandardError();
void finished(int, QProcess::ExitStatus);
protected: protected:
GdbEngine *m_engine; GdbEngine * const m_engine;
}; };
} // namespace Internal } // namespace Internal
......
...@@ -2,6 +2,7 @@ include(../../../shared/trk/trk.pri) ...@@ -2,6 +2,7 @@ include(../../../shared/trk/trk.pri)
HEADERS += \ HEADERS += \
$$PWD/abstractgdbadapter.h \ $$PWD/abstractgdbadapter.h \
$$PWD/plaingdbadapter.h \
$$PWD/gdbmi.h \ $$PWD/gdbmi.h \
$$PWD/gdbengine.h \ $$PWD/gdbengine.h \
$$PWD/gdboptionspage.h \ $$PWD/gdboptionspage.h \
...@@ -14,10 +15,11 @@ SOURCES += \ ...@@ -14,10 +15,11 @@ SOURCES += \
$$PWD/gdbmi.cpp \ $$PWD/gdbmi.cpp \
$$PWD/gdbengine.cpp \ $$PWD/gdbengine.cpp \
$$PWD/gdboptionspage.cpp \ $$PWD/gdboptionspage.cpp \
$$PWD/trkgdbadapter.cpp \ $$PWD/plaingdbadapter.cpp \
$$PWD/trkoptions.cpp \ $$PWD/trkoptions.cpp \
$$PWD/trkoptionswidget.cpp \ $$PWD/trkoptionswidget.cpp \
$$PWD/trkoptionspage.cpp $$PWD/trkoptionspage.cpp \
$$PWD/trkgdbadapter.cpp
FORMS += $$PWD/gdboptionspage.ui \ FORMS += $$PWD/gdboptionspage.ui \
$$PWD/trkoptionswidget.ui $$PWD/trkoptionswidget.ui
......
This diff is collapsed.
...@@ -33,11 +33,8 @@ ...@@ -33,11 +33,8 @@
#include "idebuggerengine.h" #include "idebuggerengine.h"
#include "debuggermanager.h" // only for StartParameters #include "debuggermanager.h" // only for StartParameters
#include "gdbmi.h" #include "gdbmi.h"
#include "abstractgdbadapter.h"
#include "outputcollector.h"
#include "watchutils.h" #include "watchutils.h"
#include "outputcollector.h"
#include <consoleprocess.h>
#include <QtCore/QByteArray> #include <QtCore/QByteArray>
#include <QtCore/QHash> #include <QtCore/QHash>
...@@ -45,6 +42,7 @@ ...@@ -45,6 +42,7 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QProcess> #include <QtCore/QProcess>
#include <QtCore/QPoint> #include <QtCore/QPoint>
#include <QtCore/QSet>
#include <QtCore/QTextCodec> #include <QtCore/QTextCodec>
#include <QtCore/QTime> #include <QtCore/QTime>
#include <QtCore/QVariant> #include <QtCore/QVariant>
...@@ -58,7 +56,7 @@ QT_END_NAMESPACE ...@@ -58,7 +56,7 @@ QT_END_NAMESPACE
namespace Debugger { namespace Debugger {
namespace Internal { namespace Internal {
class AbstractGdbAdapter;
class DebuggerManager; class DebuggerManager;
class IDebuggerManagerAccessForEngines; class IDebuggerManagerAccessForEngines;
class GdbResultRecord; class GdbResultRecord;
...@@ -75,52 +73,14 @@ enum DebuggingHelperState ...@@ -75,52 +73,14 @@ enum DebuggingHelperState
DebuggingHelperUnavailable, DebuggingHelperUnavailable,
}; };
class PlainGdbAdapter : public AbstractGdbAdapter
{
public:
PlainGdbAdapter(QObject *parent = 0)
: AbstractGdbAdapter(parent)
{
connect(&m_proc, SIGNAL(error(QProcess::ProcessError)),
this, SIGNAL(error(QProcess::ProcessError)));
connect(&m_proc, SIGNAL(readyReadStandardOutput()),
this, SIGNAL(readyReadStandardOutput()));
connect(&m_proc, SIGNAL(readyReadStandardError()),
this, SIGNAL(readyReadStandardError()));
connect(&m_proc, SIGNAL(started()),
this, SIGNAL(started()));
connect(&m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SIGNAL(finished(int, QProcess::ExitStatus)));
}
void start(const QString &program, const QStringList &args,
QIODevice::OpenMode mode) { m_proc.start(program, args, mode); }
void kill() { m_proc.kill(); }
void terminate() { m_proc.terminate(); }
bool waitForStarted(int msecs) { return m_proc.waitForStarted(msecs); }
bool waitForFinished(int msecs) { return m_proc.waitForFinished(msecs); }
QProcess::ProcessState state() const { return m_proc.state(); }
QString errorString() const { return m_proc.errorString(); }
QByteArray readAllStandardError() { return m_proc.readAllStandardError(); }
QByteArray readAllStandardOutput() { return m_proc.readAllStandardOutput(); }
qint64 write(const char *data) { return m_proc.write(data); }
void setWorkingDirectory(const QString &dir) { m_proc.setWorkingDirectory(dir); }
void setEnvironment(const QStringList &env) { m_proc.setEnvironment(env); }
bool isAdapter() const { return false; }
void attach();
void interruptInferior();
private:
QProcess m_proc;
};
class GdbEngine : public IDebuggerEngine class GdbEngine : public IDebuggerEngine
{ {
Q_OBJECT Q_OBJECT
public: public:
GdbEngine(DebuggerManager *parent, AbstractGdbAdapter *gdbAdapter); explicit GdbEngine(DebuggerManager *parent);
~GdbEngine(); ~GdbEngine();
void setGdbAdapter(AbstractGdbAdapter *adapter);
signals: signals:
void gdbInputAvailable(int channel, const QString &msg); void gdbInputAvailable(int channel, const QString &msg);
...@@ -145,7 +105,6 @@ private: ...@@ -145,7 +105,6 @@ private:
void shutdown(); void shutdown();
void setToolTipExpression(const QPoint &mousePos, TextEditor::ITextEditor *editor, int cursorPos); void setToolTipExpression(const QPoint &mousePos, TextEditor::ITextEditor *editor, int cursorPos);
void startDebugger(const DebuggerStartParametersPtr &sp); void startDebugger(const DebuggerStartParametersPtr &sp);
Q_SLOT void startDebugger2();
void exitDebugger(); void exitDebugger();
void exitDebugger2(); void exitDebugger2();
void detachDebugger(); void detachDebugger();
...@@ -217,14 +176,20 @@ public: // otherwise the Qt flag macros are unhappy ...@@ -217,14 +176,20 @@ public: // otherwise the Qt flag macros are unhappy
private: private:
typedef void (GdbEngine::*GdbCommandCallback)(const GdbResultRecord &record, const QVariant &cookie); typedef void (GdbEngine::*GdbCommandCallback)
(const GdbResultRecord &record, const QVariant &cookie);
typedef void (AbstractGdbAdapter::*AdapterCallback)
(const GdbResultRecord &record, const QVariant &cookie);
struct GdbCommand struct GdbCommand
{ {
GdbCommand() : flags(0), callback(0), callbackName(0) {} GdbCommand()
: flags(0), callback(0), adapterCallback(0), callbackName(0)
{}
int flags; int flags;
GdbCommandCallback callback; GdbCommandCallback callback;
AdapterCallback adapterCallback;
const char *callbackName; const char *callbackName;
QString command; QString command;
QVariant cookie; QVariant cookie;
...@@ -235,7 +200,7 @@ private: ...@@ -235,7 +200,7 @@ private:
// queue". resultNeeded == true increments m_pendingResults on // queue". resultNeeded == true increments m_pendingResults on
// send and decrements on receipt, effectively preventing // send and decrements on receipt, effectively preventing
// watch model updates before everything is finished. // watch model updates before everything is finished.
void flushCommand(GdbCommand &cmd); void flushCommand(const GdbCommand &cmd);
void postCommand(const QString &command, void postCommand(const QString &command,
GdbCommandFlags flags, GdbCommandFlags flags,
GdbCommandCallback callback = 0, GdbCommandCallback callback = 0,
...@@ -245,7 +210,11 @@ private: ...@@ -245,7 +210,11 @@ private:
GdbCommandCallback callback = 0, GdbCommandCallback callback = 0,
const char *callbackName = 0, const char *callbackName = 0,
const QVariant &cookie = QVariant()); const QVariant &cookie = QVariant());
void postCommand(const QString &command,
AdapterCallback callback,
const char *callbackName,
const QVariant &cookie = QVariant());
void postCommandHelper(const GdbCommand &cmd);
void setTokenBarrier(); void setTokenBarrier();
void updateLocals(); void updateLocals();
...@@ -257,25 +226,36 @@ private slots: ...@@ -257,25 +226,36 @@ private slots:
void readUploadStandardOutput(); void readUploadStandardOutput();
void readUploadStandardError(); void readUploadStandardError();
void readDebugeeOutput(const QByteArray &data); void readDebugeeOutput(const QByteArray &data);
void stubStarted();
void stubError(const QString &msg);
void uploadProcError(QProcess::ProcessError error); void uploadProcError(QProcess::ProcessError error);
void emitStartFailed(); void emitStartFailed();
void handleAdapterStarted();
void handleAdapterStartFailed(const QString &msg);
void handleInferiorPrepared();
void handleInferiorPreparationFailed(const QString &msg);
void handleInferiorStarted();
void handleInferiorStartFailed(const QString &msg);
void handleInferiorShutDown();
void handleInferiorShutdownFailed(const QString &msg);
void handleAdapterShutDown();
void handleAdapterShutdownFailed(const QString &msg);
private: private:
int terminationIndex(const QByteArray &buffer, int &length); int terminationIndex(const QByteArray &buffer, int &length);
void handleResponse(const QByteArray &buff); void handleResponse(const QByteArray &buff);
void handleStart(const GdbResultRecord &response, const QVariant &); void handleStart(const GdbResultRecord &response, const QVariant &);
void handleAttach(const GdbResultRecord &, const QVariant &); void handleAttach(const GdbResultRecord &, const QVariant &);
void handleStubAttached(const GdbResultRecord &, const QVariant &);
void handleAqcuiredInferior(); void handleAqcuiredInferior();
void handleAsyncOutput2(const GdbResultRecord &, const QVariant &cookie);
void handleAsyncOutput2(const GdbMi &data);
void handleAsyncOutput(const GdbMi &data); void handleAsyncOutput(const GdbMi &data);
void handleStop1(const GdbResultRecord &, const QVariant &cookie);
void handleStop2(const GdbResultRecord &, const QVariant &cookie);
void handleStop2(const GdbMi &data);
void handleResultRecord(const GdbResultRecord &response); void handleResultRecord(const GdbResultRecord &response);
void handleFileExecAndSymbols(const GdbResultRecord &response, const QVariant &); void handleFileExecAndSymbols(const GdbResultRecord &response, const QVariant &);
void handleExecContinue(const GdbResultRecord &response, const QVariant &); void handleExecContinue(const GdbResultRecord &response, const QVariant &);
void handleExecRun(const GdbResultRecord &response, const QVariant &); //void handleExecRun(const GdbResultRecord &response, const QVariant &);
void handleExecJumpToLine(const GdbResultRecord &response, const QVariant &); void handleExecJumpToLine(const GdbResultRecord &response, const QVariant &);
void handleExecRunToFunction(const GdbResultRecord &response, const QVariant &); void handleExecRunToFunction(const GdbResultRecord &response, const QVariant &);
void handleInfoShared(const GdbResultRecord &response, const QVariant &); void handleInfoShared(const GdbResultRecord &response, const QVariant &);
...@@ -306,7 +286,6 @@ private: ...@@ -306,7 +286,6 @@ private:
QList<WatchData> *insertions); QList<WatchData> *insertions);
const bool m_dumperInjectionLoad; const bool m_dumperInjectionLoad;
OutputCollector m_outputCollector;
QTextCodec *m_outputCodec; QTextCodec *m_outputCodec;
QTextCodec::ConverterState m_outputCodecState; QTextCodec::ConverterState m_outputCodecState;
...@@ -315,8 +294,6 @@ private: ...@@ -315,8 +294,6 @@ private:
AbstractGdbAdapter *m_gdbAdapter; AbstractGdbAdapter *m_gdbAdapter;
QProcess m_uploadProc; QProcess m_uploadProc;
Core::Utils::ConsoleProcess m_stubProc;
QHash<int, GdbCommand> m_cookieForToken; QHash<int, GdbCommand> m_cookieForToken;
QHash<int, QByteArray> m_customOutputForToken; QHash<int, QByteArray> m_customOutputForToken;
...@@ -448,7 +425,11 @@ private: ...@@ -448,7 +425,11 @@ private:
QString m_currentFrame; QString m_currentFrame;
QMap<QString, QString> m_varToType; QMap<QString, QString> m_varToType;
bool m_autoContinue; typedef void (GdbEngine::*Continuation)();
// function called after all previous responses have been received
Continuation m_continuationAfterDone;
void handleInitialBreakpointsSet();
bool m_waitingForFirstBreakpointToBeHit; bool m_waitingForFirstBreakpointToBeHit;
bool m_modulesListOutdated; bool m_modulesListOutdated;
...@@ -458,6 +439,8 @@ private: ...@@ -458,6 +439,8 @@ private:
IDebuggerManagerAccessForEngines * const qq; IDebuggerManagerAccessForEngines * const qq;
DebuggerStartParametersPtr m_startParameters; DebuggerStartParametersPtr m_startParameters;
// make sure to re-initialize new members in initializeVariables(); // make sure to re-initialize new members in initializeVariables();
public:
OutputCollector m_outputCollector;
}; };
} // namespace Internal } // namespace Internal
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** 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.
**
**************************************************************************/
#include "plaingdbadapter.h"
#include "debuggeractions.h"
#include "gdbengine.h"
#include "procinterrupt.h"
#include <utils/qtcassert.h>
#include <coreplugin/icore.h>
#include <QtCore/QFileInfo>
#include <QtGui/QMessageBox>
namespace Debugger {
namespace Internal {
#define STRINGIFY_INTERNAL(x) #x
#define STRINGIFY(x) STRINGIFY_INTERNAL(x)
#define CB(callback) \
static_cast<GdbEngine::AdapterCallback>(&PlainGdbAdapter::callback), \
STRINGIFY(callback)
///////////////////////////////////////////////////////////////////////
//
// PlainGdbAdapter
//
///////////////////////////////////////////////////////////////////////
PlainGdbAdapter::PlainGdbAdapter(GdbEngine *engine, QObject *parent)
: AbstractGdbAdapter(engine, parent)
{
connect(&m_gdbProc, SIGNAL(error(QProcess::ProcessError)),
this, SIGNAL(error(QProcess::ProcessError)));
connect(&m_gdbProc, SIGNAL(readyReadStandardOutput()),
this, SIGNAL(readyReadStandardOutput()));
connect(&m_gdbProc, SIGNAL(readyReadStandardError()),
this, SIGNAL(readyReadStandardError()));
connect(&m_gdbProc, SIGNAL(started()),
this, SIGNAL(adapterStarted()));
connect(&m_gdbProc, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(handleFinished(int, QProcess::ExitStatus)));
m_stubProc.setMode(Core::Utils::ConsoleProcess::Debug);
#ifdef Q_OS_UNIX
m_stubProc.setSettings(Core::ICore::instance()->settings());
#endif
connect(&m_stubProc, SIGNAL(processError(QString)),
this, SLOT(stubError(QString)));
connect(&m_stubProc, SIGNAL(processStarted()),
this, SLOT(stubStarted()));
// FIXME:
// connect(&m_stubProc, SIGNAL(wrapperStopped()),
// m_manager, SLOT(exitDebugger()));
}
void PlainGdbAdapter::startAdapter(const DebuggerStartParametersPtr &sp)
{
debugMessage(_("TRYING TO START ADAPTER"));
m_startParameters = sp;
QStringList gdbArgs;
gdbArgs.prepend(_("mi"));
gdbArgs.prepend(_("-i"));
if (m_startParameters->useTerminal) {
m_stubProc.stop(); // We leave the console open, so recycle it now.
m_stubProc.setWorkingDirectory(m_startParameters->workingDir);
m_stubProc.setEnvironment(m_startParameters->environment);
if (!m_stubProc.start(m_startParameters->executable,
m_startParameters->processArgs)) {
// Error message for user is delivered via a signal.
emitAdapterStartFailed(QString());
return;
}
} else {
if (!m_engine->m_outputCollector.listen()) {
emitAdapterStartFailed(tr("Cannot set up communication with child process: %1")
.arg(m_engine->m_outputCollector.errorString()));
return;
}
gdbArgs.prepend(_("--tty=") + m_engine->m_outputCollector.serverName());
if (!m_startParameters->workingDir.isEmpty())
setWorkingDirectory(m_startParameters->workingDir);
if (!m_startParameters->environment.isEmpty())
setEnvironment(m_startParameters->environment);
}
QString location = theDebuggerStringSetting(GdbLocation);
//showStatusMessage(tr("Starting Debugger: ") + loc + _c(' ') + gdbArgs.join(_(" ")));
m_gdbProc.start(location, gdbArgs);
}
void PlainGdbAdapter::prepareInferior()
{
if (!m_startParameters->processArgs.isEmpty())
m_engine->postCommand(_("-exec-arguments ") + m_startParameters->processArgs.join(_(" ")));
QFileInfo fi(m_engine->startParameters().executable);
m_engine->postCommand(_("-file-exec-and-symbols \"%1\"").arg(fi.absoluteFilePath()),
CB(handleFileExecAndSymbols));
}
void PlainGdbAdapter::handleFileExecAndSymbols(const GdbResultRecord &response, const QVariant &)
{
if (response.resultClass == GdbResultDone) {
//m_breakHandler->clearBreakMarkers();
emit inferiorPrepared();
} else if (response.resultClass == GdbResultError) {
QString msg = tr("Starting executable failed:\n") +
__(response.data.findChild("msg").data());
emit inferiorPreparationFailed(msg);
}
}
void PlainGdbAdapter::startInferior()
{
m_engine->postCommand(_("-exec-run"), CB(handleExecRun));
/*
#ifdef Q_OS_MAC
m_engine->postCommand(_("sharedlibrary apply-load-rules all"));
// On MacOS, breaking in at the entry point wreaks havoc.
m_engine->postCommand(_("tbreak main"));
m_waitingForFirstBreakpointToBeHit = true;
m_engine->postCommand(_("-exec-run"), CB(handleExecRun));
#else
// FIXME:
// if (!m_dumperInjectionLoad)
// m_engine->postCommand(_("set auto-solib-add off"));
m_engine->postCommand(_("info target"), CB(handleInfoTarget));
#endif
*/
}
void PlainGdbAdapter::handleInfoTarget(const GdbResultRecord &response, const QVariant &)
{
#if defined(Q_OS_MAC)
Q_UNUSED(response)
#else
if (response.resultClass == GdbResultDone) {
// [some leading stdout here]
// >&" Entry point: 0x80831f0 0x08048134 - 0x08048147 is .interp\n"
// [some trailing stdout here]
QString msg = _(response.data.findChild("consolestreamoutput").data());
QRegExp needle(_("\\bEntry point: (0x[0-9a-f]+)\\b"));
if (needle.indexIn(msg) != -1) {
//debugMessage(_("STREAM: ") + msg + " " + needle.cap(1));
m_engine->postCommand(_("tbreak *") + needle.cap(1));
// FIXME: m_waitingForFirstBreakpointToBeHit = true;
m_engine->postCommand(_("-exec-run"), CB(handleExecRun));
} else {
debugMessage(_("PARSING START ADDRESS FAILED: ") + msg);
emit inferiorStartFailed(_("Parsing start address failed"));
}
} else if (response.resultClass == GdbResultError) {
debugMessage(_("FETCHING START ADDRESS FAILED: " + response.toString()));
emit inferiorStartFailed(_("Fetching start address failed"));
}
#endif