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