DebugInfoWidget.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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::load_from_file("/res/icons/16x16/debug-continue.png"), [](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::load_from_file("/res/icons/16x16/debug-step-over.png"), [](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::load_from_file("/res/icons/16x16/debug-step-in.png"), [](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::load_from_file("/res/icons/16x16/debug-step-out.png"), [](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. auto& model = static_cast<BacktraceModel&>(*m_backtrace_view->model());
  60. // Note: The reconstruction of the register set here is obviously incomplete.
  61. // We currently only reconstruct eip & ebp. Ideally would also reconstruct the other registers somehow.
  62. // (Other registers may be needed to get the values of variables who are not stored on the stack)
  63. PtraceRegisters frame_regs {};
  64. frame_regs.eip = model.frames()[index.row()].instruction_address;
  65. frame_regs.ebp = model.frames()[index.row()].frame_base;
  66. m_variables_view->set_model(VariablesModel::create(frame_regs));
  67. };
  68. }
  69. bool DebugInfoWidget::does_variable_support_writing(const Debug::DebugInfo::VariableInfo* variable)
  70. {
  71. if (variable->location_type != Debug::DebugInfo::VariableInfo::LocationType::Address)
  72. return false;
  73. return variable->is_enum_type() || variable->type_name.is_one_of("int", "bool");
  74. }
  75. RefPtr<GUI::Menu> DebugInfoWidget::get_context_menu_for_variable(const GUI::ModelIndex& index)
  76. {
  77. if (!index.is_valid())
  78. return nullptr;
  79. auto context_menu = GUI::Menu::construct();
  80. auto* variable = static_cast<const Debug::DebugInfo::VariableInfo*>(index.internal_data());
  81. if (does_variable_support_writing(variable)) {
  82. context_menu->add_action(GUI::Action::create("Change value", [&](auto&) {
  83. String value;
  84. if (GUI::InputBox::show(window(), value, "Enter new value:", "Set variable value") == GUI::InputBox::ExecOK) {
  85. auto& model = static_cast<VariablesModel&>(*m_variables_view->model());
  86. model.set_variable_value(index, value, window());
  87. }
  88. }));
  89. }
  90. auto variable_address = (u32*)variable->location_data.address;
  91. if (Debugger::the().session()->watchpoint_exists(variable_address)) {
  92. context_menu->add_action(GUI::Action::create("Remove watchpoint", [variable_address](auto&) {
  93. Debugger::the().session()->remove_watchpoint(variable_address);
  94. }));
  95. } else {
  96. auto& backtrace_model = static_cast<BacktraceModel&>(*m_backtrace_view->model());
  97. auto current_frame = m_backtrace_view->selection().first().row();
  98. auto ebp = backtrace_model.frames()[current_frame].frame_base;
  99. context_menu->add_action(GUI::Action::create("Add watchpoint", [variable_address, ebp](auto&) {
  100. Debugger::the().session()->insert_watchpoint(variable_address, ebp);
  101. }));
  102. }
  103. return context_menu;
  104. }
  105. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_variables_tab()
  106. {
  107. auto variables_widget = GUI::Widget::construct();
  108. variables_widget->set_layout<GUI::HorizontalBoxLayout>();
  109. m_variables_view = variables_widget->add<GUI::TreeView>();
  110. m_variables_view->on_context_menu_request = [this](auto& index, auto& event) {
  111. m_variable_context_menu = get_context_menu_for_variable(index);
  112. if (m_variable_context_menu)
  113. m_variable_context_menu->popup(event.screen_position());
  114. };
  115. return variables_widget;
  116. }
  117. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_registers_tab()
  118. {
  119. auto registers_widget = GUI::Widget::construct();
  120. registers_widget->set_layout<GUI::HorizontalBoxLayout>();
  121. m_registers_view = registers_widget->add<GUI::TableView>();
  122. return registers_widget;
  123. }
  124. void DebugInfoWidget::update_state(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
  125. {
  126. m_variables_view->set_model(VariablesModel::create(regs));
  127. m_backtrace_view->set_model(BacktraceModel::create(debug_session, regs));
  128. if (m_registers_view->model()) {
  129. auto& previous_registers = static_cast<RegistersModel*>(m_registers_view->model())->raw_registers();
  130. m_registers_view->set_model(RegistersModel::create(regs, previous_registers));
  131. } else {
  132. m_registers_view->set_model(RegistersModel::create(regs));
  133. }
  134. auto selected_index = m_backtrace_view->model()->index(0);
  135. if (!selected_index.is_valid()) {
  136. dbgln("Warning: DebugInfoWidget: backtrace selected index is invalid");
  137. return;
  138. }
  139. m_backtrace_view->selection().set(selected_index);
  140. }
  141. void DebugInfoWidget::program_stopped()
  142. {
  143. m_variables_view->set_model({});
  144. m_backtrace_view->set_model({});
  145. m_registers_view->set_model({});
  146. }
  147. void DebugInfoWidget::set_debug_actions_enabled(bool enabled)
  148. {
  149. m_continue_action->set_enabled(enabled);
  150. m_singlestep_action->set_enabled(enabled);
  151. m_step_in_action->set_enabled(enabled);
  152. m_step_out_action->set_enabled(enabled);
  153. }
  154. }