]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/infcmd.c
* symfile.c (reread_symbols): Update objfile's entry point.
[thirdparty/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;
611c83ae 709 int *thread_p = NULL;
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
PA
733 thread_p = xmalloc (sizeof (int));
734 make_cleanup (xfree, thread_p);
735
736 if (in_thread_list (inferior_ptid))
737 *thread_p = pid_to_thread_id (inferior_ptid);
738 else
739 *thread_p = -1;
740
741 set_longjmp_breakpoint ();
742
743 make_cleanup (delete_longjmp_breakpoint_cleanup, thread_p);
c2d11a7d
JM
744 }
745
746 /* In synchronous case, all is well, just use the regular for loop. */
362646f5 747 if (!target_can_async_p ())
c2d11a7d
JM
748 {
749 for (; count > 0; count--)
750 {
751 clear_proceed_status ();
752
753 frame = get_current_frame ();
754 if (!frame) /* Avoid coredump here. Why tho? */
8a3fe4f8 755 error (_("No current frame"));
aa0cd9c1 756 step_frame_id = get_frame_id (frame);
c2d11a7d
JM
757
758 if (!single_inst)
759 {
760 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
761 if (step_range_end == 0)
762 {
763 char *name;
764 if (find_pc_partial_function (stop_pc, &name, &step_range_start,
765 &step_range_end) == 0)
8a3fe4f8 766 error (_("Cannot find bounds of current function"));
c2d11a7d
JM
767
768 target_terminal_ours ();
a3f17187 769 printf_filtered (_("\
c2d11a7d 770Single stepping until exit from function %s, \n\
a3f17187 771which has no line number information.\n"), name);
c2d11a7d
JM
772 }
773 }
774 else
775 {
776 /* Say we are stepping, but stop after one insn whatever it does. */
777 step_range_start = step_range_end = 1;
778 if (!skip_subroutines)
779 /* It is stepi.
780 Don't step over function calls, not even to functions lacking
781 line numbers. */
5fbbeb29 782 step_over_calls = STEP_OVER_NONE;
c2d11a7d
JM
783 }
784
785 if (skip_subroutines)
5fbbeb29 786 step_over_calls = STEP_OVER_ALL;
c2d11a7d
JM
787
788 step_multi = (count > 1);
789 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
790
791 if (!stop_step)
792 break;
c2d11a7d
JM
793 }
794
f107f563 795 do_cleanups (cleanups);
c2d11a7d
JM
796 return;
797 }
798 /* In case of asynchronous target things get complicated, do only
799 one step for now, before returning control to the event loop. Let
800 the continuation figure out how many other steps we need to do,
801 and handle them one at the time, through step_once(). */
802 else
803 {
611c83ae
PA
804 step_once (skip_subroutines, single_inst, count, *thread_p);
805 /* We are running, and the continuation is installed. It will
f107f563
VP
806 disable the longjmp breakpoint as appropriate. */
807 discard_cleanups (cleanups);
611c83ae 808 xfree (thread_p);
c906108c 809 }
c2d11a7d 810}
c906108c 811
c2d11a7d
JM
812/* Called after we are done with one step operation, to check whether
813 we need to step again, before we print the prompt and return control
814 to the user. If count is > 1, we will need to do one more call to
815 proceed(), via step_once(). Basically it is like step_once and
816 step_1_continuation are co-recursive. */
817static void
f107f563 818step_1_continuation (struct continuation_arg *arg, int error_p)
c2d11a7d 819{
f13468d9
VP
820 int count;
821 int skip_subroutines;
822 int single_inst;
611c83ae 823 int thread;
f107f563 824
f13468d9
VP
825 skip_subroutines = arg->data.integer;
826 single_inst = arg->next->data.integer;
827 count = arg->next->next->data.integer;
611c83ae 828 thread = arg->next->next->next->data.integer;
f13468d9
VP
829
830 if (error_p || !step_multi || !stop_step)
831 {
832 /* We either hit an error, or stopped for some reason
833 that is not stepping, or there are no further steps
834 to make. Cleanup. */
835 if (!single_inst || skip_subroutines)
611c83ae 836 delete_longjmp_breakpoint (thread);
f13468d9 837 step_multi = 0;
f107f563 838 }
f13468d9 839 else
611c83ae 840 step_once (skip_subroutines, single_inst, count - 1, thread);
c2d11a7d
JM
841}
842
843/* Do just one step operation. If count >1 we will have to set up a
844 continuation to be done after the target stops (after this one
845 step). This is useful to implement the 'step n' kind of commands, in
846 case of asynchronous targets. We had to split step_1 into two parts,
847 one to be done before proceed() and one afterwards. This function is
848 called in case of step n with n>1, after the first step operation has
849 been completed.*/
850static void
611c83ae 851step_once (int skip_subroutines, int single_inst, int count, int thread)
c2d11a7d 852{
0d06e24b
JM
853 struct continuation_arg *arg1;
854 struct continuation_arg *arg2;
855 struct continuation_arg *arg3;
611c83ae 856 struct continuation_arg *arg4;
0d06e24b 857 struct frame_info *frame;
c2d11a7d
JM
858
859 if (count > 0)
c906108c
SS
860 {
861 clear_proceed_status ();
862
863 frame = get_current_frame ();
c5aa993b 864 if (!frame) /* Avoid coredump here. Why tho? */
8a3fe4f8 865 error (_("No current frame"));
aa0cd9c1 866 step_frame_id = get_frame_id (frame);
c906108c 867
c5aa993b 868 if (!single_inst)
c906108c
SS
869 {
870 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
5fbbeb29
CF
871
872 /* If we have no line info, switch to stepi mode. */
873 if (step_range_end == 0 && step_stop_if_no_debug)
874 {
875 step_range_start = step_range_end = 1;
876 }
877 else if (step_range_end == 0)
c906108c
SS
878 {
879 char *name;
880 if (find_pc_partial_function (stop_pc, &name, &step_range_start,
881 &step_range_end) == 0)
8a3fe4f8 882 error (_("Cannot find bounds of current function"));
c906108c
SS
883
884 target_terminal_ours ();
a3f17187 885 printf_filtered (_("\
c906108c 886Single stepping until exit from function %s, \n\
a3f17187 887which has no line number information.\n"), name);
c906108c
SS
888 }
889 }
890 else
891 {
892 /* Say we are stepping, but stop after one insn whatever it does. */
893 step_range_start = step_range_end = 1;
894 if (!skip_subroutines)
895 /* It is stepi.
896 Don't step over function calls, not even to functions lacking
897 line numbers. */
5fbbeb29 898 step_over_calls = STEP_OVER_NONE;
c906108c
SS
899 }
900
901 if (skip_subroutines)
5fbbeb29 902 step_over_calls = STEP_OVER_ALL;
c906108c
SS
903
904 step_multi = (count > 1);
f107f563 905 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
c2d11a7d
JM
906 arg1 =
907 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
908 arg2 =
909 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
910 arg3 =
911 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
611c83ae
PA
912 arg4 =
913 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
c2d11a7d 914 arg1->next = arg2;
57e687d9 915 arg1->data.integer = skip_subroutines;
c2d11a7d 916 arg2->next = arg3;
57e687d9 917 arg2->data.integer = single_inst;
611c83ae 918 arg3->next = arg4;
57e687d9 919 arg3->data.integer = count;
611c83ae
PA
920 arg4->next = NULL;
921 arg4->data.integer = thread;
c2d11a7d 922 add_intermediate_continuation (step_1_continuation, arg1);
c906108c 923 }
c906108c 924}
c2d11a7d 925
c906108c
SS
926\f
927/* Continue program at specified address. */
928
929static void
fba45db2 930jump_command (char *arg, int from_tty)
c906108c 931{
52f0bd74 932 CORE_ADDR addr;
c906108c
SS
933 struct symtabs_and_lines sals;
934 struct symtab_and_line sal;
935 struct symbol *fn;
936 struct symbol *sfn;
43ff13b4 937 int async_exec = 0;
c5aa993b 938
c906108c
SS
939 ERROR_NO_INFERIOR;
940
43ff13b4
JM
941 /* Find out whether we must run in the background. */
942 if (arg != NULL)
943 async_exec = strip_bg_char (&arg);
944
945 /* If we must run in the background, but the target can't do it,
946 error out. */
362646f5 947 if (async_exec && !target_can_async_p ())
8a3fe4f8 948 error (_("Asynchronous execution not supported on this target."));
43ff13b4 949
c906108c 950 if (!arg)
e2e0b3e5 951 error_no_arg (_("starting address"));
c906108c
SS
952
953 sals = decode_line_spec_1 (arg, 1);
954 if (sals.nelts != 1)
955 {
8a3fe4f8 956 error (_("Unreasonable jump request"));
c906108c
SS
957 }
958
959 sal = sals.sals[0];
b8c9b27d 960 xfree (sals.sals);
c906108c
SS
961
962 if (sal.symtab == 0 && sal.pc == 0)
8a3fe4f8 963 error (_("No source file has been specified."));
c906108c 964
c5aa993b 965 resolve_sal_pc (&sal); /* May error out */
c906108c
SS
966
967 /* See if we are trying to jump to another function. */
968 fn = get_frame_function (get_current_frame ());
969 sfn = find_pc_function (sal.pc);
970 if (fn != NULL && sfn != fn)
971 {
972 if (!query ("Line %d is not in `%s'. Jump anyway? ", sal.line,
de5ad195 973 SYMBOL_PRINT_NAME (fn)))
c906108c 974 {
8a3fe4f8 975 error (_("Not confirmed."));
c906108c
SS
976 /* NOTREACHED */
977 }
978 }
979
c5aa993b 980 if (sfn != NULL)
c906108c
SS
981 {
982 fixup_symbol_section (sfn, 0);
c5aa993b 983 if (section_is_overlay (SYMBOL_BFD_SECTION (sfn)) &&
c906108c
SS
984 !section_is_mapped (SYMBOL_BFD_SECTION (sfn)))
985 {
986 if (!query ("WARNING!!! Destination is in unmapped overlay! Jump anyway? "))
987 {
8a3fe4f8 988 error (_("Not confirmed."));
c906108c
SS
989 /* NOTREACHED */
990 }
991 }
992 }
993
c906108c
SS
994 addr = sal.pc;
995
996 if (from_tty)
997 {
a3f17187 998 printf_filtered (_("Continuing at "));
ed49a04f 999 fputs_filtered (paddress (addr), gdb_stdout);
c906108c
SS
1000 printf_filtered (".\n");
1001 }
1002
89113898
PA
1003 /* If we are not asked to run in the bg, then prepare to run in the
1004 foreground, synchronously. */
1005 if (!async_exec && target_can_async_p ())
1006 {
1007 /* Simulate synchronous execution */
1008 async_disable_stdin ();
1009 }
1010
c906108c
SS
1011 clear_proceed_status ();
1012 proceed (addr, TARGET_SIGNAL_0, 0);
1013}
c906108c 1014\f
c5aa993b 1015
c906108c
SS
1016/* Go to line or address in current procedure */
1017static void
fba45db2 1018go_command (char *line_no, int from_tty)
c906108c 1019{
c5aa993b 1020 if (line_no == (char *) NULL || !*line_no)
b83266a0 1021 printf_filtered (GO_USAGE);
c906108c
SS
1022 else
1023 {
b83266a0
SS
1024 tbreak_command (line_no, from_tty);
1025 jump_command (line_no, from_tty);
c906108c
SS
1026 }
1027}
c906108c 1028\f
c5aa993b 1029
c906108c
SS
1030/* Continue program giving it specified signal. */
1031
1032static void
fba45db2 1033signal_command (char *signum_exp, int from_tty)
c906108c
SS
1034{
1035 enum target_signal oursig;
32c1e744 1036 int async_exec = 0;
c906108c
SS
1037
1038 dont_repeat (); /* Too dangerous. */
1039 ERROR_NO_INFERIOR;
1040
32c1e744
VP
1041 /* Find out whether we must run in the background. */
1042 if (signum_exp != NULL)
1043 async_exec = strip_bg_char (&signum_exp);
1044
1045 /* If we must run in the background, but the target can't do it,
1046 error out. */
1047 if (async_exec && !target_can_async_p ())
1048 error (_("Asynchronous execution not supported on this target."));
1049
1050 /* If we are not asked to run in the bg, then prepare to run in the
1051 foreground, synchronously. */
1052 if (!async_exec && target_can_async_p ())
1053 {
1054 /* Simulate synchronous execution. */
1055 async_disable_stdin ();
1056 }
1057
c906108c 1058 if (!signum_exp)
e2e0b3e5 1059 error_no_arg (_("signal number"));
c906108c
SS
1060
1061 /* It would be even slicker to make signal names be valid expressions,
1062 (the type could be "enum $signal" or some such), then the user could
1063 assign them to convenience variables. */
1064 oursig = target_signal_from_name (signum_exp);
1065
1066 if (oursig == TARGET_SIGNAL_UNKNOWN)
1067 {
1068 /* No, try numeric. */
bb518678 1069 int num = parse_and_eval_long (signum_exp);
c906108c
SS
1070
1071 if (num == 0)
1072 oursig = TARGET_SIGNAL_0;
1073 else
1074 oursig = target_signal_from_command (num);
1075 }
1076
1077 if (from_tty)
1078 {
1079 if (oursig == TARGET_SIGNAL_0)
a3f17187 1080 printf_filtered (_("Continuing with no signal.\n"));
c906108c 1081 else
a3f17187 1082 printf_filtered (_("Continuing with signal %s.\n"),
c906108c
SS
1083 target_signal_to_name (oursig));
1084 }
1085
1086 clear_proceed_status ();
1087 /* "signal 0" should not get stuck if we are stopped at a breakpoint.
1088 FIXME: Neither should "signal foo" but when I tried passing
1089 (CORE_ADDR)-1 unconditionally I got a testsuite failure which I haven't
1090 tried to track down yet. */
2acceee2 1091 proceed (oursig == TARGET_SIGNAL_0 ? (CORE_ADDR) -1 : stop_pc, oursig, 0);
c906108c
SS
1092}
1093
c906108c
SS
1094/* Proceed until we reach a different source line with pc greater than
1095 our current one or exit the function. We skip calls in both cases.
1096
1097 Note that eventually this command should probably be changed so
1098 that only source lines are printed out when we hit the breakpoint
1099 we set. This may involve changes to wait_for_inferior and the
1100 proceed status code. */
1101
c906108c 1102static void
fba45db2 1103until_next_command (int from_tty)
c906108c
SS
1104{
1105 struct frame_info *frame;
1106 CORE_ADDR pc;
1107 struct symbol *func;
1108 struct symtab_and_line sal;
c5aa993b 1109
c906108c
SS
1110 clear_proceed_status ();
1111
1112 frame = get_current_frame ();
1113
1114 /* Step until either exited from this function or greater
1115 than the current line (if in symbolic section) or pc (if
1116 not). */
1117
1118 pc = read_pc ();
1119 func = find_pc_function (pc);
c5aa993b 1120
c906108c
SS
1121 if (!func)
1122 {
1123 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
c5aa993b 1124
c906108c 1125 if (msymbol == NULL)
8a3fe4f8 1126 error (_("Execution is not within a known function."));
c5aa993b 1127
c906108c
SS
1128 step_range_start = SYMBOL_VALUE_ADDRESS (msymbol);
1129 step_range_end = pc;
1130 }
1131 else
1132 {
1133 sal = find_pc_line (pc, 0);
c5aa993b 1134
c906108c
SS
1135 step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
1136 step_range_end = sal.end;
1137 }
c5aa993b 1138
5fbbeb29 1139 step_over_calls = STEP_OVER_ALL;
aa0cd9c1 1140 step_frame_id = get_frame_id (frame);
c906108c
SS
1141
1142 step_multi = 0; /* Only one call to proceed */
c5aa993b 1143
2acceee2 1144 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
c906108c
SS
1145}
1146
c5aa993b 1147static void
fba45db2 1148until_command (char *arg, int from_tty)
c906108c 1149{
43ff13b4
JM
1150 int async_exec = 0;
1151
c906108c 1152 if (!target_has_execution)
8a3fe4f8 1153 error (_("The program is not running."));
43ff13b4
JM
1154
1155 /* Find out whether we must run in the background. */
1156 if (arg != NULL)
1157 async_exec = strip_bg_char (&arg);
1158
1159 /* If we must run in the background, but the target can't do it,
1160 error out. */
362646f5 1161 if (async_exec && !target_can_async_p ())
8a3fe4f8 1162 error (_("Asynchronous execution not supported on this target."));
43ff13b4
JM
1163
1164 /* If we are not asked to run in the bg, then prepare to run in the
1165 foreground, synchronously. */
362646f5 1166 if (!async_exec && target_can_async_p ())
c5aa993b 1167 {
43ff13b4 1168 /* Simulate synchronous execution */
6426a772 1169 async_disable_stdin ();
43ff13b4
JM
1170 }
1171
c906108c 1172 if (arg)
ae66c1fc 1173 until_break_command (arg, from_tty, 0);
c906108c
SS
1174 else
1175 until_next_command (from_tty);
1176}
ae66c1fc
EZ
1177
1178static void
1179advance_command (char *arg, int from_tty)
1180{
1181 int async_exec = 0;
1182
1183 if (!target_has_execution)
8a3fe4f8 1184 error (_("The program is not running."));
ae66c1fc
EZ
1185
1186 if (arg == NULL)
e2e0b3e5 1187 error_no_arg (_("a location"));
ae66c1fc
EZ
1188
1189 /* Find out whether we must run in the background. */
1190 if (arg != NULL)
1191 async_exec = strip_bg_char (&arg);
1192
1193 /* If we must run in the background, but the target can't do it,
1194 error out. */
362646f5 1195 if (async_exec && !target_can_async_p ())
8a3fe4f8 1196 error (_("Asynchronous execution not supported on this target."));
ae66c1fc
EZ
1197
1198 /* If we are not asked to run in the bg, then prepare to run in the
1199 foreground, synchronously. */
362646f5 1200 if (!async_exec && target_can_async_p ())
ae66c1fc
EZ
1201 {
1202 /* Simulate synchronous execution. */
1203 async_disable_stdin ();
1204 }
1205
1206 until_break_command (arg, from_tty, 1);
1207}
c906108c 1208\f
f941662f
MK
1209/* Print the result of a function at the end of a 'finish' command. */
1210
11cf8741 1211static void
c055b101 1212print_return_value (struct type *func_type, struct type *value_type)
11cf8741 1213{
31db7b6c 1214 struct gdbarch *gdbarch = current_gdbarch;
bb472c1e
MK
1215 struct cleanup *old_chain;
1216 struct ui_stream *stb;
44e5158b
AC
1217 struct value *value;
1218
181fc57c 1219 CHECK_TYPEDEF (value_type);
44e5158b 1220 gdb_assert (TYPE_CODE (value_type) != TYPE_CODE_VOID);
11cf8741 1221
92ad9cd9
AC
1222 /* FIXME: 2003-09-27: When returning from a nested inferior function
1223 call, it's possible (with no help from the architecture vector)
1224 to locate and return/print a "struct return" value. This is just
1225 a more complicated case of what is already being done in in the
1226 inferior function call code. In fact, when inferior function
1227 calls are made async, this will likely be made the norm. */
31db7b6c 1228
c055b101
CV
1229 switch (gdbarch_return_value (gdbarch, func_type, value_type,
1230 NULL, NULL, NULL))
44e5158b 1231 {
750eb019
AC
1232 case RETURN_VALUE_REGISTER_CONVENTION:
1233 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
181fc57c 1234 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
44e5158b 1235 value = allocate_value (value_type);
c055b101 1236 gdbarch_return_value (gdbarch, func_type, value_type, stop_registers,
990a07ab 1237 value_contents_raw (value), NULL);
750eb019
AC
1238 break;
1239 case RETURN_VALUE_STRUCT_CONVENTION:
1240 value = NULL;
1241 break;
1242 default:
e2e0b3e5 1243 internal_error (__FILE__, __LINE__, _("bad switch"));
44e5158b 1244 }
bb472c1e 1245
31db7b6c
MK
1246 if (value)
1247 {
1248 /* Print it. */
1d751abe
PA
1249 stb = ui_out_stream_new (uiout);
1250 old_chain = make_cleanup_ui_out_stream_delete (stb);
31db7b6c
MK
1251 ui_out_text (uiout, "Value returned is ");
1252 ui_out_field_fmt (uiout, "gdb-result-var", "$%d",
1253 record_latest_value (value));
1254 ui_out_text (uiout, " = ");
1255 value_print (value, stb->stream, 0, Val_no_prettyprint);
1256 ui_out_field_stream (uiout, "return-value", stb);
1257 ui_out_text (uiout, "\n");
1d751abe 1258 do_cleanups (old_chain);
31db7b6c
MK
1259 }
1260 else
1261 {
1262 ui_out_text (uiout, "Value returned has type: ");
1d751abe
PA
1263 ui_out_field_string (uiout, "return-type", TYPE_NAME (value_type));
1264 ui_out_text (uiout, ".");
1265 ui_out_text (uiout, " Cannot determine contents\n");
31db7b6c 1266 }
11cf8741
JM
1267}
1268
43ff13b4 1269/* Stuff that needs to be done by the finish command after the target
f941662f
MK
1270 has stopped. In asynchronous mode, we wait for the target to stop
1271 in the call to poll or select in the event loop, so it is
1272 impossible to do all the stuff as part of the finish_command
1273 function itself. The only chance we have to complete this command
1274 is in fetch_inferior_event, which is called by the event loop as
1275 soon as it detects that the target has stopped. This function is
1276 called via the cmd_continuation pointer. */
1277
1278static void
f107f563 1279finish_command_continuation (struct continuation_arg *arg, int error_p)
43ff13b4 1280{
52f0bd74 1281 struct symbol *function;
43ff13b4 1282 struct breakpoint *breakpoint;
0d06e24b 1283 struct cleanup *cleanups;
c5aa993b 1284
57e687d9 1285 breakpoint = (struct breakpoint *) arg->data.pointer;
f941662f
MK
1286 function = (struct symbol *) arg->next->data.pointer;
1287 cleanups = (struct cleanup *) arg->next->next->data.pointer;
43ff13b4 1288
f107f563 1289 if (!error_p)
43ff13b4 1290 {
f107f563
VP
1291 if (bpstat_find_breakpoint (stop_bpstat, breakpoint) != NULL
1292 && function != NULL)
1293 {
1294 struct type *value_type;
1295
1296 value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
1297 if (!value_type)
1298 internal_error (__FILE__, __LINE__,
1299 _("finish_command: function has no target type"));
1300
1301 if (TYPE_CODE (value_type) != TYPE_CODE_VOID)
1302 print_return_value (SYMBOL_TYPE (function), value_type);
1303 }
43ff13b4 1304 }
f941662f 1305
f107f563 1306 delete_breakpoint (breakpoint);
43ff13b4
JM
1307}
1308
f941662f
MK
1309/* "finish": Set a temporary breakpoint at the place the selected
1310 frame will return to, then continue. */
c906108c
SS
1311
1312static void
fba45db2 1313finish_command (char *arg, int from_tty)
c906108c
SS
1314{
1315 struct symtab_and_line sal;
52f0bd74
AC
1316 struct frame_info *frame;
1317 struct symbol *function;
c906108c
SS
1318 struct breakpoint *breakpoint;
1319 struct cleanup *old_chain;
0d06e24b 1320 struct continuation_arg *arg1, *arg2, *arg3;
43ff13b4
JM
1321
1322 int async_exec = 0;
1323
f941662f 1324 /* Find out whether we must run in the background. */
43ff13b4
JM
1325 if (arg != NULL)
1326 async_exec = strip_bg_char (&arg);
1327
1328 /* If we must run in the background, but the target can't do it,
f941662f 1329 error out. */
362646f5 1330 if (async_exec && !target_can_async_p ())
8a3fe4f8 1331 error (_("Asynchronous execution not supported on this target."));
43ff13b4
JM
1332
1333 /* If we are not asked to run in the bg, then prepare to run in the
f941662f 1334 foreground, synchronously. */
362646f5 1335 if (!async_exec && target_can_async_p ())
c5aa993b 1336 {
f941662f 1337 /* Simulate synchronous execution. */
6426a772 1338 async_disable_stdin ();
43ff13b4 1339 }
c906108c
SS
1340
1341 if (arg)
8a3fe4f8 1342 error (_("The \"finish\" command does not take any arguments."));
c906108c 1343 if (!target_has_execution)
8a3fe4f8 1344 error (_("The program is not running."));
c906108c 1345
206415a3 1346 frame = get_prev_frame (get_selected_frame (_("No selected frame.")));
c906108c 1347 if (frame == 0)
8a3fe4f8 1348 error (_("\"finish\" not meaningful in the outermost frame."));
c906108c
SS
1349
1350 clear_proceed_status ();
1351
bdd78e62
AC
1352 sal = find_pc_line (get_frame_pc (frame), 0);
1353 sal.pc = get_frame_pc (frame);
c906108c 1354
818dd999 1355 breakpoint = set_momentary_breakpoint (sal, get_frame_id (frame), bp_finish);
c906108c 1356
f107f563 1357 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c
SS
1358
1359 /* Find the function we will return from. */
1360
206415a3 1361 function = find_pc_function (get_frame_pc (get_selected_frame (NULL)));
c906108c 1362
f941662f
MK
1363 /* Print info on the selected frame, including level number but not
1364 source. */
c906108c
SS
1365 if (from_tty)
1366 {
a3f17187 1367 printf_filtered (_("Run till exit from "));
b04f3ab4 1368 print_stack_frame (get_selected_frame (NULL), 1, LOCATION);
c906108c
SS
1369 }
1370
32c1e744
VP
1371 proceed_to_finish = 1; /* We want stop_registers, please... */
1372 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
1373
f107f563
VP
1374 arg1 =
1375 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1376 arg2 =
1377 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1378 arg3 =
1379 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1380 arg1->next = arg2;
1381 arg2->next = arg3;
1382 arg3->next = NULL;
1383 arg1->data.pointer = breakpoint;
1384 arg2->data.pointer = function;
1385 arg3->data.pointer = old_chain;
1386 add_continuation (finish_command_continuation, arg1);
43ff13b4 1387
43ff13b4 1388 /* Do this only if not running asynchronously or if the target
f941662f
MK
1389 cannot do async execution. Otherwise, complete this command when
1390 the target actually stops, in fetch_inferior_event. */
f107f563 1391 discard_cleanups (old_chain);
362646f5 1392 if (!target_can_async_p ())
f107f563 1393 do_all_continuations (0);
c906108c
SS
1394}
1395\f
f941662f 1396
c906108c 1397static void
fba45db2 1398program_info (char *args, int from_tty)
c906108c
SS
1399{
1400 bpstat bs = stop_bpstat;
8671a17b
PA
1401 int num;
1402 int stat = bpstat_num (&bs, &num);
c5aa993b 1403
c906108c
SS
1404 if (!target_has_execution)
1405 {
a3f17187 1406 printf_filtered (_("The program being debugged is not being run.\n"));
c906108c
SS
1407 return;
1408 }
1409
1410 target_files_info ();
a3f17187 1411 printf_filtered (_("Program stopped at %s.\n"),
bb599908 1412 hex_string ((unsigned long) stop_pc));
c906108c 1413 if (stop_step)
a3f17187 1414 printf_filtered (_("It stopped after being stepped.\n"));
8671a17b 1415 else if (stat != 0)
c906108c
SS
1416 {
1417 /* There may be several breakpoints in the same place, so this
c5aa993b 1418 isn't as strange as it seems. */
8671a17b 1419 while (stat != 0)
c906108c 1420 {
8671a17b 1421 if (stat < 0)
c906108c 1422 {
a3f17187
AC
1423 printf_filtered (_("\
1424It stopped at a breakpoint that has since been deleted.\n"));
c906108c
SS
1425 }
1426 else
a3f17187 1427 printf_filtered (_("It stopped at breakpoint %d.\n"), num);
8671a17b 1428 stat = bpstat_num (&bs, &num);
c906108c
SS
1429 }
1430 }
1431 else if (stop_signal != TARGET_SIGNAL_0)
1432 {
a3f17187 1433 printf_filtered (_("It stopped with signal %s, %s.\n"),
c906108c
SS
1434 target_signal_to_name (stop_signal),
1435 target_signal_to_string (stop_signal));
1436 }
1437
1438 if (!from_tty)
1439 {
a3f17187
AC
1440 printf_filtered (_("\
1441Type \"info stack\" or \"info registers\" for more information.\n"));
c906108c
SS
1442 }
1443}
1444\f
1445static void
fba45db2 1446environment_info (char *var, int from_tty)
c906108c
SS
1447{
1448 if (var)
1449 {
aa1ee363 1450 char *val = get_in_environ (inferior_environ, var);
c906108c
SS
1451 if (val)
1452 {
1453 puts_filtered (var);
1454 puts_filtered (" = ");
1455 puts_filtered (val);
1456 puts_filtered ("\n");
1457 }
1458 else
1459 {
1460 puts_filtered ("Environment variable \"");
1461 puts_filtered (var);
1462 puts_filtered ("\" not defined.\n");
1463 }
1464 }
1465 else
1466 {
aa1ee363 1467 char **vector = environ_vector (inferior_environ);
c906108c
SS
1468 while (*vector)
1469 {
1470 puts_filtered (*vector++);
1471 puts_filtered ("\n");
1472 }
1473 }
1474}
1475
1476static void
fba45db2 1477set_environment_command (char *arg, int from_tty)
c906108c 1478{
52f0bd74 1479 char *p, *val, *var;
c906108c
SS
1480 int nullset = 0;
1481
1482 if (arg == 0)
e2e0b3e5 1483 error_no_arg (_("environment variable and value"));
c906108c
SS
1484
1485 /* Find seperation between variable name and value */
1486 p = (char *) strchr (arg, '=');
1487 val = (char *) strchr (arg, ' ');
1488
1489 if (p != 0 && val != 0)
1490 {
1491 /* We have both a space and an equals. If the space is before the
c5aa993b
JM
1492 equals, walk forward over the spaces til we see a nonspace
1493 (possibly the equals). */
c906108c
SS
1494 if (p > val)
1495 while (*val == ' ')
1496 val++;
1497
1498 /* Now if the = is after the char following the spaces,
c5aa993b 1499 take the char following the spaces. */
c906108c
SS
1500 if (p > val)
1501 p = val - 1;
1502 }
1503 else if (val != 0 && p == 0)
1504 p = val;
1505
1506 if (p == arg)
e2e0b3e5 1507 error_no_arg (_("environment variable to set"));
c906108c
SS
1508
1509 if (p == 0 || p[1] == 0)
1510 {
1511 nullset = 1;
1512 if (p == 0)
1513 p = arg + strlen (arg); /* So that savestring below will work */
1514 }
1515 else
1516 {
1517 /* Not setting variable value to null */
1518 val = p + 1;
1519 while (*val == ' ' || *val == '\t')
1520 val++;
1521 }
1522
c5aa993b
JM
1523 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
1524 p--;
c906108c
SS
1525
1526 var = savestring (arg, p - arg);
1527 if (nullset)
1528 {
a3f17187
AC
1529 printf_filtered (_("\
1530Setting environment variable \"%s\" to null value.\n"),
1531 var);
c906108c
SS
1532 set_in_environ (inferior_environ, var, "");
1533 }
1534 else
1535 set_in_environ (inferior_environ, var, val);
b8c9b27d 1536 xfree (var);
c906108c
SS
1537}
1538
1539static void
fba45db2 1540unset_environment_command (char *var, int from_tty)
c906108c
SS
1541{
1542 if (var == 0)
1543 {
1544 /* If there is no argument, delete all environment variables.
c5aa993b 1545 Ask for confirmation if reading from the terminal. */
e2e0b3e5 1546 if (!from_tty || query (_("Delete all environment variables? ")))
c906108c
SS
1547 {
1548 free_environ (inferior_environ);
1549 inferior_environ = make_environ ();
1550 }
1551 }
1552 else
1553 unset_in_environ (inferior_environ, var);
1554}
1555
1556/* Handle the execution path (PATH variable) */
1557
1558static const char path_var_name[] = "PATH";
1559
c906108c 1560static void
fba45db2 1561path_info (char *args, int from_tty)
c906108c
SS
1562{
1563 puts_filtered ("Executable and object file path: ");
1564 puts_filtered (get_in_environ (inferior_environ, path_var_name));
1565 puts_filtered ("\n");
1566}
1567
1568/* Add zero or more directories to the front of the execution path. */
1569
1570static void
fba45db2 1571path_command (char *dirname, int from_tty)
c906108c
SS
1572{
1573 char *exec_path;
1574 char *env;
1575 dont_repeat ();
1576 env = get_in_environ (inferior_environ, path_var_name);
1577 /* Can be null if path is not set */
1578 if (!env)
1579 env = "";
4fcf66da 1580 exec_path = xstrdup (env);
c906108c
SS
1581 mod_path (dirname, &exec_path);
1582 set_in_environ (inferior_environ, path_var_name, exec_path);
b8c9b27d 1583 xfree (exec_path);
c906108c 1584 if (from_tty)
c5aa993b 1585 path_info ((char *) NULL, from_tty);
c906108c 1586}
c906108c 1587\f
c5aa993b 1588
4782dc19 1589/* Print out the machine register regnum. If regnum is -1, print all
0ab7a791
AC
1590 registers (print_all == 1) or all non-float and non-vector
1591 registers (print_all == 0).
c906108c
SS
1592
1593 For most machines, having all_registers_info() print the
0ab7a791
AC
1594 register(s) one per line is good enough. If a different format is
1595 required, (eg, for MIPS or Pyramid 90x, which both have lots of
1596 regs), or there is an existing convention for showing all the
1597 registers, define the architecture method PRINT_REGISTERS_INFO to
1598 provide that format. */
c906108c 1599
666e11c5 1600void
0ab7a791
AC
1601default_print_registers_info (struct gdbarch *gdbarch,
1602 struct ui_file *file,
1603 struct frame_info *frame,
1604 int regnum, int print_all)
c906108c 1605{
0ab7a791 1606 int i;
a4bd449d
UW
1607 const int numregs = gdbarch_num_regs (gdbarch)
1608 + gdbarch_num_pseudo_regs (gdbarch);
10c42a71 1609 gdb_byte buffer[MAX_REGISTER_SIZE];
0ab7a791 1610
c906108c
SS
1611 for (i = 0; i < numregs; i++)
1612 {
4782dc19
AC
1613 /* Decide between printing all regs, non-float / vector regs, or
1614 specific reg. */
c5aa993b
JM
1615 if (regnum == -1)
1616 {
f9418c0f 1617 if (print_all)
4782dc19 1618 {
f9418c0f 1619 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
4782dc19 1620 continue;
f9418c0f
AC
1621 }
1622 else
1623 {
1624 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
4782dc19
AC
1625 continue;
1626 }
c5aa993b
JM
1627 }
1628 else
1629 {
1630 if (i != regnum)
1631 continue;
1632 }
c906108c
SS
1633
1634 /* If the register name is empty, it is undefined for this
c5aa993b 1635 processor, so don't display anything. */
a4bd449d
UW
1636 if (gdbarch_register_name (gdbarch, i) == NULL
1637 || *(gdbarch_register_name (gdbarch, i)) == '\0')
c906108c
SS
1638 continue;
1639
a4bd449d 1640 fputs_filtered (gdbarch_register_name (gdbarch, i), file);
c9f4d572 1641 print_spaces_filtered (15 - strlen (gdbarch_register_name
a4bd449d 1642 (gdbarch, i)), file);
c906108c
SS
1643
1644 /* Get the data in raw format. */
9730f241 1645 if (! frame_register_read (frame, i, buffer))
c906108c 1646 {
0ab7a791 1647 fprintf_filtered (file, "*value not available*\n");
c906108c
SS
1648 continue;
1649 }
1650
0ab7a791
AC
1651 /* If virtual format is floating, print it that way, and in raw
1652 hex. */
f949c649
TJB
1653 if (TYPE_CODE (register_type (gdbarch, i)) == TYPE_CODE_FLT
1654 || TYPE_CODE (register_type (gdbarch, i)) == TYPE_CODE_DECFLOAT)
c906108c 1655 {
0ab7a791 1656 int j;
c906108c 1657
a4bd449d 1658 val_print (register_type (gdbarch, i), buffer, 0, 0,
0ab7a791 1659 file, 0, 1, 0, Val_pretty_default);
c906108c 1660
0ab7a791 1661 fprintf_filtered (file, "\t(raw 0x");
a4bd449d 1662 for (j = 0; j < register_size (gdbarch, i); j++)
c906108c 1663 {
0ab7a791 1664 int idx;
a4bd449d 1665 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
0ab7a791
AC
1666 idx = j;
1667 else
a4bd449d 1668 idx = register_size (gdbarch, i) - 1 - j;
9730f241 1669 fprintf_filtered (file, "%02x", (unsigned char) buffer[idx]);
c906108c 1670 }
0ab7a791 1671 fprintf_filtered (file, ")");
c906108c 1672 }
c906108c
SS
1673 else
1674 {
ab4327e0 1675 /* Print the register in hex. */
a4bd449d 1676 val_print (register_type (gdbarch, i), buffer, 0, 0,
9d84ac84 1677 file, 'x', 1, 0, Val_pretty_default);
ab4327e0
EZ
1678 /* If not a vector register, print it also according to its
1679 natural format. */
a4bd449d 1680 if (TYPE_VECTOR (register_type (gdbarch, i)) == 0)
ab4327e0 1681 {
9d84ac84 1682 fprintf_filtered (file, "\t");
a4bd449d 1683 val_print (register_type (gdbarch, i), buffer, 0, 0,
9d84ac84 1684 file, 0, 1, 0, Val_pretty_default);
ab4327e0 1685 }
c906108c
SS
1686 }
1687
0ab7a791 1688 fprintf_filtered (file, "\n");
c906108c
SS
1689 }
1690}
c906108c 1691
c906108c 1692void
fba45db2 1693registers_info (char *addr_exp, int fpregs)
c906108c 1694{
206415a3 1695 struct frame_info *frame;
a4bd449d 1696 struct gdbarch *gdbarch;
c906108c 1697 int regnum, numregs;
52f0bd74 1698 char *end;
c906108c
SS
1699
1700 if (!target_has_registers)
8a3fe4f8 1701 error (_("The program has no registers now."));
206415a3 1702 frame = get_selected_frame (NULL);
a4bd449d 1703 gdbarch = get_frame_arch (frame);
c906108c
SS
1704
1705 if (!addr_exp)
1706 {
a4bd449d 1707 gdbarch_print_registers_info (gdbarch, gdb_stdout,
206415a3 1708 frame, -1, fpregs);
c906108c
SS
1709 return;
1710 }
1711
f9418c0f 1712 while (*addr_exp != '\0')
c5aa993b 1713 {
f9418c0f
AC
1714 char *start;
1715 const char *end;
c906108c 1716
f9418c0f
AC
1717 /* Keep skipping leading white space. */
1718 if (isspace ((*addr_exp)))
1719 {
1720 addr_exp++;
1721 continue;
1722 }
c906108c 1723
f9418c0f
AC
1724 /* Discard any leading ``$''. Check that there is something
1725 resembling a register following it. */
1726 if (addr_exp[0] == '$')
1727 addr_exp++;
1728 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
8a3fe4f8 1729 error (_("Missing register name"));
c906108c 1730
f9418c0f
AC
1731 /* Find the start/end of this register name/num/group. */
1732 start = addr_exp;
1733 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
1734 addr_exp++;
1735 end = addr_exp;
ad842144 1736
f9418c0f
AC
1737 /* Figure out what we've found and display it. */
1738
1739 /* A register name? */
1740 {
ad842144 1741 int regnum = frame_map_name_to_regnum (frame, start, end - start);
f9418c0f
AC
1742 if (regnum >= 0)
1743 {
ad842144
MR
1744 /* User registers lie completely outside of the range of
1745 normal registers. Catch them early so that the target
1746 never sees them. */
1747 if (regnum >= gdbarch_num_regs (gdbarch)
1748 + gdbarch_num_pseudo_regs (gdbarch))
1749 {
1750 struct value *val = value_of_user_reg (regnum, frame);
1751
1752 printf_filtered ("%s: ", start);
1753 print_scalar_formatted (value_contents (val),
1754 check_typedef (value_type (val)),
1755 'x', 0, gdb_stdout);
1756 printf_filtered ("\n");
1757 }
1758 else
1759 gdbarch_print_registers_info (gdbarch, gdb_stdout,
1760 frame, regnum, fpregs);
f9418c0f
AC
1761 continue;
1762 }
1763 }
ad842144 1764
f9418c0f
AC
1765 /* A register number? (how portable is this one?). */
1766 {
1767 char *endptr;
1768 int regnum = strtol (start, &endptr, 0);
1769 if (endptr == end
1770 && regnum >= 0
a4bd449d
UW
1771 && regnum < gdbarch_num_regs (gdbarch)
1772 + gdbarch_num_pseudo_regs (gdbarch))
f9418c0f 1773 {
a4bd449d 1774 gdbarch_print_registers_info (gdbarch, gdb_stdout,
206415a3 1775 frame, regnum, fpregs);
f9418c0f
AC
1776 continue;
1777 }
1778 }
1779
1780 /* A register group? */
1781 {
6c7d17ba 1782 struct reggroup *group;
a4bd449d 1783 for (group = reggroup_next (gdbarch, NULL);
6c7d17ba 1784 group != NULL;
a4bd449d 1785 group = reggroup_next (gdbarch, group))
f9418c0f
AC
1786 {
1787 /* Don't bother with a length check. Should the user
1788 enter a short register group name, go with the first
1789 group that matches. */
6c7d17ba 1790 if (strncmp (start, reggroup_name (group), end - start) == 0)
f9418c0f
AC
1791 break;
1792 }
6c7d17ba 1793 if (group != NULL)
f9418c0f
AC
1794 {
1795 int regnum;
f57d151a 1796 for (regnum = 0;
a4bd449d
UW
1797 regnum < gdbarch_num_regs (gdbarch)
1798 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 1799 regnum++)
f9418c0f 1800 {
a4bd449d
UW
1801 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1802 gdbarch_print_registers_info (gdbarch,
206415a3 1803 gdb_stdout, frame,
f9418c0f
AC
1804 regnum, fpregs);
1805 }
1806 continue;
1807 }
1808 }
c906108c 1809
f9418c0f 1810 /* Nothing matched. */
8a3fe4f8 1811 error (_("Invalid register `%.*s'"), (int) (end - start), start);
c5aa993b 1812 }
c906108c
SS
1813}
1814
1815void
fba45db2 1816all_registers_info (char *addr_exp, int from_tty)
c906108c
SS
1817{
1818 registers_info (addr_exp, 1);
1819}
1820
a58dd373 1821static void
fba45db2 1822nofp_registers_info (char *addr_exp, int from_tty)
c906108c
SS
1823{
1824 registers_info (addr_exp, 0);
1825}
e76f1f2e
AC
1826
1827static void
1828print_vector_info (struct gdbarch *gdbarch, struct ui_file *file,
1829 struct frame_info *frame, const char *args)
1830{
1831 if (gdbarch_print_vector_info_p (gdbarch))
1832 gdbarch_print_vector_info (gdbarch, file, frame, args);
1833 else
1834 {
1835 int regnum;
1836 int printed_something = 0;
ab4327e0 1837
f57d151a 1838 for (regnum = 0;
a4bd449d
UW
1839 regnum < gdbarch_num_regs (gdbarch)
1840 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 1841 regnum++)
e76f1f2e 1842 {
f9418c0f 1843 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
e76f1f2e
AC
1844 {
1845 printed_something = 1;
e76f1f2e 1846 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
e76f1f2e
AC
1847 }
1848 }
1849 if (!printed_something)
1850 fprintf_filtered (file, "No vector information\n");
1851 }
1852}
1853
1854static void
1855vector_info (char *args, int from_tty)
1856{
206415a3
DJ
1857 if (!target_has_registers)
1858 error (_("The program has no registers now."));
1859
1860 print_vector_info (current_gdbarch, gdb_stdout,
1861 get_selected_frame (NULL), args);
e76f1f2e 1862}
c906108c 1863\f
c5aa993b 1864
c906108c
SS
1865/*
1866 * TODO:
1867 * Should save/restore the tty state since it might be that the
1868 * program to be debugged was started on this tty and it wants
1869 * the tty in some state other than what we want. If it's running
1870 * on another terminal or without a terminal, then saving and
1871 * restoring the tty state is a harmless no-op.
1872 * This only needs to be done if we are attaching to a process.
1873 */
1874
1875/*
1876 attach_command --
1877 takes a program started up outside of gdb and ``attaches'' to it.
1878 This stops it cold in its tracks and allows us to start debugging it.
1879 and wait for the trace-trap that results from attaching. */
1880
9356cf8d
PA
1881static void
1882attach_command_post_wait (char *args, int from_tty, int async_exec)
1883{
1884 char *exec_file;
1885 char *full_exec_path = NULL;
1886
1887 stop_soon = NO_STOP_QUIETLY;
1888
1889 /* If no exec file is yet known, try to determine it from the
1890 process itself. */
1891 exec_file = (char *) get_exec_file (0);
1892 if (!exec_file)
1893 {
1894 exec_file = target_pid_to_exec_file (PIDGET (inferior_ptid));
1895 if (exec_file)
1896 {
1897 /* It's possible we don't have a full path, but rather just a
1898 filename. Some targets, such as HP-UX, don't provide the
1899 full path, sigh.
1900
1901 Attempt to qualify the filename against the source path.
1902 (If that fails, we'll just fall back on the original
1903 filename. Not much more we can do...)
1904 */
1905 if (!source_full_path_of (exec_file, &full_exec_path))
1906 full_exec_path = savestring (exec_file, strlen (exec_file));
1907
1908 exec_file_attach (full_exec_path, from_tty);
1909 symbol_file_add_main (full_exec_path, from_tty);
1910 }
1911 }
1912 else
1913 {
1914 reopen_exec_file ();
1915 reread_symbols ();
1916 }
1917
1918 /* Take any necessary post-attaching actions for this platform. */
1919 target_post_attach (PIDGET (inferior_ptid));
1920
1921 post_create_inferior (&current_target, from_tty);
1922
1923 /* Install inferior's terminal modes. */
1924 target_terminal_inferior ();
1925
1926 if (async_exec)
1927 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
1928 else
1929 {
1930 if (target_can_async_p ())
1931 async_enable_stdin ();
1932 normal_stop ();
1933 if (deprecated_attach_hook)
1934 deprecated_attach_hook ();
1935 }
1936}
1937
1938static void
f107f563 1939attach_command_continuation (struct continuation_arg *arg, int error_p)
9356cf8d
PA
1940{
1941 char *args;
1942 int from_tty;
1943 int async_exec;
1944
1945 args = (char *) arg->data.pointer;
1946 from_tty = arg->next->data.integer;
1947 async_exec = arg->next->next->data.integer;
1948
1949 attach_command_post_wait (args, from_tty, async_exec);
1950}
1951
c906108c 1952void
fba45db2 1953attach_command (char *args, int from_tty)
c906108c 1954{
c5aa993b
JM
1955 char *exec_file;
1956 char *full_exec_path = NULL;
9356cf8d 1957 int async_exec = 0;
c906108c 1958
c5aa993b 1959 dont_repeat (); /* Not for the faint of heart */
c906108c
SS
1960
1961 if (target_has_execution)
1962 {
1963 if (query ("A program is being debugged already. Kill it? "))
1964 target_kill ();
1965 else
8a3fe4f8 1966 error (_("Not killed."));
c906108c
SS
1967 }
1968
fd79ecee
DJ
1969 /* Clean up any leftovers from other runs. Some other things from
1970 this function should probably be moved into target_pre_inferior. */
1971 target_pre_inferior (from_tty);
1972
4bcff7eb
CV
1973 /* Clear out solib state. Otherwise the solib state of the previous
1974 inferior might have survived and is entirely wrong for the new
155bd5d1
AC
1975 target. This has been observed on GNU/Linux using glibc 2.3. How
1976 to reproduce:
4bcff7eb
CV
1977
1978 bash$ ./foo&
1979 [1] 4711
1980 bash$ ./foo&
1981 [1] 4712
1982 bash$ gdb ./foo
1983 [...]
1984 (gdb) attach 4711
1985 (gdb) detach
1986 (gdb) attach 4712
1987 Cannot access memory at address 0xdeadbeef
1988 */
7a72d44c 1989 clear_solib ();
4bcff7eb 1990
9356cf8d
PA
1991 if (args)
1992 {
1993 async_exec = strip_bg_char (&args);
1994
1995 /* If we get a request for running in the bg but the target
1996 doesn't support it, error out. */
1997 if (async_exec && !target_can_async_p ())
1998 error (_("Asynchronous execution not supported on this target."));
1999 }
2000
2001 /* If we don't get a request of running in the bg, then we need
2002 to simulate synchronous (fg) execution. */
2003 if (!async_exec && target_can_async_p ())
2004 {
2005 /* Simulate synchronous execution */
2006 async_disable_stdin ();
2007 }
2008
c906108c
SS
2009 target_attach (args, from_tty);
2010
2011 /* Set up the "saved terminal modes" of the inferior
2012 based on what modes we are starting it with. */
2013 target_terminal_init ();
2014
c906108c
SS
2015 /* Set up execution context to know that we should return from
2016 wait_for_inferior as soon as the target reports a stop. */
2017 init_wait_for_inferior ();
2018 clear_proceed_status ();
c906108c
SS
2019
2020 /* No traps are generated when attaching to inferior under Mach 3
2021 or GNU hurd. */
2022#ifndef ATTACH_NO_WAIT
c54cfec8
EZ
2023 /* Careful here. See comments in inferior.h. Basically some OSes
2024 don't ignore SIGSTOPs on continue requests anymore. We need a
2025 way for handle_inferior_event to reset the stop_signal variable
2026 after an attach, and this is what STOP_QUIETLY_NO_SIGSTOP is for. */
c0236d92 2027 stop_soon = STOP_QUIETLY_NO_SIGSTOP;
c906108c 2028
9356cf8d 2029 if (target_can_async_p ())
c5aa993b 2030 {
9356cf8d
PA
2031 /* sync_execution mode. Wait for stop. */
2032 struct continuation_arg *arg1, *arg2, *arg3;
c5aa993b 2033
9356cf8d
PA
2034 arg1 =
2035 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
2036 arg2 =
2037 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
2038 arg3 =
2039 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
2040 arg1->next = arg2;
2041 arg2->next = arg3;
2042 arg3->next = NULL;
2043 arg1->data.pointer = args;
2044 arg2->data.integer = from_tty;
2045 arg3->data.integer = async_exec;
2046 add_continuation (attach_command_continuation, arg1);
2047 return;
63ed89b4 2048 }
c906108c 2049
9356cf8d
PA
2050 wait_for_inferior (0);
2051#endif
6426a772 2052
9356cf8d 2053 attach_command_post_wait (args, from_tty, async_exec);
c906108c
SS
2054}
2055
2056/*
2057 * detach_command --
2058 * takes a program previously attached to and detaches it.
2059 * The program resumes execution and will no longer stop
2060 * on signals, etc. We better not have left any breakpoints
2061 * in the program or it'll die when it hits one. For this
2062 * to work, it may be necessary for the process to have been
2063 * previously attached. It *might* work if the program was
2064 * started via the normal ptrace (PTRACE_TRACEME).
2065 */
2066
2067static void
fba45db2 2068detach_command (char *args, int from_tty)
c906108c 2069{
1f5d0fc9 2070 dont_repeat (); /* Not for the faint of heart. */
c906108c 2071 target_detach (args, from_tty);
e85a822c 2072 no_shared_libraries (NULL, from_tty);
a0ef4274 2073 init_thread_list ();
9a4105ab
AC
2074 if (deprecated_detach_hook)
2075 deprecated_detach_hook ();
c906108c
SS
2076}
2077
6ad8ae5c
DJ
2078/* Disconnect from the current target without resuming it (leaving it
2079 waiting for a debugger).
2080
2081 We'd better not have left any breakpoints in the program or the
2082 next debugger will get confused. Currently only supported for some
2083 remote targets, since the normal attach mechanisms don't work on
2084 stopped processes on some native platforms (e.g. GNU/Linux). */
2085
2086static void
2087disconnect_command (char *args, int from_tty)
2088{
2089 dont_repeat (); /* Not for the faint of heart */
2090 target_disconnect (args, from_tty);
e85a822c 2091 no_shared_libraries (NULL, from_tty);
a0ef4274 2092 init_thread_list ();
9a4105ab
AC
2093 if (deprecated_detach_hook)
2094 deprecated_detach_hook ();
6ad8ae5c
DJ
2095}
2096
43ff13b4
JM
2097/* Stop the execution of the target while running in async mode, in
2098 the backgound. */
8b93c638 2099void
fba45db2 2100interrupt_target_command (char *args, int from_tty)
43ff13b4 2101{
362646f5 2102 if (target_can_async_p ())
43ff13b4 2103 {
c5aa993b 2104 dont_repeat (); /* Not for the faint of heart */
43ff13b4
JM
2105 target_stop ();
2106 }
2107}
2108
c906108c 2109static void
23e3a7ac
AC
2110print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
2111 struct frame_info *frame, const char *args)
c906108c 2112{
23e3a7ac
AC
2113 if (gdbarch_print_float_info_p (gdbarch))
2114 gdbarch_print_float_info (gdbarch, file, frame, args);
2115 else
2116 {
23e3a7ac
AC
2117 int regnum;
2118 int printed_something = 0;
ab4327e0 2119
f57d151a 2120 for (regnum = 0;
a4bd449d
UW
2121 regnum < gdbarch_num_regs (gdbarch)
2122 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 2123 regnum++)
23e3a7ac 2124 {
f9418c0f 2125 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
23e3a7ac
AC
2126 {
2127 printed_something = 1;
23e3a7ac 2128 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
23e3a7ac
AC
2129 }
2130 }
2131 if (!printed_something)
2132 fprintf_filtered (file, "\
2133No floating-point info available for this processor.\n");
23e3a7ac
AC
2134 }
2135}
2136
2137static void
2138float_info (char *args, int from_tty)
2139{
206415a3
DJ
2140 if (!target_has_registers)
2141 error (_("The program has no registers now."));
2142
1f5d0fc9 2143 print_float_info (current_gdbarch, gdb_stdout,
206415a3 2144 get_selected_frame (NULL), args);
c906108c
SS
2145}
2146\f
c906108c 2147static void
fba45db2 2148unset_command (char *args, int from_tty)
c906108c 2149{
a3f17187
AC
2150 printf_filtered (_("\
2151\"unset\" must be followed by the name of an unset subcommand.\n"));
c906108c
SS
2152 help_list (unsetlist, "unset ", -1, gdb_stdout);
2153}
2154
2155void
fba45db2 2156_initialize_infcmd (void)
c906108c 2157{
3cb3b8df
BR
2158 struct cmd_list_element *c = NULL;
2159
2160 /* add the filename of the terminal connected to inferior I/O */
2161 add_setshow_filename_cmd ("inferior-tty", class_run,
2162 &inferior_io_terminal, _("\
2163Set terminal for future runs of program being debugged."), _("\
2164Show terminal for future runs of program being debugged."), _("\
2165Usage: set inferior-tty /dev/pts/1"), NULL, NULL, &setlist, &showlist);
2166 add_com_alias ("tty", "set inferior-tty", class_alias, 0);
c906108c 2167
b4b4ac0b
AC
2168 add_setshow_optional_filename_cmd ("args", class_run,
2169 &inferior_args, _("\
2170Set argument list to give program being debugged when it is started."), _("\
2171Show argument list to give program being debugged when it is started."), _("\
2172Follow this command with any number of args, to be passed to the program."),
2173 notice_args_set,
2174 notice_args_read,
2175 &setlist, &showlist);
c906108c 2176
1a966eab
AC
2177 c = add_cmd ("environment", no_class, environment_info, _("\
2178The environment to give the program, or one variable's value.\n\
c906108c
SS
2179With an argument VAR, prints the value of environment variable VAR to\n\
2180give the program being debugged. With no arguments, prints the entire\n\
1a966eab 2181environment to be given to the program."), &showlist);
5ba2abeb 2182 set_cmd_completer (c, noop_completer);
c906108c
SS
2183
2184 add_prefix_cmd ("unset", no_class, unset_command,
1bedd215 2185 _("Complement to certain \"set\" commands."),
c906108c 2186 &unsetlist, "unset ", 0, &cmdlist);
c5aa993b 2187
1a966eab
AC
2188 c = add_cmd ("environment", class_run, unset_environment_command, _("\
2189Cancel environment variable VAR for the program.\n\
2190This does not affect the program until the next \"run\" command."),
c5aa993b 2191 &unsetlist);
5ba2abeb 2192 set_cmd_completer (c, noop_completer);
c906108c 2193
1a966eab
AC
2194 c = add_cmd ("environment", class_run, set_environment_command, _("\
2195Set environment variable value to give the program.\n\
c906108c
SS
2196Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
2197VALUES of environment variables are uninterpreted strings.\n\
1a966eab 2198This does not affect the program until the next \"run\" command."),
c5aa993b 2199 &setlist);
5ba2abeb 2200 set_cmd_completer (c, noop_completer);
c5aa993b 2201
1bedd215
AC
2202 c = add_com ("path", class_files, path_command, _("\
2203Add directory DIR(s) to beginning of search path for object files.\n\
c906108c
SS
2204$cwd in the path means the current working directory.\n\
2205This path is equivalent to the $PATH shell variable. It is a list of\n\
2206directories, separated by colons. These directories are searched to find\n\
1bedd215 2207fully linked executable files and separately compiled object files as needed."));
5ba2abeb 2208 set_cmd_completer (c, filename_completer);
c906108c 2209
1a966eab
AC
2210 c = add_cmd ("paths", no_class, path_info, _("\
2211Current search path for finding object files.\n\
c906108c
SS
2212$cwd in the path means the current working directory.\n\
2213This path is equivalent to the $PATH shell variable. It is a list of\n\
2214directories, separated by colons. These directories are searched to find\n\
1a966eab 2215fully linked executable files and separately compiled object files as needed."),
c906108c 2216 &showlist);
5ba2abeb 2217 set_cmd_completer (c, noop_completer);
c906108c 2218
1bedd215
AC
2219 add_com ("attach", class_run, attach_command, _("\
2220Attach to a process or file outside of GDB.\n\
c906108c
SS
2221This command attaches to another target, of the same type as your last\n\
2222\"target\" command (\"info files\" will show your target stack).\n\
2223The command may take as argument a process id or a device file.\n\
2224For a process id, you must have permission to send the process a signal,\n\
2225and it must have the same effective uid as the debugger.\n\
2226When using \"attach\" with a process id, the debugger finds the\n\
2227program running in the process, looking first in the current working\n\
2228directory, or (if not found there) using the source file search path\n\
2229(see the \"directory\" command). You can also use the \"file\" command\n\
1bedd215 2230to specify the program, and to load its symbol table."));
c906108c 2231
f73adfeb 2232 add_prefix_cmd ("detach", class_run, detach_command, _("\
1bedd215 2233Detach a process or file previously attached.\n\
c906108c 2234If a process, it is no longer traced, and it continues its execution. If\n\
f73adfeb
AS
2235you were debugging a file, the file is closed and gdb no longer accesses it."),
2236 &detachlist, "detach ", 0, &cmdlist);
c906108c 2237
1bedd215
AC
2238 add_com ("disconnect", class_run, disconnect_command, _("\
2239Disconnect from a target.\n\
6ad8ae5c 2240The target will wait for another debugger to connect. Not available for\n\
1bedd215 2241all targets."));
6ad8ae5c 2242
1bedd215
AC
2243 add_com ("signal", class_run, signal_command, _("\
2244Continue program giving it signal specified by the argument.\n\
2245An argument of \"0\" means continue program without giving it a signal."));
c906108c 2246
1bedd215
AC
2247 add_com ("stepi", class_run, stepi_command, _("\
2248Step one instruction exactly.\n\
2249Argument N means do this N times (or till program stops for another reason)."));
c906108c
SS
2250 add_com_alias ("si", "stepi", class_alias, 0);
2251
1bedd215
AC
2252 add_com ("nexti", class_run, nexti_command, _("\
2253Step one instruction, but proceed through subroutine calls.\n\
2254Argument N means do this N times (or till program stops for another reason)."));
c906108c
SS
2255 add_com_alias ("ni", "nexti", class_alias, 0);
2256
1bedd215
AC
2257 add_com ("finish", class_run, finish_command, _("\
2258Execute until selected stack frame returns.\n\
2259Upon return, the value returned is printed and put in the value history."));
c906108c 2260
1bedd215
AC
2261 add_com ("next", class_run, next_command, _("\
2262Step program, proceeding through subroutine calls.\n\
c906108c
SS
2263Like the \"step\" command as long as subroutine calls do not happen;\n\
2264when they do, the call is treated as one instruction.\n\
1bedd215 2265Argument N means do this N times (or till program stops for another reason)."));
c906108c
SS
2266 add_com_alias ("n", "next", class_run, 1);
2267 if (xdb_commands)
c5aa993b 2268 add_com_alias ("S", "next", class_run, 1);
c906108c 2269
1bedd215
AC
2270 add_com ("step", class_run, step_command, _("\
2271Step program until it reaches a different source line.\n\
2272Argument N means do this N times (or till program stops for another reason)."));
c906108c
SS
2273 add_com_alias ("s", "step", class_run, 1);
2274
1bedd215
AC
2275 c = add_com ("until", class_run, until_command, _("\
2276Execute until the program reaches a source line greater than the current\n\
2277or a specified location (same args as break command) within the current frame."));
5ba2abeb 2278 set_cmd_completer (c, location_completer);
c906108c 2279 add_com_alias ("u", "until", class_run, 1);
c5aa993b 2280
1bedd215
AC
2281 c = add_com ("advance", class_run, advance_command, _("\
2282Continue the program up to the given location (same form as args for break command).\n\
2283Execution will also stop upon exit from the current stack frame."));
ae66c1fc
EZ
2284 set_cmd_completer (c, location_completer);
2285
1bedd215
AC
2286 c = add_com ("jump", class_run, jump_command, _("\
2287Continue program being debugged at specified line or address.\n\
c906108c 2288Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1bedd215 2289for an address to start at."));
5ba2abeb 2290 set_cmd_completer (c, location_completer);
c906108c 2291
b83266a0 2292 if (xdb_commands)
c94fdfd0 2293 {
1bedd215
AC
2294 c = add_com ("go", class_run, go_command, _("\
2295Usage: go <location>\n\
c906108c
SS
2296Continue program being debugged, stopping at specified line or \n\
2297address.\n\
2298Give as argument either LINENUM or *ADDR, where ADDR is an \n\
2299expression for an address to start at.\n\
1bedd215 2300This command is a combination of tbreak and jump."));
5ba2abeb 2301 set_cmd_completer (c, location_completer);
c94fdfd0 2302 }
b83266a0 2303
c906108c 2304 if (xdb_commands)
c5aa993b 2305 add_com_alias ("g", "go", class_run, 1);
c906108c 2306
1bedd215
AC
2307 add_com ("continue", class_run, continue_command, _("\
2308Continue program being debugged, after signal or breakpoint.\n\
c906108c
SS
2309If proceeding from breakpoint, a number N may be used as an argument,\n\
2310which means to set the ignore count of that breakpoint to N - 1 (so that\n\
1bedd215 2311the breakpoint won't break until the Nth time it is reached)."));
c906108c
SS
2312 add_com_alias ("c", "cont", class_run, 1);
2313 add_com_alias ("fg", "cont", class_run, 1);
2314
1bedd215
AC
2315 c = add_com ("run", class_run, run_command, _("\
2316Start debugged program. You may specify arguments to give it.\n\
c906108c
SS
2317Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
2318Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
2319With no arguments, uses arguments last specified (with \"run\" or \"set args\").\n\
2320To cancel previous arguments and run with no arguments,\n\
1bedd215 2321use \"set args\" without arguments."));
5ba2abeb 2322 set_cmd_completer (c, filename_completer);
c906108c
SS
2323 add_com_alias ("r", "run", class_run, 1);
2324 if (xdb_commands)
2325 add_com ("R", class_run, run_no_args_command,
1bedd215 2326 _("Start debugged program with no arguments."));
c906108c 2327
1bedd215 2328 c = add_com ("start", class_run, start_command, _("\
a4d5f2e0
JB
2329Run the debugged program until the beginning of the main procedure.\n\
2330You may specify arguments to give to your program, just as with the\n\
1bedd215 2331\"run\" command."));
a4d5f2e0
JB
2332 set_cmd_completer (c, filename_completer);
2333
700b53b1
TT
2334 c = add_com ("interrupt", class_run, interrupt_target_command,
2335 _("Interrupt the execution of the debugged program."));
2336 set_cmd_async_ok (c);
43ff13b4 2337
1bedd215
AC
2338 add_info ("registers", nofp_registers_info, _("\
2339List of integer registers and their contents, for selected stack frame.\n\
2340Register name as argument means describe only that register."));
7194c49b 2341 add_info_alias ("r", "registers", 1);
c906108c
SS
2342
2343 if (xdb_commands)
1bedd215
AC
2344 add_com ("lr", class_info, nofp_registers_info, _("\
2345List of integer registers and their contents, for selected stack frame.\n\
2346Register name as argument means describe only that register."));
2347 add_info ("all-registers", all_registers_info, _("\
2348List of all registers and their contents, for selected stack frame.\n\
2349Register name as argument means describe only that register."));
c906108c
SS
2350
2351 add_info ("program", program_info,
1bedd215 2352 _("Execution status of the program."));
c906108c
SS
2353
2354 add_info ("float", float_info,
1bedd215 2355 _("Print the status of the floating point unit\n"));
c906108c 2356
e76f1f2e 2357 add_info ("vector", vector_info,
1bedd215 2358 _("Print the status of the vector unit\n"));
e76f1f2e 2359
c906108c
SS
2360 inferior_environ = make_environ ();
2361 init_environ (inferior_environ);
2362}