1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, 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>
50 #include "gdb_string.h"
54 extern void initialize_utils
PARAMS ((void));
56 /* Prototypes for local functions */
58 static void dont_repeat_command
PARAMS ((char *, int));
60 static void source_cleanup_lines
PARAMS ((PTR
));
62 static void user_defined_command
PARAMS ((char *, int));
64 static void init_signals
PARAMS ((void));
67 static void stop_sig
PARAMS ((int));
70 static char * line_completion_function
PARAMS ((char *, int, char *, int));
72 static char * readline_line_completion_function
PARAMS ((char *, int));
74 static void command_loop_marker
PARAMS ((int));
76 static void while_command
PARAMS ((char *, int));
78 static void if_command
PARAMS ((char *, int));
80 static struct command_line
*
81 build_command_line
PARAMS ((enum command_control_type
, char *));
83 static struct command_line
*
84 get_command_line
PARAMS ((enum command_control_type
, char *));
86 static void realloc_body_list
PARAMS ((struct command_line
*, int));
88 static enum misc_command_type read_next_line
PARAMS ((struct command_line
**));
90 static enum command_control_type
91 recurse_read_control_structure
PARAMS ((struct command_line
*));
93 static struct cleanup
* setup_user_args
PARAMS ((char *));
95 static char * locate_arg
PARAMS ((char *));
97 static char * insert_args
PARAMS ((char *));
99 static void arg_cleanup
PARAMS ((void));
101 static void init_main
PARAMS ((void));
103 static void init_cmd_lists
PARAMS ((void));
105 static void float_handler
PARAMS ((int));
107 static void init_signals
PARAMS ((void));
109 static void set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
111 static void show_history
PARAMS ((char *, int));
113 static void set_history
PARAMS ((char *, int));
115 static void set_history_size_command
PARAMS ((char *, int,
116 struct cmd_list_element
*));
118 static void show_commands
PARAMS ((char *, int));
120 static void echo_command
PARAMS ((char *, int));
122 static void pwd_command
PARAMS ((char *, int));
124 static void show_version
PARAMS ((char *, int));
126 static void document_command
PARAMS ((char *, int));
128 static void define_command
PARAMS ((char *, int));
130 static void validate_comname
PARAMS ((char *));
132 static void help_command
PARAMS ((char *, int));
134 static void show_command
PARAMS ((char *, int));
136 static void info_command
PARAMS ((char *, int));
138 static void complete_command
PARAMS ((char *, int));
140 static void do_nothing
PARAMS ((int));
143 static int quit_cover
PARAMS ((PTR
));
145 static void disconnect
PARAMS ((int));
148 static void source_cleanup
PARAMS ((FILE *));
150 /* If this definition isn't overridden by the header files, assume
151 that isatty and fileno exist on this system. */
153 #define ISATTY(FP) (isatty (fileno (FP)))
156 /* Initialization file name for gdb. This is overridden in some configs. */
158 #ifndef GDBINIT_FILENAME
159 #define GDBINIT_FILENAME ".gdbinit"
161 char gdbinit
[] = GDBINIT_FILENAME
;
163 int inhibit_gdbinit
= 0;
165 /* If nonzero, and GDB has been configured to be able to use windows,
166 attempt to open them upon startup. */
170 /* Version number of GDB, as a string. */
172 extern char *version
;
174 /* Canonical host name as a string. */
176 extern char *host_name
;
178 /* Canonical target name as a string. */
180 extern char *target_name
;
182 extern char lang_frame_mismatch_warn
[]; /* language.c */
184 /* Flag for whether we want all the "from_tty" gubbish printed. */
186 int caution
= 1; /* Default is yes, sigh. */
188 /* Define all cmd_list_elements. */
190 /* Chain containing all defined commands. */
192 struct cmd_list_element
*cmdlist
;
194 /* Chain containing all defined info subcommands. */
196 struct cmd_list_element
*infolist
;
198 /* Chain containing all defined enable subcommands. */
200 struct cmd_list_element
*enablelist
;
202 /* Chain containing all defined disable subcommands. */
204 struct cmd_list_element
*disablelist
;
206 /* Chain containing all defined toggle subcommands. */
208 struct cmd_list_element
*togglelist
;
210 /* Chain containing all defined stop subcommands. */
212 struct cmd_list_element
*stoplist
;
214 /* Chain containing all defined delete subcommands. */
216 struct cmd_list_element
*deletelist
;
218 /* Chain containing all defined "enable breakpoint" subcommands. */
220 struct cmd_list_element
*enablebreaklist
;
222 /* Chain containing all defined set subcommands */
224 struct cmd_list_element
*setlist
;
226 /* Chain containing all defined unset subcommands */
228 struct cmd_list_element
*unsetlist
;
230 /* Chain containing all defined show subcommands. */
232 struct cmd_list_element
*showlist
;
234 /* Chain containing all defined \"set history\". */
236 struct cmd_list_element
*sethistlist
;
238 /* Chain containing all defined \"show history\". */
240 struct cmd_list_element
*showhistlist
;
242 /* Chain containing all defined \"unset history\". */
244 struct cmd_list_element
*unsethistlist
;
246 /* Chain containing all defined maintenance subcommands. */
249 struct cmd_list_element
*maintenancelist
;
252 /* Chain containing all defined "maintenance info" subcommands. */
255 struct cmd_list_element
*maintenanceinfolist
;
258 /* Chain containing all defined "maintenance print" subcommands. */
261 struct cmd_list_element
*maintenanceprintlist
;
264 struct cmd_list_element
*setprintlist
;
266 struct cmd_list_element
*showprintlist
;
268 struct cmd_list_element
*setchecklist
;
270 struct cmd_list_element
*showchecklist
;
272 /* stdio stream that command input is being read from. Set to stdin normally.
273 Set by source_command to the file we are sourcing. Set to NULL if we are
274 executing a user-defined command or interacting via a GUI. */
278 /* Current working directory. */
280 char *current_directory
;
282 /* The directory name is actually stored here (usually). */
283 char gdb_dirbuf
[1024];
285 /* Function to call before reading a command, if nonzero.
286 The function receives two args: an input stream,
287 and a prompt string. */
289 void (*window_hook
) PARAMS ((FILE *, char *));
294 /* gdb prints this when reading a command interactively */
297 /* Buffer used for reading command lines, and the size
298 allocated for it so far. */
303 /* Nonzero if the current command is modified by "server ". This
304 affects things like recording into the command history, comamnds
305 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
306 whatever) can issue its own commands and also send along commands
307 from the user, and have the user not notice that the user interface
308 is issuing commands too. */
311 /* Baud rate specified for talking to serial target systems. Default
312 is left as -1, so targets can choose their own defaults. */
313 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
314 or (unsigned int)-1. This is a Bad User Interface. */
318 /* Timeout limit for response from target. */
320 int remote_timeout
= 20; /* Set default to 20 */
322 /* Non-zero tells remote* modules to output debugging info. */
324 int remote_debug
= 0;
326 /* Level of control structure. */
327 static int control_level
;
329 /* Structure for arguments to user defined functions. */
330 #define MAXUSERARGS 10
333 struct user_args
*next
;
342 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
346 #define STOP_SIGNAL SIGTSTP
347 static void stop_sig
PARAMS ((int));
351 /* Some System V have job control but not sigsetmask(). */
352 #if !defined (HAVE_SIGSETMASK)
354 #define HAVE_SIGSETMASK 1
356 #define HAVE_SIGSETMASK 0
360 #if 0 == (HAVE_SIGSETMASK)
361 #define sigsetmask(n)
364 /* Hooks for alternate command interfaces. */
366 /* Called after most modules have been initialized, but before taking users
369 void (*init_ui_hook
) PARAMS ((char *argv0
));
371 void (*ui_loop_hook
) PARAMS ((int));
374 /* Called instead of command_loop at top level. Can be invoked via
375 return_to_top_level. */
377 void (*command_loop_hook
) PARAMS ((void));
380 /* Called instead of fputs for all output. */
382 void (*fputs_unfiltered_hook
) PARAMS ((const char *linebuffer
, GDB_FILE
*stream
));
384 /* Called when the target says something to the host, which may
385 want to appear in a different window. */
387 void (*target_output_hook
) PARAMS ((char *));
389 /* Called from print_frame_info to list the line we stopped in. */
391 void (*print_frame_info_listing_hook
) PARAMS ((struct symtab
*s
, int line
,
392 int stopline
, int noerror
));
393 /* Replaces most of query. */
395 int (*query_hook
) PARAMS ((const char *, va_list));
397 /* Replaces most of warning. */
399 void (*warning_hook
) PARAMS ((const char *, va_list));
401 /* Called from gdb_flush to flush output. */
403 void (*flush_hook
) PARAMS ((GDB_FILE
*stream
));
405 /* These three functions support getting lines of text from the user. They
406 are used in sequence. First readline_begin_hook is called with a text
407 string that might be (for example) a message for the user to type in a
408 sequence of commands to be executed at a breakpoint. If this function
409 calls back to a GUI, it might take this opportunity to pop up a text
410 interaction window with this message. Next, readline_hook is called
411 with a prompt that is emitted prior to collecting the user input.
412 It can be called multiple times. Finally, readline_end_hook is called
413 to notify the GUI that we are done with the interaction window and it
416 void (*readline_begin_hook
) PARAMS ((char *, ...));
417 char * (*readline_hook
) PARAMS ((char *));
418 void (*readline_end_hook
) PARAMS ((void));
420 /* Called as appropriate to notify the interface of the specified breakpoint
423 void (*create_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
424 void (*delete_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
425 void (*modify_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
427 /* Called during long calculations to allow GUI to repair window damage, and to
428 check for stop buttons, etc... */
430 void (*interactive_hook
) PARAMS ((void));
432 /* Called when the registers have changed, as a hint to a GUI
433 to minimize window update. */
435 void (*registers_changed_hook
) PARAMS ((void));
437 /* tell the GUI someone changed the PC */
438 void (*pc_changed_hook
) PARAMS ((void));
440 /* Called when going to wait for the target. Usually allows the GUI to run
441 while waiting for target events. */
443 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
*status
));
445 /* Used by UI as a wrapper around command execution. May do various things
446 like enabling/disabling buttons, etc... */
448 void (*call_command_hook
) PARAMS ((struct cmd_list_element
*c
, char *cmd
,
451 /* Called when the current thread changes. Argument is thread id. */
453 void (*context_hook
) PARAMS ((int id
));
455 /* Takes control from error (). Typically used to prevent longjmps out of the
456 middle of the GUI. Usually used in conjunction with a catch routine. */
458 NORETURN
void (*error_hook
) PARAMS ((void)) ATTR_NORETURN
;
461 /* Where to go for return_to_top_level (RETURN_ERROR). */
462 SIGJMP_BUF error_return
;
463 /* Where to go for return_to_top_level (RETURN_QUIT). */
464 SIGJMP_BUF quit_return
;
466 /* Return for reason REASON. This generally gets back to the command
467 loop, but can be caught via catch_errors. */
470 return_to_top_level (reason
)
471 enum return_reason reason
;
476 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
477 I can think of a reason why that is vital, though). */
478 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
480 disable_current_display ();
481 do_cleanups (ALL_CLEANUPS
);
483 if (annotation_level
> 1)
494 (NORETURN
void) SIGLONGJMP
495 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
498 /* Call FUNC with arg ARGS, catching any errors. If there is no
499 error, return the value returned by FUNC. If there is an error,
500 print ERRSTRING, print the specific error message, then return
503 Must not be called with immediate_quit in effect (bad things might
504 happen, say we got a signal in the middle of a memcpy to quit_return).
505 This is an OK restriction; with very few exceptions immediate_quit can
506 be replaced by judicious use of QUIT.
508 MASK specifies what to catch; it is normally set to
509 RETURN_MASK_ALL, if for no other reason than that the code which
510 calls catch_errors might not be set up to deal with a quit which
511 isn't caught. But if the code can deal with it, it generally
512 should be RETURN_MASK_ERROR, unless for some reason it is more
513 useful to abort only the portion of the operation inside the
514 catch_errors. Note that quit should return to the command line
515 fairly quickly, even if some further processing is being done. */
518 catch_errors (func
, args
, errstring
, mask
)
519 catch_errors_ftype
*func
;
524 SIGJMP_BUF saved_error
;
525 SIGJMP_BUF saved_quit
;
528 struct cleanup
*saved_cleanup_chain
;
529 char *saved_error_pre_print
;
530 char *saved_quit_pre_print
;
532 saved_cleanup_chain
= save_cleanups ();
533 saved_error_pre_print
= error_pre_print
;
534 saved_quit_pre_print
= quit_pre_print
;
536 if (mask
& RETURN_MASK_ERROR
)
538 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (SIGJMP_BUF
));
539 error_pre_print
= errstring
;
541 if (mask
& RETURN_MASK_QUIT
)
543 memcpy (saved_quit
, quit_return
, sizeof (SIGJMP_BUF
));
544 quit_pre_print
= errstring
;
547 if (SIGSETJMP (tmp_jmp
) == 0)
549 if (mask
& RETURN_MASK_ERROR
)
550 memcpy (error_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
551 if (mask
& RETURN_MASK_QUIT
)
552 memcpy (quit_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
553 val
= (*func
) (args
);
558 restore_cleanups (saved_cleanup_chain
);
560 if (mask
& RETURN_MASK_ERROR
)
562 memcpy (error_return
, saved_error
, sizeof (SIGJMP_BUF
));
563 error_pre_print
= saved_error_pre_print
;
565 if (mask
& RETURN_MASK_QUIT
)
567 memcpy (quit_return
, saved_quit
, sizeof (SIGJMP_BUF
));
568 quit_pre_print
= saved_quit_pre_print
;
573 /* Handler for SIGHUP. */
580 catch_errors (quit_cover
, NULL
,
581 "Could not kill the program being debugged", RETURN_MASK_ALL
);
582 signal (SIGHUP
, SIG_DFL
);
583 kill (getpid (), SIGHUP
);
586 /* Just a little helper function for disconnect(). */
592 caution
= 0; /* Throw caution to the wind -- we're exiting.
593 This prevents asking the user dumb questions. */
594 quit_command((char *)0, 0);
597 #endif /* defined SIGHUP */
599 /* Line number we are currently in in a file which is being sourced. */
600 static int source_line_number
;
602 /* Name of the file we are sourcing. */
603 static char *source_file_name
;
605 /* Buffer containing the error_pre_print used by the source stuff.
607 static char *source_error
;
608 static int source_error_allocated
;
610 /* Something to glom on to the start of error_pre_print if source_file_name
612 static char *source_pre_error
;
614 /* Clean up on error during a "source" command (or execution of a
615 user-defined command). */
618 source_cleanup (stream
)
621 /* Restore the previous input stream. */
625 /* Read commands from STREAM. */
627 read_command_file (stream
)
630 struct cleanup
*cleanups
;
632 cleanups
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
635 do_cleanups (cleanups
);
638 extern void init_proc
PARAMS ((void));
640 void (*pre_init_ui_hook
) PARAMS ((void));
646 if (pre_init_ui_hook
)
649 /* Run the init function of each source file */
651 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
652 current_directory
= gdb_dirbuf
;
654 init_cmd_lists (); /* This needs to be done first */
655 initialize_targets (); /* Setup target_terminal macros for utils.c */
656 initialize_utils (); /* Make errors and warnings possible */
657 initialize_all_files ();
658 init_main (); /* But that omits this file! Do it now */
663 /* We need a default language for parsing expressions, so simple things like
664 "set width 0" won't fail if no language is explicitly set in a config file
665 or implicitly set by reading an executable during startup. */
666 set_language (language_c
);
667 expected_language
= current_language
; /* don't warn about the change. */
670 init_ui_hook (argv0
);
673 /* Allocate, initialize a new command line structure for one of the
674 control commands (if/while). */
676 static struct command_line
*
677 build_command_line (type
, args
)
678 enum command_control_type type
;
681 struct command_line
*cmd
;
684 error ("if/while commands require arguments.\n");
686 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
688 cmd
->control_type
= type
;
692 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
694 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
695 cmd
->line
= savestring (args
, strlen (args
));
699 /* Build and return a new command structure for the control commands
700 such as "if" and "while". */
702 static struct command_line
*
703 get_command_line (type
, arg
)
704 enum command_control_type type
;
707 struct command_line
*cmd
;
708 struct cleanup
*old_chain
= NULL
;
710 /* Allocate and build a new command line structure. */
711 cmd
= build_command_line (type
, arg
);
713 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
, &cmd
);
715 /* Read in the body of this command. */
716 if (recurse_read_control_structure (cmd
) == invalid_control
)
718 warning ("error reading in control structure\n");
719 do_cleanups (old_chain
);
723 discard_cleanups (old_chain
);
727 /* Recursively print a command (including full control structures). */
729 print_command_line (cmd
, depth
)
730 struct command_line
*cmd
;
737 for (i
= 0; i
< depth
; i
++)
738 fputs_filtered (" ", gdb_stdout
);
741 /* A simple command, print it and return. */
742 if (cmd
->control_type
== simple_control
)
744 fputs_filtered (cmd
->line
, gdb_stdout
);
745 fputs_filtered ("\n", gdb_stdout
);
749 /* loop_continue to jump to the start of a while loop, print it
751 if (cmd
->control_type
== continue_control
)
753 fputs_filtered ("loop_continue\n", gdb_stdout
);
757 /* loop_break to break out of a while loop, print it and return. */
758 if (cmd
->control_type
== break_control
)
760 fputs_filtered ("loop_break\n", gdb_stdout
);
764 /* A while command. Recursively print its subcommands before returning. */
765 if (cmd
->control_type
== while_control
)
767 struct command_line
*list
;
768 fputs_filtered ("while ", gdb_stdout
);
769 fputs_filtered (cmd
->line
, gdb_stdout
);
770 fputs_filtered ("\n", gdb_stdout
);
771 list
= *cmd
->body_list
;
774 print_command_line (list
, depth
+ 1);
779 /* An if command. Recursively print both arms before returning. */
780 if (cmd
->control_type
== if_control
)
782 fputs_filtered ("if ", gdb_stdout
);
783 fputs_filtered (cmd
->line
, gdb_stdout
);
784 fputs_filtered ("\n", gdb_stdout
);
786 print_command_line (cmd
->body_list
[0], depth
+ 1);
788 /* Show the false arm if it exists. */
789 if (cmd
->body_count
== 2)
793 for (i
= 0; i
< depth
; i
++)
794 fputs_filtered (" ", gdb_stdout
);
796 fputs_filtered ("else\n", gdb_stdout
);
797 print_command_line (cmd
->body_list
[1], depth
+ 1);
801 for (i
= 0; i
< depth
; i
++)
802 fputs_filtered (" ", gdb_stdout
);
804 fputs_filtered ("end\n", gdb_stdout
);
808 /* Execute the command in CMD. */
810 enum command_control_type
811 execute_control_command (cmd
)
812 struct command_line
*cmd
;
814 struct expression
*expr
;
815 struct command_line
*current
;
816 struct cleanup
*old_chain
= 0;
820 enum command_control_type ret
;
823 switch (cmd
->control_type
)
826 /* A simple command, execute it and return. */
827 new_line
= insert_args (cmd
->line
);
829 return invalid_control
;
830 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
832 execute_command (new_line
, 0);
833 ret
= cmd
->control_type
;
836 case continue_control
:
838 /* Return for "continue", and "break" so we can either
839 continue the loop at the top, or break out. */
840 ret
= cmd
->control_type
;
845 /* Parse the loop control expression for the while statement. */
846 new_line
= insert_args (cmd
->line
);
848 return invalid_control
;
849 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
851 expr
= parse_expression (new_line
);
852 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
854 ret
= simple_control
;
857 /* Keep iterating so long as the expression is true. */
864 /* Evaluate the expression. */
865 val_mark
= value_mark ();
866 val
= evaluate_expression (expr
);
867 cond_result
= value_true (val
);
868 value_free_to_mark (val_mark
);
870 /* If the value is false, then break out of the loop. */
874 /* Execute the body of the while statement. */
875 current
= *cmd
->body_list
;
878 ret
= execute_control_command (current
);
880 /* If we got an error, or a "break" command, then stop
882 if (ret
== invalid_control
|| ret
== break_control
)
888 /* If we got a "continue" command, then restart the loop
890 if (ret
== continue_control
)
893 /* Get the next statement. */
894 current
= current
->next
;
898 /* Reset RET so that we don't recurse the break all the way down. */
899 if (ret
== break_control
)
900 ret
= simple_control
;
907 new_line
= insert_args (cmd
->line
);
909 return invalid_control
;
910 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
912 /* Parse the conditional for the if statement. */
913 expr
= parse_expression (new_line
);
914 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
917 ret
= simple_control
;
919 /* Evaluate the conditional. */
920 val_mark
= value_mark ();
921 val
= evaluate_expression (expr
);
923 /* Choose which arm to take commands from based on the value of the
924 conditional expression. */
925 if (value_true (val
))
926 current
= *cmd
->body_list
;
927 else if (cmd
->body_count
== 2)
928 current
= *(cmd
->body_list
+ 1);
929 value_free_to_mark (val_mark
);
931 /* Execute commands in the given arm. */
934 ret
= execute_control_command (current
);
936 /* If we got an error, get out. */
937 if (ret
!= simple_control
)
940 /* Get the next statement in the body. */
941 current
= current
->next
;
948 warning ("Invalid control type in command structure.");
949 return invalid_control
;
953 do_cleanups (old_chain
);
958 /* "while" command support. Executes a body of statements while the
959 loop condition is nonzero. */
962 while_command (arg
, from_tty
)
966 struct command_line
*command
= NULL
;
969 command
= get_command_line (while_control
, arg
);
974 execute_control_command (command
);
975 free_command_lines (&command
);
978 /* "if" command support. Execute either the true or false arm depending
979 on the value of the if conditional. */
982 if_command (arg
, from_tty
)
986 struct command_line
*command
= NULL
;
989 command
= get_command_line (if_control
, arg
);
994 execute_control_command (command
);
995 free_command_lines (&command
);
1002 struct user_args
*oargs
= user_args
;
1004 fatal ("Internal error, arg_cleanup called with no user args.\n");
1006 user_args
= user_args
->next
;
1010 /* Bind the incomming arguments for a user defined command to
1011 $arg0, $arg1 ... $argMAXUSERARGS. */
1013 static struct cleanup
*
1017 struct user_args
*args
;
1018 struct cleanup
*old_chain
;
1019 unsigned int arg_count
= 0;
1021 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
1022 memset (args
, 0, sizeof (struct user_args
));
1024 args
->next
= user_args
;
1027 old_chain
= make_cleanup ((make_cleanup_func
) arg_cleanup
, 0);
1039 if (arg_count
>= MAXUSERARGS
)
1041 error ("user defined function may only have %d arguments.\n",
1046 /* Strip whitespace. */
1047 while (*p
== ' ' || *p
== '\t')
1050 /* P now points to an argument. */
1052 user_args
->a
[arg_count
].arg
= p
;
1054 /* Get to the end of this argument. */
1057 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
1063 else if (*p
== '\\')
1086 user_args
->a
[arg_count
].len
= p
- start_arg
;
1093 /* Given character string P, return a point to the first argument ($arg),
1094 or NULL if P contains no arguments. */
1100 while ((p
= strchr (p
, '$')))
1102 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1109 /* Insert the user defined arguments stored in user_arg into the $arg
1110 arguments found in line, with the updated copy being placed into nline. */
1116 char *p
, *save_line
, *new_line
;
1119 /* First we need to know how much memory to allocate for the new line. */
1122 while ((p
= locate_arg (line
)))
1127 if (i
>= user_args
->count
)
1129 error ("Missing argument %d in user function.\n", i
);
1132 len
+= user_args
->a
[i
].len
;
1136 /* Don't forget the tail. */
1137 len
+= strlen (line
);
1139 /* Allocate space for the new line and fill it in. */
1140 new_line
= (char *)xmalloc (len
+ 1);
1141 if (new_line
== NULL
)
1144 /* Restore pointer to beginning of old line. */
1147 /* Save pointer to beginning of new line. */
1148 save_line
= new_line
;
1150 while ((p
= locate_arg (line
)))
1154 memcpy (new_line
, line
, p
- line
);
1155 new_line
+= p
- line
;
1158 len
= user_args
->a
[i
].len
;
1161 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1166 /* Don't forget the tail. */
1167 strcpy (new_line
, line
);
1169 /* Return a pointer to the beginning of the new line. */
1174 execute_user_command (c
, args
)
1175 struct cmd_list_element
*c
;
1178 register struct command_line
*cmdlines
;
1179 struct cleanup
*old_chain
;
1180 enum command_control_type ret
;
1182 old_chain
= setup_user_args (args
);
1184 cmdlines
= c
->user_commands
;
1189 /* Set the instream to 0, indicating execution of a
1190 user-defined function. */
1191 old_chain
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
1192 instream
= (FILE *) 0;
1195 ret
= execute_control_command (cmdlines
);
1196 if (ret
!= simple_control
&& ret
!= break_control
)
1198 warning ("Error in control structure.\n");
1201 cmdlines
= cmdlines
->next
;
1203 do_cleanups (old_chain
);
1206 /* Execute the line P as a command.
1207 Pass FROM_TTY as second argument to the defining function. */
1210 execute_command (p
, from_tty
)
1214 register struct cmd_list_element
*c
;
1215 register enum language flang
;
1216 static int warned
= 0;
1217 /* FIXME: These should really be in an appropriate header file */
1218 extern void serial_log_command
PARAMS ((const char *));
1222 /* Force cleanup of any alloca areas if using C alloca instead of
1223 a builtin alloca. */
1226 /* This can happen when command_line_input hits end of file. */
1230 serial_log_command (p
);
1232 while (*p
== ' ' || *p
== '\t') p
++;
1237 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1238 /* Pass null arg rather than an empty one. */
1241 /* Clear off trailing whitespace, except for set and complete command. */
1242 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1244 p
= arg
+ strlen (arg
) - 1;
1245 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1250 /* If this command has been hooked, run the hook first. */
1252 execute_user_command (c
->hook
, (char *)0);
1254 if (c
->class == class_user
)
1255 execute_user_command (c
, arg
);
1256 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1257 do_setshow_command (arg
, from_tty
& caution
, c
);
1258 else if (c
->function
.cfunc
== NO_FUNCTION
)
1259 error ("That is not a command, just a help topic.");
1260 else if (call_command_hook
)
1261 call_command_hook (c
, arg
, from_tty
& caution
);
1263 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1266 /* Tell the user if the language has changed (except first time). */
1267 if (current_language
!= expected_language
)
1269 if (language_mode
== language_mode_auto
) {
1270 language_info (1); /* Print what changed. */
1275 /* Warn the user if the working language does not match the
1276 language of the current frame. Only warn the user if we are
1277 actually running the program, i.e. there is a stack. */
1278 /* FIXME: This should be cacheing the frame and only running when
1279 the frame changes. */
1281 if (target_has_stack
)
1283 flang
= get_frame_language ();
1285 && flang
!= language_unknown
1286 && flang
!= current_language
->la_language
)
1288 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1296 command_loop_marker (foo
)
1301 /* Read commands from `instream' and execute them
1302 until end of file or error reading instream. */
1307 struct cleanup
*old_chain
;
1309 int stdin_is_tty
= ISATTY (stdin
);
1310 long time_at_cmd_start
;
1312 long space_at_cmd_start
= 0;
1314 extern int display_time
;
1315 extern int display_space
;
1317 while (instream
&& !feof (instream
))
1320 extern int insert_mode
;
1322 if (window_hook
&& instream
== stdin
)
1323 (*window_hook
) (instream
, prompt
);
1326 if (instream
== stdin
&& stdin_is_tty
)
1327 reinitialize_more_filter ();
1328 old_chain
= make_cleanup ((make_cleanup_func
) command_loop_marker
, 0);
1331 /* A bit of paranoia: I want to make sure the "insert_mode" global
1332 * is clear except when it is being used for command-line editing
1333 * (see tuiIO.c, utils.c); otherwise normal output will
1334 * get messed up in the TUI. So clear it before/after
1335 * the command-line-input call. - RT
1339 /* Get a command-line. This calls the readline package. */
1340 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1341 instream
== stdin
, "prompt");
1348 time_at_cmd_start
= get_run_time ();
1353 extern char **environ
;
1354 char *lim
= (char *) sbrk (0);
1356 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1360 execute_command (command
, instream
== stdin
);
1361 /* Do any commands attached to breakpoint we stopped at. */
1362 bpstat_do_actions (&stop_bpstat
);
1363 do_cleanups (old_chain
);
1367 long cmd_time
= get_run_time () - time_at_cmd_start
;
1369 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1370 cmd_time
/ 1000000, cmd_time
% 1000000);
1376 extern char **environ
;
1377 char *lim
= (char *) sbrk (0);
1378 long space_now
= lim
- (char *) &environ
;
1379 long space_diff
= space_now
- space_at_cmd_start
;
1381 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1383 (space_diff
>= 0 ? '+' : '-'),
1390 /* Commands call this if they do not want to be repeated by null lines. */
1398 /* If we aren't reading from standard input, we are saving the last
1399 thing read from stdin in line and don't want to delete it. Null lines
1400 won't repeat here in any case. */
1401 if (instream
== stdin
)
1405 /* Read a line from the stream "instream" without command line editing.
1407 It prints PRROMPT once at the start.
1408 Action is compatible with "readline", e.g. space for the result is
1409 malloc'd and should be freed by the caller.
1411 A NULL return means end of file. */
1413 gdb_readline (prrompt
)
1418 int input_index
= 0;
1419 int result_size
= 80;
1423 /* Don't use a _filtered function here. It causes the assumed
1424 character position to be off, since the newline we read from
1425 the user is not accounted for. */
1426 fputs_unfiltered (prrompt
, gdb_stdout
);
1428 /* Move to a new line so the entered line doesn't have a prompt
1429 on the front of it. */
1430 fputs_unfiltered ("\n", gdb_stdout
);
1432 gdb_flush (gdb_stdout
);
1435 result
= (char *) xmalloc (result_size
);
1439 /* Read from stdin if we are executing a user defined command.
1440 This is the right thing for prompt_for_continue, at least. */
1441 c
= fgetc (instream
? instream
: stdin
);
1445 if (input_index
> 0)
1446 /* The last line does not end with a newline. Return it, and
1447 if we are called again fgetc will still return EOF and
1448 we'll return NULL then. */
1455 #ifndef CRLF_SOURCE_FILES
1459 if (input_index
> 0 && result
[input_index
- 1] == '\r')
1465 result
[input_index
++] = c
;
1466 while (input_index
>= result_size
)
1469 result
= (char *) xrealloc (result
, result_size
);
1473 result
[input_index
++] = '\0';
1477 /* Variables which control command line editing and history
1478 substitution. These variables are given default values at the end
1480 static int command_editing_p
;
1481 static int history_expansion_p
;
1482 static int write_history_p
;
1483 static int history_size
;
1484 static char *history_filename
;
1486 /* readline uses the word breaks for two things:
1487 (1) In figuring out where to point the TEXT parameter to the
1488 rl_completion_entry_function. Since we don't use TEXT for much,
1489 it doesn't matter a lot what the word breaks are for this purpose, but
1490 it does affect how much stuff M-? lists.
1491 (2) If one of the matches contains a word break character, readline
1492 will quote it. That's why we switch between
1493 gdb_completer_word_break_characters and
1494 gdb_completer_command_word_break_characters. I'm not sure when
1495 we need this behavior (perhaps for funky characters in C++ symbols?). */
1497 /* Variables which are necessary for fancy command line editing. */
1498 char *gdb_completer_word_break_characters
=
1499 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1501 /* When completing on command names, we remove '-' from the list of
1502 word break characters, since we use it in command names. If the
1503 readline library sees one in any of the current completion strings,
1504 it thinks that the string needs to be quoted and automatically supplies
1506 char *gdb_completer_command_word_break_characters
=
1507 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1509 /* Characters that can be used to quote completion strings. Note that we
1510 can't include '"' because the gdb C parser treats such quoted sequences
1512 char *gdb_completer_quote_characters
=
1515 /* Functions that are used as part of the fancy command line editing. */
1517 /* This can be used for functions which don't want to complete on symbols
1518 but don't want to complete on anything else either. */
1521 noop_completer (text
, prefix
)
1528 /* Complete on filenames. */
1530 filename_completer (text
, word
)
1534 /* From readline. */
1535 extern char *filename_completion_function
PARAMS ((char *, int));
1536 int subsequent_name
;
1538 int return_val_used
;
1539 int return_val_alloced
;
1541 return_val_used
= 0;
1542 /* Small for testing. */
1543 return_val_alloced
= 1;
1544 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1546 subsequent_name
= 0;
1550 p
= filename_completion_function (text
, subsequent_name
);
1551 if (return_val_used
>= return_val_alloced
)
1553 return_val_alloced
*= 2;
1555 (char **) xrealloc (return_val
,
1556 return_val_alloced
* sizeof (char *));
1560 return_val
[return_val_used
++] = p
;
1563 /* Like emacs, don't complete on old versions. Especially useful
1564 in the "source" command. */
1565 if (p
[strlen (p
) - 1] == '~')
1571 /* Return exactly p. */
1572 return_val
[return_val_used
++] = p
;
1573 else if (word
> text
)
1575 /* Return some portion of p. */
1576 q
= xmalloc (strlen (p
) + 5);
1577 strcpy (q
, p
+ (word
- text
));
1578 return_val
[return_val_used
++] = q
;
1583 /* Return some of TEXT plus p. */
1584 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1585 strncpy (q
, word
, text
- word
);
1586 q
[text
- word
] = '\0';
1588 return_val
[return_val_used
++] = q
;
1592 subsequent_name
= 1;
1595 /* There is no way to do this just long enough to affect quote inserting
1596 without also affecting the next completion. This should be fixed in
1598 /* Insure that readline does the right thing
1599 with respect to inserting quotes. */
1600 rl_completer_word_break_characters
= "";
1605 /* Here are some useful test cases for completion. FIXME: These should
1606 be put in the test suite. They should be tested with both M-? and TAB.
1608 "show output-" "radix"
1609 "show output" "-radix"
1610 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1611 "p " ambiguous (all symbols)
1612 "info t foo" no completions
1613 "info t " no completions
1614 "info t" ambiguous ("info target", "info terminal", etc.)
1615 "info ajksdlfk" no completions
1616 "info ajksdlfk " no completions
1618 "info " ambiguous (all info commands)
1619 "p \"a" no completions (string constant)
1620 "p 'a" ambiguous (all symbols starting with a)
1621 "p b-a" ambiguous (all symbols starting with a)
1622 "p b-" ambiguous (all symbols)
1623 "file Make" "file" (word break hard to screw up here)
1624 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1627 /* Generate completions one by one for the completer. Each time we are
1628 called return another potential completion to the caller.
1629 line_completion just completes on commands or passes the buck to the
1630 command's completer function, the stuff specific to symbol completion
1631 is in make_symbol_completion_list.
1633 TEXT is the caller's idea of the "word" we are looking at.
1635 MATCHES is the number of matches that have currently been collected from
1636 calling this completion function. When zero, then we need to initialize,
1637 otherwise the initialization has already taken place and we can just
1638 return the next potential completion string.
1640 LINE_BUFFER is available to be looked at; it contains the entire text
1641 of the line. POINT is the offset in that line of the cursor. You
1642 should pretend that the line ends at POINT.
1644 Returns NULL if there are no more completions, else a pointer to a string
1645 which is a possible completion, it is the caller's responsibility to
1649 line_completion_function (text
, matches
, line_buffer
, point
)
1655 static char **list
= (char **)NULL
; /* Cache of completions */
1656 static int index
; /* Next cached completion */
1657 char *output
= NULL
;
1658 char *tmp_command
, *p
;
1659 /* Pointer within tmp_command which corresponds to text. */
1661 struct cmd_list_element
*c
, *result_list
;
1665 /* The caller is beginning to accumulate a new set of completions, so
1666 we need to find all of them now, and cache them for returning one at
1667 a time on future calls. */
1671 /* Free the storage used by LIST, but not by the strings inside.
1672 This is because rl_complete_internal () frees the strings. */
1678 /* Choose the default set of word break characters to break completions.
1679 If we later find out that we are doing completions on command strings
1680 (as opposed to strings supplied by the individual command completer
1681 functions, which can be any string) then we will switch to the
1682 special word break set for command strings, which leaves out the
1683 '-' character used in some commands. */
1685 rl_completer_word_break_characters
=
1686 gdb_completer_word_break_characters
;
1688 /* Decide whether to complete on a list of gdb commands or on symbols. */
1689 tmp_command
= (char *) alloca (point
+ 1);
1692 strncpy (tmp_command
, line_buffer
, point
);
1693 tmp_command
[point
] = '\0';
1694 /* Since text always contains some number of characters leading up
1695 to point, we can find the equivalent position in tmp_command
1696 by subtracting that many characters from the end of tmp_command. */
1697 word
= tmp_command
+ point
- strlen (text
);
1701 /* An empty line we want to consider ambiguous; that is, it
1702 could be any command. */
1703 c
= (struct cmd_list_element
*) -1;
1708 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1711 /* Move p up to the next interesting thing. */
1712 while (*p
== ' ' || *p
== '\t')
1719 /* It is an unrecognized command. So there are no
1720 possible completions. */
1723 else if (c
== (struct cmd_list_element
*) -1)
1727 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1728 doesn't advance over that thing itself. Do so now. */
1730 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1732 if (q
!= tmp_command
+ point
)
1734 /* There is something beyond the ambiguous
1735 command, so there are no possible completions. For
1736 example, "info t " or "info t foo" does not complete
1737 to anything, because "info t" can be "info target" or
1743 /* We're trying to complete on the command which was ambiguous.
1744 This we can deal with. */
1747 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1752 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1754 /* Insure that readline does the right thing with respect to
1755 inserting quotes. */
1756 rl_completer_word_break_characters
=
1757 gdb_completer_command_word_break_characters
;
1762 /* We've recognized a full command. */
1764 if (p
== tmp_command
+ point
)
1766 /* There is no non-whitespace in the line beyond the command. */
1768 if (p
[-1] == ' ' || p
[-1] == '\t')
1770 /* The command is followed by whitespace; we need to complete
1771 on whatever comes after command. */
1774 /* It is a prefix command; what comes after it is
1775 a subcommand (e.g. "info "). */
1776 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1778 /* Insure that readline does the right thing
1779 with respect to inserting quotes. */
1780 rl_completer_word_break_characters
=
1781 gdb_completer_command_word_break_characters
;
1785 list
= complete_on_enum (c
->enums
, p
, word
);
1786 rl_completer_word_break_characters
=
1787 gdb_completer_command_word_break_characters
;
1791 /* It is a normal command; what comes after it is
1792 completed by the command's completer function. */
1793 list
= (*c
->completer
) (p
, word
);
1798 /* The command is not followed by whitespace; we need to
1799 complete on the command itself. e.g. "p" which is a
1800 command itself but also can complete to "print", "ptype"
1804 /* Find the command we are completing on. */
1806 while (q
> tmp_command
)
1808 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1814 list
= complete_on_cmdlist (result_list
, q
, word
);
1816 /* Insure that readline does the right thing
1817 with respect to inserting quotes. */
1818 rl_completer_word_break_characters
=
1819 gdb_completer_command_word_break_characters
;
1824 /* There is non-whitespace beyond the command. */
1826 if (c
->prefixlist
&& !c
->allow_unknown
)
1828 /* It is an unrecognized subcommand of a prefix command,
1829 e.g. "info adsfkdj". */
1834 list
= complete_on_enum (c
->enums
, p
, word
);
1838 /* It is a normal command. */
1839 list
= (*c
->completer
) (p
, word
);
1845 /* If we found a list of potential completions during initialization then
1846 dole them out one at a time. The vector of completions is NULL
1847 terminated, so after returning the last one, return NULL (and continue
1848 to do so) each time we are called after that, until a new list is
1853 output
= list
[index
];
1861 /* Can't do this because readline hasn't yet checked the word breaks
1862 for figuring out whether to insert a quote. */
1864 /* Make sure the word break characters are set back to normal for the
1865 next time that readline tries to complete something. */
1866 rl_completer_word_break_characters
=
1867 gdb_completer_word_break_characters
;
1873 /* Line completion interface function for readline. */
1876 readline_line_completion_function (text
, matches
)
1880 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1883 /* Skip over a possibly quoted word (as defined by the quote characters
1884 and word break characters the completer uses). Returns pointer to the
1885 location after the "word". */
1891 char quote_char
= '\0';
1894 for (scan
= str
; *scan
!= '\0'; scan
++)
1896 if (quote_char
!= '\0')
1898 /* Ignore everything until the matching close quote char */
1899 if (*scan
== quote_char
)
1901 /* Found matching close quote. */
1906 else if (strchr (gdb_completer_quote_characters
, *scan
))
1908 /* Found start of a quoted string. */
1911 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1925 #if STOP_SIGNAL == SIGTSTP
1926 signal (SIGTSTP
, SIG_DFL
);
1928 kill (getpid (), SIGTSTP
);
1929 signal (SIGTSTP
, stop_sig
);
1931 signal (STOP_SIGNAL
, stop_sig
);
1933 printf_unfiltered ("%s", prompt
);
1934 gdb_flush (gdb_stdout
);
1936 /* Forget about any previous command -- null line now will do nothing. */
1939 #endif /* STOP_SIGNAL */
1941 /* Initialize signal handlers. */
1946 /* Under System V the default disposition of a signal is reinstated after
1947 the signal is caught and delivered to an application process. On such
1948 systems one must restore the replacement signal handler if one wishes
1949 to continue handling the signal in one's program. On BSD systems this
1950 is not needed but it is harmless, and it simplifies the code to just do
1951 it unconditionally. */
1952 signal (signo
, do_nothing
);
1958 signal (SIGINT
, request_quit
);
1960 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1961 to the inferior and breakpoints will be ignored. */
1963 signal (SIGTRAP
, SIG_DFL
);
1966 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1967 passed to the inferior, which we don't want. It would be
1968 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1969 on BSD4.3 systems using vfork, that can affect the
1970 GDB process as well as the inferior (the signal handling tables
1971 might be in memory, shared between the two). Since we establish
1972 a handler for SIGQUIT, when we call exec it will set the signal
1973 to SIG_DFL for us. */
1974 signal (SIGQUIT
, do_nothing
);
1976 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1977 signal (SIGHUP
, disconnect
);
1979 signal (SIGFPE
, float_handler
);
1981 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1982 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1986 /* Read one line from the command input stream `instream'
1987 into the local static buffer `linebuffer' (whose current length
1989 The buffer is made bigger as necessary.
1990 Returns the address of the start of the line.
1992 NULL is returned for end of file.
1994 *If* the instream == stdin & stdin is a terminal, the line read
1995 is copied into the file line saver (global var char *line,
1996 length linesize) so that it can be duplicated.
1998 This routine either uses fancy command line editing or
1999 simple input as the user has requested. */
2002 command_line_input (prrompt
, repeat
, annotation_suffix
)
2005 char *annotation_suffix
;
2007 static char *linebuffer
= 0;
2008 static unsigned linelength
= 0;
2012 char *local_prompt
= prrompt
;
2016 /* The annotation suffix must be non-NULL. */
2017 if (annotation_suffix
== NULL
)
2018 annotation_suffix
= "";
2020 if (annotation_level
> 1 && instream
== stdin
)
2022 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
2023 + strlen (annotation_suffix
) + 40);
2024 if (prrompt
== NULL
)
2025 local_prompt
[0] = '\0';
2027 strcpy (local_prompt
, prrompt
);
2028 strcat (local_prompt
, "\n\032\032");
2029 strcat (local_prompt
, annotation_suffix
);
2030 strcat (local_prompt
, "\n");
2033 if (linebuffer
== 0)
2036 linebuffer
= (char *) xmalloc (linelength
);
2041 /* Control-C quits instantly if typed while in this loop
2042 since it should not wait until the user types a newline. */
2046 signal (STOP_SIGNAL
, stop_sig
);
2051 /* Make sure that all output has been output. Some machines may let
2052 you get away with leaving out some of the gdb_flush, but not all. */
2054 gdb_flush (gdb_stdout
);
2055 gdb_flush (gdb_stderr
);
2057 if (source_file_name
!= NULL
)
2059 ++source_line_number
;
2060 sprintf (source_error
,
2061 "%s%s:%d: Error in sourced command file:\n",
2064 source_line_number
);
2065 error_pre_print
= source_error
;
2068 if (annotation_level
> 1 && instream
== stdin
)
2070 printf_unfiltered ("\n\032\032pre-");
2071 printf_unfiltered (annotation_suffix
);
2072 printf_unfiltered ("\n");
2075 /* Don't use fancy stuff if not talking to stdin. */
2076 if (readline_hook
&& instream
== NULL
)
2078 rl
= (*readline_hook
) (local_prompt
);
2080 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
2082 rl
= readline (local_prompt
);
2086 rl
= gdb_readline (local_prompt
);
2089 if (annotation_level
> 1 && instream
== stdin
)
2091 printf_unfiltered ("\n\032\032post-");
2092 printf_unfiltered (annotation_suffix
);
2093 printf_unfiltered ("\n");
2096 if (!rl
|| rl
== (char *) EOF
)
2101 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
2103 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
2104 nline
= (char *) xrealloc (linebuffer
, linelength
);
2105 p
+= nline
- linebuffer
;
2109 /* Copy line. Don't copy null at end. (Leaves line alone
2110 if this was just a newline) */
2114 free (rl
); /* Allocated in readline. */
2116 if (p
== linebuffer
|| *(p
- 1) != '\\')
2119 p
--; /* Put on top of '\'. */
2120 local_prompt
= (char *) 0;
2125 signal (STOP_SIGNAL
, SIG_DFL
);
2132 #define SERVER_COMMAND_LENGTH 7
2134 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2135 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2138 /* Note that we don't set `line'. Between this and the check in
2139 dont_repeat, this insures that repeating will still do the
2142 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2145 /* Do history expansion if that is wished. */
2146 if (history_expansion_p
&& instream
== stdin
2147 && ISATTY (instream
))
2149 char *history_value
;
2152 *p
= '\0'; /* Insert null now. */
2153 expanded
= history_expand (linebuffer
, &history_value
);
2156 /* Print the changes. */
2157 printf_unfiltered ("%s\n", history_value
);
2159 /* If there was an error, call this function again. */
2162 free (history_value
);
2163 return command_line_input (prrompt
, repeat
, annotation_suffix
);
2165 if (strlen (history_value
) > linelength
)
2167 linelength
= strlen (history_value
) + 1;
2168 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2170 strcpy (linebuffer
, history_value
);
2171 p
= linebuffer
+ strlen(linebuffer
);
2172 free (history_value
);
2176 /* If we just got an empty line, and that is supposed
2177 to repeat the previous command, return the value in the
2179 if (repeat
&& p
== linebuffer
)
2181 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++) ;
2187 /* Add line to history if appropriate. */
2188 if (instream
== stdin
2189 && ISATTY (stdin
) && *linebuffer
)
2190 add_history (linebuffer
);
2192 /* Note: lines consisting solely of comments are added to the command
2193 history. This is useful when you type a command, and then
2194 realize you don't want to execute it quite yet. You can comment
2195 out the command and then later fetch it from the value history
2196 and remove the '#'. The kill ring is probably better, but some
2197 people are in the habit of commenting things out. */
2199 *p1
= '\0'; /* Found a comment. */
2201 /* Save into global buffer if appropriate. */
2204 if (linelength
> linesize
)
2206 line
= xrealloc (line
, linelength
);
2207 linesize
= linelength
;
2209 strcpy (line
, linebuffer
);
2217 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2218 code bodies. This is typically used when we encounter an "else"
2219 clause for an "if" command. */
2222 realloc_body_list (command
, new_length
)
2223 struct command_line
*command
;
2227 struct command_line
**body_list
;
2229 n
= command
->body_count
;
2231 /* Nothing to do? */
2232 if (new_length
<= n
)
2235 body_list
= (struct command_line
**)
2236 xmalloc (sizeof (struct command_line
*) * new_length
);
2238 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2240 free (command
->body_list
);
2241 command
->body_list
= body_list
;
2242 command
->body_count
= new_length
;
2245 /* Read one line from the input stream. If the command is an "else" or
2246 "end", return such an indication to the caller. */
2248 static enum misc_command_type
2249 read_next_line (command
)
2250 struct command_line
**command
;
2252 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2255 if (control_level
>= 254)
2256 error ("Control nesting too deep!\n");
2258 /* Set a prompt based on the nesting of the control commands. */
2259 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2261 for (i
= 0; i
< control_level
; i
++)
2262 control_prompt
[i
] = ' ';
2263 control_prompt
[i
] = '>';
2264 control_prompt
[i
+1] = '\0';
2265 prompt_ptr
= (char *)&control_prompt
[0];
2270 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2272 /* Not sure what to do here. */
2276 /* Strip leading and trailing whitespace. */
2277 while (*p
== ' ' || *p
== '\t')
2280 p1
= p
+ strlen (p
);
2281 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2284 /* Blanks and comments don't really do anything, but we need to
2285 distinguish them from else, end and other commands which can be
2287 if (p1
== p
|| p
[0] == '#')
2290 /* Is this the end of a simple, while, or if control structure? */
2291 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2294 /* Is the else clause of an if control structure? */
2295 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2296 return else_command
;
2298 /* Check for while, if, break, continue, etc and build a new command
2299 line structure for them. */
2300 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2301 *command
= build_command_line (while_control
, p
+ 6);
2302 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2303 *command
= build_command_line (if_control
, p
+ 3);
2304 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2306 *command
= (struct command_line
*)
2307 xmalloc (sizeof (struct command_line
));
2308 (*command
)->next
= NULL
;
2309 (*command
)->line
= NULL
;
2310 (*command
)->control_type
= break_control
;
2311 (*command
)->body_count
= 0;
2312 (*command
)->body_list
= NULL
;
2314 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2316 *command
= (struct command_line
*)
2317 xmalloc (sizeof (struct command_line
));
2318 (*command
)->next
= NULL
;
2319 (*command
)->line
= NULL
;
2320 (*command
)->control_type
= continue_control
;
2321 (*command
)->body_count
= 0;
2322 (*command
)->body_list
= NULL
;
2326 /* A normal command. */
2327 *command
= (struct command_line
*)
2328 xmalloc (sizeof (struct command_line
));
2329 (*command
)->next
= NULL
;
2330 (*command
)->line
= savestring (p
, p1
- p
);
2331 (*command
)->control_type
= simple_control
;
2332 (*command
)->body_count
= 0;
2333 (*command
)->body_list
= NULL
;
2336 /* Nothing special. */
2340 /* Recursively read in the control structures and create a command_line
2341 structure from them.
2343 The parent_control parameter is the control structure in which the
2344 following commands are nested. */
2346 static enum command_control_type
2347 recurse_read_control_structure (current_cmd
)
2348 struct command_line
*current_cmd
;
2350 int current_body
, i
;
2351 enum misc_command_type val
;
2352 enum command_control_type ret
;
2353 struct command_line
**body_ptr
, *child_tail
, *next
;
2358 /* Sanity checks. */
2359 if (current_cmd
->control_type
== simple_control
)
2361 error ("Recursed on a simple control type\n");
2362 return invalid_control
;
2365 if (current_body
> current_cmd
->body_count
)
2367 error ("Allocated body is smaller than this command type needs\n");
2368 return invalid_control
;
2371 /* Read lines from the input stream and build control structures. */
2377 val
= read_next_line (&next
);
2379 /* Just skip blanks and comments. */
2380 if (val
== nop_command
)
2383 if (val
== end_command
)
2385 if (current_cmd
->control_type
== while_control
2386 || current_cmd
->control_type
== if_control
)
2388 /* Success reading an entire control structure. */
2389 ret
= simple_control
;
2394 ret
= invalid_control
;
2399 /* Not the end of a control structure. */
2400 if (val
== else_command
)
2402 if (current_cmd
->control_type
== if_control
2403 && current_body
== 1)
2405 realloc_body_list (current_cmd
, 2);
2412 ret
= invalid_control
;
2419 child_tail
->next
= next
;
2423 body_ptr
= current_cmd
->body_list
;
2424 for (i
= 1; i
< current_body
; i
++)
2433 /* If the latest line is another control structure, then recurse
2435 if (next
->control_type
== while_control
2436 || next
->control_type
== if_control
)
2439 ret
= recurse_read_control_structure (next
);
2442 if (ret
!= simple_control
)
2452 /* Read lines from the input stream and accumulate them in a chain of
2453 struct command_line's, which is then returned. For input from a
2454 terminal, the special command "end" is used to mark the end of the
2455 input, and is not included in the returned chain of commands. */
2457 #define END_MESSAGE "End with a line saying just \"end\"."
2459 struct command_line
*
2460 read_command_lines (prompt
, from_tty
)
2464 struct command_line
*head
, *tail
, *next
;
2465 struct cleanup
*old_chain
;
2466 enum command_control_type ret
;
2467 enum misc_command_type val
;
2469 if (readline_begin_hook
)
2471 /* Note - intentional to merge messages with no newline */
2472 (*readline_begin_hook
) ("%s %s\n", prompt
, END_MESSAGE
);
2474 else if (from_tty
&& input_from_terminal_p ())
2476 printf_unfiltered ("%s\n%s\n", prompt
, END_MESSAGE
);
2477 gdb_flush (gdb_stdout
);
2485 val
= read_next_line (&next
);
2487 /* Ignore blank lines or comments. */
2488 if (val
== nop_command
)
2491 if (val
== end_command
)
2493 ret
= simple_control
;
2497 if (val
!= ok_command
)
2499 ret
= invalid_control
;
2503 if (next
->control_type
== while_control
2504 || next
->control_type
== if_control
)
2507 ret
= recurse_read_control_structure (next
);
2510 if (ret
== invalid_control
)
2521 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
,
2531 if (ret
!= invalid_control
)
2533 discard_cleanups (old_chain
);
2536 do_cleanups (old_chain
);
2539 if (readline_end_hook
)
2541 (*readline_end_hook
) ();
2546 /* Free a chain of struct command_line's. */
2549 free_command_lines (lptr
)
2550 struct command_line
**lptr
;
2552 register struct command_line
*l
= *lptr
;
2553 register struct command_line
*next
;
2554 struct command_line
**blist
;
2559 if (l
->body_count
> 0)
2561 blist
= l
->body_list
;
2562 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2563 free_command_lines (blist
);
2572 /* Add an element to the list of info subcommands. */
2575 add_info (name
, fun
, doc
)
2577 void (*fun
) PARAMS ((char *, int));
2580 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2583 /* Add an alias to the list of info subcommands. */
2586 add_info_alias (name
, oldname
, abbrev_flag
)
2591 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2594 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2595 Therefore, its own definition is called only for "info" with no args. */
2599 info_command (arg
, from_tty
)
2603 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2604 help_list (infolist
, "info ", -1, gdb_stdout
);
2607 /* The "complete" command is used by Emacs to implement completion. */
2611 complete_command (arg
, from_tty
)
2623 argpoint
= strlen (arg
);
2625 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2627 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2629 printf_unfiltered ("%s\n", completion
);
2634 /* The "show" command with no arguments shows all the settings. */
2638 show_command (arg
, from_tty
)
2642 cmd_show_list (showlist
, from_tty
, "");
2645 /* Add an element to the list of commands. */
2648 add_com (name
, class, fun
, doc
)
2650 enum command_class
class;
2651 void (*fun
) PARAMS ((char *, int));
2654 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2657 /* Add an alias or abbreviation command to the list of commands. */
2660 add_com_alias (name
, oldname
, class, abbrev_flag
)
2663 enum command_class
class;
2666 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2673 error ("Argument required (%s).", why
);
2678 help_command (command
, from_tty
)
2680 int from_tty
; /* Ignored */
2682 help_cmd (command
, gdb_stdout
);
2686 validate_comname (comname
)
2692 error_no_arg ("name of command to define");
2697 if (!isalnum(*p
) && *p
!= '-' && *p
!= '_')
2698 error ("Junk in argument list: \"%s\"", p
);
2703 /* This is just a placeholder in the command data structures. */
2705 user_defined_command (ignore
, from_tty
)
2712 define_command (comname
, from_tty
)
2716 register struct command_line
*cmds
;
2717 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2718 char *tem
= comname
;
2720 #define HOOK_STRING "hook-"
2723 validate_comname (comname
);
2725 /* Look it up, and verify that we got an exact match. */
2726 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2727 if (c
&& !STREQ (comname
, c
->name
))
2732 if (c
->class == class_user
|| c
->class == class_alias
)
2733 tem
= "Redefine command \"%s\"? ";
2735 tem
= "Really redefine built-in command \"%s\"? ";
2736 if (!query (tem
, c
->name
))
2737 error ("Command \"%s\" not redefined.", c
->name
);
2740 /* If this new command is a hook, then mark the command which it
2741 is hooking. Note that we allow hooking `help' commands, so that
2742 we can hook the `stop' pseudo-command. */
2744 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2746 /* Look up cmd it hooks, and verify that we got an exact match. */
2747 tem
= comname
+HOOK_LEN
;
2748 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2749 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2753 warning ("Your new `%s' command does not hook any existing command.",
2755 if (!query ("Proceed? "))
2756 error ("Not confirmed.");
2760 comname
= savestring (comname
, strlen (comname
));
2762 /* If the rest of the commands will be case insensitive, this one
2763 should behave in the same manner. */
2764 for (tem
= comname
; *tem
; tem
++)
2765 if (isupper(*tem
)) *tem
= tolower(*tem
);
2768 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
2769 cmds
= read_command_lines (tmpbuf
, from_tty
);
2771 if (c
&& c
->class == class_user
)
2772 free_command_lines (&c
->user_commands
);
2774 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2775 (c
&& c
->class == class_user
)
2776 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2777 newc
->user_commands
= cmds
;
2779 /* If this new command is a hook, then mark both commands as being
2783 hookc
->hook
= newc
; /* Target gets hooked. */
2784 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2789 document_command (comname
, from_tty
)
2793 struct command_line
*doclines
;
2794 register struct cmd_list_element
*c
;
2795 char *tem
= comname
;
2798 validate_comname (comname
);
2800 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2802 if (c
->class != class_user
)
2803 error ("Command \"%s\" is built-in.", comname
);
2805 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
2806 doclines
= read_command_lines (tmpbuf
, from_tty
);
2808 if (c
->doc
) free (c
->doc
);
2811 register struct command_line
*cl1
;
2812 register int len
= 0;
2814 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2815 len
+= strlen (cl1
->line
) + 1;
2817 c
->doc
= (char *) xmalloc (len
+ 1);
2820 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2822 strcat (c
->doc
, cl1
->line
);
2824 strcat (c
->doc
, "\n");
2828 free_command_lines (&doclines
);
2831 /* Print the GDB banner. */
2833 print_gdb_version (stream
)
2836 /* From GNU coding standards, first line is meant to be easy for a
2837 program to parse, and is just canonical program name and version
2838 number, which starts after last space. */
2840 fprintf_filtered (stream
, "GNU gdb %s\n", version
);
2842 /* Second line is a copyright notice. */
2844 fprintf_filtered (stream
, "Copyright 1998 Free Software Foundation, Inc.\n");
2846 /* Following the copyright is a brief statement that the program is
2847 free software, that users are free to copy and change it on
2848 certain conditions, that it is covered by the GNU GPL, and that
2849 there is no warranty. */
2851 fprintf_filtered (stream
, "\
2852 GDB is free software, covered by the GNU General Public License, and you are\n\
2853 welcome to change it and/or distribute copies of it under certain conditions.\n\
2854 Type \"show copying\" to see the conditions.\n\
2855 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2857 /* After the required info we print the configuration information. */
2859 fprintf_filtered (stream
, "This GDB was configured as \"");
2860 if (!STREQ (host_name
, target_name
))
2862 fprintf_filtered (stream
, "--host=%s --target=%s", host_name
, target_name
);
2866 fprintf_filtered (stream
, "%s", host_name
);
2868 fprintf_filtered (stream
, "\".");
2873 show_version (args
, from_tty
)
2878 print_gdb_version (gdb_stdout
);
2879 printf_filtered ("\n");
2883 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2889 printf_unfiltered ("%s", prompt
);
2890 gdb_flush (gdb_stdout
);
2893 /* This replaces the above for the frontends: it returns a pointer
2905 /* ??rehrauer: I don't know why this fails, since it looks as though
2906 assignments to prompt are wrapped in calls to savestring...
2910 prompt
= savestring (s
, strlen (s
));
2914 /* If necessary, make the user confirm that we should quit. Return
2915 non-zero if we should quit, zero if we shouldn't. */
2920 if (inferior_pid
!= 0 && target_has_execution
)
2924 /* This is something of a hack. But there's no reliable way to
2925 see if a GUI is running. The `use_windows' variable doesn't
2928 s
= "A debugging session is active.\nDo you still want to close the debugger?";
2929 else if (attach_flag
)
2930 s
= "The program is running. Quit anyway (and detach it)? ";
2932 s
= "The program is running. Exit anyway? ";
2941 /* Quit without asking for confirmation. */
2944 quit_force (args
, from_tty
)
2950 /* An optional expression may be used to cause gdb to terminate with the
2951 value of that expression. */
2954 value_ptr val
= parse_and_eval (args
);
2956 exit_code
= (int) value_as_long (val
);
2959 if (inferior_pid
!= 0 && target_has_execution
)
2962 target_detach (args
, from_tty
);
2967 /* UDI wants this, to kill the TIP. */
2970 /* Save the history information if it is appropriate to do so. */
2971 if (write_history_p
&& history_filename
)
2972 write_history (history_filename
);
2974 do_final_cleanups(ALL_CLEANUPS
); /* Do any final cleanups before exiting */
2977 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
2978 /* The above does not need to be inside a tuiDo(), since
2979 * it is not manipulating the curses screen, but rather,
2980 * it is tearing it down.
2989 /* Handle the quit command. */
2992 quit_command (args
, from_tty
)
2996 if (! quit_confirm ())
2997 error ("Not confirmed.");
2998 quit_force (args
, from_tty
);
3001 /* Returns whether GDB is running on a terminal and whether the user
3002 desires that questions be asked of them on that terminal. */
3005 input_from_terminal_p ()
3007 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
3012 pwd_command (args
, from_tty
)
3016 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
3017 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3019 if (!STREQ (gdb_dirbuf
, current_directory
))
3020 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3021 current_directory
, gdb_dirbuf
);
3023 printf_unfiltered ("Working directory %s.\n", current_directory
);
3027 cd_command (dir
, from_tty
)
3032 /* Found something other than leading repetitions of "/..". */
3033 int found_real_path
;
3036 /* If the new directory is absolute, repeat is a no-op; if relative,
3037 repeat might be useful but is more likely to be a mistake. */
3041 error_no_arg ("new working directory");
3043 dir
= tilde_expand (dir
);
3044 make_cleanup (free
, dir
);
3046 if (chdir (dir
) < 0)
3047 perror_with_name (dir
);
3050 dir
= savestring (dir
, len
- (len
> 1 && SLASH_P(dir
[len
-1])));
3052 current_directory
= dir
;
3055 if (SLASH_P (current_directory
[0]) && current_directory
[1] == '\0')
3056 current_directory
= concat (current_directory
, dir
, NULL
);
3058 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
3062 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3064 found_real_path
= 0;
3065 for (p
= current_directory
; *p
;)
3067 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
3069 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
3070 && (p
[3] == 0 || SLASH_P (p
[3])))
3072 if (found_real_path
)
3074 /* Search backwards for the directory just before the "/.."
3075 and obliterate it and the "/..". */
3077 while (q
!= current_directory
&& ! SLASH_P (q
[-1]))
3080 if (q
== current_directory
)
3081 /* current_directory is
3082 a relative pathname ("can't happen"--leave it alone). */
3086 strcpy (q
- 1, p
+ 3);
3091 /* We are dealing with leading repetitions of "/..", for example
3092 "/../..", which is the Mach super-root. */
3097 found_real_path
= 1;
3102 forget_cached_source_info ();
3105 pwd_command ((char *) 0, 1);
3108 struct source_cleanup_lines_args
{
3111 char *old_pre_error
;
3112 char *old_error_pre_print
;
3116 source_cleanup_lines (args
)
3119 struct source_cleanup_lines_args
*p
=
3120 (struct source_cleanup_lines_args
*)args
;
3121 source_line_number
= p
->old_line
;
3122 source_file_name
= p
->old_file
;
3123 source_pre_error
= p
->old_pre_error
;
3124 error_pre_print
= p
->old_error_pre_print
;
3129 source_command (args
, from_tty
)
3134 struct cleanup
*old_cleanups
;
3136 struct source_cleanup_lines_args old_lines
;
3141 error ("source command requires pathname of file to source.");
3144 file
= tilde_expand (file
);
3145 old_cleanups
= make_cleanup (free
, file
);
3147 stream
= fopen (file
, FOPEN_RT
);
3150 perror_with_name (file
);
3154 make_cleanup ((make_cleanup_func
) fclose
, stream
);
3156 old_lines
.old_line
= source_line_number
;
3157 old_lines
.old_file
= source_file_name
;
3158 old_lines
.old_pre_error
= source_pre_error
;
3159 old_lines
.old_error_pre_print
= error_pre_print
;
3160 make_cleanup (source_cleanup_lines
, &old_lines
);
3161 source_line_number
= 0;
3162 source_file_name
= file
;
3163 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
3164 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
3165 make_cleanup (free
, source_pre_error
);
3166 /* This will get set every time we read a line. So it won't stay "" for
3168 error_pre_print
= "";
3170 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
3171 if (source_error_allocated
< needed_length
)
3173 source_error_allocated
*= 2;
3174 if (source_error_allocated
< needed_length
)
3175 source_error_allocated
= needed_length
;
3176 if (source_error
== NULL
)
3177 source_error
= xmalloc (source_error_allocated
);
3179 source_error
= xrealloc (source_error
, source_error_allocated
);
3182 read_command_file (stream
);
3184 do_cleanups (old_cleanups
);
3189 echo_command (text
, from_tty
)
3197 while ((c
= *p
++) != '\0')
3201 /* \ at end of argument is used after spaces
3202 so they won't be lost. */
3206 c
= parse_escape (&p
);
3208 printf_filtered ("%c", c
);
3211 printf_filtered ("%c", c
);
3214 /* Force this output to appear now. */
3216 gdb_flush (gdb_stdout
);
3221 dont_repeat_command (ignored
, from_tty
)
3225 *line
= 0; /* Can't call dont_repeat here because we're not
3226 necessarily reading from stdin. */
3229 /* Functions to manipulate command line editing control variables. */
3231 /* Number of commands to print in each call to show_commands. */
3232 #define Hist_print 10
3234 show_commands (args
, from_tty
)
3238 /* Index for history commands. Relative to history_base. */
3241 /* Number of the history entry which we are planning to display next.
3242 Relative to history_base. */
3245 /* The first command in the history which doesn't exist (i.e. one more
3246 than the number of the last command). Relative to history_base. */
3249 extern HIST_ENTRY
*history_get
PARAMS ((int));
3251 /* Print out some of the commands from the command history. */
3252 /* First determine the length of the history list. */
3253 hist_len
= history_size
;
3254 for (offset
= 0; offset
< history_size
; offset
++)
3256 if (!history_get (history_base
+ offset
))
3265 if (args
[0] == '+' && args
[1] == '\0')
3266 /* "info editing +" should print from the stored position. */
3269 /* "info editing <exp>" should print around command number <exp>. */
3270 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3272 /* "show commands" means print the last Hist_print commands. */
3275 num
= hist_len
- Hist_print
;
3281 /* If there are at least Hist_print commands, we want to display the last
3282 Hist_print rather than, say, the last 6. */
3283 if (hist_len
- num
< Hist_print
)
3285 num
= hist_len
- Hist_print
;
3290 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3292 printf_filtered ("%5d %s\n", history_base
+ offset
,
3293 (history_get (history_base
+ offset
))->line
);
3296 /* The next command we want to display is the next one that we haven't
3300 /* If the user repeats this command with return, it should do what
3301 "show commands +" does. This is unnecessary if arg is null,
3302 because "show commands +" is not useful after "show commands". */
3303 if (from_tty
&& args
)
3310 /* Called by do_setshow_command. */
3313 set_history_size_command (args
, from_tty
, c
)
3316 struct cmd_list_element
*c
;
3318 if (history_size
== INT_MAX
)
3319 unstifle_history ();
3320 else if (history_size
>= 0)
3321 stifle_history (history_size
);
3324 history_size
= INT_MAX
;
3325 error ("History size must be non-negative");
3331 set_history (args
, from_tty
)
3335 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3336 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3341 show_history (args
, from_tty
)
3345 cmd_show_list (showhistlist
, from_tty
, "");
3348 int info_verbose
= 0; /* Default verbose msgs off */
3350 /* Called by do_setshow_command. An elaborate joke. */
3353 set_verbose (args
, from_tty
, c
)
3356 struct cmd_list_element
*c
;
3358 char *cmdname
= "verbose";
3359 struct cmd_list_element
*showcmd
;
3361 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3365 c
->doc
= "Set verbose printing of informational messages.";
3366 showcmd
->doc
= "Show verbose printing of informational messages.";
3370 c
->doc
= "Set verbosity.";
3371 showcmd
->doc
= "Show verbosity.";
3376 float_handler (signo
)
3379 /* This message is based on ANSI C, section 4.7. Note that integer
3380 divide by zero causes this, so "float" is a misnomer. */
3381 signal (SIGFPE
, float_handler
);
3382 error ("Erroneous arithmetic operation.");
3396 enablebreaklist
= NULL
;
3401 showhistlist
= NULL
;
3402 unsethistlist
= NULL
;
3403 #if MAINTENANCE_CMDS
3404 maintenancelist
= NULL
;
3405 maintenanceinfolist
= NULL
;
3406 maintenanceprintlist
= NULL
;
3408 setprintlist
= NULL
;
3409 showprintlist
= NULL
;
3410 setchecklist
= NULL
;
3411 showchecklist
= NULL
;
3414 /* Init the history buffer. Note that we are called after the init file(s)
3415 * have been read so that the user can change the history file via his
3416 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3417 * overrides all of this.
3425 tmpenv
= getenv ("HISTSIZE");
3427 history_size
= atoi (tmpenv
);
3428 else if (!history_size
)
3431 stifle_history (history_size
);
3433 tmpenv
= getenv ("GDBHISTFILE");
3435 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3436 else if (!history_filename
) {
3437 /* We include the current directory so that if the user changes
3438 directories the file written will be the same as the one
3440 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3442 read_history (history_filename
);
3448 struct cmd_list_element
*c
;
3450 #ifdef DEFAULT_PROMPT
3451 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3453 prompt
= savestring ("(gdb) ", 6);
3456 /* Set the important stuff up for command editing. */
3457 command_editing_p
= 1;
3458 history_expansion_p
= 0;
3459 write_history_p
= 0;
3461 /* Setup important stuff for command line editing. */
3462 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3463 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3464 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3465 rl_readline_name
= "gdb";
3467 /* Define the classes of commands.
3468 They will appear in the help list in the reverse of this order. */
3470 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3471 "Maintenance commands.\n\
3472 Some gdb commands are provided just for use by gdb maintainers.\n\
3473 These commands are subject to frequent change, and may not be as\n\
3474 well documented as user commands.",
3476 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3477 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3478 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3479 The commands in this class are those defined by the user.\n\
3480 Use the \"define\" command to define a command.", &cmdlist
);
3481 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3483 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3484 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3485 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3486 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3487 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3488 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3489 counting from zero for the innermost (currently executing) frame.\n\n\
3490 At any time gdb identifies one frame as the \"selected\" frame.\n\
3491 Variable lookups are done with respect to the selected frame.\n\
3492 When the program being debugged stops, gdb selects the innermost frame.\n\
3493 The commands below can be used to select other frames by number or address.",
3495 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3497 add_com ("pwd", class_files
, pwd_command
,
3498 "Print working directory. This is used for your program as well.");
3499 c
= add_cmd ("cd", class_files
, cd_command
,
3500 "Set working directory to DIR for debugger and program being debugged.\n\
3501 The change does not take effect for the program being debugged\n\
3502 until the next time it is started.", &cmdlist
);
3503 c
->completer
= filename_completer
;
3506 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3511 add_com ("echo", class_support
, echo_command
,
3512 "Print a constant string. Give string as argument.\n\
3513 C escape sequences may be used in the argument.\n\
3514 No newline is added at the end of the argument;\n\
3515 use \"\\n\" if you want a newline to be printed.\n\
3516 Since leading and trailing whitespace are ignored in command arguments,\n\
3517 if you want to print some you must use \"\\\" before leading whitespace\n\
3518 to be printed or after trailing whitespace.");
3519 add_com ("document", class_support
, document_command
,
3520 "Document a user-defined command.\n\
3521 Give command name as argument. Give documentation on following lines.\n\
3522 End with a line of just \"end\".");
3523 add_com ("define", class_support
, define_command
,
3524 "Define a new command name. Command name is argument.\n\
3525 Definition appears on following lines, one command per line.\n\
3526 End with a line of just \"end\".\n\
3527 Use the \"document\" command to give documentation for the new command.\n\
3528 Commands defined in this way may have up to ten arguments.");
3531 c
= add_cmd ("source", class_support
, source_command
,
3532 "Read commands from a file named FILE.\n\
3533 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3534 when gdb is started.", &cmdlist
);
3536 /* Punt file name, we can't help it easily. */
3537 c
= add_cmd ("source", class_support
, source_command
,
3538 "Read commands from a file named FILE.\n\
3539 Note that the file \".gdbinit\" is read automatically in this way\n\
3540 when gdb is started.", &cmdlist
);
3542 c
->completer
= filename_completer
;
3544 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3545 add_com ("help", class_support
, help_command
, "Print list of commands.");
3546 add_com_alias ("q", "quit", class_support
, 1);
3547 add_com_alias ("h", "help", class_support
, 1);
3549 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
3550 Primarily used inside of user-defined commands that should not be repeated when\n\
3553 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3556 add_show_from_set (c
, &showlist
);
3557 c
->function
.sfunc
= set_verbose
;
3558 set_verbose (NULL
, 0, c
);
3561 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3562 "Set editing of command lines as they are typed.\n\
3563 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3564 Without an argument, command line editing is enabled. To edit, use\n\
3565 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3568 add_prefix_cmd ("history", class_support
, set_history
,
3569 "Generic command for setting command history parameters.",
3570 &sethistlist
, "set history ", 0, &setlist
);
3571 add_prefix_cmd ("history", class_support
, show_history
,
3572 "Generic command for showing command history parameters.",
3573 &showhistlist
, "show history ", 0, &showlist
);
3576 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3577 "Set history expansion on command input.\n\
3578 Without an argument, history expansion is enabled.", &sethistlist
),
3582 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3583 "Set saving of the history record on exit.\n\
3584 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
3585 Without an argument, saving is enabled.", &sethistlist
),
3588 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3589 "Set the size of the command history, \n\
3590 ie. the number of previous commands to keep a record of.", &sethistlist
);
3591 add_show_from_set (c
, &showhistlist
);
3592 c
->function
.sfunc
= set_history_size_command
;
3595 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3596 "Set the filename in which to record the command history\n\
3597 (the list of previous commands of which a record is kept).", &sethistlist
),
3601 (add_set_cmd ("confirm", class_support
, var_boolean
,
3603 "Set whether to confirm potentially dangerous operations.",
3607 add_prefix_cmd ("info", class_info
, info_command
,
3608 "Generic command for showing things about the program being debugged.",
3609 &infolist
, "info ", 0, &cmdlist
);
3610 add_com_alias ("i", "info", class_info
, 1);
3612 add_com ("complete", class_obscure
, complete_command
,
3613 "List the completions for the rest of the line as a command.");
3615 add_prefix_cmd ("show", class_info
, show_command
,
3616 "Generic command for showing things about the debugger.",
3617 &showlist
, "show ", 0, &cmdlist
);
3618 /* Another way to get at the same thing. */
3619 add_info ("set", show_command
, "Show all GDB settings.");
3621 add_cmd ("commands", no_class
, show_commands
,
3622 "Show the history of commands you typed.\n\
3623 You can supply a command number to start with, or a `+' to start after\n\
3624 the previous command number shown.",
3627 add_cmd ("version", no_class
, show_version
,
3628 "Show what version of GDB this is.", &showlist
);
3630 add_com ("while", class_support
, while_command
,
3631 "Execute nested commands WHILE the conditional expression is non zero.\n\
3632 The conditional expression must follow the word `while' and must in turn be\n\
3633 followed by a new line. The nested commands must be entered one per line,\n\
3634 and should be terminated by the word `end'.");
3636 add_com ("if", class_support
, if_command
,
3637 "Execute nested commands once IF the conditional expression is non zero.\n\
3638 The conditional expression must follow the word `if' and must in turn be\n\
3639 followed by a new line. The nested commands must be entered one per line,\n\
3640 and should be terminated by the word 'else' or `end'. If an else clause\n\
3641 is used, the same rules apply to its nested commands as to the first ones.");
3643 /* If target is open when baud changes, it doesn't take effect until the
3644 next open (I think, not sure). */
3645 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3646 var_zinteger
, (char *)&baud_rate
,
3647 "Set baud rate for remote serial I/O.\n\
3648 This value is used to set the speed of the serial port when debugging\n\
3649 using remote targets.", &setlist
),
3653 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3654 "Set debugging of remote protocol.\n\
3655 When enabled, each packet sent or received with the remote target\n\
3656 is displayed.", &setlist
),
3660 add_set_cmd ("remotetimeout", no_class
, var_integer
, (char *)&remote_timeout
,
3661 "Set timeout limit to wait for target to respond.\n\
3662 This value is used to set the time limit for gdb to wait for a response\n\
3663 from the target.", &setlist
),
3666 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
3667 (char *)&annotation_level
, "Set annotation_level.\n\
3668 0 == normal; 1 == fullname (for use when running under emacs)\n\
3669 2 == output annotated suitably for use by programs that control GDB.",
3671 c
= add_show_from_set (c
, &showlist
);