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