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