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