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