e0125e11b329cfa2a34966ad68166533075a03fa
[binutils-gdb.git] / gdb / infrun.c
1 /* Target-struct-independent code to start (run) and stop an inferior
2 process.
3
4 Copyright (C) 1986-2023 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "displaced-stepping.h"
23 #include "infrun.h"
24 #include <ctype.h>
25 #include "symtab.h"
26 #include "frame.h"
27 #include "inferior.h"
28 #include "breakpoint.h"
29 #include "gdbcore.h"
30 #include "gdbcmd.h"
31 #include "target.h"
32 #include "target-connection.h"
33 #include "gdbthread.h"
34 #include "annotate.h"
35 #include "symfile.h"
36 #include "top.h"
37 #include "ui.h"
38 #include "inf-loop.h"
39 #include "regcache.h"
40 #include "value.h"
41 #include "observable.h"
42 #include "language.h"
43 #include "solib.h"
44 #include "main.h"
45 #include "block.h"
46 #include "mi/mi-common.h"
47 #include "event-top.h"
48 #include "record.h"
49 #include "record-full.h"
50 #include "inline-frame.h"
51 #include "jit.h"
52 #include "tracepoint.h"
53 #include "skip.h"
54 #include "probe.h"
55 #include "objfiles.h"
56 #include "completer.h"
57 #include "target-descriptions.h"
58 #include "target-dcache.h"
59 #include "terminal.h"
60 #include "solist.h"
61 #include "gdbsupport/event-loop.h"
62 #include "thread-fsm.h"
63 #include "gdbsupport/enum-flags.h"
64 #include "progspace-and-thread.h"
65 #include "gdbsupport/gdb_optional.h"
66 #include "arch-utils.h"
67 #include "gdbsupport/scope-exit.h"
68 #include "gdbsupport/forward-scope-exit.h"
69 #include "gdbsupport/gdb_select.h"
70 #include <unordered_map>
71 #include "async-event.h"
72 #include "gdbsupport/selftest.h"
73 #include "scoped-mock-context.h"
74 #include "test-target.h"
75 #include "gdbsupport/common-debug.h"
76 #include "gdbsupport/buildargv.h"
77 #include "extension.h"
78 #include "disasm.h"
79 #include "interps.h"
80
81 /* Prototypes for local functions */
82
83 static void sig_print_info (enum gdb_signal);
84
85 static void sig_print_header (void);
86
87 static void follow_inferior_reset_breakpoints (void);
88
89 static bool currently_stepping (struct thread_info *tp);
90
91 static void insert_hp_step_resume_breakpoint_at_frame (frame_info_ptr);
92
93 static void insert_step_resume_breakpoint_at_caller (frame_info_ptr);
94
95 static void insert_longjmp_resume_breakpoint (struct gdbarch *, CORE_ADDR);
96
97 static bool maybe_software_singlestep (struct gdbarch *gdbarch);
98
99 static void resume (gdb_signal sig);
100
101 static void wait_for_inferior (inferior *inf);
102
103 static void restart_threads (struct thread_info *event_thread,
104 inferior *inf = nullptr);
105
106 static bool start_step_over (void);
107
108 static bool step_over_info_valid_p (void);
109
110 /* Asynchronous signal handler registered as event loop source for
111 when we have pending events ready to be passed to the core. */
112 static struct async_event_handler *infrun_async_inferior_event_token;
113
114 /* Stores whether infrun_async was previously enabled or disabled.
115 Starts off as -1, indicating "never enabled/disabled". */
116 static int infrun_is_async = -1;
117
118 /* See infrun.h. */
119
120 void
121 infrun_async (int enable)
122 {
123 if (infrun_is_async != enable)
124 {
125 infrun_is_async = enable;
126
127 infrun_debug_printf ("enable=%d", enable);
128
129 if (enable)
130 mark_async_event_handler (infrun_async_inferior_event_token);
131 else
132 clear_async_event_handler (infrun_async_inferior_event_token);
133 }
134 }
135
136 /* See infrun.h. */
137
138 void
139 mark_infrun_async_event_handler (void)
140 {
141 mark_async_event_handler (infrun_async_inferior_event_token);
142 }
143
144 /* When set, stop the 'step' command if we enter a function which has
145 no line number information. The normal behavior is that we step
146 over such function. */
147 bool step_stop_if_no_debug = false;
148 static void
149 show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
150 struct cmd_list_element *c, const char *value)
151 {
152 gdb_printf (file, _("Mode of the step operation is %s.\n"), value);
153 }
154
155 /* proceed and normal_stop use this to notify the user when the
156 inferior stopped in a different thread than it had been running in.
157 It can also be used to find for which thread normal_stop last
158 reported a stop. */
159 static thread_info_ref previous_thread;
160
161 /* See infrun.h. */
162
163 void
164 update_previous_thread ()
165 {
166 if (inferior_ptid == null_ptid)
167 previous_thread = nullptr;
168 else
169 previous_thread = thread_info_ref::new_reference (inferior_thread ());
170 }
171
172 /* See infrun.h. */
173
174 thread_info *
175 get_previous_thread ()
176 {
177 return previous_thread.get ();
178 }
179
180 /* If set (default for legacy reasons), when following a fork, GDB
181 will detach from one of the fork branches, child or parent.
182 Exactly which branch is detached depends on 'set follow-fork-mode'
183 setting. */
184
185 static bool detach_fork = true;
186
187 bool debug_infrun = false;
188 static void
189 show_debug_infrun (struct ui_file *file, int from_tty,
190 struct cmd_list_element *c, const char *value)
191 {
192 gdb_printf (file, _("Inferior debugging is %s.\n"), value);
193 }
194
195 /* Support for disabling address space randomization. */
196
197 bool disable_randomization = true;
198
199 static void
200 show_disable_randomization (struct ui_file *file, int from_tty,
201 struct cmd_list_element *c, const char *value)
202 {
203 if (target_supports_disable_randomization ())
204 gdb_printf (file,
205 _("Disabling randomization of debuggee's "
206 "virtual address space is %s.\n"),
207 value);
208 else
209 gdb_puts (_("Disabling randomization of debuggee's "
210 "virtual address space is unsupported on\n"
211 "this platform.\n"), file);
212 }
213
214 static void
215 set_disable_randomization (const char *args, int from_tty,
216 struct cmd_list_element *c)
217 {
218 if (!target_supports_disable_randomization ())
219 error (_("Disabling randomization of debuggee's "
220 "virtual address space is unsupported on\n"
221 "this platform."));
222 }
223
224 /* User interface for non-stop mode. */
225
226 bool non_stop = false;
227 static bool non_stop_1 = false;
228
229 static void
230 set_non_stop (const char *args, int from_tty,
231 struct cmd_list_element *c)
232 {
233 if (target_has_execution ())
234 {
235 non_stop_1 = non_stop;
236 error (_("Cannot change this setting while the inferior is running."));
237 }
238
239 non_stop = non_stop_1;
240 }
241
242 static void
243 show_non_stop (struct ui_file *file, int from_tty,
244 struct cmd_list_element *c, const char *value)
245 {
246 gdb_printf (file,
247 _("Controlling the inferior in non-stop mode is %s.\n"),
248 value);
249 }
250
251 /* "Observer mode" is somewhat like a more extreme version of
252 non-stop, in which all GDB operations that might affect the
253 target's execution have been disabled. */
254
255 static bool observer_mode = false;
256 static bool observer_mode_1 = false;
257
258 static void
259 set_observer_mode (const char *args, int from_tty,
260 struct cmd_list_element *c)
261 {
262 if (target_has_execution ())
263 {
264 observer_mode_1 = observer_mode;
265 error (_("Cannot change this setting while the inferior is running."));
266 }
267
268 observer_mode = observer_mode_1;
269
270 may_write_registers = !observer_mode;
271 may_write_memory = !observer_mode;
272 may_insert_breakpoints = !observer_mode;
273 may_insert_tracepoints = !observer_mode;
274 /* We can insert fast tracepoints in or out of observer mode,
275 but enable them if we're going into this mode. */
276 if (observer_mode)
277 may_insert_fast_tracepoints = true;
278 may_stop = !observer_mode;
279 update_target_permissions ();
280
281 /* Going *into* observer mode we must force non-stop, then
282 going out we leave it that way. */
283 if (observer_mode)
284 {
285 pagination_enabled = false;
286 non_stop = non_stop_1 = true;
287 }
288
289 if (from_tty)
290 gdb_printf (_("Observer mode is now %s.\n"),
291 (observer_mode ? "on" : "off"));
292 }
293
294 static void
295 show_observer_mode (struct ui_file *file, int from_tty,
296 struct cmd_list_element *c, const char *value)
297 {
298 gdb_printf (file, _("Observer mode is %s.\n"), value);
299 }
300
301 /* This updates the value of observer mode based on changes in
302 permissions. Note that we are deliberately ignoring the values of
303 may-write-registers and may-write-memory, since the user may have
304 reason to enable these during a session, for instance to turn on a
305 debugging-related global. */
306
307 void
308 update_observer_mode (void)
309 {
310 bool newval = (!may_insert_breakpoints
311 && !may_insert_tracepoints
312 && may_insert_fast_tracepoints
313 && !may_stop
314 && non_stop);
315
316 /* Let the user know if things change. */
317 if (newval != observer_mode)
318 gdb_printf (_("Observer mode is now %s.\n"),
319 (newval ? "on" : "off"));
320
321 observer_mode = observer_mode_1 = newval;
322 }
323
324 /* Tables of how to react to signals; the user sets them. */
325
326 static unsigned char signal_stop[GDB_SIGNAL_LAST];
327 static unsigned char signal_print[GDB_SIGNAL_LAST];
328 static unsigned char signal_program[GDB_SIGNAL_LAST];
329
330 /* Table of signals that are registered with "catch signal". A
331 non-zero entry indicates that the signal is caught by some "catch
332 signal" command. */
333 static unsigned char signal_catch[GDB_SIGNAL_LAST];
334
335 /* Table of signals that the target may silently handle.
336 This is automatically determined from the flags above,
337 and simply cached here. */
338 static unsigned char signal_pass[GDB_SIGNAL_LAST];
339
340 #define SET_SIGS(nsigs,sigs,flags) \
341 do { \
342 int signum = (nsigs); \
343 while (signum-- > 0) \
344 if ((sigs)[signum]) \
345 (flags)[signum] = 1; \
346 } while (0)
347
348 #define UNSET_SIGS(nsigs,sigs,flags) \
349 do { \
350 int signum = (nsigs); \
351 while (signum-- > 0) \
352 if ((sigs)[signum]) \
353 (flags)[signum] = 0; \
354 } while (0)
355
356 /* Update the target's copy of SIGNAL_PROGRAM. The sole purpose of
357 this function is to avoid exporting `signal_program'. */
358
359 void
360 update_signals_program_target (void)
361 {
362 target_program_signals (signal_program);
363 }
364
365 /* Value to pass to target_resume() to cause all threads to resume. */
366
367 #define RESUME_ALL minus_one_ptid
368
369 /* Command list pointer for the "stop" placeholder. */
370
371 static struct cmd_list_element *stop_command;
372
373 /* Nonzero if we want to give control to the user when we're notified
374 of shared library events by the dynamic linker. */
375 int stop_on_solib_events;
376
377 /* Enable or disable optional shared library event breakpoints
378 as appropriate when the above flag is changed. */
379
380 static void
381 set_stop_on_solib_events (const char *args,
382 int from_tty, struct cmd_list_element *c)
383 {
384 update_solib_breakpoints ();
385 }
386
387 static void
388 show_stop_on_solib_events (struct ui_file *file, int from_tty,
389 struct cmd_list_element *c, const char *value)
390 {
391 gdb_printf (file, _("Stopping for shared library events is %s.\n"),
392 value);
393 }
394
395 /* True after stop if current stack frame should be printed. */
396
397 static bool stop_print_frame;
398
399 /* This is a cached copy of the target/ptid/waitstatus of the last
400 event returned by target_wait().
401 This information is returned by get_last_target_status(). */
402 static process_stratum_target *target_last_proc_target;
403 static ptid_t target_last_wait_ptid;
404 static struct target_waitstatus target_last_waitstatus;
405
406 void init_thread_stepping_state (struct thread_info *tss);
407
408 static const char follow_fork_mode_child[] = "child";
409 static const char follow_fork_mode_parent[] = "parent";
410
411 static const char *const follow_fork_mode_kind_names[] = {
412 follow_fork_mode_child,
413 follow_fork_mode_parent,
414 nullptr
415 };
416
417 static const char *follow_fork_mode_string = follow_fork_mode_parent;
418 static void
419 show_follow_fork_mode_string (struct ui_file *file, int from_tty,
420 struct cmd_list_element *c, const char *value)
421 {
422 gdb_printf (file,
423 _("Debugger response to a program "
424 "call of fork or vfork is \"%s\".\n"),
425 value);
426 }
427 \f
428
429 /* Handle changes to the inferior list based on the type of fork,
430 which process is being followed, and whether the other process
431 should be detached. On entry inferior_ptid must be the ptid of
432 the fork parent. At return inferior_ptid is the ptid of the
433 followed inferior. */
434
435 static bool
436 follow_fork_inferior (bool follow_child, bool detach_fork)
437 {
438 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
439
440 infrun_debug_printf ("follow_child = %d, detach_fork = %d",
441 follow_child, detach_fork);
442
443 target_waitkind fork_kind = inferior_thread ()->pending_follow.kind ();
444 gdb_assert (fork_kind == TARGET_WAITKIND_FORKED
445 || fork_kind == TARGET_WAITKIND_VFORKED);
446 bool has_vforked = fork_kind == TARGET_WAITKIND_VFORKED;
447 ptid_t parent_ptid = inferior_ptid;
448 ptid_t child_ptid = inferior_thread ()->pending_follow.child_ptid ();
449
450 if (has_vforked
451 && !non_stop /* Non-stop always resumes both branches. */
452 && current_ui->prompt_state == PROMPT_BLOCKED
453 && !(follow_child || detach_fork || sched_multi))
454 {
455 /* The parent stays blocked inside the vfork syscall until the
456 child execs or exits. If we don't let the child run, then
457 the parent stays blocked. If we're telling the parent to run
458 in the foreground, the user will not be able to ctrl-c to get
459 back the terminal, effectively hanging the debug session. */
460 gdb_printf (gdb_stderr, _("\
461 Can not resume the parent process over vfork in the foreground while\n\
462 holding the child stopped. Try \"set detach-on-fork\" or \
463 \"set schedule-multiple\".\n"));
464 return true;
465 }
466
467 inferior *parent_inf = current_inferior ();
468 inferior *child_inf = nullptr;
469
470 gdb_assert (parent_inf->thread_waiting_for_vfork_done == nullptr);
471
472 if (!follow_child)
473 {
474 /* Detach new forked process? */
475 if (detach_fork)
476 {
477 /* Before detaching from the child, remove all breakpoints
478 from it. If we forked, then this has already been taken
479 care of by infrun.c. If we vforked however, any
480 breakpoint inserted in the parent is visible in the
481 child, even those added while stopped in a vfork
482 catchpoint. This will remove the breakpoints from the
483 parent also, but they'll be reinserted below. */
484 if (has_vforked)
485 {
486 /* Keep breakpoints list in sync. */
487 remove_breakpoints_inf (current_inferior ());
488 }
489
490 if (print_inferior_events)
491 {
492 /* Ensure that we have a process ptid. */
493 ptid_t process_ptid = ptid_t (child_ptid.pid ());
494
495 target_terminal::ours_for_output ();
496 gdb_printf (_("[Detaching after %s from child %s]\n"),
497 has_vforked ? "vfork" : "fork",
498 target_pid_to_str (process_ptid).c_str ());
499 }
500 }
501 else
502 {
503 /* Add process to GDB's tables. */
504 child_inf = add_inferior (child_ptid.pid ());
505
506 child_inf->attach_flag = parent_inf->attach_flag;
507 copy_terminal_info (child_inf, parent_inf);
508 child_inf->set_arch (parent_inf->arch ());
509 child_inf->tdesc_info = parent_inf->tdesc_info;
510
511 child_inf->symfile_flags = SYMFILE_NO_READ;
512
513 /* If this is a vfork child, then the address-space is
514 shared with the parent. */
515 if (has_vforked)
516 {
517 child_inf->pspace = parent_inf->pspace;
518 child_inf->aspace = parent_inf->aspace;
519
520 exec_on_vfork (child_inf);
521
522 /* The parent will be frozen until the child is done
523 with the shared region. Keep track of the
524 parent. */
525 child_inf->vfork_parent = parent_inf;
526 child_inf->pending_detach = false;
527 parent_inf->vfork_child = child_inf;
528 parent_inf->pending_detach = false;
529 }
530 else
531 {
532 child_inf->aspace = new address_space ();
533 child_inf->pspace = new program_space (child_inf->aspace);
534 child_inf->removable = true;
535 clone_program_space (child_inf->pspace, parent_inf->pspace);
536 }
537 }
538
539 if (has_vforked)
540 {
541 /* If we detached from the child, then we have to be careful
542 to not insert breakpoints in the parent until the child
543 is done with the shared memory region. However, if we're
544 staying attached to the child, then we can and should
545 insert breakpoints, so that we can debug it. A
546 subsequent child exec or exit is enough to know when does
547 the child stops using the parent's address space. */
548 parent_inf->thread_waiting_for_vfork_done
549 = detach_fork ? inferior_thread () : nullptr;
550 parent_inf->pspace->breakpoints_not_allowed = detach_fork;
551
552 infrun_debug_printf
553 ("parent_inf->thread_waiting_for_vfork_done == %s",
554 (parent_inf->thread_waiting_for_vfork_done == nullptr
555 ? "nullptr"
556 : (parent_inf->thread_waiting_for_vfork_done
557 ->ptid.to_string ().c_str ())));
558 }
559 }
560 else
561 {
562 /* Follow the child. */
563
564 if (print_inferior_events)
565 {
566 std::string parent_pid = target_pid_to_str (parent_ptid);
567 std::string child_pid = target_pid_to_str (child_ptid);
568
569 target_terminal::ours_for_output ();
570 gdb_printf (_("[Attaching after %s %s to child %s]\n"),
571 parent_pid.c_str (),
572 has_vforked ? "vfork" : "fork",
573 child_pid.c_str ());
574 }
575
576 /* Add the new inferior first, so that the target_detach below
577 doesn't unpush the target. */
578
579 child_inf = add_inferior (child_ptid.pid ());
580
581 child_inf->attach_flag = parent_inf->attach_flag;
582 copy_terminal_info (child_inf, parent_inf);
583 child_inf->set_arch (parent_inf->arch ());
584 child_inf->tdesc_info = parent_inf->tdesc_info;
585
586 if (has_vforked)
587 {
588 /* If this is a vfork child, then the address-space is shared
589 with the parent. */
590 child_inf->aspace = parent_inf->aspace;
591 child_inf->pspace = parent_inf->pspace;
592
593 exec_on_vfork (child_inf);
594 }
595 else if (detach_fork)
596 {
597 /* We follow the child and detach from the parent: move the parent's
598 program space to the child. This simplifies some things, like
599 doing "next" over fork() and landing on the expected line in the
600 child (note, that is broken with "set detach-on-fork off").
601
602 Before assigning brand new spaces for the parent, remove
603 breakpoints from it: because the new pspace won't match
604 currently inserted locations, the normal detach procedure
605 wouldn't remove them, and we would leave them inserted when
606 detaching. */
607 remove_breakpoints_inf (parent_inf);
608
609 child_inf->aspace = parent_inf->aspace;
610 child_inf->pspace = parent_inf->pspace;
611 parent_inf->aspace = new address_space ();
612 parent_inf->pspace = new program_space (parent_inf->aspace);
613 clone_program_space (parent_inf->pspace, child_inf->pspace);
614
615 /* The parent inferior is still the current one, so keep things
616 in sync. */
617 set_current_program_space (parent_inf->pspace);
618 }
619 else
620 {
621 child_inf->aspace = new address_space ();
622 child_inf->pspace = new program_space (child_inf->aspace);
623 child_inf->removable = true;
624 child_inf->symfile_flags = SYMFILE_NO_READ;
625 clone_program_space (child_inf->pspace, parent_inf->pspace);
626 }
627 }
628
629 gdb_assert (current_inferior () == parent_inf);
630
631 /* If we are setting up an inferior for the child, target_follow_fork is
632 responsible for pushing the appropriate targets on the new inferior's
633 target stack and adding the initial thread (with ptid CHILD_PTID).
634
635 If we are not setting up an inferior for the child (because following
636 the parent and detach_fork is true), it is responsible for detaching
637 from CHILD_PTID. */
638 target_follow_fork (child_inf, child_ptid, fork_kind, follow_child,
639 detach_fork);
640
641 gdb::observers::inferior_forked.notify (parent_inf, child_inf, fork_kind);
642
643 /* target_follow_fork must leave the parent as the current inferior. If we
644 want to follow the child, we make it the current one below. */
645 gdb_assert (current_inferior () == parent_inf);
646
647 /* If there is a child inferior, target_follow_fork must have created a thread
648 for it. */
649 if (child_inf != nullptr)
650 gdb_assert (!child_inf->thread_list.empty ());
651
652 /* Clear the parent thread's pending follow field. Do this before calling
653 target_detach, so that the target can differentiate the two following
654 cases:
655
656 - We continue past a fork with "follow-fork-mode == child" &&
657 "detach-on-fork on", and therefore detach the parent. In that
658 case the target should not detach the fork child.
659 - We run to a fork catchpoint and the user types "detach". In that
660 case, the target should detach the fork child in addition to the
661 parent.
662
663 The former case will have pending_follow cleared, the later will have
664 pending_follow set. */
665 thread_info *parent_thread = parent_inf->find_thread (parent_ptid);
666 gdb_assert (parent_thread != nullptr);
667 parent_thread->pending_follow.set_spurious ();
668
669 /* Detach the parent if needed. */
670 if (follow_child)
671 {
672 /* If we're vforking, we want to hold on to the parent until
673 the child exits or execs. At child exec or exit time we
674 can remove the old breakpoints from the parent and detach
675 or resume debugging it. Otherwise, detach the parent now;
676 we'll want to reuse it's program/address spaces, but we
677 can't set them to the child before removing breakpoints
678 from the parent, otherwise, the breakpoints module could
679 decide to remove breakpoints from the wrong process (since
680 they'd be assigned to the same address space). */
681
682 if (has_vforked)
683 {
684 gdb_assert (child_inf->vfork_parent == nullptr);
685 gdb_assert (parent_inf->vfork_child == nullptr);
686 child_inf->vfork_parent = parent_inf;
687 child_inf->pending_detach = false;
688 parent_inf->vfork_child = child_inf;
689 parent_inf->pending_detach = detach_fork;
690 }
691 else if (detach_fork)
692 {
693 if (print_inferior_events)
694 {
695 /* Ensure that we have a process ptid. */
696 ptid_t process_ptid = ptid_t (parent_ptid.pid ());
697
698 target_terminal::ours_for_output ();
699 gdb_printf (_("[Detaching after fork from "
700 "parent %s]\n"),
701 target_pid_to_str (process_ptid).c_str ());
702 }
703
704 target_detach (parent_inf, 0);
705 }
706 }
707
708 /* If we ended up creating a new inferior, call post_create_inferior to inform
709 the various subcomponents. */
710 if (child_inf != nullptr)
711 {
712 /* If FOLLOW_CHILD, we leave CHILD_INF as the current inferior
713 (do not restore the parent as the current inferior). */
714 gdb::optional<scoped_restore_current_thread> maybe_restore;
715
716 if (!follow_child && !sched_multi)
717 maybe_restore.emplace ();
718
719 switch_to_thread (*child_inf->threads ().begin ());
720 post_create_inferior (0);
721 }
722
723 return false;
724 }
725
726 /* Set the last target status as TP having stopped. */
727
728 static void
729 set_last_target_status_stopped (thread_info *tp)
730 {
731 set_last_target_status (tp->inf->process_target (), tp->ptid,
732 target_waitstatus {}.set_stopped (GDB_SIGNAL_0));
733 }
734
735 /* Tell the target to follow the fork we're stopped at. Returns true
736 if the inferior should be resumed; false, if the target for some
737 reason decided it's best not to resume. */
738
739 static bool
740 follow_fork ()
741 {
742 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
743
744 bool follow_child = (follow_fork_mode_string == follow_fork_mode_child);
745 bool should_resume = true;
746
747 /* Copy user stepping state to the new inferior thread. FIXME: the
748 followed fork child thread should have a copy of most of the
749 parent thread structure's run control related fields, not just these.
750 Initialized to avoid "may be used uninitialized" warnings from gcc. */
751 struct breakpoint *step_resume_breakpoint = nullptr;
752 struct breakpoint *exception_resume_breakpoint = nullptr;
753 CORE_ADDR step_range_start = 0;
754 CORE_ADDR step_range_end = 0;
755 int current_line = 0;
756 symtab *current_symtab = nullptr;
757 struct frame_id step_frame_id = { 0 };
758
759 if (!non_stop)
760 {
761 thread_info *cur_thr = inferior_thread ();
762
763 ptid_t resume_ptid
764 = user_visible_resume_ptid (cur_thr->control.stepping_command);
765 process_stratum_target *resume_target
766 = user_visible_resume_target (resume_ptid);
767
768 /* Check if there's a thread that we're about to resume, other
769 than the current, with an unfollowed fork/vfork. If so,
770 switch back to it, to tell the target to follow it (in either
771 direction). We'll afterwards refuse to resume, and inform
772 the user what happened. */
773 for (thread_info *tp : all_non_exited_threads (resume_target,
774 resume_ptid))
775 {
776 if (tp == cur_thr)
777 continue;
778
779 /* follow_fork_inferior clears tp->pending_follow, and below
780 we'll need the value after the follow_fork_inferior
781 call. */
782 target_waitkind kind = tp->pending_follow.kind ();
783
784 if (kind != TARGET_WAITKIND_SPURIOUS)
785 {
786 infrun_debug_printf ("need to follow-fork [%s] first",
787 tp->ptid.to_string ().c_str ());
788
789 switch_to_thread (tp);
790
791 /* Set up inferior(s) as specified by the caller, and
792 tell the target to do whatever is necessary to follow
793 either parent or child. */
794 if (follow_child)
795 {
796 /* The thread that started the execution command
797 won't exist in the child. Abort the command and
798 immediately stop in this thread, in the child,
799 inside fork. */
800 should_resume = false;
801 }
802 else
803 {
804 /* Following the parent, so let the thread fork its
805 child freely, it won't influence the current
806 execution command. */
807 if (follow_fork_inferior (follow_child, detach_fork))
808 {
809 /* Target refused to follow, or there's some
810 other reason we shouldn't resume. */
811 switch_to_thread (cur_thr);
812 set_last_target_status_stopped (cur_thr);
813 return false;
814 }
815
816 /* If we're following a vfork, when we need to leave
817 the just-forked thread as selected, as we need to
818 solo-resume it to collect the VFORK_DONE event.
819 If we're following a fork, however, switch back
820 to the original thread that we continue stepping
821 it, etc. */
822 if (kind != TARGET_WAITKIND_VFORKED)
823 {
824 gdb_assert (kind == TARGET_WAITKIND_FORKED);
825 switch_to_thread (cur_thr);
826 }
827 }
828
829 break;
830 }
831 }
832 }
833
834 thread_info *tp = inferior_thread ();
835
836 /* If there were any forks/vforks that were caught and are now to be
837 followed, then do so now. */
838 switch (tp->pending_follow.kind ())
839 {
840 case TARGET_WAITKIND_FORKED:
841 case TARGET_WAITKIND_VFORKED:
842 {
843 ptid_t parent, child;
844 std::unique_ptr<struct thread_fsm> thread_fsm;
845
846 /* If the user did a next/step, etc, over a fork call,
847 preserve the stepping state in the fork child. */
848 if (follow_child && should_resume)
849 {
850 step_resume_breakpoint = clone_momentary_breakpoint
851 (tp->control.step_resume_breakpoint);
852 step_range_start = tp->control.step_range_start;
853 step_range_end = tp->control.step_range_end;
854 current_line = tp->current_line;
855 current_symtab = tp->current_symtab;
856 step_frame_id = tp->control.step_frame_id;
857 exception_resume_breakpoint
858 = clone_momentary_breakpoint (tp->control.exception_resume_breakpoint);
859 thread_fsm = tp->release_thread_fsm ();
860
861 /* For now, delete the parent's sr breakpoint, otherwise,
862 parent/child sr breakpoints are considered duplicates,
863 and the child version will not be installed. Remove
864 this when the breakpoints module becomes aware of
865 inferiors and address spaces. */
866 delete_step_resume_breakpoint (tp);
867 tp->control.step_range_start = 0;
868 tp->control.step_range_end = 0;
869 tp->control.step_frame_id = null_frame_id;
870 delete_exception_resume_breakpoint (tp);
871 }
872
873 parent = inferior_ptid;
874 child = tp->pending_follow.child_ptid ();
875
876 /* If handling a vfork, stop all the inferior's threads, they will be
877 restarted when the vfork shared region is complete. */
878 if (tp->pending_follow.kind () == TARGET_WAITKIND_VFORKED
879 && target_is_non_stop_p ())
880 stop_all_threads ("handling vfork", tp->inf);
881
882 process_stratum_target *parent_targ = tp->inf->process_target ();
883 /* Set up inferior(s) as specified by the caller, and tell the
884 target to do whatever is necessary to follow either parent
885 or child. */
886 if (follow_fork_inferior (follow_child, detach_fork))
887 {
888 /* Target refused to follow, or there's some other reason
889 we shouldn't resume. */
890 should_resume = 0;
891 }
892 else
893 {
894 /* If we followed the child, switch to it... */
895 if (follow_child)
896 {
897 tp = parent_targ->find_thread (child);
898 switch_to_thread (tp);
899
900 /* ... and preserve the stepping state, in case the
901 user was stepping over the fork call. */
902 if (should_resume)
903 {
904 tp->control.step_resume_breakpoint
905 = step_resume_breakpoint;
906 tp->control.step_range_start = step_range_start;
907 tp->control.step_range_end = step_range_end;
908 tp->current_line = current_line;
909 tp->current_symtab = current_symtab;
910 tp->control.step_frame_id = step_frame_id;
911 tp->control.exception_resume_breakpoint
912 = exception_resume_breakpoint;
913 tp->set_thread_fsm (std::move (thread_fsm));
914 }
915 else
916 {
917 /* If we get here, it was because we're trying to
918 resume from a fork catchpoint, but, the user
919 has switched threads away from the thread that
920 forked. In that case, the resume command
921 issued is most likely not applicable to the
922 child, so just warn, and refuse to resume. */
923 warning (_("Not resuming: switched threads "
924 "before following fork child."));
925 }
926
927 /* Reset breakpoints in the child as appropriate. */
928 follow_inferior_reset_breakpoints ();
929 }
930 }
931 }
932 break;
933 case TARGET_WAITKIND_SPURIOUS:
934 /* Nothing to follow. */
935 break;
936 default:
937 internal_error ("Unexpected pending_follow.kind %d\n",
938 tp->pending_follow.kind ());
939 break;
940 }
941
942 if (!should_resume)
943 set_last_target_status_stopped (tp);
944 return should_resume;
945 }
946
947 static void
948 follow_inferior_reset_breakpoints (void)
949 {
950 struct thread_info *tp = inferior_thread ();
951
952 /* Was there a step_resume breakpoint? (There was if the user
953 did a "next" at the fork() call.) If so, explicitly reset its
954 thread number. Cloned step_resume breakpoints are disabled on
955 creation, so enable it here now that it is associated with the
956 correct thread.
957
958 step_resumes are a form of bp that are made to be per-thread.
959 Since we created the step_resume bp when the parent process
960 was being debugged, and now are switching to the child process,
961 from the breakpoint package's viewpoint, that's a switch of
962 "threads". We must update the bp's notion of which thread
963 it is for, or it'll be ignored when it triggers. */
964
965 if (tp->control.step_resume_breakpoint)
966 {
967 breakpoint_re_set_thread (tp->control.step_resume_breakpoint);
968 tp->control.step_resume_breakpoint->first_loc ().enabled = 1;
969 }
970
971 /* Treat exception_resume breakpoints like step_resume breakpoints. */
972 if (tp->control.exception_resume_breakpoint)
973 {
974 breakpoint_re_set_thread (tp->control.exception_resume_breakpoint);
975 tp->control.exception_resume_breakpoint->first_loc ().enabled = 1;
976 }
977
978 /* Reinsert all breakpoints in the child. The user may have set
979 breakpoints after catching the fork, in which case those
980 were never set in the child, but only in the parent. This makes
981 sure the inserted breakpoints match the breakpoint list. */
982
983 breakpoint_re_set ();
984 insert_breakpoints ();
985 }
986
987 /* The child has exited or execed: resume THREAD, a thread of the parent,
988 if it was meant to be executing. */
989
990 static void
991 proceed_after_vfork_done (thread_info *thread)
992 {
993 if (thread->state == THREAD_RUNNING
994 && !thread->executing ()
995 && !thread->stop_requested
996 && thread->stop_signal () == GDB_SIGNAL_0)
997 {
998 infrun_debug_printf ("resuming vfork parent thread %s",
999 thread->ptid.to_string ().c_str ());
1000
1001 switch_to_thread (thread);
1002 clear_proceed_status (0);
1003 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1004 }
1005 }
1006
1007 /* Called whenever we notice an exec or exit event, to handle
1008 detaching or resuming a vfork parent. */
1009
1010 static void
1011 handle_vfork_child_exec_or_exit (int exec)
1012 {
1013 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
1014
1015 struct inferior *inf = current_inferior ();
1016
1017 if (inf->vfork_parent)
1018 {
1019 inferior *resume_parent = nullptr;
1020
1021 /* This exec or exit marks the end of the shared memory region
1022 between the parent and the child. Break the bonds. */
1023 inferior *vfork_parent = inf->vfork_parent;
1024 inf->vfork_parent->vfork_child = nullptr;
1025 inf->vfork_parent = nullptr;
1026
1027 /* If the user wanted to detach from the parent, now is the
1028 time. */
1029 if (vfork_parent->pending_detach)
1030 {
1031 struct program_space *pspace;
1032 struct address_space *aspace;
1033
1034 /* follow-fork child, detach-on-fork on. */
1035
1036 vfork_parent->pending_detach = false;
1037
1038 scoped_restore_current_pspace_and_thread restore_thread;
1039
1040 /* We're letting loose of the parent. */
1041 thread_info *tp = any_live_thread_of_inferior (vfork_parent);
1042 switch_to_thread (tp);
1043
1044 /* We're about to detach from the parent, which implicitly
1045 removes breakpoints from its address space. There's a
1046 catch here: we want to reuse the spaces for the child,
1047 but, parent/child are still sharing the pspace at this
1048 point, although the exec in reality makes the kernel give
1049 the child a fresh set of new pages. The problem here is
1050 that the breakpoints module being unaware of this, would
1051 likely chose the child process to write to the parent
1052 address space. Swapping the child temporarily away from
1053 the spaces has the desired effect. Yes, this is "sort
1054 of" a hack. */
1055
1056 pspace = inf->pspace;
1057 aspace = inf->aspace;
1058 inf->aspace = nullptr;
1059 inf->pspace = nullptr;
1060
1061 if (print_inferior_events)
1062 {
1063 std::string pidstr
1064 = target_pid_to_str (ptid_t (vfork_parent->pid));
1065
1066 target_terminal::ours_for_output ();
1067
1068 if (exec)
1069 {
1070 gdb_printf (_("[Detaching vfork parent %s "
1071 "after child exec]\n"), pidstr.c_str ());
1072 }
1073 else
1074 {
1075 gdb_printf (_("[Detaching vfork parent %s "
1076 "after child exit]\n"), pidstr.c_str ());
1077 }
1078 }
1079
1080 target_detach (vfork_parent, 0);
1081
1082 /* Put it back. */
1083 inf->pspace = pspace;
1084 inf->aspace = aspace;
1085 }
1086 else if (exec)
1087 {
1088 /* We're staying attached to the parent, so, really give the
1089 child a new address space. */
1090 inf->pspace = new program_space (maybe_new_address_space ());
1091 inf->aspace = inf->pspace->aspace;
1092 inf->removable = true;
1093 set_current_program_space (inf->pspace);
1094
1095 resume_parent = vfork_parent;
1096 }
1097 else
1098 {
1099 /* If this is a vfork child exiting, then the pspace and
1100 aspaces were shared with the parent. Since we're
1101 reporting the process exit, we'll be mourning all that is
1102 found in the address space, and switching to null_ptid,
1103 preparing to start a new inferior. But, since we don't
1104 want to clobber the parent's address/program spaces, we
1105 go ahead and create a new one for this exiting
1106 inferior. */
1107
1108 /* Switch to no-thread while running clone_program_space, so
1109 that clone_program_space doesn't want to read the
1110 selected frame of a dead process. */
1111 scoped_restore_current_thread restore_thread;
1112 switch_to_no_thread ();
1113
1114 inf->pspace = new program_space (maybe_new_address_space ());
1115 inf->aspace = inf->pspace->aspace;
1116 set_current_program_space (inf->pspace);
1117 inf->removable = true;
1118 inf->symfile_flags = SYMFILE_NO_READ;
1119 clone_program_space (inf->pspace, vfork_parent->pspace);
1120
1121 resume_parent = vfork_parent;
1122 }
1123
1124 gdb_assert (current_program_space == inf->pspace);
1125
1126 if (non_stop && resume_parent != nullptr)
1127 {
1128 /* If the user wanted the parent to be running, let it go
1129 free now. */
1130 scoped_restore_current_thread restore_thread;
1131
1132 infrun_debug_printf ("resuming vfork parent process %d",
1133 resume_parent->pid);
1134
1135 for (thread_info *thread : resume_parent->threads ())
1136 proceed_after_vfork_done (thread);
1137 }
1138 }
1139 }
1140
1141 /* Handle TARGET_WAITKIND_VFORK_DONE. */
1142
1143 static void
1144 handle_vfork_done (thread_info *event_thread)
1145 {
1146 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
1147
1148 /* We only care about this event if inferior::thread_waiting_for_vfork_done is
1149 set, that is if we are waiting for a vfork child not under our control
1150 (because we detached it) to exec or exit.
1151
1152 If an inferior has vforked and we are debugging the child, we don't use
1153 the vfork-done event to get notified about the end of the shared address
1154 space window. We rely instead on the child's exec or exit event, and the
1155 inferior::vfork_{parent,child} fields are used instead. See
1156 handle_vfork_child_exec_or_exit for that. */
1157 if (event_thread->inf->thread_waiting_for_vfork_done == nullptr)
1158 {
1159 infrun_debug_printf ("not waiting for a vfork-done event");
1160 return;
1161 }
1162
1163 /* We stopped all threads (other than the vforking thread) of the inferior in
1164 follow_fork and kept them stopped until now. It should therefore not be
1165 possible for another thread to have reported a vfork during that window.
1166 If THREAD_WAITING_FOR_VFORK_DONE is set, it has to be the same thread whose
1167 vfork-done we are handling right now. */
1168 gdb_assert (event_thread->inf->thread_waiting_for_vfork_done == event_thread);
1169
1170 event_thread->inf->thread_waiting_for_vfork_done = nullptr;
1171 event_thread->inf->pspace->breakpoints_not_allowed = 0;
1172
1173 /* On non-stop targets, we stopped all the inferior's threads in follow_fork,
1174 resume them now. On all-stop targets, everything that needs to be resumed
1175 will be when we resume the event thread. */
1176 if (target_is_non_stop_p ())
1177 {
1178 /* restart_threads and start_step_over may change the current thread, make
1179 sure we leave the event thread as the current thread. */
1180 scoped_restore_current_thread restore_thread;
1181
1182 insert_breakpoints ();
1183 start_step_over ();
1184
1185 if (!step_over_info_valid_p ())
1186 restart_threads (event_thread, event_thread->inf);
1187 }
1188 }
1189
1190 /* Enum strings for "set|show follow-exec-mode". */
1191
1192 static const char follow_exec_mode_new[] = "new";
1193 static const char follow_exec_mode_same[] = "same";
1194 static const char *const follow_exec_mode_names[] =
1195 {
1196 follow_exec_mode_new,
1197 follow_exec_mode_same,
1198 nullptr,
1199 };
1200
1201 static const char *follow_exec_mode_string = follow_exec_mode_same;
1202 static void
1203 show_follow_exec_mode_string (struct ui_file *file, int from_tty,
1204 struct cmd_list_element *c, const char *value)
1205 {
1206 gdb_printf (file, _("Follow exec mode is \"%s\".\n"), value);
1207 }
1208
1209 /* EXEC_FILE_TARGET is assumed to be non-NULL. */
1210
1211 static void
1212 follow_exec (ptid_t ptid, const char *exec_file_target)
1213 {
1214 int pid = ptid.pid ();
1215 ptid_t process_ptid;
1216
1217 /* Switch terminal for any messages produced e.g. by
1218 breakpoint_re_set. */
1219 target_terminal::ours_for_output ();
1220
1221 /* This is an exec event that we actually wish to pay attention to.
1222 Refresh our symbol table to the newly exec'd program, remove any
1223 momentary bp's, etc.
1224
1225 If there are breakpoints, they aren't really inserted now,
1226 since the exec() transformed our inferior into a fresh set
1227 of instructions.
1228
1229 We want to preserve symbolic breakpoints on the list, since
1230 we have hopes that they can be reset after the new a.out's
1231 symbol table is read.
1232
1233 However, any "raw" breakpoints must be removed from the list
1234 (e.g., the solib bp's), since their address is probably invalid
1235 now.
1236
1237 And, we DON'T want to call delete_breakpoints() here, since
1238 that may write the bp's "shadow contents" (the instruction
1239 value that was overwritten with a TRAP instruction). Since
1240 we now have a new a.out, those shadow contents aren't valid. */
1241
1242 mark_breakpoints_out ();
1243
1244 /* The target reports the exec event to the main thread, even if
1245 some other thread does the exec, and even if the main thread was
1246 stopped or already gone. We may still have non-leader threads of
1247 the process on our list. E.g., on targets that don't have thread
1248 exit events (like remote) and nothing forces an update of the
1249 thread list up to here. When debugging remotely, it's best to
1250 avoid extra traffic, when possible, so avoid syncing the thread
1251 list with the target, and instead go ahead and delete all threads
1252 of the process but the one that reported the event. Note this must
1253 be done before calling update_breakpoints_after_exec, as
1254 otherwise clearing the threads' resources would reference stale
1255 thread breakpoints -- it may have been one of these threads that
1256 stepped across the exec. We could just clear their stepping
1257 states, but as long as we're iterating, might as well delete
1258 them. Deleting them now rather than at the next user-visible
1259 stop provides a nicer sequence of events for user and MI
1260 notifications. */
1261 for (thread_info *th : all_threads_safe ())
1262 if (th->ptid.pid () == pid && th->ptid != ptid)
1263 delete_thread (th);
1264
1265 /* We also need to clear any left over stale state for the
1266 leader/event thread. E.g., if there was any step-resume
1267 breakpoint or similar, it's gone now. We cannot truly
1268 step-to-next statement through an exec(). */
1269 thread_info *th = inferior_thread ();
1270 th->control.step_resume_breakpoint = nullptr;
1271 th->control.exception_resume_breakpoint = nullptr;
1272 th->control.single_step_breakpoints = nullptr;
1273 th->control.step_range_start = 0;
1274 th->control.step_range_end = 0;
1275
1276 /* The user may have had the main thread held stopped in the
1277 previous image (e.g., schedlock on, or non-stop). Release
1278 it now. */
1279 th->stop_requested = 0;
1280
1281 update_breakpoints_after_exec ();
1282
1283 /* What is this a.out's name? */
1284 process_ptid = ptid_t (pid);
1285 gdb_printf (_("%s is executing new program: %s\n"),
1286 target_pid_to_str (process_ptid).c_str (),
1287 exec_file_target);
1288
1289 /* We've followed the inferior through an exec. Therefore, the
1290 inferior has essentially been killed & reborn. */
1291
1292 breakpoint_init_inferior (inf_execd);
1293
1294 gdb::unique_xmalloc_ptr<char> exec_file_host
1295 = exec_file_find (exec_file_target, nullptr);
1296
1297 /* If we were unable to map the executable target pathname onto a host
1298 pathname, tell the user that. Otherwise GDB's subsequent behavior
1299 is confusing. Maybe it would even be better to stop at this point
1300 so that the user can specify a file manually before continuing. */
1301 if (exec_file_host == nullptr)
1302 warning (_("Could not load symbols for executable %s.\n"
1303 "Do you need \"set sysroot\"?"),
1304 exec_file_target);
1305
1306 /* Reset the shared library package. This ensures that we get a
1307 shlib event when the child reaches "_start", at which point the
1308 dld will have had a chance to initialize the child. */
1309 /* Also, loading a symbol file below may trigger symbol lookups, and
1310 we don't want those to be satisfied by the libraries of the
1311 previous incarnation of this process. */
1312 no_shared_libraries (nullptr, 0);
1313
1314 inferior *execing_inferior = current_inferior ();
1315 inferior *following_inferior;
1316
1317 if (follow_exec_mode_string == follow_exec_mode_new)
1318 {
1319 /* The user wants to keep the old inferior and program spaces
1320 around. Create a new fresh one, and switch to it. */
1321
1322 /* Do exit processing for the original inferior before setting the new
1323 inferior's pid. Having two inferiors with the same pid would confuse
1324 find_inferior_p(t)id. Transfer the terminal state and info from the
1325 old to the new inferior. */
1326 following_inferior = add_inferior_with_spaces ();
1327
1328 swap_terminal_info (following_inferior, execing_inferior);
1329 exit_inferior (execing_inferior);
1330
1331 following_inferior->pid = pid;
1332 }
1333 else
1334 {
1335 /* follow-exec-mode is "same", we continue execution in the execing
1336 inferior. */
1337 following_inferior = execing_inferior;
1338
1339 /* The old description may no longer be fit for the new image.
1340 E.g, a 64-bit process exec'ed a 32-bit process. Clear the
1341 old description; we'll read a new one below. No need to do
1342 this on "follow-exec-mode new", as the old inferior stays
1343 around (its description is later cleared/refetched on
1344 restart). */
1345 target_clear_description ();
1346 }
1347
1348 target_follow_exec (following_inferior, ptid, exec_file_target);
1349
1350 gdb_assert (current_inferior () == following_inferior);
1351 gdb_assert (current_program_space == following_inferior->pspace);
1352
1353 /* Attempt to open the exec file. SYMFILE_DEFER_BP_RESET is used
1354 because the proper displacement for a PIE (Position Independent
1355 Executable) main symbol file will only be computed by
1356 solib_create_inferior_hook below. breakpoint_re_set would fail
1357 to insert the breakpoints with the zero displacement. */
1358 try_open_exec_file (exec_file_host.get (), following_inferior,
1359 SYMFILE_DEFER_BP_RESET);
1360
1361 /* If the target can specify a description, read it. Must do this
1362 after flipping to the new executable (because the target supplied
1363 description must be compatible with the executable's
1364 architecture, and the old executable may e.g., be 32-bit, while
1365 the new one 64-bit), and before anything involving memory or
1366 registers. */
1367 target_find_description ();
1368
1369 gdb::observers::inferior_execd.notify (execing_inferior, following_inferior);
1370
1371 breakpoint_re_set ();
1372
1373 /* Reinsert all breakpoints. (Those which were symbolic have
1374 been reset to the proper address in the new a.out, thanks
1375 to symbol_file_command...). */
1376 insert_breakpoints ();
1377
1378 /* The next resume of this inferior should bring it to the shlib
1379 startup breakpoints. (If the user had also set bp's on
1380 "main" from the old (parent) process, then they'll auto-
1381 matically get reset there in the new process.). */
1382 }
1383
1384 /* The chain of threads that need to do a step-over operation to get
1385 past e.g., a breakpoint. What technique is used to step over the
1386 breakpoint/watchpoint does not matter -- all threads end up in the
1387 same queue, to maintain rough temporal order of execution, in order
1388 to avoid starvation, otherwise, we could e.g., find ourselves
1389 constantly stepping the same couple threads past their breakpoints
1390 over and over, if the single-step finish fast enough. */
1391 thread_step_over_list global_thread_step_over_list;
1392
1393 /* Bit flags indicating what the thread needs to step over. */
1394
1395 enum step_over_what_flag
1396 {
1397 /* Step over a breakpoint. */
1398 STEP_OVER_BREAKPOINT = 1,
1399
1400 /* Step past a non-continuable watchpoint, in order to let the
1401 instruction execute so we can evaluate the watchpoint
1402 expression. */
1403 STEP_OVER_WATCHPOINT = 2
1404 };
1405 DEF_ENUM_FLAGS_TYPE (enum step_over_what_flag, step_over_what);
1406
1407 /* Info about an instruction that is being stepped over. */
1408
1409 struct step_over_info
1410 {
1411 /* If we're stepping past a breakpoint, this is the address space
1412 and address of the instruction the breakpoint is set at. We'll
1413 skip inserting all breakpoints here. Valid iff ASPACE is
1414 non-NULL. */
1415 const address_space *aspace = nullptr;
1416 CORE_ADDR address = 0;
1417
1418 /* The instruction being stepped over triggers a nonsteppable
1419 watchpoint. If true, we'll skip inserting watchpoints. */
1420 int nonsteppable_watchpoint_p = 0;
1421
1422 /* The thread's global number. */
1423 int thread = -1;
1424 };
1425
1426 /* The step-over info of the location that is being stepped over.
1427
1428 Note that with async/breakpoint always-inserted mode, a user might
1429 set a new breakpoint/watchpoint/etc. exactly while a breakpoint is
1430 being stepped over. As setting a new breakpoint inserts all
1431 breakpoints, we need to make sure the breakpoint being stepped over
1432 isn't inserted then. We do that by only clearing the step-over
1433 info when the step-over is actually finished (or aborted).
1434
1435 Presently GDB can only step over one breakpoint at any given time.
1436 Given threads that can't run code in the same address space as the
1437 breakpoint's can't really miss the breakpoint, GDB could be taught
1438 to step-over at most one breakpoint per address space (so this info
1439 could move to the address space object if/when GDB is extended).
1440 The set of breakpoints being stepped over will normally be much
1441 smaller than the set of all breakpoints, so a flag in the
1442 breakpoint location structure would be wasteful. A separate list
1443 also saves complexity and run-time, as otherwise we'd have to go
1444 through all breakpoint locations clearing their flag whenever we
1445 start a new sequence. Similar considerations weigh against storing
1446 this info in the thread object. Plus, not all step overs actually
1447 have breakpoint locations -- e.g., stepping past a single-step
1448 breakpoint, or stepping to complete a non-continuable
1449 watchpoint. */
1450 static struct step_over_info step_over_info;
1451
1452 /* Record the address of the breakpoint/instruction we're currently
1453 stepping over.
1454 N.B. We record the aspace and address now, instead of say just the thread,
1455 because when we need the info later the thread may be running. */
1456
1457 static void
1458 set_step_over_info (const address_space *aspace, CORE_ADDR address,
1459 int nonsteppable_watchpoint_p,
1460 int thread)
1461 {
1462 step_over_info.aspace = aspace;
1463 step_over_info.address = address;
1464 step_over_info.nonsteppable_watchpoint_p = nonsteppable_watchpoint_p;
1465 step_over_info.thread = thread;
1466 }
1467
1468 /* Called when we're not longer stepping over a breakpoint / an
1469 instruction, so all breakpoints are free to be (re)inserted. */
1470
1471 static void
1472 clear_step_over_info (void)
1473 {
1474 infrun_debug_printf ("clearing step over info");
1475 step_over_info.aspace = nullptr;
1476 step_over_info.address = 0;
1477 step_over_info.nonsteppable_watchpoint_p = 0;
1478 step_over_info.thread = -1;
1479 }
1480
1481 /* See infrun.h. */
1482
1483 int
1484 stepping_past_instruction_at (struct address_space *aspace,
1485 CORE_ADDR address)
1486 {
1487 return (step_over_info.aspace != nullptr
1488 && breakpoint_address_match (aspace, address,
1489 step_over_info.aspace,
1490 step_over_info.address));
1491 }
1492
1493 /* See infrun.h. */
1494
1495 int
1496 thread_is_stepping_over_breakpoint (int thread)
1497 {
1498 return (step_over_info.thread != -1
1499 && thread == step_over_info.thread);
1500 }
1501
1502 /* See infrun.h. */
1503
1504 int
1505 stepping_past_nonsteppable_watchpoint (void)
1506 {
1507 return step_over_info.nonsteppable_watchpoint_p;
1508 }
1509
1510 /* Returns true if step-over info is valid. */
1511
1512 static bool
1513 step_over_info_valid_p (void)
1514 {
1515 return (step_over_info.aspace != nullptr
1516 || stepping_past_nonsteppable_watchpoint ());
1517 }
1518
1519 \f
1520 /* Displaced stepping. */
1521
1522 /* In non-stop debugging mode, we must take special care to manage
1523 breakpoints properly; in particular, the traditional strategy for
1524 stepping a thread past a breakpoint it has hit is unsuitable.
1525 'Displaced stepping' is a tactic for stepping one thread past a
1526 breakpoint it has hit while ensuring that other threads running
1527 concurrently will hit the breakpoint as they should.
1528
1529 The traditional way to step a thread T off a breakpoint in a
1530 multi-threaded program in all-stop mode is as follows:
1531
1532 a0) Initially, all threads are stopped, and breakpoints are not
1533 inserted.
1534 a1) We single-step T, leaving breakpoints uninserted.
1535 a2) We insert breakpoints, and resume all threads.
1536
1537 In non-stop debugging, however, this strategy is unsuitable: we
1538 don't want to have to stop all threads in the system in order to
1539 continue or step T past a breakpoint. Instead, we use displaced
1540 stepping:
1541
1542 n0) Initially, T is stopped, other threads are running, and
1543 breakpoints are inserted.
1544 n1) We copy the instruction "under" the breakpoint to a separate
1545 location, outside the main code stream, making any adjustments
1546 to the instruction, register, and memory state as directed by
1547 T's architecture.
1548 n2) We single-step T over the instruction at its new location.
1549 n3) We adjust the resulting register and memory state as directed
1550 by T's architecture. This includes resetting T's PC to point
1551 back into the main instruction stream.
1552 n4) We resume T.
1553
1554 This approach depends on the following gdbarch methods:
1555
1556 - gdbarch_max_insn_length and gdbarch_displaced_step_location
1557 indicate where to copy the instruction, and how much space must
1558 be reserved there. We use these in step n1.
1559
1560 - gdbarch_displaced_step_copy_insn copies a instruction to a new
1561 address, and makes any necessary adjustments to the instruction,
1562 register contents, and memory. We use this in step n1.
1563
1564 - gdbarch_displaced_step_fixup adjusts registers and memory after
1565 we have successfully single-stepped the instruction, to yield the
1566 same effect the instruction would have had if we had executed it
1567 at its original address. We use this in step n3.
1568
1569 The gdbarch_displaced_step_copy_insn and
1570 gdbarch_displaced_step_fixup functions must be written so that
1571 copying an instruction with gdbarch_displaced_step_copy_insn,
1572 single-stepping across the copied instruction, and then applying
1573 gdbarch_displaced_insn_fixup should have the same effects on the
1574 thread's memory and registers as stepping the instruction in place
1575 would have. Exactly which responsibilities fall to the copy and
1576 which fall to the fixup is up to the author of those functions.
1577
1578 See the comments in gdbarch.sh for details.
1579
1580 Note that displaced stepping and software single-step cannot
1581 currently be used in combination, although with some care I think
1582 they could be made to. Software single-step works by placing
1583 breakpoints on all possible subsequent instructions; if the
1584 displaced instruction is a PC-relative jump, those breakpoints
1585 could fall in very strange places --- on pages that aren't
1586 executable, or at addresses that are not proper instruction
1587 boundaries. (We do generally let other threads run while we wait
1588 to hit the software single-step breakpoint, and they might
1589 encounter such a corrupted instruction.) One way to work around
1590 this would be to have gdbarch_displaced_step_copy_insn fully
1591 simulate the effect of PC-relative instructions (and return NULL)
1592 on architectures that use software single-stepping.
1593
1594 In non-stop mode, we can have independent and simultaneous step
1595 requests, so more than one thread may need to simultaneously step
1596 over a breakpoint. The current implementation assumes there is
1597 only one scratch space per process. In this case, we have to
1598 serialize access to the scratch space. If thread A wants to step
1599 over a breakpoint, but we are currently waiting for some other
1600 thread to complete a displaced step, we leave thread A stopped and
1601 place it in the displaced_step_request_queue. Whenever a displaced
1602 step finishes, we pick the next thread in the queue and start a new
1603 displaced step operation on it. See displaced_step_prepare and
1604 displaced_step_finish for details. */
1605
1606 /* Return true if THREAD is doing a displaced step. */
1607
1608 static bool
1609 displaced_step_in_progress_thread (thread_info *thread)
1610 {
1611 gdb_assert (thread != nullptr);
1612
1613 return thread->displaced_step_state.in_progress ();
1614 }
1615
1616 /* Return true if INF has a thread doing a displaced step. */
1617
1618 static bool
1619 displaced_step_in_progress (inferior *inf)
1620 {
1621 return inf->displaced_step_state.in_progress_count > 0;
1622 }
1623
1624 /* Return true if any thread is doing a displaced step. */
1625
1626 static bool
1627 displaced_step_in_progress_any_thread ()
1628 {
1629 for (inferior *inf : all_non_exited_inferiors ())
1630 {
1631 if (displaced_step_in_progress (inf))
1632 return true;
1633 }
1634
1635 return false;
1636 }
1637
1638 static void
1639 infrun_inferior_exit (struct inferior *inf)
1640 {
1641 inf->displaced_step_state.reset ();
1642 inf->thread_waiting_for_vfork_done = nullptr;
1643 }
1644
1645 static void
1646 infrun_inferior_execd (inferior *exec_inf, inferior *follow_inf)
1647 {
1648 /* If some threads where was doing a displaced step in this inferior at the
1649 moment of the exec, they no longer exist. Even if the exec'ing thread
1650 doing a displaced step, we don't want to to any fixup nor restore displaced
1651 stepping buffer bytes. */
1652 follow_inf->displaced_step_state.reset ();
1653
1654 for (thread_info *thread : follow_inf->threads ())
1655 thread->displaced_step_state.reset ();
1656
1657 /* Since an in-line step is done with everything else stopped, if there was
1658 one in progress at the time of the exec, it must have been the exec'ing
1659 thread. */
1660 clear_step_over_info ();
1661
1662 follow_inf->thread_waiting_for_vfork_done = nullptr;
1663 }
1664
1665 /* If ON, and the architecture supports it, GDB will use displaced
1666 stepping to step over breakpoints. If OFF, or if the architecture
1667 doesn't support it, GDB will instead use the traditional
1668 hold-and-step approach. If AUTO (which is the default), GDB will
1669 decide which technique to use to step over breakpoints depending on
1670 whether the target works in a non-stop way (see use_displaced_stepping). */
1671
1672 static enum auto_boolean can_use_displaced_stepping = AUTO_BOOLEAN_AUTO;
1673
1674 static void
1675 show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
1676 struct cmd_list_element *c,
1677 const char *value)
1678 {
1679 if (can_use_displaced_stepping == AUTO_BOOLEAN_AUTO)
1680 gdb_printf (file,
1681 _("Debugger's willingness to use displaced stepping "
1682 "to step over breakpoints is %s (currently %s).\n"),
1683 value, target_is_non_stop_p () ? "on" : "off");
1684 else
1685 gdb_printf (file,
1686 _("Debugger's willingness to use displaced stepping "
1687 "to step over breakpoints is %s.\n"), value);
1688 }
1689
1690 /* Return true if the gdbarch implements the required methods to use
1691 displaced stepping. */
1692
1693 static bool
1694 gdbarch_supports_displaced_stepping (gdbarch *arch)
1695 {
1696 /* Only check for the presence of `prepare`. The gdbarch verification ensures
1697 that if `prepare` is provided, so is `finish`. */
1698 return gdbarch_displaced_step_prepare_p (arch);
1699 }
1700
1701 /* Return non-zero if displaced stepping can/should be used to step
1702 over breakpoints of thread TP. */
1703
1704 static bool
1705 use_displaced_stepping (thread_info *tp)
1706 {
1707 /* If the user disabled it explicitly, don't use displaced stepping. */
1708 if (can_use_displaced_stepping == AUTO_BOOLEAN_FALSE)
1709 return false;
1710
1711 /* If "auto", only use displaced stepping if the target operates in a non-stop
1712 way. */
1713 if (can_use_displaced_stepping == AUTO_BOOLEAN_AUTO
1714 && !target_is_non_stop_p ())
1715 return false;
1716
1717 gdbarch *gdbarch = get_thread_regcache (tp)->arch ();
1718
1719 /* If the architecture doesn't implement displaced stepping, don't use
1720 it. */
1721 if (!gdbarch_supports_displaced_stepping (gdbarch))
1722 return false;
1723
1724 /* If recording, don't use displaced stepping. */
1725 if (find_record_target () != nullptr)
1726 return false;
1727
1728 /* If displaced stepping failed before for this inferior, don't bother trying
1729 again. */
1730 if (tp->inf->displaced_step_state.failed_before)
1731 return false;
1732
1733 return true;
1734 }
1735
1736 /* Simple function wrapper around displaced_step_thread_state::reset. */
1737
1738 static void
1739 displaced_step_reset (displaced_step_thread_state *displaced)
1740 {
1741 displaced->reset ();
1742 }
1743
1744 /* A cleanup that wraps displaced_step_reset. We use this instead of, say,
1745 SCOPE_EXIT, because it needs to be discardable with "cleanup.release ()". */
1746
1747 using displaced_step_reset_cleanup = FORWARD_SCOPE_EXIT (displaced_step_reset);
1748
1749 /* Prepare to single-step, using displaced stepping.
1750
1751 Note that we cannot use displaced stepping when we have a signal to
1752 deliver. If we have a signal to deliver and an instruction to step
1753 over, then after the step, there will be no indication from the
1754 target whether the thread entered a signal handler or ignored the
1755 signal and stepped over the instruction successfully --- both cases
1756 result in a simple SIGTRAP. In the first case we mustn't do a
1757 fixup, and in the second case we must --- but we can't tell which.
1758 Comments in the code for 'random signals' in handle_inferior_event
1759 explain how we handle this case instead.
1760
1761 Returns DISPLACED_STEP_PREPARE_STATUS_OK if preparing was successful -- this
1762 thread is going to be stepped now; DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
1763 if displaced stepping this thread got queued; or
1764 DISPLACED_STEP_PREPARE_STATUS_CANT if this instruction can't be displaced
1765 stepped. */
1766
1767 static displaced_step_prepare_status
1768 displaced_step_prepare_throw (thread_info *tp)
1769 {
1770 regcache *regcache = get_thread_regcache (tp);
1771 struct gdbarch *gdbarch = regcache->arch ();
1772 displaced_step_thread_state &disp_step_thread_state
1773 = tp->displaced_step_state;
1774
1775 /* We should never reach this function if the architecture does not
1776 support displaced stepping. */
1777 gdb_assert (gdbarch_supports_displaced_stepping (gdbarch));
1778
1779 /* Nor if the thread isn't meant to step over a breakpoint. */
1780 gdb_assert (tp->control.trap_expected);
1781
1782 /* Disable range stepping while executing in the scratch pad. We
1783 want a single-step even if executing the displaced instruction in
1784 the scratch buffer lands within the stepping range (e.g., a
1785 jump/branch). */
1786 tp->control.may_range_step = 0;
1787
1788 /* We are about to start a displaced step for this thread. If one is already
1789 in progress, something's wrong. */
1790 gdb_assert (!disp_step_thread_state.in_progress ());
1791
1792 if (tp->inf->displaced_step_state.unavailable)
1793 {
1794 /* The gdbarch tells us it's not worth asking to try a prepare because
1795 it is likely that it will return unavailable, so don't bother asking. */
1796
1797 displaced_debug_printf ("deferring step of %s",
1798 tp->ptid.to_string ().c_str ());
1799
1800 global_thread_step_over_chain_enqueue (tp);
1801 return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
1802 }
1803
1804 displaced_debug_printf ("displaced-stepping %s now",
1805 tp->ptid.to_string ().c_str ());
1806
1807 scoped_restore_current_thread restore_thread;
1808
1809 switch_to_thread (tp);
1810
1811 CORE_ADDR original_pc = regcache_read_pc (regcache);
1812 CORE_ADDR displaced_pc;
1813
1814 /* Display the instruction we are going to displaced step. */
1815 if (debug_displaced)
1816 {
1817 string_file tmp_stream;
1818 int dislen = gdb_print_insn (gdbarch, original_pc, &tmp_stream,
1819 nullptr);
1820
1821 if (dislen > 0)
1822 {
1823 gdb::byte_vector insn_buf (dislen);
1824 read_memory (original_pc, insn_buf.data (), insn_buf.size ());
1825
1826 std::string insn_bytes = bytes_to_string (insn_buf);
1827
1828 displaced_debug_printf ("original insn %s: %s \t %s",
1829 paddress (gdbarch, original_pc),
1830 insn_bytes.c_str (),
1831 tmp_stream.string ().c_str ());
1832 }
1833 else
1834 displaced_debug_printf ("original insn %s: invalid length: %d",
1835 paddress (gdbarch, original_pc), dislen);
1836 }
1837
1838 displaced_step_prepare_status status
1839 = gdbarch_displaced_step_prepare (gdbarch, tp, displaced_pc);
1840
1841 if (status == DISPLACED_STEP_PREPARE_STATUS_CANT)
1842 {
1843 displaced_debug_printf ("failed to prepare (%s)",
1844 tp->ptid.to_string ().c_str ());
1845
1846 return DISPLACED_STEP_PREPARE_STATUS_CANT;
1847 }
1848 else if (status == DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE)
1849 {
1850 /* Not enough displaced stepping resources available, defer this
1851 request by placing it the queue. */
1852
1853 displaced_debug_printf ("not enough resources available, "
1854 "deferring step of %s",
1855 tp->ptid.to_string ().c_str ());
1856
1857 global_thread_step_over_chain_enqueue (tp);
1858
1859 return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
1860 }
1861
1862 gdb_assert (status == DISPLACED_STEP_PREPARE_STATUS_OK);
1863
1864 /* Save the information we need to fix things up if the step
1865 succeeds. */
1866 disp_step_thread_state.set (gdbarch);
1867
1868 tp->inf->displaced_step_state.in_progress_count++;
1869
1870 displaced_debug_printf ("prepared successfully thread=%s, "
1871 "original_pc=%s, displaced_pc=%s",
1872 tp->ptid.to_string ().c_str (),
1873 paddress (gdbarch, original_pc),
1874 paddress (gdbarch, displaced_pc));
1875
1876 /* Display the new displaced instruction(s). */
1877 if (debug_displaced)
1878 {
1879 string_file tmp_stream;
1880 CORE_ADDR addr = displaced_pc;
1881
1882 /* If displaced stepping is going to use h/w single step then we know
1883 that the replacement instruction can only be a single instruction,
1884 in that case set the end address at the next byte.
1885
1886 Otherwise the displaced stepping copy instruction routine could
1887 have generated multiple instructions, and all we know is that they
1888 must fit within the LEN bytes of the buffer. */
1889 CORE_ADDR end
1890 = addr + (gdbarch_displaced_step_hw_singlestep (gdbarch)
1891 ? 1 : gdbarch_displaced_step_buffer_length (gdbarch));
1892
1893 while (addr < end)
1894 {
1895 int dislen = gdb_print_insn (gdbarch, addr, &tmp_stream, nullptr);
1896 if (dislen <= 0)
1897 {
1898 displaced_debug_printf
1899 ("replacement insn %s: invalid length: %d",
1900 paddress (gdbarch, addr), dislen);
1901 break;
1902 }
1903
1904 gdb::byte_vector insn_buf (dislen);
1905 read_memory (addr, insn_buf.data (), insn_buf.size ());
1906
1907 std::string insn_bytes = bytes_to_string (insn_buf);
1908 std::string insn_str = tmp_stream.release ();
1909 displaced_debug_printf ("replacement insn %s: %s \t %s",
1910 paddress (gdbarch, addr),
1911 insn_bytes.c_str (),
1912 insn_str.c_str ());
1913 addr += dislen;
1914 }
1915 }
1916
1917 return DISPLACED_STEP_PREPARE_STATUS_OK;
1918 }
1919
1920 /* Wrapper for displaced_step_prepare_throw that disabled further
1921 attempts at displaced stepping if we get a memory error. */
1922
1923 static displaced_step_prepare_status
1924 displaced_step_prepare (thread_info *thread)
1925 {
1926 displaced_step_prepare_status status
1927 = DISPLACED_STEP_PREPARE_STATUS_CANT;
1928
1929 try
1930 {
1931 status = displaced_step_prepare_throw (thread);
1932 }
1933 catch (const gdb_exception_error &ex)
1934 {
1935 if (ex.error != MEMORY_ERROR
1936 && ex.error != NOT_SUPPORTED_ERROR)
1937 throw;
1938
1939 infrun_debug_printf ("caught exception, disabling displaced stepping: %s",
1940 ex.what ());
1941
1942 /* Be verbose if "set displaced-stepping" is "on", silent if
1943 "auto". */
1944 if (can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
1945 {
1946 warning (_("disabling displaced stepping: %s"),
1947 ex.what ());
1948 }
1949
1950 /* Disable further displaced stepping attempts. */
1951 thread->inf->displaced_step_state.failed_before = 1;
1952 }
1953
1954 return status;
1955 }
1956
1957 /* Maybe disable thread-{cloned,created,exited} event reporting after
1958 a step-over (either in-line or displaced) finishes. */
1959
1960 static void
1961 update_thread_events_after_step_over (thread_info *event_thread,
1962 const target_waitstatus &event_status)
1963 {
1964 if (target_supports_set_thread_options (0))
1965 {
1966 /* We can control per-thread options. Disable events for the
1967 event thread, unless the thread is gone. */
1968 if (event_status.kind () != TARGET_WAITKIND_THREAD_EXITED)
1969 event_thread->set_thread_options (0);
1970 }
1971 else
1972 {
1973 /* We can only control the target-wide target_thread_events
1974 setting. Disable it, but only if other threads don't need it
1975 enabled. */
1976 if (!displaced_step_in_progress_any_thread ())
1977 target_thread_events (false);
1978 }
1979 }
1980
1981 /* If we displaced stepped an instruction successfully, adjust registers and
1982 memory to yield the same effect the instruction would have had if we had
1983 executed it at its original address, and return
1984 DISPLACED_STEP_FINISH_STATUS_OK. If the instruction didn't complete,
1985 relocate the PC and return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED.
1986
1987 If the thread wasn't displaced stepping, return
1988 DISPLACED_STEP_FINISH_STATUS_OK as well. */
1989
1990 static displaced_step_finish_status
1991 displaced_step_finish (thread_info *event_thread,
1992 const target_waitstatus &event_status)
1993 {
1994 /* Check whether the parent is displaced stepping. */
1995 struct regcache *regcache = get_thread_regcache (event_thread);
1996 struct gdbarch *gdbarch = regcache->arch ();
1997 inferior *parent_inf = event_thread->inf;
1998
1999 /* If this was a fork/vfork/clone, this event indicates that the
2000 displaced stepping of the syscall instruction has been done, so
2001 we perform cleanup for parent here. Also note that this
2002 operation also cleans up the child for vfork, because their pages
2003 are shared. */
2004
2005 /* If this is a fork (child gets its own address space copy) and
2006 some displaced step buffers were in use at the time of the fork,
2007 restore the displaced step buffer bytes in the child process.
2008
2009 Architectures which support displaced stepping and fork events
2010 must supply an implementation of
2011 gdbarch_displaced_step_restore_all_in_ptid. This is not enforced
2012 during gdbarch validation to support architectures which support
2013 displaced stepping but not forks. */
2014 if (event_status.kind () == TARGET_WAITKIND_FORKED
2015 && gdbarch_supports_displaced_stepping (gdbarch))
2016 gdbarch_displaced_step_restore_all_in_ptid
2017 (gdbarch, parent_inf, event_status.child_ptid ());
2018
2019 displaced_step_thread_state *displaced = &event_thread->displaced_step_state;
2020
2021 /* Was this thread performing a displaced step? */
2022 if (!displaced->in_progress ())
2023 return DISPLACED_STEP_FINISH_STATUS_OK;
2024
2025 update_thread_events_after_step_over (event_thread, event_status);
2026
2027 gdb_assert (event_thread->inf->displaced_step_state.in_progress_count > 0);
2028 event_thread->inf->displaced_step_state.in_progress_count--;
2029
2030 /* Fixup may need to read memory/registers. Switch to the thread
2031 that we're fixing up. Also, target_stopped_by_watchpoint checks
2032 the current thread, and displaced_step_restore performs ptid-dependent
2033 memory accesses using current_inferior(). */
2034 switch_to_thread (event_thread);
2035
2036 displaced_step_reset_cleanup cleanup (displaced);
2037
2038 /* Do the fixup, and release the resources acquired to do the displaced
2039 step. */
2040 displaced_step_finish_status status
2041 = gdbarch_displaced_step_finish (displaced->get_original_gdbarch (),
2042 event_thread, event_status);
2043
2044 if (event_status.kind () == TARGET_WAITKIND_FORKED
2045 || event_status.kind () == TARGET_WAITKIND_VFORKED
2046 || event_status.kind () == TARGET_WAITKIND_THREAD_CLONED)
2047 {
2048 /* Since the vfork/fork/clone syscall instruction was executed
2049 in the scratchpad, the child's PC is also within the
2050 scratchpad. Set the child's PC to the parent's PC value,
2051 which has already been fixed up. Note: we use the parent's
2052 aspace here, although we're touching the child, because the
2053 child hasn't been added to the inferior list yet at this
2054 point. */
2055
2056 struct regcache *child_regcache
2057 = get_thread_arch_aspace_regcache (parent_inf,
2058 event_status.child_ptid (),
2059 gdbarch,
2060 parent_inf->aspace);
2061 /* Read PC value of parent. */
2062 CORE_ADDR parent_pc = regcache_read_pc (regcache);
2063
2064 displaced_debug_printf ("write child pc from %s to %s",
2065 paddress (gdbarch,
2066 regcache_read_pc (child_regcache)),
2067 paddress (gdbarch, parent_pc));
2068
2069 regcache_write_pc (child_regcache, parent_pc);
2070 }
2071
2072 return status;
2073 }
2074
2075 /* Data to be passed around while handling an event. This data is
2076 discarded between events. */
2077 struct execution_control_state
2078 {
2079 explicit execution_control_state (thread_info *thr = nullptr)
2080 : ptid (thr == nullptr ? null_ptid : thr->ptid),
2081 event_thread (thr)
2082 {
2083 }
2084
2085 process_stratum_target *target = nullptr;
2086 ptid_t ptid;
2087 /* The thread that got the event, if this was a thread event; NULL
2088 otherwise. */
2089 struct thread_info *event_thread;
2090
2091 struct target_waitstatus ws;
2092 int stop_func_filled_in = 0;
2093 CORE_ADDR stop_func_alt_start = 0;
2094 CORE_ADDR stop_func_start = 0;
2095 CORE_ADDR stop_func_end = 0;
2096 const char *stop_func_name = nullptr;
2097 int wait_some_more = 0;
2098
2099 /* True if the event thread hit the single-step breakpoint of
2100 another thread. Thus the event doesn't cause a stop, the thread
2101 needs to be single-stepped past the single-step breakpoint before
2102 we can switch back to the original stepping thread. */
2103 int hit_singlestep_breakpoint = 0;
2104 };
2105
2106 static void keep_going_pass_signal (struct execution_control_state *ecs);
2107 static void prepare_to_wait (struct execution_control_state *ecs);
2108 static bool keep_going_stepped_thread (struct thread_info *tp);
2109 static step_over_what thread_still_needs_step_over (struct thread_info *tp);
2110
2111 /* Are there any pending step-over requests? If so, run all we can
2112 now and return true. Otherwise, return false. */
2113
2114 static bool
2115 start_step_over (void)
2116 {
2117 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
2118
2119 /* Don't start a new step-over if we already have an in-line
2120 step-over operation ongoing. */
2121 if (step_over_info_valid_p ())
2122 return false;
2123
2124 /* Steal the global thread step over chain. As we try to initiate displaced
2125 steps, threads will be enqueued in the global chain if no buffers are
2126 available. If we iterated on the global chain directly, we might iterate
2127 indefinitely. */
2128 thread_step_over_list threads_to_step
2129 = std::move (global_thread_step_over_list);
2130
2131 infrun_debug_printf ("stealing global queue of threads to step, length = %d",
2132 thread_step_over_chain_length (threads_to_step));
2133
2134 bool started = false;
2135
2136 /* On scope exit (whatever the reason, return or exception), if there are
2137 threads left in the THREADS_TO_STEP chain, put back these threads in the
2138 global list. */
2139 SCOPE_EXIT
2140 {
2141 if (threads_to_step.empty ())
2142 infrun_debug_printf ("step-over queue now empty");
2143 else
2144 {
2145 infrun_debug_printf ("putting back %d threads to step in global queue",
2146 thread_step_over_chain_length (threads_to_step));
2147
2148 global_thread_step_over_chain_enqueue_chain
2149 (std::move (threads_to_step));
2150 }
2151 };
2152
2153 thread_step_over_list_safe_range range
2154 = make_thread_step_over_list_safe_range (threads_to_step);
2155
2156 for (thread_info *tp : range)
2157 {
2158 step_over_what step_what;
2159 int must_be_in_line;
2160
2161 gdb_assert (!tp->stop_requested);
2162
2163 if (tp->inf->displaced_step_state.unavailable)
2164 {
2165 /* The arch told us to not even try preparing another displaced step
2166 for this inferior. Just leave the thread in THREADS_TO_STEP, it
2167 will get moved to the global chain on scope exit. */
2168 continue;
2169 }
2170
2171 if (tp->inf->thread_waiting_for_vfork_done != nullptr)
2172 {
2173 /* When we stop all threads, handling a vfork, any thread in the step
2174 over chain remains there. A user could also try to continue a
2175 thread stopped at a breakpoint while another thread is waiting for
2176 a vfork-done event. In any case, we don't want to start a step
2177 over right now. */
2178 continue;
2179 }
2180
2181 /* Remove thread from the THREADS_TO_STEP chain. If anything goes wrong
2182 while we try to prepare the displaced step, we don't add it back to
2183 the global step over chain. This is to avoid a thread staying in the
2184 step over chain indefinitely if something goes wrong when resuming it
2185 If the error is intermittent and it still needs a step over, it will
2186 get enqueued again when we try to resume it normally. */
2187 threads_to_step.erase (threads_to_step.iterator_to (*tp));
2188
2189 step_what = thread_still_needs_step_over (tp);
2190 must_be_in_line = ((step_what & STEP_OVER_WATCHPOINT)
2191 || ((step_what & STEP_OVER_BREAKPOINT)
2192 && !use_displaced_stepping (tp)));
2193
2194 /* We currently stop all threads of all processes to step-over
2195 in-line. If we need to start a new in-line step-over, let
2196 any pending displaced steps finish first. */
2197 if (must_be_in_line && displaced_step_in_progress_any_thread ())
2198 {
2199 global_thread_step_over_chain_enqueue (tp);
2200 continue;
2201 }
2202
2203 if (tp->control.trap_expected
2204 || tp->resumed ()
2205 || tp->executing ())
2206 {
2207 internal_error ("[%s] has inconsistent state: "
2208 "trap_expected=%d, resumed=%d, executing=%d\n",
2209 tp->ptid.to_string ().c_str (),
2210 tp->control.trap_expected,
2211 tp->resumed (),
2212 tp->executing ());
2213 }
2214
2215 infrun_debug_printf ("resuming [%s] for step-over",
2216 tp->ptid.to_string ().c_str ());
2217
2218 /* keep_going_pass_signal skips the step-over if the breakpoint
2219 is no longer inserted. In all-stop, we want to keep looking
2220 for a thread that needs a step-over instead of resuming TP,
2221 because we wouldn't be able to resume anything else until the
2222 target stops again. In non-stop, the resume always resumes
2223 only TP, so it's OK to let the thread resume freely. */
2224 if (!target_is_non_stop_p () && !step_what)
2225 continue;
2226
2227 switch_to_thread (tp);
2228 execution_control_state ecs (tp);
2229 keep_going_pass_signal (&ecs);
2230
2231 if (!ecs.wait_some_more)
2232 error (_("Command aborted."));
2233
2234 /* If the thread's step over could not be initiated because no buffers
2235 were available, it was re-added to the global step over chain. */
2236 if (tp->resumed ())
2237 {
2238 infrun_debug_printf ("[%s] was resumed.",
2239 tp->ptid.to_string ().c_str ());
2240 gdb_assert (!thread_is_in_step_over_chain (tp));
2241 }
2242 else
2243 {
2244 infrun_debug_printf ("[%s] was NOT resumed.",
2245 tp->ptid.to_string ().c_str ());
2246 gdb_assert (thread_is_in_step_over_chain (tp));
2247 }
2248
2249 /* If we started a new in-line step-over, we're done. */
2250 if (step_over_info_valid_p ())
2251 {
2252 gdb_assert (tp->control.trap_expected);
2253 started = true;
2254 break;
2255 }
2256
2257 if (!target_is_non_stop_p ())
2258 {
2259 /* On all-stop, shouldn't have resumed unless we needed a
2260 step over. */
2261 gdb_assert (tp->control.trap_expected
2262 || tp->step_after_step_resume_breakpoint);
2263
2264 /* With remote targets (at least), in all-stop, we can't
2265 issue any further remote commands until the program stops
2266 again. */
2267 started = true;
2268 break;
2269 }
2270
2271 /* Either the thread no longer needed a step-over, or a new
2272 displaced stepping sequence started. Even in the latter
2273 case, continue looking. Maybe we can also start another
2274 displaced step on a thread of other process. */
2275 }
2276
2277 return started;
2278 }
2279
2280 /* Update global variables holding ptids to hold NEW_PTID if they were
2281 holding OLD_PTID. */
2282 static void
2283 infrun_thread_ptid_changed (process_stratum_target *target,
2284 ptid_t old_ptid, ptid_t new_ptid)
2285 {
2286 if (inferior_ptid == old_ptid
2287 && current_inferior ()->process_target () == target)
2288 inferior_ptid = new_ptid;
2289 }
2290
2291 \f
2292
2293 static const char schedlock_off[] = "off";
2294 static const char schedlock_on[] = "on";
2295 static const char schedlock_step[] = "step";
2296 static const char schedlock_replay[] = "replay";
2297 static const char *const scheduler_enums[] = {
2298 schedlock_off,
2299 schedlock_on,
2300 schedlock_step,
2301 schedlock_replay,
2302 nullptr
2303 };
2304 static const char *scheduler_mode = schedlock_replay;
2305 static void
2306 show_scheduler_mode (struct ui_file *file, int from_tty,
2307 struct cmd_list_element *c, const char *value)
2308 {
2309 gdb_printf (file,
2310 _("Mode for locking scheduler "
2311 "during execution is \"%s\".\n"),
2312 value);
2313 }
2314
2315 static void
2316 set_schedlock_func (const char *args, int from_tty, struct cmd_list_element *c)
2317 {
2318 if (!target_can_lock_scheduler ())
2319 {
2320 scheduler_mode = schedlock_off;
2321 error (_("Target '%s' cannot support this command."),
2322 target_shortname ());
2323 }
2324 }
2325
2326 /* True if execution commands resume all threads of all processes by
2327 default; otherwise, resume only threads of the current inferior
2328 process. */
2329 bool sched_multi = false;
2330
2331 /* Try to setup for software single stepping. Return true if target_resume()
2332 should use hardware single step.
2333
2334 GDBARCH the current gdbarch. */
2335
2336 static bool
2337 maybe_software_singlestep (struct gdbarch *gdbarch)
2338 {
2339 bool hw_step = true;
2340
2341 if (execution_direction == EXEC_FORWARD
2342 && gdbarch_software_single_step_p (gdbarch))
2343 hw_step = !insert_single_step_breakpoints (gdbarch);
2344
2345 return hw_step;
2346 }
2347
2348 /* See infrun.h. */
2349
2350 ptid_t
2351 user_visible_resume_ptid (int step)
2352 {
2353 ptid_t resume_ptid;
2354
2355 if (non_stop)
2356 {
2357 /* With non-stop mode on, threads are always handled
2358 individually. */
2359 resume_ptid = inferior_ptid;
2360 }
2361 else if ((scheduler_mode == schedlock_on)
2362 || (scheduler_mode == schedlock_step && step))
2363 {
2364 /* User-settable 'scheduler' mode requires solo thread
2365 resume. */
2366 resume_ptid = inferior_ptid;
2367 }
2368 else if ((scheduler_mode == schedlock_replay)
2369 && target_record_will_replay (minus_one_ptid, execution_direction))
2370 {
2371 /* User-settable 'scheduler' mode requires solo thread resume in replay
2372 mode. */
2373 resume_ptid = inferior_ptid;
2374 }
2375 else if (!sched_multi && target_supports_multi_process ())
2376 {
2377 /* Resume all threads of the current process (and none of other
2378 processes). */
2379 resume_ptid = ptid_t (inferior_ptid.pid ());
2380 }
2381 else
2382 {
2383 /* Resume all threads of all processes. */
2384 resume_ptid = RESUME_ALL;
2385 }
2386
2387 return resume_ptid;
2388 }
2389
2390 /* See infrun.h. */
2391
2392 process_stratum_target *
2393 user_visible_resume_target (ptid_t resume_ptid)
2394 {
2395 return (resume_ptid == minus_one_ptid && sched_multi
2396 ? nullptr
2397 : current_inferior ()->process_target ());
2398 }
2399
2400 /* Find a thread from the inferiors that we'll resume that is waiting
2401 for a vfork-done event. */
2402
2403 static thread_info *
2404 find_thread_waiting_for_vfork_done ()
2405 {
2406 gdb_assert (!target_is_non_stop_p ());
2407
2408 if (sched_multi)
2409 {
2410 for (inferior *inf : all_non_exited_inferiors ())
2411 if (inf->thread_waiting_for_vfork_done != nullptr)
2412 return inf->thread_waiting_for_vfork_done;
2413 }
2414 else
2415 {
2416 inferior *cur_inf = current_inferior ();
2417 if (cur_inf->thread_waiting_for_vfork_done != nullptr)
2418 return cur_inf->thread_waiting_for_vfork_done;
2419 }
2420 return nullptr;
2421 }
2422
2423 /* Return a ptid representing the set of threads that we will resume,
2424 in the perspective of the target, assuming run control handling
2425 does not require leaving some threads stopped (e.g., stepping past
2426 breakpoint). USER_STEP indicates whether we're about to start the
2427 target for a stepping command. */
2428
2429 static ptid_t
2430 internal_resume_ptid (int user_step)
2431 {
2432 /* In non-stop, we always control threads individually. Note that
2433 the target may always work in non-stop mode even with "set
2434 non-stop off", in which case user_visible_resume_ptid could
2435 return a wildcard ptid. */
2436 if (target_is_non_stop_p ())
2437 return inferior_ptid;
2438
2439 /* The rest of the function assumes non-stop==off and
2440 target-non-stop==off.
2441
2442 If a thread is waiting for a vfork-done event, it means breakpoints are out
2443 for this inferior (well, program space in fact). We don't want to resume
2444 any thread other than the one waiting for vfork done, otherwise these other
2445 threads could miss breakpoints. So if a thread in the resumption set is
2446 waiting for a vfork-done event, resume only that thread.
2447
2448 The resumption set width depends on whether schedule-multiple is on or off.
2449
2450 Note that if the target_resume interface was more flexible, we could be
2451 smarter here when schedule-multiple is on. For example, imagine 3
2452 inferiors with 2 threads each (1.1, 1.2, 2.1, 2.2, 3.1 and 3.2). Threads
2453 2.1 and 3.2 are both waiting for a vfork-done event. Then we could ask the
2454 target(s) to resume:
2455
2456 - All threads of inferior 1
2457 - Thread 2.1
2458 - Thread 3.2
2459
2460 Since we don't have that flexibility (we can only pass one ptid), just
2461 resume the first thread waiting for a vfork-done event we find (e.g. thread
2462 2.1). */
2463 thread_info *thr = find_thread_waiting_for_vfork_done ();
2464 if (thr != nullptr)
2465 {
2466 /* If we have a thread that is waiting for a vfork-done event,
2467 then we should have switched to it earlier. Calling
2468 target_resume with thread scope is only possible when the
2469 current thread matches the thread scope. */
2470 gdb_assert (thr->ptid == inferior_ptid);
2471 gdb_assert (thr->inf->process_target ()
2472 == inferior_thread ()->inf->process_target ());
2473 return thr->ptid;
2474 }
2475
2476 return user_visible_resume_ptid (user_step);
2477 }
2478
2479 /* Wrapper for target_resume, that handles infrun-specific
2480 bookkeeping. */
2481
2482 static void
2483 do_target_resume (ptid_t resume_ptid, bool step, enum gdb_signal sig)
2484 {
2485 struct thread_info *tp = inferior_thread ();
2486
2487 gdb_assert (!tp->stop_requested);
2488
2489 /* Install inferior's terminal modes. */
2490 target_terminal::inferior ();
2491
2492 /* Avoid confusing the next resume, if the next stop/resume
2493 happens to apply to another thread. */
2494 tp->set_stop_signal (GDB_SIGNAL_0);
2495
2496 /* Advise target which signals may be handled silently.
2497
2498 If we have removed breakpoints because we are stepping over one
2499 in-line (in any thread), we need to receive all signals to avoid
2500 accidentally skipping a breakpoint during execution of a signal
2501 handler.
2502
2503 Likewise if we're displaced stepping, otherwise a trap for a
2504 breakpoint in a signal handler might be confused with the
2505 displaced step finishing. We don't make the displaced_step_finish
2506 step distinguish the cases instead, because:
2507
2508 - a backtrace while stopped in the signal handler would show the
2509 scratch pad as frame older than the signal handler, instead of
2510 the real mainline code.
2511
2512 - when the thread is later resumed, the signal handler would
2513 return to the scratch pad area, which would no longer be
2514 valid. */
2515 if (step_over_info_valid_p ()
2516 || displaced_step_in_progress (tp->inf))
2517 target_pass_signals ({});
2518 else
2519 target_pass_signals (signal_pass);
2520
2521 /* Request that the target report thread-{created,cloned,exited}
2522 events in the following situations:
2523
2524 - If we are performing an in-line step-over-breakpoint, then we
2525 will remove a breakpoint from the target and only run the
2526 current thread. We don't want any new thread (spawned by the
2527 step) to start running, as it might miss the breakpoint. We
2528 need to clear the step-over state if the stepped thread exits,
2529 so we also enable thread-exit events.
2530
2531 - If we are stepping over a breakpoint out of line (displaced
2532 stepping) then we won't remove a breakpoint from the target,
2533 but, if the step spawns a new clone thread, then we will need
2534 to fixup the $pc address in the clone child too, so we need it
2535 to start stopped. We need to release the displaced stepping
2536 buffer if the stepped thread exits, so we also enable
2537 thread-exit events.
2538 */
2539 if (step_over_info_valid_p ()
2540 || displaced_step_in_progress_thread (tp))
2541 {
2542 gdb_thread_options options
2543 = GDB_THREAD_OPTION_CLONE | GDB_THREAD_OPTION_EXIT;
2544 if (target_supports_set_thread_options (options))
2545 tp->set_thread_options (options);
2546 else
2547 target_thread_events (true);
2548 }
2549
2550 /* If we're resuming more than one thread simultaneously, then any
2551 thread other than the leader is being set to run free. Clear any
2552 previous thread option for those threads. */
2553 if (resume_ptid != inferior_ptid && target_supports_set_thread_options (0))
2554 {
2555 process_stratum_target *resume_target = tp->inf->process_target ();
2556 for (thread_info *thr_iter : all_non_exited_threads (resume_target,
2557 resume_ptid))
2558 if (thr_iter != tp)
2559 thr_iter->set_thread_options (0);
2560 }
2561
2562 infrun_debug_printf ("resume_ptid=%s, step=%d, sig=%s",
2563 resume_ptid.to_string ().c_str (),
2564 step, gdb_signal_to_symbol_string (sig));
2565
2566 target_resume (resume_ptid, step, sig);
2567 }
2568
2569 /* Resume the inferior. SIG is the signal to give the inferior
2570 (GDB_SIGNAL_0 for none). Note: don't call this directly; instead
2571 call 'resume', which handles exceptions. */
2572
2573 static void
2574 resume_1 (enum gdb_signal sig)
2575 {
2576 struct regcache *regcache = get_current_regcache ();
2577 struct gdbarch *gdbarch = regcache->arch ();
2578 struct thread_info *tp = inferior_thread ();
2579 const address_space *aspace = regcache->aspace ();
2580 ptid_t resume_ptid;
2581 /* This represents the user's step vs continue request. When
2582 deciding whether "set scheduler-locking step" applies, it's the
2583 user's intention that counts. */
2584 const int user_step = tp->control.stepping_command;
2585 /* This represents what we'll actually request the target to do.
2586 This can decay from a step to a continue, if e.g., we need to
2587 implement single-stepping with breakpoints (software
2588 single-step). */
2589 bool step;
2590
2591 gdb_assert (!tp->stop_requested);
2592 gdb_assert (!thread_is_in_step_over_chain (tp));
2593
2594 if (tp->has_pending_waitstatus ())
2595 {
2596 infrun_debug_printf
2597 ("thread %s has pending wait "
2598 "status %s (currently_stepping=%d).",
2599 tp->ptid.to_string ().c_str (),
2600 tp->pending_waitstatus ().to_string ().c_str (),
2601 currently_stepping (tp));
2602
2603 tp->inf->process_target ()->threads_executing = true;
2604 tp->set_resumed (true);
2605
2606 /* FIXME: What should we do if we are supposed to resume this
2607 thread with a signal? Maybe we should maintain a queue of
2608 pending signals to deliver. */
2609 if (sig != GDB_SIGNAL_0)
2610 {
2611 warning (_("Couldn't deliver signal %s to %s."),
2612 gdb_signal_to_name (sig),
2613 tp->ptid.to_string ().c_str ());
2614 }
2615
2616 tp->set_stop_signal (GDB_SIGNAL_0);
2617
2618 if (target_can_async_p ())
2619 {
2620 target_async (true);
2621 /* Tell the event loop we have an event to process. */
2622 mark_async_event_handler (infrun_async_inferior_event_token);
2623 }
2624 return;
2625 }
2626
2627 tp->stepped_breakpoint = 0;
2628
2629 /* Depends on stepped_breakpoint. */
2630 step = currently_stepping (tp);
2631
2632 if (current_inferior ()->thread_waiting_for_vfork_done != nullptr)
2633 {
2634 /* Don't try to single-step a vfork parent that is waiting for
2635 the child to get out of the shared memory region (by exec'ing
2636 or exiting). This is particularly important on software
2637 single-step archs, as the child process would trip on the
2638 software single step breakpoint inserted for the parent
2639 process. Since the parent will not actually execute any
2640 instruction until the child is out of the shared region (such
2641 are vfork's semantics), it is safe to simply continue it.
2642 Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
2643 the parent, and tell it to `keep_going', which automatically
2644 re-sets it stepping. */
2645 infrun_debug_printf ("resume : clear step");
2646 step = false;
2647 }
2648
2649 CORE_ADDR pc = regcache_read_pc (regcache);
2650
2651 infrun_debug_printf ("step=%d, signal=%s, trap_expected=%d, "
2652 "current thread [%s] at %s",
2653 step, gdb_signal_to_symbol_string (sig),
2654 tp->control.trap_expected,
2655 inferior_ptid.to_string ().c_str (),
2656 paddress (gdbarch, pc));
2657
2658 /* Normally, by the time we reach `resume', the breakpoints are either
2659 removed or inserted, as appropriate. The exception is if we're sitting
2660 at a permanent breakpoint; we need to step over it, but permanent
2661 breakpoints can't be removed. So we have to test for it here. */
2662 if (breakpoint_here_p (aspace, pc) == permanent_breakpoint_here)
2663 {
2664 if (sig != GDB_SIGNAL_0)
2665 {
2666 /* We have a signal to pass to the inferior. The resume
2667 may, or may not take us to the signal handler. If this
2668 is a step, we'll need to stop in the signal handler, if
2669 there's one, (if the target supports stepping into
2670 handlers), or in the next mainline instruction, if
2671 there's no handler. If this is a continue, we need to be
2672 sure to run the handler with all breakpoints inserted.
2673 In all cases, set a breakpoint at the current address
2674 (where the handler returns to), and once that breakpoint
2675 is hit, resume skipping the permanent breakpoint. If
2676 that breakpoint isn't hit, then we've stepped into the
2677 signal handler (or hit some other event). We'll delete
2678 the step-resume breakpoint then. */
2679
2680 infrun_debug_printf ("resume: skipping permanent breakpoint, "
2681 "deliver signal first");
2682
2683 clear_step_over_info ();
2684 tp->control.trap_expected = 0;
2685
2686 if (tp->control.step_resume_breakpoint == nullptr)
2687 {
2688 /* Set a "high-priority" step-resume, as we don't want
2689 user breakpoints at PC to trigger (again) when this
2690 hits. */
2691 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2692 gdb_assert (tp->control.step_resume_breakpoint->first_loc ()
2693 .permanent);
2694
2695 tp->step_after_step_resume_breakpoint = step;
2696 }
2697
2698 insert_breakpoints ();
2699 }
2700 else
2701 {
2702 /* There's no signal to pass, we can go ahead and skip the
2703 permanent breakpoint manually. */
2704 infrun_debug_printf ("skipping permanent breakpoint");
2705 gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
2706 /* Update pc to reflect the new address from which we will
2707 execute instructions. */
2708 pc = regcache_read_pc (regcache);
2709
2710 if (step)
2711 {
2712 /* We've already advanced the PC, so the stepping part
2713 is done. Now we need to arrange for a trap to be
2714 reported to handle_inferior_event. Set a breakpoint
2715 at the current PC, and run to it. Don't update
2716 prev_pc, because if we end in
2717 switch_back_to_stepped_thread, we want the "expected
2718 thread advanced also" branch to be taken. IOW, we
2719 don't want this thread to step further from PC
2720 (overstep). */
2721 gdb_assert (!step_over_info_valid_p ());
2722 insert_single_step_breakpoint (gdbarch, aspace, pc);
2723 insert_breakpoints ();
2724
2725 resume_ptid = internal_resume_ptid (user_step);
2726 do_target_resume (resume_ptid, false, GDB_SIGNAL_0);
2727 tp->set_resumed (true);
2728 return;
2729 }
2730 }
2731 }
2732
2733 /* If we have a breakpoint to step over, make sure to do a single
2734 step only. Same if we have software watchpoints. */
2735 if (tp->control.trap_expected || bpstat_should_step ())
2736 tp->control.may_range_step = 0;
2737
2738 /* If displaced stepping is enabled, step over breakpoints by executing a
2739 copy of the instruction at a different address.
2740
2741 We can't use displaced stepping when we have a signal to deliver;
2742 the comments for displaced_step_prepare explain why. The
2743 comments in the handle_inferior event for dealing with 'random
2744 signals' explain what we do instead.
2745
2746 We can't use displaced stepping when we are waiting for vfork_done
2747 event, displaced stepping breaks the vfork child similarly as single
2748 step software breakpoint. */
2749 if (tp->control.trap_expected
2750 && use_displaced_stepping (tp)
2751 && !step_over_info_valid_p ()
2752 && sig == GDB_SIGNAL_0
2753 && current_inferior ()->thread_waiting_for_vfork_done == nullptr)
2754 {
2755 displaced_step_prepare_status prepare_status
2756 = displaced_step_prepare (tp);
2757
2758 if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE)
2759 {
2760 infrun_debug_printf ("Got placed in step-over queue");
2761
2762 tp->control.trap_expected = 0;
2763 return;
2764 }
2765 else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_CANT)
2766 {
2767 /* Fallback to stepping over the breakpoint in-line. */
2768
2769 if (target_is_non_stop_p ())
2770 stop_all_threads ("displaced stepping falling back on inline stepping");
2771
2772 set_step_over_info (regcache->aspace (),
2773 regcache_read_pc (regcache), 0, tp->global_num);
2774
2775 step = maybe_software_singlestep (gdbarch);
2776
2777 insert_breakpoints ();
2778 }
2779 else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_OK)
2780 {
2781 /* Update pc to reflect the new address from which we will
2782 execute instructions due to displaced stepping. */
2783 pc = regcache_read_pc (get_thread_regcache (tp));
2784
2785 step = gdbarch_displaced_step_hw_singlestep (gdbarch);
2786 }
2787 else
2788 gdb_assert_not_reached ("Invalid displaced_step_prepare_status "
2789 "value.");
2790 }
2791
2792 /* Do we need to do it the hard way, w/temp breakpoints? */
2793 else if (step)
2794 step = maybe_software_singlestep (gdbarch);
2795
2796 /* Currently, our software single-step implementation leads to different
2797 results than hardware single-stepping in one situation: when stepping
2798 into delivering a signal which has an associated signal handler,
2799 hardware single-step will stop at the first instruction of the handler,
2800 while software single-step will simply skip execution of the handler.
2801
2802 For now, this difference in behavior is accepted since there is no
2803 easy way to actually implement single-stepping into a signal handler
2804 without kernel support.
2805
2806 However, there is one scenario where this difference leads to follow-on
2807 problems: if we're stepping off a breakpoint by removing all breakpoints
2808 and then single-stepping. In this case, the software single-step
2809 behavior means that even if there is a *breakpoint* in the signal
2810 handler, GDB still would not stop.
2811
2812 Fortunately, we can at least fix this particular issue. We detect
2813 here the case where we are about to deliver a signal while software
2814 single-stepping with breakpoints removed. In this situation, we
2815 revert the decisions to remove all breakpoints and insert single-
2816 step breakpoints, and instead we install a step-resume breakpoint
2817 at the current address, deliver the signal without stepping, and
2818 once we arrive back at the step-resume breakpoint, actually step
2819 over the breakpoint we originally wanted to step over. */
2820 if (thread_has_single_step_breakpoints_set (tp)
2821 && sig != GDB_SIGNAL_0
2822 && step_over_info_valid_p ())
2823 {
2824 /* If we have nested signals or a pending signal is delivered
2825 immediately after a handler returns, might already have
2826 a step-resume breakpoint set on the earlier handler. We cannot
2827 set another step-resume breakpoint; just continue on until the
2828 original breakpoint is hit. */
2829 if (tp->control.step_resume_breakpoint == nullptr)
2830 {
2831 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2832 tp->step_after_step_resume_breakpoint = 1;
2833 }
2834
2835 delete_single_step_breakpoints (tp);
2836
2837 clear_step_over_info ();
2838 tp->control.trap_expected = 0;
2839
2840 insert_breakpoints ();
2841 }
2842
2843 /* If STEP is set, it's a request to use hardware stepping
2844 facilities. But in that case, we should never
2845 use singlestep breakpoint. */
2846 gdb_assert (!(thread_has_single_step_breakpoints_set (tp) && step));
2847
2848 /* Decide the set of threads to ask the target to resume. */
2849 if (tp->control.trap_expected)
2850 {
2851 /* We're allowing a thread to run past a breakpoint it has
2852 hit, either by single-stepping the thread with the breakpoint
2853 removed, or by displaced stepping, with the breakpoint inserted.
2854 In the former case, we need to single-step only this thread,
2855 and keep others stopped, as they can miss this breakpoint if
2856 allowed to run. That's not really a problem for displaced
2857 stepping, but, we still keep other threads stopped, in case
2858 another thread is also stopped for a breakpoint waiting for
2859 its turn in the displaced stepping queue. */
2860 resume_ptid = inferior_ptid;
2861 }
2862 else
2863 resume_ptid = internal_resume_ptid (user_step);
2864
2865 if (execution_direction != EXEC_REVERSE
2866 && step && breakpoint_inserted_here_p (aspace, pc))
2867 {
2868 /* There are two cases where we currently need to step a
2869 breakpoint instruction when we have a signal to deliver:
2870
2871 - See handle_signal_stop where we handle random signals that
2872 could take out us out of the stepping range. Normally, in
2873 that case we end up continuing (instead of stepping) over the
2874 signal handler with a breakpoint at PC, but there are cases
2875 where we should _always_ single-step, even if we have a
2876 step-resume breakpoint, like when a software watchpoint is
2877 set. Assuming single-stepping and delivering a signal at the
2878 same time would takes us to the signal handler, then we could
2879 have removed the breakpoint at PC to step over it. However,
2880 some hardware step targets (like e.g., Mac OS) can't step
2881 into signal handlers, and for those, we need to leave the
2882 breakpoint at PC inserted, as otherwise if the handler
2883 recurses and executes PC again, it'll miss the breakpoint.
2884 So we leave the breakpoint inserted anyway, but we need to
2885 record that we tried to step a breakpoint instruction, so
2886 that adjust_pc_after_break doesn't end up confused.
2887
2888 - In non-stop if we insert a breakpoint (e.g., a step-resume)
2889 in one thread after another thread that was stepping had been
2890 momentarily paused for a step-over. When we re-resume the
2891 stepping thread, it may be resumed from that address with a
2892 breakpoint that hasn't trapped yet. Seen with
2893 gdb.threads/non-stop-fair-events.exp, on targets that don't
2894 do displaced stepping. */
2895
2896 infrun_debug_printf ("resume: [%s] stepped breakpoint",
2897 tp->ptid.to_string ().c_str ());
2898
2899 tp->stepped_breakpoint = 1;
2900
2901 /* Most targets can step a breakpoint instruction, thus
2902 executing it normally. But if this one cannot, just
2903 continue and we will hit it anyway. */
2904 if (gdbarch_cannot_step_breakpoint (gdbarch))
2905 step = false;
2906 }
2907
2908 if (tp->control.may_range_step)
2909 {
2910 /* If we're resuming a thread with the PC out of the step
2911 range, then we're doing some nested/finer run control
2912 operation, like stepping the thread out of the dynamic
2913 linker or the displaced stepping scratch pad. We
2914 shouldn't have allowed a range step then. */
2915 gdb_assert (pc_in_thread_step_range (pc, tp));
2916 }
2917
2918 do_target_resume (resume_ptid, step, sig);
2919 tp->set_resumed (true);
2920 }
2921
2922 /* Resume the inferior. SIG is the signal to give the inferior
2923 (GDB_SIGNAL_0 for none). This is a wrapper around 'resume_1' that
2924 rolls back state on error. */
2925
2926 static void
2927 resume (gdb_signal sig)
2928 {
2929 try
2930 {
2931 resume_1 (sig);
2932 }
2933 catch (const gdb_exception &ex)
2934 {
2935 /* If resuming is being aborted for any reason, delete any
2936 single-step breakpoint resume_1 may have created, to avoid
2937 confusing the following resumption, and to avoid leaving
2938 single-step breakpoints perturbing other threads, in case
2939 we're running in non-stop mode. */
2940 if (inferior_ptid != null_ptid)
2941 delete_single_step_breakpoints (inferior_thread ());
2942 throw;
2943 }
2944 }
2945
2946 \f
2947 /* Proceeding. */
2948
2949 /* See infrun.h. */
2950
2951 /* Counter that tracks number of user visible stops. This can be used
2952 to tell whether a command has proceeded the inferior past the
2953 current location. This allows e.g., inferior function calls in
2954 breakpoint commands to not interrupt the command list. When the
2955 call finishes successfully, the inferior is standing at the same
2956 breakpoint as if nothing happened (and so we don't call
2957 normal_stop). */
2958 static ULONGEST current_stop_id;
2959
2960 /* See infrun.h. */
2961
2962 ULONGEST
2963 get_stop_id (void)
2964 {
2965 return current_stop_id;
2966 }
2967
2968 /* Called when we report a user visible stop. */
2969
2970 static void
2971 new_stop_id (void)
2972 {
2973 current_stop_id++;
2974 }
2975
2976 /* Clear out all variables saying what to do when inferior is continued.
2977 First do this, then set the ones you want, then call `proceed'. */
2978
2979 static void
2980 clear_proceed_status_thread (struct thread_info *tp)
2981 {
2982 infrun_debug_printf ("%s", tp->ptid.to_string ().c_str ());
2983
2984 /* If we're starting a new sequence, then the previous finished
2985 single-step is no longer relevant. */
2986 if (tp->has_pending_waitstatus ())
2987 {
2988 if (tp->stop_reason () == TARGET_STOPPED_BY_SINGLE_STEP)
2989 {
2990 infrun_debug_printf ("pending event of %s was a finished step. "
2991 "Discarding.",
2992 tp->ptid.to_string ().c_str ());
2993
2994 tp->clear_pending_waitstatus ();
2995 tp->set_stop_reason (TARGET_STOPPED_BY_NO_REASON);
2996 }
2997 else
2998 {
2999 infrun_debug_printf
3000 ("thread %s has pending wait status %s (currently_stepping=%d).",
3001 tp->ptid.to_string ().c_str (),
3002 tp->pending_waitstatus ().to_string ().c_str (),
3003 currently_stepping (tp));
3004 }
3005 }
3006
3007 /* If this signal should not be seen by program, give it zero.
3008 Used for debugging signals. */
3009 if (!signal_pass_state (tp->stop_signal ()))
3010 tp->set_stop_signal (GDB_SIGNAL_0);
3011
3012 tp->release_thread_fsm ();
3013
3014 tp->control.trap_expected = 0;
3015 tp->control.step_range_start = 0;
3016 tp->control.step_range_end = 0;
3017 tp->control.may_range_step = 0;
3018 tp->control.step_frame_id = null_frame_id;
3019 tp->control.step_stack_frame_id = null_frame_id;
3020 tp->control.step_over_calls = STEP_OVER_UNDEBUGGABLE;
3021 tp->control.step_start_function = nullptr;
3022 tp->stop_requested = 0;
3023
3024 tp->control.stop_step = 0;
3025
3026 tp->control.proceed_to_finish = 0;
3027
3028 tp->control.stepping_command = 0;
3029
3030 /* Discard any remaining commands or status from previous stop. */
3031 bpstat_clear (&tp->control.stop_bpstat);
3032 }
3033
3034 /* Notify the current interpreter and observers that the target is about to
3035 proceed. */
3036
3037 static void
3038 notify_about_to_proceed ()
3039 {
3040 top_level_interpreter ()->on_about_to_proceed ();
3041 gdb::observers::about_to_proceed.notify ();
3042 }
3043
3044 void
3045 clear_proceed_status (int step)
3046 {
3047 /* With scheduler-locking replay, stop replaying other threads if we're
3048 not replaying the user-visible resume ptid.
3049
3050 This is a convenience feature to not require the user to explicitly
3051 stop replaying the other threads. We're assuming that the user's
3052 intent is to resume tracing the recorded process. */
3053 if (!non_stop && scheduler_mode == schedlock_replay
3054 && target_record_is_replaying (minus_one_ptid)
3055 && !target_record_will_replay (user_visible_resume_ptid (step),
3056 execution_direction))
3057 target_record_stop_replaying ();
3058
3059 if (!non_stop && inferior_ptid != null_ptid)
3060 {
3061 ptid_t resume_ptid = user_visible_resume_ptid (step);
3062 process_stratum_target *resume_target
3063 = user_visible_resume_target (resume_ptid);
3064
3065 /* In all-stop mode, delete the per-thread status of all threads
3066 we're about to resume, implicitly and explicitly. */
3067 for (thread_info *tp : all_non_exited_threads (resume_target, resume_ptid))
3068 clear_proceed_status_thread (tp);
3069 }
3070
3071 if (inferior_ptid != null_ptid)
3072 {
3073 struct inferior *inferior;
3074
3075 if (non_stop)
3076 {
3077 /* If in non-stop mode, only delete the per-thread status of
3078 the current thread. */
3079 clear_proceed_status_thread (inferior_thread ());
3080 }
3081
3082 inferior = current_inferior ();
3083 inferior->control.stop_soon = NO_STOP_QUIETLY;
3084 }
3085
3086 notify_about_to_proceed ();
3087 }
3088
3089 /* Returns true if TP is still stopped at a breakpoint that needs
3090 stepping-over in order to make progress. If the breakpoint is gone
3091 meanwhile, we can skip the whole step-over dance. */
3092
3093 static bool
3094 thread_still_needs_step_over_bp (struct thread_info *tp)
3095 {
3096 if (tp->stepping_over_breakpoint)
3097 {
3098 struct regcache *regcache = get_thread_regcache (tp);
3099
3100 if (breakpoint_here_p (regcache->aspace (),
3101 regcache_read_pc (regcache))
3102 == ordinary_breakpoint_here)
3103 return true;
3104
3105 tp->stepping_over_breakpoint = 0;
3106 }
3107
3108 return false;
3109 }
3110
3111 /* Check whether thread TP still needs to start a step-over in order
3112 to make progress when resumed. Returns an bitwise or of enum
3113 step_over_what bits, indicating what needs to be stepped over. */
3114
3115 static step_over_what
3116 thread_still_needs_step_over (struct thread_info *tp)
3117 {
3118 step_over_what what = 0;
3119
3120 if (thread_still_needs_step_over_bp (tp))
3121 what |= STEP_OVER_BREAKPOINT;
3122
3123 if (tp->stepping_over_watchpoint
3124 && !target_have_steppable_watchpoint ())
3125 what |= STEP_OVER_WATCHPOINT;
3126
3127 return what;
3128 }
3129
3130 /* Returns true if scheduler locking applies. STEP indicates whether
3131 we're about to do a step/next-like command to a thread. */
3132
3133 static bool
3134 schedlock_applies (struct thread_info *tp)
3135 {
3136 return (scheduler_mode == schedlock_on
3137 || (scheduler_mode == schedlock_step
3138 && tp->control.stepping_command)
3139 || (scheduler_mode == schedlock_replay
3140 && target_record_will_replay (minus_one_ptid,
3141 execution_direction)));
3142 }
3143
3144 /* Set process_stratum_target::COMMIT_RESUMED_STATE in all target
3145 stacks that have threads executing and don't have threads with
3146 pending events. */
3147
3148 static void
3149 maybe_set_commit_resumed_all_targets ()
3150 {
3151 scoped_restore_current_thread restore_thread;
3152
3153 for (inferior *inf : all_non_exited_inferiors ())
3154 {
3155 process_stratum_target *proc_target = inf->process_target ();
3156
3157 if (proc_target->commit_resumed_state)
3158 {
3159 /* We already set this in a previous iteration, via another
3160 inferior sharing the process_stratum target. */
3161 continue;
3162 }
3163
3164 /* If the target has no resumed threads, it would be useless to
3165 ask it to commit the resumed threads. */
3166 if (!proc_target->threads_executing)
3167 {
3168 infrun_debug_printf ("not requesting commit-resumed for target "
3169 "%s, no resumed threads",
3170 proc_target->shortname ());
3171 continue;
3172 }
3173
3174 /* As an optimization, if a thread from this target has some
3175 status to report, handle it before requiring the target to
3176 commit its resumed threads: handling the status might lead to
3177 resuming more threads. */
3178 if (proc_target->has_resumed_with_pending_wait_status ())
3179 {
3180 infrun_debug_printf ("not requesting commit-resumed for target %s, a"
3181 " thread has a pending waitstatus",
3182 proc_target->shortname ());
3183 continue;
3184 }
3185
3186 switch_to_inferior_no_thread (inf);
3187
3188 if (target_has_pending_events ())
3189 {
3190 infrun_debug_printf ("not requesting commit-resumed for target %s, "
3191 "target has pending events",
3192 proc_target->shortname ());
3193 continue;
3194 }
3195
3196 infrun_debug_printf ("enabling commit-resumed for target %s",
3197 proc_target->shortname ());
3198
3199 proc_target->commit_resumed_state = true;
3200 }
3201 }
3202
3203 /* See infrun.h. */
3204
3205 void
3206 maybe_call_commit_resumed_all_targets ()
3207 {
3208 scoped_restore_current_thread restore_thread;
3209
3210 for (inferior *inf : all_non_exited_inferiors ())
3211 {
3212 process_stratum_target *proc_target = inf->process_target ();
3213
3214 if (!proc_target->commit_resumed_state)
3215 continue;
3216
3217 switch_to_inferior_no_thread (inf);
3218
3219 infrun_debug_printf ("calling commit_resumed for target %s",
3220 proc_target->shortname());
3221
3222 target_commit_resumed ();
3223 }
3224 }
3225
3226 /* To track nesting of scoped_disable_commit_resumed objects, ensuring
3227 that only the outermost one attempts to re-enable
3228 commit-resumed. */
3229 static bool enable_commit_resumed = true;
3230
3231 /* See infrun.h. */
3232
3233 scoped_disable_commit_resumed::scoped_disable_commit_resumed
3234 (const char *reason)
3235 : m_reason (reason),
3236 m_prev_enable_commit_resumed (enable_commit_resumed)
3237 {
3238 infrun_debug_printf ("reason=%s", m_reason);
3239
3240 enable_commit_resumed = false;
3241
3242 for (inferior *inf : all_non_exited_inferiors ())
3243 {
3244 process_stratum_target *proc_target = inf->process_target ();
3245
3246 if (m_prev_enable_commit_resumed)
3247 {
3248 /* This is the outermost instance: force all
3249 COMMIT_RESUMED_STATE to false. */
3250 proc_target->commit_resumed_state = false;
3251 }
3252 else
3253 {
3254 /* This is not the outermost instance, we expect
3255 COMMIT_RESUMED_STATE to have been cleared by the
3256 outermost instance. */
3257 gdb_assert (!proc_target->commit_resumed_state);
3258 }
3259 }
3260 }
3261
3262 /* See infrun.h. */
3263
3264 void
3265 scoped_disable_commit_resumed::reset ()
3266 {
3267 if (m_reset)
3268 return;
3269 m_reset = true;
3270
3271 infrun_debug_printf ("reason=%s", m_reason);
3272
3273 gdb_assert (!enable_commit_resumed);
3274
3275 enable_commit_resumed = m_prev_enable_commit_resumed;
3276
3277 if (m_prev_enable_commit_resumed)
3278 {
3279 /* This is the outermost instance, re-enable
3280 COMMIT_RESUMED_STATE on the targets where it's possible. */
3281 maybe_set_commit_resumed_all_targets ();
3282 }
3283 else
3284 {
3285 /* This is not the outermost instance, we expect
3286 COMMIT_RESUMED_STATE to still be false. */
3287 for (inferior *inf : all_non_exited_inferiors ())
3288 {
3289 process_stratum_target *proc_target = inf->process_target ();
3290 gdb_assert (!proc_target->commit_resumed_state);
3291 }
3292 }
3293 }
3294
3295 /* See infrun.h. */
3296
3297 scoped_disable_commit_resumed::~scoped_disable_commit_resumed ()
3298 {
3299 reset ();
3300 }
3301
3302 /* See infrun.h. */
3303
3304 void
3305 scoped_disable_commit_resumed::reset_and_commit ()
3306 {
3307 reset ();
3308 maybe_call_commit_resumed_all_targets ();
3309 }
3310
3311 /* See infrun.h. */
3312
3313 scoped_enable_commit_resumed::scoped_enable_commit_resumed
3314 (const char *reason)
3315 : m_reason (reason),
3316 m_prev_enable_commit_resumed (enable_commit_resumed)
3317 {
3318 infrun_debug_printf ("reason=%s", m_reason);
3319
3320 if (!enable_commit_resumed)
3321 {
3322 enable_commit_resumed = true;
3323
3324 /* Re-enable COMMIT_RESUMED_STATE on the targets where it's
3325 possible. */
3326 maybe_set_commit_resumed_all_targets ();
3327
3328 maybe_call_commit_resumed_all_targets ();
3329 }
3330 }
3331
3332 /* See infrun.h. */
3333
3334 scoped_enable_commit_resumed::~scoped_enable_commit_resumed ()
3335 {
3336 infrun_debug_printf ("reason=%s", m_reason);
3337
3338 gdb_assert (enable_commit_resumed);
3339
3340 enable_commit_resumed = m_prev_enable_commit_resumed;
3341
3342 if (!enable_commit_resumed)
3343 {
3344 /* Force all COMMIT_RESUMED_STATE back to false. */
3345 for (inferior *inf : all_non_exited_inferiors ())
3346 {
3347 process_stratum_target *proc_target = inf->process_target ();
3348 proc_target->commit_resumed_state = false;
3349 }
3350 }
3351 }
3352
3353 /* Check that all the targets we're about to resume are in non-stop
3354 mode. Ideally, we'd only care whether all targets support
3355 target-async, but we're not there yet. E.g., stop_all_threads
3356 doesn't know how to handle all-stop targets. Also, the remote
3357 protocol in all-stop mode is synchronous, irrespective of
3358 target-async, which means that things like a breakpoint re-set
3359 triggered by one target would try to read memory from all targets
3360 and fail. */
3361
3362 static void
3363 check_multi_target_resumption (process_stratum_target *resume_target)
3364 {
3365 if (!non_stop && resume_target == nullptr)
3366 {
3367 scoped_restore_current_thread restore_thread;
3368
3369 /* This is used to track whether we're resuming more than one
3370 target. */
3371 process_stratum_target *first_connection = nullptr;
3372
3373 /* The first inferior we see with a target that does not work in
3374 always-non-stop mode. */
3375 inferior *first_not_non_stop = nullptr;
3376
3377 for (inferior *inf : all_non_exited_inferiors ())
3378 {
3379 switch_to_inferior_no_thread (inf);
3380
3381 if (!target_has_execution ())
3382 continue;
3383
3384 process_stratum_target *proc_target
3385 = current_inferior ()->process_target();
3386
3387 if (!target_is_non_stop_p ())
3388 first_not_non_stop = inf;
3389
3390 if (first_connection == nullptr)
3391 first_connection = proc_target;
3392 else if (first_connection != proc_target
3393 && first_not_non_stop != nullptr)
3394 {
3395 switch_to_inferior_no_thread (first_not_non_stop);
3396
3397 proc_target = current_inferior ()->process_target();
3398
3399 error (_("Connection %d (%s) does not support "
3400 "multi-target resumption."),
3401 proc_target->connection_number,
3402 make_target_connection_string (proc_target).c_str ());
3403 }
3404 }
3405 }
3406 }
3407
3408 /* Helper function for `proceed`. Check if thread TP is suitable for
3409 resuming, and, if it is, switch to the thread and call
3410 `keep_going_pass_signal`. If TP is not suitable for resuming then this
3411 function will just return without switching threads. */
3412
3413 static void
3414 proceed_resume_thread_checked (thread_info *tp)
3415 {
3416 if (!tp->inf->has_execution ())
3417 {
3418 infrun_debug_printf ("[%s] target has no execution",
3419 tp->ptid.to_string ().c_str ());
3420 return;
3421 }
3422
3423 if (tp->resumed ())
3424 {
3425 infrun_debug_printf ("[%s] resumed",
3426 tp->ptid.to_string ().c_str ());
3427 gdb_assert (tp->executing () || tp->has_pending_waitstatus ());
3428 return;
3429 }
3430
3431 if (thread_is_in_step_over_chain (tp))
3432 {
3433 infrun_debug_printf ("[%s] needs step-over",
3434 tp->ptid.to_string ().c_str ());
3435 return;
3436 }
3437
3438 /* When handling a vfork GDB removes all breakpoints from the program
3439 space in which the vfork is being handled. If we are following the
3440 parent then GDB will set the thread_waiting_for_vfork_done member of
3441 the parent inferior. In this case we should take care to only resume
3442 the vfork parent thread, the kernel will hold this thread suspended
3443 until the vfork child has exited or execd, at which point the parent
3444 will be resumed and a VFORK_DONE event sent to GDB. */
3445 if (tp->inf->thread_waiting_for_vfork_done != nullptr)
3446 {
3447 if (target_is_non_stop_p ())
3448 {
3449 /* For non-stop targets, regardless of whether GDB is using
3450 all-stop or non-stop mode, threads are controlled
3451 individually.
3452
3453 When a thread is handling a vfork, breakpoints are removed
3454 from the inferior (well, program space in fact), so it is
3455 critical that we don't try to resume any thread other than the
3456 vfork parent. */
3457 if (tp != tp->inf->thread_waiting_for_vfork_done)
3458 {
3459 infrun_debug_printf ("[%s] thread %s of this inferior is "
3460 "waiting for vfork-done",
3461 tp->ptid.to_string ().c_str (),
3462 tp->inf->thread_waiting_for_vfork_done
3463 ->ptid.to_string ().c_str ());
3464 return;
3465 }
3466 }
3467 else
3468 {
3469 /* For all-stop targets, when we attempt to resume the inferior,
3470 we will only resume the vfork parent thread, this is handled
3471 in internal_resume_ptid.
3472
3473 Additionally, we will always be called with the vfork parent
3474 thread as the current thread (TP) thanks to follow_fork, as
3475 such the following assertion should hold.
3476
3477 Beyond this there is nothing more that needs to be done
3478 here. */
3479 gdb_assert (tp == tp->inf->thread_waiting_for_vfork_done);
3480 }
3481 }
3482
3483 /* When handling a vfork GDB removes all breakpoints from the program
3484 space in which the vfork is being handled. If we are following the
3485 child then GDB will set vfork_child member of the vfork parent
3486 inferior. Once the child has either exited or execd then GDB will
3487 detach from the parent process. Until that point GDB should not
3488 resume any thread in the parent process. */
3489 if (tp->inf->vfork_child != nullptr)
3490 {
3491 infrun_debug_printf ("[%s] thread is part of a vfork parent, child is %d",
3492 tp->ptid.to_string ().c_str (),
3493 tp->inf->vfork_child->pid);
3494 return;
3495 }
3496
3497 infrun_debug_printf ("resuming %s",
3498 tp->ptid.to_string ().c_str ());
3499
3500 execution_control_state ecs (tp);
3501 switch_to_thread (tp);
3502 keep_going_pass_signal (&ecs);
3503 if (!ecs.wait_some_more)
3504 error (_("Command aborted."));
3505 }
3506
3507 /* Basic routine for continuing the program in various fashions.
3508
3509 ADDR is the address to resume at, or -1 for resume where stopped.
3510 SIGGNAL is the signal to give it, or GDB_SIGNAL_0 for none,
3511 or GDB_SIGNAL_DEFAULT for act according to how it stopped.
3512
3513 You should call clear_proceed_status before calling proceed. */
3514
3515 void
3516 proceed (CORE_ADDR addr, enum gdb_signal siggnal)
3517 {
3518 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
3519
3520 struct regcache *regcache;
3521 struct gdbarch *gdbarch;
3522 CORE_ADDR pc;
3523
3524 /* If we're stopped at a fork/vfork, switch to either the parent or child
3525 thread as defined by the "set follow-fork-mode" command, or, if both
3526 the parent and child are controlled by GDB, and schedule-multiple is
3527 on, follow the child. If none of the above apply then we just proceed
3528 resuming the current thread. */
3529 if (!follow_fork ())
3530 {
3531 /* The target for some reason decided not to resume. */
3532 normal_stop ();
3533 if (target_can_async_p ())
3534 inferior_event_handler (INF_EXEC_COMPLETE);
3535 return;
3536 }
3537
3538 /* We'll update this if & when we switch to a new thread. */
3539 update_previous_thread ();
3540
3541 regcache = get_current_regcache ();
3542 gdbarch = regcache->arch ();
3543 const address_space *aspace = regcache->aspace ();
3544
3545 pc = regcache_read_pc_protected (regcache);
3546
3547 thread_info *cur_thr = inferior_thread ();
3548
3549 infrun_debug_printf ("cur_thr = %s", cur_thr->ptid.to_string ().c_str ());
3550
3551 /* Fill in with reasonable starting values. */
3552 init_thread_stepping_state (cur_thr);
3553
3554 gdb_assert (!thread_is_in_step_over_chain (cur_thr));
3555
3556 ptid_t resume_ptid
3557 = user_visible_resume_ptid (cur_thr->control.stepping_command);
3558 process_stratum_target *resume_target
3559 = user_visible_resume_target (resume_ptid);
3560
3561 check_multi_target_resumption (resume_target);
3562
3563 if (addr == (CORE_ADDR) -1)
3564 {
3565 if (cur_thr->stop_pc_p ()
3566 && pc == cur_thr->stop_pc ()
3567 && breakpoint_here_p (aspace, pc) == ordinary_breakpoint_here
3568 && execution_direction != EXEC_REVERSE)
3569 /* There is a breakpoint at the address we will resume at,
3570 step one instruction before inserting breakpoints so that
3571 we do not stop right away (and report a second hit at this
3572 breakpoint).
3573
3574 Note, we don't do this in reverse, because we won't
3575 actually be executing the breakpoint insn anyway.
3576 We'll be (un-)executing the previous instruction. */
3577 cur_thr->stepping_over_breakpoint = 1;
3578 else if (gdbarch_single_step_through_delay_p (gdbarch)
3579 && gdbarch_single_step_through_delay (gdbarch,
3580 get_current_frame ()))
3581 /* We stepped onto an instruction that needs to be stepped
3582 again before re-inserting the breakpoint, do so. */
3583 cur_thr->stepping_over_breakpoint = 1;
3584 }
3585 else
3586 {
3587 regcache_write_pc (regcache, addr);
3588 }
3589
3590 if (siggnal != GDB_SIGNAL_DEFAULT)
3591 cur_thr->set_stop_signal (siggnal);
3592
3593 /* If an exception is thrown from this point on, make sure to
3594 propagate GDB's knowledge of the executing state to the
3595 frontend/user running state. */
3596 scoped_finish_thread_state finish_state (resume_target, resume_ptid);
3597
3598 /* Even if RESUME_PTID is a wildcard, and we end up resuming fewer
3599 threads (e.g., we might need to set threads stepping over
3600 breakpoints first), from the user/frontend's point of view, all
3601 threads in RESUME_PTID are now running. Unless we're calling an
3602 inferior function, as in that case we pretend the inferior
3603 doesn't run at all. */
3604 if (!cur_thr->control.in_infcall)
3605 set_running (resume_target, resume_ptid, true);
3606
3607 infrun_debug_printf ("addr=%s, signal=%s, resume_ptid=%s",
3608 paddress (gdbarch, addr),
3609 gdb_signal_to_symbol_string (siggnal),
3610 resume_ptid.to_string ().c_str ());
3611
3612 annotate_starting ();
3613
3614 /* Make sure that output from GDB appears before output from the
3615 inferior. */
3616 gdb_flush (gdb_stdout);
3617
3618 /* Since we've marked the inferior running, give it the terminal. A
3619 QUIT/Ctrl-C from here on is forwarded to the target (which can
3620 still detect attempts to unblock a stuck connection with repeated
3621 Ctrl-C from within target_pass_ctrlc). */
3622 target_terminal::inferior ();
3623
3624 /* In a multi-threaded task we may select another thread and
3625 then continue or step.
3626
3627 But if a thread that we're resuming had stopped at a breakpoint,
3628 it will immediately cause another breakpoint stop without any
3629 execution (i.e. it will report a breakpoint hit incorrectly). So
3630 we must step over it first.
3631
3632 Look for threads other than the current (TP) that reported a
3633 breakpoint hit and haven't been resumed yet since. */
3634
3635 /* If scheduler locking applies, we can avoid iterating over all
3636 threads. */
3637 if (!non_stop && !schedlock_applies (cur_thr))
3638 {
3639 for (thread_info *tp : all_non_exited_threads (resume_target,
3640 resume_ptid))
3641 {
3642 switch_to_thread_no_regs (tp);
3643
3644 /* Ignore the current thread here. It's handled
3645 afterwards. */
3646 if (tp == cur_thr)
3647 continue;
3648
3649 if (!thread_still_needs_step_over (tp))
3650 continue;
3651
3652 gdb_assert (!thread_is_in_step_over_chain (tp));
3653
3654 infrun_debug_printf ("need to step-over [%s] first",
3655 tp->ptid.to_string ().c_str ());
3656
3657 global_thread_step_over_chain_enqueue (tp);
3658 }
3659
3660 switch_to_thread (cur_thr);
3661 }
3662
3663 /* Enqueue the current thread last, so that we move all other
3664 threads over their breakpoints first. */
3665 if (cur_thr->stepping_over_breakpoint)
3666 global_thread_step_over_chain_enqueue (cur_thr);
3667
3668 /* If the thread isn't started, we'll still need to set its prev_pc,
3669 so that switch_back_to_stepped_thread knows the thread hasn't
3670 advanced. Must do this before resuming any thread, as in
3671 all-stop/remote, once we resume we can't send any other packet
3672 until the target stops again. */
3673 cur_thr->prev_pc = regcache_read_pc_protected (regcache);
3674
3675 {
3676 scoped_disable_commit_resumed disable_commit_resumed ("proceeding");
3677 bool step_over_started = start_step_over ();
3678
3679 if (step_over_info_valid_p ())
3680 {
3681 /* Either this thread started a new in-line step over, or some
3682 other thread was already doing one. In either case, don't
3683 resume anything else until the step-over is finished. */
3684 }
3685 else if (step_over_started && !target_is_non_stop_p ())
3686 {
3687 /* A new displaced stepping sequence was started. In all-stop,
3688 we can't talk to the target anymore until it next stops. */
3689 }
3690 else if (!non_stop && target_is_non_stop_p ())
3691 {
3692 INFRUN_SCOPED_DEBUG_START_END
3693 ("resuming threads, all-stop-on-top-of-non-stop");
3694
3695 /* In all-stop, but the target is always in non-stop mode.
3696 Start all other threads that are implicitly resumed too. */
3697 for (thread_info *tp : all_non_exited_threads (resume_target,
3698 resume_ptid))
3699 {
3700 switch_to_thread_no_regs (tp);
3701 proceed_resume_thread_checked (tp);
3702 }
3703 }
3704 else
3705 proceed_resume_thread_checked (cur_thr);
3706
3707 disable_commit_resumed.reset_and_commit ();
3708 }
3709
3710 finish_state.release ();
3711
3712 /* If we've switched threads above, switch back to the previously
3713 current thread. We don't want the user to see a different
3714 selected thread. */
3715 switch_to_thread (cur_thr);
3716
3717 /* Tell the event loop to wait for it to stop. If the target
3718 supports asynchronous execution, it'll do this from within
3719 target_resume. */
3720 if (!target_can_async_p ())
3721 mark_async_event_handler (infrun_async_inferior_event_token);
3722 }
3723 \f
3724
3725 /* Start remote-debugging of a machine over a serial link. */
3726
3727 void
3728 start_remote (int from_tty)
3729 {
3730 inferior *inf = current_inferior ();
3731 inf->control.stop_soon = STOP_QUIETLY_REMOTE;
3732
3733 /* Always go on waiting for the target, regardless of the mode. */
3734 /* FIXME: cagney/1999-09-23: At present it isn't possible to
3735 indicate to wait_for_inferior that a target should timeout if
3736 nothing is returned (instead of just blocking). Because of this,
3737 targets expecting an immediate response need to, internally, set
3738 things up so that the target_wait() is forced to eventually
3739 timeout. */
3740 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
3741 differentiate to its caller what the state of the target is after
3742 the initial open has been performed. Here we're assuming that
3743 the target has stopped. It should be possible to eventually have
3744 target_open() return to the caller an indication that the target
3745 is currently running and GDB state should be set to the same as
3746 for an async run. */
3747 wait_for_inferior (inf);
3748
3749 /* Now that the inferior has stopped, do any bookkeeping like
3750 loading shared libraries. We want to do this before normal_stop,
3751 so that the displayed frame is up to date. */
3752 post_create_inferior (from_tty);
3753
3754 normal_stop ();
3755 }
3756
3757 /* Initialize static vars when a new inferior begins. */
3758
3759 void
3760 init_wait_for_inferior (void)
3761 {
3762 /* These are meaningless until the first time through wait_for_inferior. */
3763
3764 breakpoint_init_inferior (inf_starting);
3765
3766 clear_proceed_status (0);
3767
3768 nullify_last_target_wait_ptid ();
3769
3770 update_previous_thread ();
3771 }
3772
3773 \f
3774
3775 static void handle_inferior_event (struct execution_control_state *ecs);
3776
3777 static void handle_step_into_function (struct gdbarch *gdbarch,
3778 struct execution_control_state *ecs);
3779 static void handle_step_into_function_backward (struct gdbarch *gdbarch,
3780 struct execution_control_state *ecs);
3781 static void handle_signal_stop (struct execution_control_state *ecs);
3782 static void check_exception_resume (struct execution_control_state *,
3783 frame_info_ptr);
3784
3785 static void end_stepping_range (struct execution_control_state *ecs);
3786 static void stop_waiting (struct execution_control_state *ecs);
3787 static void keep_going (struct execution_control_state *ecs);
3788 static void process_event_stop_test (struct execution_control_state *ecs);
3789 static bool switch_back_to_stepped_thread (struct execution_control_state *ecs);
3790
3791 /* This function is attached as a "thread_stop_requested" observer.
3792 Cleanup local state that assumed the PTID was to be resumed, and
3793 report the stop to the frontend. */
3794
3795 static void
3796 infrun_thread_stop_requested (ptid_t ptid)
3797 {
3798 process_stratum_target *curr_target = current_inferior ()->process_target ();
3799
3800 /* PTID was requested to stop. If the thread was already stopped,
3801 but the user/frontend doesn't know about that yet (e.g., the
3802 thread had been temporarily paused for some step-over), set up
3803 for reporting the stop now. */
3804 for (thread_info *tp : all_threads (curr_target, ptid))
3805 {
3806 if (tp->state != THREAD_RUNNING)
3807 continue;
3808 if (tp->executing ())
3809 continue;
3810
3811 /* Remove matching threads from the step-over queue, so
3812 start_step_over doesn't try to resume them
3813 automatically. */
3814 if (thread_is_in_step_over_chain (tp))
3815 global_thread_step_over_chain_remove (tp);
3816
3817 /* If the thread is stopped, but the user/frontend doesn't
3818 know about that yet, queue a pending event, as if the
3819 thread had just stopped now. Unless the thread already had
3820 a pending event. */
3821 if (!tp->has_pending_waitstatus ())
3822 {
3823 target_waitstatus ws;
3824 ws.set_stopped (GDB_SIGNAL_0);
3825 tp->set_pending_waitstatus (ws);
3826 }
3827
3828 /* Clear the inline-frame state, since we're re-processing the
3829 stop. */
3830 clear_inline_frame_state (tp);
3831
3832 /* If this thread was paused because some other thread was
3833 doing an inline-step over, let that finish first. Once
3834 that happens, we'll restart all threads and consume pending
3835 stop events then. */
3836 if (step_over_info_valid_p ())
3837 continue;
3838
3839 /* Otherwise we can process the (new) pending event now. Set
3840 it so this pending event is considered by
3841 do_target_wait. */
3842 tp->set_resumed (true);
3843 }
3844 }
3845
3846 /* Delete the step resume, single-step and longjmp/exception resume
3847 breakpoints of TP. */
3848
3849 static void
3850 delete_thread_infrun_breakpoints (struct thread_info *tp)
3851 {
3852 delete_step_resume_breakpoint (tp);
3853 delete_exception_resume_breakpoint (tp);
3854 delete_single_step_breakpoints (tp);
3855 }
3856
3857 /* If the target still has execution, call FUNC for each thread that
3858 just stopped. In all-stop, that's all the non-exited threads; in
3859 non-stop, that's the current thread, only. */
3860
3861 typedef void (*for_each_just_stopped_thread_callback_func)
3862 (struct thread_info *tp);
3863
3864 static void
3865 for_each_just_stopped_thread (for_each_just_stopped_thread_callback_func func)
3866 {
3867 if (!target_has_execution () || inferior_ptid == null_ptid)
3868 return;
3869
3870 if (target_is_non_stop_p ())
3871 {
3872 /* If in non-stop mode, only the current thread stopped. */
3873 func (inferior_thread ());
3874 }
3875 else
3876 {
3877 /* In all-stop mode, all threads have stopped. */
3878 for (thread_info *tp : all_non_exited_threads ())
3879 func (tp);
3880 }
3881 }
3882
3883 /* Delete the step resume and longjmp/exception resume breakpoints of
3884 the threads that just stopped. */
3885
3886 static void
3887 delete_just_stopped_threads_infrun_breakpoints (void)
3888 {
3889 for_each_just_stopped_thread (delete_thread_infrun_breakpoints);
3890 }
3891
3892 /* Delete the single-step breakpoints of the threads that just
3893 stopped. */
3894
3895 static void
3896 delete_just_stopped_threads_single_step_breakpoints (void)
3897 {
3898 for_each_just_stopped_thread (delete_single_step_breakpoints);
3899 }
3900
3901 /* See infrun.h. */
3902
3903 void
3904 print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
3905 const struct target_waitstatus &ws)
3906 {
3907 infrun_debug_printf ("target_wait (%s [%s], status) =",
3908 waiton_ptid.to_string ().c_str (),
3909 target_pid_to_str (waiton_ptid).c_str ());
3910 infrun_debug_printf (" %s [%s],",
3911 result_ptid.to_string ().c_str (),
3912 target_pid_to_str (result_ptid).c_str ());
3913 infrun_debug_printf (" %s", ws.to_string ().c_str ());
3914 }
3915
3916 /* Select a thread at random, out of those which are resumed and have
3917 had events. */
3918
3919 static struct thread_info *
3920 random_pending_event_thread (inferior *inf, ptid_t waiton_ptid)
3921 {
3922 process_stratum_target *proc_target = inf->process_target ();
3923 thread_info *thread
3924 = proc_target->random_resumed_with_pending_wait_status (inf, waiton_ptid);
3925
3926 if (thread == nullptr)
3927 {
3928 infrun_debug_printf ("None found.");
3929 return nullptr;
3930 }
3931
3932 infrun_debug_printf ("Found %s.", thread->ptid.to_string ().c_str ());
3933 gdb_assert (thread->resumed ());
3934 gdb_assert (thread->has_pending_waitstatus ());
3935
3936 return thread;
3937 }
3938
3939 /* Wrapper for target_wait that first checks whether threads have
3940 pending statuses to report before actually asking the target for
3941 more events. INF is the inferior we're using to call target_wait
3942 on. */
3943
3944 static ptid_t
3945 do_target_wait_1 (inferior *inf, ptid_t ptid,
3946 target_waitstatus *status, target_wait_flags options)
3947 {
3948 struct thread_info *tp;
3949
3950 /* We know that we are looking for an event in the target of inferior
3951 INF, but we don't know which thread the event might come from. As
3952 such we want to make sure that INFERIOR_PTID is reset so that none of
3953 the wait code relies on it - doing so is always a mistake. */
3954 switch_to_inferior_no_thread (inf);
3955
3956 /* First check if there is a resumed thread with a wait status
3957 pending. */
3958 if (ptid == minus_one_ptid || ptid.is_pid ())
3959 {
3960 tp = random_pending_event_thread (inf, ptid);
3961 }
3962 else
3963 {
3964 infrun_debug_printf ("Waiting for specific thread %s.",
3965 ptid.to_string ().c_str ());
3966
3967 /* We have a specific thread to check. */
3968 tp = inf->find_thread (ptid);
3969 gdb_assert (tp != nullptr);
3970 if (!tp->has_pending_waitstatus ())
3971 tp = nullptr;
3972 }
3973
3974 if (tp != nullptr
3975 && (tp->stop_reason () == TARGET_STOPPED_BY_SW_BREAKPOINT
3976 || tp->stop_reason () == TARGET_STOPPED_BY_HW_BREAKPOINT))
3977 {
3978 struct regcache *regcache = get_thread_regcache (tp);
3979 struct gdbarch *gdbarch = regcache->arch ();
3980 CORE_ADDR pc;
3981 int discard = 0;
3982
3983 pc = regcache_read_pc (regcache);
3984
3985 if (pc != tp->stop_pc ())
3986 {
3987 infrun_debug_printf ("PC of %s changed. was=%s, now=%s",
3988 tp->ptid.to_string ().c_str (),
3989 paddress (gdbarch, tp->stop_pc ()),
3990 paddress (gdbarch, pc));
3991 discard = 1;
3992 }
3993 else if (!breakpoint_inserted_here_p (regcache->aspace (), pc))
3994 {
3995 infrun_debug_printf ("previous breakpoint of %s, at %s gone",
3996 tp->ptid.to_string ().c_str (),
3997 paddress (gdbarch, pc));
3998
3999 discard = 1;
4000 }
4001
4002 if (discard)
4003 {
4004 infrun_debug_printf ("pending event of %s cancelled.",
4005 tp->ptid.to_string ().c_str ());
4006
4007 tp->clear_pending_waitstatus ();
4008 target_waitstatus ws;
4009 ws.set_spurious ();
4010 tp->set_pending_waitstatus (ws);
4011 tp->set_stop_reason (TARGET_STOPPED_BY_NO_REASON);
4012 }
4013 }
4014
4015 if (tp != nullptr)
4016 {
4017 infrun_debug_printf ("Using pending wait status %s for %s.",
4018 tp->pending_waitstatus ().to_string ().c_str (),
4019 tp->ptid.to_string ().c_str ());
4020
4021 /* Now that we've selected our final event LWP, un-adjust its PC
4022 if it was a software breakpoint (and the target doesn't
4023 always adjust the PC itself). */
4024 if (tp->stop_reason () == TARGET_STOPPED_BY_SW_BREAKPOINT
4025 && !target_supports_stopped_by_sw_breakpoint ())
4026 {
4027 struct regcache *regcache;
4028 struct gdbarch *gdbarch;
4029 int decr_pc;
4030
4031 regcache = get_thread_regcache (tp);
4032 gdbarch = regcache->arch ();
4033
4034 decr_pc = gdbarch_decr_pc_after_break (gdbarch);
4035 if (decr_pc != 0)
4036 {
4037 CORE_ADDR pc;
4038
4039 pc = regcache_read_pc (regcache);
4040 regcache_write_pc (regcache, pc + decr_pc);
4041 }
4042 }
4043
4044 tp->set_stop_reason (TARGET_STOPPED_BY_NO_REASON);
4045 *status = tp->pending_waitstatus ();
4046 tp->clear_pending_waitstatus ();
4047
4048 /* Wake up the event loop again, until all pending events are
4049 processed. */
4050 if (target_is_async_p ())
4051 mark_async_event_handler (infrun_async_inferior_event_token);
4052 return tp->ptid;
4053 }
4054
4055 /* But if we don't find one, we'll have to wait. */
4056
4057 /* We can't ask a non-async target to do a non-blocking wait, so this will be
4058 a blocking wait. */
4059 if (!target_can_async_p ())
4060 options &= ~TARGET_WNOHANG;
4061
4062 return target_wait (ptid, status, options);
4063 }
4064
4065 /* Wrapper for target_wait that first checks whether threads have
4066 pending statuses to report before actually asking the target for
4067 more events. Polls for events from all inferiors/targets. */
4068
4069 static bool
4070 do_target_wait (execution_control_state *ecs, target_wait_flags options)
4071 {
4072 int num_inferiors = 0;
4073 int random_selector;
4074
4075 /* For fairness, we pick the first inferior/target to poll at random
4076 out of all inferiors that may report events, and then continue
4077 polling the rest of the inferior list starting from that one in a
4078 circular fashion until the whole list is polled once. */
4079
4080 auto inferior_matches = [] (inferior *inf)
4081 {
4082 return inf->process_target () != nullptr;
4083 };
4084
4085 /* First see how many matching inferiors we have. */
4086 for (inferior *inf : all_inferiors ())
4087 if (inferior_matches (inf))
4088 num_inferiors++;
4089
4090 if (num_inferiors == 0)
4091 {
4092 ecs->ws.set_ignore ();
4093 return false;
4094 }
4095
4096 /* Now randomly pick an inferior out of those that matched. */
4097 random_selector = (int)
4098 ((num_inferiors * (double) rand ()) / (RAND_MAX + 1.0));
4099
4100 if (num_inferiors > 1)
4101 infrun_debug_printf ("Found %d inferiors, starting at #%d",
4102 num_inferiors, random_selector);
4103
4104 /* Select the Nth inferior that matched. */
4105
4106 inferior *selected = nullptr;
4107
4108 for (inferior *inf : all_inferiors ())
4109 if (inferior_matches (inf))
4110 if (random_selector-- == 0)
4111 {
4112 selected = inf;
4113 break;
4114 }
4115
4116 /* Now poll for events out of each of the matching inferior's
4117 targets, starting from the selected one. */
4118
4119 auto do_wait = [&] (inferior *inf)
4120 {
4121 ecs->ptid = do_target_wait_1 (inf, minus_one_ptid, &ecs->ws, options);
4122 ecs->target = inf->process_target ();
4123 return (ecs->ws.kind () != TARGET_WAITKIND_IGNORE);
4124 };
4125
4126 /* Needed in 'all-stop + target-non-stop' mode, because we end up
4127 here spuriously after the target is all stopped and we've already
4128 reported the stop to the user, polling for events. */
4129 scoped_restore_current_thread restore_thread;
4130
4131 intrusive_list_iterator<inferior> start
4132 = inferior_list.iterator_to (*selected);
4133
4134 for (intrusive_list_iterator<inferior> it = start;
4135 it != inferior_list.end ();
4136 ++it)
4137 {
4138 inferior *inf = &*it;
4139
4140 if (inferior_matches (inf) && do_wait (inf))
4141 return true;
4142 }
4143
4144 for (intrusive_list_iterator<inferior> it = inferior_list.begin ();
4145 it != start;
4146 ++it)
4147 {
4148 inferior *inf = &*it;
4149
4150 if (inferior_matches (inf) && do_wait (inf))
4151 return true;
4152 }
4153
4154 ecs->ws.set_ignore ();
4155 return false;
4156 }
4157
4158 /* An event reported by wait_one. */
4159
4160 struct wait_one_event
4161 {
4162 /* The target the event came out of. */
4163 process_stratum_target *target;
4164
4165 /* The PTID the event was for. */
4166 ptid_t ptid;
4167
4168 /* The waitstatus. */
4169 target_waitstatus ws;
4170 };
4171
4172 static bool handle_one (const wait_one_event &event);
4173 static int finish_step_over (struct execution_control_state *ecs);
4174
4175 /* Prepare and stabilize the inferior for detaching it. E.g.,
4176 detaching while a thread is displaced stepping is a recipe for
4177 crashing it, as nothing would readjust the PC out of the scratch
4178 pad. */
4179
4180 void
4181 prepare_for_detach (void)
4182 {
4183 struct inferior *inf = current_inferior ();
4184 ptid_t pid_ptid = ptid_t (inf->pid);
4185 scoped_restore_current_thread restore_thread;
4186
4187 scoped_restore restore_detaching = make_scoped_restore (&inf->detaching, true);
4188
4189 /* Remove all threads of INF from the global step-over chain. We
4190 want to stop any ongoing step-over, not start any new one. */
4191 thread_step_over_list_safe_range range
4192 = make_thread_step_over_list_safe_range (global_thread_step_over_list);
4193
4194 for (thread_info *tp : range)
4195 if (tp->inf == inf)
4196 {
4197 infrun_debug_printf ("removing thread %s from global step over chain",
4198 tp->ptid.to_string ().c_str ());
4199 global_thread_step_over_chain_remove (tp);
4200 }
4201
4202 /* If we were already in the middle of an inline step-over, and the
4203 thread stepping belongs to the inferior we're detaching, we need
4204 to restart the threads of other inferiors. */
4205 if (step_over_info.thread != -1)
4206 {
4207 infrun_debug_printf ("inline step-over in-process while detaching");
4208
4209 thread_info *thr = find_thread_global_id (step_over_info.thread);
4210 if (thr->inf == inf)
4211 {
4212 /* Since we removed threads of INF from the step-over chain,
4213 we know this won't start a step-over for INF. */
4214 clear_step_over_info ();
4215
4216 if (target_is_non_stop_p ())
4217 {
4218 /* Start a new step-over in another thread if there's
4219 one that needs it. */
4220 start_step_over ();
4221
4222 /* Restart all other threads (except the
4223 previously-stepping thread, since that one is still
4224 running). */
4225 if (!step_over_info_valid_p ())
4226 restart_threads (thr);
4227 }
4228 }
4229 }
4230
4231 if (displaced_step_in_progress (inf))
4232 {
4233 infrun_debug_printf ("displaced-stepping in-process while detaching");
4234
4235 /* Stop threads currently displaced stepping, aborting it. */
4236
4237 for (thread_info *thr : inf->non_exited_threads ())
4238 {
4239 if (thr->displaced_step_state.in_progress ())
4240 {
4241 if (thr->executing ())
4242 {
4243 if (!thr->stop_requested)
4244 {
4245 target_stop (thr->ptid);
4246 thr->stop_requested = true;
4247 }
4248 }
4249 else
4250 thr->set_resumed (false);
4251 }
4252 }
4253
4254 while (displaced_step_in_progress (inf))
4255 {
4256 wait_one_event event;
4257
4258 event.target = inf->process_target ();
4259 event.ptid = do_target_wait_1 (inf, pid_ptid, &event.ws, 0);
4260
4261 if (debug_infrun)
4262 print_target_wait_results (pid_ptid, event.ptid, event.ws);
4263
4264 handle_one (event);
4265 }
4266
4267 /* It's OK to leave some of the threads of INF stopped, since
4268 they'll be detached shortly. */
4269 }
4270 }
4271
4272 /* If all-stop, but there exists a non-stop target, stop all threads
4273 now that we're presenting the stop to the user. */
4274
4275 static void
4276 stop_all_threads_if_all_stop_mode ()
4277 {
4278 if (!non_stop && exists_non_stop_target ())
4279 stop_all_threads ("presenting stop to user in all-stop");
4280 }
4281
4282 /* Wait for control to return from inferior to debugger.
4283
4284 If inferior gets a signal, we may decide to start it up again
4285 instead of returning. That is why there is a loop in this function.
4286 When this function actually returns it means the inferior
4287 should be left stopped and GDB should read more commands. */
4288
4289 static void
4290 wait_for_inferior (inferior *inf)
4291 {
4292 infrun_debug_printf ("wait_for_inferior ()");
4293
4294 SCOPE_EXIT { delete_just_stopped_threads_infrun_breakpoints (); };
4295
4296 /* If an error happens while handling the event, propagate GDB's
4297 knowledge of the executing state to the frontend/user running
4298 state. */
4299 scoped_finish_thread_state finish_state
4300 (inf->process_target (), minus_one_ptid);
4301
4302 while (1)
4303 {
4304 execution_control_state ecs;
4305
4306 overlay_cache_invalid = 1;
4307
4308 /* Flush target cache before starting to handle each event.
4309 Target was running and cache could be stale. This is just a
4310 heuristic. Running threads may modify target memory, but we
4311 don't get any event. */
4312 target_dcache_invalidate ();
4313
4314 ecs.ptid = do_target_wait_1 (inf, minus_one_ptid, &ecs.ws, 0);
4315 ecs.target = inf->process_target ();
4316
4317 if (debug_infrun)
4318 print_target_wait_results (minus_one_ptid, ecs.ptid, ecs.ws);
4319
4320 /* Now figure out what to do with the result of the result. */
4321 handle_inferior_event (&ecs);
4322
4323 if (!ecs.wait_some_more)
4324 break;
4325 }
4326
4327 stop_all_threads_if_all_stop_mode ();
4328
4329 /* No error, don't finish the state yet. */
4330 finish_state.release ();
4331 }
4332
4333 /* Cleanup that reinstalls the readline callback handler, if the
4334 target is running in the background. If while handling the target
4335 event something triggered a secondary prompt, like e.g., a
4336 pagination prompt, we'll have removed the callback handler (see
4337 gdb_readline_wrapper_line). Need to do this as we go back to the
4338 event loop, ready to process further input. Note this has no
4339 effect if the handler hasn't actually been removed, because calling
4340 rl_callback_handler_install resets the line buffer, thus losing
4341 input. */
4342
4343 static void
4344 reinstall_readline_callback_handler_cleanup ()
4345 {
4346 struct ui *ui = current_ui;
4347
4348 if (!ui->async)
4349 {
4350 /* We're not going back to the top level event loop yet. Don't
4351 install the readline callback, as it'd prep the terminal,
4352 readline-style (raw, noecho) (e.g., --batch). We'll install
4353 it the next time the prompt is displayed, when we're ready
4354 for input. */
4355 return;
4356 }
4357
4358 if (ui->command_editing && ui->prompt_state != PROMPT_BLOCKED)
4359 gdb_rl_callback_handler_reinstall ();
4360 }
4361
4362 /* Clean up the FSMs of threads that are now stopped. In non-stop,
4363 that's just the event thread. In all-stop, that's all threads. In
4364 all-stop, threads that had a pending exit no longer have a reason
4365 to be around, as their FSMs/commands are canceled, so we delete
4366 them. This avoids "info threads" listing such threads as if they
4367 were alive (and failing to read their registers), the user being
4368 able to select and resume them (and that failing), etc. */
4369
4370 static void
4371 clean_up_just_stopped_threads_fsms (struct execution_control_state *ecs)
4372 {
4373 /* The first clean_up call below assumes the event thread is the current
4374 one. */
4375 if (ecs->event_thread != nullptr)
4376 gdb_assert (ecs->event_thread == inferior_thread ());
4377
4378 if (ecs->event_thread != nullptr
4379 && ecs->event_thread->thread_fsm () != nullptr)
4380 ecs->event_thread->thread_fsm ()->clean_up (ecs->event_thread);
4381
4382 if (!non_stop)
4383 {
4384 scoped_restore_current_thread restore_thread;
4385
4386 for (thread_info *thr : all_threads_safe ())
4387 {
4388 if (thr->state == THREAD_EXITED)
4389 continue;
4390
4391 if (thr == ecs->event_thread)
4392 continue;
4393
4394 if (thr->thread_fsm () != nullptr)
4395 {
4396 switch_to_thread (thr);
4397 thr->thread_fsm ()->clean_up (thr);
4398 }
4399
4400 /* As we are cancelling the command/FSM of this thread,
4401 whatever was the reason we needed to report a thread
4402 exited event to the user, that reason is gone. Delete
4403 the thread, so that the user doesn't see it in the thread
4404 list, the next proceed doesn't try to resume it, etc. */
4405 if (thr->has_pending_waitstatus ()
4406 && (thr->pending_waitstatus ().kind ()
4407 == TARGET_WAITKIND_THREAD_EXITED))
4408 delete_thread (thr);
4409 }
4410 }
4411 }
4412
4413 /* Helper for all_uis_check_sync_execution_done that works on the
4414 current UI. */
4415
4416 static void
4417 check_curr_ui_sync_execution_done (void)
4418 {
4419 struct ui *ui = current_ui;
4420
4421 if (ui->prompt_state == PROMPT_NEEDED
4422 && ui->async
4423 && !gdb_in_secondary_prompt_p (ui))
4424 {
4425 target_terminal::ours ();
4426 top_level_interpreter ()->on_sync_execution_done ();
4427 ui->register_file_handler ();
4428 }
4429 }
4430
4431 /* See infrun.h. */
4432
4433 void
4434 all_uis_check_sync_execution_done (void)
4435 {
4436 SWITCH_THRU_ALL_UIS ()
4437 {
4438 check_curr_ui_sync_execution_done ();
4439 }
4440 }
4441
4442 /* See infrun.h. */
4443
4444 void
4445 all_uis_on_sync_execution_starting (void)
4446 {
4447 SWITCH_THRU_ALL_UIS ()
4448 {
4449 if (current_ui->prompt_state == PROMPT_NEEDED)
4450 async_disable_stdin ();
4451 }
4452 }
4453
4454 /* A quit_handler callback installed while we're handling inferior
4455 events. */
4456
4457 static void
4458 infrun_quit_handler ()
4459 {
4460 if (target_terminal::is_ours ())
4461 {
4462 /* Do nothing.
4463
4464 default_quit_handler would throw a quit in this case, but if
4465 we're handling an event while we have the terminal, it means
4466 the target is running a background execution command, and
4467 thus when users press Ctrl-C, they're wanting to interrupt
4468 whatever command they were executing in the command line.
4469 E.g.:
4470
4471 (gdb) c&
4472 (gdb) foo bar whatever<ctrl-c>
4473
4474 That Ctrl-C should clear the input line, not interrupt event
4475 handling if it happens that the user types Ctrl-C at just the
4476 "wrong" time!
4477
4478 It's as-if background event handling was handled by a
4479 separate background thread.
4480
4481 To be clear, the Ctrl-C is not lost -- it will be processed
4482 by the next QUIT call once we're out of fetch_inferior_event
4483 again. */
4484 }
4485 else
4486 {
4487 if (check_quit_flag ())
4488 target_pass_ctrlc ();
4489 }
4490 }
4491
4492 /* Asynchronous version of wait_for_inferior. It is called by the
4493 event loop whenever a change of state is detected on the file
4494 descriptor corresponding to the target. It can be called more than
4495 once to complete a single execution command. In such cases we need
4496 to keep the state in a global variable ECSS. If it is the last time
4497 that this function is called for a single execution command, then
4498 report to the user that the inferior has stopped, and do the
4499 necessary cleanups. */
4500
4501 void
4502 fetch_inferior_event ()
4503 {
4504 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
4505
4506 execution_control_state ecs;
4507 int cmd_done = 0;
4508
4509 /* Events are always processed with the main UI as current UI. This
4510 way, warnings, debug output, etc. are always consistently sent to
4511 the main console. */
4512 scoped_restore save_ui = make_scoped_restore (&current_ui, main_ui);
4513
4514 /* Temporarily disable pagination. Otherwise, the user would be
4515 given an option to press 'q' to quit, which would cause an early
4516 exit and could leave GDB in a half-baked state. */
4517 scoped_restore save_pagination
4518 = make_scoped_restore (&pagination_enabled, false);
4519
4520 /* Install a quit handler that does nothing if we have the terminal
4521 (meaning the target is running a background execution command),
4522 so that Ctrl-C never interrupts GDB before the event is fully
4523 handled. */
4524 scoped_restore restore_quit_handler
4525 = make_scoped_restore (&quit_handler, infrun_quit_handler);
4526
4527 /* Make sure a SIGINT does not interrupt an extension language while
4528 we're handling an event. That could interrupt a Python unwinder
4529 or a Python observer or some such. A Ctrl-C should either be
4530 forwarded to the inferior if the inferior has the terminal, or,
4531 if GDB has the terminal, should interrupt the command the user is
4532 typing in the CLI. */
4533 scoped_disable_cooperative_sigint_handling restore_coop_sigint;
4534
4535 /* End up with readline processing input, if necessary. */
4536 {
4537 SCOPE_EXIT { reinstall_readline_callback_handler_cleanup (); };
4538
4539 /* We're handling a live event, so make sure we're doing live
4540 debugging. If we're looking at traceframes while the target is
4541 running, we're going to need to get back to that mode after
4542 handling the event. */
4543 gdb::optional<scoped_restore_current_traceframe> maybe_restore_traceframe;
4544 if (non_stop)
4545 {
4546 maybe_restore_traceframe.emplace ();
4547 set_current_traceframe (-1);
4548 }
4549
4550 /* The user/frontend should not notice a thread switch due to
4551 internal events. Make sure we revert to the user selected
4552 thread and frame after handling the event and running any
4553 breakpoint commands. */
4554 scoped_restore_current_thread restore_thread;
4555
4556 overlay_cache_invalid = 1;
4557 /* Flush target cache before starting to handle each event. Target
4558 was running and cache could be stale. This is just a heuristic.
4559 Running threads may modify target memory, but we don't get any
4560 event. */
4561 target_dcache_invalidate ();
4562
4563 scoped_restore save_exec_dir
4564 = make_scoped_restore (&execution_direction,
4565 target_execution_direction ());
4566
4567 /* Allow targets to pause their resumed threads while we handle
4568 the event. */
4569 scoped_disable_commit_resumed disable_commit_resumed ("handling event");
4570
4571 if (!do_target_wait (&ecs, TARGET_WNOHANG))
4572 {
4573 infrun_debug_printf ("do_target_wait returned no event");
4574 disable_commit_resumed.reset_and_commit ();
4575 return;
4576 }
4577
4578 gdb_assert (ecs.ws.kind () != TARGET_WAITKIND_IGNORE);
4579
4580 /* Switch to the inferior that generated the event, so we can do
4581 target calls. If the event was not associated to a ptid, */
4582 if (ecs.ptid != null_ptid
4583 && ecs.ptid != minus_one_ptid)
4584 switch_to_inferior_no_thread (find_inferior_ptid (ecs.target, ecs.ptid));
4585 else
4586 switch_to_target_no_thread (ecs.target);
4587
4588 if (debug_infrun)
4589 print_target_wait_results (minus_one_ptid, ecs.ptid, ecs.ws);
4590
4591 /* If an error happens while handling the event, propagate GDB's
4592 knowledge of the executing state to the frontend/user running
4593 state. */
4594 ptid_t finish_ptid = !target_is_non_stop_p () ? minus_one_ptid : ecs.ptid;
4595 scoped_finish_thread_state finish_state (ecs.target, finish_ptid);
4596
4597 /* Get executed before scoped_restore_current_thread above to apply
4598 still for the thread which has thrown the exception. */
4599 auto defer_bpstat_clear
4600 = make_scope_exit (bpstat_clear_actions);
4601 auto defer_delete_threads
4602 = make_scope_exit (delete_just_stopped_threads_infrun_breakpoints);
4603
4604 int stop_id = get_stop_id ();
4605
4606 /* Now figure out what to do with the result of the result. */
4607 handle_inferior_event (&ecs);
4608
4609 if (!ecs.wait_some_more)
4610 {
4611 struct inferior *inf = find_inferior_ptid (ecs.target, ecs.ptid);
4612 bool should_stop = true;
4613 struct thread_info *thr = ecs.event_thread;
4614
4615 delete_just_stopped_threads_infrun_breakpoints ();
4616
4617 if (thr != nullptr && thr->thread_fsm () != nullptr)
4618 should_stop = thr->thread_fsm ()->should_stop (thr);
4619
4620 if (!should_stop)
4621 {
4622 keep_going (&ecs);
4623 }
4624 else
4625 {
4626 bool should_notify_stop = true;
4627 bool proceeded = false;
4628
4629 stop_all_threads_if_all_stop_mode ();
4630
4631 clean_up_just_stopped_threads_fsms (&ecs);
4632
4633 if (stop_id != get_stop_id ())
4634 {
4635 /* If the stop-id has changed then a stop has already been
4636 presented to the user in handle_inferior_event, this is
4637 likely a failed inferior call. As the stop has already
4638 been announced then we should not notify again.
4639
4640 Also, if the prompt state is not PROMPT_NEEDED then GDB
4641 will not be ready for user input after this function. */
4642 should_notify_stop = false;
4643 gdb_assert (current_ui->prompt_state == PROMPT_NEEDED);
4644 }
4645 else if (thr != nullptr && thr->thread_fsm () != nullptr)
4646 should_notify_stop
4647 = thr->thread_fsm ()->should_notify_stop ();
4648
4649 if (should_notify_stop)
4650 {
4651 /* We may not find an inferior if this was a process exit. */
4652 if (inf == nullptr || inf->control.stop_soon == NO_STOP_QUIETLY)
4653 proceeded = normal_stop ();
4654 }
4655
4656 if (!proceeded)
4657 {
4658 inferior_event_handler (INF_EXEC_COMPLETE);
4659 cmd_done = 1;
4660 }
4661
4662 /* If we got a TARGET_WAITKIND_NO_RESUMED event, then the
4663 previously selected thread is gone. We have two
4664 choices - switch to no thread selected, or restore the
4665 previously selected thread (now exited). We chose the
4666 later, just because that's what GDB used to do. After
4667 this, "info threads" says "The current thread <Thread
4668 ID 2> has terminated." instead of "No thread
4669 selected.". */
4670 if (!non_stop
4671 && cmd_done
4672 && ecs.ws.kind () != TARGET_WAITKIND_NO_RESUMED)
4673 restore_thread.dont_restore ();
4674 }
4675 }
4676
4677 defer_delete_threads.release ();
4678 defer_bpstat_clear.release ();
4679
4680 /* No error, don't finish the thread states yet. */
4681 finish_state.release ();
4682
4683 disable_commit_resumed.reset_and_commit ();
4684
4685 /* This scope is used to ensure that readline callbacks are
4686 reinstalled here. */
4687 }
4688
4689 /* Handling this event might have caused some inferiors to become prunable.
4690 For example, the exit of an inferior that was automatically added. Try
4691 to get rid of them. Keeping those around slows down things linearly.
4692
4693 Note that this never removes the current inferior. Therefore, call this
4694 after RESTORE_THREAD went out of scope, in case the event inferior (which was
4695 temporarily made the current inferior) is meant to be deleted.
4696
4697 Call this before all_uis_check_sync_execution_done, so that notifications about
4698 removed inferiors appear before the prompt. */
4699 prune_inferiors ();
4700
4701 /* If a UI was in sync execution mode, and now isn't, restore its
4702 prompt (a synchronous execution command has finished, and we're
4703 ready for input). */
4704 all_uis_check_sync_execution_done ();
4705
4706 if (cmd_done
4707 && exec_done_display_p
4708 && (inferior_ptid == null_ptid
4709 || inferior_thread ()->state != THREAD_RUNNING))
4710 gdb_printf (_("completed.\n"));
4711 }
4712
4713 /* See infrun.h. */
4714
4715 void
4716 set_step_info (thread_info *tp, frame_info_ptr frame,
4717 struct symtab_and_line sal)
4718 {
4719 /* This can be removed once this function no longer implicitly relies on the
4720 inferior_ptid value. */
4721 gdb_assert (inferior_ptid == tp->ptid);
4722
4723 tp->control.step_frame_id = get_frame_id (frame);
4724 tp->control.step_stack_frame_id = get_stack_frame_id (frame);
4725
4726 tp->current_symtab = sal.symtab;
4727 tp->current_line = sal.line;
4728
4729 infrun_debug_printf
4730 ("symtab = %s, line = %d, step_frame_id = %s, step_stack_frame_id = %s",
4731 tp->current_symtab != nullptr ? tp->current_symtab->filename : "<null>",
4732 tp->current_line,
4733 tp->control.step_frame_id.to_string ().c_str (),
4734 tp->control.step_stack_frame_id.to_string ().c_str ());
4735 }
4736
4737 /* Clear context switchable stepping state. */
4738
4739 void
4740 init_thread_stepping_state (struct thread_info *tss)
4741 {
4742 tss->stepped_breakpoint = 0;
4743 tss->stepping_over_breakpoint = 0;
4744 tss->stepping_over_watchpoint = 0;
4745 tss->step_after_step_resume_breakpoint = 0;
4746 }
4747
4748 /* See infrun.h. */
4749
4750 void
4751 set_last_target_status (process_stratum_target *target, ptid_t ptid,
4752 const target_waitstatus &status)
4753 {
4754 target_last_proc_target = target;
4755 target_last_wait_ptid = ptid;
4756 target_last_waitstatus = status;
4757 }
4758
4759 /* See infrun.h. */
4760
4761 void
4762 get_last_target_status (process_stratum_target **target, ptid_t *ptid,
4763 target_waitstatus *status)
4764 {
4765 if (target != nullptr)
4766 *target = target_last_proc_target;
4767 if (ptid != nullptr)
4768 *ptid = target_last_wait_ptid;
4769 if (status != nullptr)
4770 *status = target_last_waitstatus;
4771 }
4772
4773 /* See infrun.h. */
4774
4775 void
4776 nullify_last_target_wait_ptid (void)
4777 {
4778 target_last_proc_target = nullptr;
4779 target_last_wait_ptid = minus_one_ptid;
4780 target_last_waitstatus = {};
4781 }
4782
4783 /* Switch thread contexts. */
4784
4785 static void
4786 context_switch (execution_control_state *ecs)
4787 {
4788 if (ecs->ptid != inferior_ptid
4789 && (inferior_ptid == null_ptid
4790 || ecs->event_thread != inferior_thread ()))
4791 {
4792 infrun_debug_printf ("Switching context from %s to %s",
4793 inferior_ptid.to_string ().c_str (),
4794 ecs->ptid.to_string ().c_str ());
4795 }
4796
4797 switch_to_thread (ecs->event_thread);
4798 }
4799
4800 /* If the target can't tell whether we've hit breakpoints
4801 (target_supports_stopped_by_sw_breakpoint), and we got a SIGTRAP,
4802 check whether that could have been caused by a breakpoint. If so,
4803 adjust the PC, per gdbarch_decr_pc_after_break. */
4804
4805 static void
4806 adjust_pc_after_break (struct thread_info *thread,
4807 const target_waitstatus &ws)
4808 {
4809 struct regcache *regcache;
4810 struct gdbarch *gdbarch;
4811 CORE_ADDR breakpoint_pc, decr_pc;
4812
4813 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
4814 we aren't, just return.
4815
4816 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
4817 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
4818 implemented by software breakpoints should be handled through the normal
4819 breakpoint layer.
4820
4821 NOTE drow/2004-01-31: On some targets, breakpoints may generate
4822 different signals (SIGILL or SIGEMT for instance), but it is less
4823 clear where the PC is pointing afterwards. It may not match
4824 gdbarch_decr_pc_after_break. I don't know any specific target that
4825 generates these signals at breakpoints (the code has been in GDB since at
4826 least 1992) so I can not guess how to handle them here.
4827
4828 In earlier versions of GDB, a target with
4829 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
4830 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
4831 target with both of these set in GDB history, and it seems unlikely to be
4832 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4833
4834 if (ws.kind () != TARGET_WAITKIND_STOPPED)
4835 return;
4836
4837 if (ws.sig () != GDB_SIGNAL_TRAP)
4838 return;
4839
4840 /* In reverse execution, when a breakpoint is hit, the instruction
4841 under it has already been de-executed. The reported PC always
4842 points at the breakpoint address, so adjusting it further would
4843 be wrong. E.g., consider this case on a decr_pc_after_break == 1
4844 architecture:
4845
4846 B1 0x08000000 : INSN1
4847 B2 0x08000001 : INSN2
4848 0x08000002 : INSN3
4849 PC -> 0x08000003 : INSN4
4850
4851 Say you're stopped at 0x08000003 as above. Reverse continuing
4852 from that point should hit B2 as below. Reading the PC when the
4853 SIGTRAP is reported should read 0x08000001 and INSN2 should have
4854 been de-executed already.
4855
4856 B1 0x08000000 : INSN1
4857 B2 PC -> 0x08000001 : INSN2
4858 0x08000002 : INSN3
4859 0x08000003 : INSN4
4860
4861 We can't apply the same logic as for forward execution, because
4862 we would wrongly adjust the PC to 0x08000000, since there's a
4863 breakpoint at PC - 1. We'd then report a hit on B1, although
4864 INSN1 hadn't been de-executed yet. Doing nothing is the correct
4865 behaviour. */
4866 if (execution_direction == EXEC_REVERSE)
4867 return;
4868
4869 /* If the target can tell whether the thread hit a SW breakpoint,
4870 trust it. Targets that can tell also adjust the PC
4871 themselves. */
4872 if (target_supports_stopped_by_sw_breakpoint ())
4873 return;
4874
4875 /* Note that relying on whether a breakpoint is planted in memory to
4876 determine this can fail. E.g,. the breakpoint could have been
4877 removed since. Or the thread could have been told to step an
4878 instruction the size of a breakpoint instruction, and only
4879 _after_ was a breakpoint inserted at its address. */
4880
4881 /* If this target does not decrement the PC after breakpoints, then
4882 we have nothing to do. */
4883 regcache = get_thread_regcache (thread);
4884 gdbarch = regcache->arch ();
4885
4886 decr_pc = gdbarch_decr_pc_after_break (gdbarch);
4887 if (decr_pc == 0)
4888 return;
4889
4890 const address_space *aspace = regcache->aspace ();
4891
4892 /* Find the location where (if we've hit a breakpoint) the
4893 breakpoint would be. */
4894 breakpoint_pc = regcache_read_pc (regcache) - decr_pc;
4895
4896 /* If the target can't tell whether a software breakpoint triggered,
4897 fallback to figuring it out based on breakpoints we think were
4898 inserted in the target, and on whether the thread was stepped or
4899 continued. */
4900
4901 /* Check whether there actually is a software breakpoint inserted at
4902 that location.
4903
4904 If in non-stop mode, a race condition is possible where we've
4905 removed a breakpoint, but stop events for that breakpoint were
4906 already queued and arrive later. To suppress those spurious
4907 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
4908 and retire them after a number of stop events are reported. Note
4909 this is an heuristic and can thus get confused. The real fix is
4910 to get the "stopped by SW BP and needs adjustment" info out of
4911 the target/kernel (and thus never reach here; see above). */
4912 if (software_breakpoint_inserted_here_p (aspace, breakpoint_pc)
4913 || (target_is_non_stop_p ()
4914 && moribund_breakpoint_here_p (aspace, breakpoint_pc)))
4915 {
4916 gdb::optional<scoped_restore_tmpl<int>> restore_operation_disable;
4917
4918 if (record_full_is_used ())
4919 restore_operation_disable.emplace
4920 (record_full_gdb_operation_disable_set ());
4921
4922 /* When using hardware single-step, a SIGTRAP is reported for both
4923 a completed single-step and a software breakpoint. Need to
4924 differentiate between the two, as the latter needs adjusting
4925 but the former does not.
4926
4927 The SIGTRAP can be due to a completed hardware single-step only if
4928 - we didn't insert software single-step breakpoints
4929 - this thread is currently being stepped
4930
4931 If any of these events did not occur, we must have stopped due
4932 to hitting a software breakpoint, and have to back up to the
4933 breakpoint address.
4934
4935 As a special case, we could have hardware single-stepped a
4936 software breakpoint. In this case (prev_pc == breakpoint_pc),
4937 we also need to back up to the breakpoint address. */
4938
4939 if (thread_has_single_step_breakpoints_set (thread)
4940 || !currently_stepping (thread)
4941 || (thread->stepped_breakpoint
4942 && thread->prev_pc == breakpoint_pc))
4943 regcache_write_pc (regcache, breakpoint_pc);
4944 }
4945 }
4946
4947 static bool
4948 stepped_in_from (frame_info_ptr frame, struct frame_id step_frame_id)
4949 {
4950 for (frame = get_prev_frame (frame);
4951 frame != nullptr;
4952 frame = get_prev_frame (frame))
4953 {
4954 if (get_frame_id (frame) == step_frame_id)
4955 return true;
4956
4957 if (get_frame_type (frame) != INLINE_FRAME)
4958 break;
4959 }
4960
4961 return false;
4962 }
4963
4964 /* Look for an inline frame that is marked for skip.
4965 If PREV_FRAME is TRUE start at the previous frame,
4966 otherwise start at the current frame. Stop at the
4967 first non-inline frame, or at the frame where the
4968 step started. */
4969
4970 static bool
4971 inline_frame_is_marked_for_skip (bool prev_frame, struct thread_info *tp)
4972 {
4973 frame_info_ptr frame = get_current_frame ();
4974
4975 if (prev_frame)
4976 frame = get_prev_frame (frame);
4977
4978 for (; frame != nullptr; frame = get_prev_frame (frame))
4979 {
4980 const char *fn = nullptr;
4981 symtab_and_line sal;
4982 struct symbol *sym;
4983
4984 if (get_frame_id (frame) == tp->control.step_frame_id)
4985 break;
4986 if (get_frame_type (frame) != INLINE_FRAME)
4987 break;
4988
4989 sal = find_frame_sal (frame);
4990 sym = get_frame_function (frame);
4991
4992 if (sym != nullptr)
4993 fn = sym->print_name ();
4994
4995 if (sal.line != 0
4996 && function_name_is_marked_for_skip (fn, sal))
4997 return true;
4998 }
4999
5000 return false;
5001 }
5002
5003 /* If the event thread has the stop requested flag set, pretend it
5004 stopped for a GDB_SIGNAL_0 (i.e., as if it stopped due to
5005 target_stop). */
5006
5007 static bool
5008 handle_stop_requested (struct execution_control_state *ecs)
5009 {
5010 if (ecs->event_thread->stop_requested)
5011 {
5012 ecs->ws.set_stopped (GDB_SIGNAL_0);
5013 handle_signal_stop (ecs);
5014 return true;
5015 }
5016 return false;
5017 }
5018
5019 /* Auxiliary function that handles syscall entry/return events.
5020 It returns true if the inferior should keep going (and GDB
5021 should ignore the event), or false if the event deserves to be
5022 processed. */
5023
5024 static bool
5025 handle_syscall_event (struct execution_control_state *ecs)
5026 {
5027 struct regcache *regcache;
5028 int syscall_number;
5029
5030 context_switch (ecs);
5031
5032 regcache = get_thread_regcache (ecs->event_thread);
5033 syscall_number = ecs->ws.syscall_number ();
5034 ecs->event_thread->set_stop_pc (regcache_read_pc (regcache));
5035
5036 if (catch_syscall_enabled () > 0
5037 && catching_syscall_number (syscall_number))
5038 {
5039 infrun_debug_printf ("syscall number=%d", syscall_number);
5040
5041 ecs->event_thread->control.stop_bpstat
5042 = bpstat_stop_status_nowatch (regcache->aspace (),
5043 ecs->event_thread->stop_pc (),
5044 ecs->event_thread, ecs->ws);
5045
5046 if (handle_stop_requested (ecs))
5047 return false;
5048
5049 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
5050 {
5051 /* Catchpoint hit. */
5052 return false;
5053 }
5054 }
5055
5056 if (handle_stop_requested (ecs))
5057 return false;
5058
5059 /* If no catchpoint triggered for this, then keep going. */
5060 keep_going (ecs);
5061
5062 return true;
5063 }
5064
5065 /* Lazily fill in the execution_control_state's stop_func_* fields. */
5066
5067 static void
5068 fill_in_stop_func (struct gdbarch *gdbarch,
5069 struct execution_control_state *ecs)
5070 {
5071 if (!ecs->stop_func_filled_in)
5072 {
5073 const block *block;
5074 const general_symbol_info *gsi;
5075
5076 /* Don't care about return value; stop_func_start and stop_func_name
5077 will both be 0 if it doesn't work. */
5078 find_pc_partial_function_sym (ecs->event_thread->stop_pc (),
5079 &gsi,
5080 &ecs->stop_func_start,
5081 &ecs->stop_func_end,
5082 &block);
5083 ecs->stop_func_name = gsi == nullptr ? nullptr : gsi->print_name ();
5084
5085 /* The call to find_pc_partial_function, above, will set
5086 stop_func_start and stop_func_end to the start and end
5087 of the range containing the stop pc. If this range
5088 contains the entry pc for the block (which is always the
5089 case for contiguous blocks), advance stop_func_start past
5090 the function's start offset and entrypoint. Note that
5091 stop_func_start is NOT advanced when in a range of a
5092 non-contiguous block that does not contain the entry pc. */
5093 if (block != nullptr
5094 && ecs->stop_func_start <= block->entry_pc ()
5095 && block->entry_pc () < ecs->stop_func_end)
5096 {
5097 ecs->stop_func_start
5098 += gdbarch_deprecated_function_start_offset (gdbarch);
5099
5100 /* PowerPC functions have a Local Entry Point (LEP) and a Global
5101 Entry Point (GEP). There is only one Entry Point (GEP = LEP) for
5102 other architectures. */
5103 ecs->stop_func_alt_start = ecs->stop_func_start;
5104
5105 if (gdbarch_skip_entrypoint_p (gdbarch))
5106 ecs->stop_func_start
5107 = gdbarch_skip_entrypoint (gdbarch, ecs->stop_func_start);
5108 }
5109
5110 ecs->stop_func_filled_in = 1;
5111 }
5112 }
5113
5114
5115 /* Return the STOP_SOON field of the inferior pointed at by ECS. */
5116
5117 static enum stop_kind
5118 get_inferior_stop_soon (execution_control_state *ecs)
5119 {
5120 struct inferior *inf = find_inferior_ptid (ecs->target, ecs->ptid);
5121
5122 gdb_assert (inf != nullptr);
5123 return inf->control.stop_soon;
5124 }
5125
5126 /* Poll for one event out of the current target. Store the resulting
5127 waitstatus in WS, and return the event ptid. Does not block. */
5128
5129 static ptid_t
5130 poll_one_curr_target (struct target_waitstatus *ws)
5131 {
5132 ptid_t event_ptid;
5133
5134 overlay_cache_invalid = 1;
5135
5136 /* Flush target cache before starting to handle each event.
5137 Target was running and cache could be stale. This is just a
5138 heuristic. Running threads may modify target memory, but we
5139 don't get any event. */
5140 target_dcache_invalidate ();
5141
5142 event_ptid = target_wait (minus_one_ptid, ws, TARGET_WNOHANG);
5143
5144 if (debug_infrun)
5145 print_target_wait_results (minus_one_ptid, event_ptid, *ws);
5146
5147 return event_ptid;
5148 }
5149
5150 /* Wait for one event out of any target. */
5151
5152 static wait_one_event
5153 wait_one ()
5154 {
5155 while (1)
5156 {
5157 for (inferior *inf : all_inferiors ())
5158 {
5159 process_stratum_target *target = inf->process_target ();
5160 if (target == nullptr
5161 || !target->is_async_p ()
5162 || !target->threads_executing)
5163 continue;
5164
5165 switch_to_inferior_no_thread (inf);
5166
5167 wait_one_event event;
5168 event.target = target;
5169 event.ptid = poll_one_curr_target (&event.ws);
5170
5171 if (event.ws.kind () == TARGET_WAITKIND_NO_RESUMED)
5172 {
5173 /* If nothing is resumed, remove the target from the
5174 event loop. */
5175 target_async (false);
5176 }
5177 else if (event.ws.kind () != TARGET_WAITKIND_IGNORE)
5178 return event;
5179 }
5180
5181 /* Block waiting for some event. */
5182
5183 fd_set readfds;
5184 int nfds = 0;
5185
5186 FD_ZERO (&readfds);
5187
5188 for (inferior *inf : all_inferiors ())
5189 {
5190 process_stratum_target *target = inf->process_target ();
5191 if (target == nullptr
5192 || !target->is_async_p ()
5193 || !target->threads_executing)
5194 continue;
5195
5196 int fd = target->async_wait_fd ();
5197 FD_SET (fd, &readfds);
5198 if (nfds <= fd)
5199 nfds = fd + 1;
5200 }
5201
5202 if (nfds == 0)
5203 {
5204 /* No waitable targets left. All must be stopped. */
5205 target_waitstatus ws;
5206 ws.set_no_resumed ();
5207 return {nullptr, minus_one_ptid, std::move (ws)};
5208 }
5209
5210 QUIT;
5211
5212 int numfds = interruptible_select (nfds, &readfds, 0, nullptr, 0);
5213 if (numfds < 0)
5214 {
5215 if (errno == EINTR)
5216 continue;
5217 else
5218 perror_with_name ("interruptible_select");
5219 }
5220 }
5221 }
5222
5223 /* Save the thread's event and stop reason to process it later. */
5224
5225 static void
5226 save_waitstatus (struct thread_info *tp, const target_waitstatus &ws)
5227 {
5228 infrun_debug_printf ("saving status %s for %s",
5229 ws.to_string ().c_str (),
5230 tp->ptid.to_string ().c_str ());
5231
5232 /* Record for later. */
5233 tp->set_pending_waitstatus (ws);
5234
5235 if (ws.kind () == TARGET_WAITKIND_STOPPED
5236 && ws.sig () == GDB_SIGNAL_TRAP)
5237 {
5238 struct regcache *regcache = get_thread_regcache (tp);
5239 const address_space *aspace = regcache->aspace ();
5240 CORE_ADDR pc = regcache_read_pc (regcache);
5241
5242 adjust_pc_after_break (tp, tp->pending_waitstatus ());
5243
5244 scoped_restore_current_thread restore_thread;
5245 switch_to_thread (tp);
5246
5247 if (target_stopped_by_watchpoint ())
5248 tp->set_stop_reason (TARGET_STOPPED_BY_WATCHPOINT);
5249 else if (target_supports_stopped_by_sw_breakpoint ()
5250 && target_stopped_by_sw_breakpoint ())
5251 tp->set_stop_reason (TARGET_STOPPED_BY_SW_BREAKPOINT);
5252 else if (target_supports_stopped_by_hw_breakpoint ()
5253 && target_stopped_by_hw_breakpoint ())
5254 tp->set_stop_reason (TARGET_STOPPED_BY_HW_BREAKPOINT);
5255 else if (!target_supports_stopped_by_hw_breakpoint ()
5256 && hardware_breakpoint_inserted_here_p (aspace, pc))
5257 tp->set_stop_reason (TARGET_STOPPED_BY_HW_BREAKPOINT);
5258 else if (!target_supports_stopped_by_sw_breakpoint ()
5259 && software_breakpoint_inserted_here_p (aspace, pc))
5260 tp->set_stop_reason (TARGET_STOPPED_BY_SW_BREAKPOINT);
5261 else if (!thread_has_single_step_breakpoints_set (tp)
5262 && currently_stepping (tp))
5263 tp->set_stop_reason (TARGET_STOPPED_BY_SINGLE_STEP);
5264 }
5265 }
5266
5267 /* Mark the non-executing threads accordingly. In all-stop, all
5268 threads of all processes are stopped when we get any event
5269 reported. In non-stop mode, only the event thread stops. */
5270
5271 static void
5272 mark_non_executing_threads (process_stratum_target *target,
5273 ptid_t event_ptid,
5274 const target_waitstatus &ws)
5275 {
5276 ptid_t mark_ptid;
5277
5278 if (!target_is_non_stop_p ())
5279 mark_ptid = minus_one_ptid;
5280 else if (ws.kind () == TARGET_WAITKIND_SIGNALLED
5281 || ws.kind () == TARGET_WAITKIND_EXITED)
5282 {
5283 /* If we're handling a process exit in non-stop mode, even
5284 though threads haven't been deleted yet, one would think
5285 that there is nothing to do, as threads of the dead process
5286 will be soon deleted, and threads of any other process were
5287 left running. However, on some targets, threads survive a
5288 process exit event. E.g., for the "checkpoint" command,
5289 when the current checkpoint/fork exits, linux-fork.c
5290 automatically switches to another fork from within
5291 target_mourn_inferior, by associating the same
5292 inferior/thread to another fork. We haven't mourned yet at
5293 this point, but we must mark any threads left in the
5294 process as not-executing so that finish_thread_state marks
5295 them stopped (in the user's perspective) if/when we present
5296 the stop to the user. */
5297 mark_ptid = ptid_t (event_ptid.pid ());
5298 }
5299 else
5300 mark_ptid = event_ptid;
5301
5302 set_executing (target, mark_ptid, false);
5303
5304 /* Likewise the resumed flag. */
5305 set_resumed (target, mark_ptid, false);
5306 }
5307
5308 /* Handle one event after stopping threads. If the eventing thread
5309 reports back any interesting event, we leave it pending. If the
5310 eventing thread was in the middle of a displaced step, we
5311 cancel/finish it, and unless the thread's inferior is being
5312 detached, put the thread back in the step-over chain. Returns true
5313 if there are no resumed threads left in the target (thus there's no
5314 point in waiting further), false otherwise. */
5315
5316 static bool
5317 handle_one (const wait_one_event &event)
5318 {
5319 infrun_debug_printf
5320 ("%s %s", event.ws.to_string ().c_str (),
5321 event.ptid.to_string ().c_str ());
5322
5323 if (event.ws.kind () == TARGET_WAITKIND_NO_RESUMED)
5324 {
5325 /* All resumed threads exited. */
5326 return true;
5327 }
5328 else if (event.ws.kind () == TARGET_WAITKIND_THREAD_EXITED
5329 || event.ws.kind () == TARGET_WAITKIND_EXITED
5330 || event.ws.kind () == TARGET_WAITKIND_SIGNALLED)
5331 {
5332 /* One thread/process exited/signalled. */
5333
5334 thread_info *t = nullptr;
5335
5336 /* The target may have reported just a pid. If so, try
5337 the first non-exited thread. */
5338 if (event.ptid.is_pid ())
5339 {
5340 int pid = event.ptid.pid ();
5341 inferior *inf = find_inferior_pid (event.target, pid);
5342 for (thread_info *tp : inf->non_exited_threads ())
5343 {
5344 t = tp;
5345 break;
5346 }
5347
5348 /* If there is no available thread, the event would
5349 have to be appended to a per-inferior event list,
5350 which does not exist (and if it did, we'd have
5351 to adjust run control command to be able to
5352 resume such an inferior). We assert here instead
5353 of going into an infinite loop. */
5354 gdb_assert (t != nullptr);
5355
5356 infrun_debug_printf
5357 ("using %s", t->ptid.to_string ().c_str ());
5358 }
5359 else
5360 {
5361 t = event.target->find_thread (event.ptid);
5362 /* Check if this is the first time we see this thread.
5363 Don't bother adding if it individually exited. */
5364 if (t == nullptr
5365 && event.ws.kind () != TARGET_WAITKIND_THREAD_EXITED)
5366 t = add_thread (event.target, event.ptid);
5367 }
5368
5369 if (t != nullptr)
5370 {
5371 /* Set the threads as non-executing to avoid
5372 another stop attempt on them. */
5373 switch_to_thread_no_regs (t);
5374 mark_non_executing_threads (event.target, event.ptid,
5375 event.ws);
5376 save_waitstatus (t, event.ws);
5377 t->stop_requested = false;
5378
5379 if (event.ws.kind () == TARGET_WAITKIND_THREAD_EXITED)
5380 {
5381 if (displaced_step_finish (t, event.ws)
5382 != DISPLACED_STEP_FINISH_STATUS_OK)
5383 {
5384 gdb_assert_not_reached ("displaced_step_finish on "
5385 "exited thread failed");
5386 }
5387 }
5388 }
5389 }
5390 else
5391 {
5392 thread_info *t = event.target->find_thread (event.ptid);
5393 if (t == nullptr)
5394 t = add_thread (event.target, event.ptid);
5395
5396 t->stop_requested = 0;
5397 t->set_executing (false);
5398 t->set_resumed (false);
5399 t->control.may_range_step = 0;
5400
5401 /* This may be the first time we see the inferior report
5402 a stop. */
5403 if (t->inf->needs_setup)
5404 {
5405 switch_to_thread_no_regs (t);
5406 setup_inferior (0);
5407 }
5408
5409 if (event.ws.kind () == TARGET_WAITKIND_STOPPED
5410 && event.ws.sig () == GDB_SIGNAL_0)
5411 {
5412 /* We caught the event that we intended to catch, so
5413 there's no event to save as pending. */
5414
5415 if (displaced_step_finish (t, event.ws)
5416 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
5417 {
5418 /* Add it back to the step-over queue. */
5419 infrun_debug_printf
5420 ("displaced-step of %s canceled",
5421 t->ptid.to_string ().c_str ());
5422
5423 t->control.trap_expected = 0;
5424 if (!t->inf->detaching)
5425 global_thread_step_over_chain_enqueue (t);
5426 }
5427 }
5428 else
5429 {
5430 struct regcache *regcache;
5431
5432 infrun_debug_printf
5433 ("target_wait %s, saving status for %s",
5434 event.ws.to_string ().c_str (),
5435 t->ptid.to_string ().c_str ());
5436
5437 /* Record for later. */
5438 save_waitstatus (t, event.ws);
5439
5440 if (displaced_step_finish (t, event.ws)
5441 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
5442 {
5443 /* Add it back to the step-over queue. */
5444 t->control.trap_expected = 0;
5445 if (!t->inf->detaching)
5446 global_thread_step_over_chain_enqueue (t);
5447 }
5448
5449 regcache = get_thread_regcache (t);
5450 t->set_stop_pc (regcache_read_pc (regcache));
5451
5452 infrun_debug_printf ("saved stop_pc=%s for %s "
5453 "(currently_stepping=%d)",
5454 paddress (current_inferior ()->arch (),
5455 t->stop_pc ()),
5456 t->ptid.to_string ().c_str (),
5457 currently_stepping (t));
5458 }
5459 }
5460
5461 return false;
5462 }
5463
5464 /* See infrun.h. */
5465
5466 void
5467 stop_all_threads (const char *reason, inferior *inf)
5468 {
5469 /* We may need multiple passes to discover all threads. */
5470 int pass;
5471 int iterations = 0;
5472
5473 gdb_assert (exists_non_stop_target ());
5474
5475 INFRUN_SCOPED_DEBUG_START_END ("reason=%s, inf=%d", reason,
5476 inf != nullptr ? inf->num : -1);
5477
5478 infrun_debug_show_threads ("non-exited threads",
5479 all_non_exited_threads ());
5480
5481 scoped_restore_current_thread restore_thread;
5482
5483 /* Enable thread events on relevant targets. */
5484 for (auto *target : all_non_exited_process_targets ())
5485 {
5486 if (inf != nullptr && inf->process_target () != target)
5487 continue;
5488
5489 switch_to_target_no_thread (target);
5490 target_thread_events (true);
5491 }
5492
5493 SCOPE_EXIT
5494 {
5495 /* Disable thread events on relevant targets. */
5496 for (auto *target : all_non_exited_process_targets ())
5497 {
5498 if (inf != nullptr && inf->process_target () != target)
5499 continue;
5500
5501 switch_to_target_no_thread (target);
5502 target_thread_events (false);
5503 }
5504
5505 /* Use debug_prefixed_printf directly to get a meaningful function
5506 name. */
5507 if (debug_infrun)
5508 debug_prefixed_printf ("infrun", "stop_all_threads", "done");
5509 };
5510
5511 /* Request threads to stop, and then wait for the stops. Because
5512 threads we already know about can spawn more threads while we're
5513 trying to stop them, and we only learn about new threads when we
5514 update the thread list, do this in a loop, and keep iterating
5515 until two passes find no threads that need to be stopped. */
5516 for (pass = 0; pass < 2; pass++, iterations++)
5517 {
5518 infrun_debug_printf ("pass=%d, iterations=%d", pass, iterations);
5519 while (1)
5520 {
5521 int waits_needed = 0;
5522
5523 for (auto *target : all_non_exited_process_targets ())
5524 {
5525 if (inf != nullptr && inf->process_target () != target)
5526 continue;
5527
5528 switch_to_target_no_thread (target);
5529 update_thread_list ();
5530 }
5531
5532 /* Go through all threads looking for threads that we need
5533 to tell the target to stop. */
5534 for (thread_info *t : all_non_exited_threads ())
5535 {
5536 if (inf != nullptr && t->inf != inf)
5537 continue;
5538
5539 /* For a single-target setting with an all-stop target,
5540 we would not even arrive here. For a multi-target
5541 setting, until GDB is able to handle a mixture of
5542 all-stop and non-stop targets, simply skip all-stop
5543 targets' threads. This should be fine due to the
5544 protection of 'check_multi_target_resumption'. */
5545
5546 switch_to_thread_no_regs (t);
5547 if (!target_is_non_stop_p ())
5548 continue;
5549
5550 if (t->executing ())
5551 {
5552 /* If already stopping, don't request a stop again.
5553 We just haven't seen the notification yet. */
5554 if (!t->stop_requested)
5555 {
5556 infrun_debug_printf (" %s executing, need stop",
5557 t->ptid.to_string ().c_str ());
5558 target_stop (t->ptid);
5559 t->stop_requested = 1;
5560 }
5561 else
5562 {
5563 infrun_debug_printf (" %s executing, already stopping",
5564 t->ptid.to_string ().c_str ());
5565 }
5566
5567 if (t->stop_requested)
5568 waits_needed++;
5569 }
5570 else
5571 {
5572 infrun_debug_printf (" %s not executing",
5573 t->ptid.to_string ().c_str ());
5574
5575 /* The thread may be not executing, but still be
5576 resumed with a pending status to process. */
5577 t->set_resumed (false);
5578 }
5579 }
5580
5581 if (waits_needed == 0)
5582 break;
5583
5584 /* If we find new threads on the second iteration, restart
5585 over. We want to see two iterations in a row with all
5586 threads stopped. */
5587 if (pass > 0)
5588 pass = -1;
5589
5590 for (int i = 0; i < waits_needed; i++)
5591 {
5592 wait_one_event event = wait_one ();
5593 if (handle_one (event))
5594 break;
5595 }
5596 }
5597 }
5598 }
5599
5600 /* Handle a TARGET_WAITKIND_NO_RESUMED event. Return true if we
5601 handled the event and should continue waiting. Return false if we
5602 should stop and report the event to the user. */
5603
5604 static bool
5605 handle_no_resumed (struct execution_control_state *ecs)
5606 {
5607 if (target_can_async_p ())
5608 {
5609 bool any_sync = false;
5610
5611 for (ui *ui : all_uis ())
5612 {
5613 if (ui->prompt_state == PROMPT_BLOCKED)
5614 {
5615 any_sync = true;
5616 break;
5617 }
5618 }
5619 if (!any_sync)
5620 {
5621 /* There were no unwaited-for children left in the target, but,
5622 we're not synchronously waiting for events either. Just
5623 ignore. */
5624
5625 infrun_debug_printf ("TARGET_WAITKIND_NO_RESUMED (ignoring: bg)");
5626 prepare_to_wait (ecs);
5627 return true;
5628 }
5629 }
5630
5631 /* Otherwise, if we were running a synchronous execution command, we
5632 may need to cancel it and give the user back the terminal.
5633
5634 In non-stop mode, the target can't tell whether we've already
5635 consumed previous stop events, so it can end up sending us a
5636 no-resumed event like so:
5637
5638 #0 - thread 1 is left stopped
5639
5640 #1 - thread 2 is resumed and hits breakpoint
5641 -> TARGET_WAITKIND_STOPPED
5642
5643 #2 - thread 3 is resumed and exits
5644 this is the last resumed thread, so
5645 -> TARGET_WAITKIND_NO_RESUMED
5646
5647 #3 - gdb processes stop for thread 2 and decides to re-resume
5648 it.
5649
5650 #4 - gdb processes the TARGET_WAITKIND_NO_RESUMED event.
5651 thread 2 is now resumed, so the event should be ignored.
5652
5653 IOW, if the stop for thread 2 doesn't end a foreground command,
5654 then we need to ignore the following TARGET_WAITKIND_NO_RESUMED
5655 event. But it could be that the event meant that thread 2 itself
5656 (or whatever other thread was the last resumed thread) exited.
5657
5658 To address this we refresh the thread list and check whether we
5659 have resumed threads _now_. In the example above, this removes
5660 thread 3 from the thread list. If thread 2 was re-resumed, we
5661 ignore this event. If we find no thread resumed, then we cancel
5662 the synchronous command and show "no unwaited-for " to the
5663 user. */
5664
5665 inferior *curr_inf = current_inferior ();
5666
5667 scoped_restore_current_thread restore_thread;
5668 update_thread_list ();
5669
5670 /* If:
5671
5672 - the current target has no thread executing, and
5673 - the current inferior is native, and
5674 - the current inferior is the one which has the terminal, and
5675 - we did nothing,
5676
5677 then a Ctrl-C from this point on would remain stuck in the
5678 kernel, until a thread resumes and dequeues it. That would
5679 result in the GDB CLI not reacting to Ctrl-C, not able to
5680 interrupt the program. To address this, if the current inferior
5681 no longer has any thread executing, we give the terminal to some
5682 other inferior that has at least one thread executing. */
5683 bool swap_terminal = true;
5684
5685 /* Whether to ignore this TARGET_WAITKIND_NO_RESUMED event, or
5686 whether to report it to the user. */
5687 bool ignore_event = false;
5688
5689 for (thread_info *thread : all_non_exited_threads ())
5690 {
5691 if (swap_terminal && thread->executing ())
5692 {
5693 if (thread->inf != curr_inf)
5694 {
5695 target_terminal::ours ();
5696
5697 switch_to_thread (thread);
5698 target_terminal::inferior ();
5699 }
5700 swap_terminal = false;
5701 }
5702
5703 if (!ignore_event && thread->resumed ())
5704 {
5705 /* Either there were no unwaited-for children left in the
5706 target at some point, but there are now, or some target
5707 other than the eventing one has unwaited-for children
5708 left. Just ignore. */
5709 infrun_debug_printf ("TARGET_WAITKIND_NO_RESUMED "
5710 "(ignoring: found resumed)");
5711
5712 ignore_event = true;
5713 }
5714
5715 if (ignore_event && !swap_terminal)
5716 break;
5717 }
5718
5719 if (ignore_event)
5720 {
5721 switch_to_inferior_no_thread (curr_inf);
5722 prepare_to_wait (ecs);
5723 return true;
5724 }
5725
5726 /* Go ahead and report the event. */
5727 return false;
5728 }
5729
5730 /* Handle a TARGET_WAITKIND_THREAD_EXITED event. Return true if we
5731 handled the event and should continue waiting. Return false if we
5732 should stop and report the event to the user. */
5733
5734 static bool
5735 handle_thread_exited (execution_control_state *ecs)
5736 {
5737 context_switch (ecs);
5738
5739 /* Clear these so we don't re-start the thread stepping over a
5740 breakpoint/watchpoint. */
5741 ecs->event_thread->stepping_over_breakpoint = 0;
5742 ecs->event_thread->stepping_over_watchpoint = 0;
5743
5744 /* Maybe the thread was doing a step-over, if so release
5745 resources and start any further pending step-overs.
5746
5747 If we are on a non-stop target and the thread was doing an
5748 in-line step, this also restarts the other threads. */
5749 int ret = finish_step_over (ecs);
5750
5751 /* finish_step_over returns true if it moves ecs' wait status
5752 back into the thread, so that we go handle another pending
5753 event before this one. But we know it never does that if
5754 the event thread has exited. */
5755 gdb_assert (ret == 0);
5756
5757 /* If finish_step_over started a new in-line step-over, don't
5758 try to restart anything else. */
5759 if (step_over_info_valid_p ())
5760 {
5761 delete_thread (ecs->event_thread);
5762 return true;
5763 }
5764
5765 /* Maybe we are on an all-stop target and we got this event
5766 while doing a step-like command on another thread. If so,
5767 go back to doing that. If this thread was stepping,
5768 switch_back_to_stepped_thread will consider that the thread
5769 was interrupted mid-step and will try keep stepping it. We
5770 don't want that, the thread is gone. So clear the proceed
5771 status so it doesn't do that. */
5772 clear_proceed_status_thread (ecs->event_thread);
5773 if (switch_back_to_stepped_thread (ecs))
5774 {
5775 delete_thread (ecs->event_thread);
5776 return true;
5777 }
5778
5779 inferior *inf = ecs->event_thread->inf;
5780 bool slock_applies = schedlock_applies (ecs->event_thread);
5781
5782 delete_thread (ecs->event_thread);
5783 ecs->event_thread = nullptr;
5784
5785 /* Continue handling the event as if we had gotten a
5786 TARGET_WAITKIND_NO_RESUMED. */
5787 auto handle_as_no_resumed = [ecs] ()
5788 {
5789 /* handle_no_resumed doesn't really look at the event kind, but
5790 normal_stop does. */
5791 ecs->ws.set_no_resumed ();
5792 ecs->event_thread = nullptr;
5793 ecs->ptid = minus_one_ptid;
5794
5795 /* Re-record the last target status. */
5796 set_last_target_status (ecs->target, ecs->ptid, ecs->ws);
5797
5798 return handle_no_resumed (ecs);
5799 };
5800
5801 /* If we are on an all-stop target, the target has stopped all
5802 threads to report the event. We don't actually want to
5803 stop, so restart the threads. */
5804 if (!target_is_non_stop_p ())
5805 {
5806 if (slock_applies)
5807 {
5808 /* Since the target is !non-stop, then everything is stopped
5809 at this point, and we can't assume we'll get further
5810 events until we resume the target again. Handle this
5811 event like if it were a TARGET_WAITKIND_NO_RESUMED. Note
5812 this refreshes the thread list and checks whether there
5813 are other resumed threads before deciding whether to
5814 print "no-unwaited-for left". This is important because
5815 the user could have done:
5816
5817 (gdb) set scheduler-locking on
5818 (gdb) thread 1
5819 (gdb) c&
5820 (gdb) thread 2
5821 (gdb) c
5822
5823 ... and only one of the threads exited. */
5824 return handle_as_no_resumed ();
5825 }
5826 else
5827 {
5828 /* Switch to the first non-exited thread we can find, and
5829 resume. */
5830 auto range = inf->non_exited_threads ();
5831 if (range.begin () == range.end ())
5832 {
5833 /* Looks like the target reported a
5834 TARGET_WAITKIND_THREAD_EXITED for its last known
5835 thread. */
5836 return handle_as_no_resumed ();
5837 }
5838 thread_info *non_exited_thread = *range.begin ();
5839 switch_to_thread (non_exited_thread);
5840 insert_breakpoints ();
5841 resume (GDB_SIGNAL_0);
5842 }
5843 }
5844
5845 prepare_to_wait (ecs);
5846 return true;
5847 }
5848
5849 /* Given an execution control state that has been freshly filled in by
5850 an event from the inferior, figure out what it means and take
5851 appropriate action.
5852
5853 The alternatives are:
5854
5855 1) stop_waiting and return; to really stop and return to the
5856 debugger.
5857
5858 2) keep_going and return; to wait for the next event (set
5859 ecs->event_thread->stepping_over_breakpoint to 1 to single step
5860 once). */
5861
5862 static void
5863 handle_inferior_event (struct execution_control_state *ecs)
5864 {
5865 /* Make sure that all temporary struct value objects that were
5866 created during the handling of the event get deleted at the
5867 end. */
5868 scoped_value_mark free_values;
5869
5870 infrun_debug_printf ("%s", ecs->ws.to_string ().c_str ());
5871
5872 if (ecs->ws.kind () == TARGET_WAITKIND_IGNORE)
5873 {
5874 /* We had an event in the inferior, but we are not interested in
5875 handling it at this level. The lower layers have already
5876 done what needs to be done, if anything.
5877
5878 One of the possible circumstances for this is when the
5879 inferior produces output for the console. The inferior has
5880 not stopped, and we are ignoring the event. Another possible
5881 circumstance is any event which the lower level knows will be
5882 reported multiple times without an intervening resume. */
5883 prepare_to_wait (ecs);
5884 return;
5885 }
5886
5887 if (ecs->ws.kind () == TARGET_WAITKIND_NO_RESUMED
5888 && handle_no_resumed (ecs))
5889 return;
5890
5891 /* Cache the last target/ptid/waitstatus. */
5892 set_last_target_status (ecs->target, ecs->ptid, ecs->ws);
5893
5894 /* Always clear state belonging to the previous time we stopped. */
5895 stop_stack_dummy = STOP_NONE;
5896
5897 if (ecs->ws.kind () == TARGET_WAITKIND_NO_RESUMED)
5898 {
5899 /* No unwaited-for children left. IOW, all resumed children
5900 have exited. */
5901 stop_waiting (ecs);
5902 return;
5903 }
5904
5905 if (ecs->ws.kind () != TARGET_WAITKIND_EXITED
5906 && ecs->ws.kind () != TARGET_WAITKIND_SIGNALLED)
5907 {
5908 ecs->event_thread = ecs->target->find_thread (ecs->ptid);
5909 /* If it's a new thread, add it to the thread database. */
5910 if (ecs->event_thread == nullptr)
5911 ecs->event_thread = add_thread (ecs->target, ecs->ptid);
5912
5913 /* Disable range stepping. If the next step request could use a
5914 range, this will be end up re-enabled then. */
5915 ecs->event_thread->control.may_range_step = 0;
5916 }
5917
5918 /* Dependent on valid ECS->EVENT_THREAD. */
5919 adjust_pc_after_break (ecs->event_thread, ecs->ws);
5920
5921 /* Dependent on the current PC value modified by adjust_pc_after_break. */
5922 reinit_frame_cache ();
5923
5924 breakpoint_retire_moribund ();
5925
5926 /* First, distinguish signals caused by the debugger from signals
5927 that have to do with the program's own actions. Note that
5928 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
5929 on the operating system version. Here we detect when a SIGILL or
5930 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
5931 something similar for SIGSEGV, since a SIGSEGV will be generated
5932 when we're trying to execute a breakpoint instruction on a
5933 non-executable stack. This happens for call dummy breakpoints
5934 for architectures like SPARC that place call dummies on the
5935 stack. */
5936 if (ecs->ws.kind () == TARGET_WAITKIND_STOPPED
5937 && (ecs->ws.sig () == GDB_SIGNAL_ILL
5938 || ecs->ws.sig () == GDB_SIGNAL_SEGV
5939 || ecs->ws.sig () == GDB_SIGNAL_EMT))
5940 {
5941 struct regcache *regcache = get_thread_regcache (ecs->event_thread);
5942
5943 if (breakpoint_inserted_here_p (regcache->aspace (),
5944 regcache_read_pc (regcache)))
5945 {
5946 infrun_debug_printf ("Treating signal as SIGTRAP");
5947 ecs->ws.set_stopped (GDB_SIGNAL_TRAP);
5948 }
5949 }
5950
5951 mark_non_executing_threads (ecs->target, ecs->ptid, ecs->ws);
5952
5953 switch (ecs->ws.kind ())
5954 {
5955 case TARGET_WAITKIND_LOADED:
5956 {
5957 context_switch (ecs);
5958 /* Ignore gracefully during startup of the inferior, as it might
5959 be the shell which has just loaded some objects, otherwise
5960 add the symbols for the newly loaded objects. Also ignore at
5961 the beginning of an attach or remote session; we will query
5962 the full list of libraries once the connection is
5963 established. */
5964
5965 stop_kind stop_soon = get_inferior_stop_soon (ecs);
5966 if (stop_soon == NO_STOP_QUIETLY)
5967 {
5968 struct regcache *regcache;
5969
5970 regcache = get_thread_regcache (ecs->event_thread);
5971
5972 handle_solib_event ();
5973
5974 ecs->event_thread->set_stop_pc (regcache_read_pc (regcache));
5975 ecs->event_thread->control.stop_bpstat
5976 = bpstat_stop_status_nowatch (regcache->aspace (),
5977 ecs->event_thread->stop_pc (),
5978 ecs->event_thread, ecs->ws);
5979
5980 if (handle_stop_requested (ecs))
5981 return;
5982
5983 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
5984 {
5985 /* A catchpoint triggered. */
5986 process_event_stop_test (ecs);
5987 return;
5988 }
5989
5990 /* If requested, stop when the dynamic linker notifies
5991 gdb of events. This allows the user to get control
5992 and place breakpoints in initializer routines for
5993 dynamically loaded objects (among other things). */
5994 ecs->event_thread->set_stop_signal (GDB_SIGNAL_0);
5995 if (stop_on_solib_events)
5996 {
5997 /* Make sure we print "Stopped due to solib-event" in
5998 normal_stop. */
5999 stop_print_frame = true;
6000
6001 stop_waiting (ecs);
6002 return;
6003 }
6004 }
6005
6006 /* If we are skipping through a shell, or through shared library
6007 loading that we aren't interested in, resume the program. If
6008 we're running the program normally, also resume. */
6009 if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
6010 {
6011 /* Loading of shared libraries might have changed breakpoint
6012 addresses. Make sure new breakpoints are inserted. */
6013 if (stop_soon == NO_STOP_QUIETLY)
6014 insert_breakpoints ();
6015 resume (GDB_SIGNAL_0);
6016 prepare_to_wait (ecs);
6017 return;
6018 }
6019
6020 /* But stop if we're attaching or setting up a remote
6021 connection. */
6022 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
6023 || stop_soon == STOP_QUIETLY_REMOTE)
6024 {
6025 infrun_debug_printf ("quietly stopped");
6026 stop_waiting (ecs);
6027 return;
6028 }
6029
6030 internal_error (_("unhandled stop_soon: %d"), (int) stop_soon);
6031 }
6032
6033 case TARGET_WAITKIND_SPURIOUS:
6034 if (handle_stop_requested (ecs))
6035 return;
6036 context_switch (ecs);
6037 resume (GDB_SIGNAL_0);
6038 prepare_to_wait (ecs);
6039 return;
6040
6041 case TARGET_WAITKIND_THREAD_CREATED:
6042 if (handle_stop_requested (ecs))
6043 return;
6044 context_switch (ecs);
6045 if (!switch_back_to_stepped_thread (ecs))
6046 keep_going (ecs);
6047 return;
6048
6049 case TARGET_WAITKIND_THREAD_EXITED:
6050 if (handle_thread_exited (ecs))
6051 return;
6052 stop_waiting (ecs);
6053 break;
6054
6055 case TARGET_WAITKIND_EXITED:
6056 case TARGET_WAITKIND_SIGNALLED:
6057 {
6058 /* Depending on the system, ecs->ptid may point to a thread or
6059 to a process. On some targets, target_mourn_inferior may
6060 need to have access to the just-exited thread. That is the
6061 case of GNU/Linux's "checkpoint" support, for example.
6062 Call the switch_to_xxx routine as appropriate. */
6063 thread_info *thr = ecs->target->find_thread (ecs->ptid);
6064 if (thr != nullptr)
6065 switch_to_thread (thr);
6066 else
6067 {
6068 inferior *inf = find_inferior_ptid (ecs->target, ecs->ptid);
6069 switch_to_inferior_no_thread (inf);
6070 }
6071 }
6072 handle_vfork_child_exec_or_exit (0);
6073 target_terminal::ours (); /* Must do this before mourn anyway. */
6074
6075 /* Clearing any previous state of convenience variables. */
6076 clear_exit_convenience_vars ();
6077
6078 if (ecs->ws.kind () == TARGET_WAITKIND_EXITED)
6079 {
6080 /* Record the exit code in the convenience variable $_exitcode, so
6081 that the user can inspect this again later. */
6082 set_internalvar_integer (lookup_internalvar ("_exitcode"),
6083 (LONGEST) ecs->ws.exit_status ());
6084
6085 /* Also record this in the inferior itself. */
6086 current_inferior ()->has_exit_code = true;
6087 current_inferior ()->exit_code = (LONGEST) ecs->ws.exit_status ();
6088
6089 /* Support the --return-child-result option. */
6090 return_child_result_value = ecs->ws.exit_status ();
6091
6092 interps_notify_exited (ecs->ws.exit_status ());
6093 }
6094 else
6095 {
6096 struct gdbarch *gdbarch = current_inferior ()->arch ();
6097
6098 if (gdbarch_gdb_signal_to_target_p (gdbarch))
6099 {
6100 /* Set the value of the internal variable $_exitsignal,
6101 which holds the signal uncaught by the inferior. */
6102 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
6103 gdbarch_gdb_signal_to_target (gdbarch,
6104 ecs->ws.sig ()));
6105 }
6106 else
6107 {
6108 /* We don't have access to the target's method used for
6109 converting between signal numbers (GDB's internal
6110 representation <-> target's representation).
6111 Therefore, we cannot do a good job at displaying this
6112 information to the user. It's better to just warn
6113 her about it (if infrun debugging is enabled), and
6114 give up. */
6115 infrun_debug_printf ("Cannot fill $_exitsignal with the correct "
6116 "signal number.");
6117 }
6118
6119 interps_notify_signal_exited (ecs->ws.sig ());
6120 }
6121
6122 gdb_flush (gdb_stdout);
6123 target_mourn_inferior (inferior_ptid);
6124 stop_print_frame = false;
6125 stop_waiting (ecs);
6126 return;
6127
6128 case TARGET_WAITKIND_FORKED:
6129 case TARGET_WAITKIND_VFORKED:
6130 case TARGET_WAITKIND_THREAD_CLONED:
6131
6132 displaced_step_finish (ecs->event_thread, ecs->ws);
6133
6134 /* Start a new step-over in another thread if there's one that
6135 needs it. */
6136 start_step_over ();
6137
6138 context_switch (ecs);
6139
6140 /* Immediately detach breakpoints from the child before there's
6141 any chance of letting the user delete breakpoints from the
6142 breakpoint lists. If we don't do this early, it's easy to
6143 leave left over traps in the child, vis: "break foo; catch
6144 fork; c; <fork>; del; c; <child calls foo>". We only follow
6145 the fork on the last `continue', and by that time the
6146 breakpoint at "foo" is long gone from the breakpoint table.
6147 If we vforked, then we don't need to unpatch here, since both
6148 parent and child are sharing the same memory pages; we'll
6149 need to unpatch at follow/detach time instead to be certain
6150 that new breakpoints added between catchpoint hit time and
6151 vfork follow are detached. */
6152 if (ecs->ws.kind () == TARGET_WAITKIND_FORKED)
6153 {
6154 /* This won't actually modify the breakpoint list, but will
6155 physically remove the breakpoints from the child. */
6156 detach_breakpoints (ecs->ws.child_ptid ());
6157 }
6158
6159 delete_just_stopped_threads_single_step_breakpoints ();
6160
6161 /* In case the event is caught by a catchpoint, remember that
6162 the event is to be followed at the next resume of the thread,
6163 and not immediately. */
6164 ecs->event_thread->pending_follow = ecs->ws;
6165
6166 ecs->event_thread->set_stop_pc
6167 (regcache_read_pc (get_thread_regcache (ecs->event_thread)));
6168
6169 ecs->event_thread->control.stop_bpstat
6170 = bpstat_stop_status_nowatch (get_current_regcache ()->aspace (),
6171 ecs->event_thread->stop_pc (),
6172 ecs->event_thread, ecs->ws);
6173
6174 if (handle_stop_requested (ecs))
6175 return;
6176
6177 /* If no catchpoint triggered for this, then keep going. Note
6178 that we're interested in knowing the bpstat actually causes a
6179 stop, not just if it may explain the signal. Software
6180 watchpoints, for example, always appear in the bpstat. */
6181 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
6182 {
6183 bool follow_child
6184 = (ecs->ws.kind () != TARGET_WAITKIND_THREAD_CLONED
6185 && follow_fork_mode_string == follow_fork_mode_child);
6186
6187 ecs->event_thread->set_stop_signal (GDB_SIGNAL_0);
6188
6189 process_stratum_target *targ
6190 = ecs->event_thread->inf->process_target ();
6191
6192 bool should_resume;
6193 if (ecs->ws.kind () != TARGET_WAITKIND_THREAD_CLONED)
6194 should_resume = follow_fork ();
6195 else
6196 {
6197 should_resume = true;
6198 inferior *inf = ecs->event_thread->inf;
6199 inf->top_target ()->follow_clone (ecs->ws.child_ptid ());
6200 ecs->event_thread->pending_follow.set_spurious ();
6201 }
6202
6203 /* Note that one of these may be an invalid pointer,
6204 depending on detach_fork. */
6205 thread_info *parent = ecs->event_thread;
6206 thread_info *child = targ->find_thread (ecs->ws.child_ptid ());
6207
6208 /* At this point, the parent is marked running, and the
6209 child is marked stopped. */
6210
6211 /* If not resuming the parent, mark it stopped. */
6212 if (ecs->ws.kind () != TARGET_WAITKIND_THREAD_CLONED
6213 && follow_child && !detach_fork && !non_stop && !sched_multi)
6214 parent->set_running (false);
6215
6216 /* If resuming the child, mark it running. */
6217 if (ecs->ws.kind () == TARGET_WAITKIND_THREAD_CLONED
6218 || (follow_child || (!detach_fork && (non_stop || sched_multi))))
6219 child->set_running (true);
6220
6221 /* In non-stop mode, also resume the other branch. */
6222 if ((ecs->ws.kind () == TARGET_WAITKIND_THREAD_CLONED
6223 && target_is_non_stop_p ())
6224 || (!detach_fork && (non_stop
6225 || (sched_multi
6226 && target_is_non_stop_p ()))))
6227 {
6228 if (follow_child)
6229 switch_to_thread (parent);
6230 else
6231 switch_to_thread (child);
6232
6233 ecs->event_thread = inferior_thread ();
6234 ecs->ptid = inferior_ptid;
6235 keep_going (ecs);
6236 }
6237
6238 if (follow_child)
6239 switch_to_thread (child);
6240 else
6241 switch_to_thread (parent);
6242
6243 ecs->event_thread = inferior_thread ();
6244 ecs->ptid = inferior_ptid;
6245
6246 if (should_resume)
6247 {
6248 /* Never call switch_back_to_stepped_thread if we are waiting for
6249 vfork-done (waiting for an external vfork child to exec or
6250 exit). We will resume only the vforking thread for the purpose
6251 of collecting the vfork-done event, and we will restart any
6252 step once the critical shared address space window is done. */
6253 if ((!follow_child
6254 && detach_fork
6255 && parent->inf->thread_waiting_for_vfork_done != nullptr)
6256 || !switch_back_to_stepped_thread (ecs))
6257 keep_going (ecs);
6258 }
6259 else
6260 stop_waiting (ecs);
6261 return;
6262 }
6263 process_event_stop_test (ecs);
6264 return;
6265
6266 case TARGET_WAITKIND_VFORK_DONE:
6267 /* Done with the shared memory region. Re-insert breakpoints in
6268 the parent, and keep going. */
6269
6270 context_switch (ecs);
6271
6272 handle_vfork_done (ecs->event_thread);
6273 gdb_assert (inferior_thread () == ecs->event_thread);
6274
6275 if (handle_stop_requested (ecs))
6276 return;
6277
6278 if (!switch_back_to_stepped_thread (ecs))
6279 {
6280 gdb_assert (inferior_thread () == ecs->event_thread);
6281 /* This also takes care of reinserting breakpoints in the
6282 previously locked inferior. */
6283 keep_going (ecs);
6284 }
6285 return;
6286
6287 case TARGET_WAITKIND_EXECD:
6288
6289 /* Note we can't read registers yet (the stop_pc), because we
6290 don't yet know the inferior's post-exec architecture.
6291 'stop_pc' is explicitly read below instead. */
6292 switch_to_thread_no_regs (ecs->event_thread);
6293
6294 /* Do whatever is necessary to the parent branch of the vfork. */
6295 handle_vfork_child_exec_or_exit (1);
6296
6297 /* This causes the eventpoints and symbol table to be reset.
6298 Must do this now, before trying to determine whether to
6299 stop. */
6300 follow_exec (inferior_ptid, ecs->ws.execd_pathname ());
6301
6302 /* In follow_exec we may have deleted the original thread and
6303 created a new one. Make sure that the event thread is the
6304 execd thread for that case (this is a nop otherwise). */
6305 ecs->event_thread = inferior_thread ();
6306
6307 ecs->event_thread->set_stop_pc
6308 (regcache_read_pc (get_thread_regcache (ecs->event_thread)));
6309
6310 ecs->event_thread->control.stop_bpstat
6311 = bpstat_stop_status_nowatch (get_current_regcache ()->aspace (),
6312 ecs->event_thread->stop_pc (),
6313 ecs->event_thread, ecs->ws);
6314
6315 if (handle_stop_requested (ecs))
6316 return;
6317
6318 /* If no catchpoint triggered for this, then keep going. */
6319 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
6320 {
6321 ecs->event_thread->set_stop_signal (GDB_SIGNAL_0);
6322 keep_going (ecs);
6323 return;
6324 }
6325 process_event_stop_test (ecs);
6326 return;
6327
6328 /* Be careful not to try to gather much state about a thread
6329 that's in a syscall. It's frequently a losing proposition. */
6330 case TARGET_WAITKIND_SYSCALL_ENTRY:
6331 /* Getting the current syscall number. */
6332 if (handle_syscall_event (ecs) == 0)
6333 process_event_stop_test (ecs);
6334 return;
6335
6336 /* Before examining the threads further, step this thread to
6337 get it entirely out of the syscall. (We get notice of the
6338 event when the thread is just on the verge of exiting a
6339 syscall. Stepping one instruction seems to get it back
6340 into user code.) */
6341 case TARGET_WAITKIND_SYSCALL_RETURN:
6342 if (handle_syscall_event (ecs) == 0)
6343 process_event_stop_test (ecs);
6344 return;
6345
6346 case TARGET_WAITKIND_STOPPED:
6347 handle_signal_stop (ecs);
6348 return;
6349
6350 case TARGET_WAITKIND_NO_HISTORY:
6351 /* Reverse execution: target ran out of history info. */
6352
6353 /* Switch to the stopped thread. */
6354 context_switch (ecs);
6355 infrun_debug_printf ("stopped");
6356
6357 delete_just_stopped_threads_single_step_breakpoints ();
6358 ecs->event_thread->set_stop_pc
6359 (regcache_read_pc (get_thread_regcache (inferior_thread ())));
6360
6361 if (handle_stop_requested (ecs))
6362 return;
6363
6364 interps_notify_no_history ();
6365 stop_waiting (ecs);
6366 return;
6367 }
6368 }
6369
6370 /* Restart threads back to what they were trying to do back when we
6371 paused them (because of an in-line step-over or vfork, for example).
6372 The EVENT_THREAD thread is ignored (not restarted).
6373
6374 If INF is non-nullptr, only resume threads from INF. */
6375
6376 static void
6377 restart_threads (struct thread_info *event_thread, inferior *inf)
6378 {
6379 INFRUN_SCOPED_DEBUG_START_END ("event_thread=%s, inf=%d",
6380 event_thread->ptid.to_string ().c_str (),
6381 inf != nullptr ? inf->num : -1);
6382
6383 gdb_assert (!step_over_info_valid_p ());
6384
6385 /* In case the instruction just stepped spawned a new thread. */
6386 update_thread_list ();
6387
6388 for (thread_info *tp : all_non_exited_threads ())
6389 {
6390 if (inf != nullptr && tp->inf != inf)
6391 continue;
6392
6393 if (tp->inf->detaching)
6394 {
6395 infrun_debug_printf ("restart threads: [%s] inferior detaching",
6396 tp->ptid.to_string ().c_str ());
6397 continue;
6398 }
6399
6400 switch_to_thread_no_regs (tp);
6401
6402 if (tp == event_thread)
6403 {
6404 infrun_debug_printf ("restart threads: [%s] is event thread",
6405 tp->ptid.to_string ().c_str ());
6406 continue;
6407 }
6408
6409 if (!(tp->state == THREAD_RUNNING || tp->control.in_infcall))
6410 {
6411 infrun_debug_printf ("restart threads: [%s] not meant to be running",
6412 tp->ptid.to_string ().c_str ());
6413 continue;
6414 }
6415
6416 if (tp->resumed ())
6417 {
6418 infrun_debug_printf ("restart threads: [%s] resumed",
6419 tp->ptid.to_string ().c_str ());
6420 gdb_assert (tp->executing () || tp->has_pending_waitstatus ());
6421 continue;
6422 }
6423
6424 if (thread_is_in_step_over_chain (tp))
6425 {
6426 infrun_debug_printf ("restart threads: [%s] needs step-over",
6427 tp->ptid.to_string ().c_str ());
6428 gdb_assert (!tp->resumed ());
6429 continue;
6430 }
6431
6432
6433 if (tp->has_pending_waitstatus ())
6434 {
6435 infrun_debug_printf ("restart threads: [%s] has pending status",
6436 tp->ptid.to_string ().c_str ());
6437 tp->set_resumed (true);
6438 continue;
6439 }
6440
6441 gdb_assert (!tp->stop_requested);
6442
6443 /* If some thread needs to start a step-over at this point, it
6444 should still be in the step-over queue, and thus skipped
6445 above. */
6446 if (thread_still_needs_step_over (tp))
6447 {
6448 internal_error ("thread [%s] needs a step-over, but not in "
6449 "step-over queue\n",
6450 tp->ptid.to_string ().c_str ());
6451 }
6452
6453 if (currently_stepping (tp))
6454 {
6455 infrun_debug_printf ("restart threads: [%s] was stepping",
6456 tp->ptid.to_string ().c_str ());
6457 keep_going_stepped_thread (tp);
6458 }
6459 else
6460 {
6461 infrun_debug_printf ("restart threads: [%s] continuing",
6462 tp->ptid.to_string ().c_str ());
6463 execution_control_state ecs (tp);
6464 switch_to_thread (tp);
6465 keep_going_pass_signal (&ecs);
6466 }
6467 }
6468 }
6469
6470 /* Callback for iterate_over_threads. Find a resumed thread that has
6471 a pending waitstatus. */
6472
6473 static int
6474 resumed_thread_with_pending_status (struct thread_info *tp,
6475 void *arg)
6476 {
6477 return tp->resumed () && tp->has_pending_waitstatus ();
6478 }
6479
6480 /* Called when we get an event that may finish an in-line or
6481 out-of-line (displaced stepping) step-over started previously.
6482 Return true if the event is processed and we should go back to the
6483 event loop; false if the caller should continue processing the
6484 event. */
6485
6486 static int
6487 finish_step_over (struct execution_control_state *ecs)
6488 {
6489 displaced_step_finish (ecs->event_thread, ecs->ws);
6490
6491 bool had_step_over_info = step_over_info_valid_p ();
6492
6493 if (had_step_over_info)
6494 {
6495 /* If we're stepping over a breakpoint with all threads locked,
6496 then only the thread that was stepped should be reporting
6497 back an event. */
6498 gdb_assert (ecs->event_thread->control.trap_expected);
6499
6500 update_thread_events_after_step_over (ecs->event_thread, ecs->ws);
6501
6502 clear_step_over_info ();
6503 }
6504
6505 if (!target_is_non_stop_p ())
6506 return 0;
6507
6508 /* Start a new step-over in another thread if there's one that
6509 needs it. */
6510 start_step_over ();
6511
6512 /* If we were stepping over a breakpoint before, and haven't started
6513 a new in-line step-over sequence, then restart all other threads
6514 (except the event thread). We can't do this in all-stop, as then
6515 e.g., we wouldn't be able to issue any other remote packet until
6516 these other threads stop. */
6517 if (had_step_over_info && !step_over_info_valid_p ())
6518 {
6519 struct thread_info *pending;
6520
6521 /* If we only have threads with pending statuses, the restart
6522 below won't restart any thread and so nothing re-inserts the
6523 breakpoint we just stepped over. But we need it inserted
6524 when we later process the pending events, otherwise if
6525 another thread has a pending event for this breakpoint too,
6526 we'd discard its event (because the breakpoint that
6527 originally caused the event was no longer inserted). */
6528 context_switch (ecs);
6529 insert_breakpoints ();
6530
6531 restart_threads (ecs->event_thread);
6532
6533 /* If we have events pending, go through handle_inferior_event
6534 again, picking up a pending event at random. This avoids
6535 thread starvation. */
6536
6537 /* But not if we just stepped over a watchpoint in order to let
6538 the instruction execute so we can evaluate its expression.
6539 The set of watchpoints that triggered is recorded in the
6540 breakpoint objects themselves (see bp->watchpoint_triggered).
6541 If we processed another event first, that other event could
6542 clobber this info. */
6543 if (ecs->event_thread->stepping_over_watchpoint)
6544 return 0;
6545
6546 /* The code below is meant to avoid one thread hogging the event
6547 loop by doing constant in-line step overs. If the stepping
6548 thread exited, there's no risk for this to happen, so we can
6549 safely let our caller process the event immediately. */
6550 if (ecs->ws.kind () == TARGET_WAITKIND_THREAD_EXITED)
6551 return 0;
6552
6553 pending = iterate_over_threads (resumed_thread_with_pending_status,
6554 nullptr);
6555 if (pending != nullptr)
6556 {
6557 struct thread_info *tp = ecs->event_thread;
6558 struct regcache *regcache;
6559
6560 infrun_debug_printf ("found resumed threads with "
6561 "pending events, saving status");
6562
6563 gdb_assert (pending != tp);
6564
6565 /* Record the event thread's event for later. */
6566 save_waitstatus (tp, ecs->ws);
6567 /* This was cleared early, by handle_inferior_event. Set it
6568 so this pending event is considered by
6569 do_target_wait. */
6570 tp->set_resumed (true);
6571
6572 gdb_assert (!tp->executing ());
6573
6574 regcache = get_thread_regcache (tp);
6575 tp->set_stop_pc (regcache_read_pc (regcache));
6576
6577 infrun_debug_printf ("saved stop_pc=%s for %s "
6578 "(currently_stepping=%d)",
6579 paddress (current_inferior ()->arch (),
6580 tp->stop_pc ()),
6581 tp->ptid.to_string ().c_str (),
6582 currently_stepping (tp));
6583
6584 /* This in-line step-over finished; clear this so we won't
6585 start a new one. This is what handle_signal_stop would
6586 do, if we returned false. */
6587 tp->stepping_over_breakpoint = 0;
6588
6589 /* Wake up the event loop again. */
6590 mark_async_event_handler (infrun_async_inferior_event_token);
6591
6592 prepare_to_wait (ecs);
6593 return 1;
6594 }
6595 }
6596
6597 return 0;
6598 }
6599
6600 /* See infrun.h. */
6601
6602 void
6603 notify_signal_received (gdb_signal sig)
6604 {
6605 interps_notify_signal_received (sig);
6606 gdb::observers::signal_received.notify (sig);
6607 }
6608
6609 /* See infrun.h. */
6610
6611 void
6612 notify_normal_stop (bpstat *bs, int print_frame)
6613 {
6614 interps_notify_normal_stop (bs, print_frame);
6615 gdb::observers::normal_stop.notify (bs, print_frame);
6616 }
6617
6618 /* See infrun.h. */
6619
6620 void notify_user_selected_context_changed (user_selected_what selection)
6621 {
6622 interps_notify_user_selected_context_changed (selection);
6623 gdb::observers::user_selected_context_changed.notify (selection);
6624 }
6625
6626 /* Come here when the program has stopped with a signal. */
6627
6628 static void
6629 handle_signal_stop (struct execution_control_state *ecs)
6630 {
6631 frame_info_ptr frame;
6632 struct gdbarch *gdbarch;
6633 int stopped_by_watchpoint;
6634 enum stop_kind stop_soon;
6635 int random_signal;
6636
6637 gdb_assert (ecs->ws.kind () == TARGET_WAITKIND_STOPPED);
6638
6639 ecs->event_thread->set_stop_signal (ecs->ws.sig ());
6640
6641 /* Do we need to clean up the state of a thread that has
6642 completed a displaced single-step? (Doing so usually affects
6643 the PC, so do it here, before we set stop_pc.) */
6644 if (finish_step_over (ecs))
6645 return;
6646
6647 /* If we either finished a single-step or hit a breakpoint, but
6648 the user wanted this thread to be stopped, pretend we got a
6649 SIG0 (generic unsignaled stop). */
6650 if (ecs->event_thread->stop_requested
6651 && ecs->event_thread->stop_signal () == GDB_SIGNAL_TRAP)
6652 ecs->event_thread->set_stop_signal (GDB_SIGNAL_0);
6653
6654 ecs->event_thread->set_stop_pc
6655 (regcache_read_pc (get_thread_regcache (ecs->event_thread)));
6656
6657 context_switch (ecs);
6658
6659 if (deprecated_context_hook)
6660 deprecated_context_hook (ecs->event_thread->global_num);
6661
6662 if (debug_infrun)
6663 {
6664 struct regcache *regcache = get_thread_regcache (ecs->event_thread);
6665 struct gdbarch *reg_gdbarch = regcache->arch ();
6666
6667 infrun_debug_printf
6668 ("stop_pc=%s", paddress (reg_gdbarch, ecs->event_thread->stop_pc ()));
6669 if (target_stopped_by_watchpoint ())
6670 {
6671 CORE_ADDR addr;
6672
6673 infrun_debug_printf ("stopped by watchpoint");
6674
6675 if (target_stopped_data_address (current_inferior ()->top_target (),
6676 &addr))
6677 infrun_debug_printf ("stopped data address=%s",
6678 paddress (reg_gdbarch, addr));
6679 else
6680 infrun_debug_printf ("(no data address available)");
6681 }
6682 }
6683
6684 /* This is originated from start_remote(), start_inferior() and
6685 shared libraries hook functions. */
6686 stop_soon = get_inferior_stop_soon (ecs);
6687 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
6688 {
6689 infrun_debug_printf ("quietly stopped");
6690 stop_print_frame = true;
6691 stop_waiting (ecs);
6692 return;
6693 }
6694
6695 /* This originates from attach_command(). We need to overwrite
6696 the stop_signal here, because some kernels don't ignore a
6697 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
6698 See more comments in inferior.h. On the other hand, if we
6699 get a non-SIGSTOP, report it to the user - assume the backend
6700 will handle the SIGSTOP if it should show up later.
6701
6702 Also consider that the attach is complete when we see a
6703 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
6704 target extended-remote report it instead of a SIGSTOP
6705 (e.g. gdbserver). We already rely on SIGTRAP being our
6706 signal, so this is no exception.
6707
6708 Also consider that the attach is complete when we see a
6709 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
6710 the target to stop all threads of the inferior, in case the
6711 low level attach operation doesn't stop them implicitly. If
6712 they weren't stopped implicitly, then the stub will report a
6713 GDB_SIGNAL_0, meaning: stopped for no particular reason
6714 other than GDB's request. */
6715 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
6716 && (ecs->event_thread->stop_signal () == GDB_SIGNAL_STOP
6717 || ecs->event_thread->stop_signal () == GDB_SIGNAL_TRAP
6718 || ecs->event_thread->stop_signal () == GDB_SIGNAL_0))
6719 {
6720 stop_print_frame = true;
6721 stop_waiting (ecs);
6722 ecs->event_thread->set_stop_signal (GDB_SIGNAL_0);
6723 return;
6724 }
6725
6726 /* At this point, get hold of the now-current thread's frame. */
6727 frame = get_current_frame ();
6728 gdbarch = get_frame_arch (frame);
6729
6730 /* Pull the single step breakpoints out of the target. */
6731 if (ecs->event_thread->stop_signal () == GDB_SIGNAL_TRAP)
6732 {
6733 struct regcache *regcache;
6734 CORE_ADDR pc;
6735
6736 regcache = get_thread_regcache (ecs->event_thread);
6737 const address_space *aspace = regcache->aspace ();
6738
6739 pc = regcache_read_pc (regcache);
6740
6741 /* However, before doing so, if this single-step breakpoint was
6742 actually for another thread, set this thread up for moving
6743 past it. */
6744 if (!thread_has_single_step_breakpoint_here (ecs->event_thread,
6745 aspace, pc))
6746 {
6747 if (single_step_breakpoint_inserted_here_p (aspace, pc))
6748 {
6749 infrun_debug_printf ("[%s] hit another thread's single-step "
6750 "breakpoint",
6751 ecs->ptid.to_string ().c_str ());
6752 ecs->hit_singlestep_breakpoint = 1;
6753 }
6754 }
6755 else
6756 {
6757 infrun_debug_printf ("[%s] hit its single-step breakpoint",
6758 ecs->ptid.to_string ().c_str ());
6759 }
6760 }
6761 delete_just_stopped_threads_single_step_breakpoints ();
6762
6763 if (ecs->event_thread->stop_signal () == GDB_SIGNAL_TRAP
6764 && ecs->event_thread->control.trap_expected
6765 && ecs->event_thread->stepping_over_watchpoint)
6766 stopped_by_watchpoint = 0;
6767 else
6768 stopped_by_watchpoint = watchpoints_triggered (ecs->ws);
6769
6770 /* If necessary, step over this watchpoint. We'll be back to display
6771 it in a moment. */
6772 if (stopped_by_watchpoint
6773 && (target_have_steppable_watchpoint ()
6774 || gdbarch_have_nonsteppable_watchpoint (gdbarch)))
6775 {
6776 /* At this point, we are stopped at an instruction which has
6777 attempted to write to a piece of memory under control of
6778 a watchpoint. The instruction hasn't actually executed
6779 yet. If we were to evaluate the watchpoint expression
6780 now, we would get the old value, and therefore no change
6781 would seem to have occurred.
6782
6783 In order to make watchpoints work `right', we really need
6784 to complete the memory write, and then evaluate the
6785 watchpoint expression. We do this by single-stepping the
6786 target.
6787
6788 It may not be necessary to disable the watchpoint to step over
6789 it. For example, the PA can (with some kernel cooperation)
6790 single step over a watchpoint without disabling the watchpoint.
6791
6792 It is far more common to need to disable a watchpoint to step
6793 the inferior over it. If we have non-steppable watchpoints,
6794 we must disable the current watchpoint; it's simplest to
6795 disable all watchpoints.
6796
6797 Any breakpoint at PC must also be stepped over -- if there's
6798 one, it will have already triggered before the watchpoint
6799 triggered, and we either already reported it to the user, or
6800 it didn't cause a stop and we called keep_going. In either
6801 case, if there was a breakpoint at PC, we must be trying to
6802 step past it. */
6803 ecs->event_thread->stepping_over_watchpoint = 1;
6804 keep_going (ecs);
6805 return;
6806 }
6807
6808 ecs->event_thread->stepping_over_breakpoint = 0;
6809 ecs->event_thread->stepping_over_watchpoint = 0;
6810 bpstat_clear (&ecs->event_thread->control.stop_bpstat);
6811 ecs->event_thread->control.stop_step = 0;
6812 stop_print_frame = true;
6813 stopped_by_random_signal = 0;
6814 bpstat *stop_chain = nullptr;
6815
6816 /* Hide inlined functions starting here, unless we just performed stepi or
6817 nexti. After stepi and nexti, always show the innermost frame (not any
6818 inline function call sites). */
6819 if (ecs->event_thread->control.step_range_end != 1)
6820 {
6821 const address_space *aspace
6822 = get_thread_regcache (ecs->event_thread)->aspace ();
6823
6824 /* skip_inline_frames is expensive, so we avoid it if we can
6825 determine that the address is one where functions cannot have
6826 been inlined. This improves performance with inferiors that
6827 load a lot of shared libraries, because the solib event
6828 breakpoint is defined as the address of a function (i.e. not
6829 inline). Note that we have to check the previous PC as well
6830 as the current one to catch cases when we have just
6831 single-stepped off a breakpoint prior to reinstating it.
6832 Note that we're assuming that the code we single-step to is
6833 not inline, but that's not definitive: there's nothing
6834 preventing the event breakpoint function from containing
6835 inlined code, and the single-step ending up there. If the
6836 user had set a breakpoint on that inlined code, the missing
6837 skip_inline_frames call would break things. Fortunately
6838 that's an extremely unlikely scenario. */
6839 if (!pc_at_non_inline_function (aspace,
6840 ecs->event_thread->stop_pc (),
6841 ecs->ws)
6842 && !(ecs->event_thread->stop_signal () == GDB_SIGNAL_TRAP
6843 && ecs->event_thread->control.trap_expected
6844 && pc_at_non_inline_function (aspace,
6845 ecs->event_thread->prev_pc,
6846 ecs->ws)))
6847 {
6848 stop_chain = build_bpstat_chain (aspace,
6849 ecs->event_thread->stop_pc (),
6850 ecs->ws);
6851 skip_inline_frames (ecs->event_thread, stop_chain);
6852
6853 /* Re-fetch current thread's frame in case that invalidated
6854 the frame cache. */
6855 frame = get_current_frame ();
6856 gdbarch = get_frame_arch (frame);
6857 }
6858 }
6859
6860 if (ecs->event_thread->stop_signal () == GDB_SIGNAL_TRAP
6861 && ecs->event_thread->control.trap_expected
6862 && gdbarch_single_step_through_delay_p (gdbarch)
6863 && currently_stepping (ecs->event_thread))
6864 {
6865 /* We're trying to step off a breakpoint. Turns out that we're
6866 also on an instruction that needs to be stepped multiple
6867 times before it's been fully executing. E.g., architectures
6868 with a delay slot. It needs to be stepped twice, once for
6869 the instruction and once for the delay slot. */
6870 int step_through_delay
6871 = gdbarch_single_step_through_delay (gdbarch, frame);
6872
6873 if (step_through_delay)
6874 infrun_debug_printf ("step through delay");
6875
6876 if (ecs->event_thread->control.step_range_end == 0
6877 && step_through_delay)
6878 {
6879 /* The user issued a continue when stopped at a breakpoint.
6880 Set up for another trap and get out of here. */
6881 ecs->event_thread->stepping_over_breakpoint = 1;
6882 keep_going (ecs);
6883 return;
6884 }
6885 else if (step_through_delay)
6886 {
6887 /* The user issued a step when stopped at a breakpoint.
6888 Maybe we should stop, maybe we should not - the delay
6889 slot *might* correspond to a line of source. In any
6890 case, don't decide that here, just set
6891 ecs->stepping_over_breakpoint, making sure we
6892 single-step again before breakpoints are re-inserted. */
6893 ecs->event_thread->stepping_over_breakpoint = 1;
6894 }
6895 }
6896
6897 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
6898 handles this event. */
6899 ecs->event_thread->control.stop_bpstat
6900 = bpstat_stop_status (get_current_regcache ()->aspace (),
6901 ecs->event_thread->stop_pc (),
6902 ecs->event_thread, ecs->ws, stop_chain);
6903
6904 /* Following in case break condition called a
6905 function. */
6906 stop_print_frame = true;
6907
6908 /* This is where we handle "moribund" watchpoints. Unlike
6909 software breakpoints traps, hardware watchpoint traps are
6910 always distinguishable from random traps. If no high-level
6911 watchpoint is associated with the reported stop data address
6912 anymore, then the bpstat does not explain the signal ---
6913 simply make sure to ignore it if `stopped_by_watchpoint' is
6914 set. */
6915
6916 if (ecs->event_thread->stop_signal () == GDB_SIGNAL_TRAP
6917 && !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
6918 GDB_SIGNAL_TRAP)
6919 && stopped_by_watchpoint)
6920 {
6921 infrun_debug_printf ("no user watchpoint explains watchpoint SIGTRAP, "
6922 "ignoring");
6923 }
6924
6925 /* NOTE: cagney/2003-03-29: These checks for a random signal
6926 at one stage in the past included checks for an inferior
6927 function call's call dummy's return breakpoint. The original
6928 comment, that went with the test, read:
6929
6930 ``End of a stack dummy. Some systems (e.g. Sony news) give
6931 another signal besides SIGTRAP, so check here as well as
6932 above.''
6933
6934 If someone ever tries to get call dummys on a
6935 non-executable stack to work (where the target would stop
6936 with something like a SIGSEGV), then those tests might need
6937 to be re-instated. Given, however, that the tests were only
6938 enabled when momentary breakpoints were not being used, I
6939 suspect that it won't be the case.
6940
6941 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
6942 be necessary for call dummies on a non-executable stack on
6943 SPARC. */
6944
6945 /* See if the breakpoints module can explain the signal. */
6946 random_signal
6947 = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
6948 ecs->event_thread->stop_signal ());
6949
6950 /* Maybe this was a trap for a software breakpoint that has since
6951 been removed. */
6952 if (random_signal && target_stopped_by_sw_breakpoint ())
6953 {
6954 if (gdbarch_program_breakpoint_here_p (gdbarch,
6955 ecs->event_thread->stop_pc ()))
6956 {
6957 struct regcache *regcache;
6958 int decr_pc;
6959
6960 /* Re-adjust PC to what the program would see if GDB was not
6961 debugging it. */
6962 regcache = get_thread_regcache (ecs->event_thread);
6963 decr_pc = gdbarch_decr_pc_after_break (gdbarch);
6964 if (decr_pc != 0)
6965 {
6966 gdb::optional<scoped_restore_tmpl<int>>
6967 restore_operation_disable;
6968
6969 if (record_full_is_used ())
6970 restore_operation_disable.emplace
6971 (record_full_gdb_operation_disable_set ());
6972
6973 regcache_write_pc (regcache,
6974 ecs->event_thread->stop_pc () + decr_pc);
6975 }
6976 }
6977 else
6978 {
6979 /* A delayed software breakpoint event. Ignore the trap. */
6980 infrun_debug_printf ("delayed software breakpoint trap, ignoring");
6981 random_signal = 0;
6982 }
6983 }
6984
6985 /* Maybe this was a trap for a hardware breakpoint/watchpoint that
6986 has since been removed. */
6987 if (random_signal && target_stopped_by_hw_breakpoint ())
6988 {
6989 /* A delayed hardware breakpoint event. Ignore the trap. */
6990 infrun_debug_printf ("delayed hardware breakpoint/watchpoint "
6991 "trap, ignoring");
6992 random_signal = 0;
6993 }
6994
6995 /* If not, perhaps stepping/nexting can. */
6996 if (random_signal)
6997 random_signal = !(ecs->event_thread->stop_signal () == GDB_SIGNAL_TRAP
6998 && currently_stepping (ecs->event_thread));
6999
7000 /* Perhaps the thread hit a single-step breakpoint of _another_
7001 thread. Single-step breakpoints are transparent to the
7002 breakpoints module. */
7003 if (random_signal)
7004 random_signal = !ecs->hit_singlestep_breakpoint;
7005
7006 /* No? Perhaps we got a moribund watchpoint. */
7007 if (random_signal)
7008 random_signal = !stopped_by_watchpoint;
7009
7010 /* Always stop if the user explicitly requested this thread to
7011 remain stopped. */
7012 if (ecs->event_thread->stop_requested)
7013 {
7014 random_signal = 1;
7015 infrun_debug_printf ("user-requested stop");
7016 }
7017
7018 /* For the program's own signals, act according to
7019 the signal handling tables. */
7020
7021 if (random_signal)
7022 {
7023 /* Signal not for debugging purposes. */
7024 enum gdb_signal stop_signal = ecs->event_thread->stop_signal ();
7025
7026 infrun_debug_printf ("random signal (%s)",
7027 gdb_signal_to_symbol_string (stop_signal));
7028
7029 stopped_by_random_signal = 1;
7030
7031 /* Always stop on signals if we're either just gaining control
7032 of the program, or the user explicitly requested this thread
7033 to remain stopped. */
7034 if (stop_soon != NO_STOP_QUIETLY
7035 || ecs->event_thread->stop_requested
7036 || signal_stop_state (ecs->event_thread->stop_signal ()))
7037 {
7038 stop_waiting (ecs);
7039 return;
7040 }
7041
7042 /* Notify observers the signal has "handle print" set. Note we
7043 returned early above if stopping; normal_stop handles the
7044 printing in that case. */
7045 if (signal_print[ecs->event_thread->stop_signal ()])
7046 {
7047 /* The signal table tells us to print about this signal. */
7048 target_terminal::ours_for_output ();
7049 notify_signal_received (ecs->event_thread->stop_signal ());
7050 target_terminal::inferior ();
7051 }
7052
7053 /* Clear the signal if it should not be passed. */
7054 if (signal_program[ecs->event_thread->stop_signal ()] == 0)
7055 ecs->event_thread->set_stop_signal (GDB_SIGNAL_0);
7056
7057 if (ecs->event_thread->prev_pc == ecs->event_thread->stop_pc ()
7058 && ecs->event_thread->control.trap_expected
7059 && ecs->event_thread->control.step_resume_breakpoint == nullptr)
7060 {
7061 /* We were just starting a new sequence, attempting to
7062 single-step off of a breakpoint and expecting a SIGTRAP.
7063 Instead this signal arrives. This signal will take us out
7064 of the stepping range so GDB needs to remember to, when
7065 the signal handler returns, resume stepping off that
7066 breakpoint. */
7067 /* To simplify things, "continue" is forced to use the same
7068 code paths as single-step - set a breakpoint at the
7069 signal return address and then, once hit, step off that
7070 breakpoint. */
7071 infrun_debug_printf ("signal arrived while stepping over breakpoint");
7072
7073 insert_hp_step_resume_breakpoint_at_frame (frame);
7074 ecs->event_thread->step_after_step_resume_breakpoint = 1;
7075 /* Reset trap_expected to ensure breakpoints are re-inserted. */
7076 ecs->event_thread->control.trap_expected = 0;
7077
7078 /* If we were nexting/stepping some other thread, switch to
7079 it, so that we don't continue it, losing control. */
7080 if (!switch_back_to_stepped_thread (ecs))
7081 keep_going (ecs);
7082 return;
7083 }
7084
7085 if (ecs->event_thread->stop_signal () != GDB_SIGNAL_0
7086 && (pc_in_thread_step_range (ecs->event_thread->stop_pc (),
7087 ecs->event_thread)
7088 || ecs->event_thread->control.step_range_end == 1)
7089 && (get_stack_frame_id (frame)
7090 == ecs->event_thread->control.step_stack_frame_id)
7091 && ecs->event_thread->control.step_resume_breakpoint == nullptr)
7092 {
7093 /* The inferior is about to take a signal that will take it
7094 out of the single step range. Set a breakpoint at the
7095 current PC (which is presumably where the signal handler
7096 will eventually return) and then allow the inferior to
7097 run free.
7098
7099 Note that this is only needed for a signal delivered
7100 while in the single-step range. Nested signals aren't a
7101 problem as they eventually all return. */
7102 infrun_debug_printf ("signal may take us out of single-step range");
7103
7104 clear_step_over_info ();
7105 insert_hp_step_resume_breakpoint_at_frame (frame);
7106 ecs->event_thread->step_after_step_resume_breakpoint = 1;
7107 /* Reset trap_expected to ensure breakpoints are re-inserted. */
7108 ecs->event_thread->control.trap_expected = 0;
7109 keep_going (ecs);
7110 return;
7111 }
7112
7113 /* Note: step_resume_breakpoint may be non-NULL. This occurs
7114 when either there's a nested signal, or when there's a
7115 pending signal enabled just as the signal handler returns
7116 (leaving the inferior at the step-resume-breakpoint without
7117 actually executing it). Either way continue until the
7118 breakpoint is really hit. */
7119
7120 if (!switch_back_to_stepped_thread (ecs))
7121 {
7122 infrun_debug_printf ("random signal, keep going");
7123
7124 keep_going (ecs);
7125 }
7126 return;
7127 }
7128
7129 process_event_stop_test (ecs);
7130 }
7131
7132 /* Come here when we've got some debug event / signal we can explain
7133 (IOW, not a random signal), and test whether it should cause a
7134 stop, or whether we should resume the inferior (transparently).
7135 E.g., could be a breakpoint whose condition evaluates false; we
7136 could be still stepping within the line; etc. */
7137
7138 static void
7139 process_event_stop_test (struct execution_control_state *ecs)
7140 {
7141 struct symtab_and_line stop_pc_sal;
7142 frame_info_ptr frame;
7143 struct gdbarch *gdbarch;
7144 CORE_ADDR jmp_buf_pc;
7145 struct bpstat_what what;
7146
7147 /* Handle cases caused by hitting a breakpoint. */
7148
7149 frame = get_current_frame ();
7150 gdbarch = get_frame_arch (frame);
7151
7152 what = bpstat_what (ecs->event_thread->control.stop_bpstat);
7153
7154 if (what.call_dummy)
7155 {
7156 stop_stack_dummy = what.call_dummy;
7157 }
7158
7159 /* A few breakpoint types have callbacks associated (e.g.,
7160 bp_jit_event). Run them now. */
7161 bpstat_run_callbacks (ecs->event_thread->control.stop_bpstat);
7162
7163 /* If we hit an internal event that triggers symbol changes, the
7164 current frame will be invalidated within bpstat_what (e.g., if we
7165 hit an internal solib event). Re-fetch it. */
7166 frame = get_current_frame ();
7167 gdbarch = get_frame_arch (frame);
7168
7169 switch (what.main_action)
7170 {
7171 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
7172 /* If we hit the breakpoint at longjmp while stepping, we
7173 install a momentary breakpoint at the target of the
7174 jmp_buf. */
7175
7176 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME");
7177
7178 ecs->event_thread->stepping_over_breakpoint = 1;
7179
7180 if (what.is_longjmp)
7181 {
7182 struct value *arg_value;
7183
7184 /* If we set the longjmp breakpoint via a SystemTap probe,
7185 then use it to extract the arguments. The destination PC
7186 is the third argument to the probe. */
7187 arg_value = probe_safe_evaluate_at_pc (frame, 2);
7188 if (arg_value)
7189 {
7190 jmp_buf_pc = value_as_address (arg_value);
7191 jmp_buf_pc = gdbarch_addr_bits_remove (gdbarch, jmp_buf_pc);
7192 }
7193 else if (!gdbarch_get_longjmp_target_p (gdbarch)
7194 || !gdbarch_get_longjmp_target (gdbarch,
7195 frame, &jmp_buf_pc))
7196 {
7197 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME "
7198 "(!gdbarch_get_longjmp_target)");
7199 keep_going (ecs);
7200 return;
7201 }
7202
7203 /* Insert a breakpoint at resume address. */
7204 insert_longjmp_resume_breakpoint (gdbarch, jmp_buf_pc);
7205 }
7206 else
7207 check_exception_resume (ecs, frame);
7208 keep_going (ecs);
7209 return;
7210
7211 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
7212 {
7213 frame_info_ptr init_frame;
7214
7215 /* There are several cases to consider.
7216
7217 1. The initiating frame no longer exists. In this case we
7218 must stop, because the exception or longjmp has gone too
7219 far.
7220
7221 2. The initiating frame exists, and is the same as the
7222 current frame. We stop, because the exception or longjmp
7223 has been caught.
7224
7225 3. The initiating frame exists and is different from the
7226 current frame. This means the exception or longjmp has
7227 been caught beneath the initiating frame, so keep going.
7228
7229 4. longjmp breakpoint has been placed just to protect
7230 against stale dummy frames and user is not interested in
7231 stopping around longjmps. */
7232
7233 infrun_debug_printf ("BPSTAT_WHAT_CLEAR_LONGJMP_RESUME");
7234
7235 gdb_assert (ecs->event_thread->control.exception_resume_breakpoint
7236 != nullptr);
7237 delete_exception_resume_breakpoint (ecs->event_thread);
7238
7239 if (what.is_longjmp)
7240 {
7241 check_longjmp_breakpoint_for_call_dummy (ecs->event_thread);
7242
7243 if (!frame_id_p (ecs->event_thread->initiating_frame))
7244 {
7245 /* Case 4. */
7246 keep_going (ecs);
7247 return;
7248 }
7249 }
7250
7251 init_frame = frame_find_by_id (ecs->event_thread->initiating_frame);
7252
7253 if (init_frame)
7254 {
7255 struct frame_id current_id
7256 = get_frame_id (get_current_frame ());
7257 if (current_id == ecs->event_thread->initiating_frame)
7258 {
7259 /* Case 2. Fall through. */
7260 }
7261 else
7262 {
7263 /* Case 3. */
7264 keep_going (ecs);
7265 return;
7266 }
7267 }
7268
7269 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
7270 exists. */
7271 delete_step_resume_breakpoint (ecs->event_thread);
7272
7273 end_stepping_range (ecs);
7274 }
7275 return;
7276
7277 case BPSTAT_WHAT_SINGLE:
7278 infrun_debug_printf ("BPSTAT_WHAT_SINGLE");
7279 ecs->event_thread->stepping_over_breakpoint = 1;
7280 /* Still need to check other stuff, at least the case where we
7281 are stepping and step out of the right range. */
7282 break;
7283
7284 case BPSTAT_WHAT_STEP_RESUME:
7285 infrun_debug_printf ("BPSTAT_WHAT_STEP_RESUME");
7286
7287 delete_step_resume_breakpoint (ecs->event_thread);
7288 if (ecs->event_thread->control.proceed_to_finish
7289 && execution_direction == EXEC_REVERSE)
7290 {
7291 struct thread_info *tp = ecs->event_thread;
7292
7293 /* We are finishing a function in reverse, and just hit the
7294 step-resume breakpoint at the start address of the
7295 function, and we're almost there -- just need to back up
7296 by one more single-step, which should take us back to the
7297 function call. */
7298 tp->control.step_range_start = tp->control.step_range_end = 1;
7299 keep_going (ecs);
7300 return;
7301 }
7302 fill_in_stop_func (gdbarch, ecs);
7303 if (ecs->event_thread->stop_pc () == ecs->stop_func_start
7304 && execution_direction == EXEC_REVERSE)
7305 {
7306 /* We are stepping over a function call in reverse, and just
7307 hit the step-resume breakpoint at the start address of
7308 the function. Go back to single-stepping, which should
7309 take us back to the function call. */
7310 ecs->event_thread->stepping_over_breakpoint = 1;
7311 keep_going (ecs);
7312 return;
7313 }
7314 break;
7315
7316 case BPSTAT_WHAT_STOP_NOISY:
7317 infrun_debug_printf ("BPSTAT_WHAT_STOP_NOISY");
7318 stop_print_frame = true;
7319
7320 /* Assume the thread stopped for a breakpoint. We'll still check
7321 whether a/the breakpoint is there when the thread is next
7322 resumed. */
7323 ecs->event_thread->stepping_over_breakpoint = 1;
7324
7325 stop_waiting (ecs);
7326 return;
7327
7328 case BPSTAT_WHAT_STOP_SILENT:
7329 infrun_debug_printf ("BPSTAT_WHAT_STOP_SILENT");
7330 stop_print_frame = false;
7331
7332 /* Assume the thread stopped for a breakpoint. We'll still check
7333 whether a/the breakpoint is there when the thread is next
7334 resumed. */
7335 ecs->event_thread->stepping_over_breakpoint = 1;
7336 stop_waiting (ecs);
7337 return;
7338
7339 case BPSTAT_WHAT_HP_STEP_RESUME:
7340 infrun_debug_printf ("BPSTAT_WHAT_HP_STEP_RESUME");
7341
7342 delete_step_resume_breakpoint (ecs->event_thread);
7343 if (ecs->event_thread->step_after_step_resume_breakpoint)
7344 {
7345 /* Back when the step-resume breakpoint was inserted, we
7346 were trying to single-step off a breakpoint. Go back to
7347 doing that. */
7348 ecs->event_thread->step_after_step_resume_breakpoint = 0;
7349 ecs->event_thread->stepping_over_breakpoint = 1;
7350 keep_going (ecs);
7351 return;
7352 }
7353 break;
7354
7355 case BPSTAT_WHAT_KEEP_CHECKING:
7356 break;
7357 }
7358
7359 /* If we stepped a permanent breakpoint and we had a high priority
7360 step-resume breakpoint for the address we stepped, but we didn't
7361 hit it, then we must have stepped into the signal handler. The
7362 step-resume was only necessary to catch the case of _not_
7363 stepping into the handler, so delete it, and fall through to
7364 checking whether the step finished. */
7365 if (ecs->event_thread->stepped_breakpoint)
7366 {
7367 struct breakpoint *sr_bp
7368 = ecs->event_thread->control.step_resume_breakpoint;
7369
7370 if (sr_bp != nullptr
7371 && sr_bp->first_loc ().permanent
7372 && sr_bp->type == bp_hp_step_resume
7373 && sr_bp->first_loc ().address == ecs->event_thread->prev_pc)
7374 {
7375 infrun_debug_printf ("stepped permanent breakpoint, stopped in handler");
7376 delete_step_resume_breakpoint (ecs->event_thread);
7377 ecs->event_thread->step_after_step_resume_breakpoint = 0;
7378 }
7379 }
7380
7381 /* We come here if we hit a breakpoint but should not stop for it.
7382 Possibly we also were stepping and should stop for that. So fall
7383 through and test for stepping. But, if not stepping, do not
7384 stop. */
7385
7386 /* In all-stop mode, if we're currently stepping but have stopped in
7387 some other thread, we need to switch back to the stepped thread. */
7388 if (switch_back_to_stepped_thread (ecs))
7389 return;
7390
7391 if (ecs->event_thread->control.step_resume_breakpoint)
7392 {
7393 infrun_debug_printf ("step-resume breakpoint is inserted");
7394
7395 /* Having a step-resume breakpoint overrides anything
7396 else having to do with stepping commands until
7397 that breakpoint is reached. */
7398 keep_going (ecs);
7399 return;
7400 }
7401
7402 if (ecs->event_thread->control.step_range_end == 0)
7403 {
7404 infrun_debug_printf ("no stepping, continue");
7405 /* Likewise if we aren't even stepping. */
7406 keep_going (ecs);
7407 return;
7408 }
7409
7410 /* Re-fetch current thread's frame in case the code above caused
7411 the frame cache to be re-initialized, making our FRAME variable
7412 a dangling pointer. */
7413 frame = get_current_frame ();
7414 gdbarch = get_frame_arch (frame);
7415 fill_in_stop_func (gdbarch, ecs);
7416
7417 /* If stepping through a line, keep going if still within it.
7418
7419 Note that step_range_end is the address of the first instruction
7420 beyond the step range, and NOT the address of the last instruction
7421 within it!
7422
7423 Note also that during reverse execution, we may be stepping
7424 through a function epilogue and therefore must detect when
7425 the current-frame changes in the middle of a line. */
7426
7427 if (pc_in_thread_step_range (ecs->event_thread->stop_pc (),
7428 ecs->event_thread)
7429 && (execution_direction != EXEC_REVERSE
7430 || get_frame_id (frame) == ecs->event_thread->control.step_frame_id))
7431 {
7432 infrun_debug_printf
7433 ("stepping inside range [%s-%s]",
7434 paddress (gdbarch, ecs->event_thread->control.step_range_start),
7435 paddress (gdbarch, ecs->event_thread->control.step_range_end));
7436
7437 /* Tentatively re-enable range stepping; `resume' disables it if
7438 necessary (e.g., if we're stepping over a breakpoint or we
7439 have software watchpoints). */
7440 ecs->event_thread->control.may_range_step = 1;
7441
7442 /* When stepping backward, stop at beginning of line range
7443 (unless it's the function entry point, in which case
7444 keep going back to the call point). */
7445 CORE_ADDR stop_pc = ecs->event_thread->stop_pc ();
7446 if (stop_pc == ecs->event_thread->control.step_range_start
7447 && stop_pc != ecs->stop_func_start
7448 && execution_direction == EXEC_REVERSE)
7449 end_stepping_range (ecs);
7450 else
7451 keep_going (ecs);
7452
7453 return;
7454 }
7455
7456 /* We stepped out of the stepping range. */
7457
7458 /* If we are stepping at the source level and entered the runtime
7459 loader dynamic symbol resolution code...
7460
7461 EXEC_FORWARD: we keep on single stepping until we exit the run
7462 time loader code and reach the callee's address.
7463
7464 EXEC_REVERSE: we've already executed the callee (backward), and
7465 the runtime loader code is handled just like any other
7466 undebuggable function call. Now we need only keep stepping
7467 backward through the trampoline code, and that's handled further
7468 down, so there is nothing for us to do here. */
7469
7470 if (execution_direction != EXEC_REVERSE
7471 && ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
7472 && in_solib_dynsym_resolve_code (ecs->event_thread->stop_pc ())
7473 && (ecs->event_thread->control.step_start_function == nullptr
7474 || !in_solib_dynsym_resolve_code (
7475 ecs->event_thread->control.step_start_function->value_block ()
7476 ->entry_pc ())))
7477 {
7478 CORE_ADDR pc_after_resolver =
7479 gdbarch_skip_solib_resolver (gdbarch, ecs->event_thread->stop_pc ());
7480
7481 infrun_debug_printf ("stepped into dynsym resolve code");
7482
7483 if (pc_after_resolver)
7484 {
7485 /* Set up a step-resume breakpoint at the address
7486 indicated by SKIP_SOLIB_RESOLVER. */
7487 symtab_and_line sr_sal;
7488 sr_sal.pc = pc_after_resolver;
7489 sr_sal.pspace = get_frame_program_space (frame);
7490
7491 insert_step_resume_breakpoint_at_sal (gdbarch,
7492 sr_sal, null_frame_id);
7493 }
7494
7495 keep_going (ecs);
7496 return;
7497 }
7498
7499 /* Step through an indirect branch thunk. */
7500 if (ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
7501 && gdbarch_in_indirect_branch_thunk (gdbarch,
7502 ecs->event_thread->stop_pc ()))
7503 {
7504 infrun_debug_printf ("stepped into indirect branch thunk");
7505 keep_going (ecs);
7506 return;
7507 }
7508
7509 if (ecs->event_thread->control.step_range_end != 1
7510 && (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
7511 || ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
7512 && get_frame_type (frame) == SIGTRAMP_FRAME)
7513 {
7514 infrun_debug_printf ("stepped into signal trampoline");
7515 /* The inferior, while doing a "step" or "next", has ended up in
7516 a signal trampoline (either by a signal being delivered or by
7517 the signal handler returning). Just single-step until the
7518 inferior leaves the trampoline (either by calling the handler
7519 or returning). */
7520 keep_going (ecs);
7521 return;
7522 }
7523
7524 /* If we're in the return path from a shared library trampoline,
7525 we want to proceed through the trampoline when stepping. */
7526 /* macro/2012-04-25: This needs to come before the subroutine
7527 call check below as on some targets return trampolines look
7528 like subroutine calls (MIPS16 return thunks). */
7529 if (gdbarch_in_solib_return_trampoline (gdbarch,
7530 ecs->event_thread->stop_pc (),
7531 ecs->stop_func_name)
7532 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
7533 {
7534 /* Determine where this trampoline returns. */
7535 CORE_ADDR stop_pc = ecs->event_thread->stop_pc ();
7536 CORE_ADDR real_stop_pc
7537 = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
7538
7539 infrun_debug_printf ("stepped into solib return tramp");
7540
7541 /* Only proceed through if we know where it's going. */
7542 if (real_stop_pc)
7543 {
7544 /* And put the step-breakpoint there and go until there. */
7545 symtab_and_line sr_sal;
7546 sr_sal.pc = real_stop_pc;
7547 sr_sal.section = find_pc_overlay (sr_sal.pc);
7548 sr_sal.pspace = get_frame_program_space (frame);
7549
7550 /* Do not specify what the fp should be when we stop since
7551 on some machines the prologue is where the new fp value
7552 is established. */
7553 insert_step_resume_breakpoint_at_sal (gdbarch,
7554 sr_sal, null_frame_id);
7555
7556 /* Restart without fiddling with the step ranges or
7557 other state. */
7558 keep_going (ecs);
7559 return;
7560 }
7561 }
7562
7563 /* Check for subroutine calls. The check for the current frame
7564 equalling the step ID is not necessary - the check of the
7565 previous frame's ID is sufficient - but it is a common case and
7566 cheaper than checking the previous frame's ID.
7567
7568 NOTE: frame_id::operator== will never report two invalid frame IDs as
7569 being equal, so to get into this block, both the current and
7570 previous frame must have valid frame IDs. */
7571 /* The outer_frame_id check is a heuristic to detect stepping
7572 through startup code. If we step over an instruction which
7573 sets the stack pointer from an invalid value to a valid value,
7574 we may detect that as a subroutine call from the mythical
7575 "outermost" function. This could be fixed by marking
7576 outermost frames as !stack_p,code_p,special_p. Then the
7577 initial outermost frame, before sp was valid, would
7578 have code_addr == &_start. See the comment in frame_id::operator==
7579 for more. */
7580
7581 /* We want "nexti" to step into, not over, signal handlers invoked
7582 by the kernel, therefore this subroutine check should not trigger
7583 for a signal handler invocation. On most platforms, this is already
7584 not the case, as the kernel puts a signal trampoline frame onto the
7585 stack to handle proper return after the handler, and therefore at this
7586 point, the current frame is a grandchild of the step frame, not a
7587 child. However, on some platforms, the kernel actually uses a
7588 trampoline to handle *invocation* of the handler. In that case,
7589 when executing the first instruction of the trampoline, this check
7590 would erroneously detect the trampoline invocation as a subroutine
7591 call. Fix this by checking for SIGTRAMP_FRAME. */
7592 if ((get_stack_frame_id (frame)
7593 != ecs->event_thread->control.step_stack_frame_id)
7594 && get_frame_type (frame) != SIGTRAMP_FRAME
7595 && ((frame_unwind_caller_id (get_current_frame ())
7596 == ecs->event_thread->control.step_stack_frame_id)
7597 && ((ecs->event_thread->control.step_stack_frame_id
7598 != outer_frame_id)
7599 || (ecs->event_thread->control.step_start_function
7600 != find_pc_function (ecs->event_thread->stop_pc ())))))
7601 {
7602 CORE_ADDR stop_pc = ecs->event_thread->stop_pc ();
7603 CORE_ADDR real_stop_pc;
7604
7605 infrun_debug_printf ("stepped into subroutine");
7606
7607 if (ecs->event_thread->control.step_over_calls == STEP_OVER_NONE)
7608 {
7609 /* I presume that step_over_calls is only 0 when we're
7610 supposed to be stepping at the assembly language level
7611 ("stepi"). Just stop. */
7612 /* And this works the same backward as frontward. MVS */
7613 end_stepping_range (ecs);
7614 return;
7615 }
7616
7617 /* Reverse stepping through solib trampolines. */
7618
7619 if (execution_direction == EXEC_REVERSE
7620 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
7621 && (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
7622 || (ecs->stop_func_start == 0
7623 && in_solib_dynsym_resolve_code (stop_pc))))
7624 {
7625 /* Any solib trampoline code can be handled in reverse
7626 by simply continuing to single-step. We have already
7627 executed the solib function (backwards), and a few
7628 steps will take us back through the trampoline to the
7629 caller. */
7630 keep_going (ecs);
7631 return;
7632 }
7633
7634 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
7635 {
7636 /* We're doing a "next".
7637
7638 Normal (forward) execution: set a breakpoint at the
7639 callee's return address (the address at which the caller
7640 will resume).
7641
7642 Reverse (backward) execution. set the step-resume
7643 breakpoint at the start of the function that we just
7644 stepped into (backwards), and continue to there. When we
7645 get there, we'll need to single-step back to the caller. */
7646
7647 if (execution_direction == EXEC_REVERSE)
7648 {
7649 /* If we're already at the start of the function, we've either
7650 just stepped backward into a single instruction function,
7651 or stepped back out of a signal handler to the first instruction
7652 of the function. Just keep going, which will single-step back
7653 to the caller. */
7654 if (ecs->stop_func_start != stop_pc && ecs->stop_func_start != 0)
7655 {
7656 /* Normal function call return (static or dynamic). */
7657 symtab_and_line sr_sal;
7658 sr_sal.pc = ecs->stop_func_start;
7659 sr_sal.pspace = get_frame_program_space (frame);
7660 insert_step_resume_breakpoint_at_sal (gdbarch,
7661 sr_sal, get_stack_frame_id (frame));
7662 }
7663 }
7664 else
7665 insert_step_resume_breakpoint_at_caller (frame);
7666
7667 keep_going (ecs);
7668 return;
7669 }
7670
7671 /* If we are in a function call trampoline (a stub between the
7672 calling routine and the real function), locate the real
7673 function. That's what tells us (a) whether we want to step
7674 into it at all, and (b) what prologue we want to run to the
7675 end of, if we do step into it. */
7676 real_stop_pc = skip_language_trampoline (frame, stop_pc);
7677 if (real_stop_pc == 0)
7678 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
7679 if (real_stop_pc != 0)
7680 ecs->stop_func_start = real_stop_pc;
7681
7682 if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
7683 {
7684 symtab_and_line sr_sal;
7685 sr_sal.pc = ecs->stop_func_start;
7686 sr_sal.pspace = get_frame_program_space (frame);
7687
7688 insert_step_resume_breakpoint_at_sal (gdbarch,
7689 sr_sal, null_frame_id);
7690 keep_going (ecs);
7691 return;
7692 }
7693
7694 /* If we have line number information for the function we are
7695 thinking of stepping into and the function isn't on the skip
7696 list, step into it.
7697
7698 If there are several symtabs at that PC (e.g. with include
7699 files), just want to know whether *any* of them have line
7700 numbers. find_pc_line handles this. */
7701 {
7702 struct symtab_and_line tmp_sal;
7703
7704 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
7705 if (tmp_sal.line != 0
7706 && !function_name_is_marked_for_skip (ecs->stop_func_name,
7707 tmp_sal)
7708 && !inline_frame_is_marked_for_skip (true, ecs->event_thread))
7709 {
7710 if (execution_direction == EXEC_REVERSE)
7711 handle_step_into_function_backward (gdbarch, ecs);
7712 else
7713 handle_step_into_function (gdbarch, ecs);
7714 return;
7715 }
7716 }
7717
7718 /* If we have no line number and the step-stop-if-no-debug is
7719 set, we stop the step so that the user has a chance to switch
7720 in assembly mode. */
7721 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
7722 && step_stop_if_no_debug)
7723 {
7724 end_stepping_range (ecs);
7725 return;
7726 }
7727
7728 if (execution_direction == EXEC_REVERSE)
7729 {
7730 /* If we're already at the start of the function, we've either just
7731 stepped backward into a single instruction function without line
7732 number info, or stepped back out of a signal handler to the first
7733 instruction of the function without line number info. Just keep
7734 going, which will single-step back to the caller. */
7735 if (ecs->stop_func_start != stop_pc)
7736 {
7737 /* Set a breakpoint at callee's start address.
7738 From there we can step once and be back in the caller. */
7739 symtab_and_line sr_sal;
7740 sr_sal.pc = ecs->stop_func_start;
7741 sr_sal.pspace = get_frame_program_space (frame);
7742 insert_step_resume_breakpoint_at_sal (gdbarch,
7743 sr_sal, null_frame_id);
7744 }
7745 }
7746 else
7747 /* Set a breakpoint at callee's return address (the address
7748 at which the caller will resume). */
7749 insert_step_resume_breakpoint_at_caller (frame);
7750
7751 keep_going (ecs);
7752 return;
7753 }
7754
7755 /* Reverse stepping through solib trampolines. */
7756
7757 if (execution_direction == EXEC_REVERSE
7758 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
7759 {
7760 CORE_ADDR stop_pc = ecs->event_thread->stop_pc ();
7761
7762 if (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
7763 || (ecs->stop_func_start == 0
7764 && in_solib_dynsym_resolve_code (stop_pc)))
7765 {
7766 /* Any solib trampoline code can be handled in reverse
7767 by simply continuing to single-step. We have already
7768 executed the solib function (backwards), and a few
7769 steps will take us back through the trampoline to the
7770 caller. */
7771 keep_going (ecs);
7772 return;
7773 }
7774 else if (in_solib_dynsym_resolve_code (stop_pc))
7775 {
7776 /* Stepped backward into the solib dynsym resolver.
7777 Set a breakpoint at its start and continue, then
7778 one more step will take us out. */
7779 symtab_and_line sr_sal;
7780 sr_sal.pc = ecs->stop_func_start;
7781 sr_sal.pspace = get_frame_program_space (frame);
7782 insert_step_resume_breakpoint_at_sal (gdbarch,
7783 sr_sal, null_frame_id);
7784 keep_going (ecs);
7785 return;
7786 }
7787 }
7788
7789 /* This always returns the sal for the inner-most frame when we are in a
7790 stack of inlined frames, even if GDB actually believes that it is in a
7791 more outer frame. This is checked for below by calls to
7792 inline_skipped_frames. */
7793 stop_pc_sal = find_pc_line (ecs->event_thread->stop_pc (), 0);
7794
7795 /* NOTE: tausq/2004-05-24: This if block used to be done before all
7796 the trampoline processing logic, however, there are some trampolines
7797 that have no names, so we should do trampoline handling first. */
7798 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
7799 && ecs->stop_func_name == nullptr
7800 && stop_pc_sal.line == 0)
7801 {
7802 infrun_debug_printf ("stepped into undebuggable function");
7803
7804 /* The inferior just stepped into, or returned to, an
7805 undebuggable function (where there is no debugging information
7806 and no line number corresponding to the address where the
7807 inferior stopped). Since we want to skip this kind of code,
7808 we keep going until the inferior returns from this
7809 function - unless the user has asked us not to (via
7810 set step-mode) or we no longer know how to get back
7811 to the call site. */
7812 if (step_stop_if_no_debug
7813 || !frame_id_p (frame_unwind_caller_id (frame)))
7814 {
7815 /* If we have no line number and the step-stop-if-no-debug
7816 is set, we stop the step so that the user has a chance to
7817 switch in assembly mode. */
7818 end_stepping_range (ecs);
7819 return;
7820 }
7821 else
7822 {
7823 /* Set a breakpoint at callee's return address (the address
7824 at which the caller will resume). */
7825 insert_step_resume_breakpoint_at_caller (frame);
7826 keep_going (ecs);
7827 return;
7828 }
7829 }
7830
7831 if (execution_direction == EXEC_REVERSE
7832 && ecs->event_thread->control.proceed_to_finish
7833 && ecs->event_thread->stop_pc () >= ecs->stop_func_alt_start
7834 && ecs->event_thread->stop_pc () < ecs->stop_func_start)
7835 {
7836 /* We are executing the reverse-finish command.
7837 If the system supports multiple entry points and we are finishing a
7838 function in reverse. If we are between the entry points single-step
7839 back to the alternate entry point. If we are at the alternate entry
7840 point -- just need to back up by one more single-step, which
7841 should take us back to the function call. */
7842 ecs->event_thread->control.step_range_start
7843 = ecs->event_thread->control.step_range_end = 1;
7844 keep_going (ecs);
7845 return;
7846
7847 }
7848
7849 if (ecs->event_thread->control.step_range_end == 1)
7850 {
7851 /* It is stepi or nexti. We always want to stop stepping after
7852 one instruction. */
7853 infrun_debug_printf ("stepi/nexti");
7854 end_stepping_range (ecs);
7855 return;
7856 }
7857
7858 if (stop_pc_sal.line == 0)
7859 {
7860 /* We have no line number information. That means to stop
7861 stepping (does this always happen right after one instruction,
7862 when we do "s" in a function with no line numbers,
7863 or can this happen as a result of a return or longjmp?). */
7864 infrun_debug_printf ("line number info");
7865 end_stepping_range (ecs);
7866 return;
7867 }
7868
7869 /* Look for "calls" to inlined functions, part one. If the inline
7870 frame machinery detected some skipped call sites, we have entered
7871 a new inline function. */
7872
7873 if ((get_frame_id (get_current_frame ())
7874 == ecs->event_thread->control.step_frame_id)
7875 && inline_skipped_frames (ecs->event_thread))
7876 {
7877 infrun_debug_printf ("stepped into inlined function");
7878
7879 symtab_and_line call_sal = find_frame_sal (get_current_frame ());
7880
7881 if (ecs->event_thread->control.step_over_calls != STEP_OVER_ALL)
7882 {
7883 /* For "step", we're going to stop. But if the call site
7884 for this inlined function is on the same source line as
7885 we were previously stepping, go down into the function
7886 first. Otherwise stop at the call site. */
7887
7888 if (call_sal.line == ecs->event_thread->current_line
7889 && call_sal.symtab == ecs->event_thread->current_symtab)
7890 {
7891 step_into_inline_frame (ecs->event_thread);
7892 if (inline_frame_is_marked_for_skip (false, ecs->event_thread))
7893 {
7894 keep_going (ecs);
7895 return;
7896 }
7897 }
7898
7899 end_stepping_range (ecs);
7900 return;
7901 }
7902 else
7903 {
7904 /* For "next", we should stop at the call site if it is on a
7905 different source line. Otherwise continue through the
7906 inlined function. */
7907 if (call_sal.line == ecs->event_thread->current_line
7908 && call_sal.symtab == ecs->event_thread->current_symtab)
7909 keep_going (ecs);
7910 else
7911 end_stepping_range (ecs);
7912 return;
7913 }
7914 }
7915
7916 /* Look for "calls" to inlined functions, part two. If we are still
7917 in the same real function we were stepping through, but we have
7918 to go further up to find the exact frame ID, we are stepping
7919 through a more inlined call beyond its call site. */
7920
7921 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
7922 && (get_frame_id (get_current_frame ())
7923 != ecs->event_thread->control.step_frame_id)
7924 && stepped_in_from (get_current_frame (),
7925 ecs->event_thread->control.step_frame_id))
7926 {
7927 infrun_debug_printf ("stepping through inlined function");
7928
7929 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL
7930 || inline_frame_is_marked_for_skip (false, ecs->event_thread))
7931 keep_going (ecs);
7932 else
7933 end_stepping_range (ecs);
7934 return;
7935 }
7936
7937 bool refresh_step_info = true;
7938 if ((ecs->event_thread->stop_pc () == stop_pc_sal.pc)
7939 && (ecs->event_thread->current_line != stop_pc_sal.line
7940 || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
7941 {
7942 /* We are at a different line. */
7943
7944 if (stop_pc_sal.is_stmt)
7945 {
7946 /* We are at the start of a statement.
7947
7948 So stop. Note that we don't stop if we step into the middle of a
7949 statement. That is said to make things like for (;;) statements
7950 work better. */
7951 infrun_debug_printf ("stepped to a different line");
7952 end_stepping_range (ecs);
7953 return;
7954 }
7955 else if (get_frame_id (get_current_frame ())
7956 == ecs->event_thread->control.step_frame_id)
7957 {
7958 /* We are not at the start of a statement, and we have not changed
7959 frame.
7960
7961 We ignore this line table entry, and continue stepping forward,
7962 looking for a better place to stop. */
7963 refresh_step_info = false;
7964 infrun_debug_printf ("stepped to a different line, but "
7965 "it's not the start of a statement");
7966 }
7967 else
7968 {
7969 /* We are not the start of a statement, and we have changed frame.
7970
7971 We ignore this line table entry, and continue stepping forward,
7972 looking for a better place to stop. Keep refresh_step_info at
7973 true to note that the frame has changed, but ignore the line
7974 number to make sure we don't ignore a subsequent entry with the
7975 same line number. */
7976 stop_pc_sal.line = 0;
7977 infrun_debug_printf ("stepped to a different frame, but "
7978 "it's not the start of a statement");
7979 }
7980 }
7981
7982 /* We aren't done stepping.
7983
7984 Optimize by setting the stepping range to the line.
7985 (We might not be in the original line, but if we entered a
7986 new line in mid-statement, we continue stepping. This makes
7987 things like for(;;) statements work better.)
7988
7989 If we entered a SAL that indicates a non-statement line table entry,
7990 then we update the stepping range, but we don't update the step info,
7991 which includes things like the line number we are stepping away from.
7992 This means we will stop when we find a line table entry that is marked
7993 as is-statement, even if it matches the non-statement one we just
7994 stepped into. */
7995
7996 ecs->event_thread->control.step_range_start = stop_pc_sal.pc;
7997 ecs->event_thread->control.step_range_end = stop_pc_sal.end;
7998 ecs->event_thread->control.may_range_step = 1;
7999 infrun_debug_printf
8000 ("updated step range, start = %s, end = %s, may_range_step = %d",
8001 paddress (gdbarch, ecs->event_thread->control.step_range_start),
8002 paddress (gdbarch, ecs->event_thread->control.step_range_end),
8003 ecs->event_thread->control.may_range_step);
8004 if (refresh_step_info)
8005 set_step_info (ecs->event_thread, frame, stop_pc_sal);
8006
8007 infrun_debug_printf ("keep going");
8008 keep_going (ecs);
8009 }
8010
8011 static bool restart_stepped_thread (process_stratum_target *resume_target,
8012 ptid_t resume_ptid);
8013
8014 /* In all-stop mode, if we're currently stepping but have stopped in
8015 some other thread, we may need to switch back to the stepped
8016 thread. Returns true we set the inferior running, false if we left
8017 it stopped (and the event needs further processing). */
8018
8019 static bool
8020 switch_back_to_stepped_thread (struct execution_control_state *ecs)
8021 {
8022 if (!target_is_non_stop_p ())
8023 {
8024 /* If any thread is blocked on some internal breakpoint, and we
8025 simply need to step over that breakpoint to get it going
8026 again, do that first. */
8027
8028 /* However, if we see an event for the stepping thread, then we
8029 know all other threads have been moved past their breakpoints
8030 already. Let the caller check whether the step is finished,
8031 etc., before deciding to move it past a breakpoint. */
8032 if (ecs->event_thread->control.step_range_end != 0)
8033 return false;
8034
8035 /* Check if the current thread is blocked on an incomplete
8036 step-over, interrupted by a random signal. */
8037 if (ecs->event_thread->control.trap_expected
8038 && ecs->event_thread->stop_signal () != GDB_SIGNAL_TRAP)
8039 {
8040 infrun_debug_printf
8041 ("need to finish step-over of [%s]",
8042 ecs->event_thread->ptid.to_string ().c_str ());
8043 keep_going (ecs);
8044 return true;
8045 }
8046
8047 /* Check if the current thread is blocked by a single-step
8048 breakpoint of another thread. */
8049 if (ecs->hit_singlestep_breakpoint)
8050 {
8051 infrun_debug_printf ("need to step [%s] over single-step breakpoint",
8052 ecs->ptid.to_string ().c_str ());
8053 keep_going (ecs);
8054 return true;
8055 }
8056
8057 /* If this thread needs yet another step-over (e.g., stepping
8058 through a delay slot), do it first before moving on to
8059 another thread. */
8060 if (thread_still_needs_step_over (ecs->event_thread))
8061 {
8062 infrun_debug_printf
8063 ("thread [%s] still needs step-over",
8064 ecs->event_thread->ptid.to_string ().c_str ());
8065 keep_going (ecs);
8066 return true;
8067 }
8068
8069 /* If scheduler locking applies even if not stepping, there's no
8070 need to walk over threads. Above we've checked whether the
8071 current thread is stepping. If some other thread not the
8072 event thread is stepping, then it must be that scheduler
8073 locking is not in effect. */
8074 if (schedlock_applies (ecs->event_thread))
8075 return false;
8076
8077 /* Otherwise, we no longer expect a trap in the current thread.
8078 Clear the trap_expected flag before switching back -- this is
8079 what keep_going does as well, if we call it. */
8080 ecs->event_thread->control.trap_expected = 0;
8081
8082 /* Likewise, clear the signal if it should not be passed. */
8083 if (!signal_program[ecs->event_thread->stop_signal ()])
8084 ecs->event_thread->set_stop_signal (GDB_SIGNAL_0);
8085
8086 if (restart_stepped_thread (ecs->target, ecs->ptid))
8087 {
8088 prepare_to_wait (ecs);
8089 return true;
8090 }
8091
8092 switch_to_thread (ecs->event_thread);
8093 }
8094
8095 return false;
8096 }
8097
8098 /* Look for the thread that was stepping, and resume it.
8099 RESUME_TARGET / RESUME_PTID indicate the set of threads the caller
8100 is resuming. Return true if a thread was started, false
8101 otherwise. */
8102
8103 static bool
8104 restart_stepped_thread (process_stratum_target *resume_target,
8105 ptid_t resume_ptid)
8106 {
8107 /* Do all pending step-overs before actually proceeding with
8108 step/next/etc. */
8109 if (start_step_over ())
8110 return true;
8111
8112 for (thread_info *tp : all_threads_safe ())
8113 {
8114 if (tp->state == THREAD_EXITED)
8115 continue;
8116
8117 if (tp->has_pending_waitstatus ())
8118 continue;
8119
8120 /* Ignore threads of processes the caller is not
8121 resuming. */
8122 if (!sched_multi
8123 && (tp->inf->process_target () != resume_target
8124 || tp->inf->pid != resume_ptid.pid ()))
8125 continue;
8126
8127 if (tp->control.trap_expected)
8128 {
8129 infrun_debug_printf ("switching back to stepped thread (step-over)");
8130
8131 if (keep_going_stepped_thread (tp))
8132 return true;
8133 }
8134 }
8135
8136 for (thread_info *tp : all_threads_safe ())
8137 {
8138 if (tp->state == THREAD_EXITED)
8139 continue;
8140
8141 if (tp->has_pending_waitstatus ())
8142 continue;
8143
8144 /* Ignore threads of processes the caller is not
8145 resuming. */
8146 if (!sched_multi
8147 && (tp->inf->process_target () != resume_target
8148 || tp->inf->pid != resume_ptid.pid ()))
8149 continue;
8150
8151 /* Did we find the stepping thread? */
8152 if (tp->control.step_range_end)
8153 {
8154 infrun_debug_printf ("switching back to stepped thread (stepping)");
8155
8156 if (keep_going_stepped_thread (tp))
8157 return true;
8158 }
8159 }
8160
8161 return false;
8162 }
8163
8164 /* See infrun.h. */
8165
8166 void
8167 restart_after_all_stop_detach (process_stratum_target *proc_target)
8168 {
8169 /* Note we don't check target_is_non_stop_p() here, because the
8170 current inferior may no longer have a process_stratum target
8171 pushed, as we just detached. */
8172
8173 /* See if we have a THREAD_RUNNING thread that need to be
8174 re-resumed. If we have any thread that is already executing,
8175 then we don't need to resume the target -- it is already been
8176 resumed. With the remote target (in all-stop), it's even
8177 impossible to issue another resumption if the target is already
8178 resumed, until the target reports a stop. */
8179 for (thread_info *thr : all_threads (proc_target))
8180 {
8181 if (thr->state != THREAD_RUNNING)
8182 continue;
8183
8184 /* If we have any thread that is already executing, then we
8185 don't need to resume the target -- it is already been
8186 resumed. */
8187 if (thr->executing ())
8188 return;
8189
8190 /* If we have a pending event to process, skip resuming the
8191 target and go straight to processing it. */
8192 if (thr->resumed () && thr->has_pending_waitstatus ())
8193 return;
8194 }
8195
8196 /* Alright, we need to re-resume the target. If a thread was
8197 stepping, we need to restart it stepping. */
8198 if (restart_stepped_thread (proc_target, minus_one_ptid))
8199 return;
8200
8201 /* Otherwise, find the first THREAD_RUNNING thread and resume
8202 it. */
8203 for (thread_info *thr : all_threads (proc_target))
8204 {
8205 if (thr->state != THREAD_RUNNING)
8206 continue;
8207
8208 execution_control_state ecs (thr);
8209 switch_to_thread (thr);
8210 keep_going (&ecs);
8211 return;
8212 }
8213 }
8214
8215 /* Set a previously stepped thread back to stepping. Returns true on
8216 success, false if the resume is not possible (e.g., the thread
8217 vanished). */
8218
8219 static bool
8220 keep_going_stepped_thread (struct thread_info *tp)
8221 {
8222 frame_info_ptr frame;
8223
8224 /* If the stepping thread exited, then don't try to switch back and
8225 resume it, which could fail in several different ways depending
8226 on the target. Instead, just keep going.
8227
8228 We can find a stepping dead thread in the thread list in two
8229 cases:
8230
8231 - The target supports thread exit events, and when the target
8232 tries to delete the thread from the thread list, inferior_ptid
8233 pointed at the exiting thread. In such case, calling
8234 delete_thread does not really remove the thread from the list;
8235 instead, the thread is left listed, with 'exited' state.
8236
8237 - The target's debug interface does not support thread exit
8238 events, and so we have no idea whatsoever if the previously
8239 stepping thread is still alive. For that reason, we need to
8240 synchronously query the target now. */
8241
8242 if (tp->state == THREAD_EXITED || !target_thread_alive (tp->ptid))
8243 {
8244 infrun_debug_printf ("not resuming previously stepped thread, it has "
8245 "vanished");
8246
8247 delete_thread (tp);
8248 return false;
8249 }
8250
8251 infrun_debug_printf ("resuming previously stepped thread");
8252
8253 execution_control_state ecs (tp);
8254 switch_to_thread (tp);
8255
8256 tp->set_stop_pc (regcache_read_pc (get_thread_regcache (tp)));
8257 frame = get_current_frame ();
8258
8259 /* If the PC of the thread we were trying to single-step has
8260 changed, then that thread has trapped or been signaled, but the
8261 event has not been reported to GDB yet. Re-poll the target
8262 looking for this particular thread's event (i.e. temporarily
8263 enable schedlock) by:
8264
8265 - setting a break at the current PC
8266 - resuming that particular thread, only (by setting trap
8267 expected)
8268
8269 This prevents us continuously moving the single-step breakpoint
8270 forward, one instruction at a time, overstepping. */
8271
8272 if (tp->stop_pc () != tp->prev_pc)
8273 {
8274 ptid_t resume_ptid;
8275
8276 infrun_debug_printf ("expected thread advanced also (%s -> %s)",
8277 paddress (current_inferior ()->arch (), tp->prev_pc),
8278 paddress (current_inferior ()->arch (),
8279 tp->stop_pc ()));
8280
8281 /* Clear the info of the previous step-over, as it's no longer
8282 valid (if the thread was trying to step over a breakpoint, it
8283 has already succeeded). It's what keep_going would do too,
8284 if we called it. Do this before trying to insert the sss
8285 breakpoint, otherwise if we were previously trying to step
8286 over this exact address in another thread, the breakpoint is
8287 skipped. */
8288 clear_step_over_info ();
8289 tp->control.trap_expected = 0;
8290
8291 insert_single_step_breakpoint (get_frame_arch (frame),
8292 get_frame_address_space (frame),
8293 tp->stop_pc ());
8294
8295 tp->set_resumed (true);
8296 resume_ptid = internal_resume_ptid (tp->control.stepping_command);
8297 do_target_resume (resume_ptid, false, GDB_SIGNAL_0);
8298 }
8299 else
8300 {
8301 infrun_debug_printf ("expected thread still hasn't advanced");
8302
8303 keep_going_pass_signal (&ecs);
8304 }
8305
8306 return true;
8307 }
8308
8309 /* Is thread TP in the middle of (software or hardware)
8310 single-stepping? (Note the result of this function must never be
8311 passed directly as target_resume's STEP parameter.) */
8312
8313 static bool
8314 currently_stepping (struct thread_info *tp)
8315 {
8316 return ((tp->control.step_range_end
8317 && tp->control.step_resume_breakpoint == nullptr)
8318 || tp->control.trap_expected
8319 || tp->stepped_breakpoint
8320 || bpstat_should_step ());
8321 }
8322
8323 /* Inferior has stepped into a subroutine call with source code that
8324 we should not step over. Do step to the first line of code in
8325 it. */
8326
8327 static void
8328 handle_step_into_function (struct gdbarch *gdbarch,
8329 struct execution_control_state *ecs)
8330 {
8331 fill_in_stop_func (gdbarch, ecs);
8332
8333 compunit_symtab *cust
8334 = find_pc_compunit_symtab (ecs->event_thread->stop_pc ());
8335 if (cust != nullptr && cust->language () != language_asm)
8336 ecs->stop_func_start
8337 = gdbarch_skip_prologue_noexcept (gdbarch, ecs->stop_func_start);
8338
8339 symtab_and_line stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
8340 /* Use the step_resume_break to step until the end of the prologue,
8341 even if that involves jumps (as it seems to on the vax under
8342 4.2). */
8343 /* If the prologue ends in the middle of a source line, continue to
8344 the end of that source line (if it is still within the function).
8345 Otherwise, just go to end of prologue. */
8346 if (stop_func_sal.end
8347 && stop_func_sal.pc != ecs->stop_func_start
8348 && stop_func_sal.end < ecs->stop_func_end)
8349 ecs->stop_func_start = stop_func_sal.end;
8350
8351 /* Architectures which require breakpoint adjustment might not be able
8352 to place a breakpoint at the computed address. If so, the test
8353 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
8354 ecs->stop_func_start to an address at which a breakpoint may be
8355 legitimately placed.
8356
8357 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
8358 made, GDB will enter an infinite loop when stepping through
8359 optimized code consisting of VLIW instructions which contain
8360 subinstructions corresponding to different source lines. On
8361 FR-V, it's not permitted to place a breakpoint on any but the
8362 first subinstruction of a VLIW instruction. When a breakpoint is
8363 set, GDB will adjust the breakpoint address to the beginning of
8364 the VLIW instruction. Thus, we need to make the corresponding
8365 adjustment here when computing the stop address. */
8366
8367 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
8368 {
8369 ecs->stop_func_start
8370 = gdbarch_adjust_breakpoint_address (gdbarch,
8371 ecs->stop_func_start);
8372 }
8373
8374 if (ecs->stop_func_start == ecs->event_thread->stop_pc ())
8375 {
8376 /* We are already there: stop now. */
8377 end_stepping_range (ecs);
8378 return;
8379 }
8380 else
8381 {
8382 /* Put the step-breakpoint there and go until there. */
8383 symtab_and_line sr_sal;
8384 sr_sal.pc = ecs->stop_func_start;
8385 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
8386 sr_sal.pspace = get_frame_program_space (get_current_frame ());
8387
8388 /* Do not specify what the fp should be when we stop since on
8389 some machines the prologue is where the new fp value is
8390 established. */
8391 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal, null_frame_id);
8392
8393 /* And make sure stepping stops right away then. */
8394 ecs->event_thread->control.step_range_end
8395 = ecs->event_thread->control.step_range_start;
8396 }
8397 keep_going (ecs);
8398 }
8399
8400 /* Inferior has stepped backward into a subroutine call with source
8401 code that we should not step over. Do step to the beginning of the
8402 last line of code in it. */
8403
8404 static void
8405 handle_step_into_function_backward (struct gdbarch *gdbarch,
8406 struct execution_control_state *ecs)
8407 {
8408 struct compunit_symtab *cust;
8409 struct symtab_and_line stop_func_sal;
8410
8411 fill_in_stop_func (gdbarch, ecs);
8412
8413 cust = find_pc_compunit_symtab (ecs->event_thread->stop_pc ());
8414 if (cust != nullptr && cust->language () != language_asm)
8415 ecs->stop_func_start
8416 = gdbarch_skip_prologue_noexcept (gdbarch, ecs->stop_func_start);
8417
8418 stop_func_sal = find_pc_line (ecs->event_thread->stop_pc (), 0);
8419
8420 /* OK, we're just going to keep stepping here. */
8421 if (stop_func_sal.pc == ecs->event_thread->stop_pc ())
8422 {
8423 /* We're there already. Just stop stepping now. */
8424 end_stepping_range (ecs);
8425 }
8426 else
8427 {
8428 /* Else just reset the step range and keep going.
8429 No step-resume breakpoint, they don't work for
8430 epilogues, which can have multiple entry paths. */
8431 ecs->event_thread->control.step_range_start = stop_func_sal.pc;
8432 ecs->event_thread->control.step_range_end = stop_func_sal.end;
8433 keep_going (ecs);
8434 }
8435 return;
8436 }
8437
8438 /* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
8439 This is used to both functions and to skip over code. */
8440
8441 static void
8442 insert_step_resume_breakpoint_at_sal_1 (struct gdbarch *gdbarch,
8443 struct symtab_and_line sr_sal,
8444 struct frame_id sr_id,
8445 enum bptype sr_type)
8446 {
8447 /* There should never be more than one step-resume or longjmp-resume
8448 breakpoint per thread, so we should never be setting a new
8449 step_resume_breakpoint when one is already active. */
8450 gdb_assert (inferior_thread ()->control.step_resume_breakpoint == nullptr);
8451 gdb_assert (sr_type == bp_step_resume || sr_type == bp_hp_step_resume);
8452
8453 infrun_debug_printf ("inserting step-resume breakpoint at %s",
8454 paddress (gdbarch, sr_sal.pc));
8455
8456 inferior_thread ()->control.step_resume_breakpoint
8457 = set_momentary_breakpoint (gdbarch, sr_sal, sr_id, sr_type).release ();
8458 }
8459
8460 void
8461 insert_step_resume_breakpoint_at_sal (struct gdbarch *gdbarch,
8462 struct symtab_and_line sr_sal,
8463 struct frame_id sr_id)
8464 {
8465 insert_step_resume_breakpoint_at_sal_1 (gdbarch,
8466 sr_sal, sr_id,
8467 bp_step_resume);
8468 }
8469
8470 /* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
8471 This is used to skip a potential signal handler.
8472
8473 This is called with the interrupted function's frame. The signal
8474 handler, when it returns, will resume the interrupted function at
8475 RETURN_FRAME.pc. */
8476
8477 static void
8478 insert_hp_step_resume_breakpoint_at_frame (frame_info_ptr return_frame)
8479 {
8480 gdb_assert (return_frame != nullptr);
8481
8482 struct gdbarch *gdbarch = get_frame_arch (return_frame);
8483
8484 symtab_and_line sr_sal;
8485 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch, get_frame_pc (return_frame));
8486 sr_sal.section = find_pc_overlay (sr_sal.pc);
8487 sr_sal.pspace = get_frame_program_space (return_frame);
8488
8489 insert_step_resume_breakpoint_at_sal_1 (gdbarch, sr_sal,
8490 get_stack_frame_id (return_frame),
8491 bp_hp_step_resume);
8492 }
8493
8494 /* Insert a "step-resume breakpoint" at the previous frame's PC. This
8495 is used to skip a function after stepping into it (for "next" or if
8496 the called function has no debugging information).
8497
8498 The current function has almost always been reached by single
8499 stepping a call or return instruction. NEXT_FRAME belongs to the
8500 current function, and the breakpoint will be set at the caller's
8501 resume address.
8502
8503 This is a separate function rather than reusing
8504 insert_hp_step_resume_breakpoint_at_frame in order to avoid
8505 get_prev_frame, which may stop prematurely (see the implementation
8506 of frame_unwind_caller_id for an example). */
8507
8508 static void
8509 insert_step_resume_breakpoint_at_caller (frame_info_ptr next_frame)
8510 {
8511 /* We shouldn't have gotten here if we don't know where the call site
8512 is. */
8513 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame)));
8514
8515 struct gdbarch *gdbarch = frame_unwind_caller_arch (next_frame);
8516
8517 symtab_and_line sr_sal;
8518 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch,
8519 frame_unwind_caller_pc (next_frame));
8520 sr_sal.section = find_pc_overlay (sr_sal.pc);
8521 sr_sal.pspace = frame_unwind_program_space (next_frame);
8522
8523 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal,
8524 frame_unwind_caller_id (next_frame));
8525 }
8526
8527 /* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
8528 new breakpoint at the target of a jmp_buf. The handling of
8529 longjmp-resume uses the same mechanisms used for handling
8530 "step-resume" breakpoints. */
8531
8532 static void
8533 insert_longjmp_resume_breakpoint (struct gdbarch *gdbarch, CORE_ADDR pc)
8534 {
8535 /* There should never be more than one longjmp-resume breakpoint per
8536 thread, so we should never be setting a new
8537 longjmp_resume_breakpoint when one is already active. */
8538 gdb_assert (inferior_thread ()->control.exception_resume_breakpoint == nullptr);
8539
8540 infrun_debug_printf ("inserting longjmp-resume breakpoint at %s",
8541 paddress (gdbarch, pc));
8542
8543 inferior_thread ()->control.exception_resume_breakpoint =
8544 set_momentary_breakpoint_at_pc (gdbarch, pc, bp_longjmp_resume).release ();
8545 }
8546
8547 /* Insert an exception resume breakpoint. TP is the thread throwing
8548 the exception. The block B is the block of the unwinder debug hook
8549 function. FRAME is the frame corresponding to the call to this
8550 function. SYM is the symbol of the function argument holding the
8551 target PC of the exception. */
8552
8553 static void
8554 insert_exception_resume_breakpoint (struct thread_info *tp,
8555 const struct block *b,
8556 frame_info_ptr frame,
8557 struct symbol *sym)
8558 {
8559 try
8560 {
8561 struct block_symbol vsym;
8562 struct value *value;
8563 CORE_ADDR handler;
8564 struct breakpoint *bp;
8565
8566 vsym = lookup_symbol_search_name (sym->search_name (),
8567 b, VAR_DOMAIN);
8568 value = read_var_value (vsym.symbol, vsym.block, frame);
8569 /* If the value was optimized out, revert to the old behavior. */
8570 if (! value->optimized_out ())
8571 {
8572 handler = value_as_address (value);
8573
8574 infrun_debug_printf ("exception resume at %lx",
8575 (unsigned long) handler);
8576
8577 /* set_momentary_breakpoint_at_pc creates a thread-specific
8578 breakpoint for the current inferior thread. */
8579 gdb_assert (tp == inferior_thread ());
8580 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
8581 handler,
8582 bp_exception_resume).release ();
8583
8584 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
8585 frame = nullptr;
8586
8587 tp->control.exception_resume_breakpoint = bp;
8588 }
8589 }
8590 catch (const gdb_exception_error &e)
8591 {
8592 /* We want to ignore errors here. */
8593 }
8594 }
8595
8596 /* A helper for check_exception_resume that sets an
8597 exception-breakpoint based on a SystemTap probe. */
8598
8599 static void
8600 insert_exception_resume_from_probe (struct thread_info *tp,
8601 const struct bound_probe *probe,
8602 frame_info_ptr frame)
8603 {
8604 struct value *arg_value;
8605 CORE_ADDR handler;
8606 struct breakpoint *bp;
8607
8608 arg_value = probe_safe_evaluate_at_pc (frame, 1);
8609 if (!arg_value)
8610 return;
8611
8612 handler = value_as_address (arg_value);
8613
8614 infrun_debug_printf ("exception resume at %s",
8615 paddress (probe->objfile->arch (), handler));
8616
8617 /* set_momentary_breakpoint_at_pc creates a thread-specific breakpoint
8618 for the current inferior thread. */
8619 gdb_assert (tp == inferior_thread ());
8620 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
8621 handler, bp_exception_resume).release ();
8622 tp->control.exception_resume_breakpoint = bp;
8623 }
8624
8625 /* This is called when an exception has been intercepted. Check to
8626 see whether the exception's destination is of interest, and if so,
8627 set an exception resume breakpoint there. */
8628
8629 static void
8630 check_exception_resume (struct execution_control_state *ecs,
8631 frame_info_ptr frame)
8632 {
8633 struct bound_probe probe;
8634 struct symbol *func;
8635
8636 /* First see if this exception unwinding breakpoint was set via a
8637 SystemTap probe point. If so, the probe has two arguments: the
8638 CFA and the HANDLER. We ignore the CFA, extract the handler, and
8639 set a breakpoint there. */
8640 probe = find_probe_by_pc (get_frame_pc (frame));
8641 if (probe.prob)
8642 {
8643 insert_exception_resume_from_probe (ecs->event_thread, &probe, frame);
8644 return;
8645 }
8646
8647 func = get_frame_function (frame);
8648 if (!func)
8649 return;
8650
8651 try
8652 {
8653 const struct block *b;
8654 int argno = 0;
8655
8656 /* The exception breakpoint is a thread-specific breakpoint on
8657 the unwinder's debug hook, declared as:
8658
8659 void _Unwind_DebugHook (void *cfa, void *handler);
8660
8661 The CFA argument indicates the frame to which control is
8662 about to be transferred. HANDLER is the destination PC.
8663
8664 We ignore the CFA and set a temporary breakpoint at HANDLER.
8665 This is not extremely efficient but it avoids issues in gdb
8666 with computing the DWARF CFA, and it also works even in weird
8667 cases such as throwing an exception from inside a signal
8668 handler. */
8669
8670 b = func->value_block ();
8671 for (struct symbol *sym : block_iterator_range (b))
8672 {
8673 if (!sym->is_argument ())
8674 continue;
8675
8676 if (argno == 0)
8677 ++argno;
8678 else
8679 {
8680 insert_exception_resume_breakpoint (ecs->event_thread,
8681 b, frame, sym);
8682 break;
8683 }
8684 }
8685 }
8686 catch (const gdb_exception_error &e)
8687 {
8688 }
8689 }
8690
8691 static void
8692 stop_waiting (struct execution_control_state *ecs)
8693 {
8694 infrun_debug_printf ("stop_waiting");
8695
8696 /* Let callers know we don't want to wait for the inferior anymore. */
8697 ecs->wait_some_more = 0;
8698 }
8699
8700 /* Like keep_going, but passes the signal to the inferior, even if the
8701 signal is set to nopass. */
8702
8703 static void
8704 keep_going_pass_signal (struct execution_control_state *ecs)
8705 {
8706 gdb_assert (ecs->event_thread->ptid == inferior_ptid);
8707 gdb_assert (!ecs->event_thread->resumed ());
8708
8709 /* Save the pc before execution, to compare with pc after stop. */
8710 ecs->event_thread->prev_pc
8711 = regcache_read_pc_protected (get_thread_regcache (ecs->event_thread));
8712
8713 if (ecs->event_thread->control.trap_expected)
8714 {
8715 struct thread_info *tp = ecs->event_thread;
8716
8717 infrun_debug_printf ("%s has trap_expected set, "
8718 "resuming to collect trap",
8719 tp->ptid.to_string ().c_str ());
8720
8721 /* We haven't yet gotten our trap, and either: intercepted a
8722 non-signal event (e.g., a fork); or took a signal which we
8723 are supposed to pass through to the inferior. Simply
8724 continue. */
8725 resume (ecs->event_thread->stop_signal ());
8726 }
8727 else if (step_over_info_valid_p ())
8728 {
8729 /* Another thread is stepping over a breakpoint in-line. If
8730 this thread needs a step-over too, queue the request. In
8731 either case, this resume must be deferred for later. */
8732 struct thread_info *tp = ecs->event_thread;
8733
8734 if (ecs->hit_singlestep_breakpoint
8735 || thread_still_needs_step_over (tp))
8736 {
8737 infrun_debug_printf ("step-over already in progress: "
8738 "step-over for %s deferred",
8739 tp->ptid.to_string ().c_str ());
8740 global_thread_step_over_chain_enqueue (tp);
8741 }
8742 else
8743 infrun_debug_printf ("step-over in progress: resume of %s deferred",
8744 tp->ptid.to_string ().c_str ());
8745 }
8746 else
8747 {
8748 struct regcache *regcache = get_current_regcache ();
8749 int remove_bp;
8750 int remove_wps;
8751 step_over_what step_what;
8752
8753 /* Either the trap was not expected, but we are continuing
8754 anyway (if we got a signal, the user asked it be passed to
8755 the child)
8756 -- or --
8757 We got our expected trap, but decided we should resume from
8758 it.
8759
8760 We're going to run this baby now!
8761
8762 Note that insert_breakpoints won't try to re-insert
8763 already inserted breakpoints. Therefore, we don't
8764 care if breakpoints were already inserted, or not. */
8765
8766 /* If we need to step over a breakpoint, and we're not using
8767 displaced stepping to do so, insert all breakpoints
8768 (watchpoints, etc.) but the one we're stepping over, step one
8769 instruction, and then re-insert the breakpoint when that step
8770 is finished. */
8771
8772 step_what = thread_still_needs_step_over (ecs->event_thread);
8773
8774 remove_bp = (ecs->hit_singlestep_breakpoint
8775 || (step_what & STEP_OVER_BREAKPOINT));
8776 remove_wps = (step_what & STEP_OVER_WATCHPOINT);
8777
8778 /* We can't use displaced stepping if we need to step past a
8779 watchpoint. The instruction copied to the scratch pad would
8780 still trigger the watchpoint. */
8781 if (remove_bp
8782 && (remove_wps || !use_displaced_stepping (ecs->event_thread)))
8783 {
8784 set_step_over_info (regcache->aspace (),
8785 regcache_read_pc (regcache), remove_wps,
8786 ecs->event_thread->global_num);
8787 }
8788 else if (remove_wps)
8789 set_step_over_info (nullptr, 0, remove_wps, -1);
8790
8791 /* If we now need to do an in-line step-over, we need to stop
8792 all other threads. Note this must be done before
8793 insert_breakpoints below, because that removes the breakpoint
8794 we're about to step over, otherwise other threads could miss
8795 it. */
8796 if (step_over_info_valid_p () && target_is_non_stop_p ())
8797 stop_all_threads ("starting in-line step-over");
8798
8799 /* Stop stepping if inserting breakpoints fails. */
8800 try
8801 {
8802 insert_breakpoints ();
8803 }
8804 catch (const gdb_exception_error &e)
8805 {
8806 exception_print (gdb_stderr, e);
8807 stop_waiting (ecs);
8808 clear_step_over_info ();
8809 return;
8810 }
8811
8812 ecs->event_thread->control.trap_expected = (remove_bp || remove_wps);
8813
8814 resume (ecs->event_thread->stop_signal ());
8815 }
8816
8817 prepare_to_wait (ecs);
8818 }
8819
8820 /* Called when we should continue running the inferior, because the
8821 current event doesn't cause a user visible stop. This does the
8822 resuming part; waiting for the next event is done elsewhere. */
8823
8824 static void
8825 keep_going (struct execution_control_state *ecs)
8826 {
8827 if (ecs->event_thread->control.trap_expected
8828 && ecs->event_thread->stop_signal () == GDB_SIGNAL_TRAP)
8829 ecs->event_thread->control.trap_expected = 0;
8830
8831 if (!signal_program[ecs->event_thread->stop_signal ()])
8832 ecs->event_thread->set_stop_signal (GDB_SIGNAL_0);
8833 keep_going_pass_signal (ecs);
8834 }
8835
8836 /* This function normally comes after a resume, before
8837 handle_inferior_event exits. It takes care of any last bits of
8838 housekeeping, and sets the all-important wait_some_more flag. */
8839
8840 static void
8841 prepare_to_wait (struct execution_control_state *ecs)
8842 {
8843 infrun_debug_printf ("prepare_to_wait");
8844
8845 ecs->wait_some_more = 1;
8846
8847 /* If the target can't async, emulate it by marking the infrun event
8848 handler such that as soon as we get back to the event-loop, we
8849 immediately end up in fetch_inferior_event again calling
8850 target_wait. */
8851 if (!target_can_async_p ())
8852 mark_infrun_async_event_handler ();
8853 }
8854
8855 /* We are done with the step range of a step/next/si/ni command.
8856 Called once for each n of a "step n" operation. */
8857
8858 static void
8859 end_stepping_range (struct execution_control_state *ecs)
8860 {
8861 ecs->event_thread->control.stop_step = 1;
8862 stop_waiting (ecs);
8863 }
8864
8865 /* Several print_*_reason functions to print why the inferior has stopped.
8866 We always print something when the inferior exits, or receives a signal.
8867 The rest of the cases are dealt with later on in normal_stop and
8868 print_it_typical. Ideally there should be a call to one of these
8869 print_*_reason functions functions from handle_inferior_event each time
8870 stop_waiting is called.
8871
8872 Note that we don't call these directly, instead we delegate that to
8873 the interpreters, through observers. Interpreters then call these
8874 with whatever uiout is right. */
8875
8876 void
8877 print_signal_exited_reason (struct ui_out *uiout, enum gdb_signal siggnal)
8878 {
8879 annotate_signalled ();
8880 if (uiout->is_mi_like_p ())
8881 uiout->field_string
8882 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
8883 uiout->text ("\nProgram terminated with signal ");
8884 annotate_signal_name ();
8885 uiout->field_string ("signal-name",
8886 gdb_signal_to_name (siggnal));
8887 annotate_signal_name_end ();
8888 uiout->text (", ");
8889 annotate_signal_string ();
8890 uiout->field_string ("signal-meaning",
8891 gdb_signal_to_string (siggnal));
8892 annotate_signal_string_end ();
8893 uiout->text (".\n");
8894 uiout->text ("The program no longer exists.\n");
8895 }
8896
8897 void
8898 print_exited_reason (struct ui_out *uiout, int exitstatus)
8899 {
8900 struct inferior *inf = current_inferior ();
8901 std::string pidstr = target_pid_to_str (ptid_t (inf->pid));
8902
8903 annotate_exited (exitstatus);
8904 if (exitstatus)
8905 {
8906 if (uiout->is_mi_like_p ())
8907 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXITED));
8908 std::string exit_code_str
8909 = string_printf ("0%o", (unsigned int) exitstatus);
8910 uiout->message ("[Inferior %s (%s) exited with code %pF]\n",
8911 plongest (inf->num), pidstr.c_str (),
8912 string_field ("exit-code", exit_code_str.c_str ()));
8913 }
8914 else
8915 {
8916 if (uiout->is_mi_like_p ())
8917 uiout->field_string
8918 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
8919 uiout->message ("[Inferior %s (%s) exited normally]\n",
8920 plongest (inf->num), pidstr.c_str ());
8921 }
8922 }
8923
8924 void
8925 print_signal_received_reason (struct ui_out *uiout, enum gdb_signal siggnal)
8926 {
8927 struct thread_info *thr = inferior_thread ();
8928
8929 infrun_debug_printf ("signal = %s", gdb_signal_to_string (siggnal));
8930
8931 annotate_signal ();
8932
8933 if (uiout->is_mi_like_p ())
8934 ;
8935 else if (show_thread_that_caused_stop ())
8936 {
8937 uiout->text ("\nThread ");
8938 uiout->field_string ("thread-id", print_thread_id (thr));
8939
8940 const char *name = thread_name (thr);
8941 if (name != nullptr)
8942 {
8943 uiout->text (" \"");
8944 uiout->field_string ("name", name);
8945 uiout->text ("\"");
8946 }
8947 }
8948 else
8949 uiout->text ("\nProgram");
8950
8951 if (siggnal == GDB_SIGNAL_0 && !uiout->is_mi_like_p ())
8952 uiout->text (" stopped");
8953 else
8954 {
8955 uiout->text (" received signal ");
8956 annotate_signal_name ();
8957 if (uiout->is_mi_like_p ())
8958 uiout->field_string
8959 ("reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
8960 uiout->field_string ("signal-name", gdb_signal_to_name (siggnal));
8961 annotate_signal_name_end ();
8962 uiout->text (", ");
8963 annotate_signal_string ();
8964 uiout->field_string ("signal-meaning", gdb_signal_to_string (siggnal));
8965
8966 struct regcache *regcache = get_current_regcache ();
8967 struct gdbarch *gdbarch = regcache->arch ();
8968 if (gdbarch_report_signal_info_p (gdbarch))
8969 gdbarch_report_signal_info (gdbarch, uiout, siggnal);
8970
8971 annotate_signal_string_end ();
8972 }
8973 uiout->text (".\n");
8974 }
8975
8976 void
8977 print_no_history_reason (struct ui_out *uiout)
8978 {
8979 if (uiout->is_mi_like_p ())
8980 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_NO_HISTORY));
8981 else
8982 uiout->text ("\nNo more reverse-execution history.\n");
8983 }
8984
8985 /* Print current location without a level number, if we have changed
8986 functions or hit a breakpoint. Print source line if we have one.
8987 bpstat_print contains the logic deciding in detail what to print,
8988 based on the event(s) that just occurred. */
8989
8990 static void
8991 print_stop_location (const target_waitstatus &ws)
8992 {
8993 int bpstat_ret;
8994 enum print_what source_flag;
8995 int do_frame_printing = 1;
8996 struct thread_info *tp = inferior_thread ();
8997
8998 bpstat_ret = bpstat_print (tp->control.stop_bpstat, ws.kind ());
8999 switch (bpstat_ret)
9000 {
9001 case PRINT_UNKNOWN:
9002 /* FIXME: cagney/2002-12-01: Given that a frame ID does (or
9003 should) carry around the function and does (or should) use
9004 that when doing a frame comparison. */
9005 if (tp->control.stop_step
9006 && (tp->control.step_frame_id
9007 == get_frame_id (get_current_frame ()))
9008 && (tp->control.step_start_function
9009 == find_pc_function (tp->stop_pc ())))
9010 {
9011 /* Finished step, just print source line. */
9012 source_flag = SRC_LINE;
9013 }
9014 else
9015 {
9016 /* Print location and source line. */
9017 source_flag = SRC_AND_LOC;
9018 }
9019 break;
9020 case PRINT_SRC_AND_LOC:
9021 /* Print location and source line. */
9022 source_flag = SRC_AND_LOC;
9023 break;
9024 case PRINT_SRC_ONLY:
9025 source_flag = SRC_LINE;
9026 break;
9027 case PRINT_NOTHING:
9028 /* Something bogus. */
9029 source_flag = SRC_LINE;
9030 do_frame_printing = 0;
9031 break;
9032 default:
9033 internal_error (_("Unknown value."));
9034 }
9035
9036 /* The behavior of this routine with respect to the source
9037 flag is:
9038 SRC_LINE: Print only source line
9039 LOCATION: Print only location
9040 SRC_AND_LOC: Print location and source line. */
9041 if (do_frame_printing)
9042 print_stack_frame (get_selected_frame (nullptr), 0, source_flag, 1);
9043 }
9044
9045 /* See infrun.h. */
9046
9047 void
9048 print_stop_event (struct ui_out *uiout, bool displays)
9049 {
9050 struct target_waitstatus last;
9051 struct thread_info *tp;
9052
9053 get_last_target_status (nullptr, nullptr, &last);
9054
9055 {
9056 scoped_restore save_uiout = make_scoped_restore (&current_uiout, uiout);
9057
9058 print_stop_location (last);
9059
9060 /* Display the auto-display expressions. */
9061 if (displays)
9062 do_displays ();
9063 }
9064
9065 tp = inferior_thread ();
9066 if (tp->thread_fsm () != nullptr
9067 && tp->thread_fsm ()->finished_p ())
9068 {
9069 struct return_value_info *rv;
9070
9071 rv = tp->thread_fsm ()->return_value ();
9072 if (rv != nullptr)
9073 print_return_value (uiout, rv);
9074 }
9075 }
9076
9077 /* See infrun.h. */
9078
9079 void
9080 maybe_remove_breakpoints (void)
9081 {
9082 if (!breakpoints_should_be_inserted_now () && target_has_execution ())
9083 {
9084 if (remove_breakpoints ())
9085 {
9086 target_terminal::ours_for_output ();
9087 gdb_printf (_("Cannot remove breakpoints because "
9088 "program is no longer writable.\nFurther "
9089 "execution is probably impossible.\n"));
9090 }
9091 }
9092 }
9093
9094 /* The execution context that just caused a normal stop. */
9095
9096 struct stop_context
9097 {
9098 stop_context ();
9099
9100 DISABLE_COPY_AND_ASSIGN (stop_context);
9101
9102 bool changed () const;
9103
9104 /* The stop ID. */
9105 ULONGEST stop_id;
9106
9107 /* The event PTID. */
9108
9109 ptid_t ptid;
9110
9111 /* If stopp for a thread event, this is the thread that caused the
9112 stop. */
9113 thread_info_ref thread;
9114
9115 /* The inferior that caused the stop. */
9116 int inf_num;
9117 };
9118
9119 /* Initializes a new stop context. If stopped for a thread event, this
9120 takes a strong reference to the thread. */
9121
9122 stop_context::stop_context ()
9123 {
9124 stop_id = get_stop_id ();
9125 ptid = inferior_ptid;
9126 inf_num = current_inferior ()->num;
9127
9128 if (inferior_ptid != null_ptid)
9129 {
9130 /* Take a strong reference so that the thread can't be deleted
9131 yet. */
9132 thread = thread_info_ref::new_reference (inferior_thread ());
9133 }
9134 }
9135
9136 /* Return true if the current context no longer matches the saved stop
9137 context. */
9138
9139 bool
9140 stop_context::changed () const
9141 {
9142 if (ptid != inferior_ptid)
9143 return true;
9144 if (inf_num != current_inferior ()->num)
9145 return true;
9146 if (thread != nullptr && thread->state != THREAD_STOPPED)
9147 return true;
9148 if (get_stop_id () != stop_id)
9149 return true;
9150 return false;
9151 }
9152
9153 /* See infrun.h. */
9154
9155 bool
9156 normal_stop ()
9157 {
9158 struct target_waitstatus last;
9159
9160 get_last_target_status (nullptr, nullptr, &last);
9161
9162 new_stop_id ();
9163
9164 /* If an exception is thrown from this point on, make sure to
9165 propagate GDB's knowledge of the executing state to the
9166 frontend/user running state. A QUIT is an easy exception to see
9167 here, so do this before any filtered output. */
9168
9169 ptid_t finish_ptid = null_ptid;
9170
9171 if (!non_stop)
9172 finish_ptid = minus_one_ptid;
9173 else if (last.kind () == TARGET_WAITKIND_SIGNALLED
9174 || last.kind () == TARGET_WAITKIND_EXITED)
9175 {
9176 /* On some targets, we may still have live threads in the
9177 inferior when we get a process exit event. E.g., for
9178 "checkpoint", when the current checkpoint/fork exits,
9179 linux-fork.c automatically switches to another fork from
9180 within target_mourn_inferior. */
9181 if (inferior_ptid != null_ptid)
9182 finish_ptid = ptid_t (inferior_ptid.pid ());
9183 }
9184 else if (last.kind () != TARGET_WAITKIND_NO_RESUMED)
9185 finish_ptid = inferior_ptid;
9186
9187 gdb::optional<scoped_finish_thread_state> maybe_finish_thread_state;
9188 if (finish_ptid != null_ptid)
9189 {
9190 maybe_finish_thread_state.emplace
9191 (user_visible_resume_target (finish_ptid), finish_ptid);
9192 }
9193
9194 /* As we're presenting a stop, and potentially removing breakpoints,
9195 update the thread list so we can tell whether there are threads
9196 running on the target. With target remote, for example, we can
9197 only learn about new threads when we explicitly update the thread
9198 list. Do this before notifying the interpreters about signal
9199 stops, end of stepping ranges, etc., so that the "new thread"
9200 output is emitted before e.g., "Program received signal FOO",
9201 instead of after. */
9202 update_thread_list ();
9203
9204 if (last.kind () == TARGET_WAITKIND_STOPPED && stopped_by_random_signal)
9205 notify_signal_received (inferior_thread ()->stop_signal ());
9206
9207 /* As with the notification of thread events, we want to delay
9208 notifying the user that we've switched thread context until
9209 the inferior actually stops.
9210
9211 There's no point in saying anything if the inferior has exited.
9212 Note that SIGNALLED here means "exited with a signal", not
9213 "received a signal".
9214
9215 Also skip saying anything in non-stop mode. In that mode, as we
9216 don't want GDB to switch threads behind the user's back, to avoid
9217 races where the user is typing a command to apply to thread x,
9218 but GDB switches to thread y before the user finishes entering
9219 the command, fetch_inferior_event installs a cleanup to restore
9220 the current thread back to the thread the user had selected right
9221 after this event is handled, so we're not really switching, only
9222 informing of a stop. */
9223 if (!non_stop)
9224 {
9225 if ((last.kind () != TARGET_WAITKIND_SIGNALLED
9226 && last.kind () != TARGET_WAITKIND_EXITED
9227 && last.kind () != TARGET_WAITKIND_NO_RESUMED)
9228 && target_has_execution ()
9229 && previous_thread != inferior_thread ())
9230 {
9231 SWITCH_THRU_ALL_UIS ()
9232 {
9233 target_terminal::ours_for_output ();
9234 gdb_printf (_("[Switching to %s]\n"),
9235 target_pid_to_str (inferior_ptid).c_str ());
9236 annotate_thread_changed ();
9237 }
9238 }
9239
9240 update_previous_thread ();
9241 }
9242
9243 if (last.kind () == TARGET_WAITKIND_NO_RESUMED)
9244 {
9245 stop_print_frame = false;
9246
9247 SWITCH_THRU_ALL_UIS ()
9248 if (current_ui->prompt_state == PROMPT_BLOCKED)
9249 {
9250 target_terminal::ours_for_output ();
9251 gdb_printf (_("No unwaited-for children left.\n"));
9252 }
9253 }
9254
9255 /* Note: this depends on the update_thread_list call above. */
9256 maybe_remove_breakpoints ();
9257
9258 /* If an auto-display called a function and that got a signal,
9259 delete that auto-display to avoid an infinite recursion. */
9260
9261 if (stopped_by_random_signal)
9262 disable_current_display ();
9263
9264 SWITCH_THRU_ALL_UIS ()
9265 {
9266 async_enable_stdin ();
9267 }
9268
9269 /* Let the user/frontend see the threads as stopped. */
9270 maybe_finish_thread_state.reset ();
9271
9272 /* Select innermost stack frame - i.e., current frame is frame 0,
9273 and current location is based on that. Handle the case where the
9274 dummy call is returning after being stopped. E.g. the dummy call
9275 previously hit a breakpoint. (If the dummy call returns
9276 normally, we won't reach here.) Do this before the stop hook is
9277 run, so that it doesn't get to see the temporary dummy frame,
9278 which is not where we'll present the stop. */
9279 if (has_stack_frames ())
9280 {
9281 if (stop_stack_dummy == STOP_STACK_DUMMY)
9282 {
9283 /* Pop the empty frame that contains the stack dummy. This
9284 also restores inferior state prior to the call (struct
9285 infcall_suspend_state). */
9286 frame_info_ptr frame = get_current_frame ();
9287
9288 gdb_assert (get_frame_type (frame) == DUMMY_FRAME);
9289 frame_pop (frame);
9290 /* frame_pop calls reinit_frame_cache as the last thing it
9291 does which means there's now no selected frame. */
9292 }
9293
9294 select_frame (get_current_frame ());
9295
9296 /* Set the current source location. */
9297 set_current_sal_from_frame (get_current_frame ());
9298 }
9299
9300 /* Look up the hook_stop and run it (CLI internally handles problem
9301 of stop_command's pre-hook not existing). */
9302 stop_context saved_context;
9303
9304 try
9305 {
9306 execute_cmd_pre_hook (stop_command);
9307 }
9308 catch (const gdb_exception_error &ex)
9309 {
9310 exception_fprintf (gdb_stderr, ex,
9311 "Error while running hook_stop:\n");
9312 }
9313
9314 /* If the stop hook resumes the target, then there's no point in
9315 trying to notify about the previous stop; its context is
9316 gone. Likewise if the command switches thread or inferior --
9317 the observers would print a stop for the wrong
9318 thread/inferior. */
9319 if (saved_context.changed ())
9320 return true;
9321
9322 /* Notify observers about the stop. This is where the interpreters
9323 print the stop event. */
9324 notify_normal_stop ((inferior_ptid != null_ptid
9325 ? inferior_thread ()->control.stop_bpstat
9326 : nullptr),
9327 stop_print_frame);
9328 annotate_stopped ();
9329
9330 if (target_has_execution ())
9331 {
9332 if (last.kind () != TARGET_WAITKIND_SIGNALLED
9333 && last.kind () != TARGET_WAITKIND_EXITED
9334 && last.kind () != TARGET_WAITKIND_NO_RESUMED)
9335 /* Delete the breakpoint we stopped at, if it wants to be deleted.
9336 Delete any breakpoint that is to be deleted at the next stop. */
9337 breakpoint_auto_delete (inferior_thread ()->control.stop_bpstat);
9338 }
9339
9340 return false;
9341 }
9342 \f
9343 int
9344 signal_stop_state (int signo)
9345 {
9346 return signal_stop[signo];
9347 }
9348
9349 int
9350 signal_print_state (int signo)
9351 {
9352 return signal_print[signo];
9353 }
9354
9355 int
9356 signal_pass_state (int signo)
9357 {
9358 return signal_program[signo];
9359 }
9360
9361 static void
9362 signal_cache_update (int signo)
9363 {
9364 if (signo == -1)
9365 {
9366 for (signo = 0; signo < (int) GDB_SIGNAL_LAST; signo++)
9367 signal_cache_update (signo);
9368
9369 return;
9370 }
9371
9372 signal_pass[signo] = (signal_stop[signo] == 0
9373 && signal_print[signo] == 0
9374 && signal_program[signo] == 1
9375 && signal_catch[signo] == 0);
9376 }
9377
9378 int
9379 signal_stop_update (int signo, int state)
9380 {
9381 int ret = signal_stop[signo];
9382
9383 signal_stop[signo] = state;
9384 signal_cache_update (signo);
9385 return ret;
9386 }
9387
9388 int
9389 signal_print_update (int signo, int state)
9390 {
9391 int ret = signal_print[signo];
9392
9393 signal_print[signo] = state;
9394 signal_cache_update (signo);
9395 return ret;
9396 }
9397
9398 int
9399 signal_pass_update (int signo, int state)
9400 {
9401 int ret = signal_program[signo];
9402
9403 signal_program[signo] = state;
9404 signal_cache_update (signo);
9405 return ret;
9406 }
9407
9408 /* Update the global 'signal_catch' from INFO and notify the
9409 target. */
9410
9411 void
9412 signal_catch_update (const unsigned int *info)
9413 {
9414 int i;
9415
9416 for (i = 0; i < GDB_SIGNAL_LAST; ++i)
9417 signal_catch[i] = info[i] > 0;
9418 signal_cache_update (-1);
9419 target_pass_signals (signal_pass);
9420 }
9421
9422 static void
9423 sig_print_header (void)
9424 {
9425 gdb_printf (_("Signal Stop\tPrint\tPass "
9426 "to program\tDescription\n"));
9427 }
9428
9429 static void
9430 sig_print_info (enum gdb_signal oursig)
9431 {
9432 const char *name = gdb_signal_to_name (oursig);
9433 int name_padding = 13 - strlen (name);
9434
9435 if (name_padding <= 0)
9436 name_padding = 0;
9437
9438 gdb_printf ("%s", name);
9439 gdb_printf ("%*.*s ", name_padding, name_padding, " ");
9440 gdb_printf ("%s\t", signal_stop[oursig] ? "Yes" : "No");
9441 gdb_printf ("%s\t", signal_print[oursig] ? "Yes" : "No");
9442 gdb_printf ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
9443 gdb_printf ("%s\n", gdb_signal_to_string (oursig));
9444 }
9445
9446 /* Specify how various signals in the inferior should be handled. */
9447
9448 static void
9449 handle_command (const char *args, int from_tty)
9450 {
9451 int digits, wordlen;
9452 int sigfirst, siglast;
9453 enum gdb_signal oursig;
9454 int allsigs;
9455
9456 if (args == nullptr)
9457 {
9458 error_no_arg (_("signal to handle"));
9459 }
9460
9461 /* Allocate and zero an array of flags for which signals to handle. */
9462
9463 const size_t nsigs = GDB_SIGNAL_LAST;
9464 unsigned char sigs[nsigs] {};
9465
9466 /* Break the command line up into args. */
9467
9468 gdb_argv built_argv (args);
9469
9470 /* Walk through the args, looking for signal oursigs, signal names, and
9471 actions. Signal numbers and signal names may be interspersed with
9472 actions, with the actions being performed for all signals cumulatively
9473 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
9474
9475 for (char *arg : built_argv)
9476 {
9477 wordlen = strlen (arg);
9478 for (digits = 0; isdigit (arg[digits]); digits++)
9479 {;
9480 }
9481 allsigs = 0;
9482 sigfirst = siglast = -1;
9483
9484 if (wordlen >= 1 && !strncmp (arg, "all", wordlen))
9485 {
9486 /* Apply action to all signals except those used by the
9487 debugger. Silently skip those. */
9488 allsigs = 1;
9489 sigfirst = 0;
9490 siglast = nsigs - 1;
9491 }
9492 else if (wordlen >= 1 && !strncmp (arg, "stop", wordlen))
9493 {
9494 SET_SIGS (nsigs, sigs, signal_stop);
9495 SET_SIGS (nsigs, sigs, signal_print);
9496 }
9497 else if (wordlen >= 1 && !strncmp (arg, "ignore", wordlen))
9498 {
9499 UNSET_SIGS (nsigs, sigs, signal_program);
9500 }
9501 else if (wordlen >= 2 && !strncmp (arg, "print", wordlen))
9502 {
9503 SET_SIGS (nsigs, sigs, signal_print);
9504 }
9505 else if (wordlen >= 2 && !strncmp (arg, "pass", wordlen))
9506 {
9507 SET_SIGS (nsigs, sigs, signal_program);
9508 }
9509 else if (wordlen >= 3 && !strncmp (arg, "nostop", wordlen))
9510 {
9511 UNSET_SIGS (nsigs, sigs, signal_stop);
9512 }
9513 else if (wordlen >= 3 && !strncmp (arg, "noignore", wordlen))
9514 {
9515 SET_SIGS (nsigs, sigs, signal_program);
9516 }
9517 else if (wordlen >= 4 && !strncmp (arg, "noprint", wordlen))
9518 {
9519 UNSET_SIGS (nsigs, sigs, signal_print);
9520 UNSET_SIGS (nsigs, sigs, signal_stop);
9521 }
9522 else if (wordlen >= 4 && !strncmp (arg, "nopass", wordlen))
9523 {
9524 UNSET_SIGS (nsigs, sigs, signal_program);
9525 }
9526 else if (digits > 0)
9527 {
9528 /* It is numeric. The numeric signal refers to our own
9529 internal signal numbering from target.h, not to host/target
9530 signal number. This is a feature; users really should be
9531 using symbolic names anyway, and the common ones like
9532 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
9533
9534 sigfirst = siglast = (int)
9535 gdb_signal_from_command (atoi (arg));
9536 if (arg[digits] == '-')
9537 {
9538 siglast = (int)
9539 gdb_signal_from_command (atoi (arg + digits + 1));
9540 }
9541 if (sigfirst > siglast)
9542 {
9543 /* Bet he didn't figure we'd think of this case... */
9544 std::swap (sigfirst, siglast);
9545 }
9546 }
9547 else
9548 {
9549 oursig = gdb_signal_from_name (arg);
9550 if (oursig != GDB_SIGNAL_UNKNOWN)
9551 {
9552 sigfirst = siglast = (int) oursig;
9553 }
9554 else
9555 {
9556 /* Not a number and not a recognized flag word => complain. */
9557 error (_("Unrecognized or ambiguous flag word: \"%s\"."), arg);
9558 }
9559 }
9560
9561 /* If any signal numbers or symbol names were found, set flags for
9562 which signals to apply actions to. */
9563
9564 for (int signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
9565 {
9566 switch ((enum gdb_signal) signum)
9567 {
9568 case GDB_SIGNAL_TRAP:
9569 case GDB_SIGNAL_INT:
9570 if (!allsigs && !sigs[signum])
9571 {
9572 if (query (_("%s is used by the debugger.\n\
9573 Are you sure you want to change it? "),
9574 gdb_signal_to_name ((enum gdb_signal) signum)))
9575 {
9576 sigs[signum] = 1;
9577 }
9578 else
9579 gdb_printf (_("Not confirmed, unchanged.\n"));
9580 }
9581 break;
9582 case GDB_SIGNAL_0:
9583 case GDB_SIGNAL_DEFAULT:
9584 case GDB_SIGNAL_UNKNOWN:
9585 /* Make sure that "all" doesn't print these. */
9586 break;
9587 default:
9588 sigs[signum] = 1;
9589 break;
9590 }
9591 }
9592 }
9593
9594 for (int signum = 0; signum < nsigs; signum++)
9595 if (sigs[signum])
9596 {
9597 signal_cache_update (-1);
9598 target_pass_signals (signal_pass);
9599 target_program_signals (signal_program);
9600
9601 if (from_tty)
9602 {
9603 /* Show the results. */
9604 sig_print_header ();
9605 for (; signum < nsigs; signum++)
9606 if (sigs[signum])
9607 sig_print_info ((enum gdb_signal) signum);
9608 }
9609
9610 break;
9611 }
9612 }
9613
9614 /* Complete the "handle" command. */
9615
9616 static void
9617 handle_completer (struct cmd_list_element *ignore,
9618 completion_tracker &tracker,
9619 const char *text, const char *word)
9620 {
9621 static const char * const keywords[] =
9622 {
9623 "all",
9624 "stop",
9625 "ignore",
9626 "print",
9627 "pass",
9628 "nostop",
9629 "noignore",
9630 "noprint",
9631 "nopass",
9632 nullptr,
9633 };
9634
9635 signal_completer (ignore, tracker, text, word);
9636 complete_on_enum (tracker, keywords, word, word);
9637 }
9638
9639 enum gdb_signal
9640 gdb_signal_from_command (int num)
9641 {
9642 if (num >= 1 && num <= 15)
9643 return (enum gdb_signal) num;
9644 error (_("Only signals 1-15 are valid as numeric signals.\n\
9645 Use \"info signals\" for a list of symbolic signals."));
9646 }
9647
9648 /* Print current contents of the tables set by the handle command.
9649 It is possible we should just be printing signals actually used
9650 by the current target (but for things to work right when switching
9651 targets, all signals should be in the signal tables). */
9652
9653 static void
9654 info_signals_command (const char *signum_exp, int from_tty)
9655 {
9656 enum gdb_signal oursig;
9657
9658 sig_print_header ();
9659
9660 if (signum_exp)
9661 {
9662 /* First see if this is a symbol name. */
9663 oursig = gdb_signal_from_name (signum_exp);
9664 if (oursig == GDB_SIGNAL_UNKNOWN)
9665 {
9666 /* No, try numeric. */
9667 oursig =
9668 gdb_signal_from_command (parse_and_eval_long (signum_exp));
9669 }
9670 sig_print_info (oursig);
9671 return;
9672 }
9673
9674 gdb_printf ("\n");
9675 /* These ugly casts brought to you by the native VAX compiler. */
9676 for (oursig = GDB_SIGNAL_FIRST;
9677 (int) oursig < (int) GDB_SIGNAL_LAST;
9678 oursig = (enum gdb_signal) ((int) oursig + 1))
9679 {
9680 QUIT;
9681
9682 if (oursig != GDB_SIGNAL_UNKNOWN
9683 && oursig != GDB_SIGNAL_DEFAULT && oursig != GDB_SIGNAL_0)
9684 sig_print_info (oursig);
9685 }
9686
9687 gdb_printf (_("\nUse the \"handle\" command "
9688 "to change these tables.\n"));
9689 }
9690
9691 /* The $_siginfo convenience variable is a bit special. We don't know
9692 for sure the type of the value until we actually have a chance to
9693 fetch the data. The type can change depending on gdbarch, so it is
9694 also dependent on which thread you have selected.
9695
9696 1. making $_siginfo be an internalvar that creates a new value on
9697 access.
9698
9699 2. making the value of $_siginfo be an lval_computed value. */
9700
9701 /* This function implements the lval_computed support for reading a
9702 $_siginfo value. */
9703
9704 static void
9705 siginfo_value_read (struct value *v)
9706 {
9707 LONGEST transferred;
9708
9709 /* If we can access registers, so can we access $_siginfo. Likewise
9710 vice versa. */
9711 validate_registers_access ();
9712
9713 transferred =
9714 target_read (current_inferior ()->top_target (),
9715 TARGET_OBJECT_SIGNAL_INFO,
9716 nullptr,
9717 v->contents_all_raw ().data (),
9718 v->offset (),
9719 v->type ()->length ());
9720
9721 if (transferred != v->type ()->length ())
9722 error (_("Unable to read siginfo"));
9723 }
9724
9725 /* This function implements the lval_computed support for writing a
9726 $_siginfo value. */
9727
9728 static void
9729 siginfo_value_write (struct value *v, struct value *fromval)
9730 {
9731 LONGEST transferred;
9732
9733 /* If we can access registers, so can we access $_siginfo. Likewise
9734 vice versa. */
9735 validate_registers_access ();
9736
9737 transferred = target_write (current_inferior ()->top_target (),
9738 TARGET_OBJECT_SIGNAL_INFO,
9739 nullptr,
9740 fromval->contents_all_raw ().data (),
9741 v->offset (),
9742 fromval->type ()->length ());
9743
9744 if (transferred != fromval->type ()->length ())
9745 error (_("Unable to write siginfo"));
9746 }
9747
9748 static const struct lval_funcs siginfo_value_funcs =
9749 {
9750 siginfo_value_read,
9751 siginfo_value_write
9752 };
9753
9754 /* Return a new value with the correct type for the siginfo object of
9755 the current thread using architecture GDBARCH. Return a void value
9756 if there's no object available. */
9757
9758 static struct value *
9759 siginfo_make_value (struct gdbarch *gdbarch, struct internalvar *var,
9760 void *ignore)
9761 {
9762 if (target_has_stack ()
9763 && inferior_ptid != null_ptid
9764 && gdbarch_get_siginfo_type_p (gdbarch))
9765 {
9766 struct type *type = gdbarch_get_siginfo_type (gdbarch);
9767
9768 return value::allocate_computed (type, &siginfo_value_funcs, nullptr);
9769 }
9770
9771 return value::allocate (builtin_type (gdbarch)->builtin_void);
9772 }
9773
9774 \f
9775 /* infcall_suspend_state contains state about the program itself like its
9776 registers and any signal it received when it last stopped.
9777 This state must be restored regardless of how the inferior function call
9778 ends (either successfully, or after it hits a breakpoint or signal)
9779 if the program is to properly continue where it left off. */
9780
9781 class infcall_suspend_state
9782 {
9783 public:
9784 /* Capture state from GDBARCH, TP, and REGCACHE that must be restored
9785 once the inferior function call has finished. */
9786 infcall_suspend_state (struct gdbarch *gdbarch,
9787 const struct thread_info *tp,
9788 struct regcache *regcache)
9789 : m_registers (new readonly_detached_regcache (*regcache))
9790 {
9791 tp->save_suspend_to (m_thread_suspend);
9792
9793 gdb::unique_xmalloc_ptr<gdb_byte> siginfo_data;
9794
9795 if (gdbarch_get_siginfo_type_p (gdbarch))
9796 {
9797 struct type *type = gdbarch_get_siginfo_type (gdbarch);
9798 size_t len = type->length ();
9799
9800 siginfo_data.reset ((gdb_byte *) xmalloc (len));
9801
9802 if (target_read (current_inferior ()->top_target (),
9803 TARGET_OBJECT_SIGNAL_INFO, nullptr,
9804 siginfo_data.get (), 0, len) != len)
9805 {
9806 /* Errors ignored. */
9807 siginfo_data.reset (nullptr);
9808 }
9809 }
9810
9811 if (siginfo_data)
9812 {
9813 m_siginfo_gdbarch = gdbarch;
9814 m_siginfo_data = std::move (siginfo_data);
9815 }
9816 }
9817
9818 /* Return a pointer to the stored register state. */
9819
9820 readonly_detached_regcache *registers () const
9821 {
9822 return m_registers.get ();
9823 }
9824
9825 /* Restores the stored state into GDBARCH, TP, and REGCACHE. */
9826
9827 void restore (struct gdbarch *gdbarch,
9828 struct thread_info *tp,
9829 struct regcache *regcache) const
9830 {
9831 tp->restore_suspend_from (m_thread_suspend);
9832
9833 if (m_siginfo_gdbarch == gdbarch)
9834 {
9835 struct type *type = gdbarch_get_siginfo_type (gdbarch);
9836
9837 /* Errors ignored. */
9838 target_write (current_inferior ()->top_target (),
9839 TARGET_OBJECT_SIGNAL_INFO, nullptr,
9840 m_siginfo_data.get (), 0, type->length ());
9841 }
9842
9843 /* The inferior can be gone if the user types "print exit(0)"
9844 (and perhaps other times). */
9845 if (target_has_execution ())
9846 /* NB: The register write goes through to the target. */
9847 regcache->restore (registers ());
9848 }
9849
9850 private:
9851 /* How the current thread stopped before the inferior function call was
9852 executed. */
9853 struct thread_suspend_state m_thread_suspend;
9854
9855 /* The registers before the inferior function call was executed. */
9856 std::unique_ptr<readonly_detached_regcache> m_registers;
9857
9858 /* Format of SIGINFO_DATA or NULL if it is not present. */
9859 struct gdbarch *m_siginfo_gdbarch = nullptr;
9860
9861 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
9862 gdbarch_get_siginfo_type ()->length (). For different gdbarch the
9863 content would be invalid. */
9864 gdb::unique_xmalloc_ptr<gdb_byte> m_siginfo_data;
9865 };
9866
9867 infcall_suspend_state_up
9868 save_infcall_suspend_state ()
9869 {
9870 struct thread_info *tp = inferior_thread ();
9871 struct regcache *regcache = get_current_regcache ();
9872 struct gdbarch *gdbarch = regcache->arch ();
9873
9874 infcall_suspend_state_up inf_state
9875 (new struct infcall_suspend_state (gdbarch, tp, regcache));
9876
9877 /* Having saved the current state, adjust the thread state, discarding
9878 any stop signal information. The stop signal is not useful when
9879 starting an inferior function call, and run_inferior_call will not use
9880 the signal due to its `proceed' call with GDB_SIGNAL_0. */
9881 tp->set_stop_signal (GDB_SIGNAL_0);
9882
9883 return inf_state;
9884 }
9885
9886 /* Restore inferior session state to INF_STATE. */
9887
9888 void
9889 restore_infcall_suspend_state (struct infcall_suspend_state *inf_state)
9890 {
9891 struct thread_info *tp = inferior_thread ();
9892 struct regcache *regcache = get_current_regcache ();
9893 struct gdbarch *gdbarch = regcache->arch ();
9894
9895 inf_state->restore (gdbarch, tp, regcache);
9896 discard_infcall_suspend_state (inf_state);
9897 }
9898
9899 void
9900 discard_infcall_suspend_state (struct infcall_suspend_state *inf_state)
9901 {
9902 delete inf_state;
9903 }
9904
9905 readonly_detached_regcache *
9906 get_infcall_suspend_state_regcache (struct infcall_suspend_state *inf_state)
9907 {
9908 return inf_state->registers ();
9909 }
9910
9911 /* infcall_control_state contains state regarding gdb's control of the
9912 inferior itself like stepping control. It also contains session state like
9913 the user's currently selected frame. */
9914
9915 struct infcall_control_state
9916 {
9917 struct thread_control_state thread_control;
9918 struct inferior_control_state inferior_control;
9919
9920 /* Other fields: */
9921 enum stop_stack_kind stop_stack_dummy = STOP_NONE;
9922 int stopped_by_random_signal = 0;
9923
9924 /* ID and level of the selected frame when the inferior function
9925 call was made. */
9926 struct frame_id selected_frame_id {};
9927 int selected_frame_level = -1;
9928 };
9929
9930 /* Save all of the information associated with the inferior<==>gdb
9931 connection. */
9932
9933 infcall_control_state_up
9934 save_infcall_control_state ()
9935 {
9936 infcall_control_state_up inf_status (new struct infcall_control_state);
9937 struct thread_info *tp = inferior_thread ();
9938 struct inferior *inf = current_inferior ();
9939
9940 inf_status->thread_control = tp->control;
9941 inf_status->inferior_control = inf->control;
9942
9943 tp->control.step_resume_breakpoint = nullptr;
9944 tp->control.exception_resume_breakpoint = nullptr;
9945
9946 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
9947 chain. If caller's caller is walking the chain, they'll be happier if we
9948 hand them back the original chain when restore_infcall_control_state is
9949 called. */
9950 tp->control.stop_bpstat = bpstat_copy (tp->control.stop_bpstat);
9951
9952 /* Other fields: */
9953 inf_status->stop_stack_dummy = stop_stack_dummy;
9954 inf_status->stopped_by_random_signal = stopped_by_random_signal;
9955
9956 save_selected_frame (&inf_status->selected_frame_id,
9957 &inf_status->selected_frame_level);
9958
9959 return inf_status;
9960 }
9961
9962 /* Restore inferior session state to INF_STATUS. */
9963
9964 void
9965 restore_infcall_control_state (struct infcall_control_state *inf_status)
9966 {
9967 struct thread_info *tp = inferior_thread ();
9968 struct inferior *inf = current_inferior ();
9969
9970 if (tp->control.step_resume_breakpoint)
9971 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
9972
9973 if (tp->control.exception_resume_breakpoint)
9974 tp->control.exception_resume_breakpoint->disposition
9975 = disp_del_at_next_stop;
9976
9977 /* Handle the bpstat_copy of the chain. */
9978 bpstat_clear (&tp->control.stop_bpstat);
9979
9980 tp->control = inf_status->thread_control;
9981 inf->control = inf_status->inferior_control;
9982
9983 /* Other fields: */
9984 stop_stack_dummy = inf_status->stop_stack_dummy;
9985 stopped_by_random_signal = inf_status->stopped_by_random_signal;
9986
9987 if (target_has_stack ())
9988 {
9989 restore_selected_frame (inf_status->selected_frame_id,
9990 inf_status->selected_frame_level);
9991 }
9992
9993 delete inf_status;
9994 }
9995
9996 void
9997 discard_infcall_control_state (struct infcall_control_state *inf_status)
9998 {
9999 if (inf_status->thread_control.step_resume_breakpoint)
10000 inf_status->thread_control.step_resume_breakpoint->disposition
10001 = disp_del_at_next_stop;
10002
10003 if (inf_status->thread_control.exception_resume_breakpoint)
10004 inf_status->thread_control.exception_resume_breakpoint->disposition
10005 = disp_del_at_next_stop;
10006
10007 /* See save_infcall_control_state for info on stop_bpstat. */
10008 bpstat_clear (&inf_status->thread_control.stop_bpstat);
10009
10010 delete inf_status;
10011 }
10012 \f
10013 /* See infrun.h. */
10014
10015 void
10016 clear_exit_convenience_vars (void)
10017 {
10018 clear_internalvar (lookup_internalvar ("_exitsignal"));
10019 clear_internalvar (lookup_internalvar ("_exitcode"));
10020 }
10021 \f
10022
10023 /* User interface for reverse debugging:
10024 Set exec-direction / show exec-direction commands
10025 (returns error unless target implements to_set_exec_direction method). */
10026
10027 enum exec_direction_kind execution_direction = EXEC_FORWARD;
10028 static const char exec_forward[] = "forward";
10029 static const char exec_reverse[] = "reverse";
10030 static const char *exec_direction = exec_forward;
10031 static const char *const exec_direction_names[] = {
10032 exec_forward,
10033 exec_reverse,
10034 nullptr
10035 };
10036
10037 static void
10038 set_exec_direction_func (const char *args, int from_tty,
10039 struct cmd_list_element *cmd)
10040 {
10041 if (target_can_execute_reverse ())
10042 {
10043 if (!strcmp (exec_direction, exec_forward))
10044 execution_direction = EXEC_FORWARD;
10045 else if (!strcmp (exec_direction, exec_reverse))
10046 execution_direction = EXEC_REVERSE;
10047 }
10048 else
10049 {
10050 exec_direction = exec_forward;
10051 error (_("Target does not support this operation."));
10052 }
10053 }
10054
10055 static void
10056 show_exec_direction_func (struct ui_file *out, int from_tty,
10057 struct cmd_list_element *cmd, const char *value)
10058 {
10059 switch (execution_direction) {
10060 case EXEC_FORWARD:
10061 gdb_printf (out, _("Forward.\n"));
10062 break;
10063 case EXEC_REVERSE:
10064 gdb_printf (out, _("Reverse.\n"));
10065 break;
10066 default:
10067 internal_error (_("bogus execution_direction value: %d"),
10068 (int) execution_direction);
10069 }
10070 }
10071
10072 static void
10073 show_schedule_multiple (struct ui_file *file, int from_tty,
10074 struct cmd_list_element *c, const char *value)
10075 {
10076 gdb_printf (file, _("Resuming the execution of threads "
10077 "of all processes is %s.\n"), value);
10078 }
10079
10080 /* Implementation of `siginfo' variable. */
10081
10082 static const struct internalvar_funcs siginfo_funcs =
10083 {
10084 siginfo_make_value,
10085 nullptr,
10086 };
10087
10088 /* Callback for infrun's target events source. This is marked when a
10089 thread has a pending status to process. */
10090
10091 static void
10092 infrun_async_inferior_event_handler (gdb_client_data data)
10093 {
10094 clear_async_event_handler (infrun_async_inferior_event_token);
10095 inferior_event_handler (INF_REG_EVENT);
10096 }
10097
10098 #if GDB_SELF_TEST
10099 namespace selftests
10100 {
10101
10102 /* Verify that when two threads with the same ptid exist (from two different
10103 targets) and one of them changes ptid, we only update inferior_ptid if
10104 it is appropriate. */
10105
10106 static void
10107 infrun_thread_ptid_changed ()
10108 {
10109 gdbarch *arch = current_inferior ()->arch ();
10110
10111 /* The thread which inferior_ptid represents changes ptid. */
10112 {
10113 scoped_restore_current_pspace_and_thread restore;
10114
10115 scoped_mock_context<test_target_ops> target1 (arch);
10116 scoped_mock_context<test_target_ops> target2 (arch);
10117
10118 ptid_t old_ptid (111, 222);
10119 ptid_t new_ptid (111, 333);
10120
10121 target1.mock_inferior.pid = old_ptid.pid ();
10122 target1.mock_thread.ptid = old_ptid;
10123 target1.mock_inferior.ptid_thread_map.clear ();
10124 target1.mock_inferior.ptid_thread_map[old_ptid] = &target1.mock_thread;
10125
10126 target2.mock_inferior.pid = old_ptid.pid ();
10127 target2.mock_thread.ptid = old_ptid;
10128 target2.mock_inferior.ptid_thread_map.clear ();
10129 target2.mock_inferior.ptid_thread_map[old_ptid] = &target2.mock_thread;
10130
10131 auto restore_inferior_ptid = make_scoped_restore (&inferior_ptid, old_ptid);
10132 set_current_inferior (&target1.mock_inferior);
10133
10134 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
10135
10136 gdb_assert (inferior_ptid == new_ptid);
10137 }
10138
10139 /* A thread with the same ptid as inferior_ptid, but from another target,
10140 changes ptid. */
10141 {
10142 scoped_restore_current_pspace_and_thread restore;
10143
10144 scoped_mock_context<test_target_ops> target1 (arch);
10145 scoped_mock_context<test_target_ops> target2 (arch);
10146
10147 ptid_t old_ptid (111, 222);
10148 ptid_t new_ptid (111, 333);
10149
10150 target1.mock_inferior.pid = old_ptid.pid ();
10151 target1.mock_thread.ptid = old_ptid;
10152 target1.mock_inferior.ptid_thread_map.clear ();
10153 target1.mock_inferior.ptid_thread_map[old_ptid] = &target1.mock_thread;
10154
10155 target2.mock_inferior.pid = old_ptid.pid ();
10156 target2.mock_thread.ptid = old_ptid;
10157 target2.mock_inferior.ptid_thread_map.clear ();
10158 target2.mock_inferior.ptid_thread_map[old_ptid] = &target2.mock_thread;
10159
10160 auto restore_inferior_ptid = make_scoped_restore (&inferior_ptid, old_ptid);
10161 set_current_inferior (&target2.mock_inferior);
10162
10163 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
10164
10165 gdb_assert (inferior_ptid == old_ptid);
10166 }
10167 }
10168
10169 } /* namespace selftests */
10170
10171 #endif /* GDB_SELF_TEST */
10172
10173 void _initialize_infrun ();
10174 void
10175 _initialize_infrun ()
10176 {
10177 struct cmd_list_element *c;
10178
10179 /* Register extra event sources in the event loop. */
10180 infrun_async_inferior_event_token
10181 = create_async_event_handler (infrun_async_inferior_event_handler, nullptr,
10182 "infrun");
10183
10184 cmd_list_element *info_signals_cmd
10185 = add_info ("signals", info_signals_command, _("\
10186 What debugger does when program gets various signals.\n\
10187 Specify a signal as argument to print info on that signal only."));
10188 add_info_alias ("handle", info_signals_cmd, 0);
10189
10190 c = add_com ("handle", class_run, handle_command, _("\
10191 Specify how to handle signals.\n\
10192 Usage: handle SIGNAL [ACTIONS]\n\
10193 Args are signals and actions to apply to those signals.\n\
10194 If no actions are specified, the current settings for the specified signals\n\
10195 will be displayed instead.\n\
10196 \n\
10197 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
10198 from 1-15 are allowed for compatibility with old versions of GDB.\n\
10199 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
10200 The special arg \"all\" is recognized to mean all signals except those\n\
10201 used by the debugger, typically SIGTRAP and SIGINT.\n\
10202 \n\
10203 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
10204 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
10205 Stop means reenter debugger if this signal happens (implies print).\n\
10206 Print means print a message if this signal happens.\n\
10207 Pass means let program see this signal; otherwise program doesn't know.\n\
10208 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
10209 Pass and Stop may be combined.\n\
10210 \n\
10211 Multiple signals may be specified. Signal numbers and signal names\n\
10212 may be interspersed with actions, with the actions being performed for\n\
10213 all signals cumulatively specified."));
10214 set_cmd_completer (c, handle_completer);
10215
10216 stop_command = add_cmd ("stop", class_obscure,
10217 not_just_help_class_command, _("\
10218 There is no `stop' command, but you can set a hook on `stop'.\n\
10219 This allows you to set a list of commands to be run each time execution\n\
10220 of the program stops."), &cmdlist);
10221
10222 add_setshow_boolean_cmd
10223 ("infrun", class_maintenance, &debug_infrun,
10224 _("Set inferior debugging."),
10225 _("Show inferior debugging."),
10226 _("When non-zero, inferior specific debugging is enabled."),
10227 nullptr, show_debug_infrun, &setdebuglist, &showdebuglist);
10228
10229 add_setshow_boolean_cmd ("non-stop", no_class,
10230 &non_stop_1, _("\
10231 Set whether gdb controls the inferior in non-stop mode."), _("\
10232 Show whether gdb controls the inferior in non-stop mode."), _("\
10233 When debugging a multi-threaded program and this setting is\n\
10234 off (the default, also called all-stop mode), when one thread stops\n\
10235 (for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
10236 all other threads in the program while you interact with the thread of\n\
10237 interest. When you continue or step a thread, you can allow the other\n\
10238 threads to run, or have them remain stopped, but while you inspect any\n\
10239 thread's state, all threads stop.\n\
10240 \n\
10241 In non-stop mode, when one thread stops, other threads can continue\n\
10242 to run freely. You'll be able to step each thread independently,\n\
10243 leave it stopped or free to run as needed."),
10244 set_non_stop,
10245 show_non_stop,
10246 &setlist,
10247 &showlist);
10248
10249 for (size_t i = 0; i < GDB_SIGNAL_LAST; i++)
10250 {
10251 signal_stop[i] = 1;
10252 signal_print[i] = 1;
10253 signal_program[i] = 1;
10254 signal_catch[i] = 0;
10255 }
10256
10257 /* Signals caused by debugger's own actions should not be given to
10258 the program afterwards.
10259
10260 Do not deliver GDB_SIGNAL_TRAP by default, except when the user
10261 explicitly specifies that it should be delivered to the target
10262 program. Typically, that would occur when a user is debugging a
10263 target monitor on a simulator: the target monitor sets a
10264 breakpoint; the simulator encounters this breakpoint and halts
10265 the simulation handing control to GDB; GDB, noting that the stop
10266 address doesn't map to any known breakpoint, returns control back
10267 to the simulator; the simulator then delivers the hardware
10268 equivalent of a GDB_SIGNAL_TRAP to the program being
10269 debugged. */
10270 signal_program[GDB_SIGNAL_TRAP] = 0;
10271 signal_program[GDB_SIGNAL_INT] = 0;
10272
10273 /* Signals that are not errors should not normally enter the debugger. */
10274 signal_stop[GDB_SIGNAL_ALRM] = 0;
10275 signal_print[GDB_SIGNAL_ALRM] = 0;
10276 signal_stop[GDB_SIGNAL_VTALRM] = 0;
10277 signal_print[GDB_SIGNAL_VTALRM] = 0;
10278 signal_stop[GDB_SIGNAL_PROF] = 0;
10279 signal_print[GDB_SIGNAL_PROF] = 0;
10280 signal_stop[GDB_SIGNAL_CHLD] = 0;
10281 signal_print[GDB_SIGNAL_CHLD] = 0;
10282 signal_stop[GDB_SIGNAL_IO] = 0;
10283 signal_print[GDB_SIGNAL_IO] = 0;
10284 signal_stop[GDB_SIGNAL_POLL] = 0;
10285 signal_print[GDB_SIGNAL_POLL] = 0;
10286 signal_stop[GDB_SIGNAL_URG] = 0;
10287 signal_print[GDB_SIGNAL_URG] = 0;
10288 signal_stop[GDB_SIGNAL_WINCH] = 0;
10289 signal_print[GDB_SIGNAL_WINCH] = 0;
10290 signal_stop[GDB_SIGNAL_PRIO] = 0;
10291 signal_print[GDB_SIGNAL_PRIO] = 0;
10292
10293 /* These signals are used internally by user-level thread
10294 implementations. (See signal(5) on Solaris.) Like the above
10295 signals, a healthy program receives and handles them as part of
10296 its normal operation. */
10297 signal_stop[GDB_SIGNAL_LWP] = 0;
10298 signal_print[GDB_SIGNAL_LWP] = 0;
10299 signal_stop[GDB_SIGNAL_WAITING] = 0;
10300 signal_print[GDB_SIGNAL_WAITING] = 0;
10301 signal_stop[GDB_SIGNAL_CANCEL] = 0;
10302 signal_print[GDB_SIGNAL_CANCEL] = 0;
10303 signal_stop[GDB_SIGNAL_LIBRT] = 0;
10304 signal_print[GDB_SIGNAL_LIBRT] = 0;
10305
10306 /* Update cached state. */
10307 signal_cache_update (-1);
10308
10309 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
10310 &stop_on_solib_events, _("\
10311 Set stopping for shared library events."), _("\
10312 Show stopping for shared library events."), _("\
10313 If nonzero, gdb will give control to the user when the dynamic linker\n\
10314 notifies gdb of shared library events. The most common event of interest\n\
10315 to the user would be loading/unloading of a new library."),
10316 set_stop_on_solib_events,
10317 show_stop_on_solib_events,
10318 &setlist, &showlist);
10319
10320 add_setshow_enum_cmd ("follow-fork-mode", class_run,
10321 follow_fork_mode_kind_names,
10322 &follow_fork_mode_string, _("\
10323 Set debugger response to a program call of fork or vfork."), _("\
10324 Show debugger response to a program call of fork or vfork."), _("\
10325 A fork or vfork creates a new process. follow-fork-mode can be:\n\
10326 parent - the original process is debugged after a fork\n\
10327 child - the new process is debugged after a fork\n\
10328 The unfollowed process will continue to run.\n\
10329 By default, the debugger will follow the parent process."),
10330 nullptr,
10331 show_follow_fork_mode_string,
10332 &setlist, &showlist);
10333
10334 add_setshow_enum_cmd ("follow-exec-mode", class_run,
10335 follow_exec_mode_names,
10336 &follow_exec_mode_string, _("\
10337 Set debugger response to a program call of exec."), _("\
10338 Show debugger response to a program call of exec."), _("\
10339 An exec call replaces the program image of a process.\n\
10340 \n\
10341 follow-exec-mode can be:\n\
10342 \n\
10343 new - the debugger creates a new inferior and rebinds the process\n\
10344 to this new inferior. The program the process was running before\n\
10345 the exec call can be restarted afterwards by restarting the original\n\
10346 inferior.\n\
10347 \n\
10348 same - the debugger keeps the process bound to the same inferior.\n\
10349 The new executable image replaces the previous executable loaded in\n\
10350 the inferior. Restarting the inferior after the exec call restarts\n\
10351 the executable the process was running after the exec call.\n\
10352 \n\
10353 By default, the debugger will use the same inferior."),
10354 nullptr,
10355 show_follow_exec_mode_string,
10356 &setlist, &showlist);
10357
10358 add_setshow_enum_cmd ("scheduler-locking", class_run,
10359 scheduler_enums, &scheduler_mode, _("\
10360 Set mode for locking scheduler during execution."), _("\
10361 Show mode for locking scheduler during execution."), _("\
10362 off == no locking (threads may preempt at any time)\n\
10363 on == full locking (no thread except the current thread may run)\n\
10364 This applies to both normal execution and replay mode.\n\
10365 step == scheduler locked during stepping commands (step, next, stepi, nexti).\n\
10366 In this mode, other threads may run during other commands.\n\
10367 This applies to both normal execution and replay mode.\n\
10368 replay == scheduler locked in replay mode and unlocked during normal execution."),
10369 set_schedlock_func, /* traps on target vector */
10370 show_scheduler_mode,
10371 &setlist, &showlist);
10372
10373 add_setshow_boolean_cmd ("schedule-multiple", class_run, &sched_multi, _("\
10374 Set mode for resuming threads of all processes."), _("\
10375 Show mode for resuming threads of all processes."), _("\
10376 When on, execution commands (such as 'continue' or 'next') resume all\n\
10377 threads of all processes. When off (which is the default), execution\n\
10378 commands only resume the threads of the current process. The set of\n\
10379 threads that are resumed is further refined by the scheduler-locking\n\
10380 mode (see help set scheduler-locking)."),
10381 nullptr,
10382 show_schedule_multiple,
10383 &setlist, &showlist);
10384
10385 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
10386 Set mode of the step operation."), _("\
10387 Show mode of the step operation."), _("\
10388 When set, doing a step over a function without debug line information\n\
10389 will stop at the first instruction of that function. Otherwise, the\n\
10390 function is skipped and the step command stops at a different source line."),
10391 nullptr,
10392 show_step_stop_if_no_debug,
10393 &setlist, &showlist);
10394
10395 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run,
10396 &can_use_displaced_stepping, _("\
10397 Set debugger's willingness to use displaced stepping."), _("\
10398 Show debugger's willingness to use displaced stepping."), _("\
10399 If on, gdb will use displaced stepping to step over breakpoints if it is\n\
10400 supported by the target architecture. If off, gdb will not use displaced\n\
10401 stepping to step over breakpoints, even if such is supported by the target\n\
10402 architecture. If auto (which is the default), gdb will use displaced stepping\n\
10403 if the target architecture supports it and non-stop mode is active, but will not\n\
10404 use it in all-stop mode (see help set non-stop)."),
10405 nullptr,
10406 show_can_use_displaced_stepping,
10407 &setlist, &showlist);
10408
10409 add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
10410 &exec_direction, _("Set direction of execution.\n\
10411 Options are 'forward' or 'reverse'."),
10412 _("Show direction of execution (forward/reverse)."),
10413 _("Tells gdb whether to execute forward or backward."),
10414 set_exec_direction_func, show_exec_direction_func,
10415 &setlist, &showlist);
10416
10417 /* Set/show detach-on-fork: user-settable mode. */
10418
10419 add_setshow_boolean_cmd ("detach-on-fork", class_run, &detach_fork, _("\
10420 Set whether gdb will detach the child of a fork."), _("\
10421 Show whether gdb will detach the child of a fork."), _("\
10422 Tells gdb whether to detach the child of a fork."),
10423 nullptr, nullptr, &setlist, &showlist);
10424
10425 /* Set/show disable address space randomization mode. */
10426
10427 add_setshow_boolean_cmd ("disable-randomization", class_support,
10428 &disable_randomization, _("\
10429 Set disabling of debuggee's virtual address space randomization."), _("\
10430 Show disabling of debuggee's virtual address space randomization."), _("\
10431 When this mode is on (which is the default), randomization of the virtual\n\
10432 address space is disabled. Standalone programs run with the randomization\n\
10433 enabled by default on some platforms."),
10434 &set_disable_randomization,
10435 &show_disable_randomization,
10436 &setlist, &showlist);
10437
10438 /* ptid initializations */
10439 inferior_ptid = null_ptid;
10440 target_last_wait_ptid = minus_one_ptid;
10441
10442 gdb::observers::thread_ptid_changed.attach (infrun_thread_ptid_changed,
10443 "infrun");
10444 gdb::observers::thread_stop_requested.attach (infrun_thread_stop_requested,
10445 "infrun");
10446 gdb::observers::inferior_exit.attach (infrun_inferior_exit, "infrun");
10447 gdb::observers::inferior_execd.attach (infrun_inferior_execd, "infrun");
10448
10449 /* Explicitly create without lookup, since that tries to create a
10450 value with a void typed value, and when we get here, gdbarch
10451 isn't initialized yet. At this point, we're quite sure there
10452 isn't another convenience variable of the same name. */
10453 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs, nullptr);
10454
10455 add_setshow_boolean_cmd ("observer", no_class,
10456 &observer_mode_1, _("\
10457 Set whether gdb controls the inferior in observer mode."), _("\
10458 Show whether gdb controls the inferior in observer mode."), _("\
10459 In observer mode, GDB can get data from the inferior, but not\n\
10460 affect its execution. Registers and memory may not be changed,\n\
10461 breakpoints may not be set, and the program cannot be interrupted\n\
10462 or signalled."),
10463 set_observer_mode,
10464 show_observer_mode,
10465 &setlist,
10466 &showlist);
10467
10468 #if GDB_SELF_TEST
10469 selftests::register_test ("infrun_thread_ptid_changed",
10470 selftests::infrun_thread_ptid_changed);
10471 #endif
10472 }