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