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