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