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 (char *, int);
62 static void source_cleanup_lines (PTR
);
64 static void user_defined_command (char *, int);
66 static void init_signals (void);
69 static void stop_sig (int);
72 static char *line_completion_function (char *, int, char *, int);
74 static char *readline_line_completion_function (char *, int);
76 static void while_command (char *, int);
78 static void if_command (char *, int);
80 static struct command_line
*build_command_line (enum command_control_type
,
83 static struct command_line
*get_command_line (enum command_control_type
,
86 static void realloc_body_list (struct command_line
*, int);
88 static enum misc_command_type
read_next_line (struct command_line
**);
90 static enum command_control_type
91 recurse_read_control_structure (struct command_line
*);
93 static struct cleanup
*setup_user_args (char *);
95 static char *locate_arg (char *);
97 static char *insert_args (char *);
99 static void arg_cleanup (void *);
101 static void init_main (void);
103 static void init_cmd_lists (void);
105 static void float_handler (int);
107 static void init_signals (void);
109 static void set_verbose (char *, int, struct cmd_list_element
*);
111 static void show_history (char *, int);
113 static void set_history (char *, int);
115 static void set_history_size_command (char *, int, struct cmd_list_element
*);
117 static void show_commands (char *, int);
119 static void echo_command (char *, int);
121 static void pwd_command (char *, int);
123 static void show_version (char *, int);
125 static void document_command (char *, int);
127 static void define_command (char *, int);
129 static void validate_comname (char *);
131 static void help_command (char *, int);
133 static void show_command (char *, int);
135 static void info_command (char *, int);
137 static void complete_command (char *, int);
139 static void do_nothing (int);
141 static void show_debug (char *, int);
143 static void set_debug (char *, int);
146 /* NOTE 1999-04-29: This function will be static again, once we modify
147 gdb to use the event loop as the default command loop and we merge
148 event-top.c into this file, top.c */
149 /* static */ int quit_cover (PTR
);
151 static void disconnect (int);
154 static void do_restore_instream_cleanup (void *stream
);
156 static struct cleanup
*make_cleanup_free_command_lines (struct command_line
**);
158 /* Default command line prompt. This is overriden in some configs. */
160 #ifndef DEFAULT_PROMPT
161 #define DEFAULT_PROMPT "(gdb) "
164 /* Initialization file name for gdb. This is overridden in some configs. */
166 #ifndef GDBINIT_FILENAME
167 #define GDBINIT_FILENAME ".gdbinit"
169 char gdbinit
[] = GDBINIT_FILENAME
;
171 int inhibit_gdbinit
= 0;
173 /* If nonzero, and GDB has been configured to be able to use windows,
174 attempt to open them upon startup. */
178 extern char lang_frame_mismatch_warn
[]; /* language.c */
180 /* Flag for whether we want all the "from_tty" gubbish printed. */
182 int caution
= 1; /* Default is yes, sigh. */
184 /* Define all cmd_list_elements. */
186 /* Chain containing all defined commands. */
188 struct cmd_list_element
*cmdlist
;
190 /* Chain containing all defined info subcommands. */
192 struct cmd_list_element
*infolist
;
194 /* Chain containing all defined enable subcommands. */
196 struct cmd_list_element
*enablelist
;
198 /* Chain containing all defined disable subcommands. */
200 struct cmd_list_element
*disablelist
;
202 /* Chain containing all defined toggle subcommands. */
204 struct cmd_list_element
*togglelist
;
206 /* Chain containing all defined stop subcommands. */
208 struct cmd_list_element
*stoplist
;
210 /* Chain containing all defined delete subcommands. */
212 struct cmd_list_element
*deletelist
;
214 /* Chain containing all defined "enable breakpoint" subcommands. */
216 struct cmd_list_element
*enablebreaklist
;
218 /* Chain containing all defined set subcommands */
220 struct cmd_list_element
*setlist
;
222 /* Chain containing all defined unset subcommands */
224 struct cmd_list_element
*unsetlist
;
226 /* Chain containing all defined show subcommands. */
228 struct cmd_list_element
*showlist
;
230 /* Chain containing all defined \"set history\". */
232 struct cmd_list_element
*sethistlist
;
234 /* Chain containing all defined \"show history\". */
236 struct cmd_list_element
*showhistlist
;
238 /* Chain containing all defined \"unset history\". */
240 struct cmd_list_element
*unsethistlist
;
242 /* Chain containing all defined maintenance subcommands. */
244 struct cmd_list_element
*maintenancelist
;
246 /* Chain containing all defined "maintenance info" subcommands. */
248 struct cmd_list_element
*maintenanceinfolist
;
250 /* Chain containing all defined "maintenance print" subcommands. */
252 struct cmd_list_element
*maintenanceprintlist
;
254 struct cmd_list_element
*setprintlist
;
256 struct cmd_list_element
*showprintlist
;
258 struct cmd_list_element
*setdebuglist
;
260 struct cmd_list_element
*showdebuglist
;
262 struct cmd_list_element
*setchecklist
;
264 struct cmd_list_element
*showchecklist
;
266 /* stdio stream that command input is being read from. Set to stdin normally.
267 Set by source_command to the file we are sourcing. Set to NULL if we are
268 executing a user-defined command or interacting via a GUI. */
272 /* Current working directory. */
274 char *current_directory
;
276 /* The directory name is actually stored here (usually). */
277 char gdb_dirbuf
[1024];
279 /* Function to call before reading a command, if nonzero.
280 The function receives two args: an input stream,
281 and a prompt string. */
283 void (*window_hook
) (FILE *, char *);
288 /* gdb prints this when reading a command interactively */
289 static char *gdb_prompt_string
; /* the global prompt string */
290 extern char *get_prompt (void); /* access function for prompt string */
292 /* Buffer used for reading command lines, and the size
293 allocated for it so far. */
298 /* Nonzero if the current command is modified by "server ". This
299 affects things like recording into the command history, commands
300 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
301 whatever) can issue its own commands and also send along commands
302 from the user, and have the user not notice that the user interface
303 is issuing commands too. */
306 /* Baud rate specified for talking to serial target systems. Default
307 is left as -1, so targets can choose their own defaults. */
308 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
309 or (unsigned int)-1. This is a Bad User Interface. */
313 /* Timeout limit for response from target. */
315 /* The default value has been changed many times over the years. It
316 was originally 5 seconds. But that was thought to be a long time
317 to sit and wait, so it was changed to 2 seconds. That was thought
318 to be plenty unless the connection was going through some terminal
319 server or multiplexer or other form of hairy serial connection.
321 In mid-1996, remote_timeout was moved from remote.c to top.c and
322 it began being used in other remote-* targets. It appears that the
323 default was changed to 20 seconds at that time, perhaps because the
324 Hitachi E7000 ICE didn't always respond in a timely manner.
326 But if 5 seconds is a long time to sit and wait for retransmissions,
327 20 seconds is far worse. This demonstrates the difficulty of using
328 a single variable for all protocol timeouts.
330 As remote.c is used much more than remote-e7000.c, it was changed
331 back to 2 seconds in 1999. */
333 int remote_timeout
= 2;
335 /* Non-zero tells remote* modules to output debugging info. */
337 int remote_debug
= 0;
339 /* Non-zero means the target is running. Note: this is different from
340 saying that there is an active target and we are stopped at a
341 breakpoint, for instance. This is a real indicator whether the
342 target is off and running, which gdb is doing something else. */
343 int target_executing
= 0;
345 /* Level of control structure. */
346 static int control_level
;
348 /* Structure for arguments to user defined functions. */
349 #define MAXUSERARGS 10
352 struct user_args
*next
;
363 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
367 #define STOP_SIGNAL SIGTSTP
368 static void stop_sig (int);
372 /* Some System V have job control but not sigsetmask(). */
373 #if !defined (HAVE_SIGSETMASK)
375 #define HAVE_SIGSETMASK 1
377 #define HAVE_SIGSETMASK 0
381 #if 0 == (HAVE_SIGSETMASK)
382 #define sigsetmask(n)
385 /* Hooks for alternate command interfaces. */
387 /* Called after most modules have been initialized, but before taking users
390 void (*init_ui_hook
) (char *argv0
);
392 /* This hook is called from within gdb's many mini-event loops which could
393 steal control from a real user interface's event loop. It returns
394 non-zero if the user is requesting a detach, zero otherwise. */
396 int (*ui_loop_hook
) (int);
398 /* Called instead of command_loop at top level. Can be invoked via
399 return_to_top_level. */
401 void (*command_loop_hook
) (void);
404 /* Called from print_frame_info to list the line we stopped in. */
406 void (*print_frame_info_listing_hook
) (struct symtab
* s
, int line
,
407 int stopline
, int noerror
);
408 /* Replaces most of query. */
410 int (*query_hook
) (const char *, va_list);
412 /* Replaces most of warning. */
414 void (*warning_hook
) (const char *, va_list);
416 /* These three functions support getting lines of text from the user. They
417 are used in sequence. First readline_begin_hook is called with a text
418 string that might be (for example) a message for the user to type in a
419 sequence of commands to be executed at a breakpoint. If this function
420 calls back to a GUI, it might take this opportunity to pop up a text
421 interaction window with this message. Next, readline_hook is called
422 with a prompt that is emitted prior to collecting the user input.
423 It can be called multiple times. Finally, readline_end_hook is called
424 to notify the GUI that we are done with the interaction window and it
427 void (*readline_begin_hook
) (char *, ...);
428 char *(*readline_hook
) (char *);
429 void (*readline_end_hook
) (void);
431 /* Called as appropriate to notify the interface of the specified breakpoint
434 void (*create_breakpoint_hook
) (struct breakpoint
* bpt
);
435 void (*delete_breakpoint_hook
) (struct breakpoint
* bpt
);
436 void (*modify_breakpoint_hook
) (struct breakpoint
* bpt
);
438 /* Called as appropriate to notify the interface that we have attached
439 to or detached from an already running process. */
441 void (*attach_hook
) (void);
442 void (*detach_hook
) (void);
444 /* Called during long calculations to allow GUI to repair window damage, and to
445 check for stop buttons, etc... */
447 void (*interactive_hook
) (void);
449 /* Called when the registers have changed, as a hint to a GUI
450 to minimize window update. */
452 void (*registers_changed_hook
) (void);
454 /* Tell the GUI someone changed the register REGNO. -1 means
455 that the caller does not know which register changed or
456 that several registers have changed (see value_assign). */
457 void (*register_changed_hook
) (int regno
);
459 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
460 void (*memory_changed_hook
) (CORE_ADDR addr
, int len
);
462 /* Called when going to wait for the target. Usually allows the GUI to run
463 while waiting for target events. */
465 int (*target_wait_hook
) (int pid
, struct target_waitstatus
* status
);
467 /* Used by UI as a wrapper around command execution. May do various things
468 like enabling/disabling buttons, etc... */
470 void (*call_command_hook
) (struct cmd_list_element
* c
, char *cmd
,
473 /* Called after a `set' command has finished. Is only run if the
474 `set' command succeeded. */
476 void (*set_hook
) (struct cmd_list_element
* c
);
478 /* Called when the current thread changes. Argument is thread id. */
480 void (*context_hook
) (int id
);
482 /* Takes control from error (). Typically used to prevent longjmps out of the
483 middle of the GUI. Usually used in conjunction with a catch routine. */
485 NORETURN
void (*error_hook
) (void) ATTR_NORETURN
;
488 /* One should use catch_errors rather than manipulating these
490 #if defined(HAVE_SIGSETJMP)
491 #define SIGJMP_BUF sigjmp_buf
492 #define SIGSETJMP(buf) sigsetjmp(buf, 1)
493 #define SIGLONGJMP(buf,val) siglongjmp(buf,val)
495 #define SIGJMP_BUF jmp_buf
496 #define SIGSETJMP(buf) setjmp(buf)
497 #define SIGLONGJMP(buf,val) longjmp(buf,val)
500 /* Where to go for return_to_top_level. */
501 static SIGJMP_BUF
*catch_return
;
503 /* Return for reason REASON to the nearest containing catch_errors(). */
506 return_to_top_level (enum return_reason reason
)
511 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
512 I can think of a reason why that is vital, though). */
513 bpstat_clear_actions (stop_bpstat
); /* Clear queued breakpoint commands */
515 disable_current_display ();
516 do_cleanups (ALL_CLEANUPS
);
517 if (event_loop_p
&& target_can_async_p () && !target_executing
)
518 do_exec_cleanups (ALL_CLEANUPS
);
519 if (event_loop_p
&& sync_execution
)
520 do_exec_error_cleanups (ALL_CLEANUPS
);
522 if (annotation_level
> 1)
533 /* Jump to the containing catch_errors() call, communicating REASON
534 to that call via setjmp's return value. Note that REASON can't
535 be zero, by definition in defs.h. */
537 (NORETURN
void) SIGLONGJMP (*catch_return
, (int) reason
);
540 /* Call FUNC with arg ARGS, catching any errors. If there is no
541 error, return the value returned by FUNC. If there is an error,
542 print ERRSTRING, print the specific error message, then return
545 Must not be called with immediate_quit in effect (bad things might
546 happen, say we got a signal in the middle of a memcpy to quit_return).
547 This is an OK restriction; with very few exceptions immediate_quit can
548 be replaced by judicious use of QUIT.
550 MASK specifies what to catch; it is normally set to
551 RETURN_MASK_ALL, if for no other reason than that the code which
552 calls catch_errors might not be set up to deal with a quit which
553 isn't caught. But if the code can deal with it, it generally
554 should be RETURN_MASK_ERROR, unless for some reason it is more
555 useful to abort only the portion of the operation inside the
556 catch_errors. Note that quit should return to the command line
557 fairly quickly, even if some further processing is being done. */
559 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
560 error() et.al. could maintain a set of flags that indicate the the
561 current state of each of the longjmp buffers. This would give the
562 longjmp code the chance to detect a longjmp botch (before it gets
563 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
564 code also randomly used a SET_TOP_LEVEL macro that directly
565 initialize the longjmp buffers. */
567 /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
568 be consolidated into a single file instead of being distributed
569 between utils.c and top.c? */
572 catch_errors (catch_errors_ftype
*func
, PTR args
, char *errstring
,
575 SIGJMP_BUF
*saved_catch
;
578 struct cleanup
*saved_cleanup_chain
;
579 char *saved_error_pre_print
;
580 char *saved_quit_pre_print
;
582 /* Return value from SIGSETJMP(): enum return_reason if error or
583 quit caught, 0 otherwise. */
586 /* Override error/quit messages during FUNC. */
588 saved_error_pre_print
= error_pre_print
;
589 saved_quit_pre_print
= quit_pre_print
;
591 if (mask
& RETURN_MASK_ERROR
)
592 error_pre_print
= errstring
;
593 if (mask
& RETURN_MASK_QUIT
)
594 quit_pre_print
= errstring
;
596 /* Prevent error/quit during FUNC from calling cleanups established
599 saved_cleanup_chain
= save_cleanups ();
601 /* Call FUNC, catching error/quit events. */
603 saved_catch
= catch_return
;
604 catch_return
= &catch;
605 caught
= SIGSETJMP (catch);
607 val
= (*func
) (args
);
610 catch_return
= saved_catch
;
612 /* FIXME: cagney/1999-11-05: A correct FUNC implementation will
613 clean things up (restoring the cleanup chain) to the state they
614 were just prior to the call. Unfortunately, many FUNC's are not
615 that well behaved. This could be fixed by adding either a
616 do_cleanups call (to cover the problem) or an assertion check to
617 detect bad FUNCs code. */
619 /* Restore the cleanup chain and error/quit messages to their
622 restore_cleanups (saved_cleanup_chain
);
624 if (mask
& RETURN_MASK_QUIT
)
625 quit_pre_print
= saved_quit_pre_print
;
626 if (mask
& RETURN_MASK_ERROR
)
627 error_pre_print
= saved_error_pre_print
;
629 /* Return normally if no error/quit event occurred. */
634 /* If the caller didn't request that the event be caught, relay the
635 event to the next containing catch_errors(). */
637 if (!(mask
& RETURN_MASK (caught
)))
638 return_to_top_level (caught
);
640 /* Tell the caller that an event was caught.
642 FIXME: nsd/2000-02-22: When MASK is RETURN_MASK_ALL, the caller
643 can't tell what type of event occurred.
645 A possible fix is to add a new interface, catch_event(), that
646 returns enum return_reason after catching an error or a quit.
648 When returning normally, i.e. without catching an error or a
649 quit, catch_event() could return RETURN_NORMAL, which would be
650 added to enum return_reason. FUNC would return information
651 exclusively via ARGS.
653 Alternatively, normal catch_event() could return FUNC's return
654 value. The caller would need to be aware of potential overlap
655 with enum return_reason, which could be publicly restricted to
656 negative values to simplify return value processing in FUNC and
662 struct captured_command_args
664 catch_command_errors_ftype
*command
;
670 do_captured_command (void *data
)
672 struct captured_command_args
*context
= data
;
673 context
->command (context
->arg
, context
->from_tty
);
674 /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
675 isn't needed. Instead an assertion check could be made that
676 simply confirmed that the called function correctly cleaned up
677 after itself. Unfortunately, old code (prior to 1999-11-04) in
678 main.c was calling SET_TOP_LEVEL(), calling the command function,
679 and then *always* calling do_cleanups(). For the moment we
680 remain ``bug compatible'' with that old code.. */
681 do_cleanups (ALL_CLEANUPS
);
686 catch_command_errors (catch_command_errors_ftype
* command
,
687 char *arg
, int from_tty
, return_mask mask
)
689 struct captured_command_args args
;
690 args
.command
= command
;
692 args
.from_tty
= from_tty
;
693 return catch_errors (do_captured_command
, &args
, "", mask
);
697 /* Handler for SIGHUP. */
701 disconnect (int signo
)
703 catch_errors (quit_cover
, NULL
,
704 "Could not kill the program being debugged", RETURN_MASK_ALL
);
705 signal (SIGHUP
, SIG_DFL
);
706 kill (getpid (), SIGHUP
);
709 /* Just a little helper function for disconnect(). */
711 /* NOTE 1999-04-29: This function will be static again, once we modify
712 gdb to use the event loop as the default command loop and we merge
713 event-top.c into this file, top.c */
717 caution
= 0; /* Throw caution to the wind -- we're exiting.
718 This prevents asking the user dumb questions. */
719 quit_command ((char *) 0, 0);
722 #endif /* defined SIGHUP */
724 /* Line number we are currently in in a file which is being sourced. */
725 /* NOTE 1999-04-29: This variable will be static again, once we modify
726 gdb to use the event loop as the default command loop and we merge
727 event-top.c into this file, top.c */
728 /* static */ int source_line_number
;
730 /* Name of the file we are sourcing. */
731 /* NOTE 1999-04-29: This variable will be static again, once we modify
732 gdb to use the event loop as the default command loop and we merge
733 event-top.c into this file, top.c */
734 /* static */ char *source_file_name
;
736 /* Buffer containing the error_pre_print used by the source stuff.
738 /* NOTE 1999-04-29: This variable will be static again, once we modify
739 gdb to use the event loop as the default command loop and we merge
740 event-top.c into this file, top.c */
741 /* static */ char *source_error
;
742 static int source_error_allocated
;
744 /* Something to glom on to the start of error_pre_print if source_file_name
746 /* NOTE 1999-04-29: This variable will be static again, once we modify
747 gdb to use the event loop as the default command loop and we merge
748 event-top.c into this file, top.c */
749 /* static */ char *source_pre_error
;
751 /* Clean up on error during a "source" command (or execution of a
752 user-defined command). */
755 do_restore_instream_cleanup (void *stream
)
757 /* Restore the previous input stream. */
761 /* Read commands from STREAM. */
763 read_command_file (FILE *stream
)
765 struct cleanup
*cleanups
;
767 cleanups
= make_cleanup (do_restore_instream_cleanup
, instream
);
770 do_cleanups (cleanups
);
773 extern void init_proc (void);
775 void (*pre_init_ui_hook
) (void);
779 do_chdir_cleanup (void *old_dir
)
787 gdb_init (char *argv0
)
789 if (pre_init_ui_hook
)
792 /* Run the init function of each source file */
794 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
795 current_directory
= gdb_dirbuf
;
798 /* Make sure we return to the original directory upon exit, come
799 what may, since the OS doesn't do that for us. */
800 make_final_cleanup (do_chdir_cleanup
, xstrdup (current_directory
));
803 init_cmd_lists (); /* This needs to be done first */
804 initialize_targets (); /* Setup target_terminal macros for utils.c */
805 initialize_utils (); /* Make errors and warnings possible */
806 initialize_all_files ();
807 initialize_current_architecture ();
808 init_main (); /* But that omits this file! Do it now */
810 /* The signal handling mechanism is different depending whether or
811 not the async version is run. NOTE: in the future we plan to make
812 the event loop be the default engine of gdb, and this difference
815 async_init_signals ();
819 /* We need a default language for parsing expressions, so simple things like
820 "set width 0" won't fail if no language is explicitly set in a config file
821 or implicitly set by reading an executable during startup. */
822 set_language (language_c
);
823 expected_language
= current_language
; /* don't warn about the change. */
826 /* Install the default UI */
827 uiout
= cli_out_new (gdb_stdout
);
831 /* All the interpreters should have had a look at things by now.
832 Initialize the selected interpreter. */
833 if (interpreter_p
&& !init_ui_hook
)
835 fprintf_unfiltered (gdb_stderr
, "Interpreter `%s' unrecognized.\n",
842 init_ui_hook (argv0
);
845 /* Allocate, initialize a new command line structure for one of the
846 control commands (if/while). */
848 static struct command_line
*
849 build_command_line (enum command_control_type type
, char *args
)
851 struct command_line
*cmd
;
854 error ("if/while commands require arguments.\n");
856 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
858 cmd
->control_type
= type
;
862 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
864 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
865 cmd
->line
= savestring (args
, strlen (args
));
869 /* Build and return a new command structure for the control commands
870 such as "if" and "while". */
872 static struct command_line
*
873 get_command_line (enum command_control_type type
, char *arg
)
875 struct command_line
*cmd
;
876 struct cleanup
*old_chain
= NULL
;
878 /* Allocate and build a new command line structure. */
879 cmd
= build_command_line (type
, arg
);
881 old_chain
= make_cleanup_free_command_lines (&cmd
);
883 /* Read in the body of this command. */
884 if (recurse_read_control_structure (cmd
) == invalid_control
)
886 warning ("error reading in control structure\n");
887 do_cleanups (old_chain
);
891 discard_cleanups (old_chain
);
895 /* Recursively print a command (including full control structures). */
898 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
901 struct command_line
*list
;
908 ui_out_spaces (uiout
, 2 * depth
);
910 /* A simple command, print it and continue. */
911 if (list
->control_type
== simple_control
)
913 ui_out_field_string (uiout
, NULL
, list
->line
);
914 ui_out_text (uiout
, "\n");
919 /* loop_continue to jump to the start of a while loop, print it
921 if (list
->control_type
== continue_control
)
923 ui_out_field_string (uiout
, NULL
, "loop_continue");
924 ui_out_text (uiout
, "\n");
929 /* loop_break to break out of a while loop, print it and continue. */
930 if (list
->control_type
== break_control
)
932 ui_out_field_string (uiout
, NULL
, "loop_break");
933 ui_out_text (uiout
, "\n");
938 /* A while command. Recursively print its subcommands and continue. */
939 if (list
->control_type
== while_control
)
941 ui_out_text (uiout
, "while ");
942 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
943 ui_out_text (uiout
, "\n");
944 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
945 ui_out_field_string (uiout
, NULL
, "end");
947 ui_out_spaces (uiout
, 2 * depth
);
948 ui_out_text (uiout
, "end\n");
953 /* An if command. Recursively print both arms before continueing. */
954 if (list
->control_type
== if_control
)
956 ui_out_text (uiout
, "if ");
957 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
958 ui_out_text (uiout
, "\n");
960 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
962 /* Show the false arm if it exists. */
963 if (list
->body_count
== 2)
966 ui_out_spaces (uiout
, 2 * depth
);
967 ui_out_field_string (uiout
, NULL
, "else");
968 ui_out_text (uiout
, "else\n");
969 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
972 ui_out_field_string (uiout
, NULL
, "end");
974 ui_out_spaces (uiout
, 2 * depth
);
975 ui_out_text (uiout
, "end\n");
980 /* ignore illegal command type and try next */
986 print_command_line (struct command_line
*cmd
, unsigned int depth
,
987 struct ui_file
*stream
)
993 for (i
= 0; i
< depth
; i
++)
994 fputs_filtered (" ", stream
);
997 /* A simple command, print it and return. */
998 if (cmd
->control_type
== simple_control
)
1000 fputs_filtered (cmd
->line
, stream
);
1001 fputs_filtered ("\n", stream
);
1005 /* loop_continue to jump to the start of a while loop, print it
1007 if (cmd
->control_type
== continue_control
)
1009 fputs_filtered ("loop_continue\n", stream
);
1013 /* loop_break to break out of a while loop, print it and return. */
1014 if (cmd
->control_type
== break_control
)
1016 fputs_filtered ("loop_break\n", stream
);
1020 /* A while command. Recursively print its subcommands before returning. */
1021 if (cmd
->control_type
== while_control
)
1023 struct command_line
*list
;
1024 fputs_filtered ("while ", stream
);
1025 fputs_filtered (cmd
->line
, stream
);
1026 fputs_filtered ("\n", stream
);
1027 list
= *cmd
->body_list
;
1030 print_command_line (list
, depth
+ 1, stream
);
1035 /* An if command. Recursively print both arms before returning. */
1036 if (cmd
->control_type
== if_control
)
1038 fputs_filtered ("if ", stream
);
1039 fputs_filtered (cmd
->line
, stream
);
1040 fputs_filtered ("\n", stream
);
1042 print_command_line (cmd
->body_list
[0], depth
+ 1, stream
);
1044 /* Show the false arm if it exists. */
1045 if (cmd
->body_count
== 2)
1049 for (i
= 0; i
< depth
; i
++)
1050 fputs_filtered (" ", stream
);
1052 fputs_filtered ("else\n", stream
);
1053 print_command_line (cmd
->body_list
[1], depth
+ 1, stream
);
1057 for (i
= 0; i
< depth
; i
++)
1058 fputs_filtered (" ", stream
);
1060 fputs_filtered ("end\n", stream
);
1065 /* Execute the command in CMD. */
1067 enum command_control_type
1068 execute_control_command (struct command_line
*cmd
)
1070 struct expression
*expr
;
1071 struct command_line
*current
;
1072 struct cleanup
*old_chain
= 0;
1076 enum command_control_type ret
;
1079 switch (cmd
->control_type
)
1081 case simple_control
:
1082 /* A simple command, execute it and return. */
1083 new_line
= insert_args (cmd
->line
);
1085 return invalid_control
;
1086 old_chain
= make_cleanup (free_current_contents
, &new_line
);
1087 execute_command (new_line
, 0);
1088 ret
= cmd
->control_type
;
1091 case continue_control
:
1093 /* Return for "continue", and "break" so we can either
1094 continue the loop at the top, or break out. */
1095 ret
= cmd
->control_type
;
1100 /* Parse the loop control expression for the while statement. */
1101 new_line
= insert_args (cmd
->line
);
1103 return invalid_control
;
1104 old_chain
= make_cleanup (free_current_contents
, &new_line
);
1105 expr
= parse_expression (new_line
);
1106 make_cleanup (free_current_contents
, &expr
);
1108 ret
= simple_control
;
1111 /* Keep iterating so long as the expression is true. */
1118 /* Evaluate the expression. */
1119 val_mark
= value_mark ();
1120 val
= evaluate_expression (expr
);
1121 cond_result
= value_true (val
);
1122 value_free_to_mark (val_mark
);
1124 /* If the value is false, then break out of the loop. */
1128 /* Execute the body of the while statement. */
1129 current
= *cmd
->body_list
;
1132 ret
= execute_control_command (current
);
1134 /* If we got an error, or a "break" command, then stop
1136 if (ret
== invalid_control
|| ret
== break_control
)
1142 /* If we got a "continue" command, then restart the loop
1144 if (ret
== continue_control
)
1147 /* Get the next statement. */
1148 current
= current
->next
;
1152 /* Reset RET so that we don't recurse the break all the way down. */
1153 if (ret
== break_control
)
1154 ret
= simple_control
;
1161 new_line
= insert_args (cmd
->line
);
1163 return invalid_control
;
1164 old_chain
= make_cleanup (free_current_contents
, &new_line
);
1165 /* Parse the conditional for the if statement. */
1166 expr
= parse_expression (new_line
);
1167 make_cleanup (free_current_contents
, &expr
);
1170 ret
= simple_control
;
1172 /* Evaluate the conditional. */
1173 val_mark
= value_mark ();
1174 val
= evaluate_expression (expr
);
1176 /* Choose which arm to take commands from based on the value of the
1177 conditional expression. */
1178 if (value_true (val
))
1179 current
= *cmd
->body_list
;
1180 else if (cmd
->body_count
== 2)
1181 current
= *(cmd
->body_list
+ 1);
1182 value_free_to_mark (val_mark
);
1184 /* Execute commands in the given arm. */
1187 ret
= execute_control_command (current
);
1189 /* If we got an error, get out. */
1190 if (ret
!= simple_control
)
1193 /* Get the next statement in the body. */
1194 current
= current
->next
;
1201 warning ("Invalid control type in command structure.");
1202 return invalid_control
;
1206 do_cleanups (old_chain
);
1211 /* "while" command support. Executes a body of statements while the
1212 loop condition is nonzero. */
1215 while_command (char *arg
, int from_tty
)
1217 struct command_line
*command
= NULL
;
1220 command
= get_command_line (while_control
, arg
);
1222 if (command
== NULL
)
1225 execute_control_command (command
);
1226 free_command_lines (&command
);
1229 /* "if" command support. Execute either the true or false arm depending
1230 on the value of the if conditional. */
1233 if_command (char *arg
, int from_tty
)
1235 struct command_line
*command
= NULL
;
1238 command
= get_command_line (if_control
, arg
);
1240 if (command
== NULL
)
1243 execute_control_command (command
);
1244 free_command_lines (&command
);
1249 arg_cleanup (void *ignore
)
1251 struct user_args
*oargs
= user_args
;
1253 internal_error ("Internal error, arg_cleanup called with no user args.\n");
1255 user_args
= user_args
->next
;
1259 /* Bind the incomming arguments for a user defined command to
1260 $arg0, $arg1 ... $argMAXUSERARGS. */
1262 static struct cleanup
*
1263 setup_user_args (char *p
)
1265 struct user_args
*args
;
1266 struct cleanup
*old_chain
;
1267 unsigned int arg_count
= 0;
1269 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
1270 memset (args
, 0, sizeof (struct user_args
));
1272 args
->next
= user_args
;
1275 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
1287 if (arg_count
>= MAXUSERARGS
)
1289 error ("user defined function may only have %d arguments.\n",
1294 /* Strip whitespace. */
1295 while (*p
== ' ' || *p
== '\t')
1298 /* P now points to an argument. */
1300 user_args
->a
[arg_count
].arg
= p
;
1302 /* Get to the end of this argument. */
1305 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
1311 else if (*p
== '\\')
1334 user_args
->a
[arg_count
].len
= p
- start_arg
;
1341 /* Given character string P, return a point to the first argument ($arg),
1342 or NULL if P contains no arguments. */
1345 locate_arg (char *p
)
1347 while ((p
= strchr (p
, '$')))
1349 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1356 /* Insert the user defined arguments stored in user_arg into the $arg
1357 arguments found in line, with the updated copy being placed into nline. */
1360 insert_args (char *line
)
1362 char *p
, *save_line
, *new_line
;
1365 /* First we need to know how much memory to allocate for the new line. */
1368 while ((p
= locate_arg (line
)))
1373 if (i
>= user_args
->count
)
1375 error ("Missing argument %d in user function.\n", i
);
1378 len
+= user_args
->a
[i
].len
;
1382 /* Don't forget the tail. */
1383 len
+= strlen (line
);
1385 /* Allocate space for the new line and fill it in. */
1386 new_line
= (char *) xmalloc (len
+ 1);
1387 if (new_line
== NULL
)
1390 /* Restore pointer to beginning of old line. */
1393 /* Save pointer to beginning of new line. */
1394 save_line
= new_line
;
1396 while ((p
= locate_arg (line
)))
1400 memcpy (new_line
, line
, p
- line
);
1401 new_line
+= p
- line
;
1404 len
= user_args
->a
[i
].len
;
1407 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1412 /* Don't forget the tail. */
1413 strcpy (new_line
, line
);
1415 /* Return a pointer to the beginning of the new line. */
1420 execute_user_command (struct cmd_list_element
*c
, char *args
)
1422 register struct command_line
*cmdlines
;
1423 struct cleanup
*old_chain
;
1424 enum command_control_type ret
;
1426 old_chain
= setup_user_args (args
);
1428 cmdlines
= c
->user_commands
;
1433 /* Set the instream to 0, indicating execution of a
1434 user-defined function. */
1435 old_chain
= make_cleanup (do_restore_instream_cleanup
, instream
);
1436 instream
= (FILE *) 0;
1439 ret
= execute_control_command (cmdlines
);
1440 if (ret
!= simple_control
&& ret
!= break_control
)
1442 warning ("Error in control structure.\n");
1445 cmdlines
= cmdlines
->next
;
1447 do_cleanups (old_chain
);
1450 /* Execute the line P as a command.
1451 Pass FROM_TTY as second argument to the defining function. */
1454 execute_command (char *p
, int from_tty
)
1456 register struct cmd_list_element
*c
;
1457 register enum language flang
;
1458 static int warned
= 0;
1460 /* FIXME: These should really be in an appropriate header file */
1461 extern void serial_log_command (const char *);
1465 /* Force cleanup of any alloca areas if using C alloca instead of
1466 a builtin alloca. */
1469 /* This can happen when command_line_input hits end of file. */
1473 serial_log_command (p
);
1475 while (*p
== ' ' || *p
== '\t')
1482 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1484 /* If the target is running, we allow only a limited set of
1486 if (event_loop_p
&& target_can_async_p () && target_executing
)
1487 if (!strcmp (c
->name
, "help")
1488 && !strcmp (c
->name
, "pwd")
1489 && !strcmp (c
->name
, "show")
1490 && !strcmp (c
->name
, "stop"))
1491 error ("Cannot execute this command while the target is running.");
1493 /* Pass null arg rather than an empty one. */
1496 /* Clear off trailing whitespace, except for set and complete command. */
1497 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1499 p
= arg
+ strlen (arg
) - 1;
1500 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1505 /* If this command has been pre-hooked, run the hook first. */
1506 if ((c
->hook_pre
) && (!c
->hook_in
))
1508 c
->hook_in
= 1; /* Prevent recursive hooking */
1509 execute_user_command (c
->hook_pre
, (char *) 0);
1510 c
->hook_in
= 0; /* Allow hook to work again once it is complete */
1513 if (c
->flags
& DEPRECATED_WARN_USER
)
1514 deprecated_cmd_warning (&line
);
1516 if (c
->class == class_user
)
1517 execute_user_command (c
, arg
);
1518 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1519 do_setshow_command (arg
, from_tty
& caution
, c
);
1520 else if (c
->function
.cfunc
== NO_FUNCTION
)
1521 error ("That is not a command, just a help topic.");
1522 else if (call_command_hook
)
1523 call_command_hook (c
, arg
, from_tty
& caution
);
1525 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1527 /* If this command has been post-hooked, run the hook last. */
1528 if ((c
->hook_post
) && (!c
->hook_in
))
1530 c
->hook_in
= 1; /* Prevent recursive hooking */
1531 execute_user_command (c
->hook_post
, (char *) 0);
1532 c
->hook_in
= 0; /* allow hook to work again once it is complete */
1537 /* Tell the user if the language has changed (except first time). */
1538 if (current_language
!= expected_language
)
1540 if (language_mode
== language_mode_auto
)
1542 language_info (1); /* Print what changed. */
1547 /* Warn the user if the working language does not match the
1548 language of the current frame. Only warn the user if we are
1549 actually running the program, i.e. there is a stack. */
1550 /* FIXME: This should be cacheing the frame and only running when
1551 the frame changes. */
1553 if (target_has_stack
)
1555 flang
= get_frame_language ();
1557 && flang
!= language_unknown
1558 && flang
!= current_language
->la_language
)
1560 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1566 /* Read commands from `instream' and execute them
1567 until end of file or error reading instream. */
1572 struct cleanup
*old_chain
;
1574 int stdin_is_tty
= ISATTY (stdin
);
1575 long time_at_cmd_start
;
1577 long space_at_cmd_start
= 0;
1579 extern int display_time
;
1580 extern int display_space
;
1582 while (instream
&& !feof (instream
))
1585 extern int insert_mode
;
1587 if (window_hook
&& instream
== stdin
)
1588 (*window_hook
) (instream
, get_prompt ());
1591 if (instream
== stdin
&& stdin_is_tty
)
1592 reinitialize_more_filter ();
1593 old_chain
= make_cleanup (null_cleanup
, 0);
1596 /* A bit of paranoia: I want to make sure the "insert_mode" global
1597 * is clear except when it is being used for command-line editing
1598 * (see tuiIO.c, utils.c); otherwise normal output will
1599 * get messed up in the TUI. So clear it before/after
1600 * the command-line-input call. - RT
1604 /* Get a command-line. This calls the readline package. */
1605 command
= command_line_input (instream
== stdin
?
1606 get_prompt () : (char *) NULL
,
1607 instream
== stdin
, "prompt");
1614 time_at_cmd_start
= get_run_time ();
1619 extern char **environ
;
1620 char *lim
= (char *) sbrk (0);
1622 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1626 execute_command (command
, instream
== stdin
);
1627 /* Do any commands attached to breakpoint we stopped at. */
1628 bpstat_do_actions (&stop_bpstat
);
1629 do_cleanups (old_chain
);
1633 long cmd_time
= get_run_time () - time_at_cmd_start
;
1635 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1636 cmd_time
/ 1000000, cmd_time
% 1000000);
1642 extern char **environ
;
1643 char *lim
= (char *) sbrk (0);
1644 long space_now
= lim
- (char *) &environ
;
1645 long space_diff
= space_now
- space_at_cmd_start
;
1647 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1649 (space_diff
>= 0 ? '+' : '-'),
1656 /* Read commands from `instream' and execute them until end of file or
1657 error reading instream. This command loop doesnt care about any
1658 such things as displaying time and space usage. If the user asks
1659 for those, they won't work. */
1661 simplified_command_loop (char *(*read_input_func
) (char *),
1662 void (*execute_command_func
) (char *, int))
1664 struct cleanup
*old_chain
;
1666 int stdin_is_tty
= ISATTY (stdin
);
1668 while (instream
&& !feof (instream
))
1671 if (instream
== stdin
&& stdin_is_tty
)
1672 reinitialize_more_filter ();
1673 old_chain
= make_cleanup (null_cleanup
, 0);
1675 /* Get a command-line. */
1676 command
= (*read_input_func
) (instream
== stdin
?
1677 get_prompt () : (char *) NULL
);
1682 (*execute_command_func
) (command
, instream
== stdin
);
1684 /* Do any commands attached to breakpoint we stopped at. */
1685 bpstat_do_actions (&stop_bpstat
);
1687 do_cleanups (old_chain
);
1691 /* Commands call this if they do not want to be repeated by null lines. */
1699 /* If we aren't reading from standard input, we are saving the last
1700 thing read from stdin in line and don't want to delete it. Null lines
1701 won't repeat here in any case. */
1702 if (instream
== stdin
)
1706 /* Read a line from the stream "instream" without command line editing.
1708 It prints PROMPT_ARG once at the start.
1709 Action is compatible with "readline", e.g. space for the result is
1710 malloc'd and should be freed by the caller.
1712 A NULL return means end of file. */
1714 gdb_readline (char *prompt_arg
)
1718 int input_index
= 0;
1719 int result_size
= 80;
1723 /* Don't use a _filtered function here. It causes the assumed
1724 character position to be off, since the newline we read from
1725 the user is not accounted for. */
1726 fputs_unfiltered (prompt_arg
, gdb_stdout
);
1728 /* Move to a new line so the entered line doesn't have a prompt
1729 on the front of it. */
1730 fputs_unfiltered ("\n", gdb_stdout
);
1732 gdb_flush (gdb_stdout
);
1735 result
= (char *) xmalloc (result_size
);
1739 /* Read from stdin if we are executing a user defined command.
1740 This is the right thing for prompt_for_continue, at least. */
1741 c
= fgetc (instream
? instream
: stdin
);
1745 if (input_index
> 0)
1746 /* The last line does not end with a newline. Return it, and
1747 if we are called again fgetc will still return EOF and
1748 we'll return NULL then. */
1755 #ifndef CRLF_SOURCE_FILES
1759 if (input_index
> 0 && result
[input_index
- 1] == '\r')
1765 result
[input_index
++] = c
;
1766 while (input_index
>= result_size
)
1769 result
= (char *) xrealloc (result
, result_size
);
1773 result
[input_index
++] = '\0';
1777 /* Variables which control command line editing and history
1778 substitution. These variables are given default values at the end
1780 static int command_editing_p
;
1781 /* NOTE 1999-04-29: This variable will be static again, once we modify
1782 gdb to use the event loop as the default command loop and we merge
1783 event-top.c into this file, top.c */
1784 /* static */ int history_expansion_p
;
1785 static int write_history_p
;
1786 static int history_size
;
1787 static char *history_filename
;
1789 /* readline uses the word breaks for two things:
1790 (1) In figuring out where to point the TEXT parameter to the
1791 rl_completion_entry_function. Since we don't use TEXT for much,
1792 it doesn't matter a lot what the word breaks are for this purpose, but
1793 it does affect how much stuff M-? lists.
1794 (2) If one of the matches contains a word break character, readline
1795 will quote it. That's why we switch between
1796 gdb_completer_word_break_characters and
1797 gdb_completer_command_word_break_characters. I'm not sure when
1798 we need this behavior (perhaps for funky characters in C++ symbols?). */
1800 /* Variables which are necessary for fancy command line editing. */
1801 char *gdb_completer_word_break_characters
=
1802 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1804 /* When completing on command names, we remove '-' from the list of
1805 word break characters, since we use it in command names. If the
1806 readline library sees one in any of the current completion strings,
1807 it thinks that the string needs to be quoted and automatically supplies
1809 char *gdb_completer_command_word_break_characters
=
1810 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1812 /* When completing on file names, we remove from the list of word
1813 break characters any characters that are commonly used in file
1814 names, such as '-', '+', '~', etc. Otherwise, readline displays
1815 incorrect completion candidates. */
1816 char *gdb_completer_file_name_break_characters
= " \t\n*|\"';:?/><";
1818 /* Characters that can be used to quote completion strings. Note that we
1819 can't include '"' because the gdb C parser treats such quoted sequences
1821 char *gdb_completer_quote_characters
=
1824 /* Functions that are used as part of the fancy command line editing. */
1826 /* This can be used for functions which don't want to complete on symbols
1827 but don't want to complete on anything else either. */
1830 noop_completer (char *text
, char *prefix
)
1835 /* Complete on filenames. */
1837 filename_completer (char *text
, char *word
)
1839 /* From readline. */
1840 extern char *filename_completion_function (char *, int);
1841 int subsequent_name
;
1843 int return_val_used
;
1844 int return_val_alloced
;
1846 return_val_used
= 0;
1847 /* Small for testing. */
1848 return_val_alloced
= 1;
1849 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1851 subsequent_name
= 0;
1855 p
= filename_completion_function (text
, subsequent_name
);
1856 if (return_val_used
>= return_val_alloced
)
1858 return_val_alloced
*= 2;
1860 (char **) xrealloc (return_val
,
1861 return_val_alloced
* sizeof (char *));
1865 return_val
[return_val_used
++] = p
;
1868 /* We need to set subsequent_name to a non-zero value before the
1869 continue line below, because otherwise, if the first file seen
1870 by GDB is a backup file whose name ends in a `~', we will loop
1872 subsequent_name
= 1;
1873 /* Like emacs, don't complete on old versions. Especially useful
1874 in the "source" command. */
1875 if (p
[strlen (p
) - 1] == '~')
1881 /* Return exactly p. */
1882 return_val
[return_val_used
++] = p
;
1883 else if (word
> text
)
1885 /* Return some portion of p. */
1886 q
= xmalloc (strlen (p
) + 5);
1887 strcpy (q
, p
+ (word
- text
));
1888 return_val
[return_val_used
++] = q
;
1893 /* Return some of TEXT plus p. */
1894 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1895 strncpy (q
, word
, text
- word
);
1896 q
[text
- word
] = '\0';
1898 return_val
[return_val_used
++] = q
;
1904 /* There is no way to do this just long enough to affect quote inserting
1905 without also affecting the next completion. This should be fixed in
1907 /* Insure that readline does the right thing
1908 with respect to inserting quotes. */
1909 rl_completer_word_break_characters
= "";
1914 /* Here are some useful test cases for completion. FIXME: These should
1915 be put in the test suite. They should be tested with both M-? and TAB.
1917 "show output-" "radix"
1918 "show output" "-radix"
1919 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1920 "p " ambiguous (all symbols)
1921 "info t foo" no completions
1922 "info t " no completions
1923 "info t" ambiguous ("info target", "info terminal", etc.)
1924 "info ajksdlfk" no completions
1925 "info ajksdlfk " no completions
1927 "info " ambiguous (all info commands)
1928 "p \"a" no completions (string constant)
1929 "p 'a" ambiguous (all symbols starting with a)
1930 "p b-a" ambiguous (all symbols starting with a)
1931 "p b-" ambiguous (all symbols)
1932 "file Make" "file" (word break hard to screw up here)
1933 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1936 /* Generate completions one by one for the completer. Each time we are
1937 called return another potential completion to the caller.
1938 line_completion just completes on commands or passes the buck to the
1939 command's completer function, the stuff specific to symbol completion
1940 is in make_symbol_completion_list.
1942 TEXT is the caller's idea of the "word" we are looking at.
1944 MATCHES is the number of matches that have currently been collected from
1945 calling this completion function. When zero, then we need to initialize,
1946 otherwise the initialization has already taken place and we can just
1947 return the next potential completion string.
1949 LINE_BUFFER is available to be looked at; it contains the entire text
1950 of the line. POINT is the offset in that line of the cursor. You
1951 should pretend that the line ends at POINT.
1953 Returns NULL if there are no more completions, else a pointer to a string
1954 which is a possible completion, it is the caller's responsibility to
1958 line_completion_function (char *text
, int matches
, char *line_buffer
, int point
)
1960 static char **list
= (char **) NULL
; /* Cache of completions */
1961 static int index
; /* Next cached completion */
1962 char *output
= NULL
;
1963 char *tmp_command
, *p
;
1964 /* Pointer within tmp_command which corresponds to text. */
1966 struct cmd_list_element
*c
, *result_list
;
1970 /* The caller is beginning to accumulate a new set of completions, so
1971 we need to find all of them now, and cache them for returning one at
1972 a time on future calls. */
1976 /* Free the storage used by LIST, but not by the strings inside.
1977 This is because rl_complete_internal () frees the strings. */
1983 /* Choose the default set of word break characters to break completions.
1984 If we later find out that we are doing completions on command strings
1985 (as opposed to strings supplied by the individual command completer
1986 functions, which can be any string) then we will switch to the
1987 special word break set for command strings, which leaves out the
1988 '-' character used in some commands. */
1990 rl_completer_word_break_characters
=
1991 gdb_completer_word_break_characters
;
1993 /* Decide whether to complete on a list of gdb commands or on symbols. */
1994 tmp_command
= (char *) alloca (point
+ 1);
1997 strncpy (tmp_command
, line_buffer
, point
);
1998 tmp_command
[point
] = '\0';
1999 /* Since text always contains some number of characters leading up
2000 to point, we can find the equivalent position in tmp_command
2001 by subtracting that many characters from the end of tmp_command. */
2002 word
= tmp_command
+ point
- strlen (text
);
2006 /* An empty line we want to consider ambiguous; that is, it
2007 could be any command. */
2008 c
= (struct cmd_list_element
*) -1;
2013 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
2016 /* Move p up to the next interesting thing. */
2017 while (*p
== ' ' || *p
== '\t')
2024 /* It is an unrecognized command. So there are no
2025 possible completions. */
2028 else if (c
== (struct cmd_list_element
*) -1)
2032 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
2033 doesn't advance over that thing itself. Do so now. */
2035 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
2037 if (q
!= tmp_command
+ point
)
2039 /* There is something beyond the ambiguous
2040 command, so there are no possible completions. For
2041 example, "info t " or "info t foo" does not complete
2042 to anything, because "info t" can be "info target" or
2048 /* We're trying to complete on the command which was ambiguous.
2049 This we can deal with. */
2052 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
2057 list
= complete_on_cmdlist (cmdlist
, p
, word
);
2059 /* Insure that readline does the right thing with respect to
2060 inserting quotes. */
2061 rl_completer_word_break_characters
=
2062 gdb_completer_command_word_break_characters
;
2067 /* We've recognized a full command. */
2069 if (p
== tmp_command
+ point
)
2071 /* There is no non-whitespace in the line beyond the command. */
2073 if (p
[-1] == ' ' || p
[-1] == '\t')
2075 /* The command is followed by whitespace; we need to complete
2076 on whatever comes after command. */
2079 /* It is a prefix command; what comes after it is
2080 a subcommand (e.g. "info "). */
2081 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
2083 /* Insure that readline does the right thing
2084 with respect to inserting quotes. */
2085 rl_completer_word_break_characters
=
2086 gdb_completer_command_word_break_characters
;
2090 list
= complete_on_enum (c
->enums
, p
, word
);
2091 rl_completer_word_break_characters
=
2092 gdb_completer_command_word_break_characters
;
2096 /* It is a normal command; what comes after it is
2097 completed by the command's completer function. */
2098 list
= (*c
->completer
) (p
, word
);
2099 if (c
->completer
== filename_completer
)
2100 rl_completer_word_break_characters
=
2101 gdb_completer_file_name_break_characters
;
2106 /* The command is not followed by whitespace; we need to
2107 complete on the command itself. e.g. "p" which is a
2108 command itself but also can complete to "print", "ptype"
2112 /* Find the command we are completing on. */
2114 while (q
> tmp_command
)
2116 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
2122 list
= complete_on_cmdlist (result_list
, q
, word
);
2124 /* Insure that readline does the right thing
2125 with respect to inserting quotes. */
2126 rl_completer_word_break_characters
=
2127 gdb_completer_command_word_break_characters
;
2132 /* There is non-whitespace beyond the command. */
2134 if (c
->prefixlist
&& !c
->allow_unknown
)
2136 /* It is an unrecognized subcommand of a prefix command,
2137 e.g. "info adsfkdj". */
2142 list
= complete_on_enum (c
->enums
, p
, word
);
2146 /* It is a normal command. */
2147 list
= (*c
->completer
) (p
, word
);
2148 if (c
->completer
== filename_completer
)
2149 rl_completer_word_break_characters
=
2150 gdb_completer_file_name_break_characters
;
2156 /* If we found a list of potential completions during initialization then
2157 dole them out one at a time. The vector of completions is NULL
2158 terminated, so after returning the last one, return NULL (and continue
2159 to do so) each time we are called after that, until a new list is
2164 output
= list
[index
];
2172 /* Can't do this because readline hasn't yet checked the word breaks
2173 for figuring out whether to insert a quote. */
2175 /* Make sure the word break characters are set back to normal for the
2176 next time that readline tries to complete something. */
2177 rl_completer_word_break_characters
=
2178 gdb_completer_word_break_characters
;
2184 /* Line completion interface function for readline. */
2187 readline_line_completion_function (char *text
, int matches
)
2189 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
2192 /* Skip over a possibly quoted word (as defined by the quote characters
2193 and word break characters the completer uses). Returns pointer to the
2194 location after the "word". */
2197 skip_quoted (char *str
)
2199 char quote_char
= '\0';
2202 for (scan
= str
; *scan
!= '\0'; scan
++)
2204 if (quote_char
!= '\0')
2206 /* Ignore everything until the matching close quote char */
2207 if (*scan
== quote_char
)
2209 /* Found matching close quote. */
2214 else if (strchr (gdb_completer_quote_characters
, *scan
))
2216 /* Found start of a quoted string. */
2219 else if (strchr (gdb_completer_word_break_characters
, *scan
))
2230 stop_sig (int signo
)
2232 #if STOP_SIGNAL == SIGTSTP
2233 signal (SIGTSTP
, SIG_DFL
);
2235 kill (getpid (), SIGTSTP
);
2236 signal (SIGTSTP
, stop_sig
);
2238 signal (STOP_SIGNAL
, stop_sig
);
2240 printf_unfiltered ("%s", get_prompt ());
2241 gdb_flush (gdb_stdout
);
2243 /* Forget about any previous command -- null line now will do nothing. */
2246 #endif /* STOP_SIGNAL */
2248 /* Initialize signal handlers. */
2250 do_nothing (int signo
)
2252 /* Under System V the default disposition of a signal is reinstated after
2253 the signal is caught and delivered to an application process. On such
2254 systems one must restore the replacement signal handler if one wishes
2255 to continue handling the signal in one's program. On BSD systems this
2256 is not needed but it is harmless, and it simplifies the code to just do
2257 it unconditionally. */
2258 signal (signo
, do_nothing
);
2264 signal (SIGINT
, request_quit
);
2266 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2267 to the inferior and breakpoints will be ignored. */
2269 signal (SIGTRAP
, SIG_DFL
);
2272 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2273 passed to the inferior, which we don't want. It would be
2274 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2275 on BSD4.3 systems using vfork, that can affect the
2276 GDB process as well as the inferior (the signal handling tables
2277 might be in memory, shared between the two). Since we establish
2278 a handler for SIGQUIT, when we call exec it will set the signal
2279 to SIG_DFL for us. */
2280 signal (SIGQUIT
, do_nothing
);
2282 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
2283 signal (SIGHUP
, disconnect
);
2285 signal (SIGFPE
, float_handler
);
2287 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2288 signal (SIGWINCH
, SIGWINCH_HANDLER
);
2292 /* Read one line from the command input stream `instream'
2293 into the local static buffer `linebuffer' (whose current length
2295 The buffer is made bigger as necessary.
2296 Returns the address of the start of the line.
2298 NULL is returned for end of file.
2300 *If* the instream == stdin & stdin is a terminal, the line read
2301 is copied into the file line saver (global var char *line,
2302 length linesize) so that it can be duplicated.
2304 This routine either uses fancy command line editing or
2305 simple input as the user has requested. */
2308 command_line_input (char *prompt_arg
, int repeat
, char *annotation_suffix
)
2310 static char *linebuffer
= 0;
2311 static unsigned linelength
= 0;
2315 char *local_prompt
= prompt_arg
;
2319 /* The annotation suffix must be non-NULL. */
2320 if (annotation_suffix
== NULL
)
2321 annotation_suffix
= "";
2323 if (annotation_level
> 1 && instream
== stdin
)
2325 local_prompt
= alloca ((prompt_arg
== NULL
? 0 : strlen (prompt_arg
))
2326 + strlen (annotation_suffix
) + 40);
2327 if (prompt_arg
== NULL
)
2328 local_prompt
[0] = '\0';
2330 strcpy (local_prompt
, prompt_arg
);
2331 strcat (local_prompt
, "\n\032\032");
2332 strcat (local_prompt
, annotation_suffix
);
2333 strcat (local_prompt
, "\n");
2336 if (linebuffer
== 0)
2339 linebuffer
= (char *) xmalloc (linelength
);
2344 /* Control-C quits instantly if typed while in this loop
2345 since it should not wait until the user types a newline. */
2351 signal (STOP_SIGNAL
, handle_stop_sig
);
2353 signal (STOP_SIGNAL
, stop_sig
);
2359 /* Make sure that all output has been output. Some machines may let
2360 you get away with leaving out some of the gdb_flush, but not all. */
2362 gdb_flush (gdb_stdout
);
2363 gdb_flush (gdb_stderr
);
2365 if (source_file_name
!= NULL
)
2367 ++source_line_number
;
2368 sprintf (source_error
,
2369 "%s%s:%d: Error in sourced command file:\n",
2372 source_line_number
);
2373 error_pre_print
= source_error
;
2376 if (annotation_level
> 1 && instream
== stdin
)
2378 printf_unfiltered ("\n\032\032pre-");
2379 printf_unfiltered (annotation_suffix
);
2380 printf_unfiltered ("\n");
2383 /* Don't use fancy stuff if not talking to stdin. */
2384 if (readline_hook
&& instream
== NULL
)
2386 rl
= (*readline_hook
) (local_prompt
);
2388 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
2390 rl
= readline (local_prompt
);
2394 rl
= gdb_readline (local_prompt
);
2397 if (annotation_level
> 1 && instream
== stdin
)
2399 printf_unfiltered ("\n\032\032post-");
2400 printf_unfiltered (annotation_suffix
);
2401 printf_unfiltered ("\n");
2404 if (!rl
|| rl
== (char *) EOF
)
2409 if (strlen (rl
) + 1 + (p
- linebuffer
) > linelength
)
2411 linelength
= strlen (rl
) + 1 + (p
- linebuffer
);
2412 nline
= (char *) xrealloc (linebuffer
, linelength
);
2413 p
+= nline
- linebuffer
;
2417 /* Copy line. Don't copy null at end. (Leaves line alone
2418 if this was just a newline) */
2422 free (rl
); /* Allocated in readline. */
2424 if (p
== linebuffer
|| *(p
- 1) != '\\')
2427 p
--; /* Put on top of '\'. */
2428 local_prompt
= (char *) 0;
2433 signal (STOP_SIGNAL
, SIG_DFL
);
2440 #define SERVER_COMMAND_LENGTH 7
2442 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2443 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2446 /* Note that we don't set `line'. Between this and the check in
2447 dont_repeat, this insures that repeating will still do the
2450 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2453 /* Do history expansion if that is wished. */
2454 if (history_expansion_p
&& instream
== stdin
2455 && ISATTY (instream
))
2457 char *history_value
;
2460 *p
= '\0'; /* Insert null now. */
2461 expanded
= history_expand (linebuffer
, &history_value
);
2464 /* Print the changes. */
2465 printf_unfiltered ("%s\n", history_value
);
2467 /* If there was an error, call this function again. */
2470 free (history_value
);
2471 return command_line_input (prompt_arg
, repeat
, annotation_suffix
);
2473 if (strlen (history_value
) > linelength
)
2475 linelength
= strlen (history_value
) + 1;
2476 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2478 strcpy (linebuffer
, history_value
);
2479 p
= linebuffer
+ strlen (linebuffer
);
2480 free (history_value
);
2484 /* If we just got an empty line, and that is supposed
2485 to repeat the previous command, return the value in the
2487 if (repeat
&& p
== linebuffer
)
2489 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++);
2495 /* Add line to history if appropriate. */
2496 if (instream
== stdin
2497 && ISATTY (stdin
) && *linebuffer
)
2498 add_history (linebuffer
);
2500 /* Note: lines consisting solely of comments are added to the command
2501 history. This is useful when you type a command, and then
2502 realize you don't want to execute it quite yet. You can comment
2503 out the command and then later fetch it from the value history
2504 and remove the '#'. The kill ring is probably better, but some
2505 people are in the habit of commenting things out. */
2507 *p1
= '\0'; /* Found a comment. */
2509 /* Save into global buffer if appropriate. */
2512 if (linelength
> linesize
)
2514 line
= xrealloc (line
, linelength
);
2515 linesize
= linelength
;
2517 strcpy (line
, linebuffer
);
2525 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2526 code bodies. This is typically used when we encounter an "else"
2527 clause for an "if" command. */
2530 realloc_body_list (struct command_line
*command
, int new_length
)
2533 struct command_line
**body_list
;
2535 n
= command
->body_count
;
2537 /* Nothing to do? */
2538 if (new_length
<= n
)
2541 body_list
= (struct command_line
**)
2542 xmalloc (sizeof (struct command_line
*) * new_length
);
2544 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2546 free (command
->body_list
);
2547 command
->body_list
= body_list
;
2548 command
->body_count
= new_length
;
2551 /* Read one line from the input stream. If the command is an "else" or
2552 "end", return such an indication to the caller. */
2554 static enum misc_command_type
2555 read_next_line (struct command_line
**command
)
2557 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2560 if (control_level
>= 254)
2561 error ("Control nesting too deep!\n");
2563 /* Set a prompt based on the nesting of the control commands. */
2564 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2566 for (i
= 0; i
< control_level
; i
++)
2567 control_prompt
[i
] = ' ';
2568 control_prompt
[i
] = '>';
2569 control_prompt
[i
+ 1] = '\0';
2570 prompt_ptr
= (char *) &control_prompt
[0];
2575 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2577 /* Not sure what to do here. */
2581 /* Strip leading and trailing whitespace. */
2582 while (*p
== ' ' || *p
== '\t')
2585 p1
= p
+ strlen (p
);
2586 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2589 /* Blanks and comments don't really do anything, but we need to
2590 distinguish them from else, end and other commands which can be
2592 if (p1
== p
|| p
[0] == '#')
2595 /* Is this the end of a simple, while, or if control structure? */
2596 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2599 /* Is the else clause of an if control structure? */
2600 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2601 return else_command
;
2603 /* Check for while, if, break, continue, etc and build a new command
2604 line structure for them. */
2605 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2606 *command
= build_command_line (while_control
, p
+ 6);
2607 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2608 *command
= build_command_line (if_control
, p
+ 3);
2609 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2611 *command
= (struct command_line
*)
2612 xmalloc (sizeof (struct command_line
));
2613 (*command
)->next
= NULL
;
2614 (*command
)->line
= NULL
;
2615 (*command
)->control_type
= break_control
;
2616 (*command
)->body_count
= 0;
2617 (*command
)->body_list
= NULL
;
2619 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2621 *command
= (struct command_line
*)
2622 xmalloc (sizeof (struct command_line
));
2623 (*command
)->next
= NULL
;
2624 (*command
)->line
= NULL
;
2625 (*command
)->control_type
= continue_control
;
2626 (*command
)->body_count
= 0;
2627 (*command
)->body_list
= NULL
;
2631 /* A normal command. */
2632 *command
= (struct command_line
*)
2633 xmalloc (sizeof (struct command_line
));
2634 (*command
)->next
= NULL
;
2635 (*command
)->line
= savestring (p
, p1
- p
);
2636 (*command
)->control_type
= simple_control
;
2637 (*command
)->body_count
= 0;
2638 (*command
)->body_list
= NULL
;
2641 /* Nothing special. */
2645 /* Recursively read in the control structures and create a command_line
2646 structure from them.
2648 The parent_control parameter is the control structure in which the
2649 following commands are nested. */
2651 static enum command_control_type
2652 recurse_read_control_structure (struct command_line
*current_cmd
)
2654 int current_body
, i
;
2655 enum misc_command_type val
;
2656 enum command_control_type ret
;
2657 struct command_line
**body_ptr
, *child_tail
, *next
;
2662 /* Sanity checks. */
2663 if (current_cmd
->control_type
== simple_control
)
2665 error ("Recursed on a simple control type\n");
2666 return invalid_control
;
2669 if (current_body
> current_cmd
->body_count
)
2671 error ("Allocated body is smaller than this command type needs\n");
2672 return invalid_control
;
2675 /* Read lines from the input stream and build control structures. */
2681 val
= read_next_line (&next
);
2683 /* Just skip blanks and comments. */
2684 if (val
== nop_command
)
2687 if (val
== end_command
)
2689 if (current_cmd
->control_type
== while_control
2690 || current_cmd
->control_type
== if_control
)
2692 /* Success reading an entire control structure. */
2693 ret
= simple_control
;
2698 ret
= invalid_control
;
2703 /* Not the end of a control structure. */
2704 if (val
== else_command
)
2706 if (current_cmd
->control_type
== if_control
2707 && current_body
== 1)
2709 realloc_body_list (current_cmd
, 2);
2716 ret
= invalid_control
;
2723 child_tail
->next
= next
;
2727 body_ptr
= current_cmd
->body_list
;
2728 for (i
= 1; i
< current_body
; i
++)
2737 /* If the latest line is another control structure, then recurse
2739 if (next
->control_type
== while_control
2740 || next
->control_type
== if_control
)
2743 ret
= recurse_read_control_structure (next
);
2746 if (ret
!= simple_control
)
2756 /* Read lines from the input stream and accumulate them in a chain of
2757 struct command_line's, which is then returned. For input from a
2758 terminal, the special command "end" is used to mark the end of the
2759 input, and is not included in the returned chain of commands. */
2761 #define END_MESSAGE "End with a line saying just \"end\"."
2763 struct command_line
*
2764 read_command_lines (char *prompt_arg
, int from_tty
)
2766 struct command_line
*head
, *tail
, *next
;
2767 struct cleanup
*old_chain
;
2768 enum command_control_type ret
;
2769 enum misc_command_type val
;
2772 if (readline_begin_hook
)
2774 /* Note - intentional to merge messages with no newline */
2775 (*readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
2777 else if (from_tty
&& input_from_terminal_p ())
2779 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
2780 gdb_flush (gdb_stdout
);
2788 val
= read_next_line (&next
);
2790 /* Ignore blank lines or comments. */
2791 if (val
== nop_command
)
2794 if (val
== end_command
)
2796 ret
= simple_control
;
2800 if (val
!= ok_command
)
2802 ret
= invalid_control
;
2806 if (next
->control_type
== while_control
2807 || next
->control_type
== if_control
)
2810 ret
= recurse_read_control_structure (next
);
2813 if (ret
== invalid_control
)
2824 old_chain
= make_cleanup_free_command_lines (&head
);
2833 if (ret
!= invalid_control
)
2835 discard_cleanups (old_chain
);
2838 do_cleanups (old_chain
);
2841 if (readline_end_hook
)
2843 (*readline_end_hook
) ();
2848 /* Free a chain of struct command_line's. */
2851 free_command_lines (struct command_line
**lptr
)
2853 register struct command_line
*l
= *lptr
;
2854 register struct command_line
*next
;
2855 struct command_line
**blist
;
2860 if (l
->body_count
> 0)
2862 blist
= l
->body_list
;
2863 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2864 free_command_lines (blist
);
2874 do_free_command_lines_cleanup (void *arg
)
2876 free_command_lines (arg
);
2879 static struct cleanup
*
2880 make_cleanup_free_command_lines (struct command_line
**arg
)
2882 return make_cleanup (do_free_command_lines_cleanup
, arg
);
2885 /* Add an element to the list of info subcommands. */
2887 struct cmd_list_element
*
2888 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
2890 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2893 /* Add an alias to the list of info subcommands. */
2895 struct cmd_list_element
*
2896 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
2898 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2901 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2902 Therefore, its own definition is called only for "info" with no args. */
2906 info_command (char *arg
, int from_tty
)
2908 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2909 help_list (infolist
, "info ", -1, gdb_stdout
);
2912 /* The "complete" command is used by Emacs to implement completion. */
2916 complete_command (char *arg
, int from_tty
)
2926 argpoint
= strlen (arg
);
2928 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2930 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2932 printf_unfiltered ("%s\n", completion
);
2937 /* The "show" command with no arguments shows all the settings. */
2941 show_command (char *arg
, int from_tty
)
2943 cmd_show_list (showlist
, from_tty
, "");
2946 /* Add an element to the list of commands. */
2948 struct cmd_list_element
*
2949 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
2952 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
2955 /* Add an alias or abbreviation command to the list of commands. */
2957 struct cmd_list_element
*
2958 add_com_alias (char *name
, char *oldname
, enum command_class
class,
2961 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2965 error_no_arg (char *why
)
2967 error ("Argument required (%s).", why
);
2970 /* Provide documentation on command or list given by COMMAND. FROM_TTY
2975 help_command (char *command
, int from_tty
)
2977 help_cmd (command
, gdb_stdout
);
2981 validate_comname (char *comname
)
2986 error_no_arg ("name of command to define");
2991 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
2992 error ("Junk in argument list: \"%s\"", p
);
2997 /* This is just a placeholder in the command data structures. */
2999 user_defined_command (char *ignore
, int from_tty
)
3004 define_command (char *comname
, int from_tty
)
3006 #define MAX_TMPBUF 128
3013 register struct command_line
*cmds
;
3014 register struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0;
3015 char *tem
= comname
;
3017 char tmpbuf
[MAX_TMPBUF
];
3018 int hook_type
= CMD_NO_HOOK
;
3019 int hook_name_size
= 0;
3021 #define HOOK_STRING "hook-"
3023 #define HOOK_POST_STRING "hookpost-"
3024 #define HOOK_POST_LEN 9
3026 validate_comname (comname
);
3028 /* Look it up, and verify that we got an exact match. */
3029 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
3030 if (c
&& !STREQ (comname
, c
->name
))
3035 if (c
->class == class_user
|| c
->class == class_alias
)
3036 tem
= "Redefine command \"%s\"? ";
3038 tem
= "Really redefine built-in command \"%s\"? ";
3039 if (!query (tem
, c
->name
))
3040 error ("Command \"%s\" not redefined.", c
->name
);
3043 /* If this new command is a hook, then mark the command which it
3044 is hooking. Note that we allow hooking `help' commands, so that
3045 we can hook the `stop' pseudo-command. */
3047 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
3049 hook_type
= CMD_PRE_HOOK
;
3050 hook_name_size
= HOOK_LEN
;
3052 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
3054 hook_type
= CMD_POST_HOOK
;
3055 hook_name_size
= HOOK_POST_LEN
;
3058 if (hook_type
!= CMD_NO_HOOK
)
3060 /* Look up cmd it hooks, and verify that we got an exact match. */
3061 tem
= comname
+ hook_name_size
;
3062 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
3063 if (hookc
&& !STREQ (comname
+ hook_name_size
, hookc
->name
))
3067 warning ("Your new `%s' command does not hook any existing command.",
3069 if (!query ("Proceed? "))
3070 error ("Not confirmed.");
3074 comname
= savestring (comname
, strlen (comname
));
3076 /* If the rest of the commands will be case insensitive, this one
3077 should behave in the same manner. */
3078 for (tem
= comname
; *tem
; tem
++)
3080 *tem
= tolower (*tem
);
3082 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
3083 cmds
= read_command_lines (tmpbuf
, from_tty
);
3085 if (c
&& c
->class == class_user
)
3086 free_command_lines (&c
->user_commands
);
3088 newc
= add_cmd (comname
, class_user
, user_defined_command
,
3089 (c
&& c
->class == class_user
)
3090 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
3091 newc
->user_commands
= cmds
;
3093 /* If this new command is a hook, then mark both commands as being
3100 hookc
->hook_pre
= newc
; /* Target gets hooked. */
3101 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
3104 hookc
->hook_pre
= newc
; /* Target gets hooked. */
3105 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
3108 /* Should never come here as hookc would be 0. */
3114 document_command (char *comname
, int from_tty
)
3116 struct command_line
*doclines
;
3117 register struct cmd_list_element
*c
;
3118 char *tem
= comname
;
3121 validate_comname (comname
);
3123 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
3125 if (c
->class != class_user
)
3126 error ("Command \"%s\" is built-in.", comname
);
3128 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
3129 doclines
= read_command_lines (tmpbuf
, from_tty
);
3135 register struct command_line
*cl1
;
3136 register int len
= 0;
3138 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
3139 len
+= strlen (cl1
->line
) + 1;
3141 c
->doc
= (char *) xmalloc (len
+ 1);
3144 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
3146 strcat (c
->doc
, cl1
->line
);
3148 strcat (c
->doc
, "\n");
3152 free_command_lines (&doclines
);
3155 /* Print the GDB banner. */
3157 print_gdb_version (struct ui_file
*stream
)
3159 /* From GNU coding standards, first line is meant to be easy for a
3160 program to parse, and is just canonical program name and version
3161 number, which starts after last space. */
3164 /* Print it console style until a format is defined */
3165 fprintf_filtered (stream
, "GNU gdb %s (UI_OUT)\n", version
);
3167 fprintf_filtered (stream
, "GNU gdb %s\n", version
);
3170 /* Second line is a copyright notice. */
3172 fprintf_filtered (stream
, "Copyright 2000 Free Software Foundation, Inc.\n");
3174 /* Following the copyright is a brief statement that the program is
3175 free software, that users are free to copy and change it on
3176 certain conditions, that it is covered by the GNU GPL, and that
3177 there is no warranty. */
3179 fprintf_filtered (stream
, "\
3180 GDB is free software, covered by the GNU General Public License, and you are\n\
3181 welcome to change it and/or distribute copies of it under certain conditions.\n\
3182 Type \"show copying\" to see the conditions.\n\
3183 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
3185 /* After the required info we print the configuration information. */
3187 fprintf_filtered (stream
, "This GDB was configured as \"");
3188 if (!STREQ (host_name
, target_name
))
3190 fprintf_filtered (stream
, "--host=%s --target=%s", host_name
, target_name
);
3194 fprintf_filtered (stream
, "%s", host_name
);
3196 fprintf_filtered (stream
, "\".");
3201 show_version (char *args
, int from_tty
)
3204 print_gdb_version (gdb_stdout
);
3205 printf_filtered ("\n");
3209 /* get_prompt: access method for the GDB prompt string. */
3211 #define MAX_PROMPT_SIZE 256
3214 * int get_prompt_1 (char * buf);
3216 * Work-horse for get_prompt (called via catch_errors).
3217 * Argument is buffer to hold the formatted prompt.
3219 * Returns: 1 for success (use formatted prompt)
3220 * 0 for failure (use gdb_prompt_string).
3223 static int gdb_prompt_escape
;
3226 get_prompt_1 (void *data
)
3228 char *formatted_prompt
= data
;
3232 local_prompt
= PROMPT (0);
3234 local_prompt
= gdb_prompt_string
;
3237 if (gdb_prompt_escape
== 0)
3239 return 0; /* do no formatting */
3242 /* formatted prompt */
3244 char fmt
[40], *promptp
, *outp
, *tmp
;
3251 struct type
*arg_type
, *elt_type
;
3253 promptp
= local_prompt
;
3254 outp
= formatted_prompt
;
3256 while (*promptp
!= '\0')
3258 int available
= MAX_PROMPT_SIZE
- (outp
- formatted_prompt
) - 1;
3260 if (*promptp
!= gdb_prompt_escape
)
3262 if (available
>= 1) /* overflow protect */
3263 *outp
++ = *promptp
++;
3267 /* GDB prompt string contains escape char. Parse for arg.
3268 Two consecutive escape chars followed by arg followed by
3269 a comma means to insert the arg using a default format.
3270 Otherwise a printf format string may be included between
3271 the two escape chars. eg:
3272 %%foo, insert foo using default format
3273 %2.2f%foo, insert foo using "%2.2f" format
3274 A mismatch between the format string and the data type
3275 of "foo" is an error (which we don't know how to protect
3278 fmt
[0] = '\0'; /* assume null format string */
3279 if (promptp
[1] == gdb_prompt_escape
) /* double esc char */
3281 promptp
+= 2; /* skip past two escape chars. */
3285 /* extract format string from between two esc chars */
3289 fmt
[i
++] = *promptp
++; /* copy format string */
3291 while (i
< sizeof (fmt
) - 1 &&
3292 *promptp
!= gdb_prompt_escape
&&
3295 if (*promptp
!= gdb_prompt_escape
)
3296 error ("Syntax error at prompt position %d",
3297 promptp
- local_prompt
);
3300 promptp
++; /* skip second escape char */
3301 fmt
[i
++] = '\0'; /* terminate the format string */
3305 arg_val
= parse_to_comma_and_eval (&promptp
);
3306 if (*promptp
== ',')
3307 promptp
++; /* skip past the comma */
3308 arg_type
= check_typedef (VALUE_TYPE (arg_val
));
3309 switch (TYPE_CODE (arg_type
))
3311 case TYPE_CODE_ARRAY
:
3312 elt_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
3313 if (TYPE_LENGTH (arg_type
) > 0 &&
3314 TYPE_LENGTH (elt_type
) == 1 &&
3315 TYPE_CODE (elt_type
) == TYPE_CODE_INT
)
3317 int len
= TYPE_LENGTH (arg_type
);
3319 if (VALUE_LAZY (arg_val
))
3320 value_fetch_lazy (arg_val
);
3321 tmp
= VALUE_CONTENTS (arg_val
);
3323 if (len
> available
)
3324 len
= available
; /* overflow protect */
3326 /* FIXME: how to protect GDB from crashing
3327 from bad user-supplied format string? */
3329 sprintf (outp
, fmt
, tmp
);
3331 strncpy (outp
, tmp
, len
);
3336 elt_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
3337 addrval
= value_as_pointer (arg_val
);
3339 if (TYPE_LENGTH (elt_type
) == 1 &&
3340 TYPE_CODE (elt_type
) == TYPE_CODE_INT
&&
3343 /* display it as a string */
3344 char *default_fmt
= "%s";
3348 /* Limiting the number of bytes that the following call
3349 will read protects us from sprintf overflow later. */
3350 i
= target_read_string (addrval
, /* src */
3352 available
, /* len */
3354 if (err
) /* read failed */
3355 error ("%s on target_read", safe_strerror (err
));
3357 tmp
[i
] = '\0'; /* force-terminate string */
3358 /* FIXME: how to protect GDB from crashing
3359 from bad user-supplied format string? */
3360 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3366 /* display it as a pointer */
3367 char *default_fmt
= "0x%x";
3369 /* FIXME: how to protect GDB from crashing
3370 from bad user-supplied format string? */
3371 if (available
>= 16 /*? */ ) /* overflow protect */
3372 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3378 char *default_fmt
= "%g";
3380 doubleval
= value_as_double (arg_val
);
3381 /* FIXME: how to protect GDB from crashing
3382 from bad user-supplied format string? */
3383 if (available
>= 16 /*? */ ) /* overflow protect */
3384 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3385 (double) doubleval
);
3390 char *default_fmt
= "%d";
3392 longval
= value_as_long (arg_val
);
3393 /* FIXME: how to protect GDB from crashing
3394 from bad user-supplied format string? */
3395 if (available
>= 16 /*? */ ) /* overflow protect */
3396 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3400 case TYPE_CODE_BOOL
:
3402 /* no default format for bool */
3403 longval
= value_as_long (arg_val
);
3404 if (available
>= 8 /*? */ ) /* overflow protect */
3407 strcpy (outp
, "<true>");
3409 strcpy (outp
, "<false>");
3413 case TYPE_CODE_ENUM
:
3415 /* no default format for enum */
3416 longval
= value_as_long (arg_val
);
3417 len
= TYPE_NFIELDS (arg_type
);
3418 /* find enum name if possible */
3419 for (i
= 0; i
< len
; i
++)
3420 if (TYPE_FIELD_BITPOS (arg_type
, i
) == longval
)
3421 break; /* match -- end loop */
3423 if (i
< len
) /* enum name found */
3425 char *name
= TYPE_FIELD_NAME (arg_type
, i
);
3427 strncpy (outp
, name
, available
);
3428 /* in casel available < strlen (name), */
3429 outp
[available
] = '\0';
3433 if (available
>= 16 /*? */ ) /* overflow protect */
3434 sprintf (outp
, "%ld", (long) longval
);
3438 case TYPE_CODE_VOID
:
3440 break; /* void type -- no output */
3442 error ("bad data type at prompt position %d",
3443 promptp
- local_prompt
);
3446 outp
+= strlen (outp
);
3449 *outp
++ = '\0'; /* terminate prompt string */
3457 static char buf
[MAX_PROMPT_SIZE
];
3459 if (catch_errors (get_prompt_1
, buf
, "bad formatted prompt: ",
3462 return &buf
[0]; /* successful formatted prompt */
3466 /* Prompt could not be formatted. */
3470 return gdb_prompt_string
;
3475 set_prompt (char *s
)
3477 /* ??rehrauer: I don't know why this fails, since it looks as though
3478 assignments to prompt are wrapped in calls to savestring...
3483 PROMPT (0) = savestring (s
, strlen (s
));
3485 gdb_prompt_string
= savestring (s
, strlen (s
));
3489 /* If necessary, make the user confirm that we should quit. Return
3490 non-zero if we should quit, zero if we shouldn't. */
3495 if (inferior_pid
!= 0 && target_has_execution
)
3499 /* This is something of a hack. But there's no reliable way to
3500 see if a GUI is running. The `use_windows' variable doesn't
3503 s
= "A debugging session is active.\nDo you still want to close the debugger?";
3504 else if (attach_flag
)
3505 s
= "The program is running. Quit anyway (and detach it)? ";
3507 s
= "The program is running. Exit anyway? ";
3516 /* Quit without asking for confirmation. */
3519 quit_force (char *args
, int from_tty
)
3523 /* An optional expression may be used to cause gdb to terminate with the
3524 value of that expression. */
3527 value_ptr val
= parse_and_eval (args
);
3529 exit_code
= (int) value_as_long (val
);
3532 if (inferior_pid
!= 0 && target_has_execution
)
3535 target_detach (args
, from_tty
);
3540 /* UDI wants this, to kill the TIP. */
3543 /* Save the history information if it is appropriate to do so. */
3544 if (write_history_p
&& history_filename
)
3545 write_history (history_filename
);
3547 do_final_cleanups (ALL_CLEANUPS
); /* Do any final cleanups before exiting */
3550 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3551 /* The above does not need to be inside a tuiDo(), since
3552 * it is not manipulating the curses screen, but rather,
3553 * it is tearing it down.
3562 /* Handle the quit command. */
3565 quit_command (char *args
, int from_tty
)
3567 if (!quit_confirm ())
3568 error ("Not confirmed.");
3569 quit_force (args
, from_tty
);
3572 /* Returns whether GDB is running on a terminal and whether the user
3573 desires that questions be asked of them on that terminal. */
3576 input_from_terminal_p (void)
3578 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
3583 pwd_command (char *args
, int from_tty
)
3586 error ("The \"pwd\" command does not take an argument: %s", args
);
3587 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3589 if (!STREQ (gdb_dirbuf
, current_directory
))
3590 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3591 current_directory
, gdb_dirbuf
);
3593 printf_unfiltered ("Working directory %s.\n", current_directory
);
3597 cd_command (char *dir
, int from_tty
)
3600 /* Found something other than leading repetitions of "/..". */
3601 int found_real_path
;
3604 /* If the new directory is absolute, repeat is a no-op; if relative,
3605 repeat might be useful but is more likely to be a mistake. */
3609 error_no_arg ("new working directory");
3611 dir
= tilde_expand (dir
);
3612 make_cleanup (free
, dir
);
3614 if (chdir (dir
) < 0)
3615 perror_with_name (dir
);
3617 #if defined(_WIN32) || defined(__MSDOS__)
3618 /* There's too much mess with DOSish names like "d:", "d:.",
3619 "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
3620 simply get the canonicalized name of the current directory. */
3621 dir
= getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3625 if (SLASH_P (dir
[len
- 1]))
3627 /* Remove the trailing slash unless this is a root directory
3628 (including a drive letter on non-Unix systems). */
3629 if (!(len
== 1) /* "/" */
3630 #if defined(_WIN32) || defined(__MSDOS__)
3631 && !(!SLASH_P (*dir
) && ROOTED_P (dir
) && len
<= 3) /* "d:/" */
3637 dir
= savestring (dir
, len
);
3639 current_directory
= dir
;
3642 if (SLASH_P (current_directory
[strlen (current_directory
) - 1]))
3643 current_directory
= concat (current_directory
, dir
, NULL
);
3645 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
3649 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3651 found_real_path
= 0;
3652 for (p
= current_directory
; *p
;)
3654 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
3656 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
3657 && (p
[3] == 0 || SLASH_P (p
[3])))
3659 if (found_real_path
)
3661 /* Search backwards for the directory just before the "/.."
3662 and obliterate it and the "/..". */
3664 while (q
!= current_directory
&& !SLASH_P (q
[-1]))
3667 if (q
== current_directory
)
3668 /* current_directory is
3669 a relative pathname ("can't happen"--leave it alone). */
3673 strcpy (q
- 1, p
+ 3);
3678 /* We are dealing with leading repetitions of "/..", for example
3679 "/../..", which is the Mach super-root. */
3684 found_real_path
= 1;
3689 forget_cached_source_info ();
3692 pwd_command ((char *) 0, 1);
3695 struct source_cleanup_lines_args
3699 char *old_pre_error
;
3700 char *old_error_pre_print
;
3704 source_cleanup_lines (PTR args
)
3706 struct source_cleanup_lines_args
*p
=
3707 (struct source_cleanup_lines_args
*) args
;
3708 source_line_number
= p
->old_line
;
3709 source_file_name
= p
->old_file
;
3710 source_pre_error
= p
->old_pre_error
;
3711 error_pre_print
= p
->old_error_pre_print
;
3716 do_fclose_cleanup (void *stream
)
3722 source_command (char *args
, int from_tty
)
3725 struct cleanup
*old_cleanups
;
3727 struct source_cleanup_lines_args old_lines
;
3732 error ("source command requires pathname of file to source.");
3735 file
= tilde_expand (file
);
3736 old_cleanups
= make_cleanup (free
, file
);
3738 stream
= fopen (file
, FOPEN_RT
);
3742 perror_with_name (file
);
3747 make_cleanup (do_fclose_cleanup
, stream
);
3749 old_lines
.old_line
= source_line_number
;
3750 old_lines
.old_file
= source_file_name
;
3751 old_lines
.old_pre_error
= source_pre_error
;
3752 old_lines
.old_error_pre_print
= error_pre_print
;
3753 make_cleanup (source_cleanup_lines
, &old_lines
);
3754 source_line_number
= 0;
3755 source_file_name
= file
;
3756 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
3757 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
3758 make_cleanup (free
, source_pre_error
);
3759 /* This will get set every time we read a line. So it won't stay "" for
3761 error_pre_print
= "";
3763 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
3764 if (source_error_allocated
< needed_length
)
3766 source_error_allocated
*= 2;
3767 if (source_error_allocated
< needed_length
)
3768 source_error_allocated
= needed_length
;
3769 if (source_error
== NULL
)
3770 source_error
= xmalloc (source_error_allocated
);
3772 source_error
= xrealloc (source_error
, source_error_allocated
);
3775 read_command_file (stream
);
3777 do_cleanups (old_cleanups
);
3782 echo_command (char *text
, int from_tty
)
3788 while ((c
= *p
++) != '\0')
3792 /* \ at end of argument is used after spaces
3793 so they won't be lost. */
3797 c
= parse_escape (&p
);
3799 printf_filtered ("%c", c
);
3802 printf_filtered ("%c", c
);
3805 /* Force this output to appear now. */
3807 gdb_flush (gdb_stdout
);
3812 dont_repeat_command (char *ignored
, int from_tty
)
3814 *line
= 0; /* Can't call dont_repeat here because we're not
3815 necessarily reading from stdin. */
3818 /* Functions to manipulate command line editing control variables. */
3820 /* Number of commands to print in each call to show_commands. */
3821 #define Hist_print 10
3823 show_commands (char *args
, int from_tty
)
3825 /* Index for history commands. Relative to history_base. */
3828 /* Number of the history entry which we are planning to display next.
3829 Relative to history_base. */
3832 /* The first command in the history which doesn't exist (i.e. one more
3833 than the number of the last command). Relative to history_base. */
3836 extern HIST_ENTRY
*history_get (int);
3838 /* Print out some of the commands from the command history. */
3839 /* First determine the length of the history list. */
3840 hist_len
= history_size
;
3841 for (offset
= 0; offset
< history_size
; offset
++)
3843 if (!history_get (history_base
+ offset
))
3852 if (args
[0] == '+' && args
[1] == '\0')
3853 /* "info editing +" should print from the stored position. */
3856 /* "info editing <exp>" should print around command number <exp>. */
3857 num
= (parse_and_eval_long (args
) - history_base
) - Hist_print
/ 2;
3859 /* "show commands" means print the last Hist_print commands. */
3862 num
= hist_len
- Hist_print
;
3868 /* If there are at least Hist_print commands, we want to display the last
3869 Hist_print rather than, say, the last 6. */
3870 if (hist_len
- num
< Hist_print
)
3872 num
= hist_len
- Hist_print
;
3877 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3879 printf_filtered ("%5d %s\n", history_base
+ offset
,
3880 (history_get (history_base
+ offset
))->line
);
3883 /* The next command we want to display is the next one that we haven't
3887 /* If the user repeats this command with return, it should do what
3888 "show commands +" does. This is unnecessary if arg is null,
3889 because "show commands +" is not useful after "show commands". */
3890 if (from_tty
&& args
)
3897 /* Called by do_setshow_command. */
3900 set_history_size_command (char *args
, int from_tty
, struct cmd_list_element
*c
)
3902 if (history_size
== INT_MAX
)
3903 unstifle_history ();
3904 else if (history_size
>= 0)
3905 stifle_history (history_size
);
3908 history_size
= INT_MAX
;
3909 error ("History size must be non-negative");
3915 set_history (char *args
, int from_tty
)
3917 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3918 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3923 show_history (char *args
, int from_tty
)
3925 cmd_show_list (showhistlist
, from_tty
, "");
3928 int info_verbose
= 0; /* Default verbose msgs off */
3930 /* Called by do_setshow_command. An elaborate joke. */
3933 set_verbose (char *args
, int from_tty
, struct cmd_list_element
*c
)
3935 char *cmdname
= "verbose";
3936 struct cmd_list_element
*showcmd
;
3938 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3942 c
->doc
= "Set verbose printing of informational messages.";
3943 showcmd
->doc
= "Show verbose printing of informational messages.";
3947 c
->doc
= "Set verbosity.";
3948 showcmd
->doc
= "Show verbosity.";
3953 float_handler (int signo
)
3955 /* This message is based on ANSI C, section 4.7. Note that integer
3956 divide by zero causes this, so "float" is a misnomer. */
3957 signal (SIGFPE
, float_handler
);
3958 error ("Erroneous arithmetic operation.");
3962 set_debug (char *arg
, int from_tty
)
3964 printf_unfiltered ("\"set debug\" must be followed by the name of a print subcommand.\n");
3965 help_list (setdebuglist
, "set debug ", -1, gdb_stdout
);
3969 show_debug (char *args
, int from_tty
)
3971 cmd_show_list (showdebuglist
, from_tty
, "");
3975 init_cmd_lists (void)
3984 enablebreaklist
= NULL
;
3989 showhistlist
= NULL
;
3990 unsethistlist
= NULL
;
3991 maintenancelist
= NULL
;
3992 maintenanceinfolist
= NULL
;
3993 maintenanceprintlist
= NULL
;
3994 setprintlist
= NULL
;
3995 showprintlist
= NULL
;
3996 setchecklist
= NULL
;
3997 showchecklist
= NULL
;
4000 /* Init the history buffer. Note that we are called after the init file(s)
4001 * have been read so that the user can change the history file via his
4002 * .gdbinit file (for instance). The GDBHISTFILE environment variable
4003 * overrides all of this.
4011 tmpenv
= getenv ("HISTSIZE");
4013 history_size
= atoi (tmpenv
);
4014 else if (!history_size
)
4017 stifle_history (history_size
);
4019 tmpenv
= getenv ("GDBHISTFILE");
4021 history_filename
= savestring (tmpenv
, strlen (tmpenv
));
4022 else if (!history_filename
)
4024 /* We include the current directory so that if the user changes
4025 directories the file written will be the same as the one
4028 /* No leading dots in file names are allowed on MSDOS. */
4029 history_filename
= concat (current_directory
, "/_gdb_history", NULL
);
4031 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
4034 read_history (history_filename
);
4040 struct cmd_list_element
*c
;
4042 /* If we are running the asynchronous version,
4043 we initialize the prompts differently. */
4046 gdb_prompt_string
= savestring (DEFAULT_PROMPT
, strlen (DEFAULT_PROMPT
));
4050 /* initialize the prompt stack to a simple "(gdb) " prompt or to
4051 whatever the DEFAULT_PROMPT is. */
4052 the_prompts
.top
= 0;
4054 PROMPT (0) = savestring (DEFAULT_PROMPT
, strlen (DEFAULT_PROMPT
));
4056 /* Set things up for annotation_level > 1, if the user ever decides
4058 async_annotation_suffix
= "prompt";
4059 /* Set the variable associated with the setshow prompt command. */
4060 new_async_prompt
= savestring (PROMPT (0), strlen (PROMPT (0)));
4062 gdb_prompt_escape
= 0; /* default to none. */
4064 /* Set the important stuff up for command editing. */
4065 command_editing_p
= 1;
4066 history_expansion_p
= 0;
4067 write_history_p
= 0;
4069 /* Setup important stuff for command line editing. */
4070 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
4071 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
4072 rl_completer_quote_characters
= gdb_completer_quote_characters
;
4073 rl_readline_name
= "gdb";
4075 /* Define the classes of commands.
4076 They will appear in the help list in the reverse of this order. */
4078 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
4079 "Maintenance commands.\n\
4080 Some gdb commands are provided just for use by gdb maintainers.\n\
4081 These commands are subject to frequent change, and may not be as\n\
4082 well documented as user commands.",
4084 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
4085 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
4086 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
4087 The commands in this class are those defined by the user.\n\
4088 Use the \"define\" command to define a command.", &cmdlist
);
4089 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
4091 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
4092 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
4093 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
4094 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
4095 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
4096 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
4097 counting from zero for the innermost (currently executing) frame.\n\n\
4098 At any time gdb identifies one frame as the \"selected\" frame.\n\
4099 Variable lookups are done with respect to the selected frame.\n\
4100 When the program being debugged stops, gdb selects the innermost frame.\n\
4101 The commands below can be used to select other frames by number or address.",
4103 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
4105 add_com ("pwd", class_files
, pwd_command
,
4106 "Print working directory. This is used for your program as well.");
4107 c
= add_cmd ("cd", class_files
, cd_command
,
4108 "Set working directory to DIR for debugger and program being debugged.\n\
4109 The change does not take effect for the program being debugged\n\
4110 until the next time it is started.", &cmdlist
);
4111 c
->completer
= filename_completer
;
4113 /* The set prompt command is different depending whether or not the
4114 async version is run. NOTE: this difference is going to
4115 disappear as we make the event loop be the default engine of
4120 (add_set_cmd ("prompt", class_support
, var_string
,
4121 (char *) &gdb_prompt_string
, "Set gdb's prompt",
4127 c
= add_set_cmd ("prompt", class_support
, var_string
,
4128 (char *) &new_async_prompt
, "Set gdb's prompt",
4130 add_show_from_set (c
, &showlist
);
4131 c
->function
.sfunc
= set_async_prompt
;
4135 (add_set_cmd ("prompt-escape-char", class_support
, var_zinteger
,
4136 (char *) &gdb_prompt_escape
,
4137 "Set escape character for formatting of gdb's prompt",
4141 add_com ("echo", class_support
, echo_command
,
4142 "Print a constant string. Give string as argument.\n\
4143 C escape sequences may be used in the argument.\n\
4144 No newline is added at the end of the argument;\n\
4145 use \"\\n\" if you want a newline to be printed.\n\
4146 Since leading and trailing whitespace are ignored in command arguments,\n\
4147 if you want to print some you must use \"\\\" before leading whitespace\n\
4148 to be printed or after trailing whitespace.");
4149 add_com ("document", class_support
, document_command
,
4150 "Document a user-defined command.\n\
4151 Give command name as argument. Give documentation on following lines.\n\
4152 End with a line of just \"end\".");
4153 add_com ("define", class_support
, define_command
,
4154 "Define a new command name. Command name is argument.\n\
4155 Definition appears on following lines, one command per line.\n\
4156 End with a line of just \"end\".\n\
4157 Use the \"document\" command to give documentation for the new command.\n\
4158 Commands defined in this way may have up to ten arguments.");
4161 c
= add_cmd ("source", class_support
, source_command
,
4162 "Read commands from a file named FILE.\n\
4163 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
4164 when gdb is started.", &cmdlist
);
4166 /* Punt file name, we can't help it easily. */
4167 c
= add_cmd ("source", class_support
, source_command
,
4168 "Read commands from a file named FILE.\n\
4169 Note that the file \".gdbinit\" is read automatically in this way\n\
4170 when gdb is started.", &cmdlist
);
4172 c
->completer
= filename_completer
;
4174 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
4175 add_com ("help", class_support
, help_command
, "Print list of commands.");
4176 add_com_alias ("q", "quit", class_support
, 1);
4177 add_com_alias ("h", "help", class_support
, 1);
4179 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
4180 Primarily used inside of user-defined commands that should not be repeated when\n\
4183 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *) &info_verbose
,
4186 add_show_from_set (c
, &showlist
);
4187 c
->function
.sfunc
= set_verbose
;
4188 set_verbose (NULL
, 0, c
);
4190 /* The set editing command is different depending whether or not the
4191 async version is run. NOTE: this difference is going to disappear
4192 as we make the event loop be the default engine of gdb. */
4196 (add_set_cmd ("editing", class_support
, var_boolean
, (char *) &command_editing_p
,
4197 "Set editing of command lines as they are typed.\n\
4198 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4199 Without an argument, command line editing is enabled. To edit, use\n\
4200 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
4205 c
= add_set_cmd ("editing", class_support
, var_boolean
, (char *) &async_command_editing_p
,
4206 "Set editing of command lines as they are typed.\n\
4207 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4208 Without an argument, command line editing is enabled. To edit, use\n\
4209 EMACS-like or VI-like commands like control-P or ESC.", &setlist
);
4211 add_show_from_set (c
, &showlist
);
4212 c
->function
.sfunc
= set_async_editing_command
;
4215 add_prefix_cmd ("history", class_support
, set_history
,
4216 "Generic command for setting command history parameters.",
4217 &sethistlist
, "set history ", 0, &setlist
);
4218 add_prefix_cmd ("history", class_support
, show_history
,
4219 "Generic command for showing command history parameters.",
4220 &showhistlist
, "show history ", 0, &showlist
);
4223 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *) &history_expansion_p
,
4224 "Set history expansion on command input.\n\
4225 Without an argument, history expansion is enabled.", &sethistlist
),
4229 (add_set_cmd ("save", no_class
, var_boolean
, (char *) &write_history_p
,
4230 "Set saving of the history record on exit.\n\
4231 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
4232 Without an argument, saving is enabled.", &sethistlist
),
4235 c
= add_set_cmd ("size", no_class
, var_integer
, (char *) &history_size
,
4236 "Set the size of the command history, \n\
4237 ie. the number of previous commands to keep a record of.", &sethistlist
);
4238 add_show_from_set (c
, &showhistlist
);
4239 c
->function
.sfunc
= set_history_size_command
;
4242 (add_set_cmd ("filename", no_class
, var_filename
, (char *) &history_filename
,
4243 "Set the filename in which to record the command history\n\
4244 (the list of previous commands of which a record is kept).", &sethistlist
),
4248 (add_set_cmd ("confirm", class_support
, var_boolean
,
4250 "Set whether to confirm potentially dangerous operations.",
4254 add_prefix_cmd ("info", class_info
, info_command
,
4255 "Generic command for showing things about the program being debugged.",
4256 &infolist
, "info ", 0, &cmdlist
);
4257 add_com_alias ("i", "info", class_info
, 1);
4259 add_com ("complete", class_obscure
, complete_command
,
4260 "List the completions for the rest of the line as a command.");
4262 add_prefix_cmd ("show", class_info
, show_command
,
4263 "Generic command for showing things about the debugger.",
4264 &showlist
, "show ", 0, &cmdlist
);
4265 /* Another way to get at the same thing. */
4266 add_info ("set", show_command
, "Show all GDB settings.");
4268 add_cmd ("commands", no_class
, show_commands
,
4269 "Show the history of commands you typed.\n\
4270 You can supply a command number to start with, or a `+' to start after\n\
4271 the previous command number shown.",
4274 add_cmd ("version", no_class
, show_version
,
4275 "Show what version of GDB this is.", &showlist
);
4277 add_com ("while", class_support
, while_command
,
4278 "Execute nested commands WHILE the conditional expression is non zero.\n\
4279 The conditional expression must follow the word `while' and must in turn be\n\
4280 followed by a new line. The nested commands must be entered one per line,\n\
4281 and should be terminated by the word `end'.");
4283 add_com ("if", class_support
, if_command
,
4284 "Execute nested commands once IF the conditional expression is non zero.\n\
4285 The conditional expression must follow the word `if' and must in turn be\n\
4286 followed by a new line. The nested commands must be entered one per line,\n\
4287 and should be terminated by the word 'else' or `end'. If an else clause\n\
4288 is used, the same rules apply to its nested commands as to the first ones.");
4290 /* If target is open when baud changes, it doesn't take effect until the
4291 next open (I think, not sure). */
4292 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
4293 var_zinteger
, (char *) &baud_rate
,
4294 "Set baud rate for remote serial I/O.\n\
4295 This value is used to set the speed of the serial port when debugging\n\
4296 using remote targets.", &setlist
),
4299 c
= add_set_cmd ("remotedebug", no_class
, var_zinteger
,
4300 (char *) &remote_debug
,
4301 "Set debugging of remote protocol.\n\
4302 When enabled, each packet sent or received with the remote target\n\
4303 is displayed.", &setlist
);
4304 deprecate_cmd (c
, "set debug remote");
4305 deprecate_cmd (add_show_from_set (c
, &showlist
), "show debug remote");
4307 add_show_from_set (add_set_cmd ("remote", no_class
, var_zinteger
,
4308 (char *) &remote_debug
,
4309 "Set debugging of remote protocol.\n\
4310 When enabled, each packet sent or received with the remote target\n\
4311 is displayed.", &setdebuglist
),
4315 add_set_cmd ("remotetimeout", no_class
, var_integer
, (char *) &remote_timeout
,
4316 "Set timeout limit to wait for target to respond.\n\
4317 This value is used to set the time limit for gdb to wait for a response\n\
4318 from the target.", &setlist
),
4321 /* The set annotate command is different depending whether or not
4322 the async version is run. NOTE: this difference is going to
4323 disappear as we make the event loop be the default engine of
4327 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
4328 (char *) &annotation_level
, "Set annotation_level.\n\
4329 0 == normal; 1 == fullname (for use when running under emacs)\n\
4330 2 == output annotated suitably for use by programs that control GDB.",
4332 c
= add_show_from_set (c
, &showlist
);
4336 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
4337 (char *) &annotation_level
, "Set annotation_level.\n\
4338 0 == normal; 1 == fullname (for use when running under emacs)\n\
4339 2 == output annotated suitably for use by programs that control GDB.",
4341 add_show_from_set (c
, &showlist
);
4342 c
->function
.sfunc
= set_async_annotation_level
;
4347 (add_set_cmd ("exec-done-display", class_support
, var_boolean
, (char *) &exec_done_display_p
,
4348 "Set notification of completion for asynchronous execution commands.\n\
4349 Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist
),
4352 add_prefix_cmd ("debug", no_class
, set_debug
,
4353 "Generic command for setting gdb debugging flags",
4354 &setdebuglist
, "set debug ", 0, &setlist
);
4356 add_prefix_cmd ("debug", no_class
, show_debug
,
4357 "Generic command for showing gdb debugging flags",
4358 &showdebuglist
, "show debug ", 0, &showlist
);