public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Add overloads of for_each_thread/find_thread that filter on pid
@ 2017-10-14 13:22 Simon Marchi
  2017-10-21 16:21 ` Simon Marchi
  0 siblings, 1 reply; 2+ messages in thread
From: Simon Marchi @ 2017-10-14 13:22 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

It happens often that we want to iterate or find threads restricted to a
given pid.  I think it's worth having an overload to help with this.
Right now there is a single user of each of the find_thread and
for_each_thread overload, but as we replace the usages of find_inferior
with for_each_thread/find_thread, more usages will pop up.

Here's an example of a future change that could use it:
https://github.com/simark/binutils-gdb/commit/9ae5b90d8e6a6836f26ef6685e1f343112e1182a

gdb/gdbserver/ChangeLog:

	* gdbthread.h (find_thread, for_each_thread): New functions.
	* inferiors.c (thread_of_pid): Remove.
	(find_any_thread_of_pid): Use find_thread.
	* linux-low.c (num_lwps): Use for_each_thread.
---
 gdb/gdbserver/gdbthread.h | 25 +++++++++++++++++++++++++
 gdb/gdbserver/inferiors.c | 15 +++------------
 gdb/gdbserver/linux-low.c |  6 +++---
 3 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/gdb/gdbserver/gdbthread.h b/gdb/gdbserver/gdbthread.h
index 8ace051..b82d5b0 100644
--- a/gdb/gdbserver/gdbthread.h
+++ b/gdb/gdbserver/gdbthread.h
@@ -111,6 +111,18 @@ find_thread (Func func)
   return NULL;
 }
 
+/* Like the above, but only consider threads with pid PID.  */
+
+template <typename Func>
+static thread_info *
+find_thread (int pid, Func func)
+{
+  return find_thread ([&] (thread_info *thread)
+    {
+      return thread->id.pid () == pid && func (thread);
+    });
+}
+
 /* Invoke FUNC for each thread.  */
 
 template <typename Func>
@@ -128,6 +140,19 @@ for_each_thread (Func func)
     }
 }
 
+/* Like the above, but only consider threads with pid PID.  */
+
+template <typename Func>
+static void
+for_each_thread (int pid, Func func)
+{
+  for_each_thread ([&] (thread_info *thread)
+    {
+      if (pid == thread->id.pid ())
+	func (thread);
+    });
+}
+
 /* Find the a random thread for which FUNC (THREAD) returns true.  If
    no entry is found then return NULL.  */
 
diff --git a/gdb/gdbserver/inferiors.c b/gdb/gdbserver/inferiors.c
index 564121f..a0ece4d 100644
--- a/gdb/gdbserver/inferiors.c
+++ b/gdb/gdbserver/inferiors.c
@@ -132,23 +132,14 @@ find_thread_process (const struct process_info *const process)
   return find_any_thread_of_pid (process->pid);
 }
 
-/* Helper for find_any_thread_of_pid.  Returns true if a thread
-   matches a PID.  */
-
-static int
-thread_of_pid (thread_info *entry, void *pid_p)
-{
-  int pid = *(int *) pid_p;
-
-  return (ptid_get_pid (entry->id) == pid);
-}
-
 /* See gdbthread.h.  */
 
 struct thread_info *
 find_any_thread_of_pid (int pid)
 {
-  return find_inferior (&all_threads, thread_of_pid, &pid);
+  return find_thread (pid, [] (thread_info *thread) {
+    return true;
+  });
 }
 
 static void
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index 0b93fa2..b367e53 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -1869,10 +1869,10 @@ num_lwps (int pid)
 {
   int count = 0;
 
-  for_each_thread ([&] (thread_info *thread) {
-    if (thread->id.pid () == pid)
+  for_each_thread (pid, [&] (thread_info *thread)
+    {
       count++;
-  });
+    });
 
   return count;
 }
-- 
2.7.4

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Add overloads of for_each_thread/find_thread that filter on pid
  2017-10-14 13:22 [PATCH] Add overloads of for_each_thread/find_thread that filter on pid Simon Marchi
@ 2017-10-21 16:21 ` Simon Marchi
  0 siblings, 0 replies; 2+ messages in thread
From: Simon Marchi @ 2017-10-21 16:21 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 2017-10-14 09:21 AM, Simon Marchi wrote:
> It happens often that we want to iterate or find threads restricted to a
> given pid.  I think it's worth having an overload to help with this.
> Right now there is a single user of each of the find_thread and
> for_each_thread overload, but as we replace the usages of find_inferior
> with for_each_thread/find_thread, more usages will pop up.
> 
> Here's an example of a future change that could use it:
> https://github.com/simark/binutils-gdb/commit/9ae5b90d8e6a6836f26ef6685e1f343112e1182a
> 
> gdb/gdbserver/ChangeLog:
> 
> 	* gdbthread.h (find_thread, for_each_thread): New functions.
> 	* inferiors.c (thread_of_pid): Remove.
> 	(find_any_thread_of_pid): Use find_thread.
> 	* linux-low.c (num_lwps): Use for_each_thread.

I pushed this patch in.

Simon

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-10-21 16:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-14 13:22 [PATCH] Add overloads of for_each_thread/find_thread that filter on pid Simon Marchi
2017-10-21 16:21 ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).