• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

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

GNU Binutils with patches for OS216


Commit MetaInfo

修订版7eb4eee40d56a2b0dc8aadbf52b12c9c17d12ef7 (tree)
时间2019-09-07 07:31:05
作者Pedro Alves <palves@redh...>
CommiterPedro Alves

Log Message

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.

更改概述

差异

--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -2949,6 +2949,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
29492949 {
29502950 for (thread_info *tp : all_non_exited_threads (resume_ptid))
29512951 {
2952+ switch_to_thread_no_regs (tp);
2953+
29522954 /* Ignore the current thread here. It's handled
29532955 afterwards. */
29542956 if (tp == cur_thr)
@@ -2966,6 +2968,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
29662968
29672969 thread_step_over_chain_enqueue (tp);
29682970 }
2971+
2972+ switch_to_thread (cur_thr);
29692973 }
29702974
29712975 /* Enqueue the current thread last, so that we move all other
@@ -3002,6 +3006,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
30023006 Start all other threads that are implicitly resumed too. */
30033007 for (thread_info *tp : all_non_exited_threads (resume_ptid))
30043008 {
3009+ switch_to_thread_no_regs (tp);
3010+
30053011 if (tp->resumed)
30063012 {
30073013 if (debug_infrun)
@@ -4347,6 +4353,7 @@ stop_all_threads (void)
43474353 "infrun: %s executing, "
43484354 "need stop\n",
43494355 target_pid_to_str (t->ptid).c_str ());
4356+ switch_to_thread_no_regs (t);
43504357 target_stop (t->ptid);
43514358 t->stop_requested = 1;
43524359 }
@@ -5191,6 +5198,8 @@ restart_threads (struct thread_info *event_thread)
51915198
51925199 for (thread_info *tp : all_non_exited_threads ())
51935200 {
5201+ switch_to_thread_no_regs (tp);
5202+
51945203 if (tp == event_thread)
51955204 {
51965205 if (debug_infrun)
@@ -5449,9 +5458,8 @@ handle_signal_stop (struct execution_control_state *ecs)
54495458 {
54505459 struct regcache *regcache = get_thread_regcache (ecs->event_thread);
54515460 struct gdbarch *reg_gdbarch = regcache->arch ();
5452- scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
54535461
5454- inferior_ptid = ecs->ptid;
5462+ switch_to_thread (ecs->event_thread);
54555463
54565464 fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = %s\n",
54575465 paddress (reg_gdbarch,
@@ -6885,6 +6893,8 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
68856893
68866894 for (thread_info *tp : all_non_exited_threads ())
68876895 {
6896+ switch_to_thread_no_regs (tp);
6897+
68886898 /* Ignore threads of processes the caller is not
68896899 resuming. */
68906900 if (!sched_multi
@@ -6936,6 +6946,8 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
69366946 return 1;
69376947 }
69386948 }
6949+
6950+ switch_to_thread (ecs->event_thread);
69396951 }
69406952
69416953 return 0;
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -62,8 +62,6 @@ static int highest_thread_num;
6262 spawned new threads we haven't heard of yet. */
6363 static int threads_executing;
6464
65-static int thread_alive (struct thread_info *);
66-
6765 /* RAII type used to increase / decrease the refcount of each thread
6866 in a given list of threads. */
6967
@@ -679,14 +677,38 @@ any_live_thread_of_inferior (inferior *inf)
679677 }
680678
681679 /* 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)
684682 {
685683 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;
690712 }
691713
692714 /* See gdbthreads.h. */
@@ -694,9 +716,15 @@ thread_alive (struct thread_info *tp)
694716 void
695717 prune_threads (void)
696718 {
719+ scoped_restore_current_thread restore_thread;
720+
697721 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+ }
700728 }
701729
702730 /* See gdbthreads.h. */
@@ -1037,6 +1065,9 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
10371065 gdb::optional<ui_out_emit_list> list_emitter;
10381066 gdb::optional<ui_out_emit_table> table_emitter;
10391067
1068+ /* We'll be switching threads temporarily below. */
1069+ scoped_restore_current_thread restore_thread;
1070+
10401071 if (uiout->is_mi_like_p ())
10411072 list_emitter.emplace (uiout, "threads");
10421073 else
@@ -1054,6 +1085,10 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
10541085
10551086 if (!uiout->is_mi_like_p ())
10561087 {
1088+ /* Switch inferiors so we're looking at the right
1089+ target stack. */
1090+ switch_to_inferior_no_thread (tp->inf);
1091+
10571092 target_id_col_width
10581093 = std::max (target_id_col_width,
10591094 thread_target_id_str (tp).size ());
@@ -1085,9 +1120,6 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
10851120 uiout->table_body ();
10861121 }
10871122
1088- /* We'll be switching threads temporarily. */
1089- scoped_restore_current_thread restore_thread;
1090-
10911123 for (inferior *inf : all_inferiors ())
10921124 for (thread_info *tp : inf->threads ())
10931125 {
@@ -1116,6 +1148,9 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
11161148 if (show_global_ids || uiout->is_mi_like_p ())
11171149 uiout->field_signed ("id", tp->global_num);
11181150
1151+ /* Switch to the thread (and inferior / target). */
1152+ switch_to_thread (tp);
1153+
11191154 /* For the CLI, we stuff everything into the target-id field.
11201155 This is a gross hack to make the output come out looking
11211156 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,
11471182 uiout->text ("(running)\n");
11481183 else
11491184 {
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
11511186 frame). */
1152- switch_to_thread (tp);
11531187 print_stack_frame (get_selected_frame (NULL),
11541188 /* For MI output, print frame level. */
11551189 uiout->is_mi_like_p (),
@@ -1662,7 +1696,7 @@ thread_apply_all_command (const char *cmd, int from_tty)
16621696 scoped_restore_current_thread restore_thread;
16631697
16641698 for (thread_info *thr : thr_list_cpy)
1665- if (thread_alive (thr))
1699+ if (switch_to_thread_if_alive (thr))
16661700 thr_try_catch_cmd (thr, cmd, from_tty, flags);
16671701 }
16681702 }
@@ -1819,7 +1853,7 @@ thread_apply_command (const char *tidlist, int from_tty)
18191853 continue;
18201854 }
18211855
1822- if (!thread_alive (tp))
1856+ if (!switch_to_thread_if_alive (tp))
18231857 {
18241858 warning (_("Thread %s has terminated."), print_thread_id (tp));
18251859 continue;
@@ -1983,11 +2017,9 @@ show_print_thread_events (struct ui_file *file, int from_tty,
19832017 void
19842018 thread_select (const char *tidstr, thread_info *tp)
19852019 {
1986- if (!thread_alive (tp))
2020+ if (!switch_to_thread_if_alive (tp))
19872021 error (_("Thread ID %s has terminated."), tidstr);
19882022
1989- switch_to_thread (tp);
1990-
19912023 annotate_thread_changed ();
19922024
19932025 /* Since the current thread may have changed, see if there is any