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 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 #include "breakpoint.h"
32 #include <readline/readline.h>
33 #include <readline/history.h>
35 /* readline defines this. */
39 #include <sys/types.h>
46 #include <sys/param.h>
49 #ifdef SET_STACK_LIMIT_HUGE
51 #include <sys/resource.h>
54 int original_stack_limit;
58 /* If this definition isn't overridden by the header files, assume
59 that isatty and fileno exist on this system. */
61 #define ISATTY(FP) (isatty (fileno (FP)))
64 /* Initialization file name for gdb. This is overridden in some configs. */
66 #ifndef GDBINIT_FILENAME
67 #define GDBINIT_FILENAME ".gdbinit"
69 char gdbinit[] = GDBINIT_FILENAME;
71 #define ALL_CLEANUPS ((struct cleanup *)0)
73 /* Version number of GDB, as a string. */
77 /* Flag for whether we want all the "from_tty" gubbish printed. */
79 int caution = 1; /* Default is yes, sigh. */
82 * Define all cmd_list_element's
85 /* Chain containing all defined commands. */
87 struct cmd_list_element *cmdlist;
89 /* Chain containing all defined info subcommands. */
91 struct cmd_list_element *infolist;
93 /* Chain containing all defined enable subcommands. */
95 struct cmd_list_element *enablelist;
97 /* Chain containing all defined disable subcommands. */
99 struct cmd_list_element *disablelist;
101 /* Chain containing all defined delete subcommands. */
103 struct cmd_list_element *deletelist;
105 /* Chain containing all defined "enable breakpoint" subcommands. */
107 struct cmd_list_element *enablebreaklist;
109 /* Chain containing all defined set subcommands */
111 struct cmd_list_element *setlist;
113 /* Chain containing all defined show subcommands. */
114 struct cmd_list_element *showlist;
116 /* Chain containing all defined \"set history\". */
118 struct cmd_list_element *sethistlist;
120 /* Chain containing all defined \"show history\". */
121 struct cmd_list_element *showhistlist;
123 /* Chain containing all defined \"unset history\". */
125 struct cmd_list_element *unsethistlist;
127 /* stdio stream that command input is being read from. */
131 /* Current working directory. */
133 char *current_directory;
135 /* The directory name is actually stored here (usually). */
136 static char dirbuf[MAXPATHLEN];
138 /* Function to call before reading a command, if nonzero.
139 The function receives two args: an input stream,
140 and a prompt string. */
142 void (*window_hook) ();
144 extern int frame_file_full_name;
148 /* The external commands we call... */
149 extern void init_source_path ();
150 extern void directory_command ();
151 extern void exec_file_command ();
152 extern void symbol_file_command ();
153 extern void core_file_command ();
154 extern void tty_command ();
156 extern void help_list ();
157 extern void initialize_all_files ();
158 extern void init_malloc ();
160 /* Forward declarations for this file */
161 void free_command_lines ();
162 char *gdb_readline ();
163 char *command_line_input ();
164 static void initialize_main ();
165 static void initialize_cmd_lists ();
166 static void init_signals ();
167 static void quit_command ();
168 void command_loop ();
169 static void source_command ();
170 static void print_gdb_version ();
171 static void float_handler ();
172 static void cd_command ();
173 static void read_command_file ();
177 /* gdb prints this when reading a command interactively */
180 /* Buffer used for reading command lines, and the size
181 allocated for it so far. */
186 /* Baud rate specified for talking to serial target systems. Default
187 is left as a zero pointer, so targets can choose their own defaults. */
191 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
195 #define STOP_SIGNAL SIGTSTP
199 /* Some System V have job control but not sigsetmask(). */
200 #if !defined (HAVE_SIGSETMASK)
201 #define HAVE_SIGSETMASK !defined (USG)
205 #define sigsetmask(n)
208 /* This is how `error' returns to command level. */
210 jmp_buf to_top_level;
213 return_to_top_level ()
217 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
218 clear_momentary_breakpoints ();
219 disable_current_display ();
220 do_cleanups (ALL_CLEANUPS);
221 longjmp (to_top_level, 1);
224 /* Call FUNC with arg ARGS, catching any errors.
225 If there is no error, return the value returned by FUNC.
226 If there is an error, return zero after printing ERRSTRING
227 (which is in addition to the specific error message already printed). */
230 catch_errors (func, args, errstring)
237 struct cleanup *saved_cleanup_chain;
239 saved_cleanup_chain = save_cleanups ();
241 bcopy (to_top_level, saved, sizeof (jmp_buf));
243 if (setjmp (to_top_level) == 0)
244 val = (*func) (args);
248 fprintf (stderr, "%s\n", errstring);
252 restore_cleanups (saved_cleanup_chain);
254 bcopy (saved, to_top_level, sizeof (jmp_buf));
258 /* Handler for SIGHUP. */
263 kill_inferior_fast ();
264 signal (SIGHUP, SIG_DFL);
265 kill (getpid (), SIGHUP);
268 /* Clean up on error during a "source" command (or execution of a
269 user-defined command). */
272 source_cleanup (stream)
275 /* Restore the previous input stream. */
279 /* Read commands from STREAM. */
281 read_command_file (stream)
284 struct cleanup *cleanups;
286 cleanups = make_cleanup (source_cleanup, instream);
289 do_cleanups (cleanups);
298 static int inhibit_gdbinit = 0;
299 static int quiet = 0;
300 static int batch = 0;
302 /* Pointers to various arguments from command line. */
304 char *execarg = NULL;
305 char *corearg = NULL;
309 /* Pointers to all arguments of +command option. */
311 /* Allocated size of cmdarg. */
313 /* Number of elements of cmdarg used. */
316 /* Indices of all arguments of +directory option. */
318 /* Allocated size. */
320 /* Number of elements used. */
325 /* This needs to happen before the first use of malloc. */
328 #if defined (ALIGN_STACK_ON_STARTUP)
329 i = (int) &count & 0x3;
335 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
338 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
342 line = (char *) xmalloc (linesize);
343 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
347 current_directory = dirbuf;
349 #ifdef SET_STACK_LIMIT_HUGE
353 /* Set the stack limit huge so that alloca (particularly stringtab
354 * in dbxread.c) does not fail. */
355 getrlimit (RLIMIT_STACK, &rlim);
356 original_stack_limit = rlim.rlim_cur;
357 rlim.rlim_cur = rlim.rlim_max;
358 setrlimit (RLIMIT_STACK, &rlim);
360 #endif /* SET_STACK_LIMIT_HUGE */
362 /* Parse arguments and options. */
365 static int print_help;
366 /* When var field is 0, use flag field to record the equivalent
367 short option (or arbitrary numbers starting at 10 for those
368 with no equivalent). */
369 static struct option long_options[] =
371 {"quiet", 0, &quiet, 1},
372 {"nx", 0, &inhibit_gdbinit, 1},
373 {"batch", 0, &batch, 1},
374 {"epoch", 0, &epoch_interface, 1},
375 {"fullname", 0, &frame_file_full_name, 1},
376 {"help", 0, &print_help, 1},
378 {"symbols", 1, 0, 's'},
383 {"command", 1, 0, 'x'},
385 {"directory", 1, 0, 'd'},
389 /* Allow machine descriptions to add more options... */
390 #ifdef ADDITIONAL_OPTIONS
397 c = getopt_long_only (argc, argv, "",
398 long_options, &option_index);
402 /* Long option that takes an argument. */
403 if (c == 0 && long_options[option_index].flag == 0)
404 c = long_options[option_index].val;
409 /* Long option that just sets a flag. */
428 cmdarg[ncmd++] = optarg;
432 cmdarg = (char **) xrealloc ((char *)cmdarg,
433 cmdsize * sizeof (*cmdarg));
437 dirarg[ndir++] = optarg;
441 dirarg = (char **) xrealloc ((char *)dirarg,
442 dirsize * sizeof (*dirarg));
454 #ifdef ADDITIONAL_OPTION_CASES
455 ADDITIONAL_OPTION_CASES
459 "Use `%s +help' for a complete list of options.\n",
468 This is GDB, the GNU debugger. Use the command\n\
469 gdb [options] [executable [core-file]]\n\
470 to enter the debugger.\n\
472 Options available are:\n\
473 -help Print this message.\n\
474 -quiet Do not print version number on startup.\n\
475 -fullname Output information used by emacs-GDB interface.\n\
476 -epoch Output information used by epoch emacs-GDB interface.\n\
477 -batch Exit after processing options.\n\
478 -nx Do not read .gdbinit file.\n\
479 -tty=TTY Use TTY for input/output by the program being debugged.\n\
480 -cd=DIR Change current directory to DIR.\n\
481 -directory=DIR Search for source files in DIR.\n\
482 -command=FILE Execute GDB commands from FILE.\n\
483 -symbols=SYMFILE Read symbols from SYMFILE.\n\
484 -exec=EXECFILE Use EXECFILE as the executable.\n\
485 -se=FILE Use FILE as symbol file and executable file.\n\
486 -core=COREFILE Analyze the core dump COREFILE.\n\
487 -b BAUDRATE Set serial port baud rate used for remote debugging\n\
489 #ifdef ADDITIONAL_OPTION_HELP
490 fputs (ADDITIONAL_OPTION_HELP, stderr);
493 For more information, type \"help\" from within GDB, or consult the\n\
494 GDB manual (available as on-line info or a printed manual).\n", stderr);
495 /* Exiting after printing this message seems like
496 the most useful thing to do. */
500 /* OK, that's all the options. The other arguments are filenames. */
502 for (; optind < argc; optind++)
506 symarg = argv[optind];
507 execarg = argv[optind];
510 corearg = argv[optind];
514 "Excess command line arguments ignored. (%s%s)\n",
515 argv[optind], (optind == argc - 1) ? "" : " ...");
522 /* Run the init function of each source file */
524 initialize_cmd_lists (); /* This needs to be done first */
525 initialize_all_files ();
526 initialize_main (); /* But that omits this file! Do it now */
531 /* Print all the junk in one place, with a blank line after it
532 to separate it from important stuff like "no such file".
533 Also, we skip most of the noise, like Emacs, if started with
534 a file name rather than with no arguments. */
536 print_gdb_version (1);
537 printf ("Type \"help\" for a list of commands.\n\n");
541 /* Now perform all the actions indicated by the arguments. */
544 if (!setjmp (to_top_level))
546 cd_command (cdarg, 0);
550 do_cleanups (ALL_CLEANUPS);
552 for (i = 0; i < ndir; i++)
553 if (!setjmp (to_top_level))
554 directory_command (dirarg[i], 0);
556 do_cleanups (ALL_CLEANUPS);
560 && strcmp (execarg, symarg) == 0)
562 /* The exec file and the symbol-file are the same. If we can't open
563 it, better only print one error message. */
564 if (!setjmp (to_top_level))
566 exec_file_command (execarg, !batch);
567 symbol_file_command (symarg, !batch);
573 if (!setjmp (to_top_level))
574 exec_file_command (execarg, !batch);
576 if (!setjmp (to_top_level))
577 symbol_file_command (symarg, !batch);
579 do_cleanups (ALL_CLEANUPS);
582 if (!setjmp (to_top_level))
583 core_file_command (corearg, !batch);
584 else if (!setjmp (to_top_level))
585 attach_command (corearg, !batch);
586 do_cleanups (ALL_CLEANUPS);
589 if (!setjmp (to_top_level))
590 tty_command (ttyarg, !batch);
591 do_cleanups (ALL_CLEANUPS);
593 #ifdef ADDITIONAL_OPTION_HANDLER
594 ADDITIONAL_OPTION_HANDLER;
598 struct stat homebuf, cwdbuf;
599 char *homedir, *homeinit;
601 /* Read init file, if it exists in home directory */
602 homedir = getenv ("HOME");
605 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
606 strlen (gdbinit) + 10);
607 strcpy (homeinit, getenv ("HOME"));
608 strcat (homeinit, "/");
609 strcat (homeinit, gdbinit);
610 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
611 if (!setjmp (to_top_level))
612 source_command (homeinit, 0);
613 do_cleanups (ALL_CLEANUPS);
615 /* Do stats; no need to do them elsewhere since we'll only
616 need them if homedir is set. Make sure that they are
617 zero in case one of them fails (this guarantees that they
618 won't match if either exists). */
620 bzero (&homebuf, sizeof (struct stat));
621 bzero (&cwdbuf, sizeof (struct stat));
623 stat (homeinit, &homebuf);
624 stat (gdbinit, &cwdbuf); /* We'll only need this if
628 /* Read the input file in the current directory, *if* it isn't
629 the same file (it should exist, also). */
632 || bcmp ((char *) &homebuf,
634 sizeof (struct stat)))
635 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
636 if (!setjmp (to_top_level))
637 source_command (gdbinit, 0);
638 do_cleanups (ALL_CLEANUPS);
641 for (i = 0; i < ncmd; i++)
642 if (!setjmp (to_top_level))
644 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
645 read_command_file (stdin);
647 source_command (cmdarg[i], !batch);
648 do_cleanups (ALL_CLEANUPS);
654 /* We have hit the end of the batch file. */
658 /* Do any host- or target-specific hacks. This is used for i960 targets
659 to force the user to set a nindy target and spec its parameters. */
661 #ifdef BEFORE_MAIN_LOOP_HOOK
662 BEFORE_MAIN_LOOP_HOOK;
665 /* The command loop. */
669 if (!setjmp (to_top_level))
671 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
673 quit_command ((char *)0, instream == stdin);
676 /* No exit -- exit is through quit_command. */
679 /* Execute the line P as a command.
680 Pass FROM_TTY as second argument to the defining function. */
683 execute_command (p, from_tty)
687 register struct cmd_list_element *c;
688 register struct command_line *cmdlines;
692 /* This can happen when command_line_input hits end of file. */
696 while (*p == ' ' || *p == '\t') p++;
701 c = lookup_cmd (&p, cmdlist, "", 0, 1);
702 /* Pass null arg rather than an empty one. */
704 if (c->class == class_user)
706 struct cleanup *old_chain;
709 error ("User-defined commands cannot take arguments.");
710 cmdlines = c->user_commands;
715 /* Set the instream to 0, indicating execution of a
716 user-defined function. */
717 old_chain = make_cleanup (source_cleanup, instream);
718 instream = (FILE *) 0;
721 execute_command (cmdlines->line, 0);
722 cmdlines = cmdlines->next;
724 do_cleanups (old_chain);
726 else if (c->type == set_cmd || c->type == show_cmd)
727 do_setshow_command (arg, from_tty & caution, c);
728 else if (c->function == NO_FUNCTION)
729 error ("That is not a command, just a help topic.");
731 (*c->function) (arg, from_tty & caution);
737 command_loop_marker (foo)
742 /* Read commands from `instream' and execute them
743 until end of file or error reading instream. */
747 struct cleanup *old_chain;
749 int stdin_is_tty = ISATTY (stdin);
751 while (!feof (instream))
753 if (window_hook && instream == stdin)
754 (*window_hook) (instream, prompt);
757 if (instream == stdin && stdin_is_tty)
758 reinitialize_more_filter ();
759 old_chain = make_cleanup (command_loop_marker, 0);
760 command = command_line_input (instream == stdin ? prompt : 0,
764 execute_command (command, instream == stdin);
765 /* Do any commands attached to breakpoint we stopped at. */
766 bpstat_do_actions (&stop_bpstat);
767 do_cleanups (old_chain);
771 /* Commands call this if they do not want to be repeated by null lines. */
776 /* If we aren't reading from standard input, we are saving the last
777 thing read from stdin in line and don't want to delete it. Null lines
778 won't repeat here in any case. */
779 if (instream == stdin)
783 /* Read a line from the stream "instream" without command line editing.
785 It prints PRROMPT once at the start.
787 If RETURN_RESULT is set it allocates
788 space for whatever the user types and returns the result.
789 If not, it just discards what the user types and returns a garbage
792 No matter what return_result is, a NULL return means end of file. */
794 gdb_readline (prrompt, return_result)
801 int result_size = 80;
810 result = (char *) xmalloc (result_size);
814 /* Read from stdin if we are executing a user defined command.
815 This is the right thing for prompt_for_continue, at least. */
816 c = fgetc (instream ? instream : stdin);
817 if (c == EOF || c == '\n')
821 result[input_index++] = c;
822 while (input_index >= result_size)
825 result = (char *) xrealloc (result, result_size);
839 result[input_index++] = '\0';
843 /* Return any old non-NULL pointer. */
844 return (char *) "non-NULL";
847 /* Declaration for fancy readline with command line editing. */
850 /* Variables which control command line editing and history
851 substitution. These variables are given default values at the end
853 static int command_editing_p;
854 static int history_expansion_p;
855 static int write_history_p;
856 static int history_size;
857 static char *history_filename;
859 /* Variables which are necessary for fancy command line editing. */
860 char *gdb_completer_word_break_characters =
861 " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
863 /* Functions that are used as part of the fancy command line editing. */
865 /* This can be used for functions which don't want to complete on symbols
866 but don't want to complete on anything else either. */
869 noop_completer (text)
875 /* Generate symbol names one by one for the completer. If STATE is
876 zero, then we need to initialize, otherwise the initialization has
877 already taken place. TEXT is what we expect the symbol to start
878 with. RL_LINE_BUFFER is available to be looked at; it contains the
879 entire text of the line. RL_POINT is the offset in that line of
880 the cursor. You should pretend that the line ends at RL_POINT.
881 The result is NULL if there are no more completions, else a char
882 string which is a possible completion. */
884 symbol_completion_function (text, state)
888 static char **list = (char **)NULL;
891 extern char *rl_line_buffer;
893 char *tmp_command, *p;
894 struct cmd_list_element *c, *result_list;
898 /* Free the storage used by LIST, but not by the strings inside. This is
899 because rl_complete_internal () frees the strings. */
905 /* Decide whether to complete on a list of gdb commands or on
907 tmp_command = (char *) alloca (rl_point + 1);
910 strncpy (tmp_command, rl_line_buffer, rl_point);
911 tmp_command[rl_point] = '\0';
915 /* An empty line we want to consider ambiguous; that is,
916 it could be any command. */
917 c = (struct cmd_list_element *) -1;
921 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
923 /* Move p up to the next interesting thing. */
924 while (*p == ' ' || *p == '\t')
928 /* He's typed something unrecognizable. Sigh. */
930 else if (c == (struct cmd_list_element *) -1)
932 /* If we didn't recognize everything up to the thing that
933 needs completing, and we don't know what command it is
934 yet, we are in trouble. Part of the trouble might be
935 that the list of delimiters used by readline includes
936 '-', which we use in commands. Check for this. */
937 if (p + strlen(text) != tmp_command + rl_point) {
938 if (tmp_command[rl_point - strlen(text) - 1] == '-')
941 /* This really should not produce an error. Better would
942 be to pretend to hit RETURN here; this would produce a
943 response like "Ambiguous command: foo, foobar, etc",
944 and leave the line available for re-entry with ^P. Instead,
945 this error blows away the user's typed input without
946 any way to get it back. */
947 error (" Unrecognized command.");
951 /* He's typed something ambiguous. This is easier. */
953 list = complete_on_cmdlist (*result_list->prefixlist, text);
955 list = complete_on_cmdlist (cmdlist, text);
959 /* If we've gotten this far, gdb has recognized a full
960 command. There are several possibilities:
962 1) We need to complete on the command.
963 2) We need to complete on the possibilities coming after
965 2) We need to complete the text of what comes after the
969 /* Always (might be longer versions of thie command). */
970 list = complete_on_cmdlist (result_list, text);
971 else if (!*p && !*text)
974 list = complete_on_cmdlist (*c->prefixlist, "");
976 list = (*c->completer) ("");
980 if (c->prefixlist && !c->allow_unknown)
983 /* Something like "info adsfkdj". But error() is not
984 the proper response; just return no completions
987 error ("\"%s\" command requires a subcommand.",
994 list = (*c->completer) (text);
999 /* If the debugged program wasn't compiled with symbols, or if we're
1000 clearly completing on a command and no command matches, return
1003 return ((char *)NULL);
1005 output = list[index];
1016 #if STOP_SIGNAL == SIGTSTP
1017 signal (SIGTSTP, SIG_DFL);
1019 kill (getpid (), SIGTSTP);
1020 signal (SIGTSTP, stop_sig);
1022 signal (STOP_SIGNAL, stop_sig);
1024 printf ("%s", prompt);
1027 /* Forget about any previous command -- null line now will do nothing. */
1030 #endif /* STOP_SIGNAL */
1033 Writing the history file upon a terminating signal is not useful,
1034 because the info is rarely relevant and is in the core dump anyway.
1035 It is an annoyance to have the file cluttering up the place.
1036 /* The list of signals that would terminate us if not caught.
1037 We catch them, but just so that we can write the history file,
1039 int terminating_signals[] = {
1040 SIGHUP, SIGINT, SIGILL, SIGTRAP, SIGIOT,
1041 SIGEMT, SIGFPE, SIGKILL, SIGBUS, SIGSEGV, SIGSYS,
1042 SIGPIPE, SIGALRM, SIGTERM,
1063 #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (int))
1066 catch_termination (sig)
1069 /* We are probably here because GDB has a bug. Write out the history
1070 so that we might have a better chance of reproducing it. */
1071 /* Tell the user what we are doing so he can delete the file if
1073 write_history (history_filename);
1074 printf ("\n%s written.\n", history_filename);
1075 signal (sig, SIG_DFL);
1076 kill (getpid (), sig);
1080 /* Initialize signal handlers. */
1089 extern void request_quit ();
1093 for (i = 0; i < TERMSIGS_LENGTH; i++)
1094 signal (terminating_signals[i], catch_termination);
1097 signal (SIGINT, request_quit);
1099 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1100 passed to the inferior, which we don't want. It would be
1101 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1102 on BSD4.3 systems using vfork, that will (apparently) affect the
1103 GDB process as well as the inferior (the signal handling tables
1104 being shared between the two, apparently). Since we establish
1105 a handler for SIGQUIT, when we call exec it will set the signal
1106 to SIG_DFL for us. */
1107 signal (SIGQUIT, do_nothing);
1108 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1109 signal (SIGHUP, disconnect);
1110 signal (SIGFPE, float_handler);
1113 /* Read one line from the command input stream `instream'
1114 into the local static buffer `linebuffer' (whose current length
1116 The buffer is made bigger as necessary.
1117 Returns the address of the start of the line.
1119 NULL is returned for end of file.
1121 *If* the instream == stdin & stdin is a terminal, the line read
1122 is copied into the file line saver (global var char *line,
1123 length linesize) so that it can be duplicated.
1125 This routine either uses fancy command line editing or
1126 simple input as the user has requested. */
1129 command_line_input (prrompt, repeat)
1133 static char *linebuffer = 0;
1134 static int linelength = 0;
1138 char *local_prompt = prrompt;
1143 if (linebuffer == 0)
1146 linebuffer = (char *) xmalloc (linelength);
1151 /* Control-C quits instantly if typed while in this loop
1152 since it should not wait until the user types a newline. */
1155 signal (STOP_SIGNAL, stop_sig);
1160 /* Don't use fancy stuff if not talking to stdin. */
1161 if (command_editing_p && instream == stdin
1162 && ISATTY (instream))
1163 rl = readline (local_prompt);
1165 rl = gdb_readline (local_prompt, 1);
1167 if (!rl || rl == (char *) EOF)
1172 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
1174 linelength = strlen(rl) + 1 + (p - linebuffer);
1175 nline = (char *) xrealloc (linebuffer, linelength);
1176 p += nline - linebuffer;
1180 /* Copy line. Don't copy null at end. (Leaves line alone
1181 if this was just a newline) */
1185 free (rl); /* Allocated in readline. */
1187 if (p == linebuffer || *(p - 1) != '\\')
1190 p--; /* Put on top of '\'. */
1191 local_prompt = (char *) 0;
1195 signal (SIGTSTP, SIG_DFL);
1202 /* Do history expansion if that is wished. */
1203 if (history_expansion_p && instream == stdin
1204 && ISATTY (instream))
1206 char *history_value;
1209 *p = '\0'; /* Insert null now. */
1210 expanded = history_expand (linebuffer, &history_value);
1213 /* Print the changes. */
1214 printf ("%s\n", history_value);
1216 /* If there was an error, call this function again. */
1219 free (history_value);
1220 return command_line_input (prrompt, repeat);
1222 if (strlen (history_value) > linelength)
1224 linelength = strlen (history_value) + 1;
1225 linebuffer = (char *) xrealloc (linebuffer, linelength);
1227 strcpy (linebuffer, history_value);
1228 p = linebuffer + strlen(linebuffer);
1229 free (history_value);
1233 /* If we just got an empty line, and that is supposed
1234 to repeat the previous command, return the value in the
1238 if (p == linebuffer)
1241 while (*p1 == ' ' || *p1 == '\t')
1249 /* Add line to history if appropriate. */
1250 if (instream == stdin
1251 && ISATTY (stdin) && *linebuffer)
1252 add_history (linebuffer);
1254 /* Note: lines consisting soley of comments are added to the command
1255 history. This is useful when you type a command, and then
1256 realize you don't want to execute it quite yet. You can comment
1257 out the command and then later fetch it from the value history
1258 and remove the '#'. The kill ring is probably better, but some
1259 people are in the habit of commenting things out. */
1261 while ((c = *p1++) != '\0')
1264 while ((c = *p1++) != '"')
1266 /* Make sure an escaped '"' doesn't make us think the string
1274 while ((c = *p1++) != '\'')
1276 /* Make sure an escaped '\'' doesn't make us think the string
1285 /* Found a comment. */
1291 /* Save into global buffer if appropriate. */
1294 if (linelength > linesize)
1296 line = xrealloc (line, linelength);
1297 linesize = linelength;
1299 strcpy (line, linebuffer);
1306 /* Read lines from the input stream
1307 and accumulate them in a chain of struct command_line's
1308 which is then returned. */
1310 struct command_line *
1311 read_command_lines ()
1313 struct command_line *first = 0;
1314 register struct command_line *next, *tail = 0;
1315 register char *p, *p1;
1316 struct cleanup *old_chain = 0;
1321 p = command_line_input (0, instream == stdin);
1323 /* Treat end of file like "end". */
1326 /* Remove leading and trailing blanks. */
1327 while (*p == ' ' || *p == '\t') p++;
1328 p1 = p + strlen (p);
1329 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
1331 /* Is this "end"? */
1332 if (p1 - p == 3 && !strncmp (p, "end", 3))
1335 /* No => add this line to the chain of command lines. */
1336 next = (struct command_line *) xmalloc (sizeof (struct command_line));
1337 next->line = savestring (p, p1 - p);
1345 /* We just read the first line.
1346 From now on, arrange to throw away the lines we have
1347 if we quit or get an error while inside this function. */
1349 old_chain = make_cleanup (free_command_lines, &first);
1356 /* Now we are about to return the chain to our caller,
1357 so freeing it becomes his responsibility. */
1359 discard_cleanups (old_chain);
1363 /* Free a chain of struct command_line's. */
1366 free_command_lines (lptr)
1367 struct command_line **lptr;
1369 register struct command_line *l = *lptr;
1370 register struct command_line *next;
1381 /* Add an element to the list of info subcommands. */
1384 add_info (name, fun, doc)
1389 add_cmd (name, no_class, fun, doc, &infolist);
1392 /* Add an alias to the list of info subcommands. */
1395 add_info_alias (name, oldname, abbrev_flag)
1400 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
1403 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1404 Therefore, its own definition is called only for "info" with no args. */
1408 info_command (arg, from_tty)
1412 printf ("\"info\" must be followed by the name of an info command.\n");
1413 help_list (infolist, "info ", -1, stdout);
1416 /* The "show" command with no arguments shows all the settings. */
1420 show_command (arg, from_tty)
1424 cmd_show_list (showlist, from_tty, "");
1427 /* Add an element to the list of commands. */
1430 add_com (name, class, fun, doc)
1432 enum command_class class;
1436 add_cmd (name, class, fun, doc, &cmdlist);
1439 /* Add an alias or abbreviation command to the list of commands. */
1442 add_com_alias (name, oldname, class, abbrev_flag)
1445 enum command_class class;
1448 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
1455 error ("Argument required (%s).", why);
1459 help_command (command, from_tty)
1461 int from_tty; /* Ignored */
1463 help_cmd (command, stdout);
1467 validate_comname (comname)
1473 error_no_arg ("name of command to define");
1478 if (!(*p >= 'A' && *p <= 'Z')
1479 && !(*p >= 'a' && *p <= 'z')
1480 && !(*p >= '0' && *p <= '9')
1482 error ("Junk in argument list: \"%s\"", p);
1488 define_command (comname, from_tty)
1492 register struct command_line *cmds;
1493 register struct cmd_list_element *c, *newc;
1494 char *tem = comname;
1495 extern void not_just_help_class_command ();
1497 validate_comname (comname);
1499 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1502 if (c->class == class_user || c->class == class_alias)
1503 tem = "Redefine command \"%s\"? ";
1505 tem = "Really redefine built-in command \"%s\"? ";
1506 if (!query (tem, comname))
1507 error ("Command \"%s\" not redefined.", comname);
1512 printf ("Type commands for definition of \"%s\".\n\
1513 End with a line saying just \"end\".\n", comname);
1516 comname = savestring (comname, strlen (comname));
1518 cmds = read_command_lines ();
1520 if (c && c->class == class_user)
1521 free_command_lines (&c->user_commands);
1523 newc = add_cmd (comname, class_user, not_just_help_class_command,
1524 (c && c->class == class_user)
1525 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1526 newc->user_commands = cmds;
1530 document_command (comname, from_tty)
1534 struct command_line *doclines;
1535 register struct cmd_list_element *c;
1536 char *tem = comname;
1538 validate_comname (comname);
1540 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1542 if (c->class != class_user)
1543 error ("Command \"%s\" is built-in.", comname);
1546 printf ("Type documentation for \"%s\".\n\
1547 End with a line saying just \"end\".\n", comname);
1549 doclines = read_command_lines ();
1551 if (c->doc) free (c->doc);
1554 register struct command_line *cl1;
1555 register int len = 0;
1557 for (cl1 = doclines; cl1; cl1 = cl1->next)
1558 len += strlen (cl1->line) + 1;
1560 c->doc = (char *) xmalloc (len + 1);
1563 for (cl1 = doclines; cl1; cl1 = cl1->next)
1565 strcat (c->doc, cl1->line);
1567 strcat (c->doc, "\n");
1571 free_command_lines (&doclines);
1575 print_gdb_version (shout)
1578 printf ("GDB %s, Copyright (C) 1991 Free Software Foundation, Inc.\n",
1582 There is ABSOLUTELY NO WARRANTY for GDB; type \"info warranty\" for details.\n\
1583 GDB is free software and you are welcome to distribute copies of it\n\
1584 under certain conditions; type \"info copying\" to see the conditions.\n");
1588 show_version (args, from_tty)
1593 print_gdb_version (0);
1597 /* xgdb calls this to reprint the usual GDB prompt. */
1602 printf ("%s", prompt);
1607 quit_command (args, from_tty)
1611 if (inferior_pid != 0 && target_has_execution)
1613 if (query ("The program is running. Quit anyway? "))
1615 target_kill (args, from_tty);
1618 error ("Not confirmed.");
1620 /* Save the history information if it is appropriate to do so. */
1621 if (write_history_p && history_filename)
1622 write_history (history_filename);
1627 input_from_terminal_p ()
1629 return (instream == stdin) & caution;
1633 pwd_command (args, from_tty)
1637 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
1640 if (strcmp (dirbuf, current_directory))
1641 printf ("Working directory %s\n (canonically %s).\n",
1642 current_directory, dirbuf);
1644 printf ("Working directory %s.\n", current_directory);
1648 cd_command (dir, from_tty)
1655 /* If the new directory is absolute, repeat is a no-op; if relative,
1656 repeat might be useful but is more likely to be a mistake. */
1660 error_no_arg ("new working directory");
1662 dir = tilde_expand (dir);
1663 make_cleanup (free, dir);
1666 dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
1668 current_directory = dir;
1671 current_directory = concat (current_directory, "/", dir);
1675 /* Now simplify any occurrences of `.' and `..' in the pathname. */
1683 for (p = current_directory; *p;)
1685 if (!strncmp (p, "/./", 2)
1686 && (p[2] == 0 || p[2] == '/'))
1688 else if (!strncmp (p, "/..", 3)
1689 && (p[3] == 0 || p[3] == '/')
1690 && p != current_directory)
1693 while (q != current_directory && q[-1] != '/') q--;
1694 if (q != current_directory)
1704 if (chdir (dir) < 0)
1705 perror_with_name (dir);
1707 forget_cached_source_info ();
1710 pwd_command ((char *) 0, 1);
1714 source_command (args, from_tty)
1719 struct cleanup *cleanups;
1723 /* Let source without arguments read .gdbinit. */
1726 file = tilde_expand (file);
1727 make_cleanup (free, file);
1729 stream = fopen (file, "r");
1731 perror_with_name (file);
1733 cleanups = make_cleanup (fclose, stream);
1735 read_command_file (stream);
1737 do_cleanups (cleanups);
1742 echo_command (text, from_tty)
1754 /* \ at end of argument is used after spaces
1755 so they won't be lost. */
1759 c = parse_escape (&p);
1771 dump_me_command (args, from_tty)
1775 if (query ("Should GDB dump core? "))
1777 signal (SIGQUIT, SIG_DFL);
1778 kill (getpid (), SIGQUIT);
1782 /* Functions to manipulate command line editing control variables. */
1784 /* Number of commands to print in each call to show_commands. */
1785 #define Hist_print 10
1787 show_commands (args, from_tty)
1791 /* Index for history commands. Relative to history_base. */
1794 /* Number of the history entry which we are planning to display next.
1795 Relative to history_base. */
1798 /* The first command in the history which doesn't exist (i.e. one more
1799 than the number of the last command). Relative to history_base. */
1802 struct _hist_entry *history_get();
1803 extern int history_base;
1806 /* This is all reported by individual "show" commands. */
1807 printf_filtered ("Interactive command editing is %s.\n",
1808 command_editing_p ? "on" : "off");
1810 printf_filtered ("History expansion of command input is %s.\n",
1811 history_expansion_p ? "on" : "off");
1812 printf_filtered ("Writing of a history record upon exit is %s.\n",
1813 write_history_p ? "enabled" : "disabled");
1814 printf_filtered ("The size of the history list (number of stored commands) is %d.\n",
1816 printf_filtered ("The name of the history record is \"%s\".\n\n",
1817 history_filename ? history_filename : "");
1820 /* Print out some of the commands from the command history. */
1821 /* First determine the length of the history list. */
1822 hist_len = history_size;
1823 for (offset = 0; offset < history_size; offset++)
1825 if (!history_get (history_base + offset))
1834 if (args[0] == '+' && args[1] == '\0')
1835 /* "info editing +" should print from the stored position. */
1838 /* "info editing <exp>" should print around command number <exp>. */
1839 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
1841 /* "info editing" means print the last Hist_print commands. */
1844 num = hist_len - Hist_print;
1850 /* If there are at least Hist_print commands, we want to display the last
1851 Hist_print rather than, say, the last 6. */
1852 if (hist_len - num < Hist_print)
1854 num = hist_len - Hist_print;
1860 /* No need for a header now that "info editing" only prints one thing. */
1861 if (num == hist_len - Hist_print)
1862 printf_filtered ("The list of the last %d commands is:\n\n", Hist_print);
1864 printf_filtered ("Some of the stored commands are:\n\n");
1867 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
1869 printf_filtered ("%5d %s\n", history_base + offset,
1870 (history_get (history_base + offset))->line);
1873 /* The next command we want to display is the next one that we haven't
1877 /* If the user repeats this command with return, it should do what
1878 "info editing +" does. This is unnecessary if arg is null,
1879 because "info editing +" is not useful after "info editing". */
1880 if (from_tty && args)
1887 /* Called by do_setshow_command. */
1889 set_history_size_command (args, from_tty, c)
1892 struct cmd_list_element *c;
1894 if (history_size == UINT_MAX)
1895 unstifle_history ();
1897 stifle_history (history_size);
1901 set_history (args, from_tty)
1905 printf ("\"set history\" must be followed by the name of a history subcommand.\n");
1906 help_list (sethistlist, "set history ", -1, stdout);
1910 show_history (args, from_tty)
1914 cmd_show_list (showhistlist, from_tty, "");
1917 int info_verbose = 0; /* Default verbose msgs off */
1919 /* Called by do_setshow_command. An elaborate joke. */
1921 set_verbose (args, from_tty, c)
1924 struct cmd_list_element *c;
1926 char *cmdname = "verbose";
1927 struct cmd_list_element *showcmd;
1929 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
1933 c->doc = "Set verbose printing of informational messages.";
1934 showcmd->doc = "Show verbose printing of informational messages.";
1938 c->doc = "Set verbosity.";
1939 showcmd->doc = "Show verbosity.";
1946 /* This message is based on ANSI C, section 4.7. Note that integer
1947 divide by zero causes this, so "float" is a misnomer. */
1948 error ("Erroneous arithmetic operation.");
1951 /* Return whether we are running a batch file or from terminal. */
1955 return !(instream == stdin && ISATTY (stdin));
1960 initialize_cmd_lists ()
1962 cmdlist = (struct cmd_list_element *) 0;
1963 infolist = (struct cmd_list_element *) 0;
1964 enablelist = (struct cmd_list_element *) 0;
1965 disablelist = (struct cmd_list_element *) 0;
1966 deletelist = (struct cmd_list_element *) 0;
1967 enablebreaklist = (struct cmd_list_element *) 0;
1968 setlist = (struct cmd_list_element *) 0;
1970 sethistlist = (struct cmd_list_element *) 0;
1971 showhistlist = NULL;
1972 unsethistlist = (struct cmd_list_element *) 0;
1978 struct cmd_list_element *c;
1982 #ifdef DEFAULT_PROMPT
1983 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
1985 prompt = savestring ("(gdb) ", 6);
1988 /* Set the important stuff up for command editing. */
1989 command_editing_p = 1;
1990 history_expansion_p = 0;
1991 write_history_p = 0;
1993 if (tmpenv = getenv ("HISTSIZE"))
1994 history_size = atoi (tmpenv);
1998 stifle_history (history_size);
2000 if (tmpenv = getenv ("GDBHISTFILE"))
2001 history_filename = savestring (tmpenv, strlen(tmpenv));
2003 /* We include the current directory so that if the user changes
2004 directories the file written will be the same as the one
2006 history_filename = concat (current_directory, "/.gdb_history", "");
2008 read_history (history_filename);
2010 /* Setup important stuff for command line editing. */
2011 rl_completion_entry_function = (int (*)()) symbol_completion_function;
2012 rl_completer_word_break_characters = gdb_completer_word_break_characters;
2013 rl_readline_name = "gdb";
2015 /* Define the classes of commands.
2016 They will appear in the help list in the reverse of this order. */
2018 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
2019 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
2020 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
2021 The commands in this class are those defined by the user.\n\
2022 Use the \"define\" command to define a command.", &cmdlist);
2023 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
2024 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
2025 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
2026 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
2027 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
2028 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
2029 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2030 counting from zero for the innermost (currently executing) frame.\n\n\
2031 At any time gdb identifies one frame as the \"selected\" frame.\n\
2032 Variable lookups are done with respect to the selected frame.\n\
2033 When the program being debugged stops, gdb selects the innermost frame.\n\
2034 The commands below can be used to select other frames by number or address.",
2036 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
2038 add_com ("pwd", class_files, pwd_command,
2039 "Print working directory. This is used for your program as well.");
2040 add_com ("cd", class_files, cd_command,
2041 "Set working directory to DIR for debugger and program being debugged.\n\
2042 The change does not take effect for the program being debugged\n\
2043 until the next time it is started.");
2046 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
2051 add_com ("echo", class_support, echo_command,
2052 "Print a constant string. Give string as argument.\n\
2053 C escape sequences may be used in the argument.\n\
2054 No newline is added at the end of the argument;\n\
2055 use \"\\n\" if you want a newline to be printed.\n\
2056 Since leading and trailing whitespace are ignored in command arguments,\n\
2057 if you want to print some you must use \"\\\" before leading whitespace\n\
2058 to be printed or after trailing whitespace.");
2059 add_com ("document", class_support, document_command,
2060 "Document a user-defined command.\n\
2061 Give command name as argument. Give documentation on following lines.\n\
2062 End with a line of just \"end\".");
2063 add_com ("define", class_support, define_command,
2064 "Define a new command name. Command name is argument.\n\
2065 Definition appears on following lines, one command per line.\n\
2066 End with a line of just \"end\".\n\
2067 Use the \"document\" command to give documentation for the new command.\n\
2068 Commands defined in this way do not take arguments.");
2071 add_com ("source", class_support, source_command,
2072 "Read commands from a file named FILE.\n\
2073 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
2074 when gdb is started.");
2076 /* Punt file name, we can't help it easily. */
2077 add_com ("source", class_support, source_command,
2078 "Read commands from a file named FILE.\n\
2079 Note that the file \".gdbinit\" is read automatically in this way\n\
2080 when gdb is started.");
2083 add_com ("quit", class_support, quit_command, "Exit gdb.");
2084 add_com ("help", class_support, help_command, "Print list of commands.");
2085 add_com_alias ("q", "quit", class_support, 1);
2086 add_com_alias ("h", "help", class_support, 1);
2089 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
2092 add_show_from_set (c, &showlist);
2093 c->function = set_verbose;
2094 set_verbose (NULL, 0, c);
2096 add_com ("dump-me", class_obscure, dump_me_command,
2097 "Get fatal error; make debugger dump its core.");
2100 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
2101 "Set command line editing.\n\
2102 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2103 Without an argument, command line editing is enabled.", &setlist),
2106 add_prefix_cmd ("history", class_support, set_history,
2107 "Generic command for setting command history parameters.",
2108 &sethistlist, "set history ", 0, &setlist);
2109 add_prefix_cmd ("history", class_support, show_history,
2110 "Generic command for showing command history parameters.",
2111 &showhistlist, "show history ", 0, &showlist);
2114 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
2115 "Set history expansion on command input.\n\
2116 Without an argument, history expansion is enabled.", &sethistlist),
2120 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
2121 "Set saving of the history record on exit.\n\
2122 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2123 Without an argument, saving is enabled.", &sethistlist),
2126 c = add_set_cmd ("size", no_class, var_uinteger, (char *)&history_size,
2127 "Set the size of the command history, \n\
2128 ie. the number of previous commands to keep a record of.", &sethistlist);
2129 add_show_from_set (c, &showhistlist);
2130 c->function = set_history_size_command;
2133 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
2134 "Set the filename in which to record the command history\n\
2135 (the list of previous commands of which a record is kept).", &sethistlist),
2139 (add_set_cmd ("confirm", class_support, var_boolean,
2141 "Set whether to confirm potentially dangerous operations.",
2145 add_prefix_cmd ("info", class_info, info_command,
2146 "Generic command for printing status.",
2147 &infolist, "info ", 0, &cmdlist);
2148 add_com_alias ("i", "info", class_info, 1);
2150 add_prefix_cmd ("show", class_info, show_command,
2151 "Generic command for showing things set with \"set\".",
2152 &showlist, "show ", 0, &cmdlist);
2153 /* Another way to get at the same thing. */
2154 add_info ("set", show_command, "Show all GDB settings.");
2156 add_cmd ("commands", no_class, show_commands, "Status of command editor.",
2159 add_cmd ("version", no_class, show_version,
2160 "Report what version of GDB this is.", &showlist);