• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GNU Binutils with patches for OS216


Commit MetaInfo

Revision026579ce0631d27b80cb98efe74f967487b11b40 (tree)
Zeit2018-10-01 19:27:53
AutorPedro Alves <palves@redh...>
CommiterPedro Alves

Log Message

Per-inferior thread list, thread ranges/iterators, down with ALL_THREADS, etc.

As preparation for multi-target, this patch makes each inferior have
its own thread list.

This isn't absolutely necessary for multi-target, but simplifies
things. It originally stemmed from the desire to eliminate the
init_thread_list calls sprinkled around, plus it makes it more
efficient to iterate over threads of a given inferior (no need to
always iterate over threads of all inferiors).

We still need to iterate over threads of all inferiors in a number of
places, which means we'd need adjust the ALL_THREADS /
ALL_NON_EXITED_THREADS macros. However, naively tweaking those macros
to have an extra for loop, like:

#define ALL_THREADS (thr, inf) \
for (inf = inferior_list; inf; inf = inf->next) \

for (thr = inf->thread_list; thr; thr = thr->next)

causes problems with code that does "break" or "continue" within the
ALL_THREADS loop body. Plus, we need to declare the extra "inf" local
variable in order to pass it as temporary variable to ALL_THREADS
(etc.)

It gets even trickier when we consider extending the macros to filter
out threads matching a ptid_t and a target. The macros become tricker
to read/write. Been there.

An alternative (which was my next attempt), is to replace the
ALL_THREADS etc. iteration style with for_each_all_threads,
for_each_non_exited_threads, etc. functions which would take a
callback as parameter, which would usually be passed a lambda.
However, I did not find that satisfactory at all, because the
resulting code ends up a little less natural / more noisy to read,
write and debug/step-through (due to use of lambdas), and in many
places where we use "continue;" to skip to the next thread now need to
use "return;". (I ran into hard to debug bugs caused by a
continue/return confusion.)

I.e., before:

ALL_NON_EXITED_THREADS (tp)
{

if (tp->not_what_I_want)
continue;
// do something

}

would turn into:

for_each_non_exited_thread ([&] (thread_info *tp)
{

if (tp->not_what_I_want)
return;
// do something

});

Lastly, the solution I settled with was to replace the ALL_THREADS /
ALL_NON_EXITED_THREADS / ALL_INFERIORS macros with (C++20-like) ranges
and iterators, such that you can instead naturaly iterate over
threads/inferiors using range-for, like e.g,.:

// all threads, including THREAD_EXITED threads.
for (thread_info *tp : all_threads ())
{ .... }
// all non-exited threads.
for (thread_info *tp : all_non_exited_threads ())
{ .... }
// all non-exited threads of INF inferior.
for (thread_info *tp : inf->non_exited_threads ())
{ .... }

The all_non_exited_threads() function takes an optional filter ptid_t as
parameter, which is quite convenient when we need to iterate over
threads matching that filter. See e.g., how the
set_executing/set_stop_requested/finish_thread_state etc. functions in
thread.c end up being simplified.

Most of the patch thus is about adding the infrustructure for allowing
the above. Later on when we get to actual multi-target, these
functions/ranges/iterators will gain a "target_ops *" parameter so
that e.g., we can iterate over all threads of a given target that
match a given filter ptid_t.

The only entry points users needs to be aware of are the
all_threads/all_non_exited_threads etc. functions seen above. Thus,
those functions are declared in gdbthread.h/inferior.h. The actual
iterators/ranges are mainly "internals" and thus are put out of view
in the new thread-iter.h/thread-iter.c/inferior-iter.h files. That
keeps the gdbthread.h/inferior.h headers quite a bit more readable.

A common/safe-iterator.h header is added which adds a template that
can be used to build "safe" iterators, which are forward iterators
that can be used to replace the ALL_THREADS_SAFE macro and other
instances of the same idiom in future.

There's a little bit of shuffling of code between
gdbthread.h/thread.c/inferior.h in the patch. That is necessary in
order to avoid circular dependencies between the
gdbthread.h/inferior.h headers.

As for the init_thread_list calls sprinkled around, they're all
eliminated by this patch, and a new, central call is added to
inferior_appeared. Note how also related to that, there's a call to
init_wait_for_inferior in remote.c that is eliminated.
init_wait_for_inferior is currently responsible for discarding skipped
inline frames, which had to be moved elsewhere. Given that nowadays
we always have a thread even for single-threaded processes, the
natural place is to delete a frame's inline frame info when we delete
the thread. I.e., from clear_thread_inferior_resources.

gdb/ChangeLog:
yyyy-mm-dd Pedro Alves <palves@redhat.com>

* Makefile.in (COMMON_SFILES): Add thread-iter.c.
* breakpoint.c (breakpoints_should_be_inserted_now): Replace
ALL_NON_EXITED_THREADS with all_non_exited_threads.
(print_one_breakpoint_location): Replace ALL_INFERIORS with
all_inferiors.
* bsd-kvm.c: Include inferior.h.
* btrace.c (btrace_free_objfile): Replace ALL_NON_EXITED_THREADS
with all_non_exited_threads.
* common/filtered-iterator.h: New.
* common/safe-iterator.h: New.
* corelow.c (core_target_open): Don't call init_thread_list here.
* darwin-nat.c (thread_info_from_private_thread_info): Replace
ALL_THREADS with all_threads.
* fbsd-nat.c (fbsd_nat_target::resume): Replace
ALL_NON_EXITED_THREADS with inf->non_exited_threads.
* fbsd-tdep.c (fbsd_make_corefile_notes): Replace
ALL_NON_EXITED_THREADS with inf->non_exited_threads.
* fork-child.c (postfork_hook): Don't call init_thread_list here.
* gdbarch-selftests.c (register_to_value_test): Adjust.
* gdbthread.h: Don't include "inferior.h" here.
(struct inferior): Forward declare.
(enum step_over_calls_kind): Moved here from inferior.h.
(thread_info::deletable): Definition moved to thread.c.
(find_thread_ptid (inferior *, ptid_t)): Declare.
(ALL_THREADS, ALL_THREADS_BY_INFERIOR, ALL_THREADS_SAFE): Delete.
Include "thread-iter.h".
(all_threads, all_non_exited_threads, all_threads_safe): New.
(any_thread_p): Declare.
(thread_list): Delete.
* infcmd.c (signal_command): Replace ALL_NON_EXITED_THREADS with
all_non_exited_threads.
(proceed_after_attach_callback): Delete.
(proceed_after_attach): Take an inferior pointer instead of an
integer PID. Adjust to use range-for.
(attach_post_wait): Pass down inferior pointer instead of pid.
Use range-for instead of ALL_NON_EXITED_THREADS.
(detach_command): Remove init_thread_list call.
* inferior-iter.h: New.
* inferior.c (struct delete_thread_of_inferior_arg): Delete.
(delete_thread_of_inferior): Delete.
(delete_inferior, exit_inferior_1): Use range-for with
inf->threads_safe() instead of iterate_over_threads.
(inferior_appeared): Call init_thread_list here.
(discard_all_inferiors): Use all_non_exited_inferiors.
(find_inferior_id, find_inferior_pid): Use all_inferiors.
(iterate_over_inferiors): Use all_inferiors_safe.
(have_inferiors, number_of_live_inferiors): Use
all_non_exited_inferiors.
(number_of_inferiors): Use all_inferiors and std::distance.
(print_inferior): Use all_inferiors.
* inferior.h: Include gdbthread.h.
(enum step_over_calls_kind): Moved to gdbthread.h.
(struct inferior) <thread_list>: New field.
<threads, non_exited_threads, threads_safe>: New methods.
(ALL_INFERIORS): Delete.
Include "inferior-iter.h".
(ALL_NON_EXITED_INFERIORS): Delete.
(all_inferiors_safe, all_inferiors, all_non_exited_inferiors): New
functions.
* inflow.c (child_interrupt, child_pass_ctrlc): Replace
ALL_NON_EXITED_THREADS with all_non_exited_threads.
* infrun.c (follow_exec): Use all_threads_safe.
(clear_proceed_status, proceed): Use all_non_exited_threads.
(init_wait_for_inferior): Don't clear inline frame state here.
(infrun_thread_stop_requested, for_each_just_stopped_thread): Use
all_threads instead of ALL_NON_EXITED_THREADS.
(random_pending_event_thread): Use all_non_exited_threads instead
of ALL_NON_EXITED_THREADS. Use a lambda for repeated code.
(clean_up_just_stopped_threads_fsms): Use all_non_exited_threads
instead of ALL_NON_EXITED_THREADS.
(handle_no_resumed): Use all_non_exited_threads instead of
ALL_NON_EXITED_THREADS. Use all_inferiors instead of
ALL_INFERIORS.
(restart_threads, switch_back_to_stepped_thread): Use
all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
* linux-nat.c (check_zombie_leaders): Replace ALL_INFERIORS with
all_inferiors.
(kill_unfollowed_fork_children): Use inf->non_exited_threads
instead of ALL_NON_EXITED_THREADS.
* linux-tdep.c (linux_make_corefile_notes): Use
inf->non_exited_threads instead of ALL_NON_EXITED_THREADS.
* linux-thread-db.c (thread_db_target::update_thread_list):
Replace ALL_INFERIORS with all_inferiors.
(thread_db_target::thread_handle_to_thread_info): Use
inf->non_exited_threads instead of ALL_NON_EXITED_THREADS.
* mi/mi-interp.c (multiple_inferiors_p): New.
(mi_on_resume_1): Simplify using all_non_exited_threads and
multiple_inferiors_p.
* mi/mi-main.c (mi_cmd_thread_list_ids): Use all_non_exited_threads
instead of ALL_NON_EXITED_THREADS.
* nto-procfs.c (nto_procfs_target::open): Don't call
init_thread_list here.
* record-btrace.c (record_btrace_target_open)
(record_btrace_target::stop_recording)
(record_btrace_target::close)
(record_btrace_target::record_is_replaying)
(record_btrace_target::resume, record_btrace_target::wait)
(record_btrace_target::record_stop_replaying): Use
all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
* record-full.c (record_full_wait_1): Use all_non_exited_threads
instead of ALL_NON_EXITED_THREADS.
* regcache.c (cooked_read_test): Remove reference to global
thread_list.
* remote-sim.c (gdbsim_target::create_inferior): Don't call
init_thread_list here.
* remote.c (remote_target::update_thread_list): Use
all_threads_safe instead of ALL_NON_EXITED_THREADS.
(remote_target::process_initial_stop_replies): Replace
ALL_INFERIORS with all_non_exited_inferiors and use
all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
(remote_target::open_1): Don't call init_thread_list here.
(remote_target::append_pending_thread_resumptions)
(remote_target::remote_resume_with_hc): Use all_non_exited_threads
instead of ALL_NON_EXITED_THREADS.
(remote_target::commit_resume)
(remote_target::remove_new_fork_children): Replace ALL_INFERIORS
with all_non_exited_inferiors and use all_non_exited_threads
instead of ALL_NON_EXITED_THREADS.
(remote_target::kill_new_fork_children): Use
all_non_exited_threads instead of ALL_NON_EXITED_THREADS. Remove
init_thread_list and init_wait_for_inferior calls.
(remote_target::remote_btrace_maybe_reopen)
(remote_target::thread_handle_to_thread_info): Use
all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
* target.c (target_terminal::restore_inferior)
(target_terminal_is_ours_kind): Replace ALL_INFERIORS with
all_non_exited_inferiors.
* thread-iter.c: New file.
* thread-iter.h: New file.
* thread.c: Include "inline-frame.h".
(thread_list): Delete.
(clear_thread_inferior_resources): Call clear_inline_frame_state.
(init_thread_list): Use all_threads_safe instead of
ALL_THREADS_SAFE. Adjust to per-inferior thread lists.
(new_thread): Adjust to per-inferior thread lists.
(add_thread_silent): Pass inferior to find_thread_ptid.
(thread_info::deletable): New, moved from the header.
(delete_thread_1): Adjust to per-inferior thread lists.
(find_thread_global_id): Use inf->threads().
(find_thread_ptid): Use find_inferior_ptid and pass inferior to
find_thread_ptid.
(find_thread_ptid(inferior*, ptid_t)): New overload.
(iterate_over_threads): Use all_threads_safe.
(any_thread_p): New.
(thread_count): Use all_threads and std::distance.
(live_threads_count): Use all_non_exited_threads and
std::distance.
(valid_global_thread_id): Use all_threads.
(in_thread_list): Use find_thread_ptid.
(first_thread_of_inferior): Adjust to per-inferior thread lists.
(any_thread_of_inferior, any_live_thread_of_inferior): Use
inf->non_exited_threads().
(prune_threads, delete_exited_threads): Use all_threads_safe.
(thread_change_ptid): Pass inferior pointer to find_thread_ptid.
(set_resumed, set_running): Use all_non_exited_threads.
(is_thread_state, is_stopped, is_exited, is_running)
(is_executing): Delete.
(set_executing, set_stop_requested, finish_thread_state): Use
all_non_exited_threads.
(print_thread_info_1): Use all_inferiors and all_threads.
(thread_apply_all_command): Use all_non_exited_threads.
(thread_find_command): Use all_threads.
(update_threads_executing): Use all_non_exited_threads.
* tid-parse.c (parse_thread_id): Use inf->threads.
* x86-bsd-nat.c (x86bsd_dr_set): Use inf->non_exited_threads ().

Ändern Zusammenfassung

Diff

