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