]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/infcmd.c
Remove most calls to fixup_symbol_section
[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 section = sfn->obj_section (sfn->objfile ());
1110 if (section_is_overlay (section)
1111 && !section_is_mapped (section))
1112 {
1113 if (!query (_("WARNING!!! Destination is in "
1114 "unmapped overlay! Jump anyway? ")))
1115 {
1116 error (_("Not confirmed."));
1117 /* NOTREACHED */
1118 }
1119 }
1120 }
1121
1122 addr = sal.pc;
1123
1124 if (from_tty)
1125 {
1126 gdb_printf (_("Continuing at "));
1127 gdb_puts (paddress (gdbarch, addr));
1128 gdb_printf (".\n");
1129 }
1130
1131 clear_proceed_status (0);
1132 proceed (addr, GDB_SIGNAL_0);
1133 }
1134 \f
1135 /* Continue program giving it specified signal. */
1136
1137 static void
1138 signal_command (const char *signum_exp, int from_tty)
1139 {
1140 enum gdb_signal oursig;
1141 int async_exec;
1142
1143 dont_repeat (); /* Too dangerous. */
1144 ERROR_NO_INFERIOR;
1145 ensure_not_tfind_mode ();
1146 ensure_valid_thread ();
1147 ensure_not_running ();
1148
1149 /* Find out whether we must run in the background. */
1150 gdb::unique_xmalloc_ptr<char> stripped
1151 = strip_bg_char (signum_exp, &async_exec);
1152 signum_exp = stripped.get ();
1153
1154 prepare_execution_command (current_inferior ()->top_target (), async_exec);
1155
1156 if (!signum_exp)
1157 error_no_arg (_("signal number"));
1158
1159 /* It would be even slicker to make signal names be valid expressions,
1160 (the type could be "enum $signal" or some such), then the user could
1161 assign them to convenience variables. */
1162 oursig = gdb_signal_from_name (signum_exp);
1163
1164 if (oursig == GDB_SIGNAL_UNKNOWN)
1165 {
1166 /* No, try numeric. */
1167 int num = parse_and_eval_long (signum_exp);
1168
1169 if (num == 0)
1170 oursig = GDB_SIGNAL_0;
1171 else
1172 oursig = gdb_signal_from_command (num);
1173 }
1174
1175 /* Look for threads other than the current that this command ends up
1176 resuming too (due to schedlock off), and warn if they'll get a
1177 signal delivered. "signal 0" is used to suppress a previous
1178 signal, but if the current thread is no longer the one that got
1179 the signal, then the user is potentially suppressing the signal
1180 of the wrong thread. */
1181 if (!non_stop)
1182 {
1183 int must_confirm = 0;
1184
1185 /* This indicates what will be resumed. Either a single thread,
1186 a whole process, or all threads of all processes. */
1187 ptid_t resume_ptid = user_visible_resume_ptid (0);
1188 process_stratum_target *resume_target
1189 = user_visible_resume_target (resume_ptid);
1190
1191 thread_info *current = inferior_thread ();
1192
1193 for (thread_info *tp : all_non_exited_threads (resume_target, resume_ptid))
1194 {
1195 if (tp == current)
1196 continue;
1197
1198 if (tp->stop_signal () != GDB_SIGNAL_0
1199 && signal_pass_state (tp->stop_signal ()))
1200 {
1201 if (!must_confirm)
1202 gdb_printf (_("Note:\n"));
1203 gdb_printf (_(" Thread %s previously stopped with signal %s, %s.\n"),
1204 print_thread_id (tp),
1205 gdb_signal_to_name (tp->stop_signal ()),
1206 gdb_signal_to_string (tp->stop_signal ()));
1207 must_confirm = 1;
1208 }
1209 }
1210
1211 if (must_confirm
1212 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1213 "still deliver the signals noted above to their respective threads.\n"
1214 "Continue anyway? "),
1215 print_thread_id (inferior_thread ())))
1216 error (_("Not confirmed."));
1217 }
1218
1219 if (from_tty)
1220 {
1221 if (oursig == GDB_SIGNAL_0)
1222 gdb_printf (_("Continuing with no signal.\n"));
1223 else
1224 gdb_printf (_("Continuing with signal %s.\n"),
1225 gdb_signal_to_name (oursig));
1226 }
1227
1228 clear_proceed_status (0);
1229 proceed ((CORE_ADDR) -1, oursig);
1230 }
1231
1232 /* Queue a signal to be delivered to the current thread. */
1233
1234 static void
1235 queue_signal_command (const char *signum_exp, int from_tty)
1236 {
1237 enum gdb_signal oursig;
1238 struct thread_info *tp;
1239
1240 ERROR_NO_INFERIOR;
1241 ensure_not_tfind_mode ();
1242 ensure_valid_thread ();
1243 ensure_not_running ();
1244
1245 if (signum_exp == nullptr)
1246 error_no_arg (_("signal number"));
1247
1248 /* It would be even slicker to make signal names be valid expressions,
1249 (the type could be "enum $signal" or some such), then the user could
1250 assign them to convenience variables. */
1251 oursig = gdb_signal_from_name (signum_exp);
1252
1253 if (oursig == GDB_SIGNAL_UNKNOWN)
1254 {
1255 /* No, try numeric. */
1256 int num = parse_and_eval_long (signum_exp);
1257
1258 if (num == 0)
1259 oursig = GDB_SIGNAL_0;
1260 else
1261 oursig = gdb_signal_from_command (num);
1262 }
1263
1264 if (oursig != GDB_SIGNAL_0
1265 && !signal_pass_state (oursig))
1266 error (_("Signal handling set to not pass this signal to the program."));
1267
1268 tp = inferior_thread ();
1269 tp->set_stop_signal (oursig);
1270 }
1271
1272 /* Data for the FSM that manages the until (with no argument)
1273 command. */
1274
1275 struct until_next_fsm : public thread_fsm
1276 {
1277 /* The thread that as current when the command was executed. */
1278 int thread;
1279
1280 until_next_fsm (struct interp *cmd_interp, int thread)
1281 : thread_fsm (cmd_interp),
1282 thread (thread)
1283 {
1284 }
1285
1286 bool should_stop (struct thread_info *thread) override;
1287 void clean_up (struct thread_info *thread) override;
1288 enum async_reply_reason do_async_reply_reason () override;
1289 };
1290
1291 /* Implementation of the 'should_stop' FSM method for the until (with
1292 no arg) command. */
1293
1294 bool
1295 until_next_fsm::should_stop (struct thread_info *tp)
1296 {
1297 if (tp->control.stop_step)
1298 set_finished ();
1299
1300 return true;
1301 }
1302
1303 /* Implementation of the 'clean_up' FSM method for the until (with no
1304 arg) command. */
1305
1306 void
1307 until_next_fsm::clean_up (struct thread_info *thread)
1308 {
1309 delete_longjmp_breakpoint (thread->global_num);
1310 }
1311
1312 /* Implementation of the 'async_reply_reason' FSM method for the until
1313 (with no arg) command. */
1314
1315 enum async_reply_reason
1316 until_next_fsm::do_async_reply_reason ()
1317 {
1318 return EXEC_ASYNC_END_STEPPING_RANGE;
1319 }
1320
1321 /* Proceed until we reach a different source line with pc greater than
1322 our current one or exit the function. We skip calls in both cases.
1323
1324 Note that eventually this command should probably be changed so
1325 that only source lines are printed out when we hit the breakpoint
1326 we set. This may involve changes to wait_for_inferior and the
1327 proceed status code. */
1328
1329 static void
1330 until_next_command (int from_tty)
1331 {
1332 frame_info_ptr frame;
1333 CORE_ADDR pc;
1334 struct symbol *func;
1335 struct symtab_and_line sal;
1336 struct thread_info *tp = inferior_thread ();
1337 int thread = tp->global_num;
1338 struct until_next_fsm *sm;
1339
1340 clear_proceed_status (0);
1341 set_step_frame (tp);
1342
1343 frame = get_current_frame ();
1344
1345 /* Step until either exited from this function or greater
1346 than the current line (if in symbolic section) or pc (if
1347 not). */
1348
1349 pc = get_frame_pc (frame);
1350 func = find_pc_function (pc);
1351
1352 if (!func)
1353 {
1354 struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc);
1355
1356 if (msymbol.minsym == nullptr)
1357 error (_("Execution is not within a known function."));
1358
1359 tp->control.step_range_start = msymbol.value_address ();
1360 /* The upper-bound of step_range is exclusive. In order to make PC
1361 within the range, set the step_range_end with PC + 1. */
1362 tp->control.step_range_end = pc + 1;
1363 }
1364 else
1365 {
1366 sal = find_pc_line (pc, 0);
1367
1368 tp->control.step_range_start = func->value_block ()->entry_pc ();
1369 tp->control.step_range_end = sal.end;
1370
1371 /* By setting the step_range_end based on the current pc, we are
1372 assuming that the last line table entry for any given source line
1373 will have is_stmt set to true. This is not necessarily the case,
1374 there may be additional entries for the same source line with
1375 is_stmt set false. Consider the following code:
1376
1377 for (int i = 0; i < 10; i++)
1378 loop_body ();
1379
1380 Clang-13, will generate multiple line table entries at the end of
1381 the loop all associated with the 'for' line. The first of these
1382 entries is marked is_stmt true, but the other entries are is_stmt
1383 false.
1384
1385 If we only use the values in SAL, then our stepping range may not
1386 extend to the end of the loop. The until command will reach the
1387 end of the range, find a non is_stmt instruction, and step to the
1388 next is_stmt instruction. This stopping point, however, will be
1389 inside the loop, which is not what we wanted.
1390
1391 Instead, we now check any subsequent line table entries to see if
1392 they are for the same line. If they are, and they are marked
1393 is_stmt false, then we extend the end of our stepping range.
1394
1395 When we finish this process the end of the stepping range will
1396 point either to a line with a different line number, or, will
1397 point at an address for the same line number that is marked as a
1398 statement. */
1399
1400 struct symtab_and_line final_sal
1401 = find_pc_line (tp->control.step_range_end, 0);
1402
1403 while (final_sal.line == sal.line && final_sal.symtab == sal.symtab
1404 && !final_sal.is_stmt)
1405 {
1406 tp->control.step_range_end = final_sal.end;
1407 final_sal = find_pc_line (final_sal.end, 0);
1408 }
1409 }
1410 tp->control.may_range_step = 1;
1411
1412 tp->control.step_over_calls = STEP_OVER_ALL;
1413
1414 set_longjmp_breakpoint (tp, get_frame_id (frame));
1415 delete_longjmp_breakpoint_cleanup lj_deleter (thread);
1416
1417 sm = new until_next_fsm (command_interp (), tp->global_num);
1418 tp->set_thread_fsm (std::unique_ptr<thread_fsm> (sm));
1419 lj_deleter.release ();
1420
1421 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1422 }
1423
1424 static void
1425 until_command (const char *arg, int from_tty)
1426 {
1427 int async_exec;
1428
1429 ERROR_NO_INFERIOR;
1430 ensure_not_tfind_mode ();
1431 ensure_valid_thread ();
1432 ensure_not_running ();
1433
1434 /* Find out whether we must run in the background. */
1435 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1436 arg = stripped.get ();
1437
1438 prepare_execution_command (current_inferior ()->top_target (), async_exec);
1439
1440 if (arg)
1441 until_break_command (arg, from_tty, 0);
1442 else
1443 until_next_command (from_tty);
1444 }
1445
1446 static void
1447 advance_command (const char *arg, int from_tty)
1448 {
1449 int async_exec;
1450
1451 ERROR_NO_INFERIOR;
1452 ensure_not_tfind_mode ();
1453 ensure_valid_thread ();
1454 ensure_not_running ();
1455
1456 if (arg == nullptr)
1457 error_no_arg (_("a location"));
1458
1459 /* Find out whether we must run in the background. */
1460 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1461 arg = stripped.get ();
1462
1463 prepare_execution_command (current_inferior ()->top_target (), async_exec);
1464
1465 until_break_command (arg, from_tty, 1);
1466 }
1467 \f
1468 /* See inferior.h. */
1469
1470 struct value *
1471 get_return_value (struct symbol *func_symbol, struct value *function)
1472 {
1473 regcache *stop_regs = get_current_regcache ();
1474 struct gdbarch *gdbarch = stop_regs->arch ();
1475 struct value *value;
1476
1477 struct type *value_type
1478 = check_typedef (func_symbol->type ()->target_type ());
1479 gdb_assert (value_type->code () != TYPE_CODE_VOID);
1480
1481 if (is_nocall_function (check_typedef (::value_type (function))))
1482 {
1483 warning (_("Function '%s' does not follow the target calling "
1484 "convention, cannot determine its returned value."),
1485 func_symbol->print_name ());
1486
1487 return nullptr;
1488 }
1489
1490 /* FIXME: 2003-09-27: When returning from a nested inferior function
1491 call, it's possible (with no help from the architecture vector)
1492 to locate and return/print a "struct return" value. This is just
1493 a more complicated case of what is already being done in the
1494 inferior function call code. In fact, when inferior function
1495 calls are made async, this will likely be made the norm. */
1496
1497 switch (gdbarch_return_value_as_value (gdbarch, function, value_type,
1498 nullptr, nullptr, nullptr))
1499 {
1500 case RETURN_VALUE_REGISTER_CONVENTION:
1501 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
1502 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
1503 gdbarch_return_value_as_value (gdbarch, function, value_type, stop_regs,
1504 &value, nullptr);
1505 break;
1506 case RETURN_VALUE_STRUCT_CONVENTION:
1507 value = nullptr;
1508 break;
1509 default:
1510 internal_error (_("bad switch"));
1511 }
1512
1513 return value;
1514 }
1515
1516 /* The captured function return value/type and its position in the
1517 value history. */
1518
1519 struct return_value_info
1520 {
1521 /* The captured return value. May be NULL if we weren't able to
1522 retrieve it. See get_return_value. */
1523 struct value *value;
1524
1525 /* The return type. In some cases, we'll not be able extract the
1526 return value, but we always know the type. */
1527 struct type *type;
1528
1529 /* If we captured a value, this is the value history index. */
1530 int value_history_index;
1531 };
1532
1533 /* Helper for print_return_value. */
1534
1535 static void
1536 print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv)
1537 {
1538 if (rv->value != nullptr)
1539 {
1540 /* Print it. */
1541 uiout->text ("Value returned is ");
1542 uiout->field_fmt ("gdb-result-var", "$%d",
1543 rv->value_history_index);
1544 uiout->text (" = ");
1545
1546 if (finish_print)
1547 {
1548 struct value_print_options opts;
1549 get_user_print_options (&opts);
1550
1551 string_file stb;
1552 value_print (rv->value, &stb, &opts);
1553 uiout->field_stream ("return-value", stb);
1554 }
1555 else
1556 uiout->field_string ("return-value", _("<not displayed>"),
1557 metadata_style.style ());
1558 uiout->text ("\n");
1559 }
1560 else
1561 {
1562 std::string type_name = type_to_string (rv->type);
1563 uiout->text ("Value returned has type: ");
1564 uiout->field_string ("return-type", type_name);
1565 uiout->text (".");
1566 uiout->text (" Cannot determine contents\n");
1567 }
1568 }
1569
1570 /* Print the result of a function at the end of a 'finish' command.
1571 RV points at an object representing the captured return value/type
1572 and its position in the value history. */
1573
1574 void
1575 print_return_value (struct ui_out *uiout, struct return_value_info *rv)
1576 {
1577 if (rv->type == nullptr
1578 || check_typedef (rv->type)->code () == TYPE_CODE_VOID)
1579 return;
1580
1581 try
1582 {
1583 /* print_return_value_1 can throw an exception in some
1584 circumstances. We need to catch this so that we still
1585 delete the breakpoint. */
1586 print_return_value_1 (uiout, rv);
1587 }
1588 catch (const gdb_exception &ex)
1589 {
1590 exception_print (gdb_stdout, ex);
1591 }
1592 }
1593
1594 /* Data for the FSM that manages the finish command. */
1595
1596 struct finish_command_fsm : public thread_fsm
1597 {
1598 /* The momentary breakpoint set at the function's return address in
1599 the caller. */
1600 breakpoint_up breakpoint;
1601
1602 /* The function that we're stepping out of. */
1603 struct symbol *function = nullptr;
1604
1605 /* If the FSM finishes successfully, this stores the function's
1606 return value. */
1607 struct return_value_info return_value_info {};
1608
1609 /* If the current function uses the "struct return convention",
1610 this holds the address at which the value being returned will
1611 be stored, or zero if that address could not be determined or
1612 the "struct return convention" is not being used. */
1613 CORE_ADDR return_buf;
1614
1615 explicit finish_command_fsm (struct interp *cmd_interp)
1616 : thread_fsm (cmd_interp)
1617 {
1618 }
1619
1620 bool should_stop (struct thread_info *thread) override;
1621 void clean_up (struct thread_info *thread) override;
1622 struct return_value_info *return_value () override;
1623 enum async_reply_reason do_async_reply_reason () override;
1624 };
1625
1626 /* Implementation of the 'should_stop' FSM method for the finish
1627 commands. Detects whether the thread stepped out of the function
1628 successfully, and if so, captures the function's return value and
1629 marks the FSM finished. */
1630
1631 bool
1632 finish_command_fsm::should_stop (struct thread_info *tp)
1633 {
1634 struct return_value_info *rv = &return_value_info;
1635
1636 if (function != nullptr
1637 && bpstat_find_breakpoint (tp->control.stop_bpstat,
1638 breakpoint.get ()) != nullptr)
1639 {
1640 /* We're done. */
1641 set_finished ();
1642
1643 rv->type = function->type ()->target_type ();
1644 if (rv->type == nullptr)
1645 internal_error (_("finish_command: function has no target type"));
1646
1647 if (check_typedef (rv->type)->code () != TYPE_CODE_VOID)
1648 {
1649 struct value *func;
1650
1651 func = read_var_value (function, nullptr, get_current_frame ());
1652
1653 if (return_buf != 0)
1654 /* Retrieve return value from the buffer where it was saved. */
1655 rv->value = value_at (rv->type, return_buf);
1656 else
1657 rv->value = get_return_value (function, func);
1658
1659 if (rv->value != nullptr)
1660 rv->value_history_index = record_latest_value (rv->value);
1661 }
1662 }
1663 else if (tp->control.stop_step)
1664 {
1665 /* Finishing from an inline frame, or reverse finishing. In
1666 either case, there's no way to retrieve the return value. */
1667 set_finished ();
1668 }
1669
1670 return true;
1671 }
1672
1673 /* Implementation of the 'clean_up' FSM method for the finish
1674 commands. */
1675
1676 void
1677 finish_command_fsm::clean_up (struct thread_info *thread)
1678 {
1679 breakpoint.reset ();
1680 delete_longjmp_breakpoint (thread->global_num);
1681 }
1682
1683 /* Implementation of the 'return_value' FSM method for the finish
1684 commands. */
1685
1686 struct return_value_info *
1687 finish_command_fsm::return_value ()
1688 {
1689 return &return_value_info;
1690 }
1691
1692 /* Implementation of the 'async_reply_reason' FSM method for the
1693 finish commands. */
1694
1695 enum async_reply_reason
1696 finish_command_fsm::do_async_reply_reason ()
1697 {
1698 if (execution_direction == EXEC_REVERSE)
1699 return EXEC_ASYNC_END_STEPPING_RANGE;
1700 else
1701 return EXEC_ASYNC_FUNCTION_FINISHED;
1702 }
1703
1704 /* finish_backward -- helper function for finish_command. */
1705
1706 static void
1707 finish_backward (struct finish_command_fsm *sm)
1708 {
1709 struct symtab_and_line sal;
1710 struct thread_info *tp = inferior_thread ();
1711 CORE_ADDR pc;
1712 CORE_ADDR func_addr;
1713
1714 pc = get_frame_pc (get_current_frame ());
1715
1716 if (find_pc_partial_function (pc, nullptr, &func_addr, nullptr) == 0)
1717 error (_("Cannot find bounds of current function"));
1718
1719 sal = find_pc_line (func_addr, 0);
1720
1721 tp->control.proceed_to_finish = 1;
1722 /* Special case: if we're sitting at the function entry point,
1723 then all we need to do is take a reverse singlestep. We
1724 don't need to set a breakpoint, and indeed it would do us
1725 no good to do so.
1726
1727 Note that this can only happen at frame #0, since there's
1728 no way that a function up the stack can have a return address
1729 that's equal to its entry point. */
1730
1731 if (sal.pc != pc)
1732 {
1733 frame_info_ptr frame = get_selected_frame (nullptr);
1734 struct gdbarch *gdbarch = get_frame_arch (frame);
1735
1736 /* Set a step-resume at the function's entry point. Once that's
1737 hit, we'll do one more step backwards. */
1738 symtab_and_line sr_sal;
1739 sr_sal.pc = sal.pc;
1740 sr_sal.pspace = get_frame_program_space (frame);
1741 insert_step_resume_breakpoint_at_sal (gdbarch,
1742 sr_sal, null_frame_id);
1743
1744 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1745 }
1746 else
1747 {
1748 /* We're almost there -- we just need to back up by one more
1749 single-step. */
1750 tp->control.step_range_start = tp->control.step_range_end = 1;
1751 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1752 }
1753 }
1754
1755 /* finish_forward -- helper function for finish_command. FRAME is the
1756 frame that called the function we're about to step out of. */
1757
1758 static void
1759 finish_forward (struct finish_command_fsm *sm, frame_info_ptr frame)
1760 {
1761 struct frame_id frame_id = get_frame_id (frame);
1762 struct gdbarch *gdbarch = get_frame_arch (frame);
1763 struct symtab_and_line sal;
1764 struct thread_info *tp = inferior_thread ();
1765
1766 sal = find_pc_line (get_frame_pc (frame), 0);
1767 sal.pc = get_frame_pc (frame);
1768
1769 sm->breakpoint = set_momentary_breakpoint (gdbarch, sal,
1770 get_stack_frame_id (frame),
1771 bp_finish);
1772
1773 /* set_momentary_breakpoint invalidates FRAME. */
1774 frame = nullptr;
1775
1776 set_longjmp_breakpoint (tp, frame_id);
1777
1778 /* We want to print return value, please... */
1779 tp->control.proceed_to_finish = 1;
1780
1781 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1782 }
1783
1784 /* Skip frames for "finish". */
1785
1786 static frame_info_ptr
1787 skip_finish_frames (frame_info_ptr frame)
1788 {
1789 frame_info_ptr start;
1790
1791 do
1792 {
1793 start = frame;
1794
1795 frame = skip_tailcall_frames (frame);
1796 if (frame == nullptr)
1797 break;
1798
1799 frame = skip_unwritable_frames (frame);
1800 if (frame == nullptr)
1801 break;
1802 }
1803 while (start != frame);
1804
1805 return frame;
1806 }
1807
1808 /* "finish": Set a temporary breakpoint at the place the selected
1809 frame will return to, then continue. */
1810
1811 static void
1812 finish_command (const char *arg, int from_tty)
1813 {
1814 frame_info_ptr frame;
1815 int async_exec;
1816 struct finish_command_fsm *sm;
1817 struct thread_info *tp;
1818
1819 ERROR_NO_INFERIOR;
1820 ensure_not_tfind_mode ();
1821 ensure_valid_thread ();
1822 ensure_not_running ();
1823
1824 /* Find out whether we must run in the background. */
1825 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1826 arg = stripped.get ();
1827
1828 prepare_execution_command (current_inferior ()->top_target (), async_exec);
1829
1830 if (arg)
1831 error (_("The \"finish\" command does not take any arguments."));
1832
1833 frame = get_prev_frame (get_selected_frame (_("No selected frame.")));
1834 if (frame == 0)
1835 error (_("\"finish\" not meaningful in the outermost frame."));
1836
1837 clear_proceed_status (0);
1838
1839 tp = inferior_thread ();
1840
1841 sm = new finish_command_fsm (command_interp ());
1842
1843 tp->set_thread_fsm (std::unique_ptr<thread_fsm> (sm));
1844
1845 /* Finishing from an inline frame is completely different. We don't
1846 try to show the "return value" - no way to locate it. */
1847 if (get_frame_type (get_selected_frame (_("No selected frame.")))
1848 == INLINE_FRAME)
1849 {
1850 /* Claim we are stepping in the calling frame. An empty step
1851 range means that we will stop once we aren't in a function
1852 called by that frame. We don't use the magic "1" value for
1853 step_range_end, because then infrun will think this is nexti,
1854 and not step over the rest of this inlined function call. */
1855 set_step_info (tp, frame, {});
1856 tp->control.step_range_start = get_frame_pc (frame);
1857 tp->control.step_range_end = tp->control.step_range_start;
1858 tp->control.step_over_calls = STEP_OVER_ALL;
1859
1860 /* Print info on the selected frame, including level number but not
1861 source. */
1862 if (from_tty)
1863 {
1864 gdb_printf (_("Run till exit from "));
1865 print_stack_frame (get_selected_frame (nullptr), 1, LOCATION, 0);
1866 }
1867
1868 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1869 return;
1870 }
1871
1872 /* Find the function we will return from. */
1873 frame_info_ptr callee_frame = get_selected_frame (nullptr);
1874 sm->function = find_pc_function (get_frame_pc (callee_frame));
1875 sm->return_buf = 0; /* Initialize buffer address is not available. */
1876
1877 /* Determine the return convention. If it is RETURN_VALUE_STRUCT_CONVENTION,
1878 attempt to determine the address of the return buffer. */
1879 if (sm->function != nullptr)
1880 {
1881 enum return_value_convention return_value;
1882 struct gdbarch *gdbarch = get_frame_arch (callee_frame);
1883
1884 struct type * val_type
1885 = check_typedef (sm->function->type ()->target_type ());
1886
1887 return_value
1888 = gdbarch_return_value_as_value (gdbarch,
1889 read_var_value (sm->function, nullptr,
1890 callee_frame),
1891 val_type, nullptr, nullptr, nullptr);
1892
1893 if (return_value == RETURN_VALUE_STRUCT_CONVENTION
1894 && val_type->code () != TYPE_CODE_VOID)
1895 sm->return_buf = gdbarch_get_return_buf_addr (gdbarch, val_type,
1896 callee_frame);
1897 }
1898
1899 /* Print info on the selected frame, including level number but not
1900 source. */
1901 if (from_tty)
1902 {
1903 if (execution_direction == EXEC_REVERSE)
1904 gdb_printf (_("Run back to call of "));
1905 else
1906 {
1907 if (sm->function != nullptr && TYPE_NO_RETURN (sm->function->type ())
1908 && !query (_("warning: Function %s does not return normally.\n"
1909 "Try to finish anyway? "),
1910 sm->function->print_name ()))
1911 error (_("Not confirmed."));
1912 gdb_printf (_("Run till exit from "));
1913 }
1914
1915 print_stack_frame (callee_frame, 1, LOCATION, 0);
1916 }
1917
1918 if (execution_direction == EXEC_REVERSE)
1919 finish_backward (sm);
1920 else
1921 {
1922 frame = skip_finish_frames (frame);
1923
1924 if (frame == nullptr)
1925 error (_("Cannot find the caller frame."));
1926
1927 finish_forward (sm, frame);
1928 }
1929 }
1930 \f
1931
1932 static void
1933 info_program_command (const char *args, int from_tty)
1934 {
1935 bpstat *bs;
1936 int num, stat;
1937 ptid_t ptid;
1938 process_stratum_target *proc_target;
1939
1940 if (!target_has_execution ())
1941 {
1942 gdb_printf (_("The program being debugged is not being run.\n"));
1943 return;
1944 }
1945
1946 if (non_stop)
1947 {
1948 ptid = inferior_ptid;
1949 proc_target = current_inferior ()->process_target ();
1950 }
1951 else
1952 get_last_target_status (&proc_target, &ptid, nullptr);
1953
1954 if (ptid == null_ptid || ptid == minus_one_ptid)
1955 error (_("No selected thread."));
1956
1957 thread_info *tp = find_thread_ptid (proc_target, ptid);
1958
1959 if (tp->state == THREAD_EXITED)
1960 error (_("Invalid selected thread."));
1961 else if (tp->state == THREAD_RUNNING)
1962 error (_("Selected thread is running."));
1963
1964 bs = tp->control.stop_bpstat;
1965 stat = bpstat_num (&bs, &num);
1966
1967 target_files_info ();
1968 gdb_printf (_("Program stopped at %s.\n"),
1969 paddress (target_gdbarch (), tp->stop_pc ()));
1970 if (tp->control.stop_step)
1971 gdb_printf (_("It stopped after being stepped.\n"));
1972 else if (stat != 0)
1973 {
1974 /* There may be several breakpoints in the same place, so this
1975 isn't as strange as it seems. */
1976 while (stat != 0)
1977 {
1978 if (stat < 0)
1979 {
1980 gdb_printf (_("It stopped at a breakpoint "
1981 "that has since been deleted.\n"));
1982 }
1983 else
1984 gdb_printf (_("It stopped at breakpoint %d.\n"), num);
1985 stat = bpstat_num (&bs, &num);
1986 }
1987 }
1988 else if (tp->stop_signal () != GDB_SIGNAL_0)
1989 {
1990 gdb_printf (_("It stopped with signal %s, %s.\n"),
1991 gdb_signal_to_name (tp->stop_signal ()),
1992 gdb_signal_to_string (tp->stop_signal ()));
1993 }
1994
1995 if (from_tty)
1996 {
1997 gdb_printf (_("Type \"info stack\" or \"info "
1998 "registers\" for more information.\n"));
1999 }
2000 }
2001 \f
2002 static void
2003 environment_info (const char *var, int from_tty)
2004 {
2005 if (var)
2006 {
2007 const char *val = current_inferior ()->environment.get (var);
2008
2009 if (val)
2010 {
2011 gdb_puts (var);
2012 gdb_puts (" = ");
2013 gdb_puts (val);
2014 gdb_puts ("\n");
2015 }
2016 else
2017 {
2018 gdb_puts ("Environment variable \"");
2019 gdb_puts (var);
2020 gdb_puts ("\" not defined.\n");
2021 }
2022 }
2023 else
2024 {
2025 char **envp = current_inferior ()->environment.envp ();
2026
2027 for (int idx = 0; envp[idx] != nullptr; ++idx)
2028 {
2029 gdb_puts (envp[idx]);
2030 gdb_puts ("\n");
2031 }
2032 }
2033 }
2034
2035 static void
2036 set_environment_command (const char *arg, int from_tty)
2037 {
2038 const char *p, *val;
2039 int nullset = 0;
2040
2041 if (arg == 0)
2042 error_no_arg (_("environment variable and value"));
2043
2044 /* Find separation between variable name and value. */
2045 p = (char *) strchr (arg, '=');
2046 val = (char *) strchr (arg, ' ');
2047
2048 if (p != 0 && val != 0)
2049 {
2050 /* We have both a space and an equals. If the space is before the
2051 equals, walk forward over the spaces til we see a nonspace
2052 (possibly the equals). */
2053 if (p > val)
2054 while (*val == ' ')
2055 val++;
2056
2057 /* Now if the = is after the char following the spaces,
2058 take the char following the spaces. */
2059 if (p > val)
2060 p = val - 1;
2061 }
2062 else if (val != 0 && p == 0)
2063 p = val;
2064
2065 if (p == arg)
2066 error_no_arg (_("environment variable to set"));
2067
2068 if (p == 0 || p[1] == 0)
2069 {
2070 nullset = 1;
2071 if (p == 0)
2072 p = arg + strlen (arg); /* So that savestring below will work. */
2073 }
2074 else
2075 {
2076 /* Not setting variable value to null. */
2077 val = p + 1;
2078 while (*val == ' ' || *val == '\t')
2079 val++;
2080 }
2081
2082 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
2083 p--;
2084
2085 std::string var (arg, p - arg);
2086 if (nullset)
2087 {
2088 gdb_printf (_("Setting environment variable "
2089 "\"%s\" to null value.\n"),
2090 var.c_str ());
2091 current_inferior ()->environment.set (var.c_str (), "");
2092 }
2093 else
2094 current_inferior ()->environment.set (var.c_str (), val);
2095 }
2096
2097 static void
2098 unset_environment_command (const char *var, int from_tty)
2099 {
2100 if (var == 0)
2101 {
2102 /* If there is no argument, delete all environment variables.
2103 Ask for confirmation if reading from the terminal. */
2104 if (!from_tty || query (_("Delete all environment variables? ")))
2105 current_inferior ()->environment.clear ();
2106 }
2107 else
2108 current_inferior ()->environment.unset (var);
2109 }
2110
2111 /* Handle the execution path (PATH variable). */
2112
2113 static const char path_var_name[] = "PATH";
2114
2115 static void
2116 path_info (const char *args, int from_tty)
2117 {
2118 gdb_puts ("Executable and object file path: ");
2119 gdb_puts (current_inferior ()->environment.get (path_var_name));
2120 gdb_puts ("\n");
2121 }
2122
2123 /* Add zero or more directories to the front of the execution path. */
2124
2125 static void
2126 path_command (const char *dirname, int from_tty)
2127 {
2128 const char *env;
2129
2130 dont_repeat ();
2131 env = current_inferior ()->environment.get (path_var_name);
2132 /* Can be null if path is not set. */
2133 if (!env)
2134 env = "";
2135 std::string exec_path = env;
2136 mod_path (dirname, exec_path);
2137 current_inferior ()->environment.set (path_var_name, exec_path.c_str ());
2138 if (from_tty)
2139 path_info (nullptr, from_tty);
2140 }
2141 \f
2142
2143 static void
2144 pad_to_column (string_file &stream, int col)
2145 {
2146 /* At least one space must be printed to separate columns. */
2147 stream.putc (' ');
2148 const int size = stream.size ();
2149 if (size < col)
2150 stream.puts (n_spaces (col - size));
2151 }
2152
2153 /* Print out the register NAME with value VAL, to FILE, in the default
2154 fashion. */
2155
2156 static void
2157 default_print_one_register_info (struct ui_file *file,
2158 const char *name,
2159 struct value *val)
2160 {
2161 struct type *regtype = value_type (val);
2162 int print_raw_format;
2163 string_file format_stream;
2164 enum tab_stops
2165 {
2166 value_column_1 = 15,
2167 /* Give enough room for "0x", 16 hex digits and two spaces in
2168 preceding column. */
2169 value_column_2 = value_column_1 + 2 + 16 + 2,
2170 };
2171
2172 format_stream.puts (name);
2173 pad_to_column (format_stream, value_column_1);
2174
2175 print_raw_format = (value_entirely_available (val)
2176 && !value_optimized_out (val));
2177
2178 /* If virtual format is floating, print it that way, and in raw
2179 hex. */
2180 if (regtype->code () == TYPE_CODE_FLT
2181 || regtype->code () == TYPE_CODE_DECFLOAT)
2182 {
2183 struct value_print_options opts;
2184 const gdb_byte *valaddr = value_contents_for_printing (val).data ();
2185 enum bfd_endian byte_order = type_byte_order (regtype);
2186
2187 get_user_print_options (&opts);
2188 opts.deref_ref = true;
2189
2190 common_val_print (val, &format_stream, 0, &opts, current_language);
2191
2192 if (print_raw_format)
2193 {
2194 pad_to_column (format_stream, value_column_2);
2195 format_stream.puts ("(raw ");
2196 print_hex_chars (&format_stream, valaddr, regtype->length (),
2197 byte_order, true);
2198 format_stream.putc (')');
2199 }
2200 }
2201 else
2202 {
2203 struct value_print_options opts;
2204
2205 /* Print the register in hex. */
2206 get_formatted_print_options (&opts, 'x');
2207 opts.deref_ref = true;
2208 common_val_print (val, &format_stream, 0, &opts, current_language);
2209 /* If not a vector register, print it also according to its
2210 natural format. */
2211 if (print_raw_format && regtype->is_vector () == 0)
2212 {
2213 pad_to_column (format_stream, value_column_2);
2214 get_user_print_options (&opts);
2215 opts.deref_ref = true;
2216 common_val_print (val, &format_stream, 0, &opts, current_language);
2217 }
2218 }
2219
2220 gdb_puts (format_stream.c_str (), file);
2221 gdb_printf (file, "\n");
2222 }
2223
2224 /* Print out the machine register regnum. If regnum is -1, print all
2225 registers (print_all == 1) or all non-float and non-vector
2226 registers (print_all == 0).
2227
2228 For most machines, having all_registers_info() print the
2229 register(s) one per line is good enough. If a different format is
2230 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2231 regs), or there is an existing convention for showing all the
2232 registers, define the architecture method PRINT_REGISTERS_INFO to
2233 provide that format. */
2234
2235 void
2236 default_print_registers_info (struct gdbarch *gdbarch,
2237 struct ui_file *file,
2238 frame_info_ptr frame,
2239 int regnum, int print_all)
2240 {
2241 int i;
2242 const int numregs = gdbarch_num_cooked_regs (gdbarch);
2243
2244 for (i = 0; i < numregs; i++)
2245 {
2246 /* Decide between printing all regs, non-float / vector regs, or
2247 specific reg. */
2248 if (regnum == -1)
2249 {
2250 if (print_all)
2251 {
2252 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
2253 continue;
2254 }
2255 else
2256 {
2257 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
2258 continue;
2259 }
2260 }
2261 else
2262 {
2263 if (i != regnum)
2264 continue;
2265 }
2266
2267 /* If the register name is empty, it is undefined for this
2268 processor, so don't display anything. */
2269 if (*(gdbarch_register_name (gdbarch, i)) == '\0')
2270 continue;
2271
2272 default_print_one_register_info (file,
2273 gdbarch_register_name (gdbarch, i),
2274 value_of_register (i, frame));
2275 }
2276 }
2277
2278 void
2279 registers_info (const char *addr_exp, int fpregs)
2280 {
2281 frame_info_ptr frame;
2282 struct gdbarch *gdbarch;
2283
2284 if (!target_has_registers ())
2285 error (_("The program has no registers now."));
2286 frame = get_selected_frame (nullptr);
2287 gdbarch = get_frame_arch (frame);
2288
2289 if (!addr_exp)
2290 {
2291 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2292 frame, -1, fpregs);
2293 return;
2294 }
2295
2296 while (*addr_exp != '\0')
2297 {
2298 const char *start;
2299 const char *end;
2300
2301 /* Skip leading white space. */
2302 addr_exp = skip_spaces (addr_exp);
2303
2304 /* Discard any leading ``$''. Check that there is something
2305 resembling a register following it. */
2306 if (addr_exp[0] == '$')
2307 addr_exp++;
2308 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
2309 error (_("Missing register name"));
2310
2311 /* Find the start/end of this register name/num/group. */
2312 start = addr_exp;
2313 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
2314 addr_exp++;
2315 end = addr_exp;
2316
2317 /* Figure out what we've found and display it. */
2318
2319 /* A register name? */
2320 {
2321 int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start);
2322
2323 if (regnum >= 0)
2324 {
2325 /* User registers lie completely outside of the range of
2326 normal registers. Catch them early so that the target
2327 never sees them. */
2328 if (regnum >= gdbarch_num_cooked_regs (gdbarch))
2329 {
2330 struct value *regval = value_of_user_reg (regnum, frame);
2331 const char *regname = user_reg_map_regnum_to_name (gdbarch,
2332 regnum);
2333
2334 /* Print in the same fashion
2335 gdbarch_print_registers_info's default
2336 implementation prints. */
2337 default_print_one_register_info (gdb_stdout,
2338 regname,
2339 regval);
2340 }
2341 else
2342 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2343 frame, regnum, fpregs);
2344 continue;
2345 }
2346 }
2347
2348 /* A register group? */
2349 {
2350 const struct reggroup *group = nullptr;
2351 for (const struct reggroup *g : gdbarch_reggroups (gdbarch))
2352 {
2353 /* Don't bother with a length check. Should the user
2354 enter a short register group name, go with the first
2355 group that matches. */
2356 if (strncmp (start, g->name (), end - start) == 0)
2357 {
2358 group = g;
2359 break;
2360 }
2361 }
2362 if (group != nullptr)
2363 {
2364 int regnum;
2365
2366 for (regnum = 0;
2367 regnum < gdbarch_num_cooked_regs (gdbarch);
2368 regnum++)
2369 {
2370 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
2371 gdbarch_print_registers_info (gdbarch,
2372 gdb_stdout, frame,
2373 regnum, fpregs);
2374 }
2375 continue;
2376 }
2377 }
2378
2379 /* Nothing matched. */
2380 error (_("Invalid register `%.*s'"), (int) (end - start), start);
2381 }
2382 }
2383
2384 static void
2385 info_all_registers_command (const char *addr_exp, int from_tty)
2386 {
2387 registers_info (addr_exp, 1);
2388 }
2389
2390 static void
2391 info_registers_command (const char *addr_exp, int from_tty)
2392 {
2393 registers_info (addr_exp, 0);
2394 }
2395
2396 static void
2397 print_vector_info (struct ui_file *file,
2398 frame_info_ptr frame, const char *args)
2399 {
2400 struct gdbarch *gdbarch = get_frame_arch (frame);
2401
2402 if (gdbarch_print_vector_info_p (gdbarch))
2403 gdbarch_print_vector_info (gdbarch, file, frame, args);
2404 else
2405 {
2406 int regnum;
2407 int printed_something = 0;
2408
2409 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
2410 {
2411 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
2412 {
2413 printed_something = 1;
2414 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2415 }
2416 }
2417 if (!printed_something)
2418 gdb_printf (file, "No vector information\n");
2419 }
2420 }
2421
2422 static void
2423 info_vector_command (const char *args, int from_tty)
2424 {
2425 if (!target_has_registers ())
2426 error (_("The program has no registers now."));
2427
2428 print_vector_info (gdb_stdout, get_selected_frame (nullptr), args);
2429 }
2430 \f
2431 /* Kill the inferior process. Make us have no inferior. */
2432
2433 static void
2434 kill_command (const char *arg, int from_tty)
2435 {
2436 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2437 It should be a distinct flag that indicates that a target is active, cuz
2438 some targets don't have processes! */
2439
2440 if (inferior_ptid == null_ptid)
2441 error (_("The program is not being run."));
2442 if (!query (_("Kill the program being debugged? ")))
2443 error (_("Not confirmed."));
2444
2445 int pid = current_inferior ()->pid;
2446 /* Save the pid as a string before killing the inferior, since that
2447 may unpush the current target, and we need the string after. */
2448 std::string pid_str = target_pid_to_str (ptid_t (pid));
2449 int infnum = current_inferior ()->num;
2450
2451 target_kill ();
2452 bfd_cache_close_all ();
2453
2454 if (print_inferior_events)
2455 gdb_printf (_("[Inferior %d (%s) killed]\n"),
2456 infnum, pid_str.c_str ());
2457 }
2458
2459 /* Used in `attach&' command. Proceed threads of inferior INF iff
2460 they stopped due to debugger request, and when they did, they
2461 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that
2462 have been explicitly been told to stop. */
2463
2464 static void
2465 proceed_after_attach (inferior *inf)
2466 {
2467 /* Don't error out if the current thread is running, because
2468 there may be other stopped threads. */
2469
2470 /* Backup current thread and selected frame. */
2471 scoped_restore_current_thread restore_thread;
2472
2473 for (thread_info *thread : inf->non_exited_threads ())
2474 if (!thread->executing ()
2475 && !thread->stop_requested
2476 && thread->stop_signal () == GDB_SIGNAL_0)
2477 {
2478 switch_to_thread (thread);
2479 clear_proceed_status (0);
2480 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2481 }
2482 }
2483
2484 /* See inferior.h. */
2485
2486 void
2487 setup_inferior (int from_tty)
2488 {
2489 struct inferior *inferior;
2490
2491 inferior = current_inferior ();
2492 inferior->needs_setup = false;
2493
2494 /* If no exec file is yet known, try to determine it from the
2495 process itself. */
2496 if (get_exec_file (0) == nullptr)
2497 exec_file_locate_attach (inferior_ptid.pid (), 1, from_tty);
2498 else
2499 {
2500 reopen_exec_file ();
2501 reread_symbols (from_tty);
2502 }
2503
2504 /* Take any necessary post-attaching actions for this platform. */
2505 target_post_attach (inferior_ptid.pid ());
2506
2507 post_create_inferior (from_tty);
2508 }
2509
2510 /* What to do after the first program stops after attaching. */
2511 enum attach_post_wait_mode
2512 {
2513 /* Do nothing. Leaves threads as they are. */
2514 ATTACH_POST_WAIT_NOTHING,
2515
2516 /* Re-resume threads that are marked running. */
2517 ATTACH_POST_WAIT_RESUME,
2518
2519 /* Stop all threads. */
2520 ATTACH_POST_WAIT_STOP,
2521 };
2522
2523 /* Called after we've attached to a process and we've seen it stop for
2524 the first time. Resume, stop, or don't touch the threads according
2525 to MODE. */
2526
2527 static void
2528 attach_post_wait (int from_tty, enum attach_post_wait_mode mode)
2529 {
2530 struct inferior *inferior;
2531
2532 inferior = current_inferior ();
2533 inferior->control.stop_soon = NO_STOP_QUIETLY;
2534
2535 if (inferior->needs_setup)
2536 setup_inferior (from_tty);
2537
2538 if (mode == ATTACH_POST_WAIT_RESUME)
2539 {
2540 /* The user requested an `attach&', so be sure to leave threads
2541 that didn't get a signal running. */
2542
2543 /* Immediately resume all suspended threads of this inferior,
2544 and this inferior only. This should have no effect on
2545 already running threads. If a thread has been stopped with a
2546 signal, leave it be. */
2547 if (non_stop)
2548 proceed_after_attach (inferior);
2549 else
2550 {
2551 if (inferior_thread ()->stop_signal () == GDB_SIGNAL_0)
2552 {
2553 clear_proceed_status (0);
2554 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2555 }
2556 }
2557 }
2558 else if (mode == ATTACH_POST_WAIT_STOP)
2559 {
2560 /* The user requested a plain `attach', so be sure to leave
2561 the inferior stopped. */
2562
2563 /* At least the current thread is already stopped. */
2564
2565 /* In all-stop, by definition, all threads have to be already
2566 stopped at this point. In non-stop, however, although the
2567 selected thread is stopped, others may still be executing.
2568 Be sure to explicitly stop all threads of the process. This
2569 should have no effect on already stopped threads. */
2570 if (non_stop)
2571 target_stop (ptid_t (inferior->pid));
2572 else if (target_is_non_stop_p ())
2573 {
2574 struct thread_info *lowest = inferior_thread ();
2575
2576 stop_all_threads ("attaching");
2577
2578 /* It's not defined which thread will report the attach
2579 stop. For consistency, always select the thread with
2580 lowest GDB number, which should be the main thread, if it
2581 still exists. */
2582 for (thread_info *thread : current_inferior ()->non_exited_threads ())
2583 if (thread->inf->num < lowest->inf->num
2584 || thread->per_inf_num < lowest->per_inf_num)
2585 lowest = thread;
2586
2587 switch_to_thread (lowest);
2588 }
2589
2590 /* Tell the user/frontend where we're stopped. */
2591 normal_stop ();
2592 if (deprecated_attach_hook)
2593 deprecated_attach_hook ();
2594 }
2595 }
2596
2597 /* "attach" command entry point. Takes a program started up outside
2598 of gdb and ``attaches'' to it. This stops it cold in its tracks
2599 and allows us to start debugging it. */
2600
2601 void
2602 attach_command (const char *args, int from_tty)
2603 {
2604 int async_exec;
2605 struct target_ops *attach_target;
2606 struct inferior *inferior = current_inferior ();
2607 enum attach_post_wait_mode mode;
2608
2609 dont_repeat (); /* Not for the faint of heart */
2610
2611 scoped_disable_commit_resumed disable_commit_resumed ("attaching");
2612
2613 if (gdbarch_has_global_solist (target_gdbarch ()))
2614 /* Don't complain if all processes share the same symbol
2615 space. */
2616 ;
2617 else if (target_has_execution ())
2618 {
2619 if (query (_("A program is being debugged already. Kill it? ")))
2620 target_kill ();
2621 else
2622 error (_("Not killed."));
2623 }
2624
2625 /* Clean up any leftovers from other runs. Some other things from
2626 this function should probably be moved into target_pre_inferior. */
2627 target_pre_inferior (from_tty);
2628
2629 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
2630 args = stripped.get ();
2631
2632 attach_target = find_attach_target ();
2633
2634 prepare_execution_command (attach_target, async_exec);
2635
2636 if (non_stop && !attach_target->supports_non_stop ())
2637 error (_("Cannot attach to this target in non-stop mode"));
2638
2639 attach_target->attach (args, from_tty);
2640 /* to_attach should push the target, so after this point we
2641 shouldn't refer to attach_target again. */
2642 attach_target = nullptr;
2643
2644 infrun_debug_show_threads ("immediately after attach",
2645 current_inferior ()->non_exited_threads ());
2646
2647 /* Enable async mode if it is supported by the target. */
2648 if (target_can_async_p ())
2649 target_async (true);
2650
2651 /* Set up the "saved terminal modes" of the inferior
2652 based on what modes we are starting it with. */
2653 target_terminal::init ();
2654
2655 /* Install inferior's terminal modes. This may look like a no-op,
2656 as we've just saved them above, however, this does more than
2657 restore terminal settings:
2658
2659 - installs a SIGINT handler that forwards SIGINT to the inferior.
2660 Otherwise a Ctrl-C pressed just while waiting for the initial
2661 stop would end up as a spurious Quit.
2662
2663 - removes stdin from the event loop, which we need if attaching
2664 in the foreground, otherwise on targets that report an initial
2665 stop on attach (which are most) we'd process input/commands
2666 while we're in the event loop waiting for that stop. That is,
2667 before the attach continuation runs and the command is really
2668 finished. */
2669 target_terminal::inferior ();
2670
2671 /* Set up execution context to know that we should return from
2672 wait_for_inferior as soon as the target reports a stop. */
2673 init_wait_for_inferior ();
2674
2675 inferior->needs_setup = true;
2676
2677 if (target_is_non_stop_p ())
2678 {
2679 /* If we find that the current thread isn't stopped, explicitly
2680 do so now, because we're going to install breakpoints and
2681 poke at memory. */
2682
2683 if (async_exec)
2684 /* The user requested an `attach&'; stop just one thread. */
2685 target_stop (inferior_ptid);
2686 else
2687 /* The user requested an `attach', so stop all threads of this
2688 inferior. */
2689 target_stop (ptid_t (inferior_ptid.pid ()));
2690 }
2691
2692 /* Check for exec file mismatch, and let the user solve it. */
2693 validate_exec_file (from_tty);
2694
2695 mode = async_exec ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_STOP;
2696
2697 /* Some system don't generate traps when attaching to inferior.
2698 E.g. Mach 3 or GNU hurd. */
2699 if (!target_attach_no_wait ())
2700 {
2701 /* Careful here. See comments in inferior.h. Basically some
2702 OSes don't ignore SIGSTOPs on continue requests anymore. We
2703 need a way for handle_inferior_event to reset the stop_signal
2704 variable after an attach, and this is what
2705 STOP_QUIETLY_NO_SIGSTOP is for. */
2706 inferior->control.stop_soon = STOP_QUIETLY_NO_SIGSTOP;
2707
2708 /* Wait for stop. */
2709 inferior->add_continuation ([=] ()
2710 {
2711 attach_post_wait (from_tty, mode);
2712 });
2713
2714 /* Let infrun consider waiting for events out of this
2715 target. */
2716 inferior->process_target ()->threads_executing = true;
2717
2718 if (!target_is_async_p ())
2719 mark_infrun_async_event_handler ();
2720 return;
2721 }
2722 else
2723 attach_post_wait (from_tty, mode);
2724
2725 disable_commit_resumed.reset_and_commit ();
2726 }
2727
2728 /* We had just found out that the target was already attached to an
2729 inferior. PTID points at a thread of this new inferior, that is
2730 the most likely to be stopped right now, but not necessarily so.
2731 The new inferior is assumed to be already added to the inferior
2732 list at this point. If LEAVE_RUNNING, then leave the threads of
2733 this inferior running, except those we've explicitly seen reported
2734 as stopped. */
2735
2736 void
2737 notice_new_inferior (thread_info *thr, bool leave_running, int from_tty)
2738 {
2739 enum attach_post_wait_mode mode
2740 = leave_running ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_NOTHING;
2741
2742 gdb::optional<scoped_restore_current_thread> restore_thread;
2743
2744 if (inferior_ptid != null_ptid)
2745 restore_thread.emplace ();
2746
2747 /* Avoid reading registers -- we haven't fetched the target
2748 description yet. */
2749 switch_to_thread_no_regs (thr);
2750
2751 /* When we "notice" a new inferior we need to do all the things we
2752 would normally do if we had just attached to it. */
2753
2754 if (thr->executing ())
2755 {
2756 struct inferior *inferior = current_inferior ();
2757
2758 /* We're going to install breakpoints, and poke at memory,
2759 ensure that the inferior is stopped for a moment while we do
2760 that. */
2761 target_stop (inferior_ptid);
2762
2763 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
2764
2765 /* Wait for stop before proceeding. */
2766 inferior->add_continuation ([=] ()
2767 {
2768 attach_post_wait (from_tty, mode);
2769 });
2770
2771 return;
2772 }
2773
2774 attach_post_wait (from_tty, mode);
2775 }
2776
2777 /*
2778 * detach_command --
2779 * takes a program previously attached to and detaches it.
2780 * The program resumes execution and will no longer stop
2781 * on signals, etc. We better not have left any breakpoints
2782 * in the program or it'll die when it hits one. For this
2783 * to work, it may be necessary for the process to have been
2784 * previously attached. It *might* work if the program was
2785 * started via the normal ptrace (PTRACE_TRACEME).
2786 */
2787
2788 void
2789 detach_command (const char *args, int from_tty)
2790 {
2791 dont_repeat (); /* Not for the faint of heart. */
2792
2793 if (inferior_ptid == null_ptid)
2794 error (_("The program is not being run."));
2795
2796 scoped_disable_commit_resumed disable_commit_resumed ("detaching");
2797
2798 query_if_trace_running (from_tty);
2799
2800 disconnect_tracing ();
2801
2802 /* Hold a strong reference to the target while (maybe)
2803 detaching the parent. Otherwise detaching could close the
2804 target. */
2805 auto target_ref
2806 = target_ops_ref::new_reference (current_inferior ()->process_target ());
2807
2808 /* Save this before detaching, since detaching may unpush the
2809 process_stratum target. */
2810 bool was_non_stop_p = target_is_non_stop_p ();
2811
2812 target_detach (current_inferior (), from_tty);
2813
2814 /* The current inferior process was just detached successfully. Get
2815 rid of breakpoints that no longer make sense. Note we don't do
2816 this within target_detach because that is also used when
2817 following child forks, and in that case we will want to transfer
2818 breakpoints to the child, not delete them. */
2819 breakpoint_init_inferior (inf_exited);
2820
2821 /* If the solist is global across inferiors, don't clear it when we
2822 detach from a single inferior. */
2823 if (!gdbarch_has_global_solist (target_gdbarch ()))
2824 no_shared_libraries (nullptr, from_tty);
2825
2826 if (deprecated_detach_hook)
2827 deprecated_detach_hook ();
2828
2829 if (!was_non_stop_p)
2830 restart_after_all_stop_detach (as_process_stratum_target (target_ref.get ()));
2831
2832 disable_commit_resumed.reset_and_commit ();
2833 }
2834
2835 /* Disconnect from the current target without resuming it (leaving it
2836 waiting for a debugger).
2837
2838 We'd better not have left any breakpoints in the program or the
2839 next debugger will get confused. Currently only supported for some
2840 remote targets, since the normal attach mechanisms don't work on
2841 stopped processes on some native platforms (e.g. GNU/Linux). */
2842
2843 static void
2844 disconnect_command (const char *args, int from_tty)
2845 {
2846 dont_repeat (); /* Not for the faint of heart. */
2847 query_if_trace_running (from_tty);
2848 disconnect_tracing ();
2849 target_disconnect (args, from_tty);
2850 no_shared_libraries (nullptr, from_tty);
2851 init_thread_list ();
2852 if (deprecated_detach_hook)
2853 deprecated_detach_hook ();
2854 }
2855
2856 /* Stop PTID in the current target, and tag the PTID threads as having
2857 been explicitly requested to stop. PTID can be a thread, a
2858 process, or minus_one_ptid, meaning all threads of all inferiors of
2859 the current target. */
2860
2861 static void
2862 stop_current_target_threads_ns (ptid_t ptid)
2863 {
2864 target_stop (ptid);
2865
2866 /* Tag the thread as having been explicitly requested to stop, so
2867 other parts of gdb know not to resume this thread automatically,
2868 if it was stopped due to an internal event. Limit this to
2869 non-stop mode, as when debugging a multi-threaded application in
2870 all-stop mode, we will only get one stop event --- it's undefined
2871 which thread will report the event. */
2872 set_stop_requested (current_inferior ()->process_target (),
2873 ptid, 1);
2874 }
2875
2876 /* See inferior.h. */
2877
2878 void
2879 interrupt_target_1 (bool all_threads)
2880 {
2881 scoped_disable_commit_resumed disable_commit_resumed ("interrupting");
2882
2883 if (non_stop)
2884 {
2885 if (all_threads)
2886 {
2887 scoped_restore_current_thread restore_thread;
2888
2889 for (inferior *inf : all_inferiors ())
2890 {
2891 switch_to_inferior_no_thread (inf);
2892 stop_current_target_threads_ns (minus_one_ptid);
2893 }
2894 }
2895 else
2896 stop_current_target_threads_ns (inferior_ptid);
2897 }
2898 else
2899 target_interrupt ();
2900
2901 disable_commit_resumed.reset_and_commit ();
2902 }
2903
2904 /* interrupt [-a]
2905 Stop the execution of the target while running in async mode, in
2906 the background. In all-stop, stop the whole process. In non-stop
2907 mode, stop the current thread only by default, or stop all threads
2908 if the `-a' switch is used. */
2909
2910 static void
2911 interrupt_command (const char *args, int from_tty)
2912 {
2913 if (target_can_async_p ())
2914 {
2915 int all_threads = 0;
2916
2917 dont_repeat (); /* Not for the faint of heart. */
2918
2919 if (args != nullptr
2920 && startswith (args, "-a"))
2921 all_threads = 1;
2922
2923 interrupt_target_1 (all_threads);
2924 }
2925 }
2926
2927 /* See inferior.h. */
2928
2929 void
2930 default_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
2931 frame_info_ptr frame, const char *args)
2932 {
2933 int regnum;
2934 int printed_something = 0;
2935
2936 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
2937 {
2938 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
2939 {
2940 printed_something = 1;
2941 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2942 }
2943 }
2944 if (!printed_something)
2945 gdb_printf (file, "No floating-point info "
2946 "available for this processor.\n");
2947 }
2948
2949 static void
2950 info_float_command (const char *args, int from_tty)
2951 {
2952 frame_info_ptr frame;
2953
2954 if (!target_has_registers ())
2955 error (_("The program has no registers now."));
2956
2957 frame = get_selected_frame (nullptr);
2958 gdbarch_print_float_info (get_frame_arch (frame), gdb_stdout, frame, args);
2959 }
2960 \f
2961 /* Implement `info proc' family of commands. */
2962
2963 static void
2964 info_proc_cmd_1 (const char *args, enum info_proc_what what, int from_tty)
2965 {
2966 struct gdbarch *gdbarch = get_current_arch ();
2967
2968 if (!target_info_proc (args, what))
2969 {
2970 if (gdbarch_info_proc_p (gdbarch))
2971 gdbarch_info_proc (gdbarch, args, what);
2972 else
2973 error (_("Not supported on this target."));
2974 }
2975 }
2976
2977 /* Implement `info proc' when given without any further parameters. */
2978
2979 static void
2980 info_proc_cmd (const char *args, int from_tty)
2981 {
2982 info_proc_cmd_1 (args, IP_MINIMAL, from_tty);
2983 }
2984
2985 /* Implement `info proc mappings'. */
2986
2987 static void
2988 info_proc_cmd_mappings (const char *args, int from_tty)
2989 {
2990 info_proc_cmd_1 (args, IP_MAPPINGS, from_tty);
2991 }
2992
2993 /* Implement `info proc stat'. */
2994
2995 static void
2996 info_proc_cmd_stat (const char *args, int from_tty)
2997 {
2998 info_proc_cmd_1 (args, IP_STAT, from_tty);
2999 }
3000
3001 /* Implement `info proc status'. */
3002
3003 static void
3004 info_proc_cmd_status (const char *args, int from_tty)
3005 {
3006 info_proc_cmd_1 (args, IP_STATUS, from_tty);
3007 }
3008
3009 /* Implement `info proc cwd'. */
3010
3011 static void
3012 info_proc_cmd_cwd (const char *args, int from_tty)
3013 {
3014 info_proc_cmd_1 (args, IP_CWD, from_tty);
3015 }
3016
3017 /* Implement `info proc cmdline'. */
3018
3019 static void
3020 info_proc_cmd_cmdline (const char *args, int from_tty)
3021 {
3022 info_proc_cmd_1 (args, IP_CMDLINE, from_tty);
3023 }
3024
3025 /* Implement `info proc exe'. */
3026
3027 static void
3028 info_proc_cmd_exe (const char *args, int from_tty)
3029 {
3030 info_proc_cmd_1 (args, IP_EXE, from_tty);
3031 }
3032
3033 /* Implement `info proc files'. */
3034
3035 static void
3036 info_proc_cmd_files (const char *args, int from_tty)
3037 {
3038 info_proc_cmd_1 (args, IP_FILES, from_tty);
3039 }
3040
3041 /* Implement `info proc all'. */
3042
3043 static void
3044 info_proc_cmd_all (const char *args, int from_tty)
3045 {
3046 info_proc_cmd_1 (args, IP_ALL, from_tty);
3047 }
3048
3049 /* Implement `show print finish'. */
3050
3051 static void
3052 show_print_finish (struct ui_file *file, int from_tty,
3053 struct cmd_list_element *c,
3054 const char *value)
3055 {
3056 gdb_printf (file, _("\
3057 Printing of return value after `finish' is %s.\n"),
3058 value);
3059 }
3060
3061
3062 /* This help string is used for the run, start, and starti commands.
3063 It is defined as a macro to prevent duplication. */
3064
3065 #define RUN_ARGS_HELP \
3066 "You may specify arguments to give it.\n\
3067 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3068 shell that will start the program (specified by the \"$SHELL\" environment\n\
3069 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3070 are also allowed.\n\
3071 \n\
3072 With no arguments, uses arguments last specified (with \"run\" or \n\
3073 \"set args\"). To cancel previous arguments and run with no arguments,\n\
3074 use \"set args\" without arguments.\n\
3075 \n\
3076 To start the inferior without using a shell, use \"set startup-with-shell off\"."
3077
3078 void _initialize_infcmd ();
3079 void
3080 _initialize_infcmd ()
3081 {
3082 static struct cmd_list_element *info_proc_cmdlist;
3083 struct cmd_list_element *c = nullptr;
3084 const char *cmd_name;
3085
3086 /* Add the filename of the terminal connected to inferior I/O. */
3087 add_setshow_optional_filename_cmd ("inferior-tty", class_run,
3088 &inferior_io_terminal_scratch, _("\
3089 Set terminal for future runs of program being debugged."), _("\
3090 Show terminal for future runs of program being debugged."), _("\
3091 Usage: set inferior-tty [TTY]\n\n\
3092 If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3093 is restored."),
3094 set_inferior_tty_command,
3095 show_inferior_tty_command,
3096 &setlist, &showlist);
3097 cmd_name = "inferior-tty";
3098 c = lookup_cmd (&cmd_name, setlist, "", nullptr, -1, 1);
3099 gdb_assert (c != nullptr);
3100 add_alias_cmd ("tty", c, class_run, 0, &cmdlist);
3101
3102 cmd_name = "args";
3103 add_setshow_string_noescape_cmd (cmd_name, class_run,
3104 &inferior_args_scratch, _("\
3105 Set argument list to give program being debugged when it is started."), _("\
3106 Show argument list to give program being debugged when it is started."), _("\
3107 Follow this command with any number of args, to be passed to the program."),
3108 set_args_command,
3109 show_args_command,
3110 &setlist, &showlist);
3111 c = lookup_cmd (&cmd_name, setlist, "", nullptr, -1, 1);
3112 gdb_assert (c != nullptr);
3113 set_cmd_completer (c, filename_completer);
3114
3115 cmd_name = "cwd";
3116 add_setshow_string_noescape_cmd (cmd_name, class_run,
3117 &inferior_cwd_scratch, _("\
3118 Set the current working directory to be used when the inferior is started.\n\
3119 Changing this setting does not have any effect on inferiors that are\n\
3120 already running."),
3121 _("\
3122 Show the current working directory that is used when the inferior is started."),
3123 _("\
3124 Use this command to change the current working directory that will be used\n\
3125 when the inferior is started. This setting does not affect GDB's current\n\
3126 working directory."),
3127 set_cwd_command,
3128 show_cwd_command,
3129 &setlist, &showlist);
3130 c = lookup_cmd (&cmd_name, setlist, "", nullptr, -1, 1);
3131 gdb_assert (c != nullptr);
3132 set_cmd_completer (c, filename_completer);
3133
3134 c = add_cmd ("environment", no_class, environment_info, _("\
3135 The environment to give the program, or one variable's value.\n\
3136 With an argument VAR, prints the value of environment variable VAR to\n\
3137 give the program being debugged. With no arguments, prints the entire\n\
3138 environment to be given to the program."), &showlist);
3139 set_cmd_completer (c, noop_completer);
3140
3141 add_basic_prefix_cmd ("unset", no_class,
3142 _("Complement to certain \"set\" commands."),
3143 &unsetlist, 0, &cmdlist);
3144
3145 c = add_cmd ("environment", class_run, unset_environment_command, _("\
3146 Cancel environment variable VAR for the program.\n\
3147 This does not affect the program until the next \"run\" command."),
3148 &unsetlist);
3149 set_cmd_completer (c, noop_completer);
3150
3151 c = add_cmd ("environment", class_run, set_environment_command, _("\
3152 Set environment variable value to give the program.\n\
3153 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3154 VALUES of environment variables are uninterpreted strings.\n\
3155 This does not affect the program until the next \"run\" command."),
3156 &setlist);
3157 set_cmd_completer (c, noop_completer);
3158
3159 c = add_com ("path", class_files, path_command, _("\
3160 Add directory DIR(s) to beginning of search path for object files.\n\
3161 $cwd in the path means the current working directory.\n\
3162 This path is equivalent to the $PATH shell variable. It is a list of\n\
3163 directories, separated by colons. These directories are searched to find\n\
3164 fully linked executable files and separately compiled object files as \
3165 needed."));
3166 set_cmd_completer (c, filename_completer);
3167
3168 c = add_cmd ("paths", no_class, path_info, _("\
3169 Current search path for finding 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 &showlist);
3176 set_cmd_completer (c, noop_completer);
3177
3178 add_prefix_cmd ("kill", class_run, kill_command,
3179 _("Kill execution of program being debugged."),
3180 &killlist, 0, &cmdlist);
3181
3182 add_com ("attach", class_run, attach_command, _("\
3183 Attach to a process or file outside of GDB.\n\
3184 This command attaches to another target, of the same type as your last\n\
3185 \"target\" command (\"info files\" will show your target stack).\n\
3186 The command may take as argument a process id or a device file.\n\
3187 For a process id, you must have permission to send the process a signal,\n\
3188 and it must have the same effective uid as the debugger.\n\
3189 When using \"attach\" with a process id, the debugger finds the\n\
3190 program running in the process, looking first in the current working\n\
3191 directory, or (if not found there) using the source file search path\n\
3192 (see the \"directory\" command). You can also use the \"file\" command\n\
3193 to specify the program, and to load its symbol table."));
3194
3195 add_prefix_cmd ("detach", class_run, detach_command, _("\
3196 Detach a process or file previously attached.\n\
3197 If a process, it is no longer traced, and it continues its execution. If\n\
3198 you were debugging a file, the file is closed and gdb no longer accesses it."),
3199 &detachlist, 0, &cmdlist);
3200
3201 add_com ("disconnect", class_run, disconnect_command, _("\
3202 Disconnect from a target.\n\
3203 The target will wait for another debugger to connect. Not available for\n\
3204 all targets."));
3205
3206 c = add_com ("signal", class_run, signal_command, _("\
3207 Continue program with the specified signal.\n\
3208 Usage: signal SIGNAL\n\
3209 The SIGNAL argument is processed the same as the handle command.\n\
3210 \n\
3211 An argument of \"0\" means continue the program without sending it a signal.\n\
3212 This is useful in cases where the program stopped because of a signal,\n\
3213 and you want to resume the program while discarding the signal.\n\
3214 \n\
3215 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3216 the current thread only."));
3217 set_cmd_completer (c, signal_completer);
3218
3219 c = add_com ("queue-signal", class_run, queue_signal_command, _("\
3220 Queue a signal to be delivered to the current thread when it is resumed.\n\
3221 Usage: queue-signal SIGNAL\n\
3222 The SIGNAL argument is processed the same as the handle command.\n\
3223 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3224 \n\
3225 An argument of \"0\" means remove any currently queued signal from\n\
3226 the current thread. This is useful in cases where the program stopped\n\
3227 because of a signal, and you want to resume it while discarding the signal.\n\
3228 \n\
3229 In a multi-threaded program the signal is queued with, or discarded from,\n\
3230 the current thread only."));
3231 set_cmd_completer (c, signal_completer);
3232
3233 cmd_list_element *stepi_cmd
3234 = add_com ("stepi", class_run, stepi_command, _("\
3235 Step one instruction exactly.\n\
3236 Usage: stepi [N]\n\
3237 Argument N means step N times (or till program stops for another \
3238 reason)."));
3239 add_com_alias ("si", stepi_cmd, class_run, 0);
3240
3241 cmd_list_element *nexti_cmd
3242 = add_com ("nexti", class_run, nexti_command, _("\
3243 Step one instruction, but proceed through subroutine calls.\n\
3244 Usage: nexti [N]\n\
3245 Argument N means step N times (or till program stops for another \
3246 reason)."));
3247 add_com_alias ("ni", nexti_cmd, class_run, 0);
3248
3249 cmd_list_element *finish_cmd
3250 = add_com ("finish", class_run, finish_command, _("\
3251 Execute until selected stack frame returns.\n\
3252 Usage: finish\n\
3253 Upon return, the value returned is printed and put in the value history."));
3254 add_com_alias ("fin", finish_cmd, class_run, 1);
3255
3256 cmd_list_element *next_cmd
3257 = add_com ("next", class_run, next_command, _("\
3258 Step program, proceeding through subroutine calls.\n\
3259 Usage: next [N]\n\
3260 Unlike \"step\", if the current source line calls a subroutine,\n\
3261 this command does not enter the subroutine, but instead steps over\n\
3262 the call, in effect treating it as a single source line."));
3263 add_com_alias ("n", next_cmd, class_run, 1);
3264
3265 cmd_list_element *step_cmd
3266 = add_com ("step", class_run, step_command, _("\
3267 Step program until it reaches a different source line.\n\
3268 Usage: step [N]\n\
3269 Argument N means step N times (or till program stops for another \
3270 reason)."));
3271 add_com_alias ("s", step_cmd, class_run, 1);
3272
3273 cmd_list_element *until_cmd
3274 = add_com ("until", class_run, until_command, _("\
3275 Execute until past the current line or past a LOCATION.\n\
3276 Execute until the program reaches a source line greater than the current\n\
3277 or a specified location (same args as break command) within the current \
3278 frame."));
3279 set_cmd_completer (until_cmd, location_completer);
3280 add_com_alias ("u", until_cmd, class_run, 1);
3281
3282 c = add_com ("advance", class_run, advance_command, _("\
3283 Continue the program up to the given location (same form as args for break \
3284 command).\n\
3285 Execution will also stop upon exit from the current stack frame."));
3286 set_cmd_completer (c, location_completer);
3287
3288 cmd_list_element *jump_cmd
3289 = add_com ("jump", class_run, jump_command, _("\
3290 Continue program being debugged at specified line or address.\n\
3291 Usage: jump LOCATION\n\
3292 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3293 for an address to start at."));
3294 set_cmd_completer (jump_cmd, location_completer);
3295 add_com_alias ("j", jump_cmd, class_run, 1);
3296
3297 cmd_list_element *continue_cmd
3298 = add_com ("continue", class_run, continue_command, _("\
3299 Continue program being debugged, after signal or breakpoint.\n\
3300 Usage: continue [N]\n\
3301 If proceeding from breakpoint, a number N may be used as an argument,\n\
3302 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3303 the breakpoint won't break until the Nth time it is reached).\n\
3304 \n\
3305 If non-stop mode is enabled, continue only the current thread,\n\
3306 otherwise all the threads in the program are continued. To \n\
3307 continue all stopped threads in non-stop mode, use the -a option.\n\
3308 Specifying -a and an ignore count simultaneously is an error."));
3309 add_com_alias ("c", continue_cmd, class_run, 1);
3310 add_com_alias ("fg", continue_cmd, class_run, 1);
3311
3312 cmd_list_element *run_cmd
3313 = add_com ("run", class_run, run_command, _("\
3314 Start debugged program.\n"
3315 RUN_ARGS_HELP));
3316 set_cmd_completer (run_cmd, filename_completer);
3317 add_com_alias ("r", run_cmd, class_run, 1);
3318
3319 c = add_com ("start", class_run, start_command, _("\
3320 Start the debugged program stopping at the beginning of the main procedure.\n"
3321 RUN_ARGS_HELP));
3322 set_cmd_completer (c, filename_completer);
3323
3324 c = add_com ("starti", class_run, starti_command, _("\
3325 Start the debugged program stopping at the first instruction.\n"
3326 RUN_ARGS_HELP));
3327 set_cmd_completer (c, filename_completer);
3328
3329 add_com ("interrupt", class_run, interrupt_command,
3330 _("Interrupt the execution of the debugged program.\n\
3331 If non-stop mode is enabled, interrupt only the current thread,\n\
3332 otherwise all the threads in the program are stopped. To \n\
3333 interrupt all running threads in non-stop mode, use the -a option."));
3334
3335 cmd_list_element *info_registers_cmd
3336 = add_info ("registers", info_registers_command, _("\
3337 List of integer registers and their contents, for selected stack frame.\n\
3338 One or more register names as argument means describe the given registers.\n\
3339 One or more register group names as argument means describe the registers\n\
3340 in the named register groups."));
3341 add_info_alias ("r", info_registers_cmd, 1);
3342 set_cmd_completer (info_registers_cmd, reg_or_group_completer);
3343
3344 c = add_info ("all-registers", info_all_registers_command, _("\
3345 List of all registers and their contents, for selected stack frame.\n\
3346 One or more register names as argument means describe the given registers.\n\
3347 One or more register group names as argument means describe the registers\n\
3348 in the named register groups."));
3349 set_cmd_completer (c, reg_or_group_completer);
3350
3351 add_info ("program", info_program_command,
3352 _("Execution status of the program."));
3353
3354 add_info ("float", info_float_command,
3355 _("Print the status of the floating point unit."));
3356
3357 add_info ("vector", info_vector_command,
3358 _("Print the status of the vector unit."));
3359
3360 add_prefix_cmd ("proc", class_info, info_proc_cmd,
3361 _("\
3362 Show additional information about a process.\n\
3363 Specify any process id, or use the program being debugged by default."),
3364 &info_proc_cmdlist,
3365 1/*allow-unknown*/, &infolist);
3366
3367 add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\
3368 List memory regions mapped by the specified process."),
3369 &info_proc_cmdlist);
3370
3371 add_cmd ("stat", class_info, info_proc_cmd_stat, _("\
3372 List process info from /proc/PID/stat."),
3373 &info_proc_cmdlist);
3374
3375 add_cmd ("status", class_info, info_proc_cmd_status, _("\
3376 List process info from /proc/PID/status."),
3377 &info_proc_cmdlist);
3378
3379 add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\
3380 List current working directory of the specified process."),
3381 &info_proc_cmdlist);
3382
3383 add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\
3384 List command line arguments of the specified process."),
3385 &info_proc_cmdlist);
3386
3387 add_cmd ("exe", class_info, info_proc_cmd_exe, _("\
3388 List absolute filename for executable of the specified process."),
3389 &info_proc_cmdlist);
3390
3391 add_cmd ("files", class_info, info_proc_cmd_files, _("\
3392 List files opened by the specified process."),
3393 &info_proc_cmdlist);
3394
3395 add_cmd ("all", class_info, info_proc_cmd_all, _("\
3396 List all available info about the specified process."),
3397 &info_proc_cmdlist);
3398
3399 add_setshow_boolean_cmd ("finish", class_support,
3400 &finish_print, _("\
3401 Set whether `finish' prints the return value."), _("\
3402 Show whether `finish' prints the return value."), nullptr,
3403 nullptr,
3404 show_print_finish,
3405 &setprintlist, &showprintlist);
3406 }