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