DisassemblyWidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2020, Luke Wilde <luke.wilde@live.co.uk>
  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 "DisassemblyWidget.h"
  27. #include "DisassemblyModel.h"
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGfx/Palette.h>
  31. namespace HackStudio {
  32. void UnavailableDisassemblyWidget::paint_event(GUI::PaintEvent& event)
  33. {
  34. Frame::paint_event(event);
  35. if (reason().is_empty())
  36. return;
  37. GUI::Painter painter(*this);
  38. painter.add_clip_rect(event.rect());
  39. painter.draw_text(frame_inner_rect(), reason(), Gfx::TextAlignment::Center, palette().window_text(), Gfx::TextElision::Right);
  40. }
  41. DisassemblyWidget::DisassemblyWidget()
  42. {
  43. set_layout<GUI::VerticalBoxLayout>();
  44. m_top_container = add<GUI::Widget>();
  45. m_top_container->set_layout<GUI::HorizontalBoxLayout>();
  46. m_top_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  47. m_top_container->set_preferred_size(0, 20);
  48. m_function_name_label = m_top_container->add<GUI::Label>("");
  49. m_function_name_label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  50. m_disassembly_view = add<GUI::TableView>();
  51. m_unavailable_disassembly_widget = add<UnavailableDisassemblyWidget>("");
  52. hide_disassembly("Program isn't running");
  53. }
  54. void DisassemblyWidget::update_state(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
  55. {
  56. m_disassembly_view->set_model(DisassemblyModel::create(debug_session, regs));
  57. if (m_disassembly_view->model()->row_count() > 0) {
  58. auto containing_function = debug_session.debug_info().get_containing_function(regs.eip);
  59. if (containing_function.has_value())
  60. m_function_name_label->set_text(containing_function.value().name);
  61. else
  62. m_function_name_label->set_text("<missing>");
  63. show_disassembly();
  64. } else {
  65. hide_disassembly("No disassembly to show for this function");
  66. }
  67. }
  68. void DisassemblyWidget::program_stopped()
  69. {
  70. m_disassembly_view->set_model({});
  71. m_function_name_label->set_text("");
  72. hide_disassembly("Program isn't running");
  73. }
  74. void DisassemblyWidget::show_disassembly()
  75. {
  76. m_top_container->set_visible(true);
  77. m_disassembly_view->set_visible(true);
  78. m_function_name_label->set_visible(true);
  79. m_unavailable_disassembly_widget->set_visible(false);
  80. }
  81. void DisassemblyWidget::hide_disassembly(const String& reason)
  82. {
  83. m_top_container->set_visible(false);
  84. m_disassembly_view->set_visible(false);
  85. m_function_name_label->set_visible(false);
  86. m_unavailable_disassembly_widget->set_visible(true);
  87. m_unavailable_disassembly_widget->set_reason(reason);
  88. }
  89. }