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