DebugInfoWidget.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. pthread_mutex_lock(Debugger::the().continue_mutex());
  47. Debugger::the().set_continue_type(Debugger::ContinueType::Continue);
  48. pthread_cond_signal(Debugger::the().continue_cond());
  49. pthread_mutex_unlock(Debugger::the().continue_mutex());
  50. });
  51. 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&) {
  52. pthread_mutex_lock(Debugger::the().continue_mutex());
  53. Debugger::the().set_continue_type(Debugger::ContinueType::SourceStepOver);
  54. pthread_cond_signal(Debugger::the().continue_cond());
  55. pthread_mutex_unlock(Debugger::the().continue_mutex());
  56. });
  57. 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&) {
  58. pthread_mutex_lock(Debugger::the().continue_mutex());
  59. Debugger::the().set_continue_type(Debugger::ContinueType::SourceSingleStep);
  60. pthread_cond_signal(Debugger::the().continue_cond());
  61. pthread_mutex_unlock(Debugger::the().continue_mutex());
  62. });
  63. 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&) {
  64. pthread_mutex_lock(Debugger::the().continue_mutex());
  65. Debugger::the().set_continue_type(Debugger::ContinueType::SourceStepOut);
  66. pthread_cond_signal(Debugger::the().continue_cond());
  67. pthread_mutex_unlock(Debugger::the().continue_mutex());
  68. });
  69. m_toolbar->add_action(*m_continue_action);
  70. m_toolbar->add_action(*m_singlestep_action);
  71. m_toolbar->add_action(*m_step_in_action);
  72. m_toolbar->add_action(*m_step_out_action);
  73. set_debug_actions_enabled(false);
  74. }
  75. DebugInfoWidget::DebugInfoWidget()
  76. {
  77. set_layout<GUI::VerticalBoxLayout>();
  78. auto& toolbar_container = add<GUI::ToolBarContainer>();
  79. m_toolbar = toolbar_container.add<GUI::ToolBar>();
  80. init_toolbar();
  81. auto& bottom_box = add<GUI::Widget>();
  82. bottom_box.set_layout<GUI::HorizontalBoxLayout>();
  83. auto& splitter = bottom_box.add<GUI::HorizontalSplitter>();
  84. m_backtrace_view = splitter.add<GUI::ListView>();
  85. auto& variables_tab_widget = splitter.add<GUI::TabWidget>();
  86. variables_tab_widget.set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  87. variables_tab_widget.add_widget("Variables", build_variables_tab());
  88. variables_tab_widget.add_widget("Registers", build_registers_tab());
  89. m_backtrace_view->on_selection = [this](auto& index) {
  90. auto& model = static_cast<BacktraceModel&>(*m_backtrace_view->model());
  91. // Note: The reconstruction of the register set here is obviously incomplete.
  92. // We currently only reconstruct eip & ebp. Ideally would also reconstruct the other registers somehow.
  93. // (Other registers may be needed to get the values of variables who are not stored on the stack)
  94. PtraceRegisters frame_regs {};
  95. frame_regs.eip = model.frames()[index.row()].instruction_address;
  96. frame_regs.ebp = model.frames()[index.row()].frame_base;
  97. m_variables_view->set_model(VariablesModel::create(frame_regs));
  98. };
  99. }
  100. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_variables_tab()
  101. {
  102. auto variables_widget = GUI::Widget::construct();
  103. variables_widget->set_layout<GUI::HorizontalBoxLayout>();
  104. m_variables_view = variables_widget->add<GUI::TreeView>();
  105. auto is_valid_index = [](auto& index) {
  106. if (!index.is_valid())
  107. return false;
  108. auto* variable = static_cast<const Debug::DebugInfo::VariableInfo*>(index.internal_data());
  109. if (variable->location_type != Debug::DebugInfo::VariableInfo::LocationType::Address)
  110. return false;
  111. return variable->is_enum_type() || variable->type_name.is_one_of("int", "bool");
  112. };
  113. m_variables_view->on_context_menu_request = [this, is_valid_index](auto& index, auto& event) {
  114. if (!is_valid_index(index))
  115. return;
  116. m_variable_context_menu->popup(event.screen_position());
  117. };
  118. m_variables_view->on_activation = [this, is_valid_index](auto& index) {
  119. if (!is_valid_index(index))
  120. return;
  121. String value;
  122. if (GUI::InputBox::show(value, window(), "Enter new value:", "Set variable value") == GUI::InputBox::ExecOK) {
  123. auto& model = static_cast<VariablesModel&>(*m_variables_view->model());
  124. model.set_variable_value(index, value, window());
  125. }
  126. };
  127. auto edit_variable_action = GUI::Action::create("Change value", [this](auto&) {
  128. m_variables_view->on_activation(m_variables_view->selection().first());
  129. });
  130. m_variable_context_menu = GUI::Menu::construct();
  131. m_variable_context_menu->add_action(edit_variable_action);
  132. return variables_widget;
  133. }
  134. NonnullRefPtr<GUI::Widget> DebugInfoWidget::build_registers_tab()
  135. {
  136. auto registers_widget = GUI::Widget::construct();
  137. registers_widget->set_layout<GUI::HorizontalBoxLayout>();
  138. m_registers_view = registers_widget->add<GUI::TableView>();
  139. return registers_widget;
  140. }
  141. void DebugInfoWidget::update_state(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
  142. {
  143. m_variables_view->set_model(VariablesModel::create(regs));
  144. m_backtrace_view->set_model(BacktraceModel::create(debug_session, regs));
  145. if (m_registers_view->model()) {
  146. auto& previous_registers = static_cast<RegistersModel*>(m_registers_view->model())->raw_registers();
  147. m_registers_view->set_model(RegistersModel::create(regs, previous_registers));
  148. } else {
  149. m_registers_view->set_model(RegistersModel::create(regs));
  150. }
  151. auto selected_index = m_backtrace_view->model()->index(0);
  152. if (!selected_index.is_valid()) {
  153. dbg() << "Warning: DebugInfoWidget: backtrace selected index is invalid";
  154. return;
  155. }
  156. m_backtrace_view->selection().set(selected_index);
  157. }
  158. void DebugInfoWidget::program_stopped()
  159. {
  160. m_variables_view->set_model({});
  161. m_backtrace_view->set_model({});
  162. m_registers_view->set_model({});
  163. }
  164. void DebugInfoWidget::set_debug_actions_enabled(bool enabled)
  165. {
  166. m_continue_action->set_enabled(enabled);
  167. m_singlestep_action->set_enabled(enabled);
  168. m_step_in_action->set_enabled(enabled);
  169. m_step_out_action->set_enabled(enabled);
  170. }
  171. }