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