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