GNU Binutils with patches for OS216
修订版 | 7eb4eee40d56a2b0dc8aadbf52b12c9c17d12ef7 (tree) |
---|---|
时间 | 2019-09-07 07:31:05 |
作者 | Pedro Alves <palves@redh...> |
Commiter | Pedro Alves |
switch inferior/thread before calling target methods
Once each inferior has its own target stack, we'll need to make sure
that the right inferior is selected before we call into target
methods.
It kind of sounds worse than it is in practice. Not that many places
need to be concerned.
In thread.c, we add a new switch_to_thread_if_alive function that
centralizes the switching before calls to target_thread_alive. Other
cases are handled with explicit switching.
gdb/ChangeLog:
yyyy-mm-dd Pedro Alves <palves@redhat.com>
* gdbthread.h (scoped_restore_current_thread)
<dont_restore, restore, m_dont_restore>: Declare.
* thread.c (thread_alive): Add assertion. Return bool.
(switch_to_thread_if_alive): New.
(prune_threads): Switch inferior/thread.
(print_thread_info_1): Switch thread before calling target methods.
(scoped_restore_current_thread::restore): New, factored out from
...
(scoped_restore_current_thread::~scoped_restore_current_thread):
... this.
(scoped_restore_current_thread::scoped_restore_current_thread):
Add assertion.
(thread_apply_all_command, thread_select): Use
switch_to_thread_if_alive.
* infrun.c (proceed, restart_threads, handle_signal_stop)
(switch_back_to_stepped_thread): Switch current thread before
calling target methods.
@@ -2949,6 +2949,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal) | ||
2949 | 2949 | { |
2950 | 2950 | for (thread_info *tp : all_non_exited_threads (resume_ptid)) |
2951 | 2951 | { |
2952 | + switch_to_thread_no_regs (tp); | |
2953 | + | |
2952 | 2954 | /* Ignore the current thread here. It's handled |
2953 | 2955 | afterwards. */ |
2954 | 2956 | if (tp == cur_thr) |
@@ -2966,6 +2968,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal) | ||
2966 | 2968 | |
2967 | 2969 | thread_step_over_chain_enqueue (tp); |
2968 | 2970 | } |
2971 | + | |
2972 | + switch_to_thread (cur_thr); | |
2969 | 2973 | } |
2970 | 2974 | |
2971 | 2975 | /* Enqueue the current thread last, so that we move all other |
@@ -3002,6 +3006,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal) | ||
3002 | 3006 | Start all other threads that are implicitly resumed too. */ |
3003 | 3007 | for (thread_info *tp : all_non_exited_threads (resume_ptid)) |
3004 | 3008 | { |
3009 | + switch_to_thread_no_regs (tp); | |
3010 | + | |
3005 | 3011 | if (tp->resumed) |
3006 | 3012 | { |
3007 | 3013 | if (debug_infrun) |
@@ -4347,6 +4353,7 @@ stop_all_threads (void) | ||
4347 | 4353 | "infrun: %s executing, " |
4348 | 4354 | "need stop\n", |
4349 | 4355 | target_pid_to_str (t->ptid).c_str ()); |
4356 | + switch_to_thread_no_regs (t); | |
4350 | 4357 | target_stop (t->ptid); |
4351 | 4358 | t->stop_requested = 1; |
4352 | 4359 | } |
@@ -5191,6 +5198,8 @@ restart_threads (struct thread_info *event_thread) | ||
5191 | 5198 | |
5192 | 5199 | for (thread_info *tp : all_non_exited_threads ()) |
5193 | 5200 | { |
5201 | + switch_to_thread_no_regs (tp); | |
5202 | + | |
5194 | 5203 | if (tp == event_thread) |
5195 | 5204 | { |
5196 | 5205 | if (debug_infrun) |
@@ -5449,9 +5458,8 @@ handle_signal_stop (struct execution_control_state *ecs) | ||
5449 | 5458 | { |
5450 | 5459 | struct regcache *regcache = get_thread_regcache (ecs->event_thread); |
5451 | 5460 | struct gdbarch *reg_gdbarch = regcache->arch (); |
5452 | - scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); | |
5453 | 5461 | |
5454 | - inferior_ptid = ecs->ptid; | |
5462 | + switch_to_thread (ecs->event_thread); | |
5455 | 5463 | |
5456 | 5464 | fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = %s\n", |
5457 | 5465 | paddress (reg_gdbarch, |
@@ -6885,6 +6893,8 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs) | ||
6885 | 6893 | |
6886 | 6894 | for (thread_info *tp : all_non_exited_threads ()) |
6887 | 6895 | { |
6896 | + switch_to_thread_no_regs (tp); | |
6897 | + | |
6888 | 6898 | /* Ignore threads of processes the caller is not |
6889 | 6899 | resuming. */ |
6890 | 6900 | if (!sched_multi |
@@ -6936,6 +6946,8 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs) | ||
6936 | 6946 | return 1; |
6937 | 6947 | } |
6938 | 6948 | } |
6949 | + | |
6950 | + switch_to_thread (ecs->event_thread); | |
6939 | 6951 | } |
6940 | 6952 | |
6941 | 6953 | return 0; |
@@ -62,8 +62,6 @@ static int highest_thread_num; | ||
62 | 62 | spawned new threads we haven't heard of yet. */ |
63 | 63 | static int threads_executing; |
64 | 64 | |
65 | -static int thread_alive (struct thread_info *); | |
66 | - | |
67 | 65 | /* RAII type used to increase / decrease the refcount of each thread |
68 | 66 | in a given list of threads. */ |
69 | 67 |
@@ -679,14 +677,38 @@ any_live_thread_of_inferior (inferior *inf) | ||
679 | 677 | } |
680 | 678 | |
681 | 679 | /* Return true if TP is an active thread. */ |
682 | -static int | |
683 | -thread_alive (struct thread_info *tp) | |
680 | +static bool | |
681 | +thread_alive (thread_info *tp) | |
684 | 682 | { |
685 | 683 | if (tp->state == THREAD_EXITED) |
686 | - return 0; | |
687 | - if (!target_thread_alive (tp->ptid)) | |
688 | - return 0; | |
689 | - return 1; | |
684 | + return false; | |
685 | + | |
686 | + /* Ensure we're looking at the right target stack. */ | |
687 | + gdb_assert (tp->inf == current_inferior ()); | |
688 | + | |
689 | + return target_thread_alive (tp->ptid); | |
690 | +} | |
691 | + | |
692 | +/* Switch to thread TP if it is alive. Returns true if successfully | |
693 | + switched, false otherwise. */ | |
694 | + | |
695 | +static bool | |
696 | +switch_to_thread_if_alive (thread_info *thr) | |
697 | +{ | |
698 | + scoped_restore_current_thread restore_thread; | |
699 | + | |
700 | + /* Switch inferior first, so that we're looking at the right target | |
701 | + stack. */ | |
702 | + switch_to_inferior_no_thread (thr->inf); | |
703 | + | |
704 | + if (thread_alive (thr)) | |
705 | + { | |
706 | + switch_to_thread (thr); | |
707 | + restore_thread.dont_restore (); | |
708 | + return true; | |
709 | + } | |
710 | + | |
711 | + return false; | |
690 | 712 | } |
691 | 713 | |
692 | 714 | /* See gdbthreads.h. */ |
@@ -694,9 +716,15 @@ thread_alive (struct thread_info *tp) | ||
694 | 716 | void |
695 | 717 | prune_threads (void) |
696 | 718 | { |
719 | + scoped_restore_current_thread restore_thread; | |
720 | + | |
697 | 721 | for (thread_info *tp : all_threads_safe ()) |
698 | - if (!thread_alive (tp)) | |
699 | - delete_thread (tp); | |
722 | + { | |
723 | + switch_to_inferior_no_thread (tp->inf); | |
724 | + | |
725 | + if (!thread_alive (tp)) | |
726 | + delete_thread (tp); | |
727 | + } | |
700 | 728 | } |
701 | 729 | |
702 | 730 | /* See gdbthreads.h. */ |
@@ -1037,6 +1065,9 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads, | ||
1037 | 1065 | gdb::optional<ui_out_emit_list> list_emitter; |
1038 | 1066 | gdb::optional<ui_out_emit_table> table_emitter; |
1039 | 1067 | |
1068 | + /* We'll be switching threads temporarily below. */ | |
1069 | + scoped_restore_current_thread restore_thread; | |
1070 | + | |
1040 | 1071 | if (uiout->is_mi_like_p ()) |
1041 | 1072 | list_emitter.emplace (uiout, "threads"); |
1042 | 1073 | else |
@@ -1054,6 +1085,10 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads, | ||
1054 | 1085 | |
1055 | 1086 | if (!uiout->is_mi_like_p ()) |
1056 | 1087 | { |
1088 | + /* Switch inferiors so we're looking at the right | |
1089 | + target stack. */ | |
1090 | + switch_to_inferior_no_thread (tp->inf); | |
1091 | + | |
1057 | 1092 | target_id_col_width |
1058 | 1093 | = std::max (target_id_col_width, |
1059 | 1094 | thread_target_id_str (tp).size ()); |
@@ -1085,9 +1120,6 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads, | ||
1085 | 1120 | uiout->table_body (); |
1086 | 1121 | } |
1087 | 1122 | |
1088 | - /* We'll be switching threads temporarily. */ | |
1089 | - scoped_restore_current_thread restore_thread; | |
1090 | - | |
1091 | 1123 | for (inferior *inf : all_inferiors ()) |
1092 | 1124 | for (thread_info *tp : inf->threads ()) |
1093 | 1125 | { |
@@ -1116,6 +1148,9 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads, | ||
1116 | 1148 | if (show_global_ids || uiout->is_mi_like_p ()) |
1117 | 1149 | uiout->field_signed ("id", tp->global_num); |
1118 | 1150 | |
1151 | + /* Switch to the thread (and inferior / target). */ | |
1152 | + switch_to_thread (tp); | |
1153 | + | |
1119 | 1154 | /* For the CLI, we stuff everything into the target-id field. |
1120 | 1155 | This is a gross hack to make the output come out looking |
1121 | 1156 | correct. The underlying problem here is that ui-out has no |
@@ -1147,9 +1182,8 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads, | ||
1147 | 1182 | uiout->text ("(running)\n"); |
1148 | 1183 | else |
1149 | 1184 | { |
1150 | - /* The switch below puts us at the top of the stack (leaf | |
1185 | + /* The switch above put us at the top of the stack (leaf | |
1151 | 1186 | frame). */ |
1152 | - switch_to_thread (tp); | |
1153 | 1187 | print_stack_frame (get_selected_frame (NULL), |
1154 | 1188 | /* For MI output, print frame level. */ |
1155 | 1189 | uiout->is_mi_like_p (), |
@@ -1662,7 +1696,7 @@ thread_apply_all_command (const char *cmd, int from_tty) | ||
1662 | 1696 | scoped_restore_current_thread restore_thread; |
1663 | 1697 | |
1664 | 1698 | for (thread_info *thr : thr_list_cpy) |
1665 | - if (thread_alive (thr)) | |
1699 | + if (switch_to_thread_if_alive (thr)) | |
1666 | 1700 | thr_try_catch_cmd (thr, cmd, from_tty, flags); |
1667 | 1701 | } |
1668 | 1702 | } |
@@ -1819,7 +1853,7 @@ thread_apply_command (const char *tidlist, int from_tty) | ||
1819 | 1853 | continue; |
1820 | 1854 | } |
1821 | 1855 | |
1822 | - if (!thread_alive (tp)) | |
1856 | + if (!switch_to_thread_if_alive (tp)) | |
1823 | 1857 | { |
1824 | 1858 | warning (_("Thread %s has terminated."), print_thread_id (tp)); |
1825 | 1859 | continue; |
@@ -1983,11 +2017,9 @@ show_print_thread_events (struct ui_file *file, int from_tty, | ||
1983 | 2017 | void |
1984 | 2018 | thread_select (const char *tidstr, thread_info *tp) |
1985 | 2019 | { |
1986 | - if (!thread_alive (tp)) | |
2020 | + if (!switch_to_thread_if_alive (tp)) | |
1987 | 2021 | error (_("Thread ID %s has terminated."), tidstr); |
1988 | 2022 | |
1989 | - switch_to_thread (tp); | |
1990 | - | |
1991 | 2023 | annotate_thread_changed (); |
1992 | 2024 | |
1993 | 2025 | /* Since the current thread may have changed, see if there is any |