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