2018-06-22 08:45:00 +00:00
|
|
|
#include "BreakpointWidget.h"
|
|
|
|
#include "ui_BreakpointWidget.h"
|
2018-06-26 07:38:44 +00:00
|
|
|
#include "dialogs/BreakpointsDialog.h"
|
2019-02-22 16:50:45 +00:00
|
|
|
#include "core/MainWindow.h"
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/Helpers.h"
|
2019-12-26 20:51:55 +00:00
|
|
|
#include "widgets/BoolToggleDelegate.h"
|
2018-06-22 08:45:00 +00:00
|
|
|
#include <QMenu>
|
2019-12-26 20:51:55 +00:00
|
|
|
#include <QStyledItemDelegate>
|
|
|
|
#include <QCheckBox>
|
2018-06-22 08:45:00 +00:00
|
|
|
|
2019-12-26 20:51:55 +00:00
|
|
|
BreakpointModel::BreakpointModel(QObject *parent)
|
|
|
|
: AddressableItemModel<QAbstractListModel>(parent)
|
2018-06-22 08:45:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-12-26 20:51:55 +00:00
|
|
|
void BreakpointModel::refresh()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
breakpoints = Core()->getBreakpoints();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:45:00 +00:00
|
|
|
int BreakpointModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
2019-12-26 20:51:55 +00:00
|
|
|
return breakpoints.count();
|
2018-06-22 08:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int BreakpointModel::columnCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return BreakpointModel::ColumnCount;
|
|
|
|
}
|
|
|
|
|
2020-01-04 18:05:49 +00:00
|
|
|
static QString formatHwBreakpoint(int permission) {
|
|
|
|
char data[] = "rwx";
|
|
|
|
if ((permission & (R_BP_PROT_READ | R_BP_PROT_ACCESS)) == 0) {
|
|
|
|
data[0] = '-';
|
|
|
|
}
|
|
|
|
if ((permission & (R_BP_PROT_WRITE | R_BP_PROT_ACCESS)) == 0) {
|
|
|
|
data[1] = '-';
|
|
|
|
}
|
|
|
|
if ((permission & R_BP_PROT_EXEC) == 0) {
|
|
|
|
data[2] = '-';
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:45:00 +00:00
|
|
|
QVariant BreakpointModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2019-12-26 20:51:55 +00:00
|
|
|
if (index.row() >= breakpoints.count())
|
2018-06-22 08:45:00 +00:00
|
|
|
return QVariant();
|
|
|
|
|
2019-12-26 20:51:55 +00:00
|
|
|
const BreakpointDescription &breakpoint = breakpoints.at(index.row());
|
2018-06-22 08:45:00 +00:00
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (index.column()) {
|
|
|
|
case AddrColumn:
|
|
|
|
return RAddressString(breakpoint.addr);
|
2020-01-04 18:05:49 +00:00
|
|
|
case NameColumn:
|
|
|
|
return breakpoint.name;
|
|
|
|
case TypeColumn:
|
|
|
|
|
|
|
|
if (breakpoint.hw) {
|
|
|
|
return tr("HW %1").arg(formatHwBreakpoint(breakpoint.permission));
|
|
|
|
} else {
|
|
|
|
return tr("SW");
|
|
|
|
}
|
2018-06-22 08:45:00 +00:00
|
|
|
case TraceColumn:
|
|
|
|
return breakpoint.trace;
|
|
|
|
case EnabledColumn:
|
|
|
|
return breakpoint.enabled;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
2019-12-26 20:51:55 +00:00
|
|
|
case Qt::EditRole:
|
|
|
|
switch (index.column()) {
|
2020-01-04 18:05:49 +00:00
|
|
|
case AddrColumn:
|
|
|
|
return breakpoint.addr;
|
2019-12-26 20:51:55 +00:00
|
|
|
case TraceColumn:
|
|
|
|
return breakpoint.trace;
|
|
|
|
case EnabledColumn:
|
|
|
|
return breakpoint.enabled;
|
|
|
|
default:
|
|
|
|
return data(index, Qt::DisplayRole);
|
|
|
|
}
|
2018-06-22 08:45:00 +00:00
|
|
|
case BreakpointDescriptionRole:
|
|
|
|
return QVariant::fromValue(breakpoint);
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant BreakpointModel::headerData(int section, Qt::Orientation, int role) const
|
|
|
|
{
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (section) {
|
|
|
|
case AddrColumn:
|
|
|
|
return tr("Offset");
|
2020-01-04 18:05:49 +00:00
|
|
|
case NameColumn:
|
|
|
|
return tr("Name");
|
|
|
|
case TypeColumn:
|
|
|
|
return tr("Type");
|
2018-06-22 08:45:00 +00:00
|
|
|
case TraceColumn:
|
|
|
|
return tr("Tracing");
|
|
|
|
case EnabledColumn:
|
2020-01-04 18:05:49 +00:00
|
|
|
return tr("Enabled");
|
2018-06-22 08:45:00 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 20:51:55 +00:00
|
|
|
bool BreakpointModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
if (index.row() >= breakpoints.count())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
BreakpointDescription &breakpoint = breakpoints[index.row()];
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::EditRole:
|
|
|
|
switch (index.column()) {
|
|
|
|
case TraceColumn:
|
|
|
|
breakpoint.trace = value.toBool();
|
2020-01-04 18:05:49 +00:00
|
|
|
Core()->setBreakpointTrace(breakpoint.index, breakpoint.trace);
|
2019-12-26 20:51:55 +00:00
|
|
|
emit dataChanged(index, index, {role, Qt::DisplayRole});
|
|
|
|
return true;
|
|
|
|
case EnabledColumn:
|
|
|
|
breakpoint.enabled = value.toBool();
|
|
|
|
if (breakpoint.enabled) {
|
|
|
|
Core()->enableBreakpoint(breakpoint.addr);
|
|
|
|
} else {
|
|
|
|
Core()->disableBreakpoint(breakpoint.addr);
|
|
|
|
}
|
|
|
|
emit dataChanged(index, index, {role, Qt::DisplayRole});
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags BreakpointModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
switch (index.column()) {
|
|
|
|
case TraceColumn:
|
|
|
|
return AddressableItemModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
|
|
|
|
case EnabledColumn:
|
|
|
|
return AddressableItemModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
|
|
|
|
default:
|
|
|
|
return AddressableItemModel::flags(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RVA BreakpointModel::address(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.row() < breakpoints.count()) {
|
|
|
|
return breakpoints.at(index.row()).addr;
|
|
|
|
}
|
|
|
|
return RVA_INVALID;
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:45:00 +00:00
|
|
|
BreakpointProxyModel::BreakpointProxyModel(BreakpointModel *sourceModel, QObject *parent)
|
2019-12-26 20:51:55 +00:00
|
|
|
: AddressableFilterProxyModel(sourceModel, parent)
|
2018-06-22 08:45:00 +00:00
|
|
|
{
|
2020-01-04 18:05:49 +00:00
|
|
|
// Use numeric values instead of numbers converted to strings if available
|
|
|
|
this->setSortRole(Qt::EditRole);
|
2018-06-22 08:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BreakpointWidget::BreakpointWidget(MainWindow *main, QAction *action) :
|
|
|
|
CutterDockWidget(main, action),
|
|
|
|
ui(new Ui::BreakpointWidget)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2019-12-26 20:51:55 +00:00
|
|
|
ui->breakpointTreeView->setMainWindow(mainWindow);
|
|
|
|
breakpointModel = new BreakpointModel(this);
|
2018-06-22 08:45:00 +00:00
|
|
|
breakpointProxyModel = new BreakpointProxyModel(breakpointModel, this);
|
|
|
|
ui->breakpointTreeView->setModel(breakpointProxyModel);
|
|
|
|
ui->breakpointTreeView->sortByColumn(BreakpointModel::AddrColumn, Qt::AscendingOrder);
|
2019-12-26 20:51:55 +00:00
|
|
|
ui->breakpointTreeView->setItemDelegate(new BoolTogggleDelegate(this));
|
2018-06-22 08:45:00 +00:00
|
|
|
|
2019-01-13 18:11:59 +00:00
|
|
|
refreshDeferrer = createRefreshDeferrer([this]() {
|
|
|
|
refreshBreakpoint();
|
|
|
|
});
|
|
|
|
|
2018-06-22 08:45:00 +00:00
|
|
|
setScrollMode();
|
2019-12-26 20:51:55 +00:00
|
|
|
|
2019-05-26 10:12:23 +00:00
|
|
|
actionDelBreakpoint = new QAction(tr("Delete breakpoint"), this);
|
2019-10-12 08:33:21 +00:00
|
|
|
actionDelBreakpoint->setShortcut(Qt::Key_Delete);
|
|
|
|
actionDelBreakpoint->setShortcutContext(Qt::WidgetShortcut);
|
2018-06-22 08:45:00 +00:00
|
|
|
connect(actionDelBreakpoint, &QAction::triggered, this, &BreakpointWidget::delBreakpoint);
|
2019-10-12 08:33:21 +00:00
|
|
|
ui->breakpointTreeView->addAction(actionDelBreakpoint);
|
|
|
|
|
|
|
|
actionToggleBreakpoint = new QAction(tr("Toggle breakpoint"), this);
|
|
|
|
actionToggleBreakpoint->setShortcut(Qt::Key_Space);
|
|
|
|
actionToggleBreakpoint->setShortcutContext(Qt::WidgetShortcut);
|
2018-06-22 08:45:00 +00:00
|
|
|
connect(actionToggleBreakpoint, &QAction::triggered, this, &BreakpointWidget::toggleBreakpoint);
|
2019-10-12 08:33:21 +00:00
|
|
|
ui->breakpointTreeView->addAction(actionToggleBreakpoint);
|
|
|
|
|
2020-01-04 18:05:49 +00:00
|
|
|
actionEditBreakpoint = new QAction(tr("Edit"), this);
|
|
|
|
connect(actionEditBreakpoint, &QAction::triggered, this, &BreakpointWidget::editBreakpoint);
|
|
|
|
|
2019-12-26 20:51:55 +00:00
|
|
|
auto contextMenu = ui->breakpointTreeView->getItemContextMenu();
|
2020-01-04 18:05:49 +00:00
|
|
|
contextMenu->addAction(actionEditBreakpoint);
|
2019-12-26 20:51:55 +00:00
|
|
|
contextMenu->addAction(actionToggleBreakpoint);
|
|
|
|
contextMenu->addAction(actionDelBreakpoint);
|
|
|
|
|
2018-06-22 08:45:00 +00:00
|
|
|
connect(Core(), &CutterCore::refreshAll, this, &BreakpointWidget::refreshBreakpoint);
|
|
|
|
connect(Core(), &CutterCore::breakpointsChanged, this, &BreakpointWidget::refreshBreakpoint);
|
2019-12-11 22:18:40 +00:00
|
|
|
connect(Core(), &CutterCore::codeRebased, this, &BreakpointWidget::refreshBreakpoint);
|
2018-07-01 21:29:38 +00:00
|
|
|
connect(Core(), &CutterCore::refreshCodeViews, this, &BreakpointWidget::refreshBreakpoint);
|
2018-06-26 07:38:44 +00:00
|
|
|
connect(ui->addBreakpoint, &QAbstractButton::clicked, this, &BreakpointWidget::addBreakpointDialog);
|
|
|
|
connect(ui->delBreakpoint, &QAbstractButton::clicked, this, &BreakpointWidget::delBreakpoint);
|
2018-06-22 08:45:00 +00:00
|
|
|
connect(ui->delAllBreakpoints, &QAbstractButton::clicked, Core(), &CutterCore::delAllBreakpoints);
|
|
|
|
}
|
|
|
|
|
2019-01-13 18:11:59 +00:00
|
|
|
BreakpointWidget::~BreakpointWidget() = default;
|
2018-06-22 08:45:00 +00:00
|
|
|
|
|
|
|
void BreakpointWidget::refreshBreakpoint()
|
|
|
|
{
|
2019-12-26 20:51:55 +00:00
|
|
|
if (editing || !refreshDeferrer->attemptRefresh(nullptr)) {
|
2019-01-13 18:11:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-26 20:51:55 +00:00
|
|
|
breakpointModel->refresh();
|
2018-06-22 08:45:00 +00:00
|
|
|
|
|
|
|
ui->breakpointTreeView->resizeColumnToContents(0);
|
|
|
|
ui->breakpointTreeView->resizeColumnToContents(1);
|
|
|
|
ui->breakpointTreeView->resizeColumnToContents(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BreakpointWidget::setScrollMode()
|
|
|
|
{
|
|
|
|
qhelpers::setVerticalScrollMode(ui->breakpointTreeView);
|
|
|
|
}
|
|
|
|
|
2018-06-26 07:38:44 +00:00
|
|
|
void BreakpointWidget::addBreakpointDialog()
|
|
|
|
{
|
2020-01-04 18:05:49 +00:00
|
|
|
BreakpointsDialog::createNewBreakpoint(RVA_INVALID, this);
|
2018-06-26 07:38:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 20:51:55 +00:00
|
|
|
QVector<RVA> BreakpointWidget::getSelectedAddresses() const
|
|
|
|
{
|
|
|
|
auto selection = ui->breakpointTreeView->selectionModel()->selectedRows();
|
|
|
|
QVector<RVA> breakpointAddressese(selection.count());
|
|
|
|
int index = 0;
|
|
|
|
for (auto row : selection) {
|
|
|
|
breakpointAddressese[index++] = breakpointProxyModel->address(row);
|
|
|
|
}
|
|
|
|
return breakpointAddressese;
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:45:00 +00:00
|
|
|
void BreakpointWidget::delBreakpoint()
|
|
|
|
{
|
2019-12-26 20:51:55 +00:00
|
|
|
auto breakpointsToRemove = getSelectedAddresses();
|
|
|
|
for (auto address : breakpointsToRemove) {
|
|
|
|
Core()->delBreakpoint(address);
|
|
|
|
}
|
2018-06-22 08:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BreakpointWidget::toggleBreakpoint()
|
|
|
|
{
|
2019-12-26 20:51:55 +00:00
|
|
|
auto selection = ui->breakpointTreeView->selectionModel()->selectedRows();
|
|
|
|
editing = true;
|
|
|
|
for (auto row : selection) {
|
|
|
|
auto cell = breakpointProxyModel->index(row.row(), BreakpointModel::EnabledColumn);
|
|
|
|
breakpointProxyModel->setData(cell, !cell.data(Qt::EditRole).toBool());
|
2018-06-22 08:45:00 +00:00
|
|
|
}
|
2019-12-26 20:51:55 +00:00
|
|
|
editing = false;
|
2018-10-10 11:33:55 +00:00
|
|
|
}
|
2020-01-04 18:05:49 +00:00
|
|
|
|
|
|
|
void BreakpointWidget::editBreakpoint()
|
|
|
|
{
|
|
|
|
auto index = ui->breakpointTreeView->currentIndex();
|
|
|
|
if (index.isValid()) {
|
|
|
|
auto data = breakpointProxyModel->data(index, BreakpointModel::BreakpointDescriptionRole);
|
|
|
|
if (!data.isNull()) {
|
|
|
|
auto breakpoint = data.value<BreakpointDescription>();
|
|
|
|
BreakpointsDialog::editBreakpoint(breakpoint, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|