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