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