gdb: make inferior::terminal a unique ptr
[deliverable/binutils-gdb.git] / gdb / infcmd.c
... / ...
CommitLineData
1/* Memory-access and commands for "inferior" process, for GDB.
2
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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 "continuations.h"
51#include "linespec.h"
52#include "thread-fsm.h"
53#include "top.h"
54#include "interps.h"
55#include "skip.h"
56#include "gdbsupport/gdb_optional.h"
57#include "source.h"
58#include "cli/cli-style.h"
59
60/* Local functions: */
61
62static void until_next_command (int);
63
64static void step_1 (int, int, const char *);
65
66#define ERROR_NO_INFERIOR \
67 if (!target_has_execution) error (_("The program is not being run."));
68
69/* Scratch area where string containing arguments to give to the
70 program will be stored by 'set args'. As soon as anything is
71 stored, notice_args_set will move it into per-inferior storage.
72 Arguments are separated by spaces. Empty string (pointer to '\0')
73 means no args. */
74
75static char *inferior_args_scratch;
76
77/* Scratch area where the new cwd will be stored by 'set cwd'. */
78
79static char *inferior_cwd_scratch;
80
81/* Scratch area where 'set inferior-tty' will store user-provided value.
82 We'll immediate copy it into per-inferior storage. */
83
84static char *inferior_io_terminal_scratch;
85
86/* Pid of our debugged inferior, or 0 if no inferior now.
87 Since various parts of infrun.c test this to see whether there is a program
88 being debugged it should be nonzero (currently 3 is used) for remote
89 debugging. */
90
91ptid_t inferior_ptid;
92
93/* Nonzero if stopped due to completion of a stack dummy routine. */
94
95enum stop_stack_kind stop_stack_dummy;
96
97/* Nonzero if stopped due to a random (unexpected) signal in inferior
98 process. */
99
100int stopped_by_random_signal;
101
102\f
103/* Accessor routines. */
104
105/* Set the io terminal for the current inferior. Ownership of
106 TERMINAL_NAME is not transferred. */
107
108void
109set_inferior_io_terminal (const char *terminal_name)
110{
111 if (terminal_name != NULL && *terminal_name != '\0')
112 current_inferior ()->terminal.reset (xstrdup (terminal_name));
113 else
114 current_inferior ()->terminal = NULL;
115}
116
117const char *
118get_inferior_io_terminal (void)
119{
120 return current_inferior ()->terminal.get ();
121}
122
123static void
124set_inferior_tty_command (const char *args, int from_tty,
125 struct cmd_list_element *c)
126{
127 /* CLI has assigned the user-provided value to inferior_io_terminal_scratch.
128 Now route it to current inferior. */
129 set_inferior_io_terminal (inferior_io_terminal_scratch);
130}
131
132static void
133show_inferior_tty_command (struct ui_file *file, int from_tty,
134 struct cmd_list_element *c, const char *value)
135{
136 /* Note that we ignore the passed-in value in favor of computing it
137 directly. */
138 const char *inferior_io_terminal = get_inferior_io_terminal ();
139
140 if (inferior_io_terminal == NULL)
141 inferior_io_terminal = "";
142 fprintf_filtered (gdb_stdout,
143 _("Terminal for future runs of program being debugged "
144 "is \"%s\".\n"), inferior_io_terminal);
145}
146
147const char *
148get_inferior_args (void)
149{
150 if (current_inferior ()->argc != 0)
151 {
152 gdb::array_view<char * const> args (current_inferior ()->argv,
153 current_inferior ()->argc);
154 std::string n = construct_inferior_arguments (args);
155 set_inferior_args (n.c_str ());
156 }
157
158 if (current_inferior ()->args == NULL)
159 current_inferior ()->args = xstrdup ("");
160
161 return current_inferior ()->args;
162}
163
164/* Set the arguments for the current inferior. Ownership of
165 NEWARGS is not transferred. */
166
167void
168set_inferior_args (const char *newargs)
169{
170 xfree (current_inferior ()->args);
171 current_inferior ()->args = newargs ? xstrdup (newargs) : NULL;
172 current_inferior ()->argc = 0;
173 current_inferior ()->argv = 0;
174}
175
176void
177set_inferior_args_vector (int argc, char **argv)
178{
179 current_inferior ()->argc = argc;
180 current_inferior ()->argv = argv;
181}
182
183/* Notice when `set args' is run. */
184
185static void
186set_args_command (const char *args, int from_tty, struct cmd_list_element *c)
187{
188 /* CLI has assigned the user-provided value to inferior_args_scratch.
189 Now route it to current inferior. */
190 set_inferior_args (inferior_args_scratch);
191}
192
193/* Notice when `show args' is run. */
194
195static void
196show_args_command (struct ui_file *file, int from_tty,
197 struct cmd_list_element *c, const char *value)
198{
199 /* Note that we ignore the passed-in value in favor of computing it
200 directly. */
201 deprecated_show_value_hack (file, from_tty, c, get_inferior_args ());
202}
203
204/* See gdbsupport/common-inferior.h. */
205
206void
207set_inferior_cwd (const char *cwd)
208{
209 struct inferior *inf = current_inferior ();
210
211 gdb_assert (inf != NULL);
212
213 if (cwd == NULL)
214 inf->cwd.reset ();
215 else
216 inf->cwd.reset (xstrdup (cwd));
217}
218
219/* See gdbsupport/common-inferior.h. */
220
221const char *
222get_inferior_cwd ()
223{
224 return current_inferior ()->cwd.get ();
225}
226
227/* Handle the 'set cwd' command. */
228
229static void
230set_cwd_command (const char *args, int from_tty, struct cmd_list_element *c)
231{
232 if (*inferior_cwd_scratch == '\0')
233 set_inferior_cwd (NULL);
234 else
235 set_inferior_cwd (inferior_cwd_scratch);
236}
237
238/* Handle the 'show cwd' command. */
239
240static void
241show_cwd_command (struct ui_file *file, int from_tty,
242 struct cmd_list_element *c, const char *value)
243{
244 const char *cwd = get_inferior_cwd ();
245
246 if (cwd == NULL)
247 fprintf_filtered (gdb_stdout,
248 _("\
249You have not set the inferior's current working directory.\n\
250The inferior will inherit GDB's cwd if native debugging, or the remote\n\
251server's cwd if remote debugging.\n"));
252 else
253 fprintf_filtered (gdb_stdout,
254 _("Current working directory that will be used "
255 "when starting the inferior is \"%s\".\n"), cwd);
256}
257
258
259/* This function strips the '&' character (indicating background
260 execution) that is added as *the last* of the arguments ARGS of a
261 command. A copy of the incoming ARGS without the '&' is returned,
262 unless the resulting string after stripping is empty, in which case
263 NULL is returned. *BG_CHAR_P is an output boolean that indicates
264 whether the '&' character was found. */
265
266static gdb::unique_xmalloc_ptr<char>
267strip_bg_char (const char *args, int *bg_char_p)
268{
269 const char *p;
270
271 if (args == NULL || *args == '\0')
272 {
273 *bg_char_p = 0;
274 return NULL;
275 }
276
277 p = args + strlen (args);
278 if (p[-1] == '&')
279 {
280 p--;
281 while (p > args && isspace (p[-1]))
282 p--;
283
284 *bg_char_p = 1;
285 if (p != args)
286 return gdb::unique_xmalloc_ptr<char>
287 (savestring (args, p - args));
288 else
289 return gdb::unique_xmalloc_ptr<char> (nullptr);
290 }
291
292 *bg_char_p = 0;
293 return make_unique_xstrdup (args);
294}
295
296/* Common actions to take after creating any sort of inferior, by any
297 means (running, attaching, connecting, et cetera). The target
298 should be stopped. */
299
300void
301post_create_inferior (struct target_ops *target, int from_tty)
302{
303
304 /* Be sure we own the terminal in case write operations are performed. */
305 target_terminal::ours_for_output ();
306
307 /* If the target hasn't taken care of this already, do it now.
308 Targets which need to access registers during to_open,
309 to_create_inferior, or to_attach should do it earlier; but many
310 don't need to. */
311 target_find_description ();
312
313 /* Now that we know the register layout, retrieve current PC. But
314 if the PC is unavailable (e.g., we're opening a core file with
315 missing registers info), ignore it. */
316 thread_info *thr = inferior_thread ();
317
318 thr->suspend.stop_pc = 0;
319 try
320 {
321 regcache *rc = get_thread_regcache (thr);
322 thr->suspend.stop_pc = regcache_read_pc (rc);
323 }
324 catch (const gdb_exception_error &ex)
325 {
326 if (ex.error != NOT_AVAILABLE_ERROR)
327 throw;
328 }
329
330 if (exec_bfd)
331 {
332 const unsigned solib_add_generation
333 = current_program_space->solib_add_generation;
334
335 /* Create the hooks to handle shared library load and unload
336 events. */
337 solib_create_inferior_hook (from_tty);
338
339 if (current_program_space->solib_add_generation == solib_add_generation)
340 {
341 /* The platform-specific hook should load initial shared libraries,
342 but didn't. FROM_TTY will be incorrectly 0 but such solib
343 targets should be fixed anyway. Call it only after the solib
344 target has been initialized by solib_create_inferior_hook. */
345
346 if (info_verbose)
347 warning (_("platform-specific solib_create_inferior_hook did "
348 "not load initial shared libraries."));
349
350 /* If the solist is global across processes, there's no need to
351 refetch it here. */
352 if (!gdbarch_has_global_solist (target_gdbarch ()))
353 solib_add (NULL, 0, auto_solib_add);
354 }
355 }
356
357 /* If the user sets watchpoints before execution having started,
358 then she gets software watchpoints, because GDB can't know which
359 target will end up being pushed, or if it supports hardware
360 watchpoints or not. breakpoint_re_set takes care of promoting
361 watchpoints to hardware watchpoints if possible, however, if this
362 new inferior doesn't load shared libraries or we don't pull in
363 symbols from any other source on this target/arch,
364 breakpoint_re_set is never called. Call it now so that software
365 watchpoints get a chance to be promoted to hardware watchpoints
366 if the now pushed target supports hardware watchpoints. */
367 breakpoint_re_set ();
368
369 gdb::observers::inferior_created.notify (target, from_tty);
370}
371
372/* Kill the inferior if already running. This function is designed
373 to be called when we are about to start the execution of the program
374 from the beginning. Ask the user to confirm that he wants to restart
375 the program being debugged when FROM_TTY is non-null. */
376
377static void
378kill_if_already_running (int from_tty)
379{
380 if (inferior_ptid != null_ptid && target_has_execution)
381 {
382 /* Bail out before killing the program if we will not be able to
383 restart it. */
384 target_require_runnable ();
385
386 if (from_tty
387 && !query (_("The program being debugged has been started already.\n\
388Start it from the beginning? ")))
389 error (_("Program not restarted."));
390 target_kill ();
391 }
392}
393
394/* See inferior.h. */
395
396void
397prepare_execution_command (struct target_ops *target, int background)
398{
399 /* If we get a request for running in the bg but the target
400 doesn't support it, error out. */
401 if (background && !target->can_async_p ())
402 error (_("Asynchronous execution not supported on this target."));
403
404 if (!background)
405 {
406 /* If we get a request for running in the fg, then we need to
407 simulate synchronous (fg) execution. Note no cleanup is
408 necessary for this. stdin is re-enabled whenever an error
409 reaches the top level. */
410 all_uis_on_sync_execution_starting ();
411 }
412}
413
414/* Determine how the new inferior will behave. */
415
416enum run_how
417 {
418 /* Run program without any explicit stop during startup. */
419 RUN_NORMAL,
420
421 /* Stop at the beginning of the program's main function. */
422 RUN_STOP_AT_MAIN,
423
424 /* Stop at the first instruction of the program. */
425 RUN_STOP_AT_FIRST_INSN
426 };
427
428/* Implement the "run" command. Force a stop during program start if
429 requested by RUN_HOW. */
430
431static void
432run_command_1 (const char *args, int from_tty, enum run_how run_how)
433{
434 const char *exec_file;
435 struct ui_out *uiout = current_uiout;
436 struct target_ops *run_target;
437 int async_exec;
438
439 dont_repeat ();
440
441 kill_if_already_running (from_tty);
442
443 init_wait_for_inferior ();
444 clear_breakpoint_hit_counts ();
445
446 /* Clean up any leftovers from other runs. Some other things from
447 this function should probably be moved into target_pre_inferior. */
448 target_pre_inferior (from_tty);
449
450 /* The comment here used to read, "The exec file is re-read every
451 time we do a generic_mourn_inferior, so we just have to worry
452 about the symbol file." The `generic_mourn_inferior' function
453 gets called whenever the program exits. However, suppose the
454 program exits, and *then* the executable file changes? We need
455 to check again here. Since reopen_exec_file doesn't do anything
456 if the timestamp hasn't changed, I don't see the harm. */
457 reopen_exec_file ();
458 reread_symbols ();
459
460 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
461 args = stripped.get ();
462
463 /* Do validation and preparation before possibly changing anything
464 in the inferior. */
465
466 run_target = find_run_target ();
467
468 prepare_execution_command (run_target, async_exec);
469
470 if (non_stop && !run_target->supports_non_stop ())
471 error (_("The target does not support running in non-stop mode."));
472
473 /* Done. Can now set breakpoints, change inferior args, etc. */
474
475 /* Insert temporary breakpoint in main function if requested. */
476 if (run_how == RUN_STOP_AT_MAIN)
477 {
478 std::string arg = string_printf ("-qualified %s", main_name ());
479 tbreak_command (arg.c_str (), 0);
480 }
481
482 exec_file = get_exec_file (0);
483
484 /* We keep symbols from add-symbol-file, on the grounds that the
485 user might want to add some symbols before running the program
486 (right?). But sometimes (dynamic loading where the user manually
487 introduces the new symbols with add-symbol-file), the code which
488 the symbols describe does not persist between runs. Currently
489 the user has to manually nuke all symbols between runs if they
490 want them to go away (PR 2207). This is probably reasonable. */
491
492 /* If there were other args, beside '&', process them. */
493 if (args != NULL)
494 set_inferior_args (args);
495
496 if (from_tty)
497 {
498 uiout->field_string (NULL, "Starting program");
499 uiout->text (": ");
500 if (exec_file)
501 uiout->field_string ("execfile", exec_file);
502 uiout->spaces (1);
503 /* We call get_inferior_args() because we might need to compute
504 the value now. */
505 uiout->field_string ("infargs", get_inferior_args ());
506 uiout->text ("\n");
507 uiout->flush ();
508 }
509
510 /* We call get_inferior_args() because we might need to compute
511 the value now. */
512 run_target->create_inferior (exec_file,
513 std::string (get_inferior_args ()),
514 current_inferior ()->environment.envp (),
515 from_tty);
516 /* to_create_inferior should push the target, so after this point we
517 shouldn't refer to run_target again. */
518 run_target = NULL;
519
520 /* We're starting off a new process. When we get out of here, in
521 non-stop mode, finish the state of all threads of that process,
522 but leave other threads alone, as they may be stopped in internal
523 events --- the frontend shouldn't see them as stopped. In
524 all-stop, always finish the state of all threads, as we may be
525 resuming more than just the new process. */
526 process_stratum_target *finish_target;
527 ptid_t finish_ptid;
528 if (non_stop)
529 {
530 finish_target = current_inferior ()->process_target ();
531 finish_ptid = ptid_t (current_inferior ()->pid);
532 }
533 else
534 {
535 finish_target = nullptr;
536 finish_ptid = minus_one_ptid;
537 }
538 scoped_finish_thread_state finish_state (finish_target, finish_ptid);
539
540 /* Pass zero for FROM_TTY, because at this point the "run" command
541 has done its thing; now we are setting up the running program. */
542 post_create_inferior (current_top_target (), 0);
543
544 /* Queue a pending event so that the program stops immediately. */
545 if (run_how == RUN_STOP_AT_FIRST_INSN)
546 {
547 thread_info *thr = inferior_thread ();
548 thr->suspend.waitstatus_pending_p = 1;
549 thr->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED;
550 thr->suspend.waitstatus.value.sig = GDB_SIGNAL_0;
551 }
552
553 /* Start the target running. Do not use -1 continuation as it would skip
554 breakpoint right at the entry point. */
555 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0);
556
557 /* Since there was no error, there's no need to finish the thread
558 states here. */
559 finish_state.release ();
560}
561
562static void
563run_command (const char *args, int from_tty)
564{
565 run_command_1 (args, from_tty, RUN_NORMAL);
566}
567
568/* Start the execution of the program up until the beginning of the main
569 program. */
570
571static void
572start_command (const char *args, int from_tty)
573{
574 /* Some languages such as Ada need to search inside the program
575 minimal symbols for the location where to put the temporary
576 breakpoint before starting. */
577 if (!have_minimal_symbols ())
578 error (_("No symbol table loaded. Use the \"file\" command."));
579
580 /* Run the program until reaching the main procedure... */
581 run_command_1 (args, from_tty, RUN_STOP_AT_MAIN);
582}
583
584/* Start the execution of the program stopping at the first
585 instruction. */
586
587static void
588starti_command (const char *args, int from_tty)
589{
590 run_command_1 (args, from_tty, RUN_STOP_AT_FIRST_INSN);
591}
592
593static int
594proceed_thread_callback (struct thread_info *thread, void *arg)
595{
596 /* We go through all threads individually instead of compressing
597 into a single target `resume_all' request, because some threads
598 may be stopped in internal breakpoints/events, or stopped waiting
599 for its turn in the displaced stepping queue (that is, they are
600 running && !executing). The target side has no idea about why
601 the thread is stopped, so a `resume_all' command would resume too
602 much. If/when GDB gains a way to tell the target `hold this
603 thread stopped until I say otherwise', then we can optimize
604 this. */
605 if (thread->state != THREAD_STOPPED)
606 return 0;
607
608 if (!thread->inf->has_execution ())
609 return 0;
610
611 switch_to_thread (thread);
612 clear_proceed_status (0);
613 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
614 return 0;
615}
616
617static void
618ensure_valid_thread (void)
619{
620 if (inferior_ptid == null_ptid
621 || inferior_thread ()->state == THREAD_EXITED)
622 error (_("Cannot execute this command without a live selected thread."));
623}
624
625/* If the user is looking at trace frames, any resumption of execution
626 is likely to mix up recorded and live target data. So simply
627 disallow those commands. */
628
629static void
630ensure_not_tfind_mode (void)
631{
632 if (get_traceframe_number () >= 0)
633 error (_("Cannot execute this command while looking at trace frames."));
634}
635
636/* Throw an error indicating the current thread is running. */
637
638static void
639error_is_running (void)
640{
641 error (_("Cannot execute this command while "
642 "the selected thread is running."));
643}
644
645/* Calls error_is_running if the current thread is running. */
646
647static void
648ensure_not_running (void)
649{
650 if (inferior_thread ()->state == THREAD_RUNNING)
651 error_is_running ();
652}
653
654void
655continue_1 (int all_threads)
656{
657 ERROR_NO_INFERIOR;
658 ensure_not_tfind_mode ();
659
660 if (non_stop && all_threads)
661 {
662 /* Don't error out if the current thread is running, because
663 there may be other stopped threads. */
664
665 /* Backup current thread and selected frame and restore on scope
666 exit. */
667 scoped_restore_current_thread restore_thread;
668
669 iterate_over_threads (proceed_thread_callback, NULL);
670
671 if (current_ui->prompt_state == PROMPT_BLOCKED)
672 {
673 /* If all threads in the target were already running,
674 proceed_thread_callback ends up never calling proceed,
675 and so nothing calls this to put the inferior's terminal
676 settings in effect and remove stdin from the event loop,
677 which we must when running a foreground command. E.g.:
678
679 (gdb) c -a&
680 Continuing.
681 <all threads are running now>
682 (gdb) c -a
683 Continuing.
684 <no thread was resumed, but the inferior now owns the terminal>
685 */
686 target_terminal::inferior ();
687 }
688 }
689 else
690 {
691 ensure_valid_thread ();
692 ensure_not_running ();
693 clear_proceed_status (0);
694 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
695 }
696}
697
698/* continue [-a] [proceed-count] [&] */
699
700static void
701continue_command (const char *args, int from_tty)
702{
703 int async_exec;
704 bool all_threads_p = false;
705
706 ERROR_NO_INFERIOR;
707
708 /* Find out whether we must run in the background. */
709 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
710 args = stripped.get ();
711
712 if (args != NULL)
713 {
714 if (startswith (args, "-a"))
715 {
716 all_threads_p = true;
717 args += sizeof ("-a") - 1;
718 if (*args == '\0')
719 args = NULL;
720 }
721 }
722
723 if (!non_stop && all_threads_p)
724 error (_("`-a' is meaningless in all-stop mode."));
725
726 if (args != NULL && all_threads_p)
727 error (_("Can't resume all threads and specify "
728 "proceed count simultaneously."));
729
730 /* If we have an argument left, set proceed count of breakpoint we
731 stopped at. */
732 if (args != NULL)
733 {
734 bpstat bs = NULL;
735 int num, stat;
736 int stopped = 0;
737 struct thread_info *tp;
738
739 if (non_stop)
740 tp = inferior_thread ();
741 else
742 {
743 process_stratum_target *last_target;
744 ptid_t last_ptid;
745
746 get_last_target_status (&last_target, &last_ptid, nullptr);
747 tp = find_thread_ptid (last_target, last_ptid);
748 }
749 if (tp != NULL)
750 bs = tp->control.stop_bpstat;
751
752 while ((stat = bpstat_num (&bs, &num)) != 0)
753 if (stat > 0)
754 {
755 set_ignore_count (num,
756 parse_and_eval_long (args) - 1,
757 from_tty);
758 /* set_ignore_count prints a message ending with a period.
759 So print two spaces before "Continuing.". */
760 if (from_tty)
761 printf_filtered (" ");
762 stopped = 1;
763 }
764
765 if (!stopped && from_tty)
766 {
767 printf_filtered
768 ("Not stopped at any breakpoint; argument ignored.\n");
769 }
770 }
771
772 ERROR_NO_INFERIOR;
773 ensure_not_tfind_mode ();
774
775 if (!non_stop || !all_threads_p)
776 {
777 ensure_valid_thread ();
778 ensure_not_running ();
779 }
780
781 prepare_execution_command (current_top_target (), async_exec);
782
783 if (from_tty)
784 printf_filtered (_("Continuing.\n"));
785
786 continue_1 (all_threads_p);
787}
788\f
789/* Record in TP the starting point of a "step" or "next" command. */
790
791static void
792set_step_frame (thread_info *tp)
793{
794 /* This can be removed once this function no longer implicitly relies on the
795 inferior_ptid value. */
796 gdb_assert (inferior_ptid == tp->ptid);
797
798 frame_info *frame = get_current_frame ();
799
800 symtab_and_line sal = find_frame_sal (frame);
801 set_step_info (tp, frame, sal);
802
803 CORE_ADDR pc = get_frame_pc (frame);
804 tp->control.step_start_function = find_pc_function (pc);
805}
806
807/* Step until outside of current statement. */
808
809static void
810step_command (const char *count_string, int from_tty)
811{
812 step_1 (0, 0, count_string);
813}
814
815/* Likewise, but skip over subroutine calls as if single instructions. */
816
817static void
818next_command (const char *count_string, int from_tty)
819{
820 step_1 (1, 0, count_string);
821}
822
823/* Likewise, but step only one instruction. */
824
825static void
826stepi_command (const char *count_string, int from_tty)
827{
828 step_1 (0, 1, count_string);
829}
830
831static void
832nexti_command (const char *count_string, int from_tty)
833{
834 step_1 (1, 1, count_string);
835}
836
837/* Data for the FSM that manages the step/next/stepi/nexti
838 commands. */
839
840struct step_command_fsm : public thread_fsm
841{
842 /* How many steps left in a "step N"-like command. */
843 int count;
844
845 /* If true, this is a next/nexti, otherwise a step/stepi. */
846 int skip_subroutines;
847
848 /* If true, this is a stepi/nexti, otherwise a step/step. */
849 int single_inst;
850
851 explicit step_command_fsm (struct interp *cmd_interp)
852 : thread_fsm (cmd_interp)
853 {
854 }
855
856 void clean_up (struct thread_info *thread) override;
857 bool should_stop (struct thread_info *thread) override;
858 enum async_reply_reason do_async_reply_reason () override;
859};
860
861/* Prepare for a step/next/etc. command. Any target resource
862 allocated here is undone in the FSM's clean_up method. */
863
864static void
865step_command_fsm_prepare (struct step_command_fsm *sm,
866 int skip_subroutines, int single_inst,
867 int count, struct thread_info *thread)
868{
869 sm->skip_subroutines = skip_subroutines;
870 sm->single_inst = single_inst;
871 sm->count = count;
872
873 /* Leave the si command alone. */
874 if (!sm->single_inst || sm->skip_subroutines)
875 set_longjmp_breakpoint (thread, get_frame_id (get_current_frame ()));
876
877 thread->control.stepping_command = 1;
878}
879
880static int prepare_one_step (thread_info *, struct step_command_fsm *sm);
881
882static void
883step_1 (int skip_subroutines, int single_inst, const char *count_string)
884{
885 int count;
886 int async_exec;
887 struct thread_info *thr;
888 struct step_command_fsm *step_sm;
889
890 ERROR_NO_INFERIOR;
891 ensure_not_tfind_mode ();
892 ensure_valid_thread ();
893 ensure_not_running ();
894
895 gdb::unique_xmalloc_ptr<char> stripped
896 = strip_bg_char (count_string, &async_exec);
897 count_string = stripped.get ();
898
899 prepare_execution_command (current_top_target (), async_exec);
900
901 count = count_string ? parse_and_eval_long (count_string) : 1;
902
903 clear_proceed_status (1);
904
905 /* Setup the execution command state machine to handle all the COUNT
906 steps. */
907 thr = inferior_thread ();
908 step_sm = new step_command_fsm (command_interp ());
909 thr->thread_fsm = step_sm;
910
911 step_command_fsm_prepare (step_sm, skip_subroutines,
912 single_inst, count, thr);
913
914 /* Do only one step for now, before returning control to the event
915 loop. Let the continuation figure out how many other steps we
916 need to do, and handle them one at the time, through
917 step_once. */
918 if (!prepare_one_step (thr, step_sm))
919 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
920 else
921 {
922 int proceeded;
923
924 /* Stepped into an inline frame. Pretend that we've
925 stopped. */
926 thr->thread_fsm->clean_up (thr);
927 proceeded = normal_stop ();
928 if (!proceeded)
929 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
930 all_uis_check_sync_execution_done ();
931 }
932}
933
934/* Implementation of the 'should_stop' FSM method for stepping
935 commands. Called after we are done with one step operation, to
936 check whether we need to step again, before we print the prompt and
937 return control to the user. If count is > 1, returns false, as we
938 will need to keep going. */
939
940bool
941step_command_fsm::should_stop (struct thread_info *tp)
942{
943 if (tp->control.stop_step)
944 {
945 /* There are more steps to make, and we did stop due to
946 ending a stepping range. Do another step. */
947 if (--count > 0)
948 return prepare_one_step (tp, this);
949
950 set_finished ();
951 }
952
953 return true;
954}
955
956/* Implementation of the 'clean_up' FSM method for stepping commands. */
957
958void
959step_command_fsm::clean_up (struct thread_info *thread)
960{
961 if (!single_inst || skip_subroutines)
962 delete_longjmp_breakpoint (thread->global_num);
963}
964
965/* Implementation of the 'async_reply_reason' FSM method for stepping
966 commands. */
967
968enum async_reply_reason
969step_command_fsm::do_async_reply_reason ()
970{
971 return EXEC_ASYNC_END_STEPPING_RANGE;
972}
973
974/* Prepare for one step in "step N". The actual target resumption is
975 done by the caller. Return true if we're done and should thus
976 report a stop to the user. Returns false if the target needs to be
977 resumed. */
978
979static int
980prepare_one_step (thread_info *tp, struct step_command_fsm *sm)
981{
982 /* This can be removed once this function no longer implicitly relies on the
983 inferior_ptid value. */
984 gdb_assert (inferior_ptid == tp->ptid);
985
986 if (sm->count > 0)
987 {
988 struct frame_info *frame = get_current_frame ();
989
990 set_step_frame (tp);
991
992 if (!sm->single_inst)
993 {
994 CORE_ADDR pc;
995
996 /* Step at an inlined function behaves like "down". */
997 if (!sm->skip_subroutines
998 && inline_skipped_frames (tp))
999 {
1000 ptid_t resume_ptid;
1001 const char *fn = NULL;
1002 symtab_and_line sal;
1003 struct symbol *sym;
1004
1005 /* Pretend that we've ran. */
1006 resume_ptid = user_visible_resume_ptid (1);
1007 set_running (tp->inf->process_target (), resume_ptid, true);
1008
1009 step_into_inline_frame (tp);
1010
1011 frame = get_current_frame ();
1012 sal = find_frame_sal (frame);
1013 sym = get_frame_function (frame);
1014
1015 if (sym != NULL)
1016 fn = sym->print_name ();
1017
1018 if (sal.line == 0
1019 || !function_name_is_marked_for_skip (fn, sal))
1020 {
1021 sm->count--;
1022 return prepare_one_step (tp, sm);
1023 }
1024 }
1025
1026 pc = get_frame_pc (frame);
1027 find_pc_line_pc_range (pc,
1028 &tp->control.step_range_start,
1029 &tp->control.step_range_end);
1030
1031 tp->control.may_range_step = 1;
1032
1033 /* If we have no line info, switch to stepi mode. */
1034 if (tp->control.step_range_end == 0 && step_stop_if_no_debug)
1035 {
1036 tp->control.step_range_start = tp->control.step_range_end = 1;
1037 tp->control.may_range_step = 0;
1038 }
1039 else if (tp->control.step_range_end == 0)
1040 {
1041 const char *name;
1042
1043 if (find_pc_partial_function (pc, &name,
1044 &tp->control.step_range_start,
1045 &tp->control.step_range_end) == 0)
1046 error (_("Cannot find bounds of current function"));
1047
1048 target_terminal::ours_for_output ();
1049 printf_filtered (_("Single stepping until exit from function %s,"
1050 "\nwhich has no line number information.\n"),
1051 name);
1052 }
1053 }
1054 else
1055 {
1056 /* Say we are stepping, but stop after one insn whatever it does. */
1057 tp->control.step_range_start = tp->control.step_range_end = 1;
1058 if (!sm->skip_subroutines)
1059 /* It is stepi.
1060 Don't step over function calls, not even to functions lacking
1061 line numbers. */
1062 tp->control.step_over_calls = STEP_OVER_NONE;
1063 }
1064
1065 if (sm->skip_subroutines)
1066 tp->control.step_over_calls = STEP_OVER_ALL;
1067
1068 return 0;
1069 }
1070
1071 /* Done. */
1072 sm->set_finished ();
1073 return 1;
1074}
1075
1076\f
1077/* Continue program at specified address. */
1078
1079static void
1080jump_command (const char *arg, int from_tty)
1081{
1082 struct gdbarch *gdbarch = get_current_arch ();
1083 CORE_ADDR addr;
1084 struct symbol *fn;
1085 struct symbol *sfn;
1086 int async_exec;
1087
1088 ERROR_NO_INFERIOR;
1089 ensure_not_tfind_mode ();
1090 ensure_valid_thread ();
1091 ensure_not_running ();
1092
1093 /* Find out whether we must run in the background. */
1094 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1095 arg = stripped.get ();
1096
1097 prepare_execution_command (current_top_target (), async_exec);
1098
1099 if (!arg)
1100 error_no_arg (_("starting address"));
1101
1102 std::vector<symtab_and_line> sals
1103 = decode_line_with_last_displayed (arg, DECODE_LINE_FUNFIRSTLINE);
1104 if (sals.size () != 1)
1105 error (_("Unreasonable jump request"));
1106
1107 symtab_and_line &sal = sals[0];
1108
1109 if (sal.symtab == 0 && sal.pc == 0)
1110 error (_("No source file has been specified."));
1111
1112 resolve_sal_pc (&sal); /* May error out. */
1113
1114 /* See if we are trying to jump to another function. */
1115 fn = get_frame_function (get_current_frame ());
1116 sfn = find_pc_function (sal.pc);
1117 if (fn != NULL && sfn != fn)
1118 {
1119 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal.line,
1120 fn->print_name ()))
1121 {
1122 error (_("Not confirmed."));
1123 /* NOTREACHED */
1124 }
1125 }
1126
1127 if (sfn != NULL)
1128 {
1129 struct obj_section *section;
1130
1131 fixup_symbol_section (sfn, 0);
1132 section = SYMBOL_OBJ_SECTION (symbol_objfile (sfn), sfn);
1133 if (section_is_overlay (section)
1134 && !section_is_mapped (section))
1135 {
1136 if (!query (_("WARNING!!! Destination is in "
1137 "unmapped overlay! Jump anyway? ")))
1138 {
1139 error (_("Not confirmed."));
1140 /* NOTREACHED */
1141 }
1142 }
1143 }
1144
1145 addr = sal.pc;
1146
1147 if (from_tty)
1148 {
1149 printf_filtered (_("Continuing at "));
1150 fputs_filtered (paddress (gdbarch, addr), gdb_stdout);
1151 printf_filtered (".\n");
1152 }
1153
1154 clear_proceed_status (0);
1155 proceed (addr, GDB_SIGNAL_0);
1156}
1157\f
1158/* Continue program giving it specified signal. */
1159
1160static void
1161signal_command (const char *signum_exp, int from_tty)
1162{
1163 enum gdb_signal oursig;
1164 int async_exec;
1165
1166 dont_repeat (); /* Too dangerous. */
1167 ERROR_NO_INFERIOR;
1168 ensure_not_tfind_mode ();
1169 ensure_valid_thread ();
1170 ensure_not_running ();
1171
1172 /* Find out whether we must run in the background. */
1173 gdb::unique_xmalloc_ptr<char> stripped
1174 = strip_bg_char (signum_exp, &async_exec);
1175 signum_exp = stripped.get ();
1176
1177 prepare_execution_command (current_top_target (), async_exec);
1178
1179 if (!signum_exp)
1180 error_no_arg (_("signal number"));
1181
1182 /* It would be even slicker to make signal names be valid expressions,
1183 (the type could be "enum $signal" or some such), then the user could
1184 assign them to convenience variables. */
1185 oursig = gdb_signal_from_name (signum_exp);
1186
1187 if (oursig == GDB_SIGNAL_UNKNOWN)
1188 {
1189 /* No, try numeric. */
1190 int num = parse_and_eval_long (signum_exp);
1191
1192 if (num == 0)
1193 oursig = GDB_SIGNAL_0;
1194 else
1195 oursig = gdb_signal_from_command (num);
1196 }
1197
1198 /* Look for threads other than the current that this command ends up
1199 resuming too (due to schedlock off), and warn if they'll get a
1200 signal delivered. "signal 0" is used to suppress a previous
1201 signal, but if the current thread is no longer the one that got
1202 the signal, then the user is potentially suppressing the signal
1203 of the wrong thread. */
1204 if (!non_stop)
1205 {
1206 int must_confirm = 0;
1207
1208 /* This indicates what will be resumed. Either a single thread,
1209 a whole process, or all threads of all processes. */
1210 ptid_t resume_ptid = user_visible_resume_ptid (0);
1211 process_stratum_target *resume_target
1212 = user_visible_resume_target (resume_ptid);
1213
1214 thread_info *current = inferior_thread ();
1215
1216 for (thread_info *tp : all_non_exited_threads (resume_target, resume_ptid))
1217 {
1218 if (tp == current)
1219 continue;
1220
1221 if (tp->suspend.stop_signal != GDB_SIGNAL_0
1222 && signal_pass_state (tp->suspend.stop_signal))
1223 {
1224 if (!must_confirm)
1225 printf_unfiltered (_("Note:\n"));
1226 printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"),
1227 print_thread_id (tp),
1228 gdb_signal_to_name (tp->suspend.stop_signal),
1229 gdb_signal_to_string (tp->suspend.stop_signal));
1230 must_confirm = 1;
1231 }
1232 }
1233
1234 if (must_confirm
1235 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1236 "still deliver the signals noted above to their respective threads.\n"
1237 "Continue anyway? "),
1238 print_thread_id (inferior_thread ())))
1239 error (_("Not confirmed."));
1240 }
1241
1242 if (from_tty)
1243 {
1244 if (oursig == GDB_SIGNAL_0)
1245 printf_filtered (_("Continuing with no signal.\n"));
1246 else
1247 printf_filtered (_("Continuing with signal %s.\n"),
1248 gdb_signal_to_name (oursig));
1249 }
1250
1251 clear_proceed_status (0);
1252 proceed ((CORE_ADDR) -1, oursig);
1253}
1254
1255/* Queue a signal to be delivered to the current thread. */
1256
1257static void
1258queue_signal_command (const char *signum_exp, int from_tty)
1259{
1260 enum gdb_signal oursig;
1261 struct thread_info *tp;
1262
1263 ERROR_NO_INFERIOR;
1264 ensure_not_tfind_mode ();
1265 ensure_valid_thread ();
1266 ensure_not_running ();
1267
1268 if (signum_exp == NULL)
1269 error_no_arg (_("signal number"));
1270
1271 /* It would be even slicker to make signal names be valid expressions,
1272 (the type could be "enum $signal" or some such), then the user could
1273 assign them to convenience variables. */
1274 oursig = gdb_signal_from_name (signum_exp);
1275
1276 if (oursig == GDB_SIGNAL_UNKNOWN)
1277 {
1278 /* No, try numeric. */
1279 int num = parse_and_eval_long (signum_exp);
1280
1281 if (num == 0)
1282 oursig = GDB_SIGNAL_0;
1283 else
1284 oursig = gdb_signal_from_command (num);
1285 }
1286
1287 if (oursig != GDB_SIGNAL_0
1288 && !signal_pass_state (oursig))
1289 error (_("Signal handling set to not pass this signal to the program."));
1290
1291 tp = inferior_thread ();
1292 tp->suspend.stop_signal = oursig;
1293}
1294
1295/* Data for the FSM that manages the until (with no argument)
1296 command. */
1297
1298struct until_next_fsm : public thread_fsm
1299{
1300 /* The thread that as current when the command was executed. */
1301 int thread;
1302
1303 until_next_fsm (struct interp *cmd_interp, int thread)
1304 : thread_fsm (cmd_interp),
1305 thread (thread)
1306 {
1307 }
1308
1309 bool should_stop (struct thread_info *thread) override;
1310 void clean_up (struct thread_info *thread) override;
1311 enum async_reply_reason do_async_reply_reason () override;
1312};
1313
1314/* Implementation of the 'should_stop' FSM method for the until (with
1315 no arg) command. */
1316
1317bool
1318until_next_fsm::should_stop (struct thread_info *tp)
1319{
1320 if (tp->control.stop_step)
1321 set_finished ();
1322
1323 return true;
1324}
1325
1326/* Implementation of the 'clean_up' FSM method for the until (with no
1327 arg) command. */
1328
1329void
1330until_next_fsm::clean_up (struct thread_info *thread)
1331{
1332 delete_longjmp_breakpoint (thread->global_num);
1333}
1334
1335/* Implementation of the 'async_reply_reason' FSM method for the until
1336 (with no arg) command. */
1337
1338enum async_reply_reason
1339until_next_fsm::do_async_reply_reason ()
1340{
1341 return EXEC_ASYNC_END_STEPPING_RANGE;
1342}
1343
1344/* Proceed until we reach a different source line with pc greater than
1345 our current one or exit the function. We skip calls in both cases.
1346
1347 Note that eventually this command should probably be changed so
1348 that only source lines are printed out when we hit the breakpoint
1349 we set. This may involve changes to wait_for_inferior and the
1350 proceed status code. */
1351
1352static void
1353until_next_command (int from_tty)
1354{
1355 struct frame_info *frame;
1356 CORE_ADDR pc;
1357 struct symbol *func;
1358 struct symtab_and_line sal;
1359 struct thread_info *tp = inferior_thread ();
1360 int thread = tp->global_num;
1361 struct until_next_fsm *sm;
1362
1363 clear_proceed_status (0);
1364 set_step_frame (tp);
1365
1366 frame = get_current_frame ();
1367
1368 /* Step until either exited from this function or greater
1369 than the current line (if in symbolic section) or pc (if
1370 not). */
1371
1372 pc = get_frame_pc (frame);
1373 func = find_pc_function (pc);
1374
1375 if (!func)
1376 {
1377 struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc);
1378
1379 if (msymbol.minsym == NULL)
1380 error (_("Execution is not within a known function."));
1381
1382 tp->control.step_range_start = BMSYMBOL_VALUE_ADDRESS (msymbol);
1383 /* The upper-bound of step_range is exclusive. In order to make PC
1384 within the range, set the step_range_end with PC + 1. */
1385 tp->control.step_range_end = pc + 1;
1386 }
1387 else
1388 {
1389 sal = find_pc_line (pc, 0);
1390
1391 tp->control.step_range_start = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func));
1392 tp->control.step_range_end = sal.end;
1393 }
1394 tp->control.may_range_step = 1;
1395
1396 tp->control.step_over_calls = STEP_OVER_ALL;
1397
1398 set_longjmp_breakpoint (tp, get_frame_id (frame));
1399 delete_longjmp_breakpoint_cleanup lj_deleter (thread);
1400
1401 sm = new until_next_fsm (command_interp (), tp->global_num);
1402 tp->thread_fsm = sm;
1403 lj_deleter.release ();
1404
1405 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1406}
1407
1408static void
1409until_command (const char *arg, int from_tty)
1410{
1411 int async_exec;
1412
1413 ERROR_NO_INFERIOR;
1414 ensure_not_tfind_mode ();
1415 ensure_valid_thread ();
1416 ensure_not_running ();
1417
1418 /* Find out whether we must run in the background. */
1419 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1420 arg = stripped.get ();
1421
1422 prepare_execution_command (current_top_target (), async_exec);
1423
1424 if (arg)
1425 until_break_command (arg, from_tty, 0);
1426 else
1427 until_next_command (from_tty);
1428}
1429
1430static void
1431advance_command (const char *arg, int from_tty)
1432{
1433 int async_exec;
1434
1435 ERROR_NO_INFERIOR;
1436 ensure_not_tfind_mode ();
1437 ensure_valid_thread ();
1438 ensure_not_running ();
1439
1440 if (arg == NULL)
1441 error_no_arg (_("a location"));
1442
1443 /* Find out whether we must run in the background. */
1444 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1445 arg = stripped.get ();
1446
1447 prepare_execution_command (current_top_target (), async_exec);
1448
1449 until_break_command (arg, from_tty, 1);
1450}
1451\f
1452/* Return the value of the result of a function at the end of a 'finish'
1453 command/BP. DTOR_DATA (if not NULL) can represent inferior registers
1454 right after an inferior call has finished. */
1455
1456struct value *
1457get_return_value (struct value *function, struct type *value_type)
1458{
1459 regcache *stop_regs = get_current_regcache ();
1460 struct gdbarch *gdbarch = stop_regs->arch ();
1461 struct value *value;
1462
1463 value_type = check_typedef (value_type);
1464 gdb_assert (value_type->code () != TYPE_CODE_VOID);
1465
1466 /* FIXME: 2003-09-27: When returning from a nested inferior function
1467 call, it's possible (with no help from the architecture vector)
1468 to locate and return/print a "struct return" value. This is just
1469 a more complicated case of what is already being done in the
1470 inferior function call code. In fact, when inferior function
1471 calls are made async, this will likely be made the norm. */
1472
1473 switch (gdbarch_return_value (gdbarch, function, value_type,
1474 NULL, NULL, NULL))
1475 {
1476 case RETURN_VALUE_REGISTER_CONVENTION:
1477 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
1478 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
1479 value = allocate_value (value_type);
1480 gdbarch_return_value (gdbarch, function, value_type, stop_regs,
1481 value_contents_raw (value), NULL);
1482 break;
1483 case RETURN_VALUE_STRUCT_CONVENTION:
1484 value = NULL;
1485 break;
1486 default:
1487 internal_error (__FILE__, __LINE__, _("bad switch"));
1488 }
1489
1490 return value;
1491}
1492
1493/* The captured function return value/type and its position in the
1494 value history. */
1495
1496struct return_value_info
1497{
1498 /* The captured return value. May be NULL if we weren't able to
1499 retrieve it. See get_return_value. */
1500 struct value *value;
1501
1502 /* The return type. In some cases, we'll not be able extract the
1503 return value, but we always know the type. */
1504 struct type *type;
1505
1506 /* If we captured a value, this is the value history index. */
1507 int value_history_index;
1508};
1509
1510/* Helper for print_return_value. */
1511
1512static void
1513print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv)
1514{
1515 if (rv->value != NULL)
1516 {
1517 struct value_print_options opts;
1518
1519 /* Print it. */
1520 uiout->text ("Value returned is ");
1521 uiout->field_fmt ("gdb-result-var", "$%d",
1522 rv->value_history_index);
1523 uiout->text (" = ");
1524 get_user_print_options (&opts);
1525
1526 if (opts.finish_print)
1527 {
1528 string_file stb;
1529 value_print (rv->value, &stb, &opts);
1530 uiout->field_stream ("return-value", stb);
1531 }
1532 else
1533 uiout->field_string ("return-value", _("<not displayed>"),
1534 metadata_style.style ());
1535 uiout->text ("\n");
1536 }
1537 else
1538 {
1539 std::string type_name = type_to_string (rv->type);
1540 uiout->text ("Value returned has type: ");
1541 uiout->field_string ("return-type", type_name.c_str ());
1542 uiout->text (".");
1543 uiout->text (" Cannot determine contents\n");
1544 }
1545}
1546
1547/* Print the result of a function at the end of a 'finish' command.
1548 RV points at an object representing the captured return value/type
1549 and its position in the value history. */
1550
1551void
1552print_return_value (struct ui_out *uiout, struct return_value_info *rv)
1553{
1554 if (rv->type == NULL
1555 || check_typedef (rv->type)->code () == TYPE_CODE_VOID)
1556 return;
1557
1558 try
1559 {
1560 /* print_return_value_1 can throw an exception in some
1561 circumstances. We need to catch this so that we still
1562 delete the breakpoint. */
1563 print_return_value_1 (uiout, rv);
1564 }
1565 catch (const gdb_exception &ex)
1566 {
1567 exception_print (gdb_stdout, ex);
1568 }
1569}
1570
1571/* Data for the FSM that manages the finish command. */
1572
1573struct finish_command_fsm : public thread_fsm
1574{
1575 /* The momentary breakpoint set at the function's return address in
1576 the caller. */
1577 breakpoint_up breakpoint;
1578
1579 /* The function that we're stepping out of. */
1580 struct symbol *function = nullptr;
1581
1582 /* If the FSM finishes successfully, this stores the function's
1583 return value. */
1584 struct return_value_info return_value_info {};
1585
1586 explicit finish_command_fsm (struct interp *cmd_interp)
1587 : thread_fsm (cmd_interp)
1588 {
1589 }
1590
1591 bool should_stop (struct thread_info *thread) override;
1592 void clean_up (struct thread_info *thread) override;
1593 struct return_value_info *return_value () override;
1594 enum async_reply_reason do_async_reply_reason () override;
1595};
1596
1597/* Implementation of the 'should_stop' FSM method for the finish
1598 commands. Detects whether the thread stepped out of the function
1599 successfully, and if so, captures the function's return value and
1600 marks the FSM finished. */
1601
1602bool
1603finish_command_fsm::should_stop (struct thread_info *tp)
1604{
1605 struct return_value_info *rv = &return_value_info;
1606
1607 if (function != NULL
1608 && bpstat_find_breakpoint (tp->control.stop_bpstat,
1609 breakpoint.get ()) != NULL)
1610 {
1611 /* We're done. */
1612 set_finished ();
1613
1614 rv->type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
1615 if (rv->type == NULL)
1616 internal_error (__FILE__, __LINE__,
1617 _("finish_command: function has no target type"));
1618
1619 if (check_typedef (rv->type)->code () != TYPE_CODE_VOID)
1620 {
1621 struct value *func;
1622
1623 func = read_var_value (function, NULL, get_current_frame ());
1624 rv->value = get_return_value (func, rv->type);
1625 if (rv->value != NULL)
1626 rv->value_history_index = record_latest_value (rv->value);
1627 }
1628 }
1629 else if (tp->control.stop_step)
1630 {
1631 /* Finishing from an inline frame, or reverse finishing. In
1632 either case, there's no way to retrieve the return value. */
1633 set_finished ();
1634 }
1635
1636 return true;
1637}
1638
1639/* Implementation of the 'clean_up' FSM method for the finish
1640 commands. */
1641
1642void
1643finish_command_fsm::clean_up (struct thread_info *thread)
1644{
1645 breakpoint.reset ();
1646 delete_longjmp_breakpoint (thread->global_num);
1647}
1648
1649/* Implementation of the 'return_value' FSM method for the finish
1650 commands. */
1651
1652struct return_value_info *
1653finish_command_fsm::return_value ()
1654{
1655 return &return_value_info;
1656}
1657
1658/* Implementation of the 'async_reply_reason' FSM method for the
1659 finish commands. */
1660
1661enum async_reply_reason
1662finish_command_fsm::do_async_reply_reason ()
1663{
1664 if (execution_direction == EXEC_REVERSE)
1665 return EXEC_ASYNC_END_STEPPING_RANGE;
1666 else
1667 return EXEC_ASYNC_FUNCTION_FINISHED;
1668}
1669
1670/* finish_backward -- helper function for finish_command. */
1671
1672static void
1673finish_backward (struct finish_command_fsm *sm)
1674{
1675 struct symtab_and_line sal;
1676 struct thread_info *tp = inferior_thread ();
1677 CORE_ADDR pc;
1678 CORE_ADDR func_addr;
1679
1680 pc = get_frame_pc (get_current_frame ());
1681
1682 if (find_pc_partial_function (pc, NULL, &func_addr, NULL) == 0)
1683 error (_("Cannot find bounds of current function"));
1684
1685 sal = find_pc_line (func_addr, 0);
1686
1687 tp->control.proceed_to_finish = 1;
1688 /* Special case: if we're sitting at the function entry point,
1689 then all we need to do is take a reverse singlestep. We
1690 don't need to set a breakpoint, and indeed it would do us
1691 no good to do so.
1692
1693 Note that this can only happen at frame #0, since there's
1694 no way that a function up the stack can have a return address
1695 that's equal to its entry point. */
1696
1697 if (sal.pc != pc)
1698 {
1699 struct frame_info *frame = get_selected_frame (NULL);
1700 struct gdbarch *gdbarch = get_frame_arch (frame);
1701
1702 /* Set a step-resume at the function's entry point. Once that's
1703 hit, we'll do one more step backwards. */
1704 symtab_and_line sr_sal;
1705 sr_sal.pc = sal.pc;
1706 sr_sal.pspace = get_frame_program_space (frame);
1707 insert_step_resume_breakpoint_at_sal (gdbarch,
1708 sr_sal, null_frame_id);
1709
1710 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1711 }
1712 else
1713 {
1714 /* We're almost there -- we just need to back up by one more
1715 single-step. */
1716 tp->control.step_range_start = tp->control.step_range_end = 1;
1717 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1718 }
1719}
1720
1721/* finish_forward -- helper function for finish_command. FRAME is the
1722 frame that called the function we're about to step out of. */
1723
1724static void
1725finish_forward (struct finish_command_fsm *sm, struct frame_info *frame)
1726{
1727 struct frame_id frame_id = get_frame_id (frame);
1728 struct gdbarch *gdbarch = get_frame_arch (frame);
1729 struct symtab_and_line sal;
1730 struct thread_info *tp = inferior_thread ();
1731
1732 sal = find_pc_line (get_frame_pc (frame), 0);
1733 sal.pc = get_frame_pc (frame);
1734
1735 sm->breakpoint = set_momentary_breakpoint (gdbarch, sal,
1736 get_stack_frame_id (frame),
1737 bp_finish);
1738
1739 /* set_momentary_breakpoint invalidates FRAME. */
1740 frame = NULL;
1741
1742 set_longjmp_breakpoint (tp, frame_id);
1743
1744 /* We want to print return value, please... */
1745 tp->control.proceed_to_finish = 1;
1746
1747 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1748}
1749
1750/* Skip frames for "finish". */
1751
1752static struct frame_info *
1753skip_finish_frames (struct frame_info *frame)
1754{
1755 struct frame_info *start;
1756
1757 do
1758 {
1759 start = frame;
1760
1761 frame = skip_tailcall_frames (frame);
1762 if (frame == NULL)
1763 break;
1764
1765 frame = skip_unwritable_frames (frame);
1766 if (frame == NULL)
1767 break;
1768 }
1769 while (start != frame);
1770
1771 return frame;
1772}
1773
1774/* "finish": Set a temporary breakpoint at the place the selected
1775 frame will return to, then continue. */
1776
1777static void
1778finish_command (const char *arg, int from_tty)
1779{
1780 struct frame_info *frame;
1781 int async_exec;
1782 struct finish_command_fsm *sm;
1783 struct thread_info *tp;
1784
1785 ERROR_NO_INFERIOR;
1786 ensure_not_tfind_mode ();
1787 ensure_valid_thread ();
1788 ensure_not_running ();
1789
1790 /* Find out whether we must run in the background. */
1791 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1792 arg = stripped.get ();
1793
1794 prepare_execution_command (current_top_target (), async_exec);
1795
1796 if (arg)
1797 error (_("The \"finish\" command does not take any arguments."));
1798
1799 frame = get_prev_frame (get_selected_frame (_("No selected frame.")));
1800 if (frame == 0)
1801 error (_("\"finish\" not meaningful in the outermost frame."));
1802
1803 clear_proceed_status (0);
1804
1805 tp = inferior_thread ();
1806
1807 sm = new finish_command_fsm (command_interp ());
1808
1809 tp->thread_fsm = sm;
1810
1811 /* Finishing from an inline frame is completely different. We don't
1812 try to show the "return value" - no way to locate it. */
1813 if (get_frame_type (get_selected_frame (_("No selected frame.")))
1814 == INLINE_FRAME)
1815 {
1816 /* Claim we are stepping in the calling frame. An empty step
1817 range means that we will stop once we aren't in a function
1818 called by that frame. We don't use the magic "1" value for
1819 step_range_end, because then infrun will think this is nexti,
1820 and not step over the rest of this inlined function call. */
1821 set_step_info (tp, frame, {});
1822 tp->control.step_range_start = get_frame_pc (frame);
1823 tp->control.step_range_end = tp->control.step_range_start;
1824 tp->control.step_over_calls = STEP_OVER_ALL;
1825
1826 /* Print info on the selected frame, including level number but not
1827 source. */
1828 if (from_tty)
1829 {
1830 printf_filtered (_("Run till exit from "));
1831 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0);
1832 }
1833
1834 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1835 return;
1836 }
1837
1838 /* Find the function we will return from. */
1839
1840 sm->function = find_pc_function (get_frame_pc (get_selected_frame (NULL)));
1841
1842 /* Print info on the selected frame, including level number but not
1843 source. */
1844 if (from_tty)
1845 {
1846 if (execution_direction == EXEC_REVERSE)
1847 printf_filtered (_("Run back to call of "));
1848 else
1849 {
1850 if (sm->function != NULL && TYPE_NO_RETURN (sm->function->type)
1851 && !query (_("warning: Function %s does not return normally.\n"
1852 "Try to finish anyway? "),
1853 sm->function->print_name ()))
1854 error (_("Not confirmed."));
1855 printf_filtered (_("Run till exit from "));
1856 }
1857
1858 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0);
1859 }
1860
1861 if (execution_direction == EXEC_REVERSE)
1862 finish_backward (sm);
1863 else
1864 {
1865 frame = skip_finish_frames (frame);
1866
1867 if (frame == NULL)
1868 error (_("Cannot find the caller frame."));
1869
1870 finish_forward (sm, frame);
1871 }
1872}
1873\f
1874
1875static void
1876info_program_command (const char *args, int from_tty)
1877{
1878 bpstat bs;
1879 int num, stat;
1880 ptid_t ptid;
1881 process_stratum_target *proc_target;
1882
1883 if (!target_has_execution)
1884 {
1885 printf_filtered (_("The program being debugged is not being run.\n"));
1886 return;
1887 }
1888
1889 if (non_stop)
1890 {
1891 ptid = inferior_ptid;
1892 proc_target = current_inferior ()->process_target ();
1893 }
1894 else
1895 get_last_target_status (&proc_target, &ptid, nullptr);
1896
1897 if (ptid == null_ptid || ptid == minus_one_ptid)
1898 error (_("No selected thread."));
1899
1900 thread_info *tp = find_thread_ptid (proc_target, ptid);
1901
1902 if (tp->state == THREAD_EXITED)
1903 error (_("Invalid selected thread."));
1904 else if (tp->state == THREAD_RUNNING)
1905 error (_("Selected thread is running."));
1906
1907 bs = tp->control.stop_bpstat;
1908 stat = bpstat_num (&bs, &num);
1909
1910 target_files_info ();
1911 printf_filtered (_("Program stopped at %s.\n"),
1912 paddress (target_gdbarch (), tp->suspend.stop_pc));
1913 if (tp->control.stop_step)
1914 printf_filtered (_("It stopped after being stepped.\n"));
1915 else if (stat != 0)
1916 {
1917 /* There may be several breakpoints in the same place, so this
1918 isn't as strange as it seems. */
1919 while (stat != 0)
1920 {
1921 if (stat < 0)
1922 {
1923 printf_filtered (_("It stopped at a breakpoint "
1924 "that has since been deleted.\n"));
1925 }
1926 else
1927 printf_filtered (_("It stopped at breakpoint %d.\n"), num);
1928 stat = bpstat_num (&bs, &num);
1929 }
1930 }
1931 else if (tp->suspend.stop_signal != GDB_SIGNAL_0)
1932 {
1933 printf_filtered (_("It stopped with signal %s, %s.\n"),
1934 gdb_signal_to_name (tp->suspend.stop_signal),
1935 gdb_signal_to_string (tp->suspend.stop_signal));
1936 }
1937
1938 if (from_tty)
1939 {
1940 printf_filtered (_("Type \"info stack\" or \"info "
1941 "registers\" for more information.\n"));
1942 }
1943}
1944\f
1945static void
1946environment_info (const char *var, int from_tty)
1947{
1948 if (var)
1949 {
1950 const char *val = current_inferior ()->environment.get (var);
1951
1952 if (val)
1953 {
1954 puts_filtered (var);
1955 puts_filtered (" = ");
1956 puts_filtered (val);
1957 puts_filtered ("\n");
1958 }
1959 else
1960 {
1961 puts_filtered ("Environment variable \"");
1962 puts_filtered (var);
1963 puts_filtered ("\" not defined.\n");
1964 }
1965 }
1966 else
1967 {
1968 char **envp = current_inferior ()->environment.envp ();
1969
1970 for (int idx = 0; envp[idx] != NULL; ++idx)
1971 {
1972 puts_filtered (envp[idx]);
1973 puts_filtered ("\n");
1974 }
1975 }
1976}
1977
1978static void
1979set_environment_command (const char *arg, int from_tty)
1980{
1981 const char *p, *val;
1982 int nullset = 0;
1983
1984 if (arg == 0)
1985 error_no_arg (_("environment variable and value"));
1986
1987 /* Find separation between variable name and value. */
1988 p = (char *) strchr (arg, '=');
1989 val = (char *) strchr (arg, ' ');
1990
1991 if (p != 0 && val != 0)
1992 {
1993 /* We have both a space and an equals. If the space is before the
1994 equals, walk forward over the spaces til we see a nonspace
1995 (possibly the equals). */
1996 if (p > val)
1997 while (*val == ' ')
1998 val++;
1999
2000 /* Now if the = is after the char following the spaces,
2001 take the char following the spaces. */
2002 if (p > val)
2003 p = val - 1;
2004 }
2005 else if (val != 0 && p == 0)
2006 p = val;
2007
2008 if (p == arg)
2009 error_no_arg (_("environment variable to set"));
2010
2011 if (p == 0 || p[1] == 0)
2012 {
2013 nullset = 1;
2014 if (p == 0)
2015 p = arg + strlen (arg); /* So that savestring below will work. */
2016 }
2017 else
2018 {
2019 /* Not setting variable value to null. */
2020 val = p + 1;
2021 while (*val == ' ' || *val == '\t')
2022 val++;
2023 }
2024
2025 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
2026 p--;
2027
2028 std::string var (arg, p - arg);
2029 if (nullset)
2030 {
2031 printf_filtered (_("Setting environment variable "
2032 "\"%s\" to null value.\n"),
2033 var.c_str ());
2034 current_inferior ()->environment.set (var.c_str (), "");
2035 }
2036 else
2037 current_inferior ()->environment.set (var.c_str (), val);
2038}
2039
2040static void
2041unset_environment_command (const char *var, int from_tty)
2042{
2043 if (var == 0)
2044 {
2045 /* If there is no argument, delete all environment variables.
2046 Ask for confirmation if reading from the terminal. */
2047 if (!from_tty || query (_("Delete all environment variables? ")))
2048 current_inferior ()->environment.clear ();
2049 }
2050 else
2051 current_inferior ()->environment.unset (var);
2052}
2053
2054/* Handle the execution path (PATH variable). */
2055
2056static const char path_var_name[] = "PATH";
2057
2058static void
2059path_info (const char *args, int from_tty)
2060{
2061 puts_filtered ("Executable and object file path: ");
2062 puts_filtered (current_inferior ()->environment.get (path_var_name));
2063 puts_filtered ("\n");
2064}
2065
2066/* Add zero or more directories to the front of the execution path. */
2067
2068static void
2069path_command (const char *dirname, int from_tty)
2070{
2071 char *exec_path;
2072 const char *env;
2073
2074 dont_repeat ();
2075 env = current_inferior ()->environment.get (path_var_name);
2076 /* Can be null if path is not set. */
2077 if (!env)
2078 env = "";
2079 exec_path = xstrdup (env);
2080 mod_path (dirname, &exec_path);
2081 current_inferior ()->environment.set (path_var_name, exec_path);
2082 xfree (exec_path);
2083 if (from_tty)
2084 path_info (NULL, from_tty);
2085}
2086\f
2087
2088static void
2089pad_to_column (string_file &stream, int col)
2090{
2091 /* At least one space must be printed to separate columns. */
2092 stream.putc (' ');
2093 const int size = stream.size ();
2094 if (size < col)
2095 stream.puts (n_spaces (col - size));
2096}
2097
2098/* Print out the register NAME with value VAL, to FILE, in the default
2099 fashion. */
2100
2101static void
2102default_print_one_register_info (struct ui_file *file,
2103 const char *name,
2104 struct value *val)
2105{
2106 struct type *regtype = value_type (val);
2107 int print_raw_format;
2108 string_file format_stream;
2109 enum tab_stops
2110 {
2111 value_column_1 = 15,
2112 /* Give enough room for "0x", 16 hex digits and two spaces in
2113 preceding column. */
2114 value_column_2 = value_column_1 + 2 + 16 + 2,
2115 };
2116
2117 format_stream.puts (name);
2118 pad_to_column (format_stream, value_column_1);
2119
2120 print_raw_format = (value_entirely_available (val)
2121 && !value_optimized_out (val));
2122
2123 /* If virtual format is floating, print it that way, and in raw
2124 hex. */
2125 if (regtype->code () == TYPE_CODE_FLT
2126 || regtype->code () == TYPE_CODE_DECFLOAT)
2127 {
2128 struct value_print_options opts;
2129 const gdb_byte *valaddr = value_contents_for_printing (val);
2130 enum bfd_endian byte_order = type_byte_order (regtype);
2131
2132 get_user_print_options (&opts);
2133 opts.deref_ref = 1;
2134
2135 common_val_print (val, &format_stream, 0, &opts, current_language);
2136
2137 if (print_raw_format)
2138 {
2139 pad_to_column (format_stream, value_column_2);
2140 format_stream.puts ("(raw ");
2141 print_hex_chars (&format_stream, valaddr, TYPE_LENGTH (regtype),
2142 byte_order, true);
2143 format_stream.putc (')');
2144 }
2145 }
2146 else
2147 {
2148 struct value_print_options opts;
2149
2150 /* Print the register in hex. */
2151 get_formatted_print_options (&opts, 'x');
2152 opts.deref_ref = 1;
2153 common_val_print (val, &format_stream, 0, &opts, current_language);
2154 /* If not a vector register, print it also according to its
2155 natural format. */
2156 if (print_raw_format && TYPE_VECTOR (regtype) == 0)
2157 {
2158 pad_to_column (format_stream, value_column_2);
2159 get_user_print_options (&opts);
2160 opts.deref_ref = 1;
2161 common_val_print (val, &format_stream, 0, &opts, current_language);
2162 }
2163 }
2164
2165 fputs_filtered (format_stream.c_str (), file);
2166 fprintf_filtered (file, "\n");
2167}
2168
2169/* Print out the machine register regnum. If regnum is -1, print all
2170 registers (print_all == 1) or all non-float and non-vector
2171 registers (print_all == 0).
2172
2173 For most machines, having all_registers_info() print the
2174 register(s) one per line is good enough. If a different format is
2175 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2176 regs), or there is an existing convention for showing all the
2177 registers, define the architecture method PRINT_REGISTERS_INFO to
2178 provide that format. */
2179
2180void
2181default_print_registers_info (struct gdbarch *gdbarch,
2182 struct ui_file *file,
2183 struct frame_info *frame,
2184 int regnum, int print_all)
2185{
2186 int i;
2187 const int numregs = gdbarch_num_cooked_regs (gdbarch);
2188
2189 for (i = 0; i < numregs; i++)
2190 {
2191 /* Decide between printing all regs, non-float / vector regs, or
2192 specific reg. */
2193 if (regnum == -1)
2194 {
2195 if (print_all)
2196 {
2197 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
2198 continue;
2199 }
2200 else
2201 {
2202 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
2203 continue;
2204 }
2205 }
2206 else
2207 {
2208 if (i != regnum)
2209 continue;
2210 }
2211
2212 /* If the register name is empty, it is undefined for this
2213 processor, so don't display anything. */
2214 if (gdbarch_register_name (gdbarch, i) == NULL
2215 || *(gdbarch_register_name (gdbarch, i)) == '\0')
2216 continue;
2217
2218 default_print_one_register_info (file,
2219 gdbarch_register_name (gdbarch, i),
2220 value_of_register (i, frame));
2221 }
2222}
2223
2224void
2225registers_info (const char *addr_exp, int fpregs)
2226{
2227 struct frame_info *frame;
2228 struct gdbarch *gdbarch;
2229
2230 if (!target_has_registers)
2231 error (_("The program has no registers now."));
2232 frame = get_selected_frame (NULL);
2233 gdbarch = get_frame_arch (frame);
2234
2235 if (!addr_exp)
2236 {
2237 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2238 frame, -1, fpregs);
2239 return;
2240 }
2241
2242 while (*addr_exp != '\0')
2243 {
2244 const char *start;
2245 const char *end;
2246
2247 /* Skip leading white space. */
2248 addr_exp = skip_spaces (addr_exp);
2249
2250 /* Discard any leading ``$''. Check that there is something
2251 resembling a register following it. */
2252 if (addr_exp[0] == '$')
2253 addr_exp++;
2254 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
2255 error (_("Missing register name"));
2256
2257 /* Find the start/end of this register name/num/group. */
2258 start = addr_exp;
2259 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
2260 addr_exp++;
2261 end = addr_exp;
2262
2263 /* Figure out what we've found and display it. */
2264
2265 /* A register name? */
2266 {
2267 int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start);
2268
2269 if (regnum >= 0)
2270 {
2271 /* User registers lie completely outside of the range of
2272 normal registers. Catch them early so that the target
2273 never sees them. */
2274 if (regnum >= gdbarch_num_cooked_regs (gdbarch))
2275 {
2276 struct value *regval = value_of_user_reg (regnum, frame);
2277 const char *regname = user_reg_map_regnum_to_name (gdbarch,
2278 regnum);
2279
2280 /* Print in the same fashion
2281 gdbarch_print_registers_info's default
2282 implementation prints. */
2283 default_print_one_register_info (gdb_stdout,
2284 regname,
2285 regval);
2286 }
2287 else
2288 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2289 frame, regnum, fpregs);
2290 continue;
2291 }
2292 }
2293
2294 /* A register group? */
2295 {
2296 struct reggroup *group;
2297
2298 for (group = reggroup_next (gdbarch, NULL);
2299 group != NULL;
2300 group = reggroup_next (gdbarch, group))
2301 {
2302 /* Don't bother with a length check. Should the user
2303 enter a short register group name, go with the first
2304 group that matches. */
2305 if (strncmp (start, reggroup_name (group), end - start) == 0)
2306 break;
2307 }
2308 if (group != NULL)
2309 {
2310 int regnum;
2311
2312 for (regnum = 0;
2313 regnum < gdbarch_num_cooked_regs (gdbarch);
2314 regnum++)
2315 {
2316 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
2317 gdbarch_print_registers_info (gdbarch,
2318 gdb_stdout, frame,
2319 regnum, fpregs);
2320 }
2321 continue;
2322 }
2323 }
2324
2325 /* Nothing matched. */
2326 error (_("Invalid register `%.*s'"), (int) (end - start), start);
2327 }
2328}
2329
2330static void
2331info_all_registers_command (const char *addr_exp, int from_tty)
2332{
2333 registers_info (addr_exp, 1);
2334}
2335
2336static void
2337info_registers_command (const char *addr_exp, int from_tty)
2338{
2339 registers_info (addr_exp, 0);
2340}
2341
2342static void
2343print_vector_info (struct ui_file *file,
2344 struct frame_info *frame, const char *args)
2345{
2346 struct gdbarch *gdbarch = get_frame_arch (frame);
2347
2348 if (gdbarch_print_vector_info_p (gdbarch))
2349 gdbarch_print_vector_info (gdbarch, file, frame, args);
2350 else
2351 {
2352 int regnum;
2353 int printed_something = 0;
2354
2355 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
2356 {
2357 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
2358 {
2359 printed_something = 1;
2360 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2361 }
2362 }
2363 if (!printed_something)
2364 fprintf_filtered (file, "No vector information\n");
2365 }
2366}
2367
2368static void
2369info_vector_command (const char *args, int from_tty)
2370{
2371 if (!target_has_registers)
2372 error (_("The program has no registers now."));
2373
2374 print_vector_info (gdb_stdout, get_selected_frame (NULL), args);
2375}
2376\f
2377/* Kill the inferior process. Make us have no inferior. */
2378
2379static void
2380kill_command (const char *arg, int from_tty)
2381{
2382 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2383 It should be a distinct flag that indicates that a target is active, cuz
2384 some targets don't have processes! */
2385
2386 if (inferior_ptid == null_ptid)
2387 error (_("The program is not being run."));
2388 if (!query (_("Kill the program being debugged? ")))
2389 error (_("Not confirmed."));
2390
2391 int pid = current_inferior ()->pid;
2392 /* Save the pid as a string before killing the inferior, since that
2393 may unpush the current target, and we need the string after. */
2394 std::string pid_str = target_pid_to_str (ptid_t (pid));
2395 int infnum = current_inferior ()->num;
2396
2397 target_kill ();
2398
2399 if (print_inferior_events)
2400 printf_unfiltered (_("[Inferior %d (%s) killed]\n"),
2401 infnum, pid_str.c_str ());
2402
2403 bfd_cache_close_all ();
2404}
2405
2406/* Used in `attach&' command. Proceed threads of inferior INF iff
2407 they stopped due to debugger request, and when they did, they
2408 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2409 have been explicitly been told to stop. */
2410
2411static void
2412proceed_after_attach (inferior *inf)
2413{
2414 /* Don't error out if the current thread is running, because
2415 there may be other stopped threads. */
2416
2417 /* Backup current thread and selected frame. */
2418 scoped_restore_current_thread restore_thread;
2419
2420 for (thread_info *thread : inf->non_exited_threads ())
2421 if (!thread->executing
2422 && !thread->stop_requested
2423 && thread->suspend.stop_signal == GDB_SIGNAL_0)
2424 {
2425 switch_to_thread (thread);
2426 clear_proceed_status (0);
2427 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2428 }
2429}
2430
2431/* See inferior.h. */
2432
2433void
2434setup_inferior (int from_tty)
2435{
2436 struct inferior *inferior;
2437
2438 inferior = current_inferior ();
2439 inferior->needs_setup = 0;
2440
2441 /* If no exec file is yet known, try to determine it from the
2442 process itself. */
2443 if (get_exec_file (0) == NULL)
2444 exec_file_locate_attach (inferior_ptid.pid (), 1, from_tty);
2445 else
2446 {
2447 reopen_exec_file ();
2448 reread_symbols ();
2449 }
2450
2451 /* Take any necessary post-attaching actions for this platform. */
2452 target_post_attach (inferior_ptid.pid ());
2453
2454 post_create_inferior (current_top_target (), from_tty);
2455}
2456
2457/* What to do after the first program stops after attaching. */
2458enum attach_post_wait_mode
2459{
2460 /* Do nothing. Leaves threads as they are. */
2461 ATTACH_POST_WAIT_NOTHING,
2462
2463 /* Re-resume threads that are marked running. */
2464 ATTACH_POST_WAIT_RESUME,
2465
2466 /* Stop all threads. */
2467 ATTACH_POST_WAIT_STOP,
2468};
2469
2470/* Called after we've attached to a process and we've seen it stop for
2471 the first time. If ASYNC_EXEC is true, re-resume threads that
2472 should be running. Else if ATTACH, */
2473
2474static void
2475attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mode)
2476{
2477 struct inferior *inferior;
2478
2479 inferior = current_inferior ();
2480 inferior->control.stop_soon = NO_STOP_QUIETLY;
2481
2482 if (inferior->needs_setup)
2483 setup_inferior (from_tty);
2484
2485 if (mode == ATTACH_POST_WAIT_RESUME)
2486 {
2487 /* The user requested an `attach&', so be sure to leave threads
2488 that didn't get a signal running. */
2489
2490 /* Immediatelly resume all suspended threads of this inferior,
2491 and this inferior only. This should have no effect on
2492 already running threads. If a thread has been stopped with a
2493 signal, leave it be. */
2494 if (non_stop)
2495 proceed_after_attach (inferior);
2496 else
2497 {
2498 if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0)
2499 {
2500 clear_proceed_status (0);
2501 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2502 }
2503 }
2504 }
2505 else if (mode == ATTACH_POST_WAIT_STOP)
2506 {
2507 /* The user requested a plain `attach', so be sure to leave
2508 the inferior stopped. */
2509
2510 /* At least the current thread is already stopped. */
2511
2512 /* In all-stop, by definition, all threads have to be already
2513 stopped at this point. In non-stop, however, although the
2514 selected thread is stopped, others may still be executing.
2515 Be sure to explicitly stop all threads of the process. This
2516 should have no effect on already stopped threads. */
2517 if (non_stop)
2518 target_stop (ptid_t (inferior->pid));
2519 else if (target_is_non_stop_p ())
2520 {
2521 struct thread_info *lowest = inferior_thread ();
2522
2523 stop_all_threads ();
2524
2525 /* It's not defined which thread will report the attach
2526 stop. For consistency, always select the thread with
2527 lowest GDB number, which should be the main thread, if it
2528 still exists. */
2529 for (thread_info *thread : current_inferior ()->non_exited_threads ())
2530 if (thread->inf->num < lowest->inf->num
2531 || thread->per_inf_num < lowest->per_inf_num)
2532 lowest = thread;
2533
2534 switch_to_thread (lowest);
2535 }
2536
2537 /* Tell the user/frontend where we're stopped. */
2538 normal_stop ();
2539 if (deprecated_attach_hook)
2540 deprecated_attach_hook ();
2541 }
2542}
2543
2544struct attach_command_continuation_args
2545{
2546 char *args;
2547 int from_tty;
2548 enum attach_post_wait_mode mode;
2549};
2550
2551static void
2552attach_command_continuation (void *args, int err)
2553{
2554 struct attach_command_continuation_args *a
2555 = (struct attach_command_continuation_args *) args;
2556
2557 if (err)
2558 return;
2559
2560 attach_post_wait (a->args, a->from_tty, a->mode);
2561}
2562
2563static void
2564attach_command_continuation_free_args (void *args)
2565{
2566 struct attach_command_continuation_args *a
2567 = (struct attach_command_continuation_args *) args;
2568
2569 xfree (a->args);
2570 xfree (a);
2571}
2572
2573/* "attach" command entry point. Takes a program started up outside
2574 of gdb and ``attaches'' to it. This stops it cold in its tracks
2575 and allows us to start debugging it. */
2576
2577void
2578attach_command (const char *args, int from_tty)
2579{
2580 int async_exec;
2581 struct target_ops *attach_target;
2582 struct inferior *inferior = current_inferior ();
2583 enum attach_post_wait_mode mode;
2584
2585 dont_repeat (); /* Not for the faint of heart */
2586
2587 if (gdbarch_has_global_solist (target_gdbarch ()))
2588 /* Don't complain if all processes share the same symbol
2589 space. */
2590 ;
2591 else if (target_has_execution)
2592 {
2593 if (query (_("A program is being debugged already. Kill it? ")))
2594 target_kill ();
2595 else
2596 error (_("Not killed."));
2597 }
2598
2599 /* Clean up any leftovers from other runs. Some other things from
2600 this function should probably be moved into target_pre_inferior. */
2601 target_pre_inferior (from_tty);
2602
2603 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
2604 args = stripped.get ();
2605
2606 attach_target = find_attach_target ();
2607
2608 prepare_execution_command (attach_target, async_exec);
2609
2610 if (non_stop && !attach_target->supports_non_stop ())
2611 error (_("Cannot attach to this target in non-stop mode"));
2612
2613 attach_target->attach (args, from_tty);
2614 /* to_attach should push the target, so after this point we
2615 shouldn't refer to attach_target again. */
2616 attach_target = NULL;
2617
2618 /* Set up the "saved terminal modes" of the inferior
2619 based on what modes we are starting it with. */
2620 target_terminal::init ();
2621
2622 /* Install inferior's terminal modes. This may look like a no-op,
2623 as we've just saved them above, however, this does more than
2624 restore terminal settings:
2625
2626 - installs a SIGINT handler that forwards SIGINT to the inferior.
2627 Otherwise a Ctrl-C pressed just while waiting for the initial
2628 stop would end up as a spurious Quit.
2629
2630 - removes stdin from the event loop, which we need if attaching
2631 in the foreground, otherwise on targets that report an initial
2632 stop on attach (which are most) we'd process input/commands
2633 while we're in the event loop waiting for that stop. That is,
2634 before the attach continuation runs and the command is really
2635 finished. */
2636 target_terminal::inferior ();
2637
2638 /* Set up execution context to know that we should return from
2639 wait_for_inferior as soon as the target reports a stop. */
2640 init_wait_for_inferior ();
2641 clear_proceed_status (0);
2642
2643 inferior->needs_setup = 1;
2644
2645 if (target_is_non_stop_p ())
2646 {
2647 /* If we find that the current thread isn't stopped, explicitly
2648 do so now, because we're going to install breakpoints and
2649 poke at memory. */
2650
2651 if (async_exec)
2652 /* The user requested an `attach&'; stop just one thread. */
2653 target_stop (inferior_ptid);
2654 else
2655 /* The user requested an `attach', so stop all threads of this
2656 inferior. */
2657 target_stop (ptid_t (inferior_ptid.pid ()));
2658 }
2659
2660 /* Check for exec file mismatch, and let the user solve it. */
2661 validate_exec_file (from_tty);
2662
2663 mode = async_exec ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_STOP;
2664
2665 /* Some system don't generate traps when attaching to inferior.
2666 E.g. Mach 3 or GNU hurd. */
2667 if (!target_attach_no_wait ())
2668 {
2669 struct attach_command_continuation_args *a;
2670
2671 /* Careful here. See comments in inferior.h. Basically some
2672 OSes don't ignore SIGSTOPs on continue requests anymore. We
2673 need a way for handle_inferior_event to reset the stop_signal
2674 variable after an attach, and this is what
2675 STOP_QUIETLY_NO_SIGSTOP is for. */
2676 inferior->control.stop_soon = STOP_QUIETLY_NO_SIGSTOP;
2677
2678 /* Wait for stop. */
2679 a = XNEW (struct attach_command_continuation_args);
2680 a->args = xstrdup (args);
2681 a->from_tty = from_tty;
2682 a->mode = mode;
2683 add_inferior_continuation (attach_command_continuation, a,
2684 attach_command_continuation_free_args);
2685
2686 /* Let infrun consider waiting for events out of this
2687 target. */
2688 inferior->process_target ()->threads_executing = true;
2689
2690 if (!target_is_async_p ())
2691 mark_infrun_async_event_handler ();
2692 return;
2693 }
2694 else
2695 attach_post_wait (args, from_tty, mode);
2696}
2697
2698/* We had just found out that the target was already attached to an
2699 inferior. PTID points at a thread of this new inferior, that is
2700 the most likely to be stopped right now, but not necessarily so.
2701 The new inferior is assumed to be already added to the inferior
2702 list at this point. If LEAVE_RUNNING, then leave the threads of
2703 this inferior running, except those we've explicitly seen reported
2704 as stopped. */
2705
2706void
2707notice_new_inferior (thread_info *thr, int leave_running, int from_tty)
2708{
2709 enum attach_post_wait_mode mode
2710 = leave_running ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_NOTHING;
2711
2712 gdb::optional<scoped_restore_current_thread> restore_thread;
2713
2714 if (inferior_ptid != null_ptid)
2715 restore_thread.emplace ();
2716
2717 /* Avoid reading registers -- we haven't fetched the target
2718 description yet. */
2719 switch_to_thread_no_regs (thr);
2720
2721 /* When we "notice" a new inferior we need to do all the things we
2722 would normally do if we had just attached to it. */
2723
2724 if (thr->executing)
2725 {
2726 struct attach_command_continuation_args *a;
2727 struct inferior *inferior = current_inferior ();
2728
2729 /* We're going to install breakpoints, and poke at memory,
2730 ensure that the inferior is stopped for a moment while we do
2731 that. */
2732 target_stop (inferior_ptid);
2733
2734 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
2735
2736 /* Wait for stop before proceeding. */
2737 a = XNEW (struct attach_command_continuation_args);
2738 a->args = xstrdup ("");
2739 a->from_tty = from_tty;
2740 a->mode = mode;
2741 add_inferior_continuation (attach_command_continuation, a,
2742 attach_command_continuation_free_args);
2743
2744 return;
2745 }
2746
2747 attach_post_wait ("" /* args */, from_tty, mode);
2748}
2749
2750/*
2751 * detach_command --
2752 * takes a program previously attached to and detaches it.
2753 * The program resumes execution and will no longer stop
2754 * on signals, etc. We better not have left any breakpoints
2755 * in the program or it'll die when it hits one. For this
2756 * to work, it may be necessary for the process to have been
2757 * previously attached. It *might* work if the program was
2758 * started via the normal ptrace (PTRACE_TRACEME).
2759 */
2760
2761void
2762detach_command (const char *args, int from_tty)
2763{
2764 dont_repeat (); /* Not for the faint of heart. */
2765
2766 if (inferior_ptid == null_ptid)
2767 error (_("The program is not being run."));
2768
2769 query_if_trace_running (from_tty);
2770
2771 disconnect_tracing ();
2772
2773 target_detach (current_inferior (), from_tty);
2774
2775 /* The current inferior process was just detached successfully. Get
2776 rid of breakpoints that no longer make sense. Note we don't do
2777 this within target_detach because that is also used when
2778 following child forks, and in that case we will want to transfer
2779 breakpoints to the child, not delete them. */
2780 breakpoint_init_inferior (inf_exited);
2781
2782 /* If the solist is global across inferiors, don't clear it when we
2783 detach from a single inferior. */
2784 if (!gdbarch_has_global_solist (target_gdbarch ()))
2785 no_shared_libraries (NULL, from_tty);
2786
2787 if (deprecated_detach_hook)
2788 deprecated_detach_hook ();
2789}
2790
2791/* Disconnect from the current target without resuming it (leaving it
2792 waiting for a debugger).
2793
2794 We'd better not have left any breakpoints in the program or the
2795 next debugger will get confused. Currently only supported for some
2796 remote targets, since the normal attach mechanisms don't work on
2797 stopped processes on some native platforms (e.g. GNU/Linux). */
2798
2799static void
2800disconnect_command (const char *args, int from_tty)
2801{
2802 dont_repeat (); /* Not for the faint of heart. */
2803 query_if_trace_running (from_tty);
2804 disconnect_tracing ();
2805 target_disconnect (args, from_tty);
2806 no_shared_libraries (NULL, from_tty);
2807 init_thread_list ();
2808 if (deprecated_detach_hook)
2809 deprecated_detach_hook ();
2810}
2811
2812/* Stop PTID in the current target, and tag the PTID threads as having
2813 been explicitly requested to stop. PTID can be a thread, a
2814 process, or minus_one_ptid, meaning all threads of all inferiors of
2815 the current target. */
2816
2817static void
2818stop_current_target_threads_ns (ptid_t ptid)
2819{
2820 target_stop (ptid);
2821
2822 /* Tag the thread as having been explicitly requested to stop, so
2823 other parts of gdb know not to resume this thread automatically,
2824 if it was stopped due to an internal event. Limit this to
2825 non-stop mode, as when debugging a multi-threaded application in
2826 all-stop mode, we will only get one stop event --- it's undefined
2827 which thread will report the event. */
2828 set_stop_requested (current_inferior ()->process_target (),
2829 ptid, 1);
2830}
2831
2832/* See inferior.h. */
2833
2834void
2835interrupt_target_1 (bool all_threads)
2836{
2837 if (non_stop)
2838 {
2839 if (all_threads)
2840 {
2841 scoped_restore_current_thread restore_thread;
2842
2843 for (inferior *inf : all_inferiors ())
2844 {
2845 switch_to_inferior_no_thread (inf);
2846 stop_current_target_threads_ns (minus_one_ptid);
2847 }
2848 }
2849 else
2850 stop_current_target_threads_ns (inferior_ptid);
2851 }
2852 else
2853 target_interrupt ();
2854}
2855
2856/* interrupt [-a]
2857 Stop the execution of the target while running in async mode, in
2858 the background. In all-stop, stop the whole process. In non-stop
2859 mode, stop the current thread only by default, or stop all threads
2860 if the `-a' switch is used. */
2861
2862static void
2863interrupt_command (const char *args, int from_tty)
2864{
2865 if (target_can_async_p ())
2866 {
2867 int all_threads = 0;
2868
2869 dont_repeat (); /* Not for the faint of heart. */
2870
2871 if (args != NULL
2872 && startswith (args, "-a"))
2873 all_threads = 1;
2874
2875 if (!non_stop && all_threads)
2876 error (_("-a is meaningless in all-stop mode."));
2877
2878 interrupt_target_1 (all_threads);
2879 }
2880}
2881
2882/* See inferior.h. */
2883
2884void
2885default_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
2886 struct frame_info *frame, const char *args)
2887{
2888 int regnum;
2889 int printed_something = 0;
2890
2891 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
2892 {
2893 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
2894 {
2895 printed_something = 1;
2896 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2897 }
2898 }
2899 if (!printed_something)
2900 fprintf_filtered (file, "No floating-point info "
2901 "available for this processor.\n");
2902}
2903
2904static void
2905info_float_command (const char *args, int from_tty)
2906{
2907 struct frame_info *frame;
2908
2909 if (!target_has_registers)
2910 error (_("The program has no registers now."));
2911
2912 frame = get_selected_frame (NULL);
2913 gdbarch_print_float_info (get_frame_arch (frame), gdb_stdout, frame, args);
2914}
2915\f
2916/* Implement `info proc' family of commands. */
2917
2918static void
2919info_proc_cmd_1 (const char *args, enum info_proc_what what, int from_tty)
2920{
2921 struct gdbarch *gdbarch = get_current_arch ();
2922
2923 if (!target_info_proc (args, what))
2924 {
2925 if (gdbarch_info_proc_p (gdbarch))
2926 gdbarch_info_proc (gdbarch, args, what);
2927 else
2928 error (_("Not supported on this target."));
2929 }
2930}
2931
2932/* Implement `info proc' when given without any further parameters. */
2933
2934static void
2935info_proc_cmd (const char *args, int from_tty)
2936{
2937 info_proc_cmd_1 (args, IP_MINIMAL, from_tty);
2938}
2939
2940/* Implement `info proc mappings'. */
2941
2942static void
2943info_proc_cmd_mappings (const char *args, int from_tty)
2944{
2945 info_proc_cmd_1 (args, IP_MAPPINGS, from_tty);
2946}
2947
2948/* Implement `info proc stat'. */
2949
2950static void
2951info_proc_cmd_stat (const char *args, int from_tty)
2952{
2953 info_proc_cmd_1 (args, IP_STAT, from_tty);
2954}
2955
2956/* Implement `info proc status'. */
2957
2958static void
2959info_proc_cmd_status (const char *args, int from_tty)
2960{
2961 info_proc_cmd_1 (args, IP_STATUS, from_tty);
2962}
2963
2964/* Implement `info proc cwd'. */
2965
2966static void
2967info_proc_cmd_cwd (const char *args, int from_tty)
2968{
2969 info_proc_cmd_1 (args, IP_CWD, from_tty);
2970}
2971
2972/* Implement `info proc cmdline'. */
2973
2974static void
2975info_proc_cmd_cmdline (const char *args, int from_tty)
2976{
2977 info_proc_cmd_1 (args, IP_CMDLINE, from_tty);
2978}
2979
2980/* Implement `info proc exe'. */
2981
2982static void
2983info_proc_cmd_exe (const char *args, int from_tty)
2984{
2985 info_proc_cmd_1 (args, IP_EXE, from_tty);
2986}
2987
2988/* Implement `info proc files'. */
2989
2990static void
2991info_proc_cmd_files (const char *args, int from_tty)
2992{
2993 info_proc_cmd_1 (args, IP_FILES, from_tty);
2994}
2995
2996/* Implement `info proc all'. */
2997
2998static void
2999info_proc_cmd_all (const char *args, int from_tty)
3000{
3001 info_proc_cmd_1 (args, IP_ALL, from_tty);
3002}
3003
3004/* Implement `show print finish'. */
3005
3006static void
3007show_print_finish (struct ui_file *file, int from_tty,
3008 struct cmd_list_element *c,
3009 const char *value)
3010{
3011 fprintf_filtered (file, _("\
3012Printing of return value after `finish' is %s.\n"),
3013 value);
3014}
3015
3016
3017/* This help string is used for the run, start, and starti commands.
3018 It is defined as a macro to prevent duplication. */
3019
3020#define RUN_ARGS_HELP \
3021"You may specify arguments to give it.\n\
3022Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3023shell that will start the program (specified by the \"$SHELL\" environment\n\
3024variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3025are also allowed.\n\
3026\n\
3027With no arguments, uses arguments last specified (with \"run\" or \n\
3028\"set args\"). To cancel previous arguments and run with no arguments,\n\
3029use \"set args\" without arguments.\n\
3030\n\
3031To start the inferior without using a shell, use \"set startup-with-shell off\"."
3032
3033void _initialize_infcmd ();
3034void
3035_initialize_infcmd ()
3036{
3037 static struct cmd_list_element *info_proc_cmdlist;
3038 struct cmd_list_element *c = NULL;
3039 const char *cmd_name;
3040
3041 /* Add the filename of the terminal connected to inferior I/O. */
3042 add_setshow_optional_filename_cmd ("inferior-tty", class_run,
3043 &inferior_io_terminal_scratch, _("\
3044Set terminal for future runs of program being debugged."), _("\
3045Show terminal for future runs of program being debugged."), _("\
3046Usage: set inferior-tty [TTY]\n\n\
3047If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3048is restored."),
3049 set_inferior_tty_command,
3050 show_inferior_tty_command,
3051 &setlist, &showlist);
3052 cmd_name = "inferior-tty";
3053 c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1);
3054 gdb_assert (c != NULL);
3055 add_alias_cmd ("tty", c, class_run, 0, &cmdlist);
3056
3057 cmd_name = "args";
3058 add_setshow_string_noescape_cmd (cmd_name, class_run,
3059 &inferior_args_scratch, _("\
3060Set argument list to give program being debugged when it is started."), _("\
3061Show argument list to give program being debugged when it is started."), _("\
3062Follow this command with any number of args, to be passed to the program."),
3063 set_args_command,
3064 show_args_command,
3065 &setlist, &showlist);
3066 c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1);
3067 gdb_assert (c != NULL);
3068 set_cmd_completer (c, filename_completer);
3069
3070 cmd_name = "cwd";
3071 add_setshow_string_noescape_cmd (cmd_name, class_run,
3072 &inferior_cwd_scratch, _("\
3073Set the current working directory to be used when the inferior is started.\n\
3074Changing this setting does not have any effect on inferiors that are\n\
3075already running."),
3076 _("\
3077Show the current working directory that is used when the inferior is started."),
3078 _("\
3079Use this command to change the current working directory that will be used\n\
3080when the inferior is started. This setting does not affect GDB's current\n\
3081working directory."),
3082 set_cwd_command,
3083 show_cwd_command,
3084 &setlist, &showlist);
3085 c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1);
3086 gdb_assert (c != NULL);
3087 set_cmd_completer (c, filename_completer);
3088
3089 c = add_cmd ("environment", no_class, environment_info, _("\
3090The environment to give the program, or one variable's value.\n\
3091With an argument VAR, prints the value of environment variable VAR to\n\
3092give the program being debugged. With no arguments, prints the entire\n\
3093environment to be given to the program."), &showlist);
3094 set_cmd_completer (c, noop_completer);
3095
3096 add_basic_prefix_cmd ("unset", no_class,
3097 _("Complement to certain \"set\" commands."),
3098 &unsetlist, "unset ", 0, &cmdlist);
3099
3100 c = add_cmd ("environment", class_run, unset_environment_command, _("\
3101Cancel environment variable VAR for the program.\n\
3102This does not affect the program until the next \"run\" command."),
3103 &unsetlist);
3104 set_cmd_completer (c, noop_completer);
3105
3106 c = add_cmd ("environment", class_run, set_environment_command, _("\
3107Set environment variable value to give the program.\n\
3108Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3109VALUES of environment variables are uninterpreted strings.\n\
3110This does not affect the program until the next \"run\" command."),
3111 &setlist);
3112 set_cmd_completer (c, noop_completer);
3113
3114 c = add_com ("path", class_files, path_command, _("\
3115Add directory DIR(s) to beginning of search path for object files.\n\
3116$cwd in the path means the current working directory.\n\
3117This path is equivalent to the $PATH shell variable. It is a list of\n\
3118directories, separated by colons. These directories are searched to find\n\
3119fully linked executable files and separately compiled object files as \
3120needed."));
3121 set_cmd_completer (c, filename_completer);
3122
3123 c = add_cmd ("paths", no_class, path_info, _("\
3124Current search path for finding object files.\n\
3125$cwd in the path means the current working directory.\n\
3126This path is equivalent to the $PATH shell variable. It is a list of\n\
3127directories, separated by colons. These directories are searched to find\n\
3128fully linked executable files and separately compiled object files as \
3129needed."),
3130 &showlist);
3131 set_cmd_completer (c, noop_completer);
3132
3133 add_prefix_cmd ("kill", class_run, kill_command,
3134 _("Kill execution of program being debugged."),
3135 &killlist, "kill ", 0, &cmdlist);
3136
3137 add_com ("attach", class_run, attach_command, _("\
3138Attach to a process or file outside of GDB.\n\
3139This command attaches to another target, of the same type as your last\n\
3140\"target\" command (\"info files\" will show your target stack).\n\
3141The command may take as argument a process id or a device file.\n\
3142For a process id, you must have permission to send the process a signal,\n\
3143and it must have the same effective uid as the debugger.\n\
3144When using \"attach\" with a process id, the debugger finds the\n\
3145program running in the process, looking first in the current working\n\
3146directory, or (if not found there) using the source file search path\n\
3147(see the \"directory\" command). You can also use the \"file\" command\n\
3148to specify the program, and to load its symbol table."));
3149
3150 add_prefix_cmd ("detach", class_run, detach_command, _("\
3151Detach a process or file previously attached.\n\
3152If a process, it is no longer traced, and it continues its execution. If\n\
3153you were debugging a file, the file is closed and gdb no longer accesses it."),
3154 &detachlist, "detach ", 0, &cmdlist);
3155
3156 add_com ("disconnect", class_run, disconnect_command, _("\
3157Disconnect from a target.\n\
3158The target will wait for another debugger to connect. Not available for\n\
3159all targets."));
3160
3161 c = add_com ("signal", class_run, signal_command, _("\
3162Continue program with the specified signal.\n\
3163Usage: signal SIGNAL\n\
3164The SIGNAL argument is processed the same as the handle command.\n\
3165\n\
3166An argument of \"0\" means continue the program without sending it a signal.\n\
3167This is useful in cases where the program stopped because of a signal,\n\
3168and you want to resume the program while discarding the signal.\n\
3169\n\
3170In a multi-threaded program the signal is delivered to, or discarded from,\n\
3171the current thread only."));
3172 set_cmd_completer (c, signal_completer);
3173
3174 c = add_com ("queue-signal", class_run, queue_signal_command, _("\
3175Queue a signal to be delivered to the current thread when it is resumed.\n\
3176Usage: queue-signal SIGNAL\n\
3177The SIGNAL argument is processed the same as the handle command.\n\
3178It is an error if the handling state of SIGNAL is \"nopass\".\n\
3179\n\
3180An argument of \"0\" means remove any currently queued signal from\n\
3181the current thread. This is useful in cases where the program stopped\n\
3182because of a signal, and you want to resume it while discarding the signal.\n\
3183\n\
3184In a multi-threaded program the signal is queued with, or discarded from,\n\
3185the current thread only."));
3186 set_cmd_completer (c, signal_completer);
3187
3188 add_com ("stepi", class_run, stepi_command, _("\
3189Step one instruction exactly.\n\
3190Usage: stepi [N]\n\
3191Argument N means step N times (or till program stops for another \
3192reason)."));
3193 add_com_alias ("si", "stepi", class_run, 0);
3194
3195 add_com ("nexti", class_run, nexti_command, _("\
3196Step one instruction, but proceed through subroutine calls.\n\
3197Usage: nexti [N]\n\
3198Argument N means step N times (or till program stops for another \
3199reason)."));
3200 add_com_alias ("ni", "nexti", class_run, 0);
3201
3202 add_com ("finish", class_run, finish_command, _("\
3203Execute until selected stack frame returns.\n\
3204Usage: finish\n\
3205Upon return, the value returned is printed and put in the value history."));
3206 add_com_alias ("fin", "finish", class_run, 1);
3207
3208 add_com ("next", class_run, next_command, _("\
3209Step program, proceeding through subroutine calls.\n\
3210Usage: next [N]\n\
3211Unlike \"step\", if the current source line calls a subroutine,\n\
3212this command does not enter the subroutine, but instead steps over\n\
3213the call, in effect treating it as a single source line."));
3214 add_com_alias ("n", "next", class_run, 1);
3215
3216 add_com ("step", class_run, step_command, _("\
3217Step program until it reaches a different source line.\n\
3218Usage: step [N]\n\
3219Argument N means step N times (or till program stops for another \
3220reason)."));
3221 add_com_alias ("s", "step", class_run, 1);
3222
3223 c = add_com ("until", class_run, until_command, _("\
3224Execute until past the current line or past a LOCATION.\n\
3225Execute until the program reaches a source line greater than the current\n\
3226or a specified location (same args as break command) within the current \
3227frame."));
3228 set_cmd_completer (c, location_completer);
3229 add_com_alias ("u", "until", class_run, 1);
3230
3231 c = add_com ("advance", class_run, advance_command, _("\
3232Continue the program up to the given location (same form as args for break \
3233command).\n\
3234Execution will also stop upon exit from the current stack frame."));
3235 set_cmd_completer (c, location_completer);
3236
3237 c = add_com ("jump", class_run, jump_command, _("\
3238Continue program being debugged at specified line or address.\n\
3239Usage: jump LOCATION\n\
3240Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3241for an address to start at."));
3242 set_cmd_completer (c, location_completer);
3243 add_com_alias ("j", "jump", class_run, 1);
3244
3245 add_com ("continue", class_run, continue_command, _("\
3246Continue program being debugged, after signal or breakpoint.\n\
3247Usage: continue [N]\n\
3248If proceeding from breakpoint, a number N may be used as an argument,\n\
3249which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3250the breakpoint won't break until the Nth time it is reached).\n\
3251\n\
3252If non-stop mode is enabled, continue only the current thread,\n\
3253otherwise all the threads in the program are continued. To \n\
3254continue all stopped threads in non-stop mode, use the -a option.\n\
3255Specifying -a and an ignore count simultaneously is an error."));
3256 add_com_alias ("c", "cont", class_run, 1);
3257 add_com_alias ("fg", "cont", class_run, 1);
3258
3259 c = add_com ("run", class_run, run_command, _("\
3260Start debugged program.\n"
3261RUN_ARGS_HELP));
3262 set_cmd_completer (c, filename_completer);
3263 add_com_alias ("r", "run", class_run, 1);
3264
3265 c = add_com ("start", class_run, start_command, _("\
3266Start the debugged program stopping at the beginning of the main procedure.\n"
3267RUN_ARGS_HELP));
3268 set_cmd_completer (c, filename_completer);
3269
3270 c = add_com ("starti", class_run, starti_command, _("\
3271Start the debugged program stopping at the first instruction.\n"
3272RUN_ARGS_HELP));
3273 set_cmd_completer (c, filename_completer);
3274
3275 add_com ("interrupt", class_run, interrupt_command,
3276 _("Interrupt the execution of the debugged program.\n\
3277If non-stop mode is enabled, interrupt only the current thread,\n\
3278otherwise all the threads in the program are stopped. To \n\
3279interrupt all running threads in non-stop mode, use the -a option."));
3280
3281 c = add_info ("registers", info_registers_command, _("\
3282List of integer registers and their contents, for selected stack frame.\n\
3283One or more register names as argument means describe the given registers.\n\
3284One or more register group names as argument means describe the registers\n\
3285in the named register groups."));
3286 add_info_alias ("r", "registers", 1);
3287 set_cmd_completer (c, reg_or_group_completer);
3288
3289 c = add_info ("all-registers", info_all_registers_command, _("\
3290List of all registers and their contents, for selected stack frame.\n\
3291One or more register names as argument means describe the given registers.\n\
3292One or more register group names as argument means describe the registers\n\
3293in the named register groups."));
3294 set_cmd_completer (c, reg_or_group_completer);
3295
3296 add_info ("program", info_program_command,
3297 _("Execution status of the program."));
3298
3299 add_info ("float", info_float_command,
3300 _("Print the status of the floating point unit."));
3301
3302 add_info ("vector", info_vector_command,
3303 _("Print the status of the vector unit."));
3304
3305 add_prefix_cmd ("proc", class_info, info_proc_cmd,
3306 _("\
3307Show additional information about a process.\n\
3308Specify any process id, or use the program being debugged by default."),
3309 &info_proc_cmdlist, "info proc ",
3310 1/*allow-unknown*/, &infolist);
3311
3312 add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\
3313List memory regions mapped by the specified process."),
3314 &info_proc_cmdlist);
3315
3316 add_cmd ("stat", class_info, info_proc_cmd_stat, _("\
3317List process info from /proc/PID/stat."),
3318 &info_proc_cmdlist);
3319
3320 add_cmd ("status", class_info, info_proc_cmd_status, _("\
3321List process info from /proc/PID/status."),
3322 &info_proc_cmdlist);
3323
3324 add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\
3325List current working directory of the specified process."),
3326 &info_proc_cmdlist);
3327
3328 add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\
3329List command line arguments of the specified process."),
3330 &info_proc_cmdlist);
3331
3332 add_cmd ("exe", class_info, info_proc_cmd_exe, _("\
3333List absolute filename for executable of the specified process."),
3334 &info_proc_cmdlist);
3335
3336 add_cmd ("files", class_info, info_proc_cmd_files, _("\
3337List files opened by the specified process."),
3338 &info_proc_cmdlist);
3339
3340 add_cmd ("all", class_info, info_proc_cmd_all, _("\
3341List all available info about the specified process."),
3342 &info_proc_cmdlist);
3343
3344 add_setshow_boolean_cmd ("finish", class_support,
3345 &user_print_options.finish_print, _("\
3346Set whether `finish' prints the return value."), _("\
3347Show whether `finish' prints the return value."), NULL,
3348 NULL,
3349 show_print_finish,
3350 &setprintlist, &showprintlist);
3351}
This page took 0.033504 seconds and 4 git commands to generate.