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 /* NOTE 1999-04-29: this function will be static again, after we make the
77 event loop be the default command loop for gdb, and we merge
78 event-top.c into this file, top.c */
79 /* static */ void command_loop_marker (void *);
81 static void while_command (char *, int);
83 static void if_command (char *, int);
85 static struct command_line
*build_command_line (enum command_control_type
,
88 static struct command_line
*get_command_line (enum command_control_type
,
91 static void realloc_body_list (struct command_line
*, int);
93 static enum misc_command_type
read_next_line (struct command_line
**);
95 static enum command_control_type
96 recurse_read_control_structure (struct command_line
*);
98 static struct cleanup
*setup_user_args (char *);
100 static char *locate_arg (char *);
102 static char *insert_args (char *);
104 static void arg_cleanup (void *);
106 static void init_main (void);
108 static void init_cmd_lists (void);
110 static void float_handler (int);
112 static void init_signals (void);
114 static void set_verbose (char *, int, struct cmd_list_element
*);
116 static void show_history (char *, int);
118 static void set_history (char *, int);
120 static void set_history_size_command (char *, int, struct cmd_list_element
*);
122 static void show_commands (char *, int);
124 static void echo_command (char *, int);
126 static void pwd_command (char *, int);
128 static void show_version (char *, int);
130 static void document_command (char *, int);
132 static void define_command (char *, int);
134 static void validate_comname (char *);
136 static void help_command (char *, int);
138 static void show_command (char *, int);
140 static void info_command (char *, int);
142 static void complete_command (char *, int);
144 static void do_nothing (int);
146 static void show_debug (char *, int);
148 static void set_debug (char *, int);
151 /* NOTE 1999-04-29: This function will be static again, once we modify
152 gdb to use the event loop as the default command loop and we merge
153 event-top.c into this file, top.c */
154 /* static */ int quit_cover (PTR
);
156 static void disconnect (int);
159 static void do_restore_instream_cleanup (void *stream
);
161 static struct cleanup
*make_cleanup_free_command_lines (struct command_line
**);
163 /* Default command line prompt. This is overriden in some configs. */
165 #ifndef DEFAULT_PROMPT
166 #define DEFAULT_PROMPT "(gdb) "
169 /* Initialization file name for gdb. This is overridden in some configs. */
171 #ifndef GDBINIT_FILENAME
172 #define GDBINIT_FILENAME ".gdbinit"
174 char gdbinit
[] = GDBINIT_FILENAME
;
176 int inhibit_gdbinit
= 0;
178 /* If nonzero, and GDB has been configured to be able to use windows,
179 attempt to open them upon startup. */
183 extern char lang_frame_mismatch_warn
[]; /* language.c */
185 /* Flag for whether we want all the "from_tty" gubbish printed. */
187 int caution
= 1; /* Default is yes, sigh. */
189 /* Define all cmd_list_elements. */
191 /* Chain containing all defined commands. */
193 struct cmd_list_element
*cmdlist
;
195 /* Chain containing all defined info subcommands. */
197 struct cmd_list_element
*infolist
;
199 /* Chain containing all defined enable subcommands. */
201 struct cmd_list_element
*enablelist
;
203 /* Chain containing all defined disable subcommands. */
205 struct cmd_list_element
*disablelist
;
207 /* Chain containing all defined toggle subcommands. */
209 struct cmd_list_element
*togglelist
;
211 /* Chain containing all defined stop subcommands. */
213 struct cmd_list_element
*stoplist
;
215 /* Chain containing all defined delete subcommands. */
217 struct cmd_list_element
*deletelist
;
219 /* Chain containing all defined "enable breakpoint" subcommands. */
221 struct cmd_list_element
*enablebreaklist
;
223 /* Chain containing all defined set subcommands */
225 struct cmd_list_element
*setlist
;
227 /* Chain containing all defined unset subcommands */
229 struct cmd_list_element
*unsetlist
;
231 /* Chain containing all defined show subcommands. */
233 struct cmd_list_element
*showlist
;
235 /* Chain containing all defined \"set history\". */
237 struct cmd_list_element
*sethistlist
;
239 /* Chain containing all defined \"show history\". */
241 struct cmd_list_element
*showhistlist
;
243 /* Chain containing all defined \"unset history\". */
245 struct cmd_list_element
*unsethistlist
;
247 /* Chain containing all defined maintenance subcommands. */
249 struct cmd_list_element
*maintenancelist
;
251 /* Chain containing all defined "maintenance info" subcommands. */
253 struct cmd_list_element
*maintenanceinfolist
;
255 /* Chain containing all defined "maintenance print" subcommands. */
257 struct cmd_list_element
*maintenanceprintlist
;
259 struct cmd_list_element
*setprintlist
;
261 struct cmd_list_element
*showprintlist
;
263 struct cmd_list_element
*setdebuglist
;
265 struct cmd_list_element
*showdebuglist
;
267 struct cmd_list_element
*setchecklist
;
269 struct cmd_list_element
*showchecklist
;
271 /* stdio stream that command input is being read from. Set to stdin normally.
272 Set by source_command to the file we are sourcing. Set to NULL if we are
273 executing a user-defined command or interacting via a GUI. */
277 /* Current working directory. */
279 char *current_directory
;
281 /* The directory name is actually stored here (usually). */
282 char gdb_dirbuf
[1024];
284 /* Function to call before reading a command, if nonzero.
285 The function receives two args: an input stream,
286 and a prompt string. */
288 void (*window_hook
) (FILE *, char *);
293 /* gdb prints this when reading a command interactively */
294 static char *gdb_prompt_string
; /* the global prompt string */
295 extern char *get_prompt (void); /* access function for prompt string */
297 /* Buffer used for reading command lines, and the size
298 allocated for it so far. */
303 /* Nonzero if the current command is modified by "server ". This
304 affects things like recording into the command history, commands
305 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
306 whatever) can issue its own commands and also send along commands
307 from the user, and have the user not notice that the user interface
308 is issuing commands too. */
311 /* Baud rate specified for talking to serial target systems. Default
312 is left as -1, so targets can choose their own defaults. */
313 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
314 or (unsigned int)-1. This is a Bad User Interface. */
318 /* Timeout limit for response from target. */
320 /* The default value has been changed many times over the years. It
321 was originally 5 seconds. But that was thought to be a long time
322 to sit and wait, so it was changed to 2 seconds. That was thought
323 to be plenty unless the connection was going through some terminal
324 server or multiplexer or other form of hairy serial connection.
326 In mid-1996, remote_timeout was moved from remote.c to top.c and
327 it began being used in other remote-* targets. It appears that the
328 default was changed to 20 seconds at that time, perhaps because the
329 Hitachi E7000 ICE didn't always respond in a timely manner.
331 But if 5 seconds is a long time to sit and wait for retransmissions,
332 20 seconds is far worse. This demonstrates the difficulty of using
333 a single variable for all protocol timeouts.
335 As remote.c is used much more than remote-e7000.c, it was changed
336 back to 2 seconds in 1999. */
338 int remote_timeout
= 2;
340 /* Non-zero tells remote* modules to output debugging info. */
342 int remote_debug
= 0;
344 /* Non-zero means the target is running. Note: this is different from
345 saying that there is an active target and we are stopped at a
346 breakpoint, for instance. This is a real indicator whether the
347 target is off and running, which gdb is doing something else. */
348 int target_executing
= 0;
350 /* Level of control structure. */
351 static int control_level
;
353 /* Structure for arguments to user defined functions. */
354 #define MAXUSERARGS 10
357 struct user_args
*next
;
368 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
372 #define STOP_SIGNAL SIGTSTP
373 static void stop_sig (int);
377 /* Some System V have job control but not sigsetmask(). */
378 #if !defined (HAVE_SIGSETMASK)
380 #define HAVE_SIGSETMASK 1
382 #define HAVE_SIGSETMASK 0
386 #if 0 == (HAVE_SIGSETMASK)
387 #define sigsetmask(n)
390 /* Hooks for alternate command interfaces. */
392 /* Called after most modules have been initialized, but before taking users
395 void (*init_ui_hook
) (char *argv0
);
397 /* This hook is called from within gdb's many mini-event loops which could
398 steal control from a real user interface's event loop. It returns
399 non-zero if the user is requesting a detach, zero otherwise. */
401 int (*ui_loop_hook
) (int);
403 /* Called instead of command_loop at top level. Can be invoked via
404 return_to_top_level. */
406 void (*command_loop_hook
) (void);
409 /* Called from print_frame_info to list the line we stopped in. */
411 void (*print_frame_info_listing_hook
) (struct symtab
* s
, int line
,
412 int stopline
, int noerror
);
413 /* Replaces most of query. */
415 int (*query_hook
) (const char *, va_list);
417 /* Replaces most of warning. */
419 void (*warning_hook
) (const char *, va_list);
421 /* These three functions support getting lines of text from the user. They
422 are used in sequence. First readline_begin_hook is called with a text
423 string that might be (for example) a message for the user to type in a
424 sequence of commands to be executed at a breakpoint. If this function
425 calls back to a GUI, it might take this opportunity to pop up a text
426 interaction window with this message. Next, readline_hook is called
427 with a prompt that is emitted prior to collecting the user input.
428 It can be called multiple times. Finally, readline_end_hook is called
429 to notify the GUI that we are done with the interaction window and it
432 void (*readline_begin_hook
) (char *, ...);
433 char *(*readline_hook
) (char *);
434 void (*readline_end_hook
) (void);
436 /* Called as appropriate to notify the interface of the specified breakpoint
439 void (*create_breakpoint_hook
) (struct breakpoint
* bpt
);
440 void (*delete_breakpoint_hook
) (struct breakpoint
* bpt
);
441 void (*modify_breakpoint_hook
) (struct breakpoint
* bpt
);
443 /* Called as appropriate to notify the interface that we have attached
444 to or detached from an already running process. */
446 void (*attach_hook
) (void);
447 void (*detach_hook
) (void);
449 /* Called during long calculations to allow GUI to repair window damage, and to
450 check for stop buttons, etc... */
452 void (*interactive_hook
) (void);
454 /* Called when the registers have changed, as a hint to a GUI
455 to minimize window update. */
457 void (*registers_changed_hook
) (void);
459 /* Tell the GUI someone changed the register REGNO. -1 means
460 that the caller does not know which register changed or
461 that several registers have changed (see value_assign). */
462 void (*register_changed_hook
) (int regno
);
464 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
465 void (*memory_changed_hook
) (CORE_ADDR addr
, int len
);
467 /* Called when going to wait for the target. Usually allows the GUI to run
468 while waiting for target events. */
470 int (*target_wait_hook
) (int pid
, struct target_waitstatus
* status
);
472 /* Used by UI as a wrapper around command execution. May do various things
473 like enabling/disabling buttons, etc... */
475 void (*call_command_hook
) (struct cmd_list_element
* c
, char *cmd
,
478 /* Called after a `set' command has finished. Is only run if the
479 `set' command succeeded. */
481 void (*set_hook
) (struct cmd_list_element
* c
);
483 /* Called when the current thread changes. Argument is thread id. */
485 void (*context_hook
) (int id
);
487 /* Takes control from error (). Typically used to prevent longjmps out of the
488 middle of the GUI. Usually used in conjunction with a catch routine. */
490 NORETURN
void (*error_hook
) (void) ATTR_NORETURN
;
493 /* One should use catch_errors rather than manipulating these
495 #if defined(HAVE_SIGSETJMP)
496 #define SIGJMP_BUF sigjmp_buf
497 #define SIGSETJMP(buf) sigsetjmp(buf, 1)
498 #define SIGLONGJMP(buf,val) siglongjmp(buf,val)
500 #define SIGJMP_BUF jmp_buf
501 #define SIGSETJMP(buf) setjmp(buf)
502 #define SIGLONGJMP(buf,val) longjmp(buf,val)
505 /* Where to go for return_to_top_level. */
506 static SIGJMP_BUF
*catch_return
;
508 /* Return for reason REASON to the nearest containing catch_errors(). */
511 return_to_top_level (reason
)
512 enum return_reason reason
;
517 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
518 I can think of a reason why that is vital, though). */
519 bpstat_clear_actions (stop_bpstat
); /* Clear queued breakpoint commands */
521 disable_current_display ();
522 do_cleanups (ALL_CLEANUPS
);
523 if (event_loop_p
&& target_can_async_p () && !target_executing
)
524 do_exec_cleanups (ALL_CLEANUPS
);
525 if (event_loop_p
&& sync_execution
)
526 do_exec_error_cleanups (ALL_CLEANUPS
);
528 if (annotation_level
> 1)
539 /* Jump to the containing catch_errors() call, communicating REASON
540 to that call via setjmp's return value. Note that REASON can't
541 be zero, by definition in defs.h. */
543 (NORETURN
void) SIGLONGJMP (*catch_return
, (int) reason
);
546 /* Call FUNC with arg ARGS, catching any errors. If there is no
547 error, return the value returned by FUNC. If there is an error,
548 print ERRSTRING, print the specific error message, then return
551 Must not be called with immediate_quit in effect (bad things might
552 happen, say we got a signal in the middle of a memcpy to quit_return).
553 This is an OK restriction; with very few exceptions immediate_quit can
554 be replaced by judicious use of QUIT.
556 MASK specifies what to catch; it is normally set to
557 RETURN_MASK_ALL, if for no other reason than that the code which
558 calls catch_errors might not be set up to deal with a quit which
559 isn't caught. But if the code can deal with it, it generally
560 should be RETURN_MASK_ERROR, unless for some reason it is more
561 useful to abort only the portion of the operation inside the
562 catch_errors. Note that quit should return to the command line
563 fairly quickly, even if some further processing is being done. */
565 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
566 error() et.al. could maintain a set of flags that indicate the the
567 current state of each of the longjmp buffers. This would give the
568 longjmp code the chance to detect a longjmp botch (before it gets
569 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
570 code also randomly used a SET_TOP_LEVEL macro that directly
571 initialize the longjmp buffers. */
573 /* MAYBE: cagney/1999-11-05: Should the catch_erros and cleanups code
574 be consolidated into a single file instead of being distributed
575 between utils.c and top.c? */
578 catch_errors (func
, args
, errstring
, mask
)
579 catch_errors_ftype
*func
;
584 SIGJMP_BUF
*saved_catch
;
587 struct cleanup
*saved_cleanup_chain
;
588 char *saved_error_pre_print
;
589 char *saved_quit_pre_print
;
591 /* Return value from SIGSETJMP(): enum return_reason if error or
592 quit caught, 0 otherwise. */
595 /* Override error/quit messages during FUNC. */
597 saved_error_pre_print
= error_pre_print
;
598 saved_quit_pre_print
= quit_pre_print
;
600 if (mask
& RETURN_MASK_ERROR
)
601 error_pre_print
= errstring
;
602 if (mask
& RETURN_MASK_QUIT
)
603 quit_pre_print
= errstring
;
605 /* Prevent error/quit during FUNC from calling cleanups established
608 saved_cleanup_chain
= save_cleanups ();
610 /* Call FUNC, catching error/quit events. */
612 saved_catch
= catch_return
;
613 catch_return
= &catch;
614 caught
= SIGSETJMP (catch);
616 val
= (*func
) (args
);
617 catch_return
= saved_catch
;
619 /* FIXME: cagney/1999-11-05: A correct FUNC implementaton will
620 clean things up (restoring the cleanup chain) to the state they
621 were just prior to the call. Unfortunatly, many FUNC's are not
622 that well behaved. This could be fixed by adding either a
623 do_cleanups call (to cover the problem) or an assertion check to
624 detect bad FUNCs code. */
626 /* Restore the cleanup chain and error/quit messages to their
629 restore_cleanups (saved_cleanup_chain
);
631 if (mask
& RETURN_MASK_QUIT
)
632 quit_pre_print
= saved_quit_pre_print
;
633 if (mask
& RETURN_MASK_ERROR
)
634 error_pre_print
= saved_error_pre_print
;
636 /* Return normally if no error/quit event occurred. */
641 /* If the caller didn't request that the event be caught, relay the
642 event to the next containing catch_errors(). */
644 if (!(mask
& RETURN_MASK (caught
)))
645 return_to_top_level (caught
);
647 /* Tell the caller that an event was caught.
649 FIXME: nsd/2000-02-22: When MASK is RETURN_MASK_ALL, the caller
650 can't tell what type of event occurred.
652 A possible fix is to add a new interface, catch_event(), that
653 returns enum return_reason after catching an error or a quit.
655 When returning normally, i.e. without catching an error or a
656 quit, catch_event() could return RETURN_NORMAL, which would be
657 added to enum return_reason. FUNC would return information
658 exclusively via ARGS.
660 Alternatively, normal catch_event() could return FUNC's return
661 value. The caller would need to be aware of potential overlap
662 with enum return_reason, which could be publicly restricted to
663 negative values to simplify return value processing in FUNC and
669 struct captured_command_args
671 catch_command_errors_ftype
*command
;
677 do_captured_command (void *data
)
679 struct captured_command_args
*context
= data
;
680 context
->command (context
->arg
, context
->from_tty
);
681 /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
682 isn't needed. Instead an assertion check could be made that
683 simply confirmed that the called function correctly cleaned up
684 after its self. Unfortunatly, old code (prior to 1999-11-04) in
685 main.c was calling SET_TOP_LEVEL(), calling the command function,
686 and then *always* calling do_cleanups(). For the moment we
687 remain ``bug compatible'' with that old code.. */
688 do_cleanups (ALL_CLEANUPS
);
693 catch_command_errors (catch_command_errors_ftype
* command
,
694 char *arg
, int from_tty
, return_mask mask
)
696 struct captured_command_args args
;
697 args
.command
= command
;
699 args
.from_tty
= from_tty
;
700 return catch_errors (do_captured_command
, &args
, "", mask
);
704 /* Handler for SIGHUP. */
711 catch_errors (quit_cover
, NULL
,
712 "Could not kill the program being debugged", RETURN_MASK_ALL
);
713 signal (SIGHUP
, SIG_DFL
);
714 kill (getpid (), SIGHUP
);
717 /* Just a little helper function for disconnect(). */
719 /* NOTE 1999-04-29: This function will be static again, once we modify
720 gdb to use the event loop as the default command loop and we merge
721 event-top.c into this file, top.c */
726 caution
= 0; /* Throw caution to the wind -- we're exiting.
727 This prevents asking the user dumb questions. */
728 quit_command ((char *) 0, 0);
731 #endif /* defined SIGHUP */
733 /* Line number we are currently in in a file which is being sourced. */
734 /* NOTE 1999-04-29: This variable will be static again, once we modify
735 gdb to use the event loop as the default command loop and we merge
736 event-top.c into this file, top.c */
737 /* static */ int source_line_number
;
739 /* Name of the file we are sourcing. */
740 /* NOTE 1999-04-29: This variable will be static again, once we modify
741 gdb to use the event loop as the default command loop and we merge
742 event-top.c into this file, top.c */
743 /* static */ char *source_file_name
;
745 /* Buffer containing the error_pre_print used by the source stuff.
747 /* NOTE 1999-04-29: This variable will be static again, once we modify
748 gdb to use the event loop as the default command loop and we merge
749 event-top.c into this file, top.c */
750 /* static */ char *source_error
;
751 static int source_error_allocated
;
753 /* Something to glom on to the start of error_pre_print if source_file_name
755 /* NOTE 1999-04-29: This variable will be static again, once we modify
756 gdb to use the event loop as the default command loop and we merge
757 event-top.c into this file, top.c */
758 /* static */ char *source_pre_error
;
760 /* Clean up on error during a "source" command (or execution of a
761 user-defined command). */
764 do_restore_instream_cleanup (void *stream
)
766 /* Restore the previous input stream. */
770 /* Read commands from STREAM. */
772 read_command_file (stream
)
775 struct cleanup
*cleanups
;
777 cleanups
= make_cleanup (do_restore_instream_cleanup
, instream
);
780 do_cleanups (cleanups
);
783 extern void init_proc (void);
785 void (*pre_init_ui_hook
) (void);
789 do_chdir_cleanup (void *old_dir
)
800 if (pre_init_ui_hook
)
803 /* Run the init function of each source file */
805 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
806 current_directory
= gdb_dirbuf
;
809 /* Make sure we return to the original directory upon exit, come
810 what may, since the OS doesn't do that for us. */
811 make_final_cleanup (do_chdir_cleanup
, xstrdup (current_directory
));
814 init_cmd_lists (); /* This needs to be done first */
815 initialize_targets (); /* Setup target_terminal macros for utils.c */
816 initialize_utils (); /* Make errors and warnings possible */
817 initialize_all_files ();
818 initialize_current_architecture ();
819 init_main (); /* But that omits this file! Do it now */
821 /* The signal handling mechanism is different depending whether or
822 not the async version is run. NOTE: in the future we plan to make
823 the event loop be the default engine of gdb, and this difference
826 async_init_signals ();
830 /* We need a default language for parsing expressions, so simple things like
831 "set width 0" won't fail if no language is explicitly set in a config file
832 or implicitly set by reading an executable during startup. */
833 set_language (language_c
);
834 expected_language
= current_language
; /* don't warn about the change. */
837 /* Install the default UI */
838 uiout
= cli_out_new (gdb_stdout
);
842 /* All the interpreters should have had a look at things by now.
843 Initialize the selected interpreter. */
844 if (interpreter_p
&& !init_ui_hook
)
846 fprintf_unfiltered (gdb_stderr
, "Interpreter `%s' unrecognized.\n",
853 init_ui_hook (argv0
);
856 /* Allocate, initialize a new command line structure for one of the
857 control commands (if/while). */
859 static struct command_line
*
860 build_command_line (type
, args
)
861 enum command_control_type type
;
864 struct command_line
*cmd
;
867 error ("if/while commands require arguments.\n");
869 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
871 cmd
->control_type
= type
;
875 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
877 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
878 cmd
->line
= savestring (args
, strlen (args
));
882 /* Build and return a new command structure for the control commands
883 such as "if" and "while". */
885 static struct command_line
*
886 get_command_line (type
, arg
)
887 enum command_control_type type
;
890 struct command_line
*cmd
;
891 struct cleanup
*old_chain
= NULL
;
893 /* Allocate and build a new command line structure. */
894 cmd
= build_command_line (type
, arg
);
896 old_chain
= make_cleanup_free_command_lines (&cmd
);
898 /* Read in the body of this command. */
899 if (recurse_read_control_structure (cmd
) == invalid_control
)
901 warning ("error reading in control structure\n");
902 do_cleanups (old_chain
);
906 discard_cleanups (old_chain
);
910 /* Recursively print a command (including full control structures). */
913 print_command_lines (uiout
, cmd
, depth
)
914 struct ui_out
*uiout
;
915 struct command_line
*cmd
;
918 struct command_line
*list
;
925 ui_out_spaces (uiout
, 2 * depth
);
927 /* A simple command, print it and continue. */
928 if (list
->control_type
== simple_control
)
930 ui_out_field_string (uiout
, NULL
, list
->line
);
931 ui_out_text (uiout
, "\n");
936 /* loop_continue to jump to the start of a while loop, print it
938 if (list
->control_type
== continue_control
)
940 ui_out_field_string (uiout
, NULL
, "loop_continue");
941 ui_out_text (uiout
, "\n");
946 /* loop_break to break out of a while loop, print it and continue. */
947 if (list
->control_type
== break_control
)
949 ui_out_field_string (uiout
, NULL
, "loop_break");
950 ui_out_text (uiout
, "\n");
955 /* A while command. Recursively print its subcommands and continue. */
956 if (list
->control_type
== while_control
)
958 ui_out_text (uiout
, "while ");
959 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
960 ui_out_text (uiout
, "\n");
961 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
962 ui_out_field_string (uiout
, NULL
, "end");
964 ui_out_spaces (uiout
, 2 * depth
);
965 ui_out_text (uiout
, "end\n");
970 /* An if command. Recursively print both arms before continueing. */
971 if (list
->control_type
== if_control
)
973 ui_out_text (uiout
, "if ");
974 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
975 ui_out_text (uiout
, "\n");
977 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
979 /* Show the false arm if it exists. */
980 if (list
->body_count
== 2)
983 ui_out_spaces (uiout
, 2 * depth
);
984 ui_out_field_string (uiout
, NULL
, "else");
985 ui_out_text (uiout
, "else\n");
986 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
989 ui_out_field_string (uiout
, NULL
, "end");
991 ui_out_spaces (uiout
, 2 * depth
);
992 ui_out_text (uiout
, "end\n");
997 /* ignore illegal command type and try next */
1003 print_command_line (cmd
, depth
, stream
)
1004 struct command_line
*cmd
;
1006 struct ui_file
*stream
;
1012 for (i
= 0; i
< depth
; i
++)
1013 fputs_filtered (" ", stream
);
1016 /* A simple command, print it and return. */
1017 if (cmd
->control_type
== simple_control
)
1019 fputs_filtered (cmd
->line
, stream
);
1020 fputs_filtered ("\n", stream
);
1024 /* loop_continue to jump to the start of a while loop, print it
1026 if (cmd
->control_type
== continue_control
)
1028 fputs_filtered ("loop_continue\n", stream
);
1032 /* loop_break to break out of a while loop, print it and return. */
1033 if (cmd
->control_type
== break_control
)
1035 fputs_filtered ("loop_break\n", stream
);
1039 /* A while command. Recursively print its subcommands before returning. */
1040 if (cmd
->control_type
== while_control
)
1042 struct command_line
*list
;
1043 fputs_filtered ("while ", stream
);
1044 fputs_filtered (cmd
->line
, stream
);
1045 fputs_filtered ("\n", stream
);
1046 list
= *cmd
->body_list
;
1049 print_command_line (list
, depth
+ 1, stream
);
1054 /* An if command. Recursively print both arms before returning. */
1055 if (cmd
->control_type
== if_control
)
1057 fputs_filtered ("if ", stream
);
1058 fputs_filtered (cmd
->line
, stream
);
1059 fputs_filtered ("\n", stream
);
1061 print_command_line (cmd
->body_list
[0], depth
+ 1, stream
);
1063 /* Show the false arm if it exists. */
1064 if (cmd
->body_count
== 2)
1068 for (i
= 0; i
< depth
; i
++)
1069 fputs_filtered (" ", stream
);
1071 fputs_filtered ("else\n", stream
);
1072 print_command_line (cmd
->body_list
[1], depth
+ 1, stream
);
1076 for (i
= 0; i
< depth
; i
++)
1077 fputs_filtered (" ", stream
);
1079 fputs_filtered ("end\n", stream
);
1084 /* Execute the command in CMD. */
1086 enum command_control_type
1087 execute_control_command (cmd
)
1088 struct command_line
*cmd
;
1090 struct expression
*expr
;
1091 struct command_line
*current
;
1092 struct cleanup
*old_chain
= 0;
1096 enum command_control_type ret
;
1099 switch (cmd
->control_type
)
1101 case simple_control
:
1102 /* A simple command, execute it and return. */
1103 new_line
= insert_args (cmd
->line
);
1105 return invalid_control
;
1106 old_chain
= make_cleanup (free_current_contents
, &new_line
);
1107 execute_command (new_line
, 0);
1108 ret
= cmd
->control_type
;
1111 case continue_control
:
1113 /* Return for "continue", and "break" so we can either
1114 continue the loop at the top, or break out. */
1115 ret
= cmd
->control_type
;
1120 /* Parse the loop control expression for the while statement. */
1121 new_line
= insert_args (cmd
->line
);
1123 return invalid_control
;
1124 old_chain
= make_cleanup (free_current_contents
, &new_line
);
1125 expr
= parse_expression (new_line
);
1126 make_cleanup (free_current_contents
, &expr
);
1128 ret
= simple_control
;
1131 /* Keep iterating so long as the expression is true. */
1138 /* Evaluate the expression. */
1139 val_mark
= value_mark ();
1140 val
= evaluate_expression (expr
);
1141 cond_result
= value_true (val
);
1142 value_free_to_mark (val_mark
);
1144 /* If the value is false, then break out of the loop. */
1148 /* Execute the body of the while statement. */
1149 current
= *cmd
->body_list
;
1152 ret
= execute_control_command (current
);
1154 /* If we got an error, or a "break" command, then stop
1156 if (ret
== invalid_control
|| ret
== break_control
)
1162 /* If we got a "continue" command, then restart the loop
1164 if (ret
== continue_control
)
1167 /* Get the next statement. */
1168 current
= current
->next
;
1172 /* Reset RET so that we don't recurse the break all the way down. */
1173 if (ret
== break_control
)
1174 ret
= simple_control
;
1181 new_line
= insert_args (cmd
->line
);
1183 return invalid_control
;
1184 old_chain
= make_cleanup (free_current_contents
, &new_line
);
1185 /* Parse the conditional for the if statement. */
1186 expr
= parse_expression (new_line
);
1187 make_cleanup (free_current_contents
, &expr
);
1190 ret
= simple_control
;
1192 /* Evaluate the conditional. */
1193 val_mark
= value_mark ();
1194 val
= evaluate_expression (expr
);
1196 /* Choose which arm to take commands from based on the value of the
1197 conditional expression. */
1198 if (value_true (val
))
1199 current
= *cmd
->body_list
;
1200 else if (cmd
->body_count
== 2)
1201 current
= *(cmd
->body_list
+ 1);
1202 value_free_to_mark (val_mark
);
1204 /* Execute commands in the given arm. */
1207 ret
= execute_control_command (current
);
1209 /* If we got an error, get out. */
1210 if (ret
!= simple_control
)
1213 /* Get the next statement in the body. */
1214 current
= current
->next
;
1221 warning ("Invalid control type in command structure.");
1222 return invalid_control
;
1226 do_cleanups (old_chain
);
1231 /* "while" command support. Executes a body of statements while the
1232 loop condition is nonzero. */
1235 while_command (arg
, from_tty
)
1239 struct command_line
*command
= NULL
;
1242 command
= get_command_line (while_control
, arg
);
1244 if (command
== NULL
)
1247 execute_control_command (command
);
1248 free_command_lines (&command
);
1251 /* "if" command support. Execute either the true or false arm depending
1252 on the value of the if conditional. */
1255 if_command (arg
, from_tty
)
1259 struct command_line
*command
= NULL
;
1262 command
= get_command_line (if_control
, arg
);
1264 if (command
== NULL
)
1267 execute_control_command (command
);
1268 free_command_lines (&command
);
1273 arg_cleanup (void *ignore
)
1275 struct user_args
*oargs
= user_args
;
1277 internal_error ("Internal error, arg_cleanup called with no user args.\n");
1279 user_args
= user_args
->next
;
1283 /* Bind the incomming arguments for a user defined command to
1284 $arg0, $arg1 ... $argMAXUSERARGS. */
1286 static struct cleanup
*
1290 struct user_args
*args
;
1291 struct cleanup
*old_chain
;
1292 unsigned int arg_count
= 0;
1294 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
1295 memset (args
, 0, sizeof (struct user_args
));
1297 args
->next
= user_args
;
1300 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
1312 if (arg_count
>= MAXUSERARGS
)
1314 error ("user defined function may only have %d arguments.\n",
1319 /* Strip whitespace. */
1320 while (*p
== ' ' || *p
== '\t')
1323 /* P now points to an argument. */
1325 user_args
->a
[arg_count
].arg
= p
;
1327 /* Get to the end of this argument. */
1330 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
1336 else if (*p
== '\\')
1359 user_args
->a
[arg_count
].len
= p
- start_arg
;
1366 /* Given character string P, return a point to the first argument ($arg),
1367 or NULL if P contains no arguments. */
1373 while ((p
= strchr (p
, '$')))
1375 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1382 /* Insert the user defined arguments stored in user_arg into the $arg
1383 arguments found in line, with the updated copy being placed into nline. */
1389 char *p
, *save_line
, *new_line
;
1392 /* First we need to know how much memory to allocate for the new line. */
1395 while ((p
= locate_arg (line
)))
1400 if (i
>= user_args
->count
)
1402 error ("Missing argument %d in user function.\n", i
);
1405 len
+= user_args
->a
[i
].len
;
1409 /* Don't forget the tail. */
1410 len
+= strlen (line
);
1412 /* Allocate space for the new line and fill it in. */
1413 new_line
= (char *) xmalloc (len
+ 1);
1414 if (new_line
== NULL
)
1417 /* Restore pointer to beginning of old line. */
1420 /* Save pointer to beginning of new line. */
1421 save_line
= new_line
;
1423 while ((p
= locate_arg (line
)))
1427 memcpy (new_line
, line
, p
- line
);
1428 new_line
+= p
- line
;
1431 len
= user_args
->a
[i
].len
;
1434 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1439 /* Don't forget the tail. */
1440 strcpy (new_line
, line
);
1442 /* Return a pointer to the beginning of the new line. */
1447 execute_user_command (c
, args
)
1448 struct cmd_list_element
*c
;
1451 register struct command_line
*cmdlines
;
1452 struct cleanup
*old_chain
;
1453 enum command_control_type ret
;
1455 old_chain
= setup_user_args (args
);
1457 cmdlines
= c
->user_commands
;
1462 /* Set the instream to 0, indicating execution of a
1463 user-defined function. */
1464 old_chain
= make_cleanup (do_restore_instream_cleanup
, instream
);
1465 instream
= (FILE *) 0;
1468 ret
= execute_control_command (cmdlines
);
1469 if (ret
!= simple_control
&& ret
!= break_control
)
1471 warning ("Error in control structure.\n");
1474 cmdlines
= cmdlines
->next
;
1476 do_cleanups (old_chain
);
1479 /* Execute the line P as a command.
1480 Pass FROM_TTY as second argument to the defining function. */
1483 execute_command (p
, from_tty
)
1487 register struct cmd_list_element
*c
;
1488 register enum language flang
;
1489 static int warned
= 0;
1491 /* FIXME: These should really be in an appropriate header file */
1492 extern void serial_log_command (const char *);
1496 /* Force cleanup of any alloca areas if using C alloca instead of
1497 a builtin alloca. */
1500 /* This can happen when command_line_input hits end of file. */
1504 serial_log_command (p
);
1506 while (*p
== ' ' || *p
== '\t')
1513 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1515 /* If the target is running, we allow only a limited set of
1517 if (event_loop_p
&& target_can_async_p () && target_executing
)
1518 if (!strcmp (c
->name
, "help")
1519 && !strcmp (c
->name
, "pwd")
1520 && !strcmp (c
->name
, "show")
1521 && !strcmp (c
->name
, "stop"))
1522 error ("Cannot execute this command while the target is running.");
1524 /* Pass null arg rather than an empty one. */
1527 /* Clear off trailing whitespace, except for set and complete command. */
1528 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1530 p
= arg
+ strlen (arg
) - 1;
1531 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1536 /* If this command has been hooked, run the hook first. */
1538 execute_user_command (c
->hook
, (char *) 0);
1540 if (c
->flags
& DEPRECATED_WARN_USER
)
1541 deprecated_cmd_warning (&line
);
1543 if (c
->class == class_user
)
1544 execute_user_command (c
, arg
);
1545 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1546 do_setshow_command (arg
, from_tty
& caution
, c
);
1547 else if (c
->function
.cfunc
== NO_FUNCTION
)
1548 error ("That is not a command, just a help topic.");
1549 else if (call_command_hook
)
1550 call_command_hook (c
, arg
, from_tty
& caution
);
1552 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1555 /* Tell the user if the language has changed (except first time). */
1556 if (current_language
!= expected_language
)
1558 if (language_mode
== language_mode_auto
)
1560 language_info (1); /* Print what changed. */
1565 /* Warn the user if the working language does not match the
1566 language of the current frame. Only warn the user if we are
1567 actually running the program, i.e. there is a stack. */
1568 /* FIXME: This should be cacheing the frame and only running when
1569 the frame changes. */
1571 if (target_has_stack
)
1573 flang
= get_frame_language ();
1575 && flang
!= language_unknown
1576 && flang
!= current_language
->la_language
)
1578 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1585 /* NOTE 1999-04-29: This function will be static again, once we modify
1586 gdb to use the event loop as the default command loop and we merge
1587 event-top.c into this file, top.c */
1589 command_loop_marker (void *foo
)
1593 /* Read commands from `instream' and execute them
1594 until end of file or error reading instream. */
1599 struct cleanup
*old_chain
;
1601 int stdin_is_tty
= ISATTY (stdin
);
1602 long time_at_cmd_start
;
1604 long space_at_cmd_start
= 0;
1606 extern int display_time
;
1607 extern int display_space
;
1609 while (instream
&& !feof (instream
))
1612 extern int insert_mode
;
1614 if (window_hook
&& instream
== stdin
)
1615 (*window_hook
) (instream
, get_prompt ());
1618 if (instream
== stdin
&& stdin_is_tty
)
1619 reinitialize_more_filter ();
1620 old_chain
= make_cleanup (command_loop_marker
, 0);
1623 /* A bit of paranoia: I want to make sure the "insert_mode" global
1624 * is clear except when it is being used for command-line editing
1625 * (see tuiIO.c, utils.c); otherwise normal output will
1626 * get messed up in the TUI. So clear it before/after
1627 * the command-line-input call. - RT
1631 /* Get a command-line. This calls the readline package. */
1632 command
= command_line_input (instream
== stdin
?
1633 get_prompt () : (char *) NULL
,
1634 instream
== stdin
, "prompt");
1641 time_at_cmd_start
= get_run_time ();
1646 extern char **environ
;
1647 char *lim
= (char *) sbrk (0);
1649 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1653 execute_command (command
, instream
== stdin
);
1654 /* Do any commands attached to breakpoint we stopped at. */
1655 bpstat_do_actions (&stop_bpstat
);
1656 do_cleanups (old_chain
);
1660 long cmd_time
= get_run_time () - time_at_cmd_start
;
1662 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1663 cmd_time
/ 1000000, cmd_time
% 1000000);
1669 extern char **environ
;
1670 char *lim
= (char *) sbrk (0);
1671 long space_now
= lim
- (char *) &environ
;
1672 long space_diff
= space_now
- space_at_cmd_start
;
1674 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1676 (space_diff
>= 0 ? '+' : '-'),
1683 /* Read commands from `instream' and execute them until end of file or
1684 error reading instream. This command loop doesnt care about any
1685 such things as displaying time and space usage. If the user asks
1686 for those, they won't work. */
1688 simplified_command_loop (read_input_func
, execute_command_func
)
1689 char *(*read_input_func
) (char *);
1690 void (*execute_command_func
) (char *, int);
1692 struct cleanup
*old_chain
;
1694 int stdin_is_tty
= ISATTY (stdin
);
1696 while (instream
&& !feof (instream
))
1699 if (instream
== stdin
&& stdin_is_tty
)
1700 reinitialize_more_filter ();
1701 old_chain
= make_cleanup (command_loop_marker
, 0);
1703 /* Get a command-line. */
1704 command
= (*read_input_func
) (instream
== stdin
?
1705 get_prompt () : (char *) NULL
);
1710 (*execute_command_func
) (command
, instream
== stdin
);
1712 /* Do any commands attached to breakpoint we stopped at. */
1713 bpstat_do_actions (&stop_bpstat
);
1715 do_cleanups (old_chain
);
1719 /* Commands call this if they do not want to be repeated by null lines. */
1727 /* If we aren't reading from standard input, we are saving the last
1728 thing read from stdin in line and don't want to delete it. Null lines
1729 won't repeat here in any case. */
1730 if (instream
== stdin
)
1734 /* Read a line from the stream "instream" without command line editing.
1736 It prints PROMPT_ARG once at the start.
1737 Action is compatible with "readline", e.g. space for the result is
1738 malloc'd and should be freed by the caller.
1740 A NULL return means end of file. */
1742 gdb_readline (prompt_arg
)
1747 int input_index
= 0;
1748 int result_size
= 80;
1752 /* Don't use a _filtered function here. It causes the assumed
1753 character position to be off, since the newline we read from
1754 the user is not accounted for. */
1755 fputs_unfiltered (prompt_arg
, gdb_stdout
);
1757 /* Move to a new line so the entered line doesn't have a prompt
1758 on the front of it. */
1759 fputs_unfiltered ("\n", gdb_stdout
);
1761 gdb_flush (gdb_stdout
);
1764 result
= (char *) xmalloc (result_size
);
1768 /* Read from stdin if we are executing a user defined command.
1769 This is the right thing for prompt_for_continue, at least. */
1770 c
= fgetc (instream
? instream
: stdin
);
1774 if (input_index
> 0)
1775 /* The last line does not end with a newline. Return it, and
1776 if we are called again fgetc will still return EOF and
1777 we'll return NULL then. */
1784 #ifndef CRLF_SOURCE_FILES
1788 if (input_index
> 0 && result
[input_index
- 1] == '\r')
1794 result
[input_index
++] = c
;
1795 while (input_index
>= result_size
)
1798 result
= (char *) xrealloc (result
, result_size
);
1802 result
[input_index
++] = '\0';
1806 /* Variables which control command line editing and history
1807 substitution. These variables are given default values at the end
1809 static int command_editing_p
;
1810 /* NOTE 1999-04-29: This variable will be static again, once we modify
1811 gdb to use the event loop as the default command loop and we merge
1812 event-top.c into this file, top.c */
1813 /* static */ int history_expansion_p
;
1814 static int write_history_p
;
1815 static int history_size
;
1816 static char *history_filename
;
1818 /* readline uses the word breaks for two things:
1819 (1) In figuring out where to point the TEXT parameter to the
1820 rl_completion_entry_function. Since we don't use TEXT for much,
1821 it doesn't matter a lot what the word breaks are for this purpose, but
1822 it does affect how much stuff M-? lists.
1823 (2) If one of the matches contains a word break character, readline
1824 will quote it. That's why we switch between
1825 gdb_completer_word_break_characters and
1826 gdb_completer_command_word_break_characters. I'm not sure when
1827 we need this behavior (perhaps for funky characters in C++ symbols?). */
1829 /* Variables which are necessary for fancy command line editing. */
1830 char *gdb_completer_word_break_characters
=
1831 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1833 /* When completing on command names, we remove '-' from the list of
1834 word break characters, since we use it in command names. If the
1835 readline library sees one in any of the current completion strings,
1836 it thinks that the string needs to be quoted and automatically supplies
1838 char *gdb_completer_command_word_break_characters
=
1839 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1841 /* When completing on file names, we remove from the list of word
1842 break characters any characters that are commonly used in file
1843 names, such as '-', '+', '~', etc. Otherwise, readline displays
1844 incorrect completion candidates. */
1845 char *gdb_completer_file_name_break_characters
= " \t\n*|\"';:?/><";
1847 /* Characters that can be used to quote completion strings. Note that we
1848 can't include '"' because the gdb C parser treats such quoted sequences
1850 char *gdb_completer_quote_characters
=
1853 /* Functions that are used as part of the fancy command line editing. */
1855 /* This can be used for functions which don't want to complete on symbols
1856 but don't want to complete on anything else either. */
1859 noop_completer (text
, prefix
)
1866 /* Complete on filenames. */
1868 filename_completer (text
, word
)
1872 /* From readline. */
1873 extern char *filename_completion_function (char *, int);
1874 int subsequent_name
;
1876 int return_val_used
;
1877 int return_val_alloced
;
1879 return_val_used
= 0;
1880 /* Small for testing. */
1881 return_val_alloced
= 1;
1882 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1884 subsequent_name
= 0;
1888 p
= filename_completion_function (text
, subsequent_name
);
1889 if (return_val_used
>= return_val_alloced
)
1891 return_val_alloced
*= 2;
1893 (char **) xrealloc (return_val
,
1894 return_val_alloced
* sizeof (char *));
1898 return_val
[return_val_used
++] = p
;
1901 /* We need to set subsequent_name to a non-zero value before the
1902 continue line below, because otherwise, if the first file seen
1903 by GDB is a backup file whose name ends in a `~', we will loop
1905 subsequent_name
= 1;
1906 /* Like emacs, don't complete on old versions. Especially useful
1907 in the "source" command. */
1908 if (p
[strlen (p
) - 1] == '~')
1914 /* Return exactly p. */
1915 return_val
[return_val_used
++] = p
;
1916 else if (word
> text
)
1918 /* Return some portion of p. */
1919 q
= xmalloc (strlen (p
) + 5);
1920 strcpy (q
, p
+ (word
- text
));
1921 return_val
[return_val_used
++] = q
;
1926 /* Return some of TEXT plus p. */
1927 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1928 strncpy (q
, word
, text
- word
);
1929 q
[text
- word
] = '\0';
1931 return_val
[return_val_used
++] = q
;
1937 /* There is no way to do this just long enough to affect quote inserting
1938 without also affecting the next completion. This should be fixed in
1940 /* Insure that readline does the right thing
1941 with respect to inserting quotes. */
1942 rl_completer_word_break_characters
= "";
1947 /* Here are some useful test cases for completion. FIXME: These should
1948 be put in the test suite. They should be tested with both M-? and TAB.
1950 "show output-" "radix"
1951 "show output" "-radix"
1952 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1953 "p " ambiguous (all symbols)
1954 "info t foo" no completions
1955 "info t " no completions
1956 "info t" ambiguous ("info target", "info terminal", etc.)
1957 "info ajksdlfk" no completions
1958 "info ajksdlfk " no completions
1960 "info " ambiguous (all info commands)
1961 "p \"a" no completions (string constant)
1962 "p 'a" ambiguous (all symbols starting with a)
1963 "p b-a" ambiguous (all symbols starting with a)
1964 "p b-" ambiguous (all symbols)
1965 "file Make" "file" (word break hard to screw up here)
1966 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1969 /* Generate completions one by one for the completer. Each time we are
1970 called return another potential completion to the caller.
1971 line_completion just completes on commands or passes the buck to the
1972 command's completer function, the stuff specific to symbol completion
1973 is in make_symbol_completion_list.
1975 TEXT is the caller's idea of the "word" we are looking at.
1977 MATCHES is the number of matches that have currently been collected from
1978 calling this completion function. When zero, then we need to initialize,
1979 otherwise the initialization has already taken place and we can just
1980 return the next potential completion string.
1982 LINE_BUFFER is available to be looked at; it contains the entire text
1983 of the line. POINT is the offset in that line of the cursor. You
1984 should pretend that the line ends at POINT.
1986 Returns NULL if there are no more completions, else a pointer to a string
1987 which is a possible completion, it is the caller's responsibility to
1991 line_completion_function (text
, matches
, line_buffer
, point
)
1997 static char **list
= (char **) NULL
; /* Cache of completions */
1998 static int index
; /* Next cached completion */
1999 char *output
= NULL
;
2000 char *tmp_command
, *p
;
2001 /* Pointer within tmp_command which corresponds to text. */
2003 struct cmd_list_element
*c
, *result_list
;
2007 /* The caller is beginning to accumulate a new set of completions, so
2008 we need to find all of them now, and cache them for returning one at
2009 a time on future calls. */
2013 /* Free the storage used by LIST, but not by the strings inside.
2014 This is because rl_complete_internal () frees the strings. */
2020 /* Choose the default set of word break characters to break completions.
2021 If we later find out that we are doing completions on command strings
2022 (as opposed to strings supplied by the individual command completer
2023 functions, which can be any string) then we will switch to the
2024 special word break set for command strings, which leaves out the
2025 '-' character used in some commands. */
2027 rl_completer_word_break_characters
=
2028 gdb_completer_word_break_characters
;
2030 /* Decide whether to complete on a list of gdb commands or on symbols. */
2031 tmp_command
= (char *) alloca (point
+ 1);
2034 strncpy (tmp_command
, line_buffer
, point
);
2035 tmp_command
[point
] = '\0';
2036 /* Since text always contains some number of characters leading up
2037 to point, we can find the equivalent position in tmp_command
2038 by subtracting that many characters from the end of tmp_command. */
2039 word
= tmp_command
+ point
- strlen (text
);
2043 /* An empty line we want to consider ambiguous; that is, it
2044 could be any command. */
2045 c
= (struct cmd_list_element
*) -1;
2050 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
2053 /* Move p up to the next interesting thing. */
2054 while (*p
== ' ' || *p
== '\t')
2061 /* It is an unrecognized command. So there are no
2062 possible completions. */
2065 else if (c
== (struct cmd_list_element
*) -1)
2069 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
2070 doesn't advance over that thing itself. Do so now. */
2072 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
2074 if (q
!= tmp_command
+ point
)
2076 /* There is something beyond the ambiguous
2077 command, so there are no possible completions. For
2078 example, "info t " or "info t foo" does not complete
2079 to anything, because "info t" can be "info target" or
2085 /* We're trying to complete on the command which was ambiguous.
2086 This we can deal with. */
2089 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
2094 list
= complete_on_cmdlist (cmdlist
, p
, word
);
2096 /* Insure that readline does the right thing with respect to
2097 inserting quotes. */
2098 rl_completer_word_break_characters
=
2099 gdb_completer_command_word_break_characters
;
2104 /* We've recognized a full command. */
2106 if (p
== tmp_command
+ point
)
2108 /* There is no non-whitespace in the line beyond the command. */
2110 if (p
[-1] == ' ' || p
[-1] == '\t')
2112 /* The command is followed by whitespace; we need to complete
2113 on whatever comes after command. */
2116 /* It is a prefix command; what comes after it is
2117 a subcommand (e.g. "info "). */
2118 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
2120 /* Insure that readline does the right thing
2121 with respect to inserting quotes. */
2122 rl_completer_word_break_characters
=
2123 gdb_completer_command_word_break_characters
;
2127 list
= complete_on_enum (c
->enums
, p
, word
);
2128 rl_completer_word_break_characters
=
2129 gdb_completer_command_word_break_characters
;
2133 /* It is a normal command; what comes after it is
2134 completed by the command's completer function. */
2135 list
= (*c
->completer
) (p
, word
);
2136 if (c
->completer
== filename_completer
)
2137 rl_completer_word_break_characters
=
2138 gdb_completer_file_name_break_characters
;
2143 /* The command is not followed by whitespace; we need to
2144 complete on the command itself. e.g. "p" which is a
2145 command itself but also can complete to "print", "ptype"
2149 /* Find the command we are completing on. */
2151 while (q
> tmp_command
)
2153 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
2159 list
= complete_on_cmdlist (result_list
, q
, word
);
2161 /* Insure that readline does the right thing
2162 with respect to inserting quotes. */
2163 rl_completer_word_break_characters
=
2164 gdb_completer_command_word_break_characters
;
2169 /* There is non-whitespace beyond the command. */
2171 if (c
->prefixlist
&& !c
->allow_unknown
)
2173 /* It is an unrecognized subcommand of a prefix command,
2174 e.g. "info adsfkdj". */
2179 list
= complete_on_enum (c
->enums
, p
, word
);
2183 /* It is a normal command. */
2184 list
= (*c
->completer
) (p
, word
);
2185 if (c
->completer
== filename_completer
)
2186 rl_completer_word_break_characters
=
2187 gdb_completer_file_name_break_characters
;
2193 /* If we found a list of potential completions during initialization then
2194 dole them out one at a time. The vector of completions is NULL
2195 terminated, so after returning the last one, return NULL (and continue
2196 to do so) each time we are called after that, until a new list is
2201 output
= list
[index
];
2209 /* Can't do this because readline hasn't yet checked the word breaks
2210 for figuring out whether to insert a quote. */
2212 /* Make sure the word break characters are set back to normal for the
2213 next time that readline tries to complete something. */
2214 rl_completer_word_break_characters
=
2215 gdb_completer_word_break_characters
;
2221 /* Line completion interface function for readline. */
2224 readline_line_completion_function (text
, matches
)
2228 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
2231 /* Skip over a possibly quoted word (as defined by the quote characters
2232 and word break characters the completer uses). Returns pointer to the
2233 location after the "word". */
2239 char quote_char
= '\0';
2242 for (scan
= str
; *scan
!= '\0'; scan
++)
2244 if (quote_char
!= '\0')
2246 /* Ignore everything until the matching close quote char */
2247 if (*scan
== quote_char
)
2249 /* Found matching close quote. */
2254 else if (strchr (gdb_completer_quote_characters
, *scan
))
2256 /* Found start of a quoted string. */
2259 else if (strchr (gdb_completer_word_break_characters
, *scan
))
2273 #if STOP_SIGNAL == SIGTSTP
2274 signal (SIGTSTP
, SIG_DFL
);
2276 kill (getpid (), SIGTSTP
);
2277 signal (SIGTSTP
, stop_sig
);
2279 signal (STOP_SIGNAL
, stop_sig
);
2281 printf_unfiltered ("%s", get_prompt ());
2282 gdb_flush (gdb_stdout
);
2284 /* Forget about any previous command -- null line now will do nothing. */
2287 #endif /* STOP_SIGNAL */
2289 /* Initialize signal handlers. */
2294 /* Under System V the default disposition of a signal is reinstated after
2295 the signal is caught and delivered to an application process. On such
2296 systems one must restore the replacement signal handler if one wishes
2297 to continue handling the signal in one's program. On BSD systems this
2298 is not needed but it is harmless, and it simplifies the code to just do
2299 it unconditionally. */
2300 signal (signo
, do_nothing
);
2306 signal (SIGINT
, request_quit
);
2308 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2309 to the inferior and breakpoints will be ignored. */
2311 signal (SIGTRAP
, SIG_DFL
);
2314 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2315 passed to the inferior, which we don't want. It would be
2316 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2317 on BSD4.3 systems using vfork, that can affect the
2318 GDB process as well as the inferior (the signal handling tables
2319 might be in memory, shared between the two). Since we establish
2320 a handler for SIGQUIT, when we call exec it will set the signal
2321 to SIG_DFL for us. */
2322 signal (SIGQUIT
, do_nothing
);
2324 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
2325 signal (SIGHUP
, disconnect
);
2327 signal (SIGFPE
, float_handler
);
2329 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2330 signal (SIGWINCH
, SIGWINCH_HANDLER
);
2334 /* Read one line from the command input stream `instream'
2335 into the local static buffer `linebuffer' (whose current length
2337 The buffer is made bigger as necessary.
2338 Returns the address of the start of the line.
2340 NULL is returned for end of file.
2342 *If* the instream == stdin & stdin is a terminal, the line read
2343 is copied into the file line saver (global var char *line,
2344 length linesize) so that it can be duplicated.
2346 This routine either uses fancy command line editing or
2347 simple input as the user has requested. */
2350 command_line_input (prompt_arg
, repeat
, annotation_suffix
)
2353 char *annotation_suffix
;
2355 static char *linebuffer
= 0;
2356 static unsigned linelength
= 0;
2360 char *local_prompt
= prompt_arg
;
2364 /* The annotation suffix must be non-NULL. */
2365 if (annotation_suffix
== NULL
)
2366 annotation_suffix
= "";
2368 if (annotation_level
> 1 && instream
== stdin
)
2370 local_prompt
= alloca ((prompt_arg
== NULL
? 0 : strlen (prompt_arg
))
2371 + strlen (annotation_suffix
) + 40);
2372 if (prompt_arg
== NULL
)
2373 local_prompt
[0] = '\0';
2375 strcpy (local_prompt
, prompt_arg
);
2376 strcat (local_prompt
, "\n\032\032");
2377 strcat (local_prompt
, annotation_suffix
);
2378 strcat (local_prompt
, "\n");
2381 if (linebuffer
== 0)
2384 linebuffer
= (char *) xmalloc (linelength
);
2389 /* Control-C quits instantly if typed while in this loop
2390 since it should not wait until the user types a newline. */
2396 signal (STOP_SIGNAL
, handle_stop_sig
);
2398 signal (STOP_SIGNAL
, stop_sig
);
2404 /* Make sure that all output has been output. Some machines may let
2405 you get away with leaving out some of the gdb_flush, but not all. */
2407 gdb_flush (gdb_stdout
);
2408 gdb_flush (gdb_stderr
);
2410 if (source_file_name
!= NULL
)
2412 ++source_line_number
;
2413 sprintf (source_error
,
2414 "%s%s:%d: Error in sourced command file:\n",
2417 source_line_number
);
2418 error_pre_print
= source_error
;
2421 if (annotation_level
> 1 && instream
== stdin
)
2423 printf_unfiltered ("\n\032\032pre-");
2424 printf_unfiltered (annotation_suffix
);
2425 printf_unfiltered ("\n");
2428 /* Don't use fancy stuff if not talking to stdin. */
2429 if (readline_hook
&& instream
== NULL
)
2431 rl
= (*readline_hook
) (local_prompt
);
2433 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
2435 rl
= readline (local_prompt
);
2439 rl
= gdb_readline (local_prompt
);
2442 if (annotation_level
> 1 && instream
== stdin
)
2444 printf_unfiltered ("\n\032\032post-");
2445 printf_unfiltered (annotation_suffix
);
2446 printf_unfiltered ("\n");
2449 if (!rl
|| rl
== (char *) EOF
)
2454 if (strlen (rl
) + 1 + (p
- linebuffer
) > linelength
)
2456 linelength
= strlen (rl
) + 1 + (p
- linebuffer
);
2457 nline
= (char *) xrealloc (linebuffer
, linelength
);
2458 p
+= nline
- linebuffer
;
2462 /* Copy line. Don't copy null at end. (Leaves line alone
2463 if this was just a newline) */
2467 free (rl
); /* Allocated in readline. */
2469 if (p
== linebuffer
|| *(p
- 1) != '\\')
2472 p
--; /* Put on top of '\'. */
2473 local_prompt
= (char *) 0;
2478 signal (STOP_SIGNAL
, SIG_DFL
);
2485 #define SERVER_COMMAND_LENGTH 7
2487 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2488 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2491 /* Note that we don't set `line'. Between this and the check in
2492 dont_repeat, this insures that repeating will still do the
2495 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2498 /* Do history expansion if that is wished. */
2499 if (history_expansion_p
&& instream
== stdin
2500 && ISATTY (instream
))
2502 char *history_value
;
2505 *p
= '\0'; /* Insert null now. */
2506 expanded
= history_expand (linebuffer
, &history_value
);
2509 /* Print the changes. */
2510 printf_unfiltered ("%s\n", history_value
);
2512 /* If there was an error, call this function again. */
2515 free (history_value
);
2516 return command_line_input (prompt_arg
, repeat
, annotation_suffix
);
2518 if (strlen (history_value
) > linelength
)
2520 linelength
= strlen (history_value
) + 1;
2521 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2523 strcpy (linebuffer
, history_value
);
2524 p
= linebuffer
+ strlen (linebuffer
);
2525 free (history_value
);
2529 /* If we just got an empty line, and that is supposed
2530 to repeat the previous command, return the value in the
2532 if (repeat
&& p
== linebuffer
)
2534 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++);
2540 /* Add line to history if appropriate. */
2541 if (instream
== stdin
2542 && ISATTY (stdin
) && *linebuffer
)
2543 add_history (linebuffer
);
2545 /* Note: lines consisting solely of comments are added to the command
2546 history. This is useful when you type a command, and then
2547 realize you don't want to execute it quite yet. You can comment
2548 out the command and then later fetch it from the value history
2549 and remove the '#'. The kill ring is probably better, but some
2550 people are in the habit of commenting things out. */
2552 *p1
= '\0'; /* Found a comment. */
2554 /* Save into global buffer if appropriate. */
2557 if (linelength
> linesize
)
2559 line
= xrealloc (line
, linelength
);
2560 linesize
= linelength
;
2562 strcpy (line
, linebuffer
);
2570 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2571 code bodies. This is typically used when we encounter an "else"
2572 clause for an "if" command. */
2575 realloc_body_list (command
, new_length
)
2576 struct command_line
*command
;
2580 struct command_line
**body_list
;
2582 n
= command
->body_count
;
2584 /* Nothing to do? */
2585 if (new_length
<= n
)
2588 body_list
= (struct command_line
**)
2589 xmalloc (sizeof (struct command_line
*) * new_length
);
2591 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2593 free (command
->body_list
);
2594 command
->body_list
= body_list
;
2595 command
->body_count
= new_length
;
2598 /* Read one line from the input stream. If the command is an "else" or
2599 "end", return such an indication to the caller. */
2601 static enum misc_command_type
2602 read_next_line (command
)
2603 struct command_line
**command
;
2605 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2608 if (control_level
>= 254)
2609 error ("Control nesting too deep!\n");
2611 /* Set a prompt based on the nesting of the control commands. */
2612 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2614 for (i
= 0; i
< control_level
; i
++)
2615 control_prompt
[i
] = ' ';
2616 control_prompt
[i
] = '>';
2617 control_prompt
[i
+ 1] = '\0';
2618 prompt_ptr
= (char *) &control_prompt
[0];
2623 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2625 /* Not sure what to do here. */
2629 /* Strip leading and trailing whitespace. */
2630 while (*p
== ' ' || *p
== '\t')
2633 p1
= p
+ strlen (p
);
2634 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2637 /* Blanks and comments don't really do anything, but we need to
2638 distinguish them from else, end and other commands which can be
2640 if (p1
== p
|| p
[0] == '#')
2643 /* Is this the end of a simple, while, or if control structure? */
2644 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2647 /* Is the else clause of an if control structure? */
2648 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2649 return else_command
;
2651 /* Check for while, if, break, continue, etc and build a new command
2652 line structure for them. */
2653 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2654 *command
= build_command_line (while_control
, p
+ 6);
2655 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2656 *command
= build_command_line (if_control
, p
+ 3);
2657 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2659 *command
= (struct command_line
*)
2660 xmalloc (sizeof (struct command_line
));
2661 (*command
)->next
= NULL
;
2662 (*command
)->line
= NULL
;
2663 (*command
)->control_type
= break_control
;
2664 (*command
)->body_count
= 0;
2665 (*command
)->body_list
= NULL
;
2667 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2669 *command
= (struct command_line
*)
2670 xmalloc (sizeof (struct command_line
));
2671 (*command
)->next
= NULL
;
2672 (*command
)->line
= NULL
;
2673 (*command
)->control_type
= continue_control
;
2674 (*command
)->body_count
= 0;
2675 (*command
)->body_list
= NULL
;
2679 /* A normal command. */
2680 *command
= (struct command_line
*)
2681 xmalloc (sizeof (struct command_line
));
2682 (*command
)->next
= NULL
;
2683 (*command
)->line
= savestring (p
, p1
- p
);
2684 (*command
)->control_type
= simple_control
;
2685 (*command
)->body_count
= 0;
2686 (*command
)->body_list
= NULL
;
2689 /* Nothing special. */
2693 /* Recursively read in the control structures and create a command_line
2694 structure from them.
2696 The parent_control parameter is the control structure in which the
2697 following commands are nested. */
2699 static enum command_control_type
2700 recurse_read_control_structure (current_cmd
)
2701 struct command_line
*current_cmd
;
2703 int current_body
, i
;
2704 enum misc_command_type val
;
2705 enum command_control_type ret
;
2706 struct command_line
**body_ptr
, *child_tail
, *next
;
2711 /* Sanity checks. */
2712 if (current_cmd
->control_type
== simple_control
)
2714 error ("Recursed on a simple control type\n");
2715 return invalid_control
;
2718 if (current_body
> current_cmd
->body_count
)
2720 error ("Allocated body is smaller than this command type needs\n");
2721 return invalid_control
;
2724 /* Read lines from the input stream and build control structures. */
2730 val
= read_next_line (&next
);
2732 /* Just skip blanks and comments. */
2733 if (val
== nop_command
)
2736 if (val
== end_command
)
2738 if (current_cmd
->control_type
== while_control
2739 || current_cmd
->control_type
== if_control
)
2741 /* Success reading an entire control structure. */
2742 ret
= simple_control
;
2747 ret
= invalid_control
;
2752 /* Not the end of a control structure. */
2753 if (val
== else_command
)
2755 if (current_cmd
->control_type
== if_control
2756 && current_body
== 1)
2758 realloc_body_list (current_cmd
, 2);
2765 ret
= invalid_control
;
2772 child_tail
->next
= next
;
2776 body_ptr
= current_cmd
->body_list
;
2777 for (i
= 1; i
< current_body
; i
++)
2786 /* If the latest line is another control structure, then recurse
2788 if (next
->control_type
== while_control
2789 || next
->control_type
== if_control
)
2792 ret
= recurse_read_control_structure (next
);
2795 if (ret
!= simple_control
)
2805 /* Read lines from the input stream and accumulate them in a chain of
2806 struct command_line's, which is then returned. For input from a
2807 terminal, the special command "end" is used to mark the end of the
2808 input, and is not included in the returned chain of commands. */
2810 #define END_MESSAGE "End with a line saying just \"end\"."
2812 struct command_line
*
2813 read_command_lines (prompt_arg
, from_tty
)
2817 struct command_line
*head
, *tail
, *next
;
2818 struct cleanup
*old_chain
;
2819 enum command_control_type ret
;
2820 enum misc_command_type val
;
2823 if (readline_begin_hook
)
2825 /* Note - intentional to merge messages with no newline */
2826 (*readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
2828 else if (from_tty
&& input_from_terminal_p ())
2830 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
2831 gdb_flush (gdb_stdout
);
2839 val
= read_next_line (&next
);
2841 /* Ignore blank lines or comments. */
2842 if (val
== nop_command
)
2845 if (val
== end_command
)
2847 ret
= simple_control
;
2851 if (val
!= ok_command
)
2853 ret
= invalid_control
;
2857 if (next
->control_type
== while_control
2858 || next
->control_type
== if_control
)
2861 ret
= recurse_read_control_structure (next
);
2864 if (ret
== invalid_control
)
2875 old_chain
= make_cleanup_free_command_lines (&head
);
2884 if (ret
!= invalid_control
)
2886 discard_cleanups (old_chain
);
2889 do_cleanups (old_chain
);
2892 if (readline_end_hook
)
2894 (*readline_end_hook
) ();
2899 /* Free a chain of struct command_line's. */
2902 free_command_lines (lptr
)
2903 struct command_line
**lptr
;
2905 register struct command_line
*l
= *lptr
;
2906 register struct command_line
*next
;
2907 struct command_line
**blist
;
2912 if (l
->body_count
> 0)
2914 blist
= l
->body_list
;
2915 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2916 free_command_lines (blist
);
2926 do_free_command_lines_cleanup (void *arg
)
2928 free_command_lines (arg
);
2931 static struct cleanup
*
2932 make_cleanup_free_command_lines (struct command_line
**arg
)
2934 return make_cleanup (do_free_command_lines_cleanup
, arg
);
2937 /* Add an element to the list of info subcommands. */
2939 struct cmd_list_element
*
2940 add_info (name
, fun
, doc
)
2942 void (*fun
) (char *, int);
2945 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2948 /* Add an alias to the list of info subcommands. */
2950 struct cmd_list_element
*
2951 add_info_alias (name
, oldname
, abbrev_flag
)
2956 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2959 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2960 Therefore, its own definition is called only for "info" with no args. */
2964 info_command (arg
, from_tty
)
2968 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2969 help_list (infolist
, "info ", -1, gdb_stdout
);
2972 /* The "complete" command is used by Emacs to implement completion. */
2976 complete_command (arg
, from_tty
)
2988 argpoint
= strlen (arg
);
2990 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2992 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2994 printf_unfiltered ("%s\n", completion
);
2999 /* The "show" command with no arguments shows all the settings. */
3003 show_command (arg
, from_tty
)
3007 cmd_show_list (showlist
, from_tty
, "");
3010 /* Add an element to the list of commands. */
3012 struct cmd_list_element
*
3013 add_com (name
, class, fun
, doc
)
3015 enum command_class
class;
3016 void (*fun
) (char *, int);
3019 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
3022 /* Add an alias or abbreviation command to the list of commands. */
3024 struct cmd_list_element
*
3025 add_com_alias (name
, oldname
, class, abbrev_flag
)
3028 enum command_class
class;
3031 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
3038 error ("Argument required (%s).", why
);
3043 help_command (command
, from_tty
)
3045 int from_tty
; /* Ignored */
3047 help_cmd (command
, gdb_stdout
);
3051 validate_comname (comname
)
3057 error_no_arg ("name of command to define");
3062 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
3063 error ("Junk in argument list: \"%s\"", p
);
3068 /* This is just a placeholder in the command data structures. */
3070 user_defined_command (ignore
, from_tty
)
3077 define_command (comname
, from_tty
)
3081 register struct command_line
*cmds
;
3082 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
3083 char *tem
= comname
;
3085 #define HOOK_STRING "hook-"
3088 validate_comname (comname
);
3090 /* Look it up, and verify that we got an exact match. */
3091 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
3092 if (c
&& !STREQ (comname
, c
->name
))
3097 if (c
->class == class_user
|| c
->class == class_alias
)
3098 tem
= "Redefine command \"%s\"? ";
3100 tem
= "Really redefine built-in command \"%s\"? ";
3101 if (!query (tem
, c
->name
))
3102 error ("Command \"%s\" not redefined.", c
->name
);
3105 /* If this new command is a hook, then mark the command which it
3106 is hooking. Note that we allow hooking `help' commands, so that
3107 we can hook the `stop' pseudo-command. */
3109 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
3111 /* Look up cmd it hooks, and verify that we got an exact match. */
3112 tem
= comname
+ HOOK_LEN
;
3113 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
3114 if (hookc
&& !STREQ (comname
+ HOOK_LEN
, hookc
->name
))
3118 warning ("Your new `%s' command does not hook any existing command.",
3120 if (!query ("Proceed? "))
3121 error ("Not confirmed.");
3125 comname
= savestring (comname
, strlen (comname
));
3127 /* If the rest of the commands will be case insensitive, this one
3128 should behave in the same manner. */
3129 for (tem
= comname
; *tem
; tem
++)
3131 *tem
= tolower (*tem
);
3133 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
3134 cmds
= read_command_lines (tmpbuf
, from_tty
);
3136 if (c
&& c
->class == class_user
)
3137 free_command_lines (&c
->user_commands
);
3139 newc
= add_cmd (comname
, class_user
, user_defined_command
,
3140 (c
&& c
->class == class_user
)
3141 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
3142 newc
->user_commands
= cmds
;
3144 /* If this new command is a hook, then mark both commands as being
3148 hookc
->hook
= newc
; /* Target gets hooked. */
3149 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
3154 document_command (comname
, from_tty
)
3158 struct command_line
*doclines
;
3159 register struct cmd_list_element
*c
;
3160 char *tem
= comname
;
3163 validate_comname (comname
);
3165 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
3167 if (c
->class != class_user
)
3168 error ("Command \"%s\" is built-in.", comname
);
3170 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
3171 doclines
= read_command_lines (tmpbuf
, from_tty
);
3177 register struct command_line
*cl1
;
3178 register int len
= 0;
3180 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
3181 len
+= strlen (cl1
->line
) + 1;
3183 c
->doc
= (char *) xmalloc (len
+ 1);
3186 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
3188 strcat (c
->doc
, cl1
->line
);
3190 strcat (c
->doc
, "\n");
3194 free_command_lines (&doclines
);
3197 /* Print the GDB banner. */
3199 print_gdb_version (stream
)
3200 struct ui_file
*stream
;
3202 /* From GNU coding standards, first line is meant to be easy for a
3203 program to parse, and is just canonical program name and version
3204 number, which starts after last space. */
3207 /* Print it console style until a format is defined */
3208 fprintf_filtered (stream
, "GNU gdb %s (UI_OUT)\n", version
);
3210 fprintf_filtered (stream
, "GNU gdb %s\n", version
);
3213 /* Second line is a copyright notice. */
3215 fprintf_filtered (stream
, "Copyright 2000 Free Software Foundation, Inc.\n");
3217 /* Following the copyright is a brief statement that the program is
3218 free software, that users are free to copy and change it on
3219 certain conditions, that it is covered by the GNU GPL, and that
3220 there is no warranty. */
3222 fprintf_filtered (stream
, "\
3223 GDB is free software, covered by the GNU General Public License, and you are\n\
3224 welcome to change it and/or distribute copies of it under certain conditions.\n\
3225 Type \"show copying\" to see the conditions.\n\
3226 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
3228 /* After the required info we print the configuration information. */
3230 fprintf_filtered (stream
, "This GDB was configured as \"");
3231 if (!STREQ (host_name
, target_name
))
3233 fprintf_filtered (stream
, "--host=%s --target=%s", host_name
, target_name
);
3237 fprintf_filtered (stream
, "%s", host_name
);
3239 fprintf_filtered (stream
, "\".");
3244 show_version (args
, from_tty
)
3249 print_gdb_version (gdb_stdout
);
3250 printf_filtered ("\n");
3254 /* get_prompt: access method for the GDB prompt string. */
3256 #define MAX_PROMPT_SIZE 256
3259 * int get_prompt_1 (char * buf);
3261 * Work-horse for get_prompt (called via catch_errors).
3262 * Argument is buffer to hold the formatted prompt.
3264 * Returns: 1 for success (use formatted prompt)
3265 * 0 for failure (use gdb_prompt_string).
3268 static int gdb_prompt_escape
;
3271 get_prompt_1 (formatted_prompt
)
3272 char *formatted_prompt
;
3277 local_prompt
= PROMPT (0);
3279 local_prompt
= gdb_prompt_string
;
3282 if (gdb_prompt_escape
== 0)
3284 return 0; /* do no formatting */
3287 /* formatted prompt */
3289 char fmt
[40], *promptp
, *outp
, *tmp
;
3296 struct type
*arg_type
, *elt_type
;
3298 promptp
= local_prompt
;
3299 outp
= formatted_prompt
;
3301 while (*promptp
!= '\0')
3303 int available
= MAX_PROMPT_SIZE
- (outp
- formatted_prompt
) - 1;
3305 if (*promptp
!= gdb_prompt_escape
)
3307 if (available
>= 1) /* overflow protect */
3308 *outp
++ = *promptp
++;
3312 /* GDB prompt string contains escape char. Parse for arg.
3313 Two consecutive escape chars followed by arg followed by
3314 a comma means to insert the arg using a default format.
3315 Otherwise a printf format string may be included between
3316 the two escape chars. eg:
3317 %%foo, insert foo using default format
3318 %2.2f%foo, insert foo using "%2.2f" format
3319 A mismatch between the format string and the data type
3320 of "foo" is an error (which we don't know how to protect
3323 fmt
[0] = '\0'; /* assume null format string */
3324 if (promptp
[1] == gdb_prompt_escape
) /* double esc char */
3326 promptp
+= 2; /* skip past two escape chars. */
3330 /* extract format string from between two esc chars */
3334 fmt
[i
++] = *promptp
++; /* copy format string */
3336 while (i
< sizeof (fmt
) - 1 &&
3337 *promptp
!= gdb_prompt_escape
&&
3340 if (*promptp
!= gdb_prompt_escape
)
3341 error ("Syntax error at prompt position %d",
3342 promptp
- local_prompt
);
3345 promptp
++; /* skip second escape char */
3346 fmt
[i
++] = '\0'; /* terminate the format string */
3350 arg_val
= parse_to_comma_and_eval (&promptp
);
3351 if (*promptp
== ',')
3352 promptp
++; /* skip past the comma */
3353 arg_type
= check_typedef (VALUE_TYPE (arg_val
));
3354 switch (TYPE_CODE (arg_type
))
3356 case TYPE_CODE_ARRAY
:
3357 elt_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
3358 if (TYPE_LENGTH (arg_type
) > 0 &&
3359 TYPE_LENGTH (elt_type
) == 1 &&
3360 TYPE_CODE (elt_type
) == TYPE_CODE_INT
)
3362 int len
= TYPE_LENGTH (arg_type
);
3364 if (VALUE_LAZY (arg_val
))
3365 value_fetch_lazy (arg_val
);
3366 tmp
= VALUE_CONTENTS (arg_val
);
3368 if (len
> available
)
3369 len
= available
; /* overflow protect */
3371 /* FIXME: how to protect GDB from crashing
3372 from bad user-supplied format string? */
3374 sprintf (outp
, fmt
, tmp
);
3376 strncpy (outp
, tmp
, len
);
3381 elt_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
3382 addrval
= value_as_pointer (arg_val
);
3384 if (TYPE_LENGTH (elt_type
) == 1 &&
3385 TYPE_CODE (elt_type
) == TYPE_CODE_INT
&&
3388 /* display it as a string */
3389 char *default_fmt
= "%s";
3393 /* Limiting the number of bytes that the following call
3394 will read protects us from sprintf overflow later. */
3395 i
= target_read_string (addrval
, /* src */
3397 available
, /* len */
3399 if (err
) /* read failed */
3400 error ("%s on target_read", safe_strerror (err
));
3402 tmp
[i
] = '\0'; /* force-terminate string */
3403 /* FIXME: how to protect GDB from crashing
3404 from bad user-supplied format string? */
3405 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3411 /* display it as a pointer */
3412 char *default_fmt
= "0x%x";
3414 /* FIXME: how to protect GDB from crashing
3415 from bad user-supplied format string? */
3416 if (available
>= 16 /*? */ ) /* overflow protect */
3417 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3423 char *default_fmt
= "%g";
3425 doubleval
= value_as_double (arg_val
);
3426 /* FIXME: how to protect GDB from crashing
3427 from bad user-supplied format string? */
3428 if (available
>= 16 /*? */ ) /* overflow protect */
3429 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3430 (double) doubleval
);
3435 char *default_fmt
= "%d";
3437 longval
= value_as_long (arg_val
);
3438 /* FIXME: how to protect GDB from crashing
3439 from bad user-supplied format string? */
3440 if (available
>= 16 /*? */ ) /* overflow protect */
3441 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3445 case TYPE_CODE_BOOL
:
3447 /* no default format for bool */
3448 longval
= value_as_long (arg_val
);
3449 if (available
>= 8 /*? */ ) /* overflow protect */
3452 strcpy (outp
, "<true>");
3454 strcpy (outp
, "<false>");
3458 case TYPE_CODE_ENUM
:
3460 /* no default format for enum */
3461 longval
= value_as_long (arg_val
);
3462 len
= TYPE_NFIELDS (arg_type
);
3463 /* find enum name if possible */
3464 for (i
= 0; i
< len
; i
++)
3465 if (TYPE_FIELD_BITPOS (arg_type
, i
) == longval
)
3466 break; /* match -- end loop */
3468 if (i
< len
) /* enum name found */
3470 char *name
= TYPE_FIELD_NAME (arg_type
, i
);
3472 strncpy (outp
, name
, available
);
3473 /* in casel available < strlen (name), */
3474 outp
[available
] = '\0';
3478 if (available
>= 16 /*? */ ) /* overflow protect */
3479 sprintf (outp
, "%ld", (long) longval
);
3483 case TYPE_CODE_VOID
:
3485 break; /* void type -- no output */
3487 error ("bad data type at prompt position %d",
3488 promptp
- local_prompt
);
3491 outp
+= strlen (outp
);
3494 *outp
++ = '\0'; /* terminate prompt string */
3502 static char buf
[MAX_PROMPT_SIZE
];
3504 if (catch_errors (get_prompt_1
, buf
, "bad formatted prompt: ",
3507 return &buf
[0]; /* successful formatted prompt */
3511 /* Prompt could not be formatted. */
3515 return gdb_prompt_string
;
3523 /* ??rehrauer: I don't know why this fails, since it looks as though
3524 assignments to prompt are wrapped in calls to savestring...
3529 PROMPT (0) = savestring (s
, strlen (s
));
3531 gdb_prompt_string
= savestring (s
, strlen (s
));
3535 /* If necessary, make the user confirm that we should quit. Return
3536 non-zero if we should quit, zero if we shouldn't. */
3541 if (inferior_pid
!= 0 && target_has_execution
)
3545 /* This is something of a hack. But there's no reliable way to
3546 see if a GUI is running. The `use_windows' variable doesn't
3549 s
= "A debugging session is active.\nDo you still want to close the debugger?";
3550 else if (attach_flag
)
3551 s
= "The program is running. Quit anyway (and detach it)? ";
3553 s
= "The program is running. Exit anyway? ";
3562 /* Quit without asking for confirmation. */
3565 quit_force (args
, from_tty
)
3571 /* An optional expression may be used to cause gdb to terminate with the
3572 value of that expression. */
3575 value_ptr val
= parse_and_eval (args
);
3577 exit_code
= (int) value_as_long (val
);
3580 if (inferior_pid
!= 0 && target_has_execution
)
3583 target_detach (args
, from_tty
);
3588 /* UDI wants this, to kill the TIP. */
3591 /* Save the history information if it is appropriate to do so. */
3592 if (write_history_p
&& history_filename
)
3593 write_history (history_filename
);
3595 do_final_cleanups (ALL_CLEANUPS
); /* Do any final cleanups before exiting */
3598 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3599 /* The above does not need to be inside a tuiDo(), since
3600 * it is not manipulating the curses screen, but rather,
3601 * it is tearing it down.
3610 /* Handle the quit command. */
3613 quit_command (args
, from_tty
)
3617 if (!quit_confirm ())
3618 error ("Not confirmed.");
3619 quit_force (args
, from_tty
);
3622 /* Returns whether GDB is running on a terminal and whether the user
3623 desires that questions be asked of them on that terminal. */
3626 input_from_terminal_p ()
3628 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
3633 pwd_command (args
, from_tty
)
3638 error ("The \"pwd\" command does not take an argument: %s", args
);
3639 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3641 if (!STREQ (gdb_dirbuf
, current_directory
))
3642 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3643 current_directory
, gdb_dirbuf
);
3645 printf_unfiltered ("Working directory %s.\n", current_directory
);
3649 cd_command (dir
, from_tty
)
3654 /* Found something other than leading repetitions of "/..". */
3655 int found_real_path
;
3658 /* If the new directory is absolute, repeat is a no-op; if relative,
3659 repeat might be useful but is more likely to be a mistake. */
3663 error_no_arg ("new working directory");
3665 dir
= tilde_expand (dir
);
3666 make_cleanup (free
, dir
);
3668 if (chdir (dir
) < 0)
3669 perror_with_name (dir
);
3671 #if defined(_WIN32) || defined(__MSDOS__)
3672 /* There's too much mess with DOSish names like "d:", "d:.",
3673 "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
3674 simply get the canonicalized name of the current directory. */
3675 dir
= getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3679 if (SLASH_P (dir
[len
- 1]))
3681 /* Remove the trailing slash unless this is a root directory
3682 (including a drive letter on non-Unix systems). */
3683 if (!(len
== 1) /* "/" */
3684 #if defined(_WIN32) || defined(__MSDOS__)
3685 && !(!SLASH_P (*dir
) && ROOTED_P (dir
) && len
<= 3) /* "d:/" */
3691 dir
= savestring (dir
, len
);
3693 current_directory
= dir
;
3696 if (SLASH_P (current_directory
[strlen (current_directory
) - 1]))
3697 current_directory
= concat (current_directory
, dir
, NULL
);
3699 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
3703 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3705 found_real_path
= 0;
3706 for (p
= current_directory
; *p
;)
3708 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
3710 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
3711 && (p
[3] == 0 || SLASH_P (p
[3])))
3713 if (found_real_path
)
3715 /* Search backwards for the directory just before the "/.."
3716 and obliterate it and the "/..". */
3718 while (q
!= current_directory
&& !SLASH_P (q
[-1]))
3721 if (q
== current_directory
)
3722 /* current_directory is
3723 a relative pathname ("can't happen"--leave it alone). */
3727 strcpy (q
- 1, p
+ 3);
3732 /* We are dealing with leading repetitions of "/..", for example
3733 "/../..", which is the Mach super-root. */
3738 found_real_path
= 1;
3743 forget_cached_source_info ();
3746 pwd_command ((char *) 0, 1);
3749 struct source_cleanup_lines_args
3753 char *old_pre_error
;
3754 char *old_error_pre_print
;
3758 source_cleanup_lines (args
)
3761 struct source_cleanup_lines_args
*p
=
3762 (struct source_cleanup_lines_args
*) args
;
3763 source_line_number
= p
->old_line
;
3764 source_file_name
= p
->old_file
;
3765 source_pre_error
= p
->old_pre_error
;
3766 error_pre_print
= p
->old_error_pre_print
;
3771 do_fclose_cleanup (void *stream
)
3777 source_command (args
, from_tty
)
3782 struct cleanup
*old_cleanups
;
3784 struct source_cleanup_lines_args old_lines
;
3789 error ("source command requires pathname of file to source.");
3792 file
= tilde_expand (file
);
3793 old_cleanups
= make_cleanup (free
, file
);
3795 stream
= fopen (file
, FOPEN_RT
);
3799 perror_with_name (file
);
3804 make_cleanup (do_fclose_cleanup
, stream
);
3806 old_lines
.old_line
= source_line_number
;
3807 old_lines
.old_file
= source_file_name
;
3808 old_lines
.old_pre_error
= source_pre_error
;
3809 old_lines
.old_error_pre_print
= error_pre_print
;
3810 make_cleanup (source_cleanup_lines
, &old_lines
);
3811 source_line_number
= 0;
3812 source_file_name
= file
;
3813 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
3814 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
3815 make_cleanup (free
, source_pre_error
);
3816 /* This will get set every time we read a line. So it won't stay "" for
3818 error_pre_print
= "";
3820 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
3821 if (source_error_allocated
< needed_length
)
3823 source_error_allocated
*= 2;
3824 if (source_error_allocated
< needed_length
)
3825 source_error_allocated
= needed_length
;
3826 if (source_error
== NULL
)
3827 source_error
= xmalloc (source_error_allocated
);
3829 source_error
= xrealloc (source_error
, source_error_allocated
);
3832 read_command_file (stream
);
3834 do_cleanups (old_cleanups
);
3839 echo_command (text
, from_tty
)
3847 while ((c
= *p
++) != '\0')
3851 /* \ at end of argument is used after spaces
3852 so they won't be lost. */
3856 c
= parse_escape (&p
);
3858 printf_filtered ("%c", c
);
3861 printf_filtered ("%c", c
);
3864 /* Force this output to appear now. */
3866 gdb_flush (gdb_stdout
);
3871 dont_repeat_command (ignored
, from_tty
)
3875 *line
= 0; /* Can't call dont_repeat here because we're not
3876 necessarily reading from stdin. */
3879 /* Functions to manipulate command line editing control variables. */
3881 /* Number of commands to print in each call to show_commands. */
3882 #define Hist_print 10
3884 show_commands (args
, from_tty
)
3888 /* Index for history commands. Relative to history_base. */
3891 /* Number of the history entry which we are planning to display next.
3892 Relative to history_base. */
3895 /* The first command in the history which doesn't exist (i.e. one more
3896 than the number of the last command). Relative to history_base. */
3899 extern HIST_ENTRY
*history_get (int);
3901 /* Print out some of the commands from the command history. */
3902 /* First determine the length of the history list. */
3903 hist_len
= history_size
;
3904 for (offset
= 0; offset
< history_size
; offset
++)
3906 if (!history_get (history_base
+ offset
))
3915 if (args
[0] == '+' && args
[1] == '\0')
3916 /* "info editing +" should print from the stored position. */
3919 /* "info editing <exp>" should print around command number <exp>. */
3920 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3922 /* "show commands" means print the last Hist_print commands. */
3925 num
= hist_len
- Hist_print
;
3931 /* If there are at least Hist_print commands, we want to display the last
3932 Hist_print rather than, say, the last 6. */
3933 if (hist_len
- num
< Hist_print
)
3935 num
= hist_len
- Hist_print
;
3940 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3942 printf_filtered ("%5d %s\n", history_base
+ offset
,
3943 (history_get (history_base
+ offset
))->line
);
3946 /* The next command we want to display is the next one that we haven't
3950 /* If the user repeats this command with return, it should do what
3951 "show commands +" does. This is unnecessary if arg is null,
3952 because "show commands +" is not useful after "show commands". */
3953 if (from_tty
&& args
)
3960 /* Called by do_setshow_command. */
3963 set_history_size_command (args
, from_tty
, c
)
3966 struct cmd_list_element
*c
;
3968 if (history_size
== INT_MAX
)
3969 unstifle_history ();
3970 else if (history_size
>= 0)
3971 stifle_history (history_size
);
3974 history_size
= INT_MAX
;
3975 error ("History size must be non-negative");
3981 set_history (args
, from_tty
)
3985 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3986 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3991 show_history (args
, from_tty
)
3995 cmd_show_list (showhistlist
, from_tty
, "");
3998 int info_verbose
= 0; /* Default verbose msgs off */
4000 /* Called by do_setshow_command. An elaborate joke. */
4003 set_verbose (args
, from_tty
, c
)
4006 struct cmd_list_element
*c
;
4008 char *cmdname
= "verbose";
4009 struct cmd_list_element
*showcmd
;
4011 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
4015 c
->doc
= "Set verbose printing of informational messages.";
4016 showcmd
->doc
= "Show verbose printing of informational messages.";
4020 c
->doc
= "Set verbosity.";
4021 showcmd
->doc
= "Show verbosity.";
4026 float_handler (signo
)
4029 /* This message is based on ANSI C, section 4.7. Note that integer
4030 divide by zero causes this, so "float" is a misnomer. */
4031 signal (SIGFPE
, float_handler
);
4032 error ("Erroneous arithmetic operation.");
4036 set_debug (arg
, from_tty
)
4040 printf_unfiltered ("\"set debug\" must be followed by the name of a print subcommand.\n");
4041 help_list (setdebuglist
, "set debug ", -1, gdb_stdout
);
4045 show_debug (args
, from_tty
)
4049 cmd_show_list (showdebuglist
, from_tty
, "");
4062 enablebreaklist
= NULL
;
4067 showhistlist
= NULL
;
4068 unsethistlist
= NULL
;
4069 maintenancelist
= NULL
;
4070 maintenanceinfolist
= NULL
;
4071 maintenanceprintlist
= NULL
;
4072 setprintlist
= NULL
;
4073 showprintlist
= NULL
;
4074 setchecklist
= NULL
;
4075 showchecklist
= NULL
;
4078 /* Init the history buffer. Note that we are called after the init file(s)
4079 * have been read so that the user can change the history file via his
4080 * .gdbinit file (for instance). The GDBHISTFILE environment variable
4081 * overrides all of this.
4089 tmpenv
= getenv ("HISTSIZE");
4091 history_size
= atoi (tmpenv
);
4092 else if (!history_size
)
4095 stifle_history (history_size
);
4097 tmpenv
= getenv ("GDBHISTFILE");
4099 history_filename
= savestring (tmpenv
, strlen (tmpenv
));
4100 else if (!history_filename
)
4102 /* We include the current directory so that if the user changes
4103 directories the file written will be the same as the one
4106 /* No leading dots in file names are allowed on MSDOS. */
4107 history_filename
= concat (current_directory
, "/_gdb_history", NULL
);
4109 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
4112 read_history (history_filename
);
4118 struct cmd_list_element
*c
;
4120 /* If we are running the asynchronous version,
4121 we initialize the prompts differently. */
4124 gdb_prompt_string
= savestring (DEFAULT_PROMPT
, strlen (DEFAULT_PROMPT
));
4128 /* initialize the prompt stack to a simple "(gdb) " prompt or to
4129 whatever the DEFAULT_PROMPT is. */
4130 the_prompts
.top
= 0;
4132 PROMPT (0) = savestring (DEFAULT_PROMPT
, strlen (DEFAULT_PROMPT
));
4134 /* Set things up for annotation_level > 1, if the user ever decides
4136 async_annotation_suffix
= "prompt";
4137 /* Set the variable associated with the setshow prompt command. */
4138 new_async_prompt
= savestring (PROMPT (0), strlen (PROMPT (0)));
4140 gdb_prompt_escape
= 0; /* default to none. */
4142 /* Set the important stuff up for command editing. */
4143 command_editing_p
= 1;
4144 history_expansion_p
= 0;
4145 write_history_p
= 0;
4147 /* Setup important stuff for command line editing. */
4148 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
4149 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
4150 rl_completer_quote_characters
= gdb_completer_quote_characters
;
4151 rl_readline_name
= "gdb";
4153 /* Define the classes of commands.
4154 They will appear in the help list in the reverse of this order. */
4156 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
4157 "Maintenance commands.\n\
4158 Some gdb commands are provided just for use by gdb maintainers.\n\
4159 These commands are subject to frequent change, and may not be as\n\
4160 well documented as user commands.",
4162 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
4163 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
4164 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
4165 The commands in this class are those defined by the user.\n\
4166 Use the \"define\" command to define a command.", &cmdlist
);
4167 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
4169 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
4170 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
4171 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
4172 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
4173 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
4174 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
4175 counting from zero for the innermost (currently executing) frame.\n\n\
4176 At any time gdb identifies one frame as the \"selected\" frame.\n\
4177 Variable lookups are done with respect to the selected frame.\n\
4178 When the program being debugged stops, gdb selects the innermost frame.\n\
4179 The commands below can be used to select other frames by number or address.",
4181 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
4183 add_com ("pwd", class_files
, pwd_command
,
4184 "Print working directory. This is used for your program as well.");
4185 c
= add_cmd ("cd", class_files
, cd_command
,
4186 "Set working directory to DIR for debugger and program being debugged.\n\
4187 The change does not take effect for the program being debugged\n\
4188 until the next time it is started.", &cmdlist
);
4189 c
->completer
= filename_completer
;
4191 /* The set prompt command is different depending whether or not the
4192 async version is run. NOTE: this difference is going to
4193 disappear as we make the event loop be the default engine of
4198 (add_set_cmd ("prompt", class_support
, var_string
,
4199 (char *) &gdb_prompt_string
, "Set gdb's prompt",
4205 c
= add_set_cmd ("prompt", class_support
, var_string
,
4206 (char *) &new_async_prompt
, "Set gdb's prompt",
4208 add_show_from_set (c
, &showlist
);
4209 c
->function
.sfunc
= set_async_prompt
;
4213 (add_set_cmd ("prompt-escape-char", class_support
, var_zinteger
,
4214 (char *) &gdb_prompt_escape
,
4215 "Set escape character for formatting of gdb's prompt",
4219 add_com ("echo", class_support
, echo_command
,
4220 "Print a constant string. Give string as argument.\n\
4221 C escape sequences may be used in the argument.\n\
4222 No newline is added at the end of the argument;\n\
4223 use \"\\n\" if you want a newline to be printed.\n\
4224 Since leading and trailing whitespace are ignored in command arguments,\n\
4225 if you want to print some you must use \"\\\" before leading whitespace\n\
4226 to be printed or after trailing whitespace.");
4227 add_com ("document", class_support
, document_command
,
4228 "Document a user-defined command.\n\
4229 Give command name as argument. Give documentation on following lines.\n\
4230 End with a line of just \"end\".");
4231 add_com ("define", class_support
, define_command
,
4232 "Define a new command name. Command name is argument.\n\
4233 Definition appears on following lines, one command per line.\n\
4234 End with a line of just \"end\".\n\
4235 Use the \"document\" command to give documentation for the new command.\n\
4236 Commands defined in this way may have up to ten arguments.");
4239 c
= add_cmd ("source", class_support
, source_command
,
4240 "Read commands from a file named FILE.\n\
4241 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
4242 when gdb is started.", &cmdlist
);
4244 /* Punt file name, we can't help it easily. */
4245 c
= add_cmd ("source", class_support
, source_command
,
4246 "Read commands from a file named FILE.\n\
4247 Note that the file \".gdbinit\" is read automatically in this way\n\
4248 when gdb is started.", &cmdlist
);
4250 c
->completer
= filename_completer
;
4252 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
4253 add_com ("help", class_support
, help_command
, "Print list of commands.");
4254 add_com_alias ("q", "quit", class_support
, 1);
4255 add_com_alias ("h", "help", class_support
, 1);
4257 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
4258 Primarily used inside of user-defined commands that should not be repeated when\n\
4261 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *) &info_verbose
,
4264 add_show_from_set (c
, &showlist
);
4265 c
->function
.sfunc
= set_verbose
;
4266 set_verbose (NULL
, 0, c
);
4268 /* The set editing command is different depending whether or not the
4269 async version is run. NOTE: this difference is going to disappear
4270 as we make the event loop be the default engine of gdb. */
4274 (add_set_cmd ("editing", class_support
, var_boolean
, (char *) &command_editing_p
,
4275 "Set editing of command lines as they are typed.\n\
4276 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4277 Without an argument, command line editing is enabled. To edit, use\n\
4278 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
4283 c
= add_set_cmd ("editing", class_support
, var_boolean
, (char *) &async_command_editing_p
,
4284 "Set editing of command lines as they are typed.\n\
4285 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4286 Without an argument, command line editing is enabled. To edit, use\n\
4287 EMACS-like or VI-like commands like control-P or ESC.", &setlist
);
4289 add_show_from_set (c
, &showlist
);
4290 c
->function
.sfunc
= set_async_editing_command
;
4293 add_prefix_cmd ("history", class_support
, set_history
,
4294 "Generic command for setting command history parameters.",
4295 &sethistlist
, "set history ", 0, &setlist
);
4296 add_prefix_cmd ("history", class_support
, show_history
,
4297 "Generic command for showing command history parameters.",
4298 &showhistlist
, "show history ", 0, &showlist
);
4301 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *) &history_expansion_p
,
4302 "Set history expansion on command input.\n\
4303 Without an argument, history expansion is enabled.", &sethistlist
),
4307 (add_set_cmd ("save", no_class
, var_boolean
, (char *) &write_history_p
,
4308 "Set saving of the history record on exit.\n\
4309 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
4310 Without an argument, saving is enabled.", &sethistlist
),
4313 c
= add_set_cmd ("size", no_class
, var_integer
, (char *) &history_size
,
4314 "Set the size of the command history, \n\
4315 ie. the number of previous commands to keep a record of.", &sethistlist
);
4316 add_show_from_set (c
, &showhistlist
);
4317 c
->function
.sfunc
= set_history_size_command
;
4320 (add_set_cmd ("filename", no_class
, var_filename
, (char *) &history_filename
,
4321 "Set the filename in which to record the command history\n\
4322 (the list of previous commands of which a record is kept).", &sethistlist
),
4326 (add_set_cmd ("confirm", class_support
, var_boolean
,
4328 "Set whether to confirm potentially dangerous operations.",
4332 add_prefix_cmd ("info", class_info
, info_command
,
4333 "Generic command for showing things about the program being debugged.",
4334 &infolist
, "info ", 0, &cmdlist
);
4335 add_com_alias ("i", "info", class_info
, 1);
4337 add_com ("complete", class_obscure
, complete_command
,
4338 "List the completions for the rest of the line as a command.");
4340 add_prefix_cmd ("show", class_info
, show_command
,
4341 "Generic command for showing things about the debugger.",
4342 &showlist
, "show ", 0, &cmdlist
);
4343 /* Another way to get at the same thing. */
4344 add_info ("set", show_command
, "Show all GDB settings.");
4346 add_cmd ("commands", no_class
, show_commands
,
4347 "Show the history of commands you typed.\n\
4348 You can supply a command number to start with, or a `+' to start after\n\
4349 the previous command number shown.",
4352 add_cmd ("version", no_class
, show_version
,
4353 "Show what version of GDB this is.", &showlist
);
4355 add_com ("while", class_support
, while_command
,
4356 "Execute nested commands WHILE the conditional expression is non zero.\n\
4357 The conditional expression must follow the word `while' and must in turn be\n\
4358 followed by a new line. The nested commands must be entered one per line,\n\
4359 and should be terminated by the word `end'.");
4361 add_com ("if", class_support
, if_command
,
4362 "Execute nested commands once IF the conditional expression is non zero.\n\
4363 The conditional expression must follow the word `if' and must in turn be\n\
4364 followed by a new line. The nested commands must be entered one per line,\n\
4365 and should be terminated by the word 'else' or `end'. If an else clause\n\
4366 is used, the same rules apply to its nested commands as to the first ones.");
4368 /* If target is open when baud changes, it doesn't take effect until the
4369 next open (I think, not sure). */
4370 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
4371 var_zinteger
, (char *) &baud_rate
,
4372 "Set baud rate for remote serial I/O.\n\
4373 This value is used to set the speed of the serial port when debugging\n\
4374 using remote targets.", &setlist
),
4377 c
= add_set_cmd ("remotedebug", no_class
, var_zinteger
,
4378 (char *) &remote_debug
,
4379 "Set debugging of remote protocol.\n\
4380 When enabled, each packet sent or received with the remote target\n\
4381 is displayed.", &setlist
);
4382 deprecate_cmd (c
, "set debug remote");
4383 deprecate_cmd (add_show_from_set (c
, &showlist
), "show debug remote");
4385 add_show_from_set (add_set_cmd ("remote", no_class
, var_zinteger
,
4386 (char *) &remote_debug
,
4387 "Set debugging of remote protocol.\n\
4388 When enabled, each packet sent or received with the remote target\n\
4389 is displayed.", &setdebuglist
),
4393 add_set_cmd ("remotetimeout", no_class
, var_integer
, (char *) &remote_timeout
,
4394 "Set timeout limit to wait for target to respond.\n\
4395 This value is used to set the time limit for gdb to wait for a response\n\
4396 from the target.", &setlist
),
4399 /* The set annotate command is different depending whether or not
4400 the async version is run. NOTE: this difference is going to
4401 disappear as we make the event loop be the default engine of
4405 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
4406 (char *) &annotation_level
, "Set annotation_level.\n\
4407 0 == normal; 1 == fullname (for use when running under emacs)\n\
4408 2 == output annotated suitably for use by programs that control GDB.",
4410 c
= add_show_from_set (c
, &showlist
);
4414 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
4415 (char *) &annotation_level
, "Set annotation_level.\n\
4416 0 == normal; 1 == fullname (for use when running under emacs)\n\
4417 2 == output annotated suitably for use by programs that control GDB.",
4419 add_show_from_set (c
, &showlist
);
4420 c
->function
.sfunc
= set_async_annotation_level
;
4425 (add_set_cmd ("exec-done-display", class_support
, var_boolean
, (char *) &exec_done_display_p
,
4426 "Set notification of completion for asynchronous execution commands.\n\
4427 Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist
),
4430 add_prefix_cmd ("debug", no_class
, set_debug
,
4431 "Generic command for setting gdb debugging flags",
4432 &setdebuglist
, "set debug ", 0, &setlist
);
4434 add_prefix_cmd ("debug", no_class
, show_debug
,
4435 "Generic command for showing gdb debugging flags",
4436 &showdebuglist
, "show debug ", 0, &showlist
);