ThreadStackWidget.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ThreadStackWidget.h"
  8. #include <LibCore/Timer.h>
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/Model.h>
  11. #include <LibGUI/Widget.h>
  12. #include <LibSymbolication/Symbolication.h>
  13. #include <LibThreading/BackgroundAction.h>
  14. REGISTER_WIDGET(SystemMonitor, ThreadStackWidget)
  15. namespace SystemMonitor {
  16. class ThreadStackModel final : public GUI::Model {
  17. enum Column {
  18. Address,
  19. Object,
  20. Symbol
  21. };
  22. public:
  23. int column_count(GUI::ModelIndex const&) const override { return 3; };
  24. int row_count(GUI::ModelIndex const&) const override { return m_symbols.size(); };
  25. bool is_column_sortable(int) const override { return false; }
  26. DeprecatedString column_name(int column) const override
  27. {
  28. switch (column) {
  29. case Column::Address:
  30. return "Address";
  31. case Column::Object:
  32. return "Object";
  33. case Column::Symbol:
  34. return "Symbol";
  35. default:
  36. VERIFY_NOT_REACHED();
  37. }
  38. }
  39. GUI::Variant data(GUI::ModelIndex const& model_index, GUI::ModelRole) const override
  40. {
  41. auto& symbol = m_symbols[model_index.row()];
  42. switch (model_index.column()) {
  43. case Column::Address:
  44. return DeprecatedString::formatted("{:p}", symbol.address);
  45. case Column::Object:
  46. return symbol.object;
  47. case Column::Symbol:
  48. return symbol.name;
  49. default:
  50. VERIFY_NOT_REACHED();
  51. }
  52. };
  53. void set_symbols(Vector<Symbolication::Symbol> const& symbols)
  54. {
  55. if (m_symbols == symbols)
  56. return;
  57. m_symbols = symbols;
  58. invalidate();
  59. }
  60. private:
  61. Vector<Symbolication::Symbol> m_symbols;
  62. };
  63. ThreadStackWidget::ThreadStackWidget()
  64. {
  65. set_layout<GUI::VerticalBoxLayout>(4);
  66. m_stack_table = add<GUI::TableView>();
  67. m_stack_table->set_model(adopt_ref(*new ThreadStackModel()));
  68. }
  69. void ThreadStackWidget::show_event(GUI::ShowEvent&)
  70. {
  71. refresh();
  72. if (!m_timer) {
  73. m_timer = add<Core::Timer>(1000, [this] { refresh(); });
  74. m_timer->start();
  75. }
  76. }
  77. void ThreadStackWidget::hide_event(GUI::HideEvent&)
  78. {
  79. m_timer = nullptr;
  80. }
  81. void ThreadStackWidget::set_ids(pid_t pid, pid_t tid)
  82. {
  83. if (m_pid == pid && m_tid == tid)
  84. return;
  85. m_pid = pid;
  86. m_tid = tid;
  87. }
  88. class CompletionEvent : public Core::CustomEvent {
  89. public:
  90. explicit CompletionEvent(Vector<Symbolication::Symbol> symbols)
  91. : Core::CustomEvent(0)
  92. , m_symbols(move(symbols))
  93. {
  94. }
  95. Vector<Symbolication::Symbol> const& symbols() const { return m_symbols; }
  96. private:
  97. Vector<Symbolication::Symbol> m_symbols;
  98. };
  99. void ThreadStackWidget::refresh()
  100. {
  101. (void)Threading::BackgroundAction<Vector<Symbolication::Symbol>>::construct(
  102. [pid = m_pid, tid = m_tid](auto&) {
  103. return Symbolication::symbolicate_thread(pid, tid, Symbolication::IncludeSourcePosition::No);
  104. },
  105. [weak_this = make_weak_ptr()](auto result) -> ErrorOr<void> {
  106. if (!weak_this)
  107. return {};
  108. Core::EventLoop::current().post_event(const_cast<Core::Object&>(*weak_this), make<CompletionEvent>(move(result)));
  109. return {};
  110. });
  111. }
  112. void ThreadStackWidget::custom_event(Core::CustomEvent& event)
  113. {
  114. auto& completion_event = verify_cast<CompletionEvent>(event);
  115. verify_cast<ThreadStackModel>(m_stack_table->model())->set_symbols(completion_event.symbols());
  116. }
  117. }