1 /* Top level `main' program for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "call-cmds.h"
28 #include "breakpoint.h"
30 #include "expression.h"
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 ((GDB_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_name
;
185 /* Canonical target name as a string. */
187 extern char *target_name
;
189 extern char lang_frame_mismatch_warn
[]; /* language.c */
191 /* Flag for whether we want all the "from_tty" gubbish printed. */
193 int caution
= 1; /* Default is yes, sigh. */
196 * Define all cmd_list_element's
199 /* Chain containing all defined commands. */
201 struct cmd_list_element
*cmdlist
;
203 /* Chain containing all defined info subcommands. */
205 struct cmd_list_element
*infolist
;
207 /* Chain containing all defined enable subcommands. */
209 struct cmd_list_element
*enablelist
;
211 /* Chain containing all defined disable subcommands. */
213 struct cmd_list_element
*disablelist
;
215 /* Chain containing all defined delete subcommands. */
217 struct cmd_list_element
*deletelist
;
219 /* Chain containing all defined "enable breakpoint" subcommands. */
221 struct cmd_list_element
*enablebreaklist
;
223 /* Chain containing all defined set subcommands */
225 struct cmd_list_element
*setlist
;
227 /* Chain containing all defined unset subcommands */
229 struct cmd_list_element
*unsetlist
;
231 /* Chain containing all defined show subcommands. */
233 struct cmd_list_element
*showlist
;
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 static char 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 *));
292 extern int mapped_symbol_files
;
293 extern int readnow_symbol_files
;
298 /* gdb prints this when reading a command interactively */
301 /* Buffer used for reading command lines, and the size
302 allocated for it so far. */
307 /* Baud rate specified for talking to serial target systems. Default
308 is left as -1, so targets can choose their own defaults. */
312 /* Non-zero tells remote* modules to output debugging info. */
314 int remote_debug
= 0;
316 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
320 #define STOP_SIGNAL SIGTSTP
321 static void stop_sig
PARAMS ((int));
325 /* Some System V have job control but not sigsetmask(). */
326 #if !defined (HAVE_SIGSETMASK)
328 #define HAVE_SIGSETMASK 1
330 #define HAVE_SIGSETMASK 0
334 #if 0 == (HAVE_SIGSETMASK)
335 #define sigsetmask(n)
338 /* Where to go for return_to_top_level (RETURN_ERROR). */
339 static jmp_buf error_return
;
340 /* Where to go for return_to_top_level (RETURN_QUIT). */
341 static jmp_buf quit_return
;
343 /* Temporary variable for SET_TOP_LEVEL. */
344 static int top_level_val
;
346 /* Do a setjmp on error_return and quit_return. catch_errors is
347 generally a cleaner way to do this, but main() would look pretty
348 ugly if it had to use catch_errors each time. */
350 #define SET_TOP_LEVEL() \
351 (((top_level_val = setjmp (error_return)) \
352 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \
355 /* Return for reason REASON. This generally gets back to the command
356 loop, but can be caught via catch_errors. */
359 return_to_top_level (reason
)
360 enum return_reason reason
;
365 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
366 I can think of a reason why that is vital, though). */
367 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
369 disable_current_display ();
370 do_cleanups (ALL_CLEANUPS
);
371 (NORETURN
void) longjmp
372 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
375 /* Call FUNC with arg ARGS, catching any errors. If there is no
376 error, return the value returned by FUNC. If there is an error,
377 print ERRSTRING, print the specific error message, then return
380 Must not be called with immediate_quit in effect (bad things might
381 happen, say we got a signal in the middle of a memcpy to quit_return).
382 This is an OK restriction; with very few exceptions immediate_quit can
383 be replaced by judicious use of QUIT.
385 MASK specifies what to catch; it is normally set to
386 RETURN_MASK_ALL, if for no other reason than that the code which
387 calls catch_errors might not be set up to deal with a quit which
388 isn't caught. But if the code can deal with it, it generally
389 should be RETURN_MASK_ERROR, unless for some reason it is more
390 useful to abort only the portion of the operation inside the
391 catch_errors. Note that quit should return to the command line
392 fairly quickly, even if some further processing is being done. */
395 catch_errors (func
, args
, errstring
, mask
)
396 int (*func
) PARAMS ((char *));
405 struct cleanup
*saved_cleanup_chain
;
406 char *saved_error_pre_print
;
408 saved_cleanup_chain
= save_cleanups ();
409 saved_error_pre_print
= error_pre_print
;
411 if (mask
& RETURN_MASK_ERROR
)
412 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (jmp_buf));
413 if (mask
& RETURN_MASK_QUIT
)
414 memcpy (saved_quit
, quit_return
, sizeof (jmp_buf));
415 error_pre_print
= errstring
;
417 if (setjmp (tmp_jmp
) == 0)
419 if (mask
& RETURN_MASK_ERROR
)
420 memcpy (error_return
, tmp_jmp
, sizeof (jmp_buf));
421 if (mask
& RETURN_MASK_QUIT
)
422 memcpy (quit_return
, tmp_jmp
, sizeof (jmp_buf));
423 val
= (*func
) (args
);
428 restore_cleanups (saved_cleanup_chain
);
430 error_pre_print
= saved_error_pre_print
;
431 if (mask
& RETURN_MASK_ERROR
)
432 memcpy (error_return
, saved_error
, sizeof (jmp_buf));
433 if (mask
& RETURN_MASK_QUIT
)
434 memcpy (quit_return
, saved_quit
, sizeof (jmp_buf));
438 /* Handler for SIGHUP. */
444 catch_errors (quit_cover
, NULL
,
445 "Could not kill the program being debugged", RETURN_MASK_ALL
);
446 signal (SIGHUP
, SIG_DFL
);
447 kill (getpid (), SIGHUP
);
450 /* Just a little helper function for disconnect(). */
456 caution
= 0; /* Throw caution to the wind -- we're exiting.
457 This prevents asking the user dumb questions. */
458 quit_command((char *)0, 0);
462 /* Line number we are currently in in a file which is being sourced. */
463 static int source_line_number
;
465 /* Name of the file we are sourcing. */
466 static char *source_file_name
;
468 /* Buffer containing the error_pre_print used by the source stuff.
470 static char *source_error
;
471 static int source_error_allocated
;
473 /* Something to glom on to the start of error_pre_print if source_file_name
475 static char *source_pre_error
;
477 /* Clean up on error during a "source" command (or execution of a
478 user-defined command). */
481 source_cleanup (stream
)
484 /* Restore the previous input stream. */
488 /* Read commands from STREAM. */
490 read_command_file (stream
)
493 struct cleanup
*cleanups
;
495 cleanups
= make_cleanup (source_cleanup
, instream
);
498 do_cleanups (cleanups
);
507 static int quiet
= 0;
508 static int batch
= 0;
510 /* Pointers to various arguments from command line. */
512 char *execarg
= NULL
;
513 char *corearg
= NULL
;
517 /* These are static so that we can take their address in an initializer. */
518 static int print_help
;
519 static int print_version
;
521 /* Pointers to all arguments of --command option. */
523 /* Allocated size of cmdarg. */
525 /* Number of elements of cmdarg used. */
528 /* Indices of all arguments of --directory option. */
530 /* Allocated size. */
532 /* Number of elements used. */
535 struct stat homebuf
, cwdbuf
;
536 char *homedir
, *homeinit
;
540 /* This needs to happen before the first use of malloc. */
541 init_malloc ((PTR
) NULL
);
543 #if defined (ALIGN_STACK_ON_STARTUP)
544 i
= (int) &count
& 0x3;
549 /* If error() is called from initialization code, just exit */
550 if (SET_TOP_LEVEL ()) {
555 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
558 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
562 line
= (char *) xmalloc (linesize
);
563 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
566 getcwd (dirbuf
, sizeof (dirbuf
));
567 current_directory
= dirbuf
;
569 #ifdef SET_STACK_LIMIT_HUGE
573 /* Set the stack limit huge so that alloca (particularly stringtab
574 * in dbxread.c) does not fail. */
575 getrlimit (RLIMIT_STACK
, &rlim
);
576 original_stack_limit
= rlim
.rlim_cur
;
577 rlim
.rlim_cur
= rlim
.rlim_max
;
578 setrlimit (RLIMIT_STACK
, &rlim
);
580 #endif /* SET_STACK_LIMIT_HUGE */
582 /* Parse arguments and options. */
585 /* When var field is 0, use flag field to record the equivalent
586 short option (or arbitrary numbers starting at 10 for those
587 with no equivalent). */
588 static struct option long_options
[] =
590 {"readnow", no_argument
, &readnow_symbol_files
, 1},
591 {"r", no_argument
, &readnow_symbol_files
, 1},
592 {"mapped", no_argument
, &mapped_symbol_files
, 1},
593 {"m", no_argument
, &mapped_symbol_files
, 1},
594 {"quiet", no_argument
, &quiet
, 1},
595 {"q", no_argument
, &quiet
, 1},
596 {"silent", no_argument
, &quiet
, 1},
597 {"nx", no_argument
, &inhibit_gdbinit
, 1},
598 {"n", no_argument
, &inhibit_gdbinit
, 1},
599 {"batch", no_argument
, &batch
, 1},
600 {"epoch", no_argument
, &epoch_interface
, 1},
601 {"fullname", no_argument
, &frame_file_full_name
, 1},
602 {"f", no_argument
, &frame_file_full_name
, 1},
603 {"help", no_argument
, &print_help
, 1},
604 {"se", required_argument
, 0, 10},
605 {"symbols", required_argument
, 0, 's'},
606 {"s", required_argument
, 0, 's'},
607 {"exec", required_argument
, 0, 'e'},
608 {"e", required_argument
, 0, 'e'},
609 {"core", required_argument
, 0, 'c'},
610 {"c", required_argument
, 0, 'c'},
611 {"command", required_argument
, 0, 'x'},
612 {"version", no_argument
, &print_version
, 1},
613 {"x", required_argument
, 0, 'x'},
614 {"directory", required_argument
, 0, 'd'},
615 {"cd", required_argument
, 0, 11},
616 {"tty", required_argument
, 0, 't'},
617 {"baud", required_argument
, 0, 'b'},
618 {"b", required_argument
, 0, 'b'},
619 /* Allow machine descriptions to add more options... */
620 #ifdef ADDITIONAL_OPTIONS
623 {0, no_argument
, 0, 0},
630 c
= getopt_long_only (argc
, argv
, "",
631 long_options
, &option_index
);
635 /* Long option that takes an argument. */
636 if (c
== 0 && long_options
[option_index
].flag
== 0)
637 c
= long_options
[option_index
].val
;
642 /* Long option that just sets a flag. */
661 cmdarg
[ncmd
++] = optarg
;
665 cmdarg
= (char **) xrealloc ((char *)cmdarg
,
666 cmdsize
* sizeof (*cmdarg
));
670 dirarg
[ndir
++] = optarg
;
674 dirarg
= (char **) xrealloc ((char *)dirarg
,
675 dirsize
* sizeof (*dirarg
));
689 i
= strtol (optarg
, &p
, 0);
690 if (i
== 0 && p
== optarg
)
691 warning ("Could not set baud rate to `%s'.\n", optarg
);
697 #ifdef ADDITIONAL_OPTION_CASES
698 ADDITIONAL_OPTION_CASES
701 fprintf_unfiltered (gdb_stderr
,
702 "Use `%s --help' for a complete list of options.\n",
708 /* OK, that's all the options. The other arguments are filenames. */
710 for (; optind
< argc
; optind
++)
714 symarg
= argv
[optind
];
715 execarg
= argv
[optind
];
718 corearg
= argv
[optind
];
721 fprintf_unfiltered (gdb_stderr
,
722 "Excess command line arguments ignored. (%s%s)\n",
723 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
730 /* Run the init function of each source file */
732 init_cmd_lists (); /* This needs to be done first */
733 initialize_all_files ();
734 init_main (); /* But that omits this file! Do it now */
737 /* Do these (and anything which might call wrap_here or *_filtered)
738 after initialize_all_files. */
741 print_gdb_version (gdb_stdout
);
743 printf_filtered ("\n");
749 /* --version is intentionally not documented here, because we
750 are printing the version here, and the help is long enough
753 print_gdb_version (gdb_stdout
);
754 /* Make sure the output gets printed. */
756 printf_filtered ("\n");
758 /* But don't use *_filtered here. We don't want to prompt for continue
759 no matter how small the screen or how much we're going to print. */
761 This is the GNU debugger. Usage:\n\
762 gdb [options] [executable-file [core-file or process-id]]\n\
764 --help Print this message.\n\
765 --quiet Do not print version number on startup.\n\
766 --fullname Output information used by emacs-GDB interface.\n\
767 --epoch Output information used by epoch emacs-GDB interface.\n\
768 --batch Exit after processing options.\n\
769 --nx Do not read .gdbinit file.\n\
770 --tty=TTY Use TTY for input/output by the program being debugged.\n\
771 --cd=DIR Change current directory to DIR.\n\
772 --directory=DIR Search for source files in DIR.\n\
773 --command=FILE Execute GDB commands from FILE.\n\
774 --symbols=SYMFILE Read symbols from SYMFILE.\n\
775 --exec=EXECFILE Use EXECFILE as the executable.\n\
776 --se=FILE Use FILE as symbol file and executable file.\n\
777 --core=COREFILE Analyze the core dump COREFILE.\n\
778 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
779 --mapped Use mapped symbol files if supported on this system.\n\
780 --readnow Fully read symbol files on first access.\n\
782 #ifdef ADDITIONAL_OPTION_HELP
783 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, gdb_stdout
);
785 fputs_unfiltered ("\n\
786 For more information, type \"help\" from within GDB, or consult the\n\
787 GDB manual (available as on-line info or a printed manual).\n", gdb_stdout
);
793 /* Print all the junk at the top, with trailing "..." if we are about
794 to read a symbol file (possibly slowly). */
795 print_gnu_advertisement ();
796 print_gdb_version (gdb_stdout
);
798 printf_filtered ("..");
800 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
803 error_pre_print
= "\n\n";
804 /* We may get more than one warning, don't double space all of them... */
805 warning_pre_print
= "\nwarning: ";
807 /* We need a default language for parsing expressions, so simple things like
808 "set width 0" won't fail if no language is explicitly set in a config file
809 or implicitly set by reading an executable during startup. */
810 set_language (language_c
);
811 expected_language
= current_language
; /* don't warn about the change. */
813 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
814 *before* all the command line arguments are processed; it sets
815 global parameters, which are independent of what file you are
816 debugging or what directory you are in. */
817 homedir
= getenv ("HOME");
820 homeinit
= (char *) alloca (strlen (getenv ("HOME")) +
821 strlen (gdbinit
) + 10);
822 strcpy (homeinit
, getenv ("HOME"));
823 strcat (homeinit
, "/");
824 strcat (homeinit
, gdbinit
);
825 if (!inhibit_gdbinit
&& access (homeinit
, R_OK
) == 0)
827 if (!SET_TOP_LEVEL ())
828 source_command (homeinit
, 0);
830 do_cleanups (ALL_CLEANUPS
);
832 /* Do stats; no need to do them elsewhere since we'll only
833 need them if homedir is set. Make sure that they are
834 zero in case one of them fails (this guarantees that they
835 won't match if either exists). */
837 memset (&homebuf
, 0, sizeof (struct stat
));
838 memset (&cwdbuf
, 0, sizeof (struct stat
));
840 stat (homeinit
, &homebuf
);
841 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
845 /* Now perform all the actions indicated by the arguments. */
848 if (!SET_TOP_LEVEL ())
850 cd_command (cdarg
, 0);
854 do_cleanups (ALL_CLEANUPS
);
856 for (i
= 0; i
< ndir
; i
++)
857 if (!SET_TOP_LEVEL ())
858 directory_command (dirarg
[i
], 0);
860 do_cleanups (ALL_CLEANUPS
);
864 && STREQ (execarg
, symarg
))
866 /* The exec file and the symbol-file are the same. If we can't open
867 it, better only print one error message. */
868 if (!SET_TOP_LEVEL ())
870 exec_file_command (execarg
, !batch
);
871 symbol_file_command (symarg
, 0);
877 if (!SET_TOP_LEVEL ())
878 exec_file_command (execarg
, !batch
);
880 if (!SET_TOP_LEVEL ())
881 symbol_file_command (symarg
, 0);
883 do_cleanups (ALL_CLEANUPS
);
885 /* After the symbol file has been read, print a newline to get us
886 beyond the copyright line... But errors should still set off
887 the error message with a (single) blank line. */
889 printf_filtered ("\n");
890 error_pre_print
= "\n";
891 warning_pre_print
= "\nwarning: ";
894 if (!SET_TOP_LEVEL ())
895 core_file_command (corearg
, !batch
);
896 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
897 attach_command (corearg
, !batch
);
898 do_cleanups (ALL_CLEANUPS
);
901 if (!SET_TOP_LEVEL ())
902 tty_command (ttyarg
, !batch
);
903 do_cleanups (ALL_CLEANUPS
);
905 #ifdef ADDITIONAL_OPTION_HANDLER
906 ADDITIONAL_OPTION_HANDLER
;
909 /* Error messages should no longer be distinguished with extra output. */
911 warning_pre_print
= "warning: ";
913 /* Read the .gdbinit file in the current directory, *if* it isn't
914 the same as the $HOME/.gdbinit file (it should exist, also). */
917 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
918 if (!inhibit_gdbinit
&& access (gdbinit
, R_OK
) == 0)
920 if (!SET_TOP_LEVEL ())
921 source_command (gdbinit
, 0);
923 do_cleanups (ALL_CLEANUPS
);
925 for (i
= 0; i
< ncmd
; i
++)
927 if (!SET_TOP_LEVEL ())
929 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
930 read_command_file (stdin
);
932 source_command (cmdarg
[i
], !batch
);
933 do_cleanups (ALL_CLEANUPS
);
938 /* Read in the old history after all the command files have been read. */
943 /* We have hit the end of the batch file. */
947 /* Do any host- or target-specific hacks. This is used for i960 targets
948 to force the user to set a nindy target and spec its parameters. */
950 #ifdef BEFORE_MAIN_LOOP_HOOK
951 BEFORE_MAIN_LOOP_HOOK
;
954 /* The command loop. */
958 if (!SET_TOP_LEVEL ())
960 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
962 quit_command ((char *)0, instream
== stdin
);
965 /* No exit -- exit is through quit_command. */
969 execute_user_command (c
, args
)
970 struct cmd_list_element
*c
;
973 register struct command_line
*cmdlines
;
974 struct cleanup
*old_chain
;
977 error ("User-defined commands cannot take arguments.");
979 cmdlines
= c
->user_commands
;
984 /* Set the instream to 0, indicating execution of a
985 user-defined function. */
986 old_chain
= make_cleanup (source_cleanup
, instream
);
987 instream
= (FILE *) 0;
990 execute_command (cmdlines
->line
, 0);
991 cmdlines
= cmdlines
->next
;
993 do_cleanups (old_chain
);
996 /* Execute the line P as a command.
997 Pass FROM_TTY as second argument to the defining function. */
1000 execute_command (p
, from_tty
)
1004 register struct cmd_list_element
*c
;
1005 register enum language flang
;
1006 static int warned
= 0;
1010 /* This can happen when command_line_input hits end of file. */
1014 while (*p
== ' ' || *p
== '\t') p
++;
1019 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1020 /* Pass null arg rather than an empty one. */
1023 /* If this command has been hooked, run the hook first. */
1025 execute_user_command (c
->hook
, (char *)0);
1027 if (c
->class == class_user
)
1028 execute_user_command (c
, arg
);
1029 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1030 do_setshow_command (arg
, from_tty
& caution
, c
);
1031 else if (c
->function
.cfunc
== NO_FUNCTION
)
1032 error ("That is not a command, just a help topic.");
1034 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1037 /* Tell the user if the language has changed (except first time). */
1038 if (current_language
!= expected_language
)
1040 if (language_mode
== language_mode_auto
) {
1041 language_info (1); /* Print what changed. */
1046 /* Warn the user if the working language does not match the
1047 language of the current frame. Only warn the user if we are
1048 actually running the program, i.e. there is a stack. */
1049 /* FIXME: This should be cacheing the frame and only running when
1050 the frame changes. */
1051 if (target_has_stack
)
1053 flang
= get_frame_language ();
1055 && flang
!= language_unknown
1056 && flang
!= current_language
->la_language
)
1058 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1066 command_loop_marker (foo
)
1071 /* Read commands from `instream' and execute them
1072 until end of file or error reading instream. */
1076 struct cleanup
*old_chain
;
1078 int stdin_is_tty
= ISATTY (stdin
);
1080 while (!feof (instream
))
1082 if (window_hook
&& instream
== stdin
)
1083 (*window_hook
) (instream
, prompt
);
1086 if (instream
== stdin
&& stdin_is_tty
)
1087 reinitialize_more_filter ();
1088 old_chain
= make_cleanup (command_loop_marker
, 0);
1089 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1093 execute_command (command
, instream
== stdin
);
1094 /* Do any commands attached to breakpoint we stopped at. */
1095 bpstat_do_actions (&stop_bpstat
);
1096 do_cleanups (old_chain
);
1100 /* Commands call this if they do not want to be repeated by null lines. */
1105 /* If we aren't reading from standard input, we are saving the last
1106 thing read from stdin in line and don't want to delete it. Null lines
1107 won't repeat here in any case. */
1108 if (instream
== stdin
)
1112 /* Read a line from the stream "instream" without command line editing.
1114 It prints PRROMPT once at the start.
1115 Action is compatible with "readline", e.g. space for the result is
1116 malloc'd and should be freed by the caller.
1118 A NULL return means end of file. */
1120 gdb_readline (prrompt
)
1125 int input_index
= 0;
1126 int result_size
= 80;
1130 /* Don't use a _filtered function here. It causes the assumed
1131 character position to be off, since the newline we read from
1132 the user is not accounted for. */
1133 fputs_unfiltered (prrompt
, gdb_stdout
);
1134 gdb_flush (gdb_stdout
);
1137 result
= (char *) xmalloc (result_size
);
1141 /* Read from stdin if we are executing a user defined command.
1142 This is the right thing for prompt_for_continue, at least. */
1143 c
= fgetc (instream
? instream
: stdin
);
1147 if (input_index
> 0)
1148 /* The last line does not end with a newline. Return it, and
1149 if we are called again fgetc will still return EOF and
1150 we'll return NULL then. */
1159 result
[input_index
++] = c
;
1160 while (input_index
>= result_size
)
1163 result
= (char *) xrealloc (result
, result_size
);
1167 result
[input_index
++] = '\0';
1171 /* Variables which control command line editing and history
1172 substitution. These variables are given default values at the end
1174 static int command_editing_p
;
1175 static int history_expansion_p
;
1176 static int write_history_p
;
1177 static int history_size
;
1178 static char *history_filename
;
1180 /* readline uses the word breaks for two things:
1181 (1) In figuring out where to point the TEXT parameter to the
1182 rl_completion_entry_function. Since we don't use TEXT for much,
1183 it doesn't matter a lot what the word breaks are for this purpose, but
1184 it does affect how much stuff M-? lists.
1185 (2) If one of the matches contains a word break character, readline
1186 will quote it. That's why we switch between
1187 gdb_completer_word_break_characters and
1188 gdb_completer_command_word_break_characters. I'm not sure when
1189 we need this behavior (perhaps for funky characters in C++ symbols?). */
1191 /* Variables which are necessary for fancy command line editing. */
1192 char *gdb_completer_word_break_characters
=
1193 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1195 /* When completing on command names, we remove '-' from the list of
1196 word break characters, since we use it in command names. If the
1197 readline library sees one in any of the current completion strings,
1198 it thinks that the string needs to be quoted and automatically supplies
1200 char *gdb_completer_command_word_break_characters
=
1201 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1203 /* Characters that can be used to quote completion strings. Note that we
1204 can't include '"' because the gdb C parser treats such quoted sequences
1206 char *gdb_completer_quote_characters
=
1209 /* Functions that are used as part of the fancy command line editing. */
1211 /* This can be used for functions which don't want to complete on symbols
1212 but don't want to complete on anything else either. */
1215 noop_completer (text
, prefix
)
1222 /* Complete on filenames. */
1224 filename_completer (text
, word
)
1228 /* From readline. */
1229 extern char *filename_completion_function ();
1230 int subsequent_name
;
1232 int return_val_used
;
1233 int return_val_alloced
;
1235 return_val_used
= 0;
1236 /* Small for testing. */
1237 return_val_alloced
= 1;
1238 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1240 subsequent_name
= 0;
1244 p
= filename_completion_function (text
, subsequent_name
);
1245 if (return_val_used
>= return_val_alloced
)
1247 return_val_alloced
*= 2;
1249 (char **) xrealloc (return_val
,
1250 return_val_alloced
* sizeof (char *));
1254 return_val
[return_val_used
++] = p
;
1257 /* Like emacs, don't complete on old versions. Especially useful
1258 in the "source" command. */
1259 if (p
[strlen (p
) - 1] == '~')
1265 /* Return exactly p. */
1266 return_val
[return_val_used
++] = p
;
1267 else if (word
> text
)
1269 /* Return some portion of p. */
1270 q
= xmalloc (strlen (p
) + 5);
1271 strcpy (q
, p
+ (word
- text
));
1272 return_val
[return_val_used
++] = q
;
1277 /* Return some of TEXT plus p. */
1278 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1279 strncpy (q
, word
, text
- word
);
1280 q
[text
- word
] = '\0';
1282 return_val
[return_val_used
++] = q
;
1286 subsequent_name
= 1;
1289 /* There is no way to do this just long enough to affect quote inserting
1290 without also affecting the next completion. This should be fixed in
1292 /* Insure that readline does the right thing
1293 with respect to inserting quotes. */
1294 rl_completer_word_break_characters
= "";
1299 /* Here are some useful test cases for completion. FIXME: These should
1300 be put in the test suite. They should be tested with both M-? and TAB.
1302 "show output-" "radix"
1303 "show output" "-radix"
1304 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1305 "p " ambiguous (all symbols)
1306 "info t foo" no completions
1307 "info t " no completions
1308 "info t" ambiguous ("info target", "info terminal", etc.)
1309 "info ajksdlfk" no completions
1310 "info ajksdlfk " no completions
1312 "info " ambiguous (all info commands)
1313 "p \"a" no completions (string constant)
1314 "p 'a" ambiguous (all symbols starting with a)
1315 "p b-a" ambiguous (all symbols starting with a)
1316 "p b-" ambiguous (all symbols)
1317 "file Make" "file" (word break hard to screw up here)
1318 "file ../gdb.stabs/wi" "erd" (needs to not break word at slash)
1321 /* Generate completions one by one for the completer. Each time we are
1322 called return another potential completion to the caller. The function
1323 is misnamed; it just completes on commands or passes the buck to the
1324 command's completer function; the stuff specific to symbol completion
1325 is in make_symbol_completion_list.
1327 TEXT is readline's idea of the "word" we are looking at; we don't really
1328 like readline's ideas about word breaking so we ignore it.
1330 MATCHES is the number of matches that have currently been collected from
1331 calling this completion function. When zero, then we need to initialize,
1332 otherwise the initialization has already taken place and we can just
1333 return the next potential completion string.
1335 Returns NULL if there are no more completions, else a pointer to a string
1336 which is a possible completion.
1338 RL_LINE_BUFFER is available to be looked at; it contains the entire text
1339 of the line. RL_POINT is the offset in that line of the cursor. You
1340 should pretend that the line ends at RL_POINT. */
1343 symbol_completion_function (text
, matches
)
1347 static char **list
= (char **)NULL
; /* Cache of completions */
1348 static int index
; /* Next cached completion */
1349 char *output
= NULL
;
1350 char *tmp_command
, *p
;
1351 /* Pointer within tmp_command which corresponds to text. */
1353 struct cmd_list_element
*c
, *result_list
;
1354 extern char *rl_line_buffer
;
1355 extern int rl_point
;
1359 /* The caller is beginning to accumulate a new set of completions, so
1360 we need to find all of them now, and cache them for returning one at
1361 a time on future calls. */
1365 /* Free the storage used by LIST, but not by the strings inside.
1366 This is because rl_complete_internal () frees the strings. */
1372 /* Choose the default set of word break characters to break completions.
1373 If we later find out that we are doing completions on command strings
1374 (as opposed to strings supplied by the individual command completer
1375 functions, which can be any string) then we will switch to the
1376 special word break set for command strings, which leaves out the
1377 '-' character used in some commands. */
1379 rl_completer_word_break_characters
=
1380 gdb_completer_word_break_characters
;
1382 /* Decide whether to complete on a list of gdb commands or on symbols. */
1383 tmp_command
= (char *) alloca (rl_point
+ 1);
1386 strncpy (tmp_command
, rl_line_buffer
, rl_point
);
1387 tmp_command
[rl_point
] = '\0';
1388 /* Since text always contains some number of characters leading up
1389 to rl_point, we can find the equivalent position in tmp_command
1390 by subtracting that many characters from the end of tmp_command. */
1391 word
= tmp_command
+ rl_point
- strlen (text
);
1395 /* An empty line we want to consider ambiguous; that is, it
1396 could be any command. */
1397 c
= (struct cmd_list_element
*) -1;
1402 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1405 /* Move p up to the next interesting thing. */
1406 while (*p
== ' ' || *p
== '\t')
1413 /* It is an unrecognized command. So there are no
1414 possible completions. */
1417 else if (c
== (struct cmd_list_element
*) -1)
1421 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1422 doesn't advance over that thing itself. Do so now. */
1424 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1426 if (q
!= tmp_command
+ rl_point
)
1428 /* There is something beyond the ambiguous
1429 command, so there are no possible completions. For
1430 example, "info t " or "info t foo" does not complete
1431 to anything, because "info t" can be "info target" or
1437 /* We're trying to complete on the command which was ambiguous.
1438 This we can deal with. */
1441 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1446 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1448 /* Insure that readline does the right thing with respect to
1449 inserting quotes. */
1450 rl_completer_word_break_characters
=
1451 gdb_completer_command_word_break_characters
;
1456 /* We've recognized a full command. */
1458 if (p
== tmp_command
+ rl_point
)
1460 /* There is no non-whitespace in the line beyond the command. */
1462 if (p
[-1] == ' ' || p
[-1] == '\t')
1464 /* The command is followed by whitespace; we need to complete
1465 on whatever comes after command. */
1468 /* It is a prefix command; what comes after it is
1469 a subcommand (e.g. "info "). */
1470 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1472 /* Insure that readline does the right thing
1473 with respect to inserting quotes. */
1474 rl_completer_word_break_characters
=
1475 gdb_completer_command_word_break_characters
;
1479 /* It is a normal command; what comes after it is
1480 completed by the command's completer function. */
1481 list
= (*c
->completer
) (p
, word
);
1486 /* The command is not followed by whitespace; we need to
1487 complete on the command itself. e.g. "p" which is a
1488 command itself but also can complete to "print", "ptype"
1492 /* Find the command we are completing on. */
1494 while (q
> tmp_command
)
1496 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1502 list
= complete_on_cmdlist (result_list
, q
, word
);
1504 /* Insure that readline does the right thing
1505 with respect to inserting quotes. */
1506 rl_completer_word_break_characters
=
1507 gdb_completer_command_word_break_characters
;
1512 /* There is non-whitespace beyond the command. */
1514 if (c
->prefixlist
&& !c
->allow_unknown
)
1516 /* It is an unrecognized subcommand of a prefix command,
1517 e.g. "info adsfkdj". */
1522 /* It is a normal command. */
1523 list
= (*c
->completer
) (p
, word
);
1529 /* If we found a list of potential completions during initialization then
1530 dole them out one at a time. The vector of completions is NULL
1531 terminated, so after returning the last one, return NULL (and continue
1532 to do so) each time we are called after that, until a new list is
1537 output
= list
[index
];
1545 /* Can't do this because readline hasn't yet checked the word breaks
1546 for figuring out whether to insert a quote. */
1548 /* Make sure the word break characters are set back to normal for the
1549 next time that readline tries to complete something. */
1550 rl_completer_word_break_characters
=
1551 gdb_completer_word_break_characters
;
1557 /* Skip over a possibly quoted word (as defined by the quote characters
1558 and word break characters the completer uses). Returns pointer to the
1559 location after the "word". */
1565 char quote_char
= '\0';
1568 for (scan
= str
; *scan
!= '\0'; scan
++)
1570 if (quote_char
!= '\0')
1572 /* Ignore everything until the matching close quote char */
1573 if (*scan
== quote_char
)
1575 /* Found matching close quote. */
1580 else if (strchr (gdb_completer_quote_characters
, *scan
))
1582 /* Found start of a quoted string. */
1585 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1599 #if STOP_SIGNAL == SIGTSTP
1600 signal (SIGTSTP
, SIG_DFL
);
1602 kill (getpid (), SIGTSTP
);
1603 signal (SIGTSTP
, stop_sig
);
1605 signal (STOP_SIGNAL
, stop_sig
);
1607 printf_unfiltered ("%s", prompt
);
1608 gdb_flush (gdb_stdout
);
1610 /* Forget about any previous command -- null line now will do nothing. */
1613 #endif /* STOP_SIGNAL */
1615 /* Initialize signal handlers. */
1625 signal (SIGINT
, request_quit
);
1627 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1628 passed to the inferior, which we don't want. It would be
1629 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1630 on BSD4.3 systems using vfork, that can affect the
1631 GDB process as well as the inferior (the signal handling tables
1632 might be in memory, shared between the two). Since we establish
1633 a handler for SIGQUIT, when we call exec it will set the signal
1634 to SIG_DFL for us. */
1635 signal (SIGQUIT
, do_nothing
);
1636 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
1637 signal (SIGHUP
, disconnect
);
1638 signal (SIGFPE
, float_handler
);
1640 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1641 signal (SIGWINCH
, SIGWINCH_HANDLER
);
1645 /* Read one line from the command input stream `instream'
1646 into the local static buffer `linebuffer' (whose current length
1648 The buffer is made bigger as necessary.
1649 Returns the address of the start of the line.
1651 NULL is returned for end of file.
1653 *If* the instream == stdin & stdin is a terminal, the line read
1654 is copied into the file line saver (global var char *line,
1655 length linesize) so that it can be duplicated.
1657 This routine either uses fancy command line editing or
1658 simple input as the user has requested. */
1661 command_line_input (prrompt
, repeat
)
1665 static char *linebuffer
= 0;
1666 static unsigned linelength
= 0;
1670 char *local_prompt
= prrompt
;
1675 if (linebuffer
== 0)
1678 linebuffer
= (char *) xmalloc (linelength
);
1683 /* Control-C quits instantly if typed while in this loop
1684 since it should not wait until the user types a newline. */
1688 signal (STOP_SIGNAL
, stop_sig
);
1693 /* Reports are that some Sys V's don't flush gdb_stdout/err on reads
1694 from stdin, when stdin/out are sockets rather than ttys. So we
1695 have to do it ourselves, to make emacs-gdb and xxgdb work.
1696 On other machines, doing this once per input should be a cheap nop. */
1697 gdb_flush (gdb_stdout
);
1698 gdb_flush (gdb_stderr
);
1700 if (source_file_name
!= NULL
)
1702 ++source_line_number
;
1703 sprintf (source_error
,
1704 "%s%s:%d: Error in sourced command file:\n",
1707 source_line_number
);
1708 error_pre_print
= source_error
;
1711 /* Don't use fancy stuff if not talking to stdin. */
1712 if (command_editing_p
&& instream
== stdin
1713 && ISATTY (instream
))
1714 rl
= readline (local_prompt
);
1716 rl
= gdb_readline (local_prompt
);
1718 if (!rl
|| rl
== (char *) EOF
)
1723 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
1725 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
1726 nline
= (char *) xrealloc (linebuffer
, linelength
);
1727 p
+= nline
- linebuffer
;
1731 /* Copy line. Don't copy null at end. (Leaves line alone
1732 if this was just a newline) */
1736 free (rl
); /* Allocated in readline. */
1738 if (p
== linebuffer
|| *(p
- 1) != '\\')
1741 p
--; /* Put on top of '\'. */
1742 local_prompt
= (char *) 0;
1747 signal (STOP_SIGNAL
, SIG_DFL
);
1754 /* Do history expansion if that is wished. */
1755 if (history_expansion_p
&& instream
== stdin
1756 && ISATTY (instream
))
1758 char *history_value
;
1761 *p
= '\0'; /* Insert null now. */
1762 expanded
= history_expand (linebuffer
, &history_value
);
1765 /* Print the changes. */
1766 printf_unfiltered ("%s\n", history_value
);
1768 /* If there was an error, call this function again. */
1771 free (history_value
);
1772 return command_line_input (prrompt
, repeat
);
1774 if (strlen (history_value
) > linelength
)
1776 linelength
= strlen (history_value
) + 1;
1777 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
1779 strcpy (linebuffer
, history_value
);
1780 p
= linebuffer
+ strlen(linebuffer
);
1781 free (history_value
);
1785 /* If we just got an empty line, and that is supposed
1786 to repeat the previous command, return the value in the
1790 if (p
== linebuffer
)
1793 while (*p1
== ' ' || *p1
== '\t')
1801 /* Add line to history if appropriate. */
1802 if (instream
== stdin
1803 && ISATTY (stdin
) && *linebuffer
)
1804 add_history (linebuffer
);
1806 /* Note: lines consisting soley of comments are added to the command
1807 history. This is useful when you type a command, and then
1808 realize you don't want to execute it quite yet. You can comment
1809 out the command and then later fetch it from the value history
1810 and remove the '#'. The kill ring is probably better, but some
1811 people are in the habit of commenting things out. */
1813 while ((c
= *p1
++) != '\0')
1816 while ((c
= *p1
++) != '"')
1818 /* Make sure an escaped '"' doesn't make us think the string
1826 while ((c
= *p1
++) != '\'')
1828 /* Make sure an escaped '\'' doesn't make us think the string
1837 /* Found a comment. */
1843 /* Save into global buffer if appropriate. */
1846 if (linelength
> linesize
)
1848 line
= xrealloc (line
, linelength
);
1849 linesize
= linelength
;
1851 strcpy (line
, linebuffer
);
1858 /* Read lines from the input stream
1859 and accumulate them in a chain of struct command_line's
1860 which is then returned. */
1862 struct command_line
*
1863 read_command_lines ()
1865 struct command_line
*first
= 0;
1866 register struct command_line
*next
, *tail
= 0;
1867 register char *p
, *p1
;
1868 struct cleanup
*old_chain
= 0;
1873 p
= command_line_input ((char *) NULL
, instream
== stdin
);
1875 /* Treat end of file like "end". */
1878 /* Remove leading and trailing blanks. */
1879 while (*p
== ' ' || *p
== '\t') p
++;
1880 p1
= p
+ strlen (p
);
1881 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t')) p1
--;
1883 /* Is this "end"? */
1884 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
1887 /* No => add this line to the chain of command lines. */
1888 next
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1889 next
->line
= savestring (p
, p1
- p
);
1897 /* We just read the first line.
1898 From now on, arrange to throw away the lines we have
1899 if we quit or get an error while inside this function. */
1901 old_chain
= make_cleanup (free_command_lines
, &first
);
1908 /* Now we are about to return the chain to our caller,
1909 so freeing it becomes his responsibility. */
1911 discard_cleanups (old_chain
);
1915 /* Free a chain of struct command_line's. */
1918 free_command_lines (lptr
)
1919 struct command_line
**lptr
;
1921 register struct command_line
*l
= *lptr
;
1922 register struct command_line
*next
;
1933 /* Add an element to the list of info subcommands. */
1936 add_info (name
, fun
, doc
)
1938 void (*fun
) PARAMS ((char *, int));
1941 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
1944 /* Add an alias to the list of info subcommands. */
1947 add_info_alias (name
, oldname
, abbrev_flag
)
1952 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
1955 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1956 Therefore, its own definition is called only for "info" with no args. */
1960 info_command (arg
, from_tty
)
1964 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
1965 help_list (infolist
, "info ", -1, gdb_stdout
);
1968 /* The "show" command with no arguments shows all the settings. */
1972 show_command (arg
, from_tty
)
1976 cmd_show_list (showlist
, from_tty
, "");
1979 /* Add an element to the list of commands. */
1982 add_com (name
, class, fun
, doc
)
1984 enum command_class
class;
1985 void (*fun
) PARAMS ((char *, int));
1988 add_cmd (name
, class, fun
, doc
, &cmdlist
);
1991 /* Add an alias or abbreviation command to the list of commands. */
1994 add_com_alias (name
, oldname
, class, abbrev_flag
)
1997 enum command_class
class;
2000 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2007 error ("Argument required (%s).", why
);
2012 help_command (command
, from_tty
)
2014 int from_tty
; /* Ignored */
2016 help_cmd (command
, gdb_stdout
);
2020 validate_comname (comname
)
2026 error_no_arg ("name of command to define");
2031 if (!isalnum(*p
) && *p
!= '-')
2032 error ("Junk in argument list: \"%s\"", p
);
2037 /* This is just a placeholder in the command data structures. */
2039 user_defined_command (ignore
, from_tty
)
2046 define_command (comname
, from_tty
)
2050 register struct command_line
*cmds
;
2051 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2052 char *tem
= comname
;
2053 #define HOOK_STRING "hook-"
2056 validate_comname (comname
);
2058 /* Look it up, and verify that we got an exact match. */
2059 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2060 if (c
&& !STREQ (comname
, c
->name
))
2065 if (c
->class == class_user
|| c
->class == class_alias
)
2066 tem
= "Redefine command \"%s\"? ";
2068 tem
= "Really redefine built-in command \"%s\"? ";
2069 if (!query (tem
, c
->name
))
2070 error ("Command \"%s\" not redefined.", c
->name
);
2073 /* If this new command is a hook, then mark the command which it
2074 is hooking. Note that we allow hooking `help' commands, so that
2075 we can hook the `stop' pseudo-command. */
2077 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2079 /* Look up cmd it hooks, and verify that we got an exact match. */
2080 tem
= comname
+HOOK_LEN
;
2081 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2082 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2086 warning ("Your new `%s' command does not hook any existing command.",
2088 if (!query ("Proceed? ", (char *)0))
2089 error ("Not confirmed.");
2093 comname
= savestring (comname
, strlen (comname
));
2095 /* If the rest of the commands will be case insensitive, this one
2096 should behave in the same manner. */
2097 for (tem
= comname
; *tem
; tem
++)
2098 if (isupper(*tem
)) *tem
= tolower(*tem
);
2102 printf_unfiltered ("Type commands for definition of \"%s\".\n\
2103 End with a line saying just \"end\".\n", comname
);
2104 gdb_flush (gdb_stdout
);
2107 cmds
= read_command_lines ();
2109 if (c
&& c
->class == class_user
)
2110 free_command_lines (&c
->user_commands
);
2112 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2113 (c
&& c
->class == class_user
)
2114 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2115 newc
->user_commands
= cmds
;
2117 /* If this new command is a hook, then mark both commands as being
2121 hookc
->hook
= newc
; /* Target gets hooked. */
2122 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2127 document_command (comname
, from_tty
)
2131 struct command_line
*doclines
;
2132 register struct cmd_list_element
*c
;
2133 char *tem
= comname
;
2135 validate_comname (comname
);
2137 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2139 if (c
->class != class_user
)
2140 error ("Command \"%s\" is built-in.", comname
);
2143 printf_unfiltered ("Type documentation for \"%s\".\n\
2144 End with a line saying just \"end\".\n", comname
);
2146 doclines
= read_command_lines ();
2148 if (c
->doc
) free (c
->doc
);
2151 register struct command_line
*cl1
;
2152 register int len
= 0;
2154 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2155 len
+= strlen (cl1
->line
) + 1;
2157 c
->doc
= (char *) xmalloc (len
+ 1);
2160 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2162 strcat (c
->doc
, cl1
->line
);
2164 strcat (c
->doc
, "\n");
2168 free_command_lines (&doclines
);
2172 print_gnu_advertisement()
2174 printf_unfiltered ("\
2175 GDB is free software and you are welcome to distribute copies of it\n\
2176 under certain conditions; type \"show copying\" to see the conditions.\n\
2177 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2182 print_gdb_version (stream
)
2185 fprintf_filtered (stream
, "\
2186 GDB %s (%s", version
, host_name
);
2188 if (!STREQ (host_name
, target_name
))
2189 fprintf_filtered (stream
, " --target %s", target_name
);
2191 fprintf_filtered (stream
, "), ");
2193 fprintf_filtered (stream
, "Copyright 1994 Free Software Foundation, Inc.");
2198 show_version (args
, from_tty
)
2203 print_gnu_advertisement ();
2204 print_gdb_version (gdb_stdout
);
2205 printf_filtered ("\n");
2209 /* xgdb calls this to reprint the usual GDB prompt. */
2214 printf_unfiltered ("%s", prompt
);
2215 gdb_flush (gdb_stdout
);
2219 quit_command (args
, from_tty
)
2223 if (inferior_pid
!= 0 && target_has_execution
)
2227 if (query ("The program is running. Quit anyway (and detach it)? "))
2228 target_detach (args
, from_tty
);
2230 error ("Not confirmed.");
2234 if (query ("The program is running. Quit anyway (and kill it)? "))
2237 error ("Not confirmed.");
2240 /* Save the history information if it is appropriate to do so. */
2241 if (write_history_p
&& history_filename
)
2242 write_history (history_filename
);
2246 /* Returns whether GDB is running on a terminal and whether the user
2247 desires that questions be asked of them on that terminal. */
2250 input_from_terminal_p ()
2252 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
2257 pwd_command (args
, from_tty
)
2261 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
2262 getcwd (dirbuf
, sizeof (dirbuf
));
2264 if (!STREQ (dirbuf
, current_directory
))
2265 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2266 current_directory
, dirbuf
);
2268 printf_unfiltered ("Working directory %s.\n", current_directory
);
2272 cd_command (dir
, from_tty
)
2277 /* Found something other than leading repetitions of "/..". */
2278 int found_real_path
;
2281 /* If the new directory is absolute, repeat is a no-op; if relative,
2282 repeat might be useful but is more likely to be a mistake. */
2286 error_no_arg ("new working directory");
2288 dir
= tilde_expand (dir
);
2289 make_cleanup (free
, dir
);
2291 if (chdir (dir
) < 0)
2292 perror_with_name (dir
);
2295 dir
= savestring (dir
, len
- (len
> 1 && dir
[len
-1] == '/'));
2297 current_directory
= dir
;
2300 if (current_directory
[0] == '/' && current_directory
[1] == '\0')
2301 current_directory
= concat (current_directory
, dir
, NULL
);
2303 current_directory
= concat (current_directory
, "/", dir
, NULL
);
2307 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2309 found_real_path
= 0;
2310 for (p
= current_directory
; *p
;)
2312 if (p
[0] == '/' && p
[1] == '.' && (p
[2] == 0 || p
[2] == '/'))
2314 else if (p
[0] == '/' && p
[1] == '.' && p
[2] == '.'
2315 && (p
[3] == 0 || p
[3] == '/'))
2317 if (found_real_path
)
2319 /* Search backwards for the directory just before the "/.."
2320 and obliterate it and the "/..". */
2322 while (q
!= current_directory
&& q
[-1] != '/')
2325 if (q
== current_directory
)
2326 /* current_directory is
2327 a relative pathname ("can't happen"--leave it alone). */
2331 strcpy (q
- 1, p
+ 3);
2336 /* We are dealing with leading repetitions of "/..", for example
2337 "/../..", which is the Mach super-root. */
2342 found_real_path
= 1;
2347 forget_cached_source_info ();
2350 pwd_command ((char *) 0, 1);
2353 struct source_cleanup_lines_args
{
2356 char *old_pre_error
;
2357 char *old_error_pre_print
;
2361 source_cleanup_lines (args
)
2364 struct source_cleanup_lines_args
*p
=
2365 (struct source_cleanup_lines_args
*)args
;
2366 source_line_number
= p
->old_line
;
2367 source_file_name
= p
->old_file
;
2368 source_pre_error
= p
->old_pre_error
;
2369 error_pre_print
= p
->old_error_pre_print
;
2374 source_command (args
, from_tty
)
2379 struct cleanup
*old_cleanups
;
2381 struct source_cleanup_lines_args old_lines
;
2386 error ("source command requires pathname of file to source.");
2389 file
= tilde_expand (file
);
2390 old_cleanups
= make_cleanup (free
, file
);
2392 stream
= fopen (file
, FOPEN_RT
);
2394 perror_with_name (file
);
2396 make_cleanup (fclose
, stream
);
2398 old_lines
.old_line
= source_line_number
;
2399 old_lines
.old_file
= source_file_name
;
2400 old_lines
.old_pre_error
= source_pre_error
;
2401 old_lines
.old_error_pre_print
= error_pre_print
;
2402 make_cleanup (source_cleanup_lines
, &old_lines
);
2403 source_line_number
= 0;
2404 source_file_name
= file
;
2405 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
2406 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
2407 make_cleanup (free
, source_pre_error
);
2408 /* This will get set every time we read a line. So it won't stay "" for
2410 error_pre_print
= "";
2412 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
2413 if (source_error_allocated
< needed_length
)
2415 source_error_allocated
*= 2;
2416 if (source_error_allocated
< needed_length
)
2417 source_error_allocated
= needed_length
;
2418 if (source_error
== NULL
)
2419 source_error
= xmalloc (source_error_allocated
);
2421 source_error
= xrealloc (source_error
, source_error_allocated
);
2424 read_command_file (stream
);
2426 do_cleanups (old_cleanups
);
2431 echo_command (text
, from_tty
)
2439 while ((c
= *p
++) != '\0')
2443 /* \ at end of argument is used after spaces
2444 so they won't be lost. */
2448 c
= parse_escape (&p
);
2450 printf_filtered ("%c", c
);
2453 printf_filtered ("%c", c
);
2456 /* Force this output to appear now. */
2458 gdb_flush (gdb_stdout
);
2462 /* Functions to manipulate command line editing control variables. */
2464 /* Number of commands to print in each call to show_commands. */
2465 #define Hist_print 10
2467 show_commands (args
, from_tty
)
2471 /* Index for history commands. Relative to history_base. */
2474 /* Number of the history entry which we are planning to display next.
2475 Relative to history_base. */
2478 /* The first command in the history which doesn't exist (i.e. one more
2479 than the number of the last command). Relative to history_base. */
2482 extern HIST_ENTRY
*history_get
PARAMS ((int));
2483 extern int history_base
;
2485 /* Print out some of the commands from the command history. */
2486 /* First determine the length of the history list. */
2487 hist_len
= history_size
;
2488 for (offset
= 0; offset
< history_size
; offset
++)
2490 if (!history_get (history_base
+ offset
))
2499 if (args
[0] == '+' && args
[1] == '\0')
2500 /* "info editing +" should print from the stored position. */
2503 /* "info editing <exp>" should print around command number <exp>. */
2504 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
2506 /* "show commands" means print the last Hist_print commands. */
2509 num
= hist_len
- Hist_print
;
2515 /* If there are at least Hist_print commands, we want to display the last
2516 Hist_print rather than, say, the last 6. */
2517 if (hist_len
- num
< Hist_print
)
2519 num
= hist_len
- Hist_print
;
2524 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
2526 printf_filtered ("%5d %s\n", history_base
+ offset
,
2527 (history_get (history_base
+ offset
))->line
);
2530 /* The next command we want to display is the next one that we haven't
2534 /* If the user repeats this command with return, it should do what
2535 "show commands +" does. This is unnecessary if arg is null,
2536 because "show commands +" is not useful after "show commands". */
2537 if (from_tty
&& args
)
2544 /* Called by do_setshow_command. */
2547 set_history_size_command (args
, from_tty
, c
)
2550 struct cmd_list_element
*c
;
2552 if (history_size
== INT_MAX
)
2553 unstifle_history ();
2554 else if (history_size
>= 0)
2555 stifle_history (history_size
);
2558 history_size
= INT_MAX
;
2559 error ("History size must be non-negative");
2565 set_history (args
, from_tty
)
2569 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
2570 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
2575 show_history (args
, from_tty
)
2579 cmd_show_list (showhistlist
, from_tty
, "");
2582 int info_verbose
= 0; /* Default verbose msgs off */
2584 /* Called by do_setshow_command. An elaborate joke. */
2587 set_verbose (args
, from_tty
, c
)
2590 struct cmd_list_element
*c
;
2592 char *cmdname
= "verbose";
2593 struct cmd_list_element
*showcmd
;
2595 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
2599 c
->doc
= "Set verbose printing of informational messages.";
2600 showcmd
->doc
= "Show verbose printing of informational messages.";
2604 c
->doc
= "Set verbosity.";
2605 showcmd
->doc
= "Show verbosity.";
2610 float_handler (signo
)
2613 /* This message is based on ANSI C, section 4.7. Note that integer
2614 divide by zero causes this, so "float" is a misnomer. */
2615 signal (SIGFPE
, float_handler
);
2616 error ("Erroneous arithmetic operation.");
2619 /* Return whether we are running a batch file or from terminal. */
2623 return !(instream
== stdin
&& ISATTY (stdin
));
2635 enablebreaklist
= NULL
;
2640 showhistlist
= NULL
;
2641 unsethistlist
= NULL
;
2642 #if MAINTENANCE_CMDS
2643 maintenancelist
= NULL
;
2644 maintenanceinfolist
= NULL
;
2645 maintenanceprintlist
= NULL
;
2647 setprintlist
= NULL
;
2648 showprintlist
= NULL
;
2649 setchecklist
= NULL
;
2650 showchecklist
= NULL
;
2653 /* Init the history buffer. Note that we are called after the init file(s)
2654 * have been read so that the user can change the history file via his
2655 * .gdbinit file (for instance). The GDBHISTFILE environment variable
2656 * overrides all of this.
2664 tmpenv
= getenv ("HISTSIZE");
2666 history_size
= atoi (tmpenv
);
2667 else if (!history_size
)
2670 stifle_history (history_size
);
2672 tmpenv
= getenv ("GDBHISTFILE");
2674 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
2675 else if (!history_filename
) {
2676 /* We include the current directory so that if the user changes
2677 directories the file written will be the same as the one
2679 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
2681 read_history (history_filename
);
2687 struct cmd_list_element
*c
;
2689 #ifdef DEFAULT_PROMPT
2690 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
2692 prompt
= savestring ("(gdb) ", 6);
2695 /* Set the important stuff up for command editing. */
2696 command_editing_p
= 1;
2697 history_expansion_p
= 0;
2698 write_history_p
= 0;
2700 /* Setup important stuff for command line editing. */
2701 rl_completion_entry_function
= (int (*)()) symbol_completion_function
;
2702 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
2703 rl_completer_quote_characters
= gdb_completer_quote_characters
;
2704 rl_readline_name
= "gdb";
2706 /* Define the classes of commands.
2707 They will appear in the help list in the reverse of this order. */
2709 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
2710 "Maintenance commands.\n\
2711 Some gdb commands are provided just for use by gdb maintainers.\n\
2712 These commands are subject to frequent change, and may not be as\n\
2713 well documented as user commands.",
2715 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
2716 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
2717 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
2718 The commands in this class are those defined by the user.\n\
2719 Use the \"define\" command to define a command.", &cmdlist
);
2720 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
2721 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
2722 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
2723 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
2724 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
2725 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
2726 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2727 counting from zero for the innermost (currently executing) frame.\n\n\
2728 At any time gdb identifies one frame as the \"selected\" frame.\n\
2729 Variable lookups are done with respect to the selected frame.\n\
2730 When the program being debugged stops, gdb selects the innermost frame.\n\
2731 The commands below can be used to select other frames by number or address.",
2733 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
2735 add_com ("pwd", class_files
, pwd_command
,
2736 "Print working directory. This is used for your program as well.");
2737 c
= add_cmd ("cd", class_files
, cd_command
,
2738 "Set working directory to DIR for debugger and program being debugged.\n\
2739 The change does not take effect for the program being debugged\n\
2740 until the next time it is started.", &cmdlist
);
2741 c
->completer
= filename_completer
;
2744 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
2749 add_com ("echo", class_support
, echo_command
,
2750 "Print a constant string. Give string as argument.\n\
2751 C escape sequences may be used in the argument.\n\
2752 No newline is added at the end of the argument;\n\
2753 use \"\\n\" if you want a newline to be printed.\n\
2754 Since leading and trailing whitespace are ignored in command arguments,\n\
2755 if you want to print some you must use \"\\\" before leading whitespace\n\
2756 to be printed or after trailing whitespace.");
2757 add_com ("document", class_support
, document_command
,
2758 "Document a user-defined command.\n\
2759 Give command name as argument. Give documentation on following lines.\n\
2760 End with a line of just \"end\".");
2761 add_com ("define", class_support
, define_command
,
2762 "Define a new command name. Command name is argument.\n\
2763 Definition appears on following lines, one command per line.\n\
2764 End with a line of just \"end\".\n\
2765 Use the \"document\" command to give documentation for the new command.\n\
2766 Commands defined in this way do not take arguments.");
2769 c
= add_cmd ("source", class_support
, source_command
,
2770 "Read commands from a file named FILE.\n\
2771 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
2772 when gdb is started.", &cmdlist
);
2774 /* Punt file name, we can't help it easily. */
2775 c
= add_cmd ("source", class_support
, source_command
,
2776 "Read commands from a file named FILE.\n\
2777 Note that the file \".gdbinit\" is read automatically in this way\n\
2778 when gdb is started.", &cmdlist
);
2780 c
->completer
= filename_completer
;
2782 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
2783 add_com ("help", class_support
, help_command
, "Print list of commands.");
2784 add_com_alias ("q", "quit", class_support
, 1);
2785 add_com_alias ("h", "help", class_support
, 1);
2788 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
2791 add_show_from_set (c
, &showlist
);
2792 c
->function
.sfunc
= set_verbose
;
2793 set_verbose (NULL
, 0, c
);
2796 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
2797 "Set editing of command lines as they are typed.\n\
2798 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2799 Without an argument, command line editing is enabled. To edit, use\n\
2800 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
2803 add_prefix_cmd ("history", class_support
, set_history
,
2804 "Generic command for setting command history parameters.",
2805 &sethistlist
, "set history ", 0, &setlist
);
2806 add_prefix_cmd ("history", class_support
, show_history
,
2807 "Generic command for showing command history parameters.",
2808 &showhistlist
, "show history ", 0, &showlist
);
2811 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
2812 "Set history expansion on command input.\n\
2813 Without an argument, history expansion is enabled.", &sethistlist
),
2817 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
2818 "Set saving of the history record on exit.\n\
2819 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2820 Without an argument, saving is enabled.", &sethistlist
),
2823 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
2824 "Set the size of the command history, \n\
2825 ie. the number of previous commands to keep a record of.", &sethistlist
);
2826 add_show_from_set (c
, &showhistlist
);
2827 c
->function
.sfunc
= set_history_size_command
;
2830 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
2831 "Set the filename in which to record the command history\n\
2832 (the list of previous commands of which a record is kept).", &sethistlist
),
2836 (add_set_cmd ("confirm", class_support
, var_boolean
,
2838 "Set whether to confirm potentially dangerous operations.",
2842 add_prefix_cmd ("info", class_info
, info_command
,
2843 "Generic command for showing things about the program being debugged.",
2844 &infolist
, "info ", 0, &cmdlist
);
2845 add_com_alias ("i", "info", class_info
, 1);
2847 add_prefix_cmd ("show", class_info
, show_command
,
2848 "Generic command for showing things about the debugger.",
2849 &showlist
, "show ", 0, &cmdlist
);
2850 /* Another way to get at the same thing. */
2851 add_info ("set", show_command
, "Show all GDB settings.");
2853 add_cmd ("commands", no_class
, show_commands
,
2854 "Show the the history of commands you typed.\n\
2855 You can supply a command number to start with, or a `+' to start after\n\
2856 the previous command number shown.",
2859 add_cmd ("version", no_class
, show_version
,
2860 "Show what version of GDB this is.", &showlist
);
2862 /* If target is open when baud changes, it doesn't take effect until the
2863 next open (I think, not sure). */
2864 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
2865 var_zinteger
, (char *)&baud_rate
,
2866 "Set baud rate for remote serial I/O.\n\
2867 This value is used to set the speed of the serial port when debugging\n\
2868 using remote targets.", &setlist
),
2872 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
2873 "Set debugging of remote protocol.\n\
2874 When enabled, each packet sent or received with the remote target\n\
2875 is displayed.", &setlist
),