[binutils-gdb] gdbserver: add and use `process_info::find_thread(ptid)`

Simon Marchi simark@sourceware.org
Thu Dec 5 17:04:50 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7a0a2f75f003ec1c3cb5d1013070913cef4d1f35

commit 7a0a2f75f003ec1c3cb5d1013070913cef4d1f35
Author: Simon Marchi <simon.marchi@efficios.com>
Date:   Tue Dec 3 11:05:38 2024 -0500

    gdbserver: add and use `process_info::find_thread(ptid)`
    
    Add an overload of `process_info::find_thread` that finds a thread by
    ptid.  Use it in two spots.
    
    Change-Id: I2b7fb819bf4f83f7bd37f8641c38e878119b3814

Diff:
---
 gdbserver/inferiors.cc | 28 ++++++++++++++++------------
 gdbserver/inferiors.h  |  4 ++++
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/gdbserver/inferiors.cc b/gdbserver/inferiors.cc
index b0610d6d07d..7a0209b1673 100644
--- a/gdbserver/inferiors.cc
+++ b/gdbserver/inferiors.cc
@@ -71,13 +71,7 @@ find_thread_ptid (ptid_t ptid)
   if (process == nullptr)
     return nullptr;
 
-  auto &thread_map = process->thread_map ();
-
-  if (auto it = thread_map.find (ptid);
-      it != thread_map.end ())
-    return it->second;
-
-  return nullptr;
+  return process->find_thread (ptid);
 }
 
 /* Find a thread associated with the given PROCESS, or NULL if no
@@ -232,6 +226,18 @@ find_process (gdb::function_view<bool (process_info *)> func)
 
 /* See inferiors.h.  */
 
+thread_info *
+process_info::find_thread (ptid_t ptid)
+{
+  if (auto it = m_ptid_thread_map.find (ptid);
+      it != m_ptid_thread_map.end ())
+    return it->second;
+
+  return nullptr;
+}
+
+/* See inferiors.h.  */
+
 thread_info *
 process_info::find_thread (gdb::function_view<bool (thread_info *)> func)
 {
@@ -282,11 +288,9 @@ find_thread (ptid_t filter, gdb::function_view<bool (thread_info *)> func)
   if (filter.is_pid ())
     return process->find_thread (func);
 
-  auto &thread_map = process->thread_map ();
-
-  if (auto it = thread_map.find (filter);
-      it != thread_map.end () && func (it->second))
-    return it->second;
+  if (thread_info *thread = process->find_thread (filter);
+      thread != nullptr && func (thread))
+    return thread;
 
   return nullptr;
 }
diff --git a/gdbserver/inferiors.h b/gdbserver/inferiors.h
index f6eb3dfa488..5757a1f96e3 100644
--- a/gdbserver/inferiors.h
+++ b/gdbserver/inferiors.h
@@ -97,6 +97,10 @@ struct process_info : public intrusive_list_node<process_info>
   std::unordered_map<ptid_t, thread_info *> &thread_map ()
   { return m_ptid_thread_map; }
 
+  /* Return the thread with ptid PTID, or nullptr if no such thread is
+     found.  */
+  thread_info *find_thread (ptid_t ptid);
+
   /* Find the first thread for which FUNC returns true.  Return nullptr if no
      such thread is found.  */
   thread_info *find_thread (gdb::function_view<bool (thread_info *)> func);


More information about the Gdb-cvs mailing list