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