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