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. */
37 /* readline include files */
38 #include <readline/readline.h>
39 #include <readline/history.h>
41 /* readline defines this. */
44 #include <sys/types.h>
49 #include "gdb_string.h"
53 extern void initialize_utils
PARAMS ((void));
55 /* Prototypes for local functions */
57 static void dont_repeat_command
PARAMS ((char *, int));
59 static void source_cleanup_lines
PARAMS ((PTR
));
61 static void user_defined_command
PARAMS ((char *, int));
63 static void init_signals
PARAMS ((void));
66 static void stop_sig
PARAMS ((int));
69 static char * line_completion_function
PARAMS ((char *, int, char *, int));
71 static char * readline_line_completion_function
PARAMS ((char *, int));
73 static void command_loop_marker
PARAMS ((int));
75 static void while_command
PARAMS ((char *, int));
77 static void if_command
PARAMS ((char *, int));
79 static struct command_line
*
80 build_command_line
PARAMS ((enum command_control_type
, char *));
82 static struct command_line
*
83 get_command_line
PARAMS ((enum command_control_type
, char *));
85 static void realloc_body_list
PARAMS ((struct command_line
*, int));
87 static enum misc_command_type read_next_line
PARAMS ((struct command_line
**));
89 static enum command_control_type
90 recurse_read_control_structure
PARAMS ((struct command_line
*));
92 static struct cleanup
* setup_user_args
PARAMS ((char *));
94 static char * locate_arg
PARAMS ((char *));
96 static char * insert_args
PARAMS ((char *));
98 static void arg_cleanup
PARAMS ((void));
100 static void init_main
PARAMS ((void));
102 static void init_cmd_lists
PARAMS ((void));
104 static void float_handler
PARAMS ((int));
106 static void init_signals
PARAMS ((void));
108 static void set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
110 static void show_history
PARAMS ((char *, int));
112 static void set_history
PARAMS ((char *, int));
114 static void set_history_size_command
PARAMS ((char *, int,
115 struct cmd_list_element
*));
117 static void show_commands
PARAMS ((char *, int));
119 static void echo_command
PARAMS ((char *, int));
121 static void pwd_command
PARAMS ((char *, int));
123 static void show_version
PARAMS ((char *, int));
125 static void document_command
PARAMS ((char *, int));
127 static void define_command
PARAMS ((char *, int));
129 static void validate_comname
PARAMS ((char *));
131 static void help_command
PARAMS ((char *, int));
133 static void show_command
PARAMS ((char *, int));
135 static void info_command
PARAMS ((char *, int));
137 static void complete_command
PARAMS ((char *, int));
139 static void do_nothing
PARAMS ((int));
142 static int quit_cover
PARAMS ((PTR
));
144 static void disconnect
PARAMS ((int));
147 static void source_cleanup
PARAMS ((FILE *));
149 /* If this definition isn't overridden by the header files, assume
150 that isatty and fileno exist on this system. */
152 #define ISATTY(FP) (isatty (fileno (FP)))
155 /* Initialization file name for gdb. This is overridden in some configs. */
157 #ifndef GDBINIT_FILENAME
158 #define GDBINIT_FILENAME ".gdbinit"
160 char gdbinit
[] = GDBINIT_FILENAME
;
162 int inhibit_gdbinit
= 0;
164 /* If nonzero, and GDB has been configured to be able to use windows,
165 attempt to open them upon startup. */
169 /* Version number of GDB, as a string. */
171 extern char *version
;
173 /* Canonical host name as a string. */
175 extern char *host_name
;
177 /* Canonical target name as a string. */
179 extern char *target_name
;
181 extern char lang_frame_mismatch_warn
[]; /* language.c */
183 /* Flag for whether we want all the "from_tty" gubbish printed. */
185 int caution
= 1; /* Default is yes, sigh. */
187 /* Define all cmd_list_elements. */
189 /* Chain containing all defined commands. */
191 struct cmd_list_element
*cmdlist
;
193 /* Chain containing all defined info subcommands. */
195 struct cmd_list_element
*infolist
;
197 /* Chain containing all defined enable subcommands. */
199 struct cmd_list_element
*enablelist
;
201 /* Chain containing all defined disable subcommands. */
203 struct cmd_list_element
*disablelist
;
205 /* Chain containing all defined toggle subcommands. */
207 struct cmd_list_element
*togglelist
;
209 /* Chain containing all defined stop subcommands. */
211 struct cmd_list_element
*stoplist
;
213 /* Chain containing all defined delete subcommands. */
215 struct cmd_list_element
*deletelist
;
217 /* Chain containing all defined "enable breakpoint" subcommands. */
219 struct cmd_list_element
*enablebreaklist
;
221 /* Chain containing all defined set subcommands */
223 struct cmd_list_element
*setlist
;
225 /* Chain containing all defined unset subcommands */
227 struct cmd_list_element
*unsetlist
;
229 /* Chain containing all defined show subcommands. */
231 struct cmd_list_element
*showlist
;
233 /* Chain containing all defined \"set history\". */
235 struct cmd_list_element
*sethistlist
;
237 /* Chain containing all defined \"show history\". */
239 struct cmd_list_element
*showhistlist
;
241 /* Chain containing all defined \"unset history\". */
243 struct cmd_list_element
*unsethistlist
;
245 /* Chain containing all defined maintenance subcommands. */
247 struct cmd_list_element
*maintenancelist
;
249 /* Chain containing all defined "maintenance info" subcommands. */
251 struct cmd_list_element
*maintenanceinfolist
;
253 /* Chain containing all defined "maintenance print" subcommands. */
255 struct cmd_list_element
*maintenanceprintlist
;
257 struct cmd_list_element
*setprintlist
;
259 struct cmd_list_element
*showprintlist
;
261 struct cmd_list_element
*setchecklist
;
263 struct cmd_list_element
*showchecklist
;
265 /* stdio stream that command input is being read from. Set to stdin normally.
266 Set by source_command to the file we are sourcing. Set to NULL if we are
267 executing a user-defined command or interacting via a GUI. */
271 /* Current working directory. */
273 char *current_directory
;
275 /* The directory name is actually stored here (usually). */
276 char gdb_dirbuf
[1024];
278 /* Function to call before reading a command, if nonzero.
279 The function receives two args: an input stream,
280 and a prompt string. */
282 void (*window_hook
) PARAMS ((FILE *, char *));
287 /* gdb prints this when reading a command interactively */
290 /* Buffer used for reading command lines, and the size
291 allocated for it so far. */
296 /* Nonzero if the current command is modified by "server ". This
297 affects things like recording into the command history, comamnds
298 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
299 whatever) can issue its own commands and also send along commands
300 from the user, and have the user not notice that the user interface
301 is issuing commands too. */
304 /* Baud rate specified for talking to serial target systems. Default
305 is left as -1, so targets can choose their own defaults. */
306 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
307 or (unsigned int)-1. This is a Bad User Interface. */
311 /* Timeout limit for response from target. */
313 int remote_timeout
= 20; /* Set default to 20 */
315 /* Non-zero tells remote* modules to output debugging info. */
317 int remote_debug
= 0;
319 /* Level of control structure. */
320 static int control_level
;
322 /* Structure for arguments to user defined functions. */
323 #define MAXUSERARGS 10
326 struct user_args
*next
;
335 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
339 #define STOP_SIGNAL SIGTSTP
340 static void stop_sig
PARAMS ((int));
344 /* Some System V have job control but not sigsetmask(). */
345 #if !defined (HAVE_SIGSETMASK)
347 #define HAVE_SIGSETMASK 1
349 #define HAVE_SIGSETMASK 0
353 #if 0 == (HAVE_SIGSETMASK)
354 #define sigsetmask(n)
357 /* Hooks for alternate command interfaces. */
359 /* Called after most modules have been initialized, but before taking users
362 void (*init_ui_hook
) PARAMS ((char *argv0
));
364 /* This hook is called from within gdb's many mini-event loops which could
365 steal control from a real user interface's event loop. It returns
366 non-zero if the user is requesting a detach, zero otherwise. */
368 int (*ui_loop_hook
) PARAMS ((int));
370 /* Called instead of command_loop at top level. Can be invoked via
371 return_to_top_level. */
373 void (*command_loop_hook
) PARAMS ((void));
376 /* Called instead of fputs for all output. */
378 void (*fputs_unfiltered_hook
) PARAMS ((const char *linebuffer
, GDB_FILE
*stream
));
380 /* Called when the target says something to the host, which may
381 want to appear in a different window. */
383 void (*target_output_hook
) PARAMS ((char *));
385 /* Called from print_frame_info to list the line we stopped in. */
387 void (*print_frame_info_listing_hook
) PARAMS ((struct symtab
*s
, int line
,
388 int stopline
, int noerror
));
389 /* Replaces most of query. */
391 int (*query_hook
) PARAMS ((const char *, va_list));
393 /* Replaces most of warning. */
395 void (*warning_hook
) PARAMS ((const char *, va_list));
397 /* Called from gdb_flush to flush output. */
399 void (*flush_hook
) PARAMS ((GDB_FILE
*stream
));
401 /* These three functions support getting lines of text from the user. They
402 are used in sequence. First readline_begin_hook is called with a text
403 string that might be (for example) a message for the user to type in a
404 sequence of commands to be executed at a breakpoint. If this function
405 calls back to a GUI, it might take this opportunity to pop up a text
406 interaction window with this message. Next, readline_hook is called
407 with a prompt that is emitted prior to collecting the user input.
408 It can be called multiple times. Finally, readline_end_hook is called
409 to notify the GUI that we are done with the interaction window and it
412 void (*readline_begin_hook
) PARAMS ((char *, ...));
413 char * (*readline_hook
) PARAMS ((char *));
414 void (*readline_end_hook
) PARAMS ((void));
416 /* Called as appropriate to notify the interface of the specified breakpoint
419 void (*create_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
420 void (*delete_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
421 void (*modify_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
423 /* Called during long calculations to allow GUI to repair window damage, and to
424 check for stop buttons, etc... */
426 void (*interactive_hook
) PARAMS ((void));
428 /* Called when the registers have changed, as a hint to a GUI
429 to minimize window update. */
431 void (*registers_changed_hook
) PARAMS ((void));
433 /* Tell the GUI someone changed the register REGNO. -1 means
434 that the caller does not know which register changed or
435 that several registers have changed (see value_assign).*/
436 void (*register_changed_hook
) PARAMS ((int regno
));
438 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
439 void (*memory_changed_hook
) PARAMS ((CORE_ADDR addr
, int len
));
441 /* Called when going to wait for the target. Usually allows the GUI to run
442 while waiting for target events. */
444 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
*status
));
446 /* Used by UI as a wrapper around command execution. May do various things
447 like enabling/disabling buttons, etc... */
449 void (*call_command_hook
) PARAMS ((struct cmd_list_element
*c
, char *cmd
,
452 /* Called when the current thread changes. Argument is thread id. */
454 void (*context_hook
) PARAMS ((int id
));
456 /* Takes control from error (). Typically used to prevent longjmps out of the
457 middle of the GUI. Usually used in conjunction with a catch routine. */
459 NORETURN
void (*error_hook
) PARAMS ((void)) ATTR_NORETURN
;
462 /* Where to go for return_to_top_level (RETURN_ERROR). */
463 SIGJMP_BUF error_return
;
464 /* Where to go for return_to_top_level (RETURN_QUIT). */
465 SIGJMP_BUF quit_return
;
467 /* Return for reason REASON. This generally gets back to the command
468 loop, but can be caught via catch_errors. */
471 return_to_top_level (reason
)
472 enum return_reason reason
;
477 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
478 I can think of a reason why that is vital, though). */
479 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
481 disable_current_display ();
482 do_cleanups (ALL_CLEANUPS
);
484 if (annotation_level
> 1)
495 (NORETURN
void) SIGLONGJMP
496 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
499 /* Call FUNC with arg ARGS, catching any errors. If there is no
500 error, return the value returned by FUNC. If there is an error,
501 print ERRSTRING, print the specific error message, then return
504 Must not be called with immediate_quit in effect (bad things might
505 happen, say we got a signal in the middle of a memcpy to quit_return).
506 This is an OK restriction; with very few exceptions immediate_quit can
507 be replaced by judicious use of QUIT.
509 MASK specifies what to catch; it is normally set to
510 RETURN_MASK_ALL, if for no other reason than that the code which
511 calls catch_errors might not be set up to deal with a quit which
512 isn't caught. But if the code can deal with it, it generally
513 should be RETURN_MASK_ERROR, unless for some reason it is more
514 useful to abort only the portion of the operation inside the
515 catch_errors. Note that quit should return to the command line
516 fairly quickly, even if some further processing is being done. */
519 catch_errors (func
, args
, errstring
, mask
)
520 catch_errors_ftype
*func
;
525 SIGJMP_BUF saved_error
;
526 SIGJMP_BUF saved_quit
;
529 struct cleanup
*saved_cleanup_chain
;
530 char *saved_error_pre_print
;
531 char *saved_quit_pre_print
;
533 saved_cleanup_chain
= save_cleanups ();
534 saved_error_pre_print
= error_pre_print
;
535 saved_quit_pre_print
= quit_pre_print
;
537 if (mask
& RETURN_MASK_ERROR
)
539 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (SIGJMP_BUF
));
540 error_pre_print
= errstring
;
542 if (mask
& RETURN_MASK_QUIT
)
544 memcpy (saved_quit
, quit_return
, sizeof (SIGJMP_BUF
));
545 quit_pre_print
= errstring
;
548 if (SIGSETJMP (tmp_jmp
) == 0)
550 if (mask
& RETURN_MASK_ERROR
)
551 memcpy (error_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
552 if (mask
& RETURN_MASK_QUIT
)
553 memcpy (quit_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
554 val
= (*func
) (args
);
559 restore_cleanups (saved_cleanup_chain
);
561 if (mask
& RETURN_MASK_ERROR
)
563 memcpy (error_return
, saved_error
, sizeof (SIGJMP_BUF
));
564 error_pre_print
= saved_error_pre_print
;
566 if (mask
& RETURN_MASK_QUIT
)
568 memcpy (quit_return
, saved_quit
, sizeof (SIGJMP_BUF
));
569 quit_pre_print
= saved_quit_pre_print
;
574 /* Handler for SIGHUP. */
581 catch_errors (quit_cover
, NULL
,
582 "Could not kill the program being debugged", RETURN_MASK_ALL
);
583 signal (SIGHUP
, SIG_DFL
);
584 kill (getpid (), SIGHUP
);
587 /* Just a little helper function for disconnect(). */
593 caution
= 0; /* Throw caution to the wind -- we're exiting.
594 This prevents asking the user dumb questions. */
595 quit_command((char *)0, 0);
598 #endif /* defined SIGHUP */
600 /* Line number we are currently in in a file which is being sourced. */
601 static int source_line_number
;
603 /* Name of the file we are sourcing. */
604 static char *source_file_name
;
606 /* Buffer containing the error_pre_print used by the source stuff.
608 static char *source_error
;
609 static int source_error_allocated
;
611 /* Something to glom on to the start of error_pre_print if source_file_name
613 static char *source_pre_error
;
615 /* Clean up on error during a "source" command (or execution of a
616 user-defined command). */
619 source_cleanup (stream
)
622 /* Restore the previous input stream. */
626 /* Read commands from STREAM. */
628 read_command_file (stream
)
631 struct cleanup
*cleanups
;
633 cleanups
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
636 do_cleanups (cleanups
);
639 extern void init_proc
PARAMS ((void));
641 void (*pre_init_ui_hook
) PARAMS ((void));
647 if (pre_init_ui_hook
)
650 /* Run the init function of each source file */
652 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
653 current_directory
= gdb_dirbuf
;
655 init_cmd_lists (); /* This needs to be done first */
656 initialize_targets (); /* Setup target_terminal macros for utils.c */
657 initialize_utils (); /* Make errors and warnings possible */
658 initialize_all_files ();
659 init_main (); /* But that omits this file! Do it now */
664 /* We need a default language for parsing expressions, so simple things like
665 "set width 0" won't fail if no language is explicitly set in a config file
666 or implicitly set by reading an executable during startup. */
667 set_language (language_c
);
668 expected_language
= current_language
; /* don't warn about the change. */
671 init_ui_hook (argv0
);
674 /* Allocate, initialize a new command line structure for one of the
675 control commands (if/while). */
677 static struct command_line
*
678 build_command_line (type
, args
)
679 enum command_control_type type
;
682 struct command_line
*cmd
;
685 error ("if/while commands require arguments.\n");
687 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
689 cmd
->control_type
= type
;
693 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
695 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
696 cmd
->line
= savestring (args
, strlen (args
));
700 /* Build and return a new command structure for the control commands
701 such as "if" and "while". */
703 static struct command_line
*
704 get_command_line (type
, arg
)
705 enum command_control_type type
;
708 struct command_line
*cmd
;
709 struct cleanup
*old_chain
= NULL
;
711 /* Allocate and build a new command line structure. */
712 cmd
= build_command_line (type
, arg
);
714 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
, &cmd
);
716 /* Read in the body of this command. */
717 if (recurse_read_control_structure (cmd
) == invalid_control
)
719 warning ("error reading in control structure\n");
720 do_cleanups (old_chain
);
724 discard_cleanups (old_chain
);
728 /* Recursively print a command (including full control structures). */
730 print_command_line (cmd
, depth
)
731 struct command_line
*cmd
;
738 for (i
= 0; i
< depth
; i
++)
739 fputs_filtered (" ", gdb_stdout
);
742 /* A simple command, print it and return. */
743 if (cmd
->control_type
== simple_control
)
745 fputs_filtered (cmd
->line
, gdb_stdout
);
746 fputs_filtered ("\n", gdb_stdout
);
750 /* loop_continue to jump to the start of a while loop, print it
752 if (cmd
->control_type
== continue_control
)
754 fputs_filtered ("loop_continue\n", gdb_stdout
);
758 /* loop_break to break out of a while loop, print it and return. */
759 if (cmd
->control_type
== break_control
)
761 fputs_filtered ("loop_break\n", gdb_stdout
);
765 /* A while command. Recursively print its subcommands before returning. */
766 if (cmd
->control_type
== while_control
)
768 struct command_line
*list
;
769 fputs_filtered ("while ", gdb_stdout
);
770 fputs_filtered (cmd
->line
, gdb_stdout
);
771 fputs_filtered ("\n", gdb_stdout
);
772 list
= *cmd
->body_list
;
775 print_command_line (list
, depth
+ 1);
780 /* An if command. Recursively print both arms before returning. */
781 if (cmd
->control_type
== if_control
)
783 fputs_filtered ("if ", gdb_stdout
);
784 fputs_filtered (cmd
->line
, gdb_stdout
);
785 fputs_filtered ("\n", gdb_stdout
);
787 print_command_line (cmd
->body_list
[0], depth
+ 1);
789 /* Show the false arm if it exists. */
790 if (cmd
->body_count
== 2)
794 for (i
= 0; i
< depth
; i
++)
795 fputs_filtered (" ", gdb_stdout
);
797 fputs_filtered ("else\n", gdb_stdout
);
798 print_command_line (cmd
->body_list
[1], depth
+ 1);
802 for (i
= 0; i
< depth
; i
++)
803 fputs_filtered (" ", gdb_stdout
);
805 fputs_filtered ("end\n", gdb_stdout
);
809 /* Execute the command in CMD. */
811 enum command_control_type
812 execute_control_command (cmd
)
813 struct command_line
*cmd
;
815 struct expression
*expr
;
816 struct command_line
*current
;
817 struct cleanup
*old_chain
= 0;
821 enum command_control_type ret
;
824 switch (cmd
->control_type
)
827 /* A simple command, execute it and return. */
828 new_line
= insert_args (cmd
->line
);
830 return invalid_control
;
831 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
833 execute_command (new_line
, 0);
834 ret
= cmd
->control_type
;
837 case continue_control
:
839 /* Return for "continue", and "break" so we can either
840 continue the loop at the top, or break out. */
841 ret
= cmd
->control_type
;
846 /* Parse the loop control expression for the while statement. */
847 new_line
= insert_args (cmd
->line
);
849 return invalid_control
;
850 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
852 expr
= parse_expression (new_line
);
853 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
855 ret
= simple_control
;
858 /* Keep iterating so long as the expression is true. */
865 /* Evaluate the expression. */
866 val_mark
= value_mark ();
867 val
= evaluate_expression (expr
);
868 cond_result
= value_true (val
);
869 value_free_to_mark (val_mark
);
871 /* If the value is false, then break out of the loop. */
875 /* Execute the body of the while statement. */
876 current
= *cmd
->body_list
;
879 ret
= execute_control_command (current
);
881 /* If we got an error, or a "break" command, then stop
883 if (ret
== invalid_control
|| ret
== break_control
)
889 /* If we got a "continue" command, then restart the loop
891 if (ret
== continue_control
)
894 /* Get the next statement. */
895 current
= current
->next
;
899 /* Reset RET so that we don't recurse the break all the way down. */
900 if (ret
== break_control
)
901 ret
= simple_control
;
908 new_line
= insert_args (cmd
->line
);
910 return invalid_control
;
911 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
913 /* Parse the conditional for the if statement. */
914 expr
= parse_expression (new_line
);
915 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
918 ret
= simple_control
;
920 /* Evaluate the conditional. */
921 val_mark
= value_mark ();
922 val
= evaluate_expression (expr
);
924 /* Choose which arm to take commands from based on the value of the
925 conditional expression. */
926 if (value_true (val
))
927 current
= *cmd
->body_list
;
928 else if (cmd
->body_count
== 2)
929 current
= *(cmd
->body_list
+ 1);
930 value_free_to_mark (val_mark
);
932 /* Execute commands in the given arm. */
935 ret
= execute_control_command (current
);
937 /* If we got an error, get out. */
938 if (ret
!= simple_control
)
941 /* Get the next statement in the body. */
942 current
= current
->next
;
949 warning ("Invalid control type in command structure.");
950 return invalid_control
;
954 do_cleanups (old_chain
);
959 /* "while" command support. Executes a body of statements while the
960 loop condition is nonzero. */
963 while_command (arg
, from_tty
)
967 struct command_line
*command
= NULL
;
970 command
= get_command_line (while_control
, arg
);
975 execute_control_command (command
);
976 free_command_lines (&command
);
979 /* "if" command support. Execute either the true or false arm depending
980 on the value of the if conditional. */
983 if_command (arg
, from_tty
)
987 struct command_line
*command
= NULL
;
990 command
= get_command_line (if_control
, arg
);
995 execute_control_command (command
);
996 free_command_lines (&command
);
1003 struct user_args
*oargs
= user_args
;
1005 fatal ("Internal error, arg_cleanup called with no user args.\n");
1007 user_args
= user_args
->next
;
1011 /* Bind the incomming arguments for a user defined command to
1012 $arg0, $arg1 ... $argMAXUSERARGS. */
1014 static struct cleanup
*
1018 struct user_args
*args
;
1019 struct cleanup
*old_chain
;
1020 unsigned int arg_count
= 0;
1022 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
1023 memset (args
, 0, sizeof (struct user_args
));
1025 args
->next
= user_args
;
1028 old_chain
= make_cleanup ((make_cleanup_func
) arg_cleanup
, 0);
1040 if (arg_count
>= MAXUSERARGS
)
1042 error ("user defined function may only have %d arguments.\n",
1047 /* Strip whitespace. */
1048 while (*p
== ' ' || *p
== '\t')
1051 /* P now points to an argument. */
1053 user_args
->a
[arg_count
].arg
= p
;
1055 /* Get to the end of this argument. */
1058 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
1064 else if (*p
== '\\')
1087 user_args
->a
[arg_count
].len
= p
- start_arg
;
1094 /* Given character string P, return a point to the first argument ($arg),
1095 or NULL if P contains no arguments. */
1101 while ((p
= strchr (p
, '$')))
1103 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1110 /* Insert the user defined arguments stored in user_arg into the $arg
1111 arguments found in line, with the updated copy being placed into nline. */
1117 char *p
, *save_line
, *new_line
;
1120 /* First we need to know how much memory to allocate for the new line. */
1123 while ((p
= locate_arg (line
)))
1128 if (i
>= user_args
->count
)
1130 error ("Missing argument %d in user function.\n", i
);
1133 len
+= user_args
->a
[i
].len
;
1137 /* Don't forget the tail. */
1138 len
+= strlen (line
);
1140 /* Allocate space for the new line and fill it in. */
1141 new_line
= (char *)xmalloc (len
+ 1);
1142 if (new_line
== NULL
)
1145 /* Restore pointer to beginning of old line. */
1148 /* Save pointer to beginning of new line. */
1149 save_line
= new_line
;
1151 while ((p
= locate_arg (line
)))
1155 memcpy (new_line
, line
, p
- line
);
1156 new_line
+= p
- line
;
1159 len
= user_args
->a
[i
].len
;
1162 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1167 /* Don't forget the tail. */
1168 strcpy (new_line
, line
);
1170 /* Return a pointer to the beginning of the new line. */
1175 execute_user_command (c
, args
)
1176 struct cmd_list_element
*c
;
1179 register struct command_line
*cmdlines
;
1180 struct cleanup
*old_chain
;
1181 enum command_control_type ret
;
1183 old_chain
= setup_user_args (args
);
1185 cmdlines
= c
->user_commands
;
1190 /* Set the instream to 0, indicating execution of a
1191 user-defined function. */
1192 old_chain
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
1193 instream
= (FILE *) 0;
1196 ret
= execute_control_command (cmdlines
);
1197 if (ret
!= simple_control
&& ret
!= break_control
)
1199 warning ("Error in control structure.\n");
1202 cmdlines
= cmdlines
->next
;
1204 do_cleanups (old_chain
);
1207 /* Execute the line P as a command.
1208 Pass FROM_TTY as second argument to the defining function. */
1211 execute_command (p
, from_tty
)
1215 register struct cmd_list_element
*c
;
1216 register enum language flang
;
1217 static int warned
= 0;
1218 /* FIXME: These should really be in an appropriate header file */
1219 extern void serial_log_command
PARAMS ((const char *));
1223 /* Force cleanup of any alloca areas if using C alloca instead of
1224 a builtin alloca. */
1227 /* This can happen when command_line_input hits end of file. */
1231 serial_log_command (p
);
1233 while (*p
== ' ' || *p
== '\t') p
++;
1238 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1239 /* Pass null arg rather than an empty one. */
1242 /* Clear off trailing whitespace, except for set and complete command. */
1243 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1245 p
= arg
+ strlen (arg
) - 1;
1246 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1251 /* If this command has been hooked, run the hook first. */
1253 execute_user_command (c
->hook
, (char *)0);
1255 if (c
->class == class_user
)
1256 execute_user_command (c
, arg
);
1257 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1258 do_setshow_command (arg
, from_tty
& caution
, c
);
1259 else if (c
->function
.cfunc
== NO_FUNCTION
)
1260 error ("That is not a command, just a help topic.");
1261 else if (call_command_hook
)
1262 call_command_hook (c
, arg
, from_tty
& caution
);
1264 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1267 /* Tell the user if the language has changed (except first time). */
1268 if (current_language
!= expected_language
)
1270 if (language_mode
== language_mode_auto
) {
1271 language_info (1); /* Print what changed. */
1276 /* Warn the user if the working language does not match the
1277 language of the current frame. Only warn the user if we are
1278 actually running the program, i.e. there is a stack. */
1279 /* FIXME: This should be cacheing the frame and only running when
1280 the frame changes. */
1282 if (target_has_stack
)
1284 flang
= get_frame_language ();
1286 && flang
!= language_unknown
1287 && flang
!= current_language
->la_language
)
1289 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1297 command_loop_marker (foo
)
1302 /* Read commands from `instream' and execute them
1303 until end of file or error reading instream. */
1308 struct cleanup
*old_chain
;
1310 int stdin_is_tty
= ISATTY (stdin
);
1311 long time_at_cmd_start
;
1313 long space_at_cmd_start
= 0;
1315 extern int display_time
;
1316 extern int display_space
;
1318 while (instream
&& !feof (instream
))
1321 extern int insert_mode
;
1323 if (window_hook
&& instream
== stdin
)
1324 (*window_hook
) (instream
, prompt
);
1327 if (instream
== stdin
&& stdin_is_tty
)
1328 reinitialize_more_filter ();
1329 old_chain
= make_cleanup ((make_cleanup_func
) command_loop_marker
, 0);
1332 /* A bit of paranoia: I want to make sure the "insert_mode" global
1333 * is clear except when it is being used for command-line editing
1334 * (see tuiIO.c, utils.c); otherwise normal output will
1335 * get messed up in the TUI. So clear it before/after
1336 * the command-line-input call. - RT
1340 /* Get a command-line. This calls the readline package. */
1341 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1342 instream
== stdin
, "prompt");
1349 time_at_cmd_start
= get_run_time ();
1354 extern char **environ
;
1355 char *lim
= (char *) sbrk (0);
1357 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1361 execute_command (command
, instream
== stdin
);
1362 /* Do any commands attached to breakpoint we stopped at. */
1363 bpstat_do_actions (&stop_bpstat
);
1364 do_cleanups (old_chain
);
1368 long cmd_time
= get_run_time () - time_at_cmd_start
;
1370 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1371 cmd_time
/ 1000000, cmd_time
% 1000000);
1377 extern char **environ
;
1378 char *lim
= (char *) sbrk (0);
1379 long space_now
= lim
- (char *) &environ
;
1380 long space_diff
= space_now
- space_at_cmd_start
;
1382 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1384 (space_diff
>= 0 ? '+' : '-'),
1391 /* Commands call this if they do not want to be repeated by null lines. */
1399 /* If we aren't reading from standard input, we are saving the last
1400 thing read from stdin in line and don't want to delete it. Null lines
1401 won't repeat here in any case. */
1402 if (instream
== stdin
)
1406 /* Read a line from the stream "instream" without command line editing.
1408 It prints PRROMPT once at the start.
1409 Action is compatible with "readline", e.g. space for the result is
1410 malloc'd and should be freed by the caller.
1412 A NULL return means end of file. */
1414 gdb_readline (prrompt
)
1419 int input_index
= 0;
1420 int result_size
= 80;
1424 /* Don't use a _filtered function here. It causes the assumed
1425 character position to be off, since the newline we read from
1426 the user is not accounted for. */
1427 fputs_unfiltered (prrompt
, gdb_stdout
);
1429 /* Move to a new line so the entered line doesn't have a prompt
1430 on the front of it. */
1431 fputs_unfiltered ("\n", gdb_stdout
);
1433 gdb_flush (gdb_stdout
);
1436 result
= (char *) xmalloc (result_size
);
1440 /* Read from stdin if we are executing a user defined command.
1441 This is the right thing for prompt_for_continue, at least. */
1442 c
= fgetc (instream
? instream
: stdin
);
1446 if (input_index
> 0)
1447 /* The last line does not end with a newline. Return it, and
1448 if we are called again fgetc will still return EOF and
1449 we'll return NULL then. */
1456 #ifndef CRLF_SOURCE_FILES
1460 if (input_index
> 0 && result
[input_index
- 1] == '\r')
1466 result
[input_index
++] = c
;
1467 while (input_index
>= result_size
)
1470 result
= (char *) xrealloc (result
, result_size
);
1474 result
[input_index
++] = '\0';
1478 /* Variables which control command line editing and history
1479 substitution. These variables are given default values at the end
1481 static int command_editing_p
;
1482 static int history_expansion_p
;
1483 static int write_history_p
;
1484 static int history_size
;
1485 static char *history_filename
;
1487 /* readline uses the word breaks for two things:
1488 (1) In figuring out where to point the TEXT parameter to the
1489 rl_completion_entry_function. Since we don't use TEXT for much,
1490 it doesn't matter a lot what the word breaks are for this purpose, but
1491 it does affect how much stuff M-? lists.
1492 (2) If one of the matches contains a word break character, readline
1493 will quote it. That's why we switch between
1494 gdb_completer_word_break_characters and
1495 gdb_completer_command_word_break_characters. I'm not sure when
1496 we need this behavior (perhaps for funky characters in C++ symbols?). */
1498 /* Variables which are necessary for fancy command line editing. */
1499 char *gdb_completer_word_break_characters
=
1500 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1502 /* When completing on command names, we remove '-' from the list of
1503 word break characters, since we use it in command names. If the
1504 readline library sees one in any of the current completion strings,
1505 it thinks that the string needs to be quoted and automatically supplies
1507 char *gdb_completer_command_word_break_characters
=
1508 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1510 /* Characters that can be used to quote completion strings. Note that we
1511 can't include '"' because the gdb C parser treats such quoted sequences
1513 char *gdb_completer_quote_characters
=
1516 /* Functions that are used as part of the fancy command line editing. */
1518 /* This can be used for functions which don't want to complete on symbols
1519 but don't want to complete on anything else either. */
1522 noop_completer (text
, prefix
)
1529 /* Complete on filenames. */
1531 filename_completer (text
, word
)
1535 /* From readline. */
1536 extern char *filename_completion_function
PARAMS ((char *, int));
1537 int subsequent_name
;
1539 int return_val_used
;
1540 int return_val_alloced
;
1542 return_val_used
= 0;
1543 /* Small for testing. */
1544 return_val_alloced
= 1;
1545 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1547 subsequent_name
= 0;
1551 p
= filename_completion_function (text
, subsequent_name
);
1552 if (return_val_used
>= return_val_alloced
)
1554 return_val_alloced
*= 2;
1556 (char **) xrealloc (return_val
,
1557 return_val_alloced
* sizeof (char *));
1561 return_val
[return_val_used
++] = p
;
1564 /* Like emacs, don't complete on old versions. Especially useful
1565 in the "source" command. */
1566 if (p
[strlen (p
) - 1] == '~')
1572 /* Return exactly p. */
1573 return_val
[return_val_used
++] = p
;
1574 else if (word
> text
)
1576 /* Return some portion of p. */
1577 q
= xmalloc (strlen (p
) + 5);
1578 strcpy (q
, p
+ (word
- text
));
1579 return_val
[return_val_used
++] = q
;
1584 /* Return some of TEXT plus p. */
1585 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1586 strncpy (q
, word
, text
- word
);
1587 q
[text
- word
] = '\0';
1589 return_val
[return_val_used
++] = q
;
1593 subsequent_name
= 1;
1596 /* There is no way to do this just long enough to affect quote inserting
1597 without also affecting the next completion. This should be fixed in
1599 /* Insure that readline does the right thing
1600 with respect to inserting quotes. */
1601 rl_completer_word_break_characters
= "";
1606 /* Here are some useful test cases for completion. FIXME: These should
1607 be put in the test suite. They should be tested with both M-? and TAB.
1609 "show output-" "radix"
1610 "show output" "-radix"
1611 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1612 "p " ambiguous (all symbols)
1613 "info t foo" no completions
1614 "info t " no completions
1615 "info t" ambiguous ("info target", "info terminal", etc.)
1616 "info ajksdlfk" no completions
1617 "info ajksdlfk " no completions
1619 "info " ambiguous (all info commands)
1620 "p \"a" no completions (string constant)
1621 "p 'a" ambiguous (all symbols starting with a)
1622 "p b-a" ambiguous (all symbols starting with a)
1623 "p b-" ambiguous (all symbols)
1624 "file Make" "file" (word break hard to screw up here)
1625 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1628 /* Generate completions one by one for the completer. Each time we are
1629 called return another potential completion to the caller.
1630 line_completion just completes on commands or passes the buck to the
1631 command's completer function, the stuff specific to symbol completion
1632 is in make_symbol_completion_list.
1634 TEXT is the caller's idea of the "word" we are looking at.
1636 MATCHES is the number of matches that have currently been collected from
1637 calling this completion function. When zero, then we need to initialize,
1638 otherwise the initialization has already taken place and we can just
1639 return the next potential completion string.
1641 LINE_BUFFER is available to be looked at; it contains the entire text
1642 of the line. POINT is the offset in that line of the cursor. You
1643 should pretend that the line ends at POINT.
1645 Returns NULL if there are no more completions, else a pointer to a string
1646 which is a possible completion, it is the caller's responsibility to
1650 line_completion_function (text
, matches
, line_buffer
, point
)
1656 static char **list
= (char **)NULL
; /* Cache of completions */
1657 static int index
; /* Next cached completion */
1658 char *output
= NULL
;
1659 char *tmp_command
, *p
;
1660 /* Pointer within tmp_command which corresponds to text. */
1662 struct cmd_list_element
*c
, *result_list
;
1666 /* The caller is beginning to accumulate a new set of completions, so
1667 we need to find all of them now, and cache them for returning one at
1668 a time on future calls. */
1672 /* Free the storage used by LIST, but not by the strings inside.
1673 This is because rl_complete_internal () frees the strings. */
1679 /* Choose the default set of word break characters to break completions.
1680 If we later find out that we are doing completions on command strings
1681 (as opposed to strings supplied by the individual command completer
1682 functions, which can be any string) then we will switch to the
1683 special word break set for command strings, which leaves out the
1684 '-' character used in some commands. */
1686 rl_completer_word_break_characters
=
1687 gdb_completer_word_break_characters
;
1689 /* Decide whether to complete on a list of gdb commands or on symbols. */
1690 tmp_command
= (char *) alloca (point
+ 1);
1693 strncpy (tmp_command
, line_buffer
, point
);
1694 tmp_command
[point
] = '\0';
1695 /* Since text always contains some number of characters leading up
1696 to point, we can find the equivalent position in tmp_command
1697 by subtracting that many characters from the end of tmp_command. */
1698 word
= tmp_command
+ point
- strlen (text
);
1702 /* An empty line we want to consider ambiguous; that is, it
1703 could be any command. */
1704 c
= (struct cmd_list_element
*) -1;
1709 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1712 /* Move p up to the next interesting thing. */
1713 while (*p
== ' ' || *p
== '\t')
1720 /* It is an unrecognized command. So there are no
1721 possible completions. */
1724 else if (c
== (struct cmd_list_element
*) -1)
1728 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1729 doesn't advance over that thing itself. Do so now. */
1731 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1733 if (q
!= tmp_command
+ point
)
1735 /* There is something beyond the ambiguous
1736 command, so there are no possible completions. For
1737 example, "info t " or "info t foo" does not complete
1738 to anything, because "info t" can be "info target" or
1744 /* We're trying to complete on the command which was ambiguous.
1745 This we can deal with. */
1748 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1753 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1755 /* Insure that readline does the right thing with respect to
1756 inserting quotes. */
1757 rl_completer_word_break_characters
=
1758 gdb_completer_command_word_break_characters
;
1763 /* We've recognized a full command. */
1765 if (p
== tmp_command
+ point
)
1767 /* There is no non-whitespace in the line beyond the command. */
1769 if (p
[-1] == ' ' || p
[-1] == '\t')
1771 /* The command is followed by whitespace; we need to complete
1772 on whatever comes after command. */
1775 /* It is a prefix command; what comes after it is
1776 a subcommand (e.g. "info "). */
1777 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1779 /* Insure that readline does the right thing
1780 with respect to inserting quotes. */
1781 rl_completer_word_break_characters
=
1782 gdb_completer_command_word_break_characters
;
1786 list
= complete_on_enum (c
->enums
, p
, word
);
1787 rl_completer_word_break_characters
=
1788 gdb_completer_command_word_break_characters
;
1792 /* It is a normal command; what comes after it is
1793 completed by the command's completer function. */
1794 list
= (*c
->completer
) (p
, word
);
1799 /* The command is not followed by whitespace; we need to
1800 complete on the command itself. e.g. "p" which is a
1801 command itself but also can complete to "print", "ptype"
1805 /* Find the command we are completing on. */
1807 while (q
> tmp_command
)
1809 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1815 list
= complete_on_cmdlist (result_list
, q
, word
);
1817 /* Insure that readline does the right thing
1818 with respect to inserting quotes. */
1819 rl_completer_word_break_characters
=
1820 gdb_completer_command_word_break_characters
;
1825 /* There is non-whitespace beyond the command. */
1827 if (c
->prefixlist
&& !c
->allow_unknown
)
1829 /* It is an unrecognized subcommand of a prefix command,
1830 e.g. "info adsfkdj". */
1835 list
= complete_on_enum (c
->enums
, p
, word
);
1839 /* It is a normal command. */
1840 list
= (*c
->completer
) (p
, word
);
1846 /* If we found a list of potential completions during initialization then
1847 dole them out one at a time. The vector of completions is NULL
1848 terminated, so after returning the last one, return NULL (and continue
1849 to do so) each time we are called after that, until a new list is
1854 output
= list
[index
];
1862 /* Can't do this because readline hasn't yet checked the word breaks
1863 for figuring out whether to insert a quote. */
1865 /* Make sure the word break characters are set back to normal for the
1866 next time that readline tries to complete something. */
1867 rl_completer_word_break_characters
=
1868 gdb_completer_word_break_characters
;
1874 /* Line completion interface function for readline. */
1877 readline_line_completion_function (text
, matches
)
1881 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1884 /* Skip over a possibly quoted word (as defined by the quote characters
1885 and word break characters the completer uses). Returns pointer to the
1886 location after the "word". */
1892 char quote_char
= '\0';
1895 for (scan
= str
; *scan
!= '\0'; scan
++)
1897 if (quote_char
!= '\0')
1899 /* Ignore everything until the matching close quote char */
1900 if (*scan
== quote_char
)
1902 /* Found matching close quote. */
1907 else if (strchr (gdb_completer_quote_characters
, *scan
))
1909 /* Found start of a quoted string. */
1912 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1926 #if STOP_SIGNAL == SIGTSTP
1927 signal (SIGTSTP
, SIG_DFL
);
1929 kill (getpid (), SIGTSTP
);
1930 signal (SIGTSTP
, stop_sig
);
1932 signal (STOP_SIGNAL
, stop_sig
);
1934 printf_unfiltered ("%s", prompt
);
1935 gdb_flush (gdb_stdout
);
1937 /* Forget about any previous command -- null line now will do nothing. */
1940 #endif /* STOP_SIGNAL */
1942 /* Initialize signal handlers. */
1947 /* Under System V the default disposition of a signal is reinstated after
1948 the signal is caught and delivered to an application process. On such
1949 systems one must restore the replacement signal handler if one wishes
1950 to continue handling the signal in one's program. On BSD systems this
1951 is not needed but it is harmless, and it simplifies the code to just do
1952 it unconditionally. */
1953 signal (signo
, do_nothing
);
1959 signal (SIGINT
, request_quit
);
1961 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1962 to the inferior and breakpoints will be ignored. */
1964 signal (SIGTRAP
, SIG_DFL
);
1967 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1968 passed to the inferior, which we don't want. It would be
1969 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1970 on BSD4.3 systems using vfork, that can affect the
1971 GDB process as well as the inferior (the signal handling tables
1972 might be in memory, shared between the two). Since we establish
1973 a handler for SIGQUIT, when we call exec it will set the signal
1974 to SIG_DFL for us. */
1975 signal (SIGQUIT
, do_nothing
);
1977 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1978 signal (SIGHUP
, disconnect
);
1980 signal (SIGFPE
, float_handler
);
1982 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1983 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1987 /* Read one line from the command input stream `instream'
1988 into the local static buffer `linebuffer' (whose current length
1990 The buffer is made bigger as necessary.
1991 Returns the address of the start of the line.
1993 NULL is returned for end of file.
1995 *If* the instream == stdin & stdin is a terminal, the line read
1996 is copied into the file line saver (global var char *line,
1997 length linesize) so that it can be duplicated.
1999 This routine either uses fancy command line editing or
2000 simple input as the user has requested. */
2003 command_line_input (prrompt
, repeat
, annotation_suffix
)
2006 char *annotation_suffix
;
2008 static char *linebuffer
= 0;
2009 static unsigned linelength
= 0;
2013 char *local_prompt
= prrompt
;
2017 /* The annotation suffix must be non-NULL. */
2018 if (annotation_suffix
== NULL
)
2019 annotation_suffix
= "";
2021 if (annotation_level
> 1 && instream
== stdin
)
2023 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
2024 + strlen (annotation_suffix
) + 40);
2025 if (prrompt
== NULL
)
2026 local_prompt
[0] = '\0';
2028 strcpy (local_prompt
, prrompt
);
2029 strcat (local_prompt
, "\n\032\032");
2030 strcat (local_prompt
, annotation_suffix
);
2031 strcat (local_prompt
, "\n");
2034 if (linebuffer
== 0)
2037 linebuffer
= (char *) xmalloc (linelength
);
2042 /* Control-C quits instantly if typed while in this loop
2043 since it should not wait until the user types a newline. */
2047 signal (STOP_SIGNAL
, stop_sig
);
2052 /* Make sure that all output has been output. Some machines may let
2053 you get away with leaving out some of the gdb_flush, but not all. */
2055 gdb_flush (gdb_stdout
);
2056 gdb_flush (gdb_stderr
);
2058 if (source_file_name
!= NULL
)
2060 ++source_line_number
;
2061 sprintf (source_error
,
2062 "%s%s:%d: Error in sourced command file:\n",
2065 source_line_number
);
2066 error_pre_print
= source_error
;
2069 if (annotation_level
> 1 && instream
== stdin
)
2071 printf_unfiltered ("\n\032\032pre-");
2072 printf_unfiltered (annotation_suffix
);
2073 printf_unfiltered ("\n");
2076 /* Don't use fancy stuff if not talking to stdin. */
2077 if (readline_hook
&& instream
== NULL
)
2079 rl
= (*readline_hook
) (local_prompt
);
2081 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
2083 rl
= readline (local_prompt
);
2087 rl
= gdb_readline (local_prompt
);
2090 if (annotation_level
> 1 && instream
== stdin
)
2092 printf_unfiltered ("\n\032\032post-");
2093 printf_unfiltered (annotation_suffix
);
2094 printf_unfiltered ("\n");
2097 if (!rl
|| rl
== (char *) EOF
)
2102 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
2104 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
2105 nline
= (char *) xrealloc (linebuffer
, linelength
);
2106 p
+= nline
- linebuffer
;
2110 /* Copy line. Don't copy null at end. (Leaves line alone
2111 if this was just a newline) */
2115 free (rl
); /* Allocated in readline. */
2117 if (p
== linebuffer
|| *(p
- 1) != '\\')
2120 p
--; /* Put on top of '\'. */
2121 local_prompt
= (char *) 0;
2126 signal (STOP_SIGNAL
, SIG_DFL
);
2133 #define SERVER_COMMAND_LENGTH 7
2135 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2136 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2139 /* Note that we don't set `line'. Between this and the check in
2140 dont_repeat, this insures that repeating will still do the
2143 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2146 /* Do history expansion if that is wished. */
2147 if (history_expansion_p
&& instream
== stdin
2148 && ISATTY (instream
))
2150 char *history_value
;
2153 *p
= '\0'; /* Insert null now. */
2154 expanded
= history_expand (linebuffer
, &history_value
);
2157 /* Print the changes. */
2158 printf_unfiltered ("%s\n", history_value
);
2160 /* If there was an error, call this function again. */
2163 free (history_value
);
2164 return command_line_input (prrompt
, repeat
, annotation_suffix
);
2166 if (strlen (history_value
) > linelength
)
2168 linelength
= strlen (history_value
) + 1;
2169 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2171 strcpy (linebuffer
, history_value
);
2172 p
= linebuffer
+ strlen(linebuffer
);
2173 free (history_value
);
2177 /* If we just got an empty line, and that is supposed
2178 to repeat the previous command, return the value in the
2180 if (repeat
&& p
== linebuffer
)
2182 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++) ;
2188 /* Add line to history if appropriate. */
2189 if (instream
== stdin
2190 && ISATTY (stdin
) && *linebuffer
)
2191 add_history (linebuffer
);
2193 /* Note: lines consisting solely of comments are added to the command
2194 history. This is useful when you type a command, and then
2195 realize you don't want to execute it quite yet. You can comment
2196 out the command and then later fetch it from the value history
2197 and remove the '#'. The kill ring is probably better, but some
2198 people are in the habit of commenting things out. */
2200 *p1
= '\0'; /* Found a comment. */
2202 /* Save into global buffer if appropriate. */
2205 if (linelength
> linesize
)
2207 line
= xrealloc (line
, linelength
);
2208 linesize
= linelength
;
2210 strcpy (line
, linebuffer
);
2218 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2219 code bodies. This is typically used when we encounter an "else"
2220 clause for an "if" command. */
2223 realloc_body_list (command
, new_length
)
2224 struct command_line
*command
;
2228 struct command_line
**body_list
;
2230 n
= command
->body_count
;
2232 /* Nothing to do? */
2233 if (new_length
<= n
)
2236 body_list
= (struct command_line
**)
2237 xmalloc (sizeof (struct command_line
*) * new_length
);
2239 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2241 free (command
->body_list
);
2242 command
->body_list
= body_list
;
2243 command
->body_count
= new_length
;
2246 /* Read one line from the input stream. If the command is an "else" or
2247 "end", return such an indication to the caller. */
2249 static enum misc_command_type
2250 read_next_line (command
)
2251 struct command_line
**command
;
2253 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2256 if (control_level
>= 254)
2257 error ("Control nesting too deep!\n");
2259 /* Set a prompt based on the nesting of the control commands. */
2260 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2262 for (i
= 0; i
< control_level
; i
++)
2263 control_prompt
[i
] = ' ';
2264 control_prompt
[i
] = '>';
2265 control_prompt
[i
+1] = '\0';
2266 prompt_ptr
= (char *)&control_prompt
[0];
2271 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2273 /* Not sure what to do here. */
2277 /* Strip leading and trailing whitespace. */
2278 while (*p
== ' ' || *p
== '\t')
2281 p1
= p
+ strlen (p
);
2282 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2285 /* Blanks and comments don't really do anything, but we need to
2286 distinguish them from else, end and other commands which can be
2288 if (p1
== p
|| p
[0] == '#')
2291 /* Is this the end of a simple, while, or if control structure? */
2292 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2295 /* Is the else clause of an if control structure? */
2296 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2297 return else_command
;
2299 /* Check for while, if, break, continue, etc and build a new command
2300 line structure for them. */
2301 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2302 *command
= build_command_line (while_control
, p
+ 6);
2303 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2304 *command
= build_command_line (if_control
, p
+ 3);
2305 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2307 *command
= (struct command_line
*)
2308 xmalloc (sizeof (struct command_line
));
2309 (*command
)->next
= NULL
;
2310 (*command
)->line
= NULL
;
2311 (*command
)->control_type
= break_control
;
2312 (*command
)->body_count
= 0;
2313 (*command
)->body_list
= NULL
;
2315 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2317 *command
= (struct command_line
*)
2318 xmalloc (sizeof (struct command_line
));
2319 (*command
)->next
= NULL
;
2320 (*command
)->line
= NULL
;
2321 (*command
)->control_type
= continue_control
;
2322 (*command
)->body_count
= 0;
2323 (*command
)->body_list
= NULL
;
2327 /* A normal command. */
2328 *command
= (struct command_line
*)
2329 xmalloc (sizeof (struct command_line
));
2330 (*command
)->next
= NULL
;
2331 (*command
)->line
= savestring (p
, p1
- p
);
2332 (*command
)->control_type
= simple_control
;
2333 (*command
)->body_count
= 0;
2334 (*command
)->body_list
= NULL
;
2337 /* Nothing special. */
2341 /* Recursively read in the control structures and create a command_line
2342 structure from them.
2344 The parent_control parameter is the control structure in which the
2345 following commands are nested. */
2347 static enum command_control_type
2348 recurse_read_control_structure (current_cmd
)
2349 struct command_line
*current_cmd
;
2351 int current_body
, i
;
2352 enum misc_command_type val
;
2353 enum command_control_type ret
;
2354 struct command_line
**body_ptr
, *child_tail
, *next
;
2359 /* Sanity checks. */
2360 if (current_cmd
->control_type
== simple_control
)
2362 error ("Recursed on a simple control type\n");
2363 return invalid_control
;
2366 if (current_body
> current_cmd
->body_count
)
2368 error ("Allocated body is smaller than this command type needs\n");
2369 return invalid_control
;
2372 /* Read lines from the input stream and build control structures. */
2378 val
= read_next_line (&next
);
2380 /* Just skip blanks and comments. */
2381 if (val
== nop_command
)
2384 if (val
== end_command
)
2386 if (current_cmd
->control_type
== while_control
2387 || current_cmd
->control_type
== if_control
)
2389 /* Success reading an entire control structure. */
2390 ret
= simple_control
;
2395 ret
= invalid_control
;
2400 /* Not the end of a control structure. */
2401 if (val
== else_command
)
2403 if (current_cmd
->control_type
== if_control
2404 && current_body
== 1)
2406 realloc_body_list (current_cmd
, 2);
2413 ret
= invalid_control
;
2420 child_tail
->next
= next
;
2424 body_ptr
= current_cmd
->body_list
;
2425 for (i
= 1; i
< current_body
; i
++)
2434 /* If the latest line is another control structure, then recurse
2436 if (next
->control_type
== while_control
2437 || next
->control_type
== if_control
)
2440 ret
= recurse_read_control_structure (next
);
2443 if (ret
!= simple_control
)
2453 /* Read lines from the input stream and accumulate them in a chain of
2454 struct command_line's, which is then returned. For input from a
2455 terminal, the special command "end" is used to mark the end of the
2456 input, and is not included in the returned chain of commands. */
2458 #define END_MESSAGE "End with a line saying just \"end\"."
2460 struct command_line
*
2461 read_command_lines (prompt
, from_tty
)
2465 struct command_line
*head
, *tail
, *next
;
2466 struct cleanup
*old_chain
;
2467 enum command_control_type ret
;
2468 enum misc_command_type val
;
2471 if (readline_begin_hook
)
2473 /* Note - intentional to merge messages with no newline */
2474 (*readline_begin_hook
) ("%s %s\n", prompt
, END_MESSAGE
);
2476 else if (from_tty
&& input_from_terminal_p ())
2478 printf_unfiltered ("%s\n%s\n", prompt
, END_MESSAGE
);
2479 gdb_flush (gdb_stdout
);
2487 val
= read_next_line (&next
);
2489 /* Ignore blank lines or comments. */
2490 if (val
== nop_command
)
2493 if (val
== end_command
)
2495 ret
= simple_control
;
2499 if (val
!= ok_command
)
2501 ret
= invalid_control
;
2505 if (next
->control_type
== while_control
2506 || next
->control_type
== if_control
)
2509 ret
= recurse_read_control_structure (next
);
2512 if (ret
== invalid_control
)
2523 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
,
2533 if (ret
!= invalid_control
)
2535 discard_cleanups (old_chain
);
2538 do_cleanups (old_chain
);
2541 if (readline_end_hook
)
2543 (*readline_end_hook
) ();
2548 /* Free a chain of struct command_line's. */
2551 free_command_lines (lptr
)
2552 struct command_line
**lptr
;
2554 register struct command_line
*l
= *lptr
;
2555 register struct command_line
*next
;
2556 struct command_line
**blist
;
2561 if (l
->body_count
> 0)
2563 blist
= l
->body_list
;
2564 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2565 free_command_lines (blist
);
2574 /* Add an element to the list of info subcommands. */
2577 add_info (name
, fun
, doc
)
2579 void (*fun
) PARAMS ((char *, int));
2582 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2585 /* Add an alias to the list of info subcommands. */
2588 add_info_alias (name
, oldname
, abbrev_flag
)
2593 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2596 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2597 Therefore, its own definition is called only for "info" with no args. */
2601 info_command (arg
, from_tty
)
2605 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2606 help_list (infolist
, "info ", -1, gdb_stdout
);
2609 /* The "complete" command is used by Emacs to implement completion. */
2613 complete_command (arg
, from_tty
)
2625 argpoint
= strlen (arg
);
2627 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2629 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2631 printf_unfiltered ("%s\n", completion
);
2636 /* The "show" command with no arguments shows all the settings. */
2640 show_command (arg
, from_tty
)
2644 cmd_show_list (showlist
, from_tty
, "");
2647 /* Add an element to the list of commands. */
2650 add_com (name
, class, fun
, doc
)
2652 enum command_class
class;
2653 void (*fun
) PARAMS ((char *, int));
2656 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2659 /* Add an alias or abbreviation command to the list of commands. */
2662 add_com_alias (name
, oldname
, class, abbrev_flag
)
2665 enum command_class
class;
2668 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2675 error ("Argument required (%s).", why
);
2680 help_command (command
, from_tty
)
2682 int from_tty
; /* Ignored */
2684 help_cmd (command
, gdb_stdout
);
2688 validate_comname (comname
)
2694 error_no_arg ("name of command to define");
2699 if (!isalnum(*p
) && *p
!= '-' && *p
!= '_')
2700 error ("Junk in argument list: \"%s\"", p
);
2705 /* This is just a placeholder in the command data structures. */
2707 user_defined_command (ignore
, from_tty
)
2714 define_command (comname
, from_tty
)
2718 register struct command_line
*cmds
;
2719 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2720 char *tem
= comname
;
2722 #define HOOK_STRING "hook-"
2725 validate_comname (comname
);
2727 /* Look it up, and verify that we got an exact match. */
2728 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2729 if (c
&& !STREQ (comname
, c
->name
))
2734 if (c
->class == class_user
|| c
->class == class_alias
)
2735 tem
= "Redefine command \"%s\"? ";
2737 tem
= "Really redefine built-in command \"%s\"? ";
2738 if (!query (tem
, c
->name
))
2739 error ("Command \"%s\" not redefined.", c
->name
);
2742 /* If this new command is a hook, then mark the command which it
2743 is hooking. Note that we allow hooking `help' commands, so that
2744 we can hook the `stop' pseudo-command. */
2746 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2748 /* Look up cmd it hooks, and verify that we got an exact match. */
2749 tem
= comname
+HOOK_LEN
;
2750 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2751 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2755 warning ("Your new `%s' command does not hook any existing command.",
2757 if (!query ("Proceed? "))
2758 error ("Not confirmed.");
2762 comname
= savestring (comname
, strlen (comname
));
2764 /* If the rest of the commands will be case insensitive, this one
2765 should behave in the same manner. */
2766 for (tem
= comname
; *tem
; tem
++)
2767 if (isupper(*tem
)) *tem
= tolower(*tem
);
2769 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
2770 cmds
= read_command_lines (tmpbuf
, from_tty
);
2772 if (c
&& c
->class == class_user
)
2773 free_command_lines (&c
->user_commands
);
2775 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2776 (c
&& c
->class == class_user
)
2777 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2778 newc
->user_commands
= cmds
;
2780 /* If this new command is a hook, then mark both commands as being
2784 hookc
->hook
= newc
; /* Target gets hooked. */
2785 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2790 document_command (comname
, from_tty
)
2794 struct command_line
*doclines
;
2795 register struct cmd_list_element
*c
;
2796 char *tem
= comname
;
2799 validate_comname (comname
);
2801 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2803 if (c
->class != class_user
)
2804 error ("Command \"%s\" is built-in.", comname
);
2806 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
2807 doclines
= read_command_lines (tmpbuf
, from_tty
);
2809 if (c
->doc
) free (c
->doc
);
2812 register struct command_line
*cl1
;
2813 register int len
= 0;
2815 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2816 len
+= strlen (cl1
->line
) + 1;
2818 c
->doc
= (char *) xmalloc (len
+ 1);
2821 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2823 strcat (c
->doc
, cl1
->line
);
2825 strcat (c
->doc
, "\n");
2829 free_command_lines (&doclines
);
2832 /* Print the GDB banner. */
2834 print_gdb_version (stream
)
2837 /* From GNU coding standards, first line is meant to be easy for a
2838 program to parse, and is just canonical program name and version
2839 number, which starts after last space. */
2841 fprintf_filtered (stream
, "GNU gdb %s\n", version
);
2843 /* Second line is a copyright notice. */
2845 fprintf_filtered (stream
, "Copyright 1998 Free Software Foundation, Inc.\n");
2847 /* Following the copyright is a brief statement that the program is
2848 free software, that users are free to copy and change it on
2849 certain conditions, that it is covered by the GNU GPL, and that
2850 there is no warranty. */
2852 fprintf_filtered (stream
, "\
2853 GDB is free software, covered by the GNU General Public License, and you are\n\
2854 welcome to change it and/or distribute copies of it under certain conditions.\n\
2855 Type \"show copying\" to see the conditions.\n\
2856 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2858 /* After the required info we print the configuration information. */
2860 fprintf_filtered (stream
, "This GDB was configured as \"");
2861 if (!STREQ (host_name
, target_name
))
2863 fprintf_filtered (stream
, "--host=%s --target=%s", host_name
, target_name
);
2867 fprintf_filtered (stream
, "%s", host_name
);
2869 fprintf_filtered (stream
, "\".");
2874 show_version (args
, from_tty
)
2879 print_gdb_version (gdb_stdout
);
2880 printf_filtered ("\n");
2884 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2890 printf_unfiltered ("%s", prompt
);
2891 gdb_flush (gdb_stdout
);
2894 /* This replaces the above for the frontends: it returns a pointer
2906 /* ??rehrauer: I don't know why this fails, since it looks as though
2907 assignments to prompt are wrapped in calls to savestring...
2911 prompt
= savestring (s
, strlen (s
));
2915 /* If necessary, make the user confirm that we should quit. Return
2916 non-zero if we should quit, zero if we shouldn't. */
2921 if (inferior_pid
!= 0 && target_has_execution
)
2925 /* This is something of a hack. But there's no reliable way to
2926 see if a GUI is running. The `use_windows' variable doesn't
2929 s
= "A debugging session is active.\nDo you still want to close the debugger?";
2930 else if (attach_flag
)
2931 s
= "The program is running. Quit anyway (and detach it)? ";
2933 s
= "The program is running. Exit anyway? ";
2942 /* Quit without asking for confirmation. */
2945 quit_force (args
, from_tty
)
2951 /* An optional expression may be used to cause gdb to terminate with the
2952 value of that expression. */
2955 value_ptr val
= parse_and_eval (args
);
2957 exit_code
= (int) value_as_long (val
);
2960 if (inferior_pid
!= 0 && target_has_execution
)
2963 target_detach (args
, from_tty
);
2968 /* UDI wants this, to kill the TIP. */
2971 /* Save the history information if it is appropriate to do so. */
2972 if (write_history_p
&& history_filename
)
2973 write_history (history_filename
);
2975 do_final_cleanups(ALL_CLEANUPS
); /* Do any final cleanups before exiting */
2978 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
2979 /* The above does not need to be inside a tuiDo(), since
2980 * it is not manipulating the curses screen, but rather,
2981 * it is tearing it down.
2990 /* Handle the quit command. */
2993 quit_command (args
, from_tty
)
2997 if (! quit_confirm ())
2998 error ("Not confirmed.");
2999 quit_force (args
, from_tty
);
3002 /* Returns whether GDB is running on a terminal and whether the user
3003 desires that questions be asked of them on that terminal. */
3006 input_from_terminal_p ()
3008 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
3013 pwd_command (args
, from_tty
)
3017 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
3018 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3020 if (!STREQ (gdb_dirbuf
, current_directory
))
3021 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3022 current_directory
, gdb_dirbuf
);
3024 printf_unfiltered ("Working directory %s.\n", current_directory
);
3028 cd_command (dir
, from_tty
)
3033 /* Found something other than leading repetitions of "/..". */
3034 int found_real_path
;
3037 /* If the new directory is absolute, repeat is a no-op; if relative,
3038 repeat might be useful but is more likely to be a mistake. */
3042 error_no_arg ("new working directory");
3044 dir
= tilde_expand (dir
);
3045 make_cleanup (free
, dir
);
3047 if (chdir (dir
) < 0)
3048 perror_with_name (dir
);
3051 dir
= savestring (dir
, len
- (len
> 1 && SLASH_P(dir
[len
-1])));
3053 current_directory
= dir
;
3056 if (SLASH_P (current_directory
[0]) && current_directory
[1] == '\0')
3057 current_directory
= concat (current_directory
, dir
, NULL
);
3059 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
3063 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3065 found_real_path
= 0;
3066 for (p
= current_directory
; *p
;)
3068 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
3070 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
3071 && (p
[3] == 0 || SLASH_P (p
[3])))
3073 if (found_real_path
)
3075 /* Search backwards for the directory just before the "/.."
3076 and obliterate it and the "/..". */
3078 while (q
!= current_directory
&& ! SLASH_P (q
[-1]))
3081 if (q
== current_directory
)
3082 /* current_directory is
3083 a relative pathname ("can't happen"--leave it alone). */
3087 strcpy (q
- 1, p
+ 3);
3092 /* We are dealing with leading repetitions of "/..", for example
3093 "/../..", which is the Mach super-root. */
3098 found_real_path
= 1;
3103 forget_cached_source_info ();
3106 pwd_command ((char *) 0, 1);
3109 struct source_cleanup_lines_args
{
3112 char *old_pre_error
;
3113 char *old_error_pre_print
;
3117 source_cleanup_lines (args
)
3120 struct source_cleanup_lines_args
*p
=
3121 (struct source_cleanup_lines_args
*)args
;
3122 source_line_number
= p
->old_line
;
3123 source_file_name
= p
->old_file
;
3124 source_pre_error
= p
->old_pre_error
;
3125 error_pre_print
= p
->old_error_pre_print
;
3130 source_command (args
, from_tty
)
3135 struct cleanup
*old_cleanups
;
3137 struct source_cleanup_lines_args old_lines
;
3142 error ("source command requires pathname of file to source.");
3145 file
= tilde_expand (file
);
3146 old_cleanups
= make_cleanup (free
, file
);
3148 stream
= fopen (file
, FOPEN_RT
);
3152 perror_with_name (file
);
3157 make_cleanup ((make_cleanup_func
) fclose
, stream
);
3159 old_lines
.old_line
= source_line_number
;
3160 old_lines
.old_file
= source_file_name
;
3161 old_lines
.old_pre_error
= source_pre_error
;
3162 old_lines
.old_error_pre_print
= error_pre_print
;
3163 make_cleanup (source_cleanup_lines
, &old_lines
);
3164 source_line_number
= 0;
3165 source_file_name
= file
;
3166 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
3167 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
3168 make_cleanup (free
, source_pre_error
);
3169 /* This will get set every time we read a line. So it won't stay "" for
3171 error_pre_print
= "";
3173 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
3174 if (source_error_allocated
< needed_length
)
3176 source_error_allocated
*= 2;
3177 if (source_error_allocated
< needed_length
)
3178 source_error_allocated
= needed_length
;
3179 if (source_error
== NULL
)
3180 source_error
= xmalloc (source_error_allocated
);
3182 source_error
= xrealloc (source_error
, source_error_allocated
);
3185 read_command_file (stream
);
3187 do_cleanups (old_cleanups
);
3192 echo_command (text
, from_tty
)
3200 while ((c
= *p
++) != '\0')
3204 /* \ at end of argument is used after spaces
3205 so they won't be lost. */
3209 c
= parse_escape (&p
);
3211 printf_filtered ("%c", c
);
3214 printf_filtered ("%c", c
);
3217 /* Force this output to appear now. */
3219 gdb_flush (gdb_stdout
);
3224 dont_repeat_command (ignored
, from_tty
)
3228 *line
= 0; /* Can't call dont_repeat here because we're not
3229 necessarily reading from stdin. */
3232 /* Functions to manipulate command line editing control variables. */
3234 /* Number of commands to print in each call to show_commands. */
3235 #define Hist_print 10
3237 show_commands (args
, from_tty
)
3241 /* Index for history commands. Relative to history_base. */
3244 /* Number of the history entry which we are planning to display next.
3245 Relative to history_base. */
3248 /* The first command in the history which doesn't exist (i.e. one more
3249 than the number of the last command). Relative to history_base. */
3252 extern HIST_ENTRY
*history_get
PARAMS ((int));
3254 /* Print out some of the commands from the command history. */
3255 /* First determine the length of the history list. */
3256 hist_len
= history_size
;
3257 for (offset
= 0; offset
< history_size
; offset
++)
3259 if (!history_get (history_base
+ offset
))
3268 if (args
[0] == '+' && args
[1] == '\0')
3269 /* "info editing +" should print from the stored position. */
3272 /* "info editing <exp>" should print around command number <exp>. */
3273 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3275 /* "show commands" means print the last Hist_print commands. */
3278 num
= hist_len
- Hist_print
;
3284 /* If there are at least Hist_print commands, we want to display the last
3285 Hist_print rather than, say, the last 6. */
3286 if (hist_len
- num
< Hist_print
)
3288 num
= hist_len
- Hist_print
;
3293 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3295 printf_filtered ("%5d %s\n", history_base
+ offset
,
3296 (history_get (history_base
+ offset
))->line
);
3299 /* The next command we want to display is the next one that we haven't
3303 /* If the user repeats this command with return, it should do what
3304 "show commands +" does. This is unnecessary if arg is null,
3305 because "show commands +" is not useful after "show commands". */
3306 if (from_tty
&& args
)
3313 /* Called by do_setshow_command. */
3316 set_history_size_command (args
, from_tty
, c
)
3319 struct cmd_list_element
*c
;
3321 if (history_size
== INT_MAX
)
3322 unstifle_history ();
3323 else if (history_size
>= 0)
3324 stifle_history (history_size
);
3327 history_size
= INT_MAX
;
3328 error ("History size must be non-negative");
3334 set_history (args
, from_tty
)
3338 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3339 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3344 show_history (args
, from_tty
)
3348 cmd_show_list (showhistlist
, from_tty
, "");
3351 int info_verbose
= 0; /* Default verbose msgs off */
3353 /* Called by do_setshow_command. An elaborate joke. */
3356 set_verbose (args
, from_tty
, c
)
3359 struct cmd_list_element
*c
;
3361 char *cmdname
= "verbose";
3362 struct cmd_list_element
*showcmd
;
3364 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3368 c
->doc
= "Set verbose printing of informational messages.";
3369 showcmd
->doc
= "Show verbose printing of informational messages.";
3373 c
->doc
= "Set verbosity.";
3374 showcmd
->doc
= "Show verbosity.";
3379 float_handler (signo
)
3382 /* This message is based on ANSI C, section 4.7. Note that integer
3383 divide by zero causes this, so "float" is a misnomer. */
3384 signal (SIGFPE
, float_handler
);
3385 error ("Erroneous arithmetic operation.");
3399 enablebreaklist
= NULL
;
3404 showhistlist
= NULL
;
3405 unsethistlist
= NULL
;
3406 maintenancelist
= NULL
;
3407 maintenanceinfolist
= NULL
;
3408 maintenanceprintlist
= NULL
;
3409 setprintlist
= NULL
;
3410 showprintlist
= NULL
;
3411 setchecklist
= NULL
;
3412 showchecklist
= NULL
;
3415 /* Init the history buffer. Note that we are called after the init file(s)
3416 * have been read so that the user can change the history file via his
3417 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3418 * overrides all of this.
3426 tmpenv
= getenv ("HISTSIZE");
3428 history_size
= atoi (tmpenv
);
3429 else if (!history_size
)
3432 stifle_history (history_size
);
3434 tmpenv
= getenv ("GDBHISTFILE");
3436 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3437 else if (!history_filename
) {
3438 /* We include the current directory so that if the user changes
3439 directories the file written will be the same as the one
3441 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3443 read_history (history_filename
);
3449 struct cmd_list_element
*c
;
3451 #ifdef DEFAULT_PROMPT
3452 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3454 prompt
= savestring ("(gdb) ", 6);
3457 /* Set the important stuff up for command editing. */
3458 command_editing_p
= 1;
3459 history_expansion_p
= 0;
3460 write_history_p
= 0;
3462 /* Setup important stuff for command line editing. */
3463 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3464 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3465 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3466 rl_readline_name
= "gdb";
3468 /* Define the classes of commands.
3469 They will appear in the help list in the reverse of this order. */
3471 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3472 "Maintenance commands.\n\
3473 Some gdb commands are provided just for use by gdb maintainers.\n\
3474 These commands are subject to frequent change, and may not be as\n\
3475 well documented as user commands.",
3477 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3478 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3479 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3480 The commands in this class are those defined by the user.\n\
3481 Use the \"define\" command to define a command.", &cmdlist
);
3482 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3484 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3485 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3486 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3487 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3488 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3489 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3490 counting from zero for the innermost (currently executing) frame.\n\n\
3491 At any time gdb identifies one frame as the \"selected\" frame.\n\
3492 Variable lookups are done with respect to the selected frame.\n\
3493 When the program being debugged stops, gdb selects the innermost frame.\n\
3494 The commands below can be used to select other frames by number or address.",
3496 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3498 add_com ("pwd", class_files
, pwd_command
,
3499 "Print working directory. This is used for your program as well.");
3500 c
= add_cmd ("cd", class_files
, cd_command
,
3501 "Set working directory to DIR for debugger and program being debugged.\n\
3502 The change does not take effect for the program being debugged\n\
3503 until the next time it is started.", &cmdlist
);
3504 c
->completer
= filename_completer
;
3507 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3512 add_com ("echo", class_support
, echo_command
,
3513 "Print a constant string. Give string as argument.\n\
3514 C escape sequences may be used in the argument.\n\
3515 No newline is added at the end of the argument;\n\
3516 use \"\\n\" if you want a newline to be printed.\n\
3517 Since leading and trailing whitespace are ignored in command arguments,\n\
3518 if you want to print some you must use \"\\\" before leading whitespace\n\
3519 to be printed or after trailing whitespace.");
3520 add_com ("document", class_support
, document_command
,
3521 "Document a user-defined command.\n\
3522 Give command name as argument. Give documentation on following lines.\n\
3523 End with a line of just \"end\".");
3524 add_com ("define", class_support
, define_command
,
3525 "Define a new command name. Command name is argument.\n\
3526 Definition appears on following lines, one command per line.\n\
3527 End with a line of just \"end\".\n\
3528 Use the \"document\" command to give documentation for the new command.\n\
3529 Commands defined in this way may have up to ten arguments.");
3532 c
= add_cmd ("source", class_support
, source_command
,
3533 "Read commands from a file named FILE.\n\
3534 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3535 when gdb is started.", &cmdlist
);
3537 /* Punt file name, we can't help it easily. */
3538 c
= add_cmd ("source", class_support
, source_command
,
3539 "Read commands from a file named FILE.\n\
3540 Note that the file \".gdbinit\" is read automatically in this way\n\
3541 when gdb is started.", &cmdlist
);
3543 c
->completer
= filename_completer
;
3545 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3546 add_com ("help", class_support
, help_command
, "Print list of commands.");
3547 add_com_alias ("q", "quit", class_support
, 1);
3548 add_com_alias ("h", "help", class_support
, 1);
3550 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
3551 Primarily used inside of user-defined commands that should not be repeated when\n\
3554 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3557 add_show_from_set (c
, &showlist
);
3558 c
->function
.sfunc
= set_verbose
;
3559 set_verbose (NULL
, 0, c
);
3562 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3563 "Set editing of command lines as they are typed.\n\
3564 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3565 Without an argument, command line editing is enabled. To edit, use\n\
3566 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3569 add_prefix_cmd ("history", class_support
, set_history
,
3570 "Generic command for setting command history parameters.",
3571 &sethistlist
, "set history ", 0, &setlist
);
3572 add_prefix_cmd ("history", class_support
, show_history
,
3573 "Generic command for showing command history parameters.",
3574 &showhistlist
, "show history ", 0, &showlist
);
3577 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3578 "Set history expansion on command input.\n\
3579 Without an argument, history expansion is enabled.", &sethistlist
),
3583 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3584 "Set saving of the history record on exit.\n\
3585 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
3586 Without an argument, saving is enabled.", &sethistlist
),
3589 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3590 "Set the size of the command history, \n\
3591 ie. the number of previous commands to keep a record of.", &sethistlist
);
3592 add_show_from_set (c
, &showhistlist
);
3593 c
->function
.sfunc
= set_history_size_command
;
3596 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3597 "Set the filename in which to record the command history\n\
3598 (the list of previous commands of which a record is kept).", &sethistlist
),
3602 (add_set_cmd ("confirm", class_support
, var_boolean
,
3604 "Set whether to confirm potentially dangerous operations.",
3608 add_prefix_cmd ("info", class_info
, info_command
,
3609 "Generic command for showing things about the program being debugged.",
3610 &infolist
, "info ", 0, &cmdlist
);
3611 add_com_alias ("i", "info", class_info
, 1);
3613 add_com ("complete", class_obscure
, complete_command
,
3614 "List the completions for the rest of the line as a command.");
3616 add_prefix_cmd ("show", class_info
, show_command
,
3617 "Generic command for showing things about the debugger.",
3618 &showlist
, "show ", 0, &cmdlist
);
3619 /* Another way to get at the same thing. */
3620 add_info ("set", show_command
, "Show all GDB settings.");
3622 add_cmd ("commands", no_class
, show_commands
,
3623 "Show the history of commands you typed.\n\
3624 You can supply a command number to start with, or a `+' to start after\n\
3625 the previous command number shown.",
3628 add_cmd ("version", no_class
, show_version
,
3629 "Show what version of GDB this is.", &showlist
);
3631 add_com ("while", class_support
, while_command
,
3632 "Execute nested commands WHILE the conditional expression is non zero.\n\
3633 The conditional expression must follow the word `while' and must in turn be\n\
3634 followed by a new line. The nested commands must be entered one per line,\n\
3635 and should be terminated by the word `end'.");
3637 add_com ("if", class_support
, if_command
,
3638 "Execute nested commands once IF the conditional expression is non zero.\n\
3639 The conditional expression must follow the word `if' and must in turn be\n\
3640 followed by a new line. The nested commands must be entered one per line,\n\
3641 and should be terminated by the word 'else' or `end'. If an else clause\n\
3642 is used, the same rules apply to its nested commands as to the first ones.");
3644 /* If target is open when baud changes, it doesn't take effect until the
3645 next open (I think, not sure). */
3646 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3647 var_zinteger
, (char *)&baud_rate
,
3648 "Set baud rate for remote serial I/O.\n\
3649 This value is used to set the speed of the serial port when debugging\n\
3650 using remote targets.", &setlist
),
3654 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3655 "Set debugging of remote protocol.\n\
3656 When enabled, each packet sent or received with the remote target\n\
3657 is displayed.", &setlist
),
3661 add_set_cmd ("remotetimeout", no_class
, var_integer
, (char *)&remote_timeout
,
3662 "Set timeout limit to wait for target to respond.\n\
3663 This value is used to set the time limit for gdb to wait for a response\n\
3664 from the target.", &setlist
),
3667 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
3668 (char *)&annotation_level
, "Set annotation_level.\n\
3669 0 == normal; 1 == fullname (for use when running under emacs)\n\
3670 2 == output annotated suitably for use by programs that control GDB.",
3672 c
= add_show_from_set (c
, &showlist
);