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