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