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