]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/infrun.c
ignore invalid DOF provider sections
[thirdparty/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
4fa8626c
DJ
3436static void
3437adjust_pc_after_break (struct execution_control_state *ecs)
3438{
24a73cce
UW
3439 struct regcache *regcache;
3440 struct gdbarch *gdbarch;
6c95b8df 3441 struct address_space *aspace;
118e6252 3442 CORE_ADDR breakpoint_pc, decr_pc;
4fa8626c 3443
4fa8626c
DJ
3444 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
3445 we aren't, just return.
9709f61c
DJ
3446
3447 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
b798847d
UW
3448 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
3449 implemented by software breakpoints should be handled through the normal
3450 breakpoint layer.
8fb3e588 3451
4fa8626c
DJ
3452 NOTE drow/2004-01-31: On some targets, breakpoints may generate
3453 different signals (SIGILL or SIGEMT for instance), but it is less
3454 clear where the PC is pointing afterwards. It may not match
b798847d
UW
3455 gdbarch_decr_pc_after_break. I don't know any specific target that
3456 generates these signals at breakpoints (the code has been in GDB since at
3457 least 1992) so I can not guess how to handle them here.
8fb3e588 3458
e6cf7916
UW
3459 In earlier versions of GDB, a target with
3460 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
b798847d
UW
3461 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
3462 target with both of these set in GDB history, and it seems unlikely to be
3463 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4fa8626c
DJ
3464
3465 if (ecs->ws.kind != TARGET_WAITKIND_STOPPED)
3466 return;
3467
a493e3e2 3468 if (ecs->ws.value.sig != GDB_SIGNAL_TRAP)
4fa8626c
DJ
3469 return;
3470
4058b839
PA
3471 /* In reverse execution, when a breakpoint is hit, the instruction
3472 under it has already been de-executed. The reported PC always
3473 points at the breakpoint address, so adjusting it further would
3474 be wrong. E.g., consider this case on a decr_pc_after_break == 1
3475 architecture:
3476
3477 B1 0x08000000 : INSN1
3478 B2 0x08000001 : INSN2
3479 0x08000002 : INSN3
3480 PC -> 0x08000003 : INSN4
3481
3482 Say you're stopped at 0x08000003 as above. Reverse continuing
3483 from that point should hit B2 as below. Reading the PC when the
3484 SIGTRAP is reported should read 0x08000001 and INSN2 should have
3485 been de-executed already.
3486
3487 B1 0x08000000 : INSN1
3488 B2 PC -> 0x08000001 : INSN2
3489 0x08000002 : INSN3
3490 0x08000003 : INSN4
3491
3492 We can't apply the same logic as for forward execution, because
3493 we would wrongly adjust the PC to 0x08000000, since there's a
3494 breakpoint at PC - 1. We'd then report a hit on B1, although
3495 INSN1 hadn't been de-executed yet. Doing nothing is the correct
3496 behaviour. */
3497 if (execution_direction == EXEC_REVERSE)
3498 return;
3499
1cf4d951
PA
3500 /* If the target can tell whether the thread hit a SW breakpoint,
3501 trust it. Targets that can tell also adjust the PC
3502 themselves. */
3503 if (target_supports_stopped_by_sw_breakpoint ())
3504 return;
3505
3506 /* Note that relying on whether a breakpoint is planted in memory to
3507 determine this can fail. E.g,. the breakpoint could have been
3508 removed since. Or the thread could have been told to step an
3509 instruction the size of a breakpoint instruction, and only
3510 _after_ was a breakpoint inserted at its address. */
3511
24a73cce
UW
3512 /* If this target does not decrement the PC after breakpoints, then
3513 we have nothing to do. */
3514 regcache = get_thread_regcache (ecs->ptid);
3515 gdbarch = get_regcache_arch (regcache);
118e6252 3516
527a273a 3517 decr_pc = gdbarch_decr_pc_after_break (gdbarch);
118e6252 3518 if (decr_pc == 0)
24a73cce
UW
3519 return;
3520
6c95b8df
PA
3521 aspace = get_regcache_aspace (regcache);
3522
8aad930b
AC
3523 /* Find the location where (if we've hit a breakpoint) the
3524 breakpoint would be. */
118e6252 3525 breakpoint_pc = regcache_read_pc (regcache) - decr_pc;
8aad930b 3526
1cf4d951
PA
3527 /* If the target can't tell whether a software breakpoint triggered,
3528 fallback to figuring it out based on breakpoints we think were
3529 inserted in the target, and on whether the thread was stepped or
3530 continued. */
3531
1c5cfe86
PA
3532 /* Check whether there actually is a software breakpoint inserted at
3533 that location.
3534
3535 If in non-stop mode, a race condition is possible where we've
3536 removed a breakpoint, but stop events for that breakpoint were
3537 already queued and arrive later. To suppress those spurious
3538 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
1cf4d951
PA
3539 and retire them after a number of stop events are reported. Note
3540 this is an heuristic and can thus get confused. The real fix is
3541 to get the "stopped by SW BP and needs adjustment" info out of
3542 the target/kernel (and thus never reach here; see above). */
6c95b8df
PA
3543 if (software_breakpoint_inserted_here_p (aspace, breakpoint_pc)
3544 || (non_stop && moribund_breakpoint_here_p (aspace, breakpoint_pc)))
8aad930b 3545 {
77f9e713 3546 struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
abbb1732 3547
8213266a 3548 if (record_full_is_used ())
77f9e713 3549 record_full_gdb_operation_disable_set ();
96429cc8 3550
1c0fdd0e
UW
3551 /* When using hardware single-step, a SIGTRAP is reported for both
3552 a completed single-step and a software breakpoint. Need to
3553 differentiate between the two, as the latter needs adjusting
3554 but the former does not.
3555
3556 The SIGTRAP can be due to a completed hardware single-step only if
3557 - we didn't insert software single-step breakpoints
1c0fdd0e
UW
3558 - this thread is currently being stepped
3559
3560 If any of these events did not occur, we must have stopped due
3561 to hitting a software breakpoint, and have to back up to the
3562 breakpoint address.
3563
3564 As a special case, we could have hardware single-stepped a
3565 software breakpoint. In this case (prev_pc == breakpoint_pc),
3566 we also need to back up to the breakpoint address. */
3567
34b7e8a6 3568 if (thread_has_single_step_breakpoints_set (ecs->event_thread)
4e1c45ea 3569 || !currently_stepping (ecs->event_thread)
7f5ef605
PA
3570 || (ecs->event_thread->stepped_breakpoint
3571 && ecs->event_thread->prev_pc == breakpoint_pc))
515630c5 3572 regcache_write_pc (regcache, breakpoint_pc);
96429cc8 3573
77f9e713 3574 do_cleanups (old_cleanups);
8aad930b 3575 }
4fa8626c
DJ
3576}
3577
edb3359d
DJ
3578static int
3579stepped_in_from (struct frame_info *frame, struct frame_id step_frame_id)
3580{
3581 for (frame = get_prev_frame (frame);
3582 frame != NULL;
3583 frame = get_prev_frame (frame))
3584 {
3585 if (frame_id_eq (get_frame_id (frame), step_frame_id))
3586 return 1;
3587 if (get_frame_type (frame) != INLINE_FRAME)
3588 break;
3589 }
3590
3591 return 0;
3592}
3593
a96d9b2e
SDJ
3594/* Auxiliary function that handles syscall entry/return events.
3595 It returns 1 if the inferior should keep going (and GDB
3596 should ignore the event), or 0 if the event deserves to be
3597 processed. */
ca2163eb 3598
a96d9b2e 3599static int
ca2163eb 3600handle_syscall_event (struct execution_control_state *ecs)
a96d9b2e 3601{
ca2163eb 3602 struct regcache *regcache;
ca2163eb
PA
3603 int syscall_number;
3604
3605 if (!ptid_equal (ecs->ptid, inferior_ptid))
3606 context_switch (ecs->ptid);
3607
3608 regcache = get_thread_regcache (ecs->ptid);
f90263c1 3609 syscall_number = ecs->ws.value.syscall_number;
ca2163eb
PA
3610 stop_pc = regcache_read_pc (regcache);
3611
a96d9b2e
SDJ
3612 if (catch_syscall_enabled () > 0
3613 && catching_syscall_number (syscall_number) > 0)
3614 {
3615 if (debug_infrun)
3616 fprintf_unfiltered (gdb_stdlog, "infrun: syscall number = '%d'\n",
3617 syscall_number);
a96d9b2e 3618
16c381f0 3619 ecs->event_thread->control.stop_bpstat
6c95b8df 3620 = bpstat_stop_status (get_regcache_aspace (regcache),
09ac7c10 3621 stop_pc, ecs->ptid, &ecs->ws);
ab04a2af 3622
ce12b012 3623 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
ca2163eb
PA
3624 {
3625 /* Catchpoint hit. */
ca2163eb
PA
3626 return 0;
3627 }
a96d9b2e 3628 }
ca2163eb
PA
3629
3630 /* If no catchpoint triggered for this, then keep going. */
ca2163eb
PA
3631 keep_going (ecs);
3632 return 1;
a96d9b2e
SDJ
3633}
3634
7e324e48
GB
3635/* Lazily fill in the execution_control_state's stop_func_* fields. */
3636
3637static void
3638fill_in_stop_func (struct gdbarch *gdbarch,
3639 struct execution_control_state *ecs)
3640{
3641 if (!ecs->stop_func_filled_in)
3642 {
3643 /* Don't care about return value; stop_func_start and stop_func_name
3644 will both be 0 if it doesn't work. */
3645 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
3646 &ecs->stop_func_start, &ecs->stop_func_end);
3647 ecs->stop_func_start
3648 += gdbarch_deprecated_function_start_offset (gdbarch);
3649
591a12a1
UW
3650 if (gdbarch_skip_entrypoint_p (gdbarch))
3651 ecs->stop_func_start = gdbarch_skip_entrypoint (gdbarch,
3652 ecs->stop_func_start);
3653
7e324e48
GB
3654 ecs->stop_func_filled_in = 1;
3655 }
3656}
3657
4f5d7f63
PA
3658
3659/* Return the STOP_SOON field of the inferior pointed at by PTID. */
3660
3661static enum stop_kind
3662get_inferior_stop_soon (ptid_t ptid)
3663{
c9657e70 3664 struct inferior *inf = find_inferior_ptid (ptid);
4f5d7f63
PA
3665
3666 gdb_assert (inf != NULL);
3667 return inf->control.stop_soon;
3668}
3669
05ba8510
PA
3670/* Given an execution control state that has been freshly filled in by
3671 an event from the inferior, figure out what it means and take
3672 appropriate action.
3673
3674 The alternatives are:
3675
22bcd14b 3676 1) stop_waiting and return; to really stop and return to the
05ba8510
PA
3677 debugger.
3678
3679 2) keep_going and return; to wait for the next event (set
3680 ecs->event_thread->stepping_over_breakpoint to 1 to single step
3681 once). */
c906108c 3682
ec9499be 3683static void
0b6e5e10 3684handle_inferior_event_1 (struct execution_control_state *ecs)
cd0fc7c3 3685{
d6b48e9c
PA
3686 enum stop_kind stop_soon;
3687
28736962
PA
3688 if (ecs->ws.kind == TARGET_WAITKIND_IGNORE)
3689 {
3690 /* We had an event in the inferior, but we are not interested in
3691 handling it at this level. The lower layers have already
3692 done what needs to be done, if anything.
3693
3694 One of the possible circumstances for this is when the
3695 inferior produces output for the console. The inferior has
3696 not stopped, and we are ignoring the event. Another possible
3697 circumstance is any event which the lower level knows will be
3698 reported multiple times without an intervening resume. */
3699 if (debug_infrun)
3700 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
3701 prepare_to_wait (ecs);
3702 return;
3703 }
3704
0e5bf2a8
PA
3705 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED
3706 && target_can_async_p () && !sync_execution)
3707 {
3708 /* There were no unwaited-for children left in the target, but,
3709 we're not synchronously waiting for events either. Just
3710 ignore. Otherwise, if we were running a synchronous
3711 execution command, we need to cancel it and give the user
3712 back the terminal. */
3713 if (debug_infrun)
3714 fprintf_unfiltered (gdb_stdlog,
3715 "infrun: TARGET_WAITKIND_NO_RESUMED (ignoring)\n");
3716 prepare_to_wait (ecs);
3717 return;
3718 }
3719
1777feb0 3720 /* Cache the last pid/waitstatus. */
c32c64b7 3721 set_last_target_status (ecs->ptid, ecs->ws);
e02bc4cc 3722
ca005067 3723 /* Always clear state belonging to the previous time we stopped. */
aa7d318d 3724 stop_stack_dummy = STOP_NONE;
ca005067 3725
0e5bf2a8
PA
3726 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED)
3727 {
3728 /* No unwaited-for children left. IOW, all resumed children
3729 have exited. */
3730 if (debug_infrun)
3731 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_RESUMED\n");
3732
3733 stop_print_frame = 0;
22bcd14b 3734 stop_waiting (ecs);
0e5bf2a8
PA
3735 return;
3736 }
3737
8c90c137 3738 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
64776a0b 3739 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
359f5fe6
PA
3740 {
3741 ecs->event_thread = find_thread_ptid (ecs->ptid);
3742 /* If it's a new thread, add it to the thread database. */
3743 if (ecs->event_thread == NULL)
3744 ecs->event_thread = add_thread (ecs->ptid);
c1e36e3e
PA
3745
3746 /* Disable range stepping. If the next step request could use a
3747 range, this will be end up re-enabled then. */
3748 ecs->event_thread->control.may_range_step = 0;
359f5fe6 3749 }
88ed393a
JK
3750
3751 /* Dependent on valid ECS->EVENT_THREAD. */
3752 adjust_pc_after_break (ecs);
3753
3754 /* Dependent on the current PC value modified by adjust_pc_after_break. */
3755 reinit_frame_cache ();
3756
28736962
PA
3757 breakpoint_retire_moribund ();
3758
2b009048
DJ
3759 /* First, distinguish signals caused by the debugger from signals
3760 that have to do with the program's own actions. Note that
3761 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
3762 on the operating system version. Here we detect when a SIGILL or
3763 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
3764 something similar for SIGSEGV, since a SIGSEGV will be generated
3765 when we're trying to execute a breakpoint instruction on a
3766 non-executable stack. This happens for call dummy breakpoints
3767 for architectures like SPARC that place call dummies on the
3768 stack. */
2b009048 3769 if (ecs->ws.kind == TARGET_WAITKIND_STOPPED
a493e3e2
PA
3770 && (ecs->ws.value.sig == GDB_SIGNAL_ILL
3771 || ecs->ws.value.sig == GDB_SIGNAL_SEGV
3772 || ecs->ws.value.sig == GDB_SIGNAL_EMT))
2b009048 3773 {
de0a0249
UW
3774 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3775
3776 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
3777 regcache_read_pc (regcache)))
3778 {
3779 if (debug_infrun)
3780 fprintf_unfiltered (gdb_stdlog,
3781 "infrun: Treating signal as SIGTRAP\n");
a493e3e2 3782 ecs->ws.value.sig = GDB_SIGNAL_TRAP;
de0a0249 3783 }
2b009048
DJ
3784 }
3785
28736962
PA
3786 /* Mark the non-executing threads accordingly. In all-stop, all
3787 threads of all processes are stopped when we get any event
3788 reported. In non-stop mode, only the event thread stops. If
3789 we're handling a process exit in non-stop mode, there's nothing
3790 to do, as threads of the dead process are gone, and threads of
3791 any other process were left running. */
3792 if (!non_stop)
3793 set_executing (minus_one_ptid, 0);
3794 else if (ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
3795 && ecs->ws.kind != TARGET_WAITKIND_EXITED)
7aee8dc2 3796 set_executing (ecs->ptid, 0);
8c90c137 3797
488f131b
JB
3798 switch (ecs->ws.kind)
3799 {
3800 case TARGET_WAITKIND_LOADED:
527159b7 3801 if (debug_infrun)
8a9de0e4 3802 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
5c09a2c5
PA
3803 if (!ptid_equal (ecs->ptid, inferior_ptid))
3804 context_switch (ecs->ptid);
b0f4b84b
DJ
3805 /* Ignore gracefully during startup of the inferior, as it might
3806 be the shell which has just loaded some objects, otherwise
3807 add the symbols for the newly loaded objects. Also ignore at
3808 the beginning of an attach or remote session; we will query
3809 the full list of libraries once the connection is
3810 established. */
4f5d7f63
PA
3811
3812 stop_soon = get_inferior_stop_soon (ecs->ptid);
c0236d92 3813 if (stop_soon == NO_STOP_QUIETLY)
488f131b 3814 {
edcc5120
TT
3815 struct regcache *regcache;
3816
edcc5120
TT
3817 regcache = get_thread_regcache (ecs->ptid);
3818
3819 handle_solib_event ();
3820
3821 ecs->event_thread->control.stop_bpstat
3822 = bpstat_stop_status (get_regcache_aspace (regcache),
3823 stop_pc, ecs->ptid, &ecs->ws);
ab04a2af 3824
ce12b012 3825 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
edcc5120
TT
3826 {
3827 /* A catchpoint triggered. */
94c57d6a
PA
3828 process_event_stop_test (ecs);
3829 return;
edcc5120 3830 }
488f131b 3831
b0f4b84b
DJ
3832 /* If requested, stop when the dynamic linker notifies
3833 gdb of events. This allows the user to get control
3834 and place breakpoints in initializer routines for
3835 dynamically loaded objects (among other things). */
a493e3e2 3836 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
b0f4b84b
DJ
3837 if (stop_on_solib_events)
3838 {
55409f9d
DJ
3839 /* Make sure we print "Stopped due to solib-event" in
3840 normal_stop. */
3841 stop_print_frame = 1;
3842
22bcd14b 3843 stop_waiting (ecs);
b0f4b84b
DJ
3844 return;
3845 }
488f131b 3846 }
b0f4b84b
DJ
3847
3848 /* If we are skipping through a shell, or through shared library
3849 loading that we aren't interested in, resume the program. If
5c09a2c5 3850 we're running the program normally, also resume. */
b0f4b84b
DJ
3851 if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
3852 {
74960c60
VP
3853 /* Loading of shared libraries might have changed breakpoint
3854 addresses. Make sure new breakpoints are inserted. */
a25a5a45 3855 if (stop_soon == NO_STOP_QUIETLY)
74960c60 3856 insert_breakpoints ();
64ce06e4 3857 resume (GDB_SIGNAL_0);
b0f4b84b
DJ
3858 prepare_to_wait (ecs);
3859 return;
3860 }
3861
5c09a2c5
PA
3862 /* But stop if we're attaching or setting up a remote
3863 connection. */
3864 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
3865 || stop_soon == STOP_QUIETLY_REMOTE)
3866 {
3867 if (debug_infrun)
3868 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
22bcd14b 3869 stop_waiting (ecs);
5c09a2c5
PA
3870 return;
3871 }
3872
3873 internal_error (__FILE__, __LINE__,
3874 _("unhandled stop_soon: %d"), (int) stop_soon);
c5aa993b 3875
488f131b 3876 case TARGET_WAITKIND_SPURIOUS:
527159b7 3877 if (debug_infrun)
8a9de0e4 3878 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
64776a0b 3879 if (!ptid_equal (ecs->ptid, inferior_ptid))
8b3ee56d 3880 context_switch (ecs->ptid);
64ce06e4 3881 resume (GDB_SIGNAL_0);
488f131b
JB
3882 prepare_to_wait (ecs);
3883 return;
c5aa993b 3884
488f131b 3885 case TARGET_WAITKIND_EXITED:
940c3c06 3886 case TARGET_WAITKIND_SIGNALLED:
527159b7 3887 if (debug_infrun)
940c3c06
PA
3888 {
3889 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3890 fprintf_unfiltered (gdb_stdlog,
3891 "infrun: TARGET_WAITKIND_EXITED\n");
3892 else
3893 fprintf_unfiltered (gdb_stdlog,
3894 "infrun: TARGET_WAITKIND_SIGNALLED\n");
3895 }
3896
fb66883a 3897 inferior_ptid = ecs->ptid;
c9657e70 3898 set_current_inferior (find_inferior_ptid (ecs->ptid));
6c95b8df
PA
3899 set_current_program_space (current_inferior ()->pspace);
3900 handle_vfork_child_exec_or_exit (0);
1777feb0 3901 target_terminal_ours (); /* Must do this before mourn anyway. */
488f131b 3902
0c557179
SDJ
3903 /* Clearing any previous state of convenience variables. */
3904 clear_exit_convenience_vars ();
3905
940c3c06
PA
3906 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3907 {
3908 /* Record the exit code in the convenience variable $_exitcode, so
3909 that the user can inspect this again later. */
3910 set_internalvar_integer (lookup_internalvar ("_exitcode"),
3911 (LONGEST) ecs->ws.value.integer);
3912
3913 /* Also record this in the inferior itself. */
3914 current_inferior ()->has_exit_code = 1;
3915 current_inferior ()->exit_code = (LONGEST) ecs->ws.value.integer;
8cf64490 3916
98eb56a4
PA
3917 /* Support the --return-child-result option. */
3918 return_child_result_value = ecs->ws.value.integer;
3919
fd664c91 3920 observer_notify_exited (ecs->ws.value.integer);
940c3c06
PA
3921 }
3922 else
0c557179
SDJ
3923 {
3924 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3925 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3926
3927 if (gdbarch_gdb_signal_to_target_p (gdbarch))
3928 {
3929 /* Set the value of the internal variable $_exitsignal,
3930 which holds the signal uncaught by the inferior. */
3931 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
3932 gdbarch_gdb_signal_to_target (gdbarch,
3933 ecs->ws.value.sig));
3934 }
3935 else
3936 {
3937 /* We don't have access to the target's method used for
3938 converting between signal numbers (GDB's internal
3939 representation <-> target's representation).
3940 Therefore, we cannot do a good job at displaying this
3941 information to the user. It's better to just warn
3942 her about it (if infrun debugging is enabled), and
3943 give up. */
3944 if (debug_infrun)
3945 fprintf_filtered (gdb_stdlog, _("\
3946Cannot fill $_exitsignal with the correct signal number.\n"));
3947 }
3948
fd664c91 3949 observer_notify_signal_exited (ecs->ws.value.sig);
0c557179 3950 }
8cf64490 3951
488f131b
JB
3952 gdb_flush (gdb_stdout);
3953 target_mourn_inferior ();
488f131b 3954 stop_print_frame = 0;
22bcd14b 3955 stop_waiting (ecs);
488f131b 3956 return;
c5aa993b 3957
488f131b 3958 /* The following are the only cases in which we keep going;
1777feb0 3959 the above cases end in a continue or goto. */
488f131b 3960 case TARGET_WAITKIND_FORKED:
deb3b17b 3961 case TARGET_WAITKIND_VFORKED:
527159b7 3962 if (debug_infrun)
fed708ed
PA
3963 {
3964 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
3965 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
3966 else
3967 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_VFORKED\n");
3968 }
c906108c 3969
e2d96639
YQ
3970 /* Check whether the inferior is displaced stepping. */
3971 {
3972 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3973 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3974 struct displaced_step_inferior_state *displaced
3975 = get_displaced_stepping_state (ptid_get_pid (ecs->ptid));
3976
3977 /* If checking displaced stepping is supported, and thread
3978 ecs->ptid is displaced stepping. */
3979 if (displaced && ptid_equal (displaced->step_ptid, ecs->ptid))
3980 {
3981 struct inferior *parent_inf
c9657e70 3982 = find_inferior_ptid (ecs->ptid);
e2d96639
YQ
3983 struct regcache *child_regcache;
3984 CORE_ADDR parent_pc;
3985
3986 /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
3987 indicating that the displaced stepping of syscall instruction
3988 has been done. Perform cleanup for parent process here. Note
3989 that this operation also cleans up the child process for vfork,
3990 because their pages are shared. */
a493e3e2 3991 displaced_step_fixup (ecs->ptid, GDB_SIGNAL_TRAP);
e2d96639
YQ
3992
3993 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
3994 {
3995 /* Restore scratch pad for child process. */
3996 displaced_step_restore (displaced, ecs->ws.value.related_pid);
3997 }
3998
3999 /* Since the vfork/fork syscall instruction was executed in the scratchpad,
4000 the child's PC is also within the scratchpad. Set the child's PC
4001 to the parent's PC value, which has already been fixed up.
4002 FIXME: we use the parent's aspace here, although we're touching
4003 the child, because the child hasn't been added to the inferior
4004 list yet at this point. */
4005
4006 child_regcache
4007 = get_thread_arch_aspace_regcache (ecs->ws.value.related_pid,
4008 gdbarch,
4009 parent_inf->aspace);
4010 /* Read PC value of parent process. */
4011 parent_pc = regcache_read_pc (regcache);
4012
4013 if (debug_displaced)
4014 fprintf_unfiltered (gdb_stdlog,
4015 "displaced: write child pc from %s to %s\n",
4016 paddress (gdbarch,
4017 regcache_read_pc (child_regcache)),
4018 paddress (gdbarch, parent_pc));
4019
4020 regcache_write_pc (child_regcache, parent_pc);
4021 }
4022 }
4023
5a2901d9 4024 if (!ptid_equal (ecs->ptid, inferior_ptid))
c3a01a22 4025 context_switch (ecs->ptid);
5a2901d9 4026
b242c3c2
PA
4027 /* Immediately detach breakpoints from the child before there's
4028 any chance of letting the user delete breakpoints from the
4029 breakpoint lists. If we don't do this early, it's easy to
4030 leave left over traps in the child, vis: "break foo; catch
4031 fork; c; <fork>; del; c; <child calls foo>". We only follow
4032 the fork on the last `continue', and by that time the
4033 breakpoint at "foo" is long gone from the breakpoint table.
4034 If we vforked, then we don't need to unpatch here, since both
4035 parent and child are sharing the same memory pages; we'll
4036 need to unpatch at follow/detach time instead to be certain
4037 that new breakpoints added between catchpoint hit time and
4038 vfork follow are detached. */
4039 if (ecs->ws.kind != TARGET_WAITKIND_VFORKED)
4040 {
b242c3c2
PA
4041 /* This won't actually modify the breakpoint list, but will
4042 physically remove the breakpoints from the child. */
d80ee84f 4043 detach_breakpoints (ecs->ws.value.related_pid);
b242c3c2
PA
4044 }
4045
34b7e8a6 4046 delete_just_stopped_threads_single_step_breakpoints ();
d03285ec 4047
e58b0e63
PA
4048 /* In case the event is caught by a catchpoint, remember that
4049 the event is to be followed at the next resume of the thread,
4050 and not immediately. */
4051 ecs->event_thread->pending_follow = ecs->ws;
4052
fb14de7b 4053 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
675bf4cb 4054
16c381f0 4055 ecs->event_thread->control.stop_bpstat
6c95b8df 4056 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
09ac7c10 4057 stop_pc, ecs->ptid, &ecs->ws);
675bf4cb 4058
ce12b012
PA
4059 /* If no catchpoint triggered for this, then keep going. Note
4060 that we're interested in knowing the bpstat actually causes a
4061 stop, not just if it may explain the signal. Software
4062 watchpoints, for example, always appear in the bpstat. */
4063 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
04e68871 4064 {
6c95b8df
PA
4065 ptid_t parent;
4066 ptid_t child;
e58b0e63 4067 int should_resume;
3e43a32a
MS
4068 int follow_child
4069 = (follow_fork_mode_string == follow_fork_mode_child);
e58b0e63 4070
a493e3e2 4071 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
e58b0e63
PA
4072
4073 should_resume = follow_fork ();
4074
6c95b8df
PA
4075 parent = ecs->ptid;
4076 child = ecs->ws.value.related_pid;
4077
4078 /* In non-stop mode, also resume the other branch. */
4079 if (non_stop && !detach_fork)
4080 {
4081 if (follow_child)
4082 switch_to_thread (parent);
4083 else
4084 switch_to_thread (child);
4085
4086 ecs->event_thread = inferior_thread ();
4087 ecs->ptid = inferior_ptid;
4088 keep_going (ecs);
4089 }
4090
4091 if (follow_child)
4092 switch_to_thread (child);
4093 else
4094 switch_to_thread (parent);
4095
e58b0e63
PA
4096 ecs->event_thread = inferior_thread ();
4097 ecs->ptid = inferior_ptid;
4098
4099 if (should_resume)
4100 keep_going (ecs);
4101 else
22bcd14b 4102 stop_waiting (ecs);
04e68871
DJ
4103 return;
4104 }
94c57d6a
PA
4105 process_event_stop_test (ecs);
4106 return;
488f131b 4107
6c95b8df
PA
4108 case TARGET_WAITKIND_VFORK_DONE:
4109 /* Done with the shared memory region. Re-insert breakpoints in
4110 the parent, and keep going. */
4111
4112 if (debug_infrun)
3e43a32a
MS
4113 fprintf_unfiltered (gdb_stdlog,
4114 "infrun: TARGET_WAITKIND_VFORK_DONE\n");
6c95b8df
PA
4115
4116 if (!ptid_equal (ecs->ptid, inferior_ptid))
4117 context_switch (ecs->ptid);
4118
4119 current_inferior ()->waiting_for_vfork_done = 0;
56710373 4120 current_inferior ()->pspace->breakpoints_not_allowed = 0;
6c95b8df
PA
4121 /* This also takes care of reinserting breakpoints in the
4122 previously locked inferior. */
4123 keep_going (ecs);
4124 return;
4125
488f131b 4126 case TARGET_WAITKIND_EXECD:
527159b7 4127 if (debug_infrun)
fc5261f2 4128 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
488f131b 4129
5a2901d9 4130 if (!ptid_equal (ecs->ptid, inferior_ptid))
c3a01a22 4131 context_switch (ecs->ptid);
5a2901d9 4132
fb14de7b 4133 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
795e548f 4134
6c95b8df
PA
4135 /* Do whatever is necessary to the parent branch of the vfork. */
4136 handle_vfork_child_exec_or_exit (1);
4137
795e548f
PA
4138 /* This causes the eventpoints and symbol table to be reset.
4139 Must do this now, before trying to determine whether to
4140 stop. */
71b43ef8 4141 follow_exec (inferior_ptid, ecs->ws.value.execd_pathname);
795e548f 4142
16c381f0 4143 ecs->event_thread->control.stop_bpstat
6c95b8df 4144 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
09ac7c10 4145 stop_pc, ecs->ptid, &ecs->ws);
795e548f 4146
71b43ef8
PA
4147 /* Note that this may be referenced from inside
4148 bpstat_stop_status above, through inferior_has_execd. */
4149 xfree (ecs->ws.value.execd_pathname);
4150 ecs->ws.value.execd_pathname = NULL;
4151
04e68871 4152 /* If no catchpoint triggered for this, then keep going. */
ce12b012 4153 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
04e68871 4154 {
a493e3e2 4155 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
04e68871
DJ
4156 keep_going (ecs);
4157 return;
4158 }
94c57d6a
PA
4159 process_event_stop_test (ecs);
4160 return;
488f131b 4161
b4dc5ffa
MK
4162 /* Be careful not to try to gather much state about a thread
4163 that's in a syscall. It's frequently a losing proposition. */
488f131b 4164 case TARGET_WAITKIND_SYSCALL_ENTRY:
527159b7 4165 if (debug_infrun)
3e43a32a
MS
4166 fprintf_unfiltered (gdb_stdlog,
4167 "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
1777feb0 4168 /* Getting the current syscall number. */
94c57d6a
PA
4169 if (handle_syscall_event (ecs) == 0)
4170 process_event_stop_test (ecs);
4171 return;
c906108c 4172
488f131b
JB
4173 /* Before examining the threads further, step this thread to
4174 get it entirely out of the syscall. (We get notice of the
4175 event when the thread is just on the verge of exiting a
4176 syscall. Stepping one instruction seems to get it back
b4dc5ffa 4177 into user code.) */
488f131b 4178 case TARGET_WAITKIND_SYSCALL_RETURN:
527159b7 4179 if (debug_infrun)
3e43a32a
MS
4180 fprintf_unfiltered (gdb_stdlog,
4181 "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
94c57d6a
PA
4182 if (handle_syscall_event (ecs) == 0)
4183 process_event_stop_test (ecs);
4184 return;
c906108c 4185
488f131b 4186 case TARGET_WAITKIND_STOPPED:
527159b7 4187 if (debug_infrun)
8a9de0e4 4188 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
16c381f0 4189 ecs->event_thread->suspend.stop_signal = ecs->ws.value.sig;
4f5d7f63
PA
4190 handle_signal_stop (ecs);
4191 return;
c906108c 4192
b2175913 4193 case TARGET_WAITKIND_NO_HISTORY:
4b4e080e
PA
4194 if (debug_infrun)
4195 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_HISTORY\n");
b2175913 4196 /* Reverse execution: target ran out of history info. */
eab402df 4197
34b7e8a6 4198 delete_just_stopped_threads_single_step_breakpoints ();
fb14de7b 4199 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
fd664c91 4200 observer_notify_no_history ();
22bcd14b 4201 stop_waiting (ecs);
b2175913 4202 return;
488f131b 4203 }
4f5d7f63
PA
4204}
4205
0b6e5e10
JB
4206/* A wrapper around handle_inferior_event_1, which also makes sure
4207 that all temporary struct value objects that were created during
4208 the handling of the event get deleted at the end. */
4209
4210static void
4211handle_inferior_event (struct execution_control_state *ecs)
4212{
4213 struct value *mark = value_mark ();
4214
4215 handle_inferior_event_1 (ecs);
4216 /* Purge all temporary values created during the event handling,
4217 as it could be a long time before we return to the command level
4218 where such values would otherwise be purged. */
4219 value_free_to_mark (mark);
4220}
4221
4f5d7f63
PA
4222/* Come here when the program has stopped with a signal. */
4223
4224static void
4225handle_signal_stop (struct execution_control_state *ecs)
4226{
4227 struct frame_info *frame;
4228 struct gdbarch *gdbarch;
4229 int stopped_by_watchpoint;
4230 enum stop_kind stop_soon;
4231 int random_signal;
c906108c 4232
f0407826
DE
4233 gdb_assert (ecs->ws.kind == TARGET_WAITKIND_STOPPED);
4234
4235 /* Do we need to clean up the state of a thread that has
4236 completed a displaced single-step? (Doing so usually affects
4237 the PC, so do it here, before we set stop_pc.) */
4238 displaced_step_fixup (ecs->ptid,
4239 ecs->event_thread->suspend.stop_signal);
4240
4241 /* If we either finished a single-step or hit a breakpoint, but
4242 the user wanted this thread to be stopped, pretend we got a
4243 SIG0 (generic unsignaled stop). */
4244 if (ecs->event_thread->stop_requested
4245 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
4246 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
237fc4c9 4247
515630c5 4248 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
488f131b 4249
527159b7 4250 if (debug_infrun)
237fc4c9 4251 {
5af949e3
UW
4252 struct regcache *regcache = get_thread_regcache (ecs->ptid);
4253 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7f82dfc7
JK
4254 struct cleanup *old_chain = save_inferior_ptid ();
4255
4256 inferior_ptid = ecs->ptid;
5af949e3
UW
4257
4258 fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = %s\n",
4259 paddress (gdbarch, stop_pc));
d92524f1 4260 if (target_stopped_by_watchpoint ())
237fc4c9
PA
4261 {
4262 CORE_ADDR addr;
abbb1732 4263
237fc4c9
PA
4264 fprintf_unfiltered (gdb_stdlog, "infrun: stopped by watchpoint\n");
4265
4266 if (target_stopped_data_address (&current_target, &addr))
4267 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
4268 "infrun: stopped data address = %s\n",
4269 paddress (gdbarch, addr));
237fc4c9
PA
4270 else
4271 fprintf_unfiltered (gdb_stdlog,
4272 "infrun: (no data address available)\n");
4273 }
7f82dfc7
JK
4274
4275 do_cleanups (old_chain);
237fc4c9 4276 }
527159b7 4277
36fa8042
PA
4278 /* This is originated from start_remote(), start_inferior() and
4279 shared libraries hook functions. */
4280 stop_soon = get_inferior_stop_soon (ecs->ptid);
4281 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
4282 {
4283 if (!ptid_equal (ecs->ptid, inferior_ptid))
4284 context_switch (ecs->ptid);
4285 if (debug_infrun)
4286 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
4287 stop_print_frame = 1;
22bcd14b 4288 stop_waiting (ecs);
36fa8042
PA
4289 return;
4290 }
4291
4292 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4293 && stop_after_trap)
4294 {
4295 if (!ptid_equal (ecs->ptid, inferior_ptid))
4296 context_switch (ecs->ptid);
4297 if (debug_infrun)
4298 fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
4299 stop_print_frame = 0;
22bcd14b 4300 stop_waiting (ecs);
36fa8042
PA
4301 return;
4302 }
4303
4304 /* This originates from attach_command(). We need to overwrite
4305 the stop_signal here, because some kernels don't ignore a
4306 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
4307 See more comments in inferior.h. On the other hand, if we
4308 get a non-SIGSTOP, report it to the user - assume the backend
4309 will handle the SIGSTOP if it should show up later.
4310
4311 Also consider that the attach is complete when we see a
4312 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
4313 target extended-remote report it instead of a SIGSTOP
4314 (e.g. gdbserver). We already rely on SIGTRAP being our
4315 signal, so this is no exception.
4316
4317 Also consider that the attach is complete when we see a
4318 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
4319 the target to stop all threads of the inferior, in case the
4320 low level attach operation doesn't stop them implicitly. If
4321 they weren't stopped implicitly, then the stub will report a
4322 GDB_SIGNAL_0, meaning: stopped for no particular reason
4323 other than GDB's request. */
4324 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
4325 && (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_STOP
4326 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4327 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_0))
4328 {
4329 stop_print_frame = 1;
22bcd14b 4330 stop_waiting (ecs);
36fa8042
PA
4331 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
4332 return;
4333 }
4334
488f131b 4335 /* See if something interesting happened to the non-current thread. If
b40c7d58
DJ
4336 so, then switch to that thread. */
4337 if (!ptid_equal (ecs->ptid, inferior_ptid))
488f131b 4338 {
527159b7 4339 if (debug_infrun)
8a9de0e4 4340 fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
527159b7 4341
0d1e5fa7 4342 context_switch (ecs->ptid);
c5aa993b 4343
9a4105ab
AC
4344 if (deprecated_context_hook)
4345 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
488f131b 4346 }
c906108c 4347
568d6575
UW
4348 /* At this point, get hold of the now-current thread's frame. */
4349 frame = get_current_frame ();
4350 gdbarch = get_frame_arch (frame);
4351
2adfaa28 4352 /* Pull the single step breakpoints out of the target. */
af48d08f 4353 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
488f131b 4354 {
af48d08f
PA
4355 struct regcache *regcache;
4356 struct address_space *aspace;
4357 CORE_ADDR pc;
2adfaa28 4358
af48d08f
PA
4359 regcache = get_thread_regcache (ecs->ptid);
4360 aspace = get_regcache_aspace (regcache);
4361 pc = regcache_read_pc (regcache);
34b7e8a6 4362
af48d08f
PA
4363 /* However, before doing so, if this single-step breakpoint was
4364 actually for another thread, set this thread up for moving
4365 past it. */
4366 if (!thread_has_single_step_breakpoint_here (ecs->event_thread,
4367 aspace, pc))
4368 {
4369 if (single_step_breakpoint_inserted_here_p (aspace, pc))
2adfaa28
PA
4370 {
4371 if (debug_infrun)
4372 {
4373 fprintf_unfiltered (gdb_stdlog,
af48d08f 4374 "infrun: [%s] hit another thread's "
34b7e8a6
PA
4375 "single-step breakpoint\n",
4376 target_pid_to_str (ecs->ptid));
2adfaa28 4377 }
af48d08f
PA
4378 ecs->hit_singlestep_breakpoint = 1;
4379 }
4380 }
4381 else
4382 {
4383 if (debug_infrun)
4384 {
4385 fprintf_unfiltered (gdb_stdlog,
4386 "infrun: [%s] hit its "
4387 "single-step breakpoint\n",
4388 target_pid_to_str (ecs->ptid));
2adfaa28
PA
4389 }
4390 }
488f131b 4391 }
af48d08f 4392 delete_just_stopped_threads_single_step_breakpoints ();
c906108c 4393
963f9c80
PA
4394 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4395 && ecs->event_thread->control.trap_expected
4396 && ecs->event_thread->stepping_over_watchpoint)
d983da9c
DJ
4397 stopped_by_watchpoint = 0;
4398 else
4399 stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
4400
4401 /* If necessary, step over this watchpoint. We'll be back to display
4402 it in a moment. */
4403 if (stopped_by_watchpoint
d92524f1 4404 && (target_have_steppable_watchpoint
568d6575 4405 || gdbarch_have_nonsteppable_watchpoint (gdbarch)))
488f131b 4406 {
488f131b
JB
4407 /* At this point, we are stopped at an instruction which has
4408 attempted to write to a piece of memory under control of
4409 a watchpoint. The instruction hasn't actually executed
4410 yet. If we were to evaluate the watchpoint expression
4411 now, we would get the old value, and therefore no change
4412 would seem to have occurred.
4413
4414 In order to make watchpoints work `right', we really need
4415 to complete the memory write, and then evaluate the
d983da9c
DJ
4416 watchpoint expression. We do this by single-stepping the
4417 target.
4418
7f89fd65 4419 It may not be necessary to disable the watchpoint to step over
d983da9c
DJ
4420 it. For example, the PA can (with some kernel cooperation)
4421 single step over a watchpoint without disabling the watchpoint.
4422
4423 It is far more common to need to disable a watchpoint to step
4424 the inferior over it. If we have non-steppable watchpoints,
4425 we must disable the current watchpoint; it's simplest to
963f9c80
PA
4426 disable all watchpoints.
4427
4428 Any breakpoint at PC must also be stepped over -- if there's
4429 one, it will have already triggered before the watchpoint
4430 triggered, and we either already reported it to the user, or
4431 it didn't cause a stop and we called keep_going. In either
4432 case, if there was a breakpoint at PC, we must be trying to
4433 step past it. */
4434 ecs->event_thread->stepping_over_watchpoint = 1;
4435 keep_going (ecs);
488f131b
JB
4436 return;
4437 }
4438
4e1c45ea 4439 ecs->event_thread->stepping_over_breakpoint = 0;
963f9c80 4440 ecs->event_thread->stepping_over_watchpoint = 0;
16c381f0
JK
4441 bpstat_clear (&ecs->event_thread->control.stop_bpstat);
4442 ecs->event_thread->control.stop_step = 0;
488f131b 4443 stop_print_frame = 1;
488f131b 4444 stopped_by_random_signal = 0;
488f131b 4445
edb3359d
DJ
4446 /* Hide inlined functions starting here, unless we just performed stepi or
4447 nexti. After stepi and nexti, always show the innermost frame (not any
4448 inline function call sites). */
16c381f0 4449 if (ecs->event_thread->control.step_range_end != 1)
0574c78f
GB
4450 {
4451 struct address_space *aspace =
4452 get_regcache_aspace (get_thread_regcache (ecs->ptid));
4453
4454 /* skip_inline_frames is expensive, so we avoid it if we can
4455 determine that the address is one where functions cannot have
4456 been inlined. This improves performance with inferiors that
4457 load a lot of shared libraries, because the solib event
4458 breakpoint is defined as the address of a function (i.e. not
4459 inline). Note that we have to check the previous PC as well
4460 as the current one to catch cases when we have just
4461 single-stepped off a breakpoint prior to reinstating it.
4462 Note that we're assuming that the code we single-step to is
4463 not inline, but that's not definitive: there's nothing
4464 preventing the event breakpoint function from containing
4465 inlined code, and the single-step ending up there. If the
4466 user had set a breakpoint on that inlined code, the missing
4467 skip_inline_frames call would break things. Fortunately
4468 that's an extremely unlikely scenario. */
09ac7c10 4469 if (!pc_at_non_inline_function (aspace, stop_pc, &ecs->ws)
a210c238
MR
4470 && !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4471 && ecs->event_thread->control.trap_expected
4472 && pc_at_non_inline_function (aspace,
4473 ecs->event_thread->prev_pc,
09ac7c10 4474 &ecs->ws)))
1c5a993e
MR
4475 {
4476 skip_inline_frames (ecs->ptid);
4477
4478 /* Re-fetch current thread's frame in case that invalidated
4479 the frame cache. */
4480 frame = get_current_frame ();
4481 gdbarch = get_frame_arch (frame);
4482 }
0574c78f 4483 }
edb3359d 4484
a493e3e2 4485 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
16c381f0 4486 && ecs->event_thread->control.trap_expected
568d6575 4487 && gdbarch_single_step_through_delay_p (gdbarch)
4e1c45ea 4488 && currently_stepping (ecs->event_thread))
3352ef37 4489 {
b50d7442 4490 /* We're trying to step off a breakpoint. Turns out that we're
3352ef37 4491 also on an instruction that needs to be stepped multiple
1777feb0 4492 times before it's been fully executing. E.g., architectures
3352ef37
AC
4493 with a delay slot. It needs to be stepped twice, once for
4494 the instruction and once for the delay slot. */
4495 int step_through_delay
568d6575 4496 = gdbarch_single_step_through_delay (gdbarch, frame);
abbb1732 4497
527159b7 4498 if (debug_infrun && step_through_delay)
8a9de0e4 4499 fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
16c381f0
JK
4500 if (ecs->event_thread->control.step_range_end == 0
4501 && step_through_delay)
3352ef37
AC
4502 {
4503 /* The user issued a continue when stopped at a breakpoint.
4504 Set up for another trap and get out of here. */
4e1c45ea 4505 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
4506 keep_going (ecs);
4507 return;
4508 }
4509 else if (step_through_delay)
4510 {
4511 /* The user issued a step when stopped at a breakpoint.
4512 Maybe we should stop, maybe we should not - the delay
4513 slot *might* correspond to a line of source. In any
ca67fcb8
VP
4514 case, don't decide that here, just set
4515 ecs->stepping_over_breakpoint, making sure we
4516 single-step again before breakpoints are re-inserted. */
4e1c45ea 4517 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
4518 }
4519 }
4520
ab04a2af
TT
4521 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
4522 handles this event. */
4523 ecs->event_thread->control.stop_bpstat
4524 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
4525 stop_pc, ecs->ptid, &ecs->ws);
db82e815 4526
ab04a2af
TT
4527 /* Following in case break condition called a
4528 function. */
4529 stop_print_frame = 1;
73dd234f 4530
ab04a2af
TT
4531 /* This is where we handle "moribund" watchpoints. Unlike
4532 software breakpoints traps, hardware watchpoint traps are
4533 always distinguishable from random traps. If no high-level
4534 watchpoint is associated with the reported stop data address
4535 anymore, then the bpstat does not explain the signal ---
4536 simply make sure to ignore it if `stopped_by_watchpoint' is
4537 set. */
4538
4539 if (debug_infrun
4540 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
47591c29 4541 && !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
427cd150 4542 GDB_SIGNAL_TRAP)
ab04a2af
TT
4543 && stopped_by_watchpoint)
4544 fprintf_unfiltered (gdb_stdlog,
4545 "infrun: no user watchpoint explains "
4546 "watchpoint SIGTRAP, ignoring\n");
73dd234f 4547
bac7d97b 4548 /* NOTE: cagney/2003-03-29: These checks for a random signal
ab04a2af
TT
4549 at one stage in the past included checks for an inferior
4550 function call's call dummy's return breakpoint. The original
4551 comment, that went with the test, read:
03cebad2 4552
ab04a2af
TT
4553 ``End of a stack dummy. Some systems (e.g. Sony news) give
4554 another signal besides SIGTRAP, so check here as well as
4555 above.''
73dd234f 4556
ab04a2af
TT
4557 If someone ever tries to get call dummys on a
4558 non-executable stack to work (where the target would stop
4559 with something like a SIGSEGV), then those tests might need
4560 to be re-instated. Given, however, that the tests were only
4561 enabled when momentary breakpoints were not being used, I
4562 suspect that it won't be the case.
488f131b 4563
ab04a2af
TT
4564 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
4565 be necessary for call dummies on a non-executable stack on
4566 SPARC. */
488f131b 4567
bac7d97b 4568 /* See if the breakpoints module can explain the signal. */
47591c29
PA
4569 random_signal
4570 = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
4571 ecs->event_thread->suspend.stop_signal);
bac7d97b 4572
1cf4d951
PA
4573 /* Maybe this was a trap for a software breakpoint that has since
4574 been removed. */
4575 if (random_signal && target_stopped_by_sw_breakpoint ())
4576 {
4577 if (program_breakpoint_here_p (gdbarch, stop_pc))
4578 {
4579 struct regcache *regcache;
4580 int decr_pc;
4581
4582 /* Re-adjust PC to what the program would see if GDB was not
4583 debugging it. */
4584 regcache = get_thread_regcache (ecs->event_thread->ptid);
527a273a 4585 decr_pc = gdbarch_decr_pc_after_break (gdbarch);
1cf4d951
PA
4586 if (decr_pc != 0)
4587 {
4588 struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
4589
4590 if (record_full_is_used ())
4591 record_full_gdb_operation_disable_set ();
4592
4593 regcache_write_pc (regcache, stop_pc + decr_pc);
4594
4595 do_cleanups (old_cleanups);
4596 }
4597 }
4598 else
4599 {
4600 /* A delayed software breakpoint event. Ignore the trap. */
4601 if (debug_infrun)
4602 fprintf_unfiltered (gdb_stdlog,
4603 "infrun: delayed software breakpoint "
4604 "trap, ignoring\n");
4605 random_signal = 0;
4606 }
4607 }
4608
4609 /* Maybe this was a trap for a hardware breakpoint/watchpoint that
4610 has since been removed. */
4611 if (random_signal && target_stopped_by_hw_breakpoint ())
4612 {
4613 /* A delayed hardware breakpoint event. Ignore the trap. */
4614 if (debug_infrun)
4615 fprintf_unfiltered (gdb_stdlog,
4616 "infrun: delayed hardware breakpoint/watchpoint "
4617 "trap, ignoring\n");
4618 random_signal = 0;
4619 }
4620
bac7d97b
PA
4621 /* If not, perhaps stepping/nexting can. */
4622 if (random_signal)
4623 random_signal = !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4624 && currently_stepping (ecs->event_thread));
ab04a2af 4625
2adfaa28
PA
4626 /* Perhaps the thread hit a single-step breakpoint of _another_
4627 thread. Single-step breakpoints are transparent to the
4628 breakpoints module. */
4629 if (random_signal)
4630 random_signal = !ecs->hit_singlestep_breakpoint;
4631
bac7d97b
PA
4632 /* No? Perhaps we got a moribund watchpoint. */
4633 if (random_signal)
4634 random_signal = !stopped_by_watchpoint;
ab04a2af 4635
488f131b
JB
4636 /* For the program's own signals, act according to
4637 the signal handling tables. */
4638
ce12b012 4639 if (random_signal)
488f131b
JB
4640 {
4641 /* Signal not for debugging purposes. */
c9657e70 4642 struct inferior *inf = find_inferior_ptid (ecs->ptid);
c9737c08 4643 enum gdb_signal stop_signal = ecs->event_thread->suspend.stop_signal;
488f131b 4644
527159b7 4645 if (debug_infrun)
c9737c08
PA
4646 fprintf_unfiltered (gdb_stdlog, "infrun: random signal (%s)\n",
4647 gdb_signal_to_symbol_string (stop_signal));
527159b7 4648
488f131b
JB
4649 stopped_by_random_signal = 1;
4650
252fbfc8
PA
4651 /* Always stop on signals if we're either just gaining control
4652 of the program, or the user explicitly requested this thread
4653 to remain stopped. */
d6b48e9c 4654 if (stop_soon != NO_STOP_QUIETLY
252fbfc8 4655 || ecs->event_thread->stop_requested
24291992 4656 || (!inf->detaching
16c381f0 4657 && signal_stop_state (ecs->event_thread->suspend.stop_signal)))
488f131b 4658 {
22bcd14b 4659 stop_waiting (ecs);
488f131b
JB
4660 return;
4661 }
b57bacec
PA
4662
4663 /* Notify observers the signal has "handle print" set. Note we
4664 returned early above if stopping; normal_stop handles the
4665 printing in that case. */
4666 if (signal_print[ecs->event_thread->suspend.stop_signal])
4667 {
4668 /* The signal table tells us to print about this signal. */
4669 target_terminal_ours_for_output ();
4670 observer_notify_signal_received (ecs->event_thread->suspend.stop_signal);
4671 target_terminal_inferior ();
4672 }
488f131b
JB
4673
4674 /* Clear the signal if it should not be passed. */
16c381f0 4675 if (signal_program[ecs->event_thread->suspend.stop_signal] == 0)
a493e3e2 4676 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
488f131b 4677
fb14de7b 4678 if (ecs->event_thread->prev_pc == stop_pc
16c381f0 4679 && ecs->event_thread->control.trap_expected
8358c15c 4680 && ecs->event_thread->control.step_resume_breakpoint == NULL)
68f53502
AC
4681 {
4682 /* We were just starting a new sequence, attempting to
4683 single-step off of a breakpoint and expecting a SIGTRAP.
237fc4c9 4684 Instead this signal arrives. This signal will take us out
68f53502
AC
4685 of the stepping range so GDB needs to remember to, when
4686 the signal handler returns, resume stepping off that
4687 breakpoint. */
4688 /* To simplify things, "continue" is forced to use the same
4689 code paths as single-step - set a breakpoint at the
4690 signal return address and then, once hit, step off that
4691 breakpoint. */
237fc4c9
PA
4692 if (debug_infrun)
4693 fprintf_unfiltered (gdb_stdlog,
4694 "infrun: signal arrived while stepping over "
4695 "breakpoint\n");
d3169d93 4696
2c03e5be 4697 insert_hp_step_resume_breakpoint_at_frame (frame);
4e1c45ea 4698 ecs->event_thread->step_after_step_resume_breakpoint = 1;
2455069d
UW
4699 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4700 ecs->event_thread->control.trap_expected = 0;
d137e6dc
PA
4701
4702 /* If we were nexting/stepping some other thread, switch to
4703 it, so that we don't continue it, losing control. */
4704 if (!switch_back_to_stepped_thread (ecs))
4705 keep_going (ecs);
9d799f85 4706 return;
68f53502 4707 }
9d799f85 4708
e5f8a7cc
PA
4709 if (ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_0
4710 && (pc_in_thread_step_range (stop_pc, ecs->event_thread)
4711 || ecs->event_thread->control.step_range_end == 1)
edb3359d 4712 && frame_id_eq (get_stack_frame_id (frame),
16c381f0 4713 ecs->event_thread->control.step_stack_frame_id)
8358c15c 4714 && ecs->event_thread->control.step_resume_breakpoint == NULL)
d303a6c7
AC
4715 {
4716 /* The inferior is about to take a signal that will take it
4717 out of the single step range. Set a breakpoint at the
4718 current PC (which is presumably where the signal handler
4719 will eventually return) and then allow the inferior to
4720 run free.
4721
4722 Note that this is only needed for a signal delivered
4723 while in the single-step range. Nested signals aren't a
4724 problem as they eventually all return. */
237fc4c9
PA
4725 if (debug_infrun)
4726 fprintf_unfiltered (gdb_stdlog,
4727 "infrun: signal may take us out of "
4728 "single-step range\n");
4729
2c03e5be 4730 insert_hp_step_resume_breakpoint_at_frame (frame);
e5f8a7cc 4731 ecs->event_thread->step_after_step_resume_breakpoint = 1;
2455069d
UW
4732 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4733 ecs->event_thread->control.trap_expected = 0;
9d799f85
AC
4734 keep_going (ecs);
4735 return;
d303a6c7 4736 }
9d799f85
AC
4737
4738 /* Note: step_resume_breakpoint may be non-NULL. This occures
4739 when either there's a nested signal, or when there's a
4740 pending signal enabled just as the signal handler returns
4741 (leaving the inferior at the step-resume-breakpoint without
4742 actually executing it). Either way continue until the
4743 breakpoint is really hit. */
c447ac0b
PA
4744
4745 if (!switch_back_to_stepped_thread (ecs))
4746 {
4747 if (debug_infrun)
4748 fprintf_unfiltered (gdb_stdlog,
4749 "infrun: random signal, keep going\n");
4750
4751 keep_going (ecs);
4752 }
4753 return;
488f131b 4754 }
94c57d6a
PA
4755
4756 process_event_stop_test (ecs);
4757}
4758
4759/* Come here when we've got some debug event / signal we can explain
4760 (IOW, not a random signal), and test whether it should cause a
4761 stop, or whether we should resume the inferior (transparently).
4762 E.g., could be a breakpoint whose condition evaluates false; we
4763 could be still stepping within the line; etc. */
4764
4765static void
4766process_event_stop_test (struct execution_control_state *ecs)
4767{
4768 struct symtab_and_line stop_pc_sal;
4769 struct frame_info *frame;
4770 struct gdbarch *gdbarch;
cdaa5b73
PA
4771 CORE_ADDR jmp_buf_pc;
4772 struct bpstat_what what;
94c57d6a 4773
cdaa5b73 4774 /* Handle cases caused by hitting a breakpoint. */
611c83ae 4775
cdaa5b73
PA
4776 frame = get_current_frame ();
4777 gdbarch = get_frame_arch (frame);
fcf3daef 4778
cdaa5b73 4779 what = bpstat_what (ecs->event_thread->control.stop_bpstat);
611c83ae 4780
cdaa5b73
PA
4781 if (what.call_dummy)
4782 {
4783 stop_stack_dummy = what.call_dummy;
4784 }
186c406b 4785
cdaa5b73
PA
4786 /* If we hit an internal event that triggers symbol changes, the
4787 current frame will be invalidated within bpstat_what (e.g., if we
4788 hit an internal solib event). Re-fetch it. */
4789 frame = get_current_frame ();
4790 gdbarch = get_frame_arch (frame);
e2e4d78b 4791
cdaa5b73
PA
4792 switch (what.main_action)
4793 {
4794 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
4795 /* If we hit the breakpoint at longjmp while stepping, we
4796 install a momentary breakpoint at the target of the
4797 jmp_buf. */
186c406b 4798
cdaa5b73
PA
4799 if (debug_infrun)
4800 fprintf_unfiltered (gdb_stdlog,
4801 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
186c406b 4802
cdaa5b73 4803 ecs->event_thread->stepping_over_breakpoint = 1;
611c83ae 4804
cdaa5b73
PA
4805 if (what.is_longjmp)
4806 {
4807 struct value *arg_value;
4808
4809 /* If we set the longjmp breakpoint via a SystemTap probe,
4810 then use it to extract the arguments. The destination PC
4811 is the third argument to the probe. */
4812 arg_value = probe_safe_evaluate_at_pc (frame, 2);
4813 if (arg_value)
8fa0c4f8
AA
4814 {
4815 jmp_buf_pc = value_as_address (arg_value);
4816 jmp_buf_pc = gdbarch_addr_bits_remove (gdbarch, jmp_buf_pc);
4817 }
cdaa5b73
PA
4818 else if (!gdbarch_get_longjmp_target_p (gdbarch)
4819 || !gdbarch_get_longjmp_target (gdbarch,
4820 frame, &jmp_buf_pc))
e2e4d78b 4821 {
cdaa5b73
PA
4822 if (debug_infrun)
4823 fprintf_unfiltered (gdb_stdlog,
4824 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME "
4825 "(!gdbarch_get_longjmp_target)\n");
4826 keep_going (ecs);
4827 return;
e2e4d78b 4828 }
e2e4d78b 4829
cdaa5b73
PA
4830 /* Insert a breakpoint at resume address. */
4831 insert_longjmp_resume_breakpoint (gdbarch, jmp_buf_pc);
4832 }
4833 else
4834 check_exception_resume (ecs, frame);
4835 keep_going (ecs);
4836 return;
e81a37f7 4837
cdaa5b73
PA
4838 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
4839 {
4840 struct frame_info *init_frame;
e81a37f7 4841
cdaa5b73 4842 /* There are several cases to consider.
c906108c 4843
cdaa5b73
PA
4844 1. The initiating frame no longer exists. In this case we
4845 must stop, because the exception or longjmp has gone too
4846 far.
2c03e5be 4847
cdaa5b73
PA
4848 2. The initiating frame exists, and is the same as the
4849 current frame. We stop, because the exception or longjmp
4850 has been caught.
2c03e5be 4851
cdaa5b73
PA
4852 3. The initiating frame exists and is different from the
4853 current frame. This means the exception or longjmp has
4854 been caught beneath the initiating frame, so keep going.
c906108c 4855
cdaa5b73
PA
4856 4. longjmp breakpoint has been placed just to protect
4857 against stale dummy frames and user is not interested in
4858 stopping around longjmps. */
c5aa993b 4859
cdaa5b73
PA
4860 if (debug_infrun)
4861 fprintf_unfiltered (gdb_stdlog,
4862 "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
c5aa993b 4863
cdaa5b73
PA
4864 gdb_assert (ecs->event_thread->control.exception_resume_breakpoint
4865 != NULL);
4866 delete_exception_resume_breakpoint (ecs->event_thread);
c5aa993b 4867
cdaa5b73
PA
4868 if (what.is_longjmp)
4869 {
b67a2c6f 4870 check_longjmp_breakpoint_for_call_dummy (ecs->event_thread);
c5aa993b 4871
cdaa5b73 4872 if (!frame_id_p (ecs->event_thread->initiating_frame))
e5ef252a 4873 {
cdaa5b73
PA
4874 /* Case 4. */
4875 keep_going (ecs);
4876 return;
e5ef252a 4877 }
cdaa5b73 4878 }
c5aa993b 4879
cdaa5b73 4880 init_frame = frame_find_by_id (ecs->event_thread->initiating_frame);
527159b7 4881
cdaa5b73
PA
4882 if (init_frame)
4883 {
4884 struct frame_id current_id
4885 = get_frame_id (get_current_frame ());
4886 if (frame_id_eq (current_id,
4887 ecs->event_thread->initiating_frame))
4888 {
4889 /* Case 2. Fall through. */
4890 }
4891 else
4892 {
4893 /* Case 3. */
4894 keep_going (ecs);
4895 return;
4896 }
68f53502 4897 }
488f131b 4898
cdaa5b73
PA
4899 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
4900 exists. */
4901 delete_step_resume_breakpoint (ecs->event_thread);
e5ef252a 4902
bdc36728 4903 end_stepping_range (ecs);
cdaa5b73
PA
4904 }
4905 return;
e5ef252a 4906
cdaa5b73
PA
4907 case BPSTAT_WHAT_SINGLE:
4908 if (debug_infrun)
4909 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
4910 ecs->event_thread->stepping_over_breakpoint = 1;
4911 /* Still need to check other stuff, at least the case where we
4912 are stepping and step out of the right range. */
4913 break;
e5ef252a 4914
cdaa5b73
PA
4915 case BPSTAT_WHAT_STEP_RESUME:
4916 if (debug_infrun)
4917 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
e5ef252a 4918
cdaa5b73
PA
4919 delete_step_resume_breakpoint (ecs->event_thread);
4920 if (ecs->event_thread->control.proceed_to_finish
4921 && execution_direction == EXEC_REVERSE)
4922 {
4923 struct thread_info *tp = ecs->event_thread;
4924
4925 /* We are finishing a function in reverse, and just hit the
4926 step-resume breakpoint at the start address of the
4927 function, and we're almost there -- just need to back up
4928 by one more single-step, which should take us back to the
4929 function call. */
4930 tp->control.step_range_start = tp->control.step_range_end = 1;
4931 keep_going (ecs);
e5ef252a 4932 return;
cdaa5b73
PA
4933 }
4934 fill_in_stop_func (gdbarch, ecs);
4935 if (stop_pc == ecs->stop_func_start
4936 && execution_direction == EXEC_REVERSE)
4937 {
4938 /* We are stepping over a function call in reverse, and just
4939 hit the step-resume breakpoint at the start address of
4940 the function. Go back to single-stepping, which should
4941 take us back to the function call. */
4942 ecs->event_thread->stepping_over_breakpoint = 1;
4943 keep_going (ecs);
4944 return;
4945 }
4946 break;
e5ef252a 4947
cdaa5b73
PA
4948 case BPSTAT_WHAT_STOP_NOISY:
4949 if (debug_infrun)
4950 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
4951 stop_print_frame = 1;
e5ef252a 4952
99619bea
PA
4953 /* Assume the thread stopped for a breapoint. We'll still check
4954 whether a/the breakpoint is there when the thread is next
4955 resumed. */
4956 ecs->event_thread->stepping_over_breakpoint = 1;
e5ef252a 4957
22bcd14b 4958 stop_waiting (ecs);
cdaa5b73 4959 return;
e5ef252a 4960
cdaa5b73
PA
4961 case BPSTAT_WHAT_STOP_SILENT:
4962 if (debug_infrun)
4963 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
4964 stop_print_frame = 0;
e5ef252a 4965
99619bea
PA
4966 /* Assume the thread stopped for a breapoint. We'll still check
4967 whether a/the breakpoint is there when the thread is next
4968 resumed. */
4969 ecs->event_thread->stepping_over_breakpoint = 1;
22bcd14b 4970 stop_waiting (ecs);
cdaa5b73
PA
4971 return;
4972
4973 case BPSTAT_WHAT_HP_STEP_RESUME:
4974 if (debug_infrun)
4975 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_HP_STEP_RESUME\n");
4976
4977 delete_step_resume_breakpoint (ecs->event_thread);
4978 if (ecs->event_thread->step_after_step_resume_breakpoint)
4979 {
4980 /* Back when the step-resume breakpoint was inserted, we
4981 were trying to single-step off a breakpoint. Go back to
4982 doing that. */
4983 ecs->event_thread->step_after_step_resume_breakpoint = 0;
4984 ecs->event_thread->stepping_over_breakpoint = 1;
4985 keep_going (ecs);
4986 return;
e5ef252a 4987 }
cdaa5b73
PA
4988 break;
4989
4990 case BPSTAT_WHAT_KEEP_CHECKING:
4991 break;
e5ef252a 4992 }
c906108c 4993
af48d08f
PA
4994 /* If we stepped a permanent breakpoint and we had a high priority
4995 step-resume breakpoint for the address we stepped, but we didn't
4996 hit it, then we must have stepped into the signal handler. The
4997 step-resume was only necessary to catch the case of _not_
4998 stepping into the handler, so delete it, and fall through to
4999 checking whether the step finished. */
5000 if (ecs->event_thread->stepped_breakpoint)
5001 {
5002 struct breakpoint *sr_bp
5003 = ecs->event_thread->control.step_resume_breakpoint;
5004
8d707a12
PA
5005 if (sr_bp != NULL
5006 && sr_bp->loc->permanent
af48d08f
PA
5007 && sr_bp->type == bp_hp_step_resume
5008 && sr_bp->loc->address == ecs->event_thread->prev_pc)
5009 {
5010 if (debug_infrun)
5011 fprintf_unfiltered (gdb_stdlog,
5012 "infrun: stepped permanent breakpoint, stopped in "
5013 "handler\n");
5014 delete_step_resume_breakpoint (ecs->event_thread);
5015 ecs->event_thread->step_after_step_resume_breakpoint = 0;
5016 }
5017 }
5018
cdaa5b73
PA
5019 /* We come here if we hit a breakpoint but should not stop for it.
5020 Possibly we also were stepping and should stop for that. So fall
5021 through and test for stepping. But, if not stepping, do not
5022 stop. */
c906108c 5023
a7212384
UW
5024 /* In all-stop mode, if we're currently stepping but have stopped in
5025 some other thread, we need to switch back to the stepped thread. */
c447ac0b
PA
5026 if (switch_back_to_stepped_thread (ecs))
5027 return;
776f04fa 5028
8358c15c 5029 if (ecs->event_thread->control.step_resume_breakpoint)
488f131b 5030 {
527159b7 5031 if (debug_infrun)
d3169d93
DJ
5032 fprintf_unfiltered (gdb_stdlog,
5033 "infrun: step-resume breakpoint is inserted\n");
527159b7 5034
488f131b
JB
5035 /* Having a step-resume breakpoint overrides anything
5036 else having to do with stepping commands until
5037 that breakpoint is reached. */
488f131b
JB
5038 keep_going (ecs);
5039 return;
5040 }
c5aa993b 5041
16c381f0 5042 if (ecs->event_thread->control.step_range_end == 0)
488f131b 5043 {
527159b7 5044 if (debug_infrun)
8a9de0e4 5045 fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
488f131b 5046 /* Likewise if we aren't even stepping. */
488f131b
JB
5047 keep_going (ecs);
5048 return;
5049 }
c5aa993b 5050
4b7703ad
JB
5051 /* Re-fetch current thread's frame in case the code above caused
5052 the frame cache to be re-initialized, making our FRAME variable
5053 a dangling pointer. */
5054 frame = get_current_frame ();
628fe4e4 5055 gdbarch = get_frame_arch (frame);
7e324e48 5056 fill_in_stop_func (gdbarch, ecs);
4b7703ad 5057
488f131b 5058 /* If stepping through a line, keep going if still within it.
c906108c 5059
488f131b
JB
5060 Note that step_range_end is the address of the first instruction
5061 beyond the step range, and NOT the address of the last instruction
31410e84
MS
5062 within it!
5063
5064 Note also that during reverse execution, we may be stepping
5065 through a function epilogue and therefore must detect when
5066 the current-frame changes in the middle of a line. */
5067
ce4c476a 5068 if (pc_in_thread_step_range (stop_pc, ecs->event_thread)
31410e84 5069 && (execution_direction != EXEC_REVERSE
388a8562 5070 || frame_id_eq (get_frame_id (frame),
16c381f0 5071 ecs->event_thread->control.step_frame_id)))
488f131b 5072 {
527159b7 5073 if (debug_infrun)
5af949e3
UW
5074 fprintf_unfiltered
5075 (gdb_stdlog, "infrun: stepping inside range [%s-%s]\n",
16c381f0
JK
5076 paddress (gdbarch, ecs->event_thread->control.step_range_start),
5077 paddress (gdbarch, ecs->event_thread->control.step_range_end));
b2175913 5078
c1e36e3e
PA
5079 /* Tentatively re-enable range stepping; `resume' disables it if
5080 necessary (e.g., if we're stepping over a breakpoint or we
5081 have software watchpoints). */
5082 ecs->event_thread->control.may_range_step = 1;
5083
b2175913
MS
5084 /* When stepping backward, stop at beginning of line range
5085 (unless it's the function entry point, in which case
5086 keep going back to the call point). */
16c381f0 5087 if (stop_pc == ecs->event_thread->control.step_range_start
b2175913
MS
5088 && stop_pc != ecs->stop_func_start
5089 && execution_direction == EXEC_REVERSE)
bdc36728 5090 end_stepping_range (ecs);
b2175913
MS
5091 else
5092 keep_going (ecs);
5093
488f131b
JB
5094 return;
5095 }
c5aa993b 5096
488f131b 5097 /* We stepped out of the stepping range. */
c906108c 5098
488f131b 5099 /* If we are stepping at the source level and entered the runtime
388a8562
MS
5100 loader dynamic symbol resolution code...
5101
5102 EXEC_FORWARD: we keep on single stepping until we exit the run
5103 time loader code and reach the callee's address.
5104
5105 EXEC_REVERSE: we've already executed the callee (backward), and
5106 the runtime loader code is handled just like any other
5107 undebuggable function call. Now we need only keep stepping
5108 backward through the trampoline code, and that's handled further
5109 down, so there is nothing for us to do here. */
5110
5111 if (execution_direction != EXEC_REVERSE
16c381f0 5112 && ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
cfd8ab24 5113 && in_solib_dynsym_resolve_code (stop_pc))
488f131b 5114 {
4c8c40e6 5115 CORE_ADDR pc_after_resolver =
568d6575 5116 gdbarch_skip_solib_resolver (gdbarch, stop_pc);
c906108c 5117
527159b7 5118 if (debug_infrun)
3e43a32a
MS
5119 fprintf_unfiltered (gdb_stdlog,
5120 "infrun: stepped into dynsym resolve code\n");
527159b7 5121
488f131b
JB
5122 if (pc_after_resolver)
5123 {
5124 /* Set up a step-resume breakpoint at the address
5125 indicated by SKIP_SOLIB_RESOLVER. */
5126 struct symtab_and_line sr_sal;
abbb1732 5127
fe39c653 5128 init_sal (&sr_sal);
488f131b 5129 sr_sal.pc = pc_after_resolver;
6c95b8df 5130 sr_sal.pspace = get_frame_program_space (frame);
488f131b 5131
a6d9a66e
UW
5132 insert_step_resume_breakpoint_at_sal (gdbarch,
5133 sr_sal, null_frame_id);
c5aa993b 5134 }
c906108c 5135
488f131b
JB
5136 keep_going (ecs);
5137 return;
5138 }
c906108c 5139
16c381f0
JK
5140 if (ecs->event_thread->control.step_range_end != 1
5141 && (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
5142 || ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
568d6575 5143 && get_frame_type (frame) == SIGTRAMP_FRAME)
488f131b 5144 {
527159b7 5145 if (debug_infrun)
3e43a32a
MS
5146 fprintf_unfiltered (gdb_stdlog,
5147 "infrun: stepped into signal trampoline\n");
42edda50 5148 /* The inferior, while doing a "step" or "next", has ended up in
8fb3e588
AC
5149 a signal trampoline (either by a signal being delivered or by
5150 the signal handler returning). Just single-step until the
5151 inferior leaves the trampoline (either by calling the handler
5152 or returning). */
488f131b
JB
5153 keep_going (ecs);
5154 return;
5155 }
c906108c 5156
14132e89
MR
5157 /* If we're in the return path from a shared library trampoline,
5158 we want to proceed through the trampoline when stepping. */
5159 /* macro/2012-04-25: This needs to come before the subroutine
5160 call check below as on some targets return trampolines look
5161 like subroutine calls (MIPS16 return thunks). */
5162 if (gdbarch_in_solib_return_trampoline (gdbarch,
5163 stop_pc, ecs->stop_func_name)
5164 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
5165 {
5166 /* Determine where this trampoline returns. */
5167 CORE_ADDR real_stop_pc;
5168
5169 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
5170
5171 if (debug_infrun)
5172 fprintf_unfiltered (gdb_stdlog,
5173 "infrun: stepped into solib return tramp\n");
5174
5175 /* Only proceed through if we know where it's going. */
5176 if (real_stop_pc)
5177 {
5178 /* And put the step-breakpoint there and go until there. */
5179 struct symtab_and_line sr_sal;
5180
5181 init_sal (&sr_sal); /* initialize to zeroes */
5182 sr_sal.pc = real_stop_pc;
5183 sr_sal.section = find_pc_overlay (sr_sal.pc);
5184 sr_sal.pspace = get_frame_program_space (frame);
5185
5186 /* Do not specify what the fp should be when we stop since
5187 on some machines the prologue is where the new fp value
5188 is established. */
5189 insert_step_resume_breakpoint_at_sal (gdbarch,
5190 sr_sal, null_frame_id);
5191
5192 /* Restart without fiddling with the step ranges or
5193 other state. */
5194 keep_going (ecs);
5195 return;
5196 }
5197 }
5198
c17eaafe
DJ
5199 /* Check for subroutine calls. The check for the current frame
5200 equalling the step ID is not necessary - the check of the
5201 previous frame's ID is sufficient - but it is a common case and
5202 cheaper than checking the previous frame's ID.
14e60db5
DJ
5203
5204 NOTE: frame_id_eq will never report two invalid frame IDs as
5205 being equal, so to get into this block, both the current and
5206 previous frame must have valid frame IDs. */
005ca36a
JB
5207 /* The outer_frame_id check is a heuristic to detect stepping
5208 through startup code. If we step over an instruction which
5209 sets the stack pointer from an invalid value to a valid value,
5210 we may detect that as a subroutine call from the mythical
5211 "outermost" function. This could be fixed by marking
5212 outermost frames as !stack_p,code_p,special_p. Then the
5213 initial outermost frame, before sp was valid, would
ce6cca6d 5214 have code_addr == &_start. See the comment in frame_id_eq
005ca36a 5215 for more. */
edb3359d 5216 if (!frame_id_eq (get_stack_frame_id (frame),
16c381f0 5217 ecs->event_thread->control.step_stack_frame_id)
005ca36a 5218 && (frame_id_eq (frame_unwind_caller_id (get_current_frame ()),
16c381f0
JK
5219 ecs->event_thread->control.step_stack_frame_id)
5220 && (!frame_id_eq (ecs->event_thread->control.step_stack_frame_id,
005ca36a 5221 outer_frame_id)
885eeb5b
PA
5222 || (ecs->event_thread->control.step_start_function
5223 != find_pc_function (stop_pc)))))
488f131b 5224 {
95918acb 5225 CORE_ADDR real_stop_pc;
8fb3e588 5226
527159b7 5227 if (debug_infrun)
8a9de0e4 5228 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
527159b7 5229
b7a084be 5230 if (ecs->event_thread->control.step_over_calls == STEP_OVER_NONE)
95918acb
AC
5231 {
5232 /* I presume that step_over_calls is only 0 when we're
5233 supposed to be stepping at the assembly language level
5234 ("stepi"). Just stop. */
388a8562 5235 /* And this works the same backward as frontward. MVS */
bdc36728 5236 end_stepping_range (ecs);
95918acb
AC
5237 return;
5238 }
8fb3e588 5239
388a8562
MS
5240 /* Reverse stepping through solib trampolines. */
5241
5242 if (execution_direction == EXEC_REVERSE
16c381f0 5243 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
388a8562
MS
5244 && (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
5245 || (ecs->stop_func_start == 0
5246 && in_solib_dynsym_resolve_code (stop_pc))))
5247 {
5248 /* Any solib trampoline code can be handled in reverse
5249 by simply continuing to single-step. We have already
5250 executed the solib function (backwards), and a few
5251 steps will take us back through the trampoline to the
5252 caller. */
5253 keep_going (ecs);
5254 return;
5255 }
5256
16c381f0 5257 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
8567c30f 5258 {
b2175913
MS
5259 /* We're doing a "next".
5260
5261 Normal (forward) execution: set a breakpoint at the
5262 callee's return address (the address at which the caller
5263 will resume).
5264
5265 Reverse (backward) execution. set the step-resume
5266 breakpoint at the start of the function that we just
5267 stepped into (backwards), and continue to there. When we
6130d0b7 5268 get there, we'll need to single-step back to the caller. */
b2175913
MS
5269
5270 if (execution_direction == EXEC_REVERSE)
5271 {
acf9414f
JK
5272 /* If we're already at the start of the function, we've either
5273 just stepped backward into a single instruction function,
5274 or stepped back out of a signal handler to the first instruction
5275 of the function. Just keep going, which will single-step back
5276 to the caller. */
58c48e72 5277 if (ecs->stop_func_start != stop_pc && ecs->stop_func_start != 0)
acf9414f
JK
5278 {
5279 struct symtab_and_line sr_sal;
5280
5281 /* Normal function call return (static or dynamic). */
5282 init_sal (&sr_sal);
5283 sr_sal.pc = ecs->stop_func_start;
5284 sr_sal.pspace = get_frame_program_space (frame);
5285 insert_step_resume_breakpoint_at_sal (gdbarch,
5286 sr_sal, null_frame_id);
5287 }
b2175913
MS
5288 }
5289 else
568d6575 5290 insert_step_resume_breakpoint_at_caller (frame);
b2175913 5291
8567c30f
AC
5292 keep_going (ecs);
5293 return;
5294 }
a53c66de 5295
95918acb 5296 /* If we are in a function call trampoline (a stub between the
8fb3e588
AC
5297 calling routine and the real function), locate the real
5298 function. That's what tells us (a) whether we want to step
5299 into it at all, and (b) what prologue we want to run to the
5300 end of, if we do step into it. */
568d6575 5301 real_stop_pc = skip_language_trampoline (frame, stop_pc);
95918acb 5302 if (real_stop_pc == 0)
568d6575 5303 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
95918acb
AC
5304 if (real_stop_pc != 0)
5305 ecs->stop_func_start = real_stop_pc;
8fb3e588 5306
db5f024e 5307 if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
1b2bfbb9
RC
5308 {
5309 struct symtab_and_line sr_sal;
abbb1732 5310
1b2bfbb9
RC
5311 init_sal (&sr_sal);
5312 sr_sal.pc = ecs->stop_func_start;
6c95b8df 5313 sr_sal.pspace = get_frame_program_space (frame);
1b2bfbb9 5314
a6d9a66e
UW
5315 insert_step_resume_breakpoint_at_sal (gdbarch,
5316 sr_sal, null_frame_id);
8fb3e588
AC
5317 keep_going (ecs);
5318 return;
1b2bfbb9
RC
5319 }
5320
95918acb 5321 /* If we have line number information for the function we are
1bfeeb0f
JL
5322 thinking of stepping into and the function isn't on the skip
5323 list, step into it.
95918acb 5324
8fb3e588
AC
5325 If there are several symtabs at that PC (e.g. with include
5326 files), just want to know whether *any* of them have line
5327 numbers. find_pc_line handles this. */
95918acb
AC
5328 {
5329 struct symtab_and_line tmp_sal;
8fb3e588 5330
95918acb 5331 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
2b914b52 5332 if (tmp_sal.line != 0
85817405
JK
5333 && !function_name_is_marked_for_skip (ecs->stop_func_name,
5334 &tmp_sal))
95918acb 5335 {
b2175913 5336 if (execution_direction == EXEC_REVERSE)
568d6575 5337 handle_step_into_function_backward (gdbarch, ecs);
b2175913 5338 else
568d6575 5339 handle_step_into_function (gdbarch, ecs);
95918acb
AC
5340 return;
5341 }
5342 }
5343
5344 /* If we have no line number and the step-stop-if-no-debug is
8fb3e588
AC
5345 set, we stop the step so that the user has a chance to switch
5346 in assembly mode. */
16c381f0 5347 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
078130d0 5348 && step_stop_if_no_debug)
95918acb 5349 {
bdc36728 5350 end_stepping_range (ecs);
95918acb
AC
5351 return;
5352 }
5353
b2175913
MS
5354 if (execution_direction == EXEC_REVERSE)
5355 {
acf9414f
JK
5356 /* If we're already at the start of the function, we've either just
5357 stepped backward into a single instruction function without line
5358 number info, or stepped back out of a signal handler to the first
5359 instruction of the function without line number info. Just keep
5360 going, which will single-step back to the caller. */
5361 if (ecs->stop_func_start != stop_pc)
5362 {
5363 /* Set a breakpoint at callee's start address.
5364 From there we can step once and be back in the caller. */
5365 struct symtab_and_line sr_sal;
abbb1732 5366
acf9414f
JK
5367 init_sal (&sr_sal);
5368 sr_sal.pc = ecs->stop_func_start;
5369 sr_sal.pspace = get_frame_program_space (frame);
5370 insert_step_resume_breakpoint_at_sal (gdbarch,
5371 sr_sal, null_frame_id);
5372 }
b2175913
MS
5373 }
5374 else
5375 /* Set a breakpoint at callee's return address (the address
5376 at which the caller will resume). */
568d6575 5377 insert_step_resume_breakpoint_at_caller (frame);
b2175913 5378
95918acb 5379 keep_going (ecs);
488f131b 5380 return;
488f131b 5381 }
c906108c 5382
fdd654f3
MS
5383 /* Reverse stepping through solib trampolines. */
5384
5385 if (execution_direction == EXEC_REVERSE
16c381f0 5386 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
fdd654f3
MS
5387 {
5388 if (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
5389 || (ecs->stop_func_start == 0
5390 && in_solib_dynsym_resolve_code (stop_pc)))
5391 {
5392 /* Any solib trampoline code can be handled in reverse
5393 by simply continuing to single-step. We have already
5394 executed the solib function (backwards), and a few
5395 steps will take us back through the trampoline to the
5396 caller. */
5397 keep_going (ecs);
5398 return;
5399 }
5400 else if (in_solib_dynsym_resolve_code (stop_pc))
5401 {
5402 /* Stepped backward into the solib dynsym resolver.
5403 Set a breakpoint at its start and continue, then
5404 one more step will take us out. */
5405 struct symtab_and_line sr_sal;
abbb1732 5406
fdd654f3
MS
5407 init_sal (&sr_sal);
5408 sr_sal.pc = ecs->stop_func_start;
9d1807c3 5409 sr_sal.pspace = get_frame_program_space (frame);
fdd654f3
MS
5410 insert_step_resume_breakpoint_at_sal (gdbarch,
5411 sr_sal, null_frame_id);
5412 keep_going (ecs);
5413 return;
5414 }
5415 }
5416
2afb61aa 5417 stop_pc_sal = find_pc_line (stop_pc, 0);
7ed0fe66 5418
1b2bfbb9
RC
5419 /* NOTE: tausq/2004-05-24: This if block used to be done before all
5420 the trampoline processing logic, however, there are some trampolines
5421 that have no names, so we should do trampoline handling first. */
16c381f0 5422 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
7ed0fe66 5423 && ecs->stop_func_name == NULL
2afb61aa 5424 && stop_pc_sal.line == 0)
1b2bfbb9 5425 {
527159b7 5426 if (debug_infrun)
3e43a32a
MS
5427 fprintf_unfiltered (gdb_stdlog,
5428 "infrun: stepped into undebuggable function\n");
527159b7 5429
1b2bfbb9 5430 /* The inferior just stepped into, or returned to, an
7ed0fe66
DJ
5431 undebuggable function (where there is no debugging information
5432 and no line number corresponding to the address where the
1b2bfbb9
RC
5433 inferior stopped). Since we want to skip this kind of code,
5434 we keep going until the inferior returns from this
14e60db5
DJ
5435 function - unless the user has asked us not to (via
5436 set step-mode) or we no longer know how to get back
5437 to the call site. */
5438 if (step_stop_if_no_debug
c7ce8faa 5439 || !frame_id_p (frame_unwind_caller_id (frame)))
1b2bfbb9
RC
5440 {
5441 /* If we have no line number and the step-stop-if-no-debug
5442 is set, we stop the step so that the user has a chance to
5443 switch in assembly mode. */
bdc36728 5444 end_stepping_range (ecs);
1b2bfbb9
RC
5445 return;
5446 }
5447 else
5448 {
5449 /* Set a breakpoint at callee's return address (the address
5450 at which the caller will resume). */
568d6575 5451 insert_step_resume_breakpoint_at_caller (frame);
1b2bfbb9
RC
5452 keep_going (ecs);
5453 return;
5454 }
5455 }
5456
16c381f0 5457 if (ecs->event_thread->control.step_range_end == 1)
1b2bfbb9
RC
5458 {
5459 /* It is stepi or nexti. We always want to stop stepping after
5460 one instruction. */
527159b7 5461 if (debug_infrun)
8a9de0e4 5462 fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
bdc36728 5463 end_stepping_range (ecs);
1b2bfbb9
RC
5464 return;
5465 }
5466
2afb61aa 5467 if (stop_pc_sal.line == 0)
488f131b
JB
5468 {
5469 /* We have no line number information. That means to stop
5470 stepping (does this always happen right after one instruction,
5471 when we do "s" in a function with no line numbers,
5472 or can this happen as a result of a return or longjmp?). */
527159b7 5473 if (debug_infrun)
8a9de0e4 5474 fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
bdc36728 5475 end_stepping_range (ecs);
488f131b
JB
5476 return;
5477 }
c906108c 5478
edb3359d
DJ
5479 /* Look for "calls" to inlined functions, part one. If the inline
5480 frame machinery detected some skipped call sites, we have entered
5481 a new inline function. */
5482
5483 if (frame_id_eq (get_frame_id (get_current_frame ()),
16c381f0 5484 ecs->event_thread->control.step_frame_id)
edb3359d
DJ
5485 && inline_skipped_frames (ecs->ptid))
5486 {
5487 struct symtab_and_line call_sal;
5488
5489 if (debug_infrun)
5490 fprintf_unfiltered (gdb_stdlog,
5491 "infrun: stepped into inlined function\n");
5492
5493 find_frame_sal (get_current_frame (), &call_sal);
5494
16c381f0 5495 if (ecs->event_thread->control.step_over_calls != STEP_OVER_ALL)
edb3359d
DJ
5496 {
5497 /* For "step", we're going to stop. But if the call site
5498 for this inlined function is on the same source line as
5499 we were previously stepping, go down into the function
5500 first. Otherwise stop at the call site. */
5501
5502 if (call_sal.line == ecs->event_thread->current_line
5503 && call_sal.symtab == ecs->event_thread->current_symtab)
5504 step_into_inline_frame (ecs->ptid);
5505
bdc36728 5506 end_stepping_range (ecs);
edb3359d
DJ
5507 return;
5508 }
5509 else
5510 {
5511 /* For "next", we should stop at the call site if it is on a
5512 different source line. Otherwise continue through the
5513 inlined function. */
5514 if (call_sal.line == ecs->event_thread->current_line
5515 && call_sal.symtab == ecs->event_thread->current_symtab)
5516 keep_going (ecs);
5517 else
bdc36728 5518 end_stepping_range (ecs);
edb3359d
DJ
5519 return;
5520 }
5521 }
5522
5523 /* Look for "calls" to inlined functions, part two. If we are still
5524 in the same real function we were stepping through, but we have
5525 to go further up to find the exact frame ID, we are stepping
5526 through a more inlined call beyond its call site. */
5527
5528 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
5529 && !frame_id_eq (get_frame_id (get_current_frame ()),
16c381f0 5530 ecs->event_thread->control.step_frame_id)
edb3359d 5531 && stepped_in_from (get_current_frame (),
16c381f0 5532 ecs->event_thread->control.step_frame_id))
edb3359d
DJ
5533 {
5534 if (debug_infrun)
5535 fprintf_unfiltered (gdb_stdlog,
5536 "infrun: stepping through inlined function\n");
5537
16c381f0 5538 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
edb3359d
DJ
5539 keep_going (ecs);
5540 else
bdc36728 5541 end_stepping_range (ecs);
edb3359d
DJ
5542 return;
5543 }
5544
2afb61aa 5545 if ((stop_pc == stop_pc_sal.pc)
4e1c45ea
PA
5546 && (ecs->event_thread->current_line != stop_pc_sal.line
5547 || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
488f131b
JB
5548 {
5549 /* We are at the start of a different line. So stop. Note that
5550 we don't stop if we step into the middle of a different line.
5551 That is said to make things like for (;;) statements work
5552 better. */
527159b7 5553 if (debug_infrun)
3e43a32a
MS
5554 fprintf_unfiltered (gdb_stdlog,
5555 "infrun: stepped to a different line\n");
bdc36728 5556 end_stepping_range (ecs);
488f131b
JB
5557 return;
5558 }
c906108c 5559
488f131b 5560 /* We aren't done stepping.
c906108c 5561
488f131b
JB
5562 Optimize by setting the stepping range to the line.
5563 (We might not be in the original line, but if we entered a
5564 new line in mid-statement, we continue stepping. This makes
5565 things like for(;;) statements work better.) */
c906108c 5566
16c381f0
JK
5567 ecs->event_thread->control.step_range_start = stop_pc_sal.pc;
5568 ecs->event_thread->control.step_range_end = stop_pc_sal.end;
c1e36e3e 5569 ecs->event_thread->control.may_range_step = 1;
edb3359d 5570 set_step_info (frame, stop_pc_sal);
488f131b 5571
527159b7 5572 if (debug_infrun)
8a9de0e4 5573 fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
488f131b 5574 keep_going (ecs);
104c1213
JM
5575}
5576
c447ac0b
PA
5577/* In all-stop mode, if we're currently stepping but have stopped in
5578 some other thread, we may need to switch back to the stepped
5579 thread. Returns true we set the inferior running, false if we left
5580 it stopped (and the event needs further processing). */
5581
5582static int
5583switch_back_to_stepped_thread (struct execution_control_state *ecs)
5584{
5585 if (!non_stop)
5586 {
5587 struct thread_info *tp;
99619bea 5588 struct thread_info *stepping_thread;
483805cf 5589 struct thread_info *step_over;
99619bea
PA
5590
5591 /* If any thread is blocked on some internal breakpoint, and we
5592 simply need to step over that breakpoint to get it going
5593 again, do that first. */
5594
5595 /* However, if we see an event for the stepping thread, then we
5596 know all other threads have been moved past their breakpoints
5597 already. Let the caller check whether the step is finished,
5598 etc., before deciding to move it past a breakpoint. */
5599 if (ecs->event_thread->control.step_range_end != 0)
5600 return 0;
5601
5602 /* Check if the current thread is blocked on an incomplete
5603 step-over, interrupted by a random signal. */
5604 if (ecs->event_thread->control.trap_expected
5605 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
c447ac0b 5606 {
99619bea
PA
5607 if (debug_infrun)
5608 {
5609 fprintf_unfiltered (gdb_stdlog,
5610 "infrun: need to finish step-over of [%s]\n",
5611 target_pid_to_str (ecs->event_thread->ptid));
5612 }
5613 keep_going (ecs);
5614 return 1;
5615 }
2adfaa28 5616
99619bea
PA
5617 /* Check if the current thread is blocked by a single-step
5618 breakpoint of another thread. */
5619 if (ecs->hit_singlestep_breakpoint)
5620 {
5621 if (debug_infrun)
5622 {
5623 fprintf_unfiltered (gdb_stdlog,
5624 "infrun: need to step [%s] over single-step "
5625 "breakpoint\n",
5626 target_pid_to_str (ecs->ptid));
5627 }
5628 keep_going (ecs);
5629 return 1;
5630 }
5631
483805cf
PA
5632 /* Otherwise, we no longer expect a trap in the current thread.
5633 Clear the trap_expected flag before switching back -- this is
5634 what keep_going does as well, if we call it. */
5635 ecs->event_thread->control.trap_expected = 0;
5636
70509625
PA
5637 /* Likewise, clear the signal if it should not be passed. */
5638 if (!signal_program[ecs->event_thread->suspend.stop_signal])
5639 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
5640
483805cf
PA
5641 /* If scheduler locking applies even if not stepping, there's no
5642 need to walk over threads. Above we've checked whether the
5643 current thread is stepping. If some other thread not the
5644 event thread is stepping, then it must be that scheduler
5645 locking is not in effect. */
856e7dd6 5646 if (schedlock_applies (ecs->event_thread))
483805cf
PA
5647 return 0;
5648
5649 /* Look for the stepping/nexting thread, and check if any other
5650 thread other than the stepping thread needs to start a
5651 step-over. Do all step-overs before actually proceeding with
5652 step/next/etc. */
5653 stepping_thread = NULL;
5654 step_over = NULL;
034f788c 5655 ALL_NON_EXITED_THREADS (tp)
483805cf
PA
5656 {
5657 /* Ignore threads of processes we're not resuming. */
5658 if (!sched_multi
5659 && ptid_get_pid (tp->ptid) != ptid_get_pid (inferior_ptid))
5660 continue;
5661
5662 /* When stepping over a breakpoint, we lock all threads
5663 except the one that needs to move past the breakpoint.
5664 If a non-event thread has this set, the "incomplete
5665 step-over" check above should have caught it earlier. */
5666 gdb_assert (!tp->control.trap_expected);
5667
5668 /* Did we find the stepping thread? */
5669 if (tp->control.step_range_end)
5670 {
5671 /* Yep. There should only one though. */
5672 gdb_assert (stepping_thread == NULL);
5673
5674 /* The event thread is handled at the top, before we
5675 enter this loop. */
5676 gdb_assert (tp != ecs->event_thread);
5677
5678 /* If some thread other than the event thread is
5679 stepping, then scheduler locking can't be in effect,
5680 otherwise we wouldn't have resumed the current event
5681 thread in the first place. */
856e7dd6 5682 gdb_assert (!schedlock_applies (tp));
483805cf
PA
5683
5684 stepping_thread = tp;
5685 }
5686 else if (thread_still_needs_step_over (tp))
5687 {
5688 step_over = tp;
5689
5690 /* At the top we've returned early if the event thread
5691 is stepping. If some other thread not the event
5692 thread is stepping, then scheduler locking can't be
5693 in effect, and we can resume this thread. No need to
5694 keep looking for the stepping thread then. */
5695 break;
5696 }
5697 }
99619bea 5698
483805cf 5699 if (step_over != NULL)
99619bea 5700 {
483805cf 5701 tp = step_over;
99619bea 5702 if (debug_infrun)
c447ac0b 5703 {
99619bea
PA
5704 fprintf_unfiltered (gdb_stdlog,
5705 "infrun: need to step-over [%s]\n",
5706 target_pid_to_str (tp->ptid));
c447ac0b
PA
5707 }
5708
483805cf 5709 /* Only the stepping thread should have this set. */
99619bea
PA
5710 gdb_assert (tp->control.step_range_end == 0);
5711
99619bea
PA
5712 ecs->ptid = tp->ptid;
5713 ecs->event_thread = tp;
5714 switch_to_thread (ecs->ptid);
5715 keep_going (ecs);
5716 return 1;
5717 }
5718
483805cf 5719 if (stepping_thread != NULL)
99619bea
PA
5720 {
5721 struct frame_info *frame;
5722 struct gdbarch *gdbarch;
5723
483805cf
PA
5724 tp = stepping_thread;
5725
c447ac0b
PA
5726 /* If the stepping thread exited, then don't try to switch
5727 back and resume it, which could fail in several different
5728 ways depending on the target. Instead, just keep going.
5729
5730 We can find a stepping dead thread in the thread list in
5731 two cases:
5732
5733 - The target supports thread exit events, and when the
5734 target tries to delete the thread from the thread list,
5735 inferior_ptid pointed at the exiting thread. In such
5736 case, calling delete_thread does not really remove the
5737 thread from the list; instead, the thread is left listed,
5738 with 'exited' state.
5739
5740 - The target's debug interface does not support thread
5741 exit events, and so we have no idea whatsoever if the
5742 previously stepping thread is still alive. For that
5743 reason, we need to synchronously query the target
5744 now. */
5745 if (is_exited (tp->ptid)
5746 || !target_thread_alive (tp->ptid))
5747 {
5748 if (debug_infrun)
5749 fprintf_unfiltered (gdb_stdlog,
5750 "infrun: not switching back to "
5751 "stepped thread, it has vanished\n");
5752
5753 delete_thread (tp->ptid);
5754 keep_going (ecs);
5755 return 1;
5756 }
5757
c447ac0b
PA
5758 if (debug_infrun)
5759 fprintf_unfiltered (gdb_stdlog,
5760 "infrun: switching back to stepped thread\n");
5761
5762 ecs->event_thread = tp;
5763 ecs->ptid = tp->ptid;
5764 context_switch (ecs->ptid);
2adfaa28
PA
5765
5766 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
5767 frame = get_current_frame ();
5768 gdbarch = get_frame_arch (frame);
5769
5770 /* If the PC of the thread we were trying to single-step has
99619bea
PA
5771 changed, then that thread has trapped or been signaled,
5772 but the event has not been reported to GDB yet. Re-poll
5773 the target looking for this particular thread's event
5774 (i.e. temporarily enable schedlock) by:
2adfaa28
PA
5775
5776 - setting a break at the current PC
5777 - resuming that particular thread, only (by setting
5778 trap expected)
5779
5780 This prevents us continuously moving the single-step
5781 breakpoint forward, one instruction at a time,
5782 overstepping. */
5783
af48d08f 5784 if (stop_pc != tp->prev_pc)
2adfaa28 5785 {
64ce06e4
PA
5786 ptid_t resume_ptid;
5787
2adfaa28
PA
5788 if (debug_infrun)
5789 fprintf_unfiltered (gdb_stdlog,
5790 "infrun: expected thread advanced also\n");
5791
7c16b83e
PA
5792 /* Clear the info of the previous step-over, as it's no
5793 longer valid. It's what keep_going would do too, if
5794 we called it. Must do this before trying to insert
5795 the sss breakpoint, otherwise if we were previously
5796 trying to step over this exact address in another
5797 thread, the breakpoint ends up not installed. */
5798 clear_step_over_info ();
5799
2adfaa28
PA
5800 insert_single_step_breakpoint (get_frame_arch (frame),
5801 get_frame_address_space (frame),
5802 stop_pc);
2adfaa28 5803
64ce06e4
PA
5804 resume_ptid = user_visible_resume_ptid (tp->control.stepping_command);
5805 do_target_resume (resume_ptid,
5806 currently_stepping (tp), GDB_SIGNAL_0);
2adfaa28
PA
5807 prepare_to_wait (ecs);
5808 }
5809 else
5810 {
5811 if (debug_infrun)
5812 fprintf_unfiltered (gdb_stdlog,
5813 "infrun: expected thread still "
5814 "hasn't advanced\n");
5815 keep_going (ecs);
5816 }
5817
c447ac0b
PA
5818 return 1;
5819 }
5820 }
5821 return 0;
5822}
5823
b3444185 5824/* Is thread TP in the middle of single-stepping? */
104c1213 5825
a289b8f6 5826static int
b3444185 5827currently_stepping (struct thread_info *tp)
a7212384 5828{
8358c15c
JK
5829 return ((tp->control.step_range_end
5830 && tp->control.step_resume_breakpoint == NULL)
5831 || tp->control.trap_expected
af48d08f 5832 || tp->stepped_breakpoint
8358c15c 5833 || bpstat_should_step ());
a7212384
UW
5834}
5835
b2175913
MS
5836/* Inferior has stepped into a subroutine call with source code that
5837 we should not step over. Do step to the first line of code in
5838 it. */
c2c6d25f
JM
5839
5840static void
568d6575
UW
5841handle_step_into_function (struct gdbarch *gdbarch,
5842 struct execution_control_state *ecs)
c2c6d25f 5843{
43f3e411 5844 struct compunit_symtab *cust;
2afb61aa 5845 struct symtab_and_line stop_func_sal, sr_sal;
c2c6d25f 5846
7e324e48
GB
5847 fill_in_stop_func (gdbarch, ecs);
5848
43f3e411
DE
5849 cust = find_pc_compunit_symtab (stop_pc);
5850 if (cust != NULL && compunit_language (cust) != language_asm)
568d6575 5851 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
b2175913 5852 ecs->stop_func_start);
c2c6d25f 5853
2afb61aa 5854 stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
c2c6d25f
JM
5855 /* Use the step_resume_break to step until the end of the prologue,
5856 even if that involves jumps (as it seems to on the vax under
5857 4.2). */
5858 /* If the prologue ends in the middle of a source line, continue to
5859 the end of that source line (if it is still within the function).
5860 Otherwise, just go to end of prologue. */
2afb61aa
PA
5861 if (stop_func_sal.end
5862 && stop_func_sal.pc != ecs->stop_func_start
5863 && stop_func_sal.end < ecs->stop_func_end)
5864 ecs->stop_func_start = stop_func_sal.end;
c2c6d25f 5865
2dbd5e30
KB
5866 /* Architectures which require breakpoint adjustment might not be able
5867 to place a breakpoint at the computed address. If so, the test
5868 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
5869 ecs->stop_func_start to an address at which a breakpoint may be
5870 legitimately placed.
8fb3e588 5871
2dbd5e30
KB
5872 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
5873 made, GDB will enter an infinite loop when stepping through
5874 optimized code consisting of VLIW instructions which contain
5875 subinstructions corresponding to different source lines. On
5876 FR-V, it's not permitted to place a breakpoint on any but the
5877 first subinstruction of a VLIW instruction. When a breakpoint is
5878 set, GDB will adjust the breakpoint address to the beginning of
5879 the VLIW instruction. Thus, we need to make the corresponding
5880 adjustment here when computing the stop address. */
8fb3e588 5881
568d6575 5882 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
2dbd5e30
KB
5883 {
5884 ecs->stop_func_start
568d6575 5885 = gdbarch_adjust_breakpoint_address (gdbarch,
8fb3e588 5886 ecs->stop_func_start);
2dbd5e30
KB
5887 }
5888
c2c6d25f
JM
5889 if (ecs->stop_func_start == stop_pc)
5890 {
5891 /* We are already there: stop now. */
bdc36728 5892 end_stepping_range (ecs);
c2c6d25f
JM
5893 return;
5894 }
5895 else
5896 {
5897 /* Put the step-breakpoint there and go until there. */
fe39c653 5898 init_sal (&sr_sal); /* initialize to zeroes */
c2c6d25f
JM
5899 sr_sal.pc = ecs->stop_func_start;
5900 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
6c95b8df 5901 sr_sal.pspace = get_frame_program_space (get_current_frame ());
44cbf7b5 5902
c2c6d25f 5903 /* Do not specify what the fp should be when we stop since on
488f131b
JB
5904 some machines the prologue is where the new fp value is
5905 established. */
a6d9a66e 5906 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal, null_frame_id);
c2c6d25f
JM
5907
5908 /* And make sure stepping stops right away then. */
16c381f0
JK
5909 ecs->event_thread->control.step_range_end
5910 = ecs->event_thread->control.step_range_start;
c2c6d25f
JM
5911 }
5912 keep_going (ecs);
5913}
d4f3574e 5914
b2175913
MS
5915/* Inferior has stepped backward into a subroutine call with source
5916 code that we should not step over. Do step to the beginning of the
5917 last line of code in it. */
5918
5919static void
568d6575
UW
5920handle_step_into_function_backward (struct gdbarch *gdbarch,
5921 struct execution_control_state *ecs)
b2175913 5922{
43f3e411 5923 struct compunit_symtab *cust;
167e4384 5924 struct symtab_and_line stop_func_sal;
b2175913 5925
7e324e48
GB
5926 fill_in_stop_func (gdbarch, ecs);
5927
43f3e411
DE
5928 cust = find_pc_compunit_symtab (stop_pc);
5929 if (cust != NULL && compunit_language (cust) != language_asm)
568d6575 5930 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
b2175913
MS
5931 ecs->stop_func_start);
5932
5933 stop_func_sal = find_pc_line (stop_pc, 0);
5934
5935 /* OK, we're just going to keep stepping here. */
5936 if (stop_func_sal.pc == stop_pc)
5937 {
5938 /* We're there already. Just stop stepping now. */
bdc36728 5939 end_stepping_range (ecs);
b2175913
MS
5940 }
5941 else
5942 {
5943 /* Else just reset the step range and keep going.
5944 No step-resume breakpoint, they don't work for
5945 epilogues, which can have multiple entry paths. */
16c381f0
JK
5946 ecs->event_thread->control.step_range_start = stop_func_sal.pc;
5947 ecs->event_thread->control.step_range_end = stop_func_sal.end;
b2175913
MS
5948 keep_going (ecs);
5949 }
5950 return;
5951}
5952
d3169d93 5953/* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
44cbf7b5
AC
5954 This is used to both functions and to skip over code. */
5955
5956static void
2c03e5be
PA
5957insert_step_resume_breakpoint_at_sal_1 (struct gdbarch *gdbarch,
5958 struct symtab_and_line sr_sal,
5959 struct frame_id sr_id,
5960 enum bptype sr_type)
44cbf7b5 5961{
611c83ae
PA
5962 /* There should never be more than one step-resume or longjmp-resume
5963 breakpoint per thread, so we should never be setting a new
44cbf7b5 5964 step_resume_breakpoint when one is already active. */
8358c15c 5965 gdb_assert (inferior_thread ()->control.step_resume_breakpoint == NULL);
2c03e5be 5966 gdb_assert (sr_type == bp_step_resume || sr_type == bp_hp_step_resume);
d3169d93
DJ
5967
5968 if (debug_infrun)
5969 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
5970 "infrun: inserting step-resume breakpoint at %s\n",
5971 paddress (gdbarch, sr_sal.pc));
d3169d93 5972
8358c15c 5973 inferior_thread ()->control.step_resume_breakpoint
2c03e5be
PA
5974 = set_momentary_breakpoint (gdbarch, sr_sal, sr_id, sr_type);
5975}
5976
9da8c2a0 5977void
2c03e5be
PA
5978insert_step_resume_breakpoint_at_sal (struct gdbarch *gdbarch,
5979 struct symtab_and_line sr_sal,
5980 struct frame_id sr_id)
5981{
5982 insert_step_resume_breakpoint_at_sal_1 (gdbarch,
5983 sr_sal, sr_id,
5984 bp_step_resume);
44cbf7b5 5985}
7ce450bd 5986
2c03e5be
PA
5987/* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
5988 This is used to skip a potential signal handler.
7ce450bd 5989
14e60db5
DJ
5990 This is called with the interrupted function's frame. The signal
5991 handler, when it returns, will resume the interrupted function at
5992 RETURN_FRAME.pc. */
d303a6c7
AC
5993
5994static void
2c03e5be 5995insert_hp_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
d303a6c7
AC
5996{
5997 struct symtab_and_line sr_sal;
a6d9a66e 5998 struct gdbarch *gdbarch;
d303a6c7 5999
f4c1edd8 6000 gdb_assert (return_frame != NULL);
d303a6c7
AC
6001 init_sal (&sr_sal); /* initialize to zeros */
6002
a6d9a66e 6003 gdbarch = get_frame_arch (return_frame);
568d6575 6004 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch, get_frame_pc (return_frame));
d303a6c7 6005 sr_sal.section = find_pc_overlay (sr_sal.pc);
6c95b8df 6006 sr_sal.pspace = get_frame_program_space (return_frame);
d303a6c7 6007
2c03e5be
PA
6008 insert_step_resume_breakpoint_at_sal_1 (gdbarch, sr_sal,
6009 get_stack_frame_id (return_frame),
6010 bp_hp_step_resume);
d303a6c7
AC
6011}
6012
2c03e5be
PA
6013/* Insert a "step-resume breakpoint" at the previous frame's PC. This
6014 is used to skip a function after stepping into it (for "next" or if
6015 the called function has no debugging information).
14e60db5
DJ
6016
6017 The current function has almost always been reached by single
6018 stepping a call or return instruction. NEXT_FRAME belongs to the
6019 current function, and the breakpoint will be set at the caller's
6020 resume address.
6021
6022 This is a separate function rather than reusing
2c03e5be 6023 insert_hp_step_resume_breakpoint_at_frame in order to avoid
14e60db5 6024 get_prev_frame, which may stop prematurely (see the implementation
c7ce8faa 6025 of frame_unwind_caller_id for an example). */
14e60db5
DJ
6026
6027static void
6028insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
6029{
6030 struct symtab_and_line sr_sal;
a6d9a66e 6031 struct gdbarch *gdbarch;
14e60db5
DJ
6032
6033 /* We shouldn't have gotten here if we don't know where the call site
6034 is. */
c7ce8faa 6035 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame)));
14e60db5
DJ
6036
6037 init_sal (&sr_sal); /* initialize to zeros */
6038
a6d9a66e 6039 gdbarch = frame_unwind_caller_arch (next_frame);
c7ce8faa
DJ
6040 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch,
6041 frame_unwind_caller_pc (next_frame));
14e60db5 6042 sr_sal.section = find_pc_overlay (sr_sal.pc);
6c95b8df 6043 sr_sal.pspace = frame_unwind_program_space (next_frame);
14e60db5 6044
a6d9a66e 6045 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal,
c7ce8faa 6046 frame_unwind_caller_id (next_frame));
14e60db5
DJ
6047}
6048
611c83ae
PA
6049/* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
6050 new breakpoint at the target of a jmp_buf. The handling of
6051 longjmp-resume uses the same mechanisms used for handling
6052 "step-resume" breakpoints. */
6053
6054static void
a6d9a66e 6055insert_longjmp_resume_breakpoint (struct gdbarch *gdbarch, CORE_ADDR pc)
611c83ae 6056{
e81a37f7
TT
6057 /* There should never be more than one longjmp-resume breakpoint per
6058 thread, so we should never be setting a new
611c83ae 6059 longjmp_resume_breakpoint when one is already active. */
e81a37f7 6060 gdb_assert (inferior_thread ()->control.exception_resume_breakpoint == NULL);
611c83ae
PA
6061
6062 if (debug_infrun)
6063 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
6064 "infrun: inserting longjmp-resume breakpoint at %s\n",
6065 paddress (gdbarch, pc));
611c83ae 6066
e81a37f7 6067 inferior_thread ()->control.exception_resume_breakpoint =
a6d9a66e 6068 set_momentary_breakpoint_at_pc (gdbarch, pc, bp_longjmp_resume);
611c83ae
PA
6069}
6070
186c406b
TT
6071/* Insert an exception resume breakpoint. TP is the thread throwing
6072 the exception. The block B is the block of the unwinder debug hook
6073 function. FRAME is the frame corresponding to the call to this
6074 function. SYM is the symbol of the function argument holding the
6075 target PC of the exception. */
6076
6077static void
6078insert_exception_resume_breakpoint (struct thread_info *tp,
3977b71f 6079 const struct block *b,
186c406b
TT
6080 struct frame_info *frame,
6081 struct symbol *sym)
6082{
492d29ea 6083 TRY
186c406b
TT
6084 {
6085 struct symbol *vsym;
6086 struct value *value;
6087 CORE_ADDR handler;
6088 struct breakpoint *bp;
6089
d12307c1
PMR
6090 vsym = lookup_symbol (SYMBOL_LINKAGE_NAME (sym), b, VAR_DOMAIN,
6091 NULL).symbol;
186c406b
TT
6092 value = read_var_value (vsym, frame);
6093 /* If the value was optimized out, revert to the old behavior. */
6094 if (! value_optimized_out (value))
6095 {
6096 handler = value_as_address (value);
6097
6098 if (debug_infrun)
6099 fprintf_unfiltered (gdb_stdlog,
6100 "infrun: exception resume at %lx\n",
6101 (unsigned long) handler);
6102
6103 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
6104 handler, bp_exception_resume);
c70a6932
JK
6105
6106 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
6107 frame = NULL;
6108
186c406b
TT
6109 bp->thread = tp->num;
6110 inferior_thread ()->control.exception_resume_breakpoint = bp;
6111 }
6112 }
492d29ea
PA
6113 CATCH (e, RETURN_MASK_ERROR)
6114 {
6115 /* We want to ignore errors here. */
6116 }
6117 END_CATCH
186c406b
TT
6118}
6119
28106bc2
SDJ
6120/* A helper for check_exception_resume that sets an
6121 exception-breakpoint based on a SystemTap probe. */
6122
6123static void
6124insert_exception_resume_from_probe (struct thread_info *tp,
729662a5 6125 const struct bound_probe *probe,
28106bc2
SDJ
6126 struct frame_info *frame)
6127{
6128 struct value *arg_value;
6129 CORE_ADDR handler;
6130 struct breakpoint *bp;
6131
6132 arg_value = probe_safe_evaluate_at_pc (frame, 1);
6133 if (!arg_value)
6134 return;
6135
6136 handler = value_as_address (arg_value);
6137
6138 if (debug_infrun)
6139 fprintf_unfiltered (gdb_stdlog,
6140 "infrun: exception resume at %s\n",
6bac7473 6141 paddress (get_objfile_arch (probe->objfile),
28106bc2
SDJ
6142 handler));
6143
6144 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
6145 handler, bp_exception_resume);
6146 bp->thread = tp->num;
6147 inferior_thread ()->control.exception_resume_breakpoint = bp;
6148}
6149
186c406b
TT
6150/* This is called when an exception has been intercepted. Check to
6151 see whether the exception's destination is of interest, and if so,
6152 set an exception resume breakpoint there. */
6153
6154static void
6155check_exception_resume (struct execution_control_state *ecs,
28106bc2 6156 struct frame_info *frame)
186c406b 6157{
729662a5 6158 struct bound_probe probe;
28106bc2
SDJ
6159 struct symbol *func;
6160
6161 /* First see if this exception unwinding breakpoint was set via a
6162 SystemTap probe point. If so, the probe has two arguments: the
6163 CFA and the HANDLER. We ignore the CFA, extract the handler, and
6164 set a breakpoint there. */
6bac7473 6165 probe = find_probe_by_pc (get_frame_pc (frame));
729662a5 6166 if (probe.probe)
28106bc2 6167 {
729662a5 6168 insert_exception_resume_from_probe (ecs->event_thread, &probe, frame);
28106bc2
SDJ
6169 return;
6170 }
6171
6172 func = get_frame_function (frame);
6173 if (!func)
6174 return;
186c406b 6175
492d29ea 6176 TRY
186c406b 6177 {
3977b71f 6178 const struct block *b;
8157b174 6179 struct block_iterator iter;
186c406b
TT
6180 struct symbol *sym;
6181 int argno = 0;
6182
6183 /* The exception breakpoint is a thread-specific breakpoint on
6184 the unwinder's debug hook, declared as:
6185
6186 void _Unwind_DebugHook (void *cfa, void *handler);
6187
6188 The CFA argument indicates the frame to which control is
6189 about to be transferred. HANDLER is the destination PC.
6190
6191 We ignore the CFA and set a temporary breakpoint at HANDLER.
6192 This is not extremely efficient but it avoids issues in gdb
6193 with computing the DWARF CFA, and it also works even in weird
6194 cases such as throwing an exception from inside a signal
6195 handler. */
6196
6197 b = SYMBOL_BLOCK_VALUE (func);
6198 ALL_BLOCK_SYMBOLS (b, iter, sym)
6199 {
6200 if (!SYMBOL_IS_ARGUMENT (sym))
6201 continue;
6202
6203 if (argno == 0)
6204 ++argno;
6205 else
6206 {
6207 insert_exception_resume_breakpoint (ecs->event_thread,
6208 b, frame, sym);
6209 break;
6210 }
6211 }
6212 }
492d29ea
PA
6213 CATCH (e, RETURN_MASK_ERROR)
6214 {
6215 }
6216 END_CATCH
186c406b
TT
6217}
6218
104c1213 6219static void
22bcd14b 6220stop_waiting (struct execution_control_state *ecs)
104c1213 6221{
527159b7 6222 if (debug_infrun)
22bcd14b 6223 fprintf_unfiltered (gdb_stdlog, "infrun: stop_waiting\n");
527159b7 6224
31e77af2
PA
6225 clear_step_over_info ();
6226
cd0fc7c3
SS
6227 /* Let callers know we don't want to wait for the inferior anymore. */
6228 ecs->wait_some_more = 0;
6229}
6230
a9ba6bae
PA
6231/* Called when we should continue running the inferior, because the
6232 current event doesn't cause a user visible stop. This does the
6233 resuming part; waiting for the next event is done elsewhere. */
d4f3574e
SS
6234
6235static void
6236keep_going (struct execution_control_state *ecs)
6237{
c4dbc9af
PA
6238 /* Make sure normal_stop is called if we get a QUIT handled before
6239 reaching resume. */
6240 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
6241
d4f3574e 6242 /* Save the pc before execution, to compare with pc after stop. */
fb14de7b
UW
6243 ecs->event_thread->prev_pc
6244 = regcache_read_pc (get_thread_regcache (ecs->ptid));
d4f3574e 6245
16c381f0 6246 if (ecs->event_thread->control.trap_expected
a493e3e2 6247 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
d4f3574e 6248 {
a9ba6bae
PA
6249 /* We haven't yet gotten our trap, and either: intercepted a
6250 non-signal event (e.g., a fork); or took a signal which we
6251 are supposed to pass through to the inferior. Simply
6252 continue. */
c4dbc9af 6253 discard_cleanups (old_cleanups);
64ce06e4 6254 resume (ecs->event_thread->suspend.stop_signal);
d4f3574e
SS
6255 }
6256 else
6257 {
31e77af2 6258 struct regcache *regcache = get_current_regcache ();
963f9c80
PA
6259 int remove_bp;
6260 int remove_wps;
31e77af2 6261
d4f3574e 6262 /* Either the trap was not expected, but we are continuing
a9ba6bae
PA
6263 anyway (if we got a signal, the user asked it be passed to
6264 the child)
6265 -- or --
6266 We got our expected trap, but decided we should resume from
6267 it.
d4f3574e 6268
a9ba6bae 6269 We're going to run this baby now!
d4f3574e 6270
c36b740a
VP
6271 Note that insert_breakpoints won't try to re-insert
6272 already inserted breakpoints. Therefore, we don't
6273 care if breakpoints were already inserted, or not. */
a9ba6bae 6274
31e77af2
PA
6275 /* If we need to step over a breakpoint, and we're not using
6276 displaced stepping to do so, insert all breakpoints
6277 (watchpoints, etc.) but the one we're stepping over, step one
6278 instruction, and then re-insert the breakpoint when that step
6279 is finished. */
963f9c80
PA
6280
6281 remove_bp = (ecs->hit_singlestep_breakpoint
6282 || thread_still_needs_step_over (ecs->event_thread));
6283 remove_wps = (ecs->event_thread->stepping_over_watchpoint
6284 && !target_have_steppable_watchpoint);
6285
cb71640d
PA
6286 /* We can't use displaced stepping if we need to step past a
6287 watchpoint. The instruction copied to the scratch pad would
6288 still trigger the watchpoint. */
6289 if (remove_bp
6290 && (remove_wps
6291 || !use_displaced_stepping (get_regcache_arch (regcache))))
45e8c884 6292 {
31e77af2 6293 set_step_over_info (get_regcache_aspace (regcache),
963f9c80 6294 regcache_read_pc (regcache), remove_wps);
45e8c884 6295 }
963f9c80
PA
6296 else if (remove_wps)
6297 set_step_over_info (NULL, 0, remove_wps);
45e8c884 6298 else
31e77af2 6299 clear_step_over_info ();
abbb1732 6300
31e77af2 6301 /* Stop stepping if inserting breakpoints fails. */
492d29ea 6302 TRY
31e77af2
PA
6303 {
6304 insert_breakpoints ();
6305 }
492d29ea 6306 CATCH (e, RETURN_MASK_ERROR)
31e77af2
PA
6307 {
6308 exception_print (gdb_stderr, e);
22bcd14b 6309 stop_waiting (ecs);
de1fe8c8 6310 discard_cleanups (old_cleanups);
31e77af2 6311 return;
d4f3574e 6312 }
492d29ea 6313 END_CATCH
d4f3574e 6314
963f9c80 6315 ecs->event_thread->control.trap_expected = (remove_bp || remove_wps);
d4f3574e 6316
a9ba6bae
PA
6317 /* Do not deliver GDB_SIGNAL_TRAP (except when the user
6318 explicitly specifies that such a signal should be delivered
6319 to the target program). Typically, that would occur when a
6320 user is debugging a target monitor on a simulator: the target
6321 monitor sets a breakpoint; the simulator encounters this
6322 breakpoint and halts the simulation handing control to GDB;
6323 GDB, noting that the stop address doesn't map to any known
6324 breakpoint, returns control back to the simulator; the
6325 simulator then delivers the hardware equivalent of a
6326 GDB_SIGNAL_TRAP to the program being debugged. */
a493e3e2 6327 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
16c381f0 6328 && !signal_program[ecs->event_thread->suspend.stop_signal])
a493e3e2 6329 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
d4f3574e 6330
c4dbc9af 6331 discard_cleanups (old_cleanups);
64ce06e4 6332 resume (ecs->event_thread->suspend.stop_signal);
d4f3574e
SS
6333 }
6334
488f131b 6335 prepare_to_wait (ecs);
d4f3574e
SS
6336}
6337
104c1213
JM
6338/* This function normally comes after a resume, before
6339 handle_inferior_event exits. It takes care of any last bits of
6340 housekeeping, and sets the all-important wait_some_more flag. */
cd0fc7c3 6341
104c1213
JM
6342static void
6343prepare_to_wait (struct execution_control_state *ecs)
cd0fc7c3 6344{
527159b7 6345 if (debug_infrun)
8a9de0e4 6346 fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
104c1213 6347
104c1213
JM
6348 /* This is the old end of the while loop. Let everybody know we
6349 want to wait for the inferior some more and get called again
6350 soon. */
6351 ecs->wait_some_more = 1;
c906108c 6352}
11cf8741 6353
fd664c91 6354/* We are done with the step range of a step/next/si/ni command.
b57bacec 6355 Called once for each n of a "step n" operation. */
fd664c91
PA
6356
6357static void
bdc36728 6358end_stepping_range (struct execution_control_state *ecs)
fd664c91 6359{
bdc36728 6360 ecs->event_thread->control.stop_step = 1;
bdc36728 6361 stop_waiting (ecs);
fd664c91
PA
6362}
6363
33d62d64
JK
6364/* Several print_*_reason functions to print why the inferior has stopped.
6365 We always print something when the inferior exits, or receives a signal.
6366 The rest of the cases are dealt with later on in normal_stop and
6367 print_it_typical. Ideally there should be a call to one of these
6368 print_*_reason functions functions from handle_inferior_event each time
22bcd14b 6369 stop_waiting is called.
33d62d64 6370
fd664c91
PA
6371 Note that we don't call these directly, instead we delegate that to
6372 the interpreters, through observers. Interpreters then call these
6373 with whatever uiout is right. */
33d62d64 6374
fd664c91
PA
6375void
6376print_end_stepping_range_reason (struct ui_out *uiout)
33d62d64 6377{
fd664c91 6378 /* For CLI-like interpreters, print nothing. */
33d62d64 6379
fd664c91
PA
6380 if (ui_out_is_mi_like_p (uiout))
6381 {
6382 ui_out_field_string (uiout, "reason",
6383 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
6384 }
6385}
33d62d64 6386
fd664c91
PA
6387void
6388print_signal_exited_reason (struct ui_out *uiout, enum gdb_signal siggnal)
11cf8741 6389{
33d62d64
JK
6390 annotate_signalled ();
6391 if (ui_out_is_mi_like_p (uiout))
6392 ui_out_field_string
6393 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
6394 ui_out_text (uiout, "\nProgram terminated with signal ");
6395 annotate_signal_name ();
6396 ui_out_field_string (uiout, "signal-name",
2ea28649 6397 gdb_signal_to_name (siggnal));
33d62d64
JK
6398 annotate_signal_name_end ();
6399 ui_out_text (uiout, ", ");
6400 annotate_signal_string ();
6401 ui_out_field_string (uiout, "signal-meaning",
2ea28649 6402 gdb_signal_to_string (siggnal));
33d62d64
JK
6403 annotate_signal_string_end ();
6404 ui_out_text (uiout, ".\n");
6405 ui_out_text (uiout, "The program no longer exists.\n");
6406}
6407
fd664c91
PA
6408void
6409print_exited_reason (struct ui_out *uiout, int exitstatus)
33d62d64 6410{
fda326dd
TT
6411 struct inferior *inf = current_inferior ();
6412 const char *pidstr = target_pid_to_str (pid_to_ptid (inf->pid));
6413
33d62d64
JK
6414 annotate_exited (exitstatus);
6415 if (exitstatus)
6416 {
6417 if (ui_out_is_mi_like_p (uiout))
6418 ui_out_field_string (uiout, "reason",
6419 async_reason_lookup (EXEC_ASYNC_EXITED));
fda326dd
TT
6420 ui_out_text (uiout, "[Inferior ");
6421 ui_out_text (uiout, plongest (inf->num));
6422 ui_out_text (uiout, " (");
6423 ui_out_text (uiout, pidstr);
6424 ui_out_text (uiout, ") exited with code ");
33d62d64 6425 ui_out_field_fmt (uiout, "exit-code", "0%o", (unsigned int) exitstatus);
fda326dd 6426 ui_out_text (uiout, "]\n");
33d62d64
JK
6427 }
6428 else
11cf8741 6429 {
9dc5e2a9 6430 if (ui_out_is_mi_like_p (uiout))
034dad6f 6431 ui_out_field_string
33d62d64 6432 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
fda326dd
TT
6433 ui_out_text (uiout, "[Inferior ");
6434 ui_out_text (uiout, plongest (inf->num));
6435 ui_out_text (uiout, " (");
6436 ui_out_text (uiout, pidstr);
6437 ui_out_text (uiout, ") exited normally]\n");
33d62d64 6438 }
33d62d64
JK
6439}
6440
fd664c91
PA
6441void
6442print_signal_received_reason (struct ui_out *uiout, enum gdb_signal siggnal)
33d62d64
JK
6443{
6444 annotate_signal ();
6445
a493e3e2 6446 if (siggnal == GDB_SIGNAL_0 && !ui_out_is_mi_like_p (uiout))
33d62d64
JK
6447 {
6448 struct thread_info *t = inferior_thread ();
6449
6450 ui_out_text (uiout, "\n[");
6451 ui_out_field_string (uiout, "thread-name",
6452 target_pid_to_str (t->ptid));
6453 ui_out_field_fmt (uiout, "thread-id", "] #%d", t->num);
6454 ui_out_text (uiout, " stopped");
6455 }
6456 else
6457 {
6458 ui_out_text (uiout, "\nProgram received signal ");
8b93c638 6459 annotate_signal_name ();
33d62d64
JK
6460 if (ui_out_is_mi_like_p (uiout))
6461 ui_out_field_string
6462 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
488f131b 6463 ui_out_field_string (uiout, "signal-name",
2ea28649 6464 gdb_signal_to_name (siggnal));
8b93c638
JM
6465 annotate_signal_name_end ();
6466 ui_out_text (uiout, ", ");
6467 annotate_signal_string ();
488f131b 6468 ui_out_field_string (uiout, "signal-meaning",
2ea28649 6469 gdb_signal_to_string (siggnal));
8b93c638 6470 annotate_signal_string_end ();
33d62d64
JK
6471 }
6472 ui_out_text (uiout, ".\n");
6473}
252fbfc8 6474
fd664c91
PA
6475void
6476print_no_history_reason (struct ui_out *uiout)
33d62d64 6477{
fd664c91 6478 ui_out_text (uiout, "\nNo more reverse-execution history.\n");
11cf8741 6479}
43ff13b4 6480
0c7e1a46
PA
6481/* Print current location without a level number, if we have changed
6482 functions or hit a breakpoint. Print source line if we have one.
6483 bpstat_print contains the logic deciding in detail what to print,
6484 based on the event(s) that just occurred. */
6485
6486void
6487print_stop_event (struct target_waitstatus *ws)
6488{
6489 int bpstat_ret;
f486487f 6490 enum print_what source_flag;
0c7e1a46
PA
6491 int do_frame_printing = 1;
6492 struct thread_info *tp = inferior_thread ();
6493
6494 bpstat_ret = bpstat_print (tp->control.stop_bpstat, ws->kind);
6495 switch (bpstat_ret)
6496 {
6497 case PRINT_UNKNOWN:
6498 /* FIXME: cagney/2002-12-01: Given that a frame ID does (or
6499 should) carry around the function and does (or should) use
6500 that when doing a frame comparison. */
6501 if (tp->control.stop_step
6502 && frame_id_eq (tp->control.step_frame_id,
6503 get_frame_id (get_current_frame ()))
885eeb5b 6504 && tp->control.step_start_function == find_pc_function (stop_pc))
0c7e1a46
PA
6505 {
6506 /* Finished step, just print source line. */
6507 source_flag = SRC_LINE;
6508 }
6509 else
6510 {
6511 /* Print location and source line. */
6512 source_flag = SRC_AND_LOC;
6513 }
6514 break;
6515 case PRINT_SRC_AND_LOC:
6516 /* Print location and source line. */
6517 source_flag = SRC_AND_LOC;
6518 break;
6519 case PRINT_SRC_ONLY:
6520 source_flag = SRC_LINE;
6521 break;
6522 case PRINT_NOTHING:
6523 /* Something bogus. */
6524 source_flag = SRC_LINE;
6525 do_frame_printing = 0;
6526 break;
6527 default:
6528 internal_error (__FILE__, __LINE__, _("Unknown value."));
6529 }
6530
6531 /* The behavior of this routine with respect to the source
6532 flag is:
6533 SRC_LINE: Print only source line
6534 LOCATION: Print only location
6535 SRC_AND_LOC: Print location and source line. */
6536 if (do_frame_printing)
6537 print_stack_frame (get_selected_frame (NULL), 0, source_flag, 1);
6538
6539 /* Display the auto-display expressions. */
6540 do_displays ();
6541}
6542
c906108c
SS
6543/* Here to return control to GDB when the inferior stops for real.
6544 Print appropriate messages, remove breakpoints, give terminal our modes.
6545
6546 STOP_PRINT_FRAME nonzero means print the executing frame
6547 (pc, function, args, file, line number and line text).
6548 BREAKPOINTS_FAILED nonzero means stop was due to error
6549 attempting to insert breakpoints. */
6550
6551void
96baa820 6552normal_stop (void)
c906108c 6553{
73b65bb0
DJ
6554 struct target_waitstatus last;
6555 ptid_t last_ptid;
29f49a6a 6556 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
73b65bb0
DJ
6557
6558 get_last_target_status (&last_ptid, &last);
6559
29f49a6a
PA
6560 /* If an exception is thrown from this point on, make sure to
6561 propagate GDB's knowledge of the executing state to the
6562 frontend/user running state. A QUIT is an easy exception to see
6563 here, so do this before any filtered output. */
c35b1492
PA
6564 if (!non_stop)
6565 make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
6566 else if (last.kind != TARGET_WAITKIND_SIGNALLED
0e5bf2a8
PA
6567 && last.kind != TARGET_WAITKIND_EXITED
6568 && last.kind != TARGET_WAITKIND_NO_RESUMED)
c35b1492 6569 make_cleanup (finish_thread_state_cleanup, &inferior_ptid);
29f49a6a 6570
b57bacec
PA
6571 /* As we're presenting a stop, and potentially removing breakpoints,
6572 update the thread list so we can tell whether there are threads
6573 running on the target. With target remote, for example, we can
6574 only learn about new threads when we explicitly update the thread
6575 list. Do this before notifying the interpreters about signal
6576 stops, end of stepping ranges, etc., so that the "new thread"
6577 output is emitted before e.g., "Program received signal FOO",
6578 instead of after. */
6579 update_thread_list ();
6580
6581 if (last.kind == TARGET_WAITKIND_STOPPED && stopped_by_random_signal)
6582 observer_notify_signal_received (inferior_thread ()->suspend.stop_signal);
6583
c906108c
SS
6584 /* As with the notification of thread events, we want to delay
6585 notifying the user that we've switched thread context until
6586 the inferior actually stops.
6587
73b65bb0
DJ
6588 There's no point in saying anything if the inferior has exited.
6589 Note that SIGNALLED here means "exited with a signal", not
b65dc60b
PA
6590 "received a signal".
6591
6592 Also skip saying anything in non-stop mode. In that mode, as we
6593 don't want GDB to switch threads behind the user's back, to avoid
6594 races where the user is typing a command to apply to thread x,
6595 but GDB switches to thread y before the user finishes entering
6596 the command, fetch_inferior_event installs a cleanup to restore
6597 the current thread back to the thread the user had selected right
6598 after this event is handled, so we're not really switching, only
6599 informing of a stop. */
4f8d22e3
PA
6600 if (!non_stop
6601 && !ptid_equal (previous_inferior_ptid, inferior_ptid)
73b65bb0
DJ
6602 && target_has_execution
6603 && last.kind != TARGET_WAITKIND_SIGNALLED
0e5bf2a8
PA
6604 && last.kind != TARGET_WAITKIND_EXITED
6605 && last.kind != TARGET_WAITKIND_NO_RESUMED)
c906108c
SS
6606 {
6607 target_terminal_ours_for_output ();
a3f17187 6608 printf_filtered (_("[Switching to %s]\n"),
c95310c6 6609 target_pid_to_str (inferior_ptid));
b8fa951a 6610 annotate_thread_changed ();
39f77062 6611 previous_inferior_ptid = inferior_ptid;
c906108c 6612 }
c906108c 6613
0e5bf2a8
PA
6614 if (last.kind == TARGET_WAITKIND_NO_RESUMED)
6615 {
6616 gdb_assert (sync_execution || !target_can_async_p ());
6617
6618 target_terminal_ours_for_output ();
6619 printf_filtered (_("No unwaited-for children left.\n"));
6620 }
6621
b57bacec 6622 /* Note: this depends on the update_thread_list call above. */
a25a5a45 6623 if (!breakpoints_should_be_inserted_now () && target_has_execution)
c906108c
SS
6624 {
6625 if (remove_breakpoints ())
6626 {
6627 target_terminal_ours_for_output ();
3e43a32a
MS
6628 printf_filtered (_("Cannot remove breakpoints because "
6629 "program is no longer writable.\nFurther "
6630 "execution is probably impossible.\n"));
c906108c
SS
6631 }
6632 }
c906108c 6633
c906108c
SS
6634 /* If an auto-display called a function and that got a signal,
6635 delete that auto-display to avoid an infinite recursion. */
6636
6637 if (stopped_by_random_signal)
6638 disable_current_display ();
6639
b57bacec 6640 /* Notify observers if we finished a "step"-like command, etc. */
af679fd0
PA
6641 if (target_has_execution
6642 && last.kind != TARGET_WAITKIND_SIGNALLED
6643 && last.kind != TARGET_WAITKIND_EXITED
16c381f0 6644 && inferior_thread ()->control.stop_step)
b57bacec 6645 {
31cc0b80 6646 /* But not if in the middle of doing a "step n" operation for
b57bacec
PA
6647 n > 1 */
6648 if (inferior_thread ()->step_multi)
6649 goto done;
6650
6651 observer_notify_end_stepping_range ();
6652 }
c906108c
SS
6653
6654 target_terminal_ours ();
0f641c01 6655 async_enable_stdin ();
c906108c 6656
7abfe014
DJ
6657 /* Set the current source location. This will also happen if we
6658 display the frame below, but the current SAL will be incorrect
6659 during a user hook-stop function. */
d729566a 6660 if (has_stack_frames () && !stop_stack_dummy)
5166082f 6661 set_current_sal_from_frame (get_current_frame ());
7abfe014 6662
28bf096c
PA
6663 /* Let the user/frontend see the threads as stopped, but defer to
6664 call_function_by_hand if the thread finished an infcall
6665 successfully. We may be e.g., evaluating a breakpoint condition.
6666 In that case, the thread had state THREAD_RUNNING before the
6667 infcall, and shall remain marked running, all without informing
6668 the user/frontend about state transition changes. */
6669 if (target_has_execution
6670 && inferior_thread ()->control.in_infcall
6671 && stop_stack_dummy == STOP_STACK_DUMMY)
251bde03
PA
6672 discard_cleanups (old_chain);
6673 else
6674 do_cleanups (old_chain);
dd7e2d2b
PA
6675
6676 /* Look up the hook_stop and run it (CLI internally handles problem
6677 of stop_command's pre-hook not existing). */
6678 if (stop_command)
6679 catch_errors (hook_stop_stub, stop_command,
6680 "Error while running hook_stop:\n", RETURN_MASK_ALL);
6681
d729566a 6682 if (!has_stack_frames ())
d51fd4c8 6683 goto done;
c906108c 6684
32400beb
PA
6685 if (last.kind == TARGET_WAITKIND_SIGNALLED
6686 || last.kind == TARGET_WAITKIND_EXITED)
6687 goto done;
6688
c906108c
SS
6689 /* Select innermost stack frame - i.e., current frame is frame 0,
6690 and current location is based on that.
6691 Don't do this on return from a stack dummy routine,
1777feb0 6692 or if the program has exited. */
c906108c
SS
6693
6694 if (!stop_stack_dummy)
6695 {
0f7d239c 6696 select_frame (get_current_frame ());
c906108c 6697
d01a8610
AS
6698 /* If --batch-silent is enabled then there's no need to print the current
6699 source location, and to try risks causing an error message about
6700 missing source files. */
6701 if (stop_print_frame && !batch_silent)
0c7e1a46 6702 print_stop_event (&last);
c906108c
SS
6703 }
6704
aa7d318d 6705 if (stop_stack_dummy == STOP_STACK_DUMMY)
c906108c 6706 {
b89667eb
DE
6707 /* Pop the empty frame that contains the stack dummy.
6708 This also restores inferior state prior to the call
16c381f0 6709 (struct infcall_suspend_state). */
b89667eb 6710 struct frame_info *frame = get_current_frame ();
abbb1732 6711
b89667eb
DE
6712 gdb_assert (get_frame_type (frame) == DUMMY_FRAME);
6713 frame_pop (frame);
3e43a32a
MS
6714 /* frame_pop() calls reinit_frame_cache as the last thing it
6715 does which means there's currently no selected frame. We
6716 don't need to re-establish a selected frame if the dummy call
6717 returns normally, that will be done by
6718 restore_infcall_control_state. However, we do have to handle
6719 the case where the dummy call is returning after being
6720 stopped (e.g. the dummy call previously hit a breakpoint).
6721 We can't know which case we have so just always re-establish
6722 a selected frame here. */
0f7d239c 6723 select_frame (get_current_frame ());
c906108c
SS
6724 }
6725
c906108c
SS
6726done:
6727 annotate_stopped ();
41d2bdb4
PA
6728
6729 /* Suppress the stop observer if we're in the middle of:
6730
6731 - a step n (n > 1), as there still more steps to be done.
6732
6733 - a "finish" command, as the observer will be called in
6734 finish_command_continuation, so it can include the inferior
6735 function's return value.
6736
6737 - calling an inferior function, as we pretend we inferior didn't
6738 run at all. The return value of the call is handled by the
6739 expression evaluator, through call_function_by_hand. */
6740
6741 if (!target_has_execution
6742 || last.kind == TARGET_WAITKIND_SIGNALLED
6743 || last.kind == TARGET_WAITKIND_EXITED
0e5bf2a8 6744 || last.kind == TARGET_WAITKIND_NO_RESUMED
2ca0b532
PA
6745 || (!(inferior_thread ()->step_multi
6746 && inferior_thread ()->control.stop_step)
16c381f0
JK
6747 && !(inferior_thread ()->control.stop_bpstat
6748 && inferior_thread ()->control.proceed_to_finish)
6749 && !inferior_thread ()->control.in_infcall))
347bddb7
PA
6750 {
6751 if (!ptid_equal (inferior_ptid, null_ptid))
16c381f0 6752 observer_notify_normal_stop (inferior_thread ()->control.stop_bpstat,
1d33d6ba 6753 stop_print_frame);
347bddb7 6754 else
1d33d6ba 6755 observer_notify_normal_stop (NULL, stop_print_frame);
347bddb7 6756 }
347bddb7 6757
48844aa6
PA
6758 if (target_has_execution)
6759 {
6760 if (last.kind != TARGET_WAITKIND_SIGNALLED
6761 && last.kind != TARGET_WAITKIND_EXITED)
6762 /* Delete the breakpoint we stopped at, if it wants to be deleted.
6763 Delete any breakpoint that is to be deleted at the next stop. */
16c381f0 6764 breakpoint_auto_delete (inferior_thread ()->control.stop_bpstat);
94cc34af 6765 }
6c95b8df
PA
6766
6767 /* Try to get rid of automatically added inferiors that are no
6768 longer needed. Keeping those around slows down things linearly.
6769 Note that this never removes the current inferior. */
6770 prune_inferiors ();
c906108c
SS
6771}
6772
6773static int
96baa820 6774hook_stop_stub (void *cmd)
c906108c 6775{
5913bcb0 6776 execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
c906108c
SS
6777 return (0);
6778}
6779\f
c5aa993b 6780int
96baa820 6781signal_stop_state (int signo)
c906108c 6782{
d6b48e9c 6783 return signal_stop[signo];
c906108c
SS
6784}
6785
c5aa993b 6786int
96baa820 6787signal_print_state (int signo)
c906108c
SS
6788{
6789 return signal_print[signo];
6790}
6791
c5aa993b 6792int
96baa820 6793signal_pass_state (int signo)
c906108c
SS
6794{
6795 return signal_program[signo];
6796}
6797
2455069d
UW
6798static void
6799signal_cache_update (int signo)
6800{
6801 if (signo == -1)
6802 {
a493e3e2 6803 for (signo = 0; signo < (int) GDB_SIGNAL_LAST; signo++)
2455069d
UW
6804 signal_cache_update (signo);
6805
6806 return;
6807 }
6808
6809 signal_pass[signo] = (signal_stop[signo] == 0
6810 && signal_print[signo] == 0
ab04a2af
TT
6811 && signal_program[signo] == 1
6812 && signal_catch[signo] == 0);
2455069d
UW
6813}
6814
488f131b 6815int
7bda5e4a 6816signal_stop_update (int signo, int state)
d4f3574e
SS
6817{
6818 int ret = signal_stop[signo];
abbb1732 6819
d4f3574e 6820 signal_stop[signo] = state;
2455069d 6821 signal_cache_update (signo);
d4f3574e
SS
6822 return ret;
6823}
6824
488f131b 6825int
7bda5e4a 6826signal_print_update (int signo, int state)
d4f3574e
SS
6827{
6828 int ret = signal_print[signo];
abbb1732 6829
d4f3574e 6830 signal_print[signo] = state;
2455069d 6831 signal_cache_update (signo);
d4f3574e
SS
6832 return ret;
6833}
6834
488f131b 6835int
7bda5e4a 6836signal_pass_update (int signo, int state)
d4f3574e
SS
6837{
6838 int ret = signal_program[signo];
abbb1732 6839
d4f3574e 6840 signal_program[signo] = state;
2455069d 6841 signal_cache_update (signo);
d4f3574e
SS
6842 return ret;
6843}
6844
ab04a2af
TT
6845/* Update the global 'signal_catch' from INFO and notify the
6846 target. */
6847
6848void
6849signal_catch_update (const unsigned int *info)
6850{
6851 int i;
6852
6853 for (i = 0; i < GDB_SIGNAL_LAST; ++i)
6854 signal_catch[i] = info[i] > 0;
6855 signal_cache_update (-1);
6856 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
6857}
6858
c906108c 6859static void
96baa820 6860sig_print_header (void)
c906108c 6861{
3e43a32a
MS
6862 printf_filtered (_("Signal Stop\tPrint\tPass "
6863 "to program\tDescription\n"));
c906108c
SS
6864}
6865
6866static void
2ea28649 6867sig_print_info (enum gdb_signal oursig)
c906108c 6868{
2ea28649 6869 const char *name = gdb_signal_to_name (oursig);
c906108c 6870 int name_padding = 13 - strlen (name);
96baa820 6871
c906108c
SS
6872 if (name_padding <= 0)
6873 name_padding = 0;
6874
6875 printf_filtered ("%s", name);
488f131b 6876 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
c906108c
SS
6877 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
6878 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
6879 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
2ea28649 6880 printf_filtered ("%s\n", gdb_signal_to_string (oursig));
c906108c
SS
6881}
6882
6883/* Specify how various signals in the inferior should be handled. */
6884
6885static void
96baa820 6886handle_command (char *args, int from_tty)
c906108c
SS
6887{
6888 char **argv;
6889 int digits, wordlen;
6890 int sigfirst, signum, siglast;
2ea28649 6891 enum gdb_signal oursig;
c906108c
SS
6892 int allsigs;
6893 int nsigs;
6894 unsigned char *sigs;
6895 struct cleanup *old_chain;
6896
6897 if (args == NULL)
6898 {
e2e0b3e5 6899 error_no_arg (_("signal to handle"));
c906108c
SS
6900 }
6901
1777feb0 6902 /* Allocate and zero an array of flags for which signals to handle. */
c906108c 6903
a493e3e2 6904 nsigs = (int) GDB_SIGNAL_LAST;
c906108c
SS
6905 sigs = (unsigned char *) alloca (nsigs);
6906 memset (sigs, 0, nsigs);
6907
1777feb0 6908 /* Break the command line up into args. */
c906108c 6909
d1a41061 6910 argv = gdb_buildargv (args);
7a292a7a 6911 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
6912
6913 /* Walk through the args, looking for signal oursigs, signal names, and
6914 actions. Signal numbers and signal names may be interspersed with
6915 actions, with the actions being performed for all signals cumulatively
1777feb0 6916 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
c906108c
SS
6917
6918 while (*argv != NULL)
6919 {
6920 wordlen = strlen (*argv);
6921 for (digits = 0; isdigit ((*argv)[digits]); digits++)
6922 {;
6923 }
6924 allsigs = 0;
6925 sigfirst = siglast = -1;
6926
6927 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
6928 {
6929 /* Apply action to all signals except those used by the
1777feb0 6930 debugger. Silently skip those. */
c906108c
SS
6931 allsigs = 1;
6932 sigfirst = 0;
6933 siglast = nsigs - 1;
6934 }
6935 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
6936 {
6937 SET_SIGS (nsigs, sigs, signal_stop);
6938 SET_SIGS (nsigs, sigs, signal_print);
6939 }
6940 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
6941 {
6942 UNSET_SIGS (nsigs, sigs, signal_program);
6943 }
6944 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
6945 {
6946 SET_SIGS (nsigs, sigs, signal_print);
6947 }
6948 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
6949 {
6950 SET_SIGS (nsigs, sigs, signal_program);
6951 }
6952 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
6953 {
6954 UNSET_SIGS (nsigs, sigs, signal_stop);
6955 }
6956 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
6957 {
6958 SET_SIGS (nsigs, sigs, signal_program);
6959 }
6960 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
6961 {
6962 UNSET_SIGS (nsigs, sigs, signal_print);
6963 UNSET_SIGS (nsigs, sigs, signal_stop);
6964 }
6965 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
6966 {
6967 UNSET_SIGS (nsigs, sigs, signal_program);
6968 }
6969 else if (digits > 0)
6970 {
6971 /* It is numeric. The numeric signal refers to our own
6972 internal signal numbering from target.h, not to host/target
6973 signal number. This is a feature; users really should be
6974 using symbolic names anyway, and the common ones like
6975 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
6976
6977 sigfirst = siglast = (int)
2ea28649 6978 gdb_signal_from_command (atoi (*argv));
c906108c
SS
6979 if ((*argv)[digits] == '-')
6980 {
6981 siglast = (int)
2ea28649 6982 gdb_signal_from_command (atoi ((*argv) + digits + 1));
c906108c
SS
6983 }
6984 if (sigfirst > siglast)
6985 {
1777feb0 6986 /* Bet he didn't figure we'd think of this case... */
c906108c
SS
6987 signum = sigfirst;
6988 sigfirst = siglast;
6989 siglast = signum;
6990 }
6991 }
6992 else
6993 {
2ea28649 6994 oursig = gdb_signal_from_name (*argv);
a493e3e2 6995 if (oursig != GDB_SIGNAL_UNKNOWN)
c906108c
SS
6996 {
6997 sigfirst = siglast = (int) oursig;
6998 }
6999 else
7000 {
7001 /* Not a number and not a recognized flag word => complain. */
8a3fe4f8 7002 error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
c906108c
SS
7003 }
7004 }
7005
7006 /* If any signal numbers or symbol names were found, set flags for
1777feb0 7007 which signals to apply actions to. */
c906108c
SS
7008
7009 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
7010 {
2ea28649 7011 switch ((enum gdb_signal) signum)
c906108c 7012 {
a493e3e2
PA
7013 case GDB_SIGNAL_TRAP:
7014 case GDB_SIGNAL_INT:
c906108c
SS
7015 if (!allsigs && !sigs[signum])
7016 {
9e2f0ad4 7017 if (query (_("%s is used by the debugger.\n\
3e43a32a 7018Are you sure you want to change it? "),
2ea28649 7019 gdb_signal_to_name ((enum gdb_signal) signum)))
c906108c
SS
7020 {
7021 sigs[signum] = 1;
7022 }
7023 else
7024 {
a3f17187 7025 printf_unfiltered (_("Not confirmed, unchanged.\n"));
c906108c
SS
7026 gdb_flush (gdb_stdout);
7027 }
7028 }
7029 break;
a493e3e2
PA
7030 case GDB_SIGNAL_0:
7031 case GDB_SIGNAL_DEFAULT:
7032 case GDB_SIGNAL_UNKNOWN:
c906108c
SS
7033 /* Make sure that "all" doesn't print these. */
7034 break;
7035 default:
7036 sigs[signum] = 1;
7037 break;
7038 }
7039 }
7040
7041 argv++;
7042 }
7043
3a031f65
PA
7044 for (signum = 0; signum < nsigs; signum++)
7045 if (sigs[signum])
7046 {
2455069d 7047 signal_cache_update (-1);
a493e3e2
PA
7048 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
7049 target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
c906108c 7050
3a031f65
PA
7051 if (from_tty)
7052 {
7053 /* Show the results. */
7054 sig_print_header ();
7055 for (; signum < nsigs; signum++)
7056 if (sigs[signum])
aead7601 7057 sig_print_info ((enum gdb_signal) signum);
3a031f65
PA
7058 }
7059
7060 break;
7061 }
c906108c
SS
7062
7063 do_cleanups (old_chain);
7064}
7065
de0bea00
MF
7066/* Complete the "handle" command. */
7067
7068static VEC (char_ptr) *
7069handle_completer (struct cmd_list_element *ignore,
6f937416 7070 const char *text, const char *word)
de0bea00
MF
7071{
7072 VEC (char_ptr) *vec_signals, *vec_keywords, *return_val;
7073 static const char * const keywords[] =
7074 {
7075 "all",
7076 "stop",
7077 "ignore",
7078 "print",
7079 "pass",
7080 "nostop",
7081 "noignore",
7082 "noprint",
7083 "nopass",
7084 NULL,
7085 };
7086
7087 vec_signals = signal_completer (ignore, text, word);
7088 vec_keywords = complete_on_enum (keywords, word, word);
7089
7090 return_val = VEC_merge (char_ptr, vec_signals, vec_keywords);
7091 VEC_free (char_ptr, vec_signals);
7092 VEC_free (char_ptr, vec_keywords);
7093 return return_val;
7094}
7095
2ea28649
PA
7096enum gdb_signal
7097gdb_signal_from_command (int num)
ed01b82c
PA
7098{
7099 if (num >= 1 && num <= 15)
2ea28649 7100 return (enum gdb_signal) num;
ed01b82c
PA
7101 error (_("Only signals 1-15 are valid as numeric signals.\n\
7102Use \"info signals\" for a list of symbolic signals."));
7103}
7104
c906108c
SS
7105/* Print current contents of the tables set by the handle command.
7106 It is possible we should just be printing signals actually used
7107 by the current target (but for things to work right when switching
7108 targets, all signals should be in the signal tables). */
7109
7110static void
96baa820 7111signals_info (char *signum_exp, int from_tty)
c906108c 7112{
2ea28649 7113 enum gdb_signal oursig;
abbb1732 7114
c906108c
SS
7115 sig_print_header ();
7116
7117 if (signum_exp)
7118 {
7119 /* First see if this is a symbol name. */
2ea28649 7120 oursig = gdb_signal_from_name (signum_exp);
a493e3e2 7121 if (oursig == GDB_SIGNAL_UNKNOWN)
c906108c
SS
7122 {
7123 /* No, try numeric. */
7124 oursig =
2ea28649 7125 gdb_signal_from_command (parse_and_eval_long (signum_exp));
c906108c
SS
7126 }
7127 sig_print_info (oursig);
7128 return;
7129 }
7130
7131 printf_filtered ("\n");
7132 /* These ugly casts brought to you by the native VAX compiler. */
a493e3e2
PA
7133 for (oursig = GDB_SIGNAL_FIRST;
7134 (int) oursig < (int) GDB_SIGNAL_LAST;
2ea28649 7135 oursig = (enum gdb_signal) ((int) oursig + 1))
c906108c
SS
7136 {
7137 QUIT;
7138
a493e3e2
PA
7139 if (oursig != GDB_SIGNAL_UNKNOWN
7140 && oursig != GDB_SIGNAL_DEFAULT && oursig != GDB_SIGNAL_0)
c906108c
SS
7141 sig_print_info (oursig);
7142 }
7143
3e43a32a
MS
7144 printf_filtered (_("\nUse the \"handle\" command "
7145 "to change these tables.\n"));
c906108c 7146}
4aa995e1 7147
c709acd1
PA
7148/* Check if it makes sense to read $_siginfo from the current thread
7149 at this point. If not, throw an error. */
7150
7151static void
7152validate_siginfo_access (void)
7153{
7154 /* No current inferior, no siginfo. */
7155 if (ptid_equal (inferior_ptid, null_ptid))
7156 error (_("No thread selected."));
7157
7158 /* Don't try to read from a dead thread. */
7159 if (is_exited (inferior_ptid))
7160 error (_("The current thread has terminated"));
7161
7162 /* ... or from a spinning thread. */
7163 if (is_running (inferior_ptid))
7164 error (_("Selected thread is running."));
7165}
7166
4aa995e1
PA
7167/* The $_siginfo convenience variable is a bit special. We don't know
7168 for sure the type of the value until we actually have a chance to
7a9dd1b2 7169 fetch the data. The type can change depending on gdbarch, so it is
4aa995e1
PA
7170 also dependent on which thread you have selected.
7171
7172 1. making $_siginfo be an internalvar that creates a new value on
7173 access.
7174
7175 2. making the value of $_siginfo be an lval_computed value. */
7176
7177/* This function implements the lval_computed support for reading a
7178 $_siginfo value. */
7179
7180static void
7181siginfo_value_read (struct value *v)
7182{
7183 LONGEST transferred;
7184
c709acd1
PA
7185 validate_siginfo_access ();
7186
4aa995e1
PA
7187 transferred =
7188 target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO,
7189 NULL,
7190 value_contents_all_raw (v),
7191 value_offset (v),
7192 TYPE_LENGTH (value_type (v)));
7193
7194 if (transferred != TYPE_LENGTH (value_type (v)))
7195 error (_("Unable to read siginfo"));
7196}
7197
7198/* This function implements the lval_computed support for writing a
7199 $_siginfo value. */
7200
7201static void
7202siginfo_value_write (struct value *v, struct value *fromval)
7203{
7204 LONGEST transferred;
7205
c709acd1
PA
7206 validate_siginfo_access ();
7207
4aa995e1
PA
7208 transferred = target_write (&current_target,
7209 TARGET_OBJECT_SIGNAL_INFO,
7210 NULL,
7211 value_contents_all_raw (fromval),
7212 value_offset (v),
7213 TYPE_LENGTH (value_type (fromval)));
7214
7215 if (transferred != TYPE_LENGTH (value_type (fromval)))
7216 error (_("Unable to write siginfo"));
7217}
7218
c8f2448a 7219static const struct lval_funcs siginfo_value_funcs =
4aa995e1
PA
7220 {
7221 siginfo_value_read,
7222 siginfo_value_write
7223 };
7224
7225/* Return a new value with the correct type for the siginfo object of
78267919
UW
7226 the current thread using architecture GDBARCH. Return a void value
7227 if there's no object available. */
4aa995e1 7228
2c0b251b 7229static struct value *
22d2b532
SDJ
7230siginfo_make_value (struct gdbarch *gdbarch, struct internalvar *var,
7231 void *ignore)
4aa995e1 7232{
4aa995e1 7233 if (target_has_stack
78267919
UW
7234 && !ptid_equal (inferior_ptid, null_ptid)
7235 && gdbarch_get_siginfo_type_p (gdbarch))
4aa995e1 7236 {
78267919 7237 struct type *type = gdbarch_get_siginfo_type (gdbarch);
abbb1732 7238
78267919 7239 return allocate_computed_value (type, &siginfo_value_funcs, NULL);
4aa995e1
PA
7240 }
7241
78267919 7242 return allocate_value (builtin_type (gdbarch)->builtin_void);
4aa995e1
PA
7243}
7244
c906108c 7245\f
16c381f0
JK
7246/* infcall_suspend_state contains state about the program itself like its
7247 registers and any signal it received when it last stopped.
7248 This state must be restored regardless of how the inferior function call
7249 ends (either successfully, or after it hits a breakpoint or signal)
7250 if the program is to properly continue where it left off. */
7251
7252struct infcall_suspend_state
7a292a7a 7253{
16c381f0 7254 struct thread_suspend_state thread_suspend;
16c381f0
JK
7255
7256 /* Other fields: */
7a292a7a 7257 CORE_ADDR stop_pc;
b89667eb 7258 struct regcache *registers;
1736ad11 7259
35515841 7260 /* Format of SIGINFO_DATA or NULL if it is not present. */
1736ad11
JK
7261 struct gdbarch *siginfo_gdbarch;
7262
7263 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
7264 TYPE_LENGTH (gdbarch_get_siginfo_type ()). For different gdbarch the
7265 content would be invalid. */
7266 gdb_byte *siginfo_data;
b89667eb
DE
7267};
7268
16c381f0
JK
7269struct infcall_suspend_state *
7270save_infcall_suspend_state (void)
b89667eb 7271{
16c381f0 7272 struct infcall_suspend_state *inf_state;
b89667eb 7273 struct thread_info *tp = inferior_thread ();
1736ad11
JK
7274 struct regcache *regcache = get_current_regcache ();
7275 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7276 gdb_byte *siginfo_data = NULL;
7277
7278 if (gdbarch_get_siginfo_type_p (gdbarch))
7279 {
7280 struct type *type = gdbarch_get_siginfo_type (gdbarch);
7281 size_t len = TYPE_LENGTH (type);
7282 struct cleanup *back_to;
7283
7284 siginfo_data = xmalloc (len);
7285 back_to = make_cleanup (xfree, siginfo_data);
7286
7287 if (target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
7288 siginfo_data, 0, len) == len)
7289 discard_cleanups (back_to);
7290 else
7291 {
7292 /* Errors ignored. */
7293 do_cleanups (back_to);
7294 siginfo_data = NULL;
7295 }
7296 }
7297
41bf6aca 7298 inf_state = XCNEW (struct infcall_suspend_state);
1736ad11
JK
7299
7300 if (siginfo_data)
7301 {
7302 inf_state->siginfo_gdbarch = gdbarch;
7303 inf_state->siginfo_data = siginfo_data;
7304 }
b89667eb 7305
16c381f0 7306 inf_state->thread_suspend = tp->suspend;
16c381f0 7307
35515841 7308 /* run_inferior_call will not use the signal due to its `proceed' call with
a493e3e2
PA
7309 GDB_SIGNAL_0 anyway. */
7310 tp->suspend.stop_signal = GDB_SIGNAL_0;
35515841 7311
b89667eb
DE
7312 inf_state->stop_pc = stop_pc;
7313
1736ad11 7314 inf_state->registers = regcache_dup (regcache);
b89667eb
DE
7315
7316 return inf_state;
7317}
7318
7319/* Restore inferior session state to INF_STATE. */
7320
7321void
16c381f0 7322restore_infcall_suspend_state (struct infcall_suspend_state *inf_state)
b89667eb
DE
7323{
7324 struct thread_info *tp = inferior_thread ();
1736ad11
JK
7325 struct regcache *regcache = get_current_regcache ();
7326 struct gdbarch *gdbarch = get_regcache_arch (regcache);
b89667eb 7327
16c381f0 7328 tp->suspend = inf_state->thread_suspend;
16c381f0 7329
b89667eb
DE
7330 stop_pc = inf_state->stop_pc;
7331
1736ad11
JK
7332 if (inf_state->siginfo_gdbarch == gdbarch)
7333 {
7334 struct type *type = gdbarch_get_siginfo_type (gdbarch);
1736ad11
JK
7335
7336 /* Errors ignored. */
7337 target_write (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6acef6cd 7338 inf_state->siginfo_data, 0, TYPE_LENGTH (type));
1736ad11
JK
7339 }
7340
b89667eb
DE
7341 /* The inferior can be gone if the user types "print exit(0)"
7342 (and perhaps other times). */
7343 if (target_has_execution)
7344 /* NB: The register write goes through to the target. */
1736ad11 7345 regcache_cpy (regcache, inf_state->registers);
803b5f95 7346
16c381f0 7347 discard_infcall_suspend_state (inf_state);
b89667eb
DE
7348}
7349
7350static void
16c381f0 7351do_restore_infcall_suspend_state_cleanup (void *state)
b89667eb 7352{
16c381f0 7353 restore_infcall_suspend_state (state);
b89667eb
DE
7354}
7355
7356struct cleanup *
16c381f0
JK
7357make_cleanup_restore_infcall_suspend_state
7358 (struct infcall_suspend_state *inf_state)
b89667eb 7359{
16c381f0 7360 return make_cleanup (do_restore_infcall_suspend_state_cleanup, inf_state);
b89667eb
DE
7361}
7362
7363void
16c381f0 7364discard_infcall_suspend_state (struct infcall_suspend_state *inf_state)
b89667eb
DE
7365{
7366 regcache_xfree (inf_state->registers);
803b5f95 7367 xfree (inf_state->siginfo_data);
b89667eb
DE
7368 xfree (inf_state);
7369}
7370
7371struct regcache *
16c381f0 7372get_infcall_suspend_state_regcache (struct infcall_suspend_state *inf_state)
b89667eb
DE
7373{
7374 return inf_state->registers;
7375}
7376
16c381f0
JK
7377/* infcall_control_state contains state regarding gdb's control of the
7378 inferior itself like stepping control. It also contains session state like
7379 the user's currently selected frame. */
b89667eb 7380
16c381f0 7381struct infcall_control_state
b89667eb 7382{
16c381f0
JK
7383 struct thread_control_state thread_control;
7384 struct inferior_control_state inferior_control;
d82142e2
JK
7385
7386 /* Other fields: */
7387 enum stop_stack_kind stop_stack_dummy;
7388 int stopped_by_random_signal;
7a292a7a 7389 int stop_after_trap;
7a292a7a 7390
b89667eb 7391 /* ID if the selected frame when the inferior function call was made. */
101dcfbe 7392 struct frame_id selected_frame_id;
7a292a7a
SS
7393};
7394
c906108c 7395/* Save all of the information associated with the inferior<==>gdb
b89667eb 7396 connection. */
c906108c 7397
16c381f0
JK
7398struct infcall_control_state *
7399save_infcall_control_state (void)
c906108c 7400{
16c381f0 7401 struct infcall_control_state *inf_status = xmalloc (sizeof (*inf_status));
4e1c45ea 7402 struct thread_info *tp = inferior_thread ();
d6b48e9c 7403 struct inferior *inf = current_inferior ();
7a292a7a 7404
16c381f0
JK
7405 inf_status->thread_control = tp->control;
7406 inf_status->inferior_control = inf->control;
d82142e2 7407
8358c15c 7408 tp->control.step_resume_breakpoint = NULL;
5b79abe7 7409 tp->control.exception_resume_breakpoint = NULL;
8358c15c 7410
16c381f0
JK
7411 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
7412 chain. If caller's caller is walking the chain, they'll be happier if we
7413 hand them back the original chain when restore_infcall_control_state is
7414 called. */
7415 tp->control.stop_bpstat = bpstat_copy (tp->control.stop_bpstat);
d82142e2
JK
7416
7417 /* Other fields: */
7418 inf_status->stop_stack_dummy = stop_stack_dummy;
7419 inf_status->stopped_by_random_signal = stopped_by_random_signal;
7420 inf_status->stop_after_trap = stop_after_trap;
c5aa993b 7421
206415a3 7422 inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
b89667eb 7423
7a292a7a 7424 return inf_status;
c906108c
SS
7425}
7426
c906108c 7427static int
96baa820 7428restore_selected_frame (void *args)
c906108c 7429{
488f131b 7430 struct frame_id *fid = (struct frame_id *) args;
c906108c 7431 struct frame_info *frame;
c906108c 7432
101dcfbe 7433 frame = frame_find_by_id (*fid);
c906108c 7434
aa0cd9c1
AC
7435 /* If inf_status->selected_frame_id is NULL, there was no previously
7436 selected frame. */
101dcfbe 7437 if (frame == NULL)
c906108c 7438 {
8a3fe4f8 7439 warning (_("Unable to restore previously selected frame."));
c906108c
SS
7440 return 0;
7441 }
7442
0f7d239c 7443 select_frame (frame);
c906108c
SS
7444
7445 return (1);
7446}
7447
b89667eb
DE
7448/* Restore inferior session state to INF_STATUS. */
7449
c906108c 7450void
16c381f0 7451restore_infcall_control_state (struct infcall_control_state *inf_status)
c906108c 7452{
4e1c45ea 7453 struct thread_info *tp = inferior_thread ();
d6b48e9c 7454 struct inferior *inf = current_inferior ();
4e1c45ea 7455
8358c15c
JK
7456 if (tp->control.step_resume_breakpoint)
7457 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
7458
5b79abe7
TT
7459 if (tp->control.exception_resume_breakpoint)
7460 tp->control.exception_resume_breakpoint->disposition
7461 = disp_del_at_next_stop;
7462
d82142e2 7463 /* Handle the bpstat_copy of the chain. */
16c381f0 7464 bpstat_clear (&tp->control.stop_bpstat);
d82142e2 7465
16c381f0
JK
7466 tp->control = inf_status->thread_control;
7467 inf->control = inf_status->inferior_control;
d82142e2
JK
7468
7469 /* Other fields: */
7470 stop_stack_dummy = inf_status->stop_stack_dummy;
7471 stopped_by_random_signal = inf_status->stopped_by_random_signal;
7472 stop_after_trap = inf_status->stop_after_trap;
c906108c 7473
b89667eb 7474 if (target_has_stack)
c906108c 7475 {
c906108c 7476 /* The point of catch_errors is that if the stack is clobbered,
101dcfbe
AC
7477 walking the stack might encounter a garbage pointer and
7478 error() trying to dereference it. */
488f131b
JB
7479 if (catch_errors
7480 (restore_selected_frame, &inf_status->selected_frame_id,
7481 "Unable to restore previously selected frame:\n",
7482 RETURN_MASK_ERROR) == 0)
c906108c
SS
7483 /* Error in restoring the selected frame. Select the innermost
7484 frame. */
0f7d239c 7485 select_frame (get_current_frame ());
c906108c 7486 }
c906108c 7487
72cec141 7488 xfree (inf_status);
7a292a7a 7489}
c906108c 7490
74b7792f 7491static void
16c381f0 7492do_restore_infcall_control_state_cleanup (void *sts)
74b7792f 7493{
16c381f0 7494 restore_infcall_control_state (sts);
74b7792f
AC
7495}
7496
7497struct cleanup *
16c381f0
JK
7498make_cleanup_restore_infcall_control_state
7499 (struct infcall_control_state *inf_status)
74b7792f 7500{
16c381f0 7501 return make_cleanup (do_restore_infcall_control_state_cleanup, inf_status);
74b7792f
AC
7502}
7503
c906108c 7504void
16c381f0 7505discard_infcall_control_state (struct infcall_control_state *inf_status)
7a292a7a 7506{
8358c15c
JK
7507 if (inf_status->thread_control.step_resume_breakpoint)
7508 inf_status->thread_control.step_resume_breakpoint->disposition
7509 = disp_del_at_next_stop;
7510
5b79abe7
TT
7511 if (inf_status->thread_control.exception_resume_breakpoint)
7512 inf_status->thread_control.exception_resume_breakpoint->disposition
7513 = disp_del_at_next_stop;
7514
1777feb0 7515 /* See save_infcall_control_state for info on stop_bpstat. */
16c381f0 7516 bpstat_clear (&inf_status->thread_control.stop_bpstat);
8358c15c 7517
72cec141 7518 xfree (inf_status);
7a292a7a 7519}
b89667eb 7520\f
ca6724c1
KB
7521/* restore_inferior_ptid() will be used by the cleanup machinery
7522 to restore the inferior_ptid value saved in a call to
7523 save_inferior_ptid(). */
ce696e05
KB
7524
7525static void
7526restore_inferior_ptid (void *arg)
7527{
7528 ptid_t *saved_ptid_ptr = arg;
abbb1732 7529
ce696e05
KB
7530 inferior_ptid = *saved_ptid_ptr;
7531 xfree (arg);
7532}
7533
7534/* Save the value of inferior_ptid so that it may be restored by a
7535 later call to do_cleanups(). Returns the struct cleanup pointer
7536 needed for later doing the cleanup. */
7537
7538struct cleanup *
7539save_inferior_ptid (void)
7540{
7541 ptid_t *saved_ptid_ptr;
7542
7543 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
7544 *saved_ptid_ptr = inferior_ptid;
7545 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
7546}
0c557179 7547
7f89fd65 7548/* See infrun.h. */
0c557179
SDJ
7549
7550void
7551clear_exit_convenience_vars (void)
7552{
7553 clear_internalvar (lookup_internalvar ("_exitsignal"));
7554 clear_internalvar (lookup_internalvar ("_exitcode"));
7555}
c5aa993b 7556\f
488f131b 7557
b2175913
MS
7558/* User interface for reverse debugging:
7559 Set exec-direction / show exec-direction commands
7560 (returns error unless target implements to_set_exec_direction method). */
7561
32231432 7562int execution_direction = EXEC_FORWARD;
b2175913
MS
7563static const char exec_forward[] = "forward";
7564static const char exec_reverse[] = "reverse";
7565static const char *exec_direction = exec_forward;
40478521 7566static const char *const exec_direction_names[] = {
b2175913
MS
7567 exec_forward,
7568 exec_reverse,
7569 NULL
7570};
7571
7572static void
7573set_exec_direction_func (char *args, int from_tty,
7574 struct cmd_list_element *cmd)
7575{
7576 if (target_can_execute_reverse)
7577 {
7578 if (!strcmp (exec_direction, exec_forward))
7579 execution_direction = EXEC_FORWARD;
7580 else if (!strcmp (exec_direction, exec_reverse))
7581 execution_direction = EXEC_REVERSE;
7582 }
8bbed405
MS
7583 else
7584 {
7585 exec_direction = exec_forward;
7586 error (_("Target does not support this operation."));
7587 }
b2175913
MS
7588}
7589
7590static void
7591show_exec_direction_func (struct ui_file *out, int from_tty,
7592 struct cmd_list_element *cmd, const char *value)
7593{
7594 switch (execution_direction) {
7595 case EXEC_FORWARD:
7596 fprintf_filtered (out, _("Forward.\n"));
7597 break;
7598 case EXEC_REVERSE:
7599 fprintf_filtered (out, _("Reverse.\n"));
7600 break;
b2175913 7601 default:
d8b34453
PA
7602 internal_error (__FILE__, __LINE__,
7603 _("bogus execution_direction value: %d"),
7604 (int) execution_direction);
b2175913
MS
7605 }
7606}
7607
d4db2f36
PA
7608static void
7609show_schedule_multiple (struct ui_file *file, int from_tty,
7610 struct cmd_list_element *c, const char *value)
7611{
3e43a32a
MS
7612 fprintf_filtered (file, _("Resuming the execution of threads "
7613 "of all processes is %s.\n"), value);
d4db2f36 7614}
ad52ddc6 7615
22d2b532
SDJ
7616/* Implementation of `siginfo' variable. */
7617
7618static const struct internalvar_funcs siginfo_funcs =
7619{
7620 siginfo_make_value,
7621 NULL,
7622 NULL
7623};
7624
c906108c 7625void
96baa820 7626_initialize_infrun (void)
c906108c 7627{
52f0bd74
AC
7628 int i;
7629 int numsigs;
de0bea00 7630 struct cmd_list_element *c;
c906108c 7631
1bedd215
AC
7632 add_info ("signals", signals_info, _("\
7633What debugger does when program gets various signals.\n\
7634Specify a signal as argument to print info on that signal only."));
c906108c
SS
7635 add_info_alias ("handle", "signals", 0);
7636
de0bea00 7637 c = add_com ("handle", class_run, handle_command, _("\
dfbd5e7b 7638Specify how to handle signals.\n\
486c7739 7639Usage: handle SIGNAL [ACTIONS]\n\
c906108c 7640Args are signals and actions to apply to those signals.\n\
dfbd5e7b 7641If no actions are specified, the current settings for the specified signals\n\
486c7739
MF
7642will be displayed instead.\n\
7643\n\
c906108c
SS
7644Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
7645from 1-15 are allowed for compatibility with old versions of GDB.\n\
7646Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
7647The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215 7648used by the debugger, typically SIGTRAP and SIGINT.\n\
486c7739 7649\n\
1bedd215 7650Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
c906108c
SS
7651\"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
7652Stop means reenter debugger if this signal happens (implies print).\n\
7653Print means print a message if this signal happens.\n\
7654Pass means let program see this signal; otherwise program doesn't know.\n\
7655Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
dfbd5e7b
PA
7656Pass and Stop may be combined.\n\
7657\n\
7658Multiple signals may be specified. Signal numbers and signal names\n\
7659may be interspersed with actions, with the actions being performed for\n\
7660all signals cumulatively specified."));
de0bea00 7661 set_cmd_completer (c, handle_completer);
486c7739 7662
c906108c 7663 if (!dbx_commands)
1a966eab
AC
7664 stop_command = add_cmd ("stop", class_obscure,
7665 not_just_help_class_command, _("\
7666There is no `stop' command, but you can set a hook on `stop'.\n\
c906108c 7667This allows you to set a list of commands to be run each time execution\n\
1a966eab 7668of the program stops."), &cmdlist);
c906108c 7669
ccce17b0 7670 add_setshow_zuinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
85c07804
AC
7671Set inferior debugging."), _("\
7672Show inferior debugging."), _("\
7673When non-zero, inferior specific debugging is enabled."),
ccce17b0
YQ
7674 NULL,
7675 show_debug_infrun,
7676 &setdebuglist, &showdebuglist);
527159b7 7677
3e43a32a
MS
7678 add_setshow_boolean_cmd ("displaced", class_maintenance,
7679 &debug_displaced, _("\
237fc4c9
PA
7680Set displaced stepping debugging."), _("\
7681Show displaced stepping debugging."), _("\
7682When non-zero, displaced stepping specific debugging is enabled."),
7683 NULL,
7684 show_debug_displaced,
7685 &setdebuglist, &showdebuglist);
7686
ad52ddc6
PA
7687 add_setshow_boolean_cmd ("non-stop", no_class,
7688 &non_stop_1, _("\
7689Set whether gdb controls the inferior in non-stop mode."), _("\
7690Show whether gdb controls the inferior in non-stop mode."), _("\
7691When debugging a multi-threaded program and this setting is\n\
7692off (the default, also called all-stop mode), when one thread stops\n\
7693(for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
7694all other threads in the program while you interact with the thread of\n\
7695interest. When you continue or step a thread, you can allow the other\n\
7696threads to run, or have them remain stopped, but while you inspect any\n\
7697thread's state, all threads stop.\n\
7698\n\
7699In non-stop mode, when one thread stops, other threads can continue\n\
7700to run freely. You'll be able to step each thread independently,\n\
7701leave it stopped or free to run as needed."),
7702 set_non_stop,
7703 show_non_stop,
7704 &setlist,
7705 &showlist);
7706
a493e3e2 7707 numsigs = (int) GDB_SIGNAL_LAST;
488f131b 7708 signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
c906108c
SS
7709 signal_print = (unsigned char *)
7710 xmalloc (sizeof (signal_print[0]) * numsigs);
7711 signal_program = (unsigned char *)
7712 xmalloc (sizeof (signal_program[0]) * numsigs);
ab04a2af
TT
7713 signal_catch = (unsigned char *)
7714 xmalloc (sizeof (signal_catch[0]) * numsigs);
2455069d 7715 signal_pass = (unsigned char *)
4395285e 7716 xmalloc (sizeof (signal_pass[0]) * numsigs);
c906108c
SS
7717 for (i = 0; i < numsigs; i++)
7718 {
7719 signal_stop[i] = 1;
7720 signal_print[i] = 1;
7721 signal_program[i] = 1;
ab04a2af 7722 signal_catch[i] = 0;
c906108c
SS
7723 }
7724
7725 /* Signals caused by debugger's own actions
7726 should not be given to the program afterwards. */
a493e3e2
PA
7727 signal_program[GDB_SIGNAL_TRAP] = 0;
7728 signal_program[GDB_SIGNAL_INT] = 0;
c906108c
SS
7729
7730 /* Signals that are not errors should not normally enter the debugger. */
a493e3e2
PA
7731 signal_stop[GDB_SIGNAL_ALRM] = 0;
7732 signal_print[GDB_SIGNAL_ALRM] = 0;
7733 signal_stop[GDB_SIGNAL_VTALRM] = 0;
7734 signal_print[GDB_SIGNAL_VTALRM] = 0;
7735 signal_stop[GDB_SIGNAL_PROF] = 0;
7736 signal_print[GDB_SIGNAL_PROF] = 0;
7737 signal_stop[GDB_SIGNAL_CHLD] = 0;
7738 signal_print[GDB_SIGNAL_CHLD] = 0;
7739 signal_stop[GDB_SIGNAL_IO] = 0;
7740 signal_print[GDB_SIGNAL_IO] = 0;
7741 signal_stop[GDB_SIGNAL_POLL] = 0;
7742 signal_print[GDB_SIGNAL_POLL] = 0;
7743 signal_stop[GDB_SIGNAL_URG] = 0;
7744 signal_print[GDB_SIGNAL_URG] = 0;
7745 signal_stop[GDB_SIGNAL_WINCH] = 0;
7746 signal_print[GDB_SIGNAL_WINCH] = 0;
7747 signal_stop[GDB_SIGNAL_PRIO] = 0;
7748 signal_print[GDB_SIGNAL_PRIO] = 0;
c906108c 7749
cd0fc7c3
SS
7750 /* These signals are used internally by user-level thread
7751 implementations. (See signal(5) on Solaris.) Like the above
7752 signals, a healthy program receives and handles them as part of
7753 its normal operation. */
a493e3e2
PA
7754 signal_stop[GDB_SIGNAL_LWP] = 0;
7755 signal_print[GDB_SIGNAL_LWP] = 0;
7756 signal_stop[GDB_SIGNAL_WAITING] = 0;
7757 signal_print[GDB_SIGNAL_WAITING] = 0;
7758 signal_stop[GDB_SIGNAL_CANCEL] = 0;
7759 signal_print[GDB_SIGNAL_CANCEL] = 0;
cd0fc7c3 7760
2455069d
UW
7761 /* Update cached state. */
7762 signal_cache_update (-1);
7763
85c07804
AC
7764 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
7765 &stop_on_solib_events, _("\
7766Set stopping for shared library events."), _("\
7767Show stopping for shared library events."), _("\
c906108c
SS
7768If nonzero, gdb will give control to the user when the dynamic linker\n\
7769notifies gdb of shared library events. The most common event of interest\n\
85c07804 7770to the user would be loading/unloading of a new library."),
f9e14852 7771 set_stop_on_solib_events,
920d2a44 7772 show_stop_on_solib_events,
85c07804 7773 &setlist, &showlist);
c906108c 7774
7ab04401
AC
7775 add_setshow_enum_cmd ("follow-fork-mode", class_run,
7776 follow_fork_mode_kind_names,
7777 &follow_fork_mode_string, _("\
7778Set debugger response to a program call of fork or vfork."), _("\
7779Show debugger response to a program call of fork or vfork."), _("\
c906108c
SS
7780A fork or vfork creates a new process. follow-fork-mode can be:\n\
7781 parent - the original process is debugged after a fork\n\
7782 child - the new process is debugged after a fork\n\
ea1dd7bc 7783The unfollowed process will continue to run.\n\
7ab04401
AC
7784By default, the debugger will follow the parent process."),
7785 NULL,
920d2a44 7786 show_follow_fork_mode_string,
7ab04401
AC
7787 &setlist, &showlist);
7788
6c95b8df
PA
7789 add_setshow_enum_cmd ("follow-exec-mode", class_run,
7790 follow_exec_mode_names,
7791 &follow_exec_mode_string, _("\
7792Set debugger response to a program call of exec."), _("\
7793Show debugger response to a program call of exec."), _("\
7794An exec call replaces the program image of a process.\n\
7795\n\
7796follow-exec-mode can be:\n\
7797\n\
cce7e648 7798 new - the debugger creates a new inferior and rebinds the process\n\
6c95b8df
PA
7799to this new inferior. The program the process was running before\n\
7800the exec call can be restarted afterwards by restarting the original\n\
7801inferior.\n\
7802\n\
7803 same - the debugger keeps the process bound to the same inferior.\n\
7804The new executable image replaces the previous executable loaded in\n\
7805the inferior. Restarting the inferior after the exec call restarts\n\
7806the executable the process was running after the exec call.\n\
7807\n\
7808By default, the debugger will use the same inferior."),
7809 NULL,
7810 show_follow_exec_mode_string,
7811 &setlist, &showlist);
7812
7ab04401
AC
7813 add_setshow_enum_cmd ("scheduler-locking", class_run,
7814 scheduler_enums, &scheduler_mode, _("\
7815Set mode for locking scheduler during execution."), _("\
7816Show mode for locking scheduler during execution."), _("\
c906108c
SS
7817off == no locking (threads may preempt at any time)\n\
7818on == full locking (no thread except the current thread may run)\n\
856e7dd6
PA
7819step == scheduler locked during stepping commands (step, next, stepi, nexti).\n\
7820 In this mode, other threads may run during other commands."),
7ab04401 7821 set_schedlock_func, /* traps on target vector */
920d2a44 7822 show_scheduler_mode,
7ab04401 7823 &setlist, &showlist);
5fbbeb29 7824
d4db2f36
PA
7825 add_setshow_boolean_cmd ("schedule-multiple", class_run, &sched_multi, _("\
7826Set mode for resuming threads of all processes."), _("\
7827Show mode for resuming threads of all processes."), _("\
7828When on, execution commands (such as 'continue' or 'next') resume all\n\
7829threads of all processes. When off (which is the default), execution\n\
7830commands only resume the threads of the current process. The set of\n\
7831threads that are resumed is further refined by the scheduler-locking\n\
7832mode (see help set scheduler-locking)."),
7833 NULL,
7834 show_schedule_multiple,
7835 &setlist, &showlist);
7836
5bf193a2
AC
7837 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
7838Set mode of the step operation."), _("\
7839Show mode of the step operation."), _("\
7840When set, doing a step over a function without debug line information\n\
7841will stop at the first instruction of that function. Otherwise, the\n\
7842function is skipped and the step command stops at a different source line."),
7843 NULL,
920d2a44 7844 show_step_stop_if_no_debug,
5bf193a2 7845 &setlist, &showlist);
ca6724c1 7846
72d0e2c5
YQ
7847 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run,
7848 &can_use_displaced_stepping, _("\
237fc4c9
PA
7849Set debugger's willingness to use displaced stepping."), _("\
7850Show debugger's willingness to use displaced stepping."), _("\
fff08868
HZ
7851If on, gdb will use displaced stepping to step over breakpoints if it is\n\
7852supported by the target architecture. If off, gdb will not use displaced\n\
7853stepping to step over breakpoints, even if such is supported by the target\n\
7854architecture. If auto (which is the default), gdb will use displaced stepping\n\
7855if the target architecture supports it and non-stop mode is active, but will not\n\
7856use it in all-stop mode (see help set non-stop)."),
72d0e2c5
YQ
7857 NULL,
7858 show_can_use_displaced_stepping,
7859 &setlist, &showlist);
237fc4c9 7860
b2175913
MS
7861 add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
7862 &exec_direction, _("Set direction of execution.\n\
7863Options are 'forward' or 'reverse'."),
7864 _("Show direction of execution (forward/reverse)."),
7865 _("Tells gdb whether to execute forward or backward."),
7866 set_exec_direction_func, show_exec_direction_func,
7867 &setlist, &showlist);
7868
6c95b8df
PA
7869 /* Set/show detach-on-fork: user-settable mode. */
7870
7871 add_setshow_boolean_cmd ("detach-on-fork", class_run, &detach_fork, _("\
7872Set whether gdb will detach the child of a fork."), _("\
7873Show whether gdb will detach the child of a fork."), _("\
7874Tells gdb whether to detach the child of a fork."),
7875 NULL, NULL, &setlist, &showlist);
7876
03583c20
UW
7877 /* Set/show disable address space randomization mode. */
7878
7879 add_setshow_boolean_cmd ("disable-randomization", class_support,
7880 &disable_randomization, _("\
7881Set disabling of debuggee's virtual address space randomization."), _("\
7882Show disabling of debuggee's virtual address space randomization."), _("\
7883When this mode is on (which is the default), randomization of the virtual\n\
7884address space is disabled. Standalone programs run with the randomization\n\
7885enabled by default on some platforms."),
7886 &set_disable_randomization,
7887 &show_disable_randomization,
7888 &setlist, &showlist);
7889
ca6724c1 7890 /* ptid initializations */
ca6724c1
KB
7891 inferior_ptid = null_ptid;
7892 target_last_wait_ptid = minus_one_ptid;
5231c1fd
PA
7893
7894 observer_attach_thread_ptid_changed (infrun_thread_ptid_changed);
252fbfc8 7895 observer_attach_thread_stop_requested (infrun_thread_stop_requested);
a07daef3 7896 observer_attach_thread_exit (infrun_thread_thread_exit);
fc1cf338 7897 observer_attach_inferior_exit (infrun_inferior_exit);
4aa995e1
PA
7898
7899 /* Explicitly create without lookup, since that tries to create a
7900 value with a void typed value, and when we get here, gdbarch
7901 isn't initialized yet. At this point, we're quite sure there
7902 isn't another convenience variable of the same name. */
22d2b532 7903 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs, NULL);
d914c394
SS
7904
7905 add_setshow_boolean_cmd ("observer", no_class,
7906 &observer_mode_1, _("\
7907Set whether gdb controls the inferior in observer mode."), _("\
7908Show whether gdb controls the inferior in observer mode."), _("\
7909In observer mode, GDB can get data from the inferior, but not\n\
7910affect its execution. Registers and memory may not be changed,\n\
7911breakpoints may not be set, and the program cannot be interrupted\n\
7912or signalled."),
7913 set_observer_mode,
7914 show_observer_mode,
7915 &setlist,
7916 &showlist);
c906108c 7917}