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