summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2018-11-22 16:09:14 +0000
committerPedro Alves <palves@redhat.com>2018-11-22 16:13:23 +0000
commit080363310650c93ad8e93018bcb6760ba5d32d1c (patch)
treefc6ebb4711aa27416bfdcf580e98be748f74d22b
parentFix follow_exec latent problem (diff)
downloadbinutils-gdb-080363310650c93ad8e93018bcb6760ba5d32d1c.tar.gz
binutils-gdb-080363310650c93ad8e93018bcb6760ba5d32d1c.tar.bz2
binutils-gdb-080363310650c93ad8e93018bcb6760ba5d32d1c.tar.xz
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: 2018-11-22 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 ().
-rw-r--r--gdb/ChangeLog168
-rw-r--r--gdb/Makefile.in1
-rw-r--r--gdb/breakpoint.c7
-rw-r--r--gdb/bsd-kvm.c3
-rw-r--r--gdb/btrace.c4
-rw-r--r--gdb/common/filtered-iterator.h87
-rw-r--r--gdb/common/safe-iterator.h93
-rw-r--r--gdb/corelow.c6
-rw-r--r--gdb/darwin-nat.c10
-rw-r--r--gdb/fbsd-nat.c21
-rw-r--r--gdb/fbsd-tdep.c6
-rw-r--r--gdb/fork-child.c7
-rw-r--r--gdb/gdbarch-selftests.c2
-rw-r--r--gdb/gdbthread.h94
-rw-r--r--gdb/infcmd.c69
-rw-r--r--gdb/inferior-iter.h117
-rw-r--r--gdb/inferior.c135
-rw-r--r--gdb/inferior.h91
-rw-r--r--gdb/inflow.c6
-rw-r--r--gdb/infrun.c222
-rw-r--r--gdb/linux-nat.c43
-rw-r--r--gdb/linux-tdep.c6
-rw-r--r--gdb/linux-thread-db.c8
-rw-r--r--gdb/mi/mi-interp.c60
-rw-r--r--gdb/mi/mi-main.c5
-rw-r--r--gdb/nto-procfs.c2
-rw-r--r--gdb/record-btrace.c72
-rw-r--r--gdb/record-full.c4
-rw-r--r--gdb/regcache.c3
-rw-r--r--gdb/remote-sim.c3
-rw-r--r--gdb/remote.c77
-rw-r--r--gdb/target.c8
-rw-r--r--gdb/thread-iter.c101
-rw-r--r--gdb/thread-iter.h311
-rw-r--r--gdb/thread.c346
-rw-r--r--gdb/tid-parse.c12
-rw-r--r--gdb/x86-bsd-nat.c14
37 files changed, 1403 insertions, 821 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 4887e57..0b8651c 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,173 @@
12018-11-22 Pedro Alves <palves@redhat.com> 12018-11-22 Pedro Alves <palves@redhat.com>
2 2
3 * Makefile.in (COMMON_SFILES): Add thread-iter.c.
4 * breakpoint.c (breakpoints_should_be_inserted_now): Replace
5 ALL_NON_EXITED_THREADS with all_non_exited_threads.
6 (print_one_breakpoint_location): Replace ALL_INFERIORS with
7 all_inferiors.
8 * bsd-kvm.c: Include inferior.h.
9 * btrace.c (btrace_free_objfile): Replace ALL_NON_EXITED_THREADS
10 with all_non_exited_threads.
11 * common/filtered-iterator.h: New.
12 * common/safe-iterator.h: New.
13 * corelow.c (core_target_open): Don't call init_thread_list here.
14 * darwin-nat.c (thread_info_from_private_thread_info): Replace
15 ALL_THREADS with all_threads.
16 * fbsd-nat.c (fbsd_nat_target::resume): Replace
17 ALL_NON_EXITED_THREADS with inf->non_exited_threads.
18 * fbsd-tdep.c (fbsd_make_corefile_notes): Replace
19 ALL_NON_EXITED_THREADS with inf->non_exited_threads.
20 * fork-child.c (postfork_hook): Don't call init_thread_list here.
21 * gdbarch-selftests.c (register_to_value_test): Adjust.
22 * gdbthread.h: Don't include "inferior.h" here.
23 (struct inferior): Forward declare.
24 (enum step_over_calls_kind): Moved here from inferior.h.
25 (thread_info::deletable): Definition moved to thread.c.
26 (find_thread_ptid (inferior *, ptid_t)): Declare.
27 (ALL_THREADS, ALL_THREADS_BY_INFERIOR, ALL_THREADS_SAFE): Delete.
28 Include "thread-iter.h".
29 (all_threads, all_non_exited_threads, all_threads_safe): New.
30 (any_thread_p): Declare.
31 (thread_list): Delete.
32 * infcmd.c (signal_command): Replace ALL_NON_EXITED_THREADS with
33 all_non_exited_threads.
34 (proceed_after_attach_callback): Delete.
35 (proceed_after_attach): Take an inferior pointer instead of an
36 integer PID. Adjust to use range-for.
37 (attach_post_wait): Pass down inferior pointer instead of pid.
38 Use range-for instead of ALL_NON_EXITED_THREADS.
39 (detach_command): Remove init_thread_list call.
40 * inferior-iter.h: New.
41 * inferior.c (struct delete_thread_of_inferior_arg): Delete.
42 (delete_thread_of_inferior): Delete.
43 (delete_inferior, exit_inferior_1): Use range-for with
44 inf->threads_safe() instead of iterate_over_threads.
45 (inferior_appeared): Call init_thread_list here.
46 (discard_all_inferiors): Use all_non_exited_inferiors.
47 (find_inferior_id, find_inferior_pid): Use all_inferiors.
48 (iterate_over_inferiors): Use all_inferiors_safe.
49 (have_inferiors, number_of_live_inferiors): Use
50 all_non_exited_inferiors.
51 (number_of_inferiors): Use all_inferiors and std::distance.
52 (print_inferior): Use all_inferiors.
53 * inferior.h: Include gdbthread.h.
54 (enum step_over_calls_kind): Moved to gdbthread.h.
55 (struct inferior) <thread_list>: New field.
56 <threads, non_exited_threads, threads_safe>: New methods.
57 (ALL_INFERIORS): Delete.
58 Include "inferior-iter.h".
59 (ALL_NON_EXITED_INFERIORS): Delete.
60 (all_inferiors_safe, all_inferiors, all_non_exited_inferiors): New
61 functions.
62 * inflow.c (child_interrupt, child_pass_ctrlc): Replace
63 ALL_NON_EXITED_THREADS with all_non_exited_threads.
64 * infrun.c (follow_exec): Use all_threads_safe.
65 (clear_proceed_status, proceed): Use all_non_exited_threads.
66 (init_wait_for_inferior): Don't clear inline frame state here.
67 (infrun_thread_stop_requested, for_each_just_stopped_thread): Use
68 all_threads instead of ALL_NON_EXITED_THREADS.
69 (random_pending_event_thread): Use all_non_exited_threads instead
70 of ALL_NON_EXITED_THREADS. Use a lambda for repeated code.
71 (clean_up_just_stopped_threads_fsms): Use all_non_exited_threads
72 instead of ALL_NON_EXITED_THREADS.
73 (handle_no_resumed): Use all_non_exited_threads instead of
74 ALL_NON_EXITED_THREADS. Use all_inferiors instead of
75 ALL_INFERIORS.
76 (restart_threads, switch_back_to_stepped_thread): Use
77 all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
78 * linux-nat.c (check_zombie_leaders): Replace ALL_INFERIORS with
79 all_inferiors.
80 (kill_unfollowed_fork_children): Use inf->non_exited_threads
81 instead of ALL_NON_EXITED_THREADS.
82 * linux-tdep.c (linux_make_corefile_notes): Use
83 inf->non_exited_threads instead of ALL_NON_EXITED_THREADS.
84 * linux-thread-db.c (thread_db_target::update_thread_list):
85 Replace ALL_INFERIORS with all_inferiors.
86 (thread_db_target::thread_handle_to_thread_info): Use
87 inf->non_exited_threads instead of ALL_NON_EXITED_THREADS.
88 * mi/mi-interp.c (multiple_inferiors_p): New.
89 (mi_on_resume_1): Simplify using all_non_exited_threads and
90 multiple_inferiors_p.
91 * mi/mi-main.c (mi_cmd_thread_list_ids): Use all_non_exited_threads
92 instead of ALL_NON_EXITED_THREADS.
93 * nto-procfs.c (nto_procfs_target::open): Don't call
94 init_thread_list here.
95 * record-btrace.c (record_btrace_target_open)
96 (record_btrace_target::stop_recording)
97 (record_btrace_target::close)
98 (record_btrace_target::record_is_replaying)
99 (record_btrace_target::resume, record_btrace_target::wait)
100 (record_btrace_target::record_stop_replaying): Use
101 all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
102 * record-full.c (record_full_wait_1): Use all_non_exited_threads
103 instead of ALL_NON_EXITED_THREADS.
104 * regcache.c (cooked_read_test): Remove reference to global
105 thread_list.
106 * remote-sim.c (gdbsim_target::create_inferior): Don't call
107 init_thread_list here.
108 * remote.c (remote_target::update_thread_list): Use
109 all_threads_safe instead of ALL_NON_EXITED_THREADS.
110 (remote_target::process_initial_stop_replies): Replace
111 ALL_INFERIORS with all_non_exited_inferiors and use
112 all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
113 (remote_target::open_1): Don't call init_thread_list here.
114 (remote_target::append_pending_thread_resumptions)
115 (remote_target::remote_resume_with_hc): Use all_non_exited_threads
116 instead of ALL_NON_EXITED_THREADS.
117 (remote_target::commit_resume)
118 (remote_target::remove_new_fork_children): Replace ALL_INFERIORS
119 with all_non_exited_inferiors and use all_non_exited_threads
120 instead of ALL_NON_EXITED_THREADS.
121 (remote_target::kill_new_fork_children): Use
122 all_non_exited_threads instead of ALL_NON_EXITED_THREADS. Remove
123 init_thread_list and init_wait_for_inferior calls.
124 (remote_target::remote_btrace_maybe_reopen)
125 (remote_target::thread_handle_to_thread_info): Use
126 all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
127 * target.c (target_terminal::restore_inferior)
128 (target_terminal_is_ours_kind): Replace ALL_INFERIORS with
129 all_non_exited_inferiors.
130 * thread-iter.c: New file.
131 * thread-iter.h: New file.
132 * thread.c: Include "inline-frame.h".
133 (thread_list): Delete.
134 (clear_thread_inferior_resources): Call clear_inline_frame_state.
135 (init_thread_list): Use all_threads_safe instead of
136 ALL_THREADS_SAFE. Adjust to per-inferior thread lists.
137 (new_thread): Adjust to per-inferior thread lists.
138 (add_thread_silent): Pass inferior to find_thread_ptid.
139 (thread_info::deletable): New, moved from the header.
140 (delete_thread_1): Adjust to per-inferior thread lists.
141 (find_thread_global_id): Use inf->threads().
142 (find_thread_ptid): Use find_inferior_ptid and pass inferior to
143 find_thread_ptid.
144 (find_thread_ptid(inferior*, ptid_t)): New overload.
145 (iterate_over_threads): Use all_threads_safe.
146 (any_thread_p): New.
147 (thread_count): Use all_threads and std::distance.
148 (live_threads_count): Use all_non_exited_threads and
149 std::distance.
150 (valid_global_thread_id): Use all_threads.
151 (in_thread_list): Use find_thread_ptid.
152 (first_thread_of_inferior): Adjust to per-inferior thread lists.
153 (any_thread_of_inferior, any_live_thread_of_inferior): Use
154 inf->non_exited_threads().
155 (prune_threads, delete_exited_threads): Use all_threads_safe.
156 (thread_change_ptid): Pass inferior pointer to find_thread_ptid.
157 (set_resumed, set_running): Use all_non_exited_threads.
158 (is_thread_state, is_stopped, is_exited, is_running)
159 (is_executing): Delete.
160 (set_executing, set_stop_requested, finish_thread_state): Use
161 all_non_exited_threads.
162 (print_thread_info_1): Use all_inferiors and all_threads.
163 (thread_apply_all_command): Use all_non_exited_threads.
164 (thread_find_command): Use all_threads.
165 (update_threads_executing): Use all_non_exited_threads.
166 * tid-parse.c (parse_thread_id): Use inf->threads.
167 * x86-bsd-nat.c (x86bsd_dr_set): Use inf->non_exited_threads ().
168
1692018-11-22 Pedro Alves <palves@redhat.com>
170
3 * infrun.c (follow_exec) <set follow-exec new>: Add thread and 171 * infrun.c (follow_exec) <set follow-exec new>: Add thread and
4 switch to it before calling into try_open_exec_file. 172 switch to it before calling into try_open_exec_file.
5 173
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 4001bcb..3be058f 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1111,6 +1111,7 @@ COMMON_SFILES = \
1111 target-descriptions.c \ 1111 target-descriptions.c \
1112 target-memory.c \ 1112 target-memory.c \
1113 thread.c \ 1113 thread.c \
1114 thread-iter.c \
1114 thread-fsm.c \ 1115 thread-fsm.c \
1115 tid-parse.c \ 1116 tid-parse.c \
1116 top.c \ 1117 top.c \
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 6bd456e..8af3d54 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -400,8 +400,6 @@ breakpoints_should_be_inserted_now (void)
400 } 400 }
401 else if (target_has_execution) 401 else if (target_has_execution)
402 { 402 {
403 struct thread_info *tp;
404
405 if (always_inserted_mode) 403 if (always_inserted_mode)
406 { 404 {
407 /* The user wants breakpoints inserted even if all threads 405 /* The user wants breakpoints inserted even if all threads
@@ -414,7 +412,7 @@ breakpoints_should_be_inserted_now (void)
414 412
415 /* Don't remove breakpoints yet if, even though all threads are 413 /* Don't remove breakpoints yet if, even though all threads are
416 stopped, we still have events to process. */ 414 stopped, we still have events to process. */
417 ALL_NON_EXITED_THREADS (tp) 415 for (thread_info *tp : all_non_exited_threads ())
418 if (tp->resumed 416 if (tp->resumed
419 && tp->suspend.waitstatus_pending_p) 417 && tp->suspend.waitstatus_pending_p)
420 return 1; 418 return 1;
@@ -6149,11 +6147,10 @@ print_one_breakpoint_location (struct breakpoint *b,
6149 6147
6150 if (loc != NULL && !header_of_multiple) 6148 if (loc != NULL && !header_of_multiple)
6151 { 6149 {
6152 struct inferior *inf;
6153 std::vector<int> inf_nums; 6150 std::vector<int> inf_nums;
6154 int mi_only = 1; 6151 int mi_only = 1;
6155 6152
6156 ALL_INFERIORS (inf) 6153 for (inferior *inf : all_inferiors ())
6157 { 6154 {
6158 if (inf->pspace == loc->pspace) 6155 if (inf->pspace == loc->pspace)
6159 inf_nums.push_back (inf->num); 6156 inf_nums.push_back (inf->num);
diff --git a/gdb/bsd-kvm.c b/gdb/bsd-kvm.c
index 078cd30..af8305f 100644
--- a/gdb/bsd-kvm.c
+++ b/gdb/bsd-kvm.c
@@ -25,7 +25,8 @@
25#include "regcache.h" 25#include "regcache.h"
26#include "target.h" 26#include "target.h"
27#include "value.h" 27#include "value.h"
28#include "gdbcore.h" /* for get_exec_file */ 28#include "gdbcore.h"
29#include "inferior.h" /* for get_exec_file */
29#include "gdbthread.h" 30#include "gdbthread.h"
30 31
31#include <fcntl.h> 32#include <fcntl.h>
diff --git a/gdb/btrace.c b/gdb/btrace.c
index d3ad0ab..7d75d22 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -2003,11 +2003,9 @@ btrace_clear (struct thread_info *tp)
2003void 2003void
2004btrace_free_objfile (struct objfile *objfile) 2004btrace_free_objfile (struct objfile *objfile)
2005{ 2005{
2006 struct thread_info *tp;
2007
2008 DEBUG ("free objfile"); 2006 DEBUG ("free objfile");
2009 2007
2010 ALL_NON_EXITED_THREADS (tp) 2008 for (thread_info *tp : all_non_exited_threads ())
2011 btrace_clear (tp); 2009 btrace_clear (tp);
2012} 2010}
2013 2011
diff --git a/gdb/common/filtered-iterator.h b/gdb/common/filtered-iterator.h
new file mode 100644
index 0000000..fe1c20b
--- /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
27template<typename BaseIterator, typename FilterFunc>
28class filtered_iterator
29{
30public:
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
72private:
73
74 void skip_filtered ()
75 {
76 for (; m_it != m_end; ++m_it)
77 if (m_filter (*m_it))
78 break;
79 }
80
81private:
82 FilterFunc m_filter {};
83 BaseIterator m_it {};
84 BaseIterator m_end {};
85};
86
87#endif /* FILTERED_ITERATOR_H */
diff --git a/gdb/common/safe-iterator.h b/gdb/common/safe-iterator.h
new file mode 100644
index 0000000..4210766
--- /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
40template<typename Iterator>
41class basic_safe_iterator
42{
43public:
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
82private:
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 */
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 8e9ac9a..72f2807 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -421,12 +421,6 @@ core_target_open (const char *arg, int from_tty)
421 push_target (target); 421 push_target (target);
422 target_holder.release (); 422 target_holder.release ();
423 423
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
430 inferior_ptid = null_ptid; 424 inferior_ptid = null_ptid;
431 425
432 /* Need to flush the register cache (and the frame cache) from a 426 /* Need to flush the register cache (and the frame cache) from a
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index f985c0c..84312f0 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1707,19 +1707,15 @@ darwin_attach_pid (struct inferior *inf)
1707static struct thread_info * 1707static struct thread_info *
1708thread_info_from_private_thread_info (darwin_thread_info *pti) 1708thread_info_from_private_thread_info (darwin_thread_info *pti)
1709{ 1709{
1710 struct thread_info *it; 1710 for (struct thread_info *it : all_threads ())
1711
1712 ALL_THREADS (it)
1713 { 1711 {
1714 darwin_thread_info *iter_pti = get_darwin_thread_info (it); 1712 darwin_thread_info *iter_pti = get_darwin_thread_info (it);
1715 1713
1716 if (iter_pti->gdb_port == pti->gdb_port) 1714 if (iter_pti->gdb_port == pti->gdb_port)
1717 break; 1715 return it;
1718 } 1716 }
1719 1717
1720 gdb_assert (it != NULL); 1718 gdb_assert_not_reached ("did not find gdb thread for darwin thread");
1721
1722 return it;
1723} 1719}
1724 1720
1725static void 1721static void
diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index 107a729..d11e990 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -1158,13 +1158,11 @@ fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
1158 if (ptid.lwp_p ()) 1158 if (ptid.lwp_p ())
1159 { 1159 {
1160 /* If ptid is a specific LWP, suspend all other LWPs in the process. */ 1160 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
1161 struct thread_info *tp; 1161 inferior *inf = find_inferior_ptid (ptid);
1162 int request;
1163 1162
1164 ALL_NON_EXITED_THREADS (tp) 1163 for (thread_info *tp : inf->non_exited_threads ())
1165 { 1164 {
1166 if (tp->ptid.pid () != ptid.pid ()) 1165 int request;
1167 continue;
1168 1166
1169 if (tp->ptid.lwp () == ptid.lwp ()) 1167 if (tp->ptid.lwp () == ptid.lwp ())
1170 request = PT_RESUME; 1168 request = PT_RESUME;
@@ -1179,16 +1177,9 @@ fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
1179 { 1177 {
1180 /* If ptid is a wildcard, resume all matching threads (they won't run 1178 /* If ptid is a wildcard, resume all matching threads (they won't run
1181 until the process is continued however). */ 1179 until the process is continued however). */
1182 struct thread_info *tp; 1180 for (thread_info *tp : all_non_exited_threads (ptid))
1183 1181 if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1)
1184 ALL_NON_EXITED_THREADS (tp) 1182 perror_with_name (("ptrace"));
1185 {
1186 if (!tp->ptid.matches (ptid))
1187 continue;
1188
1189 if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1)
1190 perror_with_name (("ptrace"));
1191 }
1192 ptid = inferior_ptid; 1183 ptid = inferior_ptid;
1193 } 1184 }
1194 1185
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index db9a217..be6ae86 100644
--- 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)
653 struct fbsd_corefile_thread_data thread_args; 653 struct fbsd_corefile_thread_data thread_args;
654 char *note_data = NULL; 654 char *note_data = NULL;
655 Elf_Internal_Ehdr *i_ehdrp; 655 Elf_Internal_Ehdr *i_ehdrp;
656 struct thread_info *curr_thr, *signalled_thr, *thr; 656 struct thread_info *curr_thr, *signalled_thr;
657 657
658 /* Put a "FreeBSD" label in the ELF header. */ 658 /* Put a "FreeBSD" label in the ELF header. */
659 i_ehdrp = elf_elfheader (obfd); 659 i_ehdrp = elf_elfheader (obfd);
@@ -706,12 +706,10 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
706 thread_args.stop_signal = signalled_thr->suspend.stop_signal; 706 thread_args.stop_signal = signalled_thr->suspend.stop_signal;
707 707
708 fbsd_corefile_thread (signalled_thr, &thread_args); 708 fbsd_corefile_thread (signalled_thr, &thread_args);
709 ALL_NON_EXITED_THREADS (thr) 709 for (thread_info *thr : current_inferior ()->non_exited_threads ())
710 { 710 {
711 if (thr == signalled_thr) 711 if (thr == signalled_thr)
712 continue; 712 continue;
713 if (thr->ptid.pid () != inferior_ptid.pid ())
714 continue;
715 713
716 fbsd_corefile_thread (thr, &thread_args); 714 fbsd_corefile_thread (thr, &thread_args);
717 } 715 }
diff --git a/gdb/fork-child.c b/gdb/fork-child.c
index 1de96b6..1742740 100644
--- a/gdb/fork-child.c
+++ b/gdb/fork-child.c
@@ -78,12 +78,7 @@ prefork_hook (const char *args)
78void 78void
79postfork_hook (pid_t pid) 79postfork_hook (pid_t pid)
80{ 80{
81 struct inferior *inf; 81 inferior *inf = current_inferior ();
82
83 if (!have_inferiors ())
84 init_thread_list ();
85
86 inf = current_inferior ();
87 82
88 inferior_appeared (inf, pid); 83 inferior_appeared (inf, pid);
89 84
diff --git a/gdb/gdbarch-selftests.c b/gdb/gdbarch-selftests.c
index 4486056..663146f 100644
--- a/gdb/gdbarch-selftests.c
+++ b/gdb/gdbarch-selftests.c
@@ -86,7 +86,7 @@ register_to_value_test (struct gdbarch *gdbarch)
86 thread_info mock_thread (&mock_inferior, mock_ptid); 86 thread_info mock_thread (&mock_inferior, mock_ptid);
87 87
88 scoped_restore restore_thread_list 88 scoped_restore restore_thread_list
89 = make_scoped_restore (&thread_list, &mock_thread); 89 = make_scoped_restore (&mock_inferior.thread_list, &mock_thread);
90 90
91 /* Add the mock inferior to the inferior list so that look ups by 91 /* Add the mock inferior to the inferior list so that look ups by
92 target+ptid can find it. */ 92 target+ptid can find it. */
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 2738e44..94fc1b7 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -26,7 +26,6 @@ struct symtab;
26#include "breakpoint.h" 26#include "breakpoint.h"
27#include "frame.h" 27#include "frame.h"
28#include "ui-out.h" 28#include "ui-out.h"
29#include "inferior.h"
30#include "btrace.h" 29#include "btrace.h"
31#include "common/vec.h" 30#include "common/vec.h"
32#include "target/waitstatus.h" 31#include "target/waitstatus.h"
@@ -34,6 +33,8 @@ struct symtab;
34#include "common/refcounted-object.h" 33#include "common/refcounted-object.h"
35#include "common-gdbthread.h" 34#include "common-gdbthread.h"
36 35
36struct inferior;
37
37/* Frontend view of the thread state. Possible extensions: stepping, 38/* Frontend view of the thread state. Possible extensions: stepping,
38 finishing, until(ling),... */ 39 finishing, until(ling),... */
39enum thread_state 40enum thread_state
@@ -43,6 +44,17 @@ enum thread_state
43 THREAD_EXITED, 44 THREAD_EXITED,
44}; 45};
45 46
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
51enum step_over_calls_kind
52 {
53 STEP_OVER_NONE,
54 STEP_OVER_ALL,
55 STEP_OVER_UNDEBUGGABLE
56 };
57
46/* Inferior thread specific part of `struct infcall_control_state'. 58/* Inferior thread specific part of `struct infcall_control_state'.
47 59
48 Inferior process counterpart is `struct inferior_control_state'. */ 60 Inferior process counterpart is `struct inferior_control_state'. */
@@ -213,12 +225,7 @@ public:
213 explicit thread_info (inferior *inf, ptid_t ptid); 225 explicit thread_info (inferior *inf, ptid_t ptid);
214 ~thread_info (); 226 ~thread_info ();
215 227
216 bool deletable () const 228 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 }
222 229
223 /* Mark this thread as running and notify observers. */ 230 /* Mark this thread as running and notify observers. */
224 void set_running (bool running); 231 void set_running (bool running);
@@ -449,6 +456,10 @@ extern int valid_global_thread_id (int global_id);
449/* Search function to lookup a thread by 'pid'. */ 456/* Search function to lookup a thread by 'pid'. */
450extern struct thread_info *find_thread_ptid (ptid_t ptid); 457extern struct thread_info *find_thread_ptid (ptid_t ptid);
451 458
459/* Search function to lookup a thread by 'ptid'. Only searches in
460 threads of INF. */
461extern struct thread_info *find_thread_ptid (inferior *inf, ptid_t ptid);
462
452/* Find thread by GDB global thread ID. */ 463/* Find thread by GDB global thread ID. */
453struct thread_info *find_thread_global_id (int global_id); 464struct thread_info *find_thread_global_id (int global_id);
454 465
@@ -475,32 +486,61 @@ void thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid);
475typedef int (*thread_callback_func) (struct thread_info *, void *); 486typedef int (*thread_callback_func) (struct thread_info *, void *);
476extern struct thread_info *iterate_over_threads (thread_callback_func, void *); 487extern struct thread_info *iterate_over_threads (thread_callback_func, void *);
477 488
478/* Traverse all threads. */ 489/* Pull in the internals of the inferiors/threads ranges and
479#define ALL_THREADS(T) \ 490 iterators. Must be done after struct thread_info is defined. */
480 for (T = thread_list; T; T = T->next) \ 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*/
499inline all_threads_range
500all_threads ()
501{
502 return {};
503}
504
505/* Likewise, but accept a filter PTID. */
481 506
482/* Traverse over all threads, sorted by inferior. */ 507inline all_matching_threads_range
483#define ALL_THREADS_BY_INFERIOR(inf, tp) \ 508all_threads (ptid_t filter_ptid)
484 ALL_INFERIORS (inf) \ 509{
485 ALL_THREADS (tp) \ 510 return all_matching_threads_range (filter_ptid);
486 if (inf == tp->inf) 511}
487 512
488/* Traverse all threads, except those that have THREAD_EXITED 513/* Return a range that can be used to walk over all non-exited threads
489 state. */ 514 of all inferiors, with range-for. FILTER_PTID can be used to
515 filter out thread that don't match. */
516
517inline all_non_exited_threads_range
518all_non_exited_threads (ptid_t filter_ptid = minus_one_ptid)
519{
520 return all_non_exited_threads_range (filter_ptid);
521}
490 522
491#define ALL_NON_EXITED_THREADS(T) \ 523/* Return a range that can be used to walk over all threads of all
492 for (T = thread_list; T; T = T->next) \ 524 inferiors, with range-for, safely. I.e., it is safe to delete the
493 if ((T)->state != THREAD_EXITED) 525 currently-iterated thread. When combined with range-for, this
526 allow convenient patterns like this:
494 527
495/* Traverse all threads, including those that have THREAD_EXITED 528 for (thread_info *t : all_threads_safe ())
496 state. Allows deleting the currently iterated thread. */ 529 if (some_condition ())
497#define ALL_THREADS_SAFE(T, TMP) \ 530 delete f;
498 for ((T) = thread_list; \ 531*/
499 (T) != NULL ? ((TMP) = (T)->next, 1): 0; \ 532
500 (T) = (TMP)) 533inline all_threads_safe_range
534all_threads_safe ()
535{
536 return all_threads_safe_range ();
537}
501 538
502extern int thread_count (void); 539extern int thread_count (void);
503 540
541/* Return true if we have any thread in any inferior. */
542extern bool any_thread_p ();
543
504/* Switch context to thread THR. Also sets the STOP_PC global. */ 544/* Switch context to thread THR. Also sets the STOP_PC global. */
505extern void switch_to_thread (struct thread_info *thr); 545extern void switch_to_thread (struct thread_info *thr);
506 546
@@ -748,6 +788,4 @@ extern void print_selected_thread_frame (struct ui_out *uiout,
748 alive anymore. */ 788 alive anymore. */
749extern void thread_select (const char *tidstr, class thread_info *thr); 789extern void thread_select (const char *tidstr, class thread_info *thr);
750 790
751extern struct thread_info *thread_list;
752
753#endif /* GDBTHREAD_H */ 791#endif /* GDBTHREAD_H */
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 27177a4..4406f86 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -1339,20 +1339,16 @@ signal_command (const char *signum_exp, int from_tty)
1339 of the wrong thread. */ 1339 of the wrong thread. */
1340 if (!non_stop) 1340 if (!non_stop)
1341 { 1341 {
1342 struct thread_info *tp;
1343 ptid_t resume_ptid;
1344 int must_confirm = 0; 1342 int must_confirm = 0;
1345 1343
1346 /* This indicates what will be resumed. Either a single thread, 1344 /* This indicates what will be resumed. Either a single thread,
1347 a whole process, or all threads of all processes. */ 1345 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);
1349 1347
1350 ALL_NON_EXITED_THREADS (tp) 1348 for (thread_info *tp : all_non_exited_threads (resume_ptid))
1351 { 1349 {
1352 if (tp->ptid == inferior_ptid) 1350 if (tp->ptid == inferior_ptid)
1353 continue; 1351 continue;
1354 if (!tp->ptid.matches (resume_ptid))
1355 continue;
1356 1352
1357 if (tp->suspend.stop_signal != GDB_SIGNAL_0 1353 if (tp->suspend.stop_signal != GDB_SIGNAL_0
1358 && signal_pass_state (tp->suspend.stop_signal)) 1354 && signal_pass_state (tp->suspend.stop_signal))
@@ -2620,34 +2616,13 @@ kill_command (const char *arg, int from_tty)
2620 bfd_cache_close_all (); 2616 bfd_cache_close_all ();
2621} 2617}
2622 2618
2623/* Used in `attach&' command. ARG is a point to an integer 2619/* Used in `attach&' command. Proceed threads of inferior INF iff
2624 representing a process id. Proceed threads of this process iff
2625 they stopped due to debugger request, and when they did, they 2620 they stopped due to debugger request, and when they did, they
2626 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads 2621 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2627 that have been explicitly been told to stop. */ 2622 have been explicitly been told to stop. */
2628
2629static int
2630proceed_after_attach_callback (struct thread_info *thread,
2631 void *arg)
2632{
2633 int pid = * (int *) arg;
2634
2635 if (thread->ptid.pid () == pid
2636 && thread->state != THREAD_EXITED
2637 && !thread->executing
2638 && !thread->stop_requested
2639 && thread->suspend.stop_signal == GDB_SIGNAL_0)
2640 {
2641 switch_to_thread (thread);
2642 clear_proceed_status (0);
2643 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2644 }
2645
2646 return 0;
2647}
2648 2623
2649static void 2624static void
2650proceed_after_attach (int pid) 2625proceed_after_attach (inferior *inf)
2651{ 2626{
2652 /* Don't error out if the current thread is running, because 2627 /* Don't error out if the current thread is running, because
2653 there may be other stopped threads. */ 2628 there may be other stopped threads. */
@@ -2655,7 +2630,15 @@ proceed_after_attach (int pid)
2655 /* Backup current thread and selected frame. */ 2630 /* Backup current thread and selected frame. */
2656 scoped_restore_current_thread restore_thread; 2631 scoped_restore_current_thread restore_thread;
2657 2632
2658 iterate_over_threads (proceed_after_attach_callback, &pid); 2633 for (thread_info *thread : inf->non_exited_threads ())
2634 if (!thread->executing
2635 && !thread->stop_requested
2636 && thread->suspend.stop_signal == GDB_SIGNAL_0)
2637 {
2638 switch_to_thread (thread);
2639 clear_proceed_status (0);
2640 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2641 }
2659} 2642}
2660 2643
2661/* See inferior.h. */ 2644/* See inferior.h. */
@@ -2722,7 +2705,7 @@ attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mod
2722 already running threads. If a thread has been stopped with a 2705 already running threads. If a thread has been stopped with a
2723 signal, leave it be. */ 2706 signal, leave it be. */
2724 if (non_stop) 2707 if (non_stop)
2725 proceed_after_attach (inferior->pid); 2708 proceed_after_attach (inferior);
2726 else 2709 else
2727 { 2710 {
2728 if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0) 2711 if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0)
@@ -2748,9 +2731,7 @@ attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mod
2748 target_stop (ptid_t (inferior->pid)); 2731 target_stop (ptid_t (inferior->pid));
2749 else if (target_is_non_stop_p ()) 2732 else if (target_is_non_stop_p ())
2750 { 2733 {
2751 struct thread_info *thread;
2752 struct thread_info *lowest = inferior_thread (); 2734 struct thread_info *lowest = inferior_thread ();
2753 int pid = current_inferior ()->pid;
2754 2735
2755 stop_all_threads (); 2736 stop_all_threads ();
2756 2737
@@ -2758,15 +2739,10 @@ attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mod
2758 stop. For consistency, always select the thread with 2739 stop. For consistency, always select the thread with
2759 lowest GDB number, which should be the main thread, if it 2740 lowest GDB number, which should be the main thread, if it
2760 still exists. */ 2741 still exists. */
2761 ALL_NON_EXITED_THREADS (thread) 2742 for (thread_info *thread : current_inferior ()->non_exited_threads ())
2762 { 2743 if (thread->inf->num < lowest->inf->num
2763 if (thread->ptid.pid () == pid) 2744 || thread->per_inf_num < lowest->per_inf_num)
2764 { 2745 lowest = thread;
2765 if (thread->inf->num < lowest->inf->num
2766 || thread->per_inf_num < lowest->per_inf_num)
2767 lowest = thread;
2768 }
2769 }
2770 2746
2771 switch_to_thread (lowest); 2747 switch_to_thread (lowest);
2772 } 2748 }
@@ -3014,11 +2990,6 @@ detach_command (const char *args, int from_tty)
3014 if (!gdbarch_has_global_solist (target_gdbarch ())) 2990 if (!gdbarch_has_global_solist (target_gdbarch ()))
3015 no_shared_libraries (NULL, from_tty); 2991 no_shared_libraries (NULL, from_tty);
3016 2992
3017 /* If we still have inferiors to debug, then don't mess with their
3018 threads. */
3019 if (!have_inferiors ())
3020 init_thread_list ();
3021
3022 if (deprecated_detach_hook) 2993 if (deprecated_detach_hook)
3023 deprecated_detach_hook (); 2994 deprecated_detach_hook ();
3024} 2995}
diff --git a/gdb/inferior-iter.h b/gdb/inferior-iter.h
new file mode 100644
index 0000000..2993c3e
--- /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
28class all_inferiors_iterator
29{
30public:
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
60private:
61 inferior *m_inf;
62};
63
64/* Filter for filtered_iterator. Filters out exited inferiors. */
65
66struct 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
76using 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. */
81struct 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
91using 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
98struct 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
109struct 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) */
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 394386a..2dff643 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -134,34 +134,10 @@ add_inferior (int pid)
134 return inf; 134 return inf;
135} 135}
136 136
137struct delete_thread_of_inferior_arg
138{
139 int pid;
140 int silent;
141};
142
143static int
144delete_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
160void 137void
161delete_inferior (struct inferior *todel) 138delete_inferior (struct inferior *todel)
162{ 139{
163 struct inferior *inf, *infprev; 140 struct inferior *inf, *infprev;
164 struct delete_thread_of_inferior_arg arg;
165 141
166 infprev = NULL; 142 infprev = NULL;
167 143
@@ -172,10 +148,8 @@ delete_inferior (struct inferior *todel)
172 if (!inf) 148 if (!inf)
173 return; 149 return;
174 150
175 arg.pid = inf->pid; 151 for (thread_info *tp : inf->threads_safe ())
176 arg.silent = 1; 152 delete_thread_silent (tp);
177
178 iterate_over_threads (delete_thread_of_inferior, &arg);
179 153
180 if (infprev) 154 if (infprev)
181 infprev->next = inf->next; 155 infprev->next = inf->next;
@@ -198,7 +172,6 @@ static void
198exit_inferior_1 (struct inferior *inftoex, int silent) 172exit_inferior_1 (struct inferior *inftoex, int silent)
199{ 173{
200 struct inferior *inf; 174 struct inferior *inf;
201 struct delete_thread_of_inferior_arg arg;
202 175
203 for (inf = inferior_list; inf; inf = inf->next) 176 for (inf = inferior_list; inf; inf = inf->next)
204 if (inf == inftoex) 177 if (inf == inftoex)
@@ -207,10 +180,13 @@ exit_inferior_1 (struct inferior *inftoex, int silent)
207 if (!inf) 180 if (!inf)
208 return; 181 return;
209 182
210 arg.pid = inf->pid; 183 for (thread_info *tp : inf->threads_safe ())
211 arg.silent = silent; 184 {
212 185 if (silent)
213 iterate_over_threads (delete_thread_of_inferior, &arg); 186 delete_thread_silent (tp);
187 else
188 delete_thread (tp);
189 }
214 190
215 gdb::observers::inferior_exit.notify (inf); 191 gdb::observers::inferior_exit.notify (inf);
216 192
@@ -273,6 +249,11 @@ detach_inferior (inferior *inf)
273void 249void
274inferior_appeared (struct inferior *inf, int pid) 250inferior_appeared (struct inferior *inf, int pid)
275{ 251{
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
276 inf->pid = pid; 257 inf->pid = pid;
277 inf->has_exit_code = 0; 258 inf->has_exit_code = 0;
278 inf->exit_code = 0; 259 inf->exit_code = 0;
@@ -283,21 +264,14 @@ inferior_appeared (struct inferior *inf, int pid)
283void 264void
284discard_all_inferiors (void) 265discard_all_inferiors (void)
285{ 266{
286 struct inferior *inf; 267 for (inferior *inf : all_non_exited_inferiors ())
287 268 exit_inferior_silent (inf);
288 for (inf = inferior_list; inf; inf = inf->next)
289 {
290 if (inf->pid != 0)
291 exit_inferior_silent (inf);
292 }
293} 269}
294 270
295struct inferior * 271struct inferior *
296find_inferior_id (int num) 272find_inferior_id (int num)
297{ 273{
298 struct inferior *inf; 274 for (inferior *inf : all_inferiors ())
299
300 for (inf = inferior_list; inf; inf = inf->next)
301 if (inf->num == num) 275 if (inf->num == num)
302 return inf; 276 return inf;
303 277
@@ -307,14 +281,12 @@ find_inferior_id (int num)
307struct inferior * 281struct inferior *
308find_inferior_pid (int pid) 282find_inferior_pid (int pid)
309{ 283{
310 struct inferior *inf;
311
312 /* Looking for inferior pid == 0 is always wrong, and indicative of 284 /* Looking for inferior pid == 0 is always wrong, and indicative of
313 a bug somewhere else. There may be more than one with pid == 0, 285 a bug somewhere else. There may be more than one with pid == 0,
314 for instance. */ 286 for instance. */
315 gdb_assert (pid != 0); 287 gdb_assert (pid != 0);
316 288
317 for (inf = inferior_list; inf; inf = inf->next) 289 for (inferior *inf : all_inferiors ())
318 if (inf->pid == pid) 290 if (inf->pid == pid)
319 return inf; 291 return inf;
320 292
@@ -334,16 +306,14 @@ find_inferior_ptid (ptid_t ptid)
334struct inferior * 306struct inferior *
335find_inferior_for_program_space (struct program_space *pspace) 307find_inferior_for_program_space (struct program_space *pspace)
336{ 308{
337 struct inferior *inf = current_inferior (); 309 struct inferior *cur_inf = current_inferior ();
338 310
339 if (inf->pspace == pspace) 311 if (cur_inf->pspace == pspace)
340 return inf; 312 return cur_inf;
341 313
342 for (inf = inferior_list; inf != NULL; inf = inf->next) 314 for (inferior *inf : all_inferiors ())
343 { 315 if (inf->pspace == pspace)
344 if (inf->pspace == pspace) 316 return inf;
345 return inf;
346 }
347 317
348 return NULL; 318 return NULL;
349} 319}
@@ -352,14 +322,9 @@ struct inferior *
352iterate_over_inferiors (int (*callback) (struct inferior *, void *), 322iterate_over_inferiors (int (*callback) (struct inferior *, void *),
353 void *data) 323 void *data)
354{ 324{
355 struct inferior *inf, *infnext; 325 for (inferior *inf : all_inferiors_safe ())
356 326 if ((*callback) (inf, data))
357 for (inf = inferior_list; inf; inf = infnext) 327 return inf;
358 {
359 infnext = inf->next;
360 if ((*callback) (inf, data))
361 return inf;
362 }
363 328
364 return NULL; 329 return NULL;
365} 330}
@@ -367,11 +332,8 @@ iterate_over_inferiors (int (*callback) (struct inferior *, void *),
367int 332int
368have_inferiors (void) 333have_inferiors (void)
369{ 334{
370 struct inferior *inf; 335 for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
371 336 return 1;
372 for (inf = inferior_list; inf; inf = inf->next)
373 if (inf->pid != 0)
374 return 1;
375 337
376 return 0; 338 return 0;
377} 339}
@@ -383,24 +345,17 @@ have_inferiors (void)
383int 345int
384number_of_live_inferiors (void) 346number_of_live_inferiors (void)
385{ 347{
386 struct inferior *inf;
387 int num_inf = 0; 348 int num_inf = 0;
388 349
389 for (inf = inferior_list; inf; inf = inf->next) 350 for (inferior *inf : all_non_exited_inferiors ())
390 if (inf->pid != 0) 351 if (target_has_execution_1 (ptid_t (inf->pid)))
391 { 352 for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
392 struct thread_info *tp; 353 {
393 354 /* Found a live thread in this inferior, go to the next
394 ALL_NON_EXITED_THREADS (tp) 355 inferior. */
395 if (tp && tp->ptid.pid () == inf->pid) 356 ++num_inf;
396 if (target_has_execution_1 (tp->ptid)) 357 break;
397 { 358 }
398 /* Found a live thread in this inferior, go to the next
399 inferior. */
400 ++num_inf;
401 break;
402 }
403 }
404 359
405 return num_inf; 360 return num_inf;
406} 361}
@@ -445,13 +400,8 @@ prune_inferiors (void)
445int 400int
446number_of_inferiors (void) 401number_of_inferiors (void)
447{ 402{
448 struct inferior *inf; 403 auto rng = all_inferiors ();
449 int count = 0; 404 return std::distance (rng.begin (), rng.end ());
450
451 for (inf = inferior_list; inf != NULL; inf = inf->next)
452 count++;
453
454 return count;
455} 405}
456 406
457/* Converts an inferior process id to a string. Like 407/* Converts an inferior process id to a string. Like
@@ -491,11 +441,10 @@ print_selected_inferior (struct ui_out *uiout)
491static void 441static void
492print_inferior (struct ui_out *uiout, const char *requested_inferiors) 442print_inferior (struct ui_out *uiout, const char *requested_inferiors)
493{ 443{
494 struct inferior *inf;
495 int inf_count = 0; 444 int inf_count = 0;
496 445
497 /* Compute number of inferiors we will print. */ 446 /* Compute number of inferiors we will print. */
498 for (inf = inferior_list; inf; inf = inf->next) 447 for (inferior *inf : all_inferiors ())
499 { 448 {
500 if (!number_is_in_list (requested_inferiors, inf->num)) 449 if (!number_is_in_list (requested_inferiors, inf->num))
501 continue; 450 continue;
@@ -516,7 +465,7 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
516 uiout->table_header (17, ui_left, "exec", "Executable"); 465 uiout->table_header (17, ui_left, "exec", "Executable");
517 466
518 uiout->table_body (); 467 uiout->table_body ();
519 for (inf = inferior_list; inf; inf = inf->next) 468 for (inferior *inf : all_inferiors ())
520 { 469 {
521 if (!number_is_in_list (requested_inferiors, inf->num)) 470 if (!number_is_in_list (requested_inferiors, inf->num))
522 continue; 471 continue;
diff --git a/gdb/inferior.h b/gdb/inferior.h
index af5e920..33c2eac 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -53,6 +53,7 @@ struct thread_info;
53#include "common/refcounted-object.h" 53#include "common/refcounted-object.h"
54 54
55#include "common-inferior.h" 55#include "common-inferior.h"
56#include "gdbthread.h"
56 57
57struct infcall_suspend_state; 58struct infcall_suspend_state;
58struct infcall_control_state; 59struct infcall_control_state;
@@ -245,17 +246,6 @@ extern int stopped_by_random_signal;
245 `set print inferior-events'. */ 246 `set print inferior-events'. */
246extern int print_inferior_events; 247extern int print_inferior_events;
247 248
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
252enum step_over_calls_kind
253 {
254 STEP_OVER_NONE,
255 STEP_OVER_ALL,
256 STEP_OVER_UNDEBUGGABLE
257 };
258
259/* Anything but NO_STOP_QUIETLY means we expect a trap and the caller 249/* Anything but NO_STOP_QUIETLY means we expect a trap and the caller
260 will handle it themselves. STOP_QUIETLY is used when running in 250 will handle it themselves. STOP_QUIETLY is used when running in
261 the shell before the child program has been exec'd and when running 251 the shell before the child program has been exec'd and when running
@@ -360,6 +350,38 @@ public:
360 /* Pointer to next inferior in singly-linked list of inferiors. */ 350 /* Pointer to next inferior in singly-linked list of inferiors. */
361 struct inferior *next = NULL; 351 struct inferior *next = NULL;
362 352
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
363 /* Convenient handle (GDB inferior id). Unique across all 385 /* Convenient handle (GDB inferior id). Unique across all
364 inferiors. */ 386 inferiors. */
365 int num = 0; 387 int num = 0;
@@ -575,16 +597,49 @@ private:
575 597
576/* Traverse all inferiors. */ 598/* Traverse all inferiors. */
577 599
578#define ALL_INFERIORS(I) \ 600extern struct inferior *inferior_list;
579 for ((I) = inferior_list; (I); (I) = (I)->next)
580 601
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"
582 605
583#define ALL_NON_EXITED_INFERIORS(I) \ 606/* Return a range that can be used to walk over all inferiors
584 ALL_INFERIORS (I) \ 607 inferiors, with range-for, safely. I.e., it is safe to delete the
585 if ((I)->pid != 0) 608 currently-iterated inferior. When combined with range-for, this
609 allow convenient patterns like this:
586 610
587extern struct inferior *inferior_list; 611 for (inferior *inf : all_inferiors_safe ())
612 if (some_condition ())
613 delete inf;
614*/
615
616inline all_inferiors_safe_range
617all_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
629inline all_inferiors_range
630all_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
638inline all_non_exited_inferiors_range
639all_non_exited_inferiors ()
640{
641 return {};
642}
588 643
589/* Prune away automatically added inferiors that aren't required 644/* Prune away automatically added inferiors that aren't required
590 anymore. */ 645 anymore. */
diff --git a/gdb/inflow.c b/gdb/inflow.c
index a0c1c7d..163e28c 100644
--- a/gdb/inflow.c
+++ b/gdb/inflow.c
@@ -546,9 +546,8 @@ void
546child_interrupt (struct target_ops *self) 546child_interrupt (struct target_ops *self)
547{ 547{
548 /* Interrupt the first inferior that has a resumed thread. */ 548 /* Interrupt the first inferior that has a resumed thread. */
549 thread_info *thr;
550 thread_info *resumed = NULL; 549 thread_info *resumed = NULL;
551 ALL_NON_EXITED_THREADS (thr) 550 for (thread_info *thr : all_non_exited_threads ())
552 { 551 {
553 if (thr->executing) 552 if (thr->executing)
554 { 553 {
@@ -605,8 +604,7 @@ child_pass_ctrlc (struct target_ops *self)
605 604
606 /* Otherwise, pass the Ctrl-C to the first inferior that was resumed 605 /* Otherwise, pass the Ctrl-C to the first inferior that was resumed
607 in the foreground. */ 606 in the foreground. */
608 inferior *inf; 607 for (inferior *inf : all_inferiors ())
609 ALL_INFERIORS (inf)
610 { 608 {
611 if (inf->terminal_state != target_terminal_state::is_ours) 609 if (inf->terminal_state != target_terminal_state::is_ours)
612 { 610 {
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 81f45be..46a8985 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1082,7 +1082,6 @@ show_follow_exec_mode_string (struct ui_file *file, int from_tty,
1082static void 1082static void
1083follow_exec (ptid_t ptid, char *exec_file_target) 1083follow_exec (ptid_t ptid, char *exec_file_target)
1084{ 1084{
1085 struct thread_info *th, *tmp;
1086 struct inferior *inf = current_inferior (); 1085 struct inferior *inf = current_inferior ();
1087 int pid = ptid.pid (); 1086 int pid = ptid.pid ();
1088 ptid_t process_ptid; 1087 ptid_t process_ptid;
@@ -1129,7 +1128,7 @@ follow_exec (ptid_t ptid, char *exec_file_target)
1129 them. Deleting them now rather than at the next user-visible 1128 them. Deleting them now rather than at the next user-visible
1130 stop provides a nicer sequence of events for user and MI 1129 stop provides a nicer sequence of events for user and MI
1131 notifications. */ 1130 notifications. */
1132 ALL_THREADS_SAFE (th, tmp) 1131 for (thread_info *th : all_threads_safe ())
1133 if (th->ptid.pid () == pid && th->ptid != ptid) 1132 if (th->ptid.pid () == pid && th->ptid != ptid)
1134 delete_thread (th); 1133 delete_thread (th);
1135 1134
@@ -1137,7 +1136,7 @@ follow_exec (ptid_t ptid, char *exec_file_target)
1137 leader/event thread. E.g., if there was any step-resume 1136 leader/event thread. E.g., if there was any step-resume
1138 breakpoint or similar, it's gone now. We cannot truly 1137 breakpoint or similar, it's gone now. We cannot truly
1139 step-to-next statement through an exec(). */ 1138 step-to-next statement through an exec(). */
1140 th = inferior_thread (); 1139 thread_info *th = inferior_thread ();
1141 th->control.step_resume_breakpoint = NULL; 1140 th->control.step_resume_breakpoint = NULL;
1142 th->control.exception_resume_breakpoint = NULL; 1141 th->control.exception_resume_breakpoint = NULL;
1143 th->control.single_step_breakpoints = NULL; 1142 th->control.single_step_breakpoints = NULL;
@@ -2851,21 +2850,14 @@ clear_proceed_status (int step)
2851 execution_direction)) 2850 execution_direction))
2852 target_record_stop_replaying (); 2851 target_record_stop_replaying ();
2853 2852
2854 if (!non_stop) 2853 if (!non_stop && inferior_ptid != null_ptid)
2855 { 2854 {
2856 struct thread_info *tp; 2855 ptid_t resume_ptid = user_visible_resume_ptid (step);
2857 ptid_t resume_ptid;
2858
2859 resume_ptid = user_visible_resume_ptid (step);
2860 2856
2861 /* In all-stop mode, delete the per-thread status of all threads 2857 /* In all-stop mode, delete the per-thread status of all threads
2862 we're about to resume, implicitly and explicitly. */ 2858 we're about to resume, implicitly and explicitly. */
2863 ALL_NON_EXITED_THREADS (tp) 2859 for (thread_info *tp : all_non_exited_threads (resume_ptid))
2864 { 2860 clear_proceed_status_thread (tp);
2865 if (!tp->ptid.matches (resume_ptid))
2866 continue;
2867 clear_proceed_status_thread (tp);
2868 }
2869 } 2861 }
2870 2862
2871 if (inferior_ptid != null_ptid) 2863 if (inferior_ptid != null_ptid)
@@ -2954,7 +2946,6 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
2954{ 2946{
2955 struct regcache *regcache; 2947 struct regcache *regcache;
2956 struct gdbarch *gdbarch; 2948 struct gdbarch *gdbarch;
2957 struct thread_info *tp;
2958 CORE_ADDR pc; 2949 CORE_ADDR pc;
2959 ptid_t resume_ptid; 2950 ptid_t resume_ptid;
2960 struct execution_control_state ecss; 2951 struct execution_control_state ecss;
@@ -2981,16 +2972,16 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
2981 const address_space *aspace = regcache->aspace (); 2972 const address_space *aspace = regcache->aspace ();
2982 2973
2983 pc = regcache_read_pc (regcache); 2974 pc = regcache_read_pc (regcache);
2984 tp = inferior_thread (); 2975 thread_info *cur_thr = inferior_thread ();
2985 2976
2986 /* Fill in with reasonable starting values. */ 2977 /* Fill in with reasonable starting values. */
2987 init_thread_stepping_state (tp); 2978 init_thread_stepping_state (cur_thr);
2988 2979
2989 gdb_assert (!thread_is_in_step_over_chain (tp)); 2980 gdb_assert (!thread_is_in_step_over_chain (cur_thr));
2990 2981
2991 if (addr == (CORE_ADDR) -1) 2982 if (addr == (CORE_ADDR) -1)
2992 { 2983 {
2993 if (pc == tp->suspend.stop_pc 2984 if (pc == cur_thr->suspend.stop_pc
2994 && breakpoint_here_p (aspace, pc) == ordinary_breakpoint_here 2985 && breakpoint_here_p (aspace, pc) == ordinary_breakpoint_here
2995 && execution_direction != EXEC_REVERSE) 2986 && execution_direction != EXEC_REVERSE)
2996 /* There is a breakpoint at the address we will resume at, 2987 /* There is a breakpoint at the address we will resume at,
@@ -3001,13 +2992,13 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
3001 Note, we don't do this in reverse, because we won't 2992 Note, we don't do this in reverse, because we won't
3002 actually be executing the breakpoint insn anyway. 2993 actually be executing the breakpoint insn anyway.
3003 We'll be (un-)executing the previous instruction. */ 2994 We'll be (un-)executing the previous instruction. */
3004 tp->stepping_over_breakpoint = 1; 2995 cur_thr->stepping_over_breakpoint = 1;
3005 else if (gdbarch_single_step_through_delay_p (gdbarch) 2996 else if (gdbarch_single_step_through_delay_p (gdbarch)
3006 && gdbarch_single_step_through_delay (gdbarch, 2997 && gdbarch_single_step_through_delay (gdbarch,
3007 get_current_frame ())) 2998 get_current_frame ()))
3008 /* We stepped onto an instruction that needs to be stepped 2999 /* We stepped onto an instruction that needs to be stepped
3009 again before re-inserting the breakpoint, do so. */ 3000 again before re-inserting the breakpoint, do so. */
3010 tp->stepping_over_breakpoint = 1; 3001 cur_thr->stepping_over_breakpoint = 1;
3011 } 3002 }
3012 else 3003 else
3013 { 3004 {
@@ -3015,9 +3006,9 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
3015 } 3006 }
3016 3007
3017 if (siggnal != GDB_SIGNAL_DEFAULT) 3008 if (siggnal != GDB_SIGNAL_DEFAULT)
3018 tp->suspend.stop_signal = siggnal; 3009 cur_thr->suspend.stop_signal = siggnal;
3019 3010
3020 resume_ptid = user_visible_resume_ptid (tp->control.stepping_command); 3011 resume_ptid = user_visible_resume_ptid (cur_thr->control.stepping_command);
3021 3012
3022 /* If an exception is thrown from this point on, make sure to 3013 /* If an exception is thrown from this point on, make sure to
3023 propagate GDB's knowledge of the executing state to the 3014 propagate GDB's knowledge of the executing state to the
@@ -3030,7 +3021,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
3030 threads in RESUME_PTID are now running. Unless we're calling an 3021 threads in RESUME_PTID are now running. Unless we're calling an
3031 inferior function, as in that case we pretend the inferior 3022 inferior function, as in that case we pretend the inferior
3032 doesn't run at all. */ 3023 doesn't run at all. */
3033 if (!tp->control.in_infcall) 3024 if (!cur_thr->control.in_infcall)
3034 set_running (resume_ptid, 1); 3025 set_running (resume_ptid, 1);
3035 3026
3036 if (debug_infrun) 3027 if (debug_infrun)
@@ -3064,19 +3055,13 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
3064 3055
3065 /* If scheduler locking applies, we can avoid iterating over all 3056 /* If scheduler locking applies, we can avoid iterating over all
3066 threads. */ 3057 threads. */
3067 if (!non_stop && !schedlock_applies (tp)) 3058 if (!non_stop && !schedlock_applies (cur_thr))
3068 { 3059 {
3069 struct thread_info *current = tp; 3060 for (thread_info *tp : all_non_exited_threads (resume_ptid))
3070 3061 {
3071 ALL_NON_EXITED_THREADS (tp)
3072 {
3073 /* Ignore the current thread here. It's handled 3062 /* Ignore the current thread here. It's handled
3074 afterwards. */ 3063 afterwards. */
3075 if (tp == current) 3064 if (tp == cur_thr)
3076 continue;
3077
3078 /* Ignore threads of processes we're not resuming. */
3079 if (!tp->ptid.matches (resume_ptid))
3080 continue; 3065 continue;
3081 3066
3082 if (!thread_still_needs_step_over (tp)) 3067 if (!thread_still_needs_step_over (tp))
@@ -3091,21 +3076,19 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
3091 3076
3092 thread_step_over_chain_enqueue (tp); 3077 thread_step_over_chain_enqueue (tp);
3093 } 3078 }
3094
3095 tp = current;
3096 } 3079 }
3097 3080
3098 /* Enqueue the current thread last, so that we move all other 3081 /* Enqueue the current thread last, so that we move all other
3099 threads over their breakpoints first. */ 3082 threads over their breakpoints first. */
3100 if (tp->stepping_over_breakpoint) 3083 if (cur_thr->stepping_over_breakpoint)
3101 thread_step_over_chain_enqueue (tp); 3084 thread_step_over_chain_enqueue (cur_thr);
3102 3085
3103 /* If the thread isn't started, we'll still need to set its prev_pc, 3086 /* If the thread isn't started, we'll still need to set its prev_pc,
3104 so that switch_back_to_stepped_thread knows the thread hasn't 3087 so that switch_back_to_stepped_thread knows the thread hasn't
3105 advanced. Must do this before resuming any thread, as in 3088 advanced. Must do this before resuming any thread, as in
3106 all-stop/remote, once we resume we can't send any other packet 3089 all-stop/remote, once we resume we can't send any other packet
3107 until the target stops again. */ 3090 until the target stops again. */
3108 tp->prev_pc = regcache_read_pc (regcache); 3091 cur_thr->prev_pc = regcache_read_pc (regcache);
3109 3092
3110 { 3093 {
3111 scoped_restore save_defer_tc = make_scoped_defer_target_commit_resume (); 3094 scoped_restore save_defer_tc = make_scoped_defer_target_commit_resume ();
@@ -3127,12 +3110,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
3127 { 3110 {
3128 /* In all-stop, but the target is always in non-stop mode. 3111 /* In all-stop, but the target is always in non-stop mode.
3129 Start all other threads that are implicitly resumed too. */ 3112 Start all other threads that are implicitly resumed too. */
3130 ALL_NON_EXITED_THREADS (tp) 3113 for (thread_info *tp : all_non_exited_threads (resume_ptid))
3131 { 3114 {
3132 /* Ignore threads of processes we're not resuming. */
3133 if (!tp->ptid.matches (resume_ptid))
3134 continue;
3135
3136 if (tp->resumed) 3115 if (tp->resumed)
3137 { 3116 {
3138 if (debug_infrun) 3117 if (debug_infrun)
@@ -3164,11 +3143,11 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
3164 error (_("Command aborted.")); 3143 error (_("Command aborted."));
3165 } 3144 }
3166 } 3145 }
3167 else if (!tp->resumed && !thread_is_in_step_over_chain (tp)) 3146 else if (!cur_thr->resumed && !thread_is_in_step_over_chain (cur_thr))
3168 { 3147 {
3169 /* The thread wasn't started, and isn't queued, run it now. */ 3148 /* The thread wasn't started, and isn't queued, run it now. */
3170 reset_ecs (ecs, tp); 3149 reset_ecs (ecs, cur_thr);
3171 switch_to_thread (tp); 3150 switch_to_thread (cur_thr);
3172 keep_going_pass_signal (ecs); 3151 keep_going_pass_signal (ecs);
3173 if (!ecs->wait_some_more) 3152 if (!ecs->wait_some_more)
3174 error (_("Command aborted.")); 3153 error (_("Command aborted."));
@@ -3235,9 +3214,6 @@ init_wait_for_inferior (void)
3235 target_last_wait_ptid = minus_one_ptid; 3214 target_last_wait_ptid = minus_one_ptid;
3236 3215
3237 previous_inferior_ptid = inferior_ptid; 3216 previous_inferior_ptid = inferior_ptid;
3238
3239 /* Discard any skipped inlined frames. */
3240 clear_inline_frame_state (minus_one_ptid);
3241} 3217}
3242 3218
3243 3219
@@ -3265,53 +3241,50 @@ static int switch_back_to_stepped_thread (struct execution_control_state *ecs);
3265static void 3241static void
3266infrun_thread_stop_requested (ptid_t ptid) 3242infrun_thread_stop_requested (ptid_t ptid)
3267{ 3243{
3268 struct thread_info *tp;
3269
3270 /* PTID was requested to stop. If the thread was already stopped, 3244 /* PTID was requested to stop. If the thread was already stopped,
3271 but the user/frontend doesn't know about that yet (e.g., the 3245 but the user/frontend doesn't know about that yet (e.g., the
3272 thread had been temporarily paused for some step-over), set up 3246 thread had been temporarily paused for some step-over), set up
3273 for reporting the stop now. */ 3247 for reporting the stop now. */
3274 ALL_NON_EXITED_THREADS (tp) 3248 for (thread_info *tp : all_threads (ptid))
3275 if (tp->ptid.matches (ptid)) 3249 {
3276 { 3250 if (tp->state != THREAD_RUNNING)
3277 if (tp->state != THREAD_RUNNING) 3251 continue;
3278 continue; 3252 if (tp->executing)
3279 if (tp->executing) 3253 continue;
3280 continue;
3281 3254
3282 /* Remove matching threads from the step-over queue, so 3255 /* Remove matching threads from the step-over queue, so
3283 start_step_over doesn't try to resume them 3256 start_step_over doesn't try to resume them
3284 automatically. */ 3257 automatically. */
3285 if (thread_is_in_step_over_chain (tp)) 3258 if (thread_is_in_step_over_chain (tp))
3286 thread_step_over_chain_remove (tp); 3259 thread_step_over_chain_remove (tp);
3287
3288 /* If the thread is stopped, but the user/frontend doesn't
3289 know about that yet, queue a pending event, as if the
3290 thread had just stopped now. Unless the thread already had
3291 a pending event. */
3292 if (!tp->suspend.waitstatus_pending_p)
3293 {
3294 tp->suspend.waitstatus_pending_p = 1;
3295 tp->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED;
3296 tp->suspend.waitstatus.value.sig = GDB_SIGNAL_0;
3297 }
3298 3260
3299 /* Clear the inline-frame state, since we're re-processing the 3261 /* If the thread is stopped, but the user/frontend doesn't
3300 stop. */ 3262 know about that yet, queue a pending event, as if the
3301 clear_inline_frame_state (tp->ptid); 3263 thread had just stopped now. Unless the thread already had
3264 a pending event. */
3265 if (!tp->suspend.waitstatus_pending_p)
3266 {
3267 tp->suspend.waitstatus_pending_p = 1;
3268 tp->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED;
3269 tp->suspend.waitstatus.value.sig = GDB_SIGNAL_0;
3270 }
3302 3271
3303 /* If this thread was paused because some other thread was 3272 /* Clear the inline-frame state, since we're re-processing the
3304 doing an inline-step over, let that finish first. Once 3273 stop. */
3305 that happens, we'll restart all threads and consume pending 3274 clear_inline_frame_state (tp->ptid);
3306 stop events then. */
3307 if (step_over_info_valid_p ())
3308 continue;
3309 3275
3310 /* Otherwise we can process the (new) pending event now. Set 3276 /* If this thread was paused because some other thread was
3311 it so this pending event is considered by 3277 doing an inline-step over, let that finish first. Once
3312 do_target_wait. */ 3278 that happens, we'll restart all threads and consume pending
3313 tp->resumed = 1; 3279 stop events then. */
3314 } 3280 if (step_over_info_valid_p ())
3281 continue;
3282
3283 /* Otherwise we can process the (new) pending event now. Set
3284 it so this pending event is considered by
3285 do_target_wait. */
3286 tp->resumed = 1;
3287 }
3315} 3288}
3316 3289
3317static void 3290static void
@@ -3352,13 +3325,9 @@ for_each_just_stopped_thread (for_each_just_stopped_thread_callback_func func)
3352 } 3325 }
3353 else 3326 else
3354 { 3327 {
3355 struct thread_info *tp;
3356
3357 /* In all-stop mode, all threads have stopped. */ 3328 /* In all-stop mode, all threads have stopped. */
3358 ALL_NON_EXITED_THREADS (tp) 3329 for (thread_info *tp : all_non_exited_threads ())
3359 { 3330 func (tp);
3360 func (tp);
3361 }
3362 } 3331 }
3363} 3332}
3364 3333
@@ -3427,24 +3396,26 @@ print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
3427static struct thread_info * 3396static struct thread_info *
3428random_pending_event_thread (ptid_t waiton_ptid) 3397random_pending_event_thread (ptid_t waiton_ptid)
3429{ 3398{
3430 struct thread_info *event_tp;
3431 int num_events = 0; 3399 int num_events = 0;
3432 int random_selector; 3400
3401 auto has_event = [] (thread_info *tp)
3402 {
3403 return (tp->resumed
3404 && tp->suspend.waitstatus_pending_p);
3405 };
3433 3406
3434 /* First see how many events we have. Count only resumed threads 3407 /* First see how many events we have. Count only resumed threads
3435 that have an event pending. */ 3408 that have an event pending. */
3436 ALL_NON_EXITED_THREADS (event_tp) 3409 for (thread_info *tp : all_non_exited_threads (waiton_ptid))
3437 if (event_tp->ptid.matches (waiton_ptid) 3410 if (has_event (tp))
3438 && event_tp->resumed
3439 && event_tp->suspend.waitstatus_pending_p)
3440 num_events++; 3411 num_events++;
3441 3412
3442 if (num_events == 0) 3413 if (num_events == 0)
3443 return NULL; 3414 return NULL;
3444 3415
3445 /* Now randomly pick a thread out of those that have had events. */ 3416 /* Now randomly pick a thread out of those that have had events. */
3446 random_selector = (int) 3417 int random_selector = (int) ((num_events * (double) rand ())
3447 ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); 3418 / (RAND_MAX + 1.0));
3448 3419
3449 if (debug_infrun && num_events > 1) 3420 if (debug_infrun && num_events > 1)
3450 fprintf_unfiltered (gdb_stdlog, 3421 fprintf_unfiltered (gdb_stdlog,
@@ -3452,14 +3423,12 @@ random_pending_event_thread (ptid_t waiton_ptid)
3452 num_events, random_selector); 3423 num_events, random_selector);
3453 3424
3454 /* Select the Nth thread that has had an event. */ 3425 /* Select the Nth thread that has had an event. */
3455 ALL_NON_EXITED_THREADS (event_tp) 3426 for (thread_info *tp : all_non_exited_threads (waiton_ptid))
3456 if (event_tp->ptid.matches (waiton_ptid) 3427 if (has_event (tp))
3457 && event_tp->resumed
3458 && event_tp->suspend.waitstatus_pending_p)
3459 if (random_selector-- == 0) 3428 if (random_selector-- == 0)
3460 break; 3429 return tp;
3461 3430
3462 return event_tp; 3431 gdb_assert_not_reached ("event thread not found");
3463} 3432}
3464 3433
3465/* Wrapper for target_wait that first checks whether threads have 3434/* Wrapper for target_wait that first checks whether threads have
@@ -3755,14 +3724,14 @@ reinstall_readline_callback_handler_cleanup (void *arg)
3755static void 3724static void
3756clean_up_just_stopped_threads_fsms (struct execution_control_state *ecs) 3725clean_up_just_stopped_threads_fsms (struct execution_control_state *ecs)
3757{ 3726{
3758 struct thread_info *thr = ecs->event_thread; 3727 if (ecs->event_thread != NULL
3759 3728 && ecs->event_thread->thread_fsm != NULL)
3760 if (thr != NULL && thr->thread_fsm != NULL) 3729 thread_fsm_clean_up (ecs->event_thread->thread_fsm,
3761 thread_fsm_clean_up (thr->thread_fsm, thr); 3730 ecs->event_thread);
3762 3731
3763 if (!non_stop) 3732 if (!non_stop)
3764 { 3733 {
3765 ALL_NON_EXITED_THREADS (thr) 3734 for (thread_info *thr : all_non_exited_threads ())
3766 { 3735 {
3767 if (thr->thread_fsm == NULL) 3736 if (thr->thread_fsm == NULL)
3768 continue; 3737 continue;
@@ -4461,13 +4430,12 @@ stop_all_threads (void)
4461 ptid_t event_ptid; 4430 ptid_t event_ptid;
4462 struct target_waitstatus ws; 4431 struct target_waitstatus ws;
4463 int need_wait = 0; 4432 int need_wait = 0;
4464 struct thread_info *t;
4465 4433
4466 update_thread_list (); 4434 update_thread_list ();
4467 4435
4468 /* Go through all threads looking for threads that we need 4436 /* Go through all threads looking for threads that we need
4469 to tell the target to stop. */ 4437 to tell the target to stop. */
4470 ALL_NON_EXITED_THREADS (t) 4438 for (thread_info *t : all_non_exited_threads ())
4471 { 4439 {
4472 if (t->executing) 4440 if (t->executing)
4473 { 4441 {
@@ -4539,9 +4507,7 @@ stop_all_threads (void)
4539 } 4507 }
4540 else 4508 else
4541 { 4509 {
4542 inferior *inf; 4510 thread_info *t = find_thread_ptid (event_ptid);
4543
4544 t = find_thread_ptid (event_ptid);
4545 if (t == NULL) 4511 if (t == NULL)
4546 t = add_thread (event_ptid); 4512 t = add_thread (event_ptid);
4547 4513
@@ -4552,7 +4518,7 @@ stop_all_threads (void)
4552 4518
4553 /* This may be the first time we see the inferior report 4519 /* This may be the first time we see the inferior report
4554 a stop. */ 4520 a stop. */
4555 inf = find_inferior_ptid (event_ptid); 4521 inferior *inf = find_inferior_ptid (event_ptid);
4556 if (inf->needs_setup) 4522 if (inf->needs_setup)
4557 { 4523 {
4558 switch_to_thread_no_regs (t); 4524 switch_to_thread_no_regs (t);
@@ -4642,9 +4608,6 @@ stop_all_threads (void)
4642static int 4608static int
4643handle_no_resumed (struct execution_control_state *ecs) 4609handle_no_resumed (struct execution_control_state *ecs)
4644{ 4610{
4645 struct inferior *inf;
4646 struct thread_info *thread;
4647
4648 if (target_can_async_p ()) 4611 if (target_can_async_p ())
4649 { 4612 {
4650 struct ui *ui; 4613 struct ui *ui;
@@ -4707,7 +4670,7 @@ handle_no_resumed (struct execution_control_state *ecs)
4707 the synchronous command show "no unwaited-for " to the user. */ 4670 the synchronous command show "no unwaited-for " to the user. */
4708 update_thread_list (); 4671 update_thread_list ();
4709 4672
4710 ALL_NON_EXITED_THREADS (thread) 4673 for (thread_info *thread : all_non_exited_threads ())
4711 { 4674 {
4712 if (thread->executing 4675 if (thread->executing
4713 || thread->suspend.waitstatus_pending_p) 4676 || thread->suspend.waitstatus_pending_p)
@@ -4727,12 +4690,12 @@ handle_no_resumed (struct execution_control_state *ecs)
4727 process exited meanwhile (thus updating the thread list results 4690 process exited meanwhile (thus updating the thread list results
4728 in an empty thread list). In this case we know we'll be getting 4691 in an empty thread list). In this case we know we'll be getting
4729 a process exit event shortly. */ 4692 a process exit event shortly. */
4730 ALL_INFERIORS (inf) 4693 for (inferior *inf : all_inferiors ())
4731 { 4694 {
4732 if (inf->pid == 0) 4695 if (inf->pid == 0)
4733 continue; 4696 continue;
4734 4697
4735 thread = any_live_thread_of_inferior (inf); 4698 thread_info *thread = any_live_thread_of_inferior (inf);
4736 if (thread == NULL) 4699 if (thread == NULL)
4737 { 4700 {
4738 if (debug_infrun) 4701 if (debug_infrun)
@@ -5383,12 +5346,10 @@ handle_inferior_event (struct execution_control_state *ecs)
5383static void 5346static void
5384restart_threads (struct thread_info *event_thread) 5347restart_threads (struct thread_info *event_thread)
5385{ 5348{
5386 struct thread_info *tp;
5387
5388 /* In case the instruction just stepped spawned a new thread. */ 5349 /* In case the instruction just stepped spawned a new thread. */
5389 update_thread_list (); 5350 update_thread_list ();
5390 5351
5391 ALL_NON_EXITED_THREADS (tp) 5352 for (thread_info *tp : all_non_exited_threads ())
5392 { 5353 {
5393 if (tp == event_thread) 5354 if (tp == event_thread)
5394 { 5355 {
@@ -6996,7 +6957,6 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
6996{ 6957{
6997 if (!target_is_non_stop_p ()) 6958 if (!target_is_non_stop_p ())
6998 { 6959 {
6999 struct thread_info *tp;
7000 struct thread_info *stepping_thread; 6960 struct thread_info *stepping_thread;
7001 6961
7002 /* If any thread is blocked on some internal breakpoint, and we 6962 /* If any thread is blocked on some internal breakpoint, and we
@@ -7083,7 +7043,7 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
7083 /* Look for the stepping/nexting thread. */ 7043 /* Look for the stepping/nexting thread. */
7084 stepping_thread = NULL; 7044 stepping_thread = NULL;
7085 7045
7086 ALL_NON_EXITED_THREADS (tp) 7046 for (thread_info *tp : all_non_exited_threads ())
7087 { 7047 {
7088 /* Ignore threads of processes the caller is not 7048 /* Ignore threads of processes the caller is not
7089 resuming. */ 7049 resuming. */
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 64015e7..786213d 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -3171,9 +3171,7 @@ linux_nat_filter_event (int lwpid, int status)
3171static void 3171static void
3172check_zombie_leaders (void) 3172check_zombie_leaders (void)
3173{ 3173{
3174 struct inferior *inf; 3174 for (inferior *inf : all_inferiors ())
3175
3176 ALL_INFERIORS (inf)
3177 { 3175 {
3178 struct lwp_info *leader_lp; 3176 struct lwp_info *leader_lp;
3179 3177
@@ -3678,28 +3676,25 @@ kill_wait_callback (struct lwp_info *lp, void *data)
3678static void 3676static void
3679kill_unfollowed_fork_children (struct inferior *inf) 3677kill_unfollowed_fork_children (struct inferior *inf)
3680{ 3678{
3681 struct thread_info *thread; 3679 for (thread_info *thread : inf->non_exited_threads ())
3680 {
3681 struct target_waitstatus *ws = &thread->pending_follow;
3682 3682
3683 ALL_NON_EXITED_THREADS (thread) 3683 if (ws->kind == TARGET_WAITKIND_FORKED
3684 if (thread->inf == inf) 3684 || ws->kind == TARGET_WAITKIND_VFORKED)
3685 { 3685 {
3686 struct target_waitstatus *ws = &thread->pending_follow; 3686 ptid_t child_ptid = ws->value.related_pid;
3687 3687 int child_pid = child_ptid.pid ();
3688 if (ws->kind == TARGET_WAITKIND_FORKED 3688 int child_lwp = child_ptid.lwp ();
3689 || ws->kind == TARGET_WAITKIND_VFORKED) 3689
3690 { 3690 kill_one_lwp (child_lwp);
3691 ptid_t child_ptid = ws->value.related_pid; 3691 kill_wait_one_lwp (child_lwp);
3692 int child_pid = child_ptid.pid (); 3692
3693 int child_lwp = child_ptid.lwp (); 3693 /* Let the arch-specific native code know this process is
3694 3694 gone. */
3695 kill_one_lwp (child_lwp); 3695 linux_target->low_forget_process (child_pid);
3696 kill_wait_one_lwp (child_lwp); 3696 }
3697 3697 }
3698 /* Let the arch-specific native code know this process is
3699 gone. */
3700 linux_target->low_forget_process (child_pid);
3701 }
3702 }
3703} 3698}
3704 3699
3705void 3700void
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index ecdb928..51fe037 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1913,7 +1913,7 @@ linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
1913 struct linux_corefile_thread_data thread_args; 1913 struct linux_corefile_thread_data thread_args;
1914 struct elf_internal_linux_prpsinfo prpsinfo; 1914 struct elf_internal_linux_prpsinfo prpsinfo;
1915 char *note_data = NULL; 1915 char *note_data = NULL;
1916 struct thread_info *curr_thr, *signalled_thr, *thr; 1916 struct thread_info *curr_thr, *signalled_thr;
1917 1917
1918 if (! gdbarch_iterate_over_regset_sections_p (gdbarch)) 1918 if (! gdbarch_iterate_over_regset_sections_p (gdbarch))
1919 return NULL; 1919 return NULL;
@@ -1962,12 +1962,10 @@ linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
1962 thread_args.stop_signal = signalled_thr->suspend.stop_signal; 1962 thread_args.stop_signal = signalled_thr->suspend.stop_signal;
1963 1963
1964 linux_corefile_thread (signalled_thr, &thread_args); 1964 linux_corefile_thread (signalled_thr, &thread_args);
1965 ALL_NON_EXITED_THREADS (thr) 1965 for (thread_info *thr : current_inferior ()->non_exited_threads ())
1966 { 1966 {
1967 if (thr == signalled_thr) 1967 if (thr == signalled_thr)
1968 continue; 1968 continue;
1969 if (thr->ptid.pid () != inferior_ptid.pid ())
1970 continue;
1971 1969
1972 linux_corefile_thread (thr, &thread_args); 1970 linux_corefile_thread (thr, &thread_args);
1973 } 1971 }
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index ad193d6..74acec2 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -1587,11 +1587,10 @@ void
1587thread_db_target::update_thread_list () 1587thread_db_target::update_thread_list ()
1588{ 1588{
1589 struct thread_db_info *info; 1589 struct thread_db_info *info;
1590 struct inferior *inf;
1591 1590
1592 prune_threads (); 1591 prune_threads ();
1593 1592
1594 ALL_INFERIORS (inf) 1593 for (inferior *inf : all_inferiors ())
1595 { 1594 {
1596 struct thread_info *thread; 1595 struct thread_info *thread;
1597 1596
@@ -1671,7 +1670,6 @@ thread_db_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
1671 int handle_len, 1670 int handle_len,
1672 inferior *inf) 1671 inferior *inf)
1673{ 1672{
1674 struct thread_info *tp;
1675 thread_t handle_tid; 1673 thread_t handle_tid;
1676 1674
1677 /* Thread handle sizes must match in order to proceed. We don't use an 1675 /* 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,
1684 1682
1685 handle_tid = * (const thread_t *) thread_handle; 1683 handle_tid = * (const thread_t *) thread_handle;
1686 1684
1687 ALL_NON_EXITED_THREADS (tp) 1685 for (thread_info *tp : inf->non_exited_threads ())
1688 { 1686 {
1689 thread_db_thread_info *priv = get_thread_db_thread_info (tp); 1687 thread_db_thread_info *priv = get_thread_db_thread_info (tp);
1690 1688
1691 if (tp->inf == inf && priv != NULL && handle_tid == priv->tid) 1689 if (priv != NULL && handle_tid == priv->tid)
1692 return tp; 1690 return tp;
1693 } 1691 }
1694 1692
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index e055dce..9a317bc 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -951,6 +951,24 @@ mi_output_running (struct thread_info *thread)
951 } 951 }
952} 952}
953 953
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
958static bool
959multiple_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
954static void 972static void
955mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid) 973mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid)
956{ 974{
@@ -968,43 +986,15 @@ mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid)
968 current_token ? current_token : ""); 986 current_token ? current_token : "");
969 } 987 }
970 988
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 ())
972 fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n"); 994 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 }
1001 else 995 else
1002 { 996 for (thread_info *tp : all_non_exited_threads (ptid))
1003 thread_info *ti = find_thread_ptid (ptid); 997 mi_output_running (tp);
1004
1005 gdb_assert (ti);
1006 mi_output_running (ti);
1007 }
1008 998
1009 if (!running_result_record_printed && mi_proceeded) 999 if (!running_result_record_printed && mi_proceeded)
1010 { 1000 {
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 5562935..872870f 100644
--- 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)
587 { 587 {
588 ui_out_emit_tuple tuple_emitter (current_uiout, "thread-ids"); 588 ui_out_emit_tuple tuple_emitter (current_uiout, "thread-ids");
589 589
590 struct thread_info *tp; 590 for (thread_info *tp : all_non_exited_threads ())
591 ALL_NON_EXITED_THREADS (tp)
592 { 591 {
593 if (tp->ptid == inferior_ptid) 592 if (tp->ptid == inferior_ptid)
594 current_thread = tp->global_num; 593 current_thread = tp->global_num;
@@ -1995,7 +1994,7 @@ mi_execute_command (const char *cmd, int from_tty)
1995 top_level_interpreter ()->interp_ui_out ()->is_mi_like_p () 1994 top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ()
1996 /* Don't try report anything if there are no threads -- 1995 /* Don't try report anything if there are no threads --
1997 the program is dead. */ 1996 the program is dead. */
1998 && thread_count () != 0 1997 && any_thread_p ()
1999 /* If the command already reports the thread change, no need to do it 1998 /* If the command already reports the thread change, no need to do it
2000 again. */ 1999 again. */
2001 && !command_notifies_uscc_observer (command.get ())) 2000 && !command_notifies_uscc_observer (command.get ()))
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index ed2ef37..742222b 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -214,8 +214,6 @@ nto_procfs_target::open (const char *arg, int from_tty)
214 nto_procfs_node = ND_LOCAL_NODE; 214 nto_procfs_node = ND_LOCAL_NODE;
215 nodestr = (arg != NULL) ? xstrdup (arg) : NULL; 215 nodestr = (arg != NULL) ? xstrdup (arg) : NULL;
216 216
217 init_thread_list ();
218
219 if (nodestr) 217 if (nodestr)
220 { 218 {
221 nto_procfs_node = netmgr_strtond (nodestr, &endstr); 219 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index c0e3341..814f080 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -379,7 +379,6 @@ record_btrace_target_open (const char *args, int from_tty)
379 /* If we fail to enable btrace for one thread, disable it for the threads for 379 /* If we fail to enable btrace for one thread, disable it for the threads for
380 which it was successfully enabled. */ 380 which it was successfully enabled. */
381 scoped_btrace_disable btrace_disable; 381 scoped_btrace_disable btrace_disable;
382 struct thread_info *tp;
383 382
384 DEBUG ("open"); 383 DEBUG ("open");
385 384
@@ -388,7 +387,7 @@ record_btrace_target_open (const char *args, int from_tty)
388 if (!target_has_execution) 387 if (!target_has_execution)
389 error (_("The program is not being run.")); 388 error (_("The program is not being run."));
390 389
391 ALL_NON_EXITED_THREADS (tp) 390 for (thread_info *tp : all_non_exited_threads ())
392 if (args == NULL || *args == 0 || number_is_in_list (args, tp->global_num)) 391 if (args == NULL || *args == 0 || number_is_in_list (args, tp->global_num))
393 { 392 {
394 btrace_enable (tp, &record_btrace_conf); 393 btrace_enable (tp, &record_btrace_conf);
@@ -406,13 +405,11 @@ record_btrace_target_open (const char *args, int from_tty)
406void 405void
407record_btrace_target::stop_recording () 406record_btrace_target::stop_recording ()
408{ 407{
409 struct thread_info *tp;
410
411 DEBUG ("stop recording"); 408 DEBUG ("stop recording");
412 409
413 record_btrace_auto_disable (); 410 record_btrace_auto_disable ();
414 411
415 ALL_NON_EXITED_THREADS (tp) 412 for (thread_info *tp : all_non_exited_threads ())
416 if (tp->btrace.target != NULL) 413 if (tp->btrace.target != NULL)
417 btrace_disable (tp); 414 btrace_disable (tp);
418} 415}
@@ -437,8 +434,6 @@ record_btrace_target::disconnect (const char *args,
437void 434void
438record_btrace_target::close () 435record_btrace_target::close ()
439{ 436{
440 struct thread_info *tp;
441
442 if (record_btrace_async_inferior_event_handler != NULL) 437 if (record_btrace_async_inferior_event_handler != NULL)
443 delete_async_event_handler (&record_btrace_async_inferior_event_handler); 438 delete_async_event_handler (&record_btrace_async_inferior_event_handler);
444 439
@@ -448,7 +443,7 @@ record_btrace_target::close ()
448 443
449 /* We should have already stopped recording. 444 /* We should have already stopped recording.
450 Tear down btrace in case we have not. */ 445 Tear down btrace in case we have not. */
451 ALL_NON_EXITED_THREADS (tp) 446 for (thread_info *tp : all_non_exited_threads ())
452 btrace_teardown (tp); 447 btrace_teardown (tp);
453} 448}
454 449
@@ -1398,10 +1393,8 @@ record_btrace_target::record_method (ptid_t ptid)
1398bool 1393bool
1399record_btrace_target::record_is_replaying (ptid_t ptid) 1394record_btrace_target::record_is_replaying (ptid_t ptid)
1400{ 1395{
1401 struct thread_info *tp; 1396 for (thread_info *tp : all_non_exited_threads (ptid))
1402 1397 if (btrace_is_replaying (tp))
1403 ALL_NON_EXITED_THREADS (tp)
1404 if (tp->ptid.matches (ptid) && btrace_is_replaying (tp))
1405 return true; 1398 return true;
1406 1399
1407 return false; 1400 return false;
@@ -2129,7 +2122,6 @@ record_btrace_stop_replaying_at_end (struct thread_info *tp)
2129void 2122void
2130record_btrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal) 2123record_btrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
2131{ 2124{
2132 struct thread_info *tp;
2133 enum btrace_thread_flag flag, cflag; 2125 enum btrace_thread_flag flag, cflag;
2134 2126
2135 DEBUG ("resume %s: %s%s", target_pid_to_str (ptid), 2127 DEBUG ("resume %s: %s%s", target_pid_to_str (ptid),
@@ -2174,20 +2166,18 @@ record_btrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
2174 { 2166 {
2175 gdb_assert (inferior_ptid.matches (ptid)); 2167 gdb_assert (inferior_ptid.matches (ptid));
2176 2168
2177 ALL_NON_EXITED_THREADS (tp) 2169 for (thread_info *tp : all_non_exited_threads (ptid))
2178 if (tp->ptid.matches (ptid)) 2170 {
2179 { 2171 if (tp->ptid.matches (inferior_ptid))
2180 if (tp->ptid.matches (inferior_ptid)) 2172 record_btrace_resume_thread (tp, flag);
2181 record_btrace_resume_thread (tp, flag); 2173 else
2182 else 2174 record_btrace_resume_thread (tp, cflag);
2183 record_btrace_resume_thread (tp, cflag); 2175 }
2184 }
2185 } 2176 }
2186 else 2177 else
2187 { 2178 {
2188 ALL_NON_EXITED_THREADS (tp) 2179 for (thread_info *tp : all_non_exited_threads (ptid))
2189 if (tp->ptid.matches (ptid)) 2180 record_btrace_resume_thread (tp, flag);
2190 record_btrace_resume_thread (tp, flag);
2191 } 2181 }
2192 2182
2193 /* Async support. */ 2183 /* Async support. */
@@ -2544,16 +2534,9 @@ record_btrace_target::wait (ptid_t ptid, struct target_waitstatus *status,
2544 } 2534 }
2545 2535
2546 /* Keep a work list of moving threads. */ 2536 /* Keep a work list of moving threads. */
2547 { 2537 for (thread_info *tp : all_non_exited_threads (ptid))
2548 thread_info *tp; 2538 if ((tp->btrace.flags & (BTHR_MOVE | BTHR_STOP)) != 0)
2549 2539 moving.push_back (tp);
2550 ALL_NON_EXITED_THREADS (tp)
2551 {
2552 if (tp->ptid.matches (ptid)
2553 && ((tp->btrace.flags & (BTHR_MOVE | BTHR_STOP)) != 0))
2554 moving.push_back (tp);
2555 }
2556 }
2557 2540
2558 if (moving.empty ()) 2541 if (moving.empty ())
2559 { 2542 {
@@ -2634,9 +2617,7 @@ record_btrace_target::wait (ptid_t ptid, struct target_waitstatus *status,
2634 /* Stop all other threads. */ 2617 /* Stop all other threads. */
2635 if (!target_is_non_stop_p ()) 2618 if (!target_is_non_stop_p ())
2636 { 2619 {
2637 thread_info *tp; 2620 for (thread_info *tp : all_non_exited_threads ())
2638
2639 ALL_NON_EXITED_THREADS (tp)
2640 record_btrace_cancel_resume (tp); 2621 record_btrace_cancel_resume (tp);
2641 } 2622 }
2642 2623
@@ -2673,14 +2654,11 @@ record_btrace_target::stop (ptid_t ptid)
2673 } 2654 }
2674 else 2655 else
2675 { 2656 {
2676 struct thread_info *tp; 2657 for (thread_info *tp : all_non_exited_threads (ptid))
2677 2658 {
2678 ALL_NON_EXITED_THREADS (tp) 2659 tp->btrace.flags &= ~BTHR_MOVE;
2679 if (tp->ptid.matches (ptid)) 2660 tp->btrace.flags |= BTHR_STOP;
2680 { 2661 }
2681 tp->btrace.flags &= ~BTHR_MOVE;
2682 tp->btrace.flags |= BTHR_STOP;
2683 }
2684 } 2662 }
2685 } 2663 }
2686 2664
@@ -2873,9 +2851,7 @@ record_btrace_target::goto_record (ULONGEST insn)
2873void 2851void
2874record_btrace_target::record_stop_replaying () 2852record_btrace_target::record_stop_replaying ()
2875{ 2853{
2876 struct thread_info *tp; 2854 for (thread_info *tp : all_non_exited_threads ())
2877
2878 ALL_NON_EXITED_THREADS (tp)
2879 record_btrace_stop_replaying (tp); 2855 record_btrace_stop_replaying (tp);
2880} 2856}
2881 2857
diff --git a/gdb/record-full.c b/gdb/record-full.c
index 4d8988d..dbaa8c3 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -1201,8 +1201,6 @@ record_full_wait_1 (struct target_ops *ops,
1201 1201
1202 while (1) 1202 while (1)
1203 { 1203 {
1204 struct thread_info *tp;
1205
1206 ret = ops->beneath ()->wait (ptid, status, options); 1204 ret = ops->beneath ()->wait (ptid, status, options);
1207 if (status->kind == TARGET_WAITKIND_IGNORE) 1205 if (status->kind == TARGET_WAITKIND_IGNORE)
1208 { 1206 {
@@ -1213,7 +1211,7 @@ record_full_wait_1 (struct target_ops *ops,
1213 return ret; 1211 return ret;
1214 } 1212 }
1215 1213
1216 ALL_NON_EXITED_THREADS (tp) 1214 for (thread_info *tp : all_non_exited_threads ())
1217 delete_single_step_breakpoints (tp); 1215 delete_single_step_breakpoints (tp);
1218 1216
1219 if (record_full_resume_step) 1217 if (record_full_resume_step)
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 946035a..6e0e8c3 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -1573,9 +1573,6 @@ cooked_read_test (struct gdbarch *gdbarch)
1573 mock_inferior.aspace = &mock_aspace; 1573 mock_inferior.aspace = &mock_aspace;
1574 thread_info mock_thread (&mock_inferior, mock_ptid); 1574 thread_info mock_thread (&mock_inferior, mock_ptid);
1575 1575
1576 scoped_restore restore_thread_list
1577 = make_scoped_restore (&thread_list, &mock_thread);
1578
1579 /* Add the mock inferior to the inferior list so that look ups by 1576 /* Add the mock inferior to the inferior list so that look ups by
1580 target+ptid can find it. */ 1577 target+ptid can find it. */
1581 scoped_restore restore_inferior_list 1578 scoped_restore restore_inferior_list
diff --git a/gdb/remote-sim.c b/gdb/remote-sim.c
index d30b38e..63e4145 100644
--- a/gdb/remote-sim.c
+++ b/gdb/remote-sim.c
@@ -674,9 +674,6 @@ gdbsim_target::create_inferior (const char *exec_file,
674 built_argv.reset (arg_buf); 674 built_argv.reset (arg_buf);
675 } 675 }
676 676
677 if (!have_inferiors ())
678 init_thread_list ();
679
680 if (sim_create_inferior (sim_data->gdbsim_desc, exec_bfd, 677 if (sim_create_inferior (sim_data->gdbsim_desc, exec_bfd,
681 built_argv.get (), env) 678 built_argv.get (), env)
682 != SIM_RC_OK) 679 != SIM_RC_OK)
diff --git a/gdb/remote.c b/gdb/remote.c
index 6004509..90b5dab 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -3785,8 +3785,6 @@ remote_target::update_thread_list ()
3785 || remote_get_threads_with_qthreadinfo (&context) 3785 || remote_get_threads_with_qthreadinfo (&context)
3786 || remote_get_threads_with_ql (&context)) 3786 || remote_get_threads_with_ql (&context))
3787 { 3787 {
3788 struct thread_info *tp, *tmp;
3789
3790 got_list = 1; 3788 got_list = 1;
3791 3789
3792 if (context.items.empty () 3790 if (context.items.empty ()
@@ -3803,7 +3801,7 @@ remote_target::update_thread_list ()
3803 /* CONTEXT now holds the current thread list on the remote 3801 /* CONTEXT now holds the current thread list on the remote
3804 target end. Delete GDB-side threads no longer found on the 3802 target end. Delete GDB-side threads no longer found on the
3805 target. */ 3803 target. */
3806 ALL_THREADS_SAFE (tp, tmp) 3804 for (thread_info *tp : all_threads_safe ())
3807 { 3805 {
3808 if (!context.contains_thread (tp->ptid)) 3806 if (!context.contains_thread (tp->ptid))
3809 { 3807 {
@@ -3830,7 +3828,7 @@ remote_target::update_thread_list ()
3830 3828
3831 remote_notice_new_inferior (item.ptid, executing); 3829 remote_notice_new_inferior (item.ptid, executing);
3832 3830
3833 tp = find_thread_ptid (item.ptid); 3831 thread_info *tp = find_thread_ptid (item.ptid);
3834 remote_thread_info *info = get_remote_thread_info (tp); 3832 remote_thread_info *info = get_remote_thread_info (tp);
3835 info->core = item.core; 3833 info->core = item.core;
3836 info->extra = std::move (item.extra); 3834 info->extra = std::move (item.extra);
@@ -4385,8 +4383,6 @@ void
4385remote_target::process_initial_stop_replies (int from_tty) 4383remote_target::process_initial_stop_replies (int from_tty)
4386{ 4384{
4387 int pending_stop_replies = stop_reply_queue_length (); 4385 int pending_stop_replies = stop_reply_queue_length ();
4388 struct inferior *inf;
4389 struct thread_info *thread;
4390 struct thread_info *selected = NULL; 4386 struct thread_info *selected = NULL;
4391 struct thread_info *lowest_stopped = NULL; 4387 struct thread_info *lowest_stopped = NULL;
4392 struct thread_info *first = NULL; 4388 struct thread_info *first = NULL;
@@ -4453,16 +4449,13 @@ remote_target::process_initial_stop_replies (int from_tty)
4453 4449
4454 /* "Notice" the new inferiors before anything related to 4450 /* "Notice" the new inferiors before anything related to
4455 registers/memory. */ 4451 registers/memory. */
4456 ALL_INFERIORS (inf) 4452 for (inferior *inf : all_non_exited_inferiors ())
4457 { 4453 {
4458 if (inf->pid == 0)
4459 continue;
4460
4461 inf->needs_setup = 1; 4454 inf->needs_setup = 1;
4462 4455
4463 if (non_stop) 4456 if (non_stop)
4464 { 4457 {
4465 thread = any_live_thread_of_inferior (inf); 4458 thread_info *thread = any_live_thread_of_inferior (inf);
4466 notice_new_inferior (thread, thread->state == THREAD_RUNNING, 4459 notice_new_inferior (thread, thread->state == THREAD_RUNNING,
4467 from_tty); 4460 from_tty);
4468 } 4461 }
@@ -4477,14 +4470,11 @@ remote_target::process_initial_stop_replies (int from_tty)
4477 4470
4478 /* If all threads of an inferior were already stopped, we 4471 /* If all threads of an inferior were already stopped, we
4479 haven't setup the inferior yet. */ 4472 haven't setup the inferior yet. */
4480 ALL_INFERIORS (inf) 4473 for (inferior *inf : all_non_exited_inferiors ())
4481 { 4474 {
4482 if (inf->pid == 0)
4483 continue;
4484
4485 if (inf->needs_setup) 4475 if (inf->needs_setup)
4486 { 4476 {
4487 thread = any_live_thread_of_inferior (inf); 4477 thread_info *thread = any_live_thread_of_inferior (inf);
4488 switch_to_thread_no_regs (thread); 4478 switch_to_thread_no_regs (thread);
4489 setup_inferior (0); 4479 setup_inferior (0);
4490 } 4480 }
@@ -4494,7 +4484,7 @@ remote_target::process_initial_stop_replies (int from_tty)
4494 /* Now go over all threads that are stopped, and print their current 4484 /* Now go over all threads that are stopped, and print their current
4495 frame. If all-stop, then if there's a signalled thread, pick 4485 frame. If all-stop, then if there's a signalled thread, pick
4496 that as current. */ 4486 that as current. */
4497 ALL_NON_EXITED_THREADS (thread) 4487 for (thread_info *thread : all_non_exited_threads ())
4498 { 4488 {
4499 if (first == NULL) 4489 if (first == NULL)
4500 first = thread; 4490 first = thread;
@@ -4521,7 +4511,7 @@ remote_target::process_initial_stop_replies (int from_tty)
4521 others with their status pending. */ 4511 others with their status pending. */
4522 if (!non_stop) 4512 if (!non_stop)
4523 { 4513 {
4524 thread = selected; 4514 thread_info *thread = selected;
4525 if (thread == NULL) 4515 if (thread == NULL)
4526 thread = lowest_stopped; 4516 thread = lowest_stopped;
4527 if (thread == NULL) 4517 if (thread == NULL)
@@ -4531,7 +4521,7 @@ remote_target::process_initial_stop_replies (int from_tty)
4531 } 4521 }
4532 4522
4533 /* For "info program". */ 4523 /* For "info program". */
4534 thread = inferior_thread (); 4524 thread_info *thread = inferior_thread ();
4535 if (thread->state == THREAD_STOPPED) 4525 if (thread->state == THREAD_STOPPED)
4536 set_last_target_status (inferior_ptid, thread->suspend.waitstatus); 4526 set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
4537} 4527}
@@ -4730,7 +4720,7 @@ remote_target::start_remote (int from_tty, int extended_p)
4730 "warning: couldn't determine remote " 4720 "warning: couldn't determine remote "
4731 "current thread; picking first in list.\n"); 4721 "current thread; picking first in list.\n");
4732 4722
4733 inferior_ptid = thread_list->ptid; 4723 inferior_ptid = inferior_list->thread_list->ptid;
4734 } 4724 }
4735 } 4725 }
4736 4726
@@ -5612,9 +5602,6 @@ remote_target::open_1 (const char *name, int from_tty, int extended_p)
5612 /* First delete any symbols previously loaded from shared libraries. */ 5602 /* First delete any symbols previously loaded from shared libraries. */
5613 no_shared_libraries (NULL, 0); 5603 no_shared_libraries (NULL, 0);
5614 5604
5615 /* Start afresh. */
5616 init_thread_list ();
5617
5618 /* Start the remote connection. If error() or QUIT, discard this 5605 /* Start the remote connection. If error() or QUIT, discard this
5619 target (we'd otherwise be in an inconsistent state) and then 5606 target (we'd otherwise be in an inconsistent state) and then
5620 propogate the error on up the exception chain. This ensures that 5607 propogate the error on up the exception chain. This ensures that
@@ -6112,11 +6099,8 @@ char *
6112remote_target::append_pending_thread_resumptions (char *p, char *endp, 6099remote_target::append_pending_thread_resumptions (char *p, char *endp,
6113 ptid_t ptid) 6100 ptid_t ptid)
6114{ 6101{
6115 struct thread_info *thread; 6102 for (thread_info *thread : all_non_exited_threads (ptid))
6116 6103 if (inferior_ptid != thread->ptid
6117 ALL_NON_EXITED_THREADS (thread)
6118 if (thread->ptid.matches (ptid)
6119 && inferior_ptid != thread->ptid
6120 && thread->suspend.stop_signal != GDB_SIGNAL_0) 6104 && thread->suspend.stop_signal != GDB_SIGNAL_0)
6121 { 6105 {
6122 p = append_resumption (p, endp, thread->ptid, 6106 p = append_resumption (p, endp, thread->ptid,
@@ -6136,7 +6120,6 @@ remote_target::remote_resume_with_hc (ptid_t ptid, int step,
6136 gdb_signal siggnal) 6120 gdb_signal siggnal)
6137{ 6121{
6138 struct remote_state *rs = get_remote_state (); 6122 struct remote_state *rs = get_remote_state ();
6139 struct thread_info *thread;
6140 char *buf; 6123 char *buf;
6141 6124
6142 rs->last_sent_signal = siggnal; 6125 rs->last_sent_signal = siggnal;
@@ -6149,7 +6132,7 @@ remote_target::remote_resume_with_hc (ptid_t ptid, int step,
6149 else 6132 else
6150 set_continue_thread (ptid); 6133 set_continue_thread (ptid);
6151 6134
6152 ALL_NON_EXITED_THREADS (thread) 6135 for (thread_info *thread : all_non_exited_threads ())
6153 resume_clear_thread_private_info (thread); 6136 resume_clear_thread_private_info (thread);
6154 6137
6155 buf = rs->buf; 6138 buf = rs->buf;
@@ -6457,8 +6440,6 @@ vcont_builder::push_action (ptid_t ptid, bool step, gdb_signal siggnal)
6457void 6440void
6458remote_target::commit_resume () 6441remote_target::commit_resume ()
6459{ 6442{
6460 struct inferior *inf;
6461 struct thread_info *tp;
6462 int any_process_wildcard; 6443 int any_process_wildcard;
6463 int may_global_wildcard_vcont; 6444 int may_global_wildcard_vcont;
6464 6445
@@ -6521,7 +6502,7 @@ remote_target::commit_resume ()
6521 may_global_wildcard_vcont = 1; 6502 may_global_wildcard_vcont = 1;
6522 6503
6523 /* And assume every process is individually wildcard-able too. */ 6504 /* And assume every process is individually wildcard-able too. */
6524 ALL_NON_EXITED_INFERIORS (inf) 6505 for (inferior *inf : all_non_exited_inferiors ())
6525 { 6506 {
6526 remote_inferior *priv = get_remote_inferior (inf); 6507 remote_inferior *priv = get_remote_inferior (inf);
6527 6508
@@ -6532,7 +6513,7 @@ remote_target::commit_resume ()
6532 disable process and global wildcard resumes appropriately. */ 6513 disable process and global wildcard resumes appropriately. */
6533 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont); 6514 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont);
6534 6515
6535 ALL_NON_EXITED_THREADS (tp) 6516 for (thread_info *tp : all_non_exited_threads ())
6536 { 6517 {
6537 /* If a thread of a process is not meant to be resumed, then we 6518 /* If a thread of a process is not meant to be resumed, then we
6538 can't wildcard that process. */ 6519 can't wildcard that process. */
@@ -6561,7 +6542,7 @@ remote_target::commit_resume ()
6561 struct vcont_builder vcont_builder (this); 6542 struct vcont_builder vcont_builder (this);
6562 6543
6563 /* Threads first. */ 6544 /* Threads first. */
6564 ALL_NON_EXITED_THREADS (tp) 6545 for (thread_info *tp : all_non_exited_threads ())
6565 { 6546 {
6566 remote_thread_info *remote_thr = get_remote_thread_info (tp); 6547 remote_thread_info *remote_thr = get_remote_thread_info (tp);
6567 6548
@@ -6590,7 +6571,7 @@ remote_target::commit_resume ()
6590 supposed to be resumed. */ 6571 supposed to be resumed. */
6591 any_process_wildcard = 0; 6572 any_process_wildcard = 0;
6592 6573
6593 ALL_NON_EXITED_INFERIORS (inf) 6574 for (inferior *inf : all_non_exited_inferiors ())
6594 { 6575 {
6595 if (get_remote_inferior (inf)->may_wildcard_vcont) 6576 if (get_remote_inferior (inf)->may_wildcard_vcont)
6596 { 6577 {
@@ -6611,7 +6592,7 @@ remote_target::commit_resume ()
6611 } 6592 }
6612 else 6593 else
6613 { 6594 {
6614 ALL_NON_EXITED_INFERIORS (inf) 6595 for (inferior *inf : all_non_exited_inferiors ())
6615 { 6596 {
6616 if (get_remote_inferior (inf)->may_wildcard_vcont) 6597 if (get_remote_inferior (inf)->may_wildcard_vcont)
6617 { 6598 {
@@ -7018,13 +6999,12 @@ is_pending_fork_parent_thread (struct thread_info *thread)
7018void 6999void
7019remote_target::remove_new_fork_children (threads_listing_context *context) 7000remote_target::remove_new_fork_children (threads_listing_context *context)
7020{ 7001{
7021 struct thread_info * thread;
7022 int pid = -1; 7002 int pid = -1;
7023 struct notif_client *notif = &notif_client_stop; 7003 struct notif_client *notif = &notif_client_stop;
7024 7004
7025 /* For any threads stopped at a fork event, remove the corresponding 7005 /* For any threads stopped at a fork event, remove the corresponding
7026 fork child threads from the CONTEXT list. */ 7006 fork child threads from the CONTEXT list. */
7027 ALL_NON_EXITED_THREADS (thread) 7007 for (thread_info *thread : all_non_exited_threads ())
7028 { 7008 {
7029 struct target_waitstatus *ws = thread_pending_fork_status (thread); 7009 struct target_waitstatus *ws = thread_pending_fork_status (thread);
7030 7010
@@ -9716,12 +9696,11 @@ void
9716remote_target::kill_new_fork_children (int pid) 9696remote_target::kill_new_fork_children (int pid)
9717{ 9697{
9718 remote_state *rs = get_remote_state (); 9698 remote_state *rs = get_remote_state ();
9719 struct thread_info *thread;
9720 struct notif_client *notif = &notif_client_stop; 9699 struct notif_client *notif = &notif_client_stop;
9721 9700
9722 /* Kill the fork child threads of any threads in process PID 9701 /* Kill the fork child threads of any threads in process PID
9723 that are stopped at a fork event. */ 9702 that are stopped at a fork event. */
9724 ALL_NON_EXITED_THREADS (thread) 9703 for (thread_info *thread : all_non_exited_threads ())
9725 { 9704 {
9726 struct target_waitstatus *ws = &thread->pending_follow; 9705 struct target_waitstatus *ws = &thread->pending_follow;
9727 9706
@@ -10163,15 +10142,6 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
10163 extended_remote_restart (); 10142 extended_remote_restart ();
10164 } 10143 }
10165 10144
10166 if (!have_inferiors ())
10167 {
10168 /* Clean up from the last time we ran, before we mark the target
10169 running again. This will mark breakpoints uninserted, and
10170 get_offsets may insert breakpoints. */
10171 init_thread_list ();
10172 init_wait_for_inferior ();
10173 }
10174
10175 /* vRun's success return is a stop reply. */ 10145 /* vRun's success return is a stop reply. */
10176 stop_reply = run_worked ? rs->buf : NULL; 10146 stop_reply = run_worked ? rs->buf : NULL;
10177 add_current_inferior_and_thread (stop_reply); 10147 add_current_inferior_and_thread (stop_reply);
@@ -13770,7 +13740,6 @@ void
13770remote_target::remote_btrace_maybe_reopen () 13740remote_target::remote_btrace_maybe_reopen ()
13771{ 13741{
13772 struct remote_state *rs = get_remote_state (); 13742 struct remote_state *rs = get_remote_state ();
13773 struct thread_info *tp;
13774 int btrace_target_pushed = 0; 13743 int btrace_target_pushed = 0;
13775#if !defined (HAVE_LIBIPT) 13744#if !defined (HAVE_LIBIPT)
13776 int warned = 0; 13745 int warned = 0;
@@ -13778,7 +13747,7 @@ remote_target::remote_btrace_maybe_reopen ()
13778 13747
13779 scoped_restore_current_thread restore_thread; 13748 scoped_restore_current_thread restore_thread;
13780 13749
13781 ALL_NON_EXITED_THREADS (tp) 13750 for (thread_info *tp : all_non_exited_threads ())
13782 { 13751 {
13783 set_general_thread (tp->ptid); 13752 set_general_thread (tp->ptid);
13784 13753
@@ -14062,9 +14031,7 @@ remote_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
14062 int handle_len, 14031 int handle_len,
14063 inferior *inf) 14032 inferior *inf)
14064{ 14033{
14065 struct thread_info *tp; 14034 for (thread_info *tp : all_non_exited_threads ())
14066
14067 ALL_NON_EXITED_THREADS (tp)
14068 { 14035 {
14069 remote_thread_info *priv = get_remote_thread_info (tp); 14036 remote_thread_info *priv = get_remote_thread_info (tp);
14070 14037
diff --git a/gdb/target.c b/gdb/target.c
index 9404025..29ce5eb 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -473,9 +473,8 @@ target_terminal::restore_inferior (void)
473 473
474 { 474 {
475 scoped_restore_current_inferior restore_inferior; 475 scoped_restore_current_inferior restore_inferior;
476 struct inferior *inf;
477 476
478 ALL_INFERIORS (inf) 477 for (struct inferior *inf : all_inferiors ())
479 { 478 {
480 if (inf->terminal_state == target_terminal_state::is_ours_for_output) 479 if (inf->terminal_state == target_terminal_state::is_ours_for_output)
481 { 480 {
@@ -501,14 +500,13 @@ static void
501target_terminal_is_ours_kind (target_terminal_state desired_state) 500target_terminal_is_ours_kind (target_terminal_state desired_state)
502{ 501{
503 scoped_restore_current_inferior restore_inferior; 502 scoped_restore_current_inferior restore_inferior;
504 struct inferior *inf;
505 503
506 /* Must do this in two passes. First, have all inferiors save the 504 /* Must do this in two passes. First, have all inferiors save the
507 current terminal settings. Then, after all inferiors have add a 505 current terminal settings. Then, after all inferiors have add a
508 chance to safely save the terminal settings, restore GDB's 506 chance to safely save the terminal settings, restore GDB's
509 terminal settings. */ 507 terminal settings. */
510 508
511 ALL_INFERIORS (inf) 509 for (inferior *inf : all_inferiors ())
512 { 510 {
513 if (inf->terminal_state == target_terminal_state::is_inferior) 511 if (inf->terminal_state == target_terminal_state::is_inferior)
514 { 512 {
@@ -517,7 +515,7 @@ target_terminal_is_ours_kind (target_terminal_state desired_state)
517 } 515 }
518 } 516 }
519 517
520 ALL_INFERIORS (inf) 518 for (inferior *inf : all_inferiors ())
521 { 519 {
522 /* Note we don't check is_inferior here like above because we 520 /* Note we don't check is_inferior here like above because we
523 need to handle 'is_ours_for_output -> is_ours' too. Careful 521 need to handle 'is_ours_for_output -> is_ours' too. Careful
diff --git a/gdb/thread-iter.c b/gdb/thread-iter.c
new file mode 100644
index 0000000..bfe119a
--- /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
26all_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
36void
37all_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
58bool
59all_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
67all_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
81void
82all_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}
diff --git a/gdb/thread-iter.h b/gdb/thread-iter.h
new file mode 100644
index 0000000..446305f
--- /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
28class inf_threads_iterator
29{
30public:
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
61private:
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. */
69template<typename Iterator>
70struct basic_inf_threads_range
71{
72 friend struct inferior;
73private:
74 explicit basic_inf_threads_range (struct thread_info *head)
75 : m_head (head)
76 {}
77
78public:
79 Iterator begin () const { return Iterator (m_head); }
80 Iterator end () const { return Iterator (); }
81
82private:
83 thread_info *m_head;
84};
85
86/* A forward iterator that iterates over all threads of all
87 inferiors. */
88
89class all_threads_iterator
90{
91public:
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
125private:
126 /* Advance to the next thread. */
127 void advance ();
128
129private:
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
138class all_matching_threads_iterator
139{
140public:
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
173private:
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
181private:
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
194struct 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
204using 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
209using 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
214using all_threads_safe_iterator
215 = basic_safe_iterator<all_threads_iterator>;
216
217/* Iterate over all threads of an inferior, safely. */
218
219using 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
225using 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
231using 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
237using 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
243struct 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
255struct 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
266struct all_matching_threads_range
267{
268public:
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
281private:
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
291class all_non_exited_threads_range
292{
293public:
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
307private:
308 ptid_t m_filter_ptid;
309};
310
311#endif /* THREAD_ITER_H */
diff --git a/gdb/thread.c b/gdb/thread.c
index 5071fdb..48d605e 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -45,12 +45,12 @@
45#include "tid-parse.h" 45#include "tid-parse.h"
46#include <algorithm> 46#include <algorithm>
47#include "common/gdb_optional.h" 47#include "common/gdb_optional.h"
48#include "inline-frame.h"
48 49
49/* Definition of struct thread_info exported to gdbthread.h. */ 50/* Definition of struct thread_info exported to gdbthread.h. */
50 51
51/* Prototypes for local functions. */ 52/* Prototypes for local functions. */
52 53
53struct thread_info *thread_list = NULL;
54static int highest_thread_num; 54static int highest_thread_num;
55 55
56/* True if any thread is, or may be executing. We need to track this 56/* 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)
194 btrace_teardown (tp); 194 btrace_teardown (tp);
195 195
196 thread_cancel_execution_command (tp); 196 thread_cancel_execution_command (tp);
197
198 clear_inline_frame_state (tp->ptid);
197} 199}
198 200
199/* Set the TP's state as exited. */ 201/* Set the TP's state as exited. */
@@ -220,20 +222,19 @@ set_thread_exited (thread_info *tp, int silent)
220void 222void
221init_thread_list (void) 223init_thread_list (void)
222{ 224{
223 struct thread_info *tp, *tmp;
224
225 highest_thread_num = 0; 225 highest_thread_num = 0;
226 226
227 ALL_THREADS_SAFE (tp, tmp) 227 for (thread_info *tp : all_threads_safe ())
228 { 228 {
229 inferior *inf = tp->inf;
230
229 if (tp->deletable ()) 231 if (tp->deletable ())
230 delete tp; 232 delete tp;
231 else 233 else
232 set_thread_exited (tp, 1); 234 set_thread_exited (tp, 1);
233 }
234 235
235 thread_list = NULL; 236 inf->thread_list = NULL;
236 threads_executing = 0; 237 }
237} 238}
238 239
239/* Allocate a new thread of inferior INF with target id PTID and add 240/* 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)
244{ 245{
245 thread_info *tp = new thread_info (inf, ptid); 246 thread_info *tp = new thread_info (inf, ptid);
246 247
247 if (thread_list == NULL) 248 if (inf->thread_list == NULL)
248 thread_list = tp; 249 inf->thread_list = tp;
249 else 250 else
250 { 251 {
251 struct thread_info *last; 252 struct thread_info *last;
252 253
253 for (last = thread_list; last->next != NULL; last = last->next) 254 for (last = inf->thread_list; last->next != NULL; last = last->next)
254 ; 255 ;
255 last->next = tp; 256 last->next = tp;
256 } 257 }
@@ -261,11 +262,10 @@ new_thread (struct inferior *inf, ptid_t ptid)
261struct thread_info * 262struct thread_info *
262add_thread_silent (ptid_t ptid) 263add_thread_silent (ptid_t ptid)
263{ 264{
264 struct thread_info *tp;
265 struct inferior *inf = find_inferior_ptid (ptid); 265 struct inferior *inf = find_inferior_ptid (ptid);
266 gdb_assert (inf != NULL); 266 gdb_assert (inf != NULL);
267 267
268 tp = find_thread_ptid (ptid); 268 thread_info *tp = find_thread_ptid (inf, ptid);
269 if (tp) 269 if (tp)
270 /* Found an old thread with the same id. It has to be dead, 270 /* Found an old thread with the same id. It has to be dead,
271 otherwise we wouldn't be adding a new thread with the same id. 271 otherwise we wouldn't be adding a new thread with the same id.
@@ -352,6 +352,16 @@ thread_info::~thread_info ()
352 xfree (this->name); 352 xfree (this->name);
353} 353}
354 354
355/* See gdbthread.h. */
356
357bool
358thread_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
355/* Add TP to the end of the step-over chain LIST_P. */ 365/* Add TP to the end of the step-over chain LIST_P. */
356 366
357static void 367static void
@@ -442,7 +452,7 @@ delete_thread_1 (thread_info *thr, bool silent)
442 452
443 tpprev = NULL; 453 tpprev = NULL;
444 454
445 for (tp = thread_list; tp; tpprev = tp, tp = tp->next) 455 for (tp = thr->inf->thread_list; tp; tpprev = tp, tp = tp->next)
446 if (tp == thr) 456 if (tp == thr)
447 break; 457 break;
448 458
@@ -460,7 +470,7 @@ delete_thread_1 (thread_info *thr, bool silent)
460 if (tpprev) 470 if (tpprev)
461 tpprev->next = tp->next; 471 tpprev->next = tp->next;
462 else 472 else
463 thread_list = tp->next; 473 tp->inf->thread_list = tp->next;
464 474
465 delete tp; 475 delete tp;
466} 476}
@@ -485,9 +495,7 @@ delete_thread_silent (thread_info *thread)
485struct thread_info * 495struct thread_info *
486find_thread_global_id (int global_id) 496find_thread_global_id (int global_id)
487{ 497{
488 struct thread_info *tp; 498 for (thread_info *tp : all_threads ())
489
490 for (tp = thread_list; tp; tp = tp->next)
491 if (tp->global_num == global_id) 499 if (tp->global_num == global_id)
492 return tp; 500 return tp;
493 501
@@ -497,10 +505,8 @@ find_thread_global_id (int global_id)
497static struct thread_info * 505static struct thread_info *
498find_thread_id (struct inferior *inf, int thr_num) 506find_thread_id (struct inferior *inf, int thr_num)
499{ 507{
500 struct thread_info *tp; 508 for (thread_info *tp : inf->threads ())
501 509 if (tp->per_inf_num == thr_num)
502 for (tp = thread_list; tp; tp = tp->next)
503 if (tp->inf == inf && tp->per_inf_num == thr_num)
504 return tp; 510 return tp;
505 511
506 return NULL; 512 return NULL;
@@ -511,9 +517,18 @@ find_thread_id (struct inferior *inf, int thr_num)
511struct thread_info * 517struct thread_info *
512find_thread_ptid (ptid_t ptid) 518find_thread_ptid (ptid_t ptid)
513{ 519{
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}
515 525
516 for (tp = thread_list; tp; tp = tp->next) 526/* See gdbthread.h. */
527
528struct thread_info *
529find_thread_ptid (inferior *inf, ptid_t ptid)
530{
531 for (thread_info *tp : inf->threads ())
517 if (tp->ptid == ptid) 532 if (tp->ptid == ptid)
518 return tp; 533 return tp;
519 534
@@ -549,28 +564,28 @@ struct thread_info *
549iterate_over_threads (int (*callback) (struct thread_info *, void *), 564iterate_over_threads (int (*callback) (struct thread_info *, void *),
550 void *data) 565 void *data)
551{ 566{
552 struct thread_info *tp, *next; 567 for (thread_info *tp : all_threads_safe ())
553 568 if ((*callback) (tp, data))
554 for (tp = thread_list; tp; tp = next) 569 return tp;
555 {
556 next = tp->next;
557 if ((*callback) (tp, data))
558 return tp;
559 }
560 570
561 return NULL; 571 return NULL;
562} 572}
563 573
574/* See gdbthread.h. */
575
576bool
577any_thread_p ()
578{
579 for (thread_info *tp ATTRIBUTE_UNUSED : all_threads ())
580 return true;
581 return false;
582}
583
564int 584int
565thread_count (void) 585thread_count (void)
566{ 586{
567 int result = 0; 587 auto rng = all_threads ();
568 struct thread_info *tp; 588 return std::distance (rng.begin (), rng.end ());
569
570 for (tp = thread_list; tp; tp = tp->next)
571 ++result;
572
573 return result;
574} 589}
575 590
576/* Return the number of non-exited threads in the thread list. */ 591/* Return the number of non-exited threads in the thread list. */
@@ -578,21 +593,14 @@ thread_count (void)
578static int 593static int
579live_threads_count (void) 594live_threads_count (void)
580{ 595{
581 int result = 0; 596 auto rng = all_non_exited_threads ();
582 struct thread_info *tp; 597 return std::distance (rng.begin (), rng.end ());
583
584 ALL_NON_EXITED_THREADS (tp)
585 ++result;
586
587 return result;
588} 598}
589 599
590int 600int
591valid_global_thread_id (int global_id) 601valid_global_thread_id (int global_id)
592{ 602{
593 struct thread_info *tp; 603 for (thread_info *tp : all_threads ())
594
595 for (tp = thread_list; tp; tp = tp->next)
596 if (tp->global_num == global_id) 604 if (tp->global_num == global_id)
597 return 1; 605 return 1;
598 606
@@ -602,13 +610,7 @@ valid_global_thread_id (int global_id)
602int 610int
603in_thread_list (ptid_t ptid) 611in_thread_list (ptid_t ptid)
604{ 612{
605 struct thread_info *tp; 613 return find_thread_ptid (ptid) != nullptr;
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. */
612} 614}
613 615
614/* Finds the first thread of the inferior. */ 616/* Finds the first thread of the inferior. */
@@ -616,30 +618,20 @@ in_thread_list (ptid_t ptid)
616thread_info * 618thread_info *
617first_thread_of_inferior (inferior *inf) 619first_thread_of_inferior (inferior *inf)
618{ 620{
619 struct thread_info *tp, *ret = NULL; 621 return inf->thread_list;
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;
627} 622}
628 623
629thread_info * 624thread_info *
630any_thread_of_inferior (inferior *inf) 625any_thread_of_inferior (inferior *inf)
631{ 626{
632 struct thread_info *tp;
633
634 gdb_assert (inf->pid != 0); 627 gdb_assert (inf->pid != 0);
635 628
636 /* Prefer the current thread. */ 629 /* Prefer the current thread. */
637 if (inf == current_inferior ()) 630 if (inf == current_inferior ())
638 return inferior_thread (); 631 return inferior_thread ();
639 632
640 ALL_NON_EXITED_THREADS (tp) 633 for (thread_info *tp : inf->non_exited_threads ())
641 if (tp->inf == inf) 634 return tp;
642 return tp;
643 635
644 return NULL; 636 return NULL;
645} 637}
@@ -648,7 +640,6 @@ thread_info *
648any_live_thread_of_inferior (inferior *inf) 640any_live_thread_of_inferior (inferior *inf)
649{ 641{
650 struct thread_info *curr_tp = NULL; 642 struct thread_info *curr_tp = NULL;
651 struct thread_info *tp;
652 struct thread_info *tp_executing = NULL; 643 struct thread_info *tp_executing = NULL;
653 644
654 gdb_assert (inf != NULL && inf->pid != 0); 645 gdb_assert (inf != NULL && inf->pid != 0);
@@ -666,14 +657,13 @@ any_live_thread_of_inferior (inferior *inf)
666 return curr_tp; 657 return curr_tp;
667 } 658 }
668 659
669 ALL_NON_EXITED_THREADS (tp) 660 for (thread_info *tp : inf->non_exited_threads ())
670 if (tp->inf == inf) 661 {
671 { 662 if (!tp->executing)
672 if (!tp->executing) 663 return tp;
673 return tp;
674 664