rpcsx/rpcs3/rpcs3qt/breakpoint_list.cpp

251 lines
6 KiB
C++
Raw Normal View History

2020-12-05 13:08:24 +01:00
#include "breakpoint_list.h"
2020-02-22 20:42:49 +01:00
#include "breakpoint_handler.h"
2020-02-22 20:42:49 +01:00
#include "Emu/CPU/CPUDisAsm.h"
#include "Emu/Cell/PPUThread.h"
2021-10-15 01:26:51 +02:00
#include "Emu/Cell/SPUThread.h"
#include <QMenu>
#include <QMessageBox>
#include <QMouseEvent>
extern bool is_using_interpreter(thread_class t_class);
2021-10-15 01:26:51 +02:00
breakpoint_list::breakpoint_list(QWidget* parent, breakpoint_handler* handler) : QListWidget(parent), m_ppu_breakpoint_handler(handler)
{
setEditTriggers(QAbstractItemView::NoEditTriggers);
setContextMenuPolicy(Qt::CustomContextMenu);
setSelectionMode(QAbstractItemView::ExtendedSelection);
connect(this, &QListWidget::itemDoubleClicked, this, &breakpoint_list::OnBreakpointListDoubleClicked);
connect(this, &QListWidget::customContextMenuRequested, this, &breakpoint_list::OnBreakpointListRightClicked);
2021-03-23 23:23:37 +01:00
m_delete_action = new QAction(tr("&Delete"), this);
m_delete_action->setShortcut(Qt::Key_Delete);
m_delete_action->setShortcutContext(Qt::WidgetShortcut);
connect(m_delete_action, &QAction::triggered, this, &breakpoint_list::OnBreakpointListDelete);
addAction(m_delete_action);
// Hide until used in order to allow as much space for registers panel as possible
hide();
}
/**
* It's unfortunate I need a method like this to sync these. Should ponder a cleaner way to do this.
*/
2024-08-21 18:41:55 +02:00
void breakpoint_list::UpdateCPUData(std::shared_ptr<CPUDisAsm> disasm)
{
2024-08-21 18:41:55 +02:00
m_disasm = std::move(disasm);
}
void breakpoint_list::ClearBreakpoints()
{
while (count())
{
auto* currentItem = takeItem(0);
2021-04-07 23:05:18 +02:00
const u32 loc = currentItem->data(Qt::UserRole).value<u32>();
2021-10-15 01:26:51 +02:00
m_ppu_breakpoint_handler->RemoveBreakpoint(loc);
delete currentItem;
}
hide();
}
void breakpoint_list::RemoveBreakpoint(u32 addr)
{
2021-10-15 01:26:51 +02:00
m_ppu_breakpoint_handler->RemoveBreakpoint(addr);
for (int i = 0; i < count(); i++)
{
QListWidgetItem* currentItem = item(i);
if (currentItem->data(Qt::UserRole).value<u32>() == addr)
{
delete takeItem(i);
break;
}
}
if (!count())
{
hide();
}
}
bool breakpoint_list::AddBreakpoint(u32 pc)
{
2021-10-15 01:26:51 +02:00
if (!m_ppu_breakpoint_handler->AddBreakpoint(pc))
{
return false;
}
2020-12-16 07:53:59 +01:00
m_disasm->disasm(pc);
QString text = QString::fromStdString(m_disasm->last_opcode);
2021-03-23 20:39:39 +01:00
text.remove(10, 13);
2021-03-23 20:39:39 +01:00
QListWidgetItem* breakpoint_item = new QListWidgetItem(text);
breakpoint_item->setForeground(m_text_color_bp);
breakpoint_item->setBackground(m_color_bp);
breakpoint_item->setData(Qt::UserRole, pc);
addItem(breakpoint_item);
2020-06-13 18:16:36 +02:00
show();
return true;
}
/**
* If breakpoint exists, we remove it, else add new one. Yeah, it'd be nicer from a code logic to have it be set/reset. But, that logic has to happen somewhere anyhow.
*/
void breakpoint_list::HandleBreakpointRequest(u32 loc, bool only_add)
{
2024-08-21 18:41:55 +02:00
const auto cpu = m_disasm ? m_disasm->get_cpu() : nullptr;
if (!cpu || cpu->state & cpu_flag::exit)
{
return;
}
2024-08-21 18:41:55 +02:00
if (!is_using_interpreter(cpu->get_class()))
{
2021-10-15 01:26:51 +02:00
QMessageBox::warning(this, tr("Interpreters-Only Feature!"), tr("Cannot set breakpoints on non-interpreter decoders."));
return;
}
2024-08-21 18:41:55 +02:00
switch (cpu->get_class())
{
case thread_class::spu:
2021-10-15 01:26:51 +02:00
{
if (loc >= SPU_LS_SIZE || loc % 4)
{
QMessageBox::warning(this, tr("Invalid Memory For Breakpoints!"), tr("Cannot set breakpoints on non-SPU executable memory!"));
return;
}
2024-08-21 18:41:55 +02:00
const auto spu = static_cast<spu_thread*>(cpu);
2021-10-15 01:26:51 +02:00
auto& list = spu->local_breakpoints;
const u32 pos_at = loc / 4;
const u32 pos_bit = 1u << (pos_at % 8);
2021-10-15 01:26:51 +02:00
if (list[pos_at / 8].fetch_xor(pos_bit) & pos_bit)
2021-10-15 01:26:51 +02:00
{
if (std::none_of(list.begin(), list.end(), [](auto& val){ return val.load(); }))
{
spu->has_active_local_bps = false;
}
}
else
{
if (!spu->has_active_local_bps.exchange(true))
{
spu->state.atomic_op([](bs_t<cpu_flag>& flags)
{
if (flags & cpu_flag::pending)
{
flags += cpu_flag::pending_recheck;
}
else
{
flags += cpu_flag::pending;
}
});
}
}
return;
}
case thread_class::ppu:
break;
2021-10-15 01:26:51 +02:00
default:
QMessageBox::warning(this, tr("Unimplemented Breakpoints For Thread Type!"), tr("Cannot set breakpoints on a thread not an PPU/SPU currently, sorry."));
return;
}
2021-10-15 01:26:51 +02:00
if (!vm::check_addr(loc, vm::page_executable))
{
2021-10-15 01:26:51 +02:00
QMessageBox::warning(this, tr("Invalid Memory For Breakpoints!"), tr("Cannot set breakpoints on non-executable memory!"));
return;
}
2021-10-15 01:26:51 +02:00
if (m_ppu_breakpoint_handler->HasBreakpoint(loc))
{
if (!only_add)
{
RemoveBreakpoint(loc);
}
}
else
{
if (!AddBreakpoint(loc))
{
QMessageBox::warning(this, tr("Unknown error while setting breakpoint!"), tr("Failed to set breakpoints."));
return;
}
}
}
void breakpoint_list::OnBreakpointListDoubleClicked()
{
if (QListWidgetItem* item = currentItem())
{
const u32 address = item->data(Qt::UserRole).value<u32>();
Q_EMIT RequestShowAddress(address);
}
}
void breakpoint_list::OnBreakpointListRightClicked(const QPoint &pos)
{
if (!itemAt(pos))
{
return;
}
2021-03-23 23:23:37 +01:00
m_context_menu = new QMenu();
if (selectedItems().count() == 1)
{
2021-03-23 23:23:37 +01:00
QAction* rename_action = m_context_menu->addAction(tr("&Rename"));
2021-03-23 20:39:39 +01:00
connect(rename_action, &QAction::triggered, this, [this]()
{
QListWidgetItem* current_item = selectedItems().first();
current_item->setFlags(current_item->flags() | Qt::ItemIsEditable);
editItem(current_item);
});
2021-03-23 23:23:37 +01:00
m_context_menu->addSeparator();
}
2021-03-23 23:23:37 +01:00
m_context_menu->addAction(m_delete_action);
m_context_menu->exec(viewport()->mapToGlobal(pos));
m_context_menu->deleteLater();
m_context_menu = nullptr;
}
void breakpoint_list::OnBreakpointListDelete()
{
2021-03-23 20:39:39 +01:00
for (int i = selectedItems().count() - 1; i >= 0; i--)
{
RemoveBreakpoint(::at32(selectedItems(), i)->data(Qt::UserRole).value<u32>());
2021-03-23 23:23:37 +01:00
}
if (m_context_menu)
{
m_context_menu->close();
}
}
void breakpoint_list::mouseDoubleClickEvent(QMouseEvent* ev)
{
if (!ev) return;
// Qt's itemDoubleClicked signal doesn't distinguish between mouse buttons and there is no simple way to get the pressed button.
// So we have to ignore this event when another button is pressed.
if (ev->button() != Qt::LeftButton)
{
ev->ignore();
return;
}
QListWidget::mouseDoubleClickEvent(ev);
}