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