xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/ptid.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
18dffb485Schristos /* The ptid_t type and common functions operating on it.
28dffb485Schristos 
3*5ba1f45fSchristos    Copyright (C) 1986-2024 Free Software Foundation, Inc.
48dffb485Schristos 
58dffb485Schristos    This file is part of GDB.
68dffb485Schristos 
78dffb485Schristos    This program is free software; you can redistribute it and/or modify
88dffb485Schristos    it under the terms of the GNU General Public License as published by
98dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
108dffb485Schristos    (at your option) any later version.
118dffb485Schristos 
128dffb485Schristos    This program is distributed in the hope that it will be useful,
138dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
148dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158dffb485Schristos    GNU General Public License for more details.
168dffb485Schristos 
178dffb485Schristos    You should have received a copy of the GNU General Public License
188dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos #ifndef COMMON_PTID_H
218dffb485Schristos #define COMMON_PTID_H
228dffb485Schristos 
238dffb485Schristos /* The ptid struct is a collection of the various "ids" necessary for
248dffb485Schristos    identifying the inferior process/thread being debugged.  This
258dffb485Schristos    consists of the process id (pid), lightweight process id (lwp) and
268dffb485Schristos    thread id (tid).  When manipulating ptids, the constructors,
278dffb485Schristos    accessors, and predicates declared in this file should be used.  Do
288dffb485Schristos    NOT access the struct ptid members directly.
298dffb485Schristos 
308dffb485Schristos    process_stratum targets that handle threading themselves should
318dffb485Schristos    prefer using the ptid.lwp field, leaving the ptid.tid field for any
328dffb485Schristos    thread_stratum target that might want to sit on top.
338dffb485Schristos */
348dffb485Schristos 
358dffb485Schristos #include <functional>
364b169a6bSchristos #include <string>
374b169a6bSchristos #include "gdbsupport/common-types.h"
388dffb485Schristos 
398dffb485Schristos class ptid_t
408dffb485Schristos {
418dffb485Schristos public:
42*5ba1f45fSchristos   using pid_type = int;
43*5ba1f45fSchristos   using lwp_type = long;
44*5ba1f45fSchristos   using tid_type = ULONGEST;
45*5ba1f45fSchristos 
468dffb485Schristos   /* Must have a trivial defaulted default constructor so that the
478dffb485Schristos      type remains POD.  */
488dffb485Schristos   ptid_t () noexcept = default;
498dffb485Schristos 
508dffb485Schristos   /* Make a ptid given the necessary PID, LWP, and TID components.
518dffb485Schristos 
528dffb485Schristos      A ptid with only a PID (LWP and TID equal to zero) is usually used to
538dffb485Schristos      represent a whole process, including all its lwps/threads.  */
548dffb485Schristos 
55*5ba1f45fSchristos   explicit constexpr ptid_t (pid_type pid, lwp_type lwp = 0, tid_type tid = 0)
568dffb485Schristos     : m_pid (pid), m_lwp (lwp), m_tid (tid)
578dffb485Schristos   {}
588dffb485Schristos 
598dffb485Schristos   /* Fetch the pid (process id) component from the ptid.  */
608dffb485Schristos 
61*5ba1f45fSchristos   constexpr pid_type pid () const
628dffb485Schristos   { return m_pid; }
638dffb485Schristos 
648dffb485Schristos   /* Return true if the ptid's lwp member is non-zero.  */
658dffb485Schristos 
668dffb485Schristos   constexpr bool lwp_p () const
678dffb485Schristos   { return m_lwp != 0; }
688dffb485Schristos 
698dffb485Schristos   /* Fetch the lwp (lightweight process) component from the ptid.  */
708dffb485Schristos 
71*5ba1f45fSchristos   constexpr lwp_type lwp () const
728dffb485Schristos   { return m_lwp; }
738dffb485Schristos 
748dffb485Schristos   /* Return true if the ptid's tid member is non-zero.  */
758dffb485Schristos 
768dffb485Schristos   constexpr bool tid_p () const
778dffb485Schristos   { return m_tid != 0; }
788dffb485Schristos 
798dffb485Schristos   /* Fetch the tid (thread id) component from a ptid.  */
808dffb485Schristos 
81*5ba1f45fSchristos   constexpr tid_type tid () const
828dffb485Schristos   { return m_tid; }
838dffb485Schristos 
848dffb485Schristos   /* Return true if the ptid represents a whole process, including all its
858dffb485Schristos      lwps/threads.  Such ptids have the form of (pid, 0, 0), with
868dffb485Schristos      pid != -1.  */
878dffb485Schristos 
888dffb485Schristos   constexpr bool is_pid () const
898dffb485Schristos   {
908dffb485Schristos     return (*this != make_null ()
918dffb485Schristos 	    && *this != make_minus_one ()
928dffb485Schristos 	    && m_lwp == 0
938dffb485Schristos 	    && m_tid == 0);
948dffb485Schristos   }
958dffb485Schristos 
968dffb485Schristos   /* Compare two ptids to see if they are equal.  */
978dffb485Schristos 
988dffb485Schristos   constexpr bool operator== (const ptid_t &other) const
998dffb485Schristos   {
1008dffb485Schristos     return (m_pid == other.m_pid
1018dffb485Schristos 	    && m_lwp == other.m_lwp
1028dffb485Schristos 	    && m_tid == other.m_tid);
1038dffb485Schristos   }
1048dffb485Schristos 
1058dffb485Schristos   /* Compare two ptids to see if they are different.  */
1068dffb485Schristos 
1078dffb485Schristos   constexpr bool operator!= (const ptid_t &other) const
1088dffb485Schristos   {
1098dffb485Schristos     return !(*this == other);
1108dffb485Schristos   }
1118dffb485Schristos 
1128dffb485Schristos   /* Return true if the ptid matches FILTER.  FILTER can be the wild
1138dffb485Schristos      card MINUS_ONE_PTID (all ptids match it); can be a ptid representing
1148dffb485Schristos      a process (ptid.is_pid () returns true), in which case, all lwps and
1158dffb485Schristos      threads of that given process match, lwps and threads of other
1168dffb485Schristos      processes do not; or, it can represent a specific thread, in which
1178dffb485Schristos      case, only that thread will match true.  The ptid must represent a
1188dffb485Schristos      specific LWP or THREAD, it can never be a wild card.  */
1198dffb485Schristos 
1208dffb485Schristos   constexpr bool matches (const ptid_t &filter) const
1218dffb485Schristos   {
1228dffb485Schristos     return (/* If filter represents any ptid, it's always a match.  */
1238dffb485Schristos 	    filter == make_minus_one ()
1248dffb485Schristos 	    /* If filter is only a pid, any ptid with that pid
1258dffb485Schristos 	       matches.  */
1268dffb485Schristos 	    || (filter.is_pid () && m_pid == filter.pid ())
1278dffb485Schristos 
1288dffb485Schristos 	    /* Otherwise, this ptid only matches if it's exactly equal
1298dffb485Schristos 	       to filter.  */
1308dffb485Schristos 	    || *this == filter);
1318dffb485Schristos   }
1328dffb485Schristos 
1334b169a6bSchristos   /* Return a string representation of the ptid.
1344b169a6bSchristos 
1354b169a6bSchristos      This is only meant to be used in debug messages.  */
1364b169a6bSchristos 
1374b169a6bSchristos   std::string to_string () const;
1384b169a6bSchristos 
1398dffb485Schristos   /* Make a null ptid.  */
1408dffb485Schristos 
1418dffb485Schristos   static constexpr ptid_t make_null ()
1428dffb485Schristos   { return ptid_t (0, 0, 0); }
1438dffb485Schristos 
1448dffb485Schristos   /* Make a minus one ptid.  */
1458dffb485Schristos 
1468dffb485Schristos   static constexpr ptid_t make_minus_one ()
1478dffb485Schristos   { return ptid_t (-1, 0, 0); }
1488dffb485Schristos 
1498dffb485Schristos private:
1508dffb485Schristos   /* Process id.  */
151*5ba1f45fSchristos   pid_type m_pid;
1528dffb485Schristos 
1538dffb485Schristos   /* Lightweight process id.  */
154*5ba1f45fSchristos   lwp_type m_lwp;
1558dffb485Schristos 
1568dffb485Schristos   /* Thread id.  */
157*5ba1f45fSchristos   tid_type m_tid;
1588dffb485Schristos };
1598dffb485Schristos 
160*5ba1f45fSchristos namespace std
161*5ba1f45fSchristos {
162*5ba1f45fSchristos template<>
163*5ba1f45fSchristos struct hash<ptid_t>
1648dffb485Schristos {
1658dffb485Schristos   size_t operator() (const ptid_t &ptid) const
1668dffb485Schristos   {
1678dffb485Schristos     std::hash<long> long_hash;
1688dffb485Schristos 
1698dffb485Schristos     return (long_hash (ptid.pid ())
1708dffb485Schristos 	    + long_hash (ptid.lwp ())
1718dffb485Schristos 	    + long_hash (ptid.tid ()));
1728dffb485Schristos   }
1738dffb485Schristos };
174*5ba1f45fSchristos }
1758dffb485Schristos 
1768dffb485Schristos /* The null or zero ptid, often used to indicate no process. */
1778dffb485Schristos 
1788dffb485Schristos extern const ptid_t null_ptid;
1798dffb485Schristos 
1808dffb485Schristos /* The (-1,0,0) ptid, often used to indicate either an error condition
1818dffb485Schristos    or a "don't care" condition, i.e, "run all threads."  */
1828dffb485Schristos 
1838dffb485Schristos extern const ptid_t minus_one_ptid;
1848dffb485Schristos 
1858dffb485Schristos #endif /* COMMON_PTID_H */
186