Automatic date update in version.in
[binutils-gdb.git] / gdb / infcmd.c
blobec818bc6936f1ecb2c4b11d1e0723e1ea3a05ed0
1 /* Memory-access and commands for "inferior" process, for GDB.
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "gdbsupport/environ.h"
28 #include "value.h"
29 #include "gdbcmd.h"
30 #include "symfile.h"
31 #include "gdbcore.h"
32 #include "target.h"
33 #include "language.h"
34 #include "objfiles.h"
35 #include "completer.h"
36 #include "ui-out.h"
37 #include "regcache.h"
38 #include "reggroups.h"
39 #include "block.h"
40 #include "solib.h"
41 #include <ctype.h>
42 #include "observable.h"
43 #include "target-descriptions.h"
44 #include "user-regs.h"
45 #include "gdbthread.h"
46 #include "valprint.h"
47 #include "inline-frame.h"
48 #include "tracepoint.h"
49 #include "inf-loop.h"
50 #include "linespec.h"
51 #include "thread-fsm.h"
52 #include "ui.h"
53 #include "interps.h"
54 #include "skip.h"
55 #include <optional>
56 #include "source.h"
57 #include "cli/cli-style.h"
58 #include "dwarf2/loc.h"
60 /* Local functions: */
62 static void until_next_command (int);
64 static void step_1 (int, int, const char *);
66 #define ERROR_NO_INFERIOR \
67 if (!target_has_execution ()) error (_("The program is not being run."));
69 /* Pid of our debugged inferior, or 0 if no inferior now.
70 Since various parts of infrun.c test this to see whether there is a program
71 being debugged it should be nonzero (currently 3 is used) for remote
72 debugging. */
74 ptid_t inferior_ptid;
76 /* Nonzero if stopped due to completion of a stack dummy routine. */
78 enum stop_stack_kind stop_stack_dummy;
80 /* Nonzero if stopped due to a random (unexpected) signal in inferior
81 process. */
83 int stopped_by_random_signal;
86 /* Whether "finish" should print the value. */
88 static bool finish_print = true;
92 /* Store the new value passed to 'set inferior-tty'. */
94 static void
95 set_tty_value (const std::string &tty)
97 current_inferior ()->set_tty (tty);
100 /* Get the current 'inferior-tty' value. */
102 static const std::string &
103 get_tty_value ()
105 return current_inferior ()->tty ();
108 /* Implement 'show inferior-tty' command. */
110 static void
111 show_inferior_tty_command (struct ui_file *file, int from_tty,
112 struct cmd_list_element *c, const char *value)
114 /* Note that we ignore the passed-in value in favor of computing it
115 directly. */
116 const std::string &inferior_tty = current_inferior ()->tty ();
118 gdb_printf (file,
119 _("Terminal for future runs of program being debugged "
120 "is \"%s\".\n"), inferior_tty.c_str ());
123 /* Store the new value passed to 'set args'. */
125 static void
126 set_args_value (const std::string &args)
128 current_inferior ()->set_args (args);
131 /* Return the value for 'show args' to display. */
133 static const std::string &
134 get_args_value ()
136 return current_inferior ()->args ();
139 /* Callback to implement 'show args' command. */
141 static void
142 show_args_command (struct ui_file *file, int from_tty,
143 struct cmd_list_element *c, const char *value)
145 /* Ignore the passed in value, pull the argument directly from the
146 inferior. However, these should always be the same. */
147 gdb_printf (file, _("\
148 Argument list to give program being debugged when it is started is \"%s\".\n"),
149 current_inferior ()->args ().c_str ());
152 /* See gdbsupport/common-inferior.h. */
154 const std::string &
155 get_inferior_cwd ()
157 return current_inferior ()->cwd ();
160 /* Store the new value passed to 'set cwd'. */
162 static void
163 set_cwd_value (const std::string &args)
165 current_inferior ()->set_cwd (args);
168 /* Handle the 'show cwd' command. */
170 static void
171 show_cwd_command (struct ui_file *file, int from_tty,
172 struct cmd_list_element *c, const char *value)
174 const std::string &cwd = current_inferior ()->cwd ();
176 if (cwd.empty ())
177 gdb_printf (file,
178 _("\
179 You have not set the inferior's current working directory.\n\
180 The inferior will inherit GDB's cwd if native debugging, or the remote\n\
181 server's cwd if remote debugging.\n"));
182 else
183 gdb_printf (file,
184 _("Current working directory that will be used "
185 "when starting the inferior is \"%s\".\n"),
186 cwd.c_str ());
190 /* This function strips the '&' character (indicating background
191 execution) that is added as *the last* of the arguments ARGS of a
192 command. A copy of the incoming ARGS without the '&' is returned,
193 unless the resulting string after stripping is empty, in which case
194 NULL is returned. *BG_CHAR_P is an output boolean that indicates
195 whether the '&' character was found. */
197 static gdb::unique_xmalloc_ptr<char>
198 strip_bg_char (const char *args, int *bg_char_p)
200 const char *p;
202 if (args == nullptr || *args == '\0')
204 *bg_char_p = 0;
205 return nullptr;
208 p = args + strlen (args);
209 if (p[-1] == '&')
211 p--;
212 while (p > args && isspace (p[-1]))
213 p--;
215 *bg_char_p = 1;
216 if (p != args)
217 return gdb::unique_xmalloc_ptr<char>
218 (savestring (args, p - args));
219 else
220 return gdb::unique_xmalloc_ptr<char> (nullptr);
223 *bg_char_p = 0;
224 return make_unique_xstrdup (args);
227 /* Common actions to take after creating any sort of inferior, by any
228 means (running, attaching, connecting, et cetera). The target
229 should be stopped. */
231 void
232 post_create_inferior (int from_tty)
235 /* Be sure we own the terminal in case write operations are performed. */
236 target_terminal::ours_for_output ();
238 infrun_debug_show_threads ("threads in the newly created inferior",
239 current_inferior ()->non_exited_threads ());
241 /* If the target hasn't taken care of this already, do it now.
242 Targets which need to access registers during to_open,
243 to_create_inferior, or to_attach should do it earlier; but many
244 don't need to. */
245 target_find_description ();
247 /* Now that we know the register layout, retrieve current PC. But
248 if the PC is unavailable (e.g., we're opening a core file with
249 missing registers info), ignore it. */
250 thread_info *thr = inferior_thread ();
252 thr->clear_stop_pc ();
255 regcache *rc = get_thread_regcache (thr);
256 thr->set_stop_pc (regcache_read_pc (rc));
258 catch (const gdb_exception_error &ex)
260 if (ex.error != NOT_AVAILABLE_ERROR)
261 throw;
264 if (current_program_space->exec_bfd ())
266 const unsigned solib_add_generation
267 = current_program_space->solib_add_generation;
269 scoped_restore restore_in_initial_library_scan
270 = make_scoped_restore (&current_inferior ()->in_initial_library_scan,
271 true);
273 /* Create the hooks to handle shared library load and unload
274 events. */
275 solib_create_inferior_hook (from_tty);
277 if (current_program_space->solib_add_generation == solib_add_generation)
279 /* The platform-specific hook should load initial shared libraries,
280 but didn't. FROM_TTY will be incorrectly 0 but such solib
281 targets should be fixed anyway. Call it only after the solib
282 target has been initialized by solib_create_inferior_hook. */
284 if (info_verbose)
285 warning (_("platform-specific solib_create_inferior_hook did "
286 "not load initial shared libraries."));
288 /* If the solist is global across processes, there's no need to
289 refetch it here. */
290 if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
291 solib_add (nullptr, 0, auto_solib_add);
295 /* If the user sets watchpoints before execution having started,
296 then she gets software watchpoints, because GDB can't know which
297 target will end up being pushed, or if it supports hardware
298 watchpoints or not. breakpoint_re_set takes care of promoting
299 watchpoints to hardware watchpoints if possible, however, if this
300 new inferior doesn't load shared libraries or we don't pull in
301 symbols from any other source on this target/arch,
302 breakpoint_re_set is never called. Call it now so that software
303 watchpoints get a chance to be promoted to hardware watchpoints
304 if the now pushed target supports hardware watchpoints. */
305 breakpoint_re_set ();
307 gdb::observers::inferior_created.notify (current_inferior ());
310 /* Kill the inferior if already running. This function is designed
311 to be called when we are about to start the execution of the program
312 from the beginning. Ask the user to confirm that he wants to restart
313 the program being debugged when FROM_TTY is non-null. */
315 static void
316 kill_if_already_running (int from_tty)
318 if (inferior_ptid != null_ptid && target_has_execution ())
320 /* Bail out before killing the program if we will not be able to
321 restart it. */
322 target_require_runnable ();
324 if (from_tty
325 && !query (_("The program being debugged has been started already.\n\
326 Start it from the beginning? ")))
327 error (_("Program not restarted."));
328 target_kill ();
332 /* See inferior.h. */
334 void
335 prepare_execution_command (struct target_ops *target, int background)
337 /* If we get a request for running in the bg but the target
338 doesn't support it, error out. */
339 if (background && !target_can_async_p (target))
340 error (_("Asynchronous execution not supported on this target."));
342 if (!background)
344 /* If we get a request for running in the fg, then we need to
345 simulate synchronous (fg) execution. Note no cleanup is
346 necessary for this. stdin is re-enabled whenever an error
347 reaches the top level. */
348 all_uis_on_sync_execution_starting ();
352 /* Determine how the new inferior will behave. */
354 enum run_how
356 /* Run program without any explicit stop during startup. */
357 RUN_NORMAL,
359 /* Stop at the beginning of the program's main function. */
360 RUN_STOP_AT_MAIN,
362 /* Stop at the first instruction of the program. */
363 RUN_STOP_AT_FIRST_INSN
366 /* Implement the "run" command. Force a stop during program start if
367 requested by RUN_HOW. */
369 static void
370 run_command_1 (const char *args, int from_tty, enum run_how run_how)
372 const char *exec_file;
373 struct ui_out *uiout = current_uiout;
374 struct target_ops *run_target;
375 int async_exec;
377 dont_repeat ();
379 scoped_disable_commit_resumed disable_commit_resumed ("running");
381 kill_if_already_running (from_tty);
383 init_wait_for_inferior ();
384 clear_breakpoint_hit_counts ();
386 /* Clean up any leftovers from other runs. Some other things from
387 this function should probably be moved into target_pre_inferior. */
388 target_pre_inferior (from_tty);
390 /* The comment here used to read, "The exec file is re-read every
391 time we do a generic_mourn_inferior, so we just have to worry
392 about the symbol file." The `generic_mourn_inferior' function
393 gets called whenever the program exits. However, suppose the
394 program exits, and *then* the executable file changes? We need
395 to check again here. Since reopen_exec_file doesn't do anything
396 if the timestamp hasn't changed, I don't see the harm. */
397 reopen_exec_file ();
398 reread_symbols (from_tty);
400 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
401 args = stripped.get ();
403 /* Do validation and preparation before possibly changing anything
404 in the inferior. */
406 run_target = find_run_target ();
408 prepare_execution_command (run_target, async_exec);
410 if (non_stop && !run_target->supports_non_stop ())
411 error (_("The target does not support running in non-stop mode."));
413 /* Done. Can now set breakpoints, change inferior args, etc. */
415 /* Insert temporary breakpoint in main function if requested. */
416 if (run_how == RUN_STOP_AT_MAIN)
418 /* To avoid other inferiors hitting this breakpoint, make it
419 inferior-specific. */
420 std::string arg = string_printf ("-qualified %s inferior %d",
421 main_name (),
422 current_inferior ()->num);
423 tbreak_command (arg.c_str (), 0);
426 exec_file = get_exec_file (0);
428 /* We keep symbols from add-symbol-file, on the grounds that the
429 user might want to add some symbols before running the program
430 (right?). But sometimes (dynamic loading where the user manually
431 introduces the new symbols with add-symbol-file), the code which
432 the symbols describe does not persist between runs. Currently
433 the user has to manually nuke all symbols between runs if they
434 want them to go away (PR 2207). This is probably reasonable. */
436 /* If there were other args, beside '&', process them. */
437 if (args != nullptr)
438 current_inferior ()->set_args (args);
440 if (from_tty)
442 uiout->field_string (nullptr, "Starting program");
443 uiout->text (": ");
444 if (exec_file)
445 uiout->field_string ("execfile", exec_file,
446 file_name_style.style ());
447 uiout->spaces (1);
448 uiout->field_string ("infargs", current_inferior ()->args ());
449 uiout->text ("\n");
450 uiout->flush ();
453 run_target->create_inferior (exec_file,
454 current_inferior ()->args (),
455 current_inferior ()->environment.envp (),
456 from_tty);
457 /* to_create_inferior should push the target, so after this point we
458 shouldn't refer to run_target again. */
459 run_target = nullptr;
461 infrun_debug_show_threads ("immediately after create_process",
462 current_inferior ()->non_exited_threads ());
464 /* We're starting off a new process. When we get out of here, in
465 non-stop mode, finish the state of all threads of that process,
466 but leave other threads alone, as they may be stopped in internal
467 events --- the frontend shouldn't see them as stopped. In
468 all-stop, always finish the state of all threads, as we may be
469 resuming more than just the new process. */
470 process_stratum_target *finish_target;
471 ptid_t finish_ptid;
472 if (non_stop)
474 finish_target = current_inferior ()->process_target ();
475 finish_ptid = ptid_t (current_inferior ()->pid);
477 else
479 finish_target = nullptr;
480 finish_ptid = minus_one_ptid;
482 scoped_finish_thread_state finish_state (finish_target, finish_ptid);
484 /* Pass zero for FROM_TTY, because at this point the "run" command
485 has done its thing; now we are setting up the running program. */
486 post_create_inferior (0);
488 /* Queue a pending event so that the program stops immediately. */
489 if (run_how == RUN_STOP_AT_FIRST_INSN)
491 thread_info *thr = inferior_thread ();
492 target_waitstatus ws;
493 ws.set_stopped (GDB_SIGNAL_0);
494 thr->set_pending_waitstatus (ws);
497 /* Start the target running. Do not use -1 continuation as it would skip
498 breakpoint right at the entry point. */
499 proceed (regcache_read_pc (get_thread_regcache (inferior_thread ())),
500 GDB_SIGNAL_0);
502 /* Since there was no error, there's no need to finish the thread
503 states here. */
504 finish_state.release ();
506 disable_commit_resumed.reset_and_commit ();
509 static void
510 run_command (const char *args, int from_tty)
512 run_command_1 (args, from_tty, RUN_NORMAL);
515 /* Start the execution of the program up until the beginning of the main
516 program. */
518 static void
519 start_command (const char *args, int from_tty)
521 /* Some languages such as Ada need to search inside the program
522 minimal symbols for the location where to put the temporary
523 breakpoint before starting. */
524 if (!have_minimal_symbols ())
525 error (_("No symbol table loaded. Use the \"file\" command."));
527 /* Run the program until reaching the main procedure... */
528 run_command_1 (args, from_tty, RUN_STOP_AT_MAIN);
531 /* Start the execution of the program stopping at the first
532 instruction. */
534 static void
535 starti_command (const char *args, int from_tty)
537 run_command_1 (args, from_tty, RUN_STOP_AT_FIRST_INSN);
540 static int
541 proceed_thread_callback (struct thread_info *thread, void *arg)
543 /* We go through all threads individually instead of compressing
544 into a single target `resume_all' request, because some threads
545 may be stopped in internal breakpoints/events, or stopped waiting
546 for its turn in the displaced stepping queue (that is, they are
547 running && !executing). The target side has no idea about why
548 the thread is stopped, so a `resume_all' command would resume too
549 much. If/when GDB gains a way to tell the target `hold this
550 thread stopped until I say otherwise', then we can optimize
551 this. */
552 if (thread->state != THREAD_STOPPED)
553 return 0;
555 if (!thread->inf->has_execution ())
556 return 0;
558 switch_to_thread (thread);
559 clear_proceed_status (0);
560 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
561 return 0;
564 static void
565 ensure_valid_thread (void)
567 if (inferior_ptid == null_ptid
568 || inferior_thread ()->state == THREAD_EXITED)
569 error (_("Cannot execute this command without a live selected thread."));
572 /* If the user is looking at trace frames, any resumption of execution
573 is likely to mix up recorded and live target data. So simply
574 disallow those commands. */
576 static void
577 ensure_not_tfind_mode (void)
579 if (get_traceframe_number () >= 0)
580 error (_("Cannot execute this command while looking at trace frames."));
583 /* Throw an error indicating the current thread is running. */
585 static void
586 error_is_running (void)
588 error (_("Cannot execute this command while "
589 "the selected thread is running."));
592 /* Calls error_is_running if the current thread is running. */
594 static void
595 ensure_not_running (void)
597 if (inferior_thread ()->state == THREAD_RUNNING)
598 error_is_running ();
601 void
602 continue_1 (int all_threads)
604 ERROR_NO_INFERIOR;
605 ensure_not_tfind_mode ();
607 if (non_stop && all_threads)
609 /* Don't error out if the current thread is running, because
610 there may be other stopped threads. */
612 /* Backup current thread and selected frame and restore on scope
613 exit. */
614 scoped_restore_current_thread restore_thread;
615 scoped_disable_commit_resumed disable_commit_resumed
616 ("continue all threads in non-stop");
618 iterate_over_threads (proceed_thread_callback, nullptr);
620 if (current_ui->prompt_state == PROMPT_BLOCKED)
622 /* If all threads in the target were already running,
623 proceed_thread_callback ends up never calling proceed,
624 and so nothing calls this to put the inferior's terminal
625 settings in effect and remove stdin from the event loop,
626 which we must when running a foreground command. E.g.:
628 (gdb) c -a&
629 Continuing.
630 <all threads are running now>
631 (gdb) c -a
632 Continuing.
633 <no thread was resumed, but the inferior now owns the terminal>
635 target_terminal::inferior ();
638 disable_commit_resumed.reset_and_commit ();
640 else
642 ensure_valid_thread ();
643 ensure_not_running ();
644 clear_proceed_status (0);
645 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
649 /* continue [-a] [proceed-count] [&] */
651 static void
652 continue_command (const char *args, int from_tty)
654 int async_exec;
655 bool all_threads_p = false;
657 ERROR_NO_INFERIOR;
659 /* Find out whether we must run in the background. */
660 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
661 args = stripped.get ();
663 if (args != nullptr)
665 if (startswith (args, "-a"))
667 all_threads_p = true;
668 args += sizeof ("-a") - 1;
669 if (*args == '\0')
670 args = nullptr;
674 if (!non_stop && all_threads_p)
675 error (_("`-a' is meaningless in all-stop mode."));
677 if (args != nullptr && all_threads_p)
678 error (_("Can't resume all threads and specify "
679 "proceed count simultaneously."));
681 /* If we have an argument left, set proceed count of breakpoint we
682 stopped at. */
683 if (args != nullptr)
685 bpstat *bs = nullptr;
686 int num, stat;
687 int stopped = 0;
688 struct thread_info *tp;
690 if (non_stop)
691 tp = inferior_thread ();
692 else
694 process_stratum_target *last_target;
695 ptid_t last_ptid;
697 get_last_target_status (&last_target, &last_ptid, nullptr);
698 tp = last_target->find_thread (last_ptid);
700 if (tp != nullptr)
701 bs = tp->control.stop_bpstat;
703 while ((stat = bpstat_num (&bs, &num)) != 0)
704 if (stat > 0)
706 set_ignore_count (num,
707 parse_and_eval_long (args) - 1,
708 from_tty);
709 /* set_ignore_count prints a message ending with a period.
710 So print two spaces before "Continuing.". */
711 if (from_tty)
712 gdb_printf (" ");
713 stopped = 1;
716 if (!stopped && from_tty)
718 gdb_printf
719 ("Not stopped at any breakpoint; argument ignored.\n");
723 ensure_not_tfind_mode ();
725 if (!non_stop || !all_threads_p)
727 ensure_valid_thread ();
728 ensure_not_running ();
731 prepare_execution_command (current_inferior ()->top_target (), async_exec);
733 if (from_tty)
734 gdb_printf (_("Continuing.\n"));
736 continue_1 (all_threads_p);
739 /* Record in TP the starting point of a "step" or "next" command. */
741 static void
742 set_step_frame (thread_info *tp)
744 /* This can be removed once this function no longer implicitly relies on the
745 inferior_ptid value. */
746 gdb_assert (inferior_ptid == tp->ptid);
748 frame_info_ptr frame = get_current_frame ();
750 symtab_and_line sal = find_frame_sal (frame);
751 set_step_info (tp, frame, sal);
753 CORE_ADDR pc = get_frame_pc (frame);
754 tp->control.step_start_function = find_pc_function (pc);
757 /* Step until outside of current statement. */
759 static void
760 step_command (const char *count_string, int from_tty)
762 step_1 (0, 0, count_string);
765 /* Likewise, but skip over subroutine calls as if single instructions. */
767 static void
768 next_command (const char *count_string, int from_tty)
770 step_1 (1, 0, count_string);
773 /* Likewise, but step only one instruction. */
775 static void
776 stepi_command (const char *count_string, int from_tty)
778 step_1 (0, 1, count_string);
781 static void
782 nexti_command (const char *count_string, int from_tty)
784 step_1 (1, 1, count_string);
787 /* Data for the FSM that manages the step/next/stepi/nexti
788 commands. */
790 struct step_command_fsm : public thread_fsm
792 /* How many steps left in a "step N"-like command. */
793 int count;
795 /* If true, this is a next/nexti, otherwise a step/stepi. */
796 int skip_subroutines;
798 /* If true, this is a stepi/nexti, otherwise a step/step. */
799 int single_inst;
801 explicit step_command_fsm (struct interp *cmd_interp)
802 : thread_fsm (cmd_interp)
806 void clean_up (struct thread_info *thread) override;
807 bool should_stop (struct thread_info *thread) override;
808 enum async_reply_reason do_async_reply_reason () override;
811 /* Prepare for a step/next/etc. command. Any target resource
812 allocated here is undone in the FSM's clean_up method. */
814 static void
815 step_command_fsm_prepare (struct step_command_fsm *sm,
816 int skip_subroutines, int single_inst,
817 int count, struct thread_info *thread)
819 sm->skip_subroutines = skip_subroutines;
820 sm->single_inst = single_inst;
821 sm->count = count;
823 /* Leave the si command alone. */
824 if (!sm->single_inst || sm->skip_subroutines)
825 set_longjmp_breakpoint (thread, get_frame_id (get_current_frame ()));
827 thread->control.stepping_command = 1;
830 static int prepare_one_step (thread_info *, struct step_command_fsm *sm);
832 static void
833 step_1 (int skip_subroutines, int single_inst, const char *count_string)
835 int count;
836 int async_exec;
837 struct thread_info *thr;
838 struct step_command_fsm *step_sm;
840 ERROR_NO_INFERIOR;
841 ensure_not_tfind_mode ();
842 ensure_valid_thread ();
843 ensure_not_running ();
845 gdb::unique_xmalloc_ptr<char> stripped
846 = strip_bg_char (count_string, &async_exec);
847 count_string = stripped.get ();
849 prepare_execution_command (current_inferior ()->top_target (), async_exec);
851 count = count_string ? parse_and_eval_long (count_string) : 1;
853 clear_proceed_status (1);
855 /* Setup the execution command state machine to handle all the COUNT
856 steps. */
857 thr = inferior_thread ();
858 step_sm = new step_command_fsm (command_interp ());
859 thr->set_thread_fsm (std::unique_ptr<thread_fsm> (step_sm));
861 step_command_fsm_prepare (step_sm, skip_subroutines,
862 single_inst, count, thr);
864 /* Do only one step for now, before returning control to the event
865 loop. Let the continuation figure out how many other steps we
866 need to do, and handle them one at the time, through
867 step_once. */
868 if (!prepare_one_step (thr, step_sm))
869 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
870 else
872 /* Stepped into an inline frame. Pretend that we've
873 stopped. */
874 thr->thread_fsm ()->clean_up (thr);
875 bool proceeded = normal_stop ();
876 if (!proceeded)
877 inferior_event_handler (INF_EXEC_COMPLETE);
878 all_uis_check_sync_execution_done ();
882 /* Implementation of the 'should_stop' FSM method for stepping
883 commands. Called after we are done with one step operation, to
884 check whether we need to step again, before we print the prompt and
885 return control to the user. If count is > 1, returns false, as we
886 will need to keep going. */
888 bool
889 step_command_fsm::should_stop (struct thread_info *tp)
891 if (tp->control.stop_step)
893 /* There are more steps to make, and we did stop due to
894 ending a stepping range. Do another step. */
895 if (--count > 0)
896 return prepare_one_step (tp, this);
898 set_finished ();
901 return true;
904 /* Implementation of the 'clean_up' FSM method for stepping commands. */
906 void
907 step_command_fsm::clean_up (struct thread_info *thread)
909 if (!single_inst || skip_subroutines)
910 delete_longjmp_breakpoint (thread->global_num);
913 /* Implementation of the 'async_reply_reason' FSM method for stepping
914 commands. */
916 enum async_reply_reason
917 step_command_fsm::do_async_reply_reason ()
919 return EXEC_ASYNC_END_STEPPING_RANGE;
922 /* Prepare for one step in "step N". The actual target resumption is
923 done by the caller. Return true if we're done and should thus
924 report a stop to the user. Returns false if the target needs to be
925 resumed. */
927 static int
928 prepare_one_step (thread_info *tp, struct step_command_fsm *sm)
930 /* This can be removed once this function no longer implicitly relies on the
931 inferior_ptid value. */
932 gdb_assert (inferior_ptid == tp->ptid);
934 if (sm->count > 0)
936 frame_info_ptr frame = get_current_frame ();
938 set_step_frame (tp);
940 if (!sm->single_inst)
942 CORE_ADDR pc;
944 /* Step at an inlined function behaves like "down". */
945 if (!sm->skip_subroutines
946 && inline_skipped_frames (tp))
948 ptid_t resume_ptid;
949 const char *fn = nullptr;
950 symtab_and_line sal;
951 struct symbol *sym;
953 /* Pretend that we've ran. */
954 resume_ptid = user_visible_resume_ptid (1);
955 set_running (tp->inf->process_target (), resume_ptid, true);
957 step_into_inline_frame (tp);
959 frame = get_current_frame ();
960 sal = find_frame_sal (frame);
961 sym = get_frame_function (frame);
963 if (sym != nullptr)
964 fn = sym->print_name ();
966 if (sal.line == 0
967 || !function_name_is_marked_for_skip (fn, sal))
969 sm->count--;
970 return prepare_one_step (tp, sm);
974 pc = get_frame_pc (frame);
975 find_pc_line_pc_range (pc,
976 &tp->control.step_range_start,
977 &tp->control.step_range_end);
979 /* There's a problem in gcc (PR gcc/98780) that causes missing line
980 table entries, which results in a too large stepping range.
981 Use inlined_subroutine info to make the range more narrow. */
982 if (inline_skipped_frames (tp) > 0)
984 symbol *sym = inline_skipped_symbol (tp);
985 if (sym->aclass () == LOC_BLOCK)
987 const block *block = sym->value_block ();
988 if (block->end () < tp->control.step_range_end)
989 tp->control.step_range_end = block->end ();
993 tp->control.may_range_step = 1;
995 /* If we have no line info, switch to stepi mode. */
996 if (tp->control.step_range_end == 0 && step_stop_if_no_debug)
998 tp->control.step_range_start = tp->control.step_range_end = 1;
999 tp->control.may_range_step = 0;
1001 else if (tp->control.step_range_end == 0)
1003 const char *name;
1005 if (find_pc_partial_function (pc, &name,
1006 &tp->control.step_range_start,
1007 &tp->control.step_range_end) == 0)
1008 error (_("Cannot find bounds of current function"));
1010 target_terminal::ours_for_output ();
1011 gdb_printf (_("Single stepping until exit from function %s,"
1012 "\nwhich has no line number information.\n"),
1013 name);
1016 else
1018 /* Say we are stepping, but stop after one insn whatever it does. */
1019 tp->control.step_range_start = tp->control.step_range_end = 1;
1020 if (!sm->skip_subroutines)
1021 /* It is stepi.
1022 Don't step over function calls, not even to functions lacking
1023 line numbers. */
1024 tp->control.step_over_calls = STEP_OVER_NONE;
1027 if (sm->skip_subroutines)
1028 tp->control.step_over_calls = STEP_OVER_ALL;
1030 return 0;
1033 /* Done. */
1034 sm->set_finished ();
1035 return 1;
1039 /* Continue program at specified address. */
1041 static void
1042 jump_command (const char *arg, int from_tty)
1044 struct gdbarch *gdbarch = get_current_arch ();
1045 CORE_ADDR addr;
1046 struct symbol *fn;
1047 struct symbol *sfn;
1048 int async_exec;
1050 ERROR_NO_INFERIOR;
1051 ensure_not_tfind_mode ();
1052 ensure_valid_thread ();
1053 ensure_not_running ();
1055 /* Find out whether we must run in the background. */
1056 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1057 arg = stripped.get ();
1059 prepare_execution_command (current_inferior ()->top_target (), async_exec);
1061 if (!arg)
1062 error_no_arg (_("starting address"));
1064 std::vector<symtab_and_line> sals
1065 = decode_line_with_current_source (arg, DECODE_LINE_FUNFIRSTLINE);
1066 if (sals.size () != 1)
1068 /* If multiple sal-objects were found, try dropping those that aren't
1069 from the current symtab. */
1070 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
1071 sals.erase (std::remove_if (sals.begin (), sals.end (),
1072 [&] (const symtab_and_line &sal)
1074 return sal.symtab != cursal.symtab;
1075 }), sals.end ());
1076 if (sals.size () != 1)
1077 error (_("Jump request is ambiguous: "
1078 "does not resolve to a single address"));
1081 symtab_and_line &sal = sals[0];
1083 if (sal.symtab == 0 && sal.pc == 0)
1084 error (_("No source file has been specified."));
1086 resolve_sal_pc (&sal); /* May error out. */
1088 /* See if we are trying to jump to another function. */
1089 fn = get_frame_function (get_current_frame ());
1090 sfn = find_pc_sect_containing_function (sal.pc,
1091 find_pc_mapped_section (sal.pc));
1092 if (fn != nullptr && sfn != fn)
1094 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal.line,
1095 fn->print_name ()))
1097 error (_("Not confirmed."));
1098 /* NOTREACHED */
1102 if (sfn != nullptr)
1104 struct obj_section *section;
1106 section = sfn->obj_section (sfn->objfile ());
1107 if (section_is_overlay (section)
1108 && !section_is_mapped (section))
1110 if (!query (_("WARNING!!! Destination is in "
1111 "unmapped overlay! Jump anyway? ")))
1113 error (_("Not confirmed."));
1114 /* NOTREACHED */
1119 addr = sal.pc;
1121 if (from_tty)
1123 gdb_printf (_("Continuing at "));
1124 gdb_puts (paddress (gdbarch, addr));
1125 gdb_printf (".\n");
1128 clear_proceed_status (0);
1129 proceed (addr, GDB_SIGNAL_0);
1132 /* Continue program giving it specified signal. */
1134 static void
1135 signal_command (const char *signum_exp, int from_tty)
1137 enum gdb_signal oursig;
1138 int async_exec;
1140 dont_repeat (); /* Too dangerous. */
1141 ERROR_NO_INFERIOR;
1142 ensure_not_tfind_mode ();
1143 ensure_valid_thread ();
1144 ensure_not_running ();
1146 /* Find out whether we must run in the background. */
1147 gdb::unique_xmalloc_ptr<char> stripped
1148 = strip_bg_char (signum_exp, &async_exec);
1149 signum_exp = stripped.get ();
1151 prepare_execution_command (current_inferior ()->top_target (), async_exec);
1153 if (!signum_exp)
1154 error_no_arg (_("signal number"));
1156 /* It would be even slicker to make signal names be valid expressions,
1157 (the type could be "enum $signal" or some such), then the user could
1158 assign them to convenience variables. */
1159 oursig = gdb_signal_from_name (signum_exp);
1161 if (oursig == GDB_SIGNAL_UNKNOWN)
1163 /* No, try numeric. */
1164 int num = parse_and_eval_long (signum_exp);
1166 if (num == 0)
1167 oursig = GDB_SIGNAL_0;
1168 else
1169 oursig = gdb_signal_from_command (num);
1172 /* Look for threads other than the current that this command ends up
1173 resuming too (due to schedlock off), and warn if they'll get a
1174 signal delivered. "signal 0" is used to suppress a previous
1175 signal, but if the current thread is no longer the one that got
1176 the signal, then the user is potentially suppressing the signal
1177 of the wrong thread. */
1178 if (!non_stop)
1180 int must_confirm = 0;
1182 /* This indicates what will be resumed. Either a single thread,
1183 a whole process, or all threads of all processes. */
1184 ptid_t resume_ptid = user_visible_resume_ptid (0);
1185 process_stratum_target *resume_target
1186 = user_visible_resume_target (resume_ptid);
1188 thread_info *current = inferior_thread ();
1190 for (thread_info *tp : all_non_exited_threads (resume_target, resume_ptid))
1192 if (tp == current)
1193 continue;
1195 if (tp->stop_signal () != GDB_SIGNAL_0
1196 && signal_pass_state (tp->stop_signal ()))
1198 if (!must_confirm)
1199 gdb_printf (_("Note:\n"));
1200 gdb_printf (_(" Thread %s previously stopped with signal %s, %s.\n"),
1201 print_thread_id (tp),
1202 gdb_signal_to_name (tp->stop_signal ()),
1203 gdb_signal_to_string (tp->stop_signal ()));
1204 must_confirm = 1;
1208 if (must_confirm
1209 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1210 "still deliver the signals noted above to their respective threads.\n"
1211 "Continue anyway? "),
1212 print_thread_id (inferior_thread ())))
1213 error (_("Not confirmed."));
1216 if (from_tty)
1218 if (oursig == GDB_SIGNAL_0)
1219 gdb_printf (_("Continuing with no signal.\n"));
1220 else
1221 gdb_printf (_("Continuing with signal %s.\n"),
1222 gdb_signal_to_name (oursig));
1225 clear_proceed_status (0);
1226 proceed ((CORE_ADDR) -1, oursig);
1229 /* Queue a signal to be delivered to the current thread. */
1231 static void
1232 queue_signal_command (const char *signum_exp, int from_tty)
1234 enum gdb_signal oursig;
1235 struct thread_info *tp;
1237 ERROR_NO_INFERIOR;
1238 ensure_not_tfind_mode ();
1239 ensure_valid_thread ();
1240 ensure_not_running ();
1242 if (signum_exp == nullptr)
1243 error_no_arg (_("signal number"));
1245 /* It would be even slicker to make signal names be valid expressions,
1246 (the type could be "enum $signal" or some such), then the user could
1247 assign them to convenience variables. */
1248 oursig = gdb_signal_from_name (signum_exp);
1250 if (oursig == GDB_SIGNAL_UNKNOWN)
1252 /* No, try numeric. */
1253 int num = parse_and_eval_long (signum_exp);
1255 if (num == 0)
1256 oursig = GDB_SIGNAL_0;
1257 else
1258 oursig = gdb_signal_from_command (num);
1261 if (oursig != GDB_SIGNAL_0
1262 && !signal_pass_state (oursig))
1263 error (_("Signal handling set to not pass this signal to the program."));
1265 tp = inferior_thread ();
1266 tp->set_stop_signal (oursig);
1269 /* Data for the FSM that manages the until (with no argument)
1270 command. */
1272 struct until_next_fsm : public thread_fsm
1274 /* The thread that as current when the command was executed. */
1275 int thread;
1277 until_next_fsm (struct interp *cmd_interp, int thread)
1278 : thread_fsm (cmd_interp),
1279 thread (thread)
1283 bool should_stop (struct thread_info *thread) override;
1284 void clean_up (struct thread_info *thread) override;
1285 enum async_reply_reason do_async_reply_reason () override;
1288 /* Implementation of the 'should_stop' FSM method for the until (with
1289 no arg) command. */
1291 bool
1292 until_next_fsm::should_stop (struct thread_info *tp)
1294 if (tp->control.stop_step)
1295 set_finished ();
1297 return true;
1300 /* Implementation of the 'clean_up' FSM method for the until (with no
1301 arg) command. */
1303 void
1304 until_next_fsm::clean_up (struct thread_info *thread)
1306 delete_longjmp_breakpoint (thread->global_num);
1309 /* Implementation of the 'async_reply_reason' FSM method for the until
1310 (with no arg) command. */
1312 enum async_reply_reason
1313 until_next_fsm::do_async_reply_reason ()
1315 return EXEC_ASYNC_END_STEPPING_RANGE;
1318 /* Proceed until we reach a different source line with pc greater than
1319 our current one or exit the function. We skip calls in both cases.
1321 Note that eventually this command should probably be changed so
1322 that only source lines are printed out when we hit the breakpoint
1323 we set. This may involve changes to wait_for_inferior and the
1324 proceed status code. */
1326 static void
1327 until_next_command (int from_tty)
1329 frame_info_ptr frame;
1330 CORE_ADDR pc;
1331 struct symbol *func;
1332 struct symtab_and_line sal;
1333 struct thread_info *tp = inferior_thread ();
1334 int thread = tp->global_num;
1335 struct until_next_fsm *sm;
1337 clear_proceed_status (0);
1338 set_step_frame (tp);
1340 frame = get_current_frame ();
1342 /* Step until either exited from this function or greater
1343 than the current line (if in symbolic section) or pc (if
1344 not). */
1346 pc = get_frame_pc (frame);
1347 func = find_pc_function (pc);
1349 if (!func)
1351 struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc);
1353 if (msymbol.minsym == nullptr)
1354 error (_("Execution is not within a known function."));
1356 tp->control.step_range_start = msymbol.value_address ();
1357 /* The upper-bound of step_range is exclusive. In order to make PC
1358 within the range, set the step_range_end with PC + 1. */
1359 tp->control.step_range_end = pc + 1;
1361 else
1363 sal = find_pc_line (pc, 0);
1365 tp->control.step_range_start = func->value_block ()->entry_pc ();
1366 tp->control.step_range_end = sal.end;
1368 /* By setting the step_range_end based on the current pc, we are
1369 assuming that the last line table entry for any given source line
1370 will have is_stmt set to true. This is not necessarily the case,
1371 there may be additional entries for the same source line with
1372 is_stmt set false. Consider the following code:
1374 for (int i = 0; i < 10; i++)
1375 loop_body ();
1377 Clang-13, will generate multiple line table entries at the end of
1378 the loop all associated with the 'for' line. The first of these
1379 entries is marked is_stmt true, but the other entries are is_stmt
1380 false.
1382 If we only use the values in SAL, then our stepping range may not
1383 extend to the end of the loop. The until command will reach the
1384 end of the range, find a non is_stmt instruction, and step to the
1385 next is_stmt instruction. This stopping point, however, will be
1386 inside the loop, which is not what we wanted.
1388 Instead, we now check any subsequent line table entries to see if
1389 they are for the same line. If they are, and they are marked
1390 is_stmt false, then we extend the end of our stepping range.
1392 When we finish this process the end of the stepping range will
1393 point either to a line with a different line number, or, will
1394 point at an address for the same line number that is marked as a
1395 statement. */
1397 struct symtab_and_line final_sal
1398 = find_pc_line (tp->control.step_range_end, 0);
1400 while (final_sal.line == sal.line && final_sal.symtab == sal.symtab
1401 && !final_sal.is_stmt)
1403 tp->control.step_range_end = final_sal.end;
1404 final_sal = find_pc_line (final_sal.end, 0);
1407 tp->control.may_range_step = 1;
1409 tp->control.step_over_calls = STEP_OVER_ALL;
1411 set_longjmp_breakpoint (tp, get_frame_id (frame));
1412 delete_longjmp_breakpoint_cleanup lj_deleter (thread);
1414 sm = new until_next_fsm (command_interp (), tp->global_num);
1415 tp->set_thread_fsm (std::unique_ptr<thread_fsm> (sm));
1416 lj_deleter.release ();
1418 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1421 static void
1422 until_command (const char *arg, int from_tty)
1424 int async_exec;
1426 ERROR_NO_INFERIOR;
1427 ensure_not_tfind_mode ();
1428 ensure_valid_thread ();
1429 ensure_not_running ();
1431 /* Find out whether we must run in the background. */
1432 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1433 arg = stripped.get ();
1435 prepare_execution_command (current_inferior ()->top_target (), async_exec);
1437 if (arg)
1438 until_break_command (arg, from_tty, 0);
1439 else
1440 until_next_command (from_tty);
1443 static void
1444 advance_command (const char *arg, int from_tty)
1446 int async_exec;
1448 ERROR_NO_INFERIOR;
1449 ensure_not_tfind_mode ();
1450 ensure_valid_thread ();
1451 ensure_not_running ();
1453 if (arg == nullptr)
1454 error_no_arg (_("a location"));
1456 /* Find out whether we must run in the background. */
1457 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1458 arg = stripped.get ();
1460 prepare_execution_command (current_inferior ()->top_target (), async_exec);
1462 until_break_command (arg, from_tty, 1);
1465 /* See inferior.h. */
1467 struct value *
1468 get_return_value (struct symbol *func_symbol, struct value *function)
1470 regcache *stop_regs = get_thread_regcache (inferior_thread ());
1471 struct gdbarch *gdbarch = stop_regs->arch ();
1472 struct value *value;
1474 struct type *value_type
1475 = check_typedef (func_symbol->type ()->target_type ());
1476 gdb_assert (value_type->code () != TYPE_CODE_VOID);
1478 if (is_nocall_function (check_typedef (function->type ())))
1480 warning (_("Function '%s' does not follow the target calling "
1481 "convention, cannot determine its returned value."),
1482 func_symbol->print_name ());
1484 return nullptr;
1487 /* FIXME: 2003-09-27: When returning from a nested inferior function
1488 call, it's possible (with no help from the architecture vector)
1489 to locate and return/print a "struct return" value. This is just
1490 a more complicated case of what is already being done in the
1491 inferior function call code. In fact, when inferior function
1492 calls are made async, this will likely be made the norm. */
1494 switch (gdbarch_return_value_as_value (gdbarch, function, value_type,
1495 nullptr, nullptr, nullptr))
1497 case RETURN_VALUE_REGISTER_CONVENTION:
1498 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
1499 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
1500 gdbarch_return_value_as_value (gdbarch, function, value_type, stop_regs,
1501 &value, nullptr);
1502 break;
1503 case RETURN_VALUE_STRUCT_CONVENTION:
1504 value = nullptr;
1505 break;
1506 default:
1507 internal_error (_("bad switch"));
1510 return value;
1513 /* The captured function return value/type and its position in the
1514 value history. */
1516 struct return_value_info
1518 /* The captured return value. May be NULL if we weren't able to
1519 retrieve it. See get_return_value. */
1520 struct value *value;
1522 /* The return type. In some cases, we'll not be able extract the
1523 return value, but we always know the type. */
1524 struct type *type;
1526 /* If we captured a value, this is the value history index. */
1527 int value_history_index;
1530 /* Helper for print_return_value. */
1532 static void
1533 print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv)
1535 if (rv->value != nullptr)
1537 /* Print it. */
1538 uiout->text ("Value returned is ");
1539 uiout->field_fmt ("gdb-result-var", "$%d",
1540 rv->value_history_index);
1541 uiout->text (" = ");
1543 if (finish_print)
1545 struct value_print_options opts;
1546 get_user_print_options (&opts);
1548 string_file stb;
1549 value_print (rv->value, &stb, &opts);
1550 uiout->field_stream ("return-value", stb);
1552 else
1553 uiout->field_string ("return-value", _("<not displayed>"),
1554 metadata_style.style ());
1555 uiout->text ("\n");
1557 else
1559 std::string type_name = type_to_string (rv->type);
1560 uiout->text ("Value returned has type: ");
1561 uiout->field_string ("return-type", type_name);
1562 uiout->text (".");
1563 uiout->text (" Cannot determine contents\n");
1567 /* Print the result of a function at the end of a 'finish' command.
1568 RV points at an object representing the captured return value/type
1569 and its position in the value history. */
1571 void
1572 print_return_value (struct ui_out *uiout, struct return_value_info *rv)
1574 if (rv->type == nullptr
1575 || check_typedef (rv->type)->code () == TYPE_CODE_VOID)
1576 return;
1580 /* print_return_value_1 can throw an exception in some
1581 circumstances. We need to catch this so that we still
1582 delete the breakpoint. */
1583 print_return_value_1 (uiout, rv);
1585 catch (const gdb_exception_error &ex)
1587 exception_print (gdb_stdout, ex);
1591 /* Data for the FSM that manages the finish command. */
1593 struct finish_command_fsm : public thread_fsm
1595 /* The momentary breakpoint set at the function's return address in
1596 the caller. */
1597 breakpoint_up breakpoint;
1599 /* The function that we're stepping out of. */
1600 struct symbol *function = nullptr;
1602 /* If the FSM finishes successfully, this stores the function's
1603 return value. */
1604 struct return_value_info return_value_info {};
1606 /* If the current function uses the "struct return convention",
1607 this holds the address at which the value being returned will
1608 be stored, or zero if that address could not be determined or
1609 the "struct return convention" is not being used. */
1610 CORE_ADDR return_buf;
1612 explicit finish_command_fsm (struct interp *cmd_interp)
1613 : thread_fsm (cmd_interp)
1617 bool should_stop (struct thread_info *thread) override;
1618 void clean_up (struct thread_info *thread) override;
1619 struct return_value_info *return_value () override;
1620 enum async_reply_reason do_async_reply_reason () override;
1623 /* Implementation of the 'should_stop' FSM method for the finish
1624 commands. Detects whether the thread stepped out of the function
1625 successfully, and if so, captures the function's return value and
1626 marks the FSM finished. */
1628 bool
1629 finish_command_fsm::should_stop (struct thread_info *tp)
1631 struct return_value_info *rv = &return_value_info;
1633 if (function != nullptr
1634 && bpstat_find_breakpoint (tp->control.stop_bpstat,
1635 breakpoint.get ()) != nullptr)
1637 /* We're done. */
1638 set_finished ();
1640 rv->type = function->type ()->target_type ();
1641 if (rv->type == nullptr)
1642 internal_error (_("finish_command: function has no target type"));
1644 if (check_typedef (rv->type)->code () != TYPE_CODE_VOID)
1646 struct value *func;
1648 func = read_var_value (function, nullptr, get_current_frame ());
1650 if (return_buf != 0)
1651 /* Retrieve return value from the buffer where it was saved. */
1652 rv->value = value_at (rv->type, return_buf);
1653 else
1654 rv->value = get_return_value (function, func);
1656 if (rv->value != nullptr)
1657 rv->value_history_index = rv->value->record_latest ();
1660 else if (tp->control.stop_step)
1662 /* Finishing from an inline frame, or reverse finishing. In
1663 either case, there's no way to retrieve the return value. */
1664 set_finished ();
1667 return true;
1670 /* Implementation of the 'clean_up' FSM method for the finish
1671 commands. */
1673 void
1674 finish_command_fsm::clean_up (struct thread_info *thread)
1676 breakpoint.reset ();
1677 delete_longjmp_breakpoint (thread->global_num);
1680 /* Implementation of the 'return_value' FSM method for the finish
1681 commands. */
1683 struct return_value_info *
1684 finish_command_fsm::return_value ()
1686 return &return_value_info;
1689 /* Implementation of the 'async_reply_reason' FSM method for the
1690 finish commands. */
1692 enum async_reply_reason
1693 finish_command_fsm::do_async_reply_reason ()
1695 if (execution_direction == EXEC_REVERSE)
1696 return EXEC_ASYNC_END_STEPPING_RANGE;
1697 else
1698 return EXEC_ASYNC_FUNCTION_FINISHED;
1701 /* finish_backward -- helper function for finish_command. */
1703 static void
1704 finish_backward (struct finish_command_fsm *sm)
1706 struct symtab_and_line sal;
1707 struct thread_info *tp = inferior_thread ();
1708 CORE_ADDR pc;
1709 CORE_ADDR func_addr;
1710 CORE_ADDR alt_entry_point;
1711 CORE_ADDR entry_point;
1712 frame_info_ptr frame = get_selected_frame (nullptr);
1713 struct gdbarch *gdbarch = get_frame_arch (frame);
1715 pc = get_frame_pc (get_current_frame ());
1717 if (find_pc_partial_function (pc, nullptr, &func_addr, nullptr) == 0)
1718 error (_("Cannot find bounds of current function"));
1720 sal = find_pc_line (func_addr, 0);
1721 alt_entry_point = sal.pc;
1722 entry_point = alt_entry_point;
1724 if (gdbarch_skip_entrypoint_p (gdbarch))
1725 /* Some architectures, like PowerPC use local and global entry points.
1726 There is only one Entry Point (GEP = LEP) for other architectures.
1727 The GEP is an alternate entry point. The LEP is the normal entry point.
1728 The value of entry_point was initialized to the alternate entry point
1729 (GEP). It will be adjusted to the normal entry point if the function
1730 has two entry points. */
1731 entry_point = gdbarch_skip_entrypoint (gdbarch, sal.pc);
1733 tp->control.proceed_to_finish = 1;
1734 /* Special case: if we're sitting at the function entry point,
1735 then all we need to do is take a reverse singlestep. We
1736 don't need to set a breakpoint, and indeed it would do us
1737 no good to do so.
1739 Note that this can only happen at frame #0, since there's
1740 no way that a function up the stack can have a return address
1741 that's equal to its entry point. */
1743 if ((pc < alt_entry_point) || (pc > entry_point))
1745 /* We are in the body of the function. Set a breakpoint to go back to
1746 the normal entry point. */
1747 symtab_and_line sr_sal;
1748 sr_sal.pc = entry_point;
1749 sr_sal.pspace = get_frame_program_space (frame);
1750 insert_step_resume_breakpoint_at_sal (gdbarch,
1751 sr_sal, null_frame_id);
1753 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1755 else
1757 /* We are either at one of the entry points or between the entry points.
1758 If we are not at the alt_entry point, go back to the alt_entry_point
1759 If we at the normal entry point step back one instruction, when we
1760 stop we will determine if we entered via the entry point or the
1761 alternate entry point. If we are at the alternate entry point,
1762 single step back to the function call. */
1763 tp->control.step_range_start = tp->control.step_range_end = 1;
1764 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1768 /* finish_forward -- helper function for finish_command. FRAME is the
1769 frame that called the function we're about to step out of. */
1771 static void
1772 finish_forward (struct finish_command_fsm *sm, frame_info_ptr frame)
1774 struct frame_id frame_id = get_frame_id (frame);
1775 struct gdbarch *gdbarch = get_frame_arch (frame);
1776 struct symtab_and_line sal;
1777 struct thread_info *tp = inferior_thread ();
1779 sal = find_pc_line (get_frame_pc (frame), 0);
1780 sal.pc = get_frame_pc (frame);
1782 sm->breakpoint = set_momentary_breakpoint (gdbarch, sal,
1783 get_stack_frame_id (frame),
1784 bp_finish);
1786 /* set_momentary_breakpoint invalidates FRAME. */
1787 frame = nullptr;
1789 set_longjmp_breakpoint (tp, frame_id);
1791 /* We want to print return value, please... */
1792 tp->control.proceed_to_finish = 1;
1794 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1797 /* Skip frames for "finish". */
1799 static frame_info_ptr
1800 skip_finish_frames (frame_info_ptr frame)
1802 frame_info_ptr start;
1806 start = frame;
1808 frame = skip_tailcall_frames (frame);
1809 if (frame == nullptr)
1810 break;
1812 frame = skip_unwritable_frames (frame);
1813 if (frame == nullptr)
1814 break;
1816 while (start != frame);
1818 return frame;
1821 /* "finish": Set a temporary breakpoint at the place the selected
1822 frame will return to, then continue. */
1824 static void
1825 finish_command (const char *arg, int from_tty)
1827 frame_info_ptr frame;
1828 int async_exec;
1829 struct finish_command_fsm *sm;
1830 struct thread_info *tp;
1832 ERROR_NO_INFERIOR;
1833 ensure_not_tfind_mode ();
1834 ensure_valid_thread ();
1835 ensure_not_running ();
1837 /* Find out whether we must run in the background. */
1838 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1839 arg = stripped.get ();
1841 prepare_execution_command (current_inferior ()->top_target (), async_exec);
1843 if (arg)
1844 error (_("The \"finish\" command does not take any arguments."));
1846 frame = get_prev_frame (get_selected_frame (_("No selected frame.")));
1847 if (frame == 0)
1848 error (_("\"finish\" not meaningful in the outermost frame."));
1850 clear_proceed_status (0);
1852 tp = inferior_thread ();
1854 sm = new finish_command_fsm (command_interp ());
1856 tp->set_thread_fsm (std::unique_ptr<thread_fsm> (sm));
1858 /* Finishing from an inline frame is completely different. We don't
1859 try to show the "return value" - no way to locate it. */
1860 if (get_frame_type (get_selected_frame (_("No selected frame.")))
1861 == INLINE_FRAME)
1863 /* Claim we are stepping in the calling frame. An empty step
1864 range means that we will stop once we aren't in a function
1865 called by that frame. We don't use the magic "1" value for
1866 step_range_end, because then infrun will think this is nexti,
1867 and not step over the rest of this inlined function call. */
1868 set_step_info (tp, frame, {});
1869 tp->control.step_range_start = get_frame_pc (frame);
1870 tp->control.step_range_end = tp->control.step_range_start;
1871 tp->control.step_over_calls = STEP_OVER_ALL;
1873 /* Print info on the selected frame, including level number but not
1874 source. */
1875 if (from_tty)
1877 gdb_printf (_("Run till exit from "));
1878 print_stack_frame (get_selected_frame (nullptr), 1, LOCATION, 0);
1881 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1882 return;
1885 /* Find the function we will return from. */
1886 frame_info_ptr callee_frame = get_selected_frame (nullptr);
1887 sm->function = find_pc_function (get_frame_pc (callee_frame));
1888 sm->return_buf = 0; /* Initialize buffer address is not available. */
1890 /* Determine the return convention. If it is RETURN_VALUE_STRUCT_CONVENTION,
1891 attempt to determine the address of the return buffer. */
1892 if (sm->function != nullptr)
1894 enum return_value_convention return_value;
1895 struct gdbarch *gdbarch = get_frame_arch (callee_frame);
1897 struct type * val_type
1898 = check_typedef (sm->function->type ()->target_type ());
1900 return_value
1901 = gdbarch_return_value_as_value (gdbarch,
1902 read_var_value (sm->function, nullptr,
1903 callee_frame),
1904 val_type, nullptr, nullptr, nullptr);
1906 if (return_value == RETURN_VALUE_STRUCT_CONVENTION
1907 && val_type->code () != TYPE_CODE_VOID)
1908 sm->return_buf = gdbarch_get_return_buf_addr (gdbarch, val_type,
1909 callee_frame);
1912 /* Print info on the selected frame, including level number but not
1913 source. */
1914 if (from_tty)
1916 if (execution_direction == EXEC_REVERSE)
1917 gdb_printf (_("Run back to call of "));
1918 else
1920 if (sm->function != nullptr && TYPE_NO_RETURN (sm->function->type ())
1921 && !query (_("warning: Function %s does not return normally.\n"
1922 "Try to finish anyway? "),
1923 sm->function->print_name ()))
1924 error (_("Not confirmed."));
1925 gdb_printf (_("Run till exit from "));
1928 print_stack_frame (callee_frame, 1, LOCATION, 0);
1931 if (execution_direction == EXEC_REVERSE)
1932 finish_backward (sm);
1933 else
1935 frame = skip_finish_frames (frame);
1937 if (frame == nullptr)
1938 error (_("Cannot find the caller frame."));
1940 finish_forward (sm, frame);
1945 static void
1946 info_program_command (const char *args, int from_tty)
1948 scoped_restore_current_thread restore_thread;
1950 thread_info *tp;
1952 /* In non-stop, since every thread is controlled individually, we'll
1953 show execution info about the current thread. In all-stop, we'll
1954 show execution info about the last stop. */
1956 if (non_stop)
1958 if (!target_has_execution ())
1960 gdb_printf (_("The program being debugged is not being run.\n"));
1961 return;
1964 if (inferior_ptid == null_ptid)
1965 error (_("No selected thread."));
1967 tp = inferior_thread ();
1969 gdb_printf (_("Selected thread %s (%s).\n"),
1970 print_thread_id (tp),
1971 target_pid_to_str (tp->ptid).c_str ());
1973 if (tp->state == THREAD_EXITED)
1975 gdb_printf (_("Selected thread has exited.\n"));
1976 return;
1978 else if (tp->state == THREAD_RUNNING)
1980 gdb_printf (_("Selected thread is running.\n"));
1981 return;
1984 else
1986 tp = get_previous_thread ();
1988 if (tp == nullptr)
1990 gdb_printf (_("The program being debugged is not being run.\n"));
1991 return;
1994 switch_to_thread (tp);
1996 gdb_printf (_("Last stopped for thread %s (%s).\n"),
1997 print_thread_id (tp),
1998 target_pid_to_str (tp->ptid).c_str ());
2000 if (tp->state == THREAD_EXITED)
2002 gdb_printf (_("Thread has since exited.\n"));
2003 return;
2006 if (tp->state == THREAD_RUNNING)
2008 gdb_printf (_("Thread is now running.\n"));
2009 return;
2013 int num;
2014 bpstat *bs = tp->control.stop_bpstat;
2015 int stat = bpstat_num (&bs, &num);
2017 target_files_info ();
2018 gdb_printf (_("Program stopped at %s.\n"),
2019 paddress (current_inferior ()->arch (), tp->stop_pc ()));
2020 if (tp->control.stop_step)
2021 gdb_printf (_("It stopped after being stepped.\n"));
2022 else if (stat != 0)
2024 /* There may be several breakpoints in the same place, so this
2025 isn't as strange as it seems. */
2026 while (stat != 0)
2028 if (stat < 0)
2030 gdb_printf (_("It stopped at a breakpoint "
2031 "that has since been deleted.\n"));
2033 else
2034 gdb_printf (_("It stopped at breakpoint %d.\n"), num);
2035 stat = bpstat_num (&bs, &num);
2038 else if (tp->stop_signal () != GDB_SIGNAL_0)
2040 gdb_printf (_("It stopped with signal %s, %s.\n"),
2041 gdb_signal_to_name (tp->stop_signal ()),
2042 gdb_signal_to_string (tp->stop_signal ()));
2045 if (from_tty)
2047 gdb_printf (_("Type \"info stack\" or \"info "
2048 "registers\" for more information.\n"));
2052 static void
2053 environment_info (const char *var, int from_tty)
2055 if (var)
2057 const char *val = current_inferior ()->environment.get (var);
2059 if (val)
2061 gdb_puts (var);
2062 gdb_puts (" = ");
2063 gdb_puts (val);
2064 gdb_puts ("\n");
2066 else
2068 gdb_puts ("Environment variable \"");
2069 gdb_puts (var);
2070 gdb_puts ("\" not defined.\n");
2073 else
2075 char **envp = current_inferior ()->environment.envp ();
2077 for (int idx = 0; envp[idx] != nullptr; ++idx)
2079 gdb_puts (envp[idx]);
2080 gdb_puts ("\n");
2085 static void
2086 set_environment_command (const char *arg, int from_tty)
2088 const char *p, *val;
2089 int nullset = 0;
2091 if (arg == 0)
2092 error_no_arg (_("environment variable and value"));
2094 /* Find separation between variable name and value. */
2095 p = (char *) strchr (arg, '=');
2096 val = (char *) strchr (arg, ' ');
2098 if (p != 0 && val != 0)
2100 /* We have both a space and an equals. If the space is before the
2101 equals, walk forward over the spaces til we see a nonspace
2102 (possibly the equals). */
2103 if (p > val)
2104 while (*val == ' ')
2105 val++;
2107 /* Now if the = is after the char following the spaces,
2108 take the char following the spaces. */
2109 if (p > val)
2110 p = val - 1;
2112 else if (val != 0 && p == 0)
2113 p = val;
2115 if (p == arg)
2116 error_no_arg (_("environment variable to set"));
2118 if (p == 0 || p[1] == 0)
2120 nullset = 1;
2121 if (p == 0)
2122 p = arg + strlen (arg); /* So that savestring below will work. */
2124 else
2126 /* Not setting variable value to null. */
2127 val = p + 1;
2128 while (*val == ' ' || *val == '\t')
2129 val++;
2132 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
2133 p--;
2135 std::string var (arg, p - arg);
2136 if (nullset)
2138 gdb_printf (_("Setting environment variable "
2139 "\"%s\" to null value.\n"),
2140 var.c_str ());
2141 current_inferior ()->environment.set (var.c_str (), "");
2143 else
2144 current_inferior ()->environment.set (var.c_str (), val);
2147 static void
2148 unset_environment_command (const char *var, int from_tty)
2150 if (var == 0)
2152 /* If there is no argument, delete all environment variables.
2153 Ask for confirmation if reading from the terminal. */
2154 if (!from_tty || query (_("Delete all environment variables? ")))
2155 current_inferior ()->environment.clear ();
2157 else
2158 current_inferior ()->environment.unset (var);
2161 /* Handle the execution path (PATH variable). */
2163 static const char path_var_name[] = "PATH";
2165 static void
2166 path_info (const char *args, int from_tty)
2168 gdb_puts ("Executable and object file path: ");
2169 gdb_puts (current_inferior ()->environment.get (path_var_name));
2170 gdb_puts ("\n");
2173 /* Add zero or more directories to the front of the execution path. */
2175 static void
2176 path_command (const char *dirname, int from_tty)
2178 const char *env;
2180 dont_repeat ();
2181 env = current_inferior ()->environment.get (path_var_name);
2182 /* Can be null if path is not set. */
2183 if (!env)
2184 env = "";
2185 std::string exec_path = env;
2186 mod_path (dirname, exec_path);
2187 current_inferior ()->environment.set (path_var_name, exec_path.c_str ());
2188 if (from_tty)
2189 path_info (nullptr, from_tty);
2193 static void
2194 pad_to_column (string_file &stream, int col)
2196 /* At least one space must be printed to separate columns. */
2197 stream.putc (' ');
2198 const int size = stream.size ();
2199 if (size < col)
2200 stream.puts (n_spaces (col - size));
2203 /* Print out the register NAME with value VAL, to FILE, in the default
2204 fashion. */
2206 static void
2207 default_print_one_register_info (struct ui_file *file,
2208 const char *name,
2209 struct value *val)
2211 struct type *regtype = val->type ();
2212 int print_raw_format;
2213 string_file format_stream;
2214 enum tab_stops
2216 value_column_1 = 15,
2217 /* Give enough room for "0x", 16 hex digits and two spaces in
2218 preceding column. */
2219 value_column_2 = value_column_1 + 2 + 16 + 2,
2222 format_stream.puts (name);
2223 pad_to_column (format_stream, value_column_1);
2225 print_raw_format = (val->entirely_available ()
2226 && !val->optimized_out ());
2228 /* If virtual format is floating, print it that way, and in raw
2229 hex. */
2230 if (regtype->code () == TYPE_CODE_FLT
2231 || regtype->code () == TYPE_CODE_DECFLOAT)
2233 struct value_print_options opts;
2234 const gdb_byte *valaddr = val->contents_for_printing ().data ();
2235 enum bfd_endian byte_order = type_byte_order (regtype);
2237 get_user_print_options (&opts);
2238 opts.deref_ref = true;
2240 common_val_print (val, &format_stream, 0, &opts, current_language);
2242 if (print_raw_format)
2244 pad_to_column (format_stream, value_column_2);
2245 format_stream.puts ("(raw ");
2246 print_hex_chars (&format_stream, valaddr, regtype->length (),
2247 byte_order, true);
2248 format_stream.putc (')');
2251 else
2253 struct value_print_options opts;
2255 /* Print the register in hex. */
2256 get_formatted_print_options (&opts, 'x');
2257 opts.deref_ref = true;
2258 common_val_print (val, &format_stream, 0, &opts, current_language);
2259 /* If not a vector register, print it also according to its
2260 natural format. */
2261 if (print_raw_format && regtype->is_vector () == 0)
2263 pad_to_column (format_stream, value_column_2);
2264 get_user_print_options (&opts);
2265 opts.deref_ref = true;
2266 common_val_print (val, &format_stream, 0, &opts, current_language);
2270 gdb_puts (format_stream.c_str (), file);
2271 gdb_printf (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. */
2285 void
2286 default_print_registers_info (struct gdbarch *gdbarch,
2287 struct ui_file *file,
2288 frame_info_ptr frame,
2289 int regnum, int print_all)
2291 int i;
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
2297 specific reg. */
2298 if (regnum == -1)
2300 if (print_all)
2302 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
2303 continue;
2305 else
2307 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
2308 continue;
2311 else
2313 if (i != regnum)
2314 continue;
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)) == '\0')
2320 continue;
2322 default_print_one_register_info (file,
2323 gdbarch_register_name (gdbarch, i),
2324 value_of_register (i, frame));
2328 void
2329 registers_info (const char *addr_exp, int fpregs)
2331 frame_info_ptr frame;
2332 struct gdbarch *gdbarch;
2334 if (!target_has_registers ())
2335 error (_("The program has no registers now."));
2336 frame = get_selected_frame (nullptr);
2337 gdbarch = get_frame_arch (frame);
2339 if (!addr_exp)
2341 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2342 frame, -1, fpregs);
2343 return;
2346 while (*addr_exp != '\0')
2348 const char *start;
2349 const char *end;
2351 /* Skip leading white space. */
2352 addr_exp = skip_spaces (addr_exp);
2354 /* Discard any leading ``$''. Check that there is something
2355 resembling a register following it. */
2356 if (addr_exp[0] == '$')
2357 addr_exp++;
2358 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
2359 error (_("Missing register name"));
2361 /* Find the start/end of this register name/num/group. */
2362 start = addr_exp;
2363 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
2364 addr_exp++;
2365 end = addr_exp;
2367 /* Figure out what we've found and display it. */
2369 /* A register name? */
2371 int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start);
2373 if (regnum >= 0)
2375 /* User registers lie completely outside of the range of
2376 normal registers. Catch them early so that the target
2377 never sees them. */
2378 if (regnum >= gdbarch_num_cooked_regs (gdbarch))
2380 struct value *regval = value_of_user_reg (regnum, frame);
2381 const char *regname = user_reg_map_regnum_to_name (gdbarch,
2382 regnum);
2384 /* Print in the same fashion
2385 gdbarch_print_registers_info's default
2386 implementation prints. */
2387 default_print_one_register_info (gdb_stdout,
2388 regname,
2389 regval);
2391 else
2392 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2393 frame, regnum, fpregs);
2394 continue;
2398 /* A register group? */
2400 const struct reggroup *group = nullptr;
2401 for (const struct reggroup *g : gdbarch_reggroups (gdbarch))
2403 /* Don't bother with a length check. Should the user
2404 enter a short register group name, go with the first
2405 group that matches. */
2406 if (strncmp (start, g->name (), end - start) == 0)
2408 group = g;
2409 break;
2412 if (group != nullptr)
2414 int regnum;
2416 for (regnum = 0;
2417 regnum < gdbarch_num_cooked_regs (gdbarch);
2418 regnum++)
2420 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
2421 gdbarch_print_registers_info (gdbarch,
2422 gdb_stdout, frame,
2423 regnum, fpregs);
2425 continue;
2429 /* Nothing matched. */
2430 error (_("Invalid register `%.*s'"), (int) (end - start), start);
2434 static void
2435 info_all_registers_command (const char *addr_exp, int from_tty)
2437 registers_info (addr_exp, 1);
2440 static void
2441 info_registers_command (const char *addr_exp, int from_tty)
2443 registers_info (addr_exp, 0);
2446 static void
2447 print_vector_info (struct ui_file *file,
2448 frame_info_ptr frame, const char *args)
2450 struct gdbarch *gdbarch = get_frame_arch (frame);
2452 if (gdbarch_print_vector_info_p (gdbarch))
2453 gdbarch_print_vector_info (gdbarch, file, frame, args);
2454 else
2456 int regnum;
2457 int printed_something = 0;
2459 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
2461 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
2463 printed_something = 1;
2464 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2467 if (!printed_something)
2468 gdb_printf (file, "No vector information\n");
2472 static void
2473 info_vector_command (const char *args, int from_tty)
2475 if (!target_has_registers ())
2476 error (_("The program has no registers now."));
2478 print_vector_info (gdb_stdout, get_selected_frame (nullptr), args);
2481 /* Kill the inferior process. Make us have no inferior. */
2483 static void
2484 kill_command (const char *arg, int from_tty)
2486 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2487 It should be a distinct flag that indicates that a target is active, cuz
2488 some targets don't have processes! */
2490 if (inferior_ptid == null_ptid)
2491 error (_("The program is not being run."));
2492 if (!query (_("Kill the program being debugged? ")))
2493 error (_("Not confirmed."));
2495 int pid = current_inferior ()->pid;
2496 /* Save the pid as a string before killing the inferior, since that
2497 may unpush the current target, and we need the string after. */
2498 std::string pid_str = target_pid_to_str (ptid_t (pid));
2499 int infnum = current_inferior ()->num;
2501 target_kill ();
2503 update_previous_thread ();
2505 if (print_inferior_events)
2506 gdb_printf (_("[Inferior %d (%s) killed]\n"),
2507 infnum, pid_str.c_str ());
2510 /* Used in `attach&' command. Proceed threads of inferior INF iff
2511 they stopped due to debugger request, and when they did, they
2512 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2513 have been explicitly been told to stop. */
2515 static void
2516 proceed_after_attach (inferior *inf)
2518 /* Don't error out if the current thread is running, because
2519 there may be other stopped threads. */
2521 /* Backup current thread and selected frame. */
2522 scoped_restore_current_thread restore_thread;
2524 for (thread_info *thread : inf->non_exited_threads ())
2525 if (!thread->executing ()
2526 && !thread->stop_requested
2527 && thread->stop_signal () == GDB_SIGNAL_0)
2529 switch_to_thread (thread);
2530 clear_proceed_status (0);
2531 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2535 /* See inferior.h. */
2537 void
2538 setup_inferior (int from_tty)
2540 struct inferior *inferior;
2542 inferior = current_inferior ();
2543 inferior->needs_setup = false;
2545 /* If no exec file is yet known, try to determine it from the
2546 process itself. */
2547 if (get_exec_file (0) == nullptr)
2548 exec_file_locate_attach (inferior_ptid.pid (), 1, from_tty);
2549 else
2551 reopen_exec_file ();
2552 reread_symbols (from_tty);
2555 /* Take any necessary post-attaching actions for this platform. */
2556 target_post_attach (inferior_ptid.pid ());
2558 post_create_inferior (from_tty);
2561 /* What to do after the first program stops after attaching. */
2562 enum attach_post_wait_mode
2564 /* Do nothing. Leaves threads as they are. */
2565 ATTACH_POST_WAIT_NOTHING,
2567 /* Re-resume threads that are marked running. */
2568 ATTACH_POST_WAIT_RESUME,
2570 /* Stop all threads. */
2571 ATTACH_POST_WAIT_STOP,
2574 /* Called after we've attached to a process and we've seen it stop for
2575 the first time. Resume, stop, or don't touch the threads according
2576 to MODE. */
2578 static void
2579 attach_post_wait (int from_tty, enum attach_post_wait_mode mode)
2581 struct inferior *inferior;
2583 inferior = current_inferior ();
2584 inferior->control.stop_soon = NO_STOP_QUIETLY;
2586 if (inferior->needs_setup)
2587 setup_inferior (from_tty);
2589 if (mode == ATTACH_POST_WAIT_RESUME)
2591 /* The user requested an `attach&', so be sure to leave threads
2592 that didn't get a signal running. */
2594 /* Immediately resume all suspended threads of this inferior,
2595 and this inferior only. This should have no effect on
2596 already running threads. If a thread has been stopped with a
2597 signal, leave it be. */
2598 if (non_stop)
2599 proceed_after_attach (inferior);
2600 else
2602 if (inferior_thread ()->stop_signal () == GDB_SIGNAL_0)
2604 clear_proceed_status (0);
2605 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2609 else if (mode == ATTACH_POST_WAIT_STOP)
2611 /* The user requested a plain `attach', so be sure to leave
2612 the inferior stopped. */
2614 /* At least the current thread is already stopped. */
2616 /* In all-stop, by definition, all threads have to be already
2617 stopped at this point. In non-stop, however, although the
2618 selected thread is stopped, others may still be executing.
2619 Be sure to explicitly stop all threads of the process. This
2620 should have no effect on already stopped threads. */
2621 if (non_stop)
2622 target_stop (ptid_t (inferior->pid));
2623 else if (target_is_non_stop_p ())
2625 struct thread_info *lowest = inferior_thread ();
2627 stop_all_threads ("attaching");
2629 /* It's not defined which thread will report the attach
2630 stop. For consistency, always select the thread with
2631 lowest GDB number, which should be the main thread, if it
2632 still exists. */
2633 for (thread_info *thread : current_inferior ()->non_exited_threads ())
2634 if (thread->inf->num < lowest->inf->num
2635 || thread->per_inf_num < lowest->per_inf_num)
2636 lowest = thread;
2638 switch_to_thread (lowest);
2641 /* Tell the user/frontend where we're stopped. */
2642 normal_stop ();
2643 if (deprecated_attach_hook)
2644 deprecated_attach_hook ();
2648 /* "attach" command entry point. Takes a program started up outside
2649 of gdb and ``attaches'' to it. This stops it cold in its tracks
2650 and allows us to start debugging it. */
2652 void
2653 attach_command (const char *args, int from_tty)
2655 int async_exec;
2656 struct target_ops *attach_target;
2657 struct inferior *inferior = current_inferior ();
2658 enum attach_post_wait_mode mode;
2660 dont_repeat (); /* Not for the faint of heart */
2662 scoped_disable_commit_resumed disable_commit_resumed ("attaching");
2664 if (gdbarch_has_global_solist (current_inferior ()->arch ()))
2665 /* Don't complain if all processes share the same symbol
2666 space. */
2668 else if (target_has_execution ())
2670 if (query (_("A program is being debugged already. Kill it? ")))
2671 target_kill ();
2672 else
2673 error (_("Not killed."));
2676 /* Clean up any leftovers from other runs. Some other things from
2677 this function should probably be moved into target_pre_inferior. */
2678 target_pre_inferior (from_tty);
2680 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
2681 args = stripped.get ();
2683 attach_target = find_attach_target ();
2685 prepare_execution_command (attach_target, async_exec);
2687 if (non_stop && !attach_target->supports_non_stop ())
2688 error (_("Cannot attach to this target in non-stop mode"));
2690 attach_target->attach (args, from_tty);
2691 /* to_attach should push the target, so after this point we
2692 shouldn't refer to attach_target again. */
2693 attach_target = nullptr;
2695 infrun_debug_show_threads ("immediately after attach",
2696 current_inferior ()->non_exited_threads ());
2698 /* Enable async mode if it is supported by the target. */
2699 if (target_can_async_p ())
2700 target_async (true);
2702 /* Set up the "saved terminal modes" of the inferior
2703 based on what modes we are starting it with. */
2704 target_terminal::init ();
2706 /* Install inferior's terminal modes. This may look like a no-op,
2707 as we've just saved them above, however, this does more than
2708 restore terminal settings:
2710 - installs a SIGINT handler that forwards SIGINT to the inferior.
2711 Otherwise a Ctrl-C pressed just while waiting for the initial
2712 stop would end up as a spurious Quit.
2714 - removes stdin from the event loop, which we need if attaching
2715 in the foreground, otherwise on targets that report an initial
2716 stop on attach (which are most) we'd process input/commands
2717 while we're in the event loop waiting for that stop. That is,
2718 before the attach continuation runs and the command is really
2719 finished. */
2720 target_terminal::inferior ();
2722 /* Set up execution context to know that we should return from
2723 wait_for_inferior as soon as the target reports a stop. */
2724 init_wait_for_inferior ();
2726 inferior->needs_setup = true;
2728 if (target_is_non_stop_p ())
2730 /* If we find that the current thread isn't stopped, explicitly
2731 do so now, because we're going to install breakpoints and
2732 poke at memory. */
2734 if (async_exec)
2735 /* The user requested an `attach&'; stop just one thread. */
2736 target_stop (inferior_ptid);
2737 else
2738 /* The user requested an `attach', so stop all threads of this
2739 inferior. */
2740 target_stop (ptid_t (inferior_ptid.pid ()));
2743 /* Check for exec file mismatch, and let the user solve it. */
2744 validate_exec_file (from_tty);
2746 mode = async_exec ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_STOP;
2748 /* Some system don't generate traps when attaching to inferior.
2749 E.g. Mach 3 or GNU hurd. */
2750 if (!target_attach_no_wait ())
2752 /* Careful here. See comments in inferior.h. Basically some
2753 OSes don't ignore SIGSTOPs on continue requests anymore. We
2754 need a way for handle_inferior_event to reset the stop_signal
2755 variable after an attach, and this is what
2756 STOP_QUIETLY_NO_SIGSTOP is for. */
2757 inferior->control.stop_soon = STOP_QUIETLY_NO_SIGSTOP;
2759 /* Wait for stop. */
2760 inferior->add_continuation ([=] ()
2762 attach_post_wait (from_tty, mode);
2765 /* Let infrun consider waiting for events out of this
2766 target. */
2767 inferior->process_target ()->threads_executing = true;
2769 if (!target_is_async_p ())
2770 mark_infrun_async_event_handler ();
2771 return;
2773 else
2774 attach_post_wait (from_tty, mode);
2776 disable_commit_resumed.reset_and_commit ();
2779 /* We had just found out that the target was already attached to an
2780 inferior. PTID points at a thread of this new inferior, that is
2781 the most likely to be stopped right now, but not necessarily so.
2782 The new inferior is assumed to be already added to the inferior
2783 list at this point. If LEAVE_RUNNING, then leave the threads of
2784 this inferior running, except those we've explicitly seen reported
2785 as stopped. */
2787 void
2788 notice_new_inferior (thread_info *thr, bool leave_running, int from_tty)
2790 enum attach_post_wait_mode mode
2791 = leave_running ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_NOTHING;
2793 std::optional<scoped_restore_current_thread> restore_thread;
2795 if (inferior_ptid != null_ptid)
2796 restore_thread.emplace ();
2798 /* Avoid reading registers -- we haven't fetched the target
2799 description yet. */
2800 switch_to_thread_no_regs (thr);
2802 /* When we "notice" a new inferior we need to do all the things we
2803 would normally do if we had just attached to it. */
2805 if (thr->executing ())
2807 struct inferior *inferior = current_inferior ();
2809 /* We're going to install breakpoints, and poke at memory,
2810 ensure that the inferior is stopped for a moment while we do
2811 that. */
2812 target_stop (inferior_ptid);
2814 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
2816 /* Wait for stop before proceeding. */
2817 inferior->add_continuation ([=] ()
2819 attach_post_wait (from_tty, mode);
2822 return;
2825 attach_post_wait (from_tty, mode);
2829 * detach_command --
2830 * takes a program previously attached to and detaches it.
2831 * The program resumes execution and will no longer stop
2832 * on signals, etc. We better not have left any breakpoints
2833 * in the program or it'll die when it hits one. For this
2834 * to work, it may be necessary for the process to have been
2835 * previously attached. It *might* work if the program was
2836 * started via the normal ptrace (PTRACE_TRACEME).
2839 void
2840 detach_command (const char *args, int from_tty)
2842 dont_repeat (); /* Not for the faint of heart. */
2844 if (inferior_ptid == null_ptid)
2845 error (_("The program is not being run."));
2847 scoped_disable_commit_resumed disable_commit_resumed ("detaching");
2849 query_if_trace_running (from_tty);
2851 disconnect_tracing ();
2853 /* Hold a strong reference to the target while (maybe)
2854 detaching the parent. Otherwise detaching could close the
2855 target. */
2856 auto target_ref
2857 = target_ops_ref::new_reference (current_inferior ()->process_target ());
2859 /* Save this before detaching, since detaching may unpush the
2860 process_stratum target. */
2861 bool was_non_stop_p = target_is_non_stop_p ();
2863 target_detach (current_inferior (), from_tty);
2865 update_previous_thread ();
2867 /* The current inferior process was just detached successfully. Get
2868 rid of breakpoints that no longer make sense. Note we don't do
2869 this within target_detach because that is also used when
2870 following child forks, and in that case we will want to transfer
2871 breakpoints to the child, not delete them. */
2872 breakpoint_init_inferior (inf_exited);
2874 /* If the solist is global across inferiors, don't clear it when we
2875 detach from a single inferior. */
2876 if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
2877 no_shared_libraries (nullptr, from_tty);
2879 if (deprecated_detach_hook)
2880 deprecated_detach_hook ();
2882 if (!was_non_stop_p)
2883 restart_after_all_stop_detach (as_process_stratum_target (target_ref.get ()));
2885 disable_commit_resumed.reset_and_commit ();
2888 /* Disconnect from the current target without resuming it (leaving it
2889 waiting for a debugger).
2891 We'd better not have left any breakpoints in the program or the
2892 next debugger will get confused. Currently only supported for some
2893 remote targets, since the normal attach mechanisms don't work on
2894 stopped processes on some native platforms (e.g. GNU/Linux). */
2896 static void
2897 disconnect_command (const char *args, int from_tty)
2899 dont_repeat (); /* Not for the faint of heart. */
2900 query_if_trace_running (from_tty);
2901 disconnect_tracing ();
2902 target_disconnect (args, from_tty);
2903 no_shared_libraries (nullptr, from_tty);
2904 init_thread_list ();
2905 update_previous_thread ();
2906 if (deprecated_detach_hook)
2907 deprecated_detach_hook ();
2910 /* Stop PTID in the current target, and tag the PTID threads as having
2911 been explicitly requested to stop. PTID can be a thread, a
2912 process, or minus_one_ptid, meaning all threads of all inferiors of
2913 the current target. */
2915 static void
2916 stop_current_target_threads_ns (ptid_t ptid)
2918 target_stop (ptid);
2920 /* Tag the thread as having been explicitly requested to stop, so
2921 other parts of gdb know not to resume this thread automatically,
2922 if it was stopped due to an internal event. Limit this to
2923 non-stop mode, as when debugging a multi-threaded application in
2924 all-stop mode, we will only get one stop event --- it's undefined
2925 which thread will report the event. */
2926 set_stop_requested (current_inferior ()->process_target (),
2927 ptid, 1);
2930 /* See inferior.h. */
2932 void
2933 interrupt_target_1 (bool all_threads)
2935 scoped_disable_commit_resumed disable_commit_resumed ("interrupting");
2937 if (non_stop)
2939 if (all_threads)
2941 scoped_restore_current_thread restore_thread;
2943 for (inferior *inf : all_inferiors ())
2945 switch_to_inferior_no_thread (inf);
2946 stop_current_target_threads_ns (minus_one_ptid);
2949 else
2950 stop_current_target_threads_ns (inferior_ptid);
2952 else
2953 target_interrupt ();
2955 disable_commit_resumed.reset_and_commit ();
2958 /* interrupt [-a]
2959 Stop the execution of the target while running in async mode, in
2960 the background. In all-stop, stop the whole process. In non-stop
2961 mode, stop the current thread only by default, or stop all threads
2962 if the `-a' switch is used. */
2964 static void
2965 interrupt_command (const char *args, int from_tty)
2967 if (target_can_async_p ())
2969 int all_threads = 0;
2971 dont_repeat (); /* Not for the faint of heart. */
2973 if (args != nullptr
2974 && startswith (args, "-a"))
2975 all_threads = 1;
2977 interrupt_target_1 (all_threads);
2981 /* See inferior.h. */
2983 void
2984 default_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
2985 frame_info_ptr frame, const char *args)
2987 int regnum;
2988 int printed_something = 0;
2990 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
2992 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
2994 printed_something = 1;
2995 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2998 if (!printed_something)
2999 gdb_printf (file, "No floating-point info "
3000 "available for this processor.\n");
3003 static void
3004 info_float_command (const char *args, int from_tty)
3006 frame_info_ptr frame;
3008 if (!target_has_registers ())
3009 error (_("The program has no registers now."));
3011 frame = get_selected_frame (nullptr);
3012 gdbarch_print_float_info (get_frame_arch (frame), gdb_stdout, frame, args);
3015 /* Implement `info proc' family of commands. */
3017 static void
3018 info_proc_cmd_1 (const char *args, enum info_proc_what what, int from_tty)
3020 struct gdbarch *gdbarch = get_current_arch ();
3022 if (!target_info_proc (args, what))
3024 if (gdbarch_info_proc_p (gdbarch))
3025 gdbarch_info_proc (gdbarch, args, what);
3026 else
3027 error (_("Not supported on this target."));
3031 /* Implement `info proc' when given without any further parameters. */
3033 static void
3034 info_proc_cmd (const char *args, int from_tty)
3036 info_proc_cmd_1 (args, IP_MINIMAL, from_tty);
3039 /* Implement `info proc mappings'. */
3041 static void
3042 info_proc_cmd_mappings (const char *args, int from_tty)
3044 info_proc_cmd_1 (args, IP_MAPPINGS, from_tty);
3047 /* Implement `info proc stat'. */
3049 static void
3050 info_proc_cmd_stat (const char *args, int from_tty)
3052 info_proc_cmd_1 (args, IP_STAT, from_tty);
3055 /* Implement `info proc status'. */
3057 static void
3058 info_proc_cmd_status (const char *args, int from_tty)
3060 info_proc_cmd_1 (args, IP_STATUS, from_tty);
3063 /* Implement `info proc cwd'. */
3065 static void
3066 info_proc_cmd_cwd (const char *args, int from_tty)
3068 info_proc_cmd_1 (args, IP_CWD, from_tty);
3071 /* Implement `info proc cmdline'. */
3073 static void
3074 info_proc_cmd_cmdline (const char *args, int from_tty)
3076 info_proc_cmd_1 (args, IP_CMDLINE, from_tty);
3079 /* Implement `info proc exe'. */
3081 static void
3082 info_proc_cmd_exe (const char *args, int from_tty)
3084 info_proc_cmd_1 (args, IP_EXE, from_tty);
3087 /* Implement `info proc files'. */
3089 static void
3090 info_proc_cmd_files (const char *args, int from_tty)
3092 info_proc_cmd_1 (args, IP_FILES, from_tty);
3095 /* Implement `info proc all'. */
3097 static void
3098 info_proc_cmd_all (const char *args, int from_tty)
3100 info_proc_cmd_1 (args, IP_ALL, from_tty);
3103 /* Implement `show print finish'. */
3105 static void
3106 show_print_finish (struct ui_file *file, int from_tty,
3107 struct cmd_list_element *c,
3108 const char *value)
3110 gdb_printf (file, _("\
3111 Printing of return value after `finish' is %s.\n"),
3112 value);
3116 /* This help string is used for the run, start, and starti commands.
3117 It is defined as a macro to prevent duplication. */
3119 #define RUN_ARGS_HELP \
3120 "You may specify arguments to give it.\n\
3121 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3122 shell that will start the program (specified by the \"$SHELL\" environment\n\
3123 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3124 are also allowed.\n\
3126 With no arguments, uses arguments last specified (with \"run\" or \n\
3127 \"set args\"). To cancel previous arguments and run with no arguments,\n\
3128 use \"set args\" without arguments.\n\
3130 To start the inferior without using a shell, use \"set startup-with-shell off\"."
3132 void _initialize_infcmd ();
3133 void
3134 _initialize_infcmd ()
3136 static struct cmd_list_element *info_proc_cmdlist;
3137 struct cmd_list_element *c = nullptr;
3139 /* Add the filename of the terminal connected to inferior I/O. */
3140 auto tty_set_show
3141 = add_setshow_optional_filename_cmd ("inferior-tty", class_run, _("\
3142 Set terminal for future runs of program being debugged."), _(" \
3143 Show terminal for future runs of program being debugged."), _(" \
3144 Usage: set inferior-tty [TTY]\n\n \
3145 If TTY is omitted, the default behavior of using the same terminal as GDB\n \
3146 is restored."),
3147 set_tty_value,
3148 get_tty_value,
3149 show_inferior_tty_command,
3150 &setlist, &showlist);
3151 add_alias_cmd ("tty", tty_set_show.set, class_run, 0, &cmdlist);
3153 auto args_set_show
3154 = add_setshow_string_noescape_cmd ("args", class_run, _("\
3155 Set argument list to give program being debugged when it is started."), _("\
3156 Show argument list to give program being debugged when it is started."), _("\
3157 Follow this command with any number of args, to be passed to the program."),
3158 set_args_value,
3159 get_args_value,
3160 show_args_command,
3161 &setlist, &showlist);
3162 set_cmd_completer (args_set_show.set, filename_completer);
3164 auto cwd_set_show
3165 = add_setshow_string_noescape_cmd ("cwd", class_run, _("\
3166 Set the current working directory to be used when the inferior is started.\n \
3167 Changing this setting does not have any effect on inferiors that are\n \
3168 already running."),
3169 _("\
3170 Show the current working directory that is used when the inferior is started."),
3171 _("\
3172 Use this command to change the current working directory that will be used\n\
3173 when the inferior is started. This setting does not affect GDB's current\n\
3174 working directory."),
3175 set_cwd_value, get_inferior_cwd,
3176 show_cwd_command,
3177 &setlist, &showlist);
3178 set_cmd_completer (cwd_set_show.set, filename_completer);
3180 c = add_cmd ("environment", no_class, environment_info, _("\
3181 The environment to give the program, or one variable's value.\n\
3182 With an argument VAR, prints the value of environment variable VAR to\n\
3183 give the program being debugged. With no arguments, prints the entire\n\
3184 environment to be given to the program."), &showlist);
3185 set_cmd_completer (c, noop_completer);
3187 add_basic_prefix_cmd ("unset", no_class,
3188 _("Complement to certain \"set\" commands."),
3189 &unsetlist, 0, &cmdlist);
3191 c = add_cmd ("environment", class_run, unset_environment_command, _("\
3192 Cancel environment variable VAR for the program.\n\
3193 This does not affect the program until the next \"run\" command."),
3194 &unsetlist);
3195 set_cmd_completer (c, noop_completer);
3197 c = add_cmd ("environment", class_run, set_environment_command, _("\
3198 Set environment variable value to give the program.\n\
3199 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3200 VALUES of environment variables are uninterpreted strings.\n\
3201 This does not affect the program until the next \"run\" command."),
3202 &setlist);
3203 set_cmd_completer (c, noop_completer);
3205 c = add_com ("path", class_files, path_command, _("\
3206 Add directory DIR(s) to beginning of search path for object files.\n\
3207 $cwd in the path means the current working directory.\n\
3208 This path is equivalent to the $PATH shell variable. It is a list of\n\
3209 directories, separated by colons. These directories are searched to find\n\
3210 fully linked executable files and separately compiled object files as \
3211 needed."));
3212 set_cmd_completer (c, filename_completer);
3214 c = add_cmd ("paths", no_class, path_info, _("\
3215 Current search path for finding 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 \
3220 needed."),
3221 &showlist);
3222 set_cmd_completer (c, noop_completer);
3224 add_prefix_cmd ("kill", class_run, kill_command,
3225 _("Kill execution of program being debugged."),
3226 &killlist, 0, &cmdlist);
3228 add_com ("attach", class_run, attach_command, _("\
3229 Attach to a process or file outside of GDB.\n\
3230 This command attaches to another target, of the same type as your last\n\
3231 \"target\" command (\"info files\" will show your target stack).\n\
3232 The command may take as argument a process id or a device file.\n\
3233 For a process id, you must have permission to send the process a signal,\n\
3234 and it must have the same effective uid as the debugger.\n\
3235 When using \"attach\" with a process id, the debugger finds the\n\
3236 program running in the process, looking first in the current working\n\
3237 directory, or (if not found there) using the source file search path\n\
3238 (see the \"directory\" command). You can also use the \"file\" command\n\
3239 to specify the program, and to load its symbol table."));
3241 add_prefix_cmd ("detach", class_run, detach_command, _("\
3242 Detach a process or file previously attached.\n\
3243 If a process, it is no longer traced, and it continues its execution. If\n\
3244 you were debugging a file, the file is closed and gdb no longer accesses it."),
3245 &detachlist, 0, &cmdlist);
3247 add_com ("disconnect", class_run, disconnect_command, _("\
3248 Disconnect from a target.\n\
3249 The target will wait for another debugger to connect. Not available for\n\
3250 all targets."));
3252 c = add_com ("signal", class_run, signal_command, _("\
3253 Continue program with the specified signal.\n\
3254 Usage: signal SIGNAL\n\
3255 The SIGNAL argument is processed the same as the handle command.\n\
3257 An argument of \"0\" means continue the program without sending it a signal.\n\
3258 This is useful in cases where the program stopped because of a signal,\n\
3259 and you want to resume the program while discarding the signal.\n\
3261 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3262 the current thread only."));
3263 set_cmd_completer (c, signal_completer);
3265 c = add_com ("queue-signal", class_run, queue_signal_command, _("\
3266 Queue a signal to be delivered to the current thread when it is resumed.\n\
3267 Usage: queue-signal SIGNAL\n\
3268 The SIGNAL argument is processed the same as the handle command.\n\
3269 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3271 An argument of \"0\" means remove any currently queued signal from\n\
3272 the current thread. This is useful in cases where the program stopped\n\
3273 because of a signal, and you want to resume it while discarding the signal.\n\
3275 In a multi-threaded program the signal is queued with, or discarded from,\n\
3276 the current thread only."));
3277 set_cmd_completer (c, signal_completer);
3279 cmd_list_element *stepi_cmd
3280 = add_com ("stepi", class_run, stepi_command, _("\
3281 Step one instruction exactly.\n\
3282 Usage: stepi [N]\n\
3283 Argument N means step N times (or till program stops for another \
3284 reason)."));
3285 add_com_alias ("si", stepi_cmd, class_run, 0);
3287 cmd_list_element *nexti_cmd
3288 = add_com ("nexti", class_run, nexti_command, _("\
3289 Step one instruction, but proceed through subroutine calls.\n\
3290 Usage: nexti [N]\n\
3291 Argument N means step N times (or till program stops for another \
3292 reason)."));
3293 add_com_alias ("ni", nexti_cmd, class_run, 0);
3295 cmd_list_element *finish_cmd
3296 = add_com ("finish", class_run, finish_command, _("\
3297 Execute until selected stack frame returns.\n\
3298 Usage: finish\n\
3299 Upon return, the value returned is printed and put in the value history."));
3300 add_com_alias ("fin", finish_cmd, class_run, 1);
3302 cmd_list_element *next_cmd
3303 = add_com ("next", class_run, next_command, _("\
3304 Step program, proceeding through subroutine calls.\n\
3305 Usage: next [N]\n\
3306 Unlike \"step\", if the current source line calls a subroutine,\n\
3307 this command does not enter the subroutine, but instead steps over\n\
3308 the call, in effect treating it as a single source line."));
3309 add_com_alias ("n", next_cmd, class_run, 1);
3311 cmd_list_element *step_cmd
3312 = add_com ("step", class_run, step_command, _("\
3313 Step program until it reaches a different source line.\n\
3314 Usage: step [N]\n\
3315 Argument N means step N times (or till program stops for another \
3316 reason)."));
3317 add_com_alias ("s", step_cmd, class_run, 1);
3319 cmd_list_element *until_cmd
3320 = add_com ("until", class_run, until_command, _("\
3321 Execute until past the current line or past a LOCATION.\n\
3322 Execute until the program reaches a source line greater than the current\n\
3323 or a specified location (same args as break command) within the current \
3324 frame."));
3325 set_cmd_completer (until_cmd, location_completer);
3326 add_com_alias ("u", until_cmd, class_run, 1);
3328 c = add_com ("advance", class_run, advance_command, _("\
3329 Continue the program up to the given location (same form as args for break \
3330 command).\n\
3331 Execution will also stop upon exit from the current stack frame."));
3332 set_cmd_completer (c, location_completer);
3334 cmd_list_element *jump_cmd
3335 = add_com ("jump", class_run, jump_command, _("\
3336 Continue program being debugged at specified line or address.\n\
3337 Usage: jump LOCATION\n\
3338 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3339 for an address to start at."));
3340 set_cmd_completer (jump_cmd, location_completer);
3341 add_com_alias ("j", jump_cmd, class_run, 1);
3343 cmd_list_element *continue_cmd
3344 = add_com ("continue", class_run, continue_command, _("\
3345 Continue program being debugged, after signal or breakpoint.\n\
3346 Usage: continue [N]\n\
3347 If proceeding from breakpoint, a number N may be used as an argument,\n\
3348 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3349 the breakpoint won't break until the Nth time it is reached).\n\
3351 If non-stop mode is enabled, continue only the current thread,\n\
3352 otherwise all the threads in the program are continued. To \n\
3353 continue all stopped threads in non-stop mode, use the -a option.\n\
3354 Specifying -a and an ignore count simultaneously is an error."));
3355 add_com_alias ("c", continue_cmd, class_run, 1);
3356 add_com_alias ("fg", continue_cmd, class_run, 1);
3358 cmd_list_element *run_cmd
3359 = add_com ("run", class_run, run_command, _("\
3360 Start debugged program.\n"
3361 RUN_ARGS_HELP));
3362 set_cmd_completer (run_cmd, filename_completer);
3363 add_com_alias ("r", run_cmd, 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"
3367 RUN_ARGS_HELP));
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"
3372 RUN_ARGS_HELP));
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 cmd_list_element *info_registers_cmd
3382 = add_info ("registers", info_registers_command, _("\
3383 List of integer registers and their contents, for selected stack frame.\n\
3384 One or more register names as argument means describe the given registers.\n\
3385 One or more register group names as argument means describe the registers\n\
3386 in the named register groups."));
3387 add_info_alias ("r", info_registers_cmd, 1);
3388 set_cmd_completer (info_registers_cmd, reg_or_group_completer);
3390 c = add_info ("all-registers", info_all_registers_command, _("\
3391 List of all registers and their contents, for selected stack frame.\n\
3392 One or more register names as argument means describe the given registers.\n\
3393 One or more register group names as argument means describe the registers\n\
3394 in the named register groups."));
3395 set_cmd_completer (c, reg_or_group_completer);
3397 add_info ("program", info_program_command,
3398 _("Execution status of the program."));
3400 add_info ("float", info_float_command,
3401 _("Print the status of the floating point unit."));
3403 add_info ("vector", info_vector_command,
3404 _("Print the status of the vector unit."));
3406 add_prefix_cmd ("proc", class_info, info_proc_cmd,
3407 _("\
3408 Show additional information about a process.\n\
3409 Specify any process id, or use the program being debugged by default."),
3410 &info_proc_cmdlist,
3411 1/*allow-unknown*/, &infolist);
3413 add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\
3414 List memory regions mapped by the specified process."),
3415 &info_proc_cmdlist);
3417 add_cmd ("stat", class_info, info_proc_cmd_stat, _("\
3418 List process info from /proc/PID/stat."),
3419 &info_proc_cmdlist);
3421 add_cmd ("status", class_info, info_proc_cmd_status, _("\
3422 List process info from /proc/PID/status."),
3423 &info_proc_cmdlist);
3425 add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\
3426 List current working directory of the specified process."),
3427 &info_proc_cmdlist);
3429 add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\
3430 List command line arguments of the specified process."),
3431 &info_proc_cmdlist);
3433 add_cmd ("exe", class_info, info_proc_cmd_exe, _("\
3434 List absolute filename for executable of the specified process."),
3435 &info_proc_cmdlist);
3437 add_cmd ("files", class_info, info_proc_cmd_files, _("\
3438 List files opened by the specified process."),
3439 &info_proc_cmdlist);
3441 add_cmd ("all", class_info, info_proc_cmd_all, _("\
3442 List all available info about the specified process."),
3443 &info_proc_cmdlist);
3445 add_setshow_boolean_cmd ("finish", class_support,
3446 &finish_print, _("\
3447 Set whether `finish' prints the return value."), _("\
3448 Show whether `finish' prints the return value."), nullptr,
3449 nullptr,
3450 show_print_finish,
3451 &setprintlist, &showprintlist);