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