|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (C) 2021 PCSX-Redux authors * |
| 3 | + * * |
| 4 | + * This program is free software; you can redistribute it and/or modify * |
| 5 | + * it under the terms of the GNU General Public License as published by * |
| 6 | + * the Free Software Foundation; either version 2 of the License, or * |
| 7 | + * (at your option) any later version. * |
| 8 | + * * |
| 9 | + * This program is distributed in the hope that it will be useful, * |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 | + * GNU General Public License for more details. * |
| 13 | + * * |
| 14 | + * You should have received a copy of the GNU General Public License * |
| 15 | + * along with this program; if not, write to the * |
| 16 | + * Free Software Foundation, Inc., * |
| 17 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
| 18 | + ***************************************************************************/ |
| 19 | + |
| 20 | +#include "gui/widgets/dynarec_disassembly.h" |
| 21 | + |
| 22 | +#include <capstone/capstone.h> |
| 23 | + |
| 24 | +#include <cinttypes> |
| 25 | +#include <fstream> |
| 26 | + |
| 27 | +#include "gui/gui.h" |
| 28 | +#include "imgui.h" |
| 29 | + |
| 30 | +void PCSX::Widgets::Disassembly::writeFile() { |
| 31 | + std::ofstream file; |
| 32 | + // Open file - default location in resources directory |
| 33 | + file.open("DynarecDisassembly.txt", std::ios::app); |
| 34 | + // If file exists, write to it, otherwise return |
| 35 | + if (file) { |
| 36 | + for (auto i = 0; i < m_items.size(); ++i) { |
| 37 | + file << m_items[i]; |
| 38 | + } |
| 39 | + } else { |
| 40 | + PCSX::g_system->printf("Disassembler Error: failed to open output file for disassembly.\n"); |
| 41 | + m_showError = true; |
| 42 | + return; |
| 43 | + } |
| 44 | + // Close out file |
| 45 | + file.close(); |
| 46 | + // If bad bit is set, there was an error, return -1 |
| 47 | + if (file.fail()) { |
| 48 | + PCSX::g_system->printf("Disassembler Error: failed to write disassembly to output file.\n"); |
| 49 | + m_showError = true; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void PCSX::Widgets::Disassembly::draw(GUI* gui, const char* title) { |
| 54 | + ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver); |
| 55 | + if (!ImGui::Begin(title, &m_show)) { |
| 56 | + ImGui::End(); |
| 57 | + return; |
| 58 | + } |
| 59 | + // Disassemble button |
| 60 | + if (ImGui::Button("Disassemble Buffer")) { |
| 61 | + m_codeSize = disassembleBuffer(); |
| 62 | + } |
| 63 | + ImGui::SameLine(); |
| 64 | + // Save to File button |
| 65 | + if (ImGui::Button("Save to File")) { |
| 66 | + writeFile(); |
| 67 | + } |
| 68 | + // Error popup |
| 69 | + if (m_showError) { |
| 70 | + ImGui::OpenPopup("Disassembler Error"); |
| 71 | + if (ImGui::BeginPopupModal("Disassembler Error", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { |
| 72 | + ImGui::Text("Disassembly Failed.\nCheck Logs"); |
| 73 | + if (ImGui::Button("Close")) { |
| 74 | + ImGui::CloseCurrentPopup(); |
| 75 | + m_showError = false; |
| 76 | + } |
| 77 | + } |
| 78 | + ImGui::EndPopup(); |
| 79 | + } |
| 80 | + // Close error popup |
| 81 | + if (ImGui::BeginPopupContextItem()) { |
| 82 | + if (ImGui::MenuItem("Close Disassembler")) { |
| 83 | + m_show = false; |
| 84 | + } |
| 85 | + ImGui::EndPopup(); |
| 86 | + } |
| 87 | + |
| 88 | + ImGui::Separator(); |
| 89 | + // Clear items button |
| 90 | + if (ImGui::SmallButton("Clear")) { |
| 91 | + m_items.clear(); |
| 92 | + m_codeSize = 0; |
| 93 | + } |
| 94 | + ImGui::SameLine(); |
| 95 | + bool copy_to_clipboard = ImGui::SmallButton("Copy"); |
| 96 | + |
| 97 | + // Options menu |
| 98 | + if (ImGui::BeginPopup("Options")) { |
| 99 | + ImGui::Checkbox("Auto-scroll", &m_autoScroll); |
| 100 | + ImGui::Checkbox("Mono", &m_mono); |
| 101 | + ImGui::EndPopup(); |
| 102 | + } |
| 103 | + |
| 104 | + ImGui::SameLine(); |
| 105 | + // Options, Filter |
| 106 | + if (ImGui::SmallButton("Options")) { |
| 107 | + ImGui::OpenPopup("Options"); |
| 108 | + } |
| 109 | + |
| 110 | + ImGui::SameLine(); |
| 111 | + // Show buffer size returned from disassembly function |
| 112 | + ImGui::Text("Code size: %.2fMB", (double)m_codeSize / (1024 * 1024)); |
| 113 | + ImGui::Separator(); |
| 114 | + |
| 115 | + if (m_mono) { |
| 116 | + gui->useMonoFont(); |
| 117 | + } |
| 118 | + // Scrolling child window containing diassembly output |
| 119 | + ImGui::BeginChild("ScrollingRegion", ImVec2(0, -4), false, ImGuiWindowFlags_HorizontalScrollbar); |
| 120 | + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing |
| 121 | + |
| 122 | + if (copy_to_clipboard) { |
| 123 | + ImGui::LogToClipboard(); |
| 124 | + } |
| 125 | + // Loop through vec and display each string item in scrolling region |
| 126 | + for (auto& item : m_items) { |
| 127 | + ImGui::TextUnformatted(item.c_str()); |
| 128 | + } |
| 129 | + |
| 130 | + if (copy_to_clipboard) { |
| 131 | + ImGui::LogFinish(); |
| 132 | + } |
| 133 | + |
| 134 | + if (m_scrollToBottom || (m_autoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())) { |
| 135 | + ImGui::SetScrollHereY(1.0f); |
| 136 | + } |
| 137 | + m_scrollToBottom = false; |
| 138 | + |
| 139 | + ImGui::PopStyleVar(); |
| 140 | + ImGui::EndChild(); |
| 141 | + |
| 142 | + if (m_mono) { |
| 143 | + ImGui::PopFont(); |
| 144 | + } |
| 145 | + |
| 146 | + ImGui::Separator(); |
| 147 | + |
| 148 | + // Auto-focus on window apparition |
| 149 | + ImGui::SetItemDefaultFocus(); |
| 150 | + |
| 151 | + ImGui::End(); |
| 152 | +} |
| 153 | + |
| 154 | +size_t PCSX::Widgets::Disassembly::disassembleBuffer() { |
| 155 | + csh handle; |
| 156 | + cs_insn* insn; |
| 157 | + size_t count; |
| 158 | + |
| 159 | + // Get pointer to code buffer along with size of buffer |
| 160 | + const uint8_t* buffer = PCSX::g_emulator->m_psxCpu->getBufferPtr(); |
| 161 | + const size_t bufferSize = PCSX::g_emulator->m_psxCpu->getBufferSize(); |
| 162 | + // Check to ensure code buffer pointer is not null and size is not 0 |
| 163 | + if (buffer == nullptr) { |
| 164 | + PCSX::g_system->printf("Disassembler Error: nullpointer to code buffer.\n"); |
| 165 | + m_showError = true; |
| 166 | + return 0; |
| 167 | + } else if (bufferSize <= 0) { |
| 168 | + PCSX::g_system->printf("Disassembler Error: Invalid code buffer size.\n"); |
| 169 | + m_showError = true; |
| 170 | + return 0; |
| 171 | + } |
| 172 | + // Attempt to initialize Capstone disassembler, if error log it and return |
| 173 | + if (cs_open(CS_ARCH, CS_MODE, &handle) != CS_ERR_OK) { |
| 174 | + PCSX::g_system->printf("Disassembler Error: Failed to initialize Capstone.\n"); |
| 175 | + m_showError = true; |
| 176 | + return 0; |
| 177 | + } |
| 178 | + // Set SKIPDATA option as to not break disassembler |
| 179 | + cs_option(handle, CS_OPT_SKIPDATA, CS_OPT_ON); |
| 180 | + // Walk code buffer and try to disassemble |
| 181 | + count = cs_disasm(handle, buffer, bufferSize, 0x0, 0, &insn); |
| 182 | + if (count > 0) { |
| 183 | + for (size_t j = 0; j < count; j++) { |
| 184 | + // Write instruction (address, mnemonic, and operand to string |
| 185 | + std::string s = |
| 186 | + fmt::sprintf("%#010" PRIx64 ":\t\t%-12s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str); |
| 187 | + // Log each instruction to the disassembly window string vector |
| 188 | + addInstruction(s); |
| 189 | + } |
| 190 | + // Call free to clean up memory allocated by capstone |
| 191 | + cs_free(insn, count); |
| 192 | + // to show the end of the disassembly for the disassembled buffer |
| 193 | + addInstruction("----End of disassembly----\n"); |
| 194 | + // If disassembly failed, log the error and close out disassembler |
| 195 | + } else { |
| 196 | + cs_close(&handle); |
| 197 | + PCSX::g_system->printf("Disassembler Error: Failed to disassemble buffer.\n"); |
| 198 | + m_showError = true; |
| 199 | + return 0; |
| 200 | + } |
| 201 | + // Successful disassembly, clean up disassembler instance and return successful result |
| 202 | + cs_close(&handle); |
| 203 | + return bufferSize; |
| 204 | +} |
0 commit comments