1 /* Target-struct-independent code to start (run) and stop an inferior
4 Copyright (C) 1986-2014 Free Software Foundation, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "exceptions.h"
28 #include "breakpoint.h"
32 #include "cli/cli-script.h"
34 #include "gdbthread.h"
46 #include "dictionary.h"
48 #include "gdb_assert.h"
49 #include "mi/mi-common.h"
50 #include "event-top.h"
52 #include "record-full.h"
53 #include "inline-frame.h"
55 #include "tracepoint.h"
56 #include "continuations.h"
61 #include "completer.h"
62 #include "target-descriptions.h"
63 #include "target-dcache.h"
65 /* Prototypes for local functions */
67 static void signals_info (char *, int);
69 static void handle_command (char *, int);
71 static void sig_print_info (enum gdb_signal);
73 static void sig_print_header (void);
75 static void resume_cleanups (void *);
77 static int hook_stop_stub (void *);
79 static int restore_selected_frame (void *);
81 static int follow_fork (void);
83 static void set_schedlock_func (char *args, int from_tty,
84 struct cmd_list_element *c);
86 static int currently_stepping (struct thread_info *tp);
88 static int currently_stepping_or_nexting_callback (struct thread_info *tp,
91 static void xdb_handle_command (char *args, int from_tty);
93 static int prepare_to_proceed (int);
95 static void print_exited_reason (int exitstatus);
97 static void print_signal_exited_reason (enum gdb_signal siggnal);
99 static void print_no_history_reason (void);
101 static void print_signal_received_reason (enum gdb_signal siggnal);
103 static void print_end_stepping_range_reason (void);
105 void _initialize_infrun (void);
107 void nullify_last_target_wait_ptid (void);
109 static void insert_hp_step_resume_breakpoint_at_frame (struct frame_info *);
111 static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
113 static void insert_longjmp_resume_breakpoint (struct gdbarch *, CORE_ADDR);
115 /* When set, stop the 'step' command if we enter a function which has
116 no line number information. The normal behavior is that we step
117 over such function. */
118 int step_stop_if_no_debug = 0;
120 show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
121 struct cmd_list_element *c, const char *value)
123 fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
126 /* In asynchronous mode, but simulating synchronous execution. */
128 int sync_execution = 0;
130 /* wait_for_inferior and normal_stop use this to notify the user
131 when the inferior stopped in a different thread than it had been
134 static ptid_t previous_inferior_ptid;
136 /* If set (default for legacy reasons), when following a fork, GDB
137 will detach from one of the fork branches, child or parent.
138 Exactly which branch is detached depends on 'set follow-fork-mode'
141 static int detach_fork = 1;
143 int debug_displaced = 0;
145 show_debug_displaced (struct ui_file *file, int from_tty,
146 struct cmd_list_element *c, const char *value)
148 fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
151 unsigned int debug_infrun = 0;
153 show_debug_infrun (struct ui_file *file, int from_tty,
154 struct cmd_list_element *c, const char *value)
156 fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
160 /* Support for disabling address space randomization. */
162 int disable_randomization = 1;
165 show_disable_randomization (struct ui_file *file, int from_tty,
166 struct cmd_list_element *c, const char *value)
168 if (target_supports_disable_randomization ())
169 fprintf_filtered (file,
170 _("Disabling randomization of debuggee's "
171 "virtual address space is %s.\n"),
174 fputs_filtered (_("Disabling randomization of debuggee's "
175 "virtual address space is unsupported on\n"
176 "this platform.\n"), file);
180 set_disable_randomization (char *args, int from_tty,
181 struct cmd_list_element *c)
183 if (!target_supports_disable_randomization ())
184 error (_("Disabling randomization of debuggee's "
185 "virtual address space is unsupported on\n"
189 /* User interface for non-stop mode. */
192 static int non_stop_1 = 0;
195 set_non_stop (char *args, int from_tty,
196 struct cmd_list_element *c)
198 if (target_has_execution)
200 non_stop_1 = non_stop;
201 error (_("Cannot change this setting while the inferior is running."));
204 non_stop = non_stop_1;
208 show_non_stop (struct ui_file *file, int from_tty,
209 struct cmd_list_element *c, const char *value)
211 fprintf_filtered (file,
212 _("Controlling the inferior in non-stop mode is %s.\n"),
216 /* "Observer mode" is somewhat like a more extreme version of
217 non-stop, in which all GDB operations that might affect the
218 target's execution have been disabled. */
220 int observer_mode = 0;
221 static int observer_mode_1 = 0;
224 set_observer_mode (char *args, int from_tty,
225 struct cmd_list_element *c)
227 if (target_has_execution)
229 observer_mode_1 = observer_mode;
230 error (_("Cannot change this setting while the inferior is running."));
233 observer_mode = observer_mode_1;
235 may_write_registers = !observer_mode;
236 may_write_memory = !observer_mode;
237 may_insert_breakpoints = !observer_mode;
238 may_insert_tracepoints = !observer_mode;
239 /* We can insert fast tracepoints in or out of observer mode,
240 but enable them if we're going into this mode. */
242 may_insert_fast_tracepoints = 1;
243 may_stop = !observer_mode;
244 update_target_permissions ();
246 /* Going *into* observer mode we must force non-stop, then
247 going out we leave it that way. */
250 target_async_permitted = 1;
251 pagination_enabled = 0;
252 non_stop = non_stop_1 = 1;
256 printf_filtered (_("Observer mode is now %s.\n"),
257 (observer_mode ? "on" : "off"));
261 show_observer_mode (struct ui_file *file, int from_tty,
262 struct cmd_list_element *c, const char *value)
264 fprintf_filtered (file, _("Observer mode is %s.\n"), value);
267 /* This updates the value of observer mode based on changes in
268 permissions. Note that we are deliberately ignoring the values of
269 may-write-registers and may-write-memory, since the user may have
270 reason to enable these during a session, for instance to turn on a
271 debugging-related global. */
274 update_observer_mode (void)
278 newval = (!may_insert_breakpoints
279 && !may_insert_tracepoints
280 && may_insert_fast_tracepoints
284 /* Let the user know if things change. */
285 if (newval != observer_mode)
286 printf_filtered (_("Observer mode is now %s.\n"),
287 (newval ? "on" : "off"));
289 observer_mode = observer_mode_1 = newval;
292 /* Tables of how to react to signals; the user sets them. */
294 static unsigned char *signal_stop;
295 static unsigned char *signal_print;
296 static unsigned char *signal_program;
298 /* Table of signals that are registered with "catch signal". A
299 non-zero entry indicates that the signal is caught by some "catch
300 signal" command. This has size GDB_SIGNAL_LAST, to accommodate all
302 static unsigned char *signal_catch;
304 /* Table of signals that the target may silently handle.
305 This is automatically determined from the flags above,
306 and simply cached here. */
307 static unsigned char *signal_pass;
309 #define SET_SIGS(nsigs,sigs,flags) \
311 int signum = (nsigs); \
312 while (signum-- > 0) \
313 if ((sigs)[signum]) \
314 (flags)[signum] = 1; \
317 #define UNSET_SIGS(nsigs,sigs,flags) \
319 int signum = (nsigs); \
320 while (signum-- > 0) \
321 if ((sigs)[signum]) \
322 (flags)[signum] = 0; \
325 /* Update the target's copy of SIGNAL_PROGRAM. The sole purpose of
326 this function is to avoid exporting `signal_program'. */
329 update_signals_program_target (void)
331 target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
334 /* Value to pass to target_resume() to cause all threads to resume. */
336 #define RESUME_ALL minus_one_ptid
338 /* Command list pointer for the "stop" placeholder. */
340 static struct cmd_list_element *stop_command;
342 /* Function inferior was in as of last step command. */
344 static struct symbol *step_start_function;
346 /* Nonzero if we want to give control to the user when we're notified
347 of shared library events by the dynamic linker. */
348 int stop_on_solib_events;
350 /* Enable or disable optional shared library event breakpoints
351 as appropriate when the above flag is changed. */
354 set_stop_on_solib_events (char *args, int from_tty, struct cmd_list_element *c)
356 update_solib_breakpoints ();
360 show_stop_on_solib_events (struct ui_file *file, int from_tty,
361 struct cmd_list_element *c, const char *value)
363 fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
367 /* Nonzero means expecting a trace trap
368 and should stop the inferior and return silently when it happens. */
372 /* Save register contents here when executing a "finish" command or are
373 about to pop a stack dummy frame, if-and-only-if proceed_to_finish is set.
374 Thus this contains the return value from the called function (assuming
375 values are returned in a register). */
377 struct regcache *stop_registers;
379 /* Nonzero after stop if current stack frame should be printed. */
381 static int stop_print_frame;
383 /* This is a cached copy of the pid/waitstatus of the last event
384 returned by target_wait()/deprecated_target_wait_hook(). This
385 information is returned by get_last_target_status(). */
386 static ptid_t target_last_wait_ptid;
387 static struct target_waitstatus target_last_waitstatus;
389 static void context_switch (ptid_t ptid);
391 void init_thread_stepping_state (struct thread_info *tss);
393 static void init_infwait_state (void);
395 static const char follow_fork_mode_child[] = "child";
396 static const char follow_fork_mode_parent[] = "parent";
398 static const char *const follow_fork_mode_kind_names[] = {
399 follow_fork_mode_child,
400 follow_fork_mode_parent,
404 static const char *follow_fork_mode_string = follow_fork_mode_parent;
406 show_follow_fork_mode_string (struct ui_file *file, int from_tty,
407 struct cmd_list_element *c, const char *value)
409 fprintf_filtered (file,
410 _("Debugger response to a program "
411 "call of fork or vfork is \"%s\".\n"),
416 /* Tell the target to follow the fork we're stopped at. Returns true
417 if the inferior should be resumed; false, if the target for some
418 reason decided it's best not to resume. */
423 int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
424 int should_resume = 1;
425 struct thread_info *tp;
427 /* Copy user stepping state to the new inferior thread. FIXME: the
428 followed fork child thread should have a copy of most of the
429 parent thread structure's run control related fields, not just these.
430 Initialized to avoid "may be used uninitialized" warnings from gcc. */
431 struct breakpoint *step_resume_breakpoint = NULL;
432 struct breakpoint *exception_resume_breakpoint = NULL;
433 CORE_ADDR step_range_start = 0;
434 CORE_ADDR step_range_end = 0;
435 struct frame_id step_frame_id = { 0 };
440 struct target_waitstatus wait_status;
442 /* Get the last target status returned by target_wait(). */
443 get_last_target_status (&wait_ptid, &wait_status);
445 /* If not stopped at a fork event, then there's nothing else to
447 if (wait_status.kind != TARGET_WAITKIND_FORKED
448 && wait_status.kind != TARGET_WAITKIND_VFORKED)
451 /* Check if we switched over from WAIT_PTID, since the event was
453 if (!ptid_equal (wait_ptid, minus_one_ptid)
454 && !ptid_equal (inferior_ptid, wait_ptid))
456 /* We did. Switch back to WAIT_PTID thread, to tell the
457 target to follow it (in either direction). We'll
458 afterwards refuse to resume, and inform the user what
460 switch_to_thread (wait_ptid);
465 tp = inferior_thread ();
467 /* If there were any forks/vforks that were caught and are now to be
468 followed, then do so now. */
469 switch (tp->pending_follow.kind)
471 case TARGET_WAITKIND_FORKED:
472 case TARGET_WAITKIND_VFORKED:
474 ptid_t parent, child;
476 /* If the user did a next/step, etc, over a fork call,
477 preserve the stepping state in the fork child. */
478 if (follow_child && should_resume)
480 step_resume_breakpoint = clone_momentary_breakpoint
481 (tp->control.step_resume_breakpoint);
482 step_range_start = tp->control.step_range_start;
483 step_range_end = tp->control.step_range_end;
484 step_frame_id = tp->control.step_frame_id;
485 exception_resume_breakpoint
486 = clone_momentary_breakpoint (tp->control.exception_resume_breakpoint);
488 /* For now, delete the parent's sr breakpoint, otherwise,
489 parent/child sr breakpoints are considered duplicates,
490 and the child version will not be installed. Remove
491 this when the breakpoints module becomes aware of
492 inferiors and address spaces. */
493 delete_step_resume_breakpoint (tp);
494 tp->control.step_range_start = 0;
495 tp->control.step_range_end = 0;
496 tp->control.step_frame_id = null_frame_id;
497 delete_exception_resume_breakpoint (tp);
500 parent = inferior_ptid;
501 child = tp->pending_follow.value.related_pid;
503 /* Tell the target to do whatever is necessary to follow
504 either parent or child. */
505 if (target_follow_fork (follow_child, detach_fork))
507 /* Target refused to follow, or there's some other reason
508 we shouldn't resume. */
513 /* This pending follow fork event is now handled, one way
514 or another. The previous selected thread may be gone
515 from the lists by now, but if it is still around, need
516 to clear the pending follow request. */
517 tp = find_thread_ptid (parent);
519 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
521 /* This makes sure we don't try to apply the "Switched
522 over from WAIT_PID" logic above. */
523 nullify_last_target_wait_ptid ();
525 /* If we followed the child, switch to it... */
528 switch_to_thread (child);
530 /* ... and preserve the stepping state, in case the
531 user was stepping over the fork call. */
534 tp = inferior_thread ();
535 tp->control.step_resume_breakpoint
536 = step_resume_breakpoint;
537 tp->control.step_range_start = step_range_start;
538 tp->control.step_range_end = step_range_end;
539 tp->control.step_frame_id = step_frame_id;
540 tp->control.exception_resume_breakpoint
541 = exception_resume_breakpoint;
545 /* If we get here, it was because we're trying to
546 resume from a fork catchpoint, but, the user
547 has switched threads away from the thread that
548 forked. In that case, the resume command
549 issued is most likely not applicable to the
550 child, so just warn, and refuse to resume. */
551 warning (_("Not resuming: switched threads "
552 "before following fork child.\n"));
555 /* Reset breakpoints in the child as appropriate. */
556 follow_inferior_reset_breakpoints ();
559 switch_to_thread (parent);
563 case TARGET_WAITKIND_SPURIOUS:
564 /* Nothing to follow. */
567 internal_error (__FILE__, __LINE__,
568 "Unexpected pending_follow.kind %d\n",
569 tp->pending_follow.kind);
573 return should_resume;
577 follow_inferior_reset_breakpoints (void)
579 struct thread_info *tp = inferior_thread ();
581 /* Was there a step_resume breakpoint? (There was if the user
582 did a "next" at the fork() call.) If so, explicitly reset its
585 step_resumes are a form of bp that are made to be per-thread.
586 Since we created the step_resume bp when the parent process
587 was being debugged, and now are switching to the child process,
588 from the breakpoint package's viewpoint, that's a switch of
589 "threads". We must update the bp's notion of which thread
590 it is for, or it'll be ignored when it triggers. */
592 if (tp->control.step_resume_breakpoint)
593 breakpoint_re_set_thread (tp->control.step_resume_breakpoint);
595 if (tp->control.exception_resume_breakpoint)
596 breakpoint_re_set_thread (tp->control.exception_resume_breakpoint);
598 /* Reinsert all breakpoints in the child. The user may have set
599 breakpoints after catching the fork, in which case those
600 were never set in the child, but only in the parent. This makes
601 sure the inserted breakpoints match the breakpoint list. */
603 breakpoint_re_set ();
604 insert_breakpoints ();
607 /* The child has exited or execed: resume threads of the parent the
608 user wanted to be executing. */
611 proceed_after_vfork_done (struct thread_info *thread,
614 int pid = * (int *) arg;
616 if (ptid_get_pid (thread->ptid) == pid
617 && is_running (thread->ptid)
618 && !is_executing (thread->ptid)
619 && !thread->stop_requested
620 && thread->suspend.stop_signal == GDB_SIGNAL_0)
623 fprintf_unfiltered (gdb_stdlog,
624 "infrun: resuming vfork parent thread %s\n",
625 target_pid_to_str (thread->ptid));
627 switch_to_thread (thread->ptid);
628 clear_proceed_status ();
629 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT, 0);
635 /* Called whenever we notice an exec or exit event, to handle
636 detaching or resuming a vfork parent. */
639 handle_vfork_child_exec_or_exit (int exec)
641 struct inferior *inf = current_inferior ();
643 if (inf->vfork_parent)
645 int resume_parent = -1;
647 /* This exec or exit marks the end of the shared memory region
648 between the parent and the child. If the user wanted to
649 detach from the parent, now is the time. */
651 if (inf->vfork_parent->pending_detach)
653 struct thread_info *tp;
654 struct cleanup *old_chain;
655 struct program_space *pspace;
656 struct address_space *aspace;
658 /* follow-fork child, detach-on-fork on. */
660 inf->vfork_parent->pending_detach = 0;
664 /* If we're handling a child exit, then inferior_ptid
665 points at the inferior's pid, not to a thread. */
666 old_chain = save_inferior_ptid ();
667 save_current_program_space ();
668 save_current_inferior ();
671 old_chain = save_current_space_and_thread ();
673 /* We're letting loose of the parent. */
674 tp = any_live_thread_of_process (inf->vfork_parent->pid);
675 switch_to_thread (tp->ptid);
677 /* We're about to detach from the parent, which implicitly
678 removes breakpoints from its address space. There's a
679 catch here: we want to reuse the spaces for the child,
680 but, parent/child are still sharing the pspace at this
681 point, although the exec in reality makes the kernel give
682 the child a fresh set of new pages. The problem here is
683 that the breakpoints module being unaware of this, would
684 likely chose the child process to write to the parent
685 address space. Swapping the child temporarily away from
686 the spaces has the desired effect. Yes, this is "sort
689 pspace = inf->pspace;
690 aspace = inf->aspace;
694 if (debug_infrun || info_verbose)
696 target_terminal_ours ();
699 fprintf_filtered (gdb_stdlog,
700 "Detaching vfork parent process "
701 "%d after child exec.\n",
702 inf->vfork_parent->pid);
704 fprintf_filtered (gdb_stdlog,
705 "Detaching vfork parent process "
706 "%d after child exit.\n",
707 inf->vfork_parent->pid);
710 target_detach (NULL, 0);
713 inf->pspace = pspace;
714 inf->aspace = aspace;
716 do_cleanups (old_chain);
720 /* We're staying attached to the parent, so, really give the
721 child a new address space. */
722 inf->pspace = add_program_space (maybe_new_address_space ());
723 inf->aspace = inf->pspace->aspace;
725 set_current_program_space (inf->pspace);
727 resume_parent = inf->vfork_parent->pid;
729 /* Break the bonds. */
730 inf->vfork_parent->vfork_child = NULL;
734 struct cleanup *old_chain;
735 struct program_space *pspace;
737 /* If this is a vfork child exiting, then the pspace and
738 aspaces were shared with the parent. Since we're
739 reporting the process exit, we'll be mourning all that is
740 found in the address space, and switching to null_ptid,
741 preparing to start a new inferior. But, since we don't
742 want to clobber the parent's address/program spaces, we
743 go ahead and create a new one for this exiting
746 /* Switch to null_ptid, so that clone_program_space doesn't want
747 to read the selected frame of a dead process. */
748 old_chain = save_inferior_ptid ();
749 inferior_ptid = null_ptid;
751 /* This inferior is dead, so avoid giving the breakpoints
752 module the option to write through to it (cloning a
753 program space resets breakpoints). */
756 pspace = add_program_space (maybe_new_address_space ());
757 set_current_program_space (pspace);
759 inf->symfile_flags = SYMFILE_NO_READ;
760 clone_program_space (pspace, inf->vfork_parent->pspace);
761 inf->pspace = pspace;
762 inf->aspace = pspace->aspace;
764 /* Put back inferior_ptid. We'll continue mourning this
766 do_cleanups (old_chain);
768 resume_parent = inf->vfork_parent->pid;
769 /* Break the bonds. */
770 inf->vfork_parent->vfork_child = NULL;
773 inf->vfork_parent = NULL;
775 gdb_assert (current_program_space == inf->pspace);
777 if (non_stop && resume_parent != -1)
779 /* If the user wanted the parent to be running, let it go
781 struct cleanup *old_chain = make_cleanup_restore_current_thread ();
784 fprintf_unfiltered (gdb_stdlog,
785 "infrun: resuming vfork parent process %d\n",
788 iterate_over_threads (proceed_after_vfork_done, &resume_parent);
790 do_cleanups (old_chain);
795 /* Enum strings for "set|show follow-exec-mode". */
797 static const char follow_exec_mode_new[] = "new";
798 static const char follow_exec_mode_same[] = "same";
799 static const char *const follow_exec_mode_names[] =
801 follow_exec_mode_new,
802 follow_exec_mode_same,
806 static const char *follow_exec_mode_string = follow_exec_mode_same;
808 show_follow_exec_mode_string (struct ui_file *file, int from_tty,
809 struct cmd_list_element *c, const char *value)
811 fprintf_filtered (file, _("Follow exec mode is \"%s\".\n"), value);
814 /* EXECD_PATHNAME is assumed to be non-NULL. */
817 follow_exec (ptid_t pid, char *execd_pathname)
819 struct thread_info *th = inferior_thread ();
820 struct inferior *inf = current_inferior ();
822 /* This is an exec event that we actually wish to pay attention to.
823 Refresh our symbol table to the newly exec'd program, remove any
826 If there are breakpoints, they aren't really inserted now,
827 since the exec() transformed our inferior into a fresh set
830 We want to preserve symbolic breakpoints on the list, since
831 we have hopes that they can be reset after the new a.out's
832 symbol table is read.
834 However, any "raw" breakpoints must be removed from the list
835 (e.g., the solib bp's), since their address is probably invalid
838 And, we DON'T want to call delete_breakpoints() here, since
839 that may write the bp's "shadow contents" (the instruction
840 value that was overwritten witha TRAP instruction). Since
841 we now have a new a.out, those shadow contents aren't valid. */
843 mark_breakpoints_out ();
845 update_breakpoints_after_exec ();
847 /* If there was one, it's gone now. We cannot truly step-to-next
848 statement through an exec(). */
849 th->control.step_resume_breakpoint = NULL;
850 th->control.exception_resume_breakpoint = NULL;
851 th->control.step_range_start = 0;
852 th->control.step_range_end = 0;
854 /* The target reports the exec event to the main thread, even if
855 some other thread does the exec, and even if the main thread was
856 already stopped --- if debugging in non-stop mode, it's possible
857 the user had the main thread held stopped in the previous image
858 --- release it now. This is the same behavior as step-over-exec
859 with scheduler-locking on in all-stop mode. */
860 th->stop_requested = 0;
862 /* What is this a.out's name? */
863 printf_unfiltered (_("%s is executing new program: %s\n"),
864 target_pid_to_str (inferior_ptid),
867 /* We've followed the inferior through an exec. Therefore, the
868 inferior has essentially been killed & reborn. */
870 gdb_flush (gdb_stdout);
872 breakpoint_init_inferior (inf_execd);
874 if (gdb_sysroot && *gdb_sysroot)
876 char *name = alloca (strlen (gdb_sysroot)
877 + strlen (execd_pathname)
880 strcpy (name, gdb_sysroot);
881 strcat (name, execd_pathname);
882 execd_pathname = name;
885 /* Reset the shared library package. This ensures that we get a
886 shlib event when the child reaches "_start", at which point the
887 dld will have had a chance to initialize the child. */
888 /* Also, loading a symbol file below may trigger symbol lookups, and
889 we don't want those to be satisfied by the libraries of the
890 previous incarnation of this process. */
891 no_shared_libraries (NULL, 0);
893 if (follow_exec_mode_string == follow_exec_mode_new)
895 struct program_space *pspace;
897 /* The user wants to keep the old inferior and program spaces
898 around. Create a new fresh one, and switch to it. */
900 inf = add_inferior (current_inferior ()->pid);
901 pspace = add_program_space (maybe_new_address_space ());
902 inf->pspace = pspace;
903 inf->aspace = pspace->aspace;
905 exit_inferior_num_silent (current_inferior ()->num);
907 set_current_inferior (inf);
908 set_current_program_space (pspace);
912 /* The old description may no longer be fit for the new image.
913 E.g, a 64-bit process exec'ed a 32-bit process. Clear the
914 old description; we'll read a new one below. No need to do
915 this on "follow-exec-mode new", as the old inferior stays
916 around (its description is later cleared/refetched on
918 target_clear_description ();
921 gdb_assert (current_program_space == inf->pspace);
923 /* That a.out is now the one to use. */
924 exec_file_attach (execd_pathname, 0);
926 /* SYMFILE_DEFER_BP_RESET is used as the proper displacement for PIE
927 (Position Independent Executable) main symbol file will get applied by
928 solib_create_inferior_hook below. breakpoint_re_set would fail to insert
929 the breakpoints with the zero displacement. */
931 symbol_file_add (execd_pathname,
933 | SYMFILE_MAINLINE | SYMFILE_DEFER_BP_RESET),
936 if ((inf->symfile_flags & SYMFILE_NO_READ) == 0)
937 set_initial_language ();
939 /* If the target can specify a description, read it. Must do this
940 after flipping to the new executable (because the target supplied
941 description must be compatible with the executable's
942 architecture, and the old executable may e.g., be 32-bit, while
943 the new one 64-bit), and before anything involving memory or
945 target_find_description ();
947 solib_create_inferior_hook (0);
949 jit_inferior_created_hook ();
951 breakpoint_re_set ();
953 /* Reinsert all breakpoints. (Those which were symbolic have
954 been reset to the proper address in the new a.out, thanks
955 to symbol_file_command...). */
956 insert_breakpoints ();
958 /* The next resume of this inferior should bring it to the shlib
959 startup breakpoints. (If the user had also set bp's on
960 "main" from the old (parent) process, then they'll auto-
961 matically get reset there in the new process.). */
964 /* Non-zero if we just simulating a single-step. This is needed
965 because we cannot remove the breakpoints in the inferior process
966 until after the `wait' in `wait_for_inferior'. */
967 static int singlestep_breakpoints_inserted_p = 0;
969 /* The thread we inserted single-step breakpoints for. */
970 static ptid_t singlestep_ptid;
972 /* PC when we started this single-step. */
973 static CORE_ADDR singlestep_pc;
975 /* If another thread hit the singlestep breakpoint, we save the original
976 thread here so that we can resume single-stepping it later. */
977 static ptid_t saved_singlestep_ptid;
978 static int stepping_past_singlestep_breakpoint;
980 /* If not equal to null_ptid, this means that after stepping over breakpoint
981 is finished, we need to switch to deferred_step_ptid, and step it.
983 The use case is when one thread has hit a breakpoint, and then the user
984 has switched to another thread and issued 'step'. We need to step over
985 breakpoint in the thread which hit the breakpoint, but then continue
986 stepping the thread user has selected. */
987 static ptid_t deferred_step_ptid;
989 /* Displaced stepping. */
991 /* In non-stop debugging mode, we must take special care to manage
992 breakpoints properly; in particular, the traditional strategy for
993 stepping a thread past a breakpoint it has hit is unsuitable.
994 'Displaced stepping' is a tactic for stepping one thread past a
995 breakpoint it has hit while ensuring that other threads running
996 concurrently will hit the breakpoint as they should.
998 The traditional way to step a thread T off a breakpoint in a
999 multi-threaded program in all-stop mode is as follows:
1001 a0) Initially, all threads are stopped, and breakpoints are not
1003 a1) We single-step T, leaving breakpoints uninserted.
1004 a2) We insert breakpoints, and resume all threads.
1006 In non-stop debugging, however, this strategy is unsuitable: we
1007 don't want to have to stop all threads in the system in order to
1008 continue or step T past a breakpoint. Instead, we use displaced
1011 n0) Initially, T is stopped, other threads are running, and
1012 breakpoints are inserted.
1013 n1) We copy the instruction "under" the breakpoint to a separate
1014 location, outside the main code stream, making any adjustments
1015 to the instruction, register, and memory state as directed by
1017 n2) We single-step T over the instruction at its new location.
1018 n3) We adjust the resulting register and memory state as directed
1019 by T's architecture. This includes resetting T's PC to point
1020 back into the main instruction stream.
1023 This approach depends on the following gdbarch methods:
1025 - gdbarch_max_insn_length and gdbarch_displaced_step_location
1026 indicate where to copy the instruction, and how much space must
1027 be reserved there. We use these in step n1.
1029 - gdbarch_displaced_step_copy_insn copies a instruction to a new
1030 address, and makes any necessary adjustments to the instruction,
1031 register contents, and memory. We use this in step n1.
1033 - gdbarch_displaced_step_fixup adjusts registers and memory after
1034 we have successfuly single-stepped the instruction, to yield the
1035 same effect the instruction would have had if we had executed it
1036 at its original address. We use this in step n3.
1038 - gdbarch_displaced_step_free_closure provides cleanup.
1040 The gdbarch_displaced_step_copy_insn and
1041 gdbarch_displaced_step_fixup functions must be written so that
1042 copying an instruction with gdbarch_displaced_step_copy_insn,
1043 single-stepping across the copied instruction, and then applying
1044 gdbarch_displaced_insn_fixup should have the same effects on the
1045 thread's memory and registers as stepping the instruction in place
1046 would have. Exactly which responsibilities fall to the copy and
1047 which fall to the fixup is up to the author of those functions.
1049 See the comments in gdbarch.sh for details.
1051 Note that displaced stepping and software single-step cannot
1052 currently be used in combination, although with some care I think
1053 they could be made to. Software single-step works by placing
1054 breakpoints on all possible subsequent instructions; if the
1055 displaced instruction is a PC-relative jump, those breakpoints
1056 could fall in very strange places --- on pages that aren't
1057 executable, or at addresses that are not proper instruction
1058 boundaries. (We do generally let other threads run while we wait
1059 to hit the software single-step breakpoint, and they might
1060 encounter such a corrupted instruction.) One way to work around
1061 this would be to have gdbarch_displaced_step_copy_insn fully
1062 simulate the effect of PC-relative instructions (and return NULL)
1063 on architectures that use software single-stepping.
1065 In non-stop mode, we can have independent and simultaneous step
1066 requests, so more than one thread may need to simultaneously step
1067 over a breakpoint. The current implementation assumes there is
1068 only one scratch space per process. In this case, we have to
1069 serialize access to the scratch space. If thread A wants to step
1070 over a breakpoint, but we are currently waiting for some other
1071 thread to complete a displaced step, we leave thread A stopped and
1072 place it in the displaced_step_request_queue. Whenever a displaced
1073 step finishes, we pick the next thread in the queue and start a new
1074 displaced step operation on it. See displaced_step_prepare and
1075 displaced_step_fixup for details. */
1077 struct displaced_step_request
1080 struct displaced_step_request *next;
1083 /* Per-inferior displaced stepping state. */
1084 struct displaced_step_inferior_state
1086 /* Pointer to next in linked list. */
1087 struct displaced_step_inferior_state *next;
1089 /* The process this displaced step state refers to. */
1092 /* A queue of pending displaced stepping requests. One entry per
1093 thread that needs to do a displaced step. */
1094 struct displaced_step_request *step_request_queue;
1096 /* If this is not null_ptid, this is the thread carrying out a
1097 displaced single-step in process PID. This thread's state will
1098 require fixing up once it has completed its step. */
1101 /* The architecture the thread had when we stepped it. */
1102 struct gdbarch *step_gdbarch;
1104 /* The closure provided gdbarch_displaced_step_copy_insn, to be used
1105 for post-step cleanup. */
1106 struct displaced_step_closure *step_closure;
1108 /* The address of the original instruction, and the copy we
1110 CORE_ADDR step_original, step_copy;
1112 /* Saved contents of copy area. */
1113 gdb_byte *step_saved_copy;
1116 /* The list of states of processes involved in displaced stepping
1118 static struct displaced_step_inferior_state *displaced_step_inferior_states;
1120 /* Get the displaced stepping state of process PID. */
1122 static struct displaced_step_inferior_state *
1123 get_displaced_stepping_state (int pid)
1125 struct displaced_step_inferior_state *state;
1127 for (state = displaced_step_inferior_states;
1129 state = state->next)
1130 if (state->pid == pid)
1136 /* Add a new displaced stepping state for process PID to the displaced
1137 stepping state list, or return a pointer to an already existing
1138 entry, if it already exists. Never returns NULL. */
1140 static struct displaced_step_inferior_state *
1141 add_displaced_stepping_state (int pid)
1143 struct displaced_step_inferior_state *state;
1145 for (state = displaced_step_inferior_states;
1147 state = state->next)
1148 if (state->pid == pid)
1151 state = xcalloc (1, sizeof (*state));
1153 state->next = displaced_step_inferior_states;
1154 displaced_step_inferior_states = state;
1159 /* If inferior is in displaced stepping, and ADDR equals to starting address
1160 of copy area, return corresponding displaced_step_closure. Otherwise,
1163 struct displaced_step_closure*
1164 get_displaced_step_closure_by_addr (CORE_ADDR addr)
1166 struct displaced_step_inferior_state *displaced
1167 = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
1169 /* If checking the mode of displaced instruction in copy area. */
1170 if (displaced && !ptid_equal (displaced->step_ptid, null_ptid)
1171 && (displaced->step_copy == addr))
1172 return displaced->step_closure;
1177 /* Remove the displaced stepping state of process PID. */
1180 remove_displaced_stepping_state (int pid)
1182 struct displaced_step_inferior_state *it, **prev_next_p;
1184 gdb_assert (pid != 0);
1186 it = displaced_step_inferior_states;
1187 prev_next_p = &displaced_step_inferior_states;
1192 *prev_next_p = it->next;
1197 prev_next_p = &it->next;
1203 infrun_inferior_exit (struct inferior *inf)
1205 remove_displaced_stepping_state (inf->pid);
1208 /* If ON, and the architecture supports it, GDB will use displaced
1209 stepping to step over breakpoints. If OFF, or if the architecture
1210 doesn't support it, GDB will instead use the traditional
1211 hold-and-step approach. If AUTO (which is the default), GDB will
1212 decide which technique to use to step over breakpoints depending on
1213 which of all-stop or non-stop mode is active --- displaced stepping
1214 in non-stop mode; hold-and-step in all-stop mode. */
1216 static enum auto_boolean can_use_displaced_stepping = AUTO_BOOLEAN_AUTO;
1219 show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
1220 struct cmd_list_element *c,
1223 if (can_use_displaced_stepping == AUTO_BOOLEAN_AUTO)
1224 fprintf_filtered (file,
1225 _("Debugger's willingness to use displaced stepping "
1226 "to step over breakpoints is %s (currently %s).\n"),
1227 value, non_stop ? "on" : "off");
1229 fprintf_filtered (file,
1230 _("Debugger's willingness to use displaced stepping "
1231 "to step over breakpoints is %s.\n"), value);
1234 /* Return non-zero if displaced stepping can/should be used to step
1235 over breakpoints. */
1238 use_displaced_stepping (struct gdbarch *gdbarch)
1240 return (((can_use_displaced_stepping == AUTO_BOOLEAN_AUTO && non_stop)
1241 || can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
1242 && gdbarch_displaced_step_copy_insn_p (gdbarch)
1243 && find_record_target () == NULL);
1246 /* Clean out any stray displaced stepping state. */
1248 displaced_step_clear (struct displaced_step_inferior_state *displaced)
1250 /* Indicate that there is no cleanup pending. */
1251 displaced->step_ptid = null_ptid;
1253 if (displaced->step_closure)
1255 gdbarch_displaced_step_free_closure (displaced->step_gdbarch,
1256 displaced->step_closure);
1257 displaced->step_closure = NULL;
1262 displaced_step_clear_cleanup (void *arg)
1264 struct displaced_step_inferior_state *state = arg;
1266 displaced_step_clear (state);
1269 /* Dump LEN bytes at BUF in hex to FILE, followed by a newline. */
1271 displaced_step_dump_bytes (struct ui_file *file,
1272 const gdb_byte *buf,
1277 for (i = 0; i < len; i++)
1278 fprintf_unfiltered (file, "%02x ", buf[i]);
1279 fputs_unfiltered ("\n", file);
1282 /* Prepare to single-step, using displaced stepping.
1284 Note that we cannot use displaced stepping when we have a signal to
1285 deliver. If we have a signal to deliver and an instruction to step
1286 over, then after the step, there will be no indication from the
1287 target whether the thread entered a signal handler or ignored the
1288 signal and stepped over the instruction successfully --- both cases
1289 result in a simple SIGTRAP. In the first case we mustn't do a
1290 fixup, and in the second case we must --- but we can't tell which.
1291 Comments in the code for 'random signals' in handle_inferior_event
1292 explain how we handle this case instead.
1294 Returns 1 if preparing was successful -- this thread is going to be
1295 stepped now; or 0 if displaced stepping this thread got queued. */
1297 displaced_step_prepare (ptid_t ptid)
1299 struct cleanup *old_cleanups, *ignore_cleanups;
1300 struct thread_info *tp = find_thread_ptid (ptid);
1301 struct regcache *regcache = get_thread_regcache (ptid);
1302 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1303 CORE_ADDR original, copy;
1305 struct displaced_step_closure *closure;
1306 struct displaced_step_inferior_state *displaced;
1309 /* We should never reach this function if the architecture does not
1310 support displaced stepping. */
1311 gdb_assert (gdbarch_displaced_step_copy_insn_p (gdbarch));
1313 /* Disable range stepping while executing in the scratch pad. We
1314 want a single-step even if executing the displaced instruction in
1315 the scratch buffer lands within the stepping range (e.g., a
1317 tp->control.may_range_step = 0;
1319 /* We have to displaced step one thread at a time, as we only have
1320 access to a single scratch space per inferior. */
1322 displaced = add_displaced_stepping_state (ptid_get_pid (ptid));
1324 if (!ptid_equal (displaced->step_ptid, null_ptid))
1326 /* Already waiting for a displaced step to finish. Defer this
1327 request and place in queue. */
1328 struct displaced_step_request *req, *new_req;
1330 if (debug_displaced)
1331 fprintf_unfiltered (gdb_stdlog,
1332 "displaced: defering step of %s\n",
1333 target_pid_to_str (ptid));
1335 new_req = xmalloc (sizeof (*new_req));
1336 new_req->ptid = ptid;
1337 new_req->next = NULL;
1339 if (displaced->step_request_queue)
1341 for (req = displaced->step_request_queue;
1345 req->next = new_req;
1348 displaced->step_request_queue = new_req;
1354 if (debug_displaced)
1355 fprintf_unfiltered (gdb_stdlog,
1356 "displaced: stepping %s now\n",
1357 target_pid_to_str (ptid));
1360 displaced_step_clear (displaced);
1362 old_cleanups = save_inferior_ptid ();
1363 inferior_ptid = ptid;
1365 original = regcache_read_pc (regcache);
1367 copy = gdbarch_displaced_step_location (gdbarch);
1368 len = gdbarch_max_insn_length (gdbarch);
1370 /* Save the original contents of the copy area. */
1371 displaced->step_saved_copy = xmalloc (len);
1372 ignore_cleanups = make_cleanup (free_current_contents,
1373 &displaced->step_saved_copy);
1374 status = target_read_memory (copy, displaced->step_saved_copy, len);
1376 throw_error (MEMORY_ERROR,
1377 _("Error accessing memory address %s (%s) for "
1378 "displaced-stepping scratch space."),
1379 paddress (gdbarch, copy), safe_strerror (status));
1380 if (debug_displaced)
1382 fprintf_unfiltered (gdb_stdlog, "displaced: saved %s: ",
1383 paddress (gdbarch, copy));
1384 displaced_step_dump_bytes (gdb_stdlog,
1385 displaced->step_saved_copy,
1389 closure = gdbarch_displaced_step_copy_insn (gdbarch,
1390 original, copy, regcache);
1392 /* We don't support the fully-simulated case at present. */
1393 gdb_assert (closure);
1395 /* Save the information we need to fix things up if the step
1397 displaced->step_ptid = ptid;
1398 displaced->step_gdbarch = gdbarch;
1399 displaced->step_closure = closure;
1400 displaced->step_original = original;
1401 displaced->step_copy = copy;
1403 make_cleanup (displaced_step_clear_cleanup, displaced);
1405 /* Resume execution at the copy. */
1406 regcache_write_pc (regcache, copy);
1408 discard_cleanups (ignore_cleanups);
1410 do_cleanups (old_cleanups);
1412 if (debug_displaced)
1413 fprintf_unfiltered (gdb_stdlog, "displaced: displaced pc to %s\n",
1414 paddress (gdbarch, copy));
1420 write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
1421 const gdb_byte *myaddr, int len)
1423 struct cleanup *ptid_cleanup = save_inferior_ptid ();
1425 inferior_ptid = ptid;
1426 write_memory (memaddr, myaddr, len);
1427 do_cleanups (ptid_cleanup);
1430 /* Restore the contents of the copy area for thread PTID. */
1433 displaced_step_restore (struct displaced_step_inferior_state *displaced,
1436 ULONGEST len = gdbarch_max_insn_length (displaced->step_gdbarch);
1438 write_memory_ptid (ptid, displaced->step_copy,
1439 displaced->step_saved_copy, len);
1440 if (debug_displaced)
1441 fprintf_unfiltered (gdb_stdlog, "displaced: restored %s %s\n",
1442 target_pid_to_str (ptid),
1443 paddress (displaced->step_gdbarch,
1444 displaced->step_copy));
1448 displaced_step_fixup (ptid_t event_ptid, enum gdb_signal signal)
1450 struct cleanup *old_cleanups;
1451 struct displaced_step_inferior_state *displaced
1452 = get_displaced_stepping_state (ptid_get_pid (event_ptid));
1454 /* Was any thread of this process doing a displaced step? */
1455 if (displaced == NULL)
1458 /* Was this event for the pid we displaced? */
1459 if (ptid_equal (displaced->step_ptid, null_ptid)
1460 || ! ptid_equal (displaced->step_ptid, event_ptid))
1463 old_cleanups = make_cleanup (displaced_step_clear_cleanup, displaced);
1465 displaced_step_restore (displaced, displaced->step_ptid);
1467 /* Did the instruction complete successfully? */
1468 if (signal == GDB_SIGNAL_TRAP)
1470 /* Fix up the resulting state. */
1471 gdbarch_displaced_step_fixup (displaced->step_gdbarch,
1472 displaced->step_closure,
1473 displaced->step_original,
1474 displaced->step_copy,
1475 get_thread_regcache (displaced->step_ptid));
1479 /* Since the instruction didn't complete, all we can do is
1481 struct regcache *regcache = get_thread_regcache (event_ptid);
1482 CORE_ADDR pc = regcache_read_pc (regcache);
1484 pc = displaced->step_original + (pc - displaced->step_copy);
1485 regcache_write_pc (regcache, pc);
1488 do_cleanups (old_cleanups);
1490 displaced->step_ptid = null_ptid;
1492 /* Are there any pending displaced stepping requests? If so, run
1493 one now. Leave the state object around, since we're likely to
1494 need it again soon. */
1495 while (displaced->step_request_queue)
1497 struct displaced_step_request *head;
1499 struct regcache *regcache;
1500 struct gdbarch *gdbarch;
1501 CORE_ADDR actual_pc;
1502 struct address_space *aspace;
1504 head = displaced->step_request_queue;
1506 displaced->step_request_queue = head->next;
1509 context_switch (ptid);
1511 regcache = get_thread_regcache (ptid);
1512 actual_pc = regcache_read_pc (regcache);
1513 aspace = get_regcache_aspace (regcache);
1515 if (breakpoint_here_p (aspace, actual_pc))
1517 if (debug_displaced)
1518 fprintf_unfiltered (gdb_stdlog,
1519 "displaced: stepping queued %s now\n",
1520 target_pid_to_str (ptid));
1522 displaced_step_prepare (ptid);
1524 gdbarch = get_regcache_arch (regcache);
1526 if (debug_displaced)
1528 CORE_ADDR actual_pc = regcache_read_pc (regcache);
1531 fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
1532 paddress (gdbarch, actual_pc));
1533 read_memory (actual_pc, buf, sizeof (buf));
1534 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1537 if (gdbarch_displaced_step_hw_singlestep (gdbarch,
1538 displaced->step_closure))
1539 target_resume (ptid, 1, GDB_SIGNAL_0);
1541 target_resume (ptid, 0, GDB_SIGNAL_0);
1543 /* Done, we're stepping a thread. */
1549 struct thread_info *tp = inferior_thread ();
1551 /* The breakpoint we were sitting under has since been
1553 tp->control.trap_expected = 0;
1555 /* Go back to what we were trying to do. */
1556 step = currently_stepping (tp);
1558 if (debug_displaced)
1559 fprintf_unfiltered (gdb_stdlog,
1560 "displaced: breakpoint is gone: %s, step(%d)\n",
1561 target_pid_to_str (tp->ptid), step);
1563 target_resume (ptid, step, GDB_SIGNAL_0);
1564 tp->suspend.stop_signal = GDB_SIGNAL_0;
1566 /* This request was discarded. See if there's any other
1567 thread waiting for its turn. */
1572 /* Update global variables holding ptids to hold NEW_PTID if they were
1573 holding OLD_PTID. */
1575 infrun_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1577 struct displaced_step_request *it;
1578 struct displaced_step_inferior_state *displaced;
1580 if (ptid_equal (inferior_ptid, old_ptid))
1581 inferior_ptid = new_ptid;
1583 if (ptid_equal (singlestep_ptid, old_ptid))
1584 singlestep_ptid = new_ptid;
1586 if (ptid_equal (deferred_step_ptid, old_ptid))
1587 deferred_step_ptid = new_ptid;
1589 for (displaced = displaced_step_inferior_states;
1591 displaced = displaced->next)
1593 if (ptid_equal (displaced->step_ptid, old_ptid))
1594 displaced->step_ptid = new_ptid;
1596 for (it = displaced->step_request_queue; it; it = it->next)
1597 if (ptid_equal (it->ptid, old_ptid))
1598 it->ptid = new_ptid;
1605 /* Things to clean up if we QUIT out of resume (). */
1607 resume_cleanups (void *ignore)
1612 static const char schedlock_off[] = "off";
1613 static const char schedlock_on[] = "on";
1614 static const char schedlock_step[] = "step";
1615 static const char *const scheduler_enums[] = {
1621 static const char *scheduler_mode = schedlock_off;
1623 show_scheduler_mode (struct ui_file *file, int from_tty,
1624 struct cmd_list_element *c, const char *value)
1626 fprintf_filtered (file,
1627 _("Mode for locking scheduler "
1628 "during execution is \"%s\".\n"),
1633 set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
1635 if (!target_can_lock_scheduler)
1637 scheduler_mode = schedlock_off;
1638 error (_("Target '%s' cannot support this command."), target_shortname);
1642 /* True if execution commands resume all threads of all processes by
1643 default; otherwise, resume only threads of the current inferior
1645 int sched_multi = 0;
1647 /* Try to setup for software single stepping over the specified location.
1648 Return 1 if target_resume() should use hardware single step.
1650 GDBARCH the current gdbarch.
1651 PC the location to step over. */
1654 maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc)
1658 if (execution_direction == EXEC_FORWARD
1659 && gdbarch_software_single_step_p (gdbarch)
1660 && gdbarch_software_single_step (gdbarch, get_current_frame ()))
1663 /* Do not pull these breakpoints until after a `wait' in
1664 `wait_for_inferior'. */
1665 singlestep_breakpoints_inserted_p = 1;
1666 singlestep_ptid = inferior_ptid;
1672 /* Return a ptid representing the set of threads that we will proceed,
1673 in the perspective of the user/frontend. We may actually resume
1674 fewer threads at first, e.g., if a thread is stopped at a
1675 breakpoint that needs stepping-off, but that should not be visible
1676 to the user/frontend, and neither should the frontend/user be
1677 allowed to proceed any of the threads that happen to be stopped for
1678 internal run control handling, if a previous command wanted them
1682 user_visible_resume_ptid (int step)
1684 /* By default, resume all threads of all processes. */
1685 ptid_t resume_ptid = RESUME_ALL;
1687 /* Maybe resume only all threads of the current process. */
1688 if (!sched_multi && target_supports_multi_process ())
1690 resume_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
1693 /* Maybe resume a single thread after all. */
1696 /* With non-stop mode on, threads are always handled
1698 resume_ptid = inferior_ptid;
1700 else if ((scheduler_mode == schedlock_on)
1701 || (scheduler_mode == schedlock_step
1702 && (step || singlestep_breakpoints_inserted_p)))
1704 /* User-settable 'scheduler' mode requires solo thread resume. */
1705 resume_ptid = inferior_ptid;
1711 /* Resume the inferior, but allow a QUIT. This is useful if the user
1712 wants to interrupt some lengthy single-stepping operation
1713 (for child processes, the SIGINT goes to the inferior, and so
1714 we get a SIGINT random_signal, but for remote debugging and perhaps
1715 other targets, that's not true).
1717 STEP nonzero if we should step (zero to continue instead).
1718 SIG is the signal to give the inferior (zero for none). */
1720 resume (int step, enum gdb_signal sig)
1722 int should_resume = 1;
1723 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
1724 struct regcache *regcache = get_current_regcache ();
1725 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1726 struct thread_info *tp = inferior_thread ();
1727 CORE_ADDR pc = regcache_read_pc (regcache);
1728 struct address_space *aspace = get_regcache_aspace (regcache);
1732 if (current_inferior ()->waiting_for_vfork_done)
1734 /* Don't try to single-step a vfork parent that is waiting for
1735 the child to get out of the shared memory region (by exec'ing
1736 or exiting). This is particularly important on software
1737 single-step archs, as the child process would trip on the
1738 software single step breakpoint inserted for the parent
1739 process. Since the parent will not actually execute any
1740 instruction until the child is out of the shared region (such
1741 are vfork's semantics), it is safe to simply continue it.
1742 Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
1743 the parent, and tell it to `keep_going', which automatically
1744 re-sets it stepping. */
1746 fprintf_unfiltered (gdb_stdlog,
1747 "infrun: resume : clear step\n");
1752 fprintf_unfiltered (gdb_stdlog,
1753 "infrun: resume (step=%d, signal=%s), "
1754 "trap_expected=%d, current thread [%s] at %s\n",
1755 step, gdb_signal_to_symbol_string (sig),
1756 tp->control.trap_expected,
1757 target_pid_to_str (inferior_ptid),
1758 paddress (gdbarch, pc));
1760 /* Normally, by the time we reach `resume', the breakpoints are either
1761 removed or inserted, as appropriate. The exception is if we're sitting
1762 at a permanent breakpoint; we need to step over it, but permanent
1763 breakpoints can't be removed. So we have to test for it here. */
1764 if (breakpoint_here_p (aspace, pc) == permanent_breakpoint_here)
1766 if (gdbarch_skip_permanent_breakpoint_p (gdbarch))
1767 gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
1770 The program is stopped at a permanent breakpoint, but GDB does not know\n\
1771 how to step past a permanent breakpoint on this architecture. Try using\n\
1772 a command like `return' or `jump' to continue execution."));
1775 /* If we have a breakpoint to step over, make sure to do a single
1776 step only. Same if we have software watchpoints. */
1777 if (tp->control.trap_expected || bpstat_should_step ())
1778 tp->control.may_range_step = 0;
1780 /* If enabled, step over breakpoints by executing a copy of the
1781 instruction at a different address.
1783 We can't use displaced stepping when we have a signal to deliver;
1784 the comments for displaced_step_prepare explain why. The
1785 comments in the handle_inferior event for dealing with 'random
1786 signals' explain what we do instead.
1788 We can't use displaced stepping when we are waiting for vfork_done
1789 event, displaced stepping breaks the vfork child similarly as single
1790 step software breakpoint. */
1791 if (use_displaced_stepping (gdbarch)
1792 && (tp->control.trap_expected
1793 || (step && gdbarch_software_single_step_p (gdbarch)))
1794 && sig == GDB_SIGNAL_0
1795 && !current_inferior ()->waiting_for_vfork_done)
1797 struct displaced_step_inferior_state *displaced;
1799 if (!displaced_step_prepare (inferior_ptid))
1801 /* Got placed in displaced stepping queue. Will be resumed
1802 later when all the currently queued displaced stepping
1803 requests finish. The thread is not executing at this point,
1804 and the call to set_executing will be made later. But we
1805 need to call set_running here, since from frontend point of view,
1806 the thread is running. */
1807 set_running (inferior_ptid, 1);
1808 discard_cleanups (old_cleanups);
1812 /* Update pc to reflect the new address from which we will execute
1813 instructions due to displaced stepping. */
1814 pc = regcache_read_pc (get_thread_regcache (inferior_ptid));
1816 displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
1817 step = gdbarch_displaced_step_hw_singlestep (gdbarch,
1818 displaced->step_closure);
1821 /* Do we need to do it the hard way, w/temp breakpoints? */
1823 step = maybe_software_singlestep (gdbarch, pc);
1825 /* Currently, our software single-step implementation leads to different
1826 results than hardware single-stepping in one situation: when stepping
1827 into delivering a signal which has an associated signal handler,
1828 hardware single-step will stop at the first instruction of the handler,
1829 while software single-step will simply skip execution of the handler.
1831 For now, this difference in behavior is accepted since there is no
1832 easy way to actually implement single-stepping into a signal handler
1833 without kernel support.
1835 However, there is one scenario where this difference leads to follow-on
1836 problems: if we're stepping off a breakpoint by removing all breakpoints
1837 and then single-stepping. In this case, the software single-step
1838 behavior means that even if there is a *breakpoint* in the signal
1839 handler, GDB still would not stop.
1841 Fortunately, we can at least fix this particular issue. We detect
1842 here the case where we are about to deliver a signal while software
1843 single-stepping with breakpoints removed. In this situation, we
1844 revert the decisions to remove all breakpoints and insert single-
1845 step breakpoints, and instead we install a step-resume breakpoint
1846 at the current address, deliver the signal without stepping, and
1847 once we arrive back at the step-resume breakpoint, actually step
1848 over the breakpoint we originally wanted to step over. */
1849 if (singlestep_breakpoints_inserted_p
1850 && tp->control.trap_expected && sig != GDB_SIGNAL_0)
1852 /* If we have nested signals or a pending signal is delivered
1853 immediately after a handler returns, might might already have
1854 a step-resume breakpoint set on the earlier handler. We cannot
1855 set another step-resume breakpoint; just continue on until the
1856 original breakpoint is hit. */
1857 if (tp->control.step_resume_breakpoint == NULL)
1859 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
1860 tp->step_after_step_resume_breakpoint = 1;
1863 remove_single_step_breakpoints ();
1864 singlestep_breakpoints_inserted_p = 0;
1866 insert_breakpoints ();
1867 tp->control.trap_expected = 0;
1874 /* If STEP is set, it's a request to use hardware stepping
1875 facilities. But in that case, we should never
1876 use singlestep breakpoint. */
1877 gdb_assert (!(singlestep_breakpoints_inserted_p && step));
1879 /* Decide the set of threads to ask the target to resume. Start
1880 by assuming everything will be resumed, than narrow the set
1881 by applying increasingly restricting conditions. */
1882 resume_ptid = user_visible_resume_ptid (step);
1884 /* Maybe resume a single thread after all. */
1885 if (singlestep_breakpoints_inserted_p
1886 && stepping_past_singlestep_breakpoint)
1888 /* The situation here is as follows. In thread T1 we wanted to
1889 single-step. Lacking hardware single-stepping we've
1890 set breakpoint at the PC of the next instruction -- call it
1891 P. After resuming, we've hit that breakpoint in thread T2.
1892 Now we've removed original breakpoint, inserted breakpoint
1893 at P+1, and try to step to advance T2 past breakpoint.
1894 We need to step only T2, as if T1 is allowed to freely run,
1895 it can run past P, and if other threads are allowed to run,
1896 they can hit breakpoint at P+1, and nested hits of single-step
1897 breakpoints is not something we'd want -- that's complicated
1898 to support, and has no value. */
1899 resume_ptid = inferior_ptid;
1901 else if ((step || singlestep_breakpoints_inserted_p)
1902 && tp->control.trap_expected)
1904 /* We're allowing a thread to run past a breakpoint it has
1905 hit, by single-stepping the thread with the breakpoint
1906 removed. In which case, we need to single-step only this
1907 thread, and keep others stopped, as they can miss this
1908 breakpoint if allowed to run.
1910 The current code actually removes all breakpoints when
1911 doing this, not just the one being stepped over, so if we
1912 let other threads run, we can actually miss any
1913 breakpoint, not just the one at PC. */
1914 resume_ptid = inferior_ptid;
1917 if (gdbarch_cannot_step_breakpoint (gdbarch))
1919 /* Most targets can step a breakpoint instruction, thus
1920 executing it normally. But if this one cannot, just
1921 continue and we will hit it anyway. */
1922 if (step && breakpoint_inserted_here_p (aspace, pc))
1927 && use_displaced_stepping (gdbarch)
1928 && tp->control.trap_expected)
1930 struct regcache *resume_regcache = get_thread_regcache (resume_ptid);
1931 struct gdbarch *resume_gdbarch = get_regcache_arch (resume_regcache);
1932 CORE_ADDR actual_pc = regcache_read_pc (resume_regcache);
1935 fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
1936 paddress (resume_gdbarch, actual_pc));
1937 read_memory (actual_pc, buf, sizeof (buf));
1938 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1941 if (tp->control.may_range_step)
1943 /* If we're resuming a thread with the PC out of the step
1944 range, then we're doing some nested/finer run control
1945 operation, like stepping the thread out of the dynamic
1946 linker or the displaced stepping scratch pad. We
1947 shouldn't have allowed a range step then. */
1948 gdb_assert (pc_in_thread_step_range (pc, tp));
1951 /* Install inferior's terminal modes. */
1952 target_terminal_inferior ();
1954 /* Avoid confusing the next resume, if the next stop/resume
1955 happens to apply to another thread. */
1956 tp->suspend.stop_signal = GDB_SIGNAL_0;
1958 /* Advise target which signals may be handled silently. If we have
1959 removed breakpoints because we are stepping over one (which can
1960 happen only if we are not using displaced stepping), we need to
1961 receive all signals to avoid accidentally skipping a breakpoint
1962 during execution of a signal handler. */
1963 if ((step || singlestep_breakpoints_inserted_p)
1964 && tp->control.trap_expected
1965 && !use_displaced_stepping (gdbarch))
1966 target_pass_signals (0, NULL);
1968 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
1970 target_resume (resume_ptid, step, sig);
1973 discard_cleanups (old_cleanups);
1978 /* Clear out all variables saying what to do when inferior is continued.
1979 First do this, then set the ones you want, then call `proceed'. */
1982 clear_proceed_status_thread (struct thread_info *tp)
1985 fprintf_unfiltered (gdb_stdlog,
1986 "infrun: clear_proceed_status_thread (%s)\n",
1987 target_pid_to_str (tp->ptid));
1989 tp->control.trap_expected = 0;
1990 tp->control.step_range_start = 0;
1991 tp->control.step_range_end = 0;
1992 tp->control.may_range_step = 0;
1993 tp->control.step_frame_id = null_frame_id;
1994 tp->control.step_stack_frame_id = null_frame_id;
1995 tp->control.step_over_calls = STEP_OVER_UNDEBUGGABLE;
1996 tp->stop_requested = 0;
1998 tp->control.stop_step = 0;
2000 tp->control.proceed_to_finish = 0;
2002 /* Discard any remaining commands or status from previous stop. */
2003 bpstat_clear (&tp->control.stop_bpstat);
2007 clear_proceed_status_callback (struct thread_info *tp, void *data)
2009 if (is_exited (tp->ptid))
2012 clear_proceed_status_thread (tp);
2017 clear_proceed_status (void)
2021 /* In all-stop mode, delete the per-thread status of all
2022 threads, even if inferior_ptid is null_ptid, there may be
2023 threads on the list. E.g., we may be launching a new
2024 process, while selecting the executable. */
2025 iterate_over_threads (clear_proceed_status_callback, NULL);
2028 if (!ptid_equal (inferior_ptid, null_ptid))
2030 struct inferior *inferior;
2034 /* If in non-stop mode, only delete the per-thread status of
2035 the current thread. */
2036 clear_proceed_status_thread (inferior_thread ());
2039 inferior = current_inferior ();
2040 inferior->control.stop_soon = NO_STOP_QUIETLY;
2043 stop_after_trap = 0;
2045 observer_notify_about_to_proceed ();
2049 regcache_xfree (stop_registers);
2050 stop_registers = NULL;
2054 /* Check the current thread against the thread that reported the most recent
2055 event. If a step-over is required return TRUE and set the current thread
2056 to the old thread. Otherwise return FALSE.
2058 This should be suitable for any targets that support threads. */
2061 prepare_to_proceed (int step)
2064 struct target_waitstatus wait_status;
2065 int schedlock_enabled;
2067 /* With non-stop mode on, threads are always handled individually. */
2068 gdb_assert (! non_stop);
2070 /* Get the last target status returned by target_wait(). */
2071 get_last_target_status (&wait_ptid, &wait_status);
2073 /* Make sure we were stopped at a breakpoint. */
2074 if (wait_status.kind != TARGET_WAITKIND_STOPPED
2075 || (wait_status.value.sig != GDB_SIGNAL_TRAP
2076 && wait_status.value.sig != GDB_SIGNAL_ILL
2077 && wait_status.value.sig != GDB_SIGNAL_SEGV
2078 && wait_status.value.sig != GDB_SIGNAL_EMT))
2083 schedlock_enabled = (scheduler_mode == schedlock_on
2084 || (scheduler_mode == schedlock_step
2087 /* Don't switch over to WAIT_PTID if scheduler locking is on. */
2088 if (schedlock_enabled)
2091 /* Don't switch over if we're about to resume some other process
2092 other than WAIT_PTID's, and schedule-multiple is off. */
2094 && ptid_get_pid (wait_ptid) != ptid_get_pid (inferior_ptid))
2097 /* Switched over from WAIT_PID. */
2098 if (!ptid_equal (wait_ptid, minus_one_ptid)
2099 && !ptid_equal (inferior_ptid, wait_ptid))
2101 struct regcache *regcache = get_thread_regcache (wait_ptid);
2103 if (breakpoint_here_p (get_regcache_aspace (regcache),
2104 regcache_read_pc (regcache)))
2106 /* If stepping, remember current thread to switch back to. */
2108 deferred_step_ptid = inferior_ptid;
2110 /* Switch back to WAIT_PID thread. */
2111 switch_to_thread (wait_ptid);
2114 fprintf_unfiltered (gdb_stdlog,
2115 "infrun: prepare_to_proceed (step=%d), "
2116 "switched to [%s]\n",
2117 step, target_pid_to_str (inferior_ptid));
2119 /* We return 1 to indicate that there is a breakpoint here,
2120 so we need to step over it before continuing to avoid
2121 hitting it straight away. */
2129 /* Basic routine for continuing the program in various fashions.
2131 ADDR is the address to resume at, or -1 for resume where stopped.
2132 SIGGNAL is the signal to give it, or 0 for none,
2133 or -1 for act according to how it stopped.
2134 STEP is nonzero if should trap after one instruction.
2135 -1 means return after that and print nothing.
2136 You should probably set various step_... variables
2137 before calling here, if you are stepping.
2139 You should call clear_proceed_status before calling proceed. */
2142 proceed (CORE_ADDR addr, enum gdb_signal siggnal, int step)
2144 struct regcache *regcache;
2145 struct gdbarch *gdbarch;
2146 struct thread_info *tp;
2148 struct address_space *aspace;
2149 /* GDB may force the inferior to step due to various reasons. */
2152 /* If we're stopped at a fork/vfork, follow the branch set by the
2153 "set follow-fork-mode" command; otherwise, we'll just proceed
2154 resuming the current thread. */
2155 if (!follow_fork ())
2157 /* The target for some reason decided not to resume. */
2159 if (target_can_async_p ())
2160 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
2164 /* We'll update this if & when we switch to a new thread. */
2165 previous_inferior_ptid = inferior_ptid;
2167 regcache = get_current_regcache ();
2168 gdbarch = get_regcache_arch (regcache);
2169 aspace = get_regcache_aspace (regcache);
2170 pc = regcache_read_pc (regcache);
2173 step_start_function = find_pc_function (pc);
2175 stop_after_trap = 1;
2177 if (addr == (CORE_ADDR) -1)
2179 if (pc == stop_pc && breakpoint_here_p (aspace, pc)
2180 && execution_direction != EXEC_REVERSE)
2181 /* There is a breakpoint at the address we will resume at,
2182 step one instruction before inserting breakpoints so that
2183 we do not stop right away (and report a second hit at this
2186 Note, we don't do this in reverse, because we won't
2187 actually be executing the breakpoint insn anyway.
2188 We'll be (un-)executing the previous instruction. */
2191 else if (gdbarch_single_step_through_delay_p (gdbarch)
2192 && gdbarch_single_step_through_delay (gdbarch,
2193 get_current_frame ()))
2194 /* We stepped onto an instruction that needs to be stepped
2195 again before re-inserting the breakpoint, do so. */
2200 regcache_write_pc (regcache, addr);
2204 fprintf_unfiltered (gdb_stdlog,
2205 "infrun: proceed (addr=%s, signal=%s, step=%d)\n",
2206 paddress (gdbarch, addr),
2207 gdb_signal_to_symbol_string (siggnal), step);
2210 /* In non-stop, each thread is handled individually. The context
2211 must already be set to the right thread here. */
2215 /* In a multi-threaded task we may select another thread and
2216 then continue or step.
2218 But if the old thread was stopped at a breakpoint, it will
2219 immediately cause another breakpoint stop without any
2220 execution (i.e. it will report a breakpoint hit incorrectly).
2221 So we must step over it first.
2223 prepare_to_proceed checks the current thread against the
2224 thread that reported the most recent event. If a step-over
2225 is required it returns TRUE and sets the current thread to
2227 if (prepare_to_proceed (step))
2231 /* prepare_to_proceed may change the current thread. */
2232 tp = inferior_thread ();
2236 tp->control.trap_expected = 1;
2237 /* If displaced stepping is enabled, we can step over the
2238 breakpoint without hitting it, so leave all breakpoints
2239 inserted. Otherwise we need to disable all breakpoints, step
2240 one instruction, and then re-add them when that step is
2242 if (!use_displaced_stepping (gdbarch))
2243 remove_breakpoints ();
2246 /* We can insert breakpoints if we're not trying to step over one,
2247 or if we are stepping over one but we're using displaced stepping
2249 if (! tp->control.trap_expected || use_displaced_stepping (gdbarch))
2250 insert_breakpoints ();
2254 /* Pass the last stop signal to the thread we're resuming,
2255 irrespective of whether the current thread is the thread that
2256 got the last event or not. This was historically GDB's
2257 behaviour before keeping a stop_signal per thread. */
2259 struct thread_info *last_thread;
2261 struct target_waitstatus last_status;
2263 get_last_target_status (&last_ptid, &last_status);
2264 if (!ptid_equal (inferior_ptid, last_ptid)
2265 && !ptid_equal (last_ptid, null_ptid)
2266 && !ptid_equal (last_ptid, minus_one_ptid))
2268 last_thread = find_thread_ptid (last_ptid);
2271 tp->suspend.stop_signal = last_thread->suspend.stop_signal;
2272 last_thread->suspend.stop_signal = GDB_SIGNAL_0;
2277 if (siggnal != GDB_SIGNAL_DEFAULT)
2278 tp->suspend.stop_signal = siggnal;
2279 /* If this signal should not be seen by program,
2280 give it zero. Used for debugging signals. */
2281 else if (!signal_program[tp->suspend.stop_signal])
2282 tp->suspend.stop_signal = GDB_SIGNAL_0;
2284 annotate_starting ();
2286 /* Make sure that output from GDB appears before output from the
2288 gdb_flush (gdb_stdout);
2290 /* Refresh prev_pc value just prior to resuming. This used to be
2291 done in stop_stepping, however, setting prev_pc there did not handle
2292 scenarios such as inferior function calls or returning from
2293 a function via the return command. In those cases, the prev_pc
2294 value was not set properly for subsequent commands. The prev_pc value
2295 is used to initialize the starting line number in the ecs. With an
2296 invalid value, the gdb next command ends up stopping at the position
2297 represented by the next line table entry past our start position.
2298 On platforms that generate one line table entry per line, this
2299 is not a problem. However, on the ia64, the compiler generates
2300 extraneous line table entries that do not increase the line number.
2301 When we issue the gdb next command on the ia64 after an inferior call
2302 or a return command, we often end up a few instructions forward, still
2303 within the original line we started.
2305 An attempt was made to refresh the prev_pc at the same time the
2306 execution_control_state is initialized (for instance, just before
2307 waiting for an inferior event). But this approach did not work
2308 because of platforms that use ptrace, where the pc register cannot
2309 be read unless the inferior is stopped. At that point, we are not
2310 guaranteed the inferior is stopped and so the regcache_read_pc() call
2311 can fail. Setting the prev_pc value here ensures the value is updated
2312 correctly when the inferior is stopped. */
2313 tp->prev_pc = regcache_read_pc (get_current_regcache ());
2315 /* Fill in with reasonable starting values. */
2316 init_thread_stepping_state (tp);
2318 /* Reset to normal state. */
2319 init_infwait_state ();
2321 /* Resume inferior. */
2322 resume (force_step || step || bpstat_should_step (),
2323 tp->suspend.stop_signal);
2325 /* Wait for it to stop (if not standalone)
2326 and in any case decode why it stopped, and act accordingly. */
2327 /* Do this only if we are not using the event loop, or if the target
2328 does not support asynchronous execution. */
2329 if (!target_can_async_p ())
2331 wait_for_inferior ();
2337 /* Start remote-debugging of a machine over a serial link. */
2340 start_remote (int from_tty)
2342 struct inferior *inferior;
2344 inferior = current_inferior ();
2345 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
2347 /* Always go on waiting for the target, regardless of the mode. */
2348 /* FIXME: cagney/1999-09-23: At present it isn't possible to
2349 indicate to wait_for_inferior that a target should timeout if
2350 nothing is returned (instead of just blocking). Because of this,
2351 targets expecting an immediate response need to, internally, set
2352 things up so that the target_wait() is forced to eventually
2354 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
2355 differentiate to its caller what the state of the target is after
2356 the initial open has been performed. Here we're assuming that
2357 the target has stopped. It should be possible to eventually have
2358 target_open() return to the caller an indication that the target
2359 is currently running and GDB state should be set to the same as
2360 for an async run. */
2361 wait_for_inferior ();
2363 /* Now that the inferior has stopped, do any bookkeeping like
2364 loading shared libraries. We want to do this before normal_stop,
2365 so that the displayed frame is up to date. */
2366 post_create_inferior (¤t_target, from_tty);
2371 /* Initialize static vars when a new inferior begins. */
2374 init_wait_for_inferior (void)
2376 /* These are meaningless until the first time through wait_for_inferior. */
2378 breakpoint_init_inferior (inf_starting);
2380 clear_proceed_status ();
2382 stepping_past_singlestep_breakpoint = 0;
2383 deferred_step_ptid = null_ptid;
2385 target_last_wait_ptid = minus_one_ptid;
2387 previous_inferior_ptid = inferior_ptid;
2388 init_infwait_state ();
2390 /* Discard any skipped inlined frames. */
2391 clear_inline_frame_state (minus_one_ptid);
2395 /* This enum encodes possible reasons for doing a target_wait, so that
2396 wfi can call target_wait in one place. (Ultimately the call will be
2397 moved out of the infinite loop entirely.) */
2401 infwait_normal_state,
2402 infwait_thread_hop_state,
2403 infwait_step_watch_state,
2404 infwait_nonstep_watch_state
2407 /* The PTID we'll do a target_wait on.*/
2410 /* Current inferior wait state. */
2411 static enum infwait_states infwait_state;
2413 /* Data to be passed around while handling an event. This data is
2414 discarded between events. */
2415 struct execution_control_state
2418 /* The thread that got the event, if this was a thread event; NULL
2420 struct thread_info *event_thread;
2422 struct target_waitstatus ws;
2423 int stop_func_filled_in;
2424 CORE_ADDR stop_func_start;
2425 CORE_ADDR stop_func_end;
2426 const char *stop_func_name;
2429 /* We were in infwait_step_watch_state or
2430 infwait_nonstep_watch_state state, and the thread reported an
2432 int stepped_after_stopped_by_watchpoint;
2435 static void handle_inferior_event (struct execution_control_state *ecs);
2437 static void handle_step_into_function (struct gdbarch *gdbarch,
2438 struct execution_control_state *ecs);
2439 static void handle_step_into_function_backward (struct gdbarch *gdbarch,
2440 struct execution_control_state *ecs);
2441 static void handle_signal_stop (struct execution_control_state *ecs);
2442 static void check_exception_resume (struct execution_control_state *,
2443 struct frame_info *);
2445 static void stop_stepping (struct execution_control_state *ecs);
2446 static void prepare_to_wait (struct execution_control_state *ecs);
2447 static void keep_going (struct execution_control_state *ecs);
2448 static void process_event_stop_test (struct execution_control_state *ecs);
2449 static int switch_back_to_stepped_thread (struct execution_control_state *ecs);
2451 /* Callback for iterate over threads. If the thread is stopped, but
2452 the user/frontend doesn't know about that yet, go through
2453 normal_stop, as if the thread had just stopped now. ARG points at
2454 a ptid. If PTID is MINUS_ONE_PTID, applies to all threads. If
2455 ptid_is_pid(PTID) is true, applies to all threads of the process
2456 pointed at by PTID. Otherwise, apply only to the thread pointed by
2460 infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
2462 ptid_t ptid = * (ptid_t *) arg;
2464 if ((ptid_equal (info->ptid, ptid)
2465 || ptid_equal (minus_one_ptid, ptid)
2466 || (ptid_is_pid (ptid)
2467 && ptid_get_pid (ptid) == ptid_get_pid (info->ptid)))
2468 && is_running (info->ptid)
2469 && !is_executing (info->ptid))
2471 struct cleanup *old_chain;
2472 struct execution_control_state ecss;
2473 struct execution_control_state *ecs = &ecss;
2475 memset (ecs, 0, sizeof (*ecs));
2477 old_chain = make_cleanup_restore_current_thread ();
2479 overlay_cache_invalid = 1;
2480 /* Flush target cache before starting to handle each event.
2481 Target was running and cache could be stale. This is just a
2482 heuristic. Running threads may modify target memory, but we
2483 don't get any event. */
2484 target_dcache_invalidate ();
2486 /* Go through handle_inferior_event/normal_stop, so we always
2487 have consistent output as if the stop event had been
2489 ecs->ptid = info->ptid;
2490 ecs->event_thread = find_thread_ptid (info->ptid);
2491 ecs->ws.kind = TARGET_WAITKIND_STOPPED;
2492 ecs->ws.value.sig = GDB_SIGNAL_0;
2494 handle_inferior_event (ecs);
2496 if (!ecs->wait_some_more)
2498 struct thread_info *tp;
2502 /* Finish off the continuations. */
2503 tp = inferior_thread ();
2504 do_all_intermediate_continuations_thread (tp, 1);
2505 do_all_continuations_thread (tp, 1);
2508 do_cleanups (old_chain);
2514 /* This function is attached as a "thread_stop_requested" observer.
2515 Cleanup local state that assumed the PTID was to be resumed, and
2516 report the stop to the frontend. */
2519 infrun_thread_stop_requested (ptid_t ptid)
2521 struct displaced_step_inferior_state *displaced;
2523 /* PTID was requested to stop. Remove it from the displaced
2524 stepping queue, so we don't try to resume it automatically. */
2526 for (displaced = displaced_step_inferior_states;
2528 displaced = displaced->next)
2530 struct displaced_step_request *it, **prev_next_p;
2532 it = displaced->step_request_queue;
2533 prev_next_p = &displaced->step_request_queue;
2536 if (ptid_match (it->ptid, ptid))
2538 *prev_next_p = it->next;
2544 prev_next_p = &it->next;
2551 iterate_over_threads (infrun_thread_stop_requested_callback, &ptid);
2555 infrun_thread_thread_exit (struct thread_info *tp, int silent)
2557 if (ptid_equal (target_last_wait_ptid, tp->ptid))
2558 nullify_last_target_wait_ptid ();
2561 /* Callback for iterate_over_threads. */
2564 delete_step_resume_breakpoint_callback (struct thread_info *info, void *data)
2566 if (is_exited (info->ptid))
2569 delete_step_resume_breakpoint (info);
2570 delete_exception_resume_breakpoint (info);
2574 /* In all-stop, delete the step resume breakpoint of any thread that
2575 had one. In non-stop, delete the step resume breakpoint of the
2576 thread that just stopped. */
2579 delete_step_thread_step_resume_breakpoint (void)
2581 if (!target_has_execution
2582 || ptid_equal (inferior_ptid, null_ptid))
2583 /* If the inferior has exited, we have already deleted the step
2584 resume breakpoints out of GDB's lists. */
2589 /* If in non-stop mode, only delete the step-resume or
2590 longjmp-resume breakpoint of the thread that just stopped
2592 struct thread_info *tp = inferior_thread ();
2594 delete_step_resume_breakpoint (tp);
2595 delete_exception_resume_breakpoint (tp);
2598 /* In all-stop mode, delete all step-resume and longjmp-resume
2599 breakpoints of any thread that had them. */
2600 iterate_over_threads (delete_step_resume_breakpoint_callback, NULL);
2603 /* A cleanup wrapper. */
2606 delete_step_thread_step_resume_breakpoint_cleanup (void *arg)
2608 delete_step_thread_step_resume_breakpoint ();
2611 /* Pretty print the results of target_wait, for debugging purposes. */
2614 print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
2615 const struct target_waitstatus *ws)
2617 char *status_string = target_waitstatus_to_string (ws);
2618 struct ui_file *tmp_stream = mem_fileopen ();
2621 /* The text is split over several lines because it was getting too long.
2622 Call fprintf_unfiltered (gdb_stdlog) once so that the text is still
2623 output as a unit; we want only one timestamp printed if debug_timestamp
2626 fprintf_unfiltered (tmp_stream,
2627 "infrun: target_wait (%d", ptid_get_pid (waiton_ptid));
2628 if (ptid_get_pid (waiton_ptid) != -1)
2629 fprintf_unfiltered (tmp_stream,
2630 " [%s]", target_pid_to_str (waiton_ptid));
2631 fprintf_unfiltered (tmp_stream, ", status) =\n");
2632 fprintf_unfiltered (tmp_stream,
2633 "infrun: %d [%s],\n",
2634 ptid_get_pid (result_ptid),
2635 target_pid_to_str (result_ptid));
2636 fprintf_unfiltered (tmp_stream,
2640 text = ui_file_xstrdup (tmp_stream, NULL);
2642 /* This uses %s in part to handle %'s in the text, but also to avoid
2643 a gcc error: the format attribute requires a string literal. */
2644 fprintf_unfiltered (gdb_stdlog, "%s", text);
2646 xfree (status_string);
2648 ui_file_delete (tmp_stream);
2651 /* Prepare and stabilize the inferior for detaching it. E.g.,
2652 detaching while a thread is displaced stepping is a recipe for
2653 crashing it, as nothing would readjust the PC out of the scratch
2657 prepare_for_detach (void)
2659 struct inferior *inf = current_inferior ();
2660 ptid_t pid_ptid = pid_to_ptid (inf->pid);
2661 struct cleanup *old_chain_1;
2662 struct displaced_step_inferior_state *displaced;
2664 displaced = get_displaced_stepping_state (inf->pid);
2666 /* Is any thread of this process displaced stepping? If not,
2667 there's nothing else to do. */
2668 if (displaced == NULL || ptid_equal (displaced->step_ptid, null_ptid))
2672 fprintf_unfiltered (gdb_stdlog,
2673 "displaced-stepping in-process while detaching");
2675 old_chain_1 = make_cleanup_restore_integer (&inf->detaching);
2678 while (!ptid_equal (displaced->step_ptid, null_ptid))
2680 struct cleanup *old_chain_2;
2681 struct execution_control_state ecss;
2682 struct execution_control_state *ecs;
2685 memset (ecs, 0, sizeof (*ecs));
2687 overlay_cache_invalid = 1;
2688 /* Flush target cache before starting to handle each event.
2689 Target was running and cache could be stale. This is just a
2690 heuristic. Running threads may modify target memory, but we
2691 don't get any event. */
2692 target_dcache_invalidate ();
2694 if (deprecated_target_wait_hook)
2695 ecs->ptid = deprecated_target_wait_hook (pid_ptid, &ecs->ws, 0);
2697 ecs->ptid = target_wait (pid_ptid, &ecs->ws, 0);
2700 print_target_wait_results (pid_ptid, ecs->ptid, &ecs->ws);
2702 /* If an error happens while handling the event, propagate GDB's
2703 knowledge of the executing state to the frontend/user running
2705 old_chain_2 = make_cleanup (finish_thread_state_cleanup,
2708 /* Now figure out what to do with the result of the result. */
2709 handle_inferior_event (ecs);
2711 /* No error, don't finish the state yet. */
2712 discard_cleanups (old_chain_2);
2714 /* Breakpoints and watchpoints are not installed on the target
2715 at this point, and signals are passed directly to the
2716 inferior, so this must mean the process is gone. */
2717 if (!ecs->wait_some_more)
2719 discard_cleanups (old_chain_1);
2720 error (_("Program exited while detaching"));
2724 discard_cleanups (old_chain_1);
2727 /* Wait for control to return from inferior to debugger.
2729 If inferior gets a signal, we may decide to start it up again
2730 instead of returning. That is why there is a loop in this function.
2731 When this function actually returns it means the inferior
2732 should be left stopped and GDB should read more commands. */
2735 wait_for_inferior (void)
2737 struct cleanup *old_cleanups;
2741 (gdb_stdlog, "infrun: wait_for_inferior ()\n");
2744 make_cleanup (delete_step_thread_step_resume_breakpoint_cleanup, NULL);
2748 struct execution_control_state ecss;
2749 struct execution_control_state *ecs = &ecss;
2750 struct cleanup *old_chain;
2752 memset (ecs, 0, sizeof (*ecs));
2754 overlay_cache_invalid = 1;
2756 /* Flush target cache before starting to handle each event.
2757 Target was running and cache could be stale. This is just a
2758 heuristic. Running threads may modify target memory, but we
2759 don't get any event. */
2760 target_dcache_invalidate ();
2762 if (deprecated_target_wait_hook)
2763 ecs->ptid = deprecated_target_wait_hook (waiton_ptid, &ecs->ws, 0);
2765 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, 0);
2768 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
2770 /* If an error happens while handling the event, propagate GDB's
2771 knowledge of the executing state to the frontend/user running
2773 old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2775 /* Now figure out what to do with the result of the result. */
2776 handle_inferior_event (ecs);
2778 /* No error, don't finish the state yet. */
2779 discard_cleanups (old_chain);
2781 if (!ecs->wait_some_more)
2785 do_cleanups (old_cleanups);
2788 /* Asynchronous version of wait_for_inferior. It is called by the
2789 event loop whenever a change of state is detected on the file
2790 descriptor corresponding to the target. It can be called more than
2791 once to complete a single execution command. In such cases we need
2792 to keep the state in a global variable ECSS. If it is the last time
2793 that this function is called for a single execution command, then
2794 report to the user that the inferior has stopped, and do the
2795 necessary cleanups. */
2798 fetch_inferior_event (void *client_data)
2800 struct execution_control_state ecss;
2801 struct execution_control_state *ecs = &ecss;
2802 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
2803 struct cleanup *ts_old_chain;
2804 int was_sync = sync_execution;
2807 memset (ecs, 0, sizeof (*ecs));
2809 /* We're handling a live event, so make sure we're doing live
2810 debugging. If we're looking at traceframes while the target is
2811 running, we're going to need to get back to that mode after
2812 handling the event. */
2815 make_cleanup_restore_current_traceframe ();
2816 set_current_traceframe (-1);
2820 /* In non-stop mode, the user/frontend should not notice a thread
2821 switch due to internal events. Make sure we reverse to the
2822 user selected thread and frame after handling the event and
2823 running any breakpoint commands. */
2824 make_cleanup_restore_current_thread ();
2826 overlay_cache_invalid = 1;
2827 /* Flush target cache before starting to handle each event. Target
2828 was running and cache could be stale. This is just a heuristic.
2829 Running threads may modify target memory, but we don't get any
2831 target_dcache_invalidate ();
2833 make_cleanup_restore_integer (&execution_direction);
2834 execution_direction = target_execution_direction ();
2836 if (deprecated_target_wait_hook)
2838 deprecated_target_wait_hook (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
2840 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
2843 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
2845 /* If an error happens while handling the event, propagate GDB's
2846 knowledge of the executing state to the frontend/user running
2849 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2851 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &ecs->ptid);
2853 /* Get executed before make_cleanup_restore_current_thread above to apply
2854 still for the thread which has thrown the exception. */
2855 make_bpstat_clear_actions_cleanup ();
2857 /* Now figure out what to do with the result of the result. */
2858 handle_inferior_event (ecs);
2860 if (!ecs->wait_some_more)
2862 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
2864 delete_step_thread_step_resume_breakpoint ();
2866 /* We may not find an inferior if this was a process exit. */
2867 if (inf == NULL || inf->control.stop_soon == NO_STOP_QUIETLY)
2870 if (target_has_execution
2871 && ecs->ws.kind != TARGET_WAITKIND_NO_RESUMED
2872 && ecs->ws.kind != TARGET_WAITKIND_EXITED
2873 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
2874 && ecs->event_thread->step_multi
2875 && ecs->event_thread->control.stop_step)
2876 inferior_event_handler (INF_EXEC_CONTINUE, NULL);
2879 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
2884 /* No error, don't finish the thread states yet. */
2885 discard_cleanups (ts_old_chain);
2887 /* Revert thread and frame. */
2888 do_cleanups (old_chain);
2890 /* If the inferior was in sync execution mode, and now isn't,
2891 restore the prompt (a synchronous execution command has finished,
2892 and we're ready for input). */
2893 if (interpreter_async && was_sync && !sync_execution)
2894 display_gdb_prompt (0);
2898 && exec_done_display_p
2899 && (ptid_equal (inferior_ptid, null_ptid)
2900 || !is_running (inferior_ptid)))
2901 printf_unfiltered (_("completed.\n"));
2904 /* Record the frame and location we're currently stepping through. */
2906 set_step_info (struct frame_info *frame, struct symtab_and_line sal)
2908 struct thread_info *tp = inferior_thread ();
2910 tp->control.step_frame_id = get_frame_id (frame);
2911 tp->control.step_stack_frame_id = get_stack_frame_id (frame);
2913 tp->current_symtab = sal.symtab;
2914 tp->current_line = sal.line;
2917 /* Clear context switchable stepping state. */
2920 init_thread_stepping_state (struct thread_info *tss)
2922 tss->stepping_over_breakpoint = 0;
2923 tss->step_after_step_resume_breakpoint = 0;
2926 /* Return the cached copy of the last pid/waitstatus returned by
2927 target_wait()/deprecated_target_wait_hook(). The data is actually
2928 cached by handle_inferior_event(), which gets called immediately
2929 after target_wait()/deprecated_target_wait_hook(). */
2932 get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
2934 *ptidp = target_last_wait_ptid;
2935 *status = target_last_waitstatus;
2939 nullify_last_target_wait_ptid (void)
2941 target_last_wait_ptid = minus_one_ptid;
2944 /* Switch thread contexts. */
2947 context_switch (ptid_t ptid)
2949 if (debug_infrun && !ptid_equal (ptid, inferior_ptid))
2951 fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
2952 target_pid_to_str (inferior_ptid));
2953 fprintf_unfiltered (gdb_stdlog, "to %s\n",
2954 target_pid_to_str (ptid));
2957 switch_to_thread (ptid);
2961 adjust_pc_after_break (struct execution_control_state *ecs)
2963 struct regcache *regcache;
2964 struct gdbarch *gdbarch;
2965 struct address_space *aspace;
2966 CORE_ADDR breakpoint_pc, decr_pc;
2968 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
2969 we aren't, just return.
2971 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
2972 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
2973 implemented by software breakpoints should be handled through the normal
2976 NOTE drow/2004-01-31: On some targets, breakpoints may generate
2977 different signals (SIGILL or SIGEMT for instance), but it is less
2978 clear where the PC is pointing afterwards. It may not match
2979 gdbarch_decr_pc_after_break. I don't know any specific target that
2980 generates these signals at breakpoints (the code has been in GDB since at
2981 least 1992) so I can not guess how to handle them here.
2983 In earlier versions of GDB, a target with
2984 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
2985 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
2986 target with both of these set in GDB history, and it seems unlikely to be
2987 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
2989 if (ecs->ws.kind != TARGET_WAITKIND_STOPPED)
2992 if (ecs->ws.value.sig != GDB_SIGNAL_TRAP)
2995 /* In reverse execution, when a breakpoint is hit, the instruction
2996 under it has already been de-executed. The reported PC always
2997 points at the breakpoint address, so adjusting it further would
2998 be wrong. E.g., consider this case on a decr_pc_after_break == 1
3001 B1 0x08000000 : INSN1
3002 B2 0x08000001 : INSN2
3004 PC -> 0x08000003 : INSN4
3006 Say you're stopped at 0x08000003 as above. Reverse continuing
3007 from that point should hit B2 as below. Reading the PC when the
3008 SIGTRAP is reported should read 0x08000001 and INSN2 should have
3009 been de-executed already.
3011 B1 0x08000000 : INSN1
3012 B2 PC -> 0x08000001 : INSN2
3016 We can't apply the same logic as for forward execution, because
3017 we would wrongly adjust the PC to 0x08000000, since there's a
3018 breakpoint at PC - 1. We'd then report a hit on B1, although
3019 INSN1 hadn't been de-executed yet. Doing nothing is the correct
3021 if (execution_direction == EXEC_REVERSE)
3024 /* If this target does not decrement the PC after breakpoints, then
3025 we have nothing to do. */
3026 regcache = get_thread_regcache (ecs->ptid);
3027 gdbarch = get_regcache_arch (regcache);
3029 decr_pc = target_decr_pc_after_break (gdbarch);
3033 aspace = get_regcache_aspace (regcache);
3035 /* Find the location where (if we've hit a breakpoint) the
3036 breakpoint would be. */
3037 breakpoint_pc = regcache_read_pc (regcache) - decr_pc;
3039 /* Check whether there actually is a software breakpoint inserted at
3042 If in non-stop mode, a race condition is possible where we've
3043 removed a breakpoint, but stop events for that breakpoint were
3044 already queued and arrive later. To suppress those spurious
3045 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
3046 and retire them after a number of stop events are reported. */
3047 if (software_breakpoint_inserted_here_p (aspace, breakpoint_pc)
3048 || (non_stop && moribund_breakpoint_here_p (aspace, breakpoint_pc)))
3050 struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
3052 if (record_full_is_used ())
3053 record_full_gdb_operation_disable_set ();
3055 /* When using hardware single-step, a SIGTRAP is reported for both
3056 a completed single-step and a software breakpoint. Need to
3057 differentiate between the two, as the latter needs adjusting
3058 but the former does not.
3060 The SIGTRAP can be due to a completed hardware single-step only if
3061 - we didn't insert software single-step breakpoints
3062 - the thread to be examined is still the current thread
3063 - this thread is currently being stepped
3065 If any of these events did not occur, we must have stopped due
3066 to hitting a software breakpoint, and have to back up to the
3069 As a special case, we could have hardware single-stepped a
3070 software breakpoint. In this case (prev_pc == breakpoint_pc),
3071 we also need to back up to the breakpoint address. */
3073 if (singlestep_breakpoints_inserted_p
3074 || !ptid_equal (ecs->ptid, inferior_ptid)
3075 || !currently_stepping (ecs->event_thread)
3076 || ecs->event_thread->prev_pc == breakpoint_pc)
3077 regcache_write_pc (regcache, breakpoint_pc);
3079 do_cleanups (old_cleanups);
3084 init_infwait_state (void)
3086 waiton_ptid = pid_to_ptid (-1);
3087 infwait_state = infwait_normal_state;
3091 stepped_in_from (struct frame_info *frame, struct frame_id step_frame_id)
3093 for (frame = get_prev_frame (frame);
3095 frame = get_prev_frame (frame))
3097 if (frame_id_eq (get_frame_id (frame), step_frame_id))
3099 if (get_frame_type (frame) != INLINE_FRAME)
3106 /* Auxiliary function that handles syscall entry/return events.
3107 It returns 1 if the inferior should keep going (and GDB
3108 should ignore the event), or 0 if the event deserves to be
3112 handle_syscall_event (struct execution_control_state *ecs)
3114 struct regcache *regcache;
3117 if (!ptid_equal (ecs->ptid, inferior_ptid))
3118 context_switch (ecs->ptid);
3120 regcache = get_thread_regcache (ecs->ptid);
3121 syscall_number = ecs->ws.value.syscall_number;
3122 stop_pc = regcache_read_pc (regcache);
3124 if (catch_syscall_enabled () > 0
3125 && catching_syscall_number (syscall_number) > 0)
3128 fprintf_unfiltered (gdb_stdlog, "infrun: syscall number = '%d'\n",
3131 ecs->event_thread->control.stop_bpstat
3132 = bpstat_stop_status (get_regcache_aspace (regcache),
3133 stop_pc, ecs->ptid, &ecs->ws);
3135 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
3137 /* Catchpoint hit. */
3142 /* If no catchpoint triggered for this, then keep going. */
3147 /* Lazily fill in the execution_control_state's stop_func_* fields. */
3150 fill_in_stop_func (struct gdbarch *gdbarch,
3151 struct execution_control_state *ecs)
3153 if (!ecs->stop_func_filled_in)
3155 /* Don't care about return value; stop_func_start and stop_func_name
3156 will both be 0 if it doesn't work. */
3157 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
3158 &ecs->stop_func_start, &ecs->stop_func_end);
3159 ecs->stop_func_start
3160 += gdbarch_deprecated_function_start_offset (gdbarch);
3162 if (gdbarch_skip_entrypoint_p (gdbarch))
3163 ecs->stop_func_start = gdbarch_skip_entrypoint (gdbarch,
3164 ecs->stop_func_start);
3166 ecs->stop_func_filled_in = 1;
3171 /* Return the STOP_SOON field of the inferior pointed at by PTID. */
3173 static enum stop_kind
3174 get_inferior_stop_soon (ptid_t ptid)
3176 struct inferior *inf = find_inferior_pid (ptid_get_pid (ptid));
3178 gdb_assert (inf != NULL);
3179 return inf->control.stop_soon;
3182 /* Given an execution control state that has been freshly filled in by
3183 an event from the inferior, figure out what it means and take
3186 The alternatives are:
3188 1) stop_stepping and return; to really stop and return to the
3191 2) keep_going and return; to wait for the next event (set
3192 ecs->event_thread->stepping_over_breakpoint to 1 to single step
3196 handle_inferior_event (struct execution_control_state *ecs)
3198 enum stop_kind stop_soon;
3200 if (ecs->ws.kind == TARGET_WAITKIND_IGNORE)
3202 /* We had an event in the inferior, but we are not interested in
3203 handling it at this level. The lower layers have already
3204 done what needs to be done, if anything.
3206 One of the possible circumstances for this is when the
3207 inferior produces output for the console. The inferior has
3208 not stopped, and we are ignoring the event. Another possible
3209 circumstance is any event which the lower level knows will be
3210 reported multiple times without an intervening resume. */
3212 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
3213 prepare_to_wait (ecs);
3217 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED
3218 && target_can_async_p () && !sync_execution)
3220 /* There were no unwaited-for children left in the target, but,
3221 we're not synchronously waiting for events either. Just
3222 ignore. Otherwise, if we were running a synchronous
3223 execution command, we need to cancel it and give the user
3224 back the terminal. */
3226 fprintf_unfiltered (gdb_stdlog,
3227 "infrun: TARGET_WAITKIND_NO_RESUMED (ignoring)\n");
3228 prepare_to_wait (ecs);
3232 /* Cache the last pid/waitstatus. */
3233 target_last_wait_ptid = ecs->ptid;
3234 target_last_waitstatus = ecs->ws;
3236 /* Always clear state belonging to the previous time we stopped. */
3237 stop_stack_dummy = STOP_NONE;
3239 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED)
3241 /* No unwaited-for children left. IOW, all resumed children
3244 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_RESUMED\n");
3246 stop_print_frame = 0;
3247 stop_stepping (ecs);
3251 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
3252 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
3254 ecs->event_thread = find_thread_ptid (ecs->ptid);
3255 /* If it's a new thread, add it to the thread database. */
3256 if (ecs->event_thread == NULL)
3257 ecs->event_thread = add_thread (ecs->ptid);
3259 /* Disable range stepping. If the next step request could use a
3260 range, this will be end up re-enabled then. */
3261 ecs->event_thread->control.may_range_step = 0;
3264 /* Dependent on valid ECS->EVENT_THREAD. */
3265 adjust_pc_after_break (ecs);
3267 /* Dependent on the current PC value modified by adjust_pc_after_break. */
3268 reinit_frame_cache ();
3270 breakpoint_retire_moribund ();
3272 /* First, distinguish signals caused by the debugger from signals
3273 that have to do with the program's own actions. Note that
3274 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
3275 on the operating system version. Here we detect when a SIGILL or
3276 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
3277 something similar for SIGSEGV, since a SIGSEGV will be generated
3278 when we're trying to execute a breakpoint instruction on a
3279 non-executable stack. This happens for call dummy breakpoints
3280 for architectures like SPARC that place call dummies on the
3282 if (ecs->ws.kind == TARGET_WAITKIND_STOPPED
3283 && (ecs->ws.value.sig == GDB_SIGNAL_ILL
3284 || ecs->ws.value.sig == GDB_SIGNAL_SEGV
3285 || ecs->ws.value.sig == GDB_SIGNAL_EMT))
3287 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3289 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
3290 regcache_read_pc (regcache)))
3293 fprintf_unfiltered (gdb_stdlog,
3294 "infrun: Treating signal as SIGTRAP\n");
3295 ecs->ws.value.sig = GDB_SIGNAL_TRAP;
3299 /* Mark the non-executing threads accordingly. In all-stop, all
3300 threads of all processes are stopped when we get any event
3301 reported. In non-stop mode, only the event thread stops. If
3302 we're handling a process exit in non-stop mode, there's nothing
3303 to do, as threads of the dead process are gone, and threads of
3304 any other process were left running. */
3306 set_executing (minus_one_ptid, 0);
3307 else if (ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
3308 && ecs->ws.kind != TARGET_WAITKIND_EXITED)
3309 set_executing (ecs->ptid, 0);
3311 switch (infwait_state)
3313 case infwait_thread_hop_state:
3315 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_thread_hop_state\n");
3318 case infwait_normal_state:
3320 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_normal_state\n");
3323 case infwait_step_watch_state:
3325 fprintf_unfiltered (gdb_stdlog,
3326 "infrun: infwait_step_watch_state\n");
3328 ecs->stepped_after_stopped_by_watchpoint = 1;
3331 case infwait_nonstep_watch_state:
3333 fprintf_unfiltered (gdb_stdlog,
3334 "infrun: infwait_nonstep_watch_state\n");
3335 insert_breakpoints ();
3337 /* FIXME-maybe: is this cleaner than setting a flag? Does it
3338 handle things like signals arriving and other things happening
3339 in combination correctly? */
3340 ecs->stepped_after_stopped_by_watchpoint = 1;
3344 internal_error (__FILE__, __LINE__, _("bad switch"));
3347 infwait_state = infwait_normal_state;
3348 waiton_ptid = pid_to_ptid (-1);
3350 switch (ecs->ws.kind)
3352 case TARGET_WAITKIND_LOADED:
3354 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
3355 if (!ptid_equal (ecs->ptid, inferior_ptid))
3356 context_switch (ecs->ptid);
3357 /* Ignore gracefully during startup of the inferior, as it might
3358 be the shell which has just loaded some objects, otherwise
3359 add the symbols for the newly loaded objects. Also ignore at
3360 the beginning of an attach or remote session; we will query
3361 the full list of libraries once the connection is
3364 stop_soon = get_inferior_stop_soon (ecs->ptid);
3365 if (stop_soon == NO_STOP_QUIETLY)
3367 struct regcache *regcache;
3369 regcache = get_thread_regcache (ecs->ptid);
3371 handle_solib_event ();
3373 ecs->event_thread->control.stop_bpstat
3374 = bpstat_stop_status (get_regcache_aspace (regcache),
3375 stop_pc, ecs->ptid, &ecs->ws);
3377 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
3379 /* A catchpoint triggered. */
3380 process_event_stop_test (ecs);
3384 /* If requested, stop when the dynamic linker notifies
3385 gdb of events. This allows the user to get control
3386 and place breakpoints in initializer routines for
3387 dynamically loaded objects (among other things). */
3388 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3389 if (stop_on_solib_events)
3391 /* Make sure we print "Stopped due to solib-event" in
3393 stop_print_frame = 1;
3395 stop_stepping (ecs);
3400 /* If we are skipping through a shell, or through shared library
3401 loading that we aren't interested in, resume the program. If
3402 we're running the program normally, also resume. */
3403 if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
3405 /* Loading of shared libraries might have changed breakpoint
3406 addresses. Make sure new breakpoints are inserted. */
3407 if (stop_soon == NO_STOP_QUIETLY
3408 && !breakpoints_always_inserted_mode ())
3409 insert_breakpoints ();
3410 resume (0, GDB_SIGNAL_0);
3411 prepare_to_wait (ecs);
3415 /* But stop if we're attaching or setting up a remote
3417 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
3418 || stop_soon == STOP_QUIETLY_REMOTE)
3421 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
3422 stop_stepping (ecs);
3426 internal_error (__FILE__, __LINE__,
3427 _("unhandled stop_soon: %d"), (int) stop_soon);
3429 case TARGET_WAITKIND_SPURIOUS:
3431 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
3432 if (!ptid_equal (ecs->ptid, inferior_ptid))
3433 context_switch (ecs->ptid);
3434 resume (0, GDB_SIGNAL_0);
3435 prepare_to_wait (ecs);
3438 case TARGET_WAITKIND_EXITED:
3439 case TARGET_WAITKIND_SIGNALLED:
3442 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3443 fprintf_unfiltered (gdb_stdlog,
3444 "infrun: TARGET_WAITKIND_EXITED\n");
3446 fprintf_unfiltered (gdb_stdlog,
3447 "infrun: TARGET_WAITKIND_SIGNALLED\n");
3450 inferior_ptid = ecs->ptid;
3451 set_current_inferior (find_inferior_pid (ptid_get_pid (ecs->ptid)));
3452 set_current_program_space (current_inferior ()->pspace);
3453 handle_vfork_child_exec_or_exit (0);
3454 target_terminal_ours (); /* Must do this before mourn anyway. */
3456 /* Clearing any previous state of convenience variables. */
3457 clear_exit_convenience_vars ();
3459 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3461 /* Record the exit code in the convenience variable $_exitcode, so
3462 that the user can inspect this again later. */
3463 set_internalvar_integer (lookup_internalvar ("_exitcode"),
3464 (LONGEST) ecs->ws.value.integer);
3466 /* Also record this in the inferior itself. */
3467 current_inferior ()->has_exit_code = 1;
3468 current_inferior ()->exit_code = (LONGEST) ecs->ws.value.integer;
3470 print_exited_reason (ecs->ws.value.integer);
3474 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3475 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3477 if (gdbarch_gdb_signal_to_target_p (gdbarch))
3479 /* Set the value of the internal variable $_exitsignal,
3480 which holds the signal uncaught by the inferior. */
3481 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
3482 gdbarch_gdb_signal_to_target (gdbarch,
3483 ecs->ws.value.sig));
3487 /* We don't have access to the target's method used for
3488 converting between signal numbers (GDB's internal
3489 representation <-> target's representation).
3490 Therefore, we cannot do a good job at displaying this
3491 information to the user. It's better to just warn
3492 her about it (if infrun debugging is enabled), and
3495 fprintf_filtered (gdb_stdlog, _("\
3496 Cannot fill $_exitsignal with the correct signal number.\n"));
3499 print_signal_exited_reason (ecs->ws.value.sig);
3502 gdb_flush (gdb_stdout);
3503 target_mourn_inferior ();
3504 singlestep_breakpoints_inserted_p = 0;
3505 cancel_single_step_breakpoints ();
3506 stop_print_frame = 0;
3507 stop_stepping (ecs);
3510 /* The following are the only cases in which we keep going;
3511 the above cases end in a continue or goto. */
3512 case TARGET_WAITKIND_FORKED:
3513 case TARGET_WAITKIND_VFORKED:
3516 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
3517 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
3519 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_VFORKED\n");
3522 /* Check whether the inferior is displaced stepping. */
3524 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3525 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3526 struct displaced_step_inferior_state *displaced
3527 = get_displaced_stepping_state (ptid_get_pid (ecs->ptid));
3529 /* If checking displaced stepping is supported, and thread
3530 ecs->ptid is displaced stepping. */
3531 if (displaced && ptid_equal (displaced->step_ptid, ecs->ptid))
3533 struct inferior *parent_inf
3534 = find_inferior_pid (ptid_get_pid (ecs->ptid));
3535 struct regcache *child_regcache;
3536 CORE_ADDR parent_pc;
3538 /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
3539 indicating that the displaced stepping of syscall instruction
3540 has been done. Perform cleanup for parent process here. Note
3541 that this operation also cleans up the child process for vfork,
3542 because their pages are shared. */
3543 displaced_step_fixup (ecs->ptid, GDB_SIGNAL_TRAP);
3545 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
3547 /* Restore scratch pad for child process. */
3548 displaced_step_restore (displaced, ecs->ws.value.related_pid);
3551 /* Since the vfork/fork syscall instruction was executed in the scratchpad,
3552 the child's PC is also within the scratchpad. Set the child's PC
3553 to the parent's PC value, which has already been fixed up.
3554 FIXME: we use the parent's aspace here, although we're touching
3555 the child, because the child hasn't been added to the inferior
3556 list yet at this point. */
3559 = get_thread_arch_aspace_regcache (ecs->ws.value.related_pid,
3561 parent_inf->aspace);
3562 /* Read PC value of parent process. */
3563 parent_pc = regcache_read_pc (regcache);
3565 if (debug_displaced)
3566 fprintf_unfiltered (gdb_stdlog,
3567 "displaced: write child pc from %s to %s\n",
3569 regcache_read_pc (child_regcache)),
3570 paddress (gdbarch, parent_pc));
3572 regcache_write_pc (child_regcache, parent_pc);
3576 if (!ptid_equal (ecs->ptid, inferior_ptid))
3577 context_switch (ecs->ptid);
3579 /* Immediately detach breakpoints from the child before there's
3580 any chance of letting the user delete breakpoints from the
3581 breakpoint lists. If we don't do this early, it's easy to
3582 leave left over traps in the child, vis: "break foo; catch
3583 fork; c; <fork>; del; c; <child calls foo>". We only follow
3584 the fork on the last `continue', and by that time the
3585 breakpoint at "foo" is long gone from the breakpoint table.
3586 If we vforked, then we don't need to unpatch here, since both
3587 parent and child are sharing the same memory pages; we'll
3588 need to unpatch at follow/detach time instead to be certain
3589 that new breakpoints added between catchpoint hit time and
3590 vfork follow are detached. */
3591 if (ecs->ws.kind != TARGET_WAITKIND_VFORKED)
3593 /* This won't actually modify the breakpoint list, but will
3594 physically remove the breakpoints from the child. */
3595 detach_breakpoints (ecs->ws.value.related_pid);
3598 if (singlestep_breakpoints_inserted_p)
3600 /* Pull the single step breakpoints out of the target. */
3601 remove_single_step_breakpoints ();
3602 singlestep_breakpoints_inserted_p = 0;
3605 /* In case the event is caught by a catchpoint, remember that
3606 the event is to be followed at the next resume of the thread,
3607 and not immediately. */
3608 ecs->event_thread->pending_follow = ecs->ws;
3610 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3612 ecs->event_thread->control.stop_bpstat
3613 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
3614 stop_pc, ecs->ptid, &ecs->ws);
3616 /* If no catchpoint triggered for this, then keep going. Note
3617 that we're interested in knowing the bpstat actually causes a
3618 stop, not just if it may explain the signal. Software
3619 watchpoints, for example, always appear in the bpstat. */
3620 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
3626 = (follow_fork_mode_string == follow_fork_mode_child);
3628 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3630 should_resume = follow_fork ();
3633 child = ecs->ws.value.related_pid;
3635 /* In non-stop mode, also resume the other branch. */
3636 if (non_stop && !detach_fork)
3639 switch_to_thread (parent);
3641 switch_to_thread (child);
3643 ecs->event_thread = inferior_thread ();
3644 ecs->ptid = inferior_ptid;
3649 switch_to_thread (child);
3651 switch_to_thread (parent);
3653 ecs->event_thread = inferior_thread ();
3654 ecs->ptid = inferior_ptid;
3659 stop_stepping (ecs);
3662 process_event_stop_test (ecs);
3665 case TARGET_WAITKIND_VFORK_DONE:
3666 /* Done with the shared memory region. Re-insert breakpoints in
3667 the parent, and keep going. */
3670 fprintf_unfiltered (gdb_stdlog,
3671 "infrun: TARGET_WAITKIND_VFORK_DONE\n");
3673 if (!ptid_equal (ecs->ptid, inferior_ptid))
3674 context_switch (ecs->ptid);
3676 current_inferior ()->waiting_for_vfork_done = 0;
3677 current_inferior ()->pspace->breakpoints_not_allowed = 0;
3678 /* This also takes care of reinserting breakpoints in the
3679 previously locked inferior. */
3683 case TARGET_WAITKIND_EXECD:
3685 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
3687 if (!ptid_equal (ecs->ptid, inferior_ptid))
3688 context_switch (ecs->ptid);
3690 singlestep_breakpoints_inserted_p = 0;
3691 cancel_single_step_breakpoints ();
3693 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3695 /* Do whatever is necessary to the parent branch of the vfork. */
3696 handle_vfork_child_exec_or_exit (1);
3698 /* This causes the eventpoints and symbol table to be reset.
3699 Must do this now, before trying to determine whether to
3701 follow_exec (inferior_ptid, ecs->ws.value.execd_pathname);
3703 ecs->event_thread->control.stop_bpstat
3704 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
3705 stop_pc, ecs->ptid, &ecs->ws);
3707 /* Note that this may be referenced from inside
3708 bpstat_stop_status above, through inferior_has_execd. */
3709 xfree (ecs->ws.value.execd_pathname);
3710 ecs->ws.value.execd_pathname = NULL;
3712 /* If no catchpoint triggered for this, then keep going. */
3713 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
3715 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3719 process_event_stop_test (ecs);
3722 /* Be careful not to try to gather much state about a thread
3723 that's in a syscall. It's frequently a losing proposition. */
3724 case TARGET_WAITKIND_SYSCALL_ENTRY:
3726 fprintf_unfiltered (gdb_stdlog,
3727 "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
3728 /* Getting the current syscall number. */
3729 if (handle_syscall_event (ecs) == 0)
3730 process_event_stop_test (ecs);
3733 /* Before examining the threads further, step this thread to
3734 get it entirely out of the syscall. (We get notice of the
3735 event when the thread is just on the verge of exiting a
3736 syscall. Stepping one instruction seems to get it back
3738 case TARGET_WAITKIND_SYSCALL_RETURN:
3740 fprintf_unfiltered (gdb_stdlog,
3741 "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
3742 if (handle_syscall_event (ecs) == 0)
3743 process_event_stop_test (ecs);
3746 case TARGET_WAITKIND_STOPPED:
3748 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
3749 ecs->event_thread->suspend.stop_signal = ecs->ws.value.sig;
3750 handle_signal_stop (ecs);
3753 case TARGET_WAITKIND_NO_HISTORY:
3755 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_HISTORY\n");
3756 /* Reverse execution: target ran out of history info. */
3758 /* Pull the single step breakpoints out of the target. */
3759 if (singlestep_breakpoints_inserted_p)
3761 if (!ptid_equal (ecs->ptid, inferior_ptid))
3762 context_switch (ecs->ptid);
3763 remove_single_step_breakpoints ();
3764 singlestep_breakpoints_inserted_p = 0;
3766 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3767 print_no_history_reason ();
3768 stop_stepping (ecs);
3773 /* Come here when the program has stopped with a signal. */
3776 handle_signal_stop (struct execution_control_state *ecs)
3778 struct frame_info *frame;
3779 struct gdbarch *gdbarch;
3780 int stopped_by_watchpoint;
3781 enum stop_kind stop_soon;
3784 if (ecs->ws.kind == TARGET_WAITKIND_STOPPED)
3786 /* Do we need to clean up the state of a thread that has
3787 completed a displaced single-step? (Doing so usually affects
3788 the PC, so do it here, before we set stop_pc.) */
3789 displaced_step_fixup (ecs->ptid,
3790 ecs->event_thread->suspend.stop_signal);
3792 /* If we either finished a single-step or hit a breakpoint, but
3793 the user wanted this thread to be stopped, pretend we got a
3794 SIG0 (generic unsignaled stop). */
3796 if (ecs->event_thread->stop_requested
3797 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
3798 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3801 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3805 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3806 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3807 struct cleanup *old_chain = save_inferior_ptid ();
3809 inferior_ptid = ecs->ptid;
3811 fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = %s\n",
3812 paddress (gdbarch, stop_pc));
3813 if (target_stopped_by_watchpoint ())
3817 fprintf_unfiltered (gdb_stdlog, "infrun: stopped by watchpoint\n");
3819 if (target_stopped_data_address (¤t_target, &addr))
3820 fprintf_unfiltered (gdb_stdlog,
3821 "infrun: stopped data address = %s\n",
3822 paddress (gdbarch, addr));
3824 fprintf_unfiltered (gdb_stdlog,
3825 "infrun: (no data address available)\n");
3828 do_cleanups (old_chain);
3831 /* This is originated from start_remote(), start_inferior() and
3832 shared libraries hook functions. */
3833 stop_soon = get_inferior_stop_soon (ecs->ptid);
3834 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
3836 if (!ptid_equal (ecs->ptid, inferior_ptid))
3837 context_switch (ecs->ptid);
3839 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
3840 stop_print_frame = 1;
3841 stop_stepping (ecs);
3845 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
3848 if (!ptid_equal (ecs->ptid, inferior_ptid))
3849 context_switch (ecs->ptid);
3851 fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
3852 stop_print_frame = 0;
3853 stop_stepping (ecs);
3857 /* This originates from attach_command(). We need to overwrite
3858 the stop_signal here, because some kernels don't ignore a
3859 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
3860 See more comments in inferior.h. On the other hand, if we
3861 get a non-SIGSTOP, report it to the user - assume the backend
3862 will handle the SIGSTOP if it should show up later.
3864 Also consider that the attach is complete when we see a
3865 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
3866 target extended-remote report it instead of a SIGSTOP
3867 (e.g. gdbserver). We already rely on SIGTRAP being our
3868 signal, so this is no exception.
3870 Also consider that the attach is complete when we see a
3871 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
3872 the target to stop all threads of the inferior, in case the
3873 low level attach operation doesn't stop them implicitly. If
3874 they weren't stopped implicitly, then the stub will report a
3875 GDB_SIGNAL_0, meaning: stopped for no particular reason
3876 other than GDB's request. */
3877 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
3878 && (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_STOP
3879 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
3880 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_0))
3882 stop_print_frame = 1;
3883 stop_stepping (ecs);
3884 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3888 if (stepping_past_singlestep_breakpoint)
3890 gdb_assert (singlestep_breakpoints_inserted_p);
3891 gdb_assert (ptid_equal (singlestep_ptid, ecs->ptid));
3892 gdb_assert (!ptid_equal (singlestep_ptid, saved_singlestep_ptid));
3894 stepping_past_singlestep_breakpoint = 0;
3896 /* We've either finished single-stepping past the single-step
3897 breakpoint, or stopped for some other reason. It would be nice if
3898 we could tell, but we can't reliably. */
3899 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
3902 fprintf_unfiltered (gdb_stdlog,
3903 "infrun: stepping_past_"
3904 "singlestep_breakpoint\n");
3905 /* Pull the single step breakpoints out of the target. */
3906 if (!ptid_equal (ecs->ptid, inferior_ptid))
3907 context_switch (ecs->ptid);
3908 remove_single_step_breakpoints ();
3909 singlestep_breakpoints_inserted_p = 0;
3911 ecs->event_thread->control.trap_expected = 0;
3913 context_switch (saved_singlestep_ptid);
3914 if (deprecated_context_hook)
3915 deprecated_context_hook (pid_to_thread_id (saved_singlestep_ptid));
3917 resume (1, GDB_SIGNAL_0);
3918 prepare_to_wait (ecs);
3923 if (!ptid_equal (deferred_step_ptid, null_ptid))
3925 /* In non-stop mode, there's never a deferred_step_ptid set. */
3926 gdb_assert (!non_stop);
3928 /* If we stopped for some other reason than single-stepping, ignore
3929 the fact that we were supposed to switch back. */
3930 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
3933 fprintf_unfiltered (gdb_stdlog,
3934 "infrun: handling deferred step\n");
3936 /* Pull the single step breakpoints out of the target. */
3937 if (singlestep_breakpoints_inserted_p)
3939 if (!ptid_equal (ecs->ptid, inferior_ptid))
3940 context_switch (ecs->ptid);
3941 remove_single_step_breakpoints ();
3942 singlestep_breakpoints_inserted_p = 0;
3945 ecs->event_thread->control.trap_expected = 0;
3947 context_switch (deferred_step_ptid);
3948 deferred_step_ptid = null_ptid;
3949 /* Suppress spurious "Switching to ..." message. */
3950 previous_inferior_ptid = inferior_ptid;
3952 resume (1, GDB_SIGNAL_0);
3953 prepare_to_wait (ecs);
3957 deferred_step_ptid = null_ptid;
3960 /* See if a thread hit a thread-specific breakpoint that was meant for
3961 another thread. If so, then step that thread past the breakpoint,
3964 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
3966 int thread_hop_needed = 0;
3967 struct address_space *aspace =
3968 get_regcache_aspace (get_thread_regcache (ecs->ptid));
3970 /* Check if a regular breakpoint has been hit before checking
3971 for a potential single step breakpoint. Otherwise, GDB will
3972 not see this breakpoint hit when stepping onto breakpoints. */
3973 if (regular_breakpoint_inserted_here_p (aspace, stop_pc))
3975 if (!breakpoint_thread_match (aspace, stop_pc, ecs->ptid))
3976 thread_hop_needed = 1;
3978 else if (singlestep_breakpoints_inserted_p)
3980 /* We have not context switched yet, so this should be true
3981 no matter which thread hit the singlestep breakpoint. */
3982 gdb_assert (ptid_equal (inferior_ptid, singlestep_ptid));
3984 fprintf_unfiltered (gdb_stdlog, "infrun: software single step "
3986 target_pid_to_str (ecs->ptid));
3988 /* The call to in_thread_list is necessary because PTIDs sometimes
3989 change when we go from single-threaded to multi-threaded. If
3990 the singlestep_ptid is still in the list, assume that it is
3991 really different from ecs->ptid. */
3992 if (!ptid_equal (singlestep_ptid, ecs->ptid)
3993 && in_thread_list (singlestep_ptid))
3995 /* If the PC of the thread we were trying to single-step
3996 has changed, discard this event (which we were going
3997 to ignore anyway), and pretend we saw that thread
3998 trap. This prevents us continuously moving the
3999 single-step breakpoint forward, one instruction at a
4000 time. If the PC has changed, then the thread we were
4001 trying to single-step has trapped or been signalled,
4002 but the event has not been reported to GDB yet.
4004 There might be some cases where this loses signal
4005 information, if a signal has arrived at exactly the
4006 same time that the PC changed, but this is the best
4007 we can do with the information available. Perhaps we
4008 should arrange to report all events for all threads
4009 when they stop, or to re-poll the remote looking for
4010 this particular thread (i.e. temporarily enable
4013 CORE_ADDR new_singlestep_pc
4014 = regcache_read_pc (get_thread_regcache (singlestep_ptid));
4016 if (new_singlestep_pc != singlestep_pc)
4018 enum gdb_signal stop_signal;
4021 fprintf_unfiltered (gdb_stdlog, "infrun: unexpected thread,"
4022 " but expected thread advanced also\n");
4024 /* The current context still belongs to
4025 singlestep_ptid. Don't swap here, since that's
4026 the context we want to use. Just fudge our
4027 state and continue. */
4028 stop_signal = ecs->event_thread->suspend.stop_signal;
4029 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
4030 ecs->ptid = singlestep_ptid;
4031 ecs->event_thread = find_thread_ptid (ecs->ptid);
4032 ecs->event_thread->suspend.stop_signal = stop_signal;
4033 stop_pc = new_singlestep_pc;
4038 fprintf_unfiltered (gdb_stdlog,
4039 "infrun: unexpected thread\n");
4041 thread_hop_needed = 1;
4042 stepping_past_singlestep_breakpoint = 1;
4043 saved_singlestep_ptid = singlestep_ptid;
4048 if (thread_hop_needed)
4050 struct regcache *thread_regcache;
4051 int remove_status = 0;
4054 fprintf_unfiltered (gdb_stdlog, "infrun: thread_hop_needed\n");
4056 /* Switch context before touching inferior memory, the
4057 previous thread may have exited. */
4058 if (!ptid_equal (inferior_ptid, ecs->ptid))
4059 context_switch (ecs->ptid);
4061 /* Saw a breakpoint, but it was hit by the wrong thread.
4064 if (singlestep_breakpoints_inserted_p)
4066 /* Pull the single step breakpoints out of the target. */
4067 remove_single_step_breakpoints ();
4068 singlestep_breakpoints_inserted_p = 0;
4071 /* If the arch can displace step, don't remove the
4073 thread_regcache = get_thread_regcache (ecs->ptid);
4074 if (!use_displaced_stepping (get_regcache_arch (thread_regcache)))
4075 remove_status = remove_breakpoints ();
4077 /* Did we fail to remove breakpoints? If so, try
4078 to set the PC past the bp. (There's at least
4079 one situation in which we can fail to remove
4080 the bp's: On HP-UX's that use ttrace, we can't
4081 change the address space of a vforking child
4082 process until the child exits (well, okay, not
4083 then either :-) or execs. */
4084 if (remove_status != 0)
4085 error (_("Cannot step over breakpoint hit in wrong thread"));
4090 /* Only need to require the next event from this
4091 thread in all-stop mode. */
4092 waiton_ptid = ecs->ptid;
4093 infwait_state = infwait_thread_hop_state;
4096 ecs->event_thread->stepping_over_breakpoint = 1;
4103 /* See if something interesting happened to the non-current thread. If
4104 so, then switch to that thread. */
4105 if (!ptid_equal (ecs->ptid, inferior_ptid))
4108 fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
4110 context_switch (ecs->ptid);
4112 if (deprecated_context_hook)
4113 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
4116 /* At this point, get hold of the now-current thread's frame. */
4117 frame = get_current_frame ();
4118 gdbarch = get_frame_arch (frame);
4120 if (singlestep_breakpoints_inserted_p)
4122 /* Pull the single step breakpoints out of the target. */
4123 remove_single_step_breakpoints ();
4124 singlestep_breakpoints_inserted_p = 0;
4127 if (ecs->stepped_after_stopped_by_watchpoint)
4128 stopped_by_watchpoint = 0;
4130 stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
4132 /* If necessary, step over this watchpoint. We'll be back to display
4134 if (stopped_by_watchpoint
4135 && (target_have_steppable_watchpoint
4136 || gdbarch_have_nonsteppable_watchpoint (gdbarch)))
4138 /* At this point, we are stopped at an instruction which has
4139 attempted to write to a piece of memory under control of
4140 a watchpoint. The instruction hasn't actually executed
4141 yet. If we were to evaluate the watchpoint expression
4142 now, we would get the old value, and therefore no change
4143 would seem to have occurred.
4145 In order to make watchpoints work `right', we really need
4146 to complete the memory write, and then evaluate the
4147 watchpoint expression. We do this by single-stepping the
4150 It may not be necessary to disable the watchpoint to stop over
4151 it. For example, the PA can (with some kernel cooperation)
4152 single step over a watchpoint without disabling the watchpoint.
4154 It is far more common to need to disable a watchpoint to step
4155 the inferior over it. If we have non-steppable watchpoints,
4156 we must disable the current watchpoint; it's simplest to
4157 disable all watchpoints and breakpoints. */
4160 if (!target_have_steppable_watchpoint)
4162 remove_breakpoints ();
4163 /* See comment in resume why we need to stop bypassing signals
4164 while breakpoints have been removed. */
4165 target_pass_signals (0, NULL);
4168 hw_step = maybe_software_singlestep (gdbarch, stop_pc);
4169 target_resume (ecs->ptid, hw_step, GDB_SIGNAL_0);
4170 waiton_ptid = ecs->ptid;
4171 if (target_have_steppable_watchpoint)
4172 infwait_state = infwait_step_watch_state;
4174 infwait_state = infwait_nonstep_watch_state;
4175 prepare_to_wait (ecs);
4179 ecs->event_thread->stepping_over_breakpoint = 0;
4180 bpstat_clear (&ecs->event_thread->control.stop_bpstat);
4181 ecs->event_thread->control.stop_step = 0;
4182 stop_print_frame = 1;
4183 stopped_by_random_signal = 0;
4185 /* Hide inlined functions starting here, unless we just performed stepi or
4186 nexti. After stepi and nexti, always show the innermost frame (not any
4187 inline function call sites). */
4188 if (ecs->event_thread->control.step_range_end != 1)
4190 struct address_space *aspace =
4191 get_regcache_aspace (get_thread_regcache (ecs->ptid));
4193 /* skip_inline_frames is expensive, so we avoid it if we can
4194 determine that the address is one where functions cannot have
4195 been inlined. This improves performance with inferiors that
4196 load a lot of shared libraries, because the solib event
4197 breakpoint is defined as the address of a function (i.e. not
4198 inline). Note that we have to check the previous PC as well
4199 as the current one to catch cases when we have just
4200 single-stepped off a breakpoint prior to reinstating it.
4201 Note that we're assuming that the code we single-step to is
4202 not inline, but that's not definitive: there's nothing
4203 preventing the event breakpoint function from containing
4204 inlined code, and the single-step ending up there. If the
4205 user had set a breakpoint on that inlined code, the missing
4206 skip_inline_frames call would break things. Fortunately
4207 that's an extremely unlikely scenario. */
4208 if (!pc_at_non_inline_function (aspace, stop_pc, &ecs->ws)
4209 && !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4210 && ecs->event_thread->control.trap_expected
4211 && pc_at_non_inline_function (aspace,
4212 ecs->event_thread->prev_pc,
4215 skip_inline_frames (ecs->ptid);
4217 /* Re-fetch current thread's frame in case that invalidated
4219 frame = get_current_frame ();
4220 gdbarch = get_frame_arch (frame);
4224 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4225 && ecs->event_thread->control.trap_expected
4226 && gdbarch_single_step_through_delay_p (gdbarch)
4227 && currently_stepping (ecs->event_thread))
4229 /* We're trying to step off a breakpoint. Turns out that we're
4230 also on an instruction that needs to be stepped multiple
4231 times before it's been fully executing. E.g., architectures
4232 with a delay slot. It needs to be stepped twice, once for
4233 the instruction and once for the delay slot. */
4234 int step_through_delay
4235 = gdbarch_single_step_through_delay (gdbarch, frame);
4237 if (debug_infrun && step_through_delay)
4238 fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
4239 if (ecs->event_thread->control.step_range_end == 0
4240 && step_through_delay)
4242 /* The user issued a continue when stopped at a breakpoint.
4243 Set up for another trap and get out of here. */
4244 ecs->event_thread->stepping_over_breakpoint = 1;
4248 else if (step_through_delay)
4250 /* The user issued a step when stopped at a breakpoint.
4251 Maybe we should stop, maybe we should not - the delay
4252 slot *might* correspond to a line of source. In any
4253 case, don't decide that here, just set
4254 ecs->stepping_over_breakpoint, making sure we
4255 single-step again before breakpoints are re-inserted. */
4256 ecs->event_thread->stepping_over_breakpoint = 1;
4260 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
4261 handles this event. */
4262 ecs->event_thread->control.stop_bpstat
4263 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
4264 stop_pc, ecs->ptid, &ecs->ws);
4266 /* Following in case break condition called a
4268 stop_print_frame = 1;
4270 /* This is where we handle "moribund" watchpoints. Unlike
4271 software breakpoints traps, hardware watchpoint traps are
4272 always distinguishable from random traps. If no high-level
4273 watchpoint is associated with the reported stop data address
4274 anymore, then the bpstat does not explain the signal ---
4275 simply make sure to ignore it if `stopped_by_watchpoint' is
4279 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4280 && !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
4282 && stopped_by_watchpoint)
4283 fprintf_unfiltered (gdb_stdlog,
4284 "infrun: no user watchpoint explains "
4285 "watchpoint SIGTRAP, ignoring\n");
4287 /* NOTE: cagney/2003-03-29: These checks for a random signal
4288 at one stage in the past included checks for an inferior
4289 function call's call dummy's return breakpoint. The original
4290 comment, that went with the test, read:
4292 ``End of a stack dummy. Some systems (e.g. Sony news) give
4293 another signal besides SIGTRAP, so check here as well as
4296 If someone ever tries to get call dummys on a
4297 non-executable stack to work (where the target would stop
4298 with something like a SIGSEGV), then those tests might need
4299 to be re-instated. Given, however, that the tests were only
4300 enabled when momentary breakpoints were not being used, I
4301 suspect that it won't be the case.
4303 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
4304 be necessary for call dummies on a non-executable stack on
4307 /* See if the breakpoints module can explain the signal. */
4309 = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
4310 ecs->event_thread->suspend.stop_signal);
4312 /* If not, perhaps stepping/nexting can. */
4314 random_signal = !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4315 && currently_stepping (ecs->event_thread));
4317 /* No? Perhaps we got a moribund watchpoint. */
4319 random_signal = !stopped_by_watchpoint;
4321 /* For the program's own signals, act according to
4322 the signal handling tables. */
4326 /* Signal not for debugging purposes. */
4328 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
4329 enum gdb_signal stop_signal = ecs->event_thread->suspend.stop_signal;
4332 fprintf_unfiltered (gdb_stdlog, "infrun: random signal (%s)\n",
4333 gdb_signal_to_symbol_string (stop_signal));
4335 stopped_by_random_signal = 1;
4337 if (signal_print[ecs->event_thread->suspend.stop_signal])
4340 target_terminal_ours_for_output ();
4341 print_signal_received_reason
4342 (ecs->event_thread->suspend.stop_signal);
4344 /* Always stop on signals if we're either just gaining control
4345 of the program, or the user explicitly requested this thread
4346 to remain stopped. */
4347 if (stop_soon != NO_STOP_QUIETLY
4348 || ecs->event_thread->stop_requested
4350 && signal_stop_state (ecs->event_thread->suspend.stop_signal)))
4352 stop_stepping (ecs);
4355 /* If not going to stop, give terminal back
4356 if we took it away. */
4358 target_terminal_inferior ();
4360 /* Clear the signal if it should not be passed. */
4361 if (signal_program[ecs->event_thread->suspend.stop_signal] == 0)
4362 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
4364 if (ecs->event_thread->prev_pc == stop_pc
4365 && ecs->event_thread->control.trap_expected
4366 && ecs->event_thread->control.step_resume_breakpoint == NULL)
4368 /* We were just starting a new sequence, attempting to
4369 single-step off of a breakpoint and expecting a SIGTRAP.
4370 Instead this signal arrives. This signal will take us out
4371 of the stepping range so GDB needs to remember to, when
4372 the signal handler returns, resume stepping off that
4374 /* To simplify things, "continue" is forced to use the same
4375 code paths as single-step - set a breakpoint at the
4376 signal return address and then, once hit, step off that
4379 fprintf_unfiltered (gdb_stdlog,
4380 "infrun: signal arrived while stepping over "
4383 insert_hp_step_resume_breakpoint_at_frame (frame);
4384 ecs->event_thread->step_after_step_resume_breakpoint = 1;
4385 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4386 ecs->event_thread->control.trap_expected = 0;
4388 /* If we were nexting/stepping some other thread, switch to
4389 it, so that we don't continue it, losing control. */
4390 if (!switch_back_to_stepped_thread (ecs))
4395 if (ecs->event_thread->control.step_range_end != 0
4396 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_0
4397 && pc_in_thread_step_range (stop_pc, ecs->event_thread)
4398 && frame_id_eq (get_stack_frame_id (frame),
4399 ecs->event_thread->control.step_stack_frame_id)
4400 && ecs->event_thread->control.step_resume_breakpoint == NULL)
4402 /* The inferior is about to take a signal that will take it
4403 out of the single step range. Set a breakpoint at the
4404 current PC (which is presumably where the signal handler
4405 will eventually return) and then allow the inferior to
4408 Note that this is only needed for a signal delivered
4409 while in the single-step range. Nested signals aren't a
4410 problem as they eventually all return. */
4412 fprintf_unfiltered (gdb_stdlog,
4413 "infrun: signal may take us out of "
4414 "single-step range\n");
4416 insert_hp_step_resume_breakpoint_at_frame (frame);
4417 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4418 ecs->event_thread->control.trap_expected = 0;
4423 /* Note: step_resume_breakpoint may be non-NULL. This occures
4424 when either there's a nested signal, or when there's a
4425 pending signal enabled just as the signal handler returns
4426 (leaving the inferior at the step-resume-breakpoint without
4427 actually executing it). Either way continue until the
4428 breakpoint is really hit. */
4430 if (!switch_back_to_stepped_thread (ecs))
4433 fprintf_unfiltered (gdb_stdlog,
4434 "infrun: random signal, keep going\n");
4441 process_event_stop_test (ecs);
4444 /* Come here when we've got some debug event / signal we can explain
4445 (IOW, not a random signal), and test whether it should cause a
4446 stop, or whether we should resume the inferior (transparently).
4447 E.g., could be a breakpoint whose condition evaluates false; we
4448 could be still stepping within the line; etc. */
4451 process_event_stop_test (struct execution_control_state *ecs)
4453 struct symtab_and_line stop_pc_sal;
4454 struct frame_info *frame;
4455 struct gdbarch *gdbarch;
4456 CORE_ADDR jmp_buf_pc;
4457 struct bpstat_what what;
4459 /* Handle cases caused by hitting a breakpoint. */
4461 frame = get_current_frame ();
4462 gdbarch = get_frame_arch (frame);
4464 what = bpstat_what (ecs->event_thread->control.stop_bpstat);
4466 if (what.call_dummy)
4468 stop_stack_dummy = what.call_dummy;
4471 /* If we hit an internal event that triggers symbol changes, the
4472 current frame will be invalidated within bpstat_what (e.g., if we
4473 hit an internal solib event). Re-fetch it. */
4474 frame = get_current_frame ();
4475 gdbarch = get_frame_arch (frame);
4477 switch (what.main_action)
4479 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
4480 /* If we hit the breakpoint at longjmp while stepping, we
4481 install a momentary breakpoint at the target of the
4485 fprintf_unfiltered (gdb_stdlog,
4486 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
4488 ecs->event_thread->stepping_over_breakpoint = 1;
4490 if (what.is_longjmp)
4492 struct value *arg_value;
4494 /* If we set the longjmp breakpoint via a SystemTap probe,
4495 then use it to extract the arguments. The destination PC
4496 is the third argument to the probe. */
4497 arg_value = probe_safe_evaluate_at_pc (frame, 2);
4499 jmp_buf_pc = value_as_address (arg_value);
4500 else if (!gdbarch_get_longjmp_target_p (gdbarch)
4501 || !gdbarch_get_longjmp_target (gdbarch,
4502 frame, &jmp_buf_pc))
4505 fprintf_unfiltered (gdb_stdlog,
4506 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME "
4507 "(!gdbarch_get_longjmp_target)\n");
4512 /* Insert a breakpoint at resume address. */
4513 insert_longjmp_resume_breakpoint (gdbarch, jmp_buf_pc);
4516 check_exception_resume (ecs, frame);
4520 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
4522 struct frame_info *init_frame;
4524 /* There are several cases to consider.
4526 1. The initiating frame no longer exists. In this case we
4527 must stop, because the exception or longjmp has gone too
4530 2. The initiating frame exists, and is the same as the
4531 current frame. We stop, because the exception or longjmp
4534 3. The initiating frame exists and is different from the
4535 current frame. This means the exception or longjmp has
4536 been caught beneath the initiating frame, so keep going.
4538 4. longjmp breakpoint has been placed just to protect
4539 against stale dummy frames and user is not interested in
4540 stopping around longjmps. */
4543 fprintf_unfiltered (gdb_stdlog,
4544 "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
4546 gdb_assert (ecs->event_thread->control.exception_resume_breakpoint
4548 delete_exception_resume_breakpoint (ecs->event_thread);
4550 if (what.is_longjmp)
4552 check_longjmp_breakpoint_for_call_dummy (ecs->event_thread->num);
4554 if (!frame_id_p (ecs->event_thread->initiating_frame))
4562 init_frame = frame_find_by_id (ecs->event_thread->initiating_frame);
4566 struct frame_id current_id
4567 = get_frame_id (get_current_frame ());
4568 if (frame_id_eq (current_id,
4569 ecs->event_thread->initiating_frame))
4571 /* Case 2. Fall through. */
4581 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
4583 delete_step_resume_breakpoint (ecs->event_thread);
4585 ecs->event_thread->control.stop_step = 1;
4586 print_end_stepping_range_reason ();
4587 stop_stepping (ecs);
4591 case BPSTAT_WHAT_SINGLE:
4593 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
4594 ecs->event_thread->stepping_over_breakpoint = 1;
4595 /* Still need to check other stuff, at least the case where we
4596 are stepping and step out of the right range. */
4599 case BPSTAT_WHAT_STEP_RESUME:
4601 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
4603 delete_step_resume_breakpoint (ecs->event_thread);
4604 if (ecs->event_thread->control.proceed_to_finish
4605 && execution_direction == EXEC_REVERSE)
4607 struct thread_info *tp = ecs->event_thread;
4609 /* We are finishing a function in reverse, and just hit the
4610 step-resume breakpoint at the start address of the
4611 function, and we're almost there -- just need to back up
4612 by one more single-step, which should take us back to the
4614 tp->control.step_range_start = tp->control.step_range_end = 1;
4618 fill_in_stop_func (gdbarch, ecs);
4619 if (stop_pc == ecs->stop_func_start
4620 && execution_direction == EXEC_REVERSE)
4622 /* We are stepping over a function call in reverse, and just
4623 hit the step-resume breakpoint at the start address of
4624 the function. Go back to single-stepping, which should
4625 take us back to the function call. */
4626 ecs->event_thread->stepping_over_breakpoint = 1;
4632 case BPSTAT_WHAT_STOP_NOISY:
4634 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
4635 stop_print_frame = 1;
4637 /* We are about to nuke the step_resume_breakpointt via the
4638 cleanup chain, so no need to worry about it here. */
4640 stop_stepping (ecs);
4643 case BPSTAT_WHAT_STOP_SILENT:
4645 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
4646 stop_print_frame = 0;
4648 /* We are about to nuke the step_resume_breakpoin via the
4649 cleanup chain, so no need to worry about it here. */
4651 stop_stepping (ecs);
4654 case BPSTAT_WHAT_HP_STEP_RESUME:
4656 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_HP_STEP_RESUME\n");
4658 delete_step_resume_breakpoint (ecs->event_thread);
4659 if (ecs->event_thread->step_after_step_resume_breakpoint)
4661 /* Back when the step-resume breakpoint was inserted, we
4662 were trying to single-step off a breakpoint. Go back to
4664 ecs->event_thread->step_after_step_resume_breakpoint = 0;
4665 ecs->event_thread->stepping_over_breakpoint = 1;
4671 case BPSTAT_WHAT_KEEP_CHECKING:
4675 /* We come here if we hit a breakpoint but should not stop for it.
4676 Possibly we also were stepping and should stop for that. So fall
4677 through and test for stepping. But, if not stepping, do not
4680 /* In all-stop mode, if we're currently stepping but have stopped in
4681 some other thread, we need to switch back to the stepped thread. */
4682 if (switch_back_to_stepped_thread (ecs))
4685 if (ecs->event_thread->control.step_resume_breakpoint)
4688 fprintf_unfiltered (gdb_stdlog,
4689 "infrun: step-resume breakpoint is inserted\n");
4691 /* Having a step-resume breakpoint overrides anything
4692 else having to do with stepping commands until
4693 that breakpoint is reached. */
4698 if (ecs->event_thread->control.step_range_end == 0)
4701 fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
4702 /* Likewise if we aren't even stepping. */
4707 /* Re-fetch current thread's frame in case the code above caused
4708 the frame cache to be re-initialized, making our FRAME variable
4709 a dangling pointer. */
4710 frame = get_current_frame ();
4711 gdbarch = get_frame_arch (frame);
4712 fill_in_stop_func (gdbarch, ecs);
4714 /* If stepping through a line, keep going if still within it.
4716 Note that step_range_end is the address of the first instruction
4717 beyond the step range, and NOT the address of the last instruction
4720 Note also that during reverse execution, we may be stepping
4721 through a function epilogue and therefore must detect when
4722 the current-frame changes in the middle of a line. */
4724 if (pc_in_thread_step_range (stop_pc, ecs->event_thread)
4725 && (execution_direction != EXEC_REVERSE
4726 || frame_id_eq (get_frame_id (frame),
4727 ecs->event_thread->control.step_frame_id)))
4731 (gdb_stdlog, "infrun: stepping inside range [%s-%s]\n",
4732 paddress (gdbarch, ecs->event_thread->control.step_range_start),
4733 paddress (gdbarch, ecs->event_thread->control.step_range_end));
4735 /* Tentatively re-enable range stepping; `resume' disables it if
4736 necessary (e.g., if we're stepping over a breakpoint or we
4737 have software watchpoints). */
4738 ecs->event_thread->control.may_range_step = 1;
4740 /* When stepping backward, stop at beginning of line range
4741 (unless it's the function entry point, in which case
4742 keep going back to the call point). */
4743 if (stop_pc == ecs->event_thread->control.step_range_start
4744 && stop_pc != ecs->stop_func_start
4745 && execution_direction == EXEC_REVERSE)
4747 ecs->event_thread->control.stop_step = 1;
4748 print_end_stepping_range_reason ();
4749 stop_stepping (ecs);
4757 /* We stepped out of the stepping range. */
4759 /* If we are stepping at the source level and entered the runtime
4760 loader dynamic symbol resolution code...
4762 EXEC_FORWARD: we keep on single stepping until we exit the run
4763 time loader code and reach the callee's address.
4765 EXEC_REVERSE: we've already executed the callee (backward), and
4766 the runtime loader code is handled just like any other
4767 undebuggable function call. Now we need only keep stepping
4768 backward through the trampoline code, and that's handled further
4769 down, so there is nothing for us to do here. */
4771 if (execution_direction != EXEC_REVERSE
4772 && ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4773 && in_solib_dynsym_resolve_code (stop_pc))
4775 CORE_ADDR pc_after_resolver =
4776 gdbarch_skip_solib_resolver (gdbarch, stop_pc);
4779 fprintf_unfiltered (gdb_stdlog,
4780 "infrun: stepped into dynsym resolve code\n");
4782 if (pc_after_resolver)
4784 /* Set up a step-resume breakpoint at the address
4785 indicated by SKIP_SOLIB_RESOLVER. */
4786 struct symtab_and_line sr_sal;
4789 sr_sal.pc = pc_after_resolver;
4790 sr_sal.pspace = get_frame_program_space (frame);
4792 insert_step_resume_breakpoint_at_sal (gdbarch,
4793 sr_sal, null_frame_id);
4800 if (ecs->event_thread->control.step_range_end != 1
4801 && (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4802 || ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
4803 && get_frame_type (frame) == SIGTRAMP_FRAME)
4806 fprintf_unfiltered (gdb_stdlog,
4807 "infrun: stepped into signal trampoline\n");
4808 /* The inferior, while doing a "step" or "next", has ended up in
4809 a signal trampoline (either by a signal being delivered or by
4810 the signal handler returning). Just single-step until the
4811 inferior leaves the trampoline (either by calling the handler
4817 /* If we're in the return path from a shared library trampoline,
4818 we want to proceed through the trampoline when stepping. */
4819 /* macro/2012-04-25: This needs to come before the subroutine
4820 call check below as on some targets return trampolines look
4821 like subroutine calls (MIPS16 return thunks). */
4822 if (gdbarch_in_solib_return_trampoline (gdbarch,
4823 stop_pc, ecs->stop_func_name)
4824 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
4826 /* Determine where this trampoline returns. */
4827 CORE_ADDR real_stop_pc;
4829 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
4832 fprintf_unfiltered (gdb_stdlog,
4833 "infrun: stepped into solib return tramp\n");
4835 /* Only proceed through if we know where it's going. */
4838 /* And put the step-breakpoint there and go until there. */
4839 struct symtab_and_line sr_sal;
4841 init_sal (&sr_sal); /* initialize to zeroes */
4842 sr_sal.pc = real_stop_pc;
4843 sr_sal.section = find_pc_overlay (sr_sal.pc);
4844 sr_sal.pspace = get_frame_program_space (frame);
4846 /* Do not specify what the fp should be when we stop since
4847 on some machines the prologue is where the new fp value
4849 insert_step_resume_breakpoint_at_sal (gdbarch,
4850 sr_sal, null_frame_id);
4852 /* Restart without fiddling with the step ranges or
4859 /* Check for subroutine calls. The check for the current frame
4860 equalling the step ID is not necessary - the check of the
4861 previous frame's ID is sufficient - but it is a common case and
4862 cheaper than checking the previous frame's ID.
4864 NOTE: frame_id_eq will never report two invalid frame IDs as
4865 being equal, so to get into this block, both the current and
4866 previous frame must have valid frame IDs. */
4867 /* The outer_frame_id check is a heuristic to detect stepping
4868 through startup code. If we step over an instruction which
4869 sets the stack pointer from an invalid value to a valid value,
4870 we may detect that as a subroutine call from the mythical
4871 "outermost" function. This could be fixed by marking
4872 outermost frames as !stack_p,code_p,special_p. Then the
4873 initial outermost frame, before sp was valid, would
4874 have code_addr == &_start. See the comment in frame_id_eq
4876 if (!frame_id_eq (get_stack_frame_id (frame),
4877 ecs->event_thread->control.step_stack_frame_id)
4878 && (frame_id_eq (frame_unwind_caller_id (get_current_frame ()),
4879 ecs->event_thread->control.step_stack_frame_id)
4880 && (!frame_id_eq (ecs->event_thread->control.step_stack_frame_id,
4882 || step_start_function != find_pc_function (stop_pc))))
4884 CORE_ADDR real_stop_pc;
4887 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
4889 if ((ecs->event_thread->control.step_over_calls == STEP_OVER_NONE)
4890 || ((ecs->event_thread->control.step_range_end == 1)
4891 && in_prologue (gdbarch, ecs->event_thread->prev_pc,
4892 ecs->stop_func_start)))
4894 /* I presume that step_over_calls is only 0 when we're
4895 supposed to be stepping at the assembly language level
4896 ("stepi"). Just stop. */
4897 /* Also, maybe we just did a "nexti" inside a prolog, so we
4898 thought it was a subroutine call but it was not. Stop as
4900 /* And this works the same backward as frontward. MVS */
4901 ecs->event_thread->control.stop_step = 1;
4902 print_end_stepping_range_reason ();
4903 stop_stepping (ecs);
4907 /* Reverse stepping through solib trampolines. */
4909 if (execution_direction == EXEC_REVERSE
4910 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
4911 && (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
4912 || (ecs->stop_func_start == 0
4913 && in_solib_dynsym_resolve_code (stop_pc))))
4915 /* Any solib trampoline code can be handled in reverse
4916 by simply continuing to single-step. We have already
4917 executed the solib function (backwards), and a few
4918 steps will take us back through the trampoline to the
4924 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
4926 /* We're doing a "next".
4928 Normal (forward) execution: set a breakpoint at the
4929 callee's return address (the address at which the caller
4932 Reverse (backward) execution. set the step-resume
4933 breakpoint at the start of the function that we just
4934 stepped into (backwards), and continue to there. When we
4935 get there, we'll need to single-step back to the caller. */
4937 if (execution_direction == EXEC_REVERSE)
4939 /* If we're already at the start of the function, we've either
4940 just stepped backward into a single instruction function,
4941 or stepped back out of a signal handler to the first instruction
4942 of the function. Just keep going, which will single-step back
4944 if (ecs->stop_func_start != stop_pc && ecs->stop_func_start != 0)
4946 struct symtab_and_line sr_sal;
4948 /* Normal function call return (static or dynamic). */
4950 sr_sal.pc = ecs->stop_func_start;
4951 sr_sal.pspace = get_frame_program_space (frame);
4952 insert_step_resume_breakpoint_at_sal (gdbarch,
4953 sr_sal, null_frame_id);
4957 insert_step_resume_breakpoint_at_caller (frame);
4963 /* If we are in a function call trampoline (a stub between the
4964 calling routine and the real function), locate the real
4965 function. That's what tells us (a) whether we want to step
4966 into it at all, and (b) what prologue we want to run to the
4967 end of, if we do step into it. */
4968 real_stop_pc = skip_language_trampoline (frame, stop_pc);
4969 if (real_stop_pc == 0)
4970 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
4971 if (real_stop_pc != 0)
4972 ecs->stop_func_start = real_stop_pc;
4974 if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
4976 struct symtab_and_line sr_sal;
4979 sr_sal.pc = ecs->stop_func_start;
4980 sr_sal.pspace = get_frame_program_space (frame);
4982 insert_step_resume_breakpoint_at_sal (gdbarch,
4983 sr_sal, null_frame_id);
4988 /* If we have line number information for the function we are
4989 thinking of stepping into and the function isn't on the skip
4992 If there are several symtabs at that PC (e.g. with include
4993 files), just want to know whether *any* of them have line
4994 numbers. find_pc_line handles this. */
4996 struct symtab_and_line tmp_sal;
4998 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
4999 if (tmp_sal.line != 0
5000 && !function_name_is_marked_for_skip (ecs->stop_func_name,
5003 if (execution_direction == EXEC_REVERSE)
5004 handle_step_into_function_backward (gdbarch, ecs);
5006 handle_step_into_function (gdbarch, ecs);
5011 /* If we have no line number and the step-stop-if-no-debug is
5012 set, we stop the step so that the user has a chance to switch
5013 in assembly mode. */
5014 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
5015 && step_stop_if_no_debug)
5017 ecs->event_thread->control.stop_step = 1;
5018 print_end_stepping_range_reason ();
5019 stop_stepping (ecs);
5023 if (execution_direction == EXEC_REVERSE)
5025 /* If we're already at the start of the function, we've either just
5026 stepped backward into a single instruction function without line
5027 number info, or stepped back out of a signal handler to the first
5028 instruction of the function without line number info. Just keep
5029 going, which will single-step back to the caller. */
5030 if (ecs->stop_func_start != stop_pc)
5032 /* Set a breakpoint at callee's start address.
5033 From there we can step once and be back in the caller. */
5034 struct symtab_and_line sr_sal;
5037 sr_sal.pc = ecs->stop_func_start;
5038 sr_sal.pspace = get_frame_program_space (frame);
5039 insert_step_resume_breakpoint_at_sal (gdbarch,
5040 sr_sal, null_frame_id);
5044 /* Set a breakpoint at callee's return address (the address
5045 at which the caller will resume). */
5046 insert_step_resume_breakpoint_at_caller (frame);
5052 /* Reverse stepping through solib trampolines. */
5054 if (execution_direction == EXEC_REVERSE
5055 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
5057 if (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
5058 || (ecs->stop_func_start == 0
5059 && in_solib_dynsym_resolve_code (stop_pc)))
5061 /* Any solib trampoline code can be handled in reverse
5062 by simply continuing to single-step. We have already
5063 executed the solib function (backwards), and a few
5064 steps will take us back through the trampoline to the
5069 else if (in_solib_dynsym_resolve_code (stop_pc))
5071 /* Stepped backward into the solib dynsym resolver.
5072 Set a breakpoint at its start and continue, then
5073 one more step will take us out. */
5074 struct symtab_and_line sr_sal;
5077 sr_sal.pc = ecs->stop_func_start;
5078 sr_sal.pspace = get_frame_program_space (frame);
5079 insert_step_resume_breakpoint_at_sal (gdbarch,
5080 sr_sal, null_frame_id);
5086 stop_pc_sal = find_pc_line (stop_pc, 0);
5088 /* NOTE: tausq/2004-05-24: This if block used to be done before all
5089 the trampoline processing logic, however, there are some trampolines
5090 that have no names, so we should do trampoline handling first. */
5091 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
5092 && ecs->stop_func_name == NULL
5093 && stop_pc_sal.line == 0)
5096 fprintf_unfiltered (gdb_stdlog,
5097 "infrun: stepped into undebuggable function\n");
5099 /* The inferior just stepped into, or returned to, an
5100 undebuggable function (where there is no debugging information
5101 and no line number corresponding to the address where the
5102 inferior stopped). Since we want to skip this kind of code,
5103 we keep going until the inferior returns from this
5104 function - unless the user has asked us not to (via
5105 set step-mode) or we no longer know how to get back
5106 to the call site. */
5107 if (step_stop_if_no_debug
5108 || !frame_id_p (frame_unwind_caller_id (frame)))
5110 /* If we have no line number and the step-stop-if-no-debug
5111 is set, we stop the step so that the user has a chance to
5112 switch in assembly mode. */
5113 ecs->event_thread->control.stop_step = 1;
5114 print_end_stepping_range_reason ();
5115 stop_stepping (ecs);
5120 /* Set a breakpoint at callee's return address (the address
5121 at which the caller will resume). */
5122 insert_step_resume_breakpoint_at_caller (frame);
5128 if (ecs->event_thread->control.step_range_end == 1)
5130 /* It is stepi or nexti. We always want to stop stepping after
5133 fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
5134 ecs->event_thread->control.stop_step = 1;
5135 print_end_stepping_range_reason ();
5136 stop_stepping (ecs);
5140 if (stop_pc_sal.line == 0)
5142 /* We have no line number information. That means to stop
5143 stepping (does this always happen right after one instruction,
5144 when we do "s" in a function with no line numbers,
5145 or can this happen as a result of a return or longjmp?). */
5147 fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
5148 ecs->event_thread->control.stop_step = 1;
5149 print_end_stepping_range_reason ();
5150 stop_stepping (ecs);
5154 /* Look for "calls" to inlined functions, part one. If the inline
5155 frame machinery detected some skipped call sites, we have entered
5156 a new inline function. */
5158 if (frame_id_eq (get_frame_id (get_current_frame ()),
5159 ecs->event_thread->control.step_frame_id)
5160 && inline_skipped_frames (ecs->ptid))
5162 struct symtab_and_line call_sal;
5165 fprintf_unfiltered (gdb_stdlog,
5166 "infrun: stepped into inlined function\n");
5168 find_frame_sal (get_current_frame (), &call_sal);
5170 if (ecs->event_thread->control.step_over_calls != STEP_OVER_ALL)
5172 /* For "step", we're going to stop. But if the call site
5173 for this inlined function is on the same source line as
5174 we were previously stepping, go down into the function
5175 first. Otherwise stop at the call site. */
5177 if (call_sal.line == ecs->event_thread->current_line
5178 && call_sal.symtab == ecs->event_thread->current_symtab)
5179 step_into_inline_frame (ecs->ptid);
5181 ecs->event_thread->control.stop_step = 1;
5182 print_end_stepping_range_reason ();
5183 stop_stepping (ecs);
5188 /* For "next", we should stop at the call site if it is on a
5189 different source line. Otherwise continue through the
5190 inlined function. */
5191 if (call_sal.line == ecs->event_thread->current_line
5192 && call_sal.symtab == ecs->event_thread->current_symtab)
5196 ecs->event_thread->control.stop_step = 1;
5197 print_end_stepping_range_reason ();
5198 stop_stepping (ecs);
5204 /* Look for "calls" to inlined functions, part two. If we are still
5205 in the same real function we were stepping through, but we have
5206 to go further up to find the exact frame ID, we are stepping
5207 through a more inlined call beyond its call site. */
5209 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
5210 && !frame_id_eq (get_frame_id (get_current_frame ()),
5211 ecs->event_thread->control.step_frame_id)
5212 && stepped_in_from (get_current_frame (),
5213 ecs->event_thread->control.step_frame_id))
5216 fprintf_unfiltered (gdb_stdlog,
5217 "infrun: stepping through inlined function\n");
5219 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
5223 ecs->event_thread->control.stop_step = 1;
5224 print_end_stepping_range_reason ();
5225 stop_stepping (ecs);
5230 if ((stop_pc == stop_pc_sal.pc)
5231 && (ecs->event_thread->current_line != stop_pc_sal.line
5232 || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
5234 /* We are at the start of a different line. So stop. Note that
5235 we don't stop if we step into the middle of a different line.
5236 That is said to make things like for (;;) statements work
5239 fprintf_unfiltered (gdb_stdlog,
5240 "infrun: stepped to a different line\n");
5241 ecs->event_thread->control.stop_step = 1;
5242 print_end_stepping_range_reason ();
5243 stop_stepping (ecs);
5247 /* We aren't done stepping.
5249 Optimize by setting the stepping range to the line.
5250 (We might not be in the original line, but if we entered a
5251 new line in mid-statement, we continue stepping. This makes
5252 things like for(;;) statements work better.) */
5254 ecs->event_thread->control.step_range_start = stop_pc_sal.pc;
5255 ecs->event_thread->control.step_range_end = stop_pc_sal.end;
5256 ecs->event_thread->control.may_range_step = 1;
5257 set_step_info (frame, stop_pc_sal);
5260 fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
5264 /* In all-stop mode, if we're currently stepping but have stopped in
5265 some other thread, we may need to switch back to the stepped
5266 thread. Returns true we set the inferior running, false if we left
5267 it stopped (and the event needs further processing). */
5270 switch_back_to_stepped_thread (struct execution_control_state *ecs)
5274 struct thread_info *tp;
5276 tp = iterate_over_threads (currently_stepping_or_nexting_callback,
5280 /* However, if the current thread is blocked on some internal
5281 breakpoint, and we simply need to step over that breakpoint
5282 to get it going again, do that first. */
5283 if ((ecs->event_thread->control.trap_expected
5284 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
5285 || ecs->event_thread->stepping_over_breakpoint)
5291 /* If the stepping thread exited, then don't try to switch
5292 back and resume it, which could fail in several different
5293 ways depending on the target. Instead, just keep going.
5295 We can find a stepping dead thread in the thread list in
5298 - The target supports thread exit events, and when the
5299 target tries to delete the thread from the thread list,
5300 inferior_ptid pointed at the exiting thread. In such
5301 case, calling delete_thread does not really remove the
5302 thread from the list; instead, the thread is left listed,
5303 with 'exited' state.
5305 - The target's debug interface does not support thread
5306 exit events, and so we have no idea whatsoever if the
5307 previously stepping thread is still alive. For that
5308 reason, we need to synchronously query the target
5310 if (is_exited (tp->ptid)
5311 || !target_thread_alive (tp->ptid))
5314 fprintf_unfiltered (gdb_stdlog,
5315 "infrun: not switching back to "
5316 "stepped thread, it has vanished\n");
5318 delete_thread (tp->ptid);
5323 /* Otherwise, we no longer expect a trap in the current thread.
5324 Clear the trap_expected flag before switching back -- this is
5325 what keep_going would do as well, if we called it. */
5326 ecs->event_thread->control.trap_expected = 0;
5329 fprintf_unfiltered (gdb_stdlog,
5330 "infrun: switching back to stepped thread\n");
5332 ecs->event_thread = tp;
5333 ecs->ptid = tp->ptid;
5334 context_switch (ecs->ptid);
5342 /* Is thread TP in the middle of single-stepping? */
5345 currently_stepping (struct thread_info *tp)
5347 return ((tp->control.step_range_end
5348 && tp->control.step_resume_breakpoint == NULL)
5349 || tp->control.trap_expected
5350 || bpstat_should_step ());
5353 /* Returns true if any thread *but* the one passed in "data" is in the
5354 middle of stepping or of handling a "next". */
5357 currently_stepping_or_nexting_callback (struct thread_info *tp, void *data)
5362 return (tp->control.step_range_end
5363 || tp->control.trap_expected);
5366 /* Inferior has stepped into a subroutine call with source code that
5367 we should not step over. Do step to the first line of code in
5371 handle_step_into_function (struct gdbarch *gdbarch,
5372 struct execution_control_state *ecs)
5375 struct symtab_and_line stop_func_sal, sr_sal;
5377 fill_in_stop_func (gdbarch, ecs);
5379 s = find_pc_symtab (stop_pc);
5380 if (s && s->language != language_asm)
5381 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
5382 ecs->stop_func_start);
5384 stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
5385 /* Use the step_resume_break to step until the end of the prologue,
5386 even if that involves jumps (as it seems to on the vax under
5388 /* If the prologue ends in the middle of a source line, continue to
5389 the end of that source line (if it is still within the function).
5390 Otherwise, just go to end of prologue. */
5391 if (stop_func_sal.end
5392 && stop_func_sal.pc != ecs->stop_func_start
5393 && stop_func_sal.end < ecs->stop_func_end)
5394 ecs->stop_func_start = stop_func_sal.end;
5396 /* Architectures which require breakpoint adjustment might not be able
5397 to place a breakpoint at the computed address. If so, the test
5398 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
5399 ecs->stop_func_start to an address at which a breakpoint may be
5400 legitimately placed.
5402 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
5403 made, GDB will enter an infinite loop when stepping through
5404 optimized code consisting of VLIW instructions which contain
5405 subinstructions corresponding to different source lines. On
5406 FR-V, it's not permitted to place a breakpoint on any but the
5407 first subinstruction of a VLIW instruction. When a breakpoint is
5408 set, GDB will adjust the breakpoint address to the beginning of
5409 the VLIW instruction. Thus, we need to make the corresponding
5410 adjustment here when computing the stop address. */
5412 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
5414 ecs->stop_func_start
5415 = gdbarch_adjust_breakpoint_address (gdbarch,
5416 ecs->stop_func_start);
5419 if (ecs->stop_func_start == stop_pc)
5421 /* We are already there: stop now. */
5422 ecs->event_thread->control.stop_step = 1;
5423 print_end_stepping_range_reason ();
5424 stop_stepping (ecs);
5429 /* Put the step-breakpoint there and go until there. */
5430 init_sal (&sr_sal); /* initialize to zeroes */
5431 sr_sal.pc = ecs->stop_func_start;
5432 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
5433 sr_sal.pspace = get_frame_program_space (get_current_frame ());
5435 /* Do not specify what the fp should be when we stop since on
5436 some machines the prologue is where the new fp value is
5438 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal, null_frame_id);
5440 /* And make sure stepping stops right away then. */
5441 ecs->event_thread->control.step_range_end
5442 = ecs->event_thread->control.step_range_start;
5447 /* Inferior has stepped backward into a subroutine call with source
5448 code that we should not step over. Do step to the beginning of the
5449 last line of code in it. */
5452 handle_step_into_function_backward (struct gdbarch *gdbarch,
5453 struct execution_control_state *ecs)
5456 struct symtab_and_line stop_func_sal;
5458 fill_in_stop_func (gdbarch, ecs);
5460 s = find_pc_symtab (stop_pc);
5461 if (s && s->language != language_asm)
5462 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
5463 ecs->stop_func_start);
5465 stop_func_sal = find_pc_line (stop_pc, 0);
5467 /* OK, we're just going to keep stepping here. */
5468 if (stop_func_sal.pc == stop_pc)
5470 /* We're there already. Just stop stepping now. */
5471 ecs->event_thread->control.stop_step = 1;
5472 print_end_stepping_range_reason ();
5473 stop_stepping (ecs);
5477 /* Else just reset the step range and keep going.
5478 No step-resume breakpoint, they don't work for
5479 epilogues, which can have multiple entry paths. */
5480 ecs->event_thread->control.step_range_start = stop_func_sal.pc;
5481 ecs->event_thread->control.step_range_end = stop_func_sal.end;
5487 /* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
5488 This is used to both functions and to skip over code. */
5491 insert_step_resume_breakpoint_at_sal_1 (struct gdbarch *gdbarch,
5492 struct symtab_and_line sr_sal,
5493 struct frame_id sr_id,
5494 enum bptype sr_type)
5496 /* There should never be more than one step-resume or longjmp-resume
5497 breakpoint per thread, so we should never be setting a new
5498 step_resume_breakpoint when one is already active. */
5499 gdb_assert (inferior_thread ()->control.step_resume_breakpoint == NULL);
5500 gdb_assert (sr_type == bp_step_resume || sr_type == bp_hp_step_resume);
5503 fprintf_unfiltered (gdb_stdlog,
5504 "infrun: inserting step-resume breakpoint at %s\n",
5505 paddress (gdbarch, sr_sal.pc));
5507 inferior_thread ()->control.step_resume_breakpoint
5508 = set_momentary_breakpoint (gdbarch, sr_sal, sr_id, sr_type);
5512 insert_step_resume_breakpoint_at_sal (struct gdbarch *gdbarch,
5513 struct symtab_and_line sr_sal,
5514 struct frame_id sr_id)
5516 insert_step_resume_breakpoint_at_sal_1 (gdbarch,
5521 /* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
5522 This is used to skip a potential signal handler.
5524 This is called with the interrupted function's frame. The signal
5525 handler, when it returns, will resume the interrupted function at
5529 insert_hp_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
5531 struct symtab_and_line sr_sal;
5532 struct gdbarch *gdbarch;
5534 gdb_assert (return_frame != NULL);
5535 init_sal (&sr_sal); /* initialize to zeros */
5537 gdbarch = get_frame_arch (return_frame);
5538 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch, get_frame_pc (return_frame));
5539 sr_sal.section = find_pc_overlay (sr_sal.pc);
5540 sr_sal.pspace = get_frame_program_space (return_frame);
5542 insert_step_resume_breakpoint_at_sal_1 (gdbarch, sr_sal,
5543 get_stack_frame_id (return_frame),
5547 /* Insert a "step-resume breakpoint" at the previous frame's PC. This
5548 is used to skip a function after stepping into it (for "next" or if
5549 the called function has no debugging information).
5551 The current function has almost always been reached by single
5552 stepping a call or return instruction. NEXT_FRAME belongs to the
5553 current function, and the breakpoint will be set at the caller's
5556 This is a separate function rather than reusing
5557 insert_hp_step_resume_breakpoint_at_frame in order to avoid
5558 get_prev_frame, which may stop prematurely (see the implementation
5559 of frame_unwind_caller_id for an example). */
5562 insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
5564 struct symtab_and_line sr_sal;
5565 struct gdbarch *gdbarch;
5567 /* We shouldn't have gotten here if we don't know where the call site
5569 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame)));
5571 init_sal (&sr_sal); /* initialize to zeros */
5573 gdbarch = frame_unwind_caller_arch (next_frame);
5574 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch,
5575 frame_unwind_caller_pc (next_frame));
5576 sr_sal.section = find_pc_overlay (sr_sal.pc);
5577 sr_sal.pspace = frame_unwind_program_space (next_frame);
5579 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal,
5580 frame_unwind_caller_id (next_frame));
5583 /* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
5584 new breakpoint at the target of a jmp_buf. The handling of
5585 longjmp-resume uses the same mechanisms used for handling
5586 "step-resume" breakpoints. */
5589 insert_longjmp_resume_breakpoint (struct gdbarch *gdbarch, CORE_ADDR pc)
5591 /* There should never be more than one longjmp-resume breakpoint per
5592 thread, so we should never be setting a new
5593 longjmp_resume_breakpoint when one is already active. */
5594 gdb_assert (inferior_thread ()->control.exception_resume_breakpoint == NULL);
5597 fprintf_unfiltered (gdb_stdlog,
5598 "infrun: inserting longjmp-resume breakpoint at %s\n",
5599 paddress (gdbarch, pc));
5601 inferior_thread ()->control.exception_resume_breakpoint =
5602 set_momentary_breakpoint_at_pc (gdbarch, pc, bp_longjmp_resume);
5605 /* Insert an exception resume breakpoint. TP is the thread throwing
5606 the exception. The block B is the block of the unwinder debug hook
5607 function. FRAME is the frame corresponding to the call to this
5608 function. SYM is the symbol of the function argument holding the
5609 target PC of the exception. */
5612 insert_exception_resume_breakpoint (struct thread_info *tp,
5614 struct frame_info *frame,
5617 volatile struct gdb_exception e;
5619 /* We want to ignore errors here. */
5620 TRY_CATCH (e, RETURN_MASK_ERROR)
5622 struct symbol *vsym;
5623 struct value *value;
5625 struct breakpoint *bp;
5627 vsym = lookup_symbol (SYMBOL_LINKAGE_NAME (sym), b, VAR_DOMAIN, NULL);
5628 value = read_var_value (vsym, frame);
5629 /* If the value was optimized out, revert to the old behavior. */
5630 if (! value_optimized_out (value))
5632 handler = value_as_address (value);
5635 fprintf_unfiltered (gdb_stdlog,
5636 "infrun: exception resume at %lx\n",
5637 (unsigned long) handler);
5639 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
5640 handler, bp_exception_resume);
5642 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
5645 bp->thread = tp->num;
5646 inferior_thread ()->control.exception_resume_breakpoint = bp;
5651 /* A helper for check_exception_resume that sets an
5652 exception-breakpoint based on a SystemTap probe. */
5655 insert_exception_resume_from_probe (struct thread_info *tp,
5656 const struct probe *probe,
5657 struct frame_info *frame)
5659 struct value *arg_value;
5661 struct breakpoint *bp;
5663 arg_value = probe_safe_evaluate_at_pc (frame, 1);
5667 handler = value_as_address (arg_value);
5670 fprintf_unfiltered (gdb_stdlog,
5671 "infrun: exception resume at %s\n",
5672 paddress (get_objfile_arch (probe->objfile),
5675 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
5676 handler, bp_exception_resume);
5677 bp->thread = tp->num;
5678 inferior_thread ()->control.exception_resume_breakpoint = bp;
5681 /* This is called when an exception has been intercepted. Check to
5682 see whether the exception's destination is of interest, and if so,
5683 set an exception resume breakpoint there. */
5686 check_exception_resume (struct execution_control_state *ecs,
5687 struct frame_info *frame)
5689 volatile struct gdb_exception e;
5690 const struct probe *probe;
5691 struct symbol *func;
5693 /* First see if this exception unwinding breakpoint was set via a
5694 SystemTap probe point. If so, the probe has two arguments: the
5695 CFA and the HANDLER. We ignore the CFA, extract the handler, and
5696 set a breakpoint there. */
5697 probe = find_probe_by_pc (get_frame_pc (frame));
5700 insert_exception_resume_from_probe (ecs->event_thread, probe, frame);
5704 func = get_frame_function (frame);
5708 TRY_CATCH (e, RETURN_MASK_ERROR)
5711 struct block_iterator iter;
5715 /* The exception breakpoint is a thread-specific breakpoint on
5716 the unwinder's debug hook, declared as:
5718 void _Unwind_DebugHook (void *cfa, void *handler);
5720 The CFA argument indicates the frame to which control is
5721 about to be transferred. HANDLER is the destination PC.
5723 We ignore the CFA and set a temporary breakpoint at HANDLER.
5724 This is not extremely efficient but it avoids issues in gdb
5725 with computing the DWARF CFA, and it also works even in weird
5726 cases such as throwing an exception from inside a signal
5729 b = SYMBOL_BLOCK_VALUE (func);
5730 ALL_BLOCK_SYMBOLS (b, iter, sym)
5732 if (!SYMBOL_IS_ARGUMENT (sym))
5739 insert_exception_resume_breakpoint (ecs->event_thread,
5748 stop_stepping (struct execution_control_state *ecs)
5751 fprintf_unfiltered (gdb_stdlog, "infrun: stop_stepping\n");
5753 /* Let callers know we don't want to wait for the inferior anymore. */
5754 ecs->wait_some_more = 0;
5757 /* Called when we should continue running the inferior, because the
5758 current event doesn't cause a user visible stop. This does the
5759 resuming part; waiting for the next event is done elsewhere. */
5762 keep_going (struct execution_control_state *ecs)
5764 /* Make sure normal_stop is called if we get a QUIT handled before
5766 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
5768 /* Save the pc before execution, to compare with pc after stop. */
5769 ecs->event_thread->prev_pc
5770 = regcache_read_pc (get_thread_regcache (ecs->ptid));
5772 if (ecs->event_thread->control.trap_expected
5773 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
5775 /* We haven't yet gotten our trap, and either: intercepted a
5776 non-signal event (e.g., a fork); or took a signal which we
5777 are supposed to pass through to the inferior. Simply
5779 discard_cleanups (old_cleanups);
5780 resume (currently_stepping (ecs->event_thread),
5781 ecs->event_thread->suspend.stop_signal);
5785 /* Either the trap was not expected, but we are continuing
5786 anyway (if we got a signal, the user asked it be passed to
5789 We got our expected trap, but decided we should resume from
5792 We're going to run this baby now!
5794 Note that insert_breakpoints won't try to re-insert
5795 already inserted breakpoints. Therefore, we don't
5796 care if breakpoints were already inserted, or not. */
5798 if (ecs->event_thread->stepping_over_breakpoint)
5800 struct regcache *thread_regcache = get_thread_regcache (ecs->ptid);
5802 if (!use_displaced_stepping (get_regcache_arch (thread_regcache)))
5804 /* Since we can't do a displaced step, we have to remove
5805 the breakpoint while we step it. To keep things
5806 simple, we remove them all. */
5807 remove_breakpoints ();
5812 volatile struct gdb_exception e;
5814 /* Stop stepping if inserting breakpoints fails. */
5815 TRY_CATCH (e, RETURN_MASK_ERROR)
5817 insert_breakpoints ();
5821 exception_print (gdb_stderr, e);
5822 stop_stepping (ecs);
5827 ecs->event_thread->control.trap_expected
5828 = ecs->event_thread->stepping_over_breakpoint;
5830 /* Do not deliver GDB_SIGNAL_TRAP (except when the user
5831 explicitly specifies that such a signal should be delivered
5832 to the target program). Typically, that would occur when a
5833 user is debugging a target monitor on a simulator: the target
5834 monitor sets a breakpoint; the simulator encounters this
5835 breakpoint and halts the simulation handing control to GDB;
5836 GDB, noting that the stop address doesn't map to any known
5837 breakpoint, returns control back to the simulator; the
5838 simulator then delivers the hardware equivalent of a
5839 GDB_SIGNAL_TRAP to the program being debugged. */
5840 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
5841 && !signal_program[ecs->event_thread->suspend.stop_signal])
5842 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
5844 discard_cleanups (old_cleanups);
5845 resume (currently_stepping (ecs->event_thread),
5846 ecs->event_thread->suspend.stop_signal);
5849 prepare_to_wait (ecs);
5852 /* This function normally comes after a resume, before
5853 handle_inferior_event exits. It takes care of any last bits of
5854 housekeeping, and sets the all-important wait_some_more flag. */
5857 prepare_to_wait (struct execution_control_state *ecs)
5860 fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
5862 /* This is the old end of the while loop. Let everybody know we
5863 want to wait for the inferior some more and get called again
5865 ecs->wait_some_more = 1;
5868 /* Several print_*_reason functions to print why the inferior has stopped.
5869 We always print something when the inferior exits, or receives a signal.
5870 The rest of the cases are dealt with later on in normal_stop and
5871 print_it_typical. Ideally there should be a call to one of these
5872 print_*_reason functions functions from handle_inferior_event each time
5873 stop_stepping is called. */
5875 /* Print why the inferior has stopped.
5876 We are done with a step/next/si/ni command, print why the inferior has
5877 stopped. For now print nothing. Print a message only if not in the middle
5878 of doing a "step n" operation for n > 1. */
5881 print_end_stepping_range_reason (void)
5883 if ((!inferior_thread ()->step_multi
5884 || !inferior_thread ()->control.stop_step)
5885 && ui_out_is_mi_like_p (current_uiout))
5886 ui_out_field_string (current_uiout, "reason",
5887 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
5890 /* The inferior was terminated by a signal, print why it stopped. */
5893 print_signal_exited_reason (enum gdb_signal siggnal)
5895 struct ui_out *uiout = current_uiout;
5897 annotate_signalled ();
5898 if (ui_out_is_mi_like_p (uiout))
5900 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
5901 ui_out_text (uiout, "\nProgram terminated with signal ");
5902 annotate_signal_name ();
5903 ui_out_field_string (uiout, "signal-name",
5904 gdb_signal_to_name (siggnal));
5905 annotate_signal_name_end ();
5906 ui_out_text (uiout, ", ");
5907 annotate_signal_string ();
5908 ui_out_field_string (uiout, "signal-meaning",
5909 gdb_signal_to_string (siggnal));
5910 annotate_signal_string_end ();
5911 ui_out_text (uiout, ".\n");
5912 ui_out_text (uiout, "The program no longer exists.\n");
5915 /* The inferior program is finished, print why it stopped. */
5918 print_exited_reason (int exitstatus)
5920 struct inferior *inf = current_inferior ();
5921 const char *pidstr = target_pid_to_str (pid_to_ptid (inf->pid));
5922 struct ui_out *uiout = current_uiout;
5924 annotate_exited (exitstatus);
5927 if (ui_out_is_mi_like_p (uiout))
5928 ui_out_field_string (uiout, "reason",
5929 async_reason_lookup (EXEC_ASYNC_EXITED));
5930 ui_out_text (uiout, "[Inferior ");
5931 ui_out_text (uiout, plongest (inf->num));
5932 ui_out_text (uiout, " (");
5933 ui_out_text (uiout, pidstr);
5934 ui_out_text (uiout, ") exited with code ");
5935 ui_out_field_fmt (uiout, "exit-code", "0%o", (unsigned int) exitstatus);
5936 ui_out_text (uiout, "]\n");
5940 if (ui_out_is_mi_like_p (uiout))
5942 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
5943 ui_out_text (uiout, "[Inferior ");
5944 ui_out_text (uiout, plongest (inf->num));
5945 ui_out_text (uiout, " (");
5946 ui_out_text (uiout, pidstr);
5947 ui_out_text (uiout, ") exited normally]\n");
5949 /* Support the --return-child-result option. */
5950 return_child_result_value = exitstatus;
5953 /* Signal received, print why the inferior has stopped. The signal table
5954 tells us to print about it. */
5957 print_signal_received_reason (enum gdb_signal siggnal)
5959 struct ui_out *uiout = current_uiout;
5963 if (siggnal == GDB_SIGNAL_0 && !ui_out_is_mi_like_p (uiout))
5965 struct thread_info *t = inferior_thread ();
5967 ui_out_text (uiout, "\n[");
5968 ui_out_field_string (uiout, "thread-name",
5969 target_pid_to_str (t->ptid));
5970 ui_out_field_fmt (uiout, "thread-id", "] #%d", t->num);
5971 ui_out_text (uiout, " stopped");
5975 ui_out_text (uiout, "\nProgram received signal ");
5976 annotate_signal_name ();
5977 if (ui_out_is_mi_like_p (uiout))
5979 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
5980 ui_out_field_string (uiout, "signal-name",
5981 gdb_signal_to_name (siggnal));
5982 annotate_signal_name_end ();
5983 ui_out_text (uiout, ", ");
5984 annotate_signal_string ();
5985 ui_out_field_string (uiout, "signal-meaning",
5986 gdb_signal_to_string (siggnal));
5987 annotate_signal_string_end ();
5989 ui_out_text (uiout, ".\n");
5992 /* Reverse execution: target ran out of history info, print why the inferior
5996 print_no_history_reason (void)
5998 ui_out_text (current_uiout, "\nNo more reverse-execution history.\n");
6001 /* Here to return control to GDB when the inferior stops for real.
6002 Print appropriate messages, remove breakpoints, give terminal our modes.
6004 STOP_PRINT_FRAME nonzero means print the executing frame
6005 (pc, function, args, file, line number and line text).
6006 BREAKPOINTS_FAILED nonzero means stop was due to error
6007 attempting to insert breakpoints. */
6012 struct target_waitstatus last;
6014 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
6016 get_last_target_status (&last_ptid, &last);
6018 /* If an exception is thrown from this point on, make sure to
6019 propagate GDB's knowledge of the executing state to the
6020 frontend/user running state. A QUIT is an easy exception to see
6021 here, so do this before any filtered output. */
6023 make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
6024 else if (last.kind != TARGET_WAITKIND_SIGNALLED
6025 && last.kind != TARGET_WAITKIND_EXITED
6026 && last.kind != TARGET_WAITKIND_NO_RESUMED)
6027 make_cleanup (finish_thread_state_cleanup, &inferior_ptid);
6029 /* In non-stop mode, we don't want GDB to switch threads behind the
6030 user's back, to avoid races where the user is typing a command to
6031 apply to thread x, but GDB switches to thread y before the user
6032 finishes entering the command. */
6034 /* As with the notification of thread events, we want to delay
6035 notifying the user that we've switched thread context until
6036 the inferior actually stops.
6038 There's no point in saying anything if the inferior has exited.
6039 Note that SIGNALLED here means "exited with a signal", not
6040 "received a signal". */
6042 && !ptid_equal (previous_inferior_ptid, inferior_ptid)
6043 && target_has_execution
6044 && last.kind != TARGET_WAITKIND_SIGNALLED
6045 && last.kind != TARGET_WAITKIND_EXITED
6046 && last.kind != TARGET_WAITKIND_NO_RESUMED)
6048 target_terminal_ours_for_output ();
6049 printf_filtered (_("[Switching to %s]\n"),
6050 target_pid_to_str (inferior_ptid));
6051 annotate_thread_changed ();
6052 previous_inferior_ptid = inferior_ptid;
6055 if (last.kind == TARGET_WAITKIND_NO_RESUMED)
6057 gdb_assert (sync_execution || !target_can_async_p ());
6059 target_terminal_ours_for_output ();
6060 printf_filtered (_("No unwaited-for children left.\n"));
6063 if (!breakpoints_always_inserted_mode () && target_has_execution)
6065 if (remove_breakpoints ())
6067 target_terminal_ours_for_output ();
6068 printf_filtered (_("Cannot remove breakpoints because "
6069 "program is no longer writable.\nFurther "
6070 "execution is probably impossible.\n"));
6074 /* If an auto-display called a function and that got a signal,
6075 delete that auto-display to avoid an infinite recursion. */
6077 if (stopped_by_random_signal)
6078 disable_current_display ();
6080 /* Don't print a message if in the middle of doing a "step n"
6081 operation for n > 1 */
6082 if (target_has_execution
6083 && last.kind != TARGET_WAITKIND_SIGNALLED
6084 && last.kind != TARGET_WAITKIND_EXITED
6085 && inferior_thread ()->step_multi
6086 && inferior_thread ()->control.stop_step)
6089 target_terminal_ours ();
6090 async_enable_stdin ();
6092 /* Set the current source location. This will also happen if we
6093 display the frame below, but the current SAL will be incorrect
6094 during a user hook-stop function. */
6095 if (has_stack_frames () && !stop_stack_dummy)
6096 set_current_sal_from_frame (get_current_frame (), 1);
6098 /* Let the user/frontend see the threads as stopped. */
6099 do_cleanups (old_chain);
6101 /* Look up the hook_stop and run it (CLI internally handles problem
6102 of stop_command's pre-hook not existing). */
6104 catch_errors (hook_stop_stub, stop_command,
6105 "Error while running hook_stop:\n", RETURN_MASK_ALL);
6107 if (!has_stack_frames ())
6110 if (last.kind == TARGET_WAITKIND_SIGNALLED
6111 || last.kind == TARGET_WAITKIND_EXITED)
6114 /* Select innermost stack frame - i.e., current frame is frame 0,
6115 and current location is based on that.
6116 Don't do this on return from a stack dummy routine,
6117 or if the program has exited. */
6119 if (!stop_stack_dummy)
6121 select_frame (get_current_frame ());
6123 /* Print current location without a level number, if
6124 we have changed functions or hit a breakpoint.
6125 Print source line if we have one.
6126 bpstat_print() contains the logic deciding in detail
6127 what to print, based on the event(s) that just occurred. */
6129 /* If --batch-silent is enabled then there's no need to print the current
6130 source location, and to try risks causing an error message about
6131 missing source files. */
6132 if (stop_print_frame && !batch_silent)
6136 int do_frame_printing = 1;
6137 struct thread_info *tp = inferior_thread ();
6139 bpstat_ret = bpstat_print (tp->control.stop_bpstat, last.kind);
6143 /* FIXME: cagney/2002-12-01: Given that a frame ID does
6144 (or should) carry around the function and does (or
6145 should) use that when doing a frame comparison. */
6146 if (tp->control.stop_step
6147 && frame_id_eq (tp->control.step_frame_id,
6148 get_frame_id (get_current_frame ()))
6149 && step_start_function == find_pc_function (stop_pc))
6150 source_flag = SRC_LINE; /* Finished step, just
6151 print source line. */
6153 source_flag = SRC_AND_LOC; /* Print location and
6156 case PRINT_SRC_AND_LOC:
6157 source_flag = SRC_AND_LOC; /* Print location and
6160 case PRINT_SRC_ONLY:
6161 source_flag = SRC_LINE;
6164 source_flag = SRC_LINE; /* something bogus */
6165 do_frame_printing = 0;
6168 internal_error (__FILE__, __LINE__, _("Unknown value."));
6171 /* The behavior of this routine with respect to the source
6173 SRC_LINE: Print only source line
6174 LOCATION: Print only location
6175 SRC_AND_LOC: Print location and source line. */
6176 if (do_frame_printing)
6177 print_stack_frame (get_selected_frame (NULL), 0, source_flag, 1);
6179 /* Display the auto-display expressions. */
6184 /* Save the function value return registers, if we care.
6185 We might be about to restore their previous contents. */
6186 if (inferior_thread ()->control.proceed_to_finish
6187 && execution_direction != EXEC_REVERSE)
6189 /* This should not be necessary. */
6191 regcache_xfree (stop_registers);
6193 /* NB: The copy goes through to the target picking up the value of
6194 all the registers. */
6195 stop_registers = regcache_dup (get_current_regcache ());
6198 if (stop_stack_dummy == STOP_STACK_DUMMY)
6200 /* Pop the empty frame that contains the stack dummy.
6201 This also restores inferior state prior to the call
6202 (struct infcall_suspend_state). */
6203 struct frame_info *frame = get_current_frame ();
6205 gdb_assert (get_frame_type (frame) == DUMMY_FRAME);
6207 /* frame_pop() calls reinit_frame_cache as the last thing it
6208 does which means there's currently no selected frame. We
6209 don't need to re-establish a selected frame if the dummy call
6210 returns normally, that will be done by
6211 restore_infcall_control_state. However, we do have to handle
6212 the case where the dummy call is returning after being
6213 stopped (e.g. the dummy call previously hit a breakpoint).
6214 We can't know which case we have so just always re-establish
6215 a selected frame here. */
6216 select_frame (get_current_frame ());
6220 annotate_stopped ();
6222 /* Suppress the stop observer if we're in the middle of:
6224 - a step n (n > 1), as there still more steps to be done.
6226 - a "finish" command, as the observer will be called in
6227 finish_command_continuation, so it can include the inferior
6228 function's return value.
6230 - calling an inferior function, as we pretend we inferior didn't
6231 run at all. The return value of the call is handled by the
6232 expression evaluator, through call_function_by_hand. */
6234 if (!target_has_execution
6235 || last.kind == TARGET_WAITKIND_SIGNALLED
6236 || last.kind == TARGET_WAITKIND_EXITED
6237 || last.kind == TARGET_WAITKIND_NO_RESUMED
6238 || (!(inferior_thread ()->step_multi
6239 && inferior_thread ()->control.stop_step)
6240 && !(inferior_thread ()->control.stop_bpstat
6241 && inferior_thread ()->control.proceed_to_finish)
6242 && !inferior_thread ()->control.in_infcall))
6244 if (!ptid_equal (inferior_ptid, null_ptid))
6245 observer_notify_normal_stop (inferior_thread ()->control.stop_bpstat,
6248 observer_notify_normal_stop (NULL, stop_print_frame);
6251 if (target_has_execution)
6253 if (last.kind != TARGET_WAITKIND_SIGNALLED
6254 && last.kind != TARGET_WAITKIND_EXITED)
6255 /* Delete the breakpoint we stopped at, if it wants to be deleted.
6256 Delete any breakpoint that is to be deleted at the next stop. */
6257 breakpoint_auto_delete (inferior_thread ()->control.stop_bpstat);
6260 /* Try to get rid of automatically added inferiors that are no
6261 longer needed. Keeping those around slows down things linearly.
6262 Note that this never removes the current inferior. */
6267 hook_stop_stub (void *cmd)
6269 execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
6274 signal_stop_state (int signo)
6276 return signal_stop[signo];
6280 signal_print_state (int signo)
6282 return signal_print[signo];
6286 signal_pass_state (int signo)
6288 return signal_program[signo];
6292 signal_cache_update (int signo)
6296 for (signo = 0; signo < (int) GDB_SIGNAL_LAST; signo++)
6297 signal_cache_update (signo);
6302 signal_pass[signo] = (signal_stop[signo] == 0
6303 && signal_print[signo] == 0
6304 && signal_program[signo] == 1
6305 && signal_catch[signo] == 0);
6309 signal_stop_update (int signo, int state)
6311 int ret = signal_stop[signo];
6313 signal_stop[signo] = state;
6314 signal_cache_update (signo);
6319 signal_print_update (int signo, int state)
6321 int ret = signal_print[signo];
6323 signal_print[signo] = state;
6324 signal_cache_update (signo);
6329 signal_pass_update (int signo, int state)
6331 int ret = signal_program[signo];
6333 signal_program[signo] = state;
6334 signal_cache_update (signo);
6338 /* Update the global 'signal_catch' from INFO and notify the
6342 signal_catch_update (const unsigned int *info)
6346 for (i = 0; i < GDB_SIGNAL_LAST; ++i)
6347 signal_catch[i] = info[i] > 0;
6348 signal_cache_update (-1);
6349 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
6353 sig_print_header (void)
6355 printf_filtered (_("Signal Stop\tPrint\tPass "
6356 "to program\tDescription\n"));
6360 sig_print_info (enum gdb_signal oursig)
6362 const char *name = gdb_signal_to_name (oursig);
6363 int name_padding = 13 - strlen (name);
6365 if (name_padding <= 0)
6368 printf_filtered ("%s", name);
6369 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
6370 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
6371 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
6372 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
6373 printf_filtered ("%s\n", gdb_signal_to_string (oursig));
6376 /* Specify how various signals in the inferior should be handled. */
6379 handle_command (char *args, int from_tty)
6382 int digits, wordlen;
6383 int sigfirst, signum, siglast;
6384 enum gdb_signal oursig;
6387 unsigned char *sigs;
6388 struct cleanup *old_chain;
6392 error_no_arg (_("signal to handle"));
6395 /* Allocate and zero an array of flags for which signals to handle. */
6397 nsigs = (int) GDB_SIGNAL_LAST;
6398 sigs = (unsigned char *) alloca (nsigs);
6399 memset (sigs, 0, nsigs);
6401 /* Break the command line up into args. */
6403 argv = gdb_buildargv (args);
6404 old_chain = make_cleanup_freeargv (argv);
6406 /* Walk through the args, looking for signal oursigs, signal names, and
6407 actions. Signal numbers and signal names may be interspersed with
6408 actions, with the actions being performed for all signals cumulatively
6409 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
6411 while (*argv != NULL)
6413 wordlen = strlen (*argv);
6414 for (digits = 0; isdigit ((*argv)[digits]); digits++)
6418 sigfirst = siglast = -1;
6420 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
6422 /* Apply action to all signals except those used by the
6423 debugger. Silently skip those. */
6426 siglast = nsigs - 1;
6428 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
6430 SET_SIGS (nsigs, sigs, signal_stop);
6431 SET_SIGS (nsigs, sigs, signal_print);
6433 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
6435 UNSET_SIGS (nsigs, sigs, signal_program);
6437 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
6439 SET_SIGS (nsigs, sigs, signal_print);
6441 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
6443 SET_SIGS (nsigs, sigs, signal_program);
6445 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
6447 UNSET_SIGS (nsigs, sigs, signal_stop);
6449 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
6451 SET_SIGS (nsigs, sigs, signal_program);
6453 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
6455 UNSET_SIGS (nsigs, sigs, signal_print);
6456 UNSET_SIGS (nsigs, sigs, signal_stop);
6458 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
6460 UNSET_SIGS (nsigs, sigs, signal_program);
6462 else if (digits > 0)
6464 /* It is numeric. The numeric signal refers to our own
6465 internal signal numbering from target.h, not to host/target
6466 signal number. This is a feature; users really should be
6467 using symbolic names anyway, and the common ones like
6468 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
6470 sigfirst = siglast = (int)
6471 gdb_signal_from_command (atoi (*argv));
6472 if ((*argv)[digits] == '-')
6475 gdb_signal_from_command (atoi ((*argv) + digits + 1));
6477 if (sigfirst > siglast)
6479 /* Bet he didn't figure we'd think of this case... */
6487 oursig = gdb_signal_from_name (*argv);
6488 if (oursig != GDB_SIGNAL_UNKNOWN)
6490 sigfirst = siglast = (int) oursig;
6494 /* Not a number and not a recognized flag word => complain. */
6495 error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
6499 /* If any signal numbers or symbol names were found, set flags for
6500 which signals to apply actions to. */
6502 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
6504 switch ((enum gdb_signal) signum)
6506 case GDB_SIGNAL_TRAP:
6507 case GDB_SIGNAL_INT:
6508 if (!allsigs && !sigs[signum])
6510 if (query (_("%s is used by the debugger.\n\
6511 Are you sure you want to change it? "),
6512 gdb_signal_to_name ((enum gdb_signal) signum)))
6518 printf_unfiltered (_("Not confirmed, unchanged.\n"));
6519 gdb_flush (gdb_stdout);
6524 case GDB_SIGNAL_DEFAULT:
6525 case GDB_SIGNAL_UNKNOWN:
6526 /* Make sure that "all" doesn't print these. */
6537 for (signum = 0; signum < nsigs; signum++)
6540 signal_cache_update (-1);
6541 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
6542 target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
6546 /* Show the results. */
6547 sig_print_header ();
6548 for (; signum < nsigs; signum++)
6550 sig_print_info (signum);
6556 do_cleanups (old_chain);
6559 /* Complete the "handle" command. */
6561 static VEC (char_ptr) *
6562 handle_completer (struct cmd_list_element *ignore,
6563 const char *text, const char *word)
6565 VEC (char_ptr) *vec_signals, *vec_keywords, *return_val;
6566 static const char * const keywords[] =
6580 vec_signals = signal_completer (ignore, text, word);
6581 vec_keywords = complete_on_enum (keywords, word, word);
6583 return_val = VEC_merge (char_ptr, vec_signals, vec_keywords);
6584 VEC_free (char_ptr, vec_signals);
6585 VEC_free (char_ptr, vec_keywords);
6590 xdb_handle_command (char *args, int from_tty)
6593 struct cleanup *old_chain;
6596 error_no_arg (_("xdb command"));
6598 /* Break the command line up into args. */
6600 argv = gdb_buildargv (args);
6601 old_chain = make_cleanup_freeargv (argv);
6602 if (argv[1] != (char *) NULL)
6607 bufLen = strlen (argv[0]) + 20;
6608 argBuf = (char *) xmalloc (bufLen);
6612 enum gdb_signal oursig;
6614 oursig = gdb_signal_from_name (argv[0]);
6615 memset (argBuf, 0, bufLen);
6616 if (strcmp (argv[1], "Q") == 0)
6617 sprintf (argBuf, "%s %s", argv[0], "noprint");
6620 if (strcmp (argv[1], "s") == 0)
6622 if (!signal_stop[oursig])
6623 sprintf (argBuf, "%s %s", argv[0], "stop");
6625 sprintf (argBuf, "%s %s", argv[0], "nostop");
6627 else if (strcmp (argv[1], "i") == 0)
6629 if (!signal_program[oursig])
6630 sprintf (argBuf, "%s %s", argv[0], "pass");
6632 sprintf (argBuf, "%s %s", argv[0], "nopass");
6634 else if (strcmp (argv[1], "r") == 0)
6636 if (!signal_print[oursig])
6637 sprintf (argBuf, "%s %s", argv[0], "print");
6639 sprintf (argBuf, "%s %s", argv[0], "noprint");
6645 handle_command (argBuf, from_tty);
6647 printf_filtered (_("Invalid signal handling flag.\n"));
6652 do_cleanups (old_chain);
6656 gdb_signal_from_command (int num)
6658 if (num >= 1 && num <= 15)
6659 return (enum gdb_signal) num;
6660 error (_("Only signals 1-15 are valid as numeric signals.\n\
6661 Use \"info signals\" for a list of symbolic signals."));
6664 /* Print current contents of the tables set by the handle command.
6665 It is possible we should just be printing signals actually used
6666 by the current target (but for things to work right when switching
6667 targets, all signals should be in the signal tables). */
6670 signals_info (char *signum_exp, int from_tty)
6672 enum gdb_signal oursig;
6674 sig_print_header ();
6678 /* First see if this is a symbol name. */
6679 oursig = gdb_signal_from_name (signum_exp);
6680 if (oursig == GDB_SIGNAL_UNKNOWN)
6682 /* No, try numeric. */
6684 gdb_signal_from_command (parse_and_eval_long (signum_exp));
6686 sig_print_info (oursig);
6690 printf_filtered ("\n");
6691 /* These ugly casts brought to you by the native VAX compiler. */
6692 for (oursig = GDB_SIGNAL_FIRST;
6693 (int) oursig < (int) GDB_SIGNAL_LAST;
6694 oursig = (enum gdb_signal) ((int) oursig + 1))
6698 if (oursig != GDB_SIGNAL_UNKNOWN
6699 && oursig != GDB_SIGNAL_DEFAULT && oursig != GDB_SIGNAL_0)
6700 sig_print_info (oursig);
6703 printf_filtered (_("\nUse the \"handle\" command "
6704 "to change these tables.\n"));
6707 /* Check if it makes sense to read $_siginfo from the current thread
6708 at this point. If not, throw an error. */
6711 validate_siginfo_access (void)
6713 /* No current inferior, no siginfo. */
6714 if (ptid_equal (inferior_ptid, null_ptid))
6715 error (_("No thread selected."));
6717 /* Don't try to read from a dead thread. */
6718 if (is_exited (inferior_ptid))
6719 error (_("The current thread has terminated"));
6721 /* ... or from a spinning thread. */
6722 if (is_running (inferior_ptid))
6723 error (_("Selected thread is running."));
6726 /* The $_siginfo convenience variable is a bit special. We don't know
6727 for sure the type of the value until we actually have a chance to
6728 fetch the data. The type can change depending on gdbarch, so it is
6729 also dependent on which thread you have selected.
6731 1. making $_siginfo be an internalvar that creates a new value on
6734 2. making the value of $_siginfo be an lval_computed value. */
6736 /* This function implements the lval_computed support for reading a
6740 siginfo_value_read (struct value *v)
6742 LONGEST transferred;
6744 validate_siginfo_access ();
6747 target_read (¤t_target, TARGET_OBJECT_SIGNAL_INFO,
6749 value_contents_all_raw (v),
6751 TYPE_LENGTH (value_type (v)));
6753 if (transferred != TYPE_LENGTH (value_type (v)))
6754 error (_("Unable to read siginfo"));
6757 /* This function implements the lval_computed support for writing a
6761 siginfo_value_write (struct value *v, struct value *fromval)
6763 LONGEST transferred;
6765 validate_siginfo_access ();
6767 transferred = target_write (¤t_target,
6768 TARGET_OBJECT_SIGNAL_INFO,
6770 value_contents_all_raw (fromval),
6772 TYPE_LENGTH (value_type (fromval)));
6774 if (transferred != TYPE_LENGTH (value_type (fromval)))
6775 error (_("Unable to write siginfo"));
6778 static const struct lval_funcs siginfo_value_funcs =
6784 /* Return a new value with the correct type for the siginfo object of
6785 the current thread using architecture GDBARCH. Return a void value
6786 if there's no object available. */
6788 static struct value *
6789 siginfo_make_value (struct gdbarch *gdbarch, struct internalvar *var,
6792 if (target_has_stack
6793 && !ptid_equal (inferior_ptid, null_ptid)
6794 && gdbarch_get_siginfo_type_p (gdbarch))
6796 struct type *type = gdbarch_get_siginfo_type (gdbarch);
6798 return allocate_computed_value (type, &siginfo_value_funcs, NULL);
6801 return allocate_value (builtin_type (gdbarch)->builtin_void);
6805 /* infcall_suspend_state contains state about the program itself like its
6806 registers and any signal it received when it last stopped.
6807 This state must be restored regardless of how the inferior function call
6808 ends (either successfully, or after it hits a breakpoint or signal)
6809 if the program is to properly continue where it left off. */
6811 struct infcall_suspend_state
6813 struct thread_suspend_state thread_suspend;
6814 #if 0 /* Currently unused and empty structures are not valid C. */
6815 struct inferior_suspend_state inferior_suspend;
6820 struct regcache *registers;
6822 /* Format of SIGINFO_DATA or NULL if it is not present. */
6823 struct gdbarch *siginfo_gdbarch;
6825 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
6826 TYPE_LENGTH (gdbarch_get_siginfo_type ()). For different gdbarch the
6827 content would be invalid. */
6828 gdb_byte *siginfo_data;
6831 struct infcall_suspend_state *
6832 save_infcall_suspend_state (void)
6834 struct infcall_suspend_state *inf_state;
6835 struct thread_info *tp = inferior_thread ();
6837 struct inferior *inf = current_inferior ();
6839 struct regcache *regcache = get_current_regcache ();
6840 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6841 gdb_byte *siginfo_data = NULL;
6843 if (gdbarch_get_siginfo_type_p (gdbarch))
6845 struct type *type = gdbarch_get_siginfo_type (gdbarch);
6846 size_t len = TYPE_LENGTH (type);
6847 struct cleanup *back_to;
6849 siginfo_data = xmalloc (len);
6850 back_to = make_cleanup (xfree, siginfo_data);
6852 if (target_read (¤t_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6853 siginfo_data, 0, len) == len)
6854 discard_cleanups (back_to);
6857 /* Errors ignored. */
6858 do_cleanups (back_to);
6859 siginfo_data = NULL;
6863 inf_state = XCNEW (struct infcall_suspend_state);
6867 inf_state->siginfo_gdbarch = gdbarch;
6868 inf_state->siginfo_data = siginfo_data;
6871 inf_state->thread_suspend = tp->suspend;
6872 #if 0 /* Currently unused and empty structures are not valid C. */
6873 inf_state->inferior_suspend = inf->suspend;
6876 /* run_inferior_call will not use the signal due to its `proceed' call with
6877 GDB_SIGNAL_0 anyway. */
6878 tp->suspend.stop_signal = GDB_SIGNAL_0;
6880 inf_state->stop_pc = stop_pc;
6882 inf_state->registers = regcache_dup (regcache);
6887 /* Restore inferior session state to INF_STATE. */
6890 restore_infcall_suspend_state (struct infcall_suspend_state *inf_state)
6892 struct thread_info *tp = inferior_thread ();
6894 struct inferior *inf = current_inferior ();
6896 struct regcache *regcache = get_current_regcache ();
6897 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6899 tp->suspend = inf_state->thread_suspend;
6900 #if 0 /* Currently unused and empty structures are not valid C. */
6901 inf->suspend = inf_state->inferior_suspend;
6904 stop_pc = inf_state->stop_pc;
6906 if (inf_state->siginfo_gdbarch == gdbarch)
6908 struct type *type = gdbarch_get_siginfo_type (gdbarch);
6910 /* Errors ignored. */
6911 target_write (¤t_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6912 inf_state->siginfo_data, 0, TYPE_LENGTH (type));
6915 /* The inferior can be gone if the user types "print exit(0)"
6916 (and perhaps other times). */
6917 if (target_has_execution)
6918 /* NB: The register write goes through to the target. */
6919 regcache_cpy (regcache, inf_state->registers);
6921 discard_infcall_suspend_state (inf_state);
6925 do_restore_infcall_suspend_state_cleanup (void *state)
6927 restore_infcall_suspend_state (state);
6931 make_cleanup_restore_infcall_suspend_state
6932 (struct infcall_suspend_state *inf_state)
6934 return make_cleanup (do_restore_infcall_suspend_state_cleanup, inf_state);
6938 discard_infcall_suspend_state (struct infcall_suspend_state *inf_state)
6940 regcache_xfree (inf_state->registers);
6941 xfree (inf_state->siginfo_data);
6946 get_infcall_suspend_state_regcache (struct infcall_suspend_state *inf_state)
6948 return inf_state->registers;
6951 /* infcall_control_state contains state regarding gdb's control of the
6952 inferior itself like stepping control. It also contains session state like
6953 the user's currently selected frame. */
6955 struct infcall_control_state
6957 struct thread_control_state thread_control;
6958 struct inferior_control_state inferior_control;
6961 enum stop_stack_kind stop_stack_dummy;
6962 int stopped_by_random_signal;
6963 int stop_after_trap;
6965 /* ID if the selected frame when the inferior function call was made. */
6966 struct frame_id selected_frame_id;
6969 /* Save all of the information associated with the inferior<==>gdb
6972 struct infcall_control_state *
6973 save_infcall_control_state (void)
6975 struct infcall_control_state *inf_status = xmalloc (sizeof (*inf_status));
6976 struct thread_info *tp = inferior_thread ();
6977 struct inferior *inf = current_inferior ();
6979 inf_status->thread_control = tp->control;
6980 inf_status->inferior_control = inf->control;
6982 tp->control.step_resume_breakpoint = NULL;
6983 tp->control.exception_resume_breakpoint = NULL;
6985 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
6986 chain. If caller's caller is walking the chain, they'll be happier if we
6987 hand them back the original chain when restore_infcall_control_state is
6989 tp->control.stop_bpstat = bpstat_copy (tp->control.stop_bpstat);
6992 inf_status->stop_stack_dummy = stop_stack_dummy;
6993 inf_status->stopped_by_random_signal = stopped_by_random_signal;
6994 inf_status->stop_after_trap = stop_after_trap;
6996 inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
7002 restore_selected_frame (void *args)
7004 struct frame_id *fid = (struct frame_id *) args;
7005 struct frame_info *frame;
7007 frame = frame_find_by_id (*fid);
7009 /* If inf_status->selected_frame_id is NULL, there was no previously
7013 warning (_("Unable to restore previously selected frame."));
7017 select_frame (frame);
7022 /* Restore inferior session state to INF_STATUS. */
7025 restore_infcall_control_state (struct infcall_control_state *inf_status)
7027 struct thread_info *tp = inferior_thread ();
7028 struct inferior *inf = current_inferior ();
7030 if (tp->control.step_resume_breakpoint)
7031 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
7033 if (tp->control.exception_resume_breakpoint)
7034 tp->control.exception_resume_breakpoint->disposition
7035 = disp_del_at_next_stop;
7037 /* Handle the bpstat_copy of the chain. */
7038 bpstat_clear (&tp->control.stop_bpstat);
7040 tp->control = inf_status->thread_control;
7041 inf->control = inf_status->inferior_control;
7044 stop_stack_dummy = inf_status->stop_stack_dummy;
7045 stopped_by_random_signal = inf_status->stopped_by_random_signal;
7046 stop_after_trap = inf_status->stop_after_trap;
7048 if (target_has_stack)
7050 /* The point of catch_errors is that if the stack is clobbered,
7051 walking the stack might encounter a garbage pointer and
7052 error() trying to dereference it. */
7054 (restore_selected_frame, &inf_status->selected_frame_id,
7055 "Unable to restore previously selected frame:\n",
7056 RETURN_MASK_ERROR) == 0)
7057 /* Error in restoring the selected frame. Select the innermost
7059 select_frame (get_current_frame ());
7066 do_restore_infcall_control_state_cleanup (void *sts)
7068 restore_infcall_control_state (sts);
7072 make_cleanup_restore_infcall_control_state
7073 (struct infcall_control_state *inf_status)
7075 return make_cleanup (do_restore_infcall_control_state_cleanup, inf_status);
7079 discard_infcall_control_state (struct infcall_control_state *inf_status)
7081 if (inf_status->thread_control.step_resume_breakpoint)
7082 inf_status->thread_control.step_resume_breakpoint->disposition
7083 = disp_del_at_next_stop;
7085 if (inf_status->thread_control.exception_resume_breakpoint)
7086 inf_status->thread_control.exception_resume_breakpoint->disposition
7087 = disp_del_at_next_stop;
7089 /* See save_infcall_control_state for info on stop_bpstat. */
7090 bpstat_clear (&inf_status->thread_control.stop_bpstat);
7096 ptid_match (ptid_t ptid, ptid_t filter)
7098 if (ptid_equal (filter, minus_one_ptid))
7100 if (ptid_is_pid (filter)
7101 && ptid_get_pid (ptid) == ptid_get_pid (filter))
7103 else if (ptid_equal (ptid, filter))
7109 /* restore_inferior_ptid() will be used by the cleanup machinery
7110 to restore the inferior_ptid value saved in a call to
7111 save_inferior_ptid(). */
7114 restore_inferior_ptid (void *arg)
7116 ptid_t *saved_ptid_ptr = arg;
7118 inferior_ptid = *saved_ptid_ptr;
7122 /* Save the value of inferior_ptid so that it may be restored by a
7123 later call to do_cleanups(). Returns the struct cleanup pointer
7124 needed for later doing the cleanup. */
7127 save_inferior_ptid (void)
7129 ptid_t *saved_ptid_ptr;
7131 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
7132 *saved_ptid_ptr = inferior_ptid;
7133 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
7136 /* See inferior.h. */
7139 clear_exit_convenience_vars (void)
7141 clear_internalvar (lookup_internalvar ("_exitsignal"));
7142 clear_internalvar (lookup_internalvar ("_exitcode"));
7146 /* User interface for reverse debugging:
7147 Set exec-direction / show exec-direction commands
7148 (returns error unless target implements to_set_exec_direction method). */
7150 int execution_direction = EXEC_FORWARD;
7151 static const char exec_forward[] = "forward";
7152 static const char exec_reverse[] = "reverse";
7153 static const char *exec_direction = exec_forward;
7154 static const char *const exec_direction_names[] = {
7161 set_exec_direction_func (char *args, int from_tty,
7162 struct cmd_list_element *cmd)
7164 if (target_can_execute_reverse)
7166 if (!strcmp (exec_direction, exec_forward))
7167 execution_direction = EXEC_FORWARD;
7168 else if (!strcmp (exec_direction, exec_reverse))
7169 execution_direction = EXEC_REVERSE;
7173 exec_direction = exec_forward;
7174 error (_("Target does not support this operation."));
7179 show_exec_direction_func (struct ui_file *out, int from_tty,
7180 struct cmd_list_element *cmd, const char *value)
7182 switch (execution_direction) {
7184 fprintf_filtered (out, _("Forward.\n"));
7187 fprintf_filtered (out, _("Reverse.\n"));
7190 internal_error (__FILE__, __LINE__,
7191 _("bogus execution_direction value: %d"),
7192 (int) execution_direction);
7197 show_schedule_multiple (struct ui_file *file, int from_tty,
7198 struct cmd_list_element *c, const char *value)
7200 fprintf_filtered (file, _("Resuming the execution of threads "
7201 "of all processes is %s.\n"), value);
7204 /* Implementation of `siginfo' variable. */
7206 static const struct internalvar_funcs siginfo_funcs =
7214 _initialize_infrun (void)
7218 struct cmd_list_element *c;
7220 add_info ("signals", signals_info, _("\
7221 What debugger does when program gets various signals.\n\
7222 Specify a signal as argument to print info on that signal only."));
7223 add_info_alias ("handle", "signals", 0);
7225 c = add_com ("handle", class_run, handle_command, _("\
7226 Specify how to handle signals.\n\
7227 Usage: handle SIGNAL [ACTIONS]\n\
7228 Args are signals and actions to apply to those signals.\n\
7229 If no actions are specified, the current settings for the specified signals\n\
7230 will be displayed instead.\n\
7232 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
7233 from 1-15 are allowed for compatibility with old versions of GDB.\n\
7234 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
7235 The special arg \"all\" is recognized to mean all signals except those\n\
7236 used by the debugger, typically SIGTRAP and SIGINT.\n\
7238 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
7239 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
7240 Stop means reenter debugger if this signal happens (implies print).\n\
7241 Print means print a message if this signal happens.\n\
7242 Pass means let program see this signal; otherwise program doesn't know.\n\
7243 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
7244 Pass and Stop may be combined.\n\
7246 Multiple signals may be specified. Signal numbers and signal names\n\
7247 may be interspersed with actions, with the actions being performed for\n\
7248 all signals cumulatively specified."));
7249 set_cmd_completer (c, handle_completer);
7253 add_com ("lz", class_info, signals_info, _("\
7254 What debugger does when program gets various signals.\n\
7255 Specify a signal as argument to print info on that signal only."));
7256 add_com ("z", class_run, xdb_handle_command, _("\
7257 Specify how to handle a signal.\n\
7258 Args are signals and actions to apply to those signals.\n\
7259 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
7260 from 1-15 are allowed for compatibility with old versions of GDB.\n\
7261 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
7262 The special arg \"all\" is recognized to mean all signals except those\n\
7263 used by the debugger, typically SIGTRAP and SIGINT.\n\
7264 Recognized actions include \"s\" (toggles between stop and nostop),\n\
7265 \"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
7266 nopass), \"Q\" (noprint)\n\
7267 Stop means reenter debugger if this signal happens (implies print).\n\
7268 Print means print a message if this signal happens.\n\
7269 Pass means let program see this signal; otherwise program doesn't know.\n\
7270 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
7271 Pass and Stop may be combined."));
7275 stop_command = add_cmd ("stop", class_obscure,
7276 not_just_help_class_command, _("\
7277 There is no `stop' command, but you can set a hook on `stop'.\n\
7278 This allows you to set a list of commands to be run each time execution\n\
7279 of the program stops."), &cmdlist);
7281 add_setshow_zuinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
7282 Set inferior debugging."), _("\
7283 Show inferior debugging."), _("\
7284 When non-zero, inferior specific debugging is enabled."),
7287 &setdebuglist, &showdebuglist);
7289 add_setshow_boolean_cmd ("displaced", class_maintenance,
7290 &debug_displaced, _("\
7291 Set displaced stepping debugging."), _("\
7292 Show displaced stepping debugging."), _("\
7293 When non-zero, displaced stepping specific debugging is enabled."),
7295 show_debug_displaced,
7296 &setdebuglist, &showdebuglist);
7298 add_setshow_boolean_cmd ("non-stop", no_class,
7300 Set whether gdb controls the inferior in non-stop mode."), _("\
7301 Show whether gdb controls the inferior in non-stop mode."), _("\
7302 When debugging a multi-threaded program and this setting is\n\
7303 off (the default, also called all-stop mode), when one thread stops\n\
7304 (for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
7305 all other threads in the program while you interact with the thread of\n\
7306 interest. When you continue or step a thread, you can allow the other\n\
7307 threads to run, or have them remain stopped, but while you inspect any\n\
7308 thread's state, all threads stop.\n\
7310 In non-stop mode, when one thread stops, other threads can continue\n\
7311 to run freely. You'll be able to step each thread independently,\n\
7312 leave it stopped or free to run as needed."),
7318 numsigs = (int) GDB_SIGNAL_LAST;
7319 signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
7320 signal_print = (unsigned char *)
7321 xmalloc (sizeof (signal_print[0]) * numsigs);
7322 signal_program = (unsigned char *)
7323 xmalloc (sizeof (signal_program[0]) * numsigs);
7324 signal_catch = (unsigned char *)
7325 xmalloc (sizeof (signal_catch[0]) * numsigs);
7326 signal_pass = (unsigned char *)
7327 xmalloc (sizeof (signal_program[0]) * numsigs);
7328 for (i = 0; i < numsigs; i++)
7331 signal_print[i] = 1;
7332 signal_program[i] = 1;
7333 signal_catch[i] = 0;
7336 /* Signals caused by debugger's own actions
7337 should not be given to the program afterwards. */
7338 signal_program[GDB_SIGNAL_TRAP] = 0;
7339 signal_program[GDB_SIGNAL_INT] = 0;
7341 /* Signals that are not errors should not normally enter the debugger. */
7342 signal_stop[GDB_SIGNAL_ALRM] = 0;
7343 signal_print[GDB_SIGNAL_ALRM] = 0;
7344 signal_stop[GDB_SIGNAL_VTALRM] = 0;
7345 signal_print[GDB_SIGNAL_VTALRM] = 0;
7346 signal_stop[GDB_SIGNAL_PROF] = 0;
7347 signal_print[GDB_SIGNAL_PROF] = 0;
7348 signal_stop[GDB_SIGNAL_CHLD] = 0;
7349 signal_print[GDB_SIGNAL_CHLD] = 0;
7350 signal_stop[GDB_SIGNAL_IO] = 0;
7351 signal_print[GDB_SIGNAL_IO] = 0;
7352 signal_stop[GDB_SIGNAL_POLL] = 0;
7353 signal_print[GDB_SIGNAL_POLL] = 0;
7354 signal_stop[GDB_SIGNAL_URG] = 0;
7355 signal_print[GDB_SIGNAL_URG] = 0;
7356 signal_stop[GDB_SIGNAL_WINCH] = 0;
7357 signal_print[GDB_SIGNAL_WINCH] = 0;
7358 signal_stop[GDB_SIGNAL_PRIO] = 0;
7359 signal_print[GDB_SIGNAL_PRIO] = 0;
7361 /* These signals are used internally by user-level thread
7362 implementations. (See signal(5) on Solaris.) Like the above
7363 signals, a healthy program receives and handles them as part of
7364 its normal operation. */
7365 signal_stop[GDB_SIGNAL_LWP] = 0;
7366 signal_print[GDB_SIGNAL_LWP] = 0;
7367 signal_stop[GDB_SIGNAL_WAITING] = 0;
7368 signal_print[GDB_SIGNAL_WAITING] = 0;
7369 signal_stop[GDB_SIGNAL_CANCEL] = 0;
7370 signal_print[GDB_SIGNAL_CANCEL] = 0;
7372 /* Update cached state. */
7373 signal_cache_update (-1);
7375 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
7376 &stop_on_solib_events, _("\
7377 Set stopping for shared library events."), _("\
7378 Show stopping for shared library events."), _("\
7379 If nonzero, gdb will give control to the user when the dynamic linker\n\
7380 notifies gdb of shared library events. The most common event of interest\n\
7381 to the user would be loading/unloading of a new library."),
7382 set_stop_on_solib_events,
7383 show_stop_on_solib_events,
7384 &setlist, &showlist);
7386 add_setshow_enum_cmd ("follow-fork-mode", class_run,
7387 follow_fork_mode_kind_names,
7388 &follow_fork_mode_string, _("\
7389 Set debugger response to a program call of fork or vfork."), _("\
7390 Show debugger response to a program call of fork or vfork."), _("\
7391 A fork or vfork creates a new process. follow-fork-mode can be:\n\
7392 parent - the original process is debugged after a fork\n\
7393 child - the new process is debugged after a fork\n\
7394 The unfollowed process will continue to run.\n\
7395 By default, the debugger will follow the parent process."),
7397 show_follow_fork_mode_string,
7398 &setlist, &showlist);
7400 add_setshow_enum_cmd ("follow-exec-mode", class_run,
7401 follow_exec_mode_names,
7402 &follow_exec_mode_string, _("\
7403 Set debugger response to a program call of exec."), _("\
7404 Show debugger response to a program call of exec."), _("\
7405 An exec call replaces the program image of a process.\n\
7407 follow-exec-mode can be:\n\
7409 new - the debugger creates a new inferior and rebinds the process\n\
7410 to this new inferior. The program the process was running before\n\
7411 the exec call can be restarted afterwards by restarting the original\n\
7414 same - the debugger keeps the process bound to the same inferior.\n\
7415 The new executable image replaces the previous executable loaded in\n\
7416 the inferior. Restarting the inferior after the exec call restarts\n\
7417 the executable the process was running after the exec call.\n\
7419 By default, the debugger will use the same inferior."),
7421 show_follow_exec_mode_string,
7422 &setlist, &showlist);
7424 add_setshow_enum_cmd ("scheduler-locking", class_run,
7425 scheduler_enums, &scheduler_mode, _("\
7426 Set mode for locking scheduler during execution."), _("\
7427 Show mode for locking scheduler during execution."), _("\
7428 off == no locking (threads may preempt at any time)\n\
7429 on == full locking (no thread except the current thread may run)\n\
7430 step == scheduler locked during every single-step operation.\n\
7431 In this mode, no other thread may run during a step command.\n\
7432 Other threads may run while stepping over a function call ('next')."),
7433 set_schedlock_func, /* traps on target vector */
7434 show_scheduler_mode,
7435 &setlist, &showlist);
7437 add_setshow_boolean_cmd ("schedule-multiple", class_run, &sched_multi, _("\
7438 Set mode for resuming threads of all processes."), _("\
7439 Show mode for resuming threads of all processes."), _("\
7440 When on, execution commands (such as 'continue' or 'next') resume all\n\
7441 threads of all processes. When off (which is the default), execution\n\
7442 commands only resume the threads of the current process. The set of\n\
7443 threads that are resumed is further refined by the scheduler-locking\n\
7444 mode (see help set scheduler-locking)."),
7446 show_schedule_multiple,
7447 &setlist, &showlist);
7449 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
7450 Set mode of the step operation."), _("\
7451 Show mode of the step operation."), _("\
7452 When set, doing a step over a function without debug line information\n\
7453 will stop at the first instruction of that function. Otherwise, the\n\
7454 function is skipped and the step command stops at a different source line."),
7456 show_step_stop_if_no_debug,
7457 &setlist, &showlist);
7459 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run,
7460 &can_use_displaced_stepping, _("\
7461 Set debugger's willingness to use displaced stepping."), _("\
7462 Show debugger's willingness to use displaced stepping."), _("\
7463 If on, gdb will use displaced stepping to step over breakpoints if it is\n\
7464 supported by the target architecture. If off, gdb will not use displaced\n\
7465 stepping to step over breakpoints, even if such is supported by the target\n\
7466 architecture. If auto (which is the default), gdb will use displaced stepping\n\
7467 if the target architecture supports it and non-stop mode is active, but will not\n\
7468 use it in all-stop mode (see help set non-stop)."),
7470 show_can_use_displaced_stepping,
7471 &setlist, &showlist);
7473 add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
7474 &exec_direction, _("Set direction of execution.\n\
7475 Options are 'forward' or 'reverse'."),
7476 _("Show direction of execution (forward/reverse)."),
7477 _("Tells gdb whether to execute forward or backward."),
7478 set_exec_direction_func, show_exec_direction_func,
7479 &setlist, &showlist);
7481 /* Set/show detach-on-fork: user-settable mode. */
7483 add_setshow_boolean_cmd ("detach-on-fork", class_run, &detach_fork, _("\
7484 Set whether gdb will detach the child of a fork."), _("\
7485 Show whether gdb will detach the child of a fork."), _("\
7486 Tells gdb whether to detach the child of a fork."),
7487 NULL, NULL, &setlist, &showlist);
7489 /* Set/show disable address space randomization mode. */
7491 add_setshow_boolean_cmd ("disable-randomization", class_support,
7492 &disable_randomization, _("\
7493 Set disabling of debuggee's virtual address space randomization."), _("\
7494 Show disabling of debuggee's virtual address space randomization."), _("\
7495 When this mode is on (which is the default), randomization of the virtual\n\
7496 address space is disabled. Standalone programs run with the randomization\n\
7497 enabled by default on some platforms."),
7498 &set_disable_randomization,
7499 &show_disable_randomization,
7500 &setlist, &showlist);
7502 /* ptid initializations */
7503 inferior_ptid = null_ptid;
7504 target_last_wait_ptid = minus_one_ptid;
7506 observer_attach_thread_ptid_changed (infrun_thread_ptid_changed);
7507 observer_attach_thread_stop_requested (infrun_thread_stop_requested);
7508 observer_attach_thread_exit (infrun_thread_thread_exit);
7509 observer_attach_inferior_exit (infrun_inferior_exit);
7511 /* Explicitly create without lookup, since that tries to create a
7512 value with a void typed value, and when we get here, gdbarch
7513 isn't initialized yet. At this point, we're quite sure there
7514 isn't another convenience variable of the same name. */
7515 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs, NULL);
7517 add_setshow_boolean_cmd ("observer", no_class,
7518 &observer_mode_1, _("\
7519 Set whether gdb controls the inferior in observer mode."), _("\
7520 Show whether gdb controls the inferior in observer mode."), _("\
7521 In observer mode, GDB can get data from the inferior, but not\n\
7522 affect its execution. Registers and memory may not be changed,\n\
7523 breakpoints may not be set, and the program cannot be interrupted\n\