ThreadStackWidget.cpp 3.3 KB

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