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