DebugInfoWidget.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "DebugInfoWidget.h"
  27. #include "BacktraceModel.h"
  28. #include "Debugger.h"
  29. #include "RegistersModel.h"
  30. #include "VariablesModel.h"
  31. #include <AK/StringBuilder.h>
  32. #include <LibGUI/Action.h>
  33. #include <LibGUI/BoxLayout.h>
  34. #include <LibGUI/InputBox.h>
  35. #include <LibGUI/Layout.h>
  36. #include <LibGUI/ListView.h>
  37. #include <LibGUI/Menu.h>
  38. #include <LibGUI/Model.h>
  39. #include <LibGUI/Splitter.h>
  40. #include <LibGUI/TabWidget.h>
  41. #include <LibGUI/TreeView.h>
  42. namespace HackStudio {
  43. void DebugInfoWidget::init_toolbar()
  44. {
  45. m_continue_action = GUI::Action::create("Continue", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-continue.png"), [](auto&) {
  46. Debugger::the().set_requested_debugger_action(Debugger::DebuggerAction::Continue);
  47. });
  48. m_singlestep_action = GUI::Action::create("Step Over", { Mod_None, Key_F10 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-step-over.png"), [](auto&) {
  49. Debugger::the().set_requested_debugger_action(Debugger::DebuggerAction::SourceStepOver);
  50. });
  51. m_step_in_action = GUI::Action::create("Step In", { Mod_None, Key_F11 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-step-in.png"), [](auto&) {
  52. Debugger::the().set_requested_debugger_action(Debugger::DebuggerAction::SourceSingleStep);
  53. });
  54. m_step_out_action = GUI::Action::create("Step Out", { Mod_Shift, Key_F11 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-step-out.png"), [](auto&) {
  55. Debugger::the().set_requested_debugger_action(Debugger::DebuggerAction::SourceStepOut);
  56. });
  57. m_toolbar->add_action(*m_continue_action);
  58. m_toolbar->add_action(*m_singlestep_action);
  59. m_toolbar->add_action(*m_step_in_action);
  60. m_toolbar->add_action(*m_step_out_action);
  61. set_debug_actions_enabled(false);
  62. }
  63. DebugInfoWidget::DebugInfoWidget()
  64. {
  65. set_layout<GUI::VerticalBoxLayout>();
  66. auto& toolbar_container = add<GUI::ToolBarContainer>();
  67. m_toolbar = toolbar_container.add<GUI::ToolBar>();
  68. init_toolbar();
  69. auto& bottom_box = add<GUI::Widget>();
  70. bottom_box.set_layout<GUI::HorizontalBoxLayout>();
  71. auto& splitter = bottom_box.add<GUI::HorizontalSplitter>();
  72. m_backtrace_view = splitter.add<GUI::ListView>();
  73. auto& variables_tab_widget = splitter.add<GUI::TabWidget>();
  74. variables_tab_widget.set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  75. variables_tab_widget.add_widget("Variables", build_variables_tab());
  76. variables_tab_widget.add_widget("Registers", build_registers_tab());
  77. m_backtrace_view->on_selection = [this](auto& index) {
  78. auto& model = static_cast<BacktraceModel&>(*m_backtrace_view->model());
  79. // Note: The reconstruction of the register set here is obviously incomplete.
  80. // We currently only reconstruct eip & ebp. Ideally would also reconstruct the other registers somehow.
  81. // (Other registers may be needed to get the values of variables who are not stored on the stack)
  82. PtraceRegisters frame_regs {};
  83. frame_regs.eip = model.frames()[index.row()].instruction_address;
  84. frame_regs.ebp = model.frames()[index.row()].frame_base;
  85. m_variables_view->set_model(VariablesModel::create(frame_regs));
  86. };
  87. }
  88. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_variables_tab()
  89. {
  90. auto variables_widget = GUI::Widget::construct();
  91. variables_widget->set_layout<GUI::HorizontalBoxLayout>();
  92. m_variables_view = variables_widget->add<GUI::TreeView>();
  93. auto is_valid_index = [](auto& index) {
  94. if (!index.is_valid())
  95. return false;
  96. auto* variable = static_cast<const Debug::DebugInfo::VariableInfo*>(index.internal_data());
  97. if (variable->location_type != Debug::DebugInfo::VariableInfo::LocationType::Address)
  98. return false;
  99. return variable->is_enum_type() || variable->type_name.is_one_of("int", "bool");
  100. };
  101. m_variables_view->on_context_menu_request = [this, is_valid_index](auto& index, auto& event) {
  102. if (!is_valid_index(index))
  103. return;
  104. m_variable_context_menu->popup(event.screen_position());
  105. };
  106. m_variables_view->on_activation = [this, is_valid_index](auto& index) {
  107. if (!is_valid_index(index))
  108. return;
  109. String value;
  110. if (GUI::InputBox::show(value, window(), "Enter new value:", "Set variable value") == GUI::InputBox::ExecOK) {
  111. auto& model = static_cast<VariablesModel&>(*m_variables_view->model());
  112. model.set_variable_value(index, value, window());
  113. }
  114. };
  115. auto edit_variable_action = GUI::Action::create("Change value", [this](auto&) {
  116. m_variables_view->on_activation(m_variables_view->selection().first());
  117. });
  118. m_variable_context_menu = GUI::Menu::construct();
  119. m_variable_context_menu->add_action(edit_variable_action);
  120. return variables_widget;
  121. }
  122. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_registers_tab()
  123. {
  124. auto registers_widget = GUI::Widget::construct();
  125. registers_widget->set_layout<GUI::HorizontalBoxLayout>();
  126. m_registers_view = registers_widget->add<GUI::TableView>();
  127. return registers_widget;
  128. }
  129. void DebugInfoWidget::update_state(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
  130. {
  131. m_variables_view->set_model(VariablesModel::create(regs));
  132. m_backtrace_view->set_model(BacktraceModel::create(debug_session, regs));
  133. if (m_registers_view->model()) {
  134. auto& previous_registers = static_cast<RegistersModel*>(m_registers_view->model())->raw_registers();
  135. m_registers_view->set_model(RegistersModel::create(regs, previous_registers));
  136. } else {
  137. m_registers_view->set_model(RegistersModel::create(regs));
  138. }
  139. auto selected_index = m_backtrace_view->model()->index(0);
  140. if (!selected_index.is_valid()) {
  141. dbgln("Warning: DebugInfoWidget: backtrace selected index is invalid");
  142. return;
  143. }
  144. m_backtrace_view->selection().set(selected_index);
  145. }
  146. void DebugInfoWidget::program_stopped()
  147. {
  148. m_variables_view->set_model({});
  149. m_backtrace_view->set_model({});
  150. m_registers_view->set_model({});
  151. }
  152. void DebugInfoWidget::set_debug_actions_enabled(bool enabled)
  153. {
  154. m_continue_action->set_enabled(enabled);
  155. m_singlestep_action->set_enabled(enabled);
  156. m_step_in_action->set_enabled(enabled);
  157. m_step_out_action->set_enabled(enabled);
  158. }
  159. }