work save

This commit is contained in:
royqh1979 2021-07-24 11:18:25 +08:00
parent cf4e7ae1c6
commit 8b2dd308e3
2 changed files with 174 additions and 12 deletions

View File

@ -1,10 +1,81 @@
#include "debugger.h" #include "debugger.h"
#include "utils.h" #include "utils.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "editor.h"
Debugger::Debugger(QObject *parent) : QObject(parent) Debugger::Debugger(QObject *parent) : QObject(parent)
{ {
mBreakpointModel=new BreakpointModel(this);
mBacktraceModel=new BacktraceModel(this);
}
void Debugger::sendCommand(const QString &command, const QString &params, bool updateWatch, bool showInConsole, DebugCommandSource source)
{
if (mExecuting && mReader) {
mReader->postCommand(command,params,updateWatch,showInConsole,source);
}
}
void Debugger::addBreakpoint(int line, const Editor* editor)
{
addBreakpoint(line,editor->filename());
}
void Debugger::addBreakpoint(int line, const QString &filename)
{
PBreakpoint bp=std::make_shared<Breakpoint>();
bp->line = line;
bp->filename = filename;
bp->condition = "";
mBreakpointModel->addBreakpoint(bp);
}
void Debugger::deleteBreakpoints(const QString &filename)
{
for (int i=mBreakpointModel->breakpoints().size()-1;i>=0;i--) {
PBreakpoint bp = mBreakpointModel->breakpoints()[i];
if (bp->filename == filename) {
mBreakpointModel->removeBreakpoint(i);
}
}
}
void Debugger::deleteBreakpoints(const Editor *editor)
{
deleteBreakpoints(editor->filename());
}
void Debugger::removeBreakpoint(int line, const Editor *editor)
{
removeBreakpoint(line,editor->filename());
}
void Debugger::removeBreakpoint(int line, const QString &filename)
{
for (int i=mBreakpointModel->breakpoints().size()-1;i>=0;i--) {
PBreakpoint bp = mBreakpointModel->breakpoints()[i];
if (bp->filename == filename && bp->line == line) {
removeBreakpoint(i);
}
}
}
void Debugger::removeBreakpoint(int index)
{
sendClearBreakpointCommand(index);
mBreakpointModel->removeBreakpoint(index);
}
void Debugger::setBreakPointCondition(int index, const QString &condition)
{
PBreakpoint breakpoint=mBreakpointModel->setBreakPointCondition(index,condition);
if (condition.isEmpty()) {
sendCommand("cond",
QString("%1").arg(breakpoint->line));
} else {
sendCommand("cond",
QString("%1 %2").arg(breakpoint->line).arg(condition));
}
} }
bool Debugger::useUTF8() const bool Debugger::useUTF8() const
@ -22,7 +93,41 @@ const BacktraceModel* Debugger::getBacktraceModel() const
return mBacktraceModel; return mBacktraceModel;
} }
DebugReader::DebugReader(QObject *parent) : QObject(parent) const BreakpointModel *Debugger::getBreakpointModel() const
{
return mBreakpointModel;
}
void Debugger::sendBreakpointCommand(int index)
{
// break "filename":linenum
PBreakpoint breakpoint = mBreakpointModel->breakpoints()[index];
QString condition;
if (!breakpoint->condition.isEmpty()) {
condition = " if " + breakpoint->condition;
}
QString filename = breakpoint->filename;
filename.replace('\\','/');
sendCommand("break",
QString("\"%1\":%2").arg(filename)
.arg(breakpoint->line)+condition);
}
void Debugger::sendClearBreakpointCommand(int index)
{
// Debugger already running? Remove it from GDB
if (mExecuting) {
//clear "filename":linenum
PBreakpoint breakpoint = mBreakpointModel->breakpoints()[index];
QString filename = breakpoint->filename;
filename.replace('\\','/');
sendCommand("clear",
QString("\"%1\":%2").arg(filename)
.arg(breakpoint->line));
}
}
DebugReader::DebugReader(QObject *parent) : QThread(parent)
{ {
} }
@ -802,6 +907,11 @@ void DebugReader::run()
BreakpointModel::BreakpointModel(QObject *parent):QAbstractTableModel(parent)
{
}
int BreakpointModel::rowCount(const QModelIndex &) const int BreakpointModel::rowCount(const QModelIndex &) const
{ {
return mList.size(); return mList.size();
@ -877,6 +987,25 @@ void BreakpointModel::removeBreakpoint(int row)
endRemoveRows(); endRemoveRows();
} }
PBreakpoint BreakpointModel::setBreakPointCondition(int index, const QString &condition)
{
beginResetModel();
PBreakpoint breakpoint = mList[index];
breakpoint->condition = condition;
endResetModel();
return breakpoint;
}
const QList<PBreakpoint> &BreakpointModel::breakpoints() const
{
return mList;
}
BacktraceModel::BacktraceModel(QObject *parent):QAbstractTableModel(parent)
{
}
int BacktraceModel::rowCount(const QModelIndex &) const int BacktraceModel::rowCount(const QModelIndex &) const
{ {
@ -952,3 +1081,8 @@ void BacktraceModel::removeTrace(int row)
mList.removeAt(row); mList.removeAt(row);
endRemoveRows(); endRemoveRows();
} }
const QList<PTrace> &BacktraceModel::backtraces() const
{
return mList;
}

