gdb-3.5
[external/binutils.git] / gdb / infcmd.c
1 /* Memory-access and commands for inferior process, for GDB.
2    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB 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 1, or (at your option)
9 any later version.
10
11 GDB 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 GDB; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "param.h"
23 #include "symtab.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "environ.h"
27 #include "value.h"
28
29 #include <signal.h>
30 #include <sys/param.h>
31
32 extern char *sys_siglist[];
33
34 #define ERROR_NO_INFERIOR \
35    if (inferior_pid == 0) error ("The program is not being run.");
36
37 /* String containing arguments to give to the program,
38    with a space added at the front.  Just a space means no args.  */
39
40 static char *inferior_args;
41
42 /* File name for default use for standard in/out in the inferior.  */
43
44 char *inferior_io_terminal;
45
46 /* Pid of our debugged inferior, or 0 if no inferior now.  */
47
48 int inferior_pid;
49
50 /* Last signal that the inferior received (why it stopped).  */
51
52 int stop_signal;
53
54 /* Address at which inferior stopped.  */
55
56 CORE_ADDR stop_pc;
57
58 /* Stack frame when program stopped.  */
59
60 FRAME_ADDR stop_frame_address;
61
62 /* Number of breakpoint it stopped at, or 0 if none.  */
63
64 int stop_breakpoint;
65
66 /* Nonzero if stopped due to a step command.  */
67
68 int stop_step;
69
70 /* Nonzero if stopped due to completion of a stack dummy routine.  */
71
72 int stop_stack_dummy;
73
74 /* Nonzero if stopped due to a random (unexpected) signal in inferior
75    process.  */
76
77 int stopped_by_random_signal;
78
79 /* Range to single step within.
80    If this is nonzero, respond to a single-step signal
81    by continuing to step if the pc is in this range.  */
82
83 CORE_ADDR step_range_start; /* Inclusive */
84 CORE_ADDR step_range_end; /* Exclusive */
85
86 /* Stack frame address as of when stepping command was issued.
87    This is how we know when we step into a subroutine call,
88    and how to set the frame for the breakpoint used to step out.  */
89
90 FRAME_ADDR step_frame_address;
91
92 /* 1 means step over all subroutine calls.
93    -1 means step over calls to undebuggable functions.  */
94
95 int step_over_calls;
96
97 /* If stepping, nonzero means step count is > 1
98    so don't print frame next time inferior stops
99    if it stops due to stepping.  */
100
101 int step_multi;
102
103 /* Environment to use for running inferior,
104    in format described in environ.h.  */
105
106 struct environ *inferior_environ;
107
108 CORE_ADDR read_pc ();
109 struct command_line *get_breakpoint_commands ();
110 void breakpoint_clear_ignore_counts ();
111
112 \f
113 int
114 have_inferior_p ()
115 {
116   return inferior_pid != 0;
117 }
118
119 static void 
120 set_args_command (args)
121      char *args;
122 {
123   free (inferior_args);
124   if (!args) args = "";
125   inferior_args = concat (" ", args, "");
126 }
127
128 void
129 tty_command (file, from_tty)
130      char *file;
131      int from_tty;
132 {
133   if (file == 0)
134     error_no_arg ("terminal name for running target process");
135
136   inferior_io_terminal = savestring (file, strlen (file));
137 }
138
139 static void
140 run_command (args, from_tty)
141      char *args;
142      int from_tty;
143 {
144   extern char **environ;
145   register int i;
146   char *exec_file;
147   char *allargs;
148
149   extern int sys_nerr;
150   extern char *sys_errlist[];
151   extern int errno;
152
153   dont_repeat ();
154
155   if (inferior_pid)
156     {
157       if (
158           !query ("The program being debugged has been started already.\n\
159 Start it from the beginning? "))
160         error ("Program not restarted.");
161       kill_inferior ();
162     }
163
164 #if 0
165   /* On the other hand, some users want to do
166          break open
167          ignore 1 40
168          run
169      So it's not clear what is best.  */
170
171   /* It is confusing to the user for ignore counts to stick around
172      from previous runs of the inferior.  So clear them.  */
173   breakpoint_clear_ignore_counts ();
174 #endif
175
176   exec_file = (char *) get_exec_file (1);
177
178   if (remote_debugging)
179     {
180       if (from_tty)
181         {
182           printf ("Starting program: %s\n", exec_file);
183           fflush (stdout);
184         }
185     }
186   else
187     {
188       if (args)
189         set_args_command (args);
190
191       if (from_tty)
192         {
193           printf ("Starting program: %s%s\n",
194                   exec_file, inferior_args);
195           fflush (stdout);
196         }
197
198       allargs = concat ("exec ", exec_file, inferior_args);
199       inferior_pid = create_inferior (allargs, environ_vector (inferior_environ));
200     }
201
202   clear_proceed_status ();
203
204   start_inferior ();
205 }
206 \f
207 void
208 cont_command (proc_count_exp, from_tty)
209      char *proc_count_exp;
210      int from_tty;
211 {
212   ERROR_NO_INFERIOR;
213
214   clear_proceed_status ();
215
216   /* If have argument, set proceed count of breakpoint we stopped at.  */
217
218   if (stop_breakpoint > 0 && proc_count_exp)
219     {
220       set_ignore_count (stop_breakpoint,
221                         parse_and_eval_address (proc_count_exp) - 1,
222                         from_tty);
223       if (from_tty)
224         printf ("  ");
225     }
226
227   if (from_tty)
228     printf ("Continuing.\n");
229
230   proceed (-1, -1, 0);
231 }
232 \f
233 /* Step until outside of current statement.  */
234 static void step_1 ();
235
236 static void
237 step_command (count_string)
238 {
239   step_1 (0, 0, count_string);
240 }
241
242 /* Likewise, but skip over subroutine calls as if single instructions.  */
243
244 static void
245 next_command (count_string)
246 {
247   step_1 (1, 0, count_string);
248 }
249
250 /* Likewise, but step only one instruction.  */
251
252 static void
253 stepi_command (count_string)
254 {
255   step_1 (0, 1, count_string);
256 }
257
258 static void
259 nexti_command (count_string)
260 {
261   step_1 (1, 1, count_string);
262 }
263
264 static void
265 step_1 (skip_subroutines, single_inst, count_string)
266      int skip_subroutines;
267      int single_inst;
268      char *count_string;
269 {
270   register int count = 1;
271
272   ERROR_NO_INFERIOR;
273   count = count_string ? parse_and_eval_address (count_string) : 1;
274
275   for (; count > 0; count--)
276     {
277       clear_proceed_status ();
278
279       step_frame_address = FRAME_FP (get_current_frame ());
280
281       if (! single_inst)
282         {
283           find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
284           if (step_range_end == 0)
285             {
286               int misc;
287
288               misc = find_pc_misc_function (stop_pc);
289               terminal_ours ();
290               printf ("Current function has no line number information.\n");
291               fflush (stdout);
292
293               /* No info or after _etext ("Can't happen") */
294               if (misc == -1 || misc == misc_function_count - 1)
295                 error ("No data available on pc function.");
296
297               printf ("Single stepping until function exit.\n");
298               fflush (stdout);
299
300               step_range_start = misc_function_vector[misc].address;
301               step_range_end = misc_function_vector[misc + 1].address;
302             }
303         }
304       else
305         {
306           /* Say we are stepping, but stop after one insn whatever it does.
307              Don't step through subroutine calls even to undebuggable
308              functions.  */
309           step_range_start = step_range_end = 1;
310           if (!skip_subroutines)
311             step_over_calls = 0;
312         }
313
314       if (skip_subroutines)
315         step_over_calls = 1;
316
317       step_multi = (count > 1);
318       proceed (-1, -1, 1);
319       if (! stop_step)
320         break;
321     }
322 }
323 \f
324 /* Continue program at specified address.  */
325
326 static void
327 jump_command (arg, from_tty)
328      char *arg;
329      int from_tty;
330 {
331   register CORE_ADDR addr;
332   struct symtabs_and_lines sals;
333   struct symtab_and_line sal;
334
335   ERROR_NO_INFERIOR;
336
337   if (!arg)
338     error_no_arg ("starting address");
339
340   sals = decode_line_spec_1 (arg, 1);
341   if (sals.nelts != 1)
342     {
343       error ("Unreasonable jump request");
344     }
345
346   sal = sals.sals[0];
347   free (sals.sals);
348
349   if (sal.symtab == 0 && sal.pc == 0)
350     error ("No source file has been specified.");
351
352   if (sal.pc == 0)
353     sal.pc = find_line_pc (sal.symtab, sal.line);
354
355   {
356     struct symbol *fn = get_frame_function (get_current_frame ());
357     struct symbol *sfn = find_pc_function (sal.pc);
358     if (fn != 0 && sfn != fn
359         && ! query ("Line %d is not in `%s'.  Jump anyway? ",
360                     sal.line, SYMBOL_NAME (fn)))
361       error ("Not confirmed.");
362   }
363
364   if (sal.pc == 0)
365     error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
366
367   addr = sal.pc;
368
369   clear_proceed_status ();
370
371   if (from_tty)
372     printf ("Continuing at 0x%x.\n", addr);
373
374   proceed (addr, 0, 0);
375 }
376
377 /* Continue program giving it specified signal.  */
378
379 static void
380 signal_command (signum_exp, from_tty)
381      char *signum_exp;
382      int from_tty;
383 {
384   register int signum;
385
386   dont_repeat ();               /* Too dangerous.  */
387   ERROR_NO_INFERIOR;
388
389   if (!signum_exp)
390     error_no_arg ("signal number");
391
392   signum = parse_and_eval_address (signum_exp);
393
394   clear_proceed_status ();
395
396   if (from_tty)
397     printf ("Continuing with signal %d.\n", signum);
398
399   proceed (stop_pc, signum, 0);
400 }
401
402 /* Execute a "stack dummy", a piece of code stored in the stack
403    by the debugger to be executed in the inferior.
404
405    To call: first, do PUSH_DUMMY_FRAME.
406    Then push the contents of the dummy.  It should end with a breakpoint insn.
407    Then call here, passing address at which to start the dummy.
408
409    The contents of all registers are saved before the dummy frame is popped
410    and copied into the buffer BUFFER.
411
412    The dummy's frame is automatically popped whenever that break is hit.
413    If that is the first time the program stops, run_stack_dummy
414    returns to its caller with that frame already gone.
415    Otherwise, the caller never gets returned to.  */
416
417 /* 4 => return instead of letting the stack dummy run.  */
418
419 static int stack_dummy_testing = 0;
420
421 void
422 run_stack_dummy (addr, buffer)
423      CORE_ADDR addr;
424      REGISTER_TYPE *buffer;
425 {
426   /* Now proceed, having reached the desired place.  */
427   clear_proceed_status ();
428   if (stack_dummy_testing & 4)
429     {
430       POP_FRAME;
431       return;
432     }
433   proceed (addr, 0, 0);
434
435   if (!stop_stack_dummy)
436     error ("Cannot continue previously requested operation.");
437
438   /* On return, the stack dummy has been popped already.  */
439
440   bcopy (stop_registers, buffer, sizeof stop_registers);
441 }
442 \f
443 /* Proceed until we reach the given line as argument or exit the
444    function.  When called with no argument, proceed until we reach a
445    different source line with pc greater than our current one or exit
446    the function.  We skip calls in both cases.
447
448    The effect of this command with an argument is identical to setting
449    a momentary breakpoint at the line specified and executing
450    "finish".
451
452    Note that eventually this command should probably be changed so
453    that only source lines are printed out when we hit the breakpoint
454    we set.  I'm going to postpone this until after a hopeful rewrite
455    of wait_for_inferior and the proceed status code. -- randy */
456
457 void
458 until_next_command (arg, from_tty)
459      char *arg;
460      int from_tty;
461 {
462   FRAME frame;
463   CORE_ADDR pc;
464   struct symbol *func;
465   struct symtab_and_line sal;
466     
467   clear_proceed_status ();
468
469   frame = get_current_frame ();
470
471   /* Step until either exited from this function or greater
472      than the current line (if in symbolic section) or pc (if
473      not). */
474
475   pc = read_pc ();
476   func = find_pc_function (pc);
477   
478   if (!func)
479     {
480       int misc_func = find_pc_misc_function (pc);
481       
482       if (misc_func != -1)
483         error ("Execution is not within a known function.");
484       
485       step_range_start = misc_function_vector[misc_func].address;
486       step_range_end = pc;
487     }
488   else
489     {
490       sal = find_pc_line (pc, 0);
491       
492       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
493       step_range_end = sal.end;
494     }
495   
496   step_over_calls = 1;
497   step_frame_address = FRAME_FP (frame);
498   
499   step_multi = 0;               /* Only one call to proceed */
500   
501   proceed (-1, -1, 1);
502 }
503
504 void 
505 until_command (arg, from_tty)
506      char *arg;
507      int from_tty;
508 {
509   if (!have_inferior_p ())
510     error ("The program is not being run.");
511
512   if (arg)
513     until_break_command (arg, from_tty);
514   else
515     until_next_command (arg, from_tty);
516 }
517 \f
518 /* "finish": Set a temporary breakpoint at the place
519    the selected frame will return to, then continue.  */
520
521 static void
522 finish_command (arg, from_tty)
523      char *arg;
524      int from_tty;
525 {
526   struct symtab_and_line sal;
527   register FRAME frame;
528   struct frame_info *fi;
529   register struct symbol *function;
530
531   if (!have_inferior_p ())
532     error ("The program is not being run.");
533   if (arg)
534     error ("The \"finish\" command does not take any arguments.");
535
536   frame = get_prev_frame (selected_frame);
537   if (frame == 0)
538     error ("\"finish\" not meaningful in the outermost frame.");
539
540   clear_proceed_status ();
541
542   fi = get_frame_info (frame);
543   sal = find_pc_line (fi->pc, 0);
544   sal.pc = fi->pc;
545   set_momentary_breakpoint (sal, frame);
546
547   /* Find the function we will return from.  */
548
549   fi = get_frame_info (selected_frame);
550   function = find_pc_function (fi->pc);
551
552   if (from_tty)
553     {
554       printf ("Run till exit from ");
555       print_selected_frame ();
556     }
557
558   proceed (-1, -1, 0);
559
560   if (stop_breakpoint == -3 && function != 0)
561     {
562       struct type *value_type;
563       register value val;
564       CORE_ADDR funcaddr;
565
566       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
567       if (!value_type)
568         fatal ("internal: finish_command: function has no target type");
569       
570       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
571         return;
572
573       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
574
575       val = value_being_returned (value_type, stop_registers,
576                                   using_struct_return (function,
577                                                        funcaddr,
578                                                        value_type));
579
580       printf ("Value returned is $%d = ", record_latest_value (val));
581       value_print (val, stdout, 0, Val_no_prettyprint);
582       putchar ('\n');
583     }
584 }
585 \f
586 static void
587 program_info ()
588 {
589   if (inferior_pid == 0)
590     {
591       printf ("The program being debugged is not being run.\n");
592       return;
593     }
594
595   printf ("Program being debugged is in process %d, stopped at 0x%x.\n",
596           inferior_pid, stop_pc);
597   if (stop_step)
598     printf ("It stopped after being stepped.\n");
599   else if (stop_breakpoint > 0)
600     printf ("It stopped at breakpoint %d.\n", stop_breakpoint);
601   else if (stop_signal)
602     printf ("It stopped with signal %d (%s).\n",
603             stop_signal, sys_siglist[stop_signal]);
604
605   printf ("\nType \"info stack\" or \"info reg\" for more information.\n");
606 }
607 \f
608 static void
609 environment_info (var)
610      char *var;
611 {
612   if (var)
613     {
614       register char *val = get_in_environ (inferior_environ, var);
615       if (val)
616         printf ("%s = %s\n", var, val);
617       else
618         printf ("Environment variable \"%s\" not defined.\n", var);
619     }
620   else
621     {
622       register char **vector = environ_vector (inferior_environ);
623       while (*vector)
624         printf ("%s\n", *vector++);
625     }
626 }
627
628 static void
629 set_environment_command (arg)
630      char *arg;
631 {
632   register char *p, *val, *var;
633   int nullset = 0;
634
635   if (arg == 0)
636     error_no_arg ("environment variable and value");
637
638   /* Find seperation between variable name and value */
639   p = (char *) index (arg, '=');
640   val = (char *) index (arg, ' ');
641
642   if (p != 0 && val != 0)
643     {
644       /* We have both a space and an equals.  If the space is before the
645          equals and the only thing between the two is more space, use
646          the equals */
647       if (p > val)
648         while (*val == ' ')
649           val++;
650
651       /* Take the smaller of the two.  If there was space before the
652          "=", they will be the same right now. */
653       p = arg + min (p - arg, val - arg);
654     }
655   else if (val != 0 && p == 0)
656     p = val;
657
658   if (p == arg)
659     error_no_arg ("environment variable to set");
660
661   if (p == 0 || p[1] == 0)
662     {
663       nullset = 1;
664       if (p == 0)
665         p = arg + strlen (arg); /* So that savestring below will work */
666     }
667   else
668     {
669       /* Not setting variable value to null */
670       val = p + 1;
671       while (*val == ' ' || *val == '\t')
672         val++;
673     }
674
675   while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
676
677   var = savestring (arg, p - arg);
678   if (nullset)
679     {
680       printf ("Setting environment variable \"%s\" to null value.\n", var);
681       set_in_environ (inferior_environ, var, "");
682     }
683   else
684     set_in_environ (inferior_environ, var, val);
685   free (var);
686 }
687
688 static void
689 unset_environment_command (var, from_tty)
690      char *var;
691      int from_tty;
692 {
693   if (var == 0)
694     /* If there is no argument, delete all environment variables.
695        Ask for confirmation if reading from the terminal.  */
696     if (!from_tty || query ("Delete all environment variables? "))
697       {
698         free_environ (inferior_environ);
699         inferior_environ = make_environ ();
700       }
701
702   unset_in_environ (inferior_environ, var);
703 }
704 \f
705 /* Read an integer from debugged memory, given address and number of bytes.  */
706
707 long
708 read_memory_integer (memaddr, len)
709      CORE_ADDR memaddr;
710      int len;
711 {
712   char cbuf;
713   short sbuf;
714   int ibuf;
715   long lbuf;
716   int result_err;
717   extern int sys_nerr;
718   extern char *sys_errlist[];
719
720   if (len == sizeof (char))
721     {
722       result_err = read_memory (memaddr, &cbuf, len);
723       if (result_err)
724         error ("Error reading memory address 0x%x: %s (%d).",
725                memaddr, (result_err < sys_nerr ?
726                          sys_errlist[result_err] :
727                          "uknown error"), result_err);
728       return cbuf;
729     }
730   if (len == sizeof (short))
731     {
732       result_err = read_memory (memaddr, &sbuf, len);
733       if (result_err)
734         error ("Error reading memory address 0x%x: %s (%d).",
735                memaddr, (result_err < sys_nerr ?
736                          sys_errlist[result_err] :
737                          "uknown error"), result_err);
738       return sbuf;
739     }
740   if (len == sizeof (int))
741     {
742       result_err = read_memory (memaddr, &ibuf, len);
743       if (result_err)
744         error ("Error reading memory address 0x%x: %s (%d).",
745                memaddr, (result_err < sys_nerr ?
746                          sys_errlist[result_err] :
747                          "uknown error"), result_err);
748       return ibuf;
749     }
750   if (len == sizeof (lbuf))
751     {
752       result_err = read_memory (memaddr, &lbuf, len);
753       if (result_err)
754         error ("Error reading memory address 0x%x: %s (%d).",
755                memaddr, (result_err < sys_nerr ?
756                          sys_errlist[result_err] :
757                          "uknown error"), result_err);
758       return lbuf;
759     }
760   error ("Cannot handle integers of %d bytes.", len);
761 }
762 \f
763 CORE_ADDR
764 read_pc ()
765 {
766   return (CORE_ADDR) read_register (PC_REGNUM);
767 }
768
769 void
770 write_pc (val)
771      CORE_ADDR val;
772 {
773   write_register (PC_REGNUM, (long) val);
774 #ifdef NPC_REGNUM
775   write_register (NPC_REGNUM, (long) val+4);
776 #endif
777 }
778
779 char *reg_names[] = REGISTER_NAMES;
780
781 /* Print out the machine register regnum. If regnum is -1,
782    print all registers.
783    For most machines, having all_registers_info() print the
784    register(s) one per line is good enough. If a different format
785    is required, (eg, for SPARC or Pyramid 90x, which both have
786    lots of regs), or there is an existing convention for showing
787    all the registers, define the macro DO_REGISTERS_INFO(regnum)
788    to provide that format.  */  
789 #if !defined (DO_REGISTERS_INFO)
790 #define DO_REGISTERS_INFO(regnum) do_registers_info(regnum)
791 static void do_registers_info (regnum)
792      int regnum;
793 {
794   register int i;
795
796   if (regnum == -1)
797     printf_filtered (
798       "Register       Contents (relative to selected stack frame)\n\n");
799
800   for (i = 0; i < NUM_REGS; i++)
801     {
802       unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
803       unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
804       REGISTER_TYPE val;
805
806       if (regnum != -1 && i != regnum)
807         continue;
808
809       /* Get the data in raw format, then convert also to virtual format.  */
810       read_relative_register_raw_bytes (i, raw_buffer);
811       REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
812
813       fputs_filtered (reg_names[i], stdout);
814       print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
815
816       /* If virtual format is floating, print it that way.  */
817       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
818           && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
819         val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
820                    stdout, 0, 1, 0, Val_pretty_default);
821       /* Else if virtual format is too long for printf,
822          print in hex a byte at a time.  */
823       else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
824         {
825           register int j;
826           printf_filtered ("0x");
827           for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
828             printf_filtered ("%02x", virtual_buffer[j]);
829         }
830       /* Else print as integer in hex and in decimal.  */
831       else
832         {
833           long val;
834
835           bcopy (virtual_buffer, &val, sizeof (long));
836           if (val == 0)
837             printf_filtered ("0");
838           else
839             printf_filtered ("0x%08x  %d", val, val);
840         }
841
842       /* If register has different raw and virtual formats,
843          print the raw format in hex now.  */
844
845       if (REGISTER_CONVERTIBLE (i))
846         {
847           register int j;
848
849           printf_filtered ("  (raw 0x");
850           for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
851             printf_filtered ("%02x", raw_buffer[j]);
852           printf_filtered (")");
853         }
854       printf_filtered ("\n");
855     }
856 }
857 #endif /* no DO_REGISTERS_INFO.  */
858
859 static void
860 registers_info (addr_exp)
861      char *addr_exp;
862 {
863   int regnum;
864
865   if (!have_inferior_p () && !have_core_file_p ())
866     error ("No inferior or core file");
867
868   if (addr_exp)
869     {
870       if (*addr_exp >= '0' && *addr_exp <= '9')
871         regnum = atoi (addr_exp);
872       else
873         {
874           register char *p = addr_exp;
875           if (p[0] == '$')
876             p++;
877           for (regnum = 0; regnum < NUM_REGS; regnum++)
878             if (!strcmp (p, reg_names[regnum]))
879               break;
880           if (regnum == NUM_REGS)
881             error ("%s: invalid register name.", addr_exp);
882         }
883     }
884   else
885     regnum = -1;
886
887   DO_REGISTERS_INFO(regnum);
888 }
889 \f
890 #ifdef ATTACH_DETACH
891 #define PROCESS_ATTACH_ALLOWED 1
892 #else
893 #define PROCESS_ATTACH_ALLOWED 0
894 #endif
895 /*
896  * TODO:
897  * Should save/restore the tty state since it might be that the
898  * program to be debugged was started on this tty and it wants
899  * the tty in some state other than what we want.  If it's running
900  * on another terminal or without a terminal, then saving and
901  * restoring the tty state is a harmless no-op.
902  * This only needs to be done if we are attaching to a process.
903  */
904
905 /*
906  * attach_command --
907  * takes a program started up outside of gdb and ``attaches'' to it.
908  * This stops it cold in its tracks and allows us to start tracing it.
909  * For this to work, we must be able to send the process a
910  * signal and we must have the same effective uid as the program.
911  */
912 static void
913 attach_command (args, from_tty)
914      char *args;
915      int from_tty;
916 {
917   char *exec_file;
918   int pid;
919   int remote = 0;
920
921   dont_repeat();
922
923   if (!args)
924     error_no_arg ("process-id or device file to attach");
925
926   while (*args == ' ' || *args == '\t') args++;
927
928   if (args[0] == '/')
929     remote = 1;
930   else
931 #ifndef ATTACH_DETACH
932     error ("Can't attach to a process on this machine.");
933 #else
934     pid = atoi (args);
935 #endif
936
937   if (inferior_pid)
938     {
939       if (query ("A program is being debugged already.  Kill it? "))
940         kill_inferior ();
941       else
942         error ("Inferior not killed.");
943     }
944
945   exec_file = (char *) get_exec_file (1);
946
947   if (from_tty)
948     {
949       if (remote)
950         printf ("Attaching remote machine\n");
951       else
952         printf ("Attaching program: %s pid %d\n",
953                 exec_file, pid);
954       fflush (stdout);
955     }
956
957 #ifdef ATTACH_DETACH
958   if (remote)
959     {
960 #endif
961       remote_open (args, from_tty);
962       start_remote ();
963 #ifdef ATTACH_DETACH
964     }
965   else
966     attach_program (pid);
967 #endif
968 }
969
970 /*
971  * detach_command --
972  * takes a program previously attached to and detaches it.
973  * The program resumes execution and will no longer stop
974  * on signals, etc.  We better not have left any breakpoints
975  * in the program or it'll die when it hits one.  For this
976  * to work, it may be necessary for the process to have been
977  * previously attached.  It *might* work if the program was
978  * started via the normal ptrace (PTRACE_TRACEME).
979  */
980
981 static void
982 detach_command (args, from_tty)
983      char *args;
984      int from_tty;
985 {
986   int signal = 0;
987
988 #ifdef ATTACH_DETACH
989   if (inferior_pid)
990     {
991       if (from_tty)
992         {
993           char *exec_file = (char *)get_exec_file (0);
994           if (exec_file == 0)
995             exec_file = "";
996           printf ("Detaching program: %s pid %d\n",
997                   exec_file, inferior_pid);
998           fflush (stdout);
999         }
1000       if (args)
1001         signal = atoi (args);
1002       
1003       detach (signal);
1004       inferior_pid = 0;
1005     }
1006   else
1007 #endif
1008     {
1009       if (!remote_debugging)
1010         error ("Not currently attached to subsidiary or remote process.");
1011
1012       if (args)
1013         error ("Argument given to \"detach\" when remotely debugging.");
1014       
1015       remote_close (from_tty);
1016     }
1017 }
1018
1019 /* ARGUSUED */
1020 static void
1021 float_info (addr_exp)
1022      char *addr_exp;
1023 {
1024 #ifdef FLOAT_INFO
1025         FLOAT_INFO;
1026 #else
1027         printf ("No floating point info available for this processor.\n");
1028 #endif
1029 }
1030 \f
1031 extern struct cmd_list_element *setlist, *deletelist;
1032
1033 void
1034 _initialize_infcmd ()
1035 {
1036   add_com ("tty", class_run, tty_command,
1037            "Set terminal for future runs of program being debugged.");
1038
1039   add_cmd ("args", class_run, set_args_command,
1040            "Specify arguments to give program being debugged when it is started.\n\
1041 Follow this command with any number of args, to be passed to the program.",
1042            &setlist);
1043
1044   add_info ("environment", environment_info,
1045             "The environment to give the program, or one variable's value.\n\
1046 With an argument VAR, prints the value of environment variable VAR to\n\
1047 give the program being debugged.  With no arguments, prints the entire\n\
1048 environment to be given to the program.");
1049
1050   add_cmd ("environment", class_run, unset_environment_command,
1051            "Cancel environment variable VAR for the program.\n\
1052 This does not affect the program until the next \"run\" command.",
1053            &deletelist);
1054
1055   add_cmd ("environment", class_run, set_environment_command,
1056            "Set environment variable value to give the program.\n\
1057 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
1058 VALUES of environment variables are uninterpreted strings.\n\
1059 This does not affect the program until the next \"run\" command.",
1060            &setlist);
1061  
1062 #ifdef ATTACH_DETACH
1063  add_com ("attach", class_run, attach_command,
1064            "Attach to a process that was started up outside of GDB.\n\
1065 This command may take as argument a process id or a device file.\n\
1066 For a process id, you must have permission to send the process a signal,\n\
1067 and it must have the same effective uid as the debugger.\n\
1068 For a device file, the file must be a connection to a remote debug server.\n\n\
1069 Before using \"attach\", you must use the \"exec-file\" command\n\
1070 to specify the program running in the process,\n\
1071 and the \"symbol-file\" command to load its symbol table.");
1072 #else
1073  add_com ("attach", class_run, attach_command,
1074            "Attach to a process that was started up outside of GDB.\n\
1075 This commands takes as an argument the name of a device file.\n\
1076 This file must be a connection to a remote debug server.\n\n\
1077 Before using \"attach\", you must use the \"exec-file\" command\n\
1078 to specify the program running in the process,\n\
1079 and the \"symbol-file\" command to load its symbol table.");
1080 #endif
1081   add_com ("detach", class_run, detach_command,
1082            "Detach the process previously attached.\n\
1083 The process is no longer traced and continues its execution.");
1084
1085   add_com ("signal", class_run, signal_command,
1086            "Continue program giving it signal number SIGNUMBER.");
1087
1088   add_com ("stepi", class_run, stepi_command,
1089            "Step one instruction exactly.\n\
1090 Argument N means do this N times (or till program stops for another reason).");
1091   add_com_alias ("si", "stepi", class_alias, 0);
1092
1093   add_com ("nexti", class_run, nexti_command,
1094            "Step one instruction, but proceed through subroutine calls.\n\
1095 Argument N means do this N times (or till program stops for another reason).");
1096   add_com_alias ("ni", "nexti", class_alias, 0);
1097
1098   add_com ("finish", class_run, finish_command,
1099            "Execute until selected stack frame returns.\n\
1100 Upon return, the value returned is printed and put in the value history.");
1101
1102   add_com ("next", class_run, next_command,
1103            "Step program, proceeding through subroutine calls.\n\
1104 Like the \"step\" command as long as subroutine calls do not happen;\n\
1105 when they do, the call is treated as one instruction.\n\
1106 Argument N means do this N times (or till program stops for another reason).");
1107   add_com_alias ("n", "next", class_run, 1);
1108
1109   add_com ("step", class_run, step_command,
1110            "Step program until it reaches a different source line.\n\
1111 Argument N means do this N times (or till program stops for another reason).");
1112   add_com_alias ("s", "step", class_run, 1);
1113
1114   add_com ("until", class_run, until_command,
1115            "Execute until the program reaches a source line greater than the current\n\
1116 or a specified line or address or function (same args as break command).\n\
1117 Execution will also stop upon exit from the current stack frame.");
1118   add_com_alias ("u", "until", class_run, 1);
1119   
1120   add_com ("jump", class_run, jump_command,
1121            "Continue program being debugged at specified line or address.\n\
1122 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1123 for an address to start at.");
1124
1125   add_com ("cont", class_run, cont_command,
1126            "Continue program being debugged, after signal or breakpoint.\n\
1127 If proceeding from breakpoint, a number N may be used as an argument:\n\
1128 then the same breakpoint won't break until the Nth time it is reached.");
1129   add_com_alias ("c", "cont", class_run, 1);
1130
1131   add_com ("run", class_run, run_command,
1132            "Start debugged program.  You may specify arguments to give it.\n\
1133 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
1134 Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
1135 With no arguments, uses arguments last specified (with \"run\" or \"set args\".\n\
1136 To cancel previous arguments and run with no arguments,\n\
1137 use \"set args\" without arguments.");
1138   add_com_alias ("r", "run", class_run, 1);
1139
1140   add_info ("registers", registers_info,
1141             "List of registers and their contents, for selected stack frame.\n\
1142 Register name as argument means describe only that register.");
1143
1144   add_info ("program", program_info,
1145             "Execution status of the program.");
1146
1147   add_info ("float", float_info,
1148             "Print the status of the floating point unit\n");
1149
1150   inferior_args = savestring (" ", 1); /* By default, no args.  */
1151   inferior_environ = make_environ ();
1152   init_environ (inferior_environ);
1153 }
1154