1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 20:07:34 +00:00

HackStudio: Add a disassembly view for the current function in debug mode

This commit is contained in:
Luke 2020-08-25 04:36:55 +01:00 committed by Andreas Kling
parent 694b86a4bf
commit 3ddc42fdf1
6 changed files with 398 additions and 1 deletions

View file

@ -3,6 +3,8 @@ set(SOURCES
Debugger/BacktraceModel.cpp Debugger/BacktraceModel.cpp
Debugger/Debugger.cpp Debugger/Debugger.cpp
Debugger/DebugInfoWidget.cpp Debugger/DebugInfoWidget.cpp
Debugger/DisassemblyModel.cpp
Debugger/DisassemblyWidget.cpp
Debugger/VariablesModel.cpp Debugger/VariablesModel.cpp
Editor.cpp Editor.cpp
EditorWrapper.cpp EditorWrapper.cpp
@ -22,4 +24,4 @@ set(SOURCES
) )
serenity_bin(HackStudio) serenity_bin(HackStudio)
target_link_libraries(HackStudio LibWeb LibMarkdown LibGUI LibGfx LibCore LibVT LibDebug) target_link_libraries(HackStudio LibWeb LibMarkdown LibGUI LibGfx LibCore LibVT LibDebug LibX86)

View file

@ -0,0 +1,133 @@
/*
* Copyright (c) 2020, Luke Wilde <luke.wilde@live.co.uk>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "DisassemblyModel.h"
#include <AK/MappedFile.h>
#include <AK/StringBuilder.h>
#include <LibDebug/DebugSession.h>
#include <LibELF/Loader.h>
#include <LibX86/Disassembler.h>
#include <LibX86/ELFSymbolProvider.h>
#include <ctype.h>
#include <stdio.h>
namespace HackStudio {
DisassemblyModel::DisassemblyModel(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
{
auto containing_function = debug_session.debug_info().get_containing_function(regs.eip);
if (!containing_function.has_value()) {
dbg() << "Cannot disassemble as the containing function was not found.";
return;
}
RefPtr<ELF::Loader> elf_loader;
if (containing_function.value().address_low >= 0xc0000000) {
auto kernel_file = make<MappedFile>("/boot/Kernel");
if (!kernel_file->is_valid())
return;
elf_loader = ELF::Loader::create((const u8*)kernel_file->data(), kernel_file->size());
} else {
elf_loader = debug_session.elf();
}
auto symbol = elf_loader->find_symbol(containing_function.value().address_low);
if (!symbol.has_value())
return;
ASSERT(symbol.has_value());
auto view = symbol.value().raw_data();
X86::ELFSymbolProvider symbol_provider(*elf_loader);
X86::SimpleInstructionStream stream((const u8*)view.characters_without_null_termination(), view.length());
X86::Disassembler disassembler(stream);
size_t offset_into_symbol = 0;
for (;;) {
auto insn = disassembler.next();
if (!insn.has_value())
break;
FlatPtr address_in_profiled_program = symbol.value().value() + offset_into_symbol;
auto disassembly = insn.value().to_string(address_in_profiled_program, &symbol_provider);
StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program });
offset_into_symbol += insn.value().length();
}
}
DisassemblyModel::~DisassemblyModel()
{
}
int DisassemblyModel::row_count(const GUI::ModelIndex&) const
{
return m_instructions.size();
}
String DisassemblyModel::column_name(int column) const
{
switch (column) {
case Column::Address:
return "Address";
case Column::InstructionBytes:
return "Insn Bytes";
case Column::Disassembly:
return "Disassembly";
default:
ASSERT_NOT_REACHED();
return {};
}
}
GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
{
auto& insn = m_instructions[index.row()];
if (role == GUI::ModelRole::Display) {
if (index.column() == Column::Address)
return String::format("%#08x", insn.address);
if (index.column() == Column::InstructionBytes) {
StringBuilder builder;
for (auto ch : insn.bytes) {
builder.appendf("%02x ", (u8)ch);
}
return builder.to_string();
}
if (index.column() == Column::Disassembly)
return insn.disassembly;
return {};
}
return {};
}
void DisassemblyModel::update()
{
did_update();
}
}

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2020, Luke Wilde <luke.wilde@live.co.uk>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Vector.h>
#include <LibGUI/Model.h>
#include <LibX86/Instruction.h>
#include <sys/arch/i386/regs.h>
namespace Debug {
class DebugSession;
}
namespace HackStudio {
struct InstructionData {
X86::Instruction insn;
String disassembly;
StringView bytes;
FlatPtr address { 0 };
};
class DisassemblyModel final : public GUI::Model {
public:
static NonnullRefPtr<DisassemblyModel> create(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
{
return adopt(*new DisassemblyModel(debug_session, regs));
}
enum Column {
Address,
InstructionBytes,
Disassembly,
__Count
};
virtual ~DisassemblyModel() override;
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Count; }
virtual String column_name(int) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual void update() override;
private:
DisassemblyModel(const Debug::DebugSession&, const PtraceRegisters&);
Vector<InstructionData> m_instructions;
};
}

View file

@ -0,0 +1,103 @@
/*
* Copyright (c) 2020, Luke Wilde <luke.wilde@live.co.uk>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "DisassemblyWidget.h"
#include "DisassemblyModel.h"
#include <LibGfx/Palette.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Painter.h>
namespace HackStudio {
void UnavailableDisassemblyWidget::paint_event(GUI::PaintEvent& event)
{
Frame::paint_event(event);
if (reason().is_empty())
return;
GUI::Painter painter(*this);
painter.add_clip_rect(event.rect());
painter.draw_text(frame_inner_rect(), reason(), Gfx::TextAlignment::Center, palette().window_text(), Gfx::TextElision::Right);
}
DisassemblyWidget::DisassemblyWidget()
{
set_layout<GUI::VerticalBoxLayout>();
m_top_container = add<GUI::Widget>();
m_top_container->set_layout<GUI::HorizontalBoxLayout>();
m_top_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_top_container->set_preferred_size(0, 20);
m_function_name_label = m_top_container->add<GUI::Label>("");
m_function_name_label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
m_disassembly_view = add<GUI::TableView>();
m_unavailable_disassembly_widget = add<UnavailableDisassemblyWidget>("");
hide_disassembly("Program isn't running");
}
void DisassemblyWidget::update_state(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
{
m_disassembly_view->set_model(DisassemblyModel::create(debug_session, regs));
if (m_disassembly_view->model()->row_count() > 0) {
auto containing_function = debug_session.debug_info().get_containing_function(regs.eip);
if (containing_function.has_value())
m_function_name_label->set_text(containing_function.value().name);
else
m_function_name_label->set_text("<missing>");
show_disassembly();
} else {
hide_disassembly("No disassembly to show for this function");
}
}
void DisassemblyWidget::program_stopped()
{
m_disassembly_view->set_model({});
m_function_name_label->set_text("");
}
void DisassemblyWidget::show_disassembly()
{
m_top_container->set_visible(true);
m_disassembly_view->set_visible(true);
m_function_name_label->set_visible(true);
m_unavailable_disassembly_widget->set_visible(false);
}
void DisassemblyWidget::hide_disassembly(const String& reason)
{
m_top_container->set_visible(false);
m_disassembly_view->set_visible(false);
m_function_name_label->set_visible(false);
m_unavailable_disassembly_widget->set_visible(true);
m_unavailable_disassembly_widget->set_reason(reason);
}
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2020, Luke Wilde <luke.wilde@live.co.uk>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "Debugger.h"
#include <AK/NonnullOwnPtr.h>
#include <LibGUI/Label.h>
#include <LibGUI/TableView.h>
#include <LibGUI/Model.h>
#include <LibGUI/Widget.h>
#include <sys/arch/i386/regs.h>
namespace HackStudio {
class UnavailableDisassemblyWidget final : public GUI::Frame {
C_OBJECT(UnavailableDisassemblyWidget)
public:
virtual ~UnavailableDisassemblyWidget() override { }
const String& reason() const { return m_reason; }
void set_reason(const String& text) { m_reason = text; }
private:
UnavailableDisassemblyWidget(const String& reason)
: m_reason(reason)
{
}
virtual void paint_event(GUI::PaintEvent& event) override;
String m_reason;
};
class DisassemblyWidget final : public GUI::Widget {
C_OBJECT(DisassemblyWidget)
public:
virtual ~DisassemblyWidget() override { }
void update_state(const Debug::DebugSession&, const PtraceRegisters&);
void program_stopped();
private:
DisassemblyWidget();
void show_disassembly();
void hide_disassembly(const String&);
RefPtr<GUI::Widget> m_top_container;
RefPtr<GUI::TableView> m_disassembly_view;
RefPtr<GUI::Label> m_function_name_label;
RefPtr<UnavailableDisassemblyWidget> m_unavailable_disassembly_widget;
};
}

View file

@ -27,6 +27,7 @@
#include "CursorTool.h" #include "CursorTool.h"
#include "Debugger/DebugInfoWidget.h" #include "Debugger/DebugInfoWidget.h"
#include "Debugger/Debugger.h" #include "Debugger/Debugger.h"
#include "Debugger/DisassemblyWidget.h"
#include "Editor.h" #include "Editor.h"
#include "EditorWrapper.h" #include "EditorWrapper.h"
#include "FindInFilesWidget.h" #include "FindInFilesWidget.h"
@ -540,6 +541,7 @@ int main_impl(int argc, char** argv)
auto& find_in_files_widget = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files"); auto& find_in_files_widget = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
auto& terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Build", false); auto& terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Build", false);
auto& debug_info_widget = s_action_tab_widget->add_tab<DebugInfoWidget>("Debug"); auto& debug_info_widget = s_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
auto& disassembly_widget = s_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
auto& locator = widget.add<Locator>(); auto& locator = widget.add<Locator>();
@ -630,6 +632,7 @@ int main_impl(int argc, char** argv)
current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1); current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
debug_info_widget.update_state(*Debugger::the().session(), regs); debug_info_widget.update_state(*Debugger::the().session(), regs);
debug_info_widget.set_debug_actions_enabled(true); debug_info_widget.set_debug_actions_enabled(true);
disassembly_widget.update_state(*Debugger::the().session(), regs);
reveal_action_tab(debug_info_widget); reveal_action_tab(debug_info_widget);
})); }));
Core::EventLoop::wake(); Core::EventLoop::wake();
@ -648,6 +651,7 @@ int main_impl(int argc, char** argv)
[&]() { [&]() {
Core::EventLoop::main().post_event(*g_window, make<Core::DeferredInvocationEvent>([&](auto&) { Core::EventLoop::main().post_event(*g_window, make<Core::DeferredInvocationEvent>([&](auto&) {
debug_info_widget.program_stopped(); debug_info_widget.program_stopped();
disassembly_widget.program_stopped();
hide_action_tabs(); hide_action_tabs();
GUI::MessageBox::show(g_window, "Program Exited", "Debugger", GUI::MessageBox::Type::Information); GUI::MessageBox::show(g_window, "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
})); }));