ptid.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* The ptid_t type and common functions operating on it.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_PTID_H
  15. #define COMMON_PTID_H
  16. /* The ptid struct is a collection of the various "ids" necessary for
  17. identifying the inferior process/thread being debugged. This
  18. consists of the process id (pid), lightweight process id (lwp) and
  19. thread id (tid). When manipulating ptids, the constructors,
  20. accessors, and predicates declared in this file should be used. Do
  21. NOT access the struct ptid members directly.
  22. process_stratum targets that handle threading themselves should
  23. prefer using the ptid.lwp field, leaving the ptid.tid field for any
  24. thread_stratum target that might want to sit on top.
  25. */
  26. #include <functional>
  27. #include <string>
  28. #include "gdbsupport/common-types.h"
  29. class ptid_t
  30. {
  31. public:
  32. /* Must have a trivial defaulted default constructor so that the
  33. type remains POD. */
  34. ptid_t () noexcept = default;
  35. /* Make a ptid given the necessary PID, LWP, and TID components.
  36. A ptid with only a PID (LWP and TID equal to zero) is usually used to
  37. represent a whole process, including all its lwps/threads. */
  38. explicit constexpr ptid_t (int pid, long lwp = 0, ULONGEST tid = 0)
  39. : m_pid (pid), m_lwp (lwp), m_tid (tid)
  40. {}
  41. /* Fetch the pid (process id) component from the ptid. */
  42. constexpr int pid () const
  43. { return m_pid; }
  44. /* Return true if the ptid's lwp member is non-zero. */
  45. constexpr bool lwp_p () const
  46. { return m_lwp != 0; }
  47. /* Fetch the lwp (lightweight process) component from the ptid. */
  48. constexpr long lwp () const
  49. { return m_lwp; }
  50. /* Return true if the ptid's tid member is non-zero. */
  51. constexpr bool tid_p () const
  52. { return m_tid != 0; }
  53. /* Fetch the tid (thread id) component from a ptid. */
  54. constexpr ULONGEST tid () const
  55. { return m_tid; }
  56. /* Return true if the ptid represents a whole process, including all its
  57. lwps/threads. Such ptids have the form of (pid, 0, 0), with
  58. pid != -1. */
  59. constexpr bool is_pid () const
  60. {
  61. return (*this != make_null ()
  62. && *this != make_minus_one ()
  63. && m_lwp == 0
  64. && m_tid == 0);
  65. }
  66. /* Compare two ptids to see if they are equal. */
  67. constexpr bool operator== (const ptid_t &other) const
  68. {
  69. return (m_pid == other.m_pid
  70. && m_lwp == other.m_lwp
  71. && m_tid == other.m_tid);
  72. }
  73. /* Compare two ptids to see if they are different. */
  74. constexpr bool operator!= (const ptid_t &other) const
  75. {
  76. return !(*this == other);
  77. }
  78. /* Return true if the ptid matches FILTER. FILTER can be the wild
  79. card MINUS_ONE_PTID (all ptids match it); can be a ptid representing
  80. a process (ptid.is_pid () returns true), in which case, all lwps and
  81. threads of that given process match, lwps and threads of other
  82. processes do not; or, it can represent a specific thread, in which
  83. case, only that thread will match true. The ptid must represent a
  84. specific LWP or THREAD, it can never be a wild card. */
  85. constexpr bool matches (const ptid_t &filter) const
  86. {
  87. return (/* If filter represents any ptid, it's always a match. */
  88. filter == make_minus_one ()
  89. /* If filter is only a pid, any ptid with that pid
  90. matches. */
  91. || (filter.is_pid () && m_pid == filter.pid ())
  92. /* Otherwise, this ptid only matches if it's exactly equal
  93. to filter. */
  94. || *this == filter);
  95. }
  96. /* Return a string representation of the ptid.
  97. This is only meant to be used in debug messages. */
  98. std::string to_string () const;
  99. /* Make a null ptid. */
  100. static constexpr ptid_t make_null ()
  101. { return ptid_t (0, 0, 0); }
  102. /* Make a minus one ptid. */
  103. static constexpr ptid_t make_minus_one ()
  104. { return ptid_t (-1, 0, 0); }
  105. private:
  106. /* Process id. */
  107. int m_pid;
  108. /* Lightweight process id. */
  109. long m_lwp;
  110. /* Thread id. */
  111. ULONGEST m_tid;
  112. };
  113. /* Functor to hash a ptid. */
  114. struct hash_ptid
  115. {
  116. size_t operator() (const ptid_t &ptid) const
  117. {
  118. std::hash<long> long_hash;
  119. return (long_hash (ptid.pid ())
  120. + long_hash (ptid.lwp ())
  121. + long_hash (ptid.tid ()));
  122. }
  123. };
  124. /* The null or zero ptid, often used to indicate no process. */
  125. extern const ptid_t null_ptid;
  126. /* The (-1,0,0) ptid, often used to indicate either an error condition
  127. or a "don't care" condition, i.e, "run all threads." */
  128. extern const ptid_t minus_one_ptid;
  129. #endif /* COMMON_PTID_H */