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