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