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