--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1105,6 +1105,7 @@ COMMON_SFILES = \
11051105 target-descriptions.c \
11061106 target-memory.c \
11071107 thread.c \
1108+ thread-iter.c \
11081109 thread-fsm.c \
11091110 tid-parse.c \
11101111 top.c \
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -400,8 +400,6 @@ breakpoints_should_be_inserted_now (void)
400400 }
401401 else if (target_has_execution)
402402 {
403- struct thread_info *tp;
404-
405403 if (always_inserted_mode)
406404 {
407405 /* The user wants breakpoints inserted even if all threads
@@ -414,7 +412,7 @@ breakpoints_should_be_inserted_now (void)
414412
415413 /* Don't remove breakpoints yet if, even though all threads are
416414 stopped, we still have events to process. */
417- ALL_NON_EXITED_THREADS (tp)
415+ for (thread_info *tp : all_non_exited_threads ())
418416 if (tp->resumed
419417 && tp->suspend.waitstatus_pending_p)
420418 return 1;
@@ -6156,11 +6154,10 @@ print_one_breakpoint_location (struct breakpoint *b,
61566154
61576155 if (loc != NULL && !header_of_multiple)
61586156 {
6159- struct inferior *inf;
61606157 std::vector<int> inf_nums;
61616158 int mi_only = 1;
61626159
6163- ALL_INFERIORS (inf)
6160+ for (inferior *inf : all_inferiors ())
61646161 {
61656162 if (inf->pspace == loc->pspace)
61666163 inf_nums.push_back (inf->num);
--- a/gdb/bsd-kvm.c
+++ b/gdb/bsd-kvm.c
@@ -25,7 +25,8 @@
2525 #include "regcache.h"
2626 #include "target.h"
2727 #include "value.h"
28-#include "gdbcore.h" /* for get_exec_file */
28+#include "gdbcore.h"
29+#include "inferior.h" /* for get_exec_file */
2930 #include "gdbthread.h"
3031
3132 #include <fcntl.h>
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -1989,11 +1989,9 @@ btrace_clear (struct thread_info *tp)
19891989 void
19901990 btrace_free_objfile (struct objfile *objfile)
19911991 {
1992- struct thread_info *tp;
1993-
19941992 DEBUG ("free objfile");
19951993
1996- ALL_NON_EXITED_THREADS (tp)
1994+ for (thread_info *tp : all_non_exited_threads ())
19971995 btrace_clear (tp);
19981996 }
19991997
--- /dev/null
+++ b/gdb/common/filtered-iterator.h
@@ -0,0 +1,87 @@
1+/* A forward filtered iterator for GDB, the GNU debugger.
2+ Copyright (C) 2018 Free Software Foundation, Inc.
3+
4+ This file is part of GDB.
5+
6+ This program is free software; you can redistribute it and/or modify
7+ it under the terms of the GNU General Public License as published by
8+ the Free Software Foundation; either version 3 of the License, or
9+ (at your option) any later version.
10+
11+ This program is distributed in the hope that it will be useful,
12+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ GNU General Public License for more details.
15+
16+ You should have received a copy of the GNU General Public License
17+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
18+
19+#ifndef FILTERED_ITERATOR_H
20+#define FILTERED_ITERATOR_H
21+
22+/* A filtered iterator. This wraps BaseIterator and automatically
23+ skips elements that FilterFunc filters out. Requires that
24+ default-constructing a BaseIterator creates a valid one-past-end
25+ iterator. */
26+
27+template<typename BaseIterator, typename FilterFunc>
28+class filtered_iterator
29+{
30+public:
31+ typedef filtered_iterator self_type;
32+ typedef typename BaseIterator::value_type value_type;
33+ typedef typename BaseIterator::reference reference;
34+ typedef typename BaseIterator::pointer pointer;
35+ typedef typename BaseIterator::iterator_category iterator_category;
36+ typedef typename BaseIterator::difference_type difference_type;
37+
38+ /* Construct by forwarding all arguments to the underlying
39+ iterator. */
40+ template<typename... Args>
41+ explicit filtered_iterator (Args &&...args)
42+ : m_it (std::forward<Args> (args)...)
43+ { skip_filtered (); }
44+
45+ /* Create a one-past-end iterator. */
46+ filtered_iterator () = default;
47+
48+ /* Need these as the variadic constructor would be a better match
49+ otherwise. */
50+ filtered_iterator (filtered_iterator &) = default;
51+ filtered_iterator (const filtered_iterator &) = default;
52+ filtered_iterator (filtered_iterator &&) = default;
53+ filtered_iterator (const filtered_iterator &&other)
54+ : filtered_iterator (static_cast<const filtered_iterator &> (other))
55+ {}
56+
57+ value_type operator* () const { return *m_it; }
58+
59+ self_type &operator++ ()
60+ {
61+ ++m_it;
62+ skip_filtered ();
63+ return *this;
64+ }
65+
66+ bool operator== (const self_type &other) const
67+ { return *m_it == *other.m_it; }
68+
69+ bool operator!= (const self_type &other) const
70+ { return *m_it != *other.m_it; }
71+
72+private:
73+
74+ void skip_filtered ()
75+ {
76+ for (; m_it != m_end; ++m_it)
77+ if (m_filter (*m_it))
78+ break;
79+ }
80+
81+private:
82+ FilterFunc m_filter {};
83+ BaseIterator m_it {};
84+ BaseIterator m_end {};
85+};
86+
87+#endif /* FILTERED_ITERATOR_H */
--- /dev/null
+++ b/gdb/common/safe-iterator.h
@@ -0,0 +1,93 @@
1+/* A safe iterator for GDB, the GNU debugger.
2+ Copyright (C) 2018 Free Software Foundation, Inc.
3+
4+ This file is part of GDB.
5+
6+ This program is free software; you can redistribute it and/or modify
7+ it under the terms of the GNU General Public License as published by
8+ the Free Software Foundation; either version 3 of the License, or
9+ (at your option) any later version.
10+
11+ This program is distributed in the hope that it will be useful,
12+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ GNU General Public License for more details.
15+
16+ You should have received a copy of the GNU General Public License
17+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
18+
19+#ifndef SAFE_ITERATOR_H
20+#define SAFE_ITERATOR_H
21+
22+/* A forward iterator that wraps Iterator, such that when iterating
23+ with iterator IT, it is possible to delete *IT without invalidating
24+ IT. Suitably wrapped in a range type and used with range-for, this
25+ allow convenient patterns like this:
26+
27+ // range_safe() returns a range type whose begin()/end() methods
28+ // return safe iterators.
29+ for (foo *f : range_safe ())
30+ {
31+ if (f->should_delete ())
32+ {
33+ // The ++it operation implicitly done by the range-for is
34+ // still OK after this.
35+ delete f;
36+ }
37+ }
38+*/
39+
40+template<typename Iterator>
41+class basic_safe_iterator
42+{
43+public:
44+ typedef basic_safe_iterator self_type;
45+ typedef typename Iterator::value_type value_type;
46+ typedef typename Iterator::reference reference;
47+ typedef typename Iterator::pointer pointer;
48+ typedef typename Iterator::iterator_category iterator_category;
49+ typedef typename Iterator::difference_type difference_type;
50+
51+ /* Construct by forwarding all arguments to the underlying
52+ iterator. */
53+ template<typename... Args>
54+ explicit basic_safe_iterator (Args &&...args)
55+ : m_it (std::forward<Args> (args)...),
56+ m_next (m_it)
57+ {
58+ if (m_it != m_end)
59+ ++m_next;
60+ }
61+
62+ /* Create a one-past-end iterator. */
63+ basic_safe_iterator ()
64+ {}
65+
66+ value_type operator* () const { return *m_it; }
67+
68+ self_type &operator++ ()
69+ {
70+ m_it = m_next;
71+ if (m_it != m_end)
72+ ++m_next;
73+ return *this;
74+ }
75+
76+ bool operator== (const self_type &other) const
77+ { return m_it == other.m_it; }
78+
79+ bool operator!= (const self_type &other) const
80+ { return m_it != other.m_it; }
81+
82+private:
83+ /* The current element. */
84+ Iterator m_it {};
85+
86+ /* The next element. Always one element ahead of M_IT. */
87+ Iterator m_next {};
88+
89+ /* A one-past-end iterator. */
90+ Iterator m_end {};
91+};
92+
93+#endif /* SAFE_ITERATOR_H */
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -421,12 +421,6 @@ core_target_open (const char *arg, int from_tty)
421421 push_target (target);
422422 target_holder.release ();
423423
424- /* Do this before acknowledging the inferior, so if
425- post_create_inferior throws (can happen easilly if you're loading
426- a core file with the wrong exec), we aren't left with threads
427- from the previous inferior. */
428- init_thread_list ();
429-
430424 inferior_ptid = null_ptid;
431425
432426 /* Need to flush the register cache (and the frame cache) from a
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1698,19 +1698,15 @@ darwin_attach_pid (struct inferior *inf)
16981698 static struct thread_info *
16991699 thread_info_from_private_thread_info (darwin_thread_info *pti)
17001700 {
1701- struct thread_info *it;
1702-
1703- ALL_THREADS (it)
1701+ for (struct thread_info *it : all_threads ())
17041702 {
17051703 darwin_thread_info *iter_pti = get_darwin_thread_info (it);
17061704
17071705 if (iter_pti->gdb_port == pti->gdb_port)
1708- break;
1706+ return it;
17091707 }
17101708
1711- gdb_assert (it != NULL);
1712-
1713- return it;
1709+ gdb_assert_not_reached ("did not find gdb thread for darwin thread");
17141710 }
17151711
17161712 static void
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -1189,13 +1189,11 @@ fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
11891189 if (ptid.lwp_p ())
11901190 {
11911191 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
1192- struct thread_info *tp;
1193- int request;
1192+ inferior *inf = find_inferior_ptid (ptid);
11941193
1195- ALL_NON_EXITED_THREADS (tp)
1194+ for (thread_info *tp : inf->non_exited_threads ())
11961195 {
1197- if (tp->ptid.pid () != ptid.pid ())
1198- continue;
1196+ int request;
11991197
12001198 if (tp->ptid.lwp () == ptid.lwp ())
12011199 request = PT_RESUME;
@@ -1210,16 +1208,9 @@ fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
12101208 {
12111209 /* If ptid is a wildcard, resume all matching threads (they won't run
12121210 until the process is continued however). */
1213- struct thread_info *tp;
1214-
1215- ALL_NON_EXITED_THREADS (tp)
1216- {
1217- if (!tp->ptid.matches (ptid))
1218- continue;
1219-
1220- if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1)
1221- perror_with_name (("ptrace"));
1222- }
1211+ for (thread_info *tp : all_non_exited_threads (ptid))
1212+ if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1)
1213+ perror_with_name (("ptrace"));
12231214 ptid = inferior_ptid;
12241215 }
12251216
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -653,7 +653,7 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
653653 struct fbsd_corefile_thread_data thread_args;
654654 char *note_data = NULL;
655655 Elf_Internal_Ehdr *i_ehdrp;
656- struct thread_info *curr_thr, *signalled_thr, *thr;
656+ struct thread_info *curr_thr, *signalled_thr;
657657
658658 /* Put a "FreeBSD" label in the ELF header. */
659659 i_ehdrp = elf_elfheader (obfd);
@@ -706,12 +706,10 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
706706 thread_args.stop_signal = signalled_thr->suspend.stop_signal;
707707
708708 fbsd_corefile_thread (signalled_thr, &thread_args);
709- ALL_NON_EXITED_THREADS (thr)
709+ for (thread_info *thr : current_inferior ()->non_exited_threads ())
710710 {
711711 if (thr == signalled_thr)
712712 continue;
713- if (thr->ptid.pid () != inferior_ptid.pid ())
714- continue;
715713
716714 fbsd_corefile_thread (thr, &thread_args);
717715 }
--- a/gdb/fork-child.c
+++ b/gdb/fork-child.c
@@ -78,12 +78,7 @@ prefork_hook (const char *args)
7878 void
7979 postfork_hook (pid_t pid)
8080 {
81- struct inferior *inf;
82-
83- if (!have_inferiors ())
84- init_thread_list ();
85-
86- inf = current_inferior ();
81+ inferior *inf = current_inferior ();
8782
8883 inferior_appeared (inf, pid);
8984
--- a/gdb/gdbarch-selftests.c
+++ b/gdb/gdbarch-selftests.c
@@ -86,7 +86,7 @@ register_to_value_test (struct gdbarch *gdbarch)
8686 thread_info mock_thread (&mock_inferior, mock_ptid);
8787
8888 scoped_restore restore_thread_list
89- = make_scoped_restore (&thread_list, &mock_thread);
89+ = make_scoped_restore (&mock_inferior.thread_list, &mock_thread);
9090
9191 /* Add the mock inferior to the inferior list so that look ups by
9292 target+ptid can find it. */
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -26,7 +26,6 @@ struct symtab;
2626 #include "breakpoint.h"
2727 #include "frame.h"
2828 #include "ui-out.h"
29-#include "inferior.h"
3029 #include "btrace.h"
3130 #include "common/vec.h"
3231 #include "target/waitstatus.h"
@@ -34,6 +33,8 @@ struct symtab;
3433 #include "common/refcounted-object.h"
3534 #include "common-gdbthread.h"
3635
36+struct inferior;
37+
3738 /* Frontend view of the thread state. Possible extensions: stepping,
3839 finishing, until(ling),... */
3940 enum thread_state
@@ -43,6 +44,17 @@ enum thread_state
4344 THREAD_EXITED,
4445 };
4546
47+/* STEP_OVER_ALL means step over all subroutine calls.
48+ STEP_OVER_UNDEBUGGABLE means step over calls to undebuggable functions.
49+ STEP_OVER_NONE means don't step over any subroutine calls. */
50+
51+enum step_over_calls_kind
52+ {
53+ STEP_OVER_NONE,
54+ STEP_OVER_ALL,
55+ STEP_OVER_UNDEBUGGABLE
56+ };
57+
4658 /* Inferior thread specific part of `struct infcall_control_state'.
4759
4860 Inferior process counterpart is `struct inferior_control_state'. */
@@ -213,12 +225,7 @@ public:
213225 explicit thread_info (inferior *inf, ptid_t ptid);
214226 ~thread_info ();
215227
216- bool deletable () const
217- {
218- /* If this is the current thread, or there's code out there that
219- relies on it existing (refcount > 0) we can't delete yet. */
220- return (refcount () == 0 && ptid != inferior_ptid);
221- }
228+ bool deletable () const;
222229
223230 /* Mark this thread as running and notify observers. */
224231 void set_running (bool running);
@@ -449,6 +456,10 @@ extern int valid_global_thread_id (int global_id);
449456 /* Search function to lookup a thread by 'pid'. */
450457 extern struct thread_info *find_thread_ptid (ptid_t ptid);
451458
459+/* Search function to lookup a thread by 'ptid'. Only searches in
460+ threads of INF. */
461+extern struct thread_info *find_thread_ptid (inferior *inf, ptid_t ptid);
462+
452463 /* Find thread by GDB global thread ID. */
453464 struct thread_info *find_thread_global_id (int global_id);
454465
@@ -475,32 +486,61 @@ void thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid);
475486 typedef int (*thread_callback_func) (struct thread_info *, void *);
476487 extern struct thread_info *iterate_over_threads (thread_callback_func, void *);
477488
478-/* Traverse all threads. */
479-#define ALL_THREADS(T) \
480- for (T = thread_list; T; T = T->next) \
489+/* Pull in the internals of the inferiors/threads ranges and
490+ iterators. Must be done after struct thread_info is defined. */
491+#include "thread-iter.h"
492+
493+/* Return a range that can be used to walk over all threads of all
494+ inferiors, with range-for. Used like this:
495+
496+ for (thread_info *thr : all_threads ())
497+ { .... }
498+*/
499+inline all_threads_range
500+all_threads ()
501+{
502+ return {};
503+}
504+
505+/* Likewise, but accept a filter PTID. */
481506
482-/* Traverse over all threads, sorted by inferior. */
483-#define ALL_THREADS_BY_INFERIOR(inf, tp) \
484- ALL_INFERIORS (inf) \
485- ALL_THREADS (tp) \
486- if (inf == tp->inf)
507+inline all_matching_threads_range
508+all_threads (ptid_t filter_ptid)
509+{
510+ return all_matching_threads_range (filter_ptid);
511+}
487512
488-/* Traverse all threads, except those that have THREAD_EXITED
489- state. */
513+/* Return a range that can be used to walk over all non-exited threads
514+ of all inferiors, with range-for. FILTER_PTID can be used to
515+ filter out thread that don't match. */
516+
517+inline all_non_exited_threads_range
518+all_non_exited_threads (ptid_t filter_ptid = minus_one_ptid)
519+{
520+ return all_non_exited_threads_range (filter_ptid);
521+}
490522
491-#define ALL_NON_EXITED_THREADS(T) \
492- for (T = thread_list; T; T = T->next) \
493- if ((T)->state != THREAD_EXITED)
523+/* Return a range that can be used to walk over all threads of all
524+ inferiors, with range-for, safely. I.e., it is safe to delete the
525+ currently-iterated thread. When combined with range-for, this
526+ allow convenient patterns like this:
494527
495-/* Traverse all threads, including those that have THREAD_EXITED
496- state. Allows deleting the currently iterated thread. */
497-#define ALL_THREADS_SAFE(T, TMP) \
498- for ((T) = thread_list; \
499- (T) != NULL ? ((TMP) = (T)->next, 1): 0; \
500- (T) = (TMP))
528+ for (thread_info *t : all_threads_safe ())
529+ if (some_condition ())
530+ delete f;
531+*/
532+
533+inline all_threads_safe_range
534+all_threads_safe ()
535+{
536+ return all_threads_safe_range ();
537+}
501538
502539 extern int thread_count (void);
503540
541+/* Return true if we have any thread in any inferior. */
542+extern bool any_thread_p ();
543+
504544 /* Switch context to thread THR. Also sets the STOP_PC global. */
505545 extern void switch_to_thread (struct thread_info *thr);
506546
@@ -748,6 +788,4 @@ extern void print_selected_thread_frame (struct ui_out *uiout,
748788 alive anymore. */
749789 extern void thread_select (const char *tidstr, class thread_info *thr);
750790
751-extern struct thread_info *thread_list;
752-
753791 #endif /* GDBTHREAD_H */
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -1339,20 +1339,16 @@ signal_command (const char *signum_exp, int from_tty)
13391339 of the wrong thread. */
13401340 if (!non_stop)
13411341 {
1342- struct thread_info *tp;
1343- ptid_t resume_ptid;
13441342 int must_confirm = 0;
13451343
13461344 /* This indicates what will be resumed. Either a single thread,
13471345 a whole process, or all threads of all processes. */
1348- resume_ptid = user_visible_resume_ptid (0);
1346+ ptid_t resume_ptid = user_visible_resume_ptid (0);
13491347
1350- ALL_NON_EXITED_THREADS (tp)
1348+ for (thread_info *tp : all_non_exited_threads (resume_ptid))
13511349 {
13521350 if (tp->ptid == inferior_ptid)
13531351 continue;
1354- if (!tp->ptid.matches (resume_ptid))
1355- continue;
13561352
13571353 if (tp->suspend.stop_signal != GDB_SIGNAL_0
13581354 && signal_pass_state (tp->suspend.stop_signal))
@@ -2626,34 +2622,13 @@ kill_command (const char *arg, int from_tty)
26262622 bfd_cache_close_all ();
26272623 }
26282624
2629-/* Used in `attach&' command. ARG is a point to an integer
2630- representing a process id. Proceed threads of this process iff
2625+/* Used in `attach&' command. Proceed threads of inferior INF iff
26312626 they stopped due to debugger request, and when they did, they
2632- reported a clean stop (GDB_SIGNAL_0). Do not proceed threads
2633- that have been explicitly been told to stop. */
2634-
2635-static int
2636-proceed_after_attach_callback (struct thread_info *thread,
2637- void *arg)
2638-{
2639- int pid = * (int *) arg;
2640-
2641- if (thread->ptid.pid () == pid
2642- && thread->state != THREAD_EXITED
2643- && !thread->executing
2644- && !thread->stop_requested
2645- && thread->suspend.stop_signal == GDB_SIGNAL_0)
2646- {
2647- switch_to_thread (thread);
2648- clear_proceed_status (0);
2649- proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2650- }
2651-
2652- return 0;
2653-}
2627+ reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2628+ have been explicitly been told to stop. */
26542629
26552630 static void
2656-proceed_after_attach (int pid)
2631+proceed_after_attach (inferior *inf)
26572632 {
26582633 /* Don't error out if the current thread is running, because
26592634 there may be other stopped threads. */
@@ -2661,7 +2636,15 @@ proceed_after_attach (int pid)
26612636 /* Backup current thread and selected frame. */
26622637 scoped_restore_current_thread restore_thread;
26632638
2664- iterate_over_threads (proceed_after_attach_callback, &pid);
2639+ for (thread_info *thread : inf->non_exited_threads ())
2640+ if (!thread->executing
2641+ && !thread->stop_requested
2642+ && thread->suspend.stop_signal == GDB_SIGNAL_0)
2643+ {
2644+ switch_to_thread (thread);
2645+ clear_proceed_status (0);
2646+ proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2647+ }
26652648 }
26662649
26672650 /* See inferior.h. */
@@ -2728,7 +2711,7 @@ attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mod
27282711 already running threads. If a thread has been stopped with a
27292712 signal, leave it be. */
27302713 if (non_stop)
2731- proceed_after_attach (inferior->pid);
2714+ proceed_after_attach (inferior);
27322715 else
27332716 {
27342717 if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0)
@@ -2754,9 +2737,7 @@ attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mod
27542737 target_stop (ptid_t (inferior->pid));
27552738 else if (target_is_non_stop_p ())
27562739 {
2757- struct thread_info *thread;
27582740 struct thread_info *lowest = inferior_thread ();
2759- int pid = current_inferior ()->pid;
27602741
27612742 stop_all_threads ();
27622743
@@ -2764,15 +2745,10 @@ attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mod
27642745 stop. For consistency, always select the thread with
27652746 lowest GDB number, which should be the main thread, if it
27662747 still exists. */
2767- ALL_NON_EXITED_THREADS (thread)
2768- {
2769- if (thread->ptid.pid () == pid)
2770- {
2771- if (thread->inf->num < lowest->inf->num
2772- || thread->per_inf_num < lowest->per_inf_num)
2773- lowest = thread;
2774- }
2775- }
2748+ for (thread_info *thread : current_inferior ()->non_exited_threads ())
2749+ if (thread->inf->num < lowest->inf->num
2750+ || thread->per_inf_num < lowest->per_inf_num)
2751+ lowest = thread;
27762752
27772753 switch_to_thread (lowest);
27782754 }
@@ -3020,11 +2996,6 @@ detach_command (const char *args, int from_tty)
30202996 if (!gdbarch_has_global_solist (target_gdbarch ()))
30212997 no_shared_libraries (NULL, from_tty);
30222998
3023- /* If we still have inferiors to debug, then don't mess with their
3024- threads. */
3025- if (!have_inferiors ())
3026- init_thread_list ();
3027-
30282999 if (deprecated_detach_hook)
30293000 deprecated_detach_hook ();
30303001 }
--- /dev/null
+++ b/gdb/inferior-iter.h
@@ -0,0 +1,117 @@
1+/* Inferior iterators and ranges for GDB, the GNU debugger.
2+
3+ Copyright (C) 2018 Free Software Foundation, Inc.
4+
5+ This file is part of GDB.
6+
7+ This program is free software; you can redistribute it and/or modify
8+ it under the terms of the GNU General Public License as published by
9+ the Free Software Foundation; either version 3 of the License, or
10+ (at your option) any later version.
11+
12+ This program is distributed in the hope that it will be useful,
13+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ GNU General Public License for more details.
16+
17+ You should have received a copy of the GNU General Public License
18+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
19+
20+#ifndef INFERIOR_ITER_H
21+#define INFERIOR_ITER_H
22+
23+#include "common/filtered-iterator.h"
24+#include "common/safe-iterator.h"
25+
26+/* A forward iterator that iterates over all inferiors. */
27+
28+class all_inferiors_iterator
29+{
30+public:
31+ typedef all_inferiors_iterator self_type;
32+ typedef struct inferior *value_type;
33+ typedef struct inferior *&reference;
34+ typedef struct inferior **pointer;
35+ typedef std::forward_iterator_tag iterator_category;
36+ typedef int difference_type;
37+
38+ /* Create an iterator pointing at HEAD. */
39+ explicit all_inferiors_iterator (inferior *head)
40+ : m_inf (head)
41+ {}
42+
43+ /* Create a one-past-end iterator. */
44+ all_inferiors_iterator ()
45+ : m_inf (nullptr)
46+ {}
47+
48+ all_inferiors_iterator &operator++ ()
49+ {
50+ m_inf = m_inf->next;
51+ return *this;
52+ }
53+
54+ inferior *operator* () const
55+ { return m_inf; }
56+
57+ bool operator!= (const all_inferiors_iterator &other) const
58+ { return m_inf != other.m_inf; }
59+
60+private:
61+ inferior *m_inf;
62+};
63+
64+/* Filter for filtered_iterator. Filters out exited inferiors. */
65+
66+struct exited_inferior_filter
67+{
68+ bool operator() (inferior *inf)
69+ {
70+ return inf->pid != 0;
71+ }
72+};
73+
74+/* Iterate over all non-exited inferiors. */
75+
76+using all_non_exited_inferiors_iterator
77+ = filtered_iterator<all_inferiors_iterator, exited_inferior_filter>;
78+
79+/* A range adapter that makes it possible to iterate over all
80+ inferiors with range-for. */
81+struct all_inferiors_range
82+{
83+ all_inferiors_iterator begin () const
84+ { return all_inferiors_iterator (inferior_list); }
85+ all_inferiors_iterator end () const
86+ { return all_inferiors_iterator (); }
87+};
88+
89+/* Iterate over all inferiors, safely. */
90+
91+using all_inferiors_safe_iterator
92+ = basic_safe_iterator<all_inferiors_iterator>;
93+
94+/* A range adapter that makes it possible to iterate over all
95+ inferiors with range-for "safely". I.e., it is safe to delete the
96+ currently-iterated inferior. */
97+
98+struct all_inferiors_safe_range
99+{
100+ all_inferiors_safe_iterator begin () const
101+ { return all_inferiors_safe_iterator (inferior_list); }
102+ all_inferiors_safe_iterator end () const
103+ { return all_inferiors_safe_iterator (); }
104+};
105+
106+/* A range adapter that makes it possible to iterate over all
107+ non-exited inferiors with range-for. */
108+
109+struct all_non_exited_inferiors_range
110+{
111+ all_non_exited_inferiors_iterator begin () const
112+ { return all_non_exited_inferiors_iterator (inferior_list); }
113+ all_non_exited_inferiors_iterator end () const
114+ { return all_non_exited_inferiors_iterator (); }
115+};
116+
117+#endif /* !defined (INFERIOR_ITER_H) */
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -134,34 +134,10 @@ add_inferior (int pid)
134134 return inf;
135135 }
136136
137-struct delete_thread_of_inferior_arg
138-{
139- int pid;
140- int silent;
141-};
142-
143-static int
144-delete_thread_of_inferior (struct thread_info *tp, void *data)
145-{
146- struct delete_thread_of_inferior_arg *arg
147- = (struct delete_thread_of_inferior_arg *) data;
148-
149- if (tp->ptid.pid () == arg->pid)
150- {
151- if (arg->silent)
152- delete_thread_silent (tp);
153- else
154- delete_thread (tp);
155- }
156-
157- return 0;
158-}
159-
160137 void
161138 delete_inferior (struct inferior *todel)
162139 {
163140 struct inferior *inf, *infprev;
164- struct delete_thread_of_inferior_arg arg;
165141
166142 infprev = NULL;
167143
@@ -172,10 +148,8 @@ delete_inferior (struct inferior *todel)
172148 if (!inf)
173149 return;
174150
175- arg.pid = inf->pid;
176- arg.silent = 1;
177-
178- iterate_over_threads (delete_thread_of_inferior, &arg);
151+ for (thread_info *tp : inf->threads_safe ())
152+ delete_thread_silent (tp);
179153
180154 if (infprev)
181155 infprev->next = inf->next;
@@ -198,7 +172,6 @@ static void
198172 exit_inferior_1 (struct inferior *inftoex, int silent)
199173 {
200174 struct inferior *inf;
201- struct delete_thread_of_inferior_arg arg;
202175
203176 for (inf = inferior_list; inf; inf = inf->next)
204177 if (inf == inftoex)
@@ -207,10 +180,13 @@ exit_inferior_1 (struct inferior *inftoex, int silent)
207180 if (!inf)
208181 return;
209182
210- arg.pid = inf->pid;
211- arg.silent = silent;
212-
213- iterate_over_threads (delete_thread_of_inferior, &arg);
183+ for (thread_info *tp : inf->threads_safe ())
184+ {
185+ if (silent)
186+ delete_thread_silent (tp);
187+ else
188+ delete_thread (tp);
189+ }
214190
215191 gdb::observers::inferior_exit.notify (inf);
216192
@@ -273,6 +249,11 @@ detach_inferior (inferior *inf)
273249 void
274250 inferior_appeared (struct inferior *inf, int pid)
275251 {
252+ /* If this is the first inferior with threads, reset the global
253+ thread id. */
254+ if (!any_thread_p ())
255+ init_thread_list ();
256+
276257 inf->pid = pid;
277258 inf->has_exit_code = 0;
278259 inf->exit_code = 0;
@@ -283,21 +264,14 @@ inferior_appeared (struct inferior *inf, int pid)
283264 void
284265 discard_all_inferiors (void)
285266 {
286- struct inferior *inf;
287-
288- for (inf = inferior_list; inf; inf = inf->next)
289- {
290- if (inf->pid != 0)
291- exit_inferior_silent (inf);
292- }
267+ for (inferior *inf : all_non_exited_inferiors ())
268+ exit_inferior_silent (inf);
293269 }
294270
295271 struct inferior *
296272 find_inferior_id (int num)
297273 {
298- struct inferior *inf;
299-
300- for (inf = inferior_list; inf; inf = inf->next)
274+ for (inferior *inf : all_inferiors ())
301275 if (inf->num == num)
302276 return inf;
303277
@@ -307,14 +281,12 @@ find_inferior_id (int num)
307281 struct inferior *
308282 find_inferior_pid (int pid)
309283 {
310- struct inferior *inf;
311-
312284 /* Looking for inferior pid == 0 is always wrong, and indicative of
313285 a bug somewhere else. There may be more than one with pid == 0,
314286 for instance. */
315287 gdb_assert (pid != 0);
316288
317- for (inf = inferior_list; inf; inf = inf->next)
289+ for (inferior *inf : all_inferiors ())
318290 if (inf->pid == pid)
319291 return inf;
320292
@@ -339,11 +311,9 @@ find_inferior_for_program_space (struct program_space *pspace)
339311 if (inf->pspace == pspace)
340312 return inf;
341313
342- for (inf = inferior_list; inf != NULL; inf = inf->next)
343- {
344- if (inf->pspace == pspace)
345- return inf;
346- }
314+ for (inferior *inf : all_inferiors ())
315+ if (inf->pspace == pspace)
316+ return inf;
347317
348318 return NULL;
349319 }
@@ -352,14 +322,9 @@ struct inferior *
352322 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
353323 void *data)
354324 {
355- struct inferior *inf, *infnext;
356-
357- for (inf = inferior_list; inf; inf = infnext)
358- {
359- infnext = inf->next;
360- if ((*callback) (inf, data))
361- return inf;
362- }
325+ for (inferior *inf : all_inferiors_safe ())
326+ if ((*callback) (inf, data))
327+ return inf;
363328
364329 return NULL;
365330 }
@@ -367,11 +332,8 @@ iterate_over_inferiors (int (*callback) (struct inferior *, void *),
367332 int
368333 have_inferiors (void)
369334 {
370- struct inferior *inf;
371-
372- for (inf = inferior_list; inf; inf = inf->next)
373- if (inf->pid != 0)
374- return 1;
335+ for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
336+ return 1;
375337
376338 return 0;
377339 }
@@ -383,24 +345,17 @@ have_inferiors (void)
383345 int
384346 number_of_live_inferiors (void)
385347 {
386- struct inferior *inf;
387348 int num_inf = 0;
388349
389- for (inf = inferior_list; inf; inf = inf->next)
390- if (inf->pid != 0)
391- {
392- struct thread_info *tp;
393-
394- ALL_NON_EXITED_THREADS (tp)
395- if (tp && tp->ptid.pid () == inf->pid)
396- if (target_has_execution_1 (tp->ptid))
397- {
398- /* Found a live thread in this inferior, go to the next
399- inferior. */
400- ++num_inf;
401- break;
402- }
403- }
350+ for (inferior *inf : all_non_exited_inferiors ())
351+ if (target_has_execution_1 (ptid_t (inf->pid)))
352+ for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
353+ {
354+ /* Found a live thread in this inferior, go to the next
355+ inferior. */
356+ ++num_inf;
357+ break;
358+ }
404359
405360 return num_inf;
406361 }
@@ -445,13 +400,8 @@ prune_inferiors (void)
445400 int
446401 number_of_inferiors (void)
447402 {
448- struct inferior *inf;
449- int count = 0;
450-
451- for (inf = inferior_list; inf != NULL; inf = inf->next)
452- count++;
453-
454- return count;
403+ auto rng = all_inferiors ();
404+ return std::distance (rng.begin (), rng.end ());
455405 }
456406
457407 /* Converts an inferior process id to a string. Like
@@ -491,11 +441,10 @@ print_selected_inferior (struct ui_out *uiout)
491441 static void
492442 print_inferior (struct ui_out *uiout, const char *requested_inferiors)
493443 {
494- struct inferior *inf;
495444 int inf_count = 0;
496445
497446 /* Compute number of inferiors we will print. */
498- for (inf = inferior_list; inf; inf = inf->next)
447+ for (inferior *inf : all_inferiors ())
499448 {
500449 if (!number_is_in_list (requested_inferiors, inf->num))
501450 continue;
@@ -516,7 +465,7 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
516465 uiout->table_header (17, ui_left, "exec", "Executable");
517466
518467 uiout->table_body ();
519- for (inf = inferior_list; inf; inf = inf->next)
468+ for (inferior *inf : all_inferiors ())
520469 {
521470 if (!number_is_in_list (requested_inferiors, inf->num))
522471 continue;
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -53,6 +53,7 @@ struct thread_info;
5353 #include "common/refcounted-object.h"
5454
5555 #include "common-inferior.h"
56+#include "gdbthread.h"
5657
5758 struct infcall_suspend_state;
5859 struct infcall_control_state;
@@ -245,17 +246,6 @@ extern int stopped_by_random_signal;
245246 `set print inferior-events'. */
246247 extern int print_inferior_events;
247248
248-/* STEP_OVER_ALL means step over all subroutine calls.
249- STEP_OVER_UNDEBUGGABLE means step over calls to undebuggable functions.
250- STEP_OVER_NONE means don't step over any subroutine calls. */
251-
252-enum step_over_calls_kind
253- {
254- STEP_OVER_NONE,
255- STEP_OVER_ALL,
256- STEP_OVER_UNDEBUGGABLE
257- };
258-
259249 /* Anything but NO_STOP_QUIETLY means we expect a trap and the caller
260250 will handle it themselves. STOP_QUIETLY is used when running in
261251 the shell before the child program has been exec'd and when running
@@ -360,6 +350,38 @@ public:
360350 /* Pointer to next inferior in singly-linked list of inferiors. */
361351 struct inferior *next = NULL;
362352
353+ /* This inferior's thread list. */
354+ thread_info *thread_list = nullptr;
355+
356+ /* Returns a range adapter covering the inferior's threads,
357+ including exited threads. Used like this:
358+
359+ for (thread_info *thr : inf->threads ())
360+ { .... }
361+ */
362+ inf_threads_range threads ()
363+ { return inf_threads_range (this->thread_list); }
364+
365+ /* Returns a range adapter covering the inferior's non-exited
366+ threads. Used like this:
367+
368+ for (thread_info *thr : inf->non_exited_threads ())
369+ { .... }
370+ */
371+ inf_non_exited_threads_range non_exited_threads ()
372+ { return inf_non_exited_threads_range (this->thread_list); }
373+
374+ /* Like inferior::threads(), but returns a range adapter that can be
375+ used with range-for, safely. I.e., it is safe to delete the
376+ currently-iterated thread, like this:
377+
378+ for (thread_info *t : inf->threads_safe ())
379+ if (some_condition ())
380+ delete f;
381+ */
382+ inline safe_inf_threads_range threads_safe ()
383+ { return safe_inf_threads_range (this->thread_list); }
384+
363385 /* Convenient handle (GDB inferior id). Unique across all
364386 inferiors. */
365387 int num = 0;
@@ -575,16 +597,49 @@ private:
575597
576598 /* Traverse all inferiors. */
577599
578-#define ALL_INFERIORS(I) \
579- for ((I) = inferior_list; (I); (I) = (I)->next)
600+extern struct inferior *inferior_list;
580601
581-/* Traverse all non-exited inferiors. */
602+/* Pull in the internals of the inferiors ranges and iterators. Must
603+ be done after struct inferior is defined. */
604+#include "inferior-iter.h"
582605
583-#define ALL_NON_EXITED_INFERIORS(I) \
584- ALL_INFERIORS (I) \
585- if ((I)->pid != 0)
606+/* Return a range that can be used to walk over all inferiors
607+ inferiors, with range-for, safely. I.e., it is safe to delete the
608+ currently-iterated inferior. When combined with range-for, this
609+ allow convenient patterns like this:
586610
587-extern struct inferior *inferior_list;
611+ for (inferior *inf : all_inferiors_safe ())
612+ if (some_condition ())
613+ delete inf;
614+*/
615+
616+inline all_inferiors_safe_range
617+all_inferiors_safe ()
618+{
619+ return {};
620+}
621+
622+/* Returns a range representing all inferiors, suitable to use with
623+ range-for, like this:
624+
625+ for (inferior *inf : all_inferiors ())
626+ [...]
627+*/
628+
629+inline all_inferiors_range
630+all_inferiors ()
631+{
632+ return {};
633+}
634+
635+/* Return a range that can be used to walk over all inferiors with PID
636+ not zero, with range-for. */
637+
638+inline all_non_exited_inferiors_range
639+all_non_exited_inferiors ()
640+{
641+ return {};
642+}
588643
589644 /* Prune away automatically added inferiors that aren't required
590645 anymore. */
--- a/gdb/inflow.c
+++ b/gdb/inflow.c
@@ -546,9 +546,8 @@ void
546546 child_interrupt (struct target_ops *self)
547547 {
548548 /* Interrupt the first inferior that has a resumed thread. */
549- thread_info *thr;
550549 thread_info *resumed = NULL;
551- ALL_NON_EXITED_THREADS (thr)
550+ for (thread_info *thr : all_non_exited_threads ())
552551 {
553552 if (thr->executing)
554553 {
@@ -605,8 +604,7 @@ child_pass_ctrlc (struct target_ops *self)
605604
606605 /* Otherwise, pass the Ctrl-C to the first inferior that was resumed
607606 in the foreground. */
608- inferior *inf;
609- ALL_INFERIORS (inf)
607+ for (inferior *inf : all_inferiors ())
610608 {
611609 if (inf->terminal_state != target_terminal_state::is_ours)
612610 {
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1082,7 +1082,6 @@ show_follow_exec_mode_string (struct ui_file *file, int from_tty,
10821082 static void
10831083 follow_exec (ptid_t ptid, char *exec_file_target)
10841084 {
1085- struct thread_info *th, *tmp;
10861085 struct inferior *inf = current_inferior ();
10871086 int pid = ptid.pid ();
10881087 ptid_t process_ptid;
@@ -1129,7 +1128,7 @@ follow_exec (ptid_t ptid, char *exec_file_target)
11291128 them. Deleting them now rather than at the next user-visible
11301129 stop provides a nicer sequence of events for user and MI
11311130 notifications. */
1132- ALL_THREADS_SAFE (th, tmp)
1131+ for (thread_info *th : all_threads_safe ())
11331132 if (th->ptid.pid () == pid && th->ptid != ptid)
11341133 delete_thread (th);
11351134
@@ -1137,7 +1136,7 @@ follow_exec (ptid_t ptid, char *exec_file_target)
11371136 leader/event thread. E.g., if there was any step-resume
11381137 breakpoint or similar, it's gone now. We cannot truly
11391138 step-to-next statement through an exec(). */
1140- th = inferior_thread ();
1139+ thread_info *th = inferior_thread ();
11411140 th->control.step_resume_breakpoint = NULL;
11421141 th->control.exception_resume_breakpoint = NULL;
11431142 th->control.single_step_breakpoints = NULL;
@@ -2862,21 +2861,14 @@ clear_proceed_status (int step)
28622861 execution_direction))
28632862 target_record_stop_replaying ();
28642863
2865- if (!non_stop)
2864+ if (!non_stop && inferior_ptid != null_ptid)
28662865 {
2867- struct thread_info *tp;
2868- ptid_t resume_ptid;
2869-
2870- resume_ptid = user_visible_resume_ptid (step);
2866+ ptid_t resume_ptid = user_visible_resume_ptid (step);
28712867
28722868 /* In all-stop mode, delete the per-thread status of all threads
28732869 we're about to resume, implicitly and explicitly. */
2874- ALL_NON_EXITED_THREADS (tp)
2875- {
2876- if (!tp->ptid.matches (resume_ptid))
2877- continue;
2878- clear_proceed_status_thread (tp);
2879- }
2870+ for (thread_info *tp : all_non_exited_threads (resume_ptid))
2871+ clear_proceed_status_thread (tp);
28802872 }
28812873
28822874 if (inferior_ptid != null_ptid)
@@ -3079,17 +3071,13 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
30793071 {
30803072 struct thread_info *current = tp;
30813073
3082- ALL_NON_EXITED_THREADS (tp)
3083- {
3074+ for (thread_info *tp : all_non_exited_threads (resume_ptid))
3075+ {
30843076 /* Ignore the current thread here. It's handled
30853077 afterwards. */
30863078 if (tp == current)
30873079 continue;
30883080
3089- /* Ignore threads of processes we're not resuming. */
3090- if (!tp->ptid.matches (resume_ptid))
3091- continue;
3092-
30933081 if (!thread_still_needs_step_over (tp))
30943082 continue;
30953083
@@ -3138,12 +3126,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
31383126 {
31393127 /* In all-stop, but the target is always in non-stop mode.
31403128 Start all other threads that are implicitly resumed too. */
3141- ALL_NON_EXITED_THREADS (tp)
3129+ for (thread_info *tp : all_non_exited_threads (resume_ptid))
31423130 {
3143- /* Ignore threads of processes we're not resuming. */
3144- if (!tp->ptid.matches (resume_ptid))
3145- continue;
3146-
31473131 if (tp->resumed)
31483132 {
31493133 if (debug_infrun)
@@ -3246,9 +3230,6 @@ init_wait_for_inferior (void)
32463230 target_last_wait_ptid = minus_one_ptid;
32473231
32483232 previous_inferior_ptid = inferior_ptid;
3249-
3250- /* Discard any skipped inlined frames. */
3251- clear_inline_frame_state (minus_one_ptid);
32523233 }
32533234
32543235
@@ -3276,53 +3257,50 @@ static int switch_back_to_stepped_thread (struct execution_control_state *ecs);
32763257 static void
32773258 infrun_thread_stop_requested (ptid_t ptid)
32783259 {
3279- struct thread_info *tp;
3280-
32813260 /* PTID was requested to stop. If the thread was already stopped,
32823261 but the user/frontend doesn't know about that yet (e.g., the
32833262 thread had been temporarily paused for some step-over), set up
32843263 for reporting the stop now. */
3285- ALL_NON_EXITED_THREADS (tp)
3286- if (tp->ptid.matches (ptid))
3287- {
3288- if (tp->state != THREAD_RUNNING)
3289- continue;
3290- if (tp->executing)
3291- continue;
3264+ for (thread_info *tp : all_threads (ptid))
3265+ {
3266+ if (tp->state != THREAD_RUNNING)
3267+ continue;
3268+ if (tp->executing)
3269+ continue;
32923270
3293- /* Remove matching threads from the step-over queue, so
3294- start_step_over doesn't try to resume them
3295- automatically. */
3296- if (thread_is_in_step_over_chain (tp))
3297- thread_step_over_chain_remove (tp);
3298-
3299- /* If the thread is stopped, but the user/frontend doesn't
3300- know about that yet, queue a pending event, as if the
3301- thread had just stopped now. Unless the thread already had
3302- a pending event. */
3303- if (!tp->suspend.waitstatus_pending_p)
3304- {
3305- tp->suspend.waitstatus_pending_p = 1;
3306- tp->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED;
3307- tp->suspend.waitstatus.value.sig = GDB_SIGNAL_0;
3308- }
3271+ /* Remove matching threads from the step-over queue, so
3272+ start_step_over doesn't try to resume them
3273+ automatically. */
3274+ if (thread_is_in_step_over_chain (tp))
3275+ thread_step_over_chain_remove (tp);
33093276
3310- /* Clear the inline-frame state, since we're re-processing the
3311- stop. */
3312- clear_inline_frame_state (tp->ptid);
3277+ /* If the thread is stopped, but the user/frontend doesn't
3278+ know about that yet, queue a pending event, as if the
3279+ thread had just stopped now. Unless the thread already had
3280+ a pending event. */
3281+ if (!tp->suspend.waitstatus_pending_p)
3282+ {
3283+ tp->suspend.waitstatus_pending_p = 1;
3284+ tp->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED;
3285+ tp->suspend.waitstatus.value.sig = GDB_SIGNAL_0;
3286+ }
33133287
3314- /* If this thread was paused because some other thread was
3315- doing an inline-step over, let that finish first. Once
3316- that happens, we'll restart all threads and consume pending
3317- stop events then. */
3318- if (step_over_info_valid_p ())
3319- continue;
3288+ /* Clear the inline-frame state, since we're re-processing the
3289+ stop. */
3290+ clear_inline_frame_state (tp->ptid);
33203291
3321- /* Otherwise we can process the (new) pending event now. Set
3322- it so this pending event is considered by
3323- do_target_wait. */
3324- tp->resumed = 1;
3325- }
3292+ /* If this thread was paused because some other thread was
3293+ doing an inline-step over, let that finish first. Once
3294+ that happens, we'll restart all threads and consume pending
3295+ stop events then. */
3296+ if (step_over_info_valid_p ())
3297+ continue;
3298+
3299+ /* Otherwise we can process the (new) pending event now. Set
3300+ it so this pending event is considered by
3301+ do_target_wait. */
3302+ tp->resumed = 1;
3303+ }
33263304 }
33273305
33283306 static void
@@ -3363,13 +3341,9 @@ for_each_just_stopped_thread (for_each_just_stopped_thread_callback_func func)
33633341 }
33643342 else
33653343 {
3366- struct thread_info *tp;
3367-
33683344 /* In all-stop mode, all threads have stopped. */
3369- ALL_NON_EXITED_THREADS (tp)
3370- {
3371- func (tp);
3372- }
3345+ for (thread_info *tp : all_non_exited_threads ())
3346+ func (tp);
33733347 }
33743348 }
33753349
@@ -3438,24 +3412,26 @@ print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
34383412 static struct thread_info *
34393413 random_pending_event_thread (ptid_t waiton_ptid)
34403414 {
3441- struct thread_info *event_tp;
34423415 int num_events = 0;
3443- int random_selector;
3416+
3417+ auto has_event = [] (thread_info *tp)
3418+ {
3419+ return (tp->resumed
3420+ && tp->suspend.waitstatus_pending_p);
3421+ };
34443422
34453423 /* First see how many events we have. Count only resumed threads
34463424 that have an event pending. */
3447- ALL_NON_EXITED_THREADS (event_tp)
3448- if (event_tp->ptid.matches (waiton_ptid)
3449- && event_tp->resumed
3450- && event_tp->suspend.waitstatus_pending_p)
3425+ for (thread_info *tp : all_non_exited_threads (waiton_ptid))
3426+ if (has_event (tp))
34513427 num_events++;
34523428
34533429 if (num_events == 0)
34543430 return NULL;
34553431
34563432 /* Now randomly pick a thread out of those that have had events. */
3457- random_selector = (int)
3458- ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
3433+ int random_selector = (int) ((num_events * (double) rand ())
3434+ / (RAND_MAX + 1.0));
34593435
34603436 if (debug_infrun && num_events > 1)
34613437 fprintf_unfiltered (gdb_stdlog,
@@ -3463,14 +3439,12 @@ random_pending_event_thread (ptid_t waiton_ptid)
34633439 num_events, random_selector);
34643440
34653441 /* Select the Nth thread that has had an event. */
3466- ALL_NON_EXITED_THREADS (event_tp)
3467- if (event_tp->ptid.matches (waiton_ptid)
3468- && event_tp->resumed
3469- && event_tp->suspend.waitstatus_pending_p)
3442+ for (thread_info *tp : all_non_exited_threads (waiton_ptid))
3443+ if (has_event (tp))
34703444 if (random_selector-- == 0)
3471- break;
3445+ return tp;
34723446
3473- return event_tp;
3447+ gdb_assert_not_reached ("event thread not found");
34743448 }
34753449
34763450 /* Wrapper for target_wait that first checks whether threads have
@@ -3766,14 +3740,14 @@ reinstall_readline_callback_handler_cleanup (void *arg)
37663740 static void
37673741 clean_up_just_stopped_threads_fsms (struct execution_control_state *ecs)
37683742 {
3769- struct thread_info *thr = ecs->event_thread;
3770-
3771- if (thr != NULL && thr->thread_fsm != NULL)
3772- thread_fsm_clean_up (thr->thread_fsm, thr);
3743+ if (ecs->event_thread != NULL
3744+ && ecs->event_thread->thread_fsm != NULL)
3745+ thread_fsm_clean_up (ecs->event_thread->thread_fsm,
3746+ ecs->event_thread);
37733747
37743748 if (!non_stop)
37753749 {
3776- ALL_NON_EXITED_THREADS (thr)
3750+ for (thread_info *thr : all_non_exited_threads ())
37773751 {
37783752 if (thr->thread_fsm == NULL)
37793753 continue;
@@ -4472,13 +4446,12 @@ stop_all_threads (void)
44724446 ptid_t event_ptid;
44734447 struct target_waitstatus ws;
44744448 int need_wait = 0;
4475- struct thread_info *t;
44764449
44774450 update_thread_list ();
44784451
44794452 /* Go through all threads looking for threads that we need
44804453 to tell the target to stop. */
4481- ALL_NON_EXITED_THREADS (t)
4454+ for (thread_info *t : all_non_exited_threads ())
44824455 {
44834456 if (t->executing)
44844457 {
@@ -4550,9 +4523,7 @@ stop_all_threads (void)
45504523 }
45514524 else
45524525 {
4553- inferior *inf;
4554-
4555- t = find_thread_ptid (event_ptid);
4526+ thread_info *t = find_thread_ptid (event_ptid);
45564527 if (t == NULL)
45574528 t = add_thread (event_ptid);
45584529
@@ -4563,7 +4534,7 @@ stop_all_threads (void)
45634534
45644535 /* This may be the first time we see the inferior report
45654536 a stop. */
4566- inf = find_inferior_ptid (event_ptid);
4537+ inferior *inf = find_inferior_ptid (event_ptid);
45674538 if (inf->needs_setup)
45684539 {
45694540 switch_to_thread_no_regs (t);
@@ -4653,9 +4624,6 @@ stop_all_threads (void)
46534624 static int
46544625 handle_no_resumed (struct execution_control_state *ecs)
46554626 {
4656- struct inferior *inf;
4657- struct thread_info *thread;
4658-
46594627 if (target_can_async_p ())
46604628 {
46614629 struct ui *ui;
@@ -4718,7 +4686,7 @@ handle_no_resumed (struct execution_control_state *ecs)
47184686 the synchronous command show "no unwaited-for " to the user. */
47194687 update_thread_list ();
47204688
4721- ALL_NON_EXITED_THREADS (thread)
4689+ for (thread_info *thread : all_non_exited_threads ())
47224690 {
47234691 if (thread->executing
47244692 || thread->suspend.waitstatus_pending_p)
@@ -4738,7 +4706,7 @@ handle_no_resumed (struct execution_control_state *ecs)
47384706 process exited meanwhile (thus updating the thread list results
47394707 in an empty thread list). In this case we know we'll be getting
47404708 a process exit event shortly. */
4741- ALL_INFERIORS (inf)
4709+ for (inferior *inf : all_inferiors ())
47424710 {
47434711 if (inf->pid == 0)
47444712 continue;
@@ -5394,12 +5362,10 @@ handle_inferior_event (struct execution_control_state *ecs)
53945362 static void
53955363 restart_threads (struct thread_info *event_thread)
53965364 {
5397- struct thread_info *tp;
5398-
53995365 /* In case the instruction just stepped spawned a new thread. */
54005366 update_thread_list ();
54015367
5402- ALL_NON_EXITED_THREADS (tp)
5368+ for (thread_info *tp : all_non_exited_threads ())
54035369 {
54045370 if (tp == event_thread)
54055371 {
@@ -7007,7 +6973,6 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
70076973 {
70086974 if (!target_is_non_stop_p ())
70096975 {
7010- struct thread_info *tp;
70116976 struct thread_info *stepping_thread;
70126977
70136978 /* If any thread is blocked on some internal breakpoint, and we
@@ -7094,7 +7059,7 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
70947059 /* Look for the stepping/nexting thread. */
70957060 stepping_thread = NULL;
70967061
7097- ALL_NON_EXITED_THREADS (tp)
7062+ for (thread_info *tp : all_non_exited_threads ())
70987063 {
70997064 /* Ignore threads of processes the caller is not
71007065 resuming. */
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -3171,9 +3171,7 @@ linux_nat_filter_event (int lwpid, int status)
31713171 static void
31723172 check_zombie_leaders (void)
31733173 {
3174- struct inferior *inf;
3175-
3176- ALL_INFERIORS (inf)
3174+ for (inferior *inf : all_inferiors ())
31773175 {
31783176 struct lwp_info *leader_lp;
31793177
@@ -3678,28 +3676,25 @@ kill_wait_callback (struct lwp_info *lp, void *data)
36783676 static void
36793677 kill_unfollowed_fork_children (struct inferior *inf)
36803678 {
3681- struct thread_info *thread;
3679+ for (thread_info *thread : inf->non_exited_threads ())
3680+ {
3681+ struct target_waitstatus *ws = &thread->pending_follow;
36823682
3683- ALL_NON_EXITED_THREADS (thread)
3684- if (thread->inf == inf)
3685- {
3686- struct target_waitstatus *ws = &thread->pending_follow;
3687-
3688- if (ws->kind == TARGET_WAITKIND_FORKED
3689- || ws->kind == TARGET_WAITKIND_VFORKED)
3690- {
3691- ptid_t child_ptid = ws->value.related_pid;
3692- int child_pid = child_ptid.pid ();
3693- int child_lwp = child_ptid.lwp ();
3694-
3695- kill_one_lwp (child_lwp);
3696- kill_wait_one_lwp (child_lwp);
3697-
3698- /* Let the arch-specific native code know this process is
3699- gone. */
3700- linux_target->low_forget_process (child_pid);
3701- }
3702- }
3683+ if (ws->kind == TARGET_WAITKIND_FORKED
3684+ || ws->kind == TARGET_WAITKIND_VFORKED)
3685+ {
3686+ ptid_t child_ptid = ws->value.related_pid;
3687+ int child_pid = child_ptid.pid ();
3688+ int child_lwp = child_ptid.lwp ();
3689+
3690+ kill_one_lwp (child_lwp);
3691+ kill_wait_one_lwp (child_lwp);
3692+
3693+ /* Let the arch-specific native code know this process is
3694+ gone. */
3695+ linux_target->low_forget_process (child_pid);
3696+ }
3697+ }
37033698 }
37043699
37053700 void
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1910,7 +1910,7 @@ linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
19101910 struct linux_corefile_thread_data thread_args;
19111911 struct elf_internal_linux_prpsinfo prpsinfo;
19121912 char *note_data = NULL;
1913- struct thread_info *curr_thr, *signalled_thr, *thr;
1913+ struct thread_info *curr_thr, *signalled_thr;
19141914
19151915 if (! gdbarch_iterate_over_regset_sections_p (gdbarch))
19161916 return NULL;
@@ -1959,12 +1959,10 @@ linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
19591959 thread_args.stop_signal = signalled_thr->suspend.stop_signal;
19601960
19611961 linux_corefile_thread (signalled_thr, &thread_args);
1962- ALL_NON_EXITED_THREADS (thr)
1962+ for (thread_info *thr : current_inferior ()->non_exited_threads ())
19631963 {
19641964 if (thr == signalled_thr)
19651965 continue;
1966- if (thr->ptid.pid () != inferior_ptid.pid ())
1967- continue;
19681966
19691967 linux_corefile_thread (thr, &thread_args);
19701968 }
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -1587,11 +1587,10 @@ void
15871587 thread_db_target::update_thread_list ()
15881588 {
15891589 struct thread_db_info *info;
1590- struct inferior *inf;
15911590
15921591 prune_threads ();
15931592
1594- ALL_INFERIORS (inf)
1593+ for (inferior *inf : all_inferiors ())
15951594 {
15961595 struct thread_info *thread;
15971596
@@ -1671,7 +1670,6 @@ thread_db_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
16711670 int handle_len,
16721671 inferior *inf)
16731672 {
1674- struct thread_info *tp;
16751673 thread_t handle_tid;
16761674
16771675 /* Thread handle sizes must match in order to proceed. We don't use an
@@ -1684,11 +1682,11 @@ thread_db_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
16841682
16851683 handle_tid = * (const thread_t *) thread_handle;
16861684
1687- ALL_NON_EXITED_THREADS (tp)
1685+ for (thread_info *tp : inf->non_exited_threads ())
16881686 {
16891687 thread_db_thread_info *priv = get_thread_db_thread_info (tp);
16901688
1691- if (tp->inf == inf && priv != NULL && handle_tid == priv->tid)
1689+ if (priv != NULL && handle_tid == priv->tid)
16921690 return tp;
16931691 }
16941692
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -951,6 +951,24 @@ mi_output_running (struct thread_info *thread)
951951 }
952952 }
953953
954+/* Return true if there are multiple inferiors loaded. This is used
955+ for backwards compatibility -- if there's only one inferior, output
956+ "all", otherwise, output each resumed thread individually. */
957+
958+static bool
959+multiple_inferiors_p ()
960+{
961+ int count = 0;
962+ for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
963+ {
964+ count++;
965+ if (count > 1)
966+ return true;
967+ }
968+
969+ return false;
970+}
971+
954972 static void
955973 mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid)
956974 {
@@ -968,43 +986,15 @@ mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid)
968986 current_token ? current_token : "");
969987 }
970988
971- if (ptid.pid () == -1)
989+ /* Backwards compatibility. If doing a wildcard resume and there's
990+ only one inferior, output "all", otherwise, output each resumed
991+ thread individually. */
992+ if ((ptid == minus_one_ptid || ptid.is_pid ())
993+ && !multiple_inferiors_p ())
972994 fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n");
973- else if (ptid.is_pid ())
974- {
975- int count = 0;
976- inferior *inf;
977-
978- /* Backwards compatibility. If there's only one inferior,
979- output "all", otherwise, output each resumed thread
980- individually. */
981- ALL_INFERIORS (inf)
982- if (inf->pid != 0)
983- {
984- count++;
985- if (count > 1)
986- break;
987- }
988-
989- if (count == 1)
990- fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n");
991- else
992- {
993- thread_info *tp;
994- inferior *curinf = current_inferior ();
995-
996- ALL_NON_EXITED_THREADS (tp)
997- if (tp->inf == curinf)
998- mi_output_running (tp);
999- }
1000- }
1001995 else
1002- {
1003- thread_info *ti = find_thread_ptid (ptid);
1004-
1005- gdb_assert (ti);
1006- mi_output_running (ti);
1007- }
996+ for (thread_info *tp : all_non_exited_threads (ptid))
997+ mi_output_running (tp);
1008998
1009999 if (!running_result_record_printed && mi_proceeded)
10101000 {
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -587,8 +587,7 @@ mi_cmd_thread_list_ids (const char *command, char **argv, int argc)
587587 {
588588 ui_out_emit_tuple tuple_emitter (current_uiout, "thread-ids");
589589
590- struct thread_info *tp;
591- ALL_NON_EXITED_THREADS (tp)
590+ for (thread_info *tp : all_non_exited_threads ())
592591 {
593592 if (tp->ptid == inferior_ptid)
594593 current_thread = tp->global_num;
@@ -1995,7 +1994,7 @@ mi_execute_command (const char *cmd, int from_tty)
19951994 top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ()
19961995 /* Don't try report anything if there are no threads --
19971996 the program is dead. */
1998- && thread_count () != 0
1997+ && any_thread_p ()
19991998 /* If the command already reports the thread change, no need to do it
20001999 again. */
20012000 && !command_notifies_uscc_observer (command.get ()))
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -214,8 +214,6 @@ nto_procfs_target::open (const char *arg, int from_tty)
214214 nto_procfs_node = ND_LOCAL_NODE;
215215 nodestr = (arg != NULL) ? xstrdup (arg) : NULL;
216216
217- init_thread_list ();
218-
219217 if (nodestr)
220218 {
221219 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -379,7 +379,6 @@ record_btrace_target_open (const char *args, int from_tty)
379379 /* If we fail to enable btrace for one thread, disable it for the threads for
380380 which it was successfully enabled. */
381381 scoped_btrace_disable btrace_disable;
382- struct thread_info *tp;
383382
384383 DEBUG ("open");
385384
@@ -388,7 +387,7 @@ record_btrace_target_open (const char *args, int from_tty)
388387 if (!target_has_execution)
389388 error (_("The program is not being run."));
390389
391- ALL_NON_EXITED_THREADS (tp)
390+ for (thread_info *tp : all_non_exited_threads ())
392391 if (args == NULL || *args == 0 || number_is_in_list (args, tp->global_num))
393392 {
394393 btrace_enable (tp, &record_btrace_conf);
@@ -406,13 +405,11 @@ record_btrace_target_open (const char *args, int from_tty)
406405 void
407406 record_btrace_target::stop_recording ()
408407 {
409- struct thread_info *tp;
410-
411408 DEBUG ("stop recording");
412409
413410 record_btrace_auto_disable ();
414411
415- ALL_NON_EXITED_THREADS (tp)
412+ for (thread_info *tp : all_non_exited_threads ())
416413 if (tp->btrace.target != NULL)
417414 btrace_disable (tp);
418415 }
@@ -437,8 +434,6 @@ record_btrace_target::disconnect (const char *args,
437434 void
438435 record_btrace_target::close ()
439436 {
440- struct thread_info *tp;
441-
442437 if (record_btrace_async_inferior_event_handler != NULL)
443438 delete_async_event_handler (&record_btrace_async_inferior_event_handler);
444439
@@ -448,7 +443,7 @@ record_btrace_target::close ()
448443
449444 /* We should have already stopped recording.
450445 Tear down btrace in case we have not. */
451- ALL_NON_EXITED_THREADS (tp)
446+ for (thread_info *tp : all_non_exited_threads ())
452447 btrace_teardown (tp);
453448 }
454449
@@ -1398,10 +1393,8 @@ record_btrace_target::record_method (ptid_t ptid)
13981393 bool
13991394 record_btrace_target::record_is_replaying (ptid_t ptid)
14001395 {
1401- struct thread_info *tp;
1402-
1403- ALL_NON_EXITED_THREADS (tp)
1404- if (tp->ptid.matches (ptid) && btrace_is_replaying (tp))
1396+ for (thread_info *tp : all_non_exited_threads (ptid))
1397+ if (btrace_is_replaying (tp))
14051398 return true;
14061399
14071400 return false;
@@ -2132,7 +2125,6 @@ record_btrace_stop_replaying_at_end (struct thread_info *tp)
21322125 void
21332126 record_btrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
21342127 {
2135- struct thread_info *tp;
21362128 enum btrace_thread_flag flag, cflag;
21372129
21382130 DEBUG ("resume %s: %s%s", target_pid_to_str (ptid),
@@ -2177,20 +2169,18 @@ record_btrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
21772169 {
21782170 gdb_assert (inferior_ptid.matches (ptid));
21792171
2180- ALL_NON_EXITED_THREADS (tp)
2181- if (tp->ptid.matches (ptid))
2182- {
2183- if (tp->ptid.matches (inferior_ptid))
2184- record_btrace_resume_thread (tp, flag);
2185- else
2186- record_btrace_resume_thread (tp, cflag);
2187- }
2172+ for (thread_info *tp : all_non_exited_threads (ptid))
2173+ {
2174+ if (tp->ptid.matches (inferior_ptid))
2175+ record_btrace_resume_thread (tp, flag);
2176+ else
2177+ record_btrace_resume_thread (tp, cflag);
2178+ }
21882179 }
21892180 else
21902181 {
2191- ALL_NON_EXITED_THREADS (tp)
2192- if (tp->ptid.matches (ptid))
2193- record_btrace_resume_thread (tp, flag);
2182+ for (thread_info *tp : all_non_exited_threads (ptid))
2183+ record_btrace_resume_thread (tp, flag);
21942184 }
21952185
21962186 /* Async support. */
@@ -2547,16 +2537,9 @@ record_btrace_target::wait (ptid_t ptid, struct target_waitstatus *status,
25472537 }
25482538
25492539 /* Keep a work list of moving threads. */
2550- {
2551- thread_info *tp;
2552-
2553- ALL_NON_EXITED_THREADS (tp)
2554- {
2555- if (tp->ptid.matches (ptid)
2556- && ((tp->btrace.flags & (BTHR_MOVE | BTHR_STOP)) != 0))
2557- moving.push_back (tp);
2558- }
2559- }
2540+ for (thread_info *tp : all_non_exited_threads (ptid))
2541+ if ((tp->btrace.flags & (BTHR_MOVE | BTHR_STOP)) != 0)
2542+ moving.push_back (tp);
25602543
25612544 if (moving.empty ())
25622545 {
@@ -2637,9 +2620,7 @@ record_btrace_target::wait (ptid_t ptid, struct target_waitstatus *status,
26372620 /* Stop all other threads. */
26382621 if (!target_is_non_stop_p ())
26392622 {
2640- thread_info *tp;
2641-
2642- ALL_NON_EXITED_THREADS (tp)
2623+ for (thread_info *tp : all_non_exited_threads ())
26432624 record_btrace_cancel_resume (tp);
26442625 }
26452626
@@ -2676,14 +2657,11 @@ record_btrace_target::stop (ptid_t ptid)
26762657 }
26772658 else
26782659 {
2679- struct thread_info *tp;
2680-
2681- ALL_NON_EXITED_THREADS (tp)
2682- if (tp->ptid.matches (ptid))
2683- {
2684- tp->btrace.flags &= ~BTHR_MOVE;
2685- tp->btrace.flags |= BTHR_STOP;
2686- }
2660+ for (thread_info *tp : all_non_exited_threads (ptid))
2661+ {
2662+ tp->btrace.flags &= ~BTHR_MOVE;
2663+ tp->btrace.flags |= BTHR_STOP;
2664+ }
26872665 }
26882666 }
26892667
@@ -2876,9 +2854,7 @@ record_btrace_target::goto_record (ULONGEST insn)
28762854 void
28772855 record_btrace_target::record_stop_replaying ()
28782856 {
2879- struct thread_info *tp;
2880-
2881- ALL_NON_EXITED_THREADS (tp)
2857+ for (thread_info *tp : all_non_exited_threads ())
28822858 record_btrace_stop_replaying (tp);
28832859 }
28842860
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -1201,8 +1201,6 @@ record_full_wait_1 (struct target_ops *ops,
12011201
12021202 while (1)
12031203 {
1204- struct thread_info *tp;
1205-
12061204 ret = ops->beneath ()->wait (ptid, status, options);
12071205 if (status->kind == TARGET_WAITKIND_IGNORE)
12081206 {
@@ -1213,7 +1211,7 @@ record_full_wait_1 (struct target_ops *ops,
12131211 return ret;
12141212 }
12151213
1216- ALL_NON_EXITED_THREADS (tp)
1214+ for (thread_info *tp : all_non_exited_threads ())
12171215 delete_single_step_breakpoints (tp);
12181216
12191217 if (record_full_resume_step)
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -1577,9 +1577,6 @@ cooked_read_test (struct gdbarch *gdbarch)
15771577 mock_inferior.aspace = &mock_aspace;
15781578 thread_info mock_thread (&mock_inferior, mock_ptid);
15791579
1580- scoped_restore restore_thread_list
1581- = make_scoped_restore (&thread_list, &mock_thread);
1582-
15831580 /* Add the mock inferior to the inferior list so that look ups by
15841581 target+ptid can find it. */
15851582 scoped_restore restore_inferior_list
--- a/gdb/remote-sim.c
+++ b/gdb/remote-sim.c
@@ -674,9 +674,6 @@ gdbsim_target::create_inferior (const char *exec_file,
674674 built_argv.reset (arg_buf);
675675 }
676676
677- if (!have_inferiors ())
678- init_thread_list ();
679-
680677 if (sim_create_inferior (sim_data->gdbsim_desc, exec_bfd,
681678 built_argv.get (), env)
682679 != SIM_RC_OK)
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -3785,8 +3785,6 @@ remote_target::update_thread_list ()
37853785 || remote_get_threads_with_qthreadinfo (&context)
37863786 || remote_get_threads_with_ql (&context))
37873787 {
3788- struct thread_info *tp, *tmp;
3789-
37903788 got_list = 1;
37913789
37923790 if (context.items.empty ()
@@ -3803,7 +3801,7 @@ remote_target::update_thread_list ()
38033801 /* CONTEXT now holds the current thread list on the remote
38043802 target end. Delete GDB-side threads no longer found on the
38053803 target. */
3806- ALL_THREADS_SAFE (tp, tmp)
3804+ for (thread_info *tp : all_threads_safe ())
38073805 {
38083806 if (!context.contains_thread (tp->ptid))
38093807 {
@@ -4385,8 +4383,6 @@ void
43854383 remote_target::process_initial_stop_replies (int from_tty)
43864384 {
43874385 int pending_stop_replies = stop_reply_queue_length ();
4388- struct inferior *inf;
4389- struct thread_info *thread;
43904386 struct thread_info *selected = NULL;
43914387 struct thread_info *lowest_stopped = NULL;
43924388 struct thread_info *first = NULL;
@@ -4454,16 +4450,13 @@ remote_target::process_initial_stop_replies (int from_tty)
44544450
44554451 /* "Notice" the new inferiors before anything related to
44564452 registers/memory. */
4457- ALL_INFERIORS (inf)
4453+ for (inferior *inf : all_non_exited_inferiors ())
44584454 {
4459- if (inf->pid == 0)
4460- continue;
4461-
44624455 inf->needs_setup = 1;
44634456
44644457 if (non_stop)
44654458 {
4466- thread = any_live_thread_of_inferior (inf);
4459+ thread_info *thread = any_live_thread_of_inferior (inf);
44674460 notice_new_inferior (thread, thread->state == THREAD_RUNNING,
44684461 from_tty);
44694462 }
@@ -4478,14 +4471,11 @@ remote_target::process_initial_stop_replies (int from_tty)
44784471
44794472 /* If all threads of an inferior were already stopped, we
44804473 haven't setup the inferior yet. */
4481- ALL_INFERIORS (inf)
4474+ for (inferior *inf : all_non_exited_inferiors ())
44824475 {
4483- if (inf->pid == 0)
4484- continue;
4485-
44864476 if (inf->needs_setup)
44874477 {
4488- thread = any_live_thread_of_inferior (inf);
4478+ thread_info *thread = any_live_thread_of_inferior (inf);
44894479 switch_to_thread_no_regs (thread);
44904480 setup_inferior (0);
44914481 }
@@ -4495,7 +4485,7 @@ remote_target::process_initial_stop_replies (int from_tty)
44954485 /* Now go over all threads that are stopped, and print their current
44964486 frame. If all-stop, then if there's a signalled thread, pick
44974487 that as current. */
4498- ALL_NON_EXITED_THREADS (thread)
4488+ for (thread_info *thread : all_non_exited_threads ())
44994489 {
45004490 if (first == NULL)
45014491 first = thread;
@@ -4522,7 +4512,7 @@ remote_target::process_initial_stop_replies (int from_tty)
45224512 others with their status pending. */
45234513 if (!non_stop)
45244514 {
4525- thread = selected;
4515+ thread_info *thread = selected;
45264516 if (thread == NULL)
45274517 thread = lowest_stopped;
45284518 if (thread == NULL)
@@ -4532,7 +4522,7 @@ remote_target::process_initial_stop_replies (int from_tty)
45324522 }
45334523
45344524 /* For "info program". */
4535- thread = inferior_thread ();
4525+ thread_info *thread = inferior_thread ();
45364526 if (thread->state == THREAD_STOPPED)
45374527 set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
45384528 }
@@ -4731,7 +4721,7 @@ remote_target::start_remote (int from_tty, int extended_p)
47314721 "warning: couldn't determine remote "
47324722 "current thread; picking first in list.\n");
47334723
4734- inferior_ptid = thread_list->ptid;
4724+ inferior_ptid = inferior_list->thread_list->ptid;
47354725 }
47364726 }
47374727
@@ -5613,9 +5603,6 @@ remote_target::open_1 (const char *name, int from_tty, int extended_p)
56135603 /* First delete any symbols previously loaded from shared libraries. */
56145604 no_shared_libraries (NULL, 0);
56155605
5616- /* Start afresh. */
5617- init_thread_list ();
5618-
56195606 /* Start the remote connection. If error() or QUIT, discard this
56205607 target (we'd otherwise be in an inconsistent state) and then
56215608 propogate the error on up the exception chain. This ensures that
@@ -6113,11 +6100,8 @@ char *
61136100 remote_target::append_pending_thread_resumptions (char *p, char *endp,
61146101 ptid_t ptid)
61156102 {
6116- struct thread_info *thread;
6117-
6118- ALL_NON_EXITED_THREADS (thread)
6119- if (thread->ptid.matches (ptid)
6120- && inferior_ptid != thread->ptid
6103+ for (thread_info *thread : all_non_exited_threads (ptid))
6104+ if (inferior_ptid != thread->ptid
61216105 && thread->suspend.stop_signal != GDB_SIGNAL_0)
61226106 {
61236107 p = append_resumption (p, endp, thread->ptid,
@@ -6137,7 +6121,6 @@ remote_target::remote_resume_with_hc (ptid_t ptid, int step,
61376121 gdb_signal siggnal)
61386122 {
61396123 struct remote_state *rs = get_remote_state ();
6140- struct thread_info *thread;
61416124 char *buf;
61426125
61436126 rs->last_sent_signal = siggnal;
@@ -6150,7 +6133,7 @@ remote_target::remote_resume_with_hc (ptid_t ptid, int step,
61506133 else
61516134 set_continue_thread (ptid);
61526135
6153- ALL_NON_EXITED_THREADS (thread)
6136+ for (thread_info *thread : all_non_exited_threads ())
61546137 resume_clear_thread_private_info (thread);
61556138
61566139 buf = rs->buf;
@@ -6458,8 +6441,6 @@ vcont_builder::push_action (ptid_t ptid, bool step, gdb_signal siggnal)
64586441 void
64596442 remote_target::commit_resume ()
64606443 {
6461- struct inferior *inf;
6462- struct thread_info *tp;
64636444 int any_process_wildcard;
64646445 int may_global_wildcard_vcont;
64656446
@@ -6522,7 +6503,7 @@ remote_target::commit_resume ()
65226503 may_global_wildcard_vcont = 1;
65236504
65246505 /* And assume every process is individually wildcard-able too. */
6525- ALL_NON_EXITED_INFERIORS (inf)
6506+ for (inferior *inf : all_non_exited_inferiors ())
65266507 {
65276508 remote_inferior *priv = get_remote_inferior (inf);
65286509
@@ -6533,7 +6514,7 @@ remote_target::commit_resume ()
65336514 disable process and global wildcard resumes appropriately. */
65346515 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont);
65356516
6536- ALL_NON_EXITED_THREADS (tp)
6517+ for (thread_info *tp : all_non_exited_threads ())
65376518 {
65386519 /* If a thread of a process is not meant to be resumed, then we
65396520 can't wildcard that process. */
@@ -6562,7 +6543,7 @@ remote_target::commit_resume ()
65626543 struct vcont_builder vcont_builder (this);
65636544
65646545 /* Threads first. */
6565- ALL_NON_EXITED_THREADS (tp)
6546+ for (thread_info *tp : all_non_exited_threads ())
65666547 {
65676548 remote_thread_info *remote_thr = get_remote_thread_info (tp);
65686549
@@ -6591,7 +6572,7 @@ remote_target::commit_resume ()
65916572 supposed to be resumed. */
65926573 any_process_wildcard = 0;
65936574
6594- ALL_NON_EXITED_INFERIORS (inf)
6575+ for (inferior *inf : all_non_exited_inferiors ())
65956576 {
65966577 if (get_remote_inferior (inf)->may_wildcard_vcont)
65976578 {
@@ -6612,7 +6593,7 @@ remote_target::commit_resume ()
66126593 }
66136594 else
66146595 {
6615- ALL_NON_EXITED_INFERIORS (inf)
6596+ for (inferior *inf : all_non_exited_inferiors ())
66166597 {
66176598 if (get_remote_inferior (inf)->may_wildcard_vcont)
66186599 {
@@ -7019,13 +7000,12 @@ is_pending_fork_parent_thread (struct thread_info *thread)
70197000 void
70207001 remote_target::remove_new_fork_children (threads_listing_context *context)
70217002 {
7022- struct thread_info * thread;
70237003 int pid = -1;
70247004 struct notif_client *notif = &notif_client_stop;
70257005
70267006 /* For any threads stopped at a fork event, remove the corresponding
70277007 fork child threads from the CONTEXT list. */
7028- ALL_NON_EXITED_THREADS (thread)
7008+ for (thread_info *thread : all_non_exited_threads ())
70297009 {
70307010 struct target_waitstatus *ws = thread_pending_fork_status (thread);
70317011
@@ -9717,12 +9697,11 @@ void
97179697 remote_target::kill_new_fork_children (int pid)
97189698 {
97199699 remote_state *rs = get_remote_state ();
9720- struct thread_info *thread;
97219700 struct notif_client *notif = &notif_client_stop;
97229701
97239702 /* Kill the fork child threads of any threads in process PID
97249703 that are stopped at a fork event. */
9725- ALL_NON_EXITED_THREADS (thread)
9704+ for (thread_info *thread : all_non_exited_threads ())
97269705 {
97279706 struct target_waitstatus *ws = &thread->pending_follow;
97289707
@@ -10164,15 +10143,6 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
1016410143 extended_remote_restart ();
1016510144 }
1016610145
10167- if (!have_inferiors ())
10168- {
10169- /* Clean up from the last time we ran, before we mark the target
10170- running again. This will mark breakpoints uninserted, and
10171- get_offsets may insert breakpoints. */
10172- init_thread_list ();
10173- init_wait_for_inferior ();
10174- }
10175-
1017610146 /* vRun's success return is a stop reply. */
1017710147 stop_reply = run_worked ? rs->buf : NULL;
1017810148 add_current_inferior_and_thread (stop_reply);
@@ -13781,7 +13751,6 @@ void
1378113751 remote_target::remote_btrace_maybe_reopen ()
1378213752 {
1378313753 struct remote_state *rs = get_remote_state ();
13784- struct thread_info *tp;
1378513754 int btrace_target_pushed = 0;
1378613755 #if !defined (HAVE_LIBIPT)
1378713756 int warned = 0;
@@ -13789,7 +13758,7 @@ remote_target::remote_btrace_maybe_reopen ()
1378913758
1379013759 scoped_restore_current_thread restore_thread;
1379113760
13792- ALL_NON_EXITED_THREADS (tp)
13761+ for (thread_info *tp : all_non_exited_threads ())
1379313762 {
1379413763 set_general_thread (tp->ptid);
1379513764
@@ -14073,9 +14042,7 @@ remote_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
1407314042 int handle_len,
1407414043 inferior *inf)
1407514044 {
14076- struct thread_info *tp;
14077-
14078- ALL_NON_EXITED_THREADS (tp)
14045+ for (thread_info *tp : all_non_exited_threads ())
1407914046 {
1408014047 remote_thread_info *priv = get_remote_thread_info (tp);
1408114048
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -473,9 +473,8 @@ target_terminal::restore_inferior (void)
473473
474474 {
475475 scoped_restore_current_inferior restore_inferior;
476- struct inferior *inf;
477476
478- ALL_INFERIORS (inf)
477+ for (struct inferior *inf : all_inferiors ())
479478 {
480479 if (inf->terminal_state == target_terminal_state::is_ours_for_output)
481480 {
@@ -501,14 +500,13 @@ static void
501500 target_terminal_is_ours_kind (target_terminal_state desired_state)
502501 {
503502 scoped_restore_current_inferior restore_inferior;
504- struct inferior *inf;
505503
506504 /* Must do this in two passes. First, have all inferiors save the
507505 current terminal settings. Then, after all inferiors have add a
508506 chance to safely save the terminal settings, restore GDB's
509507 terminal settings. */
510508
511- ALL_INFERIORS (inf)
509+ for (inferior *inf : all_inferiors ())
512510 {
513511 if (inf->terminal_state == target_terminal_state::is_inferior)
514512 {
@@ -517,7 +515,7 @@ target_terminal_is_ours_kind (target_terminal_state desired_state)
517515 }
518516 }
519517
520- ALL_INFERIORS (inf)
518+ for (inferior *inf : all_inferiors ())
521519 {
522520 /* Note we don't check is_inferior here like above because we
523521 need to handle 'is_ours_for_output -> is_ours' too. Careful
--- /dev/null
+++ b/gdb/thread-iter.c
@@ -0,0 +1,101 @@
1+/* Thread iterators and ranges for GDB, the GNU debugger.
2+
3+ Copyright (C) 2018 Free Software Foundation, Inc.
4+
5+ This file is part of GDB.
6+
7+ This program is free software; you can redistribute it and/or modify
8+ it under the terms of the GNU General Public License as published by
9+ the Free Software Foundation; either version 3 of the License, or
10+ (at your option) any later version.
11+
12+ This program is distributed in the hope that it will be useful,
13+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ GNU General Public License for more details.
16+
17+ You should have received a copy of the GNU General Public License
18+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
19+
20+#include "defs.h"
21+#include "gdbthread.h"
22+#include "inferior.h"
23+
24+/* See thread-iter.h. */
25+
26+all_threads_iterator::all_threads_iterator (begin_t)
27+{
28+ /* Advance M_INF/M_THR to the first thread's position. */
29+ for (m_inf = inferior_list; m_inf != NULL; m_inf = m_inf->next)
30+ if ((m_thr = m_inf->thread_list) != NULL)
31+ return;
32+}
33+
34+/* See thread-iter.h. */
35+
36+void
37+all_threads_iterator::advance ()
38+{
39+ /* The loop below is written in the natural way as-if we'd always
40+ start at the beginning of the inferior list. This fast forwards
41+ the algorithm to the actual current position. */
42+ goto start;
43+
44+ for (; m_inf != NULL; m_inf = m_inf->next)
45+ {
46+ m_thr = m_inf->thread_list;
47+ while (m_thr != NULL)
48+ {
49+ return;
50+ start:
51+ m_thr = m_thr->next;
52+ }
53+ }
54+}
55+
56+/* See thread-iter.h. */
57+
58+bool
59+all_matching_threads_iterator::m_inf_matches ()
60+{
61+ return (m_filter_ptid == minus_one_ptid
62+ || m_filter_ptid.pid () == m_inf->pid);
63+}
64+
65+/* See thread-iter.h. */
66+
67+all_matching_threads_iterator::all_matching_threads_iterator
68+ (ptid_t filter_ptid)
69+ : m_filter_ptid (filter_ptid)
70+{
71+ m_thr = nullptr;
72+ for (m_inf = inferior_list; m_inf != NULL; m_inf = m_inf->next)
73+ if (m_inf_matches ())
74+ for (m_thr = m_inf->thread_list; m_thr != NULL; m_thr = m_thr->next)
75+ if (m_thr->ptid.matches (m_filter_ptid))
76+ return;
77+}
78+
79+/* See thread-iter.h. */
80+
81+void
82+all_matching_threads_iterator::advance ()
83+{
84+ /* The loop below is written in the natural way as-if we'd always
85+ start at the beginning of the inferior list. This fast forwards
86+ the algorithm to the actual current position. */
87+ goto start;
88+
89+ for (; m_inf != NULL; m_inf = m_inf->next)
90+ if (m_inf_matches ())
91+ {
92+ m_thr = m_inf->thread_list;
93+ while (m_thr != NULL)
94+ {
95+ if (m_thr->ptid.matches (m_filter_ptid))
96+ return;
97+ start:
98+ m_thr = m_thr->next;
99+ }
100+ }
101+}
--- /dev/null
+++ b/gdb/thread-iter.h
@@ -0,0 +1,311 @@
1+/* Thread iterators and ranges for GDB, the GNU debugger.
2+ Copyright (C) 2018 Free Software Foundation, Inc.
3+
4+ This file is part of GDB.
5+
6+ This program is free software; you can redistribute it and/or modify
7+ it under the terms of the GNU General Public License as published by
8+ the Free Software Foundation; either version 3 of the License, or
9+ (at your option) any later version.
10+
11+ This program is distributed in the hope that it will be useful,
12+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ GNU General Public License for more details.
15+
16+ You should have received a copy of the GNU General Public License
17+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
18+
19+#ifndef THREAD_ITER_H
20+#define THREAD_ITER_H
21+
22+#include "common/filtered-iterator.h"
23+#include "common/safe-iterator.h"
24+
25+/* A forward iterator that iterates over a given inferior's
26+ threads. */
27+
28+class inf_threads_iterator
29+{
30+public:
31+ typedef inf_threads_iterator self_type;
32+ typedef struct thread_info *value_type;
33+ typedef struct thread_info *&reference;
34+ typedef struct thread_info **pointer;
35+ typedef std::forward_iterator_tag iterator_category;
36+ typedef int difference_type;
37+
38+ /* Create an iterator pointing at HEAD. This takes a thread pointer
39+ instead of an inferior pointer to avoid circular dependencies
40+ between the thread and inferior header files. */
41+ explicit inf_threads_iterator (struct thread_info *head)
42+ : m_thr (head)
43+ {}
44+
45+ /* Create a one-past-end iterator. */
46+ inf_threads_iterator ()
47+ : m_thr (nullptr)
48+ {}
49+
50+ inf_threads_iterator& operator++ ()
51+ {
52+ m_thr = m_thr->next;
53+ return *this;
54+ }
55+
56+ thread_info *operator* () const { return m_thr; }
57+
58+ bool operator!= (const inf_threads_iterator &other) const
59+ { return m_thr != other.m_thr; }
60+
61+private:
62+ /* The currently-iterated thread. NULL if we reached the end of the
63+ list. */
64+ thread_info *m_thr;
65+};
66+
67+/* A range adapter that makes it possible to iterate over an
68+ inferior's thread list with range-for. */
69+template<typename Iterator>
70+struct basic_inf_threads_range
71+{
72+ friend struct inferior;
73+private:
74+ explicit basic_inf_threads_range (struct thread_info *head)
75+ : m_head (head)
76+ {}
77+
78+public:
79+ Iterator begin () const { return Iterator (m_head); }
80+ Iterator end () const { return Iterator (); }
81+
82+private:
83+ thread_info *m_head;
84+};
85+
86+/* A forward iterator that iterates over all threads of all
87+ inferiors. */
88+
89+class all_threads_iterator
90+{
91+public:
92+ typedef all_threads_iterator self_type;
93+ typedef struct thread_info *value_type;
94+ typedef struct thread_info *&reference;
95+ typedef struct thread_info **pointer;
96+ typedef std::forward_iterator_tag iterator_category;
97+ typedef int difference_type;
98+
99+ /* Tag type. */
100+ struct begin_t {};
101+
102+ /* Create an iterator that points to the first thread of the first
103+ inferior. */
104+ explicit all_threads_iterator (begin_t);
105+
106+ /* Create a one-past-end iterator. */
107+ all_threads_iterator ()
108+ : m_thr (nullptr)
109+ {}
110+
111+ thread_info *operator* () const { return m_thr; }
112+
113+ all_threads_iterator &operator++ ()
114+ {
115+ advance ();
116+ return *this;
117+ }
118+
119+ bool operator== (const all_threads_iterator &other) const
120+ { return m_thr == other.m_thr; }
121+
122+ bool operator!= (const all_threads_iterator &other) const
123+ { return m_thr != other.m_thr; }
124+
125+private:
126+ /* Advance to the next thread. */
127+ void advance ();
128+
129+private:
130+ /* The current inferior and thread. M_THR is NULL if we reached the
131+ end of the threads list of the last inferior. */
132+ inferior *m_inf;
133+ thread_info *m_thr;
134+};
135+
136+/* Iterate over all threads that match a given PTID. */
137+
138+class all_matching_threads_iterator
139+{
140+public:
141+ typedef all_matching_threads_iterator self_type;
142+ typedef struct thread_info *value_type;
143+ typedef struct thread_info *&reference;
144+ typedef struct thread_info **pointer;
145+ typedef std::forward_iterator_tag iterator_category;
146+ typedef int difference_type;
147+
148+ /* Creates an iterator that iterates over all threads that match
149+ FILTER_PTID. */
150+ explicit all_matching_threads_iterator (ptid_t filter_ptid);
151+
152+ /* Create a one-past-end iterator. */
153+ all_matching_threads_iterator ()
154+ : m_inf (nullptr),
155+ m_thr (nullptr),
156+ m_filter_ptid (minus_one_ptid)
157+ {}
158+
159+ thread_info *operator* () const { return m_thr; }
160+
161+ all_matching_threads_iterator &operator++ ()
162+ {
163+ advance ();
164+ return *this;
165+ }
166+
167+ bool operator== (const all_matching_threads_iterator &other) const
168+ { return m_thr == other.m_thr; }
169+
170+ bool operator!= (const all_matching_threads_iterator &other) const
171+ { return m_thr != other.m_thr; }
172+
173+private:
174+ /* Advance to next thread, skipping filtered threads. */
175+ void advance ();
176+
177+ /* True if M_INF matches the process identified by
178+ M_FILTER_PTID. */
179+ bool m_inf_matches ();
180+
181+private:
182+ /* The current inferior. */
183+ inferior *m_inf;
184+
185+ /* The current thread. */
186+ thread_info *m_thr;
187+
188+ /* The filter. */
189+ ptid_t m_filter_ptid;
190+};
191+
192+/* Filter for filtered_iterator. Filters out exited threads. */
193+
194+struct non_exited_thread_filter
195+{
196+ bool operator() (struct thread_info *thr) const
197+ {
198+ return thr->state != THREAD_EXITED;
199+ }
200+};
201+
202+/* Iterate over all non-exited threads that match a given PTID. */
203+
204+using all_non_exited_threads_iterator
205+ = filtered_iterator<all_matching_threads_iterator, non_exited_thread_filter>;
206+
207+/* Iterate over all non-exited threads of an inferior. */
208+
209+using inf_non_exited_threads_iterator
210+ = filtered_iterator<inf_threads_iterator, non_exited_thread_filter>;
211+
212+/* Iterate over all threads of all inferiors, safely. */
213+
214+using all_threads_safe_iterator
215+ = basic_safe_iterator<all_threads_iterator>;
216+
217+/* Iterate over all threads of an inferior, safely. */
218+
219+using safe_inf_threads_iterator
220+ = basic_safe_iterator<inf_threads_iterator>;
221+
222+/* A range adapter that makes it possible to iterate over all threads
223+ of an inferior with range-for. */
224+
225+using inf_threads_range
226+ = basic_inf_threads_range<inf_threads_iterator>;
227+
228+/* A range adapter that makes it possible to iterate over all
229+ non-exited threads of an inferior with range-for. */
230+
231+using inf_non_exited_threads_range
232+ = basic_inf_threads_range<inf_non_exited_threads_iterator>;
233+
234+/* A range adapter that makes it possible to iterate over all threads
235+ of an inferior with range-for, safely. */
236+
237+using safe_inf_threads_range
238+ = basic_inf_threads_range<safe_inf_threads_iterator>;
239+
240+/* A range adapter that makes it possible to iterate over all threads
241+ of all inferiors with range-for. */
242+
243+struct all_threads_range
244+{
245+ all_threads_iterator begin () const
246+ { return all_threads_iterator (all_threads_iterator::begin_t {}); }
247+ all_threads_iterator end () const
248+ { return all_threads_iterator (); }
249+};
250+
251+/* A range adapter that makes it possible to iterate over all threads
252+ with range-for "safely". I.e., it is safe to delete the
253+ currently-iterated thread. */
254+
255+struct all_threads_safe_range
256+{
257+ all_threads_safe_iterator begin () const
258+ { return all_threads_safe_iterator (all_threads_iterator::begin_t {}); }
259+ all_threads_safe_iterator end () const
260+ { return all_threads_safe_iterator (); }
261+};
262+
263+/* A range adapter that makes it possible to iterate over all threads
264+ that match a PTID filter with range-for. */
265+
266+struct all_matching_threads_range
267+{
268+public:
269+ explicit all_matching_threads_range (ptid_t filter_ptid)
270+ : m_filter_ptid (filter_ptid)
271+ {}
272+ all_matching_threads_range ()
273+ : m_filter_ptid (minus_one_ptid)
274+ {}
275+
276+ all_matching_threads_iterator begin () const
277+ { return all_matching_threads_iterator (m_filter_ptid); }
278+ all_matching_threads_iterator end () const
279+ { return all_matching_threads_iterator (); }
280+
281+private:
282+ /* The filter. */
283+ ptid_t m_filter_ptid;
284+};
285+
286+/* A range adapter that makes it possible to iterate over all
287+ non-exited threads of all inferiors, with range-for.
288+ Threads/inferiors that do not match FILTER_PTID are filtered
289+ out. */
290+
291+class all_non_exited_threads_range
292+{
293+public:
294+ explicit all_non_exited_threads_range (ptid_t filter_ptid)
295+ : m_filter_ptid (filter_ptid)
296+ {}
297+
298+ all_non_exited_threads_range ()
299+ : m_filter_ptid (minus_one_ptid)
300+ {}
301+
302+ all_non_exited_threads_iterator begin () const
303+ { return all_non_exited_threads_iterator (m_filter_ptid); }
304+ all_non_exited_threads_iterator end () const
305+ { return all_non_exited_threads_iterator (); }
306+
307+private:
308+ ptid_t m_filter_ptid;
309+};
310+
311+#endif /* THREAD_ITER_H */
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -45,12 +45,12 @@
4545 #include "tid-parse.h"
4646 #include <algorithm>
4747 #include "common/gdb_optional.h"
48+#include "inline-frame.h"
4849
4950 /* Definition of struct thread_info exported to gdbthread.h. */
5051
5152 /* Prototypes for local functions. */
5253
53-struct thread_info *thread_list = NULL;
5454 static int highest_thread_num;
5555
5656 /* True if any thread is, or may be executing. We need to track this
@@ -194,6 +194,8 @@ clear_thread_inferior_resources (struct thread_info *tp)
194194 btrace_teardown (tp);
195195
196196 thread_cancel_execution_command (tp);
197+
198+ clear_inline_frame_state (tp->ptid);
197199 }
198200
199201 /* Set the TP's state as exited. */
@@ -220,20 +222,19 @@ set_thread_exited (thread_info *tp, int silent)
220222 void
221223 init_thread_list (void)
222224 {
223- struct thread_info *tp, *tmp;
224-
225225 highest_thread_num = 0;
226226
227- ALL_THREADS_SAFE (tp, tmp)
227+ for (thread_info *tp : all_threads_safe ())
228228 {
229+ inferior *inf = tp->inf;
230+
229231 if (tp->deletable ())
230232 delete tp;
231233 else
232234 set_thread_exited (tp, 1);
233- }
234235
235- thread_list = NULL;
236- threads_executing = 0;
236+ inf->thread_list = NULL;
237+ }
237238 }
238239
239240 /* Allocate a new thread of inferior INF with target id PTID and add
@@ -244,13 +245,13 @@ new_thread (struct inferior *inf, ptid_t ptid)
244245 {
245246 thread_info *tp = new thread_info (inf, ptid);
246247
247- if (thread_list == NULL)
248- thread_list = tp;
248+ if (inf->thread_list == NULL)
249+ inf->thread_list = tp;
249250 else
250251 {
251252 struct thread_info *last;
252253
253- for (last = thread_list; last->next != NULL; last = last->next)
254+ for (last = inf->thread_list; last->next != NULL; last = last->next)
254255 ;
255256 last->next = tp;
256257 }
@@ -261,11 +262,10 @@ new_thread (struct inferior *inf, ptid_t ptid)
261262 struct thread_info *
262263 add_thread_silent (ptid_t ptid)
263264 {
264- struct thread_info *tp;
265265 struct inferior *inf = find_inferior_ptid (ptid);
266266 gdb_assert (inf != NULL);
267267
268- tp = find_thread_ptid (ptid);
268+ thread_info *tp = find_thread_ptid (inf, ptid);
269269 if (tp)
270270 /* Found an old thread with the same id. It has to be dead,
271271 otherwise we wouldn't be adding a new thread with the same id.
@@ -352,6 +352,16 @@ thread_info::~thread_info ()
352352 xfree (this->name);
353353 }
354354
355+/* See gdbthread.h. */
356+
357+bool
358+thread_info::deletable () const
359+{
360+ /* If this is the current thread, or there's code out there that
361+ relies on it existing (refcount > 0) we can't delete yet. */
362+ return refcount () == 0 && ptid != inferior_ptid;
363+}
364+
355365 /* Add TP to the end of the step-over chain LIST_P. */
356366
357367 static void
@@ -442,7 +452,7 @@ delete_thread_1 (thread_info *thr, bool silent)
442452
443453 tpprev = NULL;
444454
445- for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
455+ for (tp = thr->inf->thread_list; tp; tpprev = tp, tp = tp->next)
446456 if (tp == thr)
447457 break;
448458
@@ -460,7 +470,7 @@ delete_thread_1 (thread_info *thr, bool silent)
460470 if (tpprev)
461471 tpprev->next = tp->next;
462472 else
463- thread_list = tp->next;
473+ tp->inf->thread_list = tp->next;
464474
465475 delete tp;
466476 }
@@ -485,9 +495,7 @@ delete_thread_silent (thread_info *thread)
485495 struct thread_info *
486496 find_thread_global_id (int global_id)
487497 {
488- struct thread_info *tp;
489-
490- for (tp = thread_list; tp; tp = tp->next)
498+ for (thread_info *tp : all_threads ())
491499 if (tp->global_num == global_id)
492500 return tp;
493501
@@ -497,10 +505,8 @@ find_thread_global_id (int global_id)
497505 static struct thread_info *
498506 find_thread_id (struct inferior *inf, int thr_num)
499507 {
500- struct thread_info *tp;
501-
502- for (tp = thread_list; tp; tp = tp->next)
503- if (tp->inf == inf && tp->per_inf_num == thr_num)
508+ for (thread_info *tp : inf->threads ())
509+ if (tp->per_inf_num == thr_num)
504510 return tp;
505511
506512 return NULL;
@@ -511,9 +517,18 @@ find_thread_id (struct inferior *inf, int thr_num)
511517 struct thread_info *
512518 find_thread_ptid (ptid_t ptid)
513519 {
514- struct thread_info *tp;
520+ inferior *inf = find_inferior_ptid (ptid);
521+ if (inf == NULL)
522+ return NULL;
523+ return find_thread_ptid (inf, ptid);
524+}
515525
516- for (tp = thread_list; tp; tp = tp->next)
526+/* See gdbthread.h. */
527+
528+struct thread_info *
529+find_thread_ptid (inferior *inf, ptid_t ptid)
530+{
531+ for (thread_info *tp : inf->threads ())
517532 if (tp->ptid == ptid)
518533 return tp;
519534
@@ -549,28 +564,28 @@ struct thread_info *
549564 iterate_over_threads (int (*callback) (struct thread_info *, void *),
550565 void *data)
551566 {
552- struct thread_info *tp, *next;
553-
554- for (tp = thread_list; tp; tp = next)
555- {
556- next = tp->next;
557- if ((*callback) (tp, data))
558- return tp;
559- }
567+ for (thread_info *tp : all_threads_safe ())
568+ if ((*callback) (tp, data))
569+ return tp;
560570
561571 return NULL;
562572 }
563573
574+/* See gdbthread.h. */
575+
576+bool
577+any_thread_p ()
578+{
579+ for (thread_info *tp ATTRIBUTE_UNUSED : all_threads ())
580+ return true;
581+ return false;
582+}
583+
564584 int
565585 thread_count (void)
566586 {
567- int result = 0;
568- struct thread_info *tp;
569-
570- for (tp = thread_list; tp; tp = tp->next)
571- ++result;
572-
573- return result;
587+ auto rng = all_threads ();
588+ return std::distance (rng.begin (), rng.end ());
574589 }
575590
576591 /* Return the number of non-exited threads in the thread list. */
@@ -578,21 +593,14 @@ thread_count (void)
578593 static int
579594 live_threads_count (void)
580595 {
581- int result = 0;
582- struct thread_info *tp;
583-
584- ALL_NON_EXITED_THREADS (tp)
585- ++result;
586-
587- return result;
596+ auto rng = all_non_exited_threads ();
597+ return std::distance (rng.begin (), rng.end ());
588598 }
589599
590600 int
591601 valid_global_thread_id (int global_id)
592602 {
593- struct thread_info *tp;
594-
595- for (tp = thread_list; tp; tp = tp->next)
603+ for (thread_info *tp : all_threads ())
596604 if (tp->global_num == global_id)
597605 return 1;
598606
@@ -602,13 +610,7 @@ valid_global_thread_id (int global_id)
602610 int
603611 in_thread_list (ptid_t ptid)
604612 {
605- struct thread_info *tp;
606-
607- for (tp = thread_list; tp; tp = tp->next)
608- if (tp->ptid == ptid)
609- return 1;
610-
611- return 0; /* Never heard of 'im. */
613+ return find_thread_ptid (ptid) != nullptr;
612614 }
613615
614616 /* Finds the first thread of the inferior. */
@@ -616,30 +618,20 @@ in_thread_list (ptid_t ptid)
616618 thread_info *
617619 first_thread_of_inferior (inferior *inf)
618620 {
619- struct thread_info *tp, *ret = NULL;
620-
621- for (tp = thread_list; tp; tp = tp->next)
622- if (tp->inf == inf)
623- if (ret == NULL || tp->global_num < ret->global_num)
624- ret = tp;
625-
626- return ret;
621+ return inf->thread_list;
627622 }
628623
629624 thread_info *
630625 any_thread_of_inferior (inferior *inf)
631626 {
632- struct thread_info *tp;
633-
634627 gdb_assert (inf->pid != 0);
635628
636629 /* Prefer the current thread. */
637630 if (inf == current_inferior ())
638631 return inferior_thread ();
639632
640- ALL_NON_EXITED_THREADS (tp)
641- if (tp->inf == inf)
642- return tp;
633+ for (thread_info *tp : inf->non_exited_threads ())
634+ return tp;
643635
644636 return NULL;
645637 }
@@ -648,7 +640,6 @@ thread_info *
648640 any_live_thread_of_inferior (inferior *inf)
649641 {
650642 struct thread_info *curr_tp = NULL;
651- struct thread_info *tp;
652643 struct thread_info *tp_executing = NULL;
653644
654645 gdb_assert (inf != NULL && inf->pid != 0);
@@ -666,14 +657,13 @@ any_live_thread_of_inferior (inferior *inf)
666657 return curr_tp;
667658 }
668659
669- ALL_NON_EXITED_THREADS (tp)
670- if (tp->inf == inf)
671- {
672- if (!tp->executing)
673- return tp;
660+ for (thread_info *tp : inf->non_exited_threads ())
661+ {
662+ if (!tp->executing)
663+ return tp;
674664
675- tp_executing = tp;
676- }
665+ tp_executing = tp;
666+ }
677667
678668 /* If both the current thread and all live threads are executing,
679669 prefer the current thread. */
@@ -700,13 +690,9 @@ thread_alive (struct thread_info *tp)
700690 void
701691 prune_threads (void)
702692 {
703- struct thread_info *tp, *tmp;
704-
705- ALL_THREADS_SAFE (tp, tmp)
706- {
707- if (!thread_alive (tp))
708- delete_thread (tp);
709- }
693+ for (thread_info *tp : all_threads_safe ())
694+ if (!thread_alive (tp))
695+ delete_thread (tp);
710696 }
711697
712698 /* See gdbthreads.h. */
@@ -714,13 +700,9 @@ prune_threads (void)
714700 void
715701 delete_exited_threads (void)
716702 {
717- struct thread_info *tp, *tmp;
718-
719- ALL_THREADS_SAFE (tp, tmp)
720- {
721- if (tp->state == THREAD_EXITED)
722- delete_thread (tp);
723- }
703+ for (thread_info *tp : all_threads_safe ())
704+ if (tp->state == THREAD_EXITED)
705+ delete_thread (tp);
724706 }
725707
726708 /* Return true value if stack temporaies are enabled for the thread
@@ -785,7 +767,7 @@ thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
785767 inf = find_inferior_ptid (old_ptid);
786768 inf->pid = new_ptid.pid ();
787769
788- tp = find_thread_ptid (old_ptid);
770+ tp = find_thread_ptid (inf, old_ptid);
789771 tp->ptid = new_ptid;
790772
791773 gdb::observers::thread_ptid_changed.notify (old_ptid, new_ptid);
@@ -796,21 +778,8 @@ thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
796778 void
797779 set_resumed (ptid_t ptid, int resumed)
798780 {
799- struct thread_info *tp;
800- int all = ptid == minus_one_ptid;
801-
802- if (all || ptid.is_pid ())
803- {
804- for (tp = thread_list; tp; tp = tp->next)
805- if (all || tp->ptid.pid () == ptid.pid ())
806- tp->resumed = resumed;
807- }
808- else
809- {
810- tp = find_thread_ptid (ptid);
811- gdb_assert (tp != NULL);
812- tp->resumed = resumed;
813- }
781+ for (thread_info *tp : all_non_exited_threads (ptid))
782+ tp->resumed = resumed;
814783 }
815784
816785 /* Helper for set_running, that marks one thread either running or
@@ -849,74 +818,20 @@ thread_info::set_running (bool running)
849818 void
850819 set_running (ptid_t ptid, int running)
851820 {
852- struct thread_info *tp;
853- int all = ptid == minus_one_ptid;
854- int any_started = 0;
821+ /* We try not to notify the observer if no thread has actually
822+ changed the running state -- merely to reduce the number of
823+ messages to the MI frontend. A frontend is supposed to handle
824+ multiple *running notifications just fine. */
825+ bool any_started = false;
855826
856- /* We try not to notify the observer if no thread has actually changed
857- the running state -- merely to reduce the number of messages to
858- frontend. Frontend is supposed to handle multiple *running just fine. */
859- if (all || ptid.is_pid ())
860- {
861- for (tp = thread_list; tp; tp = tp->next)
862- if (all || tp->ptid.pid () == ptid.pid ())
863- {
864- if (tp->state == THREAD_EXITED)
865- continue;
827+ for (thread_info *tp : all_non_exited_threads (ptid))
828+ if (set_running_thread (tp, running))
829+ any_started = true;
866830
867- if (set_running_thread (tp, running))
868- any_started = 1;
869- }
870- }
871- else
872- {
873- tp = find_thread_ptid (ptid);
874- gdb_assert (tp != NULL);
875- gdb_assert (tp->state != THREAD_EXITED);
876- if (set_running_thread (tp, running))
877- any_started = 1;
878- }
879831 if (any_started)
880832 gdb::observers::target_resumed.notify (ptid);
881833 }
882834
883-static int
884-is_thread_state (ptid_t ptid, enum thread_state state)
885-{
886- struct thread_info *tp;
887-
888- tp = find_thread_ptid (ptid);
889- gdb_assert (tp);
890- return tp->state == state;
891-}
892-
893-int
894-is_stopped (ptid_t ptid)
895-{
896- return is_thread_state (ptid, THREAD_STOPPED);
897-}
898-
899-int
900-is_exited (ptid_t ptid)
901-{
902- return is_thread_state (ptid, THREAD_EXITED);
903-}
904-
905-int
906-is_running (ptid_t ptid)
907-{
908- return is_thread_state (ptid, THREAD_RUNNING);
909-}
910-
911-int
912-is_executing (ptid_t ptid)
913-{
914- struct thread_info *tp;
915-
916- tp = find_thread_ptid (ptid);
917- gdb_assert (tp);
918- return tp->executing;
919-}
920835
921836 /* Helper for set_executing. Set's the thread's 'executing' field
922837 from EXECUTING, and if EXECUTING is true also clears the thread's
@@ -933,23 +848,10 @@ set_executing_thread (thread_info *thr, bool executing)
933848 void
934849 set_executing (ptid_t ptid, int executing)
935850 {
936- struct thread_info *tp;
937- int all = ptid == minus_one_ptid;
851+ for (thread_info *tp : all_non_exited_threads (ptid))
852+ set_executing_thread (tp, executing);
938853
939- if (all || ptid.is_pid ())
940- {
941- for (tp = thread_list; tp; tp = tp->next)
942- if (all || tp->ptid.pid () == ptid.pid ())
943- set_executing_thread (tp, executing);
944- }
945- else
946- {
947- tp = find_thread_ptid (ptid);
948- gdb_assert (tp);
949- set_executing_thread (tp, executing);
950- }
951-
952- /* It only takes one running thread to spawn more threads.*/
854+ /* It only takes one running thread to spawn more threads. */
953855 if (executing)
954856 threads_executing = 1;
955857 /* Only clear the flag if the caller is telling us everything is
@@ -969,21 +871,8 @@ threads_are_executing (void)
969871 void
970872 set_stop_requested (ptid_t ptid, int stop)
971873 {
972- struct thread_info *tp;
973- int all = ptid == minus_one_ptid;
974-
975- if (all || ptid.is_pid ())
976- {
977- for (tp = thread_list; tp; tp = tp->next)
978- if (all || tp->ptid.pid () == ptid.pid ())
979- tp->stop_requested = stop;
980- }
981- else
982- {
983- tp = find_thread_ptid (ptid);
984- gdb_assert (tp);
985- tp->stop_requested = stop;
986- }
874+ for (thread_info *tp : all_non_exited_threads (ptid))
875+ tp->stop_requested = stop;
987876
988877 /* Call the stop requested observer so other components of GDB can
989878 react to this request. */
@@ -994,35 +883,11 @@ set_stop_requested (ptid_t ptid, int stop)
994883 void
995884 finish_thread_state (ptid_t ptid)
996885 {
997- struct thread_info *tp;
998- int all;
999- int any_started = 0;
886+ bool any_started = false;
1000887
1001- all = ptid == minus_one_ptid;
1002-
1003- if (all || ptid.is_pid ())
1004- {
1005- for (tp = thread_list; tp; tp = tp->next)
1006- {
1007- if (tp->state == THREAD_EXITED)
1008- continue;
1009- if (all || ptid.pid () == tp->ptid.pid ())
1010- {
1011- if (set_running_thread (tp, tp->executing))
1012- any_started = 1;
1013- }
1014- }
1015- }
1016- else
1017- {
1018- tp = find_thread_ptid (ptid);
1019- gdb_assert (tp);
1020- if (tp->state != THREAD_EXITED)
1021- {
1022- if (set_running_thread (tp, tp->executing))
1023- any_started = 1;
1024- }
1025- }
888+ for (thread_info *tp : all_non_exited_threads (ptid))
889+ if (set_running_thread (tp, tp->executing))
890+ any_started = true;
1026891
1027892 if (any_started)
1028893 gdb::observers::target_resumed.notify (ptid);
@@ -1148,8 +1013,6 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
11481013 int global_ids, int pid,
11491014 int show_global_ids)
11501015 {
1151- struct thread_info *tp;
1152- struct inferior *inf;
11531016 int default_inf_num = current_inferior ()->num;
11541017
11551018 update_thread_list ();
@@ -1178,7 +1041,7 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
11781041 accommodate the largest entry. */
11791042 size_t target_id_col_width = 17;
11801043
1181- ALL_THREADS (tp)
1044+ for (thread_info *tp : all_threads ())
11821045 {
11831046 if (!should_print_thread (requested_threads, default_inf_num,
11841047 global_ids, pid, tp))
@@ -1220,7 +1083,8 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
12201083 /* We'll be switching threads temporarily. */
12211084 scoped_restore_current_thread restore_thread;
12221085
1223- ALL_THREADS_BY_INFERIOR (inf, tp)
1086+ for (inferior *inf : all_inferiors ())
1087+ for (thread_info *tp : inf->threads ())
12241088 {
12251089 int core;
12261090
@@ -1667,16 +1531,9 @@ thread_apply_all_command (const char *cmd, int from_tty)
16671531 std::vector<thread_info *> thr_list_cpy;
16681532 thr_list_cpy.reserve (tc);
16691533
1670- {
1671- thread_info *tp;
1672-
1673- ALL_NON_EXITED_THREADS (tp)
1674- {
1675- thr_list_cpy.push_back (tp);
1676- }
1677-
1678- gdb_assert (thr_list_cpy.size () == tc);
1679- }
1534+ for (thread_info *tp : all_non_exited_threads ())
1535+ thr_list_cpy.push_back (tp);
1536+ gdb_assert (thr_list_cpy.size () == tc);
16801537
16811538 /* Increment the refcounts, and restore them back on scope
16821539 exit. */
@@ -1869,7 +1726,6 @@ thread_name_command (const char *arg, int from_tty)
18691726 static void
18701727 thread_find_command (const char *arg, int from_tty)
18711728 {
1872- struct thread_info *tp;
18731729 const char *tmp;
18741730 unsigned long match = 0;
18751731
@@ -1881,7 +1737,7 @@ thread_find_command (const char *arg, int from_tty)
18811737 error (_("Invalid regexp (%s): %s"), tmp, arg);
18821738
18831739 update_thread_list ();
1884- for (tp = thread_list; tp; tp = tp->next)
1740+ for (thread_info *tp : all_threads ())
18851741 {
18861742 if (tp->name != NULL && re_exec (tp->name))
18871743 {
@@ -1993,10 +1849,8 @@ print_selected_thread_frame (struct ui_out *uiout,
19931849 static void
19941850 update_threads_executing (void)
19951851 {
1996- struct thread_info *tp;
1997-
19981852 threads_executing = 0;
1999- ALL_NON_EXITED_THREADS (tp)
1853+ for (thread_info *tp : all_non_exited_threads ())
20001854 {
20011855 if (tp->executing)
20021856 {
--- a/gdb/tid-parse.c
+++ b/gdb/tid-parse.c
@@ -55,7 +55,6 @@ parse_thread_id (const char *tidstr, const char **end)
5555 {
5656 const char *number = tidstr;
5757 const char *dot, *p1;
58- struct thread_info *tp;
5958 struct inferior *inf;
6059 int thr_num;
6160 int explicit_inf_id = 0;
@@ -90,12 +89,13 @@ parse_thread_id (const char *tidstr, const char **end)
9089 if (thr_num == 0)
9190 invalid_thread_id_error (number);
9291
93- ALL_THREADS (tp)
94- {
95- if (tp->ptid.pid () == inf->pid
96- && tp->per_inf_num == thr_num)
92+ thread_info *tp = nullptr;
93+ for (thread_info *it : inf->threads ())
94+ if (it->per_inf_num == thr_num)
95+ {
96+ tp = it;
9797 break;
98- }
98+ }
9999
100100 if (tp == NULL)
101101 {
--- a/gdb/x86-bsd-nat.c
+++ b/gdb/x86-bsd-nat.c
@@ -67,7 +67,6 @@ x86bsd_dr_get (ptid_t ptid, int regnum)
6767 static void
6868 x86bsd_dr_set (int regnum, unsigned long value)
6969 {
70- struct thread_info *thread;
7170 struct dbreg dbregs;
7271
7372 if (ptrace (PT_GETDBREGS, get_ptrace_pid (inferior_ptid),
@@ -81,13 +80,12 @@ x86bsd_dr_set (int regnum, unsigned long value)
8180
8281 DBREG_DRX ((&dbregs), regnum) = value;
8382
84- ALL_NON_EXITED_THREADS (thread)
85- if (thread->inf == current_inferior ())
86- {
87- if (ptrace (PT_SETDBREGS, get_ptrace_pid (thread->ptid),
88- (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
89- perror_with_name (_("Couldn't write debug registers"));
90- }
83+ for (thread_info *thread : current_inferior ()->non_exited_threads ())
84+ {
85+ if (ptrace (PT_SETDBREGS, get_ptrace_pid (thread->ptid),
86+ (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
87+ perror_with_name (_("Couldn't write debug registers"));
88+ }
9189 }
9290
9391 static void