gdb: make inferior_list use intrusive_list

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.

Co-Authored-By: Simon Marchi <simon.marchi@efficios.com>
Change-Id: I7eb6a8f867d4dcf8b8cd2dcffd118f7270756018
This commit is contained in:
Pedro Alves 2020-06-14 20:57:04 +01:00 committed by Simon Marchi
parent bf80931081
commit 08bdefb58b
10 changed files with 104 additions and 117 deletions

View file

@ -1444,8 +1444,6 @@ ada_tasks_normal_stop_observer (struct bpstats *unused_args, int unused_args2)
static void static void
ada_tasks_new_objfile_observer (struct objfile *objfile) ada_tasks_new_objfile_observer (struct objfile *objfile)
{ {
struct inferior *inf;
/* Invalidate the relevant data in our program-space data. */ /* Invalidate the relevant data in our program-space data. */
if (objfile == NULL) 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 If all objfiles are being cleared (OBJFILE is NULL), then
clear the caches for all inferiors. */ 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) if (objfile == NULL || inf->pspace == objfile->pspace)
ada_tasks_invalidate_inferior_data (inf); ada_tasks_invalidate_inferior_data (inf);
} }

View file

@ -36,18 +36,21 @@ public:
typedef int difference_type; typedef int difference_type;
/* Create an iterator pointing at HEAD. */ /* Create an iterator pointing at HEAD. */
all_inferiors_iterator (process_stratum_target *proc_target, inferior *head) all_inferiors_iterator (process_stratum_target *proc_target,
: m_proc_target (proc_target) const intrusive_list<inferior> &list)
: m_proc_target (proc_target), m_inf_iter (list.begin ())
{ {
intrusive_list<inferior>::iterator end;
/* Advance M_INF to the first inferior's position. */ /* 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 ()) if (m_inf_matches ())
return; return;
} }
/* Create a one-past-end iterator. */ /* Create a one-past-end iterator. */
all_inferiors_iterator () all_inferiors_iterator ()
: m_proc_target (nullptr), m_inf (nullptr) : m_proc_target (nullptr)
{} {}
all_inferiors_iterator &operator++ () all_inferiors_iterator &operator++ ()
@ -57,37 +60,39 @@ public:
} }
inferior *operator* () const inferior *operator* () const
{ return m_inf; } { return &*m_inf_iter; }
bool operator!= (const all_inferiors_iterator &other) const bool operator!= (const all_inferiors_iterator &other) const
{ return m_inf != other.m_inf; } { return m_inf_iter != other.m_inf_iter; }
private: private:
/* Advance to next inferior, skipping filtered inferiors. */ /* Advance to next inferior, skipping filtered inferiors. */
void advance () void advance ()
{ {
intrusive_list<inferior>::iterator end;
/* The loop below is written in the natural way as-if we'd always /* The loop below is written in the natural way as-if we'd always
start at the beginning of the inferior list. This start at the beginning of the inferior list. This
fast-forwards the algorithm to the actual current position. */ fast-forwards the algorithm to the actual current position. */
goto start; goto start;
while (m_inf != NULL) while (m_inf_iter != end)
{ {
if (m_inf_matches ()) if (m_inf_matches ())
return; return;
start: start:
m_inf = m_inf->next; ++m_inf_iter;
} }
} }
bool m_inf_matches () bool m_inf_matches ()
{ {
return (m_proc_target == nullptr 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; process_stratum_target *m_proc_target;
inferior *m_inf; intrusive_list<inferior>::iterator m_inf_iter;
}; };
/* A range adapter that makes it possible to iterate over all /* A range adapter that makes it possible to iterate over all

View file

@ -41,7 +41,7 @@
DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD) DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
struct inferior *inferior_list = NULL; intrusive_list<inferior> inferior_list;
static int highest_inferior_num; static int highest_inferior_num;
/* See inferior.h. */ /* See inferior.h. */
@ -126,16 +126,7 @@ add_inferior_silent (int pid)
{ {
inferior *inf = new inferior (pid); inferior *inf = new inferior (pid);
if (inferior_list == NULL) inferior_list.push_back (*inf);
inferior_list = inf;
else
{
inferior *last;
for (last = inferior_list; last->next != NULL; last = last->next)
;
last->next = inf;
}
gdb::observers::inferior_added.notify (inf); gdb::observers::inferior_added.notify (inf);
@ -177,25 +168,12 @@ inferior::clear_thread_list (bool silent)
} }
void 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); inf->clear_thread_list (true);
if (infprev) auto it = inferior_list.iterator_to (*inf);
infprev->next = inf->next; inferior_list.erase (it);
else
inferior_list = inf->next;
gdb::observers::inferior_removed.notify (inf); gdb::observers::inferior_removed.notify (inf);
@ -210,17 +188,8 @@ delete_inferior (struct inferior *todel)
exit of its threads. */ exit of its threads. */
static void 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); inf->clear_thread_list (silent);
gdb::observers::inferior_exit.notify (inf); gdb::observers::inferior_exit.notify (inf);
@ -388,22 +357,14 @@ have_live_inferiors (void)
void void
prune_inferiors (void) prune_inferiors (void)
{ {
inferior *ss; for (inferior *inf : all_inferiors_safe ())
ss = inferior_list;
while (ss)
{ {
if (!ss->deletable () if (!inf->deletable ()
|| !ss->removable || !inf->removable
|| ss->pid != 0) || inf->pid != 0)
{ continue;
ss = ss->next;
continue;
}
inferior *ss_next = ss->next; delete_inferior (inf);
delete_inferior (ss);
ss = ss_next;
} }
} }

