2008-07-12 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / infcmd.c
CommitLineData
c906108c 1/* Memory-access and commands for "inferior" process, for GDB.
990a07ab 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1
DJ
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
23#include <signal.h>
24#include "gdb_string.h"
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "frame.h"
28#include "inferior.h"
29#include "environ.h"
30#include "value.h"
31#include "gdbcmd.h"
1adeb98a 32#include "symfile.h"
c906108c
SS
33#include "gdbcore.h"
34#include "target.h"
35#include "language.h"
36#include "symfile.h"
37#include "objfiles.h"
c94fdfd0 38#include "completer.h"
8b93c638 39#include "ui-out.h"
6426a772 40#include "event-top.h"
96baa820 41#include "parser-defs.h"
36160dc4 42#include "regcache.h"
f9418c0f 43#include "reggroups.h"
fe898f56 44#include "block.h"
a77053c2 45#include "solib.h"
f9418c0f 46#include <ctype.h>
92ad9cd9 47#include "gdb_assert.h"
281b533b 48#include "observer.h"
424163ea 49#include "target-descriptions.h"
ad842144 50#include "user-regs.h"
32c1e744 51#include "exceptions.h"
700b53b1 52#include "cli/cli-decode.h"
a0ef4274 53#include "gdbthread.h"
72cec141 54
a58dd373 55/* Functions exported for general use, in inferior.h: */
c906108c 56
a14ed312 57void all_registers_info (char *, int);
c906108c 58
a14ed312 59void registers_info (char *, int);
c906108c 60
a58dd373
EZ
61void nexti_command (char *, int);
62
63void stepi_command (char *, int);
c906108c 64
a14ed312 65void continue_command (char *, int);
c906108c 66
a58dd373
EZ
67void interrupt_target_command (char *args, int from_tty);
68
69/* Local functions: */
70
71static void nofp_registers_info (char *, int);
72
c055b101
CV
73static void print_return_value (struct type *func_type,
74 struct type *value_type);
11cf8741 75
f107f563
VP
76static void finish_command_continuation (struct continuation_arg *,
77 int error_p);
43ff13b4 78
a14ed312 79static void until_next_command (int);
c906108c 80
a14ed312 81static void until_command (char *, int);
c906108c 82
a14ed312 83static void path_info (char *, int);
c906108c 84
a14ed312 85static void path_command (char *, int);
c906108c 86
a14ed312 87static void unset_command (char *, int);
c906108c 88
a14ed312 89static void float_info (char *, int);
c906108c 90
a14ed312 91static void detach_command (char *, int);
c906108c 92
6ad8ae5c
DJ
93static void disconnect_command (char *, int);
94
a14ed312 95static void unset_environment_command (char *, int);
c906108c 96
a14ed312 97static void set_environment_command (char *, int);
c906108c 98
a14ed312 99static void environment_info (char *, int);
c906108c 100
a14ed312 101static void program_info (char *, int);
c906108c 102
a14ed312 103static void finish_command (char *, int);
c906108c 104
a14ed312 105static void signal_command (char *, int);
c906108c 106
a14ed312 107static void jump_command (char *, int);
c906108c 108
a14ed312 109static void step_1 (int, int, char *);
611c83ae 110static void step_once (int skip_subroutines, int single_inst, int count, int thread);
f107f563 111static void step_1_continuation (struct continuation_arg *arg, int error_p);
c906108c 112
a14ed312 113static void next_command (char *, int);
c906108c 114
a14ed312 115static void step_command (char *, int);
c906108c 116
a14ed312 117static void run_command (char *, int);
c906108c 118
a14ed312 119static void run_no_args_command (char *args, int from_tty);
392a587b 120
a14ed312 121static void go_command (char *line_no, int from_tty);
392a587b 122
a14ed312 123static int strip_bg_char (char **);
43ff13b4 124
a14ed312 125void _initialize_infcmd (void);
c906108c
SS
126
127#define GO_USAGE "Usage: go <location>\n"
128
c906108c 129#define ERROR_NO_INFERIOR \
8a3fe4f8 130 if (!target_has_execution) error (_("The program is not being run."));
c906108c
SS
131
132/* String containing arguments to give to the program, separated by spaces.
133 Empty string (pointer to '\0') means no args. */
134
135static char *inferior_args;
136
552c04a7
TT
137/* The inferior arguments as a vector. If INFERIOR_ARGC is nonzero,
138 then we must compute INFERIOR_ARGS from this (via the target). */
139
140static int inferior_argc;
141static char **inferior_argv;
142
c906108c
SS
143/* File name for default use for standard in/out in the inferior. */
144
3cb3b8df 145static char *inferior_io_terminal;
c906108c
SS
146
147/* Pid of our debugged inferior, or 0 if no inferior now.
148 Since various parts of infrun.c test this to see whether there is a program
149 being debugged it should be nonzero (currently 3 is used) for remote
150 debugging. */
151
39f77062 152ptid_t inferior_ptid;
c906108c
SS
153
154/* Last signal that the inferior received (why it stopped). */
155
156enum target_signal stop_signal;
157
158/* Address at which inferior stopped. */
159
160CORE_ADDR stop_pc;
161
162/* Chain containing status of breakpoint(s) that we have stopped at. */
163
164bpstat stop_bpstat;
165
166/* Flag indicating that a command has proceeded the inferior past the
167 current breakpoint. */
168
169int breakpoint_proceeded;
170
171/* Nonzero if stopped due to a step command. */
172
173int stop_step;
174
175/* Nonzero if stopped due to completion of a stack dummy routine. */
176
177int stop_stack_dummy;
178
179/* Nonzero if stopped due to a random (unexpected) signal in inferior
180 process. */
181
182int stopped_by_random_signal;
183
184/* Range to single step within.
185 If this is nonzero, respond to a single-step signal
186 by continuing to step if the pc is in this range. */
187
c5aa993b
JM
188CORE_ADDR step_range_start; /* Inclusive */
189CORE_ADDR step_range_end; /* Exclusive */
c906108c
SS
190
191/* Stack frame address as of when stepping command was issued.
192 This is how we know when we step into a subroutine call,
193 and how to set the frame for the breakpoint used to step out. */
194
aa0cd9c1 195struct frame_id step_frame_id;
c906108c 196
5fbbeb29 197enum step_over_calls_kind step_over_calls;
c906108c
SS
198
199/* If stepping, nonzero means step count is > 1
200 so don't print frame next time inferior stops
201 if it stops due to stepping. */
202
203int step_multi;
204
205/* Environment to use for running inferior,
206 in format described in environ.h. */
207
1bf1958d 208struct gdb_environ *inferior_environ;
f5871ec0 209
8f6a8e84
VP
210/* When set, no calls to target_resumed observer will be made. */
211int suppress_resume_observer = 0;
212/* When set, normal_stop will not call the normal_stop observer. */
213int suppress_stop_observer = 0;
c906108c 214\f
07091751
FN
215/* Accessor routines. */
216
3cb3b8df
BR
217void
218set_inferior_io_terminal (const char *terminal_name)
219{
220 if (inferior_io_terminal)
221 xfree (inferior_io_terminal);
222
223 if (!terminal_name)
224 inferior_io_terminal = NULL;
225 else
226 inferior_io_terminal = savestring (terminal_name, strlen (terminal_name));
227}
228
229const char *
230get_inferior_io_terminal (void)
231{
232 return inferior_io_terminal;
233}
234
07091751
FN
235char *
236get_inferior_args (void)
237{
552c04a7
TT
238 if (inferior_argc != 0)
239 {
240 char *n, *old;
241
242 n = gdbarch_construct_inferior_arguments (current_gdbarch,
243 inferior_argc, inferior_argv);
244 old = set_inferior_args (n);
245 xfree (old);
246 }
247
248 if (inferior_args == NULL)
249 inferior_args = xstrdup ("");
250
07091751
FN
251 return inferior_args;
252}
253
254char *
255set_inferior_args (char *newargs)
256{
257 char *saved_args = inferior_args;
258
259 inferior_args = newargs;
552c04a7
TT
260 inferior_argc = 0;
261 inferior_argv = 0;
07091751
FN
262
263 return saved_args;
264}
c5aa993b 265
552c04a7
TT
266void
267set_inferior_args_vector (int argc, char **argv)
268{
269 inferior_argc = argc;
270 inferior_argv = argv;
271}
272
273/* Notice when `set args' is run. */
274static void
275notice_args_set (char *args, int from_tty, struct cmd_list_element *c)
276{
277 inferior_argc = 0;
278 inferior_argv = 0;
279}
280
281/* Notice when `show args' is run. */
282static void
b4b4ac0b
AC
283notice_args_read (struct ui_file *file, int from_tty,
284 struct cmd_list_element *c, const char *value)
552c04a7 285{
258c00cc
TT
286 /* Note that we ignore the passed-in value in favor of computing it
287 directly. */
288 deprecated_show_value_hack (file, from_tty, c, get_inferior_args ());
552c04a7
TT
289}
290
c2a727fa
TT
291\f
292/* Compute command-line string given argument vector. This does the
293 same shell processing as fork_inferior. */
c2a727fa
TT
294char *
295construct_inferior_arguments (struct gdbarch *gdbarch, int argc, char **argv)
296{
297 char *result;
298
299 if (STARTUP_WITH_SHELL)
300 {
301 /* This holds all the characters considered special to the
302 typical Unix shells. We include `^' because the SunOS
303 /bin/sh treats it as a synonym for `|'. */
304 char *special = "\"!#$&*()\\|[]{}<>?'\"`~^; \t\n";
305 int i;
306 int length = 0;
307 char *out, *cp;
308
309 /* We over-compute the size. It shouldn't matter. */
310 for (i = 0; i < argc; ++i)
03c6228e 311 length += 2 * strlen (argv[i]) + 1 + 2 * (argv[i][0] == '\0');
c2a727fa
TT
312
313 result = (char *) xmalloc (length);
314 out = result;
315
316 for (i = 0; i < argc; ++i)
317 {
318 if (i > 0)
319 *out++ = ' ';
320
03c6228e
AS
321 /* Need to handle empty arguments specially. */
322 if (argv[i][0] == '\0')
c2a727fa 323 {
03c6228e
AS
324 *out++ = '\'';
325 *out++ = '\'';
326 }
327 else
328 {
329 for (cp = argv[i]; *cp; ++cp)
330 {
331 if (strchr (special, *cp) != NULL)
332 *out++ = '\\';
333 *out++ = *cp;
334 }
c2a727fa
TT
335 }
336 }
337 *out = '\0';
338 }
339 else
340 {
341 /* In this case we can't handle arguments that contain spaces,
342 tabs, or newlines -- see breakup_args(). */
343 int i;
344 int length = 0;
345
346 for (i = 0; i < argc; ++i)
347 {
348 char *cp = strchr (argv[i], ' ');
349 if (cp == NULL)
350 cp = strchr (argv[i], '\t');
351 if (cp == NULL)
352 cp = strchr (argv[i], '\n');
353 if (cp != NULL)
8a3fe4f8 354 error (_("can't handle command-line argument containing whitespace"));
c2a727fa
TT
355 length += strlen (argv[i]) + 1;
356 }
357
358 result = (char *) xmalloc (length);
359 result[0] = '\0';
360 for (i = 0; i < argc; ++i)
361 {
362 if (i > 0)
363 strcat (result, " ");
364 strcat (result, argv[i]);
365 }
366 }
367
368 return result;
369}
552c04a7
TT
370\f
371
43ff13b4
JM
372/* This function detects whether or not a '&' character (indicating
373 background execution) has been added as *the last* of the arguments ARGS
374 of a command. If it has, it removes it and returns 1. Otherwise it
375 does nothing and returns 0. */
c5aa993b 376static int
fba45db2 377strip_bg_char (char **args)
43ff13b4
JM
378{
379 char *p = NULL;
c5aa993b 380
9846de1b 381 p = strchr (*args, '&');
c5aa993b 382
9846de1b 383 if (p)
43ff13b4
JM
384 {
385 if (p == (*args + strlen (*args) - 1))
386 {
c5aa993b 387 if (strlen (*args) > 1)
43ff13b4
JM
388 {
389 do
390 p--;
391 while (*p == ' ' || *p == '\t');
392 *(p + 1) = '\0';
393 }
394 else
395 *args = 0;
396 return 1;
397 }
398 }
399 return 0;
400}
401
c906108c 402void
fba45db2 403tty_command (char *file, int from_tty)
c906108c
SS
404{
405 if (file == 0)
e2e0b3e5 406 error_no_arg (_("terminal name for running target process"));
c906108c 407
3cb3b8df 408 set_inferior_io_terminal (file);
c906108c
SS
409}
410
281b533b
DJ
411/* Common actions to take after creating any sort of inferior, by any
412 means (running, attaching, connecting, et cetera). The target
413 should be stopped. */
414
415void
416post_create_inferior (struct target_ops *target, int from_tty)
417{
b79599ff
DP
418 /* Be sure we own the terminal in case write operations are performed. */
419 target_terminal_ours ();
420
424163ea
DJ
421 /* If the target hasn't taken care of this already, do it now.
422 Targets which need to access registers during to_open,
423 to_create_inferior, or to_attach should do it earlier; but many
424 don't need to. */
425 target_find_description ();
426
9353355f
DJ
427 if (exec_bfd)
428 {
429 /* Sometimes the platform-specific hook loads initial shared
430 libraries, and sometimes it doesn't. Try to do so first, so
de1b3c3d
PA
431 that we can add them with the correct value for FROM_TTY.
432 If we made all the inferior hook methods consistent,
433 this call could be removed. */
9353355f
DJ
434#ifdef SOLIB_ADD
435 SOLIB_ADD (NULL, from_tty, target, auto_solib_add);
436#else
437 solib_add (NULL, from_tty, target, auto_solib_add);
438#endif
439
440 /* Create the hooks to handle shared library load and unload
441 events. */
442#ifdef SOLIB_CREATE_INFERIOR_HOOK
37706b70 443 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
9353355f
DJ
444#else
445 solib_create_inferior_hook ();
446#endif
9353355f
DJ
447 }
448
281b533b
DJ
449 observer_notify_inferior_created (target, from_tty);
450}
451
a4d5f2e0
JB
452/* Kill the inferior if already running. This function is designed
453 to be called when we are about to start the execution of the program
454 from the beginning. Ask the user to confirm that he wants to restart
455 the program being debugged when FROM_TTY is non-null. */
c906108c 456
8edfe269 457static void
a4d5f2e0
JB
458kill_if_already_running (int from_tty)
459{
39f77062 460 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
c906108c 461 {
8edfe269
DJ
462 /* Bail out before killing the program if we will not be able to
463 restart it. */
464 target_require_runnable ();
465
adf40b2e
JM
466 if (from_tty
467 && !query ("The program being debugged has been started already.\n\
c906108c 468Start it from the beginning? "))
8a3fe4f8 469 error (_("Program not restarted."));
c906108c 470 target_kill ();
e85a822c 471 no_shared_libraries (NULL, from_tty);
c906108c
SS
472 init_wait_for_inferior ();
473 }
a4d5f2e0
JB
474}
475
f67a969f
JB
476/* Implement the "run" command. If TBREAK_AT_MAIN is set, then insert
477 a temporary breakpoint at the begining of the main program before
478 running the program. */
479
a4d5f2e0 480static void
f67a969f 481run_command_1 (char *args, int from_tty, int tbreak_at_main)
a4d5f2e0
JB
482{
483 char *exec_file;
c906108c 484
a4d5f2e0
JB
485 dont_repeat ();
486
487 kill_if_already_running (from_tty);
c906108c
SS
488 clear_breakpoint_hit_counts ();
489
fd79ecee
DJ
490 /* Clean up any leftovers from other runs. Some other things from
491 this function should probably be moved into target_pre_inferior. */
492 target_pre_inferior (from_tty);
493
c906108c
SS
494 /* Purge old solib objfiles. */
495 objfile_purge_solibs ();
496
615b9dba 497 clear_solib ();
c906108c 498
39ad761d
JB
499 /* The comment here used to read, "The exec file is re-read every
500 time we do a generic_mourn_inferior, so we just have to worry
501 about the symbol file." The `generic_mourn_inferior' function
502 gets called whenever the program exits. However, suppose the
503 program exits, and *then* the executable file changes? We need
504 to check again here. Since reopen_exec_file doesn't do anything
505 if the timestamp hasn't changed, I don't see the harm. */
506 reopen_exec_file ();
c906108c
SS
507 reread_symbols ();
508
f67a969f
JB
509 /* Insert the temporary breakpoint if a location was specified. */
510 if (tbreak_at_main)
511 tbreak_command (main_name (), 0);
512
39ad761d
JB
513 exec_file = (char *) get_exec_file (0);
514
c906108c
SS
515 /* We keep symbols from add-symbol-file, on the grounds that the
516 user might want to add some symbols before running the program
517 (right?). But sometimes (dynamic loading where the user manually
518 introduces the new symbols with add-symbol-file), the code which
519 the symbols describe does not persist between runs. Currently
520 the user has to manually nuke all symbols between runs if they
521 want them to go away (PR 2207). This is probably reasonable. */
522
43ff13b4 523 if (!args)
6426a772 524 {
362646f5 525 if (target_can_async_p ())
6426a772
JM
526 async_disable_stdin ();
527 }
43ff13b4 528 else
c906108c 529 {
43ff13b4
JM
530 int async_exec = strip_bg_char (&args);
531
532 /* If we get a request for running in the bg but the target
533 doesn't support it, error out. */
362646f5 534 if (async_exec && !target_can_async_p ())
8a3fe4f8 535 error (_("Asynchronous execution not supported on this target."));
43ff13b4
JM
536
537 /* If we don't get a request of running in the bg, then we need
c5aa993b 538 to simulate synchronous (fg) execution. */
362646f5 539 if (!async_exec && target_can_async_p ())
43ff13b4
JM
540 {
541 /* Simulate synchronous execution */
6426a772 542 async_disable_stdin ();
43ff13b4
JM
543 }
544
545 /* If there were other args, beside '&', process them. */
546 if (args)
547 {
07091751
FN
548 char *old_args = set_inferior_args (xstrdup (args));
549 xfree (old_args);
43ff13b4 550 }
c906108c
SS
551 }
552
553 if (from_tty)
554 {
8b93c638
JM
555 ui_out_field_string (uiout, NULL, "Starting program");
556 ui_out_text (uiout, ": ");
557 if (exec_file)
558 ui_out_field_string (uiout, "execfile", exec_file);
559 ui_out_spaces (uiout, 1);
552c04a7
TT
560 /* We call get_inferior_args() because we might need to compute
561 the value now. */
562 ui_out_field_string (uiout, "infargs", get_inferior_args ());
8b93c638
JM
563 ui_out_text (uiout, "\n");
564 ui_out_flush (uiout);
c906108c
SS
565 }
566
552c04a7
TT
567 /* We call get_inferior_args() because we might need to compute
568 the value now. */
569 target_create_inferior (exec_file, get_inferior_args (),
c27cda74 570 environ_vector (inferior_environ), from_tty);
281b533b 571
de1b3c3d
PA
572 /* Pass zero for FROM_TTY, because at this point the "run" command
573 has done its thing; now we are setting up the running program. */
574 post_create_inferior (&current_target, 0);
281b533b
DJ
575
576 /* Start the target running. */
577 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
c906108c
SS
578}
579
580
f67a969f
JB
581static void
582run_command (char *args, int from_tty)
583{
584 run_command_1 (args, from_tty, 0);
585}
586
c906108c 587static void
fba45db2 588run_no_args_command (char *args, int from_tty)
c906108c 589{
07091751
FN
590 char *old_args = set_inferior_args (xstrdup (""));
591 xfree (old_args);
c906108c 592}
c906108c 593\f
c5aa993b 594
a4d5f2e0
JB
595/* Start the execution of the program up until the beginning of the main
596 program. */
597
598static void
599start_command (char *args, int from_tty)
600{
601 /* Some languages such as Ada need to search inside the program
602 minimal symbols for the location where to put the temporary
603 breakpoint before starting. */
604 if (!have_minimal_symbols ())
8a3fe4f8 605 error (_("No symbol table loaded. Use the \"file\" command."));
a4d5f2e0 606
f67a969f
JB
607 /* Run the program until reaching the main procedure... */
608 run_command_1 (args, from_tty, 1);
a4d5f2e0
JB
609}
610
8cae4b3f
PA
611static int
612proceed_thread_callback (struct thread_info *thread, void *arg)
613{
4f8d22e3 614 if (!is_stopped (thread->ptid))
8cae4b3f
PA
615 return 0;
616
617 context_switch_to (thread->ptid);
618 clear_proceed_status ();
619 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
620 return 0;
621}
622
77ebaa5a
VP
623void
624continue_1 (int all_threads)
625{
626 if (non_stop && all_threads)
627 {
628 /* Don't error out if the current thread is running, because
629 there may be other stopped threads. */
630 struct cleanup *old_chain;
631
632 /* Backup current thread and selected frame. */
633 old_chain = make_cleanup_restore_current_thread ();
634
635 iterate_over_threads (proceed_thread_callback, NULL);
636
637 /* Restore selected ptid. */
638 do_cleanups (old_chain);
639 }
640 else
641 {
642 ensure_not_running ();
643 clear_proceed_status ();
644 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
645 }
646}
647
8cae4b3f 648/* continue [-a] [proceed-count] [&] */
c906108c 649void
8cae4b3f 650continue_command (char *args, int from_tty)
c906108c 651{
c5aa993b 652 int async_exec = 0;
8cae4b3f 653 int all_threads = 0;
c906108c
SS
654 ERROR_NO_INFERIOR;
655
43ff13b4 656 /* Find out whether we must run in the background. */
8cae4b3f
PA
657 if (args != NULL)
658 async_exec = strip_bg_char (&args);
43ff13b4
JM
659
660 /* If we must run in the background, but the target can't do it,
661 error out. */
362646f5 662 if (async_exec && !target_can_async_p ())
8a3fe4f8 663 error (_("Asynchronous execution not supported on this target."));
43ff13b4
JM
664
665 /* If we are not asked to run in the bg, then prepare to run in the
666 foreground, synchronously. */
362646f5 667 if (!async_exec && target_can_async_p ())
c5aa993b 668 {
43ff13b4 669 /* Simulate synchronous execution */
6426a772 670 async_disable_stdin ();
43ff13b4 671 }
c906108c 672
8cae4b3f
PA
673 if (args != NULL)
674 {
675 if (strncmp (args, "-a", sizeof ("-a") - 1) == 0)
676 {
677 all_threads = 1;
678 args += sizeof ("-a") - 1;
679 if (*args == '\0')
680 args = NULL;
681 }
682 }
683
684 if (!non_stop && all_threads)
685 error (_("`-a' is meaningless in all-stop mode."));
686
687 if (args != NULL && all_threads)
688 error (_("\
689Can't resume all threads and specify proceed count simultaneously."));
690
691 /* If we have an argument left, set proceed count of breakpoint we
692 stopped at. */
693 if (args != NULL)
c906108c
SS
694 {
695 bpstat bs = stop_bpstat;
8671a17b
PA
696 int num, stat;
697 int stopped = 0;
698
699 while ((stat = bpstat_num (&bs, &num)) != 0)
700 if (stat > 0)
701 {
702 set_ignore_count (num,
8cae4b3f 703 parse_and_eval_long (args) - 1,
8671a17b
PA
704 from_tty);
705 /* set_ignore_count prints a message ending with a period.
706 So print two spaces before "Continuing.". */
707 if (from_tty)
708 printf_filtered (" ");
709 stopped = 1;
710 }
711
712 if (!stopped && from_tty)
c906108c
SS
713 {
714 printf_filtered
715 ("Not stopped at any breakpoint; argument ignored.\n");
716 }
c906108c
SS
717 }
718
719 if (from_tty)
a3f17187 720 printf_filtered (_("Continuing.\n"));
c906108c 721
77ebaa5a 722 continue_1 (all_threads);
c906108c
SS
723}
724\f
725/* Step until outside of current statement. */
726
c906108c 727static void
fba45db2 728step_command (char *count_string, int from_tty)
c906108c
SS
729{
730 step_1 (0, 0, count_string);
731}
732
733/* Likewise, but skip over subroutine calls as if single instructions. */
734
c906108c 735static void
fba45db2 736next_command (char *count_string, int from_tty)
c906108c
SS
737{
738 step_1 (1, 0, count_string);
739}
740
741/* Likewise, but step only one instruction. */
742
c906108c 743void
fba45db2 744stepi_command (char *count_string, int from_tty)
c906108c
SS
745{
746 step_1 (0, 1, count_string);
747}
748
c906108c 749void
fba45db2 750nexti_command (char *count_string, int from_tty)
c906108c
SS
751{
752 step_1 (1, 1, count_string);
753}
754
74b7792f 755static void
611c83ae 756delete_longjmp_breakpoint_cleanup (void *arg)
74b7792f 757{
611c83ae
PA
758 int thread = * (int *) arg;
759 delete_longjmp_breakpoint (thread);
74b7792f
AC
760}
761
c906108c 762static void
fba45db2 763step_1 (int skip_subroutines, int single_inst, char *count_string)
c906108c 764{
52f0bd74 765 int count = 1;
c906108c 766 struct frame_info *frame;
f107f563 767 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
43ff13b4 768 int async_exec = 0;
b3dc826b 769 int thread = -1;
c5aa993b 770
c906108c 771 ERROR_NO_INFERIOR;
94cc34af 772 ensure_not_running ();
43ff13b4
JM
773
774 if (count_string)
775 async_exec = strip_bg_char (&count_string);
c5aa993b 776
43ff13b4
JM
777 /* If we get a request for running in the bg but the target
778 doesn't support it, error out. */
362646f5 779 if (async_exec && !target_can_async_p ())
8a3fe4f8 780 error (_("Asynchronous execution not supported on this target."));
43ff13b4
JM
781
782 /* If we don't get a request of running in the bg, then we need
783 to simulate synchronous (fg) execution. */
362646f5 784 if (!async_exec && target_can_async_p ())
43ff13b4
JM
785 {
786 /* Simulate synchronous execution */
6426a772 787 async_disable_stdin ();
43ff13b4
JM
788 }
789
bb518678 790 count = count_string ? parse_and_eval_long (count_string) : 1;
c906108c 791
c5aa993b 792 if (!single_inst || skip_subroutines) /* leave si command alone */
c906108c 793 {
611c83ae 794 if (in_thread_list (inferior_ptid))
b3dc826b 795 thread = pid_to_thread_id (inferior_ptid);
611c83ae
PA
796
797 set_longjmp_breakpoint ();
798
b3dc826b 799 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c2d11a7d
JM
800 }
801
802 /* In synchronous case, all is well, just use the regular for loop. */
362646f5 803 if (!target_can_async_p ())
c2d11a7d
JM
804 {
805 for (; count > 0; count--)
806 {
807 clear_proceed_status ();
808
809 frame = get_current_frame ();
810 if (!frame) /* Avoid coredump here. Why tho? */
8a3fe4f8 811 error (_("No current frame"));
aa0cd9c1 812 step_frame_id = get_frame_id (frame);
c2d11a7d
JM
813
814 if (!single_inst)
815 {
816 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
817 if (step_range_end == 0)
818 {
819 char *name;
820 if (find_pc_partial_function (stop_pc, &name, &step_range_start,
821 &step_range_end) == 0)
8a3fe4f8 822 error (_("Cannot find bounds of current function"));
c2d11a7d
JM
823
824 target_terminal_ours ();
a3f17187 825 printf_filtered (_("\
c2d11a7d 826Single stepping until exit from function %s, \n\
a3f17187 827which has no line number information.\n"), name);
c2d11a7d
JM
828 }
829 }
830 else
831 {
832 /* Say we are stepping, but stop after one insn whatever it does. */
833 step_range_start = step_range_end = 1;
834 if (!skip_subroutines)
835 /* It is stepi.
836 Don't step over function calls, not even to functions lacking
837 line numbers. */
5fbbeb29 838 step_over_calls = STEP_OVER_NONE;
c2d11a7d
JM
839 }
840
841 if (skip_subroutines)
5fbbeb29 842 step_over_calls = STEP_OVER_ALL;
c2d11a7d
JM
843
844 step_multi = (count > 1);
845 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
846
847 if (!stop_step)
848 break;
c2d11a7d
JM
849 }
850
f107f563 851 do_cleanups (cleanups);
c2d11a7d
JM
852 return;
853 }
854 /* In case of asynchronous target things get complicated, do only
855 one step for now, before returning control to the event loop. Let
856 the continuation figure out how many other steps we need to do,
857 and handle them one at the time, through step_once(). */
858 else
859 {
b3dc826b 860 step_once (skip_subroutines, single_inst, count, thread);
611c83ae 861 /* We are running, and the continuation is installed. It will
f107f563
VP
862 disable the longjmp breakpoint as appropriate. */
863 discard_cleanups (cleanups);
c906108c 864 }
c2d11a7d 865}
c906108c 866
c2d11a7d
JM
867/* Called after we are done with one step operation, to check whether
868 we need to step again, before we print the prompt and return control
869 to the user. If count is > 1, we will need to do one more call to
870 proceed(), via step_once(). Basically it is like step_once and
871 step_1_continuation are co-recursive. */
872static void
f107f563 873step_1_continuation (struct continuation_arg *arg, int error_p)
c2d11a7d 874{
f13468d9
VP
875 int count;
876 int skip_subroutines;
877 int single_inst;
611c83ae 878 int thread;
f107f563 879
f13468d9
VP
880 skip_subroutines = arg->data.integer;
881 single_inst = arg->next->data.integer;
882 count = arg->next->next->data.integer;
611c83ae 883 thread = arg->next->next->next->data.integer;
f13468d9
VP
884
885 if (error_p || !step_multi || !stop_step)
886 {
887 /* We either hit an error, or stopped for some reason
888 that is not stepping, or there are no further steps
889 to make. Cleanup. */
890 if (!single_inst || skip_subroutines)
611c83ae 891 delete_longjmp_breakpoint (thread);
f13468d9 892 step_multi = 0;
f107f563 893 }
f13468d9 894 else
611c83ae 895 step_once (skip_subroutines, single_inst, count - 1, thread);
c2d11a7d
JM
896}
897
898/* Do just one step operation. If count >1 we will have to set up a
899 continuation to be done after the target stops (after this one
900 step). This is useful to implement the 'step n' kind of commands, in
901 case of asynchronous targets. We had to split step_1 into two parts,
902 one to be done before proceed() and one afterwards. This function is
903 called in case of step n with n>1, after the first step operation has
904 been completed.*/
905static void
611c83ae 906step_once (int skip_subroutines, int single_inst, int count, int thread)
c2d11a7d 907{
0d06e24b
JM
908 struct continuation_arg *arg1;
909 struct continuation_arg *arg2;
910 struct continuation_arg *arg3;
611c83ae 911 struct continuation_arg *arg4;
0d06e24b 912 struct frame_info *frame;
c2d11a7d
JM
913
914 if (count > 0)
c906108c
SS
915 {
916 clear_proceed_status ();
917
918 frame = get_current_frame ();
c5aa993b 919 if (!frame) /* Avoid coredump here. Why tho? */
8a3fe4f8 920 error (_("No current frame"));
aa0cd9c1 921 step_frame_id = get_frame_id (frame);
c906108c 922
c5aa993b 923 if (!single_inst)
c906108c
SS
924 {
925 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
5fbbeb29
CF
926
927 /* If we have no line info, switch to stepi mode. */
928 if (step_range_end == 0 && step_stop_if_no_debug)
929 {
930 step_range_start = step_range_end = 1;
931 }
932 else if (step_range_end == 0)
c906108c
SS
933 {
934 char *name;
935 if (find_pc_partial_function (stop_pc, &name, &step_range_start,
936 &step_range_end) == 0)
8a3fe4f8 937 error (_("Cannot find bounds of current function"));
c906108c
SS
938
939 target_terminal_ours ();
a3f17187 940 printf_filtered (_("\
c906108c 941Single stepping until exit from function %s, \n\
a3f17187 942which has no line number information.\n"), name);
c906108c
SS
943 }
944 }
945 else
946 {
947 /* Say we are stepping, but stop after one insn whatever it does. */
948 step_range_start = step_range_end = 1;
949 if (!skip_subroutines)
950 /* It is stepi.
951 Don't step over function calls, not even to functions lacking
952 line numbers. */
5fbbeb29 953 step_over_calls = STEP_OVER_NONE;
c906108c
SS
954 }
955
956 if (skip_subroutines)
5fbbeb29 957 step_over_calls = STEP_OVER_ALL;
c906108c
SS
958
959 step_multi = (count > 1);
f107f563 960 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
c2d11a7d
JM
961 arg1 =
962 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
963 arg2 =
964 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
965 arg3 =
966 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
611c83ae
PA
967 arg4 =
968 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
c2d11a7d 969 arg1->next = arg2;
57e687d9 970 arg1->data.integer = skip_subroutines;
c2d11a7d 971 arg2->next = arg3;
57e687d9 972 arg2->data.integer = single_inst;
611c83ae 973 arg3->next = arg4;
57e687d9 974 arg3->data.integer = count;
611c83ae
PA
975 arg4->next = NULL;
976 arg4->data.integer = thread;
c2d11a7d 977 add_intermediate_continuation (step_1_continuation, arg1);
c906108c 978 }
c906108c 979}
c2d11a7d 980
c906108c
SS
981\f
982/* Continue program at specified address. */
983
984static void
fba45db2 985jump_command (char *arg, int from_tty)
c906108c 986{
52f0bd74 987 CORE_ADDR addr;
c906108c
SS
988 struct symtabs_and_lines sals;
989 struct symtab_and_line sal;
990 struct symbol *fn;
991 struct symbol *sfn;
43ff13b4 992 int async_exec = 0;
c5aa993b 993
c906108c 994 ERROR_NO_INFERIOR;
94cc34af 995 ensure_not_running ();
c906108c 996
43ff13b4
JM
997 /* Find out whether we must run in the background. */
998 if (arg != NULL)
999 async_exec = strip_bg_char (&arg);
1000
1001 /* If we must run in the background, but the target can't do it,
1002 error out. */
362646f5 1003 if (async_exec && !target_can_async_p ())
8a3fe4f8 1004 error (_("Asynchronous execution not supported on this target."));
43ff13b4 1005
c906108c 1006 if (!arg)
e2e0b3e5 1007 error_no_arg (_("starting address"));
c906108c
SS
1008
1009 sals = decode_line_spec_1 (arg, 1);
1010 if (sals.nelts != 1)
1011 {
8a3fe4f8 1012 error (_("Unreasonable jump request"));
c906108c
SS
1013 }
1014
1015 sal = sals.sals[0];
b8c9b27d 1016 xfree (sals.sals);
c906108c
SS
1017
1018 if (sal.symtab == 0 && sal.pc == 0)
8a3fe4f8 1019 error (_("No source file has been specified."));
c906108c 1020
c5aa993b 1021 resolve_sal_pc (&sal); /* May error out */
c906108c
SS
1022
1023 /* See if we are trying to jump to another function. */
1024 fn = get_frame_function (get_current_frame ());
1025 sfn = find_pc_function (sal.pc);
1026 if (fn != NULL && sfn != fn)
1027 {
1028 if (!query ("Line %d is not in `%s'. Jump anyway? ", sal.line,
de5ad195 1029 SYMBOL_PRINT_NAME (fn)))
c906108c 1030 {
8a3fe4f8 1031 error (_("Not confirmed."));
c906108c
SS
1032 /* NOTREACHED */
1033 }
1034 }
1035
c5aa993b 1036 if (sfn != NULL)
c906108c
SS
1037 {
1038 fixup_symbol_section (sfn, 0);
c5aa993b 1039 if (section_is_overlay (SYMBOL_BFD_SECTION (sfn)) &&
c906108c
SS
1040 !section_is_mapped (SYMBOL_BFD_SECTION (sfn)))
1041 {
1042 if (!query ("WARNING!!! Destination is in unmapped overlay! Jump anyway? "))
1043 {
8a3fe4f8 1044 error (_("Not confirmed."));
c906108c
SS
1045 /* NOTREACHED */
1046 }
1047 }
1048 }
1049
c906108c
SS
1050 addr = sal.pc;
1051
1052 if (from_tty)
1053 {
a3f17187 1054 printf_filtered (_("Continuing at "));
ed49a04f 1055 fputs_filtered (paddress (addr), gdb_stdout);
c906108c
SS
1056 printf_filtered (".\n");
1057 }
1058
89113898
PA
1059 /* If we are not asked to run in the bg, then prepare to run in the
1060 foreground, synchronously. */
1061 if (!async_exec && target_can_async_p ())
1062 {
1063 /* Simulate synchronous execution */
1064 async_disable_stdin ();
1065 }
1066
c906108c
SS
1067 clear_proceed_status ();
1068 proceed (addr, TARGET_SIGNAL_0, 0);
1069}
c906108c 1070\f
c5aa993b 1071
c906108c
SS
1072/* Go to line or address in current procedure */
1073static void
fba45db2 1074go_command (char *line_no, int from_tty)
c906108c 1075{
c5aa993b 1076 if (line_no == (char *) NULL || !*line_no)
b83266a0 1077 printf_filtered (GO_USAGE);
c906108c
SS
1078 else
1079 {
b83266a0
SS
1080 tbreak_command (line_no, from_tty);
1081 jump_command (line_no, from_tty);
c906108c
SS
1082 }
1083}
c906108c 1084\f
c5aa993b 1085
c906108c
SS
1086/* Continue program giving it specified signal. */
1087
1088static void
fba45db2 1089signal_command (char *signum_exp, int from_tty)
c906108c
SS
1090{
1091 enum target_signal oursig;
32c1e744 1092 int async_exec = 0;
c906108c
SS
1093
1094 dont_repeat (); /* Too dangerous. */
1095 ERROR_NO_INFERIOR;
94cc34af 1096 ensure_not_running ();
c906108c 1097
32c1e744
VP
1098 /* Find out whether we must run in the background. */
1099 if (signum_exp != NULL)
1100 async_exec = strip_bg_char (&signum_exp);
1101
1102 /* If we must run in the background, but the target can't do it,
1103 error out. */
1104 if (async_exec && !target_can_async_p ())
1105 error (_("Asynchronous execution not supported on this target."));
1106
1107 /* If we are not asked to run in the bg, then prepare to run in the
1108 foreground, synchronously. */
1109 if (!async_exec && target_can_async_p ())
1110 {
1111 /* Simulate synchronous execution. */
1112 async_disable_stdin ();
1113 }
1114
c906108c 1115 if (!signum_exp)
e2e0b3e5 1116 error_no_arg (_("signal number"));
c906108c
SS
1117
1118 /* It would be even slicker to make signal names be valid expressions,
1119 (the type could be "enum $signal" or some such), then the user could
1120 assign them to convenience variables. */
1121 oursig = target_signal_from_name (signum_exp);
1122
1123 if (oursig == TARGET_SIGNAL_UNKNOWN)
1124 {
1125 /* No, try numeric. */
bb518678 1126 int num = parse_and_eval_long (signum_exp);
c906108c
SS
1127
1128 if (num == 0)
1129 oursig = TARGET_SIGNAL_0;
1130 else
1131 oursig = target_signal_from_command (num);
1132 }
1133
1134 if (from_tty)
1135 {
1136 if (oursig == TARGET_SIGNAL_0)
a3f17187 1137 printf_filtered (_("Continuing with no signal.\n"));
c906108c 1138 else
a3f17187 1139 printf_filtered (_("Continuing with signal %s.\n"),
c906108c
SS
1140 target_signal_to_name (oursig));
1141 }
1142
1143 clear_proceed_status ();
1144 /* "signal 0" should not get stuck if we are stopped at a breakpoint.
1145 FIXME: Neither should "signal foo" but when I tried passing
1146 (CORE_ADDR)-1 unconditionally I got a testsuite failure which I haven't
1147 tried to track down yet. */
2acceee2 1148 proceed (oursig == TARGET_SIGNAL_0 ? (CORE_ADDR) -1 : stop_pc, oursig, 0);
c906108c
SS
1149}
1150
c906108c
SS
1151/* Proceed until we reach a different source line with pc greater than
1152 our current one or exit the function. We skip calls in both cases.
1153
1154 Note that eventually this command should probably be changed so
1155 that only source lines are printed out when we hit the breakpoint
1156 we set. This may involve changes to wait_for_inferior and the
1157 proceed status code. */
1158
c906108c 1159static void
fba45db2 1160until_next_command (int from_tty)
c906108c
SS
1161{
1162 struct frame_info *frame;
1163 CORE_ADDR pc;
1164 struct symbol *func;
1165 struct symtab_and_line sal;
c5aa993b 1166
c906108c
SS
1167 clear_proceed_status ();
1168
1169 frame = get_current_frame ();
1170
1171 /* Step until either exited from this function or greater
1172 than the current line (if in symbolic section) or pc (if
1173 not). */
1174
1175 pc = read_pc ();
1176 func = find_pc_function (pc);
c5aa993b 1177
c906108c
SS
1178 if (!func)
1179 {
1180 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
c5aa993b 1181
c906108c 1182 if (msymbol == NULL)
8a3fe4f8 1183 error (_("Execution is not within a known function."));
c5aa993b 1184
c906108c
SS
1185 step_range_start = SYMBOL_VALUE_ADDRESS (msymbol);
1186 step_range_end = pc;
1187 }
1188 else
1189 {
1190 sal = find_pc_line (pc, 0);
c5aa993b 1191
c906108c
SS
1192 step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
1193 step_range_end = sal.end;
1194 }
c5aa993b 1195
5fbbeb29 1196 step_over_calls = STEP_OVER_ALL;
aa0cd9c1 1197 step_frame_id = get_frame_id (frame);
c906108c
SS
1198
1199 step_multi = 0; /* Only one call to proceed */
c5aa993b 1200
2acceee2 1201 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
c906108c
SS
1202}
1203
c5aa993b 1204static void
fba45db2 1205until_command (char *arg, int from_tty)
c906108c 1206{
43ff13b4
JM
1207 int async_exec = 0;
1208
c906108c 1209 if (!target_has_execution)
8a3fe4f8 1210 error (_("The program is not running."));
43ff13b4
JM
1211
1212 /* Find out whether we must run in the background. */
1213 if (arg != NULL)
1214 async_exec = strip_bg_char (&arg);
1215
1216 /* If we must run in the background, but the target can't do it,
1217 error out. */
362646f5 1218 if (async_exec && !target_can_async_p ())
8a3fe4f8 1219 error (_("Asynchronous execution not supported on this target."));
43ff13b4
JM
1220
1221 /* If we are not asked to run in the bg, then prepare to run in the
1222 foreground, synchronously. */
362646f5 1223 if (!async_exec && target_can_async_p ())
c5aa993b 1224 {
43ff13b4 1225 /* Simulate synchronous execution */
6426a772 1226 async_disable_stdin ();
43ff13b4
JM
1227 }
1228
c906108c 1229 if (arg)
ae66c1fc 1230 until_break_command (arg, from_tty, 0);
c906108c
SS
1231 else
1232 until_next_command (from_tty);
1233}
ae66c1fc
EZ
1234
1235static void
1236advance_command (char *arg, int from_tty)
1237{
1238 int async_exec = 0;
1239
1240 if (!target_has_execution)
8a3fe4f8 1241 error (_("The program is not running."));
ae66c1fc
EZ
1242
1243 if (arg == NULL)
e2e0b3e5 1244 error_no_arg (_("a location"));
ae66c1fc
EZ
1245
1246 /* Find out whether we must run in the background. */
1247 if (arg != NULL)
1248 async_exec = strip_bg_char (&arg);
1249
1250 /* If we must run in the background, but the target can't do it,
1251 error out. */
362646f5 1252 if (async_exec && !target_can_async_p ())
8a3fe4f8 1253 error (_("Asynchronous execution not supported on this target."));
ae66c1fc
EZ
1254
1255 /* If we are not asked to run in the bg, then prepare to run in the
1256 foreground, synchronously. */
362646f5 1257 if (!async_exec && target_can_async_p ())
ae66c1fc
EZ
1258 {
1259 /* Simulate synchronous execution. */
1260 async_disable_stdin ();
1261 }
1262
1263 until_break_command (arg, from_tty, 1);
1264}
c906108c 1265\f
f941662f
MK
1266/* Print the result of a function at the end of a 'finish' command. */
1267
11cf8741 1268static void
c055b101 1269print_return_value (struct type *func_type, struct type *value_type)
11cf8741 1270{
31db7b6c 1271 struct gdbarch *gdbarch = current_gdbarch;
bb472c1e
MK
1272 struct cleanup *old_chain;
1273 struct ui_stream *stb;
44e5158b
AC
1274 struct value *value;
1275
181fc57c 1276 CHECK_TYPEDEF (value_type);
44e5158b 1277 gdb_assert (TYPE_CODE (value_type) != TYPE_CODE_VOID);
11cf8741 1278
92ad9cd9
AC
1279 /* FIXME: 2003-09-27: When returning from a nested inferior function
1280 call, it's possible (with no help from the architecture vector)
1281 to locate and return/print a "struct return" value. This is just
1282 a more complicated case of what is already being done in in the
1283 inferior function call code. In fact, when inferior function
1284 calls are made async, this will likely be made the norm. */
31db7b6c 1285
c055b101
CV
1286 switch (gdbarch_return_value (gdbarch, func_type, value_type,
1287 NULL, NULL, NULL))
44e5158b 1288 {
750eb019
AC
1289 case RETURN_VALUE_REGISTER_CONVENTION:
1290 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
181fc57c 1291 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
44e5158b 1292 value = allocate_value (value_type);
c055b101 1293 gdbarch_return_value (gdbarch, func_type, value_type, stop_registers,
990a07ab 1294 value_contents_raw (value), NULL);
750eb019
AC
1295 break;
1296 case RETURN_VALUE_STRUCT_CONVENTION:
1297 value = NULL;
1298 break;
1299 default:
e2e0b3e5 1300 internal_error (__FILE__, __LINE__, _("bad switch"));
44e5158b 1301 }
bb472c1e 1302
31db7b6c
MK
1303 if (value)
1304 {
1305 /* Print it. */
1d751abe
PA
1306 stb = ui_out_stream_new (uiout);
1307 old_chain = make_cleanup_ui_out_stream_delete (stb);
31db7b6c
MK
1308 ui_out_text (uiout, "Value returned is ");
1309 ui_out_field_fmt (uiout, "gdb-result-var", "$%d",
1310 record_latest_value (value));
1311 ui_out_text (uiout, " = ");
1312 value_print (value, stb->stream, 0, Val_no_prettyprint);
1313 ui_out_field_stream (uiout, "return-value", stb);
1314 ui_out_text (uiout, "\n");
1d751abe 1315 do_cleanups (old_chain);
31db7b6c
MK
1316 }
1317 else
1318 {
1319 ui_out_text (uiout, "Value returned has type: ");
1d751abe
PA
1320 ui_out_field_string (uiout, "return-type", TYPE_NAME (value_type));
1321 ui_out_text (uiout, ".");
1322 ui_out_text (uiout, " Cannot determine contents\n");
31db7b6c 1323 }
11cf8741
JM
1324}
1325
43ff13b4 1326/* Stuff that needs to be done by the finish command after the target
f941662f
MK
1327 has stopped. In asynchronous mode, we wait for the target to stop
1328 in the call to poll or select in the event loop, so it is
1329 impossible to do all the stuff as part of the finish_command
1330 function itself. The only chance we have to complete this command
1331 is in fetch_inferior_event, which is called by the event loop as
1332 soon as it detects that the target has stopped. This function is
1333 called via the cmd_continuation pointer. */
1334
1335static void
f107f563 1336finish_command_continuation (struct continuation_arg *arg, int error_p)
43ff13b4 1337{
52f0bd74 1338 struct symbol *function;
43ff13b4 1339 struct breakpoint *breakpoint;
0d06e24b 1340 struct cleanup *cleanups;
c5aa993b 1341
57e687d9 1342 breakpoint = (struct breakpoint *) arg->data.pointer;
f941662f 1343 function = (struct symbol *) arg->next->data.pointer;
43ff13b4 1344
f107f563 1345 if (!error_p)
43ff13b4 1346 {
f107f563
VP
1347 if (bpstat_find_breakpoint (stop_bpstat, breakpoint) != NULL
1348 && function != NULL)
1349 {
1350 struct type *value_type;
1351
1352 value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
1353 if (!value_type)
1354 internal_error (__FILE__, __LINE__,
1355 _("finish_command: function has no target type"));
1356
1357 if (TYPE_CODE (value_type) != TYPE_CODE_VOID)
1358 print_return_value (SYMBOL_TYPE (function), value_type);
1359 }
f5871ec0
VP
1360
1361 /* We suppress normal call of normal_stop observer and do it here so that
1362 that *stopped notification includes the return value. */
1363 observer_notify_normal_stop (stop_bpstat);
43ff13b4 1364 }
f941662f 1365
8f6a8e84 1366 suppress_stop_observer = 0;
f107f563 1367 delete_breakpoint (breakpoint);
43ff13b4
JM
1368}
1369
f941662f
MK
1370/* "finish": Set a temporary breakpoint at the place the selected
1371 frame will return to, then continue. */
c906108c
SS
1372
1373static void
fba45db2 1374finish_command (char *arg, int from_tty)
c906108c
SS
1375{
1376 struct symtab_and_line sal;
52f0bd74
AC
1377 struct frame_info *frame;
1378 struct symbol *function;
c906108c
SS
1379 struct breakpoint *breakpoint;
1380 struct cleanup *old_chain;
0d06e24b 1381 struct continuation_arg *arg1, *arg2, *arg3;
43ff13b4
JM
1382
1383 int async_exec = 0;
1384
f941662f 1385 /* Find out whether we must run in the background. */
43ff13b4
JM
1386 if (arg != NULL)
1387 async_exec = strip_bg_char (&arg);
1388
1389 /* If we must run in the background, but the target can't do it,
f941662f 1390 error out. */
362646f5 1391 if (async_exec && !target_can_async_p ())
8a3fe4f8 1392 error (_("Asynchronous execution not supported on this target."));
43ff13b4
JM
1393
1394 /* If we are not asked to run in the bg, then prepare to run in the
f941662f 1395 foreground, synchronously. */
362646f5 1396 if (!async_exec && target_can_async_p ())
c5aa993b 1397 {
f941662f 1398 /* Simulate synchronous execution. */
6426a772 1399 async_disable_stdin ();
43ff13b4 1400 }
c906108c
SS
1401
1402 if (arg)
8a3fe4f8 1403 error (_("The \"finish\" command does not take any arguments."));
c906108c 1404 if (!target_has_execution)
8a3fe4f8 1405 error (_("The program is not running."));
c906108c 1406
206415a3 1407 frame = get_prev_frame (get_selected_frame (_("No selected frame.")));
c906108c 1408 if (frame == 0)
8a3fe4f8 1409 error (_("\"finish\" not meaningful in the outermost frame."));
c906108c
SS
1410
1411 clear_proceed_status ();
1412
bdd78e62
AC
1413 sal = find_pc_line (get_frame_pc (frame), 0);
1414 sal.pc = get_frame_pc (frame);
c906108c 1415
818dd999 1416 breakpoint = set_momentary_breakpoint (sal, get_frame_id (frame), bp_finish);
c906108c 1417
f107f563 1418 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c
SS
1419
1420 /* Find the function we will return from. */
1421
206415a3 1422 function = find_pc_function (get_frame_pc (get_selected_frame (NULL)));
c906108c 1423
f941662f
MK
1424 /* Print info on the selected frame, including level number but not
1425 source. */
c906108c
SS
1426 if (from_tty)
1427 {
a3f17187 1428 printf_filtered (_("Run till exit from "));
b04f3ab4 1429 print_stack_frame (get_selected_frame (NULL), 1, LOCATION);
c906108c
SS
1430 }
1431
32c1e744 1432 proceed_to_finish = 1; /* We want stop_registers, please... */
8f6a8e84
VP
1433 make_cleanup_restore_integer (&suppress_stop_observer);
1434 suppress_stop_observer = 1;
32c1e744
VP
1435 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
1436
f107f563
VP
1437 arg1 =
1438 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1439 arg2 =
1440 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
f107f563 1441 arg1->next = arg2;
3d3191a6 1442 arg2->next = NULL;
f107f563
VP
1443 arg1->data.pointer = breakpoint;
1444 arg2->data.pointer = function;
f107f563 1445 add_continuation (finish_command_continuation, arg1);
f7f9a841 1446
f107f563 1447 discard_cleanups (old_chain);
362646f5 1448 if (!target_can_async_p ())
f107f563 1449 do_all_continuations (0);
c906108c
SS
1450}
1451\f
f941662f 1452
c906108c 1453static void
fba45db2 1454program_info (char *args, int from_tty)
c906108c
SS
1455{
1456 bpstat bs = stop_bpstat;
8671a17b
PA
1457 int num;
1458 int stat = bpstat_num (&bs, &num);
c5aa993b 1459
c906108c
SS
1460 if (!target_has_execution)
1461 {
a3f17187 1462 printf_filtered (_("The program being debugged is not being run.\n"));
c906108c
SS
1463 return;
1464 }
1465
1466 target_files_info ();
a3f17187 1467 printf_filtered (_("Program stopped at %s.\n"),
bb599908 1468 hex_string ((unsigned long) stop_pc));
c906108c 1469 if (stop_step)
a3f17187 1470 printf_filtered (_("It stopped after being stepped.\n"));
8671a17b 1471 else if (stat != 0)
c906108c
SS
1472 {
1473 /* There may be several breakpoints in the same place, so this
c5aa993b 1474 isn't as strange as it seems. */
8671a17b 1475 while (stat != 0)
c906108c 1476 {
8671a17b 1477 if (stat < 0)
c906108c 1478 {
a3f17187
AC
1479 printf_filtered (_("\
1480It stopped at a breakpoint that has since been deleted.\n"));
c906108c
SS
1481 }
1482 else
a3f17187 1483 printf_filtered (_("It stopped at breakpoint %d.\n"), num);
8671a17b 1484 stat = bpstat_num (&bs, &num);
c906108c
SS
1485 }
1486 }
1487 else if (stop_signal != TARGET_SIGNAL_0)
1488 {
a3f17187 1489 printf_filtered (_("It stopped with signal %s, %s.\n"),
c906108c
SS
1490 target_signal_to_name (stop_signal),
1491 target_signal_to_string (stop_signal));
1492 }
1493
1494 if (!from_tty)
1495 {
a3f17187
AC
1496 printf_filtered (_("\
1497Type \"info stack\" or \"info registers\" for more information.\n"));
c906108c
SS
1498 }
1499}
1500\f
1501static void
fba45db2 1502environment_info (char *var, int from_tty)
c906108c
SS
1503{
1504 if (var)
1505 {
aa1ee363 1506 char *val = get_in_environ (inferior_environ, var);
c906108c
SS
1507 if (val)
1508 {
1509 puts_filtered (var);
1510 puts_filtered (" = ");
1511 puts_filtered (val);
1512 puts_filtered ("\n");
1513 }
1514 else
1515 {
1516 puts_filtered ("Environment variable \"");
1517 puts_filtered (var);
1518 puts_filtered ("\" not defined.\n");
1519 }
1520 }
1521 else
1522 {
aa1ee363 1523 char **vector = environ_vector (inferior_environ);
c906108c
SS
1524 while (*vector)
1525 {
1526 puts_filtered (*vector++);
1527 puts_filtered ("\n");
1528 }
1529 }
1530}
1531
1532static void
fba45db2 1533set_environment_command (char *arg, int from_tty)
c906108c 1534{
52f0bd74 1535 char *p, *val, *var;
c906108c
SS
1536 int nullset = 0;
1537
1538 if (arg == 0)
e2e0b3e5 1539 error_no_arg (_("environment variable and value"));
c906108c
SS
1540
1541 /* Find seperation between variable name and value */
1542 p = (char *) strchr (arg, '=');
1543 val = (char *) strchr (arg, ' ');
1544
1545 if (p != 0 && val != 0)
1546 {
1547 /* We have both a space and an equals. If the space is before the
c5aa993b
JM
1548 equals, walk forward over the spaces til we see a nonspace
1549 (possibly the equals). */
c906108c
SS
1550 if (p > val)
1551 while (*val == ' ')
1552 val++;
1553
1554 /* Now if the = is after the char following the spaces,
c5aa993b 1555 take the char following the spaces. */
c906108c
SS
1556 if (p > val)
1557 p = val - 1;
1558 }
1559 else if (val != 0 && p == 0)
1560 p = val;
1561
1562 if (p == arg)
e2e0b3e5 1563 error_no_arg (_("environment variable to set"));
c906108c
SS
1564
1565 if (p == 0 || p[1] == 0)
1566 {
1567 nullset = 1;
1568 if (p == 0)
1569 p = arg + strlen (arg); /* So that savestring below will work */
1570 }
1571 else
1572 {
1573 /* Not setting variable value to null */
1574 val = p + 1;
1575 while (*val == ' ' || *val == '\t')
1576 val++;
1577 }
1578
c5aa993b
JM
1579 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
1580 p--;
c906108c
SS
1581
1582 var = savestring (arg, p - arg);
1583 if (nullset)
1584 {
a3f17187
AC
1585 printf_filtered (_("\
1586Setting environment variable \"%s\" to null value.\n"),
1587 var);
c906108c
SS
1588 set_in_environ (inferior_environ, var, "");
1589 }
1590 else
1591 set_in_environ (inferior_environ, var, val);
b8c9b27d 1592 xfree (var);
c906108c
SS
1593}
1594
1595static void
fba45db2 1596unset_environment_command (char *var, int from_tty)
c906108c
SS
1597{
1598 if (var == 0)
1599 {
1600 /* If there is no argument, delete all environment variables.
c5aa993b 1601 Ask for confirmation if reading from the terminal. */
e2e0b3e5 1602 if (!from_tty || query (_("Delete all environment variables? ")))
c906108c
SS
1603 {
1604 free_environ (inferior_environ);
1605 inferior_environ = make_environ ();
1606 }
1607 }
1608 else
1609 unset_in_environ (inferior_environ, var);
1610}
1611
1612/* Handle the execution path (PATH variable) */
1613
1614static const char path_var_name[] = "PATH";
1615
c906108c 1616static void
fba45db2 1617path_info (char *args, int from_tty)
c906108c
SS
1618{
1619 puts_filtered ("Executable and object file path: ");
1620 puts_filtered (get_in_environ (inferior_environ, path_var_name));
1621 puts_filtered ("\n");
1622}
1623
1624/* Add zero or more directories to the front of the execution path. */
1625
1626static void
fba45db2 1627path_command (char *dirname, int from_tty)
c906108c
SS
1628{
1629 char *exec_path;
1630 char *env;
1631 dont_repeat ();
1632 env = get_in_environ (inferior_environ, path_var_name);
1633 /* Can be null if path is not set */
1634 if (!env)
1635 env = "";
4fcf66da 1636 exec_path = xstrdup (env);
c906108c
SS
1637 mod_path (dirname, &exec_path);
1638 set_in_environ (inferior_environ, path_var_name, exec_path);
b8c9b27d 1639 xfree (exec_path);
c906108c 1640 if (from_tty)
c5aa993b 1641 path_info ((char *) NULL, from_tty);
c906108c 1642}
c906108c 1643\f
c5aa993b 1644
4782dc19 1645/* Print out the machine register regnum. If regnum is -1, print all
0ab7a791
AC
1646 registers (print_all == 1) or all non-float and non-vector
1647 registers (print_all == 0).
c906108c
SS
1648
1649 For most machines, having all_registers_info() print the
0ab7a791
AC
1650 register(s) one per line is good enough. If a different format is
1651 required, (eg, for MIPS or Pyramid 90x, which both have lots of
1652 regs), or there is an existing convention for showing all the
1653 registers, define the architecture method PRINT_REGISTERS_INFO to
1654 provide that format. */
c906108c 1655
666e11c5 1656void
0ab7a791
AC
1657default_print_registers_info (struct gdbarch *gdbarch,
1658 struct ui_file *file,
1659 struct frame_info *frame,
1660 int regnum, int print_all)
c906108c 1661{
0ab7a791 1662 int i;
a4bd449d
UW
1663 const int numregs = gdbarch_num_regs (gdbarch)
1664 + gdbarch_num_pseudo_regs (gdbarch);
10c42a71 1665 gdb_byte buffer[MAX_REGISTER_SIZE];
0ab7a791 1666
c906108c
SS
1667 for (i = 0; i < numregs; i++)
1668 {
4782dc19
AC
1669 /* Decide between printing all regs, non-float / vector regs, or
1670 specific reg. */
c5aa993b
JM
1671 if (regnum == -1)
1672 {
f9418c0f 1673 if (print_all)
4782dc19 1674 {
f9418c0f 1675 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
4782dc19 1676 continue;
f9418c0f
AC
1677 }
1678 else
1679 {
1680 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
4782dc19
AC
1681 continue;
1682 }
c5aa993b
JM
1683 }
1684 else
1685 {
1686 if (i != regnum)
1687 continue;
1688 }
c906108c
SS
1689
1690 /* If the register name is empty, it is undefined for this
c5aa993b 1691 processor, so don't display anything. */
a4bd449d
UW
1692 if (gdbarch_register_name (gdbarch, i) == NULL
1693 || *(gdbarch_register_name (gdbarch, i)) == '\0')
c906108c
SS
1694 continue;
1695
a4bd449d 1696 fputs_filtered (gdbarch_register_name (gdbarch, i), file);
c9f4d572 1697 print_spaces_filtered (15 - strlen (gdbarch_register_name
a4bd449d 1698 (gdbarch, i)), file);
c906108c
SS
1699
1700 /* Get the data in raw format. */
9730f241 1701 if (! frame_register_read (frame, i, buffer))
c906108c 1702 {
0ab7a791 1703 fprintf_filtered (file, "*value not available*\n");
c906108c
SS
1704 continue;
1705 }
1706
0ab7a791
AC
1707 /* If virtual format is floating, print it that way, and in raw
1708 hex. */
f949c649
TJB
1709 if (TYPE_CODE (register_type (gdbarch, i)) == TYPE_CODE_FLT
1710 || TYPE_CODE (register_type (gdbarch, i)) == TYPE_CODE_DECFLOAT)
c906108c 1711 {
0ab7a791 1712 int j;
c906108c 1713
a4bd449d 1714 val_print (register_type (gdbarch, i), buffer, 0, 0,
d8ca156b 1715 file, 0, 1, 0, Val_pretty_default, current_language);
c906108c 1716
0ab7a791 1717 fprintf_filtered (file, "\t(raw 0x");
a4bd449d 1718 for (j = 0; j < register_size (gdbarch, i); j++)
c906108c 1719 {
0ab7a791 1720 int idx;
a4bd449d 1721 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
0ab7a791
AC
1722 idx = j;
1723 else
a4bd449d 1724 idx = register_size (gdbarch, i) - 1 - j;
9730f241 1725 fprintf_filtered (file, "%02x", (unsigned char) buffer[idx]);
c906108c 1726 }
0ab7a791 1727 fprintf_filtered (file, ")");
c906108c 1728 }
c906108c
SS
1729 else
1730 {
ab4327e0 1731 /* Print the register in hex. */
a4bd449d 1732 val_print (register_type (gdbarch, i), buffer, 0, 0,
d8ca156b 1733 file, 'x', 1, 0, Val_pretty_default, current_language);
ab4327e0
EZ
1734 /* If not a vector register, print it also according to its
1735 natural format. */
a4bd449d 1736 if (TYPE_VECTOR (register_type (gdbarch, i)) == 0)
ab4327e0 1737 {
9d84ac84 1738 fprintf_filtered (file, "\t");
a4bd449d 1739 val_print (register_type (gdbarch, i), buffer, 0, 0,
d8ca156b 1740 file, 0, 1, 0, Val_pretty_default, current_language);
ab4327e0 1741 }
c906108c
SS
1742 }
1743
0ab7a791 1744 fprintf_filtered (file, "\n");
c906108c
SS
1745 }
1746}
c906108c 1747
c906108c 1748void
fba45db2 1749registers_info (char *addr_exp, int fpregs)
c906108c 1750{
206415a3 1751 struct frame_info *frame;
a4bd449d 1752 struct gdbarch *gdbarch;
c906108c 1753 int regnum, numregs;
52f0bd74 1754 char *end;
c906108c
SS
1755
1756 if (!target_has_registers)
8a3fe4f8 1757 error (_("The program has no registers now."));
206415a3 1758 frame = get_selected_frame (NULL);
a4bd449d 1759 gdbarch = get_frame_arch (frame);
c906108c
SS
1760
1761 if (!addr_exp)
1762 {
a4bd449d 1763 gdbarch_print_registers_info (gdbarch, gdb_stdout,
206415a3 1764 frame, -1, fpregs);
c906108c
SS
1765 return;
1766 }
1767
f9418c0f 1768 while (*addr_exp != '\0')
c5aa993b 1769 {
f9418c0f
AC
1770 char *start;
1771 const char *end;
c906108c 1772
f9418c0f
AC
1773 /* Keep skipping leading white space. */
1774 if (isspace ((*addr_exp)))
1775 {
1776 addr_exp++;
1777 continue;
1778 }
c906108c 1779
f9418c0f
AC
1780 /* Discard any leading ``$''. Check that there is something
1781 resembling a register following it. */
1782 if (addr_exp[0] == '$')
1783 addr_exp++;
1784 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
8a3fe4f8 1785 error (_("Missing register name"));
c906108c 1786
f9418c0f
AC
1787 /* Find the start/end of this register name/num/group. */
1788 start = addr_exp;
1789 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
1790 addr_exp++;
1791 end = addr_exp;
ad842144 1792
f9418c0f
AC
1793 /* Figure out what we've found and display it. */
1794
1795 /* A register name? */
1796 {
ad842144 1797 int regnum = frame_map_name_to_regnum (frame, start, end - start);
f9418c0f
AC
1798 if (regnum >= 0)
1799 {
ad842144
MR
1800 /* User registers lie completely outside of the range of
1801 normal registers. Catch them early so that the target
1802 never sees them. */
1803 if (regnum >= gdbarch_num_regs (gdbarch)
1804 + gdbarch_num_pseudo_regs (gdbarch))
1805 {
1806 struct value *val = value_of_user_reg (regnum, frame);
1807
1808 printf_filtered ("%s: ", start);
1809 print_scalar_formatted (value_contents (val),
1810 check_typedef (value_type (val)),
1811 'x', 0, gdb_stdout);
1812 printf_filtered ("\n");
1813 }
1814 else
1815 gdbarch_print_registers_info (gdbarch, gdb_stdout,
1816 frame, regnum, fpregs);
f9418c0f
AC
1817 continue;
1818 }
1819 }
ad842144 1820
f9418c0f
AC
1821 /* A register number? (how portable is this one?). */
1822 {
1823 char *endptr;
1824 int regnum = strtol (start, &endptr, 0);
1825 if (endptr == end
1826 && regnum >= 0
a4bd449d
UW
1827 && regnum < gdbarch_num_regs (gdbarch)
1828 + gdbarch_num_pseudo_regs (gdbarch))
f9418c0f 1829 {
a4bd449d 1830 gdbarch_print_registers_info (gdbarch, gdb_stdout,
206415a3 1831 frame, regnum, fpregs);
f9418c0f
AC
1832 continue;
1833 }
1834 }
1835
1836 /* A register group? */
1837 {
6c7d17ba 1838 struct reggroup *group;
a4bd449d 1839 for (group = reggroup_next (gdbarch, NULL);
6c7d17ba 1840 group != NULL;
a4bd449d 1841 group = reggroup_next (gdbarch, group))
f9418c0f
AC
1842 {
1843 /* Don't bother with a length check. Should the user
1844 enter a short register group name, go with the first
1845 group that matches. */
6c7d17ba 1846 if (strncmp (start, reggroup_name (group), end - start) == 0)
f9418c0f
AC
1847 break;
1848 }
6c7d17ba 1849 if (group != NULL)
f9418c0f
AC
1850 {
1851 int regnum;
f57d151a 1852 for (regnum = 0;
a4bd449d
UW
1853 regnum < gdbarch_num_regs (gdbarch)
1854 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 1855 regnum++)
f9418c0f 1856 {
a4bd449d
UW
1857 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1858 gdbarch_print_registers_info (gdbarch,
206415a3 1859 gdb_stdout, frame,
f9418c0f
AC
1860 regnum, fpregs);
1861 }
1862 continue;
1863 }
1864 }
c906108c 1865
f9418c0f 1866 /* Nothing matched. */
8a3fe4f8 1867 error (_("Invalid register `%.*s'"), (int) (end - start), start);
c5aa993b 1868 }
c906108c
SS
1869}
1870
1871void
fba45db2 1872all_registers_info (char *addr_exp, int from_tty)
c906108c
SS
1873{
1874 registers_info (addr_exp, 1);
1875}
1876
a58dd373 1877static void
fba45db2 1878nofp_registers_info (char *addr_exp, int from_tty)
c906108c
SS
1879{
1880 registers_info (addr_exp, 0);
1881}
e76f1f2e
AC
1882
1883static void
1884print_vector_info (struct gdbarch *gdbarch, struct ui_file *file,
1885 struct frame_info *frame, const char *args)
1886{
1887 if (gdbarch_print_vector_info_p (gdbarch))
1888 gdbarch_print_vector_info (gdbarch, file, frame, args);
1889 else
1890 {
1891 int regnum;
1892 int printed_something = 0;
ab4327e0 1893
f57d151a 1894 for (regnum = 0;
a4bd449d
UW
1895 regnum < gdbarch_num_regs (gdbarch)
1896 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 1897 regnum++)
e76f1f2e 1898 {
f9418c0f 1899 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
e76f1f2e
AC
1900 {
1901 printed_something = 1;
e76f1f2e 1902 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
e76f1f2e
AC
1903 }
1904 }
1905 if (!printed_something)
1906 fprintf_filtered (file, "No vector information\n");
1907 }
1908}
1909
1910static void
1911vector_info (char *args, int from_tty)
1912{
206415a3
DJ
1913 if (!target_has_registers)
1914 error (_("The program has no registers now."));
1915
1916 print_vector_info (current_gdbarch, gdb_stdout,
1917 get_selected_frame (NULL), args);
e76f1f2e 1918}
c906108c 1919\f
c5aa993b 1920
c906108c
SS
1921/*
1922 * TODO:
1923 * Should save/restore the tty state since it might be that the
1924 * program to be debugged was started on this tty and it wants
1925 * the tty in some state other than what we want. If it's running
1926 * on another terminal or without a terminal, then saving and
1927 * restoring the tty state is a harmless no-op.
1928 * This only needs to be done if we are attaching to a process.
1929 */
1930
1931/*
1932 attach_command --
1933 takes a program started up outside of gdb and ``attaches'' to it.
1934 This stops it cold in its tracks and allows us to start debugging it.
1935 and wait for the trace-trap that results from attaching. */
1936
9356cf8d
PA
1937static void
1938attach_command_post_wait (char *args, int from_tty, int async_exec)
1939{
1940 char *exec_file;
1941 char *full_exec_path = NULL;
1942
1943 stop_soon = NO_STOP_QUIETLY;
1944
1945 /* If no exec file is yet known, try to determine it from the
1946 process itself. */
1947 exec_file = (char *) get_exec_file (0);
1948 if (!exec_file)
1949 {
1950 exec_file = target_pid_to_exec_file (PIDGET (inferior_ptid));
1951 if (exec_file)
1952 {
1953 /* It's possible we don't have a full path, but rather just a
1954 filename. Some targets, such as HP-UX, don't provide the
1955 full path, sigh.
1956
1957 Attempt to qualify the filename against the source path.
1958 (If that fails, we'll just fall back on the original
1959 filename. Not much more we can do...)
1960 */
1961 if (!source_full_path_of (exec_file, &full_exec_path))
1962 full_exec_path = savestring (exec_file, strlen (exec_file));
1963
1964 exec_file_attach (full_exec_path, from_tty);
1965 symbol_file_add_main (full_exec_path, from_tty);
1966 }
1967 }
1968 else
1969 {
1970 reopen_exec_file ();
1971 reread_symbols ();
1972 }
1973
1974 /* Take any necessary post-attaching actions for this platform. */
1975 target_post_attach (PIDGET (inferior_ptid));
1976
1977 post_create_inferior (&current_target, from_tty);
1978
1979 /* Install inferior's terminal modes. */
1980 target_terminal_inferior ();
1981
1982 if (async_exec)
1983 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
1984 else
1985 {
1986 if (target_can_async_p ())
1987 async_enable_stdin ();
1988 normal_stop ();
1989 if (deprecated_attach_hook)
1990 deprecated_attach_hook ();
1991 }
1992}
1993
1994static void
f107f563 1995attach_command_continuation (struct continuation_arg *arg, int error_p)
9356cf8d
PA
1996{
1997 char *args;
1998 int from_tty;
1999 int async_exec;
2000
2001 args = (char *) arg->data.pointer;
2002 from_tty = arg->next->data.integer;
2003 async_exec = arg->next->next->data.integer;
2004
2005 attach_command_post_wait (args, from_tty, async_exec);
2006}
2007
c906108c 2008void
fba45db2 2009attach_command (char *args, int from_tty)
c906108c 2010{
c5aa993b
JM
2011 char *exec_file;
2012 char *full_exec_path = NULL;
9356cf8d 2013 int async_exec = 0;
c906108c 2014
c5aa993b 2015 dont_repeat (); /* Not for the faint of heart */
c906108c
SS
2016
2017 if (target_has_execution)
2018 {
2019 if (query ("A program is being debugged already. Kill it? "))
2020 target_kill ();
2021 else
8a3fe4f8 2022 error (_("Not killed."));
c906108c
SS
2023 }
2024
fd79ecee
DJ
2025 /* Clean up any leftovers from other runs. Some other things from
2026 this function should probably be moved into target_pre_inferior. */
2027 target_pre_inferior (from_tty);
2028
4bcff7eb
CV
2029 /* Clear out solib state. Otherwise the solib state of the previous
2030 inferior might have survived and is entirely wrong for the new
155bd5d1
AC
2031 target. This has been observed on GNU/Linux using glibc 2.3. How
2032 to reproduce:
4bcff7eb
CV
2033
2034 bash$ ./foo&
2035 [1] 4711
2036 bash$ ./foo&
2037 [1] 4712
2038 bash$ gdb ./foo
2039 [...]
2040 (gdb) attach 4711
2041 (gdb) detach
2042 (gdb) attach 4712
2043 Cannot access memory at address 0xdeadbeef
2044 */
7a72d44c 2045 clear_solib ();
4bcff7eb 2046
9356cf8d
PA
2047 if (args)
2048 {
2049 async_exec = strip_bg_char (&args);
2050
2051 /* If we get a request for running in the bg but the target
2052 doesn't support it, error out. */
2053 if (async_exec && !target_can_async_p ())
2054 error (_("Asynchronous execution not supported on this target."));
2055 }
2056
2057 /* If we don't get a request of running in the bg, then we need
2058 to simulate synchronous (fg) execution. */
2059 if (!async_exec && target_can_async_p ())
2060 {
2061 /* Simulate synchronous execution */
2062 async_disable_stdin ();
2063 }
2064
c906108c
SS
2065 target_attach (args, from_tty);
2066
2067 /* Set up the "saved terminal modes" of the inferior
2068 based on what modes we are starting it with. */
2069 target_terminal_init ();
2070
c906108c
SS
2071 /* Set up execution context to know that we should return from
2072 wait_for_inferior as soon as the target reports a stop. */
2073 init_wait_for_inferior ();
2074 clear_proceed_status ();
c906108c 2075
dc177b7a
PA
2076 /* Some system don't generate traps when attaching to inferior.
2077 E.g. Mach 3 or GNU hurd. */
2078 if (!target_attach_no_wait)
c5aa993b 2079 {
dc177b7a
PA
2080 /* Careful here. See comments in inferior.h. Basically some
2081 OSes don't ignore SIGSTOPs on continue requests anymore. We
2082 need a way for handle_inferior_event to reset the stop_signal
2083 variable after an attach, and this is what
2084 STOP_QUIETLY_NO_SIGSTOP is for. */
2085 stop_soon = STOP_QUIETLY_NO_SIGSTOP;
c5aa993b 2086
dc177b7a
PA
2087 if (target_can_async_p ())
2088 {
2089 /* sync_execution mode. Wait for stop. */
2090 struct continuation_arg *arg1, *arg2, *arg3;
2091
2092 arg1 =
2093 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
2094 arg2 =
2095 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
2096 arg3 =
2097 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
2098 arg1->next = arg2;
2099 arg2->next = arg3;
2100 arg3->next = NULL;
2101 arg1->data.pointer = args;
2102 arg2->data.integer = from_tty;
2103 arg3->data.integer = async_exec;
2104 add_continuation (attach_command_continuation, arg1);
2105 return;
2106 }
c906108c 2107
dc177b7a
PA
2108 wait_for_inferior (0);
2109 }
6426a772 2110
9356cf8d 2111 attach_command_post_wait (args, from_tty, async_exec);
c906108c
SS
2112}
2113
2114/*
2115 * detach_command --
2116 * takes a program previously attached to and detaches it.
2117 * The program resumes execution and will no longer stop
2118 * on signals, etc. We better not have left any breakpoints
2119 * in the program or it'll die when it hits one. For this
2120 * to work, it may be necessary for the process to have been
2121 * previously attached. It *might* work if the program was
2122 * started via the normal ptrace (PTRACE_TRACEME).
2123 */
2124
2125static void
fba45db2 2126detach_command (char *args, int from_tty)
c906108c 2127{
1f5d0fc9 2128 dont_repeat (); /* Not for the faint of heart. */
c906108c 2129 target_detach (args, from_tty);
e85a822c 2130 no_shared_libraries (NULL, from_tty);
a0ef4274 2131 init_thread_list ();
9a4105ab
AC
2132 if (deprecated_detach_hook)
2133 deprecated_detach_hook ();
c906108c
SS
2134}
2135
6ad8ae5c
DJ
2136/* Disconnect from the current target without resuming it (leaving it
2137 waiting for a debugger).
2138
2139 We'd better not have left any breakpoints in the program or the
2140 next debugger will get confused. Currently only supported for some
2141 remote targets, since the normal attach mechanisms don't work on
2142 stopped processes on some native platforms (e.g. GNU/Linux). */
2143
2144static void
2145disconnect_command (char *args, int from_tty)
2146{
2147 dont_repeat (); /* Not for the faint of heart */
2148 target_disconnect (args, from_tty);
e85a822c 2149 no_shared_libraries (NULL, from_tty);
a0ef4274 2150 init_thread_list ();
9a4105ab
AC
2151 if (deprecated_detach_hook)
2152 deprecated_detach_hook ();
6ad8ae5c
DJ
2153}
2154
77ebaa5a
VP
2155void
2156interrupt_target_1 (int all_threads)
2157{
2158 ptid_t ptid;
2159 if (all_threads)
2160 ptid = minus_one_ptid;
2161 else
2162 ptid = inferior_ptid;
2163 target_stop (ptid);
2164}
2165
43ff13b4 2166/* Stop the execution of the target while running in async mode, in
8cae4b3f
PA
2167 the backgound. In all-stop, stop the whole process. In non-stop
2168 mode, stop the current thread only by default, or stop all threads
2169 if the `-a' switch is used. */
2170
2171/* interrupt [-a] */
8b93c638 2172void
fba45db2 2173interrupt_target_command (char *args, int from_tty)
43ff13b4 2174{
362646f5 2175 if (target_can_async_p ())
43ff13b4 2176 {
8cae4b3f
PA
2177 int all_threads = 0;
2178
c5aa993b 2179 dont_repeat (); /* Not for the faint of heart */
94cc34af 2180
8cae4b3f
PA
2181 if (args != NULL
2182 && strncmp (args, "-a", sizeof ("-a") - 1) == 0)
2183 all_threads = 1;
2184
2185 if (!non_stop && all_threads)
2186 error (_("-a is meaningless in all-stop mode."));
2187
77ebaa5a 2188 interrupt_target_1 (all_threads);
43ff13b4
JM
2189 }
2190}
2191
c906108c 2192static void
23e3a7ac
AC
2193print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
2194 struct frame_info *frame, const char *args)
c906108c 2195{
23e3a7ac
AC
2196 if (gdbarch_print_float_info_p (gdbarch))
2197 gdbarch_print_float_info (gdbarch, file, frame, args);
2198 else
2199 {
23e3a7ac
AC
2200 int regnum;
2201 int printed_something = 0;
ab4327e0 2202
f57d151a 2203 for (regnum = 0;
a4bd449d
UW
2204 regnum < gdbarch_num_regs (gdbarch)
2205 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 2206 regnum++)
23e3a7ac 2207 {
f9418c0f 2208 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
23e3a7ac
AC
2209 {
2210 printed_something = 1;
23e3a7ac 2211 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
23e3a7ac
AC
2212 }
2213 }
2214 if (!printed_something)
2215 fprintf_filtered (file, "\
2216No floating-point info available for this processor.\n");
23e3a7ac
AC
2217 }
2218}
2219
2220static void
2221float_info (char *args, int from_tty)
2222{
206415a3
DJ
2223 if (!target_has_registers)
2224 error (_("The program has no registers now."));
2225
1f5d0fc9 2226 print_float_info (current_gdbarch, gdb_stdout,
206415a3 2227 get_selected_frame (NULL), args);
c906108c
SS
2228}
2229\f
c906108c 2230static void
fba45db2 2231unset_command (char *args, int from_tty)
c906108c 2232{
a3f17187
AC
2233 printf_filtered (_("\
2234\"unset\" must be followed by the name of an unset subcommand.\n"));
c906108c
SS
2235 help_list (unsetlist, "unset ", -1, gdb_stdout);
2236}
2237
2238void
fba45db2 2239_initialize_infcmd (void)
c906108c 2240{
3cb3b8df
BR
2241 struct cmd_list_element *c = NULL;
2242
2243 /* add the filename of the terminal connected to inferior I/O */
2244 add_setshow_filename_cmd ("inferior-tty", class_run,
2245 &inferior_io_terminal, _("\
2246Set terminal for future runs of program being debugged."), _("\
2247Show terminal for future runs of program being debugged."), _("\
2248Usage: set inferior-tty /dev/pts/1"), NULL, NULL, &setlist, &showlist);
2249 add_com_alias ("tty", "set inferior-tty", class_alias, 0);
c906108c 2250
b4b4ac0b
AC
2251 add_setshow_optional_filename_cmd ("args", class_run,
2252 &inferior_args, _("\
2253Set argument list to give program being debugged when it is started."), _("\
2254Show argument list to give program being debugged when it is started."), _("\
2255Follow this command with any number of args, to be passed to the program."),
2256 notice_args_set,
2257 notice_args_read,
2258 &setlist, &showlist);
c906108c 2259
1a966eab
AC
2260 c = add_cmd ("environment", no_class, environment_info, _("\
2261The environment to give the program, or one variable's value.\n\
c906108c
SS
2262With an argument VAR, prints the value of environment variable VAR to\n\
2263give the program being debugged. With no arguments, prints the entire\n\
1a966eab 2264environment to be given to the program."), &showlist);
5ba2abeb 2265 set_cmd_completer (c, noop_completer);
c906108c
SS
2266
2267 add_prefix_cmd ("unset", no_class, unset_command,
1bedd215 2268 _("Complement to certain \"set\" commands."),
c906108c 2269 &unsetlist, "unset ", 0, &cmdlist);
c5aa993b 2270
1a966eab
AC
2271 c = add_cmd ("environment", class_run, unset_environment_command, _("\
2272Cancel environment variable VAR for the program.\n\
2273This does not affect the program until the next \"run\" command."),
c5aa993b 2274 &unsetlist);
5ba2abeb 2275 set_cmd_completer (c, noop_completer);
c906108c 2276
1a966eab
AC
2277 c = add_cmd ("environment", class_run, set_environment_command, _("\
2278Set environment variable value to give the program.\n\
c906108c
SS
2279Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
2280VALUES of environment variables are uninterpreted strings.\n\
1a966eab 2281This does not affect the program until the next \"run\" command."),
c5aa993b 2282 &setlist);
5ba2abeb 2283 set_cmd_completer (c, noop_completer);
c5aa993b 2284
1bedd215
AC
2285 c = add_com ("path", class_files, path_command, _("\
2286Add directory DIR(s) to beginning of search path for object files.\n\
c906108c
SS
2287$cwd in the path means the current working directory.\n\
2288This path is equivalent to the $PATH shell variable. It is a list of\n\
2289directories, separated by colons. These directories are searched to find\n\
1bedd215 2290fully linked executable files and separately compiled object files as needed."));
5ba2abeb 2291 set_cmd_completer (c, filename_completer);
c906108c 2292
1a966eab
AC
2293 c = add_cmd ("paths", no_class, path_info, _("\
2294Current search path for finding object files.\n\
c906108c
SS
2295$cwd in the path means the current working directory.\n\
2296This path is equivalent to the $PATH shell variable. It is a list of\n\
2297directories, separated by colons. These directories are searched to find\n\
1a966eab 2298fully linked executable files and separately compiled object files as needed."),
c906108c 2299 &showlist);
5ba2abeb 2300 set_cmd_completer (c, noop_completer);
c906108c 2301
1bedd215
AC
2302 add_com ("attach", class_run, attach_command, _("\
2303Attach to a process or file outside of GDB.\n\
c906108c
SS
2304This command attaches to another target, of the same type as your last\n\
2305\"target\" command (\"info files\" will show your target stack).\n\
2306The command may take as argument a process id or a device file.\n\
2307For a process id, you must have permission to send the process a signal,\n\
2308and it must have the same effective uid as the debugger.\n\
2309When using \"attach\" with a process id, the debugger finds the\n\
2310program running in the process, looking first in the current working\n\
2311directory, or (if not found there) using the source file search path\n\
2312(see the \"directory\" command). You can also use the \"file\" command\n\
1bedd215 2313to specify the program, and to load its symbol table."));
c906108c 2314
f73adfeb 2315 add_prefix_cmd ("detach", class_run, detach_command, _("\
1bedd215 2316Detach a process or file previously attached.\n\
c906108c 2317If a process, it is no longer traced, and it continues its execution. If\n\
f73adfeb
AS
2318you were debugging a file, the file is closed and gdb no longer accesses it."),
2319 &detachlist, "detach ", 0, &cmdlist);
c906108c 2320
1bedd215
AC
2321 add_com ("disconnect", class_run, disconnect_command, _("\
2322Disconnect from a target.\n\
6ad8ae5c 2323The target will wait for another debugger to connect. Not available for\n\
1bedd215 2324all targets."));
6ad8ae5c 2325
1bedd215
AC
2326 add_com ("signal", class_run, signal_command, _("\
2327Continue program giving it signal specified by the argument.\n\
2328An argument of \"0\" means continue program without giving it a signal."));
c906108c 2329
1bedd215
AC
2330 add_com ("stepi", class_run, stepi_command, _("\
2331Step one instruction exactly.\n\
2332Argument N means do this N times (or till program stops for another reason)."));
c906108c
SS
2333 add_com_alias ("si", "stepi", class_alias, 0);
2334
1bedd215
AC
2335 add_com ("nexti", class_run, nexti_command, _("\
2336Step one instruction, but proceed through subroutine calls.\n\
2337Argument N means do this N times (or till program stops for another reason)."));
c906108c
SS
2338 add_com_alias ("ni", "nexti", class_alias, 0);
2339
1bedd215
AC
2340 add_com ("finish", class_run, finish_command, _("\
2341Execute until selected stack frame returns.\n\
2342Upon return, the value returned is printed and put in the value history."));
0e479716 2343 add_com_alias ("fin", "finish", class_run, 1);
c906108c 2344
1bedd215
AC
2345 add_com ("next", class_run, next_command, _("\
2346Step program, proceeding through subroutine calls.\n\
c906108c
SS
2347Like the \"step\" command as long as subroutine calls do not happen;\n\
2348when they do, the call is treated as one instruction.\n\
1bedd215 2349Argument N means do this N times (or till program stops for another reason)."));
c906108c
SS
2350 add_com_alias ("n", "next", class_run, 1);
2351 if (xdb_commands)
c5aa993b 2352 add_com_alias ("S", "next", class_run, 1);
c906108c 2353
1bedd215
AC
2354 add_com ("step", class_run, step_command, _("\
2355Step program until it reaches a different source line.\n\
2356Argument N means do this N times (or till program stops for another reason)."));
c906108c
SS
2357 add_com_alias ("s", "step", class_run, 1);
2358
1bedd215
AC
2359 c = add_com ("until", class_run, until_command, _("\
2360Execute until the program reaches a source line greater than the current\n\
2361or a specified location (same args as break command) within the current frame."));
5ba2abeb 2362 set_cmd_completer (c, location_completer);
c906108c 2363 add_com_alias ("u", "until", class_run, 1);
c5aa993b 2364
1bedd215
AC
2365 c = add_com ("advance", class_run, advance_command, _("\
2366Continue the program up to the given location (same form as args for break command).\n\
2367Execution will also stop upon exit from the current stack frame."));
ae66c1fc
EZ
2368 set_cmd_completer (c, location_completer);
2369
1bedd215
AC
2370 c = add_com ("jump", class_run, jump_command, _("\
2371Continue program being debugged at specified line or address.\n\
c906108c 2372Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1bedd215 2373for an address to start at."));
5ba2abeb 2374 set_cmd_completer (c, location_completer);
c906108c 2375
b83266a0 2376 if (xdb_commands)
c94fdfd0 2377 {
1bedd215
AC
2378 c = add_com ("go", class_run, go_command, _("\
2379Usage: go <location>\n\
c906108c
SS
2380Continue program being debugged, stopping at specified line or \n\
2381address.\n\
2382Give as argument either LINENUM or *ADDR, where ADDR is an \n\
2383expression for an address to start at.\n\
1bedd215 2384This command is a combination of tbreak and jump."));
5ba2abeb 2385 set_cmd_completer (c, location_completer);
c94fdfd0 2386 }
b83266a0 2387
c906108c 2388 if (xdb_commands)
c5aa993b 2389 add_com_alias ("g", "go", class_run, 1);
c906108c 2390
8cae4b3f 2391 c = add_com ("continue", class_run, continue_command, _("\
1bedd215 2392Continue program being debugged, after signal or breakpoint.\n\
c906108c
SS
2393If proceeding from breakpoint, a number N may be used as an argument,\n\
2394which means to set the ignore count of that breakpoint to N - 1 (so that\n\
8cae4b3f
PA
2395the breakpoint won't break until the Nth time it is reached).\n\
2396\n\
2397If non-stop mode is enabled, continue only the current thread,\n\
2398otherwise all the threads in the program are continued. To \n\
2399continue all stopped threads in non-stop mode, use the -a option.\n\
2400Specifying -a and an ignore count simultaneously is an error."));
c906108c
SS
2401 add_com_alias ("c", "cont", class_run, 1);
2402 add_com_alias ("fg", "cont", class_run, 1);
8cae4b3f 2403 set_cmd_async_ok (c);
c906108c 2404
1bedd215
AC
2405 c = add_com ("run", class_run, run_command, _("\
2406Start debugged program. You may specify arguments to give it.\n\
c906108c
SS
2407Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
2408Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
2409With no arguments, uses arguments last specified (with \"run\" or \"set args\").\n\
2410To cancel previous arguments and run with no arguments,\n\
1bedd215 2411use \"set args\" without arguments."));
5ba2abeb 2412 set_cmd_completer (c, filename_completer);
c906108c
SS
2413 add_com_alias ("r", "run", class_run, 1);
2414 if (xdb_commands)
2415 add_com ("R", class_run, run_no_args_command,
1bedd215 2416 _("Start debugged program with no arguments."));
c906108c 2417
1bedd215 2418 c = add_com ("start", class_run, start_command, _("\
a4d5f2e0
JB
2419Run the debugged program until the beginning of the main procedure.\n\
2420You may specify arguments to give to your program, just as with the\n\
1bedd215 2421\"run\" command."));
a4d5f2e0
JB
2422 set_cmd_completer (c, filename_completer);
2423
700b53b1 2424 c = add_com ("interrupt", class_run, interrupt_target_command,
8cae4b3f
PA
2425 _("Interrupt the execution of the debugged program.\n\
2426If non-stop mode is enabled, interrupt only the current thread,\n\
2427otherwise all the threads in the program are stopped. To \n\
2428interrupt all running threads in non-stop mode, use the -a option."));
700b53b1 2429 set_cmd_async_ok (c);
43ff13b4 2430
1bedd215
AC
2431 add_info ("registers", nofp_registers_info, _("\
2432List of integer registers and their contents, for selected stack frame.\n\
2433Register name as argument means describe only that register."));
7194c49b 2434 add_info_alias ("r", "registers", 1);
c906108c
SS
2435
2436 if (xdb_commands)
1bedd215
AC
2437 add_com ("lr", class_info, nofp_registers_info, _("\
2438List of integer registers and their contents, for selected stack frame.\n\
2439Register name as argument means describe only that register."));
2440 add_info ("all-registers", all_registers_info, _("\
2441List of all registers and their contents, for selected stack frame.\n\
2442Register name as argument means describe only that register."));
c906108c
SS
2443
2444 add_info ("program", program_info,
1bedd215 2445 _("Execution status of the program."));
c906108c
SS
2446
2447 add_info ("float", float_info,
1bedd215 2448 _("Print the status of the floating point unit\n"));
c906108c 2449
e76f1f2e 2450 add_info ("vector", vector_info,
1bedd215 2451 _("Print the status of the vector unit\n"));
e76f1f2e 2452
c906108c
SS
2453 inferior_environ = make_environ ();
2454 init_environ (inferior_environ);
2455}
This page took 1.815395 seconds and 4 git commands to generate.