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