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 /* 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 the registers have changed, as a hint to a GUI
403 to minimize window update. */
405 void (*registers_changed_hook
) PARAMS ((void));
407 /* Called when going to wait for the target. Usually allows the GUI to run
408 while waiting for target events. */
410 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
*status
));
412 /* Used by UI as a wrapper around command execution. May do various things
413 like enabling/disabling buttons, etc... */
415 void (*call_command_hook
) PARAMS ((struct cmd_list_element
*c
, char *cmd
,
418 /* An alternate way to read memory for disassembly. This is used to provide a
419 switch that allows disassembly to come from an exec file rather than a
420 remote target. This is a speed hack. */
422 int (*dis_asm_read_memory_hook
) PARAMS ((bfd_vma memaddr
, bfd_byte
*myaddr
,
423 int len
, disassemble_info
*info
));
424 /* Takes control from error (). Typically used to prevent longjmps out of the
425 middle of the GUI. Usually used in conjunction with a catch routine. */
427 NORETURN
void (*error_hook
) PARAMS (());
430 /* Where to go for return_to_top_level (RETURN_ERROR). */
431 jmp_buf error_return
;
432 /* Where to go for return_to_top_level (RETURN_QUIT). */
435 /* Return for reason REASON. This generally gets back to the command
436 loop, but can be caught via catch_errors. */
439 return_to_top_level (reason
)
440 enum return_reason reason
;
445 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
446 I can think of a reason why that is vital, though). */
447 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
449 disable_current_display ();
450 do_cleanups (ALL_CLEANUPS
);
452 if (annotation_level
> 1)
463 (NORETURN
void) longjmp
464 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
467 /* Call FUNC with arg ARGS, catching any errors. If there is no
468 error, return the value returned by FUNC. If there is an error,
469 print ERRSTRING, print the specific error message, then return
472 Must not be called with immediate_quit in effect (bad things might
473 happen, say we got a signal in the middle of a memcpy to quit_return).
474 This is an OK restriction; with very few exceptions immediate_quit can
475 be replaced by judicious use of QUIT.
477 MASK specifies what to catch; it is normally set to
478 RETURN_MASK_ALL, if for no other reason than that the code which
479 calls catch_errors might not be set up to deal with a quit which
480 isn't caught. But if the code can deal with it, it generally
481 should be RETURN_MASK_ERROR, unless for some reason it is more
482 useful to abort only the portion of the operation inside the
483 catch_errors. Note that quit should return to the command line
484 fairly quickly, even if some further processing is being done. */
487 catch_errors (func
, args
, errstring
, mask
)
488 int (*func
) PARAMS ((char *));
497 struct cleanup
*saved_cleanup_chain
;
498 char *saved_error_pre_print
;
500 saved_cleanup_chain
= save_cleanups ();
501 saved_error_pre_print
= error_pre_print
;
503 if (mask
& RETURN_MASK_ERROR
)
504 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (jmp_buf));
505 if (mask
& RETURN_MASK_QUIT
)
506 memcpy (saved_quit
, quit_return
, sizeof (jmp_buf));
507 error_pre_print
= errstring
;
509 if (setjmp (tmp_jmp
) == 0)
511 if (mask
& RETURN_MASK_ERROR
)
512 memcpy (error_return
, tmp_jmp
, sizeof (jmp_buf));
513 if (mask
& RETURN_MASK_QUIT
)
514 memcpy (quit_return
, tmp_jmp
, sizeof (jmp_buf));
515 val
= (*func
) (args
);
520 restore_cleanups (saved_cleanup_chain
);
522 error_pre_print
= saved_error_pre_print
;
523 if (mask
& RETURN_MASK_ERROR
)
524 memcpy (error_return
, saved_error
, sizeof (jmp_buf));
525 if (mask
& RETURN_MASK_QUIT
)
526 memcpy (quit_return
, saved_quit
, sizeof (jmp_buf));
530 /* Handler for SIGHUP. */
536 catch_errors (quit_cover
, NULL
,
537 "Could not kill the program being debugged", RETURN_MASK_ALL
);
538 signal (SIGHUP
, SIG_DFL
);
539 kill (getpid (), SIGHUP
);
542 /* Just a little helper function for disconnect(). */
548 caution
= 0; /* Throw caution to the wind -- we're exiting.
549 This prevents asking the user dumb questions. */
550 quit_command((char *)0, 0);
554 /* Line number we are currently in in a file which is being sourced. */
555 static int source_line_number
;
557 /* Name of the file we are sourcing. */
558 static char *source_file_name
;
560 /* Buffer containing the error_pre_print used by the source stuff.
562 static char *source_error
;
563 static int source_error_allocated
;
565 /* Something to glom on to the start of error_pre_print if source_file_name
567 static char *source_pre_error
;
569 /* Clean up on error during a "source" command (or execution of a
570 user-defined command). */
573 source_cleanup (stream
)
576 /* Restore the previous input stream. */
580 /* Read commands from STREAM. */
582 read_command_file (stream
)
585 struct cleanup
*cleanups
;
587 cleanups
= make_cleanup (source_cleanup
, instream
);
590 do_cleanups (cleanups
);
593 extern void init_proc ();
595 void (*pre_init_ui_hook
) PARAMS ((void));
600 if (pre_init_ui_hook
)
603 /* Run the init function of each source file */
605 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
606 current_directory
= gdb_dirbuf
;
608 init_cmd_lists (); /* This needs to be done first */
609 initialize_targets (); /* Setup target_terminal macros for utils.c */
610 initialize_utils (); /* Make errors and warnings possible */
611 initialize_all_files ();
612 init_main (); /* But that omits this file! Do it now */
617 /* We need a default language for parsing expressions, so simple things like
618 "set width 0" won't fail if no language is explicitly set in a config file
619 or implicitly set by reading an executable during startup. */
620 set_language (language_c
);
621 expected_language
= current_language
; /* don't warn about the change. */
627 /* Allocate, initialize a new command line structure for one of the
628 control commands (if/while). */
630 static struct command_line
*
631 build_command_line (type
, args
)
632 enum command_control_type type
;
635 struct command_line
*cmd
;
637 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
639 cmd
->control_type
= type
;
643 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
645 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
646 cmd
->line
= savestring (args
, strlen (args
));
650 /* Build and return a new command structure for the control commands
651 such as "if" and "while". */
653 static struct command_line
*
654 get_command_line (type
, arg
)
655 enum command_control_type type
;
658 struct command_line
*cmd
;
659 struct cleanup
*old_chain
= NULL
;
661 /* Allocate and build a new command line structure. */
662 cmd
= build_command_line (type
, arg
);
664 old_chain
= make_cleanup (free_command_lines
, &cmd
);
666 /* Read in the body of this command. */
667 if (recurse_read_control_structure (cmd
) == invalid_control
)
669 warning ("error reading in control structure\n");
670 do_cleanups (old_chain
);
674 discard_cleanups (old_chain
);
678 /* Recursively print a command (including full control structures). */
680 print_command_line (cmd
, depth
)
681 struct command_line
*cmd
;
688 for (i
= 0; i
< depth
; i
++)
689 fputs_filtered (" ", gdb_stdout
);
692 /* A simple command, print it and return. */
693 if (cmd
->control_type
== simple_control
)
695 fputs_filtered (cmd
->line
, gdb_stdout
);
696 fputs_filtered ("\n", gdb_stdout
);
700 /* loop_continue to jump to the start of a while loop, print it
702 if (cmd
->control_type
== continue_control
)
704 fputs_filtered ("loop_continue\n", gdb_stdout
);
708 /* loop_break to break out of a while loop, print it and return. */
709 if (cmd
->control_type
== break_control
)
711 fputs_filtered ("loop_break\n", gdb_stdout
);
715 /* A while command. Recursively print its subcommands before returning. */
716 if (cmd
->control_type
== while_control
)
718 struct command_line
*list
;
719 fputs_filtered ("while ", gdb_stdout
);
720 fputs_filtered (cmd
->line
, gdb_stdout
);
721 fputs_filtered ("\n", gdb_stdout
);
722 list
= *cmd
->body_list
;
725 print_command_line (list
, depth
+ 1);
730 /* An if command. Recursively print both arms before returning. */
731 if (cmd
->control_type
== if_control
)
733 fputs_filtered ("if ", gdb_stdout
);
734 fputs_filtered (cmd
->line
, gdb_stdout
);
735 fputs_filtered ("\n", gdb_stdout
);
737 print_command_line (cmd
->body_list
[0], depth
+ 1);
739 /* Show the false arm if it exists. */
740 if (cmd
->body_count
== 2)
744 for (i
= 0; i
< depth
; i
++)
745 fputs_filtered (" ", gdb_stdout
);
747 fputs_filtered ("else\n", gdb_stdout
);
748 print_command_line (cmd
->body_list
[1], depth
+ 1);
752 for (i
= 0; i
< depth
; i
++)
753 fputs_filtered (" ", gdb_stdout
);
755 fputs_filtered ("end\n", gdb_stdout
);
759 /* Execute the command in CMD. */
761 enum command_control_type
762 execute_control_command (cmd
)
763 struct command_line
*cmd
;
765 struct expression
*expr
;
766 struct command_line
*current
;
767 struct cleanup
*old_chain
= 0;
770 enum command_control_type ret
;
773 switch (cmd
->control_type
)
776 /* A simple command, execute it and return. */
777 new_line
= insert_args (cmd
->line
);
779 return invalid_control
;
780 old_chain
= make_cleanup (free_current_contents
, &new_line
);
781 execute_command (new_line
, 0);
782 ret
= cmd
->control_type
;
785 case continue_control
:
787 /* Return for "continue", and "break" so we can either
788 continue the loop at the top, or break out. */
789 ret
= cmd
->control_type
;
794 /* Parse the loop control expression for the while statement. */
795 new_line
= insert_args (cmd
->line
);
797 return invalid_control
;
798 old_chain
= make_cleanup (free_current_contents
, &new_line
);
799 expr
= parse_expression (new_line
);
800 make_cleanup (free_current_contents
, &expr
);
802 ret
= simple_control
;
805 /* Keep iterating so long as the expression is true. */
808 /* Evaluate the expression. */
809 val
= evaluate_expression (expr
);
811 /* If the value is false, then break out of the loop. */
812 if (!value_true (val
))
815 /* Execute the body of the while statement. */
816 current
= *cmd
->body_list
;
819 ret
= execute_control_command (current
);
821 /* If we got an error, or a "break" command, then stop
823 if (ret
== invalid_control
|| ret
== break_control
)
829 /* If we got a "continue" command, then restart the loop
831 if (ret
== continue_control
)
834 /* Get the next statement. */
835 current
= current
->next
;
839 /* Reset RET so that we don't recurse the break all the way down. */
840 if (ret
== break_control
)
841 ret
= simple_control
;
848 new_line
= insert_args (cmd
->line
);
850 return invalid_control
;
851 old_chain
= make_cleanup (free_current_contents
, &new_line
);
852 /* Parse the conditional for the if statement. */
853 expr
= parse_expression (new_line
);
854 make_cleanup (free_current_contents
, &expr
);
857 ret
= simple_control
;
859 /* Evaluate the conditional. */
860 val
= evaluate_expression (expr
);
862 /* Choose which arm to take commands from based on the value of the
863 conditional expression. */
864 if (value_true (val
))
865 current
= *cmd
->body_list
;
866 else if (cmd
->body_count
== 2)
867 current
= *(cmd
->body_list
+ 1);
869 /* Execute commands in the given arm. */
872 ret
= execute_control_command (current
);
874 /* If we got an error, get out. */
875 if (ret
!= simple_control
)
878 /* Get the next statement in the body. */
879 current
= current
->next
;
886 warning ("Invalid control type in command structure.");
887 return invalid_control
;
891 do_cleanups (old_chain
);
896 /* "while" command support. Executes a body of statements while the
897 loop condition is nonzero. */
900 while_command (arg
, from_tty
)
904 struct command_line
*command
= NULL
;
907 command
= get_command_line (while_control
, arg
);
912 execute_control_command (command
);
913 free_command_lines (&command
);
916 /* "if" command support. Execute either the true or false arm depending
917 on the value of the if conditional. */
920 if_command (arg
, from_tty
)
924 struct command_line
*command
= NULL
;
927 command
= get_command_line (if_control
, arg
);
932 execute_control_command (command
);
933 free_command_lines (&command
);
940 struct user_args
*oargs
= user_args
;
942 fatal ("Internal error, arg_cleanup called with no user args.\n");
944 user_args
= user_args
->next
;
948 /* Bind the incomming arguments for a user defined command to
949 $arg0, $arg1 ... $argMAXUSERARGS. */
951 static struct cleanup
*
955 struct user_args
*args
;
956 struct cleanup
*old_chain
;
957 unsigned int arg_count
= 0;
959 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
960 memset (args
, 0, sizeof (struct user_args
));
962 args
->next
= user_args
;
965 old_chain
= make_cleanup (arg_cleanup
, 0);
974 if (arg_count
>= MAXUSERARGS
)
976 error ("user defined function may only have %d arguments.\n",
981 /* Strip whitespace. */
982 while (*p
== ' ' || *p
== '\t')
985 /* P now points to an argument. */
987 user_args
->a
[arg_count
].arg
= p
;
989 /* Get to the end of this argument. */
990 while (*p
&& *p
!= ' ' && *p
!= '\t')
993 user_args
->a
[arg_count
].len
= p
- start_arg
;
1000 /* Given character string P, return a point to the first argument ($arg),
1001 or NULL if P contains no arguments. */
1007 while (p
= strchr (p
, '$'))
1009 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1016 /* Insert the user defined arguments stored in user_arg into the $arg
1017 arguments found in line, with the updated copy being placed into nline. */
1023 char *p
, *save_line
, *new_line
;
1026 /* First we need to know how much memory to allocate for the new line. */
1029 while (p
= locate_arg (line
))
1034 if (i
>= user_args
->count
)
1036 error ("Missing argument %d in user function.\n", i
);
1039 len
+= user_args
->a
[i
].len
;
1043 /* Don't forget the tail. */
1044 len
+= strlen (line
);
1046 /* Allocate space for the new line and fill it in. */
1047 new_line
= (char *)xmalloc (len
+ 1);
1048 if (new_line
== NULL
)
1051 /* Restore pointer to beginning of old line. */
1054 /* Save pointer to beginning of new line. */
1055 save_line
= new_line
;
1057 while (p
= locate_arg (line
))
1061 memcpy (new_line
, line
, p
- line
);
1062 new_line
+= p
- line
;
1065 if (len
= user_args
->a
[i
].len
)
1067 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1072 /* Don't forget the tail. */
1073 strcpy (new_line
, line
);
1075 /* Return a pointer to the beginning of the new line. */
1080 execute_user_command (c
, args
)
1081 struct cmd_list_element
*c
;
1084 register struct command_line
*cmdlines
;
1085 struct cleanup
*old_chain
;
1086 enum command_control_type ret
;
1088 old_chain
= setup_user_args (args
);
1090 cmdlines
= c
->user_commands
;
1095 /* Set the instream to 0, indicating execution of a
1096 user-defined function. */
1097 old_chain
= make_cleanup (source_cleanup
, instream
);
1098 instream
= (FILE *) 0;
1101 ret
= execute_control_command (cmdlines
);
1102 if (ret
!= simple_control
&& ret
!= break_control
)
1104 warning ("Error in control structure.\n");
1107 cmdlines
= cmdlines
->next
;
1109 do_cleanups (old_chain
);
1112 /* Execute the line P as a command.
1113 Pass FROM_TTY as second argument to the defining function. */
1116 execute_command (p
, from_tty
)
1120 register struct cmd_list_element
*c
;
1121 register enum language flang
;
1122 static int warned
= 0;
1126 /* This can happen when command_line_input hits end of file. */
1130 while (*p
== ' ' || *p
== '\t') p
++;
1135 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1136 /* Pass null arg rather than an empty one. */
1139 /* If this command has been hooked, run the hook first. */
1141 execute_user_command (c
->hook
, (char *)0);
1143 if (c
->class == class_user
)
1144 execute_user_command (c
, arg
);
1145 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1146 do_setshow_command (arg
, from_tty
& caution
, c
);
1147 else if (c
->function
.cfunc
== NO_FUNCTION
)
1148 error ("That is not a command, just a help topic.");
1149 else if (call_command_hook
)
1150 call_command_hook (c
, arg
, from_tty
& caution
);
1152 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1155 /* Tell the user if the language has changed (except first time). */
1156 if (current_language
!= expected_language
)
1158 if (language_mode
== language_mode_auto
) {
1159 language_info (1); /* Print what changed. */
1164 /* Warn the user if the working language does not match the
1165 language of the current frame. Only warn the user if we are
1166 actually running the program, i.e. there is a stack. */
1167 /* FIXME: This should be cacheing the frame and only running when
1168 the frame changes. */
1170 if (target_has_stack
)
1172 flang
= get_frame_language ();
1174 && flang
!= language_unknown
1175 && flang
!= current_language
->la_language
)
1177 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1185 command_loop_marker (foo
)
1190 /* Read commands from `instream' and execute them
1191 until end of file or error reading instream. */
1196 struct cleanup
*old_chain
;
1198 int stdin_is_tty
= ISATTY (stdin
);
1199 long time_at_cmd_start
;
1200 long space_at_cmd_start
;
1201 extern int display_time
;
1202 extern int display_space
;
1204 while (!feof (instream
))
1206 if (window_hook
&& instream
== stdin
)
1207 (*window_hook
) (instream
, prompt
);
1210 if (instream
== stdin
&& stdin_is_tty
)
1211 reinitialize_more_filter ();
1212 old_chain
= make_cleanup (command_loop_marker
, 0);
1213 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1214 instream
== stdin
, "prompt");
1218 time_at_cmd_start
= get_run_time ();
1222 extern char **environ
;
1223 char *lim
= (char *) sbrk (0);
1225 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1228 execute_command (command
, instream
== stdin
);
1229 /* Do any commands attached to breakpoint we stopped at. */
1230 bpstat_do_actions (&stop_bpstat
);
1231 do_cleanups (old_chain
);
1235 long cmd_time
= get_run_time () - time_at_cmd_start
;
1237 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1238 cmd_time
/ 1000000, cmd_time
% 1000000);
1243 extern char **environ
;
1244 char *lim
= (char *) sbrk (0);
1245 long space_now
= lim
- (char *) &environ
;
1246 long space_diff
= space_now
- space_at_cmd_start
;
1248 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1250 (space_diff
>= 0 ? '+' : '-'),
1256 /* Commands call this if they do not want to be repeated by null lines. */
1264 /* If we aren't reading from standard input, we are saving the last
1265 thing read from stdin in line and don't want to delete it. Null lines
1266 won't repeat here in any case. */
1267 if (instream
== stdin
)
1271 /* Read a line from the stream "instream" without command line editing.
1273 It prints PRROMPT once at the start.
1274 Action is compatible with "readline", e.g. space for the result is
1275 malloc'd and should be freed by the caller.
1277 A NULL return means end of file. */
1279 gdb_readline (prrompt
)
1284 int input_index
= 0;
1285 int result_size
= 80;
1289 /* Don't use a _filtered function here. It causes the assumed
1290 character position to be off, since the newline we read from
1291 the user is not accounted for. */
1292 fputs_unfiltered (prrompt
, gdb_stdout
);
1293 /* start-sanitize-mpw */
1295 /* Move to a new line so the entered line doesn't have a prompt
1296 on the front of it. */
1297 fputs_unfiltered ("\n", gdb_stdout
);
1299 /* end-sanitize-mpw */
1300 gdb_flush (gdb_stdout
);
1303 result
= (char *) xmalloc (result_size
);
1307 /* Read from stdin if we are executing a user defined command.
1308 This is the right thing for prompt_for_continue, at least. */
1309 c
= fgetc (instream
? instream
: stdin
);
1313 if (input_index
> 0)
1314 /* The last line does not end with a newline. Return it, and
1315 if we are called again fgetc will still return EOF and
1316 we'll return NULL then. */
1325 result
[input_index
++] = c
;
1326 while (input_index
>= result_size
)
1329 result
= (char *) xrealloc (result
, result_size
);
1333 result
[input_index
++] = '\0';
1337 /* Variables which control command line editing and history
1338 substitution. These variables are given default values at the end
1340 static int command_editing_p
;
1341 static int history_expansion_p
;
1342 static int write_history_p
;
1343 static int history_size
;
1344 static char *history_filename
;
1346 /* readline uses the word breaks for two things:
1347 (1) In figuring out where to point the TEXT parameter to the
1348 rl_completion_entry_function. Since we don't use TEXT for much,
1349 it doesn't matter a lot what the word breaks are for this purpose, but
1350 it does affect how much stuff M-? lists.
1351 (2) If one of the matches contains a word break character, readline
1352 will quote it. That's why we switch between
1353 gdb_completer_word_break_characters and
1354 gdb_completer_command_word_break_characters. I'm not sure when
1355 we need this behavior (perhaps for funky characters in C++ symbols?). */
1357 /* Variables which are necessary for fancy command line editing. */
1358 char *gdb_completer_word_break_characters
=
1359 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1361 /* When completing on command names, we remove '-' from the list of
1362 word break characters, since we use it in command names. If the
1363 readline library sees one in any of the current completion strings,
1364 it thinks that the string needs to be quoted and automatically supplies
1366 char *gdb_completer_command_word_break_characters
=
1367 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1369 /* Characters that can be used to quote completion strings. Note that we
1370 can't include '"' because the gdb C parser treats such quoted sequences
1372 char *gdb_completer_quote_characters
=
1375 /* Functions that are used as part of the fancy command line editing. */
1377 /* This can be used for functions which don't want to complete on symbols
1378 but don't want to complete on anything else either. */
1381 noop_completer (text
, prefix
)
1388 /* Complete on filenames. */
1390 filename_completer (text
, word
)
1394 /* From readline. */
1395 extern char *filename_completion_function ();
1396 int subsequent_name
;
1398 int return_val_used
;
1399 int return_val_alloced
;
1401 return_val_used
= 0;
1402 /* Small for testing. */
1403 return_val_alloced
= 1;
1404 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1406 subsequent_name
= 0;
1410 p
= filename_completion_function (text
, subsequent_name
);
1411 if (return_val_used
>= return_val_alloced
)
1413 return_val_alloced
*= 2;
1415 (char **) xrealloc (return_val
,
1416 return_val_alloced
* sizeof (char *));
1420 return_val
[return_val_used
++] = p
;
1423 /* Like emacs, don't complete on old versions. Especially useful
1424 in the "source" command. */
1425 if (p
[strlen (p
) - 1] == '~')
1431 /* Return exactly p. */
1432 return_val
[return_val_used
++] = p
;
1433 else if (word
> text
)
1435 /* Return some portion of p. */
1436 q
= xmalloc (strlen (p
) + 5);
1437 strcpy (q
, p
+ (word
- text
));
1438 return_val
[return_val_used
++] = q
;
1443 /* Return some of TEXT plus p. */
1444 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1445 strncpy (q
, word
, text
- word
);
1446 q
[text
- word
] = '\0';
1448 return_val
[return_val_used
++] = q
;
1452 subsequent_name
= 1;
1455 /* There is no way to do this just long enough to affect quote inserting
1456 without also affecting the next completion. This should be fixed in
1458 /* Insure that readline does the right thing
1459 with respect to inserting quotes. */
1460 rl_completer_word_break_characters
= "";
1465 /* Here are some useful test cases for completion. FIXME: These should
1466 be put in the test suite. They should be tested with both M-? and TAB.
1468 "show output-" "radix"
1469 "show output" "-radix"
1470 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1471 "p " ambiguous (all symbols)
1472 "info t foo" no completions
1473 "info t " no completions
1474 "info t" ambiguous ("info target", "info terminal", etc.)
1475 "info ajksdlfk" no completions
1476 "info ajksdlfk " no completions
1478 "info " ambiguous (all info commands)
1479 "p \"a" no completions (string constant)
1480 "p 'a" ambiguous (all symbols starting with a)
1481 "p b-a" ambiguous (all symbols starting with a)
1482 "p b-" ambiguous (all symbols)
1483 "file Make" "file" (word break hard to screw up here)
1484 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1487 /* Generate completions one by one for the completer. Each time we are
1488 called return another potential completion to the caller.
1489 line_completion just completes on commands or passes the buck to the
1490 command's completer function, the stuff specific to symbol completion
1491 is in make_symbol_completion_list.
1493 TEXT is the caller's idea of the "word" we are looking at.
1495 MATCHES is the number of matches that have currently been collected from
1496 calling this completion function. When zero, then we need to initialize,
1497 otherwise the initialization has already taken place and we can just
1498 return the next potential completion string.
1500 LINE_BUFFER is available to be looked at; it contains the entire text
1501 of the line. POINT is the offset in that line of the cursor. You
1502 should pretend that the line ends at POINT.
1504 Returns NULL if there are no more completions, else a pointer to a string
1505 which is a possible completion, it is the caller's responsibility to
1509 line_completion_function (text
, matches
, line_buffer
, point
)
1515 static char **list
= (char **)NULL
; /* Cache of completions */
1516 static int index
; /* Next cached completion */
1517 char *output
= NULL
;
1518 char *tmp_command
, *p
;
1519 /* Pointer within tmp_command which corresponds to text. */
1521 struct cmd_list_element
*c
, *result_list
;
1525 /* The caller is beginning to accumulate a new set of completions, so
1526 we need to find all of them now, and cache them for returning one at
1527 a time on future calls. */
1531 /* Free the storage used by LIST, but not by the strings inside.
1532 This is because rl_complete_internal () frees the strings. */
1538 /* Choose the default set of word break characters to break completions.
1539 If we later find out that we are doing completions on command strings
1540 (as opposed to strings supplied by the individual command completer
1541 functions, which can be any string) then we will switch to the
1542 special word break set for command strings, which leaves out the
1543 '-' character used in some commands. */
1545 rl_completer_word_break_characters
=
1546 gdb_completer_word_break_characters
;
1548 /* Decide whether to complete on a list of gdb commands or on symbols. */
1549 tmp_command
= (char *) alloca (point
+ 1);
1552 strncpy (tmp_command
, line_buffer
, point
);
1553 tmp_command
[point
] = '\0';
1554 /* Since text always contains some number of characters leading up
1555 to point, we can find the equivalent position in tmp_command
1556 by subtracting that many characters from the end of tmp_command. */
1557 word
= tmp_command
+ point
- strlen (text
);
1561 /* An empty line we want to consider ambiguous; that is, it
1562 could be any command. */
1563 c
= (struct cmd_list_element
*) -1;
1568 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1571 /* Move p up to the next interesting thing. */
1572 while (*p
== ' ' || *p
== '\t')
1579 /* It is an unrecognized command. So there are no
1580 possible completions. */
1583 else if (c
== (struct cmd_list_element
*) -1)
1587 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1588 doesn't advance over that thing itself. Do so now. */
1590 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1592 if (q
!= tmp_command
+ point
)
1594 /* There is something beyond the ambiguous
1595 command, so there are no possible completions. For
1596 example, "info t " or "info t foo" does not complete
1597 to anything, because "info t" can be "info target" or
1603 /* We're trying to complete on the command which was ambiguous.
1604 This we can deal with. */
1607 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1612 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1614 /* Insure that readline does the right thing with respect to
1615 inserting quotes. */
1616 rl_completer_word_break_characters
=
1617 gdb_completer_command_word_break_characters
;
1622 /* We've recognized a full command. */
1624 if (p
== tmp_command
+ point
)
1626 /* There is no non-whitespace in the line beyond the command. */
1628 if (p
[-1] == ' ' || p
[-1] == '\t')
1630 /* The command is followed by whitespace; we need to complete
1631 on whatever comes after command. */
1634 /* It is a prefix command; what comes after it is
1635 a subcommand (e.g. "info "). */
1636 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1638 /* Insure that readline does the right thing
1639 with respect to inserting quotes. */
1640 rl_completer_word_break_characters
=
1641 gdb_completer_command_word_break_characters
;
1645 /* It is a normal command; what comes after it is
1646 completed by the command's completer function. */
1647 list
= (*c
->completer
) (p
, word
);
1652 /* The command is not followed by whitespace; we need to
1653 complete on the command itself. e.g. "p" which is a
1654 command itself but also can complete to "print", "ptype"
1658 /* Find the command we are completing on. */
1660 while (q
> tmp_command
)
1662 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1668 list
= complete_on_cmdlist (result_list
, q
, word
);
1670 /* Insure that readline does the right thing
1671 with respect to inserting quotes. */
1672 rl_completer_word_break_characters
=
1673 gdb_completer_command_word_break_characters
;
1678 /* There is non-whitespace beyond the command. */
1680 if (c
->prefixlist
&& !c
->allow_unknown
)
1682 /* It is an unrecognized subcommand of a prefix command,
1683 e.g. "info adsfkdj". */
1688 /* It is a normal command. */
1689 list
= (*c
->completer
) (p
, word
);
1695 /* If we found a list of potential completions during initialization then
1696 dole them out one at a time. The vector of completions is NULL
1697 terminated, so after returning the last one, return NULL (and continue
1698 to do so) each time we are called after that, until a new list is
1703 output
= list
[index
];
1711 /* Can't do this because readline hasn't yet checked the word breaks
1712 for figuring out whether to insert a quote. */
1714 /* Make sure the word break characters are set back to normal for the
1715 next time that readline tries to complete something. */
1716 rl_completer_word_break_characters
=
1717 gdb_completer_word_break_characters
;
1723 /* Line completion interface function for readline. */
1726 readline_line_completion_function (text
, matches
)
1730 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1733 /* Skip over a possibly quoted word (as defined by the quote characters
1734 and word break characters the completer uses). Returns pointer to the
1735 location after the "word". */
1741 char quote_char
= '\0';
1744 for (scan
= str
; *scan
!= '\0'; scan
++)
1746 if (quote_char
!= '\0')
1748 /* Ignore everything until the matching close quote char */
1749 if (*scan
== quote_char
)
1751 /* Found matching close quote. */
1756 else if (strchr (gdb_completer_quote_characters
, *scan
))
1758 /* Found start of a quoted string. */
1761 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1775 #if STOP_SIGNAL == SIGTSTP
1776 signal (SIGTSTP
, SIG_DFL
);
1778 kill (getpid (), SIGTSTP
);
1779 signal (SIGTSTP
, stop_sig
);
1781 signal (STOP_SIGNAL
, stop_sig
);
1783 printf_unfiltered ("%s", prompt
);
1784 gdb_flush (gdb_stdout
);
1786 /* Forget about any previous command -- null line now will do nothing. */
1789 #endif /* STOP_SIGNAL */
1791 /* Initialize signal handlers. */
1801 signal (SIGINT
, request_quit
);
1803 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1804 passed to the inferior, which we don't want. It would be
1805 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1806 on BSD4.3 systems using vfork, that can affect the
1807 GDB process as well as the inferior (the signal handling tables
1808 might be in memory, shared between the two). Since we establish
1809 a handler for SIGQUIT, when we call exec it will set the signal
1810 to SIG_DFL for us. */
1811 signal (SIGQUIT
, do_nothing
);
1812 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1813 signal (SIGHUP
, disconnect
);
1814 signal (SIGFPE
, float_handler
);
1816 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1817 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1821 /* Read one line from the command input stream `instream'
1822 into the local static buffer `linebuffer' (whose current length
1824 The buffer is made bigger as necessary.
1825 Returns the address of the start of the line.
1827 NULL is returned for end of file.
1829 *If* the instream == stdin & stdin is a terminal, the line read
1830 is copied into the file line saver (global var char *line,
1831 length linesize) so that it can be duplicated.
1833 This routine either uses fancy command line editing or
1834 simple input as the user has requested. */
1837 command_line_input (prrompt
, repeat
, annotation_suffix
)
1840 char *annotation_suffix
;
1842 static char *linebuffer
= 0;
1843 static unsigned linelength
= 0;
1847 char *local_prompt
= prrompt
;
1852 /* The annotation suffix must be non-NULL. */
1853 if (annotation_suffix
== NULL
)
1854 annotation_suffix
= "";
1856 if (annotation_level
> 1 && instream
== stdin
)
1858 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
1859 + strlen (annotation_suffix
) + 40);
1860 if (prrompt
== NULL
)
1861 local_prompt
[0] = '\0';
1863 strcpy (local_prompt
, prrompt
);
1864 strcat (local_prompt
, "\n\032\032");
1865 strcat (local_prompt
, annotation_suffix
);
1866 strcat (local_prompt
, "\n");
1869 if (linebuffer
== 0)
1872 linebuffer
= (char *) xmalloc (linelength
);
1877 /* Control-C quits instantly if typed while in this loop
1878 since it should not wait until the user types a newline. */
1882 signal (STOP_SIGNAL
, stop_sig
);
1887 /* Make sure that all output has been output. Some machines may let
1888 you get away with leaving out some of the gdb_flush, but not all. */
1890 gdb_flush (gdb_stdout
);
1891 gdb_flush (gdb_stderr
);
1893 if (source_file_name
!= NULL
)
1895 ++source_line_number
;
1896 sprintf (source_error
,
1897 "%s%s:%d: Error in sourced command file:\n",
1900 source_line_number
);
1901 error_pre_print
= source_error
;
1904 if (annotation_level
> 1 && instream
== stdin
)
1906 printf_unfiltered ("\n\032\032pre-");
1907 printf_unfiltered (annotation_suffix
);
1908 printf_unfiltered ("\n");
1911 /* Don't use fancy stuff if not talking to stdin. */
1912 if (command_editing_p
&& instream
== stdin
1913 && ISATTY (instream
))
1914 rl
= readline (local_prompt
);
1916 rl
= gdb_readline (local_prompt
);
1918 if (annotation_level
> 1 && instream
== stdin
)
1920 printf_unfiltered ("\n\032\032post-");
1921 printf_unfiltered (annotation_suffix
);
1922 printf_unfiltered ("\n");
1925 if (!rl
|| rl
== (char *) EOF
)
1930 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
1932 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
1933 nline
= (char *) xrealloc (linebuffer
, linelength
);
1934 p
+= nline
- linebuffer
;
1938 /* Copy line. Don't copy null at end. (Leaves line alone
1939 if this was just a newline) */
1943 free (rl
); /* Allocated in readline. */
1945 if (p
== linebuffer
|| *(p
- 1) != '\\')
1948 p
--; /* Put on top of '\'. */
1949 local_prompt
= (char *) 0;
1954 signal (STOP_SIGNAL
, SIG_DFL
);
1961 #define SERVER_COMMAND_LENGTH 7
1963 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
1964 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
1967 /* Note that we don't set `line'. Between this and the check in
1968 dont_repeat, this insures that repeating will still do the
1971 return linebuffer
+ SERVER_COMMAND_LENGTH
;
1974 /* Do history expansion if that is wished. */
1975 if (history_expansion_p
&& instream
== stdin
1976 && ISATTY (instream
))
1978 char *history_value
;
1981 *p
= '\0'; /* Insert null now. */
1982 expanded
= history_expand (linebuffer
, &history_value
);
1985 /* Print the changes. */
1986 printf_unfiltered ("%s\n", history_value
);
1988 /* If there was an error, call this function again. */
1991 free (history_value
);
1992 return command_line_input (prrompt
, repeat
, annotation_suffix
);
1994 if (strlen (history_value
) > linelength
)
1996 linelength
= strlen (history_value
) + 1;
1997 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
1999 strcpy (linebuffer
, history_value
);
2000 p
= linebuffer
+ strlen(linebuffer
);
2001 free (history_value
);
2005 /* If we just got an empty line, and that is supposed
2006 to repeat the previous command, return the value in the
2010 if (p
== linebuffer
)
2013 while (*p1
== ' ' || *p1
== '\t')
2021 /* Add line to history if appropriate. */
2022 if (instream
== stdin
2023 && ISATTY (stdin
) && *linebuffer
)
2024 add_history (linebuffer
);
2026 /* Note: lines consisting solely of comments are added to the command
2027 history. This is useful when you type a command, and then
2028 realize you don't want to execute it quite yet. You can comment
2029 out the command and then later fetch it from the value history
2030 and remove the '#'. The kill ring is probably better, but some
2031 people are in the habit of commenting things out. */
2033 while ((c
= *p1
++) != '\0')
2036 while ((c
= *p1
++) != '"')
2038 /* Make sure an escaped '"' doesn't make us think the string
2046 while ((c
= *p1
++) != '\'')
2048 /* Make sure an escaped '\'' doesn't make us think the string
2057 /* Found a comment. */
2063 /* Save into global buffer if appropriate. */
2066 if (linelength
> linesize
)
2068 line
= xrealloc (line
, linelength
);
2069 linesize
= linelength
;
2071 strcpy (line
, linebuffer
);
2079 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2080 code bodies. This is typically used when we encounter an "else"
2081 clause for an "if" command. */
2084 realloc_body_list (command
, new_length
)
2085 struct command_line
*command
;
2089 struct command_line
**body_list
;
2091 n
= command
->body_count
;
2093 /* Nothing to do? */
2094 if (new_length
<= n
)
2097 body_list
= (struct command_line
**)
2098 xmalloc (sizeof (struct command_line
*) * new_length
);
2100 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2102 free (command
->body_list
);
2103 command
->body_list
= body_list
;
2104 command
->body_count
= new_length
;
2107 /* Read one line from the input stream. If the command is an "else" or
2108 "end", return such an indication to the caller. */
2110 static enum misc_command_type
2111 read_next_line (command
)
2112 struct command_line
**command
;
2114 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2117 if (control_level
>= 254)
2118 error ("Control nesting too deep!\n");
2120 /* Set a prompt based on the nesting of the control commands. */
2121 if (instream
== stdin
)
2123 for (i
= 0; i
< control_level
; i
++)
2124 control_prompt
[i
] = ' ';
2125 control_prompt
[i
] = '>';
2126 control_prompt
[i
+1] = '\0';
2127 prompt_ptr
= (char *)&control_prompt
[0];
2132 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2134 /* Not sure what to do here. */
2138 /* Strip leading and trailing whitespace. */
2139 while (*p
== ' ' || *p
== '\t')
2142 p1
= p
+ strlen (p
);
2143 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2146 /* Blanks and comments don't really do anything, but we need to
2147 distinguish them from else, end and other commands which can be
2149 if (p1
== p
|| p
[0] == '#')
2152 /* Is this the end of a simple, while, or if control structure? */
2153 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2156 /* Is the else clause of an if control structure? */
2157 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2158 return else_command
;
2160 /* Check for while, if, break, continue, etc and build a new command
2161 line structure for them. */
2162 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2163 *command
= build_command_line (while_control
, p
+ 6);
2164 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2165 *command
= build_command_line (if_control
, p
+ 3);
2166 else if (p1
- p
== 5 && !strncmp (p
, "loop_break", 5))
2168 *command
= (struct command_line
*)
2169 xmalloc (sizeof (struct command_line
));
2170 (*command
)->next
= NULL
;
2171 (*command
)->line
= NULL
;
2172 (*command
)->control_type
= break_control
;
2173 (*command
)->body_count
= 0;
2174 (*command
)->body_list
= NULL
;
2176 else if (p1
- p
== 8 && !strncmp (p
, "loop_continue", 8))
2178 *command
= (struct command_line
*)
2179 xmalloc (sizeof (struct command_line
));
2180 (*command
)->next
= NULL
;
2181 (*command
)->line
= NULL
;
2182 (*command
)->control_type
= continue_control
;
2183 (*command
)->body_count
= 0;
2184 (*command
)->body_list
= NULL
;
2188 /* A normal command. */
2189 *command
= (struct command_line
*)
2190 xmalloc (sizeof (struct command_line
));
2191 (*command
)->next
= NULL
;
2192 (*command
)->line
= savestring (p
, p1
- p
);
2193 (*command
)->control_type
= simple_control
;
2194 (*command
)->body_count
= 0;
2195 (*command
)->body_list
= NULL
;
2198 /* Nothing special. */
2202 /* Recursively read in the control structures and create a command_line
2205 The parent_control parameter is the control structure in which the
2206 following commands are nested. */
2208 static enum command_control_type
2209 recurse_read_control_structure (current_cmd
)
2210 struct command_line
*current_cmd
;
2212 int current_body
, i
;
2213 enum misc_command_type val
;
2214 enum command_control_type ret
;
2215 struct command_line
**body_ptr
, *child_tail
, *next
;
2216 struct cleanup
*old_chains
, *tmp_chains
;
2222 /* Sanity checks. */
2223 if (current_cmd
->control_type
== simple_control
)
2225 error ("Recursed on a simple control type\n");
2226 return invalid_control
;
2229 if (current_body
> current_cmd
->body_count
)
2231 error ("Allocated body is smaller than this command type needs\n");
2232 return invalid_control
;
2235 /* Read lines from the input stream and build control structures. */
2241 val
= read_next_line (&next
);
2243 /* Just skip blanks and comments. */
2244 if (val
== nop_command
)
2247 if (val
== end_command
)
2249 if (current_cmd
->control_type
== while_control
2250 || current_cmd
->control_type
== if_control
)
2252 /* Success reading an entire control structure. */
2253 ret
= simple_control
;
2258 ret
= invalid_control
;
2263 /* Not the end of a control structure. */
2264 if (val
== else_command
)
2266 if (current_cmd
->control_type
== if_control
2267 && current_body
== 1)
2269 realloc_body_list (current_cmd
, 2);
2276 ret
= invalid_control
;
2283 child_tail
->next
= next
;
2287 /* We have just read the first line of the child's control
2288 structure. From now on, arrange to throw away the line
2289 we have if we quit or get an error. */
2290 body_ptr
= current_cmd
->body_list
;
2291 for (i
= 1; i
< current_body
; i
++)
2296 tmp_chains
= make_cleanup (free_command_lines
, body_ptr
);
2299 old_chains
= tmp_chains
;
2304 /* If the latest line is another control structure, then recurse
2306 if (next
->control_type
== while_control
2307 || next
->control_type
== if_control
)
2310 ret
= recurse_read_control_structure (next
);
2313 if (ret
!= simple_control
)
2319 if (ret
== invalid_control
&& old_chains
)
2320 do_cleanups (old_chains
);
2321 else if (old_chains
)
2322 discard_cleanups (old_chains
);
2328 /* Read lines from the input stream
2329 and accumulate them in a chain of struct command_line's
2330 which is then returned. */
2332 struct command_line
*
2333 read_command_lines ()
2335 struct command_line
*head
, *tail
, *next
;
2336 struct cleanup
*old_chain
;
2337 enum command_control_type ret
;
2338 enum misc_command_type val
;
2345 val
= read_next_line (&next
);
2347 /* Ignore blank lines or comments. */
2348 if (val
== nop_command
)
2351 if (val
== end_command
)
2353 ret
= simple_control
;
2357 if (val
!= ok_command
)
2359 ret
= invalid_control
;
2363 if (next
->control_type
== while_control
2364 || next
->control_type
== if_control
)
2367 ret
= recurse_read_control_structure (next
);
2370 if (ret
== invalid_control
)
2381 old_chain
= make_cleanup (free_command_lines
, &head
);
2390 if (ret
!= invalid_control
)
2392 discard_cleanups (old_chain
);
2396 do_cleanups (old_chain
);
2402 /* Free a chain of struct command_line's. */
2405 free_command_lines (lptr
)
2406 struct command_line
**lptr
;
2408 register struct command_line
*l
= *lptr
;
2409 register struct command_line
*next
;
2410 struct command_line
**blist
;
2415 if (l
->body_count
> 0)
2417 blist
= l
->body_list
;
2418 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2419 free_command_lines (blist
);
2428 /* Add an element to the list of info subcommands. */
2431 add_info (name
, fun
, doc
)
2433 void (*fun
) PARAMS ((char *, int));
2436 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2439 /* Add an alias to the list of info subcommands. */
2442 add_info_alias (name
, oldname
, abbrev_flag
)
2447 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2450 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2451 Therefore, its own definition is called only for "info" with no args. */
2455 info_command (arg
, from_tty
)
2459 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2460 help_list (infolist
, "info ", -1, gdb_stdout
);
2463 /* The "complete" command is used by Emacs to implement completion. */
2467 complete_command (arg
, from_tty
)
2479 argpoint
= strlen (arg
);
2481 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2483 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2485 printf_unfiltered ("%s\n", completion
);
2490 /* The "show" command with no arguments shows all the settings. */
2494 show_command (arg
, from_tty
)
2498 cmd_show_list (showlist
, from_tty
, "");
2501 /* Add an element to the list of commands. */
2504 add_com (name
, class, fun
, doc
)
2506 enum command_class
class;
2507 void (*fun
) PARAMS ((char *, int));
2510 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2513 /* Add an alias or abbreviation command to the list of commands. */
2516 add_com_alias (name
, oldname
, class, abbrev_flag
)
2519 enum command_class
class;
2522 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2529 error ("Argument required (%s).", why
);
2534 help_command (command
, from_tty
)
2536 int from_tty
; /* Ignored */
2538 help_cmd (command
, gdb_stdout
);
2542 validate_comname (comname
)
2548 error_no_arg ("name of command to define");
2553 if (!isalnum(*p
) && *p
!= '-')
2554 error ("Junk in argument list: \"%s\"", p
);
2559 /* This is just a placeholder in the command data structures. */
2561 user_defined_command (ignore
, from_tty
)
2568 define_command (comname
, from_tty
)
2572 register struct command_line
*cmds
;
2573 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2574 char *tem
= comname
;
2575 #define HOOK_STRING "hook-"
2578 validate_comname (comname
);
2580 /* Look it up, and verify that we got an exact match. */
2581 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2582 if (c
&& !STREQ (comname
, c
->name
))
2587 if (c
->class == class_user
|| c
->class == class_alias
)
2588 tem
= "Redefine command \"%s\"? ";
2590 tem
= "Really redefine built-in command \"%s\"? ";
2591 if (!query (tem
, c
->name
))
2592 error ("Command \"%s\" not redefined.", c
->name
);
2595 /* If this new command is a hook, then mark the command which it
2596 is hooking. Note that we allow hooking `help' commands, so that
2597 we can hook the `stop' pseudo-command. */
2599 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2601 /* Look up cmd it hooks, and verify that we got an exact match. */
2602 tem
= comname
+HOOK_LEN
;
2603 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2604 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2608 warning ("Your new `%s' command does not hook any existing command.",
2610 if (!query ("Proceed? ", (char *)0))
2611 error ("Not confirmed.");
2615 comname
= savestring (comname
, strlen (comname
));
2617 /* If the rest of the commands will be case insensitive, this one
2618 should behave in the same manner. */
2619 for (tem
= comname
; *tem
; tem
++)
2620 if (isupper(*tem
)) *tem
= tolower(*tem
);
2624 printf_unfiltered ("Type commands for definition of \"%s\".\n\
2625 End with a line saying just \"end\".\n", comname
);
2626 gdb_flush (gdb_stdout
);
2630 cmds
= read_command_lines ();
2632 if (c
&& c
->class == class_user
)
2633 free_command_lines (&c
->user_commands
);
2635 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2636 (c
&& c
->class == class_user
)
2637 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2638 newc
->user_commands
= cmds
;
2640 /* If this new command is a hook, then mark both commands as being
2644 hookc
->hook
= newc
; /* Target gets hooked. */
2645 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2650 document_command (comname
, from_tty
)
2654 struct command_line
*doclines
;
2655 register struct cmd_list_element
*c
;
2656 char *tem
= comname
;
2658 validate_comname (comname
);
2660 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2662 if (c
->class != class_user
)
2663 error ("Command \"%s\" is built-in.", comname
);
2666 printf_unfiltered ("Type documentation for \"%s\".\n\
2667 End with a line saying just \"end\".\n", comname
);
2669 doclines
= read_command_lines ();
2671 if (c
->doc
) free (c
->doc
);
2674 register struct command_line
*cl1
;
2675 register int len
= 0;
2677 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2678 len
+= strlen (cl1
->line
) + 1;
2680 c
->doc
= (char *) xmalloc (len
+ 1);
2683 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2685 strcat (c
->doc
, cl1
->line
);
2687 strcat (c
->doc
, "\n");
2691 free_command_lines (&doclines
);
2695 print_gnu_advertisement ()
2697 printf_unfiltered ("\
2698 GDB is free software and you are welcome to distribute copies of it\n\
2699 under certain conditions; type \"show copying\" to see the conditions.\n\
2700 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2705 print_gdb_version (stream
)
2708 fprintf_filtered (stream
, "\
2709 GDB %s (%s", version
, host_name
);
2711 if (!STREQ (host_name
, target_name
))
2712 fprintf_filtered (stream
, " --target %s", target_name
);
2714 fprintf_filtered (stream
, "), ");
2716 fprintf_filtered (stream
, "Copyright 1994 Free Software Foundation, Inc.");
2721 show_version (args
, from_tty
)
2726 print_gnu_advertisement ();
2727 print_gdb_version (gdb_stdout
);
2728 printf_filtered ("\n");
2732 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2738 printf_unfiltered ("%s", prompt
);
2739 gdb_flush (gdb_stdout
);
2743 quit_command (args
, from_tty
)
2747 if (inferior_pid
!= 0 && target_has_execution
)
2751 if (query ("The program is running. Quit anyway (and detach it)? "))
2752 target_detach (args
, from_tty
);
2754 error ("Not confirmed.");
2758 if (query ("The program is running. Quit anyway (and kill it)? "))
2761 error ("Not confirmed.");
2764 /* UDI wants this, to kill the TIP. */
2767 /* Save the history information if it is appropriate to do so. */
2768 if (write_history_p
&& history_filename
)
2769 write_history (history_filename
);
2774 /* Returns whether GDB is running on a terminal and whether the user
2775 desires that questions be asked of them on that terminal. */
2778 input_from_terminal_p ()
2780 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
2785 pwd_command (args
, from_tty
)
2789 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
2790 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
2792 if (!STREQ (gdb_dirbuf
, current_directory
))
2793 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2794 current_directory
, gdb_dirbuf
);
2796 printf_unfiltered ("Working directory %s.\n", current_directory
);
2800 cd_command (dir
, from_tty
)
2805 /* Found something other than leading repetitions of "/..". */
2806 int found_real_path
;
2809 /* If the new directory is absolute, repeat is a no-op; if relative,
2810 repeat might be useful but is more likely to be a mistake. */
2814 error_no_arg ("new working directory");
2816 dir
= tilde_expand (dir
);
2817 make_cleanup (free
, dir
);
2819 if (chdir (dir
) < 0)
2820 perror_with_name (dir
);
2823 dir
= savestring (dir
, len
- (len
> 1 && dir
[len
-1] == '/'));
2825 current_directory
= dir
;
2828 if (current_directory
[0] == '/' && current_directory
[1] == '\0')
2829 current_directory
= concat (current_directory
, dir
, NULL
);
2831 current_directory
= concat (current_directory
, "/", dir
, NULL
);
2835 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2837 found_real_path
= 0;
2838 for (p
= current_directory
; *p
;)
2840 if (p
[0] == '/' && p
[1] == '.' && (p
[2] == 0 || p
[2] == '/'))
2842 else if (p
[0] == '/' && p
[1] == '.' && p
[2] == '.'
2843 && (p
[3] == 0 || p
[3] == '/'))
2845 if (found_real_path
)
2847 /* Search backwards for the directory just before the "/.."
2848 and obliterate it and the "/..". */
2850 while (q
!= current_directory
&& q
[-1] != '/')
2853 if (q
== current_directory
)
2854 /* current_directory is
2855 a relative pathname ("can't happen"--leave it alone). */
2859 strcpy (q
- 1, p
+ 3);
2864 /* We are dealing with leading repetitions of "/..", for example
2865 "/../..", which is the Mach super-root. */
2870 found_real_path
= 1;
2875 forget_cached_source_info ();
2878 pwd_command ((char *) 0, 1);
2881 struct source_cleanup_lines_args
{
2884 char *old_pre_error
;
2885 char *old_error_pre_print
;
2889 source_cleanup_lines (args
)
2892 struct source_cleanup_lines_args
*p
=
2893 (struct source_cleanup_lines_args
*)args
;
2894 source_line_number
= p
->old_line
;
2895 source_file_name
= p
->old_file
;
2896 source_pre_error
= p
->old_pre_error
;
2897 error_pre_print
= p
->old_error_pre_print
;
2902 source_command (args
, from_tty
)
2907 struct cleanup
*old_cleanups
;
2909 struct source_cleanup_lines_args old_lines
;
2914 error ("source command requires pathname of file to source.");
2917 file
= tilde_expand (file
);
2918 old_cleanups
= make_cleanup (free
, file
);
2920 stream
= fopen (file
, FOPEN_RT
);
2922 perror_with_name (file
);
2924 make_cleanup (fclose
, stream
);
2926 old_lines
.old_line
= source_line_number
;
2927 old_lines
.old_file
= source_file_name
;
2928 old_lines
.old_pre_error
= source_pre_error
;
2929 old_lines
.old_error_pre_print
= error_pre_print
;
2930 make_cleanup (source_cleanup_lines
, &old_lines
);
2931 source_line_number
= 0;
2932 source_file_name
= file
;
2933 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
2934 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
2935 make_cleanup (free
, source_pre_error
);
2936 /* This will get set every time we read a line. So it won't stay "" for
2938 error_pre_print
= "";
2940 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
2941 if (source_error_allocated
< needed_length
)
2943 source_error_allocated
*= 2;
2944 if (source_error_allocated
< needed_length
)
2945 source_error_allocated
= needed_length
;
2946 if (source_error
== NULL
)
2947 source_error
= xmalloc (source_error_allocated
);
2949 source_error
= xrealloc (source_error
, source_error_allocated
);
2952 read_command_file (stream
);
2954 do_cleanups (old_cleanups
);
2959 echo_command (text
, from_tty
)
2967 while ((c
= *p
++) != '\0')
2971 /* \ at end of argument is used after spaces
2972 so they won't be lost. */
2976 c
= parse_escape (&p
);
2978 printf_filtered ("%c", c
);
2981 printf_filtered ("%c", c
);
2984 /* Force this output to appear now. */
2986 gdb_flush (gdb_stdout
);
2990 #ifdef TARGET_BYTE_ORDER_SELECTABLE
2992 /* Functions to manipulate the endianness of the target. */
2994 #ifndef TARGET_BYTE_ORDER_DEFAULT
2995 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
2998 int target_byte_order
= TARGET_BYTE_ORDER_DEFAULT
;
3000 static int target_byte_order_auto
= 1;
3002 /* Called if the user enters ``set endian'' without an argument. */
3004 set_endian (args
, from_tty
)
3008 printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
3009 show_endian (args
, from_tty
);
3012 /* Called by ``set endian big''. */
3014 set_endian_big (args
, from_tty
)
3018 target_byte_order
= BIG_ENDIAN
;
3019 target_byte_order_auto
= 0;
3022 /* Called by ``set endian little''. */
3024 set_endian_little (args
, from_tty
)
3028 target_byte_order
= LITTLE_ENDIAN
;
3029 target_byte_order_auto
= 0;
3032 /* Called by ``set endian auto''. */
3034 set_endian_auto (args
, from_tty
)
3038 target_byte_order_auto
= 1;
3041 /* Called by ``show endian''. */
3043 show_endian (args
, from_tty
)
3048 (target_byte_order_auto
3049 ? "The target endianness is set automatically (currently %s endian)\n"
3050 : "The target is assumed to be %s endian\n");
3051 printf_unfiltered (msg
, TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3054 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3056 /* Set the endianness from a BFD. */
3058 set_endian_from_file (abfd
)
3061 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3064 if (abfd
->xvec
->byteorder_big_p
)
3067 want
= LITTLE_ENDIAN
;
3068 if (target_byte_order_auto
)
3069 target_byte_order
= want
;
3070 else if (target_byte_order
!= want
)
3071 warning ("%s endian file does not match %s endian target.",
3072 want
== BIG_ENDIAN
? "big" : "little",
3073 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3075 #else /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3077 if (abfd
->xvec
->byteorder_big_p
3078 ? TARGET_BYTE_ORDER
!= BIG_ENDIAN
3079 : TARGET_BYTE_ORDER
== BIG_ENDIAN
)
3080 warning ("%s endian file does not match %s endian target.",
3081 abfd
->xvec
->byteorder_big_p
? "big" : "little",
3082 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
3084 #endif /* ! defined (TARGET_BYTE_ORDER_SELECTABLE) */
3087 /* Functions to manipulate command line editing control variables. */
3089 /* Number of commands to print in each call to show_commands. */
3090 #define Hist_print 10
3092 show_commands (args
, from_tty
)
3096 /* Index for history commands. Relative to history_base. */
3099 /* Number of the history entry which we are planning to display next.
3100 Relative to history_base. */
3103 /* The first command in the history which doesn't exist (i.e. one more
3104 than the number of the last command). Relative to history_base. */
3107 extern HIST_ENTRY
*history_get
PARAMS ((int));
3109 /* Print out some of the commands from the command history. */
3110 /* First determine the length of the history list. */
3111 hist_len
= history_size
;
3112 for (offset
= 0; offset
< history_size
; offset
++)
3114 if (!history_get (history_base
+ offset
))
3123 if (args
[0] == '+' && args
[1] == '\0')
3124 /* "info editing +" should print from the stored position. */
3127 /* "info editing <exp>" should print around command number <exp>. */
3128 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3130 /* "show commands" means print the last Hist_print commands. */
3133 num
= hist_len
- Hist_print
;
3139 /* If there are at least Hist_print commands, we want to display the last
3140 Hist_print rather than, say, the last 6. */
3141 if (hist_len
- num
< Hist_print
)
3143 num
= hist_len
- Hist_print
;
3148 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3150 printf_filtered ("%5d %s\n", history_base
+ offset
,
3151 (history_get (history_base
+ offset
))->line
);
3154 /* The next command we want to display is the next one that we haven't
3158 /* If the user repeats this command with return, it should do what
3159 "show commands +" does. This is unnecessary if arg is null,
3160 because "show commands +" is not useful after "show commands". */
3161 if (from_tty
&& args
)
3168 /* Called by do_setshow_command. */
3171 set_history_size_command (args
, from_tty
, c
)
3174 struct cmd_list_element
*c
;
3176 if (history_size
== INT_MAX
)
3177 unstifle_history ();
3178 else if (history_size
>= 0)
3179 stifle_history (history_size
);
3182 history_size
= INT_MAX
;
3183 error ("History size must be non-negative");
3189 set_history (args
, from_tty
)
3193 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3194 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3199 show_history (args
, from_tty
)
3203 cmd_show_list (showhistlist
, from_tty
, "");
3206 int info_verbose
= 0; /* Default verbose msgs off */
3208 /* Called by do_setshow_command. An elaborate joke. */
3211 set_verbose (args
, from_tty
, c
)
3214 struct cmd_list_element
*c
;
3216 char *cmdname
= "verbose";
3217 struct cmd_list_element
*showcmd
;
3219 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3223 c
->doc
= "Set verbose printing of informational messages.";
3224 showcmd
->doc
= "Show verbose printing of informational messages.";
3228 c
->doc
= "Set verbosity.";
3229 showcmd
->doc
= "Show verbosity.";
3234 float_handler (signo
)
3237 /* This message is based on ANSI C, section 4.7. Note that integer
3238 divide by zero causes this, so "float" is a misnomer. */
3239 signal (SIGFPE
, float_handler
);
3240 error ("Erroneous arithmetic operation.");
3252 enablebreaklist
= NULL
;
3256 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3260 showhistlist
= NULL
;
3261 unsethistlist
= NULL
;
3262 #if MAINTENANCE_CMDS
3263 maintenancelist
= NULL
;
3264 maintenanceinfolist
= NULL
;
3265 maintenanceprintlist
= NULL
;
3267 setprintlist
= NULL
;
3268 showprintlist
= NULL
;
3269 setchecklist
= NULL
;
3270 showchecklist
= NULL
;
3273 /* Init the history buffer. Note that we are called after the init file(s)
3274 * have been read so that the user can change the history file via his
3275 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3276 * overrides all of this.
3284 tmpenv
= getenv ("HISTSIZE");
3286 history_size
= atoi (tmpenv
);
3287 else if (!history_size
)
3290 stifle_history (history_size
);
3292 tmpenv
= getenv ("GDBHISTFILE");
3294 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3295 else if (!history_filename
) {
3296 /* We include the current directory so that if the user changes
3297 directories the file written will be the same as the one
3299 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3301 read_history (history_filename
);
3307 struct cmd_list_element
*c
;
3309 #ifdef TARGET_BYTE_ORDER_SELECTABLE
3311 add_prefix_cmd ("endian", class_support
, set_endian
,
3312 "Set endianness of target.",
3313 &endianlist
, "set endian ", 0, &setlist
);
3314 add_cmd ("big", class_support
, set_endian_big
,
3315 "Set target as being big endian.", &endianlist
);
3316 add_cmd ("little", class_support
, set_endian_little
,
3317 "Set target as being little endian.", &endianlist
);
3318 add_cmd ("auto", class_support
, set_endian_auto
,
3319 "Select target endianness automatically.", &endianlist
);
3320 add_cmd ("endian", class_support
, show_endian
,
3321 "Show endianness of target.", &showlist
);
3323 #endif /* defined (TARGET_BYTE_ORDER_SELECTABLE) */
3325 #ifdef DEFAULT_PROMPT
3326 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3328 prompt
= savestring ("(gdb) ", 6);
3331 /* Set the important stuff up for command editing. */
3332 command_editing_p
= 1;
3333 history_expansion_p
= 0;
3334 write_history_p
= 0;
3336 /* Setup important stuff for command line editing. */
3337 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3338 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3339 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3340 rl_readline_name
= "gdb";
3342 /* Define the classes of commands.
3343 They will appear in the help list in the reverse of this order. */
3345 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3346 "Maintenance commands.\n\
3347 Some gdb commands are provided just for use by gdb maintainers.\n\
3348 These commands are subject to frequent change, and may not be as\n\
3349 well documented as user commands.",
3351 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3352 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3353 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3354 The commands in this class are those defined by the user.\n\
3355 Use the \"define\" command to define a command.", &cmdlist
);
3356 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3357 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3358 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3359 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3360 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3361 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3362 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3363 counting from zero for the innermost (currently executing) frame.\n\n\
3364 At any time gdb identifies one frame as the \"selected\" frame.\n\
3365 Variable lookups are done with respect to the selected frame.\n\
3366 When the program being debugged stops, gdb selects the innermost frame.\n\
3367 The commands below can be used to select other frames by number or address.",
3369 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3371 add_com ("pwd", class_files
, pwd_command
,
3372 "Print working directory. This is used for your program as well.");
3373 c
= add_cmd ("cd", class_files
, cd_command
,
3374 "Set working directory to DIR for debugger and program being debugged.\n\
3375 The change does not take effect for the program being debugged\n\
3376 until the next time it is started.", &cmdlist
);
3377 c
->completer
= filename_completer
;
3380 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3385 add_com ("echo", class_support
, echo_command
,
3386 "Print a constant string. Give string as argument.\n\
3387 C escape sequences may be used in the argument.\n\
3388 No newline is added at the end of the argument;\n\
3389 use \"\\n\" if you want a newline to be printed.\n\
3390 Since leading and trailing whitespace are ignored in command arguments,\n\
3391 if you want to print some you must use \"\\\" before leading whitespace\n\
3392 to be printed or after trailing whitespace.");
3393 add_com ("document", class_support
, document_command
,
3394 "Document a user-defined command.\n\
3395 Give command name as argument. Give documentation on following lines.\n\
3396 End with a line of just \"end\".");
3397 add_com ("define", class_support
, define_command
,
3398 "Define a new command name. Command name is argument.\n\
3399 Definition appears on following lines, one command per line.\n\
3400 End with a line of just \"end\".\n\
3401 Use the \"document\" command to give documentation for the new command.\n\
3402 Commands defined in this way may have up to ten arguments.");
3405 c
= add_cmd ("source", class_support
, source_command
,
3406 "Read commands from a file named FILE.\n\
3407 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3408 when gdb is started.", &cmdlist
);
3410 /* Punt file name, we can't help it easily. */
3411 c
= add_cmd ("source", class_support
, source_command
,
3412 "Read commands from a file named FILE.\n\
3413 Note that the file \".gdbinit\" is read automatically in this way\n\
3414 when gdb is started.", &cmdlist
);
3416 c
->completer
= filename_completer
;
3418 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3419 add_com ("help", class_support
, help_command
, "Print list of commands.");
3420 add_com_alias ("q", "quit", class_support
, 1);
3421 add_com_alias ("h", "help", class_support
, 1);
3424 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3427 add_show_from_set (c
, &showlist
);
3428 c
->function
.sfunc
= set_verbose
;
3429 set_verbose (NULL
, 0, c
);
3432 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3433 "Set editing of command lines as they are typed.\n\
3434 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
3435 Without an argument, command line editing is enabled. To edit, use\n\
3436 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3439 add_prefix_cmd ("history", class_support
, set_history
,
3440 "Generic command for setting command history parameters.",
3441 &sethistlist
, "set history ", 0, &setlist
);
3442 add_prefix_cmd ("history", class_support
, show_history
,
3443 "Generic command for showing command history parameters.",
3444 &showhistlist
, "show history ", 0, &showlist
);
3447 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3448 "Set history expansion on command input.\n\
3449 Without an argument, history expansion is enabled.", &sethistlist
),
3453 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3454 "Set saving of the history record on exit.\n\
3455 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
3456 Without an argument, saving is enabled.", &sethistlist
),
3459 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3460 "Set the size of the command history, \n\
3461 ie. the number of previous commands to keep a record of.", &sethistlist
);
3462 add_show_from_set (c
, &showhistlist
);
3463 c
->function
.sfunc
= set_history_size_command
;
3466 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3467 "Set the filename in which to record the command history\n\
3468 (the list of previous commands of which a record is kept).", &sethistlist
),
3472 (add_set_cmd ("confirm", class_support
, var_boolean
,
3474 "Set whether to confirm potentially dangerous operations.",
3478 add_prefix_cmd ("info", class_info
, info_command
,
3479 "Generic command for showing things about the program being debugged.",
3480 &infolist
, "info ", 0, &cmdlist
);
3481 add_com_alias ("i", "info", class_info
, 1);
3483 add_com ("complete", class_obscure
, complete_command
,
3484 "List the completions for the rest of the line as a command.");
3486 add_prefix_cmd ("show", class_info
, show_command
,
3487 "Generic command for showing things about the debugger.",
3488 &showlist
, "show ", 0, &cmdlist
);
3489 /* Another way to get at the same thing. */
3490 add_info ("set", show_command
, "Show all GDB settings.");
3492 add_cmd ("commands", no_class
, show_commands
,
3493 "Show the the history of commands you typed.\n\
3494 You can supply a command number to start with, or a `+' to start after\n\
3495 the previous command number shown.",
3498 add_cmd ("version", no_class
, show_version
,
3499 "Show what version of GDB this is.", &showlist
);
3501 add_com ("while", class_support
, while_command
,
3502 "Execute nested commands WHILE the conditional expression is non zero.\n\
3503 The conditional expression must follow the word `while' and must in turn be\
3504 followed by a new line. The nested commands must be entered one per line,\
3505 and should be terminated by the word `end'.");
3507 add_com ("if", class_support
, if_command
,
3508 "Execute nested commands once IF the conditional expression is non zero.\n\
3509 The conditional expression must follow the word `if' and must in turn be\
3510 followed by a new line. The nested commands must be entered one per line,\
3511 and should be terminated by the word 'else' or `end'. If an else clause\
3512 is used, the same rules apply to its nested commands as to the first ones.");
3514 /* If target is open when baud changes, it doesn't take effect until the
3515 next open (I think, not sure). */
3516 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3517 var_zinteger
, (char *)&baud_rate
,
3518 "Set baud rate for remote serial I/O.\n\
3519 This value is used to set the speed of the serial port when debugging\n\
3520 using remote targets.", &setlist
),
3524 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3525 "Set debugging of remote protocol.\n\
3526 When enabled, each packet sent or received with the remote target\n\
3527 is displayed.", &setlist
),