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 register REGNO. -1 means
438 that the caller does not know which register changed or
439 that several registers have changed (see value_assign).*/
440 void (*register_changed_hook
) PARAMS ((int regno
));
442 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
443 void (*memory_changed_hook
) PARAMS ((CORE_ADDR addr
, int len
));
445 /* Called when going to wait for the target. Usually allows the GUI to run
446 while waiting for target events. */
448 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
*status
));
450 /* Used by UI as a wrapper around command execution. May do various things
451 like enabling/disabling buttons, etc... */
453 void (*call_command_hook
) PARAMS ((struct cmd_list_element
*c
, char *cmd
,
456 /* Called when the current thread changes. Argument is thread id. */
458 void (*context_hook
) PARAMS ((int id
));
460 /* Takes control from error (). Typically used to prevent longjmps out of the
461 middle of the GUI. Usually used in conjunction with a catch routine. */
463 NORETURN
void (*error_hook
) PARAMS ((void)) ATTR_NORETURN
;
466 /* Where to go for return_to_top_level (RETURN_ERROR). */
467 SIGJMP_BUF error_return
;
468 /* Where to go for return_to_top_level (RETURN_QUIT). */
469 SIGJMP_BUF quit_return
;
471 /* Return for reason REASON. This generally gets back to the command
472 loop, but can be caught via catch_errors. */
475 return_to_top_level (reason
)
476 enum return_reason reason
;
481 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
482 I can think of a reason why that is vital, though). */
483 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
485 disable_current_display ();
486 do_cleanups (ALL_CLEANUPS
);
488 if (annotation_level
> 1)
499 (NORETURN
void) SIGLONGJMP
500 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
503 /* Call FUNC with arg ARGS, catching any errors. If there is no
504 error, return the value returned by FUNC. If there is an error,
505 print ERRSTRING, print the specific error message, then return
508 Must not be called with immediate_quit in effect (bad things might
509 happen, say we got a signal in the middle of a memcpy to quit_return).
510 This is an OK restriction; with very few exceptions immediate_quit can
511 be replaced by judicious use of QUIT.
513 MASK specifies what to catch; it is normally set to
514 RETURN_MASK_ALL, if for no other reason than that the code which
515 calls catch_errors might not be set up to deal with a quit which
516 isn't caught. But if the code can deal with it, it generally
517 should be RETURN_MASK_ERROR, unless for some reason it is more
518 useful to abort only the portion of the operation inside the
519 catch_errors. Note that quit should return to the command line
520 fairly quickly, even if some further processing is being done. */
523 catch_errors (func
, args
, errstring
, mask
)
524 catch_errors_ftype
*func
;
529 SIGJMP_BUF saved_error
;
530 SIGJMP_BUF saved_quit
;
533 struct cleanup
*saved_cleanup_chain
;
534 char *saved_error_pre_print
;
535 char *saved_quit_pre_print
;
537 saved_cleanup_chain
= save_cleanups ();
538 saved_error_pre_print
= error_pre_print
;
539 saved_quit_pre_print
= quit_pre_print
;
541 if (mask
& RETURN_MASK_ERROR
)
543 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (SIGJMP_BUF
));
544 error_pre_print
= errstring
;
546 if (mask
& RETURN_MASK_QUIT
)
548 memcpy (saved_quit
, quit_return
, sizeof (SIGJMP_BUF
));
549 quit_pre_print
= errstring
;
552 if (SIGSETJMP (tmp_jmp
) == 0)
554 if (mask
& RETURN_MASK_ERROR
)
555 memcpy (error_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
556 if (mask
& RETURN_MASK_QUIT
)
557 memcpy (quit_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
558 val
= (*func
) (args
);
563 restore_cleanups (saved_cleanup_chain
);
565 if (mask
& RETURN_MASK_ERROR
)
567 memcpy (error_return
, saved_error
, sizeof (SIGJMP_BUF
));
568 error_pre_print
= saved_error_pre_print
;
570 if (mask
& RETURN_MASK_QUIT
)
572 memcpy (quit_return
, saved_quit
, sizeof (SIGJMP_BUF
));
573 quit_pre_print
= saved_quit_pre_print
;
578 /* Handler for SIGHUP. */
585 catch_errors (quit_cover
, NULL
,
586 "Could not kill the program being debugged", RETURN_MASK_ALL
);
587 signal (SIGHUP
, SIG_DFL
);
588 kill (getpid (), SIGHUP
);
591 /* Just a little helper function for disconnect(). */
597 caution
= 0; /* Throw caution to the wind -- we're exiting.
598 This prevents asking the user dumb questions. */
599 quit_command((char *)0, 0);
602 #endif /* defined SIGHUP */
604 /* Line number we are currently in in a file which is being sourced. */
605 static int source_line_number
;
607 /* Name of the file we are sourcing. */
608 static char *source_file_name
;
610 /* Buffer containing the error_pre_print used by the source stuff.
612 static char *source_error
;
613 static int source_error_allocated
;
615 /* Something to glom on to the start of error_pre_print if source_file_name
617 static char *source_pre_error
;
619 /* Clean up on error during a "source" command (or execution of a
620 user-defined command). */
623 source_cleanup (stream
)
626 /* Restore the previous input stream. */
630 /* Read commands from STREAM. */
632 read_command_file (stream
)
635 struct cleanup
*cleanups
;
637 cleanups
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
640 do_cleanups (cleanups
);
643 extern void init_proc
PARAMS ((void));
645 void (*pre_init_ui_hook
) PARAMS ((void));
651 if (pre_init_ui_hook
)
654 /* Run the init function of each source file */
656 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
657 current_directory
= gdb_dirbuf
;
659 init_cmd_lists (); /* This needs to be done first */
660 initialize_targets (); /* Setup target_terminal macros for utils.c */
661 initialize_utils (); /* Make errors and warnings possible */
662 initialize_all_files ();
663 init_main (); /* But that omits this file! Do it now */
668 /* We need a default language for parsing expressions, so simple things like
669 "set width 0" won't fail if no language is explicitly set in a config file
670 or implicitly set by reading an executable during startup. */
671 set_language (language_c
);
672 expected_language
= current_language
; /* don't warn about the change. */
675 init_ui_hook (argv0
);
678 /* Allocate, initialize a new command line structure for one of the
679 control commands (if/while). */
681 static struct command_line
*
682 build_command_line (type
, args
)
683 enum command_control_type type
;
686 struct command_line
*cmd
;
689 error ("if/while commands require arguments.\n");
691 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
693 cmd
->control_type
= type
;
697 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
699 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
700 cmd
->line
= savestring (args
, strlen (args
));
704 /* Build and return a new command structure for the control commands
705 such as "if" and "while". */
707 static struct command_line
*
708 get_command_line (type
, arg
)
709 enum command_control_type type
;
712 struct command_line
*cmd
;
713 struct cleanup
*old_chain
= NULL
;
715 /* Allocate and build a new command line structure. */
716 cmd
= build_command_line (type
, arg
);
718 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
, &cmd
);
720 /* Read in the body of this command. */
721 if (recurse_read_control_structure (cmd
) == invalid_control
)
723 warning ("error reading in control structure\n");
724 do_cleanups (old_chain
);
728 discard_cleanups (old_chain
);
732 /* Recursively print a command (including full control structures). */
734 print_command_line (cmd
, depth
)
735 struct command_line
*cmd
;
742 for (i
= 0; i
< depth
; i
++)
743 fputs_filtered (" ", gdb_stdout
);
746 /* A simple command, print it and return. */
747 if (cmd
->control_type
== simple_control
)
749 fputs_filtered (cmd
->line
, gdb_stdout
);
750 fputs_filtered ("\n", gdb_stdout
);
754 /* loop_continue to jump to the start of a while loop, print it
756 if (cmd
->control_type
== continue_control
)
758 fputs_filtered ("loop_continue\n", gdb_stdout
);
762 /* loop_break to break out of a while loop, print it and return. */
763 if (cmd
->control_type
== break_control
)
765 fputs_filtered ("loop_break\n", gdb_stdout
);
769 /* A while command. Recursively print its subcommands before returning. */
770 if (cmd
->control_type
== while_control
)
772 struct command_line
*list
;
773 fputs_filtered ("while ", gdb_stdout
);
774 fputs_filtered (cmd
->line
, gdb_stdout
);
775 fputs_filtered ("\n", gdb_stdout
);
776 list
= *cmd
->body_list
;
779 print_command_line (list
, depth
+ 1);
784 /* An if command. Recursively print both arms before returning. */
785 if (cmd
->control_type
== if_control
)
787 fputs_filtered ("if ", gdb_stdout
);
788 fputs_filtered (cmd
->line
, gdb_stdout
);
789 fputs_filtered ("\n", gdb_stdout
);
791 print_command_line (cmd
->body_list
[0], depth
+ 1);
793 /* Show the false arm if it exists. */
794 if (cmd
->body_count
== 2)
798 for (i
= 0; i
< depth
; i
++)
799 fputs_filtered (" ", gdb_stdout
);
801 fputs_filtered ("else\n", gdb_stdout
);
802 print_command_line (cmd
->body_list
[1], depth
+ 1);
806 for (i
= 0; i
< depth
; i
++)
807 fputs_filtered (" ", gdb_stdout
);
809 fputs_filtered ("end\n", gdb_stdout
);
813 /* Execute the command in CMD. */
815 enum command_control_type
816 execute_control_command (cmd
)
817 struct command_line
*cmd
;
819 struct expression
*expr
;
820 struct command_line
*current
;
821 struct cleanup
*old_chain
= 0;
825 enum command_control_type ret
;
828 switch (cmd
->control_type
)
831 /* A simple command, execute it and return. */
832 new_line
= insert_args (cmd
->line
);
834 return invalid_control
;
835 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
837 execute_command (new_line
, 0);
838 ret
= cmd
->control_type
;
841 case continue_control
:
843 /* Return for "continue", and "break" so we can either
844 continue the loop at the top, or break out. */
845 ret
= cmd
->control_type
;
850 /* Parse the loop control expression for the while statement. */
851 new_line
= insert_args (cmd
->line
);
853 return invalid_control
;
854 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
856 expr
= parse_expression (new_line
);
857 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
859 ret
= simple_control
;
862 /* Keep iterating so long as the expression is true. */
869 /* Evaluate the expression. */
870 val_mark
= value_mark ();
871 val
= evaluate_expression (expr
);
872 cond_result
= value_true (val
);
873 value_free_to_mark (val_mark
);
875 /* If the value is false, then break out of the loop. */
879 /* Execute the body of the while statement. */
880 current
= *cmd
->body_list
;
883 ret
= execute_control_command (current
);
885 /* If we got an error, or a "break" command, then stop
887 if (ret
== invalid_control
|| ret
== break_control
)
893 /* If we got a "continue" command, then restart the loop
895 if (ret
== continue_control
)
898 /* Get the next statement. */
899 current
= current
->next
;
903 /* Reset RET so that we don't recurse the break all the way down. */
904 if (ret
== break_control
)
905 ret
= simple_control
;
912 new_line
= insert_args (cmd
->line
);
914 return invalid_control
;
915 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
917 /* Parse the conditional for the if statement. */
918 expr
= parse_expression (new_line
);
919 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
922 ret
= simple_control
;
924 /* Evaluate the conditional. */
925 val_mark
= value_mark ();
926 val
= evaluate_expression (expr
);
928 /* Choose which arm to take commands from based on the value of the
929 conditional expression. */
930 if (value_true (val
))
931 current
= *cmd
->body_list
;
932 else if (cmd
->body_count
== 2)
933 current
= *(cmd
->body_list
+ 1);
934 value_free_to_mark (val_mark
);
936 /* Execute commands in the given arm. */
939 ret
= execute_control_command (current
);
941 /* If we got an error, get out. */
942 if (ret
!= simple_control
)
945 /* Get the next statement in the body. */
946 current
= current
->next
;
953 warning ("Invalid control type in command structure.");
954 return invalid_control
;
958 do_cleanups (old_chain
);
963 /* "while" command support. Executes a body of statements while the
964 loop condition is nonzero. */
967 while_command (arg
, from_tty
)
971 struct command_line
*command
= NULL
;
974 command
= get_command_line (while_control
, arg
);
979 execute_control_command (command
);
980 free_command_lines (&command
);
983 /* "if" command support. Execute either the true or false arm depending
984 on the value of the if conditional. */
987 if_command (arg
, from_tty
)
991 struct command_line
*command
= NULL
;
994 command
= get_command_line (if_control
, arg
);
999 execute_control_command (command
);
1000 free_command_lines (&command
);
1007 struct user_args
*oargs
= user_args
;
1009 fatal ("Internal error, arg_cleanup called with no user args.\n");
1011 user_args
= user_args
->next
;
1015 /* Bind the incomming arguments for a user defined command to
1016 $arg0, $arg1 ... $argMAXUSERARGS. */
1018 static struct cleanup
*
1022 struct user_args
*args
;
1023 struct cleanup
*old_chain
;
1024 unsigned int arg_count
= 0;
1026 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
1027 memset (args
, 0, sizeof (struct user_args
));
1029 args
->next
= user_args
;
1032 old_chain
= make_cleanup ((make_cleanup_func
) arg_cleanup
, 0);
1044 if (arg_count
>= MAXUSERARGS
)
1046 error ("user defined function may only have %d arguments.\n",
1051 /* Strip whitespace. */
1052 while (*p
== ' ' || *p
== '\t')
1055 /* P now points to an argument. */
1057 user_args
->a
[arg_count
].arg
= p
;
1059 /* Get to the end of this argument. */
1062 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
1068 else if (*p
== '\\')
1091 user_args
->a
[arg_count
].len
= p
- start_arg
;
1098 /* Given character string P, return a point to the first argument ($arg),
1099 or NULL if P contains no arguments. */
1105 while ((p
= strchr (p
, '$')))
1107 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1114 /* Insert the user defined arguments stored in user_arg into the $arg
1115 arguments found in line, with the updated copy being placed into nline. */
1121 char *p
, *save_line
, *new_line
;
1124 /* First we need to know how much memory to allocate for the new line. */
1127 while ((p
= locate_arg (line
)))
1132 if (i
>= user_args
->count
)
1134 error ("Missing argument %d in user function.\n", i
);
1137 len
+= user_args
->a
[i
].len
;
1141 /* Don't forget the tail. */
1142 len
+= strlen (line
);
1144 /* Allocate space for the new line and fill it in. */
1145 new_line
= (char *)xmalloc (len
+ 1);
1146 if (new_line
== NULL
)
1149 /* Restore pointer to beginning of old line. */
1152 /* Save pointer to beginning of new line. */
1153 save_line
= new_line
;
1155 while ((p
= locate_arg (line
)))
1159 memcpy (new_line
, line
, p
- line
);
1160 new_line
+= p
- line
;
1163 len
= user_args
->a
[i
].len
;
1166 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1171 /* Don't forget the tail. */
1172 strcpy (new_line
, line
);
1174 /* Return a pointer to the beginning of the new line. */
1179 execute_user_command (c
, args
)
1180 struct cmd_list_element
*c
;
1183 register struct command_line
*cmdlines
;
1184 struct cleanup
*old_chain
;
1185 enum command_control_type ret
;
1187 old_chain
= setup_user_args (args
);
1189 cmdlines
= c
->user_commands
;
1194 /* Set the instream to 0, indicating execution of a
1195 user-defined function. */
1196 old_chain
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
1197 instream
= (FILE *) 0;
1200 ret
= execute_control_command (cmdlines
);
1201 if (ret
!= simple_control
&& ret
!= break_control
)
1203 warning ("Error in control structure.\n");
1206 cmdlines
= cmdlines
->next
;
1208 do_cleanups (old_chain
);
1211 /* Execute the line P as a command.
1212 Pass FROM_TTY as second argument to the defining function. */
1215 execute_command (p
, from_tty
)
1219 register struct cmd_list_element
*c
;
1220 register enum language flang
;
1221 static int warned
= 0;
1222 /* FIXME: These should really be in an appropriate header file */
1223 extern void serial_log_command
PARAMS ((const char *));
1227 /* Force cleanup of any alloca areas if using C alloca instead of
1228 a builtin alloca. */
1231 /* This can happen when command_line_input hits end of file. */
1235 serial_log_command (p
);
1237 while (*p
== ' ' || *p
== '\t') p
++;
1242 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1243 /* Pass null arg rather than an empty one. */
1246 /* Clear off trailing whitespace, except for set and complete command. */
1247 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1249 p
= arg
+ strlen (arg
) - 1;
1250 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1255 /* If this command has been hooked, run the hook first. */
1257 execute_user_command (c
->hook
, (char *)0);
1259 if (c
->class == class_user
)
1260 execute_user_command (c
, arg
);
1261 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1262 do_setshow_command (arg
, from_tty
& caution
, c
);
1263 else if (c
->function
.cfunc
== NO_FUNCTION
)
1264 error ("That is not a command, just a help topic.");
1265 else if (call_command_hook
)
1266 call_command_hook (c
, arg
, from_tty
& caution
);
1268 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1271 /* Tell the user if the language has changed (except first time). */
1272 if (current_language
!= expected_language
)
1274 if (language_mode
== language_mode_auto
) {
1275 language_info (1); /* Print what changed. */
1280 /* Warn the user if the working language does not match the
1281 language of the current frame. Only warn the user if we are
1282 actually running the program, i.e. there is a stack. */
1283 /* FIXME: This should be cacheing the frame and only running when
1284 the frame changes. */
1286 if (target_has_stack
)
1288 flang
= get_frame_language ();
1290 && flang
!= language_unknown
1291 && flang
!= current_language
->la_language
)
1293 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1301 command_loop_marker (foo
)
1306 /* Read commands from `instream' and execute them
1307 until end of file or error reading instream. */
1312 struct cleanup
*old_chain
;
1314 int stdin_is_tty
= ISATTY (stdin
);
1315 long time_at_cmd_start
;
1317 long space_at_cmd_start
= 0;
1319 extern int display_time
;
1320 extern int display_space
;
1322 while (instream
&& !feof (instream
))
1325 extern int insert_mode
;
1327 if (window_hook
&& instream
== stdin
)
1328 (*window_hook
) (instream
, prompt
);
1331 if (instream
== stdin
&& stdin_is_tty
)
1332 reinitialize_more_filter ();
1333 old_chain
= make_cleanup ((make_cleanup_func
) command_loop_marker
, 0);
1336 /* A bit of paranoia: I want to make sure the "insert_mode" global
1337 * is clear except when it is being used for command-line editing
1338 * (see tuiIO.c, utils.c); otherwise normal output will
1339 * get messed up in the TUI. So clear it before/after
1340 * the command-line-input call. - RT
1344 /* Get a command-line. This calls the readline package. */
1345 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1346 instream
== stdin
, "prompt");
1353 time_at_cmd_start
= get_run_time ();
1358 extern char **environ
;
1359 char *lim
= (char *) sbrk (0);
1361 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1365 execute_command (command
, instream
== stdin
);
1366 /* Do any commands attached to breakpoint we stopped at. */
1367 bpstat_do_actions (&stop_bpstat
);
1368 do_cleanups (old_chain
);
1372 long cmd_time
= get_run_time () - time_at_cmd_start
;
1374 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1375 cmd_time
/ 1000000, cmd_time
% 1000000);
1381 extern char **environ
;
1382 char *lim
= (char *) sbrk (0);
1383 long space_now
= lim
- (char *) &environ
;
1384 long space_diff
= space_now
- space_at_cmd_start
;
1386 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1388 (space_diff
>= 0 ? '+' : '-'),
1395 /* Commands call this if they do not want to be repeated by null lines. */
1403 /* If we aren't reading from standard input, we are saving the last
1404 thing read from stdin in line and don't want to delete it. Null lines
1405 won't repeat here in any case. */
1406 if (instream
== stdin
)
1410 /* Read a line from the stream "instream" without command line editing.
1412 It prints PRROMPT once at the start.
1413 Action is compatible with "readline", e.g. space for the result is
1414 malloc'd and should be freed by the caller.
1416 A NULL return means end of file. */
1418 gdb_readline (prrompt
)
1423 int input_index
= 0;
1424 int result_size
= 80;
1428 /* Don't use a _filtered function here. It causes the assumed
1429 character position to be off, since the newline we read from
1430 the user is not accounted for. */
1431 fputs_unfiltered (prrompt
, gdb_stdout
);
1433 /* Move to a new line so the entered line doesn't have a prompt
1434 on the front of it. */
1435 fputs_unfiltered ("\n", gdb_stdout
);
1437 gdb_flush (gdb_stdout
);
1440 result
= (char *) xmalloc (result_size
);
1444 /* Read from stdin if we are executing a user defined command.
1445 This is the right thing for prompt_for_continue, at least. */
1446 c
= fgetc (instream
? instream
: stdin
);
1450 if (input_index
> 0)
1451 /* The last line does not end with a newline. Return it, and
1452 if we are called again fgetc will still return EOF and
1453 we'll return NULL then. */
1460 #ifndef CRLF_SOURCE_FILES
1464 if (input_index
> 0 && result
[input_index
- 1] == '\r')
1470 result
[input_index
++] = c
;
1471 while (input_index
>= result_size
)
1474 result
= (char *) xrealloc (result
, result_size
);
1478 result
[input_index
++] = '\0';
1482 /* Variables which control command line editing and history
1483 substitution. These variables are given default values at the end
1485 static int command_editing_p
;
1486 static int history_expansion_p
;
1487 static int write_history_p
;
1488 static int history_size
;
1489 static char *history_filename
;
1491 /* readline uses the word breaks for two things:
1492 (1) In figuring out where to point the TEXT parameter to the
1493 rl_completion_entry_function. Since we don't use TEXT for much,
1494 it doesn't matter a lot what the word breaks are for this purpose, but
1495 it does affect how much stuff M-? lists.
1496 (2) If one of the matches contains a word break character, readline
1497 will quote it. That's why we switch between
1498 gdb_completer_word_break_characters and
1499 gdb_completer_command_word_break_characters. I'm not sure when
1500 we need this behavior (perhaps for funky characters in C++ symbols?). */
1502 /* Variables which are necessary for fancy command line editing. */
1503 char *gdb_completer_word_break_characters
=
1504 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1506 /* When completing on command names, we remove '-' from the list of
1507 word break characters, since we use it in command names. If the
1508 readline library sees one in any of the current completion strings,
1509 it thinks that the string needs to be quoted and automatically supplies
1511 char *gdb_completer_command_word_break_characters
=
1512 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1514 /* Characters that can be used to quote completion strings. Note that we
1515 can't include '"' because the gdb C parser treats such quoted sequences
1517 char *gdb_completer_quote_characters
=
1520 /* Functions that are used as part of the fancy command line editing. */
1522 /* This can be used for functions which don't want to complete on symbols
1523 but don't want to complete on anything else either. */
1526 noop_completer (text
, prefix
)
1533 /* Complete on filenames. */
1535 filename_completer (text
, word
)
1539 /* From readline. */
1540 extern char *filename_completion_function
PARAMS ((char *, int));
1541 int subsequent_name
;
1543 int return_val_used
;
1544 int return_val_alloced
;
1546 return_val_used
= 0;
1547 /* Small for testing. */
1548 return_val_alloced
= 1;
1549 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1551 subsequent_name
= 0;
1555 p
= filename_completion_function (text
, subsequent_name
);
1556 if (return_val_used
>= return_val_alloced
)
1558 return_val_alloced
*= 2;
1560 (char **) xrealloc (return_val
,
1561 return_val_alloced
* sizeof (char *));
1565 return_val
[return_val_used
++] = p
;
1568 /* Like emacs, don't complete on old versions. Especially useful
1569 in the "source" command. */
1570 if (p
[strlen (p
) - 1] == '~')
1576 /* Return exactly p. */
1577 return_val
[return_val_used
++] = p
;
1578 else if (word
> text
)
1580 /* Return some portion of p. */
1581 q
= xmalloc (strlen (p
) + 5);
1582 strcpy (q
, p
+ (word
- text
));
1583 return_val
[return_val_used
++] = q
;
1588 /* Return some of TEXT plus p. */
1589 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1590 strncpy (q
, word
, text
- word
);
1591 q
[text
- word
] = '\0';
1593 return_val
[return_val_used
++] = q
;
1597 subsequent_name
= 1;
1600 /* There is no way to do this just long enough to affect quote inserting
1601 without also affecting the next completion. This should be fixed in
1603 /* Insure that readline does the right thing
1604 with respect to inserting quotes. */
1605 rl_completer_word_break_characters
= "";
1610 /* Here are some useful test cases for completion. FIXME: These should
1611 be put in the test suite. They should be tested with both M-? and TAB.
1613 "show output-" "radix"
1614 "show output" "-radix"
1615 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1616 "p " ambiguous (all symbols)
1617 "info t foo" no completions
1618 "info t " no completions
1619 "info t" ambiguous ("info target", "info terminal", etc.)
1620 "info ajksdlfk" no completions
1621 "info ajksdlfk " no completions
1623 "info " ambiguous (all info commands)
1624 "p \"a" no completions (string constant)
1625 "p 'a" ambiguous (all symbols starting with a)
1626 "p b-a" ambiguous (all symbols starting with a)
1627 "p b-" ambiguous (all symbols)
1628 "file Make" "file" (word break hard to screw up here)
1629 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1632 /* Generate completions one by one for the completer. Each time we are
1633 called return another potential completion to the caller.
1634 line_completion just completes on commands or passes the buck to the
1635 command's completer function, the stuff specific to symbol completion
1636 is in make_symbol_completion_list.
1638 TEXT is the caller's idea of the "word" we are looking at.
1640 MATCHES is the number of matches that have currently been collected from
1641 calling this completion function. When zero, then we need to initialize,
1642 otherwise the initialization has already taken place and we can just
1643 return the next potential completion string.
1645 LINE_BUFFER is available to be looked at; it contains the entire text
1646 of the line. POINT is the offset in that line of the cursor. You
1647 should pretend that the line ends at POINT.
1649 Returns NULL if there are no more completions, else a pointer to a string
1650 which is a possible completion, it is the caller's responsibility to
1654 line_completion_function (text
, matches
, line_buffer
, point
)
1660 static char **list
= (char **)NULL
; /* Cache of completions */
1661 static int index
; /* Next cached completion */
1662 char *output
= NULL
;
1663 char *tmp_command
, *p
;
1664 /* Pointer within tmp_command which corresponds to text. */
1666 struct cmd_list_element
*c
, *result_list
;
1670 /* The caller is beginning to accumulate a new set of completions, so
1671 we need to find all of them now, and cache them for returning one at
1672 a time on future calls. */
1676 /* Free the storage used by LIST, but not by the strings inside.
1677 This is because rl_complete_internal () frees the strings. */
1683 /* Choose the default set of word break characters to break completions.
1684 If we later find out that we are doing completions on command strings
1685 (as opposed to strings supplied by the individual command completer
1686 functions, which can be any string) then we will switch to the
1687 special word break set for command strings, which leaves out the
1688 '-' character used in some commands. */
1690 rl_completer_word_break_characters
=
1691 gdb_completer_word_break_characters
;
1693 /* Decide whether to complete on a list of gdb commands or on symbols. */
1694 tmp_command
= (char *) alloca (point
+ 1);
1697 strncpy (tmp_command
, line_buffer
, point
);
1698 tmp_command
[point
] = '\0';
1699 /* Since text always contains some number of characters leading up
1700 to point, we can find the equivalent position in tmp_command
1701 by subtracting that many characters from the end of tmp_command. */
1702 word
= tmp_command
+ point
- strlen (text
);
1706 /* An empty line we want to consider ambiguous; that is, it
1707 could be any command. */
1708 c
= (struct cmd_list_element
*) -1;
1713 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1716 /* Move p up to the next interesting thing. */
1717 while (*p
== ' ' || *p
== '\t')
1724 /* It is an unrecognized command. So there are no
1725 possible completions. */
1728 else if (c
== (struct cmd_list_element
*) -1)
1732 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1733 doesn't advance over that thing itself. Do so now. */
1735 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1737 if (q
!= tmp_command
+ point
)
1739 /* There is something beyond the ambiguous
1740 command, so there are no possible completions. For
1741 example, "info t " or "info t foo" does not complete
1742 to anything, because "info t" can be "info target" or
1748 /* We're trying to complete on the command which was ambiguous.
1749 This we can deal with. */
1752 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1757 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1759 /* Insure that readline does the right thing with respect to
1760 inserting quotes. */
1761 rl_completer_word_break_characters
=
1762 gdb_completer_command_word_break_characters
;
1767 /* We've recognized a full command. */
1769 if (p
== tmp_command
+ point
)
1771 /* There is no non-whitespace in the line beyond the command. */
1773 if (p
[-1] == ' ' || p
[-1] == '\t')
1775 /* The command is followed by whitespace; we need to complete
1776 on whatever comes after command. */
1779 /* It is a prefix command; what comes after it is
1780 a subcommand (e.g. "info "). */
1781 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1783 /* Insure that readline does the right thing
1784 with respect to inserting quotes. */
1785 rl_completer_word_break_characters
=
1786 gdb_completer_command_word_break_characters
;
1790 list
= complete_on_enum (c
->enums
, p
, word
);
1791 rl_completer_word_break_characters
=
1792 gdb_completer_command_word_break_characters
;
1796 /* It is a normal command; what comes after it is
1797 completed by the command's completer function. */
1798 list
= (*c
->completer
) (p
, word
);
1803 /* The command is not followed by whitespace; we need to
1804 complete on the command itself. e.g. "p" which is a
1805 command itself but also can complete to "print", "ptype"
1809 /* Find the command we are completing on. */
1811 while (q
> tmp_command
)
1813 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1819 list
= complete_on_cmdlist (result_list
, q
, word
);
1821 /* Insure that readline does the right thing
1822 with respect to inserting quotes. */
1823 rl_completer_word_break_characters
=
1824 gdb_completer_command_word_break_characters
;
1829 /* There is non-whitespace beyond the command. */
1831 if (c
->prefixlist
&& !c
->allow_unknown
)
1833 /* It is an unrecognized subcommand of a prefix command,
1834 e.g. "info adsfkdj". */
1839 list
= complete_on_enum (c
->enums
, p
, word
);
1843 /* It is a normal command. */
1844 list
= (*c
->completer
) (p
, word
);
1850 /* If we found a list of potential completions during initialization then
1851 dole them out one at a time. The vector of completions is NULL
1852 terminated, so after returning the last one, return NULL (and continue
1853 to do so) each time we are called after that, until a new list is
1858 output
= list
[index
];
1866 /* Can't do this because readline hasn't yet checked the word breaks
1867 for figuring out whether to insert a quote. */
1869 /* Make sure the word break characters are set back to normal for the
1870 next time that readline tries to complete something. */
1871 rl_completer_word_break_characters
=
1872 gdb_completer_word_break_characters
;
1878 /* Line completion interface function for readline. */
1881 readline_line_completion_function (text
, matches
)
1885 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1888 /* Skip over a possibly quoted word (as defined by the quote characters
1889 and word break characters the completer uses). Returns pointer to the
1890 location after the "word". */
1896 char quote_char
= '\0';
1899 for (scan
= str
; *scan
!= '\0'; scan
++)
1901 if (quote_char
!= '\0')
1903 /* Ignore everything until the matching close quote char */
1904 if (*scan
== quote_char
)
1906 /* Found matching close quote. */
1911 else if (strchr (gdb_completer_quote_characters
, *scan
))
1913 /* Found start of a quoted string. */
1916 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1930 #if STOP_SIGNAL == SIGTSTP
1931 signal (SIGTSTP
, SIG_DFL
);
1933 kill (getpid (), SIGTSTP
);
1934 signal (SIGTSTP
, stop_sig
);
1936 signal (STOP_SIGNAL
, stop_sig
);
1938 printf_unfiltered ("%s", prompt
);
1939 gdb_flush (gdb_stdout
);
1941 /* Forget about any previous command -- null line now will do nothing. */
1944 #endif /* STOP_SIGNAL */
1946 /* Initialize signal handlers. */
1951 /* Under System V the default disposition of a signal is reinstated after
1952 the signal is caught and delivered to an application process. On such
1953 systems one must restore the replacement signal handler if one wishes
1954 to continue handling the signal in one's program. On BSD systems this
1955 is not needed but it is harmless, and it simplifies the code to just do
1956 it unconditionally. */
1957 signal (signo
, do_nothing
);
1963 signal (SIGINT
, request_quit
);
1965 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1966 to the inferior and breakpoints will be ignored. */
1968 signal (SIGTRAP
, SIG_DFL
);
1971 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1972 passed to the inferior, which we don't want. It would be
1973 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1974 on BSD4.3 systems using vfork, that can affect the
1975 GDB process as well as the inferior (the signal handling tables
1976 might be in memory, shared between the two). Since we establish
1977 a handler for SIGQUIT, when we call exec it will set the signal
1978 to SIG_DFL for us. */
1979 signal (SIGQUIT
, do_nothing
);
1981 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1982 signal (SIGHUP
, disconnect
);
1984 signal (SIGFPE
, float_handler
);
1986 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1987 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1991 /* Read one line from the command input stream `instream'
1992 into the local static buffer `linebuffer' (whose current length
1994 The buffer is made bigger as necessary.
1995 Returns the address of the start of the line.
1997 NULL is returned for end of file.
1999 *If* the instream == stdin & stdin is a terminal, the line read
2000 is copied into the file line saver (global var char *line,
2001 length linesize) so that it can be duplicated.
2003 This routine either uses fancy command line editing or
2004 simple input as the user has requested. */
2007 command_line_input (prrompt
, repeat
, annotation_suffix
)
2010 char *annotation_suffix
;
2012 static char *linebuffer
= 0;
2013 static unsigned linelength
= 0;
2017 char *local_prompt
= prrompt
;
2021 /* The annotation suffix must be non-NULL. */
2022 if (annotation_suffix
== NULL
)
2023 annotation_suffix
= "";
2025 if (annotation_level
> 1 && instream
== stdin
)
2027 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
2028 + strlen (annotation_suffix
) + 40);
2029 if (prrompt
== NULL
)
2030 local_prompt
[0] = '\0';
2032 strcpy (local_prompt
, prrompt
);
2033 strcat (local_prompt
, "\n\032\032");
2034 strcat (local_prompt
, annotation_suffix
);
2035 strcat (local_prompt
, "\n");
2038 if (linebuffer
== 0)
2041 linebuffer
= (char *) xmalloc (linelength
);
2046 /* Control-C quits instantly if typed while in this loop
2047 since it should not wait until the user types a newline. */
2051 signal (STOP_SIGNAL
, stop_sig
);
2056 /* Make sure that all output has been output. Some machines may let
2057 you get away with leaving out some of the gdb_flush, but not all. */
2059 gdb_flush (gdb_stdout
);
2060 gdb_flush (gdb_stderr
);
2062 if (source_file_name
!= NULL
)
2064 ++source_line_number
;
2065 sprintf (source_error
,
2066 "%s%s:%d: Error in sourced command file:\n",
2069 source_line_number
);
2070 error_pre_print
= source_error
;
2073 if (annotation_level
> 1 && instream
== stdin
)
2075 printf_unfiltered ("\n\032\032pre-");
2076 printf_unfiltered (annotation_suffix
);
2077 printf_unfiltered ("\n");
2080 /* Don't use fancy stuff if not talking to stdin. */
2081 if (readline_hook
&& instream
== NULL
)
2083 rl
= (*readline_hook
) (local_prompt
);
2085 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
2087 rl
= readline (local_prompt
);
2091 rl
= gdb_readline (local_prompt
);
2094 if (annotation_level
> 1 && instream
== stdin
)
2096 printf_unfiltered ("\n\032\032post-");
2097 printf_unfiltered (annotation_suffix
);
2098 printf_unfiltered ("\n");
2101 if (!rl
|| rl
== (char *) EOF
)
2106 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
2108 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
2109 nline
= (char *) xrealloc (linebuffer
, linelength
);
2110 p
+= nline
- linebuffer
;
2114 /* Copy line. Don't copy null at end. (Leaves line alone
2115 if this was just a newline) */
2119 free (rl
); /* Allocated in readline. */
2121 if (p
== linebuffer
|| *(p
- 1) != '\\')
2124 p
--; /* Put on top of '\'. */
2125 local_prompt
= (char *) 0;
2130 signal (STOP_SIGNAL
, SIG_DFL
);
2137 #define SERVER_COMMAND_LENGTH 7
2139 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2140 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2143 /* Note that we don't set `line'. Between this and the check in
2144 dont_repeat, this insures that repeating will still do the
2147 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2150 /* Do history expansion if that is wished. */
2151 if (history_expansion_p
&& instream
== stdin
2152 && ISATTY (instream
))
2154 char *history_value
;
2157 *p
= '\0'; /* Insert null now. */
2158 expanded
= history_expand (linebuffer
, &history_value
);
2161 /* Print the changes. */
2162 printf_unfiltered ("%s\n", history_value
);
2164 /* If there was an error, call this function again. */
2167 free (history_value
);
2168 return command_line_input (prrompt
, repeat
, annotation_suffix
);
2170 if (strlen (history_value
) > linelength
)
2172 linelength
= strlen (history_value
) + 1;
2173 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2175 strcpy (linebuffer
, history_value
);
2176 p
= linebuffer
+ strlen(linebuffer
);
2177 free (history_value
);
2181 /* If we just got an empty line, and that is supposed
2182 to repeat the previous command, return the value in the
2184 if (repeat
&& p
== linebuffer
)
2186 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++) ;
2192 /* Add line to history if appropriate. */
2193 if (instream
== stdin
2194 && ISATTY (stdin
) && *linebuffer
)
2195 add_history (linebuffer
);
2197 /* Note: lines consisting solely of comments are added to the command
2198 history. This is useful when you type a command, and then
2199 realize you don't want to execute it quite yet. You can comment
2200 out the command and then later fetch it from the value history
2201 and remove the '#'. The kill ring is probably better, but some
2202 people are in the habit of commenting things out. */
2204 *p1
= '\0'; /* Found a comment. */
2206 /* Save into global buffer if appropriate. */
2209 if (linelength
> linesize
)
2211 line
= xrealloc (line
, linelength
);
2212 linesize
= linelength
;
2214 strcpy (line
, linebuffer
);
2222 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2223 code bodies. This is typically used when we encounter an "else"
2224 clause for an "if" command. */
2227 realloc_body_list (command
, new_length
)
2228 struct command_line
*command
;
2232 struct command_line
**body_list
;
2234 n
= command
->body_count
;
2236 /* Nothing to do? */
2237 if (new_length
<= n
)
2240 body_list
= (struct command_line
**)
2241 xmalloc (sizeof (struct command_line
*) * new_length
);
2243 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2245 free (command
->body_list
);
2246 command
->body_list
= body_list
;
2247 command
->body_count
= new_length
;
2250 /* Read one line from the input stream. If the command is an "else" or
2251 "end", return such an indication to the caller. */
2253 static enum misc_command_type
2254 read_next_line (command
)
2255 struct command_line
**command
;
2257 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2260 if (control_level
>= 254)
2261 error ("Control nesting too deep!\n");
2263 /* Set a prompt based on the nesting of the control commands. */
2264 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2266 for (i
= 0; i
< control_level
; i
++)
2267 control_prompt
[i
] = ' ';
2268 control_prompt
[i
] = '>';
2269 control_prompt
[i
+1] = '\0';
2270 prompt_ptr
= (char *)&control_prompt
[0];
2275 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2277 /* Not sure what to do here. */
2281 /* Strip leading and trailing whitespace. */
2282 while (*p
== ' ' || *p
== '\t')
2285 p1
= p
+ strlen (p
);
2286 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2289 /* Blanks and comments don't really do anything, but we need to
2290 distinguish them from else, end and other commands which can be
2292 if (p1
== p
|| p
[0] == '#')
2295 /* Is this the end of a simple, while, or if control structure? */
2296 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2299 /* Is the else clause of an if control structure? */
2300 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2301 return else_command
;
2303 /* Check for while, if, break, continue, etc and build a new command
2304 line structure for them. */
2305 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2306 *command
= build_command_line (while_control
, p
+ 6);
2307 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2308 *command
= build_command_line (if_control
, p
+ 3);
2309 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2311 *command
= (struct command_line
*)
2312 xmalloc (sizeof (struct command_line
));
2313 (*command
)->next
= NULL
;
2314 (*command
)->line
= NULL
;
2315 (*command
)->control_type
= break_control
;
2316 (*command
)->body_count
= 0;
2317 (*command
)->body_list
= NULL
;
2319 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2321 *command
= (struct command_line
*)
2322 xmalloc (sizeof (struct command_line
));
2323 (*command
)->next
= NULL
;
2324 (*command
)->line
= NULL
;
2325 (*command
)->control_type
= continue_control
;
2326 (*command
)->body_count
= 0;
2327 (*command
)->body_list
= NULL
;
2331 /* A normal command. */
2332 *command
= (struct command_line
*)
2333 xmalloc (sizeof (struct command_line
));
2334 (*command
)->next
= NULL
;
2335 (*command
)->line
= savestring (p
, p1
- p
);
2336 (*command
)->control_type
= simple_control
;
2337 (*command
)->body_count
= 0;
2338 (*command
)->body_list
= NULL
;
2341 /* Nothing special. */
2345 /* Recursively read in the control structures and create a command_line
2346 structure from them.
2348 The parent_control parameter is the control structure in which the
2349 following commands are nested. */
2351 static enum command_control_type
2352 recurse_read_control_structure (current_cmd
)
2353 struct command_line
*current_cmd
;
2355 int current_body
, i
;
2356 enum misc_command_type val
;
2357 enum command_control_type ret
;
2358 struct command_line
**body_ptr
, *child_tail
, *next
;
2363 /* Sanity checks. */
2364 if (current_cmd
->control_type
== simple_control
)
2366 error ("Recursed on a simple control type\n");
2367 return invalid_control
;
2370 if (current_body
> current_cmd
->body_count
)
2372 error ("Allocated body is smaller than this command type needs\n");
2373 return invalid_control
;
2376 /* Read lines from the input stream and build control structures. */
2382 val
= read_next_line (&next
);
2384 /* Just skip blanks and comments. */
2385 if (val
== nop_command
)
2388 if (val
== end_command
)
2390 if (current_cmd
->control_type
== while_control
2391 || current_cmd
->control_type
== if_control
)
2393 /* Success reading an entire control structure. */
2394 ret
= simple_control
;
2399 ret
= invalid_control
;
2404 /* Not the end of a control structure. */
2405 if (val
== else_command
)
2407 if (current_cmd
->control_type
== if_control
2408 && current_body
== 1)
2410 realloc_body_list (current_cmd
, 2);
2417 ret
= invalid_control
;
2424 child_tail
->next
= next
;
2428 body_ptr
= current_cmd
->body_list
;
2429 for (i
= 1; i
< current_body
; i
++)
2438 /* If the latest line is another control structure, then recurse
2440 if (next
->control_type
== while_control
2441 || next
->control_type
== if_control
)
2444 ret
= recurse_read_control_structure (next
);
2447 if (ret
!= simple_control
)
2457 /* Read lines from the input stream and accumulate them in a chain of
2458 struct command_line's, which is then returned. For input from a
2459 terminal, the special command "end" is used to mark the end of the
2460 input, and is not included in the returned chain of commands. */
2462 #define END_MESSAGE "End with a line saying just \"end\"."
2464 struct command_line
*
2465 read_command_lines (prompt
, from_tty
)
2469 struct command_line
*head
, *tail
, *next
;
2470 struct cleanup
*old_chain
;
2471 enum command_control_type ret
;
2472 enum misc_command_type val
;
2474 if (readline_begin_hook
)
2476 /* Note - intentional to merge messages with no newline */
2477 (*readline_begin_hook
) ("%s %s\n", prompt
, END_MESSAGE
);
2479 else if (from_tty
&& input_from_terminal_p ())
2481 printf_unfiltered ("%s\n%s\n", prompt
, END_MESSAGE
);
2482 gdb_flush (gdb_stdout
);
2490 val
= read_next_line (&next
);
2492 /* Ignore blank lines or comments. */
2493 if (val
== nop_command
)
2496 if (val
== end_command
)
2498 ret
= simple_control
;
2502 if (val
!= ok_command
)
2504 ret
= invalid_control
;
2508 if (next
->control_type
== while_control
2509 || next
->control_type
== if_control
)
2512 ret
= recurse_read_control_structure (next
);
2515 if (ret
== invalid_control
)
2526 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
,
2536 if (ret
!= invalid_control
)
2538 discard_cleanups (old_chain
);
2541 do_cleanups (old_chain
);
2544 if (readline_end_hook
)
2546 (*readline_end_hook
) ();
2551 /* Free a chain of struct command_line's. */
2554 free_command_lines (lptr
)
2555 struct command_line
**lptr
;
2557 register struct command_line
*l
= *lptr
;
2558 register struct command_line
*next
;
2559 struct command_line
**blist
;
2564 if (l
->body_count
> 0)
2566 blist
= l
->body_list
;
2567 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2568 free_command_lines (blist
);
2577 /* Add an element to the list of info subcommands. */
2580 add_info (name
, fun
, doc
)
2582 void (*fun
) PARAMS ((char *, int));
2585 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2588 /* Add an alias to the list of info subcommands. */
2591 add_info_alias (name
, oldname
, abbrev_flag
)
2596 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2599 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2600 Therefore, its own definition is called only for "info" with no args. */
2604 info_command (arg
, from_tty
)
2608 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2609 help_list (infolist
, "info ", -1, gdb_stdout
);
2612 /* The "complete" command is used by Emacs to implement completion. */
2616 complete_command (arg
, from_tty
)
2628 argpoint
= strlen (arg
);
2630 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2632 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2634 printf_unfiltered ("%s\n", completion
);
2639 /* The "show" command with no arguments shows all the settings. */
2643 show_command (arg
, from_tty
)
2647 cmd_show_list (showlist
, from_tty
, "");
2650 /* Add an element to the list of commands. */
2653 add_com (name
, class, fun
, doc
)
2655 enum command_class
class;
2656 void (*fun
) PARAMS ((char *, int));
2659 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2662 /* Add an alias or abbreviation command to the list of commands. */
2665 add_com_alias (name
, oldname
, class, abbrev_flag
)
2668 enum command_class
class;
2671 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2678 error ("Argument required (%s).", why
);
2683 help_command (command
, from_tty
)
2685 int from_tty
; /* Ignored */
2687 help_cmd (command
, gdb_stdout
);
2691 validate_comname (comname
)
2697 error_no_arg ("name of command to define");
2702 if (!isalnum(*p
) && *p
!= '-' && *p
!= '_')
2703 error ("Junk in argument list: \"%s\"", p
);
2708 /* This is just a placeholder in the command data structures. */
2710 user_defined_command (ignore
, from_tty
)
2717 define_command (comname
, from_tty
)
2721 register struct command_line
*cmds
;
2722 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2723 char *tem
= comname
;
2725 #define HOOK_STRING "hook-"
2728 validate_comname (comname
);
2730 /* Look it up, and verify that we got an exact match. */
2731 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2732 if (c
&& !STREQ (comname
, c
->name
))
2737 if (c
->class == class_user
|| c
->class == class_alias
)
2738 tem
= "Redefine command \"%s\"? ";
2740 tem
= "Really redefine built-in command \"%s\"? ";
2741 if (!query (tem
, c
->name
))
2742 error ("Command \"%s\" not redefined.", c
->name
);
2745 /* If this new command is a hook, then mark the command which it
2746 is hooking. Note that we allow hooking `help' commands, so that
2747 we can hook the `stop' pseudo-command. */
2749 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2751 /* Look up cmd it hooks, and verify that we got an exact match. */
2752 tem
= comname
+HOOK_LEN
;
2753 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2754 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2758 warning ("Your new `%s' command does not hook any existing command.",
2760 if (!query ("Proceed? "))
2761 error ("Not confirmed.");
2765 comname
= savestring (comname
, strlen (comname
));
2767 /* If the rest of the commands will be case insensitive, this one
2768 should behave in the same manner. */
2769 for (tem
= comname
; *tem
; tem
++)
2770 if (isupper(*tem
)) *tem
= tolower(*tem
);
2773 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
2774 cmds
= read_command_lines (tmpbuf
, from_tty
);
2776 if (c
&& c
->class == class_user
)
2777 free_command_lines (&c
->user_commands
);
2779 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2780 (c
&& c
->class == class_user
)
2781 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2782 newc
->user_commands
= cmds
;
2784 /* If this new command is a hook, then mark both commands as being
2788 hookc
->hook
= newc
; /* Target gets hooked. */
2789 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2794 document_command (comname
, from_tty
)
2798 struct command_line
*doclines
;
2799 register struct cmd_list_element
*c
;
2800 char *tem
= comname
;
2803 validate_comname (comname
);
2805 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2807 if (c
->class != class_user
)
2808 error ("Command \"%s\" is built-in.", comname
);
2810 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
2811 doclines
= read_command_lines (tmpbuf
, from_tty
);
2813 if (c
->doc
) free (c
->doc
);
2816 register struct command_line
*cl1
;
2817 register int len
= 0;
2819 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2820 len
+= strlen (cl1
->line
) + 1;
2822 c
->doc
= (char *) xmalloc (len
+ 1);
2825 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2827 strcat (c
->doc
, cl1
->line
);
2829 strcat (c
->doc
, "\n");
2833 free_command_lines (&doclines
);
2836 /* Print the GDB banner. */
2838 print_gdb_version (stream
)
2841 /* From GNU coding standards, first line is meant to be easy for a
2842 program to parse, and is just canonical program name and version
2843 number, which starts after last space. */
2845 fprintf_filtered (stream
, "GNU gdb %s\n", version
);
2847 /* Second line is a copyright notice. */
2849 fprintf_filtered (stream
, "Copyright 1998 Free Software Foundation, Inc.\n");
2851 /* Following the copyright is a brief statement that the program is
2852 free software, that users are free to copy and change it on
2853 certain conditions, that it is covered by the GNU GPL, and that
2854 there is no warranty. */
2856 fprintf_filtered (stream
, "\
2857 GDB is free software, covered by the GNU General Public License, and you are\n\
2858 welcome to change it and/or distribute copies of it under certain conditions.\n\
2859 Type \"show copying\" to see the conditions.\n\
2860 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2862 /* After the required info we print the configuration information. */
2864 fprintf_filtered (stream
, "This GDB was configured as \"");
2865 if (!STREQ (host_name
, target_name
))
2867 fprintf_filtered (stream
, "--host=%s --target=%s", host_name
, target_name
);
2871 fprintf_filtered (stream
, "%s", host_name
);
2873 fprintf_filtered (stream
, "\".");
2878 show_version (args
, from_tty
)
2883 print_gdb_version (gdb_stdout
);
2884 printf_filtered ("\n");
2888 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2894 printf_unfiltered ("%s", prompt
);
2895 gdb_flush (gdb_stdout
);
2898 /* This replaces the above for the frontends: it returns a pointer
2910 /* ??rehrauer: I don't know why this fails, since it looks as though
2911 assignments to prompt are wrapped in calls to savestring...
2915 prompt
= savestring (s
, strlen (s
));
2919 /* If necessary, make the user confirm that we should quit. Return
2920 non-zero if we should quit, zero if we shouldn't. */
2925 if (inferior_pid
!= 0 && target_has_execution
)
2929 /* This is something of a hack. But there's no reliable way to
2930 see if a GUI is running. The `use_windows' variable doesn't
2933 s
= "A debugging session is active.\nDo you still want to close the debugger?";
2934 else if (attach_flag
)
2935 s
= "The program is running. Quit anyway (and detach it)? ";
2937 s
= "The program is running. Exit anyway? ";
2946 /* Quit without asking for confirmation. */
2949 quit_force (args
, from_tty
)
2955 /* An optional expression may be used to cause gdb to terminate with the
2956 value of that expression. */
2959 value_ptr val
= parse_and_eval (args
);
2961 exit_code
= (int) value_as_long (val
);
2964 if (inferior_pid
!= 0 && target_has_execution
)
2967 target_detach (args
, from_tty
);
2972 /* UDI wants this, to kill the TIP. */
2975 /* Save the history information if it is appropriate to do so. */
2976 if (write_history_p
&& history_filename
)
2977 write_history (history_filename
);
2979 do_final_cleanups(ALL_CLEANUPS
); /* Do any final cleanups before exiting */
2982 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
2983 /* The above does not need to be inside a tuiDo(), since
2984 * it is not manipulating the curses screen, but rather,
2985 * it is tearing it down.
2994 /* Handle the quit command. */
2997 quit_command (args
, from_tty
)
3001 if (! quit_confirm ())
3002 error ("Not confirmed.");
3003 quit_force (args
, from_tty
);
3006 /* Returns whether GDB is running on a terminal and whether the user
3007 desires that questions be asked of them on that terminal. */
3010 input_from_terminal_p ()
3012 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
3017 pwd_command (args
, from_tty
)
3021 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
3022 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3024 if (!STREQ (gdb_dirbuf
, current_directory
))
3025 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3026 current_directory
, gdb_dirbuf
);
3028 printf_unfiltered ("Working directory %s.\n", current_directory
);
3032 cd_command (dir
, from_tty
)
3037 /* Found something other than leading repetitions of "/..". */
3038 int found_real_path
;
3041 /* If the new directory is absolute, repeat is a no-op; if relative,
3042 repeat might be useful but is more likely to be a mistake. */
3046 error_no_arg ("new working directory");
3048 dir
= tilde_expand (dir
);
3049 make_cleanup (free
, dir
);
3051 if (chdir (dir
) < 0)
3052 perror_with_name (dir
);
3055 dir
= savestring (dir
, len
- (len
> 1 && SLASH_P(dir
[len
-1])));
3057 current_directory
= dir
;
3060 if (SLASH_P (current_directory
[0]) && current_directory
[1] == '\0')
3061 current_directory
= concat (current_directory
, dir
, NULL
);
3063 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
3067 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3069 found_real_path
= 0;
3070 for (p
= current_directory
; *p
;)
3072 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
3074 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
3075 && (p
[3] == 0 || SLASH_P (p
[3])))
3077 if (found_real_path
)
3079 /* Search backwards for the directory just before the "/.."
3080 and obliterate it and the "/..". */
3082 while (q
!= current_directory
&& ! SLASH_P (q
[-1]))
3085 if (q
== current_directory
)
3086 /* current_directory is
3087 a relative pathname ("can't happen"--leave it alone). */
3091 strcpy (q
- 1, p
+ 3);
3096 /* We are dealing with leading repetitions of "/..", for example
3097 "/../..", which is the Mach super-root. */
3102 found_real_path
= 1;
3107 forget_cached_source_info ();
3110 pwd_command ((char *) 0, 1);
3113 struct source_cleanup_lines_args
{
3116 char *old_pre_error
;
3117 char *old_error_pre_print
;
3121 source_cleanup_lines (args
)
3124 struct source_cleanup_lines_args
*p
=
3125 (struct source_cleanup_lines_args
*)args
;
3126 source_line_number
= p
->old_line
;
3127 source_file_name
= p
->old_file
;
3128 source_pre_error
= p
->old_pre_error
;
3129 error_pre_print
= p
->old_error_pre_print
;
3134 source_command (args
, from_tty
)
3139 struct cleanup
*old_cleanups
;
3141 struct source_cleanup_lines_args old_lines
;
3146 error ("source command requires pathname of file to source.");
3149 file
= tilde_expand (file
);
3150 old_cleanups
= make_cleanup (free
, file
);
3152 stream
= fopen (file
, FOPEN_RT
);
3155 perror_with_name (file
);
3159 make_cleanup ((make_cleanup_func
) fclose
, stream
);
3161 old_lines
.old_line
= source_line_number
;
3162 old_lines
.old_file
= source_file_name
;
3163 old_lines
.old_pre_error
= source_pre_error
;
3164 old_lines
.old_error_pre_print
= error_pre_print
;
3165 make_cleanup (source_cleanup_lines
, &old_lines
);
3166 source_line_number
= 0;
3167 source_file_name
= file
;
3168 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
3169 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
3170 make_cleanup (free
, source_pre_error
);
3171 /* This will get set every time we read a line. So it won't stay "" for
3173 error_pre_print
= "";
3175 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
3176 if (source_error_allocated
< needed_length
)
3178 source_error_allocated
*= 2;
3179 if (source_error_allocated
< needed_length
)
3180 source_error_allocated
= needed_length
;
3181 if (source_error
== NULL
)
3182 source_error
= xmalloc (source_error_allocated
);
3184 source_error
= xrealloc (source_error
, source_error_allocated
);
3187 read_command_file (stream
);
3189 do_cleanups (old_cleanups
);
3194 echo_command (text
, from_tty
)
3202 while ((c
= *p
++) != '\0')
3206 /* \ at end of argument is used after spaces
3207 so they won't be lost. */
3211 c
= parse_escape (&p
);
3213 printf_filtered ("%c", c
);
3216 printf_filtered ("%c", c
);
3219 /* Force this output to appear now. */
3221 gdb_flush (gdb_stdout
);
3226 dont_repeat_command (ignored
, from_tty
)
3230 *line
= 0; /* Can't call dont_repeat here because we're not
3231 necessarily reading from stdin. */
3234 /* Functions to manipulate command line editing control variables. */
3236 /* Number of commands to print in each call to show_commands. */
3237 #define Hist_print 10
3239 show_commands (args
, from_tty
)
3243 /* Index for history commands. Relative to history_base. */
3246 /* Number of the history entry which we are planning to display next.
3247 Relative to history_base. */
3250 /* The first command in the history which doesn't exist (i.e. one more
3251 than the number of the last command). Relative to history_base. */
3254 extern HIST_ENTRY
*history_get
PARAMS ((int));
3256 /* Print out some of the commands from the command history. */
3257 /* First determine the length of the history list. */
3258 hist_len
= history_size
;
3259 for (offset
= 0; offset
< history_size
; offset
++)
3261 if (!history_get (history_base
+ offset
))
3270 if (args
[0] == '+' && args
[1] == '\0')
3271 /* "info editing +" should print from the stored position. */
3274 /* "info editing <exp>" should print around command number <exp>. */
3275 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3277 /* "show commands" means print the last Hist_print commands. */
3280 num
= hist_len
- Hist_print
;
3286 /* If there are at least Hist_print commands, we want to display the last
3287 Hist_print rather than, say, the last 6. */
3288 if (hist_len
- num
< Hist_print
)
3290 num
= hist_len
- Hist_print
;
3295 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3297 printf_filtered ("%5d %s\n", history_base
+ offset
,
3298 (history_get (history_base
+ offset
))->line
);
3301 /* The next command we want to display is the next one that we haven't
3305 /* If the user repeats this command with return, it should do what
3306 "show commands +" does. This is unnecessary if arg is null,
3307 because "show commands +" is not useful after "show commands". */
3308 if (from_tty
&& args
)
3315 /* Called by do_setshow_command. */
3318 set_history_size_command (args
, from_tty
, c
)
3321 struct cmd_list_element
*c
;
3323 if (history_size
== INT_MAX
)
3324 unstifle_history ();
3325 else if (history_size
>= 0)
3326 stifle_history (history_size
);
3329 history_size
= INT_MAX
;
3330 error ("History size must be non-negative");
3336 set_history (args
, from_tty
)
3340 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3341 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3346 show_history (args
, from_tty
)
3350 cmd_show_list (showhistlist
, from_tty
, "");
3353 int info_verbose
= 0; /* Default verbose msgs off */
3355 /* Called by do_setshow_command. An elaborate joke. */
3358 set_verbose (args
, from_tty
, c
)
3361 struct cmd_list_element
*c
;
3363 char *cmdname
= "verbose";
3364 struct cmd_list_element
*showcmd
;
3366 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3370 c
->doc
= "Set verbose printing of informational messages.";
3371 showcmd
->doc
= "Show verbose printing of informational messages.";
3375 c
->doc
= "Set verbosity.";
3376 showcmd
->doc
= "Show verbosity.";
3381 float_handler (signo
)
3384 /* This message is based on ANSI C, section 4.7. Note that integer
3385 divide by zero causes this, so "float" is a misnomer. */
3386 signal (SIGFPE
, float_handler
);
3387 error ("Erroneous arithmetic operation.");
3401 enablebreaklist
= NULL
;
3406 showhistlist
= NULL
;
3407 unsethistlist
= NULL
;
3408 #if MAINTENANCE_CMDS
3409 maintenancelist
= NULL
;
3410 maintenanceinfolist
= NULL
;
3411 maintenanceprintlist
= NULL
;
3413 setprintlist
= NULL
;
3414 showprintlist
= NULL
;
3415 setchecklist
= NULL
;
3416 showchecklist
= NULL
;
3419 /* Init the history buffer. Note that we are called after the init file(s)
3420 * have been read so that the user can change the history file via his
3421 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3422 * overrides all of this.
3430 tmpenv
= getenv ("HISTSIZE");
3432 history_size
= atoi (tmpenv
);
3433 else if (!history_size
)
3436 stifle_history (history_size
);
3438 tmpenv
= getenv ("GDBHISTFILE");
3440 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3441 else if (!history_filename
) {
3442 /* We include the current directory so that if the user changes
3443 directories the file written will be the same as the one
3445 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3447 read_history (history_filename
);
3453 struct cmd_list_element
*c
;
3455 #ifdef DEFAULT_PROMPT
3456 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3458 prompt
= savestring ("(gdb) ", 6);
3461 /* Set the important stuff up for command editing. */
3462 command_editing_p
= 1;
3463 history_expansion_p
= 0;
3464 write_history_p
= 0;
3466 /* Setup important stuff for command line editing. */
3467 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3468 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3469 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3470 rl_readline_name
= "gdb";
3472 /* Define the classes of commands.
3473 They will appear in the help list in the reverse of this order. */
3475 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3476 "Maintenance commands.\n\
3477 Some gdb commands are provided just for use by gdb maintainers.\n\
3478 These commands are subject to frequent change, and may not be as\n\
3479 well documented as user commands.",
3481 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3482 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3483 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3484 The commands in this class are those defined by the user.\n\
3485 Use the \"define\" command to define a command.", &cmdlist
);
3486 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3488 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3489 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3490 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3491 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3492 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3493 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3494 counting from zero for the innermost (currently executing) frame.\n\n\
3495 At any time gdb identifies one frame as the \"selected\" frame.\n\
3496 Variable lookups are done with respect to the selected frame.\n\
3497 When the program being debugged stops, gdb selects the innermost frame.\n\
3498 The commands below can be used to select other frames by number or address.",
3500 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3502 add_com ("pwd", class_files
, pwd_command
,
3503 "Print working directory. This is used for your program as well.");
3504 c
= add_cmd ("cd", class_files
, cd_command
,
3505 "Set working directory to DIR for debugger and program being debugged.\n\
3506 The change does not take effect for the program being debugged\n\
3507 until the next time it is started.", &cmdlist
);
3508 c
->completer
= filename_completer
;
3511 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3516 add_com ("echo", class_support
, echo_command
,
3517 "Print a constant string. Give string as argument.\n\
3518 C escape sequences may be used in the argument.\n\
3519 No newline is added at the end of the argument;\n\
3520 use \"\\n\" if you want a newline to be printed.\n\
3521 Since leading and trailing whitespace are ignored in command arguments,\n\
3522 if you want to print some you must use \"\\\" before leading whitespace\n\
3523 to be printed or after trailing whitespace.");
3524 add_com ("document", class_support
, document_command
,
3525 "Document a user-defined command.\n\
3526 Give command name as argument. Give documentation on following lines.\n\
3527 End with a line of just \"end\".");
3528 add_com ("define", class_support
, define_command
,
3529 "Define a new command name. Command name is argument.\n\
3530 Definition appears on following lines, one command per line.\n\
3531 End with a line of just \"end\".\n\
3532 Use the \"document\" command to give documentation for the new command.\n\
3533 Commands defined in this way may have up to ten arguments.");
3536 c
= add_cmd ("source", class_support
, source_command
,
3537 "Read commands from a file named FILE.\n\
3538 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3539 when gdb is started.", &cmdlist
);
3541 /* Punt file name, we can't help it easily. */
3542 c
= add_cmd ("source", class_support
, source_command
,
3543 "Read commands from a file named FILE.\n\
3544 Note that the file \".gdbinit\" is read automatically in this way\n\
3545 when gdb is started.", &cmdlist
);
3547 c
->completer
= filename_completer
;
3549 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3550 add_com ("help", class_support
, help_command
, "Print list of commands.");
3551 add_com_alias ("q", "quit", class_support
, 1);
3552 add_com_alias ("h", "help", class_support
, 1);
3554 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
3555 Primarily used inside of user-defined commands that should not be repeated when\n\
3558 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3561 add_show_from_set (c
, &showlist
);
3562 c
->function
.sfunc
= set_verbose
;
3563 set_verbose (NULL
, 0, c
);
3566 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3567 "Set editing of command lines as they are typed.\n\
3568 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3569 Without an argument, command line editing is enabled. To edit, use\n\
3570 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3573 add_prefix_cmd ("history", class_support
, set_history
,
3574 "Generic command for setting command history parameters.",
3575 &sethistlist
, "set history ", 0, &setlist
);
3576 add_prefix_cmd ("history", class_support
, show_history
,
3577 "Generic command for showing command history parameters.",
3578 &showhistlist
, "show history ", 0, &showlist
);
3581 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3582 "Set history expansion on command input.\n\
3583 Without an argument, history expansion is enabled.", &sethistlist
),
3587 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3588 "Set saving of the history record on exit.\n\
3589 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
3590 Without an argument, saving is enabled.", &sethistlist
),
3593 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3594 "Set the size of the command history, \n\
3595 ie. the number of previous commands to keep a record of.", &sethistlist
);
3596 add_show_from_set (c
, &showhistlist
);
3597 c
->function
.sfunc
= set_history_size_command
;
3600 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3601 "Set the filename in which to record the command history\n\
3602 (the list of previous commands of which a record is kept).", &sethistlist
),
3606 (add_set_cmd ("confirm", class_support
, var_boolean
,
3608 "Set whether to confirm potentially dangerous operations.",
3612 add_prefix_cmd ("info", class_info
, info_command
,
3613 "Generic command for showing things about the program being debugged.",
3614 &infolist
, "info ", 0, &cmdlist
);
3615 add_com_alias ("i", "info", class_info
, 1);
3617 add_com ("complete", class_obscure
, complete_command
,
3618 "List the completions for the rest of the line as a command.");
3620 add_prefix_cmd ("show", class_info
, show_command
,
3621 "Generic command for showing things about the debugger.",
3622 &showlist
, "show ", 0, &cmdlist
);
3623 /* Another way to get at the same thing. */
3624 add_info ("set", show_command
, "Show all GDB settings.");
3626 add_cmd ("commands", no_class
, show_commands
,
3627 "Show the history of commands you typed.\n\
3628 You can supply a command number to start with, or a `+' to start after\n\
3629 the previous command number shown.",
3632 add_cmd ("version", no_class
, show_version
,
3633 "Show what version of GDB this is.", &showlist
);
3635 add_com ("while", class_support
, while_command
,
3636 "Execute nested commands WHILE the conditional expression is non zero.\n\
3637 The conditional expression must follow the word `while' and must in turn be\n\
3638 followed by a new line. The nested commands must be entered one per line,\n\
3639 and should be terminated by the word `end'.");
3641 add_com ("if", class_support
, if_command
,
3642 "Execute nested commands once IF the conditional expression is non zero.\n\
3643 The conditional expression must follow the word `if' and must in turn be\n\
3644 followed by a new line. The nested commands must be entered one per line,\n\
3645 and should be terminated by the word 'else' or `end'. If an else clause\n\
3646 is used, the same rules apply to its nested commands as to the first ones.");
3648 /* If target is open when baud changes, it doesn't take effect until the
3649 next open (I think, not sure). */
3650 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3651 var_zinteger
, (char *)&baud_rate
,
3652 "Set baud rate for remote serial I/O.\n\
3653 This value is used to set the speed of the serial port when debugging\n\
3654 using remote targets.", &setlist
),
3658 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3659 "Set debugging of remote protocol.\n\
3660 When enabled, each packet sent or received with the remote target\n\
3661 is displayed.", &setlist
),
3665 add_set_cmd ("remotetimeout", no_class
, var_integer
, (char *)&remote_timeout
,
3666 "Set timeout limit to wait for target to respond.\n\
3667 This value is used to set the time limit for gdb to wait for a response\n\
3668 from the target.", &setlist
),
3671 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
3672 (char *)&annotation_level
, "Set annotation_level.\n\
3673 0 == normal; 1 == fullname (for use when running under emacs)\n\
3674 2 == output annotated suitably for use by programs that control GDB.",
3676 c
= add_show_from_set (c
, &showlist
);