1 /* Memory-access and commands for "inferior" process, for GDB.
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
36 #include "completer.h"
38 #include "event-top.h"
39 #include "parser-defs.h"
41 #include "reggroups.h"
46 #include "target-descriptions.h"
47 #include "user-regs.h"
48 #include "cli/cli-decode.h"
49 #include "gdbthread.h"
51 #include "inline-frame.h"
52 #include "tracepoint.h"
54 #include "continuations.h"
56 #include "cli/cli-utils.h"
58 #include "thread-fsm.h"
61 #include "common/gdb_optional.h"
63 /* Local functions: */
65 static void info_registers_command (char *, int);
67 static void until_next_command (int);
69 static void until_command (char *, int);
71 static void path_info (char *, int);
73 static void path_command (char *, int);
75 static void unset_command (char *, int);
77 static void info_float_command (char *, int);
79 static void disconnect_command (char *, int);
81 static void unset_environment_command (char *, int);
83 static void set_environment_command (char *, int);
85 static void environment_info (char *, int);
87 static void info_program_command (char *, int);
89 static void finish_command (char *, int);
91 static void signal_command (char *, int);
93 static void jump_command (char *, int);
95 static void step_1 (int, int, char *);
97 static void next_command (char *, int);
99 static void step_command (char *, int);
101 static void run_command (char *, int);
103 #define ERROR_NO_INFERIOR \
104 if (!target_has_execution) error (_("The program is not being run."));
106 /* Scratch area where string containing arguments to give to the
107 program will be stored by 'set args'. As soon as anything is
108 stored, notice_args_set will move it into per-inferior storage.
109 Arguments are separated by spaces. Empty string (pointer to '\0')
112 static char *inferior_args_scratch
;
114 /* Scratch area where 'set inferior-tty' will store user-provided value.
115 We'll immediate copy it into per-inferior storage. */
117 static char *inferior_io_terminal_scratch
;
119 /* Pid of our debugged inferior, or 0 if no inferior now.
120 Since various parts of infrun.c test this to see whether there is a program
121 being debugged it should be nonzero (currently 3 is used) for remote
124 ptid_t inferior_ptid
;
126 /* Address at which inferior stopped. */
130 /* Nonzero if stopped due to completion of a stack dummy routine. */
132 enum stop_stack_kind stop_stack_dummy
;
134 /* Nonzero if stopped due to a random (unexpected) signal in inferior
137 int stopped_by_random_signal
;
139 /* See inferior.h. */
141 int startup_with_shell
= 1;
144 /* Accessor routines. */
146 /* Set the io terminal for the current inferior. Ownership of
147 TERMINAL_NAME is not transferred. */
150 set_inferior_io_terminal (const char *terminal_name
)
152 xfree (current_inferior ()->terminal
);
154 if (terminal_name
!= NULL
&& *terminal_name
!= '\0')
155 current_inferior ()->terminal
= xstrdup (terminal_name
);
157 current_inferior ()->terminal
= NULL
;
161 get_inferior_io_terminal (void)
163 return current_inferior ()->terminal
;
167 set_inferior_tty_command (char *args
, int from_tty
,
168 struct cmd_list_element
*c
)
170 /* CLI has assigned the user-provided value to inferior_io_terminal_scratch.
171 Now route it to current inferior. */
172 set_inferior_io_terminal (inferior_io_terminal_scratch
);
176 show_inferior_tty_command (struct ui_file
*file
, int from_tty
,
177 struct cmd_list_element
*c
, const char *value
)
179 /* Note that we ignore the passed-in value in favor of computing it
181 const char *inferior_io_terminal
= get_inferior_io_terminal ();
183 if (inferior_io_terminal
== NULL
)
184 inferior_io_terminal
= "";
185 fprintf_filtered (gdb_stdout
,
186 _("Terminal for future runs of program being debugged "
187 "is \"%s\".\n"), inferior_io_terminal
);
191 get_inferior_args (void)
193 if (current_inferior ()->argc
!= 0)
197 n
= construct_inferior_arguments (current_inferior ()->argc
,
198 current_inferior ()->argv
);
199 set_inferior_args (n
);
203 if (current_inferior ()->args
== NULL
)
204 current_inferior ()->args
= xstrdup ("");
206 return current_inferior ()->args
;
209 /* Set the arguments for the current inferior. Ownership of
210 NEWARGS is not transferred. */
213 set_inferior_args (char *newargs
)
215 xfree (current_inferior ()->args
);
216 current_inferior ()->args
= newargs
? xstrdup (newargs
) : NULL
;
217 current_inferior ()->argc
= 0;
218 current_inferior ()->argv
= 0;
222 set_inferior_args_vector (int argc
, char **argv
)
224 current_inferior ()->argc
= argc
;
225 current_inferior ()->argv
= argv
;
228 /* Notice when `set args' is run. */
231 set_args_command (char *args
, int from_tty
, struct cmd_list_element
*c
)
233 /* CLI has assigned the user-provided value to inferior_args_scratch.
234 Now route it to current inferior. */
235 set_inferior_args (inferior_args_scratch
);
238 /* Notice when `show args' is run. */
241 show_args_command (struct ui_file
*file
, int from_tty
,
242 struct cmd_list_element
*c
, const char *value
)
244 /* Note that we ignore the passed-in value in favor of computing it
246 deprecated_show_value_hack (file
, from_tty
, c
, get_inferior_args ());
250 /* Compute command-line string given argument vector. This does the
251 same shell processing as fork_inferior. */
254 construct_inferior_arguments (int argc
, char **argv
)
258 if (startup_with_shell
)
261 /* This holds all the characters considered special to the
263 static const char special
[] = "\"!&*|[]{}<>?`~^=;, \t\n";
264 static const char quote
= '"';
266 /* This holds all the characters considered special to the
267 typical Unix shells. We include `^' because the SunOS
268 /bin/sh treats it as a synonym for `|'. */
269 static const char special
[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n";
270 static const char quote
= '\'';
276 /* We over-compute the size. It shouldn't matter. */
277 for (i
= 0; i
< argc
; ++i
)
278 length
+= 3 * strlen (argv
[i
]) + 1 + 2 * (argv
[i
][0] == '\0');
280 result
= (char *) xmalloc (length
);
283 for (i
= 0; i
< argc
; ++i
)
288 /* Need to handle empty arguments specially. */
289 if (argv
[i
][0] == '\0')
299 if (strpbrk (argv
[i
], special
))
305 for (cp
= argv
[i
]; *cp
; ++cp
)
309 /* A newline cannot be quoted with a backslash (it
310 just disappears), only by putting it inside
321 if (strchr (special
, *cp
) != NULL
)
337 /* In this case we can't handle arguments that contain spaces,
338 tabs, or newlines -- see breakup_args(). */
342 for (i
= 0; i
< argc
; ++i
)
344 char *cp
= strchr (argv
[i
], ' ');
346 cp
= strchr (argv
[i
], '\t');
348 cp
= strchr (argv
[i
], '\n');
350 error (_("can't handle command-line "
351 "argument containing whitespace"));
352 length
+= strlen (argv
[i
]) + 1;
355 result
= (char *) xmalloc (length
);
357 for (i
= 0; i
< argc
; ++i
)
360 strcat (result
, " ");
361 strcat (result
, argv
[i
]);
369 /* This function strips the '&' character (indicating background
370 execution) that is added as *the last* of the arguments ARGS of a
371 command. A copy of the incoming ARGS without the '&' is returned,
372 unless the resulting string after stripping is empty, in which case
373 NULL is returned. *BG_CHAR_P is an output boolean that indicates
374 whether the '&' character was found. */
377 strip_bg_char (const char *args
, int *bg_char_p
)
381 if (args
== NULL
|| *args
== '\0')
387 p
= args
+ strlen (args
);
391 while (p
> args
&& isspace (p
[-1]))
396 return savestring (args
, p
- args
);
402 return xstrdup (args
);
405 /* Common actions to take after creating any sort of inferior, by any
406 means (running, attaching, connecting, et cetera). The target
407 should be stopped. */
410 post_create_inferior (struct target_ops
*target
, int from_tty
)
413 /* Be sure we own the terminal in case write operations are performed. */
414 target_terminal_ours_for_output ();
416 /* If the target hasn't taken care of this already, do it now.
417 Targets which need to access registers during to_open,
418 to_create_inferior, or to_attach should do it earlier; but many
420 target_find_description ();
422 /* Now that we know the register layout, retrieve current PC. But
423 if the PC is unavailable (e.g., we're opening a core file with
424 missing registers info), ignore it. */
428 stop_pc
= regcache_read_pc (get_current_regcache ());
430 CATCH (ex
, RETURN_MASK_ERROR
)
432 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
433 throw_exception (ex
);
439 const unsigned solib_add_generation
440 = current_program_space
->solib_add_generation
;
442 /* Create the hooks to handle shared library load and unload
444 solib_create_inferior_hook (from_tty
);
446 if (current_program_space
->solib_add_generation
== solib_add_generation
)
448 /* The platform-specific hook should load initial shared libraries,
449 but didn't. FROM_TTY will be incorrectly 0 but such solib
450 targets should be fixed anyway. Call it only after the solib
451 target has been initialized by solib_create_inferior_hook. */
454 warning (_("platform-specific solib_create_inferior_hook did "
455 "not load initial shared libraries."));
457 /* If the solist is global across processes, there's no need to
459 if (!gdbarch_has_global_solist (target_gdbarch ()))
460 solib_add (NULL
, 0, auto_solib_add
);
464 /* If the user sets watchpoints before execution having started,
465 then she gets software watchpoints, because GDB can't know which
466 target will end up being pushed, or if it supports hardware
467 watchpoints or not. breakpoint_re_set takes care of promoting
468 watchpoints to hardware watchpoints if possible, however, if this
469 new inferior doesn't load shared libraries or we don't pull in
470 symbols from any other source on this target/arch,
471 breakpoint_re_set is never called. Call it now so that software
472 watchpoints get a chance to be promoted to hardware watchpoints
473 if the now pushed target supports hardware watchpoints. */
474 breakpoint_re_set ();
476 observer_notify_inferior_created (target
, from_tty
);
479 /* Kill the inferior if already running. This function is designed
480 to be called when we are about to start the execution of the program
481 from the beginning. Ask the user to confirm that he wants to restart
482 the program being debugged when FROM_TTY is non-null. */
485 kill_if_already_running (int from_tty
)
487 if (! ptid_equal (inferior_ptid
, null_ptid
) && target_has_execution
)
489 /* Bail out before killing the program if we will not be able to
491 target_require_runnable ();
494 && !query (_("The program being debugged has been started already.\n\
495 Start it from the beginning? ")))
496 error (_("Program not restarted."));
501 /* See inferior.h. */
504 prepare_execution_command (struct target_ops
*target
, int background
)
506 /* If we get a request for running in the bg but the target
507 doesn't support it, error out. */
508 if (background
&& !target
->to_can_async_p (target
))
509 error (_("Asynchronous execution not supported on this target."));
513 /* If we get a request for running in the fg, then we need to
514 simulate synchronous (fg) execution. Note no cleanup is
515 necessary for this. stdin is re-enabled whenever an error
516 reaches the top level. */
517 all_uis_on_sync_execution_starting ();
521 /* Implement the "run" command. If TBREAK_AT_MAIN is set, then insert
522 a temporary breakpoint at the begining of the main program before
523 running the program. */
526 run_command_1 (char *args
, int from_tty
, int tbreak_at_main
)
528 const char *exec_file
;
529 struct cleanup
*old_chain
;
531 struct ui_out
*uiout
= current_uiout
;
532 struct target_ops
*run_target
;
534 struct cleanup
*args_chain
;
538 kill_if_already_running (from_tty
);
540 init_wait_for_inferior ();
541 clear_breakpoint_hit_counts ();
543 /* Clean up any leftovers from other runs. Some other things from
544 this function should probably be moved into target_pre_inferior. */
545 target_pre_inferior (from_tty
);
547 /* The comment here used to read, "The exec file is re-read every
548 time we do a generic_mourn_inferior, so we just have to worry
549 about the symbol file." The `generic_mourn_inferior' function
550 gets called whenever the program exits. However, suppose the
551 program exits, and *then* the executable file changes? We need
552 to check again here. Since reopen_exec_file doesn't do anything
553 if the timestamp hasn't changed, I don't see the harm. */
557 args
= strip_bg_char (args
, &async_exec
);
558 args_chain
= make_cleanup (xfree
, args
);
560 /* Do validation and preparation before possibly changing anything
563 run_target
= find_run_target ();
565 prepare_execution_command (run_target
, async_exec
);
567 if (non_stop
&& !run_target
->to_supports_non_stop (run_target
))
568 error (_("The target does not support running in non-stop mode."));
570 /* Done. Can now set breakpoints, change inferior args, etc. */
572 /* Insert the temporary breakpoint if a location was specified. */
574 tbreak_command (main_name (), 0);
576 exec_file
= get_exec_file (0);
578 /* We keep symbols from add-symbol-file, on the grounds that the
579 user might want to add some symbols before running the program
580 (right?). But sometimes (dynamic loading where the user manually
581 introduces the new symbols with add-symbol-file), the code which
582 the symbols describe does not persist between runs. Currently
583 the user has to manually nuke all symbols between runs if they
584 want them to go away (PR 2207). This is probably reasonable. */
586 /* If there were other args, beside '&', process them. */
588 set_inferior_args (args
);
592 uiout
->field_string (NULL
, "Starting program");
595 uiout
->field_string ("execfile", exec_file
);
597 /* We call get_inferior_args() because we might need to compute
599 uiout
->field_string ("infargs", get_inferior_args ());
604 /* Done with ARGS. */
605 do_cleanups (args_chain
);
607 /* We call get_inferior_args() because we might need to compute
609 run_target
->to_create_inferior (run_target
, exec_file
,
610 std::string (get_inferior_args ()),
611 current_inferior ()->environment
.envp (),
613 /* to_create_inferior should push the target, so after this point we
614 shouldn't refer to run_target again. */
617 /* We're starting off a new process. When we get out of here, in
618 non-stop mode, finish the state of all threads of that process,
619 but leave other threads alone, as they may be stopped in internal
620 events --- the frontend shouldn't see them as stopped. In
621 all-stop, always finish the state of all threads, as we may be
622 resuming more than just the new process. */
624 ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
626 ptid
= minus_one_ptid
;
627 old_chain
= make_cleanup (finish_thread_state_cleanup
, &ptid
);
629 /* Pass zero for FROM_TTY, because at this point the "run" command
630 has done its thing; now we are setting up the running program. */
631 post_create_inferior (¤t_target
, 0);
633 /* Start the target running. Do not use -1 continuation as it would skip
634 breakpoint right at the entry point. */
635 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0
);
637 /* Since there was no error, there's no need to finish the thread
639 discard_cleanups (old_chain
);
643 run_command (char *args
, int from_tty
)
645 run_command_1 (args
, from_tty
, 0);
648 /* Start the execution of the program up until the beginning of the main
652 start_command (char *args
, int from_tty
)
654 /* Some languages such as Ada need to search inside the program
655 minimal symbols for the location where to put the temporary
656 breakpoint before starting. */
657 if (!have_minimal_symbols ())
658 error (_("No symbol table loaded. Use the \"file\" command."));
660 /* Run the program until reaching the main procedure... */
661 run_command_1 (args
, from_tty
, 1);
665 proceed_thread_callback (struct thread_info
*thread
, void *arg
)
667 /* We go through all threads individually instead of compressing
668 into a single target `resume_all' request, because some threads
669 may be stopped in internal breakpoints/events, or stopped waiting
670 for its turn in the displaced stepping queue (that is, they are
671 running && !executing). The target side has no idea about why
672 the thread is stopped, so a `resume_all' command would resume too
673 much. If/when GDB gains a way to tell the target `hold this
674 thread stopped until I say otherwise', then we can optimize
676 if (!is_stopped (thread
->ptid
))
679 switch_to_thread (thread
->ptid
);
680 clear_proceed_status (0);
681 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
686 ensure_valid_thread (void)
688 if (ptid_equal (inferior_ptid
, null_ptid
)
689 || is_exited (inferior_ptid
))
690 error (_("Cannot execute this command without a live selected thread."));
693 /* If the user is looking at trace frames, any resumption of execution
694 is likely to mix up recorded and live target data. So simply
695 disallow those commands. */
698 ensure_not_tfind_mode (void)
700 if (get_traceframe_number () >= 0)
701 error (_("Cannot execute this command while looking at trace frames."));
704 /* Throw an error indicating the current thread is running. */
707 error_is_running (void)
709 error (_("Cannot execute this command while "
710 "the selected thread is running."));
713 /* Calls error_is_running if the current thread is running. */
716 ensure_not_running (void)
718 if (is_running (inferior_ptid
))
723 continue_1 (int all_threads
)
726 ensure_not_tfind_mode ();
728 if (non_stop
&& all_threads
)
730 /* Don't error out if the current thread is running, because
731 there may be other stopped threads. */
733 /* Backup current thread and selected frame and restore on scope
735 scoped_restore_current_thread restore_thread
;
737 iterate_over_threads (proceed_thread_callback
, NULL
);
739 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
741 /* If all threads in the target were already running,
742 proceed_thread_callback ends up never calling proceed,
743 and so nothing calls this to put the inferior's terminal
744 settings in effect and remove stdin from the event loop,
745 which we must when running a foreground command. E.g.:
749 <all threads are running now>
752 <no thread was resumed, but the inferior now owns the terminal>
754 target_terminal_inferior ();
759 ensure_valid_thread ();
760 ensure_not_running ();
761 clear_proceed_status (0);
762 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
766 /* continue [-a] [proceed-count] [&] */
769 continue_command (char *args
, int from_tty
)
773 struct cleanup
*args_chain
;
777 /* Find out whether we must run in the background. */
778 args
= strip_bg_char (args
, &async_exec
);
779 args_chain
= make_cleanup (xfree
, args
);
783 if (startswith (args
, "-a"))
786 args
+= sizeof ("-a") - 1;
792 if (!non_stop
&& all_threads
)
793 error (_("`-a' is meaningless in all-stop mode."));
795 if (args
!= NULL
&& all_threads
)
796 error (_("Can't resume all threads and specify "
797 "proceed count simultaneously."));
799 /* If we have an argument left, set proceed count of breakpoint we
806 struct thread_info
*tp
;
809 tp
= find_thread_ptid (inferior_ptid
);
813 struct target_waitstatus ws
;
815 get_last_target_status (&last_ptid
, &ws
);
816 tp
= find_thread_ptid (last_ptid
);
819 bs
= tp
->control
.stop_bpstat
;
821 while ((stat
= bpstat_num (&bs
, &num
)) != 0)
824 set_ignore_count (num
,
825 parse_and_eval_long (args
) - 1,
827 /* set_ignore_count prints a message ending with a period.
828 So print two spaces before "Continuing.". */
830 printf_filtered (" ");
834 if (!stopped
&& from_tty
)
837 ("Not stopped at any breakpoint; argument ignored.\n");
841 /* Done with ARGS. */
842 do_cleanups (args_chain
);
845 ensure_not_tfind_mode ();
847 if (!non_stop
|| !all_threads
)
849 ensure_valid_thread ();
850 ensure_not_running ();
853 prepare_execution_command (¤t_target
, async_exec
);
856 printf_filtered (_("Continuing.\n"));
858 continue_1 (all_threads
);
861 /* Record the starting point of a "step" or "next" command. */
864 set_step_frame (void)
866 frame_info
*frame
= get_current_frame ();
868 symtab_and_line sal
= find_frame_sal (frame
);
869 set_step_info (frame
, sal
);
871 CORE_ADDR pc
= get_frame_pc (frame
);
872 thread_info
*tp
= inferior_thread ();
873 tp
->control
.step_start_function
= find_pc_function (pc
);
876 /* Step until outside of current statement. */
879 step_command (char *count_string
, int from_tty
)
881 step_1 (0, 0, count_string
);
884 /* Likewise, but skip over subroutine calls as if single instructions. */
887 next_command (char *count_string
, int from_tty
)
889 step_1 (1, 0, count_string
);
892 /* Likewise, but step only one instruction. */
895 stepi_command (char *count_string
, int from_tty
)
897 step_1 (0, 1, count_string
);
901 nexti_command (char *count_string
, int from_tty
)
903 step_1 (1, 1, count_string
);
907 delete_longjmp_breakpoint_cleanup (void *arg
)
909 int thread
= * (int *) arg
;
910 delete_longjmp_breakpoint (thread
);
913 /* Data for the FSM that manages the step/next/stepi/nexti
916 struct step_command_fsm
918 /* The base class. */
919 struct thread_fsm thread_fsm
;
921 /* How many steps left in a "step N"-like command. */
924 /* If true, this is a next/nexti, otherwise a step/stepi. */
925 int skip_subroutines
;
927 /* If true, this is a stepi/nexti, otherwise a step/step. */
931 static void step_command_fsm_clean_up (struct thread_fsm
*self
,
932 struct thread_info
*thread
);
933 static int step_command_fsm_should_stop (struct thread_fsm
*self
,
934 struct thread_info
*thread
);
935 static enum async_reply_reason
936 step_command_fsm_async_reply_reason (struct thread_fsm
*self
);
938 /* step_command_fsm's vtable. */
940 static struct thread_fsm_ops step_command_fsm_ops
=
943 step_command_fsm_clean_up
,
944 step_command_fsm_should_stop
,
945 NULL
, /* return_value */
946 step_command_fsm_async_reply_reason
,
949 /* Allocate a new step_command_fsm. */
951 static struct step_command_fsm
*
952 new_step_command_fsm (struct interp
*cmd_interp
)
954 struct step_command_fsm
*sm
;
956 sm
= XCNEW (struct step_command_fsm
);
957 thread_fsm_ctor (&sm
->thread_fsm
, &step_command_fsm_ops
, cmd_interp
);
962 /* Prepare for a step/next/etc. command. Any target resource
963 allocated here is undone in the FSM's clean_up method. */
966 step_command_fsm_prepare (struct step_command_fsm
*sm
,
967 int skip_subroutines
, int single_inst
,
968 int count
, struct thread_info
*thread
)
970 sm
->skip_subroutines
= skip_subroutines
;
971 sm
->single_inst
= single_inst
;
974 /* Leave the si command alone. */
975 if (!sm
->single_inst
|| sm
->skip_subroutines
)
976 set_longjmp_breakpoint (thread
, get_frame_id (get_current_frame ()));
978 thread
->control
.stepping_command
= 1;
981 static int prepare_one_step (struct step_command_fsm
*sm
);
984 step_1 (int skip_subroutines
, int single_inst
, char *count_string
)
988 struct cleanup
*args_chain
;
989 struct thread_info
*thr
;
990 struct step_command_fsm
*step_sm
;
993 ensure_not_tfind_mode ();
994 ensure_valid_thread ();
995 ensure_not_running ();
997 count_string
= strip_bg_char (count_string
, &async_exec
);
998 args_chain
= make_cleanup (xfree
, count_string
);
1000 prepare_execution_command (¤t_target
, async_exec
);
1002 count
= count_string
? parse_and_eval_long (count_string
) : 1;
1004 /* Done with ARGS. */
1005 do_cleanups (args_chain
);
1007 clear_proceed_status (1);
1009 /* Setup the execution command state machine to handle all the COUNT
1011 thr
= inferior_thread ();
1012 step_sm
= new_step_command_fsm (command_interp ());
1013 thr
->thread_fsm
= &step_sm
->thread_fsm
;
1015 step_command_fsm_prepare (step_sm
, skip_subroutines
,
1016 single_inst
, count
, thr
);
1018 /* Do only one step for now, before returning control to the event
1019 loop. Let the continuation figure out how many other steps we
1020 need to do, and handle them one at the time, through
1022 if (!prepare_one_step (step_sm
))
1023 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1028 /* Stepped into an inline frame. Pretend that we've
1030 thread_fsm_clean_up (thr
->thread_fsm
, thr
);
1031 proceeded
= normal_stop ();
1033 inferior_event_handler (INF_EXEC_COMPLETE
, NULL
);
1034 all_uis_check_sync_execution_done ();
1038 /* Implementation of the 'should_stop' FSM method for stepping
1039 commands. Called after we are done with one step operation, to
1040 check whether we need to step again, before we print the prompt and
1041 return control to the user. If count is > 1, returns false, as we
1042 will need to keep going. */
1045 step_command_fsm_should_stop (struct thread_fsm
*self
, struct thread_info
*tp
)
1047 struct step_command_fsm
*sm
= (struct step_command_fsm
*) self
;
1049 if (tp
->control
.stop_step
)
1051 /* There are more steps to make, and we did stop due to
1052 ending a stepping range. Do another step. */
1053 if (--sm
->count
> 0)
1054 return prepare_one_step (sm
);
1056 thread_fsm_set_finished (self
);
1062 /* Implementation of the 'clean_up' FSM method for stepping commands. */
1065 step_command_fsm_clean_up (struct thread_fsm
*self
, struct thread_info
*thread
)
1067 struct step_command_fsm
*sm
= (struct step_command_fsm
*) self
;
1069 if (!sm
->single_inst
|| sm
->skip_subroutines
)
1070 delete_longjmp_breakpoint (thread
->global_num
);
1073 /* Implementation of the 'async_reply_reason' FSM method for stepping
1076 static enum async_reply_reason
1077 step_command_fsm_async_reply_reason (struct thread_fsm
*self
)
1079 return EXEC_ASYNC_END_STEPPING_RANGE
;
1082 /* Prepare for one step in "step N". The actual target resumption is
1083 done by the caller. Return true if we're done and should thus
1084 report a stop to the user. Returns false if the target needs to be
1088 prepare_one_step (struct step_command_fsm
*sm
)
1092 struct frame_info
*frame
= get_current_frame ();
1094 /* Don't assume THREAD is a valid thread id. It is set to -1 if
1095 the longjmp breakpoint was not required. Use the
1096 INFERIOR_PTID thread instead, which is the same thread when
1098 struct thread_info
*tp
= inferior_thread ();
1102 if (!sm
->single_inst
)
1106 /* Step at an inlined function behaves like "down". */
1107 if (!sm
->skip_subroutines
1108 && inline_skipped_frames (inferior_ptid
))
1112 /* Pretend that we've ran. */
1113 resume_ptid
= user_visible_resume_ptid (1);
1114 set_running (resume_ptid
, 1);
1116 step_into_inline_frame (inferior_ptid
);
1118 return prepare_one_step (sm
);
1121 pc
= get_frame_pc (frame
);
1122 find_pc_line_pc_range (pc
,
1123 &tp
->control
.step_range_start
,
1124 &tp
->control
.step_range_end
);
1126 tp
->control
.may_range_step
= 1;
1128 /* If we have no line info, switch to stepi mode. */
1129 if (tp
->control
.step_range_end
== 0 && step_stop_if_no_debug
)
1131 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1132 tp
->control
.may_range_step
= 0;
1134 else if (tp
->control
.step_range_end
== 0)
1138 if (find_pc_partial_function (pc
, &name
,
1139 &tp
->control
.step_range_start
,
1140 &tp
->control
.step_range_end
) == 0)
1141 error (_("Cannot find bounds of current function"));
1143 target_terminal_ours_for_output ();
1144 printf_filtered (_("Single stepping until exit from function %s,"
1145 "\nwhich has no line number information.\n"),
1151 /* Say we are stepping, but stop after one insn whatever it does. */
1152 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1153 if (!sm
->skip_subroutines
)
1155 Don't step over function calls, not even to functions lacking
1157 tp
->control
.step_over_calls
= STEP_OVER_NONE
;
1160 if (sm
->skip_subroutines
)
1161 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1167 thread_fsm_set_finished (&sm
->thread_fsm
);
1172 /* Continue program at specified address. */
1175 jump_command (char *arg
, int from_tty
)
1177 struct gdbarch
*gdbarch
= get_current_arch ();
1182 struct cleanup
*args_chain
;
1185 ensure_not_tfind_mode ();
1186 ensure_valid_thread ();
1187 ensure_not_running ();
1189 /* Find out whether we must run in the background. */
1190 arg
= strip_bg_char (arg
, &async_exec
);
1191 args_chain
= make_cleanup (xfree
, arg
);
1193 prepare_execution_command (¤t_target
, async_exec
);
1196 error_no_arg (_("starting address"));
1198 std::vector
<symtab_and_line
> sals
1199 = decode_line_with_last_displayed (arg
, DECODE_LINE_FUNFIRSTLINE
);
1200 if (sals
.size () != 1)
1201 error (_("Unreasonable jump request"));
1203 /* Done with ARGS. */
1204 do_cleanups (args_chain
);
1206 symtab_and_line
&sal
= sals
[0];
1208 if (sal
.symtab
== 0 && sal
.pc
== 0)
1209 error (_("No source file has been specified."));
1211 resolve_sal_pc (&sal
); /* May error out. */
1213 /* See if we are trying to jump to another function. */
1214 fn
= get_frame_function (get_current_frame ());
1215 sfn
= find_pc_function (sal
.pc
);
1216 if (fn
!= NULL
&& sfn
!= fn
)
1218 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal
.line
,
1219 SYMBOL_PRINT_NAME (fn
)))
1221 error (_("Not confirmed."));
1228 struct obj_section
*section
;
1230 fixup_symbol_section (sfn
, 0);
1231 section
= SYMBOL_OBJ_SECTION (symbol_objfile (sfn
), sfn
);
1232 if (section_is_overlay (section
)
1233 && !section_is_mapped (section
))
1235 if (!query (_("WARNING!!! Destination is in "
1236 "unmapped overlay! Jump anyway? ")))
1238 error (_("Not confirmed."));
1248 printf_filtered (_("Continuing at "));
1249 fputs_filtered (paddress (gdbarch
, addr
), gdb_stdout
);
1250 printf_filtered (".\n");
1253 clear_proceed_status (0);
1254 proceed (addr
, GDB_SIGNAL_0
);
1257 /* Continue program giving it specified signal. */
1260 signal_command (char *signum_exp
, int from_tty
)
1262 enum gdb_signal oursig
;
1264 struct cleanup
*args_chain
;
1266 dont_repeat (); /* Too dangerous. */
1268 ensure_not_tfind_mode ();
1269 ensure_valid_thread ();
1270 ensure_not_running ();
1272 /* Find out whether we must run in the background. */
1273 signum_exp
= strip_bg_char (signum_exp
, &async_exec
);
1274 args_chain
= make_cleanup (xfree
, signum_exp
);
1276 prepare_execution_command (¤t_target
, async_exec
);
1279 error_no_arg (_("signal number"));
1281 /* It would be even slicker to make signal names be valid expressions,
1282 (the type could be "enum $signal" or some such), then the user could
1283 assign them to convenience variables. */
1284 oursig
= gdb_signal_from_name (signum_exp
);
1286 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1288 /* No, try numeric. */
1289 int num
= parse_and_eval_long (signum_exp
);
1292 oursig
= GDB_SIGNAL_0
;
1294 oursig
= gdb_signal_from_command (num
);
1297 do_cleanups (args_chain
);
1299 /* Look for threads other than the current that this command ends up
1300 resuming too (due to schedlock off), and warn if they'll get a
1301 signal delivered. "signal 0" is used to suppress a previous
1302 signal, but if the current thread is no longer the one that got
1303 the signal, then the user is potentially suppressing the signal
1304 of the wrong thread. */
1307 struct thread_info
*tp
;
1309 int must_confirm
= 0;
1311 /* This indicates what will be resumed. Either a single thread,
1312 a whole process, or all threads of all processes. */
1313 resume_ptid
= user_visible_resume_ptid (0);
1315 ALL_NON_EXITED_THREADS (tp
)
1317 if (ptid_equal (tp
->ptid
, inferior_ptid
))
1319 if (!ptid_match (tp
->ptid
, resume_ptid
))
1322 if (tp
->suspend
.stop_signal
!= GDB_SIGNAL_0
1323 && signal_pass_state (tp
->suspend
.stop_signal
))
1326 printf_unfiltered (_("Note:\n"));
1327 printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"),
1328 print_thread_id (tp
),
1329 gdb_signal_to_name (tp
->suspend
.stop_signal
),
1330 gdb_signal_to_string (tp
->suspend
.stop_signal
));
1336 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1337 "still deliver the signals noted above to their respective threads.\n"
1338 "Continue anyway? "),
1339 print_thread_id (inferior_thread ())))
1340 error (_("Not confirmed."));
1345 if (oursig
== GDB_SIGNAL_0
)
1346 printf_filtered (_("Continuing with no signal.\n"));
1348 printf_filtered (_("Continuing with signal %s.\n"),
1349 gdb_signal_to_name (oursig
));
1352 clear_proceed_status (0);
1353 proceed ((CORE_ADDR
) -1, oursig
);
1356 /* Queue a signal to be delivered to the current thread. */
1359 queue_signal_command (char *signum_exp
, int from_tty
)
1361 enum gdb_signal oursig
;
1362 struct thread_info
*tp
;
1365 ensure_not_tfind_mode ();
1366 ensure_valid_thread ();
1367 ensure_not_running ();
1369 if (signum_exp
== NULL
)
1370 error_no_arg (_("signal number"));
1372 /* It would be even slicker to make signal names be valid expressions,
1373 (the type could be "enum $signal" or some such), then the user could
1374 assign them to convenience variables. */
1375 oursig
= gdb_signal_from_name (signum_exp
);
1377 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1379 /* No, try numeric. */
1380 int num
= parse_and_eval_long (signum_exp
);
1383 oursig
= GDB_SIGNAL_0
;
1385 oursig
= gdb_signal_from_command (num
);
1388 if (oursig
!= GDB_SIGNAL_0
1389 && !signal_pass_state (oursig
))
1390 error (_("Signal handling set to not pass this signal to the program."));
1392 tp
= inferior_thread ();
1393 tp
->suspend
.stop_signal
= oursig
;
1396 /* Data for the FSM that manages the until (with no argument)
1399 struct until_next_fsm
1401 /* The base class. */
1402 struct thread_fsm thread_fsm
;
1404 /* The thread that as current when the command was executed. */
1408 static int until_next_fsm_should_stop (struct thread_fsm
*self
,
1409 struct thread_info
*thread
);
1410 static void until_next_fsm_clean_up (struct thread_fsm
*self
,
1411 struct thread_info
*thread
);
1412 static enum async_reply_reason
1413 until_next_fsm_async_reply_reason (struct thread_fsm
*self
);
1415 /* until_next_fsm's vtable. */
1417 static struct thread_fsm_ops until_next_fsm_ops
=
1420 until_next_fsm_clean_up
,
1421 until_next_fsm_should_stop
,
1422 NULL
, /* return_value */
1423 until_next_fsm_async_reply_reason
,
1426 /* Allocate a new until_next_fsm. */
1428 static struct until_next_fsm
*
1429 new_until_next_fsm (struct interp
*cmd_interp
, int thread
)
1431 struct until_next_fsm
*sm
;
1433 sm
= XCNEW (struct until_next_fsm
);
1434 thread_fsm_ctor (&sm
->thread_fsm
, &until_next_fsm_ops
, cmd_interp
);
1436 sm
->thread
= thread
;
1441 /* Implementation of the 'should_stop' FSM method for the until (with
1445 until_next_fsm_should_stop (struct thread_fsm
*self
,
1446 struct thread_info
*tp
)
1448 if (tp
->control
.stop_step
)
1449 thread_fsm_set_finished (self
);
1454 /* Implementation of the 'clean_up' FSM method for the until (with no
1458 until_next_fsm_clean_up (struct thread_fsm
*self
, struct thread_info
*thread
)
1460 struct until_next_fsm
*sm
= (struct until_next_fsm
*) self
;
1462 delete_longjmp_breakpoint (thread
->global_num
);
1465 /* Implementation of the 'async_reply_reason' FSM method for the until
1466 (with no arg) command. */
1468 static enum async_reply_reason
1469 until_next_fsm_async_reply_reason (struct thread_fsm
*self
)
1471 return EXEC_ASYNC_END_STEPPING_RANGE
;
1474 /* Proceed until we reach a different source line with pc greater than
1475 our current one or exit the function. We skip calls in both cases.
1477 Note that eventually this command should probably be changed so
1478 that only source lines are printed out when we hit the breakpoint
1479 we set. This may involve changes to wait_for_inferior and the
1480 proceed status code. */
1483 until_next_command (int from_tty
)
1485 struct frame_info
*frame
;
1487 struct symbol
*func
;
1488 struct symtab_and_line sal
;
1489 struct thread_info
*tp
= inferior_thread ();
1490 int thread
= tp
->global_num
;
1491 struct cleanup
*old_chain
;
1492 struct until_next_fsm
*sm
;
1494 clear_proceed_status (0);
1497 frame
= get_current_frame ();
1499 /* Step until either exited from this function or greater
1500 than the current line (if in symbolic section) or pc (if
1503 pc
= get_frame_pc (frame
);
1504 func
= find_pc_function (pc
);
1508 struct bound_minimal_symbol msymbol
= lookup_minimal_symbol_by_pc (pc
);
1510 if (msymbol
.minsym
== NULL
)
1511 error (_("Execution is not within a known function."));
1513 tp
->control
.step_range_start
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1514 /* The upper-bound of step_range is exclusive. In order to make PC
1515 within the range, set the step_range_end with PC + 1. */
1516 tp
->control
.step_range_end
= pc
+ 1;
1520 sal
= find_pc_line (pc
, 0);
1522 tp
->control
.step_range_start
= BLOCK_START (SYMBOL_BLOCK_VALUE (func
));
1523 tp
->control
.step_range_end
= sal
.end
;
1525 tp
->control
.may_range_step
= 1;
1527 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1529 set_longjmp_breakpoint (tp
, get_frame_id (frame
));
1530 old_chain
= make_cleanup (delete_longjmp_breakpoint_cleanup
, &thread
);
1532 sm
= new_until_next_fsm (command_interp (), tp
->global_num
);
1533 tp
->thread_fsm
= &sm
->thread_fsm
;
1534 discard_cleanups (old_chain
);
1536 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1540 until_command (char *arg
, int from_tty
)
1543 struct cleanup
*args_chain
;
1546 ensure_not_tfind_mode ();
1547 ensure_valid_thread ();
1548 ensure_not_running ();
1550 /* Find out whether we must run in the background. */
1551 arg
= strip_bg_char (arg
, &async_exec
);
1552 args_chain
= make_cleanup (xfree
, arg
);
1554 prepare_execution_command (¤t_target
, async_exec
);
1557 until_break_command (arg
, from_tty
, 0);
1559 until_next_command (from_tty
);
1561 /* Done with ARGS. */
1562 do_cleanups (args_chain
);
1566 advance_command (char *arg
, int from_tty
)
1569 struct cleanup
*args_chain
;
1572 ensure_not_tfind_mode ();
1573 ensure_valid_thread ();
1574 ensure_not_running ();
1577 error_no_arg (_("a location"));
1579 /* Find out whether we must run in the background. */
1580 arg
= strip_bg_char (arg
, &async_exec
);
1581 args_chain
= make_cleanup (xfree
, arg
);
1583 prepare_execution_command (¤t_target
, async_exec
);
1585 until_break_command (arg
, from_tty
, 1);
1587 /* Done with ARGS. */
1588 do_cleanups (args_chain
);
1591 /* Return the value of the result of a function at the end of a 'finish'
1592 command/BP. DTOR_DATA (if not NULL) can represent inferior registers
1593 right after an inferior call has finished. */
1596 get_return_value (struct value
*function
, struct type
*value_type
)
1598 regcache
stop_regs (regcache::readonly
, *get_current_regcache ());
1599 struct gdbarch
*gdbarch
= stop_regs
.arch ();
1600 struct value
*value
;
1602 value_type
= check_typedef (value_type
);
1603 gdb_assert (TYPE_CODE (value_type
) != TYPE_CODE_VOID
);
1605 /* FIXME: 2003-09-27: When returning from a nested inferior function
1606 call, it's possible (with no help from the architecture vector)
1607 to locate and return/print a "struct return" value. This is just
1608 a more complicated case of what is already being done in the
1609 inferior function call code. In fact, when inferior function
1610 calls are made async, this will likely be made the norm. */
1612 switch (gdbarch_return_value (gdbarch
, function
, value_type
,
1615 case RETURN_VALUE_REGISTER_CONVENTION
:
1616 case RETURN_VALUE_ABI_RETURNS_ADDRESS
:
1617 case RETURN_VALUE_ABI_PRESERVES_ADDRESS
:
1618 value
= allocate_value (value_type
);
1619 gdbarch_return_value (gdbarch
, function
, value_type
, &stop_regs
,
1620 value_contents_raw (value
), NULL
);
1622 case RETURN_VALUE_STRUCT_CONVENTION
:
1626 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1632 /* The captured function return value/type and its position in the
1635 struct return_value_info
1637 /* The captured return value. May be NULL if we weren't able to
1638 retrieve it. See get_return_value. */
1639 struct value
*value
;
1641 /* The return type. In some cases, we'll not be able extract the
1642 return value, but we always know the type. */
1645 /* If we captured a value, this is the value history index. */
1646 int value_history_index
;
1649 /* Helper for print_return_value. */
1652 print_return_value_1 (struct ui_out
*uiout
, struct return_value_info
*rv
)
1654 if (rv
->value
!= NULL
)
1656 struct value_print_options opts
;
1659 uiout
->text ("Value returned is ");
1660 uiout
->field_fmt ("gdb-result-var", "$%d",
1661 rv
->value_history_index
);
1662 uiout
->text (" = ");
1663 get_no_prettyformat_print_options (&opts
);
1667 value_print (rv
->value
, &stb
, &opts
);
1668 uiout
->field_stream ("return-value", stb
);
1673 std::string type_name
= type_to_string (rv
->type
);
1674 uiout
->text ("Value returned has type: ");
1675 uiout
->field_string ("return-type", type_name
.c_str ());
1677 uiout
->text (" Cannot determine contents\n");
1681 /* Print the result of a function at the end of a 'finish' command.
1682 RV points at an object representing the captured return value/type
1683 and its position in the value history. */
1686 print_return_value (struct ui_out
*uiout
, struct return_value_info
*rv
)
1688 if (rv
->type
== NULL
|| TYPE_CODE (rv
->type
) == TYPE_CODE_VOID
)
1693 /* print_return_value_1 can throw an exception in some
1694 circumstances. We need to catch this so that we still
1695 delete the breakpoint. */
1696 print_return_value_1 (uiout
, rv
);
1698 CATCH (ex
, RETURN_MASK_ALL
)
1700 exception_print (gdb_stdout
, ex
);
1705 /* Data for the FSM that manages the finish command. */
1707 struct finish_command_fsm
1709 /* The base class. */
1710 struct thread_fsm thread_fsm
;
1712 /* The momentary breakpoint set at the function's return address in
1714 struct breakpoint
*breakpoint
;
1716 /* The function that we're stepping out of. */
1717 struct symbol
*function
;
1719 /* If the FSM finishes successfully, this stores the function's
1721 struct return_value_info return_value
;
1724 static int finish_command_fsm_should_stop (struct thread_fsm
*self
,
1725 struct thread_info
*thread
);
1726 static void finish_command_fsm_clean_up (struct thread_fsm
*self
,
1727 struct thread_info
*thread
);
1728 static struct return_value_info
*
1729 finish_command_fsm_return_value (struct thread_fsm
*self
);
1730 static enum async_reply_reason
1731 finish_command_fsm_async_reply_reason (struct thread_fsm
*self
);
1733 /* finish_command_fsm's vtable. */
1735 static struct thread_fsm_ops finish_command_fsm_ops
=
1738 finish_command_fsm_clean_up
,
1739 finish_command_fsm_should_stop
,
1740 finish_command_fsm_return_value
,
1741 finish_command_fsm_async_reply_reason
,
1742 NULL
, /* should_notify_stop */
1745 /* Allocate a new finish_command_fsm. */
1747 static struct finish_command_fsm
*
1748 new_finish_command_fsm (struct interp
*cmd_interp
)
1750 struct finish_command_fsm
*sm
;
1752 sm
= XCNEW (struct finish_command_fsm
);
1753 thread_fsm_ctor (&sm
->thread_fsm
, &finish_command_fsm_ops
, cmd_interp
);
1758 /* Implementation of the 'should_stop' FSM method for the finish
1759 commands. Detects whether the thread stepped out of the function
1760 successfully, and if so, captures the function's return value and
1761 marks the FSM finished. */
1764 finish_command_fsm_should_stop (struct thread_fsm
*self
,
1765 struct thread_info
*tp
)
1767 struct finish_command_fsm
*f
= (struct finish_command_fsm
*) self
;
1768 struct return_value_info
*rv
= &f
->return_value
;
1770 if (f
->function
!= NULL
1771 && bpstat_find_breakpoint (tp
->control
.stop_bpstat
,
1772 f
->breakpoint
) != NULL
)
1775 thread_fsm_set_finished (self
);
1777 rv
->type
= TYPE_TARGET_TYPE (SYMBOL_TYPE (f
->function
));
1778 if (rv
->type
== NULL
)
1779 internal_error (__FILE__
, __LINE__
,
1780 _("finish_command: function has no target type"));
1782 if (TYPE_CODE (rv
->type
) != TYPE_CODE_VOID
)
1786 func
= read_var_value (f
->function
, NULL
, get_current_frame ());
1787 rv
->value
= get_return_value (func
, rv
->type
);
1788 if (rv
->value
!= NULL
)
1789 rv
->value_history_index
= record_latest_value (rv
->value
);
1792 else if (tp
->control
.stop_step
)
1794 /* Finishing from an inline frame, or reverse finishing. In
1795 either case, there's no way to retrieve the return value. */
1796 thread_fsm_set_finished (self
);
1802 /* Implementation of the 'clean_up' FSM method for the finish
1806 finish_command_fsm_clean_up (struct thread_fsm
*self
,
1807 struct thread_info
*thread
)
1809 struct finish_command_fsm
*f
= (struct finish_command_fsm
*) self
;
1811 if (f
->breakpoint
!= NULL
)
1813 delete_breakpoint (f
->breakpoint
);
1814 f
->breakpoint
= NULL
;
1816 delete_longjmp_breakpoint (thread
->global_num
);
1819 /* Implementation of the 'return_value' FSM method for the finish
1822 static struct return_value_info
*
1823 finish_command_fsm_return_value (struct thread_fsm
*self
)
1825 struct finish_command_fsm
*f
= (struct finish_command_fsm
*) self
;
1827 return &f
->return_value
;
1830 /* Implementation of the 'async_reply_reason' FSM method for the
1833 static enum async_reply_reason
1834 finish_command_fsm_async_reply_reason (struct thread_fsm
*self
)
1836 if (execution_direction
== EXEC_REVERSE
)
1837 return EXEC_ASYNC_END_STEPPING_RANGE
;
1839 return EXEC_ASYNC_FUNCTION_FINISHED
;
1842 /* finish_backward -- helper function for finish_command. */
1845 finish_backward (struct finish_command_fsm
*sm
)
1847 struct symtab_and_line sal
;
1848 struct thread_info
*tp
= inferior_thread ();
1850 CORE_ADDR func_addr
;
1852 pc
= get_frame_pc (get_current_frame ());
1854 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
) == 0)
1855 error (_("Cannot find bounds of current function"));
1857 sal
= find_pc_line (func_addr
, 0);
1859 tp
->control
.proceed_to_finish
= 1;
1860 /* Special case: if we're sitting at the function entry point,
1861 then all we need to do is take a reverse singlestep. We
1862 don't need to set a breakpoint, and indeed it would do us
1865 Note that this can only happen at frame #0, since there's
1866 no way that a function up the stack can have a return address
1867 that's equal to its entry point. */
1871 struct frame_info
*frame
= get_selected_frame (NULL
);
1872 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1874 /* Set a step-resume at the function's entry point. Once that's
1875 hit, we'll do one more step backwards. */
1876 symtab_and_line sr_sal
;
1878 sr_sal
.pspace
= get_frame_program_space (frame
);
1879 insert_step_resume_breakpoint_at_sal (gdbarch
,
1880 sr_sal
, null_frame_id
);
1882 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1886 /* We're almost there -- we just need to back up by one more
1888 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1889 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1893 /* finish_forward -- helper function for finish_command. FRAME is the
1894 frame that called the function we're about to step out of. */
1897 finish_forward (struct finish_command_fsm
*sm
, struct frame_info
*frame
)
1899 struct frame_id frame_id
= get_frame_id (frame
);
1900 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1901 struct symtab_and_line sal
;
1902 struct thread_info
*tp
= inferior_thread ();
1904 sal
= find_pc_line (get_frame_pc (frame
), 0);
1905 sal
.pc
= get_frame_pc (frame
);
1907 sm
->breakpoint
= set_momentary_breakpoint (gdbarch
, sal
,
1908 get_stack_frame_id (frame
),
1911 /* set_momentary_breakpoint invalidates FRAME. */
1914 set_longjmp_breakpoint (tp
, frame_id
);
1916 /* We want to print return value, please... */
1917 tp
->control
.proceed_to_finish
= 1;
1919 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1922 /* Skip frames for "finish". */
1924 static struct frame_info
*
1925 skip_finish_frames (struct frame_info
*frame
)
1927 struct frame_info
*start
;
1933 frame
= skip_tailcall_frames (frame
);
1937 frame
= skip_unwritable_frames (frame
);
1941 while (start
!= frame
);
1946 /* "finish": Set a temporary breakpoint at the place the selected
1947 frame will return to, then continue. */
1950 finish_command (char *arg
, int from_tty
)
1952 struct frame_info
*frame
;
1954 struct cleanup
*args_chain
;
1955 struct finish_command_fsm
*sm
;
1956 struct thread_info
*tp
;
1959 ensure_not_tfind_mode ();
1960 ensure_valid_thread ();
1961 ensure_not_running ();
1963 /* Find out whether we must run in the background. */
1964 arg
= strip_bg_char (arg
, &async_exec
);
1965 args_chain
= make_cleanup (xfree
, arg
);
1967 prepare_execution_command (¤t_target
, async_exec
);
1970 error (_("The \"finish\" command does not take any arguments."));
1972 /* Done with ARGS. */
1973 do_cleanups (args_chain
);
1975 frame
= get_prev_frame (get_selected_frame (_("No selected frame.")));
1977 error (_("\"finish\" not meaningful in the outermost frame."));
1979 clear_proceed_status (0);
1981 tp
= inferior_thread ();
1983 sm
= new_finish_command_fsm (command_interp ());
1985 tp
->thread_fsm
= &sm
->thread_fsm
;
1987 /* Finishing from an inline frame is completely different. We don't
1988 try to show the "return value" - no way to locate it. */
1989 if (get_frame_type (get_selected_frame (_("No selected frame.")))
1992 /* Claim we are stepping in the calling frame. An empty step
1993 range means that we will stop once we aren't in a function
1994 called by that frame. We don't use the magic "1" value for
1995 step_range_end, because then infrun will think this is nexti,
1996 and not step over the rest of this inlined function call. */
1997 set_step_info (frame
, {});
1998 tp
->control
.step_range_start
= get_frame_pc (frame
);
1999 tp
->control
.step_range_end
= tp
->control
.step_range_start
;
2000 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
2002 /* Print info on the selected frame, including level number but not
2006 printf_filtered (_("Run till exit from "));
2007 print_stack_frame (get_selected_frame (NULL
), 1, LOCATION
, 0);
2010 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2014 /* Find the function we will return from. */
2016 sm
->function
= find_pc_function (get_frame_pc (get_selected_frame (NULL
)));
2018 /* Print info on the selected frame, including level number but not
2022 if (execution_direction
== EXEC_REVERSE
)
2023 printf_filtered (_("Run back to call of "));
2026 if (sm
->function
!= NULL
&& TYPE_NO_RETURN (sm
->function
->type
)
2027 && !query (_("warning: Function %s does not return normally.\n"
2028 "Try to finish anyway? "),
2029 SYMBOL_PRINT_NAME (sm
->function
)))
2030 error (_("Not confirmed."));
2031 printf_filtered (_("Run till exit from "));
2034 print_stack_frame (get_selected_frame (NULL
), 1, LOCATION
, 0);
2037 if (execution_direction
== EXEC_REVERSE
)
2038 finish_backward (sm
);
2041 frame
= skip_finish_frames (frame
);
2044 error (_("Cannot find the caller frame."));
2046 finish_forward (sm
, frame
);
2052 info_program_command (char *args
, int from_tty
)
2056 struct thread_info
*tp
;
2059 if (!target_has_execution
)
2061 printf_filtered (_("The program being debugged is not being run.\n"));
2066 ptid
= inferior_ptid
;
2069 struct target_waitstatus ws
;
2071 get_last_target_status (&ptid
, &ws
);
2074 if (ptid_equal (ptid
, null_ptid
) || is_exited (ptid
))
2075 error (_("Invalid selected thread."));
2076 else if (is_running (ptid
))
2077 error (_("Selected thread is running."));
2079 tp
= find_thread_ptid (ptid
);
2080 bs
= tp
->control
.stop_bpstat
;
2081 stat
= bpstat_num (&bs
, &num
);
2083 target_files_info ();
2084 printf_filtered (_("Program stopped at %s.\n"),
2085 paddress (target_gdbarch (), stop_pc
));
2086 if (tp
->control
.stop_step
)
2087 printf_filtered (_("It stopped after being stepped.\n"));
2090 /* There may be several breakpoints in the same place, so this
2091 isn't as strange as it seems. */
2096 printf_filtered (_("It stopped at a breakpoint "
2097 "that has since been deleted.\n"));
2100 printf_filtered (_("It stopped at breakpoint %d.\n"), num
);
2101 stat
= bpstat_num (&bs
, &num
);
2104 else if (tp
->suspend
.stop_signal
!= GDB_SIGNAL_0
)
2106 printf_filtered (_("It stopped with signal %s, %s.\n"),
2107 gdb_signal_to_name (tp
->suspend
.stop_signal
),
2108 gdb_signal_to_string (tp
->suspend
.stop_signal
));
2113 printf_filtered (_("Type \"info stack\" or \"info "
2114 "registers\" for more information.\n"));
2119 environment_info (char *var
, int from_tty
)
2123 const char *val
= current_inferior ()->environment
.get (var
);
2127 puts_filtered (var
);
2128 puts_filtered (" = ");
2129 puts_filtered (val
);
2130 puts_filtered ("\n");
2134 puts_filtered ("Environment variable \"");
2135 puts_filtered (var
);
2136 puts_filtered ("\" not defined.\n");
2141 char **envp
= current_inferior ()->environment
.envp ();
2143 for (int idx
= 0; envp
[idx
] != NULL
; ++idx
)
2145 puts_filtered (envp
[idx
]);
2146 puts_filtered ("\n");
2152 set_environment_command (char *arg
, int from_tty
)
2154 char *p
, *val
, *var
;
2158 error_no_arg (_("environment variable and value"));
2160 /* Find seperation between variable name and value. */
2161 p
= (char *) strchr (arg
, '=');
2162 val
= (char *) strchr (arg
, ' ');
2164 if (p
!= 0 && val
!= 0)
2166 /* We have both a space and an equals. If the space is before the
2167 equals, walk forward over the spaces til we see a nonspace
2168 (possibly the equals). */
2173 /* Now if the = is after the char following the spaces,
2174 take the char following the spaces. */
2178 else if (val
!= 0 && p
== 0)
2182 error_no_arg (_("environment variable to set"));
2184 if (p
== 0 || p
[1] == 0)
2188 p
= arg
+ strlen (arg
); /* So that savestring below will work. */
2192 /* Not setting variable value to null. */
2194 while (*val
== ' ' || *val
== '\t')
2198 while (p
!= arg
&& (p
[-1] == ' ' || p
[-1] == '\t'))
2201 var
= savestring (arg
, p
- arg
);
2204 printf_filtered (_("Setting environment variable "
2205 "\"%s\" to null value.\n"),
2207 current_inferior ()->environment
.set (var
, "");
2210 current_inferior ()->environment
.set (var
, val
);
2215 unset_environment_command (char *var
, int from_tty
)
2219 /* If there is no argument, delete all environment variables.
2220 Ask for confirmation if reading from the terminal. */
2221 if (!from_tty
|| query (_("Delete all environment variables? ")))
2222 current_inferior ()->environment
.clear ();
2225 current_inferior ()->environment
.unset (var
);
2228 /* Handle the execution path (PATH variable). */
2230 static const char path_var_name
[] = "PATH";
2233 path_info (char *args
, int from_tty
)
2235 puts_filtered ("Executable and object file path: ");
2236 puts_filtered (current_inferior ()->environment
.get (path_var_name
));
2237 puts_filtered ("\n");
2240 /* Add zero or more directories to the front of the execution path. */
2243 path_command (char *dirname
, int from_tty
)
2249 env
= current_inferior ()->environment
.get (path_var_name
);
2250 /* Can be null if path is not set. */
2253 exec_path
= xstrdup (env
);
2254 mod_path (dirname
, &exec_path
);
2255 current_inferior ()->environment
.set (path_var_name
, exec_path
);
2258 path_info ((char *) NULL
, from_tty
);
2262 /* Print out the register NAME with value VAL, to FILE, in the default
2266 default_print_one_register_info (struct ui_file
*file
,
2270 struct type
*regtype
= value_type (val
);
2271 int print_raw_format
;
2273 fputs_filtered (name
, file
);
2274 print_spaces_filtered (15 - strlen (name
), file
);
2276 print_raw_format
= (value_entirely_available (val
)
2277 && !value_optimized_out (val
));
2279 /* If virtual format is floating, print it that way, and in raw
2281 if (TYPE_CODE (regtype
) == TYPE_CODE_FLT
2282 || TYPE_CODE (regtype
) == TYPE_CODE_DECFLOAT
)
2284 struct value_print_options opts
;
2285 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
2286 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (regtype
));
2288 get_user_print_options (&opts
);
2292 value_embedded_offset (val
), 0,
2293 file
, 0, val
, &opts
, current_language
);
2295 if (print_raw_format
)
2297 fprintf_filtered (file
, "\t(raw ");
2298 print_hex_chars (file
, valaddr
, TYPE_LENGTH (regtype
), byte_order
,
2300 fprintf_filtered (file
, ")");
2305 struct value_print_options opts
;
2307 /* Print the register in hex. */
2308 get_formatted_print_options (&opts
, 'x');
2311 value_embedded_offset (val
), 0,
2312 file
, 0, val
, &opts
, current_language
);
2313 /* If not a vector register, print it also according to its
2315 if (print_raw_format
&& TYPE_VECTOR (regtype
) == 0)
2317 get_user_print_options (&opts
);
2319 fprintf_filtered (file
, "\t");
2321 value_embedded_offset (val
), 0,
2322 file
, 0, val
, &opts
, current_language
);
2326 fprintf_filtered (file
, "\n");
2329 /* Print out the machine register regnum. If regnum is -1, print all
2330 registers (print_all == 1) or all non-float and non-vector
2331 registers (print_all == 0).
2333 For most machines, having all_registers_info() print the
2334 register(s) one per line is good enough. If a different format is
2335 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2336 regs), or there is an existing convention for showing all the
2337 registers, define the architecture method PRINT_REGISTERS_INFO to
2338 provide that format. */
2341 default_print_registers_info (struct gdbarch
*gdbarch
,
2342 struct ui_file
*file
,
2343 struct frame_info
*frame
,
2344 int regnum
, int print_all
)
2347 const int numregs
= gdbarch_num_regs (gdbarch
)
2348 + gdbarch_num_pseudo_regs (gdbarch
);
2350 for (i
= 0; i
< numregs
; i
++)
2352 /* Decide between printing all regs, non-float / vector regs, or
2358 if (!gdbarch_register_reggroup_p (gdbarch
, i
, all_reggroup
))
2363 if (!gdbarch_register_reggroup_p (gdbarch
, i
, general_reggroup
))
2373 /* If the register name is empty, it is undefined for this
2374 processor, so don't display anything. */
2375 if (gdbarch_register_name (gdbarch
, i
) == NULL
2376 || *(gdbarch_register_name (gdbarch
, i
)) == '\0')
2379 default_print_one_register_info (file
,
2380 gdbarch_register_name (gdbarch
, i
),
2381 value_of_register (i
, frame
));
2386 registers_info (char *addr_exp
, int fpregs
)
2388 struct frame_info
*frame
;
2389 struct gdbarch
*gdbarch
;
2391 if (!target_has_registers
)
2392 error (_("The program has no registers now."));
2393 frame
= get_selected_frame (NULL
);
2394 gdbarch
= get_frame_arch (frame
);
2398 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2403 while (*addr_exp
!= '\0')
2408 /* Skip leading white space. */
2409 addr_exp
= skip_spaces (addr_exp
);
2411 /* Discard any leading ``$''. Check that there is something
2412 resembling a register following it. */
2413 if (addr_exp
[0] == '$')
2415 if (isspace ((*addr_exp
)) || (*addr_exp
) == '\0')
2416 error (_("Missing register name"));
2418 /* Find the start/end of this register name/num/group. */
2420 while ((*addr_exp
) != '\0' && !isspace ((*addr_exp
)))
2424 /* Figure out what we've found and display it. */
2426 /* A register name? */
2428 int regnum
= user_reg_map_name_to_regnum (gdbarch
, start
, end
- start
);
2432 /* User registers lie completely outside of the range of
2433 normal registers. Catch them early so that the target
2435 if (regnum
>= gdbarch_num_regs (gdbarch
)
2436 + gdbarch_num_pseudo_regs (gdbarch
))
2438 struct value
*regval
= value_of_user_reg (regnum
, frame
);
2439 const char *regname
= user_reg_map_regnum_to_name (gdbarch
,
2442 /* Print in the same fashion
2443 gdbarch_print_registers_info's default
2444 implementation prints. */
2445 default_print_one_register_info (gdb_stdout
,
2450 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2451 frame
, regnum
, fpregs
);
2456 /* A register group? */
2458 struct reggroup
*group
;
2460 for (group
= reggroup_next (gdbarch
, NULL
);
2462 group
= reggroup_next (gdbarch
, group
))
2464 /* Don't bother with a length check. Should the user
2465 enter a short register group name, go with the first
2466 group that matches. */
2467 if (strncmp (start
, reggroup_name (group
), end
- start
) == 0)
2475 regnum
< gdbarch_num_regs (gdbarch
)
2476 + gdbarch_num_pseudo_regs (gdbarch
);
2479 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
2480 gdbarch_print_registers_info (gdbarch
,
2488 /* Nothing matched. */
2489 error (_("Invalid register `%.*s'"), (int) (end
- start
), start
);
2494 info_all_registers_command (char *addr_exp
, int from_tty
)
2496 registers_info (addr_exp
, 1);
2500 info_registers_command (char *addr_exp
, int from_tty
)
2502 registers_info (addr_exp
, 0);
2506 print_vector_info (struct ui_file
*file
,
2507 struct frame_info
*frame
, const char *args
)
2509 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
2511 if (gdbarch_print_vector_info_p (gdbarch
))
2512 gdbarch_print_vector_info (gdbarch
, file
, frame
, args
);
2516 int printed_something
= 0;
2519 regnum
< gdbarch_num_regs (gdbarch
)
2520 + gdbarch_num_pseudo_regs (gdbarch
);
2523 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, vector_reggroup
))
2525 printed_something
= 1;
2526 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2529 if (!printed_something
)
2530 fprintf_filtered (file
, "No vector information\n");
2535 info_vector_command (char *args
, int from_tty
)
2537 if (!target_has_registers
)
2538 error (_("The program has no registers now."));
2540 print_vector_info (gdb_stdout
, get_selected_frame (NULL
), args
);
2543 /* Kill the inferior process. Make us have no inferior. */
2546 kill_command (char *arg
, int from_tty
)
2548 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2549 It should be a distinct flag that indicates that a target is active, cuz
2550 some targets don't have processes! */
2552 if (ptid_equal (inferior_ptid
, null_ptid
))
2553 error (_("The program is not being run."));
2554 if (!query (_("Kill the program being debugged? ")))
2555 error (_("Not confirmed."));
2558 /* If we still have other inferiors to debug, then don't mess with
2559 with their threads. */
2560 if (!have_inferiors ())
2562 init_thread_list (); /* Destroy thread info. */
2564 /* Killing off the inferior can leave us with a core file. If
2565 so, print the state we are left in. */
2566 if (target_has_stack
)
2568 printf_filtered (_("In %s,\n"), target_longname
);
2569 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
2572 bfd_cache_close_all ();
2575 /* Used in `attach&' command. ARG is a point to an integer
2576 representing a process id. Proceed threads of this process iff
2577 they stopped due to debugger request, and when they did, they
2578 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads
2579 that have been explicitly been told to stop. */
2582 proceed_after_attach_callback (struct thread_info
*thread
,
2585 int pid
= * (int *) arg
;
2587 if (ptid_get_pid (thread
->ptid
) == pid
2588 && !is_exited (thread
->ptid
)
2589 && !is_executing (thread
->ptid
)
2590 && !thread
->stop_requested
2591 && thread
->suspend
.stop_signal
== GDB_SIGNAL_0
)
2593 switch_to_thread (thread
->ptid
);
2594 clear_proceed_status (0);
2595 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2602 proceed_after_attach (int pid
)
2604 /* Don't error out if the current thread is running, because
2605 there may be other stopped threads. */
2607 /* Backup current thread and selected frame. */
2608 scoped_restore_current_thread restore_thread
;
2610 iterate_over_threads (proceed_after_attach_callback
, &pid
);
2613 /* See inferior.h. */
2616 setup_inferior (int from_tty
)
2618 struct inferior
*inferior
;
2620 inferior
= current_inferior ();
2621 inferior
->needs_setup
= 0;
2623 /* If no exec file is yet known, try to determine it from the
2625 if (get_exec_file (0) == NULL
)
2626 exec_file_locate_attach (ptid_get_pid (inferior_ptid
), 1, from_tty
);
2629 reopen_exec_file ();
2633 /* Take any necessary post-attaching actions for this platform. */
2634 target_post_attach (ptid_get_pid (inferior_ptid
));
2636 post_create_inferior (¤t_target
, from_tty
);
2639 /* What to do after the first program stops after attaching. */
2640 enum attach_post_wait_mode
2642 /* Do nothing. Leaves threads as they are. */
2643 ATTACH_POST_WAIT_NOTHING
,
2645 /* Re-resume threads that are marked running. */
2646 ATTACH_POST_WAIT_RESUME
,
2648 /* Stop all threads. */
2649 ATTACH_POST_WAIT_STOP
,
2652 /* Called after we've attached to a process and we've seen it stop for
2653 the first time. If ASYNC_EXEC is true, re-resume threads that
2654 should be running. Else if ATTACH, */
2657 attach_post_wait (const char *args
, int from_tty
, enum attach_post_wait_mode mode
)
2659 struct inferior
*inferior
;
2661 inferior
= current_inferior ();
2662 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
2664 if (inferior
->needs_setup
)
2665 setup_inferior (from_tty
);
2667 if (mode
== ATTACH_POST_WAIT_RESUME
)
2669 /* The user requested an `attach&', so be sure to leave threads
2670 that didn't get a signal running. */
2672 /* Immediatelly resume all suspended threads of this inferior,
2673 and this inferior only. This should have no effect on
2674 already running threads. If a thread has been stopped with a
2675 signal, leave it be. */
2677 proceed_after_attach (inferior
->pid
);
2680 if (inferior_thread ()->suspend
.stop_signal
== GDB_SIGNAL_0
)
2682 clear_proceed_status (0);
2683 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2687 else if (mode
== ATTACH_POST_WAIT_STOP
)
2689 /* The user requested a plain `attach', so be sure to leave
2690 the inferior stopped. */
2692 /* At least the current thread is already stopped. */
2694 /* In all-stop, by definition, all threads have to be already
2695 stopped at this point. In non-stop, however, although the
2696 selected thread is stopped, others may still be executing.
2697 Be sure to explicitly stop all threads of the process. This
2698 should have no effect on already stopped threads. */
2700 target_stop (pid_to_ptid (inferior
->pid
));
2701 else if (target_is_non_stop_p ())
2703 struct thread_info
*thread
;
2704 struct thread_info
*lowest
= inferior_thread ();
2705 int pid
= current_inferior ()->pid
;
2707 stop_all_threads ();
2709 /* It's not defined which thread will report the attach
2710 stop. For consistency, always select the thread with
2711 lowest GDB number, which should be the main thread, if it
2713 ALL_NON_EXITED_THREADS (thread
)
2715 if (ptid_get_pid (thread
->ptid
) == pid
)
2717 if (thread
->inf
->num
< lowest
->inf
->num
2718 || thread
->per_inf_num
< lowest
->per_inf_num
)
2723 switch_to_thread (lowest
->ptid
);
2726 /* Tell the user/frontend where we're stopped. */
2728 if (deprecated_attach_hook
)
2729 deprecated_attach_hook ();
2733 struct attach_command_continuation_args
2737 enum attach_post_wait_mode mode
;
2741 attach_command_continuation (void *args
, int err
)
2743 struct attach_command_continuation_args
*a
2744 = (struct attach_command_continuation_args
*) args
;
2749 attach_post_wait (a
->args
, a
->from_tty
, a
->mode
);
2753 attach_command_continuation_free_args (void *args
)
2755 struct attach_command_continuation_args
*a
2756 = (struct attach_command_continuation_args
*) args
;
2762 /* "attach" command entry point. Takes a program started up outside
2763 of gdb and ``attaches'' to it. This stops it cold in its tracks
2764 and allows us to start debugging it. */
2767 attach_command (char *args
, int from_tty
)
2770 struct cleanup
*args_chain
;
2771 struct target_ops
*attach_target
;
2772 struct inferior
*inferior
= current_inferior ();
2773 enum attach_post_wait_mode mode
;
2775 dont_repeat (); /* Not for the faint of heart */
2777 if (gdbarch_has_global_solist (target_gdbarch ()))
2778 /* Don't complain if all processes share the same symbol
2781 else if (target_has_execution
)
2783 if (query (_("A program is being debugged already. Kill it? ")))
2786 error (_("Not killed."));
2789 /* Clean up any leftovers from other runs. Some other things from
2790 this function should probably be moved into target_pre_inferior. */
2791 target_pre_inferior (from_tty
);
2793 args
= strip_bg_char (args
, &async_exec
);
2794 args_chain
= make_cleanup (xfree
, args
);
2796 attach_target
= find_attach_target ();
2798 prepare_execution_command (attach_target
, async_exec
);
2800 if (non_stop
&& !attach_target
->to_supports_non_stop (attach_target
))
2801 error (_("Cannot attach to this target in non-stop mode"));
2803 attach_target
->to_attach (attach_target
, args
, from_tty
);
2804 /* to_attach should push the target, so after this point we
2805 shouldn't refer to attach_target again. */
2806 attach_target
= NULL
;
2808 /* Set up the "saved terminal modes" of the inferior
2809 based on what modes we are starting it with. */
2810 target_terminal_init ();
2812 /* Install inferior's terminal modes. This may look like a no-op,
2813 as we've just saved them above, however, this does more than
2814 restore terminal settings:
2816 - installs a SIGINT handler that forwards SIGINT to the inferior.
2817 Otherwise a Ctrl-C pressed just while waiting for the initial
2818 stop would end up as a spurious Quit.
2820 - removes stdin from the event loop, which we need if attaching
2821 in the foreground, otherwise on targets that report an initial
2822 stop on attach (which are most) we'd process input/commands
2823 while we're in the event loop waiting for that stop. That is,
2824 before the attach continuation runs and the command is really
2826 target_terminal_inferior ();
2828 /* Set up execution context to know that we should return from
2829 wait_for_inferior as soon as the target reports a stop. */
2830 init_wait_for_inferior ();
2831 clear_proceed_status (0);
2833 inferior
->needs_setup
= 1;
2835 if (target_is_non_stop_p ())
2837 /* If we find that the current thread isn't stopped, explicitly
2838 do so now, because we're going to install breakpoints and
2842 /* The user requested an `attach&'; stop just one thread. */
2843 target_stop (inferior_ptid
);
2845 /* The user requested an `attach', so stop all threads of this
2847 target_stop (pid_to_ptid (ptid_get_pid (inferior_ptid
)));
2850 mode
= async_exec
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_STOP
;
2852 /* Some system don't generate traps when attaching to inferior.
2853 E.g. Mach 3 or GNU hurd. */
2854 if (!target_attach_no_wait
)
2856 struct attach_command_continuation_args
*a
;
2858 /* Careful here. See comments in inferior.h. Basically some
2859 OSes don't ignore SIGSTOPs on continue requests anymore. We
2860 need a way for handle_inferior_event to reset the stop_signal
2861 variable after an attach, and this is what
2862 STOP_QUIETLY_NO_SIGSTOP is for. */
2863 inferior
->control
.stop_soon
= STOP_QUIETLY_NO_SIGSTOP
;
2865 /* Wait for stop. */
2866 a
= XNEW (struct attach_command_continuation_args
);
2867 a
->args
= xstrdup (args
);
2868 a
->from_tty
= from_tty
;
2870 add_inferior_continuation (attach_command_continuation
, a
,
2871 attach_command_continuation_free_args
);
2872 /* Done with ARGS. */
2873 do_cleanups (args_chain
);
2875 if (!target_is_async_p ())
2876 mark_infrun_async_event_handler ();
2880 /* Done with ARGS. */
2881 do_cleanups (args_chain
);
2883 attach_post_wait (args
, from_tty
, mode
);
2886 /* We had just found out that the target was already attached to an
2887 inferior. PTID points at a thread of this new inferior, that is
2888 the most likely to be stopped right now, but not necessarily so.
2889 The new inferior is assumed to be already added to the inferior
2890 list at this point. If LEAVE_RUNNING, then leave the threads of
2891 this inferior running, except those we've explicitly seen reported
2895 notice_new_inferior (ptid_t ptid
, int leave_running
, int from_tty
)
2897 enum attach_post_wait_mode mode
2898 = leave_running
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_NOTHING
;
2900 gdb::optional
<scoped_restore_current_thread
> restore_thread
;
2902 if (inferior_ptid
!= null_ptid
)
2903 restore_thread
.emplace ();
2905 /* Avoid reading registers -- we haven't fetched the target
2907 switch_to_thread_no_regs (find_thread_ptid (ptid
));
2909 /* When we "notice" a new inferior we need to do all the things we
2910 would normally do if we had just attached to it. */
2912 if (is_executing (inferior_ptid
))
2914 struct attach_command_continuation_args
*a
;
2915 struct inferior
*inferior
= current_inferior ();
2917 /* We're going to install breakpoints, and poke at memory,
2918 ensure that the inferior is stopped for a moment while we do
2920 target_stop (inferior_ptid
);
2922 inferior
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
2924 /* Wait for stop before proceeding. */
2925 a
= XNEW (struct attach_command_continuation_args
);
2926 a
->args
= xstrdup ("");
2927 a
->from_tty
= from_tty
;
2929 add_inferior_continuation (attach_command_continuation
, a
,
2930 attach_command_continuation_free_args
);
2935 attach_post_wait ("" /* args */, from_tty
, mode
);
2940 * takes a program previously attached to and detaches it.
2941 * The program resumes execution and will no longer stop
2942 * on signals, etc. We better not have left any breakpoints
2943 * in the program or it'll die when it hits one. For this
2944 * to work, it may be necessary for the process to have been
2945 * previously attached. It *might* work if the program was
2946 * started via the normal ptrace (PTRACE_TRACEME).
2950 detach_command (char *args
, int from_tty
)
2952 dont_repeat (); /* Not for the faint of heart. */
2954 if (ptid_equal (inferior_ptid
, null_ptid
))
2955 error (_("The program is not being run."));
2957 query_if_trace_running (from_tty
);
2959 disconnect_tracing ();
2961 target_detach (args
, from_tty
);
2963 /* The current inferior process was just detached successfully. Get
2964 rid of breakpoints that no longer make sense. Note we don't do
2965 this within target_detach because that is also used when
2966 following child forks, and in that case we will want to transfer
2967 breakpoints to the child, not delete them. */
2968 breakpoint_init_inferior (inf_exited
);
2970 /* If the solist is global across inferiors, don't clear it when we
2971 detach from a single inferior. */
2972 if (!gdbarch_has_global_solist (target_gdbarch ()))
2973 no_shared_libraries (NULL
, from_tty
);
2975 /* If we still have inferiors to debug, then don't mess with their
2977 if (!have_inferiors ())
2978 init_thread_list ();
2980 if (deprecated_detach_hook
)
2981 deprecated_detach_hook ();
2984 /* Disconnect from the current target without resuming it (leaving it
2985 waiting for a debugger).
2987 We'd better not have left any breakpoints in the program or the
2988 next debugger will get confused. Currently only supported for some
2989 remote targets, since the normal attach mechanisms don't work on
2990 stopped processes on some native platforms (e.g. GNU/Linux). */
2993 disconnect_command (char *args
, int from_tty
)
2995 dont_repeat (); /* Not for the faint of heart. */
2996 query_if_trace_running (from_tty
);
2997 disconnect_tracing ();
2998 target_disconnect (args
, from_tty
);
2999 no_shared_libraries (NULL
, from_tty
);
3000 init_thread_list ();
3001 if (deprecated_detach_hook
)
3002 deprecated_detach_hook ();
3006 interrupt_target_1 (int all_threads
)
3011 ptid
= minus_one_ptid
;
3013 ptid
= inferior_ptid
;
3018 target_interrupt (ptid
);
3020 /* Tag the thread as having been explicitly requested to stop, so
3021 other parts of gdb know not to resume this thread automatically,
3022 if it was stopped due to an internal event. Limit this to
3023 non-stop mode, as when debugging a multi-threaded application in
3024 all-stop mode, we will only get one stop event --- it's undefined
3025 which thread will report the event. */
3027 set_stop_requested (ptid
, 1);
3031 Stop the execution of the target while running in async mode, in
3032 the background. In all-stop, stop the whole process. In non-stop
3033 mode, stop the current thread only by default, or stop all threads
3034 if the `-a' switch is used. */
3037 interrupt_command (char *args
, int from_tty
)
3039 if (target_can_async_p ())
3041 int all_threads
= 0;
3043 dont_repeat (); /* Not for the faint of heart. */
3046 && startswith (args
, "-a"))
3049 if (!non_stop
&& all_threads
)
3050 error (_("-a is meaningless in all-stop mode."));
3052 interrupt_target_1 (all_threads
);
3056 /* See inferior.h. */
3059 default_print_float_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
3060 struct frame_info
*frame
, const char *args
)
3063 int printed_something
= 0;
3066 regnum
< gdbarch_num_regs (gdbarch
)
3067 + gdbarch_num_pseudo_regs (gdbarch
);
3070 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, float_reggroup
))
3072 printed_something
= 1;
3073 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
3076 if (!printed_something
)
3077 fprintf_filtered (file
, "No floating-point info "
3078 "available for this processor.\n");
3082 info_float_command (char *args
, int from_tty
)
3084 struct frame_info
*frame
;
3086 if (!target_has_registers
)
3087 error (_("The program has no registers now."));
3089 frame
= get_selected_frame (NULL
);
3090 gdbarch_print_float_info (get_frame_arch (frame
), gdb_stdout
, frame
, args
);
3094 unset_command (char *args
, int from_tty
)
3096 printf_filtered (_("\"unset\" must be followed by the "
3097 "name of an unset subcommand.\n"));
3098 help_list (unsetlist
, "unset ", all_commands
, gdb_stdout
);
3101 /* Implement `info proc' family of commands. */
3104 info_proc_cmd_1 (char *args
, enum info_proc_what what
, int from_tty
)
3106 struct gdbarch
*gdbarch
= get_current_arch ();
3108 if (!target_info_proc (args
, what
))
3110 if (gdbarch_info_proc_p (gdbarch
))
3111 gdbarch_info_proc (gdbarch
, args
, what
);
3113 error (_("Not supported on this target."));
3117 /* Implement `info proc' when given without any futher parameters. */
3120 info_proc_cmd (char *args
, int from_tty
)
3122 info_proc_cmd_1 (args
, IP_MINIMAL
, from_tty
);
3125 /* Implement `info proc mappings'. */
3128 info_proc_cmd_mappings (char *args
, int from_tty
)
3130 info_proc_cmd_1 (args
, IP_MAPPINGS
, from_tty
);
3133 /* Implement `info proc stat'. */
3136 info_proc_cmd_stat (char *args
, int from_tty
)
3138 info_proc_cmd_1 (args
, IP_STAT
, from_tty
);
3141 /* Implement `info proc status'. */
3144 info_proc_cmd_status (char *args
, int from_tty
)
3146 info_proc_cmd_1 (args
, IP_STATUS
, from_tty
);
3149 /* Implement `info proc cwd'. */
3152 info_proc_cmd_cwd (char *args
, int from_tty
)
3154 info_proc_cmd_1 (args
, IP_CWD
, from_tty
);
3157 /* Implement `info proc cmdline'. */
3160 info_proc_cmd_cmdline (char *args
, int from_tty
)
3162 info_proc_cmd_1 (args
, IP_CMDLINE
, from_tty
);
3165 /* Implement `info proc exe'. */
3168 info_proc_cmd_exe (char *args
, int from_tty
)
3170 info_proc_cmd_1 (args
, IP_EXE
, from_tty
);
3173 /* Implement `info proc all'. */
3176 info_proc_cmd_all (char *args
, int from_tty
)
3178 info_proc_cmd_1 (args
, IP_ALL
, from_tty
);
3182 _initialize_infcmd (void)
3184 static struct cmd_list_element
*info_proc_cmdlist
;
3185 struct cmd_list_element
*c
= NULL
;
3186 const char *cmd_name
;
3188 /* Add the filename of the terminal connected to inferior I/O. */
3189 add_setshow_optional_filename_cmd ("inferior-tty", class_run
,
3190 &inferior_io_terminal_scratch
, _("\
3191 Set terminal for future runs of program being debugged."), _("\
3192 Show terminal for future runs of program being debugged."), _("\
3193 Usage: set inferior-tty [TTY]\n\n\
3194 If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3196 set_inferior_tty_command
,
3197 show_inferior_tty_command
,
3198 &setlist
, &showlist
);
3199 cmd_name
= "inferior-tty";
3200 c
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
3201 gdb_assert (c
!= NULL
);
3202 add_alias_cmd ("tty", c
, class_alias
, 0, &cmdlist
);
3205 add_setshow_string_noescape_cmd (cmd_name
, class_run
,
3206 &inferior_args_scratch
, _("\
3207 Set argument list to give program being debugged when it is started."), _("\
3208 Show argument list to give program being debugged when it is started."), _("\
3209 Follow this command with any number of args, to be passed to the program."),
3212 &setlist
, &showlist
);
3213 c
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
3214 gdb_assert (c
!= NULL
);
3215 set_cmd_completer (c
, filename_completer
);
3217 c
= add_cmd ("environment", no_class
, environment_info
, _("\
3218 The environment to give the program, or one variable's value.\n\
3219 With an argument VAR, prints the value of environment variable VAR to\n\
3220 give the program being debugged. With no arguments, prints the entire\n\
3221 environment to be given to the program."), &showlist
);
3222 set_cmd_completer (c
, noop_completer
);
3224 add_prefix_cmd ("unset", no_class
, unset_command
,
3225 _("Complement to certain \"set\" commands."),
3226 &unsetlist
, "unset ", 0, &cmdlist
);
3228 c
= add_cmd ("environment", class_run
, unset_environment_command
, _("\
3229 Cancel environment variable VAR for the program.\n\
3230 This does not affect the program until the next \"run\" command."),
3232 set_cmd_completer (c
, noop_completer
);
3234 c
= add_cmd ("environment", class_run
, set_environment_command
, _("\
3235 Set environment variable value to give the program.\n\
3236 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3237 VALUES of environment variables are uninterpreted strings.\n\
3238 This does not affect the program until the next \"run\" command."),
3240 set_cmd_completer (c
, noop_completer
);
3242 c
= add_com ("path", class_files
, path_command
, _("\
3243 Add directory DIR(s) to beginning of search path for object files.\n\
3244 $cwd in the path means the current working directory.\n\
3245 This path is equivalent to the $PATH shell variable. It is a list of\n\
3246 directories, separated by colons. These directories are searched to find\n\
3247 fully linked executable files and separately compiled object files as \
3249 set_cmd_completer (c
, filename_completer
);
3251 c
= add_cmd ("paths", no_class
, path_info
, _("\
3252 Current search path for finding object files.\n\
3253 $cwd in the path means the current working directory.\n\
3254 This path is equivalent to the $PATH shell variable. It is a list of\n\
3255 directories, separated by colons. These directories are searched to find\n\
3256 fully linked executable files and separately compiled object files as \
3259 set_cmd_completer (c
, noop_completer
);
3261 add_prefix_cmd ("kill", class_run
, kill_command
,
3262 _("Kill execution of program being debugged."),
3263 &killlist
, "kill ", 0, &cmdlist
);
3265 add_com ("attach", class_run
, attach_command
, _("\
3266 Attach to a process or file outside of GDB.\n\
3267 This command attaches to another target, of the same type as your last\n\
3268 \"target\" command (\"info files\" will show your target stack).\n\
3269 The command may take as argument a process id or a device file.\n\
3270 For a process id, you must have permission to send the process a signal,\n\
3271 and it must have the same effective uid as the debugger.\n\
3272 When using \"attach\" with a process id, the debugger finds the\n\
3273 program running in the process, looking first in the current working\n\
3274 directory, or (if not found there) using the source file search path\n\
3275 (see the \"directory\" command). You can also use the \"file\" command\n\
3276 to specify the program, and to load its symbol table."));
3278 add_prefix_cmd ("detach", class_run
, detach_command
, _("\
3279 Detach a process or file previously attached.\n\
3280 If a process, it is no longer traced, and it continues its execution. If\n\
3281 you were debugging a file, the file is closed and gdb no longer accesses it."),
3282 &detachlist
, "detach ", 0, &cmdlist
);
3284 add_com ("disconnect", class_run
, disconnect_command
, _("\
3285 Disconnect from a target.\n\
3286 The target will wait for another debugger to connect. Not available for\n\
3289 c
= add_com ("signal", class_run
, signal_command
, _("\
3290 Continue program with the specified signal.\n\
3291 Usage: signal SIGNAL\n\
3292 The SIGNAL argument is processed the same as the handle command.\n\
3294 An argument of \"0\" means continue the program without sending it a signal.\n\
3295 This is useful in cases where the program stopped because of a signal,\n\
3296 and you want to resume the program while discarding the signal.\n\
3298 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3299 the current thread only."));
3300 set_cmd_completer (c
, signal_completer
);
3302 c
= add_com ("queue-signal", class_run
, queue_signal_command
, _("\
3303 Queue a signal to be delivered to the current thread when it is resumed.\n\
3304 Usage: queue-signal SIGNAL\n\
3305 The SIGNAL argument is processed the same as the handle command.\n\
3306 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3308 An argument of \"0\" means remove any currently queued signal from\n\
3309 the current thread. This is useful in cases where the program stopped\n\
3310 because of a signal, and you want to resume it while discarding the signal.\n\
3312 In a multi-threaded program the signal is queued with, or discarded from,\n\
3313 the current thread only."));
3314 set_cmd_completer (c
, signal_completer
);
3316 add_com ("stepi", class_run
, stepi_command
, _("\
3317 Step one instruction exactly.\n\
3319 Argument N means step N times (or till program stops for another \
3321 add_com_alias ("si", "stepi", class_alias
, 0);
3323 add_com ("nexti", class_run
, nexti_command
, _("\
3324 Step one instruction, but proceed through subroutine calls.\n\
3326 Argument N means step N times (or till program stops for another \
3328 add_com_alias ("ni", "nexti", class_alias
, 0);
3330 add_com ("finish", class_run
, finish_command
, _("\
3331 Execute until selected stack frame returns.\n\
3333 Upon return, the value returned is printed and put in the value history."));
3334 add_com_alias ("fin", "finish", class_run
, 1);
3336 add_com ("next", class_run
, next_command
, _("\
3337 Step program, proceeding through subroutine calls.\n\
3339 Unlike \"step\", if the current source line calls a subroutine,\n\
3340 this command does not enter the subroutine, but instead steps over\n\
3341 the call, in effect treating it as a single source line."));
3342 add_com_alias ("n", "next", class_run
, 1);
3344 add_com ("step", class_run
, step_command
, _("\
3345 Step program until it reaches a different source line.\n\
3347 Argument N means step N times (or till program stops for another \
3349 add_com_alias ("s", "step", class_run
, 1);
3351 c
= add_com ("until", class_run
, until_command
, _("\
3352 Execute until the program reaches a source line greater than the current\n\
3353 or a specified location (same args as break command) within the current \
3355 set_cmd_completer (c
, location_completer
);
3356 add_com_alias ("u", "until", class_run
, 1);
3358 c
= add_com ("advance", class_run
, advance_command
, _("\
3359 Continue the program up to the given location (same form as args for break \
3361 Execution will also stop upon exit from the current stack frame."));
3362 set_cmd_completer (c
, location_completer
);
3364 c
= add_com ("jump", class_run
, jump_command
, _("\
3365 Continue program being debugged at specified line or address.\n\
3366 Usage: jump <location>\n\
3367 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3368 for an address to start at."));
3369 set_cmd_completer (c
, location_completer
);
3370 add_com_alias ("j", "jump", class_run
, 1);
3372 add_com ("continue", class_run
, continue_command
, _("\
3373 Continue program being debugged, after signal or breakpoint.\n\
3374 Usage: continue [N]\n\
3375 If proceeding from breakpoint, a number N may be used as an argument,\n\
3376 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3377 the breakpoint won't break until the Nth time it is reached).\n\
3379 If non-stop mode is enabled, continue only the current thread,\n\
3380 otherwise all the threads in the program are continued. To \n\
3381 continue all stopped threads in non-stop mode, use the -a option.\n\
3382 Specifying -a and an ignore count simultaneously is an error."));
3383 add_com_alias ("c", "cont", class_run
, 1);
3384 add_com_alias ("fg", "cont", class_run
, 1);
3386 c
= add_com ("run", class_run
, run_command
, _("\
3387 Start debugged program. You may specify arguments to give it.\n\
3388 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3389 shell that will start the program (specified by the \"$SHELL\"\
3390 environment\nvariable). Input and output redirection with \">\",\
3391 \"<\", or \">>\"\nare also allowed.\n\n\
3392 With no arguments, uses arguments last specified (with \"run\" \
3393 or \"set args\").\n\
3394 To cancel previous arguments and run with no arguments,\n\
3395 use \"set args\" without arguments.\n\
3396 To start the inferior without using a shell, use \"set \
3397 startup-with-shell off\"."));
3398 set_cmd_completer (c
, filename_completer
);
3399 add_com_alias ("r", "run", class_run
, 1);
3401 c
= add_com ("start", class_run
, start_command
, _("\
3402 Run the debugged program until the beginning of the main procedure.\n\
3403 You may specify arguments to give to your program, just as with the\n\
3404 \"run\" command."));
3405 set_cmd_completer (c
, filename_completer
);
3407 add_com ("interrupt", class_run
, interrupt_command
,
3408 _("Interrupt the execution of the debugged program.\n\
3409 If non-stop mode is enabled, interrupt only the current thread,\n\
3410 otherwise all the threads in the program are stopped. To \n\
3411 interrupt all running threads in non-stop mode, use the -a option."));
3413 c
= add_info ("registers", info_registers_command
, _("\
3414 List of integer registers and their contents, for selected stack frame.\n\
3415 Register name as argument means describe only that register."));
3416 add_info_alias ("r", "registers", 1);
3417 set_cmd_completer (c
, reg_or_group_completer
);
3419 c
= add_info ("all-registers", info_all_registers_command
, _("\
3420 List of all registers and their contents, for selected stack frame.\n\
3421 Register name as argument means describe only that register."));
3422 set_cmd_completer (c
, reg_or_group_completer
);
3424 add_info ("program", info_program_command
,
3425 _("Execution status of the program."));
3427 add_info ("float", info_float_command
,
3428 _("Print the status of the floating point unit\n"));
3430 add_info ("vector", info_vector_command
,
3431 _("Print the status of the vector unit\n"));
3433 add_prefix_cmd ("proc", class_info
, info_proc_cmd
,
3435 Show /proc process information about any running process.\n\
3436 Specify any process id, or use the program being debugged by default."),
3437 &info_proc_cmdlist
, "info proc ",
3438 1/*allow-unknown*/, &infolist
);
3440 add_cmd ("mappings", class_info
, info_proc_cmd_mappings
, _("\
3441 List of mapped memory regions."),
3442 &info_proc_cmdlist
);
3444 add_cmd ("stat", class_info
, info_proc_cmd_stat
, _("\
3445 List process info from /proc/PID/stat."),
3446 &info_proc_cmdlist
);
3448 add_cmd ("status", class_info
, info_proc_cmd_status
, _("\
3449 List process info from /proc/PID/status."),
3450 &info_proc_cmdlist
);
3452 add_cmd ("cwd", class_info
, info_proc_cmd_cwd
, _("\
3453 List current working directory of the process."),
3454 &info_proc_cmdlist
);
3456 add_cmd ("cmdline", class_info
, info_proc_cmd_cmdline
, _("\
3457 List command line arguments of the process."),
3458 &info_proc_cmdlist
);
3460 add_cmd ("exe", class_info
, info_proc_cmd_exe
, _("\
3461 List absolute filename for executable of the process."),
3462 &info_proc_cmdlist
);
3464 add_cmd ("all", class_info
, info_proc_cmd_all
, _("\
3465 List all available /proc info."),
3466 &info_proc_cmdlist
);