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