DebugInfoWidget.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DebugInfoWidget.h"
  7. #include "BacktraceModel.h"
  8. #include "Debugger.h"
  9. #include "RegistersModel.h"
  10. #include "VariablesModel.h"
  11. #include <AK/StringBuilder.h>
  12. #include <LibGUI/Action.h>
  13. #include <LibGUI/BoxLayout.h>
  14. #include <LibGUI/InputBox.h>
  15. #include <LibGUI/ListView.h>
  16. #include <LibGUI/Menu.h>
  17. #include <LibGUI/Model.h>
  18. #include <LibGUI/Splitter.h>
  19. #include <LibGUI/TabWidget.h>
  20. #include <LibGUI/TreeView.h>
  21. namespace HackStudio {
  22. void DebugInfoWidget::init_toolbar()
  23. {
  24. m_continue_action = GUI::Action::create("Continue", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/debug-continue.png").release_value_but_fixme_should_propagate_errors(), [](auto&) {
  25. Debugger::the().set_requested_debugger_action(Debugger::DebuggerAction::Continue);
  26. });
  27. m_singlestep_action = GUI::Action::create("Step Over", { Mod_None, Key_F10 }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/debug-step-over.png").release_value_but_fixme_should_propagate_errors(), [](auto&) {
  28. Debugger::the().set_requested_debugger_action(Debugger::DebuggerAction::SourceStepOver);
  29. });
  30. m_step_in_action = GUI::Action::create("Step In", { Mod_None, Key_F11 }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/debug-step-in.png").release_value_but_fixme_should_propagate_errors(), [](auto&) {
  31. Debugger::the().set_requested_debugger_action(Debugger::DebuggerAction::SourceSingleStep);
  32. });
  33. m_step_out_action = GUI::Action::create("Step Out", { Mod_Shift, Key_F11 }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/debug-step-out.png").release_value_but_fixme_should_propagate_errors(), [](auto&) {
  34. Debugger::the().set_requested_debugger_action(Debugger::DebuggerAction::SourceStepOut);
  35. });
  36. m_toolbar->add_action(*m_continue_action);
  37. m_toolbar->add_action(*m_singlestep_action);
  38. m_toolbar->add_action(*m_step_in_action);
  39. m_toolbar->add_action(*m_step_out_action);
  40. set_debug_actions_enabled(false);
  41. }
  42. DebugInfoWidget::DebugInfoWidget()
  43. {
  44. set_layout<GUI::VerticalBoxLayout>();
  45. auto& toolbar_container = add<GUI::ToolbarContainer>();
  46. m_toolbar = toolbar_container.add<GUI::Toolbar>();
  47. init_toolbar();
  48. auto& bottom_box = add<GUI::Widget>();
  49. bottom_box.set_layout<GUI::HorizontalBoxLayout>();
  50. auto& splitter = bottom_box.add<GUI::HorizontalSplitter>();
  51. m_backtrace_view = splitter.add<GUI::ListView>();
  52. auto& variables_tab_widget = splitter.add<GUI::TabWidget>();
  53. variables_tab_widget.set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  54. variables_tab_widget.add_widget(build_variables_tab());
  55. variables_tab_widget.add_widget(build_registers_tab());
  56. m_backtrace_view->on_selection_change = [this] {
  57. const auto& index = m_backtrace_view->selection().first();
  58. if (!index.is_valid()) {
  59. return;
  60. }
  61. auto& model = static_cast<BacktraceModel&>(*m_backtrace_view->model());
  62. // Note: The reconstruction of the register set here is obviously incomplete.
  63. // We currently only reconstruct eip & ebp. Ideally would also reconstruct the other registers somehow.
  64. // (Other registers may be needed to get the values of variables who are not stored on the stack)
  65. PtraceRegisters frame_regs {};
  66. auto backtrace_frame = model.frames()[index.row()];
  67. frame_regs.set_ip(backtrace_frame.instruction_address);
  68. frame_regs.set_bp(backtrace_frame.frame_base);
  69. m_variables_view->set_model(VariablesModel::create(static_cast<VariablesModel&>(*m_variables_view->model()).inspector(), frame_regs));
  70. if (on_backtrace_frame_selection && backtrace_frame.m_source_position.has_value()) {
  71. on_backtrace_frame_selection(*backtrace_frame.m_source_position);
  72. } else {
  73. dbgln("no source position info");
  74. }
  75. };
  76. }
  77. bool DebugInfoWidget::does_variable_support_writing(Debug::DebugInfo::VariableInfo const* variable)
  78. {
  79. if (variable->location_type != Debug::DebugInfo::VariableInfo::LocationType::Address)
  80. return false;
  81. return variable->is_enum_type() || variable->type_name.is_one_of("int", "bool");
  82. }
  83. RefPtr<GUI::Menu> DebugInfoWidget::get_context_menu_for_variable(const GUI::ModelIndex& index)
  84. {
  85. if (!index.is_valid())
  86. return nullptr;
  87. auto context_menu = GUI::Menu::construct();
  88. auto* variable = static_cast<Debug::DebugInfo::VariableInfo const*>(index.internal_data());
  89. if (does_variable_support_writing(variable)) {
  90. context_menu->add_action(GUI::Action::create("Change value", [&](auto&) {
  91. String value;
  92. if (GUI::InputBox::show(window(), value, "Enter new value:", "Set variable value") == GUI::InputBox::ExecResult::OK) {
  93. auto& model = static_cast<VariablesModel&>(*m_variables_view->model());
  94. model.set_variable_value(index, value, window());
  95. }
  96. }));
  97. }
  98. auto variable_address = variable->location_data.address;
  99. if (Debugger::the().session()->watchpoint_exists(variable_address)) {
  100. context_menu->add_action(GUI::Action::create("Remove watchpoint", [variable_address](auto&) {
  101. Debugger::the().session()->remove_watchpoint(variable_address);
  102. }));
  103. } else {
  104. auto& backtrace_model = static_cast<BacktraceModel&>(*m_backtrace_view->model());
  105. auto current_frame = m_backtrace_view->selection().first().row();
  106. auto ebp = backtrace_model.frames()[current_frame].frame_base;
  107. context_menu->add_action(GUI::Action::create("Add watchpoint", [variable_address, ebp](auto&) {
  108. Debugger::the().session()->insert_watchpoint(variable_address, ebp);
  109. }));
  110. }
  111. return context_menu;
  112. }
  113. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_variables_tab()
  114. {
  115. auto variables_widget = GUI::Widget::construct();
  116. variables_widget->set_title("Variables");
  117. variables_widget->set_layout<GUI::HorizontalBoxLayout>();
  118. m_variables_view = variables_widget->add<GUI::TreeView>();
  119. m_variables_view->on_context_menu_request = [this](auto& index, auto& event) {
  120. m_variable_context_menu = get_context_menu_for_variable(index);
  121. if (m_variable_context_menu)
  122. m_variable_context_menu->popup(event.screen_position());
  123. };
  124. return variables_widget;
  125. }
  126. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_registers_tab()
  127. {
  128. auto registers_widget = GUI::Widget::construct();
  129. registers_widget->set_title("Registers");
  130. registers_widget->set_layout<GUI::HorizontalBoxLayout>();
  131. m_registers_view = registers_widget->add<GUI::TableView>();
  132. return registers_widget;
  133. }
  134. void DebugInfoWidget::update_state(Debug::ProcessInspector& inspector, PtraceRegisters const& regs)
  135. {
  136. m_variables_view->set_model(VariablesModel::create(inspector, regs));
  137. m_backtrace_view->set_model(BacktraceModel::create(inspector, regs));
  138. if (m_registers_view->model()) {
  139. auto& previous_registers = static_cast<RegistersModel*>(m_registers_view->model())->raw_registers();
  140. m_registers_view->set_model(RegistersModel::create(regs, previous_registers));
  141. } else {
  142. m_registers_view->set_model(RegistersModel::create(regs));
  143. }
  144. auto selected_index = m_backtrace_view->model()->index(0);
  145. if (!selected_index.is_valid()) {
  146. dbgln("Warning: DebugInfoWidget: backtrace selected index is invalid");
  147. return;
  148. }
  149. m_backtrace_view->selection().set(selected_index);
  150. }
  151. void DebugInfoWidget::program_stopped()
  152. {
  153. m_variables_view->set_model({});
  154. m_backtrace_view->set_model({});
  155. m_registers_view->set_model({});
  156. }
  157. void DebugInfoWidget::set_debug_actions_enabled(bool enabled)
  158. {
  159. m_continue_action->set_enabled(enabled);
  160. m_singlestep_action->set_enabled(enabled);
  161. m_step_in_action->set_enabled(enabled);
  162. m_step_out_action->set_enabled(enabled);
  163. }
  164. }