1 /* Top level for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include <sys/types.h>
33 #include <sys/param.h>
36 #ifdef SET_STACK_LIMIT_HUGE
38 #include <sys/resource.h>
41 int original_stack_limit
;
44 /* If this definition isn't overridden by the header files, assume
45 that isatty and fileno exist on this system. */
47 #define ISATTY(FP) (isatty (fileno (FP)))
52 /* Version number of GDB, as a string. */
57 * Declare all cmd_list_element's
60 /* Chain containing all defined commands. */
62 struct cmd_list_element
*cmdlist
;
64 /* Chain containing all defined info subcommands. */
66 struct cmd_list_element
*infolist
;
68 /* Chain containing all defined enable subcommands. */
70 struct cmd_list_element
*enablelist
;
72 /* Chain containing all defined disable subcommands. */
74 struct cmd_list_element
*disablelist
;
76 /* Chain containing all defined delete subcommands. */
78 struct cmd_list_element
*deletelist
;
80 /* Chain containing all defined "enable breakpoint" subcommands. */
82 struct cmd_list_element
*enablebreaklist
;
84 /* Chain containing all defined set subcommands */
86 struct cmd_list_element
*setlist
;
88 /* Chain containing all defined \"set history\". */
90 struct cmd_list_element
*sethistlist
;
92 /* Chain containing all defined \"unset history\". */
94 struct cmd_list_element
*unsethistlist
;
96 /* stdio stream that command input is being read from. */
100 /* Current working directory. */
102 char *current_directory
;
104 /* The directory name is actually stored here (usually). */
105 static char dirbuf
[MAXPATHLEN
];
107 /* The number of lines on a page, and the number of spaces
109 int linesize
, pagesize
;
111 /* Nonzero if we should refrain from using an X window. */
113 int inhibit_windows
= 0;
115 /* Function to call before reading a command, if nonzero.
116 The function receives two args: an input stream,
117 and a prompt string. */
119 void (*window_hook
) ();
121 extern int frame_file_full_name
;
124 void free_command_lines ();
125 char *gdb_readline ();
126 char *command_line_input ();
127 static void initialize_main ();
128 static void initialize_cmd_lists ();
129 void command_loop ();
130 static void source_command ();
131 static void print_gdb_version ();
132 static void float_handler ();
133 static void cd_command ();
137 /* gdb prints this when reading a command interactively */
140 /* Buffer used for reading command lines, and the size
141 allocated for it so far. */
147 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
151 #define STOP_SIGNAL SIGTSTP
155 /* This is how `error' returns to command level. */
157 jmp_buf to_top_level
;
160 return_to_top_level ()
164 clear_breakpoint_commands ();
165 clear_momentary_breakpoints ();
166 disable_current_display ();
168 longjmp (to_top_level
, 1);
171 /* Call FUNC with arg ARG, catching any errors.
172 If there is no error, return the value returned by FUNC.
173 If there is an error, return zero after printing ERRSTRING
174 (which is in addition to the specific error message already printed). */
177 catch_errors (func
, arg
, errstring
)
184 struct cleanup
*saved_cleanup_chain
;
186 saved_cleanup_chain
= save_cleanups ();
188 bcopy (to_top_level
, saved
, sizeof (jmp_buf));
190 if (setjmp (to_top_level
) == 0)
194 fprintf (stderr
, "%s\n", errstring
);
198 restore_cleanups (saved_cleanup_chain
);
200 bcopy (saved
, to_top_level
, sizeof (jmp_buf));
204 /* Handler for SIGHUP. */
209 kill_inferior_fast ();
210 signal (SIGHUP
, SIG_DFL
);
211 kill (getpid (), SIGHUP
);
214 /* Clean up on error during a "source" command (or execution of a
215 user-defined command).
216 Close the file opened by the command
217 and restore the previous input stream. */
220 source_cleanup (stream
)
223 /* Instream may be 0; set to it when executing user-defined command. */
231 main (argc
, argv
, envp
)
237 int inhibit_gdbinit
= 0;
242 #if defined (ALIGN_STACK_ON_STARTUP)
243 i
= (int) &count
& 0x3;
250 line
= (char *) xmalloc (linesize
);
254 current_directory
= dirbuf
;
256 #ifdef SET_STACK_LIMIT_HUGE
260 /* Set the stack limit huge so that alloca (particularly stringtab
261 * in dbxread.c) does not fail. */
262 getrlimit (RLIMIT_STACK
, &rlim
);
263 original_stack_limit
= rlim
.rlim_cur
;
264 rlim
.rlim_cur
= rlim
.rlim_max
;
265 setrlimit (RLIMIT_STACK
, &rlim
);
267 #endif /* SET_STACK_LIMIT_HUGE */
269 /* Look for flag arguments. */
271 for (i
= 1; i
< argc
; i
++)
273 if (!strcmp (argv
[i
], "-q") || !strcmp (argv
[i
], "-quiet"))
275 else if (!strcmp (argv
[i
], "-nx"))
277 else if (!strcmp (argv
[i
], "-nw"))
279 else if (!strcmp (argv
[i
], "-batch"))
280 batch
= 1, quiet
= 1;
281 else if (!strcmp (argv
[i
], "-fullname"))
282 frame_file_full_name
= 1;
283 else if (!strcmp (argv
[i
], "-xgdb_verbose"))
285 /* -help: print a summary of command line switches. */
286 else if (!strcmp (argv
[i
], "-help"))
289 This is GDB, the GNU debugger. Use the command\n\
290 gdb [options] [executable [core-file]]\n\
291 to enter the debugger.\n\
293 Options available are:\n\
294 -help Print this message.\n\
295 -quiet Do not print version number on startup.\n\
296 -fullname Output information used by emacs-GDB interface.\n\
297 -batch Exit after processing options.\n\
298 -nx Do not read .gdbinit file.\n\
299 -tty TTY Use TTY for input/output by the program being debugged.\n\
300 -cd DIR Change current directory to DIR.\n\
301 -directory DIR Search for source files in DIR.\n\
302 -command FILE Execute GDB commands from FILE.\n\
303 -symbols SYMFILE Read symbols from SYMFILE.\n\
304 -exec EXECFILE Use EXECFILE as the executable.\n\
305 -se FILE Use FILE as symbol file and executable file.\n\
306 -core COREFILE Analyze the core dump COREFILE.\n\
308 For more information, type \"help\" from within GDB, or consult the\n\
309 GDB manual (available as on-line info or a printed manual).\n", stderr
);
310 /* Exiting after printing this message seems like
311 the most useful thing to do. */
314 else if (argv
[i
][0] == '-')
315 /* Other options take arguments, so don't confuse an
316 argument with an option. */
320 /* Run the init function of each source file */
322 initialize_cmd_lists (); /* This needs to be done first */
323 initialize_all_files ();
324 initialize_main (); /* But that omits this file! Do it now */
325 initialize_signals ();
328 print_gdb_version ();
330 /* Process the command line arguments. */
333 for (i
= 1; i
< argc
; i
++)
335 register char *arg
= argv
[i
];
336 /* Args starting with - say what to do with the following arg
340 extern void exec_file_command (), symbol_file_command ();
341 extern void core_file_command (), directory_command ();
342 extern void tty_command ();
344 if (!strcmp (arg
, "-q") || !strcmp (arg
, "-nx")
345 || !strcmp (arg
, "-quiet") || !strcmp (arg
, "-batch")
346 || !strcmp (arg
, "-fullname") || !strcmp (arg
, "-nw")
347 || !strcmp (arg
, "-xgdb_verbose")
348 || !strcmp (arg
, "-help"))
349 /* Already processed above */
353 fprintf (stderr
, "No argument follows \"%s\".\n", arg
);
354 if (!setjmp (to_top_level
))
356 /* -s foo: get syms from foo. -e foo: execute foo.
357 -se foo: do both with foo. -c foo: use foo as core dump. */
358 if (!strcmp (arg
, "-se"))
360 exec_file_command (argv
[i
], !batch
);
361 symbol_file_command (argv
[i
], !batch
);
363 else if (!strcmp (arg
, "-s") || !strcmp (arg
, "-symbols"))
364 symbol_file_command (argv
[i
], !batch
);
365 else if (!strcmp (arg
, "-e") || !strcmp (arg
, "-exec"))
366 exec_file_command (argv
[i
], !batch
);
367 else if (!strcmp (arg
, "-c") || !strcmp (arg
, "-core"))
368 core_file_command (argv
[i
], !batch
);
369 /* -x foo: execute commands from foo. */
370 else if (!strcmp (arg
, "-x") || !strcmp (arg
, "-command")
371 || !strcmp (arg
, "-commands"))
372 source_command (argv
[i
]);
373 /* -d foo: add directory `foo' to source-file directory
375 else if (!strcmp (arg
, "-d") || !strcmp (arg
, "-dir")
376 || !strcmp (arg
, "-directory"))
377 directory_command (argv
[i
], 0);
378 /* -cd FOO: specify current directory as FOO.
379 GDB remembers the precise string FOO as the dirname. */
380 else if (!strcmp (arg
, "-cd"))
382 cd_command (argv
[i
], 0);
385 /* -t /def/ttyp1: use /dev/ttyp1 for inferior I/O. */
386 else if (!strcmp (arg
, "-t") || !strcmp (arg
, "-tty"))
387 tty_command (argv
[i
], 0);
390 error ("Unknown command-line switch: \"%s\"\n", arg
);
395 /* Args not thus accounted for
396 are treated as, first, the symbol/executable file
397 and, second, the core dump file. */
399 if (!setjmp (to_top_level
))
403 exec_file_command (arg
, !batch
);
404 symbol_file_command (arg
, !batch
);
408 core_file_command (arg
, !batch
);
412 fprintf (stderr
, "Excess command line args ignored. (%s%s)\n",
413 arg
, (i
== argc
- 1) ? "" : " ...");
419 struct stat homebuf
, cwdbuf
;
420 char *homedir
, *homeinit
;
422 /* Read init file, if it exists in home directory */
423 homedir
= getenv ("HOME");
426 homeinit
= (char *) alloca (strlen (getenv ("HOME")) + 10);
427 strcpy (homeinit
, getenv ("HOME"));
428 strcat (homeinit
, "/.gdbinit");
429 if (!inhibit_gdbinit
&& access (homeinit
, R_OK
) == 0)
430 if (!setjmp (to_top_level
))
431 source_command (homeinit
);
433 /* Do stats; no need to do them elsewhere since we'll only
434 need them if homedir is set. Make sure that they are
435 zero in case one of them fails (guarantees that they
436 won't match if either exits). */
438 bzero (&homebuf
, sizeof (struct stat
));
439 bzero (&cwdbuf
, sizeof (struct stat
));
441 stat (homeinit
, &homebuf
);
442 stat ("./.gdbinit", &cwdbuf
); /* We'll only need this if
446 /* Read the input file in the current directory, *if* it isn't
447 the same file (it should exist, also). */
450 || bcmp ((char *) &homebuf
,
452 sizeof (struct stat
)))
453 if (!inhibit_gdbinit
&& access (".gdbinit", R_OK
) == 0)
454 if (!setjmp (to_top_level
))
455 source_command (".gdbinit");
461 fatal ("Attempt to read commands from stdin in batch mode.");
463 /* We have hit the end of the batch file. */
468 printf ("Type \"help\" for a list of commands.\n");
470 /* The command loop. */
474 if (!setjmp (to_top_level
))
476 clearerr (stdin
); /* Don't get hung if C-d is typed. */
480 /* Execute the line P as a command.
481 Pass FROM_TTY as second argument to the defining function. */
484 execute_command (p
, from_tty
)
488 register struct cmd_list_element
*c
;
489 register struct command_line
*cmdlines
;
492 while (*p
== ' ' || *p
== '\t') p
++;
495 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
496 if (c
->function
== 0)
497 error ("That is not a command, just a help topic.");
498 else if (c
->class == (int) class_user
)
500 struct cleanup
*old_chain
;
503 error ("User-defined commands cannot take arguments.");
504 cmdlines
= (struct command_line
*) c
->function
;
505 if (cmdlines
== (struct command_line
*) 0)
509 /* Set the instream to 0, indicating execution of a
510 user-defined function. */
511 old_chain
= make_cleanup (source_cleanup
, instream
);
512 instream
= (FILE *) 0;
515 execute_command (cmdlines
->line
, 0);
516 cmdlines
= cmdlines
->next
;
518 do_cleanups (old_chain
);
521 /* Pass null arg rather than an empty one. */
522 (*c
->function
) (*p
? p
: 0, from_tty
);
531 /* Read commands from `instream' and execute them
532 until end of file. */
536 struct cleanup
*old_chain
;
537 while (!feof (instream
))
539 if (window_hook
&& instream
== stdin
)
540 (*window_hook
) (instream
, prompt
);
543 if (instream
== stdin
&& ISATTY (stdin
))
544 reinitialize_more_filter ();
545 old_chain
= make_cleanup (do_nothing
, 0);
546 execute_command (command_line_input (instream
== stdin
? prompt
: 0,
549 /* Do any commands attached to breakpoint we stopped at. */
550 do_breakpoint_commands ();
551 do_cleanups (old_chain
);
555 /* Commands call this if they do not want to be repeated by null lines. */
560 /* If we aren't reading from standard input, we are saving the last
561 thing read from stdin in line and don't want to delete it. Null lines
562 won't repeat here in any case. */
563 if (instream
== stdin
)
567 /* Read a line from the stream "instream" without command line editing.
569 It prints PROMPT once at the start.
571 If RETURN_RESULT is set it allocates
572 space for whatever the user types and returns the result.
573 If not, it just discards what the user types. */
575 gdb_readline (prompt
, return_result
)
582 int result_size
= 80;
591 result
= (char *) xmalloc (result_size
);
595 c
= fgetc (instream
);
596 if (c
== -1 || c
== '\n')
600 result
[input_index
++] = c
;
601 while (input_index
>= result_size
)
604 result
= (char *) xrealloc (result
, result_size
);
610 result
[input_index
++] = '\0';
617 /* Declaration for fancy readline with command line editing. */
620 /* Variables which control command line editing and history
621 substitution. These variables are given default values at the end
623 static int command_editing_p
;
624 static int history_expansion_p
;
625 static int write_history_p
;
626 static int history_size
;
627 static char *history_filename
;
629 /* Variables which are necessary for fancy command line editing. */
630 char *gdb_completer_word_break_characters
=
631 " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
633 /* Functions that are used as part of the fancy command line editing. */
635 /* Generate symbol names one by one for the completer. If STATE is
636 zero, then we need to initialize, otherwise the initialization has
637 already taken place. TEXT is what we expect the symbol to start
638 with. RL_LINE_BUFFER is available to be looked at; it contains the
639 entire text of the line. RL_POINT is the offset in that line of
640 the cursor. You should pretend that the line ends at RL_POINT. */
642 symbol_completion_function (text
, state
)
646 char **make_symbol_completion_list ();
647 static char **list
= (char **)NULL
;
650 extern char *rl_line_buffer
;
652 char *tmp_command
, *p
;
653 struct cmd_list_element
*c
, *result_list
;
657 /* Free the storage used by LIST, but not by the strings inside. This is
658 because rl_complete_internal () frees the strings. */
664 /* Decide whether to complete on a list of gdb commands or on
666 tmp_command
= (char *) alloca (rl_point
+ 1);
669 strncpy (tmp_command
, rl_line_buffer
, rl_point
);
670 tmp_command
[rl_point
] = '\0';
674 /* An empty line we want to consider ambiguous; that is,
675 it could be any command. */
676 c
= (struct cmd_list_element
*) -1;
680 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
682 /* Move p up to the next interesting thing. */
683 while (*p
== ' ' || *p
== '\t')
687 /* He's typed something unrecognizable. Sigh. */
689 else if (c
== (struct cmd_list_element
*) -1)
691 if (p
+ strlen(text
) != tmp_command
+ rl_point
)
692 error ("Unrecognized command.");
694 /* He's typed something ambiguous. This is easier. */
696 list
= complete_on_cmdlist (*result_list
->prefixlist
, text
);
698 list
= complete_on_cmdlist (cmdlist
, text
);
702 /* If we've gotten this far, gdb has recognized a full
703 command. There are several possibilities:
705 1) We need to complete on the command.
706 2) We need to complete on the possibilities coming after
708 2) We need to complete the text of what comes after the
712 /* Always (might be longer versions of thie command). */
713 list
= complete_on_cmdlist (result_list
, text
);
714 else if (!*p
&& !*text
)
717 list
= complete_on_cmdlist (*c
->prefixlist
, "");
719 list
= make_symbol_completion_list ("");
723 if (c
->prefixlist
&& !c
->allow_unknown
)
726 error ("\"%s\" command requires a subcommand.",
730 list
= make_symbol_completion_list (text
);
735 /* If the debugged program wasn't compiled with symbols, or if we're
736 clearly completing on a command and no command matches, return
739 return ((char *)NULL
);
741 output
= list
[index
];
752 #if STOP_SIGNAL == SIGTSTP
753 signal (SIGTSTP
, SIG_DFL
);
755 kill (getpid (), SIGTSTP
);
756 signal (SIGTSTP
, stop_sig
);
758 signal (STOP_SIGNAL
, stop_sig
);
760 printf ("%s", prompt
);
763 /* Forget about any previous command -- null line now will do nothing. */
766 #endif /* STOP_SIGNAL */
769 Writing the history file upon a terminating signal is
not useful
,
770 because the info is rarely relevant
and is in the core dump anyway
.
771 It is an annoyance to have the file cluttering up the place
.
772 /* The list of signals that would terminate us if not caught.
773 We catch them, but just so that we can write the history file,
775 int terminating_signals
[] = {
776 SIGHUP
, SIGINT
, SIGILL
, SIGTRAP
, SIGIOT
,
777 SIGEMT
, SIGFPE
, SIGKILL
, SIGBUS
, SIGSEGV
, SIGSYS
,
778 SIGPIPE
, SIGALRM
, SIGTERM
,
799 #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (int))
802 catch_termination (sig
)
805 /* We are probably here because GDB has a bug. Write out the history
806 so that we might have a better chance of reproducing it. */
807 /* Tell the user what we are doing so he can delete the file if
809 write_history (history_filename
);
810 printf ("\n%s written.\n", history_filename
);
811 signal (sig
, SIG_DFL
);
812 kill (getpid (), sig
);
816 /* Initialize signal handlers. */
817 initialize_signals ()
819 extern void request_quit ();
823 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
824 signal (terminating_signals
[i
], catch_termination
);
827 signal (SIGINT
, request_quit
);
829 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
830 passed to the inferior, which we don't want. It would be
831 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
832 on BSD4.3 systems using vfork, that will (apparently) affect the
833 GDB process as well as the inferior (the signal handling tables
834 being shared between the two, apparently). Since we establish
835 a handler for SIGQUIT, when we call exec it will set the signal
836 to SIG_DFL for us. */
837 signal (SIGQUIT
, do_nothing
);
838 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
839 signal (SIGHUP
, disconnect
);
840 signal (SIGFPE
, float_handler
);
843 /* Read one line from the command input stream `instream'
844 into the local static buffer `linebuffer' (whose current length
846 The buffer is made bigger as necessary.
847 Returns the address of the start of the line.
849 *If* the instream == stdin & stdin is a terminal, the line read
850 is copied into the file line saver (global var char *line,
851 length linesize) so that it can be duplicated.
853 This routine either uses fancy command line editing or
854 simple input as the user has requested. */
857 command_line_input (prompt
, repeat
)
861 static char *linebuffer
= 0;
862 static int linelength
= 0;
864 register char *p1
, *rl
;
865 char *local_prompt
= prompt
;
872 linebuffer
= (char *) xmalloc (linelength
);
877 /* Control-C quits instantly if typed while in this loop
878 since it should not wait until the user types a newline. */
881 signal (STOP_SIGNAL
, stop_sig
);
886 /* Don't use fancy stuff if not talking to stdin. */
887 if (command_editing_p
&& instream
== stdin
888 && ISATTY (instream
))
889 rl
= readline (local_prompt
);
891 rl
= gdb_readline (local_prompt
, 1);
893 if (!rl
|| rl
== (char *) EOF
) break;
894 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
896 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
897 nline
= (char *) xrealloc (linebuffer
, linelength
);
898 p
+= nline
- linebuffer
;
902 /* Copy line. Don't copy null at end. (Leaves line alone
903 if this was just a newline) */
907 free (rl
); /* Allocated in readline. */
909 if (p
== linebuffer
|| *(p
- 1) != '\\')
912 p
--; /* Put on top of '\'. */
913 local_prompt
= (char *) 0;
917 signal (SIGTSTP
, SIG_DFL
);
921 /* Do history expansion if that is wished. */
922 if (history_expansion_p
&& instream
== stdin
923 && ISATTY (instream
))
928 *p
= '\0'; /* Insert null now. */
929 expanded
= history_expand (linebuffer
, &history_value
);
932 /* Print the changes. */
933 printf ("%s\n", history_value
);
935 /* If there was an error, call this function again. */
938 free (history_value
);
939 return command_line_input (prompt
, repeat
);
941 if (strlen (history_value
) > linelength
)
943 linelength
= strlen (history_value
) + 1;
944 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
946 strcpy (linebuffer
, history_value
);
947 p
= linebuffer
+ strlen(linebuffer
);
948 free (history_value
);
952 /* If we just got an empty line, and that is supposed
953 to repeat the previous command, return the value in the
960 while (*p1
== ' ' || *p1
== '\t')
968 /* Add line to history if appropriate. */
969 if (instream
== stdin
970 && ISATTY (stdin
) && *linebuffer
)
971 add_history (linebuffer
);
973 /* If line is a comment, clear it out. */
974 /* Note: comments are added to the command history.
975 This is useful when you type a command, and then realize
976 you don't want to execute it quite yet. You can comment out the
977 command and then later fetch it from the value history and
980 while ((c
= *p1
) == ' ' || c
== '\t') p1
++;
984 /* Save into global buffer if appropriate. */
987 if (linelength
> linesize
)
989 line
= xrealloc (line
, linelength
);
990 linesize
= linelength
;
992 strcpy (line
, linebuffer
);
999 /* Read lines from the input stream
1000 and accumulate them in a chain of struct command_line's
1001 which is then returned. */
1003 struct command_line
*
1004 read_command_lines ()
1006 struct command_line
*first
= 0;
1007 register struct command_line
*next
, *tail
= 0;
1008 register char *p
, *p1
;
1009 struct cleanup
*old_chain
= 0;
1014 p
= command_line_input (0, instream
== stdin
);
1015 /* Remove leading and trailing blanks. */
1016 while (*p
== ' ' || *p
== '\t') p
++;
1017 p1
= p
+ strlen (p
);
1018 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t')) p1
--;
1020 /* Is this "end"? */
1021 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
1024 /* No => add this line to the chain of command lines. */
1025 next
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1026 next
->line
= savestring (p
, p1
- p
);
1034 /* We just read the first line.
1035 From now on, arrange to throw away the lines we have
1036 if we quit or get an error while inside this function. */
1038 old_chain
= make_cleanup (free_command_lines
, &first
);
1045 /* Now we are about to return the chain to our caller,
1046 so freeing it becomes his responsibility. */
1048 discard_cleanups (old_chain
);
1052 /* Free a chain of struct command_line's. */
1055 free_command_lines (lptr
)
1056 struct command_line
**lptr
;
1058 register struct command_line
*l
= *lptr
;
1059 register struct command_line
*next
;
1070 /* Add an element to the list of info subcommands. */
1073 add_info (name
, fun
, doc
)
1078 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
1081 /* Add an alias to the list of info subcommands. */
1084 add_info_alias (name
, oldname
, abbrev_flag
)
1089 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
1092 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1093 Therefore, its own definition is called only for "info" with no args. */
1098 printf ("\"info\" must be followed by the name of an info command.\n");
1099 help_list (infolist
, "info ", -1, stdout
);
1102 /* Add an element to the list of commands. */
1105 add_com (name
, class, fun
, doc
)
1111 add_cmd (name
, class, fun
, doc
, &cmdlist
);
1114 /* Add an alias or abbreviation command to the list of commands. */
1117 add_com_alias (name
, oldname
, class, abbrev_flag
)
1123 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
1130 error ("Argument required (%s).", why
);
1134 help_command (command
, from_tty
)
1136 int from_tty
; /* Ignored */
1138 help_cmd (command
, stdout
);
1142 validate_comname (comname
)
1148 error_no_arg ("name of command to define");
1153 if (!(*p
>= 'A' && *p
<= 'Z')
1154 && !(*p
>= 'a' && *p
<= 'z')
1155 && !(*p
>= '0' && *p
<= '9')
1157 error ("Junk in argument list: \"%s\"", p
);
1163 define_command (comname
, from_tty
)
1167 register struct command_line
*cmds
;
1168 register struct cmd_list_element
*c
;
1169 char *tem
= comname
;
1171 validate_comname (comname
);
1173 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
1176 if (c
->class == (int) class_user
|| c
->class == (int) class_alias
)
1177 tem
= "Redefine command \"%s\"? ";
1179 tem
= "Really redefine built-in command \"%s\"? ";
1180 if (!query (tem
, comname
))
1181 error ("Command \"%s\" not redefined.", comname
);
1186 printf ("Type commands for definition of \"%s\".\n\
1187 End with a line saying just \"end\".\n", comname
);
1190 comname
= savestring (comname
, strlen (comname
));
1192 cmds
= read_command_lines ();
1194 if (c
&& c
->class == (int) class_user
)
1195 free_command_lines (&c
->function
);
1197 add_com (comname
, class_user
, cmds
,
1198 (c
&& c
->class == (int) class_user
)
1199 ? c
->doc
: savestring ("User-defined.", 13));
1203 document_command (comname
, from_tty
)
1207 struct command_line
*doclines
;
1208 register struct cmd_list_element
*c
;
1209 char *tem
= comname
;
1211 validate_comname (comname
);
1213 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1215 if (c
->class != (int) class_user
)
1216 error ("Command \"%s\" is built-in.", comname
);
1219 printf ("Type documentation for \"%s\".\n\
1220 End with a line saying just \"end\".\n", comname
);
1222 doclines
= read_command_lines ();
1224 if (c
->doc
) free (c
->doc
);
1227 register struct command_line
*cl1
;
1228 register int len
= 0;
1230 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1231 len
+= strlen (cl1
->line
) + 1;
1233 c
->doc
= (char *) xmalloc (len
+ 1);
1236 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1238 strcat (c
->doc
, cl1
->line
);
1240 strcat (c
->doc
, "\n");
1244 free_command_lines (&doclines
);
1248 print_gdb_version ()
1250 printf ("GDB %s, Copyright (C) 1989 Free Software Foundation, Inc.\n\
1251 There is ABSOLUTELY NO WARRANTY for GDB; type \"info warranty\" for details.\n\
1252 GDB is free software and you are welcome to distribute copies of it\n\
1253 under certain conditions; type \"info copying\" to see the conditions.\n",
1261 print_gdb_version ();
1265 /* xgdb calls this to reprint the usual GDB prompt. */
1270 printf ("%s", prompt
);
1274 /* Command to specify a prompt string instead of "(gdb) ". */
1277 set_prompt_command (text
)
1285 error_no_arg ("string to which to set prompt");
1287 new = (char *) xmalloc (strlen (text
) + 2);
1293 /* \ at end of argument is used after spaces
1294 so they won't be lost. */
1297 c
= parse_escape (&p
);
1299 break; /* C loses */
1306 if (*(p
- 1) != '\\')
1309 new = (char *) xrealloc (new, q
- new);
1317 if (have_inferior_p ())
1319 if (query ("The program is running. Quit anyway? "))
1321 /* Prevent any warning message from reopen_exec_file, in case
1322 we have a core file that's inconsistent with the exec file. */
1323 exec_file_command (0, 0);
1327 error ("Not confirmed.");
1329 /* Save the history information if it is appropriate to do so. */
1330 if (write_history_p
&& history_filename
)
1331 write_history (history_filename
);
1336 input_from_terminal_p ()
1338 return instream
== stdin
;
1342 pwd_command (arg
, from_tty
)
1346 if (arg
) error ("The \"pwd\" command does not take an argument: %s", arg
);
1349 if (strcmp (dirbuf
, current_directory
))
1350 printf ("Working directory %s\n (canonically %s).\n",
1351 current_directory
, dirbuf
);
1353 printf ("Working directory %s.\n", current_directory
);
1357 cd_command (dir
, from_tty
)
1365 error_no_arg ("new working directory");
1367 dir
= tilde_expand (dir
);
1368 make_cleanup (free
, dir
);
1371 dir
= savestring (dir
, len
- (len
> 1 && dir
[len
-1] == '/'));
1373 current_directory
= dir
;
1376 current_directory
= concat (current_directory
, "/", dir
);
1380 /* Now simplify any occurrences of `.' and `..' in the pathname. */
1388 for (p
= current_directory
; *p
;)
1390 if (!strncmp (p
, "/./", 2)
1391 && (p
[2] == 0 || p
[2] == '/'))
1393 else if (!strncmp (p
, "/..", 3)
1394 && (p
[3] == 0 || p
[3] == '/')
1395 && p
!= current_directory
)
1398 while (q
!= current_directory
&& q
[-1] != '/') q
--;
1399 if (q
!= current_directory
)
1409 if (chdir (dir
) < 0)
1410 perror_with_name (dir
);
1413 pwd_command ((char *) 0, 1);
1417 source_command (arg
, from_tty
)
1422 struct cleanup
*cleanups
;
1426 /* Let source without arguments read .gdbinit. */
1429 file
= tilde_expand (file
);
1430 make_cleanup (free
, file
);
1432 stream
= fopen (file
, "r");
1434 perror_with_name (file
);
1436 cleanups
= make_cleanup (source_cleanup
, instream
);
1442 do_cleanups (cleanups
);
1457 /* \ at end of argument is used after spaces
1458 so they won't be lost. */
1462 c
= parse_escape (&p
);
1474 if (query ("Should GDB dump core? "))
1476 signal (SIGQUIT
, SIG_DFL
);
1477 kill (getpid (), SIGQUIT
);
1482 parse_binary_operation (caller
, arg
)
1490 length
= strlen (arg
);
1492 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
1495 if (!strncmp (arg
, "on", length
)
1496 || !strncmp (arg
, "1", length
)
1497 || !strncmp (arg
, "yes", length
))
1500 if (!strncmp (arg
, "off", length
)
1501 || !strncmp (arg
, "0", length
)
1502 || !strncmp (arg
, "no", length
))
1505 error ("\"%s\" not given a binary valued argument.", caller
);
1508 /* Functions to manipulate command line editing control variables. */
1511 set_editing (arg
, from_tty
)
1515 command_editing_p
= parse_binary_operation ("set command-editing", arg
);
1518 /* Number of commands to print in each call to editing_info. */
1519 #define Hist_print 10
1521 editing_info (arg
, from_tty
)
1525 /* Index for history commands. Relative to history_base. */
1528 /* Number of the history entry which we are planning to display next.
1529 Relative to history_base. */
1532 /* The first command in the history which doesn't exist (i.e. one more
1533 than the number of the last command). Relative to history_base. */
1536 struct _hist_entry
{
1540 extern int history_base
;
1542 printf_filtered ("Interactive command editing is %s.\n",
1543 command_editing_p
? "on" : "off");
1545 printf_filtered ("History expansion of command input is %s.\n",
1546 history_expansion_p
? "on" : "off");
1547 printf_filtered ("Writing of a history record upon exit is %s.\n",
1548 write_history_p
? "enabled" : "disabled");
1549 printf_filtered ("The size of the history list (number of stored commands) is %d.\n",
1551 printf_filtered ("The name of the history record is \"%s\".\n\n",
1552 history_filename
? history_filename
: "");
1554 /* Print out some of the commands from the command history. */
1555 /* First determine the length of the history list. */
1556 hist_len
= history_size
;
1557 for (offset
= 0; offset
< history_size
; offset
++)
1559 if (!history_get (history_base
+ offset
))
1568 if (arg
[0] == '+' && arg
[1] == '\0')
1569 /* "info editing +" should print from the stored position. */
1572 /* "info editing <exp>" should print around command number <exp>. */
1573 num
= (parse_and_eval_address (arg
) - history_base
) - Hist_print
/ 2;
1575 /* "info editing" means print the last Hist_print commands. */
1578 num
= hist_len
- Hist_print
;
1584 /* If there are at least Hist_print commands, we want to display the last
1585 Hist_print rather than, say, the last 6. */
1586 if (hist_len
- num
< Hist_print
)
1588 num
= hist_len
- Hist_print
;
1593 if (num
== hist_len
- Hist_print
)
1594 printf_filtered ("The list of the last %d commands is:\n\n", Hist_print
);
1596 printf_filtered ("Some of the stored commands are:\n\n");
1598 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
1600 printf_filtered ("%5d %s\n", history_base
+ offset
,
1601 (history_get (history_base
+ offset
))->line
);
1604 /* The next command we want to display is the next one that we haven't
1608 /* If the user repeats this command with return, it should do what
1609 "info editing +" does. This is unnecessary if arg is null,
1610 because "info editing +" is not useful after "info editing". */
1611 if (from_tty
&& arg
)
1619 set_history_expansion (arg
, from_tty
)
1623 history_expansion_p
= parse_binary_operation ("set history expansion", arg
);
1627 set_history_write (arg
, from_tty
)
1631 write_history_p
= parse_binary_operation ("set history write", arg
);
1635 set_history (arg
, from_tty
)
1639 printf ("\"set history\" must be followed by the name of a history subcommand.\n");
1640 help_list (sethistlist
, "set history ", -1, stdout
);
1644 set_history_size (arg
, from_tty
)
1649 error_no_arg ("set history size");
1651 history_size
= atoi (arg
);
1655 set_history_filename (arg
, from_tty
)
1662 error_no_arg ("history file name");
1664 arg
= tilde_expand (arg
);
1665 make_cleanup (free
, arg
);
1667 i
= strlen (arg
) - 1;
1669 free (history_filename
);
1671 while (i
> 0 && (arg
[i
] == ' ' || arg
[i
] == '\t'))
1675 history_filename
= (char *) 0;
1677 history_filename
= savestring (arg
, i
+ 1);
1678 history_filename
[i
] = '\0';
1684 set_verbose_command (arg
, from_tty
)
1688 info_verbose
= parse_binary_operation ("set verbose", arg
);
1692 verbose_info (arg
, from_tty
)
1697 error ("\"info verbose\" does not take any arguments.\n");
1699 printf ("Verbose printing of information is %s.\n",
1700 info_verbose
? "on" : "off");
1706 error ("Invalid floating value encountered or computed.");
1711 initialize_cmd_lists ()
1713 cmdlist
= (struct cmd_list_element
*) 0;
1714 infolist
= (struct cmd_list_element
*) 0;
1715 enablelist
= (struct cmd_list_element
*) 0;
1716 disablelist
= (struct cmd_list_element
*) 0;
1717 deletelist
= (struct cmd_list_element
*) 0;
1718 enablebreaklist
= (struct cmd_list_element
*) 0;
1719 setlist
= (struct cmd_list_element
*) 0;
1720 sethistlist
= (struct cmd_list_element
*) 0;
1721 unsethistlist
= (struct cmd_list_element
*) 0;
1728 /* Command line editing externals. */
1729 extern int (*rl_completion_entry_function
)();
1730 extern char *rl_completer_word_break_characters
;
1732 /* Set default verbose mode on. */
1735 prompt
= savestring ("(gdb) ", 6);
1737 /* Set the important stuff up for command editing. */
1738 command_editing_p
= 1;
1739 history_expansion_p
= 0;
1740 write_history_p
= 0;
1742 if (tmpenv
= getenv ("HISTSIZE"))
1743 history_size
= atoi (tmpenv
);
1747 stifle_history (history_size
);
1749 if (tmpenv
= getenv ("GDBHISTFILE"))
1750 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
1752 /* We include the current directory so that if the user changes
1753 directories the file written will be the same as the one
1755 history_filename
= concat (current_directory
, "/.gdb_history", "");
1757 read_history (history_filename
);
1759 /* Setup important stuff for command line editing. */
1760 rl_completion_entry_function
= (int (*)()) symbol_completion_function
;
1761 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
1763 /* Define the classes of commands.
1764 They will appear in the help list in the reverse of this order. */
1766 add_cmd ("obscure", class_obscure
, 0, "Obscure features.", &cmdlist
);
1767 add_cmd ("alias", class_alias
, 0, "Aliases of other commands.", &cmdlist
);
1768 add_cmd ("user", class_user
, 0, "User-defined commands.\n\
1769 The commands in this class are those defined by the user.\n\
1770 Use the \"define\" command to define a command.", &cmdlist
);
1771 add_cmd ("support", class_support
, 0, "Support facilities.", &cmdlist
);
1772 add_cmd ("status", class_info
, 0, "Status inquiries.", &cmdlist
);
1773 add_cmd ("files", class_files
, 0, "Specifying and examining files.", &cmdlist
);
1774 add_cmd ("breakpoints", class_breakpoint
, 0, "Making program stop at certain points.", &cmdlist
);
1775 add_cmd ("data", class_vars
, 0, "Examining data.", &cmdlist
);
1776 add_cmd ("stack", class_stack
, 0, "Examining the stack.\n\
1777 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
1778 counting from zero for the innermost (currently executing) frame.\n\n\
1779 At any time gdb identifies one frame as the \"selected\" frame.\n\
1780 Variable lookups are done with respect to the selected frame.\n\
1781 When the program being debugged stops, gdb selects the innermost frame.\n\
1782 The commands below can be used to select other frames by number or address.",
1784 add_cmd ("running", class_run
, 0, "Running the program.", &cmdlist
);
1786 add_com ("pwd", class_files
, pwd_command
,
1787 "Print working directory. This is used for your program as well.");
1788 add_com ("cd", class_files
, cd_command
,
1789 "Set working directory to DIR for debugger and program being debugged.\n\
1790 The change does not take effect for the program being debugged\n\
1791 until the next time it is started.");
1793 add_cmd ("prompt", class_support
, set_prompt_command
,
1794 "Change gdb's prompt from the default of \"(gdb)\"",
1796 add_com ("echo", class_support
, echo_command
,
1797 "Print a constant string. Give string as argument.\n\
1798 C escape sequences may be used in the argument.\n\
1799 No newline is added at the end of the argument;\n\
1800 use \"\\n\" if you want a newline to be printed.\n\
1801 Since leading and trailing whitespace are ignored in command arguments,\n\
1802 if you want to print some you must use \"\\\" before leading whitespace\n\
1803 to be printed or after trailing whitespace.");
1804 add_com ("document", class_support
, document_command
,
1805 "Document a user-defined command.\n\
1806 Give command name as argument. Give documentation on following lines.\n\
1807 End with a line of just \"end\".");
1808 add_com ("define", class_support
, define_command
,
1809 "Define a new command name. Command name is argument.\n\
1810 Definition appears on following lines, one command per line.\n\
1811 End with a line of just \"end\".\n\
1812 Use the \"document\" command to give documentation for the new command.\n\
1813 Commands defined in this way do not take arguments.");
1815 add_com ("source", class_support
, source_command
,
1816 "Read commands from a file named FILE.\n\
1817 Note that the file \".gdbinit\" is read automatically in this way\n\
1818 when gdb is started.");
1819 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
1820 add_com ("help", class_support
, help_command
, "Print list of commands.");
1821 add_com_alias ("q", "quit", class_support
, 1);
1822 add_com_alias ("h", "help", class_support
, 1);
1824 add_cmd ("verbose", class_support
, set_verbose_command
,
1825 "Change the number of informational messages gdb prints.",
1827 add_info ("verbose", verbose_info
,
1828 "Status of gdb's verbose printing option.\n");
1830 add_com ("dump-me", class_obscure
, dump_me_command
,
1831 "Get fatal error; make debugger dump its core.");
1833 add_cmd ("editing", class_support
, set_editing
,
1834 "Enable or disable command line editing.\n\
1835 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
1836 Without an argument, command line editing is enabled.", &setlist
);
1838 add_prefix_cmd ("history", class_support
, set_history
,
1839 "Generic command for setting command history parameters.",
1840 &sethistlist
, "set history ", 0, &setlist
);
1842 add_cmd ("expansion", no_class
, set_history_expansion
,
1843 "Enable or disable history expansion on command input.\n\
1844 Without an argument, history expansion is enabled.", &sethistlist
);
1846 add_cmd ("write", no_class
, set_history_write
,
1847 "Enable or disable saving of the history record on exit.\n\
1848 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
1849 Without an argument, saving is enabled.", &sethistlist
);
1851 add_cmd ("size", no_class
, set_history_size
,
1852 "Set the size of the command history, \n\
1853 ie. the number of previous commands to keep a record of.", &sethistlist
);
1855 add_cmd ("filename", no_class
, set_history_filename
,
1856 "Set the filename in which to record the command history\n\
1857 (the list of previous commands of which a record is kept).", &sethistlist
);
1859 add_prefix_cmd ("info", class_info
, info_command
,
1860 "Generic command for printing status.",
1861 &infolist
, "info ", 0, &cmdlist
);
1862 add_com_alias ("i", "info", class_info
, 1);
1864 add_info ("editing", editing_info
, "Status of command editor.");
1866 add_info ("version", version_info
, "Report what version of GDB this is.");