View file

@ -55,6 +55,7 @@ struct thread_info;
#include "gdbsupport/refcounted-object.h" #include "gdbsupport/refcounted-object.h"
#include "gdbsupport/forward-scope-exit.h" #include "gdbsupport/forward-scope-exit.h"
#include "gdbsupport/gdb_unique_ptr.h" #include "gdbsupport/gdb_unique_ptr.h"
#include "gdbsupport/intrusive_list.h"
#include "gdbsupport/common-inferior.h" #include "gdbsupport/common-inferior.h"
#include "gdbthread.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 listed exactly once in the inferior list, so placing an inferior in
the inferior list is an implicit, not counted strong reference. */ the inferior list is an implicit, not counted strong reference. */
class inferior : public refcounted_object class inferior : public refcounted_object,
public intrusive_list_node<inferior>
{ {
public: public:
explicit inferior (int pid); explicit inferior (int pid);
@ -387,9 +389,6 @@ public:
bool has_execution () bool has_execution ()
{ return target_has_execution (this); } { 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. */ /* This inferior's thread list, sorted by creation order. */
intrusive_list<thread_info> thread_list; intrusive_list<thread_info> thread_list;
@ -653,7 +652,7 @@ private:
/* Traverse all inferiors. */ /* Traverse all inferiors. */
extern struct inferior *inferior_list; extern intrusive_list<inferior> inferior_list;
/* Pull in the internals of the inferiors ranges and iterators. Must /* Pull in the internals of the inferiors ranges and iterators. Must
be done after struct inferior is defined. */ be done after struct inferior is defined. */

View file

@ -3730,18 +3730,28 @@ do_target_wait (execution_control_state *ecs, target_wait_flags options)
reported the stop to the user, polling for events. */ reported the stop to the user, polling for events. */
scoped_restore_current_thread restore_thread; scoped_restore_current_thread restore_thread;
int inf_num = selected->num; intrusive_list_iterator<inferior> start
for (inferior *inf = selected; inf != NULL; inf = inf->next) = inferior_list.iterator_to (*selected);
if (inferior_matches (inf))
if (do_wait (inf))
return true;
for (inferior *inf = inferior_list; for (intrusive_list_iterator<inferior> it = start;
inf != NULL && inf->num < inf_num; it != inferior_list.end ();
inf = inf->next) ++it)
if (inferior_matches (inf)) {
if (do_wait (inf)) inferior *inf = &*it;
if (inferior_matches (inf) && do_wait (inf))
return true; return true;
}
for (intrusive_list_iterator<inferior> 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; ecs->ws.kind = TARGET_WAITKIND_IGNORE;
return false; return false;
@ -9452,7 +9462,6 @@ infrun_thread_ptid_changed ()
scoped_mock_context<test_target_ops> target1 (arch); scoped_mock_context<test_target_ops> target1 (arch);
scoped_mock_context<test_target_ops> target2 (arch); scoped_mock_context<test_target_ops> target2 (arch);
target2.mock_inferior.next = &target1.mock_inferior;
ptid_t old_ptid (111, 222); ptid_t old_ptid (111, 222);
ptid_t new_ptid (111, 333); ptid_t new_ptid (111, 333);
@ -9477,7 +9486,6 @@ infrun_thread_ptid_changed ()
scoped_mock_context<test_target_ops> target1 (arch); scoped_mock_context<test_target_ops> target1 (arch);
scoped_mock_context<test_target_ops> target2 (arch); scoped_mock_context<test_target_ops> target2 (arch);
target2.mock_inferior.next = &target1.mock_inferior;
ptid_t old_ptid (111, 222); ptid_t old_ptid (111, 222);
ptid_t new_ptid (111, 333); ptid_t new_ptid (111, 333);

View file

@ -404,7 +404,6 @@ void
update_address_spaces (void) update_address_spaces (void)
{ {
int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ()); int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
struct inferior *inf;
init_address_spaces (); init_address_spaces ();
@ -423,7 +422,7 @@ update_address_spaces (void)
pspace->aspace = new_address_space (); 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 ())) if (gdbarch_has_global_solist (target_gdbarch ()))
inf->aspace = maybe_new_address_space (); inf->aspace = maybe_new_address_space ();
else else

View file

@ -2038,7 +2038,6 @@ regcache_thread_ptid_changed ()
/* Prepare two targets with one thread each, with the same ptid. */ /* Prepare two targets with one thread each, with the same ptid. */
scoped_mock_context<test_target_ops> target1 (arch); scoped_mock_context<test_target_ops> target1 (arch);
scoped_mock_context<test_target_ops> target2 (arch); scoped_mock_context<test_target_ops> target2 (arch);
target2.mock_inferior.next = &target1.mock_inferior;
ptid_t old_ptid (111, 222); ptid_t old_ptid (111, 222);
ptid_t new_ptid (111, 333); ptid_t new_ptid (111, 333);

View file

@ -44,13 +44,12 @@ struct scoped_mock_context
scoped_restore_current_pspace_and_thread restore_pspace_thread; 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<inferior *> restore_inferior_list
{&inferior_list, &mock_inferior};
explicit scoped_mock_context (gdbarch *gdbarch) 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.thread_list.push_back (mock_thread);
mock_inferior.gdbarch = gdbarch; mock_inferior.gdbarch = gdbarch;
mock_inferior.aspace = mock_pspace.aspace; mock_inferior.aspace = mock_pspace.aspace;
@ -70,6 +69,7 @@ struct scoped_mock_context
~scoped_mock_context () ~scoped_mock_context ()
{ {
inferior_list.erase (inferior_list.iterator_to (mock_inferior));
pop_all_targets_at_and_above (process_stratum); pop_all_targets_at_and_above (process_stratum);
} }
}; };

View file

@ -26,15 +26,18 @@
all_threads_iterator::all_threads_iterator (begin_t) all_threads_iterator::all_threads_iterator (begin_t)
{ {
/* Advance M_INF/M_THR to the first thread's position. */ /* 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 (); auto thr_iter = inf.thread_list.begin ();
if (thr_iter != m_inf->thread_list.end ()) if (thr_iter != inf.thread_list.end ())
{ {
m_inf = &inf;
m_thr = &*thr_iter; m_thr = &*thr_iter;
return; return;
} }
} }
m_inf = nullptr;
m_thr = nullptr; m_thr = nullptr;
} }
@ -43,6 +46,7 @@ all_threads_iterator::all_threads_iterator (begin_t)
void void
all_threads_iterator::advance () all_threads_iterator::advance ()
{ {
intrusive_list<inferior>::iterator inf_iter (m_inf);
intrusive_list<thread_info>::iterator thr_iter (m_thr); intrusive_list<thread_info>::iterator thr_iter (m_thr);
/* The loop below is written in the natural way as-if we'd always /* 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. */ the algorithm to the actual current position. */
goto start; 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 (); thr_iter = m_inf->thread_list.begin ();
while (thr_iter != m_inf->thread_list.end ()) 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) gdb_assert ((filter_target == nullptr && filter_ptid == minus_one_ptid)
|| filter_target->stratum () == process_stratum); || filter_target->stratum () == process_stratum);
for (m_inf = inferior_list; m_inf != NULL; m_inf = m_inf->next) for (inferior &inf : inferior_list)
if (m_inf_matches ()) {
for (auto thr_iter = m_inf->thread_list.begin (); m_inf = &inf;
thr_iter != m_inf->thread_list.end (); if (m_inf_matches ())
++thr_iter) for (auto thr_iter = m_inf->thread_list.begin ();
if (thr_iter->ptid.matches (m_filter_ptid)) thr_iter != m_inf->thread_list.end ();
++thr_iter)
{ {
m_thr = &*thr_iter; if (thr_iter->ptid.matches (m_filter_ptid))
return; {
m_thr = &*thr_iter;
return;
}
} }
}
m_thr = nullptr; m_thr = nullptr;
} }
@ -105,6 +115,7 @@ all_matching_threads_iterator::all_matching_threads_iterator
void void
all_matching_threads_iterator::advance () all_matching_threads_iterator::advance ()
{ {
intrusive_list<inferior>::iterator inf_iter (m_inf);
intrusive_list<thread_info>::iterator thr_iter (m_thr); intrusive_list<thread_info>::iterator thr_iter (m_thr);
/* The loop below is written in the natural way as-if we'd always /* 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. */ the algorithm to the actual current position. */
goto start; goto start;
for (; m_inf != NULL; m_inf = m_inf->next) for (; inf_iter != inferior_list.end (); ++inf_iter)
if (m_inf_matches ()) {
{ m_inf = &*inf_iter;
thr_iter = m_inf->thread_list.begin (); if (m_inf_matches ())
while (thr_iter != m_inf->thread_list.end ()) {
{ thr_iter = m_inf->thread_list.begin ();
if (thr_iter->ptid.matches (m_filter_ptid)) while (thr_iter != m_inf->thread_list.end ())
{ {
m_thr = &*thr_iter; if (thr_iter->ptid.matches (m_filter_ptid))
return; {
} m_thr = &*thr_iter;
start: return;
++thr_iter; }
} start:
} ++thr_iter;
}
}
}
m_thr = nullptr; m_thr = nullptr;
} }

View file

@ -1395,7 +1395,11 @@ show_thread_that_caused_stop (void)
int int
show_inferior_qualified_tids (void) 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. */ /* See gdbthread.h. */