1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
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 /* Prototypes for local functions */
61 static char * line_completion_function
PARAMS ((char *, int, char *, int));
63 static char * readline_line_completion_function
PARAMS ((char *, int));
65 static void command_loop_marker
PARAMS ((int));
67 static void while_command
PARAMS ((char *, int));
69 static void if_command
PARAMS ((char *, int));
71 static struct command_line
*
72 build_command_line
PARAMS ((enum command_control_type
, char *));
74 static struct command_line
*
75 get_command_line
PARAMS ((enum command_control_type
, char *));
77 static void realloc_body_list
PARAMS ((struct command_line
*, int));
79 static enum misc_command_type read_next_line
PARAMS ((struct command_line
**));
81 static enum command_control_type
82 recurse_read_control_structure
PARAMS ((struct command_line
*));
84 static struct cleanup
* setup_user_args
PARAMS ((char *));
86 static char * locate_arg
PARAMS ((char *));
88 static char * insert_args
PARAMS ((char *));
90 static void arg_cleanup
PARAMS ((void));
92 static void init_main
PARAMS ((void));
94 static void init_cmd_lists
PARAMS ((void));
96 static void float_handler
PARAMS ((int));
98 static void init_signals
PARAMS ((void));
100 static void set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
102 #ifdef TARGET_BYTE_ORDER_SELECTABLE
104 static void set_endian
PARAMS ((char *, int));
106 static void set_endian_big
PARAMS ((char *, int));
108 static void set_endian_little
PARAMS ((char *, int));
110 static void set_endian_auto
PARAMS ((char *, int));
112 static void show_endian
PARAMS ((char *, int));
116 static void show_history
PARAMS ((char *, int));
118 static void set_history
PARAMS ((char *, int));
120 static void set_history_size_command
PARAMS ((char *, int,
121 struct cmd_list_element
*));
123 static void show_commands
PARAMS ((char *, int));
125 static void echo_command
PARAMS ((char *, int));
127 static void pwd_command
PARAMS ((char *, int));
129 static void show_version
PARAMS ((char *, int));
131 static void document_command
PARAMS ((char *, int));
133 static void define_command
PARAMS ((char *, int));
135 static void validate_comname
PARAMS ((char *));
137 static void help_command
PARAMS ((char *, int));
139 static void show_command
PARAMS ((char *, int));
141 static void info_command
PARAMS ((char *, int));
143 static void complete_command
PARAMS ((char *, int));
145 static void do_nothing
PARAMS ((int));
147 static int quit_cover
PARAMS ((char *));
149 static void disconnect
PARAMS ((int));
151 static void source_cleanup
PARAMS ((FILE *));
153 /* If this definition isn't overridden by the header files, assume
154 that isatty and fileno exist on this system. */
156 #define ISATTY(FP) (isatty (fileno (FP)))
159 /* Initialization file name for gdb. This is overridden in some configs. */
161 #ifndef GDBINIT_FILENAME
162 #define GDBINIT_FILENAME ".gdbinit"
164 char gdbinit
[] = GDBINIT_FILENAME
;
165 int inhibit_gdbinit
= 0;
167 /* Disable windows if non-zero */
169 int use_windows
= 1; /* Defaults to on for now */
171 /* Version number of GDB, as a string. */
173 extern char *version
;
175 /* Canonical host name as a string. */
177 extern char *host_name
;
179 /* Canonical target name as a string. */
181 extern char *target_name
;
183 extern char lang_frame_mismatch_warn
[]; /* language.c */
185 /* Flag for whether we want all the "from_tty" gubbish printed. */
187 int caution
= 1; /* Default is yes, sigh. */
190 * Define all cmd_list_element's
193 /* Chain containing all defined commands. */
195 struct cmd_list_element
*cmdlist
;
197 /* Chain containing all defined info subcommands. */
199 struct cmd_list_element
*infolist
;
201 /* Chain containing all defined enable subcommands. */
203 struct cmd_list_element
*enablelist
;
205 /* Chain containing all defined disable subcommands. */
207 struct cmd_list_element
*disablelist
;
209 /* Chain containing all defined delete subcommands. */
211 struct cmd_list_element
*deletelist
;
213 /* Chain containing all defined "enable breakpoint" subcommands. */
215 struct cmd_list_element
*enablebreaklist
;
217 /* Chain containing all defined set subcommands */
219 struct cmd_list_element
*setlist
;
221 /* Chain containing all defined unset subcommands */
223 struct cmd_list_element
*unsetlist
;
225 /* Chain containing all defined show subcommands. */
227 struct cmd_list_element
*showlist
;
229 #ifdef TARGET_BYTE_ORDER_SELECTABLE
230 /* Chain containing the \"set endian\" commands. */
232 struct cmd_list_element
*endianlist
;
235 /* Chain containing all defined \"set history\". */
237 struct cmd_list_element
*sethistlist
;
239 /* Chain containing all defined \"show history\". */
241 struct cmd_list_element
*showhistlist
;
243 /* Chain containing all defined \"unset history\". */
245 struct cmd_list_element
*unsethistlist
;
247 /* Chain containing all defined maintenance subcommands. */
250 struct cmd_list_element
*maintenancelist
;
253 /* Chain containing all defined "maintenance info" subcommands. */
256 struct cmd_list_element
*maintenanceinfolist
;
259 /* Chain containing all defined "maintenance print" subcommands. */
262 struct cmd_list_element
*maintenanceprintlist
;
265 struct cmd_list_element
*setprintlist
;
267 struct cmd_list_element
*showprintlist
;
269 struct cmd_list_element
*setchecklist
;
271 struct cmd_list_element
*showchecklist
;
273 /* stdio stream that command input is being read from. Set to stdin normally.
274 Set by source_command to the file we are sourcing. Set to NULL if we are
275 executing a user-defined command. */
279 /* Current working directory. */
281 char *current_directory
;
283 /* The directory name is actually stored here (usually). */
284 char gdb_dirbuf
[1024];
286 /* Function to call before reading a command, if nonzero.
287 The function receives two args: an input stream,
288 and a prompt string. */
290 void (*window_hook
) PARAMS ((FILE *, char *));
295 /* gdb prints this when reading a command interactively */
298 /* Buffer used for reading command lines, and the size
299 allocated for it so far. */
304 /* Nonzero if the current command is modified by "server ". This
305 affects things like recording into the command history, comamnds
306 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
307 whatever) can issue its own commands and also send along commands
308 from the user, and have the user not notice that the user interface
309 is issuing commands too. */
312 /* Baud rate specified for talking to serial target systems. Default
313 is left as -1, so targets can choose their own defaults. */
314 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
315 or (unsigned int)-1. This is a Bad User Interface. */
319 /* Non-zero tells remote* modules to output debugging info. */
321 int remote_debug
= 0;
323 /* Level of control structure. */
324 static int control_level
;
326 /* Structure for arguments to user defined functions. */
327 #define MAXUSERARGS 10
330 struct user_args
*next
;
339 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
343 #define STOP_SIGNAL SIGTSTP
344 static void stop_sig
PARAMS ((int));
348 /* Some System V have job control but not sigsetmask(). */
349 #if !defined (HAVE_SIGSETMASK)
351 #define HAVE_SIGSETMASK 1
353 #define HAVE_SIGSETMASK 0
357 #if 0 == (HAVE_SIGSETMASK)
358 #define sigsetmask(n)
361 /* Hooks for alternate command interfaces. */
363 /* Called after most modules have been initialized, but before taking users
366 void (*init_ui_hook
) PARAMS ((void));
368 /* Called instead of command_loop at top level. Can be invoked via
369 return_to_top_level. */
371 void (*command_loop_hook
) PARAMS ((void));
373 /* Called instead of fputs for all output. */
375 void (*fputs_unfiltered_hook
) PARAMS ((const char *linebuffer
, FILE *stream
));
377 /* Called from print_frame_info to list the line we stopped in. */
379 void (*print_frame_info_listing_hook
) PARAMS ((struct symtab
*s
, int line
,
380 int stopline
, int noerror
));
381 /* Replaces most of query. */
383 int (*query_hook
) PARAMS (());
385 /* Called from gdb_flush to flush output. */
387 void (*flush_hook
) PARAMS ((FILE *stream
));
389 /* Called as appropriate to notify the interface of the specified breakpoint
392 void (*create_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
393 void (*delete_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
394 void (*enable_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
395 void (*disable_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
397 /* Called during long calculations to allow GUI to repair window damage, and to
398 check for stop buttons, etc... */
400 void (*interactive_hook
) PARAMS ((void));
402 /* Called when going to wait for the target. Usually allows the GUI to run
403 while waiting for target events. */
405 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
*status
));
407 /* Used by UI as a wrapper around command execution. May do various things
408 like enabling/disabling buttons, etc... */
410 void (*call_command_hook
) PARAMS ((struct cmd_list_element
*c
, char *cmd
,
413 /* An alternate way to read memory for disassembly. This is used to provide a
414 switch that allows disassembly to come from an exec file rather than a
415 remote target. This is a speed hack. */
417 int (*dis_asm_read_memory_hook
) PARAMS ((bfd_vma memaddr
, bfd_byte
*myaddr
,
418 int len
, disassemble_info
*info
));
419 /* Takes control from error (). Typically used to prevent longjmps out of the
420 middle of the GUI. Usually used in conjunction with a catch routine. */
422 NORETURN
void (*error_hook
) PARAMS (());
425 /* Where to go for return_to_top_level (RETURN_ERROR). */
426 jmp_buf error_return
;
427 /* Where to go for return_to_top_level (RETURN_QUIT). */
430 /* Return for reason REASON. This generally gets back to the command
431 loop, but can be caught via catch_errors. */
434 return_to_top_level (reason
)
435 enum return_reason reason
;
440 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
441 I can think of a reason why that is vital, though). */
442 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
444 disable_current_display ();
445 do_cleanups (ALL_CLEANUPS
);
447 if (annotation_level
> 1)
458 (NORETURN
void) longjmp
459 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
462 /* Call FUNC with arg ARGS, catching any errors. If there is no
463 error, return the value returned by FUNC. If there is an error,
464 print ERRSTRING, print the specific error message, then return
467 Must not be called with immediate_quit in effect (bad things might
468 happen, say we got a signal in the middle of a memcpy to quit_return).
469 This is an OK restriction; with very few exceptions immediate_quit can
470 be replaced by judicious use of QUIT.
472 MASK specifies what to catch; it is normally set to
473 RETURN_MASK_ALL, if for no other reason than that the code which
474 calls catch_errors might not be set up to deal with a quit which
475 isn't caught. But if the code can deal with it, it generally
476 should be RETURN_MASK_ERROR, unless for some reason it is more
477 useful to abort only the portion of the operation inside the
478 catch_errors. Note that quit should return to the command line
479 fairly quickly, even if some further processing is being done. */
482 catch_errors (func
, args
, errstring
, mask
)
483 int (*func
) PARAMS ((char *));
492 struct cleanup
*saved_cleanup_chain
;
493 char *saved_error_pre_print
;
495 saved_cleanup_chain
= save_cleanups ();
496 saved_error_pre_print
= error_pre_print
;
498 if (mask
& RETURN_MASK_ERROR
)
499 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (jmp_buf));
500 if (mask
& RETURN_MASK_QUIT
)
501 memcpy (saved_quit
, quit_return
, sizeof (jmp_buf));
502 error_pre_print
= errstring
;
504 if (setjmp (tmp_jmp
) == 0)
506 if (mask
& RETURN_MASK_ERROR
)
507 memcpy (error_return
, tmp_jmp
, sizeof (jmp_buf));
508 if (mask
& RETURN_MASK_QUIT
)
509 memcpy (quit_return
, tmp_jmp
, sizeof (jmp_buf));
510 val
= (*func
) (args
);
515 restore_cleanups (saved_cleanup_chain
);
517 error_pre_print
= saved_error_pre_print
;
518 if (mask
& RETURN_MASK_ERROR
)
519 memcpy (error_return
, saved_error
, sizeof (jmp_buf));
520 if (mask
& RETURN_MASK_QUIT
)
521 memcpy (quit_return
, saved_quit
, sizeof (jmp_buf));
525 /* Handler for SIGHUP. */
531 catch_errors (quit_cover
, NULL
,
532 "Could not kill the program being debugged", RETURN_MASK_ALL
);
533 signal (SIGHUP
, SIG_DFL
);
534 kill (getpid (), SIGHUP
);
537 /* Just a little helper function for disconnect(). */
543 caution
= 0; /* Throw caution to the wind -- we're exiting.
544 This prevents asking the user dumb questions. */
545 quit_command((char *)0, 0);
549 /* Line number we are currently in in a file which is being sourced. */
550 static int source_line_number
;
552 /* Name of the file we are sourcing. */
553 static char *source_file_name
;
555 /* Buffer containing the error_pre_print used by the source stuff.
557 static char *source_error
;
558 static int source_error_allocated
;
560 /* Something to glom on to the start of error_pre_print if source_file_name
562 static char *source_pre_error
;
564 /* Clean up on error during a "source" command (or execution of a
565 user-defined command). */
568 source_cleanup (stream
)
571 /* Restore the previous input stream. */
575 /* Read commands from STREAM. */
577 read_command_file (stream
)
580 struct cleanup
*cleanups
;
582 cleanups
= make_cleanup (source_cleanup
, instream
);
585 do_cleanups (cleanups
);
588 extern void init_proc ();
590 void (*pre_init_ui_hook
) PARAMS ((void));
595 if (pre_init_ui_hook
)
598 /* Run the init function of each source file */
600 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
601 current_directory
= gdb_dirbuf
;
603 init_cmd_lists (); /* This needs to be done first */
604 initialize_targets (); /* Setup target_terminal macros for utils.c */
605 initialize_utils (); /* Make errors and warnings possible */
606 initialize_all_files ();
607 init_main (); /* But that omits this file! Do it now */
612 /* We need a default language for parsing expressions, so simple things like
613 "set width 0" won't fail if no language is explicitly set in a config file
614 or implicitly set by reading an executable during startup. */
615 set_language (language_c
);
616 expected_language
= current_language
; /* don't warn about the change. */
622 /* Allocate, initialize a new command line structure for one of the
623 control commands (if/while). */
625 static struct command_line
*
626 build_command_line (type
, args
)
627 enum command_control_type type
;
630 struct command_line
*cmd
;
632 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
634 cmd
->control_type
= type
;
638 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
640 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
641 cmd
->line
= savestring (args
, strlen (args
));
645 /* Build and return a new command structure for the control commands
646 such as "if" and "while". */
648 static struct command_line
*
649 get_command_line (type
, arg
)
650 enum command_control_type type
;
653 struct command_line
*cmd
;
654 struct cleanup
*old_chain
= NULL
;
656 /* Allocate and build a new command line structure. */
657 cmd
= build_command_line (type
, arg
);
659 old_chain
= make_cleanup (free_command_lines
, &cmd
);
661 /* Read in the body of this command. */
662 if (recurse_read_control_structure (cmd
) == invalid_control
)
664 warning ("error reading in control structure\n");
665 do_cleanups (old_chain
);
669 discard_cleanups (old_chain
);
673 /* Recursively print a command (including full control structures). */
675 print_command_line (cmd
, depth
)
676 struct command_line
*cmd
;
683 for (i
= 0; i
< depth
; i
++)
684 fputs_filtered (" ", gdb_stdout
);
687 /* A simple command, print it and return. */
688 if (cmd
->control_type
== simple_control
)
690 fputs_filtered (cmd
->line
, gdb_stdout
);
691 fputs_filtered ("\n", gdb_stdout
);
695 /* loop_continue to jump to the start of a while loop, print it
697 if (cmd
->control_type
== continue_control
)
699 fputs_filtered ("loop_continue\n", gdb_stdout
);
703 /* loop_break to break out of a while loop, print it and return. */
704 if (cmd
->control_type
== break_control
)
706 fputs_filtered ("loop_break\n", gdb_stdout
);
710 /* A while command. Recursively print its subcommands before returning. */
711 if (cmd
->control_type
== while_control
)
713 struct command_line
*list
;
714 fputs_filtered ("while ", gdb_stdout
);
715 fputs_filtered (cmd
->line
, gdb_stdout
);
716 fputs_filtered ("\n", gdb_stdout
);
717 list
= *cmd
->body_list
;
720 print_command_line (list
, depth
+ 1);
725 /* An if command. Recursively print both arms before returning. */
726 if (cmd
->control_type
== if_control
)
728 fputs_filtered ("if ", gdb_stdout
);
729 fputs_filtered (cmd
->line
, gdb_stdout
);
730 fputs_filtered ("\n", gdb_stdout
);
732 print_command_line (cmd
->body_list
[0], depth
+ 1);
734 /* Show the false arm if it exists. */
735 if (cmd
->body_count
== 2)
739 for (i
= 0; i
< depth
; i
++)
740 fputs_filtered (" ", gdb_stdout
);
742 fputs_filtered ("else\n", gdb_stdout
);
743 print_command_line (cmd
->body_list
[1], depth
+ 1);
747 for (i
= 0; i
< depth
; i
++)
748 fputs_filtered (" ", gdb_stdout
);
750 fputs_filtered ("end\n", gdb_stdout
);
754 /* Execute the command in CMD. */
756 enum command_control_type
757 execute_control_command (cmd
)
758 struct command_line
*cmd
;
760 struct expression
*expr
;
761 struct command_line
*current
;
762 struct cleanup
*old_chain
= 0;
765 enum command_control_type ret
;
768 switch (cmd
->control_type
)
771 /* A simple command, execute it and return. */
772 new_line
= insert_args (cmd
->line
);
774 return invalid_control
;
775 old_chain
= make_cleanup (free_current_contents
, &new_line
);
776 execute_command (new_line
, 0);
777 ret
= cmd
->control_type
;
780 case continue_control
:
782 /* Return for "continue", and "break" so we can either
783 continue the loop at the top, or break out. */
784 ret
= cmd
->control_type
;
789 /* Parse the loop control expression for the while statement. */
790 new_line
= insert_args (cmd
->line
);
792 return invalid_control
;
793 old_chain
= make_cleanup (free_current_contents
, &new_line
);
794 expr
= parse_expression (new_line
);
795 make_cleanup (free_current_contents
, &expr
);
797 ret
= simple_control
;
800 /* Keep iterating so long as the expression is true. */
803 /* Evaluate the expression. */
804 val
= evaluate_expression (expr
);
806 /* If the value is false, then break out of the loop. */
807 if (!value_true (val
))
810 /* Execute the body of the while statement. */
811 current
= *cmd
->body_list
;
814 ret
= execute_control_command (current
);
816 /* If we got an error, or a "break" command, then stop
818 if (ret
== invalid_control
|| ret
== break_control
)
824 /* If we got a "continue" command, then restart the loop
826 if (ret
== continue_control
)
829 /* Get the next statement. */
830 current
= current
->next
;
834 /* Reset RET so that we don't recurse the break all the way down. */
835 if (ret
== break_control
)
836 ret
= simple_control
;
843 new_line
= insert_args (cmd
->line
);
845 return invalid_control
;
846 old_chain
= make_cleanup (free_current_contents
, &new_line
);
847 /* Parse the conditional for the if statement. */
848 expr
= parse_expression (new_line
);
849 make_cleanup (free_current_contents
, &expr
);
852 ret
= simple_control
;
854 /* Evaluate the conditional. */
855 val
= evaluate_expression (expr
);
857 /* Choose which arm to take commands from based on the value of the
858 conditional expression. */
859 if (value_true (val
))
860 current
= *cmd
->body_list
;
861 else if (cmd
->body_count
== 2)
862 current
= *(cmd
->body_list
+ 1);
864 /* Execute commands in the given arm. */
867 ret
= execute_control_command (current
);
869 /* If we got an error, get out. */
870 if (ret
!= simple_control
)
873 /* Get the next statement in the body. */
874 current
= current
->next
;
881 warning ("Invalid control type in command structure.");
882 return invalid_control
;
886 do_cleanups (old_chain
);
891 /* "while" command support. Executes a body of statements while the
892 loop condition is nonzero. */
895 while_command (arg
, from_tty
)
899 struct command_line
*command
= NULL
;
902 command
= get_command_line (while_control
, arg
);
907 execute_control_command (command
);
908 free_command_lines (&command
);
911 /* "if" command support. Execute either the true or false arm depending
912 on the value of the if conditional. */
915 if_command (arg
, from_tty
)
919 struct command_line
*command
= NULL
;
922 command
= get_command_line (if_control
, arg
);
927 execute_control_command (command
);
928 free_command_lines (&command
);
935 struct user_args
*oargs
= user_args
;
937 fatal ("Internal error, arg_cleanup called with no user args.\n");
939 user_args
= user_args
->next
;
943 /* Bind the incomming arguments for a user defined command to
944 $arg0, $arg1 ... $argMAXUSERARGS. */
946 static struct cleanup
*
950 struct user_args
*args
;
951 struct cleanup
*old_chain
;
952 unsigned int arg_count
= 0;
954 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
955 memset (args
, 0, sizeof (struct user_args
));
957 args
->next
= user_args
;
960 old_chain
= make_cleanup (arg_cleanup
, 0);
969 if (arg_count
>= MAXUSERARGS
)
971 error ("user defined function may only have %d arguments.\n",
976 /* Strip whitespace. */
977 while (*p
== ' ' || *p
== '\t')
980 /* P now points to an argument. */
982 user_args
->a
[arg_count
].arg
= p
;
984 /* Get to the end of this argument. */
985 while (*p
&& *p
!= ' ' && *p
!= '\t')
988 user_args
->a
[arg_count
].len
= p
- start_arg
;
995 /* Given character string P, return a point to the first argument ($arg),
996 or NULL if P contains no arguments. */
1002 while (p
= strchr (p
, '$'))
1004 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1011 /* Insert the user defined arguments stored in user_arg into the $arg
1012 arguments found in line, with the updated copy being placed into nline. */
1018 char *p
, *save_line
, *new_line
;
1021 /* First we need to know how much memory to allocate for the new line. */
1024 while (p
= locate_arg (line
))
1029 if (i
>= user_args
->count
)
1031 error ("Missing argument %d in user function.\n", i
);
1034 len
+= user_args
->a
[i
].len
;
1038 /* Don't forget the tail. */
1039 len
+= strlen (line
);
1041 /* Allocate space for the new line and fill it in. */
1042 new_line
= (char *)xmalloc (len
+ 1);
1043 if (new_line
== NULL
)
1046 /* Restore pointer to beginning of old line. */
1049 /* Save pointer to beginning of new line. */
1050 save_line
= new_line
;
1052 while (p
= locate_arg (line
))
1056 memcpy (new_line
, line
, p
- line
);
1057 new_line
+= p
- line
;
1060 if (len
= user_args
->a
[i
].len
)
1062 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1067 /* Don't forget the tail. */
1068 strcpy (new_line
, line
);
1070 /* Return a pointer to the beginning of the new line. */
1075 execute_user_command (c
, args
)
1076 struct cmd_list_element
*c
;
1079 register struct command_line
*cmdlines
;
1080 struct cleanup
*old_chain
;
1081 enum command_control_type ret
;
1083 old_chain
= setup_user_args (args
);
1085 cmdlines
= c
->user_commands
;
1090 /* Set the instream to 0, indicating execution of a
1091 user-defined function. */
1092 old_chain
= make_cleanup (source_cleanup
, instream
);
1093 instream
= (FILE *) 0;
1096 ret
= execute_control_command (cmdlines
);
1097 if (ret
!= simple_control
&& ret
!= break_control
)
1099 warning ("Error in control structure.\n");
1102 cmdlines
= cmdlines
->next
;
1104 do_cleanups (old_chain
);
1107 /* Execute the line P as a command.
1108 Pass FROM_TTY as second argument to the defining function. */
1111 execute_command (p
, from_tty
)
1115 register struct cmd_list_element
*c
;
1116 register enum language flang
;
1117 static int warned
= 0;
1121 /* This can happen when command_line_input hits end of file. */
1125 while (*p
== ' ' || *p
== '\t') p
++;
1130 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1131 /* Pass null arg rather than an empty one. */
1134 /* If this command has been hooked, run the hook first. */
1136 execute_user_command (c
->hook
, (char *)0);
1138 if (c
->class == class_user
)
1139 execute_user_command (c
, arg
);
1140 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1141 do_setshow_command (arg
, from_tty
& caution
, c
);
1142 else if (c
->function
.cfunc
== NO_FUNCTION
)
1143 error ("That is not a command, just a help topic.");
1144 else if (call_command_hook
)
1145 call_command_hook (c
, arg
, from_tty
& caution
);
1147 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1150 /* Tell the user if the language has changed (except first time). */
1151 if (current_language
!= expected_language
)
1153 if (language_mode
== language_mode_auto
) {
1154 language_info (1); /* Print what changed. */
1159 /* Warn the user if the working language does not match the
1160 language of the current frame. Only warn the user if we are
1161 actually running the program, i.e. there is a stack. */
1162 /* FIXME: This should be cacheing the frame and only running when
1163 the frame changes. */
1165 if (target_has_stack
)
1167 flang
= get_frame_language ();
1169 && flang
!= language_unknown
1170 && flang
!= current_language
->la_language
)
1172 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1180 command_loop_marker (foo
)
1185 /* Read commands from `instream' and execute them
1186 until end of file or error reading instream. */
1191 struct cleanup
*old_chain
;
1193 int stdin_is_tty
= ISATTY (stdin
);
1194 long time_at_cmd_start
;
1195 long space_at_cmd_start
;
1196 extern int display_time
;
1197 extern int display_space
;
1199 while (!feof (instream
))
1201 if (window_hook
&& instream
== stdin
)
1202 (*window_hook
) (instream
, prompt
);
1205 if (instream
== stdin
&& stdin_is_tty
)
1206 reinitialize_more_filter ();
1207 old_chain
= make_cleanup (command_loop_marker
, 0);
1208 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1209 instream
== stdin
, "prompt");
1213 time_at_cmd_start
= get_run_time ();
1217 extern char **environ
;
1218 char *lim
= (char *) sbrk (0);
1220 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1223 execute_command (command
, instream
== stdin
);
1224 /* Do any commands attached to breakpoint we stopped at. */
1225 bpstat_do_actions (&stop_bpstat
);
1226 do_cleanups (old_chain
);
1230 long cmd_time
= get_run_time () - time_at_cmd_start
;
1232 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1233 cmd_time
/ 1000000, cmd_time
% 1000000);
1238 extern char **environ
;
1239 char *lim
= (char *) sbrk (0);
1240 long space_now
= lim
- (char *) &environ
;
1241 long space_diff
= space_now
- space_at_cmd_start
;
1243 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1245 (space_diff
>= 0 ? '+' : '-'),
1251 /* Commands call this if they do not want to be repeated by null lines. */
1259 /* If we aren't reading from standard input, we are saving the last
1260 thing read from stdin in line and don't want to delete it. Null lines
1261 won't repeat here in any case. */
1262 if (instream
== stdin
)
1266 /* Read a line from the stream "instream" without command line editing.
1268 It prints PRROMPT once at the start.
1269 Action is compatible with "readline", e.g. space for the result is
1270 malloc'd and should be freed by the caller.
1272 A NULL return means end of file. */
1274 gdb_readline (prrompt
)
1279 int input_index
= 0;
1280 int result_size
= 80;
1284 /* Don't use a _filtered function here. It causes the assumed
1285 character position to be off, since the newline we read from
1286 the user is not accounted for. */
1287 fputs_unfiltered (prrompt
, gdb_stdout
);
1288 /* start-sanitize-mpw */
1290 /* Move to a new line so the entered line doesn't have a prompt
1291 on the front of it. */
1292 fputs_unfiltered ("\n", gdb_stdout
);
1294 /* end-sanitize-mpw */
1295 gdb_flush (gdb_stdout
);
1298 result
= (char *) xmalloc (result_size
);
1302 /* Read from stdin if we are executing a user defined command.
1303 This is the right thing for prompt_for_continue, at least. */
1304 c
= fgetc (instream
? instream
: stdin
);
1308 if (input_index
> 0)
1309 /* The last line does not end with a newline. Return it, and
1310 if we are called again fgetc will still return EOF and
1311 we'll return NULL then. */
1320 result
[input_index
++] = c
;
1321 while (input_index
>= result_size
)
1324 result
= (char *) xrealloc (result
, result_size
);
1328 result
[input_index
++] = '\0';
1332 /* Variables which control command line editing and history
1333 substitution. These variables are given default values at the end
1335 static int command_editing_p
;
1336 static int history_expansion_p
;
1337 static int write_history_p
;
1338 static int history_size
;
1339 static char *history_filename
;
1341 /* readline uses the word breaks for two things:
1342 (1) In figuring out where to point the TEXT parameter to the
1343 rl_completion_entry_function. Since we don't use TEXT for much,
1344 it doesn't matter a lot what the word breaks are for this purpose, but
1345 it does affect how much stuff M-? lists.
1346 (2) If one of the matches contains a word break character, readline
1347 will quote it. That's why we switch between
1348 gdb_completer_word_break_characters and
1349 gdb_completer_command_word_break_characters. I'm not sure when
1350 we need this behavior (perhaps for funky characters in C++ symbols?). */
1352 /* Variables which are necessary for fancy command line editing. */
1353 char *gdb_completer_word_break_characters
=
1354 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1356 /* When completing on command names, we remove '-' from the list of
1357 word break characters, since we use it in command names. If the
1358 readline library sees one in any of the current completion strings,
1359 it thinks that the string needs to be quoted and automatically supplies
1361 char *gdb_completer_command_word_break_characters
=
1362 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1364 /* Characters that can be used to quote completion strings. Note that we
1365 can't include '"' because the gdb C parser treats such quoted sequences
1367 char *gdb_completer_quote_characters
=
1370 /* Functions that are used as part of the fancy command line editing. */
1372 /* This can be used for functions which don't want to complete on symbols
1373 but don't want to complete on anything else either. */
1376 noop_completer (text
, prefix
)
1383 /* Complete on filenames. */
1385 filename_completer (text
, word
)
1389 /* From readline. */
1390 extern char *filename_completion_function ();
1391 int subsequent_name
;
1393 int return_val_used
;
1394 int return_val_alloced
;
1396 return_val_used
= 0;
1397 /* Small for testing. */
1398 return_val_alloced
= 1;
1399 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1401 subsequent_name
= 0;
1405 p
= filename_completion_function (text
, subsequent_name
);
1406 if (return_val_used
>= return_val_alloced
)
1408 return_val_alloced
*= 2;
1410 (char **) xrealloc (return_val
,
1411 return_val_alloced
* sizeof (char *));
1415 return_val
[return_val_used
++] = p
;
1418 /* Like emacs, don't complete on old versions. Especially useful
1419 in the "source" command. */
1420 if (p
[strlen (p
) - 1] == '~')
1426 /* Return exactly p. */
1427 return_val
[return_val_used
++] = p
;
1428 else if (word
> text
)
1430 /* Return some portion of p. */
1431 q
= xmalloc (strlen (p
) + 5);
1432 strcpy (q
, p
+ (word
- text
));
1433 return_val
[return_val_used
++] = q
;
1438 /* Return some of TEXT plus p. */
1439 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1440 strncpy (q
, word
, text
- word
);
1441 q
[text
- word
] = '\0';
1443 return_val
[return_val_used
++] = q
;
1447 subsequent_name
= 1;
1450 /* There is no way to do this just long enough to affect quote inserting
1451 without also affecting the next completion. This should be fixed in
1453 /* Insure that readline does the right thing
1454 with respect to inserting quotes. */
1455 rl_completer_word_break_characters
= "";
1460 /* Here are some useful test cases for completion. FIXME: These should
1461 be put in the test suite. They should be tested with both M-? and TAB.
1463 "show output-" "radix"
1464 "show output" "-radix"
1465 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1466 "p " ambiguous (all symbols)
1467 "info t foo" no completions
1468 "info t " no completions
1469 "info t" ambiguous ("info target", "info terminal", etc.)
1470 "info ajksdlfk" no completions
1471 "info ajksdlfk " no completions
1473 "info " ambiguous (all info commands)
1474 "p \"a" no completions (string constant)
1475 "p 'a" ambiguous (all symbols starting with a)
1476 "p b-a" ambiguous (all symbols starting with a)
1477 "p b-" ambiguous (all symbols)
1478 "file Make" "file" (word break hard to screw up here)
1479 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1482 /* Generate completions one by one for the completer. Each time we are
1483 called return another potential completion to the caller.
1484 line_completion just completes on commands or passes the buck to the
1485 command's completer function, the stuff specific to symbol completion
1486 is in make_symbol_completion_list.
1488 TEXT is the caller's idea of the "word" we are looking at.
1490 MATCHES is the number of matches that have currently been collected from
1491 calling this completion function. When zero, then we need to initialize,
1492 otherwise the initialization has already taken place and we can just
1493 return the next potential completion string.
1495 LINE_BUFFER is available to be looked at; it contains the entire text
1496 of the line. POINT is the offset in that line of the cursor. You
1497 should pretend that the line ends at POINT.
1499 Returns NULL if there are no more completions, else a pointer to a string
1500 which is a possible completion, it is the caller's responsibility to
1504 line_completion_function (text
, matches
, line_buffer
, point
)
1510 static char **list
= (char **)NULL
; /* Cache of completions */
1511 static int index
; /* Next cached completion */
1512 char *output
= NULL
;
1513 char *tmp_command
, *p
;
1514 /* Pointer within tmp_command which corresponds to text. */
1516 struct cmd_list_element
*c
, *result_list
;
1520 /* The caller is beginning to accumulate a new set of completions, so
1521 we need to find all of them now, and cache them for returning one at
1522 a time on future calls. */
1526 /* Free the storage used by LIST, but not by the strings inside.
1527 This is because rl_complete_internal () frees the strings. */
1533 /* Choose the default set of word break characters to break completions.
1534 If we later find out that we are doing completions on command strings
1535 (as opposed to strings supplied by the individual command completer
1536 functions, which can be any string) then we will switch to the
1537 special word break set for command strings, which leaves out the
1538 '-' character used in some commands. */
1540 rl_completer_word_break_characters
=
1541 gdb_completer_word_break_characters
;
1543 /* Decide whether to complete on a list of gdb commands or on symbols. */
1544 tmp_command
= (char *) alloca (point
+ 1);
1547 strncpy (tmp_command
, line_buffer
, point
);
1548 tmp_command
[point
] = '\0';
1549 /* Since text always contains some number of characters leading up
1550 to point, we can find the equivalent position in tmp_command
1551 by subtracting that many characters from the end of tmp_command. */
1552 word
= tmp_command
+ point
- strlen (text
);
1556 /* An empty line we want to consider ambiguous; that is, it
1557 could be any command. */
1558 c
= (struct cmd_list_element
*) -1;
1563 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1566 /* Move p up to the next interesting thing. */
1567 while (*p
== ' ' || *p
== '\t')
1574 /* It is an unrecognized command. So there are no
1575 possible completions. */
1578 else if (c
== (struct cmd_list_element
*) -1)
1582 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1583 doesn't advance over that thing itself. Do so now. */
1585 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1587 if (q
!= tmp_command
+ point
)
1589 /* There is something beyond the ambiguous
1590 command, so there are no possible completions. For
1591 example, "info t " or "info t foo" does not complete
1592 to anything, because "info t" can be "info target" or
1598 /* We're trying to complete on the command which was ambiguous.
1599 This we can deal with. */
1602 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1607 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1609 /* Insure that readline does the right thing with respect to
1610 inserting quotes. */
1611 rl_completer_word_break_characters
=
1612 gdb_completer_command_word_break_characters
;
1617 /* We've recognized a full command. */
1619 if (p
== tmp_command
+ point
)
1621 /* There is no non-whitespace in the line beyond the command. */
1623 if (p
[-1] == ' ' || p
[-1] == '\t')
1625 /* The command is followed by whitespace; we need to complete
1626 on whatever comes after command. */
1629 /* It is a prefix command; what comes after it is
1630 a subcommand (e.g. "info "). */
1631 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1633 /* Insure that readline does the right thing
1634 with respect to inserting quotes. */
1635 rl_completer_word_break_characters
=
1636 gdb_completer_command_word_break_characters
;
1640 /* It is a normal command; what comes after it is
1641 completed by the command's completer function. */
1642 list
= (*c
->completer
) (p
, word
);
1647 /* The command is not followed by whitespace; we need to
1648 complete on the command itself. e.g. "p" which is a
1649 command itself but also can complete to "print", "ptype"
1653 /* Find the command we are completing on. */
1655 while (q
> tmp_command
)
1657 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1663 list
= complete_on_cmdlist (result_list
, q
, word
);
1665 /* Insure that readline does the right thing
1666 with respect to inserting quotes. */
1667 rl_completer_word_break_characters
=
1668 gdb_completer_command_word_break_characters
;
1673 /* There is non-whitespace beyond the command. */
1675 if (c
->prefixlist
&& !c
->allow_unknown
)
1677 /* It is an unrecognized subcommand of a prefix command,
1678 e.g. "info adsfkdj". */
1683 /* It is a normal command. */
1684 list
= (*c
->completer
) (p
, word
);
1690 /* If we found a list of potential completions during initialization then
1691 dole them out one at a time. The vector of completions is NULL
1692 terminated, so after returning the last one, return NULL (and continue
1693 to do so) each time we are called after that, until a new list is
1698 output
= list
[index
];
1706 /* Can't do this because readline hasn't yet checked the word breaks
1707 for figuring out whether to insert a quote. */
1709 /* Make sure the word break characters are set back to normal for the
1710 next time that readline tries to complete something. */
1711 rl_completer_word_break_characters
=
1712 gdb_completer_word_break_characters
;
1718 /* Line completion interface function for readline. */
1721 readline_line_completion_function (text
, matches
)
1725 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1728 /* Skip over a possibly quoted word (as defined by the quote characters
1729 and word break characters the completer uses). Returns pointer to the
1730 location after the "word". */
1736 char quote_char
= '\0';
1739 for (scan
= str
; *scan
!= '\0'; scan
++)
1741 if (quote_char
!= '\0')
1743 /* Ignore everything until the matching close quote char */
1744 if (*scan
== quote_char
)
1746 /* Found matching close quote. */
1751 else if (strchr (gdb_completer_quote_characters
, *scan
))
1753 /* Found start of a quoted string. */
1756 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1770 #if STOP_SIGNAL == SIGTSTP
1771 signal (SIGTSTP
, SIG_DFL
);
1773 kill (getpid (), SIGTSTP
);
1774 signal (SIGTSTP
, stop_sig
);
1776 signal (STOP_SIGNAL
, stop_sig
);
1778 printf_unfiltered ("%s", prompt
);
1779 gdb_flush (gdb_stdout
);
1781 /* Forget about any previous command -- null line now will do nothing. */
1784 #endif /* STOP_SIGNAL */
1786 /* Initialize signal handlers. */
1796 signal (SIGINT
, request_quit
);
1798 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1799 passed to the inferior, which we don't want. It would be
1800 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1801 on BSD4.3 systems using vfork, that can affect the
1802 GDB process as well as the inferior (the signal handling tables
1803 might be in memory, shared between the two). Since we establish
1804 a handler for SIGQUIT, when we call exec it will set the signal
1805 to SIG_DFL for us. */
1806 signal (SIGQUIT
, do_nothing
);
1807 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1808 signal (SIGHUP
, disconnect
);
1809 signal (SIGFPE
, float_handler
);
1811 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1812 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1816 /* Read one line from the command input stream `instream'
1817 into the local static buffer `linebuffer' (whose current length
1819 The buffer is made bigger as necessary.
1820 Returns the address of the start of the line.
1822 NULL is returned for end of file.
1824 *If* the instream == stdin & stdin is a terminal, the line read
1825 is copied into the file line saver (global var char *line,
1826 length linesize) so that it can be duplicated.
1828 This routine either uses fancy command line editing or
1829 simple input as the user has requested. */
1832 command_line_input (prrompt
, repeat
, annotation_suffix
)
1835 char *annotation_suffix
;
1837 static char *linebuffer
= 0;
1838 static unsigned linelength
= 0;
1842 char *local_prompt
= prrompt
;
1847 /* The annotation suffix must be non-NULL. */
1848 if (annotation_suffix
== NULL
)
1849 annotation_suffix
= "";
1851 if (annotation_level
> 1 && instream
== stdin
)
1853 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
1854 + strlen (annotation_suffix
) + 40);
1855 if (prrompt
== NULL
)
1856 local_prompt
[0] = '\0';
1858 strcpy (local_prompt
, prrompt
);
1859 strcat (local_prompt
, "\n\032\032");
1860 strcat (local_prompt
, annotation_suffix
);
1861 strcat (local_prompt
, "\n");
1864 if (linebuffer
== 0)
1867 linebuffer
= (char *) xmalloc (linelength
);
1872 /* Control-C quits instantly if typed while in this loop
1873 since it should not wait until the user types a newline. */
1877 signal (STOP_SIGNAL
, stop_sig
);
1882 /* Make sure that all output has been output. Some machines may let
1883 you get away with leaving out some of the gdb_flush, but not all. */
1885 gdb_flush (gdb_stdout
);
1886 gdb_flush (gdb_stderr
);
1888 if (source_file_name
!= NULL
)
1890 ++source_line_number
;
1891 sprintf (source_error
,
1892 "%s%s:%d: Error in sourced command file:\n",
1895 source_line_number
);
1896 error_pre_print
= source_error
;
1899 if (annotation_level
> 1 && instream
== stdin
)
1901 printf_unfiltered ("\n\032\032pre-");
1902 printf_unfiltered (annotation_suffix
);
1903 printf_unfiltered ("\n");
1906 /* Don't use fancy stuff if not talking to stdin. */
1907 if (command_editing_p
&& instream
== stdin
1908 && ISATTY (instream
))
1909 rl
= readline (local_prompt
);
1911 rl
= gdb_readline (local_prompt
);
1913 if (annotation_level
> 1 && instream
== stdin
)
1915 printf_unfiltered ("\n\032\032post-");
1916 printf_unfiltered (annotation_suffix
);
1917 printf_unfiltered ("\n");
1920 if (!rl
|| rl
== (char *) EOF
)
1925 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
1927 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
1928 nline
= (char *) xrealloc (linebuffer
, linelength
);
1929 p
+= nline
- linebuffer
;
1933 /* Copy line. Don't copy null at end. (Leaves line alone
1934 if this was just a newline) */
1938 free (rl
); /* Allocated in readline. */
1940 if (p
== linebuffer
|| *(p
- 1) != '\\')
1943 p
--; /* Put on top of '\'. */
1944 local_prompt
= (char *) 0;
1949 signal (STOP_SIGNAL
, SIG_DFL
);
1956 #define SERVER_COMMAND_LENGTH 7
1958 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
1959 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
1962 /* Note that we don't set `line'. Between this and the check in
1963 dont_repeat, this insures that repeating will still do the
1966 return linebuffer
+ SERVER_COMMAND_LENGTH
;
1969 /* Do history expansion if that is wished. */
1970 if (history_expansion_p
&& instream
== stdin
1971 && ISATTY (instream
))
1973 char *history_value
;
1976 *p
= '\0'; /* Insert null now. */
1977 expanded
= history_expand (linebuffer
, &history_value
);
1980 /* Print the changes. */
1981 printf_unfiltered ("%s\n", history_value
);
1983 /* If there was an error, call this function again. */
1986 free (history_value
);
1987 return command_line_input (prrompt
, repeat
, annotation_suffix
);
1989 if (strlen (history_value
) > linelength
)
1991 linelength
= strlen (history_value
) + 1;
1992 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
1994 strcpy (linebuffer
, history_value
);
1995 p
= linebuffer
+ strlen(linebuffer
);
1996 free (history_value
);
2000 /* If we just got an empty line, and that is supposed
2001 to repeat the previous command, return the value in the
2005 if (p
== linebuffer
)
2008 while (*p1
== ' ' || *p1
== '\t')
2016 /* Add line to history if appropriate. */
2017 if (instream
== stdin
2018 && ISATTY (stdin
) && *linebuffer
)
2019 add_history (linebuffer
);
2021 /* Note: lines consisting solely of comments are added to the command
2022 history. This is useful when you type a command, and then
2023 realize you don't want to execute it quite yet. You can comment
2024 out the command and then later fetch it from the value history
2025 and remove the '#'. The kill ring is probably better, but some
2026 people are in the habit of commenting things out. */
2028 while ((c
= *p1
++) != '\0')
2031 while ((c
= *p1
++) != '"')
2033 /* Make sure an escaped '"' doesn't make us think the string
2041 while ((c
= *p1
++) != '\'')
2043 /* Make sure an escaped '\'' doesn't make us think the string
2052 /* Found a comment. */
2058 /* Save into global buffer if appropriate. */
2061 if (linelength
> linesize
)
2063 line
= xrealloc (line
, linelength
);
2064 linesize
= linelength
;
2066 strcpy (line
, linebuffer
);
2074 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2075 code bodies. This is typically used when we encounter an "else"
2076 clause for an "if" command. */
2079 realloc_body_list (command
, new_length
)
2080 struct command_line
*command
;
2084 struct command_line
**body_list
;
2086 n
= command
->body_count
;
2088 /* Nothing to do? */
2089 if (new_length
<= n
)
2092 body_list
= (struct command_line
**)
2093 xmalloc (sizeof (struct command_line
*) * new_length
);
2095 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2097 free (command
->body_list
);
2098 command
->body_list
= body_list
;
2099 command
->body_count
= new_length
;
2102 /* Read one line from the input stream. If the command is an "else" or
2103 "end", return such an indication to the caller. */
2105 static enum misc_command_type
2106 read_next_line (command
)
2107 struct command_line
**command
;
2109 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2112 if (control_level
>= 254)
2113 error ("Control nesting too deep!\n");
2115 /* Set a prompt based on the nesting of the control commands. */
2116 if (instream
== stdin
)
2118 for (i
= 0; i
< control_level
; i
++)
2119 control_prompt
[i
] = ' ';
2120 control_prompt
[i
] = '>';
2121 control_prompt
[i
+1] = '\0';
2122 prompt_ptr
= (char *)&control_prompt
[0];
2127 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2129 /* Not sure what to do here. */
2133 /* Strip leading and trailing whitespace. */
2134 while (*p
== ' ' || *p
== '\t')
2137 p1
= p
+ strlen (p
);
2138 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2141 /* Blanks and comments don't really do anything, but we need to
2142 distinguish them from else, end and other commands which can be
2144 if (p1
== p
|| p
[0] == '#')
2147 /* Is this the end of a simple, while, or if control structure? */
2148 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2151 /* Is the else clause of an if control structure? */
2152 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2153 return else_command
;
2155 /* Check for while, if, break, continue, etc and build a new command
2156 line structure for them. */
2157 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2158 *command
= build_command_line (while_control
, p
+ 6);
2159 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2160 *command
= build_command_line (if_control
, p
+ 3);
2161 else if (p1
- p
== 5 && !strncmp (p
, "loop_break", 5))
2163 *command
= (struct command_line
*)
2164 xmalloc (sizeof (struct command_line
));
2165 (*command
)->next
= NULL
;
2166 (*command
)->line
= NULL
;
2167 (*command
)->control_type
= break_control
;
2168 (*command
)->body_count
= 0;
2169 (*command
)->body_list
= NULL
;
2171 else if (p1
- p
== 8 && !strncmp (p
, "loop_continue", 8))
2173 *command
= (struct command_line
*)
2174 xmalloc (sizeof (struct command_line
));
2175 (*command
)->next
= NULL
;
2176 (*command
)->line
= NULL
;
2177 (*command
)->control_type
= continue_control
;
2178 (*command
)->body_count
= 0;
2179 (*command
)->body_list
= NULL
;
2183 /* A normal command. */
2184 *command
= (struct command_line
*)
2185 xmalloc (sizeof (struct command_line
));
2186 (*command
)->next
= NULL
;
2187 (*command
)->line
= savestring (p
, p1
- p
);
2188 (*command
)->control_type
= simple_control
;
2189 (*command
)->body_count
= 0;
2190 (*command
)->body_list
= NULL
;
2193 /* Nothing special. */
2197 /* Recursively read in the control structures and create a command_line
2200 The parent_control parameter is the control structure in which the
2201 following commands are nested. */
2203 static enum command_control_type
2204 recurse_read_control_structure (current_cmd
)
2205 struct command_line
*current_cmd
;
2207 int current_body
, i
;
2208 enum misc_command_type val
;
2209 enum command_control_type ret
;
2210 struct command_line
**body_ptr
, *child_tail
, *next
;
2211 struct cleanup
*old_chains
, *tmp_chains
;
2217 /* Sanity checks. */
2218 if (current_cmd
->control_type
== simple_control
)
2220 error ("Recursed on a simple control type\n");
2221 return invalid_control
;
2224 if (current_body
> current_cmd
->body_count
)
2226 error ("Allocated body is smaller than this command type needs\n");
2227 return invalid_control
;
2230 /* Read lines from the input stream and build control structures. */
2236 val
= read_next_line (&next
);
2238 /* Just skip blanks and comments. */
2239 if (val
== nop_command
)
2242 if (val
== end_command
)
2244 if (current_cmd
->control_type
== while_control
2245 || current_cmd
->control_type
== if_control
)
2247 /* Success reading an entire control structure. */
2248 ret
= simple_control
;
2253 ret
= invalid_control
;
2258 /* Not the end of a control structure. */
2259 if (val
== else_command
)
2261 if (current_cmd
->control_type
== if_control
2262 && current_body
== 1)
2264 realloc_body_list (current_cmd
, 2);
2271 ret
= invalid_control
;
2278 child_tail
->next
= next
;
2282 /* We have just read the first line of the child's control
2283 structure. From now on, arrange to throw away the line
2284 we have if we quit or get an error. */
2285 body_ptr
= current_cmd
->body_list
;
2286 for (i
= 1; i
< current_body
; i
++)
2291 tmp_chains
= make_cleanup (free_command_lines
, body_ptr
);
2294 old_chains
= tmp_chains
;
2299 /* If the latest line is another control structure, then recurse
2301 if (next
->control_type
== while_control
2302 || next
->control_type
== if_control
)
2305 ret
= recurse_read_control_structure (next
);
2308 if (ret
!= simple_control
)
2314 if (ret
== invalid_control
&& old_chains
)
2315 do_cleanups (old_chains
);
2316 else if (old_chains
)
2317 discard_cleanups (old_chains
);
2323 /* Read lines from the input stream
2324 and accumulate them in a chain of struct command_line's
2325 which is then returned. */
2327 struct command_line
*
2328 read_command_lines ()
2330 struct command_line
*head
, *tail
, *next
;
2331 struct cleanup
*old_chain
;
2332 enum command_control_type ret
;
2333 enum misc_command_type val
;
2340 val
= read_next_line (&next
);
2342 /* Ignore blank lines or comments. */
2343 if (val
== nop_command
)
2346 if (val
== end_command
)
2348 ret
= simple_control
;
2352 if (val
!= ok_command
)
2354 ret
= invalid_control
;
2358 if (next
->control_type
== while_control
2359 || next
->control_type
== if_control
)
2362 ret
= recurse_read_control_structure (next
);
2365 if (ret
== invalid_control
)
2376 old_chain
= make_cleanup (free_command_lines
, &head
);
2385 if (ret
!= invalid_control
)
2387 discard_cleanups (old_chain
);
2391 do_cleanups (old_chain
);
2397 /* Free a chain of struct command_line's. */
2400 free_command_lines (lptr
)
2401 struct command_line
**lptr
;
2403 register struct command_line
*l
= *lptr
;
2404 register struct command_line
*next
;
2405 struct command_line
**blist
;
2410 if (l
->body_count
> 0)
2412 blist
= l
->body_list
;
2413 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2414 free_command_lines (blist
);
2423 /* Add an element to the list of info subcommands. */
2426 add_info (name
, fun
, doc
)
2428 void (*fun
) PARAMS ((char *, int));
2431 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2434 /* Add an alias to the list of info subcommands. */
2437 add_info_alias (name
, oldname
, abbrev_flag
)
2442 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2445 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2446 Therefore, its own definition is called only for "info" with no args. */
2450 info_command (arg
, from_tty
)
2454 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2455 help_list (infolist
, "info ", -1, gdb_stdout
);
2458 /* The "complete" command is used by Emacs to implement completion. */
2462 complete_command (arg
, from_tty
)
2474 argpoint
= strlen (arg
);
2476 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2478 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2480 printf_unfiltered ("%s\n", completion
);
2485 /* The "show" command with no arguments shows all the settings. */
2489 show_command (arg
, from_tty
)
2493 cmd_show_list (showlist
, from_tty
, "");
2496 /* Add an element to the list of commands. */
2499 add_com (name
, class, fun
, doc
)
2501 enum command_class
class;
2502 void (*fun
) PARAMS ((char *, int));
2505 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2508 /* Add an alias or abbreviation command to the list of commands. */
2511 add_com_alias (name
, oldname
, class, abbrev_flag
)
2514 enum command_class
class;
2517 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2524 error ("Argument required (%s).", why
);
2529 help_command (command
, from_tty
)
2531 int from_tty
; /* Ignored */
2533 help_cmd (command
, gdb_stdout
);
2537 validate_comname (comname
)
2543 error_no_arg ("name of command to define");
2548 if (!isalnum(*p
) && *p
!= '-')
2549 error ("Junk in argument list: \"%s\"", p
);
2554 /* This is just a placeholder in the command data structures. */
2556 user_defined_command (ignore
, from_tty
)
2563 define_command (comname
, from_tty
)
2567 register struct command_line
*cmds
;
2568 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2569 char *tem
= comname
;
2570 #define HOOK_STRING "hook-"
2573 validate_comname (comname
);
2575 /* Look it up, and verify that we got an exact match. */
2576 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2577 if (c
&& !STREQ (comname
, c
->name
))
2582 if (c
->class == class_user
|| c
->class == class_alias
)
2583 tem
= "Redefine command \"%s\"? ";
2585 tem
= "Really redefine built-in command \"%s\"? ";
2586 if (!query (tem
, c
->name
))
2587 error ("Command \"%s\" not redefined.", c
->name
);
2590 /* If this new command is a hook, then mark the command which it
2591 is hooking. Note that we allow hooking `help' commands, so that
2592 we can hook the `stop' pseudo-command. */
2594 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2596 /* Look up cmd it hooks, and verify that we got an exact match. */
2597 tem
= comname
+HOOK_LEN
;
2598 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2599 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2603 warning ("Your new `%s' command does not hook any existing command.",
2605 if (!query ("Proceed? ", (char *)0))
2606 error ("Not confirmed.");
2610 comname
= savestring (comname
, strlen (comname
));
2612 /* If the rest of the commands will be case insensitive, this one
2613 should behave in the same manner. */
2614 for (tem
= comname
; *tem
; tem
++)
2615 if (isupper(*tem
)) *tem
= tolower(*tem
);
2619 printf_unfiltered ("Type commands for definition of \"%s\".\n\
2620 End with a line saying just \"end\".\n", comname
);
2621 gdb_flush (gdb_stdout
);
2625 cmds
= read_command_lines ();
2627 if (c
&& c
->class == class_user
)
2628 free_command_lines (&c
->user_commands
);
2630 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2631 (c
&& c
->class == class_user
)
2632 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2633 newc
->user_commands
= cmds
;
2635 /* If this new command is a hook, then mark both commands as being
2639 hookc
->hook
= newc
; /* Target gets hooked. */
2640 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2645 document_command (comname
, from_tty
)
2649 struct command_line
*doclines
;
2650 register struct cmd_list_element
*c
;
2651 char *tem
= comname
;
2653 validate_comname (comname
);
2655 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2657 if (c
->class != class_user
)
2658 error ("Command \"%s\" is built-in.", comname
);
2661 printf_unfiltered ("Type documentation for \"%s\".\n\
2662 End with a line saying just \"end\".\n", comname
);
2664 doclines
= read_command_lines ();
2666 if (c
->doc
) free (c
->doc
);
2669 register struct command_line
*cl1
;
2670 register int len
= 0;
2672 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2673 len
+= strlen (cl1
->line
) + 1;
2675 c
->doc
= (char *) xmalloc (len
+ 1);
2678 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2680 strcat (c
->doc
, cl1
->line
);
2682 strcat (c
->doc
, "\n");
2686 free_command_lines (&doclines
);
2690 print_gnu_advertisement ()
2692 printf_unfiltered ("\
2693 GDB is free software and you are welcome to distribute copies of it\n\
2694 under certain conditions; type \"show copying\" to see the conditions.\n\
2695 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2700 print_gdb_version (stream
)
2703 fprintf_filtered (stream
, "\
2704 GDB %s (%s", version
, host_name
);
2706 if (!STREQ (host_name
, target_name
))
2707 fprintf_filtered (stream
, " --target %s", target_name
);
2709 fprintf_filtered (stream
, "), ");
2711 fprintf_filtered (stream
, "Copyright 1994 Free Software Foundation, Inc.");
2716 show_version (args
, from_tty
)
2721 print_gnu_advertisement ();
2722 print_gdb_version (gdb_stdout
);
2723 printf_filtered ("\n");
2727 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2733 printf_unfiltered ("%s", prompt
);
2734 gdb_flush (gdb_stdout
);
2738 quit_command (args
, from_tty
)
2742 if (inferior_pid
!= 0 && target_has_execution
)
2746 if (query ("The program is running. Quit anyway (and detach it)? "))
2747 target_detach (args
, from_tty
);
2749 error ("Not confirmed.");
2753 if (query ("The program is running. Quit anyway (and kill it)? "))
2756 error ("Not confirmed.");
2759 /* UDI wants this, to kill the TIP. */
2762 /* Save the history information if it is appropriate to do so. */
2763 if (write_history_p
&& history_filename
)
2764 write_history (history_filename
);
2769 /* Returns whether GDB is running on a terminal and whether the user
2770 desires that questions be asked of them on that terminal. */
2773 input_from_terminal_p ()
2775 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
2780 pwd_command (args
, from_tty
)
2784 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
2785 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
2787 if (!STREQ (gdb_dirbuf
, current_directory
))
2788 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2789 current_directory
, gdb_dirbuf
);
2791 printf_unfiltered ("Working directory %s.\n", current_directory
);
2795 cd_command (dir
, from_tty
)
2800 /* Found something other than leading repetitions of "/..". */
2801 int found_real_path
;
2804 /* If the new directory is absolute, repeat is a no-op; if relative,
2805 repeat might be useful but is more likely to be a mistake. */
2809 error_no_arg ("new working directory");
2811 dir
= tilde_expand (dir
);
2812 make_cleanup (free
, dir
);
2814 if (chdir (dir
) < 0)
2815 perror_with_name (dir
);
2818 dir
= savestring (dir
, len
- (len
> 1 && dir
[len
-1] == '/'));
2820 current_directory
= dir
;
2823 if (current_directory
[0] == '/' && current_directory
[1] == '\0')
2824 current_directory
= concat (current_directory
, dir
, NULL
);
2826 current_directory
= concat (current_directory
, "/", dir
, NULL
);
2830 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2832 found_real_path
= 0;
2833 for (p
= current_directory
; *p
;)
2835 if (p
[0] == '/' && p
[1] == '.' && (p
[2] == 0 || p
[2] == '/'))
2837 else if (p
[0] == '/' && p
[1] == '.' && p
[2] == '.'
2838 && (p
[3] == 0 || p
[3] == '/'))
2840 if (found_real_path
)
2842 /* Search backwards for the directory just before the "/.."
2843 and obliterate it and the "/..". */
2845 while (q
!= current_directory
&& q
[-1] != '/')
2848 if (q
== current_directory
)
2849 /* current_directory is
2850 a relative pathname ("can't happen"--leave it alone). */
2854 strcpy (q
- 1, p
+ 3);
2859 /* We are dealing with leading repetitions of "/..", for example
2860 "/../..", which is the Mach super-root. */
2865 found_real_path
= 1;
2870 forget_cached_source_info ();
2873 pwd_command ((char *) 0, 1);
2876 struct source_cleanup_lines_args
{
2879 char *old_pre_error
;
2880 char *old_error_pre_print
;
2884 source_cleanup_lines (args
)
2887 struct source_cleanup_lines_args
*p
=
2888 (struct source_cleanup_lines_args
*)args
;
2889 source_line_number
= p
->old_line
;
2890 source_file_name
= p
->old_file
;
2891 source_pre_error
= p
->old_pre_error
;
2892 error_pre_print
= p
->old_error_pre_print
;
2897 source_command (args
, from_tty
)
2902 struct cleanup
*old_cleanups
;
2904 struct source_cleanup_lines_args old_lines
;
2909 error ("source command requires pathname of file to source.");
2912 file
= tilde_expand (file
);
2913 old_cleanups
= make_cleanup (free
, file
);
2915 stream
= fopen (file
, FOPEN_RT
);
2917 perror_with_name (file
);
2919 make_cleanup (fclose
, stream
);
2921 old_lines
.old_line
= source_line_number
;
2922 old_lines
.old_file
= source_file_name
;
2923 old_lines
.old_pre_error
= source_pre_error
;
2924 old_lines
.old_error_pre_print
= error_pre_print
;
2925 make_cleanup (source_cleanup_lines
, &old_lines
);
2926 source_line_number
= 0;
2927 source_file_name
= file
;
2928 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
2929 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
2930 make_cleanup (free
, source_pre_error
);
2931 /* This will get set every time we read a line. So it won't stay "" for
2933 error_pre_print
= "";
2935 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
2936 if (source_error_allocated
< needed_length
)
2938 source_error_allocated
*= 2;
2939 if (source_error_allocated
< needed_length
)
2940 source_error_allocated
= needed_length
;
2941 if (source_error
== NULL
)
2942 source_error
= xmalloc (source_error_allocated
);
2944 source_error
= xrealloc (source_error
, source_error_allocated
);
2947 read_command_file (stream
);
2949 do_cleanups (old_cleanups
);
2954 echo_command (text
, from_tty
)
2962 while ((c
= *p
++) != '\0')
2966 /* \ at end of argument is used after spaces
2967 so they won't be lost. */
2971 c
= parse_escape (&p
);
2973 printf_filtered ("%c", c
);
2976 printf_filtered ("%c", c
);
2979 /* Force this output to appear now. */
2981 gdb_flush (gdb_stdout
);
2985 #ifdef TARGET_BYTE_ORDER_SELECTABLE
2987 /* Functions to manipulate the endianness of the target. */
2989 #ifndef TARGET_BYTE_ORDER_DEFAULT
2990 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
2993 int target_byte_order
= TARGET_BYTE_ORDER_DEFAULT
;
2995 static int target_byte_order_auto
= 1;
2997 /* Called if the user enters ``set endian'' without an argument. */
2999 set_endian (args
, from_tty
)
3003 printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
3004 show_endian (args
, from_tty
);
3007 /* Called by ``set endian big''. */
3009 set_endian_big (args
, from_tty
)
3013 target_byte_order
= BIG_ENDIAN
;
3014 target_byte_order_auto
= 0;
3017 /* Called by ``set endian little''. */
3019 set_endian_little (args
, from_tty
)
3023 target_byte_order
= LITTLE_ENDIAN
;
3024 target_byte_order_auto
= 0;
3027 /* Called by ``set endian auto''. */
3029 set_endian_auto (args
, from_tty
)
3033 target_byte_order_auto
= 1;
3036 /* Called by ``show endian''. */
3038 show_endian (args
, from_tty
)
3043 (target_byte_order_auto
3044 ? "The target endianness is set automatically (currently %s endian)\n"
3045 : "The target is assumed to be %s endian\n");
3046 printf_unfiltered (msg
, TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3049 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3051 /* Set the endianness from a BFD. */
3053 set_endian_from_file (abfd
)
3056 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3059 if (abfd
->xvec
->byteorder_big_p
)
3062 want
= LITTLE_ENDIAN
;
3063 if (target_byte_order_auto
)
3064 target_byte_order
= want
;
3065 else if (target_byte_order
!= want
)
3066 warning ("%s endian file does not match %s endian target.",
3067 want
== BIG_ENDIAN
? "big" : "little",
3068 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3070 #else /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3072 if (abfd
->xvec
->byteorder_big_p
3073 ? TARGET_BYTE_ORDER
!= BIG_ENDIAN
3074 : TARGET_BYTE_ORDER
== BIG_ENDIAN
)
3075 warning ("%s endian file does not match %s endian target.",
3076 abfd
->xvec
->byteorder_big_p
? "big" : "little",
3077 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3079 #endif /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3082 /* Functions to manipulate command line editing control variables. */
3084 /* Number of commands to print in each call to show_commands. */
3085 #define Hist_print 10
3087 show_commands (args
, from_tty
)
3091 /* Index for history commands. Relative to history_base. */
3094 /* Number of the history entry which we are planning to display next.
3095 Relative to history_base. */
3098 /* The first command in the history which doesn't exist (i.e. one more
3099 than the number of the last command). Relative to history_base. */
3102 extern HIST_ENTRY
*history_get
PARAMS ((int));
3104 /* Print out some of the commands from the command history. */
3105 /* First determine the length of the history list. */
3106 hist_len
= history_size
;
3107 for (offset
= 0; offset
< history_size
; offset
++)
3109 if (!history_get (history_base
+ offset
))
3118 if (args
[0] == '+' && args
[1] == '\0')
3119 /* "info editing +" should print from the stored position. */
3122 /* "info editing <exp>" should print around command number <exp>. */
3123 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3125 /* "show commands" means print the last Hist_print commands. */
3128 num
= hist_len
- Hist_print
;
3134 /* If there are at least Hist_print commands, we want to display the last
3135 Hist_print rather than, say, the last 6. */
3136 if (hist_len
- num
< Hist_print
)
3138 num
= hist_len
- Hist_print
;
3143 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3145 printf_filtered ("%5d %s\n", history_base
+ offset
,
3146 (history_get (history_base
+ offset
))->line
);
3149 /* The next command we want to display is the next one that we haven't
3153 /* If the user repeats this command with return, it should do what
3154 "show commands +" does. This is unnecessary if arg is null,
3155 because "show commands +" is not useful after "show commands". */
3156 if (from_tty
&& args
)
3163 /* Called by do_setshow_command. */
3166 set_history_size_command (args
, from_tty
, c
)
3169 struct cmd_list_element
*c
;
3171 if (history_size
== INT_MAX
)
3172 unstifle_history ();
3173 else if (history_size
>= 0)
3174 stifle_history (history_size
);
3177 history_size
= INT_MAX
;
3178 error ("History size must be non-negative");
3184 set_history (args
, from_tty
)
3188 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3189 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3194 show_history (args
, from_tty
)
3198 cmd_show_list (showhistlist
, from_tty
, "");
3201 int info_verbose
= 0; /* Default verbose msgs off */
3203 /* Called by do_setshow_command. An elaborate joke. */
3206 set_verbose (args
, from_tty
, c
)
3209 struct cmd_list_element
*c
;
3211 char *cmdname
= "verbose";
3212 struct cmd_list_element
*showcmd
;
3214 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3218 c
->doc
= "Set verbose printing of informational messages.";
3219 showcmd
->doc
= "Show verbose printing of informational messages.";
3223 c
->doc
= "Set verbosity.";
3224 showcmd
->doc
= "Show verbosity.";
3229 float_handler (signo
)
3232 /* This message is based on ANSI C, section 4.7. Note that integer
3233 divide by zero causes this, so "float" is a misnomer. */
3234 signal (SIGFPE
, float_handler
);
3235 error ("Erroneous arithmetic operation.");
3247 enablebreaklist
= NULL
;
3251 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3255 showhistlist
= NULL
;
3256 unsethistlist
= NULL
;
3257 #if MAINTENANCE_CMDS
3258 maintenancelist
= NULL
;
3259 maintenanceinfolist
= NULL
;
3260 maintenanceprintlist
= NULL
;
3262 setprintlist
= NULL
;
3263 showprintlist
= NULL
;
3264 setchecklist
= NULL
;
3265 showchecklist
= NULL
;
3268 /* Init the history buffer. Note that we are called after the init file(s)
3269 * have been read so that the user can change the history file via his
3270 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3271 * overrides all of this.
3279 tmpenv
= getenv ("HISTSIZE");
3281 history_size
= atoi (tmpenv
);
3282 else if (!history_size
)
3285 stifle_history (history_size
);
3287 tmpenv
= getenv ("GDBHISTFILE");
3289 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3290 else if (!history_filename
) {
3291 /* We include the current directory so that if the user changes
3292 directories the file written will be the same as the one
3294 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3296 read_history (history_filename
);
3302 struct cmd_list_element
*c
;
3304 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3306 add_prefix_cmd ("endian", class_support
, set_endian
,
3307 "Set endianness of target.",
3308 &endianlist
, "set endian ", 0, &setlist
);
3309 add_cmd ("big", class_support
, set_endian_big
,
3310 "Set target as being big endian.", &endianlist
);
3311 add_cmd ("little", class_support
, set_endian_little
,
3312 "Set target as being little endian.", &endianlist
);
3313 add_cmd ("auto", class_support
, set_endian_auto
,
3314 "Select target endianness automatically.", &endianlist
);
3315 add_cmd ("endian", class_support
, show_endian
,
3316 "Show endianness of target.", &showlist
);
3318 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3320 #ifdef DEFAULT_PROMPT
3321 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3323 prompt
= savestring ("(gdb) ", 6);
3326 /* Set the important stuff up for command editing. */
3327 command_editing_p
= 1;
3328 history_expansion_p
= 0;
3329 write_history_p
= 0;
3331 /* Setup important stuff for command line editing. */
3332 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3333 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3334 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3335 rl_readline_name
= "gdb";
3337 /* Define the classes of commands.
3338 They will appear in the help list in the reverse of this order. */
3340 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3341 "Maintenance commands.\n\
3342 Some gdb commands are provided just for use by gdb maintainers.\n\
3343 These commands are subject to frequent change, and may not be as\n\
3344 well documented as user commands.",
3346 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3347 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3348 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3349 The commands in this class are those defined by the user.\n\
3350 Use the \"define\" command to define a command.", &cmdlist
);
3351 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3352 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3353 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3354 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3355 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3356 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3357 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3358 counting from zero for the innermost (currently executing) frame.\n\n\
3359 At any time gdb identifies one frame as the \"selected\" frame.\n\
3360 Variable lookups are done with respect to the selected frame.\n\
3361 When the program being debugged stops, gdb selects the innermost frame.\n\
3362 The commands below can be used to select other frames by number or address.",
3364 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3366 add_com ("pwd", class_files
, pwd_command
,
3367 "Print working directory. This is used for your program as well.");
3368 c
= add_cmd ("cd", class_files
, cd_command
,
3369 "Set working directory to DIR for debugger and program being debugged.\n\
3370 The change does not take effect for the program being debugged\n\
3371 until the next time it is started.", &cmdlist
);
3372 c
->completer
= filename_completer
;
3375 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3380 add_com ("echo", class_support
, echo_command
,
3381 "Print a constant string. Give string as argument.\n\
3382 C escape sequences may be used in the argument.\n\
3383 No newline is added at the end of the argument;\n\
3384 use \"\\n\" if you want a newline to be printed.\n\
3385 Since leading and trailing whitespace are ignored in command arguments,\n\
3386 if you want to print some you must use \"\\\" before leading whitespace\n\
3387 to be printed or after trailing whitespace.");
3388 add_com ("document", class_support
, document_command
,
3389 "Document a user-defined command.\n\
3390 Give command name as argument. Give documentation on following lines.\n\
3391 End with a line of just \"end\".");
3392 add_com ("define", class_support
, define_command
,
3393 "Define a new command name. Command name is argument.\n\
3394 Definition appears on following lines, one command per line.\n\
3395 End with a line of just \"end\".\n\
3396 Use the \"document\" command to give documentation for the new command.\n\
3397 Commands defined in this way may have up to ten arguments.");
3400 c
= add_cmd ("source", class_support
, source_command
,
3401 "Read commands from a file named FILE.\n\
3402 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3403 when gdb is started.", &cmdlist
);
3405 /* Punt file name, we can't help it easily. */
3406 c
= add_cmd ("source", class_support
, source_command
,
3407 "Read commands from a file named FILE.\n\
3408 Note that the file \".gdbinit\" is read automatically in this way\n\
3409 when gdb is started.", &cmdlist
);
3411 c
->completer
= filename_completer
;
3413 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3414 add_com ("help", class_support
, help_command
, "Print list of commands.");
3415 add_com_alias ("q", "quit", class_support
, 1);
3416 add_com_alias ("h", "help", class_support
, 1);
3419 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3422 add_show_from_set (c
, &showlist
);
3423 c
->function
.sfunc
= set_verbose
;
3424 set_verbose (NULL
, 0, c
);
3427 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3428 "Set editing of command lines as they are typed.\n\
3429 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
3430 Without an argument, command line editing is enabled. To edit, use\n\
3431 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3434 add_prefix_cmd ("history", class_support
, set_history
,
3435 "Generic command for setting command history parameters.",
3436 &sethistlist
, "set history ", 0, &setlist
);
3437 add_prefix_cmd ("history", class_support
, show_history
,
3438 "Generic command for showing command history parameters.",
3439 &showhistlist
, "show history ", 0, &showlist
);
3442 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3443 "Set history expansion on command input.\n\
3444 Without an argument, history expansion is enabled.", &sethistlist
),
3448 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3449 "Set saving of the history record on exit.\n\
3450 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
3451 Without an argument, saving is enabled.", &sethistlist
),
3454 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3455 "Set the size of the command history, \n\
3456 ie. the number of previous commands to keep a record of.", &sethistlist
);
3457 add_show_from_set (c
, &showhistlist
);
3458 c
->function
.sfunc
= set_history_size_command
;
3461 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3462 "Set the filename in which to record the command history\n\
3463 (the list of previous commands of which a record is kept).", &sethistlist
),
3467 (add_set_cmd ("confirm", class_support
, var_boolean
,
3469 "Set whether to confirm potentially dangerous operations.",
3473 add_prefix_cmd ("info", class_info
, info_command
,
3474 "Generic command for showing things about the program being debugged.",
3475 &infolist
, "info ", 0, &cmdlist
);
3476 add_com_alias ("i", "info", class_info
, 1);
3478 add_com ("complete", class_obscure
, complete_command
,
3479 "List the completions for the rest of the line as a command.");
3481 add_prefix_cmd ("show", class_info
, show_command
,
3482 "Generic command for showing things about the debugger.",
3483 &showlist
, "show ", 0, &cmdlist
);
3484 /* Another way to get at the same thing. */
3485 add_info ("set", show_command
, "Show all GDB settings.");
3487 add_cmd ("commands", no_class
, show_commands
,
3488 "Show the the history of commands you typed.\n\
3489 You can supply a command number to start with, or a `+' to start after\n\
3490 the previous command number shown.",
3493 add_cmd ("version", no_class
, show_version
,
3494 "Show what version of GDB this is.", &showlist
);
3496 add_com ("while", class_support
, while_command
,
3497 "Execute nested commands WHILE the conditional expression is non zero.\n\
3498 The conditional expression must follow the word `while' and must in turn be\
3499 followed by a new line. The nested commands must be entered one per line,\
3500 and should be terminated by the word `end'.");
3502 add_com ("if", class_support
, if_command
,
3503 "Execute nested commands once IF the conditional expression is non zero.\n\
3504 The conditional expression must follow the word `if' and must in turn be\
3505 followed by a new line. The nested commands must be entered one per line,\
3506 and should be terminated by the word 'else' or `end'. If an else clause\
3507 is used, the same rules apply to its nested commands as to the first ones.");
3509 /* If target is open when baud changes, it doesn't take effect until the
3510 next open (I think, not sure). */
3511 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3512 var_zinteger
, (char *)&baud_rate
,
3513 "Set baud rate for remote serial I/O.\n\
3514 This value is used to set the speed of the serial port when debugging\n\
3515 using remote targets.", &setlist
),
3519 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3520 "Set debugging of remote protocol.\n\
3521 When enabled, each packet sent or received with the remote target\n\
3522 is displayed.", &setlist
),