1 /* Top level for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 #include "breakpoint.h"
34 /* readline include files */
38 /* readline defines this. */
42 #include <sys/types.h>
49 #include <sys/param.h>
53 #ifdef SET_STACK_LIMIT_HUGE
55 #include <sys/resource.h>
57 int original_stack_limit;
61 /* If this definition isn't overridden by the header files, assume
62 that isatty and fileno exist on this system. */
64 #define ISATTY(FP) (isatty (fileno (FP)))
67 /* Initialization file name for gdb. This is overridden in some configs. */
69 #ifndef GDBINIT_FILENAME
70 #define GDBINIT_FILENAME ".gdbinit"
72 char gdbinit[] = GDBINIT_FILENAME;
74 #define ALL_CLEANUPS ((struct cleanup *)0)
76 /* Version number of GDB, as a string. */
80 /* Message to be printed before the error message, when an error occurs. */
82 extern char *error_pre_print;
84 extern char lang_frame_mismatch_warn[]; /* language.c */
86 /* Flag for whether we want all the "from_tty" gubbish printed. */
88 int caution = 1; /* Default is yes, sigh. */
91 * Define all cmd_list_element's
94 /* Chain containing all defined commands. */
96 struct cmd_list_element *cmdlist;
98 /* Chain containing all defined info subcommands. */
100 struct cmd_list_element *infolist;
102 /* Chain containing all defined enable subcommands. */
104 struct cmd_list_element *enablelist;
106 /* Chain containing all defined disable subcommands. */
108 struct cmd_list_element *disablelist;
110 /* Chain containing all defined delete subcommands. */
112 struct cmd_list_element *deletelist;
114 /* Chain containing all defined "enable breakpoint" subcommands. */
116 struct cmd_list_element *enablebreaklist;
118 /* Chain containing all defined set subcommands */
120 struct cmd_list_element *setlist;
122 /* Chain containing all defined show subcommands. */
123 struct cmd_list_element *showlist;
125 /* Chain containing all defined \"set history\". */
127 struct cmd_list_element *sethistlist;
129 /* Chain containing all defined \"show history\". */
130 struct cmd_list_element *showhistlist;
132 /* Chain containing all defined \"unset history\". */
134 struct cmd_list_element *unsethistlist;
136 /* stdio stream that command input is being read from. */
140 /* Current working directory. */
142 char *current_directory;
144 /* The directory name is actually stored here (usually). */
145 static char dirbuf[MAXPATHLEN];
147 /* Function to call before reading a command, if nonzero.
148 The function receives two args: an input stream,
149 and a prompt string. */
151 void (*window_hook) ();
153 extern int frame_file_full_name;
157 /* The external commands we call... */
158 extern void init_source_path ();
159 extern void directory_command ();
160 extern void exec_file_command ();
161 extern void symbol_file_command ();
162 extern void core_file_command ();
163 extern void tty_command ();
165 extern void help_list ();
166 extern void initialize_all_files ();
167 extern void init_malloc ();
169 /* Forward declarations for this file */
170 void free_command_lines ();
171 char *gdb_readline ();
172 char *command_line_input ();
173 static void initialize_main ();
174 static void initialize_cmd_lists ();
175 static void init_signals ();
176 static void quit_command ();
177 void command_loop ();
178 static void source_command ();
179 static void print_gdb_version ();
180 static void print_gnu_advertisement ();
181 static void float_handler ();
182 static void cd_command ();
183 static void read_command_file ();
187 /* gdb prints this when reading a command interactively */
190 /* Buffer used for reading command lines, and the size
191 allocated for it so far. */
196 /* Baud rate specified for talking to serial target systems. Default
197 is left as a zero pointer, so targets can choose their own defaults. */
201 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
205 #define STOP_SIGNAL SIGTSTP
209 /* Some System V have job control but not sigsetmask(). */
210 #if !defined (HAVE_SIGSETMASK)
211 #define HAVE_SIGSETMASK !defined (USG)
215 #define sigsetmask(n)
218 /* This is how `error' returns to command level. */
220 jmp_buf to_top_level;
223 return_to_top_level ()
227 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
228 clear_momentary_breakpoints ();
229 disable_current_display ();
230 do_cleanups (ALL_CLEANUPS);
231 longjmp (to_top_level, 1);
234 /* Call FUNC with arg ARGS, catching any errors.
235 If there is no error, return the value returned by FUNC.
236 If there is an error, print ERRSTRING, print the specific error message,
240 catch_errors (func, args, errstring)
247 struct cleanup *saved_cleanup_chain;
248 char *saved_error_pre_print;
250 saved_cleanup_chain = save_cleanups ();
251 saved_error_pre_print = error_pre_print;
253 bcopy (to_top_level, saved, sizeof (jmp_buf));
254 error_pre_print = errstring;
256 if (setjmp (to_top_level) == 0)
257 val = (*func) (args);
261 restore_cleanups (saved_cleanup_chain);
263 error_pre_print = saved_error_pre_print;
264 bcopy (saved, to_top_level, sizeof (jmp_buf));
268 /* Handler for SIGHUP. */
273 kill_inferior_fast ();
274 signal (SIGHUP, SIG_DFL);
275 kill (getpid (), SIGHUP);
278 /* Clean up on error during a "source" command (or execution of a
279 user-defined command). */
282 source_cleanup (stream)
285 /* Restore the previous input stream. */
289 /* Read commands from STREAM. */
291 read_command_file (stream)
294 struct cleanup *cleanups;
296 cleanups = make_cleanup (source_cleanup, instream);
299 do_cleanups (cleanups);
308 static int inhibit_gdbinit = 0;
309 static int quiet = 0;
310 static int batch = 0;
312 /* Pointers to various arguments from command line. */
314 char *execarg = NULL;
315 char *corearg = NULL;
319 /* Pointers to all arguments of +command option. */
321 /* Allocated size of cmdarg. */
323 /* Number of elements of cmdarg used. */
326 /* Indices of all arguments of +directory option. */
328 /* Allocated size. */
330 /* Number of elements used. */
335 /* This needs to happen before the first use of malloc. */
338 #if defined (ALIGN_STACK_ON_STARTUP)
339 i = (int) &count & 0x3;
344 /* If error() is called from initialization code, just exit */
345 if (setjmp (to_top_level)) {
350 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
353 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
357 line = (char *) xmalloc (linesize);
358 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
362 current_directory = dirbuf;
364 #ifdef SET_STACK_LIMIT_HUGE
368 /* Set the stack limit huge so that alloca (particularly stringtab
369 * in dbxread.c) does not fail. */
370 getrlimit (RLIMIT_STACK, &rlim);
371 original_stack_limit = rlim.rlim_cur;
372 rlim.rlim_cur = rlim.rlim_max;
373 setrlimit (RLIMIT_STACK, &rlim);
375 #endif /* SET_STACK_LIMIT_HUGE */
377 /* Parse arguments and options. */
380 static int print_help;
381 /* When var field is 0, use flag field to record the equivalent
382 short option (or arbitrary numbers starting at 10 for those
383 with no equivalent). */
384 static struct option long_options[] =
386 {"quiet", 0, &quiet, 1},
387 {"nx", 0, &inhibit_gdbinit, 1},
388 {"batch", 0, &batch, 1},
389 {"epoch", 0, &epoch_interface, 1},
390 {"fullname", 0, &frame_file_full_name, 1},
391 {"help", 0, &print_help, 1},
393 {"symbols", 1, 0, 's'},
398 {"command", 1, 0, 'x'},
400 {"directory", 1, 0, 'd'},
404 /* Allow machine descriptions to add more options... */
405 #ifdef ADDITIONAL_OPTIONS
413 c = getopt_long_only (argc, argv, "",
414 long_options, &option_index);
418 /* Long option that takes an argument. */
419 if (c == 0 && long_options[option_index].flag == 0)
420 c = long_options[option_index].val;
425 /* Long option that just sets a flag. */
444 cmdarg[ncmd++] = optarg;
448 cmdarg = (char **) xrealloc ((char *)cmdarg,
449 cmdsize * sizeof (*cmdarg));
453 dirarg[ndir++] = optarg;
457 dirarg = (char **) xrealloc ((char *)dirarg,
458 dirsize * sizeof (*dirarg));
470 #ifdef ADDITIONAL_OPTION_CASES
471 ADDITIONAL_OPTION_CASES
475 "Use `%s +help' for a complete list of options.\n",
484 This is GDB, the GNU debugger. Use the command\n\
485 gdb [options] [executable [core-file]]\n\
486 to enter the debugger.\n\
488 Options available are:\n\
489 -help Print this message.\n\
490 -quiet Do not print version number on startup.\n\
491 -fullname Output information used by emacs-GDB interface.\n\
492 -epoch Output information used by epoch emacs-GDB interface.\n\
493 -batch Exit after processing options.\n\
494 -nx Do not read .gdbinit file.\n\
495 -tty=TTY Use TTY for input/output by the program being debugged.\n\
496 -cd=DIR Change current directory to DIR.\n\
497 -directory=DIR Search for source files in DIR.\n\
498 -command=FILE Execute GDB commands from FILE.\n\
499 -symbols=SYMFILE Read symbols from SYMFILE.\n\
500 -exec=EXECFILE Use EXECFILE as the executable.\n\
501 -se=FILE Use FILE as symbol file and executable file.\n\
502 -core=COREFILE Analyze the core dump COREFILE.\n\
503 -b BAUDRATE Set serial port baud rate used for remote debugging\n\
505 #ifdef ADDITIONAL_OPTION_HELP
506 fputs (ADDITIONAL_OPTION_HELP, stderr);
509 For more information, type \"help\" from within GDB, or consult the\n\
510 GDB manual (available as on-line info or a printed manual).\n", stderr);
511 /* Exiting after printing this message seems like
512 the most useful thing to do. */
516 /* OK, that's all the options. The other arguments are filenames. */
518 for (; optind < argc; optind++)
522 symarg = argv[optind];
523 execarg = argv[optind];
526 corearg = argv[optind];
530 "Excess command line arguments ignored. (%s%s)\n",
531 argv[optind], (optind == argc - 1) ? "" : " ...");
538 /* Run the init function of each source file */
540 initialize_cmd_lists (); /* This needs to be done first */
541 initialize_all_files ();
542 initialize_main (); /* But that omits this file! Do it now */
547 /* Print all the junk at the top, with trailing "..." if we are about
548 to read a symbol file (possibly slowly). */
549 print_gnu_advertisement ();
550 print_gdb_version ();
552 printf_filtered ("..");
554 fflush (stdout); /* Force to screen during slow operations */
557 error_pre_print = "\n\n";
559 /* Now perform all the actions indicated by the arguments. */
562 if (!setjmp (to_top_level))
564 cd_command (cdarg, 0);
568 do_cleanups (ALL_CLEANUPS);
570 for (i = 0; i < ndir; i++)
571 if (!setjmp (to_top_level))
572 directory_command (dirarg[i], 0);
574 do_cleanups (ALL_CLEANUPS);
578 && strcmp (execarg, symarg) == 0)
580 /* The exec file and the symbol-file are the same. If we can't open
581 it, better only print one error message. */
582 if (!setjmp (to_top_level))
584 exec_file_command (execarg, !batch);
585 symbol_file_command (symarg, 0);
591 if (!setjmp (to_top_level))
592 exec_file_command (execarg, !batch);
594 if (!setjmp (to_top_level))
595 symbol_file_command (symarg, 0);
597 do_cleanups (ALL_CLEANUPS);
599 /* After the symbol file has been read, print a newline to get us
600 beyond the copyright line... But errors should still set off
601 the error message with a (single) blank line. */
602 printf_filtered ("\n");
603 error_pre_print = "\n";
606 if (!setjmp (to_top_level))
607 core_file_command (corearg, !batch);
608 else if (isdigit (corearg[0]) && !setjmp (to_top_level))
609 attach_command (corearg, !batch);
610 do_cleanups (ALL_CLEANUPS);
613 if (!setjmp (to_top_level))
614 tty_command (ttyarg, !batch);
615 do_cleanups (ALL_CLEANUPS);
617 #ifdef ADDITIONAL_OPTION_HANDLER
618 ADDITIONAL_OPTION_HANDLER;
621 /* Error messages should no longer be distinguished with extra output. */
625 struct stat homebuf, cwdbuf;
626 char *homedir, *homeinit;
628 /* Read init file, if it exists in home directory */
629 homedir = getenv ("HOME");
632 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
633 strlen (gdbinit) + 10);
634 strcpy (homeinit, getenv ("HOME"));
635 strcat (homeinit, "/");
636 strcat (homeinit, gdbinit);
637 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
638 if (!setjmp (to_top_level))
639 source_command (homeinit, 0);
640 do_cleanups (ALL_CLEANUPS);
642 /* Do stats; no need to do them elsewhere since we'll only
643 need them if homedir is set. Make sure that they are
644 zero in case one of them fails (this guarantees that they
645 won't match if either exists). */
647 bzero (&homebuf, sizeof (struct stat));
648 bzero (&cwdbuf, sizeof (struct stat));
650 stat (homeinit, &homebuf);
651 stat (gdbinit, &cwdbuf); /* We'll only need this if
655 /* Read the input file in the current directory, *if* it isn't
656 the same file (it should exist, also). */
659 || bcmp ((char *) &homebuf,
661 sizeof (struct stat)))
662 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
663 if (!setjmp (to_top_level))
664 source_command (gdbinit, 0);
665 do_cleanups (ALL_CLEANUPS);
668 for (i = 0; i < ncmd; i++)
669 if (!setjmp (to_top_level))
671 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
672 read_command_file (stdin);
674 source_command (cmdarg[i], !batch);
675 do_cleanups (ALL_CLEANUPS);
681 /* We have hit the end of the batch file. */
685 /* Do any host- or target-specific hacks. This is used for i960 targets
686 to force the user to set a nindy target and spec its parameters. */
688 #ifdef BEFORE_MAIN_LOOP_HOOK
689 BEFORE_MAIN_LOOP_HOOK;
692 /* The command loop. */
696 if (!setjmp (to_top_level))
698 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
700 quit_command ((char *)0, instream == stdin);
703 /* No exit -- exit is through quit_command. */
706 /* Execute the line P as a command.
707 Pass FROM_TTY as second argument to the defining function. */
710 execute_command (p, from_tty)
714 register struct cmd_list_element *c;
715 register struct command_line *cmdlines;
716 register enum language flang;
717 static enum language current = language_unknown;
718 static int warned = 0;
722 /* This can happen when command_line_input hits end of file. */
726 while (*p == ' ' || *p == '\t') p++;
731 c = lookup_cmd (&p, cmdlist, "", 0, 1);
732 /* Pass null arg rather than an empty one. */
734 if (c->class == class_user)
736 struct cleanup *old_chain;
739 error ("User-defined commands cannot take arguments.");
740 cmdlines = c->user_commands;
745 /* Set the instream to 0, indicating execution of a
746 user-defined function. */
747 old_chain = make_cleanup (source_cleanup, instream);
748 instream = (FILE *) 0;
751 execute_command (cmdlines->line, 0);
752 cmdlines = cmdlines->next;
754 do_cleanups (old_chain);
756 else if (c->type == set_cmd || c->type == show_cmd)
757 do_setshow_command (arg, from_tty & caution, c);
758 else if (c->function == NO_FUNCTION)
759 error ("That is not a command, just a help topic.");
761 (*c->function) (arg, from_tty & caution);
764 /* Tell the user if the language has changed */
765 if (working_lang != current)
767 if (language_mode == language_mode_auto) {
768 if (current != language_unknown)
771 current = working_lang;
775 /* Warn the user if the working language does not match the
776 language of the current frame. Only warn the user if we are
777 actually running the program, i.e. there is a stack. */
778 if (target_has_stack)
780 flang = get_frame_language();
781 if(!warned && flang != language_unknown && flang != working_lang)
783 printf_filtered ("%s\n", lang_frame_mismatch_warn);
791 command_loop_marker (foo)
796 /* Read commands from `instream' and execute them
797 until end of file or error reading instream. */
801 struct cleanup *old_chain;
803 int stdin_is_tty = ISATTY (stdin);
805 while (!feof (instream))
807 if (window_hook && instream == stdin)
808 (*window_hook) (instream, prompt);
811 if (instream == stdin && stdin_is_tty)
812 reinitialize_more_filter ();
813 old_chain = make_cleanup (command_loop_marker, 0);
814 command = command_line_input (instream == stdin ? prompt : 0,
818 execute_command (command, instream == stdin);
819 /* Do any commands attached to breakpoint we stopped at. */
820 bpstat_do_actions (&stop_bpstat);
821 do_cleanups (old_chain);
825 /* Commands call this if they do not want to be repeated by null lines. */
830 /* If we aren't reading from standard input, we are saving the last
831 thing read from stdin in line and don't want to delete it. Null lines
832 won't repeat here in any case. */
833 if (instream == stdin)
837 /* Read a line from the stream "instream" without command line editing.
839 It prints PRROMPT once at the start.
840 Action is compatible with "readline", e.g. space for the result is
841 malloc'd and should be freed by the caller.
843 A NULL return means end of file. */
845 gdb_readline (prrompt)
851 int result_size = 80;
859 result = (char *) xmalloc (result_size);
863 /* Read from stdin if we are executing a user defined command.
864 This is the right thing for prompt_for_continue, at least. */
865 c = fgetc (instream ? instream : stdin);
876 result[input_index++] = c;
877 while (input_index >= result_size)
880 result = (char *) xrealloc (result, result_size);
884 result[input_index++] = '\0';
888 /* Declaration for fancy readline with command line editing. */
891 /* Variables which control command line editing and history
892 substitution. These variables are given default values at the end
894 static int command_editing_p;
895 static int history_expansion_p;
896 static int write_history_p;
897 static int history_size;
898 static char *history_filename;
900 /* Variables which are necessary for fancy command line editing. */
901 char *gdb_completer_word_break_characters =
902 " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
904 /* Functions that are used as part of the fancy command line editing. */
906 /* This can be used for functions which don't want to complete on symbols
907 but don't want to complete on anything else either. */
910 noop_completer (text)
916 /* Generate symbol names one by one for the completer. If STATE is
917 zero, then we need to initialize, otherwise the initialization has
918 already taken place. TEXT is what we expect the symbol to start
919 with. RL_LINE_BUFFER is available to be looked at; it contains the
920 entire text of the line. RL_POINT is the offset in that line of
921 the cursor. You should pretend that the line ends at RL_POINT.
922 The result is NULL if there are no more completions, else a char
923 string which is a possible completion. */
925 symbol_completion_function (text, state)
929 static char **list = (char **)NULL;
932 extern char *rl_line_buffer;
934 char *tmp_command, *p;
935 struct cmd_list_element *c, *result_list;
939 /* Free the storage used by LIST, but not by the strings inside. This is
940 because rl_complete_internal () frees the strings. */
946 /* Decide whether to complete on a list of gdb commands or on
948 tmp_command = (char *) alloca (rl_point + 1);
951 strncpy (tmp_command, rl_line_buffer, rl_point);
952 tmp_command[rl_point] = '\0';
956 /* An empty line we want to consider ambiguous; that is,
957 it could be any command. */
958 c = (struct cmd_list_element *) -1;
962 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
964 /* Move p up to the next interesting thing. */
965 while (*p == ' ' || *p == '\t')
969 /* He's typed something unrecognizable. Sigh. */
971 else if (c == (struct cmd_list_element *) -1)
973 /* If we didn't recognize everything up to the thing that
974 needs completing, and we don't know what command it is
975 yet, we are in trouble. Part of the trouble might be
976 that the list of delimiters used by readline includes
977 '-', which we use in commands. Check for this. */
978 if (p + strlen(text) != tmp_command + rl_point) {
979 if (tmp_command[rl_point - strlen(text) - 1] == '-')
982 /* This really should not produce an error. Better would
983 be to pretend to hit RETURN here; this would produce a
984 response like "Ambiguous command: foo, foobar, etc",
985 and leave the line available for re-entry with ^P. Instead,
986 this error blows away the user's typed input without
987 any way to get it back. */
988 error (" Unrecognized command.");
992 /* He's typed something ambiguous. This is easier. */
994 list = complete_on_cmdlist (*result_list->prefixlist, text);
996 list = complete_on_cmdlist (cmdlist, text);
1000 /* If we've gotten this far, gdb has recognized a full
1001 command. There are several possibilities:
1003 1) We need to complete on the command.
1004 2) We need to complete on the possibilities coming after
1006 2) We need to complete the text of what comes after the
1010 /* Always (might be longer versions of thie command). */
1011 list = complete_on_cmdlist (result_list, text);
1012 else if (!*p && !*text)
1015 list = complete_on_cmdlist (*c->prefixlist, "");
1017 list = (*c->completer) ("");
1021 if (c->prefixlist && !c->allow_unknown)
1024 /* Something like "info adsfkdj". But error() is not
1025 the proper response; just return no completions
1028 error ("\"%s\" command requires a subcommand.",
1035 list = (*c->completer) (text);
1040 /* If the debugged program wasn't compiled with symbols, or if we're
1041 clearly completing on a command and no command matches, return
1044 return ((char *)NULL);
1046 output = list[index];
1057 #if STOP_SIGNAL == SIGTSTP
1058 signal (SIGTSTP, SIG_DFL);
1060 kill (getpid (), SIGTSTP);
1061 signal (SIGTSTP, stop_sig);
1063 signal (STOP_SIGNAL, stop_sig);
1065 printf ("%s", prompt);
1068 /* Forget about any previous command -- null line now will do nothing. */
1071 #endif /* STOP_SIGNAL */
1073 /* Initialize signal handlers. */
1082 extern void request_quit ();
1084 signal (SIGINT, request_quit);
1086 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1087 passed to the inferior, which we don't want. It would be
1088 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1089 on BSD4.3 systems using vfork, that can affect the
1090 GDB process as well as the inferior (the signal handling tables
1091 might be in memory, shared between the two). Since we establish
1092 a handler for SIGQUIT, when we call exec it will set the signal
1093 to SIG_DFL for us. */
1094 signal (SIGQUIT, do_nothing);
1095 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1096 signal (SIGHUP, disconnect);
1097 signal (SIGFPE, float_handler);
1100 /* Read one line from the command input stream `instream'
1101 into the local static buffer `linebuffer' (whose current length
1103 The buffer is made bigger as necessary.
1104 Returns the address of the start of the line.
1106 NULL is returned for end of file.
1108 *If* the instream == stdin & stdin is a terminal, the line read
1109 is copied into the file line saver (global var char *line,
1110 length linesize) so that it can be duplicated.
1112 This routine either uses fancy command line editing or
1113 simple input as the user has requested. */
1116 command_line_input (prrompt, repeat)
1120 static char *linebuffer = 0;
1121 static int linelength = 0;
1125 char *local_prompt = prrompt;
1130 if (linebuffer == 0)
1133 linebuffer = (char *) xmalloc (linelength);
1138 /* Control-C quits instantly if typed while in this loop
1139 since it should not wait until the user types a newline. */
1142 signal (STOP_SIGNAL, stop_sig);
1147 /* Reports are that some Sys V's don't flush stdout/err on reads
1148 from stdin, when stdin/out are sockets rather than ttys. So we
1149 have to do it ourselves, to make emacs-gdb and xxgdb work.
1150 On other machines, doing this once per input should be a cheap nop. */
1154 /* Don't use fancy stuff if not talking to stdin. */
1155 if (command_editing_p && instream == stdin
1156 && ISATTY (instream))
1157 rl = readline (local_prompt);
1159 rl = gdb_readline (local_prompt);
1161 if (!rl || rl == (char *) EOF)
1166 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
1168 linelength = strlen(rl) + 1 + (p - linebuffer);
1169 nline = (char *) xrealloc (linebuffer, linelength);
1170 p += nline - linebuffer;
1174 /* Copy line. Don't copy null at end. (Leaves line alone
1175 if this was just a newline) */
1179 free (rl); /* Allocated in readline. */
1181 if (p == linebuffer || *(p - 1) != '\\')
1184 p--; /* Put on top of '\'. */
1185 local_prompt = (char *) 0;
1189 signal (SIGTSTP, SIG_DFL);
1196 /* Do history expansion if that is wished. */
1197 if (history_expansion_p && instream == stdin
1198 && ISATTY (instream))
1200 char *history_value;
1203 *p = '\0'; /* Insert null now. */
1204 expanded = history_expand (linebuffer, &history_value);
1207 /* Print the changes. */
1208 printf ("%s\n", history_value);
1210 /* If there was an error, call this function again. */
1213 free (history_value);
1214 return command_line_input (prrompt, repeat);
1216 if (strlen (history_value) > linelength)
1218 linelength = strlen (history_value) + 1;
1219 linebuffer = (char *) xrealloc (linebuffer, linelength);
1221 strcpy (linebuffer, history_value);
1222 p = linebuffer + strlen(linebuffer);
1223 free (history_value);
1227 /* If we just got an empty line, and that is supposed
1228 to repeat the previous command, return the value in the
1232 if (p == linebuffer)
1235 while (*p1 == ' ' || *p1 == '\t')
1243 /* Add line to history if appropriate. */
1244 if (instream == stdin
1245 && ISATTY (stdin) && *linebuffer)
1246 add_history (linebuffer);
1248 /* Note: lines consisting soley of comments are added to the command
1249 history. This is useful when you type a command, and then
1250 realize you don't want to execute it quite yet. You can comment
1251 out the command and then later fetch it from the value history
1252 and remove the '#'. The kill ring is probably better, but some
1253 people are in the habit of commenting things out. */
1255 while ((c = *p1++) != '\0')
1258 while ((c = *p1++) != '"')
1260 /* Make sure an escaped '"' doesn't make us think the string
1268 while ((c = *p1++) != '\'')
1270 /* Make sure an escaped '\'' doesn't make us think the string
1279 /* Found a comment. */
1285 /* Save into global buffer if appropriate. */
1288 if (linelength > linesize)
1290 line = xrealloc (line, linelength);
1291 linesize = linelength;
1293 strcpy (line, linebuffer);
1300 /* Read lines from the input stream
1301 and accumulate them in a chain of struct command_line's
1302 which is then returned. */
1304 struct command_line *
1305 read_command_lines ()
1307 struct command_line *first = 0;
1308 register struct command_line *next, *tail = 0;
1309 register char *p, *p1;
1310 struct cleanup *old_chain = 0;
1315 p = command_line_input (0, instream == stdin);
1317 /* Treat end of file like "end". */
1320 /* Remove leading and trailing blanks. */
1321 while (*p == ' ' || *p == '\t') p++;
1322 p1 = p + strlen (p);
1323 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
1325 /* Is this "end"? */
1326 if (p1 - p == 3 && !strncmp (p, "end", 3))
1329 /* No => add this line to the chain of command lines. */
1330 next = (struct command_line *) xmalloc (sizeof (struct command_line));
1331 next->line = savestring (p, p1 - p);
1339 /* We just read the first line.
1340 From now on, arrange to throw away the lines we have
1341 if we quit or get an error while inside this function. */
1343 old_chain = make_cleanup (free_command_lines, &first);
1350 /* Now we are about to return the chain to our caller,
1351 so freeing it becomes his responsibility. */
1353 discard_cleanups (old_chain);
1357 /* Free a chain of struct command_line's. */
1360 free_command_lines (lptr)
1361 struct command_line **lptr;
1363 register struct command_line *l = *lptr;
1364 register struct command_line *next;
1375 /* Add an element to the list of info subcommands. */
1378 add_info (name, fun, doc)
1383 add_cmd (name, no_class, fun, doc, &infolist);
1386 /* Add an alias to the list of info subcommands. */
1389 add_info_alias (name, oldname, abbrev_flag)
1394 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
1397 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1398 Therefore, its own definition is called only for "info" with no args. */
1402 info_command (arg, from_tty)
1406 printf ("\"info\" must be followed by the name of an info command.\n");
1407 help_list (infolist, "info ", -1, stdout);
1410 /* The "show" command with no arguments shows all the settings. */
1414 show_command (arg, from_tty)
1418 cmd_show_list (showlist, from_tty, "");
1421 /* Add an element to the list of commands. */
1424 add_com (name, class, fun, doc)
1426 enum command_class class;
1430 add_cmd (name, class, fun, doc, &cmdlist);
1433 /* Add an alias or abbreviation command to the list of commands. */
1436 add_com_alias (name, oldname, class, abbrev_flag)
1439 enum command_class class;
1442 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
1449 error ("Argument required (%s).", why);
1454 help_command (command, from_tty)
1456 int from_tty; /* Ignored */
1458 help_cmd (command, stdout);
1462 validate_comname (comname)
1468 error_no_arg ("name of command to define");
1473 if (!(*p >= 'A' && *p <= 'Z')
1474 && !(*p >= 'a' && *p <= 'z')
1475 && !(*p >= '0' && *p <= '9')
1477 error ("Junk in argument list: \"%s\"", p);
1483 define_command (comname, from_tty)
1487 register struct command_line *cmds;
1488 register struct cmd_list_element *c, *newc;
1489 char *tem = comname;
1490 extern void not_just_help_class_command ();
1492 validate_comname (comname);
1494 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1497 if (c->class == class_user || c->class == class_alias)
1498 tem = "Redefine command \"%s\"? ";
1500 tem = "Really redefine built-in command \"%s\"? ";
1501 if (!query (tem, comname))
1502 error ("Command \"%s\" not redefined.", comname);
1507 printf ("Type commands for definition of \"%s\".\n\
1508 End with a line saying just \"end\".\n", comname);
1511 comname = savestring (comname, strlen (comname));
1513 cmds = read_command_lines ();
1515 if (c && c->class == class_user)
1516 free_command_lines (&c->user_commands);
1518 newc = add_cmd (comname, class_user, not_just_help_class_command,
1519 (c && c->class == class_user)
1520 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1521 newc->user_commands = cmds;
1525 document_command (comname, from_tty)
1529 struct command_line *doclines;
1530 register struct cmd_list_element *c;
1531 char *tem = comname;
1533 validate_comname (comname);
1535 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1537 if (c->class != class_user)
1538 error ("Command \"%s\" is built-in.", comname);
1541 printf ("Type documentation for \"%s\".\n\
1542 End with a line saying just \"end\".\n", comname);
1544 doclines = read_command_lines ();
1546 if (c->doc) free (c->doc);
1549 register struct command_line *cl1;
1550 register int len = 0;
1552 for (cl1 = doclines; cl1; cl1 = cl1->next)
1553 len += strlen (cl1->line) + 1;
1555 c->doc = (char *) xmalloc (len + 1);
1558 for (cl1 = doclines; cl1; cl1 = cl1->next)
1560 strcat (c->doc, cl1->line);
1562 strcat (c->doc, "\n");
1566 free_command_lines (&doclines);
1570 print_gnu_advertisement()
1573 GDB is free software and you are welcome to distribute copies of it\n\
1574 under certain conditions; type \"info copying\" to see the conditions.\n\
1575 There is absolutely no warranty for GDB; type \"info warranty\" for details.\n\
1580 print_gdb_version ()
1583 GDB %s, Copyright 1991 Free Software Foundation, Inc.",
1589 show_version (args, from_tty)
1594 print_gnu_advertisement ();
1595 print_gdb_version ();
1596 printf_filtered ("\n");
1600 /* xgdb calls this to reprint the usual GDB prompt. */
1605 printf ("%s", prompt);
1610 quit_command (args, from_tty)
1614 if (inferior_pid != 0 && target_has_execution)
1616 if (query ("The program is running. Quit anyway? "))
1618 target_kill (args, from_tty);
1621 error ("Not confirmed.");
1623 /* Save the history information if it is appropriate to do so. */
1624 if (write_history_p && history_filename)
1625 write_history (history_filename);
1630 input_from_terminal_p ()
1632 return (instream == stdin) & caution;
1637 pwd_command (args, from_tty)
1641 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
1644 if (strcmp (dirbuf, current_directory))
1645 printf ("Working directory %s\n (canonically %s).\n",
1646 current_directory, dirbuf);
1648 printf ("Working directory %s.\n", current_directory);
1652 cd_command (dir, from_tty)
1659 /* If the new directory is absolute, repeat is a no-op; if relative,
1660 repeat might be useful but is more likely to be a mistake. */
1664 error_no_arg ("new working directory");
1666 dir = tilde_expand (dir);
1667 make_cleanup (free, dir);
1670 dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
1672 current_directory = dir;
1675 current_directory = concat (current_directory, "/", dir);
1679 /* Now simplify any occurrences of `.' and `..' in the pathname. */
1687 for (p = current_directory; *p;)
1689 if (!strncmp (p, "/./", 2)
1690 && (p[2] == 0 || p[2] == '/'))
1692 else if (!strncmp (p, "/..", 3)
1693 && (p[3] == 0 || p[3] == '/')
1694 && p != current_directory)
1697 while (q != current_directory && q[-1] != '/') q--;
1698 if (q != current_directory)
1708 if (chdir (dir) < 0)
1709 perror_with_name (dir);
1711 forget_cached_source_info ();
1714 pwd_command ((char *) 0, 1);
1719 source_command (args, from_tty)
1724 struct cleanup *cleanups;
1728 /* Let source without arguments read .gdbinit. */
1731 file = tilde_expand (file);
1732 make_cleanup (free, file);
1734 stream = fopen (file, "r");
1736 perror_with_name (file);
1738 cleanups = make_cleanup (fclose, stream);
1740 read_command_file (stream);
1742 do_cleanups (cleanups);
1747 echo_command (text, from_tty)
1759 /* \ at end of argument is used after spaces
1760 so they won't be lost. */
1764 c = parse_escape (&p);
1776 dump_me_command (args, from_tty)
1780 if (query ("Should GDB dump core? "))
1782 signal (SIGQUIT, SIG_DFL);
1783 kill (getpid (), SIGQUIT);
1787 /* Functions to manipulate command line editing control variables. */
1789 /* Number of commands to print in each call to show_commands. */
1790 #define Hist_print 10
1792 show_commands (args, from_tty)
1796 /* Index for history commands. Relative to history_base. */
1799 /* Number of the history entry which we are planning to display next.
1800 Relative to history_base. */
1803 /* The first command in the history which doesn't exist (i.e. one more
1804 than the number of the last command). Relative to history_base. */
1807 struct _hist_entry *history_get();
1808 extern int history_base;
1811 /* This is all reported by individual "show" commands. */
1812 printf_filtered ("Interactive command editing is %s.\n",
1813 command_editing_p ? "on" : "off");
1815 printf_filtered ("History expansion of command input is %s.\n",
1816 history_expansion_p ? "on" : "off");
1817 printf_filtered ("Writing of a history record upon exit is %s.\n",
1818 write_history_p ? "enabled" : "disabled");
1819 printf_filtered ("The size of the history list (number of stored commands) is %d.\n",
1821 printf_filtered ("The name of the history record is \"%s\".\n\n",
1822 history_filename ? history_filename : "");
1825 /* Print out some of the commands from the command history. */
1826 /* First determine the length of the history list. */
1827 hist_len = history_size;
1828 for (offset = 0; offset < history_size; offset++)
1830 if (!history_get (history_base + offset))
1839 if (args[0] == '+' && args[1] == '\0')
1840 /* "info editing +" should print from the stored position. */
1843 /* "info editing <exp>" should print around command number <exp>. */
1844 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
1846 /* "info editing" means print the last Hist_print commands. */
1849 num = hist_len - Hist_print;
1855 /* If there are at least Hist_print commands, we want to display the last
1856 Hist_print rather than, say, the last 6. */
1857 if (hist_len - num < Hist_print)
1859 num = hist_len - Hist_print;
1865 /* No need for a header now that "info editing" only prints one thing. */
1866 if (num == hist_len - Hist_print)
1867 printf_filtered ("The list of the last %d commands is:\n\n", Hist_print);
1869 printf_filtered ("Some of the stored commands are:\n\n");
1872 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
1874 printf_filtered ("%5d %s\n", history_base + offset,
1875 (history_get (history_base + offset))->line);
1878 /* The next command we want to display is the next one that we haven't
1882 /* If the user repeats this command with return, it should do what
1883 "info editing +" does. This is unnecessary if arg is null,
1884 because "info editing +" is not useful after "info editing". */
1885 if (from_tty && args)
1892 /* Called by do_setshow_command. */
1895 set_history_size_command (args, from_tty, c)
1898 struct cmd_list_element *c;
1900 if (history_size == UINT_MAX)
1901 unstifle_history ();
1903 stifle_history (history_size);
1908 set_history (args, from_tty)
1912 printf ("\"set history\" must be followed by the name of a history subcommand.\n");
1913 help_list (sethistlist, "set history ", -1, stdout);
1918 show_history (args, from_tty)
1922 cmd_show_list (showhistlist, from_tty, "");
1925 int info_verbose = 0; /* Default verbose msgs off */
1927 /* Called by do_setshow_command. An elaborate joke. */
1930 set_verbose (args, from_tty, c)
1933 struct cmd_list_element *c;
1935 char *cmdname = "verbose";
1936 struct cmd_list_element *showcmd;
1938 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
1942 c->doc = "Set verbose printing of informational messages.";
1943 showcmd->doc = "Show verbose printing of informational messages.";
1947 c->doc = "Set verbosity.";
1948 showcmd->doc = "Show verbosity.";
1955 /* This message is based on ANSI C, section 4.7. Note that integer
1956 divide by zero causes this, so "float" is a misnomer. */
1957 error ("Erroneous arithmetic operation.");
1960 /* Return whether we are running a batch file or from terminal. */
1964 return !(instream == stdin && ISATTY (stdin));
1969 initialize_cmd_lists ()
1971 cmdlist = (struct cmd_list_element *) 0;
1972 infolist = (struct cmd_list_element *) 0;
1973 enablelist = (struct cmd_list_element *) 0;
1974 disablelist = (struct cmd_list_element *) 0;
1975 deletelist = (struct cmd_list_element *) 0;
1976 enablebreaklist = (struct cmd_list_element *) 0;
1977 setlist = (struct cmd_list_element *) 0;
1979 sethistlist = (struct cmd_list_element *) 0;
1980 showhistlist = NULL;
1981 unsethistlist = (struct cmd_list_element *) 0;
1987 struct cmd_list_element *c;
1991 #ifdef DEFAULT_PROMPT
1992 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
1994 prompt = savestring ("(gdb) ", 6);
1997 /* Set the important stuff up for command editing. */
1998 command_editing_p = 1;
1999 history_expansion_p = 0;
2000 write_history_p = 0;
2002 if (tmpenv = getenv ("HISTSIZE"))
2003 history_size = atoi (tmpenv);
2007 stifle_history (history_size);
2009 if (tmpenv = getenv ("GDBHISTFILE"))
2010 history_filename = savestring (tmpenv, strlen(tmpenv));
2012 /* We include the current directory so that if the user changes
2013 directories the file written will be the same as the one
2015 history_filename = concat (current_directory, "/.gdb_history", "");
2017 read_history (history_filename);
2019 /* Setup important stuff for command line editing. */
2020 rl_completion_entry_function = (int (*)()) symbol_completion_function;
2021 rl_completer_word_break_characters = gdb_completer_word_break_characters;
2022 rl_readline_name = "gdb";
2024 /* Define the classes of commands.
2025 They will appear in the help list in the reverse of this order. */
2027 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
2028 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
2029 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
2030 The commands in this class are those defined by the user.\n\
2031 Use the \"define\" command to define a command.", &cmdlist);
2032 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
2033 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
2034 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
2035 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
2036 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
2037 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
2038 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2039 counting from zero for the innermost (currently executing) frame.\n\n\
2040 At any time gdb identifies one frame as the \"selected\" frame.\n\
2041 Variable lookups are done with respect to the selected frame.\n\
2042 When the program being debugged stops, gdb selects the innermost frame.\n\
2043 The commands below can be used to select other frames by number or address.",
2045 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
2047 add_com ("pwd", class_files, pwd_command,
2048 "Print working directory. This is used for your program as well.");
2049 add_com ("cd", class_files, cd_command,
2050 "Set working directory to DIR for debugger and program being debugged.\n\
2051 The change does not take effect for the program being debugged\n\
2052 until the next time it is started.");
2055 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
2060 add_com ("echo", class_support, echo_command,
2061 "Print a constant string. Give string as argument.\n\
2062 C escape sequences may be used in the argument.\n\
2063 No newline is added at the end of the argument;\n\
2064 use \"\\n\" if you want a newline to be printed.\n\
2065 Since leading and trailing whitespace are ignored in command arguments,\n\
2066 if you want to print some you must use \"\\\" before leading whitespace\n\
2067 to be printed or after trailing whitespace.");
2068 add_com ("document", class_support, document_command,
2069 "Document a user-defined command.\n\
2070 Give command name as argument. Give documentation on following lines.\n\
2071 End with a line of just \"end\".");
2072 add_com ("define", class_support, define_command,
2073 "Define a new command name. Command name is argument.\n\
2074 Definition appears on following lines, one command per line.\n\
2075 End with a line of just \"end\".\n\
2076 Use the \"document\" command to give documentation for the new command.\n\
2077 Commands defined in this way do not take arguments.");
2080 add_com ("source", class_support, source_command,
2081 "Read commands from a file named FILE.\n\
2082 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
2083 when gdb is started.");
2085 /* Punt file name, we can't help it easily. */
2086 add_com ("source", class_support, source_command,
2087 "Read commands from a file named FILE.\n\
2088 Note that the file \".gdbinit\" is read automatically in this way\n\
2089 when gdb is started.");
2092 add_com ("quit", class_support, quit_command, "Exit gdb.");
2093 add_com ("help", class_support, help_command, "Print list of commands.");
2094 add_com_alias ("q", "quit", class_support, 1);
2095 add_com_alias ("h", "help", class_support, 1);
2098 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
2101 add_show_from_set (c, &showlist);
2102 c->function = set_verbose;
2103 set_verbose (NULL, 0, c);
2105 add_com ("dump-me", class_obscure, dump_me_command,
2106 "Get fatal error; make debugger dump its core.");
2109 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
2110 "Set command line editing.\n\
2111 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2112 Without an argument, command line editing is enabled.", &setlist),
2115 add_prefix_cmd ("history", class_support, set_history,
2116 "Generic command for setting command history parameters.",
2117 &sethistlist, "set history ", 0, &setlist);
2118 add_prefix_cmd ("history", class_support, show_history,
2119 "Generic command for showing command history parameters.",
2120 &showhistlist, "show history ", 0, &showlist);
2123 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
2124 "Set history expansion on command input.\n\
2125 Without an argument, history expansion is enabled.", &sethistlist),
2129 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
2130 "Set saving of the history record on exit.\n\
2131 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2132 Without an argument, saving is enabled.", &sethistlist),
2135 c = add_set_cmd ("size", no_class, var_uinteger, (char *)&history_size,
2136 "Set the size of the command history, \n\
2137 ie. the number of previous commands to keep a record of.", &sethistlist);
2138 add_show_from_set (c, &showhistlist);
2139 c->function = set_history_size_command;
2142 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
2143 "Set the filename in which to record the command history\n\
2144 (the list of previous commands of which a record is kept).", &sethistlist),
2148 (add_set_cmd ("confirm", class_support, var_boolean,
2150 "Set whether to confirm potentially dangerous operations.",
2154 add_prefix_cmd ("info", class_info, info_command,
2155 "Generic command for printing status.",
2156 &infolist, "info ", 0, &cmdlist);
2157 add_com_alias ("i", "info", class_info, 1);
2159 add_prefix_cmd ("show", class_info, show_command,
2160 "Generic command for showing things set with \"set\".",
2161 &showlist, "show ", 0, &cmdlist);
2162 /* Another way to get at the same thing. */
2163 add_info ("set", show_command, "Show all GDB settings.");
2165 add_cmd ("commands", no_class, show_commands, "Status of command editor.",
2168 add_cmd ("version", no_class, show_version,
2169 "Report what version of GDB this is.", &showlist);