xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/process-stratum-target.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17f2ac410Schristos /* Abstract base class inherited by all process_stratum targets
27f2ac410Schristos 
3*6881a400Schristos    Copyright (C) 2018-2023 Free Software Foundation, Inc.
47f2ac410Schristos 
57f2ac410Schristos    This file is part of GDB.
67f2ac410Schristos 
77f2ac410Schristos    This program is free software; you can redistribute it and/or modify
87f2ac410Schristos    it under the terms of the GNU General Public License as published by
97f2ac410Schristos    the Free Software Foundation; either version 3 of the License, or
107f2ac410Schristos    (at your option) any later version.
117f2ac410Schristos 
127f2ac410Schristos    This program is distributed in the hope that it will be useful,
137f2ac410Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
147f2ac410Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157f2ac410Schristos    GNU General Public License for more details.
167f2ac410Schristos 
177f2ac410Schristos    You should have received a copy of the GNU General Public License
187f2ac410Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
197f2ac410Schristos 
207f2ac410Schristos #ifndef PROCESS_STRATUM_TARGET_H
217f2ac410Schristos #define PROCESS_STRATUM_TARGET_H
227f2ac410Schristos 
237f2ac410Schristos #include "target.h"
247d62b00eSchristos #include <set>
25*6881a400Schristos #include "gdbsupport/intrusive_list.h"
26*6881a400Schristos #include "gdbsupport/gdb-checked-static-cast.h"
27*6881a400Schristos #include "gdbthread.h"
287f2ac410Schristos 
297f2ac410Schristos /* Abstract base class inherited by all process_stratum targets.  */
307f2ac410Schristos 
317f2ac410Schristos class process_stratum_target : public target_ops
327f2ac410Schristos {
337f2ac410Schristos public:
347f2ac410Schristos   ~process_stratum_target () override = 0;
357f2ac410Schristos 
367d62b00eSchristos   strata stratum () const final override { return process_stratum; }
377d62b00eSchristos 
387d62b00eSchristos   /* Return a string representation of this target's open connection.
397d62b00eSchristos      This string is used to distinguish different instances of a given
407d62b00eSchristos      target type.  For example, when remote debugging, the target is
417d62b00eSchristos      called "remote", but since we may have more than one remote
427d62b00eSchristos      target open, connection_string() returns the connection serial
437d62b00eSchristos      connection name, e.g., "localhost:10001", "192.168.0.1:20000",
447d62b00eSchristos      etc.  This string is shown in several places, e.g., in "info
457d62b00eSchristos      connections" and "info inferiors".  */
467d62b00eSchristos   virtual const char *connection_string () { return nullptr; }
477f2ac410Schristos 
487f2ac410Schristos   /* We must default these because they must be implemented by any
497f2ac410Schristos      target that can run.  */
507f2ac410Schristos   bool can_async_p () override { return false; }
517f2ac410Schristos   bool supports_non_stop () override { return false; }
527f2ac410Schristos   bool supports_disable_randomization () override { return false; }
537f2ac410Schristos 
547f2ac410Schristos   /* This default implementation returns the inferior's address
557f2ac410Schristos      space.  */
567f2ac410Schristos   struct address_space *thread_address_space (ptid_t ptid) override;
577f2ac410Schristos 
587f2ac410Schristos   /* This default implementation always returns target_gdbarch ().  */
597f2ac410Schristos   struct gdbarch *thread_architecture (ptid_t ptid) override;
607f2ac410Schristos 
617f2ac410Schristos   /* Default implementations for process_stratum targets.  Return true
627f2ac410Schristos      if there's a selected inferior, false otherwise.  */
637f2ac410Schristos   bool has_all_memory () override;
647f2ac410Schristos   bool has_memory () override;
657f2ac410Schristos   bool has_stack () override;
667f2ac410Schristos   bool has_registers () override;
677d62b00eSchristos   bool has_execution (inferior *inf) override;
687d62b00eSchristos 
69*6881a400Schristos   /* Default implementation of follow_exec.
70*6881a400Schristos 
71*6881a400Schristos      If the current inferior and FOLLOW_INF are different (execution continues
72*6881a400Schristos      in a new inferior), push this process target to FOLLOW_INF's target stack
73*6881a400Schristos      and add an initial thread to FOLLOW_INF.  */
74*6881a400Schristos   void follow_exec (inferior *follow_inf, ptid_t ptid,
75*6881a400Schristos 		    const char *execd_pathname) override;
76*6881a400Schristos 
77*6881a400Schristos   /* Default implementation of follow_fork.
78*6881a400Schristos 
79*6881a400Schristos      If a child inferior was created by infrun while following the fork
80*6881a400Schristos      (CHILD_INF is non-nullptr), push this target on CHILD_INF's target stack
81*6881a400Schristos      and add an initial thread with ptid CHILD_PTID.  */
82*6881a400Schristos   void follow_fork (inferior *child_inf, ptid_t child_ptid,
83*6881a400Schristos 		    target_waitkind fork_kind, bool follow_child,
84*6881a400Schristos 		    bool detach_on_fork) override;
85*6881a400Schristos 
867d62b00eSchristos   /* True if any thread is, or may be executing.  We need to track
877d62b00eSchristos      this separately because until we fully sync the thread list, we
887d62b00eSchristos      won't know whether the target is fully stopped, even if we see
897d62b00eSchristos      stop events for all known threads, because any of those threads
907d62b00eSchristos      may have spawned new threads we haven't heard of yet.  */
917d62b00eSchristos   bool threads_executing = false;
927d62b00eSchristos 
93*6881a400Schristos   /* If THREAD is resumed and has a pending wait status, add it to the
94*6881a400Schristos      target's "resumed with pending wait status" list.  */
95*6881a400Schristos   void maybe_add_resumed_with_pending_wait_status (thread_info *thread);
96*6881a400Schristos 
97*6881a400Schristos   /* If THREAD is resumed and has a pending wait status, remove it from the
98*6881a400Schristos      target's "resumed with pending wait status" list.  */
99*6881a400Schristos   void maybe_remove_resumed_with_pending_wait_status (thread_info *thread);
100*6881a400Schristos 
101*6881a400Schristos   /* Return true if this target has at least one resumed thread with a pending
102*6881a400Schristos      wait status.  */
103*6881a400Schristos   bool has_resumed_with_pending_wait_status () const
104*6881a400Schristos   { return !m_resumed_with_pending_wait_status.empty (); }
105*6881a400Schristos 
106*6881a400Schristos   /* Return a random resumed thread with pending wait status belonging to INF
107*6881a400Schristos      and matching FILTER_PTID.  */
108*6881a400Schristos   thread_info *random_resumed_with_pending_wait_status
109*6881a400Schristos     (inferior *inf, ptid_t filter_ptid);
110*6881a400Schristos 
1117d62b00eSchristos   /* The connection number.  Visible in "info connections".  */
1127d62b00eSchristos   int connection_number = 0;
113*6881a400Schristos 
114*6881a400Schristos   /* Whether resumed threads must be committed to the target.
115*6881a400Schristos 
116*6881a400Schristos      When true, resumed threads must be committed to the execution
117*6881a400Schristos      target.
118*6881a400Schristos 
119*6881a400Schristos      When false, the target may leave resumed threads stopped when
120*6881a400Schristos      it's convenient or efficient to do so.  When the core requires
121*6881a400Schristos      resumed threads to be committed again, this is set back to true
122*6881a400Schristos      and calls the `commit_resumed` method to allow the target to do
123*6881a400Schristos      so.
124*6881a400Schristos 
125*6881a400Schristos      To simplify the implementation of targets, the following methods
126*6881a400Schristos      are guaranteed to be called with COMMIT_RESUMED_STATE set to
127*6881a400Schristos      false:
128*6881a400Schristos 
129*6881a400Schristos        - resume
130*6881a400Schristos        - stop
131*6881a400Schristos        - wait
132*6881a400Schristos 
133*6881a400Schristos      Knowing this, the target doesn't need to implement different
134*6881a400Schristos      behaviors depending on the COMMIT_RESUMED_STATE, and can simply
135*6881a400Schristos      assume that it is false.
136*6881a400Schristos 
137*6881a400Schristos      Targets can take advantage of this to batch resumption requests,
138*6881a400Schristos      for example.  In that case, the target doesn't actually resume in
139*6881a400Schristos      its `resume` implementation.  Instead, it takes note of the
140*6881a400Schristos      resumption intent in `resume` and defers the actual resumption to
141*6881a400Schristos      `commit_resumed`.  For example, the remote target uses this to
142*6881a400Schristos      coalesce multiple resumption requests in a single vCont
143*6881a400Schristos      packet.  */
144*6881a400Schristos   bool commit_resumed_state = false;
145*6881a400Schristos 
146*6881a400Schristos private:
147*6881a400Schristos   /* List of threads managed by this target which simultaneously are resumed
148*6881a400Schristos      and have a pending wait status.
149*6881a400Schristos 
150*6881a400Schristos      This is done for optimization reasons, it would be possible to walk the
151*6881a400Schristos      inferior thread lists to find these threads.  But since this is something
152*6881a400Schristos      we need to do quite frequently in the hot path, maintaining this list
153*6881a400Schristos      avoids walking the thread lists repeatedly.  */
154*6881a400Schristos   thread_info_resumed_with_pending_wait_status_list
155*6881a400Schristos     m_resumed_with_pending_wait_status;
1567f2ac410Schristos };
1577f2ac410Schristos 
1587d62b00eSchristos /* Downcast TARGET to process_stratum_target.  */
1597d62b00eSchristos 
1607d62b00eSchristos static inline process_stratum_target *
1617d62b00eSchristos as_process_stratum_target (target_ops *target)
1627d62b00eSchristos {
1637d62b00eSchristos   gdb_assert (target->stratum () == process_stratum);
164*6881a400Schristos   return gdb::checked_static_cast<process_stratum_target *> (target);
1657d62b00eSchristos }
1667d62b00eSchristos 
1677d62b00eSchristos /* Return a collection of targets that have non-exited inferiors.  */
1687d62b00eSchristos 
1697d62b00eSchristos extern std::set<process_stratum_target *> all_non_exited_process_targets ();
1707d62b00eSchristos 
1717d62b00eSchristos /* Switch to the first inferior (and program space) of TARGET, and
1727d62b00eSchristos    switch to no thread selected.  */
1737d62b00eSchristos 
1747d62b00eSchristos extern void switch_to_target_no_thread (process_stratum_target *target);
1757d62b00eSchristos 
1767f2ac410Schristos #endif /* !defined (PROCESS_STRATUM_TARGET_H) */
177