1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
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 */
42 /* readline defines this. */
45 #include <sys/types.h>
50 #include "gdb_string.h"
54 #include <sys/param.h>
58 extern void initialize_targets
PARAMS ((void));
60 extern void initialize_utils
PARAMS ((void));
62 /* Prototypes for local functions */
64 static char * line_completion_function
PARAMS ((char *, int, char *, int));
66 static char * readline_line_completion_function
PARAMS ((char *, int));
68 static void command_loop_marker
PARAMS ((int));
70 static void while_command
PARAMS ((char *, int));
72 static void if_command
PARAMS ((char *, int));
74 static struct command_line
*
75 build_command_line
PARAMS ((enum command_control_type
, char *));
77 static struct command_line
*
78 get_command_line
PARAMS ((enum command_control_type
, char *));
80 static void realloc_body_list
PARAMS ((struct command_line
*, int));
82 static enum misc_command_type read_next_line
PARAMS ((struct command_line
**));
84 static enum command_control_type
85 recurse_read_control_structure
PARAMS ((struct command_line
*));
87 static struct cleanup
* setup_user_args
PARAMS ((char *));
89 static char * locate_arg
PARAMS ((char *));
91 static char * insert_args
PARAMS ((char *));
93 static void arg_cleanup
PARAMS ((void));
95 static void init_main
PARAMS ((void));
97 static void init_cmd_lists
PARAMS ((void));
99 static void float_handler
PARAMS ((int));
101 static void init_signals
PARAMS ((void));
103 static void set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
105 #ifdef TARGET_BYTE_ORDER_SELECTABLE
107 static void set_endian
PARAMS ((char *, int));
109 static void set_endian_big
PARAMS ((char *, int));
111 static void set_endian_little
PARAMS ((char *, int));
113 static void set_endian_auto
PARAMS ((char *, int));
115 static void show_endian
PARAMS ((char *, int));
119 static void show_history
PARAMS ((char *, int));
121 static void set_history
PARAMS ((char *, int));
123 static void set_history_size_command
PARAMS ((char *, int,
124 struct cmd_list_element
*));
126 static void show_commands
PARAMS ((char *, int));
128 static void echo_command
PARAMS ((char *, int));
130 static void pwd_command
PARAMS ((char *, int));
132 static void show_version
PARAMS ((char *, int));
134 static void document_command
PARAMS ((char *, int));
136 static void define_command
PARAMS ((char *, int));
138 static void validate_comname
PARAMS ((char *));
140 static void help_command
PARAMS ((char *, int));
142 static void show_command
PARAMS ((char *, int));
144 static void info_command
PARAMS ((char *, int));
146 static void complete_command
PARAMS ((char *, int));
148 static void do_nothing
PARAMS ((int));
150 static int quit_cover
PARAMS ((char *));
152 static void disconnect
PARAMS ((int));
154 static void source_cleanup
PARAMS ((FILE *));
156 /* If this definition isn't overridden by the header files, assume
157 that isatty and fileno exist on this system. */
159 #define ISATTY(FP) (isatty (fileno (FP)))
162 /* Initialization file name for gdb. This is overridden in some configs. */
164 #ifndef GDBINIT_FILENAME
165 #define GDBINIT_FILENAME ".gdbinit"
167 char gdbinit
[] = GDBINIT_FILENAME
;
169 int inhibit_gdbinit
= 0;
171 /* If nonzero, and GDB has been configured to be able to use windows,
172 attempt to open them upon startup. */
176 /* Version number of GDB, as a string. */
178 extern char *version
;
180 /* Canonical host name as a string. */
182 extern char *host_name
;
184 /* Canonical target name as a string. */
186 extern char *target_name
;
188 extern char lang_frame_mismatch_warn
[]; /* language.c */
190 /* Flag for whether we want all the "from_tty" gubbish printed. */
192 int caution
= 1; /* Default is yes, sigh. */
194 /* Define all cmd_list_elements. */
196 /* Chain containing all defined commands. */
198 struct cmd_list_element
*cmdlist
;
200 /* Chain containing all defined info subcommands. */
202 struct cmd_list_element
*infolist
;
204 /* Chain containing all defined enable subcommands. */
206 struct cmd_list_element
*enablelist
;
208 /* Chain containing all defined disable subcommands. */
210 struct cmd_list_element
*disablelist
;
212 /* Chain containing all defined delete subcommands. */
214 struct cmd_list_element
*deletelist
;
216 /* Chain containing all defined "enable breakpoint" subcommands. */
218 struct cmd_list_element
*enablebreaklist
;
220 /* Chain containing all defined set subcommands */
222 struct cmd_list_element
*setlist
;
224 /* Chain containing all defined unset subcommands */
226 struct cmd_list_element
*unsetlist
;
228 /* Chain containing all defined show subcommands. */
230 struct cmd_list_element
*showlist
;
232 #ifdef TARGET_BYTE_ORDER_SELECTABLE
233 /* Chain containing the \"set endian\" commands. */
235 struct cmd_list_element
*endianlist
;
238 /* Chain containing all defined \"set history\". */
240 struct cmd_list_element
*sethistlist
;
242 /* Chain containing all defined \"show history\". */
244 struct cmd_list_element
*showhistlist
;
246 /* Chain containing all defined \"unset history\". */
248 struct cmd_list_element
*unsethistlist
;
250 /* Chain containing all defined maintenance subcommands. */
253 struct cmd_list_element
*maintenancelist
;
256 /* Chain containing all defined "maintenance info" subcommands. */
259 struct cmd_list_element
*maintenanceinfolist
;
262 /* Chain containing all defined "maintenance print" subcommands. */
265 struct cmd_list_element
*maintenanceprintlist
;
268 struct cmd_list_element
*setprintlist
;
270 struct cmd_list_element
*showprintlist
;
272 struct cmd_list_element
*setchecklist
;
274 struct cmd_list_element
*showchecklist
;
276 /* stdio stream that command input is being read from. Set to stdin normally.
277 Set by source_command to the file we are sourcing. Set to NULL if we are
278 executing a user-defined command or interacting via a GUI. */
282 /* Current working directory. */
284 char *current_directory
;
286 /* The directory name is actually stored here (usually). */
287 char gdb_dirbuf
[1024];
289 /* Function to call before reading a command, if nonzero.
290 The function receives two args: an input stream,
291 and a prompt string. */
293 void (*window_hook
) PARAMS ((FILE *, char *));
298 /* gdb prints this when reading a command interactively */
301 /* Buffer used for reading command lines, and the size
302 allocated for it so far. */
307 /* Nonzero if the current command is modified by "server ". This
308 affects things like recording into the command history, comamnds
309 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
310 whatever) can issue its own commands and also send along commands
311 from the user, and have the user not notice that the user interface
312 is issuing commands too. */
315 /* Baud rate specified for talking to serial target systems. Default
316 is left as -1, so targets can choose their own defaults. */
317 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
318 or (unsigned int)-1. This is a Bad User Interface. */
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 ((void));
371 /* Called instead of command_loop at top level. Can be invoked via
372 return_to_top_level. */
374 void (*command_loop_hook
) PARAMS ((void));
377 /* Called instead of fputs for all output. */
379 void (*fputs_unfiltered_hook
) PARAMS ((const char *linebuffer
, FILE *stream
));
381 /* Called when the target says something to the host, which may
382 want to appear in a different window. */
384 void (*target_output_hook
) PARAMS ((char *));
386 /* Called from print_frame_info to list the line we stopped in. */
388 void (*print_frame_info_listing_hook
) PARAMS ((struct symtab
*s
, int line
,
389 int stopline
, int noerror
));
390 /* Replaces most of query. */
392 int (*query_hook
) PARAMS (());
394 /* Called from gdb_flush to flush output. */
396 void (*flush_hook
) PARAMS ((FILE *stream
));
398 /* These three functions support getting lines of text from the user. They
399 are used in sequence. First readline_begin_hook is called with a text
400 string that might be (for example) a message for the user to type in a
401 sequence of commands to be executed at a breakpoint. If this function
402 calls back to a GUI, it might take this opportunity to pop up a text
403 interaction window with this message. Next, readline_hook is called
404 with a prompt that is emitted prior to collecting the user input.
405 It can be called multiple times. Finally, readline_end_hook is called
406 to notify the GUI that we are done with the interaction window and it
409 void (*readline_begin_hook
) PARAMS ((char *, ...));
410 char * (*readline_hook
) PARAMS ((char *));
411 void (*readline_end_hook
) PARAMS ((void));
413 /* Called as appropriate to notify the interface of the specified breakpoint
416 void (*create_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
417 void (*delete_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
418 void (*modify_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
420 /* Called during long calculations to allow GUI to repair window damage, and to
421 check for stop buttons, etc... */
423 void (*interactive_hook
) PARAMS ((void));
425 /* Called when the registers have changed, as a hint to a GUI
426 to minimize window update. */
428 void (*registers_changed_hook
) PARAMS ((void));
430 /* Called when going to wait for the target. Usually allows the GUI to run
431 while waiting for target events. */
433 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
*status
));
435 /* Used by UI as a wrapper around command execution. May do various things
436 like enabling/disabling buttons, etc... */
438 void (*call_command_hook
) PARAMS ((struct cmd_list_element
*c
, char *cmd
,
442 /* Takes control from error (). Typically used to prevent longjmps out of the
443 middle of the GUI. Usually used in conjunction with a catch routine. */
445 NORETURN
void (*error_hook
) PARAMS (()) ATTR_NORETURN
;
448 /* Where to go for return_to_top_level (RETURN_ERROR). */
449 jmp_buf error_return
;
450 /* Where to go for return_to_top_level (RETURN_QUIT). */
453 /* Return for reason REASON. This generally gets back to the command
454 loop, but can be caught via catch_errors. */
457 return_to_top_level (reason
)
458 enum return_reason reason
;
463 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
464 I can think of a reason why that is vital, though). */
465 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
467 disable_current_display ();
468 do_cleanups (ALL_CLEANUPS
);
470 if (annotation_level
> 1)
481 (NORETURN
void) longjmp
482 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
485 /* Call FUNC with arg ARGS, catching any errors. If there is no
486 error, return the value returned by FUNC. If there is an error,
487 print ERRSTRING, print the specific error message, then return
490 Must not be called with immediate_quit in effect (bad things might
491 happen, say we got a signal in the middle of a memcpy to quit_return).
492 This is an OK restriction; with very few exceptions immediate_quit can
493 be replaced by judicious use of QUIT.
495 MASK specifies what to catch; it is normally set to
496 RETURN_MASK_ALL, if for no other reason than that the code which
497 calls catch_errors might not be set up to deal with a quit which
498 isn't caught. But if the code can deal with it, it generally
499 should be RETURN_MASK_ERROR, unless for some reason it is more
500 useful to abort only the portion of the operation inside the
501 catch_errors. Note that quit should return to the command line
502 fairly quickly, even if some further processing is being done. */
505 catch_errors (func
, args
, errstring
, mask
)
506 int (*func
) PARAMS ((char *));
515 struct cleanup
*saved_cleanup_chain
;
516 char *saved_error_pre_print
;
517 char *saved_quit_pre_print
;
519 saved_cleanup_chain
= save_cleanups ();
520 saved_error_pre_print
= error_pre_print
;
521 saved_quit_pre_print
= quit_pre_print
;
523 if (mask
& RETURN_MASK_ERROR
)
525 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (jmp_buf));
526 error_pre_print
= errstring
;
528 if (mask
& RETURN_MASK_QUIT
)
530 memcpy (saved_quit
, quit_return
, sizeof (jmp_buf));
531 quit_pre_print
= errstring
;
534 if (setjmp (tmp_jmp
) == 0)
536 if (mask
& RETURN_MASK_ERROR
)
537 memcpy (error_return
, tmp_jmp
, sizeof (jmp_buf));
538 if (mask
& RETURN_MASK_QUIT
)
539 memcpy (quit_return
, tmp_jmp
, sizeof (jmp_buf));
540 val
= (*func
) (args
);
545 restore_cleanups (saved_cleanup_chain
);
547 if (mask
& RETURN_MASK_ERROR
)
549 memcpy (error_return
, saved_error
, sizeof (jmp_buf));
550 error_pre_print
= saved_error_pre_print
;
552 if (mask
& RETURN_MASK_QUIT
)
554 memcpy (quit_return
, saved_quit
, sizeof (jmp_buf));
555 quit_pre_print
= saved_quit_pre_print
;
560 /* Handler for SIGHUP. */
566 catch_errors (quit_cover
, NULL
,
567 "Could not kill the program being debugged", RETURN_MASK_ALL
);
568 signal (SIGHUP
, SIG_DFL
);
569 kill (getpid (), SIGHUP
);
572 /* Just a little helper function for disconnect(). */
578 caution
= 0; /* Throw caution to the wind -- we're exiting.
579 This prevents asking the user dumb questions. */
580 quit_command((char *)0, 0);
584 /* Line number we are currently in in a file which is being sourced. */
585 static int source_line_number
;
587 /* Name of the file we are sourcing. */
588 static char *source_file_name
;
590 /* Buffer containing the error_pre_print used by the source stuff.
592 static char *source_error
;
593 static int source_error_allocated
;
595 /* Something to glom on to the start of error_pre_print if source_file_name
597 static char *source_pre_error
;
599 /* Clean up on error during a "source" command (or execution of a
600 user-defined command). */
603 source_cleanup (stream
)
606 /* Restore the previous input stream. */
610 /* Read commands from STREAM. */
612 read_command_file (stream
)
615 struct cleanup
*cleanups
;
617 cleanups
= make_cleanup (source_cleanup
, instream
);
620 do_cleanups (cleanups
);
623 extern void init_proc ();
625 void (*pre_init_ui_hook
) PARAMS ((void));
630 if (pre_init_ui_hook
)
633 /* Run the init function of each source file */
635 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
636 current_directory
= gdb_dirbuf
;
638 init_cmd_lists (); /* This needs to be done first */
639 initialize_targets (); /* Setup target_terminal macros for utils.c */
640 initialize_utils (); /* Make errors and warnings possible */
641 initialize_all_files ();
642 init_main (); /* But that omits this file! Do it now */
647 /* We need a default language for parsing expressions, so simple things like
648 "set width 0" won't fail if no language is explicitly set in a config file
649 or implicitly set by reading an executable during startup. */
650 set_language (language_c
);
651 expected_language
= current_language
; /* don't warn about the change. */
657 /* Allocate, initialize a new command line structure for one of the
658 control commands (if/while). */
660 static struct command_line
*
661 build_command_line (type
, args
)
662 enum command_control_type type
;
665 struct command_line
*cmd
;
668 error ("if/while commands require arguments.\n");
670 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
672 cmd
->control_type
= type
;
676 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
678 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
679 cmd
->line
= savestring (args
, strlen (args
));
683 /* Build and return a new command structure for the control commands
684 such as "if" and "while". */
686 static struct command_line
*
687 get_command_line (type
, arg
)
688 enum command_control_type type
;
691 struct command_line
*cmd
;
692 struct cleanup
*old_chain
= NULL
;
694 /* Allocate and build a new command line structure. */
695 cmd
= build_command_line (type
, arg
);
697 old_chain
= make_cleanup (free_command_lines
, &cmd
);
699 /* Read in the body of this command. */
700 if (recurse_read_control_structure (cmd
) == invalid_control
)
702 warning ("error reading in control structure\n");
703 do_cleanups (old_chain
);
707 discard_cleanups (old_chain
);
711 /* Recursively print a command (including full control structures). */
713 print_command_line (cmd
, depth
)
714 struct command_line
*cmd
;
721 for (i
= 0; i
< depth
; i
++)
722 fputs_filtered (" ", gdb_stdout
);
725 /* A simple command, print it and return. */
726 if (cmd
->control_type
== simple_control
)
728 fputs_filtered (cmd
->line
, gdb_stdout
);
729 fputs_filtered ("\n", gdb_stdout
);
733 /* loop_continue to jump to the start of a while loop, print it
735 if (cmd
->control_type
== continue_control
)
737 fputs_filtered ("loop_continue\n", gdb_stdout
);
741 /* loop_break to break out of a while loop, print it and return. */
742 if (cmd
->control_type
== break_control
)
744 fputs_filtered ("loop_break\n", gdb_stdout
);
748 /* A while command. Recursively print its subcommands before returning. */
749 if (cmd
->control_type
== while_control
)
751 struct command_line
*list
;
752 fputs_filtered ("while ", gdb_stdout
);
753 fputs_filtered (cmd
->line
, gdb_stdout
);
754 fputs_filtered ("\n", gdb_stdout
);
755 list
= *cmd
->body_list
;
758 print_command_line (list
, depth
+ 1);
763 /* An if command. Recursively print both arms before returning. */
764 if (cmd
->control_type
== if_control
)
766 fputs_filtered ("if ", gdb_stdout
);
767 fputs_filtered (cmd
->line
, gdb_stdout
);
768 fputs_filtered ("\n", gdb_stdout
);
770 print_command_line (cmd
->body_list
[0], depth
+ 1);
772 /* Show the false arm if it exists. */
773 if (cmd
->body_count
== 2)
777 for (i
= 0; i
< depth
; i
++)
778 fputs_filtered (" ", gdb_stdout
);
780 fputs_filtered ("else\n", gdb_stdout
);
781 print_command_line (cmd
->body_list
[1], depth
+ 1);
785 for (i
= 0; i
< depth
; i
++)
786 fputs_filtered (" ", gdb_stdout
);
788 fputs_filtered ("end\n", gdb_stdout
);
792 /* Execute the command in CMD. */
794 enum command_control_type
795 execute_control_command (cmd
)
796 struct command_line
*cmd
;
798 struct expression
*expr
;
799 struct command_line
*current
;
800 struct cleanup
*old_chain
= 0;
803 enum command_control_type ret
;
806 switch (cmd
->control_type
)
809 /* A simple command, execute it and return. */
810 new_line
= insert_args (cmd
->line
);
812 return invalid_control
;
813 old_chain
= make_cleanup (free_current_contents
, &new_line
);
814 execute_command (new_line
, 0);
815 ret
= cmd
->control_type
;
818 case continue_control
:
820 /* Return for "continue", and "break" so we can either
821 continue the loop at the top, or break out. */
822 ret
= cmd
->control_type
;
827 /* Parse the loop control expression for the while statement. */
828 new_line
= insert_args (cmd
->line
);
830 return invalid_control
;
831 old_chain
= make_cleanup (free_current_contents
, &new_line
);
832 expr
= parse_expression (new_line
);
833 make_cleanup (free_current_contents
, &expr
);
835 ret
= simple_control
;
838 /* Keep iterating so long as the expression is true. */
843 /* Evaluate the expression. */
844 val
= evaluate_expression (expr
);
846 /* If the value is false, then break out of the loop. */
847 if (!value_true (val
))
850 /* Execute the body of the while statement. */
851 current
= *cmd
->body_list
;
854 ret
= execute_control_command (current
);
856 /* If we got an error, or a "break" command, then stop
858 if (ret
== invalid_control
|| ret
== break_control
)
864 /* If we got a "continue" command, then restart the loop
866 if (ret
== continue_control
)
869 /* Get the next statement. */
870 current
= current
->next
;
874 /* Reset RET so that we don't recurse the break all the way down. */
875 if (ret
== break_control
)
876 ret
= simple_control
;
883 new_line
= insert_args (cmd
->line
);
885 return invalid_control
;
886 old_chain
= make_cleanup (free_current_contents
, &new_line
);
887 /* Parse the conditional for the if statement. */
888 expr
= parse_expression (new_line
);
889 make_cleanup (free_current_contents
, &expr
);
892 ret
= simple_control
;
894 /* Evaluate the conditional. */
895 val
= evaluate_expression (expr
);
897 /* Choose which arm to take commands from based on the value of the
898 conditional expression. */
899 if (value_true (val
))
900 current
= *cmd
->body_list
;
901 else if (cmd
->body_count
== 2)
902 current
= *(cmd
->body_list
+ 1);
904 /* Execute commands in the given arm. */
907 ret
= execute_control_command (current
);
909 /* If we got an error, get out. */
910 if (ret
!= simple_control
)
913 /* Get the next statement in the body. */
914 current
= current
->next
;
921 warning ("Invalid control type in command structure.");
922 return invalid_control
;
926 do_cleanups (old_chain
);
931 /* "while" command support. Executes a body of statements while the
932 loop condition is nonzero. */
935 while_command (arg
, from_tty
)
939 struct command_line
*command
= NULL
;
942 command
= get_command_line (while_control
, arg
);
947 execute_control_command (command
);
948 free_command_lines (&command
);
951 /* "if" command support. Execute either the true or false arm depending
952 on the value of the if conditional. */
955 if_command (arg
, from_tty
)
959 struct command_line
*command
= NULL
;
962 command
= get_command_line (if_control
, arg
);
967 execute_control_command (command
);
968 free_command_lines (&command
);
975 struct user_args
*oargs
= user_args
;
977 fatal ("Internal error, arg_cleanup called with no user args.\n");
979 user_args
= user_args
->next
;
983 /* Bind the incomming arguments for a user defined command to
984 $arg0, $arg1 ... $argMAXUSERARGS. */
986 static struct cleanup
*
990 struct user_args
*args
;
991 struct cleanup
*old_chain
;
992 unsigned int arg_count
= 0;
994 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
995 memset (args
, 0, sizeof (struct user_args
));
997 args
->next
= user_args
;
1000 old_chain
= make_cleanup (arg_cleanup
, 0);
1009 if (arg_count
>= MAXUSERARGS
)
1011 error ("user defined function may only have %d arguments.\n",
1016 /* Strip whitespace. */
1017 while (*p
== ' ' || *p
== '\t')
1020 /* P now points to an argument. */
1022 user_args
->a
[arg_count
].arg
= p
;
1024 /* Get to the end of this argument. */
1025 while (*p
&& *p
!= ' ' && *p
!= '\t')
1028 user_args
->a
[arg_count
].len
= p
- start_arg
;
1035 /* Given character string P, return a point to the first argument ($arg),
1036 or NULL if P contains no arguments. */
1042 while ((p
= strchr (p
, '$')))
1044 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1051 /* Insert the user defined arguments stored in user_arg into the $arg
1052 arguments found in line, with the updated copy being placed into nline. */
1058 char *p
, *save_line
, *new_line
;
1061 /* First we need to know how much memory to allocate for the new line. */
1064 while ((p
= locate_arg (line
)))
1069 if (i
>= user_args
->count
)
1071 error ("Missing argument %d in user function.\n", i
);
1074 len
+= user_args
->a
[i
].len
;
1078 /* Don't forget the tail. */
1079 len
+= strlen (line
);
1081 /* Allocate space for the new line and fill it in. */
1082 new_line
= (char *)xmalloc (len
+ 1);
1083 if (new_line
== NULL
)
1086 /* Restore pointer to beginning of old line. */
1089 /* Save pointer to beginning of new line. */
1090 save_line
= new_line
;
1092 while ((p
= locate_arg (line
)))
1096 memcpy (new_line
, line
, p
- line
);
1097 new_line
+= p
- line
;
1100 len
= user_args
->a
[i
].len
;
1103 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1108 /* Don't forget the tail. */
1109 strcpy (new_line
, line
);
1111 /* Return a pointer to the beginning of the new line. */
1116 execute_user_command (c
, args
)
1117 struct cmd_list_element
*c
;
1120 register struct command_line
*cmdlines
;
1121 struct cleanup
*old_chain
;
1122 enum command_control_type ret
;
1124 old_chain
= setup_user_args (args
);
1126 cmdlines
= c
->user_commands
;
1131 /* Set the instream to 0, indicating execution of a
1132 user-defined function. */
1133 old_chain
= make_cleanup (source_cleanup
, instream
);
1134 instream
= (FILE *) 0;
1137 ret
= execute_control_command (cmdlines
);
1138 if (ret
!= simple_control
&& ret
!= break_control
)
1140 warning ("Error in control structure.\n");
1143 cmdlines
= cmdlines
->next
;
1145 do_cleanups (old_chain
);
1148 /* Execute the line P as a command.
1149 Pass FROM_TTY as second argument to the defining function. */
1152 execute_command (p
, from_tty
)
1156 register struct cmd_list_element
*c
;
1157 register enum language flang
;
1158 static int warned
= 0;
1159 extern FILE *serial_logfp
;
1163 /* This can happen when command_line_input hits end of file. */
1167 if (serial_logfp
!= NULL
)
1168 serial_log_command (p
);
1170 while (*p
== ' ' || *p
== '\t') p
++;
1175 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1176 /* Pass null arg rather than an empty one. */
1179 /* Clear off trailing whitespace, except for set and complete command. */
1180 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1182 p
= arg
+ strlen (arg
) - 1;
1183 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1188 /* If this command has been hooked, run the hook first. */
1190 execute_user_command (c
->hook
, (char *)0);
1192 if (c
->class == class_user
)
1193 execute_user_command (c
, arg
);
1194 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1195 do_setshow_command (arg
, from_tty
& caution
, c
);
1196 else if (c
->function
.cfunc
== NO_FUNCTION
)
1197 error ("That is not a command, just a help topic.");
1198 else if (call_command_hook
)
1199 call_command_hook (c
, arg
, from_tty
& caution
);
1201 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1204 /* Tell the user if the language has changed (except first time). */
1205 if (current_language
!= expected_language
)
1207 if (language_mode
== language_mode_auto
) {
1208 language_info (1); /* Print what changed. */
1213 /* Warn the user if the working language does not match the
1214 language of the current frame. Only warn the user if we are
1215 actually running the program, i.e. there is a stack. */
1216 /* FIXME: This should be cacheing the frame and only running when
1217 the frame changes. */
1219 if (target_has_stack
)
1221 flang
= get_frame_language ();
1223 && flang
!= language_unknown
1224 && flang
!= current_language
->la_language
)
1226 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1234 command_loop_marker (foo
)
1239 /* Read commands from `instream' and execute them
1240 until end of file or error reading instream. */
1245 struct cleanup
*old_chain
;
1247 int stdin_is_tty
= ISATTY (stdin
);
1248 long time_at_cmd_start
;
1250 long space_at_cmd_start
;
1252 extern int display_time
;
1253 extern int display_space
;
1255 while (instream
&& !feof (instream
))
1257 if (window_hook
&& instream
== stdin
)
1258 (*window_hook
) (instream
, prompt
);
1261 if (instream
== stdin
&& stdin_is_tty
)
1262 reinitialize_more_filter ();
1263 old_chain
= make_cleanup (command_loop_marker
, 0);
1264 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1265 instream
== stdin
, "prompt");
1269 time_at_cmd_start
= get_run_time ();
1274 extern char **environ
;
1275 char *lim
= (char *) sbrk (0);
1277 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1281 execute_command (command
, instream
== stdin
);
1282 /* Do any commands attached to breakpoint we stopped at. */
1283 bpstat_do_actions (&stop_bpstat
);
1284 do_cleanups (old_chain
);
1288 long cmd_time
= get_run_time () - time_at_cmd_start
;
1290 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1291 cmd_time
/ 1000000, cmd_time
% 1000000);
1297 extern char **environ
;
1298 char *lim
= (char *) sbrk (0);
1299 long space_now
= lim
- (char *) &environ
;
1300 long space_diff
= space_now
- space_at_cmd_start
;
1302 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1304 (space_diff
>= 0 ? '+' : '-'),
1311 /* Commands call this if they do not want to be repeated by null lines. */
1319 /* If we aren't reading from standard input, we are saving the last
1320 thing read from stdin in line and don't want to delete it. Null lines
1321 won't repeat here in any case. */
1322 if (instream
== stdin
)
1326 /* Read a line from the stream "instream" without command line editing.
1328 It prints PRROMPT once at the start.
1329 Action is compatible with "readline", e.g. space for the result is
1330 malloc'd and should be freed by the caller.
1332 A NULL return means end of file. */
1334 gdb_readline (prrompt
)
1339 int input_index
= 0;
1340 int result_size
= 80;
1344 /* Don't use a _filtered function here. It causes the assumed
1345 character position to be off, since the newline we read from
1346 the user is not accounted for. */
1347 fputs_unfiltered (prrompt
, gdb_stdout
);
1349 /* Move to a new line so the entered line doesn't have a prompt
1350 on the front of it. */
1351 fputs_unfiltered ("\n", gdb_stdout
);
1353 gdb_flush (gdb_stdout
);
1356 result
= (char *) xmalloc (result_size
);
1360 /* Read from stdin if we are executing a user defined command.
1361 This is the right thing for prompt_for_continue, at least. */
1362 c
= fgetc (instream
? instream
: stdin
);
1366 if (input_index
> 0)
1367 /* The last line does not end with a newline. Return it, and
1368 if we are called again fgetc will still return EOF and
1369 we'll return NULL then. */
1378 result
[input_index
++] = c
;
1379 while (input_index
>= result_size
)
1382 result
= (char *) xrealloc (result
, result_size
);
1386 result
[input_index
++] = '\0';
1390 /* Variables which control command line editing and history
1391 substitution. These variables are given default values at the end
1393 static int command_editing_p
;
1394 static int history_expansion_p
;
1395 static int write_history_p
;
1396 static int history_size
;
1397 static char *history_filename
;
1399 /* readline uses the word breaks for two things:
1400 (1) In figuring out where to point the TEXT parameter to the
1401 rl_completion_entry_function. Since we don't use TEXT for much,
1402 it doesn't matter a lot what the word breaks are for this purpose, but
1403 it does affect how much stuff M-? lists.
1404 (2) If one of the matches contains a word break character, readline
1405 will quote it. That's why we switch between
1406 gdb_completer_word_break_characters and
1407 gdb_completer_command_word_break_characters. I'm not sure when
1408 we need this behavior (perhaps for funky characters in C++ symbols?). */
1410 /* Variables which are necessary for fancy command line editing. */
1411 char *gdb_completer_word_break_characters
=
1412 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1414 /* When completing on command names, we remove '-' from the list of
1415 word break characters, since we use it in command names. If the
1416 readline library sees one in any of the current completion strings,
1417 it thinks that the string needs to be quoted and automatically supplies
1419 char *gdb_completer_command_word_break_characters
=
1420 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1422 /* Characters that can be used to quote completion strings. Note that we
1423 can't include '"' because the gdb C parser treats such quoted sequences
1425 char *gdb_completer_quote_characters
=
1428 /* Functions that are used as part of the fancy command line editing. */
1430 /* This can be used for functions which don't want to complete on symbols
1431 but don't want to complete on anything else either. */
1434 noop_completer (text
, prefix
)
1441 /* Complete on filenames. */
1443 filename_completer (text
, word
)
1447 /* From readline. */
1448 extern char *filename_completion_function ();
1449 int subsequent_name
;
1451 int return_val_used
;
1452 int return_val_alloced
;
1454 return_val_used
= 0;
1455 /* Small for testing. */
1456 return_val_alloced
= 1;
1457 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1459 subsequent_name
= 0;
1463 p
= filename_completion_function (text
, subsequent_name
);
1464 if (return_val_used
>= return_val_alloced
)
1466 return_val_alloced
*= 2;
1468 (char **) xrealloc (return_val
,
1469 return_val_alloced
* sizeof (char *));
1473 return_val
[return_val_used
++] = p
;
1476 /* Like emacs, don't complete on old versions. Especially useful
1477 in the "source" command. */
1478 if (p
[strlen (p
) - 1] == '~')
1484 /* Return exactly p. */
1485 return_val
[return_val_used
++] = p
;
1486 else if (word
> text
)
1488 /* Return some portion of p. */
1489 q
= xmalloc (strlen (p
) + 5);
1490 strcpy (q
, p
+ (word
- text
));
1491 return_val
[return_val_used
++] = q
;
1496 /* Return some of TEXT plus p. */
1497 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1498 strncpy (q
, word
, text
- word
);
1499 q
[text
- word
] = '\0';
1501 return_val
[return_val_used
++] = q
;
1505 subsequent_name
= 1;
1508 /* There is no way to do this just long enough to affect quote inserting
1509 without also affecting the next completion. This should be fixed in
1511 /* Insure that readline does the right thing
1512 with respect to inserting quotes. */
1513 rl_completer_word_break_characters
= "";
1518 /* Here are some useful test cases for completion. FIXME: These should
1519 be put in the test suite. They should be tested with both M-? and TAB.
1521 "show output-" "radix"
1522 "show output" "-radix"
1523 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1524 "p " ambiguous (all symbols)
1525 "info t foo" no completions
1526 "info t " no completions
1527 "info t" ambiguous ("info target", "info terminal", etc.)
1528 "info ajksdlfk" no completions
1529 "info ajksdlfk " no completions
1531 "info " ambiguous (all info commands)
1532 "p \"a" no completions (string constant)
1533 "p 'a" ambiguous (all symbols starting with a)
1534 "p b-a" ambiguous (all symbols starting with a)
1535 "p b-" ambiguous (all symbols)
1536 "file Make" "file" (word break hard to screw up here)
1537 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1540 /* Generate completions one by one for the completer. Each time we are
1541 called return another potential completion to the caller.
1542 line_completion just completes on commands or passes the buck to the
1543 command's completer function, the stuff specific to symbol completion
1544 is in make_symbol_completion_list.
1546 TEXT is the caller's idea of the "word" we are looking at.
1548 MATCHES is the number of matches that have currently been collected from
1549 calling this completion function. When zero, then we need to initialize,
1550 otherwise the initialization has already taken place and we can just
1551 return the next potential completion string.
1553 LINE_BUFFER is available to be looked at; it contains the entire text
1554 of the line. POINT is the offset in that line of the cursor. You
1555 should pretend that the line ends at POINT.
1557 Returns NULL if there are no more completions, else a pointer to a string
1558 which is a possible completion, it is the caller's responsibility to
1562 line_completion_function (text
, matches
, line_buffer
, point
)
1568 static char **list
= (char **)NULL
; /* Cache of completions */
1569 static int index
; /* Next cached completion */
1570 char *output
= NULL
;
1571 char *tmp_command
, *p
;
1572 /* Pointer within tmp_command which corresponds to text. */
1574 struct cmd_list_element
*c
, *result_list
;
1578 /* The caller is beginning to accumulate a new set of completions, so
1579 we need to find all of them now, and cache them for returning one at
1580 a time on future calls. */
1584 /* Free the storage used by LIST, but not by the strings inside.
1585 This is because rl_complete_internal () frees the strings. */
1591 /* Choose the default set of word break characters to break completions.
1592 If we later find out that we are doing completions on command strings
1593 (as opposed to strings supplied by the individual command completer
1594 functions, which can be any string) then we will switch to the
1595 special word break set for command strings, which leaves out the
1596 '-' character used in some commands. */
1598 rl_completer_word_break_characters
=
1599 gdb_completer_word_break_characters
;
1601 /* Decide whether to complete on a list of gdb commands or on symbols. */
1602 tmp_command
= (char *) alloca (point
+ 1);
1605 strncpy (tmp_command
, line_buffer
, point
);
1606 tmp_command
[point
] = '\0';
1607 /* Since text always contains some number of characters leading up
1608 to point, we can find the equivalent position in tmp_command
1609 by subtracting that many characters from the end of tmp_command. */
1610 word
= tmp_command
+ point
- strlen (text
);
1614 /* An empty line we want to consider ambiguous; that is, it
1615 could be any command. */
1616 c
= (struct cmd_list_element
*) -1;
1621 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1624 /* Move p up to the next interesting thing. */
1625 while (*p
== ' ' || *p
== '\t')
1632 /* It is an unrecognized command. So there are no
1633 possible completions. */
1636 else if (c
== (struct cmd_list_element
*) -1)
1640 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1641 doesn't advance over that thing itself. Do so now. */
1643 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1645 if (q
!= tmp_command
+ point
)
1647 /* There is something beyond the ambiguous
1648 command, so there are no possible completions. For
1649 example, "info t " or "info t foo" does not complete
1650 to anything, because "info t" can be "info target" or
1656 /* We're trying to complete on the command which was ambiguous.
1657 This we can deal with. */
1660 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1665 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1667 /* Insure that readline does the right thing with respect to
1668 inserting quotes. */
1669 rl_completer_word_break_characters
=
1670 gdb_completer_command_word_break_characters
;
1675 /* We've recognized a full command. */
1677 if (p
== tmp_command
+ point
)
1679 /* There is no non-whitespace in the line beyond the command. */
1681 if (p
[-1] == ' ' || p
[-1] == '\t')
1683 /* The command is followed by whitespace; we need to complete
1684 on whatever comes after command. */
1687 /* It is a prefix command; what comes after it is
1688 a subcommand (e.g. "info "). */
1689 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1691 /* Insure that readline does the right thing
1692 with respect to inserting quotes. */
1693 rl_completer_word_break_characters
=
1694 gdb_completer_command_word_break_characters
;
1698 list
= complete_on_enum (c
->enums
, p
, word
);
1699 rl_completer_word_break_characters
=
1700 gdb_completer_command_word_break_characters
;
1704 /* It is a normal command; what comes after it is
1705 completed by the command's completer function. */
1706 list
= (*c
->completer
) (p
, word
);
1711 /* The command is not followed by whitespace; we need to
1712 complete on the command itself. e.g. "p" which is a
1713 command itself but also can complete to "print", "ptype"
1717 /* Find the command we are completing on. */
1719 while (q
> tmp_command
)
1721 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1727 list
= complete_on_cmdlist (result_list
, q
, word
);
1729 /* Insure that readline does the right thing
1730 with respect to inserting quotes. */
1731 rl_completer_word_break_characters
=
1732 gdb_completer_command_word_break_characters
;
1737 /* There is non-whitespace beyond the command. */
1739 if (c
->prefixlist
&& !c
->allow_unknown
)
1741 /* It is an unrecognized subcommand of a prefix command,
1742 e.g. "info adsfkdj". */
1747 list
= complete_on_enum (c
->enums
, p
, word
);
1751 /* It is a normal command. */
1752 list
= (*c
->completer
) (p
, word
);
1758 /* If we found a list of potential completions during initialization then
1759 dole them out one at a time. The vector of completions is NULL
1760 terminated, so after returning the last one, return NULL (and continue
1761 to do so) each time we are called after that, until a new list is
1766 output
= list
[index
];
1774 /* Can't do this because readline hasn't yet checked the word breaks
1775 for figuring out whether to insert a quote. */
1777 /* Make sure the word break characters are set back to normal for the
1778 next time that readline tries to complete something. */
1779 rl_completer_word_break_characters
=
1780 gdb_completer_word_break_characters
;
1786 /* Line completion interface function for readline. */
1789 readline_line_completion_function (text
, matches
)
1793 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1796 /* Skip over a possibly quoted word (as defined by the quote characters
1797 and word break characters the completer uses). Returns pointer to the
1798 location after the "word". */
1804 char quote_char
= '\0';
1807 for (scan
= str
; *scan
!= '\0'; scan
++)
1809 if (quote_char
!= '\0')
1811 /* Ignore everything until the matching close quote char */
1812 if (*scan
== quote_char
)
1814 /* Found matching close quote. */
1819 else if (strchr (gdb_completer_quote_characters
, *scan
))
1821 /* Found start of a quoted string. */
1824 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1838 #if STOP_SIGNAL == SIGTSTP
1839 signal (SIGTSTP
, SIG_DFL
);
1841 kill (getpid (), SIGTSTP
);
1842 signal (SIGTSTP
, stop_sig
);
1844 signal (STOP_SIGNAL
, stop_sig
);
1846 printf_unfiltered ("%s", prompt
);
1847 gdb_flush (gdb_stdout
);
1849 /* Forget about any previous command -- null line now will do nothing. */
1852 #endif /* STOP_SIGNAL */
1854 /* Initialize signal handlers. */
1864 signal (SIGINT
, request_quit
);
1866 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1867 to the inferior and breakpoints will be ignored. */
1869 signal (SIGTRAP
, SIG_DFL
);
1872 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1873 passed to the inferior, which we don't want. It would be
1874 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1875 on BSD4.3 systems using vfork, that can affect the
1876 GDB process as well as the inferior (the signal handling tables
1877 might be in memory, shared between the two). Since we establish
1878 a handler for SIGQUIT, when we call exec it will set the signal
1879 to SIG_DFL for us. */
1880 signal (SIGQUIT
, do_nothing
);
1881 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1882 signal (SIGHUP
, disconnect
);
1883 signal (SIGFPE
, float_handler
);
1885 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1886 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1890 /* Read one line from the command input stream `instream'
1891 into the local static buffer `linebuffer' (whose current length
1893 The buffer is made bigger as necessary.
1894 Returns the address of the start of the line.
1896 NULL is returned for end of file.
1898 *If* the instream == stdin & stdin is a terminal, the line read
1899 is copied into the file line saver (global var char *line,
1900 length linesize) so that it can be duplicated.
1902 This routine either uses fancy command line editing or
1903 simple input as the user has requested. */
1906 command_line_input (prrompt
, repeat
, annotation_suffix
)
1909 char *annotation_suffix
;
1911 static char *linebuffer
= 0;
1912 static unsigned linelength
= 0;
1916 char *local_prompt
= prrompt
;
1920 /* The annotation suffix must be non-NULL. */
1921 if (annotation_suffix
== NULL
)
1922 annotation_suffix
= "";
1924 if (annotation_level
> 1 && instream
== stdin
)
1926 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
1927 + strlen (annotation_suffix
) + 40);
1928 if (prrompt
== NULL
)
1929 local_prompt
[0] = '\0';
1931 strcpy (local_prompt
, prrompt
);
1932 strcat (local_prompt
, "\n\032\032");
1933 strcat (local_prompt
, annotation_suffix
);
1934 strcat (local_prompt
, "\n");
1937 if (linebuffer
== 0)
1940 linebuffer
= (char *) xmalloc (linelength
);
1945 /* Control-C quits instantly if typed while in this loop
1946 since it should not wait until the user types a newline. */
1950 signal (STOP_SIGNAL
, stop_sig
);
1955 /* Make sure that all output has been output. Some machines may let
1956 you get away with leaving out some of the gdb_flush, but not all. */
1958 gdb_flush (gdb_stdout
);
1959 gdb_flush (gdb_stderr
);
1961 if (source_file_name
!= NULL
)
1963 ++source_line_number
;
1964 sprintf (source_error
,
1965 "%s%s:%d: Error in sourced command file:\n",
1968 source_line_number
);
1969 error_pre_print
= source_error
;
1972 if (annotation_level
> 1 && instream
== stdin
)
1974 printf_unfiltered ("\n\032\032pre-");
1975 printf_unfiltered (annotation_suffix
);
1976 printf_unfiltered ("\n");
1979 /* Don't use fancy stuff if not talking to stdin. */
1980 if (readline_hook
&& instream
== NULL
)
1982 rl
= (*readline_hook
) (local_prompt
);
1984 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
1986 rl
= readline (local_prompt
);
1990 rl
= gdb_readline (local_prompt
);
1993 if (annotation_level
> 1 && instream
== stdin
)
1995 printf_unfiltered ("\n\032\032post-");
1996 printf_unfiltered (annotation_suffix
);
1997 printf_unfiltered ("\n");
2000 if (!rl
|| rl
== (char *) EOF
)
2005 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
2007 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
2008 nline
= (char *) xrealloc (linebuffer
, linelength
);
2009 p
+= nline
- linebuffer
;
2013 /* Copy line. Don't copy null at end. (Leaves line alone
2014 if this was just a newline) */
2018 free (rl
); /* Allocated in readline. */
2020 if (p
== linebuffer
|| *(p
- 1) != '\\')
2023 p
--; /* Put on top of '\'. */
2024 local_prompt
= (char *) 0;
2029 signal (STOP_SIGNAL
, SIG_DFL
);
2036 #define SERVER_COMMAND_LENGTH 7
2038 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2039 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2042 /* Note that we don't set `line'. Between this and the check in
2043 dont_repeat, this insures that repeating will still do the
2046 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2049 /* Do history expansion if that is wished. */
2050 if (history_expansion_p
&& instream
== stdin
2051 && ISATTY (instream
))
2053 char *history_value
;
2056 *p
= '\0'; /* Insert null now. */
2057 expanded
= history_expand (linebuffer
, &history_value
);
2060 /* Print the changes. */
2061 printf_unfiltered ("%s\n", history_value
);
2063 /* If there was an error, call this function again. */
2066 free (history_value
);
2067 return command_line_input (prrompt
, repeat
, annotation_suffix
);
2069 if (strlen (history_value
) > linelength
)
2071 linelength
= strlen (history_value
) + 1;
2072 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2074 strcpy (linebuffer
, history_value
);
2075 p
= linebuffer
+ strlen(linebuffer
);
2076 free (history_value
);
2080 /* If we just got an empty line, and that is supposed
2081 to repeat the previous command, return the value in the
2083 if (repeat
&& p
== linebuffer
)
2085 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++) ;
2091 /* Add line to history if appropriate. */
2092 if (instream
== stdin
2093 && ISATTY (stdin
) && *linebuffer
)
2094 add_history (linebuffer
);
2096 /* Note: lines consisting solely of comments are added to the command
2097 history. This is useful when you type a command, and then
2098 realize you don't want to execute it quite yet. You can comment
2099 out the command and then later fetch it from the value history
2100 and remove the '#'. The kill ring is probably better, but some
2101 people are in the habit of commenting things out. */
2103 *p1
= '\0'; /* Found a comment. */
2105 /* Save into global buffer if appropriate. */
2108 if (linelength
> linesize
)
2110 line
= xrealloc (line
, linelength
);
2111 linesize
= linelength
;
2113 strcpy (line
, linebuffer
);
2121 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2122 code bodies. This is typically used when we encounter an "else"
2123 clause for an "if" command. */
2126 realloc_body_list (command
, new_length
)
2127 struct command_line
*command
;
2131 struct command_line
**body_list
;
2133 n
= command
->body_count
;
2135 /* Nothing to do? */
2136 if (new_length
<= n
)
2139 body_list
= (struct command_line
**)
2140 xmalloc (sizeof (struct command_line
*) * new_length
);
2142 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2144 free (command
->body_list
);
2145 command
->body_list
= body_list
;
2146 command
->body_count
= new_length
;
2149 /* Read one line from the input stream. If the command is an "else" or
2150 "end", return such an indication to the caller. */
2152 static enum misc_command_type
2153 read_next_line (command
)
2154 struct command_line
**command
;
2156 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2159 if (control_level
>= 254)
2160 error ("Control nesting too deep!\n");
2162 /* Set a prompt based on the nesting of the control commands. */
2163 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2165 for (i
= 0; i
< control_level
; i
++)
2166 control_prompt
[i
] = ' ';
2167 control_prompt
[i
] = '>';
2168 control_prompt
[i
+1] = '\0';
2169 prompt_ptr
= (char *)&control_prompt
[0];
2174 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2176 /* Not sure what to do here. */
2180 /* Strip leading and trailing whitespace. */
2181 while (*p
== ' ' || *p
== '\t')
2184 p1
= p
+ strlen (p
);
2185 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2188 /* Blanks and comments don't really do anything, but we need to
2189 distinguish them from else, end and other commands which can be
2191 if (p1
== p
|| p
[0] == '#')
2194 /* Is this the end of a simple, while, or if control structure? */
2195 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2198 /* Is the else clause of an if control structure? */
2199 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2200 return else_command
;
2202 /* Check for while, if, break, continue, etc and build a new command
2203 line structure for them. */
2204 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2205 *command
= build_command_line (while_control
, p
+ 6);
2206 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2207 *command
= build_command_line (if_control
, p
+ 3);
2208 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2210 *command
= (struct command_line
*)
2211 xmalloc (sizeof (struct command_line
));
2212 (*command
)->next
= NULL
;
2213 (*command
)->line
= NULL
;
2214 (*command
)->control_type
= break_control
;
2215 (*command
)->body_count
= 0;
2216 (*command
)->body_list
= NULL
;
2218 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2220 *command
= (struct command_line
*)
2221 xmalloc (sizeof (struct command_line
));
2222 (*command
)->next
= NULL
;
2223 (*command
)->line
= NULL
;
2224 (*command
)->control_type
= continue_control
;
2225 (*command
)->body_count
= 0;
2226 (*command
)->body_list
= NULL
;
2230 /* A normal command. */
2231 *command
= (struct command_line
*)
2232 xmalloc (sizeof (struct command_line
));
2233 (*command
)->next
= NULL
;
2234 (*command
)->line
= savestring (p
, p1
- p
);
2235 (*command
)->control_type
= simple_control
;
2236 (*command
)->body_count
= 0;
2237 (*command
)->body_list
= NULL
;
2240 /* Nothing special. */
2244 /* Recursively read in the control structures and create a command_line
2245 structure from them.
2247 The parent_control parameter is the control structure in which the
2248 following commands are nested. */
2250 static enum command_control_type
2251 recurse_read_control_structure (current_cmd
)
2252 struct command_line
*current_cmd
;
2254 int current_body
, i
;
2255 enum misc_command_type val
;
2256 enum command_control_type ret
;
2257 struct command_line
**body_ptr
, *child_tail
, *next
;
2262 /* Sanity checks. */
2263 if (current_cmd
->control_type
== simple_control
)
2265 error ("Recursed on a simple control type\n");
2266 return invalid_control
;
2269 if (current_body
> current_cmd
->body_count
)
2271 error ("Allocated body is smaller than this command type needs\n");
2272 return invalid_control
;
2275 /* Read lines from the input stream and build control structures. */
2281 val
= read_next_line (&next
);
2283 /* Just skip blanks and comments. */
2284 if (val
== nop_command
)
2287 if (val
== end_command
)
2289 if (current_cmd
->control_type
== while_control
2290 || current_cmd
->control_type
== if_control
)
2292 /* Success reading an entire control structure. */
2293 ret
= simple_control
;
2298 ret
= invalid_control
;
2303 /* Not the end of a control structure. */
2304 if (val
== else_command
)
2306 if (current_cmd
->control_type
== if_control
2307 && current_body
== 1)
2309 realloc_body_list (current_cmd
, 2);
2316 ret
= invalid_control
;
2323 child_tail
->next
= next
;
2327 body_ptr
= current_cmd
->body_list
;
2328 for (i
= 1; i
< current_body
; i
++)
2337 /* If the latest line is another control structure, then recurse
2339 if (next
->control_type
== while_control
2340 || next
->control_type
== if_control
)
2343 ret
= recurse_read_control_structure (next
);
2346 if (ret
!= simple_control
)
2356 /* Read lines from the input stream and accumulate them in a chain of
2357 struct command_line's, which is then returned. For input from a
2358 terminal, the special command "end" is used to mark the end of the
2359 input, and is not included in the returned chain of commands. */
2361 #define END_MESSAGE "End with a line saying just \"end\"."
2363 struct command_line
*
2364 read_command_lines (prompt
, from_tty
)
2368 struct command_line
*head
, *tail
, *next
;
2369 struct cleanup
*old_chain
;
2370 enum command_control_type ret
;
2371 enum misc_command_type val
;
2373 if (readline_begin_hook
)
2375 /* Note - intentional to merge messages with no newline */
2376 (*readline_begin_hook
) ("%s %s\n", prompt
, END_MESSAGE
);
2378 else if (from_tty
&& input_from_terminal_p ())
2380 printf_unfiltered ("%s\n%s\n", prompt
, END_MESSAGE
);
2381 gdb_flush (gdb_stdout
);
2389 val
= read_next_line (&next
);
2391 /* Ignore blank lines or comments. */
2392 if (val
== nop_command
)
2395 if (val
== end_command
)
2397 ret
= simple_control
;
2401 if (val
!= ok_command
)
2403 ret
= invalid_control
;
2407 if (next
->control_type
== while_control
2408 || next
->control_type
== if_control
)
2411 ret
= recurse_read_control_structure (next
);
2414 if (ret
== invalid_control
)
2425 old_chain
= make_cleanup (free_command_lines
, &head
);
2434 if (ret
!= invalid_control
)
2436 discard_cleanups (old_chain
);
2439 do_cleanups (old_chain
);
2442 if (readline_end_hook
)
2444 (*readline_end_hook
) ();
2449 /* Free a chain of struct command_line's. */
2452 free_command_lines (lptr
)
2453 struct command_line
**lptr
;
2455 register struct command_line
*l
= *lptr
;
2456 register struct command_line
*next
;
2457 struct command_line
**blist
;
2462 if (l
->body_count
> 0)
2464 blist
= l
->body_list
;
2465 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2466 free_command_lines (blist
);
2475 /* Add an element to the list of info subcommands. */
2478 add_info (name
, fun
, doc
)
2480 void (*fun
) PARAMS ((char *, int));
2483 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2486 /* Add an alias to the list of info subcommands. */
2489 add_info_alias (name
, oldname
, abbrev_flag
)
2494 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2497 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2498 Therefore, its own definition is called only for "info" with no args. */
2502 info_command (arg
, from_tty
)
2506 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2507 help_list (infolist
, "info ", -1, gdb_stdout
);
2510 /* The "complete" command is used by Emacs to implement completion. */
2514 complete_command (arg
, from_tty
)
2526 argpoint
= strlen (arg
);
2528 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2530 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2532 printf_unfiltered ("%s\n", completion
);
2537 /* The "show" command with no arguments shows all the settings. */
2541 show_command (arg
, from_tty
)
2545 cmd_show_list (showlist
, from_tty
, "");
2548 /* Add an element to the list of commands. */
2551 add_com (name
, class, fun
, doc
)
2553 enum command_class
class;
2554 void (*fun
) PARAMS ((char *, int));
2557 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2560 /* Add an alias or abbreviation command to the list of commands. */
2563 add_com_alias (name
, oldname
, class, abbrev_flag
)
2566 enum command_class
class;
2569 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2576 error ("Argument required (%s).", why
);
2581 help_command (command
, from_tty
)
2583 int from_tty
; /* Ignored */
2585 help_cmd (command
, gdb_stdout
);
2589 validate_comname (comname
)
2595 error_no_arg ("name of command to define");
2600 if (!isalnum(*p
) && *p
!= '-' && *p
!= '_')
2601 error ("Junk in argument list: \"%s\"", p
);
2606 /* This is just a placeholder in the command data structures. */
2608 user_defined_command (ignore
, from_tty
)
2615 define_command (comname
, from_tty
)
2619 register struct command_line
*cmds
;
2620 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2621 char *tem
= comname
;
2623 #define HOOK_STRING "hook-"
2626 validate_comname (comname
);
2628 /* Look it up, and verify that we got an exact match. */
2629 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2630 if (c
&& !STREQ (comname
, c
->name
))
2635 if (c
->class == class_user
|| c
->class == class_alias
)
2636 tem
= "Redefine command \"%s\"? ";
2638 tem
= "Really redefine built-in command \"%s\"? ";
2639 if (!query (tem
, c
->name
))
2640 error ("Command \"%s\" not redefined.", c
->name
);
2643 /* If this new command is a hook, then mark the command which it
2644 is hooking. Note that we allow hooking `help' commands, so that
2645 we can hook the `stop' pseudo-command. */
2647 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2649 /* Look up cmd it hooks, and verify that we got an exact match. */
2650 tem
= comname
+HOOK_LEN
;
2651 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2652 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2656 warning ("Your new `%s' command does not hook any existing command.",
2658 if (!query ("Proceed? "))
2659 error ("Not confirmed.");
2663 comname
= savestring (comname
, strlen (comname
));
2665 /* If the rest of the commands will be case insensitive, this one
2666 should behave in the same manner. */
2667 for (tem
= comname
; *tem
; tem
++)
2668 if (isupper(*tem
)) *tem
= tolower(*tem
);
2671 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
2672 cmds
= read_command_lines (tmpbuf
, from_tty
);
2674 if (c
&& c
->class == class_user
)
2675 free_command_lines (&c
->user_commands
);
2677 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2678 (c
&& c
->class == class_user
)
2679 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2680 newc
->user_commands
= cmds
;
2682 /* If this new command is a hook, then mark both commands as being
2686 hookc
->hook
= newc
; /* Target gets hooked. */
2687 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2692 document_command (comname
, from_tty
)
2696 struct command_line
*doclines
;
2697 register struct cmd_list_element
*c
;
2698 char *tem
= comname
;
2701 validate_comname (comname
);
2703 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2705 if (c
->class != class_user
)
2706 error ("Command \"%s\" is built-in.", comname
);
2708 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
2709 doclines
= read_command_lines (tmpbuf
, from_tty
);
2711 if (c
->doc
) free (c
->doc
);
2714 register struct command_line
*cl1
;
2715 register int len
= 0;
2717 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2718 len
+= strlen (cl1
->line
) + 1;
2720 c
->doc
= (char *) xmalloc (len
+ 1);
2723 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2725 strcat (c
->doc
, cl1
->line
);
2727 strcat (c
->doc
, "\n");
2731 free_command_lines (&doclines
);
2735 print_gnu_advertisement ()
2737 printf_unfiltered ("\
2738 GDB is free software and you are welcome to distribute copies of it\n\
2739 under certain conditions; type \"show copying\" to see the conditions.\n\
2740 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2745 print_gdb_version (stream
)
2748 fprintf_filtered (stream
, "\
2749 GDB %s (%s", version
, host_name
);
2751 if (!STREQ (host_name
, target_name
))
2752 fprintf_filtered (stream
, " --target %s", target_name
);
2754 fprintf_filtered (stream
, "), ");
2756 fprintf_filtered (stream
, "Copyright 1996 Free Software Foundation, Inc.");
2761 show_version (args
, from_tty
)
2766 print_gnu_advertisement ();
2767 print_gdb_version (gdb_stdout
);
2768 printf_filtered ("\n");
2772 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2778 printf_unfiltered ("%s", prompt
);
2779 gdb_flush (gdb_stdout
);
2783 quit_command (args
, from_tty
)
2789 /* An optional expression may be used to cause gdb to terminate with the
2790 value of that expression. */
2793 value_ptr val
= parse_and_eval (args
);
2795 exit_code
= (int) value_as_long (val
);
2798 if (inferior_pid
!= 0 && target_has_execution
)
2802 if (query ("The program is running. Quit anyway (and detach it)? "))
2803 target_detach (args
, from_tty
);
2805 error ("Not confirmed.");
2809 if (query ("The program is running. Quit anyway (and kill it)? "))
2812 error ("Not confirmed.");
2815 /* UDI wants this, to kill the TIP. */
2818 /* Save the history information if it is appropriate to do so. */
2819 if (write_history_p
&& history_filename
)
2820 write_history (history_filename
);
2825 /* Returns whether GDB is running on a terminal and whether the user
2826 desires that questions be asked of them on that terminal. */
2829 input_from_terminal_p ()
2831 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
2836 pwd_command (args
, from_tty
)
2840 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
2841 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
2843 if (!STREQ (gdb_dirbuf
, current_directory
))
2844 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2845 current_directory
, gdb_dirbuf
);
2847 printf_unfiltered ("Working directory %s.\n", current_directory
);
2851 cd_command (dir
, from_tty
)
2856 /* Found something other than leading repetitions of "/..". */
2857 int found_real_path
;
2860 /* If the new directory is absolute, repeat is a no-op; if relative,
2861 repeat might be useful but is more likely to be a mistake. */
2865 error_no_arg ("new working directory");
2867 dir
= tilde_expand (dir
);
2868 make_cleanup (free
, dir
);
2870 if (chdir (dir
) < 0)
2871 perror_with_name (dir
);
2874 dir
= savestring (dir
, len
- (len
> 1 && SLASH_P(dir
[len
-1])));
2876 current_directory
= dir
;
2879 if (SLASH_P (current_directory
[0]) && current_directory
[1] == '\0')
2880 current_directory
= concat (current_directory
, dir
, NULL
);
2882 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
2886 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2888 found_real_path
= 0;
2889 for (p
= current_directory
; *p
;)
2891 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
2893 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
2894 && (p
[3] == 0 || SLASH_P (p
[3])))
2896 if (found_real_path
)
2898 /* Search backwards for the directory just before the "/.."
2899 and obliterate it and the "/..". */
2901 while (q
!= current_directory
&& ! SLASH_P (q
[-1]))
2904 if (q
== current_directory
)
2905 /* current_directory is
2906 a relative pathname ("can't happen"--leave it alone). */
2910 strcpy (q
- 1, p
+ 3);
2915 /* We are dealing with leading repetitions of "/..", for example
2916 "/../..", which is the Mach super-root. */
2921 found_real_path
= 1;
2926 forget_cached_source_info ();
2929 pwd_command ((char *) 0, 1);
2932 struct source_cleanup_lines_args
{
2935 char *old_pre_error
;
2936 char *old_error_pre_print
;
2940 source_cleanup_lines (args
)
2943 struct source_cleanup_lines_args
*p
=
2944 (struct source_cleanup_lines_args
*)args
;
2945 source_line_number
= p
->old_line
;
2946 source_file_name
= p
->old_file
;
2947 source_pre_error
= p
->old_pre_error
;
2948 error_pre_print
= p
->old_error_pre_print
;
2953 source_command (args
, from_tty
)
2958 struct cleanup
*old_cleanups
;
2960 struct source_cleanup_lines_args old_lines
;
2965 error ("source command requires pathname of file to source.");
2968 file
= tilde_expand (file
);
2969 old_cleanups
= make_cleanup (free
, file
);
2971 stream
= fopen (file
, FOPEN_RT
);
2973 perror_with_name (file
);
2975 make_cleanup (fclose
, stream
);
2977 old_lines
.old_line
= source_line_number
;
2978 old_lines
.old_file
= source_file_name
;
2979 old_lines
.old_pre_error
= source_pre_error
;
2980 old_lines
.old_error_pre_print
= error_pre_print
;
2981 make_cleanup (source_cleanup_lines
, &old_lines
);
2982 source_line_number
= 0;
2983 source_file_name
= file
;
2984 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
2985 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
2986 make_cleanup (free
, source_pre_error
);
2987 /* This will get set every time we read a line. So it won't stay "" for
2989 error_pre_print
= "";
2991 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
2992 if (source_error_allocated
< needed_length
)
2994 source_error_allocated
*= 2;
2995 if (source_error_allocated
< needed_length
)
2996 source_error_allocated
= needed_length
;
2997 if (source_error
== NULL
)
2998 source_error
= xmalloc (source_error_allocated
);
3000 source_error
= xrealloc (source_error
, source_error_allocated
);
3003 read_command_file (stream
);
3005 do_cleanups (old_cleanups
);
3010 echo_command (text
, from_tty
)
3018 while ((c
= *p
++) != '\0')
3022 /* \ at end of argument is used after spaces
3023 so they won't be lost. */
3027 c
= parse_escape (&p
);
3029 printf_filtered ("%c", c
);
3032 printf_filtered ("%c", c
);
3035 /* Force this output to appear now. */
3037 gdb_flush (gdb_stdout
);
3042 dont_repeat_command (ignored
, from_tty
)
3046 *line
= 0; /* Can't call dont_repeat here because we're not
3047 necessarily reading from stdin. */
3050 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3052 /* Functions to manipulate the endianness of the target. */
3054 #ifndef TARGET_BYTE_ORDER_DEFAULT
3055 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
3058 int target_byte_order
= TARGET_BYTE_ORDER_DEFAULT
;
3060 static int target_byte_order_auto
= 1;
3062 /* Called if the user enters ``set endian'' without an argument. */
3064 set_endian (args
, from_tty
)
3068 printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
3069 show_endian (args
, from_tty
);
3072 /* Called by ``set endian big''. */
3074 set_endian_big (args
, from_tty
)
3078 target_byte_order
= BIG_ENDIAN
;
3079 target_byte_order_auto
= 0;
3082 /* Called by ``set endian little''. */
3084 set_endian_little (args
, from_tty
)
3088 target_byte_order
= LITTLE_ENDIAN
;
3089 target_byte_order_auto
= 0;
3092 /* Called by ``set endian auto''. */
3094 set_endian_auto (args
, from_tty
)
3098 target_byte_order_auto
= 1;
3101 /* Called by ``show endian''. */
3103 show_endian (args
, from_tty
)
3108 (target_byte_order_auto
3109 ? "The target endianness is set automatically (currently %s endian)\n"
3110 : "The target is assumed to be %s endian\n");
3111 printf_unfiltered ((char *) msg
, TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3114 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3116 /* Set the endianness from a BFD. */
3118 set_endian_from_file (abfd
)
3121 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3124 if (bfd_big_endian (abfd
))
3127 want
= LITTLE_ENDIAN
;
3128 if (target_byte_order_auto
)
3129 target_byte_order
= want
;
3130 else if (target_byte_order
!= want
)
3131 warning ("%s endian file does not match %s endian target.",
3132 want
== BIG_ENDIAN
? "big" : "little",
3133 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3135 #else /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3137 if (bfd_big_endian (abfd
)
3138 ? TARGET_BYTE_ORDER
!= BIG_ENDIAN
3139 : TARGET_BYTE_ORDER
== BIG_ENDIAN
)
3140 warning ("%s endian file does not match %s endian target.",
3141 bfd_big_endian (abfd
) ? "big" : "little",
3142 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3144 #endif /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3147 /* Functions to manipulate command line editing control variables. */
3149 /* Number of commands to print in each call to show_commands. */
3150 #define Hist_print 10
3152 show_commands (args
, from_tty
)
3156 /* Index for history commands. Relative to history_base. */
3159 /* Number of the history entry which we are planning to display next.
3160 Relative to history_base. */
3163 /* The first command in the history which doesn't exist (i.e. one more
3164 than the number of the last command). Relative to history_base. */
3167 extern HIST_ENTRY
*history_get
PARAMS ((int));
3169 /* Print out some of the commands from the command history. */
3170 /* First determine the length of the history list. */
3171 hist_len
= history_size
;
3172 for (offset
= 0; offset
< history_size
; offset
++)
3174 if (!history_get (history_base
+ offset
))
3183 if (args
[0] == '+' && args
[1] == '\0')
3184 /* "info editing +" should print from the stored position. */
3187 /* "info editing <exp>" should print around command number <exp>. */
3188 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3190 /* "show commands" means print the last Hist_print commands. */
3193 num
= hist_len
- Hist_print
;
3199 /* If there are at least Hist_print commands, we want to display the last
3200 Hist_print rather than, say, the last 6. */
3201 if (hist_len
- num
< Hist_print
)
3203 num
= hist_len
- Hist_print
;
3208 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3210 printf_filtered ("%5d %s\n", history_base
+ offset
,
3211 (history_get (history_base
+ offset
))->line
);
3214 /* The next command we want to display is the next one that we haven't
3218 /* If the user repeats this command with return, it should do what
3219 "show commands +" does. This is unnecessary if arg is null,
3220 because "show commands +" is not useful after "show commands". */
3221 if (from_tty
&& args
)
3228 /* Called by do_setshow_command. */
3231 set_history_size_command (args
, from_tty
, c
)
3234 struct cmd_list_element
*c
;
3236 if (history_size
== INT_MAX
)
3237 unstifle_history ();
3238 else if (history_size
>= 0)
3239 stifle_history (history_size
);
3242 history_size
= INT_MAX
;
3243 error ("History size must be non-negative");
3249 set_history (args
, from_tty
)
3253 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3254 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3259 show_history (args
, from_tty
)
3263 cmd_show_list (showhistlist
, from_tty
, "");
3266 int info_verbose
= 0; /* Default verbose msgs off */
3268 /* Called by do_setshow_command. An elaborate joke. */
3271 set_verbose (args
, from_tty
, c
)
3274 struct cmd_list_element
*c
;
3276 char *cmdname
= "verbose";
3277 struct cmd_list_element
*showcmd
;
3279 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3283 c
->doc
= "Set verbose printing of informational messages.";
3284 showcmd
->doc
= "Show verbose printing of informational messages.";
3288 c
->doc
= "Set verbosity.";
3289 showcmd
->doc
= "Show verbosity.";
3294 float_handler (signo
)
3297 /* This message is based on ANSI C, section 4.7. Note that integer
3298 divide by zero causes this, so "float" is a misnomer. */
3299 signal (SIGFPE
, float_handler
);
3300 error ("Erroneous arithmetic operation.");
3312 enablebreaklist
= NULL
;
3316 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3320 showhistlist
= NULL
;
3321 unsethistlist
= NULL
;
3322 #if MAINTENANCE_CMDS
3323 maintenancelist
= NULL
;
3324 maintenanceinfolist
= NULL
;
3325 maintenanceprintlist
= NULL
;
3327 setprintlist
= NULL
;
3328 showprintlist
= NULL
;
3329 setchecklist
= NULL
;
3330 showchecklist
= NULL
;
3333 /* Init the history buffer. Note that we are called after the init file(s)
3334 * have been read so that the user can change the history file via his
3335 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3336 * overrides all of this.
3344 tmpenv
= getenv ("HISTSIZE");
3346 history_size
= atoi (tmpenv
);
3347 else if (!history_size
)
3350 stifle_history (history_size
);
3352 tmpenv
= getenv ("GDBHISTFILE");
3354 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3355 else if (!history_filename
) {
3356 /* We include the current directory so that if the user changes
3357 directories the file written will be the same as the one
3359 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3361 read_history (history_filename
);
3367 struct cmd_list_element
*c
;
3369 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3371 add_prefix_cmd ("endian", class_support
, set_endian
,
3372 "Set endianness of target.",
3373 &endianlist
, "set endian ", 0, &setlist
);
3374 add_cmd ("big", class_support
, set_endian_big
,
3375 "Set target as being big endian.", &endianlist
);
3376 add_cmd ("little", class_support
, set_endian_little
,
3377 "Set target as being little endian.", &endianlist
);
3378 add_cmd ("auto", class_support
, set_endian_auto
,
3379 "Select target endianness automatically.", &endianlist
);
3380 add_cmd ("endian", class_support
, show_endian
,
3381 "Show endianness of target.", &showlist
);
3383 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3385 #ifdef DEFAULT_PROMPT
3386 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3388 prompt
= savestring ("(gdb) ", 6);
3391 /* Set the important stuff up for command editing. */
3392 command_editing_p
= 1;
3393 history_expansion_p
= 0;
3394 write_history_p
= 0;
3396 /* Setup important stuff for command line editing. */
3397 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3398 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3399 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3400 rl_readline_name
= "gdb";
3402 /* Define the classes of commands.
3403 They will appear in the help list in the reverse of this order. */
3405 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3406 "Maintenance commands.\n\
3407 Some gdb commands are provided just for use by gdb maintainers.\n\
3408 These commands are subject to frequent change, and may not be as\n\
3409 well documented as user commands.",
3411 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3412 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3413 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3414 The commands in this class are those defined by the user.\n\
3415 Use the \"define\" command to define a command.", &cmdlist
);
3416 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3417 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3418 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3419 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3420 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3421 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3422 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3423 counting from zero for the innermost (currently executing) frame.\n\n\
3424 At any time gdb identifies one frame as the \"selected\" frame.\n\
3425 Variable lookups are done with respect to the selected frame.\n\
3426 When the program being debugged stops, gdb selects the innermost frame.\n\
3427 The commands below can be used to select other frames by number or address.",
3429 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3431 add_com ("pwd", class_files
, pwd_command
,
3432 "Print working directory. This is used for your program as well.");
3433 c
= add_cmd ("cd", class_files
, cd_command
,
3434 "Set working directory to DIR for debugger and program being debugged.\n\
3435 The change does not take effect for the program being debugged\n\
3436 until the next time it is started.", &cmdlist
);
3437 c
->completer
= filename_completer
;
3440 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3445 add_com ("echo", class_support
, echo_command
,
3446 "Print a constant string. Give string as argument.\n\
3447 C escape sequences may be used in the argument.\n\
3448 No newline is added at the end of the argument;\n\
3449 use \"\\n\" if you want a newline to be printed.\n\
3450 Since leading and trailing whitespace are ignored in command arguments,\n\
3451 if you want to print some you must use \"\\\" before leading whitespace\n\
3452 to be printed or after trailing whitespace.");
3453 add_com ("document", class_support
, document_command
,
3454 "Document a user-defined command.\n\
3455 Give command name as argument. Give documentation on following lines.\n\
3456 End with a line of just \"end\".");
3457 add_com ("define", class_support
, define_command
,
3458 "Define a new command name. Command name is argument.\n\
3459 Definition appears on following lines, one command per line.\n\
3460 End with a line of just \"end\".\n\
3461 Use the \"document\" command to give documentation for the new command.\n\
3462 Commands defined in this way may have up to ten arguments.");
3465 c
= add_cmd ("source", class_support
, source_command
,
3466 "Read commands from a file named FILE.\n\
3467 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3468 when gdb is started.", &cmdlist
);
3470 /* Punt file name, we can't help it easily. */
3471 c
= add_cmd ("source", class_support
, source_command
,
3472 "Read commands from a file named FILE.\n\
3473 Note that the file \".gdbinit\" is read automatically in this way\n\
3474 when gdb is started.", &cmdlist
);
3476 c
->completer
= filename_completer
;
3478 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3479 add_com ("help", class_support
, help_command
, "Print list of commands.");
3480 add_com_alias ("q", "quit", class_support
, 1);
3481 add_com_alias ("h", "help", class_support
, 1);
3483 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
3484 Primarily used inside of user-defined commands that should not be repeated when\n\
3487 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3490 add_show_from_set (c
, &showlist
);
3491 c
->function
.sfunc
= set_verbose
;
3492 set_verbose (NULL
, 0, c
);
3495 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3496 "Set editing of command lines as they are typed.\n\
3497 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
3498 Without an argument, command line editing is enabled. To edit, use\n\
3499 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3502 add_prefix_cmd ("history", class_support
, set_history
,
3503 "Generic command for setting command history parameters.",
3504 &sethistlist
, "set history ", 0, &setlist
);
3505 add_prefix_cmd ("history", class_support
, show_history
,
3506 "Generic command for showing command history parameters.",
3507 &showhistlist
, "show history ", 0, &showlist
);
3510 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3511 "Set history expansion on command input.\n\
3512 Without an argument, history expansion is enabled.", &sethistlist
),
3516 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3517 "Set saving of the history record on exit.\n\
3518 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
3519 Without an argument, saving is enabled.", &sethistlist
),
3522 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3523 "Set the size of the command history, \n\
3524 ie. the number of previous commands to keep a record of.", &sethistlist
);
3525 add_show_from_set (c
, &showhistlist
);
3526 c
->function
.sfunc
= set_history_size_command
;
3529 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3530 "Set the filename in which to record the command history\n\
3531 (the list of previous commands of which a record is kept).", &sethistlist
),
3535 (add_set_cmd ("confirm", class_support
, var_boolean
,
3537 "Set whether to confirm potentially dangerous operations.",
3541 add_prefix_cmd ("info", class_info
, info_command
,
3542 "Generic command for showing things about the program being debugged.",
3543 &infolist
, "info ", 0, &cmdlist
);
3544 add_com_alias ("i", "info", class_info
, 1);
3546 add_com ("complete", class_obscure
, complete_command
,
3547 "List the completions for the rest of the line as a command.");
3549 add_prefix_cmd ("show", class_info
, show_command
,
3550 "Generic command for showing things about the debugger.",
3551 &showlist
, "show ", 0, &cmdlist
);
3552 /* Another way to get at the same thing. */
3553 add_info ("set", show_command
, "Show all GDB settings.");
3555 add_cmd ("commands", no_class
, show_commands
,
3556 "Show the history of commands you typed.\n\
3557 You can supply a command number to start with, or a `+' to start after\n\
3558 the previous command number shown.",
3561 add_cmd ("version", no_class
, show_version
,
3562 "Show what version of GDB this is.", &showlist
);
3564 add_com ("while", class_support
, while_command
,
3565 "Execute nested commands WHILE the conditional expression is non zero.\n\
3566 The conditional expression must follow the word `while' and must in turn be\n\
3567 followed by a new line. The nested commands must be entered one per line,\n\
3568 and should be terminated by the word `end'.");
3570 add_com ("if", class_support
, if_command
,
3571 "Execute nested commands once IF the conditional expression is non zero.\n\
3572 The conditional expression must follow the word `if' and must in turn be\n\
3573 followed by a new line. The nested commands must be entered one per line,\n\
3574 and should be terminated by the word 'else' or `end'. If an else clause\n\
3575 is used, the same rules apply to its nested commands as to the first ones.");
3577 /* If target is open when baud changes, it doesn't take effect until the
3578 next open (I think, not sure). */
3579 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3580 var_zinteger
, (char *)&baud_rate
,
3581 "Set baud rate for remote serial I/O.\n\
3582 This value is used to set the speed of the serial port when debugging\n\
3583 using remote targets.", &setlist
),
3587 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3588 "Set debugging of remote protocol.\n\
3589 When enabled, each packet sent or received with the remote target\n\
3590 is displayed.", &setlist
),