9 date 88.01.26.05.06.19; author gnu; state Exp;
14 date 88.01.26.01.19.05; author gnu; state Exp;
20 @Original from RMS's wheaties devl sources
26 @Add local sys_siglist for a/ux because they don't provide one, sigh.
29 @/* Memory-access and commands for inferior process, for GDB.
30 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
32 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
33 WARRANTY. No author or distributor accepts responsibility to anyone
34 for the consequences of using it or for whether it serves any
35 particular purpose or works at all, unless he says so in writing.
36 Refer to the GDB General Public License for full details.
38 Everyone is granted permission to copy, modify and redistribute GDB,
39 but only under the conditions described in the GDB General Public
40 License. A copy of this license is supposed to have been given to you
41 along with GDB so you can know your rights and responsibilities. It
42 should be in a file named COPYING. Among other things, the copyright
43 notice and this notice must be preserved on all copies.
45 In other words, go ahead and share GDB, but don't try to stop
46 anyone else from sharing it farther. Help stamp out software hoarding!
50 #include "initialize.h"
60 #include <sys/param.h>
63 /* Warning! This table is positional and highly dependent on the local
64 system. Check it closely against <sys/signal.h> when porting. */
65 char *sys_siglist[] = {
70 "Invalid instruction",
71 "Trace/breakpoint trap",
74 "Floating point exception",
87 "Stopped (tty input)",
88 "Stopped (tty output)",
90 "Cputime limit exceeded",
91 "File size limit exceeded",
92 "Virtual timer expired",
93 "Profiling timer expired",
96 "Urgent I/O condition",
100 /* More portable systems do it for you */
101 extern char *sys_siglist[];
104 #define ERROR_NO_INFERIOR \
105 if (inferior_pid == 0) error ("The program is not being run.");
107 /* String containing arguments to give to the program,
108 with a space added at the front. Just a space means no args. */
110 static char *inferior_args;
112 /* File name for default use for standard in/out in the inferior. */
114 char *inferior_io_terminal;
116 /* Pid of our debugged inferior, or 0 if no inferior now. */
120 /* Last signal that the inferior received (why it stopped). */
124 /* Address at which inferior stopped. */
128 /* Stack frame when program stopped. */
132 /* Number of breakpoint it stopped at, or 0 if none. */
136 /* Nonzero if stopped due to a step command. */
140 /* Nonzero if stopped due to completion of a stack dummy routine. */
142 int stop_stack_dummy;
144 /* Range to single step within.
145 If this is nonzero, respond to a single-step signal
146 by continuing to step if the pc is in this range. */
148 CORE_ADDR step_range_start; /* Inclusive */
149 CORE_ADDR step_range_end; /* Exclusive */
151 /* Stack frame address as of when stepping command was issued.
152 This is how we know when we step into a subroutine call,
153 and how to set the frame for the breakpoint used to step out. */
155 CORE_ADDR step_frame;
157 /* 1 means step over all subroutine calls.
158 -1 means step over calls to undebuggable functions. */
162 /* If stepping, nonzero means step count is > 1
163 so don't print frame next time inferior stops
164 if it stops due to stepping. */
168 /* Environment to use for running inferior,
169 in format described in environ.h. */
171 struct environ *inferior_environ;
173 CORE_ADDR read_pc ();
174 struct command_line *get_breakpoint_commands ();
181 return inferior_pid != 0;
185 set_args_command (args)
188 free (inferior_args);
189 if (!args) args = "";
190 inferior_args = concat (" ", args, "");
198 error_no_arg ("terminal name for running target process");
200 inferior_io_terminal = savestring (file, strlen (file));
204 run_command (args, from_tty)
208 extern char **environ;
214 extern char *sys_errlist[];
221 if (query ("The program being debugged has been started already.\n\
222 Start it from the beginning? "))
225 error ("Program already started.");
229 set_args_command (args);
231 exec_file = (char *) get_exec_file ();
234 printf ("Starting program: %s%s\n",
235 exec_file, inferior_args);
239 allargs = concat ("exec ", exec_file, inferior_args);
240 inferior_pid = create_inferior (allargs, environ_vector (inferior_environ));
242 clear_proceed_status ();
248 cont_command (proc_count_exp, from_tty)
249 char *proc_count_exp;
254 clear_proceed_status ();
256 /* If have argument, set proceed count of breakpoint we stopped at. */
258 if (stop_breakpoint && proc_count_exp)
260 set_ignore_count (stop_breakpoint,
261 parse_and_eval_address (proc_count_exp) - 1,
268 printf ("Continuing.\n");
273 /* Step until outside of current statement. */
274 static void step_1 ();
277 step_command (count_string)
279 step_1 (0, 0, count_string);
282 /* Likewise, but skip over subroutine calls as if single instructions. */
285 next_command (count_string)
287 step_1 (1, 0, count_string);
290 /* Likewise, but step only one instruction. */
293 stepi_command (count_string)
295 step_1 (0, 1, count_string);
299 nexti_command (count_string)
301 step_1 (1, 1, count_string);
305 step_1 (skip_subroutines, single_inst, count_string)
306 int skip_subroutines;
310 register int count = 1;
313 count = count_string ? parse_and_eval_address (count_string) : 1;
315 for (; count > 0; count--)
317 clear_proceed_status ();
319 step_frame = get_current_frame ();
323 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
324 if (step_range_end == 0)
327 error ("Current function has no line number information.");
332 /* Say we are stepping, but stop after one insn whatever it does.
333 Don't step through subroutine calls even to undebuggable functions. */
334 step_range_start = step_range_end = 1;
335 if (!skip_subroutines)
339 if (skip_subroutines)
342 step_multi = (count > 1);
349 /* Continue program at specified address. */
352 jump_command (arg, from_tty)
356 register CORE_ADDR addr;
357 struct symtab_and_line sal;
362 error_no_arg ("starting address");
364 sal = decode_line_spec (arg, 1);
366 if (sal.symtab == 0 && sal.pc == 0)
367 error ("No source file has been specified.");
370 sal.pc = find_line_pc (sal.symtab, sal.line);
373 struct symbol *fn = get_frame_function (get_current_frame ());
374 struct symbol *sfn = find_pc_function (sal.pc);
375 if (fn != 0 && sfn != fn
376 && ! query ("That is not in function %s. Continue there? ",
377 sal.line, SYMBOL_NAME (fn)))
378 error ("Not confirmed.");
382 error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
386 clear_proceed_status ();
389 printf ("Continuing at 0x%x.\n", addr);
391 proceed (addr, 0, 0);
394 /* Continue program giving it specified signal. */
397 signal_command (signum_exp, from_tty)
403 dont_repeat (); /* Too dangerous. */
407 error_no_arg ("signal number");
409 signum = parse_and_eval_address (signum_exp);
411 clear_proceed_status ();
414 printf ("Continuing with signal %d.\n", signum);
416 proceed (stop_pc, signum, 0);
419 /* Execute a "stack dummy", a piece of code stored in the stack
420 by the debugger to be executed in the inferior.
422 To call: first, do PUSH_DUMMY_FRAME.
423 Then push the contents of the dummy. It should end with a breakpoint insn.
424 Then call here, passing address at which to start the dummy.
426 The contents of all registers are saved before the dummy frame is popped
427 and copied into the buffer BUFFER.
429 The dummy's frame is automatically popped whenever that break is hit.
430 If that is the first time the program stops, run_stack_dummy
431 returns to its caller with that frame already gone.
432 Otherwise, the caller never gets returned to. */
434 /* 4 => return instead of letting the stack dummy run. */
436 static int stack_dummy_testing = 0;
439 run_stack_dummy (addr, buffer)
441 REGISTER_TYPE *buffer;
443 int saved_pc_changed = pc_changed;
444 int saved_stop_signal = stop_signal;
445 int saved_stop_pc = stop_pc;
446 int saved_stop_frame = stop_frame;
447 int saved_stop_breakpoint = stop_breakpoint;
448 int saved_stop_step = stop_step;
449 int saved_stop_stack_dummy = stop_stack_dummy;
450 FRAME saved_selected_frame;
451 int saved_selected_level;
452 struct command_line *saved_breakpoint_commands
453 = get_breakpoint_commands ();
455 record_selected_frame (&saved_selected_frame, &saved_selected_level);
457 /* Now proceed, having reached the desired place. */
458 clear_proceed_status ();
459 if (stack_dummy_testing & 4)
464 proceed (addr, 0, 0);
466 if (!stop_stack_dummy)
467 error ("Cannot continue previously requested operation.");
469 set_breakpoint_commands (saved_breakpoint_commands);
470 select_frame (saved_selected_frame, saved_selected_level);
471 stop_signal = saved_stop_signal;
472 stop_pc = saved_stop_pc;
473 stop_frame = saved_stop_frame;
474 stop_breakpoint = saved_stop_breakpoint;
475 stop_step = saved_stop_step;
476 stop_stack_dummy = saved_stop_stack_dummy;
477 pc_changed = saved_pc_changed;
479 /* On return, the stack dummy has been popped already. */
481 bcopy (stop_registers, buffer, sizeof stop_registers);
484 /* "finish": Set a temporary breakpoint at the place
485 the selected frame will return to, then continue. */
488 finish_command (arg, from_tty)
492 struct symtab_and_line sal;
493 register FRAME frame;
494 struct frame_info fi;
496 register struct symbol *function;
498 if (!have_inferior_p ())
499 error ("The program is not being run.");
501 error ("The \"finish\" command does not take any arguments.");
503 frame = get_prev_frame (selected_frame);
505 error ("\"finish\" not meaningful in the outermost frame.");
507 clear_proceed_status ();
509 fi = get_frame_info (frame);
510 sal = find_pc_line (fi.pc, 0);
512 set_momentary_breakpoint (sal, frame);
514 /* Find the function we will return from. */
516 fi = get_frame_info (fi.next_frame);
517 function = find_pc_function (fi.pc);
521 printf ("Run till exit from ");
522 print_selected_frame ();
527 if (stop_breakpoint == -3 && function != 0)
529 struct type *value_type;
532 if (TYPE_CODE (SYMBOL_TYPE (function)) != TYPE_CODE_VOID)
533 value_type = SYMBOL_TYPE (function);
537 val = value_being_returned (value_type, stop_registers);
538 printf ("Value returned is $%d = ", record_latest_value (val));
539 value_print (val, stdout);
547 if (inferior_pid == 0)
549 printf ("The program being debugged is not being run.\n");
553 printf ("Program being debugged is in process %d, stopped at 0x%x.\n",
554 inferior_pid, stop_pc);
556 printf ("It stopped after being stepped.\n");
557 else if (stop_breakpoint)
558 printf ("It stopped at breakpoint %d.\n", stop_breakpoint);
559 else if (stop_signal)
560 printf ("It stopped with signal %d (%s).\n",
561 stop_signal, sys_siglist[stop_signal]);
563 printf ("\nType \"info stack\" or \"info reg\" for more information.\n");
567 environment_info (var)
572 register char *val = get_in_environ (inferior_environ, var);
574 printf ("%s = %s\n", var, val);
576 printf ("Environment variable \"%s\" not defined.\n", var);
580 register char **vector = environ_vector (inferior_environ);
582 printf ("%s\n", *vector++);
587 set_environment_command (arg)
590 register char *p, *val, *var;
593 error_no_arg ("environment variable and value");
595 p = (char *) index (arg, '=');
596 val = (char *) index (arg, ' ');
597 if (p != 0 && val != 0)
598 p = arg + min (p - arg, val - arg);
599 else if (val != 0 && p == 0)
603 error ("Space or \"=\" must separate variable name and its value");
605 error_no_arg ("value for the variable");
607 error_no_arg ("environment variable to set");
610 while (*val == ' ' || *val == '\t') val++;
611 while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
613 var = savestring (arg, p - arg);
614 set_in_environ (inferior_environ, var, val);
619 unset_environment_command (var)
623 error_no_arg ("environment variable");
625 unset_in_environ (inferior_environ, var);
628 /* Read an integer from debugged memory, given address and number of bytes. */
630 read_memory_integer (memaddr, len)
639 if (len == sizeof (char))
641 read_memory (memaddr, &cbuf, len);
644 if (len == sizeof (short))
646 read_memory (memaddr, &sbuf, len);
649 if (len == sizeof (int))
651 read_memory (memaddr, &ibuf, len);
654 if (len == sizeof (lbuf))
656 read_memory (memaddr, &lbuf, len);
659 error ("Cannot handle integers of %d bytes.", len);
665 return (CORE_ADDR) read_register (PC_REGNUM);
671 write_register (PC_REGNUM, (long) val);
674 char *reg_names[] = REGISTER_NAMES;
677 registers_info (addr_exp)
685 if (*addr_exp >= '0' && *addr_exp <= '9')
686 regnum = atoi (addr_exp);
689 register char *p = addr_exp;
692 for (regnum = 0; regnum < NUM_REGS; regnum++)
693 if (!strcmp (p, reg_names[regnum]))
695 if (regnum == NUM_REGS)
696 error ("%s: invalid register name.", addr_exp);
700 printf ("Reg\tContents\n\n");
702 for (i = 0; i < NUM_REGS; i++)
704 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
705 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
708 if (addr_exp != 0 && i != regnum)
711 /* On machines with lots of registers, pause every 16 lines
712 so user can read the output. */
713 if (addr_exp == 0 && i > 0 && i % 16 == 0)
715 printf ("--Type Return to print more--");
720 /* Get the data in raw format, then convert also to virtual format. */
721 read_relative_register_raw_bytes (i, raw_buffer);
722 REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
724 printf ("%s\t", reg_names[i]);
726 /* If virtual format is floating, print it that way. */
727 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
728 && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
729 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, stdout);
730 /* Else if virtual format is too long for printf,
731 print in hex a byte at a time. */
732 else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
736 for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
737 printf ("%02x", virtual_buffer[j]);
739 /* Else print as integer in hex and in decimal. */
744 bcopy (virtual_buffer, &val, sizeof (long));
748 printf ("0x%08x %d", val, val);
751 /* If register has different raw and virtual formats,
752 print the raw format in hex now. */
754 if (REGISTER_CONVERTIBLE (i))
759 for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
760 printf ("%02x", raw_buffer[j]);
766 printf ("Contents are relative to selected stack frame.\n");
772 * Should save/restore the tty state since it might be that the
773 * program to be debugged was started on this tty and it wants
774 * the tty in some state other than what we want. If it's running
775 * on another terminal or without a terminal, then saving and
776 * restoring the tty state is a harmless no-op.
781 * takes a program started up outside of gdb and ``attaches'' to it.
782 * This stops it cold in it's tracks and allows us to start tracing
783 * it. For this to work, we must be able to send the process a
784 * signal and we must have the same effective uid as the program.
787 attach_command (args, from_tty)
797 error_no_arg ("process-id to attach");
803 if (query ("A program is being debugged already. Kill it? "))
806 error ("Inferior not killed.");
809 exec_file = (char *) get_exec_file ();
813 printf ("Attaching program: %s pid %d\n",
818 attach_program (pid);
823 * takes a program previously attached to and detaches it.
824 * The program resumes execution and will no longer stop
825 * on signals, etc. We better not have left any breakpoints
826 * in the program or it'll die when it hits one. For this
827 * to work, it may be necessary for the process to have been
828 * previously attached. It *might* work if the program was
829 * started via the normal ptrace (PTRACE_TRACEME).
833 detach_command (args, from_tty)
837 char *exec_file = (char *)get_exec_file ();
841 error ("Not currently tracing a program\n");
844 printf ("Detaching program: %s pid %d\n",
845 exec_file, inferior_pid);
849 signal = atoi (args);
854 #endif /* ATTACH_DETACH */
859 add_com ("tty", class_run, tty_command,
860 "Set terminal for future runs of program being debugged.");
862 add_com ("set-args", class_run, set_args_command,
863 "Specify arguments to give program being debugged when it is started.\n\
864 Follow this command with any number of args, to be passed to the program.");
866 add_info ("environment", environment_info,
867 "The environment to give the program, or one variable's value.\n\
868 With an argument VAR, prints the value of environment variable VAR to\n\
869 give the program being debugged. With no arguments, prints the entire\n\
870 environment to be given to the program.");
872 add_com ("unset-environment", class_run, unset_environment_command,
873 "Cancel environment variable VAR for the program.\n\
874 This does not affect the program until the next \"run\" command.");
875 add_com ("set-environment", class_run, set_environment_command,
876 "Set environment variable value to give the program.\n\
877 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
878 VALUES of environment variables are uninterpreted strings.\n\
879 This does not affect the program until the next \"run\" command.");
882 add_com ("attach", class_run, attach_command,
883 "Attach to a process that was started up outside of GDB.\n\
884 To do this, you must have permission to send the process a signal.\n\
885 And it must have the same effective uid as the debugger.\n\n\
886 Before using \"attach\", you must use the \"exec-file\" command\n\
887 to specify the program running in the process,\n\
888 and the \"symbol-file\" command to load its symbol table.");
889 add_com ("detach", class_run, detach_command,
890 "Detach the process previously attached.\n\
891 The process is no longer traced and continues its execution.");
892 #endif /* ATTACH_DETACH */
894 add_com ("signal", class_run, signal_command,
895 "Continue program giving it signal number SIGNUMBER.");
897 add_com ("stepi", class_run, stepi_command,
898 "Step one instruction exactly.\n\
899 Argument N means do this N times (or till program stops for another reason).");
900 add_com_alias ("si", "stepi", class_alias, 0);
902 add_com ("nexti", class_run, nexti_command,
903 "Step one instruction, but proceed through subroutine calls.\n\
904 Argument N means do this N times (or till program stops for another reason).");
905 add_com_alias ("ni", "nexti", class_alias, 0);
907 add_com ("finish", class_run, finish_command,
908 "Execute until selected stack frame returns.\n\
909 Upon return, the value returned is printed and put in the value history.");
911 add_com ("next", class_run, next_command,
912 "Step program, proceeding through subroutine calls.\n\
913 Like the \"step\" command as long as subroutine calls do not happen;\n\
914 when they do, the call is treated as one instruction.\n\
915 Argument N means do this N times (or till program stops for another reason).");
916 add_com_alias ("n", "next", class_run, 1);
918 add_com ("step", class_run, step_command,
919 "Step program until it reaches a different source line.\n\
920 Argument N means do this N times (or till program stops for another reason).");
921 add_com_alias ("s", "step", class_run, 1);
923 add_com ("jump", class_run, jump_command,
924 "Continue program being debugged at specified line or address.\n\
925 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
926 for an address to start at.");
928 add_com ("cont", class_run, cont_command,
929 "Continue program being debugged, after signal or breakpoint.\n\
930 If proceeding from breakpoint, a number N may be used as an argument:\n\
931 then the same breakpoint won't break until the Nth time it is reached.");
932 add_com_alias ("c", "cont", class_run, 1);
934 add_com ("run", class_run, run_command,
935 "Start debugged program. You may specify arguments to give it.\n\
936 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
937 Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
938 With no arguments, uses arguments last specified (with \"run\" or \"set-args\".\n\
939 To cancel previous arguments and run with no arguments,\n\
940 use \"set-args\" without arguments.");
941 add_com_alias ("r", "run", class_run, 1);
943 add_info ("registers", registers_info,
944 "List of registers and their contents, for selected stack frame.\n\
945 Register name as argument means describe only that register.");
947 add_info ("program", program_info,
948 "Execution status of the program.");
950 inferior_args = savestring (" ", 1); /* By default, no args. */
951 inferior_environ = make_environ ();
952 init_environ (inferior_environ);