1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "call-cmds.h"
28 #include "breakpoint.h"
30 #include "expression.h"
33 #include "terminal.h" /* For job_control. */
38 /* readline include files */
42 /* readline defines this. */
45 #include <sys/types.h>
47 /* What is this for? X_OK? */
55 #include <sys/param.h>
59 /* Prototypes for local functions */
61 static char * symbol_completion_function PARAMS ((char *, int));
63 static void command_loop_marker PARAMS ((int));
65 static void while_command PARAMS ((char *, int));
67 static void if_command PARAMS ((char *, int));
69 static enum command_control_type
70 execute_control_command PARAMS ((struct command_line *));
72 static struct command_line *
73 build_command_line PARAMS ((enum command_control_type, char *));
75 static struct command_line *
76 get_command_line PARAMS ((enum command_control_type, char *));
78 static void realloc_body_list PARAMS ((struct command_line *, int));
80 static enum misc_command_type read_next_line PARAMS ((struct command_line **));
82 static enum command_control_type
83 recurse_read_control_structure PARAMS ((struct command_line *));
85 static void init_main PARAMS ((void));
87 static void init_cmd_lists PARAMS ((void));
89 static void float_handler PARAMS ((int));
91 static void init_signals PARAMS ((void));
93 static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
95 #ifdef TARGET_BYTE_ORDER_SELECTABLE
97 static void set_endian PARAMS ((char *, int));
99 static void set_endian_big PARAMS ((char *, int));
101 static void set_endian_little PARAMS ((char *, int));
103 static void show_endian PARAMS ((char *, int));
107 static void show_history PARAMS ((char *, int));
109 static void set_history PARAMS ((char *, int));
111 static void set_history_size_command PARAMS ((char *, int,
112 struct cmd_list_element *));
114 static void show_commands PARAMS ((char *, int));
116 static void echo_command PARAMS ((char *, int));
118 static void pwd_command PARAMS ((char *, int));
120 static void show_version PARAMS ((char *, int));
122 static void document_command PARAMS ((char *, int));
124 static void define_command PARAMS ((char *, int));
126 static void validate_comname PARAMS ((char *));
128 static void help_command PARAMS ((char *, int));
130 static void show_command PARAMS ((char *, int));
132 static void info_command PARAMS ((char *, int));
134 static void complete_command PARAMS ((char *, int));
136 static void do_nothing PARAMS ((int));
138 static int quit_cover PARAMS ((char *));
140 static void disconnect PARAMS ((int));
142 static void source_cleanup PARAMS ((FILE *));
144 /* If this definition isn't overridden by the header files, assume
145 that isatty and fileno exist on this system. */
147 #define ISATTY(FP) (isatty (fileno (FP)))
150 /* Initialization file name for gdb. This is overridden in some configs. */
152 #ifndef GDBINIT_FILENAME
153 #define GDBINIT_FILENAME ".gdbinit"
155 char gdbinit[] = GDBINIT_FILENAME;
156 int inhibit_gdbinit = 0;
158 /* Disable windows if non-zero */
162 /* Version number of GDB, as a string. */
164 extern char *version;
166 /* Canonical host name as a string. */
168 extern char *host_name;
170 /* Canonical target name as a string. */
172 extern char *target_name;
174 extern char lang_frame_mismatch_warn[]; /* language.c */
176 /* Flag for whether we want all the "from_tty" gubbish printed. */
178 int caution = 1; /* Default is yes, sigh. */
181 * Define all cmd_list_element's
184 /* Chain containing all defined commands. */
186 struct cmd_list_element *cmdlist;
188 /* Chain containing all defined info subcommands. */
190 struct cmd_list_element *infolist;
192 /* Chain containing all defined enable subcommands. */
194 struct cmd_list_element *enablelist;
196 /* Chain containing all defined disable subcommands. */
198 struct cmd_list_element *disablelist;
200 /* Chain containing all defined delete subcommands. */
202 struct cmd_list_element *deletelist;
204 /* Chain containing all defined "enable breakpoint" subcommands. */
206 struct cmd_list_element *enablebreaklist;
208 /* Chain containing all defined set subcommands */
210 struct cmd_list_element *setlist;
212 /* Chain containing all defined unset subcommands */
214 struct cmd_list_element *unsetlist;
216 /* Chain containing all defined show subcommands. */
218 struct cmd_list_element *showlist;
220 #ifdef TARGET_BYTE_ORDER_SELECTABLE
221 /* Chain containing the \"set endian\" commands. */
223 struct cmd_list_element *endianlist;
226 /* Chain containing all defined \"set history\". */
228 struct cmd_list_element *sethistlist;
230 /* Chain containing all defined \"show history\". */
232 struct cmd_list_element *showhistlist;
234 /* Chain containing all defined \"unset history\". */
236 struct cmd_list_element *unsethistlist;
238 /* Chain containing all defined maintenance subcommands. */
241 struct cmd_list_element *maintenancelist;
244 /* Chain containing all defined "maintenance info" subcommands. */
247 struct cmd_list_element *maintenanceinfolist;
250 /* Chain containing all defined "maintenance print" subcommands. */
253 struct cmd_list_element *maintenanceprintlist;
256 struct cmd_list_element *setprintlist;
258 struct cmd_list_element *showprintlist;
260 struct cmd_list_element *setchecklist;
262 struct cmd_list_element *showchecklist;
264 /* stdio stream that command input is being read from. Set to stdin normally.
265 Set by source_command to the file we are sourcing. Set to NULL if we are
266 executing a user-defined command. */
270 /* Current working directory. */
272 char *current_directory;
274 /* The directory name is actually stored here (usually). */
275 char gdb_dirbuf[1024];
277 /* Function to call before reading a command, if nonzero.
278 The function receives two args: an input stream,
279 and a prompt string. */
281 void (*window_hook) PARAMS ((FILE *, char *));
286 /* gdb prints this when reading a command interactively */
289 /* Buffer used for reading command lines, and the size
290 allocated for it so far. */
295 /* Nonzero if the current command is modified by "server ". This
296 affects things like recording into the command history, comamnds
297 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
298 whatever) can issue its own commands and also send along commands
299 from the user, and have the user not notice that the user interface
300 is issuing commands too. */
303 /* Baud rate specified for talking to serial target systems. Default
304 is left as -1, so targets can choose their own defaults. */
305 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
306 or (unsigned int)-1. This is a Bad User Interface. */
310 /* Non-zero tells remote* modules to output debugging info. */
312 int remote_debug = 0;
314 /* Level of control structure. */
315 static int control_level;
317 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
321 #define STOP_SIGNAL SIGTSTP
322 static void stop_sig PARAMS ((int));
326 /* Some System V have job control but not sigsetmask(). */
327 #if !defined (HAVE_SIGSETMASK)
329 #define HAVE_SIGSETMASK 1
331 #define HAVE_SIGSETMASK 0
335 #if 0 == (HAVE_SIGSETMASK)
336 #define sigsetmask(n)
339 /* Hooks for alternate command interfaces. */
341 /* Called after most modules have been initialized, but before taking users
344 void (*init_ui_hook) PARAMS ((void));
346 /* Called instead of command_loop at top level. Can be invoked via
347 return_to_top_level. */
349 void (*command_loop_hook) PARAMS ((void));
351 /* Called instead of fputs for all output. */
353 void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer));
355 /* Called from print_frame_info to list the line we stopped in. */
357 void (*print_frame_info_listing_hook) PARAMS ((struct symtab *s, int line,
358 int stopline, int noerror));
359 /* Replaces most of query. */
361 int (*query_hook) PARAMS (());
363 /* Called from gdb_flush to flush output. */
365 void (*flush_hook) PARAMS ((FILE *stream));
367 /* Called as appropriate to notify the interface of the specified breakpoint
370 void (*create_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
371 void (*delete_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
372 void (*enable_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
373 void (*disable_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
375 /* Called during long calculations to allow GUI to repair window damage, and to
376 check for stop buttons, etc... */
378 void (*interactive_hook) PARAMS ((void));
381 /* Where to go for return_to_top_level (RETURN_ERROR). */
382 jmp_buf error_return;
383 /* Where to go for return_to_top_level (RETURN_QUIT). */
386 /* Return for reason REASON. This generally gets back to the command
387 loop, but can be caught via catch_errors. */
390 return_to_top_level (reason)
391 enum return_reason reason;
396 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
397 I can think of a reason why that is vital, though). */
398 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
400 disable_current_display ();
401 do_cleanups (ALL_CLEANUPS);
403 if (annotation_level > 1)
414 (NORETURN void) longjmp
415 (reason == RETURN_ERROR ? error_return : quit_return, 1);
418 /* Call FUNC with arg ARGS, catching any errors. If there is no
419 error, return the value returned by FUNC. If there is an error,
420 print ERRSTRING, print the specific error message, then return
423 Must not be called with immediate_quit in effect (bad things might
424 happen, say we got a signal in the middle of a memcpy to quit_return).
425 This is an OK restriction; with very few exceptions immediate_quit can
426 be replaced by judicious use of QUIT.
428 MASK specifies what to catch; it is normally set to
429 RETURN_MASK_ALL, if for no other reason than that the code which
430 calls catch_errors might not be set up to deal with a quit which
431 isn't caught. But if the code can deal with it, it generally
432 should be RETURN_MASK_ERROR, unless for some reason it is more
433 useful to abort only the portion of the operation inside the
434 catch_errors. Note that quit should return to the command line
435 fairly quickly, even if some further processing is being done. */
438 catch_errors (func, args, errstring, mask)
439 int (*func) PARAMS ((char *));
448 struct cleanup *saved_cleanup_chain;
449 char *saved_error_pre_print;
451 saved_cleanup_chain = save_cleanups ();
452 saved_error_pre_print = error_pre_print;
454 if (mask & RETURN_MASK_ERROR)
455 memcpy ((char *)saved_error, (char *)error_return, sizeof (jmp_buf));
456 if (mask & RETURN_MASK_QUIT)
457 memcpy (saved_quit, quit_return, sizeof (jmp_buf));
458 error_pre_print = errstring;
460 if (setjmp (tmp_jmp) == 0)
462 if (mask & RETURN_MASK_ERROR)
463 memcpy (error_return, tmp_jmp, sizeof (jmp_buf));
464 if (mask & RETURN_MASK_QUIT)
465 memcpy (quit_return, tmp_jmp, sizeof (jmp_buf));
466 val = (*func) (args);
471 restore_cleanups (saved_cleanup_chain);
473 error_pre_print = saved_error_pre_print;
474 if (mask & RETURN_MASK_ERROR)
475 memcpy (error_return, saved_error, sizeof (jmp_buf));
476 if (mask & RETURN_MASK_QUIT)
477 memcpy (quit_return, saved_quit, sizeof (jmp_buf));
481 /* Handler for SIGHUP. */
487 catch_errors (quit_cover, NULL,
488 "Could not kill the program being debugged", RETURN_MASK_ALL);
489 signal (SIGHUP, SIG_DFL);
490 kill (getpid (), SIGHUP);
493 /* Just a little helper function for disconnect(). */
499 caution = 0; /* Throw caution to the wind -- we're exiting.
500 This prevents asking the user dumb questions. */
501 quit_command((char *)0, 0);
505 /* Line number we are currently in in a file which is being sourced. */
506 static int source_line_number;
508 /* Name of the file we are sourcing. */
509 static char *source_file_name;
511 /* Buffer containing the error_pre_print used by the source stuff.
513 static char *source_error;
514 static int source_error_allocated;
516 /* Something to glom on to the start of error_pre_print if source_file_name
518 static char *source_pre_error;
520 /* Clean up on error during a "source" command (or execution of a
521 user-defined command). */
524 source_cleanup (stream)
527 /* Restore the previous input stream. */
531 /* Read commands from STREAM. */
533 read_command_file (stream)
536 struct cleanup *cleanups;
538 cleanups = make_cleanup (source_cleanup, instream);
541 do_cleanups (cleanups);
544 extern void init_proc ();
549 /* Run the init function of each source file */
551 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
552 current_directory = gdb_dirbuf;
554 init_cmd_lists (); /* This needs to be done first */
555 initialize_targets (); /* Setup target_terminal macros for utils.c */
556 initialize_utils (); /* Make errors and warnings possible */
557 initialize_all_files ();
558 init_main (); /* But that omits this file! Do it now */
563 /* We need a default language for parsing expressions, so simple things like
564 "set width 0" won't fail if no language is explicitly set in a config file
565 or implicitly set by reading an executable during startup. */
566 set_language (language_c);
567 expected_language = current_language; /* don't warn about the change. */
573 /* Allocate, initialize a new command line structure for one of the
574 control commands (if/while). */
576 static struct command_line *
577 build_command_line (type, args)
578 enum command_control_type type;
581 struct command_line *cmd;
583 cmd = (struct command_line *)xmalloc (sizeof (struct command_line));
585 cmd->control_type = type;
589 = (struct command_line **)xmalloc (sizeof (struct command_line *)
591 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
592 cmd->line = savestring (args, strlen (args));
596 /* Build and return a new command structure for the control commands
597 such as "if" and "while". */
599 static struct command_line *
600 get_command_line (type, arg)
601 enum command_control_type type;
604 struct command_line *cmd;
605 struct cleanup *old_chain = NULL;
607 /* Allocate and build a new command line structure. */
608 cmd = build_command_line (type, arg);
610 old_chain = make_cleanup (free_command_lines, &cmd);
612 /* Read in the body of this command. */
613 if (recurse_read_control_structure (cmd) == invalid_control)
615 warning ("error reading in control structure\n");
616 do_cleanups (old_chain);
620 discard_cleanups (old_chain);
624 /* Execute the command in CMD. */
626 static enum command_control_type
627 execute_control_command (cmd)
628 struct command_line *cmd;
630 struct expression *expr;
631 struct command_line *current;
632 struct cleanup *old_chain = 0;
633 struct cleanup *tmp_chain;
636 enum command_control_type ret;
638 switch (cmd->control_type)
641 /* A simple command, execute it and return. */
642 execute_command (cmd->line, 0);
643 return cmd->control_type;
645 case continue_control:
647 /* Return for "continue", and "break" so we can either
648 continue the loop at the top, or break out. */
649 return cmd->control_type;
653 /* Parse the loop control expression for the while statement. */
654 expr = parse_expression (cmd->line);
655 tmp_chain = make_cleanup (free_current_contents, &expr);
657 old_chain = tmp_chain;
659 ret = simple_control;
662 /* Keep iterating so long as the expression is true. */
665 /* Evaluate the expression. */
666 val = evaluate_expression (expr);
668 /* If the value is false, then break out of the loop. */
669 if (!value_true (val))
672 /* Execute the body of the while statement. */
673 current = *cmd->body_list;
676 ret = execute_control_command (current);
678 /* If we got an error, or a "break" command, then stop
680 if (ret == invalid_control || ret == break_control)
686 /* If we got a "continue" command, then restart the loop
688 if (ret == continue_control)
691 /* Get the next statement. */
692 current = current->next;
696 /* Reset RET so that we don't recurse the break all the way down. */
697 if (ret == break_control)
698 ret = simple_control;
705 /* Parse the conditional for the if statement. */
706 expr = parse_expression (cmd->line);
707 old_chain = make_cleanup (free_current_contents, &expr);
710 ret = simple_control;
712 /* Evaluate the conditional. */
713 val = evaluate_expression (expr);
715 /* Choose which arm to take commands from based on the value of the
716 conditional expression. */
717 if (value_true (val))
718 current = *cmd->body_list;
719 else if (cmd->body_count == 2)
720 current = *(cmd->body_list + 1);
722 /* Execute commands in the given arm. */
725 ret = execute_control_command (current);
727 /* If we got an error, get out. */
728 if (ret != simple_control)
731 /* Get the next statement in the body. */
732 current = current->next;
738 warning ("Invalid control type in command structure.");
739 return invalid_control;
743 do_cleanups (old_chain);
748 /* "while" command support. Executes a body of statements while the
749 loop condition is nonzero. */
752 while_command (arg, from_tty)
756 struct command_line *command = NULL;
759 command = get_command_line (while_control, arg);
764 execute_control_command (command);
765 free_command_lines (&command);
768 /* "if" command support. Execute either the true or false arm depending
769 on the value of the if conditional. */
772 if_command (arg, from_tty)
776 struct command_line *command = NULL;
779 command = get_command_line (if_control, arg);
784 execute_control_command (command);
785 free_command_lines (&command);
789 execute_user_command (c, args)
790 struct cmd_list_element *c;
793 register struct command_line *cmdlines;
794 struct cleanup *old_chain;
795 enum command_control_type ret;
798 error ("User-defined commands cannot take arguments.");
800 cmdlines = c->user_commands;
805 /* Set the instream to 0, indicating execution of a
806 user-defined function. */
807 old_chain = make_cleanup (source_cleanup, instream);
808 instream = (FILE *) 0;
811 ret = execute_control_command (cmdlines);
812 if (ret != simple_control && ret != break_control)
814 warning ("Error in control structure.\n");
817 cmdlines = cmdlines->next;
819 do_cleanups (old_chain);
822 /* Execute the line P as a command.
823 Pass FROM_TTY as second argument to the defining function. */
826 execute_command (p, from_tty)
830 register struct cmd_list_element *c;
831 register enum language flang;
832 static int warned = 0;
836 /* This can happen when command_line_input hits end of file. */
840 while (*p == ' ' || *p == '\t') p++;
845 c = lookup_cmd (&p, cmdlist, "", 0, 1);
846 /* Pass null arg rather than an empty one. */
849 /* If this command has been hooked, run the hook first. */
851 execute_user_command (c->hook, (char *)0);
853 if (c->class == class_user)
854 execute_user_command (c, arg);
855 else if (c->type == set_cmd || c->type == show_cmd)
856 do_setshow_command (arg, from_tty & caution, c);
857 else if (c->function.cfunc == NO_FUNCTION)
858 error ("That is not a command, just a help topic.");
860 (*c->function.cfunc) (arg, from_tty & caution);
863 /* Tell the user if the language has changed (except first time). */
864 if (current_language != expected_language)
866 if (language_mode == language_mode_auto) {
867 language_info (1); /* Print what changed. */
872 /* Warn the user if the working language does not match the
873 language of the current frame. Only warn the user if we are
874 actually running the program, i.e. there is a stack. */
875 /* FIXME: This should be cacheing the frame and only running when
876 the frame changes. */
877 if (target_has_stack)
879 flang = get_frame_language ();
881 && flang != language_unknown
882 && flang != current_language->la_language)
884 printf_filtered ("%s\n", lang_frame_mismatch_warn);
892 command_loop_marker (foo)
897 /* Read commands from `instream' and execute them
898 until end of file or error reading instream. */
902 struct cleanup *old_chain;
904 int stdin_is_tty = ISATTY (stdin);
906 while (!feof (instream))
908 if (window_hook && instream == stdin)
909 (*window_hook) (instream, prompt);
912 if (instream == stdin && stdin_is_tty)
913 reinitialize_more_filter ();
914 old_chain = make_cleanup (command_loop_marker, 0);
915 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
916 instream == stdin, "prompt");
919 execute_command (command, instream == stdin);
920 /* Do any commands attached to breakpoint we stopped at. */
921 bpstat_do_actions (&stop_bpstat);
922 do_cleanups (old_chain);
926 /* Commands call this if they do not want to be repeated by null lines. */
934 /* If we aren't reading from standard input, we are saving the last
935 thing read from stdin in line and don't want to delete it. Null lines
936 won't repeat here in any case. */
937 if (instream == stdin)
941 /* Read a line from the stream "instream" without command line editing.
943 It prints PRROMPT once at the start.
944 Action is compatible with "readline", e.g. space for the result is
945 malloc'd and should be freed by the caller.
947 A NULL return means end of file. */
949 gdb_readline (prrompt)
955 int result_size = 80;
959 /* Don't use a _filtered function here. It causes the assumed
960 character position to be off, since the newline we read from
961 the user is not accounted for. */
962 fputs_unfiltered (prrompt, gdb_stdout);
963 /* start-sanitize-mpw */
965 /* Move to a new line so the entered line doesn't have a prompt
966 on the front of it. */
967 fputs_unfiltered ("\n", gdb_stdout);
969 /* end-sanitize-mpw */
970 gdb_flush (gdb_stdout);
973 result = (char *) xmalloc (result_size);
977 /* Read from stdin if we are executing a user defined command.
978 This is the right thing for prompt_for_continue, at least. */
979 c = fgetc (instream ? instream : stdin);
984 /* The last line does not end with a newline. Return it, and
985 if we are called again fgetc will still return EOF and
986 we'll return NULL then. */
995 result[input_index++] = c;
996 while (input_index >= result_size)
999 result = (char *) xrealloc (result, result_size);
1003 result[input_index++] = '\0';
1007 /* Variables which control command line editing and history
1008 substitution. These variables are given default values at the end
1010 static int command_editing_p;
1011 static int history_expansion_p;
1012 static int write_history_p;
1013 static int history_size;
1014 static char *history_filename;
1016 /* readline uses the word breaks for two things:
1017 (1) In figuring out where to point the TEXT parameter to the
1018 rl_completion_entry_function. Since we don't use TEXT for much,
1019 it doesn't matter a lot what the word breaks are for this purpose, but
1020 it does affect how much stuff M-? lists.
1021 (2) If one of the matches contains a word break character, readline
1022 will quote it. That's why we switch between
1023 gdb_completer_word_break_characters and
1024 gdb_completer_command_word_break_characters. I'm not sure when
1025 we need this behavior (perhaps for funky characters in C++ symbols?). */
1027 /* Variables which are necessary for fancy command line editing. */
1028 char *gdb_completer_word_break_characters =
1029 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1031 /* When completing on command names, we remove '-' from the list of
1032 word break characters, since we use it in command names. If the
1033 readline library sees one in any of the current completion strings,
1034 it thinks that the string needs to be quoted and automatically supplies
1036 char *gdb_completer_command_word_break_characters =
1037 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1039 /* Characters that can be used to quote completion strings. Note that we
1040 can't include '"' because the gdb C parser treats such quoted sequences
1042 char *gdb_completer_quote_characters =
1045 /* Functions that are used as part of the fancy command line editing. */
1047 /* This can be used for functions which don't want to complete on symbols
1048 but don't want to complete on anything else either. */
1051 noop_completer (text, prefix)
1058 /* Complete on filenames. */
1060 filename_completer (text, word)
1064 /* From readline. */
1065 extern char *filename_completion_function ();
1066 int subsequent_name;
1068 int return_val_used;
1069 int return_val_alloced;
1071 return_val_used = 0;
1072 /* Small for testing. */
1073 return_val_alloced = 1;
1074 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1076 subsequent_name = 0;
1080 p = filename_completion_function (text, subsequent_name);
1081 if (return_val_used >= return_val_alloced)
1083 return_val_alloced *= 2;
1085 (char **) xrealloc (return_val,
1086 return_val_alloced * sizeof (char *));
1090 return_val[return_val_used++] = p;
1093 /* Like emacs, don't complete on old versions. Especially useful
1094 in the "source" command. */
1095 if (p[strlen (p) - 1] == '~')
1101 /* Return exactly p. */
1102 return_val[return_val_used++] = p;
1103 else if (word > text)
1105 /* Return some portion of p. */
1106 q = xmalloc (strlen (p) + 5);
1107 strcpy (q, p + (word - text));
1108 return_val[return_val_used++] = q;
1113 /* Return some of TEXT plus p. */
1114 q = xmalloc (strlen (p) + (text - word) + 5);
1115 strncpy (q, word, text - word);
1116 q[text - word] = '\0';
1118 return_val[return_val_used++] = q;
1122 subsequent_name = 1;
1125 /* There is no way to do this just long enough to affect quote inserting
1126 without also affecting the next completion. This should be fixed in
1128 /* Insure that readline does the right thing
1129 with respect to inserting quotes. */
1130 rl_completer_word_break_characters = "";
1135 /* Here are some useful test cases for completion. FIXME: These should
1136 be put in the test suite. They should be tested with both M-? and TAB.
1138 "show output-" "radix"
1139 "show output" "-radix"
1140 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1141 "p " ambiguous (all symbols)
1142 "info t foo" no completions
1143 "info t " no completions
1144 "info t" ambiguous ("info target", "info terminal", etc.)
1145 "info ajksdlfk" no completions
1146 "info ajksdlfk " no completions
1148 "info " ambiguous (all info commands)
1149 "p \"a" no completions (string constant)
1150 "p 'a" ambiguous (all symbols starting with a)
1151 "p b-a" ambiguous (all symbols starting with a)
1152 "p b-" ambiguous (all symbols)
1153 "file Make" "file" (word break hard to screw up here)
1154 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1157 /* Generate completions one by one for the completer. Each time we are
1158 called return another potential completion to the caller. The function
1159 is misnamed; it just completes on commands or passes the buck to the
1160 command's completer function; the stuff specific to symbol completion
1161 is in make_symbol_completion_list.
1163 TEXT is readline's idea of the "word" we are looking at; we don't really
1164 like readline's ideas about word breaking so we ignore it.
1166 MATCHES is the number of matches that have currently been collected from
1167 calling this completion function. When zero, then we need to initialize,
1168 otherwise the initialization has already taken place and we can just
1169 return the next potential completion string.
1171 Returns NULL if there are no more completions, else a pointer to a string
1172 which is a possible completion.
1174 RL_LINE_BUFFER is available to be looked at; it contains the entire text
1175 of the line. RL_POINT is the offset in that line of the cursor. You
1176 should pretend that the line ends at RL_POINT. */
1179 symbol_completion_function (text, matches)
1183 static char **list = (char **)NULL; /* Cache of completions */
1184 static int index; /* Next cached completion */
1185 char *output = NULL;
1186 char *tmp_command, *p;
1187 /* Pointer within tmp_command which corresponds to text. */
1189 struct cmd_list_element *c, *result_list;
1193 /* The caller is beginning to accumulate a new set of completions, so
1194 we need to find all of them now, and cache them for returning one at
1195 a time on future calls. */
1199 /* Free the storage used by LIST, but not by the strings inside.
1200 This is because rl_complete_internal () frees the strings. */
1206 /* Choose the default set of word break characters to break completions.
1207 If we later find out that we are doing completions on command strings
1208 (as opposed to strings supplied by the individual command completer
1209 functions, which can be any string) then we will switch to the
1210 special word break set for command strings, which leaves out the
1211 '-' character used in some commands. */
1213 rl_completer_word_break_characters =
1214 gdb_completer_word_break_characters;
1216 /* Decide whether to complete on a list of gdb commands or on symbols. */
1217 tmp_command = (char *) alloca (rl_point + 1);
1220 strncpy (tmp_command, rl_line_buffer, rl_point);
1221 tmp_command[rl_point] = '\0';
1222 /* Since text always contains some number of characters leading up
1223 to rl_point, we can find the equivalent position in tmp_command
1224 by subtracting that many characters from the end of tmp_command. */
1225 word = tmp_command + rl_point - strlen (text);
1229 /* An empty line we want to consider ambiguous; that is, it
1230 could be any command. */
1231 c = (struct cmd_list_element *) -1;
1236 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1239 /* Move p up to the next interesting thing. */
1240 while (*p == ' ' || *p == '\t')
1247 /* It is an unrecognized command. So there are no
1248 possible completions. */
1251 else if (c == (struct cmd_list_element *) -1)
1255 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1256 doesn't advance over that thing itself. Do so now. */
1258 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1260 if (q != tmp_command + rl_point)
1262 /* There is something beyond the ambiguous
1263 command, so there are no possible completions. For
1264 example, "info t " or "info t foo" does not complete
1265 to anything, because "info t" can be "info target" or
1271 /* We're trying to complete on the command which was ambiguous.
1272 This we can deal with. */
1275 list = complete_on_cmdlist (*result_list->prefixlist, p,
1280 list = complete_on_cmdlist (cmdlist, p, word);
1282 /* Insure that readline does the right thing with respect to
1283 inserting quotes. */
1284 rl_completer_word_break_characters =
1285 gdb_completer_command_word_break_characters;
1290 /* We've recognized a full command. */
1292 if (p == tmp_command + rl_point)
1294 /* There is no non-whitespace in the line beyond the command. */
1296 if (p[-1] == ' ' || p[-1] == '\t')
1298 /* The command is followed by whitespace; we need to complete
1299 on whatever comes after command. */
1302 /* It is a prefix command; what comes after it is
1303 a subcommand (e.g. "info "). */
1304 list = complete_on_cmdlist (*c->prefixlist, p, word);
1306 /* Insure that readline does the right thing
1307 with respect to inserting quotes. */
1308 rl_completer_word_break_characters =
1309 gdb_completer_command_word_break_characters;
1313 /* It is a normal command; what comes after it is
1314 completed by the command's completer function. */
1315 list = (*c->completer) (p, word);
1320 /* The command is not followed by whitespace; we need to
1321 complete on the command itself. e.g. "p" which is a
1322 command itself but also can complete to "print", "ptype"
1326 /* Find the command we are completing on. */
1328 while (q > tmp_command)
1330 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1336 list = complete_on_cmdlist (result_list, q, word);
1338 /* Insure that readline does the right thing
1339 with respect to inserting quotes. */
1340 rl_completer_word_break_characters =
1341 gdb_completer_command_word_break_characters;
1346 /* There is non-whitespace beyond the command. */
1348 if (c->prefixlist && !c->allow_unknown)
1350 /* It is an unrecognized subcommand of a prefix command,
1351 e.g. "info adsfkdj". */
1356 /* It is a normal command. */
1357 list = (*c->completer) (p, word);
1363 /* If we found a list of potential completions during initialization then
1364 dole them out one at a time. The vector of completions is NULL
1365 terminated, so after returning the last one, return NULL (and continue
1366 to do so) each time we are called after that, until a new list is
1371 output = list[index];
1379 /* Can't do this because readline hasn't yet checked the word breaks
1380 for figuring out whether to insert a quote. */
1382 /* Make sure the word break characters are set back to normal for the
1383 next time that readline tries to complete something. */
1384 rl_completer_word_break_characters =
1385 gdb_completer_word_break_characters;
1391 /* Skip over a possibly quoted word (as defined by the quote characters
1392 and word break characters the completer uses). Returns pointer to the
1393 location after the "word". */
1399 char quote_char = '\0';
1402 for (scan = str; *scan != '\0'; scan++)
1404 if (quote_char != '\0')
1406 /* Ignore everything until the matching close quote char */
1407 if (*scan == quote_char)
1409 /* Found matching close quote. */
1414 else if (strchr (gdb_completer_quote_characters, *scan))
1416 /* Found start of a quoted string. */
1419 else if (strchr (gdb_completer_word_break_characters, *scan))
1433 #if STOP_SIGNAL == SIGTSTP
1434 signal (SIGTSTP, SIG_DFL);
1436 kill (getpid (), SIGTSTP);
1437 signal (SIGTSTP, stop_sig);
1439 signal (STOP_SIGNAL, stop_sig);
1441 printf_unfiltered ("%s", prompt);
1442 gdb_flush (gdb_stdout);
1444 /* Forget about any previous command -- null line now will do nothing. */
1447 #endif /* STOP_SIGNAL */
1449 /* Initialize signal handlers. */
1459 signal (SIGINT, request_quit);
1461 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1462 passed to the inferior, which we don't want. It would be
1463 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1464 on BSD4.3 systems using vfork, that can affect the
1465 GDB process as well as the inferior (the signal handling tables
1466 might be in memory, shared between the two). Since we establish
1467 a handler for SIGQUIT, when we call exec it will set the signal
1468 to SIG_DFL for us. */
1469 signal (SIGQUIT, do_nothing);
1470 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1471 signal (SIGHUP, disconnect);
1472 signal (SIGFPE, float_handler);
1474 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1475 signal (SIGWINCH, SIGWINCH_HANDLER);
1479 /* Read one line from the command input stream `instream'
1480 into the local static buffer `linebuffer' (whose current length
1482 The buffer is made bigger as necessary.
1483 Returns the address of the start of the line.
1485 NULL is returned for end of file.
1487 *If* the instream == stdin & stdin is a terminal, the line read
1488 is copied into the file line saver (global var char *line,
1489 length linesize) so that it can be duplicated.
1491 This routine either uses fancy command line editing or
1492 simple input as the user has requested. */
1495 command_line_input (prrompt, repeat, annotation_suffix)
1498 char *annotation_suffix;
1500 static char *linebuffer = 0;
1501 static unsigned linelength = 0;
1505 char *local_prompt = prrompt;
1510 if (annotation_level > 1 && instream == stdin)
1512 local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt))
1513 + strlen (annotation_suffix) + 40);
1514 if (prrompt == NULL)
1515 local_prompt[0] = '\0';
1517 strcpy (local_prompt, prrompt);
1518 strcat (local_prompt, "\n\032\032");
1519 strcat (local_prompt, annotation_suffix);
1520 strcat (local_prompt, "\n");
1523 if (linebuffer == 0)
1526 linebuffer = (char *) xmalloc (linelength);
1531 /* Control-C quits instantly if typed while in this loop
1532 since it should not wait until the user types a newline. */
1536 signal (STOP_SIGNAL, stop_sig);
1541 /* Make sure that all output has been output. Some machines may let
1542 you get away with leaving out some of the gdb_flush, but not all. */
1544 gdb_flush (gdb_stdout);
1545 gdb_flush (gdb_stderr);
1547 if (source_file_name != NULL)
1549 ++source_line_number;
1550 sprintf (source_error,
1551 "%s%s:%d: Error in sourced command file:\n",
1554 source_line_number);
1555 error_pre_print = source_error;
1558 if (annotation_level > 1 && instream == stdin)
1560 printf_unfiltered ("\n\032\032pre-");
1561 printf_unfiltered (annotation_suffix);
1562 printf_unfiltered ("\n");
1565 /* Don't use fancy stuff if not talking to stdin. */
1566 if (command_editing_p && instream == stdin
1567 && ISATTY (instream))
1568 rl = readline (local_prompt);
1570 rl = gdb_readline (local_prompt);
1572 if (annotation_level > 1 && instream == stdin)
1574 printf_unfiltered ("\n\032\032post-");
1575 printf_unfiltered (annotation_suffix);
1576 printf_unfiltered ("\n");
1579 if (!rl || rl == (char *) EOF)
1584 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
1586 linelength = strlen(rl) + 1 + (p - linebuffer);
1587 nline = (char *) xrealloc (linebuffer, linelength);
1588 p += nline - linebuffer;
1592 /* Copy line. Don't copy null at end. (Leaves line alone
1593 if this was just a newline) */
1597 free (rl); /* Allocated in readline. */
1599 if (p == linebuffer || *(p - 1) != '\\')
1602 p--; /* Put on top of '\'. */
1603 local_prompt = (char *) 0;
1608 signal (STOP_SIGNAL, SIG_DFL);
1615 #define SERVER_COMMAND_LENGTH 7
1617 (p - linebuffer > SERVER_COMMAND_LENGTH)
1618 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
1621 /* Note that we don't set `line'. Between this and the check in
1622 dont_repeat, this insures that repeating will still do the
1625 return linebuffer + SERVER_COMMAND_LENGTH;
1628 /* Do history expansion if that is wished. */
1629 if (history_expansion_p && instream == stdin
1630 && ISATTY (instream))
1632 char *history_value;
1635 *p = '\0'; /* Insert null now. */
1636 expanded = history_expand (linebuffer, &history_value);
1639 /* Print the changes. */
1640 printf_unfiltered ("%s\n", history_value);
1642 /* If there was an error, call this function again. */
1645 free (history_value);
1646 return command_line_input (prrompt, repeat, annotation_suffix);
1648 if (strlen (history_value) > linelength)
1650 linelength = strlen (history_value) + 1;
1651 linebuffer = (char *) xrealloc (linebuffer, linelength);
1653 strcpy (linebuffer, history_value);
1654 p = linebuffer + strlen(linebuffer);
1655 free (history_value);
1659 /* If we just got an empty line, and that is supposed
1660 to repeat the previous command, return the value in the
1664 if (p == linebuffer)
1667 while (*p1 == ' ' || *p1 == '\t')
1675 /* Add line to history if appropriate. */
1676 if (instream == stdin
1677 && ISATTY (stdin) && *linebuffer)
1678 add_history (linebuffer);
1680 /* Note: lines consisting solely of comments are added to the command
1681 history. This is useful when you type a command, and then
1682 realize you don't want to execute it quite yet. You can comment
1683 out the command and then later fetch it from the value history
1684 and remove the '#'. The kill ring is probably better, but some
1685 people are in the habit of commenting things out. */
1687 while ((c = *p1++) != '\0')
1690 while ((c = *p1++) != '"')
1692 /* Make sure an escaped '"' doesn't make us think the string
1700 while ((c = *p1++) != '\'')
1702 /* Make sure an escaped '\'' doesn't make us think the string
1711 /* Found a comment. */
1717 /* Save into global buffer if appropriate. */
1720 if (linelength > linesize)
1722 line = xrealloc (line, linelength);
1723 linesize = linelength;
1725 strcpy (line, linebuffer);
1733 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
1734 code bodies. This is typically used when we encounter an "else"
1735 clause for an "if" command. */
1738 realloc_body_list (command, new_length)
1739 struct command_line *command;
1743 struct command_line **body_list;
1745 n = command->body_count;
1747 /* Nothing to do? */
1748 if (new_length <= n)
1751 body_list = (struct command_line **)
1752 xmalloc (sizeof (struct command_line *) * new_length);
1754 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
1756 free (command->body_list);
1757 command->body_list = body_list;
1758 command->body_count = new_length;
1761 /* Read one line from the input stream. If the command is an "else" or
1762 "end", return such an indication to the caller. */
1764 static enum misc_command_type
1765 read_next_line (command)
1766 struct command_line **command;
1768 char *p, *p1, *prompt_ptr, control_prompt[256];
1771 if (control_level >= 254)
1772 error ("Control nesting too deep!\n");
1774 /* Set a prompt based on the nesting of the control commands. */
1775 if (instream == stdin)
1777 for (i = 0; i < control_level; i++)
1778 control_prompt[i] = ' ';
1779 control_prompt[i] = '>';
1780 control_prompt[i+1] = '\0';
1781 prompt_ptr = (char *)&control_prompt[0];
1786 p = command_line_input (prompt_ptr, instream == stdin, NULL);
1788 /* Not sure what to do here. */
1792 /* Strip leading and trailing whitespace. */
1793 while (*p == ' ' || *p == '\t')
1796 p1 = p + strlen (p);
1797 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
1800 /* Blanks and comments don't really do anything, but we need to
1801 distinguish them from else, end and other commands which can be
1803 if (p1 == p || p[0] == '#')
1806 /* Is this the end of a simple, while, or if control structure? */
1807 if (p1 - p == 3 && !strncmp (p, "end", 3))
1810 /* Is the else clause of an if control structure? */
1811 if (p1 - p == 4 && !strncmp (p, "else", 4))
1812 return else_command;
1814 /* Check for while, if, break, continue, etc and build a new command
1815 line structure for them. */
1816 if (p1 - p > 5 && !strncmp (p, "while", 5))
1817 *command = build_command_line (while_control, p + 6);
1818 else if (p1 - p > 2 && !strncmp (p, "if", 2))
1819 *command = build_command_line (if_control, p + 3);
1820 else if (p1 - p == 5 && !strncmp (p, "loop_break", 5))
1822 *command = (struct command_line *)
1823 xmalloc (sizeof (struct command_line));
1824 (*command)->next = NULL;
1825 (*command)->line = NULL;
1826 (*command)->control_type = break_control;
1827 (*command)->body_count = 0;
1828 (*command)->body_list = NULL;
1830 else if (p1 - p == 8 && !strncmp (p, "loop_continue", 8))
1832 *command = (struct command_line *)
1833 xmalloc (sizeof (struct command_line));
1834 (*command)->next = NULL;
1835 (*command)->line = NULL;
1836 (*command)->control_type = continue_control;
1837 (*command)->body_count = 0;
1838 (*command)->body_list = NULL;
1842 /* A normal command. */
1843 *command = (struct command_line *)
1844 xmalloc (sizeof (struct command_line));
1845 (*command)->next = NULL;
1846 (*command)->line = savestring (p, p1 - p);
1847 (*command)->control_type = simple_control;
1848 (*command)->body_count = 0;
1849 (*command)->body_list = NULL;
1852 /* Nothing special. */
1856 /* Recursively read in the control structures and create a command_line
1859 The parent_control parameter is the control structure in which the
1860 following commands are nested. */
1862 static enum command_control_type
1863 recurse_read_control_structure (current_cmd)
1864 struct command_line *current_cmd;
1866 int current_body, i;
1867 enum misc_command_type val;
1868 enum command_control_type ret;
1869 struct command_line **body_ptr, *child_tail, *next;
1870 struct cleanup *old_chains, *tmp_chains;
1876 /* Sanity checks. */
1877 if (current_cmd->control_type == simple_control)
1879 error ("Recursed on a simple control type\n");
1880 return invalid_control;
1883 if (current_body > current_cmd->body_count)
1885 error ("Allocated body is smaller than this command type needs\n");
1886 return invalid_control;
1889 /* Read lines from the input stream and build control structures. */
1895 val = read_next_line (&next);
1897 /* Just skip blanks and comments. */
1898 if (val == nop_command)
1901 if (val == end_command)
1903 if (current_cmd->control_type == while_control
1904 || current_cmd->control_type == if_control)
1906 /* Success reading an entire control structure. */
1907 ret = simple_control;
1912 ret = invalid_control;
1917 /* Not the end of a control structure. */
1918 if (val == else_command)
1920 if (current_cmd->control_type == if_control
1921 && current_body == 1)
1923 realloc_body_list (current_cmd, 2);
1930 ret = invalid_control;
1937 child_tail->next = next;
1941 /* We have just read the first line of the child's control
1942 structure. From now on, arrange to throw away the line
1943 we have if we quit or get an error. */
1944 body_ptr = current_cmd->body_list;
1945 for (i = 1; i < current_body; i++)
1950 tmp_chains = make_cleanup (free_command_lines, body_ptr);
1953 old_chains = tmp_chains;
1958 /* If the latest line is another control structure, then recurse
1960 if (next->control_type == while_control
1961 || next->control_type == if_control)
1964 ret = recurse_read_control_structure (next);
1967 if (ret != simple_control)
1973 if (ret == invalid_control && old_chains)
1974 do_cleanups (old_chains);
1975 else if (old_chains)
1976 discard_cleanups (old_chains);
1982 /* Read lines from the input stream
1983 and accumulate them in a chain of struct command_line's
1984 which is then returned. */
1986 struct command_line *
1987 read_command_lines ()
1989 struct command_line *head, *tail, *next;
1990 struct cleanup *old_chain;
1991 enum command_control_type ret;
1992 enum misc_command_type val;
1999 val = read_next_line (&next);
2001 /* Ignore blank lines or comments. */
2002 if (val == nop_command)
2005 if (val == end_command)
2007 ret = simple_control;
2011 if (val != ok_command)
2013 ret = invalid_control;
2017 if (next->control_type == while_control
2018 || next->control_type == if_control)
2021 ret = recurse_read_control_structure (next);
2024 if (ret == invalid_control)
2035 old_chain = make_cleanup (free_command_lines, &head);
2044 if (ret != invalid_control)
2046 discard_cleanups (old_chain);
2050 do_cleanups (old_chain);
2056 /* Free a chain of struct command_line's. */
2059 free_command_lines (lptr)
2060 struct command_line **lptr;
2062 register struct command_line *l = *lptr;
2063 register struct command_line *next;
2064 struct command_line **blist;
2069 if (l->body_count > 0)
2071 blist = l->body_list;
2072 for (i = 0; i < l->body_count; i++, blist++)
2073 free_command_lines (blist);
2082 /* Add an element to the list of info subcommands. */
2085 add_info (name, fun, doc)
2087 void (*fun) PARAMS ((char *, int));
2090 add_cmd (name, no_class, fun, doc, &infolist);
2093 /* Add an alias to the list of info subcommands. */
2096 add_info_alias (name, oldname, abbrev_flag)
2101 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2104 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2105 Therefore, its own definition is called only for "info" with no args. */
2109 info_command (arg, from_tty)
2113 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2114 help_list (infolist, "info ", -1, gdb_stdout);
2117 /* The "complete" command is used by Emacs to implement completion. */
2121 complete_command (arg, from_tty)
2132 rl_line_buffer[0] = '\0';
2137 strcpy (rl_line_buffer, arg);
2138 rl_point = strlen (arg);
2141 for (completion = symbol_completion_function (rl_line_buffer, i = 0);
2143 completion = symbol_completion_function (rl_line_buffer, ++i))
2144 printf_unfiltered ("%s\n", completion);
2147 /* The "show" command with no arguments shows all the settings. */
2151 show_command (arg, from_tty)
2155 cmd_show_list (showlist, from_tty, "");
2158 /* Add an element to the list of commands. */
2161 add_com (name, class, fun, doc)
2163 enum command_class class;
2164 void (*fun) PARAMS ((char *, int));
2167 add_cmd (name, class, fun, doc, &cmdlist);
2170 /* Add an alias or abbreviation command to the list of commands. */
2173 add_com_alias (name, oldname, class, abbrev_flag)
2176 enum command_class class;
2179 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2186 error ("Argument required (%s).", why);
2191 help_command (command, from_tty)
2193 int from_tty; /* Ignored */
2195 help_cmd (command, gdb_stdout);
2199 validate_comname (comname)
2205 error_no_arg ("name of command to define");
2210 if (!isalnum(*p) && *p != '-')
2211 error ("Junk in argument list: \"%s\"", p);
2216 /* This is just a placeholder in the command data structures. */
2218 user_defined_command (ignore, from_tty)
2225 define_command (comname, from_tty)
2229 register struct command_line *cmds;
2230 register struct cmd_list_element *c, *newc, *hookc = 0;
2231 char *tem = comname;
2232 #define HOOK_STRING "hook-"
2235 validate_comname (comname);
2237 /* Look it up, and verify that we got an exact match. */
2238 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
2239 if (c && !STREQ (comname, c->name))
2244 if (c->class == class_user || c->class == class_alias)
2245 tem = "Redefine command \"%s\"? ";
2247 tem = "Really redefine built-in command \"%s\"? ";
2248 if (!query (tem, c->name))
2249 error ("Command \"%s\" not redefined.", c->name);
2252 /* If this new command is a hook, then mark the command which it
2253 is hooking. Note that we allow hooking `help' commands, so that
2254 we can hook the `stop' pseudo-command. */
2256 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
2258 /* Look up cmd it hooks, and verify that we got an exact match. */
2259 tem = comname+HOOK_LEN;
2260 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
2261 if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
2265 warning ("Your new `%s' command does not hook any existing command.",
2267 if (!query ("Proceed? ", (char *)0))
2268 error ("Not confirmed.");
2272 comname = savestring (comname, strlen (comname));
2274 /* If the rest of the commands will be case insensitive, this one
2275 should behave in the same manner. */
2276 for (tem = comname; *tem; tem++)
2277 if (isupper(*tem)) *tem = tolower(*tem);
2281 printf_unfiltered ("Type commands for definition of \"%s\".\n\
2282 End with a line saying just \"end\".\n", comname);
2283 gdb_flush (gdb_stdout);
2287 cmds = read_command_lines ();
2289 if (c && c->class == class_user)
2290 free_command_lines (&c->user_commands);
2292 newc = add_cmd (comname, class_user, user_defined_command,
2293 (c && c->class == class_user)
2294 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
2295 newc->user_commands = cmds;
2297 /* If this new command is a hook, then mark both commands as being
2301 hookc->hook = newc; /* Target gets hooked. */
2302 newc->hookee = hookc; /* We are marked as hooking target cmd. */
2307 document_command (comname, from_tty)
2311 struct command_line *doclines;
2312 register struct cmd_list_element *c;
2313 char *tem = comname;
2315 validate_comname (comname);
2317 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
2319 if (c->class != class_user)
2320 error ("Command \"%s\" is built-in.", comname);
2323 printf_unfiltered ("Type documentation for \"%s\".\n\
2324 End with a line saying just \"end\".\n", comname);
2326 doclines = read_command_lines ();
2328 if (c->doc) free (c->doc);
2331 register struct command_line *cl1;
2332 register int len = 0;
2334 for (cl1 = doclines; cl1; cl1 = cl1->next)
2335 len += strlen (cl1->line) + 1;
2337 c->doc = (char *) xmalloc (len + 1);
2340 for (cl1 = doclines; cl1; cl1 = cl1->next)
2342 strcat (c->doc, cl1->line);
2344 strcat (c->doc, "\n");
2348 free_command_lines (&doclines);
2352 print_gnu_advertisement ()
2354 printf_unfiltered ("\
2355 GDB is free software and you are welcome to distribute copies of it\n\
2356 under certain conditions; type \"show copying\" to see the conditions.\n\
2357 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2362 print_gdb_version (stream)
2365 fprintf_filtered (stream, "\
2366 GDB %s (%s", version, host_name);
2368 if (!STREQ (host_name, target_name))
2369 fprintf_filtered (stream, " --target %s", target_name);
2371 fprintf_filtered (stream, "), ");
2373 fprintf_filtered (stream, "Copyright 1994 Free Software Foundation, Inc.");
2378 show_version (args, from_tty)
2383 print_gnu_advertisement ();
2384 print_gdb_version (gdb_stdout);
2385 printf_filtered ("\n");
2389 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2395 printf_unfiltered ("%s", prompt);
2396 gdb_flush (gdb_stdout);
2400 quit_command (args, from_tty)
2404 if (inferior_pid != 0 && target_has_execution)
2408 if (query ("The program is running. Quit anyway (and detach it)? "))
2409 target_detach (args, from_tty);
2411 error ("Not confirmed.");
2415 if (query ("The program is running. Quit anyway (and kill it)? "))
2418 error ("Not confirmed.");
2421 /* UDI wants this, to kill the TIP. */
2424 /* Save the history information if it is appropriate to do so. */
2425 if (write_history_p && history_filename)
2426 write_history (history_filename);
2431 /* Returns whether GDB is running on a terminal and whether the user
2432 desires that questions be asked of them on that terminal. */
2435 input_from_terminal_p ()
2437 return gdb_has_a_terminal () && (instream == stdin) & caution;
2442 pwd_command (args, from_tty)
2446 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
2447 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
2449 if (!STREQ (gdb_dirbuf, current_directory))
2450 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2451 current_directory, gdb_dirbuf);
2453 printf_unfiltered ("Working directory %s.\n", current_directory);
2457 cd_command (dir, from_tty)
2462 /* Found something other than leading repetitions of "/..". */
2463 int found_real_path;
2466 /* If the new directory is absolute, repeat is a no-op; if relative,
2467 repeat might be useful but is more likely to be a mistake. */
2471 error_no_arg ("new working directory");
2473 dir = tilde_expand (dir);
2474 make_cleanup (free, dir);
2476 if (chdir (dir) < 0)
2477 perror_with_name (dir);
2480 dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
2482 current_directory = dir;
2485 if (current_directory[0] == '/' && current_directory[1] == '\0')
2486 current_directory = concat (current_directory, dir, NULL);
2488 current_directory = concat (current_directory, "/", dir, NULL);
2492 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2494 found_real_path = 0;
2495 for (p = current_directory; *p;)
2497 if (p[0] == '/' && p[1] == '.' && (p[2] == 0 || p[2] == '/'))
2499 else if (p[0] == '/' && p[1] == '.' && p[2] == '.'
2500 && (p[3] == 0 || p[3] == '/'))
2502 if (found_real_path)
2504 /* Search backwards for the directory just before the "/.."
2505 and obliterate it and the "/..". */
2507 while (q != current_directory && q[-1] != '/')
2510 if (q == current_directory)
2511 /* current_directory is
2512 a relative pathname ("can't happen"--leave it alone). */
2516 strcpy (q - 1, p + 3);
2521 /* We are dealing with leading repetitions of "/..", for example
2522 "/../..", which is the Mach super-root. */
2527 found_real_path = 1;
2532 forget_cached_source_info ();
2535 pwd_command ((char *) 0, 1);
2538 struct source_cleanup_lines_args {
2541 char *old_pre_error;
2542 char *old_error_pre_print;
2546 source_cleanup_lines (args)
2549 struct source_cleanup_lines_args *p =
2550 (struct source_cleanup_lines_args *)args;
2551 source_line_number = p->old_line;
2552 source_file_name = p->old_file;
2553 source_pre_error = p->old_pre_error;
2554 error_pre_print = p->old_error_pre_print;
2559 source_command (args, from_tty)
2564 struct cleanup *old_cleanups;
2566 struct source_cleanup_lines_args old_lines;
2571 error ("source command requires pathname of file to source.");
2574 file = tilde_expand (file);
2575 old_cleanups = make_cleanup (free, file);
2577 stream = fopen (file, FOPEN_RT);
2579 perror_with_name (file);
2581 make_cleanup (fclose, stream);
2583 old_lines.old_line = source_line_number;
2584 old_lines.old_file = source_file_name;
2585 old_lines.old_pre_error = source_pre_error;
2586 old_lines.old_error_pre_print = error_pre_print;
2587 make_cleanup (source_cleanup_lines, &old_lines);
2588 source_line_number = 0;
2589 source_file_name = file;
2590 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
2591 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
2592 make_cleanup (free, source_pre_error);
2593 /* This will get set every time we read a line. So it won't stay "" for
2595 error_pre_print = "";
2597 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
2598 if (source_error_allocated < needed_length)
2600 source_error_allocated *= 2;
2601 if (source_error_allocated < needed_length)
2602 source_error_allocated = needed_length;
2603 if (source_error == NULL)
2604 source_error = xmalloc (source_error_allocated);
2606 source_error = xrealloc (source_error, source_error_allocated);
2609 read_command_file (stream);
2611 do_cleanups (old_cleanups);
2616 echo_command (text, from_tty)
2624 while ((c = *p++) != '\0')
2628 /* \ at end of argument is used after spaces
2629 so they won't be lost. */
2633 c = parse_escape (&p);
2635 printf_filtered ("%c", c);
2638 printf_filtered ("%c", c);
2641 /* Force this output to appear now. */
2643 gdb_flush (gdb_stdout);
2647 #ifdef TARGET_BYTE_ORDER_SELECTABLE
2649 /* Functions to manipulate the endianness of the target. */
2651 #ifndef TARGET_BYTE_ORDER_DEFAULT
2652 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
2655 int target_byte_order = TARGET_BYTE_ORDER_DEFAULT;
2657 /* Called if the user enters ``set endian'' without an argument. */
2659 set_endian (args, from_tty)
2663 printf_unfiltered ("\"set endian\" must be followed by \"big\" or \"little\".\n");
2664 show_endian (args, from_tty);
2667 /* Called by ``set endian big''. */
2669 set_endian_big (args, from_tty)
2673 target_byte_order = BIG_ENDIAN;
2676 /* Called by ``set endian little''. */
2678 set_endian_little (args, from_tty)
2682 target_byte_order = LITTLE_ENDIAN;
2685 /* Called by ``show endian''. */
2687 show_endian (args, from_tty)
2691 printf_unfiltered ("The target is assumed to be %s endian.\n",
2692 TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
2695 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
2697 /* Functions to manipulate command line editing control variables. */
2699 /* Number of commands to print in each call to show_commands. */
2700 #define Hist_print 10
2702 show_commands (args, from_tty)
2706 /* Index for history commands. Relative to history_base. */
2709 /* Number of the history entry which we are planning to display next.
2710 Relative to history_base. */
2713 /* The first command in the history which doesn't exist (i.e. one more
2714 than the number of the last command). Relative to history_base. */
2717 extern HIST_ENTRY *history_get PARAMS ((int));
2719 /* Print out some of the commands from the command history. */
2720 /* First determine the length of the history list. */
2721 hist_len = history_size;
2722 for (offset = 0; offset < history_size; offset++)
2724 if (!history_get (history_base + offset))
2733 if (args[0] == '+' && args[1] == '\0')
2734 /* "info editing +" should print from the stored position. */
2737 /* "info editing <exp>" should print around command number <exp>. */
2738 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
2740 /* "show commands" means print the last Hist_print commands. */
2743 num = hist_len - Hist_print;
2749 /* If there are at least Hist_print commands, we want to display the last
2750 Hist_print rather than, say, the last 6. */
2751 if (hist_len - num < Hist_print)
2753 num = hist_len - Hist_print;
2758 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
2760 printf_filtered ("%5d %s\n", history_base + offset,
2761 (history_get (history_base + offset))->line);
2764 /* The next command we want to display is the next one that we haven't
2768 /* If the user repeats this command with return, it should do what
2769 "show commands +" does. This is unnecessary if arg is null,
2770 because "show commands +" is not useful after "show commands". */
2771 if (from_tty && args)
2778 /* Called by do_setshow_command. */
2781 set_history_size_command (args, from_tty, c)
2784 struct cmd_list_element *c;
2786 if (history_size == INT_MAX)
2787 unstifle_history ();
2788 else if (history_size >= 0)
2789 stifle_history (history_size);
2792 history_size = INT_MAX;
2793 error ("History size must be non-negative");
2799 set_history (args, from_tty)
2803 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
2804 help_list (sethistlist, "set history ", -1, gdb_stdout);
2809 show_history (args, from_tty)
2813 cmd_show_list (showhistlist, from_tty, "");
2816 int info_verbose = 0; /* Default verbose msgs off */
2818 /* Called by do_setshow_command. An elaborate joke. */
2821 set_verbose (args, from_tty, c)
2824 struct cmd_list_element *c;
2826 char *cmdname = "verbose";
2827 struct cmd_list_element *showcmd;
2829 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
2833 c->doc = "Set verbose printing of informational messages.";
2834 showcmd->doc = "Show verbose printing of informational messages.";
2838 c->doc = "Set verbosity.";
2839 showcmd->doc = "Show verbosity.";
2844 float_handler (signo)
2847 /* This message is based on ANSI C, section 4.7. Note that integer
2848 divide by zero causes this, so "float" is a misnomer. */
2849 signal (SIGFPE, float_handler);
2850 error ("Erroneous arithmetic operation.");
2862 enablebreaklist = NULL;
2866 #ifdef TARGET_BYTE_ORDER_SELECTABLE
2870 showhistlist = NULL;
2871 unsethistlist = NULL;
2872 #if MAINTENANCE_CMDS
2873 maintenancelist = NULL;
2874 maintenanceinfolist = NULL;
2875 maintenanceprintlist = NULL;
2877 setprintlist = NULL;
2878 showprintlist = NULL;
2879 setchecklist = NULL;
2880 showchecklist = NULL;
2883 /* Init the history buffer. Note that we are called after the init file(s)
2884 * have been read so that the user can change the history file via his
2885 * .gdbinit file (for instance). The GDBHISTFILE environment variable
2886 * overrides all of this.
2894 tmpenv = getenv ("HISTSIZE");
2896 history_size = atoi (tmpenv);
2897 else if (!history_size)
2900 stifle_history (history_size);
2902 tmpenv = getenv ("GDBHISTFILE");
2904 history_filename = savestring (tmpenv, strlen(tmpenv));
2905 else if (!history_filename) {
2906 /* We include the current directory so that if the user changes
2907 directories the file written will be the same as the one
2909 history_filename = concat (current_directory, "/.gdb_history", NULL);
2911 read_history (history_filename);
2917 struct cmd_list_element *c;
2919 #ifdef TARGET_BYTE_ORDER_SELECTABLE
2921 add_prefix_cmd ("endian", class_support, set_endian,
2922 "Set endianness of target.",
2923 &endianlist, "set endian ", 0, &setlist);
2924 add_cmd ("big", class_support, set_endian_big,
2925 "Set target as being big endian.", &endianlist);
2926 add_cmd ("little", class_support, set_endian_little,
2927 "Set target as being little endian.", &endianlist);
2928 add_cmd ("endian", class_support, show_endian,
2929 "Show endianness of target.", &showlist);
2931 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
2933 #ifdef DEFAULT_PROMPT
2934 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
2936 prompt = savestring ("(gdb) ", 6);
2939 /* Set the important stuff up for command editing. */
2940 command_editing_p = 1;
2941 history_expansion_p = 0;
2942 write_history_p = 0;
2944 /* Setup important stuff for command line editing. */
2945 rl_completion_entry_function = (int (*)()) symbol_completion_function;
2946 rl_completer_word_break_characters = gdb_completer_word_break_characters;
2947 rl_completer_quote_characters = gdb_completer_quote_characters;
2948 rl_readline_name = "gdb";
2950 /* Define the classes of commands.
2951 They will appear in the help list in the reverse of this order. */
2953 add_cmd ("internals", class_maintenance, NO_FUNCTION,
2954 "Maintenance commands.\n\
2955 Some gdb commands are provided just for use by gdb maintainers.\n\
2956 These commands are subject to frequent change, and may not be as\n\
2957 well documented as user commands.",
2959 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
2960 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
2961 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
2962 The commands in this class are those defined by the user.\n\
2963 Use the \"define\" command to define a command.", &cmdlist);
2964 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
2965 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
2966 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
2967 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
2968 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
2969 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
2970 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2971 counting from zero for the innermost (currently executing) frame.\n\n\
2972 At any time gdb identifies one frame as the \"selected\" frame.\n\
2973 Variable lookups are done with respect to the selected frame.\n\
2974 When the program being debugged stops, gdb selects the innermost frame.\n\
2975 The commands below can be used to select other frames by number or address.",
2977 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
2979 add_com ("pwd", class_files, pwd_command,
2980 "Print working directory. This is used for your program as well.");
2981 c = add_cmd ("cd", class_files, cd_command,
2982 "Set working directory to DIR for debugger and program being debugged.\n\
2983 The change does not take effect for the program being debugged\n\
2984 until the next time it is started.", &cmdlist);
2985 c->completer = filename_completer;
2988 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
2993 add_com ("echo", class_support, echo_command,
2994 "Print a constant string. Give string as argument.\n\
2995 C escape sequences may be used in the argument.\n\
2996 No newline is added at the end of the argument;\n\
2997 use \"\\n\" if you want a newline to be printed.\n\
2998 Since leading and trailing whitespace are ignored in command arguments,\n\
2999 if you want to print some you must use \"\\\" before leading whitespace\n\
3000 to be printed or after trailing whitespace.");
3001 add_com ("document", class_support, document_command,
3002 "Document a user-defined command.\n\
3003 Give command name as argument. Give documentation on following lines.\n\
3004 End with a line of just \"end\".");
3005 add_com ("define", class_support, define_command,
3006 "Define a new command name. Command name is argument.\n\
3007 Definition appears on following lines, one command per line.\n\
3008 End with a line of just \"end\".\n\
3009 Use the \"document\" command to give documentation for the new command.\n\
3010 Commands defined in this way do not take arguments.");
3013 c = add_cmd ("source", class_support, source_command,
3014 "Read commands from a file named FILE.\n\
3015 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
3016 when gdb is started.", &cmdlist);
3018 /* Punt file name, we can't help it easily. */
3019 c = add_cmd ("source", class_support, source_command,
3020 "Read commands from a file named FILE.\n\
3021 Note that the file \".gdbinit\" is read automatically in this way\n\
3022 when gdb is started.", &cmdlist);
3024 c->completer = filename_completer;
3026 add_com ("quit", class_support, quit_command, "Exit gdb.");
3027 add_com ("help", class_support, help_command, "Print list of commands.");
3028 add_com_alias ("q", "quit", class_support, 1);
3029 add_com_alias ("h", "help", class_support, 1);
3032 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
3035 add_show_from_set (c, &showlist);
3036 c->function.sfunc = set_verbose;
3037 set_verbose (NULL, 0, c);
3040 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
3041 "Set editing of command lines as they are typed.\n\
3042 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
3043 Without an argument, command line editing is enabled. To edit, use\n\
3044 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
3047 add_prefix_cmd ("history", class_support, set_history,
3048 "Generic command for setting command history parameters.",
3049 &sethistlist, "set history ", 0, &setlist);
3050 add_prefix_cmd ("history", class_support, show_history,
3051 "Generic command for showing command history parameters.",
3052 &showhistlist, "show history ", 0, &showlist);
3055 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
3056 "Set history expansion on command input.\n\
3057 Without an argument, history expansion is enabled.", &sethistlist),
3061 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
3062 "Set saving of the history record on exit.\n\
3063 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
3064 Without an argument, saving is enabled.", &sethistlist),
3067 c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
3068 "Set the size of the command history, \n\
3069 ie. the number of previous commands to keep a record of.", &sethistlist);
3070 add_show_from_set (c, &showhistlist);
3071 c->function.sfunc = set_history_size_command;
3074 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
3075 "Set the filename in which to record the command history\n\
3076 (the list of previous commands of which a record is kept).", &sethistlist),
3080 (add_set_cmd ("confirm", class_support, var_boolean,
3082 "Set whether to confirm potentially dangerous operations.",
3086 add_prefix_cmd ("info", class_info, info_command,
3087 "Generic command for showing things about the program being debugged.",
3088 &infolist, "info ", 0, &cmdlist);
3089 add_com_alias ("i", "info", class_info, 1);
3091 add_com ("complete", class_obscure, complete_command,
3092 "List the completions for the rest of the line as a command.");
3094 add_prefix_cmd ("show", class_info, show_command,
3095 "Generic command for showing things about the debugger.",
3096 &showlist, "show ", 0, &cmdlist);
3097 /* Another way to get at the same thing. */
3098 add_info ("set", show_command, "Show all GDB settings.");
3100 add_cmd ("commands", no_class, show_commands,
3101 "Show the the history of commands you typed.\n\
3102 You can supply a command number to start with, or a `+' to start after\n\
3103 the previous command number shown.",
3106 add_cmd ("version", no_class, show_version,
3107 "Show what version of GDB this is.", &showlist);
3109 add_com ("while", class_support, while_command,
3110 "Execute nested commands WHILE the conditional expression is non zero.\n\
3111 The conditional expression must follow the word `while' and must in turn be\
3112 followed by a new line. The nested commands must be entered one per line,\
3113 and should be terminated by the word `end'.");
3115 add_com ("if", class_support, if_command,
3116 "Execute nested commands once IF the conditional expression is non zero.\n\
3117 The conditional expression must follow the word `if' and must in turn be\
3118 followed by a new line. The nested commands must be entered one per line,\
3119 and should be terminated by the word 'else' or `end'. If an else clause\
3120 is used, the same rules apply to its nested commands as to the first ones.");
3122 /* If target is open when baud changes, it doesn't take effect until the
3123 next open (I think, not sure). */
3124 add_show_from_set (add_set_cmd ("remotebaud", no_class,
3125 var_zinteger, (char *)&baud_rate,
3126 "Set baud rate for remote serial I/O.\n\
3127 This value is used to set the speed of the serial port when debugging\n\
3128 using remote targets.", &setlist),
3132 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug,
3133 "Set debugging of remote protocol.\n\
3134 When enabled, each packet sent or received with the remote target\n\
3135 is displayed.", &setlist),