1 /* Top level `main' program for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992
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"
32 #include "terminal.h" /* For job_control. */
36 /* readline include files */
40 /* readline defines this. */
44 #include <sys/types.h>
53 #include <sys/param.h>
57 #ifdef SET_STACK_LIMIT_HUGE
59 #include <sys/resource.h>
61 int original_stack_limit
;
64 /* Prototypes for local functions */
67 symbol_completion_function
PARAMS ((char *, int));
70 command_loop
PARAMS ((void));
73 command_loop_marker
PARAMS ((int));
76 print_gdb_version
PARAMS ((FILE *));
79 quit_command
PARAMS ((char *, int));
82 init_main
PARAMS ((void));
85 init_history
PARAMS ((void));
88 init_cmd_lists
PARAMS ((void));
91 float_handler
PARAMS ((int));
94 source_command
PARAMS ((char *, int));
96 static void cd_command
PARAMS ((char *, int));
99 print_gnu_advertisement
PARAMS ((void));
102 init_signals
PARAMS ((void));
105 read_command_file
PARAMS ((FILE *));
108 set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
111 show_history
PARAMS ((char *, int));
114 set_history
PARAMS ((char *, int));
117 set_history_size_command
PARAMS ((char *, int, struct cmd_list_element
*));
120 show_commands
PARAMS ((char *, int));
123 echo_command
PARAMS ((char *, int));
126 pwd_command
PARAMS ((char *, int));
129 show_version
PARAMS ((char *, int));
132 document_command
PARAMS ((char *, int));
135 define_command
PARAMS ((char *, int));
138 validate_comname
PARAMS ((char *));
141 help_command
PARAMS ((char *, int));
144 show_command
PARAMS ((char *, int));
147 info_command
PARAMS ((char *, int));
150 do_nothing
PARAMS ((int));
153 quit_cover
PARAMS ((char *));
156 disconnect
PARAMS ((int));
159 source_cleanup
PARAMS ((FILE *));
161 /* If this definition isn't overridden by the header files, assume
162 that isatty and fileno exist on this system. */
164 #define ISATTY(FP) (isatty (fileno (FP)))
167 /* Initialization file name for gdb. This is overridden in some configs. */
169 #ifndef GDBINIT_FILENAME
170 #define GDBINIT_FILENAME ".gdbinit"
172 static char gdbinit
[] = GDBINIT_FILENAME
;
173 static int inhibit_gdbinit
= 0;
175 #define ALL_CLEANUPS ((struct cleanup *)0)
177 /* Version number of GDB, as a string. */
179 extern char *version
;
181 /* Canonical host name as a string. */
183 extern char *host_canonical
;
185 /* Canonical target name as a string. */
187 extern char *target_canonical
;
189 /* Message to be printed before the error message, when an error occurs. */
191 extern char *error_pre_print
;
193 /* Message to be printed before the warning message, when a warning occurs. */
195 extern char *warning_pre_print
;
197 extern char lang_frame_mismatch_warn
[]; /* language.c */
199 /* Flag for whether we want all the "from_tty" gubbish printed. */
201 int caution
= 1; /* Default is yes, sigh. */
204 * Define all cmd_list_element's
207 /* Chain containing all defined commands. */
209 struct cmd_list_element
*cmdlist
;
211 /* Chain containing all defined info subcommands. */
213 struct cmd_list_element
*infolist
;
215 /* Chain containing all defined enable subcommands. */
217 struct cmd_list_element
*enablelist
;
219 /* Chain containing all defined disable subcommands. */
221 struct cmd_list_element
*disablelist
;
223 /* Chain containing all defined delete subcommands. */
225 struct cmd_list_element
*deletelist
;
227 /* Chain containing all defined "enable breakpoint" subcommands. */
229 struct cmd_list_element
*enablebreaklist
;
231 /* Chain containing all defined set subcommands */
233 struct cmd_list_element
*setlist
;
235 /* Chain containing all defined unset subcommands */
237 struct cmd_list_element
*unsetlist
;
239 /* Chain containing all defined show subcommands. */
241 struct cmd_list_element
*showlist
;
243 /* Chain containing all defined \"set history\". */
245 struct cmd_list_element
*sethistlist
;
247 /* Chain containing all defined \"show history\". */
249 struct cmd_list_element
*showhistlist
;
251 /* Chain containing all defined \"unset history\". */
253 struct cmd_list_element
*unsethistlist
;
255 /* Chain containing all defined maintenance subcommands. */
258 struct cmd_list_element
*maintenancelist
;
261 /* Chain containing all defined "maintenance info" subcommands. */
264 struct cmd_list_element
*maintenanceinfolist
;
267 /* Chain containing all defined "maintenance print" subcommands. */
270 struct cmd_list_element
*maintenanceprintlist
;
273 struct cmd_list_element
*setprintlist
;
275 struct cmd_list_element
*showprintlist
;
277 struct cmd_list_element
*setchecklist
;
279 struct cmd_list_element
*showchecklist
;
281 /* stdio stream that command input is being read from. Set to stdin normally.
282 Set by source_command to the file we are sourcing. Set to NULL if we are
283 executing a user-defined command. */
287 /* Current working directory. */
289 char *current_directory
;
291 /* The directory name is actually stored here (usually). */
292 static char dirbuf
[1024];
294 /* Function to call before reading a command, if nonzero.
295 The function receives two args: an input stream,
296 and a prompt string. */
298 void (*window_hook
) PARAMS ((FILE *, char *));
300 extern int mapped_symbol_files
;
301 extern int readnow_symbol_files
;
306 /* gdb prints this when reading a command interactively */
309 /* Buffer used for reading command lines, and the size
310 allocated for it so far. */
315 /* Baud rate specified for talking to serial target systems. Default
316 is left as -1, so targets can choose their own defaults. */
320 /* Non-zero tells remote* modules to output debugging info. */
322 int remote_debug
= 0;
324 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
328 #define STOP_SIGNAL SIGTSTP
329 static void stop_sig
PARAMS ((int));
333 /* Some System V have job control but not sigsetmask(). */
334 #if !defined (HAVE_SIGSETMASK)
335 #define HAVE_SIGSETMASK !defined (USG)
338 #if 0 == (HAVE_SIGSETMASK)
339 #define sigsetmask(n)
342 /* Where to go for return_to_top_level (RETURN_ERROR). */
343 static jmp_buf error_return
;
344 /* Where to go for return_to_top_level (RETURN_QUIT). */
345 static jmp_buf quit_return
;
347 /* Temporary variable for SET_TOP_LEVEL. */
348 static int top_level_val
;
350 /* Do a setjmp on error_return and quit_return. catch_errors is
351 generally a cleaner way to do this, but main() would look pretty
352 ugly if it had to use catch_errors each time. */
354 #define SET_TOP_LEVEL() \
355 (((top_level_val = setjmp (error_return)) \
356 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \
359 /* Return for reason REASON. This generally gets back to the command
360 loop, but can be caught via catch_errors. */
363 return_to_top_level (reason
)
364 enum return_reason reason
;
369 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
370 I can think of a reason why that is vital, though). */
371 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
373 disable_current_display ();
374 do_cleanups (ALL_CLEANUPS
);
375 (NORETURN
void) longjmp
376 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
379 /* Call FUNC with arg ARGS, catching any errors. If there is no
380 error, return the value returned by FUNC. If there is an error,
381 print ERRSTRING, print the specific error message, then return
384 Must not be called with immediate_quit in effect (bad things might
385 happen, say we got a signal in the middle of a memcpy to quit_return).
386 This is an OK restriction; with very few exceptions immediate_quit can
387 be replaced by judicious use of QUIT.
389 MASK specifies what to catch; it is normally set to
390 RETURN_MASK_ALL, if for no other reason than that the code which
391 calls catch_errors might not be set up to deal with a quit which
392 isn't caught. But if the code can deal with it, it generally
393 should be RETURN_MASK_ERROR, unless for some reason it is more
394 useful to abort only the portion of the operation inside the
395 catch_errors. Note that quit should return to the command line
396 fairly quickly, even if some further processing is being done. */
399 catch_errors (func
, args
, errstring
, mask
)
400 int (*func
) PARAMS ((char *));
409 struct cleanup
*saved_cleanup_chain
;
410 char *saved_error_pre_print
;
412 saved_cleanup_chain
= save_cleanups ();
413 saved_error_pre_print
= error_pre_print
;
415 if (mask
& RETURN_MASK_ERROR
)
416 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (jmp_buf));
417 if (mask
& RETURN_MASK_QUIT
)
418 memcpy (saved_quit
, quit_return
, sizeof (jmp_buf));
419 error_pre_print
= errstring
;
421 if (setjmp (tmp_jmp
) == 0)
423 if (mask
& RETURN_MASK_ERROR
)
424 memcpy (error_return
, tmp_jmp
, sizeof (jmp_buf));
425 if (mask
& RETURN_MASK_QUIT
)
426 memcpy (quit_return
, tmp_jmp
, sizeof (jmp_buf));
427 val
= (*func
) (args
);
432 restore_cleanups (saved_cleanup_chain
);
434 error_pre_print
= saved_error_pre_print
;
435 if (mask
& RETURN_MASK_ERROR
)
436 memcpy (error_return
, saved_error
, sizeof (jmp_buf));
437 if (mask
& RETURN_MASK_QUIT
)
438 memcpy (quit_return
, saved_quit
, sizeof (jmp_buf));
442 /* Handler for SIGHUP. */
448 catch_errors (quit_cover
, NULL
,
449 "Could not kill the program being debugged", RETURN_MASK_ALL
);
450 signal (SIGHUP
, SIG_DFL
);
451 kill (getpid (), SIGHUP
);
454 /* Just a little helper function for disconnect(). */
460 caution
= 0; /* Throw caution to the wind -- we're exiting.
461 This prevents asking the user dumb questions. */
462 quit_command((char *)0, 0);
466 /* Clean up on error during a "source" command (or execution of a
467 user-defined command). */
470 source_cleanup (stream
)
473 /* Restore the previous input stream. */
477 /* Read commands from STREAM. */
479 read_command_file (stream
)
482 struct cleanup
*cleanups
;
484 cleanups
= make_cleanup (source_cleanup
, instream
);
487 do_cleanups (cleanups
);
496 static int quiet
= 0;
497 static int batch
= 0;
499 /* Pointers to various arguments from command line. */
501 char *execarg
= NULL
;
502 char *corearg
= NULL
;
506 /* These are static so that we can take their address in an initializer. */
507 static int print_help
;
508 static int print_version
;
510 /* Pointers to all arguments of --command option. */
512 /* Allocated size of cmdarg. */
514 /* Number of elements of cmdarg used. */
517 /* Indices of all arguments of --directory option. */
519 /* Allocated size. */
521 /* Number of elements used. */
524 struct stat homebuf
, cwdbuf
;
525 char *homedir
, *homeinit
;
529 /* This needs to happen before the first use of malloc. */
530 init_malloc ((PTR
) NULL
);
532 #if defined (ALIGN_STACK_ON_STARTUP)
533 i
= (int) &count
& 0x3;
538 /* If error() is called from initialization code, just exit */
539 if (SET_TOP_LEVEL ()) {
544 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
547 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
551 line
= (char *) xmalloc (linesize
);
552 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
555 getcwd (dirbuf
, sizeof (dirbuf
));
556 current_directory
= dirbuf
;
558 #ifdef SET_STACK_LIMIT_HUGE
562 /* Set the stack limit huge so that alloca (particularly stringtab
563 * in dbxread.c) does not fail. */
564 getrlimit (RLIMIT_STACK
, &rlim
);
565 original_stack_limit
= rlim
.rlim_cur
;
566 rlim
.rlim_cur
= rlim
.rlim_max
;
567 setrlimit (RLIMIT_STACK
, &rlim
);
569 #endif /* SET_STACK_LIMIT_HUGE */
571 /* Parse arguments and options. */
574 /* When var field is 0, use flag field to record the equivalent
575 short option (or arbitrary numbers starting at 10 for those
576 with no equivalent). */
577 static struct option long_options
[] =
579 {"readnow", no_argument
, &readnow_symbol_files
, 1},
580 {"r", no_argument
, &readnow_symbol_files
, 1},
581 {"mapped", no_argument
, &mapped_symbol_files
, 1},
582 {"m", no_argument
, &mapped_symbol_files
, 1},
583 {"quiet", no_argument
, &quiet
, 1},
584 {"q", no_argument
, &quiet
, 1},
585 {"silent", no_argument
, &quiet
, 1},
586 {"nx", no_argument
, &inhibit_gdbinit
, 1},
587 {"n", no_argument
, &inhibit_gdbinit
, 1},
588 {"batch", no_argument
, &batch
, 1},
589 {"epoch", no_argument
, &epoch_interface
, 1},
590 {"fullname", no_argument
, &frame_file_full_name
, 1},
591 {"f", no_argument
, &frame_file_full_name
, 1},
592 {"help", no_argument
, &print_help
, 1},
593 {"se", required_argument
, 0, 10},
594 {"symbols", required_argument
, 0, 's'},
595 {"s", required_argument
, 0, 's'},
596 {"exec", required_argument
, 0, 'e'},
597 {"e", required_argument
, 0, 'e'},
598 {"core", required_argument
, 0, 'c'},
599 {"c", required_argument
, 0, 'c'},
600 {"command", required_argument
, 0, 'x'},
601 {"version", no_argument
, &print_version
, 1},
602 {"x", required_argument
, 0, 'x'},
603 {"directory", required_argument
, 0, 'd'},
604 {"cd", required_argument
, 0, 11},
605 {"tty", required_argument
, 0, 't'},
606 {"baud", required_argument
, 0, 'b'},
607 {"b", required_argument
, 0, 'b'},
608 /* Allow machine descriptions to add more options... */
609 #ifdef ADDITIONAL_OPTIONS
612 {0, no_argument
, 0, 0},
619 c
= getopt_long_only (argc
, argv
, "",
620 long_options
, &option_index
);
624 /* Long option that takes an argument. */
625 if (c
== 0 && long_options
[option_index
].flag
== 0)
626 c
= long_options
[option_index
].val
;
631 /* Long option that just sets a flag. */
650 cmdarg
[ncmd
++] = optarg
;
654 cmdarg
= (char **) xrealloc ((char *)cmdarg
,
655 cmdsize
* sizeof (*cmdarg
));
659 dirarg
[ndir
++] = optarg
;
663 dirarg
= (char **) xrealloc ((char *)dirarg
,
664 dirsize
* sizeof (*dirarg
));
678 i
= strtol (optarg
, &p
, 0);
679 if (i
== 0 && p
== optarg
)
680 warning ("Could not set baud rate to `%s'.\n", optarg
);
686 #ifdef ADDITIONAL_OPTION_CASES
687 ADDITIONAL_OPTION_CASES
691 "Use `%s --help' for a complete list of options.\n",
697 /* OK, that's all the options. The other arguments are filenames. */
699 for (; optind
< argc
; optind
++)
703 symarg
= argv
[optind
];
704 execarg
= argv
[optind
];
707 corearg
= argv
[optind
];
711 "Excess command line arguments ignored. (%s%s)\n",
712 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
719 /* Run the init function of each source file */
721 init_cmd_lists (); /* This needs to be done first */
722 initialize_all_files ();
723 init_main (); /* But that omits this file! Do it now */
726 /* Do these (and anything which might call wrap_here or *_filtered)
727 after initialize_all_files. */
730 print_gdb_version (stdout
);
732 printf_filtered ("\n");
738 /* --version is intentionally not documented here, because we
739 are printing the version here, and the help is long enough
742 print_gdb_version (stdout
);
743 /* Make sure the output gets printed. */
745 printf_filtered ("\n");
747 /* But don't use *_filtered here. We don't want to prompt for continue
748 no matter how small the screen or how much we're going to print. */
750 This is the GNU debugger. Usage:\n\
751 gdb [options] [executable-file [core-file or process-id]]\n\
753 --help Print this message.\n\
754 --quiet Do not print version number on startup.\n\
755 --fullname Output information used by emacs-GDB interface.\n\
756 --epoch Output information used by epoch emacs-GDB interface.\n\
757 --batch Exit after processing options.\n\
758 --nx Do not read .gdbinit file.\n\
759 --tty=TTY Use TTY for input/output by the program being debugged.\n\
760 --cd=DIR Change current directory to DIR.\n\
761 --directory=DIR Search for source files in DIR.\n\
762 --command=FILE Execute GDB commands from FILE.\n\
763 --symbols=SYMFILE Read symbols from SYMFILE.\n\
764 --exec=EXECFILE Use EXECFILE as the executable.\n\
765 --se=FILE Use FILE as symbol file and executable file.\n\
766 --core=COREFILE Analyze the core dump COREFILE.\n\
767 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
768 --mapped Use mapped symbol files if supported on this system.\n\
769 --readnow Fully read symbol files on first access.\n\
771 #ifdef ADDITIONAL_OPTION_HELP
772 fputs (ADDITIONAL_OPTION_HELP
, stdout
);
775 For more information, type \"help\" from within GDB, or consult the\n\
776 GDB manual (available as on-line info or a printed manual).\n", stdout
);
782 /* Print all the junk at the top, with trailing "..." if we are about
783 to read a symbol file (possibly slowly). */
784 print_gnu_advertisement ();
785 print_gdb_version (stdout
);
787 printf_filtered ("..");
789 fflush (stdout
); /* Force to screen during slow operations */
792 error_pre_print
= "\n\n";
793 /* We may get more than one warning, don't double space all of them... */
794 warning_pre_print
= "\nwarning: ";
796 /* We need a default language for parsing expressions, so simple things like
797 "set width 0" won't fail if no language is explicitly set in a config file
798 or implicitly set by reading an executable during startup. */
799 set_language (language_c
);
800 expected_language
= current_language
; /* don't warn about the change. */
802 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
803 *before* all the command line arguments are processed; it sets
804 global parameters, which are independent of what file you are
805 debugging or what directory you are in. */
806 homedir
= getenv ("HOME");
809 homeinit
= (char *) alloca (strlen (getenv ("HOME")) +
810 strlen (gdbinit
) + 10);
811 strcpy (homeinit
, getenv ("HOME"));
812 strcat (homeinit
, "/");
813 strcat (homeinit
, gdbinit
);
814 if (!inhibit_gdbinit
&& access (homeinit
, R_OK
) == 0)
816 if (!SET_TOP_LEVEL ())
817 source_command (homeinit
, 0);
819 do_cleanups (ALL_CLEANUPS
);
821 /* Do stats; no need to do them elsewhere since we'll only
822 need them if homedir is set. Make sure that they are
823 zero in case one of them fails (this guarantees that they
824 won't match if either exists). */
826 memset (&homebuf
, 0, sizeof (struct stat
));
827 memset (&cwdbuf
, 0, sizeof (struct stat
));
829 stat (homeinit
, &homebuf
);
830 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
834 /* Now perform all the actions indicated by the arguments. */
837 if (!SET_TOP_LEVEL ())
839 cd_command (cdarg
, 0);
843 do_cleanups (ALL_CLEANUPS
);
845 for (i
= 0; i
< ndir
; i
++)
846 if (!SET_TOP_LEVEL ())
847 directory_command (dirarg
[i
], 0);
849 do_cleanups (ALL_CLEANUPS
);
853 && STREQ (execarg
, symarg
))
855 /* The exec file and the symbol-file are the same. If we can't open
856 it, better only print one error message. */
857 if (!SET_TOP_LEVEL ())
859 exec_file_command (execarg
, !batch
);
860 symbol_file_command (symarg
, 0);
866 if (!SET_TOP_LEVEL ())
867 exec_file_command (execarg
, !batch
);
869 if (!SET_TOP_LEVEL ())
870 symbol_file_command (symarg
, 0);
872 do_cleanups (ALL_CLEANUPS
);
874 /* After the symbol file has been read, print a newline to get us
875 beyond the copyright line... But errors should still set off
876 the error message with a (single) blank line. */
878 printf_filtered ("\n");
879 error_pre_print
= "\n";
880 warning_pre_print
= "\nwarning: ";
883 if (!SET_TOP_LEVEL ())
884 core_file_command (corearg
, !batch
);
885 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
886 attach_command (corearg
, !batch
);
887 do_cleanups (ALL_CLEANUPS
);
890 if (!SET_TOP_LEVEL ())
891 tty_command (ttyarg
, !batch
);
892 do_cleanups (ALL_CLEANUPS
);
894 #ifdef ADDITIONAL_OPTION_HANDLER
895 ADDITIONAL_OPTION_HANDLER
;
898 /* Error messages should no longer be distinguished with extra output. */
900 warning_pre_print
= "warning: ";
902 /* Read the .gdbinit file in the current directory, *if* it isn't
903 the same as the $HOME/.gdbinit file (it should exist, also). */
906 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
907 if (!inhibit_gdbinit
&& access (gdbinit
, R_OK
) == 0)
909 if (!SET_TOP_LEVEL ())
910 source_command (gdbinit
, 0);
912 do_cleanups (ALL_CLEANUPS
);
914 for (i
= 0; i
< ncmd
; i
++)
916 if (!SET_TOP_LEVEL ())
918 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
919 read_command_file (stdin
);
921 source_command (cmdarg
[i
], !batch
);
922 do_cleanups (ALL_CLEANUPS
);
927 /* Read in the old history after all the command files have been read. */
932 /* We have hit the end of the batch file. */
936 /* Do any host- or target-specific hacks. This is used for i960 targets
937 to force the user to set a nindy target and spec its parameters. */
939 #ifdef BEFORE_MAIN_LOOP_HOOK
940 BEFORE_MAIN_LOOP_HOOK
;
943 /* The command loop. */
947 if (!SET_TOP_LEVEL ())
949 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
951 quit_command ((char *)0, instream
== stdin
);
954 /* No exit -- exit is through quit_command. */
958 execute_user_command (c
, args
)
959 struct cmd_list_element
*c
;
962 register struct command_line
*cmdlines
;
963 struct cleanup
*old_chain
;
966 error ("User-defined commands cannot take arguments.");
968 cmdlines
= c
->user_commands
;
973 /* Set the instream to 0, indicating execution of a
974 user-defined function. */
975 old_chain
= make_cleanup (source_cleanup
, instream
);
976 instream
= (FILE *) 0;
979 execute_command (cmdlines
->line
, 0);
980 cmdlines
= cmdlines
->next
;
982 do_cleanups (old_chain
);
985 /* Execute the line P as a command.
986 Pass FROM_TTY as second argument to the defining function. */
989 execute_command (p
, from_tty
)
993 register struct cmd_list_element
*c
;
994 register enum language flang
;
995 static int warned
= 0;
999 /* This can happen when command_line_input hits end of file. */
1003 while (*p
== ' ' || *p
== '\t') p
++;
1008 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1009 /* Pass null arg rather than an empty one. */
1012 /* If this command has been hooked, run the hook first. */
1014 execute_user_command (c
->hook
, (char *)0);
1016 if (c
->class == class_user
)
1017 execute_user_command (c
, arg
);
1018 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1019 do_setshow_command (arg
, from_tty
& caution
, c
);
1020 else if (c
->function
.cfunc
== NO_FUNCTION
)
1021 error ("That is not a command, just a help topic.");
1023 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1026 /* Tell the user if the language has changed (except first time). */
1027 if (current_language
!= expected_language
)
1029 if (language_mode
== language_mode_auto
) {
1030 language_info (1); /* Print what changed. */
1035 /* Warn the user if the working language does not match the
1036 language of the current frame. Only warn the user if we are
1037 actually running the program, i.e. there is a stack. */
1038 /* FIXME: This should be cacheing the frame and only running when
1039 the frame changes. */
1040 if (target_has_stack
)
1042 flang
= get_frame_language ();
1044 && flang
!= language_unknown
1045 && flang
!= current_language
->la_language
)
1047 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1055 command_loop_marker (foo
)
1060 /* Read commands from `instream' and execute them
1061 until end of file or error reading instream. */
1065 struct cleanup
*old_chain
;
1067 int stdin_is_tty
= ISATTY (stdin
);
1069 while (!feof (instream
))
1071 if (window_hook
&& instream
== stdin
)
1072 (*window_hook
) (instream
, prompt
);
1075 if (instream
== stdin
&& stdin_is_tty
)
1076 reinitialize_more_filter ();
1077 old_chain
= make_cleanup (command_loop_marker
, 0);
1078 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1082 execute_command (command
, instream
== stdin
);
1083 /* Do any commands attached to breakpoint we stopped at. */
1084 bpstat_do_actions (&stop_bpstat
);
1085 do_cleanups (old_chain
);
1089 /* Commands call this if they do not want to be repeated by null lines. */
1094 /* If we aren't reading from standard input, we are saving the last
1095 thing read from stdin in line and don't want to delete it. Null lines
1096 won't repeat here in any case. */
1097 if (instream
== stdin
)
1101 /* Read a line from the stream "instream" without command line editing.
1103 It prints PRROMPT once at the start.
1104 Action is compatible with "readline", e.g. space for the result is
1105 malloc'd and should be freed by the caller.
1107 A NULL return means end of file. */
1109 gdb_readline (prrompt
)
1114 int input_index
= 0;
1115 int result_size
= 80;
1119 /* Don't use a _filtered function here. It causes the assumed
1120 character position to be off, since the newline we read from
1121 the user is not accounted for. */
1122 fputs (prrompt
, stdout
);
1126 result
= (char *) xmalloc (result_size
);
1130 /* Read from stdin if we are executing a user defined command.
1131 This is the right thing for prompt_for_continue, at least. */
1132 c
= fgetc (instream
? instream
: stdin
);
1136 if (input_index
> 0)
1137 /* The last line does not end with a newline. Return it, and
1138 if we are called again fgetc will still return EOF and
1139 we'll return NULL then. */
1148 result
[input_index
++] = c
;
1149 while (input_index
>= result_size
)
1152 result
= (char *) xrealloc (result
, result_size
);
1156 result
[input_index
++] = '\0';
1160 /* Variables which control command line editing and history
1161 substitution. These variables are given default values at the end
1163 static int command_editing_p
;
1164 static int history_expansion_p
;
1165 static int write_history_p
;
1166 static int history_size
;
1167 static char *history_filename
;
1169 /* readline uses the word breaks for two things:
1170 (1) In figuring out where to point the TEXT parameter to the
1171 rl_completion_entry_function. Since we don't use TEXT for much,
1172 it doesn't matter a lot what the word breaks are for this purpose, but
1173 it does affect how much stuff M-? lists.
1174 (2) If one of the matches contains a word break character, readline
1175 will quote it. That's why we switch between
1176 gdb_completer_word_break_characters and
1177 gdb_completer_command_word_break_characters. I'm not sure when
1178 we need this behavior (perhaps for funky characters in C++ symbols?). */
1180 /* Variables which are necessary for fancy command line editing. */
1181 char *gdb_completer_word_break_characters
=
1182 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1184 /* When completing on command names, we remove '-' from the list of
1185 word break characters, since we use it in command names. If the
1186 readline library sees one in any of the current completion strings,
1187 it thinks that the string needs to be quoted and automatically supplies
1189 char *gdb_completer_command_word_break_characters
=
1190 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1192 /* Characters that can be used to quote completion strings. Note that we
1193 can't include '"' because the gdb C parser treats such quoted sequences
1195 char *gdb_completer_quote_characters
=
1198 /* Functions that are used as part of the fancy command line editing. */
1200 /* This can be used for functions which don't want to complete on symbols
1201 but don't want to complete on anything else either. */
1204 noop_completer (text
, prefix
)
1211 /* Complete on filenames. */
1213 filename_completer (text
, word
)
1217 /* From readline. */
1218 extern char *filename_completion_function ();
1219 int subsequent_name
;
1221 int return_val_used
;
1222 int return_val_alloced
;
1224 return_val_used
= 0;
1225 /* Small for testing. */
1226 return_val_alloced
= 1;
1227 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1229 subsequent_name
= 0;
1233 p
= filename_completion_function (text
, subsequent_name
);
1234 if (return_val_used
>= return_val_alloced
)
1236 return_val_alloced
*= 2;
1238 (char **) xrealloc (return_val
,
1239 return_val_alloced
* sizeof (char *));
1243 return_val
[return_val_used
++] = p
;
1246 /* Like emacs, don't complete on old versions. Especially useful
1247 in the "source" command. */
1248 if (p
[strlen (p
) - 1] == '~')
1254 /* Return exactly p. */
1255 return_val
[return_val_used
++] = p
;
1256 else if (word
> text
)
1258 /* Return some portion of p. */
1259 q
= xmalloc (strlen (p
) + 5);
1260 strcpy (q
, p
+ (word
- text
));
1261 return_val
[return_val_used
++] = q
;
1266 /* Return some of TEXT plus p. */
1267 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1268 strncpy (q
, word
, text
- word
);
1269 q
[text
- word
] = '\0';
1271 return_val
[return_val_used
++] = q
;
1275 subsequent_name
= 1;
1278 /* There is no way to do this just long enough to affect quote inserting
1279 without also affecting the next completion. This should be fixed in
1281 /* Insure that readline does the right thing
1282 with respect to inserting quotes. */
1283 rl_completer_word_break_characters
= "";
1288 /* Here are some useful test cases for completion. FIXME: These should
1289 be put in the test suite. They should be tested with both M-? and TAB.
1291 "show output-" "radix"
1292 "show output" "-radix"
1293 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1294 "p " ambiguous (all symbols)
1295 "info t foo" no completions
1296 "info t " no completions
1297 "info t" ambiguous ("info target", "info terminal", etc.)
1298 "info ajksdlfk" no completions
1299 "info ajksdlfk " no completions
1301 "info " ambiguous (all info commands)
1302 "p \"a" no completions (string constant)
1303 "p 'a" ambiguous (all symbols starting with a)
1304 "p b-a" ambiguous (all symbols starting with a)
1305 "p b-" ambiguous (all symbols)
1306 "file Make" "file" (word break hard to screw up here)
1307 "file ../gdb.stabs/wi" "erd" (needs to not break word at slash)
1310 /* Generate completions one by one for the completer. Each time we are
1311 called return another potential completion to the caller. The function
1312 is misnamed; it just completes on commands or passes the buck to the
1313 command's completer function; the stuff specific to symbol completion
1314 is in make_symbol_completion_list.
1316 TEXT is readline's idea of the "word" we are looking at; we don't really
1317 like readline's ideas about word breaking so we ignore it.
1319 MATCHES is the number of matches that have currently been collected from
1320 calling this completion function. When zero, then we need to initialize,
1321 otherwise the initialization has already taken place and we can just
1322 return the next potential completion string.
1324 Returns NULL if there are no more completions, else a pointer to a string
1325 which is a possible completion.
1327 RL_LINE_BUFFER is available to be looked at; it contains the entire text
1328 of the line. RL_POINT is the offset in that line of the cursor. You
1329 should pretend that the line ends at RL_POINT. */
1332 symbol_completion_function (text
, matches
)
1336 static char **list
= (char **)NULL
; /* Cache of completions */
1337 static int index
; /* Next cached completion */
1338 char *output
= NULL
;
1339 char *tmp_command
, *p
;
1340 /* Pointer within tmp_command which corresponds to text. */
1342 struct cmd_list_element
*c
, *result_list
;
1343 extern char *rl_line_buffer
;
1344 extern int rl_point
;
1348 /* The caller is beginning to accumulate a new set of completions, so
1349 we need to find all of them now, and cache them for returning one at
1350 a time on future calls. */
1354 /* Free the storage used by LIST, but not by the strings inside.
1355 This is because rl_complete_internal () frees the strings. */
1361 /* Choose the default set of word break characters to break completions.
1362 If we later find out that we are doing completions on command strings
1363 (as opposed to strings supplied by the individual command completer
1364 functions, which can be any string) then we will switch to the
1365 special word break set for command strings, which leaves out the
1366 '-' character used in some commands. */
1368 rl_completer_word_break_characters
=
1369 gdb_completer_word_break_characters
;
1371 /* Decide whether to complete on a list of gdb commands or on symbols. */
1372 tmp_command
= (char *) alloca (rl_point
+ 1);
1375 strncpy (tmp_command
, rl_line_buffer
, rl_point
);
1376 tmp_command
[rl_point
] = '\0';
1377 /* Since text always contains some number of characters leading up
1378 to rl_point, we can find the equivalent position in tmp_command
1379 by subtracting that many characters from the end of tmp_command. */
1380 word
= tmp_command
+ rl_point
- strlen (text
);
1384 /* An empty line we want to consider ambiguous; that is, it
1385 could be any command. */
1386 c
= (struct cmd_list_element
*) -1;
1391 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1394 /* Move p up to the next interesting thing. */
1395 while (*p
== ' ' || *p
== '\t')
1402 /* It is an unrecognized command. So there are no
1403 possible completions. */
1406 else if (c
== (struct cmd_list_element
*) -1)
1410 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1411 doesn't advance over that thing itself. Do so now. */
1413 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1415 if (q
!= tmp_command
+ rl_point
)
1417 /* There is something beyond the ambiguous
1418 command, so there are no possible completions. For
1419 example, "info t " or "info t foo" does not complete
1420 to anything, because "info t" can be "info target" or
1426 /* We're trying to complete on the command which was ambiguous.
1427 This we can deal with. */
1430 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1435 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1437 /* Insure that readline does the right thing with respect to
1438 inserting quotes. */
1439 rl_completer_word_break_characters
=
1440 gdb_completer_command_word_break_characters
;
1445 /* We've recognized a full command. */
1447 if (p
== tmp_command
+ rl_point
)
1449 /* There is no non-whitespace in the line beyond the command. */
1451 if (p
[-1] == ' ' || p
[-1] == '\t')
1453 /* The command is followed by whitespace; we need to complete
1454 on whatever comes after command. */
1457 /* It is a prefix command; what comes after it is
1458 a subcommand (e.g. "info "). */
1459 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1461 /* Insure that readline does the right thing
1462 with respect to inserting quotes. */
1463 rl_completer_word_break_characters
=
1464 gdb_completer_command_word_break_characters
;
1468 /* It is a normal command; what comes after it is
1469 completed by the command's completer function. */
1470 list
= (*c
->completer
) (p
, word
);
1475 /* The command is not followed by whitespace; we need to
1476 complete on the command itself. e.g. "p" which is a
1477 command itself but also can complete to "print", "ptype"
1481 /* Find the command we are completing on. */
1483 while (q
> tmp_command
)
1485 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1491 list
= complete_on_cmdlist (result_list
, q
, word
);
1493 /* Insure that readline does the right thing
1494 with respect to inserting quotes. */
1495 rl_completer_word_break_characters
=
1496 gdb_completer_command_word_break_characters
;
1501 /* There is non-whitespace beyond the command. */
1503 if (c
->prefixlist
&& !c
->allow_unknown
)
1505 /* It is an unrecognized subcommand of a prefix command,
1506 e.g. "info adsfkdj". */
1511 /* It is a normal command. */
1512 list
= (*c
->completer
) (p
, word
);
1518 /* If we found a list of potential completions during initialization then
1519 dole them out one at a time. The vector of completions is NULL
1520 terminated, so after returning the last one, return NULL (and continue
1521 to do so) each time we are called after that, until a new list is
1526 output
= list
[index
];
1534 /* Can't do this because readline hasn't yet checked the word breaks
1535 for figuring out whether to insert a quote. */
1537 /* Make sure the word break characters are set back to normal for the
1538 next time that readline tries to complete something. */
1539 rl_completer_word_break_characters
=
1540 gdb_completer_word_break_characters
;
1546 /* Skip over a possibly quoted word (as defined by the quote characters
1547 and word break characters the completer uses). Returns pointer to the
1548 location after the "word". */
1554 char quote_char
= '\0';
1557 for (scan
= str
; *scan
!= '\0'; scan
++)
1559 if (quote_char
!= '\0')
1561 /* Ignore everything until the matching close quote char */
1562 if (*scan
== quote_char
)
1564 /* Found matching close quote. */
1569 else if (strchr (gdb_completer_quote_characters
, *scan
))
1571 /* Found start of a quoted string. */
1574 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1588 #if STOP_SIGNAL == SIGTSTP
1589 signal (SIGTSTP
, SIG_DFL
);
1591 kill (getpid (), SIGTSTP
);
1592 signal (SIGTSTP
, stop_sig
);
1594 signal (STOP_SIGNAL
, stop_sig
);
1596 printf ("%s", prompt
);
1599 /* Forget about any previous command -- null line now will do nothing. */
1602 #endif /* STOP_SIGNAL */
1604 /* Initialize signal handlers. */
1614 signal (SIGINT
, request_quit
);
1616 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1617 passed to the inferior, which we don't want. It would be
1618 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1619 on BSD4.3 systems using vfork, that can affect the
1620 GDB process as well as the inferior (the signal handling tables
1621 might be in memory, shared between the two). Since we establish
1622 a handler for SIGQUIT, when we call exec it will set the signal
1623 to SIG_DFL for us. */
1624 signal (SIGQUIT
, do_nothing
);
1625 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1626 signal (SIGHUP
, disconnect
);
1627 signal (SIGFPE
, float_handler
);
1629 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1630 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1634 /* Read one line from the command input stream `instream'
1635 into the local static buffer `linebuffer' (whose current length
1637 The buffer is made bigger as necessary.
1638 Returns the address of the start of the line.
1640 NULL is returned for end of file.
1642 *If* the instream == stdin & stdin is a terminal, the line read
1643 is copied into the file line saver (global var char *line,
1644 length linesize) so that it can be duplicated.
1646 This routine either uses fancy command line editing or
1647 simple input as the user has requested. */
1650 command_line_input (prrompt
, repeat
)
1654 static char *linebuffer
= 0;
1655 static unsigned linelength
= 0;
1659 char *local_prompt
= prrompt
;
1664 if (linebuffer
== 0)
1667 linebuffer
= (char *) xmalloc (linelength
);
1672 /* Control-C quits instantly if typed while in this loop
1673 since it should not wait until the user types a newline. */
1677 signal (STOP_SIGNAL
, stop_sig
);
1682 /* Reports are that some Sys V's don't flush stdout/err on reads
1683 from stdin, when stdin/out are sockets rather than ttys. So we
1684 have to do it ourselves, to make emacs-gdb and xxgdb work.
1685 On other machines, doing this once per input should be a cheap nop. */
1689 /* Don't use fancy stuff if not talking to stdin. */
1690 if (command_editing_p
&& instream
== stdin
1691 && ISATTY (instream
))
1692 rl
= readline (local_prompt
);
1694 rl
= gdb_readline (local_prompt
);
1696 if (!rl
|| rl
== (char *) EOF
)
1701 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
1703 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
1704 nline
= (char *) xrealloc (linebuffer
, linelength
);
1705 p
+= nline
- linebuffer
;
1709 /* Copy line. Don't copy null at end. (Leaves line alone
1710 if this was just a newline) */
1714 free (rl
); /* Allocated in readline. */
1716 if (p
== linebuffer
|| *(p
- 1) != '\\')
1719 p
--; /* Put on top of '\'. */
1720 local_prompt
= (char *) 0;
1725 signal (STOP_SIGNAL
, SIG_DFL
);
1732 /* Do history expansion if that is wished. */
1733 if (history_expansion_p
&& instream
== stdin
1734 && ISATTY (instream
))
1736 char *history_value
;
1739 *p
= '\0'; /* Insert null now. */
1740 expanded
= history_expand (linebuffer
, &history_value
);
1743 /* Print the changes. */
1744 printf ("%s\n", history_value
);
1746 /* If there was an error, call this function again. */
1749 free (history_value
);
1750 return command_line_input (prrompt
, repeat
);
1752 if (strlen (history_value
) > linelength
)
1754 linelength
= strlen (history_value
) + 1;
1755 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
1757 strcpy (linebuffer
, history_value
);
1758 p
= linebuffer
+ strlen(linebuffer
);
1759 free (history_value
);
1763 /* If we just got an empty line, and that is supposed
1764 to repeat the previous command, return the value in the
1768 if (p
== linebuffer
)
1771 while (*p1
== ' ' || *p1
== '\t')
1779 /* Add line to history if appropriate. */
1780 if (instream
== stdin
1781 && ISATTY (stdin
) && *linebuffer
)
1782 add_history (linebuffer
);
1784 /* Note: lines consisting soley of comments are added to the command
1785 history. This is useful when you type a command, and then
1786 realize you don't want to execute it quite yet. You can comment
1787 out the command and then later fetch it from the value history
1788 and remove the '#'. The kill ring is probably better, but some
1789 people are in the habit of commenting things out. */
1791 while ((c
= *p1
++) != '\0')
1794 while ((c
= *p1
++) != '"')
1796 /* Make sure an escaped '"' doesn't make us think the string
1804 while ((c
= *p1
++) != '\'')
1806 /* Make sure an escaped '\'' doesn't make us think the string
1815 /* Found a comment. */
1821 /* Save into global buffer if appropriate. */
1824 if (linelength
> linesize
)
1826 line
= xrealloc (line
, linelength
);
1827 linesize
= linelength
;
1829 strcpy (line
, linebuffer
);
1836 /* Read lines from the input stream
1837 and accumulate them in a chain of struct command_line's
1838 which is then returned. */
1840 struct command_line
*
1841 read_command_lines ()
1843 struct command_line
*first
= 0;
1844 register struct command_line
*next
, *tail
= 0;
1845 register char *p
, *p1
;
1846 struct cleanup
*old_chain
= 0;
1851 p
= command_line_input ((char *) NULL
, instream
== stdin
);
1853 /* Treat end of file like "end". */
1856 /* Remove leading and trailing blanks. */
1857 while (*p
== ' ' || *p
== '\t') p
++;
1858 p1
= p
+ strlen (p
);
1859 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t')) p1
--;
1861 /* Is this "end"? */
1862 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
1865 /* No => add this line to the chain of command lines. */
1866 next
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1867 next
->line
= savestring (p
, p1
- p
);
1875 /* We just read the first line.
1876 From now on, arrange to throw away the lines we have
1877 if we quit or get an error while inside this function. */
1879 old_chain
= make_cleanup (free_command_lines
, &first
);
1886 /* Now we are about to return the chain to our caller,
1887 so freeing it becomes his responsibility. */
1889 discard_cleanups (old_chain
);
1893 /* Free a chain of struct command_line's. */
1896 free_command_lines (lptr
)
1897 struct command_line
**lptr
;
1899 register struct command_line
*l
= *lptr
;
1900 register struct command_line
*next
;
1911 /* Add an element to the list of info subcommands. */
1914 add_info (name
, fun
, doc
)
1916 void (*fun
) PARAMS ((char *, int));
1919 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
1922 /* Add an alias to the list of info subcommands. */
1925 add_info_alias (name
, oldname
, abbrev_flag
)
1930 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
1933 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1934 Therefore, its own definition is called only for "info" with no args. */
1938 info_command (arg
, from_tty
)
1942 printf ("\"info\" must be followed by the name of an info command.\n");
1943 help_list (infolist
, "info ", -1, stdout
);
1946 /* The "show" command with no arguments shows all the settings. */
1950 show_command (arg
, from_tty
)
1954 cmd_show_list (showlist
, from_tty
, "");
1957 /* Add an element to the list of commands. */
1960 add_com (name
, class, fun
, doc
)
1962 enum command_class
class;
1963 void (*fun
) PARAMS ((char *, int));
1966 add_cmd (name
, class, fun
, doc
, &cmdlist
);
1969 /* Add an alias or abbreviation command to the list of commands. */
1972 add_com_alias (name
, oldname
, class, abbrev_flag
)
1975 enum command_class
class;
1978 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
1985 error ("Argument required (%s).", why
);
1990 help_command (command
, from_tty
)
1992 int from_tty
; /* Ignored */
1994 help_cmd (command
, stdout
);
1998 validate_comname (comname
)
2004 error_no_arg ("name of command to define");
2009 if (!isalnum(*p
) && *p
!= '-')
2010 error ("Junk in argument list: \"%s\"", p
);
2015 /* This is just a placeholder in the command data structures. */
2017 user_defined_command (ignore
, from_tty
)
2024 define_command (comname
, from_tty
)
2028 register struct command_line
*cmds
;
2029 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2030 char *tem
= comname
;
2031 #define HOOK_STRING "hook-"
2034 validate_comname (comname
);
2036 /* Look it up, and verify that we got an exact match. */
2037 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2038 if (c
&& !STREQ (comname
, c
->name
))
2043 if (c
->class == class_user
|| c
->class == class_alias
)
2044 tem
= "Redefine command \"%s\"? ";
2046 tem
= "Really redefine built-in command \"%s\"? ";
2047 if (!query (tem
, c
->name
))
2048 error ("Command \"%s\" not redefined.", c
->name
);
2051 /* If this new command is a hook, then mark the command which it
2052 is hooking. Note that we allow hooking `help' commands, so that
2053 we can hook the `stop' pseudo-command. */
2055 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2057 /* Look up cmd it hooks, and verify that we got an exact match. */
2058 tem
= comname
+HOOK_LEN
;
2059 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2060 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2064 warning ("Your new `%s' command does not hook any existing command.",
2066 if (!query ("Proceed? ", (char *)0))
2067 error ("Not confirmed.");
2071 comname
= savestring (comname
, strlen (comname
));
2073 /* If the rest of the commands will be case insensitive, this one
2074 should behave in the same manner. */
2075 for (tem
= comname
; *tem
; tem
++)
2076 if (isupper(*tem
)) *tem
= tolower(*tem
);
2080 printf ("Type commands for definition of \"%s\".\n\
2081 End with a line saying just \"end\".\n", comname
);
2085 cmds
= read_command_lines ();
2087 if (c
&& c
->class == class_user
)
2088 free_command_lines (&c
->user_commands
);
2090 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2091 (c
&& c
->class == class_user
)
2092 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2093 newc
->user_commands
= cmds
;
2095 /* If this new command is a hook, then mark both commands as being
2099 hookc
->hook
= newc
; /* Target gets hooked. */
2100 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2105 document_command (comname
, from_tty
)
2109 struct command_line
*doclines
;
2110 register struct cmd_list_element
*c
;
2111 char *tem
= comname
;
2113 validate_comname (comname
);
2115 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2117 if (c
->class != class_user
)
2118 error ("Command \"%s\" is built-in.", comname
);
2121 printf ("Type documentation for \"%s\".\n\
2122 End with a line saying just \"end\".\n", comname
);
2124 doclines
= read_command_lines ();
2126 if (c
->doc
) free (c
->doc
);
2129 register struct command_line
*cl1
;
2130 register int len
= 0;
2132 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2133 len
+= strlen (cl1
->line
) + 1;
2135 c
->doc
= (char *) xmalloc (len
+ 1);
2138 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2140 strcat (c
->doc
, cl1
->line
);
2142 strcat (c
->doc
, "\n");
2146 free_command_lines (&doclines
);
2150 print_gnu_advertisement()
2153 GDB is free software and you are welcome to distribute copies of it\n\
2154 under certain conditions; type \"show copying\" to see the conditions.\n\
2155 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2160 print_gdb_version (stream
)
2163 fprintf_filtered (stream
, "\
2164 GDB %s (%s", version
, host_canonical
);
2166 if (strcmp(host_canonical
, target_canonical
))
2167 fprintf_filtered (stream
, " --target %s", target_canonical
);
2169 fprintf_filtered (stream
, "), ");
2171 fprintf_filtered (stream
, "Copyright 1993 Free Software Foundation, Inc.");
2176 show_version (args
, from_tty
)
2181 print_gnu_advertisement ();
2182 print_gdb_version (stdout
);
2183 printf_filtered ("\n");
2187 /* xgdb calls this to reprint the usual GDB prompt. */
2192 printf ("%s", prompt
);
2197 quit_command (args
, from_tty
)
2201 if (inferior_pid
!= 0 && target_has_execution
)
2205 if (query ("The program is running. Quit anyway (and detach it)? "))
2206 target_detach (args
, from_tty
);
2208 error ("Not confirmed.");
2212 if (query ("The program is running. Quit anyway (and kill it)? "))
2215 error ("Not confirmed.");
2218 /* Save the history information if it is appropriate to do so. */
2219 if (write_history_p
&& history_filename
)
2220 write_history (history_filename
);
2224 /* Returns whether GDB is running on a terminal and whether the user
2225 desires that questions be asked of them on that terminal. */
2228 input_from_terminal_p ()
2230 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
2235 pwd_command (args
, from_tty
)
2239 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
2240 getcwd (dirbuf
, sizeof (dirbuf
));
2242 if (!STREQ (dirbuf
, current_directory
))
2243 printf ("Working directory %s\n (canonically %s).\n",
2244 current_directory
, dirbuf
);
2246 printf ("Working directory %s.\n", current_directory
);
2250 cd_command (dir
, from_tty
)
2255 /* Found something other than leading repetitions of "/..". */
2256 int found_real_path
;
2259 /* If the new directory is absolute, repeat is a no-op; if relative,
2260 repeat might be useful but is more likely to be a mistake. */
2264 error_no_arg ("new working directory");
2266 dir
= tilde_expand (dir
);
2267 make_cleanup (free
, dir
);
2269 if (chdir (dir
) < 0)
2270 perror_with_name (dir
);
2273 dir
= savestring (dir
, len
- (len
> 1 && dir
[len
-1] == '/'));
2275 current_directory
= dir
;
2278 if (current_directory
[0] == '/' && current_directory
[1] == '\0')
2279 current_directory
= concat (current_directory
, dir
, NULL
);
2281 current_directory
= concat (current_directory
, "/", dir
, NULL
);
2285 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2287 found_real_path
= 0;
2288 for (p
= current_directory
; *p
;)
2290 if (p
[0] == '/' && p
[1] == '.' && (p
[2] == 0 || p
[2] == '/'))
2292 else if (p
[0] == '/' && p
[1] == '.' && p
[2] == '.'
2293 && (p
[3] == 0 || p
[3] == '/'))
2295 if (found_real_path
)
2297 /* Search backwards for the directory just before the "/.."
2298 and obliterate it and the "/..". */
2300 while (q
!= current_directory
&& q
[-1] != '/')
2303 if (q
== current_directory
)
2304 /* current_directory is
2305 a relative pathname ("can't happen"--leave it alone). */
2309 strcpy (q
- 1, p
+ 3);
2314 /* We are dealing with leading repetitions of "/..", for example
2315 "/../..", which is the Mach super-root. */
2320 found_real_path
= 1;
2325 forget_cached_source_info ();
2328 pwd_command ((char *) 0, 1);
2333 source_command (args
, from_tty
)
2338 struct cleanup
*cleanups
;
2343 error ("source command requires pathname of file to source.");
2346 file
= tilde_expand (file
);
2347 make_cleanup (free
, file
);
2349 stream
= fopen (file
, FOPEN_RT
);
2351 perror_with_name (file
);
2353 cleanups
= make_cleanup (fclose
, stream
);
2355 read_command_file (stream
);
2357 do_cleanups (cleanups
);
2362 echo_command (text
, from_tty
)
2370 while ((c
= *p
++) != '\0')
2374 /* \ at end of argument is used after spaces
2375 so they won't be lost. */
2379 c
= parse_escape (&p
);
2381 printf_filtered ("%c", c
);
2384 printf_filtered ("%c", c
);
2387 /* Force this output to appear now. */
2393 /* Functions to manipulate command line editing control variables. */
2395 /* Number of commands to print in each call to show_commands. */
2396 #define Hist_print 10
2398 show_commands (args
, from_tty
)
2402 /* Index for history commands. Relative to history_base. */
2405 /* Number of the history entry which we are planning to display next.
2406 Relative to history_base. */
2409 /* The first command in the history which doesn't exist (i.e. one more
2410 than the number of the last command). Relative to history_base. */
2413 extern struct _hist_entry
*history_get
PARAMS ((int));
2414 extern int history_base
;
2416 /* Print out some of the commands from the command history. */
2417 /* First determine the length of the history list. */
2418 hist_len
= history_size
;
2419 for (offset
= 0; offset
< history_size
; offset
++)
2421 if (!history_get (history_base
+ offset
))
2430 if (args
[0] == '+' && args
[1] == '\0')
2431 /* "info editing +" should print from the stored position. */
2434 /* "info editing <exp>" should print around command number <exp>. */
2435 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
2437 /* "show commands" means print the last Hist_print commands. */
2440 num
= hist_len
- Hist_print
;
2446 /* If there are at least Hist_print commands, we want to display the last
2447 Hist_print rather than, say, the last 6. */
2448 if (hist_len
- num
< Hist_print
)
2450 num
= hist_len
- Hist_print
;
2455 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
2457 printf_filtered ("%5d %s\n", history_base
+ offset
,
2458 (history_get (history_base
+ offset
))->line
);
2461 /* The next command we want to display is the next one that we haven't
2465 /* If the user repeats this command with return, it should do what
2466 "show commands +" does. This is unnecessary if arg is null,
2467 because "show commands +" is not useful after "show commands". */
2468 if (from_tty
&& args
)
2475 /* Called by do_setshow_command. */
2478 set_history_size_command (args
, from_tty
, c
)
2481 struct cmd_list_element
*c
;
2483 if (history_size
== INT_MAX
)
2484 unstifle_history ();
2485 else if (history_size
>= 0)
2486 stifle_history (history_size
);
2489 history_size
= INT_MAX
;
2490 error ("History size must be non-negative");
2496 set_history (args
, from_tty
)
2500 printf ("\"set history\" must be followed by the name of a history subcommand.\n");
2501 help_list (sethistlist
, "set history ", -1, stdout
);
2506 show_history (args
, from_tty
)
2510 cmd_show_list (showhistlist
, from_tty
, "");
2513 int info_verbose
= 0; /* Default verbose msgs off */
2515 /* Called by do_setshow_command. An elaborate joke. */
2518 set_verbose (args
, from_tty
, c
)
2521 struct cmd_list_element
*c
;
2523 char *cmdname
= "verbose";
2524 struct cmd_list_element
*showcmd
;
2526 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
2530 c
->doc
= "Set verbose printing of informational messages.";
2531 showcmd
->doc
= "Show verbose printing of informational messages.";
2535 c
->doc
= "Set verbosity.";
2536 showcmd
->doc
= "Show verbosity.";
2541 float_handler (signo
)
2544 /* This message is based on ANSI C, section 4.7. Note that integer
2545 divide by zero causes this, so "float" is a misnomer. */
2546 signal (SIGFPE
, float_handler
);
2547 error ("Erroneous arithmetic operation.");
2550 /* Return whether we are running a batch file or from terminal. */
2554 return !(instream
== stdin
&& ISATTY (stdin
));
2566 enablebreaklist
= NULL
;
2571 showhistlist
= NULL
;
2572 unsethistlist
= NULL
;
2573 #if MAINTENANCE_CMDS
2574 maintenancelist
= NULL
;
2575 maintenanceinfolist
= NULL
;
2576 maintenanceprintlist
= NULL
;
2578 setprintlist
= NULL
;
2579 showprintlist
= NULL
;
2580 setchecklist
= NULL
;
2581 showchecklist
= NULL
;
2584 /* Init the history buffer. Note that we are called after the init file(s)
2585 * have been read so that the user can change the history file via his
2586 * .gdbinit file (for instance). The GDBHISTFILE environment variable
2587 * overrides all of this.
2595 tmpenv
= getenv ("HISTSIZE");
2597 history_size
= atoi (tmpenv
);
2598 else if (!history_size
)
2601 stifle_history (history_size
);
2603 tmpenv
= getenv ("GDBHISTFILE");
2605 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
2606 else if (!history_filename
) {
2607 /* We include the current directory so that if the user changes
2608 directories the file written will be the same as the one
2610 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
2612 read_history (history_filename
);
2618 struct cmd_list_element
*c
;
2620 #ifdef DEFAULT_PROMPT
2621 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
2623 prompt
= savestring ("(gdb) ", 6);
2626 /* Set the important stuff up for command editing. */
2627 command_editing_p
= 1;
2628 history_expansion_p
= 0;
2629 write_history_p
= 0;
2631 /* Setup important stuff for command line editing. */
2632 rl_completion_entry_function
= (int (*)()) symbol_completion_function
;
2633 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
2634 rl_completer_quote_characters
= gdb_completer_quote_characters
;
2635 rl_readline_name
= "gdb";
2637 /* Define the classes of commands.
2638 They will appear in the help list in the reverse of this order. */
2640 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
2641 "Maintenance commands.\n\
2642 Some gdb commands are provided just for use by gdb maintainers.\n\
2643 These commands are subject to frequent change, and may not be as\n\
2644 well documented as user commands.",
2646 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
2647 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
2648 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
2649 The commands in this class are those defined by the user.\n\
2650 Use the \"define\" command to define a command.", &cmdlist
);
2651 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
2652 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
2653 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
2654 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
2655 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
2656 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
2657 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2658 counting from zero for the innermost (currently executing) frame.\n\n\
2659 At any time gdb identifies one frame as the \"selected\" frame.\n\
2660 Variable lookups are done with respect to the selected frame.\n\
2661 When the program being debugged stops, gdb selects the innermost frame.\n\
2662 The commands below can be used to select other frames by number or address.",
2664 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
2666 add_com ("pwd", class_files
, pwd_command
,
2667 "Print working directory. This is used for your program as well.");
2668 c
= add_cmd ("cd", class_files
, cd_command
,
2669 "Set working directory to DIR for debugger and program being debugged.\n\
2670 The change does not take effect for the program being debugged\n\
2671 until the next time it is started.", &cmdlist
);
2672 c
->completer
= filename_completer
;
2675 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
2680 add_com ("echo", class_support
, echo_command
,
2681 "Print a constant string. Give string as argument.\n\
2682 C escape sequences may be used in the argument.\n\
2683 No newline is added at the end of the argument;\n\
2684 use \"\\n\" if you want a newline to be printed.\n\
2685 Since leading and trailing whitespace are ignored in command arguments,\n\
2686 if you want to print some you must use \"\\\" before leading whitespace\n\
2687 to be printed or after trailing whitespace.");
2688 add_com ("document", class_support
, document_command
,
2689 "Document a user-defined command.\n\
2690 Give command name as argument. Give documentation on following lines.\n\
2691 End with a line of just \"end\".");
2692 add_com ("define", class_support
, define_command
,
2693 "Define a new command name. Command name is argument.\n\
2694 Definition appears on following lines, one command per line.\n\
2695 End with a line of just \"end\".\n\
2696 Use the \"document\" command to give documentation for the new command.\n\
2697 Commands defined in this way do not take arguments.");
2700 c
= add_cmd ("source", class_support
, source_command
,
2701 "Read commands from a file named FILE.\n\
2702 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
2703 when gdb is started.", &cmdlist
);
2705 /* Punt file name, we can't help it easily. */
2706 c
= add_cmd ("source", class_support
, source_command
,
2707 "Read commands from a file named FILE.\n\
2708 Note that the file \".gdbinit\" is read automatically in this way\n\
2709 when gdb is started.", &cmdlist
);
2711 c
->completer
= filename_completer
;
2713 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
2714 add_com ("help", class_support
, help_command
, "Print list of commands.");
2715 add_com_alias ("q", "quit", class_support
, 1);
2716 add_com_alias ("h", "help", class_support
, 1);
2719 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
2722 add_show_from_set (c
, &showlist
);
2723 c
->function
.sfunc
= set_verbose
;
2724 set_verbose (NULL
, 0, c
);
2727 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
2728 "Set editing of command lines as they are typed.\n\
2729 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2730 Without an argument, command line editing is enabled. To edit, use\n\
2731 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
2734 add_prefix_cmd ("history", class_support
, set_history
,
2735 "Generic command for setting command history parameters.",
2736 &sethistlist
, "set history ", 0, &setlist
);
2737 add_prefix_cmd ("history", class_support
, show_history
,
2738 "Generic command for showing command history parameters.",
2739 &showhistlist
, "show history ", 0, &showlist
);
2742 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
2743 "Set history expansion on command input.\n\
2744 Without an argument, history expansion is enabled.", &sethistlist
),
2748 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
2749 "Set saving of the history record on exit.\n\
2750 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2751 Without an argument, saving is enabled.", &sethistlist
),
2754 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
2755 "Set the size of the command history, \n\
2756 ie. the number of previous commands to keep a record of.", &sethistlist
);
2757 add_show_from_set (c
, &showhistlist
);
2758 c
->function
.sfunc
= set_history_size_command
;
2761 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
2762 "Set the filename in which to record the command history\n\
2763 (the list of previous commands of which a record is kept).", &sethistlist
),
2767 (add_set_cmd ("confirm", class_support
, var_boolean
,
2769 "Set whether to confirm potentially dangerous operations.",
2773 add_prefix_cmd ("info", class_info
, info_command
,
2774 "Generic command for showing things about the program being debugged.",
2775 &infolist
, "info ", 0, &cmdlist
);
2776 add_com_alias ("i", "info", class_info
, 1);
2778 add_prefix_cmd ("show", class_info
, show_command
,
2779 "Generic command for showing things about the debugger.",
2780 &showlist
, "show ", 0, &cmdlist
);
2781 /* Another way to get at the same thing. */
2782 add_info ("set", show_command
, "Show all GDB settings.");
2784 add_cmd ("commands", no_class
, show_commands
,
2785 "Show the the history of commands you typed.\n\
2786 You can supply a command number to start with, or a `+' to start after\n\
2787 the previous command number shown.",
2790 add_cmd ("version", no_class
, show_version
,
2791 "Show what version of GDB this is.", &showlist
);
2794 add_set_cmd ("remotedebug", no_class
, var_boolean
, (char *)&remote_debug
,
2795 "Set debugging of remote protocol.\n\
2796 When enabled, each packet sent or received with the remote target\n\
2797 is displayed.", &setlist
),