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