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