mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:07:36 +00:00
HackStudio: Reorganize debugger-related files
The debugger's files are now placed under HackStudio/Debugger
This commit is contained in:
parent
ce36071447
commit
b9f0f402f4
11 changed files with 127 additions and 62 deletions
37
DevTools/HackStudio/Debugger/BreakpointCallback.h
Normal file
37
DevTools/HackStudio/Debugger/BreakpointCallback.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* 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/Function.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
enum class BreakpointChange {
|
||||
Added,
|
||||
Removed,
|
||||
};
|
||||
|
||||
typedef Function<void(const String& file, size_t line, BreakpointChange)> BreakpointChangeCallback;
|
52
DevTools/HackStudio/Debugger/DebugInfoWidget.cpp
Normal file
52
DevTools/HackStudio/Debugger/DebugInfoWidget.cpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* 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 "DebugInfoWidget.h"
|
||||
#include "Debugger.h"
|
||||
#include "VariablesModel.h"
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Model.h>
|
||||
#include <LibGUI/TableView.h>
|
||||
#include <LibGUI/TreeView.h>
|
||||
|
||||
DebugInfoWidget::DebugInfoWidget()
|
||||
{
|
||||
set_layout<GUI::HorizontalBoxLayout>();
|
||||
m_info_view = add<GUI::TreeView>();
|
||||
m_backtrace_view = add<GUI::TableView>();
|
||||
}
|
||||
|
||||
void DebugInfoWidget::update_state(const PtraceRegisters& regs)
|
||||
{
|
||||
auto model = VariablesModel::create(regs);
|
||||
m_info_view->set_model(model);
|
||||
}
|
||||
|
||||
void DebugInfoWidget::program_stopped()
|
||||
{
|
||||
m_info_view->set_model({});
|
||||
}
|
48
DevTools/HackStudio/Debugger/DebugInfoWidget.h
Normal file
48
DevTools/HackStudio/Debugger/DebugInfoWidget.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* 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/Model.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
#include <sys/arch/i386/regs.h>
|
||||
|
||||
class DebugInfoWidget final : public GUI::Widget {
|
||||
C_OBJECT(DebugInfoWidget)
|
||||
public:
|
||||
virtual ~DebugInfoWidget() override {}
|
||||
|
||||
void update_state(const PtraceRegisters&);
|
||||
void program_stopped();
|
||||
|
||||
private:
|
||||
explicit DebugInfoWidget();
|
||||
|
||||
RefPtr<GUI::TreeView> m_info_view;
|
||||
RefPtr<GUI::TableView> m_backtrace_view;
|
||||
};
|
179
DevTools/HackStudio/Debugger/Debugger.cpp
Normal file
179
DevTools/HackStudio/Debugger/Debugger.cpp
Normal file
|
@ -0,0 +1,179 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* 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 "Debugger.h"
|
||||
|
||||
static Debugger* s_the;
|
||||
|
||||
Debugger& Debugger::the()
|
||||
{
|
||||
ASSERT(s_the);
|
||||
return *s_the;
|
||||
}
|
||||
|
||||
void Debugger::initialize(
|
||||
Function<HasControlPassedToUser(const PtraceRegisters&)> on_stop_callback,
|
||||
Function<void()> on_continue_callback,
|
||||
Function<void()> on_exit_callback)
|
||||
{
|
||||
s_the = new Debugger(move(on_stop_callback), move(on_continue_callback), move(on_exit_callback));
|
||||
}
|
||||
|
||||
bool Debugger::is_initialized()
|
||||
{
|
||||
return s_the;
|
||||
}
|
||||
|
||||
Debugger::Debugger(
|
||||
Function<HasControlPassedToUser(const PtraceRegisters&)> on_stop_callback,
|
||||
Function<void()> on_continue_callback,
|
||||
Function<void()> on_exit_callback)
|
||||
: m_on_stopped_callback(move(on_stop_callback))
|
||||
, m_on_continue_callback(move(on_continue_callback))
|
||||
, m_on_exit_callback(move(on_exit_callback))
|
||||
{
|
||||
pthread_mutex_init(&m_continue_mutex, nullptr);
|
||||
pthread_cond_init(&m_continue_cond, nullptr);
|
||||
}
|
||||
|
||||
void Debugger::on_breakpoint_change(const String& file, size_t line, BreakpointChange change_type)
|
||||
{
|
||||
auto position = create_source_position(file, line);
|
||||
|
||||
if (change_type == BreakpointChange::Added) {
|
||||
Debugger::the().m_breakpoints.append(position);
|
||||
} else {
|
||||
Debugger::the().m_breakpoints.remove_all_matching([&](DebugInfo::SourcePosition val) { return val == position; });
|
||||
}
|
||||
|
||||
auto session = Debugger::the().session();
|
||||
if (!session)
|
||||
return;
|
||||
|
||||
auto address = session->debug_info().get_instruction_from_source(position.file_path, position.line_number);
|
||||
if (!address.has_value())
|
||||
return;
|
||||
|
||||
if (change_type == BreakpointChange::Added) {
|
||||
bool success = session->insert_breakpoint(reinterpret_cast<void*>(address.value()));
|
||||
ASSERT(success);
|
||||
} else {
|
||||
bool success = session->remove_breakpoint(reinterpret_cast<void*>(address.value()));
|
||||
ASSERT(success);
|
||||
}
|
||||
}
|
||||
|
||||
DebugInfo::SourcePosition Debugger::create_source_position(const String& file, size_t line)
|
||||
{
|
||||
return { String::format("./%s", file.characters()), line + 1 };
|
||||
}
|
||||
|
||||
int Debugger::start_static()
|
||||
{
|
||||
Debugger::the().start();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Debugger::start()
|
||||
{
|
||||
m_debug_session = DebugSession::exec_and_attach(m_executable_path);
|
||||
ASSERT(!!m_debug_session);
|
||||
|
||||
for (const auto& breakpoint : m_breakpoints) {
|
||||
dbg() << "insertig breakpoint at: " << breakpoint.file_path << ":" << breakpoint.line_number;
|
||||
auto address = m_debug_session->debug_info().get_instruction_from_source(breakpoint.file_path, breakpoint.line_number);
|
||||
if (address.has_value()) {
|
||||
bool success = m_debug_session->insert_breakpoint(reinterpret_cast<void*>(address.value()));
|
||||
ASSERT(success);
|
||||
} else {
|
||||
dbg() << "couldn't insert breakpoint";
|
||||
}
|
||||
}
|
||||
|
||||
debugger_loop();
|
||||
}
|
||||
|
||||
int Debugger::debugger_loop()
|
||||
{
|
||||
bool in_single_step_mode = false;
|
||||
Vector<void*> temporary_breakpoints;
|
||||
|
||||
m_debug_session->run([&](DebugSession::DebugBreakReason reason, Optional<PtraceRegisters> optional_regs) {
|
||||
if (reason == DebugSession::DebugBreakReason::Exited) {
|
||||
dbg() << "Program exited";
|
||||
m_on_exit_callback();
|
||||
return DebugSession::DebugDecision::Detach;
|
||||
}
|
||||
ASSERT(optional_regs.has_value());
|
||||
const PtraceRegisters& regs = optional_regs.value();
|
||||
|
||||
if (in_single_step_mode) {
|
||||
for (auto address : temporary_breakpoints) {
|
||||
m_debug_session->remove_breakpoint(address);
|
||||
}
|
||||
temporary_breakpoints.clear();
|
||||
in_single_step_mode = false;
|
||||
}
|
||||
|
||||
auto control_passed_to_user = m_on_stopped_callback(regs);
|
||||
|
||||
if (control_passed_to_user == HasControlPassedToUser::Yes) {
|
||||
pthread_mutex_lock(&m_continue_mutex);
|
||||
pthread_cond_wait(&m_continue_cond, &m_continue_mutex);
|
||||
pthread_mutex_unlock(&m_continue_mutex);
|
||||
|
||||
m_on_continue_callback();
|
||||
} else {
|
||||
m_continue_type = ContinueType::Continue;
|
||||
}
|
||||
|
||||
if (m_continue_type == ContinueType::Continue) {
|
||||
return DebugSession::DebugDecision::Continue;
|
||||
}
|
||||
|
||||
if (m_continue_type == ContinueType::SourceSingleStep) {
|
||||
// A possible method for source level single stepping is to single step
|
||||
// in assembly level, until the current instruction's source position has changed.
|
||||
// However, since we do not currently generate debug symbols for library code,
|
||||
// we may have to single-step over lots of library code instructions until we get back to our code,
|
||||
// which is very slow.
|
||||
// So the current method is to insert a temporary breakpoint at every known statement in our source code,
|
||||
// continue execution, and remove the temporary breakpoints once we hit the first breakpoint.
|
||||
m_debug_session->debug_info().for_each_source_position([&](DebugInfo::SourcePosition position) {
|
||||
auto address = (void*)position.address_of_first_statement;
|
||||
if ((u32)address != regs.eip && !m_debug_session->breakpoint_exists(address)) {
|
||||
m_debug_session->insert_breakpoint(address);
|
||||
temporary_breakpoints.append(address);
|
||||
}
|
||||
});
|
||||
in_single_step_mode = true;
|
||||
return DebugSession::DebugDecision::Continue;
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
});
|
||||
m_debug_session.clear();
|
||||
return 0;
|
||||
}
|
97
DevTools/HackStudio/Debugger/Debugger.h
Normal file
97
DevTools/HackStudio/Debugger/Debugger.h
Normal file
|
@ -0,0 +1,97 @@
|
|||
|
||||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* 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 "BreakpointCallback.h"
|
||||
#include <AK/Function.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibDebug/DebugSession.h>
|
||||
#include <LibThread/Lock.h>
|
||||
#include <LibThread/Thread.h>
|
||||
|
||||
class Debugger {
|
||||
public:
|
||||
static Debugger& the();
|
||||
|
||||
enum class HasControlPassedToUser {
|
||||
No,
|
||||
Yes,
|
||||
};
|
||||
|
||||
static void initialize(
|
||||
Function<HasControlPassedToUser(const PtraceRegisters&)> on_stop_callback,
|
||||
Function<void()> on_continue_callback,
|
||||
Function<void()> on_exit_callback);
|
||||
|
||||
static bool is_initialized();
|
||||
|
||||
static void on_breakpoint_change(const String& file, size_t line, BreakpointChange change_type);
|
||||
|
||||
void set_executable_path(const String& path) { m_executable_path = path; }
|
||||
|
||||
DebugSession* session() { return m_debug_session.ptr(); }
|
||||
|
||||
// Thread entry point
|
||||
static int start_static();
|
||||
|
||||
pthread_mutex_t* continue_mutex() { return &m_continue_mutex; }
|
||||
pthread_cond_t* continue_cond() { return &m_continue_cond; }
|
||||
|
||||
enum class ContinueType {
|
||||
Continue,
|
||||
SourceSingleStep,
|
||||
};
|
||||
|
||||
void set_continue_type(ContinueType type) { m_continue_type = type; }
|
||||
void reset_breakpoints() { m_breakpoints.clear(); }
|
||||
|
||||
private:
|
||||
explicit Debugger(
|
||||
Function<HasControlPassedToUser(const PtraceRegisters&)> on_stop_callback,
|
||||
Function<void()> on_continue_callback,
|
||||
Function<void()> on_exit_callback);
|
||||
|
||||
static DebugInfo::SourcePosition create_source_position(const String& file, size_t line);
|
||||
|
||||
void start();
|
||||
int debugger_loop();
|
||||
|
||||
OwnPtr<DebugSession> m_debug_session;
|
||||
|
||||
pthread_mutex_t m_continue_mutex {};
|
||||
pthread_cond_t m_continue_cond {};
|
||||
|
||||
Vector<DebugInfo::SourcePosition> m_breakpoints;
|
||||
String m_executable_path;
|
||||
|
||||
Function<HasControlPassedToUser(const PtraceRegisters&)> m_on_stopped_callback;
|
||||
Function<void()> m_on_continue_callback;
|
||||
Function<void()> m_on_exit_callback;
|
||||
|
||||
ContinueType m_continue_type { ContinueType::Continue };
|
||||
};
|
115
DevTools/HackStudio/Debugger/VariablesModel.cpp
Normal file
115
DevTools/HackStudio/Debugger/VariablesModel.cpp
Normal file
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* 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 "VariablesModel.h"
|
||||
|
||||
GUI::ModelIndex VariablesModel::index(int row, int column, const GUI::ModelIndex& parent_index) const
|
||||
{
|
||||
if (!parent_index.is_valid())
|
||||
return create_index(row, column, &m_variables[row]);
|
||||
auto* parent = static_cast<const DebugInfo::VariableInfo*>(parent_index.internal_data());
|
||||
auto* child = &parent->members[row];
|
||||
return create_index(row, column, child);
|
||||
}
|
||||
|
||||
GUI::ModelIndex VariablesModel::parent_index(const GUI::ModelIndex& index) const
|
||||
{
|
||||
if (!index.is_valid())
|
||||
return {};
|
||||
auto* child = static_cast<const DebugInfo::VariableInfo*>(index.internal_data());
|
||||
auto* parent = child->parent;
|
||||
if (parent == nullptr)
|
||||
return {};
|
||||
|
||||
if (parent->parent == nullptr) {
|
||||
for (size_t row = 0; row < m_variables.size(); row++)
|
||||
if (m_variables.ptr_at(row).ptr() == parent)
|
||||
return create_index(row, 0, parent);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
for (size_t row = 0; row < parent->parent->members.size(); row++) {
|
||||
DebugInfo::VariableInfo* child_at_row = parent->parent->members.ptr_at(row).ptr();
|
||||
if (child_at_row == parent)
|
||||
return create_index(row, 0, parent);
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
int VariablesModel::row_count(const GUI::ModelIndex& index) const
|
||||
{
|
||||
if (!index.is_valid())
|
||||
return m_variables.size();
|
||||
auto* node = static_cast<const DebugInfo::VariableInfo*>(index.internal_data());
|
||||
return node->members.size();
|
||||
}
|
||||
|
||||
String variable_value_as_string(const DebugInfo::VariableInfo& variable)
|
||||
{
|
||||
if (variable.location_type != DebugInfo::VariableInfo::LocationType::Address)
|
||||
return "N/A";
|
||||
|
||||
auto variable_address = variable.location_data.address;
|
||||
|
||||
if (variable.type == "int") {
|
||||
auto value = Debugger::the().session()->peek((u32*)variable_address);
|
||||
ASSERT(value.has_value());
|
||||
return String::format("%d", static_cast<int>(value.value()));
|
||||
}
|
||||
|
||||
if (variable.type == "char") {
|
||||
auto value = Debugger::the().session()->peek((u32*)variable_address);
|
||||
ASSERT(value.has_value());
|
||||
return String::format("'%c' (%d)", static_cast<char>(value.value()), static_cast<char>(value.value()));
|
||||
}
|
||||
|
||||
return String::format("type: %s @ %08x, ", variable.type.characters(), variable_address);
|
||||
}
|
||||
|
||||
GUI::Variant VariablesModel::data(const GUI::ModelIndex& index, Role role) const
|
||||
{
|
||||
auto* variable = static_cast<const DebugInfo::VariableInfo*>(index.internal_data());
|
||||
switch (role) {
|
||||
case Role::Display: {
|
||||
auto value_as_string = variable_value_as_string(*variable);
|
||||
return String::format("%s: %s", variable->name.characters(), value_as_string.characters());
|
||||
}
|
||||
case Role::Icon:
|
||||
return m_variable_icon;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
void VariablesModel::update()
|
||||
{
|
||||
did_update();
|
||||
}
|
||||
|
||||
RefPtr<VariablesModel> VariablesModel::create(const PtraceRegisters& regs)
|
||||
{
|
||||
auto variables = Debugger::the().session()->debug_info().get_variables_in_current_scope(regs);
|
||||
return adopt(*new VariablesModel(move(variables), regs));
|
||||
}
|
56
DevTools/HackStudio/Debugger/VariablesModel.h
Normal file
56
DevTools/HackStudio/Debugger/VariablesModel.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* 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/Model.h>
|
||||
#include <LibGUI/TreeView.h>
|
||||
#include <sys/arch/i386/regs.h>
|
||||
|
||||
class VariablesModel final : public GUI::Model {
|
||||
public:
|
||||
static RefPtr<VariablesModel> create(const PtraceRegisters& regs);
|
||||
|
||||
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
|
||||
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return 1; }
|
||||
virtual GUI::Variant data(const GUI::ModelIndex& index, Role role = Role::Display) const override;
|
||||
virtual void update() override;
|
||||
virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
|
||||
virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& = GUI::ModelIndex()) const override;
|
||||
|
||||
private:
|
||||
explicit VariablesModel(NonnullOwnPtrVector<DebugInfo::VariableInfo>&& variables, const PtraceRegisters& regs)
|
||||
: m_variables(move(variables))
|
||||
, m_regs(regs)
|
||||
{
|
||||
m_variable_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
|
||||
}
|
||||
NonnullOwnPtrVector<DebugInfo::VariableInfo> m_variables;
|
||||
PtraceRegisters m_regs;
|
||||
|
||||
GUI::Icon m_variable_icon;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue