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