2015-01-04 20:36:57 +03:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-01-04 02:51:14 +03:00
|
|
|
#include "disassembler.h"
|
2014-04-19 02:30:53 +04:00
|
|
|
|
2015-04-17 01:35:09 +03:00
|
|
|
#include "../main.h"
|
2015-01-04 02:51:14 +03:00
|
|
|
#include "../bootmanager.h"
|
|
|
|
#include "../hotkeys.h"
|
2014-04-01 06:26:50 +04:00
|
|
|
|
2014-04-11 04:50:10 +04:00
|
|
|
#include "common/common.h"
|
|
|
|
#include "core/mem_map.h"
|
2014-04-01 06:26:50 +04:00
|
|
|
|
2014-04-11 04:50:10 +04:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "common/break_points.h"
|
2014-04-13 02:59:26 +04:00
|
|
|
#include "common/symbols.h"
|
2014-12-22 09:30:09 +03:00
|
|
|
#include "core/arm/arm_interface.h"
|
2014-09-11 05:27:14 +04:00
|
|
|
#include "core/arm/skyeye_common/armdefs.h"
|
2014-04-11 04:50:10 +04:00
|
|
|
#include "core/arm/disassembler/arm_disasm.h"
|
2014-04-01 06:26:50 +04:00
|
|
|
|
2014-07-02 23:16:36 +04:00
|
|
|
|
2015-02-03 22:45:33 +03:00
|
|
|
DisassemblerModel::DisassemblerModel(QObject* parent) : QAbstractListModel(parent), base_address(0), code_size(0), program_counter(0), selection(QModelIndex()) {
|
2014-07-02 23:16:36 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int DisassemblerModel::columnCount(const QModelIndex& parent) const {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DisassemblerModel::rowCount(const QModelIndex& parent) const {
|
|
|
|
return code_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DisassemblerModel::data(const QModelIndex& index, int role) const {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
{
|
|
|
|
u32 address = base_address + index.row() * 4;
|
2014-07-23 02:58:52 +04:00
|
|
|
u32 instr = Memory::Read32(address);
|
2014-09-06 22:37:19 +04:00
|
|
|
std::string disassembly = ARM_Disasm::Disassemble(address, instr);
|
2014-07-02 23:16:36 +04:00
|
|
|
|
|
|
|
if (index.column() == 0) {
|
|
|
|
return QString("0x%1").arg((uint)(address), 8, 16, QLatin1Char('0'));
|
|
|
|
} else if (index.column() == 1) {
|
2014-09-06 22:37:19 +04:00
|
|
|
return QString::fromStdString(disassembly);
|
2014-07-23 02:58:52 +04:00
|
|
|
} else if (index.column() == 2) {
|
|
|
|
if(Symbols::HasSymbol(address)) {
|
|
|
|
TSymbol symbol = Symbols::GetSymbol(address);
|
|
|
|
return QString("%1 - Size:%2").arg(QString::fromStdString(symbol.name))
|
|
|
|
.arg(symbol.size / 4); // divide by 4 to get instruction count
|
2014-09-06 22:37:19 +04:00
|
|
|
} else if (ARM_Disasm::Decode(instr) == OP_BL) {
|
2014-07-23 02:58:52 +04:00
|
|
|
u32 offset = instr & 0xFFFFFF;
|
|
|
|
|
|
|
|
// Sign-extend the 24-bit offset
|
|
|
|
if ((offset >> 23) & 1)
|
|
|
|
offset |= 0xFF000000;
|
|
|
|
|
|
|
|
// Pre-compute the left-shift and the prefetch offset
|
|
|
|
offset <<= 2;
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
TSymbol symbol = Symbols::GetSymbol(address + offset);
|
|
|
|
return QString(" --> %1").arg(QString::fromStdString(symbol.name));
|
|
|
|
}
|
2014-07-02 23:16:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::BackgroundRole:
|
|
|
|
{
|
|
|
|
unsigned int address = base_address + 4 * index.row();
|
|
|
|
|
|
|
|
if (breakpoints.IsAddressBreakPoint(address))
|
|
|
|
return QBrush(QColor(0xFF, 0xC0, 0xC0));
|
|
|
|
else if (address == program_counter)
|
|
|
|
return QBrush(QColor(0xC0, 0xC0, 0xFF));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex DisassemblerModel::IndexFromAbsoluteAddress(unsigned int address) const {
|
|
|
|
return index((address - base_address) / 4, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const BreakPoints& DisassemblerModel::GetBreakPoints() const {
|
|
|
|
return breakpoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisassemblerModel::ParseFromAddress(unsigned int address) {
|
2014-07-23 02:50:28 +04:00
|
|
|
|
|
|
|
// NOTE: A too large value causes lagging when scrolling the disassembly
|
|
|
|
const unsigned int chunk_size = 1000*500;
|
2014-07-02 23:16:36 +04:00
|
|
|
|
|
|
|
// If we haven't loaded anything yet, initialize base address to the parameter address
|
|
|
|
if (code_size == 0)
|
|
|
|
base_address = address;
|
|
|
|
|
|
|
|
// If the new area is already loaded, just continue
|
|
|
|
if (base_address + code_size > address + chunk_size && base_address <= address)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Insert rows before currently loaded data
|
|
|
|
if (base_address > address) {
|
|
|
|
unsigned int num_rows = (address - base_address) / 4;
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), 0, num_rows);
|
|
|
|
code_size += num_rows;
|
|
|
|
base_address = address;
|
|
|
|
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert rows after currently loaded data
|
|
|
|
if (base_address + code_size < address + chunk_size) {
|
|
|
|
unsigned int num_rows = (base_address + chunk_size - code_size - address) / 4;
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), 0, num_rows);
|
|
|
|
code_size += num_rows;
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
SetNextInstruction(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisassemblerModel::OnSelectionChanged(const QModelIndex& new_selection) {
|
|
|
|
selection = new_selection;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisassemblerModel::OnSetOrUnsetBreakpoint() {
|
|
|
|
if (!selection.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned int address = base_address + selection.row() * 4;
|
|
|
|
|
|
|
|
if (breakpoints.IsAddressBreakPoint(address)) {
|
|
|
|
breakpoints.Remove(address);
|
|
|
|
} else {
|
|
|
|
breakpoints.Add(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit dataChanged(selection, selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisassemblerModel::SetNextInstruction(unsigned int address) {
|
|
|
|
QModelIndex cur_index = IndexFromAbsoluteAddress(program_counter);
|
|
|
|
QModelIndex prev_index = IndexFromAbsoluteAddress(address);
|
|
|
|
|
|
|
|
program_counter = address;
|
|
|
|
|
|
|
|
emit dataChanged(cur_index, cur_index);
|
|
|
|
emit dataChanged(prev_index, prev_index);
|
|
|
|
}
|
|
|
|
|
2015-04-17 01:35:09 +03:00
|
|
|
DisassemblerWidget::DisassemblerWidget(QWidget* parent, GMainWindow& main_window) :
|
|
|
|
QDockWidget(parent), main_window(main_window), base_addr(0) {
|
|
|
|
|
2014-04-01 06:26:50 +04:00
|
|
|
disasm_ui.setupUi(this);
|
|
|
|
|
2014-07-02 23:16:36 +04:00
|
|
|
model = new DisassemblerModel(this);
|
2014-04-01 06:26:50 +04:00
|
|
|
disasm_ui.treeView->setModel(model);
|
2014-04-19 02:34:03 +04:00
|
|
|
|
2014-04-04 05:24:07 +04:00
|
|
|
RegisterHotkey("Disassembler", "Start/Stop", QKeySequence(Qt::Key_F5), Qt::ApplicationShortcut);
|
2014-04-01 06:26:50 +04:00
|
|
|
RegisterHotkey("Disassembler", "Step", QKeySequence(Qt::Key_F10), Qt::ApplicationShortcut);
|
|
|
|
RegisterHotkey("Disassembler", "Step into", QKeySequence(Qt::Key_F11), Qt::ApplicationShortcut);
|
|
|
|
RegisterHotkey("Disassembler", "Set Breakpoint", QKeySequence(Qt::Key_F9), Qt::ApplicationShortcut);
|
|
|
|
|
2014-07-02 23:16:36 +04:00
|
|
|
connect(disasm_ui.button_breakpoint, SIGNAL(clicked()), model, SLOT(OnSetOrUnsetBreakpoint()));
|
2014-04-01 06:26:50 +04:00
|
|
|
connect(disasm_ui.button_step, SIGNAL(clicked()), this, SLOT(OnStep()));
|
|
|
|
connect(disasm_ui.button_pause, SIGNAL(clicked()), this, SLOT(OnPause()));
|
|
|
|
connect(disasm_ui.button_continue, SIGNAL(clicked()), this, SLOT(OnContinue()));
|
2014-04-04 05:24:07 +04:00
|
|
|
|
2014-07-02 23:16:36 +04:00
|
|
|
connect(disasm_ui.treeView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
|
|
|
|
model, SLOT(OnSelectionChanged(const QModelIndex&)));
|
|
|
|
|
2014-04-04 05:24:07 +04:00
|
|
|
connect(GetHotkey("Disassembler", "Start/Stop", this), SIGNAL(activated()), this, SLOT(OnToggleStartStop()));
|
2014-04-01 06:26:50 +04:00
|
|
|
connect(GetHotkey("Disassembler", "Step", this), SIGNAL(activated()), this, SLOT(OnStep()));
|
|
|
|
connect(GetHotkey("Disassembler", "Step into", this), SIGNAL(activated()), this, SLOT(OnStepInto()));
|
2014-07-02 23:16:36 +04:00
|
|
|
connect(GetHotkey("Disassembler", "Set Breakpoint", this), SIGNAL(activated()), model, SLOT(OnSetOrUnsetBreakpoint()));
|
2014-04-01 06:26:50 +04:00
|
|
|
}
|
|
|
|
|
2014-04-19 02:30:53 +04:00
|
|
|
void DisassemblerWidget::Init()
|
2014-04-04 05:24:07 +04:00
|
|
|
{
|
2014-07-02 23:16:36 +04:00
|
|
|
model->ParseFromAddress(Core::g_app_core->GetPC());
|
2014-04-04 05:24:07 +04:00
|
|
|
|
|
|
|
disasm_ui.treeView->resizeColumnToContents(0);
|
|
|
|
disasm_ui.treeView->resizeColumnToContents(1);
|
2014-04-19 02:30:53 +04:00
|
|
|
disasm_ui.treeView->resizeColumnToContents(2);
|
|
|
|
|
2014-07-02 23:16:36 +04:00
|
|
|
QModelIndex model_index = model->IndexFromAbsoluteAddress(Core::g_app_core->GetPC());
|
2014-04-04 05:24:07 +04:00
|
|
|
disasm_ui.treeView->scrollTo(model_index);
|
|
|
|
disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
|
|
|
|
}
|
|
|
|
|
2014-04-19 02:30:53 +04:00
|
|
|
void DisassemblerWidget::OnContinue()
|
2014-04-04 05:24:07 +04:00
|
|
|
{
|
2015-04-29 02:03:01 +03:00
|
|
|
main_window.GetEmuThread()->SetRunning(true);
|
2014-04-04 05:24:07 +04:00
|
|
|
}
|
|
|
|
|
2014-04-19 02:30:53 +04:00
|
|
|
void DisassemblerWidget::OnStep()
|
2014-04-04 05:24:07 +04:00
|
|
|
{
|
|
|
|
OnStepInto(); // change later
|
|
|
|
}
|
|
|
|
|
2014-04-19 02:30:53 +04:00
|
|
|
void DisassemblerWidget::OnStepInto()
|
2014-04-01 06:26:50 +04:00
|
|
|
{
|
2015-04-29 02:03:01 +03:00
|
|
|
main_window.GetEmuThread()->SetRunning(false);
|
2015-04-17 01:35:09 +03:00
|
|
|
main_window.GetEmuThread()->ExecStep();
|
2014-04-01 06:26:50 +04:00
|
|
|
}
|
|
|
|
|
2014-04-19 02:30:53 +04:00
|
|
|
void DisassemblerWidget::OnPause()
|
2014-04-01 06:26:50 +04:00
|
|
|
{
|
2015-04-29 02:03:01 +03:00
|
|
|
main_window.GetEmuThread()->SetRunning(false);
|
2014-07-02 23:16:36 +04:00
|
|
|
|
|
|
|
// TODO: By now, the CPU might not have actually stopped...
|
2014-12-28 21:11:51 +03:00
|
|
|
if (Core::g_app_core) {
|
2014-12-28 12:56:07 +03:00
|
|
|
model->SetNextInstruction(Core::g_app_core->GetPC());
|
|
|
|
}
|
2014-04-01 06:26:50 +04:00
|
|
|
}
|
|
|
|
|
2014-04-19 02:30:53 +04:00
|
|
|
void DisassemblerWidget::OnToggleStartStop()
|
2014-04-01 06:26:50 +04:00
|
|
|
{
|
2015-04-29 02:03:01 +03:00
|
|
|
main_window.GetEmuThread()->SetRunning(!main_window.GetEmuThread()->IsRunning());
|
2014-04-01 06:26:50 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 14:14:23 +03:00
|
|
|
void DisassemblerWidget::OnDebugModeEntered()
|
2014-04-01 06:26:50 +04:00
|
|
|
{
|
2014-04-11 04:50:10 +04:00
|
|
|
ARMword next_instr = Core::g_app_core->GetPC();
|
2014-04-01 06:26:50 +04:00
|
|
|
|
2015-03-30 22:37:34 +03:00
|
|
|
if (model->GetBreakPoints().IsAddressBreakPoint(next_instr))
|
2015-04-29 02:03:01 +03:00
|
|
|
main_window.GetEmuThread()->SetRunning(false);
|
2014-04-04 05:24:07 +04:00
|
|
|
|
2014-07-02 23:16:36 +04:00
|
|
|
model->SetNextInstruction(next_instr);
|
|
|
|
|
|
|
|
QModelIndex model_index = model->IndexFromAbsoluteAddress(next_instr);
|
2014-04-04 05:24:07 +04:00
|
|
|
disasm_ui.treeView->scrollTo(model_index);
|
|
|
|
disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
|
2014-04-01 06:26:50 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 14:14:23 +03:00
|
|
|
void DisassemblerWidget::OnDebugModeLeft()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-19 02:30:53 +04:00
|
|
|
int DisassemblerWidget::SelectedRow()
|
2014-04-01 06:26:50 +04:00
|
|
|
{
|
|
|
|
QModelIndex index = disasm_ui.treeView->selectionModel()->currentIndex();
|
|
|
|
if (!index.isValid())
|
|
|
|
return -1;
|
|
|
|
|
2014-07-02 23:16:36 +04:00
|
|
|
return disasm_ui.treeView->selectionModel()->currentIndex().row();
|
|
|
|
}
|