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