1 /* Memory-access and commands for inferior process, for GDB.
2 Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
30 #include <sys/param.h>
32 extern char *sys_siglist[];
34 #define ERROR_NO_INFERIOR \
35 if (inferior_pid == 0) error ("The program is not being run.");
37 /* String containing arguments to give to the program,
38 with a space added at the front. Just a space means no args. */
40 static char *inferior_args;
42 /* File name for default use for standard in/out in the inferior. */
44 char *inferior_io_terminal;
46 /* Pid of our debugged inferior, or 0 if no inferior now. */
50 /* Last signal that the inferior received (why it stopped). */
54 /* Address at which inferior stopped. */
58 /* Stack frame when program stopped. */
60 FRAME_ADDR stop_frame_address;
62 /* Number of breakpoint it stopped at, or 0 if none. */
66 /* Nonzero if stopped due to a step command. */
70 /* Nonzero if stopped due to completion of a stack dummy routine. */
74 /* Nonzero if stopped due to a random (unexpected) signal in inferior
77 int stopped_by_random_signal;
79 /* Range to single step within.
80 If this is nonzero, respond to a single-step signal
81 by continuing to step if the pc is in this range. */
83 CORE_ADDR step_range_start; /* Inclusive */
84 CORE_ADDR step_range_end; /* Exclusive */
86 /* Stack frame address as of when stepping command was issued.
87 This is how we know when we step into a subroutine call,
88 and how to set the frame for the breakpoint used to step out. */
90 FRAME_ADDR step_frame_address;
92 /* 1 means step over all subroutine calls.
93 -1 means step over calls to undebuggable functions. */
97 /* If stepping, nonzero means step count is > 1
98 so don't print frame next time inferior stops
99 if it stops due to stepping. */
103 /* Environment to use for running inferior,
104 in format described in environ.h. */
106 struct environ *inferior_environ;
108 CORE_ADDR read_pc ();
109 struct command_line *get_breakpoint_commands ();
115 return inferior_pid != 0;
119 set_args_command (args)
122 free (inferior_args);
123 if (!args) args = "";
124 inferior_args = concat (" ", args, "");
128 tty_command (file, from_tty)
133 error_no_arg ("terminal name for running target process");
135 inferior_io_terminal = savestring (file, strlen (file));
139 run_command (args, from_tty)
143 extern char **environ;
149 extern char *sys_errlist[];
157 !query ("The program being debugged has been started already.\n\
158 Start it from the beginning? "))
159 error ("Program not restarted.");
163 exec_file = (char *) get_exec_file (1);
165 if (remote_debugging)
169 printf ("Starting program: %s\n", exec_file);
176 set_args_command (args);
180 printf ("Starting program: %s%s\n",
181 exec_file, inferior_args);
185 allargs = concat ("exec ", exec_file, inferior_args);
186 inferior_pid = create_inferior (allargs, environ_vector (inferior_environ));
189 clear_proceed_status ();
195 cont_command (proc_count_exp, from_tty)
196 char *proc_count_exp;
201 clear_proceed_status ();
203 /* If have argument, set proceed count of breakpoint we stopped at. */
205 if (stop_breakpoint > 0 && proc_count_exp)
207 set_ignore_count (stop_breakpoint,
208 parse_and_eval_address (proc_count_exp) - 1,
215 printf ("Continuing.\n");
220 /* Step until outside of current statement. */
221 static void step_1 ();
224 step_command (count_string)
226 step_1 (0, 0, count_string);
229 /* Likewise, but skip over subroutine calls as if single instructions. */
232 next_command (count_string)
234 step_1 (1, 0, count_string);
237 /* Likewise, but step only one instruction. */
240 stepi_command (count_string)
242 step_1 (0, 1, count_string);
246 nexti_command (count_string)
248 step_1 (1, 1, count_string);
252 step_1 (skip_subroutines, single_inst, count_string)
253 int skip_subroutines;
257 register int count = 1;
260 count = count_string ? parse_and_eval_address (count_string) : 1;
262 for (; count > 0; count--)
264 clear_proceed_status ();
266 step_frame_address = FRAME_FP (get_current_frame ());
270 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
271 if (step_range_end == 0)
275 misc = find_pc_misc_function (stop_pc);
277 printf ("Current function has no line number information.\n");
280 /* No info or after _etext ("Can't happen") */
281 if (misc == -1 || misc == misc_function_count - 1)
282 error ("No data available on pc function.");
284 printf ("Single stepping until function exit.\n");
287 step_range_start = misc_function_vector[misc].address;
288 step_range_end = misc_function_vector[misc + 1].address;
293 /* Say we are stepping, but stop after one insn whatever it does.
294 Don't step through subroutine calls even to undebuggable
296 step_range_start = step_range_end = 1;
297 if (!skip_subroutines)
301 if (skip_subroutines)
304 step_multi = (count > 1);
311 /* Continue program at specified address. */
314 jump_command (arg, from_tty)
318 register CORE_ADDR addr;
319 struct symtabs_and_lines sals;
320 struct symtab_and_line sal;
325 error_no_arg ("starting address");
327 sals = decode_line_spec_1 (arg, 1);
330 error ("Unreasonable jump request");
336 if (sal.symtab == 0 && sal.pc == 0)
337 error ("No source file has been specified.");
340 sal.pc = find_line_pc (sal.symtab, sal.line);
343 struct symbol *fn = get_frame_function (get_current_frame ());
344 struct symbol *sfn = find_pc_function (sal.pc);
345 if (fn != 0 && sfn != fn
346 && ! query ("Line %d is not in `%s'. Jump anyway? ",
347 sal.line, SYMBOL_NAME (fn)))
348 error ("Not confirmed.");
352 error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
356 clear_proceed_status ();
359 printf ("Continuing at 0x%x.\n", addr);
361 proceed (addr, 0, 0);
364 /* Continue program giving it specified signal. */
367 signal_command (signum_exp, from_tty)
373 dont_repeat (); /* Too dangerous. */
377 error_no_arg ("signal number");
379 signum = parse_and_eval_address (signum_exp);
381 clear_proceed_status ();
384 printf ("Continuing with signal %d.\n", signum);
386 proceed (stop_pc, signum, 0);
389 /* Execute a "stack dummy", a piece of code stored in the stack
390 by the debugger to be executed in the inferior.
392 To call: first, do PUSH_DUMMY_FRAME.
393 Then push the contents of the dummy. It should end with a breakpoint insn.
394 Then call here, passing address at which to start the dummy.
396 The contents of all registers are saved before the dummy frame is popped
397 and copied into the buffer BUFFER.
399 The dummy's frame is automatically popped whenever that break is hit.
400 If that is the first time the program stops, run_stack_dummy
401 returns to its caller with that frame already gone.
402 Otherwise, the caller never gets returned to. */
404 /* 4 => return instead of letting the stack dummy run. */
406 static int stack_dummy_testing = 0;
409 run_stack_dummy (addr, buffer)
411 REGISTER_TYPE *buffer;
413 /* Now proceed, having reached the desired place. */
414 clear_proceed_status ();
415 if (stack_dummy_testing & 4)
420 proceed (addr, 0, 0);
422 if (!stop_stack_dummy)
423 error ("Cannot continue previously requested operation.");
425 /* On return, the stack dummy has been popped already. */
427 bcopy (stop_registers, buffer, sizeof stop_registers);
430 /* Proceed until we reach the given line as argument or exit the
431 function. When called with no argument, proceed until we reach a
432 different source line with pc greater than our current one or exit
433 the function. We skip calls in both cases.
435 The effect of this command with an argument is identical to setting
436 a momentary breakpoint at the line specified and executing
439 Note that eventually this command should probably be changed so
440 that only source lines are printed out when we hit the breakpoint
441 we set. I'm going to postpone this until after a hopeful rewrite
442 of wait_for_inferior and the proceed status code. -- randy */
445 until_next_command (arg, from_tty)
452 struct symtab_and_line sal;
454 clear_proceed_status ();
456 frame = get_current_frame ();
458 /* Step until either exited from this function or greater
459 than the current line (if in symbolic section) or pc (if
463 func = find_pc_function (pc);
467 int misc_func = find_pc_misc_function (pc);
470 error ("Execution is not within a known function.");
472 step_range_start = misc_function_vector[misc_func].address;
477 sal = find_pc_line (pc, 0);
479 step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
480 step_range_end = sal.end;
484 step_frame_address = FRAME_FP (frame);
486 step_multi = 0; /* Only one call to proceed */
492 until_command (arg, from_tty)
496 if (!have_inferior_p ())
497 error ("The program is not being run.");
500 until_break_command (arg, from_tty);
502 until_next_command (arg, from_tty);
505 /* "finish": Set a temporary breakpoint at the place
506 the selected frame will return to, then continue. */
509 finish_command (arg, from_tty)
513 struct symtab_and_line sal;
514 register FRAME frame;
515 struct frame_info *fi;
516 register struct symbol *function;
518 if (!have_inferior_p ())
519 error ("The program is not being run.");
521 error ("The \"finish\" command does not take any arguments.");
523 frame = get_prev_frame (selected_frame);
525 error ("\"finish\" not meaningful in the outermost frame.");
527 clear_proceed_status ();
529 fi = get_frame_info (frame);
530 sal = find_pc_line (fi->pc, 0);
532 set_momentary_breakpoint (sal, frame);
534 /* Find the function we will return from. */
536 fi = get_frame_info (selected_frame);
537 function = find_pc_function (fi->pc);
541 printf ("Run till exit from ");
542 print_selected_frame ();
547 if (stop_breakpoint == -3 && function != 0)
549 struct type *value_type;
553 value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
555 fatal ("internal: finish_command: function has no target type");
557 if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
560 funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
562 val = value_being_returned (value_type, stop_registers,
563 using_struct_return (function,
567 printf ("Value returned is $%d = ", record_latest_value (val));
568 value_print (val, stdout, 0, Val_no_prettyprint);
576 if (inferior_pid == 0)
578 printf ("The program being debugged is not being run.\n");
582 printf ("Program being debugged is in process %d, stopped at 0x%x.\n",
583 inferior_pid, stop_pc);
585 printf ("It stopped after being stepped.\n");
586 else if (stop_breakpoint > 0)
587 printf ("It stopped at breakpoint %d.\n", stop_breakpoint);
588 else if (stop_signal)
589 printf ("It stopped with signal %d (%s).\n",
590 stop_signal, sys_siglist[stop_signal]);
592 printf ("\nType \"info stack\" or \"info reg\" for more information.\n");
596 environment_info (var)
601 register char *val = get_in_environ (inferior_environ, var);
603 printf ("%s = %s\n", var, val);
605 printf ("Environment variable \"%s\" not defined.\n", var);
609 register char **vector = environ_vector (inferior_environ);
611 printf ("%s\n", *vector++);
616 set_environment_command (arg)
619 register char *p, *val, *var;
623 error_no_arg ("environment variable and value");
625 /* Find seperation between variable name and value */
626 p = (char *) index (arg, '=');
627 val = (char *) index (arg, ' ');
629 if (p != 0 && val != 0)
631 /* We have both a space and an equals. If the space is before the
632 equals and the only thing between the two is more space, use
638 /* Take the smaller of the two. If there was space before the
639 "=", they will be the same right now. */
640 p = arg + min (p - arg, val - arg);
642 else if (val != 0 && p == 0)
646 error_no_arg ("environment variable to set");
648 if (p == 0 || p[1] == 0)
652 p = arg + strlen (arg); /* So that savestring below will work */
656 /* Not setting variable value to null */
658 while (*val == ' ' || *val == '\t')
662 while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
664 var = savestring (arg, p - arg);
667 printf ("Setting environment variable \"%s\" to null value.\n", var);
668 set_in_environ (inferior_environ, var, "");
671 set_in_environ (inferior_environ, var, val);
676 unset_environment_command (var, from_tty)
681 /* If there is no argument, delete all environment variables.
682 Ask for confirmation if reading from the terminal. */
683 if (!from_tty || query ("Delete all environment variables? "))
685 free_environ (inferior_environ);
686 inferior_environ = make_environ ();
689 unset_in_environ (inferior_environ, var);
692 /* Read an integer from debugged memory, given address and number of bytes. */
695 read_memory_integer (memaddr, len)
705 extern char *sys_errlist[];
707 if (len == sizeof (char))
709 result_err = read_memory (memaddr, &cbuf, len);
711 error ("Error reading memory address 0x%x: %s (%d).",
712 memaddr, (result_err < sys_nerr ?
713 sys_errlist[result_err] :
714 "uknown error"), result_err);
717 if (len == sizeof (short))
719 result_err = read_memory (memaddr, &sbuf, len);
721 error ("Error reading memory address 0x%x: %s (%d).",
722 memaddr, (result_err < sys_nerr ?
723 sys_errlist[result_err] :
724 "uknown error"), result_err);
727 if (len == sizeof (int))
729 result_err = read_memory (memaddr, &ibuf, len);
731 error ("Error reading memory address 0x%x: %s (%d).",
732 memaddr, (result_err < sys_nerr ?
733 sys_errlist[result_err] :
734 "uknown error"), result_err);
737 if (len == sizeof (lbuf))
739 result_err = read_memory (memaddr, &lbuf, len);
741 error ("Error reading memory address 0x%x: %s (%d).",
742 memaddr, (result_err < sys_nerr ?
743 sys_errlist[result_err] :
744 "uknown error"), result_err);
747 error ("Cannot handle integers of %d bytes.", len);
753 return (CORE_ADDR) read_register (PC_REGNUM);
760 write_register (PC_REGNUM, (long) val);
762 write_register (NPC_REGNUM, (long) val+4);
766 char *reg_names[] = REGISTER_NAMES;
769 registers_info (addr_exp)
775 if (!have_inferior_p () && !have_core_file_p ())
776 error ("No inferior or core file");
780 if (*addr_exp >= '0' && *addr_exp <= '9')
781 regnum = atoi (addr_exp);
784 register char *p = addr_exp;
787 for (regnum = 0; regnum < NUM_REGS; regnum++)
788 if (!strcmp (p, reg_names[regnum]))
790 if (regnum == NUM_REGS)
791 error ("%s: invalid register name.", addr_exp);
796 "Register Contents (relative to selected stack frame)\n\n");
798 for (i = 0; i < NUM_REGS; i++)
800 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
801 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
804 if (addr_exp != 0 && i != regnum)
807 /* Get the data in raw format, then convert also to virtual format. */
808 read_relative_register_raw_bytes (i, raw_buffer);
809 REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
811 fputs_filtered (reg_names[i], stdout);
812 print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
814 /* If virtual format is floating, print it that way. */
815 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
816 && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
817 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
818 stdout, 0, 1, 0, Val_pretty_default);
819 /* Else if virtual format is too long for printf,
820 print in hex a byte at a time. */
821 else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
824 printf_filtered ("0x");
825 for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
826 printf_filtered ("%02x", virtual_buffer[j]);
828 /* Else print as integer in hex and in decimal. */
833 bcopy (virtual_buffer, &val, sizeof (long));
835 printf_filtered ("0");
837 printf_filtered ("0x%08x %d", val, val);
840 /* If register has different raw and virtual formats,
841 print the raw format in hex now. */
843 if (REGISTER_CONVERTIBLE (i))
847 printf_filtered (" (raw 0x");
848 for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
849 printf_filtered ("%02x", raw_buffer[j]);
850 printf_filtered (")");
852 printf_filtered ("\n");
857 #define PROCESS_ATTACH_ALLOWED 1
859 #define PROCESS_ATTACH_ALLOWED 0
863 * Should save/restore the tty state since it might be that the
864 * program to be debugged was started on this tty and it wants
865 * the tty in some state other than what we want. If it's running
866 * on another terminal or without a terminal, then saving and
867 * restoring the tty state is a harmless no-op.
868 * This only needs to be done if we are attaching to a process.
873 * takes a program started up outside of gdb and ``attaches'' to it.
874 * This stops it cold in its tracks and allows us to start tracing it.
875 * For this to work, we must be able to send the process a
876 * signal and we must have the same effective uid as the program.
879 attach_command (args, from_tty)
890 error_no_arg ("process-id or device file to attach");
892 while (*args == ' ' || *args == '\t') args++;
897 #ifndef ATTACH_DETACH
898 error ("Can't attach to a process on this machine.");
905 if (query ("A program is being debugged already. Kill it? "))
908 error ("Inferior not killed.");
911 exec_file = (char *) get_exec_file (1);
916 printf ("Attaching remote machine\n");
918 printf ("Attaching program: %s pid %d\n",
927 remote_open (args, from_tty);
932 attach_program (pid);
938 * takes a program previously attached to and detaches it.
939 * The program resumes execution and will no longer stop
940 * on signals, etc. We better not have left any breakpoints
941 * in the program or it'll die when it hits one. For this
942 * to work, it may be necessary for the process to have been
943 * previously attached. It *might* work if the program was
944 * started via the normal ptrace (PTRACE_TRACEME).
948 detach_command (args, from_tty)
959 char *exec_file = (char *)get_exec_file (0);
962 printf ("Detaching program: %s pid %d\n",
963 exec_file, inferior_pid);
967 signal = atoi (args);
975 if (!remote_debugging)
976 error ("Not currently attached to subsidiary or remote process.");
979 error ("Argument given to \"detach\" when remotely debugging.");
981 remote_close (from_tty);
987 float_info (addr_exp)
993 printf ("No floating point info available for this processor.\n");
997 extern struct cmd_list_element *setlist, *deletelist;
1000 _initialize_infcmd ()
1002 add_com ("tty", class_run, tty_command,
1003 "Set terminal for future runs of program being debugged.");
1005 add_cmd ("args", class_run, set_args_command,
1006 "Specify arguments to give program being debugged when it is started.\n\
1007 Follow this command with any number of args, to be passed to the program.",
1010 add_info ("environment", environment_info,
1011 "The environment to give the program, or one variable's value.\n\
1012 With an argument VAR, prints the value of environment variable VAR to\n\
1013 give the program being debugged. With no arguments, prints the entire\n\
1014 environment to be given to the program.");
1016 add_cmd ("environment", class_run, unset_environment_command,
1017 "Cancel environment variable VAR for the program.\n\
1018 This does not affect the program until the next \"run\" command.",
1021 add_cmd ("environment", class_run, set_environment_command,
1022 "Set environment variable value to give the program.\n\
1023 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
1024 VALUES of environment variables are uninterpreted strings.\n\
1025 This does not affect the program until the next \"run\" command.",
1028 #ifdef ATTACH_DETACH
1029 add_com ("attach", class_run, attach_command,
1030 "Attach to a process that was started up outside of GDB.\n\
1031 This command may take as argument a process id or a device file.\n\
1032 For a process id, you must have permission to send the process a signal,\n\
1033 and it must have the same effective uid as the debugger.\n\
1034 For a device file, the file must be a connection to a remote debug server.\n\n\
1035 Before using \"attach\", you must use the \"exec-file\" command\n\
1036 to specify the program running in the process,\n\
1037 and the \"symbol-file\" command to load its symbol table.");
1039 add_com ("attach", class_run, attach_command,
1040 "Attach to a process that was started up outside of GDB.\n\
1041 This commands takes as an argument the name of a device file.\n\
1042 This file must be a connection to a remote debug server.\n\n\
1043 Before using \"attach\", you must use the \"exec-file\" command\n\
1044 to specify the program running in the process,\n\
1045 and the \"symbol-file\" command to load its symbol table.");
1047 add_com ("detach", class_run, detach_command,
1048 "Detach the process previously attached.\n\
1049 The process is no longer traced and continues its execution.");
1051 add_com ("signal", class_run, signal_command,
1052 "Continue program giving it signal number SIGNUMBER.");
1054 add_com ("stepi", class_run, stepi_command,
1055 "Step one instruction exactly.\n\
1056 Argument N means do this N times (or till program stops for another reason).");
1057 add_com_alias ("si", "stepi", class_alias, 0);
1059 add_com ("nexti", class_run, nexti_command,
1060 "Step one instruction, but proceed through subroutine calls.\n\
1061 Argument N means do this N times (or till program stops for another reason).");
1062 add_com_alias ("ni", "nexti", class_alias, 0);
1064 add_com ("finish", class_run, finish_command,
1065 "Execute until selected stack frame returns.\n\
1066 Upon return, the value returned is printed and put in the value history.");
1068 add_com ("next", class_run, next_command,
1069 "Step program, proceeding through subroutine calls.\n\
1070 Like the \"step\" command as long as subroutine calls do not happen;\n\
1071 when they do, the call is treated as one instruction.\n\
1072 Argument N means do this N times (or till program stops for another reason).");
1073 add_com_alias ("n", "next", class_run, 1);
1075 add_com ("step", class_run, step_command,
1076 "Step program until it reaches a different source line.\n\
1077 Argument N means do this N times (or till program stops for another reason).");
1078 add_com_alias ("s", "step", class_run, 1);
1080 add_com ("until", class_run, until_command,
1081 "Execute until the program reaches a source line greater than the current\n\
1082 or a specified line or address or function (same args as break command).\n\
1083 Execution will also stop upon exit from the current stack frame.");
1084 add_com_alias ("u", "until", class_run, 1);
1086 add_com ("jump", class_run, jump_command,
1087 "Continue program being debugged at specified line or address.\n\
1088 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1089 for an address to start at.");
1091 add_com ("cont", class_run, cont_command,
1092 "Continue program being debugged, after signal or breakpoint.\n\
1093 If proceeding from breakpoint, a number N may be used as an argument:\n\
1094 then the same breakpoint won't break until the Nth time it is reached.");
1095 add_com_alias ("c", "cont", class_run, 1);
1097 add_com ("run", class_run, run_command,
1098 "Start debugged program. You may specify arguments to give it.\n\
1099 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
1100 Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
1101 With no arguments, uses arguments last specified (with \"run\" or \"set args\".\n\
1102 To cancel previous arguments and run with no arguments,\n\
1103 use \"set args\" without arguments.");
1104 add_com_alias ("r", "run", class_run, 1);
1106 add_info ("registers", registers_info,
1107 "List of registers and their contents, for selected stack frame.\n\
1108 Register name as argument means describe only that register.");
1110 add_info ("program", program_info,
1111 "Execution status of the program.");
1113 add_info ("float", float_info,
1114 "Print the status of the floating point unit\n");
1116 inferior_args = savestring (" ", 1); /* By default, no args. */
1117 inferior_environ = make_environ ();
1118 init_environ (inferior_environ);