disassembler.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QShortcut>
  5. #include "citra_qt/bootmanager.h"
  6. #include "citra_qt/debugger/disassembler.h"
  7. #include "citra_qt/hotkeys.h"
  8. #include "citra_qt/util/util.h"
  9. #include "common/break_points.h"
  10. #include "common/symbols.h"
  11. #include "core/arm/arm_interface.h"
  12. #include "core/arm/disassembler/arm_disasm.h"
  13. #include "core/core.h"
  14. #include "core/memory.h"
  15. DisassemblerModel::DisassemblerModel(QObject* parent)
  16. : QAbstractListModel(parent), base_address(0), code_size(0), program_counter(0),
  17. selection(QModelIndex()) {}
  18. int DisassemblerModel::columnCount(const QModelIndex& parent) const {
  19. return 3;
  20. }
  21. int DisassemblerModel::rowCount(const QModelIndex& parent) const {
  22. return code_size;
  23. }
  24. QVariant DisassemblerModel::data(const QModelIndex& index, int role) const {
  25. switch (role) {
  26. case Qt::DisplayRole: {
  27. u32 address = base_address + index.row() * 4;
  28. u32 instr = Memory::Read32(address);
  29. std::string disassembly = ARM_Disasm::Disassemble(address, instr);
  30. if (index.column() == 0) {
  31. return QString("0x%1").arg((uint)(address), 8, 16, QLatin1Char('0'));
  32. } else if (index.column() == 1) {
  33. return QString::fromStdString(disassembly);
  34. } else if (index.column() == 2) {
  35. if (Symbols::HasSymbol(address)) {
  36. TSymbol symbol = Symbols::GetSymbol(address);
  37. return QString("%1 - Size:%2")
  38. .arg(QString::fromStdString(symbol.name))
  39. .arg(symbol.size / 4); // divide by 4 to get instruction count
  40. } else if (ARM_Disasm::Decode(instr) == OP_BL) {
  41. u32 offset = instr & 0xFFFFFF;
  42. // Sign-extend the 24-bit offset
  43. if ((offset >> 23) & 1)
  44. offset |= 0xFF000000;
  45. // Pre-compute the left-shift and the prefetch offset
  46. offset <<= 2;
  47. offset += 8;
  48. TSymbol symbol = Symbols::GetSymbol(address + offset);
  49. return QString(" --> %1").arg(QString::fromStdString(symbol.name));
  50. }
  51. }
  52. break;
  53. }
  54. case Qt::BackgroundRole: {
  55. unsigned int address = base_address + 4 * index.row();
  56. if (breakpoints.IsAddressBreakPoint(address))
  57. return QBrush(QColor(0xFF, 0xC0, 0xC0));
  58. else if (address == program_counter)
  59. return QBrush(QColor(0xC0, 0xC0, 0xFF));
  60. break;
  61. }
  62. case Qt::FontRole: {
  63. if (index.column() == 0 || index.column() == 1) { // 2 is the symbols column
  64. return GetMonospaceFont();
  65. }
  66. break;
  67. }
  68. default:
  69. break;
  70. }
  71. return QVariant();
  72. }
  73. QModelIndex DisassemblerModel::IndexFromAbsoluteAddress(unsigned int address) const {
  74. return index((address - base_address) / 4, 0);
  75. }
  76. const BreakPoints& DisassemblerModel::GetBreakPoints() const {
  77. return breakpoints;
  78. }
  79. void DisassemblerModel::ParseFromAddress(unsigned int address) {
  80. // NOTE: A too large value causes lagging when scrolling the disassembly
  81. const unsigned int chunk_size = 1000 * 500;
  82. // If we haven't loaded anything yet, initialize base address to the parameter address
  83. if (code_size == 0)
  84. base_address = address;
  85. // If the new area is already loaded, just continue
  86. if (base_address + code_size > address + chunk_size && base_address <= address)
  87. return;
  88. // Insert rows before currently loaded data
  89. if (base_address > address) {
  90. unsigned int num_rows = (address - base_address) / 4;
  91. beginInsertRows(QModelIndex(), 0, num_rows);
  92. code_size += num_rows;
  93. base_address = address;
  94. endInsertRows();
  95. }
  96. // Insert rows after currently loaded data
  97. if (base_address + code_size < address + chunk_size) {
  98. unsigned int num_rows = (base_address + chunk_size - code_size - address) / 4;
  99. beginInsertRows(QModelIndex(), 0, num_rows);
  100. code_size += num_rows;
  101. endInsertRows();
  102. }
  103. SetNextInstruction(address);
  104. }
  105. void DisassemblerModel::OnSelectionChanged(const QModelIndex& new_selection) {
  106. selection = new_selection;
  107. }
  108. void DisassemblerModel::OnSetOrUnsetBreakpoint() {
  109. if (!selection.isValid())
  110. return;
  111. unsigned int address = base_address + selection.row() * 4;
  112. if (breakpoints.IsAddressBreakPoint(address)) {
  113. breakpoints.Remove(address);
  114. } else {
  115. breakpoints.Add(address);
  116. }
  117. emit dataChanged(selection, selection);
  118. }
  119. void DisassemblerModel::SetNextInstruction(unsigned int address) {
  120. QModelIndex cur_index = IndexFromAbsoluteAddress(program_counter);
  121. QModelIndex prev_index = IndexFromAbsoluteAddress(address);
  122. program_counter = address;
  123. emit dataChanged(cur_index, cur_index);
  124. emit dataChanged(prev_index, prev_index);
  125. }
  126. DisassemblerWidget::DisassemblerWidget(QWidget* parent, EmuThread* emu_thread)
  127. : QDockWidget(parent), base_addr(0), emu_thread(emu_thread) {
  128. disasm_ui.setupUi(this);
  129. RegisterHotkey("Disassembler", "Start/Stop", QKeySequence(Qt::Key_F5), Qt::ApplicationShortcut);
  130. RegisterHotkey("Disassembler", "Step", QKeySequence(Qt::Key_F10), Qt::ApplicationShortcut);
  131. RegisterHotkey("Disassembler", "Step into", QKeySequence(Qt::Key_F11), Qt::ApplicationShortcut);
  132. RegisterHotkey("Disassembler", "Set Breakpoint", QKeySequence(Qt::Key_F9),
  133. Qt::ApplicationShortcut);
  134. connect(disasm_ui.button_step, SIGNAL(clicked()), this, SLOT(OnStep()));
  135. connect(disasm_ui.button_pause, SIGNAL(clicked()), this, SLOT(OnPause()));
  136. connect(disasm_ui.button_continue, SIGNAL(clicked()), this, SLOT(OnContinue()));
  137. connect(GetHotkey("Disassembler", "Start/Stop", this), SIGNAL(activated()), this,
  138. SLOT(OnToggleStartStop()));
  139. connect(GetHotkey("Disassembler", "Step", this), SIGNAL(activated()), this, SLOT(OnStep()));
  140. connect(GetHotkey("Disassembler", "Step into", this), SIGNAL(activated()), this,
  141. SLOT(OnStepInto()));
  142. setEnabled(false);
  143. }
  144. void DisassemblerWidget::Init() {
  145. model->ParseFromAddress(Core::CPU().GetPC());
  146. disasm_ui.treeView->resizeColumnToContents(0);
  147. disasm_ui.treeView->resizeColumnToContents(1);
  148. disasm_ui.treeView->resizeColumnToContents(2);
  149. QModelIndex model_index = model->IndexFromAbsoluteAddress(Core::CPU().GetPC());
  150. disasm_ui.treeView->scrollTo(model_index);
  151. disasm_ui.treeView->selectionModel()->setCurrentIndex(
  152. model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
  153. }
  154. void DisassemblerWidget::OnContinue() {
  155. emu_thread->SetRunning(true);
  156. }
  157. void DisassemblerWidget::OnStep() {
  158. OnStepInto(); // change later
  159. }
  160. void DisassemblerWidget::OnStepInto() {
  161. emu_thread->SetRunning(false);
  162. emu_thread->ExecStep();
  163. }
  164. void DisassemblerWidget::OnPause() {
  165. emu_thread->SetRunning(false);
  166. // TODO: By now, the CPU might not have actually stopped...
  167. if (Core::System::GetInstance().IsPoweredOn()) {
  168. model->SetNextInstruction(Core::CPU().GetPC());
  169. }
  170. }
  171. void DisassemblerWidget::OnToggleStartStop() {
  172. emu_thread->SetRunning(!emu_thread->IsRunning());
  173. }
  174. void DisassemblerWidget::OnDebugModeEntered() {
  175. u32 next_instr = Core::CPU().GetPC();
  176. if (model->GetBreakPoints().IsAddressBreakPoint(next_instr))
  177. emu_thread->SetRunning(false);
  178. model->SetNextInstruction(next_instr);
  179. QModelIndex model_index = model->IndexFromAbsoluteAddress(next_instr);
  180. disasm_ui.treeView->scrollTo(model_index);
  181. disasm_ui.treeView->selectionModel()->setCurrentIndex(
  182. model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
  183. }
  184. void DisassemblerWidget::OnDebugModeLeft() {}
  185. int DisassemblerWidget::SelectedRow() {
  186. QModelIndex index = disasm_ui.treeView->selectionModel()->currentIndex();
  187. if (!index.isValid())
  188. return -1;
  189. return disasm_ui.treeView->selectionModel()->currentIndex().row();
  190. }
  191. void DisassemblerWidget::OnEmulationStarting(EmuThread* emu_thread) {
  192. this->emu_thread = emu_thread;
  193. model = new DisassemblerModel(this);
  194. disasm_ui.treeView->setModel(model);
  195. connect(disasm_ui.treeView->selectionModel(),
  196. SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), model,
  197. SLOT(OnSelectionChanged(const QModelIndex&)));
  198. connect(disasm_ui.button_breakpoint, SIGNAL(clicked()), model, SLOT(OnSetOrUnsetBreakpoint()));
  199. connect(GetHotkey("Disassembler", "Set Breakpoint", this), SIGNAL(activated()), model,
  200. SLOT(OnSetOrUnsetBreakpoint()));
  201. Init();
  202. setEnabled(true);
  203. }
  204. void DisassemblerWidget::OnEmulationStopping() {
  205. disasm_ui.treeView->setModel(nullptr);
  206. delete model;
  207. emu_thread = nullptr;
  208. setEnabled(false);
  209. }