ThreadStackWidget.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. ErrorOr<String> column_name(int column) const override
  27. {
  28. switch (column) {
  29. case Column::Address:
  30. return "Address"_short_string;
  31. case Column::Object:
  32. return "Object"_short_string;
  33. case Column::Symbol:
  34. return "Symbol"_short_string;
  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. ErrorOr<NonnullRefPtr<ThreadStackWidget>> ThreadStackWidget::try_create()
  64. {
  65. auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ThreadStackWidget()));
  66. TRY(widget->try_set_layout<GUI::VerticalBoxLayout>(4));
  67. widget->m_stack_table = TRY(widget->try_add<GUI::TableView>());
  68. widget->m_stack_table->set_model(TRY(try_make_ref_counted<ThreadStackModel>()));
  69. return widget;
  70. }
  71. void ThreadStackWidget::show_event(GUI::ShowEvent&)
  72. {
  73. refresh();
  74. if (!m_timer) {
  75. m_timer = add<Core::Timer>(1000, [this] { refresh(); });
  76. m_timer->start();
  77. }
  78. }
  79. void ThreadStackWidget::hide_event(GUI::HideEvent&)
  80. {
  81. m_timer = nullptr;
  82. }
  83. void ThreadStackWidget::set_ids(pid_t pid, pid_t tid)
  84. {
  85. if (m_pid == pid && m_tid == tid)
  86. return;
  87. m_pid = pid;
  88. m_tid = tid;
  89. }
  90. class CompletionEvent : public Core::CustomEvent {
  91. public:
  92. explicit CompletionEvent(Vector<Symbolication::Symbol> symbols)
  93. : Core::CustomEvent(0)
  94. , m_symbols(move(symbols))
  95. {
  96. }
  97. Vector<Symbolication::Symbol> const& symbols() const { return m_symbols; }
  98. private:
  99. Vector<Symbolication::Symbol> m_symbols;
  100. };
  101. void ThreadStackWidget::refresh()
  102. {
  103. (void)Threading::BackgroundAction<Vector<Symbolication::Symbol>>::construct(
  104. [pid = m_pid, tid = m_tid](auto&) {
  105. return Symbolication::symbolicate_thread(pid, tid, Symbolication::IncludeSourcePosition::No);
  106. },
  107. [weak_this = make_weak_ptr()](auto result) -> ErrorOr<void> {
  108. if (!weak_this)
  109. return {};
  110. Core::EventLoop::current().post_event(const_cast<Core::Object&>(*weak_this), make<CompletionEvent>(move(result)));
  111. return {};
  112. });
  113. }
  114. void ThreadStackWidget::custom_event(Core::CustomEvent& event)
  115. {
  116. auto& completion_event = verify_cast<CompletionEvent>(event);
  117. verify_cast<ThreadStackModel>(m_stack_table->model())->set_symbols(completion_event.symbols());
  118. }
  119. }