Home | History | Annotate | Line # | Download | only in gdb
      1 /* Abstract base class inherited by all process_stratum targets
      2 
      3    Copyright (C) 2018-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #ifndef PROCESS_STRATUM_TARGET_H
     21 #define PROCESS_STRATUM_TARGET_H
     22 
     23 #include "target.h"
     24 #include <set>
     25 #include "gdbsupport/intrusive_list.h"
     26 #include "gdbsupport/gdb-checked-static-cast.h"
     27 #include "gdbthread.h"
     28 
     29 /* Abstract base class inherited by all process_stratum targets.  */
     30 
     31 class process_stratum_target : public target_ops
     32 {
     33 public:
     34   ~process_stratum_target () override = 0;
     35 
     36   strata stratum () const final override { return process_stratum; }
     37 
     38   /* Return a string representation of this target's open connection.
     39      This string is used to distinguish different instances of a given
     40      target type.  For example, when remote debugging, the target is
     41      called "remote", but since we may have more than one remote
     42      target open, connection_string() returns the connection serial
     43      connection name, e.g., "localhost:10001", "192.168.0.1:20000",
     44      etc.  This string is shown in several places, e.g., in "info
     45      connections" and "info inferiors".  */
     46   virtual const char *connection_string () { return nullptr; }
     47 
     48   /* We must default these because they must be implemented by any
     49      target that can run.  */
     50   bool can_async_p () override { return false; }
     51   bool supports_non_stop () override { return false; }
     52   bool supports_disable_randomization () override { return false; }
     53 
     54   /* This default implementation always returns the current inferior's
     55      gdbarch.  */
     56   struct gdbarch *thread_architecture (ptid_t ptid) override;
     57 
     58   /* Default implementations for process_stratum targets.  Return true
     59      if there's a selected inferior, false otherwise.  */
     60   bool has_all_memory () override;
     61   bool has_memory () override;
     62   bool has_stack () override;
     63   bool has_registers () override;
     64   bool has_execution (inferior *inf) override;
     65 
     66   /* Default implementation of follow_exec.
     67 
     68      If the current inferior and FOLLOW_INF are different (execution continues
     69      in a new inferior), push this process target to FOLLOW_INF's target stack
     70      and add an initial thread to FOLLOW_INF.  */
     71   void follow_exec (inferior *follow_inf, ptid_t ptid,
     72 		    const char *execd_pathname) override;
     73 
     74   /* Default implementation of follow_fork.
     75 
     76      If a child inferior was created by infrun while following the fork
     77      (CHILD_INF is non-nullptr), push this target on CHILD_INF's target stack
     78      and add an initial thread with ptid CHILD_PTID.  */
     79   void follow_fork (inferior *child_inf, ptid_t child_ptid,
     80 		    target_waitkind fork_kind, bool follow_child,
     81 		    bool detach_on_fork) override;
     82 
     83   /* True if any thread is, or may be executing.  We need to track
     84      this separately because until we fully sync the thread list, we
     85      won't know whether the target is fully stopped, even if we see
     86      stop events for all known threads, because any of those threads
     87      may have spawned new threads we haven't heard of yet.  */
     88   bool threads_executing = false;
     89 
     90   /* If THREAD is resumed and has a pending wait status, add it to the
     91      target's "resumed with pending wait status" list.  */
     92   void maybe_add_resumed_with_pending_wait_status (thread_info *thread);
     93 
     94   /* If THREAD is resumed and has a pending wait status, remove it from the
     95      target's "resumed with pending wait status" list.  */
     96   void maybe_remove_resumed_with_pending_wait_status (thread_info *thread);
     97 
     98   /* Return true if this target has at least one resumed thread with a pending
     99      wait status.  */
    100   bool has_resumed_with_pending_wait_status () const
    101   { return !m_resumed_with_pending_wait_status.empty (); }
    102 
    103   /* Return a random resumed thread with pending wait status belonging to INF
    104      and matching FILTER_PTID.  */
    105   thread_info *random_resumed_with_pending_wait_status
    106     (inferior *inf, ptid_t filter_ptid);
    107 
    108   /* Search function to lookup a (non-exited) thread by 'ptid'.  */
    109   thread_info *find_thread (ptid_t ptid);
    110 
    111   /* The connection number.  Visible in "info connections".  */
    112   int connection_number = 0;
    113 
    114   /* Whether resumed threads must be committed to the target.
    115 
    116      When true, resumed threads must be committed to the execution
    117      target.
    118 
    119      When false, the target may leave resumed threads stopped when
    120      it's convenient or efficient to do so.  When the core requires
    121      resumed threads to be committed again, this is set back to true
    122      and calls the `commit_resumed` method to allow the target to do
    123      so.
    124 
    125      To simplify the implementation of targets, the following methods
    126      are guaranteed to be called with COMMIT_RESUMED_STATE set to
    127      false:
    128 
    129        - resume
    130        - stop
    131        - wait
    132 
    133      Knowing this, the target doesn't need to implement different
    134      behaviors depending on the COMMIT_RESUMED_STATE, and can simply
    135      assume that it is false.
    136 
    137      Targets can take advantage of this to batch resumption requests,
    138      for example.  In that case, the target doesn't actually resume in
    139      its `resume` implementation.  Instead, it takes note of the
    140      resumption intent in `resume` and defers the actual resumption to
    141      `commit_resumed`.  For example, the remote target uses this to
    142      coalesce multiple resumption requests in a single vCont
    143      packet.  */
    144   bool commit_resumed_state = false;
    145 
    146 private:
    147   /* List of threads managed by this target which simultaneously are resumed
    148      and have a pending wait status.
    149 
    150      This is done for optimization reasons, it would be possible to walk the
    151      inferior thread lists to find these threads.  But since this is something
    152      we need to do quite frequently in the hot path, maintaining this list
    153      avoids walking the thread lists repeatedly.  */
    154   thread_info_resumed_with_pending_wait_status_list
    155     m_resumed_with_pending_wait_status;
    156 };
    157 
    158 /* Downcast TARGET to process_stratum_target.  */
    159 
    160 static inline process_stratum_target *
    161 as_process_stratum_target (target_ops *target)
    162 {
    163   gdb_assert (target->stratum () == process_stratum);
    164   return gdb::checked_static_cast<process_stratum_target *> (target);
    165 }
    166 
    167 /* Return a collection of targets that have non-exited inferiors.  */
    168 
    169 extern std::set<process_stratum_target *> all_non_exited_process_targets ();
    170 
    171 /* Switch to the first inferior (and program space) of TARGET, and
    172    switch to no thread selected.  */
    173 
    174 extern void switch_to_target_no_thread (process_stratum_target *target);
    175 
    176 #endif /* !defined (PROCESS_STRATUM_TARGET_H) */
    177