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