1 /* Top level stuff for GDB, the GNU debugger.
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
26 #include "call-cmds.h"
27 #include "cli/cli-cmds.h"
28 #include "cli/cli-script.h"
29 #include "cli/cli-setshow.h"
30 #include "cli/cli-decode.h"
33 #include "exceptions.h"
36 #include "breakpoint.h"
38 #include "expression.h"
41 #include "terminal.h" /* For job_control. */
43 #include "completer.h"
48 #include "gdb_assert.h"
50 /* readline include files */
51 #include "readline/readline.h"
52 #include "readline/history.h"
54 /* readline defines this. */
57 #include <sys/types.h>
59 #include "event-top.h"
60 #include "gdb_string.h"
66 /* Default command line prompt. This is overriden in some configs. */
68 #ifndef DEFAULT_PROMPT
69 #define DEFAULT_PROMPT "(gdb) "
72 /* Initialization file name for gdb. This is overridden in some configs. */
74 #ifndef GDBINIT_FILENAME
75 #define GDBINIT_FILENAME ".gdbinit"
77 char gdbinit[] = GDBINIT_FILENAME;
79 int inhibit_gdbinit = 0;
81 /* If nonzero, and GDB has been configured to be able to use windows,
82 attempt to open them upon startup. */
86 extern char lang_frame_mismatch_warn[]; /* language.c */
88 /* Flag for whether we want all the "from_tty" gubbish printed. */
90 int caution = 1; /* Default is yes, sigh. */
92 show_caution (struct ui_file *file, int from_tty,
93 struct cmd_list_element *c, const char *value)
95 fprintf_filtered (file, _("\
96 Whether to confirm potentially dangerous operations is %s.\n"),
100 /* stdio stream that command input is being read from. Set to stdin normally.
101 Set by source_command to the file we are sourcing. Set to NULL if we are
102 executing a user-defined command or interacting via a GUI. */
106 /* Current working directory. */
108 char *current_directory;
110 /* The directory name is actually stored here (usually). */
111 char gdb_dirbuf[1024];
113 /* Function to call before reading a command, if nonzero.
114 The function receives two args: an input stream,
115 and a prompt string. */
117 void (*window_hook) (FILE *, char *);
122 /* gdb prints this when reading a command interactively */
123 static char *gdb_prompt_string; /* the global prompt string */
125 /* Buffer used for reading command lines, and the size
126 allocated for it so far. */
131 /* Nonzero if the current command is modified by "server ". This
132 affects things like recording into the command history, commands
133 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
134 whatever) can issue its own commands and also send along commands
135 from the user, and have the user not notice that the user interface
136 is issuing commands too. */
139 /* Baud rate specified for talking to serial target systems. Default
140 is left as -1, so targets can choose their own defaults. */
141 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
142 or (unsigned int)-1. This is a Bad User Interface. */
146 /* Timeout limit for response from target. */
148 /* The default value has been changed many times over the years. It
149 was originally 5 seconds. But that was thought to be a long time
150 to sit and wait, so it was changed to 2 seconds. That was thought
151 to be plenty unless the connection was going through some terminal
152 server or multiplexer or other form of hairy serial connection.
154 In mid-1996, remote_timeout was moved from remote.c to top.c and
155 it began being used in other remote-* targets. It appears that the
156 default was changed to 20 seconds at that time, perhaps because the
157 Renesas E7000 ICE didn't always respond in a timely manner.
159 But if 5 seconds is a long time to sit and wait for retransmissions,
160 20 seconds is far worse. This demonstrates the difficulty of using
161 a single variable for all protocol timeouts.
163 As remote.c is used much more than remote-e7000.c, it was changed
164 back to 2 seconds in 1999. */
166 int remote_timeout = 2;
168 /* Non-zero tells remote* modules to output debugging info. */
170 int remote_debug = 0;
172 /* Non-zero means the target is running. Note: this is different from
173 saying that there is an active target and we are stopped at a
174 breakpoint, for instance. This is a real indicator whether the
175 target is off and running, which gdb is doing something else. */
176 int target_executing = 0;
178 /* Level of control structure. */
179 static int control_level;
181 /* Sbrk location on entry to main. Used for statistics only. */
186 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
190 #define STOP_SIGNAL SIGTSTP
191 static void stop_sig (int);
195 /* Hooks for alternate command interfaces. */
197 /* Called after most modules have been initialized, but before taking users
200 If the UI fails to initialize and it wants GDB to continue
201 using the default UI, then it should clear this hook before returning. */
203 void (*deprecated_init_ui_hook) (char *argv0);
205 /* This hook is called from within gdb's many mini-event loops which could
206 steal control from a real user interface's event loop. It returns
207 non-zero if the user is requesting a detach, zero otherwise. */
209 int (*deprecated_ui_loop_hook) (int);
211 /* Called instead of command_loop at top level. Can be invoked via
212 throw_exception(). */
214 void (*deprecated_command_loop_hook) (void);
217 /* Called from print_frame_info to list the line we stopped in. */
219 void (*deprecated_print_frame_info_listing_hook) (struct symtab * s, int line,
220 int stopline, int noerror);
221 /* Replaces most of query. */
223 int (*deprecated_query_hook) (const char *, va_list);
225 /* Replaces most of warning. */
227 void (*deprecated_warning_hook) (const char *, va_list);
229 /* These three functions support getting lines of text from the user.
230 They are used in sequence. First deprecated_readline_begin_hook is
231 called with a text string that might be (for example) a message for
232 the user to type in a sequence of commands to be executed at a
233 breakpoint. If this function calls back to a GUI, it might take
234 this opportunity to pop up a text interaction window with this
235 message. Next, deprecated_readline_hook is called with a prompt
236 that is emitted prior to collecting the user input. It can be
237 called multiple times. Finally, deprecated_readline_end_hook is
238 called to notify the GUI that we are done with the interaction
239 window and it can close it. */
241 void (*deprecated_readline_begin_hook) (char *, ...);
242 char *(*deprecated_readline_hook) (char *);
243 void (*deprecated_readline_end_hook) (void);
245 /* Called as appropriate to notify the interface of the specified breakpoint
248 void (*deprecated_create_breakpoint_hook) (struct breakpoint * bpt);
249 void (*deprecated_delete_breakpoint_hook) (struct breakpoint * bpt);
250 void (*deprecated_modify_breakpoint_hook) (struct breakpoint * bpt);
252 /* Called as appropriate to notify the interface that we have attached
253 to or detached from an already running process. */
255 void (*deprecated_attach_hook) (void);
256 void (*deprecated_detach_hook) (void);
258 /* Called during long calculations to allow GUI to repair window damage, and to
259 check for stop buttons, etc... */
261 void (*deprecated_interactive_hook) (void);
263 /* Called when the registers have changed, as a hint to a GUI
264 to minimize window update. */
266 void (*deprecated_registers_changed_hook) (void);
268 /* Tell the GUI someone changed the register REGNO. -1 means
269 that the caller does not know which register changed or
270 that several registers have changed (see value_assign). */
271 void (*deprecated_register_changed_hook) (int regno);
273 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
274 void (*deprecated_memory_changed_hook) (CORE_ADDR addr, int len);
276 /* Called when going to wait for the target. Usually allows the GUI to run
277 while waiting for target events. */
279 ptid_t (*deprecated_target_wait_hook) (ptid_t ptid,
280 struct target_waitstatus * status);
282 /* Used by UI as a wrapper around command execution. May do various things
283 like enabling/disabling buttons, etc... */
285 void (*deprecated_call_command_hook) (struct cmd_list_element * c, char *cmd,
288 /* Called after a `set' command has finished. Is only run if the
289 `set' command succeeded. */
291 void (*deprecated_set_hook) (struct cmd_list_element * c);
293 /* Called when the current thread changes. Argument is thread id. */
295 void (*deprecated_context_hook) (int id);
297 /* Takes control from error (). Typically used to prevent longjmps out of the
298 middle of the GUI. Usually used in conjunction with a catch routine. */
300 void (*deprecated_error_hook) (void);
302 /* Handler for SIGHUP. */
305 /* NOTE 1999-04-29: This function will be static again, once we modify
306 gdb to use the event loop as the default command loop and we merge
307 event-top.c into this file, top.c */
311 caution = 0; /* Throw caution to the wind -- we're exiting.
312 This prevents asking the user dumb questions. */
313 quit_command ((char *) 0, 0);
316 #endif /* defined SIGHUP */
318 /* Line number we are currently in in a file which is being sourced. */
319 /* NOTE 1999-04-29: This variable will be static again, once we modify
320 gdb to use the event loop as the default command loop and we merge
321 event-top.c into this file, top.c */
322 /* static */ int source_line_number;
324 /* Name of the file we are sourcing. */
325 /* NOTE 1999-04-29: This variable will be static again, once we modify
326 gdb to use the event loop as the default command loop and we merge
327 event-top.c into this file, top.c */
328 /* static */ char *source_file_name;
330 /* Clean up on error during a "source" command (or execution of a
331 user-defined command). */
334 do_restore_instream_cleanup (void *stream)
336 /* Restore the previous input stream. */
340 /* Read commands from STREAM. */
342 read_command_file (FILE *stream)
344 struct cleanup *cleanups;
346 cleanups = make_cleanup (do_restore_instream_cleanup, instream);
349 do_cleanups (cleanups);
352 void (*pre_init_ui_hook) (void);
356 do_chdir_cleanup (void *old_dir)
363 /* Execute the line P as a command.
364 Pass FROM_TTY as second argument to the defining function. */
367 execute_command (char *p, int from_tty)
369 struct cmd_list_element *c;
371 static int warned = 0;
376 /* Force cleanup of any alloca areas if using C alloca instead of
380 /* This can happen when command_line_input hits end of file. */
384 serial_log_command (p);
386 while (*p == ' ' || *p == '\t')
393 c = lookup_cmd (&p, cmdlist, "", 0, 1);
395 /* If the target is running, we allow only a limited set of
397 if (target_can_async_p () && target_executing)
398 if (strcmp (c->name, "help") != 0
399 && strcmp (c->name, "pwd") != 0
400 && strcmp (c->name, "show") != 0
401 && strcmp (c->name, "stop") != 0)
402 error (_("Cannot execute this command while the target is running."));
404 /* Pass null arg rather than an empty one. */
407 /* FIXME: cagney/2002-02-02: The c->type test is pretty dodgy
408 while the is_complete_command(cfunc) test is just plain
409 bogus. They should both be replaced by a test of the form
410 c->strip_trailing_white_space_p. */
411 /* NOTE: cagney/2002-02-02: The function.cfunc in the below
412 can't be replaced with func. This is because it is the
413 cfunc, and not the func, that has the value that the
414 is_complete_command hack is testing for. */
415 /* Clear off trailing whitespace, except for set and complete
418 && c->type != set_cmd
419 && !is_complete_command (c))
421 p = arg + strlen (arg) - 1;
422 while (p >= arg && (*p == ' ' || *p == '\t'))
427 /* If this command has been pre-hooked, run the hook first. */
428 execute_cmd_pre_hook (c);
430 if (c->flags & DEPRECATED_WARN_USER)
431 deprecated_cmd_warning (&line);
433 if (c->class == class_user)
434 execute_user_command (c, arg);
435 else if (c->type == set_cmd || c->type == show_cmd)
436 do_setshow_command (arg, from_tty & caution, c);
437 else if (!cmd_func_p (c))
438 error (_("That is not a command, just a help topic."));
439 else if (deprecated_call_command_hook)
440 deprecated_call_command_hook (c, arg, from_tty & caution);
442 cmd_func (c, arg, from_tty & caution);
444 /* If this command has been post-hooked, run the hook last. */
445 execute_cmd_post_hook (c);
449 /* Tell the user if the language has changed (except first time). */
450 if (current_language != expected_language)
452 if (language_mode == language_mode_auto)
454 language_info (1); /* Print what changed. */
459 /* Warn the user if the working language does not match the
460 language of the current frame. Only warn the user if we are
461 actually running the program, i.e. there is a stack. */
462 /* FIXME: This should be cacheing the frame and only running when
463 the frame changes. */
465 if (target_has_stack)
467 flang = get_frame_language ();
469 && flang != language_unknown
470 && flang != current_language->la_language)
472 printf_filtered ("%s\n", lang_frame_mismatch_warn);
478 /* Read commands from `instream' and execute them
479 until end of file or error reading instream. */
484 struct cleanup *old_chain;
486 int stdin_is_tty = ISATTY (stdin);
487 long time_at_cmd_start;
489 long space_at_cmd_start = 0;
491 extern int display_time;
492 extern int display_space;
494 while (instream && !feof (instream))
496 if (window_hook && instream == stdin)
497 (*window_hook) (instream, get_prompt ());
500 if (instream == stdin && stdin_is_tty)
501 reinitialize_more_filter ();
502 old_chain = make_cleanup (null_cleanup, 0);
504 /* Get a command-line. This calls the readline package. */
505 command = command_line_input (instream == stdin ?
506 get_prompt () : (char *) NULL,
507 instream == stdin, "prompt");
511 time_at_cmd_start = get_run_time ();
516 char *lim = (char *) sbrk (0);
517 space_at_cmd_start = lim - lim_at_start;
521 execute_command (command, instream == stdin);
522 /* Do any commands attached to breakpoint we stopped at. */
523 bpstat_do_actions (&stop_bpstat);
524 do_cleanups (old_chain);
528 long cmd_time = get_run_time () - time_at_cmd_start;
530 printf_unfiltered (_("Command execution time: %ld.%06ld\n"),
531 cmd_time / 1000000, cmd_time % 1000000);
537 char *lim = (char *) sbrk (0);
538 long space_now = lim - lim_at_start;
539 long space_diff = space_now - space_at_cmd_start;
541 printf_unfiltered (_("Space used: %ld (%c%ld for this command)\n"),
543 (space_diff >= 0 ? '+' : '-'),
550 /* Read commands from `instream' and execute them until end of file or
551 error reading instream. This command loop doesnt care about any
552 such things as displaying time and space usage. If the user asks
553 for those, they won't work. */
555 simplified_command_loop (char *(*read_input_func) (char *),
556 void (*execute_command_func) (char *, int))
558 struct cleanup *old_chain;
560 int stdin_is_tty = ISATTY (stdin);
562 while (instream && !feof (instream))
565 if (instream == stdin && stdin_is_tty)
566 reinitialize_more_filter ();
567 old_chain = make_cleanup (null_cleanup, 0);
569 /* Get a command-line. */
570 command = (*read_input_func) (instream == stdin ?
571 get_prompt () : (char *) NULL);
576 (*execute_command_func) (command, instream == stdin);
578 /* Do any commands attached to breakpoint we stopped at. */
579 bpstat_do_actions (&stop_bpstat);
581 do_cleanups (old_chain);
585 /* Commands call this if they do not want to be repeated by null lines. */
593 /* If we aren't reading from standard input, we are saving the last
594 thing read from stdin in line and don't want to delete it. Null lines
595 won't repeat here in any case. */
596 if (instream == stdin)
600 /* Read a line from the stream "instream" without command line editing.
602 It prints PROMPT_ARG once at the start.
603 Action is compatible with "readline", e.g. space for the result is
604 malloc'd and should be freed by the caller.
606 A NULL return means end of file. */
608 gdb_readline (char *prompt_arg)
613 int result_size = 80;
617 /* Don't use a _filtered function here. It causes the assumed
618 character position to be off, since the newline we read from
619 the user is not accounted for. */
620 fputs_unfiltered (prompt_arg, gdb_stdout);
621 gdb_flush (gdb_stdout);
624 result = (char *) xmalloc (result_size);
628 /* Read from stdin if we are executing a user defined command.
629 This is the right thing for prompt_for_continue, at least. */
630 c = fgetc (instream ? instream : stdin);
635 /* The last line does not end with a newline. Return it, and
636 if we are called again fgetc will still return EOF and
637 we'll return NULL then. */
645 if (input_index > 0 && result[input_index - 1] == '\r')
650 result[input_index++] = c;
651 while (input_index >= result_size)
654 result = (char *) xrealloc (result, result_size);
658 result[input_index++] = '\0';
662 /* Variables which control command line editing and history
663 substitution. These variables are given default values at the end
665 static int command_editing_p;
667 /* NOTE 1999-04-29: This variable will be static again, once we modify
668 gdb to use the event loop as the default command loop and we merge
669 event-top.c into this file, top.c */
671 /* static */ int history_expansion_p;
673 static int write_history_p;
675 show_write_history_p (struct ui_file *file, int from_tty,
676 struct cmd_list_element *c, const char *value)
678 fprintf_filtered (file, _("Saving of the history record on exit is %s.\n"),
682 static int history_size;
684 show_history_size (struct ui_file *file, int from_tty,
685 struct cmd_list_element *c, const char *value)
687 fprintf_filtered (file, _("The size of the command history is %s.\n"),
691 static char *history_filename;
693 show_history_filename (struct ui_file *file, int from_tty,
694 struct cmd_list_element *c, const char *value)
696 fprintf_filtered (file, _("\
697 The filename in which to record the command history is \"%s\".\n"),
701 /* This is like readline(), but it has some gdb-specific behavior.
702 gdb can use readline in both the synchronous and async modes during
703 a single gdb invocation. At the ordinary top-level prompt we might
704 be using the async readline. That means we can't use
705 rl_pre_input_hook, since it doesn't work properly in async mode.
706 However, for a secondary prompt (" >", such as occurs during a
707 `define'), gdb just calls readline() directly, running it in
708 synchronous mode. So for operate-and-get-next to work in this
709 situation, we have to switch the hooks around. That is what
710 gdb_readline_wrapper is for. */
712 gdb_readline_wrapper (char *prompt)
714 /* Set the hook that works in this case. */
715 if (after_char_processing_hook)
717 rl_pre_input_hook = (Function *) after_char_processing_hook;
718 after_char_processing_hook = NULL;
721 return readline (prompt);
729 #if STOP_SIGNAL == SIGTSTP
730 signal (SIGTSTP, SIG_DFL);
736 sigprocmask (SIG_SETMASK, &zero, 0);
738 #elif HAVE_SIGSETMASK
741 kill (getpid (), SIGTSTP);
742 signal (SIGTSTP, stop_sig);
744 signal (STOP_SIGNAL, stop_sig);
746 printf_unfiltered ("%s", get_prompt ());
747 gdb_flush (gdb_stdout);
749 /* Forget about any previous command -- null line now will do nothing. */
752 #endif /* STOP_SIGNAL */
754 /* Initialize signal handlers. */
756 float_handler (int signo)
758 /* This message is based on ANSI C, section 4.7. Note that integer
759 divide by zero causes this, so "float" is a misnomer. */
760 signal (SIGFPE, float_handler);
761 error (_("Erroneous arithmetic operation."));
765 do_nothing (int signo)
767 /* Under System V the default disposition of a signal is reinstated after
768 the signal is caught and delivered to an application process. On such
769 systems one must restore the replacement signal handler if one wishes
770 to continue handling the signal in one's program. On BSD systems this
771 is not needed but it is harmless, and it simplifies the code to just do
772 it unconditionally. */
773 signal (signo, do_nothing);
776 /* The current saved history number from operate-and-get-next.
777 This is -1 if not valid. */
778 static int operate_saved_history = -1;
780 /* This is put on the appropriate hook and helps operate-and-get-next
783 gdb_rl_operate_and_get_next_completion (void)
785 int delta = where_history () - operate_saved_history;
786 /* The `key' argument to rl_get_previous_history is ignored. */
787 rl_get_previous_history (delta, 0);
788 operate_saved_history = -1;
790 /* readline doesn't automatically update the display for us. */
793 after_char_processing_hook = NULL;
794 rl_pre_input_hook = NULL;
797 /* This is a gdb-local readline command handler. It accepts the
798 current command line (like RET does) and, if this command was taken
799 from the history, arranges for the next command in the history to
800 appear on the command line when the prompt returns.
801 We ignore the arguments. */
803 gdb_rl_operate_and_get_next (int count, int key)
807 /* Use the async hook. */
808 after_char_processing_hook = gdb_rl_operate_and_get_next_completion;
810 /* Find the current line, and find the next line to use. */
811 where = where_history();
813 /* FIXME: kettenis/20020817: max_input_history is renamed into
814 history_max_entries in readline-4.2. When we do a new readline
815 import, we should probably change it here too, even though
816 readline maintains backwards compatibility for now by still
817 defining max_input_history. */
818 if ((history_is_stifled () && (history_length >= max_input_history)) ||
819 (where >= history_length - 1))
820 operate_saved_history = where;
822 operate_saved_history = where + 1;
824 return rl_newline (1, key);
827 /* Read one line from the command input stream `instream'
828 into the local static buffer `linebuffer' (whose current length
830 The buffer is made bigger as necessary.
831 Returns the address of the start of the line.
833 NULL is returned for end of file.
835 *If* the instream == stdin & stdin is a terminal, the line read
836 is copied into the file line saver (global var char *line,
837 length linesize) so that it can be duplicated.
839 This routine either uses fancy command line editing or
840 simple input as the user has requested. */
843 command_line_input (char *prompt_arg, int repeat, char *annotation_suffix)
845 static char *linebuffer = 0;
846 static unsigned linelength = 0;
850 char *local_prompt = prompt_arg;
854 /* The annotation suffix must be non-NULL. */
855 if (annotation_suffix == NULL)
856 annotation_suffix = "";
858 if (annotation_level > 1 && instream == stdin)
860 local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg))
861 + strlen (annotation_suffix) + 40);
862 if (prompt_arg == NULL)
863 local_prompt[0] = '\0';
865 strcpy (local_prompt, prompt_arg);
866 strcat (local_prompt, "\n\032\032");
867 strcat (local_prompt, annotation_suffix);
868 strcat (local_prompt, "\n");
874 linebuffer = (char *) xmalloc (linelength);
879 /* Control-C quits instantly if typed while in this loop
880 since it should not wait until the user types a newline. */
884 signal (STOP_SIGNAL, handle_stop_sig);
889 /* Make sure that all output has been output. Some machines may let
890 you get away with leaving out some of the gdb_flush, but not all. */
892 gdb_flush (gdb_stdout);
893 gdb_flush (gdb_stderr);
895 if (source_file_name != NULL)
896 ++source_line_number;
898 if (annotation_level > 1 && instream == stdin)
900 puts_unfiltered ("\n\032\032pre-");
901 puts_unfiltered (annotation_suffix);
902 puts_unfiltered ("\n");
905 /* Don't use fancy stuff if not talking to stdin. */
906 if (deprecated_readline_hook && instream == NULL)
908 rl = (*deprecated_readline_hook) (local_prompt);
910 else if (command_editing_p && instream == stdin && ISATTY (instream))
912 rl = gdb_readline_wrapper (local_prompt);
916 rl = gdb_readline (local_prompt);
919 if (annotation_level > 1 && instream == stdin)
921 puts_unfiltered ("\n\032\032post-");
922 puts_unfiltered (annotation_suffix);
923 puts_unfiltered ("\n");
926 if (!rl || rl == (char *) EOF)
931 if (strlen (rl) + 1 + (p - linebuffer) > linelength)
933 linelength = strlen (rl) + 1 + (p - linebuffer);
934 nline = (char *) xrealloc (linebuffer, linelength);
935 p += nline - linebuffer;
939 /* Copy line. Don't copy null at end. (Leaves line alone
940 if this was just a newline) */
944 xfree (rl); /* Allocated in readline. */
946 if (p == linebuffer || *(p - 1) != '\\')
949 p--; /* Put on top of '\'. */
950 local_prompt = (char *) 0;
955 signal (STOP_SIGNAL, SIG_DFL);
962 #define SERVER_COMMAND_LENGTH 7
964 (p - linebuffer > SERVER_COMMAND_LENGTH)
965 && strncmp (linebuffer, "server ", SERVER_COMMAND_LENGTH) == 0;
968 /* Note that we don't set `line'. Between this and the check in
969 dont_repeat, this insures that repeating will still do the
972 return linebuffer + SERVER_COMMAND_LENGTH;
975 /* Do history expansion if that is wished. */
976 if (history_expansion_p && instream == stdin
977 && ISATTY (instream))
982 *p = '\0'; /* Insert null now. */
983 expanded = history_expand (linebuffer, &history_value);
986 /* Print the changes. */
987 printf_unfiltered ("%s\n", history_value);
989 /* If there was an error, call this function again. */
992 xfree (history_value);
993 return command_line_input (prompt_arg, repeat, annotation_suffix);
995 if (strlen (history_value) > linelength)
997 linelength = strlen (history_value) + 1;
998 linebuffer = (char *) xrealloc (linebuffer, linelength);
1000 strcpy (linebuffer, history_value);
1001 p = linebuffer + strlen (linebuffer);
1002 xfree (history_value);
1006 /* If we just got an empty line, and that is supposed
1007 to repeat the previous command, return the value in the
1009 if (repeat && p == linebuffer)
1011 for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
1017 /* Add line to history if appropriate. */
1018 if (instream == stdin
1019 && ISATTY (stdin) && *linebuffer)
1020 add_history (linebuffer);
1022 /* Note: lines consisting solely of comments are added to the command
1023 history. This is useful when you type a command, and then
1024 realize you don't want to execute it quite yet. You can comment
1025 out the command and then later fetch it from the value history
1026 and remove the '#'. The kill ring is probably better, but some
1027 people are in the habit of commenting things out. */
1029 *p1 = '\0'; /* Found a comment. */
1031 /* Save into global buffer if appropriate. */
1034 if (linelength > linesize)
1036 line = xrealloc (line, linelength);
1037 linesize = linelength;
1039 strcpy (line, linebuffer);
1046 /* Print the GDB banner. */
1048 print_gdb_version (struct ui_file *stream)
1050 /* From GNU coding standards, first line is meant to be easy for a
1051 program to parse, and is just canonical program name and version
1052 number, which starts after last space. */
1054 fprintf_filtered (stream, "GNU gdb %s\n", version);
1056 /* Second line is a copyright notice. */
1058 fprintf_filtered (stream, "Copyright 2004 Free Software Foundation, Inc.\n");
1060 /* Following the copyright is a brief statement that the program is
1061 free software, that users are free to copy and change it on
1062 certain conditions, that it is covered by the GNU GPL, and that
1063 there is no warranty. */
1065 fprintf_filtered (stream, "\
1066 GDB is free software, covered by the GNU General Public License, and you are\n\
1067 welcome to change it and/or distribute copies of it under certain conditions.\n\
1068 Type \"show copying\" to see the conditions.\n\
1069 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
1071 /* After the required info we print the configuration information. */
1073 fprintf_filtered (stream, "This GDB was configured as \"");
1074 if (strcmp (host_name, target_name) != 0)
1076 fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
1080 fprintf_filtered (stream, "%s", host_name);
1082 fprintf_filtered (stream, "\".");
1085 /* get_prompt: access method for the GDB prompt string. */
1094 set_prompt (char *s)
1096 /* ??rehrauer: I don't know why this fails, since it looks as though
1097 assignments to prompt are wrapped in calls to savestring...
1101 PROMPT (0) = savestring (s, strlen (s));
1105 /* If necessary, make the user confirm that we should quit. Return
1106 non-zero if we should quit, zero if we shouldn't. */
1111 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
1115 /* This is something of a hack. But there's no reliable way to
1116 see if a GUI is running. The `use_windows' variable doesn't
1118 if (deprecated_init_ui_hook)
1119 s = "A debugging session is active.\nDo you still want to close the debugger?";
1120 else if (attach_flag)
1121 s = "The program is running. Quit anyway (and detach it)? ";
1123 s = "The program is running. Exit anyway? ";
1125 if (!query ("%s", s))
1132 /* Helper routine for quit_force that requires error handling. */
1141 quit_target (void *arg)
1143 struct qt_args *qt = (struct qt_args *)arg;
1145 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
1148 target_detach (qt->args, qt->from_tty);
1153 /* UDI wants this, to kill the TIP. */
1154 target_close (¤t_target, 1);
1156 /* Save the history information if it is appropriate to do so. */
1157 if (write_history_p && history_filename)
1158 write_history (history_filename);
1160 do_final_cleanups (ALL_CLEANUPS); /* Do any final cleanups before exiting */
1165 /* Quit without asking for confirmation. */
1168 quit_force (char *args, int from_tty)
1173 /* An optional expression may be used to cause gdb to terminate with the
1174 value of that expression. */
1177 struct value *val = parse_and_eval (args);
1179 exit_code = (int) value_as_long (val);
1183 qt.from_tty = from_tty;
1185 /* We want to handle any quit errors and exit regardless. */
1186 catch_errors (quit_target, &qt,
1187 "Quitting: ", RETURN_MASK_ALL);
1192 /* Returns whether GDB is running on a terminal and whether the user
1193 desires that questions be asked of them on that terminal. */
1196 input_from_terminal_p (void)
1198 return gdb_has_a_terminal () && (instream == stdin) & caution;
1202 dont_repeat_command (char *ignored, int from_tty)
1204 *line = 0; /* Can't call dont_repeat here because we're not
1205 necessarily reading from stdin. */
1208 /* Functions to manipulate command line editing control variables. */
1210 /* Number of commands to print in each call to show_commands. */
1211 #define Hist_print 10
1213 show_commands (char *args, int from_tty)
1215 /* Index for history commands. Relative to history_base. */
1218 /* Number of the history entry which we are planning to display next.
1219 Relative to history_base. */
1222 /* The first command in the history which doesn't exist (i.e. one more
1223 than the number of the last command). Relative to history_base. */
1226 /* Print out some of the commands from the command history. */
1227 /* First determine the length of the history list. */
1228 hist_len = history_size;
1229 for (offset = 0; offset < history_size; offset++)
1231 if (!history_get (history_base + offset))
1240 if (args[0] == '+' && args[1] == '\0')
1241 /* "info editing +" should print from the stored position. */
1244 /* "info editing <exp>" should print around command number <exp>. */
1245 num = (parse_and_eval_long (args) - history_base) - Hist_print / 2;
1247 /* "show commands" means print the last Hist_print commands. */
1250 num = hist_len - Hist_print;
1256 /* If there are at least Hist_print commands, we want to display the last
1257 Hist_print rather than, say, the last 6. */
1258 if (hist_len - num < Hist_print)
1260 num = hist_len - Hist_print;
1265 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
1267 printf_filtered ("%5d %s\n", history_base + offset,
1268 (history_get (history_base + offset))->line);
1271 /* The next command we want to display is the next one that we haven't
1275 /* If the user repeats this command with return, it should do what
1276 "show commands +" does. This is unnecessary if arg is null,
1277 because "show commands +" is not useful after "show commands". */
1278 if (from_tty && args)
1285 /* Called by do_setshow_command. */
1287 set_history_size_command (char *args, int from_tty, struct cmd_list_element *c)
1289 if (history_size == INT_MAX)
1290 unstifle_history ();
1291 else if (history_size >= 0)
1292 stifle_history (history_size);
1295 history_size = INT_MAX;
1296 error (_("History size must be non-negative"));
1301 set_history (char *args, int from_tty)
1303 printf_unfiltered (_("\"set history\" must be followed by the name of a history subcommand.\n"));
1304 help_list (sethistlist, "set history ", -1, gdb_stdout);
1308 show_history (char *args, int from_tty)
1310 cmd_show_list (showhistlist, from_tty, "");
1313 int info_verbose = 0; /* Default verbose msgs off */
1315 /* Called by do_setshow_command. An elaborate joke. */
1317 set_verbose (char *args, int from_tty, struct cmd_list_element *c)
1319 char *cmdname = "verbose";
1320 struct cmd_list_element *showcmd;
1322 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
1326 c->doc = "Set verbose printing of informational messages.";
1327 showcmd->doc = "Show verbose printing of informational messages.";
1331 c->doc = "Set verbosity.";
1332 showcmd->doc = "Show verbosity.";
1336 /* Init the history buffer. Note that we are called after the init file(s)
1337 * have been read so that the user can change the history file via his
1338 * .gdbinit file (for instance). The GDBHISTFILE environment variable
1339 * overrides all of this.
1347 tmpenv = getenv ("HISTSIZE");
1349 history_size = atoi (tmpenv);
1350 else if (!history_size)
1353 stifle_history (history_size);
1355 tmpenv = getenv ("GDBHISTFILE");
1357 history_filename = savestring (tmpenv, strlen (tmpenv));
1358 else if (!history_filename)
1360 /* We include the current directory so that if the user changes
1361 directories the file written will be the same as the one
1364 /* No leading dots in file names are allowed on MSDOS. */
1365 history_filename = concat (current_directory, "/_gdb_history", NULL);
1367 history_filename = concat (current_directory, "/.gdb_history", NULL);
1370 read_history (history_filename);
1374 show_new_async_prompt (struct ui_file *file, int from_tty,
1375 struct cmd_list_element *c, const char *value)
1377 fprintf_filtered (file, _("Gdb's prompt is \"%s\".\n"), value);
1381 show_async_command_editing_p (struct ui_file *file, int from_tty,
1382 struct cmd_list_element *c, const char *value)
1384 fprintf_filtered (file, _("\
1385 Editing of command lines as they are typed is %s.\n"),
1390 show_annotation_level (struct ui_file *file, int from_tty,
1391 struct cmd_list_element *c, const char *value)
1393 fprintf_filtered (file, _("Annotation_level is %s.\n"), value);
1397 show_exec_done_display_p (struct ui_file *file, int from_tty,
1398 struct cmd_list_element *c, const char *value)
1400 fprintf_filtered (file, _("\
1401 Notification of completion for asynchronous execution commands is %s.\n"),
1407 struct cmd_list_element *c;
1409 /* initialize the prompt stack to a simple "(gdb) " prompt or to
1410 whatever the DEFAULT_PROMPT is. */
1411 the_prompts.top = 0;
1413 PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
1415 /* Set things up for annotation_level > 1, if the user ever decides
1417 async_annotation_suffix = "prompt";
1418 /* Set the variable associated with the setshow prompt command. */
1419 new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0)));
1421 /* If gdb was started with --annotate=2, this is equivalent to the
1422 user entering the command 'set annotate 2' at the gdb prompt, so
1423 we need to do extra processing. */
1424 if (annotation_level > 1)
1425 set_async_annotation_level (NULL, 0, NULL);
1427 /* Set the important stuff up for command editing. */
1428 command_editing_p = 1;
1429 history_expansion_p = 0;
1430 write_history_p = 0;
1432 /* Setup important stuff for command line editing. */
1433 rl_completion_entry_function = readline_line_completion_function;
1434 rl_completer_word_break_characters = default_word_break_characters ();
1435 rl_completer_quote_characters = get_gdb_completer_quote_characters ();
1436 rl_readline_name = "gdb";
1437 rl_terminal_name = getenv ("TERM");
1439 /* The name for this defun comes from Bash, where it originated.
1440 15 is Control-o, the same binding this function has in Bash. */
1441 rl_add_defun ("operate-and-get-next", gdb_rl_operate_and_get_next, 15);
1443 add_setshow_string_cmd ("prompt", class_support,
1444 &new_async_prompt, _("\
1445 Set gdb's prompt"), _("\
1446 Show gdb's prompt"), NULL,
1448 show_new_async_prompt,
1449 &setlist, &showlist);
1451 add_com ("dont-repeat", class_support, dont_repeat_command, _("\
1452 Don't repeat this command.\n\
1453 Primarily used inside of user-defined commands that should not be repeated when\n\
1456 add_setshow_boolean_cmd ("editing", class_support,
1457 &async_command_editing_p, _("\
1458 Set editing of command lines as they are typed."), _("\
1459 Show editing of command lines as they are typed."), _("\
1460 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
1461 Without an argument, command line editing is enabled. To edit, use\n\
1462 EMACS-like or VI-like commands like control-P or ESC."),
1463 set_async_editing_command,
1464 show_async_command_editing_p,
1465 &setlist, &showlist);
1467 add_setshow_boolean_cmd ("save", no_class, &write_history_p, _("\
1468 Set saving of the history record on exit."), _("\
1469 Show saving of the history record on exit."), _("\
1470 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
1471 Without an argument, saving is enabled."),
1473 show_write_history_p,
1474 &sethistlist, &showhistlist);
1476 add_setshow_integer_cmd ("size", no_class, &history_size, _("\
1477 Set the size of the command history,"), _("\
1478 Show the size of the command history,"), _("\
1479 ie. the number of previous commands to keep a record of."),
1480 set_history_size_command,
1482 &sethistlist, &showhistlist);
1484 add_setshow_filename_cmd ("filename", no_class, &history_filename, _("\
1485 Set the filename in which to record the command history"), _("\
1486 Show the filename in which to record the command history"), _("\
1487 (the list of previous commands of which a record is kept)."),
1489 show_history_filename,
1490 &sethistlist, &showhistlist);
1492 add_setshow_boolean_cmd ("confirm", class_support, &caution, _("\
1493 Set whether to confirm potentially dangerous operations."), _("\
1494 Show whether to confirm potentially dangerous operations."), NULL,
1497 &setlist, &showlist);
1499 add_setshow_zinteger_cmd ("annotate", class_obscure, &annotation_level, _("\
1500 Set annotation_level."), _("\
1501 Show annotation_level."), _("\
1502 0 == normal; 1 == fullname (for use when running under emacs)\n\
1503 2 == output annotated suitably for use by programs that control GDB."),
1504 set_async_annotation_level,
1505 show_annotation_level,
1506 &setlist, &showlist);
1508 add_setshow_boolean_cmd ("exec-done-display", class_support,
1509 &exec_done_display_p, _("\
1510 Set notification of completion for asynchronous execution commands."), _("\
1511 Show notification of completion for asynchronous execution commands."), _("\
1512 Use \"on\" to enable the notification, and \"off\" to disable it."),
1514 show_exec_done_display_p,
1515 &setlist, &showlist);
1519 gdb_init (char *argv0)
1521 if (pre_init_ui_hook)
1522 pre_init_ui_hook ();
1524 /* Run the init function of each source file */
1526 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
1527 current_directory = gdb_dirbuf;
1530 /* Make sure we return to the original directory upon exit, come
1531 what may, since the OS doesn't do that for us. */
1532 make_final_cleanup (do_chdir_cleanup, xstrdup (current_directory));
1535 init_cmd_lists (); /* This needs to be done first */
1536 initialize_targets (); /* Setup target_terminal macros for utils.c */
1537 initialize_utils (); /* Make errors and warnings possible */
1538 initialize_all_files ();
1539 initialize_current_architecture ();
1541 init_main (); /* But that omits this file! Do it now */
1543 async_init_signals ();
1545 /* We need a default language for parsing expressions, so simple things like
1546 "set width 0" won't fail if no language is explicitly set in a config file
1547 or implicitly set by reading an executable during startup. */
1548 set_language (language_c);
1549 expected_language = current_language; /* don't warn about the change. */
1551 /* Allow another UI to initialize. If the UI fails to initialize,
1552 and it wants GDB to revert to the CLI, it should clear
1553 deprecated_init_ui_hook. */
1554 if (deprecated_init_ui_hook)
1555 deprecated_init_ui_hook (argv0);