gdbthread.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Multi-thread control defs for remote server for GDB.
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef GDBSERVER_GDBTHREAD_H
  15. #define GDBSERVER_GDBTHREAD_H
  16. #include "gdbsupport/common-gdbthread.h"
  17. #include "inferiors.h"
  18. #include <list>
  19. struct btrace_target_info;
  20. struct regcache;
  21. struct thread_info
  22. {
  23. thread_info (ptid_t id, void *target_data)
  24. : id (id), target_data (target_data)
  25. {}
  26. ~thread_info ()
  27. {
  28. free_register_cache (this->regcache_data);
  29. }
  30. /* The id of this thread. */
  31. ptid_t id;
  32. void *target_data;
  33. struct regcache *regcache_data = nullptr;
  34. /* The last resume GDB requested on this thread. */
  35. enum resume_kind last_resume_kind = resume_continue;
  36. /* The last wait status reported for this thread. */
  37. struct target_waitstatus last_status;
  38. /* True if LAST_STATUS hasn't been reported to GDB yet. */
  39. int status_pending_p = 0;
  40. /* Given `while-stepping', a thread may be collecting data for more
  41. than one tracepoint simultaneously. E.g.:
  42. ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
  43. ff0002 INSN2
  44. ff0003 INSN3 <-- TP2, collect $regs
  45. ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
  46. ff0005 INSN5
  47. Notice that when instruction INSN5 is reached, the while-stepping
  48. actions of both TP1 and TP3 are still being collected, and that TP2
  49. had been collected meanwhile. The whole range of ff0001-ff0005
  50. should be single-stepped, due to at least TP1's while-stepping
  51. action covering the whole range.
  52. On the other hand, the same tracepoint with a while-stepping action
  53. may be hit by more than one thread simultaneously, hence we can't
  54. keep the current step count in the tracepoint itself.
  55. This is the head of the list of the states of `while-stepping'
  56. tracepoint actions this thread is now collecting; NULL if empty.
  57. Each item in the list holds the current step of the while-stepping
  58. action. */
  59. struct wstep_state *while_stepping = nullptr;
  60. /* Branch trace target information for this thread. */
  61. struct btrace_target_info *btrace = nullptr;
  62. };
  63. extern std::list<thread_info *> all_threads;
  64. void remove_thread (struct thread_info *thread);
  65. struct thread_info *add_thread (ptid_t ptid, void *target_data);
  66. /* Return a pointer to the first thread, or NULL if there isn't one. */
  67. struct thread_info *get_first_thread (void);
  68. struct thread_info *find_thread_ptid (ptid_t ptid);
  69. /* Find any thread of the PID process. Returns NULL if none is
  70. found. */
  71. struct thread_info *find_any_thread_of_pid (int pid);
  72. /* Find the first thread for which FUNC returns true. Return NULL if no thread
  73. satisfying FUNC is found. */
  74. template <typename Func>
  75. static thread_info *
  76. find_thread (Func func)
  77. {
  78. std::list<thread_info *>::iterator next, cur = all_threads.begin ();
  79. while (cur != all_threads.end ())
  80. {
  81. next = cur;
  82. next++;
  83. if (func (*cur))
  84. return *cur;
  85. cur = next;
  86. }
  87. return NULL;
  88. }
  89. /* Like the above, but only consider threads with pid PID. */
  90. template <typename Func>
  91. static thread_info *
  92. find_thread (int pid, Func func)
  93. {
  94. return find_thread ([&] (thread_info *thread)
  95. {
  96. return thread->id.pid () == pid && func (thread);
  97. });
  98. }
  99. /* Find the first thread that matches FILTER for which FUNC returns true.
  100. Return NULL if no thread satisfying these conditions is found. */
  101. template <typename Func>
  102. static thread_info *
  103. find_thread (ptid_t filter, Func func)
  104. {
  105. return find_thread ([&] (thread_info *thread) {
  106. return thread->id.matches (filter) && func (thread);
  107. });
  108. }
  109. /* Invoke FUNC for each thread. */
  110. template <typename Func>
  111. static void
  112. for_each_thread (Func func)
  113. {
  114. std::list<thread_info *>::iterator next, cur = all_threads.begin ();
  115. while (cur != all_threads.end ())
  116. {
  117. next = cur;
  118. next++;
  119. func (*cur);
  120. cur = next;
  121. }
  122. }
  123. /* Like the above, but only consider threads with pid PID. */
  124. template <typename Func>
  125. static void
  126. for_each_thread (int pid, Func func)
  127. {
  128. for_each_thread ([&] (thread_info *thread)
  129. {
  130. if (pid == thread->id.pid ())
  131. func (thread);
  132. });
  133. }
  134. /* Find the a random thread for which FUNC (THREAD) returns true. If
  135. no entry is found then return NULL. */
  136. template <typename Func>
  137. static thread_info *
  138. find_thread_in_random (Func func)
  139. {
  140. int count = 0;
  141. int random_selector;
  142. /* First count how many interesting entries we have. */
  143. for_each_thread ([&] (thread_info *thread) {
  144. if (func (thread))
  145. count++;
  146. });
  147. if (count == 0)
  148. return NULL;
  149. /* Now randomly pick an entry out of those. */
  150. random_selector = (int)
  151. ((count * (double) rand ()) / (RAND_MAX + 1.0));
  152. thread_info *thread = find_thread ([&] (thread_info *thr_arg) {
  153. return func (thr_arg) && (random_selector-- == 0);
  154. });
  155. gdb_assert (thread != NULL);
  156. return thread;
  157. }
  158. /* Get current thread ID (Linux task ID). */
  159. #define current_ptid (current_thread->id)
  160. /* Get the ptid of THREAD. */
  161. static inline ptid_t
  162. ptid_of (const thread_info *thread)
  163. {
  164. return thread->id;
  165. }
  166. /* Get the pid of THREAD. */
  167. static inline int
  168. pid_of (const thread_info *thread)
  169. {
  170. return thread->id.pid ();
  171. }
  172. /* Get the lwp of THREAD. */
  173. static inline long
  174. lwpid_of (const thread_info *thread)
  175. {
  176. return thread->id.lwp ();
  177. }
  178. /* Switch the current thread. */
  179. void switch_to_thread (thread_info *thread);
  180. /* Save/restore current thread. */
  181. class scoped_restore_current_thread
  182. {
  183. public:
  184. scoped_restore_current_thread ();
  185. ~scoped_restore_current_thread ();
  186. DISABLE_COPY_AND_ASSIGN (scoped_restore_current_thread);
  187. /* Cancel restoring on scope exit. */
  188. void dont_restore () { m_dont_restore = true; }
  189. private:
  190. bool m_dont_restore = false;
  191. thread_info *m_thread;
  192. };
  193. #endif /* GDBSERVER_GDBTHREAD_H */