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 "utils.h"
#include "mainwindow.h"
#include "editor.h"
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
@ -22,7 +93,41 @@ const BacktraceModel* Debugger::getBacktraceModel() const
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
{
return mList.size();
@ -877,6 +987,25 @@ void BreakpointModel::removeBreakpoint(int row)
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
{
@ -952,3 +1081,8 @@ void BacktraceModel::removeTrace(int row)
mList.removeAt(row);
endRemoveRows();
}
const QList<PTrace> &BacktraceModel::backtraces() const
{
return mList;
}

View File

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