1 /* Memory-access and commands for "inferior" process, for GDB.
3 Copyright (C) 1986-2024 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/>. */
20 #include "arch-utils.h"
21 #include "exceptions.h"
27 #include "gdbsupport/environ.h"
29 #include "cli/cli-cmds.h"
30 #include "cli/cli-style.h"
36 #include "completer.h"
39 #include "reggroups.h"
43 #include "observable.h"
44 #include "target-descriptions.h"
45 #include "user-regs.h"
46 #include "gdbthread.h"
48 #include "inline-frame.h"
49 #include "tracepoint.h"
52 #include "thread-fsm.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 /* Pid of our debugged inferior, or 0 if no inferior now.
70 Since various parts of infrun.c test this to see whether there is a program
71 being debugged it should be nonzero (currently 3 is used) for remote
76 /* Nonzero if stopped due to completion of a stack dummy routine. */
78 enum stop_stack_kind stop_stack_dummy
;
80 /* Nonzero if stopped due to a random (unexpected) signal in inferior
83 int stopped_by_random_signal
;
86 /* Whether "finish" should print the value. */
88 static bool finish_print
= true;
92 /* Store the new value passed to 'set inferior-tty'. */
95 set_tty_value (const std::string
&tty
)
97 current_inferior ()->set_tty (tty
);
100 /* Get the current 'inferior-tty' value. */
102 static const std::string
&
105 return current_inferior ()->tty ();
108 /* Implement 'show inferior-tty' command. */
111 show_inferior_tty_command (struct ui_file
*file
, int from_tty
,
112 struct cmd_list_element
*c
, const char *value
)
114 /* Note that we ignore the passed-in value in favor of computing it
116 const std::string
&inferior_tty
= current_inferior ()->tty ();
119 _("Terminal for future runs of program being debugged "
120 "is \"%s\".\n"), inferior_tty
.c_str ());
123 /* Store the new value passed to 'set args'. */
126 set_args_value (const std::string
&args
)
128 current_inferior ()->set_args (args
);
131 /* Return the value for 'show args' to display. */
133 static const std::string
&
136 return current_inferior ()->args ();
139 /* Callback to implement 'show args' command. */
142 show_args_command (struct ui_file
*file
, int from_tty
,
143 struct cmd_list_element
*c
, const char *value
)
145 /* Ignore the passed in value, pull the argument directly from the
146 inferior. However, these should always be the same. */
147 gdb_printf (file
, _("\
148 Argument list to give program being debugged when it is started is \"%s\".\n"),
149 current_inferior ()->args ().c_str ());
152 /* See gdbsupport/common-inferior.h. */
157 return current_inferior ()->cwd ();
160 /* Store the new value passed to 'set cwd'. */
163 set_cwd_value (const std::string
&args
)
165 current_inferior ()->set_cwd (args
);
168 /* Handle the 'show cwd' command. */
171 show_cwd_command (struct ui_file
*file
, int from_tty
,
172 struct cmd_list_element
*c
, const char *value
)
174 const std::string
&cwd
= current_inferior ()->cwd ();
179 You have not set the inferior's current working directory.\n\
180 The inferior will inherit GDB's cwd if native debugging, or the remote\n\
181 server's cwd if remote debugging.\n"));
184 _("Current working directory that will be used "
185 "when starting the inferior is \"%s\".\n"),
190 /* This function strips the '&' character (indicating background
191 execution) that is added as *the last* of the arguments ARGS of a
192 command. A copy of the incoming ARGS without the '&' is returned,
193 unless the resulting string after stripping is empty, in which case
194 NULL is returned. *BG_CHAR_P is an output boolean that indicates
195 whether the '&' character was found. */
197 static gdb::unique_xmalloc_ptr
<char>
198 strip_bg_char (const char *args
, int *bg_char_p
)
202 if (args
== nullptr || *args
== '\0')
208 p
= args
+ strlen (args
);
212 while (p
> args
&& isspace (p
[-1]))
217 return gdb::unique_xmalloc_ptr
<char>
218 (savestring (args
, p
- args
));
220 return gdb::unique_xmalloc_ptr
<char> (nullptr);
224 return make_unique_xstrdup (args
);
227 /* Common actions to take after creating any sort of inferior, by any
228 means (running, attaching, connecting, et cetera). The target
229 should be stopped. */
232 post_create_inferior (int from_tty
)
235 /* Be sure we own the terminal in case write operations are performed. */
236 target_terminal::ours_for_output ();
238 infrun_debug_show_threads ("threads in the newly created inferior",
239 current_inferior ()->non_exited_threads ());
241 /* If the target hasn't taken care of this already, do it now.
242 Targets which need to access registers during to_open,
243 to_create_inferior, or to_attach should do it earlier; but many
245 target_find_description ();
247 /* Now that we know the register layout, retrieve current PC. But
248 if the PC is unavailable (e.g., we're opening a core file with
249 missing registers info), ignore it. */
250 thread_info
*thr
= inferior_thread ();
252 thr
->clear_stop_pc ();
255 regcache
*rc
= get_thread_regcache (thr
);
256 thr
->set_stop_pc (regcache_read_pc (rc
));
258 catch (const gdb_exception_error
&ex
)
260 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
264 if (current_program_space
->exec_bfd ())
266 const unsigned solib_add_generation
267 = current_program_space
->solib_add_generation
;
269 scoped_restore restore_in_initial_library_scan
270 = make_scoped_restore (¤t_inferior ()->in_initial_library_scan
,
273 /* Create the hooks to handle shared library load and unload
275 solib_create_inferior_hook (from_tty
);
277 if (current_program_space
->solib_add_generation
== solib_add_generation
)
279 /* The platform-specific hook should load initial shared libraries,
280 but didn't. FROM_TTY will be incorrectly 0 but such solib
281 targets should be fixed anyway. Call it only after the solib
282 target has been initialized by solib_create_inferior_hook. */
285 warning (_("platform-specific solib_create_inferior_hook did "
286 "not load initial shared libraries."));
288 /* If the solist is global across processes, there's no need to
290 if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
291 solib_add (nullptr, 0, auto_solib_add
);
295 /* If the user sets watchpoints before execution having started,
296 then she gets software watchpoints, because GDB can't know which
297 target will end up being pushed, or if it supports hardware
298 watchpoints or not. breakpoint_re_set takes care of promoting
299 watchpoints to hardware watchpoints if possible, however, if this
300 new inferior doesn't load shared libraries or we don't pull in
301 symbols from any other source on this target/arch,
302 breakpoint_re_set is never called. Call it now so that software
303 watchpoints get a chance to be promoted to hardware watchpoints
304 if the now pushed target supports hardware watchpoints. */
305 breakpoint_re_set ();
307 gdb::observers::inferior_created
.notify (current_inferior ());
310 /* Kill the inferior if already running. This function is designed
311 to be called when we are about to start the execution of the program
312 from the beginning. Ask the user to confirm that he wants to restart
313 the program being debugged when FROM_TTY is non-null. */
316 kill_if_already_running (int from_tty
)
318 if (inferior_ptid
!= null_ptid
&& target_has_execution ())
320 /* Bail out before killing the program if we will not be able to
322 target_require_runnable ();
325 && !query (_("The program being debugged has been started already.\n\
326 Start it from the beginning? ")))
327 error (_("Program not restarted."));
332 /* See inferior.h. */
335 prepare_execution_command (struct target_ops
*target
, int background
)
337 /* If we get a request for running in the bg but the target
338 doesn't support it, error out. */
339 if (background
&& !target_can_async_p (target
))
340 error (_("Asynchronous execution not supported on this target."));
344 /* If we get a request for running in the fg, then we need to
345 simulate synchronous (fg) execution. Note no cleanup is
346 necessary for this. stdin is re-enabled whenever an error
347 reaches the top level. */
348 all_uis_on_sync_execution_starting ();
352 /* Determine how the new inferior will behave. */
356 /* Run program without any explicit stop during startup. */
359 /* Stop at the beginning of the program's main function. */
362 /* Stop at the first instruction of the program. */
363 RUN_STOP_AT_FIRST_INSN
366 /* Implement the "run" command. Force a stop during program start if
367 requested by RUN_HOW. */
370 run_command_1 (const char *args
, int from_tty
, enum run_how run_how
)
372 struct ui_out
*uiout
= current_uiout
;
373 struct target_ops
*run_target
;
378 scoped_disable_commit_resumed
disable_commit_resumed ("running");
380 kill_if_already_running (from_tty
);
382 init_wait_for_inferior ();
383 clear_breakpoint_hit_counts ();
385 /* Clean up any leftovers from other runs. Some other things from
386 this function should probably be moved into target_pre_inferior. */
387 target_pre_inferior ();
389 /* The comment here used to read, "The exec file is re-read every
390 time we do a generic_mourn_inferior, so we just have to worry
391 about the symbol file." The `generic_mourn_inferior' function
392 gets called whenever the program exits. However, suppose the
393 program exits, and *then* the executable file changes? We need
394 to check again here. Since reopen_exec_file doesn't do anything
395 if the timestamp hasn't changed, I don't see the harm. */
397 reread_symbols (from_tty
);
399 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
400 args
= stripped
.get ();
402 /* Do validation and preparation before possibly changing anything
405 run_target
= find_run_target ();
407 prepare_execution_command (run_target
, async_exec
);
409 if (non_stop
&& !run_target
->supports_non_stop ())
410 error (_("The target does not support running in non-stop mode."));
412 /* Done. Can now set breakpoints, change inferior args, etc. */
414 /* Insert temporary breakpoint in main function if requested. */
415 if (run_how
== RUN_STOP_AT_MAIN
)
417 /* To avoid other inferiors hitting this breakpoint, make it
418 inferior-specific. */
419 std::string arg
= string_printf ("-qualified %s inferior %d",
421 current_inferior ()->num
);
422 tbreak_command (arg
.c_str (), 0);
425 const char *exec_file
= current_program_space
->exec_filename ();
427 /* We keep symbols from add-symbol-file, on the grounds that the
428 user might want to add some symbols before running the program
429 (right?). But sometimes (dynamic loading where the user manually
430 introduces the new symbols with add-symbol-file), the code which
431 the symbols describe does not persist between runs. Currently
432 the user has to manually nuke all symbols between runs if they
433 want them to go away (PR 2207). This is probably reasonable. */
435 /* If there were other args, beside '&', process them. */
437 current_inferior ()->set_args (args
);
441 uiout
->field_string (nullptr, "Starting program");
444 uiout
->field_string ("execfile", exec_file
,
445 file_name_style
.style ());
447 uiout
->field_string ("infargs", current_inferior ()->args ());
452 run_target
->create_inferior (exec_file
,
453 current_inferior ()->args (),
454 current_inferior ()->environment
.envp (),
456 /* to_create_inferior should push the target, so after this point we
457 shouldn't refer to run_target again. */
458 run_target
= nullptr;
460 infrun_debug_show_threads ("immediately after create_process",
461 current_inferior ()->non_exited_threads ());
463 /* We're starting off a new process. When we get out of here, in
464 non-stop mode, finish the state of all threads of that process,
465 but leave other threads alone, as they may be stopped in internal
466 events --- the frontend shouldn't see them as stopped. In
467 all-stop, always finish the state of all threads, as we may be
468 resuming more than just the new process. */
469 process_stratum_target
*finish_target
;
473 finish_target
= current_inferior ()->process_target ();
474 finish_ptid
= ptid_t (current_inferior ()->pid
);
478 finish_target
= nullptr;
479 finish_ptid
= minus_one_ptid
;
481 scoped_finish_thread_state
finish_state (finish_target
, finish_ptid
);
483 /* Pass zero for FROM_TTY, because at this point the "run" command
484 has done its thing; now we are setting up the running program. */
485 post_create_inferior (0);
487 /* Queue a pending event so that the program stops immediately. */
488 if (run_how
== RUN_STOP_AT_FIRST_INSN
)
490 thread_info
*thr
= inferior_thread ();
491 target_waitstatus ws
;
492 ws
.set_stopped (GDB_SIGNAL_0
);
493 thr
->set_pending_waitstatus (ws
);
496 /* Start the target running. Do not use -1 continuation as it would skip
497 breakpoint right at the entry point. */
498 proceed (regcache_read_pc (get_thread_regcache (inferior_thread ())),
501 /* Since there was no error, there's no need to finish the thread
503 finish_state
.release ();
505 disable_commit_resumed
.reset_and_commit ();
509 run_command (const char *args
, int from_tty
)
511 run_command_1 (args
, from_tty
, RUN_NORMAL
);
514 /* Start the execution of the program up until the beginning of the main
518 start_command (const char *args
, int from_tty
)
520 /* Some languages such as Ada need to search inside the program
521 minimal symbols for the location where to put the temporary
522 breakpoint before starting. */
523 if (!have_minimal_symbols (current_program_space
))
524 error (_("No symbol table loaded. Use the \"file\" command."));
526 /* Run the program until reaching the main procedure... */
527 run_command_1 (args
, from_tty
, RUN_STOP_AT_MAIN
);
530 /* Start the execution of the program stopping at the first
534 starti_command (const char *args
, int from_tty
)
536 run_command_1 (args
, from_tty
, RUN_STOP_AT_FIRST_INSN
);
540 proceed_thread_callback (struct thread_info
*thread
, void *arg
)
542 /* We go through all threads individually instead of compressing
543 into a single target `resume_all' request, because some threads
544 may be stopped in internal breakpoints/events, or stopped waiting
545 for its turn in the displaced stepping queue (that is, they are
546 running && !executing). The target side has no idea about why
547 the thread is stopped, so a `resume_all' command would resume too
548 much. If/when GDB gains a way to tell the target `hold this
549 thread stopped until I say otherwise', then we can optimize
551 if (thread
->state
!= THREAD_STOPPED
)
554 if (!thread
->inf
->has_execution ())
557 switch_to_thread (thread
);
558 clear_proceed_status (0);
559 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
564 ensure_valid_thread (void)
566 if (inferior_ptid
== null_ptid
567 || inferior_thread ()->state
== THREAD_EXITED
)
568 error (_("Cannot execute this command without a live selected thread."));
571 /* If the user is looking at trace frames, any resumption of execution
572 is likely to mix up recorded and live target data. So simply
573 disallow those commands. */
576 ensure_not_tfind_mode (void)
578 if (get_traceframe_number () >= 0)
579 error (_("Cannot execute this command while looking at trace frames."));
582 /* Throw an error indicating the current thread is running. */
585 error_is_running (void)
587 error (_("Cannot execute this command while "
588 "the selected thread is running."));
591 /* Calls error_is_running if the current thread is running. */
594 ensure_not_running (void)
596 if (inferior_thread ()->state
== THREAD_RUNNING
)
601 continue_1 (int all_threads
)
604 ensure_not_tfind_mode ();
606 if (non_stop
&& all_threads
)
608 /* Don't error out if the current thread is running, because
609 there may be other stopped threads. */
611 /* Backup current thread and selected frame and restore on scope
613 scoped_restore_current_thread restore_thread
;
614 scoped_disable_commit_resumed disable_commit_resumed
615 ("continue all threads in non-stop");
617 iterate_over_threads (proceed_thread_callback
, nullptr);
619 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
621 /* If all threads in the target were already running,
622 proceed_thread_callback ends up never calling proceed,
623 and so nothing calls this to put the inferior's terminal
624 settings in effect and remove stdin from the event loop,
625 which we must when running a foreground command. E.g.:
629 <all threads are running now>
632 <no thread was resumed, but the inferior now owns the terminal>
634 target_terminal::inferior ();
637 disable_commit_resumed
.reset_and_commit ();
641 ensure_valid_thread ();
642 ensure_not_running ();
643 clear_proceed_status (0);
644 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
648 /* continue [-a] [proceed-count] [&] */
651 continue_command (const char *args
, int from_tty
)
654 bool all_threads_p
= false;
658 /* Find out whether we must run in the background. */
659 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
660 args
= stripped
.get ();
664 if (startswith (args
, "-a"))
666 all_threads_p
= true;
667 args
+= sizeof ("-a") - 1;
673 if (!non_stop
&& all_threads_p
)
674 error (_("`-a' is meaningless in all-stop mode."));
676 if (args
!= nullptr && all_threads_p
)
677 error (_("Can't resume all threads and specify "
678 "proceed count simultaneously."));
680 /* If we have an argument left, set proceed count of breakpoint we
684 bpstat
*bs
= nullptr;
687 struct thread_info
*tp
;
690 tp
= inferior_thread ();
693 process_stratum_target
*last_target
;
696 get_last_target_status (&last_target
, &last_ptid
, nullptr);
697 tp
= last_target
->find_thread (last_ptid
);
700 bs
= tp
->control
.stop_bpstat
;
702 while ((stat
= bpstat_num (&bs
, &num
)) != 0)
705 set_ignore_count (num
,
706 parse_and_eval_long (args
) - 1,
708 /* set_ignore_count prints a message ending with a period.
709 So print two spaces before "Continuing.". */
715 if (!stopped
&& from_tty
)
718 ("Not stopped at any breakpoint; argument ignored.\n");
722 ensure_not_tfind_mode ();
724 if (!non_stop
|| !all_threads_p
)
726 ensure_valid_thread ();
727 ensure_not_running ();
730 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
733 gdb_printf (_("Continuing.\n"));
735 continue_1 (all_threads_p
);
738 /* Record in TP the starting point of a "step" or "next" command. */
741 set_step_frame (thread_info
*tp
)
743 /* This can be removed once this function no longer implicitly relies on the
744 inferior_ptid value. */
745 gdb_assert (inferior_ptid
== tp
->ptid
);
747 frame_info_ptr frame
= get_current_frame ();
749 symtab_and_line sal
= find_frame_sal (frame
);
750 set_step_info (tp
, frame
, sal
);
752 CORE_ADDR pc
= get_frame_pc (frame
);
753 tp
->control
.step_start_function
= find_pc_function (pc
);
756 /* Step until outside of current statement. */
759 step_command (const char *count_string
, int from_tty
)
761 step_1 (0, 0, count_string
);
764 /* Likewise, but skip over subroutine calls as if single instructions. */
767 next_command (const char *count_string
, int from_tty
)
769 step_1 (1, 0, count_string
);
772 /* Likewise, but step only one instruction. */
775 stepi_command (const char *count_string
, int from_tty
)
777 step_1 (0, 1, count_string
);
781 nexti_command (const char *count_string
, int from_tty
)
783 step_1 (1, 1, count_string
);
786 /* Data for the FSM that manages the step/next/stepi/nexti
789 struct step_command_fsm
: public thread_fsm
791 /* How many steps left in a "step N"-like command. */
794 /* If true, this is a next/nexti, otherwise a step/stepi. */
795 int skip_subroutines
;
797 /* If true, this is a stepi/nexti, otherwise a step/step. */
800 explicit step_command_fsm (struct interp
*cmd_interp
)
801 : thread_fsm (cmd_interp
)
805 void clean_up (struct thread_info
*thread
) override
;
806 bool should_stop (struct thread_info
*thread
) override
;
807 enum async_reply_reason
do_async_reply_reason () override
;
810 /* Prepare for a step/next/etc. command. Any target resource
811 allocated here is undone in the FSM's clean_up method. */
814 step_command_fsm_prepare (struct step_command_fsm
*sm
,
815 int skip_subroutines
, int single_inst
,
816 int count
, struct thread_info
*thread
)
818 sm
->skip_subroutines
= skip_subroutines
;
819 sm
->single_inst
= single_inst
;
822 /* Leave the si command alone. */
823 if (!sm
->single_inst
|| sm
->skip_subroutines
)
824 set_longjmp_breakpoint (thread
, get_frame_id (get_current_frame ()));
826 thread
->control
.stepping_command
= 1;
829 static int prepare_one_step (thread_info
*, struct step_command_fsm
*sm
);
832 step_1 (int skip_subroutines
, int single_inst
, const char *count_string
)
836 struct thread_info
*thr
;
837 struct step_command_fsm
*step_sm
;
840 ensure_not_tfind_mode ();
841 ensure_valid_thread ();
842 ensure_not_running ();
844 gdb::unique_xmalloc_ptr
<char> stripped
845 = strip_bg_char (count_string
, &async_exec
);
846 count_string
= stripped
.get ();
848 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
850 count
= count_string
? parse_and_eval_long (count_string
) : 1;
852 clear_proceed_status (1);
854 /* Setup the execution command state machine to handle all the COUNT
856 thr
= inferior_thread ();
857 step_sm
= new step_command_fsm (command_interp ());
858 thr
->set_thread_fsm (std::unique_ptr
<thread_fsm
> (step_sm
));
860 step_command_fsm_prepare (step_sm
, skip_subroutines
,
861 single_inst
, count
, thr
);
863 /* Do only one step for now, before returning control to the event
864 loop. Let the continuation figure out how many other steps we
865 need to do, and handle them one at the time, through
867 if (!prepare_one_step (thr
, step_sm
))
868 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
871 /* Stepped into an inline frame. Pretend that we've
873 thr
->thread_fsm ()->clean_up (thr
);
874 bool proceeded
= normal_stop ();
876 inferior_event_handler (INF_EXEC_COMPLETE
);
877 all_uis_check_sync_execution_done ();
881 /* Implementation of the 'should_stop' FSM method for stepping
882 commands. Called after we are done with one step operation, to
883 check whether we need to step again, before we print the prompt and
884 return control to the user. If count is > 1, returns false, as we
885 will need to keep going. */
888 step_command_fsm::should_stop (struct thread_info
*tp
)
890 if (tp
->control
.stop_step
)
892 /* There are more steps to make, and we did stop due to
893 ending a stepping range. Do another step. */
895 return prepare_one_step (tp
, this);
903 /* Implementation of the 'clean_up' FSM method for stepping commands. */
906 step_command_fsm::clean_up (struct thread_info
*thread
)
908 if (!single_inst
|| skip_subroutines
)
909 delete_longjmp_breakpoint (thread
->global_num
);
912 /* Implementation of the 'async_reply_reason' FSM method for stepping
915 enum async_reply_reason
916 step_command_fsm::do_async_reply_reason ()
918 return EXEC_ASYNC_END_STEPPING_RANGE
;
921 /* Prepare for one step in "step N". The actual target resumption is
922 done by the caller. Return true if we're done and should thus
923 report a stop to the user. Returns false if the target needs to be
927 prepare_one_step (thread_info
*tp
, struct step_command_fsm
*sm
)
929 /* This can be removed once this function no longer implicitly relies on the
930 inferior_ptid value. */
931 gdb_assert (inferior_ptid
== tp
->ptid
);
935 frame_info_ptr frame
= get_current_frame ();
939 if (!sm
->single_inst
)
943 /* Step at an inlined function behaves like "down". */
944 if (!sm
->skip_subroutines
945 && inline_skipped_frames (tp
))
948 const char *fn
= nullptr;
952 /* Pretend that we've ran. */
953 resume_ptid
= user_visible_resume_ptid (1);
954 set_running (tp
->inf
->process_target (), resume_ptid
, true);
956 step_into_inline_frame (tp
);
958 frame
= get_current_frame ();
959 sal
= find_frame_sal (frame
);
960 sym
= get_frame_function (frame
);
963 fn
= sym
->print_name ();
966 || !function_name_is_marked_for_skip (fn
, sal
))
969 return prepare_one_step (tp
, sm
);
973 pc
= get_frame_pc (frame
);
974 find_pc_line_pc_range (pc
,
975 &tp
->control
.step_range_start
,
976 &tp
->control
.step_range_end
);
978 if (execution_direction
== EXEC_REVERSE
)
980 symtab_and_line sal
= find_pc_line (pc
, 0);
981 symtab_and_line sal_start
982 = find_pc_line (tp
->control
.step_range_start
, 0);
984 if (sal
.line
== sal_start
.line
)
985 /* Executing in reverse, the step_range_start address is in
986 the same line. We want to stop in the previous line so
987 move step_range_start before the current line. */
988 tp
->control
.step_range_start
--;
991 /* There's a problem in gcc (PR gcc/98780) that causes missing line
992 table entries, which results in a too large stepping range.
993 Use inlined_subroutine info to make the range more narrow. */
994 if (inline_skipped_frames (tp
) > 0)
996 const symbol
*sym
= inline_skipped_symbol (tp
);
997 if (sym
->aclass () == LOC_BLOCK
)
999 const block
*block
= sym
->value_block ();
1000 if (block
->end () < tp
->control
.step_range_end
)
1001 tp
->control
.step_range_end
= block
->end ();
1005 tp
->control
.may_range_step
= 1;
1007 /* If we have no line info, switch to stepi mode. */
1008 if (tp
->control
.step_range_end
== 0 && step_stop_if_no_debug
)
1010 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1011 tp
->control
.may_range_step
= 0;
1013 else if (tp
->control
.step_range_end
== 0)
1017 if (find_pc_partial_function (pc
, &name
,
1018 &tp
->control
.step_range_start
,
1019 &tp
->control
.step_range_end
) == 0)
1020 error (_("Cannot find bounds of current function"));
1022 target_terminal::ours_for_output ();
1023 gdb_printf (_("Single stepping until exit from function %s,"
1024 "\nwhich has no line number information.\n"),
1030 /* Say we are stepping, but stop after one insn whatever it does. */
1031 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1032 if (!sm
->skip_subroutines
)
1034 Don't step over function calls, not even to functions lacking
1036 tp
->control
.step_over_calls
= STEP_OVER_NONE
;
1039 if (sm
->skip_subroutines
)
1040 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1046 sm
->set_finished ();
1051 /* Continue program at specified address. */
1054 jump_command (const char *arg
, int from_tty
)
1056 struct gdbarch
*gdbarch
= get_current_arch ();
1063 ensure_not_tfind_mode ();
1064 ensure_valid_thread ();
1065 ensure_not_running ();
1067 /* Find out whether we must run in the background. */
1068 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1069 arg
= stripped
.get ();
1071 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1074 error_no_arg (_("starting address"));
1076 std::vector
<symtab_and_line
> sals
1077 = decode_line_with_current_source (arg
, DECODE_LINE_FUNFIRSTLINE
);
1078 if (sals
.size () != 1)
1080 /* If multiple sal-objects were found, try dropping those that aren't
1081 from the current symtab. */
1082 symtab_and_line cursal
1083 = get_current_source_symtab_and_line (current_program_space
);
1084 sals
.erase (std::remove_if (sals
.begin (), sals
.end (),
1085 [&] (const symtab_and_line
&sal
)
1087 return sal
.symtab
!= cursal
.symtab
;
1089 if (sals
.size () != 1)
1090 error (_("Jump request is ambiguous: "
1091 "does not resolve to a single address"));
1094 symtab_and_line
&sal
= sals
[0];
1096 if (sal
.symtab
== 0 && sal
.pc
== 0)
1097 error (_("No source file has been specified."));
1099 resolve_sal_pc (&sal
); /* May error out. */
1101 /* See if we are trying to jump to another function. */
1102 fn
= get_frame_function (get_current_frame ());
1103 sfn
= find_pc_sect_containing_function (sal
.pc
,
1104 find_pc_mapped_section (sal
.pc
));
1105 if (fn
!= nullptr && sfn
!= fn
)
1107 if (!query (_("Line %ps is not in `%s'. Jump anyway? "),
1108 styled_string (line_number_style
.style (),
1109 pulongest (sal
.line
)),
1112 error (_("Not confirmed."));
1119 struct obj_section
*section
;
1121 section
= sfn
->obj_section (sfn
->objfile ());
1122 if (section_is_overlay (section
)
1123 && !section_is_mapped (section
))
1125 if (!query (_("WARNING!!! Destination is in "
1126 "unmapped overlay! Jump anyway? ")))
1128 error (_("Not confirmed."));
1138 gdb_printf (_("Continuing at "));
1139 gdb_puts (paddress (gdbarch
, addr
));
1143 clear_proceed_status (0);
1144 proceed (addr
, GDB_SIGNAL_0
);
1147 /* Continue program giving it specified signal. */
1150 signal_command (const char *signum_exp
, int from_tty
)
1152 enum gdb_signal oursig
;
1155 dont_repeat (); /* Too dangerous. */
1157 ensure_not_tfind_mode ();
1158 ensure_valid_thread ();
1159 ensure_not_running ();
1161 /* Find out whether we must run in the background. */
1162 gdb::unique_xmalloc_ptr
<char> stripped
1163 = strip_bg_char (signum_exp
, &async_exec
);
1164 signum_exp
= stripped
.get ();
1166 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1169 error_no_arg (_("signal number"));
1171 /* It would be even slicker to make signal names be valid expressions,
1172 (the type could be "enum $signal" or some such), then the user could
1173 assign them to convenience variables. */
1174 oursig
= gdb_signal_from_name (signum_exp
);
1176 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1178 /* No, try numeric. */
1179 int num
= parse_and_eval_long (signum_exp
);
1182 oursig
= GDB_SIGNAL_0
;
1184 oursig
= gdb_signal_from_command (num
);
1187 /* Look for threads other than the current that this command ends up
1188 resuming too (due to schedlock off), and warn if they'll get a
1189 signal delivered. "signal 0" is used to suppress a previous
1190 signal, but if the current thread is no longer the one that got
1191 the signal, then the user is potentially suppressing the signal
1192 of the wrong thread. */
1195 int must_confirm
= 0;
1197 /* This indicates what will be resumed. Either a single thread,
1198 a whole process, or all threads of all processes. */
1199 ptid_t resume_ptid
= user_visible_resume_ptid (0);
1200 process_stratum_target
*resume_target
1201 = user_visible_resume_target (resume_ptid
);
1203 thread_info
*current
= inferior_thread ();
1205 for (thread_info
*tp
: all_non_exited_threads (resume_target
, resume_ptid
))
1210 if (tp
->stop_signal () != GDB_SIGNAL_0
1211 && signal_pass_state (tp
->stop_signal ()))
1214 gdb_printf (_("Note:\n"));
1215 gdb_printf (_(" Thread %s previously stopped with signal %s, %s.\n"),
1216 print_thread_id (tp
),
1217 gdb_signal_to_name (tp
->stop_signal ()),
1218 gdb_signal_to_string (tp
->stop_signal ()));
1224 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1225 "still deliver the signals noted above to their respective threads.\n"
1226 "Continue anyway? "),
1227 print_thread_id (inferior_thread ())))
1228 error (_("Not confirmed."));
1233 if (oursig
== GDB_SIGNAL_0
)
1234 gdb_printf (_("Continuing with no signal.\n"));
1236 gdb_printf (_("Continuing with signal %s.\n"),
1237 gdb_signal_to_name (oursig
));
1240 clear_proceed_status (0);
1241 proceed ((CORE_ADDR
) -1, oursig
);
1244 /* Queue a signal to be delivered to the current thread. */
1247 queue_signal_command (const char *signum_exp
, int from_tty
)
1249 enum gdb_signal oursig
;
1250 struct thread_info
*tp
;
1253 ensure_not_tfind_mode ();
1254 ensure_valid_thread ();
1255 ensure_not_running ();
1257 if (signum_exp
== nullptr)
1258 error_no_arg (_("signal number"));
1260 /* It would be even slicker to make signal names be valid expressions,
1261 (the type could be "enum $signal" or some such), then the user could
1262 assign them to convenience variables. */
1263 oursig
= gdb_signal_from_name (signum_exp
);
1265 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1267 /* No, try numeric. */
1268 int num
= parse_and_eval_long (signum_exp
);
1271 oursig
= GDB_SIGNAL_0
;
1273 oursig
= gdb_signal_from_command (num
);
1276 if (oursig
!= GDB_SIGNAL_0
1277 && !signal_pass_state (oursig
))
1278 error (_("Signal handling set to not pass this signal to the program."));
1280 tp
= inferior_thread ();
1281 tp
->set_stop_signal (oursig
);
1284 /* Data for the FSM that manages the until (with no argument)
1287 struct until_next_fsm
: public thread_fsm
1289 /* The thread that as current when the command was executed. */
1292 until_next_fsm (struct interp
*cmd_interp
, int thread
)
1293 : thread_fsm (cmd_interp
),
1298 bool should_stop (struct thread_info
*thread
) override
;
1299 void clean_up (struct thread_info
*thread
) override
;
1300 enum async_reply_reason
do_async_reply_reason () override
;
1303 /* Implementation of the 'should_stop' FSM method for the until (with
1307 until_next_fsm::should_stop (struct thread_info
*tp
)
1309 if (tp
->control
.stop_step
)
1315 /* Implementation of the 'clean_up' FSM method for the until (with no
1319 until_next_fsm::clean_up (struct thread_info
*thread
)
1321 delete_longjmp_breakpoint (thread
->global_num
);
1324 /* Implementation of the 'async_reply_reason' FSM method for the until
1325 (with no arg) command. */
1327 enum async_reply_reason
1328 until_next_fsm::do_async_reply_reason ()
1330 return EXEC_ASYNC_END_STEPPING_RANGE
;
1333 /* Proceed until we reach a different source line with pc greater than
1334 our current one or exit the function. We skip calls in both cases.
1336 Note that eventually this command should probably be changed so
1337 that only source lines are printed out when we hit the breakpoint
1338 we set. This may involve changes to wait_for_inferior and the
1339 proceed status code. */
1342 until_next_command (int from_tty
)
1344 frame_info_ptr frame
;
1346 struct symbol
*func
;
1347 struct symtab_and_line sal
;
1348 struct thread_info
*tp
= inferior_thread ();
1349 int thread
= tp
->global_num
;
1350 struct until_next_fsm
*sm
;
1352 clear_proceed_status (0);
1353 set_step_frame (tp
);
1355 frame
= get_current_frame ();
1357 /* Step until either exited from this function or greater
1358 than the current line (if in symbolic section) or pc (if
1361 pc
= get_frame_pc (frame
);
1362 func
= find_pc_function (pc
);
1366 bound_minimal_symbol msymbol
= lookup_minimal_symbol_by_pc (pc
);
1368 if (msymbol
.minsym
== nullptr)
1369 error (_("Execution is not within a known function."));
1371 tp
->control
.step_range_start
= msymbol
.value_address ();
1372 /* The upper-bound of step_range is exclusive. In order to make PC
1373 within the range, set the step_range_end with PC + 1. */
1374 tp
->control
.step_range_end
= pc
+ 1;
1378 sal
= find_pc_line (pc
, 0);
1380 tp
->control
.step_range_start
= func
->value_block ()->entry_pc ();
1381 tp
->control
.step_range_end
= sal
.end
;
1383 tp
->control
.may_range_step
= 1;
1385 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1387 set_longjmp_breakpoint (tp
, get_frame_id (frame
));
1388 delete_longjmp_breakpoint_cleanup
lj_deleter (thread
);
1390 sm
= new until_next_fsm (command_interp (), tp
->global_num
);
1391 tp
->set_thread_fsm (std::unique_ptr
<thread_fsm
> (sm
));
1392 lj_deleter
.release ();
1394 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1398 until_command (const char *arg
, int from_tty
)
1403 ensure_not_tfind_mode ();
1404 ensure_valid_thread ();
1405 ensure_not_running ();
1407 /* Find out whether we must run in the background. */
1408 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1409 arg
= stripped
.get ();
1411 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1414 until_break_command (arg
, from_tty
, 0);
1416 until_next_command (from_tty
);
1420 advance_command (const char *arg
, int from_tty
)
1425 ensure_not_tfind_mode ();
1426 ensure_valid_thread ();
1427 ensure_not_running ();
1430 error_no_arg (_("a location"));
1432 /* Find out whether we must run in the background. */
1433 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1434 arg
= stripped
.get ();
1436 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1438 until_break_command (arg
, from_tty
, 1);
1441 /* See inferior.h. */
1444 get_return_value (struct symbol
*func_symbol
, struct value
*function
)
1446 regcache
*stop_regs
= get_thread_regcache (inferior_thread ());
1447 struct gdbarch
*gdbarch
= stop_regs
->arch ();
1448 struct value
*value
;
1450 struct type
*value_type
1451 = check_typedef (func_symbol
->type ()->target_type ());
1452 gdb_assert (value_type
->code () != TYPE_CODE_VOID
);
1454 if (is_nocall_function (check_typedef (function
->type ())))
1456 warning (_("Function '%s' does not follow the target calling "
1457 "convention, cannot determine its returned value."),
1458 func_symbol
->print_name ());
1463 /* FIXME: 2003-09-27: When returning from a nested inferior function
1464 call, it's possible (with no help from the architecture vector)
1465 to locate and return/print a "struct return" value. This is just
1466 a more complicated case of what is already being done in the
1467 inferior function call code. In fact, when inferior function
1468 calls are made async, this will likely be made the norm. */
1470 switch (gdbarch_return_value_as_value (gdbarch
, function
, value_type
,
1471 nullptr, nullptr, nullptr))
1473 case RETURN_VALUE_REGISTER_CONVENTION
:
1474 case RETURN_VALUE_ABI_RETURNS_ADDRESS
:
1475 case RETURN_VALUE_ABI_PRESERVES_ADDRESS
:
1476 gdbarch_return_value_as_value (gdbarch
, function
, value_type
, stop_regs
,
1479 case RETURN_VALUE_STRUCT_CONVENTION
:
1483 internal_error (_("bad switch"));
1489 /* Helper for print_return_value. */
1492 print_return_value_1 (struct ui_out
*uiout
, struct return_value_info
*rv
)
1494 if (rv
->value
!= nullptr)
1497 uiout
->text ("Value returned is ");
1498 uiout
->field_fmt ("gdb-result-var", "$%d",
1499 rv
->value_history_index
);
1500 uiout
->text (" = ");
1504 struct value_print_options opts
;
1505 get_user_print_options (&opts
);
1508 value_print (rv
->value
, &stb
, &opts
);
1509 uiout
->field_stream ("return-value", stb
);
1512 uiout
->field_string ("return-value", _("<not displayed>"),
1513 metadata_style
.style ());
1518 std::string type_name
= type_to_string (rv
->type
);
1519 uiout
->text ("Value returned has type: ");
1520 uiout
->field_string ("return-type", type_name
);
1522 uiout
->text (" Cannot determine contents\n");
1526 /* Print the result of a function at the end of a 'finish' command.
1527 RV points at an object representing the captured return value/type
1528 and its position in the value history. */
1531 print_return_value (struct ui_out
*uiout
, struct return_value_info
*rv
)
1533 if (rv
->type
== nullptr
1534 || check_typedef (rv
->type
)->code () == TYPE_CODE_VOID
)
1539 /* print_return_value_1 can throw an exception in some
1540 circumstances. We need to catch this so that we still
1541 delete the breakpoint. */
1542 print_return_value_1 (uiout
, rv
);
1544 catch (const gdb_exception_error
&ex
)
1546 exception_print (gdb_stdout
, ex
);
1550 /* Data for the FSM that manages the finish command. */
1552 struct finish_command_fsm
: public thread_fsm
1554 /* The momentary breakpoint set at the function's return address in
1556 breakpoint_up breakpoint
;
1558 /* The function that we're stepping out of. */
1559 struct symbol
*function
= nullptr;
1561 /* If the FSM finishes successfully, this stores the function's
1563 struct return_value_info return_value_info
{};
1565 /* If the current function uses the "struct return convention",
1566 this holds the address at which the value being returned will
1567 be stored, or zero if that address could not be determined or
1568 the "struct return convention" is not being used. */
1569 CORE_ADDR return_buf
;
1571 explicit finish_command_fsm (struct interp
*cmd_interp
)
1572 : thread_fsm (cmd_interp
)
1576 bool should_stop (struct thread_info
*thread
) override
;
1577 void clean_up (struct thread_info
*thread
) override
;
1578 struct return_value_info
*return_value () override
;
1579 enum async_reply_reason
do_async_reply_reason () override
;
1582 /* Implementation of the 'should_stop' FSM method for the finish
1583 commands. Detects whether the thread stepped out of the function
1584 successfully, and if so, captures the function's return value and
1585 marks the FSM finished. */
1588 finish_command_fsm::should_stop (struct thread_info
*tp
)
1590 struct return_value_info
*rv
= &return_value_info
;
1592 if (function
!= nullptr
1593 && bpstat_find_breakpoint (tp
->control
.stop_bpstat
,
1594 breakpoint
.get ()) != nullptr)
1599 rv
->type
= function
->type ()->target_type ();
1600 if (rv
->type
== nullptr)
1601 internal_error (_("finish_command: function has no target type"));
1603 if (check_typedef (rv
->type
)->code () != TYPE_CODE_VOID
)
1607 func
= read_var_value (function
, nullptr, get_current_frame ());
1609 if (return_buf
!= 0)
1610 /* Retrieve return value from the buffer where it was saved. */
1611 rv
->value
= value_at (rv
->type
, return_buf
);
1613 rv
->value
= get_return_value (function
, func
);
1615 if (rv
->value
!= nullptr)
1616 rv
->value_history_index
= rv
->value
->record_latest ();
1619 else if (tp
->control
.stop_step
)
1621 /* Finishing from an inline frame, or reverse finishing. In
1622 either case, there's no way to retrieve the return value. */
1629 /* Implementation of the 'clean_up' FSM method for the finish
1633 finish_command_fsm::clean_up (struct thread_info
*thread
)
1635 breakpoint
.reset ();
1636 delete_longjmp_breakpoint (thread
->global_num
);
1639 /* Implementation of the 'return_value' FSM method for the finish
1642 struct return_value_info
*
1643 finish_command_fsm::return_value ()
1645 return &return_value_info
;
1648 /* Implementation of the 'async_reply_reason' FSM method for the
1651 enum async_reply_reason
1652 finish_command_fsm::do_async_reply_reason ()
1654 if (execution_direction
== EXEC_REVERSE
)
1655 return EXEC_ASYNC_END_STEPPING_RANGE
;
1657 return EXEC_ASYNC_FUNCTION_FINISHED
;
1660 /* finish_backward -- helper function for finish_command. */
1663 finish_backward (struct finish_command_fsm
*sm
)
1665 struct symtab_and_line sal
;
1666 struct thread_info
*tp
= inferior_thread ();
1668 CORE_ADDR func_addr
;
1669 CORE_ADDR alt_entry_point
;
1670 CORE_ADDR entry_point
;
1671 frame_info_ptr frame
= get_selected_frame (nullptr);
1672 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1674 pc
= get_frame_pc (get_current_frame ());
1676 if (find_pc_partial_function (pc
, nullptr, &func_addr
, nullptr) == 0)
1677 error (_("Cannot find bounds of current function"));
1679 sal
= find_pc_line (func_addr
, 0);
1680 alt_entry_point
= sal
.pc
;
1681 entry_point
= alt_entry_point
;
1683 if (gdbarch_skip_entrypoint_p (gdbarch
))
1684 /* Some architectures, like PowerPC use local and global entry points.
1685 There is only one Entry Point (GEP = LEP) for other architectures.
1686 The GEP is an alternate entry point. The LEP is the normal entry point.
1687 The value of entry_point was initialized to the alternate entry point
1688 (GEP). It will be adjusted to the normal entry point if the function
1689 has two entry points. */
1690 entry_point
= gdbarch_skip_entrypoint (gdbarch
, sal
.pc
);
1692 tp
->control
.proceed_to_finish
= 1;
1693 /* Special case: if we're sitting at the function entry point,
1694 then all we need to do is take a reverse singlestep. We
1695 don't need to set a breakpoint, and indeed it would do us
1698 Note that this can only happen at frame #0, since there's
1699 no way that a function up the stack can have a return address
1700 that's equal to its entry point. */
1702 if ((pc
< alt_entry_point
) || (pc
> entry_point
))
1704 /* We are in the body of the function. Set a breakpoint to go back to
1705 the normal entry point. */
1706 symtab_and_line sr_sal
;
1707 sr_sal
.pc
= entry_point
;
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 are either at one of the entry points or between the entry points.
1717 If we are not at the alt_entry point, go back to the alt_entry_point
1718 If we at the normal entry point step back one instruction, when we
1719 stop we will determine if we entered via the entry point or the
1720 alternate entry point. If we are at the alternate entry point,
1721 single step back to the function call. */
1722 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1723 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1727 /* finish_forward -- helper function for finish_command. FRAME is the
1728 frame that called the function we're about to step out of. */
1731 finish_forward (struct finish_command_fsm
*sm
, const frame_info_ptr
&frame
)
1733 struct frame_id frame_id
= get_frame_id (frame
);
1734 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1735 struct symtab_and_line sal
;
1736 struct thread_info
*tp
= inferior_thread ();
1738 sal
= find_pc_line (get_frame_pc (frame
), 0);
1739 sal
.pc
= get_frame_pc (frame
);
1741 sm
->breakpoint
= set_momentary_breakpoint (gdbarch
, sal
,
1742 get_stack_frame_id (frame
),
1745 set_longjmp_breakpoint (tp
, frame_id
);
1747 /* We want to print return value, please... */
1748 tp
->control
.proceed_to_finish
= 1;
1750 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1753 /* Skip frames for "finish". */
1755 static frame_info_ptr
1756 skip_finish_frames (const frame_info_ptr
&initial_frame
)
1758 frame_info_ptr start
;
1759 frame_info_ptr frame
= initial_frame
;
1765 frame
= skip_tailcall_frames (frame
);
1766 if (frame
== nullptr)
1769 frame
= skip_unwritable_frames (frame
);
1770 if (frame
== nullptr)
1773 while (start
!= frame
);
1778 /* "finish": Set a temporary breakpoint at the place the selected
1779 frame will return to, then continue. */
1782 finish_command (const char *arg
, int from_tty
)
1784 frame_info_ptr frame
;
1786 struct finish_command_fsm
*sm
;
1787 struct thread_info
*tp
;
1790 ensure_not_tfind_mode ();
1791 ensure_valid_thread ();
1792 ensure_not_running ();
1794 /* Find out whether we must run in the background. */
1795 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1796 arg
= stripped
.get ();
1798 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1801 error (_("The \"finish\" command does not take any arguments."));
1803 frame
= get_prev_frame (get_selected_frame (_("No selected frame.")));
1805 error (_("\"finish\" not meaningful in the outermost frame."));
1807 clear_proceed_status (0);
1809 tp
= inferior_thread ();
1811 sm
= new finish_command_fsm (command_interp ());
1813 tp
->set_thread_fsm (std::unique_ptr
<thread_fsm
> (sm
));
1815 /* Finishing from an inline frame is completely different. We don't
1816 try to show the "return value" - no way to locate it. */
1817 if (get_frame_type (get_selected_frame (_("No selected frame.")))
1820 /* Claim we are stepping in the calling frame. An empty step
1821 range means that we will stop once we aren't in a function
1822 called by that frame. We don't use the magic "1" value for
1823 step_range_end, because then infrun will think this is nexti,
1824 and not step over the rest of this inlined function call. */
1825 set_step_info (tp
, frame
, {});
1826 tp
->control
.step_range_start
= get_frame_pc (frame
);
1827 tp
->control
.step_range_end
= tp
->control
.step_range_start
;
1828 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1830 /* Print info on the selected frame, including level number but not
1834 gdb_printf (_("Run till exit from "));
1835 print_stack_frame (get_selected_frame (nullptr), 1, LOCATION
, 0);
1838 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1842 /* Find the function we will return from. */
1843 frame_info_ptr callee_frame
= get_selected_frame (nullptr);
1844 sm
->function
= find_pc_function (get_frame_pc (callee_frame
));
1845 sm
->return_buf
= 0; /* Initialize buffer address is not available. */
1847 /* Determine the return convention. If it is RETURN_VALUE_STRUCT_CONVENTION,
1848 attempt to determine the address of the return buffer. */
1849 if (sm
->function
!= nullptr)
1851 enum return_value_convention return_value
;
1852 struct gdbarch
*gdbarch
= get_frame_arch (callee_frame
);
1854 struct type
* val_type
1855 = check_typedef (sm
->function
->type ()->target_type ());
1858 = gdbarch_return_value_as_value (gdbarch
,
1859 read_var_value (sm
->function
, nullptr,
1861 val_type
, nullptr, nullptr, nullptr);
1863 if (return_value
== RETURN_VALUE_STRUCT_CONVENTION
1864 && val_type
->code () != TYPE_CODE_VOID
)
1865 sm
->return_buf
= gdbarch_get_return_buf_addr (gdbarch
, val_type
,
1869 /* Print info on the selected frame, including level number but not
1873 if (execution_direction
== EXEC_REVERSE
)
1874 gdb_printf (_("Run back to call of "));
1877 if (sm
->function
!= nullptr && TYPE_NO_RETURN (sm
->function
->type ())
1878 && !query (_("warning: Function %s does not return normally.\n"
1879 "Try to finish anyway? "),
1880 sm
->function
->print_name ()))
1881 error (_("Not confirmed."));
1882 gdb_printf (_("Run till exit from "));
1885 print_stack_frame (callee_frame
, 1, LOCATION
, 0);
1888 if (execution_direction
== EXEC_REVERSE
)
1889 finish_backward (sm
);
1892 frame
= skip_finish_frames (frame
);
1894 if (frame
== nullptr)
1895 error (_("Cannot find the caller frame."));
1897 finish_forward (sm
, frame
);
1903 info_program_command (const char *args
, int from_tty
)
1905 scoped_restore_current_thread restore_thread
;
1909 /* In non-stop, since every thread is controlled individually, we'll
1910 show execution info about the current thread. In all-stop, we'll
1911 show execution info about the last stop. */
1915 if (!target_has_execution ())
1917 gdb_printf (_("The program being debugged is not being run.\n"));
1921 if (inferior_ptid
== null_ptid
)
1922 error (_("No selected thread."));
1924 tp
= inferior_thread ();
1926 gdb_printf (_("Selected thread %s (%s).\n"),
1927 print_thread_id (tp
),
1928 target_pid_to_str (tp
->ptid
).c_str ());
1930 if (tp
->state
== THREAD_EXITED
)
1932 gdb_printf (_("Selected thread has exited.\n"));
1935 else if (tp
->state
== THREAD_RUNNING
)
1937 gdb_printf (_("Selected thread is running.\n"));
1943 tp
= get_previous_thread ();
1947 gdb_printf (_("The program being debugged is not being run.\n"));
1951 switch_to_thread (tp
);
1953 gdb_printf (_("Last stopped for thread %s (%s).\n"),
1954 print_thread_id (tp
),
1955 target_pid_to_str (tp
->ptid
).c_str ());
1957 if (tp
->state
== THREAD_EXITED
)
1959 gdb_printf (_("Thread has since exited.\n"));
1963 if (tp
->state
== THREAD_RUNNING
)
1965 gdb_printf (_("Thread is now running.\n"));
1971 bpstat
*bs
= tp
->control
.stop_bpstat
;
1972 int stat
= bpstat_num (&bs
, &num
);
1974 target_files_info ();
1975 gdb_printf (_("Program stopped at %s.\n"),
1976 paddress (current_inferior ()->arch (), tp
->stop_pc ()));
1977 if (tp
->control
.stop_step
)
1978 gdb_printf (_("It stopped after being stepped.\n"));
1981 /* There may be several breakpoints in the same place, so this
1982 isn't as strange as it seems. */
1987 gdb_printf (_("It stopped at a breakpoint "
1988 "that has since been deleted.\n"));
1991 gdb_printf (_("It stopped at breakpoint %d.\n"), num
);
1992 stat
= bpstat_num (&bs
, &num
);
1995 else if (tp
->stop_signal () != GDB_SIGNAL_0
)
1997 gdb_printf (_("It stopped with signal %s, %s.\n"),
1998 gdb_signal_to_name (tp
->stop_signal ()),
1999 gdb_signal_to_string (tp
->stop_signal ()));
2003 gdb_printf (_("Type \"%ps\" or \"%ps\" for more information.\n"),
2004 styled_string (command_style
.style (), "info stack"),
2005 styled_string (command_style
.style (), "info registers"));
2009 environment_info (const char *var
, int from_tty
)
2013 const char *val
= current_inferior ()->environment
.get (var
);
2024 gdb_puts ("Environment variable \"");
2026 gdb_puts ("\" not defined.\n");
2031 char **envp
= current_inferior ()->environment
.envp ();
2033 for (int idx
= 0; envp
[idx
] != nullptr; ++idx
)
2035 gdb_puts (envp
[idx
]);
2042 set_environment_command (const char *arg
, int from_tty
)
2044 const char *p
, *val
;
2048 error_no_arg (_("environment variable and value"));
2050 /* Find separation between variable name and value. */
2051 p
= (char *) strchr (arg
, '=');
2052 val
= (char *) strchr (arg
, ' ');
2054 if (p
!= 0 && val
!= 0)
2056 /* We have both a space and an equals. If the space is before the
2057 equals, walk forward over the spaces til we see a nonspace
2058 (possibly the equals). */
2063 /* Now if the = is after the char following the spaces,
2064 take the char following the spaces. */
2068 else if (val
!= 0 && p
== 0)
2072 error_no_arg (_("environment variable to set"));
2074 if (p
== 0 || p
[1] == 0)
2078 p
= arg
+ strlen (arg
); /* So that savestring below will work. */
2082 /* Not setting variable value to null. */
2084 while (*val
== ' ' || *val
== '\t')
2088 while (p
!= arg
&& (p
[-1] == ' ' || p
[-1] == '\t'))
2091 std::string
var (arg
, p
- arg
);
2094 gdb_printf (_("Setting environment variable "
2095 "\"%s\" to null value.\n"),
2097 current_inferior ()->environment
.set (var
.c_str (), "");
2100 current_inferior ()->environment
.set (var
.c_str (), val
);
2104 unset_environment_command (const char *var
, int from_tty
)
2108 /* If there is no argument, delete all environment variables.
2109 Ask for confirmation if reading from the terminal. */
2110 if (!from_tty
|| query (_("Delete all environment variables? ")))
2111 current_inferior ()->environment
.clear ();
2114 current_inferior ()->environment
.unset (var
);
2117 /* Handle the execution path (PATH variable). */
2119 static const char path_var_name
[] = "PATH";
2122 path_info (const char *args
, int from_tty
)
2124 gdb_puts ("Executable and object file path: ");
2125 gdb_puts (current_inferior ()->environment
.get (path_var_name
));
2129 /* Add zero or more directories to the front of the execution path. */
2132 path_command (const char *dirname
, int from_tty
)
2137 env
= current_inferior ()->environment
.get (path_var_name
);
2138 /* Can be null if path is not set. */
2141 std::string exec_path
= env
;
2142 mod_path (dirname
, exec_path
);
2143 current_inferior ()->environment
.set (path_var_name
, exec_path
.c_str ());
2145 path_info (nullptr, from_tty
);
2150 pad_to_column (string_file
&stream
, int col
)
2152 /* At least one space must be printed to separate columns. */
2154 const int size
= stream
.size ();
2156 stream
.puts (n_spaces (col
- size
));
2159 /* Print out the register NAME with value VAL, to FILE, in the default
2163 default_print_one_register_info (struct ui_file
*file
,
2167 struct type
*regtype
= val
->type ();
2168 int print_raw_format
;
2169 string_file format_stream
;
2172 value_column_1
= 15,
2173 /* Give enough room for "0x", 16 hex digits and two spaces in
2174 preceding column. */
2175 value_column_2
= value_column_1
+ 2 + 16 + 2,
2178 format_stream
.puts (name
);
2179 pad_to_column (format_stream
, value_column_1
);
2181 print_raw_format
= (val
->entirely_available ()
2182 && !val
->optimized_out ());
2184 /* If virtual format is floating, print it that way, and in raw
2186 if (regtype
->code () == TYPE_CODE_FLT
2187 || regtype
->code () == TYPE_CODE_DECFLOAT
)
2189 struct value_print_options opts
;
2190 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
2191 enum bfd_endian byte_order
= type_byte_order (regtype
);
2193 get_user_print_options (&opts
);
2194 opts
.deref_ref
= true;
2196 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2198 if (print_raw_format
)
2200 pad_to_column (format_stream
, value_column_2
);
2201 format_stream
.puts ("(raw ");
2202 print_hex_chars (&format_stream
, valaddr
, regtype
->length (),
2204 format_stream
.putc (')');
2209 struct value_print_options opts
;
2211 /* Print the register in hex. */
2212 get_formatted_print_options (&opts
, 'x');
2213 opts
.deref_ref
= true;
2214 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2215 /* If not a vector register, print it also according to its
2217 if (print_raw_format
&& regtype
->is_vector () == 0)
2219 pad_to_column (format_stream
, value_column_2
);
2220 get_user_print_options (&opts
);
2221 opts
.deref_ref
= true;
2222 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2226 gdb_puts (format_stream
.c_str (), file
);
2227 gdb_printf (file
, "\n");
2230 /* Print out the machine register regnum. If regnum is -1, print all
2231 registers (print_all == 1) or all non-float and non-vector
2232 registers (print_all == 0).
2234 For most machines, having all_registers_info() print the
2235 register(s) one per line is good enough. If a different format is
2236 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2237 regs), or there is an existing convention for showing all the
2238 registers, define the architecture method PRINT_REGISTERS_INFO to
2239 provide that format. */
2242 default_print_registers_info (struct gdbarch
*gdbarch
,
2243 struct ui_file
*file
,
2244 const frame_info_ptr
&frame
,
2245 int regnum
, int print_all
)
2248 const int numregs
= gdbarch_num_cooked_regs (gdbarch
);
2250 for (i
= 0; i
< numregs
; i
++)
2252 /* Decide between printing all regs, non-float / vector regs, or
2258 if (!gdbarch_register_reggroup_p (gdbarch
, i
, all_reggroup
))
2263 if (!gdbarch_register_reggroup_p (gdbarch
, i
, general_reggroup
))
2273 /* If the register name is empty, it is undefined for this
2274 processor, so don't display anything. */
2275 if (*(gdbarch_register_name (gdbarch
, i
)) == '\0')
2278 default_print_one_register_info
2279 (file
, gdbarch_register_name (gdbarch
, i
),
2280 value_of_register (i
, get_next_frame_sentinel_okay (frame
)));
2285 registers_info (const char *addr_exp
, int fpregs
)
2287 frame_info_ptr frame
;
2288 struct gdbarch
*gdbarch
;
2290 if (!target_has_registers ())
2291 error (_("The program has no registers now."));
2292 frame
= get_selected_frame (nullptr);
2293 gdbarch
= get_frame_arch (frame
);
2297 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2302 while (*addr_exp
!= '\0')
2307 /* Skip leading white space. */
2308 addr_exp
= skip_spaces (addr_exp
);
2310 /* Discard any leading ``$''. Check that there is something
2311 resembling a register following it. */
2312 if (addr_exp
[0] == '$')
2314 if (isspace ((*addr_exp
)) || (*addr_exp
) == '\0')
2315 error (_("Missing register name"));
2317 /* Find the start/end of this register name/num/group. */
2319 while ((*addr_exp
) != '\0' && !isspace ((*addr_exp
)))
2323 /* Figure out what we've found and display it. */
2325 /* A register name? */
2327 int regnum
= user_reg_map_name_to_regnum (gdbarch
, start
, end
- start
);
2331 /* User registers lie completely outside of the range of
2332 normal registers. Catch them early so that the target
2334 if (regnum
>= gdbarch_num_cooked_regs (gdbarch
))
2336 struct value
*regval
= value_of_user_reg (regnum
, frame
);
2337 const char *regname
= user_reg_map_regnum_to_name (gdbarch
,
2340 /* Print in the same fashion
2341 gdbarch_print_registers_info's default
2342 implementation prints. */
2343 default_print_one_register_info (gdb_stdout
,
2348 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2349 frame
, regnum
, fpregs
);
2354 /* A register group? */
2356 const struct reggroup
*group
= nullptr;
2357 for (const struct reggroup
*g
: gdbarch_reggroups (gdbarch
))
2359 /* Don't bother with a length check. Should the user
2360 enter a short register group name, go with the first
2361 group that matches. */
2362 if (strncmp (start
, g
->name (), end
- start
) == 0)
2368 if (group
!= nullptr)
2373 regnum
< gdbarch_num_cooked_regs (gdbarch
);
2376 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
2377 gdbarch_print_registers_info (gdbarch
,
2385 /* Nothing matched. */
2386 error (_("Invalid register `%.*s'"), (int) (end
- start
), start
);
2391 info_all_registers_command (const char *addr_exp
, int from_tty
)
2393 registers_info (addr_exp
, 1);
2397 info_registers_command (const char *addr_exp
, int from_tty
)
2399 registers_info (addr_exp
, 0);
2403 print_vector_info (struct ui_file
*file
,
2404 const frame_info_ptr
&frame
, const char *args
)
2406 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
2408 if (gdbarch_print_vector_info_p (gdbarch
))
2409 gdbarch_print_vector_info (gdbarch
, file
, frame
, args
);
2413 int printed_something
= 0;
2415 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2417 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, vector_reggroup
))
2419 printed_something
= 1;
2420 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2423 if (!printed_something
)
2424 gdb_printf (file
, "No vector information\n");
2429 info_vector_command (const char *args
, int from_tty
)
2431 if (!target_has_registers ())
2432 error (_("The program has no registers now."));
2434 print_vector_info (gdb_stdout
, get_selected_frame (nullptr), args
);
2437 /* Kill the inferior process. Make us have no inferior. */
2440 kill_command (const char *arg
, int from_tty
)
2442 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2443 It should be a distinct flag that indicates that a target is active, cuz
2444 some targets don't have processes! */
2446 if (inferior_ptid
== null_ptid
)
2447 error (_("The program is not being run."));
2448 if (!query (_("Kill the program being debugged? ")))
2449 error (_("Not confirmed."));
2451 int pid
= current_inferior ()->pid
;
2452 /* Save the pid as a string before killing the inferior, since that
2453 may unpush the current target, and we need the string after. */
2454 std::string pid_str
= target_pid_to_str (ptid_t (pid
));
2455 int infnum
= current_inferior ()->num
;
2459 update_previous_thread ();
2461 if (print_inferior_events
)
2462 gdb_printf (_("[Inferior %d (%s) killed]\n"),
2463 infnum
, pid_str
.c_str ());
2466 /* Used in `attach&' command. Proceed threads of inferior INF iff
2467 they stopped due to debugger request, and when they did, they
2468 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2469 have been explicitly been told to stop. */
2472 proceed_after_attach (inferior
*inf
)
2474 /* Don't error out if the current thread is running, because
2475 there may be other stopped threads. */
2477 /* Backup current thread and selected frame. */
2478 scoped_restore_current_thread restore_thread
;
2480 for (thread_info
*thread
: inf
->non_exited_threads ())
2481 if (!thread
->executing ()
2482 && !thread
->stop_requested
2483 && thread
->stop_signal () == GDB_SIGNAL_0
)
2485 switch_to_thread (thread
);
2486 clear_proceed_status (0);
2487 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2491 /* See inferior.h. */
2494 setup_inferior (int from_tty
)
2496 struct inferior
*inferior
;
2498 inferior
= current_inferior ();
2499 inferior
->needs_setup
= false;
2501 /* If no exec file is yet known, try to determine it from the
2503 if (current_program_space
->exec_filename () == nullptr)
2504 exec_file_locate_attach (inferior_ptid
.pid (), 1, from_tty
);
2507 reopen_exec_file ();
2508 reread_symbols (from_tty
);
2511 /* Take any necessary post-attaching actions for this platform. */
2512 target_post_attach (inferior_ptid
.pid ());
2514 post_create_inferior (from_tty
);
2517 /* What to do after the first program stops after attaching. */
2518 enum attach_post_wait_mode
2520 /* Do nothing. Leaves threads as they are. */
2521 ATTACH_POST_WAIT_NOTHING
,
2523 /* Re-resume threads that are marked running. */
2524 ATTACH_POST_WAIT_RESUME
,
2526 /* Stop all threads. */
2527 ATTACH_POST_WAIT_STOP
,
2530 /* Called after we've attached to a process and we've seen it stop for
2531 the first time. Resume, stop, or don't touch the threads according
2535 attach_post_wait (int from_tty
, enum attach_post_wait_mode mode
)
2537 struct inferior
*inferior
;
2539 inferior
= current_inferior ();
2540 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
2542 if (inferior
->needs_setup
)
2543 setup_inferior (from_tty
);
2545 if (mode
== ATTACH_POST_WAIT_RESUME
)
2547 /* The user requested an `attach&', so be sure to leave threads
2548 that didn't get a signal running. */
2550 /* Immediately resume all suspended threads of this inferior,
2551 and this inferior only. This should have no effect on
2552 already running threads. If a thread has been stopped with a
2553 signal, leave it be. */
2555 proceed_after_attach (inferior
);
2558 if (inferior_thread ()->stop_signal () == GDB_SIGNAL_0
)
2560 clear_proceed_status (0);
2561 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2565 else if (mode
== ATTACH_POST_WAIT_STOP
)
2567 /* The user requested a plain `attach', so be sure to leave
2568 the inferior stopped. */
2570 /* At least the current thread is already stopped. */
2572 /* In all-stop, by definition, all threads have to be already
2573 stopped at this point. In non-stop, however, although the
2574 selected thread is stopped, others may still be executing.
2575 Be sure to explicitly stop all threads of the process. This
2576 should have no effect on already stopped threads. */
2578 target_stop (ptid_t (inferior
->pid
));
2579 else if (target_is_non_stop_p ())
2581 struct thread_info
*lowest
= inferior_thread ();
2583 stop_all_threads ("attaching");
2585 /* It's not defined which thread will report the attach
2586 stop. For consistency, always select the thread with
2587 lowest GDB number, which should be the main thread, if it
2589 for (thread_info
*thread
: current_inferior ()->non_exited_threads ())
2590 if (thread
->inf
->num
< lowest
->inf
->num
2591 || thread
->per_inf_num
< lowest
->per_inf_num
)
2594 switch_to_thread (lowest
);
2597 /* Tell the user/frontend where we're stopped. */
2599 if (deprecated_attach_hook
)
2600 deprecated_attach_hook ();
2604 /* "attach" command entry point. Takes a program started up outside
2605 of gdb and ``attaches'' to it. This stops it cold in its tracks
2606 and allows us to start debugging it. */
2609 attach_command (const char *args
, int from_tty
)
2612 struct target_ops
*attach_target
;
2613 struct inferior
*inferior
= current_inferior ();
2614 enum attach_post_wait_mode mode
;
2616 dont_repeat (); /* Not for the faint of heart */
2618 scoped_disable_commit_resumed
disable_commit_resumed ("attaching");
2620 if (gdbarch_has_global_solist (current_inferior ()->arch ()))
2621 /* Don't complain if all processes share the same symbol
2624 else if (target_has_execution ())
2626 if (query (_("A program is being debugged already. Kill it? ")))
2629 error (_("Not killed."));
2632 /* Clean up any leftovers from other runs. Some other things from
2633 this function should probably be moved into target_pre_inferior. */
2634 target_pre_inferior ();
2636 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
2637 args
= stripped
.get ();
2639 attach_target
= find_attach_target ();
2641 prepare_execution_command (attach_target
, async_exec
);
2643 if (non_stop
&& !attach_target
->supports_non_stop ())
2644 error (_("Cannot attach to this target in non-stop mode"));
2646 attach_target
->attach (args
, from_tty
);
2647 /* to_attach should push the target, so after this point we
2648 shouldn't refer to attach_target again. */
2649 attach_target
= nullptr;
2651 infrun_debug_show_threads ("immediately after attach",
2652 current_inferior ()->non_exited_threads ());
2654 /* Enable async mode if it is supported by the target. */
2655 if (target_can_async_p ())
2656 target_async (true);
2658 /* Set up the "saved terminal modes" of the inferior
2659 based on what modes we are starting it with. */
2660 target_terminal::init ();
2662 /* Install inferior's terminal modes. This may look like a no-op,
2663 as we've just saved them above, however, this does more than
2664 restore terminal settings:
2666 - installs a SIGINT handler that forwards SIGINT to the inferior.
2667 Otherwise a Ctrl-C pressed just while waiting for the initial
2668 stop would end up as a spurious Quit.
2670 - removes stdin from the event loop, which we need if attaching
2671 in the foreground, otherwise on targets that report an initial
2672 stop on attach (which are most) we'd process input/commands
2673 while we're in the event loop waiting for that stop. That is,
2674 before the attach continuation runs and the command is really
2676 target_terminal::inferior ();
2678 /* Set up execution context to know that we should return from
2679 wait_for_inferior as soon as the target reports a stop. */
2680 init_wait_for_inferior ();
2682 inferior
->needs_setup
= true;
2684 if (target_is_non_stop_p ())
2686 /* If we find that the current thread isn't stopped, explicitly
2687 do so now, because we're going to install breakpoints and
2691 /* The user requested an `attach&'; stop just one thread. */
2692 target_stop (inferior_ptid
);
2694 /* The user requested an `attach', so stop all threads of this
2696 target_stop (ptid_t (inferior_ptid
.pid ()));
2699 /* Check for exec file mismatch, and let the user solve it. */
2700 validate_exec_file (from_tty
);
2702 mode
= async_exec
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_STOP
;
2704 /* Some system don't generate traps when attaching to inferior.
2705 E.g. Mach 3 or GNU hurd. */
2706 if (!target_attach_no_wait ())
2708 /* Careful here. See comments in inferior.h. Basically some
2709 OSes don't ignore SIGSTOPs on continue requests anymore. We
2710 need a way for handle_inferior_event to reset the stop_signal
2711 variable after an attach, and this is what
2712 STOP_QUIETLY_NO_SIGSTOP is for. */
2713 inferior
->control
.stop_soon
= STOP_QUIETLY_NO_SIGSTOP
;
2715 /* Wait for stop. */
2716 inferior
->add_continuation ([=] ()
2718 attach_post_wait (from_tty
, mode
);
2721 /* Let infrun consider waiting for events out of this
2723 inferior
->process_target ()->threads_executing
= true;
2725 if (!target_is_async_p ())
2726 mark_infrun_async_event_handler ();
2730 attach_post_wait (from_tty
, mode
);
2732 disable_commit_resumed
.reset_and_commit ();
2735 /* We had just found out that the target was already attached to an
2736 inferior. PTID points at a thread of this new inferior, that is
2737 the most likely to be stopped right now, but not necessarily so.
2738 The new inferior is assumed to be already added to the inferior
2739 list at this point. If LEAVE_RUNNING, then leave the threads of
2740 this inferior running, except those we've explicitly seen reported
2744 notice_new_inferior (thread_info
*thr
, bool leave_running
, int from_tty
)
2746 enum attach_post_wait_mode mode
2747 = leave_running
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_NOTHING
;
2749 std::optional
<scoped_restore_current_thread
> restore_thread
;
2751 if (inferior_ptid
!= null_ptid
)
2752 restore_thread
.emplace ();
2754 /* Avoid reading registers -- we haven't fetched the target
2756 switch_to_thread_no_regs (thr
);
2758 /* When we "notice" a new inferior we need to do all the things we
2759 would normally do if we had just attached to it. */
2761 if (thr
->executing ())
2763 struct inferior
*inferior
= current_inferior ();
2765 /* We're going to install breakpoints, and poke at memory,
2766 ensure that the inferior is stopped for a moment while we do
2768 target_stop (inferior_ptid
);
2770 inferior
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
2772 /* Wait for stop before proceeding. */
2773 inferior
->add_continuation ([=] ()
2775 attach_post_wait (from_tty
, mode
);
2781 attach_post_wait (from_tty
, mode
);
2786 * takes a program previously attached to and detaches it.
2787 * The program resumes execution and will no longer stop
2788 * on signals, etc. We better not have left any breakpoints
2789 * in the program or it'll die when it hits one. For this
2790 * to work, it may be necessary for the process to have been
2791 * previously attached. It *might* work if the program was
2792 * started via the normal ptrace (PTRACE_TRACEME).
2796 detach_command (const char *args
, int from_tty
)
2798 dont_repeat (); /* Not for the faint of heart. */
2800 if (inferior_ptid
== null_ptid
)
2801 error (_("The program is not being run."));
2803 scoped_disable_commit_resumed
disable_commit_resumed ("detaching");
2805 query_if_trace_running (from_tty
);
2807 disconnect_tracing ();
2809 /* Hold a strong reference to the target while (maybe)
2810 detaching the parent. Otherwise detaching could close the
2812 inferior
*inf
= current_inferior ();
2813 auto target_ref
= target_ops_ref::new_reference (inf
->process_target ());
2815 /* Save this before detaching, since detaching may unpush the
2816 process_stratum target. */
2817 bool was_non_stop_p
= target_is_non_stop_p ();
2819 target_detach (inf
, from_tty
);
2821 update_previous_thread ();
2823 /* The current inferior process was just detached successfully. Get
2824 rid of breakpoints that no longer make sense. Note we don't do
2825 this within target_detach because that is also used when
2826 following child forks, and in that case we will want to transfer
2827 breakpoints to the child, not delete them. */
2828 breakpoint_init_inferior (inf
, inf_exited
);
2830 /* If the solist is global across inferiors, don't clear it when we
2831 detach from a single inferior. */
2832 if (!gdbarch_has_global_solist (inf
->arch ()))
2833 no_shared_libraries (inf
->pspace
);
2835 if (deprecated_detach_hook
)
2836 deprecated_detach_hook ();
2838 if (!was_non_stop_p
)
2839 restart_after_all_stop_detach (as_process_stratum_target (target_ref
.get ()));
2841 disable_commit_resumed
.reset_and_commit ();
2844 /* Disconnect from the current target without resuming it (leaving it
2845 waiting for a debugger).
2847 We'd better not have left any breakpoints in the program or the
2848 next debugger will get confused. Currently only supported for some
2849 remote targets, since the normal attach mechanisms don't work on
2850 stopped processes on some native platforms (e.g. GNU/Linux). */
2853 disconnect_command (const char *args
, int from_tty
)
2855 dont_repeat (); /* Not for the faint of heart. */
2856 query_if_trace_running (from_tty
);
2857 disconnect_tracing ();
2858 target_disconnect (args
, from_tty
);
2859 no_shared_libraries (current_program_space
);
2860 init_thread_list ();
2861 update_previous_thread ();
2862 if (deprecated_detach_hook
)
2863 deprecated_detach_hook ();
2866 /* Stop PTID in the current target, and tag the PTID threads as having
2867 been explicitly requested to stop. PTID can be a thread, a
2868 process, or minus_one_ptid, meaning all threads of all inferiors of
2869 the current target. */
2872 stop_current_target_threads_ns (ptid_t ptid
)
2876 /* Tag the thread as having been explicitly requested to stop, so
2877 other parts of gdb know not to resume this thread automatically,
2878 if it was stopped due to an internal event. Limit this to
2879 non-stop mode, as when debugging a multi-threaded application in
2880 all-stop mode, we will only get one stop event --- it's undefined
2881 which thread will report the event. */
2882 set_stop_requested (current_inferior ()->process_target (),
2886 /* See inferior.h. */
2889 interrupt_target_1 (bool all_threads
)
2891 scoped_disable_commit_resumed
disable_commit_resumed ("interrupting");
2897 scoped_restore_current_thread restore_thread
;
2899 for (inferior
*inf
: all_inferiors ())
2901 switch_to_inferior_no_thread (inf
);
2902 stop_current_target_threads_ns (minus_one_ptid
);
2906 stop_current_target_threads_ns (inferior_ptid
);
2909 target_interrupt ();
2911 disable_commit_resumed
.reset_and_commit ();
2915 Stop the execution of the target while running in async mode, in
2916 the background. In all-stop, stop the whole process. In non-stop
2917 mode, stop the current thread only by default, or stop all threads
2918 if the `-a' switch is used. */
2921 interrupt_command (const char *args
, int from_tty
)
2923 if (target_can_async_p ())
2925 int all_threads
= 0;
2927 dont_repeat (); /* Not for the faint of heart. */
2930 && startswith (args
, "-a"))
2933 interrupt_target_1 (all_threads
);
2937 /* See inferior.h. */
2940 default_print_float_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
2941 const frame_info_ptr
&frame
, const char *args
)
2944 int printed_something
= 0;
2946 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2948 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, float_reggroup
))
2950 printed_something
= 1;
2951 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2954 if (!printed_something
)
2955 gdb_printf (file
, "No floating-point info "
2956 "available for this processor.\n");
2960 info_float_command (const char *args
, int from_tty
)
2962 frame_info_ptr frame
;
2964 if (!target_has_registers ())
2965 error (_("The program has no registers now."));
2967 frame
= get_selected_frame (nullptr);
2968 gdbarch_print_float_info (get_frame_arch (frame
), gdb_stdout
, frame
, args
);
2971 /* Implement `info proc' family of commands. */
2974 info_proc_cmd_1 (const char *args
, enum info_proc_what what
, int from_tty
)
2976 struct gdbarch
*gdbarch
= get_current_arch ();
2978 if (!target_info_proc (args
, what
))
2980 if (gdbarch_info_proc_p (gdbarch
))
2981 gdbarch_info_proc (gdbarch
, args
, what
);
2983 error (_("Not supported on this target."));
2987 /* Implement `info proc' when given without any further parameters. */
2990 info_proc_cmd (const char *args
, int from_tty
)
2992 info_proc_cmd_1 (args
, IP_MINIMAL
, from_tty
);
2995 /* Implement `info proc mappings'. */
2998 info_proc_cmd_mappings (const char *args
, int from_tty
)
3000 info_proc_cmd_1 (args
, IP_MAPPINGS
, from_tty
);
3003 /* Implement `info proc stat'. */
3006 info_proc_cmd_stat (const char *args
, int from_tty
)
3008 info_proc_cmd_1 (args
, IP_STAT
, from_tty
);
3011 /* Implement `info proc status'. */
3014 info_proc_cmd_status (const char *args
, int from_tty
)
3016 info_proc_cmd_1 (args
, IP_STATUS
, from_tty
);
3019 /* Implement `info proc cwd'. */
3022 info_proc_cmd_cwd (const char *args
, int from_tty
)
3024 info_proc_cmd_1 (args
, IP_CWD
, from_tty
);
3027 /* Implement `info proc cmdline'. */
3030 info_proc_cmd_cmdline (const char *args
, int from_tty
)
3032 info_proc_cmd_1 (args
, IP_CMDLINE
, from_tty
);
3035 /* Implement `info proc exe'. */
3038 info_proc_cmd_exe (const char *args
, int from_tty
)
3040 info_proc_cmd_1 (args
, IP_EXE
, from_tty
);
3043 /* Implement `info proc files'. */
3046 info_proc_cmd_files (const char *args
, int from_tty
)
3048 info_proc_cmd_1 (args
, IP_FILES
, from_tty
);
3051 /* Implement `info proc all'. */
3054 info_proc_cmd_all (const char *args
, int from_tty
)
3056 info_proc_cmd_1 (args
, IP_ALL
, from_tty
);
3059 /* Implement `show print finish'. */
3062 show_print_finish (struct ui_file
*file
, int from_tty
,
3063 struct cmd_list_element
*c
,
3066 gdb_printf (file
, _("\
3067 Printing of return value after `finish' is %s.\n"),
3072 /* This help string is used for the run, start, and starti commands.
3073 It is defined as a macro to prevent duplication. */
3075 #define RUN_ARGS_HELP \
3076 "You may specify arguments to give it.\n\
3077 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3078 shell that will start the program (specified by the \"$SHELL\" environment\n\
3079 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3080 are also allowed.\n\
3082 With no arguments, uses arguments last specified (with \"run\" or\n\
3083 \"set args\"). To cancel previous arguments and run with no arguments,\n\
3084 use \"set args\" without arguments.\n\
3086 To start the inferior without using a shell, use \"set startup-with-shell off\"."
3088 void _initialize_infcmd ();
3090 _initialize_infcmd ()
3092 static struct cmd_list_element
*info_proc_cmdlist
;
3093 struct cmd_list_element
*c
= nullptr;
3095 /* Add the filename of the terminal connected to inferior I/O. */
3097 = add_setshow_optional_filename_cmd ("inferior-tty", class_run
, _("\
3098 Set terminal for future runs of program being debugged."), _("\
3099 Show terminal for future runs of program being debugged."), _("\
3100 Usage: set inferior-tty [TTY]\n\n\
3101 If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3105 show_inferior_tty_command
,
3106 &setlist
, &showlist
);
3107 add_alias_cmd ("tty", tty_set_show
.set
, class_run
, 0, &cmdlist
);
3110 = add_setshow_string_noescape_cmd ("args", class_run
, _("\
3111 Set argument list to give program being debugged when it is started."), _("\
3112 Show argument list to give program being debugged when it is started."), _("\
3113 Follow this command with any number of args, to be passed to the program."),
3117 &setlist
, &showlist
);
3118 set_cmd_completer (args_set_show
.set
, deprecated_filename_completer
);
3121 = add_setshow_string_noescape_cmd ("cwd", class_run
, _("\
3122 Set the current working directory to be used when the inferior is started.\n\
3123 Changing this setting does not have any effect on inferiors that are\n\
3126 Show the current working directory that is used when the inferior is started."),
3128 Use this command to change the current working directory that will be used\n\
3129 when the inferior is started. This setting does not affect GDB's current\n\
3130 working directory."),
3131 set_cwd_value
, get_inferior_cwd
,
3133 &setlist
, &showlist
);
3134 set_cmd_completer (cwd_set_show
.set
, deprecated_filename_completer
);
3136 c
= add_cmd ("environment", no_class
, environment_info
, _("\
3137 The environment to give the program, or one variable's value.\n\
3138 With an argument VAR, prints the value of environment variable VAR to\n\
3139 give the program being debugged. With no arguments, prints the entire\n\
3140 environment to be given to the program."), &showlist
);
3141 set_cmd_completer (c
, noop_completer
);
3143 add_basic_prefix_cmd ("unset", no_class
,
3144 _("Complement to certain \"set\" commands."),
3145 &unsetlist
, 0, &cmdlist
);
3147 c
= add_cmd ("environment", class_run
, unset_environment_command
, _("\
3148 Cancel environment variable VAR for the program.\n\
3149 This does not affect the program until the next \"run\" command."),
3151 set_cmd_completer (c
, noop_completer
);
3153 c
= add_cmd ("environment", class_run
, set_environment_command
, _("\
3154 Set environment variable value to give the program.\n\
3155 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3156 VALUES of environment variables are uninterpreted strings.\n\
3157 This does not affect the program until the next \"run\" command."),
3159 set_cmd_completer (c
, noop_completer
);
3161 c
= add_com ("path", class_files
, path_command
, _("\
3162 Add directory DIR(s) to beginning of search path for object files.\n\
3163 $cwd in the path means the current working directory.\n\
3164 This path is equivalent to the $PATH shell variable. It is a list of\n\
3165 directories, separated by colons. These directories are searched to find\n\
3166 fully linked executable files and separately compiled object files as \
3168 set_cmd_completer (c
, deprecated_filename_completer
);
3170 c
= add_cmd ("paths", no_class
, path_info
, _("\
3171 Current search path for finding object files.\n\
3172 $cwd in the path means the current working directory.\n\
3173 This path is equivalent to the $PATH shell variable. It is a list of\n\
3174 directories, separated by colons. These directories are searched to find\n\
3175 fully linked executable files and separately compiled object files as \
3178 set_cmd_completer (c
, noop_completer
);
3180 add_prefix_cmd ("kill", class_run
, kill_command
,
3181 _("Kill execution of program being debugged."),
3182 &killlist
, 0, &cmdlist
);
3184 add_com ("attach", class_run
, attach_command
, _("\
3185 Attach to a process or file outside of GDB.\n\
3186 This command attaches to another target, of the same type as your last\n\
3187 \"target\" command (\"info files\" will show your target stack).\n\
3188 The command may take as argument a process id or a device file.\n\
3189 For a process id, you must have permission to send the process a signal,\n\
3190 and it must have the same effective uid as the debugger.\n\
3191 When using \"attach\" with a process id, the debugger finds the\n\
3192 program running in the process, looking first in the current working\n\
3193 directory, or (if not found there) using the source file search path\n\
3194 (see the \"directory\" command). You can also use the \"file\" command\n\
3195 to specify the program, and to load its symbol table."));
3197 add_prefix_cmd ("detach", class_run
, detach_command
, _("\
3198 Detach a process or file previously attached.\n\
3199 If a process, it is no longer traced, and it continues its execution. If\n\
3200 you were debugging a file, the file is closed and gdb no longer accesses it."),
3201 &detachlist
, 0, &cmdlist
);
3203 add_com ("disconnect", class_run
, disconnect_command
, _("\
3204 Disconnect from a target.\n\
3205 The target will wait for another debugger to connect. Not available for\n\
3208 c
= add_com ("signal", class_run
, signal_command
, _("\
3209 Continue program with the specified signal.\n\
3210 Usage: signal SIGNAL\n\
3211 The SIGNAL argument is processed the same as the handle command.\n\
3213 An argument of \"0\" means continue the program without sending it a signal.\n\
3214 This is useful in cases where the program stopped because of a signal,\n\
3215 and you want to resume the program while discarding the signal.\n\
3217 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3218 the current thread only."));
3219 set_cmd_completer (c
, signal_completer
);
3221 c
= add_com ("queue-signal", class_run
, queue_signal_command
, _("\
3222 Queue a signal to be delivered to the current thread when it is resumed.\n\
3223 Usage: queue-signal SIGNAL\n\
3224 The SIGNAL argument is processed the same as the handle command.\n\
3225 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3227 An argument of \"0\" means remove any currently queued signal from\n\
3228 the current thread. This is useful in cases where the program stopped\n\
3229 because of a signal, and you want to resume it while discarding the signal.\n\
3231 In a multi-threaded program the signal is queued with, or discarded from,\n\
3232 the current thread only."));
3233 set_cmd_completer (c
, signal_completer
);
3235 cmd_list_element
*stepi_cmd
3236 = add_com ("stepi", class_run
, stepi_command
, _("\
3237 Step one instruction exactly.\n\
3239 Argument N means step N times (or till program stops for another \
3241 add_com_alias ("si", stepi_cmd
, class_run
, 0);
3243 cmd_list_element
*nexti_cmd
3244 = add_com ("nexti", class_run
, nexti_command
, _("\
3245 Step one instruction, but proceed through subroutine calls.\n\
3247 Argument N means step N times (or till program stops for another \
3249 add_com_alias ("ni", nexti_cmd
, class_run
, 0);
3251 cmd_list_element
*finish_cmd
3252 = add_com ("finish", class_run
, finish_command
, _("\
3253 Execute until selected stack frame returns.\n\
3255 Upon return, the value returned is printed and put in the value history."));
3256 add_com_alias ("fin", finish_cmd
, class_run
, 1);
3258 cmd_list_element
*next_cmd
3259 = add_com ("next", class_run
, next_command
, _("\
3260 Step program, proceeding through subroutine calls.\n\
3262 Unlike \"step\", if the current source line calls a subroutine,\n\
3263 this command does not enter the subroutine, but instead steps over\n\
3264 the call, in effect treating it as a single source line."));
3265 add_com_alias ("n", next_cmd
, class_run
, 1);
3267 cmd_list_element
*step_cmd
3268 = add_com ("step", class_run
, step_command
, _("\
3269 Step program until it reaches a different source line.\n\
3271 Argument N means step N times (or till program stops for another \
3273 add_com_alias ("s", step_cmd
, class_run
, 1);
3275 cmd_list_element
*until_cmd
3276 = add_com ("until", class_run
, until_command
, _("\
3277 Execute until past the current line or past a LOCATION.\n\
3278 Execute until the program reaches a source line greater than the current\n\
3279 or a specified location (same args as break command) within the current \
3281 set_cmd_completer (until_cmd
, location_completer
);
3282 add_com_alias ("u", until_cmd
, class_run
, 1);
3284 c
= add_com ("advance", class_run
, advance_command
, _("\
3285 Continue the program up to the given location.\n\
3286 Usage: advance LOCSPEC\n\
3287 The argument is a location specification, i.e., the same forms\n\
3288 accepted by the 'break' command.\n\
3289 Execution will also stop upon exit from the current stack frame."));
3290 set_cmd_completer (c
, location_completer
);
3292 cmd_list_element
*jump_cmd
3293 = add_com ("jump", class_run
, jump_command
, _("\
3294 Continue program being debugged at specified line or address.\n\
3295 Usage: jump LOCATION\n\
3296 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3297 for an address to start at."));
3298 set_cmd_completer (jump_cmd
, location_completer
);
3299 add_com_alias ("j", jump_cmd
, class_run
, 1);
3301 cmd_list_element
*continue_cmd
3302 = add_com ("continue", class_run
, continue_command
, _("\
3303 Continue program being debugged, after signal or breakpoint.\n\
3304 Usage: continue [N]\n\
3305 If proceeding from breakpoint, a number N may be used as an argument,\n\
3306 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3307 the breakpoint won't break until the Nth time it is reached).\n\
3309 If non-stop mode is enabled, continue only the current thread,\n\
3310 otherwise all the threads in the program are continued. To\n\
3311 continue all stopped threads in non-stop mode, use the -a option.\n\
3312 Specifying -a and an ignore count simultaneously is an error."));
3313 add_com_alias ("c", continue_cmd
, class_run
, 1);
3314 add_com_alias ("fg", continue_cmd
, class_run
, 1);
3316 cmd_list_element
*run_cmd
3317 = add_com ("run", class_run
, run_command
, _("\
3318 Start debugged program.\n"
3320 set_cmd_completer (run_cmd
, deprecated_filename_completer
);
3321 add_com_alias ("r", run_cmd
, class_run
, 1);
3323 c
= add_com ("start", class_run
, start_command
, _("\
3324 Start the debugged program stopping at the beginning of the main procedure.\n"
3326 set_cmd_completer (c
, deprecated_filename_completer
);
3328 c
= add_com ("starti", class_run
, starti_command
, _("\
3329 Start the debugged program stopping at the first instruction.\n"
3331 set_cmd_completer (c
, deprecated_filename_completer
);
3333 add_com ("interrupt", class_run
, interrupt_command
,
3334 _("Interrupt the execution of the debugged program.\n\
3335 If non-stop mode is enabled, interrupt only the current thread,\n\
3336 otherwise all the threads in the program are stopped. To\n\
3337 interrupt all running threads in non-stop mode, use the -a option."));
3339 cmd_list_element
*info_registers_cmd
3340 = add_info ("registers", info_registers_command
, _("\
3341 List of integer registers and their contents, for selected stack frame.\n\
3342 One or more register names as argument means describe the given registers.\n\
3343 One or more register group names as argument means describe the registers\n\
3344 in the named register groups."));
3345 add_info_alias ("r", info_registers_cmd
, 1);
3346 set_cmd_completer (info_registers_cmd
, reg_or_group_completer
);
3348 c
= add_info ("all-registers", info_all_registers_command
, _("\
3349 List of all registers and their contents, for selected stack frame.\n\
3350 One or more register names as argument means describe the given registers.\n\
3351 One or more register group names as argument means describe the registers\n\
3352 in the named register groups."));
3353 set_cmd_completer (c
, reg_or_group_completer
);
3355 add_info ("program", info_program_command
,
3356 _("Execution status of the program."));
3358 add_info ("float", info_float_command
,
3359 _("Print the status of the floating point unit."));
3361 add_info ("vector", info_vector_command
,
3362 _("Print the status of the vector unit."));
3364 add_prefix_cmd ("proc", class_info
, info_proc_cmd
,
3366 Show additional information about a process.\n\
3367 Specify any process id, or use the program being debugged by default."),
3369 1/*allow-unknown*/, &infolist
);
3371 add_cmd ("mappings", class_info
, info_proc_cmd_mappings
, _("\
3372 List memory regions mapped by the specified process."),
3373 &info_proc_cmdlist
);
3375 add_cmd ("stat", class_info
, info_proc_cmd_stat
, _("\
3376 List process info from /proc/PID/stat."),
3377 &info_proc_cmdlist
);
3379 add_cmd ("status", class_info
, info_proc_cmd_status
, _("\
3380 List process info from /proc/PID/status."),
3381 &info_proc_cmdlist
);
3383 add_cmd ("cwd", class_info
, info_proc_cmd_cwd
, _("\
3384 List current working directory of the specified process."),
3385 &info_proc_cmdlist
);
3387 add_cmd ("cmdline", class_info
, info_proc_cmd_cmdline
, _("\
3388 List command line arguments of the specified process."),
3389 &info_proc_cmdlist
);
3391 add_cmd ("exe", class_info
, info_proc_cmd_exe
, _("\
3392 List absolute filename for executable of the specified process."),
3393 &info_proc_cmdlist
);
3395 add_cmd ("files", class_info
, info_proc_cmd_files
, _("\
3396 List files opened by the specified process."),
3397 &info_proc_cmdlist
);
3399 add_cmd ("all", class_info
, info_proc_cmd_all
, _("\
3400 List all available info about the specified process."),
3401 &info_proc_cmdlist
);
3403 add_setshow_boolean_cmd ("finish", class_support
,
3405 Set whether `finish' prints the return value."), _("\
3406 Show whether `finish' prints the return value."), nullptr,
3409 &setprintlist
, &showprintlist
);