1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986-2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "call-cmds.h"
28 #include "breakpoint.h"
30 #include "expression.h"
33 #include "terminal.h" /* For job_control. */
38 /* readline include files */
39 #include <readline/readline.h>
40 #include <readline/history.h>
42 /* readline defines this. */
45 #include <sys/types.h>
49 #include "event-top.h"
50 #include "gdb_string.h"
58 /* Prototypes for local functions */
60 static void dont_repeat_command
PARAMS ((char *, int));
62 static void source_cleanup_lines
PARAMS ((PTR
));
64 static void user_defined_command
PARAMS ((char *, int));
66 static void init_signals
PARAMS ((void));
69 static void stop_sig
PARAMS ((int));
72 static char *line_completion_function
PARAMS ((char *, int, char *, int));
74 static char *readline_line_completion_function
PARAMS ((char *, int));
76 /* NOTE 1999-04-29: this function will be static again, after we make the
77 event loop be the default command loop for gdb, and we merge
78 event-top.c into this file, top.c */
79 /* static */ void command_loop_marker (void *);
81 static void while_command
PARAMS ((char *, int));
83 static void if_command
PARAMS ((char *, int));
85 static struct command_line
*
86 build_command_line
PARAMS ((enum command_control_type
, char *));
88 static struct command_line
*
89 get_command_line
PARAMS ((enum command_control_type
, char *));
91 static void realloc_body_list
PARAMS ((struct command_line
*, int));
93 static enum misc_command_type read_next_line
PARAMS ((struct command_line
**));
95 static enum command_control_type
96 recurse_read_control_structure
PARAMS ((struct command_line
*));
98 static struct cleanup
*setup_user_args
PARAMS ((char *));
100 static char *locate_arg
PARAMS ((char *));
102 static char *insert_args
PARAMS ((char *));
104 static void arg_cleanup (void *);
106 static void init_main
PARAMS ((void));
108 static void init_cmd_lists
PARAMS ((void));
110 static void float_handler
PARAMS ((int));
112 static void init_signals
PARAMS ((void));
114 static void set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
116 static void show_history
PARAMS ((char *, int));
118 static void set_history
PARAMS ((char *, int));
120 static void set_history_size_command
PARAMS ((char *, int,
121 struct cmd_list_element
*));
123 static void show_commands
PARAMS ((char *, int));
125 static void echo_command
PARAMS ((char *, int));
127 static void pwd_command
PARAMS ((char *, int));
129 static void show_version
PARAMS ((char *, int));
131 static void document_command
PARAMS ((char *, int));
133 static void define_command
PARAMS ((char *, int));
135 static void validate_comname
PARAMS ((char *));
137 static void help_command
PARAMS ((char *, int));
139 static void show_command
PARAMS ((char *, int));
141 static void info_command
PARAMS ((char *, int));
143 static void complete_command
PARAMS ((char *, int));
145 static void do_nothing
PARAMS ((int));
147 static void show_debug
PARAMS ((char *, int));
149 static void set_debug
PARAMS ((char *, int));
152 /* NOTE 1999-04-29: 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 do_restore_instream_cleanup (void *stream
);
162 static struct cleanup
*make_cleanup_free_command_lines (struct command_line
**);
164 /* Default command line prompt. This is overriden in some configs. */
166 #ifndef DEFAULT_PROMPT
167 #define DEFAULT_PROMPT "(gdb) "
170 /* Initialization file name for gdb. This is overridden in some configs. */
172 #ifndef GDBINIT_FILENAME
173 #define GDBINIT_FILENAME ".gdbinit"
175 char gdbinit
[] = GDBINIT_FILENAME
;
177 int inhibit_gdbinit
= 0;
179 /* If nonzero, and GDB has been configured to be able to use windows,
180 attempt to open them upon startup. */
184 extern char lang_frame_mismatch_warn
[]; /* language.c */
186 /* Flag for whether we want all the "from_tty" gubbish printed. */
188 int caution
= 1; /* Default is yes, sigh. */
190 /* Define all cmd_list_elements. */
192 /* Chain containing all defined commands. */
194 struct cmd_list_element
*cmdlist
;
196 /* Chain containing all defined info subcommands. */
198 struct cmd_list_element
*infolist
;
200 /* Chain containing all defined enable subcommands. */
202 struct cmd_list_element
*enablelist
;
204 /* Chain containing all defined disable subcommands. */
206 struct cmd_list_element
*disablelist
;
208 /* Chain containing all defined toggle subcommands. */
210 struct cmd_list_element
*togglelist
;
212 /* Chain containing all defined stop subcommands. */
214 struct cmd_list_element
*stoplist
;
216 /* Chain containing all defined delete subcommands. */
218 struct cmd_list_element
*deletelist
;
220 /* Chain containing all defined "enable breakpoint" subcommands. */
222 struct cmd_list_element
*enablebreaklist
;
224 /* Chain containing all defined set subcommands */
226 struct cmd_list_element
*setlist
;
228 /* Chain containing all defined unset subcommands */
230 struct cmd_list_element
*unsetlist
;
232 /* Chain containing all defined show subcommands. */
234 struct cmd_list_element
*showlist
;
236 /* Chain containing all defined \"set history\". */
238 struct cmd_list_element
*sethistlist
;
240 /* Chain containing all defined \"show history\". */
242 struct cmd_list_element
*showhistlist
;
244 /* Chain containing all defined \"unset history\". */
246 struct cmd_list_element
*unsethistlist
;
248 /* Chain containing all defined maintenance subcommands. */
250 struct cmd_list_element
*maintenancelist
;
252 /* Chain containing all defined "maintenance info" subcommands. */
254 struct cmd_list_element
*maintenanceinfolist
;
256 /* Chain containing all defined "maintenance print" subcommands. */
258 struct cmd_list_element
*maintenanceprintlist
;
260 struct cmd_list_element
*setprintlist
;
262 struct cmd_list_element
*showprintlist
;
264 struct cmd_list_element
*setdebuglist
;
266 struct cmd_list_element
*showdebuglist
;
268 struct cmd_list_element
*setchecklist
;
270 struct cmd_list_element
*showchecklist
;
272 /* stdio stream that command input is being read from. Set to stdin normally.
273 Set by source_command to the file we are sourcing. Set to NULL if we are
274 executing a user-defined command or interacting via a GUI. */
278 /* Current working directory. */
280 char *current_directory
;
282 /* The directory name is actually stored here (usually). */
283 char gdb_dirbuf
[1024];
285 /* Function to call before reading a command, if nonzero.
286 The function receives two args: an input stream,
287 and a prompt string. */
289 void (*window_hook
) PARAMS ((FILE *, char *));
294 /* gdb prints this when reading a command interactively */
295 static char *gdb_prompt_string
; /* the global prompt string */
296 extern char *get_prompt
PARAMS ((void)); /* access function for prompt string */
298 /* Buffer used for reading command lines, and the size
299 allocated for it so far. */
304 /* Nonzero if the current command is modified by "server ". This
305 affects things like recording into the command history, commands
306 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
307 whatever) can issue its own commands and also send along commands
308 from the user, and have the user not notice that the user interface
309 is issuing commands too. */
312 /* Baud rate specified for talking to serial target systems. Default
313 is left as -1, so targets can choose their own defaults. */
314 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
315 or (unsigned int)-1. This is a Bad User Interface. */
319 /* Timeout limit for response from target. */
321 /* The default value has been changed many times over the years. It
322 was originally 5 seconds. But that was thought to be a long time
323 to sit and wait, so it was changed to 2 seconds. That was thought
324 to be plenty unless the connection was going through some terminal
325 server or multiplexer or other form of hairy serial connection.
327 In mid-1996, remote_timeout was moved from remote.c to top.c and
328 it began being used in other remote-* targets. It appears that the
329 default was changed to 20 seconds at that time, perhaps because the
330 Hitachi E7000 ICE didn't always respond in a timely manner.
332 But if 5 seconds is a long time to sit and wait for retransmissions,
333 20 seconds is far worse. This demonstrates the difficulty of using
334 a single variable for all protocol timeouts.
336 As remote.c is used much more than remote-e7000.c, it was changed
337 back to 2 seconds in 1999. */
339 int remote_timeout
= 2;
341 /* Non-zero tells remote* modules to output debugging info. */
343 int remote_debug
= 0;
345 /* Non-zero means the target is running. Note: this is different from
346 saying that there is an active target and we are stopped at a
347 breakpoint, for instance. This is a real indicator whether the
348 target is off and running, which gdb is doing something else. */
349 int target_executing
= 0;
351 /* Level of control structure. */
352 static int control_level
;
354 /* Structure for arguments to user defined functions. */
355 #define MAXUSERARGS 10
358 struct user_args
*next
;
369 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
373 #define STOP_SIGNAL SIGTSTP
374 static void stop_sig
PARAMS ((int));
378 /* Some System V have job control but not sigsetmask(). */
379 #if !defined (HAVE_SIGSETMASK)
381 #define HAVE_SIGSETMASK 1
383 #define HAVE_SIGSETMASK 0
387 #if 0 == (HAVE_SIGSETMASK)
388 #define sigsetmask(n)
391 /* Hooks for alternate command interfaces. */
393 /* Called after most modules have been initialized, but before taking users
396 void (*init_ui_hook
) PARAMS ((char *argv0
));
398 /* This hook is called from within gdb's many mini-event loops which could
399 steal control from a real user interface's event loop. It returns
400 non-zero if the user is requesting a detach, zero otherwise. */
402 int (*ui_loop_hook
) PARAMS ((int));
404 /* Called instead of command_loop at top level. Can be invoked via
405 return_to_top_level. */
407 void (*command_loop_hook
) PARAMS ((void));
410 /* Called from print_frame_info to list the line we stopped in. */
412 void (*print_frame_info_listing_hook
) PARAMS ((struct symtab
* s
, int line
,
413 int stopline
, int noerror
));
414 /* Replaces most of query. */
416 int (*query_hook
) PARAMS ((const char *, va_list));
418 /* Replaces most of warning. */
420 void (*warning_hook
) PARAMS ((const char *, va_list));
422 /* These three functions support getting lines of text from the user. They
423 are used in sequence. First readline_begin_hook is called with a text
424 string that might be (for example) a message for the user to type in a
425 sequence of commands to be executed at a breakpoint. If this function
426 calls back to a GUI, it might take this opportunity to pop up a text
427 interaction window with this message. Next, readline_hook is called
428 with a prompt that is emitted prior to collecting the user input.
429 It can be called multiple times. Finally, readline_end_hook is called
430 to notify the GUI that we are done with the interaction window and it
433 void (*readline_begin_hook
) PARAMS ((char *,...));
434 char *(*readline_hook
) PARAMS ((char *));
435 void (*readline_end_hook
) PARAMS ((void));
437 /* Called as appropriate to notify the interface of the specified breakpoint
440 void (*create_breakpoint_hook
) PARAMS ((struct breakpoint
* bpt
));
441 void (*delete_breakpoint_hook
) PARAMS ((struct breakpoint
* bpt
));
442 void (*modify_breakpoint_hook
) PARAMS ((struct breakpoint
* bpt
));
444 /* Called as appropriate to notify the interface that we have attached
445 to or detached from an already running process. */
447 void (*attach_hook
) PARAMS ((void));
448 void (*detach_hook
) PARAMS ((void));
450 /* Called during long calculations to allow GUI to repair window damage, and to
451 check for stop buttons, etc... */
453 void (*interactive_hook
) PARAMS ((void));
455 /* Called when the registers have changed, as a hint to a GUI
456 to minimize window update. */
458 void (*registers_changed_hook
) PARAMS ((void));
460 /* Tell the GUI someone changed the register REGNO. -1 means
461 that the caller does not know which register changed or
462 that several registers have changed (see value_assign). */
463 void (*register_changed_hook
) PARAMS ((int regno
));
465 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
466 void (*memory_changed_hook
) PARAMS ((CORE_ADDR addr
, int len
));
468 /* Called when going to wait for the target. Usually allows the GUI to run
469 while waiting for target events. */
471 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
* status
));
473 /* Used by UI as a wrapper around command execution. May do various things
474 like enabling/disabling buttons, etc... */
476 void (*call_command_hook
) PARAMS ((struct cmd_list_element
* c
, char *cmd
,
479 /* Called after a `set' command has finished. Is only run if the
480 `set' command succeeded. */
482 void (*set_hook
) (struct cmd_list_element
* c
);
484 /* Called when the current thread changes. Argument is thread id. */
486 void (*context_hook
) PARAMS ((int id
));
488 /* Takes control from error (). Typically used to prevent longjmps out of the
489 middle of the GUI. Usually used in conjunction with a catch routine. */
491 NORETURN
void (*error_hook
) (void) ATTR_NORETURN
;
494 /* One should use catch_errors rather than manipulating these
496 #if defined(HAVE_SIGSETJMP)
497 #define SIGJMP_BUF sigjmp_buf
498 #define SIGSETJMP(buf) sigsetjmp(buf, 1)
499 #define SIGLONGJMP(buf,val) siglongjmp(buf,val)
501 #define SIGJMP_BUF jmp_buf
502 #define SIGSETJMP(buf) setjmp(buf)
503 #define SIGLONGJMP(buf,val) longjmp(buf,val)
506 /* Where to go for return_to_top_level. */
507 static SIGJMP_BUF
*catch_return
;
509 /* Return for reason REASON to the nearest containing catch_errors(). */
512 return_to_top_level (reason
)
513 enum return_reason reason
;
518 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
519 I can think of a reason why that is vital, though). */
520 bpstat_clear_actions (stop_bpstat
); /* Clear queued breakpoint commands */
522 disable_current_display ();
523 do_cleanups (ALL_CLEANUPS
);
524 if (event_loop_p
&& target_can_async_p () && !target_executing
)
525 do_exec_cleanups (ALL_CLEANUPS
);
526 if (event_loop_p
&& sync_execution
)
527 do_exec_error_cleanups (ALL_CLEANUPS
);
529 if (annotation_level
> 1)
540 /* Jump to the containing catch_errors() call, communicating REASON
541 to that call via setjmp's return value. Note that REASON can't
542 be zero, by definition in defs.h. */
544 (NORETURN
void) SIGLONGJMP (*catch_return
, (int) reason
);
547 /* Call FUNC with arg ARGS, catching any errors. If there is no
548 error, return the value returned by FUNC. If there is an error,
549 print ERRSTRING, print the specific error message, then return
552 Must not be called with immediate_quit in effect (bad things might
553 happen, say we got a signal in the middle of a memcpy to quit_return).
554 This is an OK restriction; with very few exceptions immediate_quit can
555 be replaced by judicious use of QUIT.
557 MASK specifies what to catch; it is normally set to
558 RETURN_MASK_ALL, if for no other reason than that the code which
559 calls catch_errors might not be set up to deal with a quit which
560 isn't caught. But if the code can deal with it, it generally
561 should be RETURN_MASK_ERROR, unless for some reason it is more
562 useful to abort only the portion of the operation inside the
563 catch_errors. Note that quit should return to the command line
564 fairly quickly, even if some further processing is being done. */
566 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
567 error() et.al. could maintain a set of flags that indicate the the
568 current state of each of the longjmp buffers. This would give the
569 longjmp code the chance to detect a longjmp botch (before it gets
570 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
571 code also randomly used a SET_TOP_LEVEL macro that directly
572 initialize the longjmp buffers. */
574 /* MAYBE: cagney/1999-11-05: Should the catch_erros and cleanups code
575 be consolidated into a single file instead of being distributed
576 between utils.c and top.c? */
579 catch_errors (func
, args
, errstring
, mask
)
580 catch_errors_ftype
*func
;
585 SIGJMP_BUF
*saved_catch
;
588 struct cleanup
*saved_cleanup_chain
;
589 char *saved_error_pre_print
;
590 char *saved_quit_pre_print
;
592 /* Return value from SIGSETJMP(): enum return_reason if error or
593 quit caught, 0 otherwise. */
596 /* Override error/quit messages during FUNC. */
598 saved_error_pre_print
= error_pre_print
;
599 saved_quit_pre_print
= quit_pre_print
;
601 if (mask
& RETURN_MASK_ERROR
)
602 error_pre_print
= errstring
;
603 if (mask
& RETURN_MASK_QUIT
)
604 quit_pre_print
= errstring
;
606 /* Prevent error/quit during FUNC from calling cleanups established
609 saved_cleanup_chain
= save_cleanups ();
611 /* Call FUNC, catching error/quit events. */
613 saved_catch
= catch_return
;
614 catch_return
= &catch;
615 caught
= SIGSETJMP (catch);
617 val
= (*func
) (args
);
618 catch_return
= saved_catch
;
620 /* FIXME: cagney/1999-11-05: A correct FUNC implementaton will
621 clean things up (restoring the cleanup chain) to the state they
622 were just prior to the call. Unfortunatly, many FUNC's are not
623 that well behaved. This could be fixed by adding either a
624 do_cleanups call (to cover the problem) or an assertion check to
625 detect bad FUNCs code. */
627 /* Restore the cleanup chain and error/quit messages to their
630 restore_cleanups (saved_cleanup_chain
);
632 if (mask
& RETURN_MASK_QUIT
)
633 quit_pre_print
= saved_quit_pre_print
;
634 if (mask
& RETURN_MASK_ERROR
)
635 error_pre_print
= saved_error_pre_print
;
637 /* Return normally if no error/quit event occurred. */
642 /* If the caller didn't request that the event be caught, relay the
643 event to the next containing catch_errors(). */
645 if (!(mask
& RETURN_MASK (caught
)))
646 return_to_top_level (caught
);
648 /* Tell the caller that an event was caught.
650 FIXME: nsd/2000-02-22: When MASK is RETURN_MASK_ALL, the caller
651 can't tell what type of event occurred.
653 A possible fix is to add a new interface, catch_event(), that
654 returns enum return_reason after catching an error or a quit.
656 When returning normally, i.e. without catching an error or a
657 quit, catch_event() could return RETURN_NORMAL, which would be
658 added to enum return_reason. FUNC would return information
659 exclusively via ARGS.
661 Alternatively, normal catch_event() could return FUNC's return
662 value. The caller would need to be aware of potential overlap
663 with enum return_reason, which could be publicly restricted to
664 negative values to simplify return value processing in FUNC and
670 struct captured_command_args
672 catch_command_errors_ftype
*command
;
678 do_captured_command (void *data
)
680 struct captured_command_args
*context
= data
;
681 context
->command (context
->arg
, context
->from_tty
);
682 /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
683 isn't needed. Instead an assertion check could be made that
684 simply confirmed that the called function correctly cleaned up
685 after its self. Unfortunatly, old code (prior to 1999-11-04) in
686 main.c was calling SET_TOP_LEVEL(), calling the command function,
687 and then *always* calling do_cleanups(). For the moment we
688 remain ``bug compatible'' with that old code.. */
689 do_cleanups (ALL_CLEANUPS
);
694 catch_command_errors (catch_command_errors_ftype
* command
,
695 char *arg
, int from_tty
, return_mask mask
)
697 struct captured_command_args args
;
698 args
.command
= command
;
700 args
.from_tty
= from_tty
;
701 return catch_errors (do_captured_command
, &args
, "", mask
);
705 /* Handler for SIGHUP. */
712 catch_errors (quit_cover
, NULL
,
713 "Could not kill the program being debugged", RETURN_MASK_ALL
);
714 signal (SIGHUP
, SIG_DFL
);
715 kill (getpid (), SIGHUP
);
718 /* Just a little helper function for disconnect(). */
720 /* NOTE 1999-04-29: This function will be static again, once we modify
721 gdb to use the event loop as the default command loop and we merge
722 event-top.c into this file, top.c */
727 caution
= 0; /* Throw caution to the wind -- we're exiting.
728 This prevents asking the user dumb questions. */
729 quit_command ((char *) 0, 0);
732 #endif /* defined SIGHUP */
734 /* Line number we are currently in in a file which is being sourced. */
735 /* NOTE 1999-04-29: This variable will be static again, once we modify
736 gdb to use the event loop as the default command loop and we merge
737 event-top.c into this file, top.c */
738 /* static */ int source_line_number
;
740 /* Name of the file we are sourcing. */
741 /* NOTE 1999-04-29: This variable will be static again, once we modify
742 gdb to use the event loop as the default command loop and we merge
743 event-top.c into this file, top.c */
744 /* static */ char *source_file_name
;
746 /* Buffer containing the error_pre_print used by the source stuff.
748 /* NOTE 1999-04-29: This variable will be static again, once we modify
749 gdb to use the event loop as the default command loop and we merge
750 event-top.c into this file, top.c */
751 /* static */ char *source_error
;
752 static int source_error_allocated
;
754 /* Something to glom on to the start of error_pre_print if source_file_name
756 /* NOTE 1999-04-29: This variable will be static again, once we modify
757 gdb to use the event loop as the default command loop and we merge
758 event-top.c into this file, top.c */
759 /* static */ char *source_pre_error
;
761 /* Clean up on error during a "source" command (or execution of a
762 user-defined command). */
765 do_restore_instream_cleanup (void *stream
)
767 /* Restore the previous input stream. */
771 /* Read commands from STREAM. */
773 read_command_file (stream
)
776 struct cleanup
*cleanups
;
778 cleanups
= make_cleanup (do_restore_instream_cleanup
, instream
);
781 do_cleanups (cleanups
);
784 extern void init_proc
PARAMS ((void));
786 void (*pre_init_ui_hook
) PARAMS ((void));
790 do_chdir_cleanup (void *old_dir
)
801 if (pre_init_ui_hook
)
804 /* Run the init function of each source file */
806 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
807 current_directory
= gdb_dirbuf
;
810 /* Make sure we return to the original directory upon exit, come
811 what may, since the OS doesn't do that for us. */
812 make_final_cleanup (do_chdir_cleanup
, xstrdup (current_directory
));
815 init_cmd_lists (); /* This needs to be done first */
816 initialize_targets (); /* Setup target_terminal macros for utils.c */
817 initialize_utils (); /* Make errors and warnings possible */
818 initialize_all_files ();
819 initialize_current_architecture ();
820 init_main (); /* But that omits this file! Do it now */
822 /* The signal handling mechanism is different depending whether or
823 not the async version is run. NOTE: in the future we plan to make
824 the event loop be the default engine of gdb, and this difference
827 async_init_signals ();
831 /* We need a default language for parsing expressions, so simple things like
832 "set width 0" won't fail if no language is explicitly set in a config file
833 or implicitly set by reading an executable during startup. */
834 set_language (language_c
);
835 expected_language
= current_language
; /* don't warn about the change. */
838 /* Install the default UI */
839 uiout
= cli_out_new (gdb_stdout
);
843 /* All the interpreters should have had a look at things by now.
844 Initialize the selected interpreter. */
845 if (interpreter_p
&& !init_ui_hook
)
847 fprintf_unfiltered (gdb_stderr
, "Interpreter `%s' unrecognized.\n",
854 init_ui_hook (argv0
);
857 /* Allocate, initialize a new command line structure for one of the
858 control commands (if/while). */
860 static struct command_line
*
861 build_command_line (type
, args
)
862 enum command_control_type type
;
865 struct command_line
*cmd
;
868 error ("if/while commands require arguments.\n");
870 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
872 cmd
->control_type
= type
;
876 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
878 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
879 cmd
->line
= savestring (args
, strlen (args
));
883 /* Build and return a new command structure for the control commands
884 such as "if" and "while". */
886 static struct command_line
*
887 get_command_line (type
, arg
)
888 enum command_control_type type
;
891 struct command_line
*cmd
;
892 struct cleanup
*old_chain
= NULL
;
894 /* Allocate and build a new command line structure. */
895 cmd
= build_command_line (type
, arg
);
897 old_chain
= make_cleanup_free_command_lines (&cmd
);
899 /* Read in the body of this command. */
900 if (recurse_read_control_structure (cmd
) == invalid_control
)
902 warning ("error reading in control structure\n");
903 do_cleanups (old_chain
);
907 discard_cleanups (old_chain
);
911 /* Recursively print a command (including full control structures). */
914 print_command_lines (uiout
, cmd
, depth
)
915 struct ui_out
*uiout
;
916 struct command_line
*cmd
;
919 struct command_line
*list
;
926 ui_out_spaces (uiout
, 2 * depth
);
928 /* A simple command, print it and continue. */
929 if (list
->control_type
== simple_control
)
931 ui_out_field_string (uiout
, NULL
, list
->line
);
932 ui_out_text (uiout
, "\n");
937 /* loop_continue to jump to the start of a while loop, print it
939 if (list
->control_type
== continue_control
)
941 ui_out_field_string (uiout
, NULL
, "loop_continue");
942 ui_out_text (uiout
, "\n");
947 /* loop_break to break out of a while loop, print it and continue. */
948 if (list
->control_type
== break_control
)
950 ui_out_field_string (uiout
, NULL
, "loop_break");
951 ui_out_text (uiout
, "\n");
956 /* A while command. Recursively print its subcommands and continue. */
957 if (list
->control_type
== while_control
)
959 ui_out_text (uiout
, "while ");
960 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
961 ui_out_text (uiout
, "\n");
962 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
963 ui_out_field_string (uiout
, NULL
, "end");
965 ui_out_spaces (uiout
, 2 * depth
);
966 ui_out_text (uiout
, "end\n");
971 /* An if command. Recursively print both arms before continueing. */
972 if (list
->control_type
== if_control
)
974 ui_out_text (uiout
, "if ");
975 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
976 ui_out_text (uiout
, "\n");
978 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
980 /* Show the false arm if it exists. */
981 if (list
->body_count
== 2)
984 ui_out_spaces (uiout
, 2 * depth
);
985 ui_out_field_string (uiout
, NULL
, "else");
986 ui_out_text (uiout
, "else\n");
987 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
990 ui_out_field_string (uiout
, NULL
, "end");
992 ui_out_spaces (uiout
, 2 * depth
);
993 ui_out_text (uiout
, "end\n");
998 /* ignore illegal command type and try next */
1000 } /* while (list) */
1004 print_command_line (cmd
, depth
, stream
)
1005 struct command_line
*cmd
;
1007 struct ui_file
*stream
;
1013 for (i
= 0; i
< depth
; i
++)
1014 fputs_filtered (" ", stream
);
1017 /* A simple command, print it and return. */
1018 if (cmd
->control_type
== simple_control
)
1020 fputs_filtered (cmd
->line
, stream
);
1021 fputs_filtered ("\n", stream
);
1025 /* loop_continue to jump to the start of a while loop, print it
1027 if (cmd
->control_type
== continue_control
)
1029 fputs_filtered ("loop_continue\n", stream
);
1033 /* loop_break to break out of a while loop, print it and return. */
1034 if (cmd
->control_type
== break_control
)
1036 fputs_filtered ("loop_break\n", stream
);
1040 /* A while command. Recursively print its subcommands before returning. */
1041 if (cmd
->control_type
== while_control
)
1043 struct command_line
*list
;
1044 fputs_filtered ("while ", stream
);
1045 fputs_filtered (cmd
->line
, stream
);
1046 fputs_filtered ("\n", stream
);
1047 list
= *cmd
->body_list
;
1050 print_command_line (list
, depth
+ 1, stream
);
1055 /* An if command. Recursively print both arms before returning. */
1056 if (cmd
->control_type
== if_control
)
1058 fputs_filtered ("if ", stream
);
1059 fputs_filtered (cmd
->line
, stream
);
1060 fputs_filtered ("\n", stream
);
1062 print_command_line (cmd
->body_list
[0], depth
+ 1, stream
);
1064 /* Show the false arm if it exists. */
1065 if (cmd
->body_count
== 2)
1069 for (i
= 0; i
< depth
; i
++)
1070 fputs_filtered (" ", stream
);
1072 fputs_filtered ("else\n", stream
);
1073 print_command_line (cmd
->body_list
[1], depth
+ 1, stream
);
1077 for (i
= 0; i
< depth
; i
++)
1078 fputs_filtered (" ", stream
);
1080 fputs_filtered ("end\n", stream
);
1085 /* Execute the command in CMD. */
1087 enum command_control_type
1088 execute_control_command (cmd
)
1089 struct command_line
*cmd
;
1091 struct expression
*expr
;
1092 struct command_line
*current
;
1093 struct cleanup
*old_chain
= 0;
1097 enum command_control_type ret
;
1100 switch (cmd
->control_type
)
1102 case simple_control
:
1103 /* A simple command, execute it and return. */
1104 new_line
= insert_args (cmd
->line
);
1106 return invalid_control
;
1107 old_chain
= make_cleanup (free_current_contents
, &new_line
);
1108 execute_command (new_line
, 0);
1109 ret
= cmd
->control_type
;
1112 case continue_control
:
1114 /* Return for "continue", and "break" so we can either
1115 continue the loop at the top, or break out. */
1116 ret
= cmd
->control_type
;
1121 /* Parse the loop control expression for the while statement. */
1122 new_line
= insert_args (cmd
->line
);
1124 return invalid_control
;
1125 old_chain
= make_cleanup (free_current_contents
, &new_line
);
1126 expr
= parse_expression (new_line
);
1127 make_cleanup (free_current_contents
, &expr
);
1129 ret
= simple_control
;
1132 /* Keep iterating so long as the expression is true. */
1139 /* Evaluate the expression. */
1140 val_mark
= value_mark ();
1141 val
= evaluate_expression (expr
);
1142 cond_result
= value_true (val
);
1143 value_free_to_mark (val_mark
);
1145 /* If the value is false, then break out of the loop. */
1149 /* Execute the body of the while statement. */
1150 current
= *cmd
->body_list
;
1153 ret
= execute_control_command (current
);
1155 /* If we got an error, or a "break" command, then stop
1157 if (ret
== invalid_control
|| ret
== break_control
)
1163 /* If we got a "continue" command, then restart the loop
1165 if (ret
== continue_control
)
1168 /* Get the next statement. */
1169 current
= current
->next
;
1173 /* Reset RET so that we don't recurse the break all the way down. */
1174 if (ret
== break_control
)
1175 ret
= simple_control
;
1182 new_line
= insert_args (cmd
->line
);
1184 return invalid_control
;
1185 old_chain
= make_cleanup (free_current_contents
, &new_line
);
1186 /* Parse the conditional for the if statement. */
1187 expr
= parse_expression (new_line
);
1188 make_cleanup (free_current_contents
, &expr
);
1191 ret
= simple_control
;
1193 /* Evaluate the conditional. */
1194 val_mark
= value_mark ();
1195 val
= evaluate_expression (expr
);
1197 /* Choose which arm to take commands from based on the value of the
1198 conditional expression. */
1199 if (value_true (val
))
1200 current
= *cmd
->body_list
;
1201 else if (cmd
->body_count
== 2)
1202 current
= *(cmd
->body_list
+ 1);
1203 value_free_to_mark (val_mark
);
1205 /* Execute commands in the given arm. */
1208 ret
= execute_control_command (current
);
1210 /* If we got an error, get out. */
1211 if (ret
!= simple_control
)
1214 /* Get the next statement in the body. */
1215 current
= current
->next
;
1222 warning ("Invalid control type in command structure.");
1223 return invalid_control
;
1227 do_cleanups (old_chain
);
1232 /* "while" command support. Executes a body of statements while the
1233 loop condition is nonzero. */
1236 while_command (arg
, from_tty
)
1240 struct command_line
*command
= NULL
;
1243 command
= get_command_line (while_control
, arg
);
1245 if (command
== NULL
)
1248 execute_control_command (command
);
1249 free_command_lines (&command
);
1252 /* "if" command support. Execute either the true or false arm depending
1253 on the value of the if conditional. */
1256 if_command (arg
, from_tty
)
1260 struct command_line
*command
= NULL
;
1263 command
= get_command_line (if_control
, arg
);
1265 if (command
== NULL
)
1268 execute_control_command (command
);
1269 free_command_lines (&command
);
1274 arg_cleanup (void *ignore
)
1276 struct user_args
*oargs
= user_args
;
1278 internal_error ("Internal error, arg_cleanup called with no user args.\n");
1280 user_args
= user_args
->next
;
1284 /* Bind the incomming arguments for a user defined command to
1285 $arg0, $arg1 ... $argMAXUSERARGS. */
1287 static struct cleanup
*
1291 struct user_args
*args
;
1292 struct cleanup
*old_chain
;
1293 unsigned int arg_count
= 0;
1295 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
1296 memset (args
, 0, sizeof (struct user_args
));
1298 args
->next
= user_args
;
1301 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
1313 if (arg_count
>= MAXUSERARGS
)
1315 error ("user defined function may only have %d arguments.\n",
1320 /* Strip whitespace. */
1321 while (*p
== ' ' || *p
== '\t')
1324 /* P now points to an argument. */
1326 user_args
->a
[arg_count
].arg
= p
;
1328 /* Get to the end of this argument. */
1331 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
1337 else if (*p
== '\\')
1360 user_args
->a
[arg_count
].len
= p
- start_arg
;
1367 /* Given character string P, return a point to the first argument ($arg),
1368 or NULL if P contains no arguments. */
1374 while ((p
= strchr (p
, '$')))
1376 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1383 /* Insert the user defined arguments stored in user_arg into the $arg
1384 arguments found in line, with the updated copy being placed into nline. */
1390 char *p
, *save_line
, *new_line
;
1393 /* First we need to know how much memory to allocate for the new line. */
1396 while ((p
= locate_arg (line
)))
1401 if (i
>= user_args
->count
)
1403 error ("Missing argument %d in user function.\n", i
);
1406 len
+= user_args
->a
[i
].len
;
1410 /* Don't forget the tail. */
1411 len
+= strlen (line
);
1413 /* Allocate space for the new line and fill it in. */
1414 new_line
= (char *) xmalloc (len
+ 1);
1415 if (new_line
== NULL
)
1418 /* Restore pointer to beginning of old line. */
1421 /* Save pointer to beginning of new line. */
1422 save_line
= new_line
;
1424 while ((p
= locate_arg (line
)))
1428 memcpy (new_line
, line
, p
- line
);
1429 new_line
+= p
- line
;
1432 len
= user_args
->a
[i
].len
;
1435 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1440 /* Don't forget the tail. */
1441 strcpy (new_line
, line
);
1443 /* Return a pointer to the beginning of the new line. */
1448 execute_user_command (c
, args
)
1449 struct cmd_list_element
*c
;
1452 register struct command_line
*cmdlines
;
1453 struct cleanup
*old_chain
;
1454 enum command_control_type ret
;
1456 old_chain
= setup_user_args (args
);
1458 cmdlines
= c
->user_commands
;
1463 /* Set the instream to 0, indicating execution of a
1464 user-defined function. */
1465 old_chain
= make_cleanup (do_restore_instream_cleanup
, instream
);
1466 instream
= (FILE *) 0;
1469 ret
= execute_control_command (cmdlines
);
1470 if (ret
!= simple_control
&& ret
!= break_control
)
1472 warning ("Error in control structure.\n");
1475 cmdlines
= cmdlines
->next
;
1477 do_cleanups (old_chain
);
1480 /* Execute the line P as a command.
1481 Pass FROM_TTY as second argument to the defining function. */
1484 execute_command (p
, from_tty
)
1488 register struct cmd_list_element
*c
;
1489 register enum language flang
;
1490 static int warned
= 0;
1492 /* FIXME: These should really be in an appropriate header file */
1493 extern void serial_log_command
PARAMS ((const char *));
1497 /* Force cleanup of any alloca areas if using C alloca instead of
1498 a builtin alloca. */
1501 /* This can happen when command_line_input hits end of file. */
1505 serial_log_command (p
);
1507 while (*p
== ' ' || *p
== '\t')
1514 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1516 /* If the target is running, we allow only a limited set of
1518 if (event_loop_p
&& target_can_async_p () && target_executing
)
1519 if (!strcmp (c
->name
, "help")
1520 && !strcmp (c
->name
, "pwd")
1521 && !strcmp (c
->name
, "show")
1522 && !strcmp (c
->name
, "stop"))
1523 error ("Cannot execute this command while the target is running.");
1525 /* Pass null arg rather than an empty one. */
1528 /* Clear off trailing whitespace, except for set and complete command. */
1529 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1531 p
= arg
+ strlen (arg
) - 1;
1532 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1537 /* If this command has been hooked, run the hook first. */
1539 execute_user_command (c
->hook
, (char *) 0);
1541 if (c
->flags
& DEPRECATED_WARN_USER
)
1542 deprecated_cmd_warning (&line
);
1544 if (c
->class == class_user
)
1545 execute_user_command (c
, arg
);
1546 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1547 do_setshow_command (arg
, from_tty
& caution
, c
);
1548 else if (c
->function
.cfunc
== NO_FUNCTION
)
1549 error ("That is not a command, just a help topic.");
1550 else if (call_command_hook
)
1551 call_command_hook (c
, arg
, from_tty
& caution
);
1553 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1556 /* Tell the user if the language has changed (except first time). */
1557 if (current_language
!= expected_language
)
1559 if (language_mode
== language_mode_auto
)
1561 language_info (1); /* Print what changed. */
1566 /* Warn the user if the working language does not match the
1567 language of the current frame. Only warn the user if we are
1568 actually running the program, i.e. there is a stack. */
1569 /* FIXME: This should be cacheing the frame and only running when
1570 the frame changes. */
1572 if (target_has_stack
)
1574 flang
= get_frame_language ();
1576 && flang
!= language_unknown
1577 && flang
!= current_language
->la_language
)
1579 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1586 /* NOTE 1999-04-29: This function will be static again, once we modify
1587 gdb to use the event loop as the default command loop and we merge
1588 event-top.c into this file, top.c */
1590 command_loop_marker (void *foo
)
1594 /* Read commands from `instream' and execute them
1595 until end of file or error reading instream. */
1600 struct cleanup
*old_chain
;
1602 int stdin_is_tty
= ISATTY (stdin
);
1603 long time_at_cmd_start
;
1605 long space_at_cmd_start
= 0;
1607 extern int display_time
;
1608 extern int display_space
;
1610 while (instream
&& !feof (instream
))
1613 extern int insert_mode
;
1615 if (window_hook
&& instream
== stdin
)
1616 (*window_hook
) (instream
, get_prompt ());
1619 if (instream
== stdin
&& stdin_is_tty
)
1620 reinitialize_more_filter ();
1621 old_chain
= make_cleanup (command_loop_marker
, 0);
1624 /* A bit of paranoia: I want to make sure the "insert_mode" global
1625 * is clear except when it is being used for command-line editing
1626 * (see tuiIO.c, utils.c); otherwise normal output will
1627 * get messed up in the TUI. So clear it before/after
1628 * the command-line-input call. - RT
1632 /* Get a command-line. This calls the readline package. */
1633 command
= command_line_input (instream
== stdin
?
1634 get_prompt () : (char *) NULL
,
1635 instream
== stdin
, "prompt");
1642 time_at_cmd_start
= get_run_time ();
1647 extern char **environ
;
1648 char *lim
= (char *) sbrk (0);
1650 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1654 execute_command (command
, instream
== stdin
);
1655 /* Do any commands attached to breakpoint we stopped at. */
1656 bpstat_do_actions (&stop_bpstat
);
1657 do_cleanups (old_chain
);
1661 long cmd_time
= get_run_time () - time_at_cmd_start
;
1663 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1664 cmd_time
/ 1000000, cmd_time
% 1000000);
1670 extern char **environ
;
1671 char *lim
= (char *) sbrk (0);
1672 long space_now
= lim
- (char *) &environ
;
1673 long space_diff
= space_now
- space_at_cmd_start
;
1675 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1677 (space_diff
>= 0 ? '+' : '-'),
1684 /* Read commands from `instream' and execute them until end of file or
1685 error reading instream. This command loop doesnt care about any
1686 such things as displaying time and space usage. If the user asks
1687 for those, they won't work. */
1689 simplified_command_loop (read_input_func
, execute_command_func
)
1690 char *(*read_input_func
) (char *);
1691 void (*execute_command_func
) (char *, int);
1693 struct cleanup
*old_chain
;
1695 int stdin_is_tty
= ISATTY (stdin
);
1697 while (instream
&& !feof (instream
))
1700 if (instream
== stdin
&& stdin_is_tty
)
1701 reinitialize_more_filter ();
1702 old_chain
= make_cleanup (command_loop_marker
, 0);
1704 /* Get a command-line. */
1705 command
= (*read_input_func
) (instream
== stdin
?
1706 get_prompt () : (char *) NULL
);
1711 (*execute_command_func
) (command
, instream
== stdin
);
1713 /* Do any commands attached to breakpoint we stopped at. */
1714 bpstat_do_actions (&stop_bpstat
);
1716 do_cleanups (old_chain
);
1720 /* Commands call this if they do not want to be repeated by null lines. */
1728 /* If we aren't reading from standard input, we are saving the last
1729 thing read from stdin in line and don't want to delete it. Null lines
1730 won't repeat here in any case. */
1731 if (instream
== stdin
)
1735 /* Read a line from the stream "instream" without command line editing.
1737 It prints PROMPT_ARG once at the start.
1738 Action is compatible with "readline", e.g. space for the result is
1739 malloc'd and should be freed by the caller.
1741 A NULL return means end of file. */
1743 gdb_readline (prompt_arg
)
1748 int input_index
= 0;
1749 int result_size
= 80;
1753 /* Don't use a _filtered function here. It causes the assumed
1754 character position to be off, since the newline we read from
1755 the user is not accounted for. */
1756 fputs_unfiltered (prompt_arg
, gdb_stdout
);
1758 /* Move to a new line so the entered line doesn't have a prompt
1759 on the front of it. */
1760 fputs_unfiltered ("\n", gdb_stdout
);
1762 gdb_flush (gdb_stdout
);
1765 result
= (char *) xmalloc (result_size
);
1769 /* Read from stdin if we are executing a user defined command.
1770 This is the right thing for prompt_for_continue, at least. */
1771 c
= fgetc (instream
? instream
: stdin
);
1775 if (input_index
> 0)
1776 /* The last line does not end with a newline. Return it, and
1777 if we are called again fgetc will still return EOF and
1778 we'll return NULL then. */
1785 #ifndef CRLF_SOURCE_FILES
1789 if (input_index
> 0 && result
[input_index
- 1] == '\r')
1795 result
[input_index
++] = c
;
1796 while (input_index
>= result_size
)
1799 result
= (char *) xrealloc (result
, result_size
);
1803 result
[input_index
++] = '\0';
1807 /* Variables which control command line editing and history
1808 substitution. These variables are given default values at the end
1810 static int command_editing_p
;
1811 /* NOTE 1999-04-29: This variable will be static again, once we modify
1812 gdb to use the event loop as the default command loop and we merge
1813 event-top.c into this file, top.c */
1814 /* static */ int history_expansion_p
;
1815 static int write_history_p
;
1816 static int history_size
;
1817 static char *history_filename
;
1819 /* readline uses the word breaks for two things:
1820 (1) In figuring out where to point the TEXT parameter to the
1821 rl_completion_entry_function. Since we don't use TEXT for much,
1822 it doesn't matter a lot what the word breaks are for this purpose, but
1823 it does affect how much stuff M-? lists.
1824 (2) If one of the matches contains a word break character, readline
1825 will quote it. That's why we switch between
1826 gdb_completer_word_break_characters and
1827 gdb_completer_command_word_break_characters. I'm not sure when
1828 we need this behavior (perhaps for funky characters in C++ symbols?). */
1830 /* Variables which are necessary for fancy command line editing. */
1831 char *gdb_completer_word_break_characters
=
1832 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1834 /* When completing on command names, we remove '-' from the list of
1835 word break characters, since we use it in command names. If the
1836 readline library sees one in any of the current completion strings,
1837 it thinks that the string needs to be quoted and automatically supplies
1839 char *gdb_completer_command_word_break_characters
=
1840 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1842 /* When completing on file names, we remove from the list of word
1843 break characters any characters that are commonly used in file
1844 names, such as '-', '+', '~', etc. Otherwise, readline displays
1845 incorrect completion candidates. */
1846 char *gdb_completer_file_name_break_characters
= " \t\n*|\"';:?/><";
1848 /* Characters that can be used to quote completion strings. Note that we
1849 can't include '"' because the gdb C parser treats such quoted sequences
1851 char *gdb_completer_quote_characters
=
1854 /* Functions that are used as part of the fancy command line editing. */
1856 /* This can be used for functions which don't want to complete on symbols
1857 but don't want to complete on anything else either. */
1860 noop_completer (text
, prefix
)
1867 /* Complete on filenames. */
1869 filename_completer (text
, word
)
1873 /* From readline. */
1874 extern char *filename_completion_function
PARAMS ((char *, int));
1875 int subsequent_name
;
1877 int return_val_used
;
1878 int return_val_alloced
;
1880 return_val_used
= 0;
1881 /* Small for testing. */
1882 return_val_alloced
= 1;
1883 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1885 subsequent_name
= 0;
1889 p
= filename_completion_function (text
, subsequent_name
);
1890 if (return_val_used
>= return_val_alloced
)
1892 return_val_alloced
*= 2;
1894 (char **) xrealloc (return_val
,
1895 return_val_alloced
* sizeof (char *));
1899 return_val
[return_val_used
++] = p
;
1902 /* We need to set subsequent_name to a non-zero value before the
1903 continue line below, because otherwise, if the first file seen
1904 by GDB is a backup file whose name ends in a `~', we will loop
1906 subsequent_name
= 1;
1907 /* Like emacs, don't complete on old versions. Especially useful
1908 in the "source" command. */
1909 if (p
[strlen (p
) - 1] == '~')
1915 /* Return exactly p. */
1916 return_val
[return_val_used
++] = p
;
1917 else if (word
> text
)
1919 /* Return some portion of p. */
1920 q
= xmalloc (strlen (p
) + 5);
1921 strcpy (q
, p
+ (word
- text
));
1922 return_val
[return_val_used
++] = q
;
1927 /* Return some of TEXT plus p. */
1928 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1929 strncpy (q
, word
, text
- word
);
1930 q
[text
- word
] = '\0';
1932 return_val
[return_val_used
++] = q
;
1938 /* There is no way to do this just long enough to affect quote inserting
1939 without also affecting the next completion. This should be fixed in
1941 /* Insure that readline does the right thing
1942 with respect to inserting quotes. */
1943 rl_completer_word_break_characters
= "";
1948 /* Here are some useful test cases for completion. FIXME: These should
1949 be put in the test suite. They should be tested with both M-? and TAB.
1951 "show output-" "radix"
1952 "show output" "-radix"
1953 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1954 "p " ambiguous (all symbols)
1955 "info t foo" no completions
1956 "info t " no completions
1957 "info t" ambiguous ("info target", "info terminal", etc.)
1958 "info ajksdlfk" no completions
1959 "info ajksdlfk " no completions
1961 "info " ambiguous (all info commands)
1962 "p \"a" no completions (string constant)
1963 "p 'a" ambiguous (all symbols starting with a)
1964 "p b-a" ambiguous (all symbols starting with a)
1965 "p b-" ambiguous (all symbols)
1966 "file Make" "file" (word break hard to screw up here)
1967 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1970 /* Generate completions one by one for the completer. Each time we are
1971 called return another potential completion to the caller.
1972 line_completion just completes on commands or passes the buck to the
1973 command's completer function, the stuff specific to symbol completion
1974 is in make_symbol_completion_list.
1976 TEXT is the caller's idea of the "word" we are looking at.
1978 MATCHES is the number of matches that have currently been collected from
1979 calling this completion function. When zero, then we need to initialize,
1980 otherwise the initialization has already taken place and we can just
1981 return the next potential completion string.
1983 LINE_BUFFER is available to be looked at; it contains the entire text
1984 of the line. POINT is the offset in that line of the cursor. You
1985 should pretend that the line ends at POINT.
1987 Returns NULL if there are no more completions, else a pointer to a string
1988 which is a possible completion, it is the caller's responsibility to
1992 line_completion_function (text
, matches
, line_buffer
, point
)
1998 static char **list
= (char **) NULL
; /* Cache of completions */
1999 static int index
; /* Next cached completion */
2000 char *output
= NULL
;
2001 char *tmp_command
, *p
;
2002 /* Pointer within tmp_command which corresponds to text. */
2004 struct cmd_list_element
*c
, *result_list
;
2008 /* The caller is beginning to accumulate a new set of completions, so
2009 we need to find all of them now, and cache them for returning one at
2010 a time on future calls. */
2014 /* Free the storage used by LIST, but not by the strings inside.
2015 This is because rl_complete_internal () frees the strings. */
2021 /* Choose the default set of word break characters to break completions.
2022 If we later find out that we are doing completions on command strings
2023 (as opposed to strings supplied by the individual command completer
2024 functions, which can be any string) then we will switch to the
2025 special word break set for command strings, which leaves out the
2026 '-' character used in some commands. */
2028 rl_completer_word_break_characters
=
2029 gdb_completer_word_break_characters
;
2031 /* Decide whether to complete on a list of gdb commands or on symbols. */
2032 tmp_command
= (char *) alloca (point
+ 1);
2035 strncpy (tmp_command
, line_buffer
, point
);
2036 tmp_command
[point
] = '\0';
2037 /* Since text always contains some number of characters leading up
2038 to point, we can find the equivalent position in tmp_command
2039 by subtracting that many characters from the end of tmp_command. */
2040 word
= tmp_command
+ point
- strlen (text
);
2044 /* An empty line we want to consider ambiguous; that is, it
2045 could be any command. */
2046 c
= (struct cmd_list_element
*) -1;
2051 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
2054 /* Move p up to the next interesting thing. */
2055 while (*p
== ' ' || *p
== '\t')
2062 /* It is an unrecognized command. So there are no
2063 possible completions. */
2066 else if (c
== (struct cmd_list_element
*) -1)
2070 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
2071 doesn't advance over that thing itself. Do so now. */
2073 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
2075 if (q
!= tmp_command
+ point
)
2077 /* There is something beyond the ambiguous
2078 command, so there are no possible completions. For
2079 example, "info t " or "info t foo" does not complete
2080 to anything, because "info t" can be "info target" or
2086 /* We're trying to complete on the command which was ambiguous.
2087 This we can deal with. */
2090 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
2095 list
= complete_on_cmdlist (cmdlist
, p
, word
);
2097 /* Insure that readline does the right thing with respect to
2098 inserting quotes. */
2099 rl_completer_word_break_characters
=
2100 gdb_completer_command_word_break_characters
;
2105 /* We've recognized a full command. */
2107 if (p
== tmp_command
+ point
)
2109 /* There is no non-whitespace in the line beyond the command. */
2111 if (p
[-1] == ' ' || p
[-1] == '\t')
2113 /* The command is followed by whitespace; we need to complete
2114 on whatever comes after command. */
2117 /* It is a prefix command; what comes after it is
2118 a subcommand (e.g. "info "). */
2119 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
2121 /* Insure that readline does the right thing
2122 with respect to inserting quotes. */
2123 rl_completer_word_break_characters
=
2124 gdb_completer_command_word_break_characters
;
2128 list
= complete_on_enum (c
->enums
, p
, word
);
2129 rl_completer_word_break_characters
=
2130 gdb_completer_command_word_break_characters
;
2134 /* It is a normal command; what comes after it is
2135 completed by the command's completer function. */
2136 list
= (*c
->completer
) (p
, word
);
2137 if (c
->completer
== filename_completer
)
2138 rl_completer_word_break_characters
=
2139 gdb_completer_file_name_break_characters
;
2144 /* The command is not followed by whitespace; we need to
2145 complete on the command itself. e.g. "p" which is a
2146 command itself but also can complete to "print", "ptype"
2150 /* Find the command we are completing on. */
2152 while (q
> tmp_command
)
2154 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
2160 list
= complete_on_cmdlist (result_list
, q
, word
);
2162 /* Insure that readline does the right thing
2163 with respect to inserting quotes. */
2164 rl_completer_word_break_characters
=
2165 gdb_completer_command_word_break_characters
;
2170 /* There is non-whitespace beyond the command. */
2172 if (c
->prefixlist
&& !c
->allow_unknown
)
2174 /* It is an unrecognized subcommand of a prefix command,
2175 e.g. "info adsfkdj". */
2180 list
= complete_on_enum (c
->enums
, p
, word
);
2184 /* It is a normal command. */
2185 list
= (*c
->completer
) (p
, word
);
2186 if (c
->completer
== filename_completer
)
2187 rl_completer_word_break_characters
=
2188 gdb_completer_file_name_break_characters
;
2194 /* If we found a list of potential completions during initialization then
2195 dole them out one at a time. The vector of completions is NULL
2196 terminated, so after returning the last one, return NULL (and continue
2197 to do so) each time we are called after that, until a new list is
2202 output
= list
[index
];
2210 /* Can't do this because readline hasn't yet checked the word breaks
2211 for figuring out whether to insert a quote. */
2213 /* Make sure the word break characters are set back to normal for the
2214 next time that readline tries to complete something. */
2215 rl_completer_word_break_characters
=
2216 gdb_completer_word_break_characters
;
2222 /* Line completion interface function for readline. */
2225 readline_line_completion_function (text
, matches
)
2229 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
2232 /* Skip over a possibly quoted word (as defined by the quote characters
2233 and word break characters the completer uses). Returns pointer to the
2234 location after the "word". */
2240 char quote_char
= '\0';
2243 for (scan
= str
; *scan
!= '\0'; scan
++)
2245 if (quote_char
!= '\0')
2247 /* Ignore everything until the matching close quote char */
2248 if (*scan
== quote_char
)
2250 /* Found matching close quote. */
2255 else if (strchr (gdb_completer_quote_characters
, *scan
))
2257 /* Found start of a quoted string. */
2260 else if (strchr (gdb_completer_word_break_characters
, *scan
))
2274 #if STOP_SIGNAL == SIGTSTP
2275 signal (SIGTSTP
, SIG_DFL
);
2277 kill (getpid (), SIGTSTP
);
2278 signal (SIGTSTP
, stop_sig
);
2280 signal (STOP_SIGNAL
, stop_sig
);
2282 printf_unfiltered ("%s", get_prompt ());
2283 gdb_flush (gdb_stdout
);
2285 /* Forget about any previous command -- null line now will do nothing. */
2288 #endif /* STOP_SIGNAL */
2290 /* Initialize signal handlers. */
2295 /* Under System V the default disposition of a signal is reinstated after
2296 the signal is caught and delivered to an application process. On such
2297 systems one must restore the replacement signal handler if one wishes
2298 to continue handling the signal in one's program. On BSD systems this
2299 is not needed but it is harmless, and it simplifies the code to just do
2300 it unconditionally. */
2301 signal (signo
, do_nothing
);
2307 signal (SIGINT
, request_quit
);
2309 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2310 to the inferior and breakpoints will be ignored. */
2312 signal (SIGTRAP
, SIG_DFL
);
2315 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2316 passed to the inferior, which we don't want. It would be
2317 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2318 on BSD4.3 systems using vfork, that can affect the
2319 GDB process as well as the inferior (the signal handling tables
2320 might be in memory, shared between the two). Since we establish
2321 a handler for SIGQUIT, when we call exec it will set the signal
2322 to SIG_DFL for us. */
2323 signal (SIGQUIT
, do_nothing
);
2325 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
2326 signal (SIGHUP
, disconnect
);
2328 signal (SIGFPE
, float_handler
);
2330 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2331 signal (SIGWINCH
, SIGWINCH_HANDLER
);
2335 /* Read one line from the command input stream `instream'
2336 into the local static buffer `linebuffer' (whose current length
2338 The buffer is made bigger as necessary.
2339 Returns the address of the start of the line.
2341 NULL is returned for end of file.
2343 *If* the instream == stdin & stdin is a terminal, the line read
2344 is copied into the file line saver (global var char *line,
2345 length linesize) so that it can be duplicated.
2347 This routine either uses fancy command line editing or
2348 simple input as the user has requested. */
2351 command_line_input (prompt_arg
, repeat
, annotation_suffix
)
2354 char *annotation_suffix
;
2356 static char *linebuffer
= 0;
2357 static unsigned linelength
= 0;
2361 char *local_prompt
= prompt_arg
;
2365 /* The annotation suffix must be non-NULL. */
2366 if (annotation_suffix
== NULL
)
2367 annotation_suffix
= "";
2369 if (annotation_level
> 1 && instream
== stdin
)
2371 local_prompt
= alloca ((prompt_arg
== NULL
? 0 : strlen (prompt_arg
))
2372 + strlen (annotation_suffix
) + 40);
2373 if (prompt_arg
== NULL
)
2374 local_prompt
[0] = '\0';
2376 strcpy (local_prompt
, prompt_arg
);
2377 strcat (local_prompt
, "\n\032\032");
2378 strcat (local_prompt
, annotation_suffix
);
2379 strcat (local_prompt
, "\n");
2382 if (linebuffer
== 0)
2385 linebuffer
= (char *) xmalloc (linelength
);
2390 /* Control-C quits instantly if typed while in this loop
2391 since it should not wait until the user types a newline. */
2397 signal (STOP_SIGNAL
, handle_stop_sig
);
2399 signal (STOP_SIGNAL
, stop_sig
);
2405 /* Make sure that all output has been output. Some machines may let
2406 you get away with leaving out some of the gdb_flush, but not all. */
2408 gdb_flush (gdb_stdout
);
2409 gdb_flush (gdb_stderr
);
2411 if (source_file_name
!= NULL
)
2413 ++source_line_number
;
2414 sprintf (source_error
,
2415 "%s%s:%d: Error in sourced command file:\n",
2418 source_line_number
);
2419 error_pre_print
= source_error
;
2422 if (annotation_level
> 1 && instream
== stdin
)
2424 printf_unfiltered ("\n\032\032pre-");
2425 printf_unfiltered (annotation_suffix
);
2426 printf_unfiltered ("\n");
2429 /* Don't use fancy stuff if not talking to stdin. */
2430 if (readline_hook
&& instream
== NULL
)
2432 rl
= (*readline_hook
) (local_prompt
);
2434 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
2436 rl
= readline (local_prompt
);
2440 rl
= gdb_readline (local_prompt
);
2443 if (annotation_level
> 1 && instream
== stdin
)
2445 printf_unfiltered ("\n\032\032post-");
2446 printf_unfiltered (annotation_suffix
);
2447 printf_unfiltered ("\n");
2450 if (!rl
|| rl
== (char *) EOF
)
2455 if (strlen (rl
) + 1 + (p
- linebuffer
) > linelength
)
2457 linelength
= strlen (rl
) + 1 + (p
- linebuffer
);
2458 nline
= (char *) xrealloc (linebuffer
, linelength
);
2459 p
+= nline
- linebuffer
;
2463 /* Copy line. Don't copy null at end. (Leaves line alone
2464 if this was just a newline) */
2468 free (rl
); /* Allocated in readline. */
2470 if (p
== linebuffer
|| *(p
- 1) != '\\')
2473 p
--; /* Put on top of '\'. */
2474 local_prompt
= (char *) 0;
2479 signal (STOP_SIGNAL
, SIG_DFL
);
2486 #define SERVER_COMMAND_LENGTH 7
2488 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2489 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2492 /* Note that we don't set `line'. Between this and the check in
2493 dont_repeat, this insures that repeating will still do the
2496 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2499 /* Do history expansion if that is wished. */
2500 if (history_expansion_p
&& instream
== stdin
2501 && ISATTY (instream
))
2503 char *history_value
;
2506 *p
= '\0'; /* Insert null now. */
2507 expanded
= history_expand (linebuffer
, &history_value
);
2510 /* Print the changes. */
2511 printf_unfiltered ("%s\n", history_value
);
2513 /* If there was an error, call this function again. */
2516 free (history_value
);
2517 return command_line_input (prompt_arg
, repeat
, annotation_suffix
);
2519 if (strlen (history_value
) > linelength
)
2521 linelength
= strlen (history_value
) + 1;
2522 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2524 strcpy (linebuffer
, history_value
);
2525 p
= linebuffer
+ strlen (linebuffer
);
2526 free (history_value
);
2530 /* If we just got an empty line, and that is supposed
2531 to repeat the previous command, return the value in the
2533 if (repeat
&& p
== linebuffer
)
2535 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++);
2541 /* Add line to history if appropriate. */
2542 if (instream
== stdin
2543 && ISATTY (stdin
) && *linebuffer
)
2544 add_history (linebuffer
);
2546 /* Note: lines consisting solely of comments are added to the command
2547 history. This is useful when you type a command, and then
2548 realize you don't want to execute it quite yet. You can comment
2549 out the command and then later fetch it from the value history
2550 and remove the '#'. The kill ring is probably better, but some
2551 people are in the habit of commenting things out. */
2553 *p1
= '\0'; /* Found a comment. */
2555 /* Save into global buffer if appropriate. */
2558 if (linelength
> linesize
)
2560 line
= xrealloc (line
, linelength
);
2561 linesize
= linelength
;
2563 strcpy (line
, linebuffer
);
2571 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2572 code bodies. This is typically used when we encounter an "else"
2573 clause for an "if" command. */
2576 realloc_body_list (command
, new_length
)
2577 struct command_line
*command
;
2581 struct command_line
**body_list
;
2583 n
= command
->body_count
;
2585 /* Nothing to do? */
2586 if (new_length
<= n
)
2589 body_list
= (struct command_line
**)
2590 xmalloc (sizeof (struct command_line
*) * new_length
);
2592 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2594 free (command
->body_list
);
2595 command
->body_list
= body_list
;
2596 command
->body_count
= new_length
;
2599 /* Read one line from the input stream. If the command is an "else" or
2600 "end", return such an indication to the caller. */
2602 static enum misc_command_type
2603 read_next_line (command
)
2604 struct command_line
**command
;
2606 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2609 if (control_level
>= 254)
2610 error ("Control nesting too deep!\n");
2612 /* Set a prompt based on the nesting of the control commands. */
2613 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2615 for (i
= 0; i
< control_level
; i
++)
2616 control_prompt
[i
] = ' ';
2617 control_prompt
[i
] = '>';
2618 control_prompt
[i
+ 1] = '\0';
2619 prompt_ptr
= (char *) &control_prompt
[0];
2624 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2626 /* Not sure what to do here. */
2630 /* Strip leading and trailing whitespace. */
2631 while (*p
== ' ' || *p
== '\t')
2634 p1
= p
+ strlen (p
);
2635 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2638 /* Blanks and comments don't really do anything, but we need to
2639 distinguish them from else, end and other commands which can be
2641 if (p1
== p
|| p
[0] == '#')
2644 /* Is this the end of a simple, while, or if control structure? */
2645 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2648 /* Is the else clause of an if control structure? */
2649 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2650 return else_command
;
2652 /* Check for while, if, break, continue, etc and build a new command
2653 line structure for them. */
2654 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2655 *command
= build_command_line (while_control
, p
+ 6);
2656 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2657 *command
= build_command_line (if_control
, p
+ 3);
2658 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2660 *command
= (struct command_line
*)
2661 xmalloc (sizeof (struct command_line
));
2662 (*command
)->next
= NULL
;
2663 (*command
)->line
= NULL
;
2664 (*command
)->control_type
= break_control
;
2665 (*command
)->body_count
= 0;
2666 (*command
)->body_list
= NULL
;
2668 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2670 *command
= (struct command_line
*)
2671 xmalloc (sizeof (struct command_line
));
2672 (*command
)->next
= NULL
;
2673 (*command
)->line
= NULL
;
2674 (*command
)->control_type
= continue_control
;
2675 (*command
)->body_count
= 0;
2676 (*command
)->body_list
= NULL
;
2680 /* A normal command. */
2681 *command
= (struct command_line
*)
2682 xmalloc (sizeof (struct command_line
));
2683 (*command
)->next
= NULL
;
2684 (*command
)->line
= savestring (p
, p1
- p
);
2685 (*command
)->control_type
= simple_control
;
2686 (*command
)->body_count
= 0;
2687 (*command
)->body_list
= NULL
;
2690 /* Nothing special. */
2694 /* Recursively read in the control structures and create a command_line
2695 structure from them.
2697 The parent_control parameter is the control structure in which the
2698 following commands are nested. */
2700 static enum command_control_type
2701 recurse_read_control_structure (current_cmd
)
2702 struct command_line
*current_cmd
;
2704 int current_body
, i
;
2705 enum misc_command_type val
;
2706 enum command_control_type ret
;
2707 struct command_line
**body_ptr
, *child_tail
, *next
;
2712 /* Sanity checks. */
2713 if (current_cmd
->control_type
== simple_control
)
2715 error ("Recursed on a simple control type\n");
2716 return invalid_control
;
2719 if (current_body
> current_cmd
->body_count
)
2721 error ("Allocated body is smaller than this command type needs\n");
2722 return invalid_control
;
2725 /* Read lines from the input stream and build control structures. */
2731 val
= read_next_line (&next
);
2733 /* Just skip blanks and comments. */
2734 if (val
== nop_command
)
2737 if (val
== end_command
)
2739 if (current_cmd
->control_type
== while_control
2740 || current_cmd
->control_type
== if_control
)
2742 /* Success reading an entire control structure. */
2743 ret
= simple_control
;
2748 ret
= invalid_control
;
2753 /* Not the end of a control structure. */
2754 if (val
== else_command
)
2756 if (current_cmd
->control_type
== if_control
2757 && current_body
== 1)
2759 realloc_body_list (current_cmd
, 2);
2766 ret
= invalid_control
;
2773 child_tail
->next
= next
;
2777 body_ptr
= current_cmd
->body_list
;
2778 for (i
= 1; i
< current_body
; i
++)
2787 /* If the latest line is another control structure, then recurse
2789 if (next
->control_type
== while_control
2790 || next
->control_type
== if_control
)
2793 ret
= recurse_read_control_structure (next
);
2796 if (ret
!= simple_control
)
2806 /* Read lines from the input stream and accumulate them in a chain of
2807 struct command_line's, which is then returned. For input from a
2808 terminal, the special command "end" is used to mark the end of the
2809 input, and is not included in the returned chain of commands. */
2811 #define END_MESSAGE "End with a line saying just \"end\"."
2813 struct command_line
*
2814 read_command_lines (prompt_arg
, from_tty
)
2818 struct command_line
*head
, *tail
, *next
;
2819 struct cleanup
*old_chain
;
2820 enum command_control_type ret
;
2821 enum misc_command_type val
;
2824 if (readline_begin_hook
)
2826 /* Note - intentional to merge messages with no newline */
2827 (*readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
2829 else if (from_tty
&& input_from_terminal_p ())
2831 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
2832 gdb_flush (gdb_stdout
);
2840 val
= read_next_line (&next
);
2842 /* Ignore blank lines or comments. */
2843 if (val
== nop_command
)
2846 if (val
== end_command
)
2848 ret
= simple_control
;
2852 if (val
!= ok_command
)
2854 ret
= invalid_control
;
2858 if (next
->control_type
== while_control
2859 || next
->control_type
== if_control
)
2862 ret
= recurse_read_control_structure (next
);
2865 if (ret
== invalid_control
)
2876 old_chain
= make_cleanup_free_command_lines (&head
);
2885 if (ret
!= invalid_control
)
2887 discard_cleanups (old_chain
);
2890 do_cleanups (old_chain
);
2893 if (readline_end_hook
)
2895 (*readline_end_hook
) ();
2900 /* Free a chain of struct command_line's. */
2903 free_command_lines (lptr
)
2904 struct command_line
**lptr
;
2906 register struct command_line
*l
= *lptr
;
2907 register struct command_line
*next
;
2908 struct command_line
**blist
;
2913 if (l
->body_count
> 0)
2915 blist
= l
->body_list
;
2916 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2917 free_command_lines (blist
);
2927 do_free_command_lines_cleanup (void *arg
)
2929 free_command_lines (arg
);
2932 static struct cleanup
*
2933 make_cleanup_free_command_lines (struct command_line
**arg
)
2935 return make_cleanup (do_free_command_lines_cleanup
, arg
);
2938 /* Add an element to the list of info subcommands. */
2940 struct cmd_list_element
*
2941 add_info (name
, fun
, doc
)
2943 void (*fun
) PARAMS ((char *, int));
2946 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2949 /* Add an alias to the list of info subcommands. */
2951 struct cmd_list_element
*
2952 add_info_alias (name
, oldname
, abbrev_flag
)
2957 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2960 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2961 Therefore, its own definition is called only for "info" with no args. */
2965 info_command (arg
, from_tty
)
2969 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2970 help_list (infolist
, "info ", -1, gdb_stdout
);
2973 /* The "complete" command is used by Emacs to implement completion. */
2977 complete_command (arg
, from_tty
)
2989 argpoint
= strlen (arg
);
2991 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2993 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2995 printf_unfiltered ("%s\n", completion
);
3000 /* The "show" command with no arguments shows all the settings. */
3004 show_command (arg
, from_tty
)
3008 cmd_show_list (showlist
, from_tty
, "");
3011 /* Add an element to the list of commands. */
3013 struct cmd_list_element
*
3014 add_com (name
, class, fun
, doc
)
3016 enum command_class
class;
3017 void (*fun
) PARAMS ((char *, int));
3020 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
3023 /* Add an alias or abbreviation command to the list of commands. */
3025 struct cmd_list_element
*
3026 add_com_alias (name
, oldname
, class, abbrev_flag
)
3029 enum command_class
class;
3032 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
3039 error ("Argument required (%s).", why
);
3044 help_command (command
, from_tty
)
3046 int from_tty
; /* Ignored */
3048 help_cmd (command
, gdb_stdout
);
3052 validate_comname (comname
)
3058 error_no_arg ("name of command to define");
3063 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
3064 error ("Junk in argument list: \"%s\"", p
);
3069 /* This is just a placeholder in the command data structures. */
3071 user_defined_command (ignore
, from_tty
)
3078 define_command (comname
, from_tty
)
3082 register struct command_line
*cmds
;
3083 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
3084 char *tem
= comname
;
3086 #define HOOK_STRING "hook-"
3089 validate_comname (comname
);
3091 /* Look it up, and verify that we got an exact match. */
3092 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
3093 if (c
&& !STREQ (comname
, c
->name
))
3098 if (c
->class == class_user
|| c
->class == class_alias
)
3099 tem
= "Redefine command \"%s\"? ";
3101 tem
= "Really redefine built-in command \"%s\"? ";
3102 if (!query (tem
, c
->name
))
3103 error ("Command \"%s\" not redefined.", c
->name
);
3106 /* If this new command is a hook, then mark the command which it
3107 is hooking. Note that we allow hooking `help' commands, so that
3108 we can hook the `stop' pseudo-command. */
3110 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
3112 /* Look up cmd it hooks, and verify that we got an exact match. */
3113 tem
= comname
+ HOOK_LEN
;
3114 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
3115 if (hookc
&& !STREQ (comname
+ HOOK_LEN
, hookc
->name
))
3119 warning ("Your new `%s' command does not hook any existing command.",
3121 if (!query ("Proceed? "))
3122 error ("Not confirmed.");
3126 comname
= savestring (comname
, strlen (comname
));
3128 /* If the rest of the commands will be case insensitive, this one
3129 should behave in the same manner. */
3130 for (tem
= comname
; *tem
; tem
++)
3132 *tem
= tolower (*tem
);
3134 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
3135 cmds
= read_command_lines (tmpbuf
, from_tty
);
3137 if (c
&& c
->class == class_user
)
3138 free_command_lines (&c
->user_commands
);
3140 newc
= add_cmd (comname
, class_user
, user_defined_command
,
3141 (c
&& c
->class == class_user
)
3142 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
3143 newc
->user_commands
= cmds
;
3145 /* If this new command is a hook, then mark both commands as being
3149 hookc
->hook
= newc
; /* Target gets hooked. */
3150 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
3155 document_command (comname
, from_tty
)
3159 struct command_line
*doclines
;
3160 register struct cmd_list_element
*c
;
3161 char *tem
= comname
;
3164 validate_comname (comname
);
3166 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
3168 if (c
->class != class_user
)
3169 error ("Command \"%s\" is built-in.", comname
);
3171 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
3172 doclines
= read_command_lines (tmpbuf
, from_tty
);
3178 register struct command_line
*cl1
;
3179 register int len
= 0;
3181 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
3182 len
+= strlen (cl1
->line
) + 1;
3184 c
->doc
= (char *) xmalloc (len
+ 1);
3187 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
3189 strcat (c
->doc
, cl1
->line
);
3191 strcat (c
->doc
, "\n");
3195 free_command_lines (&doclines
);
3198 /* Print the GDB banner. */
3200 print_gdb_version (stream
)
3201 struct ui_file
*stream
;
3203 /* From GNU coding standards, first line is meant to be easy for a
3204 program to parse, and is just canonical program name and version
3205 number, which starts after last space. */
3208 /* Print it console style until a format is defined */
3209 fprintf_filtered (stream
, "GNU gdb %s (UI_OUT)\n", version
);
3211 fprintf_filtered (stream
, "GNU gdb %s\n", version
);
3214 /* Second line is a copyright notice. */
3216 fprintf_filtered (stream
, "Copyright 2000 Free Software Foundation, Inc.\n");
3218 /* Following the copyright is a brief statement that the program is
3219 free software, that users are free to copy and change it on
3220 certain conditions, that it is covered by the GNU GPL, and that
3221 there is no warranty. */
3223 fprintf_filtered (stream
, "\
3224 GDB is free software, covered by the GNU General Public License, and you are\n\
3225 welcome to change it and/or distribute copies of it under certain conditions.\n\
3226 Type \"show copying\" to see the conditions.\n\
3227 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
3229 /* After the required info we print the configuration information. */
3231 fprintf_filtered (stream
, "This GDB was configured as \"");
3232 if (!STREQ (host_name
, target_name
))
3234 fprintf_filtered (stream
, "--host=%s --target=%s", host_name
, target_name
);
3238 fprintf_filtered (stream
, "%s", host_name
);
3240 fprintf_filtered (stream
, "\".");
3245 show_version (args
, from_tty
)
3250 print_gdb_version (gdb_stdout
);
3251 printf_filtered ("\n");
3255 /* get_prompt: access method for the GDB prompt string. */
3257 #define MAX_PROMPT_SIZE 256
3260 * int get_prompt_1 (char * buf);
3262 * Work-horse for get_prompt (called via catch_errors).
3263 * Argument is buffer to hold the formatted prompt.
3265 * Returns: 1 for success (use formatted prompt)
3266 * 0 for failure (use gdb_prompt_string).
3269 static int gdb_prompt_escape
;
3272 get_prompt_1 (formatted_prompt
)
3273 char *formatted_prompt
;
3278 local_prompt
= PROMPT (0);
3280 local_prompt
= gdb_prompt_string
;
3283 if (gdb_prompt_escape
== 0)
3285 return 0; /* do no formatting */
3288 /* formatted prompt */
3290 char fmt
[40], *promptp
, *outp
, *tmp
;
3297 struct type
*arg_type
, *elt_type
;
3299 promptp
= local_prompt
;
3300 outp
= formatted_prompt
;
3302 while (*promptp
!= '\0')
3304 int available
= MAX_PROMPT_SIZE
- (outp
- formatted_prompt
) - 1;
3306 if (*promptp
!= gdb_prompt_escape
)
3308 if (available
>= 1) /* overflow protect */
3309 *outp
++ = *promptp
++;
3313 /* GDB prompt string contains escape char. Parse for arg.
3314 Two consecutive escape chars followed by arg followed by
3315 a comma means to insert the arg using a default format.
3316 Otherwise a printf format string may be included between
3317 the two escape chars. eg:
3318 %%foo, insert foo using default format
3319 %2.2f%foo, insert foo using "%2.2f" format
3320 A mismatch between the format string and the data type
3321 of "foo" is an error (which we don't know how to protect
3324 fmt
[0] = '\0'; /* assume null format string */
3325 if (promptp
[1] == gdb_prompt_escape
) /* double esc char */
3327 promptp
+= 2; /* skip past two escape chars. */
3331 /* extract format string from between two esc chars */
3335 fmt
[i
++] = *promptp
++; /* copy format string */
3337 while (i
< sizeof (fmt
) - 1 &&
3338 *promptp
!= gdb_prompt_escape
&&
3341 if (*promptp
!= gdb_prompt_escape
)
3342 error ("Syntax error at prompt position %d",
3343 promptp
- local_prompt
);
3346 promptp
++; /* skip second escape char */
3347 fmt
[i
++] = '\0'; /* terminate the format string */
3351 arg_val
= parse_to_comma_and_eval (&promptp
);
3352 if (*promptp
== ',')
3353 promptp
++; /* skip past the comma */
3354 arg_type
= check_typedef (VALUE_TYPE (arg_val
));
3355 switch (TYPE_CODE (arg_type
))
3357 case TYPE_CODE_ARRAY
:
3358 elt_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
3359 if (TYPE_LENGTH (arg_type
) > 0 &&
3360 TYPE_LENGTH (elt_type
) == 1 &&
3361 TYPE_CODE (elt_type
) == TYPE_CODE_INT
)
3363 int len
= TYPE_LENGTH (arg_type
);
3365 if (VALUE_LAZY (arg_val
))
3366 value_fetch_lazy (arg_val
);
3367 tmp
= VALUE_CONTENTS (arg_val
);
3369 if (len
> available
)
3370 len
= available
; /* overflow protect */
3372 /* FIXME: how to protect GDB from crashing
3373 from bad user-supplied format string? */
3375 sprintf (outp
, fmt
, tmp
);
3377 strncpy (outp
, tmp
, len
);
3382 elt_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
3383 addrval
= value_as_pointer (arg_val
);
3385 if (TYPE_LENGTH (elt_type
) == 1 &&
3386 TYPE_CODE (elt_type
) == TYPE_CODE_INT
&&
3389 /* display it as a string */
3390 char *default_fmt
= "%s";
3394 /* Limiting the number of bytes that the following call
3395 will read protects us from sprintf overflow later. */
3396 i
= target_read_string (addrval
, /* src */
3398 available
, /* len */
3400 if (err
) /* read failed */
3401 error ("%s on target_read", safe_strerror (err
));
3403 tmp
[i
] = '\0'; /* force-terminate string */
3404 /* FIXME: how to protect GDB from crashing
3405 from bad user-supplied format string? */
3406 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3412 /* display it as a pointer */
3413 char *default_fmt
= "0x%x";
3415 /* FIXME: how to protect GDB from crashing
3416 from bad user-supplied format string? */
3417 if (available
>= 16 /*? */ ) /* overflow protect */
3418 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3424 char *default_fmt
= "%g";
3426 doubleval
= value_as_double (arg_val
);
3427 /* FIXME: how to protect GDB from crashing
3428 from bad user-supplied format string? */
3429 if (available
>= 16 /*? */ ) /* overflow protect */
3430 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3431 (double) doubleval
);
3436 char *default_fmt
= "%d";
3438 longval
= value_as_long (arg_val
);
3439 /* FIXME: how to protect GDB from crashing
3440 from bad user-supplied format string? */
3441 if (available
>= 16 /*? */ ) /* overflow protect */
3442 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3446 case TYPE_CODE_BOOL
:
3448 /* no default format for bool */
3449 longval
= value_as_long (arg_val
);
3450 if (available
>= 8 /*? */ ) /* overflow protect */
3453 strcpy (outp
, "<true>");
3455 strcpy (outp
, "<false>");
3459 case TYPE_CODE_ENUM
:
3461 /* no default format for enum */
3462 longval
= value_as_long (arg_val
);
3463 len
= TYPE_NFIELDS (arg_type
);
3464 /* find enum name if possible */
3465 for (i
= 0; i
< len
; i
++)
3466 if (TYPE_FIELD_BITPOS (arg_type
, i
) == longval
)
3467 break; /* match -- end loop */
3469 if (i
< len
) /* enum name found */
3471 char *name
= TYPE_FIELD_NAME (arg_type
, i
);
3473 strncpy (outp
, name
, available
);
3474 /* in casel available < strlen (name), */
3475 outp
[available
] = '\0';
3479 if (available
>= 16 /*? */ ) /* overflow protect */
3480 sprintf (outp
, "%ld", (long) longval
);
3484 case TYPE_CODE_VOID
:
3486 break; /* void type -- no output */
3488 error ("bad data type at prompt position %d",
3489 promptp
- local_prompt
);
3492 outp
+= strlen (outp
);
3495 *outp
++ = '\0'; /* terminate prompt string */
3503 static char buf
[MAX_PROMPT_SIZE
];
3505 if (catch_errors (get_prompt_1
, buf
, "bad formatted prompt: ",
3508 return &buf
[0]; /* successful formatted prompt */
3512 /* Prompt could not be formatted. */
3516 return gdb_prompt_string
;
3524 /* ??rehrauer: I don't know why this fails, since it looks as though
3525 assignments to prompt are wrapped in calls to savestring...
3530 PROMPT (0) = savestring (s
, strlen (s
));
3532 gdb_prompt_string
= savestring (s
, strlen (s
));
3536 /* If necessary, make the user confirm that we should quit. Return
3537 non-zero if we should quit, zero if we shouldn't. */
3542 if (inferior_pid
!= 0 && target_has_execution
)
3546 /* This is something of a hack. But there's no reliable way to
3547 see if a GUI is running. The `use_windows' variable doesn't
3550 s
= "A debugging session is active.\nDo you still want to close the debugger?";
3551 else if (attach_flag
)
3552 s
= "The program is running. Quit anyway (and detach it)? ";
3554 s
= "The program is running. Exit anyway? ";
3563 /* Quit without asking for confirmation. */
3566 quit_force (args
, from_tty
)
3572 /* An optional expression may be used to cause gdb to terminate with the
3573 value of that expression. */
3576 value_ptr val
= parse_and_eval (args
);
3578 exit_code
= (int) value_as_long (val
);
3581 if (inferior_pid
!= 0 && target_has_execution
)
3584 target_detach (args
, from_tty
);
3589 /* UDI wants this, to kill the TIP. */
3592 /* Save the history information if it is appropriate to do so. */
3593 if (write_history_p
&& history_filename
)
3594 write_history (history_filename
);
3596 do_final_cleanups (ALL_CLEANUPS
); /* Do any final cleanups before exiting */
3599 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3600 /* The above does not need to be inside a tuiDo(), since
3601 * it is not manipulating the curses screen, but rather,
3602 * it is tearing it down.
3611 /* Handle the quit command. */
3614 quit_command (args
, from_tty
)
3618 if (!quit_confirm ())
3619 error ("Not confirmed.");
3620 quit_force (args
, from_tty
);
3623 /* Returns whether GDB is running on a terminal and whether the user
3624 desires that questions be asked of them on that terminal. */
3627 input_from_terminal_p ()
3629 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
3634 pwd_command (args
, from_tty
)
3639 error ("The \"pwd\" command does not take an argument: %s", args
);
3640 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3642 if (!STREQ (gdb_dirbuf
, current_directory
))
3643 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3644 current_directory
, gdb_dirbuf
);
3646 printf_unfiltered ("Working directory %s.\n", current_directory
);
3650 cd_command (dir
, from_tty
)
3655 /* Found something other than leading repetitions of "/..". */
3656 int found_real_path
;
3659 /* If the new directory is absolute, repeat is a no-op; if relative,
3660 repeat might be useful but is more likely to be a mistake. */
3664 error_no_arg ("new working directory");
3666 dir
= tilde_expand (dir
);
3667 make_cleanup (free
, dir
);
3669 if (chdir (dir
) < 0)
3670 perror_with_name (dir
);
3672 #if defined(_WIN32) || defined(__MSDOS__)
3673 /* There's too much mess with DOSish names like "d:", "d:.",
3674 "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
3675 simply get the canonicalized name of the current directory. */
3676 dir
= getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3680 if (SLASH_P (dir
[len
- 1]))
3682 /* Remove the trailing slash unless this is a root directory
3683 (including a drive letter on non-Unix systems). */
3684 if (!(len
== 1) /* "/" */
3685 #if defined(_WIN32) || defined(__MSDOS__)
3686 && !(!SLASH_P (*dir
) && ROOTED_P (dir
) && len
<= 3) /* "d:/" */
3692 dir
= savestring (dir
, len
);
3694 current_directory
= dir
;
3697 if (SLASH_P (current_directory
[strlen (current_directory
) - 1]))
3698 current_directory
= concat (current_directory
, dir
, NULL
);
3700 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
3704 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3706 found_real_path
= 0;
3707 for (p
= current_directory
; *p
;)
3709 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
3711 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
3712 && (p
[3] == 0 || SLASH_P (p
[3])))
3714 if (found_real_path
)
3716 /* Search backwards for the directory just before the "/.."
3717 and obliterate it and the "/..". */
3719 while (q
!= current_directory
&& !SLASH_P (q
[-1]))
3722 if (q
== current_directory
)
3723 /* current_directory is
3724 a relative pathname ("can't happen"--leave it alone). */
3728 strcpy (q
- 1, p
+ 3);
3733 /* We are dealing with leading repetitions of "/..", for example
3734 "/../..", which is the Mach super-root. */
3739 found_real_path
= 1;
3744 forget_cached_source_info ();
3747 pwd_command ((char *) 0, 1);
3750 struct source_cleanup_lines_args
3754 char *old_pre_error
;
3755 char *old_error_pre_print
;
3759 source_cleanup_lines (args
)
3762 struct source_cleanup_lines_args
*p
=
3763 (struct source_cleanup_lines_args
*) args
;
3764 source_line_number
= p
->old_line
;
3765 source_file_name
= p
->old_file
;
3766 source_pre_error
= p
->old_pre_error
;
3767 error_pre_print
= p
->old_error_pre_print
;
3772 do_fclose_cleanup (void *stream
)
3778 source_command (args
, from_tty
)
3783 struct cleanup
*old_cleanups
;
3785 struct source_cleanup_lines_args old_lines
;
3790 error ("source command requires pathname of file to source.");
3793 file
= tilde_expand (file
);
3794 old_cleanups
= make_cleanup (free
, file
);
3796 stream
= fopen (file
, FOPEN_RT
);
3800 perror_with_name (file
);
3805 make_cleanup (do_fclose_cleanup
, stream
);
3807 old_lines
.old_line
= source_line_number
;
3808 old_lines
.old_file
= source_file_name
;
3809 old_lines
.old_pre_error
= source_pre_error
;
3810 old_lines
.old_error_pre_print
= error_pre_print
;
3811 make_cleanup (source_cleanup_lines
, &old_lines
);
3812 source_line_number
= 0;
3813 source_file_name
= file
;
3814 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
3815 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
3816 make_cleanup (free
, source_pre_error
);
3817 /* This will get set every time we read a line. So it won't stay "" for
3819 error_pre_print
= "";
3821 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
3822 if (source_error_allocated
< needed_length
)
3824 source_error_allocated
*= 2;
3825 if (source_error_allocated
< needed_length
)
3826 source_error_allocated
= needed_length
;
3827 if (source_error
== NULL
)
3828 source_error
= xmalloc (source_error_allocated
);
3830 source_error
= xrealloc (source_error
, source_error_allocated
);
3833 read_command_file (stream
);
3835 do_cleanups (old_cleanups
);
3840 echo_command (text
, from_tty
)
3848 while ((c
= *p
++) != '\0')
3852 /* \ at end of argument is used after spaces
3853 so they won't be lost. */
3857 c
= parse_escape (&p
);
3859 printf_filtered ("%c", c
);
3862 printf_filtered ("%c", c
);
3865 /* Force this output to appear now. */
3867 gdb_flush (gdb_stdout
);
3872 dont_repeat_command (ignored
, from_tty
)
3876 *line
= 0; /* Can't call dont_repeat here because we're not
3877 necessarily reading from stdin. */
3880 /* Functions to manipulate command line editing control variables. */
3882 /* Number of commands to print in each call to show_commands. */
3883 #define Hist_print 10
3885 show_commands (args
, from_tty
)
3889 /* Index for history commands. Relative to history_base. */
3892 /* Number of the history entry which we are planning to display next.
3893 Relative to history_base. */
3896 /* The first command in the history which doesn't exist (i.e. one more
3897 than the number of the last command). Relative to history_base. */
3900 extern HIST_ENTRY
*history_get
PARAMS ((int));
3902 /* Print out some of the commands from the command history. */
3903 /* First determine the length of the history list. */
3904 hist_len
= history_size
;
3905 for (offset
= 0; offset
< history_size
; offset
++)
3907 if (!history_get (history_base
+ offset
))
3916 if (args
[0] == '+' && args
[1] == '\0')
3917 /* "info editing +" should print from the stored position. */
3920 /* "info editing <exp>" should print around command number <exp>. */
3921 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3923 /* "show commands" means print the last Hist_print commands. */
3926 num
= hist_len
- Hist_print
;
3932 /* If there are at least Hist_print commands, we want to display the last
3933 Hist_print rather than, say, the last 6. */
3934 if (hist_len
- num
< Hist_print
)
3936 num
= hist_len
- Hist_print
;
3941 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3943 printf_filtered ("%5d %s\n", history_base
+ offset
,
3944 (history_get (history_base
+ offset
))->line
);
3947 /* The next command we want to display is the next one that we haven't
3951 /* If the user repeats this command with return, it should do what
3952 "show commands +" does. This is unnecessary if arg is null,
3953 because "show commands +" is not useful after "show commands". */
3954 if (from_tty
&& args
)
3961 /* Called by do_setshow_command. */
3964 set_history_size_command (args
, from_tty
, c
)
3967 struct cmd_list_element
*c
;
3969 if (history_size
== INT_MAX
)
3970 unstifle_history ();
3971 else if (history_size
>= 0)
3972 stifle_history (history_size
);
3975 history_size
= INT_MAX
;
3976 error ("History size must be non-negative");
3982 set_history (args
, from_tty
)
3986 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3987 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3992 show_history (args
, from_tty
)
3996 cmd_show_list (showhistlist
, from_tty
, "");
3999 int info_verbose
= 0; /* Default verbose msgs off */
4001 /* Called by do_setshow_command. An elaborate joke. */
4004 set_verbose (args
, from_tty
, c
)
4007 struct cmd_list_element
*c
;
4009 char *cmdname
= "verbose";
4010 struct cmd_list_element
*showcmd
;
4012 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
4016 c
->doc
= "Set verbose printing of informational messages.";
4017 showcmd
->doc
= "Show verbose printing of informational messages.";
4021 c
->doc
= "Set verbosity.";
4022 showcmd
->doc
= "Show verbosity.";
4027 float_handler (signo
)
4030 /* This message is based on ANSI C, section 4.7. Note that integer
4031 divide by zero causes this, so "float" is a misnomer. */
4032 signal (SIGFPE
, float_handler
);
4033 error ("Erroneous arithmetic operation.");
4037 set_debug (arg
, from_tty
)
4041 printf_unfiltered ("\"set debug\" must be followed by the name of a print subcommand.\n");
4042 help_list (setdebuglist
, "set debug ", -1, gdb_stdout
);
4046 show_debug (args
, from_tty
)
4050 cmd_show_list (showdebuglist
, from_tty
, "");
4063 enablebreaklist
= NULL
;
4068 showhistlist
= NULL
;
4069 unsethistlist
= NULL
;
4070 maintenancelist
= NULL
;
4071 maintenanceinfolist
= NULL
;
4072 maintenanceprintlist
= NULL
;
4073 setprintlist
= NULL
;
4074 showprintlist
= NULL
;
4075 setchecklist
= NULL
;
4076 showchecklist
= NULL
;
4079 /* Init the history buffer. Note that we are called after the init file(s)
4080 * have been read so that the user can change the history file via his
4081 * .gdbinit file (for instance). The GDBHISTFILE environment variable
4082 * overrides all of this.
4090 tmpenv
= getenv ("HISTSIZE");
4092 history_size
= atoi (tmpenv
);
4093 else if (!history_size
)
4096 stifle_history (history_size
);
4098 tmpenv
= getenv ("GDBHISTFILE");
4100 history_filename
= savestring (tmpenv
, strlen (tmpenv
));
4101 else if (!history_filename
)
4103 /* We include the current directory so that if the user changes
4104 directories the file written will be the same as the one
4107 /* No leading dots in file names are allowed on MSDOS. */
4108 history_filename
= concat (current_directory
, "/_gdb_history", NULL
);
4110 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
4113 read_history (history_filename
);
4119 struct cmd_list_element
*c
;
4121 /* If we are running the asynchronous version,
4122 we initialize the prompts differently. */
4125 gdb_prompt_string
= savestring (DEFAULT_PROMPT
, strlen (DEFAULT_PROMPT
));
4129 /* initialize the prompt stack to a simple "(gdb) " prompt or to
4130 whatever the DEFAULT_PROMPT is. */
4131 the_prompts
.top
= 0;
4133 PROMPT (0) = savestring (DEFAULT_PROMPT
, strlen (DEFAULT_PROMPT
));
4135 /* Set things up for annotation_level > 1, if the user ever decides
4137 async_annotation_suffix
= "prompt";
4138 /* Set the variable associated with the setshow prompt command. */
4139 new_async_prompt
= savestring (PROMPT (0), strlen (PROMPT (0)));
4141 gdb_prompt_escape
= 0; /* default to none. */
4143 /* Set the important stuff up for command editing. */
4144 command_editing_p
= 1;
4145 history_expansion_p
= 0;
4146 write_history_p
= 0;
4148 /* Setup important stuff for command line editing. */
4149 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
4150 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
4151 rl_completer_quote_characters
= gdb_completer_quote_characters
;
4152 rl_readline_name
= "gdb";
4154 /* Define the classes of commands.
4155 They will appear in the help list in the reverse of this order. */
4157 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
4158 "Maintenance commands.\n\
4159 Some gdb commands are provided just for use by gdb maintainers.\n\
4160 These commands are subject to frequent change, and may not be as\n\
4161 well documented as user commands.",
4163 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
4164 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
4165 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
4166 The commands in this class are those defined by the user.\n\
4167 Use the \"define\" command to define a command.", &cmdlist
);
4168 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
4170 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
4171 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
4172 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
4173 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
4174 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
4175 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
4176 counting from zero for the innermost (currently executing) frame.\n\n\
4177 At any time gdb identifies one frame as the \"selected\" frame.\n\
4178 Variable lookups are done with respect to the selected frame.\n\
4179 When the program being debugged stops, gdb selects the innermost frame.\n\
4180 The commands below can be used to select other frames by number or address.",
4182 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
4184 add_com ("pwd", class_files
, pwd_command
,
4185 "Print working directory. This is used for your program as well.");
4186 c
= add_cmd ("cd", class_files
, cd_command
,
4187 "Set working directory to DIR for debugger and program being debugged.\n\
4188 The change does not take effect for the program being debugged\n\
4189 until the next time it is started.", &cmdlist
);
4190 c
->completer
= filename_completer
;
4192 /* The set prompt command is different depending whether or not the
4193 async version is run. NOTE: this difference is going to
4194 disappear as we make the event loop be the default engine of
4199 (add_set_cmd ("prompt", class_support
, var_string
,
4200 (char *) &gdb_prompt_string
, "Set gdb's prompt",
4206 c
= add_set_cmd ("prompt", class_support
, var_string
,
4207 (char *) &new_async_prompt
, "Set gdb's prompt",
4209 add_show_from_set (c
, &showlist
);
4210 c
->function
.sfunc
= set_async_prompt
;
4214 (add_set_cmd ("prompt-escape-char", class_support
, var_zinteger
,
4215 (char *) &gdb_prompt_escape
,
4216 "Set escape character for formatting of gdb's prompt",
4220 add_com ("echo", class_support
, echo_command
,
4221 "Print a constant string. Give string as argument.\n\
4222 C escape sequences may be used in the argument.\n\
4223 No newline is added at the end of the argument;\n\
4224 use \"\\n\" if you want a newline to be printed.\n\
4225 Since leading and trailing whitespace are ignored in command arguments,\n\
4226 if you want to print some you must use \"\\\" before leading whitespace\n\
4227 to be printed or after trailing whitespace.");
4228 add_com ("document", class_support
, document_command
,
4229 "Document a user-defined command.\n\
4230 Give command name as argument. Give documentation on following lines.\n\
4231 End with a line of just \"end\".");
4232 add_com ("define", class_support
, define_command
,
4233 "Define a new command name. Command name is argument.\n\
4234 Definition appears on following lines, one command per line.\n\
4235 End with a line of just \"end\".\n\
4236 Use the \"document\" command to give documentation for the new command.\n\
4237 Commands defined in this way may have up to ten arguments.");
4240 c
= add_cmd ("source", class_support
, source_command
,
4241 "Read commands from a file named FILE.\n\
4242 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
4243 when gdb is started.", &cmdlist
);
4245 /* Punt file name, we can't help it easily. */
4246 c
= add_cmd ("source", class_support
, source_command
,
4247 "Read commands from a file named FILE.\n\
4248 Note that the file \".gdbinit\" is read automatically in this way\n\
4249 when gdb is started.", &cmdlist
);
4251 c
->completer
= filename_completer
;
4253 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
4254 add_com ("help", class_support
, help_command
, "Print list of commands.");
4255 add_com_alias ("q", "quit", class_support
, 1);
4256 add_com_alias ("h", "help", class_support
, 1);
4258 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
4259 Primarily used inside of user-defined commands that should not be repeated when\n\
4262 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *) &info_verbose
,
4265 add_show_from_set (c
, &showlist
);
4266 c
->function
.sfunc
= set_verbose
;
4267 set_verbose (NULL
, 0, c
);
4269 /* The set editing command is different depending whether or not the
4270 async version is run. NOTE: this difference is going to disappear
4271 as we make the event loop be the default engine of gdb. */
4275 (add_set_cmd ("editing", class_support
, var_boolean
, (char *) &command_editing_p
,
4276 "Set editing of command lines as they are typed.\n\
4277 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4278 Without an argument, command line editing is enabled. To edit, use\n\
4279 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
4284 c
= add_set_cmd ("editing", class_support
, var_boolean
, (char *) &async_command_editing_p
,
4285 "Set editing of command lines as they are typed.\n\
4286 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4287 Without an argument, command line editing is enabled. To edit, use\n\
4288 EMACS-like or VI-like commands like control-P or ESC.", &setlist
);
4290 add_show_from_set (c
, &showlist
);
4291 c
->function
.sfunc
= set_async_editing_command
;
4294 add_prefix_cmd ("history", class_support
, set_history
,
4295 "Generic command for setting command history parameters.",
4296 &sethistlist
, "set history ", 0, &setlist
);
4297 add_prefix_cmd ("history", class_support
, show_history
,
4298 "Generic command for showing command history parameters.",
4299 &showhistlist
, "show history ", 0, &showlist
);
4302 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *) &history_expansion_p
,
4303 "Set history expansion on command input.\n\
4304 Without an argument, history expansion is enabled.", &sethistlist
),
4308 (add_set_cmd ("save", no_class
, var_boolean
, (char *) &write_history_p
,
4309 "Set saving of the history record on exit.\n\
4310 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
4311 Without an argument, saving is enabled.", &sethistlist
),
4314 c
= add_set_cmd ("size", no_class
, var_integer
, (char *) &history_size
,
4315 "Set the size of the command history, \n\
4316 ie. the number of previous commands to keep a record of.", &sethistlist
);
4317 add_show_from_set (c
, &showhistlist
);
4318 c
->function
.sfunc
= set_history_size_command
;
4321 (add_set_cmd ("filename", no_class
, var_filename
, (char *) &history_filename
,
4322 "Set the filename in which to record the command history\n\
4323 (the list of previous commands of which a record is kept).", &sethistlist
),
4327 (add_set_cmd ("confirm", class_support
, var_boolean
,
4329 "Set whether to confirm potentially dangerous operations.",
4333 add_prefix_cmd ("info", class_info
, info_command
,
4334 "Generic command for showing things about the program being debugged.",
4335 &infolist
, "info ", 0, &cmdlist
);
4336 add_com_alias ("i", "info", class_info
, 1);
4338 add_com ("complete", class_obscure
, complete_command
,
4339 "List the completions for the rest of the line as a command.");
4341 add_prefix_cmd ("show", class_info
, show_command
,
4342 "Generic command for showing things about the debugger.",
4343 &showlist
, "show ", 0, &cmdlist
);
4344 /* Another way to get at the same thing. */
4345 add_info ("set", show_command
, "Show all GDB settings.");
4347 add_cmd ("commands", no_class
, show_commands
,
4348 "Show the history of commands you typed.\n\
4349 You can supply a command number to start with, or a `+' to start after\n\
4350 the previous command number shown.",
4353 add_cmd ("version", no_class
, show_version
,
4354 "Show what version of GDB this is.", &showlist
);
4356 add_com ("while", class_support
, while_command
,
4357 "Execute nested commands WHILE the conditional expression is non zero.\n\
4358 The conditional expression must follow the word `while' and must in turn be\n\
4359 followed by a new line. The nested commands must be entered one per line,\n\
4360 and should be terminated by the word `end'.");
4362 add_com ("if", class_support
, if_command
,
4363 "Execute nested commands once IF the conditional expression is non zero.\n\
4364 The conditional expression must follow the word `if' and must in turn be\n\
4365 followed by a new line. The nested commands must be entered one per line,\n\
4366 and should be terminated by the word 'else' or `end'. If an else clause\n\
4367 is used, the same rules apply to its nested commands as to the first ones.");
4369 /* If target is open when baud changes, it doesn't take effect until the
4370 next open (I think, not sure). */
4371 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
4372 var_zinteger
, (char *) &baud_rate
,
4373 "Set baud rate for remote serial I/O.\n\
4374 This value is used to set the speed of the serial port when debugging\n\
4375 using remote targets.", &setlist
),
4378 c
= add_set_cmd ("remotedebug", no_class
, var_zinteger
,
4379 (char *) &remote_debug
,
4380 "Set debugging of remote protocol.\n\
4381 When enabled, each packet sent or received with the remote target\n\
4382 is displayed.", &setlist
);
4383 deprecate_cmd (c
, "set debug remote");
4384 deprecate_cmd (add_show_from_set (c
, &showlist
), "show debug remote");
4386 add_show_from_set (add_set_cmd ("remote", no_class
, var_zinteger
,
4387 (char *) &remote_debug
,
4388 "Set debugging of remote protocol.\n\
4389 When enabled, each packet sent or received with the remote target\n\
4390 is displayed.", &setdebuglist
),
4394 add_set_cmd ("remotetimeout", no_class
, var_integer
, (char *) &remote_timeout
,
4395 "Set timeout limit to wait for target to respond.\n\
4396 This value is used to set the time limit for gdb to wait for a response\n\
4397 from the target.", &setlist
),
4400 /* The set annotate command is different depending whether or not
4401 the async version is run. NOTE: this difference is going to
4402 disappear as we make the event loop be the default engine of
4406 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
4407 (char *) &annotation_level
, "Set annotation_level.\n\
4408 0 == normal; 1 == fullname (for use when running under emacs)\n\
4409 2 == output annotated suitably for use by programs that control GDB.",
4411 c
= add_show_from_set (c
, &showlist
);
4415 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
4416 (char *) &annotation_level
, "Set annotation_level.\n\
4417 0 == normal; 1 == fullname (for use when running under emacs)\n\
4418 2 == output annotated suitably for use by programs that control GDB.",
4420 add_show_from_set (c
, &showlist
);
4421 c
->function
.sfunc
= set_async_annotation_level
;
4426 (add_set_cmd ("exec-done-display", class_support
, var_boolean
, (char *) &exec_done_display_p
,
4427 "Set notification of completion for asynchronous execution commands.\n\
4428 Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist
),
4431 add_prefix_cmd ("debug", no_class
, set_debug
,
4432 "Generic command for setting gdb debugging flags",
4433 &setdebuglist
, "set debug ", 0, &setlist
);
4435 add_prefix_cmd ("debug", no_class
, show_debug
,
4436 "Generic command for showing gdb debugging flags",
4437 &showdebuglist
, "show debug ", 0, &showlist
);