1/* The ptid_t type and common functions operating on it.
2
3   Copyright (C) 1986-2023 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 COMMON_PTID_H
21#define COMMON_PTID_H
22
23/* The ptid struct is a collection of the various "ids" necessary for
24   identifying the inferior process/thread being debugged.  This
25   consists of the process id (pid), lightweight process id (lwp) and
26   thread id (tid).  When manipulating ptids, the constructors,
27   accessors, and predicates declared in this file should be used.  Do
28   NOT access the struct ptid members directly.
29
30   process_stratum targets that handle threading themselves should
31   prefer using the ptid.lwp field, leaving the ptid.tid field for any
32   thread_stratum target that might want to sit on top.
33*/
34
35#include <functional>
36#include <string>
37#include "gdbsupport/common-types.h"
38
39class ptid_t
40{
41public:
42  /* Must have a trivial defaulted default constructor so that the
43     type remains POD.  */
44  ptid_t () noexcept = default;
45
46  /* Make a ptid given the necessary PID, LWP, and TID components.
47
48     A ptid with only a PID (LWP and TID equal to zero) is usually used to
49     represent a whole process, including all its lwps/threads.  */
50
51  explicit constexpr ptid_t (int pid, long lwp = 0, ULONGEST tid = 0)
52    : m_pid (pid), m_lwp (lwp), m_tid (tid)
53  {}
54
55  /* Fetch the pid (process id) component from the ptid.  */
56
57  constexpr int pid () const
58  { return m_pid; }
59
60  /* Return true if the ptid's lwp member is non-zero.  */
61
62  constexpr bool lwp_p () const
63  { return m_lwp != 0; }
64
65  /* Fetch the lwp (lightweight process) component from the ptid.  */
66
67  constexpr long lwp () const
68  { return m_lwp; }
69
70  /* Return true if the ptid's tid member is non-zero.  */
71
72  constexpr bool tid_p () const
73  { return m_tid != 0; }
74
75  /* Fetch the tid (thread id) component from a ptid.  */
76
77  constexpr ULONGEST tid () const
78  { return m_tid; }
79
80  /* Return true if the ptid represents a whole process, including all its
81     lwps/threads.  Such ptids have the form of (pid, 0, 0), with
82     pid != -1.  */
83
84  constexpr bool is_pid () const
85  {
86    return (*this != make_null ()
87	    && *this != make_minus_one ()
88	    && m_lwp == 0
89	    && m_tid == 0);
90  }
91
92  /* Compare two ptids to see if they are equal.  */
93
94  constexpr bool operator== (const ptid_t &other) const
95  {
96    return (m_pid == other.m_pid
97	    && m_lwp == other.m_lwp
98	    && m_tid == other.m_tid);
99  }
100
101  /* Compare two ptids to see if they are different.  */
102
103  constexpr bool operator!= (const ptid_t &other) const
104  {
105    return !(*this == other);
106  }
107
108  /* Return true if the ptid matches FILTER.  FILTER can be the wild
109     card MINUS_ONE_PTID (all ptids match it); can be a ptid representing
110     a process (ptid.is_pid () returns true), in which case, all lwps and
111     threads of that given process match, lwps and threads of other
112     processes do not; or, it can represent a specific thread, in which
113     case, only that thread will match true.  The ptid must represent a
114     specific LWP or THREAD, it can never be a wild card.  */
115
116  constexpr bool matches (const ptid_t &filter) const
117  {
118    return (/* If filter represents any ptid, it's always a match.  */
119	    filter == make_minus_one ()
120	    /* If filter is only a pid, any ptid with that pid
121	       matches.  */
122	    || (filter.is_pid () && m_pid == filter.pid ())
123
124	    /* Otherwise, this ptid only matches if it's exactly equal
125	       to filter.  */
126	    || *this == filter);
127  }
128
129  /* Return a string representation of the ptid.
130
131     This is only meant to be used in debug messages.  */
132
133  std::string to_string () const;
134
135  /* Make a null ptid.  */
136
137  static constexpr ptid_t make_null ()
138  { return ptid_t (0, 0, 0); }
139
140  /* Make a minus one ptid.  */
141
142  static constexpr ptid_t make_minus_one ()
143  { return ptid_t (-1, 0, 0); }
144
145private:
146  /* Process id.  */
147  int m_pid;
148
149  /* Lightweight process id.  */
150  long m_lwp;
151
152  /* Thread id.  */
153  ULONGEST m_tid;
154};
155
156/* Functor to hash a ptid.  */
157
158struct hash_ptid
159{
160  size_t operator() (const ptid_t &ptid) const
161  {
162    std::hash<long> long_hash;
163
164    return (long_hash (ptid.pid ())
165	    + long_hash (ptid.lwp ())
166	    + long_hash (ptid.tid ()));
167  }
168};
169
170/* The null or zero ptid, often used to indicate no process. */
171
172extern const ptid_t null_ptid;
173
174/* The (-1,0,0) ptid, often used to indicate either an error condition
175   or a "don't care" condition, i.e, "run all threads."  */
176
177extern const ptid_t minus_one_ptid;
178
179#endif /* COMMON_PTID_H */
180