1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986-2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "call-cmds.h"
28 #include "breakpoint.h"
30 #include "expression.h"
33 #include "terminal.h" /* For job_control. */
38 /* readline include files */
39 #include <readline/readline.h>
40 #include <readline/history.h>
42 /* readline defines this. */
45 #include <sys/types.h>
49 #include "event-top.h"
50 #include "gdb_string.h"
58 /* Prototypes for local functions */
60 static void dont_repeat_command
PARAMS ((char *, int));
62 static void source_cleanup_lines
PARAMS ((PTR
));
64 static void user_defined_command
PARAMS ((char *, int));
66 static void init_signals
PARAMS ((void));
69 static void stop_sig
PARAMS ((int));
72 static char *line_completion_function
PARAMS ((char *, int, char *, int));
74 static char *readline_line_completion_function
PARAMS ((char *, int));
76 /* NOTE 1999-04-29: this function will be static again, after we make the
77 event loop be the default command loop for gdb, and we merge
78 event-top.c into this file, top.c */
79 /* static */ void command_loop_marker
PARAMS ((int));
81 static void while_command
PARAMS ((char *, int));
83 static void if_command
PARAMS ((char *, int));
85 static struct command_line
*
86 build_command_line
PARAMS ((enum command_control_type
, char *));
88 static struct command_line
*
89 get_command_line
PARAMS ((enum command_control_type
, char *));
91 static void realloc_body_list
PARAMS ((struct command_line
*, int));
93 static enum misc_command_type read_next_line
PARAMS ((struct command_line
**));
95 static enum command_control_type
96 recurse_read_control_structure
PARAMS ((struct command_line
*));
98 static struct cleanup
*setup_user_args
PARAMS ((char *));
100 static char *locate_arg
PARAMS ((char *));
102 static char *insert_args
PARAMS ((char *));
104 static void arg_cleanup
PARAMS ((void));
106 static void init_main
PARAMS ((void));
108 static void init_cmd_lists
PARAMS ((void));
110 static void float_handler
PARAMS ((int));
112 static void init_signals
PARAMS ((void));
114 static void set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
116 static void show_history
PARAMS ((char *, int));
118 static void set_history
PARAMS ((char *, int));
120 static void set_history_size_command
PARAMS ((char *, int,
121 struct cmd_list_element
*));
123 static void show_commands
PARAMS ((char *, int));
125 static void echo_command
PARAMS ((char *, int));
127 static void pwd_command
PARAMS ((char *, int));
129 static void show_version
PARAMS ((char *, int));
131 static void document_command
PARAMS ((char *, int));
133 static void define_command
PARAMS ((char *, int));
135 static void validate_comname
PARAMS ((char *));
137 static void help_command
PARAMS ((char *, int));
139 static void show_command
PARAMS ((char *, int));
141 static void info_command
PARAMS ((char *, int));
143 static void complete_command
PARAMS ((char *, int));
145 static void do_nothing
PARAMS ((int));
148 /* NOTE 1999-04-29: This function will be static again, once we modify
149 gdb to use the event loop as the default command loop and we merge
150 event-top.c into this file, top.c */
151 /* static */ int quit_cover
PARAMS ((PTR
));
153 static void disconnect
PARAMS ((int));
156 static void source_cleanup
PARAMS ((FILE *));
158 /* Default command line prompt. This is overriden in some configs. */
160 #ifndef DEFAULT_PROMPT
161 #define DEFAULT_PROMPT "(gdb) "
164 /* Initialization file name for gdb. This is overridden in some configs. */
166 #ifndef GDBINIT_FILENAME
167 #define GDBINIT_FILENAME ".gdbinit"
169 char gdbinit
[] = GDBINIT_FILENAME
;
171 int inhibit_gdbinit
= 0;
173 /* If nonzero, and GDB has been configured to be able to use windows,
174 attempt to open them upon startup. */
178 extern char lang_frame_mismatch_warn
[]; /* language.c */
180 /* Flag for whether we want all the "from_tty" gubbish printed. */
182 int caution
= 1; /* Default is yes, sigh. */
184 /* Define all cmd_list_elements. */
186 /* Chain containing all defined commands. */
188 struct cmd_list_element
*cmdlist
;
190 /* Chain containing all defined info subcommands. */
192 struct cmd_list_element
*infolist
;
194 /* Chain containing all defined enable subcommands. */
196 struct cmd_list_element
*enablelist
;
198 /* Chain containing all defined disable subcommands. */
200 struct cmd_list_element
*disablelist
;
202 /* Chain containing all defined toggle subcommands. */
204 struct cmd_list_element
*togglelist
;
206 /* Chain containing all defined stop subcommands. */
208 struct cmd_list_element
*stoplist
;
210 /* Chain containing all defined delete subcommands. */
212 struct cmd_list_element
*deletelist
;
214 /* Chain containing all defined "enable breakpoint" subcommands. */
216 struct cmd_list_element
*enablebreaklist
;
218 /* Chain containing all defined set subcommands */
220 struct cmd_list_element
*setlist
;
222 /* Chain containing all defined unset subcommands */
224 struct cmd_list_element
*unsetlist
;
226 /* Chain containing all defined show subcommands. */
228 struct cmd_list_element
*showlist
;
230 /* Chain containing all defined \"set history\". */
232 struct cmd_list_element
*sethistlist
;
234 /* Chain containing all defined \"show history\". */
236 struct cmd_list_element
*showhistlist
;
238 /* Chain containing all defined \"unset history\". */
240 struct cmd_list_element
*unsethistlist
;
242 /* Chain containing all defined maintenance subcommands. */
244 struct cmd_list_element
*maintenancelist
;
246 /* Chain containing all defined "maintenance info" subcommands. */
248 struct cmd_list_element
*maintenanceinfolist
;
250 /* Chain containing all defined "maintenance print" subcommands. */
252 struct cmd_list_element
*maintenanceprintlist
;
254 struct cmd_list_element
*setprintlist
;
256 struct cmd_list_element
*showprintlist
;
258 struct cmd_list_element
*setchecklist
;
260 struct cmd_list_element
*showchecklist
;
262 /* stdio stream that command input is being read from. Set to stdin normally.
263 Set by source_command to the file we are sourcing. Set to NULL if we are
264 executing a user-defined command or interacting via a GUI. */
268 /* Current working directory. */
270 char *current_directory
;
272 /* The directory name is actually stored here (usually). */
273 char gdb_dirbuf
[1024];
275 /* Function to call before reading a command, if nonzero.
276 The function receives two args: an input stream,
277 and a prompt string. */
279 void (*window_hook
) PARAMS ((FILE *, char *));
284 /* gdb prints this when reading a command interactively */
285 static char *gdb_prompt_string
; /* the global prompt string */
286 extern char *get_prompt
PARAMS ((void)); /* access function for prompt string */
288 /* Buffer used for reading command lines, and the size
289 allocated for it so far. */
294 /* Nonzero if the current command is modified by "server ". This
295 affects things like recording into the command history, commands
296 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
297 whatever) can issue its own commands and also send along commands
298 from the user, and have the user not notice that the user interface
299 is issuing commands too. */
302 /* Baud rate specified for talking to serial target systems. Default
303 is left as -1, so targets can choose their own defaults. */
304 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
305 or (unsigned int)-1. This is a Bad User Interface. */
309 /* Timeout limit for response from target. */
311 /* The default value has been changed many times over the years. It
312 was originally 5 seconds. But that was thought to be a long time
313 to sit and wait, so it was changed to 2 seconds. That was thought
314 to be plenty unless the connection was going through some terminal
315 server or multiplexer or other form of hairy serial connection.
317 In mid-1996, remote_timeout was moved from remote.c to top.c and
318 it began being used in other remote-* targets. It appears that the
319 default was changed to 20 seconds at that time, perhaps because the
320 Hitachi E7000 ICE didn't always respond in a timely manner.
322 But if 5 seconds is a long time to sit and wait for retransmissions,
323 20 seconds is far worse. This demonstrates the difficulty of using
324 a single variable for all protocol timeouts.
326 As remote.c is used much more than remote-e7000.c, it was changed
327 back to 2 seconds in 1999. */
329 int remote_timeout
= 2;
331 /* Non-zero tells remote* modules to output debugging info. */
333 int remote_debug
= 0;
335 /* Non-zero means the target is running. Note: this is different from
336 saying that there is an active target and we are stopped at a
337 breakpoint, for instance. This is a real indicator whether the
338 target is off and running, which gdb is doing something else. */
339 int target_executing
= 0;
341 /* Level of control structure. */
342 static int control_level
;
344 /* Structure for arguments to user defined functions. */
345 #define MAXUSERARGS 10
348 struct user_args
*next
;
359 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
363 #define STOP_SIGNAL SIGTSTP
364 static void stop_sig
PARAMS ((int));
368 /* Some System V have job control but not sigsetmask(). */
369 #if !defined (HAVE_SIGSETMASK)
371 #define HAVE_SIGSETMASK 1
373 #define HAVE_SIGSETMASK 0
377 #if 0 == (HAVE_SIGSETMASK)
378 #define sigsetmask(n)
381 /* Hooks for alternate command interfaces. */
383 /* Called after most modules have been initialized, but before taking users
386 void (*init_ui_hook
) PARAMS ((char *argv0
));
388 /* This hook is called from within gdb's many mini-event loops which could
389 steal control from a real user interface's event loop. It returns
390 non-zero if the user is requesting a detach, zero otherwise. */
392 int (*ui_loop_hook
) PARAMS ((int));
394 /* Called instead of command_loop at top level. Can be invoked via
395 return_to_top_level. */
397 void (*command_loop_hook
) PARAMS ((void));
400 /* Called from print_frame_info to list the line we stopped in. */
402 void (*print_frame_info_listing_hook
) PARAMS ((struct symtab
* s
, int line
,
403 int stopline
, int noerror
));
404 /* Replaces most of query. */
406 int (*query_hook
) PARAMS ((const char *, va_list));
408 /* Replaces most of warning. */
410 void (*warning_hook
) PARAMS ((const char *, va_list));
412 /* These three functions support getting lines of text from the user. They
413 are used in sequence. First readline_begin_hook is called with a text
414 string that might be (for example) a message for the user to type in a
415 sequence of commands to be executed at a breakpoint. If this function
416 calls back to a GUI, it might take this opportunity to pop up a text
417 interaction window with this message. Next, readline_hook is called
418 with a prompt that is emitted prior to collecting the user input.
419 It can be called multiple times. Finally, readline_end_hook is called
420 to notify the GUI that we are done with the interaction window and it
423 void (*readline_begin_hook
) PARAMS ((char *,...));
424 char *(*readline_hook
) PARAMS ((char *));
425 void (*readline_end_hook
) PARAMS ((void));
427 /* Called as appropriate to notify the interface of the specified breakpoint
430 void (*create_breakpoint_hook
) PARAMS ((struct breakpoint
* bpt
));
431 void (*delete_breakpoint_hook
) PARAMS ((struct breakpoint
* bpt
));
432 void (*modify_breakpoint_hook
) PARAMS ((struct breakpoint
* bpt
));
434 /* Called as appropriate to notify the interface that we have attached
435 to or detached from an already running process. */
437 void (*attach_hook
) PARAMS ((void));
438 void (*detach_hook
) PARAMS ((void));
440 /* Called during long calculations to allow GUI to repair window damage, and to
441 check for stop buttons, etc... */
443 void (*interactive_hook
) PARAMS ((void));
445 /* Called when the registers have changed, as a hint to a GUI
446 to minimize window update. */
448 void (*registers_changed_hook
) PARAMS ((void));
450 /* Tell the GUI someone changed the register REGNO. -1 means
451 that the caller does not know which register changed or
452 that several registers have changed (see value_assign). */
453 void (*register_changed_hook
) PARAMS ((int regno
));
455 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
456 void (*memory_changed_hook
) PARAMS ((CORE_ADDR addr
, int len
));
458 /* Called when going to wait for the target. Usually allows the GUI to run
459 while waiting for target events. */
461 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
* status
));
463 /* Used by UI as a wrapper around command execution. May do various things
464 like enabling/disabling buttons, etc... */
466 void (*call_command_hook
) PARAMS ((struct cmd_list_element
* c
, char *cmd
,
469 /* Called after a `set' command has finished. Is only run if the
470 `set' command succeeded. */
472 void (*set_hook
) PARAMS ((struct cmd_list_element
*c
));
474 /* Called when the current thread changes. Argument is thread id. */
476 void (*context_hook
) PARAMS ((int id
));
478 /* Takes control from error (). Typically used to prevent longjmps out of the
479 middle of the GUI. Usually used in conjunction with a catch routine. */
481 NORETURN
void (*error_hook
)
482 PARAMS ((void)) ATTR_NORETURN
;
485 /* One should use catch_errors rather than manipulating these
487 #if defined(HAVE_SIGSETJMP)
488 #define SIGJMP_BUF sigjmp_buf
489 #define SIGSETJMP(buf) sigsetjmp(buf, 1)
490 #define SIGLONGJMP(buf,val) siglongjmp(buf,val)
492 #define SIGJMP_BUF jmp_buf
493 #define SIGSETJMP(buf) setjmp(buf)
494 #define SIGLONGJMP(buf,val) longjmp(buf,val)
497 /* Where to go for return_to_top_level. */
498 static SIGJMP_BUF
*catch_return
;
500 /* Return for reason REASON to the nearest containing catch_errors(). */
503 return_to_top_level (reason
)
504 enum return_reason reason
;
509 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
510 I can think of a reason why that is vital, though). */
511 bpstat_clear_actions (stop_bpstat
); /* Clear queued breakpoint commands */
513 disable_current_display ();
514 do_cleanups (ALL_CLEANUPS
);
515 if (event_loop_p
&& target_can_async_p () && !target_executing
)
516 do_exec_cleanups (ALL_CLEANUPS
);
517 if (event_loop_p
&& sync_execution
)
518 do_exec_error_cleanups (ALL_CLEANUPS
);
520 if (annotation_level
> 1)
531 /* Jump to the containing catch_errors() call, communicating REASON
532 to that call via setjmp's return value. Note that REASON can't
533 be zero, by definition in defs.h. */
535 (NORETURN
void) SIGLONGJMP (*catch_return
, (int)reason
);
538 /* Call FUNC with arg ARGS, catching any errors. If there is no
539 error, return the value returned by FUNC. If there is an error,
540 print ERRSTRING, print the specific error message, then return
543 Must not be called with immediate_quit in effect (bad things might
544 happen, say we got a signal in the middle of a memcpy to quit_return).
545 This is an OK restriction; with very few exceptions immediate_quit can
546 be replaced by judicious use of QUIT.
548 MASK specifies what to catch; it is normally set to
549 RETURN_MASK_ALL, if for no other reason than that the code which
550 calls catch_errors might not be set up to deal with a quit which
551 isn't caught. But if the code can deal with it, it generally
552 should be RETURN_MASK_ERROR, unless for some reason it is more
553 useful to abort only the portion of the operation inside the
554 catch_errors. Note that quit should return to the command line
555 fairly quickly, even if some further processing is being done. */
557 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
558 error() et.al. could maintain a set of flags that indicate the the
559 current state of each of the longjmp buffers. This would give the
560 longjmp code the chance to detect a longjmp botch (before it gets
561 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
562 code also randomly used a SET_TOP_LEVEL macro that directly
563 initialize the longjmp buffers. */
565 /* MAYBE: cagney/1999-11-05: Should the catch_erros and cleanups code
566 be consolidated into a single file instead of being distributed
567 between utils.c and top.c? */
570 catch_errors (func
, args
, errstring
, mask
)
571 catch_errors_ftype
*func
;
576 SIGJMP_BUF
*saved_catch
;
579 struct cleanup
*saved_cleanup_chain
;
580 char *saved_error_pre_print
;
581 char *saved_quit_pre_print
;
583 /* Return value from SIGSETJMP(): enum return_reason if error or
584 quit caught, 0 otherwise. */
587 /* Override error/quit messages during FUNC. */
589 saved_error_pre_print
= error_pre_print
;
590 saved_quit_pre_print
= quit_pre_print
;
592 if (mask
& RETURN_MASK_ERROR
)
593 error_pre_print
= errstring
;
594 if (mask
& RETURN_MASK_QUIT
)
595 quit_pre_print
= errstring
;
597 /* Prevent error/quit during FUNC from calling cleanups established
600 saved_cleanup_chain
= save_cleanups ();
602 /* Call FUNC, catching error/quit events. */
604 saved_catch
= catch_return
;
605 catch_return
= &catch;
606 caught
= SIGSETJMP (catch);
608 val
= (*func
) (args
);
609 catch_return
= saved_catch
;
611 /* FIXME: cagney/1999-11-05: A correct FUNC implementaton will
612 clean things up (restoring the cleanup chain) to the state they
613 were just prior to the call. Unfortunatly, many FUNC's are not
614 that well behaved. This could be fixed by adding either a
615 do_cleanups call (to cover the problem) or an assertion check to
616 detect bad FUNCs code. */
618 /* Restore the cleanup chain and error/quit messages to their
621 restore_cleanups (saved_cleanup_chain
);
623 if (mask
& RETURN_MASK_QUIT
)
624 quit_pre_print
= saved_quit_pre_print
;
625 if (mask
& RETURN_MASK_ERROR
)
626 error_pre_print
= saved_error_pre_print
;
628 /* Return normally if no error/quit event occurred. */
633 /* If the caller didn't request that the event be caught, relay the
634 event to the next containing catch_errors(). */
636 if (!(mask
& RETURN_MASK (caught
)))
637 return_to_top_level (caught
);
639 /* Tell the caller that an event was caught.
641 FIXME: nsd/2000-02-22: When MASK is RETURN_MASK_ALL, the caller
642 can't tell what type of event occurred.
644 A possible fix is to add a new interface, catch_event(), that
645 returns enum return_reason after catching an error or a quit.
647 When returning normally, i.e. without catching an error or a
648 quit, catch_event() could return RETURN_NORMAL, which would be
649 added to enum return_reason. FUNC would return information
650 exclusively via ARGS.
652 Alternatively, normal catch_event() could return FUNC's return
653 value. The caller would need to be aware of potential overlap
654 with enum return_reason, which could be publicly restricted to
655 negative values to simplify return value processing in FUNC and
661 struct captured_command_args
663 catch_command_errors_ftype
*command
;
669 do_captured_command (void *data
)
671 struct captured_command_args
*context
= data
;
672 context
->command (context
->arg
, context
->from_tty
);
673 /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
674 isn't needed. Instead an assertion check could be made that
675 simply confirmed that the called function correctly cleaned up
676 after its self. Unfortunatly, old code (prior to 1999-11-04) in
677 main.c was calling SET_TOP_LEVEL(), calling the command function,
678 and then *always* calling do_cleanups(). For the moment we
679 remain ``bug compatible'' with that old code.. */
680 do_cleanups (ALL_CLEANUPS
);
685 catch_command_errors (catch_command_errors_ftype
*command
,
686 char *arg
, int from_tty
, return_mask mask
)
688 struct captured_command_args args
;
689 args
.command
= command
;
691 args
.from_tty
= from_tty
;
692 return catch_errors (do_captured_command
, &args
, "", mask
);
696 /* Handler for SIGHUP. */
703 catch_errors (quit_cover
, NULL
,
704 "Could not kill the program being debugged", RETURN_MASK_ALL
);
705 signal (SIGHUP
, SIG_DFL
);
706 kill (getpid (), SIGHUP
);
709 /* Just a little helper function for disconnect(). */
711 /* NOTE 1999-04-29: This function will be static again, once we modify
712 gdb to use the event loop as the default command loop and we merge
713 event-top.c into this file, top.c */
718 caution
= 0; /* Throw caution to the wind -- we're exiting.
719 This prevents asking the user dumb questions. */
720 quit_command ((char *) 0, 0);
723 #endif /* defined SIGHUP */
725 /* Line number we are currently in in a file which is being sourced. */
726 /* NOTE 1999-04-29: This variable will be static again, once we modify
727 gdb to use the event loop as the default command loop and we merge
728 event-top.c into this file, top.c */
729 /* static */ int source_line_number
;
731 /* Name of the file we are sourcing. */
732 /* NOTE 1999-04-29: This variable will be static again, once we modify
733 gdb to use the event loop as the default command loop and we merge
734 event-top.c into this file, top.c */
735 /* static */ char *source_file_name
;
737 /* Buffer containing the error_pre_print used by the source stuff.
739 /* NOTE 1999-04-29: This variable will be static again, once we modify
740 gdb to use the event loop as the default command loop and we merge
741 event-top.c into this file, top.c */
742 /* static */ char *source_error
;
743 static int source_error_allocated
;
745 /* Something to glom on to the start of error_pre_print if source_file_name
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_pre_error
;
752 /* Clean up on error during a "source" command (or execution of a
753 user-defined command). */
756 source_cleanup (stream
)
759 /* Restore the previous input stream. */
763 /* Read commands from STREAM. */
765 read_command_file (stream
)
768 struct cleanup
*cleanups
;
770 cleanups
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
773 do_cleanups (cleanups
);
776 extern void init_proc
PARAMS ((void));
778 void (*pre_init_ui_hook
) PARAMS ((void));
784 if (pre_init_ui_hook
)
787 /* Run the init function of each source file */
789 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
790 current_directory
= gdb_dirbuf
;
793 /* Make sure we return to the original directory upon exit, come
794 what may, since the OS doesn't do that for us. */
795 make_final_cleanup ((make_cleanup_func
) chdir
, strsave (current_directory
));
798 init_cmd_lists (); /* This needs to be done first */
799 initialize_targets (); /* Setup target_terminal macros for utils.c */
800 initialize_utils (); /* Make errors and warnings possible */
801 initialize_all_files ();
802 initialize_current_architecture ();
803 init_main (); /* But that omits this file! Do it now */
805 /* The signal handling mechanism is different depending whether or
806 not the async version is run. NOTE: in the future we plan to make
807 the event loop be the default engine of gdb, and this difference
810 async_init_signals ();
814 /* We need a default language for parsing expressions, so simple things like
815 "set width 0" won't fail if no language is explicitly set in a config file
816 or implicitly set by reading an executable during startup. */
817 set_language (language_c
);
818 expected_language
= current_language
; /* don't warn about the change. */
821 /* Install the default UI */
822 uiout
= cli_out_new (gdb_stdout
);
826 /* All the interpreters should have had a look at things by now.
827 Initialize the selected interpreter. */
828 if (interpreter_p
&& !init_ui_hook
)
830 fprintf_unfiltered (gdb_stderr
, "Interpreter `%s' unrecognized.\n",
837 init_ui_hook (argv0
);
840 /* Allocate, initialize a new command line structure for one of the
841 control commands (if/while). */
843 static struct command_line
*
844 build_command_line (type
, args
)
845 enum command_control_type type
;
848 struct command_line
*cmd
;
851 error ("if/while commands require arguments.\n");
853 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
855 cmd
->control_type
= type
;
859 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
861 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
862 cmd
->line
= savestring (args
, strlen (args
));
866 /* Build and return a new command structure for the control commands
867 such as "if" and "while". */
869 static struct command_line
*
870 get_command_line (type
, arg
)
871 enum command_control_type type
;
874 struct command_line
*cmd
;
875 struct cleanup
*old_chain
= NULL
;
877 /* Allocate and build a new command line structure. */
878 cmd
= build_command_line (type
, arg
);
880 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
, &cmd
);
882 /* Read in the body of this command. */
883 if (recurse_read_control_structure (cmd
) == invalid_control
)
885 warning ("error reading in control structure\n");
886 do_cleanups (old_chain
);
890 discard_cleanups (old_chain
);
894 /* Recursively print a command (including full control structures). */
897 print_command_lines (uiout
, cmd
, depth
)
898 struct ui_out
*uiout
;
899 struct command_line
*cmd
;
902 struct command_line
*list
;
909 ui_out_spaces (uiout
, 2 * depth
);
911 /* A simple command, print it and continue. */
912 if (list
->control_type
== simple_control
)
914 ui_out_field_string (uiout
, NULL
, list
->line
);
915 ui_out_text (uiout
, "\n");
920 /* loop_continue to jump to the start of a while loop, print it
922 if (list
->control_type
== continue_control
)
924 ui_out_field_string (uiout
, NULL
, "loop_continue");
925 ui_out_text (uiout
, "\n");
930 /* loop_break to break out of a while loop, print it and continue. */
931 if (list
->control_type
== break_control
)
933 ui_out_field_string (uiout
, NULL
, "loop_break");
934 ui_out_text (uiout
, "\n");
939 /* A while command. Recursively print its subcommands and continue. */
940 if (list
->control_type
== while_control
)
942 ui_out_text (uiout
, "while ");
943 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
944 ui_out_text (uiout
, "\n");
945 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
946 ui_out_field_string (uiout
, NULL
, "end");
948 ui_out_spaces (uiout
, 2 * depth
);
949 ui_out_text (uiout
, "end\n");
954 /* An if command. Recursively print both arms before continueing. */
955 if (list
->control_type
== if_control
)
957 ui_out_text (uiout
, "if ");
958 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
959 ui_out_text (uiout
, "\n");
961 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
963 /* Show the false arm if it exists. */
964 if (list
->body_count
== 2)
967 ui_out_spaces (uiout
, 2 * depth
);
968 ui_out_field_string (uiout
, NULL
, "else");
969 ui_out_text (uiout
, "else\n");
970 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
973 ui_out_field_string (uiout
, NULL
, "end");
975 ui_out_spaces (uiout
, 2 * depth
);
976 ui_out_text (uiout
, "end\n");
981 /* ignore illegal command type and try next */
987 print_command_line (cmd
, depth
, stream
)
988 struct command_line
*cmd
;
990 struct ui_file
*stream
;
996 for (i
= 0; i
< depth
; i
++)
997 fputs_filtered (" ", stream
);
1000 /* A simple command, print it and return. */
1001 if (cmd
->control_type
== simple_control
)
1003 fputs_filtered (cmd
->line
, stream
);
1004 fputs_filtered ("\n", stream
);
1008 /* loop_continue to jump to the start of a while loop, print it
1010 if (cmd
->control_type
== continue_control
)
1012 fputs_filtered ("loop_continue\n", stream
);
1016 /* loop_break to break out of a while loop, print it and return. */
1017 if (cmd
->control_type
== break_control
)
1019 fputs_filtered ("loop_break\n", stream
);
1023 /* A while command. Recursively print its subcommands before returning. */
1024 if (cmd
->control_type
== while_control
)
1026 struct command_line
*list
;
1027 fputs_filtered ("while ", stream
);
1028 fputs_filtered (cmd
->line
, stream
);
1029 fputs_filtered ("\n", stream
);
1030 list
= *cmd
->body_list
;
1033 print_command_line (list
, depth
+ 1, stream
);
1038 /* An if command. Recursively print both arms before returning. */
1039 if (cmd
->control_type
== if_control
)
1041 fputs_filtered ("if ", stream
);
1042 fputs_filtered (cmd
->line
, stream
);
1043 fputs_filtered ("\n", stream
);
1045 print_command_line (cmd
->body_list
[0], depth
+ 1, stream
);
1047 /* Show the false arm if it exists. */
1048 if (cmd
->body_count
== 2)
1052 for (i
= 0; i
< depth
; i
++)
1053 fputs_filtered (" ", stream
);
1055 fputs_filtered ("else\n", stream
);
1056 print_command_line (cmd
->body_list
[1], depth
+ 1, stream
);
1060 for (i
= 0; i
< depth
; i
++)
1061 fputs_filtered (" ", stream
);
1063 fputs_filtered ("end\n", stream
);
1068 /* Execute the command in CMD. */
1070 enum command_control_type
1071 execute_control_command (cmd
)
1072 struct command_line
*cmd
;
1074 struct expression
*expr
;
1075 struct command_line
*current
;
1076 struct cleanup
*old_chain
= 0;
1080 enum command_control_type ret
;
1083 switch (cmd
->control_type
)
1085 case simple_control
:
1086 /* A simple command, execute it and return. */
1087 new_line
= insert_args (cmd
->line
);
1089 return invalid_control
;
1090 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
1092 execute_command (new_line
, 0);
1093 ret
= cmd
->control_type
;
1096 case continue_control
:
1098 /* Return for "continue", and "break" so we can either
1099 continue the loop at the top, or break out. */
1100 ret
= cmd
->control_type
;
1105 /* Parse the loop control expression for the while statement. */
1106 new_line
= insert_args (cmd
->line
);
1108 return invalid_control
;
1109 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
1111 expr
= parse_expression (new_line
);
1112 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
1114 ret
= simple_control
;
1117 /* Keep iterating so long as the expression is true. */
1124 /* Evaluate the expression. */
1125 val_mark
= value_mark ();
1126 val
= evaluate_expression (expr
);
1127 cond_result
= value_true (val
);
1128 value_free_to_mark (val_mark
);
1130 /* If the value is false, then break out of the loop. */
1134 /* Execute the body of the while statement. */
1135 current
= *cmd
->body_list
;
1138 ret
= execute_control_command (current
);
1140 /* If we got an error, or a "break" command, then stop
1142 if (ret
== invalid_control
|| ret
== break_control
)
1148 /* If we got a "continue" command, then restart the loop
1150 if (ret
== continue_control
)
1153 /* Get the next statement. */
1154 current
= current
->next
;
1158 /* Reset RET so that we don't recurse the break all the way down. */
1159 if (ret
== break_control
)
1160 ret
= simple_control
;
1167 new_line
= insert_args (cmd
->line
);
1169 return invalid_control
;
1170 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
1172 /* Parse the conditional for the if statement. */
1173 expr
= parse_expression (new_line
);
1174 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
1177 ret
= simple_control
;
1179 /* Evaluate the conditional. */
1180 val_mark
= value_mark ();
1181 val
= evaluate_expression (expr
);
1183 /* Choose which arm to take commands from based on the value of the
1184 conditional expression. */
1185 if (value_true (val
))
1186 current
= *cmd
->body_list
;
1187 else if (cmd
->body_count
== 2)
1188 current
= *(cmd
->body_list
+ 1);
1189 value_free_to_mark (val_mark
);
1191 /* Execute commands in the given arm. */
1194 ret
= execute_control_command (current
);
1196 /* If we got an error, get out. */
1197 if (ret
!= simple_control
)
1200 /* Get the next statement in the body. */
1201 current
= current
->next
;
1208 warning ("Invalid control type in command structure.");
1209 return invalid_control
;
1213 do_cleanups (old_chain
);
1218 /* "while" command support. Executes a body of statements while the
1219 loop condition is nonzero. */
1222 while_command (arg
, from_tty
)
1226 struct command_line
*command
= NULL
;
1229 command
= get_command_line (while_control
, arg
);
1231 if (command
== NULL
)
1234 execute_control_command (command
);
1235 free_command_lines (&command
);
1238 /* "if" command support. Execute either the true or false arm depending
1239 on the value of the if conditional. */
1242 if_command (arg
, from_tty
)
1246 struct command_line
*command
= NULL
;
1249 command
= get_command_line (if_control
, arg
);
1251 if (command
== NULL
)
1254 execute_control_command (command
);
1255 free_command_lines (&command
);
1262 struct user_args
*oargs
= user_args
;
1264 internal_error ("Internal error, arg_cleanup called with no user args.\n");
1266 user_args
= user_args
->next
;
1270 /* Bind the incomming arguments for a user defined command to
1271 $arg0, $arg1 ... $argMAXUSERARGS. */
1273 static struct cleanup
*
1277 struct user_args
*args
;
1278 struct cleanup
*old_chain
;
1279 unsigned int arg_count
= 0;
1281 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
1282 memset (args
, 0, sizeof (struct user_args
));
1284 args
->next
= user_args
;
1287 old_chain
= make_cleanup ((make_cleanup_func
) arg_cleanup
, 0);
1299 if (arg_count
>= MAXUSERARGS
)
1301 error ("user defined function may only have %d arguments.\n",
1306 /* Strip whitespace. */
1307 while (*p
== ' ' || *p
== '\t')
1310 /* P now points to an argument. */
1312 user_args
->a
[arg_count
].arg
= p
;
1314 /* Get to the end of this argument. */
1317 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
1323 else if (*p
== '\\')
1346 user_args
->a
[arg_count
].len
= p
- start_arg
;
1353 /* Given character string P, return a point to the first argument ($arg),
1354 or NULL if P contains no arguments. */
1360 while ((p
= strchr (p
, '$')))
1362 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1369 /* Insert the user defined arguments stored in user_arg into the $arg
1370 arguments found in line, with the updated copy being placed into nline. */
1376 char *p
, *save_line
, *new_line
;
1379 /* First we need to know how much memory to allocate for the new line. */
1382 while ((p
= locate_arg (line
)))
1387 if (i
>= user_args
->count
)
1389 error ("Missing argument %d in user function.\n", i
);
1392 len
+= user_args
->a
[i
].len
;
1396 /* Don't forget the tail. */
1397 len
+= strlen (line
);
1399 /* Allocate space for the new line and fill it in. */
1400 new_line
= (char *) xmalloc (len
+ 1);
1401 if (new_line
== NULL
)
1404 /* Restore pointer to beginning of old line. */
1407 /* Save pointer to beginning of new line. */
1408 save_line
= new_line
;
1410 while ((p
= locate_arg (line
)))
1414 memcpy (new_line
, line
, p
- line
);
1415 new_line
+= p
- line
;
1418 len
= user_args
->a
[i
].len
;
1421 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1426 /* Don't forget the tail. */
1427 strcpy (new_line
, line
);
1429 /* Return a pointer to the beginning of the new line. */
1434 execute_user_command (c
, args
)
1435 struct cmd_list_element
*c
;
1438 register struct command_line
*cmdlines
;
1439 struct cleanup
*old_chain
;
1440 enum command_control_type ret
;
1442 old_chain
= setup_user_args (args
);
1444 cmdlines
= c
->user_commands
;
1449 /* Set the instream to 0, indicating execution of a
1450 user-defined function. */
1451 old_chain
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
1452 instream
= (FILE *) 0;
1455 ret
= execute_control_command (cmdlines
);
1456 if (ret
!= simple_control
&& ret
!= break_control
)
1458 warning ("Error in control structure.\n");
1461 cmdlines
= cmdlines
->next
;
1463 do_cleanups (old_chain
);
1466 /* Execute the line P as a command.
1467 Pass FROM_TTY as second argument to the defining function. */
1470 execute_command (p
, from_tty
)
1474 register struct cmd_list_element
*c
;
1475 register enum language flang
;
1476 static int warned
= 0;
1478 /* FIXME: These should really be in an appropriate header file */
1479 extern void serial_log_command
PARAMS ((const char *));
1483 /* Force cleanup of any alloca areas if using C alloca instead of
1484 a builtin alloca. */
1487 /* This can happen when command_line_input hits end of file. */
1491 serial_log_command (p
);
1493 while (*p
== ' ' || *p
== '\t')
1500 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1502 /* If the target is running, we allow only a limited set of
1504 if (event_loop_p
&& target_can_async_p () && target_executing
)
1505 if (!strcmp (c
->name
, "help")
1506 && !strcmp (c
->name
, "pwd")
1507 && !strcmp (c
->name
, "show")
1508 && !strcmp (c
->name
, "stop"))
1509 error ("Cannot execute this command while the target is running.");
1511 /* Pass null arg rather than an empty one. */
1514 /* Clear off trailing whitespace, except for set and complete command. */
1515 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1517 p
= arg
+ strlen (arg
) - 1;
1518 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1523 /* If this command has been hooked, run the hook first. */
1525 execute_user_command (c
->hook
, (char *) 0);
1527 if (c
->flags
& DEPRECATED_WARN_USER
)
1528 deprecated_cmd_warning (&line
);
1530 if (c
->class == class_user
)
1531 execute_user_command (c
, arg
);
1532 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1533 do_setshow_command (arg
, from_tty
& caution
, c
);
1534 else if (c
->function
.cfunc
== NO_FUNCTION
)
1535 error ("That is not a command, just a help topic.");
1536 else if (call_command_hook
)
1537 call_command_hook (c
, arg
, from_tty
& caution
);
1539 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1542 /* Tell the user if the language has changed (except first time). */
1543 if (current_language
!= expected_language
)
1545 if (language_mode
== language_mode_auto
)
1547 language_info (1); /* Print what changed. */
1552 /* Warn the user if the working language does not match the
1553 language of the current frame. Only warn the user if we are
1554 actually running the program, i.e. there is a stack. */
1555 /* FIXME: This should be cacheing the frame and only running when
1556 the frame changes. */
1558 if (target_has_stack
)
1560 flang
= get_frame_language ();
1562 && flang
!= language_unknown
1563 && flang
!= current_language
->la_language
)
1565 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1572 /* NOTE 1999-04-29: This function will be static again, once we modify
1573 gdb to use the event loop as the default command loop and we merge
1574 event-top.c into this file, top.c */
1576 command_loop_marker (foo
)
1581 /* Read commands from `instream' and execute them
1582 until end of file or error reading instream. */
1587 struct cleanup
*old_chain
;
1589 int stdin_is_tty
= ISATTY (stdin
);
1590 long time_at_cmd_start
;
1592 long space_at_cmd_start
= 0;
1594 extern int display_time
;
1595 extern int display_space
;
1597 while (instream
&& !feof (instream
))
1600 extern int insert_mode
;
1602 if (window_hook
&& instream
== stdin
)
1603 (*window_hook
) (instream
, get_prompt ());
1606 if (instream
== stdin
&& stdin_is_tty
)
1607 reinitialize_more_filter ();
1608 old_chain
= make_cleanup ((make_cleanup_func
) command_loop_marker
, 0);
1611 /* A bit of paranoia: I want to make sure the "insert_mode" global
1612 * is clear except when it is being used for command-line editing
1613 * (see tuiIO.c, utils.c); otherwise normal output will
1614 * get messed up in the TUI. So clear it before/after
1615 * the command-line-input call. - RT
1619 /* Get a command-line. This calls the readline package. */
1620 command
= command_line_input (instream
== stdin
?
1621 get_prompt () : (char *) NULL
,
1622 instream
== stdin
, "prompt");
1629 time_at_cmd_start
= get_run_time ();
1634 extern char **environ
;
1635 char *lim
= (char *) sbrk (0);
1637 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1641 execute_command (command
, instream
== stdin
);
1642 /* Do any commands attached to breakpoint we stopped at. */
1643 bpstat_do_actions (&stop_bpstat
);
1644 do_cleanups (old_chain
);
1648 long cmd_time
= get_run_time () - time_at_cmd_start
;
1650 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1651 cmd_time
/ 1000000, cmd_time
% 1000000);
1657 extern char **environ
;
1658 char *lim
= (char *) sbrk (0);
1659 long space_now
= lim
- (char *) &environ
;
1660 long space_diff
= space_now
- space_at_cmd_start
;
1662 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1664 (space_diff
>= 0 ? '+' : '-'),
1671 /* Read commands from `instream' and execute them until end of file or
1672 error reading instream. This command loop doesnt care about any
1673 such things as displaying time and space usage. If the user asks
1674 for those, they won't work. */
1676 simplified_command_loop (read_input_func
, execute_command_func
)
1677 char *(*read_input_func
) (char *);
1678 void (*execute_command_func
) (char *, int);
1680 struct cleanup
*old_chain
;
1682 int stdin_is_tty
= ISATTY (stdin
);
1684 while (instream
&& !feof (instream
))
1687 if (instream
== stdin
&& stdin_is_tty
)
1688 reinitialize_more_filter ();
1689 old_chain
= make_cleanup ((make_cleanup_func
) command_loop_marker
, 0);
1691 /* Get a command-line. */
1692 command
= (*read_input_func
) (instream
== stdin
?
1693 get_prompt () : (char *) NULL
);
1698 (*execute_command_func
) (command
, instream
== stdin
);
1700 /* Do any commands attached to breakpoint we stopped at. */
1701 bpstat_do_actions (&stop_bpstat
);
1703 do_cleanups (old_chain
);
1707 /* Commands call this if they do not want to be repeated by null lines. */
1715 /* If we aren't reading from standard input, we are saving the last
1716 thing read from stdin in line and don't want to delete it. Null lines
1717 won't repeat here in any case. */
1718 if (instream
== stdin
)
1722 /* Read a line from the stream "instream" without command line editing.
1724 It prints PROMPT_ARG once at the start.
1725 Action is compatible with "readline", e.g. space for the result is
1726 malloc'd and should be freed by the caller.
1728 A NULL return means end of file. */
1730 gdb_readline (prompt_arg
)
1735 int input_index
= 0;
1736 int result_size
= 80;
1740 /* Don't use a _filtered function here. It causes the assumed
1741 character position to be off, since the newline we read from
1742 the user is not accounted for. */
1743 fputs_unfiltered (prompt_arg
, gdb_stdout
);
1745 /* Move to a new line so the entered line doesn't have a prompt
1746 on the front of it. */
1747 fputs_unfiltered ("\n", gdb_stdout
);
1749 gdb_flush (gdb_stdout
);
1752 result
= (char *) xmalloc (result_size
);
1756 /* Read from stdin if we are executing a user defined command.
1757 This is the right thing for prompt_for_continue, at least. */
1758 c
= fgetc (instream
? instream
: stdin
);
1762 if (input_index
> 0)
1763 /* The last line does not end with a newline. Return it, and
1764 if we are called again fgetc will still return EOF and
1765 we'll return NULL then. */
1772 #ifndef CRLF_SOURCE_FILES
1776 if (input_index
> 0 && result
[input_index
- 1] == '\r')
1782 result
[input_index
++] = c
;
1783 while (input_index
>= result_size
)
1786 result
= (char *) xrealloc (result
, result_size
);
1790 result
[input_index
++] = '\0';
1794 /* Variables which control command line editing and history
1795 substitution. These variables are given default values at the end
1797 static int command_editing_p
;
1798 /* NOTE 1999-04-29: This variable will be static again, once we modify
1799 gdb to use the event loop as the default command loop and we merge
1800 event-top.c into this file, top.c */
1801 /* static */ int history_expansion_p
;
1802 static int write_history_p
;
1803 static int history_size
;
1804 static char *history_filename
;
1806 /* readline uses the word breaks for two things:
1807 (1) In figuring out where to point the TEXT parameter to the
1808 rl_completion_entry_function. Since we don't use TEXT for much,
1809 it doesn't matter a lot what the word breaks are for this purpose, but
1810 it does affect how much stuff M-? lists.
1811 (2) If one of the matches contains a word break character, readline
1812 will quote it. That's why we switch between
1813 gdb_completer_word_break_characters and
1814 gdb_completer_command_word_break_characters. I'm not sure when
1815 we need this behavior (perhaps for funky characters in C++ symbols?). */
1817 /* Variables which are necessary for fancy command line editing. */
1818 char *gdb_completer_word_break_characters
=
1819 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1821 /* When completing on command names, we remove '-' from the list of
1822 word break characters, since we use it in command names. If the
1823 readline library sees one in any of the current completion strings,
1824 it thinks that the string needs to be quoted and automatically supplies
1826 char *gdb_completer_command_word_break_characters
=
1827 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1829 /* Characters that can be used to quote completion strings. Note that we
1830 can't include '"' because the gdb C parser treats such quoted sequences
1832 char *gdb_completer_quote_characters
=
1835 /* Functions that are used as part of the fancy command line editing. */
1837 /* This can be used for functions which don't want to complete on symbols
1838 but don't want to complete on anything else either. */
1841 noop_completer (text
, prefix
)
1848 /* Complete on filenames. */
1850 filename_completer (text
, word
)
1854 /* From readline. */
1855 extern char *filename_completion_function
PARAMS ((char *, int));
1856 int subsequent_name
;
1858 int return_val_used
;
1859 int return_val_alloced
;
1861 return_val_used
= 0;
1862 /* Small for testing. */
1863 return_val_alloced
= 1;
1864 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1866 subsequent_name
= 0;
1870 p
= filename_completion_function (text
, subsequent_name
);
1871 if (return_val_used
>= return_val_alloced
)
1873 return_val_alloced
*= 2;
1875 (char **) xrealloc (return_val
,
1876 return_val_alloced
* sizeof (char *));
1880 return_val
[return_val_used
++] = p
;
1883 /* Like emacs, don't complete on old versions. Especially useful
1884 in the "source" command. */
1885 if (p
[strlen (p
) - 1] == '~')
1891 /* Return exactly p. */
1892 return_val
[return_val_used
++] = p
;
1893 else if (word
> text
)
1895 /* Return some portion of p. */
1896 q
= xmalloc (strlen (p
) + 5);
1897 strcpy (q
, p
+ (word
- text
));
1898 return_val
[return_val_used
++] = q
;
1903 /* Return some of TEXT plus p. */
1904 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1905 strncpy (q
, word
, text
- word
);
1906 q
[text
- word
] = '\0';
1908 return_val
[return_val_used
++] = q
;
1912 subsequent_name
= 1;
1915 /* There is no way to do this just long enough to affect quote inserting
1916 without also affecting the next completion. This should be fixed in
1918 /* Insure that readline does the right thing
1919 with respect to inserting quotes. */
1920 rl_completer_word_break_characters
= "";
1925 /* Here are some useful test cases for completion. FIXME: These should
1926 be put in the test suite. They should be tested with both M-? and TAB.
1928 "show output-" "radix"
1929 "show output" "-radix"
1930 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1931 "p " ambiguous (all symbols)
1932 "info t foo" no completions
1933 "info t " no completions
1934 "info t" ambiguous ("info target", "info terminal", etc.)
1935 "info ajksdlfk" no completions
1936 "info ajksdlfk " no completions
1938 "info " ambiguous (all info commands)
1939 "p \"a" no completions (string constant)
1940 "p 'a" ambiguous (all symbols starting with a)
1941 "p b-a" ambiguous (all symbols starting with a)
1942 "p b-" ambiguous (all symbols)
1943 "file Make" "file" (word break hard to screw up here)
1944 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1947 /* Generate completions one by one for the completer. Each time we are
1948 called return another potential completion to the caller.
1949 line_completion just completes on commands or passes the buck to the
1950 command's completer function, the stuff specific to symbol completion
1951 is in make_symbol_completion_list.
1953 TEXT is the caller's idea of the "word" we are looking at.
1955 MATCHES is the number of matches that have currently been collected from
1956 calling this completion function. When zero, then we need to initialize,
1957 otherwise the initialization has already taken place and we can just
1958 return the next potential completion string.
1960 LINE_BUFFER is available to be looked at; it contains the entire text
1961 of the line. POINT is the offset in that line of the cursor. You
1962 should pretend that the line ends at POINT.
1964 Returns NULL if there are no more completions, else a pointer to a string
1965 which is a possible completion, it is the caller's responsibility to
1969 line_completion_function (text
, matches
, line_buffer
, point
)
1975 static char **list
= (char **) NULL
; /* Cache of completions */
1976 static int index
; /* Next cached completion */
1977 char *output
= NULL
;
1978 char *tmp_command
, *p
;
1979 /* Pointer within tmp_command which corresponds to text. */
1981 struct cmd_list_element
*c
, *result_list
;
1985 /* The caller is beginning to accumulate a new set of completions, so
1986 we need to find all of them now, and cache them for returning one at
1987 a time on future calls. */
1991 /* Free the storage used by LIST, but not by the strings inside.
1992 This is because rl_complete_internal () frees the strings. */
1998 /* Choose the default set of word break characters to break completions.
1999 If we later find out that we are doing completions on command strings
2000 (as opposed to strings supplied by the individual command completer
2001 functions, which can be any string) then we will switch to the
2002 special word break set for command strings, which leaves out the
2003 '-' character used in some commands. */
2005 rl_completer_word_break_characters
=
2006 gdb_completer_word_break_characters
;
2008 /* Decide whether to complete on a list of gdb commands or on symbols. */
2009 tmp_command
= (char *) alloca (point
+ 1);
2012 strncpy (tmp_command
, line_buffer
, point
);
2013 tmp_command
[point
] = '\0';
2014 /* Since text always contains some number of characters leading up
2015 to point, we can find the equivalent position in tmp_command
2016 by subtracting that many characters from the end of tmp_command. */
2017 word
= tmp_command
+ point
- strlen (text
);
2021 /* An empty line we want to consider ambiguous; that is, it
2022 could be any command. */
2023 c
= (struct cmd_list_element
*) -1;
2028 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
2031 /* Move p up to the next interesting thing. */
2032 while (*p
== ' ' || *p
== '\t')
2039 /* It is an unrecognized command. So there are no
2040 possible completions. */
2043 else if (c
== (struct cmd_list_element
*) -1)
2047 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
2048 doesn't advance over that thing itself. Do so now. */
2050 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
2052 if (q
!= tmp_command
+ point
)
2054 /* There is something beyond the ambiguous
2055 command, so there are no possible completions. For
2056 example, "info t " or "info t foo" does not complete
2057 to anything, because "info t" can be "info target" or
2063 /* We're trying to complete on the command which was ambiguous.
2064 This we can deal with. */
2067 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
2072 list
= complete_on_cmdlist (cmdlist
, p
, word
);
2074 /* Insure that readline does the right thing with respect to
2075 inserting quotes. */
2076 rl_completer_word_break_characters
=
2077 gdb_completer_command_word_break_characters
;
2082 /* We've recognized a full command. */
2084 if (p
== tmp_command
+ point
)
2086 /* There is no non-whitespace in the line beyond the command. */
2088 if (p
[-1] == ' ' || p
[-1] == '\t')
2090 /* The command is followed by whitespace; we need to complete
2091 on whatever comes after command. */
2094 /* It is a prefix command; what comes after it is
2095 a subcommand (e.g. "info "). */
2096 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
2098 /* Insure that readline does the right thing
2099 with respect to inserting quotes. */
2100 rl_completer_word_break_characters
=
2101 gdb_completer_command_word_break_characters
;
2105 list
= complete_on_enum (c
->enums
, p
, word
);
2106 rl_completer_word_break_characters
=
2107 gdb_completer_command_word_break_characters
;
2111 /* It is a normal command; what comes after it is
2112 completed by the command's completer function. */
2113 list
= (*c
->completer
) (p
, word
);
2118 /* The command is not followed by whitespace; we need to
2119 complete on the command itself. e.g. "p" which is a
2120 command itself but also can complete to "print", "ptype"
2124 /* Find the command we are completing on. */
2126 while (q
> tmp_command
)
2128 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
2134 list
= complete_on_cmdlist (result_list
, q
, word
);
2136 /* Insure that readline does the right thing
2137 with respect to inserting quotes. */
2138 rl_completer_word_break_characters
=
2139 gdb_completer_command_word_break_characters
;
2144 /* There is non-whitespace beyond the command. */
2146 if (c
->prefixlist
&& !c
->allow_unknown
)
2148 /* It is an unrecognized subcommand of a prefix command,
2149 e.g. "info adsfkdj". */
2154 list
= complete_on_enum (c
->enums
, p
, word
);
2158 /* It is a normal command. */
2159 list
= (*c
->completer
) (p
, word
);
2165 /* If we found a list of potential completions during initialization then
2166 dole them out one at a time. The vector of completions is NULL
2167 terminated, so after returning the last one, return NULL (and continue
2168 to do so) each time we are called after that, until a new list is
2173 output
= list
[index
];
2181 /* Can't do this because readline hasn't yet checked the word breaks
2182 for figuring out whether to insert a quote. */
2184 /* Make sure the word break characters are set back to normal for the
2185 next time that readline tries to complete something. */
2186 rl_completer_word_break_characters
=
2187 gdb_completer_word_break_characters
;
2193 /* Line completion interface function for readline. */
2196 readline_line_completion_function (text
, matches
)
2200 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
2203 /* Skip over a possibly quoted word (as defined by the quote characters
2204 and word break characters the completer uses). Returns pointer to the
2205 location after the "word". */
2211 char quote_char
= '\0';
2214 for (scan
= str
; *scan
!= '\0'; scan
++)
2216 if (quote_char
!= '\0')
2218 /* Ignore everything until the matching close quote char */
2219 if (*scan
== quote_char
)
2221 /* Found matching close quote. */
2226 else if (strchr (gdb_completer_quote_characters
, *scan
))
2228 /* Found start of a quoted string. */
2231 else if (strchr (gdb_completer_word_break_characters
, *scan
))
2245 #if STOP_SIGNAL == SIGTSTP
2246 signal (SIGTSTP
, SIG_DFL
);
2248 kill (getpid (), SIGTSTP
);
2249 signal (SIGTSTP
, stop_sig
);
2251 signal (STOP_SIGNAL
, stop_sig
);
2253 printf_unfiltered ("%s", get_prompt ());
2254 gdb_flush (gdb_stdout
);
2256 /* Forget about any previous command -- null line now will do nothing. */
2259 #endif /* STOP_SIGNAL */
2261 /* Initialize signal handlers. */
2266 /* Under System V the default disposition of a signal is reinstated after
2267 the signal is caught and delivered to an application process. On such
2268 systems one must restore the replacement signal handler if one wishes
2269 to continue handling the signal in one's program. On BSD systems this
2270 is not needed but it is harmless, and it simplifies the code to just do
2271 it unconditionally. */
2272 signal (signo
, do_nothing
);
2278 signal (SIGINT
, request_quit
);
2280 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2281 to the inferior and breakpoints will be ignored. */
2283 signal (SIGTRAP
, SIG_DFL
);
2286 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2287 passed to the inferior, which we don't want. It would be
2288 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2289 on BSD4.3 systems using vfork, that can affect the
2290 GDB process as well as the inferior (the signal handling tables
2291 might be in memory, shared between the two). Since we establish
2292 a handler for SIGQUIT, when we call exec it will set the signal
2293 to SIG_DFL for us. */
2294 signal (SIGQUIT
, do_nothing
);
2296 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
2297 signal (SIGHUP
, disconnect
);
2299 signal (SIGFPE
, float_handler
);
2301 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2302 signal (SIGWINCH
, SIGWINCH_HANDLER
);
2306 /* Read one line from the command input stream `instream'
2307 into the local static buffer `linebuffer' (whose current length
2309 The buffer is made bigger as necessary.
2310 Returns the address of the start of the line.
2312 NULL is returned for end of file.
2314 *If* the instream == stdin & stdin is a terminal, the line read
2315 is copied into the file line saver (global var char *line,
2316 length linesize) so that it can be duplicated.
2318 This routine either uses fancy command line editing or
2319 simple input as the user has requested. */
2322 command_line_input (prompt_arg
, repeat
, annotation_suffix
)
2325 char *annotation_suffix
;
2327 static char *linebuffer
= 0;
2328 static unsigned linelength
= 0;
2332 char *local_prompt
= prompt_arg
;
2336 /* The annotation suffix must be non-NULL. */
2337 if (annotation_suffix
== NULL
)
2338 annotation_suffix
= "";
2340 if (annotation_level
> 1 && instream
== stdin
)
2342 local_prompt
= alloca ((prompt_arg
== NULL
? 0 : strlen (prompt_arg
))
2343 + strlen (annotation_suffix
) + 40);
2344 if (prompt_arg
== NULL
)
2345 local_prompt
[0] = '\0';
2347 strcpy (local_prompt
, prompt_arg
);
2348 strcat (local_prompt
, "\n\032\032");
2349 strcat (local_prompt
, annotation_suffix
);
2350 strcat (local_prompt
, "\n");
2353 if (linebuffer
== 0)
2356 linebuffer
= (char *) xmalloc (linelength
);
2361 /* Control-C quits instantly if typed while in this loop
2362 since it should not wait until the user types a newline. */
2368 signal (STOP_SIGNAL
, handle_stop_sig
);
2370 signal (STOP_SIGNAL
, stop_sig
);
2376 /* Make sure that all output has been output. Some machines may let
2377 you get away with leaving out some of the gdb_flush, but not all. */
2379 gdb_flush (gdb_stdout
);
2380 gdb_flush (gdb_stderr
);
2382 if (source_file_name
!= NULL
)
2384 ++source_line_number
;
2385 sprintf (source_error
,
2386 "%s%s:%d: Error in sourced command file:\n",
2389 source_line_number
);
2390 error_pre_print
= source_error
;
2393 if (annotation_level
> 1 && instream
== stdin
)
2395 printf_unfiltered ("\n\032\032pre-");
2396 printf_unfiltered (annotation_suffix
);
2397 printf_unfiltered ("\n");
2400 /* Don't use fancy stuff if not talking to stdin. */
2401 if (readline_hook
&& instream
== NULL
)
2403 rl
= (*readline_hook
) (local_prompt
);
2405 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
2407 rl
= readline (local_prompt
);
2411 rl
= gdb_readline (local_prompt
);
2414 if (annotation_level
> 1 && instream
== stdin
)
2416 printf_unfiltered ("\n\032\032post-");
2417 printf_unfiltered (annotation_suffix
);
2418 printf_unfiltered ("\n");
2421 if (!rl
|| rl
== (char *) EOF
)
2426 if (strlen (rl
) + 1 + (p
- linebuffer
) > linelength
)
2428 linelength
= strlen (rl
) + 1 + (p
- linebuffer
);
2429 nline
= (char *) xrealloc (linebuffer
, linelength
);
2430 p
+= nline
- linebuffer
;
2434 /* Copy line. Don't copy null at end. (Leaves line alone
2435 if this was just a newline) */
2439 free (rl
); /* Allocated in readline. */
2441 if (p
== linebuffer
|| *(p
- 1) != '\\')
2444 p
--; /* Put on top of '\'. */
2445 local_prompt
= (char *) 0;
2450 signal (STOP_SIGNAL
, SIG_DFL
);
2457 #define SERVER_COMMAND_LENGTH 7
2459 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2460 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2463 /* Note that we don't set `line'. Between this and the check in
2464 dont_repeat, this insures that repeating will still do the
2467 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2470 /* Do history expansion if that is wished. */
2471 if (history_expansion_p
&& instream
== stdin
2472 && ISATTY (instream
))
2474 char *history_value
;
2477 *p
= '\0'; /* Insert null now. */
2478 expanded
= history_expand (linebuffer
, &history_value
);
2481 /* Print the changes. */
2482 printf_unfiltered ("%s\n", history_value
);
2484 /* If there was an error, call this function again. */
2487 free (history_value
);
2488 return command_line_input (prompt_arg
, repeat
, annotation_suffix
);
2490 if (strlen (history_value
) > linelength
)
2492 linelength
= strlen (history_value
) + 1;
2493 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2495 strcpy (linebuffer
, history_value
);
2496 p
= linebuffer
+ strlen (linebuffer
);
2497 free (history_value
);
2501 /* If we just got an empty line, and that is supposed
2502 to repeat the previous command, return the value in the
2504 if (repeat
&& p
== linebuffer
)
2506 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++);
2512 /* Add line to history if appropriate. */
2513 if (instream
== stdin
2514 && ISATTY (stdin
) && *linebuffer
)
2515 add_history (linebuffer
);
2517 /* Note: lines consisting solely of comments are added to the command
2518 history. This is useful when you type a command, and then
2519 realize you don't want to execute it quite yet. You can comment
2520 out the command and then later fetch it from the value history
2521 and remove the '#'. The kill ring is probably better, but some
2522 people are in the habit of commenting things out. */
2524 *p1
= '\0'; /* Found a comment. */
2526 /* Save into global buffer if appropriate. */
2529 if (linelength
> linesize
)
2531 line
= xrealloc (line
, linelength
);
2532 linesize
= linelength
;
2534 strcpy (line
, linebuffer
);
2542 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2543 code bodies. This is typically used when we encounter an "else"
2544 clause for an "if" command. */
2547 realloc_body_list (command
, new_length
)
2548 struct command_line
*command
;
2552 struct command_line
**body_list
;
2554 n
= command
->body_count
;
2556 /* Nothing to do? */
2557 if (new_length
<= n
)
2560 body_list
= (struct command_line
**)
2561 xmalloc (sizeof (struct command_line
*) * new_length
);
2563 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2565 free (command
->body_list
);
2566 command
->body_list
= body_list
;
2567 command
->body_count
= new_length
;
2570 /* Read one line from the input stream. If the command is an "else" or
2571 "end", return such an indication to the caller. */
2573 static enum misc_command_type
2574 read_next_line (command
)
2575 struct command_line
**command
;
2577 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2580 if (control_level
>= 254)
2581 error ("Control nesting too deep!\n");
2583 /* Set a prompt based on the nesting of the control commands. */
2584 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2586 for (i
= 0; i
< control_level
; i
++)
2587 control_prompt
[i
] = ' ';
2588 control_prompt
[i
] = '>';
2589 control_prompt
[i
+ 1] = '\0';
2590 prompt_ptr
= (char *) &control_prompt
[0];
2595 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2597 /* Not sure what to do here. */
2601 /* Strip leading and trailing whitespace. */
2602 while (*p
== ' ' || *p
== '\t')
2605 p1
= p
+ strlen (p
);
2606 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2609 /* Blanks and comments don't really do anything, but we need to
2610 distinguish them from else, end and other commands which can be
2612 if (p1
== p
|| p
[0] == '#')
2615 /* Is this the end of a simple, while, or if control structure? */
2616 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2619 /* Is the else clause of an if control structure? */
2620 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2621 return else_command
;
2623 /* Check for while, if, break, continue, etc and build a new command
2624 line structure for them. */
2625 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2626 *command
= build_command_line (while_control
, p
+ 6);
2627 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2628 *command
= build_command_line (if_control
, p
+ 3);
2629 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2631 *command
= (struct command_line
*)
2632 xmalloc (sizeof (struct command_line
));
2633 (*command
)->next
= NULL
;
2634 (*command
)->line
= NULL
;
2635 (*command
)->control_type
= break_control
;
2636 (*command
)->body_count
= 0;
2637 (*command
)->body_list
= NULL
;
2639 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2641 *command
= (struct command_line
*)
2642 xmalloc (sizeof (struct command_line
));
2643 (*command
)->next
= NULL
;
2644 (*command
)->line
= NULL
;
2645 (*command
)->control_type
= continue_control
;
2646 (*command
)->body_count
= 0;
2647 (*command
)->body_list
= NULL
;
2651 /* A normal command. */
2652 *command
= (struct command_line
*)
2653 xmalloc (sizeof (struct command_line
));
2654 (*command
)->next
= NULL
;
2655 (*command
)->line
= savestring (p
, p1
- p
);
2656 (*command
)->control_type
= simple_control
;
2657 (*command
)->body_count
= 0;
2658 (*command
)->body_list
= NULL
;
2661 /* Nothing special. */
2665 /* Recursively read in the control structures and create a command_line
2666 structure from them.
2668 The parent_control parameter is the control structure in which the
2669 following commands are nested. */
2671 static enum command_control_type
2672 recurse_read_control_structure (current_cmd
)
2673 struct command_line
*current_cmd
;
2675 int current_body
, i
;
2676 enum misc_command_type val
;
2677 enum command_control_type ret
;
2678 struct command_line
**body_ptr
, *child_tail
, *next
;
2683 /* Sanity checks. */
2684 if (current_cmd
->control_type
== simple_control
)
2686 error ("Recursed on a simple control type\n");
2687 return invalid_control
;
2690 if (current_body
> current_cmd
->body_count
)
2692 error ("Allocated body is smaller than this command type needs\n");
2693 return invalid_control
;
2696 /* Read lines from the input stream and build control structures. */
2702 val
= read_next_line (&next
);
2704 /* Just skip blanks and comments. */
2705 if (val
== nop_command
)
2708 if (val
== end_command
)
2710 if (current_cmd
->control_type
== while_control
2711 || current_cmd
->control_type
== if_control
)
2713 /* Success reading an entire control structure. */
2714 ret
= simple_control
;
2719 ret
= invalid_control
;
2724 /* Not the end of a control structure. */
2725 if (val
== else_command
)
2727 if (current_cmd
->control_type
== if_control
2728 && current_body
== 1)
2730 realloc_body_list (current_cmd
, 2);
2737 ret
= invalid_control
;
2744 child_tail
->next
= next
;
2748 body_ptr
= current_cmd
->body_list
;
2749 for (i
= 1; i
< current_body
; i
++)
2758 /* If the latest line is another control structure, then recurse
2760 if (next
->control_type
== while_control
2761 || next
->control_type
== if_control
)
2764 ret
= recurse_read_control_structure (next
);
2767 if (ret
!= simple_control
)
2777 /* Read lines from the input stream and accumulate them in a chain of
2778 struct command_line's, which is then returned. For input from a
2779 terminal, the special command "end" is used to mark the end of the
2780 input, and is not included in the returned chain of commands. */
2782 #define END_MESSAGE "End with a line saying just \"end\"."
2784 struct command_line
*
2785 read_command_lines (prompt_arg
, from_tty
)
2789 struct command_line
*head
, *tail
, *next
;
2790 struct cleanup
*old_chain
;
2791 enum command_control_type ret
;
2792 enum misc_command_type val
;
2795 if (readline_begin_hook
)
2797 /* Note - intentional to merge messages with no newline */
2798 (*readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
2800 else if (from_tty
&& input_from_terminal_p ())
2802 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
2803 gdb_flush (gdb_stdout
);
2811 val
= read_next_line (&next
);
2813 /* Ignore blank lines or comments. */
2814 if (val
== nop_command
)
2817 if (val
== end_command
)
2819 ret
= simple_control
;
2823 if (val
!= ok_command
)
2825 ret
= invalid_control
;
2829 if (next
->control_type
== while_control
2830 || next
->control_type
== if_control
)
2833 ret
= recurse_read_control_structure (next
);
2836 if (ret
== invalid_control
)
2847 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
,
2857 if (ret
!= invalid_control
)
2859 discard_cleanups (old_chain
);
2862 do_cleanups (old_chain
);
2865 if (readline_end_hook
)
2867 (*readline_end_hook
) ();
2872 /* Free a chain of struct command_line's. */
2875 free_command_lines (lptr
)
2876 struct command_line
**lptr
;
2878 register struct command_line
*l
= *lptr
;
2879 register struct command_line
*next
;
2880 struct command_line
**blist
;
2885 if (l
->body_count
> 0)
2887 blist
= l
->body_list
;
2888 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2889 free_command_lines (blist
);
2898 /* Add an element to the list of info subcommands. */
2900 struct cmd_list_element
*
2901 add_info (name
, fun
, doc
)
2903 void (*fun
) PARAMS ((char *, int));
2906 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2909 /* Add an alias to the list of info subcommands. */
2911 struct cmd_list_element
*
2912 add_info_alias (name
, oldname
, abbrev_flag
)
2917 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2920 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2921 Therefore, its own definition is called only for "info" with no args. */
2925 info_command (arg
, from_tty
)
2929 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2930 help_list (infolist
, "info ", -1, gdb_stdout
);
2933 /* The "complete" command is used by Emacs to implement completion. */
2937 complete_command (arg
, from_tty
)
2949 argpoint
= strlen (arg
);
2951 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2953 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2955 printf_unfiltered ("%s\n", completion
);
2960 /* The "show" command with no arguments shows all the settings. */
2964 show_command (arg
, from_tty
)
2968 cmd_show_list (showlist
, from_tty
, "");
2971 /* Add an element to the list of commands. */
2973 struct cmd_list_element
*
2974 add_com (name
, class, fun
, doc
)
2976 enum command_class
class;
2977 void (*fun
) PARAMS ((char *, int));
2980 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
2983 /* Add an alias or abbreviation command to the list of commands. */
2985 struct cmd_list_element
*
2986 add_com_alias (name
, oldname
, class, abbrev_flag
)
2989 enum command_class
class;
2992 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2999 error ("Argument required (%s).", why
);
3004 help_command (command
, from_tty
)
3006 int from_tty
; /* Ignored */
3008 help_cmd (command
, gdb_stdout
);
3012 validate_comname (comname
)
3018 error_no_arg ("name of command to define");
3023 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
3024 error ("Junk in argument list: \"%s\"", p
);
3029 /* This is just a placeholder in the command data structures. */
3031 user_defined_command (ignore
, from_tty
)
3038 define_command (comname
, from_tty
)
3042 register struct command_line
*cmds
;
3043 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
3044 char *tem
= comname
;
3046 #define HOOK_STRING "hook-"
3049 validate_comname (comname
);
3051 /* Look it up, and verify that we got an exact match. */
3052 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
3053 if (c
&& !STREQ (comname
, c
->name
))
3058 if (c
->class == class_user
|| c
->class == class_alias
)
3059 tem
= "Redefine command \"%s\"? ";
3061 tem
= "Really redefine built-in command \"%s\"? ";
3062 if (!query (tem
, c
->name
))
3063 error ("Command \"%s\" not redefined.", c
->name
);
3066 /* If this new command is a hook, then mark the command which it
3067 is hooking. Note that we allow hooking `help' commands, so that
3068 we can hook the `stop' pseudo-command. */
3070 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
3072 /* Look up cmd it hooks, and verify that we got an exact match. */
3073 tem
= comname
+ HOOK_LEN
;
3074 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
3075 if (hookc
&& !STREQ (comname
+ HOOK_LEN
, hookc
->name
))
3079 warning ("Your new `%s' command does not hook any existing command.",
3081 if (!query ("Proceed? "))
3082 error ("Not confirmed.");
3086 comname
= savestring (comname
, strlen (comname
));
3088 /* If the rest of the commands will be case insensitive, this one
3089 should behave in the same manner. */
3090 for (tem
= comname
; *tem
; tem
++)
3092 *tem
= tolower (*tem
);
3094 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
3095 cmds
= read_command_lines (tmpbuf
, from_tty
);
3097 if (c
&& c
->class == class_user
)
3098 free_command_lines (&c
->user_commands
);
3100 newc
= add_cmd (comname
, class_user
, user_defined_command
,
3101 (c
&& c
->class == class_user
)
3102 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
3103 newc
->user_commands
= cmds
;
3105 /* If this new command is a hook, then mark both commands as being
3109 hookc
->hook
= newc
; /* Target gets hooked. */
3110 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
3115 document_command (comname
, from_tty
)
3119 struct command_line
*doclines
;
3120 register struct cmd_list_element
*c
;
3121 char *tem
= comname
;
3124 validate_comname (comname
);
3126 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
3128 if (c
->class != class_user
)
3129 error ("Command \"%s\" is built-in.", comname
);
3131 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
3132 doclines
= read_command_lines (tmpbuf
, from_tty
);
3138 register struct command_line
*cl1
;
3139 register int len
= 0;
3141 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
3142 len
+= strlen (cl1
->line
) + 1;
3144 c
->doc
= (char *) xmalloc (len
+ 1);
3147 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
3149 strcat (c
->doc
, cl1
->line
);
3151 strcat (c
->doc
, "\n");
3155 free_command_lines (&doclines
);
3158 /* Print the GDB banner. */
3160 print_gdb_version (stream
)
3161 struct ui_file
*stream
;
3163 /* From GNU coding standards, first line is meant to be easy for a
3164 program to parse, and is just canonical program name and version
3165 number, which starts after last space. */
3168 /* Print it console style until a format is defined */
3169 fprintf_filtered (stream
, "GNU gdb %s (UI_OUT)\n", version
);
3171 fprintf_filtered (stream
, "GNU gdb %s\n", version
);
3174 /* Second line is a copyright notice. */
3176 fprintf_filtered (stream
, "Copyright 1998 Free Software Foundation, Inc.\n");
3178 /* Following the copyright is a brief statement that the program is
3179 free software, that users are free to copy and change it on
3180 certain conditions, that it is covered by the GNU GPL, and that
3181 there is no warranty. */
3183 fprintf_filtered (stream
, "\
3184 GDB is free software, covered by the GNU General Public License, and you are\n\
3185 welcome to change it and/or distribute copies of it under certain conditions.\n\
3186 Type \"show copying\" to see the conditions.\n\
3187 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
3189 /* After the required info we print the configuration information. */
3191 fprintf_filtered (stream
, "This GDB was configured as \"");
3192 if (!STREQ (host_name
, target_name
))
3194 fprintf_filtered (stream
, "--host=%s --target=%s", host_name
, target_name
);
3198 fprintf_filtered (stream
, "%s", host_name
);
3200 fprintf_filtered (stream
, "\".");
3205 show_version (args
, from_tty
)
3210 print_gdb_version (gdb_stdout
);
3211 printf_filtered ("\n");
3215 /* get_prompt: access method for the GDB prompt string. */
3217 #define MAX_PROMPT_SIZE 256
3220 * int get_prompt_1 (char * buf);
3222 * Work-horse for get_prompt (called via catch_errors).
3223 * Argument is buffer to hold the formatted prompt.
3225 * Returns: 1 for success (use formatted prompt)
3226 * 0 for failure (use gdb_prompt_string).
3229 static int gdb_prompt_escape
;
3232 get_prompt_1 (formatted_prompt
)
3233 char *formatted_prompt
;
3238 local_prompt
= PROMPT (0);
3240 local_prompt
= gdb_prompt_string
;
3243 if (gdb_prompt_escape
== 0)
3245 return 0; /* do no formatting */
3248 /* formatted prompt */
3250 char fmt
[40], *promptp
, *outp
, *tmp
;
3257 struct type
*arg_type
, *elt_type
;
3259 promptp
= local_prompt
;
3260 outp
= formatted_prompt
;
3262 while (*promptp
!= '\0')
3264 int available
= MAX_PROMPT_SIZE
- (outp
- formatted_prompt
) - 1;
3266 if (*promptp
!= gdb_prompt_escape
)
3268 if (available
>= 1) /* overflow protect */
3269 *outp
++ = *promptp
++;
3273 /* GDB prompt string contains escape char. Parse for arg.
3274 Two consecutive escape chars followed by arg followed by
3275 a comma means to insert the arg using a default format.
3276 Otherwise a printf format string may be included between
3277 the two escape chars. eg:
3278 %%foo, insert foo using default format
3279 %2.2f%foo, insert foo using "%2.2f" format
3280 A mismatch between the format string and the data type
3281 of "foo" is an error (which we don't know how to protect
3284 fmt
[0] = '\0'; /* assume null format string */
3285 if (promptp
[1] == gdb_prompt_escape
) /* double esc char */
3287 promptp
+= 2; /* skip past two escape chars. */
3291 /* extract format string from between two esc chars */
3295 fmt
[i
++] = *promptp
++; /* copy format string */
3297 while (i
< sizeof (fmt
) - 1 &&
3298 *promptp
!= gdb_prompt_escape
&&
3301 if (*promptp
!= gdb_prompt_escape
)
3302 error ("Syntax error at prompt position %d",
3303 promptp
- local_prompt
);
3306 promptp
++; /* skip second escape char */
3307 fmt
[i
++] = '\0'; /* terminate the format string */
3311 arg_val
= parse_to_comma_and_eval (&promptp
);
3312 if (*promptp
== ',')
3313 promptp
++; /* skip past the comma */
3314 arg_type
= check_typedef (VALUE_TYPE (arg_val
));
3315 switch (TYPE_CODE (arg_type
))
3317 case TYPE_CODE_ARRAY
:
3318 elt_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
3319 if (TYPE_LENGTH (arg_type
) > 0 &&
3320 TYPE_LENGTH (elt_type
) == 1 &&
3321 TYPE_CODE (elt_type
) == TYPE_CODE_INT
)
3323 int len
= TYPE_LENGTH (arg_type
);
3325 if (VALUE_LAZY (arg_val
))
3326 value_fetch_lazy (arg_val
);
3327 tmp
= VALUE_CONTENTS (arg_val
);
3329 if (len
> available
)
3330 len
= available
; /* overflow protect */
3332 /* FIXME: how to protect GDB from crashing
3333 from bad user-supplied format string? */
3335 sprintf (outp
, fmt
, tmp
);
3337 strncpy (outp
, tmp
, len
);
3342 elt_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
3343 addrval
= value_as_pointer (arg_val
);
3345 if (TYPE_LENGTH (elt_type
) == 1 &&
3346 TYPE_CODE (elt_type
) == TYPE_CODE_INT
&&
3349 /* display it as a string */
3350 char *default_fmt
= "%s";
3354 /* Limiting the number of bytes that the following call
3355 will read protects us from sprintf overflow later. */
3356 i
= target_read_string (addrval
, /* src */
3358 available
, /* len */
3360 if (err
) /* read failed */
3361 error ("%s on target_read", safe_strerror (err
));
3363 tmp
[i
] = '\0'; /* force-terminate string */
3364 /* FIXME: how to protect GDB from crashing
3365 from bad user-supplied format string? */
3366 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3372 /* display it as a pointer */
3373 char *default_fmt
= "0x%x";
3375 /* FIXME: how to protect GDB from crashing
3376 from bad user-supplied format string? */
3377 if (available
>= 16 /*? */ ) /* overflow protect */
3378 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3384 char *default_fmt
= "%g";
3386 doubleval
= value_as_double (arg_val
);
3387 /* FIXME: how to protect GDB from crashing
3388 from bad user-supplied format string? */
3389 if (available
>= 16 /*? */ ) /* overflow protect */
3390 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3391 (double) doubleval
);
3396 char *default_fmt
= "%d";
3398 longval
= value_as_long (arg_val
);
3399 /* FIXME: how to protect GDB from crashing
3400 from bad user-supplied format string? */
3401 if (available
>= 16 /*? */ ) /* overflow protect */
3402 sprintf (outp
, fmt
[0] == 0 ? default_fmt
: fmt
,
3406 case TYPE_CODE_BOOL
:
3408 /* no default format for bool */
3409 longval
= value_as_long (arg_val
);
3410 if (available
>= 8 /*? */ ) /* overflow protect */
3413 strcpy (outp
, "<true>");
3415 strcpy (outp
, "<false>");
3419 case TYPE_CODE_ENUM
:
3421 /* no default format for enum */
3422 longval
= value_as_long (arg_val
);
3423 len
= TYPE_NFIELDS (arg_type
);
3424 /* find enum name if possible */
3425 for (i
= 0; i
< len
; i
++)
3426 if (TYPE_FIELD_BITPOS (arg_type
, i
) == longval
)
3427 break; /* match -- end loop */
3429 if (i
< len
) /* enum name found */
3431 char *name
= TYPE_FIELD_NAME (arg_type
, i
);
3433 strncpy (outp
, name
, available
);
3434 /* in casel available < strlen (name), */
3435 outp
[available
] = '\0';
3439 if (available
>= 16 /*? */ ) /* overflow protect */
3440 sprintf (outp
, "%ld", (long) longval
);
3444 case TYPE_CODE_VOID
:
3446 break; /* void type -- no output */
3448 error ("bad data type at prompt position %d",
3449 promptp
- local_prompt
);
3452 outp
+= strlen (outp
);
3455 *outp
++ = '\0'; /* terminate prompt string */
3463 static char buf
[MAX_PROMPT_SIZE
];
3465 if (catch_errors (get_prompt_1
, buf
, "bad formatted prompt: ",
3468 return &buf
[0]; /* successful formatted prompt */
3472 /* Prompt could not be formatted. */
3476 return gdb_prompt_string
;
3484 /* ??rehrauer: I don't know why this fails, since it looks as though
3485 assignments to prompt are wrapped in calls to savestring...
3490 PROMPT (0) = savestring (s
, strlen (s
));
3492 gdb_prompt_string
= savestring (s
, strlen (s
));
3496 /* If necessary, make the user confirm that we should quit. Return
3497 non-zero if we should quit, zero if we shouldn't. */
3502 if (inferior_pid
!= 0 && target_has_execution
)
3506 /* This is something of a hack. But there's no reliable way to
3507 see if a GUI is running. The `use_windows' variable doesn't
3510 s
= "A debugging session is active.\nDo you still want to close the debugger?";
3511 else if (attach_flag
)
3512 s
= "The program is running. Quit anyway (and detach it)? ";
3514 s
= "The program is running. Exit anyway? ";
3523 /* Quit without asking for confirmation. */
3526 quit_force (args
, from_tty
)
3532 /* An optional expression may be used to cause gdb to terminate with the
3533 value of that expression. */
3536 value_ptr val
= parse_and_eval (args
);
3538 exit_code
= (int) value_as_long (val
);
3541 if (inferior_pid
!= 0 && target_has_execution
)
3544 target_detach (args
, from_tty
);
3549 /* UDI wants this, to kill the TIP. */
3552 /* Save the history information if it is appropriate to do so. */
3553 if (write_history_p
&& history_filename
)
3554 write_history (history_filename
);
3556 do_final_cleanups (ALL_CLEANUPS
); /* Do any final cleanups before exiting */
3559 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3560 /* The above does not need to be inside a tuiDo(), since
3561 * it is not manipulating the curses screen, but rather,
3562 * it is tearing it down.
3571 /* Handle the quit command. */
3574 quit_command (args
, from_tty
)
3578 if (!quit_confirm ())
3579 error ("Not confirmed.");
3580 quit_force (args
, from_tty
);
3583 /* Returns whether GDB is running on a terminal and whether the user
3584 desires that questions be asked of them on that terminal. */
3587 input_from_terminal_p ()
3589 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
3594 pwd_command (args
, from_tty
)
3599 error ("The \"pwd\" command does not take an argument: %s", args
);
3600 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3602 if (!STREQ (gdb_dirbuf
, current_directory
))
3603 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3604 current_directory
, gdb_dirbuf
);
3606 printf_unfiltered ("Working directory %s.\n", current_directory
);
3610 cd_command (dir
, from_tty
)
3615 /* Found something other than leading repetitions of "/..". */
3616 int found_real_path
;
3619 /* If the new directory is absolute, repeat is a no-op; if relative,
3620 repeat might be useful but is more likely to be a mistake. */
3624 error_no_arg ("new working directory");
3626 dir
= tilde_expand (dir
);
3627 make_cleanup (free
, dir
);
3629 if (chdir (dir
) < 0)
3630 perror_with_name (dir
);
3632 #if defined(_WIN32) || defined(__MSDOS__)
3633 /* There's too much mess with DOSish names like "d:", "d:.",
3634 "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
3635 simply get the canonicalized name of the current directory. */
3636 dir
= getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3640 if (SLASH_P (dir
[len
-1]))
3642 /* Remove the trailing slash unless this is a root directory
3643 (including a drive letter on non-Unix systems). */
3644 if (!(len
== 1) /* "/" */
3645 #if defined(_WIN32) || defined(__MSDOS__)
3646 && !(!SLASH_P (*dir
) && ROOTED_P (dir
) && len
<= 3) /* "d:/" */
3652 dir
= savestring (dir
, len
);
3654 current_directory
= dir
;
3657 if (SLASH_P (current_directory
[strlen (current_directory
) - 1]))
3658 current_directory
= concat (current_directory
, dir
, NULL
);
3660 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
3664 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3666 found_real_path
= 0;
3667 for (p
= current_directory
; *p
;)
3669 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
3671 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
3672 && (p
[3] == 0 || SLASH_P (p
[3])))
3674 if (found_real_path
)
3676 /* Search backwards for the directory just before the "/.."
3677 and obliterate it and the "/..". */
3679 while (q
!= current_directory
&& !SLASH_P (q
[-1]))
3682 if (q
== current_directory
)
3683 /* current_directory is
3684 a relative pathname ("can't happen"--leave it alone). */
3688 strcpy (q
- 1, p
+ 3);
3693 /* We are dealing with leading repetitions of "/..", for example
3694 "/../..", which is the Mach super-root. */
3699 found_real_path
= 1;
3704 forget_cached_source_info ();
3707 pwd_command ((char *) 0, 1);
3710 struct source_cleanup_lines_args
3714 char *old_pre_error
;
3715 char *old_error_pre_print
;
3719 source_cleanup_lines (args
)
3722 struct source_cleanup_lines_args
*p
=
3723 (struct source_cleanup_lines_args
*) args
;
3724 source_line_number
= p
->old_line
;
3725 source_file_name
= p
->old_file
;
3726 source_pre_error
= p
->old_pre_error
;
3727 error_pre_print
= p
->old_error_pre_print
;
3732 source_command (args
, from_tty
)
3737 struct cleanup
*old_cleanups
;
3739 struct source_cleanup_lines_args old_lines
;
3744 error ("source command requires pathname of file to source.");
3747 file
= tilde_expand (file
);
3748 old_cleanups
= make_cleanup (free
, file
);
3750 stream
= fopen (file
, FOPEN_RT
);
3754 perror_with_name (file
);
3759 make_cleanup ((make_cleanup_func
) fclose
, stream
);
3761 old_lines
.old_line
= source_line_number
;
3762 old_lines
.old_file
= source_file_name
;
3763 old_lines
.old_pre_error
= source_pre_error
;
3764 old_lines
.old_error_pre_print
= error_pre_print
;
3765 make_cleanup (source_cleanup_lines
, &old_lines
);
3766 source_line_number
= 0;
3767 source_file_name
= file
;
3768 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
3769 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
3770 make_cleanup (free
, source_pre_error
);
3771 /* This will get set every time we read a line. So it won't stay "" for
3773 error_pre_print
= "";
3775 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
3776 if (source_error_allocated
< needed_length
)
3778 source_error_allocated
*= 2;
3779 if (source_error_allocated
< needed_length
)
3780 source_error_allocated
= needed_length
;
3781 if (source_error
== NULL
)
3782 source_error
= xmalloc (source_error_allocated
);
3784 source_error
= xrealloc (source_error
, source_error_allocated
);
3787 read_command_file (stream
);
3789 do_cleanups (old_cleanups
);
3794 echo_command (text
, from_tty
)
3802 while ((c
= *p
++) != '\0')
3806 /* \ at end of argument is used after spaces
3807 so they won't be lost. */
3811 c
= parse_escape (&p
);
3813 printf_filtered ("%c", c
);
3816 printf_filtered ("%c", c
);
3819 /* Force this output to appear now. */
3821 gdb_flush (gdb_stdout
);
3826 dont_repeat_command (ignored
, from_tty
)
3830 *line
= 0; /* Can't call dont_repeat here because we're not
3831 necessarily reading from stdin. */
3834 /* Functions to manipulate command line editing control variables. */
3836 /* Number of commands to print in each call to show_commands. */
3837 #define Hist_print 10
3839 show_commands (args
, from_tty
)
3843 /* Index for history commands. Relative to history_base. */
3846 /* Number of the history entry which we are planning to display next.
3847 Relative to history_base. */
3850 /* The first command in the history which doesn't exist (i.e. one more
3851 than the number of the last command). Relative to history_base. */
3854 extern HIST_ENTRY
*history_get
PARAMS ((int));
3856 /* Print out some of the commands from the command history. */
3857 /* First determine the length of the history list. */
3858 hist_len
= history_size
;
3859 for (offset
= 0; offset
< history_size
; offset
++)
3861 if (!history_get (history_base
+ offset
))
3870 if (args
[0] == '+' && args
[1] == '\0')
3871 /* "info editing +" should print from the stored position. */
3874 /* "info editing <exp>" should print around command number <exp>. */
3875 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3877 /* "show commands" means print the last Hist_print commands. */
3880 num
= hist_len
- Hist_print
;
3886 /* If there are at least Hist_print commands, we want to display the last
3887 Hist_print rather than, say, the last 6. */
3888 if (hist_len
- num
< Hist_print
)
3890 num
= hist_len
- Hist_print
;
3895 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3897 printf_filtered ("%5d %s\n", history_base
+ offset
,
3898 (history_get (history_base
+ offset
))->line
);
3901 /* The next command we want to display is the next one that we haven't
3905 /* If the user repeats this command with return, it should do what
3906 "show commands +" does. This is unnecessary if arg is null,
3907 because "show commands +" is not useful after "show commands". */
3908 if (from_tty
&& args
)
3915 /* Called by do_setshow_command. */
3918 set_history_size_command (args
, from_tty
, c
)
3921 struct cmd_list_element
*c
;
3923 if (history_size
== INT_MAX
)
3924 unstifle_history ();
3925 else if (history_size
>= 0)
3926 stifle_history (history_size
);
3929 history_size
= INT_MAX
;
3930 error ("History size must be non-negative");
3936 set_history (args
, from_tty
)
3940 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3941 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3946 show_history (args
, from_tty
)
3950 cmd_show_list (showhistlist
, from_tty
, "");
3953 int info_verbose
= 0; /* Default verbose msgs off */
3955 /* Called by do_setshow_command. An elaborate joke. */
3958 set_verbose (args
, from_tty
, c
)
3961 struct cmd_list_element
*c
;
3963 char *cmdname
= "verbose";
3964 struct cmd_list_element
*showcmd
;
3966 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3970 c
->doc
= "Set verbose printing of informational messages.";
3971 showcmd
->doc
= "Show verbose printing of informational messages.";
3975 c
->doc
= "Set verbosity.";
3976 showcmd
->doc
= "Show verbosity.";
3981 float_handler (signo
)
3984 /* This message is based on ANSI C, section 4.7. Note that integer
3985 divide by zero causes this, so "float" is a misnomer. */
3986 signal (SIGFPE
, float_handler
);
3987 error ("Erroneous arithmetic operation.");
4001 enablebreaklist
= NULL
;
4006 showhistlist
= NULL
;
4007 unsethistlist
= NULL
;
4008 maintenancelist
= NULL
;
4009 maintenanceinfolist
= NULL
;
4010 maintenanceprintlist
= NULL
;
4011 setprintlist
= NULL
;
4012 showprintlist
= NULL
;
4013 setchecklist
= NULL
;
4014 showchecklist
= NULL
;
4017 /* Init the history buffer. Note that we are called after the init file(s)
4018 * have been read so that the user can change the history file via his
4019 * .gdbinit file (for instance). The GDBHISTFILE environment variable
4020 * overrides all of this.
4028 tmpenv
= getenv ("HISTSIZE");
4030 history_size
= atoi (tmpenv
);
4031 else if (!history_size
)
4034 stifle_history (history_size
);
4036 tmpenv
= getenv ("GDBHISTFILE");
4038 history_filename
= savestring (tmpenv
, strlen (tmpenv
));
4039 else if (!history_filename
)
4041 /* We include the current directory so that if the user changes
4042 directories the file written will be the same as the one
4045 /* No leading dots in file names are allowed on MSDOS. */
4046 history_filename
= concat (current_directory
, "/_gdb_history", NULL
);
4048 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
4051 read_history (history_filename
);
4057 struct cmd_list_element
*c
;
4059 /* If we are running the asynchronous version,
4060 we initialize the prompts differently. */
4063 gdb_prompt_string
= savestring (DEFAULT_PROMPT
, strlen (DEFAULT_PROMPT
));
4067 /* initialize the prompt stack to a simple "(gdb) " prompt or to
4068 whatever the DEFAULT_PROMPT is. */
4069 the_prompts
.top
= 0;
4071 PROMPT (0) = savestring (DEFAULT_PROMPT
, strlen (DEFAULT_PROMPT
));
4073 /* Set things up for annotation_level > 1, if the user ever decides
4075 async_annotation_suffix
= "prompt";
4076 /* Set the variable associated with the setshow prompt command. */
4077 new_async_prompt
= savestring (PROMPT (0), strlen (PROMPT (0)));
4079 gdb_prompt_escape
= 0; /* default to none. */
4081 /* Set the important stuff up for command editing. */
4082 command_editing_p
= 1;
4083 history_expansion_p
= 0;
4084 write_history_p
= 0;
4086 /* Setup important stuff for command line editing. */
4087 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
4088 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
4089 rl_completer_quote_characters
= gdb_completer_quote_characters
;
4090 rl_readline_name
= "gdb";
4092 /* Define the classes of commands.
4093 They will appear in the help list in the reverse of this order. */
4095 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
4096 "Maintenance commands.\n\
4097 Some gdb commands are provided just for use by gdb maintainers.\n\
4098 These commands are subject to frequent change, and may not be as\n\
4099 well documented as user commands.",
4101 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
4102 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
4103 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
4104 The commands in this class are those defined by the user.\n\
4105 Use the \"define\" command to define a command.", &cmdlist
);
4106 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
4108 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
4109 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
4110 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
4111 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
4112 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
4113 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
4114 counting from zero for the innermost (currently executing) frame.\n\n\
4115 At any time gdb identifies one frame as the \"selected\" frame.\n\
4116 Variable lookups are done with respect to the selected frame.\n\
4117 When the program being debugged stops, gdb selects the innermost frame.\n\
4118 The commands below can be used to select other frames by number or address.",
4120 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
4122 add_com ("pwd", class_files
, pwd_command
,
4123 "Print working directory. This is used for your program as well.");
4124 c
= add_cmd ("cd", class_files
, cd_command
,
4125 "Set working directory to DIR for debugger and program being debugged.\n\
4126 The change does not take effect for the program being debugged\n\
4127 until the next time it is started.", &cmdlist
);
4128 c
->completer
= filename_completer
;
4130 /* The set prompt command is different depending whether or not the
4131 async version is run. NOTE: this difference is going to
4132 disappear as we make the event loop be the default engine of
4137 (add_set_cmd ("prompt", class_support
, var_string
,
4138 (char *) &gdb_prompt_string
, "Set gdb's prompt",
4144 c
= add_set_cmd ("prompt", class_support
, var_string
,
4145 (char *) &new_async_prompt
, "Set gdb's prompt",
4147 add_show_from_set (c
, &showlist
);
4148 c
->function
.sfunc
= set_async_prompt
;
4152 (add_set_cmd ("prompt-escape-char", class_support
, var_zinteger
,
4153 (char *) &gdb_prompt_escape
,
4154 "Set escape character for formatting of gdb's prompt",
4158 add_com ("echo", class_support
, echo_command
,
4159 "Print a constant string. Give string as argument.\n\
4160 C escape sequences may be used in the argument.\n\
4161 No newline is added at the end of the argument;\n\
4162 use \"\\n\" if you want a newline to be printed.\n\
4163 Since leading and trailing whitespace are ignored in command arguments,\n\
4164 if you want to print some you must use \"\\\" before leading whitespace\n\
4165 to be printed or after trailing whitespace.");
4166 add_com ("document", class_support
, document_command
,
4167 "Document a user-defined command.\n\
4168 Give command name as argument. Give documentation on following lines.\n\
4169 End with a line of just \"end\".");
4170 add_com ("define", class_support
, define_command
,
4171 "Define a new command name. Command name is argument.\n\
4172 Definition appears on following lines, one command per line.\n\
4173 End with a line of just \"end\".\n\
4174 Use the \"document\" command to give documentation for the new command.\n\
4175 Commands defined in this way may have up to ten arguments.");
4178 c
= add_cmd ("source", class_support
, source_command
,
4179 "Read commands from a file named FILE.\n\
4180 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
4181 when gdb is started.", &cmdlist
);
4183 /* Punt file name, we can't help it easily. */
4184 c
= add_cmd ("source", class_support
, source_command
,
4185 "Read commands from a file named FILE.\n\
4186 Note that the file \".gdbinit\" is read automatically in this way\n\
4187 when gdb is started.", &cmdlist
);
4189 c
->completer
= filename_completer
;
4191 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
4192 add_com ("help", class_support
, help_command
, "Print list of commands.");
4193 add_com_alias ("q", "quit", class_support
, 1);
4194 add_com_alias ("h", "help", class_support
, 1);
4196 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
4197 Primarily used inside of user-defined commands that should not be repeated when\n\
4200 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *) &info_verbose
,
4203 add_show_from_set (c
, &showlist
);
4204 c
->function
.sfunc
= set_verbose
;
4205 set_verbose (NULL
, 0, c
);
4207 /* The set editing command is different depending whether or not the
4208 async version is run. NOTE: this difference is going to disappear
4209 as we make the event loop be the default engine of gdb. */
4213 (add_set_cmd ("editing", class_support
, var_boolean
, (char *) &command_editing_p
,
4214 "Set editing of command lines as they are typed.\n\
4215 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4216 Without an argument, command line editing is enabled. To edit, use\n\
4217 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
4222 c
= add_set_cmd ("editing", class_support
, var_boolean
, (char *) &async_command_editing_p
,
4223 "Set editing of command lines as they are typed.\n\
4224 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4225 Without an argument, command line editing is enabled. To edit, use\n\
4226 EMACS-like or VI-like commands like control-P or ESC.", &setlist
);
4228 add_show_from_set (c
, &showlist
);
4229 c
->function
.sfunc
= set_async_editing_command
;
4232 add_prefix_cmd ("history", class_support
, set_history
,
4233 "Generic command for setting command history parameters.",
4234 &sethistlist
, "set history ", 0, &setlist
);
4235 add_prefix_cmd ("history", class_support
, show_history
,
4236 "Generic command for showing command history parameters.",
4237 &showhistlist
, "show history ", 0, &showlist
);
4240 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *) &history_expansion_p
,
4241 "Set history expansion on command input.\n\
4242 Without an argument, history expansion is enabled.", &sethistlist
),
4246 (add_set_cmd ("save", no_class
, var_boolean
, (char *) &write_history_p
,
4247 "Set saving of the history record on exit.\n\
4248 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
4249 Without an argument, saving is enabled.", &sethistlist
),
4252 c
= add_set_cmd ("size", no_class
, var_integer
, (char *) &history_size
,
4253 "Set the size of the command history, \n\
4254 ie. the number of previous commands to keep a record of.", &sethistlist
);
4255 add_show_from_set (c
, &showhistlist
);
4256 c
->function
.sfunc
= set_history_size_command
;
4259 (add_set_cmd ("filename", no_class
, var_filename
, (char *) &history_filename
,
4260 "Set the filename in which to record the command history\n\
4261 (the list of previous commands of which a record is kept).", &sethistlist
),
4265 (add_set_cmd ("confirm", class_support
, var_boolean
,
4267 "Set whether to confirm potentially dangerous operations.",
4271 add_prefix_cmd ("info", class_info
, info_command
,
4272 "Generic command for showing things about the program being debugged.",
4273 &infolist
, "info ", 0, &cmdlist
);
4274 add_com_alias ("i", "info", class_info
, 1);
4276 add_com ("complete", class_obscure
, complete_command
,
4277 "List the completions for the rest of the line as a command.");
4279 add_prefix_cmd ("show", class_info
, show_command
,
4280 "Generic command for showing things about the debugger.",
4281 &showlist
, "show ", 0, &cmdlist
);
4282 /* Another way to get at the same thing. */
4283 add_info ("set", show_command
, "Show all GDB settings.");
4285 add_cmd ("commands", no_class
, show_commands
,
4286 "Show the history of commands you typed.\n\
4287 You can supply a command number to start with, or a `+' to start after\n\
4288 the previous command number shown.",
4291 add_cmd ("version", no_class
, show_version
,
4292 "Show what version of GDB this is.", &showlist
);
4294 add_com ("while", class_support
, while_command
,
4295 "Execute nested commands WHILE the conditional expression is non zero.\n\
4296 The conditional expression must follow the word `while' and must in turn be\n\
4297 followed by a new line. The nested commands must be entered one per line,\n\
4298 and should be terminated by the word `end'.");
4300 add_com ("if", class_support
, if_command
,
4301 "Execute nested commands once IF the conditional expression is non zero.\n\
4302 The conditional expression must follow the word `if' and must in turn be\n\
4303 followed by a new line. The nested commands must be entered one per line,\n\
4304 and should be terminated by the word 'else' or `end'. If an else clause\n\
4305 is used, the same rules apply to its nested commands as to the first ones.");
4307 /* If target is open when baud changes, it doesn't take effect until the
4308 next open (I think, not sure). */
4309 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
4310 var_zinteger
, (char *) &baud_rate
,
4311 "Set baud rate for remote serial I/O.\n\
4312 This value is used to set the speed of the serial port when debugging\n\
4313 using remote targets.", &setlist
),
4317 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *) &remote_debug
,
4318 "Set debugging of remote protocol.\n\
4319 When enabled, each packet sent or received with the remote target\n\
4320 is displayed.", &setlist
),
4324 add_set_cmd ("remotetimeout", no_class
, var_integer
, (char *) &remote_timeout
,
4325 "Set timeout limit to wait for target to respond.\n\
4326 This value is used to set the time limit for gdb to wait for a response\n\
4327 from the target.", &setlist
),
4330 /* The set annotate command is different depending whether or not
4331 the async version is run. NOTE: this difference is going to
4332 disappear as we make the event loop be the default engine of
4336 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
4337 (char *) &annotation_level
, "Set annotation_level.\n\
4338 0 == normal; 1 == fullname (for use when running under emacs)\n\
4339 2 == output annotated suitably for use by programs that control GDB.",
4341 c
= add_show_from_set (c
, &showlist
);
4345 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
4346 (char *) &annotation_level
, "Set annotation_level.\n\
4347 0 == normal; 1 == fullname (for use when running under emacs)\n\
4348 2 == output annotated suitably for use by programs that control GDB.",
4350 add_show_from_set (c
, &showlist
);
4351 c
->function
.sfunc
= set_async_annotation_level
;
4356 (add_set_cmd ("exec-done-display", class_support
, var_boolean
, (char *) &exec_done_display_p
,
4357 "Set notification of completion for asynchronous execution commands.\n\
4358 Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist
),