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