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