1 /* Memory-access and commands for "inferior" process, for GDB.
3 Copyright (C) 1986-2021 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 char *inferior_args_scratch
;
76 /* Scratch area where the new cwd will be stored by 'set cwd'. */
78 static char *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 char *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 char *inferior_tty
= current_inferior ()->tty ();
120 if (inferior_tty
== nullptr)
122 fprintf_filtered (gdb_stdout
,
123 _("Terminal for future runs of program being debugged "
124 "is \"%s\".\n"), inferior_tty
);
128 get_inferior_args (void)
130 if (current_inferior ()->argc
!= 0)
132 gdb::array_view
<char * const> args (current_inferior ()->argv
,
133 current_inferior ()->argc
);
134 std::string n
= construct_inferior_arguments (args
);
135 set_inferior_args (n
.c_str ());
138 if (current_inferior ()->args
== NULL
)
139 current_inferior ()->args
= make_unique_xstrdup ("");
141 return current_inferior ()->args
.get ();
144 /* Set the arguments for the current inferior. Ownership of
145 NEWARGS is not transferred. */
148 set_inferior_args (const char *newargs
)
150 if (newargs
!= nullptr)
151 current_inferior ()->args
= make_unique_xstrdup (newargs
);
153 current_inferior ()->args
.reset ();
155 current_inferior ()->argc
= 0;
156 current_inferior ()->argv
= 0;
160 set_inferior_args_vector (int argc
, char **argv
)
162 current_inferior ()->argc
= argc
;
163 current_inferior ()->argv
= argv
;
166 /* Notice when `set args' is run. */
169 set_args_command (const char *args
, int from_tty
, struct cmd_list_element
*c
)
171 /* CLI has assigned the user-provided value to inferior_args_scratch.
172 Now route it to current inferior. */
173 set_inferior_args (inferior_args_scratch
);
176 /* Notice when `show args' is run. */
179 show_args_command (struct ui_file
*file
, int from_tty
,
180 struct cmd_list_element
*c
, const char *value
)
182 /* Note that we ignore the passed-in value in favor of computing it
184 deprecated_show_value_hack (file
, from_tty
, c
, get_inferior_args ());
187 /* Set the inferior current working directory. If CWD is NULL, unset
191 set_inferior_cwd (const char *cwd
)
193 struct inferior
*inf
= current_inferior ();
195 gdb_assert (inf
!= NULL
);
200 inf
->cwd
.reset (xstrdup (cwd
));
203 /* See gdbsupport/common-inferior.h. */
208 return current_inferior ()->cwd
.get ();
211 /* Handle the 'set cwd' command. */
214 set_cwd_command (const char *args
, int from_tty
, struct cmd_list_element
*c
)
216 if (*inferior_cwd_scratch
== '\0')
217 set_inferior_cwd (NULL
);
219 set_inferior_cwd (inferior_cwd_scratch
);
222 /* Handle the 'show cwd' command. */
225 show_cwd_command (struct ui_file
*file
, int from_tty
,
226 struct cmd_list_element
*c
, const char *value
)
228 const char *cwd
= get_inferior_cwd ();
231 fprintf_filtered (gdb_stdout
,
233 You have not set the inferior's current working directory.\n\
234 The inferior will inherit GDB's cwd if native debugging, or the remote\n\
235 server's cwd if remote debugging.\n"));
237 fprintf_filtered (gdb_stdout
,
238 _("Current working directory that will be used "
239 "when starting the inferior is \"%s\".\n"), cwd
);
243 /* This function strips the '&' character (indicating background
244 execution) that is added as *the last* of the arguments ARGS of a
245 command. A copy of the incoming ARGS without the '&' is returned,
246 unless the resulting string after stripping is empty, in which case
247 NULL is returned. *BG_CHAR_P is an output boolean that indicates
248 whether the '&' character was found. */
250 static gdb::unique_xmalloc_ptr
<char>
251 strip_bg_char (const char *args
, int *bg_char_p
)
255 if (args
== NULL
|| *args
== '\0')
261 p
= args
+ strlen (args
);
265 while (p
> args
&& isspace (p
[-1]))
270 return gdb::unique_xmalloc_ptr
<char>
271 (savestring (args
, p
- args
));
273 return gdb::unique_xmalloc_ptr
<char> (nullptr);
277 return make_unique_xstrdup (args
);
280 /* Common actions to take after creating any sort of inferior, by any
281 means (running, attaching, connecting, et cetera). The target
282 should be stopped. */
285 post_create_inferior (int from_tty
)
288 /* Be sure we own the terminal in case write operations are performed. */
289 target_terminal::ours_for_output ();
291 /* If the target hasn't taken care of this already, do it now.
292 Targets which need to access registers during to_open,
293 to_create_inferior, or to_attach should do it earlier; but many
295 target_find_description ();
297 /* Now that we know the register layout, retrieve current PC. But
298 if the PC is unavailable (e.g., we're opening a core file with
299 missing registers info), ignore it. */
300 thread_info
*thr
= inferior_thread ();
302 thr
->set_stop_pc (0);
305 regcache
*rc
= get_thread_regcache (thr
);
306 thr
->set_stop_pc (regcache_read_pc (rc
));
308 catch (const gdb_exception_error
&ex
)
310 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
314 if (current_program_space
->exec_bfd ())
316 const unsigned solib_add_generation
317 = current_program_space
->solib_add_generation
;
319 scoped_restore restore_in_initial_library_scan
320 = make_scoped_restore (¤t_inferior ()->in_initial_library_scan
,
323 /* Create the hooks to handle shared library load and unload
325 solib_create_inferior_hook (from_tty
);
327 if (current_program_space
->solib_add_generation
== solib_add_generation
)
329 /* The platform-specific hook should load initial shared libraries,
330 but didn't. FROM_TTY will be incorrectly 0 but such solib
331 targets should be fixed anyway. Call it only after the solib
332 target has been initialized by solib_create_inferior_hook. */
335 warning (_("platform-specific solib_create_inferior_hook did "
336 "not load initial shared libraries."));
338 /* If the solist is global across processes, there's no need to
340 if (!gdbarch_has_global_solist (target_gdbarch ()))
341 solib_add (NULL
, 0, auto_solib_add
);
345 /* If the user sets watchpoints before execution having started,
346 then she gets software watchpoints, because GDB can't know which
347 target will end up being pushed, or if it supports hardware
348 watchpoints or not. breakpoint_re_set takes care of promoting
349 watchpoints to hardware watchpoints if possible, however, if this
350 new inferior doesn't load shared libraries or we don't pull in
351 symbols from any other source on this target/arch,
352 breakpoint_re_set is never called. Call it now so that software
353 watchpoints get a chance to be promoted to hardware watchpoints
354 if the now pushed target supports hardware watchpoints. */
355 breakpoint_re_set ();
357 gdb::observers::inferior_created
.notify (current_inferior ());
360 /* Kill the inferior if already running. This function is designed
361 to be called when we are about to start the execution of the program
362 from the beginning. Ask the user to confirm that he wants to restart
363 the program being debugged when FROM_TTY is non-null. */
366 kill_if_already_running (int from_tty
)
368 if (inferior_ptid
!= null_ptid
&& target_has_execution ())
370 /* Bail out before killing the program if we will not be able to
372 target_require_runnable ();
375 && !query (_("The program being debugged has been started already.\n\
376 Start it from the beginning? ")))
377 error (_("Program not restarted."));
382 /* See inferior.h. */
385 prepare_execution_command (struct target_ops
*target
, int background
)
387 /* If we get a request for running in the bg but the target
388 doesn't support it, error out. */
389 if (background
&& !target
->can_async_p ())
390 error (_("Asynchronous execution not supported on this target."));
394 /* If we get a request for running in the fg, then we need to
395 simulate synchronous (fg) execution. Note no cleanup is
396 necessary for this. stdin is re-enabled whenever an error
397 reaches the top level. */
398 all_uis_on_sync_execution_starting ();
402 /* Determine how the new inferior will behave. */
406 /* Run program without any explicit stop during startup. */
409 /* Stop at the beginning of the program's main function. */
412 /* Stop at the first instruction of the program. */
413 RUN_STOP_AT_FIRST_INSN
416 /* Implement the "run" command. Force a stop during program start if
417 requested by RUN_HOW. */
420 run_command_1 (const char *args
, int from_tty
, enum run_how run_how
)
422 const char *exec_file
;
423 struct ui_out
*uiout
= current_uiout
;
424 struct target_ops
*run_target
;
429 scoped_disable_commit_resumed
disable_commit_resumed ("running");
431 kill_if_already_running (from_tty
);
433 init_wait_for_inferior ();
434 clear_breakpoint_hit_counts ();
436 /* Clean up any leftovers from other runs. Some other things from
437 this function should probably be moved into target_pre_inferior. */
438 target_pre_inferior (from_tty
);
440 /* The comment here used to read, "The exec file is re-read every
441 time we do a generic_mourn_inferior, so we just have to worry
442 about the symbol file." The `generic_mourn_inferior' function
443 gets called whenever the program exits. However, suppose the
444 program exits, and *then* the executable file changes? We need
445 to check again here. Since reopen_exec_file doesn't do anything
446 if the timestamp hasn't changed, I don't see the harm. */
450 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
451 args
= stripped
.get ();
453 /* Do validation and preparation before possibly changing anything
456 run_target
= find_run_target ();
458 prepare_execution_command (run_target
, async_exec
);
460 if (non_stop
&& !run_target
->supports_non_stop ())
461 error (_("The target does not support running in non-stop mode."));
463 /* Done. Can now set breakpoints, change inferior args, etc. */
465 /* Insert temporary breakpoint in main function if requested. */
466 if (run_how
== RUN_STOP_AT_MAIN
)
468 std::string arg
= string_printf ("-qualified %s", main_name ());
469 tbreak_command (arg
.c_str (), 0);
472 exec_file
= get_exec_file (0);
474 /* We keep symbols from add-symbol-file, on the grounds that the
475 user might want to add some symbols before running the program
476 (right?). But sometimes (dynamic loading where the user manually
477 introduces the new symbols with add-symbol-file), the code which
478 the symbols describe does not persist between runs. Currently
479 the user has to manually nuke all symbols between runs if they
480 want them to go away (PR 2207). This is probably reasonable. */
482 /* If there were other args, beside '&', process them. */
484 set_inferior_args (args
);
488 uiout
->field_string (NULL
, "Starting program");
491 uiout
->field_string ("execfile", exec_file
);
493 /* We call get_inferior_args() because we might need to compute
495 uiout
->field_string ("infargs", get_inferior_args ());
500 /* We call get_inferior_args() because we might need to compute
502 run_target
->create_inferior (exec_file
,
503 std::string (get_inferior_args ()),
504 current_inferior ()->environment
.envp (),
506 /* to_create_inferior should push the target, so after this point we
507 shouldn't refer to run_target again. */
510 /* We're starting off a new process. When we get out of here, in
511 non-stop mode, finish the state of all threads of that process,
512 but leave other threads alone, as they may be stopped in internal
513 events --- the frontend shouldn't see them as stopped. In
514 all-stop, always finish the state of all threads, as we may be
515 resuming more than just the new process. */
516 process_stratum_target
*finish_target
;
520 finish_target
= current_inferior ()->process_target ();
521 finish_ptid
= ptid_t (current_inferior ()->pid
);
525 finish_target
= nullptr;
526 finish_ptid
= minus_one_ptid
;
528 scoped_finish_thread_state
finish_state (finish_target
, finish_ptid
);
530 /* Pass zero for FROM_TTY, because at this point the "run" command
531 has done its thing; now we are setting up the running program. */
532 post_create_inferior (0);
534 /* Queue a pending event so that the program stops immediately. */
535 if (run_how
== RUN_STOP_AT_FIRST_INSN
)
537 thread_info
*thr
= inferior_thread ();
538 target_waitstatus ws
;
539 ws
.kind
= TARGET_WAITKIND_STOPPED
;
540 ws
.value
.sig
= GDB_SIGNAL_0
;
541 thr
->set_pending_waitstatus (ws
);
544 /* Start the target running. Do not use -1 continuation as it would skip
545 breakpoint right at the entry point. */
546 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0
);
548 /* Since there was no error, there's no need to finish the thread
550 finish_state
.release ();
552 disable_commit_resumed
.reset_and_commit ();
556 run_command (const char *args
, int from_tty
)
558 run_command_1 (args
, from_tty
, RUN_NORMAL
);
561 /* Start the execution of the program up until the beginning of the main
565 start_command (const char *args
, int from_tty
)
567 /* Some languages such as Ada need to search inside the program
568 minimal symbols for the location where to put the temporary
569 breakpoint before starting. */
570 if (!have_minimal_symbols ())
571 error (_("No symbol table loaded. Use the \"file\" command."));
573 /* Run the program until reaching the main procedure... */
574 run_command_1 (args
, from_tty
, RUN_STOP_AT_MAIN
);
577 /* Start the execution of the program stopping at the first
581 starti_command (const char *args
, int from_tty
)
583 run_command_1 (args
, from_tty
, RUN_STOP_AT_FIRST_INSN
);
587 proceed_thread_callback (struct thread_info
*thread
, void *arg
)
589 /* We go through all threads individually instead of compressing
590 into a single target `resume_all' request, because some threads
591 may be stopped in internal breakpoints/events, or stopped waiting
592 for its turn in the displaced stepping queue (that is, they are
593 running && !executing). The target side has no idea about why
594 the thread is stopped, so a `resume_all' command would resume too
595 much. If/when GDB gains a way to tell the target `hold this
596 thread stopped until I say otherwise', then we can optimize
598 if (thread
->state
!= THREAD_STOPPED
)
601 if (!thread
->inf
->has_execution ())
604 switch_to_thread (thread
);
605 clear_proceed_status (0);
606 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
611 ensure_valid_thread (void)
613 if (inferior_ptid
== null_ptid
614 || inferior_thread ()->state
== THREAD_EXITED
)
615 error (_("Cannot execute this command without a live selected thread."));
618 /* If the user is looking at trace frames, any resumption of execution
619 is likely to mix up recorded and live target data. So simply
620 disallow those commands. */
623 ensure_not_tfind_mode (void)
625 if (get_traceframe_number () >= 0)
626 error (_("Cannot execute this command while looking at trace frames."));
629 /* Throw an error indicating the current thread is running. */
632 error_is_running (void)
634 error (_("Cannot execute this command while "
635 "the selected thread is running."));
638 /* Calls error_is_running if the current thread is running. */
641 ensure_not_running (void)
643 if (inferior_thread ()->state
== THREAD_RUNNING
)
648 continue_1 (int all_threads
)
651 ensure_not_tfind_mode ();
653 if (non_stop
&& all_threads
)
655 /* Don't error out if the current thread is running, because
656 there may be other stopped threads. */
658 /* Backup current thread and selected frame and restore on scope
660 scoped_restore_current_thread restore_thread
;
662 iterate_over_threads (proceed_thread_callback
, NULL
);
664 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
666 /* If all threads in the target were already running,
667 proceed_thread_callback ends up never calling proceed,
668 and so nothing calls this to put the inferior's terminal
669 settings in effect and remove stdin from the event loop,
670 which we must when running a foreground command. E.g.:
674 <all threads are running now>
677 <no thread was resumed, but the inferior now owns the terminal>
679 target_terminal::inferior ();
684 ensure_valid_thread ();
685 ensure_not_running ();
686 clear_proceed_status (0);
687 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
691 /* continue [-a] [proceed-count] [&] */
694 continue_command (const char *args
, int from_tty
)
697 bool all_threads_p
= false;
701 /* Find out whether we must run in the background. */
702 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
703 args
= stripped
.get ();
707 if (startswith (args
, "-a"))
709 all_threads_p
= true;
710 args
+= sizeof ("-a") - 1;
716 if (!non_stop
&& all_threads_p
)
717 error (_("`-a' is meaningless in all-stop mode."));
719 if (args
!= NULL
&& all_threads_p
)
720 error (_("Can't resume all threads and specify "
721 "proceed count simultaneously."));
723 /* If we have an argument left, set proceed count of breakpoint we
730 struct thread_info
*tp
;
733 tp
= inferior_thread ();
736 process_stratum_target
*last_target
;
739 get_last_target_status (&last_target
, &last_ptid
, nullptr);
740 tp
= find_thread_ptid (last_target
, last_ptid
);
743 bs
= tp
->control
.stop_bpstat
;
745 while ((stat
= bpstat_num (&bs
, &num
)) != 0)
748 set_ignore_count (num
,
749 parse_and_eval_long (args
) - 1,
751 /* set_ignore_count prints a message ending with a period.
752 So print two spaces before "Continuing.". */
754 printf_filtered (" ");
758 if (!stopped
&& from_tty
)
761 ("Not stopped at any breakpoint; argument ignored.\n");
766 ensure_not_tfind_mode ();
768 if (!non_stop
|| !all_threads_p
)
770 ensure_valid_thread ();
771 ensure_not_running ();
774 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
777 printf_filtered (_("Continuing.\n"));
779 continue_1 (all_threads_p
);
782 /* Record in TP the starting point of a "step" or "next" command. */
785 set_step_frame (thread_info
*tp
)
787 /* This can be removed once this function no longer implicitly relies on the
788 inferior_ptid value. */
789 gdb_assert (inferior_ptid
== tp
->ptid
);
791 frame_info
*frame
= get_current_frame ();
793 symtab_and_line sal
= find_frame_sal (frame
);
794 set_step_info (tp
, frame
, sal
);
796 CORE_ADDR pc
= get_frame_pc (frame
);
797 tp
->control
.step_start_function
= find_pc_function (pc
);
800 /* Step until outside of current statement. */
803 step_command (const char *count_string
, int from_tty
)
805 step_1 (0, 0, count_string
);
808 /* Likewise, but skip over subroutine calls as if single instructions. */
811 next_command (const char *count_string
, int from_tty
)
813 step_1 (1, 0, count_string
);
816 /* Likewise, but step only one instruction. */
819 stepi_command (const char *count_string
, int from_tty
)
821 step_1 (0, 1, count_string
);
825 nexti_command (const char *count_string
, int from_tty
)
827 step_1 (1, 1, count_string
);
830 /* Data for the FSM that manages the step/next/stepi/nexti
833 struct step_command_fsm
: public thread_fsm
835 /* How many steps left in a "step N"-like command. */
838 /* If true, this is a next/nexti, otherwise a step/stepi. */
839 int skip_subroutines
;
841 /* If true, this is a stepi/nexti, otherwise a step/step. */
844 explicit step_command_fsm (struct interp
*cmd_interp
)
845 : thread_fsm (cmd_interp
)
849 void clean_up (struct thread_info
*thread
) override
;
850 bool should_stop (struct thread_info
*thread
) override
;
851 enum async_reply_reason
do_async_reply_reason () override
;
854 /* Prepare for a step/next/etc. command. Any target resource
855 allocated here is undone in the FSM's clean_up method. */
858 step_command_fsm_prepare (struct step_command_fsm
*sm
,
859 int skip_subroutines
, int single_inst
,
860 int count
, struct thread_info
*thread
)
862 sm
->skip_subroutines
= skip_subroutines
;
863 sm
->single_inst
= single_inst
;
866 /* Leave the si command alone. */
867 if (!sm
->single_inst
|| sm
->skip_subroutines
)
868 set_longjmp_breakpoint (thread
, get_frame_id (get_current_frame ()));
870 thread
->control
.stepping_command
= 1;
873 static int prepare_one_step (thread_info
*, struct step_command_fsm
*sm
);
876 step_1 (int skip_subroutines
, int single_inst
, const char *count_string
)
880 struct thread_info
*thr
;
881 struct step_command_fsm
*step_sm
;
884 ensure_not_tfind_mode ();
885 ensure_valid_thread ();
886 ensure_not_running ();
888 gdb::unique_xmalloc_ptr
<char> stripped
889 = strip_bg_char (count_string
, &async_exec
);
890 count_string
= stripped
.get ();
892 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
894 count
= count_string
? parse_and_eval_long (count_string
) : 1;
896 clear_proceed_status (1);
898 /* Setup the execution command state machine to handle all the COUNT
900 thr
= inferior_thread ();
901 step_sm
= new step_command_fsm (command_interp ());
902 thr
->thread_fsm
= step_sm
;
904 step_command_fsm_prepare (step_sm
, skip_subroutines
,
905 single_inst
, count
, thr
);
907 /* Do only one step for now, before returning control to the event
908 loop. Let the continuation figure out how many other steps we
909 need to do, and handle them one at the time, through
911 if (!prepare_one_step (thr
, step_sm
))
912 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
917 /* Stepped into an inline frame. Pretend that we've
919 thr
->thread_fsm
->clean_up (thr
);
920 proceeded
= normal_stop ();
922 inferior_event_handler (INF_EXEC_COMPLETE
);
923 all_uis_check_sync_execution_done ();
927 /* Implementation of the 'should_stop' FSM method for stepping
928 commands. Called after we are done with one step operation, to
929 check whether we need to step again, before we print the prompt and
930 return control to the user. If count is > 1, returns false, as we
931 will need to keep going. */
934 step_command_fsm::should_stop (struct thread_info
*tp
)
936 if (tp
->control
.stop_step
)
938 /* There are more steps to make, and we did stop due to
939 ending a stepping range. Do another step. */
941 return prepare_one_step (tp
, this);
949 /* Implementation of the 'clean_up' FSM method for stepping commands. */
952 step_command_fsm::clean_up (struct thread_info
*thread
)
954 if (!single_inst
|| skip_subroutines
)
955 delete_longjmp_breakpoint (thread
->global_num
);
958 /* Implementation of the 'async_reply_reason' FSM method for stepping
961 enum async_reply_reason
962 step_command_fsm::do_async_reply_reason ()
964 return EXEC_ASYNC_END_STEPPING_RANGE
;
967 /* Prepare for one step in "step N". The actual target resumption is
968 done by the caller. Return true if we're done and should thus
969 report a stop to the user. Returns false if the target needs to be
973 prepare_one_step (thread_info
*tp
, struct step_command_fsm
*sm
)
975 /* This can be removed once this function no longer implicitly relies on the
976 inferior_ptid value. */
977 gdb_assert (inferior_ptid
== tp
->ptid
);
981 struct frame_info
*frame
= get_current_frame ();
985 if (!sm
->single_inst
)
989 /* Step at an inlined function behaves like "down". */
990 if (!sm
->skip_subroutines
991 && inline_skipped_frames (tp
))
994 const char *fn
= NULL
;
998 /* Pretend that we've ran. */
999 resume_ptid
= user_visible_resume_ptid (1);
1000 set_running (tp
->inf
->process_target (), resume_ptid
, true);
1002 step_into_inline_frame (tp
);
1004 frame
= get_current_frame ();
1005 sal
= find_frame_sal (frame
);
1006 sym
= get_frame_function (frame
);
1009 fn
= sym
->print_name ();
1012 || !function_name_is_marked_for_skip (fn
, sal
))
1015 return prepare_one_step (tp
, sm
);
1019 pc
= get_frame_pc (frame
);
1020 find_pc_line_pc_range (pc
,
1021 &tp
->control
.step_range_start
,
1022 &tp
->control
.step_range_end
);
1024 /* There's a problem in gcc (PR gcc/98780) that causes missing line
1025 table entries, which results in a too large stepping range.
1026 Use inlined_subroutine info to make the range more narrow. */
1027 if (inline_skipped_frames (tp
) > 0)
1029 symbol
*sym
= inline_skipped_symbol (tp
);
1030 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1032 const block
*block
= SYMBOL_BLOCK_VALUE (sym
);
1033 if (BLOCK_END (block
) < tp
->control
.step_range_end
)
1034 tp
->control
.step_range_end
= BLOCK_END (block
);
1038 tp
->control
.may_range_step
= 1;
1040 /* If we have no line info, switch to stepi mode. */
1041 if (tp
->control
.step_range_end
== 0 && step_stop_if_no_debug
)
1043 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1044 tp
->control
.may_range_step
= 0;
1046 else if (tp
->control
.step_range_end
== 0)
1050 if (find_pc_partial_function (pc
, &name
,
1051 &tp
->control
.step_range_start
,
1052 &tp
->control
.step_range_end
) == 0)
1053 error (_("Cannot find bounds of current function"));
1055 target_terminal::ours_for_output ();
1056 printf_filtered (_("Single stepping until exit from function %s,"
1057 "\nwhich has no line number information.\n"),
1063 /* Say we are stepping, but stop after one insn whatever it does. */
1064 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1065 if (!sm
->skip_subroutines
)
1067 Don't step over function calls, not even to functions lacking
1069 tp
->control
.step_over_calls
= STEP_OVER_NONE
;
1072 if (sm
->skip_subroutines
)
1073 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1079 sm
->set_finished ();
1084 /* Continue program at specified address. */
1087 jump_command (const char *arg
, int from_tty
)
1089 struct gdbarch
*gdbarch
= get_current_arch ();
1096 ensure_not_tfind_mode ();
1097 ensure_valid_thread ();
1098 ensure_not_running ();
1100 /* Find out whether we must run in the background. */
1101 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1102 arg
= stripped
.get ();
1104 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1107 error_no_arg (_("starting address"));
1109 std::vector
<symtab_and_line
> sals
1110 = decode_line_with_last_displayed (arg
, DECODE_LINE_FUNFIRSTLINE
);
1111 if (sals
.size () != 1)
1112 error (_("Unreasonable jump request"));
1114 symtab_and_line
&sal
= sals
[0];
1116 if (sal
.symtab
== 0 && sal
.pc
== 0)
1117 error (_("No source file has been specified."));
1119 resolve_sal_pc (&sal
); /* May error out. */
1121 /* See if we are trying to jump to another function. */
1122 fn
= get_frame_function (get_current_frame ());
1123 sfn
= find_pc_function (sal
.pc
);
1124 if (fn
!= NULL
&& sfn
!= fn
)
1126 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal
.line
,
1129 error (_("Not confirmed."));
1136 struct obj_section
*section
;
1138 fixup_symbol_section (sfn
, 0);
1139 section
= sfn
->obj_section (symbol_objfile (sfn
));
1140 if (section_is_overlay (section
)
1141 && !section_is_mapped (section
))
1143 if (!query (_("WARNING!!! Destination is in "
1144 "unmapped overlay! Jump anyway? ")))
1146 error (_("Not confirmed."));
1156 printf_filtered (_("Continuing at "));
1157 fputs_filtered (paddress (gdbarch
, addr
), gdb_stdout
);
1158 printf_filtered (".\n");
1161 clear_proceed_status (0);
1162 proceed (addr
, GDB_SIGNAL_0
);
1165 /* Continue program giving it specified signal. */
1168 signal_command (const char *signum_exp
, int from_tty
)
1170 enum gdb_signal oursig
;
1173 dont_repeat (); /* Too dangerous. */
1175 ensure_not_tfind_mode ();
1176 ensure_valid_thread ();
1177 ensure_not_running ();
1179 /* Find out whether we must run in the background. */
1180 gdb::unique_xmalloc_ptr
<char> stripped
1181 = strip_bg_char (signum_exp
, &async_exec
);
1182 signum_exp
= stripped
.get ();
1184 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1187 error_no_arg (_("signal number"));
1189 /* It would be even slicker to make signal names be valid expressions,
1190 (the type could be "enum $signal" or some such), then the user could
1191 assign them to convenience variables. */
1192 oursig
= gdb_signal_from_name (signum_exp
);
1194 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1196 /* No, try numeric. */
1197 int num
= parse_and_eval_long (signum_exp
);
1200 oursig
= GDB_SIGNAL_0
;
1202 oursig
= gdb_signal_from_command (num
);
1205 /* Look for threads other than the current that this command ends up
1206 resuming too (due to schedlock off), and warn if they'll get a
1207 signal delivered. "signal 0" is used to suppress a previous
1208 signal, but if the current thread is no longer the one that got
1209 the signal, then the user is potentially suppressing the signal
1210 of the wrong thread. */
1213 int must_confirm
= 0;
1215 /* This indicates what will be resumed. Either a single thread,
1216 a whole process, or all threads of all processes. */
1217 ptid_t resume_ptid
= user_visible_resume_ptid (0);
1218 process_stratum_target
*resume_target
1219 = user_visible_resume_target (resume_ptid
);
1221 thread_info
*current
= inferior_thread ();
1223 for (thread_info
*tp
: all_non_exited_threads (resume_target
, resume_ptid
))
1228 if (tp
->stop_signal () != GDB_SIGNAL_0
1229 && signal_pass_state (tp
->stop_signal ()))
1232 printf_unfiltered (_("Note:\n"));
1233 printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"),
1234 print_thread_id (tp
),
1235 gdb_signal_to_name (tp
->stop_signal ()),
1236 gdb_signal_to_string (tp
->stop_signal ()));
1242 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1243 "still deliver the signals noted above to their respective threads.\n"
1244 "Continue anyway? "),
1245 print_thread_id (inferior_thread ())))
1246 error (_("Not confirmed."));
1251 if (oursig
== GDB_SIGNAL_0
)
1252 printf_filtered (_("Continuing with no signal.\n"));
1254 printf_filtered (_("Continuing with signal %s.\n"),
1255 gdb_signal_to_name (oursig
));
1258 clear_proceed_status (0);
1259 proceed ((CORE_ADDR
) -1, oursig
);
1262 /* Queue a signal to be delivered to the current thread. */
1265 queue_signal_command (const char *signum_exp
, int from_tty
)
1267 enum gdb_signal oursig
;
1268 struct thread_info
*tp
;
1271 ensure_not_tfind_mode ();
1272 ensure_valid_thread ();
1273 ensure_not_running ();
1275 if (signum_exp
== NULL
)
1276 error_no_arg (_("signal number"));
1278 /* It would be even slicker to make signal names be valid expressions,
1279 (the type could be "enum $signal" or some such), then the user could
1280 assign them to convenience variables. */
1281 oursig
= gdb_signal_from_name (signum_exp
);
1283 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1285 /* No, try numeric. */
1286 int num
= parse_and_eval_long (signum_exp
);
1289 oursig
= GDB_SIGNAL_0
;
1291 oursig
= gdb_signal_from_command (num
);
1294 if (oursig
!= GDB_SIGNAL_0
1295 && !signal_pass_state (oursig
))
1296 error (_("Signal handling set to not pass this signal to the program."));
1298 tp
= inferior_thread ();
1299 tp
->set_stop_signal (oursig
);
1302 /* Data for the FSM that manages the until (with no argument)
1305 struct until_next_fsm
: public thread_fsm
1307 /* The thread that as current when the command was executed. */
1310 until_next_fsm (struct interp
*cmd_interp
, int thread
)
1311 : thread_fsm (cmd_interp
),
1316 bool should_stop (struct thread_info
*thread
) override
;
1317 void clean_up (struct thread_info
*thread
) override
;
1318 enum async_reply_reason
do_async_reply_reason () override
;
1321 /* Implementation of the 'should_stop' FSM method for the until (with
1325 until_next_fsm::should_stop (struct thread_info
*tp
)
1327 if (tp
->control
.stop_step
)
1333 /* Implementation of the 'clean_up' FSM method for the until (with no
1337 until_next_fsm::clean_up (struct thread_info
*thread
)
1339 delete_longjmp_breakpoint (thread
->global_num
);
1342 /* Implementation of the 'async_reply_reason' FSM method for the until
1343 (with no arg) command. */
1345 enum async_reply_reason
1346 until_next_fsm::do_async_reply_reason ()
1348 return EXEC_ASYNC_END_STEPPING_RANGE
;
1351 /* Proceed until we reach a different source line with pc greater than
1352 our current one or exit the function. We skip calls in both cases.
1354 Note that eventually this command should probably be changed so
1355 that only source lines are printed out when we hit the breakpoint
1356 we set. This may involve changes to wait_for_inferior and the
1357 proceed status code. */
1360 until_next_command (int from_tty
)
1362 struct frame_info
*frame
;
1364 struct symbol
*func
;
1365 struct symtab_and_line sal
;
1366 struct thread_info
*tp
= inferior_thread ();
1367 int thread
= tp
->global_num
;
1368 struct until_next_fsm
*sm
;
1370 clear_proceed_status (0);
1371 set_step_frame (tp
);
1373 frame
= get_current_frame ();
1375 /* Step until either exited from this function or greater
1376 than the current line (if in symbolic section) or pc (if
1379 pc
= get_frame_pc (frame
);
1380 func
= find_pc_function (pc
);
1384 struct bound_minimal_symbol msymbol
= lookup_minimal_symbol_by_pc (pc
);
1386 if (msymbol
.minsym
== NULL
)
1387 error (_("Execution is not within a known function."));
1389 tp
->control
.step_range_start
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1390 /* The upper-bound of step_range is exclusive. In order to make PC
1391 within the range, set the step_range_end with PC + 1. */
1392 tp
->control
.step_range_end
= pc
+ 1;
1396 sal
= find_pc_line (pc
, 0);
1398 tp
->control
.step_range_start
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func
));
1399 tp
->control
.step_range_end
= sal
.end
;
1401 tp
->control
.may_range_step
= 1;
1403 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1405 set_longjmp_breakpoint (tp
, get_frame_id (frame
));
1406 delete_longjmp_breakpoint_cleanup
lj_deleter (thread
);
1408 sm
= new until_next_fsm (command_interp (), tp
->global_num
);
1409 tp
->thread_fsm
= sm
;
1410 lj_deleter
.release ();
1412 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1416 until_command (const char *arg
, int from_tty
)
1421 ensure_not_tfind_mode ();
1422 ensure_valid_thread ();
1423 ensure_not_running ();
1425 /* Find out whether we must run in the background. */
1426 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1427 arg
= stripped
.get ();
1429 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1432 until_break_command (arg
, from_tty
, 0);
1434 until_next_command (from_tty
);
1438 advance_command (const char *arg
, int from_tty
)
1443 ensure_not_tfind_mode ();
1444 ensure_valid_thread ();
1445 ensure_not_running ();
1448 error_no_arg (_("a location"));
1450 /* Find out whether we must run in the background. */
1451 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1452 arg
= stripped
.get ();
1454 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1456 until_break_command (arg
, from_tty
, 1);
1459 /* Return the value of the result of a function at the end of a 'finish'
1460 command/BP. DTOR_DATA (if not NULL) can represent inferior registers
1461 right after an inferior call has finished. */
1464 get_return_value (struct value
*function
, struct type
*value_type
)
1466 regcache
*stop_regs
= get_current_regcache ();
1467 struct gdbarch
*gdbarch
= stop_regs
->arch ();
1468 struct value
*value
;
1470 value_type
= check_typedef (value_type
);
1471 gdb_assert (value_type
->code () != TYPE_CODE_VOID
);
1473 /* FIXME: 2003-09-27: When returning from a nested inferior function
1474 call, it's possible (with no help from the architecture vector)
1475 to locate and return/print a "struct return" value. This is just
1476 a more complicated case of what is already being done in the
1477 inferior function call code. In fact, when inferior function
1478 calls are made async, this will likely be made the norm. */
1480 switch (gdbarch_return_value (gdbarch
, function
, value_type
,
1483 case RETURN_VALUE_REGISTER_CONVENTION
:
1484 case RETURN_VALUE_ABI_RETURNS_ADDRESS
:
1485 case RETURN_VALUE_ABI_PRESERVES_ADDRESS
:
1486 value
= allocate_value (value_type
);
1487 gdbarch_return_value (gdbarch
, function
, value_type
, stop_regs
,
1488 value_contents_raw (value
), NULL
);
1490 case RETURN_VALUE_STRUCT_CONVENTION
:
1494 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1500 /* The captured function return value/type and its position in the
1503 struct return_value_info
1505 /* The captured return value. May be NULL if we weren't able to
1506 retrieve it. See get_return_value. */
1507 struct value
*value
;
1509 /* The return type. In some cases, we'll not be able extract the
1510 return value, but we always know the type. */
1513 /* If we captured a value, this is the value history index. */
1514 int value_history_index
;
1517 /* Helper for print_return_value. */
1520 print_return_value_1 (struct ui_out
*uiout
, struct return_value_info
*rv
)
1522 if (rv
->value
!= NULL
)
1524 struct value_print_options opts
;
1527 uiout
->text ("Value returned is ");
1528 uiout
->field_fmt ("gdb-result-var", "$%d",
1529 rv
->value_history_index
);
1530 uiout
->text (" = ");
1531 get_user_print_options (&opts
);
1533 if (opts
.finish_print
)
1536 value_print (rv
->value
, &stb
, &opts
);
1537 uiout
->field_stream ("return-value", stb
);
1540 uiout
->field_string ("return-value", _("<not displayed>"),
1541 metadata_style
.style ());
1546 std::string type_name
= type_to_string (rv
->type
);
1547 uiout
->text ("Value returned has type: ");
1548 uiout
->field_string ("return-type", type_name
);
1550 uiout
->text (" Cannot determine contents\n");
1554 /* Print the result of a function at the end of a 'finish' command.
1555 RV points at an object representing the captured return value/type
1556 and its position in the value history. */
1559 print_return_value (struct ui_out
*uiout
, struct return_value_info
*rv
)
1561 if (rv
->type
== NULL
1562 || check_typedef (rv
->type
)->code () == TYPE_CODE_VOID
)
1567 /* print_return_value_1 can throw an exception in some
1568 circumstances. We need to catch this so that we still
1569 delete the breakpoint. */
1570 print_return_value_1 (uiout
, rv
);
1572 catch (const gdb_exception
&ex
)
1574 exception_print (gdb_stdout
, ex
);
1578 /* Data for the FSM that manages the finish command. */
1580 struct finish_command_fsm
: public thread_fsm
1582 /* The momentary breakpoint set at the function's return address in
1584 breakpoint_up breakpoint
;
1586 /* The function that we're stepping out of. */
1587 struct symbol
*function
= nullptr;
1589 /* If the FSM finishes successfully, this stores the function's
1591 struct return_value_info return_value_info
{};
1593 explicit finish_command_fsm (struct interp
*cmd_interp
)
1594 : thread_fsm (cmd_interp
)
1598 bool should_stop (struct thread_info
*thread
) override
;
1599 void clean_up (struct thread_info
*thread
) override
;
1600 struct return_value_info
*return_value () override
;
1601 enum async_reply_reason
do_async_reply_reason () override
;
1604 /* Implementation of the 'should_stop' FSM method for the finish
1605 commands. Detects whether the thread stepped out of the function
1606 successfully, and if so, captures the function's return value and
1607 marks the FSM finished. */
1610 finish_command_fsm::should_stop (struct thread_info
*tp
)
1612 struct return_value_info
*rv
= &return_value_info
;
1614 if (function
!= NULL
1615 && bpstat_find_breakpoint (tp
->control
.stop_bpstat
,
1616 breakpoint
.get ()) != NULL
)
1621 rv
->type
= TYPE_TARGET_TYPE (SYMBOL_TYPE (function
));
1622 if (rv
->type
== NULL
)
1623 internal_error (__FILE__
, __LINE__
,
1624 _("finish_command: function has no target type"));
1626 if (check_typedef (rv
->type
)->code () != TYPE_CODE_VOID
)
1630 func
= read_var_value (function
, NULL
, get_current_frame ());
1631 rv
->value
= get_return_value (func
, rv
->type
);
1632 if (rv
->value
!= NULL
)
1633 rv
->value_history_index
= record_latest_value (rv
->value
);
1636 else if (tp
->control
.stop_step
)
1638 /* Finishing from an inline frame, or reverse finishing. In
1639 either case, there's no way to retrieve the return value. */
1646 /* Implementation of the 'clean_up' FSM method for the finish
1650 finish_command_fsm::clean_up (struct thread_info
*thread
)
1652 breakpoint
.reset ();
1653 delete_longjmp_breakpoint (thread
->global_num
);
1656 /* Implementation of the 'return_value' FSM method for the finish
1659 struct return_value_info
*
1660 finish_command_fsm::return_value ()
1662 return &return_value_info
;
1665 /* Implementation of the 'async_reply_reason' FSM method for the
1668 enum async_reply_reason
1669 finish_command_fsm::do_async_reply_reason ()
1671 if (execution_direction
== EXEC_REVERSE
)
1672 return EXEC_ASYNC_END_STEPPING_RANGE
;
1674 return EXEC_ASYNC_FUNCTION_FINISHED
;
1677 /* finish_backward -- helper function for finish_command. */
1680 finish_backward (struct finish_command_fsm
*sm
)
1682 struct symtab_and_line sal
;
1683 struct thread_info
*tp
= inferior_thread ();
1685 CORE_ADDR func_addr
;
1687 pc
= get_frame_pc (get_current_frame ());
1689 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
) == 0)
1690 error (_("Cannot find bounds of current function"));
1692 sal
= find_pc_line (func_addr
, 0);
1694 tp
->control
.proceed_to_finish
= 1;
1695 /* Special case: if we're sitting at the function entry point,
1696 then all we need to do is take a reverse singlestep. We
1697 don't need to set a breakpoint, and indeed it would do us
1700 Note that this can only happen at frame #0, since there's
1701 no way that a function up the stack can have a return address
1702 that's equal to its entry point. */
1706 struct frame_info
*frame
= get_selected_frame (NULL
);
1707 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1709 /* Set a step-resume at the function's entry point. Once that's
1710 hit, we'll do one more step backwards. */
1711 symtab_and_line sr_sal
;
1713 sr_sal
.pspace
= get_frame_program_space (frame
);
1714 insert_step_resume_breakpoint_at_sal (gdbarch
,
1715 sr_sal
, null_frame_id
);
1717 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1721 /* We're almost there -- we just need to back up by one more
1723 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1724 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1728 /* finish_forward -- helper function for finish_command. FRAME is the
1729 frame that called the function we're about to step out of. */
1732 finish_forward (struct finish_command_fsm
*sm
, struct frame_info
*frame
)
1734 struct frame_id frame_id
= get_frame_id (frame
);
1735 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1736 struct symtab_and_line sal
;
1737 struct thread_info
*tp
= inferior_thread ();
1739 sal
= find_pc_line (get_frame_pc (frame
), 0);
1740 sal
.pc
= get_frame_pc (frame
);
1742 sm
->breakpoint
= set_momentary_breakpoint (gdbarch
, sal
,
1743 get_stack_frame_id (frame
),
1746 /* set_momentary_breakpoint invalidates FRAME. */
1749 set_longjmp_breakpoint (tp
, frame_id
);
1751 /* We want to print return value, please... */
1752 tp
->control
.proceed_to_finish
= 1;
1754 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1757 /* Skip frames for "finish". */
1759 static struct frame_info
*
1760 skip_finish_frames (struct frame_info
*frame
)
1762 struct frame_info
*start
;
1768 frame
= skip_tailcall_frames (frame
);
1772 frame
= skip_unwritable_frames (frame
);
1776 while (start
!= frame
);
1781 /* "finish": Set a temporary breakpoint at the place the selected
1782 frame will return to, then continue. */
1785 finish_command (const char *arg
, int from_tty
)
1787 struct frame_info
*frame
;
1789 struct finish_command_fsm
*sm
;
1790 struct thread_info
*tp
;
1793 ensure_not_tfind_mode ();
1794 ensure_valid_thread ();
1795 ensure_not_running ();
1797 /* Find out whether we must run in the background. */
1798 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1799 arg
= stripped
.get ();
1801 prepare_execution_command (current_inferior ()->top_target (), async_exec
);
1804 error (_("The \"finish\" command does not take any arguments."));
1806 frame
= get_prev_frame (get_selected_frame (_("No selected frame.")));
1808 error (_("\"finish\" not meaningful in the outermost frame."));
1810 clear_proceed_status (0);
1812 tp
= inferior_thread ();
1814 sm
= new finish_command_fsm (command_interp ());
1816 tp
->thread_fsm
= sm
;
1818 /* Finishing from an inline frame is completely different. We don't
1819 try to show the "return value" - no way to locate it. */
1820 if (get_frame_type (get_selected_frame (_("No selected frame.")))
1823 /* Claim we are stepping in the calling frame. An empty step
1824 range means that we will stop once we aren't in a function
1825 called by that frame. We don't use the magic "1" value for
1826 step_range_end, because then infrun will think this is nexti,
1827 and not step over the rest of this inlined function call. */
1828 set_step_info (tp
, frame
, {});
1829 tp
->control
.step_range_start
= get_frame_pc (frame
);
1830 tp
->control
.step_range_end
= tp
->control
.step_range_start
;
1831 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1833 /* Print info on the selected frame, including level number but not
1837 printf_filtered (_("Run till exit from "));
1838 print_stack_frame (get_selected_frame (NULL
), 1, LOCATION
, 0);
1841 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1845 /* Find the function we will return from. */
1847 sm
->function
= find_pc_function (get_frame_pc (get_selected_frame (NULL
)));
1849 /* Print info on the selected frame, including level number but not
1853 if (execution_direction
== EXEC_REVERSE
)
1854 printf_filtered (_("Run back to call of "));
1857 if (sm
->function
!= NULL
&& TYPE_NO_RETURN (sm
->function
->type
)
1858 && !query (_("warning: Function %s does not return normally.\n"
1859 "Try to finish anyway? "),
1860 sm
->function
->print_name ()))
1861 error (_("Not confirmed."));
1862 printf_filtered (_("Run till exit from "));
1865 print_stack_frame (get_selected_frame (NULL
), 1, LOCATION
, 0);
1868 if (execution_direction
== EXEC_REVERSE
)
1869 finish_backward (sm
);
1872 frame
= skip_finish_frames (frame
);
1875 error (_("Cannot find the caller frame."));
1877 finish_forward (sm
, frame
);
1883 info_program_command (const char *args
, int from_tty
)
1888 process_stratum_target
*proc_target
;
1890 if (!target_has_execution ())
1892 printf_filtered (_("The program being debugged is not being run.\n"));
1898 ptid
= inferior_ptid
;
1899 proc_target
= current_inferior ()->process_target ();
1902 get_last_target_status (&proc_target
, &ptid
, nullptr);
1904 if (ptid
== null_ptid
|| ptid
== minus_one_ptid
)
1905 error (_("No selected thread."));
1907 thread_info
*tp
= find_thread_ptid (proc_target
, ptid
);
1909 if (tp
->state
== THREAD_EXITED
)
1910 error (_("Invalid selected thread."));
1911 else if (tp
->state
== THREAD_RUNNING
)
1912 error (_("Selected thread is running."));
1914 bs
= tp
->control
.stop_bpstat
;
1915 stat
= bpstat_num (&bs
, &num
);
1917 target_files_info ();
1918 printf_filtered (_("Program stopped at %s.\n"),
1919 paddress (target_gdbarch (), tp
->stop_pc ()));
1920 if (tp
->control
.stop_step
)
1921 printf_filtered (_("It stopped after being stepped.\n"));
1924 /* There may be several breakpoints in the same place, so this
1925 isn't as strange as it seems. */
1930 printf_filtered (_("It stopped at a breakpoint "
1931 "that has since been deleted.\n"));
1934 printf_filtered (_("It stopped at breakpoint %d.\n"), num
);
1935 stat
= bpstat_num (&bs
, &num
);
1938 else if (tp
->stop_signal () != GDB_SIGNAL_0
)
1940 printf_filtered (_("It stopped with signal %s, %s.\n"),
1941 gdb_signal_to_name (tp
->stop_signal ()),
1942 gdb_signal_to_string (tp
->stop_signal ()));
1947 printf_filtered (_("Type \"info stack\" or \"info "
1948 "registers\" for more information.\n"));
1953 environment_info (const char *var
, int from_tty
)
1957 const char *val
= current_inferior ()->environment
.get (var
);
1961 puts_filtered (var
);
1962 puts_filtered (" = ");
1963 puts_filtered (val
);
1964 puts_filtered ("\n");
1968 puts_filtered ("Environment variable \"");
1969 puts_filtered (var
);
1970 puts_filtered ("\" not defined.\n");
1975 char **envp
= current_inferior ()->environment
.envp ();
1977 for (int idx
= 0; envp
[idx
] != NULL
; ++idx
)
1979 puts_filtered (envp
[idx
]);
1980 puts_filtered ("\n");
1986 set_environment_command (const char *arg
, int from_tty
)
1988 const char *p
, *val
;
1992 error_no_arg (_("environment variable and value"));
1994 /* Find separation between variable name and value. */
1995 p
= (char *) strchr (arg
, '=');
1996 val
= (char *) strchr (arg
, ' ');
1998 if (p
!= 0 && val
!= 0)
2000 /* We have both a space and an equals. If the space is before the
2001 equals, walk forward over the spaces til we see a nonspace
2002 (possibly the equals). */
2007 /* Now if the = is after the char following the spaces,
2008 take the char following the spaces. */
2012 else if (val
!= 0 && p
== 0)
2016 error_no_arg (_("environment variable to set"));
2018 if (p
== 0 || p
[1] == 0)
2022 p
= arg
+ strlen (arg
); /* So that savestring below will work. */
2026 /* Not setting variable value to null. */
2028 while (*val
== ' ' || *val
== '\t')
2032 while (p
!= arg
&& (p
[-1] == ' ' || p
[-1] == '\t'))
2035 std::string
var (arg
, p
- arg
);
2038 printf_filtered (_("Setting environment variable "
2039 "\"%s\" to null value.\n"),
2041 current_inferior ()->environment
.set (var
.c_str (), "");
2044 current_inferior ()->environment
.set (var
.c_str (), val
);
2048 unset_environment_command (const char *var
, int from_tty
)
2052 /* If there is no argument, delete all environment variables.
2053 Ask for confirmation if reading from the terminal. */
2054 if (!from_tty
|| query (_("Delete all environment variables? ")))
2055 current_inferior ()->environment
.clear ();
2058 current_inferior ()->environment
.unset (var
);
2061 /* Handle the execution path (PATH variable). */
2063 static const char path_var_name
[] = "PATH";
2066 path_info (const char *args
, int from_tty
)
2068 puts_filtered ("Executable and object file path: ");
2069 puts_filtered (current_inferior ()->environment
.get (path_var_name
));
2070 puts_filtered ("\n");
2073 /* Add zero or more directories to the front of the execution path. */
2076 path_command (const char *dirname
, int from_tty
)
2082 env
= current_inferior ()->environment
.get (path_var_name
);
2083 /* Can be null if path is not set. */
2086 exec_path
= xstrdup (env
);
2087 mod_path (dirname
, &exec_path
);
2088 current_inferior ()->environment
.set (path_var_name
, exec_path
);
2091 path_info (NULL
, from_tty
);
2096 pad_to_column (string_file
&stream
, int col
)
2098 /* At least one space must be printed to separate columns. */
2100 const int size
= stream
.size ();
2102 stream
.puts (n_spaces (col
- size
));
2105 /* Print out the register NAME with value VAL, to FILE, in the default
2109 default_print_one_register_info (struct ui_file
*file
,
2113 struct type
*regtype
= value_type (val
);
2114 int print_raw_format
;
2115 string_file format_stream
;
2118 value_column_1
= 15,
2119 /* Give enough room for "0x", 16 hex digits and two spaces in
2120 preceding column. */
2121 value_column_2
= value_column_1
+ 2 + 16 + 2,
2124 format_stream
.puts (name
);
2125 pad_to_column (format_stream
, value_column_1
);
2127 print_raw_format
= (value_entirely_available (val
)
2128 && !value_optimized_out (val
));
2130 /* If virtual format is floating, print it that way, and in raw
2132 if (regtype
->code () == TYPE_CODE_FLT
2133 || regtype
->code () == TYPE_CODE_DECFLOAT
)
2135 struct value_print_options opts
;
2136 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
2137 enum bfd_endian byte_order
= type_byte_order (regtype
);
2139 get_user_print_options (&opts
);
2142 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2144 if (print_raw_format
)
2146 pad_to_column (format_stream
, value_column_2
);
2147 format_stream
.puts ("(raw ");
2148 print_hex_chars (&format_stream
, valaddr
, TYPE_LENGTH (regtype
),
2150 format_stream
.putc (')');
2155 struct value_print_options opts
;
2157 /* Print the register in hex. */
2158 get_formatted_print_options (&opts
, 'x');
2160 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2161 /* If not a vector register, print it also according to its
2163 if (print_raw_format
&& regtype
->is_vector () == 0)
2165 pad_to_column (format_stream
, value_column_2
);
2166 get_user_print_options (&opts
);
2168 common_val_print (val
, &format_stream
, 0, &opts
, current_language
);
2172 fputs_filtered (format_stream
.c_str (), file
);
2173 fprintf_filtered (file
, "\n");
2176 /* Print out the machine register regnum. If regnum is -1, print all
2177 registers (print_all == 1) or all non-float and non-vector
2178 registers (print_all == 0).
2180 For most machines, having all_registers_info() print the
2181 register(s) one per line is good enough. If a different format is
2182 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2183 regs), or there is an existing convention for showing all the
2184 registers, define the architecture method PRINT_REGISTERS_INFO to
2185 provide that format. */
2188 default_print_registers_info (struct gdbarch
*gdbarch
,
2189 struct ui_file
*file
,
2190 struct frame_info
*frame
,
2191 int regnum
, int print_all
)
2194 const int numregs
= gdbarch_num_cooked_regs (gdbarch
);
2196 for (i
= 0; i
< numregs
; i
++)
2198 /* Decide between printing all regs, non-float / vector regs, or
2204 if (!gdbarch_register_reggroup_p (gdbarch
, i
, all_reggroup
))
2209 if (!gdbarch_register_reggroup_p (gdbarch
, i
, general_reggroup
))
2219 /* If the register name is empty, it is undefined for this
2220 processor, so don't display anything. */
2221 if (gdbarch_register_name (gdbarch
, i
) == NULL
2222 || *(gdbarch_register_name (gdbarch
, i
)) == '\0')
2225 default_print_one_register_info (file
,
2226 gdbarch_register_name (gdbarch
, i
),
2227 value_of_register (i
, frame
));
2232 registers_info (const char *addr_exp
, int fpregs
)
2234 struct frame_info
*frame
;
2235 struct gdbarch
*gdbarch
;
2237 if (!target_has_registers ())
2238 error (_("The program has no registers now."));
2239 frame
= get_selected_frame (NULL
);
2240 gdbarch
= get_frame_arch (frame
);
2244 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2249 while (*addr_exp
!= '\0')
2254 /* Skip leading white space. */
2255 addr_exp
= skip_spaces (addr_exp
);
2257 /* Discard any leading ``$''. Check that there is something
2258 resembling a register following it. */
2259 if (addr_exp
[0] == '$')
2261 if (isspace ((*addr_exp
)) || (*addr_exp
) == '\0')
2262 error (_("Missing register name"));
2264 /* Find the start/end of this register name/num/group. */
2266 while ((*addr_exp
) != '\0' && !isspace ((*addr_exp
)))
2270 /* Figure out what we've found and display it. */
2272 /* A register name? */
2274 int regnum
= user_reg_map_name_to_regnum (gdbarch
, start
, end
- start
);
2278 /* User registers lie completely outside of the range of
2279 normal registers. Catch them early so that the target
2281 if (regnum
>= gdbarch_num_cooked_regs (gdbarch
))
2283 struct value
*regval
= value_of_user_reg (regnum
, frame
);
2284 const char *regname
= user_reg_map_regnum_to_name (gdbarch
,
2287 /* Print in the same fashion
2288 gdbarch_print_registers_info's default
2289 implementation prints. */
2290 default_print_one_register_info (gdb_stdout
,
2295 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2296 frame
, regnum
, fpregs
);
2301 /* A register group? */
2303 struct reggroup
*group
;
2305 for (group
= reggroup_next (gdbarch
, NULL
);
2307 group
= reggroup_next (gdbarch
, group
))
2309 /* Don't bother with a length check. Should the user
2310 enter a short register group name, go with the first
2311 group that matches. */
2312 if (strncmp (start
, reggroup_name (group
), end
- start
) == 0)
2320 regnum
< gdbarch_num_cooked_regs (gdbarch
);
2323 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
2324 gdbarch_print_registers_info (gdbarch
,
2332 /* Nothing matched. */
2333 error (_("Invalid register `%.*s'"), (int) (end
- start
), start
);
2338 info_all_registers_command (const char *addr_exp
, int from_tty
)
2340 registers_info (addr_exp
, 1);
2344 info_registers_command (const char *addr_exp
, int from_tty
)
2346 registers_info (addr_exp
, 0);
2350 print_vector_info (struct ui_file
*file
,
2351 struct frame_info
*frame
, const char *args
)
2353 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
2355 if (gdbarch_print_vector_info_p (gdbarch
))
2356 gdbarch_print_vector_info (gdbarch
, file
, frame
, args
);
2360 int printed_something
= 0;
2362 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2364 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, vector_reggroup
))
2366 printed_something
= 1;
2367 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2370 if (!printed_something
)
2371 fprintf_filtered (file
, "No vector information\n");
2376 info_vector_command (const char *args
, int from_tty
)
2378 if (!target_has_registers ())
2379 error (_("The program has no registers now."));
2381 print_vector_info (gdb_stdout
, get_selected_frame (NULL
), args
);
2384 /* Kill the inferior process. Make us have no inferior. */
2387 kill_command (const char *arg
, int from_tty
)
2389 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2390 It should be a distinct flag that indicates that a target is active, cuz
2391 some targets don't have processes! */
2393 if (inferior_ptid
== null_ptid
)
2394 error (_("The program is not being run."));
2395 if (!query (_("Kill the program being debugged? ")))
2396 error (_("Not confirmed."));
2398 int pid
= current_inferior ()->pid
;
2399 /* Save the pid as a string before killing the inferior, since that
2400 may unpush the current target, and we need the string after. */
2401 std::string pid_str
= target_pid_to_str (ptid_t (pid
));
2402 int infnum
= current_inferior ()->num
;
2406 if (print_inferior_events
)
2407 printf_unfiltered (_("[Inferior %d (%s) killed]\n"),
2408 infnum
, pid_str
.c_str ());
2410 bfd_cache_close_all ();
2413 /* Used in `attach&' command. Proceed threads of inferior INF iff
2414 they stopped due to debugger request, and when they did, they
2415 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2416 have been explicitly been told to stop. */
2419 proceed_after_attach (inferior
*inf
)
2421 /* Don't error out if the current thread is running, because
2422 there may be other stopped threads. */
2424 /* Backup current thread and selected frame. */
2425 scoped_restore_current_thread restore_thread
;
2427 for (thread_info
*thread
: inf
->non_exited_threads ())
2428 if (!thread
->executing
2429 && !thread
->stop_requested
2430 && thread
->stop_signal () == GDB_SIGNAL_0
)
2432 switch_to_thread (thread
);
2433 clear_proceed_status (0);
2434 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2438 /* See inferior.h. */
2441 setup_inferior (int from_tty
)
2443 struct inferior
*inferior
;
2445 inferior
= current_inferior ();
2446 inferior
->needs_setup
= 0;
2448 /* If no exec file is yet known, try to determine it from the
2450 if (get_exec_file (0) == NULL
)
2451 exec_file_locate_attach (inferior_ptid
.pid (), 1, from_tty
);
2454 reopen_exec_file ();
2458 /* Take any necessary post-attaching actions for this platform. */
2459 target_post_attach (inferior_ptid
.pid ());
2461 post_create_inferior (from_tty
);
2464 /* What to do after the first program stops after attaching. */
2465 enum attach_post_wait_mode
2467 /* Do nothing. Leaves threads as they are. */
2468 ATTACH_POST_WAIT_NOTHING
,
2470 /* Re-resume threads that are marked running. */
2471 ATTACH_POST_WAIT_RESUME
,
2473 /* Stop all threads. */
2474 ATTACH_POST_WAIT_STOP
,
2477 /* Called after we've attached to a process and we've seen it stop for
2478 the first time. Resume, stop, or don't touch the threads according
2482 attach_post_wait (int from_tty
, enum attach_post_wait_mode mode
)
2484 struct inferior
*inferior
;
2486 inferior
= current_inferior ();
2487 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
2489 if (inferior
->needs_setup
)
2490 setup_inferior (from_tty
);
2492 if (mode
== ATTACH_POST_WAIT_RESUME
)
2494 /* The user requested an `attach&', so be sure to leave threads
2495 that didn't get a signal running. */
2497 /* Immediately resume all suspended threads of this inferior,
2498 and this inferior only. This should have no effect on
2499 already running threads. If a thread has been stopped with a
2500 signal, leave it be. */
2502 proceed_after_attach (inferior
);
2505 if (inferior_thread ()->stop_signal () == GDB_SIGNAL_0
)
2507 clear_proceed_status (0);
2508 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2512 else if (mode
== ATTACH_POST_WAIT_STOP
)
2514 /* The user requested a plain `attach', so be sure to leave
2515 the inferior stopped. */
2517 /* At least the current thread is already stopped. */
2519 /* In all-stop, by definition, all threads have to be already
2520 stopped at this point. In non-stop, however, although the
2521 selected thread is stopped, others may still be executing.
2522 Be sure to explicitly stop all threads of the process. This
2523 should have no effect on already stopped threads. */
2525 target_stop (ptid_t (inferior
->pid
));
2526 else if (target_is_non_stop_p ())
2528 struct thread_info
*lowest
= inferior_thread ();
2530 stop_all_threads ();
2532 /* It's not defined which thread will report the attach
2533 stop. For consistency, always select the thread with
2534 lowest GDB number, which should be the main thread, if it
2536 for (thread_info
*thread
: current_inferior ()->non_exited_threads ())
2537 if (thread
->inf
->num
< lowest
->inf
->num
2538 || thread
->per_inf_num
< lowest
->per_inf_num
)
2541 switch_to_thread (lowest
);
2544 /* Tell the user/frontend where we're stopped. */
2546 if (deprecated_attach_hook
)
2547 deprecated_attach_hook ();
2551 /* "attach" command entry point. Takes a program started up outside
2552 of gdb and ``attaches'' to it. This stops it cold in its tracks
2553 and allows us to start debugging it. */
2556 attach_command (const char *args
, int from_tty
)
2559 struct target_ops
*attach_target
;
2560 struct inferior
*inferior
= current_inferior ();
2561 enum attach_post_wait_mode mode
;
2563 dont_repeat (); /* Not for the faint of heart */
2565 scoped_disable_commit_resumed
disable_commit_resumed ("attaching");
2567 if (gdbarch_has_global_solist (target_gdbarch ()))
2568 /* Don't complain if all processes share the same symbol
2571 else if (target_has_execution ())
2573 if (query (_("A program is being debugged already. Kill it? ")))
2576 error (_("Not killed."));
2579 /* Clean up any leftovers from other runs. Some other things from
2580 this function should probably be moved into target_pre_inferior. */
2581 target_pre_inferior (from_tty
);
2583 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
2584 args
= stripped
.get ();
2586 attach_target
= find_attach_target ();
2588 prepare_execution_command (attach_target
, async_exec
);
2590 if (non_stop
&& !attach_target
->supports_non_stop ())
2591 error (_("Cannot attach to this target in non-stop mode"));
2593 attach_target
->attach (args
, from_tty
);
2594 /* to_attach should push the target, so after this point we
2595 shouldn't refer to attach_target again. */
2596 attach_target
= NULL
;
2598 /* Set up the "saved terminal modes" of the inferior
2599 based on what modes we are starting it with. */
2600 target_terminal::init ();
2602 /* Install inferior's terminal modes. This may look like a no-op,
2603 as we've just saved them above, however, this does more than
2604 restore terminal settings:
2606 - installs a SIGINT handler that forwards SIGINT to the inferior.
2607 Otherwise a Ctrl-C pressed just while waiting for the initial
2608 stop would end up as a spurious Quit.
2610 - removes stdin from the event loop, which we need if attaching
2611 in the foreground, otherwise on targets that report an initial
2612 stop on attach (which are most) we'd process input/commands
2613 while we're in the event loop waiting for that stop. That is,
2614 before the attach continuation runs and the command is really
2616 target_terminal::inferior ();
2618 /* Set up execution context to know that we should return from
2619 wait_for_inferior as soon as the target reports a stop. */
2620 init_wait_for_inferior ();
2622 inferior
->needs_setup
= 1;
2624 if (target_is_non_stop_p ())
2626 /* If we find that the current thread isn't stopped, explicitly
2627 do so now, because we're going to install breakpoints and
2631 /* The user requested an `attach&'; stop just one thread. */
2632 target_stop (inferior_ptid
);
2634 /* The user requested an `attach', so stop all threads of this
2636 target_stop (ptid_t (inferior_ptid
.pid ()));
2639 /* Check for exec file mismatch, and let the user solve it. */
2640 validate_exec_file (from_tty
);
2642 mode
= async_exec
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_STOP
;
2644 /* Some system don't generate traps when attaching to inferior.
2645 E.g. Mach 3 or GNU hurd. */
2646 if (!target_attach_no_wait ())
2648 /* Careful here. See comments in inferior.h. Basically some
2649 OSes don't ignore SIGSTOPs on continue requests anymore. We
2650 need a way for handle_inferior_event to reset the stop_signal
2651 variable after an attach, and this is what
2652 STOP_QUIETLY_NO_SIGSTOP is for. */
2653 inferior
->control
.stop_soon
= STOP_QUIETLY_NO_SIGSTOP
;
2655 /* Wait for stop. */
2656 inferior
->add_continuation ([=] ()
2658 attach_post_wait (from_tty
, mode
);
2661 /* Let infrun consider waiting for events out of this
2663 inferior
->process_target ()->threads_executing
= true;
2665 if (!target_is_async_p ())
2666 mark_infrun_async_event_handler ();
2670 attach_post_wait (from_tty
, mode
);
2672 disable_commit_resumed
.reset_and_commit ();
2675 /* We had just found out that the target was already attached to an
2676 inferior. PTID points at a thread of this new inferior, that is
2677 the most likely to be stopped right now, but not necessarily so.
2678 The new inferior is assumed to be already added to the inferior
2679 list at this point. If LEAVE_RUNNING, then leave the threads of
2680 this inferior running, except those we've explicitly seen reported
2684 notice_new_inferior (thread_info
*thr
, bool leave_running
, int from_tty
)
2686 enum attach_post_wait_mode mode
2687 = leave_running
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_NOTHING
;
2689 gdb::optional
<scoped_restore_current_thread
> restore_thread
;
2691 if (inferior_ptid
!= null_ptid
)
2692 restore_thread
.emplace ();
2694 /* Avoid reading registers -- we haven't fetched the target
2696 switch_to_thread_no_regs (thr
);
2698 /* When we "notice" a new inferior we need to do all the things we
2699 would normally do if we had just attached to it. */
2703 struct inferior
*inferior
= current_inferior ();
2705 /* We're going to install breakpoints, and poke at memory,
2706 ensure that the inferior is stopped for a moment while we do
2708 target_stop (inferior_ptid
);
2710 inferior
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
2712 /* Wait for stop before proceeding. */
2713 inferior
->add_continuation ([=] ()
2715 attach_post_wait (from_tty
, mode
);
2721 attach_post_wait (from_tty
, mode
);
2726 * takes a program previously attached to and detaches it.
2727 * The program resumes execution and will no longer stop
2728 * on signals, etc. We better not have left any breakpoints
2729 * in the program or it'll die when it hits one. For this
2730 * to work, it may be necessary for the process to have been
2731 * previously attached. It *might* work if the program was
2732 * started via the normal ptrace (PTRACE_TRACEME).
2736 detach_command (const char *args
, int from_tty
)
2738 dont_repeat (); /* Not for the faint of heart. */
2740 if (inferior_ptid
== null_ptid
)
2741 error (_("The program is not being run."));
2743 scoped_disable_commit_resumed
disable_commit_resumed ("detaching");
2745 query_if_trace_running (from_tty
);
2747 disconnect_tracing ();
2749 /* Hold a strong reference to the target while (maybe)
2750 detaching the parent. Otherwise detaching could close the
2753 = target_ops_ref::new_reference (current_inferior ()->process_target ());
2755 /* Save this before detaching, since detaching may unpush the
2756 process_stratum target. */
2757 bool was_non_stop_p
= target_is_non_stop_p ();
2759 target_detach (current_inferior (), from_tty
);
2761 /* The current inferior process was just detached successfully. Get
2762 rid of breakpoints that no longer make sense. Note we don't do
2763 this within target_detach because that is also used when
2764 following child forks, and in that case we will want to transfer
2765 breakpoints to the child, not delete them. */
2766 breakpoint_init_inferior (inf_exited
);
2768 /* If the solist is global across inferiors, don't clear it when we
2769 detach from a single inferior. */
2770 if (!gdbarch_has_global_solist (target_gdbarch ()))
2771 no_shared_libraries (NULL
, from_tty
);
2773 if (deprecated_detach_hook
)
2774 deprecated_detach_hook ();
2776 if (!was_non_stop_p
)
2777 restart_after_all_stop_detach (as_process_stratum_target (target_ref
.get ()));
2779 disable_commit_resumed
.reset_and_commit ();
2782 /* Disconnect from the current target without resuming it (leaving it
2783 waiting for a debugger).
2785 We'd better not have left any breakpoints in the program or the
2786 next debugger will get confused. Currently only supported for some
2787 remote targets, since the normal attach mechanisms don't work on
2788 stopped processes on some native platforms (e.g. GNU/Linux). */
2791 disconnect_command (const char *args
, int from_tty
)
2793 dont_repeat (); /* Not for the faint of heart. */
2794 query_if_trace_running (from_tty
);
2795 disconnect_tracing ();
2796 target_disconnect (args
, from_tty
);
2797 no_shared_libraries (NULL
, from_tty
);
2798 init_thread_list ();
2799 if (deprecated_detach_hook
)
2800 deprecated_detach_hook ();
2803 /* Stop PTID in the current target, and tag the PTID threads as having
2804 been explicitly requested to stop. PTID can be a thread, a
2805 process, or minus_one_ptid, meaning all threads of all inferiors of
2806 the current target. */
2809 stop_current_target_threads_ns (ptid_t ptid
)
2813 /* Tag the thread as having been explicitly requested to stop, so
2814 other parts of gdb know not to resume this thread automatically,
2815 if it was stopped due to an internal event. Limit this to
2816 non-stop mode, as when debugging a multi-threaded application in
2817 all-stop mode, we will only get one stop event --- it's undefined
2818 which thread will report the event. */
2819 set_stop_requested (current_inferior ()->process_target (),
2823 /* See inferior.h. */
2826 interrupt_target_1 (bool all_threads
)
2828 scoped_disable_commit_resumed
disable_commit_resumed ("interrupting");
2834 scoped_restore_current_thread restore_thread
;
2836 for (inferior
*inf
: all_inferiors ())
2838 switch_to_inferior_no_thread (inf
);
2839 stop_current_target_threads_ns (minus_one_ptid
);
2843 stop_current_target_threads_ns (inferior_ptid
);
2846 target_interrupt ();
2848 disable_commit_resumed
.reset_and_commit ();
2852 Stop the execution of the target while running in async mode, in
2853 the background. In all-stop, stop the whole process. In non-stop
2854 mode, stop the current thread only by default, or stop all threads
2855 if the `-a' switch is used. */
2858 interrupt_command (const char *args
, int from_tty
)
2860 if (target_can_async_p ())
2862 int all_threads
= 0;
2864 dont_repeat (); /* Not for the faint of heart. */
2867 && startswith (args
, "-a"))
2870 if (!non_stop
&& all_threads
)
2871 error (_("-a is meaningless in all-stop mode."));
2873 interrupt_target_1 (all_threads
);
2877 /* See inferior.h. */
2880 default_print_float_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
2881 struct frame_info
*frame
, const char *args
)
2884 int printed_something
= 0;
2886 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2888 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, float_reggroup
))
2890 printed_something
= 1;
2891 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2894 if (!printed_something
)
2895 fprintf_filtered (file
, "No floating-point info "
2896 "available for this processor.\n");
2900 info_float_command (const char *args
, int from_tty
)
2902 struct frame_info
*frame
;
2904 if (!target_has_registers ())
2905 error (_("The program has no registers now."));
2907 frame
= get_selected_frame (NULL
);
2908 gdbarch_print_float_info (get_frame_arch (frame
), gdb_stdout
, frame
, args
);
2911 /* Implement `info proc' family of commands. */
2914 info_proc_cmd_1 (const char *args
, enum info_proc_what what
, int from_tty
)
2916 struct gdbarch
*gdbarch
= get_current_arch ();
2918 if (!target_info_proc (args
, what
))
2920 if (gdbarch_info_proc_p (gdbarch
))
2921 gdbarch_info_proc (gdbarch
, args
, what
);
2923 error (_("Not supported on this target."));
2927 /* Implement `info proc' when given without any further parameters. */
2930 info_proc_cmd (const char *args
, int from_tty
)
2932 info_proc_cmd_1 (args
, IP_MINIMAL
, from_tty
);
2935 /* Implement `info proc mappings'. */
2938 info_proc_cmd_mappings (const char *args
, int from_tty
)
2940 info_proc_cmd_1 (args
, IP_MAPPINGS
, from_tty
);
2943 /* Implement `info proc stat'. */
2946 info_proc_cmd_stat (const char *args
, int from_tty
)
2948 info_proc_cmd_1 (args
, IP_STAT
, from_tty
);
2951 /* Implement `info proc status'. */
2954 info_proc_cmd_status (const char *args
, int from_tty
)
2956 info_proc_cmd_1 (args
, IP_STATUS
, from_tty
);
2959 /* Implement `info proc cwd'. */
2962 info_proc_cmd_cwd (const char *args
, int from_tty
)
2964 info_proc_cmd_1 (args
, IP_CWD
, from_tty
);
2967 /* Implement `info proc cmdline'. */
2970 info_proc_cmd_cmdline (const char *args
, int from_tty
)
2972 info_proc_cmd_1 (args
, IP_CMDLINE
, from_tty
);
2975 /* Implement `info proc exe'. */
2978 info_proc_cmd_exe (const char *args
, int from_tty
)
2980 info_proc_cmd_1 (args
, IP_EXE
, from_tty
);
2983 /* Implement `info proc files'. */
2986 info_proc_cmd_files (const char *args
, int from_tty
)
2988 info_proc_cmd_1 (args
, IP_FILES
, from_tty
);
2991 /* Implement `info proc all'. */
2994 info_proc_cmd_all (const char *args
, int from_tty
)
2996 info_proc_cmd_1 (args
, IP_ALL
, from_tty
);
2999 /* Implement `show print finish'. */
3002 show_print_finish (struct ui_file
*file
, int from_tty
,
3003 struct cmd_list_element
*c
,
3006 fprintf_filtered (file
, _("\
3007 Printing of return value after `finish' is %s.\n"),
3012 /* This help string is used for the run, start, and starti commands.
3013 It is defined as a macro to prevent duplication. */
3015 #define RUN_ARGS_HELP \
3016 "You may specify arguments to give it.\n\
3017 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3018 shell that will start the program (specified by the \"$SHELL\" environment\n\
3019 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3020 are also allowed.\n\
3022 With no arguments, uses arguments last specified (with \"run\" or \n\
3023 \"set args\"). To cancel previous arguments and run with no arguments,\n\
3024 use \"set args\" without arguments.\n\
3026 To start the inferior without using a shell, use \"set startup-with-shell off\"."
3028 void _initialize_infcmd ();
3030 _initialize_infcmd ()
3032 static struct cmd_list_element
*info_proc_cmdlist
;
3033 struct cmd_list_element
*c
= NULL
;
3034 const char *cmd_name
;
3036 /* Add the filename of the terminal connected to inferior I/O. */
3037 add_setshow_optional_filename_cmd ("inferior-tty", class_run
,
3038 &inferior_io_terminal_scratch
, _("\
3039 Set terminal for future runs of program being debugged."), _("\
3040 Show terminal for future runs of program being debugged."), _("\
3041 Usage: set inferior-tty [TTY]\n\n\
3042 If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3044 set_inferior_tty_command
,
3045 show_inferior_tty_command
,
3046 &setlist
, &showlist
);
3047 cmd_name
= "inferior-tty";
3048 c
= lookup_cmd (&cmd_name
, setlist
, "", NULL
, -1, 1);
3049 gdb_assert (c
!= NULL
);
3050 add_alias_cmd ("tty", c
, class_run
, 0, &cmdlist
);
3053 add_setshow_string_noescape_cmd (cmd_name
, class_run
,
3054 &inferior_args_scratch
, _("\
3055 Set argument list to give program being debugged when it is started."), _("\
3056 Show argument list to give program being debugged when it is started."), _("\
3057 Follow this command with any number of args, to be passed to the program."),
3060 &setlist
, &showlist
);
3061 c
= lookup_cmd (&cmd_name
, setlist
, "", NULL
, -1, 1);
3062 gdb_assert (c
!= NULL
);
3063 set_cmd_completer (c
, filename_completer
);
3066 add_setshow_string_noescape_cmd (cmd_name
, class_run
,
3067 &inferior_cwd_scratch
, _("\
3068 Set the current working directory to be used when the inferior is started.\n\
3069 Changing this setting does not have any effect on inferiors that are\n\
3072 Show the current working directory that is used when the inferior is started."),
3074 Use this command to change the current working directory that will be used\n\
3075 when the inferior is started. This setting does not affect GDB's current\n\
3076 working directory."),
3079 &setlist
, &showlist
);
3080 c
= lookup_cmd (&cmd_name
, setlist
, "", NULL
, -1, 1);
3081 gdb_assert (c
!= NULL
);
3082 set_cmd_completer (c
, filename_completer
);
3084 c
= add_cmd ("environment", no_class
, environment_info
, _("\
3085 The environment to give the program, or one variable's value.\n\
3086 With an argument VAR, prints the value of environment variable VAR to\n\
3087 give the program being debugged. With no arguments, prints the entire\n\
3088 environment to be given to the program."), &showlist
);
3089 set_cmd_completer (c
, noop_completer
);
3091 add_basic_prefix_cmd ("unset", no_class
,
3092 _("Complement to certain \"set\" commands."),
3093 &unsetlist
, 0, &cmdlist
);
3095 c
= add_cmd ("environment", class_run
, unset_environment_command
, _("\
3096 Cancel environment variable VAR for the program.\n\
3097 This does not affect the program until the next \"run\" command."),
3099 set_cmd_completer (c
, noop_completer
);
3101 c
= add_cmd ("environment", class_run
, set_environment_command
, _("\
3102 Set environment variable value to give the program.\n\
3103 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3104 VALUES of environment variables are uninterpreted strings.\n\
3105 This does not affect the program until the next \"run\" command."),
3107 set_cmd_completer (c
, noop_completer
);
3109 c
= add_com ("path", class_files
, path_command
, _("\
3110 Add directory DIR(s) to beginning of search path for object files.\n\
3111 $cwd in the path means the current working directory.\n\
3112 This path is equivalent to the $PATH shell variable. It is a list of\n\
3113 directories, separated by colons. These directories are searched to find\n\
3114 fully linked executable files and separately compiled object files as \
3116 set_cmd_completer (c
, filename_completer
);
3118 c
= add_cmd ("paths", no_class
, path_info
, _("\
3119 Current search path for finding object files.\n\
3120 $cwd in the path means the current working directory.\n\
3121 This path is equivalent to the $PATH shell variable. It is a list of\n\
3122 directories, separated by colons. These directories are searched to find\n\
3123 fully linked executable files and separately compiled object files as \
3126 set_cmd_completer (c
, noop_completer
);
3128 add_prefix_cmd ("kill", class_run
, kill_command
,
3129 _("Kill execution of program being debugged."),
3130 &killlist
, 0, &cmdlist
);
3132 add_com ("attach", class_run
, attach_command
, _("\
3133 Attach to a process or file outside of GDB.\n\
3134 This command attaches to another target, of the same type as your last\n\
3135 \"target\" command (\"info files\" will show your target stack).\n\
3136 The command may take as argument a process id or a device file.\n\
3137 For a process id, you must have permission to send the process a signal,\n\
3138 and it must have the same effective uid as the debugger.\n\
3139 When using \"attach\" with a process id, the debugger finds the\n\
3140 program running in the process, looking first in the current working\n\
3141 directory, or (if not found there) using the source file search path\n\
3142 (see the \"directory\" command). You can also use the \"file\" command\n\
3143 to specify the program, and to load its symbol table."));
3145 add_prefix_cmd ("detach", class_run
, detach_command
, _("\
3146 Detach a process or file previously attached.\n\
3147 If a process, it is no longer traced, and it continues its execution. If\n\
3148 you were debugging a file, the file is closed and gdb no longer accesses it."),
3149 &detachlist
, 0, &cmdlist
);
3151 add_com ("disconnect", class_run
, disconnect_command
, _("\
3152 Disconnect from a target.\n\
3153 The target will wait for another debugger to connect. Not available for\n\
3156 c
= add_com ("signal", class_run
, signal_command
, _("\
3157 Continue program with the specified signal.\n\
3158 Usage: signal SIGNAL\n\
3159 The SIGNAL argument is processed the same as the handle command.\n\
3161 An argument of \"0\" means continue the program without sending it a signal.\n\
3162 This is useful in cases where the program stopped because of a signal,\n\
3163 and you want to resume the program while discarding the signal.\n\
3165 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3166 the current thread only."));
3167 set_cmd_completer (c
, signal_completer
);
3169 c
= add_com ("queue-signal", class_run
, queue_signal_command
, _("\
3170 Queue a signal to be delivered to the current thread when it is resumed.\n\
3171 Usage: queue-signal SIGNAL\n\
3172 The SIGNAL argument is processed the same as the handle command.\n\
3173 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3175 An argument of \"0\" means remove any currently queued signal from\n\
3176 the current thread. This is useful in cases where the program stopped\n\
3177 because of a signal, and you want to resume it while discarding the signal.\n\
3179 In a multi-threaded program the signal is queued with, or discarded from,\n\
3180 the current thread only."));
3181 set_cmd_completer (c
, signal_completer
);
3183 cmd_list_element
*stepi_cmd
3184 = add_com ("stepi", class_run
, stepi_command
, _("\
3185 Step one instruction exactly.\n\
3187 Argument N means step N times (or till program stops for another \
3189 add_com_alias ("si", stepi_cmd
, class_run
, 0);
3191 cmd_list_element
*nexti_cmd
3192 = add_com ("nexti", class_run
, nexti_command
, _("\
3193 Step one instruction, but proceed through subroutine calls.\n\
3195 Argument N means step N times (or till program stops for another \
3197 add_com_alias ("ni", nexti_cmd
, class_run
, 0);
3199 cmd_list_element
*finish_cmd
3200 = add_com ("finish", class_run
, finish_command
, _("\
3201 Execute until selected stack frame returns.\n\
3203 Upon return, the value returned is printed and put in the value history."));
3204 add_com_alias ("fin", finish_cmd
, class_run
, 1);
3206 cmd_list_element
*next_cmd
3207 = add_com ("next", class_run
, next_command
, _("\
3208 Step program, proceeding through subroutine calls.\n\
3210 Unlike \"step\", if the current source line calls a subroutine,\n\
3211 this command does not enter the subroutine, but instead steps over\n\
3212 the call, in effect treating it as a single source line."));
3213 add_com_alias ("n", next_cmd
, class_run
, 1);
3215 cmd_list_element
*step_cmd
3216 = add_com ("step", class_run
, step_command
, _("\
3217 Step program until it reaches a different source line.\n\
3219 Argument N means step N times (or till program stops for another \
3221 add_com_alias ("s", step_cmd
, class_run
, 1);
3223 cmd_list_element
*until_cmd
3224 = add_com ("until", class_run
, until_command
, _("\
3225 Execute until past the current line or past a LOCATION.\n\
3226 Execute until the program reaches a source line greater than the current\n\
3227 or a specified location (same args as break command) within the current \
3229 set_cmd_completer (until_cmd
, location_completer
);
3230 add_com_alias ("u", until_cmd
, class_run
, 1);
3232 c
= add_com ("advance", class_run
, advance_command
, _("\
3233 Continue the program up to the given location (same form as args for break \
3235 Execution will also stop upon exit from the current stack frame."));
3236 set_cmd_completer (c
, location_completer
);
3238 cmd_list_element
*jump_cmd
3239 = add_com ("jump", class_run
, jump_command
, _("\
3240 Continue program being debugged at specified line or address.\n\
3241 Usage: jump LOCATION\n\
3242 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3243 for an address to start at."));
3244 set_cmd_completer (jump_cmd
, location_completer
);
3245 add_com_alias ("j", jump_cmd
, class_run
, 1);
3247 cmd_list_element
*continue_cmd
3248 = add_com ("continue", class_run
, continue_command
, _("\
3249 Continue program being debugged, after signal or breakpoint.\n\
3250 Usage: continue [N]\n\
3251 If proceeding from breakpoint, a number N may be used as an argument,\n\
3252 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3253 the breakpoint won't break until the Nth time it is reached).\n\
3255 If non-stop mode is enabled, continue only the current thread,\n\
3256 otherwise all the threads in the program are continued. To \n\
3257 continue all stopped threads in non-stop mode, use the -a option.\n\
3258 Specifying -a and an ignore count simultaneously is an error."));
3259 add_com_alias ("c", continue_cmd
, class_run
, 1);
3260 add_com_alias ("fg", continue_cmd
, class_run
, 1);
3262 cmd_list_element
*run_cmd
3263 = add_com ("run", class_run
, run_command
, _("\
3264 Start debugged program.\n"
3266 set_cmd_completer (run_cmd
, filename_completer
);
3267 add_com_alias ("r", run_cmd
, class_run
, 1);
3269 c
= add_com ("start", class_run
, start_command
, _("\
3270 Start the debugged program stopping at the beginning of the main procedure.\n"
3272 set_cmd_completer (c
, filename_completer
);
3274 c
= add_com ("starti", class_run
, starti_command
, _("\
3275 Start the debugged program stopping at the first instruction.\n"
3277 set_cmd_completer (c
, filename_completer
);
3279 add_com ("interrupt", class_run
, interrupt_command
,
3280 _("Interrupt the execution of the debugged program.\n\
3281 If non-stop mode is enabled, interrupt only the current thread,\n\
3282 otherwise all the threads in the program are stopped. To \n\
3283 interrupt all running threads in non-stop mode, use the -a option."));
3285 cmd_list_element
*info_registers_cmd
3286 = add_info ("registers", info_registers_command
, _("\
3287 List of integer registers and their contents, for selected stack frame.\n\
3288 One or more register names as argument means describe the given registers.\n\
3289 One or more register group names as argument means describe the registers\n\
3290 in the named register groups."));
3291 add_info_alias ("r", info_registers_cmd
, 1);
3292 set_cmd_completer (info_registers_cmd
, reg_or_group_completer
);
3294 c
= add_info ("all-registers", info_all_registers_command
, _("\
3295 List of all registers and their contents, for selected stack frame.\n\
3296 One or more register names as argument means describe the given registers.\n\
3297 One or more register group names as argument means describe the registers\n\
3298 in the named register groups."));
3299 set_cmd_completer (c
, reg_or_group_completer
);
3301 add_info ("program", info_program_command
,
3302 _("Execution status of the program."));
3304 add_info ("float", info_float_command
,
3305 _("Print the status of the floating point unit."));
3307 add_info ("vector", info_vector_command
,
3308 _("Print the status of the vector unit."));
3310 add_prefix_cmd ("proc", class_info
, info_proc_cmd
,
3312 Show additional information about a process.\n\
3313 Specify any process id, or use the program being debugged by default."),
3315 1/*allow-unknown*/, &infolist
);
3317 add_cmd ("mappings", class_info
, info_proc_cmd_mappings
, _("\
3318 List memory regions mapped by the specified process."),
3319 &info_proc_cmdlist
);
3321 add_cmd ("stat", class_info
, info_proc_cmd_stat
, _("\
3322 List process info from /proc/PID/stat."),
3323 &info_proc_cmdlist
);
3325 add_cmd ("status", class_info
, info_proc_cmd_status
, _("\
3326 List process info from /proc/PID/status."),
3327 &info_proc_cmdlist
);
3329 add_cmd ("cwd", class_info
, info_proc_cmd_cwd
, _("\
3330 List current working directory of the specified process."),
3331 &info_proc_cmdlist
);
3333 add_cmd ("cmdline", class_info
, info_proc_cmd_cmdline
, _("\
3334 List command line arguments of the specified process."),
3335 &info_proc_cmdlist
);
3337 add_cmd ("exe", class_info
, info_proc_cmd_exe
, _("\
3338 List absolute filename for executable of the specified process."),
3339 &info_proc_cmdlist
);
3341 add_cmd ("files", class_info
, info_proc_cmd_files
, _("\
3342 List files opened by the specified process."),
3343 &info_proc_cmdlist
);
3345 add_cmd ("all", class_info
, info_proc_cmd_all
, _("\
3346 List all available info about the specified process."),
3347 &info_proc_cmdlist
);
3349 add_setshow_boolean_cmd ("finish", class_support
,
3350 &user_print_options
.finish_print
, _("\
3351 Set whether `finish' prints the return value."), _("\
3352 Show whether `finish' prints the return value."), NULL
,
3355 &setprintlist
, &showprintlist
);