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