* dbxread.c, i386-pinsn.c, i386-tdep.c, regex.c, solib.c, symmisc.c,
[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    -1 means step over calls to undebuggable functions.  */
177
178 int step_over_calls;
179
180 /* If stepping, nonzero means step count is > 1
181    so don't print frame next time inferior stops
182    if it stops due to stepping.  */
183
184 int step_multi;
185
186 /* Environment to use for running inferior,
187    in format described in environ.h.  */
188
189 struct environ *inferior_environ;
190
191 \f
192 /* ARGSUSED */
193 void
194 tty_command (file, from_tty)
195      char *file;
196      int from_tty;
197 {
198   if (file == 0)
199     error_no_arg ("terminal name for running target process");
200
201   inferior_io_terminal = savestring (file, strlen (file));
202 }
203
204 static void
205 run_command (args, from_tty)
206      char *args;
207      int from_tty;
208 {
209   char *exec_file;
210
211   dont_repeat ();
212
213   if (inferior_pid)
214     {
215       if (
216           !query ("The program being debugged has been started already.\n\
217 Start it from the beginning? "))
218         error ("Program not restarted.");
219       target_kill ();
220     }
221
222   exec_file = (char *) get_exec_file (0);
223
224   /* The exec file is re-read every time we do a generic_mourn_inferior, so
225      we just have to worry about the symbol file.  */
226   reread_symbols ();
227
228   if (args)
229     {
230       char *cmd;
231       cmd = concat ("set args ", args, NULL);
232       make_cleanup (free, cmd);
233       execute_command (cmd, from_tty);
234     }
235
236   if (from_tty)
237     {
238       printf_filtered ("Starting program: %s %s\n",
239               exec_file? exec_file: "", inferior_args);
240       fflush (stdout);
241     }
242
243   target_create_inferior (exec_file, inferior_args,
244                           environ_vector (inferior_environ));
245 }
246 \f
247 static void
248 continue_command (proc_count_exp, from_tty)
249      char *proc_count_exp;
250      int from_tty;
251 {
252   ERROR_NO_INFERIOR;
253
254   /* If have argument, set proceed count of breakpoint we stopped at.  */
255
256   if (proc_count_exp != NULL)
257     {
258       bpstat bs = stop_bpstat;
259       int num = bpstat_num (&bs);
260       if (num == 0 && from_tty)
261         {
262           printf_filtered
263             ("Not stopped at any breakpoint; argument ignored.\n");
264         }
265       while (num != 0)
266         {
267           set_ignore_count (num,
268                             parse_and_eval_address (proc_count_exp) - 1,
269                             from_tty);
270           /* set_ignore_count prints a message ending with a period.
271              So print two spaces before "Continuing.".  */
272           if (from_tty)
273             printf_filtered ("  ");
274           num = bpstat_num (&bs);
275         }
276     }
277
278   if (from_tty)
279     printf_filtered ("Continuing.\n");
280
281   clear_proceed_status ();
282
283   proceed ((CORE_ADDR) -1, -1, 0);
284 }
285 \f
286 /* Step until outside of current statement.  */
287
288 /* ARGSUSED */
289 static void
290 step_command (count_string, from_tty)
291      char *count_string;
292      int from_tty;
293 {
294   step_1 (0, 0, count_string);
295 }
296
297 /* Likewise, but skip over subroutine calls as if single instructions.  */
298
299 /* ARGSUSED */
300 static void
301 next_command (count_string, from_tty)
302      char *count_string;
303      int from_tty;
304 {
305   step_1 (1, 0, count_string);
306 }
307
308 /* Likewise, but step only one instruction.  */
309
310 /* ARGSUSED */
311 static void
312 stepi_command (count_string, from_tty)
313      char *count_string;
314      int from_tty;
315 {
316   step_1 (0, 1, count_string);
317 }
318
319 /* ARGSUSED */
320 static void
321 nexti_command (count_string, from_tty)
322      char *count_string;
323      int from_tty;
324 {
325   step_1 (1, 1, count_string);
326 }
327
328 static void
329 step_1 (skip_subroutines, single_inst, count_string)
330      int skip_subroutines;
331      int single_inst;
332      char *count_string;
333 {
334   register int count = 1;
335   FRAME fr;
336   struct cleanup *cleanups = 0;
337
338   ERROR_NO_INFERIOR;
339   count = count_string ? parse_and_eval_address (count_string) : 1;
340
341   if (!single_inst || skip_subroutines) /* leave si command alone */
342     {
343       enable_longjmp_breakpoint();
344       cleanups = make_cleanup(disable_longjmp_breakpoint, 0);
345     }
346
347   for (; count > 0; count--)
348     {
349       clear_proceed_status ();
350
351       fr = get_current_frame ();
352       if (!fr)                          /* Avoid coredump here.  Why tho? */
353         error ("No current frame");
354       step_frame_address = FRAME_FP (fr);
355
356       if (! single_inst)
357         {
358           find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
359           if (step_range_end == 0)
360             {
361               struct minimal_symbol *msymbol;
362
363               msymbol = lookup_minimal_symbol_by_pc (stop_pc);
364               target_terminal_ours ();
365               printf_filtered ("Current function has no line number information.\n");
366               fflush (stdout);
367
368               /* No info or after _etext ("Can't happen") */
369               if (msymbol == NULL || (msymbol + 1) -> name == NULL)
370                 error ("No data available on pc function.");
371
372               printf_filtered ("Single stepping until function exit.\n");
373               fflush (stdout);
374
375               step_range_start = msymbol -> address;
376               step_range_end = (msymbol + 1) -> address;
377             }
378         }
379       else
380         {
381           /* Say we are stepping, but stop after one insn whatever it does.
382              Don't step through subroutine calls even to undebuggable
383              functions.  */
384           step_range_start = step_range_end = 1;
385           if (!skip_subroutines)
386             step_over_calls = 0;
387         }
388
389       if (skip_subroutines)
390         step_over_calls = 1;
391
392       step_multi = (count > 1);
393       proceed ((CORE_ADDR) -1, -1, 1);
394       if (! stop_step)
395         break;
396 #if defined (SHIFT_INST_REGS)
397       write_register (NNPC_REGNUM, read_register (NPC_REGNUM));
398       write_register (NPC_REGNUM, read_register (PC_REGNUM));
399 #endif
400     }
401
402   if (!single_inst || skip_subroutines)
403     do_cleanups(cleanups);
404 }
405 \f
406 /* Continue program at specified address.  */
407
408 static void
409 jump_command (arg, from_tty)
410      char *arg;
411      int from_tty;
412 {
413   register CORE_ADDR addr;
414   struct symtabs_and_lines sals;
415   struct symtab_and_line sal;
416   struct symbol *fn;
417   struct symbol *sfn;
418   char *fname;
419   struct cleanup *back_to;
420
421   ERROR_NO_INFERIOR;
422
423   if (!arg)
424     error_no_arg ("starting address");
425
426   sals = decode_line_spec_1 (arg, 1);
427   if (sals.nelts != 1)
428     {
429       error ("Unreasonable jump request");
430     }
431
432   sal = sals.sals[0];
433   free ((PTR)sals.sals);
434
435   if (sal.symtab == 0 && sal.pc == 0)
436     error ("No source file has been specified.");
437
438   resolve_sal_pc (&sal);                        /* May error out */
439
440   /* See if we are trying to jump to another function. */
441   fn = get_frame_function (get_current_frame ());
442   sfn = find_pc_function (sal.pc);
443   if (fn != NULL && sfn != fn)
444     {
445       fname = strdup_demangled (SYMBOL_NAME (fn));
446       back_to = make_cleanup (free, fname);
447       if (!query ("Line %d is not in `%s'.  Jump anyway? ", sal.line, fname))
448         {
449           error ("Not confirmed.");
450           /* NOTREACHED */
451         }
452       do_cleanups (back_to);
453     }
454
455   addr = ADDR_BITS_SET (sal.pc);
456
457   if (from_tty)
458     printf_filtered ("Continuing at %s.\n", local_hex_string(addr));
459
460   clear_proceed_status ();
461   proceed (addr, 0, 0);
462 }
463
464 /* Continue program giving it specified signal.  */
465
466 static void
467 signal_command (signum_exp, from_tty)
468      char *signum_exp;
469      int from_tty;
470 {
471   register int signum;
472
473   dont_repeat ();               /* Too dangerous.  */
474   ERROR_NO_INFERIOR;
475
476   if (!signum_exp)
477     error_no_arg ("signal number");
478
479   signum = parse_and_eval_address (signum_exp);
480
481   if (from_tty)
482     printf_filtered ("Continuing with signal %d.\n", signum);
483
484   clear_proceed_status ();
485   proceed (stop_pc, signum, 0);
486 }
487
488 /* Execute a "stack dummy", a piece of code stored in the stack
489    by the debugger to be executed in the inferior.
490
491    To call: first, do PUSH_DUMMY_FRAME.
492    Then push the contents of the dummy.  It should end with a breakpoint insn.
493    Then call here, passing address at which to start the dummy.
494
495    The contents of all registers are saved before the dummy frame is popped
496    and copied into the buffer BUFFER.
497
498    The dummy's frame is automatically popped whenever that break is hit.
499    If that is the first time the program stops, run_stack_dummy
500    returns to its caller with that frame already gone.
501    Otherwise, the caller never gets returned to.  */
502
503 /* DEBUG HOOK:  4 => return instead of letting the stack dummy run.  */
504
505 static int stack_dummy_testing = 0;
506
507 void
508 run_stack_dummy (addr, buffer)
509      CORE_ADDR addr;
510      char buffer[REGISTER_BYTES];
511 {
512   /* Now proceed, having reached the desired place.  */
513   clear_proceed_status ();
514   if (stack_dummy_testing & 4)
515     {
516       POP_FRAME;
517       return;
518     }
519   proceed_to_finish = 1;        /* We want stop_registers, please... */
520   proceed (addr, 0, 0);
521
522   if (!stop_stack_dummy)
523     /* This used to say
524        "Cannot continue previously requested operation".  */
525     error ("\
526 The program being debugged stopped while in a function called from GDB.\n\
527 The expression which contained the function call has been discarded.");
528
529   /* On return, the stack dummy has been popped already.  */
530
531   (void) memcpy (buffer, stop_registers, sizeof stop_registers);
532 }
533 \f
534 /* Proceed until we reach a different source line with pc greater than
535    our current one or exit the function.  We skip calls in both cases.
536
537    Note that eventually this command should probably be changed so
538    that only source lines are printed out when we hit the breakpoint
539    we set.  I'm going to postpone this until after a hopeful rewrite
540    of wait_for_inferior and the proceed status code. -- randy */
541
542 /* ARGSUSED */
543 static void
544 until_next_command (from_tty)
545      int from_tty;
546 {
547   FRAME frame;
548   CORE_ADDR pc;
549   struct symbol *func;
550   struct symtab_and_line sal;
551  
552   clear_proceed_status ();
553
554   frame = get_current_frame ();
555
556   /* Step until either exited from this function or greater
557      than the current line (if in symbolic section) or pc (if
558      not). */
559
560   pc = read_pc ();
561   func = find_pc_function (pc);
562   
563   if (!func)
564     {
565       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
566       
567       if (msymbol == NULL)
568         error ("Execution is not within a known function.");
569       
570       step_range_start = msymbol -> address;
571       step_range_end = pc;
572     }
573   else
574     {
575       sal = find_pc_line (pc, 0);
576       
577       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
578       step_range_end = sal.end;
579     }
580   
581   step_over_calls = 1;
582   step_frame_address = FRAME_FP (frame);
583   
584   step_multi = 0;               /* Only one call to proceed */
585   
586   proceed ((CORE_ADDR) -1, -1, 1);
587 }
588
589 static void 
590 until_command (arg, from_tty)
591      char *arg;
592      int from_tty;
593 {
594   if (!target_has_execution)
595     error ("The program is not running.");
596   if (arg)
597     until_break_command (arg, from_tty);
598   else
599     until_next_command (from_tty);
600 }
601 \f
602 /* "finish": Set a temporary breakpoint at the place
603    the selected frame will return to, then continue.  */
604
605 static void
606 finish_command (arg, from_tty)
607      char *arg;
608      int from_tty;
609 {
610   struct symtab_and_line sal;
611   register FRAME frame;
612   struct frame_info *fi;
613   register struct symbol *function;
614   struct breakpoint *breakpoint;
615   struct cleanup *old_chain;
616
617   if (arg)
618     error ("The \"finish\" command does not take any arguments.");
619   if (!target_has_execution)
620     error ("The program is not running.");
621   if (selected_frame == NULL)
622     error ("No selected frame.");
623
624   frame = get_prev_frame (selected_frame);
625   if (frame == 0)
626     error ("\"finish\" not meaningful in the outermost frame.");
627
628   clear_proceed_status ();
629
630   fi = get_frame_info (frame);
631   sal = find_pc_line (fi->pc, 0);
632   sal.pc = fi->pc;
633
634   breakpoint = set_momentary_breakpoint (sal, frame, bp_finish);
635
636   old_chain = make_cleanup(delete_breakpoint, breakpoint);
637
638   /* Find the function we will return from.  */
639
640   fi = get_frame_info (selected_frame);
641   function = find_pc_function (fi->pc);
642
643   /* Print info on the selected frame, including level number
644      but not source.  */
645   if (from_tty)
646     {
647       printf_filtered ("Run till exit from ");
648       print_stack_frame (selected_frame, selected_frame_level, 0);
649     }
650
651   proceed_to_finish = 1;                /* We want stop_registers, please... */
652   proceed ((CORE_ADDR) -1, -1, 0);
653
654   /* Did we stop at our breakpoint? */
655   if (bpstat_find_breakpoint(stop_bpstat, breakpoint) != NULL
656       && function != 0)
657     {
658       struct type *value_type;
659       register value val;
660       CORE_ADDR funcaddr;
661
662       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
663       if (!value_type)
664         fatal ("internal: finish_command: function has no target type");
665       
666       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
667         return;
668
669       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
670
671       val = value_being_returned (value_type, stop_registers,
672               using_struct_return (value_of_variable (function),
673                                    funcaddr,
674                                    value_type,
675                 BLOCK_GCC_COMPILED (SYMBOL_BLOCK_VALUE (function))));
676
677       printf_filtered ("Value returned is $%d = ", record_latest_value (val));
678       value_print (val, stdout, 0, Val_no_prettyprint);
679       printf_filtered ("\n");
680     }
681   do_cleanups(old_chain);
682 }
683 \f
684 /* ARGSUSED */
685 static void
686 program_info (args, from_tty)
687     char *args;
688     int from_tty;
689 {
690   bpstat bs = stop_bpstat;
691   int num = bpstat_num (&bs);
692   
693   if (!target_has_execution)
694     {
695       printf_filtered ("The program being debugged is not being run.\n");
696       return;
697     }
698
699   target_files_info ();
700   printf_filtered ("Program stopped at %s.\n", local_hex_string(stop_pc));
701   if (stop_step)
702     printf_filtered ("It stopped after being stepped.\n");
703   else if (num != 0)
704     {
705       /* There may be several breakpoints in the same place, so this
706          isn't as strange as it seems.  */
707       while (num != 0)
708         {
709           if (num < 0)
710             printf_filtered ("It stopped at a breakpoint that has since been deleted.\n");
711           else
712             printf_filtered ("It stopped at breakpoint %d.\n", num);
713           num = bpstat_num (&bs);
714         }
715     }
716   else if (stop_signal) {
717 #ifdef PRINT_RANDOM_SIGNAL
718     PRINT_RANDOM_SIGNAL (stop_signal);
719 #else
720     printf_filtered ("It stopped with signal %d (%s).\n",
721                      stop_signal, safe_strsignal (stop_signal));
722 #endif
723   }
724
725   if (!from_tty)
726     printf_filtered ("Type \"info stack\" or \"info registers\" for more information.\n");
727 }
728 \f
729 static void
730 environment_info (var, from_tty)
731      char *var;
732      int from_tty;
733 {
734   if (var)
735     {
736       register char *val = get_in_environ (inferior_environ, var);
737       if (val)
738         printf_filtered ("%s = %s\n", var, val);
739       else
740         printf_filtered ("Environment variable \"%s\" not defined.\n", var);
741     }
742   else
743     {
744       register char **vector = environ_vector (inferior_environ);
745       while (*vector)
746         printf_filtered ("%s\n", *vector++);
747     }
748 }
749
750 static void
751 set_environment_command (arg, from_tty)
752      char *arg;
753      int from_tty;
754 {
755   register char *p, *val, *var;
756   int nullset = 0;
757
758   if (arg == 0)
759     error_no_arg ("environment variable and value");
760
761   /* Find seperation between variable name and value */
762   p = (char *) strchr (arg, '=');
763   val = (char *) strchr (arg, ' ');
764
765   if (p != 0 && val != 0)
766     {
767       /* We have both a space and an equals.  If the space is before the
768          equals and the only thing between the two is more space, use
769          the equals */
770       if (p > val)
771         while (*val == ' ')
772           val++;
773
774       /* Take the smaller of the two.  If there was space before the
775          "=", they will be the same right now. */
776       p = arg + min (p - arg, val - arg);
777     }
778   else if (val != 0 && p == 0)
779     p = val;
780
781   if (p == arg)
782     error_no_arg ("environment variable to set");
783
784   if (p == 0 || p[1] == 0)
785     {
786       nullset = 1;
787       if (p == 0)
788         p = arg + strlen (arg); /* So that savestring below will work */
789     }
790   else
791     {
792       /* Not setting variable value to null */
793       val = p + 1;
794       while (*val == ' ' || *val == '\t')
795         val++;
796     }
797
798   while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
799
800   var = savestring (arg, p - arg);
801   if (nullset)
802     {
803       printf_filtered ("Setting environment variable \"%s\" to null value.\n", var);
804       set_in_environ (inferior_environ, var, "");
805     }
806   else
807     set_in_environ (inferior_environ, var, val);
808   free (var);
809 }
810
811 static void
812 unset_environment_command (var, from_tty)
813      char *var;
814      int from_tty;
815 {
816   if (var == 0)
817     {
818       /* If there is no argument, delete all environment variables.
819          Ask for confirmation if reading from the terminal.  */
820       if (!from_tty || query ("Delete all environment variables? "))
821         {
822           free_environ (inferior_environ);
823           inferior_environ = make_environ ();
824         }
825     }
826   else
827     unset_in_environ (inferior_environ, var);
828 }
829
830 /* Handle the execution path (PATH variable) */
831
832 const static char path_var_name[] = "PATH";
833
834 /* ARGSUSED */
835 static void
836 path_info (args, from_tty)
837      char *args;
838      int from_tty;
839 {
840   printf_filtered ("Executable and object file path: %s\n", 
841       get_in_environ (inferior_environ, path_var_name));
842 }
843
844 /* Add zero or more directories to the front of the execution path.  */
845
846 static void
847 path_command (dirname, from_tty)
848      char *dirname;
849      int from_tty;
850 {
851   char *exec_path;
852
853   dont_repeat ();
854   exec_path = strsave (get_in_environ (inferior_environ, path_var_name));
855   mod_path (dirname, &exec_path);
856   set_in_environ (inferior_environ, path_var_name, exec_path);
857   free (exec_path);
858   if (from_tty)
859     path_info ((char *)NULL, from_tty);
860 }
861 \f
862 CORE_ADDR
863 read_pc ()
864 {
865   return ADDR_BITS_REMOVE ((CORE_ADDR) read_register (PC_REGNUM));
866 }
867
868 void
869 write_pc (val)
870      CORE_ADDR val;
871 {
872   write_register (PC_REGNUM, (long) val);
873 #ifdef NPC_REGNUM
874   write_register (NPC_REGNUM, (long) val+4);
875 #endif
876   pc_changed = 0;
877 }
878
879 const char * const reg_names[] = REGISTER_NAMES;
880
881 /* Print out the machine register regnum. If regnum is -1,
882    print all registers (fpregs == 1) or all non-float registers
883    (fpregs == 0).
884
885    For most machines, having all_registers_info() print the
886    register(s) one per line is good enough. If a different format
887    is required, (eg, for MIPS or Pyramid 90x, which both have
888    lots of regs), or there is an existing convention for showing
889    all the registers, define the macro DO_REGISTERS_INFO(regnum, fp)
890    to provide that format.  */  
891
892 #if !defined (DO_REGISTERS_INFO)
893 #define DO_REGISTERS_INFO(regnum, fp) do_registers_info(regnum, fp)
894 static void
895 do_registers_info (regnum, fpregs)
896      int regnum;
897      int fpregs;
898 {
899   register int i;
900
901   for (i = 0; i < NUM_REGS; i++)
902     {
903       char raw_buffer[MAX_REGISTER_RAW_SIZE];
904       char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
905
906       /* Decide between printing all regs, nonfloat regs, or specific reg.  */
907       if (regnum == -1) {
908         if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT && !fpregs)
909           continue;
910       } else {
911         if (i != regnum)
912           continue;
913       }
914
915       fputs_filtered (reg_names[i], stdout);
916       print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
917
918       /* Get the data in raw format, then convert also to virtual format.  */
919       if (read_relative_register_raw_bytes (i, raw_buffer))
920         {
921           printf_filtered ("Invalid register contents\n");
922           continue;
923         }
924       
925       target_convert_to_virtual (i, raw_buffer, virtual_buffer);
926
927       /* If virtual format is floating, print it that way, and in raw hex.  */
928       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
929           && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
930         {
931           register int j;
932
933           val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
934                      stdout, 0, 1, 0, Val_pretty_default);
935
936           printf_filtered ("\t(raw 0x");
937           for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
938             printf_filtered ("%02x", (unsigned char)raw_buffer[j]);
939           printf_filtered (")");
940         }
941
942 /* FIXME!  val_print probably can handle all of these cases now...  */
943
944       /* Else if virtual format is too long for printf,
945          print in hex a byte at a time.  */
946       else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
947         {
948           register int j;
949           printf_filtered ("0x");
950           for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
951             printf_filtered ("%02x", (unsigned char)virtual_buffer[j]);
952         }
953       /* Else print as integer in hex and in decimal.  */
954       else
955         {
956           val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
957                      stdout, 'x', 1, 0, Val_pretty_default);
958           printf_filtered ("\t");
959           val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
960                      stdout,   0, 1, 0, Val_pretty_default);
961         }
962
963       /* The SPARC wants to print even-numbered float regs as doubles
964          in addition to printing them as floats.  */
965 #ifdef PRINT_REGISTER_HOOK
966       PRINT_REGISTER_HOOK (i);
967 #endif
968
969       printf_filtered ("\n");
970     }
971 }
972 #endif /* no DO_REGISTERS_INFO.  */
973
974 static void
975 registers_info (addr_exp, fpregs)
976      char *addr_exp;
977      int fpregs;
978 {
979   int regnum;
980
981   if (!target_has_registers)
982     error ("The program has no registers now.");
983
984   if (addr_exp)
985     {
986       if (*addr_exp >= '0' && *addr_exp <= '9')
987         regnum = atoi (addr_exp);
988       else
989         {
990           register char *p = addr_exp;
991           if (p[0] == '$')
992             p++;
993           for (regnum = 0; regnum < NUM_REGS; regnum++)
994             if (!strcmp (p, reg_names[regnum]))
995               break;
996           if (regnum == NUM_REGS)
997             error ("%s: invalid register name.", addr_exp);
998         }
999     }
1000   else
1001     regnum = -1;
1002
1003   DO_REGISTERS_INFO(regnum, fpregs);
1004 }
1005
1006 static void
1007 all_registers_info (addr_exp, from_tty)
1008      char *addr_exp;
1009      int from_tty;
1010 {
1011   registers_info (addr_exp, 1);
1012 }
1013
1014 static void
1015 nofp_registers_info (addr_exp, from_tty)
1016      char *addr_exp;
1017      int from_tty;
1018 {
1019   registers_info (addr_exp, 0);
1020 }
1021 \f
1022 /*
1023  * TODO:
1024  * Should save/restore the tty state since it might be that the
1025  * program to be debugged was started on this tty and it wants
1026  * the tty in some state other than what we want.  If it's running
1027  * on another terminal or without a terminal, then saving and
1028  * restoring the tty state is a harmless no-op.
1029  * This only needs to be done if we are attaching to a process.
1030  */
1031
1032 /*
1033  * attach_command --
1034  * takes a program started up outside of gdb and ``attaches'' to it.
1035  * This stops it cold in its tracks and allows us to start tracing it.
1036  * For this to work, we must be able to send the process a
1037  * signal and we must have the same effective uid as the program.
1038  */
1039 void
1040 attach_command (args, from_tty)
1041      char *args;
1042      int from_tty;
1043 {
1044   dont_repeat ();                       /* Not for the faint of heart */
1045   target_attach (args, from_tty);
1046 }
1047
1048 /*
1049  * detach_command --
1050  * takes a program previously attached to and detaches it.
1051  * The program resumes execution and will no longer stop
1052  * on signals, etc.  We better not have left any breakpoints
1053  * in the program or it'll die when it hits one.  For this
1054  * to work, it may be necessary for the process to have been
1055  * previously attached.  It *might* work if the program was
1056  * started via the normal ptrace (PTRACE_TRACEME).
1057  */
1058
1059 static void
1060 detach_command (args, from_tty)
1061      char *args;
1062      int from_tty;
1063 {
1064   dont_repeat ();                       /* Not for the faint of heart */
1065   target_detach (args, from_tty);
1066 }
1067
1068 /* ARGSUSED */
1069 static void
1070 float_info (addr_exp, from_tty)
1071      char *addr_exp;
1072      int from_tty;
1073 {
1074 #ifdef FLOAT_INFO
1075         FLOAT_INFO;
1076 #else
1077         printf_filtered ("No floating point info available for this processor.\n");
1078 #endif
1079 }
1080 \f
1081 struct cmd_list_element *unsetlist = NULL;
1082
1083 /* ARGSUSED */
1084 static void
1085 unset_command (args, from_tty)
1086      char *args;
1087      int from_tty;
1088 {
1089   printf_filtered ("\"unset\" must be followed by the name of an unset subcommand.\n");
1090   help_list (unsetlist, "unset ", -1, stdout);
1091 }
1092
1093 void
1094 _initialize_infcmd ()
1095 {
1096   struct cmd_list_element *c;
1097   
1098   add_com ("tty", class_run, tty_command,
1099            "Set terminal for future runs of program being debugged.");
1100
1101   add_show_from_set
1102     (add_set_cmd ("args", class_run, var_string_noescape, (char *)&inferior_args,
1103                   
1104 "Set arguments to give program being debugged when it is started.\n\
1105 Follow this command with any number of args, to be passed to the program.",
1106                   &setlist),
1107      &showlist);
1108
1109   c = add_cmd
1110     ("environment", no_class, environment_info,
1111      "The environment to give the program, or one variable's value.\n\
1112 With an argument VAR, prints the value of environment variable VAR to\n\
1113 give the program being debugged.  With no arguments, prints the entire\n\
1114 environment to be given to the program.", &showlist);
1115   c->completer = noop_completer;
1116
1117   add_prefix_cmd ("unset", no_class, unset_command,
1118                   "Complement to certain \"set\" commands",
1119                   &unsetlist, "unset ", 0, &cmdlist);
1120   
1121   c = add_cmd ("environment", class_run, unset_environment_command,
1122               "Cancel environment variable VAR for the program.\n\
1123 This does not affect the program until the next \"run\" command.",
1124            &unsetlist);
1125   c->completer = noop_completer;
1126
1127   c = add_cmd ("environment", class_run, set_environment_command,
1128                "Set environment variable value to give the program.\n\
1129 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
1130 VALUES of environment variables are uninterpreted strings.\n\
1131 This does not affect the program until the next \"run\" command.",
1132            &setlist);
1133   c->completer = noop_completer;
1134  
1135   add_com ("path", class_files, path_command,
1136        "Add directory DIR(s) to beginning of search path for object files.\n\
1137 $cwd in the path means the current working directory.\n\
1138 This path is equivalent to the $PATH shell variable.  It is a list of\n\
1139 directories, separated by colons.  These directories are searched to find\n\
1140 fully linked executable files and separately compiled object files as needed.");
1141
1142   c = add_cmd ("paths", no_class, path_info,
1143             "Current search path for finding object files.\n\
1144 $cwd in the path means the current working directory.\n\
1145 This path is equivalent to the $PATH shell variable.  It is a list of\n\
1146 directories, separated by colons.  These directories are searched to find\n\
1147 fully linked executable files and separately compiled object files as needed.", &showlist);
1148   c->completer = noop_completer;
1149
1150  add_com ("attach", class_run, attach_command,
1151            "Attach to a process or file outside of GDB.\n\
1152 This command attaches to another target, of the same type as your last\n\
1153 `target' command (`info files' will show your target stack).\n\
1154 The command may take as argument a process id or a device file.\n\
1155 For a process id, you must have permission to send the process a signal,\n\
1156 and it must have the same effective uid as the debugger.\n\
1157 When using \"attach\", you should use the \"file\" command to specify\n\
1158 the program running in the process, and to load its symbol table.");
1159
1160   add_com ("detach", class_run, detach_command,
1161            "Detach a process or file previously attached.\n\
1162 If a process, it is no longer traced, and it continues its execution.  If you\n\
1163 were debugging a file, the file is closed and gdb no longer accesses it.");
1164
1165   add_com ("signal", class_run, signal_command,
1166            "Continue program giving it signal number SIGNUMBER.");
1167
1168   add_com ("stepi", class_run, stepi_command,
1169            "Step one instruction exactly.\n\
1170 Argument N means do this N times (or till program stops for another reason).");
1171   add_com_alias ("si", "stepi", class_alias, 0);
1172
1173   add_com ("nexti", class_run, nexti_command,
1174            "Step one instruction, but proceed through subroutine calls.\n\
1175 Argument N means do this N times (or till program stops for another reason).");
1176   add_com_alias ("ni", "nexti", class_alias, 0);
1177
1178   add_com ("finish", class_run, finish_command,
1179            "Execute until selected stack frame returns.\n\
1180 Upon return, the value returned is printed and put in the value history.");
1181
1182   add_com ("next", class_run, next_command,
1183            "Step program, proceeding through subroutine calls.\n\
1184 Like the \"step\" command as long as subroutine calls do not happen;\n\
1185 when they do, the call is treated as one instruction.\n\
1186 Argument N means do this N times (or till program stops for another reason).");
1187   add_com_alias ("n", "next", class_run, 1);
1188
1189   add_com ("step", class_run, step_command,
1190            "Step program until it reaches a different source line.\n\
1191 Argument N means do this N times (or till program stops for another reason).");
1192   add_com_alias ("s", "step", class_run, 1);
1193
1194   add_com ("until", class_run, until_command,
1195            "Execute until the program reaches a source line greater than the current\n\
1196 or a specified line or address or function (same args as break command).\n\
1197 Execution will also stop upon exit from the current stack frame.");
1198   add_com_alias ("u", "until", class_run, 1);
1199   
1200   add_com ("jump", class_run, jump_command,
1201            "Continue program being debugged at specified line or address.\n\
1202 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1203 for an address to start at.");
1204
1205   add_com ("continue", class_run, continue_command,
1206            "Continue program being debugged, after signal or breakpoint.\n\
1207 If proceeding from breakpoint, a number N may be used as an argument:\n\
1208 then the same breakpoint won't break until the Nth time it is reached.");
1209   add_com_alias ("c", "cont", class_run, 1);
1210   add_com_alias ("fg", "cont", class_run, 1);
1211
1212   add_com ("run", class_run, run_command,
1213            "Start debugged program.  You may specify arguments to give it.\n\
1214 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
1215 Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
1216 With no arguments, uses arguments last specified (with \"run\" or \"set args\").\n\
1217 To cancel previous arguments and run with no arguments,\n\
1218 use \"set args\" without arguments.");
1219   add_com_alias ("r", "run", class_run, 1);
1220
1221   add_info ("registers", nofp_registers_info,
1222     "List of integer registers and their contents, for selected stack frame.\n\
1223 Register name as argument means describe only that register.");
1224
1225   add_info ("all-registers", all_registers_info,
1226 "List of all registers and their contents, for selected stack frame.\n\
1227 Register name as argument means describe only that register.");
1228
1229   add_info ("program", program_info,
1230             "Execution status of the program.");
1231
1232   add_info ("float", float_info,
1233             "Print the status of the floating point unit\n");
1234
1235   inferior_args = savestring ("", 1);   /* Initially no args */
1236   inferior_environ = make_environ ();
1237   init_environ (inferior_environ);
1238 }