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