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