1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995
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., 675 Mass Ave, Cambridge, MA 02139, 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>
47 /* What is this for? X_OK? */
55 #include <sys/param.h>
59 extern void initialize_targets
PARAMS ((void));
61 extern void initialize_utils
PARAMS ((void));
63 /* Prototypes for local functions */
65 static char * line_completion_function
PARAMS ((char *, int, char *, int));
67 static char * readline_line_completion_function
PARAMS ((char *, int));
69 static void command_loop_marker
PARAMS ((int));
71 static void while_command
PARAMS ((char *, int));
73 static void if_command
PARAMS ((char *, int));
75 static struct command_line
*
76 build_command_line
PARAMS ((enum command_control_type
, char *));
78 static struct command_line
*
79 get_command_line
PARAMS ((enum command_control_type
, char *));
81 static void realloc_body_list
PARAMS ((struct command_line
*, int));
83 static enum misc_command_type read_next_line
PARAMS ((struct command_line
**));
85 static enum command_control_type
86 recurse_read_control_structure
PARAMS ((struct command_line
*));
88 static struct cleanup
* setup_user_args
PARAMS ((char *));
90 static char * locate_arg
PARAMS ((char *));
92 static char * insert_args
PARAMS ((char *));
94 static void arg_cleanup
PARAMS ((void));
96 static void init_main
PARAMS ((void));
98 static void init_cmd_lists
PARAMS ((void));
100 static void float_handler
PARAMS ((int));
102 static void init_signals
PARAMS ((void));
104 static void set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
106 #ifdef TARGET_BYTE_ORDER_SELECTABLE
108 static void set_endian
PARAMS ((char *, int));
110 static void set_endian_big
PARAMS ((char *, int));
112 static void set_endian_little
PARAMS ((char *, int));
114 static void set_endian_auto
PARAMS ((char *, int));
116 static void show_endian
PARAMS ((char *, int));
120 static void show_history
PARAMS ((char *, int));
122 static void set_history
PARAMS ((char *, int));
124 static void set_history_size_command
PARAMS ((char *, int,
125 struct cmd_list_element
*));
127 static void show_commands
PARAMS ((char *, int));
129 static void echo_command
PARAMS ((char *, int));
131 static void pwd_command
PARAMS ((char *, int));
133 static void show_version
PARAMS ((char *, int));
135 static void document_command
PARAMS ((char *, int));
137 static void define_command
PARAMS ((char *, int));
139 static void validate_comname
PARAMS ((char *));
141 static void help_command
PARAMS ((char *, int));
143 static void show_command
PARAMS ((char *, int));
145 static void info_command
PARAMS ((char *, int));
147 static void complete_command
PARAMS ((char *, int));
149 static void do_nothing
PARAMS ((int));
151 static int quit_cover
PARAMS ((char *));
153 static void disconnect
PARAMS ((int));
155 static void source_cleanup
PARAMS ((FILE *));
157 /* If this definition isn't overridden by the header files, assume
158 that isatty and fileno exist on this system. */
160 #define ISATTY(FP) (isatty (fileno (FP)))
163 /* Initialization file name for gdb. This is overridden in some configs. */
165 #ifndef GDBINIT_FILENAME
166 #define GDBINIT_FILENAME ".gdbinit"
168 char gdbinit
[] = GDBINIT_FILENAME
;
170 int inhibit_gdbinit
= 0;
172 /* If nonzero, and GDB has been configured to be able to use windows,
173 attempt to open them upon startup. */
177 /* Version number of GDB, as a string. */
179 extern char *version
;
181 /* Canonical host name as a string. */
183 extern char *host_name
;
185 /* Canonical target name as a string. */
187 extern char *target_name
;
189 extern char lang_frame_mismatch_warn
[]; /* language.c */
191 /* Flag for whether we want all the "from_tty" gubbish printed. */
193 int caution
= 1; /* Default is yes, sigh. */
195 /* Define all cmd_list_elements. */
197 /* Chain containing all defined commands. */
199 struct cmd_list_element
*cmdlist
;
201 /* Chain containing all defined info subcommands. */
203 struct cmd_list_element
*infolist
;
205 /* Chain containing all defined enable subcommands. */
207 struct cmd_list_element
*enablelist
;
209 /* Chain containing all defined disable subcommands. */
211 struct cmd_list_element
*disablelist
;
213 /* Chain containing all defined delete subcommands. */
215 struct cmd_list_element
*deletelist
;
217 /* Chain containing all defined "enable breakpoint" subcommands. */
219 struct cmd_list_element
*enablebreaklist
;
221 /* Chain containing all defined set subcommands */
223 struct cmd_list_element
*setlist
;
225 /* Chain containing all defined unset subcommands */
227 struct cmd_list_element
*unsetlist
;
229 /* Chain containing all defined show subcommands. */
231 struct cmd_list_element
*showlist
;
233 #ifdef TARGET_BYTE_ORDER_SELECTABLE
234 /* Chain containing the \"set endian\" commands. */
236 struct cmd_list_element
*endianlist
;
239 /* Chain containing all defined \"set history\". */
241 struct cmd_list_element
*sethistlist
;
243 /* Chain containing all defined \"show history\". */
245 struct cmd_list_element
*showhistlist
;
247 /* Chain containing all defined \"unset history\". */
249 struct cmd_list_element
*unsethistlist
;
251 /* Chain containing all defined maintenance subcommands. */
254 struct cmd_list_element
*maintenancelist
;
257 /* Chain containing all defined "maintenance info" subcommands. */
260 struct cmd_list_element
*maintenanceinfolist
;
263 /* Chain containing all defined "maintenance print" subcommands. */
266 struct cmd_list_element
*maintenanceprintlist
;
269 struct cmd_list_element
*setprintlist
;
271 struct cmd_list_element
*showprintlist
;
273 struct cmd_list_element
*setchecklist
;
275 struct cmd_list_element
*showchecklist
;
277 /* stdio stream that command input is being read from. Set to stdin normally.
278 Set by source_command to the file we are sourcing. Set to NULL if we are
279 executing a user-defined command. */
283 /* Current working directory. */
285 char *current_directory
;
287 /* The directory name is actually stored here (usually). */
288 char gdb_dirbuf
[1024];
290 /* Function to call before reading a command, if nonzero.
291 The function receives two args: an input stream,
292 and a prompt string. */
294 void (*window_hook
) PARAMS ((FILE *, char *));
299 /* gdb prints this when reading a command interactively */
302 /* Buffer used for reading command lines, and the size
303 allocated for it so far. */
308 /* Nonzero if the current command is modified by "server ". This
309 affects things like recording into the command history, comamnds
310 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
311 whatever) can issue its own commands and also send along commands
312 from the user, and have the user not notice that the user interface
313 is issuing commands too. */
316 /* Baud rate specified for talking to serial target systems. Default
317 is left as -1, so targets can choose their own defaults. */
318 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
319 or (unsigned int)-1. This is a Bad User Interface. */
323 /* Non-zero tells remote* modules to output debugging info. */
325 int remote_debug
= 0;
327 /* Level of control structure. */
328 static int control_level
;
330 /* Structure for arguments to user defined functions. */
331 #define MAXUSERARGS 10
334 struct user_args
*next
;
343 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
347 #define STOP_SIGNAL SIGTSTP
348 static void stop_sig
PARAMS ((int));
352 /* Some System V have job control but not sigsetmask(). */
353 #if !defined (HAVE_SIGSETMASK)
355 #define HAVE_SIGSETMASK 1
357 #define HAVE_SIGSETMASK 0
361 #if 0 == (HAVE_SIGSETMASK)
362 #define sigsetmask(n)
365 /* Hooks for alternate command interfaces. */
367 /* Called after most modules have been initialized, but before taking users
370 void (*init_ui_hook
) PARAMS ((void));
372 /* Called instead of command_loop at top level. Can be invoked via
373 return_to_top_level. */
375 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 from print_frame_info to list the line we stopped in. */
383 void (*print_frame_info_listing_hook
) PARAMS ((struct symtab
*s
, int line
,
384 int stopline
, int noerror
));
385 /* Replaces most of query. */
387 int (*query_hook
) PARAMS (());
389 /* Called from gdb_flush to flush output. */
391 void (*flush_hook
) PARAMS ((FILE *stream
));
393 /* Called as appropriate to notify the interface of the specified breakpoint
396 void (*create_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
397 void (*delete_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
398 void (*enable_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
399 void (*disable_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
401 /* Called during long calculations to allow GUI to repair window damage, and to
402 check for stop buttons, etc... */
404 void (*interactive_hook
) PARAMS ((void));
406 /* Called when the registers have changed, as a hint to a GUI
407 to minimize window update. */
409 void (*registers_changed_hook
) PARAMS ((void));
411 /* Called when going to wait for the target. Usually allows the GUI to run
412 while waiting for target events. */
414 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
*status
));
416 /* Used by UI as a wrapper around command execution. May do various things
417 like enabling/disabling buttons, etc... */
419 void (*call_command_hook
) PARAMS ((struct cmd_list_element
*c
, char *cmd
,
422 /* Takes control from error (). Typically used to prevent longjmps out of the
423 middle of the GUI. Usually used in conjunction with a catch routine. */
425 NORETURN
void (*error_hook
) PARAMS (());
428 /* Where to go for return_to_top_level (RETURN_ERROR). */
429 jmp_buf error_return
;
430 /* Where to go for return_to_top_level (RETURN_QUIT). */
433 /* Return for reason REASON. This generally gets back to the command
434 loop, but can be caught via catch_errors. */
437 return_to_top_level (reason
)
438 enum return_reason reason
;
443 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
444 I can think of a reason why that is vital, though). */
445 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
447 disable_current_display ();
448 do_cleanups (ALL_CLEANUPS
);
450 if (annotation_level
> 1)
461 (NORETURN
void) longjmp
462 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
465 /* Call FUNC with arg ARGS, catching any errors. If there is no
466 error, return the value returned by FUNC. If there is an error,
467 print ERRSTRING, print the specific error message, then return
470 Must not be called with immediate_quit in effect (bad things might
471 happen, say we got a signal in the middle of a memcpy to quit_return).
472 This is an OK restriction; with very few exceptions immediate_quit can
473 be replaced by judicious use of QUIT.
475 MASK specifies what to catch; it is normally set to
476 RETURN_MASK_ALL, if for no other reason than that the code which
477 calls catch_errors might not be set up to deal with a quit which
478 isn't caught. But if the code can deal with it, it generally
479 should be RETURN_MASK_ERROR, unless for some reason it is more
480 useful to abort only the portion of the operation inside the
481 catch_errors. Note that quit should return to the command line
482 fairly quickly, even if some further processing is being done. */
485 catch_errors (func
, args
, errstring
, mask
)
486 int (*func
) PARAMS ((char *));
495 struct cleanup
*saved_cleanup_chain
;
496 char *saved_error_pre_print
;
498 saved_cleanup_chain
= save_cleanups ();
499 saved_error_pre_print
= error_pre_print
;
501 if (mask
& RETURN_MASK_ERROR
)
502 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (jmp_buf));
503 if (mask
& RETURN_MASK_QUIT
)
504 memcpy (saved_quit
, quit_return
, sizeof (jmp_buf));
505 error_pre_print
= errstring
;
507 if (setjmp (tmp_jmp
) == 0)
509 if (mask
& RETURN_MASK_ERROR
)
510 memcpy (error_return
, tmp_jmp
, sizeof (jmp_buf));
511 if (mask
& RETURN_MASK_QUIT
)
512 memcpy (quit_return
, tmp_jmp
, sizeof (jmp_buf));
513 val
= (*func
) (args
);
518 restore_cleanups (saved_cleanup_chain
);
520 error_pre_print
= saved_error_pre_print
;
521 if (mask
& RETURN_MASK_ERROR
)
522 memcpy (error_return
, saved_error
, sizeof (jmp_buf));
523 if (mask
& RETURN_MASK_QUIT
)
524 memcpy (quit_return
, saved_quit
, sizeof (jmp_buf));
528 /* Handler for SIGHUP. */
534 catch_errors (quit_cover
, NULL
,
535 "Could not kill the program being debugged", RETURN_MASK_ALL
);
536 signal (SIGHUP
, SIG_DFL
);
537 kill (getpid (), SIGHUP
);
540 /* Just a little helper function for disconnect(). */
546 caution
= 0; /* Throw caution to the wind -- we're exiting.
547 This prevents asking the user dumb questions. */
548 quit_command((char *)0, 0);
552 /* Line number we are currently in in a file which is being sourced. */
553 static int source_line_number
;
555 /* Name of the file we are sourcing. */
556 static char *source_file_name
;
558 /* Buffer containing the error_pre_print used by the source stuff.
560 static char *source_error
;
561 static int source_error_allocated
;
563 /* Something to glom on to the start of error_pre_print if source_file_name
565 static char *source_pre_error
;
567 /* Clean up on error during a "source" command (or execution of a
568 user-defined command). */
571 source_cleanup (stream
)
574 /* Restore the previous input stream. */
578 /* Read commands from STREAM. */
580 read_command_file (stream
)
583 struct cleanup
*cleanups
;
585 cleanups
= make_cleanup (source_cleanup
, instream
);
588 do_cleanups (cleanups
);
591 extern void init_proc ();
593 void (*pre_init_ui_hook
) PARAMS ((void));
598 if (pre_init_ui_hook
)
601 /* Run the init function of each source file */
603 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
604 current_directory
= gdb_dirbuf
;
606 init_cmd_lists (); /* This needs to be done first */
607 initialize_targets (); /* Setup target_terminal macros for utils.c */
608 initialize_utils (); /* Make errors and warnings possible */
609 initialize_all_files ();
610 init_main (); /* But that omits this file! Do it now */
615 /* We need a default language for parsing expressions, so simple things like
616 "set width 0" won't fail if no language is explicitly set in a config file
617 or implicitly set by reading an executable during startup. */
618 set_language (language_c
);
619 expected_language
= current_language
; /* don't warn about the change. */
625 /* Allocate, initialize a new command line structure for one of the
626 control commands (if/while). */
628 static struct command_line
*
629 build_command_line (type
, args
)
630 enum command_control_type type
;
633 struct command_line
*cmd
;
635 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
637 cmd
->control_type
= type
;
641 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
643 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
644 cmd
->line
= savestring (args
, strlen (args
));
648 /* Build and return a new command structure for the control commands
649 such as "if" and "while". */
651 static struct command_line
*
652 get_command_line (type
, arg
)
653 enum command_control_type type
;
656 struct command_line
*cmd
;
657 struct cleanup
*old_chain
= NULL
;
659 /* Allocate and build a new command line structure. */
660 cmd
= build_command_line (type
, arg
);
662 old_chain
= make_cleanup (free_command_lines
, &cmd
);
664 /* Read in the body of this command. */
665 if (recurse_read_control_structure (cmd
) == invalid_control
)
667 warning ("error reading in control structure\n");
668 do_cleanups (old_chain
);
672 discard_cleanups (old_chain
);
676 /* Recursively print a command (including full control structures). */
678 print_command_line (cmd
, depth
)
679 struct command_line
*cmd
;
686 for (i
= 0; i
< depth
; i
++)
687 fputs_filtered (" ", gdb_stdout
);
690 /* A simple command, print it and return. */
691 if (cmd
->control_type
== simple_control
)
693 fputs_filtered (cmd
->line
, gdb_stdout
);
694 fputs_filtered ("\n", gdb_stdout
);
698 /* loop_continue to jump to the start of a while loop, print it
700 if (cmd
->control_type
== continue_control
)
702 fputs_filtered ("loop_continue\n", gdb_stdout
);
706 /* loop_break to break out of a while loop, print it and return. */
707 if (cmd
->control_type
== break_control
)
709 fputs_filtered ("loop_break\n", gdb_stdout
);
713 /* A while command. Recursively print its subcommands before returning. */
714 if (cmd
->control_type
== while_control
)
716 struct command_line
*list
;
717 fputs_filtered ("while ", gdb_stdout
);
718 fputs_filtered (cmd
->line
, gdb_stdout
);
719 fputs_filtered ("\n", gdb_stdout
);
720 list
= *cmd
->body_list
;
723 print_command_line (list
, depth
+ 1);
728 /* An if command. Recursively print both arms before returning. */
729 if (cmd
->control_type
== if_control
)
731 fputs_filtered ("if ", gdb_stdout
);
732 fputs_filtered (cmd
->line
, gdb_stdout
);
733 fputs_filtered ("\n", gdb_stdout
);
735 print_command_line (cmd
->body_list
[0], depth
+ 1);
737 /* Show the false arm if it exists. */
738 if (cmd
->body_count
== 2)
742 for (i
= 0; i
< depth
; i
++)
743 fputs_filtered (" ", gdb_stdout
);
745 fputs_filtered ("else\n", gdb_stdout
);
746 print_command_line (cmd
->body_list
[1], depth
+ 1);
750 for (i
= 0; i
< depth
; i
++)
751 fputs_filtered (" ", gdb_stdout
);
753 fputs_filtered ("end\n", gdb_stdout
);
757 /* Execute the command in CMD. */
759 enum command_control_type
760 execute_control_command (cmd
)
761 struct command_line
*cmd
;
763 struct expression
*expr
;
764 struct command_line
*current
;
765 struct cleanup
*old_chain
= 0;
768 enum command_control_type ret
;
771 switch (cmd
->control_type
)
774 /* A simple command, execute it and return. */
775 new_line
= insert_args (cmd
->line
);
777 return invalid_control
;
778 old_chain
= make_cleanup (free_current_contents
, &new_line
);
779 execute_command (new_line
, 0);
780 ret
= cmd
->control_type
;
783 case continue_control
:
785 /* Return for "continue", and "break" so we can either
786 continue the loop at the top, or break out. */
787 ret
= cmd
->control_type
;
792 /* Parse the loop control expression for the while statement. */
793 new_line
= insert_args (cmd
->line
);
795 return invalid_control
;
796 old_chain
= make_cleanup (free_current_contents
, &new_line
);
797 expr
= parse_expression (new_line
);
798 make_cleanup (free_current_contents
, &expr
);
800 ret
= simple_control
;
803 /* Keep iterating so long as the expression is true. */
806 /* Evaluate the expression. */
807 val
= evaluate_expression (expr
);
809 /* If the value is false, then break out of the loop. */
810 if (!value_true (val
))
813 /* Execute the body of the while statement. */
814 current
= *cmd
->body_list
;
817 ret
= execute_control_command (current
);
819 /* If we got an error, or a "break" command, then stop
821 if (ret
== invalid_control
|| ret
== break_control
)
827 /* If we got a "continue" command, then restart the loop
829 if (ret
== continue_control
)
832 /* Get the next statement. */
833 current
= current
->next
;
837 /* Reset RET so that we don't recurse the break all the way down. */
838 if (ret
== break_control
)
839 ret
= simple_control
;
846 new_line
= insert_args (cmd
->line
);
848 return invalid_control
;
849 old_chain
= make_cleanup (free_current_contents
, &new_line
);
850 /* Parse the conditional for the if statement. */
851 expr
= parse_expression (new_line
);
852 make_cleanup (free_current_contents
, &expr
);
855 ret
= simple_control
;
857 /* Evaluate the conditional. */
858 val
= evaluate_expression (expr
);
860 /* Choose which arm to take commands from based on the value of the
861 conditional expression. */
862 if (value_true (val
))
863 current
= *cmd
->body_list
;
864 else if (cmd
->body_count
== 2)
865 current
= *(cmd
->body_list
+ 1);
867 /* Execute commands in the given arm. */
870 ret
= execute_control_command (current
);
872 /* If we got an error, get out. */
873 if (ret
!= simple_control
)
876 /* Get the next statement in the body. */
877 current
= current
->next
;
884 warning ("Invalid control type in command structure.");
885 return invalid_control
;
889 do_cleanups (old_chain
);
894 /* "while" command support. Executes a body of statements while the
895 loop condition is nonzero. */
898 while_command (arg
, from_tty
)
902 struct command_line
*command
= NULL
;
905 command
= get_command_line (while_control
, arg
);
910 execute_control_command (command
);
911 free_command_lines (&command
);
914 /* "if" command support. Execute either the true or false arm depending
915 on the value of the if conditional. */
918 if_command (arg
, from_tty
)
922 struct command_line
*command
= NULL
;
925 command
= get_command_line (if_control
, arg
);
930 execute_control_command (command
);
931 free_command_lines (&command
);
938 struct user_args
*oargs
= user_args
;
940 fatal ("Internal error, arg_cleanup called with no user args.\n");
942 user_args
= user_args
->next
;
946 /* Bind the incomming arguments for a user defined command to
947 $arg0, $arg1 ... $argMAXUSERARGS. */
949 static struct cleanup
*
953 struct user_args
*args
;
954 struct cleanup
*old_chain
;
955 unsigned int arg_count
= 0;
957 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
958 memset (args
, 0, sizeof (struct user_args
));
960 args
->next
= user_args
;
963 old_chain
= make_cleanup (arg_cleanup
, 0);
972 if (arg_count
>= MAXUSERARGS
)
974 error ("user defined function may only have %d arguments.\n",
979 /* Strip whitespace. */
980 while (*p
== ' ' || *p
== '\t')
983 /* P now points to an argument. */
985 user_args
->a
[arg_count
].arg
= p
;
987 /* Get to the end of this argument. */
988 while (*p
&& *p
!= ' ' && *p
!= '\t')
991 user_args
->a
[arg_count
].len
= p
- start_arg
;
998 /* Given character string P, return a point to the first argument ($arg),
999 or NULL if P contains no arguments. */
1005 while ((p
= strchr (p
, '$')))
1007 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1014 /* Insert the user defined arguments stored in user_arg into the $arg
1015 arguments found in line, with the updated copy being placed into nline. */
1021 char *p
, *save_line
, *new_line
;
1024 /* First we need to know how much memory to allocate for the new line. */
1027 while ((p
= locate_arg (line
)))
1032 if (i
>= user_args
->count
)
1034 error ("Missing argument %d in user function.\n", i
);
1037 len
+= user_args
->a
[i
].len
;
1041 /* Don't forget the tail. */
1042 len
+= strlen (line
);
1044 /* Allocate space for the new line and fill it in. */
1045 new_line
= (char *)xmalloc (len
+ 1);
1046 if (new_line
== NULL
)
1049 /* Restore pointer to beginning of old line. */
1052 /* Save pointer to beginning of new line. */
1053 save_line
= new_line
;
1055 while ((p
= locate_arg (line
)))
1059 memcpy (new_line
, line
, p
- line
);
1060 new_line
+= p
- line
;
1063 len
= user_args
->a
[i
].len
;
1066 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1071 /* Don't forget the tail. */
1072 strcpy (new_line
, line
);
1074 /* Return a pointer to the beginning of the new line. */
1079 execute_user_command (c
, args
)
1080 struct cmd_list_element
*c
;
1083 register struct command_line
*cmdlines
;
1084 struct cleanup
*old_chain
;
1085 enum command_control_type ret
;
1087 old_chain
= setup_user_args (args
);
1089 cmdlines
= c
->user_commands
;
1094 /* Set the instream to 0, indicating execution of a
1095 user-defined function. */
1096 old_chain
= make_cleanup (source_cleanup
, instream
);
1097 instream
= (FILE *) 0;
1100 ret
= execute_control_command (cmdlines
);
1101 if (ret
!= simple_control
&& ret
!= break_control
)
1103 warning ("Error in control structure.\n");
1106 cmdlines
= cmdlines
->next
;
1108 do_cleanups (old_chain
);
1111 /* Execute the line P as a command.
1112 Pass FROM_TTY as second argument to the defining function. */
1115 execute_command (p
, from_tty
)
1119 register struct cmd_list_element
*c
;
1120 register enum language flang
;
1121 static int warned
= 0;
1125 /* This can happen when command_line_input hits end of file. */
1129 while (*p
== ' ' || *p
== '\t') p
++;
1134 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1135 /* Pass null arg rather than an empty one. */
1138 /* If this command has been hooked, run the hook first. */
1140 execute_user_command (c
->hook
, (char *)0);
1142 if (c
->class == class_user
)
1143 execute_user_command (c
, arg
);
1144 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1145 do_setshow_command (arg
, from_tty
& caution
, c
);
1146 else if (c
->function
.cfunc
== NO_FUNCTION
)
1147 error ("That is not a command, just a help topic.");
1148 else if (call_command_hook
)
1149 call_command_hook (c
, arg
, from_tty
& caution
);
1151 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1154 /* Tell the user if the language has changed (except first time). */
1155 if (current_language
!= expected_language
)
1157 if (language_mode
== language_mode_auto
) {
1158 language_info (1); /* Print what changed. */
1163 /* Warn the user if the working language does not match the
1164 language of the current frame. Only warn the user if we are
1165 actually running the program, i.e. there is a stack. */
1166 /* FIXME: This should be cacheing the frame and only running when
1167 the frame changes. */
1169 if (target_has_stack
)
1171 flang
= get_frame_language ();
1173 && flang
!= language_unknown
1174 && flang
!= current_language
->la_language
)
1176 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1184 command_loop_marker (foo
)
1189 /* Read commands from `instream' and execute them
1190 until end of file or error reading instream. */
1195 struct cleanup
*old_chain
;
1197 int stdin_is_tty
= ISATTY (stdin
);
1198 long time_at_cmd_start
;
1199 long space_at_cmd_start
;
1200 extern int display_time
;
1201 extern int display_space
;
1203 while (!feof (instream
))
1205 if (window_hook
&& instream
== stdin
)
1206 (*window_hook
) (instream
, prompt
);
1209 if (instream
== stdin
&& stdin_is_tty
)
1210 reinitialize_more_filter ();
1211 old_chain
= make_cleanup (command_loop_marker
, 0);
1212 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1213 instream
== stdin
, "prompt");
1217 time_at_cmd_start
= get_run_time ();
1221 extern char **environ
;
1222 char *lim
= (char *) sbrk (0);
1224 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1227 execute_command (command
, instream
== stdin
);
1228 /* Do any commands attached to breakpoint we stopped at. */
1229 bpstat_do_actions (&stop_bpstat
);
1230 do_cleanups (old_chain
);
1234 long cmd_time
= get_run_time () - time_at_cmd_start
;
1236 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1237 cmd_time
/ 1000000, cmd_time
% 1000000);
1242 extern char **environ
;
1243 char *lim
= (char *) sbrk (0);
1244 long space_now
= lim
- (char *) &environ
;
1245 long space_diff
= space_now
- space_at_cmd_start
;
1247 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1249 (space_diff
>= 0 ? '+' : '-'),
1255 /* Commands call this if they do not want to be repeated by null lines. */
1263 /* If we aren't reading from standard input, we are saving the last
1264 thing read from stdin in line and don't want to delete it. Null lines
1265 won't repeat here in any case. */
1266 if (instream
== stdin
)
1270 /* Read a line from the stream "instream" without command line editing.
1272 It prints PRROMPT once at the start.
1273 Action is compatible with "readline", e.g. space for the result is
1274 malloc'd and should be freed by the caller.
1276 A NULL return means end of file. */
1278 gdb_readline (prrompt
)
1283 int input_index
= 0;
1284 int result_size
= 80;
1288 /* Don't use a _filtered function here. It causes the assumed
1289 character position to be off, since the newline we read from
1290 the user is not accounted for. */
1291 fputs_unfiltered (prrompt
, gdb_stdout
);
1292 /* start-sanitize-mpw */
1294 /* Move to a new line so the entered line doesn't have a prompt
1295 on the front of it. */
1296 fputs_unfiltered ("\n", gdb_stdout
);
1298 /* end-sanitize-mpw */
1299 gdb_flush (gdb_stdout
);
1302 result
= (char *) xmalloc (result_size
);
1306 /* Read from stdin if we are executing a user defined command.
1307 This is the right thing for prompt_for_continue, at least. */
1308 c
= fgetc (instream
? instream
: stdin
);
1312 if (input_index
> 0)
1313 /* The last line does not end with a newline. Return it, and
1314 if we are called again fgetc will still return EOF and
1315 we'll return NULL then. */
1324 result
[input_index
++] = c
;
1325 while (input_index
>= result_size
)
1328 result
= (char *) xrealloc (result
, result_size
);
1332 result
[input_index
++] = '\0';
1336 /* Variables which control command line editing and history
1337 substitution. These variables are given default values at the end
1339 static int command_editing_p
;
1340 static int history_expansion_p
;
1341 static int write_history_p
;
1342 static int history_size
;
1343 static char *history_filename
;
1345 /* readline uses the word breaks for two things:
1346 (1) In figuring out where to point the TEXT parameter to the
1347 rl_completion_entry_function. Since we don't use TEXT for much,
1348 it doesn't matter a lot what the word breaks are for this purpose, but
1349 it does affect how much stuff M-? lists.
1350 (2) If one of the matches contains a word break character, readline
1351 will quote it. That's why we switch between
1352 gdb_completer_word_break_characters and
1353 gdb_completer_command_word_break_characters. I'm not sure when
1354 we need this behavior (perhaps for funky characters in C++ symbols?). */
1356 /* Variables which are necessary for fancy command line editing. */
1357 char *gdb_completer_word_break_characters
=
1358 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1360 /* When completing on command names, we remove '-' from the list of
1361 word break characters, since we use it in command names. If the
1362 readline library sees one in any of the current completion strings,
1363 it thinks that the string needs to be quoted and automatically supplies
1365 char *gdb_completer_command_word_break_characters
=
1366 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1368 /* Characters that can be used to quote completion strings. Note that we
1369 can't include '"' because the gdb C parser treats such quoted sequences
1371 char *gdb_completer_quote_characters
=
1374 /* Functions that are used as part of the fancy command line editing. */
1376 /* This can be used for functions which don't want to complete on symbols
1377 but don't want to complete on anything else either. */
1380 noop_completer (text
, prefix
)
1387 /* Complete on filenames. */
1389 filename_completer (text
, word
)
1393 /* From readline. */
1394 extern char *filename_completion_function ();
1395 int subsequent_name
;
1397 int return_val_used
;
1398 int return_val_alloced
;
1400 return_val_used
= 0;
1401 /* Small for testing. */
1402 return_val_alloced
= 1;
1403 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1405 subsequent_name
= 0;
1409 p
= filename_completion_function (text
, subsequent_name
);
1410 if (return_val_used
>= return_val_alloced
)
1412 return_val_alloced
*= 2;
1414 (char **) xrealloc (return_val
,
1415 return_val_alloced
* sizeof (char *));
1419 return_val
[return_val_used
++] = p
;
1422 /* Like emacs, don't complete on old versions. Especially useful
1423 in the "source" command. */
1424 if (p
[strlen (p
) - 1] == '~')
1430 /* Return exactly p. */
1431 return_val
[return_val_used
++] = p
;
1432 else if (word
> text
)
1434 /* Return some portion of p. */
1435 q
= xmalloc (strlen (p
) + 5);
1436 strcpy (q
, p
+ (word
- text
));
1437 return_val
[return_val_used
++] = q
;
1442 /* Return some of TEXT plus p. */
1443 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1444 strncpy (q
, word
, text
- word
);
1445 q
[text
- word
] = '\0';
1447 return_val
[return_val_used
++] = q
;
1451 subsequent_name
= 1;
1454 /* There is no way to do this just long enough to affect quote inserting
1455 without also affecting the next completion. This should be fixed in
1457 /* Insure that readline does the right thing
1458 with respect to inserting quotes. */
1459 rl_completer_word_break_characters
= "";
1464 /* Here are some useful test cases for completion. FIXME: These should
1465 be put in the test suite. They should be tested with both M-? and TAB.
1467 "show output-" "radix"
1468 "show output" "-radix"
1469 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1470 "p " ambiguous (all symbols)
1471 "info t foo" no completions
1472 "info t " no completions
1473 "info t" ambiguous ("info target", "info terminal", etc.)
1474 "info ajksdlfk" no completions
1475 "info ajksdlfk " no completions
1477 "info " ambiguous (all info commands)
1478 "p \"a" no completions (string constant)
1479 "p 'a" ambiguous (all symbols starting with a)
1480 "p b-a" ambiguous (all symbols starting with a)
1481 "p b-" ambiguous (all symbols)
1482 "file Make" "file" (word break hard to screw up here)
1483 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1486 /* Generate completions one by one for the completer. Each time we are
1487 called return another potential completion to the caller.
1488 line_completion just completes on commands or passes the buck to the
1489 command's completer function, the stuff specific to symbol completion
1490 is in make_symbol_completion_list.
1492 TEXT is the caller's idea of the "word" we are looking at.
1494 MATCHES is the number of matches that have currently been collected from
1495 calling this completion function. When zero, then we need to initialize,
1496 otherwise the initialization has already taken place and we can just
1497 return the next potential completion string.
1499 LINE_BUFFER is available to be looked at; it contains the entire text
1500 of the line. POINT is the offset in that line of the cursor. You
1501 should pretend that the line ends at POINT.
1503 Returns NULL if there are no more completions, else a pointer to a string
1504 which is a possible completion, it is the caller's responsibility to
1508 line_completion_function (text
, matches
, line_buffer
, point
)
1514 static char **list
= (char **)NULL
; /* Cache of completions */
1515 static int index
; /* Next cached completion */
1516 char *output
= NULL
;
1517 char *tmp_command
, *p
;
1518 /* Pointer within tmp_command which corresponds to text. */
1520 struct cmd_list_element
*c
, *result_list
;
1524 /* The caller is beginning to accumulate a new set of completions, so
1525 we need to find all of them now, and cache them for returning one at
1526 a time on future calls. */
1530 /* Free the storage used by LIST, but not by the strings inside.
1531 This is because rl_complete_internal () frees the strings. */
1537 /* Choose the default set of word break characters to break completions.
1538 If we later find out that we are doing completions on command strings
1539 (as opposed to strings supplied by the individual command completer
1540 functions, which can be any string) then we will switch to the
1541 special word break set for command strings, which leaves out the
1542 '-' character used in some commands. */
1544 rl_completer_word_break_characters
=
1545 gdb_completer_word_break_characters
;
1547 /* Decide whether to complete on a list of gdb commands or on symbols. */
1548 tmp_command
= (char *) alloca (point
+ 1);
1551 strncpy (tmp_command
, line_buffer
, point
);
1552 tmp_command
[point
] = '\0';
1553 /* Since text always contains some number of characters leading up
1554 to point, we can find the equivalent position in tmp_command
1555 by subtracting that many characters from the end of tmp_command. */
1556 word
= tmp_command
+ point
- strlen (text
);
1560 /* An empty line we want to consider ambiguous; that is, it
1561 could be any command. */
1562 c
= (struct cmd_list_element
*) -1;
1567 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1570 /* Move p up to the next interesting thing. */
1571 while (*p
== ' ' || *p
== '\t')
1578 /* It is an unrecognized command. So there are no
1579 possible completions. */
1582 else if (c
== (struct cmd_list_element
*) -1)
1586 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1587 doesn't advance over that thing itself. Do so now. */
1589 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1591 if (q
!= tmp_command
+ point
)
1593 /* There is something beyond the ambiguous
1594 command, so there are no possible completions. For
1595 example, "info t " or "info t foo" does not complete
1596 to anything, because "info t" can be "info target" or
1602 /* We're trying to complete on the command which was ambiguous.
1603 This we can deal with. */
1606 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1611 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1613 /* Insure that readline does the right thing with respect to
1614 inserting quotes. */
1615 rl_completer_word_break_characters
=
1616 gdb_completer_command_word_break_characters
;
1621 /* We've recognized a full command. */
1623 if (p
== tmp_command
+ point
)
1625 /* There is no non-whitespace in the line beyond the command. */
1627 if (p
[-1] == ' ' || p
[-1] == '\t')
1629 /* The command is followed by whitespace; we need to complete
1630 on whatever comes after command. */
1633 /* It is a prefix command; what comes after it is
1634 a subcommand (e.g. "info "). */
1635 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1637 /* Insure that readline does the right thing
1638 with respect to inserting quotes. */
1639 rl_completer_word_break_characters
=
1640 gdb_completer_command_word_break_characters
;
1644 /* It is a normal command; what comes after it is
1645 completed by the command's completer function. */
1646 list
= (*c
->completer
) (p
, word
);
1651 /* The command is not followed by whitespace; we need to
1652 complete on the command itself. e.g. "p" which is a
1653 command itself but also can complete to "print", "ptype"
1657 /* Find the command we are completing on. */
1659 while (q
> tmp_command
)
1661 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1667 list
= complete_on_cmdlist (result_list
, q
, word
);
1669 /* Insure that readline does the right thing
1670 with respect to inserting quotes. */
1671 rl_completer_word_break_characters
=
1672 gdb_completer_command_word_break_characters
;
1677 /* There is non-whitespace beyond the command. */
1679 if (c
->prefixlist
&& !c
->allow_unknown
)
1681 /* It is an unrecognized subcommand of a prefix command,
1682 e.g. "info adsfkdj". */
1687 /* It is a normal command. */
1688 list
= (*c
->completer
) (p
, word
);
1694 /* If we found a list of potential completions during initialization then
1695 dole them out one at a time. The vector of completions is NULL
1696 terminated, so after returning the last one, return NULL (and continue
1697 to do so) each time we are called after that, until a new list is
1702 output
= list
[index
];
1710 /* Can't do this because readline hasn't yet checked the word breaks
1711 for figuring out whether to insert a quote. */
1713 /* Make sure the word break characters are set back to normal for the
1714 next time that readline tries to complete something. */
1715 rl_completer_word_break_characters
=
1716 gdb_completer_word_break_characters
;
1722 /* Line completion interface function for readline. */
1725 readline_line_completion_function (text
, matches
)
1729 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1732 /* Skip over a possibly quoted word (as defined by the quote characters
1733 and word break characters the completer uses). Returns pointer to the
1734 location after the "word". */
1740 char quote_char
= '\0';
1743 for (scan
= str
; *scan
!= '\0'; scan
++)
1745 if (quote_char
!= '\0')
1747 /* Ignore everything until the matching close quote char */
1748 if (*scan
== quote_char
)
1750 /* Found matching close quote. */
1755 else if (strchr (gdb_completer_quote_characters
, *scan
))
1757 /* Found start of a quoted string. */
1760 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1774 #if STOP_SIGNAL == SIGTSTP
1775 signal (SIGTSTP
, SIG_DFL
);
1777 kill (getpid (), SIGTSTP
);
1778 signal (SIGTSTP
, stop_sig
);
1780 signal (STOP_SIGNAL
, stop_sig
);
1782 printf_unfiltered ("%s", prompt
);
1783 gdb_flush (gdb_stdout
);
1785 /* Forget about any previous command -- null line now will do nothing. */
1788 #endif /* STOP_SIGNAL */
1790 /* Initialize signal handlers. */
1800 signal (SIGINT
, request_quit
);
1802 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1803 passed to the inferior, which we don't want. It would be
1804 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1805 on BSD4.3 systems using vfork, that can affect the
1806 GDB process as well as the inferior (the signal handling tables
1807 might be in memory, shared between the two). Since we establish
1808 a handler for SIGQUIT, when we call exec it will set the signal
1809 to SIG_DFL for us. */
1810 signal (SIGQUIT
, do_nothing
);
1811 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1812 signal (SIGHUP
, disconnect
);
1813 signal (SIGFPE
, float_handler
);
1815 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1816 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1820 /* Read one line from the command input stream `instream'
1821 into the local static buffer `linebuffer' (whose current length
1823 The buffer is made bigger as necessary.
1824 Returns the address of the start of the line.
1826 NULL is returned for end of file.
1828 *If* the instream == stdin & stdin is a terminal, the line read
1829 is copied into the file line saver (global var char *line,
1830 length linesize) so that it can be duplicated.
1832 This routine either uses fancy command line editing or
1833 simple input as the user has requested. */
1836 command_line_input (prrompt
, repeat
, annotation_suffix
)
1839 char *annotation_suffix
;
1841 static char *linebuffer
= 0;
1842 static unsigned linelength
= 0;
1846 char *local_prompt
= prrompt
;
1851 /* The annotation suffix must be non-NULL. */
1852 if (annotation_suffix
== NULL
)
1853 annotation_suffix
= "";
1855 if (annotation_level
> 1 && instream
== stdin
)
1857 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
1858 + strlen (annotation_suffix
) + 40);
1859 if (prrompt
== NULL
)
1860 local_prompt
[0] = '\0';
1862 strcpy (local_prompt
, prrompt
);
1863 strcat (local_prompt
, "\n\032\032");
1864 strcat (local_prompt
, annotation_suffix
);
1865 strcat (local_prompt
, "\n");
1868 if (linebuffer
== 0)
1871 linebuffer
= (char *) xmalloc (linelength
);
1876 /* Control-C quits instantly if typed while in this loop
1877 since it should not wait until the user types a newline. */
1881 signal (STOP_SIGNAL
, stop_sig
);
1886 /* Make sure that all output has been output. Some machines may let
1887 you get away with leaving out some of the gdb_flush, but not all. */
1889 gdb_flush (gdb_stdout
);
1890 gdb_flush (gdb_stderr
);
1892 if (source_file_name
!= NULL
)
1894 ++source_line_number
;
1895 sprintf (source_error
,
1896 "%s%s:%d: Error in sourced command file:\n",
1899 source_line_number
);
1900 error_pre_print
= source_error
;
1903 if (annotation_level
> 1 && instream
== stdin
)
1905 printf_unfiltered ("\n\032\032pre-");
1906 printf_unfiltered (annotation_suffix
);
1907 printf_unfiltered ("\n");
1910 /* Don't use fancy stuff if not talking to stdin. */
1911 if (command_editing_p
&& instream
== stdin
1912 && ISATTY (instream
))
1913 rl
= readline (local_prompt
);
1915 rl
= gdb_readline (local_prompt
);
1917 if (annotation_level
> 1 && instream
== stdin
)
1919 printf_unfiltered ("\n\032\032post-");
1920 printf_unfiltered (annotation_suffix
);
1921 printf_unfiltered ("\n");
1924 if (!rl
|| rl
== (char *) EOF
)
1929 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
1931 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
1932 nline
= (char *) xrealloc (linebuffer
, linelength
);
1933 p
+= nline
- linebuffer
;
1937 /* Copy line. Don't copy null at end. (Leaves line alone
1938 if this was just a newline) */
1942 free (rl
); /* Allocated in readline. */
1944 if (p
== linebuffer
|| *(p
- 1) != '\\')
1947 p
--; /* Put on top of '\'. */
1948 local_prompt
= (char *) 0;
1953 signal (STOP_SIGNAL
, SIG_DFL
);
1960 #define SERVER_COMMAND_LENGTH 7
1962 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
1963 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
1966 /* Note that we don't set `line'. Between this and the check in
1967 dont_repeat, this insures that repeating will still do the
1970 return linebuffer
+ SERVER_COMMAND_LENGTH
;
1973 /* Do history expansion if that is wished. */
1974 if (history_expansion_p
&& instream
== stdin
1975 && ISATTY (instream
))
1977 char *history_value
;
1980 *p
= '\0'; /* Insert null now. */
1981 expanded
= history_expand (linebuffer
, &history_value
);
1984 /* Print the changes. */
1985 printf_unfiltered ("%s\n", history_value
);
1987 /* If there was an error, call this function again. */
1990 free (history_value
);
1991 return command_line_input (prrompt
, repeat
, annotation_suffix
);
1993 if (strlen (history_value
) > linelength
)
1995 linelength
= strlen (history_value
) + 1;
1996 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
1998 strcpy (linebuffer
, history_value
);
1999 p
= linebuffer
+ strlen(linebuffer
);
2000 free (history_value
);
2004 /* If we just got an empty line, and that is supposed
2005 to repeat the previous command, return the value in the
2009 if (p
== linebuffer
)
2012 while (*p1
== ' ' || *p1
== '\t')
2020 /* Add line to history if appropriate. */
2021 if (instream
== stdin
2022 && ISATTY (stdin
) && *linebuffer
)
2023 add_history (linebuffer
);
2025 /* Note: lines consisting solely of comments are added to the command
2026 history. This is useful when you type a command, and then
2027 realize you don't want to execute it quite yet. You can comment
2028 out the command and then later fetch it from the value history
2029 and remove the '#'. The kill ring is probably better, but some
2030 people are in the habit of commenting things out. */
2032 while ((c
= *p1
++) != '\0')
2035 while ((c
= *p1
++) != '"')
2037 /* Make sure an escaped '"' doesn't make us think the string
2045 while ((c
= *p1
++) != '\'')
2047 /* Make sure an escaped '\'' doesn't make us think the string
2056 /* Found a comment. */
2062 /* Save into global buffer if appropriate. */
2065 if (linelength
> linesize
)
2067 line
= xrealloc (line
, linelength
);
2068 linesize
= linelength
;
2070 strcpy (line
, linebuffer
);
2078 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2079 code bodies. This is typically used when we encounter an "else"
2080 clause for an "if" command. */
2083 realloc_body_list (command
, new_length
)
2084 struct command_line
*command
;
2088 struct command_line
**body_list
;
2090 n
= command
->body_count
;
2092 /* Nothing to do? */
2093 if (new_length
<= n
)
2096 body_list
= (struct command_line
**)
2097 xmalloc (sizeof (struct command_line
*) * new_length
);
2099 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2101 free (command
->body_list
);
2102 command
->body_list
= body_list
;
2103 command
->body_count
= new_length
;
2106 /* Read one line from the input stream. If the command is an "else" or
2107 "end", return such an indication to the caller. */
2109 static enum misc_command_type
2110 read_next_line (command
)
2111 struct command_line
**command
;
2113 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2116 if (control_level
>= 254)
2117 error ("Control nesting too deep!\n");
2119 /* Set a prompt based on the nesting of the control commands. */
2120 if (instream
== stdin
)
2122 for (i
= 0; i
< control_level
; i
++)
2123 control_prompt
[i
] = ' ';
2124 control_prompt
[i
] = '>';
2125 control_prompt
[i
+1] = '\0';
2126 prompt_ptr
= (char *)&control_prompt
[0];
2131 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2133 /* Not sure what to do here. */
2137 /* Strip leading and trailing whitespace. */
2138 while (*p
== ' ' || *p
== '\t')
2141 p1
= p
+ strlen (p
);
2142 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2145 /* Blanks and comments don't really do anything, but we need to
2146 distinguish them from else, end and other commands which can be
2148 if (p1
== p
|| p
[0] == '#')
2151 /* Is this the end of a simple, while, or if control structure? */
2152 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2155 /* Is the else clause of an if control structure? */
2156 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2157 return else_command
;
2159 /* Check for while, if, break, continue, etc and build a new command
2160 line structure for them. */
2161 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2162 *command
= build_command_line (while_control
, p
+ 6);
2163 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2164 *command
= build_command_line (if_control
, p
+ 3);
2165 else if (p1
- p
== 5 && !strncmp (p
, "loop_break", 5))
2167 *command
= (struct command_line
*)
2168 xmalloc (sizeof (struct command_line
));
2169 (*command
)->next
= NULL
;
2170 (*command
)->line
= NULL
;
2171 (*command
)->control_type
= break_control
;
2172 (*command
)->body_count
= 0;
2173 (*command
)->body_list
= NULL
;
2175 else if (p1
- p
== 8 && !strncmp (p
, "loop_continue", 8))
2177 *command
= (struct command_line
*)
2178 xmalloc (sizeof (struct command_line
));
2179 (*command
)->next
= NULL
;
2180 (*command
)->line
= NULL
;
2181 (*command
)->control_type
= continue_control
;
2182 (*command
)->body_count
= 0;
2183 (*command
)->body_list
= NULL
;
2187 /* A normal command. */
2188 *command
= (struct command_line
*)
2189 xmalloc (sizeof (struct command_line
));
2190 (*command
)->next
= NULL
;
2191 (*command
)->line
= savestring (p
, p1
- p
);
2192 (*command
)->control_type
= simple_control
;
2193 (*command
)->body_count
= 0;
2194 (*command
)->body_list
= NULL
;
2197 /* Nothing special. */
2201 /* Recursively read in the control structures and create a command_line
2204 The parent_control parameter is the control structure in which the
2205 following commands are nested. */
2207 static enum command_control_type
2208 recurse_read_control_structure (current_cmd
)
2209 struct command_line
*current_cmd
;
2211 int current_body
, i
;
2212 enum misc_command_type val
;
2213 enum command_control_type ret
;
2214 struct command_line
**body_ptr
, *child_tail
, *next
;
2215 struct cleanup
*old_chains
, *tmp_chains
;
2221 /* Sanity checks. */
2222 if (current_cmd
->control_type
== simple_control
)
2224 error ("Recursed on a simple control type\n");
2225 return invalid_control
;
2228 if (current_body
> current_cmd
->body_count
)
2230 error ("Allocated body is smaller than this command type needs\n");
2231 return invalid_control
;
2234 /* Read lines from the input stream and build control structures. */
2240 val
= read_next_line (&next
);
2242 /* Just skip blanks and comments. */
2243 if (val
== nop_command
)
2246 if (val
== end_command
)
2248 if (current_cmd
->control_type
== while_control
2249 || current_cmd
->control_type
== if_control
)
2251 /* Success reading an entire control structure. */
2252 ret
= simple_control
;
2257 ret
= invalid_control
;
2262 /* Not the end of a control structure. */
2263 if (val
== else_command
)
2265 if (current_cmd
->control_type
== if_control
2266 && current_body
== 1)
2268 realloc_body_list (current_cmd
, 2);
2275 ret
= invalid_control
;
2282 child_tail
->next
= next
;
2286 /* We have just read the first line of the child's control
2287 structure. From now on, arrange to throw away the line
2288 we have if we quit or get an error. */
2289 body_ptr
= current_cmd
->body_list
;
2290 for (i
= 1; i
< current_body
; i
++)
2295 tmp_chains
= make_cleanup (free_command_lines
, body_ptr
);
2298 old_chains
= tmp_chains
;
2303 /* If the latest line is another control structure, then recurse
2305 if (next
->control_type
== while_control
2306 || next
->control_type
== if_control
)
2309 ret
= recurse_read_control_structure (next
);
2312 if (ret
!= simple_control
)
2318 if (ret
== invalid_control
&& old_chains
)
2319 do_cleanups (old_chains
);
2320 else if (old_chains
)
2321 discard_cleanups (old_chains
);
2327 /* Read lines from the input stream
2328 and accumulate them in a chain of struct command_line's
2329 which is then returned. */
2331 struct command_line
*
2332 read_command_lines ()
2334 struct command_line
*head
, *tail
, *next
;
2335 struct cleanup
*old_chain
;
2336 enum command_control_type ret
;
2337 enum misc_command_type val
;
2344 val
= read_next_line (&next
);
2346 /* Ignore blank lines or comments. */
2347 if (val
== nop_command
)
2350 if (val
== end_command
)
2352 ret
= simple_control
;
2356 if (val
!= ok_command
)
2358 ret
= invalid_control
;
2362 if (next
->control_type
== while_control
2363 || next
->control_type
== if_control
)
2366 ret
= recurse_read_control_structure (next
);
2369 if (ret
== invalid_control
)
2380 old_chain
= make_cleanup (free_command_lines
, &head
);
2389 if (ret
!= invalid_control
)
2391 discard_cleanups (old_chain
);
2395 do_cleanups (old_chain
);
2401 /* Free a chain of struct command_line's. */
2404 free_command_lines (lptr
)
2405 struct command_line
**lptr
;
2407 register struct command_line
*l
= *lptr
;
2408 register struct command_line
*next
;
2409 struct command_line
**blist
;
2414 if (l
->body_count
> 0)
2416 blist
= l
->body_list
;
2417 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2418 free_command_lines (blist
);
2427 /* Add an element to the list of info subcommands. */
2430 add_info (name
, fun
, doc
)
2432 void (*fun
) PARAMS ((char *, int));
2435 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2438 /* Add an alias to the list of info subcommands. */
2441 add_info_alias (name
, oldname
, abbrev_flag
)
2446 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2449 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2450 Therefore, its own definition is called only for "info" with no args. */
2454 info_command (arg
, from_tty
)
2458 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2459 help_list (infolist
, "info ", -1, gdb_stdout
);
2462 /* The "complete" command is used by Emacs to implement completion. */
2466 complete_command (arg
, from_tty
)
2478 argpoint
= strlen (arg
);
2480 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2482 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2484 printf_unfiltered ("%s\n", completion
);
2489 /* The "show" command with no arguments shows all the settings. */
2493 show_command (arg
, from_tty
)
2497 cmd_show_list (showlist
, from_tty
, "");
2500 /* Add an element to the list of commands. */
2503 add_com (name
, class, fun
, doc
)
2505 enum command_class
class;
2506 void (*fun
) PARAMS ((char *, int));
2509 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2512 /* Add an alias or abbreviation command to the list of commands. */
2515 add_com_alias (name
, oldname
, class, abbrev_flag
)
2518 enum command_class
class;
2521 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2528 error ("Argument required (%s).", why
);
2533 help_command (command
, from_tty
)
2535 int from_tty
; /* Ignored */
2537 help_cmd (command
, gdb_stdout
);
2541 validate_comname (comname
)
2547 error_no_arg ("name of command to define");
2552 if (!isalnum(*p
) && *p
!= '-')
2553 error ("Junk in argument list: \"%s\"", p
);
2558 /* This is just a placeholder in the command data structures. */
2560 user_defined_command (ignore
, from_tty
)
2567 define_command (comname
, from_tty
)
2571 register struct command_line
*cmds
;
2572 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2573 char *tem
= comname
;
2574 #define HOOK_STRING "hook-"
2577 validate_comname (comname
);
2579 /* Look it up, and verify that we got an exact match. */
2580 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2581 if (c
&& !STREQ (comname
, c
->name
))
2586 if (c
->class == class_user
|| c
->class == class_alias
)
2587 tem
= "Redefine command \"%s\"? ";
2589 tem
= "Really redefine built-in command \"%s\"? ";
2590 if (!query (tem
, c
->name
))
2591 error ("Command \"%s\" not redefined.", c
->name
);
2594 /* If this new command is a hook, then mark the command which it
2595 is hooking. Note that we allow hooking `help' commands, so that
2596 we can hook the `stop' pseudo-command. */
2598 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2600 /* Look up cmd it hooks, and verify that we got an exact match. */
2601 tem
= comname
+HOOK_LEN
;
2602 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2603 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2607 warning ("Your new `%s' command does not hook any existing command.",
2609 if (!query ("Proceed? ", (char *)0))
2610 error ("Not confirmed.");
2614 comname
= savestring (comname
, strlen (comname
));
2616 /* If the rest of the commands will be case insensitive, this one
2617 should behave in the same manner. */
2618 for (tem
= comname
; *tem
; tem
++)
2619 if (isupper(*tem
)) *tem
= tolower(*tem
);
2623 printf_unfiltered ("Type commands for definition of \"%s\".\n\
2624 End with a line saying just \"end\".\n", comname
);
2625 gdb_flush (gdb_stdout
);
2629 cmds
= read_command_lines ();
2631 if (c
&& c
->class == class_user
)
2632 free_command_lines (&c
->user_commands
);
2634 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2635 (c
&& c
->class == class_user
)
2636 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2637 newc
->user_commands
= cmds
;
2639 /* If this new command is a hook, then mark both commands as being
2643 hookc
->hook
= newc
; /* Target gets hooked. */
2644 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2649 document_command (comname
, from_tty
)
2653 struct command_line
*doclines
;
2654 register struct cmd_list_element
*c
;
2655 char *tem
= comname
;
2657 validate_comname (comname
);
2659 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2661 if (c
->class != class_user
)
2662 error ("Command \"%s\" is built-in.", comname
);
2665 printf_unfiltered ("Type documentation for \"%s\".\n\
2666 End with a line saying just \"end\".\n", comname
);
2668 doclines
= read_command_lines ();
2670 if (c
->doc
) free (c
->doc
);
2673 register struct command_line
*cl1
;
2674 register int len
= 0;
2676 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2677 len
+= strlen (cl1
->line
) + 1;
2679 c
->doc
= (char *) xmalloc (len
+ 1);
2682 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2684 strcat (c
->doc
, cl1
->line
);
2686 strcat (c
->doc
, "\n");
2690 free_command_lines (&doclines
);
2694 print_gnu_advertisement ()
2696 printf_unfiltered ("\
2697 GDB is free software and you are welcome to distribute copies of it\n\
2698 under certain conditions; type \"show copying\" to see the conditions.\n\
2699 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2704 print_gdb_version (stream
)
2707 fprintf_filtered (stream
, "\
2708 GDB %s (%s", version
, host_name
);
2710 if (!STREQ (host_name
, target_name
))
2711 fprintf_filtered (stream
, " --target %s", target_name
);
2713 fprintf_filtered (stream
, "), ");
2715 fprintf_filtered (stream
, "Copyright 1994 Free Software Foundation, Inc.");
2720 show_version (args
, from_tty
)
2725 print_gnu_advertisement ();
2726 print_gdb_version (gdb_stdout
);
2727 printf_filtered ("\n");
2731 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2737 printf_unfiltered ("%s", prompt
);
2738 gdb_flush (gdb_stdout
);
2742 quit_command (args
, from_tty
)
2746 if (inferior_pid
!= 0 && target_has_execution
)
2750 if (query ("The program is running. Quit anyway (and detach it)? "))
2751 target_detach (args
, from_tty
);
2753 error ("Not confirmed.");
2757 if (query ("The program is running. Quit anyway (and kill it)? "))
2760 error ("Not confirmed.");
2763 /* UDI wants this, to kill the TIP. */
2766 /* Save the history information if it is appropriate to do so. */
2767 if (write_history_p
&& history_filename
)
2768 write_history (history_filename
);
2773 /* Returns whether GDB is running on a terminal and whether the user
2774 desires that questions be asked of them on that terminal. */
2777 input_from_terminal_p ()
2779 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
2784 pwd_command (args
, from_tty
)
2788 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
2789 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
2791 if (!STREQ (gdb_dirbuf
, current_directory
))
2792 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2793 current_directory
, gdb_dirbuf
);
2795 printf_unfiltered ("Working directory %s.\n", current_directory
);
2799 cd_command (dir
, from_tty
)
2804 /* Found something other than leading repetitions of "/..". */
2805 int found_real_path
;
2808 /* If the new directory is absolute, repeat is a no-op; if relative,
2809 repeat might be useful but is more likely to be a mistake. */
2813 error_no_arg ("new working directory");
2815 dir
= tilde_expand (dir
);
2816 make_cleanup (free
, dir
);
2818 if (chdir (dir
) < 0)
2819 perror_with_name (dir
);
2822 dir
= savestring (dir
, len
- (len
> 1 && dir
[len
-1] == '/'));
2824 current_directory
= dir
;
2827 if (current_directory
[0] == '/' && current_directory
[1] == '\0')
2828 current_directory
= concat (current_directory
, dir
, NULL
);
2830 current_directory
= concat (current_directory
, "/", dir
, NULL
);
2834 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2836 found_real_path
= 0;
2837 for (p
= current_directory
; *p
;)
2839 if (p
[0] == '/' && p
[1] == '.' && (p
[2] == 0 || p
[2] == '/'))
2841 else if (p
[0] == '/' && p
[1] == '.' && p
[2] == '.'
2842 && (p
[3] == 0 || p
[3] == '/'))
2844 if (found_real_path
)
2846 /* Search backwards for the directory just before the "/.."
2847 and obliterate it and the "/..". */
2849 while (q
!= current_directory
&& q
[-1] != '/')
2852 if (q
== current_directory
)
2853 /* current_directory is
2854 a relative pathname ("can't happen"--leave it alone). */
2858 strcpy (q
- 1, p
+ 3);
2863 /* We are dealing with leading repetitions of "/..", for example
2864 "/../..", which is the Mach super-root. */
2869 found_real_path
= 1;
2874 forget_cached_source_info ();
2877 pwd_command ((char *) 0, 1);
2880 struct source_cleanup_lines_args
{
2883 char *old_pre_error
;
2884 char *old_error_pre_print
;
2888 source_cleanup_lines (args
)
2891 struct source_cleanup_lines_args
*p
=
2892 (struct source_cleanup_lines_args
*)args
;
2893 source_line_number
= p
->old_line
;
2894 source_file_name
= p
->old_file
;
2895 source_pre_error
= p
->old_pre_error
;
2896 error_pre_print
= p
->old_error_pre_print
;
2901 source_command (args
, from_tty
)
2906 struct cleanup
*old_cleanups
;
2908 struct source_cleanup_lines_args old_lines
;
2913 error ("source command requires pathname of file to source.");
2916 file
= tilde_expand (file
);
2917 old_cleanups
= make_cleanup (free
, file
);
2919 stream
= fopen (file
, FOPEN_RT
);
2921 perror_with_name (file
);
2923 make_cleanup (fclose
, stream
);
2925 old_lines
.old_line
= source_line_number
;
2926 old_lines
.old_file
= source_file_name
;
2927 old_lines
.old_pre_error
= source_pre_error
;
2928 old_lines
.old_error_pre_print
= error_pre_print
;
2929 make_cleanup (source_cleanup_lines
, &old_lines
);
2930 source_line_number
= 0;
2931 source_file_name
= file
;
2932 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
2933 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
2934 make_cleanup (free
, source_pre_error
);
2935 /* This will get set every time we read a line. So it won't stay "" for
2937 error_pre_print
= "";
2939 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
2940 if (source_error_allocated
< needed_length
)
2942 source_error_allocated
*= 2;
2943 if (source_error_allocated
< needed_length
)
2944 source_error_allocated
= needed_length
;
2945 if (source_error
== NULL
)
2946 source_error
= xmalloc (source_error_allocated
);
2948 source_error
= xrealloc (source_error
, source_error_allocated
);
2951 read_command_file (stream
);
2953 do_cleanups (old_cleanups
);
2958 echo_command (text
, from_tty
)
2966 while ((c
= *p
++) != '\0')
2970 /* \ at end of argument is used after spaces
2971 so they won't be lost. */
2975 c
= parse_escape (&p
);
2977 printf_filtered ("%c", c
);
2980 printf_filtered ("%c", c
);
2983 /* Force this output to appear now. */
2985 gdb_flush (gdb_stdout
);
2989 #ifdef TARGET_BYTE_ORDER_SELECTABLE
2991 /* Functions to manipulate the endianness of the target. */
2993 #ifndef TARGET_BYTE_ORDER_DEFAULT
2994 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
2997 int target_byte_order
= TARGET_BYTE_ORDER_DEFAULT
;
2999 static int target_byte_order_auto
= 1;
3001 /* Called if the user enters ``set endian'' without an argument. */
3003 set_endian (args
, from_tty
)
3007 printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
3008 show_endian (args
, from_tty
);
3011 /* Called by ``set endian big''. */
3013 set_endian_big (args
, from_tty
)
3017 target_byte_order
= BIG_ENDIAN
;
3018 target_byte_order_auto
= 0;
3021 /* Called by ``set endian little''. */
3023 set_endian_little (args
, from_tty
)
3027 target_byte_order
= LITTLE_ENDIAN
;
3028 target_byte_order_auto
= 0;
3031 /* Called by ``set endian auto''. */
3033 set_endian_auto (args
, from_tty
)
3037 target_byte_order_auto
= 1;
3040 /* Called by ``show endian''. */
3042 show_endian (args
, from_tty
)
3047 (target_byte_order_auto
3048 ? "The target endianness is set automatically (currently %s endian)\n"
3049 : "The target is assumed to be %s endian\n");
3050 printf_unfiltered (msg
, TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3053 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3055 /* Set the endianness from a BFD. */
3057 set_endian_from_file (abfd
)
3060 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3063 if (abfd
->xvec
->byteorder_big_p
)
3066 want
= LITTLE_ENDIAN
;
3067 if (target_byte_order_auto
)
3068 target_byte_order
= want
;
3069 else if (target_byte_order
!= want
)
3070 warning ("%s endian file does not match %s endian target.",
3071 want
== BIG_ENDIAN
? "big" : "little",
3072 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3074 #else /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3076 if (abfd
->xvec
->byteorder_big_p
3077 ? TARGET_BYTE_ORDER
!= BIG_ENDIAN
3078 : TARGET_BYTE_ORDER
== BIG_ENDIAN
)
3079 warning ("%s endian file does not match %s endian target.",
3080 abfd
->xvec
->byteorder_big_p
? "big" : "little",
3081 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3083 #endif /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3086 /* Functions to manipulate command line editing control variables. */
3088 /* Number of commands to print in each call to show_commands. */
3089 #define Hist_print 10
3091 show_commands (args
, from_tty
)
3095 /* Index for history commands. Relative to history_base. */
3098 /* Number of the history entry which we are planning to display next.
3099 Relative to history_base. */
3102 /* The first command in the history which doesn't exist (i.e. one more
3103 than the number of the last command). Relative to history_base. */
3106 extern HIST_ENTRY
*history_get
PARAMS ((int));
3108 /* Print out some of the commands from the command history. */
3109 /* First determine the length of the history list. */
3110 hist_len
= history_size
;
3111 for (offset
= 0; offset
< history_size
; offset
++)
3113 if (!history_get (history_base
+ offset
))
3122 if (args
[0] == '+' && args
[1] == '\0')
3123 /* "info editing +" should print from the stored position. */
3126 /* "info editing <exp>" should print around command number <exp>. */
3127 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3129 /* "show commands" means print the last Hist_print commands. */
3132 num
= hist_len
- Hist_print
;
3138 /* If there are at least Hist_print commands, we want to display the last
3139 Hist_print rather than, say, the last 6. */
3140 if (hist_len
- num
< Hist_print
)
3142 num
= hist_len
- Hist_print
;
3147 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3149 printf_filtered ("%5d %s\n", history_base
+ offset
,
3150 (history_get (history_base
+ offset
))->line
);
3153 /* The next command we want to display is the next one that we haven't
3157 /* If the user repeats this command with return, it should do what
3158 "show commands +" does. This is unnecessary if arg is null,
3159 because "show commands +" is not useful after "show commands". */
3160 if (from_tty
&& args
)
3167 /* Called by do_setshow_command. */
3170 set_history_size_command (args
, from_tty
, c
)
3173 struct cmd_list_element
*c
;
3175 if (history_size
== INT_MAX
)
3176 unstifle_history ();
3177 else if (history_size
>= 0)
3178 stifle_history (history_size
);
3181 history_size
= INT_MAX
;
3182 error ("History size must be non-negative");
3188 set_history (args
, from_tty
)
3192 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3193 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3198 show_history (args
, from_tty
)
3202 cmd_show_list (showhistlist
, from_tty
, "");
3205 int info_verbose
= 0; /* Default verbose msgs off */
3207 /* Called by do_setshow_command. An elaborate joke. */
3210 set_verbose (args
, from_tty
, c
)
3213 struct cmd_list_element
*c
;
3215 char *cmdname
= "verbose";
3216 struct cmd_list_element
*showcmd
;
3218 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3222 c
->doc
= "Set verbose printing of informational messages.";
3223 showcmd
->doc
= "Show verbose printing of informational messages.";
3227 c
->doc
= "Set verbosity.";
3228 showcmd
->doc
= "Show verbosity.";
3233 float_handler (signo
)
3236 /* This message is based on ANSI C, section 4.7. Note that integer
3237 divide by zero causes this, so "float" is a misnomer. */
3238 signal (SIGFPE
, float_handler
);
3239 error ("Erroneous arithmetic operation.");
3251 enablebreaklist
= NULL
;
3255 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3259 showhistlist
= NULL
;
3260 unsethistlist
= NULL
;
3261 #if MAINTENANCE_CMDS
3262 maintenancelist
= NULL
;
3263 maintenanceinfolist
= NULL
;
3264 maintenanceprintlist
= NULL
;
3266 setprintlist
= NULL
;
3267 showprintlist
= NULL
;
3268 setchecklist
= NULL
;
3269 showchecklist
= NULL
;
3272 /* Init the history buffer. Note that we are called after the init file(s)
3273 * have been read so that the user can change the history file via his
3274 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3275 * overrides all of this.
3283 tmpenv
= getenv ("HISTSIZE");
3285 history_size
= atoi (tmpenv
);
3286 else if (!history_size
)
3289 stifle_history (history_size
);
3291 tmpenv
= getenv ("GDBHISTFILE");
3293 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3294 else if (!history_filename
) {
3295 /* We include the current directory so that if the user changes
3296 directories the file written will be the same as the one
3298 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3300 read_history (history_filename
);
3306 struct cmd_list_element
*c
;
3308 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3310 add_prefix_cmd ("endian", class_support
, set_endian
,
3311 "Set endianness of target.",
3312 &endianlist
, "set endian ", 0, &setlist
);
3313 add_cmd ("big", class_support
, set_endian_big
,
3314 "Set target as being big endian.", &endianlist
);
3315 add_cmd ("little", class_support
, set_endian_little
,
3316 "Set target as being little endian.", &endianlist
);
3317 add_cmd ("auto", class_support
, set_endian_auto
,
3318 "Select target endianness automatically.", &endianlist
);
3319 add_cmd ("endian", class_support
, show_endian
,
3320 "Show endianness of target.", &showlist
);
3322 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3324 #ifdef DEFAULT_PROMPT
3325 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3327 prompt
= savestring ("(gdb) ", 6);
3330 /* Set the important stuff up for command editing. */
3331 command_editing_p
= 1;
3332 history_expansion_p
= 0;
3333 write_history_p
= 0;
3335 /* Setup important stuff for command line editing. */
3336 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3337 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3338 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3339 rl_readline_name
= "gdb";
3341 /* Define the classes of commands.
3342 They will appear in the help list in the reverse of this order. */
3344 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3345 "Maintenance commands.\n\
3346 Some gdb commands are provided just for use by gdb maintainers.\n\
3347 These commands are subject to frequent change, and may not be as\n\
3348 well documented as user commands.",
3350 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3351 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3352 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3353 The commands in this class are those defined by the user.\n\
3354 Use the \"define\" command to define a command.", &cmdlist
);
3355 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3356 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3357 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3358 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3359 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3360 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3361 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3362 counting from zero for the innermost (currently executing) frame.\n\n\
3363 At any time gdb identifies one frame as the \"selected\" frame.\n\
3364 Variable lookups are done with respect to the selected frame.\n\
3365 When the program being debugged stops, gdb selects the innermost frame.\n\
3366 The commands below can be used to select other frames by number or address.",
3368 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3370 add_com ("pwd", class_files
, pwd_command
,
3371 "Print working directory. This is used for your program as well.");
3372 c
= add_cmd ("cd", class_files
, cd_command
,
3373 "Set working directory to DIR for debugger and program being debugged.\n\
3374 The change does not take effect for the program being debugged\n\
3375 until the next time it is started.", &cmdlist
);
3376 c
->completer
= filename_completer
;
3379 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3384 add_com ("echo", class_support
, echo_command
,
3385 "Print a constant string. Give string as argument.\n\
3386 C escape sequences may be used in the argument.\n\
3387 No newline is added at the end of the argument;\n\
3388 use \"\\n\" if you want a newline to be printed.\n\
3389 Since leading and trailing whitespace are ignored in command arguments,\n\
3390 if you want to print some you must use \"\\\" before leading whitespace\n\
3391 to be printed or after trailing whitespace.");
3392 add_com ("document", class_support
, document_command
,
3393 "Document a user-defined command.\n\
3394 Give command name as argument. Give documentation on following lines.\n\
3395 End with a line of just \"end\".");
3396 add_com ("define", class_support
, define_command
,
3397 "Define a new command name. Command name is argument.\n\
3398 Definition appears on following lines, one command per line.\n\
3399 End with a line of just \"end\".\n\
3400 Use the \"document\" command to give documentation for the new command.\n\
3401 Commands defined in this way may have up to ten arguments.");
3404 c
= add_cmd ("source", class_support
, source_command
,
3405 "Read commands from a file named FILE.\n\
3406 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3407 when gdb is started.", &cmdlist
);
3409 /* Punt file name, we can't help it easily. */
3410 c
= add_cmd ("source", class_support
, source_command
,
3411 "Read commands from a file named FILE.\n\
3412 Note that the file \".gdbinit\" is read automatically in this way\n\
3413 when gdb is started.", &cmdlist
);
3415 c
->completer
= filename_completer
;
3417 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3418 add_com ("help", class_support
, help_command
, "Print list of commands.");
3419 add_com_alias ("q", "quit", class_support
, 1);
3420 add_com_alias ("h", "help", class_support
, 1);
3423 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3426 add_show_from_set (c
, &showlist
);
3427 c
->function
.sfunc
= set_verbose
;
3428 set_verbose (NULL
, 0, c
);
3431 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3432 "Set editing of command lines as they are typed.\n\
3433 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
3434 Without an argument, command line editing is enabled. To edit, use\n\
3435 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3438 add_prefix_cmd ("history", class_support
, set_history
,
3439 "Generic command for setting command history parameters.",
3440 &sethistlist
, "set history ", 0, &setlist
);
3441 add_prefix_cmd ("history", class_support
, show_history
,
3442 "Generic command for showing command history parameters.",
3443 &showhistlist
, "show history ", 0, &showlist
);
3446 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3447 "Set history expansion on command input.\n\
3448 Without an argument, history expansion is enabled.", &sethistlist
),
3452 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3453 "Set saving of the history record on exit.\n\
3454 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
3455 Without an argument, saving is enabled.", &sethistlist
),
3458 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3459 "Set the size of the command history, \n\
3460 ie. the number of previous commands to keep a record of.", &sethistlist
);
3461 add_show_from_set (c
, &showhistlist
);
3462 c
->function
.sfunc
= set_history_size_command
;
3465 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3466 "Set the filename in which to record the command history\n\
3467 (the list of previous commands of which a record is kept).", &sethistlist
),
3471 (add_set_cmd ("confirm", class_support
, var_boolean
,
3473 "Set whether to confirm potentially dangerous operations.",
3477 add_prefix_cmd ("info", class_info
, info_command
,
3478 "Generic command for showing things about the program being debugged.",
3479 &infolist
, "info ", 0, &cmdlist
);
3480 add_com_alias ("i", "info", class_info
, 1);
3482 add_com ("complete", class_obscure
, complete_command
,
3483 "List the completions for the rest of the line as a command.");
3485 add_prefix_cmd ("show", class_info
, show_command
,
3486 "Generic command for showing things about the debugger.",
3487 &showlist
, "show ", 0, &cmdlist
);
3488 /* Another way to get at the same thing. */
3489 add_info ("set", show_command
, "Show all GDB settings.");
3491 add_cmd ("commands", no_class
, show_commands
,
3492 "Show the the history of commands you typed.\n\
3493 You can supply a command number to start with, or a `+' to start after\n\
3494 the previous command number shown.",
3497 add_cmd ("version", no_class
, show_version
,
3498 "Show what version of GDB this is.", &showlist
);
3500 add_com ("while", class_support
, while_command
,
3501 "Execute nested commands WHILE the conditional expression is non zero.\n\
3502 The conditional expression must follow the word `while' and must in turn be\
3503 followed by a new line. The nested commands must be entered one per line,\
3504 and should be terminated by the word `end'.");
3506 add_com ("if", class_support
, if_command
,
3507 "Execute nested commands once IF the conditional expression is non zero.\n\
3508 The conditional expression must follow the word `if' and must in turn be\
3509 followed by a new line. The nested commands must be entered one per line,\
3510 and should be terminated by the word 'else' or `end'. If an else clause\
3511 is used, the same rules apply to its nested commands as to the first ones.");
3513 /* If target is open when baud changes, it doesn't take effect until the
3514 next open (I think, not sure). */
3515 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3516 var_zinteger
, (char *)&baud_rate
,
3517 "Set baud rate for remote serial I/O.\n\
3518 This value is used to set the speed of the serial port when debugging\n\
3519 using remote targets.", &setlist
),
3523 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3524 "Set debugging of remote protocol.\n\
3525 When enabled, each packet sent or received with the remote target\n\
3526 is displayed.", &setlist
),