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