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