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