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