From patchwork Fri Oct 18 15:20:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephan Rohr X-Patchwork-Id: 99214 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5D54D3858C53 for ; Fri, 18 Oct 2024 15:23:09 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by sourceware.org (Postfix) with ESMTPS id B8F963858027 for ; Fri, 18 Oct 2024 15:21:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B8F963858027 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B8F963858027 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729264903; cv=none; b=F7rzI4vAvCMbtk1ZfITg+3U6begL6LYtlb/e19Osa6zqDweTFSNMSud9LqVZ0+nA8sBl+9GP0ZUyhomeW1yiY/I+8nfZOPUACbp5/z99HcpoBGxCSVEyovomH3x6zIw6DZPafAeLiqFLxzbeLRddRHypE70trZ93NIGjGpy5APE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729264903; c=relaxed/simple; bh=hnfOjvd/PliT3N/WyCzmTxXSZCjQFM8Ult6Zfp5ttAA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=sm0HQ4Phcq5cogkLRmcXNuhTXYq/6eFFTl0GoV9RluquwXsh2yBgoqDxGHuHAA0ovtx9GKgd8Xdyb+2MPe7ZktFyD4OX7Gt5j2CpiEwIDzqgDorPtb3rCx8nSNWZVUuhbus6n8wfyNsO6ExnRuIxW1RkKC7VfxJjf7uzvvmlMs0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729264900; x=1760800900; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hnfOjvd/PliT3N/WyCzmTxXSZCjQFM8Ult6Zfp5ttAA=; b=kpzU9qLkOWcNeAdASk2yT4LMwDwLls05xotWR6annCOUgcqRrlXPmjG4 TjEF1FGH/uxHqv6zkq8GsNOLhOWBQxsH6FDMrafzcdc4kWi8tLZASXRWD H+hr+TCCbM5dVQcQ/Ll4LgOCloxAonua6cqSMhsFrDT3LyMuMTwPnETwQ gHm7yCrJbKshCs7XiyNMKxyy5d1UDTw4mp/sEI0UP2bQNizC5ctQjxNCF +H02xfB+paFa6kpeevAjW/4gv6Y3Xz2uB3VJ4u/d8XqOYHqapeCdN7NLs S9kpbE0EPVwreyv8OfsYnwpAZu1Gjwni1GluKVbvt7nVR7YS+CGgmL1RR Q==; X-CSE-ConnectionGUID: nfcEiAQWT6u+wpSvWj7oxA== X-CSE-MsgGUID: 3eVBE4roRiqpCGbDKpWNrA== X-IronPort-AV: E=McAfee;i="6700,10204,11229"; a="16415742" X-IronPort-AV: E=Sophos;i="6.11,214,1725346800"; d="scan'208";a="16415742" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Oct 2024 08:21:40 -0700 X-CSE-ConnectionGUID: AdWAsnMyQsedawVeuusRiw== X-CSE-MsgGUID: d9UIOZZ9SLWU05Pl/JOC/A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,214,1725346800"; d="scan'208";a="79039294" Received: from 984fee0031af.jf.intel.com (HELO localhost) ([10.165.54.65]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Oct 2024 08:21:40 -0700 From: Stephan Rohr To: gdb-patches@sourceware.org Cc: simark@simark.ca, aburgess@redhat.com Subject: [PATCH v3 1/3] gdbserver: use 'gdb::function_view' in 'find_*' and 'for_each_*' Date: Fri, 18 Oct 2024 08:20:50 -0700 Message-Id: <20241018152052.1509373-2-stephan.rohr@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241018152052.1509373-1-stephan.rohr@intel.com> References: <20241018152052.1509373-1-stephan.rohr@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org 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'. --- gdbserver/gdbthread.h | 101 ++++-------------------------- gdbserver/inferiors.cc | 138 +++++++++++++++++++++++++++++++++++++++++ gdbserver/inferiors.h | 36 +---------- 3 files changed, 154 insertions(+), 121 deletions(-) diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h index c5a5498088f..adc5857abfd 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 @@ -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 -static thread_info * -find_thread (Func func) -{ - std::list::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 func); /* Like the above, but only consider threads with pid PID. */ -template -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 func); /* Find the first thread that matches FILTER for which FUNC returns true. Return NULL if no thread satisfying these conditions is found. */ -template -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 func); /* Invoke FUNC for each thread. */ -template -static void -for_each_thread (Func func) -{ - std::list::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 func); /* Like the above, but only consider threads with pid PID. */ -template -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 func); /* Find the a random thread for which FUNC (THREAD) returns true. If no entry is found then return NULL. */ -template -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 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 d088340474f..5621db377fb 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 func) +{ + std::list::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 func) +{ + std::list::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 func) +{ + std::list::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 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 func) +{ + return find_thread ([&] (thread_info *thread) { + return thread->id.matches (filter) && func (thread); + }); +} + +/* See gdbthread.h. */ + +void +for_each_thread (gdb::function_view func) +{ + std::list::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 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 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 00e42335014..5f865fa860a 100644 --- a/gdbserver/inferiors.h +++ b/gdbserver/inferiors.h @@ -103,43 +103,13 @@ extern std::list all_processes; /* Invoke FUNC for each process. */ -template -static void -for_each_process (Func func) -{ - std::list::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 func); /* Find the first process for which FUNC returns true. Return NULL if no process satisfying FUNC is found. */ -template -static process_info * -find_process (Func func) -{ - std::list::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 func); extern struct thread_info *current_thread;