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