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