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