1 /* Memory-access and commands for "inferior" process, for GDB.
3 Copyright (C) 1986-2020 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"
27 #include "gdbsupport/environ.h"
35 #include "completer.h"
38 #include "reggroups.h"
42 #include "observable.h"
43 #include "target-descriptions.h"
44 #include "user-regs.h"
45 #include "gdbthread.h"
47 #include "inline-frame.h"
48 #include "tracepoint.h"
50 #include "continuations.h"
52 #include "thread-fsm.h"
56 #include "gdbsupport/gdb_optional.h"
58 #include "cli/cli-style.h"
60 /* Local functions: */
62 static void until_next_command (int);
64 static void step_1 (int, int, const char *);
66 #define ERROR_NO_INFERIOR \
67 if (!target_has_execution) error (_("The program is not being run."));
69 /* Scratch area where string containing arguments to give to the
70 program will be stored by 'set args'. As soon as anything is
71 stored, notice_args_set will move it into per-inferior storage.
72 Arguments are separated by spaces. Empty string (pointer to '\0')
75 static char *inferior_args_scratch
;
77 /* Scratch area where the new cwd will be stored by 'set cwd'. */
79 static char *inferior_cwd_scratch
;
81 /* Scratch area where 'set inferior-tty' will store user-provided value.
82 We'll immediate copy it into per-inferior storage. */
84 static char *inferior_io_terminal_scratch
;
86 /* Pid of our debugged inferior, or 0 if no inferior now.
87 Since various parts of infrun.c test this to see whether there is a program
88 being debugged it should be nonzero (currently 3 is used) for remote
93 /* Nonzero if stopped due to completion of a stack dummy routine. */
95 enum stop_stack_kind stop_stack_dummy
;
97 /* Nonzero if stopped due to a random (unexpected) signal in inferior
100 int stopped_by_random_signal
;
103 /* Accessor routines. */
105 /* Set the io terminal for the current inferior. Ownership of
106 TERMINAL_NAME is not transferred. */
109 set_inferior_io_terminal (const char *terminal_name
)
111 xfree (current_inferior ()->terminal
);
113 if (terminal_name
!= NULL
&& *terminal_name
!= '\0')
114 current_inferior ()->terminal
= xstrdup (terminal_name
);
116 current_inferior ()->terminal
= NULL
;
120 get_inferior_io_terminal (void)
122 return current_inferior ()->terminal
;
126 set_inferior_tty_command (const char *args
, int from_tty
,
127 struct cmd_list_element
*c
)
129 /* CLI has assigned the user-provided value to inferior_io_terminal_scratch.
130 Now route it to current inferior. */
131 set_inferior_io_terminal (inferior_io_terminal_scratch
);
135 show_inferior_tty_command (struct ui_file
*file
, int from_tty
,
136 struct cmd_list_element
*c
, const char *value
)
138 /* Note that we ignore the passed-in value in favor of computing it
140 const char *inferior_io_terminal
= get_inferior_io_terminal ();
142 if (inferior_io_terminal
== NULL
)
143 inferior_io_terminal
= "";
144 fprintf_filtered (gdb_stdout
,
145 _("Terminal for future runs of program being debugged "
146 "is \"%s\".\n"), inferior_io_terminal
);
150 get_inferior_args (void)
152 if (current_inferior ()->argc
!= 0)
154 gdb::array_view
<char * const> args (current_inferior ()->argv
,
155 current_inferior ()->argc
);
156 std::string n
= construct_inferior_arguments (args
);
157 set_inferior_args (n
.c_str ());
160 if (current_inferior ()->args
== NULL
)
161 current_inferior ()->args
= xstrdup ("");
163 return current_inferior ()->args
;
166 /* Set the arguments for the current inferior. Ownership of
167 NEWARGS is not transferred. */
170 set_inferior_args (const char *newargs
)
172 xfree (current_inferior ()->args
);
173 current_inferior ()->args
= newargs
? xstrdup (newargs
) : NULL
;
174 current_inferior ()->argc
= 0;
175 current_inferior ()->argv
= 0;
179 set_inferior_args_vector (int argc
, char **argv
)
181 current_inferior ()->argc
= argc
;
182 current_inferior ()->argv
= argv
;
185 /* Notice when `set args' is run. */
188 set_args_command (const char *args
, int from_tty
, struct cmd_list_element
*c
)
190 /* CLI has assigned the user-provided value to inferior_args_scratch.
191 Now route it to current inferior. */
192 set_inferior_args (inferior_args_scratch
);
195 /* Notice when `show args' is run. */
198 show_args_command (struct ui_file
*file
, int from_tty
,
199 struct cmd_list_element
*c
, const char *value
)
201 /* Note that we ignore the passed-in value in favor of computing it
203 deprecated_show_value_hack (file
, from_tty
, c
, get_inferior_args ());
206 /* See gdbsupport/common-inferior.h. */
209 set_inferior_cwd (const char *cwd
)
211 struct inferior
*inf
= current_inferior ();
213 gdb_assert (inf
!= NULL
);
218 inf
->cwd
.reset (xstrdup (cwd
));
221 /* See gdbsupport/common-inferior.h. */
226 return current_inferior ()->cwd
.get ();
229 /* Handle the 'set cwd' command. */
232 set_cwd_command (const char *args
, int from_tty
, struct cmd_list_element
*c
)
234 if (*inferior_cwd_scratch
== '\0')
235 set_inferior_cwd (NULL
);
237 set_inferior_cwd (inferior_cwd_scratch
);
240 /* Handle the 'show cwd' command. */
243 show_cwd_command (struct ui_file
*file
, int from_tty
,
244 struct cmd_list_element
*c
, const char *value
)
246 const char *cwd
= get_inferior_cwd ();
249 fprintf_filtered (gdb_stdout
,
251 You have not set the inferior's current working directory.\n\
252 The inferior will inherit GDB's cwd if native debugging, or the remote\n\
253 server's cwd if remote debugging.\n"));
255 fprintf_filtered (gdb_stdout
,
256 _("Current working directory that will be used "
257 "when starting the inferior is \"%s\".\n"), cwd
);
261 /* This function strips the '&' character (indicating background
262 execution) that is added as *the last* of the arguments ARGS of a
263 command. A copy of the incoming ARGS without the '&' is returned,
264 unless the resulting string after stripping is empty, in which case
265 NULL is returned. *BG_CHAR_P is an output boolean that indicates
266 whether the '&' character was found. */
268 static gdb::unique_xmalloc_ptr
<char>
269 strip_bg_char (const char *args
, int *bg_char_p
)
273 if (args
== NULL
|| *args
== '\0')
279 p
= args
+ strlen (args
);
283 while (p
> args
&& isspace (p
[-1]))
288 return gdb::unique_xmalloc_ptr
<char>
289 (savestring (args
, p
- args
));
291 return gdb::unique_xmalloc_ptr
<char> (nullptr);
295 return make_unique_xstrdup (args
);
298 /* Common actions to take after creating any sort of inferior, by any
299 means (running, attaching, connecting, et cetera). The target
300 should be stopped. */
303 post_create_inferior (struct target_ops
*target
, int from_tty
)
306 /* Be sure we own the terminal in case write operations are performed. */
307 target_terminal::ours_for_output ();
309 /* If the target hasn't taken care of this already, do it now.
310 Targets which need to access registers during to_open,
311 to_create_inferior, or to_attach should do it earlier; but many
313 target_find_description ();
315 /* Now that we know the register layout, retrieve current PC. But
316 if the PC is unavailable (e.g., we're opening a core file with
317 missing registers info), ignore it. */
318 thread_info
*thr
= inferior_thread ();
320 thr
->suspend
.stop_pc
= 0;
323 regcache
*rc
= get_thread_regcache (thr
);
324 thr
->suspend
.stop_pc
= regcache_read_pc (rc
);
326 catch (const gdb_exception_error
&ex
)
328 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
334 const unsigned solib_add_generation
335 = current_program_space
->solib_add_generation
;
337 /* Create the hooks to handle shared library load and unload
339 solib_create_inferior_hook (from_tty
);
341 if (current_program_space
->solib_add_generation
== solib_add_generation
)
343 /* The platform-specific hook should load initial shared libraries,
344 but didn't. FROM_TTY will be incorrectly 0 but such solib
345 targets should be fixed anyway. Call it only after the solib
346 target has been initialized by solib_create_inferior_hook. */
349 warning (_("platform-specific solib_create_inferior_hook did "
350 "not load initial shared libraries."));
352 /* If the solist is global across processes, there's no need to
354 if (!gdbarch_has_global_solist (target_gdbarch ()))
355 solib_add (NULL
, 0, auto_solib_add
);
359 /* If the user sets watchpoints before execution having started,
360 then she gets software watchpoints, because GDB can't know which
361 target will end up being pushed, or if it supports hardware
362 watchpoints or not. breakpoint_re_set takes care of promoting
363 watchpoints to hardware watchpoints if possible, however, if this
364 new inferior doesn't load shared libraries or we don't pull in
365 symbols from any other source on this target/arch,
366 breakpoint_re_set is never called. Call it now so that software
367 watchpoints get a chance to be promoted to hardware watchpoints
368 if the now pushed target supports hardware watchpoints. */
369 breakpoint_re_set ();
371 gdb::observers::inferior_created
.notify (target
, from_tty
);
374 /* Kill the inferior if already running. This function is designed
375 to be called when we are about to start the execution of the program
376 from the beginning. Ask the user to confirm that he wants to restart
377 the program being debugged when FROM_TTY is non-null. */
380 kill_if_already_running (int from_tty
)
382 if (inferior_ptid
!= null_ptid
&& target_has_execution
)
384 /* Bail out before killing the program if we will not be able to
386 target_require_runnable ();
389 && !query (_("The program being debugged has been started already.\n\
390 Start it from the beginning? ")))
391 error (_("Program not restarted."));
396 /* See inferior.h. */
399 prepare_execution_command (struct target_ops
*target
, int background
)
401 /* If we get a request for running in the bg but the target
402 doesn't support it, error out. */
403 if (background
&& !target
->can_async_p ())
404 error (_("Asynchronous execution not supported on this target."));
408 /* If we get a request for running in the fg, then we need to
409 simulate synchronous (fg) execution. Note no cleanup is
410 necessary for this. stdin is re-enabled whenever an error
411 reaches the top level. */
412 all_uis_on_sync_execution_starting ();
416 /* Determine how the new inferior will behave. */
420 /* Run program without any explicit stop during startup. */
423 /* Stop at the beginning of the program's main function. */
426 /* Stop at the first instruction of the program. */
427 RUN_STOP_AT_FIRST_INSN
430 /* Implement the "run" command. Force a stop during program start if
431 requested by RUN_HOW. */
434 run_command_1 (const char *args
, int from_tty
, enum run_how run_how
)
436 const char *exec_file
;
437 struct ui_out
*uiout
= current_uiout
;
438 struct target_ops
*run_target
;
443 kill_if_already_running (from_tty
);
445 init_wait_for_inferior ();
446 clear_breakpoint_hit_counts ();
448 /* Clean up any leftovers from other runs. Some other things from
449 this function should probably be moved into target_pre_inferior. */
450 target_pre_inferior (from_tty
);
452 /* The comment here used to read, "The exec file is re-read every
453 time we do a generic_mourn_inferior, so we just have to worry
454 about the symbol file." The `generic_mourn_inferior' function
455 gets called whenever the program exits. However, suppose the
456 program exits, and *then* the executable file changes? We need
457 to check again here. Since reopen_exec_file doesn't do anything
458 if the timestamp hasn't changed, I don't see the harm. */
462 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
463 args
= stripped
.get ();
465 /* Do validation and preparation before possibly changing anything
468 run_target
= find_run_target ();
470 prepare_execution_command (run_target
, async_exec
);
472 if (non_stop
&& !run_target
->supports_non_stop ())
473 error (_("The target does not support running in non-stop mode."));
475 /* Done. Can now set breakpoints, change inferior args, etc. */
477 /* Insert temporary breakpoint in main function if requested. */
478 if (run_how
== RUN_STOP_AT_MAIN
)
480 std::string arg
= string_printf ("-qualified %s", main_name ());
481 tbreak_command (arg
.c_str (), 0);
484 exec_file
= get_exec_file (0);
486 /* We keep symbols from add-symbol-file, on the grounds that the
487 user might want to add some symbols before running the program
488 (right?). But sometimes (dynamic loading where the user manually
489 introduces the new symbols with add-symbol-file), the code which
490 the symbols describe does not persist between runs. Currently
491 the user has to manually nuke all symbols between runs if they
492 want them to go away (PR 2207). This is probably reasonable. */
494 /* If there were other args, beside '&', process them. */
496 set_inferior_args (args
);
500 uiout
->field_string (NULL
, "Starting program");
503 uiout
->field_string ("execfile", exec_file
);
505 /* We call get_inferior_args() because we might need to compute
507 uiout
->field_string ("infargs", get_inferior_args ());
512 /* We call get_inferior_args() because we might need to compute
514 run_target
->create_inferior (exec_file
,
515 std::string (get_inferior_args ()),
516 current_inferior ()->environment
.envp (),
518 /* to_create_inferior should push the target, so after this point we
519 shouldn't refer to run_target again. */
522 /* We're starting off a new process. When we get out of here, in
523 non-stop mode, finish the state of all threads of that process,
524 but leave other threads alone, as they may be stopped in internal
525 events --- the frontend shouldn't see them as stopped. In
526 all-stop, always finish the state of all threads, as we may be
527 resuming more than just the new process. */
528 process_stratum_target
*finish_target
;
532 finish_target
= current_inferior ()->process_target ();
533 finish_ptid
= ptid_t (current_inferior ()->pid
);
537 finish_target
= nullptr;
538 finish_ptid
= minus_one_ptid
;
540 scoped_finish_thread_state
finish_state (finish_target
, finish_ptid
);
542 /* Pass zero for FROM_TTY, because at this point the "run" command
543 has done its thing; now we are setting up the running program. */
544 post_create_inferior (current_top_target (), 0);
546 /* Queue a pending event so that the program stops immediately. */
547 if (run_how
== RUN_STOP_AT_FIRST_INSN
)
549 thread_info
*thr
= inferior_thread ();
550 thr
->suspend
.waitstatus_pending_p
= 1;
551 thr
->suspend
.waitstatus
.kind
= TARGET_WAITKIND_STOPPED
;
552 thr
->suspend
.waitstatus
.value
.sig
= GDB_SIGNAL_0
;
555 /* Start the target running. Do not use -1 continuation as it would skip
556 breakpoint right at the entry point. */
557 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0
);
559 /* Since there was no error, there's no need to finish the thread
561 finish_state
.release ();
565 run_command (const char *args
, int from_tty
)
567 run_command_1 (args
, from_tty
, RUN_NORMAL
);
570 /* Start the execution of the program up until the beginning of the main
574 start_command (const char *args
, int from_tty
)
576 /* Some languages such as Ada need to search inside the program
577 minimal symbols for the location where to put the temporary
578 breakpoint before starting. */
579 if (!have_minimal_symbols ())
580 error (_("No symbol table loaded. Use the \"file\" command."));
582 /* Run the program until reaching the main procedure... */
583 run_command_1 (args
, from_tty
, RUN_STOP_AT_MAIN
);
586 /* Start the execution of the program stopping at the first
590 starti_command (const char *args
, int from_tty
)
592 run_command_1 (args
, from_tty
, RUN_STOP_AT_FIRST_INSN
);
596 proceed_thread_callback (struct thread_info
*thread
, void *arg
)
598 /* We go through all threads individually instead of compressing
599 into a single target `resume_all' request, because some threads
600 may be stopped in internal breakpoints/events, or stopped waiting
601 for its turn in the displaced stepping queue (that is, they are
602 running && !executing). The target side has no idea about why
603 the thread is stopped, so a `resume_all' command would resume too
604 much. If/when GDB gains a way to tell the target `hold this
605 thread stopped until I say otherwise', then we can optimize
607 if (thread
->state
!= THREAD_STOPPED
)
610 if (!thread
->inf
->has_execution ())
613 switch_to_thread (thread
);
614 clear_proceed_status (0);
615 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
620 ensure_valid_thread (void)
622 if (inferior_ptid
== null_ptid
623 || inferior_thread ()->state
== THREAD_EXITED
)
624 error (_("Cannot execute this command without a live selected thread."));
627 /* If the user is looking at trace frames, any resumption of execution
628 is likely to mix up recorded and live target data. So simply
629 disallow those commands. */
632 ensure_not_tfind_mode (void)
634 if (get_traceframe_number () >= 0)
635 error (_("Cannot execute this command while looking at trace frames."));
638 /* Throw an error indicating the current thread is running. */
641 error_is_running (void)
643 error (_("Cannot execute this command while "
644 "the selected thread is running."));
647 /* Calls error_is_running if the current thread is running. */
650 ensure_not_running (void)
652 if (inferior_thread ()->state
== THREAD_RUNNING
)
657 continue_1 (int all_threads
)
660 ensure_not_tfind_mode ();
662 if (non_stop
&& all_threads
)
664 /* Don't error out if the current thread is running, because
665 there may be other stopped threads. */
667 /* Backup current thread and selected frame and restore on scope
669 scoped_restore_current_thread restore_thread
;
671 iterate_over_threads (proceed_thread_callback
, NULL
);
673 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
675 /* If all threads in the target were already running,
676 proceed_thread_callback ends up never calling proceed,
677 and so nothing calls this to put the inferior's terminal
678 settings in effect and remove stdin from the event loop,
679 which we must when running a foreground command. E.g.:
683 <all threads are running now>
686 <no thread was resumed, but the inferior now owns the terminal>
688 target_terminal::inferior ();
693 ensure_valid_thread ();
694 ensure_not_running ();
695 clear_proceed_status (0);
696 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
700 /* continue [-a] [proceed-count] [&] */
703 continue_command (const char *args
, int from_tty
)
706 bool all_threads_p
= false;
710 /* Find out whether we must run in the background. */
711 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
712 args
= stripped
.get ();
716 if (startswith (args
, "-a"))
718 all_threads_p
= true;
719 args
+= sizeof ("-a") - 1;
725 if (!non_stop
&& all_threads_p
)
726 error (_("`-a' is meaningless in all-stop mode."));
728 if (args
!= NULL
&& all_threads_p
)
729 error (_("Can't resume all threads and specify "
730 "proceed count simultaneously."));
732 /* If we have an argument left, set proceed count of breakpoint we
739 struct thread_info
*tp
;
742 tp
= inferior_thread ();
745 process_stratum_target
*last_target
;
748 get_last_target_status (&last_target
, &last_ptid
, nullptr);
749 tp
= find_thread_ptid (last_target
, last_ptid
);
752 bs
= tp
->control
.stop_bpstat
;
754 while ((stat
= bpstat_num (&bs
, &num
)) != 0)
757 set_ignore_count (num
,
758 parse_and_eval_long (args
) - 1,
760 /* set_ignore_count prints a message ending with a period.
761 So print two spaces before "Continuing.". */
763 printf_filtered (" ");
767 if (!stopped
&& from_tty
)
770 ("Not stopped at any breakpoint; argument ignored.\n");
775 ensure_not_tfind_mode ();
777 if (!non_stop
|| !all_threads_p
)
779 ensure_valid_thread ();
780 ensure_not_running ();
783 prepare_execution_command (current_top_target (), async_exec
);
786 printf_filtered (_("Continuing.\n"));
788 continue_1 (all_threads_p
);
791 /* Record in TP the starting point of a "step" or "next" command. */
794 set_step_frame (thread_info
*tp
)
796 /* This can be removed once this function no longer implicitly relies on the
797 inferior_ptid value. */
798 gdb_assert (inferior_ptid
== tp
->ptid
);
800 frame_info
*frame
= get_current_frame ();
802 symtab_and_line sal
= find_frame_sal (frame
);
803 set_step_info (tp
, frame
, sal
);
805 CORE_ADDR pc
= get_frame_pc (frame
);
806 tp
->control
.step_start_function
= find_pc_function (pc
);
809 /* Step until outside of current statement. */
812 step_command (const char *count_string
, int from_tty
)
814 step_1 (0, 0, count_string
);
817 /* Likewise, but skip over subroutine calls as if single instructions. */
820 next_command (const char *count_string
, int from_tty
)
822 step_1 (1, 0, count_string
);
825 /* Likewise, but step only one instruction. */
828 stepi_command (const char *count_string
, int from_tty
)
830 step_1 (0, 1, count_string
);
834 nexti_command (const char *count_string
, int from_tty
)
836 step_1 (1, 1, count_string
);
839 /* Data for the FSM that manages the step/next/stepi/nexti
842 struct step_command_fsm
: public thread_fsm
844 /* How many steps left in a "step N"-like command. */
847 /* If true, this is a next/nexti, otherwise a step/stepi. */
848 int skip_subroutines
;
850 /* If true, this is a stepi/nexti, otherwise a step/step. */
853 explicit step_command_fsm (struct interp
*cmd_interp
)
854 : thread_fsm (cmd_interp
)
858 void clean_up (struct thread_info
*thread
) override
;
859 bool should_stop (struct thread_info
*thread
) override
;
860 enum async_reply_reason
do_async_reply_reason () override
;
863 /* Prepare for a step/next/etc. command. Any target resource
864 allocated here is undone in the FSM's clean_up method. */
867 step_command_fsm_prepare (struct step_command_fsm
*sm
,
868 int skip_subroutines
, int single_inst
,
869 int count
, struct thread_info
*thread
)
871 sm
->skip_subroutines
= skip_subroutines
;
872 sm
->single_inst
= single_inst
;
875 /* Leave the si command alone. */
876 if (!sm
->single_inst
|| sm
->skip_subroutines
)
877 set_longjmp_breakpoint (thread
, get_frame_id (get_current_frame ()));
879 thread
->control
.stepping_command
= 1;
882 static int prepare_one_step (thread_info
*, struct step_command_fsm
*sm
);
885 step_1 (int skip_subroutines
, int single_inst
, const char *count_string
)
889 struct thread_info
*thr
;
890 struct step_command_fsm
*step_sm
;
893 ensure_not_tfind_mode ();
894 ensure_valid_thread ();
895 ensure_not_running ();
897 gdb::unique_xmalloc_ptr
<char> stripped
898 = strip_bg_char (count_string
, &async_exec
);
899 count_string
= stripped
.get ();
901 prepare_execution_command (current_top_target (), async_exec
);
903 count
= count_string
? parse_and_eval_long (count_string
) : 1;
905 clear_proceed_status (1);
907 /* Setup the execution command state machine to handle all the COUNT
909 thr
= inferior_thread ();
910 step_sm
= new step_command_fsm (command_interp ());
911 thr
->thread_fsm
= step_sm
;
913 step_command_fsm_prepare (step_sm
, skip_subroutines
,
914 single_inst
, count
, thr
);
916 /* Do only one step for now, before returning control to the event
917 loop. Let the continuation figure out how many other steps we
918 need to do, and handle them one at the time, through
920 if (!prepare_one_step (thr
, step_sm
))
921 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
926 /* Stepped into an inline frame. Pretend that we've
928 thr
->thread_fsm
->clean_up (thr
);
929 proceeded
= normal_stop ();
931 inferior_event_handler (INF_EXEC_COMPLETE
, NULL
);
932 all_uis_check_sync_execution_done ();
936 /* Implementation of the 'should_stop' FSM method for stepping
937 commands. Called after we are done with one step operation, to
938 check whether we need to step again, before we print the prompt and
939 return control to the user. If count is > 1, returns false, as we
940 will need to keep going. */
943 step_command_fsm::should_stop (struct thread_info
*tp
)
945 if (tp
->control
.stop_step
)
947 /* There are more steps to make, and we did stop due to
948 ending a stepping range. Do another step. */
950 return prepare_one_step (tp
, this);
958 /* Implementation of the 'clean_up' FSM method for stepping commands. */
961 step_command_fsm::clean_up (struct thread_info
*thread
)
963 if (!single_inst
|| skip_subroutines
)
964 delete_longjmp_breakpoint (thread
->global_num
);
967 /* Implementation of the 'async_reply_reason' FSM method for stepping
970 enum async_reply_reason
971 step_command_fsm::do_async_reply_reason ()
973 return EXEC_ASYNC_END_STEPPING_RANGE
;
976 /* Prepare for one step in "step N". The actual target resumption is
977 done by the caller. Return true if we're done and should thus
978 report a stop to the user. Returns false if the target needs to be
982 prepare_one_step (thread_info
*tp
, struct step_command_fsm
*sm
)
984 /* This can be removed once this function no longer implicitly relies on the
985 inferior_ptid value. */
986 gdb_assert (inferior_ptid
== tp
->ptid
);
990 struct frame_info
*frame
= get_current_frame ();
994 if (!sm
->single_inst
)
998 /* Step at an inlined function behaves like "down". */
999 if (!sm
->skip_subroutines
1000 && inline_skipped_frames (tp
))
1003 const char *fn
= NULL
;
1004 symtab_and_line sal
;
1007 /* Pretend that we've ran. */
1008 resume_ptid
= user_visible_resume_ptid (1);
1009 set_running (tp
->inf
->process_target (), resume_ptid
, true);
1011 step_into_inline_frame (tp
);
1013 frame
= get_current_frame ();
1014 sal
= find_frame_sal (frame
);
1015 sym
= get_frame_function (frame
);
1018 fn
= sym
->print_name ();
1021 || !function_name_is_marked_for_skip (fn
, sal
))
1024 return prepare_one_step (tp
, sm
);
1028 pc
= get_frame_pc (frame
);
1029 find_pc_line_pc_range (pc
,
1030 &tp
->control
.step_range_start
,
1031 &tp
->control
.step_range_end
);
1033 tp
->control
.may_range_step
= 1;
1035 /* If we have no line info, switch to stepi mode. */
1036 if (tp
->control
.step_range_end
== 0 && step_stop_if_no_debug
)
1038 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1039 tp
->control
.may_range_step
= 0;
1041 else if (tp
->control
.step_range_end
== 0)
1045 if (find_pc_partial_function (pc
, &name
,
1046 &tp
->control
.step_range_start
,
1047 &tp
->control
.step_range_end
) == 0)
1048 error (_("Cannot find bounds of current function"));
1050 target_terminal::ours_for_output ();
1051 printf_filtered (_("Single stepping until exit from function %s,"
1052 "\nwhich has no line number information.\n"),
1058 /* Say we are stepping, but stop after one insn whatever it does. */
1059 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1060 if (!sm
->skip_subroutines
)
1062 Don't step over function calls, not even to functions lacking
1064 tp
->control
.step_over_calls
= STEP_OVER_NONE
;
1067 if (sm
->skip_subroutines
)
1068 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1074 sm
->set_finished ();
1079 /* Continue program at specified address. */
1082 jump_command (const char *arg
, int from_tty
)
1084 struct gdbarch
*gdbarch
= get_current_arch ();
1091 ensure_not_tfind_mode ();
1092 ensure_valid_thread ();
1093 ensure_not_running ();
1095 /* Find out whether we must run in the background. */
1096 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1097 arg
= stripped
.get ();
1099 prepare_execution_command (current_top_target (), async_exec
);
1102 error_no_arg (_("starting address"));
1104 std::vector
<symtab_and_line
> sals
1105 = decode_line_with_last_displayed (arg
, DECODE_LINE_FUNFIRSTLINE
);
1106 if (sals
.size () != 1)
1107 error (_("Unreasonable jump request"));
1109 symtab_and_line
&sal
= sals
[0];
1111 if (sal
.symtab
== 0 && sal
.pc
== 0)
1112 error (_("No source file has been specified."));
1114 resolve_sal_pc (&sal
); /* May error out. */
1116 /* See if we are trying to jump to another function. */
1117 fn
= get_frame_function (get_current_frame ());
1118 sfn
= find_pc_function (sal
.pc
);
1119 if (fn
!= NULL
&& sfn
!= fn
)
1121 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal
.line
,
1124 error (_("Not confirmed."));
1131 struct obj_section
*section
;
1133 fixup_symbol_section (sfn
, 0);
1134 section
= SYMBOL_OBJ_SECTION (symbol_objfile (sfn
), sfn
);
1135 if (section_is_overlay (section
)
1136 && !section_is_mapped (section
))
1138 if (!query (_("WARNING!!! Destination is in "
1139 "unmapped overlay! Jump anyway? ")))
1141 error (_("Not confirmed."));
1151 printf_filtered (_("Continuing at "));
1152 fputs_filtered (paddress (gdbarch
, addr
), gdb_stdout
);
1153 printf_filtered (".\n");
1156 clear_proceed_status (0);
1157 proceed (addr
, GDB_SIGNAL_0
);
1160 /* Continue program giving it specified signal. */
1163 signal_command (const char *signum_exp
, int from_tty
)
1165 enum gdb_signal oursig
;
1168 dont_repeat (); /* Too dangerous. */
1170 ensure_not_tfind_mode ();
1171 ensure_valid_thread ();
1172 ensure_not_running ();
1174 /* Find out whether we must run in the background. */
1175 gdb::unique_xmalloc_ptr
<char> stripped
1176 = strip_bg_char (signum_exp
, &async_exec
);
1177 signum_exp
= stripped
.get ();
1179 prepare_execution_command (current_top_target (), async_exec
);
1182 error_no_arg (_("signal number"));
1184 /* It would be even slicker to make signal names be valid expressions,
1185 (the type could be "enum $signal" or some such), then the user could
1186 assign them to convenience variables. */
1187 oursig
= gdb_signal_from_name (signum_exp
);
1189 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1191 /* No, try numeric. */
1192 int num
= parse_and_eval_long (signum_exp
);
1195 oursig
= GDB_SIGNAL_0
;
1197 oursig
= gdb_signal_from_command (num
);
1200 /* Look for threads other than the current that this command ends up
1201 resuming too (due to schedlock off), and warn if they'll get a
1202 signal delivered. "signal 0" is used to suppress a previous
1203 signal, but if the current thread is no longer the one that got
1204 the signal, then the user is potentially suppressing the signal
1205 of the wrong thread. */
1208 int must_confirm
= 0;
1210 /* This indicates what will be resumed. Either a single thread,
1211 a whole process, or all threads of all processes. */
1212 ptid_t resume_ptid
= user_visible_resume_ptid (0);
1213 process_stratum_target
*resume_target
1214 = user_visible_resume_target (resume_ptid
);
1216 thread_info
*current
= inferior_thread ();
1218 for (thread_info
*tp
: all_non_exited_threads (resume_target
, resume_ptid
))
1223 if (tp
->suspend
.stop_signal
!= GDB_SIGNAL_0
1224 && signal_pass_state (tp
->suspend
.stop_signal
))
1227 printf_unfiltered (_("Note:\n"));
1228 printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"),
1229 print_thread_id (tp
),
1230 gdb_signal_to_name (tp
->suspend
.stop_signal
),
1231 gdb_signal_to_string (tp
->suspend
.stop_signal
));
1237 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1238 "still deliver the signals noted above to their respective threads.\n"
1239 "Continue anyway? "),
1240 print_thread_id (inferior_thread ())))
1241 error (_("Not confirmed."));
1246 if (oursig
== GDB_SIGNAL_0
)
1247 printf_filtered (_("Continuing with no signal.\n"));
1249 printf_filtered (_("Continuing with signal %s.\n"),
1250 gdb_signal_to_name (oursig
));
1253 clear_proceed_status (0);
1254 proceed ((CORE_ADDR
) -1, oursig
);
1257 /* Queue a signal to be delivered to the current thread. */
1260 queue_signal_command (const char *signum_exp
, int from_tty
)
1262 enum gdb_signal oursig
;
1263 struct thread_info
*tp
;
1266 ensure_not_tfind_mode ();
1267 ensure_valid_thread ();
1268 ensure_not_running ();
1270 if (signum_exp
== NULL
)
1271 error_no_arg (_("signal number"));
1273 /* It would be even slicker to make signal names be valid expressions,
1274 (the type could be "enum $signal" or some such), then the user could
1275 assign them to convenience variables. */
1276 oursig
= gdb_signal_from_name (signum_exp
);
1278 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1280 /* No, try numeric. */
1281 int num
= parse_and_eval_long (signum_exp
);
1284 oursig
= GDB_SIGNAL_0
;
1286 oursig
= gdb_signal_from_command (num
);
1289 if (oursig
!= GDB_SIGNAL_0
1290 && !signal_pass_state (oursig
))
1291 error (_("Signal handling set to not pass this signal to the program."));
1293 tp
= inferior_thread ();
1294 tp
->suspend
.stop_signal
= oursig
;
1297 /* Data for the FSM that manages the until (with no argument)
1300 struct until_next_fsm
: public thread_fsm
1302 /* The thread that as current when the command was executed. */
1305 until_next_fsm (struct interp
*cmd_interp
, int thread
)
1306 : thread_fsm (cmd_interp
),
1311 bool should_stop (struct thread_info
*thread
) override
;
1312 void clean_up (struct thread_info
*thread
) override
;
1313 enum async_reply_reason
do_async_reply_reason () override
;
1316 /* Implementation of the 'should_stop' FSM method for the until (with
1320 until_next_fsm::should_stop (struct thread_info
*tp
)
1322 if (tp
->control
.stop_step
)
1328 /* Implementation of the 'clean_up' FSM method for the until (with no
1332 until_next_fsm::clean_up (struct thread_info
*thread
)
1334 delete_longjmp_breakpoint (thread
->global_num
);
1337 /* Implementation of the 'async_reply_reason' FSM method for the until
1338 (with no arg) command. */
1340 enum async_reply_reason
1341 until_next_fsm::do_async_reply_reason ()
1343 return EXEC_ASYNC_END_STEPPING_RANGE
;
1346 /* Proceed until we reach a different source line with pc greater than
1347 our current one or exit the function. We skip calls in both cases.
1349 Note that eventually this command should probably be changed so
1350 that only source lines are printed out when we hit the breakpoint
1351 we set. This may involve changes to wait_for_inferior and the
1352 proceed status code. */
1355 until_next_command (int from_tty
)
1357 struct frame_info
*frame
;
1359 struct symbol
*func
;
1360 struct symtab_and_line sal
;
1361 struct thread_info
*tp
= inferior_thread ();
1362 int thread
= tp
->global_num
;
1363 struct until_next_fsm
*sm
;
1365 clear_proceed_status (0);
1366 set_step_frame (tp
);
1368 frame
= get_current_frame ();
1370 /* Step until either exited from this function or greater
1371 than the current line (if in symbolic section) or pc (if
1374 pc
= get_frame_pc (frame
);
1375 func
= find_pc_function (pc
);
1379 struct bound_minimal_symbol msymbol
= lookup_minimal_symbol_by_pc (pc
);
1381 if (msymbol
.minsym
== NULL
)
1382 error (_("Execution is not within a known function."));
1384 tp
->control
.step_range_start
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1385 /* The upper-bound of step_range is exclusive. In order to make PC
1386 within the range, set the step_range_end with PC + 1. */
1387 tp
->control
.step_range_end
= pc
+ 1;
1391 sal
= find_pc_line (pc
, 0);
1393 tp
->control
.step_range_start
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func
));
1394 tp
->control
.step_range_end
= sal
.end
;
1396 tp
->control
.may_range_step
= 1;
1398 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1400 set_longjmp_breakpoint (tp
, get_frame_id (frame
));
1401 delete_longjmp_breakpoint_cleanup
lj_deleter (thread
);
1403 sm
= new until_next_fsm (command_interp (), tp
->global_num
);
1404 tp
->thread_fsm
= sm
;
1405 lj_deleter
.release ();
1407 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1411 until_command (const char *arg
, int from_tty
)
1416 ensure_not_tfind_mode ();
1417 ensure_valid_thread ();
1418 ensure_not_running ();
1420 /* Find out whether we must run in the background. */
1421 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1422 arg
= stripped
.get ();
1424 prepare_execution_command (current_top_target (), async_exec
);
1427 until_break_command (arg
, from_tty
, 0);
1429 until_next_command (from_tty
);
1433 advance_command (const char *arg
, int from_tty
)
1438 ensure_not_tfind_mode ();
1439 ensure_valid_thread ();
1440 ensure_not_running ();
1443 error_no_arg (_("a location"));
1445 /* Find out whether we must run in the background. */
1446 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1447 arg
= stripped
.get ();
1449 prepare_execution_command (current_top_target (), async_exec
);
1451 until_break_command (arg
, from_tty
, 1);
1454 /* Return the value of the result of a function at the end of a 'finish'
1455 command/BP. DTOR_DATA (if not NULL) can represent inferior registers
1456 right after an inferior call has finished. */
1459 get_return_value (struct value
*function
, struct type
*value_type
)
1461 regcache
*stop_regs
= get_current_regcache ();
1462 struct gdbarch
*gdbarch
= stop_regs
->arch ();
1463 struct value
*value
;
1465 value_type
= check_typedef (value_type
);
1466 gdb_assert (value_type
->code () != TYPE_CODE_VOID
);
1468 /* FIXME: 2003-09-27: When returning from a nested inferior function
1469 call, it's possible (with no help from the architecture vector)
1470 to locate and return/print a "struct return" value. This is just
1471 a more complicated case of what is already being done in the
1472 inferior function call code. In fact, when inferior function
1473 calls are made async, this will likely be made the norm. */
1475 switch (gdbarch_return_value (gdbarch
, function
, value_type
,
1478 case RETURN_VALUE_REGISTER_CONVENTION
:
1479 case RETURN_VALUE_ABI_RETURNS_ADDRESS
:
1480 case RETURN_VALUE_ABI_PRESERVES_ADDRESS
:
1481 value
= allocate_value (value_type
);
1482 gdbarch_return_value (gdbarch
, function
, value_type
, stop_regs
,
1483 value_contents_raw (value
), NULL
);
1485 case RETURN_VALUE_STRUCT_CONVENTION
:
1489 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1495 /* The captured function return value/type and its position in the
1498 struct return_value_info
1500 /* The captured return value. May be NULL if we weren't able to
1501 retrieve it. See get_return_value. */
1502 struct value
*value
;
1504 /* The return type. In some cases, we'll not be able extract the
1505 return value, but we always know the type. */
1508 /* If we captured a value, this is the value history index. */
1509 int value_history_index
;
1512 /* Helper for print_return_value. */
1515 print_return_value_1 (struct ui_out
*uiout
, struct return_value_info
*rv
)
1517 if (rv
->value
!= NULL
)
1519 struct value_print_options opts
;
1522 uiout
->text ("Value returned is ");
1523 uiout
->field_fmt ("gdb-result-var", "$%d",
1524 rv
->value_history_index
);
1525 uiout
->text (" = ");
1526 get_user_print_options (&opts
);
1528 if (opts
.finish_print
)
1531 value_print (rv
->value
, &stb
, &opts
);
1532 uiout
->field_stream ("return-value", stb
);
1535 uiout
->field_string ("return-value", _("<not displayed>"),
1536 metadata_style
.style ());
1541 std::string type_name
= type_to_string (rv
->type
);
1542 uiout
->text ("Value returned has type: ");
1543 uiout
->field_string ("return-type", type_name
.c_str ());
1545 uiout
->text (" Cannot determine contents\n");
1549 /* Print the result of a function at the end of a 'finish' command.
1550 RV points at an object representing the captured return value/type
1551 and its position in the value history. */
1554 print_return_value (struct ui_out
*uiout
, struct return_value_info
*rv
)
1556 if (rv
->type
== NULL
1557 || check_typedef (rv
->type
)->code () == TYPE_CODE_VOID
)
1562 /* print_return_value_1 can throw an exception in some
1563 circumstances. We need to catch this so that we still
1564 delete the breakpoint. */
1565 print_return_value_1 (uiout
, rv
);
1567 catch (const gdb_exception
&ex
)
1569 exception_print (gdb_stdout
, ex
);
1573 /* Data for the FSM that manages the finish command. */
1575 struct finish_command_fsm
: public thread_fsm
1577 /* The momentary breakpoint set at the function's return address in
1579 breakpoint_up breakpoint
;
1581 /* The function that we're stepping out of. */
1582 struct symbol
*function
= nullptr;
1584 /* If the FSM finishes successfully, this stores the function's
1586 struct return_value_info return_value_info
{};
1588 explicit finish_command_fsm (struct interp
*cmd_interp
)
1589 : thread_fsm (cmd_interp
)
1593 bool should_stop (struct thread_info
*thread
) override
;
1594 void clean_up (struct thread_info
*thread
) override
;
1595 struct return_value_info
*return_value () override
;
1596 enum async_reply_reason
do_async_reply_reason () override
;
1599 /* Implementation of the 'should_stop' FSM method for the finish
1600 commands. Detects whether the thread stepped out of the function
1601 successfully, and if so, captures the function's return value and
1602 marks the FSM finished. */
1605 finish_command_fsm::should_stop (struct thread_info
*tp
)
1607 struct return_value_info
*rv
= &return_value_info
;
1609 if (function
!= NULL
1610 && bpstat_find_breakpoint (tp
->control
.stop_bpstat
,
1611 breakpoint
.get ()) != NULL
)
1616 rv
->type
= TYPE_TARGET_TYPE (SYMBOL_TYPE (function
));
1617 if (rv
->type
== NULL
)
1618 internal_error (__FILE__
, __LINE__
,
1619 _("finish_command: function has no target type"));
1621 if (check_typedef (rv
->type
)->code () != TYPE_CODE_VOID
)
1625 func
= read_var_value (function
, NULL
, get_current_frame ());
1626 rv
->value
= get_return_value (func
, rv
->type
);
1627 if (rv
->value
!= NULL
)
1628 rv
->value_history_index
= record_latest_value (rv
->value
);
1631 else if (tp
->control
.stop_step
)
1633 /* Finishing from an inline frame, or reverse finishing. In
1634 either case, there's no way to retrieve the return value. */
1641 /* Implementation of the 'clean_up' FSM method for the finish
1645 finish_command_fsm::clean_up (struct thread_info
*thread
)
1647 breakpoint
.reset ();
1648 delete_longjmp_breakpoint (thread
->global_num
);
1651 /* Implementation of the 'return_value' FSM method for the finish
1654 struct return_value_info
*
1655 finish_command_fsm::return_value ()
1657 return &return_value_info
;
1660 /* Implementation of the 'async_reply_reason' FSM method for the
1663 enum async_reply_reason
1664 finish_command_fsm::do_async_reply_reason ()
1666 if (execution_direction
== EXEC_REVERSE
)
1667 return EXEC_ASYNC_END_STEPPING_RANGE
;
1669 return EXEC_ASYNC_FUNCTION_FINISHED
;
1672 /* finish_backward -- helper function for finish_command. */
1675 finish_backward (struct finish_command_fsm
*sm
)
1677 struct symtab_and_line sal
;
1678 struct thread_info
*tp
= inferior_thread ();
1680 CORE_ADDR func_addr
;
1682 pc
= get_frame_pc (get_current_frame ());
1684 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
) == 0)
1685 error (_("Cannot find bounds of current function"));
1687 sal
= find_pc_line (func_addr
, 0);
1689 tp
->control
.proceed_to_finish
= 1;
1690 /* Special case: if we're sitting at the function entry point,
1691 then all we need to do is take a reverse singlestep. We
1692 don't need to set a breakpoint, and indeed it would do us
1695 Note that this can only happen at frame #0, since there's
1696 no way that a function up the stack can have a return address
1697 that's equal to its entry point. */
1701 struct frame_info
*frame
= get_selected_frame (NULL
);
1702 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1704 /* Set a step-resume at the function's entry point. Once that's
1705 hit, we'll do one more step backwards. */
1706 symtab_and_line sr_sal
;
1708 sr_sal
.pspace
= get_frame_program_space (frame
);
1709 insert_step_resume_breakpoint_at_sal (gdbarch
,
1710 sr_sal
, null_frame_id
);
1712 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1716 /* We're almost there -- we just need to back up by one more
1718 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1719 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1723 /* finish_forward -- helper function for finish_command. FRAME is the
1724 frame that called the function we're about to step out of. */
1727 finish_forward (struct finish_command_fsm
*sm
, struct frame_info
*frame
)
1729 struct frame_id frame_id
= get_frame_id (frame
);
1730 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1731 struct symtab_and_line sal
;
1732 struct thread_info
*tp
= inferior_thread ();
1734 sal
= find_pc_line (get_frame_pc (frame
), 0);
1735 sal
.pc
= get_frame_pc (frame
);
1737 sm
->breakpoint
= set_momentary_breakpoint (gdbarch
, sal
,
1738 get_stack_frame_id (frame
),
1741 /* set_momentary_breakpoint invalidates FRAME. */
1744 set_longjmp_breakpoint (tp
, frame_id
);
1746 /* We want to print return value, please... */
1747 tp
->control
.proceed_to_finish
= 1;
1749 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1752 /* Skip frames for "finish". */
1754 static struct frame_info
*
1755 skip_finish_frames (struct frame_info
*frame
)
1757 struct frame_info
*start
;
1763 frame
= skip_tailcall_frames (frame
);
1767 frame
= skip_unwritable_frames (frame
);
1771 while (start
!= frame
);
1776 /* "finish": Set a temporary breakpoint at the place the selected
1777 frame will return to, then continue. */
1780 finish_command (const char *arg
, int from_tty
)
1782 struct frame_info
*frame
;
1784 struct finish_command_fsm
*sm
;
1785 struct thread_info
*tp
;
1788 ensure_not_tfind_mode ();
1789 ensure_valid_thread ();
1790 ensure_not_running ();
1792 /* Find out whether we must run in the background. */
1793 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1794 arg
= stripped
.get ();
1796 prepare_execution_command (current_top_target (), async_exec
);
1799 error (_("The \"finish\" command does not take any arguments."));
1801 frame
= get_prev_frame (get_selected_frame (_("No selected frame.")));
1803 error (_("\"finish\" not meaningful in the outermost frame."));
1805 clear_proceed_status (0);
1807 tp
= inferior_thread ();
1809 sm
= new finish_command_fsm (command_interp ());
1811 tp
->thread_fsm
= sm
;
1813 /* Finishing from an inline frame is completely different. We don't
1814 try to show the "return value" - no way to locate it. */
1815 if (get_frame_type (get_selected_frame (_("No selected frame.")))
1818 /* Claim we are stepping in the calling frame. An empty step
1819 range means that we will stop once we aren't in a function
1820 called by that frame. We don't use the magic "1" value for
1821 step_range_end, because then infrun will think this is nexti,
1822 and not step over the rest of this inlined function call. */
1823 set_step_info (tp
, frame
, {});
1824 tp
->control
.step_range_start
= get_frame_pc (frame
);
1825 tp
->control
.step_range_end
= tp
->control
.step_range_start
;
1826 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1828 /* Print info on the selected frame, including level number but not
1832 printf_filtered (_("Run till exit from "));
1833 print_stack_frame (get_selected_frame (NULL
), 1, LOCATION
, 0);
1836 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1840 /* Find the function we will return from. */
1842 sm
->function
= find_pc_function (get_frame_pc (get_selected_frame (NULL
)));
1844 /* Print info on the selected frame, including level number but not
1848 if (execution_direction
== EXEC_REVERSE
)
1849 printf_filtered (_("Run back to call of "));
1852 if (sm
->function
!= NULL
&& TYPE_NO_RETURN (sm
->function
->type
)
1853 && !query (_("warning: Function %s does not return normally.\n"
1854 "Try to finish anyway? "),
1855 sm
->function
->print_name ()))
1856 error (_("Not confirmed."));
1857 printf_filtered (_("Run till exit from "));
1860 print_stack_frame (get_selected_frame (NULL
), 1, LOCATION
, 0);
1863 if (execution_direction
== EXEC_REVERSE
)
1864 finish_backward (sm
);
1867 frame
= skip_finish_frames (frame
);
1870 error (_("Cannot find the caller frame."));
1872 finish_forward (sm
, frame
);
1878 info_program_command (const char *args
, int from_tty
)
1883 process_stratum_target
*proc_target
;
1885 if (!target_has_execution
)
1887 printf_filtered (_("The program being debugged is not being run.\n"));
1893 ptid
= inferior_ptid
;
1894 proc_target
= current_inferior ()->process_target ();
1897 get_last_target_status (&proc_target
, &ptid
, nullptr);
1899 if (ptid
== null_ptid
|| ptid
== minus_one_ptid
)
1900 error (_("No selected thread."));
1902 thread_info
*tp
= find_thread_ptid (proc_target
, ptid
);
1904 if (tp
->state
== THREAD_EXITED
)
1905 error (_("Invalid selected thread."));
1906 else if (tp
->state
== THREAD_RUNNING
)
1907 error (_("Selected thread is running."));
1909 bs
= tp
->control
.stop_bpstat
;
1910 stat
= bpstat_num (&bs
, &num
);
1912 target_files_info ();
1913 printf_filtered (_("Program stopped at %s.\n"),
1914 paddress (target_gdbarch (), tp
->suspend
.stop_pc
));
1915 if (tp
->control
.stop_step
)
1916 printf_filtered (_("It stopped after being stepped.\n"));
1919 /* There may be several breakpoints in the same place, so this
1920 isn't as strange as it seems. */
1925 printf_filtered (_("It stopped at a breakpoint "
1926 "that has since been deleted.\n"));
1929 printf_filtered (_("It stopped at breakpoint %d.\n"), num
);
1930 stat
= bpstat_num (&bs
, &num
);
1933 else if (tp
->suspend
.stop_signal
!= GDB_SIGNAL_0
)
1935 printf_filtered (_("It stopped with signal %s, %s.\n"),
1936 gdb_signal_to_name (tp
->suspend
.stop_signal
),
1937 gdb_signal_to_string (tp
->suspend
.stop_signal
));
1942 printf_filtered (_("Type \"info stack\" or \"info "
1943 "registers\" for more information.\n"));
1948 environment_info (const char *var
, int from_tty
)
1952 const char *val
= current_inferior ()->environment
.get (var
);
1956 puts_filtered (var
);
1957 puts_filtered (" = ");
1958 puts_filtered (val
);
1959 puts_filtered ("\n");
1963 puts_filtered ("Environment variable \"");
1964 puts_filtered (var
);
1965 puts_filtered ("\" not defined.\n");
1970 char **envp
= current_inferior ()->environment
.envp ();
1972 for (int idx
= 0; envp
[idx
] != NULL
; ++idx
)
1974 puts_filtered (envp
[idx
]);
1975 puts_filtered ("\n");
1981 set_environment_command (const char *arg
, int from_tty
)
1983 const char *p
, *val
;
1987 error_no_arg (_("environment variable and value"));
1989 /* Find separation between variable name and value. */
1990 p
= (char *) strchr (arg
, '=');
1991 val
= (char *) strchr (arg
, ' ');
1993 if (p
!= 0 && val
!= 0)
1995 /* We have both a space and an equals. If the space is before the
1996 equals, walk forward over the spaces til we see a nonspace
1997 (possibly the equals). */
2002 /* Now if the = is after the char following the spaces,
2003 take the char following the spaces. */
2007 else if (val
!= 0 && p
== 0)
2011 error_no_arg (_("environment variable to set"));
2013 if (p
== 0 || p
[1] == 0)
2017 p
= arg
+ strlen (arg
); /* So that savestring below will work. */
2021 /* Not setting variable value to null. */
2023 while (*val
== ' ' || *val
== '\t')
2027 while (p
!= arg
&& (p
[-1] == ' ' || p
[-1] == '\t'))
2030 std::string
var (arg
, p
- arg
);
2033 printf_filtered (_("Setting environment variable "
2034 "\"%s\" to null value.\n"),
2036 current_inferior ()->environment
.set (var
.c_str (), "");
2039 current_inferior ()->environment
.set (var
.c_str (), val
);
2043 unset_environment_command (const char *var
, int from_tty
)
2047 /* If there is no argument, delete all environment variables.
2048 Ask for confirmation if reading from the terminal. */
2049 if (!from_tty
|| query (_("Delete all environment variables? ")))
2050 current_inferior ()->environment
.clear ();
2053 current_inferior ()->environment
.unset (var
);
2056 /* Handle the execution path (PATH variable). */
2058 static const char path_var_name
[] = "PATH";
2061 path_info (const char *args
, int from_tty
)
2063 puts_filtered ("Executable and object file path: ");
2064 puts_filtered (current_inferior ()->environment
.get (path_var_name
));
2065 puts_filtered ("\n");
2068 /* Add zero or more directories to the front of the execution path. */
2071 path_command (const char *dirname
, int from_tty
)
2077 env
= current_inferior ()->environment
.get (path_var_name
);
2078 /* Can be null if path is not set. */
2081 exec_path
= xstrdup (env
);
2082 mod_path (dirname
, &exec_path
);
2083 current_inferior ()->environment
.set (path_var_name
, exec_path
);
2086 path_info (NULL
, from_tty
);
2091 pad_to_column (string_file
&stream
, int col
)
2093 /* At least one space must be printed to separate columns. */
2095 const int size
= stream
.size ();
2097 stream
.puts (n_spaces (col
- size
));
2100 /* Print out the register NAME with value VAL, to FILE, in the default
2104 default_print_one_register_info (struct ui_file
*file
,
2108 struct type
*regtype
= value_type (val
);
2109 int print_raw_format
;
2110 string_file format_stream
;
2113 value_column_1
= 15,
2114 /* Give enough room for "0x", 16 hex digits and two spaces in
2115 preceding column. */
2116 value_column_2
= value_column_1
+ 2 + 16 + 2,
2119 format_stream
.puts (name
);
2120 pad_to_column (format_stream
, value_column_1
);
2122 print_raw_format
= (value_entirely_available (val
)
2123 && !value_optimized_out (val
));
2125 /* If virtual format is floating, print it that way, and in raw
2127 if (regtype
->code () == TYPE_CODE_FLT
2128 || regtype
->code () == TYPE_CODE_DECFLOAT
)
2130 struct value_print_options opts
;
2131 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
2132 enum bfd_endian byte_order
= type_byte_order (regtype
);
2134 get_user_print_options (&opts
);
2137 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2139 if (print_raw_format
)
2141 pad_to_column (format_stream
, value_column_2
);
2142 format_stream
.puts ("(raw ");
2143 print_hex_chars (&format_stream
, valaddr
, TYPE_LENGTH (regtype
),
2145 format_stream
.putc (')');
2150 struct value_print_options opts
;
2152 /* Print the register in hex. */
2153 get_formatted_print_options (&opts
, 'x');
2155 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2156 /* If not a vector register, print it also according to its
2158 if (print_raw_format
&& TYPE_VECTOR (regtype
) == 0)
2160 pad_to_column (format_stream
, value_column_2
);
2161 get_user_print_options (&opts
);
2163 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2167 fputs_filtered (format_stream
.c_str (), file
);
2168 fprintf_filtered (file
, "\n");
2171 /* Print out the machine register regnum. If regnum is -1, print all
2172 registers (print_all == 1) or all non-float and non-vector
2173 registers (print_all == 0).
2175 For most machines, having all_registers_info() print the
2176 register(s) one per line is good enough. If a different format is
2177 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2178 regs), or there is an existing convention for showing all the
2179 registers, define the architecture method PRINT_REGISTERS_INFO to
2180 provide that format. */
2183 default_print_registers_info (struct gdbarch
*gdbarch
,
2184 struct ui_file
*file
,
2185 struct frame_info
*frame
,
2186 int regnum
, int print_all
)
2189 const int numregs
= gdbarch_num_cooked_regs (gdbarch
);
2191 for (i
= 0; i
< numregs
; i
++)
2193 /* Decide between printing all regs, non-float / vector regs, or
2199 if (!gdbarch_register_reggroup_p (gdbarch
, i
, all_reggroup
))
2204 if (!gdbarch_register_reggroup_p (gdbarch
, i
, general_reggroup
))
2214 /* If the register name is empty, it is undefined for this
2215 processor, so don't display anything. */
2216 if (gdbarch_register_name (gdbarch
, i
) == NULL
2217 || *(gdbarch_register_name (gdbarch
, i
)) == '\0')
2220 default_print_one_register_info (file
,
2221 gdbarch_register_name (gdbarch
, i
),
2222 value_of_register (i
, frame
));
2227 registers_info (const char *addr_exp
, int fpregs
)
2229 struct frame_info
*frame
;
2230 struct gdbarch
*gdbarch
;
2232 if (!target_has_registers
)
2233 error (_("The program has no registers now."));
2234 frame
= get_selected_frame (NULL
);
2235 gdbarch
= get_frame_arch (frame
);
2239 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2244 while (*addr_exp
!= '\0')
2249 /* Skip leading white space. */
2250 addr_exp
= skip_spaces (addr_exp
);
2252 /* Discard any leading ``$''. Check that there is something
2253 resembling a register following it. */
2254 if (addr_exp
[0] == '$')
2256 if (isspace ((*addr_exp
)) || (*addr_exp
) == '\0')
2257 error (_("Missing register name"));
2259 /* Find the start/end of this register name/num/group. */
2261 while ((*addr_exp
) != '\0' && !isspace ((*addr_exp
)))
2265 /* Figure out what we've found and display it. */
2267 /* A register name? */
2269 int regnum
= user_reg_map_name_to_regnum (gdbarch
, start
, end
- start
);
2273 /* User registers lie completely outside of the range of
2274 normal registers. Catch them early so that the target
2276 if (regnum
>= gdbarch_num_cooked_regs (gdbarch
))
2278 struct value
*regval
= value_of_user_reg (regnum
, frame
);
2279 const char *regname
= user_reg_map_regnum_to_name (gdbarch
,
2282 /* Print in the same fashion
2283 gdbarch_print_registers_info's default
2284 implementation prints. */
2285 default_print_one_register_info (gdb_stdout
,
2290 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2291 frame
, regnum
, fpregs
);
2296 /* A register group? */
2298 struct reggroup
*group
;
2300 for (group
= reggroup_next (gdbarch
, NULL
);
2302 group
= reggroup_next (gdbarch
, group
))
2304 /* Don't bother with a length check. Should the user
2305 enter a short register group name, go with the first
2306 group that matches. */
2307 if (strncmp (start
, reggroup_name (group
), end
- start
) == 0)
2315 regnum
< gdbarch_num_cooked_regs (gdbarch
);
2318 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
2319 gdbarch_print_registers_info (gdbarch
,
2327 /* Nothing matched. */
2328 error (_("Invalid register `%.*s'"), (int) (end
- start
), start
);
2333 info_all_registers_command (const char *addr_exp
, int from_tty
)
2335 registers_info (addr_exp
, 1);
2339 info_registers_command (const char *addr_exp
, int from_tty
)
2341 registers_info (addr_exp
, 0);
2345 print_vector_info (struct ui_file
*file
,
2346 struct frame_info
*frame
, const char *args
)
2348 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
2350 if (gdbarch_print_vector_info_p (gdbarch
))
2351 gdbarch_print_vector_info (gdbarch
, file
, frame
, args
);
2355 int printed_something
= 0;
2357 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2359 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, vector_reggroup
))
2361 printed_something
= 1;
2362 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2365 if (!printed_something
)
2366 fprintf_filtered (file
, "No vector information\n");
2371 info_vector_command (const char *args
, int from_tty
)
2373 if (!target_has_registers
)
2374 error (_("The program has no registers now."));
2376 print_vector_info (gdb_stdout
, get_selected_frame (NULL
), args
);
2379 /* Kill the inferior process. Make us have no inferior. */
2382 kill_command (const char *arg
, int from_tty
)
2384 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2385 It should be a distinct flag that indicates that a target is active, cuz
2386 some targets don't have processes! */
2388 if (inferior_ptid
== null_ptid
)
2389 error (_("The program is not being run."));
2390 if (!query (_("Kill the program being debugged? ")))
2391 error (_("Not confirmed."));
2393 int pid
= current_inferior ()->pid
;
2394 /* Save the pid as a string before killing the inferior, since that
2395 may unpush the current target, and we need the string after. */
2396 std::string pid_str
= target_pid_to_str (ptid_t (pid
));
2397 int infnum
= current_inferior ()->num
;
2401 if (print_inferior_events
)
2402 printf_unfiltered (_("[Inferior %d (%s) killed]\n"),
2403 infnum
, pid_str
.c_str ());
2405 bfd_cache_close_all ();
2408 /* Used in `attach&' command. Proceed threads of inferior INF iff
2409 they stopped due to debugger request, and when they did, they
2410 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2411 have been explicitly been told to stop. */
2414 proceed_after_attach (inferior
*inf
)
2416 /* Don't error out if the current thread is running, because
2417 there may be other stopped threads. */
2419 /* Backup current thread and selected frame. */
2420 scoped_restore_current_thread restore_thread
;
2422 for (thread_info
*thread
: inf
->non_exited_threads ())
2423 if (!thread
->executing
2424 && !thread
->stop_requested
2425 && thread
->suspend
.stop_signal
== GDB_SIGNAL_0
)
2427 switch_to_thread (thread
);
2428 clear_proceed_status (0);
2429 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2433 /* See inferior.h. */
2436 setup_inferior (int from_tty
)
2438 struct inferior
*inferior
;
2440 inferior
= current_inferior ();
2441 inferior
->needs_setup
= 0;
2443 /* If no exec file is yet known, try to determine it from the
2445 if (get_exec_file (0) == NULL
)
2446 exec_file_locate_attach (inferior_ptid
.pid (), 1, from_tty
);
2449 reopen_exec_file ();
2453 /* Take any necessary post-attaching actions for this platform. */
2454 target_post_attach (inferior_ptid
.pid ());
2456 post_create_inferior (current_top_target (), from_tty
);
2459 /* What to do after the first program stops after attaching. */
2460 enum attach_post_wait_mode
2462 /* Do nothing. Leaves threads as they are. */
2463 ATTACH_POST_WAIT_NOTHING
,
2465 /* Re-resume threads that are marked running. */
2466 ATTACH_POST_WAIT_RESUME
,
2468 /* Stop all threads. */
2469 ATTACH_POST_WAIT_STOP
,
2472 /* Called after we've attached to a process and we've seen it stop for
2473 the first time. If ASYNC_EXEC is true, re-resume threads that
2474 should be running. Else if ATTACH, */
2477 attach_post_wait (const char *args
, int from_tty
, enum attach_post_wait_mode mode
)
2479 struct inferior
*inferior
;
2481 inferior
= current_inferior ();
2482 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
2484 if (inferior
->needs_setup
)
2485 setup_inferior (from_tty
);
2487 if (mode
== ATTACH_POST_WAIT_RESUME
)
2489 /* The user requested an `attach&', so be sure to leave threads
2490 that didn't get a signal running. */
2492 /* Immediatelly resume all suspended threads of this inferior,
2493 and this inferior only. This should have no effect on
2494 already running threads. If a thread has been stopped with a
2495 signal, leave it be. */
2497 proceed_after_attach (inferior
);
2500 if (inferior_thread ()->suspend
.stop_signal
== GDB_SIGNAL_0
)
2502 clear_proceed_status (0);
2503 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2507 else if (mode
== ATTACH_POST_WAIT_STOP
)
2509 /* The user requested a plain `attach', so be sure to leave
2510 the inferior stopped. */
2512 /* At least the current thread is already stopped. */
2514 /* In all-stop, by definition, all threads have to be already
2515 stopped at this point. In non-stop, however, although the
2516 selected thread is stopped, others may still be executing.
2517 Be sure to explicitly stop all threads of the process. This
2518 should have no effect on already stopped threads. */
2520 target_stop (ptid_t (inferior
->pid
));
2521 else if (target_is_non_stop_p ())
2523 struct thread_info
*lowest
= inferior_thread ();
2525 stop_all_threads ();
2527 /* It's not defined which thread will report the attach
2528 stop. For consistency, always select the thread with
2529 lowest GDB number, which should be the main thread, if it
2531 for (thread_info
*thread
: current_inferior ()->non_exited_threads ())
2532 if (thread
->inf
->num
< lowest
->inf
->num
2533 || thread
->per_inf_num
< lowest
->per_inf_num
)
2536 switch_to_thread (lowest
);
2539 /* Tell the user/frontend where we're stopped. */
2541 if (deprecated_attach_hook
)
2542 deprecated_attach_hook ();
2546 struct attach_command_continuation_args
2550 enum attach_post_wait_mode mode
;
2554 attach_command_continuation (void *args
, int err
)
2556 struct attach_command_continuation_args
*a
2557 = (struct attach_command_continuation_args
*) args
;
2562 attach_post_wait (a
->args
, a
->from_tty
, a
->mode
);
2566 attach_command_continuation_free_args (void *args
)
2568 struct attach_command_continuation_args
*a
2569 = (struct attach_command_continuation_args
*) args
;
2575 /* "attach" command entry point. Takes a program started up outside
2576 of gdb and ``attaches'' to it. This stops it cold in its tracks
2577 and allows us to start debugging it. */
2580 attach_command (const char *args
, int from_tty
)
2583 struct target_ops
*attach_target
;
2584 struct inferior
*inferior
= current_inferior ();
2585 enum attach_post_wait_mode mode
;
2587 dont_repeat (); /* Not for the faint of heart */
2589 if (gdbarch_has_global_solist (target_gdbarch ()))
2590 /* Don't complain if all processes share the same symbol
2593 else if (target_has_execution
)
2595 if (query (_("A program is being debugged already. Kill it? ")))
2598 error (_("Not killed."));
2601 /* Clean up any leftovers from other runs. Some other things from
2602 this function should probably be moved into target_pre_inferior. */
2603 target_pre_inferior (from_tty
);
2605 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
2606 args
= stripped
.get ();
2608 attach_target
= find_attach_target ();
2610 prepare_execution_command (attach_target
, async_exec
);
2612 if (non_stop
&& !attach_target
->supports_non_stop ())
2613 error (_("Cannot attach to this target in non-stop mode"));
2615 attach_target
->attach (args
, from_tty
);
2616 /* to_attach should push the target, so after this point we
2617 shouldn't refer to attach_target again. */
2618 attach_target
= NULL
;
2620 /* Set up the "saved terminal modes" of the inferior
2621 based on what modes we are starting it with. */
2622 target_terminal::init ();
2624 /* Install inferior's terminal modes. This may look like a no-op,
2625 as we've just saved them above, however, this does more than
2626 restore terminal settings:
2628 - installs a SIGINT handler that forwards SIGINT to the inferior.
2629 Otherwise a Ctrl-C pressed just while waiting for the initial
2630 stop would end up as a spurious Quit.
2632 - removes stdin from the event loop, which we need if attaching
2633 in the foreground, otherwise on targets that report an initial
2634 stop on attach (which are most) we'd process input/commands
2635 while we're in the event loop waiting for that stop. That is,
2636 before the attach continuation runs and the command is really
2638 target_terminal::inferior ();
2640 /* Set up execution context to know that we should return from
2641 wait_for_inferior as soon as the target reports a stop. */
2642 init_wait_for_inferior ();
2643 clear_proceed_status (0);
2645 inferior
->needs_setup
= 1;
2647 if (target_is_non_stop_p ())
2649 /* If we find that the current thread isn't stopped, explicitly
2650 do so now, because we're going to install breakpoints and
2654 /* The user requested an `attach&'; stop just one thread. */
2655 target_stop (inferior_ptid
);
2657 /* The user requested an `attach', so stop all threads of this
2659 target_stop (ptid_t (inferior_ptid
.pid ()));
2662 /* Check for exec file mismatch, and let the user solve it. */
2663 validate_exec_file (from_tty
);
2665 mode
= async_exec
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_STOP
;
2667 /* Some system don't generate traps when attaching to inferior.
2668 E.g. Mach 3 or GNU hurd. */
2669 if (!target_attach_no_wait ())
2671 struct attach_command_continuation_args
*a
;
2673 /* Careful here. See comments in inferior.h. Basically some
2674 OSes don't ignore SIGSTOPs on continue requests anymore. We
2675 need a way for handle_inferior_event to reset the stop_signal
2676 variable after an attach, and this is what
2677 STOP_QUIETLY_NO_SIGSTOP is for. */
2678 inferior
->control
.stop_soon
= STOP_QUIETLY_NO_SIGSTOP
;
2680 /* Wait for stop. */
2681 a
= XNEW (struct attach_command_continuation_args
);
2682 a
->args
= xstrdup (args
);
2683 a
->from_tty
= from_tty
;
2685 add_inferior_continuation (attach_command_continuation
, a
,
2686 attach_command_continuation_free_args
);
2688 /* Let infrun consider waiting for events out of this
2690 inferior
->process_target ()->threads_executing
= true;
2692 if (!target_is_async_p ())
2693 mark_infrun_async_event_handler ();
2697 attach_post_wait (args
, from_tty
, mode
);
2700 /* We had just found out that the target was already attached to an
2701 inferior. PTID points at a thread of this new inferior, that is
2702 the most likely to be stopped right now, but not necessarily so.
2703 The new inferior is assumed to be already added to the inferior
2704 list at this point. If LEAVE_RUNNING, then leave the threads of
2705 this inferior running, except those we've explicitly seen reported
2709 notice_new_inferior (thread_info
*thr
, int leave_running
, int from_tty
)
2711 enum attach_post_wait_mode mode
2712 = leave_running
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_NOTHING
;
2714 gdb::optional
<scoped_restore_current_thread
> restore_thread
;
2716 if (inferior_ptid
!= null_ptid
)
2717 restore_thread
.emplace ();
2719 /* Avoid reading registers -- we haven't fetched the target
2721 switch_to_thread_no_regs (thr
);
2723 /* When we "notice" a new inferior we need to do all the things we
2724 would normally do if we had just attached to it. */
2728 struct attach_command_continuation_args
*a
;
2729 struct inferior
*inferior
= current_inferior ();
2731 /* We're going to install breakpoints, and poke at memory,
2732 ensure that the inferior is stopped for a moment while we do
2734 target_stop (inferior_ptid
);
2736 inferior
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
2738 /* Wait for stop before proceeding. */
2739 a
= XNEW (struct attach_command_continuation_args
);
2740 a
->args
= xstrdup ("");
2741 a
->from_tty
= from_tty
;
2743 add_inferior_continuation (attach_command_continuation
, a
,
2744 attach_command_continuation_free_args
);
2749 attach_post_wait ("" /* args */, from_tty
, mode
);
2754 * takes a program previously attached to and detaches it.
2755 * The program resumes execution and will no longer stop
2756 * on signals, etc. We better not have left any breakpoints
2757 * in the program or it'll die when it hits one. For this
2758 * to work, it may be necessary for the process to have been
2759 * previously attached. It *might* work if the program was
2760 * started via the normal ptrace (PTRACE_TRACEME).
2764 detach_command (const char *args
, int from_tty
)
2766 dont_repeat (); /* Not for the faint of heart. */
2768 if (inferior_ptid
== null_ptid
)
2769 error (_("The program is not being run."));
2771 query_if_trace_running (from_tty
);
2773 disconnect_tracing ();
2775 target_detach (current_inferior (), from_tty
);
2777 /* The current inferior process was just detached successfully. Get
2778 rid of breakpoints that no longer make sense. Note we don't do
2779 this within target_detach because that is also used when
2780 following child forks, and in that case we will want to transfer
2781 breakpoints to the child, not delete them. */
2782 breakpoint_init_inferior (inf_exited
);
2784 /* If the solist is global across inferiors, don't clear it when we
2785 detach from a single inferior. */
2786 if (!gdbarch_has_global_solist (target_gdbarch ()))
2787 no_shared_libraries (NULL
, from_tty
);
2789 if (deprecated_detach_hook
)
2790 deprecated_detach_hook ();
2793 /* Disconnect from the current target without resuming it (leaving it
2794 waiting for a debugger).
2796 We'd better not have left any breakpoints in the program or the
2797 next debugger will get confused. Currently only supported for some
2798 remote targets, since the normal attach mechanisms don't work on
2799 stopped processes on some native platforms (e.g. GNU/Linux). */
2802 disconnect_command (const char *args
, int from_tty
)
2804 dont_repeat (); /* Not for the faint of heart. */
2805 query_if_trace_running (from_tty
);
2806 disconnect_tracing ();
2807 target_disconnect (args
, from_tty
);
2808 no_shared_libraries (NULL
, from_tty
);
2809 init_thread_list ();
2810 if (deprecated_detach_hook
)
2811 deprecated_detach_hook ();
2814 /* Stop PTID in the current target, and tag the PTID threads as having
2815 been explicitly requested to stop. PTID can be a thread, a
2816 process, or minus_one_ptid, meaning all threads of all inferiors of
2817 the current target. */
2820 stop_current_target_threads_ns (ptid_t ptid
)
2824 /* Tag the thread as having been explicitly requested to stop, so
2825 other parts of gdb know not to resume this thread automatically,
2826 if it was stopped due to an internal event. Limit this to
2827 non-stop mode, as when debugging a multi-threaded application in
2828 all-stop mode, we will only get one stop event --- it's undefined
2829 which thread will report the event. */
2830 set_stop_requested (current_inferior ()->process_target (),
2834 /* See inferior.h. */
2837 interrupt_target_1 (bool all_threads
)
2843 scoped_restore_current_thread restore_thread
;
2845 for (inferior
*inf
: all_inferiors ())
2847 switch_to_inferior_no_thread (inf
);
2848 stop_current_target_threads_ns (minus_one_ptid
);
2852 stop_current_target_threads_ns (inferior_ptid
);
2855 target_interrupt ();
2859 Stop the execution of the target while running in async mode, in
2860 the background. In all-stop, stop the whole process. In non-stop
2861 mode, stop the current thread only by default, or stop all threads
2862 if the `-a' switch is used. */
2865 interrupt_command (const char *args
, int from_tty
)
2867 if (target_can_async_p ())
2869 int all_threads
= 0;
2871 dont_repeat (); /* Not for the faint of heart. */
2874 && startswith (args
, "-a"))
2877 if (!non_stop
&& all_threads
)
2878 error (_("-a is meaningless in all-stop mode."));
2880 interrupt_target_1 (all_threads
);
2884 /* See inferior.h. */
2887 default_print_float_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
2888 struct frame_info
*frame
, const char *args
)
2891 int printed_something
= 0;
2893 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2895 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, float_reggroup
))
2897 printed_something
= 1;
2898 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2901 if (!printed_something
)
2902 fprintf_filtered (file
, "No floating-point info "
2903 "available for this processor.\n");
2907 info_float_command (const char *args
, int from_tty
)
2909 struct frame_info
*frame
;
2911 if (!target_has_registers
)
2912 error (_("The program has no registers now."));
2914 frame
= get_selected_frame (NULL
);
2915 gdbarch_print_float_info (get_frame_arch (frame
), gdb_stdout
, frame
, args
);
2918 /* Implement `info proc' family of commands. */
2921 info_proc_cmd_1 (const char *args
, enum info_proc_what what
, int from_tty
)
2923 struct gdbarch
*gdbarch
= get_current_arch ();
2925 if (!target_info_proc (args
, what
))
2927 if (gdbarch_info_proc_p (gdbarch
))
2928 gdbarch_info_proc (gdbarch
, args
, what
);
2930 error (_("Not supported on this target."));
2934 /* Implement `info proc' when given without any further parameters. */
2937 info_proc_cmd (const char *args
, int from_tty
)
2939 info_proc_cmd_1 (args
, IP_MINIMAL
, from_tty
);
2942 /* Implement `info proc mappings'. */
2945 info_proc_cmd_mappings (const char *args
, int from_tty
)
2947 info_proc_cmd_1 (args
, IP_MAPPINGS
, from_tty
);
2950 /* Implement `info proc stat'. */
2953 info_proc_cmd_stat (const char *args
, int from_tty
)
2955 info_proc_cmd_1 (args
, IP_STAT
, from_tty
);
2958 /* Implement `info proc status'. */
2961 info_proc_cmd_status (const char *args
, int from_tty
)
2963 info_proc_cmd_1 (args
, IP_STATUS
, from_tty
);
2966 /* Implement `info proc cwd'. */
2969 info_proc_cmd_cwd (const char *args
, int from_tty
)
2971 info_proc_cmd_1 (args
, IP_CWD
, from_tty
);
2974 /* Implement `info proc cmdline'. */
2977 info_proc_cmd_cmdline (const char *args
, int from_tty
)
2979 info_proc_cmd_1 (args
, IP_CMDLINE
, from_tty
);
2982 /* Implement `info proc exe'. */
2985 info_proc_cmd_exe (const char *args
, int from_tty
)
2987 info_proc_cmd_1 (args
, IP_EXE
, from_tty
);
2990 /* Implement `info proc files'. */
2993 info_proc_cmd_files (const char *args
, int from_tty
)
2995 info_proc_cmd_1 (args
, IP_FILES
, from_tty
);
2998 /* Implement `info proc all'. */
3001 info_proc_cmd_all (const char *args
, int from_tty
)
3003 info_proc_cmd_1 (args
, IP_ALL
, from_tty
);
3006 /* Implement `show print finish'. */
3009 show_print_finish (struct ui_file
*file
, int from_tty
,
3010 struct cmd_list_element
*c
,
3013 fprintf_filtered (file
, _("\
3014 Printing of return value after `finish' is %s.\n"),
3019 /* This help string is used for the run, start, and starti commands.
3020 It is defined as a macro to prevent duplication. */
3022 #define RUN_ARGS_HELP \
3023 "You may specify arguments to give it.\n\
3024 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3025 shell that will start the program (specified by the \"$SHELL\" environment\n\
3026 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3027 are also allowed.\n\
3029 With no arguments, uses arguments last specified (with \"run\" or \n\
3030 \"set args\"). To cancel previous arguments and run with no arguments,\n\
3031 use \"set args\" without arguments.\n\
3033 To start the inferior without using a shell, use \"set startup-with-shell off\"."
3035 void _initialize_infcmd ();
3037 _initialize_infcmd ()
3039 static struct cmd_list_element
*info_proc_cmdlist
;
3040 struct cmd_list_element
*c
= NULL
;
3041 const char *cmd_name
;
3043 /* Add the filename of the terminal connected to inferior I/O. */
3044 add_setshow_optional_filename_cmd ("inferior-tty", class_run
,
3045 &inferior_io_terminal_scratch
, _("\
3046 Set terminal for future runs of program being debugged."), _("\
3047 Show terminal for future runs of program being debugged."), _("\
3048 Usage: set inferior-tty [TTY]\n\n\
3049 If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3051 set_inferior_tty_command
,
3052 show_inferior_tty_command
,
3053 &setlist
, &showlist
);
3054 cmd_name
= "inferior-tty";
3055 c
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
3056 gdb_assert (c
!= NULL
);
3057 add_alias_cmd ("tty", c
, class_run
, 0, &cmdlist
);
3060 add_setshow_string_noescape_cmd (cmd_name
, class_run
,
3061 &inferior_args_scratch
, _("\
3062 Set argument list to give program being debugged when it is started."), _("\
3063 Show argument list to give program being debugged when it is started."), _("\
3064 Follow this command with any number of args, to be passed to the program."),
3067 &setlist
, &showlist
);
3068 c
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
3069 gdb_assert (c
!= NULL
);
3070 set_cmd_completer (c
, filename_completer
);
3073 add_setshow_string_noescape_cmd (cmd_name
, class_run
,
3074 &inferior_cwd_scratch
, _("\
3075 Set the current working directory to be used when the inferior is started.\n\
3076 Changing this setting does not have any effect on inferiors that are\n\
3079 Show the current working directory that is used when the inferior is started."),
3081 Use this command to change the current working directory that will be used\n\
3082 when the inferior is started. This setting does not affect GDB's current\n\
3083 working directory."),
3086 &setlist
, &showlist
);
3087 c
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
3088 gdb_assert (c
!= NULL
);
3089 set_cmd_completer (c
, filename_completer
);
3091 c
= add_cmd ("environment", no_class
, environment_info
, _("\
3092 The environment to give the program, or one variable's value.\n\
3093 With an argument VAR, prints the value of environment variable VAR to\n\
3094 give the program being debugged. With no arguments, prints the entire\n\
3095 environment to be given to the program."), &showlist
);
3096 set_cmd_completer (c
, noop_completer
);
3098 add_basic_prefix_cmd ("unset", no_class
,
3099 _("Complement to certain \"set\" commands."),
3100 &unsetlist
, "unset ", 0, &cmdlist
);
3102 c
= add_cmd ("environment", class_run
, unset_environment_command
, _("\
3103 Cancel environment variable VAR for the program.\n\
3104 This does not affect the program until the next \"run\" command."),
3106 set_cmd_completer (c
, noop_completer
);
3108 c
= add_cmd ("environment", class_run
, set_environment_command
, _("\
3109 Set environment variable value to give the program.\n\
3110 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3111 VALUES of environment variables are uninterpreted strings.\n\
3112 This does not affect the program until the next \"run\" command."),
3114 set_cmd_completer (c
, noop_completer
);
3116 c
= add_com ("path", class_files
, path_command
, _("\
3117 Add directory DIR(s) to beginning of search path for object files.\n\
3118 $cwd in the path means the current working directory.\n\
3119 This path is equivalent to the $PATH shell variable. It is a list of\n\
3120 directories, separated by colons. These directories are searched to find\n\
3121 fully linked executable files and separately compiled object files as \
3123 set_cmd_completer (c
, filename_completer
);
3125 c
= add_cmd ("paths", no_class
, path_info
, _("\
3126 Current search path for finding object files.\n\
3127 $cwd in the path means the current working directory.\n\
3128 This path is equivalent to the $PATH shell variable. It is a list of\n\
3129 directories, separated by colons. These directories are searched to find\n\
3130 fully linked executable files and separately compiled object files as \
3133 set_cmd_completer (c
, noop_completer
);
3135 add_prefix_cmd ("kill", class_run
, kill_command
,
3136 _("Kill execution of program being debugged."),
3137 &killlist
, "kill ", 0, &cmdlist
);
3139 add_com ("attach", class_run
, attach_command
, _("\
3140 Attach to a process or file outside of GDB.\n\
3141 This command attaches to another target, of the same type as your last\n\
3142 \"target\" command (\"info files\" will show your target stack).\n\
3143 The command may take as argument a process id or a device file.\n\
3144 For a process id, you must have permission to send the process a signal,\n\
3145 and it must have the same effective uid as the debugger.\n\
3146 When using \"attach\" with a process id, the debugger finds the\n\
3147 program running in the process, looking first in the current working\n\
3148 directory, or (if not found there) using the source file search path\n\
3149 (see the \"directory\" command). You can also use the \"file\" command\n\
3150 to specify the program, and to load its symbol table."));
3152 add_prefix_cmd ("detach", class_run
, detach_command
, _("\
3153 Detach a process or file previously attached.\n\
3154 If a process, it is no longer traced, and it continues its execution. If\n\
3155 you were debugging a file, the file is closed and gdb no longer accesses it."),
3156 &detachlist
, "detach ", 0, &cmdlist
);
3158 add_com ("disconnect", class_run
, disconnect_command
, _("\
3159 Disconnect from a target.\n\
3160 The target will wait for another debugger to connect. Not available for\n\
3163 c
= add_com ("signal", class_run
, signal_command
, _("\
3164 Continue program with the specified signal.\n\
3165 Usage: signal SIGNAL\n\
3166 The SIGNAL argument is processed the same as the handle command.\n\
3168 An argument of \"0\" means continue the program without sending it a signal.\n\
3169 This is useful in cases where the program stopped because of a signal,\n\
3170 and you want to resume the program while discarding the signal.\n\
3172 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3173 the current thread only."));
3174 set_cmd_completer (c
, signal_completer
);
3176 c
= add_com ("queue-signal", class_run
, queue_signal_command
, _("\
3177 Queue a signal to be delivered to the current thread when it is resumed.\n\
3178 Usage: queue-signal SIGNAL\n\
3179 The SIGNAL argument is processed the same as the handle command.\n\
3180 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3182 An argument of \"0\" means remove any currently queued signal from\n\
3183 the current thread. This is useful in cases where the program stopped\n\
3184 because of a signal, and you want to resume it while discarding the signal.\n\
3186 In a multi-threaded program the signal is queued with, or discarded from,\n\
3187 the current thread only."));
3188 set_cmd_completer (c
, signal_completer
);
3190 add_com ("stepi", class_run
, stepi_command
, _("\
3191 Step one instruction exactly.\n\
3193 Argument N means step N times (or till program stops for another \
3195 add_com_alias ("si", "stepi", class_run
, 0);
3197 add_com ("nexti", class_run
, nexti_command
, _("\
3198 Step one instruction, but proceed through subroutine calls.\n\
3200 Argument N means step N times (or till program stops for another \
3202 add_com_alias ("ni", "nexti", class_run
, 0);
3204 add_com ("finish", class_run
, finish_command
, _("\
3205 Execute until selected stack frame returns.\n\
3207 Upon return, the value returned is printed and put in the value history."));
3208 add_com_alias ("fin", "finish", class_run
, 1);
3210 add_com ("next", class_run
, next_command
, _("\
3211 Step program, proceeding through subroutine calls.\n\
3213 Unlike \"step\", if the current source line calls a subroutine,\n\
3214 this command does not enter the subroutine, but instead steps over\n\
3215 the call, in effect treating it as a single source line."));
3216 add_com_alias ("n", "next", class_run
, 1);
3218 add_com ("step", class_run
, step_command
, _("\
3219 Step program until it reaches a different source line.\n\
3221 Argument N means step N times (or till program stops for another \
3223 add_com_alias ("s", "step", class_run
, 1);
3225 c
= add_com ("until", class_run
, until_command
, _("\
3226 Execute until past the current line or past a LOCATION.\n\
3227 Execute until the program reaches a source line greater than the current\n\
3228 or a specified location (same args as break command) within the current \
3230 set_cmd_completer (c
, location_completer
);
3231 add_com_alias ("u", "until", class_run
, 1);
3233 c
= add_com ("advance", class_run
, advance_command
, _("\
3234 Continue the program up to the given location (same form as args for break \
3236 Execution will also stop upon exit from the current stack frame."));
3237 set_cmd_completer (c
, location_completer
);
3239 c
= add_com ("jump", class_run
, jump_command
, _("\
3240 Continue program being debugged at specified line or address.\n\
3241 Usage: jump LOCATION\n\
3242 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3243 for an address to start at."));
3244 set_cmd_completer (c
, location_completer
);
3245 add_com_alias ("j", "jump", class_run
, 1);
3247 add_com ("continue", class_run
, continue_command
, _("\
3248 Continue program being debugged, after signal or breakpoint.\n\
3249 Usage: continue [N]\n\
3250 If proceeding from breakpoint, a number N may be used as an argument,\n\
3251 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3252 the breakpoint won't break until the Nth time it is reached).\n\
3254 If non-stop mode is enabled, continue only the current thread,\n\
3255 otherwise all the threads in the program are continued. To \n\
3256 continue all stopped threads in non-stop mode, use the -a option.\n\
3257 Specifying -a and an ignore count simultaneously is an error."));
3258 add_com_alias ("c", "cont", class_run
, 1);
3259 add_com_alias ("fg", "cont", class_run
, 1);
3261 c
= add_com ("run", class_run
, run_command
, _("\
3262 Start debugged program.\n"
3264 set_cmd_completer (c
, filename_completer
);
3265 add_com_alias ("r", "run", class_run
, 1);
3267 c
= add_com ("start", class_run
, start_command
, _("\
3268 Start the debugged program stopping at the beginning of the main procedure.\n"
3270 set_cmd_completer (c
, filename_completer
);
3272 c
= add_com ("starti", class_run
, starti_command
, _("\
3273 Start the debugged program stopping at the first instruction.\n"
3275 set_cmd_completer (c
, filename_completer
);
3277 add_com ("interrupt", class_run
, interrupt_command
,
3278 _("Interrupt the execution of the debugged program.\n\
3279 If non-stop mode is enabled, interrupt only the current thread,\n\
3280 otherwise all the threads in the program are stopped. To \n\
3281 interrupt all running threads in non-stop mode, use the -a option."));
3283 c
= add_info ("registers", info_registers_command
, _("\
3284 List of integer registers and their contents, for selected stack frame.\n\
3285 One or more register names as argument means describe the given registers.\n\
3286 One or more register group names as argument means describe the registers\n\
3287 in the named register groups."));
3288 add_info_alias ("r", "registers", 1);
3289 set_cmd_completer (c
, reg_or_group_completer
);
3291 c
= add_info ("all-registers", info_all_registers_command
, _("\
3292 List of all registers and their contents, for selected stack frame.\n\
3293 One or more register names as argument means describe the given registers.\n\
3294 One or more register group names as argument means describe the registers\n\
3295 in the named register groups."));
3296 set_cmd_completer (c
, reg_or_group_completer
);
3298 add_info ("program", info_program_command
,
3299 _("Execution status of the program."));
3301 add_info ("float", info_float_command
,
3302 _("Print the status of the floating point unit."));
3304 add_info ("vector", info_vector_command
,
3305 _("Print the status of the vector unit."));
3307 add_prefix_cmd ("proc", class_info
, info_proc_cmd
,
3309 Show additional information about a process.\n\
3310 Specify any process id, or use the program being debugged by default."),
3311 &info_proc_cmdlist
, "info proc ",
3312 1/*allow-unknown*/, &infolist
);
3314 add_cmd ("mappings", class_info
, info_proc_cmd_mappings
, _("\
3315 List memory regions mapped by the specified process."),
3316 &info_proc_cmdlist
);
3318 add_cmd ("stat", class_info
, info_proc_cmd_stat
, _("\
3319 List process info from /proc/PID/stat."),
3320 &info_proc_cmdlist
);
3322 add_cmd ("status", class_info
, info_proc_cmd_status
, _("\
3323 List process info from /proc/PID/status."),
3324 &info_proc_cmdlist
);
3326 add_cmd ("cwd", class_info
, info_proc_cmd_cwd
, _("\
3327 List current working directory of the specified process."),
3328 &info_proc_cmdlist
);
3330 add_cmd ("cmdline", class_info
, info_proc_cmd_cmdline
, _("\
3331 List command line arguments of the specified process."),
3332 &info_proc_cmdlist
);
3334 add_cmd ("exe", class_info
, info_proc_cmd_exe
, _("\
3335 List absolute filename for executable of the specified process."),
3336 &info_proc_cmdlist
);
3338 add_cmd ("files", class_info
, info_proc_cmd_files
, _("\
3339 List files opened by the specified process."),
3340 &info_proc_cmdlist
);
3342 add_cmd ("all", class_info
, info_proc_cmd_all
, _("\
3343 List all available info about the specified process."),
3344 &info_proc_cmdlist
);
3346 add_setshow_boolean_cmd ("finish", class_support
,
3347 &user_print_options
.finish_print
, _("\
3348 Set whether `finish' prints the return value."), _("\
3349 Show whether `finish' prints the return value."), NULL
,
3352 &setprintlist
, &showprintlist
);