* mn10300.igen (OP_F0F4): Need to load contents of register AN0
[platform/upstream/binutils.git] / gdb / infcmd.c
1 /* Memory-access and commands for "inferior" process, for GDB.
2    Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1995, 1996 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include <signal.h>
22 #include <sys/param.h>
23 #include "gdb_string.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "frame.h"
27 #include "inferior.h"
28 #include "environ.h"
29 #include "value.h"
30 #include "gdbcmd.h"
31 #include "gdbcore.h"
32 #include "target.h"
33 #include "language.h"
34
35 static void continue_command PARAMS ((char *, int));
36
37 static void until_next_command PARAMS ((int));
38
39 static void until_command PARAMS ((char *, int));
40
41 static void path_info PARAMS ((char *, int));
42
43 static void path_command PARAMS ((char *, int));
44
45 static void unset_command PARAMS ((char *, int));
46
47 static void float_info PARAMS ((char *, int));
48
49 static void detach_command PARAMS ((char *, int));
50
51 static void nofp_registers_info PARAMS ((char *, int));
52
53 static void all_registers_info PARAMS ((char *, int));
54
55 static void registers_info PARAMS ((char *, int));
56
57 #if !defined (DO_REGISTERS_INFO)
58 static void do_registers_info PARAMS ((int, int));
59 #endif
60
61 static void unset_environment_command PARAMS ((char *, int));
62
63 static void set_environment_command PARAMS ((char *, int));
64
65 static void environment_info PARAMS ((char *, int));
66
67 static void program_info PARAMS ((char *, int));
68
69 static void finish_command PARAMS ((char *, int));
70
71 static void signal_command PARAMS ((char *, int));
72
73 static void jump_command PARAMS ((char *, int));
74
75 static void step_1 PARAMS ((int, int, char *));
76
77 static void nexti_command PARAMS ((char *, int));
78
79 static void stepi_command PARAMS ((char *, int));
80
81 static void next_command PARAMS ((char *, int));
82
83 static void step_command PARAMS ((char *, int));
84
85 static void run_command PARAMS ((char *, int));
86
87 #ifdef CALL_DUMMY_BREAKPOINT_OFFSET
88 static void breakpoint_auto_delete_contents PARAMS ((PTR));
89 #endif
90
91 #define ERROR_NO_INFERIOR \
92    if (!target_has_execution) error ("The program is not being run.");
93
94 /* String containing arguments to give to the program, separated by spaces.
95    Empty string (pointer to '\0') means no args.  */
96
97 static char *inferior_args;
98
99 /* File name for default use for standard in/out in the inferior.  */
100
101 char *inferior_io_terminal;
102
103 /* Pid of our debugged inferior, or 0 if no inferior now.
104    Since various parts of infrun.c test this to see whether there is a program
105    being debugged it should be nonzero (currently 3 is used) for remote
106    debugging.  */
107
108 int inferior_pid;
109
110 /* Last signal that the inferior received (why it stopped).  */
111
112 enum target_signal stop_signal;
113
114 /* Address at which inferior stopped.  */
115
116 CORE_ADDR stop_pc;
117
118 /* Chain containing status of breakpoint(s) that we have stopped at.  */
119
120 bpstat stop_bpstat;
121
122 /* Flag indicating that a command has proceeded the inferior past the
123    current breakpoint.  */
124
125 int breakpoint_proceeded;
126
127 /* Nonzero if stopped due to a step command.  */
128
129 int stop_step;
130
131 /* Nonzero if stopped due to completion of a stack dummy routine.  */
132
133 int stop_stack_dummy;
134
135 /* Nonzero if stopped due to a random (unexpected) signal in inferior
136    process.  */
137
138 int stopped_by_random_signal;
139
140 /* Range to single step within.
141    If this is nonzero, respond to a single-step signal
142    by continuing to step if the pc is in this range.  */
143
144 CORE_ADDR step_range_start; /* Inclusive */
145 CORE_ADDR step_range_end; /* Exclusive */
146
147 /* Stack frame address as of when stepping command was issued.
148    This is how we know when we step into a subroutine call,
149    and how to set the frame for the breakpoint used to step out.  */
150
151 CORE_ADDR step_frame_address;
152
153 /* Our notion of the current stack pointer.  */
154
155 CORE_ADDR step_sp;
156
157 /* 1 means step over all subroutine calls.
158    0 means don't step over calls (used by stepi).
159    -1 means step over calls to undebuggable functions.  */
160
161 int step_over_calls;
162
163 /* If stepping, nonzero means step count is > 1
164    so don't print frame next time inferior stops
165    if it stops due to stepping.  */
166
167 int step_multi;
168
169 /* Environment to use for running inferior,
170    in format described in environ.h.  */
171
172 struct environ *inferior_environ;
173
174 \f
175 /* ARGSUSED */
176 void
177 tty_command (file, from_tty)
178      char *file;
179      int from_tty;
180 {
181   if (file == 0)
182     error_no_arg ("terminal name for running target process");
183
184   inferior_io_terminal = savestring (file, strlen (file));
185 }
186
187 static void
188 run_command (args, from_tty)
189      char *args;
190      int from_tty;
191 {
192   char *exec_file;
193
194   dont_repeat ();
195
196   if (inferior_pid != 0 && target_has_execution)
197     {
198       if (
199           !query ("The program being debugged has been started already.\n\
200 Start it from the beginning? "))
201         error ("Program not restarted.");
202       target_kill ();
203     }
204
205   clear_breakpoint_hit_counts ();
206
207   exec_file = (char *) get_exec_file (0);
208
209   /* The exec file is re-read every time we do a generic_mourn_inferior, so
210      we just have to worry about the symbol file.  */
211   reread_symbols ();
212
213   /* We keep symbols from add-symbol-file, on the grounds that the
214      user might want to add some symbols before running the program
215      (right?).  But sometimes (dynamic loading where the user manually
216      introduces the new symbols with add-symbol-file), the code which
217      the symbols describe does not persist between runs.  Currently
218      the user has to manually nuke all symbols between runs if they
219      want them to go away (PR 2207).  This is probably reasonable.  */
220
221   if (args)
222     {
223       char *cmd;
224       cmd = concat ("set args ", args, NULL);
225       make_cleanup (free, cmd);
226       execute_command (cmd, from_tty);
227     }
228
229   if (from_tty)
230     {
231       puts_filtered("Starting program: ");
232       if (exec_file)
233         puts_filtered(exec_file);
234       puts_filtered(" ");
235       puts_filtered(inferior_args);
236       puts_filtered("\n");
237       gdb_flush (gdb_stdout);
238     }
239
240   target_create_inferior (exec_file, inferior_args,
241                           environ_vector (inferior_environ));
242 }
243 \f
244 static void
245 continue_command (proc_count_exp, from_tty)
246      char *proc_count_exp;
247      int from_tty;
248 {
249   ERROR_NO_INFERIOR;
250
251   /* If have argument, set proceed count of breakpoint we stopped at.  */
252
253   if (proc_count_exp != NULL)
254     {
255       bpstat bs = stop_bpstat;
256       int num = bpstat_num (&bs);
257       if (num == 0 && from_tty)
258         {
259           printf_filtered
260             ("Not stopped at any breakpoint; argument ignored.\n");
261         }
262       while (num != 0)
263         {
264           set_ignore_count (num,
265                             parse_and_eval_address (proc_count_exp) - 1,
266                             from_tty);
267           /* set_ignore_count prints a message ending with a period.
268              So print two spaces before "Continuing.".  */
269           if (from_tty)
270             printf_filtered ("  ");
271           num = bpstat_num (&bs);
272         }
273     }
274
275   if (from_tty)
276     printf_filtered ("Continuing.\n");
277
278   clear_proceed_status ();
279
280   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
281 }
282 \f
283 /* Step until outside of current statement.  */
284
285 /* ARGSUSED */
286 static void
287 step_command (count_string, from_tty)
288      char *count_string;
289      int from_tty;
290 {
291   step_1 (0, 0, count_string);
292 }
293
294 /* Likewise, but skip over subroutine calls as if single instructions.  */
295
296 /* ARGSUSED */
297 static void
298 next_command (count_string, from_tty)
299      char *count_string;
300      int from_tty;
301 {
302   step_1 (1, 0, count_string);
303 }
304
305 /* Likewise, but step only one instruction.  */
306
307 /* ARGSUSED */
308 static void
309 stepi_command (count_string, from_tty)
310      char *count_string;
311      int from_tty;
312 {
313   step_1 (0, 1, count_string);
314 }
315
316 /* ARGSUSED */
317 static void
318 nexti_command (count_string, from_tty)
319      char *count_string;
320      int from_tty;
321 {
322   step_1 (1, 1, count_string);
323 }
324
325 static void
326 step_1 (skip_subroutines, single_inst, count_string)
327      int skip_subroutines;
328      int single_inst;
329      char *count_string;
330 {
331   register int count = 1;
332   struct frame_info *frame;
333   struct cleanup *cleanups = 0;
334
335   ERROR_NO_INFERIOR;
336   count = count_string ? parse_and_eval_address (count_string) : 1;
337
338   if (!single_inst || skip_subroutines) /* leave si command alone */
339     {
340       enable_longjmp_breakpoint();
341       cleanups = make_cleanup(disable_longjmp_breakpoint, 0);
342     }
343
344   for (; count > 0; count--)
345     {
346       clear_proceed_status ();
347
348       frame = get_current_frame ();
349       if (!frame)                       /* Avoid coredump here.  Why tho? */
350         error ("No current frame");
351       step_frame_address = FRAME_FP (frame);
352       step_sp = read_sp ();
353
354       if (! single_inst)
355         {
356           find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
357           if (step_range_end == 0)
358             {
359               char *name;
360               if (find_pc_partial_function (stop_pc, &name, &step_range_start,
361                                             &step_range_end) == 0)
362                 error ("Cannot find bounds of current function");
363
364               target_terminal_ours ();
365               printf_filtered ("\
366 Single stepping until exit from function %s, \n\
367 which has no line number information.\n", name);
368             }
369         }
370       else
371         {
372           /* Say we are stepping, but stop after one insn whatever it does.  */
373           step_range_start = step_range_end = 1;
374           if (!skip_subroutines)
375             /* It is stepi.
376                Don't step over function calls, not even to functions lacking
377                line numbers.  */
378             step_over_calls = 0;
379         }
380
381       if (skip_subroutines)
382         step_over_calls = 1;
383
384       step_multi = (count > 1);
385       proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
386       if (! stop_step)
387         break;
388
389       /* FIXME: On nexti, this may have already been done (when we hit the
390          step resume break, I think).  Probably this should be moved to
391          wait_for_inferior (near the top).  */
392 #if defined (SHIFT_INST_REGS)
393       SHIFT_INST_REGS();
394 #endif
395     }
396
397   if (!single_inst || skip_subroutines)
398     do_cleanups(cleanups);
399 }
400 \f
401 /* Continue program at specified address.  */
402
403 static void
404 jump_command (arg, from_tty)
405      char *arg;
406      int from_tty;
407 {
408   register CORE_ADDR addr;
409   struct symtabs_and_lines sals;
410   struct symtab_and_line sal;
411   struct symbol *fn;
412   struct symbol *sfn;
413
414   ERROR_NO_INFERIOR;
415
416   if (!arg)
417     error_no_arg ("starting address");
418
419   sals = decode_line_spec_1 (arg, 1);
420   if (sals.nelts != 1)
421     {
422       error ("Unreasonable jump request");
423     }
424
425   sal = sals.sals[0];
426   free ((PTR)sals.sals);
427
428   if (sal.symtab == 0 && sal.pc == 0)
429     error ("No source file has been specified.");
430
431   resolve_sal_pc (&sal);                        /* May error out */
432
433   /* See if we are trying to jump to another function. */
434   fn = get_frame_function (get_current_frame ());
435   sfn = find_pc_function (sal.pc);
436   if (fn != NULL && sfn != fn)
437     {
438       if (!query ("Line %d is not in `%s'.  Jump anyway? ", sal.line,
439                   SYMBOL_SOURCE_NAME (fn)))
440         {
441           error ("Not confirmed.");
442           /* NOTREACHED */
443         }
444     }
445
446   addr = sal.pc;
447
448   if (from_tty)
449     {
450       printf_filtered ("Continuing at ");
451       print_address_numeric (addr, 1, gdb_stdout);
452       printf_filtered (".\n");
453     }
454
455   clear_proceed_status ();
456   proceed (addr, TARGET_SIGNAL_0, 0);
457 }
458
459 /* Continue program giving it specified signal.  */
460
461 static void
462 signal_command (signum_exp, from_tty)
463      char *signum_exp;
464      int from_tty;
465 {
466   enum target_signal oursig;
467
468   dont_repeat ();               /* Too dangerous.  */
469   ERROR_NO_INFERIOR;
470
471   if (!signum_exp)
472     error_no_arg ("signal number");
473
474   /* It would be even slicker to make signal names be valid expressions,
475      (the type could be "enum $signal" or some such), then the user could
476      assign them to convenience variables.  */
477   oursig = target_signal_from_name (signum_exp);
478
479   if (oursig == TARGET_SIGNAL_UNKNOWN)
480     {
481       /* No, try numeric.  */
482       int num = parse_and_eval_address (signum_exp);
483
484       if (num == 0)
485         oursig = TARGET_SIGNAL_0;
486       else
487         oursig = target_signal_from_command (num);
488     }
489
490   if (from_tty)
491     {
492       if (oursig == TARGET_SIGNAL_0)
493         printf_filtered ("Continuing with no signal.\n");
494       else
495         printf_filtered ("Continuing with signal %s.\n",
496                          target_signal_to_name (oursig));
497     }
498
499   clear_proceed_status ();
500   /* "signal 0" should not get stuck if we are stopped at a breakpoint.
501      FIXME: Neither should "signal foo" but when I tried passing
502      (CORE_ADDR)-1 unconditionally I got a testsuite failure which I haven't
503      tried to track down yet.  */
504   proceed (oursig == TARGET_SIGNAL_0 ? (CORE_ADDR) -1 : stop_pc, oursig, 0);
505 }
506
507 /* Call breakpoint_auto_delete on the current contents of the bpstat
508    pointed to by arg (which is really a bpstat *).  */
509
510 #ifdef CALL_DUMMY_BREAKPOINT_OFFSET
511
512 static void
513 breakpoint_auto_delete_contents (arg)
514      PTR arg;
515 {
516   breakpoint_auto_delete (*(bpstat *)arg);
517 }
518
519 #endif  /* CALL_DUMMY_BREAKPOINT_OFFSET */
520
521 /* Execute a "stack dummy", a piece of code stored in the stack
522    by the debugger to be executed in the inferior.
523
524    To call: first, do PUSH_DUMMY_FRAME.
525    Then push the contents of the dummy.  It should end with a breakpoint insn.
526    Then call here, passing address at which to start the dummy.
527
528    The contents of all registers are saved before the dummy frame is popped
529    and copied into the buffer BUFFER.
530
531    The dummy's frame is automatically popped whenever that break is hit.
532    If that is the first time the program stops, run_stack_dummy
533    returns to its caller with that frame already gone and returns 0.
534    Otherwise, run_stack-dummy returns 1 (the frame will eventually be popped
535    when we do hit that breakpoint).  */
536
537 /* DEBUG HOOK:  4 => return instead of letting the stack dummy run.  */
538
539 static int stack_dummy_testing = 0;
540
541 int
542 run_stack_dummy (addr, buffer)
543      CORE_ADDR addr;
544      char buffer[REGISTER_BYTES];
545 {
546   struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0);
547
548   /* Now proceed, having reached the desired place.  */
549   clear_proceed_status ();
550   if (stack_dummy_testing & 4)
551     {
552       POP_FRAME;
553       return(0);
554     }
555 #ifdef CALL_DUMMY_BREAKPOINT_OFFSET
556   {
557     struct breakpoint *bpt;
558     struct symtab_and_line sal;
559
560 #if CALL_DUMMY_LOCATION != AT_ENTRY_POINT
561     sal.pc = addr - CALL_DUMMY_START_OFFSET + CALL_DUMMY_BREAKPOINT_OFFSET;
562 #else
563     sal.pc = CALL_DUMMY_ADDRESS ();
564 #endif
565     sal.symtab = NULL;
566     sal.line = 0;
567
568     /* Set up a FRAME for the dummy frame so we can pass it to
569        set_momentary_breakpoint.  We need to give the breakpoint a
570        frame in case there is only one copy of the dummy (e.g.
571        CALL_DUMMY_LOCATION == AFTER_TEXT_END).  */
572     flush_cached_frames ();
573     set_current_frame (create_new_frame (read_fp (), sal.pc));
574
575     /* If defined, CALL_DUMMY_BREAKPOINT_OFFSET is where we need to put
576        a breakpoint instruction.  If not, the call dummy already has the
577        breakpoint instruction in it.
578
579        addr is the address of the call dummy plus the CALL_DUMMY_START_OFFSET,
580        so we need to subtract the CALL_DUMMY_START_OFFSET.  */
581     bpt = set_momentary_breakpoint (sal,
582                                     get_current_frame (),
583                                     bp_call_dummy);
584     bpt->disposition = del;
585
586     /* If all error()s out of proceed ended up calling normal_stop (and
587        perhaps they should; it already does in the special case of error
588        out of resume()), then we wouldn't need this.  */
589     make_cleanup (breakpoint_auto_delete_contents, &stop_bpstat);
590   }
591 #endif /* CALL_DUMMY_BREAKPOINT_OFFSET.  */
592
593   proceed_to_finish = 1;        /* We want stop_registers, please... */
594   proceed (addr, TARGET_SIGNAL_0, 0);
595
596   discard_cleanups (old_cleanups);
597
598   if (!stop_stack_dummy)
599     return 1;
600
601   /* On return, the stack dummy has been popped already.  */
602
603   memcpy (buffer, stop_registers, sizeof stop_registers);
604   return 0;
605 }
606 \f
607 /* Proceed until we reach a different source line with pc greater than
608    our current one or exit the function.  We skip calls in both cases.
609
610    Note that eventually this command should probably be changed so
611    that only source lines are printed out when we hit the breakpoint
612    we set.  This may involve changes to wait_for_inferior and the
613    proceed status code.  */
614
615 /* ARGSUSED */
616 static void
617 until_next_command (from_tty)
618      int from_tty;
619 {
620   struct frame_info *frame;
621   CORE_ADDR pc;
622   struct symbol *func;
623   struct symtab_and_line sal;
624  
625   clear_proceed_status ();
626
627   frame = get_current_frame ();
628
629   /* Step until either exited from this function or greater
630      than the current line (if in symbolic section) or pc (if
631      not). */
632
633   pc = read_pc ();
634   func = find_pc_function (pc);
635   
636   if (!func)
637     {
638       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
639       
640       if (msymbol == NULL)
641         error ("Execution is not within a known function.");
642       
643       step_range_start = SYMBOL_VALUE_ADDRESS (msymbol);
644       step_range_end = pc;
645     }
646   else
647     {
648       sal = find_pc_line (pc, 0);
649       
650       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
651       step_range_end = sal.end;
652     }
653   
654   step_over_calls = 1;
655   step_frame_address = FRAME_FP (frame);
656   step_sp = read_sp ();
657
658   step_multi = 0;               /* Only one call to proceed */
659   
660   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
661 }
662
663 static void 
664 until_command (arg, from_tty)
665      char *arg;
666      int from_tty;
667 {
668   if (!target_has_execution)
669     error ("The program is not running.");
670   if (arg)
671     until_break_command (arg, from_tty);
672   else
673     until_next_command (from_tty);
674 }
675 \f
676 /* "finish": Set a temporary breakpoint at the place
677    the selected frame will return to, then continue.  */
678
679 static void
680 finish_command (arg, from_tty)
681      char *arg;
682      int from_tty;
683 {
684   struct symtab_and_line sal;
685   register struct frame_info *frame;
686   register struct symbol *function;
687   struct breakpoint *breakpoint;
688   struct cleanup *old_chain;
689
690   if (arg)
691     error ("The \"finish\" command does not take any arguments.");
692   if (!target_has_execution)
693     error ("The program is not running.");
694   if (selected_frame == NULL)
695     error ("No selected frame.");
696
697   frame = get_prev_frame (selected_frame);
698   if (frame == 0)
699     error ("\"finish\" not meaningful in the outermost frame.");
700
701   clear_proceed_status ();
702
703   sal = find_pc_line (frame->pc, 0);
704   sal.pc = frame->pc;
705
706   breakpoint = set_momentary_breakpoint (sal, frame, bp_finish);
707
708   old_chain = make_cleanup(delete_breakpoint, breakpoint);
709
710   /* Find the function we will return from.  */
711
712   function = find_pc_function (selected_frame->pc);
713
714   /* Print info on the selected frame, including level number
715      but not source.  */
716   if (from_tty)
717     {
718       printf_filtered ("Run till exit from ");
719       print_stack_frame (selected_frame, selected_frame_level, 0);
720     }
721
722   proceed_to_finish = 1;                /* We want stop_registers, please... */
723   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
724
725   /* Did we stop at our breakpoint? */
726   if (bpstat_find_breakpoint(stop_bpstat, breakpoint) != NULL
727       && function != 0)
728     {
729       struct type *value_type;
730       register value_ptr val;
731       CORE_ADDR funcaddr;
732
733       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
734       if (!value_type)
735         fatal ("internal: finish_command: function has no target type");
736       
737       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
738         return;
739
740       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
741
742       val = value_being_returned (value_type, stop_registers,
743               using_struct_return (value_of_variable (function, NULL),
744                                    funcaddr,
745                                    check_typedef (value_type),
746                 BLOCK_GCC_COMPILED (SYMBOL_BLOCK_VALUE (function))));
747
748       printf_filtered ("Value returned is $%d = ", record_latest_value (val));
749       value_print (val, gdb_stdout, 0, Val_no_prettyprint);
750       printf_filtered ("\n");
751     }
752   do_cleanups(old_chain);
753 }
754 \f
755 /* ARGSUSED */
756 static void
757 program_info (args, from_tty)
758     char *args;
759     int from_tty;
760 {
761   bpstat bs = stop_bpstat;
762   int num = bpstat_num (&bs);
763   
764   if (!target_has_execution)
765     {
766       printf_filtered ("The program being debugged is not being run.\n");
767       return;
768     }
769
770   target_files_info ();
771   printf_filtered ("Program stopped at %s.\n",
772                    local_hex_string((unsigned long) stop_pc));
773   if (stop_step)
774     printf_filtered ("It stopped after being stepped.\n");
775   else if (num != 0)
776     {
777       /* There may be several breakpoints in the same place, so this
778          isn't as strange as it seems.  */
779       while (num != 0)
780         {
781           if (num < 0)
782             printf_filtered ("It stopped at a breakpoint that has since been deleted.\n");
783           else
784             printf_filtered ("It stopped at breakpoint %d.\n", num);
785           num = bpstat_num (&bs);
786         }
787     }
788   else if (stop_signal != TARGET_SIGNAL_0)
789     {
790       printf_filtered ("It stopped with signal %s, %s.\n",
791                        target_signal_to_name (stop_signal),
792                        target_signal_to_string (stop_signal));
793     }
794
795   if (!from_tty)
796     printf_filtered ("Type \"info stack\" or \"info registers\" for more information.\n");
797 }
798 \f
799 static void
800 environment_info (var, from_tty)
801      char *var;
802      int from_tty;
803 {
804   if (var)
805     {
806       register char *val = get_in_environ (inferior_environ, var);
807       if (val)
808         {
809           puts_filtered (var);
810           puts_filtered (" = ");
811           puts_filtered (val);
812           puts_filtered ("\n");
813         }
814       else
815         {
816           puts_filtered ("Environment variable \"");
817           puts_filtered (var);
818           puts_filtered ("\" not defined.\n");
819         }
820     }
821   else
822     {
823       register char **vector = environ_vector (inferior_environ);
824       while (*vector)
825         {
826           puts_filtered (*vector++);
827           puts_filtered ("\n");
828         }
829     }
830 }
831
832 static void
833 set_environment_command (arg, from_tty)
834      char *arg;
835      int from_tty;
836 {
837   register char *p, *val, *var;
838   int nullset = 0;
839
840   if (arg == 0)
841     error_no_arg ("environment variable and value");
842
843   /* Find seperation between variable name and value */
844   p = (char *) strchr (arg, '=');
845   val = (char *) strchr (arg, ' ');
846
847   if (p != 0 && val != 0)
848     {
849       /* We have both a space and an equals.  If the space is before the
850          equals, walk forward over the spaces til we see a nonspace 
851          (possibly the equals). */
852       if (p > val)
853         while (*val == ' ')
854           val++;
855
856       /* Now if the = is after the char following the spaces,
857          take the char following the spaces.  */
858       if (p > val)
859         p = val - 1;
860     }
861   else if (val != 0 && p == 0)
862     p = val;
863
864   if (p == arg)
865     error_no_arg ("environment variable to set");
866
867   if (p == 0 || p[1] == 0)
868     {
869       nullset = 1;
870       if (p == 0)
871         p = arg + strlen (arg); /* So that savestring below will work */
872     }
873   else
874     {
875       /* Not setting variable value to null */
876       val = p + 1;
877       while (*val == ' ' || *val == '\t')
878         val++;
879     }
880
881   while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
882
883   var = savestring (arg, p - arg);
884   if (nullset)
885     {
886       printf_filtered ("Setting environment variable \"%s\" to null value.\n", var);
887       set_in_environ (inferior_environ, var, "");
888     }
889   else
890     set_in_environ (inferior_environ, var, val);
891   free (var);
892 }
893
894 static void
895 unset_environment_command (var, from_tty)
896      char *var;
897      int from_tty;
898 {
899   if (var == 0)
900     {
901       /* If there is no argument, delete all environment variables.
902          Ask for confirmation if reading from the terminal.  */
903       if (!from_tty || query ("Delete all environment variables? "))
904         {
905           free_environ (inferior_environ);
906           inferior_environ = make_environ ();
907         }
908     }
909   else
910     unset_in_environ (inferior_environ, var);
911 }
912
913 /* Handle the execution path (PATH variable) */
914
915 static const char path_var_name[] = "PATH";
916
917 /* ARGSUSED */
918 static void
919 path_info (args, from_tty)
920      char *args;
921      int from_tty;
922 {
923   puts_filtered ("Executable and object file path: ");
924   puts_filtered (get_in_environ (inferior_environ, path_var_name));
925   puts_filtered ("\n");
926 }
927
928 /* Add zero or more directories to the front of the execution path.  */
929
930 static void
931 path_command (dirname, from_tty)
932      char *dirname;
933      int from_tty;
934 {
935   char *exec_path;
936   char *env;
937   dont_repeat ();
938   env = get_in_environ (inferior_environ, path_var_name);
939   /* Can be null if path is not set */
940   if (!env)
941     env = "";
942   exec_path = strsave (env);
943   mod_path (dirname, &exec_path);
944   set_in_environ (inferior_environ, path_var_name, exec_path);
945   free (exec_path);
946   if (from_tty)
947     path_info ((char *)NULL, from_tty);
948 }
949 \f
950 /* The array of register names.  */
951
952 char *reg_names[] = REGISTER_NAMES;
953
954 /* Print out the machine register regnum. If regnum is -1,
955    print all registers (fpregs == 1) or all non-float registers
956    (fpregs == 0).
957
958    For most machines, having all_registers_info() print the
959    register(s) one per line is good enough. If a different format
960    is required, (eg, for MIPS or Pyramid 90x, which both have
961    lots of regs), or there is an existing convention for showing
962    all the registers, define the macro DO_REGISTERS_INFO(regnum, fp)
963    to provide that format.  */  
964
965 #if !defined (DO_REGISTERS_INFO)
966
967 #define DO_REGISTERS_INFO(regnum, fp) do_registers_info(regnum, fp)
968
969 static void
970 do_registers_info (regnum, fpregs)
971      int regnum;
972      int fpregs;
973 {
974   register int i;
975   int numregs = ARCH_NUM_REGS;
976
977   for (i = 0; i < numregs; i++)
978     {
979       char raw_buffer[MAX_REGISTER_RAW_SIZE];
980       char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
981
982       /* Decide between printing all regs, nonfloat regs, or specific reg.  */
983       if (regnum == -1) {
984         if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT && !fpregs)
985           continue;
986       } else {
987         if (i != regnum)
988           continue;
989       }
990
991       /* If the register name is empty, it is undefined for this
992          processor, so don't display anything.  */
993       if (reg_names[i] == NULL || *(reg_names[i]) == '\0')
994         continue;
995
996       fputs_filtered (reg_names[i], gdb_stdout);
997       print_spaces_filtered (15 - strlen (reg_names[i]), gdb_stdout);
998
999       /* Get the data in raw format.  */
1000       if (read_relative_register_raw_bytes (i, raw_buffer))
1001         {
1002           printf_filtered ("Invalid register contents\n");
1003           continue;
1004         }
1005
1006       /* Convert raw data to virtual format if necessary.  */
1007 #ifdef REGISTER_CONVERTIBLE
1008       if (REGISTER_CONVERTIBLE (i))
1009         {
1010           REGISTER_CONVERT_TO_VIRTUAL (i, REGISTER_VIRTUAL_TYPE (i),
1011                                        raw_buffer, virtual_buffer);
1012         }
1013       else
1014 #endif
1015         memcpy (virtual_buffer, raw_buffer,
1016                 REGISTER_VIRTUAL_SIZE (i));
1017
1018       /* If virtual format is floating, print it that way, and in raw hex.  */
1019       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT)
1020         {
1021           register int j;
1022
1023 #ifdef INVALID_FLOAT
1024           if (INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
1025             printf_filtered ("<invalid float>");
1026           else
1027 #endif
1028             val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
1029                        gdb_stdout, 0, 1, 0, Val_pretty_default);
1030
1031           printf_filtered ("\t(raw 0x");
1032           for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
1033             {
1034               register int idx = TARGET_BYTE_ORDER == BIG_ENDIAN ? j
1035                 : REGISTER_RAW_SIZE (i) - 1 - j;
1036               printf_filtered ("%02x", (unsigned char)raw_buffer[idx]);
1037             }
1038           printf_filtered (")");
1039         }
1040
1041 /* FIXME!  val_print probably can handle all of these cases now...  */
1042
1043       /* Else if virtual format is too long for printf,
1044          print in hex a byte at a time.  */
1045       else if (REGISTER_VIRTUAL_SIZE (i) > (int) sizeof (long))
1046         {
1047           register int j;
1048           printf_filtered ("0x");
1049           for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
1050             printf_filtered ("%02x", (unsigned char)virtual_buffer[j]);
1051         }
1052       /* Else print as integer in hex and in decimal.  */
1053       else
1054         {
1055           val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
1056                      gdb_stdout, 'x', 1, 0, Val_pretty_default);
1057           printf_filtered ("\t");
1058           val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
1059                      gdb_stdout,   0, 1, 0, Val_pretty_default);
1060         }
1061
1062       /* The SPARC wants to print even-numbered float regs as doubles
1063          in addition to printing them as floats.  */
1064 #ifdef PRINT_REGISTER_HOOK
1065       PRINT_REGISTER_HOOK (i);
1066 #endif
1067
1068       printf_filtered ("\n");
1069     }
1070 }
1071 #endif /* no DO_REGISTERS_INFO.  */
1072
1073 static void
1074 registers_info (addr_exp, fpregs)
1075      char *addr_exp;
1076      int fpregs;
1077 {
1078   int regnum, numregs;
1079   register char *end;
1080
1081   if (!target_has_registers)
1082     error ("The program has no registers now.");
1083   if (selected_frame == NULL)
1084     error ("No selected frame.");
1085
1086   if (!addr_exp)
1087     {
1088       DO_REGISTERS_INFO(-1, fpregs);
1089       return;
1090     }
1091
1092   do
1093     {      
1094       if (addr_exp[0] == '$')
1095         addr_exp++;
1096       end = addr_exp;
1097       while (*end != '\0' && *end != ' ' && *end != '\t')
1098         ++end;
1099       numregs = ARCH_NUM_REGS;
1100       for (regnum = 0; regnum < numregs; regnum++)
1101         if (!strncmp (addr_exp, reg_names[regnum], end - addr_exp)
1102             && strlen (reg_names[regnum]) == end - addr_exp)
1103           goto found;
1104       if (*addr_exp >= '0' && *addr_exp <= '9')
1105         regnum = atoi (addr_exp);               /* Take a number */
1106       if (regnum >= numregs)            /* Bad name, or bad number */
1107         error ("%.*s: invalid register", end - addr_exp, addr_exp);
1108
1109 found:
1110       DO_REGISTERS_INFO(regnum, fpregs);
1111
1112       addr_exp = end;
1113       while (*addr_exp == ' ' || *addr_exp == '\t')
1114         ++addr_exp;
1115     } while (*addr_exp != '\0');
1116 }
1117
1118 static void
1119 all_registers_info (addr_exp, from_tty)
1120      char *addr_exp;
1121      int from_tty;
1122 {
1123   registers_info (addr_exp, 1);
1124 }
1125
1126 static void
1127 nofp_registers_info (addr_exp, from_tty)
1128      char *addr_exp;
1129      int from_tty;
1130 {
1131   registers_info (addr_exp, 0);
1132 }
1133 \f
1134 /*
1135  * TODO:
1136  * Should save/restore the tty state since it might be that the
1137  * program to be debugged was started on this tty and it wants
1138  * the tty in some state other than what we want.  If it's running
1139  * on another terminal or without a terminal, then saving and
1140  * restoring the tty state is a harmless no-op.
1141  * This only needs to be done if we are attaching to a process.
1142  */
1143
1144 /*
1145    attach_command --
1146    takes a program started up outside of gdb and ``attaches'' to it.
1147    This stops it cold in its tracks and allows us to start debugging it.
1148    and wait for the trace-trap that results from attaching.  */
1149
1150 void
1151 attach_command (args, from_tty)
1152      char *args;
1153      int from_tty;
1154 {
1155 #ifdef SOLIB_ADD
1156   extern int auto_solib_add;
1157 #endif
1158
1159   dont_repeat ();                       /* Not for the faint of heart */
1160
1161   if (target_has_execution)
1162     {
1163       if (query ("A program is being debugged already.  Kill it? "))
1164         target_kill ();
1165       else
1166         error ("Not killed.");
1167     }
1168
1169   target_attach (args, from_tty);
1170
1171   /* Set up the "saved terminal modes" of the inferior
1172      based on what modes we are starting it with.  */
1173   target_terminal_init ();
1174
1175   /* Install inferior's terminal modes.  */
1176   target_terminal_inferior ();
1177
1178   /* Set up execution context to know that we should return from
1179      wait_for_inferior as soon as the target reports a stop.  */
1180   init_wait_for_inferior ();
1181   clear_proceed_status ();
1182   stop_soon_quietly = 1;
1183
1184   /* No traps are generated when attaching to inferior under Mach 3
1185      or GNU hurd.  */
1186 #ifndef ATTACH_NO_WAIT
1187   wait_for_inferior ();
1188 #endif
1189
1190 #ifdef SOLIB_ADD
1191   if (auto_solib_add)
1192     {
1193       /* Add shared library symbols from the newly attached process, if any.  */
1194       SOLIB_ADD ((char *)0, from_tty, (struct target_ops *)0);
1195       re_enable_breakpoints_in_shlibs ();
1196     }
1197 #endif
1198
1199   normal_stop ();
1200 }
1201
1202 /*
1203  * detach_command --
1204  * takes a program previously attached to and detaches it.
1205  * The program resumes execution and will no longer stop
1206  * on signals, etc.  We better not have left any breakpoints
1207  * in the program or it'll die when it hits one.  For this
1208  * to work, it may be necessary for the process to have been
1209  * previously attached.  It *might* work if the program was
1210  * started via the normal ptrace (PTRACE_TRACEME).
1211  */
1212
1213 static void
1214 detach_command (args, from_tty)
1215      char *args;
1216      int from_tty;
1217 {
1218   dont_repeat ();                       /* Not for the faint of heart */
1219   target_detach (args, from_tty);
1220 }
1221
1222 /* ARGSUSED */
1223 static void
1224 float_info (addr_exp, from_tty)
1225      char *addr_exp;
1226      int from_tty;
1227 {
1228 #ifdef FLOAT_INFO
1229         FLOAT_INFO;
1230 #else
1231         printf_filtered ("No floating point info available for this processor.\n");
1232 #endif
1233 }
1234 \f
1235 /* ARGSUSED */
1236 static void
1237 unset_command (args, from_tty)
1238      char *args;
1239      int from_tty;
1240 {
1241   printf_filtered ("\"unset\" must be followed by the name of an unset subcommand.\n");
1242   help_list (unsetlist, "unset ", -1, gdb_stdout);
1243 }
1244
1245 void
1246 _initialize_infcmd ()
1247 {
1248   struct cmd_list_element *c;
1249   
1250   add_com ("tty", class_run, tty_command,
1251            "Set terminal for future runs of program being debugged.");
1252
1253   add_show_from_set
1254     (add_set_cmd ("args", class_run, var_string_noescape, (char *)&inferior_args,
1255                   
1256 "Set arguments to give program being debugged when it is started.\n\
1257 Follow this command with any number of args, to be passed to the program.",
1258                   &setlist),
1259      &showlist);
1260
1261   c = add_cmd
1262     ("environment", no_class, environment_info,
1263      "The environment to give the program, or one variable's value.\n\
1264 With an argument VAR, prints the value of environment variable VAR to\n\
1265 give the program being debugged.  With no arguments, prints the entire\n\
1266 environment to be given to the program.", &showlist);
1267   c->completer = noop_completer;
1268
1269   add_prefix_cmd ("unset", no_class, unset_command,
1270                   "Complement to certain \"set\" commands",
1271                   &unsetlist, "unset ", 0, &cmdlist);
1272   
1273   c = add_cmd ("environment", class_run, unset_environment_command,
1274               "Cancel environment variable VAR for the program.\n\
1275 This does not affect the program until the next \"run\" command.",
1276            &unsetlist);
1277   c->completer = noop_completer;
1278
1279   c = add_cmd ("environment", class_run, set_environment_command,
1280                "Set environment variable value to give the program.\n\
1281 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
1282 VALUES of environment variables are uninterpreted strings.\n\
1283 This does not affect the program until the next \"run\" command.",
1284            &setlist);
1285   c->completer = noop_completer;
1286  
1287   add_com ("path", class_files, path_command,
1288        "Add directory DIR(s) to beginning of search path for object files.\n\
1289 $cwd in the path means the current working directory.\n\
1290 This path is equivalent to the $PATH shell variable.  It is a list of\n\
1291 directories, separated by colons.  These directories are searched to find\n\
1292 fully linked executable files and separately compiled object files as needed.");
1293
1294   c = add_cmd ("paths", no_class, path_info,
1295             "Current search path for finding object files.\n\
1296 $cwd in the path means the current working directory.\n\
1297 This path is equivalent to the $PATH shell variable.  It is a list of\n\
1298 directories, separated by colons.  These directories are searched to find\n\
1299 fully linked executable files and separately compiled object files as needed.", &showlist);
1300   c->completer = noop_completer;
1301
1302  add_com ("attach", class_run, attach_command,
1303            "Attach to a process or file outside of GDB.\n\
1304 This command attaches to another target, of the same type as your last\n\
1305 `target' command (`info files' will show your target stack).\n\
1306 The command may take as argument a process id or a device file.\n\
1307 For a process id, you must have permission to send the process a signal,\n\
1308 and it must have the same effective uid as the debugger.\n\
1309 When using \"attach\", you should use the \"file\" command to specify\n\
1310 the program running in the process, and to load its symbol table.");
1311
1312   add_com ("detach", class_run, detach_command,
1313            "Detach a process or file previously attached.\n\
1314 If a process, it is no longer traced, and it continues its execution.  If you\n\
1315 were debugging a file, the file is closed and gdb no longer accesses it.");
1316
1317   add_com ("signal", class_run, signal_command,
1318            "Continue program giving it signal specified by the argument.\n\
1319 An argument of \"0\" means continue program without giving it a signal.");
1320
1321   add_com ("stepi", class_run, stepi_command,
1322            "Step one instruction exactly.\n\
1323 Argument N means do this N times (or till program stops for another reason).");
1324   add_com_alias ("si", "stepi", class_alias, 0);
1325
1326   add_com ("nexti", class_run, nexti_command,
1327            "Step one instruction, but proceed through subroutine calls.\n\
1328 Argument N means do this N times (or till program stops for another reason).");
1329   add_com_alias ("ni", "nexti", class_alias, 0);
1330
1331   add_com ("finish", class_run, finish_command,
1332            "Execute until selected stack frame returns.\n\
1333 Upon return, the value returned is printed and put in the value history.");
1334
1335   add_com ("next", class_run, next_command,
1336            "Step program, proceeding through subroutine calls.\n\
1337 Like the \"step\" command as long as subroutine calls do not happen;\n\
1338 when they do, the call is treated as one instruction.\n\
1339 Argument N means do this N times (or till program stops for another reason).");
1340   add_com_alias ("n", "next", class_run, 1);
1341
1342   add_com ("step", class_run, step_command,
1343            "Step program until it reaches a different source line.\n\
1344 Argument N means do this N times (or till program stops for another reason).");
1345   add_com_alias ("s", "step", class_run, 1);
1346
1347   add_com ("until", class_run, until_command,
1348            "Execute until the program reaches a source line greater than the current\n\
1349 or a specified line or address or function (same args as break command).\n\
1350 Execution will also stop upon exit from the current stack frame.");
1351   add_com_alias ("u", "until", class_run, 1);
1352   
1353   add_com ("jump", class_run, jump_command,
1354            "Continue program being debugged at specified line or address.\n\
1355 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1356 for an address to start at.");
1357
1358   add_com ("continue", class_run, continue_command,
1359            "Continue program being debugged, after signal or breakpoint.\n\
1360 If proceeding from breakpoint, a number N may be used as an argument,\n\
1361 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
1362 the breakpoint won't break until the Nth time it is reached).");
1363   add_com_alias ("c", "cont", class_run, 1);
1364   add_com_alias ("fg", "cont", class_run, 1);
1365
1366   add_com ("run", class_run, run_command,
1367            "Start debugged program.  You may specify arguments to give it.\n\
1368 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
1369 Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
1370 With no arguments, uses arguments last specified (with \"run\" or \"set args\").\n\
1371 To cancel previous arguments and run with no arguments,\n\
1372 use \"set args\" without arguments.");
1373   add_com_alias ("r", "run", class_run, 1);
1374
1375   add_info ("registers", nofp_registers_info,
1376     "List of integer registers and their contents, for selected stack frame.\n\
1377 Register name as argument means describe only that register.");
1378
1379   add_info ("all-registers", all_registers_info,
1380 "List of all registers and their contents, for selected stack frame.\n\
1381 Register name as argument means describe only that register.");
1382
1383   add_info ("program", program_info,
1384             "Execution status of the program.");
1385
1386   add_info ("float", float_info,
1387             "Print the status of the floating point unit\n");
1388
1389   inferior_args = savestring ("", 1);   /* Initially no args */
1390   inferior_environ = make_environ ();
1391   init_environ (inferior_environ);
1392 }