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 /* An alternate way to read memory for disassembly. This is used to provide a
423 switch that allows disassembly to come from an exec file rather than a
424 remote target. This is a speed hack. */
426 int (*dis_asm_read_memory_hook
) PARAMS ((bfd_vma memaddr
, bfd_byte
*myaddr
,
427 int len
, disassemble_info
*info
));
428 /* Takes control from error (). Typically used to prevent longjmps out of the
429 middle of the GUI. Usually used in conjunction with a catch routine. */
431 NORETURN
void (*error_hook
) PARAMS (());
434 /* Where to go for return_to_top_level (RETURN_ERROR). */
435 jmp_buf error_return
;
436 /* Where to go for return_to_top_level (RETURN_QUIT). */
439 /* Return for reason REASON. This generally gets back to the command
440 loop, but can be caught via catch_errors. */
443 return_to_top_level (reason
)
444 enum return_reason reason
;
449 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
450 I can think of a reason why that is vital, though). */
451 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
453 disable_current_display ();
454 do_cleanups (ALL_CLEANUPS
);
456 if (annotation_level
> 1)
467 (NORETURN
void) longjmp
468 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
471 /* Call FUNC with arg ARGS, catching any errors. If there is no
472 error, return the value returned by FUNC. If there is an error,
473 print ERRSTRING, print the specific error message, then return
476 Must not be called with immediate_quit in effect (bad things might
477 happen, say we got a signal in the middle of a memcpy to quit_return).
478 This is an OK restriction; with very few exceptions immediate_quit can
479 be replaced by judicious use of QUIT.
481 MASK specifies what to catch; it is normally set to
482 RETURN_MASK_ALL, if for no other reason than that the code which
483 calls catch_errors might not be set up to deal with a quit which
484 isn't caught. But if the code can deal with it, it generally
485 should be RETURN_MASK_ERROR, unless for some reason it is more
486 useful to abort only the portion of the operation inside the
487 catch_errors. Note that quit should return to the command line
488 fairly quickly, even if some further processing is being done. */
491 catch_errors (func
, args
, errstring
, mask
)
492 int (*func
) PARAMS ((char *));
501 struct cleanup
*saved_cleanup_chain
;
502 char *saved_error_pre_print
;
504 saved_cleanup_chain
= save_cleanups ();
505 saved_error_pre_print
= error_pre_print
;
507 if (mask
& RETURN_MASK_ERROR
)
508 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (jmp_buf));
509 if (mask
& RETURN_MASK_QUIT
)
510 memcpy (saved_quit
, quit_return
, sizeof (jmp_buf));
511 error_pre_print
= errstring
;
513 if (setjmp (tmp_jmp
) == 0)
515 if (mask
& RETURN_MASK_ERROR
)
516 memcpy (error_return
, tmp_jmp
, sizeof (jmp_buf));
517 if (mask
& RETURN_MASK_QUIT
)
518 memcpy (quit_return
, tmp_jmp
, sizeof (jmp_buf));
519 val
= (*func
) (args
);
524 restore_cleanups (saved_cleanup_chain
);
526 error_pre_print
= saved_error_pre_print
;
527 if (mask
& RETURN_MASK_ERROR
)
528 memcpy (error_return
, saved_error
, sizeof (jmp_buf));
529 if (mask
& RETURN_MASK_QUIT
)
530 memcpy (quit_return
, saved_quit
, sizeof (jmp_buf));
534 /* Handler for SIGHUP. */
540 catch_errors (quit_cover
, NULL
,
541 "Could not kill the program being debugged", RETURN_MASK_ALL
);
542 signal (SIGHUP
, SIG_DFL
);
543 kill (getpid (), SIGHUP
);
546 /* Just a little helper function for disconnect(). */
552 caution
= 0; /* Throw caution to the wind -- we're exiting.
553 This prevents asking the user dumb questions. */
554 quit_command((char *)0, 0);
558 /* Line number we are currently in in a file which is being sourced. */
559 static int source_line_number
;
561 /* Name of the file we are sourcing. */
562 static char *source_file_name
;
564 /* Buffer containing the error_pre_print used by the source stuff.
566 static char *source_error
;
567 static int source_error_allocated
;
569 /* Something to glom on to the start of error_pre_print if source_file_name
571 static char *source_pre_error
;
573 /* Clean up on error during a "source" command (or execution of a
574 user-defined command). */
577 source_cleanup (stream
)
580 /* Restore the previous input stream. */
584 /* Read commands from STREAM. */
586 read_command_file (stream
)
589 struct cleanup
*cleanups
;
591 cleanups
= make_cleanup (source_cleanup
, instream
);
594 do_cleanups (cleanups
);
597 extern void init_proc ();
599 void (*pre_init_ui_hook
) PARAMS ((void));
604 if (pre_init_ui_hook
)
607 /* Run the init function of each source file */
609 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
610 current_directory
= gdb_dirbuf
;
612 init_cmd_lists (); /* This needs to be done first */
613 initialize_targets (); /* Setup target_terminal macros for utils.c */
614 initialize_utils (); /* Make errors and warnings possible */
615 initialize_all_files ();
616 init_main (); /* But that omits this file! Do it now */
621 /* We need a default language for parsing expressions, so simple things like
622 "set width 0" won't fail if no language is explicitly set in a config file
623 or implicitly set by reading an executable during startup. */
624 set_language (language_c
);
625 expected_language
= current_language
; /* don't warn about the change. */
631 /* Allocate, initialize a new command line structure for one of the
632 control commands (if/while). */
634 static struct command_line
*
635 build_command_line (type
, args
)
636 enum command_control_type type
;
639 struct command_line
*cmd
;
641 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
643 cmd
->control_type
= type
;
647 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
649 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
650 cmd
->line
= savestring (args
, strlen (args
));
654 /* Build and return a new command structure for the control commands
655 such as "if" and "while". */
657 static struct command_line
*
658 get_command_line (type
, arg
)
659 enum command_control_type type
;
662 struct command_line
*cmd
;
663 struct cleanup
*old_chain
= NULL
;
665 /* Allocate and build a new command line structure. */
666 cmd
= build_command_line (type
, arg
);
668 old_chain
= make_cleanup (free_command_lines
, &cmd
);
670 /* Read in the body of this command. */
671 if (recurse_read_control_structure (cmd
) == invalid_control
)
673 warning ("error reading in control structure\n");
674 do_cleanups (old_chain
);
678 discard_cleanups (old_chain
);
682 /* Recursively print a command (including full control structures). */
684 print_command_line (cmd
, depth
)
685 struct command_line
*cmd
;
692 for (i
= 0; i
< depth
; i
++)
693 fputs_filtered (" ", gdb_stdout
);
696 /* A simple command, print it and return. */
697 if (cmd
->control_type
== simple_control
)
699 fputs_filtered (cmd
->line
, gdb_stdout
);
700 fputs_filtered ("\n", gdb_stdout
);
704 /* loop_continue to jump to the start of a while loop, print it
706 if (cmd
->control_type
== continue_control
)
708 fputs_filtered ("loop_continue\n", gdb_stdout
);
712 /* loop_break to break out of a while loop, print it and return. */
713 if (cmd
->control_type
== break_control
)
715 fputs_filtered ("loop_break\n", gdb_stdout
);
719 /* A while command. Recursively print its subcommands before returning. */
720 if (cmd
->control_type
== while_control
)
722 struct command_line
*list
;
723 fputs_filtered ("while ", gdb_stdout
);
724 fputs_filtered (cmd
->line
, gdb_stdout
);
725 fputs_filtered ("\n", gdb_stdout
);
726 list
= *cmd
->body_list
;
729 print_command_line (list
, depth
+ 1);
734 /* An if command. Recursively print both arms before returning. */
735 if (cmd
->control_type
== if_control
)
737 fputs_filtered ("if ", gdb_stdout
);
738 fputs_filtered (cmd
->line
, gdb_stdout
);
739 fputs_filtered ("\n", gdb_stdout
);
741 print_command_line (cmd
->body_list
[0], depth
+ 1);
743 /* Show the false arm if it exists. */
744 if (cmd
->body_count
== 2)
748 for (i
= 0; i
< depth
; i
++)
749 fputs_filtered (" ", gdb_stdout
);
751 fputs_filtered ("else\n", gdb_stdout
);
752 print_command_line (cmd
->body_list
[1], depth
+ 1);
756 for (i
= 0; i
< depth
; i
++)
757 fputs_filtered (" ", gdb_stdout
);
759 fputs_filtered ("end\n", gdb_stdout
);
763 /* Execute the command in CMD. */
765 enum command_control_type
766 execute_control_command (cmd
)
767 struct command_line
*cmd
;
769 struct expression
*expr
;
770 struct command_line
*current
;
771 struct cleanup
*old_chain
= 0;
774 enum command_control_type ret
;
777 switch (cmd
->control_type
)
780 /* A simple command, execute it and return. */
781 new_line
= insert_args (cmd
->line
);
783 return invalid_control
;
784 old_chain
= make_cleanup (free_current_contents
, &new_line
);
785 execute_command (new_line
, 0);
786 ret
= cmd
->control_type
;
789 case continue_control
:
791 /* Return for "continue", and "break" so we can either
792 continue the loop at the top, or break out. */
793 ret
= cmd
->control_type
;
798 /* Parse the loop control expression for the while statement. */
799 new_line
= insert_args (cmd
->line
);
801 return invalid_control
;
802 old_chain
= make_cleanup (free_current_contents
, &new_line
);
803 expr
= parse_expression (new_line
);
804 make_cleanup (free_current_contents
, &expr
);
806 ret
= simple_control
;
809 /* Keep iterating so long as the expression is true. */
812 /* Evaluate the expression. */
813 val
= evaluate_expression (expr
);
815 /* If the value is false, then break out of the loop. */
816 if (!value_true (val
))
819 /* Execute the body of the while statement. */
820 current
= *cmd
->body_list
;
823 ret
= execute_control_command (current
);
825 /* If we got an error, or a "break" command, then stop
827 if (ret
== invalid_control
|| ret
== break_control
)
833 /* If we got a "continue" command, then restart the loop
835 if (ret
== continue_control
)
838 /* Get the next statement. */
839 current
= current
->next
;
843 /* Reset RET so that we don't recurse the break all the way down. */
844 if (ret
== break_control
)
845 ret
= simple_control
;
852 new_line
= insert_args (cmd
->line
);
854 return invalid_control
;
855 old_chain
= make_cleanup (free_current_contents
, &new_line
);
856 /* Parse the conditional for the if statement. */
857 expr
= parse_expression (new_line
);
858 make_cleanup (free_current_contents
, &expr
);
861 ret
= simple_control
;
863 /* Evaluate the conditional. */
864 val
= evaluate_expression (expr
);
866 /* Choose which arm to take commands from based on the value of the
867 conditional expression. */
868 if (value_true (val
))
869 current
= *cmd
->body_list
;
870 else if (cmd
->body_count
== 2)
871 current
= *(cmd
->body_list
+ 1);
873 /* Execute commands in the given arm. */
876 ret
= execute_control_command (current
);
878 /* If we got an error, get out. */
879 if (ret
!= simple_control
)
882 /* Get the next statement in the body. */
883 current
= current
->next
;
890 warning ("Invalid control type in command structure.");
891 return invalid_control
;
895 do_cleanups (old_chain
);
900 /* "while" command support. Executes a body of statements while the
901 loop condition is nonzero. */
904 while_command (arg
, from_tty
)
908 struct command_line
*command
= NULL
;
911 command
= get_command_line (while_control
, arg
);
916 execute_control_command (command
);
917 free_command_lines (&command
);
920 /* "if" command support. Execute either the true or false arm depending
921 on the value of the if conditional. */
924 if_command (arg
, from_tty
)
928 struct command_line
*command
= NULL
;
931 command
= get_command_line (if_control
, arg
);
936 execute_control_command (command
);
937 free_command_lines (&command
);
944 struct user_args
*oargs
= user_args
;
946 fatal ("Internal error, arg_cleanup called with no user args.\n");
948 user_args
= user_args
->next
;
952 /* Bind the incomming arguments for a user defined command to
953 $arg0, $arg1 ... $argMAXUSERARGS. */
955 static struct cleanup
*
959 struct user_args
*args
;
960 struct cleanup
*old_chain
;
961 unsigned int arg_count
= 0;
963 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
964 memset (args
, 0, sizeof (struct user_args
));
966 args
->next
= user_args
;
969 old_chain
= make_cleanup (arg_cleanup
, 0);
978 if (arg_count
>= MAXUSERARGS
)
980 error ("user defined function may only have %d arguments.\n",
985 /* Strip whitespace. */
986 while (*p
== ' ' || *p
== '\t')
989 /* P now points to an argument. */
991 user_args
->a
[arg_count
].arg
= p
;
993 /* Get to the end of this argument. */
994 while (*p
&& *p
!= ' ' && *p
!= '\t')
997 user_args
->a
[arg_count
].len
= p
- start_arg
;
1004 /* Given character string P, return a point to the first argument ($arg),
1005 or NULL if P contains no arguments. */
1011 while ((p
= strchr (p
, '$')))
1013 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1020 /* Insert the user defined arguments stored in user_arg into the $arg
1021 arguments found in line, with the updated copy being placed into nline. */
1027 char *p
, *save_line
, *new_line
;
1030 /* First we need to know how much memory to allocate for the new line. */
1033 while ((p
= locate_arg (line
)))
1038 if (i
>= user_args
->count
)
1040 error ("Missing argument %d in user function.\n", i
);
1043 len
+= user_args
->a
[i
].len
;
1047 /* Don't forget the tail. */
1048 len
+= strlen (line
);
1050 /* Allocate space for the new line and fill it in. */
1051 new_line
= (char *)xmalloc (len
+ 1);
1052 if (new_line
== NULL
)
1055 /* Restore pointer to beginning of old line. */
1058 /* Save pointer to beginning of new line. */
1059 save_line
= new_line
;
1061 while ((p
= locate_arg (line
)))
1065 memcpy (new_line
, line
, p
- line
);
1066 new_line
+= p
- line
;
1069 len
= user_args
->a
[i
].len
;
1072 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1077 /* Don't forget the tail. */
1078 strcpy (new_line
, line
);
1080 /* Return a pointer to the beginning of the new line. */
1085 execute_user_command (c
, args
)
1086 struct cmd_list_element
*c
;
1089 register struct command_line
*cmdlines
;
1090 struct cleanup
*old_chain
;
1091 enum command_control_type ret
;
1093 old_chain
= setup_user_args (args
);
1095 cmdlines
= c
->user_commands
;
1100 /* Set the instream to 0, indicating execution of a
1101 user-defined function. */
1102 old_chain
= make_cleanup (source_cleanup
, instream
);
1103 instream
= (FILE *) 0;
1106 ret
= execute_control_command (cmdlines
);
1107 if (ret
!= simple_control
&& ret
!= break_control
)
1109 warning ("Error in control structure.\n");
1112 cmdlines
= cmdlines
->next
;
1114 do_cleanups (old_chain
);
1117 /* Execute the line P as a command.
1118 Pass FROM_TTY as second argument to the defining function. */
1121 execute_command (p
, from_tty
)
1125 register struct cmd_list_element
*c
;
1126 register enum language flang
;
1127 static int warned
= 0;
1131 /* This can happen when command_line_input hits end of file. */
1135 while (*p
== ' ' || *p
== '\t') p
++;
1140 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1141 /* Pass null arg rather than an empty one. */
1144 /* If this command has been hooked, run the hook first. */
1146 execute_user_command (c
->hook
, (char *)0);
1148 if (c
->class == class_user
)
1149 execute_user_command (c
, arg
);
1150 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1151 do_setshow_command (arg
, from_tty
& caution
, c
);
1152 else if (c
->function
.cfunc
== NO_FUNCTION
)
1153 error ("That is not a command, just a help topic.");
1154 else if (call_command_hook
)
1155 call_command_hook (c
, arg
, from_tty
& caution
);
1157 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1160 /* Tell the user if the language has changed (except first time). */
1161 if (current_language
!= expected_language
)
1163 if (language_mode
== language_mode_auto
) {
1164 language_info (1); /* Print what changed. */
1169 /* Warn the user if the working language does not match the
1170 language of the current frame. Only warn the user if we are
1171 actually running the program, i.e. there is a stack. */
1172 /* FIXME: This should be cacheing the frame and only running when
1173 the frame changes. */
1175 if (target_has_stack
)
1177 flang
= get_frame_language ();
1179 && flang
!= language_unknown
1180 && flang
!= current_language
->la_language
)
1182 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1190 command_loop_marker (foo
)
1195 /* Read commands from `instream' and execute them
1196 until end of file or error reading instream. */
1201 struct cleanup
*old_chain
;
1203 int stdin_is_tty
= ISATTY (stdin
);
1204 long time_at_cmd_start
;
1205 long space_at_cmd_start
;
1206 extern int display_time
;
1207 extern int display_space
;
1209 while (!feof (instream
))
1211 if (window_hook
&& instream
== stdin
)
1212 (*window_hook
) (instream
, prompt
);
1215 if (instream
== stdin
&& stdin_is_tty
)
1216 reinitialize_more_filter ();
1217 old_chain
= make_cleanup (command_loop_marker
, 0);
1218 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1219 instream
== stdin
, "prompt");
1223 time_at_cmd_start
= get_run_time ();
1227 extern char **environ
;
1228 char *lim
= (char *) sbrk (0);
1230 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1233 execute_command (command
, instream
== stdin
);
1234 /* Do any commands attached to breakpoint we stopped at. */
1235 bpstat_do_actions (&stop_bpstat
);
1236 do_cleanups (old_chain
);
1240 long cmd_time
= get_run_time () - time_at_cmd_start
;
1242 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1243 cmd_time
/ 1000000, cmd_time
% 1000000);
1248 extern char **environ
;
1249 char *lim
= (char *) sbrk (0);
1250 long space_now
= lim
- (char *) &environ
;
1251 long space_diff
= space_now
- space_at_cmd_start
;
1253 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1255 (space_diff
>= 0 ? '+' : '-'),
1261 /* Commands call this if they do not want to be repeated by null lines. */
1269 /* If we aren't reading from standard input, we are saving the last
1270 thing read from stdin in line and don't want to delete it. Null lines
1271 won't repeat here in any case. */
1272 if (instream
== stdin
)
1276 /* Read a line from the stream "instream" without command line editing.
1278 It prints PRROMPT once at the start.
1279 Action is compatible with "readline", e.g. space for the result is
1280 malloc'd and should be freed by the caller.
1282 A NULL return means end of file. */
1284 gdb_readline (prrompt
)
1289 int input_index
= 0;
1290 int result_size
= 80;
1294 /* Don't use a _filtered function here. It causes the assumed
1295 character position to be off, since the newline we read from
1296 the user is not accounted for. */
1297 fputs_unfiltered (prrompt
, gdb_stdout
);
1298 /* start-sanitize-mpw */
1300 /* Move to a new line so the entered line doesn't have a prompt
1301 on the front of it. */
1302 fputs_unfiltered ("\n", gdb_stdout
);
1304 /* end-sanitize-mpw */
1305 gdb_flush (gdb_stdout
);
1308 result
= (char *) xmalloc (result_size
);
1312 /* Read from stdin if we are executing a user defined command.
1313 This is the right thing for prompt_for_continue, at least. */
1314 c
= fgetc (instream
? instream
: stdin
);
1318 if (input_index
> 0)
1319 /* The last line does not end with a newline. Return it, and
1320 if we are called again fgetc will still return EOF and
1321 we'll return NULL then. */
1330 result
[input_index
++] = c
;
1331 while (input_index
>= result_size
)
1334 result
= (char *) xrealloc (result
, result_size
);
1338 result
[input_index
++] = '\0';
1342 /* Variables which control command line editing and history
1343 substitution. These variables are given default values at the end
1345 static int command_editing_p
;
1346 static int history_expansion_p
;
1347 static int write_history_p
;
1348 static int history_size
;
1349 static char *history_filename
;
1351 /* readline uses the word breaks for two things:
1352 (1) In figuring out where to point the TEXT parameter to the
1353 rl_completion_entry_function. Since we don't use TEXT for much,
1354 it doesn't matter a lot what the word breaks are for this purpose, but
1355 it does affect how much stuff M-? lists.
1356 (2) If one of the matches contains a word break character, readline
1357 will quote it. That's why we switch between
1358 gdb_completer_word_break_characters and
1359 gdb_completer_command_word_break_characters. I'm not sure when
1360 we need this behavior (perhaps for funky characters in C++ symbols?). */
1362 /* Variables which are necessary for fancy command line editing. */
1363 char *gdb_completer_word_break_characters
=
1364 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1366 /* When completing on command names, we remove '-' from the list of
1367 word break characters, since we use it in command names. If the
1368 readline library sees one in any of the current completion strings,
1369 it thinks that the string needs to be quoted and automatically supplies
1371 char *gdb_completer_command_word_break_characters
=
1372 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1374 /* Characters that can be used to quote completion strings. Note that we
1375 can't include '"' because the gdb C parser treats such quoted sequences
1377 char *gdb_completer_quote_characters
=
1380 /* Functions that are used as part of the fancy command line editing. */
1382 /* This can be used for functions which don't want to complete on symbols
1383 but don't want to complete on anything else either. */
1386 noop_completer (text
, prefix
)
1393 /* Complete on filenames. */
1395 filename_completer (text
, word
)
1399 /* From readline. */
1400 extern char *filename_completion_function ();
1401 int subsequent_name
;
1403 int return_val_used
;
1404 int return_val_alloced
;
1406 return_val_used
= 0;
1407 /* Small for testing. */
1408 return_val_alloced
= 1;
1409 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1411 subsequent_name
= 0;
1415 p
= filename_completion_function (text
, subsequent_name
);
1416 if (return_val_used
>= return_val_alloced
)
1418 return_val_alloced
*= 2;
1420 (char **) xrealloc (return_val
,
1421 return_val_alloced
* sizeof (char *));
1425 return_val
[return_val_used
++] = p
;
1428 /* Like emacs, don't complete on old versions. Especially useful
1429 in the "source" command. */
1430 if (p
[strlen (p
) - 1] == '~')
1436 /* Return exactly p. */
1437 return_val
[return_val_used
++] = p
;
1438 else if (word
> text
)
1440 /* Return some portion of p. */
1441 q
= xmalloc (strlen (p
) + 5);
1442 strcpy (q
, p
+ (word
- text
));
1443 return_val
[return_val_used
++] = q
;
1448 /* Return some of TEXT plus p. */
1449 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1450 strncpy (q
, word
, text
- word
);
1451 q
[text
- word
] = '\0';
1453 return_val
[return_val_used
++] = q
;
1457 subsequent_name
= 1;
1460 /* There is no way to do this just long enough to affect quote inserting
1461 without also affecting the next completion. This should be fixed in
1463 /* Insure that readline does the right thing
1464 with respect to inserting quotes. */
1465 rl_completer_word_break_characters
= "";
1470 /* Here are some useful test cases for completion. FIXME: These should
1471 be put in the test suite. They should be tested with both M-? and TAB.
1473 "show output-" "radix"
1474 "show output" "-radix"
1475 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1476 "p " ambiguous (all symbols)
1477 "info t foo" no completions
1478 "info t " no completions
1479 "info t" ambiguous ("info target", "info terminal", etc.)
1480 "info ajksdlfk" no completions
1481 "info ajksdlfk " no completions
1483 "info " ambiguous (all info commands)
1484 "p \"a" no completions (string constant)
1485 "p 'a" ambiguous (all symbols starting with a)
1486 "p b-a" ambiguous (all symbols starting with a)
1487 "p b-" ambiguous (all symbols)
1488 "file Make" "file" (word break hard to screw up here)
1489 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1492 /* Generate completions one by one for the completer. Each time we are
1493 called return another potential completion to the caller.
1494 line_completion just completes on commands or passes the buck to the
1495 command's completer function, the stuff specific to symbol completion
1496 is in make_symbol_completion_list.
1498 TEXT is the caller's idea of the "word" we are looking at.
1500 MATCHES is the number of matches that have currently been collected from
1501 calling this completion function. When zero, then we need to initialize,
1502 otherwise the initialization has already taken place and we can just
1503 return the next potential completion string.
1505 LINE_BUFFER is available to be looked at; it contains the entire text
1506 of the line. POINT is the offset in that line of the cursor. You
1507 should pretend that the line ends at POINT.
1509 Returns NULL if there are no more completions, else a pointer to a string
1510 which is a possible completion, it is the caller's responsibility to
1514 line_completion_function (text
, matches
, line_buffer
, point
)
1520 static char **list
= (char **)NULL
; /* Cache of completions */
1521 static int index
; /* Next cached completion */
1522 char *output
= NULL
;
1523 char *tmp_command
, *p
;
1524 /* Pointer within tmp_command which corresponds to text. */
1526 struct cmd_list_element
*c
, *result_list
;
1530 /* The caller is beginning to accumulate a new set of completions, so
1531 we need to find all of them now, and cache them for returning one at
1532 a time on future calls. */
1536 /* Free the storage used by LIST, but not by the strings inside.
1537 This is because rl_complete_internal () frees the strings. */
1543 /* Choose the default set of word break characters to break completions.
1544 If we later find out that we are doing completions on command strings
1545 (as opposed to strings supplied by the individual command completer
1546 functions, which can be any string) then we will switch to the
1547 special word break set for command strings, which leaves out the
1548 '-' character used in some commands. */
1550 rl_completer_word_break_characters
=
1551 gdb_completer_word_break_characters
;
1553 /* Decide whether to complete on a list of gdb commands or on symbols. */
1554 tmp_command
= (char *) alloca (point
+ 1);
1557 strncpy (tmp_command
, line_buffer
, point
);
1558 tmp_command
[point
] = '\0';
1559 /* Since text always contains some number of characters leading up
1560 to point, we can find the equivalent position in tmp_command
1561 by subtracting that many characters from the end of tmp_command. */
1562 word
= tmp_command
+ point
- strlen (text
);
1566 /* An empty line we want to consider ambiguous; that is, it
1567 could be any command. */
1568 c
= (struct cmd_list_element
*) -1;
1573 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1576 /* Move p up to the next interesting thing. */
1577 while (*p
== ' ' || *p
== '\t')
1584 /* It is an unrecognized command. So there are no
1585 possible completions. */
1588 else if (c
== (struct cmd_list_element
*) -1)
1592 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1593 doesn't advance over that thing itself. Do so now. */
1595 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1597 if (q
!= tmp_command
+ point
)
1599 /* There is something beyond the ambiguous
1600 command, so there are no possible completions. For
1601 example, "info t " or "info t foo" does not complete
1602 to anything, because "info t" can be "info target" or
1608 /* We're trying to complete on the command which was ambiguous.
1609 This we can deal with. */
1612 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1617 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1619 /* Insure that readline does the right thing with respect to
1620 inserting quotes. */
1621 rl_completer_word_break_characters
=
1622 gdb_completer_command_word_break_characters
;
1627 /* We've recognized a full command. */
1629 if (p
== tmp_command
+ point
)
1631 /* There is no non-whitespace in the line beyond the command. */
1633 if (p
[-1] == ' ' || p
[-1] == '\t')
1635 /* The command is followed by whitespace; we need to complete
1636 on whatever comes after command. */
1639 /* It is a prefix command; what comes after it is
1640 a subcommand (e.g. "info "). */
1641 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1643 /* Insure that readline does the right thing
1644 with respect to inserting quotes. */
1645 rl_completer_word_break_characters
=
1646 gdb_completer_command_word_break_characters
;
1650 /* It is a normal command; what comes after it is
1651 completed by the command's completer function. */
1652 list
= (*c
->completer
) (p
, word
);
1657 /* The command is not followed by whitespace; we need to
1658 complete on the command itself. e.g. "p" which is a
1659 command itself but also can complete to "print", "ptype"
1663 /* Find the command we are completing on. */
1665 while (q
> tmp_command
)
1667 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1673 list
= complete_on_cmdlist (result_list
, q
, word
);
1675 /* Insure that readline does the right thing
1676 with respect to inserting quotes. */
1677 rl_completer_word_break_characters
=
1678 gdb_completer_command_word_break_characters
;
1683 /* There is non-whitespace beyond the command. */
1685 if (c
->prefixlist
&& !c
->allow_unknown
)
1687 /* It is an unrecognized subcommand of a prefix command,
1688 e.g. "info adsfkdj". */
1693 /* It is a normal command. */
1694 list
= (*c
->completer
) (p
, word
);
1700 /* If we found a list of potential completions during initialization then
1701 dole them out one at a time. The vector of completions is NULL
1702 terminated, so after returning the last one, return NULL (and continue
1703 to do so) each time we are called after that, until a new list is
1708 output
= list
[index
];
1716 /* Can't do this because readline hasn't yet checked the word breaks
1717 for figuring out whether to insert a quote. */
1719 /* Make sure the word break characters are set back to normal for the
1720 next time that readline tries to complete something. */
1721 rl_completer_word_break_characters
=
1722 gdb_completer_word_break_characters
;
1728 /* Line completion interface function for readline. */
1731 readline_line_completion_function (text
, matches
)
1735 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1738 /* Skip over a possibly quoted word (as defined by the quote characters
1739 and word break characters the completer uses). Returns pointer to the
1740 location after the "word". */
1746 char quote_char
= '\0';
1749 for (scan
= str
; *scan
!= '\0'; scan
++)
1751 if (quote_char
!= '\0')
1753 /* Ignore everything until the matching close quote char */
1754 if (*scan
== quote_char
)
1756 /* Found matching close quote. */
1761 else if (strchr (gdb_completer_quote_characters
, *scan
))
1763 /* Found start of a quoted string. */
1766 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1780 #if STOP_SIGNAL == SIGTSTP
1781 signal (SIGTSTP
, SIG_DFL
);
1783 kill (getpid (), SIGTSTP
);
1784 signal (SIGTSTP
, stop_sig
);
1786 signal (STOP_SIGNAL
, stop_sig
);
1788 printf_unfiltered ("%s", prompt
);
1789 gdb_flush (gdb_stdout
);
1791 /* Forget about any previous command -- null line now will do nothing. */
1794 #endif /* STOP_SIGNAL */
1796 /* Initialize signal handlers. */
1806 signal (SIGINT
, request_quit
);
1808 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1809 passed to the inferior, which we don't want. It would be
1810 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1811 on BSD4.3 systems using vfork, that can affect the
1812 GDB process as well as the inferior (the signal handling tables
1813 might be in memory, shared between the two). Since we establish
1814 a handler for SIGQUIT, when we call exec it will set the signal
1815 to SIG_DFL for us. */
1816 signal (SIGQUIT
, do_nothing
);
1817 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1818 signal (SIGHUP
, disconnect
);
1819 signal (SIGFPE
, float_handler
);
1821 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1822 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1826 /* Read one line from the command input stream `instream'
1827 into the local static buffer `linebuffer' (whose current length
1829 The buffer is made bigger as necessary.
1830 Returns the address of the start of the line.
1832 NULL is returned for end of file.
1834 *If* the instream == stdin & stdin is a terminal, the line read
1835 is copied into the file line saver (global var char *line,
1836 length linesize) so that it can be duplicated.
1838 This routine either uses fancy command line editing or
1839 simple input as the user has requested. */
1842 command_line_input (prrompt
, repeat
, annotation_suffix
)
1845 char *annotation_suffix
;
1847 static char *linebuffer
= 0;
1848 static unsigned linelength
= 0;
1852 char *local_prompt
= prrompt
;
1857 /* The annotation suffix must be non-NULL. */
1858 if (annotation_suffix
== NULL
)
1859 annotation_suffix
= "";
1861 if (annotation_level
> 1 && instream
== stdin
)
1863 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
1864 + strlen (annotation_suffix
) + 40);
1865 if (prrompt
== NULL
)
1866 local_prompt
[0] = '\0';
1868 strcpy (local_prompt
, prrompt
);
1869 strcat (local_prompt
, "\n\032\032");
1870 strcat (local_prompt
, annotation_suffix
);
1871 strcat (local_prompt
, "\n");
1874 if (linebuffer
== 0)
1877 linebuffer
= (char *) xmalloc (linelength
);
1882 /* Control-C quits instantly if typed while in this loop
1883 since it should not wait until the user types a newline. */
1887 signal (STOP_SIGNAL
, stop_sig
);
1892 /* Make sure that all output has been output. Some machines may let
1893 you get away with leaving out some of the gdb_flush, but not all. */
1895 gdb_flush (gdb_stdout
);
1896 gdb_flush (gdb_stderr
);
1898 if (source_file_name
!= NULL
)
1900 ++source_line_number
;
1901 sprintf (source_error
,
1902 "%s%s:%d: Error in sourced command file:\n",
1905 source_line_number
);
1906 error_pre_print
= source_error
;
1909 if (annotation_level
> 1 && instream
== stdin
)
1911 printf_unfiltered ("\n\032\032pre-");
1912 printf_unfiltered (annotation_suffix
);
1913 printf_unfiltered ("\n");
1916 /* Don't use fancy stuff if not talking to stdin. */
1917 if (command_editing_p
&& instream
== stdin
1918 && ISATTY (instream
))
1919 rl
= readline (local_prompt
);
1921 rl
= gdb_readline (local_prompt
);
1923 if (annotation_level
> 1 && instream
== stdin
)
1925 printf_unfiltered ("\n\032\032post-");
1926 printf_unfiltered (annotation_suffix
);
1927 printf_unfiltered ("\n");
1930 if (!rl
|| rl
== (char *) EOF
)
1935 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
1937 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
1938 nline
= (char *) xrealloc (linebuffer
, linelength
);
1939 p
+= nline
- linebuffer
;
1943 /* Copy line. Don't copy null at end. (Leaves line alone
1944 if this was just a newline) */
1948 free (rl
); /* Allocated in readline. */
1950 if (p
== linebuffer
|| *(p
- 1) != '\\')
1953 p
--; /* Put on top of '\'. */
1954 local_prompt
= (char *) 0;
1959 signal (STOP_SIGNAL
, SIG_DFL
);
1966 #define SERVER_COMMAND_LENGTH 7
1968 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
1969 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
1972 /* Note that we don't set `line'. Between this and the check in
1973 dont_repeat, this insures that repeating will still do the
1976 return linebuffer
+ SERVER_COMMAND_LENGTH
;
1979 /* Do history expansion if that is wished. */
1980 if (history_expansion_p
&& instream
== stdin
1981 && ISATTY (instream
))
1983 char *history_value
;
1986 *p
= '\0'; /* Insert null now. */
1987 expanded
= history_expand (linebuffer
, &history_value
);
1990 /* Print the changes. */
1991 printf_unfiltered ("%s\n", history_value
);
1993 /* If there was an error, call this function again. */
1996 free (history_value
);
1997 return command_line_input (prrompt
, repeat
, annotation_suffix
);
1999 if (strlen (history_value
) > linelength
)
2001 linelength
= strlen (history_value
) + 1;
2002 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2004 strcpy (linebuffer
, history_value
);
2005 p
= linebuffer
+ strlen(linebuffer
);
2006 free (history_value
);
2010 /* If we just got an empty line, and that is supposed
2011 to repeat the previous command, return the value in the
2015 if (p
== linebuffer
)
2018 while (*p1
== ' ' || *p1
== '\t')
2026 /* Add line to history if appropriate. */
2027 if (instream
== stdin
2028 && ISATTY (stdin
) && *linebuffer
)
2029 add_history (linebuffer
);
2031 /* Note: lines consisting solely of comments are added to the command
2032 history. This is useful when you type a command, and then
2033 realize you don't want to execute it quite yet. You can comment
2034 out the command and then later fetch it from the value history
2035 and remove the '#'. The kill ring is probably better, but some
2036 people are in the habit of commenting things out. */
2038 while ((c
= *p1
++) != '\0')
2041 while ((c
= *p1
++) != '"')
2043 /* Make sure an escaped '"' doesn't make us think the string
2051 while ((c
= *p1
++) != '\'')
2053 /* Make sure an escaped '\'' doesn't make us think the string
2062 /* Found a comment. */
2068 /* Save into global buffer if appropriate. */
2071 if (linelength
> linesize
)
2073 line
= xrealloc (line
, linelength
);
2074 linesize
= linelength
;
2076 strcpy (line
, linebuffer
);
2084 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2085 code bodies. This is typically used when we encounter an "else"
2086 clause for an "if" command. */
2089 realloc_body_list (command
, new_length
)
2090 struct command_line
*command
;
2094 struct command_line
**body_list
;
2096 n
= command
->body_count
;
2098 /* Nothing to do? */
2099 if (new_length
<= n
)
2102 body_list
= (struct command_line
**)
2103 xmalloc (sizeof (struct command_line
*) * new_length
);
2105 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2107 free (command
->body_list
);
2108 command
->body_list
= body_list
;
2109 command
->body_count
= new_length
;
2112 /* Read one line from the input stream. If the command is an "else" or
2113 "end", return such an indication to the caller. */
2115 static enum misc_command_type
2116 read_next_line (command
)
2117 struct command_line
**command
;
2119 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2122 if (control_level
>= 254)
2123 error ("Control nesting too deep!\n");
2125 /* Set a prompt based on the nesting of the control commands. */
2126 if (instream
== stdin
)
2128 for (i
= 0; i
< control_level
; i
++)
2129 control_prompt
[i
] = ' ';
2130 control_prompt
[i
] = '>';
2131 control_prompt
[i
+1] = '\0';
2132 prompt_ptr
= (char *)&control_prompt
[0];
2137 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2139 /* Not sure what to do here. */
2143 /* Strip leading and trailing whitespace. */
2144 while (*p
== ' ' || *p
== '\t')
2147 p1
= p
+ strlen (p
);
2148 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2151 /* Blanks and comments don't really do anything, but we need to
2152 distinguish them from else, end and other commands which can be
2154 if (p1
== p
|| p
[0] == '#')
2157 /* Is this the end of a simple, while, or if control structure? */
2158 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2161 /* Is the else clause of an if control structure? */
2162 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2163 return else_command
;
2165 /* Check for while, if, break, continue, etc and build a new command
2166 line structure for them. */
2167 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2168 *command
= build_command_line (while_control
, p
+ 6);
2169 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2170 *command
= build_command_line (if_control
, p
+ 3);
2171 else if (p1
- p
== 5 && !strncmp (p
, "loop_break", 5))
2173 *command
= (struct command_line
*)
2174 xmalloc (sizeof (struct command_line
));
2175 (*command
)->next
= NULL
;
2176 (*command
)->line
= NULL
;
2177 (*command
)->control_type
= break_control
;
2178 (*command
)->body_count
= 0;
2179 (*command
)->body_list
= NULL
;
2181 else if (p1
- p
== 8 && !strncmp (p
, "loop_continue", 8))
2183 *command
= (struct command_line
*)
2184 xmalloc (sizeof (struct command_line
));
2185 (*command
)->next
= NULL
;
2186 (*command
)->line
= NULL
;
2187 (*command
)->control_type
= continue_control
;
2188 (*command
)->body_count
= 0;
2189 (*command
)->body_list
= NULL
;
2193 /* A normal command. */
2194 *command
= (struct command_line
*)
2195 xmalloc (sizeof (struct command_line
));
2196 (*command
)->next
= NULL
;
2197 (*command
)->line
= savestring (p
, p1
- p
);
2198 (*command
)->control_type
= simple_control
;
2199 (*command
)->body_count
= 0;
2200 (*command
)->body_list
= NULL
;
2203 /* Nothing special. */
2207 /* Recursively read in the control structures and create a command_line
2210 The parent_control parameter is the control structure in which the
2211 following commands are nested. */
2213 static enum command_control_type
2214 recurse_read_control_structure (current_cmd
)
2215 struct command_line
*current_cmd
;
2217 int current_body
, i
;
2218 enum misc_command_type val
;
2219 enum command_control_type ret
;
2220 struct command_line
**body_ptr
, *child_tail
, *next
;
2221 struct cleanup
*old_chains
, *tmp_chains
;
2227 /* Sanity checks. */
2228 if (current_cmd
->control_type
== simple_control
)
2230 error ("Recursed on a simple control type\n");
2231 return invalid_control
;
2234 if (current_body
> current_cmd
->body_count
)
2236 error ("Allocated body is smaller than this command type needs\n");
2237 return invalid_control
;
2240 /* Read lines from the input stream and build control structures. */
2246 val
= read_next_line (&next
);
2248 /* Just skip blanks and comments. */
2249 if (val
== nop_command
)
2252 if (val
== end_command
)
2254 if (current_cmd
->control_type
== while_control
2255 || current_cmd
->control_type
== if_control
)
2257 /* Success reading an entire control structure. */
2258 ret
= simple_control
;
2263 ret
= invalid_control
;
2268 /* Not the end of a control structure. */
2269 if (val
== else_command
)
2271 if (current_cmd
->control_type
== if_control
2272 && current_body
== 1)
2274 realloc_body_list (current_cmd
, 2);
2281 ret
= invalid_control
;
2288 child_tail
->next
= next
;
2292 /* We have just read the first line of the child's control
2293 structure. From now on, arrange to throw away the line
2294 we have if we quit or get an error. */
2295 body_ptr
= current_cmd
->body_list
;
2296 for (i
= 1; i
< current_body
; i
++)
2301 tmp_chains
= make_cleanup (free_command_lines
, body_ptr
);
2304 old_chains
= tmp_chains
;
2309 /* If the latest line is another control structure, then recurse
2311 if (next
->control_type
== while_control
2312 || next
->control_type
== if_control
)
2315 ret
= recurse_read_control_structure (next
);
2318 if (ret
!= simple_control
)
2324 if (ret
== invalid_control
&& old_chains
)
2325 do_cleanups (old_chains
);
2326 else if (old_chains
)
2327 discard_cleanups (old_chains
);
2333 /* Read lines from the input stream
2334 and accumulate them in a chain of struct command_line's
2335 which is then returned. */
2337 struct command_line
*
2338 read_command_lines ()
2340 struct command_line
*head
, *tail
, *next
;
2341 struct cleanup
*old_chain
;
2342 enum command_control_type ret
;
2343 enum misc_command_type val
;
2350 val
= read_next_line (&next
);
2352 /* Ignore blank lines or comments. */
2353 if (val
== nop_command
)
2356 if (val
== end_command
)
2358 ret
= simple_control
;
2362 if (val
!= ok_command
)
2364 ret
= invalid_control
;
2368 if (next
->control_type
== while_control
2369 || next
->control_type
== if_control
)
2372 ret
= recurse_read_control_structure (next
);
2375 if (ret
== invalid_control
)
2386 old_chain
= make_cleanup (free_command_lines
, &head
);
2395 if (ret
!= invalid_control
)
2397 discard_cleanups (old_chain
);
2401 do_cleanups (old_chain
);
2407 /* Free a chain of struct command_line's. */
2410 free_command_lines (lptr
)
2411 struct command_line
**lptr
;
2413 register struct command_line
*l
= *lptr
;
2414 register struct command_line
*next
;
2415 struct command_line
**blist
;
2420 if (l
->body_count
> 0)
2422 blist
= l
->body_list
;
2423 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2424 free_command_lines (blist
);
2433 /* Add an element to the list of info subcommands. */
2436 add_info (name
, fun
, doc
)
2438 void (*fun
) PARAMS ((char *, int));
2441 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2444 /* Add an alias to the list of info subcommands. */
2447 add_info_alias (name
, oldname
, abbrev_flag
)
2452 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2455 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2456 Therefore, its own definition is called only for "info" with no args. */
2460 info_command (arg
, from_tty
)
2464 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2465 help_list (infolist
, "info ", -1, gdb_stdout
);
2468 /* The "complete" command is used by Emacs to implement completion. */
2472 complete_command (arg
, from_tty
)
2484 argpoint
= strlen (arg
);
2486 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2488 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2490 printf_unfiltered ("%s\n", completion
);
2495 /* The "show" command with no arguments shows all the settings. */
2499 show_command (arg
, from_tty
)
2503 cmd_show_list (showlist
, from_tty
, "");
2506 /* Add an element to the list of commands. */
2509 add_com (name
, class, fun
, doc
)
2511 enum command_class
class;
2512 void (*fun
) PARAMS ((char *, int));
2515 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2518 /* Add an alias or abbreviation command to the list of commands. */
2521 add_com_alias (name
, oldname
, class, abbrev_flag
)
2524 enum command_class
class;
2527 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2534 error ("Argument required (%s).", why
);
2539 help_command (command
, from_tty
)
2541 int from_tty
; /* Ignored */
2543 help_cmd (command
, gdb_stdout
);
2547 validate_comname (comname
)
2553 error_no_arg ("name of command to define");
2558 if (!isalnum(*p
) && *p
!= '-')
2559 error ("Junk in argument list: \"%s\"", p
);
2564 /* This is just a placeholder in the command data structures. */
2566 user_defined_command (ignore
, from_tty
)
2573 define_command (comname
, from_tty
)
2577 register struct command_line
*cmds
;
2578 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2579 char *tem
= comname
;
2580 #define HOOK_STRING "hook-"
2583 validate_comname (comname
);
2585 /* Look it up, and verify that we got an exact match. */
2586 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2587 if (c
&& !STREQ (comname
, c
->name
))
2592 if (c
->class == class_user
|| c
->class == class_alias
)
2593 tem
= "Redefine command \"%s\"? ";
2595 tem
= "Really redefine built-in command \"%s\"? ";
2596 if (!query (tem
, c
->name
))
2597 error ("Command \"%s\" not redefined.", c
->name
);
2600 /* If this new command is a hook, then mark the command which it
2601 is hooking. Note that we allow hooking `help' commands, so that
2602 we can hook the `stop' pseudo-command. */
2604 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2606 /* Look up cmd it hooks, and verify that we got an exact match. */
2607 tem
= comname
+HOOK_LEN
;
2608 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2609 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2613 warning ("Your new `%s' command does not hook any existing command.",
2615 if (!query ("Proceed? ", (char *)0))
2616 error ("Not confirmed.");
2620 comname
= savestring (comname
, strlen (comname
));
2622 /* If the rest of the commands will be case insensitive, this one
2623 should behave in the same manner. */
2624 for (tem
= comname
; *tem
; tem
++)
2625 if (isupper(*tem
)) *tem
= tolower(*tem
);
2629 printf_unfiltered ("Type commands for definition of \"%s\".\n\
2630 End with a line saying just \"end\".\n", comname
);
2631 gdb_flush (gdb_stdout
);
2635 cmds
= read_command_lines ();
2637 if (c
&& c
->class == class_user
)
2638 free_command_lines (&c
->user_commands
);
2640 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2641 (c
&& c
->class == class_user
)
2642 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2643 newc
->user_commands
= cmds
;
2645 /* If this new command is a hook, then mark both commands as being
2649 hookc
->hook
= newc
; /* Target gets hooked. */
2650 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2655 document_command (comname
, from_tty
)
2659 struct command_line
*doclines
;
2660 register struct cmd_list_element
*c
;
2661 char *tem
= comname
;
2663 validate_comname (comname
);
2665 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2667 if (c
->class != class_user
)
2668 error ("Command \"%s\" is built-in.", comname
);
2671 printf_unfiltered ("Type documentation for \"%s\".\n\
2672 End with a line saying just \"end\".\n", comname
);
2674 doclines
= read_command_lines ();
2676 if (c
->doc
) free (c
->doc
);
2679 register struct command_line
*cl1
;
2680 register int len
= 0;
2682 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2683 len
+= strlen (cl1
->line
) + 1;
2685 c
->doc
= (char *) xmalloc (len
+ 1);
2688 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2690 strcat (c
->doc
, cl1
->line
);
2692 strcat (c
->doc
, "\n");
2696 free_command_lines (&doclines
);
2700 print_gnu_advertisement ()
2702 printf_unfiltered ("\
2703 GDB is free software and you are welcome to distribute copies of it\n\
2704 under certain conditions; type \"show copying\" to see the conditions.\n\
2705 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2710 print_gdb_version (stream
)
2713 fprintf_filtered (stream
, "\
2714 GDB %s (%s", version
, host_name
);
2716 if (!STREQ (host_name
, target_name
))
2717 fprintf_filtered (stream
, " --target %s", target_name
);
2719 fprintf_filtered (stream
, "), ");
2721 fprintf_filtered (stream
, "Copyright 1994 Free Software Foundation, Inc.");
2726 show_version (args
, from_tty
)
2731 print_gnu_advertisement ();
2732 print_gdb_version (gdb_stdout
);
2733 printf_filtered ("\n");
2737 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2743 printf_unfiltered ("%s", prompt
);
2744 gdb_flush (gdb_stdout
);
2748 quit_command (args
, from_tty
)
2752 if (inferior_pid
!= 0 && target_has_execution
)
2756 if (query ("The program is running. Quit anyway (and detach it)? "))
2757 target_detach (args
, from_tty
);
2759 error ("Not confirmed.");
2763 if (query ("The program is running. Quit anyway (and kill it)? "))
2766 error ("Not confirmed.");
2769 /* UDI wants this, to kill the TIP. */
2772 /* Save the history information if it is appropriate to do so. */
2773 if (write_history_p
&& history_filename
)
2774 write_history (history_filename
);
2779 /* Returns whether GDB is running on a terminal and whether the user
2780 desires that questions be asked of them on that terminal. */
2783 input_from_terminal_p ()
2785 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
2790 pwd_command (args
, from_tty
)
2794 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
2795 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
2797 if (!STREQ (gdb_dirbuf
, current_directory
))
2798 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2799 current_directory
, gdb_dirbuf
);
2801 printf_unfiltered ("Working directory %s.\n", current_directory
);
2805 cd_command (dir
, from_tty
)
2810 /* Found something other than leading repetitions of "/..". */
2811 int found_real_path
;
2814 /* If the new directory is absolute, repeat is a no-op; if relative,
2815 repeat might be useful but is more likely to be a mistake. */
2819 error_no_arg ("new working directory");
2821 dir
= tilde_expand (dir
);
2822 make_cleanup (free
, dir
);
2824 if (chdir (dir
) < 0)
2825 perror_with_name (dir
);
2828 dir
= savestring (dir
, len
- (len
> 1 && dir
[len
-1] == '/'));
2830 current_directory
= dir
;
2833 if (current_directory
[0] == '/' && current_directory
[1] == '\0')
2834 current_directory
= concat (current_directory
, dir
, NULL
);
2836 current_directory
= concat (current_directory
, "/", dir
, NULL
);
2840 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2842 found_real_path
= 0;
2843 for (p
= current_directory
; *p
;)
2845 if (p
[0] == '/' && p
[1] == '.' && (p
[2] == 0 || p
[2] == '/'))
2847 else if (p
[0] == '/' && p
[1] == '.' && p
[2] == '.'
2848 && (p
[3] == 0 || p
[3] == '/'))
2850 if (found_real_path
)
2852 /* Search backwards for the directory just before the "/.."
2853 and obliterate it and the "/..". */
2855 while (q
!= current_directory
&& q
[-1] != '/')
2858 if (q
== current_directory
)
2859 /* current_directory is
2860 a relative pathname ("can't happen"--leave it alone). */
2864 strcpy (q
- 1, p
+ 3);
2869 /* We are dealing with leading repetitions of "/..", for example
2870 "/../..", which is the Mach super-root. */
2875 found_real_path
= 1;
2880 forget_cached_source_info ();
2883 pwd_command ((char *) 0, 1);
2886 struct source_cleanup_lines_args
{
2889 char *old_pre_error
;
2890 char *old_error_pre_print
;
2894 source_cleanup_lines (args
)
2897 struct source_cleanup_lines_args
*p
=
2898 (struct source_cleanup_lines_args
*)args
;
2899 source_line_number
= p
->old_line
;
2900 source_file_name
= p
->old_file
;
2901 source_pre_error
= p
->old_pre_error
;
2902 error_pre_print
= p
->old_error_pre_print
;
2907 source_command (args
, from_tty
)
2912 struct cleanup
*old_cleanups
;
2914 struct source_cleanup_lines_args old_lines
;
2919 error ("source command requires pathname of file to source.");
2922 file
= tilde_expand (file
);
2923 old_cleanups
= make_cleanup (free
, file
);
2925 stream
= fopen (file
, FOPEN_RT
);
2927 perror_with_name (file
);
2929 make_cleanup (fclose
, stream
);
2931 old_lines
.old_line
= source_line_number
;
2932 old_lines
.old_file
= source_file_name
;
2933 old_lines
.old_pre_error
= source_pre_error
;
2934 old_lines
.old_error_pre_print
= error_pre_print
;
2935 make_cleanup (source_cleanup_lines
, &old_lines
);
2936 source_line_number
= 0;
2937 source_file_name
= file
;
2938 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
2939 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
2940 make_cleanup (free
, source_pre_error
);
2941 /* This will get set every time we read a line. So it won't stay "" for
2943 error_pre_print
= "";
2945 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
2946 if (source_error_allocated
< needed_length
)
2948 source_error_allocated
*= 2;
2949 if (source_error_allocated
< needed_length
)
2950 source_error_allocated
= needed_length
;
2951 if (source_error
== NULL
)
2952 source_error
= xmalloc (source_error_allocated
);
2954 source_error
= xrealloc (source_error
, source_error_allocated
);
2957 read_command_file (stream
);
2959 do_cleanups (old_cleanups
);
2964 echo_command (text
, from_tty
)
2972 while ((c
= *p
++) != '\0')
2976 /* \ at end of argument is used after spaces
2977 so they won't be lost. */
2981 c
= parse_escape (&p
);
2983 printf_filtered ("%c", c
);
2986 printf_filtered ("%c", c
);
2989 /* Force this output to appear now. */
2991 gdb_flush (gdb_stdout
);
2995 #ifdef TARGET_BYTE_ORDER_SELECTABLE
2997 /* Functions to manipulate the endianness of the target. */
2999 #ifndef TARGET_BYTE_ORDER_DEFAULT
3000 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
3003 int target_byte_order
= TARGET_BYTE_ORDER_DEFAULT
;
3005 static int target_byte_order_auto
= 1;
3007 /* Called if the user enters ``set endian'' without an argument. */
3009 set_endian (args
, from_tty
)
3013 printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
3014 show_endian (args
, from_tty
);
3017 /* Called by ``set endian big''. */
3019 set_endian_big (args
, from_tty
)
3023 target_byte_order
= BIG_ENDIAN
;
3024 target_byte_order_auto
= 0;
3027 /* Called by ``set endian little''. */
3029 set_endian_little (args
, from_tty
)
3033 target_byte_order
= LITTLE_ENDIAN
;
3034 target_byte_order_auto
= 0;
3037 /* Called by ``set endian auto''. */
3039 set_endian_auto (args
, from_tty
)
3043 target_byte_order_auto
= 1;
3046 /* Called by ``show endian''. */
3048 show_endian (args
, from_tty
)
3053 (target_byte_order_auto
3054 ? "The target endianness is set automatically (currently %s endian)\n"
3055 : "The target is assumed to be %s endian\n");
3056 printf_unfiltered (msg
, TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3059 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3061 /* Set the endianness from a BFD. */
3063 set_endian_from_file (abfd
)
3066 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3069 if (abfd
->xvec
->byteorder_big_p
)
3072 want
= LITTLE_ENDIAN
;
3073 if (target_byte_order_auto
)
3074 target_byte_order
= want
;
3075 else if (target_byte_order
!= want
)
3076 warning ("%s endian file does not match %s endian target.",
3077 want
== BIG_ENDIAN
? "big" : "little",
3078 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3080 #else /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3082 if (abfd
->xvec
->byteorder_big_p
3083 ? TARGET_BYTE_ORDER
!= BIG_ENDIAN
3084 : TARGET_BYTE_ORDER
== BIG_ENDIAN
)
3085 warning ("%s endian file does not match %s endian target.",
3086 abfd
->xvec
->byteorder_big_p
? "big" : "little",
3087 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3089 #endif /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3092 /* Functions to manipulate command line editing control variables. */
3094 /* Number of commands to print in each call to show_commands. */
3095 #define Hist_print 10
3097 show_commands (args
, from_tty
)
3101 /* Index for history commands. Relative to history_base. */
3104 /* Number of the history entry which we are planning to display next.
3105 Relative to history_base. */
3108 /* The first command in the history which doesn't exist (i.e. one more
3109 than the number of the last command). Relative to history_base. */
3112 extern HIST_ENTRY
*history_get
PARAMS ((int));
3114 /* Print out some of the commands from the command history. */
3115 /* First determine the length of the history list. */
3116 hist_len
= history_size
;
3117 for (offset
= 0; offset
< history_size
; offset
++)
3119 if (!history_get (history_base
+ offset
))
3128 if (args
[0] == '+' && args
[1] == '\0')
3129 /* "info editing +" should print from the stored position. */
3132 /* "info editing <exp>" should print around command number <exp>. */
3133 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3135 /* "show commands" means print the last Hist_print commands. */
3138 num
= hist_len
- Hist_print
;
3144 /* If there are at least Hist_print commands, we want to display the last
3145 Hist_print rather than, say, the last 6. */
3146 if (hist_len
- num
< Hist_print
)
3148 num
= hist_len
- Hist_print
;
3153 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3155 printf_filtered ("%5d %s\n", history_base
+ offset
,
3156 (history_get (history_base
+ offset
))->line
);
3159 /* The next command we want to display is the next one that we haven't
3163 /* If the user repeats this command with return, it should do what
3164 "show commands +" does. This is unnecessary if arg is null,
3165 because "show commands +" is not useful after "show commands". */
3166 if (from_tty
&& args
)
3173 /* Called by do_setshow_command. */
3176 set_history_size_command (args
, from_tty
, c
)
3179 struct cmd_list_element
*c
;
3181 if (history_size
== INT_MAX
)
3182 unstifle_history ();
3183 else if (history_size
>= 0)
3184 stifle_history (history_size
);
3187 history_size
= INT_MAX
;
3188 error ("History size must be non-negative");
3194 set_history (args
, from_tty
)
3198 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3199 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3204 show_history (args
, from_tty
)
3208 cmd_show_list (showhistlist
, from_tty
, "");
3211 int info_verbose
= 0; /* Default verbose msgs off */
3213 /* Called by do_setshow_command. An elaborate joke. */
3216 set_verbose (args
, from_tty
, c
)
3219 struct cmd_list_element
*c
;
3221 char *cmdname
= "verbose";
3222 struct cmd_list_element
*showcmd
;
3224 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3228 c
->doc
= "Set verbose printing of informational messages.";
3229 showcmd
->doc
= "Show verbose printing of informational messages.";
3233 c
->doc
= "Set verbosity.";
3234 showcmd
->doc
= "Show verbosity.";
3239 float_handler (signo
)
3242 /* This message is based on ANSI C, section 4.7. Note that integer
3243 divide by zero causes this, so "float" is a misnomer. */
3244 signal (SIGFPE
, float_handler
);
3245 error ("Erroneous arithmetic operation.");
3257 enablebreaklist
= NULL
;
3261 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3265 showhistlist
= NULL
;
3266 unsethistlist
= NULL
;
3267 #if MAINTENANCE_CMDS
3268 maintenancelist
= NULL
;
3269 maintenanceinfolist
= NULL
;
3270 maintenanceprintlist
= NULL
;
3272 setprintlist
= NULL
;
3273 showprintlist
= NULL
;
3274 setchecklist
= NULL
;
3275 showchecklist
= NULL
;
3278 /* Init the history buffer. Note that we are called after the init file(s)
3279 * have been read so that the user can change the history file via his
3280 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3281 * overrides all of this.
3289 tmpenv
= getenv ("HISTSIZE");
3291 history_size
= atoi (tmpenv
);
3292 else if (!history_size
)
3295 stifle_history (history_size
);
3297 tmpenv
= getenv ("GDBHISTFILE");
3299 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3300 else if (!history_filename
) {
3301 /* We include the current directory so that if the user changes
3302 directories the file written will be the same as the one
3304 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3306 read_history (history_filename
);
3312 struct cmd_list_element
*c
;
3314 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3316 add_prefix_cmd ("endian", class_support
, set_endian
,
3317 "Set endianness of target.",
3318 &endianlist
, "set endian ", 0, &setlist
);
3319 add_cmd ("big", class_support
, set_endian_big
,
3320 "Set target as being big endian.", &endianlist
);
3321 add_cmd ("little", class_support
, set_endian_little
,
3322 "Set target as being little endian.", &endianlist
);
3323 add_cmd ("auto", class_support
, set_endian_auto
,
3324 "Select target endianness automatically.", &endianlist
);
3325 add_cmd ("endian", class_support
, show_endian
,
3326 "Show endianness of target.", &showlist
);
3328 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3330 #ifdef DEFAULT_PROMPT
3331 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3333 prompt
= savestring ("(gdb) ", 6);
3336 /* Set the important stuff up for command editing. */
3337 command_editing_p
= 1;
3338 history_expansion_p
= 0;
3339 write_history_p
= 0;
3341 /* Setup important stuff for command line editing. */
3342 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3343 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3344 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3345 rl_readline_name
= "gdb";
3347 /* Define the classes of commands.
3348 They will appear in the help list in the reverse of this order. */
3350 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3351 "Maintenance commands.\n\
3352 Some gdb commands are provided just for use by gdb maintainers.\n\
3353 These commands are subject to frequent change, and may not be as\n\
3354 well documented as user commands.",
3356 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3357 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3358 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3359 The commands in this class are those defined by the user.\n\
3360 Use the \"define\" command to define a command.", &cmdlist
);
3361 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3362 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3363 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3364 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3365 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3366 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3367 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3368 counting from zero for the innermost (currently executing) frame.\n\n\
3369 At any time gdb identifies one frame as the \"selected\" frame.\n\
3370 Variable lookups are done with respect to the selected frame.\n\
3371 When the program being debugged stops, gdb selects the innermost frame.\n\
3372 The commands below can be used to select other frames by number or address.",
3374 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3376 add_com ("pwd", class_files
, pwd_command
,
3377 "Print working directory. This is used for your program as well.");
3378 c
= add_cmd ("cd", class_files
, cd_command
,
3379 "Set working directory to DIR for debugger and program being debugged.\n\
3380 The change does not take effect for the program being debugged\n\
3381 until the next time it is started.", &cmdlist
);
3382 c
->completer
= filename_completer
;
3385 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3390 add_com ("echo", class_support
, echo_command
,
3391 "Print a constant string. Give string as argument.\n\
3392 C escape sequences may be used in the argument.\n\
3393 No newline is added at the end of the argument;\n\
3394 use \"\\n\" if you want a newline to be printed.\n\
3395 Since leading and trailing whitespace are ignored in command arguments,\n\
3396 if you want to print some you must use \"\\\" before leading whitespace\n\
3397 to be printed or after trailing whitespace.");
3398 add_com ("document", class_support
, document_command
,
3399 "Document a user-defined command.\n\
3400 Give command name as argument. Give documentation on following lines.\n\
3401 End with a line of just \"end\".");
3402 add_com ("define", class_support
, define_command
,
3403 "Define a new command name. Command name is argument.\n\
3404 Definition appears on following lines, one command per line.\n\
3405 End with a line of just \"end\".\n\
3406 Use the \"document\" command to give documentation for the new command.\n\
3407 Commands defined in this way may have up to ten arguments.");
3410 c
= add_cmd ("source", class_support
, source_command
,
3411 "Read commands from a file named FILE.\n\
3412 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3413 when gdb is started.", &cmdlist
);
3415 /* Punt file name, we can't help it easily. */
3416 c
= add_cmd ("source", class_support
, source_command
,
3417 "Read commands from a file named FILE.\n\
3418 Note that the file \".gdbinit\" is read automatically in this way\n\
3419 when gdb is started.", &cmdlist
);
3421 c
->completer
= filename_completer
;
3423 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3424 add_com ("help", class_support
, help_command
, "Print list of commands.");
3425 add_com_alias ("q", "quit", class_support
, 1);
3426 add_com_alias ("h", "help", class_support
, 1);
3429 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3432 add_show_from_set (c
, &showlist
);
3433 c
->function
.sfunc
= set_verbose
;
3434 set_verbose (NULL
, 0, c
);
3437 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3438 "Set editing of command lines as they are typed.\n\
3439 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
3440 Without an argument, command line editing is enabled. To edit, use\n\
3441 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3444 add_prefix_cmd ("history", class_support
, set_history
,
3445 "Generic command for setting command history parameters.",
3446 &sethistlist
, "set history ", 0, &setlist
);
3447 add_prefix_cmd ("history", class_support
, show_history
,
3448 "Generic command for showing command history parameters.",
3449 &showhistlist
, "show history ", 0, &showlist
);
3452 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3453 "Set history expansion on command input.\n\
3454 Without an argument, history expansion is enabled.", &sethistlist
),
3458 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3459 "Set saving of the history record on exit.\n\
3460 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
3461 Without an argument, saving is enabled.", &sethistlist
),
3464 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3465 "Set the size of the command history, \n\
3466 ie. the number of previous commands to keep a record of.", &sethistlist
);
3467 add_show_from_set (c
, &showhistlist
);
3468 c
->function
.sfunc
= set_history_size_command
;
3471 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3472 "Set the filename in which to record the command history\n\
3473 (the list of previous commands of which a record is kept).", &sethistlist
),
3477 (add_set_cmd ("confirm", class_support
, var_boolean
,
3479 "Set whether to confirm potentially dangerous operations.",
3483 add_prefix_cmd ("info", class_info
, info_command
,
3484 "Generic command for showing things about the program being debugged.",
3485 &infolist
, "info ", 0, &cmdlist
);
3486 add_com_alias ("i", "info", class_info
, 1);
3488 add_com ("complete", class_obscure
, complete_command
,
3489 "List the completions for the rest of the line as a command.");
3491 add_prefix_cmd ("show", class_info
, show_command
,
3492 "Generic command for showing things about the debugger.",
3493 &showlist
, "show ", 0, &cmdlist
);
3494 /* Another way to get at the same thing. */
3495 add_info ("set", show_command
, "Show all GDB settings.");
3497 add_cmd ("commands", no_class
, show_commands
,
3498 "Show the the history of commands you typed.\n\
3499 You can supply a command number to start with, or a `+' to start after\n\
3500 the previous command number shown.",
3503 add_cmd ("version", no_class
, show_version
,
3504 "Show what version of GDB this is.", &showlist
);
3506 add_com ("while", class_support
, while_command
,
3507 "Execute nested commands WHILE the conditional expression is non zero.\n\
3508 The conditional expression must follow the word `while' 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 `end'.");
3512 add_com ("if", class_support
, if_command
,
3513 "Execute nested commands once IF the conditional expression is non zero.\n\
3514 The conditional expression must follow the word `if' and must in turn be\
3515 followed by a new line. The nested commands must be entered one per line,\
3516 and should be terminated by the word 'else' or `end'. If an else clause\
3517 is used, the same rules apply to its nested commands as to the first ones.");
3519 /* If target is open when baud changes, it doesn't take effect until the
3520 next open (I think, not sure). */
3521 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3522 var_zinteger
, (char *)&baud_rate
,
3523 "Set baud rate for remote serial I/O.\n\
3524 This value is used to set the speed of the serial port when debugging\n\
3525 using remote targets.", &setlist
),
3529 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3530 "Set debugging of remote protocol.\n\
3531 When enabled, each packet sent or received with the remote target\n\
3532 is displayed.", &setlist
),