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