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