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