1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 1997
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 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 set_endian PARAMS ((char *, int));
113 static void set_endian_big PARAMS ((char *, int));
115 static void set_endian_little PARAMS ((char *, int));
117 static void set_endian_auto PARAMS ((char *, int));
119 static void show_endian PARAMS ((char *, int));
121 static void show_history PARAMS ((char *, int));
123 static void set_history PARAMS ((char *, int));
125 static void set_history_size_command PARAMS ((char *, int,
126 struct cmd_list_element *));
128 static void show_commands PARAMS ((char *, int));
130 static void echo_command PARAMS ((char *, int));
132 static void pwd_command PARAMS ((char *, int));
134 static void show_version PARAMS ((char *, int));
136 static void document_command PARAMS ((char *, int));
138 static void define_command PARAMS ((char *, int));
140 static void validate_comname PARAMS ((char *));
142 static void help_command PARAMS ((char *, int));
144 static void show_command PARAMS ((char *, int));
146 static void info_command PARAMS ((char *, int));
148 static void complete_command PARAMS ((char *, int));
150 static void do_nothing PARAMS ((int));
153 static int quit_cover PARAMS ((char *));
155 static void disconnect PARAMS ((int));
158 static void source_cleanup PARAMS ((FILE *));
160 /* If this definition isn't overridden by the header files, assume
161 that isatty and fileno exist on this system. */
163 #define ISATTY(FP) (isatty (fileno (FP)))
166 /* Initialization file name for gdb. This is overridden in some configs. */
168 #ifndef GDBINIT_FILENAME
169 #define GDBINIT_FILENAME ".gdbinit"
171 char gdbinit[] = GDBINIT_FILENAME;
173 int inhibit_gdbinit = 0;
175 /* If nonzero, and GDB has been configured to be able to use windows,
176 attempt to open them upon startup. */
180 /* Version number of GDB, as a string. */
182 extern char *version;
184 /* Canonical host name as a string. */
186 extern char *host_name;
188 /* Canonical target name as a string. */
190 extern char *target_name;
192 extern char lang_frame_mismatch_warn[]; /* language.c */
194 /* Flag for whether we want all the "from_tty" gubbish printed. */
196 int caution = 1; /* Default is yes, sigh. */
198 /* Define all cmd_list_elements. */
200 /* Chain containing all defined commands. */
202 struct cmd_list_element *cmdlist;
204 /* Chain containing all defined info subcommands. */
206 struct cmd_list_element *infolist;
208 /* Chain containing all defined enable subcommands. */
210 struct cmd_list_element *enablelist;
212 /* Chain containing all defined disable subcommands. */
214 struct cmd_list_element *disablelist;
216 /* Chain containing all defined delete subcommands. */
218 struct cmd_list_element *deletelist;
220 /* Chain containing all defined "enable breakpoint" subcommands. */
222 struct cmd_list_element *enablebreaklist;
224 /* Chain containing all defined set subcommands */
226 struct cmd_list_element *setlist;
228 /* Chain containing all defined unset subcommands */
230 struct cmd_list_element *unsetlist;
232 /* Chain containing all defined show subcommands. */
234 struct cmd_list_element *showlist;
236 /* Chain containing the \"set endian\" commands. */
238 struct cmd_list_element *endianlist;
240 /* Chain containing all defined \"set history\". */
242 struct cmd_list_element *sethistlist;
244 /* Chain containing all defined \"show history\". */
246 struct cmd_list_element *showhistlist;
248 /* Chain containing all defined \"unset history\". */
250 struct cmd_list_element *unsethistlist;
252 /* Chain containing all defined maintenance subcommands. */
255 struct cmd_list_element *maintenancelist;
258 /* Chain containing all defined "maintenance info" subcommands. */
261 struct cmd_list_element *maintenanceinfolist;
264 /* Chain containing all defined "maintenance print" subcommands. */
267 struct cmd_list_element *maintenanceprintlist;
270 struct cmd_list_element *setprintlist;
272 struct cmd_list_element *showprintlist;
274 struct cmd_list_element *setchecklist;
276 struct cmd_list_element *showchecklist;
278 /* stdio stream that command input is being read from. Set to stdin normally.
279 Set by source_command to the file we are sourcing. Set to NULL if we are
280 executing a user-defined command or interacting via a GUI. */
284 /* Current working directory. */
286 char *current_directory;
288 /* The directory name is actually stored here (usually). */
289 char gdb_dirbuf[1024];
291 /* Function to call before reading a command, if nonzero.
292 The function receives two args: an input stream,
293 and a prompt string. */
295 void (*window_hook) PARAMS ((FILE *, char *));
300 /* gdb prints this when reading a command interactively */
303 /* Buffer used for reading command lines, and the size
304 allocated for it so far. */
309 /* Nonzero if the current command is modified by "server ". This
310 affects things like recording into the command history, comamnds
311 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
312 whatever) can issue its own commands and also send along commands
313 from the user, and have the user not notice that the user interface
314 is issuing commands too. */
317 /* Baud rate specified for talking to serial target systems. Default
318 is left as -1, so targets can choose their own defaults. */
319 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
320 or (unsigned int)-1. This is a Bad User Interface. */
324 /* Timeout limit for response from target. */
326 int remote_timeout = 20; /* Set default to 20 */
328 /* Non-zero tells remote* modules to output debugging info. */
330 int remote_debug = 0;
332 /* Level of control structure. */
333 static int control_level;
335 /* Structure for arguments to user defined functions. */
336 #define MAXUSERARGS 10
339 struct user_args *next;
348 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
352 #define STOP_SIGNAL SIGTSTP
353 static void stop_sig PARAMS ((int));
357 /* Some System V have job control but not sigsetmask(). */
358 #if !defined (HAVE_SIGSETMASK)
360 #define HAVE_SIGSETMASK 1
362 #define HAVE_SIGSETMASK 0
366 #if 0 == (HAVE_SIGSETMASK)
367 #define sigsetmask(n)
370 /* Hooks for alternate command interfaces. */
372 /* Called after most modules have been initialized, but before taking users
375 void (*init_ui_hook) PARAMS ((char *argv0));
377 /* Called instead of command_loop at top level. Can be invoked via
378 return_to_top_level. */
380 void (*command_loop_hook) PARAMS ((void));
383 /* Called instead of fputs for all output. */
385 void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer, FILE *stream));
387 /* Called when the target says something to the host, which may
388 want to appear in a different window. */
390 void (*target_output_hook) PARAMS ((char *));
392 /* Called from print_frame_info to list the line we stopped in. */
394 void (*print_frame_info_listing_hook) PARAMS ((struct symtab *s, int line,
395 int stopline, int noerror));
396 /* Replaces most of query. */
398 int (*query_hook) PARAMS ((const char *, va_list));
400 /* Called from gdb_flush to flush output. */
402 void (*flush_hook) PARAMS ((FILE *stream));
404 /* These three functions support getting lines of text from the user. They
405 are used in sequence. First readline_begin_hook is called with a text
406 string that might be (for example) a message for the user to type in a
407 sequence of commands to be executed at a breakpoint. If this function
408 calls back to a GUI, it might take this opportunity to pop up a text
409 interaction window with this message. Next, readline_hook is called
410 with a prompt that is emitted prior to collecting the user input.
411 It can be called multiple times. Finally, readline_end_hook is called
412 to notify the GUI that we are done with the interaction window and it
415 void (*readline_begin_hook) PARAMS ((char *, ...));
416 char * (*readline_hook) PARAMS ((char *));
417 void (*readline_end_hook) PARAMS ((void));
419 /* Called as appropriate to notify the interface of the specified breakpoint
422 void (*create_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
423 void (*delete_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
424 void (*modify_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
426 /* Called during long calculations to allow GUI to repair window damage, and to
427 check for stop buttons, etc... */
429 void (*interactive_hook) PARAMS ((void));
431 /* Called when the registers have changed, as a hint to a GUI
432 to minimize window update. */
434 void (*registers_changed_hook) PARAMS ((void));
436 /* Called when going to wait for the target. Usually allows the GUI to run
437 while waiting for target events. */
439 int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus *status));
441 /* Used by UI as a wrapper around command execution. May do various things
442 like enabling/disabling buttons, etc... */
444 void (*call_command_hook) PARAMS ((struct cmd_list_element *c, char *cmd,
448 /* Takes control from error (). Typically used to prevent longjmps out of the
449 middle of the GUI. Usually used in conjunction with a catch routine. */
451 NORETURN void (*error_hook) PARAMS ((void)) ATTR_NORETURN;
454 /* Where to go for return_to_top_level (RETURN_ERROR). */
455 jmp_buf error_return;
456 /* Where to go for return_to_top_level (RETURN_QUIT). */
459 /* Return for reason REASON. This generally gets back to the command
460 loop, but can be caught via catch_errors. */
463 return_to_top_level (reason)
464 enum return_reason reason;
469 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
470 I can think of a reason why that is vital, though). */
471 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
473 disable_current_display ();
474 do_cleanups (ALL_CLEANUPS);
476 if (annotation_level > 1)
487 (NORETURN void) longjmp
488 (reason == RETURN_ERROR ? error_return : quit_return, 1);
491 /* Call FUNC with arg ARGS, catching any errors. If there is no
492 error, return the value returned by FUNC. If there is an error,
493 print ERRSTRING, print the specific error message, then return
496 Must not be called with immediate_quit in effect (bad things might
497 happen, say we got a signal in the middle of a memcpy to quit_return).
498 This is an OK restriction; with very few exceptions immediate_quit can
499 be replaced by judicious use of QUIT.
501 MASK specifies what to catch; it is normally set to
502 RETURN_MASK_ALL, if for no other reason than that the code which
503 calls catch_errors might not be set up to deal with a quit which
504 isn't caught. But if the code can deal with it, it generally
505 should be RETURN_MASK_ERROR, unless for some reason it is more
506 useful to abort only the portion of the operation inside the
507 catch_errors. Note that quit should return to the command line
508 fairly quickly, even if some further processing is being done. */
511 catch_errors (func, args, errstring, mask)
512 int (*func) PARAMS ((char *));
521 struct cleanup *saved_cleanup_chain;
522 char *saved_error_pre_print;
523 char *saved_quit_pre_print;
525 saved_cleanup_chain = save_cleanups ();
526 saved_error_pre_print = error_pre_print;
527 saved_quit_pre_print = quit_pre_print;
529 if (mask & RETURN_MASK_ERROR)
531 memcpy ((char *)saved_error, (char *)error_return, sizeof (jmp_buf));
532 error_pre_print = errstring;
534 if (mask & RETURN_MASK_QUIT)
536 memcpy (saved_quit, quit_return, sizeof (jmp_buf));
537 quit_pre_print = errstring;
540 if (setjmp (tmp_jmp) == 0)
542 if (mask & RETURN_MASK_ERROR)
543 memcpy (error_return, tmp_jmp, sizeof (jmp_buf));
544 if (mask & RETURN_MASK_QUIT)
545 memcpy (quit_return, tmp_jmp, sizeof (jmp_buf));
546 val = (*func) (args);
551 restore_cleanups (saved_cleanup_chain);
553 if (mask & RETURN_MASK_ERROR)
555 memcpy (error_return, saved_error, sizeof (jmp_buf));
556 error_pre_print = saved_error_pre_print;
558 if (mask & RETURN_MASK_QUIT)
560 memcpy (quit_return, saved_quit, sizeof (jmp_buf));
561 quit_pre_print = saved_quit_pre_print;
566 /* Handler for SIGHUP. */
573 catch_errors (quit_cover, NULL,
574 "Could not kill the program being debugged", RETURN_MASK_ALL);
575 signal (SIGHUP, SIG_DFL);
576 kill (getpid (), SIGHUP);
579 /* Just a little helper function for disconnect(). */
585 caution = 0; /* Throw caution to the wind -- we're exiting.
586 This prevents asking the user dumb questions. */
587 quit_command((char *)0, 0);
590 #endif /* defined SIGHUP */
592 /* Line number we are currently in in a file which is being sourced. */
593 static int source_line_number;
595 /* Name of the file we are sourcing. */
596 static char *source_file_name;
598 /* Buffer containing the error_pre_print used by the source stuff.
600 static char *source_error;
601 static int source_error_allocated;
603 /* Something to glom on to the start of error_pre_print if source_file_name
605 static char *source_pre_error;
607 /* Clean up on error during a "source" command (or execution of a
608 user-defined command). */
611 source_cleanup (stream)
614 /* Restore the previous input stream. */
618 /* Read commands from STREAM. */
620 read_command_file (stream)
623 struct cleanup *cleanups;
625 cleanups = make_cleanup (source_cleanup, instream);
628 do_cleanups (cleanups);
631 extern void init_proc PARAMS ((void));
633 void (*pre_init_ui_hook) PARAMS ((void));
639 if (pre_init_ui_hook)
642 /* Run the init function of each source file */
644 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
645 current_directory = gdb_dirbuf;
647 init_cmd_lists (); /* This needs to be done first */
648 initialize_targets (); /* Setup target_terminal macros for utils.c */
649 initialize_utils (); /* Make errors and warnings possible */
650 initialize_all_files ();
651 init_main (); /* But that omits this file! Do it now */
656 /* We need a default language for parsing expressions, so simple things like
657 "set width 0" won't fail if no language is explicitly set in a config file
658 or implicitly set by reading an executable during startup. */
659 set_language (language_c);
660 expected_language = current_language; /* don't warn about the change. */
663 init_ui_hook (argv0);
666 /* Allocate, initialize a new command line structure for one of the
667 control commands (if/while). */
669 static struct command_line *
670 build_command_line (type, args)
671 enum command_control_type type;
674 struct command_line *cmd;
677 error ("if/while commands require arguments.\n");
679 cmd = (struct command_line *)xmalloc (sizeof (struct command_line));
681 cmd->control_type = type;
685 = (struct command_line **)xmalloc (sizeof (struct command_line *)
687 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
688 cmd->line = savestring (args, strlen (args));
692 /* Build and return a new command structure for the control commands
693 such as "if" and "while". */
695 static struct command_line *
696 get_command_line (type, arg)
697 enum command_control_type type;
700 struct command_line *cmd;
701 struct cleanup *old_chain = NULL;
703 /* Allocate and build a new command line structure. */
704 cmd = build_command_line (type, arg);
706 old_chain = make_cleanup (free_command_lines, &cmd);
708 /* Read in the body of this command. */
709 if (recurse_read_control_structure (cmd) == invalid_control)
711 warning ("error reading in control structure\n");
712 do_cleanups (old_chain);
716 discard_cleanups (old_chain);
720 /* Recursively print a command (including full control structures). */
722 print_command_line (cmd, depth)
723 struct command_line *cmd;
730 for (i = 0; i < depth; i++)
731 fputs_filtered (" ", gdb_stdout);
734 /* A simple command, print it and return. */
735 if (cmd->control_type == simple_control)
737 fputs_filtered (cmd->line, gdb_stdout);
738 fputs_filtered ("\n", gdb_stdout);
742 /* loop_continue to jump to the start of a while loop, print it
744 if (cmd->control_type == continue_control)
746 fputs_filtered ("loop_continue\n", gdb_stdout);
750 /* loop_break to break out of a while loop, print it and return. */
751 if (cmd->control_type == break_control)
753 fputs_filtered ("loop_break\n", gdb_stdout);
757 /* A while command. Recursively print its subcommands before returning. */
758 if (cmd->control_type == while_control)
760 struct command_line *list;
761 fputs_filtered ("while ", gdb_stdout);
762 fputs_filtered (cmd->line, gdb_stdout);
763 fputs_filtered ("\n", gdb_stdout);
764 list = *cmd->body_list;
767 print_command_line (list, depth + 1);
772 /* An if command. Recursively print both arms before returning. */
773 if (cmd->control_type == if_control)
775 fputs_filtered ("if ", gdb_stdout);
776 fputs_filtered (cmd->line, gdb_stdout);
777 fputs_filtered ("\n", gdb_stdout);
779 print_command_line (cmd->body_list[0], depth + 1);
781 /* Show the false arm if it exists. */
782 if (cmd->body_count == 2)
786 for (i = 0; i < depth; i++)
787 fputs_filtered (" ", gdb_stdout);
789 fputs_filtered ("else\n", gdb_stdout);
790 print_command_line (cmd->body_list[1], depth + 1);
794 for (i = 0; i < depth; i++)
795 fputs_filtered (" ", gdb_stdout);
797 fputs_filtered ("end\n", gdb_stdout);
801 /* Execute the command in CMD. */
803 enum command_control_type
804 execute_control_command (cmd)
805 struct command_line *cmd;
807 struct expression *expr;
808 struct command_line *current;
809 struct cleanup *old_chain = 0;
813 enum command_control_type ret;
816 switch (cmd->control_type)
819 /* A simple command, execute it and return. */
820 new_line = insert_args (cmd->line);
822 return invalid_control;
823 old_chain = make_cleanup (free_current_contents, &new_line);
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 (free_current_contents, &new_line);
842 expr = parse_expression (new_line);
843 make_cleanup (free_current_contents, &expr);
845 ret = simple_control;
848 /* Keep iterating so long as the expression is true. */
855 /* Evaluate the expression. */
856 val_mark = value_mark ();
857 val = evaluate_expression (expr);
858 cond_result = value_true (val);
859 value_free_to_mark (val_mark);
861 /* If the value is false, then break out of the loop. */
865 /* Execute the body of the while statement. */
866 current = *cmd->body_list;
869 ret = execute_control_command (current);
871 /* If we got an error, or a "break" command, then stop
873 if (ret == invalid_control || ret == break_control)
879 /* If we got a "continue" command, then restart the loop
881 if (ret == continue_control)
884 /* Get the next statement. */
885 current = current->next;
889 /* Reset RET so that we don't recurse the break all the way down. */
890 if (ret == break_control)
891 ret = simple_control;
898 new_line = insert_args (cmd->line);
900 return invalid_control;
901 old_chain = make_cleanup (free_current_contents, &new_line);
902 /* Parse the conditional for the if statement. */
903 expr = parse_expression (new_line);
904 make_cleanup (free_current_contents, &expr);
907 ret = simple_control;
909 /* Evaluate the conditional. */
910 val_mark = value_mark ();
911 val = evaluate_expression (expr);
913 /* Choose which arm to take commands from based on the value of the
914 conditional expression. */
915 if (value_true (val))
916 current = *cmd->body_list;
917 else if (cmd->body_count == 2)
918 current = *(cmd->body_list + 1);
919 value_free_to_mark (val_mark);
921 /* Execute commands in the given arm. */
924 ret = execute_control_command (current);
926 /* If we got an error, get out. */
927 if (ret != simple_control)
930 /* Get the next statement in the body. */
931 current = current->next;
938 warning ("Invalid control type in command structure.");
939 return invalid_control;
943 do_cleanups (old_chain);
948 /* "while" command support. Executes a body of statements while the
949 loop condition is nonzero. */
952 while_command (arg, from_tty)
956 struct command_line *command = NULL;
959 command = get_command_line (while_control, arg);
964 execute_control_command (command);
965 free_command_lines (&command);
968 /* "if" command support. Execute either the true or false arm depending
969 on the value of the if conditional. */
972 if_command (arg, from_tty)
976 struct command_line *command = NULL;
979 command = get_command_line (if_control, arg);
984 execute_control_command (command);
985 free_command_lines (&command);
992 struct user_args *oargs = user_args;
994 fatal ("Internal error, arg_cleanup called with no user args.\n");
996 user_args = user_args->next;
1000 /* Bind the incomming arguments for a user defined command to
1001 $arg0, $arg1 ... $argMAXUSERARGS. */
1003 static struct cleanup *
1007 struct user_args *args;
1008 struct cleanup *old_chain;
1009 unsigned int arg_count = 0;
1011 args = (struct user_args *)xmalloc (sizeof (struct user_args));
1012 memset (args, 0, sizeof (struct user_args));
1014 args->next = user_args;
1017 old_chain = make_cleanup (arg_cleanup, 0);
1029 if (arg_count >= MAXUSERARGS)
1031 error ("user defined function may only have %d arguments.\n",
1036 /* Strip whitespace. */
1037 while (*p == ' ' || *p == '\t')
1040 /* P now points to an argument. */
1042 user_args->a[arg_count].arg = p;
1044 /* Get to the end of this argument. */
1047 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
1053 else if (*p == '\\')
1076 user_args->a[arg_count].len = p - start_arg;
1083 /* Given character string P, return a point to the first argument ($arg),
1084 or NULL if P contains no arguments. */
1090 while ((p = strchr (p, '$')))
1092 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
1099 /* Insert the user defined arguments stored in user_arg into the $arg
1100 arguments found in line, with the updated copy being placed into nline. */
1106 char *p, *save_line, *new_line;
1109 /* First we need to know how much memory to allocate for the new line. */
1112 while ((p = locate_arg (line)))
1117 if (i >= user_args->count)
1119 error ("Missing argument %d in user function.\n", i);
1122 len += user_args->a[i].len;
1126 /* Don't forget the tail. */
1127 len += strlen (line);
1129 /* Allocate space for the new line and fill it in. */
1130 new_line = (char *)xmalloc (len + 1);
1131 if (new_line == NULL)
1134 /* Restore pointer to beginning of old line. */
1137 /* Save pointer to beginning of new line. */
1138 save_line = new_line;
1140 while ((p = locate_arg (line)))
1144 memcpy (new_line, line, p - line);
1145 new_line += p - line;
1148 len = user_args->a[i].len;
1151 memcpy (new_line, user_args->a[i].arg, len);
1156 /* Don't forget the tail. */
1157 strcpy (new_line, line);
1159 /* Return a pointer to the beginning of the new line. */
1164 execute_user_command (c, args)
1165 struct cmd_list_element *c;
1168 register struct command_line *cmdlines;
1169 struct cleanup *old_chain;
1170 enum command_control_type ret;
1172 old_chain = setup_user_args (args);
1174 cmdlines = c->user_commands;
1179 /* Set the instream to 0, indicating execution of a
1180 user-defined function. */
1181 old_chain = make_cleanup (source_cleanup, instream);
1182 instream = (FILE *) 0;
1185 ret = execute_control_command (cmdlines);
1186 if (ret != simple_control && ret != break_control)
1188 warning ("Error in control structure.\n");
1191 cmdlines = cmdlines->next;
1193 do_cleanups (old_chain);
1196 /* Execute the line P as a command.
1197 Pass FROM_TTY as second argument to the defining function. */
1200 execute_command (p, from_tty)
1204 register struct cmd_list_element *c;
1205 register enum language flang;
1206 static int warned = 0;
1207 /* FIXME: These should really be in an appropriate header file */
1208 extern void serial_log_command PARAMS ((const char *));
1212 /* Force cleanup of any alloca areas if using C alloca instead of
1213 a builtin alloca. */
1216 /* This can happen when command_line_input hits end of file. */
1220 serial_log_command (p);
1222 while (*p == ' ' || *p == '\t') p++;
1227 c = lookup_cmd (&p, cmdlist, "", 0, 1);
1228 /* Pass null arg rather than an empty one. */
1231 /* Clear off trailing whitespace, except for set and complete command. */
1232 if (arg && c->type != set_cmd && c->function.cfunc != complete_command)
1234 p = arg + strlen (arg) - 1;
1235 while (p >= arg && (*p == ' ' || *p == '\t'))
1240 /* If this command has been hooked, run the hook first. */
1242 execute_user_command (c->hook, (char *)0);
1244 if (c->class == class_user)
1245 execute_user_command (c, arg);
1246 else if (c->type == set_cmd || c->type == show_cmd)
1247 do_setshow_command (arg, from_tty & caution, c);
1248 else if (c->function.cfunc == NO_FUNCTION)
1249 error ("That is not a command, just a help topic.");
1250 else if (call_command_hook)
1251 call_command_hook (c, arg, from_tty & caution);
1253 (*c->function.cfunc) (arg, from_tty & caution);
1256 /* Tell the user if the language has changed (except first time). */
1257 if (current_language != expected_language)
1259 if (language_mode == language_mode_auto) {
1260 language_info (1); /* Print what changed. */
1265 /* Warn the user if the working language does not match the
1266 language of the current frame. Only warn the user if we are
1267 actually running the program, i.e. there is a stack. */
1268 /* FIXME: This should be cacheing the frame and only running when
1269 the frame changes. */
1271 if (target_has_stack)
1273 flang = get_frame_language ();
1275 && flang != language_unknown
1276 && flang != current_language->la_language)
1278 printf_filtered ("%s\n", lang_frame_mismatch_warn);
1286 command_loop_marker (foo)
1291 /* Read commands from `instream' and execute them
1292 until end of file or error reading instream. */
1297 struct cleanup *old_chain;
1299 int stdin_is_tty = ISATTY (stdin);
1300 long time_at_cmd_start;
1302 long space_at_cmd_start;
1304 extern int display_time;
1305 extern int display_space;
1307 while (instream && !feof (instream))
1309 if (window_hook && instream == stdin)
1310 (*window_hook) (instream, prompt);
1313 if (instream == stdin && stdin_is_tty)
1314 reinitialize_more_filter ();
1315 old_chain = make_cleanup (command_loop_marker, 0);
1316 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
1317 instream == stdin, "prompt");
1321 time_at_cmd_start = get_run_time ();
1326 extern char **environ;
1327 char *lim = (char *) sbrk (0);
1329 space_at_cmd_start = (long) (lim - (char *) &environ);
1333 execute_command (command, instream == stdin);
1334 /* Do any commands attached to breakpoint we stopped at. */
1335 bpstat_do_actions (&stop_bpstat);
1336 do_cleanups (old_chain);
1340 long cmd_time = get_run_time () - time_at_cmd_start;
1342 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1343 cmd_time / 1000000, cmd_time % 1000000);
1349 extern char **environ;
1350 char *lim = (char *) sbrk (0);
1351 long space_now = lim - (char *) &environ;
1352 long space_diff = space_now - space_at_cmd_start;
1354 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1356 (space_diff >= 0 ? '+' : '-'),
1363 /* Commands call this if they do not want to be repeated by null lines. */
1371 /* If we aren't reading from standard input, we are saving the last
1372 thing read from stdin in line and don't want to delete it. Null lines
1373 won't repeat here in any case. */
1374 if (instream == stdin)
1378 /* Read a line from the stream "instream" without command line editing.
1380 It prints PRROMPT once at the start.
1381 Action is compatible with "readline", e.g. space for the result is
1382 malloc'd and should be freed by the caller.
1384 A NULL return means end of file. */
1386 gdb_readline (prrompt)
1391 int input_index = 0;
1392 int result_size = 80;
1396 /* Don't use a _filtered function here. It causes the assumed
1397 character position to be off, since the newline we read from
1398 the user is not accounted for. */
1399 fputs_unfiltered (prrompt, gdb_stdout);
1401 /* Move to a new line so the entered line doesn't have a prompt
1402 on the front of it. */
1403 fputs_unfiltered ("\n", gdb_stdout);
1405 gdb_flush (gdb_stdout);
1408 result = (char *) xmalloc (result_size);
1412 /* Read from stdin if we are executing a user defined command.
1413 This is the right thing for prompt_for_continue, at least. */
1414 c = fgetc (instream ? instream : stdin);
1418 if (input_index > 0)
1419 /* The last line does not end with a newline. Return it, and
1420 if we are called again fgetc will still return EOF and
1421 we'll return NULL then. */
1430 result[input_index++] = c;
1431 while (input_index >= result_size)
1434 result = (char *) xrealloc (result, result_size);
1438 result[input_index++] = '\0';
1442 /* Variables which control command line editing and history
1443 substitution. These variables are given default values at the end
1445 static int command_editing_p;
1446 static int history_expansion_p;
1447 static int write_history_p;
1448 static int history_size;
1449 static char *history_filename;
1451 /* readline uses the word breaks for two things:
1452 (1) In figuring out where to point the TEXT parameter to the
1453 rl_completion_entry_function. Since we don't use TEXT for much,
1454 it doesn't matter a lot what the word breaks are for this purpose, but
1455 it does affect how much stuff M-? lists.
1456 (2) If one of the matches contains a word break character, readline
1457 will quote it. That's why we switch between
1458 gdb_completer_word_break_characters and
1459 gdb_completer_command_word_break_characters. I'm not sure when
1460 we need this behavior (perhaps for funky characters in C++ symbols?). */
1462 /* Variables which are necessary for fancy command line editing. */
1463 char *gdb_completer_word_break_characters =
1464 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1466 /* When completing on command names, we remove '-' from the list of
1467 word break characters, since we use it in command names. If the
1468 readline library sees one in any of the current completion strings,
1469 it thinks that the string needs to be quoted and automatically supplies
1471 char *gdb_completer_command_word_break_characters =
1472 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1474 /* Characters that can be used to quote completion strings. Note that we
1475 can't include '"' because the gdb C parser treats such quoted sequences
1477 char *gdb_completer_quote_characters =
1480 /* Functions that are used as part of the fancy command line editing. */
1482 /* This can be used for functions which don't want to complete on symbols
1483 but don't want to complete on anything else either. */
1486 noop_completer (text, prefix)
1493 /* Complete on filenames. */
1495 filename_completer (text, word)
1499 /* From readline. */
1500 extern char *filename_completion_function PARAMS ((char *, int));
1501 int subsequent_name;
1503 int return_val_used;
1504 int return_val_alloced;
1506 return_val_used = 0;
1507 /* Small for testing. */
1508 return_val_alloced = 1;
1509 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1511 subsequent_name = 0;
1515 p = filename_completion_function (text, subsequent_name);
1516 if (return_val_used >= return_val_alloced)
1518 return_val_alloced *= 2;
1520 (char **) xrealloc (return_val,
1521 return_val_alloced * sizeof (char *));
1525 return_val[return_val_used++] = p;
1528 /* Like emacs, don't complete on old versions. Especially useful
1529 in the "source" command. */
1530 if (p[strlen (p) - 1] == '~')
1536 /* Return exactly p. */
1537 return_val[return_val_used++] = p;
1538 else if (word > text)
1540 /* Return some portion of p. */
1541 q = xmalloc (strlen (p) + 5);
1542 strcpy (q, p + (word - text));
1543 return_val[return_val_used++] = q;
1548 /* Return some of TEXT plus p. */
1549 q = xmalloc (strlen (p) + (text - word) + 5);
1550 strncpy (q, word, text - word);
1551 q[text - word] = '\0';
1553 return_val[return_val_used++] = q;
1557 subsequent_name = 1;
1560 /* There is no way to do this just long enough to affect quote inserting
1561 without also affecting the next completion. This should be fixed in
1563 /* Insure that readline does the right thing
1564 with respect to inserting quotes. */
1565 rl_completer_word_break_characters = "";
1570 /* Here are some useful test cases for completion. FIXME: These should
1571 be put in the test suite. They should be tested with both M-? and TAB.
1573 "show output-" "radix"
1574 "show output" "-radix"
1575 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1576 "p " ambiguous (all symbols)
1577 "info t foo" no completions
1578 "info t " no completions
1579 "info t" ambiguous ("info target", "info terminal", etc.)
1580 "info ajksdlfk" no completions
1581 "info ajksdlfk " no completions
1583 "info " ambiguous (all info commands)
1584 "p \"a" no completions (string constant)
1585 "p 'a" ambiguous (all symbols starting with a)
1586 "p b-a" ambiguous (all symbols starting with a)
1587 "p b-" ambiguous (all symbols)
1588 "file Make" "file" (word break hard to screw up here)
1589 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1592 /* Generate completions one by one for the completer. Each time we are
1593 called return another potential completion to the caller.
1594 line_completion just completes on commands or passes the buck to the
1595 command's completer function, the stuff specific to symbol completion
1596 is in make_symbol_completion_list.
1598 TEXT is the caller's idea of the "word" we are looking at.
1600 MATCHES is the number of matches that have currently been collected from
1601 calling this completion function. When zero, then we need to initialize,
1602 otherwise the initialization has already taken place and we can just
1603 return the next potential completion string.
1605 LINE_BUFFER is available to be looked at; it contains the entire text
1606 of the line. POINT is the offset in that line of the cursor. You
1607 should pretend that the line ends at POINT.
1609 Returns NULL if there are no more completions, else a pointer to a string
1610 which is a possible completion, it is the caller's responsibility to
1614 line_completion_function (text, matches, line_buffer, point)
1620 static char **list = (char **)NULL; /* Cache of completions */
1621 static int index; /* Next cached completion */
1622 char *output = NULL;
1623 char *tmp_command, *p;
1624 /* Pointer within tmp_command which corresponds to text. */
1626 struct cmd_list_element *c, *result_list;
1630 /* The caller is beginning to accumulate a new set of completions, so
1631 we need to find all of them now, and cache them for returning one at
1632 a time on future calls. */
1636 /* Free the storage used by LIST, but not by the strings inside.
1637 This is because rl_complete_internal () frees the strings. */
1643 /* Choose the default set of word break characters to break completions.
1644 If we later find out that we are doing completions on command strings
1645 (as opposed to strings supplied by the individual command completer
1646 functions, which can be any string) then we will switch to the
1647 special word break set for command strings, which leaves out the
1648 '-' character used in some commands. */
1650 rl_completer_word_break_characters =
1651 gdb_completer_word_break_characters;
1653 /* Decide whether to complete on a list of gdb commands or on symbols. */
1654 tmp_command = (char *) alloca (point + 1);
1657 strncpy (tmp_command, line_buffer, point);
1658 tmp_command[point] = '\0';
1659 /* Since text always contains some number of characters leading up
1660 to point, we can find the equivalent position in tmp_command
1661 by subtracting that many characters from the end of tmp_command. */
1662 word = tmp_command + point - strlen (text);
1666 /* An empty line we want to consider ambiguous; that is, it
1667 could be any command. */
1668 c = (struct cmd_list_element *) -1;
1673 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1676 /* Move p up to the next interesting thing. */
1677 while (*p == ' ' || *p == '\t')
1684 /* It is an unrecognized command. So there are no
1685 possible completions. */
1688 else if (c == (struct cmd_list_element *) -1)
1692 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1693 doesn't advance over that thing itself. Do so now. */
1695 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1697 if (q != tmp_command + point)
1699 /* There is something beyond the ambiguous
1700 command, so there are no possible completions. For
1701 example, "info t " or "info t foo" does not complete
1702 to anything, because "info t" can be "info target" or
1708 /* We're trying to complete on the command which was ambiguous.
1709 This we can deal with. */
1712 list = complete_on_cmdlist (*result_list->prefixlist, p,
1717 list = complete_on_cmdlist (cmdlist, p, word);
1719 /* Insure that readline does the right thing with respect to
1720 inserting quotes. */
1721 rl_completer_word_break_characters =
1722 gdb_completer_command_word_break_characters;
1727 /* We've recognized a full command. */
1729 if (p == tmp_command + point)
1731 /* There is no non-whitespace in the line beyond the command. */
1733 if (p[-1] == ' ' || p[-1] == '\t')
1735 /* The command is followed by whitespace; we need to complete
1736 on whatever comes after command. */
1739 /* It is a prefix command; what comes after it is
1740 a subcommand (e.g. "info "). */
1741 list = complete_on_cmdlist (*c->prefixlist, p, word);
1743 /* Insure that readline does the right thing
1744 with respect to inserting quotes. */
1745 rl_completer_word_break_characters =
1746 gdb_completer_command_word_break_characters;
1750 list = complete_on_enum (c->enums, p, word);
1751 rl_completer_word_break_characters =
1752 gdb_completer_command_word_break_characters;
1756 /* It is a normal command; what comes after it is
1757 completed by the command's completer function. */
1758 list = (*c->completer) (p, word);
1763 /* The command is not followed by whitespace; we need to
1764 complete on the command itself. e.g. "p" which is a
1765 command itself but also can complete to "print", "ptype"
1769 /* Find the command we are completing on. */
1771 while (q > tmp_command)
1773 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1779 list = complete_on_cmdlist (result_list, q, word);
1781 /* Insure that readline does the right thing
1782 with respect to inserting quotes. */
1783 rl_completer_word_break_characters =
1784 gdb_completer_command_word_break_characters;
1789 /* There is non-whitespace beyond the command. */
1791 if (c->prefixlist && !c->allow_unknown)
1793 /* It is an unrecognized subcommand of a prefix command,
1794 e.g. "info adsfkdj". */
1799 list = complete_on_enum (c->enums, p, word);
1803 /* It is a normal command. */
1804 list = (*c->completer) (p, word);
1810 /* If we found a list of potential completions during initialization then
1811 dole them out one at a time. The vector of completions is NULL
1812 terminated, so after returning the last one, return NULL (and continue
1813 to do so) each time we are called after that, until a new list is
1818 output = list[index];
1826 /* Can't do this because readline hasn't yet checked the word breaks
1827 for figuring out whether to insert a quote. */
1829 /* Make sure the word break characters are set back to normal for the
1830 next time that readline tries to complete something. */
1831 rl_completer_word_break_characters =
1832 gdb_completer_word_break_characters;
1838 /* Line completion interface function for readline. */
1841 readline_line_completion_function (text, matches)
1845 return line_completion_function (text, matches, rl_line_buffer, rl_point);
1848 /* Skip over a possibly quoted word (as defined by the quote characters
1849 and word break characters the completer uses). Returns pointer to the
1850 location after the "word". */
1856 char quote_char = '\0';
1859 for (scan = str; *scan != '\0'; scan++)
1861 if (quote_char != '\0')
1863 /* Ignore everything until the matching close quote char */
1864 if (*scan == quote_char)
1866 /* Found matching close quote. */
1871 else if (strchr (gdb_completer_quote_characters, *scan))
1873 /* Found start of a quoted string. */
1876 else if (strchr (gdb_completer_word_break_characters, *scan))
1890 #if STOP_SIGNAL == SIGTSTP
1891 signal (SIGTSTP, SIG_DFL);
1893 kill (getpid (), SIGTSTP);
1894 signal (SIGTSTP, stop_sig);
1896 signal (STOP_SIGNAL, stop_sig);
1898 printf_unfiltered ("%s", prompt);
1899 gdb_flush (gdb_stdout);
1901 /* Forget about any previous command -- null line now will do nothing. */
1904 #endif /* STOP_SIGNAL */
1906 /* Initialize signal handlers. */
1916 signal (SIGINT, request_quit);
1918 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1919 to the inferior and breakpoints will be ignored. */
1921 signal (SIGTRAP, SIG_DFL);
1924 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1925 passed to the inferior, which we don't want. It would be
1926 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1927 on BSD4.3 systems using vfork, that can affect the
1928 GDB process as well as the inferior (the signal handling tables
1929 might be in memory, shared between the two). Since we establish
1930 a handler for SIGQUIT, when we call exec it will set the signal
1931 to SIG_DFL for us. */
1932 signal (SIGQUIT, do_nothing);
1934 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1935 signal (SIGHUP, disconnect);
1937 signal (SIGFPE, float_handler);
1939 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1940 signal (SIGWINCH, SIGWINCH_HANDLER);
1944 /* Read one line from the command input stream `instream'
1945 into the local static buffer `linebuffer' (whose current length
1947 The buffer is made bigger as necessary.
1948 Returns the address of the start of the line.
1950 NULL is returned for end of file.
1952 *If* the instream == stdin & stdin is a terminal, the line read
1953 is copied into the file line saver (global var char *line,
1954 length linesize) so that it can be duplicated.
1956 This routine either uses fancy command line editing or
1957 simple input as the user has requested. */
1960 command_line_input (prrompt, repeat, annotation_suffix)
1963 char *annotation_suffix;
1965 static char *linebuffer = 0;
1966 static unsigned linelength = 0;
1970 char *local_prompt = prrompt;
1974 /* The annotation suffix must be non-NULL. */
1975 if (annotation_suffix == NULL)
1976 annotation_suffix = "";
1978 if (annotation_level > 1 && instream == stdin)
1980 local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt))
1981 + strlen (annotation_suffix) + 40);
1982 if (prrompt == NULL)
1983 local_prompt[0] = '\0';
1985 strcpy (local_prompt, prrompt);
1986 strcat (local_prompt, "\n\032\032");
1987 strcat (local_prompt, annotation_suffix);
1988 strcat (local_prompt, "\n");
1991 if (linebuffer == 0)
1994 linebuffer = (char *) xmalloc (linelength);
1999 /* Control-C quits instantly if typed while in this loop
2000 since it should not wait until the user types a newline. */
2004 signal (STOP_SIGNAL, stop_sig);
2009 /* Make sure that all output has been output. Some machines may let
2010 you get away with leaving out some of the gdb_flush, but not all. */
2012 gdb_flush (gdb_stdout);
2013 gdb_flush (gdb_stderr);
2015 if (source_file_name != NULL)
2017 ++source_line_number;
2018 sprintf (source_error,
2019 "%s%s:%d: Error in sourced command file:\n",
2022 source_line_number);
2023 error_pre_print = source_error;
2026 if (annotation_level > 1 && instream == stdin)
2028 printf_unfiltered ("\n\032\032pre-");
2029 printf_unfiltered (annotation_suffix);
2030 printf_unfiltered ("\n");
2033 /* Don't use fancy stuff if not talking to stdin. */
2034 if (readline_hook && instream == NULL)
2036 rl = (*readline_hook) (local_prompt);
2038 else if (command_editing_p && instream == stdin && ISATTY (instream))
2040 rl = readline (local_prompt);
2044 rl = gdb_readline (local_prompt);
2047 if (annotation_level > 1 && instream == stdin)
2049 printf_unfiltered ("\n\032\032post-");
2050 printf_unfiltered (annotation_suffix);
2051 printf_unfiltered ("\n");
2054 if (!rl || rl == (char *) EOF)
2059 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
2061 linelength = strlen(rl) + 1 + (p - linebuffer);
2062 nline = (char *) xrealloc (linebuffer, linelength);
2063 p += nline - linebuffer;
2067 /* Copy line. Don't copy null at end. (Leaves line alone
2068 if this was just a newline) */
2072 free (rl); /* Allocated in readline. */
2074 if (p == linebuffer || *(p - 1) != '\\')
2077 p--; /* Put on top of '\'. */
2078 local_prompt = (char *) 0;
2083 signal (STOP_SIGNAL, SIG_DFL);
2090 #define SERVER_COMMAND_LENGTH 7
2092 (p - linebuffer > SERVER_COMMAND_LENGTH)
2093 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
2096 /* Note that we don't set `line'. Between this and the check in
2097 dont_repeat, this insures that repeating will still do the
2100 return linebuffer + SERVER_COMMAND_LENGTH;
2103 /* Do history expansion if that is wished. */
2104 if (history_expansion_p && instream == stdin
2105 && ISATTY (instream))
2107 char *history_value;
2110 *p = '\0'; /* Insert null now. */
2111 expanded = history_expand (linebuffer, &history_value);
2114 /* Print the changes. */
2115 printf_unfiltered ("%s\n", history_value);
2117 /* If there was an error, call this function again. */
2120 free (history_value);
2121 return command_line_input (prrompt, repeat, annotation_suffix);
2123 if (strlen (history_value) > linelength)
2125 linelength = strlen (history_value) + 1;
2126 linebuffer = (char *) xrealloc (linebuffer, linelength);
2128 strcpy (linebuffer, history_value);
2129 p = linebuffer + strlen(linebuffer);
2130 free (history_value);
2134 /* If we just got an empty line, and that is supposed
2135 to repeat the previous command, return the value in the
2137 if (repeat && p == linebuffer)
2139 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++) ;
2145 /* Add line to history if appropriate. */
2146 if (instream == stdin
2147 && ISATTY (stdin) && *linebuffer)
2148 add_history (linebuffer);
2150 /* Note: lines consisting solely of comments are added to the command
2151 history. This is useful when you type a command, and then
2152 realize you don't want to execute it quite yet. You can comment
2153 out the command and then later fetch it from the value history
2154 and remove the '#'. The kill ring is probably better, but some
2155 people are in the habit of commenting things out. */
2157 *p1 = '\0'; /* Found a comment. */
2159 /* Save into global buffer if appropriate. */
2162 if (linelength > linesize)
2164 line = xrealloc (line, linelength);
2165 linesize = linelength;
2167 strcpy (line, linebuffer);
2175 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2176 code bodies. This is typically used when we encounter an "else"
2177 clause for an "if" command. */
2180 realloc_body_list (command, new_length)
2181 struct command_line *command;
2185 struct command_line **body_list;
2187 n = command->body_count;
2189 /* Nothing to do? */
2190 if (new_length <= n)
2193 body_list = (struct command_line **)
2194 xmalloc (sizeof (struct command_line *) * new_length);
2196 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
2198 free (command->body_list);
2199 command->body_list = body_list;
2200 command->body_count = new_length;
2203 /* Read one line from the input stream. If the command is an "else" or
2204 "end", return such an indication to the caller. */
2206 static enum misc_command_type
2207 read_next_line (command)
2208 struct command_line **command;
2210 char *p, *p1, *prompt_ptr, control_prompt[256];
2213 if (control_level >= 254)
2214 error ("Control nesting too deep!\n");
2216 /* Set a prompt based on the nesting of the control commands. */
2217 if (instream == stdin || (instream == 0 && readline_hook != NULL))
2219 for (i = 0; i < control_level; i++)
2220 control_prompt[i] = ' ';
2221 control_prompt[i] = '>';
2222 control_prompt[i+1] = '\0';
2223 prompt_ptr = (char *)&control_prompt[0];
2228 p = command_line_input (prompt_ptr, instream == stdin, "commands");
2230 /* Not sure what to do here. */
2234 /* Strip leading and trailing whitespace. */
2235 while (*p == ' ' || *p == '\t')
2238 p1 = p + strlen (p);
2239 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
2242 /* Blanks and comments don't really do anything, but we need to
2243 distinguish them from else, end and other commands which can be
2245 if (p1 == p || p[0] == '#')
2248 /* Is this the end of a simple, while, or if control structure? */
2249 if (p1 - p == 3 && !strncmp (p, "end", 3))
2252 /* Is the else clause of an if control structure? */
2253 if (p1 - p == 4 && !strncmp (p, "else", 4))
2254 return else_command;
2256 /* Check for while, if, break, continue, etc and build a new command
2257 line structure for them. */
2258 if (p1 - p > 5 && !strncmp (p, "while", 5))
2259 *command = build_command_line (while_control, p + 6);
2260 else if (p1 - p > 2 && !strncmp (p, "if", 2))
2261 *command = build_command_line (if_control, p + 3);
2262 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
2264 *command = (struct command_line *)
2265 xmalloc (sizeof (struct command_line));
2266 (*command)->next = NULL;
2267 (*command)->line = NULL;
2268 (*command)->control_type = break_control;
2269 (*command)->body_count = 0;
2270 (*command)->body_list = NULL;
2272 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
2274 *command = (struct command_line *)
2275 xmalloc (sizeof (struct command_line));
2276 (*command)->next = NULL;
2277 (*command)->line = NULL;
2278 (*command)->control_type = continue_control;
2279 (*command)->body_count = 0;
2280 (*command)->body_list = NULL;
2284 /* A normal command. */
2285 *command = (struct command_line *)
2286 xmalloc (sizeof (struct command_line));
2287 (*command)->next = NULL;
2288 (*command)->line = savestring (p, p1 - p);
2289 (*command)->control_type = simple_control;
2290 (*command)->body_count = 0;
2291 (*command)->body_list = NULL;
2294 /* Nothing special. */
2298 /* Recursively read in the control structures and create a command_line
2299 structure from them.
2301 The parent_control parameter is the control structure in which the
2302 following commands are nested. */
2304 static enum command_control_type
2305 recurse_read_control_structure (current_cmd)
2306 struct command_line *current_cmd;
2308 int current_body, i;
2309 enum misc_command_type val;
2310 enum command_control_type ret;
2311 struct command_line **body_ptr, *child_tail, *next;
2316 /* Sanity checks. */
2317 if (current_cmd->control_type == simple_control)
2319 error ("Recursed on a simple control type\n");
2320 return invalid_control;
2323 if (current_body > current_cmd->body_count)
2325 error ("Allocated body is smaller than this command type needs\n");
2326 return invalid_control;
2329 /* Read lines from the input stream and build control structures. */
2335 val = read_next_line (&next);
2337 /* Just skip blanks and comments. */
2338 if (val == nop_command)
2341 if (val == end_command)
2343 if (current_cmd->control_type == while_control
2344 || current_cmd->control_type == if_control)
2346 /* Success reading an entire control structure. */
2347 ret = simple_control;
2352 ret = invalid_control;
2357 /* Not the end of a control structure. */
2358 if (val == else_command)
2360 if (current_cmd->control_type == if_control
2361 && current_body == 1)
2363 realloc_body_list (current_cmd, 2);
2370 ret = invalid_control;
2377 child_tail->next = next;
2381 body_ptr = current_cmd->body_list;
2382 for (i = 1; i < current_body; i++)
2391 /* If the latest line is another control structure, then recurse
2393 if (next->control_type == while_control
2394 || next->control_type == if_control)
2397 ret = recurse_read_control_structure (next);
2400 if (ret != simple_control)
2410 /* Read lines from the input stream and accumulate them in a chain of
2411 struct command_line's, which is then returned. For input from a
2412 terminal, the special command "end" is used to mark the end of the
2413 input, and is not included in the returned chain of commands. */
2415 #define END_MESSAGE "End with a line saying just \"end\"."
2417 struct command_line *
2418 read_command_lines (prompt, from_tty)
2422 struct command_line *head, *tail, *next;
2423 struct cleanup *old_chain;
2424 enum command_control_type ret;
2425 enum misc_command_type val;
2427 if (readline_begin_hook)
2429 /* Note - intentional to merge messages with no newline */
2430 (*readline_begin_hook) ("%s %s\n", prompt, END_MESSAGE);
2432 else if (from_tty && input_from_terminal_p ())
2434 printf_unfiltered ("%s\n%s\n", prompt, END_MESSAGE);
2435 gdb_flush (gdb_stdout);
2443 val = read_next_line (&next);
2445 /* Ignore blank lines or comments. */
2446 if (val == nop_command)
2449 if (val == end_command)
2451 ret = simple_control;
2455 if (val != ok_command)
2457 ret = invalid_control;
2461 if (next->control_type == while_control
2462 || next->control_type == if_control)
2465 ret = recurse_read_control_structure (next);
2468 if (ret == invalid_control)
2479 old_chain = make_cleanup (free_command_lines, &head);
2488 if (ret != invalid_control)
2490 discard_cleanups (old_chain);
2493 do_cleanups (old_chain);
2496 if (readline_end_hook)
2498 (*readline_end_hook) ();
2503 /* Free a chain of struct command_line's. */
2506 free_command_lines (lptr)
2507 struct command_line **lptr;
2509 register struct command_line *l = *lptr;
2510 register struct command_line *next;
2511 struct command_line **blist;
2516 if (l->body_count > 0)
2518 blist = l->body_list;
2519 for (i = 0; i < l->body_count; i++, blist++)
2520 free_command_lines (blist);
2529 /* Add an element to the list of info subcommands. */
2532 add_info (name, fun, doc)
2534 void (*fun) PARAMS ((char *, int));
2537 add_cmd (name, no_class, fun, doc, &infolist);
2540 /* Add an alias to the list of info subcommands. */
2543 add_info_alias (name, oldname, abbrev_flag)
2548 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2551 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2552 Therefore, its own definition is called only for "info" with no args. */
2556 info_command (arg, from_tty)
2560 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2561 help_list (infolist, "info ", -1, gdb_stdout);
2564 /* The "complete" command is used by Emacs to implement completion. */
2568 complete_command (arg, from_tty)
2580 argpoint = strlen (arg);
2582 for (completion = line_completion_function (arg, i = 0, arg, argpoint);
2584 completion = line_completion_function (arg, ++i, arg, argpoint))
2586 printf_unfiltered ("%s\n", completion);
2591 /* The "show" command with no arguments shows all the settings. */
2595 show_command (arg, from_tty)
2599 cmd_show_list (showlist, from_tty, "");
2602 /* Add an element to the list of commands. */
2605 add_com (name, class, fun, doc)
2607 enum command_class class;
2608 void (*fun) PARAMS ((char *, int));
2611 add_cmd (name, class, fun, doc, &cmdlist);
2614 /* Add an alias or abbreviation command to the list of commands. */
2617 add_com_alias (name, oldname, class, abbrev_flag)
2620 enum command_class class;
2623 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2630 error ("Argument required (%s).", why);
2635 help_command (command, from_tty)
2637 int from_tty; /* Ignored */
2639 help_cmd (command, gdb_stdout);
2643 validate_comname (comname)
2649 error_no_arg ("name of command to define");
2654 if (!isalnum(*p) && *p != '-' && *p != '_')
2655 error ("Junk in argument list: \"%s\"", p);
2660 /* This is just a placeholder in the command data structures. */
2662 user_defined_command (ignore, from_tty)
2669 define_command (comname, from_tty)
2673 register struct command_line *cmds;
2674 register struct cmd_list_element *c, *newc, *hookc = 0;
2675 char *tem = comname;
2677 #define HOOK_STRING "hook-"
2680 validate_comname (comname);
2682 /* Look it up, and verify that we got an exact match. */
2683 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
2684 if (c && !STREQ (comname, c->name))
2689 if (c->class == class_user || c->class == class_alias)
2690 tem = "Redefine command \"%s\"? ";
2692 tem = "Really redefine built-in command \"%s\"? ";
2693 if (!query (tem, c->name))
2694 error ("Command \"%s\" not redefined.", c->name);
2697 /* If this new command is a hook, then mark the command which it
2698 is hooking. Note that we allow hooking `help' commands, so that
2699 we can hook the `stop' pseudo-command. */
2701 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
2703 /* Look up cmd it hooks, and verify that we got an exact match. */
2704 tem = comname+HOOK_LEN;
2705 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
2706 if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
2710 warning ("Your new `%s' command does not hook any existing command.",
2712 if (!query ("Proceed? "))
2713 error ("Not confirmed.");
2717 comname = savestring (comname, strlen (comname));
2719 /* If the rest of the commands will be case insensitive, this one
2720 should behave in the same manner. */
2721 for (tem = comname; *tem; tem++)
2722 if (isupper(*tem)) *tem = tolower(*tem);
2725 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
2726 cmds = read_command_lines (tmpbuf, from_tty);
2728 if (c && c->class == class_user)
2729 free_command_lines (&c->user_commands);
2731 newc = add_cmd (comname, class_user, user_defined_command,
2732 (c && c->class == class_user)
2733 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
2734 newc->user_commands = cmds;
2736 /* If this new command is a hook, then mark both commands as being
2740 hookc->hook = newc; /* Target gets hooked. */
2741 newc->hookee = hookc; /* We are marked as hooking target cmd. */
2746 document_command (comname, from_tty)
2750 struct command_line *doclines;
2751 register struct cmd_list_element *c;
2752 char *tem = comname;
2755 validate_comname (comname);
2757 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
2759 if (c->class != class_user)
2760 error ("Command \"%s\" is built-in.", comname);
2762 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
2763 doclines = read_command_lines (tmpbuf, from_tty);
2765 if (c->doc) free (c->doc);
2768 register struct command_line *cl1;
2769 register int len = 0;
2771 for (cl1 = doclines; cl1; cl1 = cl1->next)
2772 len += strlen (cl1->line) + 1;
2774 c->doc = (char *) xmalloc (len + 1);
2777 for (cl1 = doclines; cl1; cl1 = cl1->next)
2779 strcat (c->doc, cl1->line);
2781 strcat (c->doc, "\n");
2785 free_command_lines (&doclines);
2789 print_gdb_version (stream)
2792 /* From GNU coding standards, first line is meant to be easy for a
2793 program to parse, and is just canonical program name and version
2794 number, which starts after last space. */
2796 fprintf_filtered (stream, "GNU gdb %s\n", version);
2798 /* Second line is a copyright notice. */
2800 fprintf_filtered (stream, "Copyright 1997 Free Software Foundation, Inc.\n");
2802 /* Following the copyright is a brief statement that the program is
2803 free software, that users are free to copy and change it on
2804 certain conditions, that it is covered by the GNU GPL, and that
2805 there is no warranty. */
2807 fprintf_filtered (stream, "\
2808 GDB is free software, covered by the GNU General Public License, and you are\n\
2809 welcome to change it and/or distribute copies of it under certain conditions.\n\
2810 Type \"show copying\" to see the conditions.\n\
2811 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2813 /* After the required info we print the configuration information. */
2815 fprintf_filtered (stream, "This GDB was configured as \"");
2816 if (!STREQ (host_name, target_name))
2818 fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
2822 fprintf_filtered (stream, "%s", host_name);
2824 fprintf_filtered (stream, "\".");
2829 show_version (args, from_tty)
2834 print_gdb_version (gdb_stdout);
2835 printf_filtered ("\n");
2839 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2845 printf_unfiltered ("%s", prompt);
2846 gdb_flush (gdb_stdout);
2850 quit_command (args, from_tty)
2856 /* An optional expression may be used to cause gdb to terminate with the
2857 value of that expression. */
2860 value_ptr val = parse_and_eval (args);
2862 exit_code = (int) value_as_long (val);
2865 if (inferior_pid != 0 && target_has_execution)
2869 if (query ("The program is running. Quit anyway (and detach it)? "))
2870 target_detach (args, from_tty);
2872 error ("Not confirmed.");
2876 if (query ("The program is running. Quit anyway (and kill it)? "))
2879 error ("Not confirmed.");
2882 /* UDI wants this, to kill the TIP. */
2885 /* Save the history information if it is appropriate to do so. */
2886 if (write_history_p && history_filename)
2887 write_history (history_filename);
2889 do_final_cleanups(ALL_CLEANUPS); /* Do any final cleanups before exiting */
2894 /* Returns whether GDB is running on a terminal and whether the user
2895 desires that questions be asked of them on that terminal. */
2898 input_from_terminal_p ()
2900 return gdb_has_a_terminal () && (instream == stdin) & caution;
2905 pwd_command (args, from_tty)
2909 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
2910 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
2912 if (!STREQ (gdb_dirbuf, current_directory))
2913 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2914 current_directory, gdb_dirbuf);
2916 printf_unfiltered ("Working directory %s.\n", current_directory);
2920 cd_command (dir, from_tty)
2925 /* Found something other than leading repetitions of "/..". */
2926 int found_real_path;
2929 /* If the new directory is absolute, repeat is a no-op; if relative,
2930 repeat might be useful but is more likely to be a mistake. */
2934 error_no_arg ("new working directory");
2936 dir = tilde_expand (dir);
2937 make_cleanup (free, dir);
2939 if (chdir (dir) < 0)
2940 perror_with_name (dir);
2943 dir = savestring (dir, len - (len > 1 && SLASH_P(dir[len-1])));
2945 current_directory = dir;
2948 if (SLASH_P (current_directory[0]) && current_directory[1] == '\0')
2949 current_directory = concat (current_directory, dir, NULL);
2951 current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
2955 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2957 found_real_path = 0;
2958 for (p = current_directory; *p;)
2960 if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2])))
2962 else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.'
2963 && (p[3] == 0 || SLASH_P (p[3])))
2965 if (found_real_path)
2967 /* Search backwards for the directory just before the "/.."
2968 and obliterate it and the "/..". */
2970 while (q != current_directory && ! SLASH_P (q[-1]))
2973 if (q == current_directory)
2974 /* current_directory is
2975 a relative pathname ("can't happen"--leave it alone). */
2979 strcpy (q - 1, p + 3);
2984 /* We are dealing with leading repetitions of "/..", for example
2985 "/../..", which is the Mach super-root. */
2990 found_real_path = 1;
2995 forget_cached_source_info ();
2998 pwd_command ((char *) 0, 1);
3001 struct source_cleanup_lines_args {
3004 char *old_pre_error;
3005 char *old_error_pre_print;
3009 source_cleanup_lines (args)
3012 struct source_cleanup_lines_args *p =
3013 (struct source_cleanup_lines_args *)args;
3014 source_line_number = p->old_line;
3015 source_file_name = p->old_file;
3016 source_pre_error = p->old_pre_error;
3017 error_pre_print = p->old_error_pre_print;
3022 source_command (args, from_tty)
3027 struct cleanup *old_cleanups;
3029 struct source_cleanup_lines_args old_lines;
3034 error ("source command requires pathname of file to source.");
3037 file = tilde_expand (file);
3038 old_cleanups = make_cleanup (free, file);
3040 stream = fopen (file, FOPEN_RT);
3043 perror_with_name (file);
3047 make_cleanup (fclose, stream);
3049 old_lines.old_line = source_line_number;
3050 old_lines.old_file = source_file_name;
3051 old_lines.old_pre_error = source_pre_error;
3052 old_lines.old_error_pre_print = error_pre_print;
3053 make_cleanup (source_cleanup_lines, &old_lines);
3054 source_line_number = 0;
3055 source_file_name = file;
3056 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
3057 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
3058 make_cleanup (free, source_pre_error);
3059 /* This will get set every time we read a line. So it won't stay "" for
3061 error_pre_print = "";
3063 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
3064 if (source_error_allocated < needed_length)
3066 source_error_allocated *= 2;
3067 if (source_error_allocated < needed_length)
3068 source_error_allocated = needed_length;
3069 if (source_error == NULL)
3070 source_error = xmalloc (source_error_allocated);
3072 source_error = xrealloc (source_error, source_error_allocated);
3075 read_command_file (stream);
3077 do_cleanups (old_cleanups);
3082 echo_command (text, from_tty)
3090 while ((c = *p++) != '\0')
3094 /* \ at end of argument is used after spaces
3095 so they won't be lost. */
3099 c = parse_escape (&p);
3101 printf_filtered ("%c", c);
3104 printf_filtered ("%c", c);
3107 /* Force this output to appear now. */
3109 gdb_flush (gdb_stdout);
3114 dont_repeat_command (ignored, from_tty)
3118 *line = 0; /* Can't call dont_repeat here because we're not
3119 necessarily reading from stdin. */
3122 /* Functions to manipulate the endianness of the target. */
3124 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3125 #ifndef TARGET_BYTE_ORDER_DEFAULT
3126 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
3128 int target_byte_order = TARGET_BYTE_ORDER_DEFAULT;
3129 int target_byte_order_auto = 1;
3131 static int target_byte_order_auto = 0;
3134 /* Called if the user enters ``set endian'' without an argument. */
3136 set_endian (args, from_tty)
3140 printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
3141 show_endian (args, from_tty);
3144 /* Called by ``set endian big''. */
3146 set_endian_big (args, from_tty)
3150 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3151 target_byte_order = BIG_ENDIAN;
3152 target_byte_order_auto = 0;
3154 printf_unfiltered ("Byte order is not selectable.");
3155 show_endian (args, from_tty);
3159 /* Called by ``set endian little''. */
3161 set_endian_little (args, from_tty)
3165 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3166 target_byte_order = LITTLE_ENDIAN;
3167 target_byte_order_auto = 0;
3169 printf_unfiltered ("Byte order is not selectable.");
3170 show_endian (args, from_tty);
3174 /* Called by ``set endian auto''. */
3176 set_endian_auto (args, from_tty)
3180 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3181 target_byte_order_auto = 1;
3183 printf_unfiltered ("Byte order is not selectable.");
3184 show_endian (args, from_tty);
3188 /* Called by ``show endian''. */
3190 show_endian (args, from_tty)
3195 (target_byte_order_auto
3196 ? "The target endianness is set automatically (currently %s endian)\n"
3197 : "The target is assumed to be %s endian\n");
3198 printf_unfiltered ((char *) msg, TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
3201 /* Set the endianness from a BFD. */
3203 set_endian_from_file (abfd)
3206 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3209 if (bfd_big_endian (abfd))
3212 want = LITTLE_ENDIAN;
3213 if (target_byte_order_auto)
3214 target_byte_order = want;
3215 else if (target_byte_order != want)
3216 warning ("%s endian file does not match %s endian target.",
3217 want == BIG_ENDIAN ? "big" : "little",
3218 TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
3220 #else /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3222 if (bfd_big_endian (abfd)
3223 ? TARGET_BYTE_ORDER != BIG_ENDIAN
3224 : TARGET_BYTE_ORDER == BIG_ENDIAN)
3225 warning ("%s endian file does not match %s endian target.",
3226 bfd_big_endian (abfd) ? "big" : "little",
3227 TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
3229 #endif /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3232 /* Functions to manipulate command line editing control variables. */
3234 /* Number of commands to print in each call to show_commands. */
3235 #define Hist_print 10
3237 show_commands (args, from_tty)
3241 /* Index for history commands. Relative to history_base. */
3244 /* Number of the history entry which we are planning to display next.
3245 Relative to history_base. */
3248 /* The first command in the history which doesn't exist (i.e. one more
3249 than the number of the last command). Relative to history_base. */
3252 extern HIST_ENTRY *history_get PARAMS ((int));
3254 /* Print out some of the commands from the command history. */
3255 /* First determine the length of the history list. */
3256 hist_len = history_size;
3257 for (offset = 0; offset < history_size; offset++)
3259 if (!history_get (history_base + offset))
3268 if (args[0] == '+' && args[1] == '\0')
3269 /* "info editing +" should print from the stored position. */
3272 /* "info editing <exp>" should print around command number <exp>. */
3273 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
3275 /* "show commands" means print the last Hist_print commands. */
3278 num = hist_len - Hist_print;
3284 /* If there are at least Hist_print commands, we want to display the last
3285 Hist_print rather than, say, the last 6. */
3286 if (hist_len - num < Hist_print)
3288 num = hist_len - Hist_print;
3293 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
3295 printf_filtered ("%5d %s\n", history_base + offset,
3296 (history_get (history_base + offset))->line);
3299 /* The next command we want to display is the next one that we haven't
3303 /* If the user repeats this command with return, it should do what
3304 "show commands +" does. This is unnecessary if arg is null,
3305 because "show commands +" is not useful after "show commands". */
3306 if (from_tty && args)
3313 /* Called by do_setshow_command. */
3316 set_history_size_command (args, from_tty, c)
3319 struct cmd_list_element *c;
3321 if (history_size == INT_MAX)
3322 unstifle_history ();
3323 else if (history_size >= 0)
3324 stifle_history (history_size);
3327 history_size = INT_MAX;
3328 error ("History size must be non-negative");
3334 set_history (args, from_tty)
3338 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3339 help_list (sethistlist, "set history ", -1, gdb_stdout);
3344 show_history (args, from_tty)
3348 cmd_show_list (showhistlist, from_tty, "");
3351 int info_verbose = 0; /* Default verbose msgs off */
3353 /* Called by do_setshow_command. An elaborate joke. */
3356 set_verbose (args, from_tty, c)
3359 struct cmd_list_element *c;
3361 char *cmdname = "verbose";
3362 struct cmd_list_element *showcmd;
3364 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
3368 c->doc = "Set verbose printing of informational messages.";
3369 showcmd->doc = "Show verbose printing of informational messages.";
3373 c->doc = "Set verbosity.";
3374 showcmd->doc = "Show verbosity.";
3379 float_handler (signo)
3382 /* This message is based on ANSI C, section 4.7. Note that integer
3383 divide by zero causes this, so "float" is a misnomer. */
3384 signal (SIGFPE, float_handler);
3385 error ("Erroneous arithmetic operation.");
3397 enablebreaklist = NULL;
3403 showhistlist = NULL;
3404 unsethistlist = NULL;
3405 #if MAINTENANCE_CMDS
3406 maintenancelist = NULL;
3407 maintenanceinfolist = NULL;
3408 maintenanceprintlist = NULL;
3410 setprintlist = NULL;
3411 showprintlist = NULL;
3412 setchecklist = NULL;
3413 showchecklist = NULL;
3416 /* Init the history buffer. Note that we are called after the init file(s)
3417 * have been read so that the user can change the history file via his
3418 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3419 * overrides all of this.
3427 tmpenv = getenv ("HISTSIZE");
3429 history_size = atoi (tmpenv);
3430 else if (!history_size)
3433 stifle_history (history_size);
3435 tmpenv = getenv ("GDBHISTFILE");
3437 history_filename = savestring (tmpenv, strlen(tmpenv));
3438 else if (!history_filename) {
3439 /* We include the current directory so that if the user changes
3440 directories the file written will be the same as the one
3442 history_filename = concat (current_directory, "/.gdb_history", NULL);
3444 read_history (history_filename);
3450 struct cmd_list_element *c;
3452 add_prefix_cmd ("endian", class_support, set_endian,
3453 "Set endianness of target.",
3454 &endianlist, "set endian ", 0, &setlist);
3455 add_cmd ("big", class_support, set_endian_big,
3456 "Set target as being big endian.", &endianlist);
3457 add_cmd ("little", class_support, set_endian_little,
3458 "Set target as being little endian.", &endianlist);
3459 add_cmd ("auto", class_support, set_endian_auto,
3460 "Select target endianness automatically.", &endianlist);
3461 add_cmd ("endian", class_support, show_endian,
3462 "Show endianness of target.", &showlist);
3464 #ifdef DEFAULT_PROMPT
3465 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
3467 prompt = savestring ("(gdb) ", 6);
3470 /* Set the important stuff up for command editing. */
3471 command_editing_p = 1;
3472 history_expansion_p = 0;
3473 write_history_p = 0;
3475 /* Setup important stuff for command line editing. */
3476 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
3477 rl_completer_word_break_characters = gdb_completer_word_break_characters;
3478 rl_completer_quote_characters = gdb_completer_quote_characters;
3479 rl_readline_name = "gdb";
3481 /* Define the classes of commands.
3482 They will appear in the help list in the reverse of this order. */
3484 add_cmd ("internals", class_maintenance, NO_FUNCTION,
3485 "Maintenance commands.\n\
3486 Some gdb commands are provided just for use by gdb maintainers.\n\
3487 These commands are subject to frequent change, and may not be as\n\
3488 well documented as user commands.",
3490 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
3491 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
3492 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
3493 The commands in this class are those defined by the user.\n\
3494 Use the \"define\" command to define a command.", &cmdlist);
3495 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
3496 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
3497 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
3498 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
3499 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
3500 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
3501 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3502 counting from zero for the innermost (currently executing) frame.\n\n\
3503 At any time gdb identifies one frame as the \"selected\" frame.\n\
3504 Variable lookups are done with respect to the selected frame.\n\
3505 When the program being debugged stops, gdb selects the innermost frame.\n\
3506 The commands below can be used to select other frames by number or address.",
3508 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
3510 add_com ("pwd", class_files, pwd_command,
3511 "Print working directory. This is used for your program as well.");
3512 c = add_cmd ("cd", class_files, cd_command,
3513 "Set working directory to DIR for debugger and program being debugged.\n\
3514 The change does not take effect for the program being debugged\n\
3515 until the next time it is started.", &cmdlist);
3516 c->completer = filename_completer;
3519 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
3524 add_com ("echo", class_support, echo_command,
3525 "Print a constant string. Give string as argument.\n\
3526 C escape sequences may be used in the argument.\n\
3527 No newline is added at the end of the argument;\n\
3528 use \"\\n\" if you want a newline to be printed.\n\
3529 Since leading and trailing whitespace are ignored in command arguments,\n\
3530 if you want to print some you must use \"\\\" before leading whitespace\n\
3531 to be printed or after trailing whitespace.");
3532 add_com ("document", class_support, document_command,
3533 "Document a user-defined command.\n\
3534 Give command name as argument. Give documentation on following lines.\n\
3535 End with a line of just \"end\".");
3536 add_com ("define", class_support, define_command,
3537 "Define a new command name. Command name is argument.\n\
3538 Definition appears on following lines, one command per line.\n\
3539 End with a line of just \"end\".\n\
3540 Use the \"document\" command to give documentation for the new command.\n\
3541 Commands defined in this way may have up to ten arguments.");
3544 c = add_cmd ("source", class_support, source_command,
3545 "Read commands from a file named FILE.\n\
3546 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
3547 when gdb is started.", &cmdlist);
3549 /* Punt file name, we can't help it easily. */
3550 c = add_cmd ("source", class_support, source_command,
3551 "Read commands from a file named FILE.\n\
3552 Note that the file \".gdbinit\" is read automatically in this way\n\
3553 when gdb is started.", &cmdlist);
3555 c->completer = filename_completer;
3557 add_com ("quit", class_support, quit_command, "Exit gdb.");
3558 add_com ("help", class_support, help_command, "Print list of commands.");
3559 add_com_alias ("q", "quit", class_support, 1);
3560 add_com_alias ("h", "help", class_support, 1);
3562 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
3563 Primarily used inside of user-defined commands that should not be repeated when\n\
3566 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
3569 add_show_from_set (c, &showlist);
3570 c->function.sfunc = set_verbose;
3571 set_verbose (NULL, 0, c);
3574 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
3575 "Set editing of command lines as they are typed.\n\
3576 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3577 Without an argument, command line editing is enabled. To edit, use\n\
3578 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
3581 add_prefix_cmd ("history", class_support, set_history,
3582 "Generic command for setting command history parameters.",
3583 &sethistlist, "set history ", 0, &setlist);
3584 add_prefix_cmd ("history", class_support, show_history,
3585 "Generic command for showing command history parameters.",
3586 &showhistlist, "show history ", 0, &showlist);
3589 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
3590 "Set history expansion on command input.\n\
3591 Without an argument, history expansion is enabled.", &sethistlist),
3595 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
3596 "Set saving of the history record on exit.\n\
3597 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
3598 Without an argument, saving is enabled.", &sethistlist),
3601 c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
3602 "Set the size of the command history, \n\
3603 ie. the number of previous commands to keep a record of.", &sethistlist);
3604 add_show_from_set (c, &showhistlist);
3605 c->function.sfunc = set_history_size_command;
3608 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
3609 "Set the filename in which to record the command history\n\
3610 (the list of previous commands of which a record is kept).", &sethistlist),
3614 (add_set_cmd ("confirm", class_support, var_boolean,
3616 "Set whether to confirm potentially dangerous operations.",
3620 add_prefix_cmd ("info", class_info, info_command,
3621 "Generic command for showing things about the program being debugged.",
3622 &infolist, "info ", 0, &cmdlist);
3623 add_com_alias ("i", "info", class_info, 1);
3625 add_com ("complete", class_obscure, complete_command,
3626 "List the completions for the rest of the line as a command.");
3628 add_prefix_cmd ("show", class_info, show_command,
3629 "Generic command for showing things about the debugger.",
3630 &showlist, "show ", 0, &cmdlist);
3631 /* Another way to get at the same thing. */
3632 add_info ("set", show_command, "Show all GDB settings.");
3634 add_cmd ("commands", no_class, show_commands,
3635 "Show the history of commands you typed.\n\
3636 You can supply a command number to start with, or a `+' to start after\n\
3637 the previous command number shown.",
3640 add_cmd ("version", no_class, show_version,
3641 "Show what version of GDB this is.", &showlist);
3643 add_com ("while", class_support, while_command,
3644 "Execute nested commands WHILE the conditional expression is non zero.\n\
3645 The conditional expression must follow the word `while' and must in turn be\n\
3646 followed by a new line. The nested commands must be entered one per line,\n\
3647 and should be terminated by the word `end'.");
3649 add_com ("if", class_support, if_command,
3650 "Execute nested commands once IF the conditional expression is non zero.\n\
3651 The conditional expression must follow the word `if' and must in turn be\n\
3652 followed by a new line. The nested commands must be entered one per line,\n\
3653 and should be terminated by the word 'else' or `end'. If an else clause\n\
3654 is used, the same rules apply to its nested commands as to the first ones.");
3656 /* If target is open when baud changes, it doesn't take effect until the
3657 next open (I think, not sure). */
3658 add_show_from_set (add_set_cmd ("remotebaud", no_class,
3659 var_zinteger, (char *)&baud_rate,
3660 "Set baud rate for remote serial I/O.\n\
3661 This value is used to set the speed of the serial port when debugging\n\
3662 using remote targets.", &setlist),
3666 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug,
3667 "Set debugging of remote protocol.\n\
3668 When enabled, each packet sent or received with the remote target\n\
3669 is displayed.", &setlist),
3673 add_set_cmd ("remotetimeout", no_class, var_integer, (char *)&remote_timeout,
3674 "Set timeout limit to wait for target to respond.\n\
3675 This value is used to set the time limit for gdb to wait for a response\n\
3676 from he target.", &setlist),
3679 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
3680 (char *)&annotation_level, "Set annotation_level.\n\
3681 0 == normal; 1 == fullname (for use when running under emacs)\n\
3682 2 == output annotated suitably for use by programs that control GDB.",
3684 c = add_show_from_set (c, &showlist);