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