DebugInfoWidget.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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("Variables", build_variables_tab());
  55. variables_tab_widget.add_widget("Registers", 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. frame_regs.set_ip(model.frames()[index.row()].instruction_address);
  67. frame_regs.set_bp(model.frames()[index.row()].frame_base);
  68. m_variables_view->set_model(VariablesModel::create(frame_regs));
  69. };
  70. }
  71. bool DebugInfoWidget::does_variable_support_writing(const Debug::DebugInfo::VariableInfo* variable)
  72. {
  73. if (variable->location_type != Debug::DebugInfo::VariableInfo::LocationType::Address)
  74. return false;
  75. return variable->is_enum_type() || variable->type_name.is_one_of("int", "bool");
  76. }
  77. RefPtr<GUI::Menu> DebugInfoWidget::get_context_menu_for_variable(const GUI::ModelIndex& index)
  78. {
  79. if (!index.is_valid())
  80. return nullptr;
  81. auto context_menu = GUI::Menu::construct();
  82. auto* variable = static_cast<const Debug::DebugInfo::VariableInfo*>(index.internal_data());
  83. if (does_variable_support_writing(variable)) {
  84. context_menu->add_action(GUI::Action::create("Change value", [&](auto&) {
  85. String value;
  86. if (GUI::InputBox::show(window(), value, "Enter new value:", "Set variable value") == GUI::InputBox::ExecOK) {
  87. auto& model = static_cast<VariablesModel&>(*m_variables_view->model());
  88. model.set_variable_value(index, value, window());
  89. }
  90. }));
  91. }
  92. auto variable_address = (FlatPtr*)variable->location_data.address;
  93. if (Debugger::the().session()->watchpoint_exists(variable_address)) {
  94. context_menu->add_action(GUI::Action::create("Remove watchpoint", [variable_address](auto&) {
  95. Debugger::the().session()->remove_watchpoint(variable_address);
  96. }));
  97. } else {
  98. auto& backtrace_model = static_cast<BacktraceModel&>(*m_backtrace_view->model());
  99. auto current_frame = m_backtrace_view->selection().first().row();
  100. auto ebp = backtrace_model.frames()[current_frame].frame_base;
  101. context_menu->add_action(GUI::Action::create("Add watchpoint", [variable_address, ebp](auto&) {
  102. Debugger::the().session()->insert_watchpoint(variable_address, ebp);
  103. }));
  104. }
  105. return context_menu;
  106. }
  107. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_variables_tab()
  108. {
  109. auto variables_widget = GUI::Widget::construct();
  110. variables_widget->set_layout<GUI::HorizontalBoxLayout>();
  111. m_variables_view = variables_widget->add<GUI::TreeView>();
  112. m_variables_view->on_context_menu_request = [this](auto& index, auto& event) {
  113. m_variable_context_menu = get_context_menu_for_variable(index);
  114. if (m_variable_context_menu)
  115. m_variable_context_menu->popup(event.screen_position());
  116. };
  117. return variables_widget;
  118. }
  119. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_registers_tab()
  120. {
  121. auto registers_widget = GUI::Widget::construct();
  122. registers_widget->set_layout<GUI::HorizontalBoxLayout>();
  123. m_registers_view = registers_widget->add<GUI::TableView>();
  124. return registers_widget;
  125. }
  126. void DebugInfoWidget::update_state(const Debug::ProcessInspector& inspector, const PtraceRegisters& regs)
  127. {
  128. m_variables_view->set_model(VariablesModel::create(regs));
  129. m_backtrace_view->set_model(BacktraceModel::create(inspector, regs));
  130. if (m_registers_view->model()) {
  131. auto& previous_registers = static_cast<RegistersModel*>(m_registers_view->model())->raw_registers();
  132. m_registers_view->set_model(RegistersModel::create(regs, previous_registers));
  133. } else {
  134. m_registers_view->set_model(RegistersModel::create(regs));
  135. }
  136. auto selected_index = m_backtrace_view->model()->index(0);
  137. if (!selected_index.is_valid()) {
  138. dbgln("Warning: DebugInfoWidget: backtrace selected index is invalid");
  139. return;
  140. }
  141. m_backtrace_view->selection().set(selected_index);
  142. }
  143. void DebugInfoWidget::program_stopped()
  144. {
  145. m_variables_view->set_model({});
  146. m_backtrace_view->set_model({});
  147. m_registers_view->set_model({});
  148. }
  149. void DebugInfoWidget::set_debug_actions_enabled(bool enabled)
  150. {
  151. m_continue_action->set_enabled(enabled);
  152. m_singlestep_action->set_enabled(enabled);
  153. m_step_in_action->set_enabled(enabled);
  154. m_step_out_action->set_enabled(enabled);
  155. }
  156. }