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