From 436252de3e9de546001c4312d0863ce7e10aa200 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 6 Apr 2017 23:29:53 -0400 Subject: Class-ify ptid_t I grew a bit tired of using ptid_get_{lwp,pid,tid} and friends, so I decided to make it a bit easier to use by making it a proper class. The fields are now private, so it's not possible to change a ptid_t field by mistake. The new methods of ptid_t map to existing functions/practice like this: ptid_t (pid, lwp, tid) -> ptid_build (pid, lwp, tid) ptid_t (pid) -> pid_to_ptid (pid) ptid.is_pid () -> ptid_is_pid (ptid) ptid == other -> ptid_equal (ptid, other) ptid != other -> !ptid_equal (ptid, other) ptid.pid () -> ptid_get_pid (ptid) ptid.lwp_p () -> ptid_lwp_p (ptid) ptid.lwp () -> ptid_get_lwp (ptid) ptid.tid_p () -> ptid_tid_p (ptid) ptid.tid () -> ptid_get_tid (ptid) ptid.matches (filter) -> ptid_match (ptid, filter) I've replaced the implementation of the existing functions with calls to the new methods. People are encouraged to gradually switch to using the ptid_t methods instead of the functions (or we can change them all in one pass eventually). Also, I'm not sure if it's worth it (because of ptid_t's relatively small size), but I have made the functions and methods take ptid_t arguments by const reference instead of by value. gdb/ChangeLog: * common/ptid.h (struct ptid): Change to... (class ptid_t): ... this. : New constructors. : New methods. : New static methods. : Rename to... : ...this. : Rename to... : ...this. : Rename to... : ...this. (ptid_build, ptid_get_pid, ptid_get_lwp, ptid_get_tid, ptid_equal, ptid_is_pid, ptid_lwp_p, ptid_tid_p, ptid_match): Take ptid arguments as references, move comment to class ptid_t. * common/ptid.c (null_ptid, minus_one_ptid): Initialize with ptid_t static methods. (ptid_build, pid_to_ptid, ptid_get_pid, ptid_get_tid, ptid_equal, ptid_is_pid, ptid_lwp_p, ptid_tid_p, ptid_match): Take ptid arguments as references, implement using ptid_t methods. * unittests/ptid-selftests.c: New file. * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add unittests/ptid-selftests.c. (SUBDIR_UNITTESTS_OBS): Add unittests/ptid-selftests.o. gdb/gdbserver/ChangeLog: * server.c (handle_v_cont): Initialize thread_resume::thread with null_ptid. --- gdb/common/ptid.c | 61 +++++++------------ gdb/common/ptid.h | 173 +++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 160 insertions(+), 74 deletions(-) (limited to 'gdb/common') diff --git a/gdb/common/ptid.c b/gdb/common/ptid.c index b56971b..81f16d0 100644 --- a/gdb/common/ptid.c +++ b/gdb/common/ptid.c @@ -22,20 +22,15 @@ /* See ptid.h for these. */ -ptid_t null_ptid = { 0, 0, 0 }; -ptid_t minus_one_ptid = { -1, 0, 0 }; +ptid_t null_ptid = ptid_t::make_null (); +ptid_t minus_one_ptid = ptid_t::make_minus_one (); /* See ptid.h. */ ptid_t ptid_build (int pid, long lwp, long tid) { - ptid_t ptid; - - ptid.pid = pid; - ptid.lwp = lwp; - ptid.tid = tid; - return ptid; + return ptid_t (pid, lwp, tid); } /* See ptid.h. */ @@ -43,81 +38,69 @@ ptid_build (int pid, long lwp, long tid) ptid_t pid_to_ptid (int pid) { - return ptid_build (pid, 0, 0); + return ptid_t (pid); } /* See ptid.h. */ int -ptid_get_pid (ptid_t ptid) +ptid_get_pid (const ptid_t &ptid) { - return ptid.pid; + return ptid.pid (); } /* See ptid.h. */ long -ptid_get_lwp (ptid_t ptid) +ptid_get_lwp (const ptid_t &ptid) { - return ptid.lwp; + return ptid.lwp (); } /* See ptid.h. */ long -ptid_get_tid (ptid_t ptid) +ptid_get_tid (const ptid_t &ptid) { - return ptid.tid; + return ptid.tid (); } /* See ptid.h. */ int -ptid_equal (ptid_t ptid1, ptid_t ptid2) +ptid_equal (const ptid_t &ptid1, const ptid_t &ptid2) { - return (ptid1.pid == ptid2.pid - && ptid1.lwp == ptid2.lwp - && ptid1.tid == ptid2.tid); + return ptid1 == ptid2; } /* See ptid.h. */ int -ptid_is_pid (ptid_t ptid) +ptid_is_pid (const ptid_t &ptid) { - if (ptid_equal (minus_one_ptid, ptid) - || ptid_equal (null_ptid, ptid)) - return 0; - - return (ptid_get_lwp (ptid) == 0 && ptid_get_tid (ptid) == 0); + return ptid.is_pid (); } /* See ptid.h. */ int -ptid_lwp_p (ptid_t ptid) +ptid_lwp_p (const ptid_t &ptid) { - return (ptid_get_lwp (ptid) != 0); + return ptid.lwp_p (); } /* See ptid.h. */ int -ptid_tid_p (ptid_t ptid) +ptid_tid_p (const ptid_t &ptid) { - return (ptid_get_tid (ptid) != 0); + return ptid.tid_p (); } +/* See ptid.h. */ + int -ptid_match (ptid_t ptid, ptid_t filter) +ptid_match (const ptid_t &ptid, const ptid_t &filter) { - if (ptid_equal (filter, minus_one_ptid)) - return 1; - if (ptid_is_pid (filter) - && ptid_get_pid (ptid) == ptid_get_pid (filter)) - return 1; - else if (ptid_equal (ptid, filter)) - return 1; - - return 0; + return ptid.matches (filter); } diff --git a/gdb/common/ptid.h b/gdb/common/ptid.h index 337bfb0..70f5199 100644 --- a/gdb/common/ptid.h +++ b/gdb/common/ptid.h @@ -32,65 +32,168 @@ thread_stratum target that might want to sit on top. */ -struct ptid +class ptid_t { +public: + /* Must have a trivial defaulted default constructor so that the + type remains POD. */ + ptid_t () noexcept = default; + + /* Make a ptid given the necessary PID, LWP, and TID components. + + A ptid with only a PID (LWP and TID equal to zero) is usually used to + represent a whole process, including all its lwps/threads. */ + + explicit constexpr ptid_t (int pid, long lwp = 0, long tid = 0) + : m_pid (pid), m_lwp (lwp), m_tid (tid) + {} + + /* Fetch the pid (process id) component from the ptid. */ + + constexpr int pid () const + { return m_pid; } + + /* Return true if the ptid's lwp member is non-zero. */ + + constexpr bool lwp_p () const + { return m_lwp != 0; } + + /* Fetch the lwp (lightweight process) component from the ptid. */ + + constexpr long lwp () const + { return m_lwp; } + + /* Return true if the ptid's tid member is non-zero. */ + + constexpr bool tid_p () const + { return m_tid != 0; } + + /* Fetch the tid (thread id) component from a ptid. */ + + constexpr long tid () const + { return m_tid; } + + /* Return true if the ptid represents a whole process, including all its + lwps/threads. Such ptids have the form of (pid, 0, 0), with + pid != -1. */ + + constexpr bool is_pid () const + { + return (*this != make_null () + && *this != make_minus_one () + && m_lwp == 0 + && m_tid == 0); + } + + /* Compare two ptids to see if they are equal. */ + + constexpr bool operator== (const ptid_t &other) const + { + return (m_pid == other.m_pid + && m_lwp == other.m_lwp + && m_tid == other.m_tid); + } + + /* Compare two ptids to see if they are different. */ + + constexpr bool operator!= (const ptid_t &other) const + { + return !(*this == other); + } + + /* Return true if the ptid matches FILTER. FILTER can be the wild + card MINUS_ONE_PTID (all ptids match it); can be a ptid representing + a process (ptid.is_pid () returns true), in which case, all lwps and + threads of that given process match, lwps and threads of other + processes do not; or, it can represent a specific thread, in which + case, only that thread will match true. The ptid must represent a + specific LWP or THREAD, it can never be a wild card. */ + + constexpr bool matches (const ptid_t &filter) const + { + return (/* If filter represents any ptid, it's always a match. */ + filter == make_minus_one () + /* If filter is only a pid, any ptid with that pid + matches. */ + || (filter.is_pid () && m_pid == filter.pid ()) + + /* Otherwise, this ptid only matches if it's exactly equal + to filter. */ + || *this == filter); + } + + /* Make a null ptid. */ + + static constexpr ptid_t make_null () + { return ptid_t (0, 0, 0); } + + /* Make a minus one ptid. */ + + static constexpr ptid_t make_minus_one () + { return ptid_t (-1, 0, 0); } + +private: /* Process id. */ - int pid; + int m_pid; /* Lightweight process id. */ - long lwp; + long m_lwp; /* Thread id. */ - long tid; + long m_tid; }; -typedef struct ptid ptid_t; - /* The null or zero ptid, often used to indicate no process. */ + extern ptid_t null_ptid; /* The (-1,0,0) ptid, often used to indicate either an error condition or a "don't care" condition, i.e, "run all threads." */ + extern ptid_t minus_one_ptid; -/* Make a ptid given the necessary PID, LWP, and TID components. */ -ptid_t ptid_build (int pid, long lwp, long tid); -/* Make a new ptid from just a pid. This ptid is usually used to - represent a whole process, including all its lwps/threads. */ -ptid_t pid_to_ptid (int pid); +/* The following functions are kept for backwards compatibility. The use of + the ptid_t methods is preferred. */ + +/* See ptid_t::ptid_t. */ + +extern ptid_t ptid_build (int pid, long lwp, long tid); + +/* See ptid_t::ptid_t. */ + +extern ptid_t pid_to_ptid (int pid); + +/* See ptid_t::pid. */ + +extern int ptid_get_pid (const ptid_t &ptid); + +/* See ptid_t::lwp. */ + +extern long ptid_get_lwp (const ptid_t &ptid); + +/* See ptid_t::tid. */ + +extern long ptid_get_tid (const ptid_t &ptid); + +/* See ptid_t::operator== and ptid_t::operator!=. */ -/* Fetch the pid (process id) component from a ptid. */ -int ptid_get_pid (ptid_t ptid); +extern int ptid_equal (const ptid_t &ptid1, const ptid_t &ptid2); -/* Fetch the lwp (lightweight process) component from a ptid. */ -long ptid_get_lwp (ptid_t ptid); +/* See ptid_t::is_pid. */ -/* Fetch the tid (thread id) component from a ptid. */ -long ptid_get_tid (ptid_t ptid); +extern int ptid_is_pid (const ptid_t &ptid); -/* Compare two ptids to see if they are equal. */ -int ptid_equal (ptid_t ptid1, ptid_t ptid2); +/* See ptid_t::lwp_p. */ -/* Returns true if PTID represents a whole process, including all its - lwps/threads. Such ptids have the form of (pid,0,0), with pid != - -1. */ -int ptid_is_pid (ptid_t ptid); +extern int ptid_lwp_p (const ptid_t &ptid); -/* Return true if PTID's lwp member is non-zero. */ -int ptid_lwp_p (ptid_t ptid); +/* See ptid_t::tid_p. */ -/* Return true if PTID's tid member is non-zero. */ -int ptid_tid_p (ptid_t ptid); +extern int ptid_tid_p (const ptid_t &ptid); -/* Returns true if PTID matches filter FILTER. FILTER can be the wild - card MINUS_ONE_PTID (all ptid match it); can be a ptid representing - a process (ptid_is_pid returns true), in which case, all lwps and - threads of that given process match, lwps and threads of other - processes do not; or, it can represent a specific thread, in which - case, only that thread will match true. PTID must represent a - specific LWP or THREAD, it can never be a wild card. */ +/* See ptid_t::matches. */ -extern int ptid_match (ptid_t ptid, ptid_t filter); +extern int ptid_match (const ptid_t &ptid, const ptid_t &filter); #endif -- cgit v1.1