1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 1998
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 */
39 #include "readline/readline.h"
42 /* readline defines this. */
45 #include <sys/types.h>
50 #include "gdb_string.h"
54 extern void initialize_utils PARAMS ((void));
56 /* Prototypes for local functions */
58 static void dont_repeat_command PARAMS ((char *, int));
60 static void source_cleanup_lines PARAMS ((PTR));
62 static void user_defined_command PARAMS ((char *, int));
64 static void init_signals PARAMS ((void));
67 static void stop_sig PARAMS ((int));
70 static char * line_completion_function PARAMS ((char *, int, char *, int));
72 static char * readline_line_completion_function PARAMS ((char *, int));
74 static void command_loop_marker PARAMS ((int));
76 static void while_command PARAMS ((char *, int));
78 static void if_command PARAMS ((char *, int));
80 static struct command_line *
81 build_command_line PARAMS ((enum command_control_type, char *));
83 static struct command_line *
84 get_command_line PARAMS ((enum command_control_type, char *));
86 static void realloc_body_list PARAMS ((struct command_line *, int));
88 static enum misc_command_type read_next_line PARAMS ((struct command_line **));
90 static enum command_control_type
91 recurse_read_control_structure PARAMS ((struct command_line *));
93 static struct cleanup * setup_user_args PARAMS ((char *));
95 static char * locate_arg PARAMS ((char *));
97 static char * insert_args PARAMS ((char *));
99 static void arg_cleanup PARAMS ((void));
101 static void init_main PARAMS ((void));
103 static void init_cmd_lists PARAMS ((void));
105 static void float_handler PARAMS ((int));
107 static void init_signals PARAMS ((void));
109 static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
111 static void show_history PARAMS ((char *, int));
113 static void set_history PARAMS ((char *, int));
115 static void set_history_size_command PARAMS ((char *, int,
116 struct cmd_list_element *));
118 static void show_commands PARAMS ((char *, int));
120 static void echo_command PARAMS ((char *, int));
122 static void pwd_command PARAMS ((char *, int));
124 static void show_version PARAMS ((char *, int));
126 static void document_command PARAMS ((char *, int));
128 static void define_command PARAMS ((char *, int));
130 static void validate_comname PARAMS ((char *));
132 static void help_command PARAMS ((char *, int));
134 static void show_command PARAMS ((char *, int));
136 static void info_command PARAMS ((char *, int));
138 static void complete_command PARAMS ((char *, int));
140 static void do_nothing PARAMS ((int));
143 static int quit_cover PARAMS ((char *));
145 static void disconnect PARAMS ((int));
148 static void source_cleanup PARAMS ((FILE *));
150 /* If this definition isn't overridden by the header files, assume
151 that isatty and fileno exist on this system. */
153 #define ISATTY(FP) (isatty (fileno (FP)))
156 /* Initialization file name for gdb. This is overridden in some configs. */
158 #ifndef GDBINIT_FILENAME
159 #define GDBINIT_FILENAME ".gdbinit"
161 char gdbinit[] = GDBINIT_FILENAME;
163 int inhibit_gdbinit = 0;
165 /* If nonzero, and GDB has been configured to be able to use windows,
166 attempt to open them upon startup. */
170 /* Version number of GDB, as a string. */
172 extern char *version;
174 /* Canonical host name as a string. */
176 extern char *host_name;
178 /* Canonical target name as a string. */
180 extern char *target_name;
182 extern char lang_frame_mismatch_warn[]; /* language.c */
184 /* Flag for whether we want all the "from_tty" gubbish printed. */
186 int caution = 1; /* Default is yes, sigh. */
188 /* Define all cmd_list_elements. */
190 /* Chain containing all defined commands. */
192 struct cmd_list_element *cmdlist;
194 /* Chain containing all defined info subcommands. */
196 struct cmd_list_element *infolist;
198 /* Chain containing all defined enable subcommands. */
200 struct cmd_list_element *enablelist;
202 /* Chain containing all defined disable subcommands. */
204 struct cmd_list_element *disablelist;
206 /* Chain containing all defined delete subcommands. */
208 struct cmd_list_element *deletelist;
210 /* Chain containing all defined "enable breakpoint" subcommands. */
212 struct cmd_list_element *enablebreaklist;
214 /* Chain containing all defined set subcommands */
216 struct cmd_list_element *setlist;
218 /* Chain containing all defined unset subcommands */
220 struct cmd_list_element *unsetlist;
222 /* Chain containing all defined show subcommands. */
224 struct cmd_list_element *showlist;
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 or interacting via a GUI. */
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 /* Timeout limit for response from target. */
312 int remote_timeout = 20; /* Set default to 20 */
314 /* Non-zero tells remote* modules to output debugging info. */
316 int remote_debug = 0;
318 /* Level of control structure. */
319 static int control_level;
321 /* Structure for arguments to user defined functions. */
322 #define MAXUSERARGS 10
325 struct user_args *next;
334 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
338 #define STOP_SIGNAL SIGTSTP
339 static void stop_sig PARAMS ((int));
343 /* Some System V have job control but not sigsetmask(). */
344 #if !defined (HAVE_SIGSETMASK)
346 #define HAVE_SIGSETMASK 1
348 #define HAVE_SIGSETMASK 0
352 #if 0 == (HAVE_SIGSETMASK)
353 #define sigsetmask(n)
356 /* Hooks for alternate command interfaces. */
358 /* Called after most modules have been initialized, but before taking users
361 void (*init_ui_hook) PARAMS ((char *argv0));
363 void (*ui_loop_hook) PARAMS ((int));
366 /* Called instead of command_loop at top level. Can be invoked via
367 return_to_top_level. */
369 void (*command_loop_hook) PARAMS ((void));
372 /* Called instead of fputs for all output. */
374 void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer, FILE *stream));
376 /* Called when the target says something to the host, which may
377 want to appear in a different window. */
379 void (*target_output_hook) PARAMS ((char *));
381 /* Called from print_frame_info to list the line we stopped in. */
383 void (*print_frame_info_listing_hook) PARAMS ((struct symtab *s, int line,
384 int stopline, int noerror));
385 /* Replaces most of query. */
387 int (*query_hook) PARAMS ((const char *, va_list));
389 /* Replaces most of warning. */
391 void (*warning_hook) PARAMS ((const char *, va_list));
393 /* Called from gdb_flush to flush output. */
395 void (*flush_hook) PARAMS ((FILE *stream));
397 /* These three functions support getting lines of text from the user. They
398 are used in sequence. First readline_begin_hook is called with a text
399 string that might be (for example) a message for the user to type in a
400 sequence of commands to be executed at a breakpoint. If this function
401 calls back to a GUI, it might take this opportunity to pop up a text
402 interaction window with this message. Next, readline_hook is called
403 with a prompt that is emitted prior to collecting the user input.
404 It can be called multiple times. Finally, readline_end_hook is called
405 to notify the GUI that we are done with the interaction window and it
408 void (*readline_begin_hook) PARAMS ((char *, ...));
409 char * (*readline_hook) PARAMS ((char *));
410 void (*readline_end_hook) PARAMS ((void));
412 /* Called as appropriate to notify the interface of the specified breakpoint
415 void (*create_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
416 void (*delete_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
417 void (*modify_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
419 /* Called during long calculations to allow GUI to repair window damage, and to
420 check for stop buttons, etc... */
422 void (*interactive_hook) PARAMS ((void));
424 /* Called when the registers have changed, as a hint to a GUI
425 to minimize window update. */
427 void (*registers_changed_hook) PARAMS ((void));
429 /* tell the GUI someone changed the PC */
430 void (*pc_changed_hook) PARAMS ((void));
432 /* Called when going to wait for the target. Usually allows the GUI to run
433 while waiting for target events. */
435 int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus *status));
437 /* Used by UI as a wrapper around command execution. May do various things
438 like enabling/disabling buttons, etc... */
440 void (*call_command_hook) PARAMS ((struct cmd_list_element *c, char *cmd,
443 /* Called when the current thread changes. Argument is thread id. */
445 void (*context_hook) PARAMS ((int id));
447 /* Takes control from error (). Typically used to prevent longjmps out of the
448 middle of the GUI. Usually used in conjunction with a catch routine. */
450 NORETURN void (*error_hook) PARAMS ((void)) ATTR_NORETURN;
453 /* Where to go for return_to_top_level (RETURN_ERROR). */
454 SIGJMP_BUF error_return;
455 /* Where to go for return_to_top_level (RETURN_QUIT). */
456 SIGJMP_BUF quit_return;
458 /* Return for reason REASON. This generally gets back to the command
459 loop, but can be caught via catch_errors. */
462 return_to_top_level (reason)
463 enum return_reason reason;
468 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
469 I can think of a reason why that is vital, though). */
470 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
472 disable_current_display ();
473 do_cleanups (ALL_CLEANUPS);
475 if (annotation_level > 1)
486 (NORETURN void) SIGLONGJMP
487 (reason == RETURN_ERROR ? error_return : quit_return, 1);
490 /* Call FUNC with arg ARGS, catching any errors. If there is no
491 error, return the value returned by FUNC. If there is an error,
492 print ERRSTRING, print the specific error message, then return
495 Must not be called with immediate_quit in effect (bad things might
496 happen, say we got a signal in the middle of a memcpy to quit_return).
497 This is an OK restriction; with very few exceptions immediate_quit can
498 be replaced by judicious use of QUIT.
500 MASK specifies what to catch; it is normally set to
501 RETURN_MASK_ALL, if for no other reason than that the code which
502 calls catch_errors might not be set up to deal with a quit which
503 isn't caught. But if the code can deal with it, it generally
504 should be RETURN_MASK_ERROR, unless for some reason it is more
505 useful to abort only the portion of the operation inside the
506 catch_errors. Note that quit should return to the command line
507 fairly quickly, even if some further processing is being done. */
510 catch_errors (func, args, errstring, mask)
511 int (*func) PARAMS ((char *));
516 SIGJMP_BUF saved_error;
517 SIGJMP_BUF saved_quit;
520 struct cleanup *saved_cleanup_chain;
521 char *saved_error_pre_print;
522 char *saved_quit_pre_print;
524 saved_cleanup_chain = save_cleanups ();
525 saved_error_pre_print = error_pre_print;
526 saved_quit_pre_print = quit_pre_print;
528 if (mask & RETURN_MASK_ERROR)
530 memcpy ((char *)saved_error, (char *)error_return, sizeof (SIGJMP_BUF));
531 error_pre_print = errstring;
533 if (mask & RETURN_MASK_QUIT)
535 memcpy (saved_quit, quit_return, sizeof (SIGJMP_BUF));
536 quit_pre_print = errstring;
539 if (SIGSETJMP (tmp_jmp) == 0)
541 if (mask & RETURN_MASK_ERROR)
542 memcpy (error_return, tmp_jmp, sizeof (SIGJMP_BUF));
543 if (mask & RETURN_MASK_QUIT)
544 memcpy (quit_return, tmp_jmp, sizeof (SIGJMP_BUF));
545 val = (*func) (args);
550 restore_cleanups (saved_cleanup_chain);
552 if (mask & RETURN_MASK_ERROR)
554 memcpy (error_return, saved_error, sizeof (SIGJMP_BUF));
555 error_pre_print = saved_error_pre_print;
557 if (mask & RETURN_MASK_QUIT)
559 memcpy (quit_return, saved_quit, sizeof (SIGJMP_BUF));
560 quit_pre_print = saved_quit_pre_print;
565 /* Handler for SIGHUP. */
572 catch_errors (quit_cover, NULL,
573 "Could not kill the program being debugged", RETURN_MASK_ALL);
574 signal (SIGHUP, SIG_DFL);
575 kill (getpid (), SIGHUP);
578 /* Just a little helper function for disconnect(). */
584 caution = 0; /* Throw caution to the wind -- we're exiting.
585 This prevents asking the user dumb questions. */
586 quit_command((char *)0, 0);
589 #endif /* defined SIGHUP */
591 /* Line number we are currently in in a file which is being sourced. */
592 static int source_line_number;
594 /* Name of the file we are sourcing. */
595 static char *source_file_name;
597 /* Buffer containing the error_pre_print used by the source stuff.
599 static char *source_error;
600 static int source_error_allocated;
602 /* Something to glom on to the start of error_pre_print if source_file_name
604 static char *source_pre_error;
606 /* Clean up on error during a "source" command (or execution of a
607 user-defined command). */
610 source_cleanup (stream)
613 /* Restore the previous input stream. */
617 /* Read commands from STREAM. */
619 read_command_file (stream)
622 struct cleanup *cleanups;
624 cleanups = make_cleanup ((make_cleanup_func) source_cleanup, instream);
627 do_cleanups (cleanups);
630 extern void init_proc PARAMS ((void));
632 void (*pre_init_ui_hook) PARAMS ((void));
638 if (pre_init_ui_hook)
641 /* Run the init function of each source file */
643 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
644 current_directory = gdb_dirbuf;
646 init_cmd_lists (); /* This needs to be done first */
647 initialize_targets (); /* Setup target_terminal macros for utils.c */
648 initialize_utils (); /* Make errors and warnings possible */
649 initialize_all_files ();
650 init_main (); /* But that omits this file! Do it now */
655 /* We need a default language for parsing expressions, so simple things like
656 "set width 0" won't fail if no language is explicitly set in a config file
657 or implicitly set by reading an executable during startup. */
658 set_language (language_c);
659 expected_language = current_language; /* don't warn about the change. */
662 init_ui_hook (argv0);
665 /* Allocate, initialize a new command line structure for one of the
666 control commands (if/while). */
668 static struct command_line *
669 build_command_line (type, args)
670 enum command_control_type type;
673 struct command_line *cmd;
676 error ("if/while commands require arguments.\n");
678 cmd = (struct command_line *)xmalloc (sizeof (struct command_line));
680 cmd->control_type = type;
684 = (struct command_line **)xmalloc (sizeof (struct command_line *)
686 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
687 cmd->line = savestring (args, strlen (args));
691 /* Build and return a new command structure for the control commands
692 such as "if" and "while". */
694 static struct command_line *
695 get_command_line (type, arg)
696 enum command_control_type type;
699 struct command_line *cmd;
700 struct cleanup *old_chain = NULL;
702 /* Allocate and build a new command line structure. */
703 cmd = build_command_line (type, arg);
705 old_chain = make_cleanup ((make_cleanup_func) free_command_lines, &cmd);
707 /* Read in the body of this command. */
708 if (recurse_read_control_structure (cmd) == invalid_control)
710 warning ("error reading in control structure\n");
711 do_cleanups (old_chain);
715 discard_cleanups (old_chain);
719 /* Recursively print a command (including full control structures). */
721 print_command_line (cmd, depth)
722 struct command_line *cmd;
729 for (i = 0; i < depth; i++)
730 fputs_filtered (" ", gdb_stdout);
733 /* A simple command, print it and return. */
734 if (cmd->control_type == simple_control)
736 fputs_filtered (cmd->line, gdb_stdout);
737 fputs_filtered ("\n", gdb_stdout);
741 /* loop_continue to jump to the start of a while loop, print it
743 if (cmd->control_type == continue_control)
745 fputs_filtered ("loop_continue\n", gdb_stdout);
749 /* loop_break to break out of a while loop, print it and return. */
750 if (cmd->control_type == break_control)
752 fputs_filtered ("loop_break\n", gdb_stdout);
756 /* A while command. Recursively print its subcommands before returning. */
757 if (cmd->control_type == while_control)
759 struct command_line *list;
760 fputs_filtered ("while ", gdb_stdout);
761 fputs_filtered (cmd->line, gdb_stdout);
762 fputs_filtered ("\n", gdb_stdout);
763 list = *cmd->body_list;
766 print_command_line (list, depth + 1);
771 /* An if command. Recursively print both arms before returning. */
772 if (cmd->control_type == if_control)
774 fputs_filtered ("if ", gdb_stdout);
775 fputs_filtered (cmd->line, gdb_stdout);
776 fputs_filtered ("\n", gdb_stdout);
778 print_command_line (cmd->body_list[0], depth + 1);
780 /* Show the false arm if it exists. */
781 if (cmd->body_count == 2)
785 for (i = 0; i < depth; i++)
786 fputs_filtered (" ", gdb_stdout);
788 fputs_filtered ("else\n", gdb_stdout);
789 print_command_line (cmd->body_list[1], depth + 1);
793 for (i = 0; i < depth; i++)
794 fputs_filtered (" ", gdb_stdout);
796 fputs_filtered ("end\n", gdb_stdout);
800 /* Execute the command in CMD. */
802 enum command_control_type
803 execute_control_command (cmd)
804 struct command_line *cmd;
806 struct expression *expr;
807 struct command_line *current;
808 struct cleanup *old_chain = 0;
812 enum command_control_type ret;
815 switch (cmd->control_type)
818 /* A simple command, execute it and return. */
819 new_line = insert_args (cmd->line);
821 return invalid_control;
822 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
824 execute_command (new_line, 0);
825 ret = cmd->control_type;
828 case continue_control:
830 /* Return for "continue", and "break" so we can either
831 continue the loop at the top, or break out. */
832 ret = cmd->control_type;
837 /* Parse the loop control expression for the while statement. */
838 new_line = insert_args (cmd->line);
840 return invalid_control;
841 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
843 expr = parse_expression (new_line);
844 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
846 ret = simple_control;
849 /* Keep iterating so long as the expression is true. */
856 /* Evaluate the expression. */
857 val_mark = value_mark ();
858 val = evaluate_expression (expr);
859 cond_result = value_true (val);
860 value_free_to_mark (val_mark);
862 /* If the value is false, then break out of the loop. */
866 /* Execute the body of the while statement. */
867 current = *cmd->body_list;
870 ret = execute_control_command (current);
872 /* If we got an error, or a "break" command, then stop
874 if (ret == invalid_control || ret == break_control)
880 /* If we got a "continue" command, then restart the loop
882 if (ret == continue_control)
885 /* Get the next statement. */
886 current = current->next;
890 /* Reset RET so that we don't recurse the break all the way down. */
891 if (ret == break_control)
892 ret = simple_control;
899 new_line = insert_args (cmd->line);
901 return invalid_control;
902 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
904 /* Parse the conditional for the if statement. */
905 expr = parse_expression (new_line);
906 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
909 ret = simple_control;
911 /* Evaluate the conditional. */
912 val_mark = value_mark ();
913 val = evaluate_expression (expr);
915 /* Choose which arm to take commands from based on the value of the
916 conditional expression. */
917 if (value_true (val))
918 current = *cmd->body_list;
919 else if (cmd->body_count == 2)
920 current = *(cmd->body_list + 1);
921 value_free_to_mark (val_mark);
923 /* Execute commands in the given arm. */
926 ret = execute_control_command (current);
928 /* If we got an error, get out. */
929 if (ret != simple_control)
932 /* Get the next statement in the body. */
933 current = current->next;
940 warning ("Invalid control type in command structure.");
941 return invalid_control;
945 do_cleanups (old_chain);
950 /* "while" command support. Executes a body of statements while the
951 loop condition is nonzero. */
954 while_command (arg, from_tty)
958 struct command_line *command = NULL;
961 command = get_command_line (while_control, arg);
966 execute_control_command (command);
967 free_command_lines (&command);
970 /* "if" command support. Execute either the true or false arm depending
971 on the value of the if conditional. */
974 if_command (arg, from_tty)
978 struct command_line *command = NULL;
981 command = get_command_line (if_control, arg);
986 execute_control_command (command);
987 free_command_lines (&command);
994 struct user_args *oargs = user_args;
996 fatal ("Internal error, arg_cleanup called with no user args.\n");
998 user_args = user_args->next;
1002 /* Bind the incomming arguments for a user defined command to
1003 $arg0, $arg1 ... $argMAXUSERARGS. */
1005 static struct cleanup *
1009 struct user_args *args;
1010 struct cleanup *old_chain;
1011 unsigned int arg_count = 0;
1013 args = (struct user_args *)xmalloc (sizeof (struct user_args));
1014 memset (args, 0, sizeof (struct user_args));
1016 args->next = user_args;
1019 old_chain = make_cleanup ((make_cleanup_func) arg_cleanup, 0);
1031 if (arg_count >= MAXUSERARGS)
1033 error ("user defined function may only have %d arguments.\n",
1038 /* Strip whitespace. */
1039 while (*p == ' ' || *p == '\t')
1042 /* P now points to an argument. */
1044 user_args->a[arg_count].arg = p;
1046 /* Get to the end of this argument. */
1049 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
1055 else if (*p == '\\')
1078 user_args->a[arg_count].len = p - start_arg;
1085 /* Given character string P, return a point to the first argument ($arg),
1086 or NULL if P contains no arguments. */
1092 while ((p = strchr (p, '$')))
1094 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
1101 /* Insert the user defined arguments stored in user_arg into the $arg
1102 arguments found in line, with the updated copy being placed into nline. */
1108 char *p, *save_line, *new_line;
1111 /* First we need to know how much memory to allocate for the new line. */
1114 while ((p = locate_arg (line)))
1119 if (i >= user_args->count)
1121 error ("Missing argument %d in user function.\n", i);
1124 len += user_args->a[i].len;
1128 /* Don't forget the tail. */
1129 len += strlen (line);
1131 /* Allocate space for the new line and fill it in. */
1132 new_line = (char *)xmalloc (len + 1);
1133 if (new_line == NULL)
1136 /* Restore pointer to beginning of old line. */
1139 /* Save pointer to beginning of new line. */
1140 save_line = new_line;
1142 while ((p = locate_arg (line)))
1146 memcpy (new_line, line, p - line);
1147 new_line += p - line;
1150 len = user_args->a[i].len;
1153 memcpy (new_line, user_args->a[i].arg, len);
1158 /* Don't forget the tail. */
1159 strcpy (new_line, line);
1161 /* Return a pointer to the beginning of the new line. */
1166 execute_user_command (c, args)
1167 struct cmd_list_element *c;
1170 register struct command_line *cmdlines;
1171 struct cleanup *old_chain;
1172 enum command_control_type ret;
1174 old_chain = setup_user_args (args);
1176 cmdlines = c->user_commands;
1181 /* Set the instream to 0, indicating execution of a
1182 user-defined function. */
1183 old_chain = make_cleanup ((make_cleanup_func) source_cleanup, instream);
1184 instream = (FILE *) 0;
1187 ret = execute_control_command (cmdlines);
1188 if (ret != simple_control && ret != break_control)
1190 warning ("Error in control structure.\n");
1193 cmdlines = cmdlines->next;
1195 do_cleanups (old_chain);
1198 /* Execute the line P as a command.
1199 Pass FROM_TTY as second argument to the defining function. */
1202 execute_command (p, from_tty)
1206 register struct cmd_list_element *c;
1207 register enum language flang;
1208 static int warned = 0;
1209 /* FIXME: These should really be in an appropriate header file */
1210 extern void serial_log_command PARAMS ((const char *));
1214 /* Force cleanup of any alloca areas if using C alloca instead of
1215 a builtin alloca. */
1218 /* This can happen when command_line_input hits end of file. */
1222 serial_log_command (p);
1224 while (*p == ' ' || *p == '\t') p++;
1229 c = lookup_cmd (&p, cmdlist, "", 0, 1);
1230 /* Pass null arg rather than an empty one. */
1233 /* Clear off trailing whitespace, except for set and complete command. */
1234 if (arg && c->type != set_cmd && c->function.cfunc != complete_command)
1236 p = arg + strlen (arg) - 1;
1237 while (p >= arg && (*p == ' ' || *p == '\t'))
1242 /* If this command has been hooked, run the hook first. */
1244 execute_user_command (c->hook, (char *)0);
1246 if (c->class == class_user)
1247 execute_user_command (c, arg);
1248 else if (c->type == set_cmd || c->type == show_cmd)
1249 do_setshow_command (arg, from_tty & caution, c);
1250 else if (c->function.cfunc == NO_FUNCTION)
1251 error ("That is not a command, just a help topic.");
1252 else if (call_command_hook)
1253 call_command_hook (c, arg, from_tty & caution);
1255 (*c->function.cfunc) (arg, from_tty & caution);
1258 /* Tell the user if the language has changed (except first time). */
1259 if (current_language != expected_language)
1261 if (language_mode == language_mode_auto) {
1262 language_info (1); /* Print what changed. */
1267 /* Warn the user if the working language does not match the
1268 language of the current frame. Only warn the user if we are
1269 actually running the program, i.e. there is a stack. */
1270 /* FIXME: This should be cacheing the frame and only running when
1271 the frame changes. */
1273 if (target_has_stack)
1275 flang = get_frame_language ();
1277 && flang != language_unknown
1278 && flang != current_language->la_language)
1280 printf_filtered ("%s\n", lang_frame_mismatch_warn);
1288 command_loop_marker (foo)
1293 /* Read commands from `instream' and execute them
1294 until end of file or error reading instream. */
1299 struct cleanup *old_chain;
1301 int stdin_is_tty = ISATTY (stdin);
1302 long time_at_cmd_start;
1304 long space_at_cmd_start;
1306 extern int display_time;
1307 extern int display_space;
1309 while (instream && !feof (instream))
1311 if (window_hook && instream == stdin)
1312 (*window_hook) (instream, prompt);
1315 if (instream == stdin && stdin_is_tty)
1316 reinitialize_more_filter ();
1317 old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0);
1318 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
1319 instream == stdin, "prompt");
1323 time_at_cmd_start = get_run_time ();
1328 extern char **environ;
1329 char *lim = (char *) sbrk (0);
1331 space_at_cmd_start = (long) (lim - (char *) &environ);
1335 execute_command (command, instream == stdin);
1336 /* Do any commands attached to breakpoint we stopped at. */
1337 bpstat_do_actions (&stop_bpstat);
1338 do_cleanups (old_chain);
1342 long cmd_time = get_run_time () - time_at_cmd_start;
1344 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1345 cmd_time / 1000000, cmd_time % 1000000);
1351 extern char **environ;
1352 char *lim = (char *) sbrk (0);
1353 long space_now = lim - (char *) &environ;
1354 long space_diff = space_now - space_at_cmd_start;
1356 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1358 (space_diff >= 0 ? '+' : '-'),
1365 /* Commands call this if they do not want to be repeated by null lines. */
1373 /* If we aren't reading from standard input, we are saving the last
1374 thing read from stdin in line and don't want to delete it. Null lines
1375 won't repeat here in any case. */
1376 if (instream == stdin)
1380 /* Read a line from the stream "instream" without command line editing.
1382 It prints PRROMPT once at the start.
1383 Action is compatible with "readline", e.g. space for the result is
1384 malloc'd and should be freed by the caller.
1386 A NULL return means end of file. */
1388 gdb_readline (prrompt)
1393 int input_index = 0;
1394 int result_size = 80;
1398 /* Don't use a _filtered function here. It causes the assumed
1399 character position to be off, since the newline we read from
1400 the user is not accounted for. */
1401 fputs_unfiltered (prrompt, gdb_stdout);
1403 /* Move to a new line so the entered line doesn't have a prompt
1404 on the front of it. */
1405 fputs_unfiltered ("\n", gdb_stdout);
1407 gdb_flush (gdb_stdout);
1410 result = (char *) xmalloc (result_size);
1414 /* Read from stdin if we are executing a user defined command.
1415 This is the right thing for prompt_for_continue, at least. */
1416 c = fgetc (instream ? instream : stdin);
1420 if (input_index > 0)
1421 /* The last line does not end with a newline. Return it, and
1422 if we are called again fgetc will still return EOF and
1423 we'll return NULL then. */
1430 #ifndef CRLF_SOURCE_FILES
1434 if (input_index > 0 && result[input_index - 1] == '\r')
1440 result[input_index++] = c;
1441 while (input_index >= result_size)
1444 result = (char *) xrealloc (result, result_size);
1448 result[input_index++] = '\0';
1452 /* Variables which control command line editing and history
1453 substitution. These variables are given default values at the end
1455 static int command_editing_p;
1456 static int history_expansion_p;
1457 static int write_history_p;
1458 static int history_size;
1459 static char *history_filename;
1461 /* readline uses the word breaks for two things:
1462 (1) In figuring out where to point the TEXT parameter to the
1463 rl_completion_entry_function. Since we don't use TEXT for much,
1464 it doesn't matter a lot what the word breaks are for this purpose, but
1465 it does affect how much stuff M-? lists.
1466 (2) If one of the matches contains a word break character, readline
1467 will quote it. That's why we switch between
1468 gdb_completer_word_break_characters and
1469 gdb_completer_command_word_break_characters. I'm not sure when
1470 we need this behavior (perhaps for funky characters in C++ symbols?). */
1472 /* Variables which are necessary for fancy command line editing. */
1473 char *gdb_completer_word_break_characters =
1474 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1476 /* When completing on command names, we remove '-' from the list of
1477 word break characters, since we use it in command names. If the
1478 readline library sees one in any of the current completion strings,
1479 it thinks that the string needs to be quoted and automatically supplies
1481 char *gdb_completer_command_word_break_characters =
1482 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1484 /* Characters that can be used to quote completion strings. Note that we
1485 can't include '"' because the gdb C parser treats such quoted sequences
1487 char *gdb_completer_quote_characters =
1490 /* Functions that are used as part of the fancy command line editing. */
1492 /* This can be used for functions which don't want to complete on symbols
1493 but don't want to complete on anything else either. */
1496 noop_completer (text, prefix)
1503 /* Complete on filenames. */
1505 filename_completer (text, word)
1509 /* From readline. */
1510 extern char *filename_completion_function PARAMS ((char *, int));
1511 int subsequent_name;
1513 int return_val_used;
1514 int return_val_alloced;
1516 return_val_used = 0;
1517 /* Small for testing. */
1518 return_val_alloced = 1;
1519 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1521 subsequent_name = 0;
1525 p = filename_completion_function (text, subsequent_name);
1526 if (return_val_used >= return_val_alloced)
1528 return_val_alloced *= 2;
1530 (char **) xrealloc (return_val,
1531 return_val_alloced * sizeof (char *));
1535 return_val[return_val_used++] = p;
1538 /* Like emacs, don't complete on old versions. Especially useful
1539 in the "source" command. */
1540 if (p[strlen (p) - 1] == '~')
1546 /* Return exactly p. */
1547 return_val[return_val_used++] = p;
1548 else if (word > text)
1550 /* Return some portion of p. */
1551 q = xmalloc (strlen (p) + 5);
1552 strcpy (q, p + (word - text));
1553 return_val[return_val_used++] = q;
1558 /* Return some of TEXT plus p. */
1559 q = xmalloc (strlen (p) + (text - word) + 5);
1560 strncpy (q, word, text - word);
1561 q[text - word] = '\0';
1563 return_val[return_val_used++] = q;
1567 subsequent_name = 1;
1570 /* There is no way to do this just long enough to affect quote inserting
1571 without also affecting the next completion. This should be fixed in
1573 /* Insure that readline does the right thing
1574 with respect to inserting quotes. */
1575 rl_completer_word_break_characters = "";
1580 /* Here are some useful test cases for completion. FIXME: These should
1581 be put in the test suite. They should be tested with both M-? and TAB.
1583 "show output-" "radix"
1584 "show output" "-radix"
1585 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1586 "p " ambiguous (all symbols)
1587 "info t foo" no completions
1588 "info t " no completions
1589 "info t" ambiguous ("info target", "info terminal", etc.)
1590 "info ajksdlfk" no completions
1591 "info ajksdlfk " no completions
1593 "info " ambiguous (all info commands)
1594 "p \"a" no completions (string constant)
1595 "p 'a" ambiguous (all symbols starting with a)
1596 "p b-a" ambiguous (all symbols starting with a)
1597 "p b-" ambiguous (all symbols)
1598 "file Make" "file" (word break hard to screw up here)
1599 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1602 /* Generate completions one by one for the completer. Each time we are
1603 called return another potential completion to the caller.
1604 line_completion just completes on commands or passes the buck to the
1605 command's completer function, the stuff specific to symbol completion
1606 is in make_symbol_completion_list.
1608 TEXT is the caller's idea of the "word" we are looking at.
1610 MATCHES is the number of matches that have currently been collected from
1611 calling this completion function. When zero, then we need to initialize,
1612 otherwise the initialization has already taken place and we can just
1613 return the next potential completion string.
1615 LINE_BUFFER is available to be looked at; it contains the entire text
1616 of the line. POINT is the offset in that line of the cursor. You
1617 should pretend that the line ends at POINT.
1619 Returns NULL if there are no more completions, else a pointer to a string
1620 which is a possible completion, it is the caller's responsibility to
1624 line_completion_function (text, matches, line_buffer, point)
1630 static char **list = (char **)NULL; /* Cache of completions */
1631 static int index; /* Next cached completion */
1632 char *output = NULL;
1633 char *tmp_command, *p;
1634 /* Pointer within tmp_command which corresponds to text. */
1636 struct cmd_list_element *c, *result_list;
1640 /* The caller is beginning to accumulate a new set of completions, so
1641 we need to find all of them now, and cache them for returning one at
1642 a time on future calls. */
1646 /* Free the storage used by LIST, but not by the strings inside.
1647 This is because rl_complete_internal () frees the strings. */
1653 /* Choose the default set of word break characters to break completions.
1654 If we later find out that we are doing completions on command strings
1655 (as opposed to strings supplied by the individual command completer
1656 functions, which can be any string) then we will switch to the
1657 special word break set for command strings, which leaves out the
1658 '-' character used in some commands. */
1660 rl_completer_word_break_characters =
1661 gdb_completer_word_break_characters;
1663 /* Decide whether to complete on a list of gdb commands or on symbols. */
1664 tmp_command = (char *) alloca (point + 1);
1667 strncpy (tmp_command, line_buffer, point);
1668 tmp_command[point] = '\0';
1669 /* Since text always contains some number of characters leading up
1670 to point, we can find the equivalent position in tmp_command
1671 by subtracting that many characters from the end of tmp_command. */
1672 word = tmp_command + point - strlen (text);
1676 /* An empty line we want to consider ambiguous; that is, it
1677 could be any command. */
1678 c = (struct cmd_list_element *) -1;
1683 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1686 /* Move p up to the next interesting thing. */
1687 while (*p == ' ' || *p == '\t')
1694 /* It is an unrecognized command. So there are no
1695 possible completions. */
1698 else if (c == (struct cmd_list_element *) -1)
1702 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1703 doesn't advance over that thing itself. Do so now. */
1705 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1707 if (q != tmp_command + point)
1709 /* There is something beyond the ambiguous
1710 command, so there are no possible completions. For
1711 example, "info t " or "info t foo" does not complete
1712 to anything, because "info t" can be "info target" or
1718 /* We're trying to complete on the command which was ambiguous.
1719 This we can deal with. */
1722 list = complete_on_cmdlist (*result_list->prefixlist, p,
1727 list = complete_on_cmdlist (cmdlist, p, word);
1729 /* Insure that readline does the right thing with respect to
1730 inserting quotes. */
1731 rl_completer_word_break_characters =
1732 gdb_completer_command_word_break_characters;
1737 /* We've recognized a full command. */
1739 if (p == tmp_command + point)
1741 /* There is no non-whitespace in the line beyond the command. */
1743 if (p[-1] == ' ' || p[-1] == '\t')
1745 /* The command is followed by whitespace; we need to complete
1746 on whatever comes after command. */
1749 /* It is a prefix command; what comes after it is
1750 a subcommand (e.g. "info "). */
1751 list = complete_on_cmdlist (*c->prefixlist, p, word);
1753 /* Insure that readline does the right thing
1754 with respect to inserting quotes. */
1755 rl_completer_word_break_characters =
1756 gdb_completer_command_word_break_characters;
1760 list = complete_on_enum (c->enums, p, word);
1761 rl_completer_word_break_characters =
1762 gdb_completer_command_word_break_characters;
1766 /* It is a normal command; what comes after it is
1767 completed by the command's completer function. */
1768 list = (*c->completer) (p, word);
1773 /* The command is not followed by whitespace; we need to
1774 complete on the command itself. e.g. "p" which is a
1775 command itself but also can complete to "print", "ptype"
1779 /* Find the command we are completing on. */
1781 while (q > tmp_command)
1783 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1789 list = complete_on_cmdlist (result_list, q, word);
1791 /* Insure that readline does the right thing
1792 with respect to inserting quotes. */
1793 rl_completer_word_break_characters =
1794 gdb_completer_command_word_break_characters;
1799 /* There is non-whitespace beyond the command. */
1801 if (c->prefixlist && !c->allow_unknown)
1803 /* It is an unrecognized subcommand of a prefix command,
1804 e.g. "info adsfkdj". */
1809 list = complete_on_enum (c->enums, p, word);
1813 /* It is a normal command. */
1814 list = (*c->completer) (p, word);
1820 /* If we found a list of potential completions during initialization then
1821 dole them out one at a time. The vector of completions is NULL
1822 terminated, so after returning the last one, return NULL (and continue
1823 to do so) each time we are called after that, until a new list is
1828 output = list[index];
1836 /* Can't do this because readline hasn't yet checked the word breaks
1837 for figuring out whether to insert a quote. */
1839 /* Make sure the word break characters are set back to normal for the
1840 next time that readline tries to complete something. */
1841 rl_completer_word_break_characters =
1842 gdb_completer_word_break_characters;
1848 /* Line completion interface function for readline. */
1851 readline_line_completion_function (text, matches)
1855 return line_completion_function (text, matches, rl_line_buffer, rl_point);
1858 /* Skip over a possibly quoted word (as defined by the quote characters
1859 and word break characters the completer uses). Returns pointer to the
1860 location after the "word". */
1866 char quote_char = '\0';
1869 for (scan = str; *scan != '\0'; scan++)
1871 if (quote_char != '\0')
1873 /* Ignore everything until the matching close quote char */
1874 if (*scan == quote_char)
1876 /* Found matching close quote. */
1881 else if (strchr (gdb_completer_quote_characters, *scan))
1883 /* Found start of a quoted string. */
1886 else if (strchr (gdb_completer_word_break_characters, *scan))
1900 #if STOP_SIGNAL == SIGTSTP
1901 signal (SIGTSTP, SIG_DFL);
1903 kill (getpid (), SIGTSTP);
1904 signal (SIGTSTP, stop_sig);
1906 signal (STOP_SIGNAL, stop_sig);
1908 printf_unfiltered ("%s", prompt);
1909 gdb_flush (gdb_stdout);
1911 /* Forget about any previous command -- null line now will do nothing. */
1914 #endif /* STOP_SIGNAL */
1916 /* Initialize signal handlers. */
1921 /* Under System V the default disposition of a signal is reinstated after
1922 the signal is caught and delivered to an application process. On such
1923 systems one must restore the replacement signal handler if one wishes
1924 to continue handling the signal in one's program. On BSD systems this
1925 is not needed but it is harmless, and it simplifies the code to just do
1926 it unconditionally. */
1927 signal (signo, do_nothing);
1933 signal (SIGINT, request_quit);
1935 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1936 to the inferior and breakpoints will be ignored. */
1938 signal (SIGTRAP, SIG_DFL);
1941 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1942 passed to the inferior, which we don't want. It would be
1943 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1944 on BSD4.3 systems using vfork, that can affect the
1945 GDB process as well as the inferior (the signal handling tables
1946 might be in memory, shared between the two). Since we establish
1947 a handler for SIGQUIT, when we call exec it will set the signal
1948 to SIG_DFL for us. */
1949 signal (SIGQUIT, do_nothing);
1951 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1952 signal (SIGHUP, disconnect);
1954 signal (SIGFPE, float_handler);
1956 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1957 signal (SIGWINCH, SIGWINCH_HANDLER);
1961 /* Read one line from the command input stream `instream'
1962 into the local static buffer `linebuffer' (whose current length
1964 The buffer is made bigger as necessary.
1965 Returns the address of the start of the line.
1967 NULL is returned for end of file.
1969 *If* the instream == stdin & stdin is a terminal, the line read
1970 is copied into the file line saver (global var char *line,
1971 length linesize) so that it can be duplicated.
1973 This routine either uses fancy command line editing or
1974 simple input as the user has requested. */
1977 command_line_input (prrompt, repeat, annotation_suffix)
1980 char *annotation_suffix;
1982 static char *linebuffer = 0;
1983 static unsigned linelength = 0;
1987 char *local_prompt = prrompt;
1991 /* The annotation suffix must be non-NULL. */
1992 if (annotation_suffix == NULL)
1993 annotation_suffix = "";
1995 if (annotation_level > 1 && instream == stdin)
1997 local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt))
1998 + strlen (annotation_suffix) + 40);
1999 if (prrompt == NULL)
2000 local_prompt[0] = '\0';
2002 strcpy (local_prompt, prrompt);
2003 strcat (local_prompt, "\n\032\032");
2004 strcat (local_prompt, annotation_suffix);
2005 strcat (local_prompt, "\n");
2008 if (linebuffer == 0)
2011 linebuffer = (char *) xmalloc (linelength);
2016 /* Control-C quits instantly if typed while in this loop
2017 since it should not wait until the user types a newline. */
2021 signal (STOP_SIGNAL, stop_sig);
2026 /* Make sure that all output has been output. Some machines may let
2027 you get away with leaving out some of the gdb_flush, but not all. */
2029 gdb_flush (gdb_stdout);
2030 gdb_flush (gdb_stderr);
2032 if (source_file_name != NULL)
2034 ++source_line_number;
2035 sprintf (source_error,
2036 "%s%s:%d: Error in sourced command file:\n",
2039 source_line_number);
2040 error_pre_print = source_error;
2043 if (annotation_level > 1 && instream == stdin)
2045 printf_unfiltered ("\n\032\032pre-");
2046 printf_unfiltered (annotation_suffix);
2047 printf_unfiltered ("\n");
2050 /* Don't use fancy stuff if not talking to stdin. */
2051 if (readline_hook && instream == NULL)
2053 rl = (*readline_hook) (local_prompt);
2055 else if (command_editing_p && instream == stdin && ISATTY (instream))
2057 rl = readline (local_prompt);
2061 rl = gdb_readline (local_prompt);
2064 if (annotation_level > 1 && instream == stdin)
2066 printf_unfiltered ("\n\032\032post-");
2067 printf_unfiltered (annotation_suffix);
2068 printf_unfiltered ("\n");
2071 if (!rl || rl == (char *) EOF)
2076 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
2078 linelength = strlen(rl) + 1 + (p - linebuffer);
2079 nline = (char *) xrealloc (linebuffer, linelength);
2080 p += nline - linebuffer;
2084 /* Copy line. Don't copy null at end. (Leaves line alone
2085 if this was just a newline) */
2089 free (rl); /* Allocated in readline. */
2091 if (p == linebuffer || *(p - 1) != '\\')
2094 p--; /* Put on top of '\'. */
2095 local_prompt = (char *) 0;
2100 signal (STOP_SIGNAL, SIG_DFL);
2107 #define SERVER_COMMAND_LENGTH 7
2109 (p - linebuffer > SERVER_COMMAND_LENGTH)
2110 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
2113 /* Note that we don't set `line'. Between this and the check in
2114 dont_repeat, this insures that repeating will still do the
2117 return linebuffer + SERVER_COMMAND_LENGTH;
2120 /* Do history expansion if that is wished. */
2121 if (history_expansion_p && instream == stdin
2122 && ISATTY (instream))
2124 char *history_value;
2127 *p = '\0'; /* Insert null now. */
2128 expanded = history_expand (linebuffer, &history_value);
2131 /* Print the changes. */
2132 printf_unfiltered ("%s\n", history_value);
2134 /* If there was an error, call this function again. */
2137 free (history_value);
2138 return command_line_input (prrompt, repeat, annotation_suffix);
2140 if (strlen (history_value) > linelength)
2142 linelength = strlen (history_value) + 1;
2143 linebuffer = (char *) xrealloc (linebuffer, linelength);
2145 strcpy (linebuffer, history_value);
2146 p = linebuffer + strlen(linebuffer);
2147 free (history_value);
2151 /* If we just got an empty line, and that is supposed
2152 to repeat the previous command, return the value in the
2154 if (repeat && p == linebuffer)
2156 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++) ;
2162 /* Add line to history if appropriate. */
2163 if (instream == stdin
2164 && ISATTY (stdin) && *linebuffer)
2165 add_history (linebuffer);
2167 /* Note: lines consisting solely of comments are added to the command
2168 history. This is useful when you type a command, and then
2169 realize you don't want to execute it quite yet. You can comment
2170 out the command and then later fetch it from the value history
2171 and remove the '#'. The kill ring is probably better, but some
2172 people are in the habit of commenting things out. */
2174 *p1 = '\0'; /* Found a comment. */
2176 /* Save into global buffer if appropriate. */
2179 if (linelength > linesize)
2181 line = xrealloc (line, linelength);
2182 linesize = linelength;
2184 strcpy (line, linebuffer);
2192 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2193 code bodies. This is typically used when we encounter an "else"
2194 clause for an "if" command. */
2197 realloc_body_list (command, new_length)
2198 struct command_line *command;
2202 struct command_line **body_list;
2204 n = command->body_count;
2206 /* Nothing to do? */
2207 if (new_length <= n)
2210 body_list = (struct command_line **)
2211 xmalloc (sizeof (struct command_line *) * new_length);
2213 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
2215 free (command->body_list);
2216 command->body_list = body_list;
2217 command->body_count = new_length;
2220 /* Read one line from the input stream. If the command is an "else" or
2221 "end", return such an indication to the caller. */
2223 static enum misc_command_type
2224 read_next_line (command)
2225 struct command_line **command;
2227 char *p, *p1, *prompt_ptr, control_prompt[256];
2230 if (control_level >= 254)
2231 error ("Control nesting too deep!\n");
2233 /* Set a prompt based on the nesting of the control commands. */
2234 if (instream == stdin || (instream == 0 && readline_hook != NULL))
2236 for (i = 0; i < control_level; i++)
2237 control_prompt[i] = ' ';
2238 control_prompt[i] = '>';
2239 control_prompt[i+1] = '\0';
2240 prompt_ptr = (char *)&control_prompt[0];
2245 p = command_line_input (prompt_ptr, instream == stdin, "commands");
2247 /* Not sure what to do here. */
2251 /* Strip leading and trailing whitespace. */
2252 while (*p == ' ' || *p == '\t')
2255 p1 = p + strlen (p);
2256 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
2259 /* Blanks and comments don't really do anything, but we need to
2260 distinguish them from else, end and other commands which can be
2262 if (p1 == p || p[0] == '#')
2265 /* Is this the end of a simple, while, or if control structure? */
2266 if (p1 - p == 3 && !strncmp (p, "end", 3))
2269 /* Is the else clause of an if control structure? */
2270 if (p1 - p == 4 && !strncmp (p, "else", 4))
2271 return else_command;
2273 /* Check for while, if, break, continue, etc and build a new command
2274 line structure for them. */
2275 if (p1 - p > 5 && !strncmp (p, "while", 5))
2276 *command = build_command_line (while_control, p + 6);
2277 else if (p1 - p > 2 && !strncmp (p, "if", 2))
2278 *command = build_command_line (if_control, p + 3);
2279 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
2281 *command = (struct command_line *)
2282 xmalloc (sizeof (struct command_line));
2283 (*command)->next = NULL;
2284 (*command)->line = NULL;
2285 (*command)->control_type = break_control;
2286 (*command)->body_count = 0;
2287 (*command)->body_list = NULL;
2289 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
2291 *command = (struct command_line *)
2292 xmalloc (sizeof (struct command_line));
2293 (*command)->next = NULL;
2294 (*command)->line = NULL;
2295 (*command)->control_type = continue_control;
2296 (*command)->body_count = 0;
2297 (*command)->body_list = NULL;
2301 /* A normal command. */
2302 *command = (struct command_line *)
2303 xmalloc (sizeof (struct command_line));
2304 (*command)->next = NULL;
2305 (*command)->line = savestring (p, p1 - p);
2306 (*command)->control_type = simple_control;
2307 (*command)->body_count = 0;
2308 (*command)->body_list = NULL;
2311 /* Nothing special. */
2315 /* Recursively read in the control structures and create a command_line
2316 structure from them.
2318 The parent_control parameter is the control structure in which the
2319 following commands are nested. */
2321 static enum command_control_type
2322 recurse_read_control_structure (current_cmd)
2323 struct command_line *current_cmd;
2325 int current_body, i;
2326 enum misc_command_type val;
2327 enum command_control_type ret;
2328 struct command_line **body_ptr, *child_tail, *next;
2333 /* Sanity checks. */
2334 if (current_cmd->control_type == simple_control)
2336 error ("Recursed on a simple control type\n");
2337 return invalid_control;
2340 if (current_body > current_cmd->body_count)
2342 error ("Allocated body is smaller than this command type needs\n");
2343 return invalid_control;
2346 /* Read lines from the input stream and build control structures. */
2352 val = read_next_line (&next);
2354 /* Just skip blanks and comments. */
2355 if (val == nop_command)
2358 if (val == end_command)
2360 if (current_cmd->control_type == while_control
2361 || current_cmd->control_type == if_control)
2363 /* Success reading an entire control structure. */
2364 ret = simple_control;
2369 ret = invalid_control;
2374 /* Not the end of a control structure. */
2375 if (val == else_command)
2377 if (current_cmd->control_type == if_control
2378 && current_body == 1)
2380 realloc_body_list (current_cmd, 2);
2387 ret = invalid_control;
2394 child_tail->next = next;
2398 body_ptr = current_cmd->body_list;
2399 for (i = 1; i < current_body; i++)
2408 /* If the latest line is another control structure, then recurse
2410 if (next->control_type == while_control
2411 || next->control_type == if_control)
2414 ret = recurse_read_control_structure (next);
2417 if (ret != simple_control)
2427 /* Read lines from the input stream and accumulate them in a chain of
2428 struct command_line's, which is then returned. For input from a
2429 terminal, the special command "end" is used to mark the end of the
2430 input, and is not included in the returned chain of commands. */
2432 #define END_MESSAGE "End with a line saying just \"end\"."
2434 struct command_line *
2435 read_command_lines (prompt, from_tty)
2439 struct command_line *head, *tail, *next;
2440 struct cleanup *old_chain;
2441 enum command_control_type ret;
2442 enum misc_command_type val;
2444 if (readline_begin_hook)
2446 /* Note - intentional to merge messages with no newline */
2447 (*readline_begin_hook) ("%s %s\n", prompt, END_MESSAGE);
2449 else if (from_tty && input_from_terminal_p ())
2451 printf_unfiltered ("%s\n%s\n", prompt, END_MESSAGE);
2452 gdb_flush (gdb_stdout);
2460 val = read_next_line (&next);
2462 /* Ignore blank lines or comments. */
2463 if (val == nop_command)
2466 if (val == end_command)
2468 ret = simple_control;
2472 if (val != ok_command)
2474 ret = invalid_control;
2478 if (next->control_type == while_control
2479 || next->control_type == if_control)
2482 ret = recurse_read_control_structure (next);
2485 if (ret == invalid_control)
2496 old_chain = make_cleanup ((make_cleanup_func) free_command_lines,
2506 if (ret != invalid_control)
2508 discard_cleanups (old_chain);
2511 do_cleanups (old_chain);
2514 if (readline_end_hook)
2516 (*readline_end_hook) ();
2521 /* Free a chain of struct command_line's. */
2524 free_command_lines (lptr)
2525 struct command_line **lptr;
2527 register struct command_line *l = *lptr;
2528 register struct command_line *next;
2529 struct command_line **blist;
2534 if (l->body_count > 0)
2536 blist = l->body_list;
2537 for (i = 0; i < l->body_count; i++, blist++)
2538 free_command_lines (blist);
2547 /* Add an element to the list of info subcommands. */
2550 add_info (name, fun, doc)
2552 void (*fun) PARAMS ((char *, int));
2555 add_cmd (name, no_class, fun, doc, &infolist);
2558 /* Add an alias to the list of info subcommands. */
2561 add_info_alias (name, oldname, abbrev_flag)
2566 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2569 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2570 Therefore, its own definition is called only for "info" with no args. */
2574 info_command (arg, from_tty)
2578 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2579 help_list (infolist, "info ", -1, gdb_stdout);
2582 /* The "complete" command is used by Emacs to implement completion. */
2586 complete_command (arg, from_tty)
2598 argpoint = strlen (arg);
2600 for (completion = line_completion_function (arg, i = 0, arg, argpoint);
2602 completion = line_completion_function (arg, ++i, arg, argpoint))
2604 printf_unfiltered ("%s\n", completion);
2609 /* The "show" command with no arguments shows all the settings. */
2613 show_command (arg, from_tty)
2617 cmd_show_list (showlist, from_tty, "");
2620 /* Add an element to the list of commands. */
2623 add_com (name, class, fun, doc)
2625 enum command_class class;
2626 void (*fun) PARAMS ((char *, int));
2629 add_cmd (name, class, fun, doc, &cmdlist);
2632 /* Add an alias or abbreviation command to the list of commands. */
2635 add_com_alias (name, oldname, class, abbrev_flag)
2638 enum command_class class;
2641 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2648 error ("Argument required (%s).", why);
2653 help_command (command, from_tty)
2655 int from_tty; /* Ignored */
2657 help_cmd (command, gdb_stdout);
2661 validate_comname (comname)
2667 error_no_arg ("name of command to define");
2672 if (!isalnum(*p) && *p != '-' && *p != '_')
2673 error ("Junk in argument list: \"%s\"", p);
2678 /* This is just a placeholder in the command data structures. */
2680 user_defined_command (ignore, from_tty)
2687 define_command (comname, from_tty)
2691 register struct command_line *cmds;
2692 register struct cmd_list_element *c, *newc, *hookc = 0;
2693 char *tem = comname;
2695 #define HOOK_STRING "hook-"
2698 validate_comname (comname);
2700 /* Look it up, and verify that we got an exact match. */
2701 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
2702 if (c && !STREQ (comname, c->name))
2707 if (c->class == class_user || c->class == class_alias)
2708 tem = "Redefine command \"%s\"? ";
2710 tem = "Really redefine built-in command \"%s\"? ";
2711 if (!query (tem, c->name))
2712 error ("Command \"%s\" not redefined.", c->name);
2715 /* If this new command is a hook, then mark the command which it
2716 is hooking. Note that we allow hooking `help' commands, so that
2717 we can hook the `stop' pseudo-command. */
2719 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
2721 /* Look up cmd it hooks, and verify that we got an exact match. */
2722 tem = comname+HOOK_LEN;
2723 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
2724 if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
2728 warning ("Your new `%s' command does not hook any existing command.",
2730 if (!query ("Proceed? "))
2731 error ("Not confirmed.");
2735 comname = savestring (comname, strlen (comname));
2737 /* If the rest of the commands will be case insensitive, this one
2738 should behave in the same manner. */
2739 for (tem = comname; *tem; tem++)
2740 if (isupper(*tem)) *tem = tolower(*tem);
2743 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
2744 cmds = read_command_lines (tmpbuf, from_tty);
2746 if (c && c->class == class_user)
2747 free_command_lines (&c->user_commands);
2749 newc = add_cmd (comname, class_user, user_defined_command,
2750 (c && c->class == class_user)
2751 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
2752 newc->user_commands = cmds;
2754 /* If this new command is a hook, then mark both commands as being
2758 hookc->hook = newc; /* Target gets hooked. */
2759 newc->hookee = hookc; /* We are marked as hooking target cmd. */
2764 document_command (comname, from_tty)
2768 struct command_line *doclines;
2769 register struct cmd_list_element *c;
2770 char *tem = comname;
2773 validate_comname (comname);
2775 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
2777 if (c->class != class_user)
2778 error ("Command \"%s\" is built-in.", comname);
2780 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
2781 doclines = read_command_lines (tmpbuf, from_tty);
2783 if (c->doc) free (c->doc);
2786 register struct command_line *cl1;
2787 register int len = 0;
2789 for (cl1 = doclines; cl1; cl1 = cl1->next)
2790 len += strlen (cl1->line) + 1;
2792 c->doc = (char *) xmalloc (len + 1);
2795 for (cl1 = doclines; cl1; cl1 = cl1->next)
2797 strcat (c->doc, cl1->line);
2799 strcat (c->doc, "\n");
2803 free_command_lines (&doclines);
2807 print_gdb_version (stream)
2810 /* From GNU coding standards, first line is meant to be easy for a
2811 program to parse, and is just canonical program name and version
2812 number, which starts after last space. */
2814 fprintf_filtered (stream, "GNU gdb %s\n", version);
2816 /* Second line is a copyright notice. */
2818 fprintf_filtered (stream, "Copyright 1998 Free Software Foundation, Inc.\n");
2820 /* Following the copyright is a brief statement that the program is
2821 free software, that users are free to copy and change it on
2822 certain conditions, that it is covered by the GNU GPL, and that
2823 there is no warranty. */
2825 fprintf_filtered (stream, "\
2826 GDB is free software, covered by the GNU General Public License, and you are\n\
2827 welcome to change it and/or distribute copies of it under certain conditions.\n\
2828 Type \"show copying\" to see the conditions.\n\
2829 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2831 /* After the required info we print the configuration information. */
2833 fprintf_filtered (stream, "This GDB was configured as \"");
2834 if (!STREQ (host_name, target_name))
2836 fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
2840 fprintf_filtered (stream, "%s", host_name);
2842 fprintf_filtered (stream, "\".");
2847 show_version (args, from_tty)
2852 print_gdb_version (gdb_stdout);
2853 printf_filtered ("\n");
2857 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2863 printf_unfiltered ("%s", prompt);
2864 gdb_flush (gdb_stdout);
2867 /* This replaces the above for the frontends: it returns a pointer
2875 /* If necessary, make the user confirm that we should quit. Return
2876 non-zero if we should quit, zero if we shouldn't. */
2881 if (inferior_pid != 0 && target_has_execution)
2885 /* This is something of a hack. But there's no reliable way to
2886 see if a GUI is running. The `use_windows' variable doesn't
2889 s = "A debugging session is active.\nDo you still want to close the debugger?";
2890 else if (attach_flag)
2891 s = "The program is running. Quit anyway (and detach it)? ";
2893 s = "The program is running. Exit anyway? ";
2902 /* Quit without asking for confirmation. */
2905 quit_force (args, from_tty)
2911 /* An optional expression may be used to cause gdb to terminate with the
2912 value of that expression. */
2915 value_ptr val = parse_and_eval (args);
2917 exit_code = (int) value_as_long (val);
2920 if (inferior_pid != 0 && target_has_execution)
2923 target_detach (args, from_tty);
2928 /* UDI wants this, to kill the TIP. */
2931 /* Save the history information if it is appropriate to do so. */
2932 if (write_history_p && history_filename)
2933 write_history (history_filename);
2935 do_final_cleanups(ALL_CLEANUPS); /* Do any final cleanups before exiting */
2940 /* Handle the quit command. */
2943 quit_command (args, from_tty)
2947 if (! quit_confirm ())
2948 error ("Not confirmed.");
2949 quit_force (args, from_tty);
2952 /* Returns whether GDB is running on a terminal and whether the user
2953 desires that questions be asked of them on that terminal. */
2956 input_from_terminal_p ()
2958 return gdb_has_a_terminal () && (instream == stdin) & caution;
2963 pwd_command (args, from_tty)
2967 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
2968 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
2970 if (!STREQ (gdb_dirbuf, current_directory))
2971 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2972 current_directory, gdb_dirbuf);
2974 printf_unfiltered ("Working directory %s.\n", current_directory);
2978 cd_command (dir, from_tty)
2983 /* Found something other than leading repetitions of "/..". */
2984 int found_real_path;
2987 /* If the new directory is absolute, repeat is a no-op; if relative,
2988 repeat might be useful but is more likely to be a mistake. */
2992 error_no_arg ("new working directory");
2994 dir = tilde_expand (dir);
2995 make_cleanup (free, dir);
2997 if (chdir (dir) < 0)
2998 perror_with_name (dir);
3001 dir = savestring (dir, len - (len > 1 && SLASH_P(dir[len-1])));
3003 current_directory = dir;
3006 if (SLASH_P (current_directory[0]) && current_directory[1] == '\0')
3007 current_directory = concat (current_directory, dir, NULL);
3009 current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
3013 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3015 found_real_path = 0;
3016 for (p = current_directory; *p;)
3018 if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2])))
3020 else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.'
3021 && (p[3] == 0 || SLASH_P (p[3])))
3023 if (found_real_path)
3025 /* Search backwards for the directory just before the "/.."
3026 and obliterate it and the "/..". */
3028 while (q != current_directory && ! SLASH_P (q[-1]))
3031 if (q == current_directory)
3032 /* current_directory is
3033 a relative pathname ("can't happen"--leave it alone). */
3037 strcpy (q - 1, p + 3);
3042 /* We are dealing with leading repetitions of "/..", for example
3043 "/../..", which is the Mach super-root. */
3048 found_real_path = 1;
3053 forget_cached_source_info ();
3056 pwd_command ((char *) 0, 1);
3059 struct source_cleanup_lines_args {
3062 char *old_pre_error;
3063 char *old_error_pre_print;
3067 source_cleanup_lines (args)
3070 struct source_cleanup_lines_args *p =
3071 (struct source_cleanup_lines_args *)args;
3072 source_line_number = p->old_line;
3073 source_file_name = p->old_file;
3074 source_pre_error = p->old_pre_error;
3075 error_pre_print = p->old_error_pre_print;
3080 source_command (args, from_tty)
3085 struct cleanup *old_cleanups;
3087 struct source_cleanup_lines_args old_lines;
3092 error ("source command requires pathname of file to source.");
3095 file = tilde_expand (file);
3096 old_cleanups = make_cleanup (free, file);
3098 stream = fopen (file, FOPEN_RT);
3101 perror_with_name (file);
3105 make_cleanup ((make_cleanup_func) fclose, stream);
3107 old_lines.old_line = source_line_number;
3108 old_lines.old_file = source_file_name;
3109 old_lines.old_pre_error = source_pre_error;
3110 old_lines.old_error_pre_print = error_pre_print;
3111 make_cleanup (source_cleanup_lines, &old_lines);
3112 source_line_number = 0;
3113 source_file_name = file;
3114 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
3115 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
3116 make_cleanup (free, source_pre_error);
3117 /* This will get set every time we read a line. So it won't stay "" for
3119 error_pre_print = "";
3121 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
3122 if (source_error_allocated < needed_length)
3124 source_error_allocated *= 2;
3125 if (source_error_allocated < needed_length)
3126 source_error_allocated = needed_length;
3127 if (source_error == NULL)
3128 source_error = xmalloc (source_error_allocated);
3130 source_error = xrealloc (source_error, source_error_allocated);
3133 read_command_file (stream);
3135 do_cleanups (old_cleanups);
3140 echo_command (text, from_tty)
3148 while ((c = *p++) != '\0')
3152 /* \ at end of argument is used after spaces
3153 so they won't be lost. */
3157 c = parse_escape (&p);
3159 printf_filtered ("%c", c);
3162 printf_filtered ("%c", c);
3165 /* Force this output to appear now. */
3167 gdb_flush (gdb_stdout);
3172 dont_repeat_command (ignored, from_tty)
3176 *line = 0; /* Can't call dont_repeat here because we're not
3177 necessarily reading from stdin. */
3180 /* Functions to manipulate command line editing control variables. */
3182 /* Number of commands to print in each call to show_commands. */
3183 #define Hist_print 10
3185 show_commands (args, from_tty)
3189 /* Index for history commands. Relative to history_base. */
3192 /* Number of the history entry which we are planning to display next.
3193 Relative to history_base. */
3196 /* The first command in the history which doesn't exist (i.e. one more
3197 than the number of the last command). Relative to history_base. */
3200 extern HIST_ENTRY *history_get PARAMS ((int));
3202 /* Print out some of the commands from the command history. */
3203 /* First determine the length of the history list. */
3204 hist_len = history_size;
3205 for (offset = 0; offset < history_size; offset++)
3207 if (!history_get (history_base + offset))
3216 if (args[0] == '+' && args[1] == '\0')
3217 /* "info editing +" should print from the stored position. */
3220 /* "info editing <exp>" should print around command number <exp>. */
3221 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
3223 /* "show commands" means print the last Hist_print commands. */
3226 num = hist_len - Hist_print;
3232 /* If there are at least Hist_print commands, we want to display the last
3233 Hist_print rather than, say, the last 6. */
3234 if (hist_len - num < Hist_print)
3236 num = hist_len - Hist_print;
3241 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
3243 printf_filtered ("%5d %s\n", history_base + offset,
3244 (history_get (history_base + offset))->line);
3247 /* The next command we want to display is the next one that we haven't
3251 /* If the user repeats this command with return, it should do what
3252 "show commands +" does. This is unnecessary if arg is null,
3253 because "show commands +" is not useful after "show commands". */
3254 if (from_tty && args)
3261 /* Called by do_setshow_command. */
3264 set_history_size_command (args, from_tty, c)
3267 struct cmd_list_element *c;
3269 if (history_size == INT_MAX)
3270 unstifle_history ();
3271 else if (history_size >= 0)
3272 stifle_history (history_size);
3275 history_size = INT_MAX;
3276 error ("History size must be non-negative");
3282 set_history (args, from_tty)
3286 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3287 help_list (sethistlist, "set history ", -1, gdb_stdout);
3292 show_history (args, from_tty)
3296 cmd_show_list (showhistlist, from_tty, "");
3299 int info_verbose = 0; /* Default verbose msgs off */
3301 /* Called by do_setshow_command. An elaborate joke. */
3304 set_verbose (args, from_tty, c)
3307 struct cmd_list_element *c;
3309 char *cmdname = "verbose";
3310 struct cmd_list_element *showcmd;
3312 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
3316 c->doc = "Set verbose printing of informational messages.";
3317 showcmd->doc = "Show verbose printing of informational messages.";
3321 c->doc = "Set verbosity.";
3322 showcmd->doc = "Show verbosity.";
3327 float_handler (signo)
3330 /* This message is based on ANSI C, section 4.7. Note that integer
3331 divide by zero causes this, so "float" is a misnomer. */
3332 signal (SIGFPE, float_handler);
3333 error ("Erroneous arithmetic operation.");
3345 enablebreaklist = NULL;
3350 showhistlist = NULL;
3351 unsethistlist = NULL;
3352 #if MAINTENANCE_CMDS
3353 maintenancelist = NULL;
3354 maintenanceinfolist = NULL;
3355 maintenanceprintlist = NULL;
3357 setprintlist = NULL;
3358 showprintlist = NULL;
3359 setchecklist = NULL;
3360 showchecklist = NULL;
3363 /* Init the history buffer. Note that we are called after the init file(s)
3364 * have been read so that the user can change the history file via his
3365 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3366 * overrides all of this.
3374 tmpenv = getenv ("HISTSIZE");
3376 history_size = atoi (tmpenv);
3377 else if (!history_size)
3380 stifle_history (history_size);
3382 tmpenv = getenv ("GDBHISTFILE");
3384 history_filename = savestring (tmpenv, strlen(tmpenv));
3385 else if (!history_filename) {
3386 /* We include the current directory so that if the user changes
3387 directories the file written will be the same as the one
3389 history_filename = concat (current_directory, "/.gdb_history", NULL);
3391 read_history (history_filename);
3397 struct cmd_list_element *c;
3399 #ifdef DEFAULT_PROMPT
3400 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
3402 prompt = savestring ("(gdb) ", 6);
3405 /* Set the important stuff up for command editing. */
3406 command_editing_p = 1;
3407 history_expansion_p = 0;
3408 write_history_p = 0;
3410 /* Setup important stuff for command line editing. */
3411 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
3412 rl_completer_word_break_characters = gdb_completer_word_break_characters;
3413 rl_completer_quote_characters = gdb_completer_quote_characters;
3414 rl_readline_name = "gdb";
3416 /* Define the classes of commands.
3417 They will appear in the help list in the reverse of this order. */
3419 add_cmd ("internals", class_maintenance, NO_FUNCTION,
3420 "Maintenance commands.\n\
3421 Some gdb commands are provided just for use by gdb maintainers.\n\
3422 These commands are subject to frequent change, and may not be as\n\
3423 well documented as user commands.",
3425 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
3426 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
3427 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
3428 The commands in this class are those defined by the user.\n\
3429 Use the \"define\" command to define a command.", &cmdlist);
3430 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
3431 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
3432 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
3433 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
3434 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
3435 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
3436 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3437 counting from zero for the innermost (currently executing) frame.\n\n\
3438 At any time gdb identifies one frame as the \"selected\" frame.\n\
3439 Variable lookups are done with respect to the selected frame.\n\
3440 When the program being debugged stops, gdb selects the innermost frame.\n\
3441 The commands below can be used to select other frames by number or address.",
3443 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
3445 add_com ("pwd", class_files, pwd_command,
3446 "Print working directory. This is used for your program as well.");
3447 c = add_cmd ("cd", class_files, cd_command,
3448 "Set working directory to DIR for debugger and program being debugged.\n\
3449 The change does not take effect for the program being debugged\n\
3450 until the next time it is started.", &cmdlist);
3451 c->completer = filename_completer;
3454 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
3459 add_com ("echo", class_support, echo_command,
3460 "Print a constant string. Give string as argument.\n\
3461 C escape sequences may be used in the argument.\n\
3462 No newline is added at the end of the argument;\n\
3463 use \"\\n\" if you want a newline to be printed.\n\
3464 Since leading and trailing whitespace are ignored in command arguments,\n\
3465 if you want to print some you must use \"\\\" before leading whitespace\n\
3466 to be printed or after trailing whitespace.");
3467 add_com ("document", class_support, document_command,
3468 "Document a user-defined command.\n\
3469 Give command name as argument. Give documentation on following lines.\n\
3470 End with a line of just \"end\".");
3471 add_com ("define", class_support, define_command,
3472 "Define a new command name. Command name is argument.\n\
3473 Definition appears on following lines, one command per line.\n\
3474 End with a line of just \"end\".\n\
3475 Use the \"document\" command to give documentation for the new command.\n\
3476 Commands defined in this way may have up to ten arguments.");
3479 c = add_cmd ("source", class_support, source_command,
3480 "Read commands from a file named FILE.\n\
3481 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
3482 when gdb is started.", &cmdlist);
3484 /* Punt file name, we can't help it easily. */
3485 c = add_cmd ("source", class_support, source_command,
3486 "Read commands from a file named FILE.\n\
3487 Note that the file \".gdbinit\" is read automatically in this way\n\
3488 when gdb is started.", &cmdlist);
3490 c->completer = filename_completer;
3492 add_com ("quit", class_support, quit_command, "Exit gdb.");
3493 add_com ("help", class_support, help_command, "Print list of commands.");
3494 add_com_alias ("q", "quit", class_support, 1);
3495 add_com_alias ("h", "help", class_support, 1);
3497 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
3498 Primarily used inside of user-defined commands that should not be repeated when\n\
3501 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
3504 add_show_from_set (c, &showlist);
3505 c->function.sfunc = set_verbose;
3506 set_verbose (NULL, 0, c);
3509 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
3510 "Set editing of command lines as they are typed.\n\
3511 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3512 Without an argument, command line editing is enabled. To edit, use\n\
3513 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
3516 add_prefix_cmd ("history", class_support, set_history,
3517 "Generic command for setting command history parameters.",
3518 &sethistlist, "set history ", 0, &setlist);
3519 add_prefix_cmd ("history", class_support, show_history,
3520 "Generic command for showing command history parameters.",
3521 &showhistlist, "show history ", 0, &showlist);
3524 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
3525 "Set history expansion on command input.\n\
3526 Without an argument, history expansion is enabled.", &sethistlist),
3530 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
3531 "Set saving of the history record on exit.\n\
3532 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
3533 Without an argument, saving is enabled.", &sethistlist),
3536 c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
3537 "Set the size of the command history, \n\
3538 ie. the number of previous commands to keep a record of.", &sethistlist);
3539 add_show_from_set (c, &showhistlist);
3540 c->function.sfunc = set_history_size_command;
3543 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
3544 "Set the filename in which to record the command history\n\
3545 (the list of previous commands of which a record is kept).", &sethistlist),
3549 (add_set_cmd ("confirm", class_support, var_boolean,
3551 "Set whether to confirm potentially dangerous operations.",
3555 add_prefix_cmd ("info", class_info, info_command,
3556 "Generic command for showing things about the program being debugged.",
3557 &infolist, "info ", 0, &cmdlist);
3558 add_com_alias ("i", "info", class_info, 1);
3560 add_com ("complete", class_obscure, complete_command,
3561 "List the completions for the rest of the line as a command.");
3563 add_prefix_cmd ("show", class_info, show_command,
3564 "Generic command for showing things about the debugger.",
3565 &showlist, "show ", 0, &cmdlist);
3566 /* Another way to get at the same thing. */
3567 add_info ("set", show_command, "Show all GDB settings.");
3569 add_cmd ("commands", no_class, show_commands,
3570 "Show the history of commands you typed.\n\
3571 You can supply a command number to start with, or a `+' to start after\n\
3572 the previous command number shown.",
3575 add_cmd ("version", no_class, show_version,
3576 "Show what version of GDB this is.", &showlist);
3578 add_com ("while", class_support, while_command,
3579 "Execute nested commands WHILE the conditional expression is non zero.\n\
3580 The conditional expression must follow the word `while' and must in turn be\n\
3581 followed by a new line. The nested commands must be entered one per line,\n\
3582 and should be terminated by the word `end'.");
3584 add_com ("if", class_support, if_command,
3585 "Execute nested commands once IF the conditional expression is non zero.\n\
3586 The conditional expression must follow the word `if' and must in turn be\n\
3587 followed by a new line. The nested commands must be entered one per line,\n\
3588 and should be terminated by the word 'else' or `end'. If an else clause\n\
3589 is used, the same rules apply to its nested commands as to the first ones.");
3591 /* If target is open when baud changes, it doesn't take effect until the
3592 next open (I think, not sure). */
3593 add_show_from_set (add_set_cmd ("remotebaud", no_class,
3594 var_zinteger, (char *)&baud_rate,
3595 "Set baud rate for remote serial I/O.\n\
3596 This value is used to set the speed of the serial port when debugging\n\
3597 using remote targets.", &setlist),
3601 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug,
3602 "Set debugging of remote protocol.\n\
3603 When enabled, each packet sent or received with the remote target\n\
3604 is displayed.", &setlist),
3608 add_set_cmd ("remotetimeout", no_class, var_integer, (char *)&remote_timeout,
3609 "Set timeout limit to wait for target to respond.\n\
3610 This value is used to set the time limit for gdb to wait for a response\n\
3611 from he target.", &setlist),
3614 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
3615 (char *)&annotation_level, "Set annotation_level.\n\
3616 0 == normal; 1 == fullname (for use when running under emacs)\n\
3617 2 == output annotated suitably for use by programs that control GDB.",
3619 c = add_show_from_set (c, &showlist);