process-stratum-target.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef PROCESS_STRATUM_TARGET_H
  15. #define PROCESS_STRATUM_TARGET_H
  16. #include "target.h"
  17. #include <set>
  18. #include "gdbsupport/intrusive_list.h"
  19. #include "gdbthread.h"
  20. /* Abstract base class inherited by all process_stratum targets. */
  21. class process_stratum_target : public target_ops
  22. {
  23. public:
  24. ~process_stratum_target () override = 0;
  25. strata stratum () const final override { return process_stratum; }
  26. /* Return a string representation of this target's open connection.
  27. This string is used to distinguish different instances of a given
  28. target type. For example, when remote debugging, the target is
  29. called "remote", but since we may have more than one remote
  30. target open, connection_string() returns the connection serial
  31. connection name, e.g., "localhost:10001", "192.168.0.1:20000",
  32. etc. This string is shown in several places, e.g., in "info
  33. connections" and "info inferiors". */
  34. virtual const char *connection_string () { return nullptr; }
  35. /* We must default these because they must be implemented by any
  36. target that can run. */
  37. bool can_async_p () override { return false; }
  38. bool supports_non_stop () override { return false; }
  39. bool supports_disable_randomization () override { return false; }
  40. /* This default implementation returns the inferior's address
  41. space. */
  42. struct address_space *thread_address_space (ptid_t ptid) override;
  43. /* This default implementation always returns target_gdbarch (). */
  44. struct gdbarch *thread_architecture (ptid_t ptid) override;
  45. /* Default implementations for process_stratum targets. Return true
  46. if there's a selected inferior, false otherwise. */
  47. bool has_all_memory () override;
  48. bool has_memory () override;
  49. bool has_stack () override;
  50. bool has_registers () override;
  51. bool has_execution (inferior *inf) override;
  52. /* Default implementation of follow_exec.
  53. If the current inferior and FOLLOW_INF are different (execution continues
  54. in a new inferior), push this process target to FOLLOW_INF's target stack
  55. and add an initial thread to FOLLOW_INF. */
  56. void follow_exec (inferior *follow_inf, ptid_t ptid,
  57. const char *execd_pathname) override;
  58. /* Default implementation of follow_fork.
  59. If a child inferior was created by infrun while following the fork
  60. (CHILD_INF is non-nullptr), push this target on CHILD_INF's target stack
  61. and add an initial thread with ptid CHILD_PTID. */
  62. void follow_fork (inferior *child_inf, ptid_t child_ptid,
  63. target_waitkind fork_kind, bool follow_child,
  64. bool detach_on_fork) override;
  65. /* True if any thread is, or may be executing. We need to track
  66. this separately because until we fully sync the thread list, we
  67. won't know whether the target is fully stopped, even if we see
  68. stop events for all known threads, because any of those threads
  69. may have spawned new threads we haven't heard of yet. */
  70. bool threads_executing = false;
  71. /* If THREAD is resumed and has a pending wait status, add it to the
  72. target's "resumed with pending wait status" list. */
  73. void maybe_add_resumed_with_pending_wait_status (thread_info *thread);
  74. /* If THREAD is resumed and has a pending wait status, remove it from the
  75. target's "resumed with pending wait status" list. */
  76. void maybe_remove_resumed_with_pending_wait_status (thread_info *thread);
  77. /* Return true if this target has at least one resumed thread with a pending
  78. wait status. */
  79. bool has_resumed_with_pending_wait_status () const
  80. { return !m_resumed_with_pending_wait_status.empty (); }
  81. /* Return a random resumed thread with pending wait status belonging to INF
  82. and matching FILTER_PTID. */
  83. thread_info *random_resumed_with_pending_wait_status
  84. (inferior *inf, ptid_t filter_ptid);
  85. /* The connection number. Visible in "info connections". */
  86. int connection_number = 0;
  87. /* Whether resumed threads must be committed to the target.
  88. When true, resumed threads must be committed to the execution
  89. target.
  90. When false, the target may leave resumed threads stopped when
  91. it's convenient or efficient to do so. When the core requires
  92. resumed threads to be committed again, this is set back to true
  93. and calls the `commit_resumed` method to allow the target to do
  94. so.
  95. To simplify the implementation of targets, the following methods
  96. are guaranteed to be called with COMMIT_RESUMED_STATE set to
  97. false:
  98. - resume
  99. - stop
  100. - wait
  101. Knowing this, the target doesn't need to implement different
  102. behaviors depending on the COMMIT_RESUMED_STATE, and can simply
  103. assume that it is false.
  104. Targets can take advantage of this to batch resumption requests,
  105. for example. In that case, the target doesn't actually resume in
  106. its `resume` implementation. Instead, it takes note of the
  107. resumption intent in `resume` and defers the actual resumption to
  108. `commit_resumed`. For example, the remote target uses this to
  109. coalesce multiple resumption requests in a single vCont
  110. packet. */
  111. bool commit_resumed_state = false;
  112. private:
  113. /* List of threads managed by this target which simultaneously are resumed
  114. and have a pending wait status.
  115. This is done for optimization reasons, it would be possible to walk the
  116. inferior thread lists to find these threads. But since this is something
  117. we need to do quite frequently in the hot path, maintaining this list
  118. avoids walking the thread lists repeatedly. */
  119. thread_info_resumed_with_pending_wait_status_list
  120. m_resumed_with_pending_wait_status;
  121. };
  122. /* Downcast TARGET to process_stratum_target. */
  123. static inline process_stratum_target *
  124. as_process_stratum_target (target_ops *target)
  125. {
  126. gdb_assert (target->stratum () == process_stratum);
  127. return static_cast<process_stratum_target *> (target);
  128. }
  129. /* Return a collection of targets that have non-exited inferiors. */
  130. extern std::set<process_stratum_target *> all_non_exited_process_targets ();
  131. /* Switch to the first inferior (and program space) of TARGET, and
  132. switch to no thread selected. */
  133. extern void switch_to_target_no_thread (process_stratum_target *target);
  134. #endif /* !defined (PROCESS_STRATUM_TARGET_H) */