diff options
author | Stephan Rohr <stephan.rohr@intel.com> | 2024-10-16 01:42:27 -0700 |
---|---|---|
committer | Stephan Rohr <stephan.rohr@intel.com> | 2024-10-22 04:41:55 -0700 |
commit | 4689c9b94690b2cd7c3494d31939afea6594ed8b (patch) | |
tree | 1940c760575df04d24e6527f423d5049ecdbfa95 | |
parent | a03c03f84ac331a1ae03fbf1d7e53001935e45be (diff) | |
download | gdb-4689c9b94690b2cd7c3494d31939afea6594ed8b.zip gdb-4689c9b94690b2cd7c3494d31939afea6594ed8b.tar.gz gdb-4689c9b94690b2cd7c3494d31939afea6594ed8b.tar.bz2 |
gdbserver: use 'gdb::function_view' in 'find_*' and 'for_each_*'
Remove the templated versions of 'find_thread', 'for_each_thread' and
'find_thread_in_random' and replace the template function argument with
'gdb::function_view'. The usage of 'gdb::function_view' produces less
cryptic messages on errors and documents well the types of the
parameters taken by the callback and its return type.
Approved-By: Simon Marchi <simon.marchi@efficios.com>
-rw-r--r-- | gdbserver/gdbthread.h | 101 | ||||
-rw-r--r-- | gdbserver/inferiors.cc | 138 | ||||
-rw-r--r-- | gdbserver/inferiors.h | 35 |
3 files changed, 153 insertions, 121 deletions
diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h index c5a5498..adc5857 100644 --- a/gdbserver/gdbthread.h +++ b/gdbserver/gdbthread.h @@ -20,6 +20,7 @@ #define GDBSERVER_GDBTHREAD_H #include "gdbsupport/common-gdbthread.h" +#include "gdbsupport/function-view.h" #include "inferiors.h" #include <list> @@ -103,111 +104,35 @@ struct thread_info *find_any_thread_of_pid (int pid); /* Find the first thread for which FUNC returns true. Return NULL if no thread satisfying FUNC is found. */ -template <typename Func> -static thread_info * -find_thread (Func func) -{ - std::list<thread_info *>::iterator next, cur = all_threads.begin (); - - while (cur != all_threads.end ()) - { - next = cur; - next++; - - if (func (*cur)) - return *cur; - - cur = next; - } - - return NULL; -} +thread_info * +find_thread (gdb::function_view<bool (thread_info *)> func); /* 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); - }); -} +thread_info * +find_thread (int pid, gdb::function_view<bool (thread_info *)> func); /* Find the first thread that matches FILTER for which FUNC returns true. Return NULL if no thread satisfying these conditions is found. */ -template <typename Func> -static thread_info * -find_thread (ptid_t filter, Func func) -{ - return find_thread ([&] (thread_info *thread) { - return thread->id.matches (filter) && func (thread); - }); -} +thread_info * +find_thread (ptid_t filter, gdb::function_view<bool (thread_info *)> func); /* Invoke FUNC for each thread. */ -template <typename Func> -static void -for_each_thread (Func func) -{ - std::list<thread_info *>::iterator next, cur = all_threads.begin (); - - while (cur != all_threads.end ()) - { - next = cur; - next++; - func (*cur); - cur = next; - } -} +void +for_each_thread (gdb::function_view<void (thread_info *)> 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); - }); -} +void +for_each_thread (int pid, gdb::function_view<void (thread_info *)> func); /* Find the a random thread for which FUNC (THREAD) returns true. If no entry is found then return NULL. */ -template <typename Func> -static thread_info * -find_thread_in_random (Func func) -{ - int count = 0; - int random_selector; - - /* First count how many interesting entries we have. */ - for_each_thread ([&] (thread_info *thread) { - if (func (thread)) - count++; - }); - - if (count == 0) - return NULL; - - /* Now randomly pick an entry out of those. */ - random_selector = (int) - ((count * (double) rand ()) / (RAND_MAX + 1.0)); - - thread_info *thread = find_thread ([&] (thread_info *thr_arg) { - return func (thr_arg) && (random_selector-- == 0); - }); - - gdb_assert (thread != NULL); - - return thread; -} +thread_info * +find_thread_in_random (gdb::function_view<bool (thread_info *)> func); /* Get current thread ID (Linux task ID). */ #define current_ptid (current_thread->id) diff --git a/gdbserver/inferiors.cc b/gdbserver/inferiors.cc index d088340..5621db3 100644 --- a/gdbserver/inferiors.cc +++ b/gdbserver/inferiors.cc @@ -215,6 +215,144 @@ current_process (void) return current_process_; } +/* See inferiors.h. */ + +void +for_each_process (gdb::function_view<void (process_info *)> func) +{ + std::list<process_info *>::iterator next, cur = all_processes.begin (); + + while (cur != all_processes.end ()) + { + next = cur; + next++; + func (*cur); + cur = next; + } +} + +/* See inferiors.h. */ + +process_info * +find_process (gdb::function_view<bool (process_info *)> func) +{ + std::list<process_info *>::iterator next, cur = all_processes.begin (); + + while (cur != all_processes.end ()) + { + next = cur; + next++; + + if (func (*cur)) + return *cur; + + cur = next; + } + + return NULL; +} + +/* See gdbthread.h. */ + +thread_info * +find_thread (gdb::function_view<bool (thread_info *)> func) +{ + std::list<thread_info *>::iterator next, cur = all_threads.begin (); + + while (cur != all_threads.end ()) + { + next = cur; + next++; + + if (func (*cur)) + return *cur; + + cur = next; + } + + return NULL; +} + +/* See gdbthread.h. */ + +thread_info * +find_thread (int pid, gdb::function_view<bool (thread_info *)> func) +{ + return find_thread ([&] (thread_info *thread) + { + return thread->id.pid () == pid && func (thread); + }); +} + +/* See gdbthread.h. */ + +thread_info * +find_thread (ptid_t filter, gdb::function_view<bool (thread_info *)> func) +{ + return find_thread ([&] (thread_info *thread) { + return thread->id.matches (filter) && func (thread); + }); +} + +/* See gdbthread.h. */ + +void +for_each_thread (gdb::function_view<void (thread_info *)> func) +{ + std::list<thread_info *>::iterator next, cur = all_threads.begin (); + + while (cur != all_threads.end ()) + { + next = cur; + next++; + func (*cur); + cur = next; + } +} + +/* See gdbthread.h. */ + +void +for_each_thread (int pid, gdb::function_view<void (thread_info *)> func) +{ + for_each_thread ([&] (thread_info *thread) + { + if (pid == thread->id.pid ()) + func (thread); + }); +} + +/* See gdbthread.h. */ + +thread_info * +find_thread_in_random (gdb::function_view<bool (thread_info *)> func) +{ + int count = 0; + int random_selector; + + /* First count how many interesting entries we have. */ + for_each_thread ([&] (thread_info *thread) { + if (func (thread)) + count++; + }); + + if (count == 0) + return NULL; + + /* Now randomly pick an entry out of those. */ + random_selector = (int) + ((count * (double) rand ()) / (RAND_MAX + 1.0)); + + thread_info *thread = find_thread ([&] (thread_info *thr_arg) { + return func (thr_arg) && (random_selector-- == 0); + }); + + gdb_assert (thread != NULL); + + return thread; +} + + /* See gdbsupport/common-gdbthread.h. */ void diff --git a/gdbserver/inferiors.h b/gdbserver/inferiors.h index 00e4233..7d45326 100644 --- a/gdbserver/inferiors.h +++ b/gdbserver/inferiors.h @@ -103,43 +103,12 @@ extern std::list<process_info *> all_processes; /* Invoke FUNC for each process. */ -template <typename Func> -static void -for_each_process (Func func) -{ - std::list<process_info *>::iterator next, cur = all_processes.begin (); - - while (cur != all_processes.end ()) - { - next = cur; - next++; - func (*cur); - cur = next; - } -} +void for_each_process (gdb::function_view<void (process_info *)> func); /* Find the first process for which FUNC returns true. Return NULL if no process satisfying FUNC is found. */ -template <typename Func> -static process_info * -find_process (Func func) -{ - std::list<process_info *>::iterator next, cur = all_processes.begin (); - - while (cur != all_processes.end ()) - { - next = cur; - next++; - - if (func (*cur)) - return *cur; - - cur = next; - } - - return NULL; -} +process_info *find_process (gdb::function_view<bool (process_info *)> func); extern struct thread_info *current_thread; |