DebugInfoWidget.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "VariablesModel.h"
  30. #include <AK/StringBuilder.h>
  31. #include <LibGUI/Action.h>
  32. #include <LibGUI/BoxLayout.h>
  33. #include <LibGUI/InputBox.h>
  34. #include <LibGUI/Layout.h>
  35. #include <LibGUI/ListView.h>
  36. #include <LibGUI/Menu.h>
  37. #include <LibGUI/Model.h>
  38. #include <LibGUI/Splitter.h>
  39. #include <LibGUI/TreeView.h>
  40. namespace HackStudio {
  41. void DebugInfoWidget::init_toolbar()
  42. {
  43. m_continue_action = GUI::Action::create("Continue", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-continue.png"), [&](auto&) {
  44. pthread_mutex_lock(Debugger::the().continue_mutex());
  45. Debugger::the().set_continue_type(Debugger::ContinueType::Continue);
  46. pthread_cond_signal(Debugger::the().continue_cond());
  47. pthread_mutex_unlock(Debugger::the().continue_mutex());
  48. });
  49. m_singlestep_action = GUI::Action::create("Step Over", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-step-over.png"), [&](auto&) {
  50. pthread_mutex_lock(Debugger::the().continue_mutex());
  51. Debugger::the().set_continue_type(Debugger::ContinueType::SourceStepOver);
  52. pthread_cond_signal(Debugger::the().continue_cond());
  53. pthread_mutex_unlock(Debugger::the().continue_mutex());
  54. });
  55. m_step_in_action = GUI::Action::create("Step In", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-step-in.png"), [&](auto&) {
  56. pthread_mutex_lock(Debugger::the().continue_mutex());
  57. Debugger::the().set_continue_type(Debugger::ContinueType::SourceSingleStep);
  58. pthread_cond_signal(Debugger::the().continue_cond());
  59. pthread_mutex_unlock(Debugger::the().continue_mutex());
  60. });
  61. m_step_out_action = GUI::Action::create("Step Out", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-step-out.png"), [&](auto&) {
  62. pthread_mutex_lock(Debugger::the().continue_mutex());
  63. Debugger::the().set_continue_type(Debugger::ContinueType::SourceStepOut);
  64. pthread_cond_signal(Debugger::the().continue_cond());
  65. pthread_mutex_unlock(Debugger::the().continue_mutex());
  66. });
  67. m_toolbar->add_action(*m_continue_action);
  68. m_toolbar->add_action(*m_singlestep_action);
  69. m_toolbar->add_action(*m_step_in_action);
  70. m_toolbar->add_action(*m_step_out_action);
  71. set_debug_actions_enabled(false);
  72. }
  73. DebugInfoWidget::DebugInfoWidget()
  74. {
  75. set_layout<GUI::VerticalBoxLayout>();
  76. auto& toolbar_container = add<GUI::ToolBarContainer>();
  77. m_toolbar = toolbar_container.add<GUI::ToolBar>();
  78. init_toolbar();
  79. auto& bottom_box = add<GUI::Widget>();
  80. bottom_box.set_layout<GUI::HorizontalBoxLayout>();
  81. auto& splitter = bottom_box.add<GUI::HorizontalSplitter>();
  82. m_backtrace_view = splitter.add<GUI::ListView>();
  83. m_variables_view = splitter.add<GUI::TreeView>();
  84. m_backtrace_view->on_selection = [this](auto& index) {
  85. auto& model = static_cast<BacktraceModel&>(*m_backtrace_view->model());
  86. // Note: The reconstruction of the register set here is obviously incomplete.
  87. // We currently only reconstruct eip & ebp. Ideally would also reconstruct the other registers somehow.
  88. // (Other registers may be needed to get the values of variables who are not stored on the stack)
  89. PtraceRegisters frame_regs {};
  90. frame_regs.eip = model.frames()[index.row()].instruction_address;
  91. frame_regs.ebp = model.frames()[index.row()].frame_base;
  92. m_variables_view->set_model(VariablesModel::create(frame_regs));
  93. };
  94. auto edit_variable_action = GUI::Action::create("Change value", [&](auto&) {
  95. m_variables_view->on_activation(m_variables_view->selection().first());
  96. });
  97. m_variable_context_menu = GUI::Menu::construct();
  98. m_variable_context_menu->add_action(edit_variable_action);
  99. auto is_valid_index = [](auto& index) {
  100. if (!index.is_valid())
  101. return false;
  102. auto* variable = static_cast<const Debug::DebugInfo::VariableInfo*>(index.internal_data());
  103. if (variable->location_type != Debug::DebugInfo::VariableInfo::LocationType::Address)
  104. return false;
  105. return variable->is_enum_type() || variable->type_name.is_one_of("int", "bool");
  106. };
  107. m_variables_view->on_context_menu_request = [this, is_valid_index](auto& index, auto& event) {
  108. if (!is_valid_index(index))
  109. return;
  110. m_variable_context_menu->popup(event.screen_position());
  111. };
  112. m_variables_view->on_activation = [this, is_valid_index](auto& index) {
  113. if (!is_valid_index(index))
  114. return;
  115. String value;
  116. if (GUI::InputBox::show(value, window(), "Enter new value:", "Set variable value") == GUI::InputBox::ExecOK) {
  117. auto& model = static_cast<VariablesModel&>(*m_variables_view->model());
  118. model.set_variable_value(index, value, window());
  119. }
  120. };
  121. }
  122. void DebugInfoWidget::update_state(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
  123. {
  124. m_variables_view->set_model(VariablesModel::create(regs));
  125. m_backtrace_view->set_model(BacktraceModel::create(debug_session, regs));
  126. auto selected_index = m_backtrace_view->model()->index(0);
  127. if (!selected_index.is_valid()) {
  128. dbg() << "Warning: DebugInfoWidget: backtrace selected index is invalid";
  129. return;
  130. }
  131. m_backtrace_view->selection().set(selected_index);
  132. }
  133. void DebugInfoWidget::program_stopped()
  134. {
  135. m_variables_view->set_model({});
  136. }
  137. void DebugInfoWidget::set_debug_actions_enabled(bool enabled)
  138. {
  139. m_continue_action->set_enabled(enabled);
  140. m_singlestep_action->set_enabled(enabled);
  141. m_step_in_action->set_enabled(enabled);
  142. m_step_out_action->set_enabled(enabled);
  143. }
  144. }