ThreadList.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //===-- ThreadList.h --------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLDB_TARGET_THREADLIST_H
  9. #define LLDB_TARGET_THREADLIST_H
  10. #include <mutex>
  11. #include <vector>
  12. #include "lldb/Target/Thread.h"
  13. #include "lldb/Target/ThreadCollection.h"
  14. #include "lldb/Utility/Iterable.h"
  15. #include "lldb/Utility/UserID.h"
  16. #include "lldb/lldb-private.h"
  17. namespace lldb_private {
  18. // This is a thread list with lots of functionality for use only by the process
  19. // for which this is the thread list. A generic container class with iterator
  20. // functionality is ThreadCollection.
  21. class ThreadList : public ThreadCollection {
  22. friend class Process;
  23. public:
  24. ThreadList(Process *process);
  25. ThreadList(const ThreadList &rhs);
  26. ~ThreadList() override;
  27. const ThreadList &operator=(const ThreadList &rhs);
  28. uint32_t GetSize(bool can_update = true);
  29. // Return the selected thread if there is one. Otherwise, return the thread
  30. // selected at index 0.
  31. lldb::ThreadSP GetSelectedThread();
  32. // Manage the thread to use for running expressions. This is usually the
  33. // Selected thread, but sometimes (e.g. when evaluating breakpoint conditions
  34. // & stop hooks) it isn't.
  35. class ExpressionExecutionThreadPusher {
  36. public:
  37. ExpressionExecutionThreadPusher(ThreadList &thread_list, lldb::tid_t tid)
  38. : m_thread_list(&thread_list), m_tid(tid) {
  39. m_thread_list->PushExpressionExecutionThread(m_tid);
  40. }
  41. ExpressionExecutionThreadPusher(lldb::ThreadSP thread_sp);
  42. ~ExpressionExecutionThreadPusher() {
  43. if (m_thread_list && m_tid != LLDB_INVALID_THREAD_ID)
  44. m_thread_list->PopExpressionExecutionThread(m_tid);
  45. }
  46. private:
  47. ThreadList *m_thread_list;
  48. lldb::tid_t m_tid;
  49. };
  50. lldb::ThreadSP GetExpressionExecutionThread();
  51. protected:
  52. void PushExpressionExecutionThread(lldb::tid_t tid);
  53. void PopExpressionExecutionThread(lldb::tid_t tid);
  54. public:
  55. bool SetSelectedThreadByID(lldb::tid_t tid, bool notify = false);
  56. bool SetSelectedThreadByIndexID(uint32_t index_id, bool notify = false);
  57. void Clear();
  58. void Flush();
  59. void Destroy();
  60. // Note that "idx" is not the same as the "thread_index". It is a zero based
  61. // index to accessing the current threads, whereas "thread_index" is a unique
  62. // index assigned
  63. lldb::ThreadSP GetThreadAtIndex(uint32_t idx, bool can_update = true);
  64. lldb::ThreadSP FindThreadByID(lldb::tid_t tid, bool can_update = true);
  65. lldb::ThreadSP FindThreadByProtocolID(lldb::tid_t tid,
  66. bool can_update = true);
  67. lldb::ThreadSP RemoveThreadByID(lldb::tid_t tid, bool can_update = true);
  68. lldb::ThreadSP RemoveThreadByProtocolID(lldb::tid_t tid,
  69. bool can_update = true);
  70. lldb::ThreadSP FindThreadByIndexID(uint32_t index_id, bool can_update = true);
  71. lldb::ThreadSP GetThreadSPForThreadPtr(Thread *thread_ptr);
  72. lldb::ThreadSP GetBackingThread(const lldb::ThreadSP &real_thread);
  73. bool ShouldStop(Event *event_ptr);
  74. Vote ShouldReportStop(Event *event_ptr);
  75. Vote ShouldReportRun(Event *event_ptr);
  76. void RefreshStateAfterStop();
  77. /// The thread list asks tells all the threads it is about to resume.
  78. /// If a thread can "resume" without having to resume the target, it
  79. /// will return false for WillResume, and then the process will not be
  80. /// restarted.
  81. ///
  82. /// \return
  83. /// \b true instructs the process to resume normally,
  84. /// \b false means start & stopped events will be generated, but
  85. /// the process will not actually run. The thread must then return
  86. /// the correct StopInfo when asked.
  87. ///
  88. bool WillResume();
  89. void DidResume();
  90. void DidStop();
  91. void DiscardThreadPlans();
  92. uint32_t GetStopID() const;
  93. void SetStopID(uint32_t stop_id);
  94. std::recursive_mutex &GetMutex() const override;
  95. void Update(ThreadList &rhs);
  96. protected:
  97. void SetShouldReportStop(Vote vote);
  98. void NotifySelectedThreadChanged(lldb::tid_t tid);
  99. // Classes that inherit from Process can see and modify these
  100. Process *m_process; ///< The process that manages this thread list.
  101. uint32_t
  102. m_stop_id; ///< The process stop ID that this thread list is valid for.
  103. lldb::tid_t
  104. m_selected_tid; ///< For targets that need the notion of a current thread.
  105. std::vector<lldb::tid_t> m_expression_tid_stack;
  106. private:
  107. ThreadList() = delete;
  108. };
  109. } // namespace lldb_private
  110. #endif // LLDB_TARGET_THREADLIST_H