1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
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 */
42 /* readline defines this. */
45 #include <sys/types.h>
50 #include "gdb_string.h"
54 #include <sys/param.h>
58 extern void initialize_targets PARAMS ((void));
60 extern void initialize_utils PARAMS ((void));
62 /* Prototypes for local functions */
64 static char * line_completion_function PARAMS ((char *, int, char *, int));
66 static char * readline_line_completion_function PARAMS ((char *, int));
68 static void command_loop_marker PARAMS ((int));
70 static void while_command PARAMS ((char *, int));
72 static void if_command PARAMS ((char *, int));
74 static struct command_line *
75 build_command_line PARAMS ((enum command_control_type, char *));
77 static struct command_line *
78 get_command_line PARAMS ((enum command_control_type, char *));
80 static void realloc_body_list PARAMS ((struct command_line *, int));
82 static enum misc_command_type read_next_line PARAMS ((struct command_line **));
84 static enum command_control_type
85 recurse_read_control_structure PARAMS ((struct command_line *));
87 static struct cleanup * setup_user_args PARAMS ((char *));
89 static char * locate_arg PARAMS ((char *));
91 static char * insert_args PARAMS ((char *));
93 static void arg_cleanup PARAMS ((void));
95 static void init_main PARAMS ((void));
97 static void init_cmd_lists PARAMS ((void));
99 static void float_handler PARAMS ((int));
101 static void init_signals PARAMS ((void));
103 static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
105 #ifdef TARGET_BYTE_ORDER_SELECTABLE
107 static void set_endian PARAMS ((char *, int));
109 static void set_endian_big PARAMS ((char *, int));
111 static void set_endian_little PARAMS ((char *, int));
113 static void set_endian_auto PARAMS ((char *, int));
115 static void show_endian PARAMS ((char *, int));
119 static void show_history PARAMS ((char *, int));
121 static void set_history PARAMS ((char *, int));
123 static void set_history_size_command PARAMS ((char *, int,
124 struct cmd_list_element *));
126 static void show_commands PARAMS ((char *, int));
128 static void echo_command PARAMS ((char *, int));
130 static void pwd_command PARAMS ((char *, int));
132 static void show_version PARAMS ((char *, int));
134 static void document_command PARAMS ((char *, int));
136 static void define_command PARAMS ((char *, int));
138 static void validate_comname PARAMS ((char *));
140 static void help_command PARAMS ((char *, int));
142 static void show_command PARAMS ((char *, int));
144 static void info_command PARAMS ((char *, int));
146 static void complete_command PARAMS ((char *, int));
148 static void do_nothing PARAMS ((int));
150 static int quit_cover PARAMS ((char *));
152 static void disconnect PARAMS ((int));
154 static void source_cleanup PARAMS ((FILE *));
156 /* If this definition isn't overridden by the header files, assume
157 that isatty and fileno exist on this system. */
159 #define ISATTY(FP) (isatty (fileno (FP)))
162 /* Initialization file name for gdb. This is overridden in some configs. */
164 #ifndef GDBINIT_FILENAME
165 #define GDBINIT_FILENAME ".gdbinit"
167 char gdbinit[] = GDBINIT_FILENAME;
169 int inhibit_gdbinit = 0;
171 /* If nonzero, and GDB has been configured to be able to use windows,
172 attempt to open them upon startup. */
176 /* Version number of GDB, as a string. */
178 extern char *version;
180 /* Canonical host name as a string. */
182 extern char *host_name;
184 /* Canonical target name as a string. */
186 extern char *target_name;
188 extern char lang_frame_mismatch_warn[]; /* language.c */
190 /* Flag for whether we want all the "from_tty" gubbish printed. */
192 int caution = 1; /* Default is yes, sigh. */
194 /* Define all cmd_list_elements. */
196 /* Chain containing all defined commands. */
198 struct cmd_list_element *cmdlist;
200 /* Chain containing all defined info subcommands. */
202 struct cmd_list_element *infolist;
204 /* Chain containing all defined enable subcommands. */
206 struct cmd_list_element *enablelist;
208 /* Chain containing all defined disable subcommands. */
210 struct cmd_list_element *disablelist;
212 /* Chain containing all defined delete subcommands. */
214 struct cmd_list_element *deletelist;
216 /* Chain containing all defined "enable breakpoint" subcommands. */
218 struct cmd_list_element *enablebreaklist;
220 /* Chain containing all defined set subcommands */
222 struct cmd_list_element *setlist;
224 /* Chain containing all defined unset subcommands */
226 struct cmd_list_element *unsetlist;
228 /* Chain containing all defined show subcommands. */
230 struct cmd_list_element *showlist;
232 #ifdef TARGET_BYTE_ORDER_SELECTABLE
233 /* Chain containing the \"set endian\" commands. */
235 struct cmd_list_element *endianlist;
238 /* Chain containing all defined \"set history\". */
240 struct cmd_list_element *sethistlist;
242 /* Chain containing all defined \"show history\". */
244 struct cmd_list_element *showhistlist;
246 /* Chain containing all defined \"unset history\". */
248 struct cmd_list_element *unsethistlist;
250 /* Chain containing all defined maintenance subcommands. */
253 struct cmd_list_element *maintenancelist;
256 /* Chain containing all defined "maintenance info" subcommands. */
259 struct cmd_list_element *maintenanceinfolist;
262 /* Chain containing all defined "maintenance print" subcommands. */
265 struct cmd_list_element *maintenanceprintlist;
268 struct cmd_list_element *setprintlist;
270 struct cmd_list_element *showprintlist;
272 struct cmd_list_element *setchecklist;
274 struct cmd_list_element *showchecklist;
276 /* stdio stream that command input is being read from. Set to stdin normally.
277 Set by source_command to the file we are sourcing. Set to NULL if we are
278 executing a user-defined command or interacting via a GUI. */
282 /* Current working directory. */
284 char *current_directory;
286 /* The directory name is actually stored here (usually). */
287 char gdb_dirbuf[1024];
289 /* Function to call before reading a command, if nonzero.
290 The function receives two args: an input stream,
291 and a prompt string. */
293 void (*window_hook) PARAMS ((FILE *, char *));
298 /* gdb prints this when reading a command interactively */
301 /* Buffer used for reading command lines, and the size
302 allocated for it so far. */
307 /* Nonzero if the current command is modified by "server ". This
308 affects things like recording into the command history, comamnds
309 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
310 whatever) can issue its own commands and also send along commands
311 from the user, and have the user not notice that the user interface
312 is issuing commands too. */
315 /* Baud rate specified for talking to serial target systems. Default
316 is left as -1, so targets can choose their own defaults. */
317 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
318 or (unsigned int)-1. This is a Bad User Interface. */
322 /* Timeout limit for response from target. */
324 int remote_timeout = 20; /* Set default to 20 */
326 /* Non-zero tells remote* modules to output debugging info. */
328 int remote_debug = 0;
330 /* Level of control structure. */
331 static int control_level;
333 /* Structure for arguments to user defined functions. */
334 #define MAXUSERARGS 10
337 struct user_args *next;
346 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
350 #define STOP_SIGNAL SIGTSTP
351 static void stop_sig PARAMS ((int));
355 /* Some System V have job control but not sigsetmask(). */
356 #if !defined (HAVE_SIGSETMASK)
358 #define HAVE_SIGSETMASK 1
360 #define HAVE_SIGSETMASK 0
364 #if 0 == (HAVE_SIGSETMASK)
365 #define sigsetmask(n)
368 /* Hooks for alternate command interfaces. */
370 /* Called after most modules have been initialized, but before taking users
373 void (*init_ui_hook) PARAMS ((void));
375 /* Called instead of command_loop at top level. Can be invoked via
376 return_to_top_level. */
378 void (*command_loop_hook) PARAMS ((void));
381 /* Called instead of fputs for all output. */
383 void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer, FILE *stream));
385 /* Called when the target says something to the host, which may
386 want to appear in a different window. */
388 void (*target_output_hook) PARAMS ((char *));
390 /* Called from print_frame_info to list the line we stopped in. */
392 void (*print_frame_info_listing_hook) PARAMS ((struct symtab *s, int line,
393 int stopline, int noerror));
394 /* Replaces most of query. */
396 int (*query_hook) PARAMS (());
398 /* Called from gdb_flush to flush output. */
400 void (*flush_hook) PARAMS ((FILE *stream));
402 /* These three functions support getting lines of text from the user. They
403 are used in sequence. First readline_begin_hook is called with a text
404 string that might be (for example) a message for the user to type in a
405 sequence of commands to be executed at a breakpoint. If this function
406 calls back to a GUI, it might take this opportunity to pop up a text
407 interaction window with this message. Next, readline_hook is called
408 with a prompt that is emitted prior to collecting the user input.
409 It can be called multiple times. Finally, readline_end_hook is called
410 to notify the GUI that we are done with the interaction window and it
413 void (*readline_begin_hook) PARAMS ((char *, ...));
414 char * (*readline_hook) PARAMS ((char *));
415 void (*readline_end_hook) PARAMS ((void));
417 /* Called as appropriate to notify the interface of the specified breakpoint
420 void (*create_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
421 void (*delete_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
422 void (*modify_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
424 /* Called during long calculations to allow GUI to repair window damage, and to
425 check for stop buttons, etc... */
427 void (*interactive_hook) PARAMS ((void));
429 /* Called when the registers have changed, as a hint to a GUI
430 to minimize window update. */
432 void (*registers_changed_hook) PARAMS ((void));
434 /* Called when going to wait for the target. Usually allows the GUI to run
435 while waiting for target events. */
437 int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus *status));
439 /* Used by UI as a wrapper around command execution. May do various things
440 like enabling/disabling buttons, etc... */
442 void (*call_command_hook) PARAMS ((struct cmd_list_element *c, char *cmd,
446 /* Takes control from error (). Typically used to prevent longjmps out of the
447 middle of the GUI. Usually used in conjunction with a catch routine. */
449 NORETURN void (*error_hook) PARAMS (()) ATTR_NORETURN;
452 /* Where to go for return_to_top_level (RETURN_ERROR). */
453 jmp_buf error_return;
454 /* Where to go for return_to_top_level (RETURN_QUIT). */
457 /* Return for reason REASON. This generally gets back to the command
458 loop, but can be caught via catch_errors. */
461 return_to_top_level (reason)
462 enum return_reason reason;
467 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
468 I can think of a reason why that is vital, though). */
469 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
471 disable_current_display ();
472 do_cleanups (ALL_CLEANUPS);
474 if (annotation_level > 1)
485 (NORETURN void) longjmp
486 (reason == RETURN_ERROR ? error_return : quit_return, 1);
489 /* Call FUNC with arg ARGS, catching any errors. If there is no
490 error, return the value returned by FUNC. If there is an error,
491 print ERRSTRING, print the specific error message, then return
494 Must not be called with immediate_quit in effect (bad things might
495 happen, say we got a signal in the middle of a memcpy to quit_return).
496 This is an OK restriction; with very few exceptions immediate_quit can
497 be replaced by judicious use of QUIT.
499 MASK specifies what to catch; it is normally set to
500 RETURN_MASK_ALL, if for no other reason than that the code which
501 calls catch_errors might not be set up to deal with a quit which
502 isn't caught. But if the code can deal with it, it generally
503 should be RETURN_MASK_ERROR, unless for some reason it is more
504 useful to abort only the portion of the operation inside the
505 catch_errors. Note that quit should return to the command line
506 fairly quickly, even if some further processing is being done. */
509 catch_errors (func, args, errstring, mask)
510 int (*func) PARAMS ((char *));
519 struct cleanup *saved_cleanup_chain;
520 char *saved_error_pre_print;
521 char *saved_quit_pre_print;
523 saved_cleanup_chain = save_cleanups ();
524 saved_error_pre_print = error_pre_print;
525 saved_quit_pre_print = quit_pre_print;
527 if (mask & RETURN_MASK_ERROR)
529 memcpy ((char *)saved_error, (char *)error_return, sizeof (jmp_buf));
530 error_pre_print = errstring;
532 if (mask & RETURN_MASK_QUIT)
534 memcpy (saved_quit, quit_return, sizeof (jmp_buf));
535 quit_pre_print = errstring;
538 if (setjmp (tmp_jmp) == 0)
540 if (mask & RETURN_MASK_ERROR)
541 memcpy (error_return, tmp_jmp, sizeof (jmp_buf));
542 if (mask & RETURN_MASK_QUIT)
543 memcpy (quit_return, tmp_jmp, sizeof (jmp_buf));
544 val = (*func) (args);
549 restore_cleanups (saved_cleanup_chain);
551 if (mask & RETURN_MASK_ERROR)
553 memcpy (error_return, saved_error, sizeof (jmp_buf));
554 error_pre_print = saved_error_pre_print;
556 if (mask & RETURN_MASK_QUIT)
558 memcpy (quit_return, saved_quit, sizeof (jmp_buf));
559 quit_pre_print = saved_quit_pre_print;
564 /* Handler for SIGHUP. */
570 catch_errors (quit_cover, NULL,
571 "Could not kill the program being debugged", RETURN_MASK_ALL);
572 signal (SIGHUP, SIG_DFL);
573 kill (getpid (), SIGHUP);
576 /* Just a little helper function for disconnect(). */
582 caution = 0; /* Throw caution to the wind -- we're exiting.
583 This prevents asking the user dumb questions. */
584 quit_command((char *)0, 0);
588 /* Line number we are currently in in a file which is being sourced. */
589 static int source_line_number;
591 /* Name of the file we are sourcing. */
592 static char *source_file_name;
594 /* Buffer containing the error_pre_print used by the source stuff.
596 static char *source_error;
597 static int source_error_allocated;
599 /* Something to glom on to the start of error_pre_print if source_file_name
601 static char *source_pre_error;
603 /* Clean up on error during a "source" command (or execution of a
604 user-defined command). */
607 source_cleanup (stream)
610 /* Restore the previous input stream. */
614 /* Read commands from STREAM. */
616 read_command_file (stream)
619 struct cleanup *cleanups;
621 cleanups = make_cleanup (source_cleanup, instream);
624 do_cleanups (cleanups);
627 extern void init_proc ();
629 void (*pre_init_ui_hook) PARAMS ((void));
634 if (pre_init_ui_hook)
637 /* Run the init function of each source file */
639 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
640 current_directory = gdb_dirbuf;
642 init_cmd_lists (); /* This needs to be done first */
643 initialize_targets (); /* Setup target_terminal macros for utils.c */
644 initialize_utils (); /* Make errors and warnings possible */
645 initialize_all_files ();
646 init_main (); /* But that omits this file! Do it now */
651 /* We need a default language for parsing expressions, so simple things like
652 "set width 0" won't fail if no language is explicitly set in a config file
653 or implicitly set by reading an executable during startup. */
654 set_language (language_c);
655 expected_language = current_language; /* don't warn about the change. */
661 /* Allocate, initialize a new command line structure for one of the
662 control commands (if/while). */
664 static struct command_line *
665 build_command_line (type, args)
666 enum command_control_type type;
669 struct command_line *cmd;
672 error ("if/while commands require arguments.\n");
674 cmd = (struct command_line *)xmalloc (sizeof (struct command_line));
676 cmd->control_type = type;
680 = (struct command_line **)xmalloc (sizeof (struct command_line *)
682 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
683 cmd->line = savestring (args, strlen (args));
687 /* Build and return a new command structure for the control commands
688 such as "if" and "while". */
690 static struct command_line *
691 get_command_line (type, arg)
692 enum command_control_type type;
695 struct command_line *cmd;
696 struct cleanup *old_chain = NULL;
698 /* Allocate and build a new command line structure. */
699 cmd = build_command_line (type, arg);
701 old_chain = make_cleanup (free_command_lines, &cmd);
703 /* Read in the body of this command. */
704 if (recurse_read_control_structure (cmd) == invalid_control)
706 warning ("error reading in control structure\n");
707 do_cleanups (old_chain);
711 discard_cleanups (old_chain);
715 /* Recursively print a command (including full control structures). */
717 print_command_line (cmd, depth)
718 struct command_line *cmd;
725 for (i = 0; i < depth; i++)
726 fputs_filtered (" ", gdb_stdout);
729 /* A simple command, print it and return. */
730 if (cmd->control_type == simple_control)
732 fputs_filtered (cmd->line, gdb_stdout);
733 fputs_filtered ("\n", gdb_stdout);
737 /* loop_continue to jump to the start of a while loop, print it
739 if (cmd->control_type == continue_control)
741 fputs_filtered ("loop_continue\n", gdb_stdout);
745 /* loop_break to break out of a while loop, print it and return. */
746 if (cmd->control_type == break_control)
748 fputs_filtered ("loop_break\n", gdb_stdout);
752 /* A while command. Recursively print its subcommands before returning. */
753 if (cmd->control_type == while_control)
755 struct command_line *list;
756 fputs_filtered ("while ", gdb_stdout);
757 fputs_filtered (cmd->line, gdb_stdout);
758 fputs_filtered ("\n", gdb_stdout);
759 list = *cmd->body_list;
762 print_command_line (list, depth + 1);
767 /* An if command. Recursively print both arms before returning. */
768 if (cmd->control_type == if_control)
770 fputs_filtered ("if ", gdb_stdout);
771 fputs_filtered (cmd->line, gdb_stdout);
772 fputs_filtered ("\n", gdb_stdout);
774 print_command_line (cmd->body_list[0], depth + 1);
776 /* Show the false arm if it exists. */
777 if (cmd->body_count == 2)
781 for (i = 0; i < depth; i++)
782 fputs_filtered (" ", gdb_stdout);
784 fputs_filtered ("else\n", gdb_stdout);
785 print_command_line (cmd->body_list[1], depth + 1);
789 for (i = 0; i < depth; i++)
790 fputs_filtered (" ", gdb_stdout);
792 fputs_filtered ("end\n", gdb_stdout);
796 /* Execute the command in CMD. */
798 enum command_control_type
799 execute_control_command (cmd)
800 struct command_line *cmd;
802 struct expression *expr;
803 struct command_line *current;
804 struct cleanup *old_chain = 0;
807 enum command_control_type ret;
810 switch (cmd->control_type)
813 /* A simple command, execute it and return. */
814 new_line = insert_args (cmd->line);
816 return invalid_control;
817 old_chain = make_cleanup (free_current_contents, &new_line);
818 execute_command (new_line, 0);
819 ret = cmd->control_type;
822 case continue_control:
824 /* Return for "continue", and "break" so we can either
825 continue the loop at the top, or break out. */
826 ret = cmd->control_type;
831 /* Parse the loop control expression for the while statement. */
832 new_line = insert_args (cmd->line);
834 return invalid_control;
835 old_chain = make_cleanup (free_current_contents, &new_line);
836 expr = parse_expression (new_line);
837 make_cleanup (free_current_contents, &expr);
839 ret = simple_control;
842 /* Keep iterating so long as the expression is true. */
847 /* Evaluate the expression. */
848 val = evaluate_expression (expr);
850 /* If the value is false, then break out of the loop. */
851 if (!value_true (val))
854 /* Execute the body of the while statement. */
855 current = *cmd->body_list;
858 ret = execute_control_command (current);
860 /* If we got an error, or a "break" command, then stop
862 if (ret == invalid_control || ret == break_control)
868 /* If we got a "continue" command, then restart the loop
870 if (ret == continue_control)
873 /* Get the next statement. */
874 current = current->next;
878 /* Reset RET so that we don't recurse the break all the way down. */
879 if (ret == break_control)
880 ret = simple_control;
887 new_line = insert_args (cmd->line);
889 return invalid_control;
890 old_chain = make_cleanup (free_current_contents, &new_line);
891 /* Parse the conditional for the if statement. */
892 expr = parse_expression (new_line);
893 make_cleanup (free_current_contents, &expr);
896 ret = simple_control;
898 /* Evaluate the conditional. */
899 val = evaluate_expression (expr);
901 /* Choose which arm to take commands from based on the value of the
902 conditional expression. */
903 if (value_true (val))
904 current = *cmd->body_list;
905 else if (cmd->body_count == 2)
906 current = *(cmd->body_list + 1);
908 /* Execute commands in the given arm. */
911 ret = execute_control_command (current);
913 /* If we got an error, get out. */
914 if (ret != simple_control)
917 /* Get the next statement in the body. */
918 current = current->next;
925 warning ("Invalid control type in command structure.");
926 return invalid_control;
930 do_cleanups (old_chain);
935 /* "while" command support. Executes a body of statements while the
936 loop condition is nonzero. */
939 while_command (arg, from_tty)
943 struct command_line *command = NULL;
946 command = get_command_line (while_control, arg);
951 execute_control_command (command);
952 free_command_lines (&command);
955 /* "if" command support. Execute either the true or false arm depending
956 on the value of the if conditional. */
959 if_command (arg, from_tty)
963 struct command_line *command = NULL;
966 command = get_command_line (if_control, arg);
971 execute_control_command (command);
972 free_command_lines (&command);
979 struct user_args *oargs = user_args;
981 fatal ("Internal error, arg_cleanup called with no user args.\n");
983 user_args = user_args->next;
987 /* Bind the incomming arguments for a user defined command to
988 $arg0, $arg1 ... $argMAXUSERARGS. */
990 static struct cleanup *
994 struct user_args *args;
995 struct cleanup *old_chain;
996 unsigned int arg_count = 0;
998 args = (struct user_args *)xmalloc (sizeof (struct user_args));
999 memset (args, 0, sizeof (struct user_args));
1001 args->next = user_args;
1004 old_chain = make_cleanup (arg_cleanup, 0);
1013 if (arg_count >= MAXUSERARGS)
1015 error ("user defined function may only have %d arguments.\n",
1020 /* Strip whitespace. */
1021 while (*p == ' ' || *p == '\t')
1024 /* P now points to an argument. */
1026 user_args->a[arg_count].arg = p;
1028 /* Get to the end of this argument. */
1029 while (*p && *p != ' ' && *p != '\t')
1032 user_args->a[arg_count].len = p - start_arg;
1039 /* Given character string P, return a point to the first argument ($arg),
1040 or NULL if P contains no arguments. */
1046 while ((p = strchr (p, '$')))
1048 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
1055 /* Insert the user defined arguments stored in user_arg into the $arg
1056 arguments found in line, with the updated copy being placed into nline. */
1062 char *p, *save_line, *new_line;
1065 /* First we need to know how much memory to allocate for the new line. */
1068 while ((p = locate_arg (line)))
1073 if (i >= user_args->count)
1075 error ("Missing argument %d in user function.\n", i);
1078 len += user_args->a[i].len;
1082 /* Don't forget the tail. */
1083 len += strlen (line);
1085 /* Allocate space for the new line and fill it in. */
1086 new_line = (char *)xmalloc (len + 1);
1087 if (new_line == NULL)
1090 /* Restore pointer to beginning of old line. */
1093 /* Save pointer to beginning of new line. */
1094 save_line = new_line;
1096 while ((p = locate_arg (line)))
1100 memcpy (new_line, line, p - line);
1101 new_line += p - line;
1104 len = user_args->a[i].len;
1107 memcpy (new_line, user_args->a[i].arg, len);
1112 /* Don't forget the tail. */
1113 strcpy (new_line, line);
1115 /* Return a pointer to the beginning of the new line. */
1120 execute_user_command (c, args)
1121 struct cmd_list_element *c;
1124 register struct command_line *cmdlines;
1125 struct cleanup *old_chain;
1126 enum command_control_type ret;
1128 old_chain = setup_user_args (args);
1130 cmdlines = c->user_commands;
1135 /* Set the instream to 0, indicating execution of a
1136 user-defined function. */
1137 old_chain = make_cleanup (source_cleanup, instream);
1138 instream = (FILE *) 0;
1141 ret = execute_control_command (cmdlines);
1142 if (ret != simple_control && ret != break_control)
1144 warning ("Error in control structure.\n");
1147 cmdlines = cmdlines->next;
1149 do_cleanups (old_chain);
1152 /* Execute the line P as a command.
1153 Pass FROM_TTY as second argument to the defining function. */
1156 execute_command (p, from_tty)
1160 register struct cmd_list_element *c;
1161 register enum language flang;
1162 static int warned = 0;
1163 extern FILE *serial_logfp;
1167 /* This can happen when command_line_input hits end of file. */
1171 if (serial_logfp != NULL)
1172 serial_log_command (p);
1174 while (*p == ' ' || *p == '\t') p++;
1179 c = lookup_cmd (&p, cmdlist, "", 0, 1);
1180 /* Pass null arg rather than an empty one. */
1183 /* Clear off trailing whitespace, except for set and complete command. */
1184 if (arg && c->type != set_cmd && c->function.cfunc != complete_command)
1186 p = arg + strlen (arg) - 1;
1187 while (p >= arg && (*p == ' ' || *p == '\t'))
1192 /* If this command has been hooked, run the hook first. */
1194 execute_user_command (c->hook, (char *)0);
1196 if (c->class == class_user)
1197 execute_user_command (c, arg);
1198 else if (c->type == set_cmd || c->type == show_cmd)
1199 do_setshow_command (arg, from_tty & caution, c);
1200 else if (c->function.cfunc == NO_FUNCTION)
1201 error ("That is not a command, just a help topic.");
1202 else if (call_command_hook)
1203 call_command_hook (c, arg, from_tty & caution);
1205 (*c->function.cfunc) (arg, from_tty & caution);
1208 /* Tell the user if the language has changed (except first time). */
1209 if (current_language != expected_language)
1211 if (language_mode == language_mode_auto) {
1212 language_info (1); /* Print what changed. */
1217 /* Warn the user if the working language does not match the
1218 language of the current frame. Only warn the user if we are
1219 actually running the program, i.e. there is a stack. */
1220 /* FIXME: This should be cacheing the frame and only running when
1221 the frame changes. */
1223 if (target_has_stack)
1225 flang = get_frame_language ();
1227 && flang != language_unknown
1228 && flang != current_language->la_language)
1230 printf_filtered ("%s\n", lang_frame_mismatch_warn);
1238 command_loop_marker (foo)
1243 /* Read commands from `instream' and execute them
1244 until end of file or error reading instream. */
1249 struct cleanup *old_chain;
1251 int stdin_is_tty = ISATTY (stdin);
1252 long time_at_cmd_start;
1254 long space_at_cmd_start;
1256 extern int display_time;
1257 extern int display_space;
1259 while (instream && !feof (instream))
1261 if (window_hook && instream == stdin)
1262 (*window_hook) (instream, prompt);
1265 if (instream == stdin && stdin_is_tty)
1266 reinitialize_more_filter ();
1267 old_chain = make_cleanup (command_loop_marker, 0);
1268 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
1269 instream == stdin, "prompt");
1273 time_at_cmd_start = get_run_time ();
1278 extern char **environ;
1279 char *lim = (char *) sbrk (0);
1281 space_at_cmd_start = (long) (lim - (char *) &environ);
1285 execute_command (command, instream == stdin);
1286 /* Do any commands attached to breakpoint we stopped at. */
1287 bpstat_do_actions (&stop_bpstat);
1288 do_cleanups (old_chain);
1292 long cmd_time = get_run_time () - time_at_cmd_start;
1294 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1295 cmd_time / 1000000, cmd_time % 1000000);
1301 extern char **environ;
1302 char *lim = (char *) sbrk (0);
1303 long space_now = lim - (char *) &environ;
1304 long space_diff = space_now - space_at_cmd_start;
1306 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1308 (space_diff >= 0 ? '+' : '-'),
1315 /* Commands call this if they do not want to be repeated by null lines. */
1323 /* If we aren't reading from standard input, we are saving the last
1324 thing read from stdin in line and don't want to delete it. Null lines
1325 won't repeat here in any case. */
1326 if (instream == stdin)
1330 /* Read a line from the stream "instream" without command line editing.
1332 It prints PRROMPT once at the start.
1333 Action is compatible with "readline", e.g. space for the result is
1334 malloc'd and should be freed by the caller.
1336 A NULL return means end of file. */
1338 gdb_readline (prrompt)
1343 int input_index = 0;
1344 int result_size = 80;
1348 /* Don't use a _filtered function here. It causes the assumed
1349 character position to be off, since the newline we read from
1350 the user is not accounted for. */
1351 fputs_unfiltered (prrompt, gdb_stdout);
1353 /* Move to a new line so the entered line doesn't have a prompt
1354 on the front of it. */
1355 fputs_unfiltered ("\n", gdb_stdout);
1357 gdb_flush (gdb_stdout);
1360 result = (char *) xmalloc (result_size);
1364 /* Read from stdin if we are executing a user defined command.
1365 This is the right thing for prompt_for_continue, at least. */
1366 c = fgetc (instream ? instream : stdin);
1370 if (input_index > 0)
1371 /* The last line does not end with a newline. Return it, and
1372 if we are called again fgetc will still return EOF and
1373 we'll return NULL then. */
1382 result[input_index++] = c;
1383 while (input_index >= result_size)
1386 result = (char *) xrealloc (result, result_size);
1390 result[input_index++] = '\0';
1394 /* Variables which control command line editing and history
1395 substitution. These variables are given default values at the end
1397 static int command_editing_p;
1398 static int history_expansion_p;
1399 static int write_history_p;
1400 static int history_size;
1401 static char *history_filename;
1403 /* readline uses the word breaks for two things:
1404 (1) In figuring out where to point the TEXT parameter to the
1405 rl_completion_entry_function. Since we don't use TEXT for much,
1406 it doesn't matter a lot what the word breaks are for this purpose, but
1407 it does affect how much stuff M-? lists.
1408 (2) If one of the matches contains a word break character, readline
1409 will quote it. That's why we switch between
1410 gdb_completer_word_break_characters and
1411 gdb_completer_command_word_break_characters. I'm not sure when
1412 we need this behavior (perhaps for funky characters in C++ symbols?). */
1414 /* Variables which are necessary for fancy command line editing. */
1415 char *gdb_completer_word_break_characters =
1416 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1418 /* When completing on command names, we remove '-' from the list of
1419 word break characters, since we use it in command names. If the
1420 readline library sees one in any of the current completion strings,
1421 it thinks that the string needs to be quoted and automatically supplies
1423 char *gdb_completer_command_word_break_characters =
1424 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1426 /* Characters that can be used to quote completion strings. Note that we
1427 can't include '"' because the gdb C parser treats such quoted sequences
1429 char *gdb_completer_quote_characters =
1432 /* Functions that are used as part of the fancy command line editing. */
1434 /* This can be used for functions which don't want to complete on symbols
1435 but don't want to complete on anything else either. */
1438 noop_completer (text, prefix)
1445 /* Complete on filenames. */
1447 filename_completer (text, word)
1451 /* From readline. */
1452 extern char *filename_completion_function ();
1453 int subsequent_name;
1455 int return_val_used;
1456 int return_val_alloced;
1458 return_val_used = 0;
1459 /* Small for testing. */
1460 return_val_alloced = 1;
1461 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1463 subsequent_name = 0;
1467 p = filename_completion_function (text, subsequent_name);
1468 if (return_val_used >= return_val_alloced)
1470 return_val_alloced *= 2;
1472 (char **) xrealloc (return_val,
1473 return_val_alloced * sizeof (char *));
1477 return_val[return_val_used++] = p;
1480 /* Like emacs, don't complete on old versions. Especially useful
1481 in the "source" command. */
1482 if (p[strlen (p) - 1] == '~')
1488 /* Return exactly p. */
1489 return_val[return_val_used++] = p;
1490 else if (word > text)
1492 /* Return some portion of p. */
1493 q = xmalloc (strlen (p) + 5);
1494 strcpy (q, p + (word - text));
1495 return_val[return_val_used++] = q;
1500 /* Return some of TEXT plus p. */
1501 q = xmalloc (strlen (p) + (text - word) + 5);
1502 strncpy (q, word, text - word);
1503 q[text - word] = '\0';
1505 return_val[return_val_used++] = q;
1509 subsequent_name = 1;
1512 /* There is no way to do this just long enough to affect quote inserting
1513 without also affecting the next completion. This should be fixed in
1515 /* Insure that readline does the right thing
1516 with respect to inserting quotes. */
1517 rl_completer_word_break_characters = "";
1522 /* Here are some useful test cases for completion. FIXME: These should
1523 be put in the test suite. They should be tested with both M-? and TAB.
1525 "show output-" "radix"
1526 "show output" "-radix"
1527 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1528 "p " ambiguous (all symbols)
1529 "info t foo" no completions
1530 "info t " no completions
1531 "info t" ambiguous ("info target", "info terminal", etc.)
1532 "info ajksdlfk" no completions
1533 "info ajksdlfk " no completions
1535 "info " ambiguous (all info commands)
1536 "p \"a" no completions (string constant)
1537 "p 'a" ambiguous (all symbols starting with a)
1538 "p b-a" ambiguous (all symbols starting with a)
1539 "p b-" ambiguous (all symbols)
1540 "file Make" "file" (word break hard to screw up here)
1541 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1544 /* Generate completions one by one for the completer. Each time we are
1545 called return another potential completion to the caller.
1546 line_completion just completes on commands or passes the buck to the
1547 command's completer function, the stuff specific to symbol completion
1548 is in make_symbol_completion_list.
1550 TEXT is the caller's idea of the "word" we are looking at.
1552 MATCHES is the number of matches that have currently been collected from
1553 calling this completion function. When zero, then we need to initialize,
1554 otherwise the initialization has already taken place and we can just
1555 return the next potential completion string.
1557 LINE_BUFFER is available to be looked at; it contains the entire text
1558 of the line. POINT is the offset in that line of the cursor. You
1559 should pretend that the line ends at POINT.
1561 Returns NULL if there are no more completions, else a pointer to a string
1562 which is a possible completion, it is the caller's responsibility to
1566 line_completion_function (text, matches, line_buffer, point)
1572 static char **list = (char **)NULL; /* Cache of completions */
1573 static int index; /* Next cached completion */
1574 char *output = NULL;
1575 char *tmp_command, *p;
1576 /* Pointer within tmp_command which corresponds to text. */
1578 struct cmd_list_element *c, *result_list;
1582 /* The caller is beginning to accumulate a new set of completions, so
1583 we need to find all of them now, and cache them for returning one at
1584 a time on future calls. */
1588 /* Free the storage used by LIST, but not by the strings inside.
1589 This is because rl_complete_internal () frees the strings. */
1595 /* Choose the default set of word break characters to break completions.
1596 If we later find out that we are doing completions on command strings
1597 (as opposed to strings supplied by the individual command completer
1598 functions, which can be any string) then we will switch to the
1599 special word break set for command strings, which leaves out the
1600 '-' character used in some commands. */
1602 rl_completer_word_break_characters =
1603 gdb_completer_word_break_characters;
1605 /* Decide whether to complete on a list of gdb commands or on symbols. */
1606 tmp_command = (char *) alloca (point + 1);
1609 strncpy (tmp_command, line_buffer, point);
1610 tmp_command[point] = '\0';
1611 /* Since text always contains some number of characters leading up
1612 to point, we can find the equivalent position in tmp_command
1613 by subtracting that many characters from the end of tmp_command. */
1614 word = tmp_command + point - strlen (text);
1618 /* An empty line we want to consider ambiguous; that is, it
1619 could be any command. */
1620 c = (struct cmd_list_element *) -1;
1625 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1628 /* Move p up to the next interesting thing. */
1629 while (*p == ' ' || *p == '\t')
1636 /* It is an unrecognized command. So there are no
1637 possible completions. */
1640 else if (c == (struct cmd_list_element *) -1)
1644 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1645 doesn't advance over that thing itself. Do so now. */
1647 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1649 if (q != tmp_command + point)
1651 /* There is something beyond the ambiguous
1652 command, so there are no possible completions. For
1653 example, "info t " or "info t foo" does not complete
1654 to anything, because "info t" can be "info target" or
1660 /* We're trying to complete on the command which was ambiguous.
1661 This we can deal with. */
1664 list = complete_on_cmdlist (*result_list->prefixlist, p,
1669 list = complete_on_cmdlist (cmdlist, p, word);
1671 /* Insure that readline does the right thing with respect to
1672 inserting quotes. */
1673 rl_completer_word_break_characters =
1674 gdb_completer_command_word_break_characters;
1679 /* We've recognized a full command. */
1681 if (p == tmp_command + point)
1683 /* There is no non-whitespace in the line beyond the command. */
1685 if (p[-1] == ' ' || p[-1] == '\t')
1687 /* The command is followed by whitespace; we need to complete
1688 on whatever comes after command. */
1691 /* It is a prefix command; what comes after it is
1692 a subcommand (e.g. "info "). */
1693 list = complete_on_cmdlist (*c->prefixlist, p, word);
1695 /* Insure that readline does the right thing
1696 with respect to inserting quotes. */
1697 rl_completer_word_break_characters =
1698 gdb_completer_command_word_break_characters;
1702 list = complete_on_enum (c->enums, p, word);
1703 rl_completer_word_break_characters =
1704 gdb_completer_command_word_break_characters;
1708 /* It is a normal command; what comes after it is
1709 completed by the command's completer function. */
1710 list = (*c->completer) (p, word);
1715 /* The command is not followed by whitespace; we need to
1716 complete on the command itself. e.g. "p" which is a
1717 command itself but also can complete to "print", "ptype"
1721 /* Find the command we are completing on. */
1723 while (q > tmp_command)
1725 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1731 list = complete_on_cmdlist (result_list, q, word);
1733 /* Insure that readline does the right thing
1734 with respect to inserting quotes. */
1735 rl_completer_word_break_characters =
1736 gdb_completer_command_word_break_characters;
1741 /* There is non-whitespace beyond the command. */
1743 if (c->prefixlist && !c->allow_unknown)
1745 /* It is an unrecognized subcommand of a prefix command,
1746 e.g. "info adsfkdj". */
1751 list = complete_on_enum (c->enums, p, word);
1755 /* It is a normal command. */
1756 list = (*c->completer) (p, word);
1762 /* If we found a list of potential completions during initialization then
1763 dole them out one at a time. The vector of completions is NULL
1764 terminated, so after returning the last one, return NULL (and continue
1765 to do so) each time we are called after that, until a new list is
1770 output = list[index];
1778 /* Can't do this because readline hasn't yet checked the word breaks
1779 for figuring out whether to insert a quote. */
1781 /* Make sure the word break characters are set back to normal for the
1782 next time that readline tries to complete something. */
1783 rl_completer_word_break_characters =
1784 gdb_completer_word_break_characters;
1790 /* Line completion interface function for readline. */
1793 readline_line_completion_function (text, matches)
1797 return line_completion_function (text, matches, rl_line_buffer, rl_point);
1800 /* Skip over a possibly quoted word (as defined by the quote characters
1801 and word break characters the completer uses). Returns pointer to the
1802 location after the "word". */
1808 char quote_char = '\0';
1811 for (scan = str; *scan != '\0'; scan++)
1813 if (quote_char != '\0')
1815 /* Ignore everything until the matching close quote char */
1816 if (*scan == quote_char)
1818 /* Found matching close quote. */
1823 else if (strchr (gdb_completer_quote_characters, *scan))
1825 /* Found start of a quoted string. */
1828 else if (strchr (gdb_completer_word_break_characters, *scan))
1842 #if STOP_SIGNAL == SIGTSTP
1843 signal (SIGTSTP, SIG_DFL);
1845 kill (getpid (), SIGTSTP);
1846 signal (SIGTSTP, stop_sig);
1848 signal (STOP_SIGNAL, stop_sig);
1850 printf_unfiltered ("%s", prompt);
1851 gdb_flush (gdb_stdout);
1853 /* Forget about any previous command -- null line now will do nothing. */
1856 #endif /* STOP_SIGNAL */
1858 /* Initialize signal handlers. */
1868 signal (SIGINT, request_quit);
1870 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1871 to the inferior and breakpoints will be ignored. */
1873 signal (SIGTRAP, SIG_DFL);
1876 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1877 passed to the inferior, which we don't want. It would be
1878 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1879 on BSD4.3 systems using vfork, that can affect the
1880 GDB process as well as the inferior (the signal handling tables
1881 might be in memory, shared between the two). Since we establish
1882 a handler for SIGQUIT, when we call exec it will set the signal
1883 to SIG_DFL for us. */
1884 signal (SIGQUIT, do_nothing);
1885 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1886 signal (SIGHUP, disconnect);
1887 signal (SIGFPE, float_handler);
1889 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1890 signal (SIGWINCH, SIGWINCH_HANDLER);
1894 /* Read one line from the command input stream `instream'
1895 into the local static buffer `linebuffer' (whose current length
1897 The buffer is made bigger as necessary.
1898 Returns the address of the start of the line.
1900 NULL is returned for end of file.
1902 *If* the instream == stdin & stdin is a terminal, the line read
1903 is copied into the file line saver (global var char *line,
1904 length linesize) so that it can be duplicated.
1906 This routine either uses fancy command line editing or
1907 simple input as the user has requested. */
1910 command_line_input (prrompt, repeat, annotation_suffix)
1913 char *annotation_suffix;
1915 static char *linebuffer = 0;
1916 static unsigned linelength = 0;
1920 char *local_prompt = prrompt;
1924 /* The annotation suffix must be non-NULL. */
1925 if (annotation_suffix == NULL)
1926 annotation_suffix = "";
1928 if (annotation_level > 1 && instream == stdin)
1930 local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt))
1931 + strlen (annotation_suffix) + 40);
1932 if (prrompt == NULL)
1933 local_prompt[0] = '\0';
1935 strcpy (local_prompt, prrompt);
1936 strcat (local_prompt, "\n\032\032");
1937 strcat (local_prompt, annotation_suffix);
1938 strcat (local_prompt, "\n");
1941 if (linebuffer == 0)
1944 linebuffer = (char *) xmalloc (linelength);
1949 /* Control-C quits instantly if typed while in this loop
1950 since it should not wait until the user types a newline. */
1954 signal (STOP_SIGNAL, stop_sig);
1959 /* Make sure that all output has been output. Some machines may let
1960 you get away with leaving out some of the gdb_flush, but not all. */
1962 gdb_flush (gdb_stdout);
1963 gdb_flush (gdb_stderr);
1965 if (source_file_name != NULL)
1967 ++source_line_number;
1968 sprintf (source_error,
1969 "%s%s:%d: Error in sourced command file:\n",
1972 source_line_number);
1973 error_pre_print = source_error;
1976 if (annotation_level > 1 && instream == stdin)
1978 printf_unfiltered ("\n\032\032pre-");
1979 printf_unfiltered (annotation_suffix);
1980 printf_unfiltered ("\n");
1983 /* Don't use fancy stuff if not talking to stdin. */
1984 if (readline_hook && instream == NULL)
1986 rl = (*readline_hook) (local_prompt);
1988 else if (command_editing_p && instream == stdin && ISATTY (instream))
1990 rl = readline (local_prompt);
1994 rl = gdb_readline (local_prompt);
1997 if (annotation_level > 1 && instream == stdin)
1999 printf_unfiltered ("\n\032\032post-");
2000 printf_unfiltered (annotation_suffix);
2001 printf_unfiltered ("\n");
2004 if (!rl || rl == (char *) EOF)
2009 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
2011 linelength = strlen(rl) + 1 + (p - linebuffer);
2012 nline = (char *) xrealloc (linebuffer, linelength);
2013 p += nline - linebuffer;
2017 /* Copy line. Don't copy null at end. (Leaves line alone
2018 if this was just a newline) */
2022 free (rl); /* Allocated in readline. */
2024 if (p == linebuffer || *(p - 1) != '\\')
2027 p--; /* Put on top of '\'. */
2028 local_prompt = (char *) 0;
2033 signal (STOP_SIGNAL, SIG_DFL);
2040 #define SERVER_COMMAND_LENGTH 7
2042 (p - linebuffer > SERVER_COMMAND_LENGTH)
2043 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
2046 /* Note that we don't set `line'. Between this and the check in
2047 dont_repeat, this insures that repeating will still do the
2050 return linebuffer + SERVER_COMMAND_LENGTH;
2053 /* Do history expansion if that is wished. */
2054 if (history_expansion_p && instream == stdin
2055 && ISATTY (instream))
2057 char *history_value;
2060 *p = '\0'; /* Insert null now. */
2061 expanded = history_expand (linebuffer, &history_value);
2064 /* Print the changes. */
2065 printf_unfiltered ("%s\n", history_value);
2067 /* If there was an error, call this function again. */
2070 free (history_value);
2071 return command_line_input (prrompt, repeat, annotation_suffix);
2073 if (strlen (history_value) > linelength)
2075 linelength = strlen (history_value) + 1;
2076 linebuffer = (char *) xrealloc (linebuffer, linelength);
2078 strcpy (linebuffer, history_value);
2079 p = linebuffer + strlen(linebuffer);
2080 free (history_value);
2084 /* If we just got an empty line, and that is supposed
2085 to repeat the previous command, return the value in the
2087 if (repeat && p == linebuffer)
2089 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++) ;
2095 /* Add line to history if appropriate. */
2096 if (instream == stdin
2097 && ISATTY (stdin) && *linebuffer)
2098 add_history (linebuffer);
2100 /* Note: lines consisting solely of comments are added to the command
2101 history. This is useful when you type a command, and then
2102 realize you don't want to execute it quite yet. You can comment
2103 out the command and then later fetch it from the value history
2104 and remove the '#'. The kill ring is probably better, but some
2105 people are in the habit of commenting things out. */
2107 *p1 = '\0'; /* Found a comment. */
2109 /* Save into global buffer if appropriate. */
2112 if (linelength > linesize)
2114 line = xrealloc (line, linelength);
2115 linesize = linelength;
2117 strcpy (line, linebuffer);
2125 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2126 code bodies. This is typically used when we encounter an "else"
2127 clause for an "if" command. */
2130 realloc_body_list (command, new_length)
2131 struct command_line *command;
2135 struct command_line **body_list;
2137 n = command->body_count;
2139 /* Nothing to do? */
2140 if (new_length <= n)
2143 body_list = (struct command_line **)
2144 xmalloc (sizeof (struct command_line *) * new_length);
2146 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
2148 free (command->body_list);
2149 command->body_list = body_list;
2150 command->body_count = new_length;
2153 /* Read one line from the input stream. If the command is an "else" or
2154 "end", return such an indication to the caller. */
2156 static enum misc_command_type
2157 read_next_line (command)
2158 struct command_line **command;
2160 char *p, *p1, *prompt_ptr, control_prompt[256];
2163 if (control_level >= 254)
2164 error ("Control nesting too deep!\n");
2166 /* Set a prompt based on the nesting of the control commands. */
2167 if (instream == stdin || (instream == 0 && readline_hook != NULL))
2169 for (i = 0; i < control_level; i++)
2170 control_prompt[i] = ' ';
2171 control_prompt[i] = '>';
2172 control_prompt[i+1] = '\0';
2173 prompt_ptr = (char *)&control_prompt[0];
2178 p = command_line_input (prompt_ptr, instream == stdin, "commands");
2180 /* Not sure what to do here. */
2184 /* Strip leading and trailing whitespace. */
2185 while (*p == ' ' || *p == '\t')
2188 p1 = p + strlen (p);
2189 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
2192 /* Blanks and comments don't really do anything, but we need to
2193 distinguish them from else, end and other commands which can be
2195 if (p1 == p || p[0] == '#')
2198 /* Is this the end of a simple, while, or if control structure? */
2199 if (p1 - p == 3 && !strncmp (p, "end", 3))
2202 /* Is the else clause of an if control structure? */
2203 if (p1 - p == 4 && !strncmp (p, "else", 4))
2204 return else_command;
2206 /* Check for while, if, break, continue, etc and build a new command
2207 line structure for them. */
2208 if (p1 - p > 5 && !strncmp (p, "while", 5))
2209 *command = build_command_line (while_control, p + 6);
2210 else if (p1 - p > 2 && !strncmp (p, "if", 2))
2211 *command = build_command_line (if_control, p + 3);
2212 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
2214 *command = (struct command_line *)
2215 xmalloc (sizeof (struct command_line));
2216 (*command)->next = NULL;
2217 (*command)->line = NULL;
2218 (*command)->control_type = break_control;
2219 (*command)->body_count = 0;
2220 (*command)->body_list = NULL;
2222 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
2224 *command = (struct command_line *)
2225 xmalloc (sizeof (struct command_line));
2226 (*command)->next = NULL;
2227 (*command)->line = NULL;
2228 (*command)->control_type = continue_control;
2229 (*command)->body_count = 0;
2230 (*command)->body_list = NULL;
2234 /* A normal command. */
2235 *command = (struct command_line *)
2236 xmalloc (sizeof (struct command_line));
2237 (*command)->next = NULL;
2238 (*command)->line = savestring (p, p1 - p);
2239 (*command)->control_type = simple_control;
2240 (*command)->body_count = 0;
2241 (*command)->body_list = NULL;
2244 /* Nothing special. */
2248 /* Recursively read in the control structures and create a command_line
2249 structure from them.
2251 The parent_control parameter is the control structure in which the
2252 following commands are nested. */
2254 static enum command_control_type
2255 recurse_read_control_structure (current_cmd)
2256 struct command_line *current_cmd;
2258 int current_body, i;
2259 enum misc_command_type val;
2260 enum command_control_type ret;
2261 struct command_line **body_ptr, *child_tail, *next;
2266 /* Sanity checks. */
2267 if (current_cmd->control_type == simple_control)
2269 error ("Recursed on a simple control type\n");
2270 return invalid_control;
2273 if (current_body > current_cmd->body_count)
2275 error ("Allocated body is smaller than this command type needs\n");
2276 return invalid_control;
2279 /* Read lines from the input stream and build control structures. */
2285 val = read_next_line (&next);
2287 /* Just skip blanks and comments. */
2288 if (val == nop_command)
2291 if (val == end_command)
2293 if (current_cmd->control_type == while_control
2294 || current_cmd->control_type == if_control)
2296 /* Success reading an entire control structure. */
2297 ret = simple_control;
2302 ret = invalid_control;
2307 /* Not the end of a control structure. */
2308 if (val == else_command)
2310 if (current_cmd->control_type == if_control
2311 && current_body == 1)
2313 realloc_body_list (current_cmd, 2);
2320 ret = invalid_control;
2327 child_tail->next = next;
2331 body_ptr = current_cmd->body_list;
2332 for (i = 1; i < current_body; i++)
2341 /* If the latest line is another control structure, then recurse
2343 if (next->control_type == while_control
2344 || next->control_type == if_control)
2347 ret = recurse_read_control_structure (next);
2350 if (ret != simple_control)
2360 /* Read lines from the input stream and accumulate them in a chain of
2361 struct command_line's, which is then returned. For input from a
2362 terminal, the special command "end" is used to mark the end of the
2363 input, and is not included in the returned chain of commands. */
2365 #define END_MESSAGE "End with a line saying just \"end\"."
2367 struct command_line *
2368 read_command_lines (prompt, from_tty)
2372 struct command_line *head, *tail, *next;
2373 struct cleanup *old_chain;
2374 enum command_control_type ret;
2375 enum misc_command_type val;
2377 if (readline_begin_hook)
2379 /* Note - intentional to merge messages with no newline */
2380 (*readline_begin_hook) ("%s %s\n", prompt, END_MESSAGE);
2382 else if (from_tty && input_from_terminal_p ())
2384 printf_unfiltered ("%s\n%s\n", prompt, END_MESSAGE);
2385 gdb_flush (gdb_stdout);
2393 val = read_next_line (&next);
2395 /* Ignore blank lines or comments. */
2396 if (val == nop_command)
2399 if (val == end_command)
2401 ret = simple_control;
2405 if (val != ok_command)
2407 ret = invalid_control;
2411 if (next->control_type == while_control
2412 || next->control_type == if_control)
2415 ret = recurse_read_control_structure (next);
2418 if (ret == invalid_control)
2429 old_chain = make_cleanup (free_command_lines, &head);
2438 if (ret != invalid_control)
2440 discard_cleanups (old_chain);
2443 do_cleanups (old_chain);
2446 if (readline_end_hook)
2448 (*readline_end_hook) ();
2453 /* Free a chain of struct command_line's. */
2456 free_command_lines (lptr)
2457 struct command_line **lptr;
2459 register struct command_line *l = *lptr;
2460 register struct command_line *next;
2461 struct command_line **blist;
2466 if (l->body_count > 0)
2468 blist = l->body_list;
2469 for (i = 0; i < l->body_count; i++, blist++)
2470 free_command_lines (blist);
2479 /* Add an element to the list of info subcommands. */
2482 add_info (name, fun, doc)
2484 void (*fun) PARAMS ((char *, int));
2487 add_cmd (name, no_class, fun, doc, &infolist);
2490 /* Add an alias to the list of info subcommands. */
2493 add_info_alias (name, oldname, abbrev_flag)
2498 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2501 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2502 Therefore, its own definition is called only for "info" with no args. */
2506 info_command (arg, from_tty)
2510 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2511 help_list (infolist, "info ", -1, gdb_stdout);
2514 /* The "complete" command is used by Emacs to implement completion. */
2518 complete_command (arg, from_tty)
2530 argpoint = strlen (arg);
2532 for (completion = line_completion_function (arg, i = 0, arg, argpoint);
2534 completion = line_completion_function (arg, ++i, arg, argpoint))
2536 printf_unfiltered ("%s\n", completion);
2541 /* The "show" command with no arguments shows all the settings. */
2545 show_command (arg, from_tty)
2549 cmd_show_list (showlist, from_tty, "");
2552 /* Add an element to the list of commands. */
2555 add_com (name, class, fun, doc)
2557 enum command_class class;
2558 void (*fun) PARAMS ((char *, int));
2561 add_cmd (name, class, fun, doc, &cmdlist);
2564 /* Add an alias or abbreviation command to the list of commands. */
2567 add_com_alias (name, oldname, class, abbrev_flag)
2570 enum command_class class;
2573 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2580 error ("Argument required (%s).", why);
2585 help_command (command, from_tty)
2587 int from_tty; /* Ignored */
2589 help_cmd (command, gdb_stdout);
2593 validate_comname (comname)
2599 error_no_arg ("name of command to define");
2604 if (!isalnum(*p) && *p != '-' && *p != '_')
2605 error ("Junk in argument list: \"%s\"", p);
2610 /* This is just a placeholder in the command data structures. */
2612 user_defined_command (ignore, from_tty)
2619 define_command (comname, from_tty)
2623 register struct command_line *cmds;
2624 register struct cmd_list_element *c, *newc, *hookc = 0;
2625 char *tem = comname;
2627 #define HOOK_STRING "hook-"
2630 validate_comname (comname);
2632 /* Look it up, and verify that we got an exact match. */
2633 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
2634 if (c && !STREQ (comname, c->name))
2639 if (c->class == class_user || c->class == class_alias)
2640 tem = "Redefine command \"%s\"? ";
2642 tem = "Really redefine built-in command \"%s\"? ";
2643 if (!query (tem, c->name))
2644 error ("Command \"%s\" not redefined.", c->name);
2647 /* If this new command is a hook, then mark the command which it
2648 is hooking. Note that we allow hooking `help' commands, so that
2649 we can hook the `stop' pseudo-command. */
2651 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
2653 /* Look up cmd it hooks, and verify that we got an exact match. */
2654 tem = comname+HOOK_LEN;
2655 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
2656 if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
2660 warning ("Your new `%s' command does not hook any existing command.",
2662 if (!query ("Proceed? "))
2663 error ("Not confirmed.");
2667 comname = savestring (comname, strlen (comname));
2669 /* If the rest of the commands will be case insensitive, this one
2670 should behave in the same manner. */
2671 for (tem = comname; *tem; tem++)
2672 if (isupper(*tem)) *tem = tolower(*tem);
2675 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
2676 cmds = read_command_lines (tmpbuf, from_tty);
2678 if (c && c->class == class_user)
2679 free_command_lines (&c->user_commands);
2681 newc = add_cmd (comname, class_user, user_defined_command,
2682 (c && c->class == class_user)
2683 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
2684 newc->user_commands = cmds;
2686 /* If this new command is a hook, then mark both commands as being
2690 hookc->hook = newc; /* Target gets hooked. */
2691 newc->hookee = hookc; /* We are marked as hooking target cmd. */
2696 document_command (comname, from_tty)
2700 struct command_line *doclines;
2701 register struct cmd_list_element *c;
2702 char *tem = comname;
2705 validate_comname (comname);
2707 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
2709 if (c->class != class_user)
2710 error ("Command \"%s\" is built-in.", comname);
2712 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
2713 doclines = read_command_lines (tmpbuf, from_tty);
2715 if (c->doc) free (c->doc);
2718 register struct command_line *cl1;
2719 register int len = 0;
2721 for (cl1 = doclines; cl1; cl1 = cl1->next)
2722 len += strlen (cl1->line) + 1;
2724 c->doc = (char *) xmalloc (len + 1);
2727 for (cl1 = doclines; cl1; cl1 = cl1->next)
2729 strcat (c->doc, cl1->line);
2731 strcat (c->doc, "\n");
2735 free_command_lines (&doclines);
2739 print_gnu_advertisement ()
2741 printf_unfiltered ("\
2742 GDB is free software and you are welcome to distribute copies of it\n\
2743 under certain conditions; type \"show copying\" to see the conditions.\n\
2744 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2749 print_gdb_version (stream)
2752 fprintf_filtered (stream, "\
2753 GDB %s (%s", version, host_name);
2755 if (!STREQ (host_name, target_name))
2756 fprintf_filtered (stream, " --target %s", target_name);
2758 fprintf_filtered (stream, "), ");
2760 fprintf_filtered (stream, "Copyright 1996 Free Software Foundation, Inc.");
2765 show_version (args, from_tty)
2770 print_gnu_advertisement ();
2771 print_gdb_version (gdb_stdout);
2772 printf_filtered ("\n");
2776 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2782 printf_unfiltered ("%s", prompt);
2783 gdb_flush (gdb_stdout);
2787 quit_command (args, from_tty)
2793 /* An optional expression may be used to cause gdb to terminate with the
2794 value of that expression. */
2797 value_ptr val = parse_and_eval (args);
2799 exit_code = (int) value_as_long (val);
2802 if (inferior_pid != 0 && target_has_execution)
2806 if (query ("The program is running. Quit anyway (and detach it)? "))
2807 target_detach (args, from_tty);
2809 error ("Not confirmed.");
2813 if (query ("The program is running. Quit anyway (and kill it)? "))
2816 error ("Not confirmed.");
2819 /* UDI wants this, to kill the TIP. */
2822 /* Save the history information if it is appropriate to do so. */
2823 if (write_history_p && history_filename)
2824 write_history (history_filename);
2829 /* Returns whether GDB is running on a terminal and whether the user
2830 desires that questions be asked of them on that terminal. */
2833 input_from_terminal_p ()
2835 return gdb_has_a_terminal () && (instream == stdin) & caution;
2840 pwd_command (args, from_tty)
2844 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
2845 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
2847 if (!STREQ (gdb_dirbuf, current_directory))
2848 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2849 current_directory, gdb_dirbuf);
2851 printf_unfiltered ("Working directory %s.\n", current_directory);
2855 cd_command (dir, from_tty)
2860 /* Found something other than leading repetitions of "/..". */
2861 int found_real_path;
2864 /* If the new directory is absolute, repeat is a no-op; if relative,
2865 repeat might be useful but is more likely to be a mistake. */
2869 error_no_arg ("new working directory");
2871 dir = tilde_expand (dir);
2872 make_cleanup (free, dir);
2874 if (chdir (dir) < 0)
2875 perror_with_name (dir);
2878 dir = savestring (dir, len - (len > 1 && SLASH_P(dir[len-1])));
2880 current_directory = dir;
2883 if (SLASH_P (current_directory[0]) && current_directory[1] == '\0')
2884 current_directory = concat (current_directory, dir, NULL);
2886 current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
2890 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2892 found_real_path = 0;
2893 for (p = current_directory; *p;)
2895 if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2])))
2897 else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.'
2898 && (p[3] == 0 || SLASH_P (p[3])))
2900 if (found_real_path)
2902 /* Search backwards for the directory just before the "/.."
2903 and obliterate it and the "/..". */
2905 while (q != current_directory && ! SLASH_P (q[-1]))
2908 if (q == current_directory)
2909 /* current_directory is
2910 a relative pathname ("can't happen"--leave it alone). */
2914 strcpy (q - 1, p + 3);
2919 /* We are dealing with leading repetitions of "/..", for example
2920 "/../..", which is the Mach super-root. */
2925 found_real_path = 1;
2930 forget_cached_source_info ();
2933 pwd_command ((char *) 0, 1);
2936 struct source_cleanup_lines_args {
2939 char *old_pre_error;
2940 char *old_error_pre_print;
2944 source_cleanup_lines (args)
2947 struct source_cleanup_lines_args *p =
2948 (struct source_cleanup_lines_args *)args;
2949 source_line_number = p->old_line;
2950 source_file_name = p->old_file;
2951 source_pre_error = p->old_pre_error;
2952 error_pre_print = p->old_error_pre_print;
2957 source_command (args, from_tty)
2962 struct cleanup *old_cleanups;
2964 struct source_cleanup_lines_args old_lines;
2969 error ("source command requires pathname of file to source.");
2972 file = tilde_expand (file);
2973 old_cleanups = make_cleanup (free, file);
2975 stream = fopen (file, FOPEN_RT);
2977 perror_with_name (file);
2979 make_cleanup (fclose, stream);
2981 old_lines.old_line = source_line_number;
2982 old_lines.old_file = source_file_name;
2983 old_lines.old_pre_error = source_pre_error;
2984 old_lines.old_error_pre_print = error_pre_print;
2985 make_cleanup (source_cleanup_lines, &old_lines);
2986 source_line_number = 0;
2987 source_file_name = file;
2988 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
2989 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
2990 make_cleanup (free, source_pre_error);
2991 /* This will get set every time we read a line. So it won't stay "" for
2993 error_pre_print = "";
2995 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
2996 if (source_error_allocated < needed_length)
2998 source_error_allocated *= 2;
2999 if (source_error_allocated < needed_length)
3000 source_error_allocated = needed_length;
3001 if (source_error == NULL)
3002 source_error = xmalloc (source_error_allocated);
3004 source_error = xrealloc (source_error, source_error_allocated);
3007 read_command_file (stream);
3009 do_cleanups (old_cleanups);
3014 echo_command (text, from_tty)
3022 while ((c = *p++) != '\0')
3026 /* \ at end of argument is used after spaces
3027 so they won't be lost. */
3031 c = parse_escape (&p);
3033 printf_filtered ("%c", c);
3036 printf_filtered ("%c", c);
3039 /* Force this output to appear now. */
3041 gdb_flush (gdb_stdout);
3046 dont_repeat_command (ignored, from_tty)
3050 *line = 0; /* Can't call dont_repeat here because we're not
3051 necessarily reading from stdin. */
3054 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3056 /* Functions to manipulate the endianness of the target. */
3058 #ifndef TARGET_BYTE_ORDER_DEFAULT
3059 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
3062 int target_byte_order = TARGET_BYTE_ORDER_DEFAULT;
3064 static int target_byte_order_auto = 1;
3066 /* Called if the user enters ``set endian'' without an argument. */
3068 set_endian (args, from_tty)
3072 printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
3073 show_endian (args, from_tty);
3076 /* Called by ``set endian big''. */
3078 set_endian_big (args, from_tty)
3082 target_byte_order = BIG_ENDIAN;
3083 target_byte_order_auto = 0;
3086 /* Called by ``set endian little''. */
3088 set_endian_little (args, from_tty)
3092 target_byte_order = LITTLE_ENDIAN;
3093 target_byte_order_auto = 0;
3096 /* Called by ``set endian auto''. */
3098 set_endian_auto (args, from_tty)
3102 target_byte_order_auto = 1;
3105 /* Called by ``show endian''. */
3107 show_endian (args, from_tty)
3112 (target_byte_order_auto
3113 ? "The target endianness is set automatically (currently %s endian)\n"
3114 : "The target is assumed to be %s endian\n");
3115 printf_unfiltered ((char *) msg, TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
3118 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3120 /* Set the endianness from a BFD. */
3122 set_endian_from_file (abfd)
3125 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3128 if (bfd_big_endian (abfd))
3131 want = LITTLE_ENDIAN;
3132 if (target_byte_order_auto)
3133 target_byte_order = want;
3134 else if (target_byte_order != want)
3135 warning ("%s endian file does not match %s endian target.",
3136 want == BIG_ENDIAN ? "big" : "little",
3137 TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
3139 #else /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3141 if (bfd_big_endian (abfd)
3142 ? TARGET_BYTE_ORDER != BIG_ENDIAN
3143 : TARGET_BYTE_ORDER == BIG_ENDIAN)
3144 warning ("%s endian file does not match %s endian target.",
3145 bfd_big_endian (abfd) ? "big" : "little",
3146 TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
3148 #endif /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3151 /* Functions to manipulate command line editing control variables. */
3153 /* Number of commands to print in each call to show_commands. */
3154 #define Hist_print 10
3156 show_commands (args, from_tty)
3160 /* Index for history commands. Relative to history_base. */
3163 /* Number of the history entry which we are planning to display next.
3164 Relative to history_base. */
3167 /* The first command in the history which doesn't exist (i.e. one more
3168 than the number of the last command). Relative to history_base. */
3171 extern HIST_ENTRY *history_get PARAMS ((int));
3173 /* Print out some of the commands from the command history. */
3174 /* First determine the length of the history list. */
3175 hist_len = history_size;
3176 for (offset = 0; offset < history_size; offset++)
3178 if (!history_get (history_base + offset))
3187 if (args[0] == '+' && args[1] == '\0')
3188 /* "info editing +" should print from the stored position. */
3191 /* "info editing <exp>" should print around command number <exp>. */
3192 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
3194 /* "show commands" means print the last Hist_print commands. */
3197 num = hist_len - Hist_print;
3203 /* If there are at least Hist_print commands, we want to display the last
3204 Hist_print rather than, say, the last 6. */
3205 if (hist_len - num < Hist_print)
3207 num = hist_len - Hist_print;
3212 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
3214 printf_filtered ("%5d %s\n", history_base + offset,
3215 (history_get (history_base + offset))->line);
3218 /* The next command we want to display is the next one that we haven't
3222 /* If the user repeats this command with return, it should do what
3223 "show commands +" does. This is unnecessary if arg is null,
3224 because "show commands +" is not useful after "show commands". */
3225 if (from_tty && args)
3232 /* Called by do_setshow_command. */
3235 set_history_size_command (args, from_tty, c)
3238 struct cmd_list_element *c;
3240 if (history_size == INT_MAX)
3241 unstifle_history ();
3242 else if (history_size >= 0)
3243 stifle_history (history_size);
3246 history_size = INT_MAX;
3247 error ("History size must be non-negative");
3253 set_history (args, from_tty)
3257 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3258 help_list (sethistlist, "set history ", -1, gdb_stdout);
3263 show_history (args, from_tty)
3267 cmd_show_list (showhistlist, from_tty, "");
3270 int info_verbose = 0; /* Default verbose msgs off */
3272 /* Called by do_setshow_command. An elaborate joke. */
3275 set_verbose (args, from_tty, c)
3278 struct cmd_list_element *c;
3280 char *cmdname = "verbose";
3281 struct cmd_list_element *showcmd;
3283 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
3287 c->doc = "Set verbose printing of informational messages.";
3288 showcmd->doc = "Show verbose printing of informational messages.";
3292 c->doc = "Set verbosity.";
3293 showcmd->doc = "Show verbosity.";
3298 float_handler (signo)
3301 /* This message is based on ANSI C, section 4.7. Note that integer
3302 divide by zero causes this, so "float" is a misnomer. */
3303 signal (SIGFPE, float_handler);
3304 error ("Erroneous arithmetic operation.");
3316 enablebreaklist = NULL;
3320 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3324 showhistlist = NULL;
3325 unsethistlist = NULL;
3326 #if MAINTENANCE_CMDS
3327 maintenancelist = NULL;
3328 maintenanceinfolist = NULL;
3329 maintenanceprintlist = NULL;
3331 setprintlist = NULL;
3332 showprintlist = NULL;
3333 setchecklist = NULL;
3334 showchecklist = NULL;
3337 /* Init the history buffer. Note that we are called after the init file(s)
3338 * have been read so that the user can change the history file via his
3339 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3340 * overrides all of this.
3348 tmpenv = getenv ("HISTSIZE");
3350 history_size = atoi (tmpenv);
3351 else if (!history_size)
3354 stifle_history (history_size);
3356 tmpenv = getenv ("GDBHISTFILE");
3358 history_filename = savestring (tmpenv, strlen(tmpenv));
3359 else if (!history_filename) {
3360 /* We include the current directory so that if the user changes
3361 directories the file written will be the same as the one
3363 history_filename = concat (current_directory, "/.gdb_history", NULL);
3365 read_history (history_filename);
3371 struct cmd_list_element *c;
3373 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3375 add_prefix_cmd ("endian", class_support, set_endian,
3376 "Set endianness of target.",
3377 &endianlist, "set endian ", 0, &setlist);
3378 add_cmd ("big", class_support, set_endian_big,
3379 "Set target as being big endian.", &endianlist);
3380 add_cmd ("little", class_support, set_endian_little,
3381 "Set target as being little endian.", &endianlist);
3382 add_cmd ("auto", class_support, set_endian_auto,
3383 "Select target endianness automatically.", &endianlist);
3384 add_cmd ("endian", class_support, show_endian,
3385 "Show endianness of target.", &showlist);
3387 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3389 #ifdef DEFAULT_PROMPT
3390 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
3392 prompt = savestring ("(gdb) ", 6);
3395 /* Set the important stuff up for command editing. */
3396 command_editing_p = 1;
3397 history_expansion_p = 0;
3398 write_history_p = 0;
3400 /* Setup important stuff for command line editing. */
3401 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
3402 rl_completer_word_break_characters = gdb_completer_word_break_characters;
3403 rl_completer_quote_characters = gdb_completer_quote_characters;
3404 rl_readline_name = "gdb";
3406 /* Define the classes of commands.
3407 They will appear in the help list in the reverse of this order. */
3409 add_cmd ("internals", class_maintenance, NO_FUNCTION,
3410 "Maintenance commands.\n\
3411 Some gdb commands are provided just for use by gdb maintainers.\n\
3412 These commands are subject to frequent change, and may not be as\n\
3413 well documented as user commands.",
3415 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
3416 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
3417 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
3418 The commands in this class are those defined by the user.\n\
3419 Use the \"define\" command to define a command.", &cmdlist);
3420 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
3421 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
3422 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
3423 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
3424 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
3425 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
3426 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3427 counting from zero for the innermost (currently executing) frame.\n\n\
3428 At any time gdb identifies one frame as the \"selected\" frame.\n\
3429 Variable lookups are done with respect to the selected frame.\n\
3430 When the program being debugged stops, gdb selects the innermost frame.\n\
3431 The commands below can be used to select other frames by number or address.",
3433 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
3435 add_com ("pwd", class_files, pwd_command,
3436 "Print working directory. This is used for your program as well.");
3437 c = add_cmd ("cd", class_files, cd_command,
3438 "Set working directory to DIR for debugger and program being debugged.\n\
3439 The change does not take effect for the program being debugged\n\
3440 until the next time it is started.", &cmdlist);
3441 c->completer = filename_completer;
3444 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
3449 add_com ("echo", class_support, echo_command,
3450 "Print a constant string. Give string as argument.\n\
3451 C escape sequences may be used in the argument.\n\
3452 No newline is added at the end of the argument;\n\
3453 use \"\\n\" if you want a newline to be printed.\n\
3454 Since leading and trailing whitespace are ignored in command arguments,\n\
3455 if you want to print some you must use \"\\\" before leading whitespace\n\
3456 to be printed or after trailing whitespace.");
3457 add_com ("document", class_support, document_command,
3458 "Document a user-defined command.\n\
3459 Give command name as argument. Give documentation on following lines.\n\
3460 End with a line of just \"end\".");
3461 add_com ("define", class_support, define_command,
3462 "Define a new command name. Command name is argument.\n\
3463 Definition appears on following lines, one command per line.\n\
3464 End with a line of just \"end\".\n\
3465 Use the \"document\" command to give documentation for the new command.\n\
3466 Commands defined in this way may have up to ten arguments.");
3469 c = add_cmd ("source", class_support, source_command,
3470 "Read commands from a file named FILE.\n\
3471 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
3472 when gdb is started.", &cmdlist);
3474 /* Punt file name, we can't help it easily. */
3475 c = add_cmd ("source", class_support, source_command,
3476 "Read commands from a file named FILE.\n\
3477 Note that the file \".gdbinit\" is read automatically in this way\n\
3478 when gdb is started.", &cmdlist);
3480 c->completer = filename_completer;
3482 add_com ("quit", class_support, quit_command, "Exit gdb.");
3483 add_com ("help", class_support, help_command, "Print list of commands.");
3484 add_com_alias ("q", "quit", class_support, 1);
3485 add_com_alias ("h", "help", class_support, 1);
3487 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
3488 Primarily used inside of user-defined commands that should not be repeated when\n\
3491 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
3494 add_show_from_set (c, &showlist);
3495 c->function.sfunc = set_verbose;
3496 set_verbose (NULL, 0, c);
3499 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
3500 "Set editing of command lines as they are typed.\n\
3501 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
3502 Without an argument, command line editing is enabled. To edit, use\n\
3503 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
3506 add_prefix_cmd ("history", class_support, set_history,
3507 "Generic command for setting command history parameters.",
3508 &sethistlist, "set history ", 0, &setlist);
3509 add_prefix_cmd ("history", class_support, show_history,
3510 "Generic command for showing command history parameters.",
3511 &showhistlist, "show history ", 0, &showlist);
3514 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
3515 "Set history expansion on command input.\n\
3516 Without an argument, history expansion is enabled.", &sethistlist),
3520 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
3521 "Set saving of the history record on exit.\n\
3522 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
3523 Without an argument, saving is enabled.", &sethistlist),
3526 c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
3527 "Set the size of the command history, \n\
3528 ie. the number of previous commands to keep a record of.", &sethistlist);
3529 add_show_from_set (c, &showhistlist);
3530 c->function.sfunc = set_history_size_command;
3533 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
3534 "Set the filename in which to record the command history\n\
3535 (the list of previous commands of which a record is kept).", &sethistlist),
3539 (add_set_cmd ("confirm", class_support, var_boolean,
3541 "Set whether to confirm potentially dangerous operations.",
3545 add_prefix_cmd ("info", class_info, info_command,
3546 "Generic command for showing things about the program being debugged.",
3547 &infolist, "info ", 0, &cmdlist);
3548 add_com_alias ("i", "info", class_info, 1);
3550 add_com ("complete", class_obscure, complete_command,
3551 "List the completions for the rest of the line as a command.");
3553 add_prefix_cmd ("show", class_info, show_command,
3554 "Generic command for showing things about the debugger.",
3555 &showlist, "show ", 0, &cmdlist);
3556 /* Another way to get at the same thing. */
3557 add_info ("set", show_command, "Show all GDB settings.");
3559 add_cmd ("commands", no_class, show_commands,
3560 "Show the history of commands you typed.\n\
3561 You can supply a command number to start with, or a `+' to start after\n\
3562 the previous command number shown.",
3565 add_cmd ("version", no_class, show_version,
3566 "Show what version of GDB this is.", &showlist);
3568 add_com ("while", class_support, while_command,
3569 "Execute nested commands WHILE the conditional expression is non zero.\n\
3570 The conditional expression must follow the word `while' and must in turn be\n\
3571 followed by a new line. The nested commands must be entered one per line,\n\
3572 and should be terminated by the word `end'.");
3574 add_com ("if", class_support, if_command,
3575 "Execute nested commands once IF the conditional expression is non zero.\n\
3576 The conditional expression must follow the word `if' and must in turn be\n\
3577 followed by a new line. The nested commands must be entered one per line,\n\
3578 and should be terminated by the word 'else' or `end'. If an else clause\n\
3579 is used, the same rules apply to its nested commands as to the first ones.");
3581 /* If target is open when baud changes, it doesn't take effect until the
3582 next open (I think, not sure). */
3583 add_show_from_set (add_set_cmd ("remotebaud", no_class,
3584 var_zinteger, (char *)&baud_rate,
3585 "Set baud rate for remote serial I/O.\n\
3586 This value is used to set the speed of the serial port when debugging\n\
3587 using remote targets.", &setlist),
3591 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug,
3592 "Set debugging of remote protocol.\n\
3593 When enabled, each packet sent or received with the remote target\n\
3594 is displayed.", &setlist),
3598 add_set_cmd ("remotetimeout", no_class, var_integer, (char *)&remote_timeout,
3599 "Set timeout limit to wait for target to respond.\n\
3600 This value is used to set the time limit for gdb to wait for a response\n\
3601 from he target.", &setlist),