1
0
mirror of synced 2025-01-11 22:02:17 +01:00
ImHex/plugins/disassembler/source/content/views/view_disassembler.cpp

263 lines
12 KiB
C++
Raw Normal View History

2021-12-07 22:47:41 +01:00
#include "content/views/view_disassembler.hpp"
#include "hex/api/content_registry.hpp"
#include <hex/providers/provider.hpp>
2021-08-29 22:15:18 +02:00
#include <hex/helpers/fmt.hpp>
2024-12-14 20:36:09 +01:00
#include <fonts/vscode_icons.hpp>
#include <cstring>
feat: Added export disassembler results to ASM file (#1987) ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> This PR implements the feature request #1781, that suggests adding a button to export disassembled instructions into an ASM file. ### Implementation description This adds a button to export the current disassembled instructions to an ASM file. Said file is suffixed by an `.asm` extension if not specified at file creation. *Note: the file is written to for every `Disassembly` item in the vector, as it was the easiest and most memory-conservative way of doing it.* The file creation task is implemented based on IPS patch exports, so it fits the same pattern. A `ToastError` is raised when the ASM export could not complete successfully. Translations have been implemented for both `en_US` and `de_DE` for the two new keys: - `hex.disassembler.view.disassembler.export`: file export button - `hex.disassembler.view.disassembler.export.popup.error`: error popup text ### Screenshots The button is disabled when the disassembler is working, or when the disassembly vector is empty. Here is a complete breakdown of the visual changes: ![image](https://github.com/user-attachments/assets/af0ce701-9d77-45f1-9a5a-90d68d00bb0d) ### Additional things As expected, the exporter writes every item's `mnemonic` and `operators` to the file, producing an output like this: `example.asm` ```asm .byte 0x7f, 0x45, 0x4c, 0x46 andeq r0, r1, r2, lsl #2 andeq r0, r0, r0 andeq r0, r0, r0 eorseq r0, lr, r3 andeq r0, r0, r1 andeq r1, r0, r0, asr #32 andeq r0, r0, r0 andeq r0, r0, r0, asr #32 ``` --------- Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
2024-12-05 23:04:38 +01:00
#include <toasts/toast_notification.hpp>
2021-02-22 11:56:33 +01:00
#include <wolv/literals.hpp>
using namespace std::literals::string_literals;
using namespace wolv::literals;
namespace hex::plugin::disasm {
2024-01-08 21:51:48 +01:00
ViewDisassembler::ViewDisassembler() : View::Window("hex.disassembler.view.disassembler.name", ICON_VS_FILE_CODE) {
EventProviderDeleted::subscribe(this, [this](const auto*) {
2023-12-19 13:10:25 +01:00
m_disassembly.clear();
});
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.menu.edit.disassemble_range" }, ICON_VS_DEBUG_LINE_BY_LINE, 3100, CTRLCMD + SHIFT + Keys::D, [this] {
ImGui::SetWindowFocus(this->getName().c_str());
this->getWindowOpenState() = true;
m_range = ui::RegionType::Region;
m_regionToDisassemble = ImHexApi::HexEditor::getSelection()->getRegion();
this->disassemble();
}, [this]{
return ImHexApi::HexEditor::isSelectionValid() && !this->m_disassemblerTask.isRunning();
});
}
ViewDisassembler::~ViewDisassembler() {
EventDataChanged::unsubscribe(this);
EventRegionSelected::unsubscribe(this);
EventProviderDeleted::unsubscribe(this);
}
2021-02-22 11:56:33 +01:00
void ViewDisassembler::disassemble() {
2023-12-19 13:10:25 +01:00
m_disassembly.clear();
if (m_regionToDisassemble.getStartAddress() < m_imageBaseAddress)
return;
m_disassemblerTask = TaskManager::createTask("hex.disassembler.view.disassembler.disassembling"_lang, m_regionToDisassemble.getSize(), [this](auto &task) {
// Create a capstone disassembler instance
if (m_currArchitecture->start()) {
auto provider = ImHexApi::Provider::get();
std::vector<u8> buffer(1_MiB, 0x00);
const u64 codeOffset = m_regionToDisassemble.getStartAddress() - m_imageBaseAddress;
// Read the data in chunks and disassemble it
u64 instructionLoadAddress = m_imageLoadAddress + codeOffset;
u64 instructionDataAddress = m_regionToDisassemble.getStartAddress();
bool hadError = false;
while (instructionDataAddress < m_regionToDisassemble.getEndAddress()) {
// Read a chunk of data
size_t bufferSize = std::min<u64>(buffer.size(), (m_regionToDisassemble.getEndAddress() - instructionDataAddress));
provider->read(instructionDataAddress, buffer.data(), bufferSize);
auto code = std::span(buffer.data(), bufferSize);
// Ask capstone to disassemble the data
while (true) {
auto instruction = m_currArchitecture->disassemble(m_imageBaseAddress, instructionLoadAddress, instructionDataAddress, code);
if (!instruction.has_value())
break;
task.update(instructionDataAddress);
m_disassembly.push_back(instruction.value());
code = code.subspan(instruction->size);
instructionDataAddress += instruction->size;
instructionLoadAddress += instruction->size;
hadError = false;
if (code.empty())
break;
}
if (hadError) break;
hadError = true;
}
m_currArchitecture->end();
}
});
2021-02-22 11:56:33 +01:00
}
feat: Added export disassembler results to ASM file (#1987) ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> This PR implements the feature request #1781, that suggests adding a button to export disassembled instructions into an ASM file. ### Implementation description This adds a button to export the current disassembled instructions to an ASM file. Said file is suffixed by an `.asm` extension if not specified at file creation. *Note: the file is written to for every `Disassembly` item in the vector, as it was the easiest and most memory-conservative way of doing it.* The file creation task is implemented based on IPS patch exports, so it fits the same pattern. A `ToastError` is raised when the ASM export could not complete successfully. Translations have been implemented for both `en_US` and `de_DE` for the two new keys: - `hex.disassembler.view.disassembler.export`: file export button - `hex.disassembler.view.disassembler.export.popup.error`: error popup text ### Screenshots The button is disabled when the disassembler is working, or when the disassembly vector is empty. Here is a complete breakdown of the visual changes: ![image](https://github.com/user-attachments/assets/af0ce701-9d77-45f1-9a5a-90d68d00bb0d) ### Additional things As expected, the exporter writes every item's `mnemonic` and `operators` to the file, producing an output like this: `example.asm` ```asm .byte 0x7f, 0x45, 0x4c, 0x46 andeq r0, r1, r2, lsl #2 andeq r0, r0, r0 andeq r0, r0, r0 eorseq r0, lr, r3 andeq r0, r0, r1 andeq r1, r0, r0, asr #32 andeq r0, r0, r0 andeq r0, r0, r0, asr #32 ``` --------- Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
2024-12-05 23:04:38 +01:00
void ViewDisassembler::exportToFile() {
TaskManager::createTask("hex.ui.common.processing"_lang, TaskManager::NoProgress, [this](auto &) {
TaskManager::doLater([this] {
fs::openFileBrowser(fs::DialogMode::Save, {}, [this](const std::fs::path &path) {
auto p = path;
if (p.extension() != ".asm")
p.replace_filename(hex::format("{}{}", p.filename().string(), ".asm"));
auto file = wolv::io::File(p, wolv::io::File::Mode::Create);
if (!file.isValid()) {
ui::ToastError::open("hex.disassembler.view.disassembler.export.popup.error"_lang);
return;
}
// As disassembly code can be quite long, we prefer writing each disassembled instruction to file
for (const ContentRegistry::Disassembler::Instruction& instruction : m_disassembly) {
feat: Added export disassembler results to ASM file (#1987) ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> This PR implements the feature request #1781, that suggests adding a button to export disassembled instructions into an ASM file. ### Implementation description This adds a button to export the current disassembled instructions to an ASM file. Said file is suffixed by an `.asm` extension if not specified at file creation. *Note: the file is written to for every `Disassembly` item in the vector, as it was the easiest and most memory-conservative way of doing it.* The file creation task is implemented based on IPS patch exports, so it fits the same pattern. A `ToastError` is raised when the ASM export could not complete successfully. Translations have been implemented for both `en_US` and `de_DE` for the two new keys: - `hex.disassembler.view.disassembler.export`: file export button - `hex.disassembler.view.disassembler.export.popup.error`: error popup text ### Screenshots The button is disabled when the disassembler is working, or when the disassembly vector is empty. Here is a complete breakdown of the visual changes: ![image](https://github.com/user-attachments/assets/af0ce701-9d77-45f1-9a5a-90d68d00bb0d) ### Additional things As expected, the exporter writes every item's `mnemonic` and `operators` to the file, producing an output like this: `example.asm` ```asm .byte 0x7f, 0x45, 0x4c, 0x46 andeq r0, r1, r2, lsl #2 andeq r0, r0, r0 andeq r0, r0, r0 eorseq r0, lr, r3 andeq r0, r0, r1 andeq r1, r0, r0, asr #32 andeq r0, r0, r0 andeq r0, r0, r0, asr #32 ``` --------- Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
2024-12-05 23:04:38 +01:00
// We test for a "bugged" case that should never happen - the instruction should always have a mnemonic
if (instruction.mnemonic.empty())
feat: Added export disassembler results to ASM file (#1987) ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> This PR implements the feature request #1781, that suggests adding a button to export disassembled instructions into an ASM file. ### Implementation description This adds a button to export the current disassembled instructions to an ASM file. Said file is suffixed by an `.asm` extension if not specified at file creation. *Note: the file is written to for every `Disassembly` item in the vector, as it was the easiest and most memory-conservative way of doing it.* The file creation task is implemented based on IPS patch exports, so it fits the same pattern. A `ToastError` is raised when the ASM export could not complete successfully. Translations have been implemented for both `en_US` and `de_DE` for the two new keys: - `hex.disassembler.view.disassembler.export`: file export button - `hex.disassembler.view.disassembler.export.popup.error`: error popup text ### Screenshots The button is disabled when the disassembler is working, or when the disassembly vector is empty. Here is a complete breakdown of the visual changes: ![image](https://github.com/user-attachments/assets/af0ce701-9d77-45f1-9a5a-90d68d00bb0d) ### Additional things As expected, the exporter writes every item's `mnemonic` and `operators` to the file, producing an output like this: `example.asm` ```asm .byte 0x7f, 0x45, 0x4c, 0x46 andeq r0, r1, r2, lsl #2 andeq r0, r0, r0 andeq r0, r0, r0 eorseq r0, lr, r3 andeq r0, r0, r1 andeq r1, r0, r0, asr #32 andeq r0, r0, r0 andeq r0, r0, r0, asr #32 ``` --------- Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
2024-12-05 23:04:38 +01:00
continue;
if (instruction.operators.empty())
file.writeString(hex::format("{}\n", instruction.mnemonic));
feat: Added export disassembler results to ASM file (#1987) ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> This PR implements the feature request #1781, that suggests adding a button to export disassembled instructions into an ASM file. ### Implementation description This adds a button to export the current disassembled instructions to an ASM file. Said file is suffixed by an `.asm` extension if not specified at file creation. *Note: the file is written to for every `Disassembly` item in the vector, as it was the easiest and most memory-conservative way of doing it.* The file creation task is implemented based on IPS patch exports, so it fits the same pattern. A `ToastError` is raised when the ASM export could not complete successfully. Translations have been implemented for both `en_US` and `de_DE` for the two new keys: - `hex.disassembler.view.disassembler.export`: file export button - `hex.disassembler.view.disassembler.export.popup.error`: error popup text ### Screenshots The button is disabled when the disassembler is working, or when the disassembly vector is empty. Here is a complete breakdown of the visual changes: ![image](https://github.com/user-attachments/assets/af0ce701-9d77-45f1-9a5a-90d68d00bb0d) ### Additional things As expected, the exporter writes every item's `mnemonic` and `operators` to the file, producing an output like this: `example.asm` ```asm .byte 0x7f, 0x45, 0x4c, 0x46 andeq r0, r1, r2, lsl #2 andeq r0, r0, r0 andeq r0, r0, r0 eorseq r0, lr, r3 andeq r0, r0, r1 andeq r1, r0, r0, asr #32 andeq r0, r0, r0 andeq r0, r0, r0, asr #32 ``` --------- Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
2024-12-05 23:04:38 +01:00
else
file.writeString(hex::format("{} {}\n", instruction.mnemonic, instruction.operators));
feat: Added export disassembler results to ASM file (#1987) ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> This PR implements the feature request #1781, that suggests adding a button to export disassembled instructions into an ASM file. ### Implementation description This adds a button to export the current disassembled instructions to an ASM file. Said file is suffixed by an `.asm` extension if not specified at file creation. *Note: the file is written to for every `Disassembly` item in the vector, as it was the easiest and most memory-conservative way of doing it.* The file creation task is implemented based on IPS patch exports, so it fits the same pattern. A `ToastError` is raised when the ASM export could not complete successfully. Translations have been implemented for both `en_US` and `de_DE` for the two new keys: - `hex.disassembler.view.disassembler.export`: file export button - `hex.disassembler.view.disassembler.export.popup.error`: error popup text ### Screenshots The button is disabled when the disassembler is working, or when the disassembly vector is empty. Here is a complete breakdown of the visual changes: ![image](https://github.com/user-attachments/assets/af0ce701-9d77-45f1-9a5a-90d68d00bb0d) ### Additional things As expected, the exporter writes every item's `mnemonic` and `operators` to the file, producing an output like this: `example.asm` ```asm .byte 0x7f, 0x45, 0x4c, 0x46 andeq r0, r1, r2, lsl #2 andeq r0, r0, r0 andeq r0, r0, r0 eorseq r0, lr, r3 andeq r0, r0, r1 andeq r1, r0, r0, asr #32 andeq r0, r0, r0 andeq r0, r0, r0, asr #32 ``` --------- Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
2024-12-05 23:04:38 +01:00
}
});
});
});
}
2021-02-22 11:56:33 +01:00
void ViewDisassembler::drawContent() {
auto provider = ImHexApi::Provider::get();
if (ImHexApi::Provider::isValid() && provider->isReadable()) {
// Draw region selection picker
ui::regionSelectionPicker(&m_regionToDisassemble, provider, &m_range, true, true);
ImGuiExt::Header("hex.disassembler.view.disassembler.position"_lang);
// Draw base address input
ImGuiExt::InputHexadecimal("hex.disassembler.view.disassembler.image_load_address"_lang, &m_imageLoadAddress, ImGuiInputTextFlags_CharsHexadecimal);
ImGui::SameLine();
ImGuiExt::HelpHover("hex.disassembler.view.disassembler.image_load_address.hint"_lang, ICON_VS_INFO);
// Draw code region start address input
ImGui::BeginDisabled(m_range == ui::RegionType::EntireData);
{
ImGuiExt::InputHexadecimal("hex.disassembler.view.disassembler.image_base_address"_lang, &m_imageBaseAddress, ImGuiInputTextFlags_CharsHexadecimal);
ImGui::SameLine();
ImGuiExt::HelpHover("hex.disassembler.view.disassembler.image_base_address.hint"_lang, ICON_VS_INFO);
}
ImGui::EndDisabled();
// Draw settings
{
ImGuiExt::Header("hex.ui.common.settings"_lang);
// Draw architecture selector
const auto &architectures = ContentRegistry::Disassembler::impl::getArchitectures();
if (architectures.empty()) {
ImGuiExt::TextSpinner("hex.disassembler.view.disassembler.arch"_lang);
} else {
if (m_currArchitecture == nullptr) {
m_currArchitecture = architectures.begin()->second();
}
if (ImGui::BeginCombo("hex.disassembler.view.disassembler.arch"_lang, m_currArchitecture->getName().c_str())) {
for (const auto &[name, creator] : architectures) {
if (ImGui::Selectable(name.c_str(), name == m_currArchitecture->getName())) {
m_currArchitecture = creator();
}
}
ImGui::EndCombo();
}
// Draw sub-settings for each architecture
if (ImGuiExt::BeginBox()) {
m_currArchitecture->drawSettings();
}
ImGuiExt::EndBox();
ui/ux: Rewrite of the entire hex editor view to make it more flexible (#512) * ui/ux: Initial recreation of the hex editor view * ui/ux: Added back support for editing cells * ux: Make scrolling and selecting bytes feel nice again * ui/ux: Improved byte selecting, added footer * sys: Make math evaluator more generic to support integer only calculations * patterns: Moved value formatting into pattern language * ui/ux: Added Goto and Search popups, improved selection * ui: Added better tooltips for bookmarks and patterns * sys: Use worse hex search algorithm on macOS Sadly it still doesn't support `std::boyer_moore_horsepool_searcher` * ui: Added back missing events, menu items and shortcuts * fix: Bookmark highlighting being rendered off by one * fix: Various macOS build errors * fix: size_t is not u64 on macos * fix: std::fmod and std::pow not working with integer types on macos * fix: Missing semicolons * sys: Added proper integer pow function * ui: Added back support for custom encodings * fix: Editor not jumping to selection when selection gets changed * ui: Turn Hexii setting into a data visualizer * sys: Added back remaining shortcuts * sys: Remove old hex editor files * sys: Moved more legacy things away from the hex editor view, updated localization * fix: Hex editor scrolling behaving weirdly and inconsistently * sys: Cleaned up Hex editor code * sys: Added selection color setting, localized all new settings * fix: Search feature not working correctly * ui: Replace custom ImGui::Disabled function with native ImGui ones * ui: Fix bookmark tooltip rendering issues * fix: Another size_t not being 64 bit issue on MacOS
2022-05-27 20:42:07 +02:00
}
}
2021-02-22 11:56:33 +01:00
// Draw disassemble button
ImGui::BeginDisabled(m_disassemblerTask.isRunning() || m_regionToDisassemble.getStartAddress() < m_imageBaseAddress);
{
if (ImGuiExt::DimmedButton("hex.disassembler.view.disassembler.disassemble"_lang))
this->disassemble();
}
ImGui::EndDisabled();
feat: Added export disassembler results to ASM file (#1987) ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> This PR implements the feature request #1781, that suggests adding a button to export disassembled instructions into an ASM file. ### Implementation description This adds a button to export the current disassembled instructions to an ASM file. Said file is suffixed by an `.asm` extension if not specified at file creation. *Note: the file is written to for every `Disassembly` item in the vector, as it was the easiest and most memory-conservative way of doing it.* The file creation task is implemented based on IPS patch exports, so it fits the same pattern. A `ToastError` is raised when the ASM export could not complete successfully. Translations have been implemented for both `en_US` and `de_DE` for the two new keys: - `hex.disassembler.view.disassembler.export`: file export button - `hex.disassembler.view.disassembler.export.popup.error`: error popup text ### Screenshots The button is disabled when the disassembler is working, or when the disassembly vector is empty. Here is a complete breakdown of the visual changes: ![image](https://github.com/user-attachments/assets/af0ce701-9d77-45f1-9a5a-90d68d00bb0d) ### Additional things As expected, the exporter writes every item's `mnemonic` and `operators` to the file, producing an output like this: `example.asm` ```asm .byte 0x7f, 0x45, 0x4c, 0x46 andeq r0, r1, r2, lsl #2 andeq r0, r0, r0 andeq r0, r0, r0 eorseq r0, lr, r3 andeq r0, r0, r1 andeq r1, r0, r0, asr #32 andeq r0, r0, r0 andeq r0, r0, r0, asr #32 ``` --------- Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
2024-12-05 23:04:38 +01:00
// Draw export to file icon button
ImGui::SameLine();
ImGui::BeginDisabled(m_disassemblerTask.isRunning() || m_disassembly.empty());
{
if (ImGuiExt::DimmedIconButton(ICON_VS_EXPORT, ImGui::GetStyleColorVec4(ImGuiCol_Text)))
this->exportToFile();
}
ImGui::EndDisabled();
ImGuiExt::InfoTooltip("hex.disassembler.view.disassembler.export"_lang);
// Draw a spinner if the disassembler is running
2023-12-19 13:10:25 +01:00
if (m_disassemblerTask.isRunning()) {
ImGuiExt::TextSpinner("hex.disassembler.view.disassembler.disassembling"_lang);
}
2021-02-22 11:56:33 +01:00
ImGui::NewLine();
ImGui::TextUnformatted("hex.disassembler.view.disassembler.disassembly.title"_lang);
ImGui::Separator();
// Draw disassembly table
if (ImGui::BeginTable("##disassembly", 4, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable)) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("hex.disassembler.view.disassembler.disassembly.address"_lang);
ImGui::TableSetupColumn("hex.disassembler.view.disassembler.disassembly.offset"_lang);
ImGui::TableSetupColumn("hex.disassembler.view.disassembler.disassembly.bytes"_lang);
ImGui::TableSetupColumn("hex.disassembler.view.disassembler.disassembly.title"_lang);
2023-12-19 13:10:25 +01:00
if (!m_disassemblerTask.isRunning()) {
ImGuiListClipper clipper;
2023-12-19 13:10:25 +01:00
clipper.Begin(m_disassembly.size());
ImGui::TableHeadersRow();
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
2023-12-19 13:10:25 +01:00
const auto &instruction = m_disassembly[i];
ImGui::TableNextRow();
ImGui::TableNextColumn();
// Draw a selectable label for the address
ImGui::PushID(i);
if (ImGui::Selectable("##DisassemblyLine", false, ImGuiSelectableFlags_SpanAllColumns)) {
ImHexApi::HexEditor::setSelection(instruction.offset, instruction.size);
}
ImGui::PopID();
// Draw instruction address
ImGui::SameLine();
ImGuiExt::TextFormatted("0x{0:X}", instruction.address);
// Draw instruction offset
ImGui::TableNextColumn();
ImGuiExt::TextFormatted("0x{0:X}", instruction.offset);
// Draw instruction bytes
ImGui::TableNextColumn();
ImGui::TextUnformatted(instruction.bytes.c_str());
// Draw instruction mnemonic and operands
ImGui::TableNextColumn();
ImGuiExt::TextFormattedColored(ImColor(0xFFD69C56), "{}", instruction.mnemonic);
ImGui::SameLine();
ImGui::TextUnformatted(instruction.operators.c_str());
}
}
clipper.End();
}
ImGui::EndTable();
}
}
}
}