From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id wBXDILYW0mAtYAAAWB0awg (envelope-from ) for ; Tue, 22 Jun 2021 12:58:30 -0400 Received: by simark.ca (Postfix, from userid 112) id 837C21E54D; Tue, 22 Jun 2021 12:58:30 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 593701E54D for ; Tue, 22 Jun 2021 12:58:28 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 19F2E3838017 for ; Tue, 22 Jun 2021 16:58:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 19F2E3838017 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1624381108; bh=pkgVuRjoGOcnrNmC8HAtMjKnBMJDuvz4WQNJtgZ50qE=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=bXJoxV0JAaj7YWXWwW7CtZg8N/JTqSVt7vsnjXSVxwIujV1zzXXJlkeXOumQfJx8v dhVUrG1ytDIoBSAYFklV+NA2Ij//SfBReoJJ5B/pWGt2atTRsdnOomLTTNPhXMVIQw 6aj/U2HflcI3clxkfMX8oO4gJ9X0W0ijtghHZwmg= Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id AF6FD3857416 for ; Tue, 22 Jun 2021 16:57:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AF6FD3857416 X-ASG-Debug-ID: 1624381026-0c856e67e2169e540001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id nr6JLO8t5Og7gXb2 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 22 Jun 2021 12:57:06 -0400 (EDT) X-Barracuda-Envelope-From: simon.marchi@polymtl.ca X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from simark.localdomain (192-222-157-6.qc.cable.ebox.net [192.222.157.6]) by smtp.ebox.ca (Postfix) with ESMTP id 57B1C441D66; Tue, 22 Jun 2021 12:57:06 -0400 (EDT) X-Barracuda-RBL-IP: 192.222.157.6 X-Barracuda-Effective-Source-IP: 192-222-157-6.qc.cable.ebox.net[192.222.157.6] X-Barracuda-Apparent-Source-IP: 192.222.157.6 To: gdb-patches@sourceware.org Subject: [PATCH 03/11] gdb: make inferior_list use intrusive_list Date: Tue, 22 Jun 2021 12:56:56 -0400 X-ASG-Orig-Subj: [PATCH 03/11] gdb: make inferior_list use intrusive_list Message-Id: <20210622165704.2404007-4-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210622165704.2404007-1-simon.marchi@polymtl.ca> References: <20210622165704.2404007-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1624381026 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Barracuda-BRTS-Status: 1 X-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-Scan-Msg-Size: 18043 X-Barracuda-Spam-Score: 0.50 X-Barracuda-Spam-Status: No, SCORE=0.50 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests=BSF_RULE7568M X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.90826 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_RULE7568M Custom Rule 7568M X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Simon Marchi via Gdb-patches Reply-To: Simon Marchi Cc: Simon Marchi Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" From: Pedro Alves Change inferior_list, the global list of inferiors, to use intrusive_list. I think most other changes are somewhat obvious fallouts from this change. There is a small change in behavior in scoped_mock_context. Before this patch, constructing a scoped_mock_context would replace the whole inferior list with only the new mock inferior. Tests using two scoped_mock_contexts therefore needed to manually link the two inferiors together, as the second scoped_mock_context would bump the first mock inferior from the thread list. With this patch, a scoped_mock_context adds its mock inferior to the inferior list on construction, and removes it on destruction. This means that tests run with mock inferiors in the inferior list in addition to any pre-existing inferiors (there is always at least one). There is no possible pid clash problem, since each scoped mock inferior uses its own process target, and pids are per process target. YYYY-MM-DD Pedro Alves YYYY-MM-DD Simon Marchi gdb/ChangeLog: * inferior.h (class inferior): Inherit from intrusive_list_node. : Remove. (inferior_list): Change type to intrusive_list. * inferior.c (inferior_list): Change type to intrusive_list. (add_inferior_silent): Adjust. (delete_inferior): Adjust. (exit_inferior_1): Adjust. (prune_inferiors): Adjust. * inferior-iter.h (class all_inferiors_iterator) : Accept intrusive_list parameter. : Remove. : New. : Adjust. * ada-tasks.c (ada_tasks_new_objfile_observer): Adjust. * infrun.c (do_target_wait): Adjust. * progspace.c (update_address_spaces): Adjust. * regcache.c (regcache_thread_ptid_changed): Don't set next. * scoped-mock-context.h (struct scoped_mock_context): Adjust. * thread-iter.c (all_threads_iterator::all_threads_iterator): Adjust. (all_threads_iterator::advance): Adjust. (all_matching_threads_iterator::all_matching_threads_iterator): Adjust. (all_matching_threads_iterator::advance): Adjust. * thread.c (show_inferior_qualified_tids): Adjust. Co-Authored-By: Simon Marchi Change-Id: I7eb6a8f867d4dcf8b8cd2dcffd118f7270756018 --- gdb/ada-tasks.c | 4 +-- gdb/inferior-iter.h | 25 ++++++++------ gdb/inferior.c | 63 +++++++----------------------------- gdb/inferior.h | 9 +++--- gdb/infrun.c | 30 ++++++++++------- gdb/progspace.c | 3 +- gdb/regcache.c | 1 - gdb/scoped-mock-context.h | 10 +++--- gdb/thread-iter.c | 68 +++++++++++++++++++++++---------------- gdb/thread.c | 6 +++- 10 files changed, 103 insertions(+), 116 deletions(-) diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c index a9c6b5eb1b3a..80a72216f96e 100644 --- a/gdb/ada-tasks.c +++ b/gdb/ada-tasks.c @@ -1444,8 +1444,6 @@ ada_tasks_normal_stop_observer (struct bpstats *unused_args, int unused_args2) static void ada_tasks_new_objfile_observer (struct objfile *objfile) { - struct inferior *inf; - /* Invalidate the relevant data in our program-space data. */ if (objfile == NULL) @@ -1468,7 +1466,7 @@ ada_tasks_new_objfile_observer (struct objfile *objfile) If all objfiles are being cleared (OBJFILE is NULL), then clear the caches for all inferiors. */ - for (inf = inferior_list; inf != NULL; inf = inf->next) + for (inferior *inf : all_inferiors ()) if (objfile == NULL || inf->pspace == objfile->pspace) ada_tasks_invalidate_inferior_data (inf); } diff --git a/gdb/inferior-iter.h b/gdb/inferior-iter.h index f999150a7b55..1701465eaf83 100644 --- a/gdb/inferior-iter.h +++ b/gdb/inferior-iter.h @@ -36,18 +36,21 @@ class all_inferiors_iterator typedef int difference_type; /* Create an iterator pointing at HEAD. */ - all_inferiors_iterator (process_stratum_target *proc_target, inferior *head) - : m_proc_target (proc_target) + all_inferiors_iterator (process_stratum_target *proc_target, + const intrusive_list &list) + : m_proc_target (proc_target), m_inf_iter (list.begin ()) { + intrusive_list::iterator end; + /* Advance M_INF to the first inferior's position. */ - for (m_inf = head; m_inf != NULL; m_inf = m_inf->next) + for (; m_inf_iter != end; ++m_inf_iter) if (m_inf_matches ()) return; } /* Create a one-past-end iterator. */ all_inferiors_iterator () - : m_proc_target (nullptr), m_inf (nullptr) + : m_proc_target (nullptr) {} all_inferiors_iterator &operator++ () @@ -57,37 +60,39 @@ class all_inferiors_iterator } inferior *operator* () const - { return m_inf; } + { return &*m_inf_iter; } bool operator!= (const all_inferiors_iterator &other) const - { return m_inf != other.m_inf; } + { return m_inf_iter != other.m_inf_iter; } private: /* Advance to next inferior, skipping filtered inferiors. */ void advance () { + intrusive_list::iterator end; + /* The loop below is written in the natural way as-if we'd always start at the beginning of the inferior list. This fast-forwards the algorithm to the actual current position. */ goto start; - while (m_inf != NULL) + while (m_inf_iter != end) { if (m_inf_matches ()) return; start: - m_inf = m_inf->next; + ++m_inf_iter; } } bool m_inf_matches () { return (m_proc_target == nullptr - || m_proc_target == m_inf->process_target ()); + || m_proc_target == m_inf_iter->process_target ()); } process_stratum_target *m_proc_target; - inferior *m_inf; + intrusive_list::iterator m_inf_iter; }; /* A range adapter that makes it possible to iterate over all diff --git a/gdb/inferior.c b/gdb/inferior.c index 693b196556d5..f1b0bdde554b 100644 --- a/gdb/inferior.c +++ b/gdb/inferior.c @@ -41,7 +41,7 @@ DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD) -struct inferior *inferior_list = NULL; +intrusive_list inferior_list; static int highest_inferior_num; /* See inferior.h. */ @@ -126,16 +126,7 @@ add_inferior_silent (int pid) { inferior *inf = new inferior (pid); - if (inferior_list == NULL) - inferior_list = inf; - else - { - inferior *last; - - for (last = inferior_list; last->next != NULL; last = last->next) - ; - last->next = inf; - } + inferior_list.push_back (*inf); gdb::observers::inferior_added.notify (inf); @@ -177,25 +168,12 @@ inferior::clear_thread_list (bool silent) } void -delete_inferior (struct inferior *todel) +delete_inferior (struct inferior *inf) { - struct inferior *inf, *infprev; - - infprev = NULL; - - for (inf = inferior_list; inf; infprev = inf, inf = inf->next) - if (inf == todel) - break; - - if (!inf) - return; - inf->clear_thread_list (true); - if (infprev) - infprev->next = inf->next; - else - inferior_list = inf->next; + auto it = inferior_list.iterator_to (*inf); + inferior_list.erase (it); gdb::observers::inferior_removed.notify (inf); @@ -210,17 +188,8 @@ delete_inferior (struct inferior *todel) exit of its threads. */ static void -exit_inferior_1 (struct inferior *inftoex, int silent) +exit_inferior_1 (struct inferior *inf, int silent) { - struct inferior *inf; - - for (inf = inferior_list; inf; inf = inf->next) - if (inf == inftoex) - break; - - if (!inf) - return; - inf->clear_thread_list (silent); gdb::observers::inferior_exit.notify (inf); @@ -388,22 +357,14 @@ have_live_inferiors (void) void prune_inferiors (void) { - inferior *ss; - - ss = inferior_list; - while (ss) + for (inferior *inf : all_inferiors_safe ()) { - if (!ss->deletable () - || !ss->removable - || ss->pid != 0) - { - ss = ss->next; - continue; - } + if (!inf->deletable () + || !inf->removable + || inf->pid != 0) + continue; - inferior *ss_next = ss->next; - delete_inferior (ss); - ss = ss_next; + delete_inferior (inf); } } diff --git a/gdb/inferior.h b/gdb/inferior.h index 2ae9f9a5f9c4..830dec3ebbaa 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -55,6 +55,7 @@ struct thread_info; #include "gdbsupport/refcounted-object.h" #include "gdbsupport/forward-scope-exit.h" #include "gdbsupport/gdb_unique_ptr.h" +#include "gdbsupport/intrusive_list.h" #include "gdbsupport/common-inferior.h" #include "gdbthread.h" @@ -339,7 +340,8 @@ extern void switch_to_inferior_no_thread (inferior *inf); listed exactly once in the inferior list, so placing an inferior in the inferior list is an implicit, not counted strong reference. */ -class inferior : public refcounted_object +class inferior : public refcounted_object, + public intrusive_list_node { public: explicit inferior (int pid); @@ -387,9 +389,6 @@ class inferior : public refcounted_object bool has_execution () { return target_has_execution (this); } - /* Pointer to next inferior in singly-linked list of inferiors. */ - struct inferior *next = NULL; - /* This inferior's thread list, sorted by creation order. */ intrusive_list thread_list; @@ -653,7 +652,7 @@ class scoped_restore_current_inferior /* Traverse all inferiors. */ -extern struct inferior *inferior_list; +extern intrusive_list inferior_list; /* Pull in the internals of the inferiors ranges and iterators. Must be done after struct inferior is defined. */ diff --git a/gdb/infrun.c b/gdb/infrun.c index 4bd21fde5907..3f7e80216b82 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -3733,18 +3733,28 @@ do_target_wait (ptid_t wait_ptid, execution_control_state *ecs, reported the stop to the user, polling for events. */ scoped_restore_current_thread restore_thread; - int inf_num = selected->num; - for (inferior *inf = selected; inf != NULL; inf = inf->next) - if (inferior_matches (inf)) - if (do_wait (inf)) + intrusive_list_iterator start + = inferior_list.iterator_to (*selected); + + for (intrusive_list_iterator it = start; + it != inferior_list.end (); + ++it) + { + inferior *inf = &*it; + + if (inferior_matches (inf) && do_wait (inf)) return true; + } - for (inferior *inf = inferior_list; - inf != NULL && inf->num < inf_num; - inf = inf->next) - if (inferior_matches (inf)) - if (do_wait (inf)) + for (intrusive_list_iterator it = inferior_list.begin (); + it != start; + ++it) + { + inferior *inf = &*it; + + if (inferior_matches (inf) && do_wait (inf)) return true; + } ecs->ws.kind = TARGET_WAITKIND_IGNORE; return false; @@ -9455,7 +9465,6 @@ infrun_thread_ptid_changed () scoped_mock_context target1 (arch); scoped_mock_context target2 (arch); - target2.mock_inferior.next = &target1.mock_inferior; ptid_t old_ptid (111, 222); ptid_t new_ptid (111, 333); @@ -9480,7 +9489,6 @@ infrun_thread_ptid_changed () scoped_mock_context target1 (arch); scoped_mock_context target2 (arch); - target2.mock_inferior.next = &target1.mock_inferior; ptid_t old_ptid (111, 222); ptid_t new_ptid (111, 333); diff --git a/gdb/progspace.c b/gdb/progspace.c index e3cc6929a236..7080bf8ee270 100644 --- a/gdb/progspace.c +++ b/gdb/progspace.c @@ -404,7 +404,6 @@ void update_address_spaces (void) { int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ()); - struct inferior *inf; init_address_spaces (); @@ -423,7 +422,7 @@ update_address_spaces (void) pspace->aspace = new_address_space (); } - for (inf = inferior_list; inf; inf = inf->next) + for (inferior *inf : all_inferiors ()) if (gdbarch_has_global_solist (target_gdbarch ())) inf->aspace = maybe_new_address_space (); else diff --git a/gdb/regcache.c b/gdb/regcache.c index fde0c612975a..21fa25d31553 100644 --- a/gdb/regcache.c +++ b/gdb/regcache.c @@ -2038,7 +2038,6 @@ regcache_thread_ptid_changed () /* Prepare two targets with one thread each, with the same ptid. */ scoped_mock_context target1 (arch); scoped_mock_context target2 (arch); - target2.mock_inferior.next = &target1.mock_inferior; ptid_t old_ptid (111, 222); ptid_t new_ptid (111, 333); diff --git a/gdb/scoped-mock-context.h b/gdb/scoped-mock-context.h index 37ffe5117423..ba3b81ed12a5 100644 --- a/gdb/scoped-mock-context.h +++ b/gdb/scoped-mock-context.h @@ -44,13 +44,12 @@ struct scoped_mock_context scoped_restore_current_pspace_and_thread restore_pspace_thread; - /* Add the mock inferior to the inferior list so that look ups by - target+ptid can find it. */ - scoped_restore_tmpl restore_inferior_list - {&inferior_list, &mock_inferior}; - explicit scoped_mock_context (gdbarch *gdbarch) { + /* Add the mock inferior to the inferior list so that look ups by + target+ptid can find it. */ + inferior_list.push_back (mock_inferior); + mock_inferior.thread_list.push_back (mock_thread); mock_inferior.gdbarch = gdbarch; mock_inferior.aspace = mock_pspace.aspace; @@ -70,6 +69,7 @@ struct scoped_mock_context ~scoped_mock_context () { + inferior_list.erase (inferior_list.iterator_to (mock_inferior)); pop_all_targets_at_and_above (process_stratum); } }; diff --git a/gdb/thread-iter.c b/gdb/thread-iter.c index a1cdd0206bd4..31b7a36eaada 100644 --- a/gdb/thread-iter.c +++ b/gdb/thread-iter.c @@ -26,15 +26,18 @@ all_threads_iterator::all_threads_iterator (begin_t) { /* Advance M_INF/M_THR to the first thread's position. */ - for (m_inf = inferior_list; m_inf != NULL; m_inf = m_inf->next) + + for (inferior &inf : inferior_list) { - auto thr_iter = m_inf->thread_list.begin (); - if (thr_iter != m_inf->thread_list.end ()) + auto thr_iter = inf.thread_list.begin (); + if (thr_iter != inf.thread_list.end ()) { + m_inf = &inf; m_thr = &*thr_iter; return; } } + m_inf = nullptr; m_thr = nullptr; } @@ -43,6 +46,7 @@ all_threads_iterator::all_threads_iterator (begin_t) void all_threads_iterator::advance () { + intrusive_list::iterator inf_iter (m_inf); intrusive_list::iterator thr_iter (m_thr); /* The loop below is written in the natural way as-if we'd always @@ -50,8 +54,9 @@ all_threads_iterator::advance () the algorithm to the actual current position. */ goto start; - for (; m_inf != NULL; m_inf = m_inf->next) + for (; inf_iter != inferior_list.end (); ++inf_iter) { + m_inf = &*inf_iter; thr_iter = m_inf->thread_list.begin (); while (thr_iter != m_inf->thread_list.end ()) { @@ -86,16 +91,21 @@ all_matching_threads_iterator::all_matching_threads_iterator gdb_assert ((filter_target == nullptr && filter_ptid == minus_one_ptid) || filter_target->stratum () == process_stratum); - for (m_inf = inferior_list; m_inf != NULL; m_inf = m_inf->next) - if (m_inf_matches ()) - for (auto thr_iter = m_inf->thread_list.begin (); - thr_iter != m_inf->thread_list.end (); - ++thr_iter) - if (thr_iter->ptid.matches (m_filter_ptid)) + for (inferior &inf : inferior_list) + { + m_inf = &inf; + if (m_inf_matches ()) + for (auto thr_iter = m_inf->thread_list.begin (); + thr_iter != m_inf->thread_list.end (); + ++thr_iter) { - m_thr = &*thr_iter; - return; + if (thr_iter->ptid.matches (m_filter_ptid)) + { + m_thr = &*thr_iter; + return; + } } + } m_thr = nullptr; } @@ -105,6 +115,7 @@ all_matching_threads_iterator::all_matching_threads_iterator void all_matching_threads_iterator::advance () { + intrusive_list::iterator inf_iter (m_inf); intrusive_list::iterator thr_iter (m_thr); /* The loop below is written in the natural way as-if we'd always @@ -112,21 +123,24 @@ all_matching_threads_iterator::advance () the algorithm to the actual current position. */ goto start; - for (; m_inf != NULL; m_inf = m_inf->next) - if (m_inf_matches ()) - { - thr_iter = m_inf->thread_list.begin (); - while (thr_iter != m_inf->thread_list.end ()) - { - if (thr_iter->ptid.matches (m_filter_ptid)) - { - m_thr = &*thr_iter; - return; - } - start: - ++thr_iter; - } - } + for (; inf_iter != inferior_list.end (); ++inf_iter) + { + m_inf = &*inf_iter; + if (m_inf_matches ()) + { + thr_iter = m_inf->thread_list.begin (); + while (thr_iter != m_inf->thread_list.end ()) + { + if (thr_iter->ptid.matches (m_filter_ptid)) + { + m_thr = &*thr_iter; + return; + } + start: + ++thr_iter; + } + } + } m_thr = nullptr; } diff --git a/gdb/thread.c b/gdb/thread.c index 89f51c01c993..506e93cf4016 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -1395,7 +1395,11 @@ show_thread_that_caused_stop (void) int show_inferior_qualified_tids (void) { - return (inferior_list->next != NULL || inferior_list->num != 1); + auto inf = inferior_list.begin (); + if (inf->num != 1) + return true; + ++inf; + return inf != inferior_list.end (); } /* See gdbthread.h. */ -- 2.32.0