* gdb.texinfo: Proofreading changes from Brian Youmans.
[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 = arg->data.integer;
584   single_inst      = arg->next->data.integer;
585   count            = arg->next->next->data.integer;
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.integer = skip_subroutines;
666       arg2->next = arg3;
667       arg2->data.integer = single_inst;
668       arg3->next = NULL;
669       arg3->data.integer = 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   /* We can stop during an inferior call because a signal is received. */
930   if (stopped_by_random_signal)
931     return 1;
932     
933   /* We may also stop prematurely because we hit a breakpoint in the
934      called routine. */
935   if (!stop_stack_dummy)
936     return 2;
937
938   /* On normal return, the stack dummy has been popped already.  */
939
940   memcpy (buffer, stop_registers, REGISTER_BYTES);
941   return 0;
942 }
943 \f
944 /* Proceed until we reach a different source line with pc greater than
945    our current one or exit the function.  We skip calls in both cases.
946
947    Note that eventually this command should probably be changed so
948    that only source lines are printed out when we hit the breakpoint
949    we set.  This may involve changes to wait_for_inferior and the
950    proceed status code.  */
951
952 /* ARGSUSED */
953 static void
954 until_next_command (from_tty)
955      int from_tty;
956 {
957   struct frame_info *frame;
958   CORE_ADDR pc;
959   struct symbol *func;
960   struct symtab_and_line sal;
961
962   clear_proceed_status ();
963
964   frame = get_current_frame ();
965
966   /* Step until either exited from this function or greater
967      than the current line (if in symbolic section) or pc (if
968      not). */
969
970   pc = read_pc ();
971   func = find_pc_function (pc);
972
973   if (!func)
974     {
975       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
976
977       if (msymbol == NULL)
978         error ("Execution is not within a known function.");
979
980       step_range_start = SYMBOL_VALUE_ADDRESS (msymbol);
981       step_range_end = pc;
982     }
983   else
984     {
985       sal = find_pc_line (pc, 0);
986
987       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
988       step_range_end = sal.end;
989     }
990
991   step_over_calls = 1;
992   step_frame_address = FRAME_FP (frame);
993   step_sp = read_sp ();
994
995   step_multi = 0;               /* Only one call to proceed */
996
997   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
998 }
999
1000 static void
1001 until_command (arg, from_tty)
1002      char *arg;
1003      int from_tty;
1004 {
1005   int async_exec = 0;
1006
1007   if (!target_has_execution)
1008     error ("The program is not running.");
1009
1010   /* Find out whether we must run in the background. */
1011   if (arg != NULL)
1012     async_exec = strip_bg_char (&arg);
1013
1014   /* If we must run in the background, but the target can't do it,
1015      error out. */
1016   if (event_loop_p && async_exec && !target_can_async_p ())
1017     error ("Asynchronous execution not supported on this target.");
1018
1019   /* If we are not asked to run in the bg, then prepare to run in the
1020      foreground, synchronously. */
1021   if (event_loop_p && !async_exec && target_can_async_p ())
1022     {
1023       /* Simulate synchronous execution */
1024       async_disable_stdin ();
1025     }
1026
1027   if (arg)
1028     until_break_command (arg, from_tty);
1029   else
1030     until_next_command (from_tty);
1031 }
1032 \f
1033
1034 /* Print the result of a function at the end of a 'finish' command. */
1035 static void
1036 print_return_value (int structure_return, struct type *value_type)
1037 {
1038   register value_ptr value;
1039 #ifdef UI_OUT
1040   static struct ui_stream *stb = NULL;
1041 #endif /* UI_OUT */
1042
1043   if (!structure_return)
1044     {
1045       value = value_being_returned (value_type, stop_registers, structure_return);
1046 #ifdef UI_OUT
1047       stb = ui_out_stream_new (uiout);
1048       ui_out_text (uiout, "Value returned is ");
1049       ui_out_field_fmt (uiout, "gdb-result-var", "$%d", record_latest_value (value));
1050       ui_out_text (uiout, "= ");
1051       value_print (value, stb->stream, 0, Val_no_prettyprint);
1052       ui_out_field_stream (uiout, "return-value", stb);
1053       ui_out_text (uiout, "\n");
1054 #else /* UI_OUT */
1055       printf_filtered ("Value returned is $%d = ", record_latest_value (value));
1056       value_print (value, gdb_stdout, 0, Val_no_prettyprint);
1057       printf_filtered ("\n");
1058 #endif /* UI_OUT */
1059     }
1060   else
1061     {
1062       /* We cannot determine the contents of the structure because
1063          it is on the stack, and we don't know where, since we did not
1064          initiate the call, as opposed to the call_function_by_hand case */
1065 #ifdef VALUE_RETURNED_FROM_STACK
1066       value = 0;
1067 #ifdef UI_OUT
1068       ui_out_text (uiout, "Value returned has type: ");
1069       ui_out_field_string (uiout, "return-type", TYPE_NAME (value_type));
1070       ui_out_text (uiout, ".");
1071       ui_out_text (uiout, " Cannot determine contents\n");
1072 #else /* UI_OUT */
1073       printf_filtered ("Value returned has type: %s.", TYPE_NAME (value_type));
1074       printf_filtered (" Cannot determine contents\n");
1075 #endif /* UI_OUT */
1076 #else
1077       value = value_being_returned (value_type, stop_registers, structure_return);
1078 #ifdef UI_OUT
1079       stb = ui_out_stream_new (uiout);
1080       ui_out_text (uiout, "Value returned is ");
1081       ui_out_field_fmt (uiout, "gdb-result-var", "$%d", record_latest_value (value));
1082       ui_out_text (uiout, "= ");
1083       value_print (value, stb->stream, 0, Val_no_prettyprint);
1084       ui_out_field_stream (uiout, "return-value", stb);
1085       ui_out_text (uiout, "\n");
1086 #else
1087       printf_filtered ("Value returned is $%d = ", record_latest_value (value));
1088       value_print (value, gdb_stdout, 0, Val_no_prettyprint);
1089       printf_filtered ("\n");
1090 #endif
1091 #endif
1092     }
1093 }
1094
1095 /* Stuff that needs to be done by the finish command after the target
1096    has stopped.  In asynchronous mode, we wait for the target to stop in
1097    the call to poll or select in the event loop, so it is impossible to
1098    do all the stuff as part of the finish_command function itself. The
1099    only chance we have to complete this command is in
1100    fetch_inferior_event, which is called by the event loop as soon as it
1101    detects that the target has stopped. This function is called via the
1102    cmd_continaution pointer. */
1103 void
1104 finish_command_continuation (arg)
1105      struct continuation_arg *arg;
1106 {
1107   register struct symbol *function;
1108   struct breakpoint *breakpoint;
1109   struct cleanup *cleanups;
1110
1111   breakpoint = (struct breakpoint *) arg->data.pointer;
1112   function   = (struct symbol *)     arg->next->data.pointer;
1113   cleanups   = (struct cleanup *)    arg->next->next->data.pointer;
1114
1115   if (bpstat_find_breakpoint (stop_bpstat, breakpoint) != NULL
1116       && function != 0)
1117     {
1118       struct type *value_type;
1119       CORE_ADDR funcaddr;
1120       int struct_return;
1121
1122       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
1123       if (!value_type)
1124         internal_error ("finish_command: function has no target type");
1125
1126       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
1127         {
1128           do_exec_cleanups (cleanups);
1129           return;
1130         }
1131
1132       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
1133
1134       struct_return = using_struct_return (value_of_variable (function, NULL),
1135                                            funcaddr,
1136                                            check_typedef (value_type),
1137                                            BLOCK_GCC_COMPILED (SYMBOL_BLOCK_VALUE (function)));
1138
1139       print_return_value (struct_return, value_type); 
1140     }
1141   do_exec_cleanups (cleanups);
1142 }
1143
1144 /* "finish": Set a temporary breakpoint at the place
1145    the selected frame will return to, then continue.  */
1146
1147 static void
1148 finish_command (arg, from_tty)
1149      char *arg;
1150      int from_tty;
1151 {
1152   struct symtab_and_line sal;
1153   register struct frame_info *frame;
1154   register struct symbol *function;
1155   struct breakpoint *breakpoint;
1156   struct cleanup *old_chain;
1157   struct continuation_arg *arg1, *arg2, *arg3;
1158
1159   int async_exec = 0;
1160
1161   /* Find out whether we must run in the background. */
1162   if (arg != NULL)
1163     async_exec = strip_bg_char (&arg);
1164
1165   /* If we must run in the background, but the target can't do it,
1166      error out. */
1167   if (event_loop_p && async_exec && !target_can_async_p ())
1168     error ("Asynchronous execution not supported on this target.");
1169
1170   /* If we are not asked to run in the bg, then prepare to run in the
1171      foreground, synchronously. */
1172   if (event_loop_p && !async_exec && target_can_async_p ())
1173     {
1174       /* Simulate synchronous execution */
1175       async_disable_stdin ();
1176     }
1177
1178   if (arg)
1179     error ("The \"finish\" command does not take any arguments.");
1180   if (!target_has_execution)
1181     error ("The program is not running.");
1182   if (selected_frame == NULL)
1183     error ("No selected frame.");
1184
1185   frame = get_prev_frame (selected_frame);
1186   if (frame == 0)
1187     error ("\"finish\" not meaningful in the outermost frame.");
1188
1189   clear_proceed_status ();
1190
1191   sal = find_pc_line (frame->pc, 0);
1192   sal.pc = frame->pc;
1193
1194   breakpoint = set_momentary_breakpoint (sal, frame, bp_finish);
1195
1196   if (!event_loop_p || !target_can_async_p ())
1197     old_chain = make_cleanup ((make_cleanup_func) delete_breakpoint, breakpoint);
1198   else
1199     old_chain = make_exec_cleanup ((make_cleanup_func) delete_breakpoint, breakpoint);
1200
1201   /* Find the function we will return from.  */
1202
1203   function = find_pc_function (selected_frame->pc);
1204
1205   /* Print info on the selected frame, including level number
1206      but not source.  */
1207   if (from_tty)
1208     {
1209       printf_filtered ("Run till exit from ");
1210       print_stack_frame (selected_frame, selected_frame_level, 0);
1211     }
1212
1213   /* If running asynchronously and the target support asynchronous
1214      execution, set things up for the rest of the finish command to be
1215      completed later on, when gdb has detected that the target has
1216      stopped, in fetch_inferior_event. */
1217   if (event_loop_p && target_can_async_p ())
1218     {
1219       arg1 =
1220         (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1221       arg2 =
1222         (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1223       arg3 =
1224         (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1225       arg1->next = arg2;
1226       arg2->next = arg3;
1227       arg3->next = NULL;
1228       arg1->data.pointer = breakpoint;
1229       arg2->data.pointer = function;
1230       arg3->data.pointer = old_chain;
1231       add_continuation (finish_command_continuation, arg1);
1232     }
1233
1234   proceed_to_finish = 1;        /* We want stop_registers, please... */
1235   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
1236
1237   /* Do this only if not running asynchronously or if the target
1238      cannot do async execution. Otherwise, complete this command when
1239      the target actually stops, in fetch_inferior_event. */
1240   if (!event_loop_p || !target_can_async_p ())
1241     {
1242
1243       /* Did we stop at our breakpoint? */
1244       if (bpstat_find_breakpoint (stop_bpstat, breakpoint) != NULL
1245           && function != 0)
1246         {
1247           struct type *value_type;
1248           CORE_ADDR funcaddr;
1249           int struct_return;
1250
1251           value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
1252           if (!value_type)
1253             internal_error ("finish_command: function has no target type");
1254
1255           /* FIXME: Shouldn't we do the cleanups before returning? */
1256           if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
1257             return;
1258
1259           funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
1260
1261           struct_return =
1262             using_struct_return (value_of_variable (function, NULL),
1263                                  funcaddr,
1264                                  check_typedef (value_type),
1265                         BLOCK_GCC_COMPILED (SYMBOL_BLOCK_VALUE (function)));
1266
1267           print_return_value (struct_return, value_type); 
1268         }
1269       do_cleanups (old_chain);
1270     }
1271 }
1272 \f
1273 /* ARGSUSED */
1274 static void
1275 program_info (args, from_tty)
1276      char *args;
1277      int from_tty;
1278 {
1279   bpstat bs = stop_bpstat;
1280   int num = bpstat_num (&bs);
1281
1282   if (!target_has_execution)
1283     {
1284       printf_filtered ("The program being debugged is not being run.\n");
1285       return;
1286     }
1287
1288   target_files_info ();
1289   printf_filtered ("Program stopped at %s.\n",
1290                    local_hex_string ((unsigned long) stop_pc));
1291   if (stop_step)
1292     printf_filtered ("It stopped after being stepped.\n");
1293   else if (num != 0)
1294     {
1295       /* There may be several breakpoints in the same place, so this
1296          isn't as strange as it seems.  */
1297       while (num != 0)
1298         {
1299           if (num < 0)
1300             {
1301               printf_filtered ("It stopped at a breakpoint that has ");
1302               printf_filtered ("since been deleted.\n");
1303             }
1304           else
1305             printf_filtered ("It stopped at breakpoint %d.\n", num);
1306           num = bpstat_num (&bs);
1307         }
1308     }
1309   else if (stop_signal != TARGET_SIGNAL_0)
1310     {
1311       printf_filtered ("It stopped with signal %s, %s.\n",
1312                        target_signal_to_name (stop_signal),
1313                        target_signal_to_string (stop_signal));
1314     }
1315
1316   if (!from_tty)
1317     {
1318       printf_filtered ("Type \"info stack\" or \"info registers\" ");
1319       printf_filtered ("for more information.\n");
1320     }
1321 }
1322 \f
1323 static void
1324 environment_info (var, from_tty)
1325      char *var;
1326      int from_tty;
1327 {
1328   if (var)
1329     {
1330       register char *val = get_in_environ (inferior_environ, var);
1331       if (val)
1332         {
1333           puts_filtered (var);
1334           puts_filtered (" = ");
1335           puts_filtered (val);
1336           puts_filtered ("\n");
1337         }
1338       else
1339         {
1340           puts_filtered ("Environment variable \"");
1341           puts_filtered (var);
1342           puts_filtered ("\" not defined.\n");
1343         }
1344     }
1345   else
1346     {
1347       register char **vector = environ_vector (inferior_environ);
1348       while (*vector)
1349         {
1350           puts_filtered (*vector++);
1351           puts_filtered ("\n");
1352         }
1353     }
1354 }
1355
1356 static void
1357 set_environment_command (arg, from_tty)
1358      char *arg;
1359      int from_tty;
1360 {
1361   register char *p, *val, *var;
1362   int nullset = 0;
1363
1364   if (arg == 0)
1365     error_no_arg ("environment variable and value");
1366
1367   /* Find seperation between variable name and value */
1368   p = (char *) strchr (arg, '=');
1369   val = (char *) strchr (arg, ' ');
1370
1371   if (p != 0 && val != 0)
1372     {
1373       /* We have both a space and an equals.  If the space is before the
1374          equals, walk forward over the spaces til we see a nonspace 
1375          (possibly the equals). */
1376       if (p > val)
1377         while (*val == ' ')
1378           val++;
1379
1380       /* Now if the = is after the char following the spaces,
1381          take the char following the spaces.  */
1382       if (p > val)
1383         p = val - 1;
1384     }
1385   else if (val != 0 && p == 0)
1386     p = val;
1387
1388   if (p == arg)
1389     error_no_arg ("environment variable to set");
1390
1391   if (p == 0 || p[1] == 0)
1392     {
1393       nullset = 1;
1394       if (p == 0)
1395         p = arg + strlen (arg); /* So that savestring below will work */
1396     }
1397   else
1398     {
1399       /* Not setting variable value to null */
1400       val = p + 1;
1401       while (*val == ' ' || *val == '\t')
1402         val++;
1403     }
1404
1405   while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
1406     p--;
1407
1408   var = savestring (arg, p - arg);
1409   if (nullset)
1410     {
1411       printf_filtered ("Setting environment variable ");
1412       printf_filtered ("\"%s\" to null value.\n", var);
1413       set_in_environ (inferior_environ, var, "");
1414     }
1415   else
1416     set_in_environ (inferior_environ, var, val);
1417   free (var);
1418 }
1419
1420 static void
1421 unset_environment_command (var, from_tty)
1422      char *var;
1423      int from_tty;
1424 {
1425   if (var == 0)
1426     {
1427       /* If there is no argument, delete all environment variables.
1428          Ask for confirmation if reading from the terminal.  */
1429       if (!from_tty || query ("Delete all environment variables? "))
1430         {
1431           free_environ (inferior_environ);
1432           inferior_environ = make_environ ();
1433         }
1434     }
1435   else
1436     unset_in_environ (inferior_environ, var);
1437 }
1438
1439 /* Handle the execution path (PATH variable) */
1440
1441 static const char path_var_name[] = "PATH";
1442
1443 /* ARGSUSED */
1444 static void
1445 path_info (args, from_tty)
1446      char *args;
1447      int from_tty;
1448 {
1449   puts_filtered ("Executable and object file path: ");
1450   puts_filtered (get_in_environ (inferior_environ, path_var_name));
1451   puts_filtered ("\n");
1452 }
1453
1454 /* Add zero or more directories to the front of the execution path.  */
1455
1456 static void
1457 path_command (dirname, from_tty)
1458      char *dirname;
1459      int from_tty;
1460 {
1461   char *exec_path;
1462   char *env;
1463   dont_repeat ();
1464   env = get_in_environ (inferior_environ, path_var_name);
1465   /* Can be null if path is not set */
1466   if (!env)
1467     env = "";
1468   exec_path = strsave (env);
1469   mod_path (dirname, &exec_path);
1470   set_in_environ (inferior_environ, path_var_name, exec_path);
1471   free (exec_path);
1472   if (from_tty)
1473     path_info ((char *) NULL, from_tty);
1474 }
1475 \f
1476
1477 #ifdef REGISTER_NAMES
1478 char *gdb_register_names[] = REGISTER_NAMES;
1479 #endif
1480 /* Print out the machine register regnum. If regnum is -1,
1481    print all registers (fpregs == 1) or all non-float registers
1482    (fpregs == 0).
1483
1484    For most machines, having all_registers_info() print the
1485    register(s) one per line is good enough. If a different format
1486    is required, (eg, for MIPS or Pyramid 90x, which both have
1487    lots of regs), or there is an existing convention for showing
1488    all the registers, define the macro DO_REGISTERS_INFO(regnum, fp)
1489    to provide that format.  */
1490
1491 #if !defined (DO_REGISTERS_INFO)
1492
1493 #define DO_REGISTERS_INFO(regnum, fp) do_registers_info(regnum, fp)
1494
1495 static void
1496 do_registers_info (regnum, fpregs)
1497      int regnum;
1498      int fpregs;
1499 {
1500   register int i;
1501   int numregs = ARCH_NUM_REGS;
1502
1503   for (i = 0; i < numregs; i++)
1504     {
1505       char raw_buffer[MAX_REGISTER_RAW_SIZE];
1506       char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
1507
1508       /* Decide between printing all regs, nonfloat regs, or specific reg.  */
1509       if (regnum == -1)
1510         {
1511           if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT && !fpregs)
1512             continue;
1513         }
1514       else
1515         {
1516           if (i != regnum)
1517             continue;
1518         }
1519
1520       /* If the register name is empty, it is undefined for this
1521          processor, so don't display anything.  */
1522       if (REGISTER_NAME (i) == NULL || *(REGISTER_NAME (i)) == '\0')
1523         continue;
1524
1525       fputs_filtered (REGISTER_NAME (i), gdb_stdout);
1526       print_spaces_filtered (15 - strlen (REGISTER_NAME (i)), gdb_stdout);
1527
1528       /* Get the data in raw format.  */
1529       if (read_relative_register_raw_bytes (i, raw_buffer))
1530         {
1531           printf_filtered ("*value not available*\n");
1532           continue;
1533         }
1534
1535       /* Convert raw data to virtual format if necessary.  */
1536       if (REGISTER_CONVERTIBLE (i))
1537         {
1538           REGISTER_CONVERT_TO_VIRTUAL (i, REGISTER_VIRTUAL_TYPE (i),
1539                                        raw_buffer, virtual_buffer);
1540         }
1541       else
1542         {
1543           memcpy (virtual_buffer, raw_buffer,
1544                   REGISTER_VIRTUAL_SIZE (i));
1545         }
1546
1547       /* If virtual format is floating, print it that way, and in raw hex.  */
1548       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT)
1549         {
1550           register int j;
1551
1552 #ifdef INVALID_FLOAT
1553           if (INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
1554             printf_filtered ("<invalid float>");
1555           else
1556 #endif
1557             val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0,
1558                        gdb_stdout, 0, 1, 0, Val_pretty_default);
1559
1560           printf_filtered ("\t(raw 0x");
1561           for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
1562             {
1563               register int idx = TARGET_BYTE_ORDER == BIG_ENDIAN ? j
1564               : REGISTER_RAW_SIZE (i) - 1 - j;
1565               printf_filtered ("%02x", (unsigned char) raw_buffer[idx]);
1566             }
1567           printf_filtered (")");
1568         }
1569
1570 /* FIXME!  val_print probably can handle all of these cases now...  */
1571
1572       /* Else if virtual format is too long for printf,
1573          print in hex a byte at a time.  */
1574       else if (REGISTER_VIRTUAL_SIZE (i) > (int) sizeof (long))
1575         {
1576           register int j;
1577           printf_filtered ("0x");
1578           for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
1579             printf_filtered ("%02x", (unsigned char) virtual_buffer[j]);
1580         }
1581       /* Else print as integer in hex and in decimal.  */
1582       else
1583         {
1584           val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0,
1585                      gdb_stdout, 'x', 1, 0, Val_pretty_default);
1586           printf_filtered ("\t");
1587           val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0,
1588                      gdb_stdout, 0, 1, 0, Val_pretty_default);
1589         }
1590
1591       /* The SPARC wants to print even-numbered float regs as doubles
1592          in addition to printing them as floats.  */
1593 #ifdef PRINT_REGISTER_HOOK
1594       PRINT_REGISTER_HOOK (i);
1595 #endif
1596
1597       printf_filtered ("\n");
1598     }
1599 }
1600 #endif /* no DO_REGISTERS_INFO.  */
1601
1602 void
1603 registers_info (addr_exp, fpregs)
1604      char *addr_exp;
1605      int fpregs;
1606 {
1607   int regnum, numregs;
1608   register char *end;
1609
1610   if (!target_has_registers)
1611     error ("The program has no registers now.");
1612   if (selected_frame == NULL)
1613     error ("No selected frame.");
1614
1615   if (!addr_exp)
1616     {
1617       DO_REGISTERS_INFO (-1, fpregs);
1618       return;
1619     }
1620
1621   do
1622     {
1623       if (addr_exp[0] == '$')
1624         addr_exp++;
1625       end = addr_exp;
1626       while (*end != '\0' && *end != ' ' && *end != '\t')
1627         ++end;
1628       numregs = ARCH_NUM_REGS;
1629
1630       regnum = target_map_name_to_register (addr_exp, end - addr_exp);
1631       if (regnum >= 0)
1632         goto found;
1633
1634       regnum = numregs;
1635
1636       if (*addr_exp >= '0' && *addr_exp <= '9')
1637         regnum = atoi (addr_exp);       /* Take a number */
1638       if (regnum >= numregs)    /* Bad name, or bad number */
1639         error ("%.*s: invalid register", end - addr_exp, addr_exp);
1640
1641     found:
1642       DO_REGISTERS_INFO (regnum, fpregs);
1643
1644       addr_exp = end;
1645       while (*addr_exp == ' ' || *addr_exp == '\t')
1646         ++addr_exp;
1647     }
1648   while (*addr_exp != '\0');
1649 }
1650
1651 void
1652 all_registers_info (addr_exp, from_tty)
1653      char *addr_exp;
1654      int from_tty;
1655 {
1656   registers_info (addr_exp, 1);
1657 }
1658
1659 void
1660 nofp_registers_info (addr_exp, from_tty)
1661      char *addr_exp;
1662      int from_tty;
1663 {
1664   registers_info (addr_exp, 0);
1665 }
1666 \f
1667
1668 /*
1669  * TODO:
1670  * Should save/restore the tty state since it might be that the
1671  * program to be debugged was started on this tty and it wants
1672  * the tty in some state other than what we want.  If it's running
1673  * on another terminal or without a terminal, then saving and
1674  * restoring the tty state is a harmless no-op.
1675  * This only needs to be done if we are attaching to a process.
1676  */
1677
1678 /*
1679    attach_command --
1680    takes a program started up outside of gdb and ``attaches'' to it.
1681    This stops it cold in its tracks and allows us to start debugging it.
1682    and wait for the trace-trap that results from attaching.  */
1683
1684 void
1685 attach_command (args, from_tty)
1686      char *args;
1687      int from_tty;
1688 {
1689 #ifdef SOLIB_ADD
1690   extern int auto_solib_add;
1691 #endif
1692
1693   char *exec_file;
1694   char *full_exec_path = NULL;
1695
1696   dont_repeat ();               /* Not for the faint of heart */
1697
1698   if (target_has_execution)
1699     {
1700       if (query ("A program is being debugged already.  Kill it? "))
1701         target_kill ();
1702       else
1703         error ("Not killed.");
1704     }
1705
1706   target_attach (args, from_tty);
1707
1708   /* Set up the "saved terminal modes" of the inferior
1709      based on what modes we are starting it with.  */
1710   target_terminal_init ();
1711
1712   /* Install inferior's terminal modes.  */
1713   target_terminal_inferior ();
1714
1715   /* Set up execution context to know that we should return from
1716      wait_for_inferior as soon as the target reports a stop.  */
1717   init_wait_for_inferior ();
1718   clear_proceed_status ();
1719   stop_soon_quietly = 1;
1720
1721   /* No traps are generated when attaching to inferior under Mach 3
1722      or GNU hurd.  */
1723 #ifndef ATTACH_NO_WAIT
1724   wait_for_inferior ();
1725 #endif
1726
1727   /*
1728    * If no exec file is yet known, try to determine it from the
1729    * process itself.
1730    */
1731   exec_file = (char *) get_exec_file (0);
1732   if (!exec_file)
1733     {
1734       exec_file = target_pid_to_exec_file (inferior_pid);
1735       if (exec_file)
1736         {
1737           /* It's possible we don't have a full path, but rather just a
1738              filename.  Some targets, such as HP-UX, don't provide the
1739              full path, sigh.
1740
1741              Attempt to qualify the filename against the source path.
1742              (If that fails, we'll just fall back on the original
1743              filename.  Not much more we can do...)
1744            */
1745           if (!source_full_path_of (exec_file, &full_exec_path))
1746             full_exec_path = savestring (exec_file, strlen (exec_file));
1747
1748           exec_file_attach (full_exec_path, from_tty);
1749           symbol_file_command (full_exec_path, from_tty);
1750         }
1751     }
1752
1753 #ifdef SOLIB_ADD
1754   if (auto_solib_add)
1755     {
1756       /* Add shared library symbols from the newly attached process, if any.  */
1757       SOLIB_ADD ((char *) 0, from_tty, &current_target);
1758       re_enable_breakpoints_in_shlibs ();
1759     }
1760 #endif
1761
1762   /* Take any necessary post-attaching actions for this platform.
1763    */
1764   target_post_attach (inferior_pid);
1765
1766   normal_stop ();
1767
1768   if (attach_hook)
1769     attach_hook ();
1770 }
1771
1772 /*
1773  * detach_command --
1774  * takes a program previously attached to and detaches it.
1775  * The program resumes execution and will no longer stop
1776  * on signals, etc.  We better not have left any breakpoints
1777  * in the program or it'll die when it hits one.  For this
1778  * to work, it may be necessary for the process to have been
1779  * previously attached.  It *might* work if the program was
1780  * started via the normal ptrace (PTRACE_TRACEME).
1781  */
1782
1783 static void
1784 detach_command (args, from_tty)
1785      char *args;
1786      int from_tty;
1787 {
1788   dont_repeat ();               /* Not for the faint of heart */
1789   target_detach (args, from_tty);
1790 #if defined(SOLIB_RESTART)
1791   SOLIB_RESTART ();
1792 #endif
1793   if (detach_hook)
1794     detach_hook ();
1795 }
1796
1797 /* Stop the execution of the target while running in async mode, in
1798    the backgound. */
1799 #ifdef UI_OUT
1800 void
1801 interrupt_target_command_wrapper (args, from_tty)
1802      char *args;
1803      int from_tty;
1804 {
1805   interrupt_target_command (args, from_tty);
1806 }
1807 #endif
1808 static void
1809 interrupt_target_command (args, from_tty)
1810      char *args;
1811      int from_tty;
1812 {
1813   if (event_loop_p && target_can_async_p ())
1814     {
1815       dont_repeat ();           /* Not for the faint of heart */
1816       target_stop ();
1817     }
1818 }
1819
1820 /* ARGSUSED */
1821 static void
1822 float_info (addr_exp, from_tty)
1823      char *addr_exp;
1824      int from_tty;
1825 {
1826 #ifdef FLOAT_INFO
1827   FLOAT_INFO;
1828 #else
1829   printf_filtered ("No floating point info available for this processor.\n");
1830 #endif
1831 }
1832 \f
1833 /* ARGSUSED */
1834 static void
1835 unset_command (args, from_tty)
1836      char *args;
1837      int from_tty;
1838 {
1839   printf_filtered ("\"unset\" must be followed by the name of ");
1840   printf_filtered ("an unset subcommand.\n");
1841   help_list (unsetlist, "unset ", -1, gdb_stdout);
1842 }
1843
1844 void
1845 _initialize_infcmd ()
1846 {
1847   struct cmd_list_element *c;
1848
1849   add_com ("tty", class_run, tty_command,
1850            "Set terminal for future runs of program being debugged.");
1851
1852   add_show_from_set
1853     (add_set_cmd ("args", class_run, var_string_noescape,
1854                   (char *) &inferior_args,
1855                   "Set argument list to give program being debugged when it is started.\n\
1856 Follow this command with any number of args, to be passed to the program.",
1857                   &setlist),
1858      &showlist);
1859
1860   c = add_cmd
1861     ("environment", no_class, environment_info,
1862      "The environment to give the program, or one variable's value.\n\
1863 With an argument VAR, prints the value of environment variable VAR to\n\
1864 give the program being debugged.  With no arguments, prints the entire\n\
1865 environment to be given to the program.", &showlist);
1866   c->completer = noop_completer;
1867
1868   add_prefix_cmd ("unset", no_class, unset_command,
1869                   "Complement to certain \"set\" commands",
1870                   &unsetlist, "unset ", 0, &cmdlist);
1871
1872   c = add_cmd ("environment", class_run, unset_environment_command,
1873                "Cancel environment variable VAR for the program.\n\
1874 This does not affect the program until the next \"run\" command.",
1875                &unsetlist);
1876   c->completer = noop_completer;
1877
1878   c = add_cmd ("environment", class_run, set_environment_command,
1879                "Set environment variable value to give the program.\n\
1880 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
1881 VALUES of environment variables are uninterpreted strings.\n\
1882 This does not affect the program until the next \"run\" command.",
1883                &setlist);
1884   c->completer = noop_completer;
1885
1886   add_com ("path", class_files, path_command,
1887            "Add directory DIR(s) to beginning of search path for object files.\n\
1888 $cwd in the path means the current working directory.\n\
1889 This path is equivalent to the $PATH shell variable.  It is a list of\n\
1890 directories, separated by colons.  These directories are searched to find\n\
1891 fully linked executable files and separately compiled object files as needed.");
1892
1893   c = add_cmd ("paths", no_class, path_info,
1894                "Current search path for finding object files.\n\
1895 $cwd in the path means the current working directory.\n\
1896 This path is equivalent to the $PATH shell variable.  It is a list of\n\
1897 directories, separated by colons.  These directories are searched to find\n\
1898 fully linked executable files and separately compiled object files as needed.",
1899                &showlist);
1900   c->completer = noop_completer;
1901
1902   add_com ("attach", class_run, attach_command,
1903            "Attach to a process or file outside of GDB.\n\
1904 This command attaches to another target, of the same type as your last\n\
1905 \"target\" command (\"info files\" will show your target stack).\n\
1906 The command may take as argument a process id or a device file.\n\
1907 For a process id, you must have permission to send the process a signal,\n\
1908 and it must have the same effective uid as the debugger.\n\
1909 When using \"attach\" with a process id, the debugger finds the\n\
1910 program running in the process, looking first in the current working\n\
1911 directory, or (if not found there) using the source file search path\n\
1912 (see the \"directory\" command).  You can also use the \"file\" command\n\
1913 to specify the program, and to load its symbol table.");
1914
1915   add_com ("detach", class_run, detach_command,
1916            "Detach a process or file previously attached.\n\
1917 If a process, it is no longer traced, and it continues its execution.  If\n\
1918 you were debugging a file, the file is closed and gdb no longer accesses it.");
1919
1920   add_com ("signal", class_run, signal_command,
1921            "Continue program giving it signal specified by the argument.\n\
1922 An argument of \"0\" means continue program without giving it a signal.");
1923
1924   add_com ("stepi", class_run, stepi_command,
1925            "Step one instruction exactly.\n\
1926 Argument N means do this N times (or till program stops for another reason).");
1927   add_com_alias ("si", "stepi", class_alias, 0);
1928
1929   add_com ("nexti", class_run, nexti_command,
1930            "Step one instruction, but proceed through subroutine calls.\n\
1931 Argument N means do this N times (or till program stops for another reason).");
1932   add_com_alias ("ni", "nexti", class_alias, 0);
1933
1934   add_com ("finish", class_run, finish_command,
1935            "Execute until selected stack frame returns.\n\
1936 Upon return, the value returned is printed and put in the value history.");
1937
1938   add_com ("next", class_run, next_command,
1939            "Step program, proceeding through subroutine calls.\n\
1940 Like the \"step\" command as long as subroutine calls do not happen;\n\
1941 when they do, the call is treated as one instruction.\n\
1942 Argument N means do this N times (or till program stops for another reason).");
1943   add_com_alias ("n", "next", class_run, 1);
1944   if (xdb_commands)
1945     add_com_alias ("S", "next", class_run, 1);
1946
1947   add_com ("step", class_run, step_command,
1948            "Step program until it reaches a different source line.\n\
1949 Argument N means do this N times (or till program stops for another reason).");
1950   add_com_alias ("s", "step", class_run, 1);
1951
1952   add_com ("until", class_run, until_command,
1953            "Execute until the program reaches a source line greater than the current\n\
1954 or a specified line or address or function (same args as break command).\n\
1955 Execution will also stop upon exit from the current stack frame.");
1956   add_com_alias ("u", "until", class_run, 1);
1957
1958   add_com ("jump", class_run, jump_command,
1959            "Continue program being debugged at specified line or address.\n\
1960 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1961 for an address to start at.");
1962
1963   if (xdb_commands)
1964     add_com ("go", class_run, go_command,
1965              "Usage: go <location>\n\
1966 Continue program being debugged, stopping at specified line or \n\
1967 address.\n\
1968 Give as argument either LINENUM or *ADDR, where ADDR is an \n\
1969 expression for an address to start at.\n\
1970 This command is a combination of tbreak and jump.");
1971
1972   if (xdb_commands)
1973     add_com_alias ("g", "go", class_run, 1);
1974
1975   add_com ("continue", class_run, continue_command,
1976            "Continue program being debugged, after signal or breakpoint.\n\
1977 If proceeding from breakpoint, a number N may be used as an argument,\n\
1978 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
1979 the breakpoint won't break until the Nth time it is reached).");
1980   add_com_alias ("c", "cont", class_run, 1);
1981   add_com_alias ("fg", "cont", class_run, 1);
1982
1983   add_com ("run", class_run, run_command,
1984            "Start debugged program.  You may specify arguments to give it.\n\
1985 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
1986 Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
1987 With no arguments, uses arguments last specified (with \"run\" or \"set args\").\n\
1988 To cancel previous arguments and run with no arguments,\n\
1989 use \"set args\" without arguments.");
1990   add_com_alias ("r", "run", class_run, 1);
1991   if (xdb_commands)
1992     add_com ("R", class_run, run_no_args_command,
1993              "Start debugged program with no arguments.");
1994
1995   add_com ("interrupt", class_run, interrupt_target_command,
1996            "Interrupt the execution of the debugged program.");
1997
1998   add_info ("registers", nofp_registers_info,
1999             "List of integer registers and their contents, for selected stack frame.\n\
2000 Register name as argument means describe only that register.");
2001   add_info_alias ("r", "registers", 1);
2002
2003   if (xdb_commands)
2004     add_com ("lr", class_info, nofp_registers_info,
2005              "List of integer registers and their contents, for selected stack frame.\n\
2006   Register name as argument means describe only that register.");
2007   add_info ("all-registers", all_registers_info,
2008             "List of all registers and their contents, for selected stack frame.\n\
2009 Register name as argument means describe only that register.");
2010
2011   add_info ("program", program_info,
2012             "Execution status of the program.");
2013
2014   add_info ("float", float_info,
2015             "Print the status of the floating point unit\n");
2016
2017   inferior_args = savestring ("", 1);   /* Initially no args */
2018   inferior_environ = make_environ ();
2019   init_environ (inferior_environ);
2020 }