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