View File

@ -55,10 +55,9 @@ struct WatchVar {
using PWatchVar = std::shared_ptr<WatchVar>; using PWatchVar = std::shared_ptr<WatchVar>;
class Editor;
struct Breakpoint { struct Breakpoint {
int line; int line;
Editor * editor; QString filename;
QString condition; QString condition;
}; };
@ -81,15 +80,19 @@ struct Register {
using PRegister = std::shared_ptr<Register>; using PRegister = std::shared_ptr<Register>;
class BreakpointModel: public QAbstractTableModel { class BreakpointModel: public QAbstractTableModel {
Q_OBJECT
// QAbstractItemModel interface // QAbstractItemModel interface
public: public:
explicit BreakpointModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
void addBreakpoint(PBreakpoint p); void addBreakpoint(PBreakpoint p);
void clear(); void clear();
void removeBreakpoint(int row); void removeBreakpoint(int index);
PBreakpoint setBreakPointCondition(int index, const QString& condition);
const QList<PBreakpoint>& breakpoints() const;
private: private:
QList<PBreakpoint> mList; QList<PBreakpoint> mList;
}; };
@ -97,44 +100,69 @@ private:
class BacktraceModel : public QAbstractTableModel { class BacktraceModel : public QAbstractTableModel {
// QAbstractItemModel interface // QAbstractItemModel interface
public: public:
explicit BacktraceModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
void addTrace(PTrace p); void addTrace(PTrace p);
void clear(); void clear();
void removeTrace(int row); void removeTrace(int index);
const QList<PTrace>& backtraces() const;
private: private:
QList<PTrace> mList; QList<PTrace> mList;
}; };
class DebugReader; class DebugReader;
class Editor;
using PDebugReader = std::shared_ptr<DebugReader>;
class Debugger : public QObject class Debugger : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit Debugger(QObject *parent = nullptr); explicit Debugger(QObject *parent = nullptr);
void addBreakpoint(int line); // Play/pause
void sendBreakpointToDebugger(int index); void start();
void stop();
void sendCommand(const QString& command, const QString& params,
bool updateWatch = true,
bool showInConsole = false,
DebugCommandSource source = DebugCommandSource::Other);
void addBreakpoint(int line, const Editor* editor);
void addBreakpoint(int line, const QString& filename);
void deleteBreakpoints(const QString& filename);
void deleteBreakpoints(const Editor* editor);
void removeBreakpoint(int line, const Editor* editor);
void removeBreakpoint(int line, const QString& filename);
void removeBreakpoint(int index);
void setBreakPointCondition(int index, const QString& condition);
bool useUTF8() const; bool useUTF8() const;
void setUseUTF8(bool useUTF8); void setUseUTF8(bool useUTF8);
const BacktraceModel* getBacktraceModel() const; const BacktraceModel *getBacktraceModel() const;
const BreakpointModel* getBreakpointModel() const;
signals: signals:
private:
void sendBreakpointCommand(int index);
void sendClearBreakpointCommand(int index);
private: private:
bool mExecuting; bool mExecuting;
bool mCommandChanged; bool mCommandChanged;
QList<PBreakpoint> mBreakpointList; BreakpointModel* mBreakpointModel;
bool mUseUTF8; bool mUseUTF8;
QString getBreakpointFile(); BacktraceModel* mBacktraceModel;
BacktraceModel mBacktraceModel; PDebugReader mReader;
friend class DebugReader; friend class DebugReader;
}; };
class DebugReader : public QThread class DebugReader : public QThread