From patchwork Tue Oct 22 15:02:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rohr, Stephan" X-Patchwork-Id: 99330 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 A71F43858433 for ; Tue, 22 Oct 2024 15:03:07 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by sourceware.org (Postfix) with ESMTPS id DC13F3858CDB for ; Tue, 22 Oct 2024 15:02:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DC13F3858CDB 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 DC13F3858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729609354; cv=none; b=o2wzFkUd3GngDhuH7ubpLDnTznCWPVEyk6a4yEG74+cCyciB2vrLu72zBrH8B+WWkLwaDiwhHq4RKPNHqGZW8xI4nqQqds055qL9l5xChCD9NYnXYTQa24Egd9/Ke9R4YXlTfiDBZ7DnfSshpPHasaHGkxsev9YCcuXjAsM9TQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729609354; c=relaxed/simple; bh=vujdH3eJboRmqNrhvTnq/tUv86agmu4ZXllwMbbf730=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ry4mLceh54qm30ECPhoe+uJ53fBu1ciAlhbCLJMWP5luLLW7BDqWl1TowKgQ14ySBfuRukgg8uatsg27BYN5Lrifk9I9YCgi/IJhq0ZYJsNsTtkLQk42HUngv21Hk83YeyZzm7hNMwqT5+jYxu38043BKVlzVfgFXJUmJ892bA0= 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=1729609351; x=1761145351; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vujdH3eJboRmqNrhvTnq/tUv86agmu4ZXllwMbbf730=; b=LuDvWY7wbbsDNFDB98MZqafh6ib3ajOMkMSjwXzj7GMPD5fgQmT0NDRv Mln5t6aTfCd9ZREFrh7QRNGjLfcUXEy345ZmiOSrooAe0kvLgIzdmf2Nr haRi0tvlxmhncL50PqHggq1AilR5sMcOHDjJMQ+FdVhfaoFUE3ASZMuAU DAmKgZibyZdpgQoSUg8PhvVkYkwYFpXDoH3Z/rL2qEGnGp1pXs6yVaDga tPpvEe76CHqHbTuNvZ/9+nWjiuhgGu07j04vv3D9XKAXmRDyrngUefH+x GDr64ogqkPFjyrsoYRna7fP/81msO+AbirxvFNR6TY43Qc+G1Ucna5DFi A==; X-CSE-ConnectionGUID: L9AKQK/LSaioR6+x78JgDw== X-CSE-MsgGUID: d25ZuP58SjWp2NjdSL7wwQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="46615029" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="46615029" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Oct 2024 08:02:30 -0700 X-CSE-ConnectionGUID: 1Ef71xxDR3uT2aiQeA37Nw== X-CSE-MsgGUID: QVZ9hHcFRhiqSJBOqAF0AA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,223,1725346800"; d="scan'208";a="84510166" Received: from 984fee0031af.jf.intel.com (HELO localhost) ([10.165.54.65]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Oct 2024 08:02:30 -0700 From: Stephan Rohr To: gdb-patches@sourceware.org Cc: simark@simark.ca, aburgess@redhat.com Subject: [PATCH v4 1/2] gdbserver: introduce 'owning_introdusive_list' type Date: Tue, 22 Oct 2024 08:02:20 -0700 Message-Id: <20241022150221.2125847-2-stephan.rohr@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241022150221.2125847-1-stephan.rohr@intel.com> References: <20241022150221.2125847-1-stephan.rohr@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 This patch replaces the 'std::list' type of 'all_processes' and 'all_threads' with the more lightweight 'owning_intrusive_list' type. --- gdbserver/gdbthread.h | 8 ++--- gdbserver/inferiors.cc | 75 +++++++++++++----------------------------- gdbserver/inferiors.h | 7 ++-- gdbserver/server.cc | 16 ++++----- 4 files changed, 37 insertions(+), 69 deletions(-) diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h index adc5857abfd..1c380d5ff22 100644 --- a/gdbserver/gdbthread.h +++ b/gdbserver/gdbthread.h @@ -21,14 +21,12 @@ #include "gdbsupport/common-gdbthread.h" #include "gdbsupport/function-view.h" -#include "inferiors.h" - -#include +#include "gdbsupport/owning_intrusive_list.h" struct btrace_target_info; struct regcache; -struct thread_info +struct thread_info : public intrusive_list_node { thread_info (ptid_t id, void *target_data) : id (id), target_data (target_data) @@ -86,7 +84,7 @@ struct thread_info gdb_thread_options thread_options = 0; }; -extern std::list all_threads; +extern owning_intrusive_list all_threads; void remove_thread (struct thread_info *thread); struct thread_info *add_thread (ptid_t ptid, void *target_data); diff --git a/gdbserver/inferiors.cc b/gdbserver/inferiors.cc index 5621db377fb..1d39a8d1a86 100644 --- a/gdbserver/inferiors.cc +++ b/gdbserver/inferiors.cc @@ -19,11 +19,12 @@ along with this program. If not, see . */ #include "gdbsupport/common-inferior.h" +#include "gdbsupport/owning_intrusive_list.h" #include "gdbthread.h" #include "dll.h" -std::list all_processes; -std::list all_threads; +owning_intrusive_list all_processes; +owning_intrusive_list all_threads; /* The current process. */ static process_info *current_process_; @@ -40,14 +41,12 @@ static std::string current_inferior_cwd; struct thread_info * add_thread (ptid_t thread_id, void *target_data) { - thread_info *new_thread = new thread_info (thread_id, target_data); - - all_threads.push_back (new_thread); + auto &new_thread = all_threads.emplace_back (thread_id, target_data); if (current_thread == NULL) - switch_to_thread (new_thread); + switch_to_thread (&new_thread); - return new_thread; + return &new_thread; } /* See gdbthread.h. */ @@ -56,7 +55,7 @@ struct thread_info * get_first_thread (void) { if (!all_threads.empty ()) - return all_threads.front (); + return &(all_threads.front ()); else return NULL; } @@ -88,12 +87,6 @@ find_any_thread_of_pid (int pid) }); } -static void -free_one_thread (thread_info *thread) -{ - delete thread; -} - void remove_thread (struct thread_info *thread) { @@ -101,10 +94,9 @@ remove_thread (struct thread_info *thread) target_disable_btrace (thread->btrace); discard_queued_stop_replies (ptid_of (thread)); - all_threads.remove (thread); + all_threads.erase (all_threads.iterator_to (*thread)); if (current_thread == thread) switch_to_thread (nullptr); - free_one_thread (thread); } void * @@ -128,7 +120,6 @@ set_thread_regcache_data (struct thread_info *thread, struct regcache *data) void clear_inferiors (void) { - for_each_thread (free_one_thread); all_threads.clear (); clear_dlls (); @@ -140,11 +131,8 @@ clear_inferiors (void) struct process_info * add_process (int pid, int attached) { - process_info *process = new process_info (pid, attached); - - all_processes.push_back (process); - - return process; + auto &process = all_processes.emplace_back (pid, attached); + return &process; } /* Remove a process from the common process list and free the memory @@ -157,10 +145,9 @@ remove_process (struct process_info *process) clear_symbol_cache (&process->symbol_cache); free_all_breakpoints (process); gdb_assert (find_thread_process (process) == NULL); - all_processes.remove (process); + all_processes.erase (all_processes.iterator_to (*process)); if (current_process () == process) switch_to_process (nullptr); - delete process; } process_info * @@ -177,7 +164,7 @@ process_info * get_first_process (void) { if (!all_processes.empty ()) - return all_processes.front (); + return &(all_processes.front ()); else return NULL; } @@ -220,13 +207,13 @@ current_process (void) void for_each_process (gdb::function_view func) { - std::list::iterator next, cur = all_processes.begin (); + owning_intrusive_list::iterator next, cur = all_processes.begin (); while (cur != all_processes.end ()) { next = cur; next++; - func (*cur); + func (&*cur); cur = next; } } @@ -236,18 +223,9 @@ for_each_process (gdb::function_view func) 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; - } + for (process_info &process : all_processes) + if (func (&process)) + return &process; return NULL; } @@ -257,18 +235,9 @@ find_process (gdb::function_view func) 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; - } + for (thread_info &thread : all_threads) + if (func (&thread)) + return &thread; return NULL; } @@ -299,13 +268,13 @@ find_thread (ptid_t filter, gdb::function_view func) void for_each_thread (gdb::function_view func) { - std::list::iterator next, cur = all_threads.begin (); + owning_intrusive_list::iterator next, cur = all_threads.begin (); while (cur != all_threads.end ()) { next = cur; next++; - func (*cur); + func (&*cur); cur = next; } } diff --git a/gdbserver/inferiors.h b/gdbserver/inferiors.h index 7d453260270..3f2c5c70a39 100644 --- a/gdbserver/inferiors.h +++ b/gdbserver/inferiors.h @@ -20,8 +20,9 @@ #define GDBSERVER_INFERIORS_H #include "gdbsupport/gdb_vecs.h" +#include "gdbsupport/owning_intrusive_list.h" + #include "dll.h" -#include struct thread_info; struct regcache; @@ -32,7 +33,7 @@ struct raw_breakpoint; struct fast_tracepoint_jump; struct process_info_private; -struct process_info +struct process_info : public intrusive_list_node { process_info (int pid_, int attached_) : pid (pid_), attached (attached_) @@ -99,7 +100,7 @@ pid_of (const process_info *proc) struct process_info *current_process (void); struct process_info *get_thread_process (const struct thread_info *); -extern std::list all_processes; +extern owning_intrusive_list all_processes; /* Invoke FUNC for each process. */ diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 5190df4aed5..5058428f58b 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -1355,15 +1355,15 @@ handle_detach (char *own_buf) another process might delete the next thread in the iteration, which is the one saved by the safe iterator. We will never delete the currently iterated on thread, so standard iteration should be safe. */ - for (thread_info *thread : all_threads) + for (thread_info &thread : all_threads) { /* Only threads that are of the process we are detaching. */ - if (thread->id.pid () != pid) + if (thread.id.pid () != pid) continue; /* Only threads that have a pending fork event. */ target_waitkind kind; - thread_info *child = target_thread_pending_child (thread, &kind); + thread_info *child = target_thread_pending_child (&thread, &kind); if (child == nullptr || kind == TARGET_WAITKIND_THREAD_CLONED) continue; @@ -1375,7 +1375,7 @@ handle_detach (char *own_buf) if (detach_inferior (fork_child_process) != 0) warning (_("Failed to detach fork child %s, child of %s"), target_pid_to_str (ptid_t (fork_child_pid)).c_str (), - target_pid_to_str (thread->id).c_str ()); + target_pid_to_str (thread.id).c_str ()); } if (detach_inferior (process) != 0) @@ -2523,7 +2523,7 @@ static void handle_query (char *own_buf, int packet_len, int *new_packet_len_p) { client_state &cs = get_client_state (); - static std::list::const_iterator thread_iter; + static owning_intrusive_list::iterator thread_iter; /* Reply the current thread id. */ if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC) @@ -2536,7 +2536,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) else { thread_iter = all_threads.begin (); - ptid = (*thread_iter)->id; + ptid = thread_iter->id; } sprintf (own_buf, "QC"); @@ -2599,7 +2599,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) thread_iter = all_threads.begin (); *own_buf++ = 'm'; - ptid_t ptid = (*thread_iter)->id; + ptid_t ptid = thread_iter->id; write_ptid (own_buf, ptid); thread_iter++; return; @@ -2611,7 +2611,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) if (thread_iter != all_threads.end ()) { *own_buf++ = 'm'; - ptid_t ptid = (*thread_iter)->id; + ptid_t ptid = thread_iter->id; write_ptid (own_buf, ptid); thread_iter++; return;