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_history ();
174 static void initialize_main ();
175 static void initialize_cmd_lists ();
176 static void init_signals ();
177 static void quit_command ();
178 void command_loop ();
179 static void source_command ();
180 static void print_gdb_version ();
181 static void print_gnu_advertisement ();
182 static void float_handler ();
183 static void cd_command ();
184 static void read_command_file ();
188 /* gdb prints this when reading a command interactively */
191 /* Buffer used for reading command lines, and the size
192 allocated for it so far. */
197 /* Baud rate specified for talking to serial target systems. Default
198 is left as a zero pointer, so targets can choose their own defaults. */
202 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
206 #define STOP_SIGNAL SIGTSTP
210 /* Some System V have job control but not sigsetmask(). */
211 #if !defined (HAVE_SIGSETMASK)
212 #define HAVE_SIGSETMASK !defined (USG)
215 #if 0 == (HAVE_SIGSETMASK)
216 #define sigsetmask(n)
219 /* This is how `error' returns to command level. */
221 jmp_buf to_top_level;
224 return_to_top_level ()
228 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
229 clear_momentary_breakpoints ();
230 disable_current_display ();
231 do_cleanups (ALL_CLEANUPS);
232 longjmp (to_top_level, 1);
235 /* Call FUNC with arg ARGS, catching any errors.
236 If there is no error, return the value returned by FUNC.
237 If there is an error, print ERRSTRING, print the specific error message,
241 catch_errors (func, args, errstring)
248 struct cleanup *saved_cleanup_chain;
249 char *saved_error_pre_print;
251 saved_cleanup_chain = save_cleanups ();
252 saved_error_pre_print = error_pre_print;
254 bcopy (to_top_level, saved, sizeof (jmp_buf));
255 error_pre_print = errstring;
257 if (setjmp (to_top_level) == 0)
258 val = (*func) (args);
262 restore_cleanups (saved_cleanup_chain);
264 error_pre_print = saved_error_pre_print;
265 bcopy (saved, to_top_level, sizeof (jmp_buf));
269 /* Handler for SIGHUP. */
274 kill_inferior_fast ();
275 signal (SIGHUP, SIG_DFL);
276 kill (getpid (), SIGHUP);
279 /* Clean up on error during a "source" command (or execution of a
280 user-defined command). */
283 source_cleanup (stream)
286 /* Restore the previous input stream. */
290 /* Read commands from STREAM. */
292 read_command_file (stream)
295 struct cleanup *cleanups;
297 cleanups = make_cleanup (source_cleanup, instream);
300 do_cleanups (cleanups);
309 static int inhibit_gdbinit = 0;
310 static int quiet = 0;
311 static int batch = 0;
313 /* Pointers to various arguments from command line. */
315 char *execarg = NULL;
316 char *corearg = NULL;
320 /* Pointers to all arguments of +command option. */
322 /* Allocated size of cmdarg. */
324 /* Number of elements of cmdarg used. */
327 /* Indices of all arguments of +directory option. */
329 /* Allocated size. */
331 /* Number of elements used. */
336 /* This needs to happen before the first use of malloc. */
339 #if defined (ALIGN_STACK_ON_STARTUP)
340 i = (int) &count & 0x3;
345 /* If error() is called from initialization code, just exit */
346 if (setjmp (to_top_level)) {
351 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
354 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
358 line = (char *) xmalloc (linesize);
359 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
363 current_directory = dirbuf;
365 #ifdef SET_STACK_LIMIT_HUGE
369 /* Set the stack limit huge so that alloca (particularly stringtab
370 * in dbxread.c) does not fail. */
371 getrlimit (RLIMIT_STACK, &rlim);
372 original_stack_limit = rlim.rlim_cur;
373 rlim.rlim_cur = rlim.rlim_max;
374 setrlimit (RLIMIT_STACK, &rlim);
376 #endif /* SET_STACK_LIMIT_HUGE */
378 /* Parse arguments and options. */
381 static int print_help;
382 /* When var field is 0, use flag field to record the equivalent
383 short option (or arbitrary numbers starting at 10 for those
384 with no equivalent). */
385 static struct option long_options[] =
387 {"quiet", 0, &quiet, 1},
388 {"nx", 0, &inhibit_gdbinit, 1},
389 {"batch", 0, &batch, 1},
390 {"epoch", 0, &epoch_interface, 1},
391 {"fullname", 0, &frame_file_full_name, 1},
392 {"help", 0, &print_help, 1},
394 {"symbols", 1, 0, 's'},
399 {"command", 1, 0, 'x'},
401 {"directory", 1, 0, 'd'},
405 /* Allow machine descriptions to add more options... */
406 #ifdef ADDITIONAL_OPTIONS
414 c = getopt_long_only (argc, argv, "",
415 long_options, &option_index);
419 /* Long option that takes an argument. */
420 if (c == 0 && long_options[option_index].flag == 0)
421 c = long_options[option_index].val;
426 /* Long option that just sets a flag. */
445 cmdarg[ncmd++] = optarg;
449 cmdarg = (char **) xrealloc ((char *)cmdarg,
450 cmdsize * sizeof (*cmdarg));
454 dirarg[ndir++] = optarg;
458 dirarg = (char **) xrealloc ((char *)dirarg,
459 dirsize * sizeof (*dirarg));
471 #ifdef ADDITIONAL_OPTION_CASES
472 ADDITIONAL_OPTION_CASES
476 "Use `%s +help' for a complete list of options.\n",
485 This is GDB, the GNU debugger. Use the command\n\
486 gdb [options] [executable [core-file]]\n\
487 to enter the debugger.\n\
489 Options available are:\n\
490 -help Print this message.\n\
491 -quiet Do not print version number on startup.\n\
492 -fullname Output information used by emacs-GDB interface.\n\
493 -epoch Output information used by epoch emacs-GDB interface.\n\
494 -batch Exit after processing options.\n\
495 -nx Do not read .gdbinit file.\n\
496 -tty=TTY Use TTY for input/output by the program being debugged.\n\
497 -cd=DIR Change current directory to DIR.\n\
498 -directory=DIR Search for source files in DIR.\n\
499 -command=FILE Execute GDB commands from FILE.\n\
500 -symbols=SYMFILE Read symbols from SYMFILE.\n\
501 -exec=EXECFILE Use EXECFILE as the executable.\n\
502 -se=FILE Use FILE as symbol file and executable file.\n\
503 -core=COREFILE Analyze the core dump COREFILE.\n\
504 -b BAUDRATE Set serial port baud rate used for remote debugging\n\
506 #ifdef ADDITIONAL_OPTION_HELP
507 fputs (ADDITIONAL_OPTION_HELP, stderr);
510 For more information, type \"help\" from within GDB, or consult the\n\
511 GDB manual (available as on-line info or a printed manual).\n", stderr);
512 /* Exiting after printing this message seems like
513 the most useful thing to do. */
517 /* OK, that's all the options. The other arguments are filenames. */
519 for (; optind < argc; optind++)
523 symarg = argv[optind];
524 execarg = argv[optind];
527 corearg = argv[optind];
531 "Excess command line arguments ignored. (%s%s)\n",
532 argv[optind], (optind == argc - 1) ? "" : " ...");
539 /* Run the init function of each source file */
541 initialize_cmd_lists (); /* This needs to be done first */
542 initialize_all_files ();
543 initialize_main (); /* But that omits this file! Do it now */
548 /* Print all the junk at the top, with trailing "..." if we are about
549 to read a symbol file (possibly slowly). */
550 print_gnu_advertisement ();
551 print_gdb_version ();
553 printf_filtered ("..");
555 fflush (stdout); /* Force to screen during slow operations */
558 error_pre_print = "\n\n";
560 /* Now perform all the actions indicated by the arguments. */
563 if (!setjmp (to_top_level))
565 cd_command (cdarg, 0);
569 do_cleanups (ALL_CLEANUPS);
571 for (i = 0; i < ndir; i++)
572 if (!setjmp (to_top_level))
573 directory_command (dirarg[i], 0);
575 do_cleanups (ALL_CLEANUPS);
579 && strcmp (execarg, symarg) == 0)
581 /* The exec file and the symbol-file are the same. If we can't open
582 it, better only print one error message. */
583 if (!setjmp (to_top_level))
585 exec_file_command (execarg, !batch);
586 symbol_file_command (symarg, 0);
592 if (!setjmp (to_top_level))
593 exec_file_command (execarg, !batch);
595 if (!setjmp (to_top_level))
596 symbol_file_command (symarg, 0);
598 do_cleanups (ALL_CLEANUPS);
600 /* After the symbol file has been read, print a newline to get us
601 beyond the copyright line... But errors should still set off
602 the error message with a (single) blank line. */
604 printf_filtered ("\n");
605 error_pre_print = "\n";
608 if (!setjmp (to_top_level))
609 core_file_command (corearg, !batch);
610 else if (isdigit (corearg[0]) && !setjmp (to_top_level))
611 attach_command (corearg, !batch);
612 do_cleanups (ALL_CLEANUPS);
615 if (!setjmp (to_top_level))
616 tty_command (ttyarg, !batch);
617 do_cleanups (ALL_CLEANUPS);
619 #ifdef ADDITIONAL_OPTION_HANDLER
620 ADDITIONAL_OPTION_HANDLER;
623 /* Error messages should no longer be distinguished with extra output. */
627 struct stat homebuf, cwdbuf;
628 char *homedir, *homeinit;
630 /* Read init file, if it exists in home directory */
631 homedir = getenv ("HOME");
634 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
635 strlen (gdbinit) + 10);
636 strcpy (homeinit, getenv ("HOME"));
637 strcat (homeinit, "/");
638 strcat (homeinit, gdbinit);
639 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
640 if (!setjmp (to_top_level))
641 source_command (homeinit, 0);
642 do_cleanups (ALL_CLEANUPS);
644 /* Do stats; no need to do them elsewhere since we'll only
645 need them if homedir is set. Make sure that they are
646 zero in case one of them fails (this guarantees that they
647 won't match if either exists). */
649 bzero (&homebuf, sizeof (struct stat));
650 bzero (&cwdbuf, sizeof (struct stat));
652 stat (homeinit, &homebuf);
653 stat (gdbinit, &cwdbuf); /* We'll only need this if
657 /* Read the input file in the current directory, *if* it isn't
658 the same file (it should exist, also). */
661 || bcmp ((char *) &homebuf,
663 sizeof (struct stat)))
664 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
665 if (!setjmp (to_top_level))
666 source_command (gdbinit, 0);
667 do_cleanups (ALL_CLEANUPS);
670 for (i = 0; i < ncmd; i++)
671 if (!setjmp (to_top_level))
673 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
674 read_command_file (stdin);
676 source_command (cmdarg[i], !batch);
677 do_cleanups (ALL_CLEANUPS);
681 /* Read in the old history after all the command files have been read. */
682 initialize_history();
686 /* We have hit the end of the batch file. */
690 /* Do any host- or target-specific hacks. This is used for i960 targets
691 to force the user to set a nindy target and spec its parameters. */
693 #ifdef BEFORE_MAIN_LOOP_HOOK
694 BEFORE_MAIN_LOOP_HOOK;
697 /* The command loop. */
701 if (!setjmp (to_top_level))
703 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
705 quit_command ((char *)0, instream == stdin);
708 /* No exit -- exit is through quit_command. */
711 /* Execute the line P as a command.
712 Pass FROM_TTY as second argument to the defining function. */
715 execute_command (p, from_tty)
719 register struct cmd_list_element *c;
720 register struct command_line *cmdlines;
721 register enum language flang;
722 static struct language_defn *saved_language = 0;
723 static int warned = 0;
727 /* This can happen when command_line_input hits end of file. */
731 while (*p == ' ' || *p == '\t') p++;
736 c = lookup_cmd (&p, cmdlist, "", 0, 1);
737 /* Pass null arg rather than an empty one. */
739 if (c->class == class_user)
741 struct cleanup *old_chain;
744 error ("User-defined commands cannot take arguments.");
745 cmdlines = c->user_commands;
750 /* Set the instream to 0, indicating execution of a
751 user-defined function. */
752 old_chain = make_cleanup (source_cleanup, instream);
753 instream = (FILE *) 0;
756 execute_command (cmdlines->line, 0);
757 cmdlines = cmdlines->next;
759 do_cleanups (old_chain);
761 else if (c->type == set_cmd || c->type == show_cmd)
762 do_setshow_command (arg, from_tty & caution, c);
763 else if (c->function == NO_FUNCTION)
764 error ("That is not a command, just a help topic.");
766 (*c->function) (arg, from_tty & caution);
769 /* Tell the user if the language has changed (except first time). */
770 if (current_language != saved_language)
772 if (language_mode == language_mode_auto) {
776 saved_language = current_language;
780 /* Warn the user if the working language does not match the
781 language of the current frame. Only warn the user if we are
782 actually running the program, i.e. there is a stack. */
783 /* FIXME: This should be cacheing the frame and only running when
784 the frame changes. */
785 if (target_has_stack)
787 flang = get_frame_language ();
789 && flang != language_unknown
790 && flang != current_language->la_language)
792 printf_filtered ("%s\n", lang_frame_mismatch_warn);
800 command_loop_marker (foo)
805 /* Read commands from `instream' and execute them
806 until end of file or error reading instream. */
810 struct cleanup *old_chain;
812 int stdin_is_tty = ISATTY (stdin);
814 while (!feof (instream))
816 if (window_hook && instream == stdin)
817 (*window_hook) (instream, prompt);
820 if (instream == stdin && stdin_is_tty)
821 reinitialize_more_filter ();
822 old_chain = make_cleanup (command_loop_marker, 0);
823 command = command_line_input (instream == stdin ? prompt : 0,
827 execute_command (command, instream == stdin);
828 /* Do any commands attached to breakpoint we stopped at. */
829 bpstat_do_actions (&stop_bpstat);
830 do_cleanups (old_chain);
834 /* Commands call this if they do not want to be repeated by null lines. */
839 /* If we aren't reading from standard input, we are saving the last
840 thing read from stdin in line and don't want to delete it. Null lines
841 won't repeat here in any case. */
842 if (instream == stdin)
846 /* Read a line from the stream "instream" without command line editing.
848 It prints PRROMPT once at the start.
849 Action is compatible with "readline", e.g. space for the result is
850 malloc'd and should be freed by the caller.
852 A NULL return means end of file. */
854 gdb_readline (prrompt)
860 int result_size = 80;
868 result = (char *) xmalloc (result_size);
872 /* Read from stdin if we are executing a user defined command.
873 This is the right thing for prompt_for_continue, at least. */
874 c = fgetc (instream ? instream : stdin);
885 result[input_index++] = c;
886 while (input_index >= result_size)
889 result = (char *) xrealloc (result, result_size);
893 result[input_index++] = '\0';
897 /* Declaration for fancy readline with command line editing. */
900 /* Variables which control command line editing and history
901 substitution. These variables are given default values at the end
903 static int command_editing_p;
904 static int history_expansion_p;
905 static int write_history_p;
906 static int history_size;
907 static char *history_filename;
909 /* Variables which are necessary for fancy command line editing. */
910 char *gdb_completer_word_break_characters =
911 " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
913 /* Functions that are used as part of the fancy command line editing. */
915 /* This can be used for functions which don't want to complete on symbols
916 but don't want to complete on anything else either. */
919 noop_completer (text)
925 /* Generate symbol names one by one for the completer. If STATE is
926 zero, then we need to initialize, otherwise the initialization has
927 already taken place. TEXT is what we expect the symbol to start
928 with. RL_LINE_BUFFER is available to be looked at; it contains the
929 entire text of the line. RL_POINT is the offset in that line of
930 the cursor. You should pretend that the line ends at RL_POINT.
931 The result is NULL if there are no more completions, else a char
932 string which is a possible completion. */
934 symbol_completion_function (text, state)
938 static char **list = (char **)NULL;
941 extern char *rl_line_buffer;
943 char *tmp_command, *p;
944 struct cmd_list_element *c, *result_list;
948 /* Free the storage used by LIST, but not by the strings inside. This is
949 because rl_complete_internal () frees the strings. */
955 /* Decide whether to complete on a list of gdb commands or on
957 tmp_command = (char *) alloca (rl_point + 1);
960 strncpy (tmp_command, rl_line_buffer, rl_point);
961 tmp_command[rl_point] = '\0';
965 /* An empty line we want to consider ambiguous; that is,
966 it could be any command. */
967 c = (struct cmd_list_element *) -1;
971 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
973 /* Move p up to the next interesting thing. */
974 while (*p == ' ' || *p == '\t')
978 /* He's typed something unrecognizable. Sigh. */
980 else if (c == (struct cmd_list_element *) -1)
982 /* If we didn't recognize everything up to the thing that
983 needs completing, and we don't know what command it is
984 yet, we are in trouble. Part of the trouble might be
985 that the list of delimiters used by readline includes
986 '-', which we use in commands. Check for this. */
987 if (p + strlen(text) != tmp_command + rl_point) {
988 if (tmp_command[rl_point - strlen(text) - 1] == '-')
991 /* This really should not produce an error. Better would
992 be to pretend to hit RETURN here; this would produce a
993 response like "Ambiguous command: foo, foobar, etc",
994 and leave the line available for re-entry with ^P. Instead,
995 this error blows away the user's typed input without
996 any way to get it back. */
997 error (" Unrecognized command.");
1001 /* He's typed something ambiguous. This is easier. */
1003 list = complete_on_cmdlist (*result_list->prefixlist, text);
1005 list = complete_on_cmdlist (cmdlist, text);
1009 /* If we've gotten this far, gdb has recognized a full
1010 command. There are several possibilities:
1012 1) We need to complete on the command.
1013 2) We need to complete on the possibilities coming after
1015 2) We need to complete the text of what comes after the
1019 /* Always (might be longer versions of thie command). */
1020 list = complete_on_cmdlist (result_list, text);
1021 else if (!*p && !*text)
1024 list = complete_on_cmdlist (*c->prefixlist, "");
1026 list = (*c->completer) ("");
1030 if (c->prefixlist && !c->allow_unknown)
1033 /* Something like "info adsfkdj". But error() is not
1034 the proper response; just return no completions
1037 error ("\"%s\" command requires a subcommand.",
1044 list = (*c->completer) (text);
1049 /* If the debugged program wasn't compiled with symbols, or if we're
1050 clearly completing on a command and no command matches, return
1053 return ((char *)NULL);
1055 output = list[index];
1066 #if STOP_SIGNAL == SIGTSTP
1067 signal (SIGTSTP, SIG_DFL);
1069 kill (getpid (), SIGTSTP);
1070 signal (SIGTSTP, stop_sig);
1072 signal (STOP_SIGNAL, stop_sig);
1074 printf ("%s", prompt);
1077 /* Forget about any previous command -- null line now will do nothing. */
1080 #endif /* STOP_SIGNAL */
1082 /* Initialize signal handlers. */
1091 extern void request_quit ();
1093 signal (SIGINT, request_quit);
1095 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1096 passed to the inferior, which we don't want. It would be
1097 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1098 on BSD4.3 systems using vfork, that can affect the
1099 GDB process as well as the inferior (the signal handling tables
1100 might be in memory, shared between the two). Since we establish
1101 a handler for SIGQUIT, when we call exec it will set the signal
1102 to SIG_DFL for us. */
1103 signal (SIGQUIT, do_nothing);
1104 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1105 signal (SIGHUP, disconnect);
1106 signal (SIGFPE, float_handler);
1109 /* Read one line from the command input stream `instream'
1110 into the local static buffer `linebuffer' (whose current length
1112 The buffer is made bigger as necessary.
1113 Returns the address of the start of the line.
1115 NULL is returned for end of file.
1117 *If* the instream == stdin & stdin is a terminal, the line read
1118 is copied into the file line saver (global var char *line,
1119 length linesize) so that it can be duplicated.
1121 This routine either uses fancy command line editing or
1122 simple input as the user has requested. */
1125 command_line_input (prrompt, repeat)
1129 static char *linebuffer = 0;
1130 static int linelength = 0;
1134 char *local_prompt = prrompt;
1139 if (linebuffer == 0)
1142 linebuffer = (char *) xmalloc (linelength);
1147 /* Control-C quits instantly if typed while in this loop
1148 since it should not wait until the user types a newline. */
1151 signal (STOP_SIGNAL, stop_sig);
1156 /* Reports are that some Sys V's don't flush stdout/err on reads
1157 from stdin, when stdin/out are sockets rather than ttys. So we
1158 have to do it ourselves, to make emacs-gdb and xxgdb work.
1159 On other machines, doing this once per input should be a cheap nop. */
1163 /* Don't use fancy stuff if not talking to stdin. */
1164 if (command_editing_p && instream == stdin
1165 && ISATTY (instream))
1166 rl = readline (local_prompt);
1168 rl = gdb_readline (local_prompt);
1170 if (!rl || rl == (char *) EOF)
1175 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
1177 linelength = strlen(rl) + 1 + (p - linebuffer);
1178 nline = (char *) xrealloc (linebuffer, linelength);
1179 p += nline - linebuffer;
1183 /* Copy line. Don't copy null at end. (Leaves line alone
1184 if this was just a newline) */
1188 free (rl); /* Allocated in readline. */
1190 if (p == linebuffer || *(p - 1) != '\\')
1193 p--; /* Put on top of '\'. */
1194 local_prompt = (char *) 0;
1198 signal (SIGTSTP, SIG_DFL);
1205 /* Do history expansion if that is wished. */
1206 if (history_expansion_p && instream == stdin
1207 && ISATTY (instream))
1209 char *history_value;
1212 *p = '\0'; /* Insert null now. */
1213 expanded = history_expand (linebuffer, &history_value);
1216 /* Print the changes. */
1217 printf ("%s\n", history_value);
1219 /* If there was an error, call this function again. */
1222 free (history_value);
1223 return command_line_input (prrompt, repeat);
1225 if (strlen (history_value) > linelength)
1227 linelength = strlen (history_value) + 1;
1228 linebuffer = (char *) xrealloc (linebuffer, linelength);
1230 strcpy (linebuffer, history_value);
1231 p = linebuffer + strlen(linebuffer);
1232 free (history_value);
1236 /* If we just got an empty line, and that is supposed
1237 to repeat the previous command, return the value in the
1241 if (p == linebuffer)
1244 while (*p1 == ' ' || *p1 == '\t')
1252 /* Add line to history if appropriate. */
1253 if (instream == stdin
1254 && ISATTY (stdin) && *linebuffer)
1255 add_history (linebuffer);
1257 /* Note: lines consisting soley of comments are added to the command
1258 history. This is useful when you type a command, and then
1259 realize you don't want to execute it quite yet. You can comment
1260 out the command and then later fetch it from the value history
1261 and remove the '#'. The kill ring is probably better, but some
1262 people are in the habit of commenting things out. */
1264 while ((c = *p1++) != '\0')
1267 while ((c = *p1++) != '"')
1269 /* Make sure an escaped '"' doesn't make us think the string
1277 while ((c = *p1++) != '\'')
1279 /* Make sure an escaped '\'' doesn't make us think the string
1288 /* Found a comment. */
1294 /* Save into global buffer if appropriate. */
1297 if (linelength > linesize)
1299 line = xrealloc (line, linelength);
1300 linesize = linelength;
1302 strcpy (line, linebuffer);
1309 /* Read lines from the input stream
1310 and accumulate them in a chain of struct command_line's
1311 which is then returned. */
1313 struct command_line *
1314 read_command_lines ()
1316 struct command_line *first = 0;
1317 register struct command_line *next, *tail = 0;
1318 register char *p, *p1;
1319 struct cleanup *old_chain = 0;
1324 p = command_line_input (0, instream == stdin);
1326 /* Treat end of file like "end". */
1329 /* Remove leading and trailing blanks. */
1330 while (*p == ' ' || *p == '\t') p++;
1331 p1 = p + strlen (p);
1332 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
1334 /* Is this "end"? */
1335 if (p1 - p == 3 && !strncmp (p, "end", 3))
1338 /* No => add this line to the chain of command lines. */
1339 next = (struct command_line *) xmalloc (sizeof (struct command_line));
1340 next->line = savestring (p, p1 - p);
1348 /* We just read the first line.
1349 From now on, arrange to throw away the lines we have
1350 if we quit or get an error while inside this function. */
1352 old_chain = make_cleanup (free_command_lines, &first);
1359 /* Now we are about to return the chain to our caller,
1360 so freeing it becomes his responsibility. */
1362 discard_cleanups (old_chain);
1366 /* Free a chain of struct command_line's. */
1369 free_command_lines (lptr)
1370 struct command_line **lptr;
1372 register struct command_line *l = *lptr;
1373 register struct command_line *next;
1384 /* Add an element to the list of info subcommands. */
1387 add_info (name, fun, doc)
1392 add_cmd (name, no_class, fun, doc, &infolist);
1395 /* Add an alias to the list of info subcommands. */
1398 add_info_alias (name, oldname, abbrev_flag)
1403 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
1406 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1407 Therefore, its own definition is called only for "info" with no args. */
1411 info_command (arg, from_tty)
1415 printf ("\"info\" must be followed by the name of an info command.\n");
1416 help_list (infolist, "info ", -1, stdout);
1419 /* The "show" command with no arguments shows all the settings. */
1423 show_command (arg, from_tty)
1427 cmd_show_list (showlist, from_tty, "");
1430 /* Add an element to the list of commands. */
1433 add_com (name, class, fun, doc)
1435 enum command_class class;
1439 add_cmd (name, class, fun, doc, &cmdlist);
1442 /* Add an alias or abbreviation command to the list of commands. */
1445 add_com_alias (name, oldname, class, abbrev_flag)
1448 enum command_class class;
1451 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
1458 error ("Argument required (%s).", why);
1463 help_command (command, from_tty)
1465 int from_tty; /* Ignored */
1467 help_cmd (command, stdout);
1471 validate_comname (comname)
1477 error_no_arg ("name of command to define");
1482 if (!(*p >= 'A' && *p <= 'Z')
1483 && !(*p >= 'a' && *p <= 'z')
1484 && !(*p >= '0' && *p <= '9')
1486 error ("Junk in argument list: \"%s\"", p);
1492 define_command (comname, from_tty)
1496 register struct command_line *cmds;
1497 register struct cmd_list_element *c, *newc;
1498 char *tem = comname;
1499 extern void not_just_help_class_command ();
1501 validate_comname (comname);
1503 /* Look it up, and verify that we got an exact match. */
1504 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1505 if (c && 0 != strcmp (comname, c->name))
1510 if (c->class == class_user || c->class == class_alias)
1511 tem = "Redefine command \"%s\"? ";
1513 tem = "Really redefine built-in command \"%s\"? ";
1514 if (!query (tem, comname))
1515 error ("Command \"%s\" not redefined.", comname);
1520 printf ("Type commands for definition of \"%s\".\n\
1521 End with a line saying just \"end\".\n", comname);
1524 comname = savestring (comname, strlen (comname));
1526 cmds = read_command_lines ();
1528 if (c && c->class == class_user)
1529 free_command_lines (&c->user_commands);
1531 newc = add_cmd (comname, class_user, not_just_help_class_command,
1532 (c && c->class == class_user)
1533 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1534 newc->user_commands = cmds;
1538 document_command (comname, from_tty)
1542 struct command_line *doclines;
1543 register struct cmd_list_element *c;
1544 char *tem = comname;
1546 validate_comname (comname);
1548 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1550 if (c->class != class_user)
1551 error ("Command \"%s\" is built-in.", comname);
1554 printf ("Type documentation for \"%s\".\n\
1555 End with a line saying just \"end\".\n", comname);
1557 doclines = read_command_lines ();
1559 if (c->doc) free (c->doc);
1562 register struct command_line *cl1;
1563 register int len = 0;
1565 for (cl1 = doclines; cl1; cl1 = cl1->next)
1566 len += strlen (cl1->line) + 1;
1568 c->doc = (char *) xmalloc (len + 1);
1571 for (cl1 = doclines; cl1; cl1 = cl1->next)
1573 strcat (c->doc, cl1->line);
1575 strcat (c->doc, "\n");
1579 free_command_lines (&doclines);
1583 print_gnu_advertisement()
1586 GDB is free software and you are welcome to distribute copies of it\n\
1587 under certain conditions; type \"show copying\" to see the conditions.\n\
1588 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
1593 print_gdb_version ()
1596 GDB %s, Copyright 1991 Free Software Foundation, Inc.",
1602 show_version (args, from_tty)
1607 print_gnu_advertisement ();
1608 print_gdb_version ();
1609 printf_filtered ("\n");
1613 /* xgdb calls this to reprint the usual GDB prompt. */
1618 printf ("%s", prompt);
1623 quit_command (args, from_tty)
1627 if (inferior_pid != 0 && target_has_execution)
1629 if (query ("The program is running. Quit anyway? "))
1631 target_kill (args, from_tty);
1634 error ("Not confirmed.");
1636 /* Save the history information if it is appropriate to do so. */
1637 if (write_history_p && history_filename)
1638 write_history (history_filename);
1643 input_from_terminal_p ()
1645 return (instream == stdin) & caution;
1650 pwd_command (args, from_tty)
1654 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
1657 if (strcmp (dirbuf, current_directory))
1658 printf ("Working directory %s\n (canonically %s).\n",
1659 current_directory, dirbuf);
1661 printf ("Working directory %s.\n", current_directory);
1665 cd_command (dir, from_tty)
1672 /* If the new directory is absolute, repeat is a no-op; if relative,
1673 repeat might be useful but is more likely to be a mistake. */
1677 error_no_arg ("new working directory");
1679 dir = tilde_expand (dir);
1680 make_cleanup (free, dir);
1683 dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
1685 current_directory = dir;
1688 current_directory = concat (current_directory, "/", dir);
1692 /* Now simplify any occurrences of `.' and `..' in the pathname. */
1700 for (p = current_directory; *p;)
1702 if (!strncmp (p, "/./", 2)
1703 && (p[2] == 0 || p[2] == '/'))
1705 else if (!strncmp (p, "/..", 3)
1706 && (p[3] == 0 || p[3] == '/')
1707 && p != current_directory)
1710 while (q != current_directory && q[-1] != '/') q--;
1711 if (q != current_directory)
1721 if (chdir (dir) < 0)
1722 perror_with_name (dir);
1724 forget_cached_source_info ();
1727 pwd_command ((char *) 0, 1);
1732 source_command (args, from_tty)
1737 struct cleanup *cleanups;
1741 /* Let source without arguments read .gdbinit. */
1744 file = tilde_expand (file);
1745 make_cleanup (free, file);
1747 stream = fopen (file, "r");
1749 perror_with_name (file);
1751 cleanups = make_cleanup (fclose, stream);
1753 read_command_file (stream);
1755 do_cleanups (cleanups);
1760 echo_command (text, from_tty)
1772 /* \ at end of argument is used after spaces
1773 so they won't be lost. */
1777 c = parse_escape (&p);
1779 printf_filtered ("%c", c);
1782 printf_filtered ("%c", c);
1785 /* Force this output to appear now. */
1792 dump_me_command (args, from_tty)
1796 if (query ("Should GDB dump core? "))
1798 signal (SIGQUIT, SIG_DFL);
1799 kill (getpid (), SIGQUIT);
1803 /* Functions to manipulate command line editing control variables. */
1805 /* Number of commands to print in each call to show_commands. */
1806 #define Hist_print 10
1808 show_commands (args, from_tty)
1812 /* Index for history commands. Relative to history_base. */
1815 /* Number of the history entry which we are planning to display next.
1816 Relative to history_base. */
1819 /* The first command in the history which doesn't exist (i.e. one more
1820 than the number of the last command). Relative to history_base. */
1823 struct _hist_entry *history_get();
1824 extern int history_base;
1827 /* This is all reported by individual "show" commands. */
1828 printf_filtered ("Interactive command editing is %s.\n",
1829 command_editing_p ? "on" : "off");
1831 printf_filtered ("History expansion of command input is %s.\n",
1832 history_expansion_p ? "on" : "off");
1833 printf_filtered ("Writing of a history record upon exit is %s.\n",
1834 write_history_p ? "enabled" : "disabled");
1835 printf_filtered ("The size of the history list (number of stored commands) is %d.\n",
1837 printf_filtered ("The name of the history record is \"%s\".\n\n",
1838 history_filename ? history_filename : "");
1841 /* Print out some of the commands from the command history. */
1842 /* First determine the length of the history list. */
1843 hist_len = history_size;
1844 for (offset = 0; offset < history_size; offset++)
1846 if (!history_get (history_base + offset))
1855 if (args[0] == '+' && args[1] == '\0')
1856 /* "info editing +" should print from the stored position. */
1859 /* "info editing <exp>" should print around command number <exp>. */
1860 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
1862 /* "info editing" means print the last Hist_print commands. */
1865 num = hist_len - Hist_print;
1871 /* If there are at least Hist_print commands, we want to display the last
1872 Hist_print rather than, say, the last 6. */
1873 if (hist_len - num < Hist_print)
1875 num = hist_len - Hist_print;
1881 /* No need for a header now that "info editing" only prints one thing. */
1882 if (num == hist_len - Hist_print)
1883 printf_filtered ("The list of the last %d commands is:\n\n", Hist_print);
1885 printf_filtered ("Some of the stored commands are:\n\n");
1888 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
1890 printf_filtered ("%5d %s\n", history_base + offset,
1891 (history_get (history_base + offset))->line);
1894 /* The next command we want to display is the next one that we haven't
1898 /* If the user repeats this command with return, it should do what
1899 "info editing +" does. This is unnecessary if arg is null,
1900 because "info editing +" is not useful after "info editing". */
1901 if (from_tty && args)
1908 /* Called by do_setshow_command. */
1911 set_history_size_command (args, from_tty, c)
1914 struct cmd_list_element *c;
1916 if (history_size == UINT_MAX)
1917 unstifle_history ();
1919 stifle_history (history_size);
1924 set_history (args, from_tty)
1928 printf ("\"set history\" must be followed by the name of a history subcommand.\n");
1929 help_list (sethistlist, "set history ", -1, stdout);
1934 show_history (args, from_tty)
1938 cmd_show_list (showhistlist, from_tty, "");
1941 int info_verbose = 0; /* Default verbose msgs off */
1943 /* Called by do_setshow_command. An elaborate joke. */
1946 set_verbose (args, from_tty, c)
1949 struct cmd_list_element *c;
1951 char *cmdname = "verbose";
1952 struct cmd_list_element *showcmd;
1954 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
1958 c->doc = "Set verbose printing of informational messages.";
1959 showcmd->doc = "Show verbose printing of informational messages.";
1963 c->doc = "Set verbosity.";
1964 showcmd->doc = "Show verbosity.";
1971 /* This message is based on ANSI C, section 4.7. Note that integer
1972 divide by zero causes this, so "float" is a misnomer. */
1973 error ("Erroneous arithmetic operation.");
1976 /* Return whether we are running a batch file or from terminal. */
1980 return !(instream == stdin && ISATTY (stdin));
1985 initialize_cmd_lists ()
1987 cmdlist = (struct cmd_list_element *) 0;
1988 infolist = (struct cmd_list_element *) 0;
1989 enablelist = (struct cmd_list_element *) 0;
1990 disablelist = (struct cmd_list_element *) 0;
1991 deletelist = (struct cmd_list_element *) 0;
1992 enablebreaklist = (struct cmd_list_element *) 0;
1993 setlist = (struct cmd_list_element *) 0;
1995 sethistlist = (struct cmd_list_element *) 0;
1996 showhistlist = NULL;
1997 unsethistlist = (struct cmd_list_element *) 0;
2000 /* Init the history buffer. Note that we are called after the init file(s)
2001 * have been read so that the user can change the history file via his
2002 * .gdbinit file (for instance). The GDBHISTFILE environment variable
2003 * overrides all of this.
2007 initialize_history()
2011 if (tmpenv = getenv ("HISTSIZE"))
2012 history_size = atoi (tmpenv);
2013 else if (!history_size)
2016 stifle_history (history_size);
2018 if (tmpenv = getenv ("GDBHISTFILE"))
2019 history_filename = savestring (tmpenv, strlen(tmpenv));
2020 else if (!history_filename) {
2021 /* We include the current directory so that if the user changes
2022 directories the file written will be the same as the one
2024 history_filename = concat (current_directory, "/.gdb_history", "");
2026 read_history (history_filename);
2032 struct cmd_list_element *c;
2034 #ifdef DEFAULT_PROMPT
2035 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
2037 prompt = savestring ("(gdb) ", 6);
2040 /* Set the important stuff up for command editing. */
2041 command_editing_p = 1;
2042 history_expansion_p = 0;
2043 write_history_p = 0;
2045 /* Setup important stuff for command line editing. */
2046 rl_completion_entry_function = (int (*)()) symbol_completion_function;
2047 rl_completer_word_break_characters = gdb_completer_word_break_characters;
2048 rl_readline_name = "gdb";
2050 /* Define the classes of commands.
2051 They will appear in the help list in the reverse of this order. */
2053 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
2054 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
2055 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
2056 The commands in this class are those defined by the user.\n\
2057 Use the \"define\" command to define a command.", &cmdlist);
2058 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
2059 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
2060 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
2061 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
2062 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
2063 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
2064 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2065 counting from zero for the innermost (currently executing) frame.\n\n\
2066 At any time gdb identifies one frame as the \"selected\" frame.\n\
2067 Variable lookups are done with respect to the selected frame.\n\
2068 When the program being debugged stops, gdb selects the innermost frame.\n\
2069 The commands below can be used to select other frames by number or address.",
2071 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
2073 add_com ("pwd", class_files, pwd_command,
2074 "Print working directory. This is used for your program as well.");
2075 add_com ("cd", class_files, cd_command,
2076 "Set working directory to DIR for debugger and program being debugged.\n\
2077 The change does not take effect for the program being debugged\n\
2078 until the next time it is started.");
2081 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
2086 add_com ("echo", class_support, echo_command,
2087 "Print a constant string. Give string as argument.\n\
2088 C escape sequences may be used in the argument.\n\
2089 No newline is added at the end of the argument;\n\
2090 use \"\\n\" if you want a newline to be printed.\n\
2091 Since leading and trailing whitespace are ignored in command arguments,\n\
2092 if you want to print some you must use \"\\\" before leading whitespace\n\
2093 to be printed or after trailing whitespace.");
2094 add_com ("document", class_support, document_command,
2095 "Document a user-defined command.\n\
2096 Give command name as argument. Give documentation on following lines.\n\
2097 End with a line of just \"end\".");
2098 add_com ("define", class_support, define_command,
2099 "Define a new command name. Command name is argument.\n\
2100 Definition appears on following lines, one command per line.\n\
2101 End with a line of just \"end\".\n\
2102 Use the \"document\" command to give documentation for the new command.\n\
2103 Commands defined in this way do not take arguments.");
2106 add_com ("source", class_support, source_command,
2107 "Read commands from a file named FILE.\n\
2108 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
2109 when gdb is started.");
2111 /* Punt file name, we can't help it easily. */
2112 add_com ("source", class_support, source_command,
2113 "Read commands from a file named FILE.\n\
2114 Note that the file \".gdbinit\" is read automatically in this way\n\
2115 when gdb is started.");
2118 add_com ("quit", class_support, quit_command, "Exit gdb.");
2119 add_com ("help", class_support, help_command, "Print list of commands.");
2120 add_com_alias ("q", "quit", class_support, 1);
2121 add_com_alias ("h", "help", class_support, 1);
2124 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
2127 add_show_from_set (c, &showlist);
2128 c->function = set_verbose;
2129 set_verbose (NULL, 0, c);
2131 add_com ("dump-me", class_obscure, dump_me_command,
2132 "Get fatal error; make debugger dump its core.");
2135 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
2136 "Set command line editing.\n\
2137 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2138 Without an argument, command line editing is enabled.", &setlist),
2141 add_prefix_cmd ("history", class_support, set_history,
2142 "Generic command for setting command history parameters.",
2143 &sethistlist, "set history ", 0, &setlist);
2144 add_prefix_cmd ("history", class_support, show_history,
2145 "Generic command for showing command history parameters.",
2146 &showhistlist, "show history ", 0, &showlist);
2149 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
2150 "Set history expansion on command input.\n\
2151 Without an argument, history expansion is enabled.", &sethistlist),
2155 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
2156 "Set saving of the history record on exit.\n\
2157 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2158 Without an argument, saving is enabled.", &sethistlist),
2161 c = add_set_cmd ("size", no_class, var_uinteger, (char *)&history_size,
2162 "Set the size of the command history, \n\
2163 ie. the number of previous commands to keep a record of.", &sethistlist);
2164 add_show_from_set (c, &showhistlist);
2165 c->function = set_history_size_command;
2168 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
2169 "Set the filename in which to record the command history\n\
2170 (the list of previous commands of which a record is kept).", &sethistlist),
2174 (add_set_cmd ("confirm", class_support, var_boolean,
2176 "Set whether to confirm potentially dangerous operations.",
2180 add_prefix_cmd ("info", class_info, info_command,
2181 "Generic command for printing status.",
2182 &infolist, "info ", 0, &cmdlist);
2183 add_com_alias ("i", "info", class_info, 1);
2185 add_prefix_cmd ("show", class_info, show_command,
2186 "Generic command for showing things set with \"set\".",
2187 &showlist, "show ", 0, &cmdlist);
2188 /* Another way to get at the same thing. */
2189 add_info ("set", show_command, "Show all GDB settings.");
2191 add_cmd ("commands", no_class, show_commands, "Status of command editor.",
2194 add_cmd ("version", no_class, show_version,
2195 "Report what version of GDB this is.", &showlist);