BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / gdb / infcmd.c
CommitLineData
af359dea
C
1/*-
2 * This code is derived from software copyrighted by the Free Software
3 * Foundation.
4 *
5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6 * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
7 */
8
9#ifndef lint
10static char sccsid[] = "@(#)infcmd.c 6.4 (Berkeley) 5/8/91";
11#endif /* not lint */
12
13/* Memory-access and commands for inferior process, for GDB.
14 Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
15
16This file is part of GDB.
17
18GDB is free software; you can redistribute it and/or modify
19it under the terms of the GNU General Public License as published by
20the Free Software Foundation; either version 1, or (at your option)
21any later version.
22
23GDB is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26GNU General Public License for more details.
27
28You should have received a copy of the GNU General Public License
29along with GDB; see the file COPYING. If not, write to
30the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
31
32#include <stdio.h>
33#include "defs.h"
34#include "param.h"
35#include "symtab.h"
36#include "frame.h"
37#include "inferior.h"
38#include "environ.h"
39#include "value.h"
40
41#include <signal.h>
42#include <sys/param.h>
43
44extern char *sys_siglist[];
45
46#define ERROR_NO_INFERIOR \
47 if (inferior_pid == 0) error ("The program is not being run.");
48
49/* String containing arguments to give to the program,
50 with a space added at the front. Just a space means no args. */
51
52static char *inferior_args;
53
54/* File name for default use for standard in/out in the inferior. */
55
56char *inferior_io_terminal;
57
58/* Pid of our debugged inferior, or 0 if no inferior now. */
59
60int inferior_pid;
61
62/* Last signal that the inferior received (why it stopped). */
63
64int stop_signal;
65
66/* Address at which inferior stopped. */
67
68CORE_ADDR stop_pc;
69
70/* Stack frame when program stopped. */
71
72FRAME_ADDR stop_frame_address;
73
74/* Number of breakpoint it stopped at, or 0 if none. */
75
76int stop_breakpoint;
77
78/* Nonzero if stopped due to a step command. */
79
80int stop_step;
81
82/* Nonzero if stopped due to completion of a stack dummy routine. */
83
84int stop_stack_dummy;
85
86/* Nonzero if stopped due to a random (unexpected) signal in inferior
87 process. */
88
89int stopped_by_random_signal;
90
91/* Range to single step within.
92 If this is nonzero, respond to a single-step signal
93 by continuing to step if the pc is in this range. */
94
95CORE_ADDR step_range_start; /* Inclusive */
96CORE_ADDR step_range_end; /* Exclusive */
97
98/* Stack frame address as of when stepping command was issued.
99 This is how we know when we step into a subroutine call,
100 and how to set the frame for the breakpoint used to step out. */
101
102FRAME_ADDR step_frame_address;
103
104/* 1 means step over all subroutine calls.
105 -1 means step over calls to undebuggable functions. */
106
107int step_over_calls;
108
109/* If stepping, nonzero means step count is > 1
110 so don't print frame next time inferior stops
111 if it stops due to stepping. */
112
113int step_multi;
114
115/* Environment to use for running inferior,
116 in format described in environ.h. */
117
118struct environ *inferior_environ;
119
120CORE_ADDR read_pc ();
121struct command_line *get_breakpoint_commands ();
122void breakpoint_clear_ignore_counts ();
123
124\f
125int
126have_inferior_p ()
127{
128 return inferior_pid != 0;
129}
130
131static void
132set_args_command (args)
133 char *args;
134{
135 free (inferior_args);
136 if (!args) args = "";
137 inferior_args = concat (" ", args, "");
138}
139
140void
141tty_command (file, from_tty)
142 char *file;
143 int from_tty;
144{
145 if (file == 0)
146 error_no_arg ("terminal name for running target process");
147
148 inferior_io_terminal = savestring (file, strlen (file));
149}
150
151static void
152run_command (args, from_tty)
153 char *args;
154 int from_tty;
155{
156 extern char **environ;
157 register int i;
158 char *exec_file;
159 char *allargs;
160
161 extern int sys_nerr;
162 extern char *sys_errlist[];
163 extern int errno;
164
165 dont_repeat ();
166
167 if (inferior_pid)
168 {
169 extern int inhibit_confirm;
170 if (!(inhibit_confirm ||
171 query ("The program being debugged has been started already.\n\
172Start it from the beginning? ")))
173 error ("Program not restarted.");
174 kill_inferior ();
175 }
176
177#if 0
178 /* On the other hand, some users want to do
179 break open
180 ignore 1 40
181 run
182 So it's not clear what is best. */
183
184 /* It is confusing to the user for ignore counts to stick around
185 from previous runs of the inferior. So clear them. */
186 breakpoint_clear_ignore_counts ();
187#endif
188
189 exec_file = (char *) get_exec_file (1);
190
191 if (remote_debugging)
192 {
193 if (from_tty)
194 {
195 printf ("Starting program: %s\n", exec_file);
196 fflush (stdout);
197 }
198 }
199 else
200 {
201 if (args)
202 set_args_command (args);
203
204 if (from_tty)
205 {
206 printf ("Starting program: %s%s\n",
207 exec_file, inferior_args);
208 fflush (stdout);
209 }
210
211 allargs = concat ("exec ", exec_file, inferior_args);
212 inferior_pid = create_inferior (allargs, environ_vector (inferior_environ));
213 }
214
215 clear_proceed_status ();
216
217 start_inferior ();
218}
219\f
220void
221cont_command (proc_count_exp, from_tty)
222 char *proc_count_exp;
223 int from_tty;
224{
225 ERROR_NO_INFERIOR;
226
227 clear_proceed_status ();
228
229 /* If have argument, set proceed count of breakpoint we stopped at. */
230
231 if (stop_breakpoint > 0 && proc_count_exp)
232 {
233 set_ignore_count (stop_breakpoint,
234 parse_and_eval_address (proc_count_exp) - 1,
235 from_tty);
236 if (from_tty)
237 printf (" ");
238 }
239
240 if (from_tty)
241 printf ("Continuing.\n");
242
243 proceed (-1, -1, 0);
244}
245\f
246/* Step until outside of current statement. */
247static void step_1 ();
248
249static void
250step_command (count_string)
251{
252 step_1 (0, 0, count_string);
253}
254
255/* Likewise, but skip over subroutine calls as if single instructions. */
256
257static void
258next_command (count_string)
259{
260 step_1 (1, 0, count_string);
261}
262
263/* Likewise, but step only one instruction. */
264
265static void
266stepi_command (count_string)
267{
268 step_1 (0, 1, count_string);
269}
270
271static void
272nexti_command (count_string)
273{
274 step_1 (1, 1, count_string);
275}
276
277static void
278step_1 (skip_subroutines, single_inst, count_string)
279 int skip_subroutines;
280 int single_inst;
281 char *count_string;
282{
283 register int count = 1;
284
285 ERROR_NO_INFERIOR;
286 count = count_string ? parse_and_eval_address (count_string) : 1;
287
288 for (; count > 0; count--)
289 {
290 clear_proceed_status ();
291
292 step_frame_address = FRAME_FP (get_current_frame ());
293
294 if (! single_inst)
295 {
296 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
297 if (step_range_end == 0)
298 {
299 int misc;
300
301 misc = find_pc_misc_function (stop_pc);
302 terminal_ours ();
303 printf ("Current function has no line number information.\n");
304 fflush (stdout);
305
306 /* No info or after _etext ("Can't happen") */
307 if (misc == -1 || misc == misc_function_count - 1)
308 error ("No data available on pc function.");
309
310 printf ("Single stepping until function exit.\n");
311 fflush (stdout);
312
313 step_range_start = misc_function_vector[misc].address;
314 step_range_end = misc_function_vector[misc + 1].address;
315 }
316 }
317 else
318 {
319 /* Say we are stepping, but stop after one insn whatever it does.
320 Don't step through subroutine calls even to undebuggable
321 functions. */
322 step_range_start = step_range_end = 1;
323 if (!skip_subroutines)
324 step_over_calls = 0;
325 }
326
327 if (skip_subroutines)
328 step_over_calls = 1;
329
330 step_multi = (count > 1);
331 proceed (-1, -1, 1);
332 if (! stop_step)
333 break;
334 }
335}
336\f
337/* Continue program at specified address. */
338
339static void
340jump_command (arg, from_tty)
341 char *arg;
342 int from_tty;
343{
344 register CORE_ADDR addr;
345 struct symtabs_and_lines sals;
346 struct symtab_and_line sal;
347
348 ERROR_NO_INFERIOR;
349
350 if (!arg)
351 error_no_arg ("starting address");
352
353 sals = decode_line_spec_1 (arg, 1);
354 if (sals.nelts != 1)
355 {
356 error ("Unreasonable jump request");
357 }
358
359 sal = sals.sals[0];
360 free (sals.sals);
361
362 if (sal.symtab == 0 && sal.pc == 0)
363 error ("No source file has been specified.");
364
365 if (sal.pc == 0)
366 sal.pc = find_line_pc (sal.symtab, sal.line);
367
368 {
369 struct symbol *fn = get_frame_function (get_current_frame ());
370 struct symbol *sfn = find_pc_function (sal.pc);
371 if (fn != 0 && sfn != fn
372 && ! query ("Line %d is not in `%s'. Jump anyway? ",
373 sal.line, SYMBOL_NAME (fn)))
374 error ("Not confirmed.");
375 }
376
377 if (sal.pc == 0)
378 error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
379
380 addr = sal.pc;
381
382 clear_proceed_status ();
383
384 if (from_tty)
385 printf ("Continuing at 0x%x.\n", addr);
386
387 proceed (addr, 0, 0);
388}
389
390/* Continue program giving it specified signal. */
391
392static void
393signal_command (signum_exp, from_tty)
394 char *signum_exp;
395 int from_tty;
396{
397 register int signum;
398
399 dont_repeat (); /* Too dangerous. */
400 ERROR_NO_INFERIOR;
401
402 if (!signum_exp)
403 error_no_arg ("signal number");
404
405 signum = parse_and_eval_address (signum_exp);
406
407 clear_proceed_status ();
408
409 if (from_tty)
410 printf ("Continuing with signal %d.\n", signum);
411
412 proceed (stop_pc, signum, 0);
413}
414
415/* Execute a "stack dummy", a piece of code stored in the stack
416 by the debugger to be executed in the inferior.
417
418 To call: first, do PUSH_DUMMY_FRAME.
419 Then push the contents of the dummy. It should end with a breakpoint insn.
420 Then call here, passing address at which to start the dummy.
421
422 The contents of all registers are saved before the dummy frame is popped
423 and copied into the buffer BUFFER.
424
425 The dummy's frame is automatically popped whenever that break is hit.
426 If that is the first time the program stops, run_stack_dummy
427 returns to its caller with that frame already gone.
428 Otherwise, the caller never gets returned to. */
429
430/* 4 => return instead of letting the stack dummy run. */
431
432static int stack_dummy_testing = 0;
433
434void
435run_stack_dummy (addr, buffer)
436 CORE_ADDR addr;
437 REGISTER_TYPE *buffer;
438{
439 /* Now proceed, having reached the desired place. */
440 clear_proceed_status ();
441#ifdef notdef
442 if (stack_dummy_testing & 4)
443 {
444 POP_FRAME;
445 return;
446 }
447#endif
448 proceed (addr, 0, 0);
449
450 if (!stop_stack_dummy)
451 error ("Cannot continue previously requested operation.");
452
453 /* On return, the stack dummy has been popped already. */
454
455 read_register_bytes(0, buffer, REGISTER_BYTES);
456}
457\f
458/* Proceed until we reach the given line as argument or exit the
459 function. When called with no argument, proceed until we reach a
460 different source line with pc greater than our current one or exit
461 the function. We skip calls in both cases.
462
463 The effect of this command with an argument is identical to setting
464 a momentary breakpoint at the line specified and executing
465 "finish".
466
467 Note that eventually this command should probably be changed so
468 that only source lines are printed out when we hit the breakpoint
469 we set. I'm going to postpone this until after a hopeful rewrite
470 of wait_for_inferior and the proceed status code. -- randy */
471
472void
473until_next_command (arg, from_tty)
474 char *arg;
475 int from_tty;
476{
477 FRAME frame;
478 CORE_ADDR pc;
479 struct symbol *func;
480 struct symtab_and_line sal;
481
482 clear_proceed_status ();
483
484 frame = get_current_frame ();
485
486 /* Step until either exited from this function or greater
487 than the current line (if in symbolic section) or pc (if
488 not). */
489
490 pc = read_pc ();
491 func = find_pc_function (pc);
492
493 if (!func)
494 {
495 int misc_func = find_pc_misc_function (pc);
496
497 if (misc_func != -1)
498 error ("Execution is not within a known function.");
499
500 step_range_start = misc_function_vector[misc_func].address;
501 step_range_end = pc;
502 }
503 else
504 {
505 sal = find_pc_line (pc, 0);
506
507 step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
508 step_range_end = sal.end;
509 }
510
511 step_over_calls = 1;
512 step_frame_address = FRAME_FP (frame);
513
514 step_multi = 0; /* Only one call to proceed */
515
516 proceed (-1, -1, 1);
517}
518
519void
520until_command (arg, from_tty)
521 char *arg;
522 int from_tty;
523{
524 if (!have_inferior_p ())
525 error ("The program is not being run.");
526
527 if (arg)
528 until_break_command (arg, from_tty);
529 else
530 until_next_command (arg, from_tty);
531}
532\f
533/* "finish": Set a temporary breakpoint at the place
534 the selected frame will return to, then continue. */
535
536static void
537finish_command (arg, from_tty)
538 char *arg;
539 int from_tty;
540{
541 struct symtab_and_line sal;
542 register FRAME frame;
543 struct frame_info *fi;
544 register struct symbol *function;
545
546 if (!have_inferior_p ())
547 error ("The program is not being run.");
548 if (arg)
549 error ("The \"finish\" command does not take any arguments.");
550
551 frame = get_prev_frame (selected_frame);
552 if (frame == 0)
553 error ("\"finish\" not meaningful in the outermost frame.");
554
555 clear_proceed_status ();
556
557 fi = get_frame_info (frame);
558 sal = find_pc_line (fi->pc, 0);
559 sal.pc = fi->pc;
560 set_momentary_breakpoint (sal, frame);
561
562 /* Find the function we will return from. */
563
564 fi = get_frame_info (selected_frame);
565 function = find_pc_function (fi->pc);
566
567 if (from_tty)
568 {
569 printf ("Run till exit from ");
570 print_selected_frame ();
571 }
572
573 proceed (-1, -1, 0);
574
575 if (stop_breakpoint == -3 && function != 0)
576 {
577 struct type *value_type;
578 register value val;
579 CORE_ADDR funcaddr;
580 extern char registers[];
581
582 value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
583 if (!value_type)
584 fatal ("internal: finish_command: function has no target type");
585
586 if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
587 return;
588
589 funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
590
591 val = value_being_returned (value_type, registers,
592 using_struct_return (function,
593 funcaddr,
594 value_type));
595
596 printf ("Value returned is $%d = ", record_latest_value (val));
597 value_print (val, stdout, 0, Val_no_prettyprint);
598 putchar ('\n');
599 }
600}
601\f
602static void
603program_info ()
604{
605 if (inferior_pid == 0)
606 {
607 printf ("The program being debugged is not being run.\n");
608 return;
609 }
610
611 printf ("Program being debugged is in process %d, stopped at 0x%x.\n",
612 inferior_pid, stop_pc);
613 if (stop_step)
614 printf ("It stopped after being stepped.\n");
615 else if (stop_breakpoint > 0)
616 printf ("It stopped at breakpoint %d.\n", stop_breakpoint);
617 else if (stop_signal)
618 printf ("It stopped with signal %d (%s).\n",
619 stop_signal, sys_siglist[stop_signal]);
620
621 printf ("\nType \"info stack\" or \"info reg\" for more information.\n");
622}
623\f
624static void
625environment_info (var)
626 char *var;
627{
628 if (var)
629 {
630 register char *val = get_in_environ (inferior_environ, var);
631 if (val)
632 printf ("%s = %s\n", var, val);
633 else
634 printf ("Environment variable \"%s\" not defined.\n", var);
635 }
636 else
637 {
638 register char **vector = environ_vector (inferior_environ);
639 while (*vector)
640 printf ("%s\n", *vector++);
641 }
642}
643
644static void
645set_environment_command (arg)
646 char *arg;
647{
648 register char *p, *val, *var;
649 int nullset = 0;
650
651 if (arg == 0)
652 error_no_arg ("environment variable and value");
653
654 /* Find seperation between variable name and value */
655 p = (char *) index (arg, '=');
656 val = (char *) index (arg, ' ');
657
658 if (p != 0 && val != 0)
659 {
660 /* We have both a space and an equals. If the space is before the
661 equals and the only thing between the two is more space, use
662 the equals */
663 if (p > val)
664 while (*val == ' ')
665 val++;
666
667 /* Take the smaller of the two. If there was space before the
668 "=", they will be the same right now. */
669 p = arg + min (p - arg, val - arg);
670 }
671 else if (val != 0 && p == 0)
672 p = val;
673
674 if (p == arg)
675 error_no_arg ("environment variable to set");
676
677 if (p == 0 || p[1] == 0)
678 {
679 nullset = 1;
680 if (p == 0)
681 p = arg + strlen (arg); /* So that savestring below will work */
682 }
683 else
684 {
685 /* Not setting variable value to null */
686 val = p + 1;
687 while (*val == ' ' || *val == '\t')
688 val++;
689 }
690
691 while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
692
693 var = savestring (arg, p - arg);
694 if (nullset)
695 {
696 printf ("Setting environment variable \"%s\" to null value.\n", var);
697 set_in_environ (inferior_environ, var, "");
698 }
699 else
700 set_in_environ (inferior_environ, var, val);
701 free (var);
702}
703
704static void
705unset_environment_command (var, from_tty)
706 char *var;
707 int from_tty;
708{
709 if (var == 0)
710 /* If there is no argument, delete all environment variables.
711 Ask for confirmation if reading from the terminal. */
712 if (!from_tty || query ("Delete all environment variables? "))
713 {
714 free_environ (inferior_environ);
715 inferior_environ = make_environ ();
716 }
717
718 unset_in_environ (inferior_environ, var);
719}
720\f
721/* Read an integer from debugged memory, given address and number of bytes. */
722
723long
724read_memory_integer (memaddr, len)
725 CORE_ADDR memaddr;
726 int len;
727{
728 char cbuf;
729 short sbuf;
730 int ibuf;
731 long lbuf;
732 int result_err;
733 extern int sys_nerr;
734 extern char *sys_errlist[];
735
736 if (len == sizeof (char))
737 {
738 result_err = read_memory (memaddr, &cbuf, len);
739 if (result_err)
740 error ("Error reading memory address 0x%x: %s (%d).",
741 memaddr, (result_err < sys_nerr ?
742 sys_errlist[result_err] :
743 "uknown error"), result_err);
744 return cbuf;
745 }
746 if (len == sizeof (short))
747 {
748 result_err = read_memory (memaddr, &sbuf, len);
749 if (result_err)
750 error ("Error reading memory address 0x%x: %s (%d).",
751 memaddr, (result_err < sys_nerr ?
752 sys_errlist[result_err] :
753 "uknown error"), result_err);
754 return sbuf;
755 }
756 if (len == sizeof (int))
757 {
758 result_err = read_memory (memaddr, &ibuf, len);
759 if (result_err)
760 error ("Error reading memory address 0x%x: %s (%d).",
761 memaddr, (result_err < sys_nerr ?
762 sys_errlist[result_err] :
763 "uknown error"), result_err);
764 return ibuf;
765 }
766 if (len == sizeof (lbuf))
767 {
768 result_err = read_memory (memaddr, &lbuf, len);
769 if (result_err)
770 error ("Error reading memory address 0x%x: %s (%d).",
771 memaddr, (result_err < sys_nerr ?
772 sys_errlist[result_err] :
773 "uknown error"), result_err);
774 return lbuf;
775 }
776 error ("Cannot handle integers of %d bytes.", len);
777}
778\f
779CORE_ADDR
780read_pc ()
781{
782 return (CORE_ADDR) read_register (PC_REGNUM);
783}
784
785void
786write_pc (val)
787 CORE_ADDR val;
788{
789 write_register (PC_REGNUM, (long) val);
790#ifdef NPC_REGNUM
791 write_register (NPC_REGNUM, (long) val+4);
792#endif
793}
794
795char *reg_names[] = REGISTER_NAMES;
796
797#if !defined (DO_REGISTERS_INFO)
798static void
799print_one_register(i)
800 int i;
801{
802 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
803 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
804 REGISTER_TYPE val;
805
806 /* Get the data in raw format, then convert also to virtual format. */
807 read_relative_register_raw_bytes (i, raw_buffer);
808 REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
809
810 fputs_filtered (reg_names[i], stdout);
811 print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
812
813 /* If virtual format is floating, print it that way. */
814 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
815 && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
816 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
817 stdout, 0, 1, 0, Val_pretty_default);
818 /* Else if virtual format is too long for printf,
819 print in hex a byte at a time. */
820 else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
821 {
822 register int j;
823 printf_filtered ("0x");
824 for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
825 printf_filtered ("%02x", virtual_buffer[j]);
826 }
827 /* Else print as integer in hex and in decimal. */
828 else
829 {
830 long val;
831
832 bcopy (virtual_buffer, &val, sizeof (long));
833 if (val == 0)
834 printf_filtered ("0");
835 else
836 printf_filtered ("0x%08x %d", val, val);
837 }
838
839 /* If register has different raw and virtual formats,
840 print the raw format in hex now. */
841
842 if (REGISTER_CONVERTIBLE (i))
843 {
844 register int j;
845
846 printf_filtered (" (raw 0x");
847 for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
848 printf_filtered ("%02x", raw_buffer[j]);
849 printf_filtered (")");
850 }
851 printf_filtered ("\n");
852}
853
854
855/* Print out the machine register regnum. If regnum is -1,
856 print all registers.
857 For most machines, having all_registers_info() print the
858 register(s) one per line is good enough. If a different format
859 is required, (eg, for SPARC or Pyramid 90x, which both have
860 lots of regs), or there is an existing convention for showing
861 all the registers, define the macro DO_REGISTERS_INFO(regnum)
862 to provide that format. */
863static void
864do_registers_info (regnum, fpregs)
865 int regnum;
866 int fpregs;
867{
868 register int i;
869
870 if (regnum >= 0) {
871 print_one_register(regnum);
872 return;
873 }
874#ifdef notdef
875 printf_filtered (
876"Register Contents (relative to selected stack frame)\n\n");
877#endif
878 for (i = 0; i < NUM_REGS; i++)
879 if (TYPE_CODE(REGISTER_VIRTUAL_TYPE(i)) != TYPE_CODE_FLT ||
880 fpregs)
881 print_one_register(i);
882}
883#endif /* no DO_REGISTERS_INFO. */
884
885static void
886registers_info (addr_exp, fpregs)
887 char *addr_exp;
888 int fpregs;
889{
890 int regnum;
891
892 if (!have_inferior_p () && !have_core_file_p ())
893 error ("No inferior or core file");
894
895 if (addr_exp)
896 {
897 if (*addr_exp >= '0' && *addr_exp <= '9')
898 regnum = atoi (addr_exp);
899 else
900 {
901 register char *p = addr_exp;
902 if (p[0] == '$')
903 p++;
904 for (regnum = 0; regnum < NUM_REGS; regnum++)
905 if (!strcmp (p, reg_names[regnum]))
906 break;
907 if (regnum == NUM_REGS)
908 error ("%s: invalid register name.", addr_exp);
909 }
910 }
911 else
912 regnum = -1;
913
914#ifdef DO_REGISTERS_INFO
915 DO_REGISTERS_INFO(regnum);
916#else
917 do_registers_info(regnum, fpregs);
918#endif
919}
920
921static void
922all_registers_info (addr_exp)
923 char *addr_exp;
924{
925 registers_info(addr_exp, 1);
926}
927
928static void
929nofp_registers_info (addr_exp)
930 char *addr_exp;
931{
932 registers_info(addr_exp, 0);
933}
934
935\f
936#ifdef ATTACH_DETACH
937#define PROCESS_ATTACH_ALLOWED 1
938#else
939#define PROCESS_ATTACH_ALLOWED 0
940#endif
941/*
942 * TODO:
943 * Should save/restore the tty state since it might be that the
944 * program to be debugged was started on this tty and it wants
945 * the tty in some state other than what we want. If it's running
946 * on another terminal or without a terminal, then saving and
947 * restoring the tty state is a harmless no-op.
948 * This only needs to be done if we are attaching to a process.
949 */
950
951/*
952 * attach_command --
953 * takes a program started up outside of gdb and ``attaches'' to it.
954 * This stops it cold in its tracks and allows us to start tracing it.
955 * For this to work, we must be able to send the process a
956 * signal and we must have the same effective uid as the program.
957 */
958static void
959attach_command (args, from_tty)
960 char *args;
961 int from_tty;
962{
963 char *exec_file;
964 int pid;
965 int remote = 0;
966
967 dont_repeat();
968
969 if (!args)
970 error_no_arg ("process-id or device file to attach");
971
972 while (*args == ' ' || *args == '\t') args++;
973
974 if (args[0] < '0' || args[0] > '9')
975 remote = 1;
976 else
977#ifndef ATTACH_DETACH
978 error ("Can't attach to a process on this machine.");
979#else
980 pid = atoi (args);
981#endif
982
983 if (inferior_pid)
984 {
985 if (query ("A program is being debugged already. Kill it? "))
986 kill_inferior ();
987 else
988 error ("Inferior not killed.");
989 }
990
991 exec_file = (char *) get_exec_file (1);
992
993 if (from_tty)
994 {
995 if (remote)
996 printf ("Attaching remote machine\n");
997 else
998 printf ("Attaching program: %s pid %d\n",
999 exec_file, pid);
1000 fflush (stdout);
1001 }
1002
1003 if (remote)
1004 {
1005 remote_open (args, from_tty);
1006 start_remote ();
1007 }
1008#ifdef ATTACH_DETACH
1009 else
1010 attach_program (pid);
1011#endif
1012}
1013
1014/*
1015 * detach_command --
1016 * takes a program previously attached to and detaches it.
1017 * The program resumes execution and will no longer stop
1018 * on signals, etc. We better not have left any breakpoints
1019 * in the program or it'll die when it hits one. For this
1020 * to work, it may be necessary for the process to have been
1021 * previously attached. It *might* work if the program was
1022 * started via the normal ptrace (PTRACE_TRACEME).
1023 */
1024
1025static void
1026detach_command (args, from_tty)
1027 char *args;
1028 int from_tty;
1029{
1030 int signal = 0;
1031
1032#ifdef ATTACH_DETACH
1033 if (inferior_pid && !remote_debugging)
1034 {
1035 if (from_tty)
1036 {
1037 char *exec_file = (char *)get_exec_file (0);
1038 if (exec_file == 0)
1039 exec_file = "";
1040 printf ("Detaching program: %s pid %d\n",
1041 exec_file, inferior_pid);
1042 fflush (stdout);
1043 }
1044 if (args)
1045 signal = atoi (args);
1046
1047 detach (signal);
1048 inferior_pid = 0;
1049 }
1050 else
1051#endif
1052 {
1053 if (!remote_debugging)
1054 error ("Not currently attached to subsidiary or remote process.");
1055
1056 if (args)
1057 error ("Argument given to \"detach\" when remotely debugging.");
1058
1059 inferior_pid = 0;
1060 remote_close (from_tty);
1061 }
1062}
1063
1064/* ARGSUSED */
1065static void
1066float_info (addr_exp)
1067 char *addr_exp;
1068{
1069#ifdef FLOAT_INFO
1070 FLOAT_INFO;
1071#else
1072 printf ("No floating point info available for this processor.\n");
1073#endif
1074}
1075\f
1076extern struct cmd_list_element *setlist, *deletelist;
1077
1078void
1079_initialize_infcmd ()
1080{
1081 add_com ("tty", class_run, tty_command,
1082 "Set terminal for future runs of program being debugged.");
1083
1084 add_cmd ("args", class_run, set_args_command,
1085 "Specify arguments to give program being debugged when it is started.\n\
1086Follow this command with any number of args, to be passed to the program.",
1087 &setlist);
1088
1089 add_info ("environment", environment_info,
1090 "The environment to give the program, or one variable's value.\n\
1091With an argument VAR, prints the value of environment variable VAR to\n\
1092give the program being debugged. With no arguments, prints the entire\n\
1093environment to be given to the program.");
1094
1095 add_cmd ("environment", class_run, unset_environment_command,
1096 "Cancel environment variable VAR for the program.\n\
1097This does not affect the program until the next \"run\" command.",
1098 &deletelist);
1099
1100 add_cmd ("environment", class_run, set_environment_command,
1101 "Set environment variable value to give the program.\n\
1102Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
1103VALUES of environment variables are uninterpreted strings.\n\
1104This does not affect the program until the next \"run\" command.",
1105 &setlist);
1106
1107#ifdef ATTACH_DETACH
1108 add_com ("attach", class_run, attach_command,
1109 "Attach to a process that was started up outside of GDB.\n\
1110This command may take as argument a process id or a device file.\n\
1111For a process id, you must have permission to send the process a signal,\n\
1112and it must have the same effective uid as the debugger.\n\
1113For a device file, the file must be a connection to a remote debug server.\n\n\
1114Before using \"attach\", you must use the \"exec-file\" command\n\
1115to specify the program running in the process,\n\
1116and the \"symbol-file\" command to load its symbol table.");
1117#else
1118 add_com ("attach", class_run, attach_command,
1119 "Attach to a process that was started up outside of GDB.\n\
1120This commands takes as an argument the name of a device file.\n\
1121This file must be a connection to a remote debug server.\n\n\
1122Before using \"attach\", you must use the \"exec-file\" command\n\
1123to specify the program running in the process,\n\
1124and the \"symbol-file\" command to load its symbol table.");
1125#endif
1126 add_com ("detach", class_run, detach_command,
1127 "Detach the process previously attached.\n\
1128The process is no longer traced and continues its execution.");
1129
1130 add_com ("signal", class_run, signal_command,
1131 "Continue program giving it signal number SIGNUMBER.");
1132
1133 add_com ("stepi", class_run, stepi_command,
1134 "Step one instruction exactly.\n\
1135Argument N means do this N times (or till program stops for another reason).");
1136 add_com_alias ("si", "stepi", class_alias, 0);
1137
1138 add_com ("nexti", class_run, nexti_command,
1139 "Step one instruction, but proceed through subroutine calls.\n\
1140Argument N means do this N times (or till program stops for another reason).");
1141 add_com_alias ("ni", "nexti", class_alias, 0);
1142
1143 add_com ("finish", class_run, finish_command,
1144 "Execute until selected stack frame returns.\n\
1145Upon return, the value returned is printed and put in the value history.");
1146
1147 add_com ("next", class_run, next_command,
1148 "Step program, proceeding through subroutine calls.\n\
1149Like the \"step\" command as long as subroutine calls do not happen;\n\
1150when they do, the call is treated as one instruction.\n\
1151Argument N means do this N times (or till program stops for another reason).");
1152 add_com_alias ("n", "next", class_run, 1);
1153
1154 add_com ("step", class_run, step_command,
1155 "Step program until it reaches a different source line.\n\
1156Argument N means do this N times (or till program stops for another reason).");
1157 add_com_alias ("s", "step", class_run, 1);
1158
1159 add_com ("until", class_run, until_command,
1160 "Execute until the program reaches a source line greater than the current\n\
1161or a specified line or address or function (same args as break command).\n\
1162Execution will also stop upon exit from the current stack frame.");
1163 add_com_alias ("u", "until", class_run, 1);
1164
1165 add_com ("jump", class_run, jump_command,
1166 "Continue program being debugged at specified line or address.\n\
1167Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1168for an address to start at.");
1169
1170 add_com ("cont", class_run, cont_command,
1171 "Continue program being debugged, after signal or breakpoint.\n\
1172If proceeding from breakpoint, a number N may be used as an argument:\n\
1173then the same breakpoint won't break until the Nth time it is reached.");
1174 add_com_alias ("c", "cont", class_run, 1);
1175
1176 add_com ("run", class_run, run_command,
1177 "Start debugged program. You may specify arguments to give it.\n\
1178Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
1179Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
1180With no arguments, uses arguments last specified (with \"run\" or \"set args\".\n\
1181To cancel previous arguments and run with no arguments,\n\
1182use \"set args\" without arguments.");
1183 add_com_alias ("r", "run", class_run, 1);
1184
1185 add_info ("registers", nofp_registers_info,
1186 "List of registers and their contents, for selected stack frame.\n\
1187Register name as argument means describe only that register.\n\
1188(Doesn't display floating point registers; use 'info all-registers'.)\n");
1189
1190 add_info ("all-registers", all_registers_info,
1191 "List of registers and their contents, for selected stack frame.\n\
1192Register name as argument means describe only that register.");
1193
1194 add_info ("program", program_info,
1195 "Execution status of the program.");
1196
1197 add_info ("float", float_info,
1198 "Print the status of the floating point unit\n");
1199
1200 inferior_args = savestring (" ", 1); /* By default, no args. */
1201 inferior_environ = make_environ ();
1202 init_environ (inferior_environ);
1203}
1204