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