1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "call-cmds.h"
28 #include "breakpoint.h"
30 #include "expression.h"
33 #include "terminal.h" /* For job_control. */
36 #include "event-loop.h"
38 /* readline include files */
39 #include <readline/readline.h>
40 #include <readline/history.h>
42 /* readline defines this. */
45 #include <sys/types.h>
50 #include "gdb_string.h"
54 extern void initialize_utils PARAMS ((void));
56 /* Prototypes for local functions */
58 static void dont_repeat_command PARAMS ((char *, int));
60 static void source_cleanup_lines PARAMS ((PTR));
62 static void user_defined_command PARAMS ((char *, int));
64 static void init_signals PARAMS ((void));
67 static void stop_sig PARAMS ((int));
70 static char * line_completion_function PARAMS ((char *, int, char *, int));
72 static char * readline_line_completion_function PARAMS ((char *, int));
74 /* NOTE 4/29/99: this function will be static again, after we make the
75 event loop be the default command loop for gdb, and we merge
76 event-top.c into this file, top.c */
77 /* static */ void command_loop_marker PARAMS ((int));
79 extern void set_async_editing_command PARAMS ((char *, int, struct cmd_list_element *));
81 extern void set_async_annotation_level PARAMS ((char *, int, struct cmd_list_element *));
83 extern void set_async_prompt PARAMS ((char *, int, struct cmd_list_element *));
85 static void while_command PARAMS ((char *, int));
87 static void if_command PARAMS ((char *, int));
89 static struct command_line *
90 build_command_line PARAMS ((enum command_control_type, char *));
92 static struct command_line *
93 get_command_line PARAMS ((enum command_control_type, char *));
95 static void realloc_body_list PARAMS ((struct command_line *, int));
97 static enum misc_command_type read_next_line PARAMS ((struct command_line **));
99 static enum command_control_type
100 recurse_read_control_structure PARAMS ((struct command_line *));
102 static struct cleanup * setup_user_args PARAMS ((char *));
104 static char * locate_arg PARAMS ((char *));
106 static char * insert_args PARAMS ((char *));
108 static void arg_cleanup PARAMS ((void));
110 static void init_main PARAMS ((void));
112 static void init_cmd_lists PARAMS ((void));
114 static void float_handler PARAMS ((int));
116 static void init_signals PARAMS ((void));
118 static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
120 static void show_history PARAMS ((char *, int));
122 static void set_history PARAMS ((char *, int));
124 static void set_history_size_command PARAMS ((char *, int,
125 struct cmd_list_element *));
127 static void show_commands PARAMS ((char *, int));
129 static void echo_command PARAMS ((char *, int));
131 static void pwd_command PARAMS ((char *, int));
133 static void show_version PARAMS ((char *, int));
135 static void document_command PARAMS ((char *, int));
137 static void define_command PARAMS ((char *, int));
139 static void validate_comname PARAMS ((char *));
141 static void help_command PARAMS ((char *, int));
143 static void show_command PARAMS ((char *, int));
145 static void info_command PARAMS ((char *, int));
147 static void complete_command PARAMS ((char *, int));
149 static void do_nothing PARAMS ((int));
152 /* NOTE 4/29/99: This function will be static again, once we modify
153 gdb to use the event loop as the default command loop and we merge
154 event-top.c into this file, top.c */
155 /* static */ int quit_cover PARAMS ((PTR));
157 static void disconnect PARAMS ((int));
160 static void source_cleanup PARAMS ((FILE *));
162 /* If this definition isn't overridden by the header files, assume
163 that isatty and fileno exist on this system. */
165 #define ISATTY(FP) (isatty (fileno (FP)))
168 /* Initialization file name for gdb. This is overridden in some configs. */
170 #ifndef GDBINIT_FILENAME
171 #define GDBINIT_FILENAME ".gdbinit"
173 char gdbinit[] = GDBINIT_FILENAME;
175 int inhibit_gdbinit = 0;
177 /* If nonzero, and GDB has been configured to be able to use windows,
178 attempt to open them upon startup. */
182 /* Version number of GDB, as a string. */
184 extern char *version;
186 /* Canonical host name as a string. */
188 extern char *host_name;
190 /* Canonical target name as a string. */
192 extern char *target_name;
194 extern char lang_frame_mismatch_warn[]; /* language.c */
196 /* Flag for whether we want all the "from_tty" gubbish printed. */
198 int caution = 1; /* Default is yes, sigh. */
200 /* Define all cmd_list_elements. */
202 /* Chain containing all defined commands. */
204 struct cmd_list_element *cmdlist;
206 /* Chain containing all defined info subcommands. */
208 struct cmd_list_element *infolist;
210 /* Chain containing all defined enable subcommands. */
212 struct cmd_list_element *enablelist;
214 /* Chain containing all defined disable subcommands. */
216 struct cmd_list_element *disablelist;
218 /* Chain containing all defined toggle subcommands. */
220 struct cmd_list_element *togglelist;
222 /* Chain containing all defined stop subcommands. */
224 struct cmd_list_element *stoplist;
226 /* Chain containing all defined delete subcommands. */
228 struct cmd_list_element *deletelist;
230 /* Chain containing all defined "enable breakpoint" subcommands. */
232 struct cmd_list_element *enablebreaklist;
234 /* Chain containing all defined set subcommands */
236 struct cmd_list_element *setlist;
238 /* Chain containing all defined unset subcommands */
240 struct cmd_list_element *unsetlist;
242 /* Chain containing all defined show subcommands. */
244 struct cmd_list_element *showlist;
246 /* Chain containing all defined \"set history\". */
248 struct cmd_list_element *sethistlist;
250 /* Chain containing all defined \"show history\". */
252 struct cmd_list_element *showhistlist;
254 /* Chain containing all defined \"unset history\". */
256 struct cmd_list_element *unsethistlist;
258 /* Chain containing all defined maintenance subcommands. */
260 struct cmd_list_element *maintenancelist;
262 /* Chain containing all defined "maintenance info" subcommands. */
264 struct cmd_list_element *maintenanceinfolist;
266 /* Chain containing all defined "maintenance print" subcommands. */
268 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 /* This hook is called from within gdb's many mini-event loops which could
378 steal control from a real user interface's event loop. It returns
379 non-zero if the user is requesting a detach, zero otherwise. */
381 int (*ui_loop_hook) PARAMS ((int));
383 /* Called instead of command_loop at top level. Can be invoked via
384 return_to_top_level. */
386 void (*command_loop_hook) PARAMS ((void));
389 /* Called instead of fputs for all output. */
391 void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer, GDB_FILE *stream));
393 /* Called when the target says something to the host, which may
394 want to appear in a different window. */
396 void (*target_output_hook) PARAMS ((char *));
398 /* Called from print_frame_info to list the line we stopped in. */
400 void (*print_frame_info_listing_hook) PARAMS ((struct symtab *s, int line,
401 int stopline, int noerror));
402 /* Replaces most of query. */
404 int (*query_hook) PARAMS ((const char *, va_list));
406 /* Replaces most of warning. */
408 void (*warning_hook) PARAMS ((const char *, va_list));
410 /* Called from gdb_flush to flush output. */
412 void (*flush_hook) PARAMS ((GDB_FILE *stream));
414 /* These three functions support getting lines of text from the user. They
415 are used in sequence. First readline_begin_hook is called with a text
416 string that might be (for example) a message for the user to type in a
417 sequence of commands to be executed at a breakpoint. If this function
418 calls back to a GUI, it might take this opportunity to pop up a text
419 interaction window with this message. Next, readline_hook is called
420 with a prompt that is emitted prior to collecting the user input.
421 It can be called multiple times. Finally, readline_end_hook is called
422 to notify the GUI that we are done with the interaction window and it
425 void (*readline_begin_hook) PARAMS ((char *, ...));
426 char * (*readline_hook) PARAMS ((char *));
427 void (*readline_end_hook) PARAMS ((void));
429 /* Called as appropriate to notify the interface of the specified breakpoint
432 void (*create_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
433 void (*delete_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
434 void (*modify_breakpoint_hook) PARAMS ((struct breakpoint *bpt));
436 /* Called during long calculations to allow GUI to repair window damage, and to
437 check for stop buttons, etc... */
439 void (*interactive_hook) PARAMS ((void));
441 /* Called when the registers have changed, as a hint to a GUI
442 to minimize window update. */
444 void (*registers_changed_hook) PARAMS ((void));
446 /* Tell the GUI someone changed the register REGNO. -1 means
447 that the caller does not know which register changed or
448 that several registers have changed (see value_assign).*/
449 void (*register_changed_hook) PARAMS ((int regno));
451 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
452 void (*memory_changed_hook) PARAMS ((CORE_ADDR addr, int len));
454 /* Called when going to wait for the target. Usually allows the GUI to run
455 while waiting for target events. */
457 int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus *status));
459 /* Used by UI as a wrapper around command execution. May do various things
460 like enabling/disabling buttons, etc... */
462 void (*call_command_hook) PARAMS ((struct cmd_list_element *c, char *cmd,
465 /* Called when the current thread changes. Argument is thread id. */
467 void (*context_hook) PARAMS ((int id));
469 /* Takes control from error (). Typically used to prevent longjmps out of the
470 middle of the GUI. Usually used in conjunction with a catch routine. */
472 NORETURN void (*error_hook) PARAMS ((void)) ATTR_NORETURN;
475 /* Where to go for return_to_top_level (RETURN_ERROR). */
476 SIGJMP_BUF error_return;
477 /* Where to go for return_to_top_level (RETURN_QUIT). */
478 SIGJMP_BUF quit_return;
480 /* Return for reason REASON. This generally gets back to the command
481 loop, but can be caught via catch_errors. */
484 return_to_top_level (reason)
485 enum return_reason reason;
490 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
491 I can think of a reason why that is vital, though). */
492 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
494 disable_current_display ();
495 do_cleanups (ALL_CLEANUPS);
497 if (annotation_level > 1)
508 (NORETURN void) SIGLONGJMP
509 (reason == RETURN_ERROR ? error_return : quit_return, 1);
512 /* Call FUNC with arg ARGS, catching any errors. If there is no
513 error, return the value returned by FUNC. If there is an error,
514 print ERRSTRING, print the specific error message, then return
517 Must not be called with immediate_quit in effect (bad things might
518 happen, say we got a signal in the middle of a memcpy to quit_return).
519 This is an OK restriction; with very few exceptions immediate_quit can
520 be replaced by judicious use of QUIT.
522 MASK specifies what to catch; it is normally set to
523 RETURN_MASK_ALL, if for no other reason than that the code which
524 calls catch_errors might not be set up to deal with a quit which
525 isn't caught. But if the code can deal with it, it generally
526 should be RETURN_MASK_ERROR, unless for some reason it is more
527 useful to abort only the portion of the operation inside the
528 catch_errors. Note that quit should return to the command line
529 fairly quickly, even if some further processing is being done. */
532 catch_errors (func, args, errstring, mask)
533 catch_errors_ftype *func;
538 SIGJMP_BUF saved_error;
539 SIGJMP_BUF saved_quit;
542 struct cleanup *saved_cleanup_chain;
543 char *saved_error_pre_print;
544 char *saved_quit_pre_print;
546 saved_cleanup_chain = save_cleanups ();
547 saved_error_pre_print = error_pre_print;
548 saved_quit_pre_print = quit_pre_print;
550 if (mask & RETURN_MASK_ERROR)
552 memcpy ((char *)saved_error, (char *)error_return, sizeof (SIGJMP_BUF));
553 error_pre_print = errstring;
555 if (mask & RETURN_MASK_QUIT)
557 memcpy (saved_quit, quit_return, sizeof (SIGJMP_BUF));
558 quit_pre_print = errstring;
561 if (SIGSETJMP (tmp_jmp) == 0)
563 if (mask & RETURN_MASK_ERROR)
564 memcpy (error_return, tmp_jmp, sizeof (SIGJMP_BUF));
565 if (mask & RETURN_MASK_QUIT)
566 memcpy (quit_return, tmp_jmp, sizeof (SIGJMP_BUF));
567 val = (*func) (args);
572 restore_cleanups (saved_cleanup_chain);
574 if (mask & RETURN_MASK_ERROR)
576 memcpy (error_return, saved_error, sizeof (SIGJMP_BUF));
577 error_pre_print = saved_error_pre_print;
579 if (mask & RETURN_MASK_QUIT)
581 memcpy (quit_return, saved_quit, sizeof (SIGJMP_BUF));
582 quit_pre_print = saved_quit_pre_print;
587 /* Handler for SIGHUP. */
594 catch_errors (quit_cover, NULL,
595 "Could not kill the program being debugged", RETURN_MASK_ALL);
596 signal (SIGHUP, SIG_DFL);
597 kill (getpid (), SIGHUP);
600 /* Just a little helper function for disconnect(). */
602 /* NOTE 4/29/99: This function will be static again, once we modify
603 gdb to use the event loop as the default command loop and we merge
604 event-top.c into this file, top.c */
609 caution = 0; /* Throw caution to the wind -- we're exiting.
610 This prevents asking the user dumb questions. */
611 quit_command((char *)0, 0);
614 #endif /* defined SIGHUP */
616 /* Line number we are currently in in a file which is being sourced. */
617 /* NOTE 4/29/99: This variable will be static again, once we modify
618 gdb to use the event loop as the default command loop and we merge
619 event-top.c into this file, top.c */
620 /* static */ int source_line_number;
622 /* Name of the file we are sourcing. */
623 /* NOTE 4/29/99: This variable will be static again, once we modify
624 gdb to use the event loop as the default command loop and we merge
625 event-top.c into this file, top.c */
626 /* static */ char *source_file_name;
628 /* Buffer containing the error_pre_print used by the source stuff.
630 /* NOTE 4/29/99: This variable will be static again, once we modify
631 gdb to use the event loop as the default command loop and we merge
632 event-top.c into this file, top.c */
633 /* static */ char *source_error;
634 static int source_error_allocated;
636 /* Something to glom on to the start of error_pre_print if source_file_name
638 /* NOTE 4/29/99: This variable will be static again, once we modify
639 gdb to use the event loop as the default command loop and we merge
640 event-top.c into this file, top.c */
641 /* static */ char *source_pre_error;
643 /* Clean up on error during a "source" command (or execution of a
644 user-defined command). */
647 source_cleanup (stream)
650 /* Restore the previous input stream. */
654 /* Read commands from STREAM. */
656 read_command_file (stream)
659 struct cleanup *cleanups;
661 cleanups = make_cleanup ((make_cleanup_func) source_cleanup, instream);
664 do_cleanups (cleanups);
667 extern void init_proc PARAMS ((void));
669 void (*pre_init_ui_hook) PARAMS ((void));
675 if (pre_init_ui_hook)
678 /* Run the init function of each source file */
680 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
681 current_directory = gdb_dirbuf;
683 init_cmd_lists (); /* This needs to be done first */
684 initialize_targets (); /* Setup target_terminal macros for utils.c */
685 initialize_utils (); /* Make errors and warnings possible */
686 initialize_all_files ();
687 init_main (); /* But that omits this file! Do it now */
689 /* The signal handling mechanism is different depending whether or
690 not the async version is run. NOTE: in the future we plan to make
691 the event loop be the default engine of gdb, and this difference
694 async_init_signals ();
700 /* We need a default language for parsing expressions, so simple things like
701 "set width 0" won't fail if no language is explicitly set in a config file
702 or implicitly set by reading an executable during startup. */
703 set_language (language_c);
704 expected_language = current_language; /* don't warn about the change. */
707 init_ui_hook (argv0);
710 /* Allocate, initialize a new command line structure for one of the
711 control commands (if/while). */
713 static struct command_line *
714 build_command_line (type, args)
715 enum command_control_type type;
718 struct command_line *cmd;
721 error ("if/while commands require arguments.\n");
723 cmd = (struct command_line *)xmalloc (sizeof (struct command_line));
725 cmd->control_type = type;
729 = (struct command_line **)xmalloc (sizeof (struct command_line *)
731 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
732 cmd->line = savestring (args, strlen (args));
736 /* Build and return a new command structure for the control commands
737 such as "if" and "while". */
739 static struct command_line *
740 get_command_line (type, arg)
741 enum command_control_type type;
744 struct command_line *cmd;
745 struct cleanup *old_chain = NULL;
747 /* Allocate and build a new command line structure. */
748 cmd = build_command_line (type, arg);
750 old_chain = make_cleanup ((make_cleanup_func) free_command_lines, &cmd);
752 /* Read in the body of this command. */
753 if (recurse_read_control_structure (cmd) == invalid_control)
755 warning ("error reading in control structure\n");
756 do_cleanups (old_chain);
760 discard_cleanups (old_chain);
764 /* Recursively print a command (including full control structures). */
766 print_command_line (cmd, depth, stream)
767 struct command_line *cmd;
775 for (i = 0; i < depth; i++)
776 fputs_filtered (" ", stream);
779 /* A simple command, print it and return. */
780 if (cmd->control_type == simple_control)
782 fputs_filtered (cmd->line, stream);
783 fputs_filtered ("\n", stream);
787 /* loop_continue to jump to the start of a while loop, print it
789 if (cmd->control_type == continue_control)
791 fputs_filtered ("loop_continue\n", stream);
795 /* loop_break to break out of a while loop, print it and return. */
796 if (cmd->control_type == break_control)
798 fputs_filtered ("loop_break\n", stream);
802 /* A while command. Recursively print its subcommands before returning. */
803 if (cmd->control_type == while_control)
805 struct command_line *list;
806 fputs_filtered ("while ", stream);
807 fputs_filtered (cmd->line, stream);
808 fputs_filtered ("\n", stream);
809 list = *cmd->body_list;
812 print_command_line (list, depth + 1, stream);
817 /* An if command. Recursively print both arms before returning. */
818 if (cmd->control_type == if_control)
820 fputs_filtered ("if ", stream);
821 fputs_filtered (cmd->line, stream);
822 fputs_filtered ("\n", stream);
824 print_command_line (cmd->body_list[0], depth + 1, stream);
826 /* Show the false arm if it exists. */
827 if (cmd->body_count == 2)
831 for (i = 0; i < depth; i++)
832 fputs_filtered (" ", stream);
834 fputs_filtered ("else\n", stream);
835 print_command_line (cmd->body_list[1], depth + 1, stream);
839 for (i = 0; i < depth; i++)
840 fputs_filtered (" ", stream);
842 fputs_filtered ("end\n", stream);
846 /* Execute the command in CMD. */
848 enum command_control_type
849 execute_control_command (cmd)
850 struct command_line *cmd;
852 struct expression *expr;
853 struct command_line *current;
854 struct cleanup *old_chain = 0;
858 enum command_control_type ret;
861 switch (cmd->control_type)
864 /* A simple command, execute it and return. */
865 new_line = insert_args (cmd->line);
867 return invalid_control;
868 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
870 execute_command (new_line, 0);
871 ret = cmd->control_type;
874 case continue_control:
876 /* Return for "continue", and "break" so we can either
877 continue the loop at the top, or break out. */
878 ret = cmd->control_type;
883 /* Parse the loop control expression for the while statement. */
884 new_line = insert_args (cmd->line);
886 return invalid_control;
887 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
889 expr = parse_expression (new_line);
890 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
892 ret = simple_control;
895 /* Keep iterating so long as the expression is true. */
902 /* Evaluate the expression. */
903 val_mark = value_mark ();
904 val = evaluate_expression (expr);
905 cond_result = value_true (val);
906 value_free_to_mark (val_mark);
908 /* If the value is false, then break out of the loop. */
912 /* Execute the body of the while statement. */
913 current = *cmd->body_list;
916 ret = execute_control_command (current);
918 /* If we got an error, or a "break" command, then stop
920 if (ret == invalid_control || ret == break_control)
926 /* If we got a "continue" command, then restart the loop
928 if (ret == continue_control)
931 /* Get the next statement. */
932 current = current->next;
936 /* Reset RET so that we don't recurse the break all the way down. */
937 if (ret == break_control)
938 ret = simple_control;
945 new_line = insert_args (cmd->line);
947 return invalid_control;
948 old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
950 /* Parse the conditional for the if statement. */
951 expr = parse_expression (new_line);
952 make_cleanup ((make_cleanup_func) free_current_contents, &expr);
955 ret = simple_control;
957 /* Evaluate the conditional. */
958 val_mark = value_mark ();
959 val = evaluate_expression (expr);
961 /* Choose which arm to take commands from based on the value of the
962 conditional expression. */
963 if (value_true (val))
964 current = *cmd->body_list;
965 else if (cmd->body_count == 2)
966 current = *(cmd->body_list + 1);
967 value_free_to_mark (val_mark);
969 /* Execute commands in the given arm. */
972 ret = execute_control_command (current);
974 /* If we got an error, get out. */
975 if (ret != simple_control)
978 /* Get the next statement in the body. */
979 current = current->next;
986 warning ("Invalid control type in command structure.");
987 return invalid_control;
991 do_cleanups (old_chain);
996 /* "while" command support. Executes a body of statements while the
997 loop condition is nonzero. */
1000 while_command (arg, from_tty)
1004 struct command_line *command = NULL;
1007 command = get_command_line (while_control, arg);
1009 if (command == NULL)
1012 execute_control_command (command);
1013 free_command_lines (&command);
1016 /* "if" command support. Execute either the true or false arm depending
1017 on the value of the if conditional. */
1020 if_command (arg, from_tty)
1024 struct command_line *command = NULL;
1027 command = get_command_line (if_control, arg);
1029 if (command == NULL)
1032 execute_control_command (command);
1033 free_command_lines (&command);
1040 struct user_args *oargs = user_args;
1042 fatal ("Internal error, arg_cleanup called with no user args.\n");
1044 user_args = user_args->next;
1048 /* Bind the incomming arguments for a user defined command to
1049 $arg0, $arg1 ... $argMAXUSERARGS. */
1051 static struct cleanup *
1055 struct user_args *args;
1056 struct cleanup *old_chain;
1057 unsigned int arg_count = 0;
1059 args = (struct user_args *)xmalloc (sizeof (struct user_args));
1060 memset (args, 0, sizeof (struct user_args));
1062 args->next = user_args;
1065 old_chain = make_cleanup ((make_cleanup_func) arg_cleanup, 0);
1077 if (arg_count >= MAXUSERARGS)
1079 error ("user defined function may only have %d arguments.\n",
1084 /* Strip whitespace. */
1085 while (*p == ' ' || *p == '\t')
1088 /* P now points to an argument. */
1090 user_args->a[arg_count].arg = p;
1092 /* Get to the end of this argument. */
1095 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
1101 else if (*p == '\\')
1124 user_args->a[arg_count].len = p - start_arg;
1131 /* Given character string P, return a point to the first argument ($arg),
1132 or NULL if P contains no arguments. */
1138 while ((p = strchr (p, '$')))
1140 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
1147 /* Insert the user defined arguments stored in user_arg into the $arg
1148 arguments found in line, with the updated copy being placed into nline. */
1154 char *p, *save_line, *new_line;
1157 /* First we need to know how much memory to allocate for the new line. */
1160 while ((p = locate_arg (line)))
1165 if (i >= user_args->count)
1167 error ("Missing argument %d in user function.\n", i);
1170 len += user_args->a[i].len;
1174 /* Don't forget the tail. */
1175 len += strlen (line);
1177 /* Allocate space for the new line and fill it in. */
1178 new_line = (char *)xmalloc (len + 1);
1179 if (new_line == NULL)
1182 /* Restore pointer to beginning of old line. */
1185 /* Save pointer to beginning of new line. */
1186 save_line = new_line;
1188 while ((p = locate_arg (line)))
1192 memcpy (new_line, line, p - line);
1193 new_line += p - line;
1196 len = user_args->a[i].len;
1199 memcpy (new_line, user_args->a[i].arg, len);
1204 /* Don't forget the tail. */
1205 strcpy (new_line, line);
1207 /* Return a pointer to the beginning of the new line. */
1212 execute_user_command (c, args)
1213 struct cmd_list_element *c;
1216 register struct command_line *cmdlines;
1217 struct cleanup *old_chain;
1218 enum command_control_type ret;
1220 old_chain = setup_user_args (args);
1222 cmdlines = c->user_commands;
1227 /* Set the instream to 0, indicating execution of a
1228 user-defined function. */
1229 old_chain = make_cleanup ((make_cleanup_func) source_cleanup, instream);
1230 instream = (FILE *) 0;
1233 ret = execute_control_command (cmdlines);
1234 if (ret != simple_control && ret != break_control)
1236 warning ("Error in control structure.\n");
1239 cmdlines = cmdlines->next;
1241 do_cleanups (old_chain);
1244 /* Execute the line P as a command.
1245 Pass FROM_TTY as second argument to the defining function. */
1248 execute_command (p, from_tty)
1252 register struct cmd_list_element *c;
1253 register enum language flang;
1254 static int warned = 0;
1255 /* FIXME: These should really be in an appropriate header file */
1256 extern void serial_log_command PARAMS ((const char *));
1260 /* Force cleanup of any alloca areas if using C alloca instead of
1261 a builtin alloca. */
1264 /* This can happen when command_line_input hits end of file. */
1268 serial_log_command (p);
1270 while (*p == ' ' || *p == '\t') p++;
1275 c = lookup_cmd (&p, cmdlist, "", 0, 1);
1276 /* Pass null arg rather than an empty one. */
1279 /* Clear off trailing whitespace, except for set and complete command. */
1280 if (arg && c->type != set_cmd && c->function.cfunc != complete_command)
1282 p = arg + strlen (arg) - 1;
1283 while (p >= arg && (*p == ' ' || *p == '\t'))
1288 /* If this command has been hooked, run the hook first. */
1290 execute_user_command (c->hook, (char *)0);
1292 if (c->class == class_user)
1293 execute_user_command (c, arg);
1294 else if (c->type == set_cmd || c->type == show_cmd)
1295 do_setshow_command (arg, from_tty & caution, c);
1296 else if (c->function.cfunc == NO_FUNCTION)
1297 error ("That is not a command, just a help topic.");
1298 else if (call_command_hook)
1299 call_command_hook (c, arg, from_tty & caution);
1301 (*c->function.cfunc) (arg, from_tty & caution);
1304 /* Tell the user if the language has changed (except first time). */
1305 if (current_language != expected_language)
1307 if (language_mode == language_mode_auto) {
1308 language_info (1); /* Print what changed. */
1313 /* Warn the user if the working language does not match the
1314 language of the current frame. Only warn the user if we are
1315 actually running the program, i.e. there is a stack. */
1316 /* FIXME: This should be cacheing the frame and only running when
1317 the frame changes. */
1319 if (target_has_stack)
1321 flang = get_frame_language ();
1323 && flang != language_unknown
1324 && flang != current_language->la_language)
1326 printf_filtered ("%s\n", lang_frame_mismatch_warn);
1333 /* NOTE 4/29/99: This function will be static again, once we modify
1334 gdb to use the event loop as the default command loop and we merge
1335 event-top.c into this file, top.c */
1337 command_loop_marker (foo)
1342 /* Read commands from `instream' and execute them
1343 until end of file or error reading instream. */
1348 struct cleanup *old_chain;
1350 int stdin_is_tty = ISATTY (stdin);
1351 long time_at_cmd_start;
1353 long space_at_cmd_start = 0;
1355 extern int display_time;
1356 extern int display_space;
1358 while (instream && !feof (instream))
1361 extern int insert_mode;
1363 if (window_hook && instream == stdin)
1364 (*window_hook) (instream, prompt);
1367 if (instream == stdin && stdin_is_tty)
1368 reinitialize_more_filter ();
1369 old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0);
1372 /* A bit of paranoia: I want to make sure the "insert_mode" global
1373 * is clear except when it is being used for command-line editing
1374 * (see tuiIO.c, utils.c); otherwise normal output will
1375 * get messed up in the TUI. So clear it before/after
1376 * the command-line-input call. - RT
1380 /* Get a command-line. This calls the readline package. */
1381 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
1382 instream == stdin, "prompt");
1389 time_at_cmd_start = get_run_time ();
1394 extern char **environ;
1395 char *lim = (char *) sbrk (0);
1397 space_at_cmd_start = (long) (lim - (char *) &environ);
1401 execute_command (command, instream == stdin);
1402 /* Do any commands attached to breakpoint we stopped at. */
1403 bpstat_do_actions (&stop_bpstat);
1404 do_cleanups (old_chain);
1408 long cmd_time = get_run_time () - time_at_cmd_start;
1410 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1411 cmd_time / 1000000, cmd_time % 1000000);
1417 extern char **environ;
1418 char *lim = (char *) sbrk (0);
1419 long space_now = lim - (char *) &environ;
1420 long space_diff = space_now - space_at_cmd_start;
1422 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1424 (space_diff >= 0 ? '+' : '-'),
1431 /* Commands call this if they do not want to be repeated by null lines. */
1439 /* If we aren't reading from standard input, we are saving the last
1440 thing read from stdin in line and don't want to delete it. Null lines
1441 won't repeat here in any case. */
1442 if (instream == stdin)
1446 /* Read a line from the stream "instream" without command line editing.
1448 It prints PRROMPT once at the start.
1449 Action is compatible with "readline", e.g. space for the result is
1450 malloc'd and should be freed by the caller.
1452 A NULL return means end of file. */
1454 gdb_readline (prrompt)
1459 int input_index = 0;
1460 int result_size = 80;
1464 /* Don't use a _filtered function here. It causes the assumed
1465 character position to be off, since the newline we read from
1466 the user is not accounted for. */
1467 fputs_unfiltered (prrompt, gdb_stdout);
1469 /* Move to a new line so the entered line doesn't have a prompt
1470 on the front of it. */
1471 fputs_unfiltered ("\n", gdb_stdout);
1473 gdb_flush (gdb_stdout);
1476 result = (char *) xmalloc (result_size);
1480 /* Read from stdin if we are executing a user defined command.
1481 This is the right thing for prompt_for_continue, at least. */
1482 c = fgetc (instream ? instream : stdin);
1486 if (input_index > 0)
1487 /* The last line does not end with a newline. Return it, and
1488 if we are called again fgetc will still return EOF and
1489 we'll return NULL then. */
1496 #ifndef CRLF_SOURCE_FILES
1500 if (input_index > 0 && result[input_index - 1] == '\r')
1506 result[input_index++] = c;
1507 while (input_index >= result_size)
1510 result = (char *) xrealloc (result, result_size);
1514 result[input_index++] = '\0';
1518 /* Variables which control command line editing and history
1519 substitution. These variables are given default values at the end
1521 static int command_editing_p;
1522 /* NOTE 4/29/99: This variable will be static again, once we modify
1523 gdb to use the event loop as the default command loop and we merge
1524 event-top.c into this file, top.c */
1525 /* static */ int history_expansion_p;
1526 static int write_history_p;
1527 static int history_size;
1528 static char *history_filename;
1530 /* readline uses the word breaks for two things:
1531 (1) In figuring out where to point the TEXT parameter to the
1532 rl_completion_entry_function. Since we don't use TEXT for much,
1533 it doesn't matter a lot what the word breaks are for this purpose, but
1534 it does affect how much stuff M-? lists.
1535 (2) If one of the matches contains a word break character, readline
1536 will quote it. That's why we switch between
1537 gdb_completer_word_break_characters and
1538 gdb_completer_command_word_break_characters. I'm not sure when
1539 we need this behavior (perhaps for funky characters in C++ symbols?). */
1541 /* Variables which are necessary for fancy command line editing. */
1542 char *gdb_completer_word_break_characters =
1543 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1545 /* When completing on command names, we remove '-' from the list of
1546 word break characters, since we use it in command names. If the
1547 readline library sees one in any of the current completion strings,
1548 it thinks that the string needs to be quoted and automatically supplies
1550 char *gdb_completer_command_word_break_characters =
1551 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1553 /* Characters that can be used to quote completion strings. Note that we
1554 can't include '"' because the gdb C parser treats such quoted sequences
1556 char *gdb_completer_quote_characters =
1559 /* Functions that are used as part of the fancy command line editing. */
1561 /* This can be used for functions which don't want to complete on symbols
1562 but don't want to complete on anything else either. */
1565 noop_completer (text, prefix)
1572 /* Complete on filenames. */
1574 filename_completer (text, word)
1578 /* From readline. */
1579 extern char *filename_completion_function PARAMS ((char *, int));
1580 int subsequent_name;
1582 int return_val_used;
1583 int return_val_alloced;
1585 return_val_used = 0;
1586 /* Small for testing. */
1587 return_val_alloced = 1;
1588 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1590 subsequent_name = 0;
1594 p = filename_completion_function (text, subsequent_name);
1595 if (return_val_used >= return_val_alloced)
1597 return_val_alloced *= 2;
1599 (char **) xrealloc (return_val,
1600 return_val_alloced * sizeof (char *));
1604 return_val[return_val_used++] = p;
1607 /* Like emacs, don't complete on old versions. Especially useful
1608 in the "source" command. */
1609 if (p[strlen (p) - 1] == '~')
1615 /* Return exactly p. */
1616 return_val[return_val_used++] = p;
1617 else if (word > text)
1619 /* Return some portion of p. */
1620 q = xmalloc (strlen (p) + 5);
1621 strcpy (q, p + (word - text));
1622 return_val[return_val_used++] = q;
1627 /* Return some of TEXT plus p. */
1628 q = xmalloc (strlen (p) + (text - word) + 5);
1629 strncpy (q, word, text - word);
1630 q[text - word] = '\0';
1632 return_val[return_val_used++] = q;
1636 subsequent_name = 1;
1639 /* There is no way to do this just long enough to affect quote inserting
1640 without also affecting the next completion. This should be fixed in
1642 /* Insure that readline does the right thing
1643 with respect to inserting quotes. */
1644 rl_completer_word_break_characters = "";
1649 /* Here are some useful test cases for completion. FIXME: These should
1650 be put in the test suite. They should be tested with both M-? and TAB.
1652 "show output-" "radix"
1653 "show output" "-radix"
1654 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1655 "p " ambiguous (all symbols)
1656 "info t foo" no completions
1657 "info t " no completions
1658 "info t" ambiguous ("info target", "info terminal", etc.)
1659 "info ajksdlfk" no completions
1660 "info ajksdlfk " no completions
1662 "info " ambiguous (all info commands)
1663 "p \"a" no completions (string constant)
1664 "p 'a" ambiguous (all symbols starting with a)
1665 "p b-a" ambiguous (all symbols starting with a)
1666 "p b-" ambiguous (all symbols)
1667 "file Make" "file" (word break hard to screw up here)
1668 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1671 /* Generate completions one by one for the completer. Each time we are
1672 called return another potential completion to the caller.
1673 line_completion just completes on commands or passes the buck to the
1674 command's completer function, the stuff specific to symbol completion
1675 is in make_symbol_completion_list.
1677 TEXT is the caller's idea of the "word" we are looking at.
1679 MATCHES is the number of matches that have currently been collected from
1680 calling this completion function. When zero, then we need to initialize,
1681 otherwise the initialization has already taken place and we can just
1682 return the next potential completion string.
1684 LINE_BUFFER is available to be looked at; it contains the entire text
1685 of the line. POINT is the offset in that line of the cursor. You
1686 should pretend that the line ends at POINT.
1688 Returns NULL if there are no more completions, else a pointer to a string
1689 which is a possible completion, it is the caller's responsibility to
1693 line_completion_function (text, matches, line_buffer, point)
1699 static char **list = (char **)NULL; /* Cache of completions */
1700 static int index; /* Next cached completion */
1701 char *output = NULL;
1702 char *tmp_command, *p;
1703 /* Pointer within tmp_command which corresponds to text. */
1705 struct cmd_list_element *c, *result_list;
1709 /* The caller is beginning to accumulate a new set of completions, so
1710 we need to find all of them now, and cache them for returning one at
1711 a time on future calls. */
1715 /* Free the storage used by LIST, but not by the strings inside.
1716 This is because rl_complete_internal () frees the strings. */
1722 /* Choose the default set of word break characters to break completions.
1723 If we later find out that we are doing completions on command strings
1724 (as opposed to strings supplied by the individual command completer
1725 functions, which can be any string) then we will switch to the
1726 special word break set for command strings, which leaves out the
1727 '-' character used in some commands. */
1729 rl_completer_word_break_characters =
1730 gdb_completer_word_break_characters;
1732 /* Decide whether to complete on a list of gdb commands or on symbols. */
1733 tmp_command = (char *) alloca (point + 1);
1736 strncpy (tmp_command, line_buffer, point);
1737 tmp_command[point] = '\0';
1738 /* Since text always contains some number of characters leading up
1739 to point, we can find the equivalent position in tmp_command
1740 by subtracting that many characters from the end of tmp_command. */
1741 word = tmp_command + point - strlen (text);
1745 /* An empty line we want to consider ambiguous; that is, it
1746 could be any command. */
1747 c = (struct cmd_list_element *) -1;
1752 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1755 /* Move p up to the next interesting thing. */
1756 while (*p == ' ' || *p == '\t')
1763 /* It is an unrecognized command. So there are no
1764 possible completions. */
1767 else if (c == (struct cmd_list_element *) -1)
1771 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1772 doesn't advance over that thing itself. Do so now. */
1774 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1776 if (q != tmp_command + point)
1778 /* There is something beyond the ambiguous
1779 command, so there are no possible completions. For
1780 example, "info t " or "info t foo" does not complete
1781 to anything, because "info t" can be "info target" or
1787 /* We're trying to complete on the command which was ambiguous.
1788 This we can deal with. */
1791 list = complete_on_cmdlist (*result_list->prefixlist, p,
1796 list = complete_on_cmdlist (cmdlist, p, word);
1798 /* Insure that readline does the right thing with respect to
1799 inserting quotes. */
1800 rl_completer_word_break_characters =
1801 gdb_completer_command_word_break_characters;
1806 /* We've recognized a full command. */
1808 if (p == tmp_command + point)
1810 /* There is no non-whitespace in the line beyond the command. */
1812 if (p[-1] == ' ' || p[-1] == '\t')
1814 /* The command is followed by whitespace; we need to complete
1815 on whatever comes after command. */
1818 /* It is a prefix command; what comes after it is
1819 a subcommand (e.g. "info "). */
1820 list = complete_on_cmdlist (*c->prefixlist, p, word);
1822 /* Insure that readline does the right thing
1823 with respect to inserting quotes. */
1824 rl_completer_word_break_characters =
1825 gdb_completer_command_word_break_characters;
1829 list = complete_on_enum (c->enums, p, word);
1830 rl_completer_word_break_characters =
1831 gdb_completer_command_word_break_characters;
1835 /* It is a normal command; what comes after it is
1836 completed by the command's completer function. */
1837 list = (*c->completer) (p, word);
1842 /* The command is not followed by whitespace; we need to
1843 complete on the command itself. e.g. "p" which is a
1844 command itself but also can complete to "print", "ptype"
1848 /* Find the command we are completing on. */
1850 while (q > tmp_command)
1852 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1858 list = complete_on_cmdlist (result_list, q, word);
1860 /* Insure that readline does the right thing
1861 with respect to inserting quotes. */
1862 rl_completer_word_break_characters =
1863 gdb_completer_command_word_break_characters;
1868 /* There is non-whitespace beyond the command. */
1870 if (c->prefixlist && !c->allow_unknown)
1872 /* It is an unrecognized subcommand of a prefix command,
1873 e.g. "info adsfkdj". */
1878 list = complete_on_enum (c->enums, p, word);
1882 /* It is a normal command. */
1883 list = (*c->completer) (p, word);
1889 /* If we found a list of potential completions during initialization then
1890 dole them out one at a time. The vector of completions is NULL
1891 terminated, so after returning the last one, return NULL (and continue
1892 to do so) each time we are called after that, until a new list is
1897 output = list[index];
1905 /* Can't do this because readline hasn't yet checked the word breaks
1906 for figuring out whether to insert a quote. */
1908 /* Make sure the word break characters are set back to normal for the
1909 next time that readline tries to complete something. */
1910 rl_completer_word_break_characters =
1911 gdb_completer_word_break_characters;
1917 /* Line completion interface function for readline. */
1920 readline_line_completion_function (text, matches)
1924 return line_completion_function (text, matches, rl_line_buffer, rl_point);
1927 /* Skip over a possibly quoted word (as defined by the quote characters
1928 and word break characters the completer uses). Returns pointer to the
1929 location after the "word". */
1935 char quote_char = '\0';
1938 for (scan = str; *scan != '\0'; scan++)
1940 if (quote_char != '\0')
1942 /* Ignore everything until the matching close quote char */
1943 if (*scan == quote_char)
1945 /* Found matching close quote. */
1950 else if (strchr (gdb_completer_quote_characters, *scan))
1952 /* Found start of a quoted string. */
1955 else if (strchr (gdb_completer_word_break_characters, *scan))
1969 #if STOP_SIGNAL == SIGTSTP
1970 signal (SIGTSTP, SIG_DFL);
1972 kill (getpid (), SIGTSTP);
1973 signal (SIGTSTP, stop_sig);
1975 signal (STOP_SIGNAL, stop_sig);
1977 printf_unfiltered ("%s", prompt);
1978 gdb_flush (gdb_stdout);
1980 /* Forget about any previous command -- null line now will do nothing. */
1983 #endif /* STOP_SIGNAL */
1985 /* Initialize signal handlers. */
1990 /* Under System V the default disposition of a signal is reinstated after
1991 the signal is caught and delivered to an application process. On such
1992 systems one must restore the replacement signal handler if one wishes
1993 to continue handling the signal in one's program. On BSD systems this
1994 is not needed but it is harmless, and it simplifies the code to just do
1995 it unconditionally. */
1996 signal (signo, do_nothing);
2002 signal (SIGINT, request_quit);
2004 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2005 to the inferior and breakpoints will be ignored. */
2007 signal (SIGTRAP, SIG_DFL);
2010 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2011 passed to the inferior, which we don't want. It would be
2012 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2013 on BSD4.3 systems using vfork, that can affect the
2014 GDB process as well as the inferior (the signal handling tables
2015 might be in memory, shared between the two). Since we establish
2016 a handler for SIGQUIT, when we call exec it will set the signal
2017 to SIG_DFL for us. */
2018 signal (SIGQUIT, do_nothing);
2020 if (signal (SIGHUP, do_nothing) != SIG_IGN)
2021 signal (SIGHUP, disconnect);
2023 signal (SIGFPE, float_handler);
2025 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2026 signal (SIGWINCH, SIGWINCH_HANDLER);
2030 /* Read one line from the command input stream `instream'
2031 into the local static buffer `linebuffer' (whose current length
2033 The buffer is made bigger as necessary.
2034 Returns the address of the start of the line.
2036 NULL is returned for end of file.
2038 *If* the instream == stdin & stdin is a terminal, the line read
2039 is copied into the file line saver (global var char *line,
2040 length linesize) so that it can be duplicated.
2042 This routine either uses fancy command line editing or
2043 simple input as the user has requested. */
2046 command_line_input (prrompt, repeat, annotation_suffix)
2049 char *annotation_suffix;
2051 static char *linebuffer = 0;
2052 static unsigned linelength = 0;
2056 char *local_prompt = prrompt;
2060 /* The annotation suffix must be non-NULL. */
2061 if (annotation_suffix == NULL)
2062 annotation_suffix = "";
2064 if (annotation_level > 1 && instream == stdin)
2066 local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt))
2067 + strlen (annotation_suffix) + 40);
2068 if (prrompt == NULL)
2069 local_prompt[0] = '\0';
2071 strcpy (local_prompt, prrompt);
2072 strcat (local_prompt, "\n\032\032");
2073 strcat (local_prompt, annotation_suffix);
2074 strcat (local_prompt, "\n");
2077 if (linebuffer == 0)
2080 linebuffer = (char *) xmalloc (linelength);
2085 /* Control-C quits instantly if typed while in this loop
2086 since it should not wait until the user types a newline. */
2090 signal (STOP_SIGNAL, stop_sig);
2095 /* Make sure that all output has been output. Some machines may let
2096 you get away with leaving out some of the gdb_flush, but not all. */
2098 gdb_flush (gdb_stdout);
2099 gdb_flush (gdb_stderr);
2101 if (source_file_name != NULL)
2103 ++source_line_number;
2104 sprintf (source_error,
2105 "%s%s:%d: Error in sourced command file:\n",
2108 source_line_number);
2109 error_pre_print = source_error;
2112 if (annotation_level > 1 && instream == stdin)
2114 printf_unfiltered ("\n\032\032pre-");
2115 printf_unfiltered (annotation_suffix);
2116 printf_unfiltered ("\n");
2119 /* Don't use fancy stuff if not talking to stdin. */
2120 if (readline_hook && instream == NULL)
2122 rl = (*readline_hook) (local_prompt);
2124 else if (command_editing_p && instream == stdin && ISATTY (instream))
2126 rl = readline (local_prompt);
2130 rl = gdb_readline (local_prompt);
2133 if (annotation_level > 1 && instream == stdin)
2135 printf_unfiltered ("\n\032\032post-");
2136 printf_unfiltered (annotation_suffix);
2137 printf_unfiltered ("\n");
2140 if (!rl || rl == (char *) EOF)
2145 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
2147 linelength = strlen(rl) + 1 + (p - linebuffer);
2148 nline = (char *) xrealloc (linebuffer, linelength);
2149 p += nline - linebuffer;
2153 /* Copy line. Don't copy null at end. (Leaves line alone
2154 if this was just a newline) */
2158 free (rl); /* Allocated in readline. */
2160 if (p == linebuffer || *(p - 1) != '\\')
2163 p--; /* Put on top of '\'. */
2164 local_prompt = (char *) 0;
2169 signal (STOP_SIGNAL, SIG_DFL);
2176 #define SERVER_COMMAND_LENGTH 7
2178 (p - linebuffer > SERVER_COMMAND_LENGTH)
2179 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
2182 /* Note that we don't set `line'. Between this and the check in
2183 dont_repeat, this insures that repeating will still do the
2186 return linebuffer + SERVER_COMMAND_LENGTH;
2189 /* Do history expansion if that is wished. */
2190 if (history_expansion_p && instream == stdin
2191 && ISATTY (instream))
2193 char *history_value;
2196 *p = '\0'; /* Insert null now. */
2197 expanded = history_expand (linebuffer, &history_value);
2200 /* Print the changes. */
2201 printf_unfiltered ("%s\n", history_value);
2203 /* If there was an error, call this function again. */
2206 free (history_value);
2207 return command_line_input (prrompt, repeat, annotation_suffix);
2209 if (strlen (history_value) > linelength)
2211 linelength = strlen (history_value) + 1;
2212 linebuffer = (char *) xrealloc (linebuffer, linelength);
2214 strcpy (linebuffer, history_value);
2215 p = linebuffer + strlen(linebuffer);
2216 free (history_value);
2220 /* If we just got an empty line, and that is supposed
2221 to repeat the previous command, return the value in the
2223 if (repeat && p == linebuffer)
2225 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++) ;
2231 /* Add line to history if appropriate. */
2232 if (instream == stdin
2233 && ISATTY (stdin) && *linebuffer)
2234 add_history (linebuffer);
2236 /* Note: lines consisting solely of comments are added to the command
2237 history. This is useful when you type a command, and then
2238 realize you don't want to execute it quite yet. You can comment
2239 out the command and then later fetch it from the value history
2240 and remove the '#'. The kill ring is probably better, but some
2241 people are in the habit of commenting things out. */
2243 *p1 = '\0'; /* Found a comment. */
2245 /* Save into global buffer if appropriate. */
2248 if (linelength > linesize)
2250 line = xrealloc (line, linelength);
2251 linesize = linelength;
2253 strcpy (line, linebuffer);
2261 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2262 code bodies. This is typically used when we encounter an "else"
2263 clause for an "if" command. */
2266 realloc_body_list (command, new_length)
2267 struct command_line *command;
2271 struct command_line **body_list;
2273 n = command->body_count;
2275 /* Nothing to do? */
2276 if (new_length <= n)
2279 body_list = (struct command_line **)
2280 xmalloc (sizeof (struct command_line *) * new_length);
2282 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
2284 free (command->body_list);
2285 command->body_list = body_list;
2286 command->body_count = new_length;
2289 /* Read one line from the input stream. If the command is an "else" or
2290 "end", return such an indication to the caller. */
2292 static enum misc_command_type
2293 read_next_line (command)
2294 struct command_line **command;
2296 char *p, *p1, *prompt_ptr, control_prompt[256];
2299 if (control_level >= 254)
2300 error ("Control nesting too deep!\n");
2302 /* Set a prompt based on the nesting of the control commands. */
2303 if (instream == stdin || (instream == 0 && readline_hook != NULL))
2305 for (i = 0; i < control_level; i++)
2306 control_prompt[i] = ' ';
2307 control_prompt[i] = '>';
2308 control_prompt[i+1] = '\0';
2309 prompt_ptr = (char *)&control_prompt[0];
2314 p = command_line_input (prompt_ptr, instream == stdin, "commands");
2316 /* Not sure what to do here. */
2320 /* Strip leading and trailing whitespace. */
2321 while (*p == ' ' || *p == '\t')
2324 p1 = p + strlen (p);
2325 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
2328 /* Blanks and comments don't really do anything, but we need to
2329 distinguish them from else, end and other commands which can be
2331 if (p1 == p || p[0] == '#')
2334 /* Is this the end of a simple, while, or if control structure? */
2335 if (p1 - p == 3 && !strncmp (p, "end", 3))
2338 /* Is the else clause of an if control structure? */
2339 if (p1 - p == 4 && !strncmp (p, "else", 4))
2340 return else_command;
2342 /* Check for while, if, break, continue, etc and build a new command
2343 line structure for them. */
2344 if (p1 - p > 5 && !strncmp (p, "while", 5))
2345 *command = build_command_line (while_control, p + 6);
2346 else if (p1 - p > 2 && !strncmp (p, "if", 2))
2347 *command = build_command_line (if_control, p + 3);
2348 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
2350 *command = (struct command_line *)
2351 xmalloc (sizeof (struct command_line));
2352 (*command)->next = NULL;
2353 (*command)->line = NULL;
2354 (*command)->control_type = break_control;
2355 (*command)->body_count = 0;
2356 (*command)->body_list = NULL;
2358 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
2360 *command = (struct command_line *)
2361 xmalloc (sizeof (struct command_line));
2362 (*command)->next = NULL;
2363 (*command)->line = NULL;
2364 (*command)->control_type = continue_control;
2365 (*command)->body_count = 0;
2366 (*command)->body_list = NULL;
2370 /* A normal command. */
2371 *command = (struct command_line *)
2372 xmalloc (sizeof (struct command_line));
2373 (*command)->next = NULL;
2374 (*command)->line = savestring (p, p1 - p);
2375 (*command)->control_type = simple_control;
2376 (*command)->body_count = 0;
2377 (*command)->body_list = NULL;
2380 /* Nothing special. */
2384 /* Recursively read in the control structures and create a command_line
2385 structure from them.
2387 The parent_control parameter is the control structure in which the
2388 following commands are nested. */
2390 static enum command_control_type
2391 recurse_read_control_structure (current_cmd)
2392 struct command_line *current_cmd;
2394 int current_body, i;
2395 enum misc_command_type val;
2396 enum command_control_type ret;
2397 struct command_line **body_ptr, *child_tail, *next;
2402 /* Sanity checks. */
2403 if (current_cmd->control_type == simple_control)
2405 error ("Recursed on a simple control type\n");
2406 return invalid_control;
2409 if (current_body > current_cmd->body_count)
2411 error ("Allocated body is smaller than this command type needs\n");
2412 return invalid_control;
2415 /* Read lines from the input stream and build control structures. */
2421 val = read_next_line (&next);
2423 /* Just skip blanks and comments. */
2424 if (val == nop_command)
2427 if (val == end_command)
2429 if (current_cmd->control_type == while_control
2430 || current_cmd->control_type == if_control)
2432 /* Success reading an entire control structure. */
2433 ret = simple_control;
2438 ret = invalid_control;
2443 /* Not the end of a control structure. */
2444 if (val == else_command)
2446 if (current_cmd->control_type == if_control
2447 && current_body == 1)
2449 realloc_body_list (current_cmd, 2);
2456 ret = invalid_control;
2463 child_tail->next = next;
2467 body_ptr = current_cmd->body_list;
2468 for (i = 1; i < current_body; i++)
2477 /* If the latest line is another control structure, then recurse
2479 if (next->control_type == while_control
2480 || next->control_type == if_control)
2483 ret = recurse_read_control_structure (next);
2486 if (ret != simple_control)
2496 /* Read lines from the input stream and accumulate them in a chain of
2497 struct command_line's, which is then returned. For input from a
2498 terminal, the special command "end" is used to mark the end of the
2499 input, and is not included in the returned chain of commands. */
2501 #define END_MESSAGE "End with a line saying just \"end\"."
2503 struct command_line *
2504 read_command_lines (prompt, from_tty)
2508 struct command_line *head, *tail, *next;
2509 struct cleanup *old_chain;
2510 enum command_control_type ret;
2511 enum misc_command_type val;
2514 if (readline_begin_hook)
2516 /* Note - intentional to merge messages with no newline */
2517 (*readline_begin_hook) ("%s %s\n", prompt, END_MESSAGE);
2519 else if (from_tty && input_from_terminal_p ())
2521 printf_unfiltered ("%s\n%s\n", prompt, END_MESSAGE);
2522 gdb_flush (gdb_stdout);
2530 val = read_next_line (&next);
2532 /* Ignore blank lines or comments. */
2533 if (val == nop_command)
2536 if (val == end_command)
2538 ret = simple_control;
2542 if (val != ok_command)
2544 ret = invalid_control;
2548 if (next->control_type == while_control
2549 || next->control_type == if_control)
2552 ret = recurse_read_control_structure (next);
2555 if (ret == invalid_control)
2566 old_chain = make_cleanup ((make_cleanup_func) free_command_lines,
2576 if (ret != invalid_control)
2578 discard_cleanups (old_chain);
2581 do_cleanups (old_chain);
2584 if (readline_end_hook)
2586 (*readline_end_hook) ();
2591 /* Free a chain of struct command_line's. */
2594 free_command_lines (lptr)
2595 struct command_line **lptr;
2597 register struct command_line *l = *lptr;
2598 register struct command_line *next;
2599 struct command_line **blist;
2604 if (l->body_count > 0)
2606 blist = l->body_list;
2607 for (i = 0; i < l->body_count; i++, blist++)
2608 free_command_lines (blist);
2617 /* Add an element to the list of info subcommands. */
2620 add_info (name, fun, doc)
2622 void (*fun) PARAMS ((char *, int));
2625 add_cmd (name, no_class, fun, doc, &infolist);
2628 /* Add an alias to the list of info subcommands. */
2631 add_info_alias (name, oldname, abbrev_flag)
2636 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2639 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2640 Therefore, its own definition is called only for "info" with no args. */
2644 info_command (arg, from_tty)
2648 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2649 help_list (infolist, "info ", -1, gdb_stdout);
2652 /* The "complete" command is used by Emacs to implement completion. */
2656 complete_command (arg, from_tty)
2668 argpoint = strlen (arg);
2670 for (completion = line_completion_function (arg, i = 0, arg, argpoint);
2672 completion = line_completion_function (arg, ++i, arg, argpoint))
2674 printf_unfiltered ("%s\n", completion);
2679 /* The "show" command with no arguments shows all the settings. */
2683 show_command (arg, from_tty)
2687 cmd_show_list (showlist, from_tty, "");
2690 /* Add an element to the list of commands. */
2693 add_com (name, class, fun, doc)
2695 enum command_class class;
2696 void (*fun) PARAMS ((char *, int));
2699 add_cmd (name, class, fun, doc, &cmdlist);
2702 /* Add an alias or abbreviation command to the list of commands. */
2705 add_com_alias (name, oldname, class, abbrev_flag)
2708 enum command_class class;
2711 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2718 error ("Argument required (%s).", why);
2723 help_command (command, from_tty)
2725 int from_tty; /* Ignored */
2727 help_cmd (command, gdb_stdout);
2731 validate_comname (comname)
2737 error_no_arg ("name of command to define");
2742 if (!isalnum(*p) && *p != '-' && *p != '_')
2743 error ("Junk in argument list: \"%s\"", p);
2748 /* This is just a placeholder in the command data structures. */
2750 user_defined_command (ignore, from_tty)
2757 define_command (comname, from_tty)
2761 register struct command_line *cmds;
2762 register struct cmd_list_element *c, *newc, *hookc = 0;
2763 char *tem = comname;
2765 #define HOOK_STRING "hook-"
2768 validate_comname (comname);
2770 /* Look it up, and verify that we got an exact match. */
2771 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
2772 if (c && !STREQ (comname, c->name))
2777 if (c->class == class_user || c->class == class_alias)
2778 tem = "Redefine command \"%s\"? ";
2780 tem = "Really redefine built-in command \"%s\"? ";
2781 if (!query (tem, c->name))
2782 error ("Command \"%s\" not redefined.", c->name);
2785 /* If this new command is a hook, then mark the command which it
2786 is hooking. Note that we allow hooking `help' commands, so that
2787 we can hook the `stop' pseudo-command. */
2789 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
2791 /* Look up cmd it hooks, and verify that we got an exact match. */
2792 tem = comname+HOOK_LEN;
2793 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
2794 if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
2798 warning ("Your new `%s' command does not hook any existing command.",
2800 if (!query ("Proceed? "))
2801 error ("Not confirmed.");
2805 comname = savestring (comname, strlen (comname));
2807 /* If the rest of the commands will be case insensitive, this one
2808 should behave in the same manner. */
2809 for (tem = comname; *tem; tem++)
2810 if (isupper(*tem)) *tem = tolower(*tem);
2812 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
2813 cmds = read_command_lines (tmpbuf, from_tty);
2815 if (c && c->class == class_user)
2816 free_command_lines (&c->user_commands);
2818 newc = add_cmd (comname, class_user, user_defined_command,
2819 (c && c->class == class_user)
2820 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
2821 newc->user_commands = cmds;
2823 /* If this new command is a hook, then mark both commands as being
2827 hookc->hook = newc; /* Target gets hooked. */
2828 newc->hookee = hookc; /* We are marked as hooking target cmd. */
2833 document_command (comname, from_tty)
2837 struct command_line *doclines;
2838 register struct cmd_list_element *c;
2839 char *tem = comname;
2842 validate_comname (comname);
2844 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
2846 if (c->class != class_user)
2847 error ("Command \"%s\" is built-in.", comname);
2849 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
2850 doclines = read_command_lines (tmpbuf, from_tty);
2852 if (c->doc) free (c->doc);
2855 register struct command_line *cl1;
2856 register int len = 0;
2858 for (cl1 = doclines; cl1; cl1 = cl1->next)
2859 len += strlen (cl1->line) + 1;
2861 c->doc = (char *) xmalloc (len + 1);
2864 for (cl1 = doclines; cl1; cl1 = cl1->next)
2866 strcat (c->doc, cl1->line);
2868 strcat (c->doc, "\n");
2872 free_command_lines (&doclines);
2875 /* Print the GDB banner. */
2877 print_gdb_version (stream)
2880 /* From GNU coding standards, first line is meant to be easy for a
2881 program to parse, and is just canonical program name and version
2882 number, which starts after last space. */
2884 fprintf_filtered (stream, "GNU gdb %s\n", version);
2886 /* Second line is a copyright notice. */
2888 fprintf_filtered (stream, "Copyright 1998 Free Software Foundation, Inc.\n");
2890 /* Following the copyright is a brief statement that the program is
2891 free software, that users are free to copy and change it on
2892 certain conditions, that it is covered by the GNU GPL, and that
2893 there is no warranty. */
2895 fprintf_filtered (stream, "\
2896 GDB is free software, covered by the GNU General Public License, and you are\n\
2897 welcome to change it and/or distribute copies of it under certain conditions.\n\
2898 Type \"show copying\" to see the conditions.\n\
2899 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2901 /* After the required info we print the configuration information. */
2903 fprintf_filtered (stream, "This GDB was configured as \"");
2904 if (!STREQ (host_name, target_name))
2906 fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
2910 fprintf_filtered (stream, "%s", host_name);
2912 fprintf_filtered (stream, "\".");
2917 show_version (args, from_tty)
2922 print_gdb_version (gdb_stdout);
2923 printf_filtered ("\n");
2927 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2933 printf_unfiltered ("%s", prompt);
2934 gdb_flush (gdb_stdout);
2937 /* This replaces the above for the frontends: it returns a pointer
2949 /* ??rehrauer: I don't know why this fails, since it looks as though
2950 assignments to prompt are wrapped in calls to savestring...
2954 prompt = savestring (s, strlen (s));
2958 /* If necessary, make the user confirm that we should quit. Return
2959 non-zero if we should quit, zero if we shouldn't. */
2964 if (inferior_pid != 0 && target_has_execution)
2968 /* This is something of a hack. But there's no reliable way to
2969 see if a GUI is running. The `use_windows' variable doesn't
2972 s = "A debugging session is active.\nDo you still want to close the debugger?";
2973 else if (attach_flag)
2974 s = "The program is running. Quit anyway (and detach it)? ";
2976 s = "The program is running. Exit anyway? ";
2985 /* Quit without asking for confirmation. */
2988 quit_force (args, from_tty)
2994 /* An optional expression may be used to cause gdb to terminate with the
2995 value of that expression. */
2998 value_ptr val = parse_and_eval (args);
3000 exit_code = (int) value_as_long (val);
3003 if (inferior_pid != 0 && target_has_execution)
3006 target_detach (args, from_tty);
3011 /* UDI wants this, to kill the TIP. */
3014 /* Save the history information if it is appropriate to do so. */
3015 if (write_history_p && history_filename)
3016 write_history (history_filename);
3018 do_final_cleanups(ALL_CLEANUPS); /* Do any final cleanups before exiting */
3021 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3022 /* The above does not need to be inside a tuiDo(), since
3023 * it is not manipulating the curses screen, but rather,
3024 * it is tearing it down.
3033 /* Handle the quit command. */
3036 quit_command (args, from_tty)
3040 if (! quit_confirm ())
3041 error ("Not confirmed.");
3042 quit_force (args, from_tty);
3045 /* Returns whether GDB is running on a terminal and whether the user
3046 desires that questions be asked of them on that terminal. */
3049 input_from_terminal_p ()
3051 return gdb_has_a_terminal () && (instream == stdin) & caution;
3056 pwd_command (args, from_tty)
3060 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
3061 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
3063 if (!STREQ (gdb_dirbuf, current_directory))
3064 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3065 current_directory, gdb_dirbuf);
3067 printf_unfiltered ("Working directory %s.\n", current_directory);
3071 cd_command (dir, from_tty)
3076 /* Found something other than leading repetitions of "/..". */
3077 int found_real_path;
3080 /* If the new directory is absolute, repeat is a no-op; if relative,
3081 repeat might be useful but is more likely to be a mistake. */
3085 error_no_arg ("new working directory");
3087 dir = tilde_expand (dir);
3088 make_cleanup (free, dir);
3090 if (chdir (dir) < 0)
3091 perror_with_name (dir);
3094 dir = savestring (dir, len - (len > 1 && SLASH_P(dir[len-1])));
3096 current_directory = dir;
3099 if (SLASH_P (current_directory[0]) && current_directory[1] == '\0')
3100 current_directory = concat (current_directory, dir, NULL);
3102 current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
3106 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3108 found_real_path = 0;
3109 for (p = current_directory; *p;)
3111 if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2])))
3113 else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.'
3114 && (p[3] == 0 || SLASH_P (p[3])))
3116 if (found_real_path)
3118 /* Search backwards for the directory just before the "/.."
3119 and obliterate it and the "/..". */
3121 while (q != current_directory && ! SLASH_P (q[-1]))
3124 if (q == current_directory)
3125 /* current_directory is
3126 a relative pathname ("can't happen"--leave it alone). */
3130 strcpy (q - 1, p + 3);
3135 /* We are dealing with leading repetitions of "/..", for example
3136 "/../..", which is the Mach super-root. */
3141 found_real_path = 1;
3146 forget_cached_source_info ();
3149 pwd_command ((char *) 0, 1);
3152 struct source_cleanup_lines_args {
3155 char *old_pre_error;
3156 char *old_error_pre_print;
3160 source_cleanup_lines (args)
3163 struct source_cleanup_lines_args *p =
3164 (struct source_cleanup_lines_args *)args;
3165 source_line_number = p->old_line;
3166 source_file_name = p->old_file;
3167 source_pre_error = p->old_pre_error;
3168 error_pre_print = p->old_error_pre_print;
3173 source_command (args, from_tty)
3178 struct cleanup *old_cleanups;
3180 struct source_cleanup_lines_args old_lines;
3185 error ("source command requires pathname of file to source.");
3188 file = tilde_expand (file);
3189 old_cleanups = make_cleanup (free, file);
3191 stream = fopen (file, FOPEN_RT);
3195 perror_with_name (file);
3200 make_cleanup ((make_cleanup_func) fclose, stream);
3202 old_lines.old_line = source_line_number;
3203 old_lines.old_file = source_file_name;
3204 old_lines.old_pre_error = source_pre_error;
3205 old_lines.old_error_pre_print = error_pre_print;
3206 make_cleanup (source_cleanup_lines, &old_lines);
3207 source_line_number = 0;
3208 source_file_name = file;
3209 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
3210 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
3211 make_cleanup (free, source_pre_error);
3212 /* This will get set every time we read a line. So it won't stay "" for
3214 error_pre_print = "";
3216 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
3217 if (source_error_allocated < needed_length)
3219 source_error_allocated *= 2;
3220 if (source_error_allocated < needed_length)
3221 source_error_allocated = needed_length;
3222 if (source_error == NULL)
3223 source_error = xmalloc (source_error_allocated);
3225 source_error = xrealloc (source_error, source_error_allocated);
3228 read_command_file (stream);
3230 do_cleanups (old_cleanups);
3235 echo_command (text, from_tty)
3243 while ((c = *p++) != '\0')
3247 /* \ at end of argument is used after spaces
3248 so they won't be lost. */
3252 c = parse_escape (&p);
3254 printf_filtered ("%c", c);
3257 printf_filtered ("%c", c);
3260 /* Force this output to appear now. */
3262 gdb_flush (gdb_stdout);
3267 dont_repeat_command (ignored, from_tty)
3271 *line = 0; /* Can't call dont_repeat here because we're not
3272 necessarily reading from stdin. */
3275 /* Functions to manipulate command line editing control variables. */
3277 /* Number of commands to print in each call to show_commands. */
3278 #define Hist_print 10
3280 show_commands (args, from_tty)
3284 /* Index for history commands. Relative to history_base. */
3287 /* Number of the history entry which we are planning to display next.
3288 Relative to history_base. */
3291 /* The first command in the history which doesn't exist (i.e. one more
3292 than the number of the last command). Relative to history_base. */
3295 extern HIST_ENTRY *history_get PARAMS ((int));
3297 /* Print out some of the commands from the command history. */
3298 /* First determine the length of the history list. */
3299 hist_len = history_size;
3300 for (offset = 0; offset < history_size; offset++)
3302 if (!history_get (history_base + offset))
3311 if (args[0] == '+' && args[1] == '\0')
3312 /* "info editing +" should print from the stored position. */
3315 /* "info editing <exp>" should print around command number <exp>. */
3316 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
3318 /* "show commands" means print the last Hist_print commands. */
3321 num = hist_len - Hist_print;
3327 /* If there are at least Hist_print commands, we want to display the last
3328 Hist_print rather than, say, the last 6. */
3329 if (hist_len - num < Hist_print)
3331 num = hist_len - Hist_print;
3336 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
3338 printf_filtered ("%5d %s\n", history_base + offset,
3339 (history_get (history_base + offset))->line);
3342 /* The next command we want to display is the next one that we haven't
3346 /* If the user repeats this command with return, it should do what
3347 "show commands +" does. This is unnecessary if arg is null,
3348 because "show commands +" is not useful after "show commands". */
3349 if (from_tty && args)
3356 /* Called by do_setshow_command. */
3359 set_history_size_command (args, from_tty, c)
3362 struct cmd_list_element *c;
3364 if (history_size == INT_MAX)
3365 unstifle_history ();
3366 else if (history_size >= 0)
3367 stifle_history (history_size);
3370 history_size = INT_MAX;
3371 error ("History size must be non-negative");
3377 set_history (args, from_tty)
3381 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3382 help_list (sethistlist, "set history ", -1, gdb_stdout);
3387 show_history (args, from_tty)
3391 cmd_show_list (showhistlist, from_tty, "");
3394 int info_verbose = 0; /* Default verbose msgs off */
3396 /* Called by do_setshow_command. An elaborate joke. */
3399 set_verbose (args, from_tty, c)
3402 struct cmd_list_element *c;
3404 char *cmdname = "verbose";
3405 struct cmd_list_element *showcmd;
3407 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
3411 c->doc = "Set verbose printing of informational messages.";
3412 showcmd->doc = "Show verbose printing of informational messages.";
3416 c->doc = "Set verbosity.";
3417 showcmd->doc = "Show verbosity.";
3422 float_handler (signo)
3425 /* This message is based on ANSI C, section 4.7. Note that integer
3426 divide by zero causes this, so "float" is a misnomer. */
3427 signal (SIGFPE, float_handler);
3428 error ("Erroneous arithmetic operation.");
3442 enablebreaklist = NULL;
3447 showhistlist = NULL;
3448 unsethistlist = NULL;
3449 maintenancelist = NULL;
3450 maintenanceinfolist = NULL;
3451 maintenanceprintlist = NULL;
3452 setprintlist = NULL;
3453 showprintlist = NULL;
3454 setchecklist = NULL;
3455 showchecklist = NULL;
3458 /* Init the history buffer. Note that we are called after the init file(s)
3459 * have been read so that the user can change the history file via his
3460 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3461 * overrides all of this.
3469 tmpenv = getenv ("HISTSIZE");
3471 history_size = atoi (tmpenv);
3472 else if (!history_size)
3475 stifle_history (history_size);
3477 tmpenv = getenv ("GDBHISTFILE");
3479 history_filename = savestring (tmpenv, strlen(tmpenv));
3480 else if (!history_filename) {
3481 /* We include the current directory so that if the user changes
3482 directories the file written will be the same as the one
3484 history_filename = concat (current_directory, "/.gdb_history", NULL);
3486 read_history (history_filename);
3492 struct cmd_list_element *c;
3494 /* from event-top.c */
3495 extern int async_command_editing_p;
3496 extern struct prompts the_prompts;
3497 extern char *async_annotation_suffix;
3498 extern char *new_async_prompt;
3500 /* If we are running the asynchronous version,
3501 we initialize the prompts differently. */
3504 #ifdef DEFAULT_PROMPT
3505 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
3507 prompt = savestring ("(gdb) ", 6);
3512 /* initialize the prompt stack to a simple "(gdb) " prompt or to
3513 whatever the DEFULAT_PROMPT is. */
3514 the_prompts.top = 0;
3516 #ifdef DEFAULT_PROMPT
3517 PROMPT(0) = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
3519 PROMPT(0) = savestring ("(gdb) ", 6);
3522 /* Set things up for annotation_level > 1, if the user ever decides
3524 async_annotation_suffix = "prompt";
3525 /* Set the variable associated with the setshow prompt command. */
3526 new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0)));
3529 /* Set the important stuff up for command editing. */
3530 command_editing_p = 1;
3531 async_command_editing_p = 1;
3532 history_expansion_p = 0;
3533 write_history_p = 0;
3535 /* Setup important stuff for command line editing. */
3536 rl_completion_entry_function = (int (*)()) readline_line_completion_function;
3537 rl_completer_word_break_characters = gdb_completer_word_break_characters;
3538 rl_completer_quote_characters = gdb_completer_quote_characters;
3539 rl_readline_name = "gdb";
3541 /* Define the classes of commands.
3542 They will appear in the help list in the reverse of this order. */
3544 add_cmd ("internals", class_maintenance, NO_FUNCTION,
3545 "Maintenance commands.\n\
3546 Some gdb commands are provided just for use by gdb maintainers.\n\
3547 These commands are subject to frequent change, and may not be as\n\
3548 well documented as user commands.",
3550 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
3551 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
3552 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
3553 The commands in this class are those defined by the user.\n\
3554 Use the \"define\" command to define a command.", &cmdlist);
3555 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
3557 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
3558 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
3559 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
3560 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
3561 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
3562 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3563 counting from zero for the innermost (currently executing) frame.\n\n\
3564 At any time gdb identifies one frame as the \"selected\" frame.\n\
3565 Variable lookups are done with respect to the selected frame.\n\
3566 When the program being debugged stops, gdb selects the innermost frame.\n\
3567 The commands below can be used to select other frames by number or address.",
3569 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
3571 add_com ("pwd", class_files, pwd_command,
3572 "Print working directory. This is used for your program as well.");
3573 c = add_cmd ("cd", class_files, cd_command,
3574 "Set working directory to DIR for debugger and program being debugged.\n\
3575 The change does not take effect for the program being debugged\n\
3576 until the next time it is started.", &cmdlist);
3577 c->completer = filename_completer;
3579 /* The set prompt command is different depending whether or not the
3580 async version is run. NOTE: this difference is going to
3581 disappear as we make the event loop be the default engine of
3586 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
3593 c = add_set_cmd ("prompt", class_support, var_string,
3594 (char *)&new_async_prompt,
3597 add_show_from_set (c, &showlist);
3598 c->function.sfunc = set_async_prompt;
3601 add_com ("echo", class_support, echo_command,
3602 "Print a constant string. Give string as argument.\n\
3603 C escape sequences may be used in the argument.\n\
3604 No newline is added at the end of the argument;\n\
3605 use \"\\n\" if you want a newline to be printed.\n\
3606 Since leading and trailing whitespace are ignored in command arguments,\n\
3607 if you want to print some you must use \"\\\" before leading whitespace\n\
3608 to be printed or after trailing whitespace.");
3609 add_com ("document", class_support, document_command,
3610 "Document a user-defined command.\n\
3611 Give command name as argument. Give documentation on following lines.\n\
3612 End with a line of just \"end\".");
3613 add_com ("define", class_support, define_command,
3614 "Define a new command name. Command name is argument.\n\
3615 Definition appears on following lines, one command per line.\n\
3616 End with a line of just \"end\".\n\
3617 Use the \"document\" command to give documentation for the new command.\n\
3618 Commands defined in this way may have up to ten arguments.");
3621 c = add_cmd ("source", class_support, source_command,
3622 "Read commands from a file named FILE.\n\
3623 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
3624 when gdb is started.", &cmdlist);
3626 /* Punt file name, we can't help it easily. */
3627 c = add_cmd ("source", class_support, source_command,
3628 "Read commands from a file named FILE.\n\
3629 Note that the file \".gdbinit\" is read automatically in this way\n\
3630 when gdb is started.", &cmdlist);
3632 c->completer = filename_completer;
3634 add_com ("quit", class_support, quit_command, "Exit gdb.");
3635 add_com ("help", class_support, help_command, "Print list of commands.");
3636 add_com_alias ("q", "quit", class_support, 1);
3637 add_com_alias ("h", "help", class_support, 1);
3639 add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
3640 Primarily used inside of user-defined commands that should not be repeated when\n\
3643 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
3646 add_show_from_set (c, &showlist);
3647 c->function.sfunc = set_verbose;
3648 set_verbose (NULL, 0, c);
3650 /* The set editing command is different depending whether or not the
3651 async version is run. NOTE: this difference is going to disappear
3652 as we make the event loop be the default engine of gdb. */
3656 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
3657 "Set editing of command lines as they are typed.\n\
3658 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3659 Without an argument, command line editing is enabled. To edit, use\n\
3660 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
3665 c = add_set_cmd ("editing", class_support, var_boolean, (char *)&async_command_editing_p,
3666 "Set editing of command lines as they are typed.\n\
3667 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3668 Without an argument, command line editing is enabled. To edit, use\n\
3669 EMACS-like or VI-like commands like control-P or ESC.", &setlist);
3671 add_show_from_set (c, &showlist);
3672 c->function.sfunc = set_async_editing_command;
3675 add_prefix_cmd ("history", class_support, set_history,
3676 "Generic command for setting command history parameters.",
3677 &sethistlist, "set history ", 0, &setlist);
3678 add_prefix_cmd ("history", class_support, show_history,
3679 "Generic command for showing command history parameters.",
3680 &showhistlist, "show history ", 0, &showlist);
3683 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
3684 "Set history expansion on command input.\n\
3685 Without an argument, history expansion is enabled.", &sethistlist),
3689 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
3690 "Set saving of the history record on exit.\n\
3691 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
3692 Without an argument, saving is enabled.", &sethistlist),
3695 c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
3696 "Set the size of the command history, \n\
3697 ie. the number of previous commands to keep a record of.", &sethistlist);
3698 add_show_from_set (c, &showhistlist);
3699 c->function.sfunc = set_history_size_command;
3702 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
3703 "Set the filename in which to record the command history\n\
3704 (the list of previous commands of which a record is kept).", &sethistlist),
3708 (add_set_cmd ("confirm", class_support, var_boolean,
3710 "Set whether to confirm potentially dangerous operations.",
3714 add_prefix_cmd ("info", class_info, info_command,
3715 "Generic command for showing things about the program being debugged.",
3716 &infolist, "info ", 0, &cmdlist);
3717 add_com_alias ("i", "info", class_info, 1);
3719 add_com ("complete", class_obscure, complete_command,
3720 "List the completions for the rest of the line as a command.");
3722 add_prefix_cmd ("show", class_info, show_command,
3723 "Generic command for showing things about the debugger.",
3724 &showlist, "show ", 0, &cmdlist);
3725 /* Another way to get at the same thing. */
3726 add_info ("set", show_command, "Show all GDB settings.");
3728 add_cmd ("commands", no_class, show_commands,
3729 "Show the history of commands you typed.\n\
3730 You can supply a command number to start with, or a `+' to start after\n\
3731 the previous command number shown.",
3734 add_cmd ("version", no_class, show_version,
3735 "Show what version of GDB this is.", &showlist);
3737 add_com ("while", class_support, while_command,
3738 "Execute nested commands WHILE the conditional expression is non zero.\n\
3739 The conditional expression must follow the word `while' and must in turn be\n\
3740 followed by a new line. The nested commands must be entered one per line,\n\
3741 and should be terminated by the word `end'.");
3743 add_com ("if", class_support, if_command,
3744 "Execute nested commands once IF the conditional expression is non zero.\n\
3745 The conditional expression must follow the word `if' and must in turn be\n\
3746 followed by a new line. The nested commands must be entered one per line,\n\
3747 and should be terminated by the word 'else' or `end'. If an else clause\n\
3748 is used, the same rules apply to its nested commands as to the first ones.");
3750 /* If target is open when baud changes, it doesn't take effect until the
3751 next open (I think, not sure). */
3752 add_show_from_set (add_set_cmd ("remotebaud", no_class,
3753 var_zinteger, (char *)&baud_rate,
3754 "Set baud rate for remote serial I/O.\n\
3755 This value is used to set the speed of the serial port when debugging\n\
3756 using remote targets.", &setlist),
3760 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug,
3761 "Set debugging of remote protocol.\n\
3762 When enabled, each packet sent or received with the remote target\n\
3763 is displayed.", &setlist),
3767 add_set_cmd ("remotetimeout", no_class, var_integer, (char *)&remote_timeout,
3768 "Set timeout limit to wait for target to respond.\n\
3769 This value is used to set the time limit for gdb to wait for a response\n\
3770 from the target.", &setlist),
3773 /* The set annotate command is different depending whether or not
3774 the async version is run. NOTE: this difference is going to
3775 disappear as we make the event loop be the default engine of
3779 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
3780 (char *)&annotation_level, "Set annotation_level.\n\
3781 0 == normal; 1 == fullname (for use when running under emacs)\n\
3782 2 == output annotated suitably for use by programs that control GDB.",
3784 c = add_show_from_set (c, &showlist);
3788 c = add_set_cmd ("annotate", class_obscure, var_zinteger,
3789 (char *)&annotation_level, "Set annotation_level.\n\
3790 0 == normal; 1 == fullname (for use when running under emacs)\n\
3791 2 == output annotated suitably for use by programs that control GDB.",
3793 add_show_from_set (c, &showlist);
3794 c->function.sfunc = set_async_annotation_level;