1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "VariablesModel.h"
#include <LibGUI/Application.h>
#include <LibGUI/MessageBox.h>
namespace HackStudio {
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 Debug::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 Debug::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);
VERIFY_NOT_REACHED();
}
for (size_t row = 0; row < parent->parent->members.size(); row++) {
Debug::DebugInfo::VariableInfo* child_at_row = parent->parent->members.ptr_at(row).ptr();
if (child_at_row == parent)
return create_index(row, 0, parent);
}
VERIFY_NOT_REACHED();
}
int VariablesModel::row_count(const GUI::ModelIndex& index) const
{
if (!index.is_valid())
return m_variables.size();
auto* node = static_cast<const Debug::DebugInfo::VariableInfo*>(index.internal_data());
return node->members.size();
}
static String variable_value_as_string(const Debug::DebugInfo::VariableInfo& variable)
{
if (variable.location_type != Debug::DebugInfo::VariableInfo::LocationType::Address)
return "N/A";
auto variable_address = variable.location_data.address;
if (variable.is_enum_type()) {
auto value = Debugger::the().session()->peek((u32*)variable_address);
VERIFY(value.has_value());
auto it = variable.type->members.find_if([&enumerator_value = value.value()](const auto& enumerator) {
return enumerator->constant_data.as_u32 == enumerator_value;
});
if (it.is_end())
return String::formatted("Unknown ({})", value.value());
return String::formatted("{}::{}", variable.type_name, (*it)->name);
}
if (variable.type_name == "int") {
auto value = Debugger::the().session()->peek((u32*)variable_address);
VERIFY(value.has_value());
return String::formatted("{}", static_cast<int>(value.value()));
}
if (variable.type_name == "char") {
auto value = Debugger::the().session()->peek((u32*)variable_address);
VERIFY(value.has_value());
return String::formatted("'{0:c}'", (char)value.value());
}
if (variable.type_name == "bool") {
auto value = Debugger::the().session()->peek((u32*)variable_address);
VERIFY(value.has_value());
return (value.value() & 1) ? "true" : "false";
}
return String::formatted("type: {} @ {:p}, ", variable.type_name, variable_address);
}
static Optional<u32> string_to_variable_value(const StringView& string_value, const Debug::DebugInfo::VariableInfo& variable)
{
if (variable.is_enum_type()) {
auto prefix_string = String::formatted("{}::", variable.type_name);
auto string_to_use = string_value;
if (string_value.starts_with(prefix_string))
string_to_use = string_value.substring_view(prefix_string.length(), string_value.length() - prefix_string.length());
auto it = variable.type->members.find_if([string_to_use](const auto& enumerator) {
return enumerator->name == string_to_use;
});
if (it.is_end())
return {};
return (*it)->constant_data.as_u32;
}
if (variable.type_name == "int") {
auto value = string_value.to_int();
if (value.has_value())
return value.value();
return {};
}
if (variable.type_name == "bool") {
if (string_value == "true")
return true;
if (string_value == "false")
return false;
return {};
}
return {};
}
void VariablesModel::set_variable_value(const GUI::ModelIndex& index, const StringView& string_value, GUI::Window* parent_window)
{
auto variable = static_cast<const Debug::DebugInfo::VariableInfo*>(index.internal_data());
auto value = string_to_variable_value(string_value, *variable);
if (value.has_value()) {
auto success = Debugger::the().session()->poke((u32*)variable->location_data.address, value.value());
VERIFY(success);
return;
}
GUI::MessageBox::show(
parent_window,
String::formatted("String value \"{}\" could not be converted to a value of type {}.", string_value, variable->type_name),
"Set value failed",
GUI::MessageBox::Type::Error);
}
GUI::Variant VariablesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
{
auto* variable = static_cast<const Debug::DebugInfo::VariableInfo*>(index.internal_data());
switch (role) {
case GUI::ModelRole::Display: {
auto value_as_string = variable_value_as_string(*variable);
return String::formatted("{}: {}", variable->name, value_as_string);
}
case GUI::ModelRole::Icon:
return m_variable_icon;
default:
return {};
}
}
void VariablesModel::update()
{
did_update();
}
RefPtr<VariablesModel> VariablesModel::create(const PtraceRegisters& regs)
{
auto lib = Debugger::the().session()->library_at(regs.eip);
if (!lib)
return nullptr;
auto variables = lib->debug_info->get_variables_in_current_scope(regs);
return adopt(*new VariablesModel(move(variables), regs));
}
}
|