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