1 /* Memory-access and commands for "inferior" process, for GDB.
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
27 #include "gdbsupport/environ.h"
35 #include "completer.h"
38 #include "reggroups.h"
42 #include "observable.h"
43 #include "target-descriptions.h"
44 #include "user-regs.h"
45 #include "gdbthread.h"
47 #include "inline-frame.h"
48 #include "tracepoint.h"
51 #include "thread-fsm.h"
55 #include "gdbsupport/gdb_optional.h"
57 #include "cli/cli-style.h"
59 /* Local functions: */
61 static void until_next_command (int);
63 static void step_1 (int, int, const char *);
65 #define ERROR_NO_INFERIOR \
66 if (!target_has_execution ()) error (_("The program is not being run."));
68 /* Scratch area where string containing arguments to give to the
69 program will be stored by 'set args'. As soon as anything is
70 stored, notice_args_set will move it into per-inferior storage.
71 Arguments are separated by spaces. Empty string (pointer to '\0')
74 static std::string inferior_args_scratch
;
76 /* Scratch area where the new cwd will be stored by 'set cwd'. */
78 static std::string inferior_cwd_scratch
;
80 /* Scratch area where 'set inferior-tty' will store user-provided value.
81 We'll immediate copy it into per-inferior storage. */
83 static std::string inferior_io_terminal_scratch
;
85 /* Pid of our debugged inferior, or 0 if no inferior now.
86 Since various parts of infrun.c test this to see whether there is a program
87 being debugged it should be nonzero (currently 3 is used) for remote
92 /* Nonzero if stopped due to completion of a stack dummy routine. */
94 enum stop_stack_kind stop_stack_dummy
;
96 /* Nonzero if stopped due to a random (unexpected) signal in inferior
99 int stopped_by_random_signal
;
104 set_inferior_tty_command (const char *args
, int from_tty
,
105 struct cmd_list_element
*c
)
107 /* CLI has assigned the user-provided value to inferior_io_terminal_scratch.
108 Now route it to current inferior. */
109 current_inferior ()->set_tty (inferior_io_terminal_scratch
);
113 show_inferior_tty_command (struct ui_file
*file
, int from_tty
,
114 struct cmd_list_element
*c
, const char *value
)
116 /* Note that we ignore the passed-in value in favor of computing it
118 const std::string
&inferior_tty
= current_inferior ()->tty ();
121 _("Terminal for future runs of program being debugged "
122 "is \"%s\".\n"), inferior_tty
.c_str ());
126 set_inferior_args_vector (int argc
, char **argv
)
128 gdb::array_view
<char * const> args (argv
, argc
);
129 std::string n
= construct_inferior_arguments (args
);
130 current_inferior ()->set_args (std::move (n
));
133 /* Notice when `set args' is run. */
136 set_args_command (const char *args
, int from_tty
, struct cmd_list_element
*c
)
138 /* CLI has assigned the user-provided value to inferior_args_scratch.
139 Now route it to current inferior. */
140 current_inferior ()->set_args (inferior_args_scratch
);
143 /* Notice when `show args' is run. */
146 show_args_command (struct ui_file
*file
, int from_tty
,
147 struct cmd_list_element
*c
, const char *value
)
149 /* Note that we ignore the passed-in value in favor of computing it
151 deprecated_show_value_hack (file
, from_tty
, c
,
152 current_inferior ()->args ().c_str ());
155 /* See gdbsupport/common-inferior.h. */
160 return current_inferior ()->cwd ();
163 /* Handle the 'set cwd' command. */
166 set_cwd_command (const char *args
, int from_tty
, struct cmd_list_element
*c
)
168 current_inferior ()->set_cwd (inferior_cwd_scratch
);
171 /* Handle the 'show cwd' command. */
174 show_cwd_command (struct ui_file
*file
, int from_tty
,
175 struct cmd_list_element
*c
, const char *value
)
177 const std::string
&cwd
= current_inferior ()->cwd ();
182 You have not set the inferior's current working directory.\n\
183 The inferior will inherit GDB's cwd if native debugging, or the remote\n\
184 server's cwd if remote debugging.\n"));
187 _("Current working directory that will be used "
188 "when starting the inferior is \"%s\".\n"),
193 /* This function strips the '&' character (indicating background
194 execution) that is added as *the last* of the arguments ARGS of a
195 command. A copy of the incoming ARGS without the '&' is returned,
196 unless the resulting string after stripping is empty, in which case
197 NULL is returned. *BG_CHAR_P is an output boolean that indicates
198 whether the '&' character was found. */
200 static gdb::unique_xmalloc_ptr
<char>
201 strip_bg_char (const char *args
, int *bg_char_p
)
205 if (args
== NULL
|| *args
== '\0')
211 p
= args
+ strlen (args
);
215 while (p
> args
&& isspace (p
[-1]))
220 return gdb::unique_xmalloc_ptr
<char>
221 (savestring (args
, p
- args
));
223 return gdb::unique_xmalloc_ptr
<char> (nullptr);
227 return make_unique_xstrdup (args
);
230 /* Common actions to take after creating any sort of inferior, by any
231 means (running, attaching, connecting, et cetera). The target
232 should be stopped. */
235 post_create_inferior (int from_tty
)
238 /* Be sure we own the terminal in case write operations are performed. */
239 target_terminal::ours_for_output ();
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 (target_gdbarch ()))
291 solib_add (NULL
, 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 const char *exec_file
;
373 struct ui_out
*uiout
= current_uiout
;
374 struct target_ops
*run_target
;
379 scoped_disable_commit_resumed
disable_commit_resumed ("running");
381 kill_if_already_running (from_tty
);
383 init_wait_for_inferior ();
384 clear_breakpoint_hit_counts ();
386 /* Clean up any leftovers from other runs. Some other things from
387 this function should probably be moved into target_pre_inferior. */
388 target_pre_inferior (from_tty
);
390 /* The comment here used to read, "The exec file is re-read every
391 time we do a generic_mourn_inferior, so we just have to worry
392 about the symbol file." The `generic_mourn_inferior' function
393 gets called whenever the program exits. However, suppose the
394 program exits, and *then* the executable file changes? We need
395 to check again here. Since reopen_exec_file doesn't do anything
396 if the timestamp hasn't changed, I don't see the harm. */
398 reread_symbols (from_tty
);
400 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
401 args
= stripped
.get ();
403 /* Do validation and preparation before possibly changing anything
406 run_target
= find_run_target ();
408 prepare_execution_command (run_target
, async_exec
);
410 if (non_stop
&& !run_target
->supports_non_stop ())
411 error (_("The target does not support running in non-stop mode."));
413 /* Done. Can now set breakpoints, change inferior args, etc. */
415 /* Insert temporary breakpoint in main function if requested. */
416 if (run_how
== RUN_STOP_AT_MAIN
)
418 std::string arg
= string_printf ("-qualified %s", main_name ());
419 tbreak_command (arg
.c_str (), 0);
422 exec_file
= get_exec_file (0);
424 /* We keep symbols from add-symbol-file, on the grounds that the
425 user might want to add some symbols before running the program
426 (right?). But sometimes (dynamic loading where the user manually
427 introduces the new symbols with add-symbol-file), the code which
428 the symbols describe does not persist between runs. Currently
429 the user has to manually nuke all symbols between runs if they
430 want them to go away (PR 2207). This is probably reasonable. */
432 /* If there were other args, beside '&', process them. */
434 current_inferior ()->set_args (args
);
438 uiout
->field_string (NULL
, "Starting program");
441 uiout
->field_string ("execfile", exec_file
,
442 file_name_style
.style ());
444 uiout
->field_string ("infargs", current_inferior ()->args ());
449 run_target
->create_inferior (exec_file
,
450 current_inferior ()->args (),
451 current_inferior ()->environment
.envp (),
453 /* to_create_inferior should push the target, so after this point we
454 shouldn't refer to run_target again. */
457 /* We're starting off a new process. When we get out of here, in
458 non-stop mode, finish the state of all threads of that process,
459 but leave other threads alone, as they may be stopped in internal
460 events --- the frontend shouldn't see them as stopped. In
461 all-stop, always finish the state of all threads, as we may be
462 resuming more than just the new process. */
463 process_stratum_target
*finish_target
;
467 finish_target
= current_inferior ()->process_target ();
468 finish_ptid
= ptid_t (current_inferior ()->pid
);
472 finish_target
= nullptr;
473 finish_ptid
= minus_one_ptid
;
475 scoped_finish_thread_state
finish_state (finish_target
, finish_ptid
);
477 /* Pass zero for FROM_TTY, because at this point the "run" command
478 has done its thing; now we are setting up the running program. */
479 post_create_inferior (0);
481 /* Queue a pending event so that the program stops immediately. */
482 if (run_how
== RUN_STOP_AT_FIRST_INSN
)
484 thread_info
*thr
= inferior_thread ();
485 target_waitstatus ws
;
486 ws
.set_stopped (GDB_SIGNAL_0
);
487 thr
->set_pending_waitstatus (ws
);
490 /* Start the target running. Do not use -1 continuation as it would skip
491 breakpoint right at the entry point. */
492 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0
);
494 /* Since there was no error, there's no need to finish the thread
496 finish_state
.release ();
498 disable_commit_resumed
.reset_and_commit ();
502 run_command (const char *args
, int from_tty
)
504 run_command_1 (args
, from_tty
, RUN_NORMAL
);
507 /* Start the execution of the program up until the beginning of the main
511 start_command (const char *args
, int from_tty
)
513 /* Some languages such as Ada need to search inside the program
514 minimal symbols for the location where to put the temporary
515 breakpoint before starting. */
516 if (!have_minimal_symbols ())
517 error (_("No symbol table loaded. Use the \"file\" command."));
519 /* Run the program until reaching the main procedure... */
520 run_command_1 (args
, from_tty
, RUN_STOP_AT_MAIN
);
523 /* Start the execution of the program stopping at the first
527 starti_command (const char *args
, int from_tty
)
529 run_command_1 (args
, from_tty
, RUN_STOP_AT_FIRST_INSN
);
533 proceed_thread_callback (struct thread_info
*thread
, void *arg
)
535 /* We go through all threads individually instead of compressing
536 into a single target `resume_all' request, because some threads
537 may be stopped in internal breakpoints/events, or stopped waiting
538 for its turn in the displaced stepping queue (that is, they are
539 running && !executing). The target side has no idea about why
540 the thread is stopped, so a `resume_all' command would resume too
541 much. If/when GDB gains a way to tell the target `hold this
542 thread stopped until I say otherwise', then we can optimize
544 if (thread
->state
!= THREAD_STOPPED
)
547 if (!thread
->inf
->has_execution ())
550 switch_to_thread (thread
);
551 clear_proceed_status (0);
552 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
557 ensure_valid_thread (void)
559 if (inferior_ptid
== null_ptid
560 || inferior_thread ()->state
== THREAD_EXITED
)
561 error (_("Cannot execute this command without a live selected thread."));
564 /* If the user is looking at trace frames, any resumption of execution
565 is likely to mix up recorded and live target data. So simply
566 disallow those commands. */
569 ensure_not_tfind_mode (void)
571 if (get_traceframe_number () >= 0)
572 error (_("Cannot execute this command while looking at trace frames."));
575 /* Throw an error indicating the current thread is running. */
578 error_is_running (void)
580 error (_("Cannot execute this command while "
581 "the selected thread is running."));
584 /* Calls error_is_running if the current thread is running. */
587 ensure_not_running (void)
589 if (inferior_thread ()->state
== THREAD_RUNNING
)
594 continue_1 (int all_threads
)
597 ensure_not_tfind_mode ();
599 if (non_stop
&& all_threads
)
601 /* Don't error out if the current thread is running, because
602 there may be other stopped threads. */
604 /* Backup current thread and selected frame and restore on scope
606 scoped_restore_current_thread restore_thread
;
607 scoped_disable_commit_resumed disable_commit_resumed
608 ("continue all threads in non-stop");
610 iterate_over_threads (proceed_thread_callback
, NULL
);
612 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
614 /* If all threads in the target were already running,
615 proceed_thread_callback ends up never calling proceed,
616 and so nothing calls this to put the inferior's terminal
617 settings in effect and remove stdin from the event loop,
618 which we must when running a foreground command. E.g.:
622 <all threads are running now>
625 <no thread was resumed, but the inferior now owns the terminal>
627 target_terminal::inferior ();
630 disable_commit_resumed
.reset_and_commit ();
634 ensure_valid_thread ();
635 ensure_not_running ();
636 clear_proceed_status (0);
637 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
641 /* continue [-a] [proceed-count] [&] */
644 continue_command (const char *args
, int from_tty
)
647 bool all_threads_p
= false;
651 /* Find out whether we must run in the background. */
652 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
653 args
= stripped
.get ();
657 if (startswith (args
, "-a"))
659 all_threads_p
= true;
660 args
+= sizeof ("-a") - 1;
666 if (!non_stop
&& all_threads_p
)
667 error (_("`-a' is meaningless in all-stop mode."));
669 if (args
!= NULL
&& all_threads_p
)
670 error (_("Can't resume all threads and specify "
671 "proceed count simultaneously."));
673 /* If we have an argument left, set proceed count of breakpoint we
677 bpstat
*bs
= nullptr;
680 struct thread_info
*tp
;
683 tp
= inferior_thread ();
686 process_stratum_target
*last_target
;
689 get_last_target_status (&last_target
, &last_ptid
, nullptr);
690 tp
= find_thread_ptid (last_target
, last_ptid
);
693 bs
= tp
->control
.stop_bpstat
;
695 while ((stat
= bpstat_num (&bs
, &num
)) != 0)
698 set_ignore_count (num
,
699 parse_and_eval_long (args
) - 1,
701 /* set_ignore_count prints a message ending with a period.
702 So print two spaces before "Continuing.". */
708 if (!stopped
&& from_tty
)
711 ("Not stopped at any breakpoint; argument ignored.\n");
716 ensure_not_tfind_mode ();
718 if (!non_stop
|| !all_threads_p
)
720 ensure_valid_thread ();
721 ensure_not_running ();
724 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
727 gdb_printf (_("Continuing.\n"));
729 continue_1 (all_threads_p
);
732 /* Record in TP the starting point of a "step" or "next" command. */
735 set_step_frame (thread_info
*tp
)
737 /* This can be removed once this function no longer implicitly relies on the
738 inferior_ptid value. */
739 gdb_assert (inferior_ptid
== tp
->ptid
);
741 frame_info
*frame
= get_current_frame ();
743 symtab_and_line sal
= find_frame_sal (frame
);
744 set_step_info (tp
, frame
, sal
);
746 CORE_ADDR pc
= get_frame_pc (frame
);
747 tp
->control
.step_start_function
= find_pc_function (pc
);
750 /* Step until outside of current statement. */
753 step_command (const char *count_string
, int from_tty
)
755 step_1 (0, 0, count_string
);
758 /* Likewise, but skip over subroutine calls as if single instructions. */
761 next_command (const char *count_string
, int from_tty
)
763 step_1 (1, 0, count_string
);
766 /* Likewise, but step only one instruction. */
769 stepi_command (const char *count_string
, int from_tty
)
771 step_1 (0, 1, count_string
);
775 nexti_command (const char *count_string
, int from_tty
)
777 step_1 (1, 1, count_string
);
780 /* Data for the FSM that manages the step/next/stepi/nexti
783 struct step_command_fsm
: public thread_fsm
785 /* How many steps left in a "step N"-like command. */
788 /* If true, this is a next/nexti, otherwise a step/stepi. */
789 int skip_subroutines
;
791 /* If true, this is a stepi/nexti, otherwise a step/step. */
794 explicit step_command_fsm (struct interp
*cmd_interp
)
795 : thread_fsm (cmd_interp
)
799 void clean_up (struct thread_info
*thread
) override
;
800 bool should_stop (struct thread_info
*thread
) override
;
801 enum async_reply_reason
do_async_reply_reason () override
;
804 /* Prepare for a step/next/etc. command. Any target resource
805 allocated here is undone in the FSM's clean_up method. */
808 step_command_fsm_prepare (struct step_command_fsm
*sm
,
809 int skip_subroutines
, int single_inst
,
810 int count
, struct thread_info
*thread
)
812 sm
->skip_subroutines
= skip_subroutines
;
813 sm
->single_inst
= single_inst
;
816 /* Leave the si command alone. */
817 if (!sm
->single_inst
|| sm
->skip_subroutines
)
818 set_longjmp_breakpoint (thread
, get_frame_id (get_current_frame ()));
820 thread
->control
.stepping_command
= 1;
823 static int prepare_one_step (thread_info
*, struct step_command_fsm
*sm
);
826 step_1 (int skip_subroutines
, int single_inst
, const char *count_string
)
830 struct thread_info
*thr
;
831 struct step_command_fsm
*step_sm
;
834 ensure_not_tfind_mode ();
835 ensure_valid_thread ();
836 ensure_not_running ();
838 gdb::unique_xmalloc_ptr
<char> stripped
839 = strip_bg_char (count_string
, &async_exec
);
840 count_string
= stripped
.get ();
842 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
844 count
= count_string
? parse_and_eval_long (count_string
) : 1;
846 clear_proceed_status (1);
848 /* Setup the execution command state machine to handle all the COUNT
850 thr
= inferior_thread ();
851 step_sm
= new step_command_fsm (command_interp ());
852 thr
->set_thread_fsm (std::unique_ptr
<thread_fsm
> (step_sm
));
854 step_command_fsm_prepare (step_sm
, skip_subroutines
,
855 single_inst
, count
, thr
);
857 /* Do only one step for now, before returning control to the event
858 loop. Let the continuation figure out how many other steps we
859 need to do, and handle them one at the time, through
861 if (!prepare_one_step (thr
, step_sm
))
862 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
867 /* Stepped into an inline frame. Pretend that we've
869 thr
->thread_fsm ()->clean_up (thr
);
870 proceeded
= normal_stop ();
872 inferior_event_handler (INF_EXEC_COMPLETE
);
873 all_uis_check_sync_execution_done ();
877 /* Implementation of the 'should_stop' FSM method for stepping
878 commands. Called after we are done with one step operation, to
879 check whether we need to step again, before we print the prompt and
880 return control to the user. If count is > 1, returns false, as we
881 will need to keep going. */
884 step_command_fsm::should_stop (struct thread_info
*tp
)
886 if (tp
->control
.stop_step
)
888 /* There are more steps to make, and we did stop due to
889 ending a stepping range. Do another step. */
891 return prepare_one_step (tp
, this);
899 /* Implementation of the 'clean_up' FSM method for stepping commands. */
902 step_command_fsm::clean_up (struct thread_info
*thread
)
904 if (!single_inst
|| skip_subroutines
)
905 delete_longjmp_breakpoint (thread
->global_num
);
908 /* Implementation of the 'async_reply_reason' FSM method for stepping
911 enum async_reply_reason
912 step_command_fsm::do_async_reply_reason ()
914 return EXEC_ASYNC_END_STEPPING_RANGE
;
917 /* Prepare for one step in "step N". The actual target resumption is
918 done by the caller. Return true if we're done and should thus
919 report a stop to the user. Returns false if the target needs to be
923 prepare_one_step (thread_info
*tp
, struct step_command_fsm
*sm
)
925 /* This can be removed once this function no longer implicitly relies on the
926 inferior_ptid value. */
927 gdb_assert (inferior_ptid
== tp
->ptid
);
931 struct frame_info
*frame
= get_current_frame ();
935 if (!sm
->single_inst
)
939 /* Step at an inlined function behaves like "down". */
940 if (!sm
->skip_subroutines
941 && inline_skipped_frames (tp
))
944 const char *fn
= NULL
;
948 /* Pretend that we've ran. */
949 resume_ptid
= user_visible_resume_ptid (1);
950 set_running (tp
->inf
->process_target (), resume_ptid
, true);
952 step_into_inline_frame (tp
);
954 frame
= get_current_frame ();
955 sal
= find_frame_sal (frame
);
956 sym
= get_frame_function (frame
);
959 fn
= sym
->print_name ();
962 || !function_name_is_marked_for_skip (fn
, sal
))
965 return prepare_one_step (tp
, sm
);
969 pc
= get_frame_pc (frame
);
970 find_pc_line_pc_range (pc
,
971 &tp
->control
.step_range_start
,
972 &tp
->control
.step_range_end
);
974 /* There's a problem in gcc (PR gcc/98780) that causes missing line
975 table entries, which results in a too large stepping range.
976 Use inlined_subroutine info to make the range more narrow. */
977 if (inline_skipped_frames (tp
) > 0)
979 symbol
*sym
= inline_skipped_symbol (tp
);
980 if (sym
->aclass () == LOC_BLOCK
)
982 const block
*block
= SYMBOL_BLOCK_VALUE (sym
);
983 if (BLOCK_END (block
) < tp
->control
.step_range_end
)
984 tp
->control
.step_range_end
= BLOCK_END (block
);
988 tp
->control
.may_range_step
= 1;
990 /* If we have no line info, switch to stepi mode. */
991 if (tp
->control
.step_range_end
== 0 && step_stop_if_no_debug
)
993 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
994 tp
->control
.may_range_step
= 0;
996 else if (tp
->control
.step_range_end
== 0)
1000 if (find_pc_partial_function (pc
, &name
,
1001 &tp
->control
.step_range_start
,
1002 &tp
->control
.step_range_end
) == 0)
1003 error (_("Cannot find bounds of current function"));
1005 target_terminal::ours_for_output ();
1006 gdb_printf (_("Single stepping until exit from function %s,"
1007 "\nwhich has no line number information.\n"),
1013 /* Say we are stepping, but stop after one insn whatever it does. */
1014 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1015 if (!sm
->skip_subroutines
)
1017 Don't step over function calls, not even to functions lacking
1019 tp
->control
.step_over_calls
= STEP_OVER_NONE
;
1022 if (sm
->skip_subroutines
)
1023 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1029 sm
->set_finished ();
1034 /* Continue program at specified address. */
1037 jump_command (const char *arg
, int from_tty
)
1039 struct gdbarch
*gdbarch
= get_current_arch ();
1046 ensure_not_tfind_mode ();
1047 ensure_valid_thread ();
1048 ensure_not_running ();
1050 /* Find out whether we must run in the background. */
1051 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1052 arg
= stripped
.get ();
1054 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1057 error_no_arg (_("starting address"));
1059 std::vector
<symtab_and_line
> sals
1060 = decode_line_with_last_displayed (arg
, DECODE_LINE_FUNFIRSTLINE
);
1061 if (sals
.size () != 1)
1062 error (_("Unreasonable jump request"));
1064 symtab_and_line
&sal
= sals
[0];
1066 if (sal
.symtab
== 0 && sal
.pc
== 0)
1067 error (_("No source file has been specified."));
1069 resolve_sal_pc (&sal
); /* May error out. */
1071 /* See if we are trying to jump to another function. */
1072 fn
= get_frame_function (get_current_frame ());
1073 sfn
= find_pc_function (sal
.pc
);
1074 if (fn
!= NULL
&& sfn
!= fn
)
1076 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal
.line
,
1079 error (_("Not confirmed."));
1086 struct obj_section
*section
;
1088 fixup_symbol_section (sfn
, 0);
1089 section
= sfn
->obj_section (symbol_objfile (sfn
));
1090 if (section_is_overlay (section
)
1091 && !section_is_mapped (section
))
1093 if (!query (_("WARNING!!! Destination is in "
1094 "unmapped overlay! Jump anyway? ")))
1096 error (_("Not confirmed."));
1106 gdb_printf (_("Continuing at "));
1107 gdb_puts (paddress (gdbarch
, addr
));
1111 clear_proceed_status (0);
1112 proceed (addr
, GDB_SIGNAL_0
);
1115 /* Continue program giving it specified signal. */
1118 signal_command (const char *signum_exp
, int from_tty
)
1120 enum gdb_signal oursig
;
1123 dont_repeat (); /* Too dangerous. */
1125 ensure_not_tfind_mode ();
1126 ensure_valid_thread ();
1127 ensure_not_running ();
1129 /* Find out whether we must run in the background. */
1130 gdb::unique_xmalloc_ptr
<char> stripped
1131 = strip_bg_char (signum_exp
, &async_exec
);
1132 signum_exp
= stripped
.get ();
1134 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1137 error_no_arg (_("signal number"));
1139 /* It would be even slicker to make signal names be valid expressions,
1140 (the type could be "enum $signal" or some such), then the user could
1141 assign them to convenience variables. */
1142 oursig
= gdb_signal_from_name (signum_exp
);
1144 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1146 /* No, try numeric. */
1147 int num
= parse_and_eval_long (signum_exp
);
1150 oursig
= GDB_SIGNAL_0
;
1152 oursig
= gdb_signal_from_command (num
);
1155 /* Look for threads other than the current that this command ends up
1156 resuming too (due to schedlock off), and warn if they'll get a
1157 signal delivered. "signal 0" is used to suppress a previous
1158 signal, but if the current thread is no longer the one that got
1159 the signal, then the user is potentially suppressing the signal
1160 of the wrong thread. */
1163 int must_confirm
= 0;
1165 /* This indicates what will be resumed. Either a single thread,
1166 a whole process, or all threads of all processes. */
1167 ptid_t resume_ptid
= user_visible_resume_ptid (0);
1168 process_stratum_target
*resume_target
1169 = user_visible_resume_target (resume_ptid
);
1171 thread_info
*current
= inferior_thread ();
1173 for (thread_info
*tp
: all_non_exited_threads (resume_target
, resume_ptid
))
1178 if (tp
->stop_signal () != GDB_SIGNAL_0
1179 && signal_pass_state (tp
->stop_signal ()))
1182 gdb_printf (_("Note:\n"));
1183 gdb_printf (_(" Thread %s previously stopped with signal %s, %s.\n"),
1184 print_thread_id (tp
),
1185 gdb_signal_to_name (tp
->stop_signal ()),
1186 gdb_signal_to_string (tp
->stop_signal ()));
1192 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1193 "still deliver the signals noted above to their respective threads.\n"
1194 "Continue anyway? "),
1195 print_thread_id (inferior_thread ())))
1196 error (_("Not confirmed."));
1201 if (oursig
== GDB_SIGNAL_0
)
1202 gdb_printf (_("Continuing with no signal.\n"));
1204 gdb_printf (_("Continuing with signal %s.\n"),
1205 gdb_signal_to_name (oursig
));
1208 clear_proceed_status (0);
1209 proceed ((CORE_ADDR
) -1, oursig
);
1212 /* Queue a signal to be delivered to the current thread. */
1215 queue_signal_command (const char *signum_exp
, int from_tty
)
1217 enum gdb_signal oursig
;
1218 struct thread_info
*tp
;
1221 ensure_not_tfind_mode ();
1222 ensure_valid_thread ();
1223 ensure_not_running ();
1225 if (signum_exp
== NULL
)
1226 error_no_arg (_("signal number"));
1228 /* It would be even slicker to make signal names be valid expressions,
1229 (the type could be "enum $signal" or some such), then the user could
1230 assign them to convenience variables. */
1231 oursig
= gdb_signal_from_name (signum_exp
);
1233 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1235 /* No, try numeric. */
1236 int num
= parse_and_eval_long (signum_exp
);
1239 oursig
= GDB_SIGNAL_0
;
1241 oursig
= gdb_signal_from_command (num
);
1244 if (oursig
!= GDB_SIGNAL_0
1245 && !signal_pass_state (oursig
))
1246 error (_("Signal handling set to not pass this signal to the program."));
1248 tp
= inferior_thread ();
1249 tp
->set_stop_signal (oursig
);
1252 /* Data for the FSM that manages the until (with no argument)
1255 struct until_next_fsm
: public thread_fsm
1257 /* The thread that as current when the command was executed. */
1260 until_next_fsm (struct interp
*cmd_interp
, int thread
)
1261 : thread_fsm (cmd_interp
),
1266 bool should_stop (struct thread_info
*thread
) override
;
1267 void clean_up (struct thread_info
*thread
) override
;
1268 enum async_reply_reason
do_async_reply_reason () override
;
1271 /* Implementation of the 'should_stop' FSM method for the until (with
1275 until_next_fsm::should_stop (struct thread_info
*tp
)
1277 if (tp
->control
.stop_step
)
1283 /* Implementation of the 'clean_up' FSM method for the until (with no
1287 until_next_fsm::clean_up (struct thread_info
*thread
)
1289 delete_longjmp_breakpoint (thread
->global_num
);
1292 /* Implementation of the 'async_reply_reason' FSM method for the until
1293 (with no arg) command. */
1295 enum async_reply_reason
1296 until_next_fsm::do_async_reply_reason ()
1298 return EXEC_ASYNC_END_STEPPING_RANGE
;
1301 /* Proceed until we reach a different source line with pc greater than
1302 our current one or exit the function. We skip calls in both cases.
1304 Note that eventually this command should probably be changed so
1305 that only source lines are printed out when we hit the breakpoint
1306 we set. This may involve changes to wait_for_inferior and the
1307 proceed status code. */
1310 until_next_command (int from_tty
)
1312 struct frame_info
*frame
;
1314 struct symbol
*func
;
1315 struct symtab_and_line sal
;
1316 struct thread_info
*tp
= inferior_thread ();
1317 int thread
= tp
->global_num
;
1318 struct until_next_fsm
*sm
;
1320 clear_proceed_status (0);
1321 set_step_frame (tp
);
1323 frame
= get_current_frame ();
1325 /* Step until either exited from this function or greater
1326 than the current line (if in symbolic section) or pc (if
1329 pc
= get_frame_pc (frame
);
1330 func
= find_pc_function (pc
);
1334 struct bound_minimal_symbol msymbol
= lookup_minimal_symbol_by_pc (pc
);
1336 if (msymbol
.minsym
== NULL
)
1337 error (_("Execution is not within a known function."));
1339 tp
->control
.step_range_start
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1340 /* The upper-bound of step_range is exclusive. In order to make PC
1341 within the range, set the step_range_end with PC + 1. */
1342 tp
->control
.step_range_end
= pc
+ 1;
1346 sal
= find_pc_line (pc
, 0);
1348 tp
->control
.step_range_start
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func
));
1349 tp
->control
.step_range_end
= sal
.end
;
1351 /* By setting the step_range_end based on the current pc, we are
1352 assuming that the last line table entry for any given source line
1353 will have is_stmt set to true. This is not necessarily the case,
1354 there may be additional entries for the same source line with
1355 is_stmt set false. Consider the following code:
1357 for (int i = 0; i < 10; i++)
1360 Clang-13, will generate multiple line table entries at the end of
1361 the loop all associated with the 'for' line. The first of these
1362 entries is marked is_stmt true, but the other entries are is_stmt
1365 If we only use the values in SAL, then our stepping range may not
1366 extend to the end of the loop. The until command will reach the
1367 end of the range, find a non is_stmt instruction, and step to the
1368 next is_stmt instruction. This stopping point, however, will be
1369 inside the loop, which is not what we wanted.
1371 Instead, we now check any subsequent line table entries to see if
1372 they are for the same line. If they are, and they are marked
1373 is_stmt false, then we extend the end of our stepping range.
1375 When we finish this process the end of the stepping range will
1376 point either to a line with a different line number, or, will
1377 point at an address for the same line number that is marked as a
1380 struct symtab_and_line final_sal
1381 = find_pc_line (tp
->control
.step_range_end
, 0);
1383 while (final_sal
.line
== sal
.line
&& final_sal
.symtab
== sal
.symtab
1384 && !final_sal
.is_stmt
)
1386 tp
->control
.step_range_end
= final_sal
.end
;
1387 final_sal
= find_pc_line (final_sal
.end
, 0);
1390 tp
->control
.may_range_step
= 1;
1392 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1394 set_longjmp_breakpoint (tp
, get_frame_id (frame
));
1395 delete_longjmp_breakpoint_cleanup
lj_deleter (thread
);
1397 sm
= new until_next_fsm (command_interp (), tp
->global_num
);
1398 tp
->set_thread_fsm (std::unique_ptr
<thread_fsm
> (sm
));
1399 lj_deleter
.release ();
1401 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1405 until_command (const char *arg
, int from_tty
)
1410 ensure_not_tfind_mode ();
1411 ensure_valid_thread ();
1412 ensure_not_running ();
1414 /* Find out whether we must run in the background. */
1415 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1416 arg
= stripped
.get ();
1418 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1421 until_break_command (arg
, from_tty
, 0);
1423 until_next_command (from_tty
);
1427 advance_command (const char *arg
, int from_tty
)
1432 ensure_not_tfind_mode ();
1433 ensure_valid_thread ();
1434 ensure_not_running ();
1437 error_no_arg (_("a location"));
1439 /* Find out whether we must run in the background. */
1440 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1441 arg
= stripped
.get ();
1443 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1445 until_break_command (arg
, from_tty
, 1);
1448 /* See inferior.h. */
1451 get_return_value (struct symbol
*func_symbol
, struct value
*function
)
1453 regcache
*stop_regs
= get_current_regcache ();
1454 struct gdbarch
*gdbarch
= stop_regs
->arch ();
1455 struct value
*value
;
1457 struct type
*value_type
1458 = check_typedef (TYPE_TARGET_TYPE (func_symbol
->type ()));
1459 gdb_assert (value_type
->code () != TYPE_CODE_VOID
);
1461 if (is_nocall_function (check_typedef (::value_type (function
))))
1463 warning (_("Function '%s' does not follow the target calling "
1464 "convention, cannot determine its returned value."),
1465 func_symbol
->print_name ());
1470 /* FIXME: 2003-09-27: When returning from a nested inferior function
1471 call, it's possible (with no help from the architecture vector)
1472 to locate and return/print a "struct return" value. This is just
1473 a more complicated case of what is already being done in the
1474 inferior function call code. In fact, when inferior function
1475 calls are made async, this will likely be made the norm. */
1477 switch (gdbarch_return_value (gdbarch
, function
, value_type
,
1480 case RETURN_VALUE_REGISTER_CONVENTION
:
1481 case RETURN_VALUE_ABI_RETURNS_ADDRESS
:
1482 case RETURN_VALUE_ABI_PRESERVES_ADDRESS
:
1483 value
= allocate_value (value_type
);
1484 gdbarch_return_value (gdbarch
, function
, value_type
, stop_regs
,
1485 value_contents_raw (value
).data (), NULL
);
1487 case RETURN_VALUE_STRUCT_CONVENTION
:
1491 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1497 /* The captured function return value/type and its position in the
1500 struct return_value_info
1502 /* The captured return value. May be NULL if we weren't able to
1503 retrieve it. See get_return_value. */
1504 struct value
*value
;
1506 /* The return type. In some cases, we'll not be able extract the
1507 return value, but we always know the type. */
1510 /* If we captured a value, this is the value history index. */
1511 int value_history_index
;
1514 /* Helper for print_return_value. */
1517 print_return_value_1 (struct ui_out
*uiout
, struct return_value_info
*rv
)
1519 if (rv
->value
!= NULL
)
1521 struct value_print_options opts
;
1524 uiout
->text ("Value returned is ");
1525 uiout
->field_fmt ("gdb-result-var", "$%d",
1526 rv
->value_history_index
);
1527 uiout
->text (" = ");
1528 get_user_print_options (&opts
);
1530 if (opts
.finish_print
)
1533 value_print (rv
->value
, &stb
, &opts
);
1534 uiout
->field_stream ("return-value", stb
);
1537 uiout
->field_string ("return-value", _("<not displayed>"),
1538 metadata_style
.style ());
1543 std::string type_name
= type_to_string (rv
->type
);
1544 uiout
->text ("Value returned has type: ");
1545 uiout
->field_string ("return-type", type_name
);
1547 uiout
->text (" Cannot determine contents\n");
1551 /* Print the result of a function at the end of a 'finish' command.
1552 RV points at an object representing the captured return value/type
1553 and its position in the value history. */
1556 print_return_value (struct ui_out
*uiout
, struct return_value_info
*rv
)
1558 if (rv
->type
== NULL
1559 || check_typedef (rv
->type
)->code () == TYPE_CODE_VOID
)
1564 /* print_return_value_1 can throw an exception in some
1565 circumstances. We need to catch this so that we still
1566 delete the breakpoint. */
1567 print_return_value_1 (uiout
, rv
);
1569 catch (const gdb_exception
&ex
)
1571 exception_print (gdb_stdout
, ex
);
1575 /* Data for the FSM that manages the finish command. */
1577 struct finish_command_fsm
: public thread_fsm
1579 /* The momentary breakpoint set at the function's return address in
1581 breakpoint_up breakpoint
;
1583 /* The function that we're stepping out of. */
1584 struct symbol
*function
= nullptr;
1586 /* If the FSM finishes successfully, this stores the function's
1588 struct return_value_info return_value_info
{};
1590 explicit finish_command_fsm (struct interp
*cmd_interp
)
1591 : thread_fsm (cmd_interp
)
1595 bool should_stop (struct thread_info
*thread
) override
;
1596 void clean_up (struct thread_info
*thread
) override
;
1597 struct return_value_info
*return_value () override
;
1598 enum async_reply_reason
do_async_reply_reason () override
;
1601 /* Implementation of the 'should_stop' FSM method for the finish
1602 commands. Detects whether the thread stepped out of the function
1603 successfully, and if so, captures the function's return value and
1604 marks the FSM finished. */
1607 finish_command_fsm::should_stop (struct thread_info
*tp
)
1609 struct return_value_info
*rv
= &return_value_info
;
1611 if (function
!= NULL
1612 && bpstat_find_breakpoint (tp
->control
.stop_bpstat
,
1613 breakpoint
.get ()) != NULL
)
1618 rv
->type
= TYPE_TARGET_TYPE (function
->type ());
1619 if (rv
->type
== NULL
)
1620 internal_error (__FILE__
, __LINE__
,
1621 _("finish_command: function has no target type"));
1623 if (check_typedef (rv
->type
)->code () != TYPE_CODE_VOID
)
1627 func
= read_var_value (function
, NULL
, get_current_frame ());
1628 rv
->value
= get_return_value (function
, func
);
1629 if (rv
->value
!= NULL
)
1630 rv
->value_history_index
= record_latest_value (rv
->value
);
1633 else if (tp
->control
.stop_step
)
1635 /* Finishing from an inline frame, or reverse finishing. In
1636 either case, there's no way to retrieve the return value. */
1643 /* Implementation of the 'clean_up' FSM method for the finish
1647 finish_command_fsm::clean_up (struct thread_info
*thread
)
1649 breakpoint
.reset ();
1650 delete_longjmp_breakpoint (thread
->global_num
);
1653 /* Implementation of the 'return_value' FSM method for the finish
1656 struct return_value_info
*
1657 finish_command_fsm::return_value ()
1659 return &return_value_info
;
1662 /* Implementation of the 'async_reply_reason' FSM method for the
1665 enum async_reply_reason
1666 finish_command_fsm::do_async_reply_reason ()
1668 if (execution_direction
== EXEC_REVERSE
)
1669 return EXEC_ASYNC_END_STEPPING_RANGE
;
1671 return EXEC_ASYNC_FUNCTION_FINISHED
;
1674 /* finish_backward -- helper function for finish_command. */
1677 finish_backward (struct finish_command_fsm
*sm
)
1679 struct symtab_and_line sal
;
1680 struct thread_info
*tp
= inferior_thread ();
1682 CORE_ADDR func_addr
;
1684 pc
= get_frame_pc (get_current_frame ());
1686 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
) == 0)
1687 error (_("Cannot find bounds of current function"));
1689 sal
= find_pc_line (func_addr
, 0);
1691 tp
->control
.proceed_to_finish
= 1;
1692 /* Special case: if we're sitting at the function entry point,
1693 then all we need to do is take a reverse singlestep. We
1694 don't need to set a breakpoint, and indeed it would do us
1697 Note that this can only happen at frame #0, since there's
1698 no way that a function up the stack can have a return address
1699 that's equal to its entry point. */
1703 struct frame_info
*frame
= get_selected_frame (NULL
);
1704 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1706 /* Set a step-resume at the function's entry point. Once that's
1707 hit, we'll do one more step backwards. */
1708 symtab_and_line sr_sal
;
1710 sr_sal
.pspace
= get_frame_program_space (frame
);
1711 insert_step_resume_breakpoint_at_sal (gdbarch
,
1712 sr_sal
, null_frame_id
);
1714 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1718 /* We're almost there -- we just need to back up by one more
1720 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1721 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1725 /* finish_forward -- helper function for finish_command. FRAME is the
1726 frame that called the function we're about to step out of. */
1729 finish_forward (struct finish_command_fsm
*sm
, struct frame_info
*frame
)
1731 struct frame_id frame_id
= get_frame_id (frame
);
1732 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1733 struct symtab_and_line sal
;
1734 struct thread_info
*tp
= inferior_thread ();
1736 sal
= find_pc_line (get_frame_pc (frame
), 0);
1737 sal
.pc
= get_frame_pc (frame
);
1739 sm
->breakpoint
= set_momentary_breakpoint (gdbarch
, sal
,
1740 get_stack_frame_id (frame
),
1743 /* set_momentary_breakpoint invalidates FRAME. */
1746 set_longjmp_breakpoint (tp
, frame_id
);
1748 /* We want to print return value, please... */
1749 tp
->control
.proceed_to_finish
= 1;
1751 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1754 /* Skip frames for "finish". */
1756 static struct frame_info
*
1757 skip_finish_frames (struct frame_info
*frame
)
1759 struct frame_info
*start
;
1765 frame
= skip_tailcall_frames (frame
);
1769 frame
= skip_unwritable_frames (frame
);
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 struct frame_info
*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 (NULL
), 1, LOCATION
, 0);
1838 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1842 /* Find the function we will return from. */
1844 sm
->function
= find_pc_function (get_frame_pc (get_selected_frame (NULL
)));
1846 /* Print info on the selected frame, including level number but not
1850 if (execution_direction
== EXEC_REVERSE
)
1851 gdb_printf (_("Run back to call of "));
1854 if (sm
->function
!= NULL
&& TYPE_NO_RETURN (sm
->function
->type ())
1855 && !query (_("warning: Function %s does not return normally.\n"
1856 "Try to finish anyway? "),
1857 sm
->function
->print_name ()))
1858 error (_("Not confirmed."));
1859 gdb_printf (_("Run till exit from "));
1862 print_stack_frame (get_selected_frame (NULL
), 1, LOCATION
, 0);
1865 if (execution_direction
== EXEC_REVERSE
)
1866 finish_backward (sm
);
1869 frame
= skip_finish_frames (frame
);
1872 error (_("Cannot find the caller frame."));
1874 finish_forward (sm
, frame
);
1880 info_program_command (const char *args
, int from_tty
)
1885 process_stratum_target
*proc_target
;
1887 if (!target_has_execution ())
1889 gdb_printf (_("The program being debugged is not being run.\n"));
1895 ptid
= inferior_ptid
;
1896 proc_target
= current_inferior ()->process_target ();
1899 get_last_target_status (&proc_target
, &ptid
, nullptr);
1901 if (ptid
== null_ptid
|| ptid
== minus_one_ptid
)
1902 error (_("No selected thread."));
1904 thread_info
*tp
= find_thread_ptid (proc_target
, ptid
);
1906 if (tp
->state
== THREAD_EXITED
)
1907 error (_("Invalid selected thread."));
1908 else if (tp
->state
== THREAD_RUNNING
)
1909 error (_("Selected thread is running."));
1911 bs
= tp
->control
.stop_bpstat
;
1912 stat
= bpstat_num (&bs
, &num
);
1914 target_files_info ();
1915 gdb_printf (_("Program stopped at %s.\n"),
1916 paddress (target_gdbarch (), tp
->stop_pc ()));
1917 if (tp
->control
.stop_step
)
1918 gdb_printf (_("It stopped after being stepped.\n"));
1921 /* There may be several breakpoints in the same place, so this
1922 isn't as strange as it seems. */
1927 gdb_printf (_("It stopped at a breakpoint "
1928 "that has since been deleted.\n"));
1931 gdb_printf (_("It stopped at breakpoint %d.\n"), num
);
1932 stat
= bpstat_num (&bs
, &num
);
1935 else if (tp
->stop_signal () != GDB_SIGNAL_0
)
1937 gdb_printf (_("It stopped with signal %s, %s.\n"),
1938 gdb_signal_to_name (tp
->stop_signal ()),
1939 gdb_signal_to_string (tp
->stop_signal ()));
1944 gdb_printf (_("Type \"info stack\" or \"info "
1945 "registers\" for more information.\n"));
1950 environment_info (const char *var
, int from_tty
)
1954 const char *val
= current_inferior ()->environment
.get (var
);
1965 gdb_puts ("Environment variable \"");
1967 gdb_puts ("\" not defined.\n");
1972 char **envp
= current_inferior ()->environment
.envp ();
1974 for (int idx
= 0; envp
[idx
] != NULL
; ++idx
)
1976 gdb_puts (envp
[idx
]);
1983 set_environment_command (const char *arg
, int from_tty
)
1985 const char *p
, *val
;
1989 error_no_arg (_("environment variable and value"));
1991 /* Find separation between variable name and value. */
1992 p
= (char *) strchr (arg
, '=');
1993 val
= (char *) strchr (arg
, ' ');
1995 if (p
!= 0 && val
!= 0)
1997 /* We have both a space and an equals. If the space is before the
1998 equals, walk forward over the spaces til we see a nonspace
1999 (possibly the equals). */
2004 /* Now if the = is after the char following the spaces,
2005 take the char following the spaces. */
2009 else if (val
!= 0 && p
== 0)
2013 error_no_arg (_("environment variable to set"));
2015 if (p
== 0 || p
[1] == 0)
2019 p
= arg
+ strlen (arg
); /* So that savestring below will work. */
2023 /* Not setting variable value to null. */
2025 while (*val
== ' ' || *val
== '\t')
2029 while (p
!= arg
&& (p
[-1] == ' ' || p
[-1] == '\t'))
2032 std::string
var (arg
, p
- arg
);
2035 gdb_printf (_("Setting environment variable "
2036 "\"%s\" to null value.\n"),
2038 current_inferior ()->environment
.set (var
.c_str (), "");
2041 current_inferior ()->environment
.set (var
.c_str (), val
);
2045 unset_environment_command (const char *var
, int from_tty
)
2049 /* If there is no argument, delete all environment variables.
2050 Ask for confirmation if reading from the terminal. */
2051 if (!from_tty
|| query (_("Delete all environment variables? ")))
2052 current_inferior ()->environment
.clear ();
2055 current_inferior ()->environment
.unset (var
);
2058 /* Handle the execution path (PATH variable). */
2060 static const char path_var_name
[] = "PATH";
2063 path_info (const char *args
, int from_tty
)
2065 gdb_puts ("Executable and object file path: ");
2066 gdb_puts (current_inferior ()->environment
.get (path_var_name
));
2070 /* Add zero or more directories to the front of the execution path. */
2073 path_command (const char *dirname
, int from_tty
)
2078 env
= current_inferior ()->environment
.get (path_var_name
);
2079 /* Can be null if path is not set. */
2082 std::string exec_path
= env
;
2083 mod_path (dirname
, exec_path
);
2084 current_inferior ()->environment
.set (path_var_name
, exec_path
.c_str ());
2086 path_info (NULL
, from_tty
);
2091 pad_to_column (string_file
&stream
, int col
)
2093 /* At least one space must be printed to separate columns. */
2095 const int size
= stream
.size ();
2097 stream
.puts (n_spaces (col
- size
));
2100 /* Print out the register NAME with value VAL, to FILE, in the default
2104 default_print_one_register_info (struct ui_file
*file
,
2108 struct type
*regtype
= value_type (val
);
2109 int print_raw_format
;
2110 string_file format_stream
;
2113 value_column_1
= 15,
2114 /* Give enough room for "0x", 16 hex digits and two spaces in
2115 preceding column. */
2116 value_column_2
= value_column_1
+ 2 + 16 + 2,
2119 format_stream
.puts (name
);
2120 pad_to_column (format_stream
, value_column_1
);
2122 print_raw_format
= (value_entirely_available (val
)
2123 && !value_optimized_out (val
));
2125 /* If virtual format is floating, print it that way, and in raw
2127 if (regtype
->code () == TYPE_CODE_FLT
2128 || regtype
->code () == TYPE_CODE_DECFLOAT
)
2130 struct value_print_options opts
;
2131 const gdb_byte
*valaddr
= value_contents_for_printing (val
).data ();
2132 enum bfd_endian byte_order
= type_byte_order (regtype
);
2134 get_user_print_options (&opts
);
2137 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2139 if (print_raw_format
)
2141 pad_to_column (format_stream
, value_column_2
);
2142 format_stream
.puts ("(raw ");
2143 print_hex_chars (&format_stream
, valaddr
, TYPE_LENGTH (regtype
),
2145 format_stream
.putc (')');
2150 struct value_print_options opts
;
2152 /* Print the register in hex. */
2153 get_formatted_print_options (&opts
, 'x');
2155 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2156 /* If not a vector register, print it also according to its
2158 if (print_raw_format
&& regtype
->is_vector () == 0)
2160 pad_to_column (format_stream
, value_column_2
);
2161 get_user_print_options (&opts
);
2163 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2167 gdb_puts (format_stream
.c_str (), file
);
2168 gdb_printf (file
, "\n");
2171 /* Print out the machine register regnum. If regnum is -1, print all
2172 registers (print_all == 1) or all non-float and non-vector
2173 registers (print_all == 0).
2175 For most machines, having all_registers_info() print the
2176 register(s) one per line is good enough. If a different format is
2177 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2178 regs), or there is an existing convention for showing all the
2179 registers, define the architecture method PRINT_REGISTERS_INFO to
2180 provide that format. */
2183 default_print_registers_info (struct gdbarch
*gdbarch
,
2184 struct ui_file
*file
,
2185 struct frame_info
*frame
,
2186 int regnum
, int print_all
)
2189 const int numregs
= gdbarch_num_cooked_regs (gdbarch
);
2191 for (i
= 0; i
< numregs
; i
++)
2193 /* Decide between printing all regs, non-float / vector regs, or
2199 if (!gdbarch_register_reggroup_p (gdbarch
, i
, all_reggroup
))
2204 if (!gdbarch_register_reggroup_p (gdbarch
, i
, general_reggroup
))
2214 /* If the register name is empty, it is undefined for this
2215 processor, so don't display anything. */
2216 if (gdbarch_register_name (gdbarch
, i
) == NULL
2217 || *(gdbarch_register_name (gdbarch
, i
)) == '\0')
2220 default_print_one_register_info (file
,
2221 gdbarch_register_name (gdbarch
, i
),
2222 value_of_register (i
, frame
));
2227 registers_info (const char *addr_exp
, int fpregs
)
2229 struct frame_info
*frame
;
2230 struct gdbarch
*gdbarch
;
2232 if (!target_has_registers ())
2233 error (_("The program has no registers now."));
2234 frame
= get_selected_frame (NULL
);
2235 gdbarch
= get_frame_arch (frame
);
2239 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2244 while (*addr_exp
!= '\0')
2249 /* Skip leading white space. */
2250 addr_exp
= skip_spaces (addr_exp
);
2252 /* Discard any leading ``$''. Check that there is something
2253 resembling a register following it. */
2254 if (addr_exp
[0] == '$')
2256 if (isspace ((*addr_exp
)) || (*addr_exp
) == '\0')
2257 error (_("Missing register name"));
2259 /* Find the start/end of this register name/num/group. */
2261 while ((*addr_exp
) != '\0' && !isspace ((*addr_exp
)))
2265 /* Figure out what we've found and display it. */
2267 /* A register name? */
2269 int regnum
= user_reg_map_name_to_regnum (gdbarch
, start
, end
- start
);
2273 /* User registers lie completely outside of the range of
2274 normal registers. Catch them early so that the target
2276 if (regnum
>= gdbarch_num_cooked_regs (gdbarch
))
2278 struct value
*regval
= value_of_user_reg (regnum
, frame
);
2279 const char *regname
= user_reg_map_regnum_to_name (gdbarch
,
2282 /* Print in the same fashion
2283 gdbarch_print_registers_info's default
2284 implementation prints. */
2285 default_print_one_register_info (gdb_stdout
,
2290 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2291 frame
, regnum
, fpregs
);
2296 /* A register group? */
2298 struct reggroup
*group
;
2300 for (group
= reggroup_next (gdbarch
, NULL
);
2302 group
= reggroup_next (gdbarch
, group
))
2304 /* Don't bother with a length check. Should the user
2305 enter a short register group name, go with the first
2306 group that matches. */
2307 if (strncmp (start
, reggroup_name (group
), end
- start
) == 0)
2315 regnum
< gdbarch_num_cooked_regs (gdbarch
);
2318 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
2319 gdbarch_print_registers_info (gdbarch
,
2327 /* Nothing matched. */
2328 error (_("Invalid register `%.*s'"), (int) (end
- start
), start
);
2333 info_all_registers_command (const char *addr_exp
, int from_tty
)
2335 registers_info (addr_exp
, 1);
2339 info_registers_command (const char *addr_exp
, int from_tty
)
2341 registers_info (addr_exp
, 0);
2345 print_vector_info (struct ui_file
*file
,
2346 struct frame_info
*frame
, const char *args
)
2348 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
2350 if (gdbarch_print_vector_info_p (gdbarch
))
2351 gdbarch_print_vector_info (gdbarch
, file
, frame
, args
);
2355 int printed_something
= 0;
2357 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2359 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, vector_reggroup
))
2361 printed_something
= 1;
2362 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2365 if (!printed_something
)
2366 gdb_printf (file
, "No vector information\n");
2371 info_vector_command (const char *args
, int from_tty
)
2373 if (!target_has_registers ())
2374 error (_("The program has no registers now."));
2376 print_vector_info (gdb_stdout
, get_selected_frame (NULL
), args
);
2379 /* Kill the inferior process. Make us have no inferior. */
2382 kill_command (const char *arg
, int from_tty
)
2384 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2385 It should be a distinct flag that indicates that a target is active, cuz
2386 some targets don't have processes! */
2388 if (inferior_ptid
== null_ptid
)
2389 error (_("The program is not being run."));
2390 if (!query (_("Kill the program being debugged? ")))
2391 error (_("Not confirmed."));
2393 int pid
= current_inferior ()->pid
;
2394 /* Save the pid as a string before killing the inferior, since that
2395 may unpush the current target, and we need the string after. */
2396 std::string pid_str
= target_pid_to_str (ptid_t (pid
));
2397 int infnum
= current_inferior ()->num
;
2400 bfd_cache_close_all ();
2402 if (print_inferior_events
)
2403 gdb_printf (_("[Inferior %d (%s) killed]\n"),
2404 infnum
, pid_str
.c_str ());
2407 /* Used in `attach&' command. Proceed threads of inferior INF iff
2408 they stopped due to debugger request, and when they did, they
2409 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2410 have been explicitly been told to stop. */
2413 proceed_after_attach (inferior
*inf
)
2415 /* Don't error out if the current thread is running, because
2416 there may be other stopped threads. */
2418 /* Backup current thread and selected frame. */
2419 scoped_restore_current_thread restore_thread
;
2421 for (thread_info
*thread
: inf
->non_exited_threads ())
2422 if (!thread
->executing ()
2423 && !thread
->stop_requested
2424 && thread
->stop_signal () == GDB_SIGNAL_0
)
2426 switch_to_thread (thread
);
2427 clear_proceed_status (0);
2428 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2432 /* See inferior.h. */
2435 setup_inferior (int from_tty
)
2437 struct inferior
*inferior
;
2439 inferior
= current_inferior ();
2440 inferior
->needs_setup
= 0;
2442 /* If no exec file is yet known, try to determine it from the
2444 if (get_exec_file (0) == NULL
)
2445 exec_file_locate_attach (inferior_ptid
.pid (), 1, from_tty
);
2448 reopen_exec_file ();
2449 reread_symbols (from_tty
);
2452 /* Take any necessary post-attaching actions for this platform. */
2453 target_post_attach (inferior_ptid
.pid ());
2455 post_create_inferior (from_tty
);
2458 /* What to do after the first program stops after attaching. */
2459 enum attach_post_wait_mode
2461 /* Do nothing. Leaves threads as they are. */
2462 ATTACH_POST_WAIT_NOTHING
,
2464 /* Re-resume threads that are marked running. */
2465 ATTACH_POST_WAIT_RESUME
,
2467 /* Stop all threads. */
2468 ATTACH_POST_WAIT_STOP
,
2471 /* Called after we've attached to a process and we've seen it stop for
2472 the first time. Resume, stop, or don't touch the threads according
2476 attach_post_wait (int from_tty
, enum attach_post_wait_mode mode
)
2478 struct inferior
*inferior
;
2480 inferior
= current_inferior ();
2481 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
2483 if (inferior
->needs_setup
)
2484 setup_inferior (from_tty
);
2486 if (mode
== ATTACH_POST_WAIT_RESUME
)
2488 /* The user requested an `attach&', so be sure to leave threads
2489 that didn't get a signal running. */
2491 /* Immediately resume all suspended threads of this inferior,
2492 and this inferior only. This should have no effect on
2493 already running threads. If a thread has been stopped with a
2494 signal, leave it be. */
2496 proceed_after_attach (inferior
);
2499 if (inferior_thread ()->stop_signal () == GDB_SIGNAL_0
)
2501 clear_proceed_status (0);
2502 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2506 else if (mode
== ATTACH_POST_WAIT_STOP
)
2508 /* The user requested a plain `attach', so be sure to leave
2509 the inferior stopped. */
2511 /* At least the current thread is already stopped. */
2513 /* In all-stop, by definition, all threads have to be already
2514 stopped at this point. In non-stop, however, although the
2515 selected thread is stopped, others may still be executing.
2516 Be sure to explicitly stop all threads of the process. This
2517 should have no effect on already stopped threads. */
2519 target_stop (ptid_t (inferior
->pid
));
2520 else if (target_is_non_stop_p ())
2522 struct thread_info
*lowest
= inferior_thread ();
2524 stop_all_threads ("attaching");
2526 /* It's not defined which thread will report the attach
2527 stop. For consistency, always select the thread with
2528 lowest GDB number, which should be the main thread, if it
2530 for (thread_info
*thread
: current_inferior ()->non_exited_threads ())
2531 if (thread
->inf
->num
< lowest
->inf
->num
2532 || thread
->per_inf_num
< lowest
->per_inf_num
)
2535 switch_to_thread (lowest
);
2538 /* Tell the user/frontend where we're stopped. */
2540 if (deprecated_attach_hook
)
2541 deprecated_attach_hook ();
2545 /* "attach" command entry point. Takes a program started up outside
2546 of gdb and ``attaches'' to it. This stops it cold in its tracks
2547 and allows us to start debugging it. */
2550 attach_command (const char *args
, int from_tty
)
2553 struct target_ops
*attach_target
;
2554 struct inferior
*inferior
= current_inferior ();
2555 enum attach_post_wait_mode mode
;
2557 dont_repeat (); /* Not for the faint of heart */
2559 scoped_disable_commit_resumed
disable_commit_resumed ("attaching");
2561 if (gdbarch_has_global_solist (target_gdbarch ()))
2562 /* Don't complain if all processes share the same symbol
2565 else if (target_has_execution ())
2567 if (query (_("A program is being debugged already. Kill it? ")))
2570 error (_("Not killed."));
2573 /* Clean up any leftovers from other runs. Some other things from
2574 this function should probably be moved into target_pre_inferior. */
2575 target_pre_inferior (from_tty
);
2577 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
2578 args
= stripped
.get ();
2580 attach_target
= find_attach_target ();
2582 prepare_execution_command (attach_target
, async_exec
);
2584 if (non_stop
&& !attach_target
->supports_non_stop ())
2585 error (_("Cannot attach to this target in non-stop mode"));
2587 attach_target
->attach (args
, from_tty
);
2588 /* to_attach should push the target, so after this point we
2589 shouldn't refer to attach_target again. */
2590 attach_target
= NULL
;
2594 infrun_debug_printf ("immediately after attach:");
2595 for (thread_info
*thread
: inferior
->non_exited_threads ())
2596 infrun_debug_printf (" thread %s, executing = %d, resumed = %d, "
2598 thread
->ptid
.to_string ().c_str (),
2599 thread
->executing (),
2601 thread_state_string (thread
->state
));
2604 /* Enable async mode if it is supported by the target. */
2605 if (target_can_async_p ())
2608 /* Set up the "saved terminal modes" of the inferior
2609 based on what modes we are starting it with. */
2610 target_terminal::init ();
2612 /* Install inferior's terminal modes. This may look like a no-op,
2613 as we've just saved them above, however, this does more than
2614 restore terminal settings:
2616 - installs a SIGINT handler that forwards SIGINT to the inferior.
2617 Otherwise a Ctrl-C pressed just while waiting for the initial
2618 stop would end up as a spurious Quit.
2620 - removes stdin from the event loop, which we need if attaching
2621 in the foreground, otherwise on targets that report an initial
2622 stop on attach (which are most) we'd process input/commands
2623 while we're in the event loop waiting for that stop. That is,
2624 before the attach continuation runs and the command is really
2626 target_terminal::inferior ();
2628 /* Set up execution context to know that we should return from
2629 wait_for_inferior as soon as the target reports a stop. */
2630 init_wait_for_inferior ();
2632 inferior
->needs_setup
= 1;
2634 if (target_is_non_stop_p ())
2636 /* If we find that the current thread isn't stopped, explicitly
2637 do so now, because we're going to install breakpoints and
2641 /* The user requested an `attach&'; stop just one thread. */
2642 target_stop (inferior_ptid
);
2644 /* The user requested an `attach', so stop all threads of this
2646 target_stop (ptid_t (inferior_ptid
.pid ()));
2649 /* Check for exec file mismatch, and let the user solve it. */
2650 validate_exec_file (from_tty
);
2652 mode
= async_exec
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_STOP
;
2654 /* Some system don't generate traps when attaching to inferior.
2655 E.g. Mach 3 or GNU hurd. */
2656 if (!target_attach_no_wait ())
2658 /* Careful here. See comments in inferior.h. Basically some
2659 OSes don't ignore SIGSTOPs on continue requests anymore. We
2660 need a way for handle_inferior_event to reset the stop_signal
2661 variable after an attach, and this is what
2662 STOP_QUIETLY_NO_SIGSTOP is for. */
2663 inferior
->control
.stop_soon
= STOP_QUIETLY_NO_SIGSTOP
;
2665 /* Wait for stop. */
2666 inferior
->add_continuation ([=] ()
2668 attach_post_wait (from_tty
, mode
);
2671 /* Let infrun consider waiting for events out of this
2673 inferior
->process_target ()->threads_executing
= true;
2675 if (!target_is_async_p ())
2676 mark_infrun_async_event_handler ();
2680 attach_post_wait (from_tty
, mode
);
2682 disable_commit_resumed
.reset_and_commit ();
2685 /* We had just found out that the target was already attached to an
2686 inferior. PTID points at a thread of this new inferior, that is
2687 the most likely to be stopped right now, but not necessarily so.
2688 The new inferior is assumed to be already added to the inferior
2689 list at this point. If LEAVE_RUNNING, then leave the threads of
2690 this inferior running, except those we've explicitly seen reported
2694 notice_new_inferior (thread_info
*thr
, bool leave_running
, int from_tty
)
2696 enum attach_post_wait_mode mode
2697 = leave_running
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_NOTHING
;
2699 gdb::optional
<scoped_restore_current_thread
> restore_thread
;
2701 if (inferior_ptid
!= null_ptid
)
2702 restore_thread
.emplace ();
2704 /* Avoid reading registers -- we haven't fetched the target
2706 switch_to_thread_no_regs (thr
);
2708 /* When we "notice" a new inferior we need to do all the things we
2709 would normally do if we had just attached to it. */
2711 if (thr
->executing ())
2713 struct inferior
*inferior
= current_inferior ();
2715 /* We're going to install breakpoints, and poke at memory,
2716 ensure that the inferior is stopped for a moment while we do
2718 target_stop (inferior_ptid
);
2720 inferior
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
2722 /* Wait for stop before proceeding. */
2723 inferior
->add_continuation ([=] ()
2725 attach_post_wait (from_tty
, mode
);
2731 attach_post_wait (from_tty
, mode
);
2736 * takes a program previously attached to and detaches it.
2737 * The program resumes execution and will no longer stop
2738 * on signals, etc. We better not have left any breakpoints
2739 * in the program or it'll die when it hits one. For this
2740 * to work, it may be necessary for the process to have been
2741 * previously attached. It *might* work if the program was
2742 * started via the normal ptrace (PTRACE_TRACEME).
2746 detach_command (const char *args
, int from_tty
)
2748 dont_repeat (); /* Not for the faint of heart. */
2750 if (inferior_ptid
== null_ptid
)
2751 error (_("The program is not being run."));
2753 scoped_disable_commit_resumed
disable_commit_resumed ("detaching");
2755 query_if_trace_running (from_tty
);
2757 disconnect_tracing ();
2759 /* Hold a strong reference to the target while (maybe)
2760 detaching the parent. Otherwise detaching could close the
2763 = target_ops_ref::new_reference (current_inferior ()->process_target ());
2765 /* Save this before detaching, since detaching may unpush the
2766 process_stratum target. */
2767 bool was_non_stop_p
= target_is_non_stop_p ();
2769 target_detach (current_inferior (), from_tty
);
2771 /* The current inferior process was just detached successfully. Get
2772 rid of breakpoints that no longer make sense. Note we don't do
2773 this within target_detach because that is also used when
2774 following child forks, and in that case we will want to transfer
2775 breakpoints to the child, not delete them. */
2776 breakpoint_init_inferior (inf_exited
);
2778 /* If the solist is global across inferiors, don't clear it when we
2779 detach from a single inferior. */
2780 if (!gdbarch_has_global_solist (target_gdbarch ()))
2781 no_shared_libraries (NULL
, from_tty
);
2783 if (deprecated_detach_hook
)
2784 deprecated_detach_hook ();
2786 if (!was_non_stop_p
)
2787 restart_after_all_stop_detach (as_process_stratum_target (target_ref
.get ()));
2789 disable_commit_resumed
.reset_and_commit ();
2792 /* Disconnect from the current target without resuming it (leaving it
2793 waiting for a debugger).
2795 We'd better not have left any breakpoints in the program or the
2796 next debugger will get confused. Currently only supported for some
2797 remote targets, since the normal attach mechanisms don't work on
2798 stopped processes on some native platforms (e.g. GNU/Linux). */
2801 disconnect_command (const char *args
, int from_tty
)
2803 dont_repeat (); /* Not for the faint of heart. */
2804 query_if_trace_running (from_tty
);
2805 disconnect_tracing ();
2806 target_disconnect (args
, from_tty
);
2807 no_shared_libraries (NULL
, from_tty
);
2808 init_thread_list ();
2809 if (deprecated_detach_hook
)
2810 deprecated_detach_hook ();
2813 /* Stop PTID in the current target, and tag the PTID threads as having
2814 been explicitly requested to stop. PTID can be a thread, a
2815 process, or minus_one_ptid, meaning all threads of all inferiors of
2816 the current target. */
2819 stop_current_target_threads_ns (ptid_t ptid
)
2823 /* Tag the thread as having been explicitly requested to stop, so
2824 other parts of gdb know not to resume this thread automatically,
2825 if it was stopped due to an internal event. Limit this to
2826 non-stop mode, as when debugging a multi-threaded application in
2827 all-stop mode, we will only get one stop event --- it's undefined
2828 which thread will report the event. */
2829 set_stop_requested (current_inferior ()->process_target (),
2833 /* See inferior.h. */
2836 interrupt_target_1 (bool all_threads
)
2838 scoped_disable_commit_resumed
disable_commit_resumed ("interrupting");
2844 scoped_restore_current_thread restore_thread
;
2846 for (inferior
*inf
: all_inferiors ())
2848 switch_to_inferior_no_thread (inf
);
2849 stop_current_target_threads_ns (minus_one_ptid
);
2853 stop_current_target_threads_ns (inferior_ptid
);
2856 target_interrupt ();
2858 disable_commit_resumed
.reset_and_commit ();
2862 Stop the execution of the target while running in async mode, in
2863 the background. In all-stop, stop the whole process. In non-stop
2864 mode, stop the current thread only by default, or stop all threads
2865 if the `-a' switch is used. */
2868 interrupt_command (const char *args
, int from_tty
)
2870 if (target_can_async_p ())
2872 int all_threads
= 0;
2874 dont_repeat (); /* Not for the faint of heart. */
2877 && startswith (args
, "-a"))
2880 if (!non_stop
&& all_threads
)
2881 error (_("-a is meaningless in all-stop mode."));
2883 interrupt_target_1 (all_threads
);
2887 /* See inferior.h. */
2890 default_print_float_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
2891 struct frame_info
*frame
, const char *args
)
2894 int printed_something
= 0;
2896 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2898 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, float_reggroup
))
2900 printed_something
= 1;
2901 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2904 if (!printed_something
)
2905 gdb_printf (file
, "No floating-point info "
2906 "available for this processor.\n");
2910 info_float_command (const char *args
, int from_tty
)
2912 struct frame_info
*frame
;
2914 if (!target_has_registers ())
2915 error (_("The program has no registers now."));
2917 frame
= get_selected_frame (NULL
);
2918 gdbarch_print_float_info (get_frame_arch (frame
), gdb_stdout
, frame
, args
);
2921 /* Implement `info proc' family of commands. */
2924 info_proc_cmd_1 (const char *args
, enum info_proc_what what
, int from_tty
)
2926 struct gdbarch
*gdbarch
= get_current_arch ();
2928 if (!target_info_proc (args
, what
))
2930 if (gdbarch_info_proc_p (gdbarch
))
2931 gdbarch_info_proc (gdbarch
, args
, what
);
2933 error (_("Not supported on this target."));
2937 /* Implement `info proc' when given without any further parameters. */
2940 info_proc_cmd (const char *args
, int from_tty
)
2942 info_proc_cmd_1 (args
, IP_MINIMAL
, from_tty
);
2945 /* Implement `info proc mappings'. */
2948 info_proc_cmd_mappings (const char *args
, int from_tty
)
2950 info_proc_cmd_1 (args
, IP_MAPPINGS
, from_tty
);
2953 /* Implement `info proc stat'. */
2956 info_proc_cmd_stat (const char *args
, int from_tty
)
2958 info_proc_cmd_1 (args
, IP_STAT
, from_tty
);
2961 /* Implement `info proc status'. */
2964 info_proc_cmd_status (const char *args
, int from_tty
)
2966 info_proc_cmd_1 (args
, IP_STATUS
, from_tty
);
2969 /* Implement `info proc cwd'. */
2972 info_proc_cmd_cwd (const char *args
, int from_tty
)
2974 info_proc_cmd_1 (args
, IP_CWD
, from_tty
);
2977 /* Implement `info proc cmdline'. */
2980 info_proc_cmd_cmdline (const char *args
, int from_tty
)
2982 info_proc_cmd_1 (args
, IP_CMDLINE
, from_tty
);
2985 /* Implement `info proc exe'. */
2988 info_proc_cmd_exe (const char *args
, int from_tty
)
2990 info_proc_cmd_1 (args
, IP_EXE
, from_tty
);
2993 /* Implement `info proc files'. */
2996 info_proc_cmd_files (const char *args
, int from_tty
)
2998 info_proc_cmd_1 (args
, IP_FILES
, from_tty
);
3001 /* Implement `info proc all'. */
3004 info_proc_cmd_all (const char *args
, int from_tty
)
3006 info_proc_cmd_1 (args
, IP_ALL
, from_tty
);
3009 /* Implement `show print finish'. */
3012 show_print_finish (struct ui_file
*file
, int from_tty
,
3013 struct cmd_list_element
*c
,
3016 gdb_printf (file
, _("\
3017 Printing of return value after `finish' is %s.\n"),
3022 /* This help string is used for the run, start, and starti commands.
3023 It is defined as a macro to prevent duplication. */
3025 #define RUN_ARGS_HELP \
3026 "You may specify arguments to give it.\n\
3027 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3028 shell that will start the program (specified by the \"$SHELL\" environment\n\
3029 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3030 are also allowed.\n\
3032 With no arguments, uses arguments last specified (with \"run\" or \n\
3033 \"set args\"). To cancel previous arguments and run with no arguments,\n\
3034 use \"set args\" without arguments.\n\
3036 To start the inferior without using a shell, use \"set startup-with-shell off\"."
3038 void _initialize_infcmd ();
3040 _initialize_infcmd ()
3042 static struct cmd_list_element
*info_proc_cmdlist
;
3043 struct cmd_list_element
*c
= NULL
;
3044 const char *cmd_name
;
3046 /* Add the filename of the terminal connected to inferior I/O. */
3047 add_setshow_optional_filename_cmd ("inferior-tty", class_run
,
3048 &inferior_io_terminal_scratch
, _("\
3049 Set terminal for future runs of program being debugged."), _("\
3050 Show terminal for future runs of program being debugged."), _("\
3051 Usage: set inferior-tty [TTY]\n\n\
3052 If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3054 set_inferior_tty_command
,
3055 show_inferior_tty_command
,
3056 &setlist
, &showlist
);
3057 cmd_name
= "inferior-tty";
3058 c
= lookup_cmd (&cmd_name
, setlist
, "", NULL
, -1, 1);
3059 gdb_assert (c
!= NULL
);
3060 add_alias_cmd ("tty", c
, class_run
, 0, &cmdlist
);
3063 add_setshow_string_noescape_cmd (cmd_name
, class_run
,
3064 &inferior_args_scratch
, _("\
3065 Set argument list to give program being debugged when it is started."), _("\
3066 Show argument list to give program being debugged when it is started."), _("\
3067 Follow this command with any number of args, to be passed to the program."),
3070 &setlist
, &showlist
);
3071 c
= lookup_cmd (&cmd_name
, setlist
, "", NULL
, -1, 1);
3072 gdb_assert (c
!= NULL
);
3073 set_cmd_completer (c
, filename_completer
);
3076 add_setshow_string_noescape_cmd (cmd_name
, class_run
,
3077 &inferior_cwd_scratch
, _("\
3078 Set the current working directory to be used when the inferior is started.\n\
3079 Changing this setting does not have any effect on inferiors that are\n\
3082 Show the current working directory that is used when the inferior is started."),
3084 Use this command to change the current working directory that will be used\n\
3085 when the inferior is started. This setting does not affect GDB's current\n\
3086 working directory."),
3089 &setlist
, &showlist
);
3090 c
= lookup_cmd (&cmd_name
, setlist
, "", NULL
, -1, 1);
3091 gdb_assert (c
!= NULL
);
3092 set_cmd_completer (c
, filename_completer
);
3094 c
= add_cmd ("environment", no_class
, environment_info
, _("\
3095 The environment to give the program, or one variable's value.\n\
3096 With an argument VAR, prints the value of environment variable VAR to\n\
3097 give the program being debugged. With no arguments, prints the entire\n\
3098 environment to be given to the program."), &showlist
);
3099 set_cmd_completer (c
, noop_completer
);
3101 add_basic_prefix_cmd ("unset", no_class
,
3102 _("Complement to certain \"set\" commands."),
3103 &unsetlist
, 0, &cmdlist
);
3105 c
= add_cmd ("environment", class_run
, unset_environment_command
, _("\
3106 Cancel environment variable VAR for the program.\n\
3107 This does not affect the program until the next \"run\" command."),
3109 set_cmd_completer (c
, noop_completer
);
3111 c
= add_cmd ("environment", class_run
, set_environment_command
, _("\
3112 Set environment variable value to give the program.\n\
3113 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3114 VALUES of environment variables are uninterpreted strings.\n\
3115 This does not affect the program until the next \"run\" command."),
3117 set_cmd_completer (c
, noop_completer
);
3119 c
= add_com ("path", class_files
, path_command
, _("\
3120 Add directory DIR(s) to beginning of search path for object files.\n\
3121 $cwd in the path means the current working directory.\n\
3122 This path is equivalent to the $PATH shell variable. It is a list of\n\
3123 directories, separated by colons. These directories are searched to find\n\
3124 fully linked executable files and separately compiled object files as \
3126 set_cmd_completer (c
, filename_completer
);
3128 c
= add_cmd ("paths", no_class
, path_info
, _("\
3129 Current search path for finding object files.\n\
3130 $cwd in the path means the current working directory.\n\
3131 This path is equivalent to the $PATH shell variable. It is a list of\n\
3132 directories, separated by colons. These directories are searched to find\n\
3133 fully linked executable files and separately compiled object files as \
3136 set_cmd_completer (c
, noop_completer
);
3138 add_prefix_cmd ("kill", class_run
, kill_command
,
3139 _("Kill execution of program being debugged."),
3140 &killlist
, 0, &cmdlist
);
3142 add_com ("attach", class_run
, attach_command
, _("\
3143 Attach to a process or file outside of GDB.\n\
3144 This command attaches to another target, of the same type as your last\n\
3145 \"target\" command (\"info files\" will show your target stack).\n\
3146 The command may take as argument a process id or a device file.\n\
3147 For a process id, you must have permission to send the process a signal,\n\
3148 and it must have the same effective uid as the debugger.\n\
3149 When using \"attach\" with a process id, the debugger finds the\n\
3150 program running in the process, looking first in the current working\n\
3151 directory, or (if not found there) using the source file search path\n\
3152 (see the \"directory\" command). You can also use the \"file\" command\n\
3153 to specify the program, and to load its symbol table."));
3155 add_prefix_cmd ("detach", class_run
, detach_command
, _("\
3156 Detach a process or file previously attached.\n\
3157 If a process, it is no longer traced, and it continues its execution. If\n\
3158 you were debugging a file, the file is closed and gdb no longer accesses it."),
3159 &detachlist
, 0, &cmdlist
);
3161 add_com ("disconnect", class_run
, disconnect_command
, _("\
3162 Disconnect from a target.\n\
3163 The target will wait for another debugger to connect. Not available for\n\
3166 c
= add_com ("signal", class_run
, signal_command
, _("\
3167 Continue program with the specified signal.\n\
3168 Usage: signal SIGNAL\n\
3169 The SIGNAL argument is processed the same as the handle command.\n\
3171 An argument of \"0\" means continue the program without sending it a signal.\n\
3172 This is useful in cases where the program stopped because of a signal,\n\
3173 and you want to resume the program while discarding the signal.\n\
3175 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3176 the current thread only."));
3177 set_cmd_completer (c
, signal_completer
);
3179 c
= add_com ("queue-signal", class_run
, queue_signal_command
, _("\
3180 Queue a signal to be delivered to the current thread when it is resumed.\n\
3181 Usage: queue-signal SIGNAL\n\
3182 The SIGNAL argument is processed the same as the handle command.\n\
3183 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3185 An argument of \"0\" means remove any currently queued signal from\n\
3186 the current thread. This is useful in cases where the program stopped\n\
3187 because of a signal, and you want to resume it while discarding the signal.\n\
3189 In a multi-threaded program the signal is queued with, or discarded from,\n\
3190 the current thread only."));
3191 set_cmd_completer (c
, signal_completer
);
3193 cmd_list_element
*stepi_cmd
3194 = add_com ("stepi", class_run
, stepi_command
, _("\
3195 Step one instruction exactly.\n\
3197 Argument N means step N times (or till program stops for another \
3199 add_com_alias ("si", stepi_cmd
, class_run
, 0);
3201 cmd_list_element
*nexti_cmd
3202 = add_com ("nexti", class_run
, nexti_command
, _("\
3203 Step one instruction, but proceed through subroutine calls.\n\
3205 Argument N means step N times (or till program stops for another \
3207 add_com_alias ("ni", nexti_cmd
, class_run
, 0);
3209 cmd_list_element
*finish_cmd
3210 = add_com ("finish", class_run
, finish_command
, _("\
3211 Execute until selected stack frame returns.\n\
3213 Upon return, the value returned is printed and put in the value history."));
3214 add_com_alias ("fin", finish_cmd
, class_run
, 1);
3216 cmd_list_element
*next_cmd
3217 = add_com ("next", class_run
, next_command
, _("\
3218 Step program, proceeding through subroutine calls.\n\
3220 Unlike \"step\", if the current source line calls a subroutine,\n\
3221 this command does not enter the subroutine, but instead steps over\n\
3222 the call, in effect treating it as a single source line."));
3223 add_com_alias ("n", next_cmd
, class_run
, 1);
3225 cmd_list_element
*step_cmd
3226 = add_com ("step", class_run
, step_command
, _("\
3227 Step program until it reaches a different source line.\n\
3229 Argument N means step N times (or till program stops for another \
3231 add_com_alias ("s", step_cmd
, class_run
, 1);
3233 cmd_list_element
*until_cmd
3234 = add_com ("until", class_run
, until_command
, _("\
3235 Execute until past the current line or past a LOCATION.\n\
3236 Execute until the program reaches a source line greater than the current\n\
3237 or a specified location (same args as break command) within the current \
3239 set_cmd_completer (until_cmd
, location_completer
);
3240 add_com_alias ("u", until_cmd
, class_run
, 1);
3242 c
= add_com ("advance", class_run
, advance_command
, _("\
3243 Continue the program up to the given location (same form as args for break \
3245 Execution will also stop upon exit from the current stack frame."));
3246 set_cmd_completer (c
, location_completer
);
3248 cmd_list_element
*jump_cmd
3249 = add_com ("jump", class_run
, jump_command
, _("\
3250 Continue program being debugged at specified line or address.\n\
3251 Usage: jump LOCATION\n\
3252 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3253 for an address to start at."));
3254 set_cmd_completer (jump_cmd
, location_completer
);
3255 add_com_alias ("j", jump_cmd
, class_run
, 1);
3257 cmd_list_element
*continue_cmd
3258 = add_com ("continue", class_run
, continue_command
, _("\
3259 Continue program being debugged, after signal or breakpoint.\n\
3260 Usage: continue [N]\n\
3261 If proceeding from breakpoint, a number N may be used as an argument,\n\
3262 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3263 the breakpoint won't break until the Nth time it is reached).\n\
3265 If non-stop mode is enabled, continue only the current thread,\n\
3266 otherwise all the threads in the program are continued. To \n\
3267 continue all stopped threads in non-stop mode, use the -a option.\n\
3268 Specifying -a and an ignore count simultaneously is an error."));
3269 add_com_alias ("c", continue_cmd
, class_run
, 1);
3270 add_com_alias ("fg", continue_cmd
, class_run
, 1);
3272 cmd_list_element
*run_cmd
3273 = add_com ("run", class_run
, run_command
, _("\
3274 Start debugged program.\n"
3276 set_cmd_completer (run_cmd
, filename_completer
);
3277 add_com_alias ("r", run_cmd
, class_run
, 1);
3279 c
= add_com ("start", class_run
, start_command
, _("\
3280 Start the debugged program stopping at the beginning of the main procedure.\n"
3282 set_cmd_completer (c
, filename_completer
);
3284 c
= add_com ("starti", class_run
, starti_command
, _("\
3285 Start the debugged program stopping at the first instruction.\n"
3287 set_cmd_completer (c
, filename_completer
);
3289 add_com ("interrupt", class_run
, interrupt_command
,
3290 _("Interrupt the execution of the debugged program.\n\
3291 If non-stop mode is enabled, interrupt only the current thread,\n\
3292 otherwise all the threads in the program are stopped. To \n\
3293 interrupt all running threads in non-stop mode, use the -a option."));
3295 cmd_list_element
*info_registers_cmd
3296 = add_info ("registers", info_registers_command
, _("\
3297 List of integer registers and their contents, for selected stack frame.\n\
3298 One or more register names as argument means describe the given registers.\n\
3299 One or more register group names as argument means describe the registers\n\
3300 in the named register groups."));
3301 add_info_alias ("r", info_registers_cmd
, 1);
3302 set_cmd_completer (info_registers_cmd
, reg_or_group_completer
);
3304 c
= add_info ("all-registers", info_all_registers_command
, _("\
3305 List of all registers and their contents, for selected stack frame.\n\
3306 One or more register names as argument means describe the given registers.\n\
3307 One or more register group names as argument means describe the registers\n\
3308 in the named register groups."));
3309 set_cmd_completer (c
, reg_or_group_completer
);
3311 add_info ("program", info_program_command
,
3312 _("Execution status of the program."));
3314 add_info ("float", info_float_command
,
3315 _("Print the status of the floating point unit."));
3317 add_info ("vector", info_vector_command
,
3318 _("Print the status of the vector unit."));
3320 add_prefix_cmd ("proc", class_info
, info_proc_cmd
,
3322 Show additional information about a process.\n\
3323 Specify any process id, or use the program being debugged by default."),
3325 1/*allow-unknown*/, &infolist
);
3327 add_cmd ("mappings", class_info
, info_proc_cmd_mappings
, _("\
3328 List memory regions mapped by the specified process."),
3329 &info_proc_cmdlist
);
3331 add_cmd ("stat", class_info
, info_proc_cmd_stat
, _("\
3332 List process info from /proc/PID/stat."),
3333 &info_proc_cmdlist
);
3335 add_cmd ("status", class_info
, info_proc_cmd_status
, _("\
3336 List process info from /proc/PID/status."),
3337 &info_proc_cmdlist
);
3339 add_cmd ("cwd", class_info
, info_proc_cmd_cwd
, _("\
3340 List current working directory of the specified process."),
3341 &info_proc_cmdlist
);
3343 add_cmd ("cmdline", class_info
, info_proc_cmd_cmdline
, _("\
3344 List command line arguments of the specified process."),
3345 &info_proc_cmdlist
);
3347 add_cmd ("exe", class_info
, info_proc_cmd_exe
, _("\
3348 List absolute filename for executable of the specified process."),
3349 &info_proc_cmdlist
);
3351 add_cmd ("files", class_info
, info_proc_cmd_files
, _("\
3352 List files opened by the specified process."),
3353 &info_proc_cmdlist
);
3355 add_cmd ("all", class_info
, info_proc_cmd_all
, _("\
3356 List all available info about the specified process."),
3357 &info_proc_cmdlist
);
3359 add_setshow_boolean_cmd ("finish", class_support
,
3360 &user_print_options
.finish_print
, _("\
3361 Set whether `finish' prints the return value."), _("\
3362 Show whether `finish' prints the return value."), NULL
,
3365 &setprintlist
, &showprintlist
);