1 /* Memory-access and commands for "inferior" process, for GDB.
3 Copyright (C) 1986-2019 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"
28 #include "gdbsupport/environ.h"
36 #include "completer.h"
38 #include "event-top.h"
39 #include "parser-defs.h"
41 #include "reggroups.h"
45 #include "observable.h"
46 #include "target-descriptions.h"
47 #include "user-regs.h"
48 #include "cli/cli-decode.h"
49 #include "gdbthread.h"
51 #include "inline-frame.h"
52 #include "tracepoint.h"
54 #include "continuations.h"
56 #include "cli/cli-utils.h"
58 #include "thread-fsm.h"
61 #include "gdbsupport/gdb_optional.h"
63 #include "cli/cli-style.h"
65 /* Local functions: */
67 static void until_next_command (int);
69 static void step_1 (int, int, const char *);
71 #define ERROR_NO_INFERIOR \
72 if (!target_has_execution) error (_("The program is not being run."));
74 /* Scratch area where string containing arguments to give to the
75 program will be stored by 'set args'. As soon as anything is
76 stored, notice_args_set will move it into per-inferior storage.
77 Arguments are separated by spaces. Empty string (pointer to '\0')
80 static char *inferior_args_scratch
;
82 /* Scratch area where the new cwd will be stored by 'set cwd'. */
84 static char *inferior_cwd_scratch
;
86 /* Scratch area where 'set inferior-tty' will store user-provided value.
87 We'll immediate copy it into per-inferior storage. */
89 static char *inferior_io_terminal_scratch
;
91 /* Pid of our debugged inferior, or 0 if no inferior now.
92 Since various parts of infrun.c test this to see whether there is a program
93 being debugged it should be nonzero (currently 3 is used) for remote
98 /* Nonzero if stopped due to completion of a stack dummy routine. */
100 enum stop_stack_kind stop_stack_dummy
;
102 /* Nonzero if stopped due to a random (unexpected) signal in inferior
105 int stopped_by_random_signal
;
107 /* See inferior.h. */
109 bool startup_with_shell
= true;
112 /* Accessor routines. */
114 /* Set the io terminal for the current inferior. Ownership of
115 TERMINAL_NAME is not transferred. */
118 set_inferior_io_terminal (const char *terminal_name
)
120 xfree (current_inferior ()->terminal
);
122 if (terminal_name
!= NULL
&& *terminal_name
!= '\0')
123 current_inferior ()->terminal
= xstrdup (terminal_name
);
125 current_inferior ()->terminal
= NULL
;
129 get_inferior_io_terminal (void)
131 return current_inferior ()->terminal
;
135 set_inferior_tty_command (const char *args
, int from_tty
,
136 struct cmd_list_element
*c
)
138 /* CLI has assigned the user-provided value to inferior_io_terminal_scratch.
139 Now route it to current inferior. */
140 set_inferior_io_terminal (inferior_io_terminal_scratch
);
144 show_inferior_tty_command (struct ui_file
*file
, int from_tty
,
145 struct cmd_list_element
*c
, const char *value
)
147 /* Note that we ignore the passed-in value in favor of computing it
149 const char *inferior_io_terminal
= get_inferior_io_terminal ();
151 if (inferior_io_terminal
== NULL
)
152 inferior_io_terminal
= "";
153 fprintf_filtered (gdb_stdout
,
154 _("Terminal for future runs of program being debugged "
155 "is \"%s\".\n"), inferior_io_terminal
);
159 get_inferior_args (void)
161 if (current_inferior ()->argc
!= 0)
165 n
= construct_inferior_arguments (current_inferior ()->argc
,
166 current_inferior ()->argv
);
167 set_inferior_args (n
);
171 if (current_inferior ()->args
== NULL
)
172 current_inferior ()->args
= xstrdup ("");
174 return current_inferior ()->args
;
177 /* Set the arguments for the current inferior. Ownership of
178 NEWARGS is not transferred. */
181 set_inferior_args (const char *newargs
)
183 xfree (current_inferior ()->args
);
184 current_inferior ()->args
= newargs
? xstrdup (newargs
) : NULL
;
185 current_inferior ()->argc
= 0;
186 current_inferior ()->argv
= 0;
190 set_inferior_args_vector (int argc
, char **argv
)
192 current_inferior ()->argc
= argc
;
193 current_inferior ()->argv
= argv
;
196 /* Notice when `set args' is run. */
199 set_args_command (const char *args
, int from_tty
, struct cmd_list_element
*c
)
201 /* CLI has assigned the user-provided value to inferior_args_scratch.
202 Now route it to current inferior. */
203 set_inferior_args (inferior_args_scratch
);
206 /* Notice when `show args' is run. */
209 show_args_command (struct ui_file
*file
, int from_tty
,
210 struct cmd_list_element
*c
, const char *value
)
212 /* Note that we ignore the passed-in value in favor of computing it
214 deprecated_show_value_hack (file
, from_tty
, c
, get_inferior_args ());
217 /* See gdbsupport/common-inferior.h. */
220 set_inferior_cwd (const char *cwd
)
222 struct inferior
*inf
= current_inferior ();
224 gdb_assert (inf
!= NULL
);
229 inf
->cwd
.reset (xstrdup (cwd
));
232 /* See gdbsupport/common-inferior.h. */
237 return current_inferior ()->cwd
.get ();
240 /* Handle the 'set cwd' command. */
243 set_cwd_command (const char *args
, int from_tty
, struct cmd_list_element
*c
)
245 if (*inferior_cwd_scratch
== '\0')
246 set_inferior_cwd (NULL
);
248 set_inferior_cwd (inferior_cwd_scratch
);
251 /* Handle the 'show cwd' command. */
254 show_cwd_command (struct ui_file
*file
, int from_tty
,
255 struct cmd_list_element
*c
, const char *value
)
257 const char *cwd
= get_inferior_cwd ();
260 fprintf_filtered (gdb_stdout
,
262 You have not set the inferior's current working directory.\n\
263 The inferior will inherit GDB's cwd if native debugging, or the remote\n\
264 server's cwd if remote debugging.\n"));
266 fprintf_filtered (gdb_stdout
,
267 _("Current working directory that will be used "
268 "when starting the inferior is \"%s\".\n"), cwd
);
272 /* Compute command-line string given argument vector. This does the
273 same shell processing as fork_inferior. */
276 construct_inferior_arguments (int argc
, char **argv
)
280 if (startup_with_shell
)
283 /* This holds all the characters considered special to the
285 static const char special
[] = "\"!&*|[]{}<>?`~^=;, \t\n";
286 static const char quote
= '"';
288 /* This holds all the characters considered special to the
289 typical Unix shells. We include `^' because the SunOS
290 /bin/sh treats it as a synonym for `|'. */
291 static const char special
[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n";
292 static const char quote
= '\'';
298 /* We over-compute the size. It shouldn't matter. */
299 for (i
= 0; i
< argc
; ++i
)
300 length
+= 3 * strlen (argv
[i
]) + 1 + 2 * (argv
[i
][0] == '\0');
302 result
= (char *) xmalloc (length
);
305 for (i
= 0; i
< argc
; ++i
)
310 /* Need to handle empty arguments specially. */
311 if (argv
[i
][0] == '\0')
321 if (strpbrk (argv
[i
], special
))
327 for (cp
= argv
[i
]; *cp
; ++cp
)
331 /* A newline cannot be quoted with a backslash (it
332 just disappears), only by putting it inside
343 if (strchr (special
, *cp
) != NULL
)
359 /* In this case we can't handle arguments that contain spaces,
360 tabs, or newlines -- see breakup_args(). */
364 for (i
= 0; i
< argc
; ++i
)
366 char *cp
= strchr (argv
[i
], ' ');
368 cp
= strchr (argv
[i
], '\t');
370 cp
= strchr (argv
[i
], '\n');
372 error (_("can't handle command-line "
373 "argument containing whitespace"));
374 length
+= strlen (argv
[i
]) + 1;
377 result
= (char *) xmalloc (length
);
379 for (i
= 0; i
< argc
; ++i
)
382 strcat (result
, " ");
383 strcat (result
, argv
[i
]);
391 /* This function strips the '&' character (indicating background
392 execution) that is added as *the last* of the arguments ARGS of a
393 command. A copy of the incoming ARGS without the '&' is returned,
394 unless the resulting string after stripping is empty, in which case
395 NULL is returned. *BG_CHAR_P is an output boolean that indicates
396 whether the '&' character was found. */
398 static gdb::unique_xmalloc_ptr
<char>
399 strip_bg_char (const char *args
, int *bg_char_p
)
403 if (args
== NULL
|| *args
== '\0')
409 p
= args
+ strlen (args
);
413 while (p
> args
&& isspace (p
[-1]))
418 return gdb::unique_xmalloc_ptr
<char>
419 (savestring (args
, p
- args
));
421 return gdb::unique_xmalloc_ptr
<char> (nullptr);
425 return make_unique_xstrdup (args
);
428 /* Common actions to take after creating any sort of inferior, by any
429 means (running, attaching, connecting, et cetera). The target
430 should be stopped. */
433 post_create_inferior (struct target_ops
*target
, int from_tty
)
436 /* Be sure we own the terminal in case write operations are performed. */
437 target_terminal::ours_for_output ();
439 /* If the target hasn't taken care of this already, do it now.
440 Targets which need to access registers during to_open,
441 to_create_inferior, or to_attach should do it earlier; but many
443 target_find_description ();
445 /* Now that we know the register layout, retrieve current PC. But
446 if the PC is unavailable (e.g., we're opening a core file with
447 missing registers info), ignore it. */
448 thread_info
*thr
= inferior_thread ();
450 thr
->suspend
.stop_pc
= 0;
453 thr
->suspend
.stop_pc
= regcache_read_pc (get_current_regcache ());
455 catch (const gdb_exception_error
&ex
)
457 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
463 const unsigned solib_add_generation
464 = current_program_space
->solib_add_generation
;
466 /* Create the hooks to handle shared library load and unload
468 solib_create_inferior_hook (from_tty
);
470 if (current_program_space
->solib_add_generation
== solib_add_generation
)
472 /* The platform-specific hook should load initial shared libraries,
473 but didn't. FROM_TTY will be incorrectly 0 but such solib
474 targets should be fixed anyway. Call it only after the solib
475 target has been initialized by solib_create_inferior_hook. */
478 warning (_("platform-specific solib_create_inferior_hook did "
479 "not load initial shared libraries."));
481 /* If the solist is global across processes, there's no need to
483 if (!gdbarch_has_global_solist (target_gdbarch ()))
484 solib_add (NULL
, 0, auto_solib_add
);
488 /* If the user sets watchpoints before execution having started,
489 then she gets software watchpoints, because GDB can't know which
490 target will end up being pushed, or if it supports hardware
491 watchpoints or not. breakpoint_re_set takes care of promoting
492 watchpoints to hardware watchpoints if possible, however, if this
493 new inferior doesn't load shared libraries or we don't pull in
494 symbols from any other source on this target/arch,
495 breakpoint_re_set is never called. Call it now so that software
496 watchpoints get a chance to be promoted to hardware watchpoints
497 if the now pushed target supports hardware watchpoints. */
498 breakpoint_re_set ();
500 gdb::observers::inferior_created
.notify (target
, from_tty
);
503 /* Kill the inferior if already running. This function is designed
504 to be called when we are about to start the execution of the program
505 from the beginning. Ask the user to confirm that he wants to restart
506 the program being debugged when FROM_TTY is non-null. */
509 kill_if_already_running (int from_tty
)
511 if (inferior_ptid
!= null_ptid
&& target_has_execution
)
513 /* Bail out before killing the program if we will not be able to
515 target_require_runnable ();
518 && !query (_("The program being debugged has been started already.\n\
519 Start it from the beginning? ")))
520 error (_("Program not restarted."));
525 /* See inferior.h. */
528 prepare_execution_command (struct target_ops
*target
, int background
)
530 /* If we get a request for running in the bg but the target
531 doesn't support it, error out. */
532 if (background
&& !target
->can_async_p ())
533 error (_("Asynchronous execution not supported on this target."));
537 /* If we get a request for running in the fg, then we need to
538 simulate synchronous (fg) execution. Note no cleanup is
539 necessary for this. stdin is re-enabled whenever an error
540 reaches the top level. */
541 all_uis_on_sync_execution_starting ();
545 /* Determine how the new inferior will behave. */
549 /* Run program without any explicit stop during startup. */
552 /* Stop at the beginning of the program's main function. */
555 /* Stop at the first instruction of the program. */
556 RUN_STOP_AT_FIRST_INSN
559 /* Implement the "run" command. Force a stop during program start if
560 requested by RUN_HOW. */
563 run_command_1 (const char *args
, int from_tty
, enum run_how run_how
)
565 const char *exec_file
;
566 struct ui_out
*uiout
= current_uiout
;
567 struct target_ops
*run_target
;
572 kill_if_already_running (from_tty
);
574 init_wait_for_inferior ();
575 clear_breakpoint_hit_counts ();
577 /* Clean up any leftovers from other runs. Some other things from
578 this function should probably be moved into target_pre_inferior. */
579 target_pre_inferior (from_tty
);
581 /* The comment here used to read, "The exec file is re-read every
582 time we do a generic_mourn_inferior, so we just have to worry
583 about the symbol file." The `generic_mourn_inferior' function
584 gets called whenever the program exits. However, suppose the
585 program exits, and *then* the executable file changes? We need
586 to check again here. Since reopen_exec_file doesn't do anything
587 if the timestamp hasn't changed, I don't see the harm. */
591 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
592 args
= stripped
.get ();
594 /* Do validation and preparation before possibly changing anything
597 run_target
= find_run_target ();
599 prepare_execution_command (run_target
, async_exec
);
601 if (non_stop
&& !run_target
->supports_non_stop ())
602 error (_("The target does not support running in non-stop mode."));
604 /* Done. Can now set breakpoints, change inferior args, etc. */
606 /* Insert temporary breakpoint in main function if requested. */
607 if (run_how
== RUN_STOP_AT_MAIN
)
609 std::string arg
= string_printf ("-qualified %s", main_name ());
610 tbreak_command (arg
.c_str (), 0);
613 exec_file
= get_exec_file (0);
615 /* We keep symbols from add-symbol-file, on the grounds that the
616 user might want to add some symbols before running the program
617 (right?). But sometimes (dynamic loading where the user manually
618 introduces the new symbols with add-symbol-file), the code which
619 the symbols describe does not persist between runs. Currently
620 the user has to manually nuke all symbols between runs if they
621 want them to go away (PR 2207). This is probably reasonable. */
623 /* If there were other args, beside '&', process them. */
625 set_inferior_args (args
);
629 uiout
->field_string (NULL
, "Starting program");
632 uiout
->field_string ("execfile", exec_file
);
634 /* We call get_inferior_args() because we might need to compute
636 uiout
->field_string ("infargs", get_inferior_args ());
641 /* We call get_inferior_args() because we might need to compute
643 run_target
->create_inferior (exec_file
,
644 std::string (get_inferior_args ()),
645 current_inferior ()->environment
.envp (),
647 /* to_create_inferior should push the target, so after this point we
648 shouldn't refer to run_target again. */
651 /* We're starting off a new process. When we get out of here, in
652 non-stop mode, finish the state of all threads of that process,
653 but leave other threads alone, as they may be stopped in internal
654 events --- the frontend shouldn't see them as stopped. In
655 all-stop, always finish the state of all threads, as we may be
656 resuming more than just the new process. */
657 ptid_t finish_ptid
= (non_stop
658 ? ptid_t (current_inferior ()->pid
)
660 scoped_finish_thread_state
finish_state (finish_ptid
);
662 /* Pass zero for FROM_TTY, because at this point the "run" command
663 has done its thing; now we are setting up the running program. */
664 post_create_inferior (current_top_target (), 0);
666 /* Queue a pending event so that the program stops immediately. */
667 if (run_how
== RUN_STOP_AT_FIRST_INSN
)
669 thread_info
*thr
= inferior_thread ();
670 thr
->suspend
.waitstatus_pending_p
= 1;
671 thr
->suspend
.waitstatus
.kind
= TARGET_WAITKIND_STOPPED
;
672 thr
->suspend
.waitstatus
.value
.sig
= GDB_SIGNAL_0
;
675 /* Start the target running. Do not use -1 continuation as it would skip
676 breakpoint right at the entry point. */
677 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0
);
679 /* Since there was no error, there's no need to finish the thread
681 finish_state
.release ();
685 run_command (const char *args
, int from_tty
)
687 run_command_1 (args
, from_tty
, RUN_NORMAL
);
690 /* Start the execution of the program up until the beginning of the main
694 start_command (const char *args
, int from_tty
)
696 /* Some languages such as Ada need to search inside the program
697 minimal symbols for the location where to put the temporary
698 breakpoint before starting. */
699 if (!have_minimal_symbols ())
700 error (_("No symbol table loaded. Use the \"file\" command."));
702 /* Run the program until reaching the main procedure... */
703 run_command_1 (args
, from_tty
, RUN_STOP_AT_MAIN
);
706 /* Start the execution of the program stopping at the first
710 starti_command (const char *args
, int from_tty
)
712 run_command_1 (args
, from_tty
, RUN_STOP_AT_FIRST_INSN
);
716 proceed_thread_callback (struct thread_info
*thread
, void *arg
)
718 /* We go through all threads individually instead of compressing
719 into a single target `resume_all' request, because some threads
720 may be stopped in internal breakpoints/events, or stopped waiting
721 for its turn in the displaced stepping queue (that is, they are
722 running && !executing). The target side has no idea about why
723 the thread is stopped, so a `resume_all' command would resume too
724 much. If/when GDB gains a way to tell the target `hold this
725 thread stopped until I say otherwise', then we can optimize
727 if (thread
->state
!= THREAD_STOPPED
)
730 switch_to_thread (thread
);
731 clear_proceed_status (0);
732 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
737 ensure_valid_thread (void)
739 if (inferior_ptid
== null_ptid
740 || inferior_thread ()->state
== THREAD_EXITED
)
741 error (_("Cannot execute this command without a live selected thread."));
744 /* If the user is looking at trace frames, any resumption of execution
745 is likely to mix up recorded and live target data. So simply
746 disallow those commands. */
749 ensure_not_tfind_mode (void)
751 if (get_traceframe_number () >= 0)
752 error (_("Cannot execute this command while looking at trace frames."));
755 /* Throw an error indicating the current thread is running. */
758 error_is_running (void)
760 error (_("Cannot execute this command while "
761 "the selected thread is running."));
764 /* Calls error_is_running if the current thread is running. */
767 ensure_not_running (void)
769 if (inferior_thread ()->state
== THREAD_RUNNING
)
774 continue_1 (int all_threads
)
777 ensure_not_tfind_mode ();
779 if (non_stop
&& all_threads
)
781 /* Don't error out if the current thread is running, because
782 there may be other stopped threads. */
784 /* Backup current thread and selected frame and restore on scope
786 scoped_restore_current_thread restore_thread
;
788 iterate_over_threads (proceed_thread_callback
, NULL
);
790 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
792 /* If all threads in the target were already running,
793 proceed_thread_callback ends up never calling proceed,
794 and so nothing calls this to put the inferior's terminal
795 settings in effect and remove stdin from the event loop,
796 which we must when running a foreground command. E.g.:
800 <all threads are running now>
803 <no thread was resumed, but the inferior now owns the terminal>
805 target_terminal::inferior ();
810 ensure_valid_thread ();
811 ensure_not_running ();
812 clear_proceed_status (0);
813 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
817 /* continue [-a] [proceed-count] [&] */
820 continue_command (const char *args
, int from_tty
)
827 /* Find out whether we must run in the background. */
828 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
829 args
= stripped
.get ();
833 if (startswith (args
, "-a"))
836 args
+= sizeof ("-a") - 1;
842 if (!non_stop
&& all_threads
)
843 error (_("`-a' is meaningless in all-stop mode."));
845 if (args
!= NULL
&& all_threads
)
846 error (_("Can't resume all threads and specify "
847 "proceed count simultaneously."));
849 /* If we have an argument left, set proceed count of breakpoint we
856 struct thread_info
*tp
;
859 tp
= inferior_thread ();
863 struct target_waitstatus ws
;
865 get_last_target_status (&last_ptid
, &ws
);
866 tp
= find_thread_ptid (last_ptid
);
869 bs
= tp
->control
.stop_bpstat
;
871 while ((stat
= bpstat_num (&bs
, &num
)) != 0)
874 set_ignore_count (num
,
875 parse_and_eval_long (args
) - 1,
877 /* set_ignore_count prints a message ending with a period.
878 So print two spaces before "Continuing.". */
880 printf_filtered (" ");
884 if (!stopped
&& from_tty
)
887 ("Not stopped at any breakpoint; argument ignored.\n");
892 ensure_not_tfind_mode ();
894 if (!non_stop
|| !all_threads
)
896 ensure_valid_thread ();
897 ensure_not_running ();
900 prepare_execution_command (current_top_target (), async_exec
);
903 printf_filtered (_("Continuing.\n"));
905 continue_1 (all_threads
);
908 /* Record the starting point of a "step" or "next" command. */
911 set_step_frame (void)
913 frame_info
*frame
= get_current_frame ();
915 symtab_and_line sal
= find_frame_sal (frame
);
916 set_step_info (frame
, sal
);
918 CORE_ADDR pc
= get_frame_pc (frame
);
919 thread_info
*tp
= inferior_thread ();
920 tp
->control
.step_start_function
= find_pc_function (pc
);
923 /* Step until outside of current statement. */
926 step_command (const char *count_string
, int from_tty
)
928 step_1 (0, 0, count_string
);
931 /* Likewise, but skip over subroutine calls as if single instructions. */
934 next_command (const char *count_string
, int from_tty
)
936 step_1 (1, 0, count_string
);
939 /* Likewise, but step only one instruction. */
942 stepi_command (const char *count_string
, int from_tty
)
944 step_1 (0, 1, count_string
);
948 nexti_command (const char *count_string
, int from_tty
)
950 step_1 (1, 1, count_string
);
953 /* Data for the FSM that manages the step/next/stepi/nexti
956 struct step_command_fsm
: public thread_fsm
958 /* How many steps left in a "step N"-like command. */
961 /* If true, this is a next/nexti, otherwise a step/stepi. */
962 int skip_subroutines
;
964 /* If true, this is a stepi/nexti, otherwise a step/step. */
967 explicit step_command_fsm (struct interp
*cmd_interp
)
968 : thread_fsm (cmd_interp
)
972 void clean_up (struct thread_info
*thread
) override
;
973 bool should_stop (struct thread_info
*thread
) override
;
974 enum async_reply_reason
do_async_reply_reason () override
;
977 /* Prepare for a step/next/etc. command. Any target resource
978 allocated here is undone in the FSM's clean_up method. */
981 step_command_fsm_prepare (struct step_command_fsm
*sm
,
982 int skip_subroutines
, int single_inst
,
983 int count
, struct thread_info
*thread
)
985 sm
->skip_subroutines
= skip_subroutines
;
986 sm
->single_inst
= single_inst
;
989 /* Leave the si command alone. */
990 if (!sm
->single_inst
|| sm
->skip_subroutines
)
991 set_longjmp_breakpoint (thread
, get_frame_id (get_current_frame ()));
993 thread
->control
.stepping_command
= 1;
996 static int prepare_one_step (struct step_command_fsm
*sm
);
999 step_1 (int skip_subroutines
, int single_inst
, const char *count_string
)
1003 struct thread_info
*thr
;
1004 struct step_command_fsm
*step_sm
;
1007 ensure_not_tfind_mode ();
1008 ensure_valid_thread ();
1009 ensure_not_running ();
1011 gdb::unique_xmalloc_ptr
<char> stripped
1012 = strip_bg_char (count_string
, &async_exec
);
1013 count_string
= stripped
.get ();
1015 prepare_execution_command (current_top_target (), async_exec
);
1017 count
= count_string
? parse_and_eval_long (count_string
) : 1;
1019 clear_proceed_status (1);
1021 /* Setup the execution command state machine to handle all the COUNT
1023 thr
= inferior_thread ();
1024 step_sm
= new step_command_fsm (command_interp ());
1025 thr
->thread_fsm
= step_sm
;
1027 step_command_fsm_prepare (step_sm
, skip_subroutines
,
1028 single_inst
, count
, thr
);
1030 /* Do only one step for now, before returning control to the event
1031 loop. Let the continuation figure out how many other steps we
1032 need to do, and handle them one at the time, through
1034 if (!prepare_one_step (step_sm
))
1035 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1040 /* Stepped into an inline frame. Pretend that we've
1042 thr
->thread_fsm
->clean_up (thr
);
1043 proceeded
= normal_stop ();
1045 inferior_event_handler (INF_EXEC_COMPLETE
, NULL
);
1046 all_uis_check_sync_execution_done ();
1050 /* Implementation of the 'should_stop' FSM method for stepping
1051 commands. Called after we are done with one step operation, to
1052 check whether we need to step again, before we print the prompt and
1053 return control to the user. If count is > 1, returns false, as we
1054 will need to keep going. */
1057 step_command_fsm::should_stop (struct thread_info
*tp
)
1059 if (tp
->control
.stop_step
)
1061 /* There are more steps to make, and we did stop due to
1062 ending a stepping range. Do another step. */
1064 return prepare_one_step (this);
1072 /* Implementation of the 'clean_up' FSM method for stepping commands. */
1075 step_command_fsm::clean_up (struct thread_info
*thread
)
1077 if (!single_inst
|| skip_subroutines
)
1078 delete_longjmp_breakpoint (thread
->global_num
);
1081 /* Implementation of the 'async_reply_reason' FSM method for stepping
1084 enum async_reply_reason
1085 step_command_fsm::do_async_reply_reason ()
1087 return EXEC_ASYNC_END_STEPPING_RANGE
;
1090 /* Prepare for one step in "step N". The actual target resumption is
1091 done by the caller. Return true if we're done and should thus
1092 report a stop to the user. Returns false if the target needs to be
1096 prepare_one_step (struct step_command_fsm
*sm
)
1100 struct frame_info
*frame
= get_current_frame ();
1102 /* Don't assume THREAD is a valid thread id. It is set to -1 if
1103 the longjmp breakpoint was not required. Use the
1104 INFERIOR_PTID thread instead, which is the same thread when
1106 struct thread_info
*tp
= inferior_thread ();
1110 if (!sm
->single_inst
)
1114 /* Step at an inlined function behaves like "down". */
1115 if (!sm
->skip_subroutines
1116 && inline_skipped_frames (tp
))
1120 /* Pretend that we've ran. */
1121 resume_ptid
= user_visible_resume_ptid (1);
1122 set_running (resume_ptid
, 1);
1124 step_into_inline_frame (tp
);
1126 return prepare_one_step (sm
);
1129 pc
= get_frame_pc (frame
);
1130 find_pc_line_pc_range (pc
,
1131 &tp
->control
.step_range_start
,
1132 &tp
->control
.step_range_end
);
1134 tp
->control
.may_range_step
= 1;
1136 /* If we have no line info, switch to stepi mode. */
1137 if (tp
->control
.step_range_end
== 0 && step_stop_if_no_debug
)
1139 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1140 tp
->control
.may_range_step
= 0;
1142 else if (tp
->control
.step_range_end
== 0)
1146 if (find_pc_partial_function (pc
, &name
,
1147 &tp
->control
.step_range_start
,
1148 &tp
->control
.step_range_end
) == 0)
1149 error (_("Cannot find bounds of current function"));
1151 target_terminal::ours_for_output ();
1152 printf_filtered (_("Single stepping until exit from function %s,"
1153 "\nwhich has no line number information.\n"),
1159 /* Say we are stepping, but stop after one insn whatever it does. */
1160 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1161 if (!sm
->skip_subroutines
)
1163 Don't step over function calls, not even to functions lacking
1165 tp
->control
.step_over_calls
= STEP_OVER_NONE
;
1168 if (sm
->skip_subroutines
)
1169 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1175 sm
->set_finished ();
1180 /* Continue program at specified address. */
1183 jump_command (const char *arg
, int from_tty
)
1185 struct gdbarch
*gdbarch
= get_current_arch ();
1192 ensure_not_tfind_mode ();
1193 ensure_valid_thread ();
1194 ensure_not_running ();
1196 /* Find out whether we must run in the background. */
1197 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1198 arg
= stripped
.get ();
1200 prepare_execution_command (current_top_target (), async_exec
);
1203 error_no_arg (_("starting address"));
1205 std::vector
<symtab_and_line
> sals
1206 = decode_line_with_last_displayed (arg
, DECODE_LINE_FUNFIRSTLINE
);
1207 if (sals
.size () != 1)
1208 error (_("Unreasonable jump request"));
1210 symtab_and_line
&sal
= sals
[0];
1212 if (sal
.symtab
== 0 && sal
.pc
== 0)
1213 error (_("No source file has been specified."));
1215 resolve_sal_pc (&sal
); /* May error out. */
1217 /* See if we are trying to jump to another function. */
1218 fn
= get_frame_function (get_current_frame ());
1219 sfn
= find_pc_function (sal
.pc
);
1220 if (fn
!= NULL
&& sfn
!= fn
)
1222 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal
.line
,
1223 SYMBOL_PRINT_NAME (fn
)))
1225 error (_("Not confirmed."));
1232 struct obj_section
*section
;
1234 fixup_symbol_section (sfn
, 0);
1235 section
= SYMBOL_OBJ_SECTION (symbol_objfile (sfn
), sfn
);
1236 if (section_is_overlay (section
)
1237 && !section_is_mapped (section
))
1239 if (!query (_("WARNING!!! Destination is in "
1240 "unmapped overlay! Jump anyway? ")))
1242 error (_("Not confirmed."));
1252 printf_filtered (_("Continuing at "));
1253 fputs_filtered (paddress (gdbarch
, addr
), gdb_stdout
);
1254 printf_filtered (".\n");
1257 clear_proceed_status (0);
1258 proceed (addr
, GDB_SIGNAL_0
);
1261 /* Continue program giving it specified signal. */
1264 signal_command (const char *signum_exp
, int from_tty
)
1266 enum gdb_signal oursig
;
1269 dont_repeat (); /* Too dangerous. */
1271 ensure_not_tfind_mode ();
1272 ensure_valid_thread ();
1273 ensure_not_running ();
1275 /* Find out whether we must run in the background. */
1276 gdb::unique_xmalloc_ptr
<char> stripped
1277 = strip_bg_char (signum_exp
, &async_exec
);
1278 signum_exp
= stripped
.get ();
1280 prepare_execution_command (current_top_target (), async_exec
);
1283 error_no_arg (_("signal number"));
1285 /* It would be even slicker to make signal names be valid expressions,
1286 (the type could be "enum $signal" or some such), then the user could
1287 assign them to convenience variables. */
1288 oursig
= gdb_signal_from_name (signum_exp
);
1290 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1292 /* No, try numeric. */
1293 int num
= parse_and_eval_long (signum_exp
);
1296 oursig
= GDB_SIGNAL_0
;
1298 oursig
= gdb_signal_from_command (num
);
1301 /* Look for threads other than the current that this command ends up
1302 resuming too (due to schedlock off), and warn if they'll get a
1303 signal delivered. "signal 0" is used to suppress a previous
1304 signal, but if the current thread is no longer the one that got
1305 the signal, then the user is potentially suppressing the signal
1306 of the wrong thread. */
1309 int must_confirm
= 0;
1311 /* This indicates what will be resumed. Either a single thread,
1312 a whole process, or all threads of all processes. */
1313 ptid_t resume_ptid
= user_visible_resume_ptid (0);
1315 for (thread_info
*tp
: all_non_exited_threads (resume_ptid
))
1317 if (tp
->ptid
== inferior_ptid
)
1320 if (tp
->suspend
.stop_signal
!= GDB_SIGNAL_0
1321 && signal_pass_state (tp
->suspend
.stop_signal
))
1324 printf_unfiltered (_("Note:\n"));
1325 printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"),
1326 print_thread_id (tp
),
1327 gdb_signal_to_name (tp
->suspend
.stop_signal
),
1328 gdb_signal_to_string (tp
->suspend
.stop_signal
));
1334 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1335 "still deliver the signals noted above to their respective threads.\n"
1336 "Continue anyway? "),
1337 print_thread_id (inferior_thread ())))
1338 error (_("Not confirmed."));
1343 if (oursig
== GDB_SIGNAL_0
)
1344 printf_filtered (_("Continuing with no signal.\n"));
1346 printf_filtered (_("Continuing with signal %s.\n"),
1347 gdb_signal_to_name (oursig
));
1350 clear_proceed_status (0);
1351 proceed ((CORE_ADDR
) -1, oursig
);
1354 /* Queue a signal to be delivered to the current thread. */
1357 queue_signal_command (const char *signum_exp
, int from_tty
)
1359 enum gdb_signal oursig
;
1360 struct thread_info
*tp
;
1363 ensure_not_tfind_mode ();
1364 ensure_valid_thread ();
1365 ensure_not_running ();
1367 if (signum_exp
== NULL
)
1368 error_no_arg (_("signal number"));
1370 /* It would be even slicker to make signal names be valid expressions,
1371 (the type could be "enum $signal" or some such), then the user could
1372 assign them to convenience variables. */
1373 oursig
= gdb_signal_from_name (signum_exp
);
1375 if (oursig
== GDB_SIGNAL_UNKNOWN
)
1377 /* No, try numeric. */
1378 int num
= parse_and_eval_long (signum_exp
);
1381 oursig
= GDB_SIGNAL_0
;
1383 oursig
= gdb_signal_from_command (num
);
1386 if (oursig
!= GDB_SIGNAL_0
1387 && !signal_pass_state (oursig
))
1388 error (_("Signal handling set to not pass this signal to the program."));
1390 tp
= inferior_thread ();
1391 tp
->suspend
.stop_signal
= oursig
;
1394 /* Data for the FSM that manages the until (with no argument)
1397 struct until_next_fsm
: public thread_fsm
1399 /* The thread that as current when the command was executed. */
1402 until_next_fsm (struct interp
*cmd_interp
, int thread
)
1403 : thread_fsm (cmd_interp
),
1408 bool should_stop (struct thread_info
*thread
) override
;
1409 void clean_up (struct thread_info
*thread
) override
;
1410 enum async_reply_reason
do_async_reply_reason () override
;
1413 /* Implementation of the 'should_stop' FSM method for the until (with
1417 until_next_fsm::should_stop (struct thread_info
*tp
)
1419 if (tp
->control
.stop_step
)
1425 /* Implementation of the 'clean_up' FSM method for the until (with no
1429 until_next_fsm::clean_up (struct thread_info
*thread
)
1431 delete_longjmp_breakpoint (thread
->global_num
);
1434 /* Implementation of the 'async_reply_reason' FSM method for the until
1435 (with no arg) command. */
1437 enum async_reply_reason
1438 until_next_fsm::do_async_reply_reason ()
1440 return EXEC_ASYNC_END_STEPPING_RANGE
;
1443 /* Proceed until we reach a different source line with pc greater than
1444 our current one or exit the function. We skip calls in both cases.
1446 Note that eventually this command should probably be changed so
1447 that only source lines are printed out when we hit the breakpoint
1448 we set. This may involve changes to wait_for_inferior and the
1449 proceed status code. */
1452 until_next_command (int from_tty
)
1454 struct frame_info
*frame
;
1456 struct symbol
*func
;
1457 struct symtab_and_line sal
;
1458 struct thread_info
*tp
= inferior_thread ();
1459 int thread
= tp
->global_num
;
1460 struct until_next_fsm
*sm
;
1462 clear_proceed_status (0);
1465 frame
= get_current_frame ();
1467 /* Step until either exited from this function or greater
1468 than the current line (if in symbolic section) or pc (if
1471 pc
= get_frame_pc (frame
);
1472 func
= find_pc_function (pc
);
1476 struct bound_minimal_symbol msymbol
= lookup_minimal_symbol_by_pc (pc
);
1478 if (msymbol
.minsym
== NULL
)
1479 error (_("Execution is not within a known function."));
1481 tp
->control
.step_range_start
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1482 /* The upper-bound of step_range is exclusive. In order to make PC
1483 within the range, set the step_range_end with PC + 1. */
1484 tp
->control
.step_range_end
= pc
+ 1;
1488 sal
= find_pc_line (pc
, 0);
1490 tp
->control
.step_range_start
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func
));
1491 tp
->control
.step_range_end
= sal
.end
;
1493 tp
->control
.may_range_step
= 1;
1495 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1497 set_longjmp_breakpoint (tp
, get_frame_id (frame
));
1498 delete_longjmp_breakpoint_cleanup
lj_deleter (thread
);
1500 sm
= new until_next_fsm (command_interp (), tp
->global_num
);
1501 tp
->thread_fsm
= sm
;
1502 lj_deleter
.release ();
1504 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1508 until_command (const char *arg
, int from_tty
)
1513 ensure_not_tfind_mode ();
1514 ensure_valid_thread ();
1515 ensure_not_running ();
1517 /* Find out whether we must run in the background. */
1518 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1519 arg
= stripped
.get ();
1521 prepare_execution_command (current_top_target (), async_exec
);
1524 until_break_command (arg
, from_tty
, 0);
1526 until_next_command (from_tty
);
1530 advance_command (const char *arg
, int from_tty
)
1535 ensure_not_tfind_mode ();
1536 ensure_valid_thread ();
1537 ensure_not_running ();
1540 error_no_arg (_("a location"));
1542 /* Find out whether we must run in the background. */
1543 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1544 arg
= stripped
.get ();
1546 prepare_execution_command (current_top_target (), async_exec
);
1548 until_break_command (arg
, from_tty
, 1);
1551 /* Return the value of the result of a function at the end of a 'finish'
1552 command/BP. DTOR_DATA (if not NULL) can represent inferior registers
1553 right after an inferior call has finished. */
1556 get_return_value (struct value
*function
, struct type
*value_type
)
1558 regcache
*stop_regs
= get_current_regcache ();
1559 struct gdbarch
*gdbarch
= stop_regs
->arch ();
1560 struct value
*value
;
1562 value_type
= check_typedef (value_type
);
1563 gdb_assert (TYPE_CODE (value_type
) != TYPE_CODE_VOID
);
1565 /* FIXME: 2003-09-27: When returning from a nested inferior function
1566 call, it's possible (with no help from the architecture vector)
1567 to locate and return/print a "struct return" value. This is just
1568 a more complicated case of what is already being done in the
1569 inferior function call code. In fact, when inferior function
1570 calls are made async, this will likely be made the norm. */
1572 switch (gdbarch_return_value (gdbarch
, function
, value_type
,
1575 case RETURN_VALUE_REGISTER_CONVENTION
:
1576 case RETURN_VALUE_ABI_RETURNS_ADDRESS
:
1577 case RETURN_VALUE_ABI_PRESERVES_ADDRESS
:
1578 value
= allocate_value (value_type
);
1579 gdbarch_return_value (gdbarch
, function
, value_type
, stop_regs
,
1580 value_contents_raw (value
), NULL
);
1582 case RETURN_VALUE_STRUCT_CONVENTION
:
1586 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1592 /* The captured function return value/type and its position in the
1595 struct return_value_info
1597 /* The captured return value. May be NULL if we weren't able to
1598 retrieve it. See get_return_value. */
1599 struct value
*value
;
1601 /* The return type. In some cases, we'll not be able extract the
1602 return value, but we always know the type. */
1605 /* If we captured a value, this is the value history index. */
1606 int value_history_index
;
1609 /* Helper for print_return_value. */
1612 print_return_value_1 (struct ui_out
*uiout
, struct return_value_info
*rv
)
1614 if (rv
->value
!= NULL
)
1616 struct value_print_options opts
;
1619 uiout
->text ("Value returned is ");
1620 uiout
->field_fmt ("gdb-result-var", "$%d",
1621 rv
->value_history_index
);
1622 uiout
->text (" = ");
1623 get_user_print_options (&opts
);
1625 if (opts
.finish_print
)
1628 value_print (rv
->value
, &stb
, &opts
);
1629 uiout
->field_stream ("return-value", stb
);
1632 uiout
->field_string ("return-value", _("<not displayed>"),
1633 metadata_style
.style ());
1638 std::string type_name
= type_to_string (rv
->type
);
1639 uiout
->text ("Value returned has type: ");
1640 uiout
->field_string ("return-type", type_name
.c_str ());
1642 uiout
->text (" Cannot determine contents\n");
1646 /* Print the result of a function at the end of a 'finish' command.
1647 RV points at an object representing the captured return value/type
1648 and its position in the value history. */
1651 print_return_value (struct ui_out
*uiout
, struct return_value_info
*rv
)
1653 if (rv
->type
== NULL
1654 || TYPE_CODE (check_typedef (rv
->type
)) == TYPE_CODE_VOID
)
1659 /* print_return_value_1 can throw an exception in some
1660 circumstances. We need to catch this so that we still
1661 delete the breakpoint. */
1662 print_return_value_1 (uiout
, rv
);
1664 catch (const gdb_exception
&ex
)
1666 exception_print (gdb_stdout
, ex
);
1670 /* Data for the FSM that manages the finish command. */
1672 struct finish_command_fsm
: public thread_fsm
1674 /* The momentary breakpoint set at the function's return address in
1676 breakpoint_up breakpoint
;
1678 /* The function that we're stepping out of. */
1679 struct symbol
*function
= nullptr;
1681 /* If the FSM finishes successfully, this stores the function's
1683 struct return_value_info return_value_info
{};
1685 explicit finish_command_fsm (struct interp
*cmd_interp
)
1686 : thread_fsm (cmd_interp
)
1690 bool should_stop (struct thread_info
*thread
) override
;
1691 void clean_up (struct thread_info
*thread
) override
;
1692 struct return_value_info
*return_value () override
;
1693 enum async_reply_reason
do_async_reply_reason () override
;
1696 /* Implementation of the 'should_stop' FSM method for the finish
1697 commands. Detects whether the thread stepped out of the function
1698 successfully, and if so, captures the function's return value and
1699 marks the FSM finished. */
1702 finish_command_fsm::should_stop (struct thread_info
*tp
)
1704 struct return_value_info
*rv
= &return_value_info
;
1706 if (function
!= NULL
1707 && bpstat_find_breakpoint (tp
->control
.stop_bpstat
,
1708 breakpoint
.get ()) != NULL
)
1713 rv
->type
= TYPE_TARGET_TYPE (SYMBOL_TYPE (function
));
1714 if (rv
->type
== NULL
)
1715 internal_error (__FILE__
, __LINE__
,
1716 _("finish_command: function has no target type"));
1718 if (TYPE_CODE (check_typedef (rv
->type
)) != TYPE_CODE_VOID
)
1722 func
= read_var_value (function
, NULL
, get_current_frame ());
1723 rv
->value
= get_return_value (func
, rv
->type
);
1724 if (rv
->value
!= NULL
)
1725 rv
->value_history_index
= record_latest_value (rv
->value
);
1728 else if (tp
->control
.stop_step
)
1730 /* Finishing from an inline frame, or reverse finishing. In
1731 either case, there's no way to retrieve the return value. */
1738 /* Implementation of the 'clean_up' FSM method for the finish
1742 finish_command_fsm::clean_up (struct thread_info
*thread
)
1744 breakpoint
.reset ();
1745 delete_longjmp_breakpoint (thread
->global_num
);
1748 /* Implementation of the 'return_value' FSM method for the finish
1751 struct return_value_info
*
1752 finish_command_fsm::return_value ()
1754 return &return_value_info
;
1757 /* Implementation of the 'async_reply_reason' FSM method for the
1760 enum async_reply_reason
1761 finish_command_fsm::do_async_reply_reason ()
1763 if (execution_direction
== EXEC_REVERSE
)
1764 return EXEC_ASYNC_END_STEPPING_RANGE
;
1766 return EXEC_ASYNC_FUNCTION_FINISHED
;
1769 /* finish_backward -- helper function for finish_command. */
1772 finish_backward (struct finish_command_fsm
*sm
)
1774 struct symtab_and_line sal
;
1775 struct thread_info
*tp
= inferior_thread ();
1777 CORE_ADDR func_addr
;
1779 pc
= get_frame_pc (get_current_frame ());
1781 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
) == 0)
1782 error (_("Cannot find bounds of current function"));
1784 sal
= find_pc_line (func_addr
, 0);
1786 tp
->control
.proceed_to_finish
= 1;
1787 /* Special case: if we're sitting at the function entry point,
1788 then all we need to do is take a reverse singlestep. We
1789 don't need to set a breakpoint, and indeed it would do us
1792 Note that this can only happen at frame #0, since there's
1793 no way that a function up the stack can have a return address
1794 that's equal to its entry point. */
1798 struct frame_info
*frame
= get_selected_frame (NULL
);
1799 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1801 /* Set a step-resume at the function's entry point. Once that's
1802 hit, we'll do one more step backwards. */
1803 symtab_and_line sr_sal
;
1805 sr_sal
.pspace
= get_frame_program_space (frame
);
1806 insert_step_resume_breakpoint_at_sal (gdbarch
,
1807 sr_sal
, null_frame_id
);
1809 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1813 /* We're almost there -- we just need to back up by one more
1815 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
1816 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1820 /* finish_forward -- helper function for finish_command. FRAME is the
1821 frame that called the function we're about to step out of. */
1824 finish_forward (struct finish_command_fsm
*sm
, struct frame_info
*frame
)
1826 struct frame_id frame_id
= get_frame_id (frame
);
1827 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1828 struct symtab_and_line sal
;
1829 struct thread_info
*tp
= inferior_thread ();
1831 sal
= find_pc_line (get_frame_pc (frame
), 0);
1832 sal
.pc
= get_frame_pc (frame
);
1834 sm
->breakpoint
= set_momentary_breakpoint (gdbarch
, sal
,
1835 get_stack_frame_id (frame
),
1838 /* set_momentary_breakpoint invalidates FRAME. */
1841 set_longjmp_breakpoint (tp
, frame_id
);
1843 /* We want to print return value, please... */
1844 tp
->control
.proceed_to_finish
= 1;
1846 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1849 /* Skip frames for "finish". */
1851 static struct frame_info
*
1852 skip_finish_frames (struct frame_info
*frame
)
1854 struct frame_info
*start
;
1860 frame
= skip_tailcall_frames (frame
);
1864 frame
= skip_unwritable_frames (frame
);
1868 while (start
!= frame
);
1873 /* "finish": Set a temporary breakpoint at the place the selected
1874 frame will return to, then continue. */
1877 finish_command (const char *arg
, int from_tty
)
1879 struct frame_info
*frame
;
1881 struct finish_command_fsm
*sm
;
1882 struct thread_info
*tp
;
1885 ensure_not_tfind_mode ();
1886 ensure_valid_thread ();
1887 ensure_not_running ();
1889 /* Find out whether we must run in the background. */
1890 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (arg
, &async_exec
);
1891 arg
= stripped
.get ();
1893 prepare_execution_command (current_top_target (), async_exec
);
1896 error (_("The \"finish\" command does not take any arguments."));
1898 frame
= get_prev_frame (get_selected_frame (_("No selected frame.")));
1900 error (_("\"finish\" not meaningful in the outermost frame."));
1902 clear_proceed_status (0);
1904 tp
= inferior_thread ();
1906 sm
= new finish_command_fsm (command_interp ());
1908 tp
->thread_fsm
= sm
;
1910 /* Finishing from an inline frame is completely different. We don't
1911 try to show the "return value" - no way to locate it. */
1912 if (get_frame_type (get_selected_frame (_("No selected frame.")))
1915 /* Claim we are stepping in the calling frame. An empty step
1916 range means that we will stop once we aren't in a function
1917 called by that frame. We don't use the magic "1" value for
1918 step_range_end, because then infrun will think this is nexti,
1919 and not step over the rest of this inlined function call. */
1920 set_step_info (frame
, {});
1921 tp
->control
.step_range_start
= get_frame_pc (frame
);
1922 tp
->control
.step_range_end
= tp
->control
.step_range_start
;
1923 tp
->control
.step_over_calls
= STEP_OVER_ALL
;
1925 /* Print info on the selected frame, including level number but not
1929 printf_filtered (_("Run till exit from "));
1930 print_stack_frame (get_selected_frame (NULL
), 1, LOCATION
, 0);
1933 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1937 /* Find the function we will return from. */
1939 sm
->function
= find_pc_function (get_frame_pc (get_selected_frame (NULL
)));
1941 /* Print info on the selected frame, including level number but not
1945 if (execution_direction
== EXEC_REVERSE
)
1946 printf_filtered (_("Run back to call of "));
1949 if (sm
->function
!= NULL
&& TYPE_NO_RETURN (sm
->function
->type
)
1950 && !query (_("warning: Function %s does not return normally.\n"
1951 "Try to finish anyway? "),
1952 SYMBOL_PRINT_NAME (sm
->function
)))
1953 error (_("Not confirmed."));
1954 printf_filtered (_("Run till exit from "));
1957 print_stack_frame (get_selected_frame (NULL
), 1, LOCATION
, 0);
1960 if (execution_direction
== EXEC_REVERSE
)
1961 finish_backward (sm
);
1964 frame
= skip_finish_frames (frame
);
1967 error (_("Cannot find the caller frame."));
1969 finish_forward (sm
, frame
);
1975 info_program_command (const char *args
, int from_tty
)
1981 if (!target_has_execution
)
1983 printf_filtered (_("The program being debugged is not being run.\n"));
1988 ptid
= inferior_ptid
;
1991 struct target_waitstatus ws
;
1993 get_last_target_status (&ptid
, &ws
);
1996 if (ptid
== null_ptid
|| ptid
== minus_one_ptid
)
1997 error (_("No selected thread."));
1999 thread_info
*tp
= find_thread_ptid (ptid
);
2001 if (tp
->state
== THREAD_EXITED
)
2002 error (_("Invalid selected thread."));
2003 else if (tp
->state
== THREAD_RUNNING
)
2004 error (_("Selected thread is running."));
2006 bs
= tp
->control
.stop_bpstat
;
2007 stat
= bpstat_num (&bs
, &num
);
2009 target_files_info ();
2010 printf_filtered (_("Program stopped at %s.\n"),
2011 paddress (target_gdbarch (), tp
->suspend
.stop_pc
));
2012 if (tp
->control
.stop_step
)
2013 printf_filtered (_("It stopped after being stepped.\n"));
2016 /* There may be several breakpoints in the same place, so this
2017 isn't as strange as it seems. */
2022 printf_filtered (_("It stopped at a breakpoint "
2023 "that has since been deleted.\n"));
2026 printf_filtered (_("It stopped at breakpoint %d.\n"), num
);
2027 stat
= bpstat_num (&bs
, &num
);
2030 else if (tp
->suspend
.stop_signal
!= GDB_SIGNAL_0
)
2032 printf_filtered (_("It stopped with signal %s, %s.\n"),
2033 gdb_signal_to_name (tp
->suspend
.stop_signal
),
2034 gdb_signal_to_string (tp
->suspend
.stop_signal
));
2039 printf_filtered (_("Type \"info stack\" or \"info "
2040 "registers\" for more information.\n"));
2045 environment_info (const char *var
, int from_tty
)
2049 const char *val
= current_inferior ()->environment
.get (var
);
2053 puts_filtered (var
);
2054 puts_filtered (" = ");
2055 puts_filtered (val
);
2056 puts_filtered ("\n");
2060 puts_filtered ("Environment variable \"");
2061 puts_filtered (var
);
2062 puts_filtered ("\" not defined.\n");
2067 char **envp
= current_inferior ()->environment
.envp ();
2069 for (int idx
= 0; envp
[idx
] != NULL
; ++idx
)
2071 puts_filtered (envp
[idx
]);
2072 puts_filtered ("\n");
2078 set_environment_command (const char *arg
, int from_tty
)
2080 const char *p
, *val
;
2084 error_no_arg (_("environment variable and value"));
2086 /* Find seperation between variable name and value. */
2087 p
= (char *) strchr (arg
, '=');
2088 val
= (char *) strchr (arg
, ' ');
2090 if (p
!= 0 && val
!= 0)
2092 /* We have both a space and an equals. If the space is before the
2093 equals, walk forward over the spaces til we see a nonspace
2094 (possibly the equals). */
2099 /* Now if the = is after the char following the spaces,
2100 take the char following the spaces. */
2104 else if (val
!= 0 && p
== 0)
2108 error_no_arg (_("environment variable to set"));
2110 if (p
== 0 || p
[1] == 0)
2114 p
= arg
+ strlen (arg
); /* So that savestring below will work. */
2118 /* Not setting variable value to null. */
2120 while (*val
== ' ' || *val
== '\t')
2124 while (p
!= arg
&& (p
[-1] == ' ' || p
[-1] == '\t'))
2127 std::string
var (arg
, p
- arg
);
2130 printf_filtered (_("Setting environment variable "
2131 "\"%s\" to null value.\n"),
2133 current_inferior ()->environment
.set (var
.c_str (), "");
2136 current_inferior ()->environment
.set (var
.c_str (), val
);
2140 unset_environment_command (const char *var
, int from_tty
)
2144 /* If there is no argument, delete all environment variables.
2145 Ask for confirmation if reading from the terminal. */
2146 if (!from_tty
|| query (_("Delete all environment variables? ")))
2147 current_inferior ()->environment
.clear ();
2150 current_inferior ()->environment
.unset (var
);
2153 /* Handle the execution path (PATH variable). */
2155 static const char path_var_name
[] = "PATH";
2158 path_info (const char *args
, int from_tty
)
2160 puts_filtered ("Executable and object file path: ");
2161 puts_filtered (current_inferior ()->environment
.get (path_var_name
));
2162 puts_filtered ("\n");
2165 /* Add zero or more directories to the front of the execution path. */
2168 path_command (const char *dirname
, int from_tty
)
2174 env
= current_inferior ()->environment
.get (path_var_name
);
2175 /* Can be null if path is not set. */
2178 exec_path
= xstrdup (env
);
2179 mod_path (dirname
, &exec_path
);
2180 current_inferior ()->environment
.set (path_var_name
, exec_path
);
2183 path_info (NULL
, from_tty
);
2188 pad_to_column (string_file
&stream
, int col
)
2190 /* At least one space must be printed to separate columns. */
2192 const int size
= stream
.size ();
2194 stream
.puts (n_spaces (col
- size
));
2197 /* Print out the register NAME with value VAL, to FILE, in the default
2201 default_print_one_register_info (struct ui_file
*file
,
2205 struct type
*regtype
= value_type (val
);
2206 int print_raw_format
;
2207 string_file format_stream
;
2210 value_column_1
= 15,
2211 /* Give enough room for "0x", 16 hex digits and two spaces in
2212 preceding column. */
2213 value_column_2
= value_column_1
+ 2 + 16 + 2,
2216 format_stream
.puts (name
);
2217 pad_to_column (format_stream
, value_column_1
);
2219 print_raw_format
= (value_entirely_available (val
)
2220 && !value_optimized_out (val
));
2222 /* If virtual format is floating, print it that way, and in raw
2224 if (TYPE_CODE (regtype
) == TYPE_CODE_FLT
2225 || TYPE_CODE (regtype
) == TYPE_CODE_DECFLOAT
)
2227 struct value_print_options opts
;
2228 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
2229 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (regtype
));
2231 get_user_print_options (&opts
);
2235 value_embedded_offset (val
), 0,
2236 &format_stream
, 0, val
, &opts
, current_language
);
2238 if (print_raw_format
)
2240 pad_to_column (format_stream
, value_column_2
);
2241 format_stream
.puts ("(raw ");
2242 print_hex_chars (&format_stream
, valaddr
, TYPE_LENGTH (regtype
),
2244 format_stream
.putc (')');
2249 struct value_print_options opts
;
2251 /* Print the register in hex. */
2252 get_formatted_print_options (&opts
, 'x');
2255 value_embedded_offset (val
), 0,
2256 &format_stream
, 0, val
, &opts
, current_language
);
2257 /* If not a vector register, print it also according to its
2259 if (print_raw_format
&& TYPE_VECTOR (regtype
) == 0)
2261 pad_to_column (format_stream
, value_column_2
);
2262 get_user_print_options (&opts
);
2265 value_embedded_offset (val
), 0,
2266 &format_stream
, 0, val
, &opts
, current_language
);
2270 fputs_filtered (format_stream
.c_str (), file
);
2271 fprintf_filtered (file
, "\n");
2274 /* Print out the machine register regnum. If regnum is -1, print all
2275 registers (print_all == 1) or all non-float and non-vector
2276 registers (print_all == 0).
2278 For most machines, having all_registers_info() print the
2279 register(s) one per line is good enough. If a different format is
2280 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2281 regs), or there is an existing convention for showing all the
2282 registers, define the architecture method PRINT_REGISTERS_INFO to
2283 provide that format. */
2286 default_print_registers_info (struct gdbarch
*gdbarch
,
2287 struct ui_file
*file
,
2288 struct frame_info
*frame
,
2289 int regnum
, int print_all
)
2292 const int numregs
= gdbarch_num_cooked_regs (gdbarch
);
2294 for (i
= 0; i
< numregs
; i
++)
2296 /* Decide between printing all regs, non-float / vector regs, or
2302 if (!gdbarch_register_reggroup_p (gdbarch
, i
, all_reggroup
))
2307 if (!gdbarch_register_reggroup_p (gdbarch
, i
, general_reggroup
))
2317 /* If the register name is empty, it is undefined for this
2318 processor, so don't display anything. */
2319 if (gdbarch_register_name (gdbarch
, i
) == NULL
2320 || *(gdbarch_register_name (gdbarch
, i
)) == '\0')
2323 default_print_one_register_info (file
,
2324 gdbarch_register_name (gdbarch
, i
),
2325 value_of_register (i
, frame
));
2330 registers_info (const char *addr_exp
, int fpregs
)
2332 struct frame_info
*frame
;
2333 struct gdbarch
*gdbarch
;
2335 if (!target_has_registers
)
2336 error (_("The program has no registers now."));
2337 frame
= get_selected_frame (NULL
);
2338 gdbarch
= get_frame_arch (frame
);
2342 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2347 while (*addr_exp
!= '\0')
2352 /* Skip leading white space. */
2353 addr_exp
= skip_spaces (addr_exp
);
2355 /* Discard any leading ``$''. Check that there is something
2356 resembling a register following it. */
2357 if (addr_exp
[0] == '$')
2359 if (isspace ((*addr_exp
)) || (*addr_exp
) == '\0')
2360 error (_("Missing register name"));
2362 /* Find the start/end of this register name/num/group. */
2364 while ((*addr_exp
) != '\0' && !isspace ((*addr_exp
)))
2368 /* Figure out what we've found and display it. */
2370 /* A register name? */
2372 int regnum
= user_reg_map_name_to_regnum (gdbarch
, start
, end
- start
);
2376 /* User registers lie completely outside of the range of
2377 normal registers. Catch them early so that the target
2379 if (regnum
>= gdbarch_num_cooked_regs (gdbarch
))
2381 struct value
*regval
= value_of_user_reg (regnum
, frame
);
2382 const char *regname
= user_reg_map_regnum_to_name (gdbarch
,
2385 /* Print in the same fashion
2386 gdbarch_print_registers_info's default
2387 implementation prints. */
2388 default_print_one_register_info (gdb_stdout
,
2393 gdbarch_print_registers_info (gdbarch
, gdb_stdout
,
2394 frame
, regnum
, fpregs
);
2399 /* A register group? */
2401 struct reggroup
*group
;
2403 for (group
= reggroup_next (gdbarch
, NULL
);
2405 group
= reggroup_next (gdbarch
, group
))
2407 /* Don't bother with a length check. Should the user
2408 enter a short register group name, go with the first
2409 group that matches. */
2410 if (strncmp (start
, reggroup_name (group
), end
- start
) == 0)
2418 regnum
< gdbarch_num_cooked_regs (gdbarch
);
2421 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
2422 gdbarch_print_registers_info (gdbarch
,
2430 /* Nothing matched. */
2431 error (_("Invalid register `%.*s'"), (int) (end
- start
), start
);
2436 info_all_registers_command (const char *addr_exp
, int from_tty
)
2438 registers_info (addr_exp
, 1);
2442 info_registers_command (const char *addr_exp
, int from_tty
)
2444 registers_info (addr_exp
, 0);
2448 print_vector_info (struct ui_file
*file
,
2449 struct frame_info
*frame
, const char *args
)
2451 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
2453 if (gdbarch_print_vector_info_p (gdbarch
))
2454 gdbarch_print_vector_info (gdbarch
, file
, frame
, args
);
2458 int printed_something
= 0;
2460 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2462 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, vector_reggroup
))
2464 printed_something
= 1;
2465 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2468 if (!printed_something
)
2469 fprintf_filtered (file
, "No vector information\n");
2474 info_vector_command (const char *args
, int from_tty
)
2476 if (!target_has_registers
)
2477 error (_("The program has no registers now."));
2479 print_vector_info (gdb_stdout
, get_selected_frame (NULL
), args
);
2482 /* Kill the inferior process. Make us have no inferior. */
2485 kill_command (const char *arg
, int from_tty
)
2487 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2488 It should be a distinct flag that indicates that a target is active, cuz
2489 some targets don't have processes! */
2491 if (inferior_ptid
== null_ptid
)
2492 error (_("The program is not being run."));
2493 if (!query (_("Kill the program being debugged? ")))
2494 error (_("Not confirmed."));
2496 int pid
= current_inferior ()->pid
;
2497 /* Save the pid as a string before killing the inferior, since that
2498 may unpush the current target, and we need the string after. */
2499 std::string pid_str
= target_pid_to_str (ptid_t (pid
));
2500 int infnum
= current_inferior ()->num
;
2504 if (print_inferior_events
)
2505 printf_unfiltered (_("[Inferior %d (%s) killed]\n"),
2506 infnum
, pid_str
.c_str ());
2508 /* If we still have other inferiors to debug, then don't mess with
2509 with their threads. */
2510 if (!have_inferiors ())
2512 init_thread_list (); /* Destroy thread info. */
2514 /* Killing off the inferior can leave us with a core file. If
2515 so, print the state we are left in. */
2516 if (target_has_stack
)
2518 printf_filtered (_("In %s,\n"), target_longname
);
2519 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
2522 bfd_cache_close_all ();
2525 /* Used in `attach&' command. Proceed threads of inferior INF iff
2526 they stopped due to debugger request, and when they did, they
2527 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2528 have been explicitly been told to stop. */
2531 proceed_after_attach (inferior
*inf
)
2533 /* Don't error out if the current thread is running, because
2534 there may be other stopped threads. */
2536 /* Backup current thread and selected frame. */
2537 scoped_restore_current_thread restore_thread
;
2539 for (thread_info
*thread
: inf
->non_exited_threads ())
2540 if (!thread
->executing
2541 && !thread
->stop_requested
2542 && thread
->suspend
.stop_signal
== GDB_SIGNAL_0
)
2544 switch_to_thread (thread
);
2545 clear_proceed_status (0);
2546 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2550 /* See inferior.h. */
2553 setup_inferior (int from_tty
)
2555 struct inferior
*inferior
;
2557 inferior
= current_inferior ();
2558 inferior
->needs_setup
= 0;
2560 /* If no exec file is yet known, try to determine it from the
2562 if (get_exec_file (0) == NULL
)
2563 exec_file_locate_attach (inferior_ptid
.pid (), 1, from_tty
);
2566 reopen_exec_file ();
2570 /* Take any necessary post-attaching actions for this platform. */
2571 target_post_attach (inferior_ptid
.pid ());
2573 post_create_inferior (current_top_target (), from_tty
);
2576 /* What to do after the first program stops after attaching. */
2577 enum attach_post_wait_mode
2579 /* Do nothing. Leaves threads as they are. */
2580 ATTACH_POST_WAIT_NOTHING
,
2582 /* Re-resume threads that are marked running. */
2583 ATTACH_POST_WAIT_RESUME
,
2585 /* Stop all threads. */
2586 ATTACH_POST_WAIT_STOP
,
2589 /* Called after we've attached to a process and we've seen it stop for
2590 the first time. If ASYNC_EXEC is true, re-resume threads that
2591 should be running. Else if ATTACH, */
2594 attach_post_wait (const char *args
, int from_tty
, enum attach_post_wait_mode mode
)
2596 struct inferior
*inferior
;
2598 inferior
= current_inferior ();
2599 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
2601 if (inferior
->needs_setup
)
2602 setup_inferior (from_tty
);
2604 if (mode
== ATTACH_POST_WAIT_RESUME
)
2606 /* The user requested an `attach&', so be sure to leave threads
2607 that didn't get a signal running. */
2609 /* Immediatelly resume all suspended threads of this inferior,
2610 and this inferior only. This should have no effect on
2611 already running threads. If a thread has been stopped with a
2612 signal, leave it be. */
2614 proceed_after_attach (inferior
);
2617 if (inferior_thread ()->suspend
.stop_signal
== GDB_SIGNAL_0
)
2619 clear_proceed_status (0);
2620 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
2624 else if (mode
== ATTACH_POST_WAIT_STOP
)
2626 /* The user requested a plain `attach', so be sure to leave
2627 the inferior stopped. */
2629 /* At least the current thread is already stopped. */
2631 /* In all-stop, by definition, all threads have to be already
2632 stopped at this point. In non-stop, however, although the
2633 selected thread is stopped, others may still be executing.
2634 Be sure to explicitly stop all threads of the process. This
2635 should have no effect on already stopped threads. */
2637 target_stop (ptid_t (inferior
->pid
));
2638 else if (target_is_non_stop_p ())
2640 struct thread_info
*lowest
= inferior_thread ();
2642 stop_all_threads ();
2644 /* It's not defined which thread will report the attach
2645 stop. For consistency, always select the thread with
2646 lowest GDB number, which should be the main thread, if it
2648 for (thread_info
*thread
: current_inferior ()->non_exited_threads ())
2649 if (thread
->inf
->num
< lowest
->inf
->num
2650 || thread
->per_inf_num
< lowest
->per_inf_num
)
2653 switch_to_thread (lowest
);
2656 /* Tell the user/frontend where we're stopped. */
2658 if (deprecated_attach_hook
)
2659 deprecated_attach_hook ();
2663 struct attach_command_continuation_args
2667 enum attach_post_wait_mode mode
;
2671 attach_command_continuation (void *args
, int err
)
2673 struct attach_command_continuation_args
*a
2674 = (struct attach_command_continuation_args
*) args
;
2679 attach_post_wait (a
->args
, a
->from_tty
, a
->mode
);
2683 attach_command_continuation_free_args (void *args
)
2685 struct attach_command_continuation_args
*a
2686 = (struct attach_command_continuation_args
*) args
;
2692 /* "attach" command entry point. Takes a program started up outside
2693 of gdb and ``attaches'' to it. This stops it cold in its tracks
2694 and allows us to start debugging it. */
2697 attach_command (const char *args
, int from_tty
)
2700 struct target_ops
*attach_target
;
2701 struct inferior
*inferior
= current_inferior ();
2702 enum attach_post_wait_mode mode
;
2704 dont_repeat (); /* Not for the faint of heart */
2706 if (gdbarch_has_global_solist (target_gdbarch ()))
2707 /* Don't complain if all processes share the same symbol
2710 else if (target_has_execution
)
2712 if (query (_("A program is being debugged already. Kill it? ")))
2715 error (_("Not killed."));
2718 /* Clean up any leftovers from other runs. Some other things from
2719 this function should probably be moved into target_pre_inferior. */
2720 target_pre_inferior (from_tty
);
2722 gdb::unique_xmalloc_ptr
<char> stripped
= strip_bg_char (args
, &async_exec
);
2723 args
= stripped
.get ();
2725 attach_target
= find_attach_target ();
2727 prepare_execution_command (attach_target
, async_exec
);
2729 if (non_stop
&& !attach_target
->supports_non_stop ())
2730 error (_("Cannot attach to this target in non-stop mode"));
2732 attach_target
->attach (args
, from_tty
);
2733 /* to_attach should push the target, so after this point we
2734 shouldn't refer to attach_target again. */
2735 attach_target
= NULL
;
2737 /* Set up the "saved terminal modes" of the inferior
2738 based on what modes we are starting it with. */
2739 target_terminal::init ();
2741 /* Install inferior's terminal modes. This may look like a no-op,
2742 as we've just saved them above, however, this does more than
2743 restore terminal settings:
2745 - installs a SIGINT handler that forwards SIGINT to the inferior.
2746 Otherwise a Ctrl-C pressed just while waiting for the initial
2747 stop would end up as a spurious Quit.
2749 - removes stdin from the event loop, which we need if attaching
2750 in the foreground, otherwise on targets that report an initial
2751 stop on attach (which are most) we'd process input/commands
2752 while we're in the event loop waiting for that stop. That is,
2753 before the attach continuation runs and the command is really
2755 target_terminal::inferior ();
2757 /* Set up execution context to know that we should return from
2758 wait_for_inferior as soon as the target reports a stop. */
2759 init_wait_for_inferior ();
2760 clear_proceed_status (0);
2762 inferior
->needs_setup
= 1;
2764 if (target_is_non_stop_p ())
2766 /* If we find that the current thread isn't stopped, explicitly
2767 do so now, because we're going to install breakpoints and
2771 /* The user requested an `attach&'; stop just one thread. */
2772 target_stop (inferior_ptid
);
2774 /* The user requested an `attach', so stop all threads of this
2776 target_stop (ptid_t (inferior_ptid
.pid ()));
2779 mode
= async_exec
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_STOP
;
2781 /* Some system don't generate traps when attaching to inferior.
2782 E.g. Mach 3 or GNU hurd. */
2783 if (!target_attach_no_wait ())
2785 struct attach_command_continuation_args
*a
;
2787 /* Careful here. See comments in inferior.h. Basically some
2788 OSes don't ignore SIGSTOPs on continue requests anymore. We
2789 need a way for handle_inferior_event to reset the stop_signal
2790 variable after an attach, and this is what
2791 STOP_QUIETLY_NO_SIGSTOP is for. */
2792 inferior
->control
.stop_soon
= STOP_QUIETLY_NO_SIGSTOP
;
2794 /* Wait for stop. */
2795 a
= XNEW (struct attach_command_continuation_args
);
2796 a
->args
= xstrdup (args
);
2797 a
->from_tty
= from_tty
;
2799 add_inferior_continuation (attach_command_continuation
, a
,
2800 attach_command_continuation_free_args
);
2802 if (!target_is_async_p ())
2803 mark_infrun_async_event_handler ();
2807 attach_post_wait (args
, from_tty
, mode
);
2810 /* We had just found out that the target was already attached to an
2811 inferior. PTID points at a thread of this new inferior, that is
2812 the most likely to be stopped right now, but not necessarily so.
2813 The new inferior is assumed to be already added to the inferior
2814 list at this point. If LEAVE_RUNNING, then leave the threads of
2815 this inferior running, except those we've explicitly seen reported
2819 notice_new_inferior (thread_info
*thr
, int leave_running
, int from_tty
)
2821 enum attach_post_wait_mode mode
2822 = leave_running
? ATTACH_POST_WAIT_RESUME
: ATTACH_POST_WAIT_NOTHING
;
2824 gdb::optional
<scoped_restore_current_thread
> restore_thread
;
2826 if (inferior_ptid
!= null_ptid
)
2827 restore_thread
.emplace ();
2829 /* Avoid reading registers -- we haven't fetched the target
2831 switch_to_thread_no_regs (thr
);
2833 /* When we "notice" a new inferior we need to do all the things we
2834 would normally do if we had just attached to it. */
2838 struct attach_command_continuation_args
*a
;
2839 struct inferior
*inferior
= current_inferior ();
2841 /* We're going to install breakpoints, and poke at memory,
2842 ensure that the inferior is stopped for a moment while we do
2844 target_stop (inferior_ptid
);
2846 inferior
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
2848 /* Wait for stop before proceeding. */
2849 a
= XNEW (struct attach_command_continuation_args
);
2850 a
->args
= xstrdup ("");
2851 a
->from_tty
= from_tty
;
2853 add_inferior_continuation (attach_command_continuation
, a
,
2854 attach_command_continuation_free_args
);
2859 attach_post_wait ("" /* args */, from_tty
, mode
);
2864 * takes a program previously attached to and detaches it.
2865 * The program resumes execution and will no longer stop
2866 * on signals, etc. We better not have left any breakpoints
2867 * in the program or it'll die when it hits one. For this
2868 * to work, it may be necessary for the process to have been
2869 * previously attached. It *might* work if the program was
2870 * started via the normal ptrace (PTRACE_TRACEME).
2874 detach_command (const char *args
, int from_tty
)
2876 dont_repeat (); /* Not for the faint of heart. */
2878 if (inferior_ptid
== null_ptid
)
2879 error (_("The program is not being run."));
2881 query_if_trace_running (from_tty
);
2883 disconnect_tracing ();
2885 target_detach (current_inferior (), from_tty
);
2887 /* The current inferior process was just detached successfully. Get
2888 rid of breakpoints that no longer make sense. Note we don't do
2889 this within target_detach because that is also used when
2890 following child forks, and in that case we will want to transfer
2891 breakpoints to the child, not delete them. */
2892 breakpoint_init_inferior (inf_exited
);
2894 /* If the solist is global across inferiors, don't clear it when we
2895 detach from a single inferior. */
2896 if (!gdbarch_has_global_solist (target_gdbarch ()))
2897 no_shared_libraries (NULL
, from_tty
);
2899 if (deprecated_detach_hook
)
2900 deprecated_detach_hook ();
2903 /* Disconnect from the current target without resuming it (leaving it
2904 waiting for a debugger).
2906 We'd better not have left any breakpoints in the program or the
2907 next debugger will get confused. Currently only supported for some
2908 remote targets, since the normal attach mechanisms don't work on
2909 stopped processes on some native platforms (e.g. GNU/Linux). */
2912 disconnect_command (const char *args
, int from_tty
)
2914 dont_repeat (); /* Not for the faint of heart. */
2915 query_if_trace_running (from_tty
);
2916 disconnect_tracing ();
2917 target_disconnect (args
, from_tty
);
2918 no_shared_libraries (NULL
, from_tty
);
2919 init_thread_list ();
2920 if (deprecated_detach_hook
)
2921 deprecated_detach_hook ();
2925 interrupt_target_1 (int all_threads
)
2930 ptid
= minus_one_ptid
;
2932 ptid
= inferior_ptid
;
2937 target_interrupt ();
2939 /* Tag the thread as having been explicitly requested to stop, so
2940 other parts of gdb know not to resume this thread automatically,
2941 if it was stopped due to an internal event. Limit this to
2942 non-stop mode, as when debugging a multi-threaded application in
2943 all-stop mode, we will only get one stop event --- it's undefined
2944 which thread will report the event. */
2946 set_stop_requested (ptid
, 1);
2950 Stop the execution of the target while running in async mode, in
2951 the background. In all-stop, stop the whole process. In non-stop
2952 mode, stop the current thread only by default, or stop all threads
2953 if the `-a' switch is used. */
2956 interrupt_command (const char *args
, int from_tty
)
2958 if (target_can_async_p ())
2960 int all_threads
= 0;
2962 dont_repeat (); /* Not for the faint of heart. */
2965 && startswith (args
, "-a"))
2968 if (!non_stop
&& all_threads
)
2969 error (_("-a is meaningless in all-stop mode."));
2971 interrupt_target_1 (all_threads
);
2975 /* See inferior.h. */
2978 default_print_float_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
2979 struct frame_info
*frame
, const char *args
)
2982 int printed_something
= 0;
2984 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
2986 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, float_reggroup
))
2988 printed_something
= 1;
2989 gdbarch_print_registers_info (gdbarch
, file
, frame
, regnum
, 1);
2992 if (!printed_something
)
2993 fprintf_filtered (file
, "No floating-point info "
2994 "available for this processor.\n");
2998 info_float_command (const char *args
, int from_tty
)
3000 struct frame_info
*frame
;
3002 if (!target_has_registers
)
3003 error (_("The program has no registers now."));
3005 frame
= get_selected_frame (NULL
);
3006 gdbarch_print_float_info (get_frame_arch (frame
), gdb_stdout
, frame
, args
);
3010 unset_command (const char *args
, int from_tty
)
3012 printf_filtered (_("\"unset\" must be followed by the "
3013 "name of an unset subcommand.\n"));
3014 help_list (unsetlist
, "unset ", all_commands
, gdb_stdout
);
3017 /* Implement `info proc' family of commands. */
3020 info_proc_cmd_1 (const char *args
, enum info_proc_what what
, int from_tty
)
3022 struct gdbarch
*gdbarch
= get_current_arch ();
3024 if (!target_info_proc (args
, what
))
3026 if (gdbarch_info_proc_p (gdbarch
))
3027 gdbarch_info_proc (gdbarch
, args
, what
);
3029 error (_("Not supported on this target."));
3033 /* Implement `info proc' when given without any futher parameters. */
3036 info_proc_cmd (const char *args
, int from_tty
)
3038 info_proc_cmd_1 (args
, IP_MINIMAL
, from_tty
);
3041 /* Implement `info proc mappings'. */
3044 info_proc_cmd_mappings (const char *args
, int from_tty
)
3046 info_proc_cmd_1 (args
, IP_MAPPINGS
, from_tty
);
3049 /* Implement `info proc stat'. */
3052 info_proc_cmd_stat (const char *args
, int from_tty
)
3054 info_proc_cmd_1 (args
, IP_STAT
, from_tty
);
3057 /* Implement `info proc status'. */
3060 info_proc_cmd_status (const char *args
, int from_tty
)
3062 info_proc_cmd_1 (args
, IP_STATUS
, from_tty
);
3065 /* Implement `info proc cwd'. */
3068 info_proc_cmd_cwd (const char *args
, int from_tty
)
3070 info_proc_cmd_1 (args
, IP_CWD
, from_tty
);
3073 /* Implement `info proc cmdline'. */
3076 info_proc_cmd_cmdline (const char *args
, int from_tty
)
3078 info_proc_cmd_1 (args
, IP_CMDLINE
, from_tty
);
3081 /* Implement `info proc exe'. */
3084 info_proc_cmd_exe (const char *args
, int from_tty
)
3086 info_proc_cmd_1 (args
, IP_EXE
, from_tty
);
3089 /* Implement `info proc files'. */
3092 info_proc_cmd_files (const char *args
, int from_tty
)
3094 info_proc_cmd_1 (args
, IP_FILES
, from_tty
);
3097 /* Implement `info proc all'. */
3100 info_proc_cmd_all (const char *args
, int from_tty
)
3102 info_proc_cmd_1 (args
, IP_ALL
, from_tty
);
3105 /* Implement `show print finish'. */
3108 show_print_finish (struct ui_file
*file
, int from_tty
,
3109 struct cmd_list_element
*c
,
3112 fprintf_filtered (file
, _("\
3113 Printing of return value after `finish' is %s.\n"),
3118 /* This help string is used for the run, start, and starti commands.
3119 It is defined as a macro to prevent duplication. */
3121 #define RUN_ARGS_HELP \
3122 "You may specify arguments to give it.\n\
3123 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3124 shell that will start the program (specified by the \"$SHELL\" environment\n\
3125 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3126 are also allowed.\n\
3128 With no arguments, uses arguments last specified (with \"run\" or \n\
3129 \"set args\"). To cancel previous arguments and run with no arguments,\n\
3130 use \"set args\" without arguments.\n\
3132 To start the inferior without using a shell, use \"set startup-with-shell off\"."
3135 _initialize_infcmd (void)
3137 static struct cmd_list_element
*info_proc_cmdlist
;
3138 struct cmd_list_element
*c
= NULL
;
3139 const char *cmd_name
;
3141 /* Add the filename of the terminal connected to inferior I/O. */
3142 add_setshow_optional_filename_cmd ("inferior-tty", class_run
,
3143 &inferior_io_terminal_scratch
, _("\
3144 Set terminal for future runs of program being debugged."), _("\
3145 Show terminal for future runs of program being debugged."), _("\
3146 Usage: set inferior-tty [TTY]\n\n\
3147 If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3149 set_inferior_tty_command
,
3150 show_inferior_tty_command
,
3151 &setlist
, &showlist
);
3152 cmd_name
= "inferior-tty";
3153 c
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
3154 gdb_assert (c
!= NULL
);
3155 add_alias_cmd ("tty", c
, class_alias
, 0, &cmdlist
);
3158 add_setshow_string_noescape_cmd (cmd_name
, class_run
,
3159 &inferior_args_scratch
, _("\
3160 Set argument list to give program being debugged when it is started."), _("\
3161 Show argument list to give program being debugged when it is started."), _("\
3162 Follow this command with any number of args, to be passed to the program."),
3165 &setlist
, &showlist
);
3166 c
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
3167 gdb_assert (c
!= NULL
);
3168 set_cmd_completer (c
, filename_completer
);
3171 add_setshow_string_noescape_cmd (cmd_name
, class_run
,
3172 &inferior_cwd_scratch
, _("\
3173 Set the current working directory to be used when the inferior is started.\n\
3174 Changing this setting does not have any effect on inferiors that are\n\
3177 Show the current working directory that is used when the inferior is started."),
3179 Use this command to change the current working directory that will be used\n\
3180 when the inferior is started. This setting does not affect GDB's current\n\
3181 working directory."),
3184 &setlist
, &showlist
);
3185 c
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
3186 gdb_assert (c
!= NULL
);
3187 set_cmd_completer (c
, filename_completer
);
3189 c
= add_cmd ("environment", no_class
, environment_info
, _("\
3190 The environment to give the program, or one variable's value.\n\
3191 With an argument VAR, prints the value of environment variable VAR to\n\
3192 give the program being debugged. With no arguments, prints the entire\n\
3193 environment to be given to the program."), &showlist
);
3194 set_cmd_completer (c
, noop_completer
);
3196 add_prefix_cmd ("unset", no_class
, unset_command
,
3197 _("Complement to certain \"set\" commands."),
3198 &unsetlist
, "unset ", 0, &cmdlist
);
3200 c
= add_cmd ("environment", class_run
, unset_environment_command
, _("\
3201 Cancel environment variable VAR for the program.\n\
3202 This does not affect the program until the next \"run\" command."),
3204 set_cmd_completer (c
, noop_completer
);
3206 c
= add_cmd ("environment", class_run
, set_environment_command
, _("\
3207 Set environment variable value to give the program.\n\
3208 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3209 VALUES of environment variables are uninterpreted strings.\n\
3210 This does not affect the program until the next \"run\" command."),
3212 set_cmd_completer (c
, noop_completer
);
3214 c
= add_com ("path", class_files
, path_command
, _("\
3215 Add directory DIR(s) to beginning of search path for object files.\n\
3216 $cwd in the path means the current working directory.\n\
3217 This path is equivalent to the $PATH shell variable. It is a list of\n\
3218 directories, separated by colons. These directories are searched to find\n\
3219 fully linked executable files and separately compiled object files as \
3221 set_cmd_completer (c
, filename_completer
);
3223 c
= add_cmd ("paths", no_class
, path_info
, _("\
3224 Current search path for finding object files.\n\
3225 $cwd in the path means the current working directory.\n\
3226 This path is equivalent to the $PATH shell variable. It is a list of\n\
3227 directories, separated by colons. These directories are searched to find\n\
3228 fully linked executable files and separately compiled object files as \
3231 set_cmd_completer (c
, noop_completer
);
3233 add_prefix_cmd ("kill", class_run
, kill_command
,
3234 _("Kill execution of program being debugged."),
3235 &killlist
, "kill ", 0, &cmdlist
);
3237 add_com ("attach", class_run
, attach_command
, _("\
3238 Attach to a process or file outside of GDB.\n\
3239 This command attaches to another target, of the same type as your last\n\
3240 \"target\" command (\"info files\" will show your target stack).\n\
3241 The command may take as argument a process id or a device file.\n\
3242 For a process id, you must have permission to send the process a signal,\n\
3243 and it must have the same effective uid as the debugger.\n\
3244 When using \"attach\" with a process id, the debugger finds the\n\
3245 program running in the process, looking first in the current working\n\
3246 directory, or (if not found there) using the source file search path\n\
3247 (see the \"directory\" command). You can also use the \"file\" command\n\
3248 to specify the program, and to load its symbol table."));
3250 add_prefix_cmd ("detach", class_run
, detach_command
, _("\
3251 Detach a process or file previously attached.\n\
3252 If a process, it is no longer traced, and it continues its execution. If\n\
3253 you were debugging a file, the file is closed and gdb no longer accesses it."),
3254 &detachlist
, "detach ", 0, &cmdlist
);
3256 add_com ("disconnect", class_run
, disconnect_command
, _("\
3257 Disconnect from a target.\n\
3258 The target will wait for another debugger to connect. Not available for\n\
3261 c
= add_com ("signal", class_run
, signal_command
, _("\
3262 Continue program with the specified signal.\n\
3263 Usage: signal SIGNAL\n\
3264 The SIGNAL argument is processed the same as the handle command.\n\
3266 An argument of \"0\" means continue the program without sending it a signal.\n\
3267 This is useful in cases where the program stopped because of a signal,\n\
3268 and you want to resume the program while discarding the signal.\n\
3270 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3271 the current thread only."));
3272 set_cmd_completer (c
, signal_completer
);
3274 c
= add_com ("queue-signal", class_run
, queue_signal_command
, _("\
3275 Queue a signal to be delivered to the current thread when it is resumed.\n\
3276 Usage: queue-signal SIGNAL\n\
3277 The SIGNAL argument is processed the same as the handle command.\n\
3278 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3280 An argument of \"0\" means remove any currently queued signal from\n\
3281 the current thread. This is useful in cases where the program stopped\n\
3282 because of a signal, and you want to resume it while discarding the signal.\n\
3284 In a multi-threaded program the signal is queued with, or discarded from,\n\
3285 the current thread only."));
3286 set_cmd_completer (c
, signal_completer
);
3288 add_com ("stepi", class_run
, stepi_command
, _("\
3289 Step one instruction exactly.\n\
3291 Argument N means step N times (or till program stops for another \
3293 add_com_alias ("si", "stepi", class_alias
, 0);
3295 add_com ("nexti", class_run
, nexti_command
, _("\
3296 Step one instruction, but proceed through subroutine calls.\n\
3298 Argument N means step N times (or till program stops for another \
3300 add_com_alias ("ni", "nexti", class_alias
, 0);
3302 add_com ("finish", class_run
, finish_command
, _("\
3303 Execute until selected stack frame returns.\n\
3305 Upon return, the value returned is printed and put in the value history."));
3306 add_com_alias ("fin", "finish", class_run
, 1);
3308 add_com ("next", class_run
, next_command
, _("\
3309 Step program, proceeding through subroutine calls.\n\
3311 Unlike \"step\", if the current source line calls a subroutine,\n\
3312 this command does not enter the subroutine, but instead steps over\n\
3313 the call, in effect treating it as a single source line."));
3314 add_com_alias ("n", "next", class_run
, 1);
3316 add_com ("step", class_run
, step_command
, _("\
3317 Step program until it reaches a different source line.\n\
3319 Argument N means step N times (or till program stops for another \
3321 add_com_alias ("s", "step", class_run
, 1);
3323 c
= add_com ("until", class_run
, until_command
, _("\
3324 Execute until past the current line or past a LOCATION.\n\
3325 Execute until the program reaches a source line greater than the current\n\
3326 or a specified location (same args as break command) within the current \
3328 set_cmd_completer (c
, location_completer
);
3329 add_com_alias ("u", "until", class_run
, 1);
3331 c
= add_com ("advance", class_run
, advance_command
, _("\
3332 Continue the program up to the given location (same form as args for break \
3334 Execution will also stop upon exit from the current stack frame."));
3335 set_cmd_completer (c
, location_completer
);
3337 c
= add_com ("jump", class_run
, jump_command
, _("\
3338 Continue program being debugged at specified line or address.\n\
3339 Usage: jump LOCATION\n\
3340 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3341 for an address to start at."));
3342 set_cmd_completer (c
, location_completer
);
3343 add_com_alias ("j", "jump", class_run
, 1);
3345 add_com ("continue", class_run
, continue_command
, _("\
3346 Continue program being debugged, after signal or breakpoint.\n\
3347 Usage: continue [N]\n\
3348 If proceeding from breakpoint, a number N may be used as an argument,\n\
3349 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3350 the breakpoint won't break until the Nth time it is reached).\n\
3352 If non-stop mode is enabled, continue only the current thread,\n\
3353 otherwise all the threads in the program are continued. To \n\
3354 continue all stopped threads in non-stop mode, use the -a option.\n\
3355 Specifying -a and an ignore count simultaneously is an error."));
3356 add_com_alias ("c", "cont", class_run
, 1);
3357 add_com_alias ("fg", "cont", class_run
, 1);
3359 c
= add_com ("run", class_run
, run_command
, _("\
3360 Start debugged program.\n"
3362 set_cmd_completer (c
, filename_completer
);
3363 add_com_alias ("r", "run", class_run
, 1);
3365 c
= add_com ("start", class_run
, start_command
, _("\
3366 Start the debugged program stopping at the beginning of the main procedure.\n"
3368 set_cmd_completer (c
, filename_completer
);
3370 c
= add_com ("starti", class_run
, starti_command
, _("\
3371 Start the debugged program stopping at the first instruction.\n"
3373 set_cmd_completer (c
, filename_completer
);
3375 add_com ("interrupt", class_run
, interrupt_command
,
3376 _("Interrupt the execution of the debugged program.\n\
3377 If non-stop mode is enabled, interrupt only the current thread,\n\
3378 otherwise all the threads in the program are stopped. To \n\
3379 interrupt all running threads in non-stop mode, use the -a option."));
3381 c
= add_info ("registers", info_registers_command
, _("\
3382 List of integer registers and their contents, for selected stack frame.\n\
3383 One or more register names as argument means describe the given registers.\n\
3384 One or more register group names as argument means describe the registers\n\
3385 in the named register groups."));
3386 add_info_alias ("r", "registers", 1);
3387 set_cmd_completer (c
, reg_or_group_completer
);
3389 c
= add_info ("all-registers", info_all_registers_command
, _("\
3390 List of all registers and their contents, for selected stack frame.\n\
3391 One or more register names as argument means describe the given registers.\n\
3392 One or more register group names as argument means describe the registers\n\
3393 in the named register groups."));
3394 set_cmd_completer (c
, reg_or_group_completer
);
3396 add_info ("program", info_program_command
,
3397 _("Execution status of the program."));
3399 add_info ("float", info_float_command
,
3400 _("Print the status of the floating point unit."));
3402 add_info ("vector", info_vector_command
,
3403 _("Print the status of the vector unit."));
3405 add_prefix_cmd ("proc", class_info
, info_proc_cmd
,
3407 Show additional information about a process.\n\
3408 Specify any process id, or use the program being debugged by default."),
3409 &info_proc_cmdlist
, "info proc ",
3410 1/*allow-unknown*/, &infolist
);
3412 add_cmd ("mappings", class_info
, info_proc_cmd_mappings
, _("\
3413 List memory regions mapped by the specified process."),
3414 &info_proc_cmdlist
);
3416 add_cmd ("stat", class_info
, info_proc_cmd_stat
, _("\
3417 List process info from /proc/PID/stat."),
3418 &info_proc_cmdlist
);
3420 add_cmd ("status", class_info
, info_proc_cmd_status
, _("\
3421 List process info from /proc/PID/status."),
3422 &info_proc_cmdlist
);
3424 add_cmd ("cwd", class_info
, info_proc_cmd_cwd
, _("\
3425 List current working directory of the specified process."),
3426 &info_proc_cmdlist
);
3428 add_cmd ("cmdline", class_info
, info_proc_cmd_cmdline
, _("\
3429 List command line arguments of the specified process."),
3430 &info_proc_cmdlist
);
3432 add_cmd ("exe", class_info
, info_proc_cmd_exe
, _("\
3433 List absolute filename for executable of the specified process."),
3434 &info_proc_cmdlist
);
3436 add_cmd ("files", class_info
, info_proc_cmd_files
, _("\
3437 List files opened by the specified process."),
3438 &info_proc_cmdlist
);
3440 add_cmd ("all", class_info
, info_proc_cmd_all
, _("\
3441 List all available info about the specified process."),
3442 &info_proc_cmdlist
);
3444 add_setshow_boolean_cmd ("finish", class_support
,
3445 &user_print_options
.finish_print
, _("\
3446 Set whether `finish' prints the return value."), _("\
3447 Show whether `finish' prints the return value."), NULL
,
3450 &setprintlist
, &showprintlist
);