process-stratum-target.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Abstract base class inherited by all process_stratum targets
  2. Copyright (C) 2018-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. #include "defs.h"
  15. #include "process-stratum-target.h"
  16. #include "inferior.h"
  17. #include <algorithm>
  18. process_stratum_target::~process_stratum_target ()
  19. {
  20. }
  21. struct address_space *
  22. process_stratum_target::thread_address_space (ptid_t ptid)
  23. {
  24. /* Fall-back to the "main" address space of the inferior. */
  25. inferior *inf = find_inferior_ptid (this, ptid);
  26. if (inf == NULL || inf->aspace == NULL)
  27. internal_error (__FILE__, __LINE__,
  28. _("Can't determine the current "
  29. "address space of thread %s\n"),
  30. target_pid_to_str (ptid).c_str ());
  31. return inf->aspace;
  32. }
  33. struct gdbarch *
  34. process_stratum_target::thread_architecture (ptid_t ptid)
  35. {
  36. inferior *inf = find_inferior_ptid (this, ptid);
  37. gdb_assert (inf != NULL);
  38. return inf->gdbarch;
  39. }
  40. bool
  41. process_stratum_target::has_all_memory ()
  42. {
  43. /* If no inferior selected, then we can't read memory here. */
  44. return inferior_ptid != null_ptid;
  45. }
  46. bool
  47. process_stratum_target::has_memory ()
  48. {
  49. /* If no inferior selected, then we can't read memory here. */
  50. return inferior_ptid != null_ptid;
  51. }
  52. bool
  53. process_stratum_target::has_stack ()
  54. {
  55. /* If no inferior selected, there's no stack. */
  56. return inferior_ptid != null_ptid;
  57. }
  58. bool
  59. process_stratum_target::has_registers ()
  60. {
  61. /* Can't read registers from no inferior. */
  62. return inferior_ptid != null_ptid;
  63. }
  64. bool
  65. process_stratum_target::has_execution (inferior *inf)
  66. {
  67. /* If there's a process running already, we can't make it run
  68. through hoops. */
  69. return inf->pid != 0;
  70. }
  71. /* See process-stratum-target.h. */
  72. void
  73. process_stratum_target::follow_exec (inferior *follow_inf, ptid_t ptid,
  74. const char *execd_pathname)
  75. {
  76. inferior *orig_inf = current_inferior ();
  77. if (orig_inf != follow_inf)
  78. {
  79. /* Execution continues in a new inferior, push the original inferior's
  80. process target on the new inferior's target stack. The process target
  81. may decide to unpush itself from the original inferior's target stack
  82. after that, at its discretion. */
  83. follow_inf->push_target (orig_inf->process_target ());
  84. thread_info *t = add_thread (follow_inf->process_target (), ptid);
  85. /* Leave the new inferior / thread as the current inferior / thread. */
  86. switch_to_thread (t);
  87. }
  88. }
  89. /* See process-stratum-target.h. */
  90. void
  91. process_stratum_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
  92. target_waitkind fork_kind,
  93. bool follow_child,
  94. bool detach_on_fork)
  95. {
  96. if (child_inf != nullptr)
  97. {
  98. child_inf->push_target (this);
  99. add_thread_silent (this, child_ptid);
  100. }
  101. }
  102. /* See process-stratum-target.h. */
  103. void
  104. process_stratum_target::maybe_add_resumed_with_pending_wait_status
  105. (thread_info *thread)
  106. {
  107. gdb_assert (!thread->resumed_with_pending_wait_status_node.is_linked ());
  108. if (thread->resumed () && thread->has_pending_waitstatus ())
  109. {
  110. infrun_debug_printf ("adding to resumed threads with event list: %s",
  111. thread->ptid.to_string ().c_str ());
  112. m_resumed_with_pending_wait_status.push_back (*thread);
  113. }
  114. }
  115. /* See process-stratum-target.h. */
  116. void
  117. process_stratum_target::maybe_remove_resumed_with_pending_wait_status
  118. (thread_info *thread)
  119. {
  120. if (thread->resumed () && thread->has_pending_waitstatus ())
  121. {
  122. infrun_debug_printf ("removing from resumed threads with event list: %s",
  123. thread->ptid.to_string ().c_str ());
  124. gdb_assert (thread->resumed_with_pending_wait_status_node.is_linked ());
  125. auto it = m_resumed_with_pending_wait_status.iterator_to (*thread);
  126. m_resumed_with_pending_wait_status.erase (it);
  127. }
  128. else
  129. gdb_assert (!thread->resumed_with_pending_wait_status_node.is_linked ());
  130. }
  131. /* See process-stratum-target.h. */
  132. thread_info *
  133. process_stratum_target::random_resumed_with_pending_wait_status
  134. (inferior *inf, ptid_t filter_ptid)
  135. {
  136. auto matches = [inf, filter_ptid] (const thread_info &thread)
  137. {
  138. return thread.inf == inf && thread.ptid.matches (filter_ptid);
  139. };
  140. /* First see how many matching events we have. */
  141. const auto &l = m_resumed_with_pending_wait_status;
  142. unsigned int count = std::count_if (l.begin (), l.end (), matches);
  143. if (count == 0)
  144. return nullptr;
  145. /* Now randomly pick a thread out of those that match the criteria. */
  146. int random_selector
  147. = (int) ((count * (double) rand ()) / (RAND_MAX + 1.0));
  148. if (count > 1)
  149. infrun_debug_printf ("Found %u events, selecting #%d",
  150. count, random_selector);
  151. /* Select the Nth thread that matches. */
  152. auto it = std::find_if (l.begin (), l.end (),
  153. [&random_selector, &matches]
  154. (const thread_info &thread)
  155. {
  156. if (!matches (thread))
  157. return false;
  158. return random_selector-- == 0;
  159. });
  160. gdb_assert (it != l.end ());
  161. return &*it;
  162. }
  163. /* See process-stratum-target.h. */
  164. std::set<process_stratum_target *>
  165. all_non_exited_process_targets ()
  166. {
  167. /* Inferiors may share targets. To eliminate duplicates, use a set. */
  168. std::set<process_stratum_target *> targets;
  169. for (inferior *inf : all_non_exited_inferiors ())
  170. targets.insert (inf->process_target ());
  171. return targets;
  172. }
  173. /* See process-stratum-target.h. */
  174. void
  175. switch_to_target_no_thread (process_stratum_target *target)
  176. {
  177. for (inferior *inf : all_inferiors (target))
  178. {
  179. switch_to_inferior_no_thread (inf);
  180. break;
  181. }
  182. }