DebugInfoWidget.cpp 7.4 KB

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