DebugInfoWidget.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/ListView.h>
  35. #include <LibGUI/Menu.h>
  36. #include <LibGUI/Model.h>
  37. #include <LibGUI/Splitter.h>
  38. #include <LibGUI/TreeView.h>
  39. DebugInfoWidget::DebugInfoWidget()
  40. {
  41. set_layout<GUI::HorizontalBoxLayout>();
  42. auto& splitter = add<GUI::HorizontalSplitter>();
  43. m_backtrace_view = splitter.add<GUI::ListView>();
  44. m_variables_view = splitter.add<GUI::TreeView>();
  45. m_backtrace_view->on_selection = [this](auto& index) {
  46. auto& model = static_cast<BacktraceModel&>(*m_backtrace_view->model());
  47. // Note: The reconstruction of the register set here is obviously incomplete.
  48. // We currently only reconstruct eip & ebp. Ideally would also reconstruct the other registers somehow.
  49. // (Other registers may be needed to get the values of variables who are not stored on the stack)
  50. PtraceRegisters frame_regs {};
  51. frame_regs.eip = model.frames()[index.row()].instruction_address;
  52. frame_regs.ebp = model.frames()[index.row()].frame_base;
  53. m_variables_view->set_model(VariablesModel::create(frame_regs));
  54. };
  55. auto edit_variable_action = GUI::Action::create("Change value", [&](auto&) {
  56. m_variables_view->on_activation(m_variables_view->selection().first());
  57. });
  58. m_variable_context_menu = GUI::Menu::construct();
  59. m_variable_context_menu->add_action(edit_variable_action);
  60. auto is_valid_index = [](auto& index) {
  61. if (!index.is_valid())
  62. return false;
  63. auto* variable = static_cast<const DebugInfo::VariableInfo*>(index.internal_data());
  64. if (variable->location_type != DebugInfo::VariableInfo::LocationType::Address)
  65. return false;
  66. return variable->is_enum_type() || variable->type_name.is_one_of("int", "bool");
  67. };
  68. m_variables_view->on_context_menu_request = [this, is_valid_index](auto& index, auto& event) {
  69. if (!is_valid_index(index))
  70. return;
  71. m_variable_context_menu->popup(event.screen_position());
  72. };
  73. m_variables_view->on_activation = [this, is_valid_index](auto& index) {
  74. if (!is_valid_index(index))
  75. return;
  76. auto input = GUI::InputBox::construct("Enter new value:", "Set variable value", window());
  77. if (input->exec() == GUI::InputBox::ExecOK) {
  78. auto& model = static_cast<VariablesModel&>(*m_variables_view->model());
  79. model.set_variable_value(index, input->text_value(), window());
  80. }
  81. };
  82. }
  83. void DebugInfoWidget::update_state(const DebugSession& debug_session, const PtraceRegisters& regs)
  84. {
  85. m_variables_view->set_model(VariablesModel::create(regs));
  86. m_backtrace_view->set_model(BacktraceModel::create(debug_session, regs));
  87. m_backtrace_view->selection().set(m_backtrace_view->model()->index(0));
  88. }
  89. void DebugInfoWidget::program_stopped()
  90. {
  91. m_variables_view->set_model({});
  92. }