179de5f3968ae90d701550ad9ad0f9d93f9293ca
[deliverable/binutils-gdb.git] / gdb / main.c
1 /* Top level `main' program for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program 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 2 of the License, or
9 (at your option) any later version.
10
11 This program 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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 int fclose ();
22 #include "defs.h"
23 #include "gdbcmd.h"
24 #include "symtab.h"
25 #include "inferior.h"
26 #include "signals.h"
27 #include "target.h"
28 #include "breakpoint.h"
29 #include "language.h"
30
31 #include "getopt.h"
32
33 /* readline include files */
34 #include "readline.h"
35 #include "history.h"
36
37 /* readline defines this. */
38 #undef savestring
39
40 #ifdef USG
41 #include <sys/types.h>
42 #include <unistd.h>
43 #endif
44
45 #include <string.h>
46 #ifndef NO_SYS_FILE
47 #include <sys/file.h>
48 #endif
49 #include <setjmp.h>
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <ctype.h>
53
54 #ifdef SET_STACK_LIMIT_HUGE
55 #include <sys/time.h>
56 #include <sys/resource.h>
57
58 int original_stack_limit;
59 #endif
60
61
62 /* If this definition isn't overridden by the header files, assume
63 that isatty and fileno exist on this system. */
64 #ifndef ISATTY
65 #define ISATTY(FP) (isatty (fileno (FP)))
66 #endif
67
68 /* Initialization file name for gdb. This is overridden in some configs. */
69
70 #ifndef GDBINIT_FILENAME
71 #define GDBINIT_FILENAME ".gdbinit"
72 #endif
73 char gdbinit[] = GDBINIT_FILENAME;
74
75 #define ALL_CLEANUPS ((struct cleanup *)0)
76
77 /* Version number of GDB, as a string. */
78
79 extern char *version;
80
81 /* Message to be printed before the error message, when an error occurs. */
82
83 extern char *error_pre_print;
84
85 extern char lang_frame_mismatch_warn[]; /* language.c */
86
87 /* Whether GDB's stdin is on a terminal. */
88
89 extern int gdb_has_a_terminal; /* inflow.c */
90
91 /* Flag for whether we want all the "from_tty" gubbish printed. */
92
93 int caution = 1; /* Default is yes, sigh. */
94
95 /*
96 * Define all cmd_list_element's
97 */
98
99 /* Chain containing all defined commands. */
100
101 struct cmd_list_element *cmdlist;
102
103 /* Chain containing all defined info subcommands. */
104
105 struct cmd_list_element *infolist;
106
107 /* Chain containing all defined enable subcommands. */
108
109 struct cmd_list_element *enablelist;
110
111 /* Chain containing all defined disable subcommands. */
112
113 struct cmd_list_element *disablelist;
114
115 /* Chain containing all defined delete subcommands. */
116
117 struct cmd_list_element *deletelist;
118
119 /* Chain containing all defined "enable breakpoint" subcommands. */
120
121 struct cmd_list_element *enablebreaklist;
122
123 /* Chain containing all defined set subcommands */
124
125 struct cmd_list_element *setlist;
126
127 /* Chain containing all defined show subcommands. */
128 struct cmd_list_element *showlist;
129
130 /* Chain containing all defined \"set history\". */
131
132 struct cmd_list_element *sethistlist;
133
134 /* Chain containing all defined \"show history\". */
135 struct cmd_list_element *showhistlist;
136
137 /* Chain containing all defined \"unset history\". */
138
139 struct cmd_list_element *unsethistlist;
140
141 /* stdio stream that command input is being read from. */
142
143 FILE *instream;
144
145 /* Current working directory. */
146
147 char *current_directory;
148
149 /* The directory name is actually stored here (usually). */
150 static char dirbuf[1024];
151
152 /* Function to call before reading a command, if nonzero.
153 The function receives two args: an input stream,
154 and a prompt string. */
155
156 void (*window_hook) ();
157
158 extern int frame_file_full_name;
159 int epoch_interface;
160 int xgdb_verbose;
161
162 /* The external commands we call... */
163 extern void init_source_path ();
164 extern void directory_command ();
165 extern void exec_file_command ();
166 extern void symbol_file_command ();
167 extern void core_file_command ();
168 extern void tty_command ();
169
170 extern void help_list ();
171 extern void initialize_all_files ();
172 extern void init_malloc ();
173
174 /* Forward declarations for this file */
175 void free_command_lines ();
176 char *gdb_readline ();
177 char *command_line_input ();
178 static void initialize_history ();
179 static void initialize_main ();
180 static void initialize_cmd_lists ();
181 static void init_signals ();
182 static void quit_command ();
183 void command_loop ();
184 static void source_command ();
185 static void print_gdb_version ();
186 static void print_gnu_advertisement ();
187 static void float_handler ();
188 static void cd_command ();
189 static void read_command_file ();
190
191 char *getenv ();
192
193 /* gdb prints this when reading a command interactively */
194 static char *prompt;
195
196 /* Buffer used for reading command lines, and the size
197 allocated for it so far. */
198
199 char *line;
200 int linesize = 100;
201
202 /* Baud rate specified for talking to serial target systems. Default
203 is left as a zero pointer, so targets can choose their own defaults. */
204
205 char *baud_rate;
206
207 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
208
209 #ifndef STOP_SIGNAL
210 #ifdef SIGTSTP
211 #define STOP_SIGNAL SIGTSTP
212 #endif
213 #endif
214
215 /* Some System V have job control but not sigsetmask(). */
216 #if !defined (HAVE_SIGSETMASK)
217 #define HAVE_SIGSETMASK !defined (USG)
218 #endif
219
220 #if 0 == (HAVE_SIGSETMASK)
221 #define sigsetmask(n)
222 #endif
223 \f
224 /* This is how `error' returns to command level. */
225
226 jmp_buf to_top_level;
227
228 void
229 return_to_top_level ()
230 {
231 quit_flag = 0;
232 immediate_quit = 0;
233 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
234 clear_momentary_breakpoints ();
235 disable_current_display ();
236 do_cleanups (ALL_CLEANUPS);
237 longjmp (to_top_level, 1);
238 }
239
240 /* Call FUNC with arg ARGS, catching any errors.
241 If there is no error, return the value returned by FUNC.
242 If there is an error, print ERRSTRING, print the specific error message,
243 then return zero. */
244
245 int
246 catch_errors (func, args, errstring)
247 int (*func) ();
248 char *args;
249 char *errstring;
250 {
251 jmp_buf saved;
252 int val;
253 struct cleanup *saved_cleanup_chain;
254 char *saved_error_pre_print;
255
256 saved_cleanup_chain = save_cleanups ();
257 saved_error_pre_print = error_pre_print;
258
259 bcopy (to_top_level, saved, sizeof (jmp_buf));
260 error_pre_print = errstring;
261
262 if (setjmp (to_top_level) == 0)
263 val = (*func) (args);
264 else
265 val = 0;
266
267 restore_cleanups (saved_cleanup_chain);
268
269 error_pre_print = saved_error_pre_print;
270 bcopy (saved, to_top_level, sizeof (jmp_buf));
271 return val;
272 }
273
274 /* Handler for SIGHUP. */
275
276 static void
277 disconnect ()
278 {
279 kill_inferior_fast ();
280 signal (SIGHUP, SIG_DFL);
281 kill (getpid (), SIGHUP);
282 }
283 \f
284 /* Clean up on error during a "source" command (or execution of a
285 user-defined command). */
286
287 static void
288 source_cleanup (stream)
289 FILE *stream;
290 {
291 /* Restore the previous input stream. */
292 instream = stream;
293 }
294
295 /* Read commands from STREAM. */
296 static void
297 read_command_file (stream)
298 FILE *stream;
299 {
300 struct cleanup *cleanups;
301
302 cleanups = make_cleanup (source_cleanup, instream);
303 instream = stream;
304 command_loop ();
305 do_cleanups (cleanups);
306 }
307 \f
308 int
309 main (argc, argv)
310 int argc;
311 char **argv;
312 {
313 int count;
314 static int inhibit_gdbinit = 0;
315 static int quiet = 0;
316 static int batch = 0;
317
318 /* Pointers to various arguments from command line. */
319 char *symarg = NULL;
320 char *execarg = NULL;
321 char *corearg = NULL;
322 char *cdarg = NULL;
323 char *ttyarg = NULL;
324
325 /* Pointers to all arguments of +command option. */
326 char **cmdarg;
327 /* Allocated size of cmdarg. */
328 int cmdsize;
329 /* Number of elements of cmdarg used. */
330 int ncmd;
331
332 /* Indices of all arguments of +directory option. */
333 char **dirarg;
334 /* Allocated size. */
335 int dirsize;
336 /* Number of elements used. */
337 int ndir;
338
339 register int i;
340
341 /* This needs to happen before the first use of malloc. */
342 init_malloc ();
343
344 #if defined (ALIGN_STACK_ON_STARTUP)
345 i = (int) &count & 0x3;
346 if (i != 0)
347 alloca (4 - i);
348 #endif
349
350 /* If error() is called from initialization code, just exit */
351 if (setjmp (to_top_level)) {
352 exit(1);
353 }
354
355 cmdsize = 1;
356 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
357 ncmd = 0;
358 dirsize = 1;
359 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
360 ndir = 0;
361
362 quit_flag = 0;
363 line = (char *) xmalloc (linesize);
364 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
365 instream = stdin;
366
367 getcwd (dirbuf, sizeof (dirbuf));
368 current_directory = dirbuf;
369
370 #ifdef SET_STACK_LIMIT_HUGE
371 {
372 struct rlimit rlim;
373
374 /* Set the stack limit huge so that alloca (particularly stringtab
375 * in dbxread.c) does not fail. */
376 getrlimit (RLIMIT_STACK, &rlim);
377 original_stack_limit = rlim.rlim_cur;
378 rlim.rlim_cur = rlim.rlim_max;
379 setrlimit (RLIMIT_STACK, &rlim);
380 }
381 #endif /* SET_STACK_LIMIT_HUGE */
382
383 /* Parse arguments and options. */
384 {
385 int c;
386 static int print_help;
387 /* When var field is 0, use flag field to record the equivalent
388 short option (or arbitrary numbers starting at 10 for those
389 with no equivalent). */
390 static struct option long_options[] =
391 {
392 {"quiet", no_argument, &quiet, 1},
393 {"q", no_argument, &quiet, 1},
394 {"nx", no_argument, &inhibit_gdbinit, 1},
395 {"n", no_argument, &inhibit_gdbinit, 1},
396 {"batch", no_argument, &batch, 1},
397 {"epoch", no_argument, &epoch_interface, 1},
398 {"fullname", no_argument, &frame_file_full_name, 1},
399 {"f", no_argument, &frame_file_full_name, 1},
400 {"help", no_argument, &print_help, 1},
401 {"se", required_argument, 0, 10},
402 {"symbols", required_argument, 0, 's'},
403 {"s", required_argument, 0, 's'},
404 {"exec", required_argument, 0, 'e'},
405 {"e", required_argument, 0, 'e'},
406 {"core", required_argument, 0, 'c'},
407 {"c", required_argument, 0, 'c'},
408 {"command", required_argument, 0, 'x'},
409 {"x", required_argument, 0, 'x'},
410 {"directory", required_argument, 0, 'd'},
411 {"cd", required_argument, 0, 11},
412 {"tty", required_argument, 0, 't'},
413 {"baud", required_argument, 0, 'b'},
414 {"b", required_argument, 0, 'b'},
415 /* Allow machine descriptions to add more options... */
416 #ifdef ADDITIONAL_OPTIONS
417 ADDITIONAL_OPTIONS
418 #endif
419 {0, no_argument, 0, 0},
420 };
421
422 while (1)
423 {
424 int option_index;
425
426 c = getopt_long_only (argc, argv, "",
427 long_options, &option_index);
428 if (c == EOF)
429 break;
430
431 /* Long option that takes an argument. */
432 if (c == 0 && long_options[option_index].flag == 0)
433 c = long_options[option_index].val;
434
435 switch (c)
436 {
437 case 0:
438 /* Long option that just sets a flag. */
439 break;
440 case 10:
441 symarg = optarg;
442 execarg = optarg;
443 break;
444 case 11:
445 cdarg = optarg;
446 break;
447 case 's':
448 symarg = optarg;
449 break;
450 case 'e':
451 execarg = optarg;
452 break;
453 case 'c':
454 corearg = optarg;
455 break;
456 case 'x':
457 cmdarg[ncmd++] = optarg;
458 if (ncmd >= cmdsize)
459 {
460 cmdsize *= 2;
461 cmdarg = (char **) xrealloc ((char *)cmdarg,
462 cmdsize * sizeof (*cmdarg));
463 }
464 break;
465 case 'd':
466 dirarg[ndir++] = optarg;
467 if (ndir >= dirsize)
468 {
469 dirsize *= 2;
470 dirarg = (char **) xrealloc ((char *)dirarg,
471 dirsize * sizeof (*dirarg));
472 }
473 break;
474 case 't':
475 ttyarg = optarg;
476 break;
477 case 'q':
478 quiet = 1;
479 break;
480 case 'b':
481 baud_rate = optarg;
482 break;
483 #ifdef ADDITIONAL_OPTION_CASES
484 ADDITIONAL_OPTION_CASES
485 #endif
486 case '?':
487 fprintf (stderr,
488 "Use `%s +help' for a complete list of options.\n",
489 argv[0]);
490 exit (1);
491 }
492
493 }
494 if (print_help)
495 {
496 fputs ("\
497 This is GDB, the GNU debugger. Use the command\n\
498 gdb [options] [executable [core-file]]\n\
499 to enter the debugger.\n\
500 \n\
501 Options available are:\n\
502 -help Print this message.\n\
503 -quiet Do not print version number on startup.\n\
504 -fullname Output information used by emacs-GDB interface.\n\
505 -epoch Output information used by epoch emacs-GDB interface.\n\
506 -batch Exit after processing options.\n\
507 -nx Do not read .gdbinit file.\n\
508 -tty=TTY Use TTY for input/output by the program being debugged.\n\
509 -cd=DIR Change current directory to DIR.\n\
510 -directory=DIR Search for source files in DIR.\n\
511 -command=FILE Execute GDB commands from FILE.\n\
512 -symbols=SYMFILE Read symbols from SYMFILE.\n\
513 -exec=EXECFILE Use EXECFILE as the executable.\n\
514 -se=FILE Use FILE as symbol file and executable file.\n\
515 -core=COREFILE Analyze the core dump COREFILE.\n\
516 -b BAUDRATE Set serial port baud rate used for remote debugging\n\
517 ", stderr);
518 #ifdef ADDITIONAL_OPTION_HELP
519 fputs (ADDITIONAL_OPTION_HELP, stderr);
520 #endif
521 fputs ("\n\
522 For more information, type \"help\" from within GDB, or consult the\n\
523 GDB manual (available as on-line info or a printed manual).\n", stderr);
524 /* Exiting after printing this message seems like
525 the most useful thing to do. */
526 exit (0);
527 }
528
529 /* OK, that's all the options. The other arguments are filenames. */
530 count = 0;
531 for (; optind < argc; optind++)
532 switch (++count)
533 {
534 case 1:
535 symarg = argv[optind];
536 execarg = argv[optind];
537 break;
538 case 2:
539 corearg = argv[optind];
540 break;
541 case 3:
542 fprintf (stderr,
543 "Excess command line arguments ignored. (%s%s)\n",
544 argv[optind], (optind == argc - 1) ? "" : " ...");
545 break;
546 }
547 if (batch)
548 quiet = 1;
549 }
550
551 /* Run the init function of each source file */
552
553 initialize_cmd_lists (); /* This needs to be done first */
554 initialize_all_files ();
555 initialize_main (); /* But that omits this file! Do it now */
556 init_signals ();
557
558 if (!quiet)
559 {
560 /* Print all the junk at the top, with trailing "..." if we are about
561 to read a symbol file (possibly slowly). */
562 print_gnu_advertisement ();
563 print_gdb_version ();
564 if (symarg)
565 printf_filtered ("..");
566 wrap_here("");
567 fflush (stdout); /* Force to screen during slow operations */
568 }
569
570 error_pre_print = "\n\n";
571
572 /* Now perform all the actions indicated by the arguments. */
573 if (cdarg != NULL)
574 {
575 if (!setjmp (to_top_level))
576 {
577 cd_command (cdarg, 0);
578 init_source_path ();
579 }
580 }
581 do_cleanups (ALL_CLEANUPS);
582
583 for (i = 0; i < ndir; i++)
584 if (!setjmp (to_top_level))
585 directory_command (dirarg[i], 0);
586 free (dirarg);
587 do_cleanups (ALL_CLEANUPS);
588
589 if (execarg != NULL
590 && symarg != NULL
591 && strcmp (execarg, symarg) == 0)
592 {
593 /* The exec file and the symbol-file are the same. If we can't open
594 it, better only print one error message. */
595 if (!setjmp (to_top_level))
596 {
597 exec_file_command (execarg, !batch);
598 symbol_file_command (symarg, 0);
599 }
600 }
601 else
602 {
603 if (execarg != NULL)
604 if (!setjmp (to_top_level))
605 exec_file_command (execarg, !batch);
606 if (symarg != NULL)
607 if (!setjmp (to_top_level))
608 symbol_file_command (symarg, 0);
609 }
610 do_cleanups (ALL_CLEANUPS);
611
612 /* After the symbol file has been read, print a newline to get us
613 beyond the copyright line... But errors should still set off
614 the error message with a (single) blank line. */
615 if (!quiet)
616 printf_filtered ("\n");
617 error_pre_print = "\n";
618
619 /* Set the initial language. */
620 {
621 extern enum language deduce_language_from_filename ();
622 extern struct partial_symtab *find_main_psymtab ();
623 struct partial_symtab *pst = find_main_psymtab ();
624 enum language lang = language_unknown;
625 if (pst == NULL) ;
626 #if 0
627 /* A better solution would set the language when reading the psymtab.
628 This would win for symbol file formats that encode the langauge,
629 such as dwarf. But, we don't do that yet. FIXME */
630 else if (pst->language != language_unknown)
631 lang = pst->language;
632 #endif
633 else if (pst->filename != NULL)
634 lang = deduce_language_from_filename (pst->filename);
635 if (lang == language_unknown) /* Make C the default language */
636 lang = language_c;
637 set_language (lang);
638 }
639
640 if (corearg != NULL)
641 if (!setjmp (to_top_level))
642 core_file_command (corearg, !batch);
643 else if (isdigit (corearg[0]) && !setjmp (to_top_level))
644 attach_command (corearg, !batch);
645 do_cleanups (ALL_CLEANUPS);
646
647 if (ttyarg != NULL)
648 if (!setjmp (to_top_level))
649 tty_command (ttyarg, !batch);
650 do_cleanups (ALL_CLEANUPS);
651
652 #ifdef ADDITIONAL_OPTION_HANDLER
653 ADDITIONAL_OPTION_HANDLER;
654 #endif
655
656 /* Error messages should no longer be distinguished with extra output. */
657 error_pre_print = 0;
658
659 {
660 struct stat homebuf, cwdbuf;
661 char *homedir, *homeinit;
662
663 /* Read init file, if it exists in home directory */
664 homedir = getenv ("HOME");
665 if (homedir)
666 {
667 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
668 strlen (gdbinit) + 10);
669 strcpy (homeinit, getenv ("HOME"));
670 strcat (homeinit, "/");
671 strcat (homeinit, gdbinit);
672 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
673 if (!setjmp (to_top_level))
674 source_command (homeinit, 0);
675 do_cleanups (ALL_CLEANUPS);
676
677 /* Do stats; no need to do them elsewhere since we'll only
678 need them if homedir is set. Make sure that they are
679 zero in case one of them fails (this guarantees that they
680 won't match if either exists). */
681
682 bzero (&homebuf, sizeof (struct stat));
683 bzero (&cwdbuf, sizeof (struct stat));
684
685 stat (homeinit, &homebuf);
686 stat (gdbinit, &cwdbuf); /* We'll only need this if
687 homedir was set. */
688 }
689
690 /* Read the input file in the current directory, *if* it isn't
691 the same file (it should exist, also). */
692
693 if (!homedir
694 || bcmp ((char *) &homebuf,
695 (char *) &cwdbuf,
696 sizeof (struct stat)))
697 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
698 if (!setjmp (to_top_level))
699 source_command (gdbinit, 0);
700 do_cleanups (ALL_CLEANUPS);
701 }
702
703 for (i = 0; i < ncmd; i++)
704 if (!setjmp (to_top_level))
705 {
706 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
707 read_command_file (stdin);
708 else
709 source_command (cmdarg[i], !batch);
710 do_cleanups (ALL_CLEANUPS);
711 }
712 free (cmdarg);
713
714 /* Read in the old history after all the command files have been read. */
715 initialize_history();
716
717 if (batch)
718 {
719 /* We have hit the end of the batch file. */
720 exit (0);
721 }
722
723 /* Do any host- or target-specific hacks. This is used for i960 targets
724 to force the user to set a nindy target and spec its parameters. */
725
726 #ifdef BEFORE_MAIN_LOOP_HOOK
727 BEFORE_MAIN_LOOP_HOOK;
728 #endif
729
730 /* The command loop. */
731
732 while (1)
733 {
734 if (!setjmp (to_top_level))
735 {
736 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
737 command_loop ();
738 quit_command ((char *)0, instream == stdin);
739 }
740 }
741 /* No exit -- exit is through quit_command. */
742 }
743
744 /* Execute the line P as a command.
745 Pass FROM_TTY as second argument to the defining function. */
746
747 void
748 execute_command (p, from_tty)
749 char *p;
750 int from_tty;
751 {
752 register struct cmd_list_element *c;
753 register struct command_line *cmdlines;
754 register enum language flang;
755 static const struct language_defn *saved_language = 0;
756 static int warned = 0;
757
758 free_all_values ();
759
760 /* This can happen when command_line_input hits end of file. */
761 if (p == NULL)
762 return;
763
764 while (*p == ' ' || *p == '\t') p++;
765 if (*p)
766 {
767 char *arg;
768
769 c = lookup_cmd (&p, cmdlist, "", 0, 1);
770 /* Pass null arg rather than an empty one. */
771 arg = *p ? p : 0;
772 if (c->class == class_user)
773 {
774 struct cleanup *old_chain;
775
776 if (*p)
777 error ("User-defined commands cannot take arguments.");
778 cmdlines = c->user_commands;
779 if (cmdlines == 0)
780 /* Null command */
781 return;
782
783 /* Set the instream to 0, indicating execution of a
784 user-defined function. */
785 old_chain = make_cleanup (source_cleanup, instream);
786 instream = (FILE *) 0;
787 while (cmdlines)
788 {
789 execute_command (cmdlines->line, 0);
790 cmdlines = cmdlines->next;
791 }
792 do_cleanups (old_chain);
793 }
794 else if (c->type == set_cmd || c->type == show_cmd)
795 do_setshow_command (arg, from_tty & caution, c);
796 else if (c->function == NO_FUNCTION)
797 error ("That is not a command, just a help topic.");
798 else
799 (*c->function) (arg, from_tty & caution);
800 }
801
802 /* Tell the user if the language has changed (except first time). */
803 if (current_language != saved_language)
804 {
805 if (language_mode == language_mode_auto) {
806 if (saved_language)
807 language_info (1); /* Print what changed. */
808 }
809 saved_language = current_language;
810 warned = 0;
811 }
812
813 /* Warn the user if the working language does not match the
814 language of the current frame. Only warn the user if we are
815 actually running the program, i.e. there is a stack. */
816 /* FIXME: This should be cacheing the frame and only running when
817 the frame changes. */
818 if (target_has_stack)
819 {
820 flang = get_frame_language ();
821 if (!warned
822 && flang != language_unknown
823 && flang != current_language->la_language)
824 {
825 printf_filtered ("%s\n", lang_frame_mismatch_warn);
826 warned = 1;
827 }
828 }
829 }
830
831 /* ARGSUSED */
832 void
833 command_loop_marker (foo)
834 int foo;
835 {
836 }
837
838 /* Read commands from `instream' and execute them
839 until end of file or error reading instream. */
840 void
841 command_loop ()
842 {
843 struct cleanup *old_chain;
844 char *command;
845 int stdin_is_tty = ISATTY (stdin);
846
847 while (!feof (instream))
848 {
849 if (window_hook && instream == stdin)
850 (*window_hook) (instream, prompt);
851
852 quit_flag = 0;
853 if (instream == stdin && stdin_is_tty)
854 reinitialize_more_filter ();
855 old_chain = make_cleanup (command_loop_marker, 0);
856 command = command_line_input (instream == stdin ? prompt : 0,
857 instream == stdin);
858 if (command == 0)
859 return;
860 execute_command (command, instream == stdin);
861 /* Do any commands attached to breakpoint we stopped at. */
862 bpstat_do_actions (&stop_bpstat);
863 do_cleanups (old_chain);
864 }
865 }
866 \f
867 /* Commands call this if they do not want to be repeated by null lines. */
868
869 void
870 dont_repeat ()
871 {
872 /* If we aren't reading from standard input, we are saving the last
873 thing read from stdin in line and don't want to delete it. Null lines
874 won't repeat here in any case. */
875 if (instream == stdin)
876 *line = 0;
877 }
878 \f
879 /* Read a line from the stream "instream" without command line editing.
880
881 It prints PRROMPT once at the start.
882 Action is compatible with "readline", e.g. space for the result is
883 malloc'd and should be freed by the caller.
884
885 A NULL return means end of file. */
886 char *
887 gdb_readline (prrompt)
888 char *prrompt;
889 {
890 int c;
891 char *result;
892 int input_index = 0;
893 int result_size = 80;
894
895 if (prrompt)
896 {
897 printf (prrompt);
898 fflush (stdout);
899 }
900
901 result = (char *) xmalloc (result_size);
902
903 while (1)
904 {
905 /* Read from stdin if we are executing a user defined command.
906 This is the right thing for prompt_for_continue, at least. */
907 c = fgetc (instream ? instream : stdin);
908
909 if (c == EOF)
910 {
911 free (result);
912 return NULL;
913 }
914
915 if (c == '\n')
916 break;
917
918 result[input_index++] = c;
919 while (input_index >= result_size)
920 {
921 result_size *= 2;
922 result = (char *) xrealloc (result, result_size);
923 }
924 }
925
926 result[input_index++] = '\0';
927 return result;
928 }
929
930 /* Declaration for fancy readline with command line editing. */
931 char *readline ();
932
933 /* Variables which control command line editing and history
934 substitution. These variables are given default values at the end
935 of this file. */
936 static int command_editing_p;
937 static int history_expansion_p;
938 static int write_history_p;
939 static int history_size;
940 static char *history_filename;
941
942 /* Variables which are necessary for fancy command line editing. */
943 char *gdb_completer_word_break_characters =
944 " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
945
946 /* Functions that are used as part of the fancy command line editing. */
947
948 /* This can be used for functions which don't want to complete on symbols
949 but don't want to complete on anything else either. */
950 /* ARGSUSED */
951 char **
952 noop_completer (text)
953 char *text;
954 {
955 return NULL;
956 }
957
958 /* Generate symbol names one by one for the completer. If STATE is
959 zero, then we need to initialize, otherwise the initialization has
960 already taken place. TEXT is what we expect the symbol to start
961 with. RL_LINE_BUFFER is available to be looked at; it contains the
962 entire text of the line. RL_POINT is the offset in that line of
963 the cursor. You should pretend that the line ends at RL_POINT.
964 The result is NULL if there are no more completions, else a char
965 string which is a possible completion. */
966 char *
967 symbol_completion_function (text, state)
968 char *text;
969 int state;
970 {
971 static char **list = (char **)NULL;
972 static int index;
973 char *output;
974 extern char *rl_line_buffer;
975 extern int rl_point;
976 char *tmp_command, *p;
977 struct cmd_list_element *c, *result_list;
978
979 if (!state)
980 {
981 /* Free the storage used by LIST, but not by the strings inside. This is
982 because rl_complete_internal () frees the strings. */
983 if (list)
984 free (list);
985 list = 0;
986 index = 0;
987
988 /* Decide whether to complete on a list of gdb commands or on
989 symbols. */
990 tmp_command = (char *) alloca (rl_point + 1);
991 p = tmp_command;
992
993 strncpy (tmp_command, rl_line_buffer, rl_point);
994 tmp_command[rl_point] = '\0';
995
996 if (rl_point == 0)
997 {
998 /* An empty line we want to consider ambiguous; that is,
999 it could be any command. */
1000 c = (struct cmd_list_element *) -1;
1001 result_list = 0;
1002 }
1003 else
1004 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1005
1006 /* Move p up to the next interesting thing. */
1007 while (*p == ' ' || *p == '\t')
1008 p++;
1009
1010 if (!c)
1011 /* He's typed something unrecognizable. Sigh. */
1012 list = (char **) 0;
1013 else if (c == (struct cmd_list_element *) -1)
1014 {
1015 /* If we didn't recognize everything up to the thing that
1016 needs completing, and we don't know what command it is
1017 yet, we are in trouble. Part of the trouble might be
1018 that the list of delimiters used by readline includes
1019 '-', which we use in commands. Check for this. */
1020 if (p + strlen(text) != tmp_command + rl_point) {
1021 if (tmp_command[rl_point - strlen(text) - 1] == '-')
1022 text = p;
1023 else {
1024 /* This really should not produce an error. Better would
1025 be to pretend to hit RETURN here; this would produce a
1026 response like "Ambiguous command: foo, foobar, etc",
1027 and leave the line available for re-entry with ^P. Instead,
1028 this error blows away the user's typed input without
1029 any way to get it back. */
1030 error (" Unrecognized command.");
1031 }
1032 }
1033
1034 /* He's typed something ambiguous. This is easier. */
1035 if (result_list)
1036 list = complete_on_cmdlist (*result_list->prefixlist, text);
1037 else
1038 list = complete_on_cmdlist (cmdlist, text);
1039 }
1040 else
1041 {
1042 /* If we've gotten this far, gdb has recognized a full
1043 command. There are several possibilities:
1044
1045 1) We need to complete on the command.
1046 2) We need to complete on the possibilities coming after
1047 the command.
1048 2) We need to complete the text of what comes after the
1049 command. */
1050
1051 if (!*p && *text)
1052 /* Always (might be longer versions of thie command). */
1053 list = complete_on_cmdlist (result_list, text);
1054 else if (!*p && !*text)
1055 {
1056 if (c->prefixlist)
1057 list = complete_on_cmdlist (*c->prefixlist, "");
1058 else
1059 list = (*c->completer) ("");
1060 }
1061 else
1062 {
1063 if (c->prefixlist && !c->allow_unknown)
1064 {
1065 #if 0
1066 /* Something like "info adsfkdj". But error() is not
1067 the proper response; just return no completions
1068 instead. */
1069 *p = '\0';
1070 error ("\"%s\" command requires a subcommand.",
1071 tmp_command);
1072 #else
1073 list = NULL;
1074 #endif
1075 }
1076 else
1077 list = (*c->completer) (text);
1078 }
1079 }
1080 }
1081
1082 /* If the debugged program wasn't compiled with symbols, or if we're
1083 clearly completing on a command and no command matches, return
1084 NULL. */
1085 if (!list)
1086 return ((char *)NULL);
1087
1088 output = list[index];
1089 if (output)
1090 index++;
1091
1092 return (output);
1093 }
1094 \f
1095 #ifdef STOP_SIGNAL
1096 static void
1097 stop_sig ()
1098 {
1099 #if STOP_SIGNAL == SIGTSTP
1100 signal (SIGTSTP, SIG_DFL);
1101 sigsetmask (0);
1102 kill (getpid (), SIGTSTP);
1103 signal (SIGTSTP, stop_sig);
1104 #else
1105 signal (STOP_SIGNAL, stop_sig);
1106 #endif
1107 printf ("%s", prompt);
1108 fflush (stdout);
1109
1110 /* Forget about any previous command -- null line now will do nothing. */
1111 dont_repeat ();
1112 }
1113 #endif /* STOP_SIGNAL */
1114
1115 /* Initialize signal handlers. */
1116 static void
1117 do_nothing ()
1118 {
1119 }
1120
1121 static void
1122 init_signals ()
1123 {
1124 extern void request_quit ();
1125
1126 signal (SIGINT, request_quit);
1127
1128 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1129 passed to the inferior, which we don't want. It would be
1130 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1131 on BSD4.3 systems using vfork, that can affect the
1132 GDB process as well as the inferior (the signal handling tables
1133 might be in memory, shared between the two). Since we establish
1134 a handler for SIGQUIT, when we call exec it will set the signal
1135 to SIG_DFL for us. */
1136 signal (SIGQUIT, do_nothing);
1137 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1138 signal (SIGHUP, disconnect);
1139 signal (SIGFPE, float_handler);
1140 }
1141 \f
1142 /* Read one line from the command input stream `instream'
1143 into the local static buffer `linebuffer' (whose current length
1144 is `linelength').
1145 The buffer is made bigger as necessary.
1146 Returns the address of the start of the line.
1147
1148 NULL is returned for end of file.
1149
1150 *If* the instream == stdin & stdin is a terminal, the line read
1151 is copied into the file line saver (global var char *line,
1152 length linesize) so that it can be duplicated.
1153
1154 This routine either uses fancy command line editing or
1155 simple input as the user has requested. */
1156
1157 char *
1158 command_line_input (prrompt, repeat)
1159 char *prrompt;
1160 int repeat;
1161 {
1162 static char *linebuffer = 0;
1163 static int linelength = 0;
1164 register char *p;
1165 char *p1;
1166 char *rl;
1167 char *local_prompt = prrompt;
1168 register int c;
1169 char *nline;
1170 char got_eof = 0;
1171
1172 if (linebuffer == 0)
1173 {
1174 linelength = 80;
1175 linebuffer = (char *) xmalloc (linelength);
1176 }
1177
1178 p = linebuffer;
1179
1180 /* Control-C quits instantly if typed while in this loop
1181 since it should not wait until the user types a newline. */
1182 immediate_quit++;
1183 #ifdef STOP_SIGNAL
1184 signal (STOP_SIGNAL, stop_sig);
1185 #endif
1186
1187 while (1)
1188 {
1189 /* Reports are that some Sys V's don't flush stdout/err on reads
1190 from stdin, when stdin/out are sockets rather than ttys. So we
1191 have to do it ourselves, to make emacs-gdb and xxgdb work.
1192 On other machines, doing this once per input should be a cheap nop. */
1193 fflush (stdout);
1194 fflush (stderr);
1195
1196 /* Don't use fancy stuff if not talking to stdin. */
1197 if (command_editing_p && instream == stdin
1198 && ISATTY (instream))
1199 rl = readline (local_prompt);
1200 else
1201 rl = gdb_readline (local_prompt);
1202
1203 if (!rl || rl == (char *) EOF)
1204 {
1205 got_eof = 1;
1206 break;
1207 }
1208 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
1209 {
1210 linelength = strlen(rl) + 1 + (p - linebuffer);
1211 nline = (char *) xrealloc (linebuffer, linelength);
1212 p += nline - linebuffer;
1213 linebuffer = nline;
1214 }
1215 p1 = rl;
1216 /* Copy line. Don't copy null at end. (Leaves line alone
1217 if this was just a newline) */
1218 while (*p1)
1219 *p++ = *p1++;
1220
1221 free (rl); /* Allocated in readline. */
1222
1223 if (p == linebuffer || *(p - 1) != '\\')
1224 break;
1225
1226 p--; /* Put on top of '\'. */
1227 local_prompt = (char *) 0;
1228 }
1229
1230 #ifdef STOP_SIGNAL
1231 signal (SIGTSTP, SIG_DFL);
1232 #endif
1233 immediate_quit--;
1234
1235 if (got_eof)
1236 return NULL;
1237
1238 /* Do history expansion if that is wished. */
1239 if (history_expansion_p && instream == stdin
1240 && ISATTY (instream))
1241 {
1242 char *history_value;
1243 int expanded;
1244
1245 *p = '\0'; /* Insert null now. */
1246 expanded = history_expand (linebuffer, &history_value);
1247 if (expanded)
1248 {
1249 /* Print the changes. */
1250 printf ("%s\n", history_value);
1251
1252 /* If there was an error, call this function again. */
1253 if (expanded < 0)
1254 {
1255 free (history_value);
1256 return command_line_input (prrompt, repeat);
1257 }
1258 if (strlen (history_value) > linelength)
1259 {
1260 linelength = strlen (history_value) + 1;
1261 linebuffer = (char *) xrealloc (linebuffer, linelength);
1262 }
1263 strcpy (linebuffer, history_value);
1264 p = linebuffer + strlen(linebuffer);
1265 free (history_value);
1266 }
1267 }
1268
1269 /* If we just got an empty line, and that is supposed
1270 to repeat the previous command, return the value in the
1271 global buffer. */
1272 if (repeat)
1273 {
1274 if (p == linebuffer)
1275 return line;
1276 p1 = linebuffer;
1277 while (*p1 == ' ' || *p1 == '\t')
1278 p1++;
1279 if (!*p1)
1280 return line;
1281 }
1282
1283 *p = 0;
1284
1285 /* Add line to history if appropriate. */
1286 if (instream == stdin
1287 && ISATTY (stdin) && *linebuffer)
1288 add_history (linebuffer);
1289
1290 /* Note: lines consisting soley of comments are added to the command
1291 history. This is useful when you type a command, and then
1292 realize you don't want to execute it quite yet. You can comment
1293 out the command and then later fetch it from the value history
1294 and remove the '#'. The kill ring is probably better, but some
1295 people are in the habit of commenting things out. */
1296 p1 = linebuffer;
1297 while ((c = *p1++) != '\0')
1298 {
1299 if (c == '"')
1300 while ((c = *p1++) != '"')
1301 {
1302 /* Make sure an escaped '"' doesn't make us think the string
1303 is ended. */
1304 if (c == '\\')
1305 parse_escape (&p1);
1306 if (c == '\0')
1307 break;
1308 }
1309 else if (c == '\'')
1310 while ((c = *p1++) != '\'')
1311 {
1312 /* Make sure an escaped '\'' doesn't make us think the string
1313 is ended. */
1314 if (c == '\\')
1315 parse_escape (&p1);
1316 if (c == '\0')
1317 break;
1318 }
1319 else if (c == '#')
1320 {
1321 /* Found a comment. */
1322 p1[-1] = '\0';
1323 break;
1324 }
1325 }
1326
1327 /* Save into global buffer if appropriate. */
1328 if (repeat)
1329 {
1330 if (linelength > linesize)
1331 {
1332 line = xrealloc (line, linelength);
1333 linesize = linelength;
1334 }
1335 strcpy (line, linebuffer);
1336 return line;
1337 }
1338
1339 return linebuffer;
1340 }
1341 \f
1342 /* Read lines from the input stream
1343 and accumulate them in a chain of struct command_line's
1344 which is then returned. */
1345
1346 struct command_line *
1347 read_command_lines ()
1348 {
1349 struct command_line *first = 0;
1350 register struct command_line *next, *tail = 0;
1351 register char *p, *p1;
1352 struct cleanup *old_chain = 0;
1353
1354 while (1)
1355 {
1356 dont_repeat ();
1357 p = command_line_input (0, instream == stdin);
1358 if (p == NULL)
1359 /* Treat end of file like "end". */
1360 break;
1361
1362 /* Remove leading and trailing blanks. */
1363 while (*p == ' ' || *p == '\t') p++;
1364 p1 = p + strlen (p);
1365 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
1366
1367 /* Is this "end"? */
1368 if (p1 - p == 3 && !strncmp (p, "end", 3))
1369 break;
1370
1371 /* No => add this line to the chain of command lines. */
1372 next = (struct command_line *) xmalloc (sizeof (struct command_line));
1373 next->line = savestring (p, p1 - p);
1374 next->next = 0;
1375 if (tail)
1376 {
1377 tail->next = next;
1378 }
1379 else
1380 {
1381 /* We just read the first line.
1382 From now on, arrange to throw away the lines we have
1383 if we quit or get an error while inside this function. */
1384 first = next;
1385 old_chain = make_cleanup (free_command_lines, &first);
1386 }
1387 tail = next;
1388 }
1389
1390 dont_repeat ();
1391
1392 /* Now we are about to return the chain to our caller,
1393 so freeing it becomes his responsibility. */
1394 if (first)
1395 discard_cleanups (old_chain);
1396 return first;
1397 }
1398
1399 /* Free a chain of struct command_line's. */
1400
1401 void
1402 free_command_lines (lptr)
1403 struct command_line **lptr;
1404 {
1405 register struct command_line *l = *lptr;
1406 register struct command_line *next;
1407
1408 while (l)
1409 {
1410 next = l->next;
1411 free (l->line);
1412 free (l);
1413 l = next;
1414 }
1415 }
1416 \f
1417 /* Add an element to the list of info subcommands. */
1418
1419 void
1420 add_info (name, fun, doc)
1421 char *name;
1422 void (*fun) ();
1423 char *doc;
1424 {
1425 add_cmd (name, no_class, fun, doc, &infolist);
1426 }
1427
1428 /* Add an alias to the list of info subcommands. */
1429
1430 void
1431 add_info_alias (name, oldname, abbrev_flag)
1432 char *name;
1433 char *oldname;
1434 int abbrev_flag;
1435 {
1436 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
1437 }
1438
1439 /* The "info" command is defined as a prefix, with allow_unknown = 0.
1440 Therefore, its own definition is called only for "info" with no args. */
1441
1442 /* ARGSUSED */
1443 static void
1444 info_command (arg, from_tty)
1445 char *arg;
1446 int from_tty;
1447 {
1448 printf ("\"info\" must be followed by the name of an info command.\n");
1449 help_list (infolist, "info ", -1, stdout);
1450 }
1451
1452 /* The "show" command with no arguments shows all the settings. */
1453
1454 /* ARGSUSED */
1455 static void
1456 show_command (arg, from_tty)
1457 char *arg;
1458 int from_tty;
1459 {
1460 cmd_show_list (showlist, from_tty, "");
1461 }
1462 \f
1463 /* Add an element to the list of commands. */
1464
1465 void
1466 add_com (name, class, fun, doc)
1467 char *name;
1468 enum command_class class;
1469 void (*fun) ();
1470 char *doc;
1471 {
1472 add_cmd (name, class, fun, doc, &cmdlist);
1473 }
1474
1475 /* Add an alias or abbreviation command to the list of commands. */
1476
1477 void
1478 add_com_alias (name, oldname, class, abbrev_flag)
1479 char *name;
1480 char *oldname;
1481 enum command_class class;
1482 int abbrev_flag;
1483 {
1484 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
1485 }
1486
1487 void
1488 error_no_arg (why)
1489 char *why;
1490 {
1491 error ("Argument required (%s).", why);
1492 }
1493
1494 /* ARGSUSED */
1495 static void
1496 help_command (command, from_tty)
1497 char *command;
1498 int from_tty; /* Ignored */
1499 {
1500 help_cmd (command, stdout);
1501 }
1502 \f
1503 static void
1504 validate_comname (comname)
1505 char *comname;
1506 {
1507 register char *p;
1508
1509 if (comname == 0)
1510 error_no_arg ("name of command to define");
1511
1512 p = comname;
1513 while (*p)
1514 {
1515 if (!(*p >= 'A' && *p <= 'Z')
1516 && !(*p >= 'a' && *p <= 'z')
1517 && !(*p >= '0' && *p <= '9')
1518 && *p != '-')
1519 error ("Junk in argument list: \"%s\"", p);
1520 p++;
1521 }
1522 }
1523
1524 static void
1525 define_command (comname, from_tty)
1526 char *comname;
1527 int from_tty;
1528 {
1529 register struct command_line *cmds;
1530 register struct cmd_list_element *c, *newc;
1531 char *tem = comname;
1532 extern void not_just_help_class_command ();
1533
1534 validate_comname (comname);
1535
1536 /* Look it up, and verify that we got an exact match. */
1537 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1538 if (c && 0 != strcmp (comname, c->name))
1539 c = 0;
1540
1541 if (c)
1542 {
1543 if (c->class == class_user || c->class == class_alias)
1544 tem = "Redefine command \"%s\"? ";
1545 else
1546 tem = "Really redefine built-in command \"%s\"? ";
1547 if (!query (tem, comname))
1548 error ("Command \"%s\" not redefined.", comname);
1549 }
1550
1551 if (from_tty)
1552 {
1553 printf ("Type commands for definition of \"%s\".\n\
1554 End with a line saying just \"end\".\n", comname);
1555 fflush (stdout);
1556 }
1557 comname = savestring (comname, strlen (comname));
1558
1559 cmds = read_command_lines ();
1560
1561 if (c && c->class == class_user)
1562 free_command_lines (&c->user_commands);
1563
1564 newc = add_cmd (comname, class_user, not_just_help_class_command,
1565 (c && c->class == class_user)
1566 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1567 newc->user_commands = cmds;
1568 }
1569
1570 static void
1571 document_command (comname, from_tty)
1572 char *comname;
1573 int from_tty;
1574 {
1575 struct command_line *doclines;
1576 register struct cmd_list_element *c;
1577 char *tem = comname;
1578
1579 validate_comname (comname);
1580
1581 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1582
1583 if (c->class != class_user)
1584 error ("Command \"%s\" is built-in.", comname);
1585
1586 if (from_tty)
1587 printf ("Type documentation for \"%s\".\n\
1588 End with a line saying just \"end\".\n", comname);
1589
1590 doclines = read_command_lines ();
1591
1592 if (c->doc) free (c->doc);
1593
1594 {
1595 register struct command_line *cl1;
1596 register int len = 0;
1597
1598 for (cl1 = doclines; cl1; cl1 = cl1->next)
1599 len += strlen (cl1->line) + 1;
1600
1601 c->doc = (char *) xmalloc (len + 1);
1602 *c->doc = 0;
1603
1604 for (cl1 = doclines; cl1; cl1 = cl1->next)
1605 {
1606 strcat (c->doc, cl1->line);
1607 if (cl1->next)
1608 strcat (c->doc, "\n");
1609 }
1610 }
1611
1612 free_command_lines (&doclines);
1613 }
1614 \f
1615 static void
1616 print_gnu_advertisement()
1617 {
1618 printf ("\
1619 GDB is free software and you are welcome to distribute copies of it\n\
1620 under certain conditions; type \"show copying\" to see the conditions.\n\
1621 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
1622 ");
1623 }
1624
1625 static void
1626 print_gdb_version ()
1627 {
1628 printf_filtered ("\
1629 GDB %s, Copyright 1992 Free Software Foundation, Inc.",
1630 version);
1631 }
1632
1633 /* ARGSUSED */
1634 static void
1635 show_version (args, from_tty)
1636 char *args;
1637 int from_tty;
1638 {
1639 immediate_quit++;
1640 print_gnu_advertisement ();
1641 print_gdb_version ();
1642 printf_filtered ("\n");
1643 immediate_quit--;
1644 }
1645 \f
1646 /* xgdb calls this to reprint the usual GDB prompt. */
1647
1648 void
1649 print_prompt ()
1650 {
1651 printf ("%s", prompt);
1652 fflush (stdout);
1653 }
1654 \f
1655 static void
1656 quit_command (args, from_tty)
1657 char *args;
1658 int from_tty;
1659 {
1660 if (inferior_pid != 0 && target_has_execution)
1661 {
1662 if (query ("The program is running. Quit anyway? "))
1663 {
1664 target_kill ();
1665 }
1666 else
1667 error ("Not confirmed.");
1668 }
1669 /* Save the history information if it is appropriate to do so. */
1670 if (write_history_p && history_filename)
1671 write_history (history_filename);
1672 exit (0);
1673 }
1674
1675 /* Returns whether GDB is running on a terminal and whether the user
1676 desires that questions be asked of them on that terminal. */
1677
1678 int
1679 input_from_terminal_p ()
1680 {
1681 return gdb_has_a_terminal && (instream == stdin) & caution;
1682 }
1683 \f
1684 /* ARGSUSED */
1685 static void
1686 pwd_command (args, from_tty)
1687 char *args;
1688 int from_tty;
1689 {
1690 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
1691 getcwd (dirbuf, sizeof (dirbuf));
1692
1693 if (strcmp (dirbuf, current_directory))
1694 printf ("Working directory %s\n (canonically %s).\n",
1695 current_directory, dirbuf);
1696 else
1697 printf ("Working directory %s.\n", current_directory);
1698 }
1699
1700 static void
1701 cd_command (dir, from_tty)
1702 char *dir;
1703 int from_tty;
1704 {
1705 int len;
1706 int change;
1707
1708 /* If the new directory is absolute, repeat is a no-op; if relative,
1709 repeat might be useful but is more likely to be a mistake. */
1710 dont_repeat ();
1711
1712 if (dir == 0)
1713 error_no_arg ("new working directory");
1714
1715 dir = tilde_expand (dir);
1716 make_cleanup (free, dir);
1717
1718 if (chdir (dir) < 0)
1719 perror_with_name (dir);
1720
1721 len = strlen (dir);
1722 dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
1723 if (dir[0] == '/')
1724 current_directory = dir;
1725 else
1726 {
1727 current_directory = concat (current_directory, "/", dir, NULL);
1728 free (dir);
1729 }
1730
1731 /* Now simplify any occurrences of `.' and `..' in the pathname. */
1732
1733 change = 1;
1734 while (change)
1735 {
1736 char *p;
1737 change = 0;
1738
1739 for (p = current_directory; *p;)
1740 {
1741 if (!strncmp (p, "/./", 2)
1742 && (p[2] == 0 || p[2] == '/'))
1743 strcpy (p, p + 2);
1744 else if (!strncmp (p, "/..", 3)
1745 && (p[3] == 0 || p[3] == '/')
1746 && p != current_directory)
1747 {
1748 char *q = p;
1749 while (q != current_directory && q[-1] != '/') q--;
1750 if (q != current_directory)
1751 {
1752 strcpy (q-1, p+3);
1753 p = q-1;
1754 }
1755 }
1756 else p++;
1757 }
1758 }
1759
1760 forget_cached_source_info ();
1761
1762 if (from_tty)
1763 pwd_command ((char *) 0, 1);
1764 }
1765 \f
1766 /* ARGSUSED */
1767 static void
1768 source_command (args, from_tty)
1769 char *args;
1770 int from_tty;
1771 {
1772 FILE *stream;
1773 struct cleanup *cleanups;
1774 char *file = args;
1775
1776 if (file == 0)
1777 /* Let source without arguments read .gdbinit. */
1778 file = gdbinit;
1779
1780 file = tilde_expand (file);
1781 make_cleanup (free, file);
1782
1783 stream = fopen (file, "r");
1784 if (stream == 0)
1785 perror_with_name (file);
1786
1787 cleanups = make_cleanup (fclose, stream);
1788
1789 read_command_file (stream);
1790
1791 do_cleanups (cleanups);
1792 }
1793
1794 /* ARGSUSED */
1795 static void
1796 echo_command (text, from_tty)
1797 char *text;
1798 int from_tty;
1799 {
1800 char *p = text;
1801 register int c;
1802
1803 if (text)
1804 while (c = *p++)
1805 {
1806 if (c == '\\')
1807 {
1808 /* \ at end of argument is used after spaces
1809 so they won't be lost. */
1810 if (*p == 0)
1811 return;
1812
1813 c = parse_escape (&p);
1814 if (c >= 0)
1815 printf_filtered ("%c", c);
1816 }
1817 else
1818 printf_filtered ("%c", c);
1819 }
1820
1821 /* Force this output to appear now. */
1822 wrap_here ("");
1823 fflush (stdout);
1824 }
1825
1826 /* ARGSUSED */
1827 static void
1828 dump_me_command (args, from_tty)
1829 char *args;
1830 int from_tty;
1831 {
1832 if (query ("Should GDB dump core? "))
1833 {
1834 signal (SIGQUIT, SIG_DFL);
1835 kill (getpid (), SIGQUIT);
1836 }
1837 }
1838 \f
1839 /* Functions to manipulate command line editing control variables. */
1840
1841 /* Number of commands to print in each call to show_commands. */
1842 #define Hist_print 10
1843 static void
1844 show_commands (args, from_tty)
1845 char *args;
1846 int from_tty;
1847 {
1848 /* Index for history commands. Relative to history_base. */
1849 int offset;
1850
1851 /* Number of the history entry which we are planning to display next.
1852 Relative to history_base. */
1853 static int num = 0;
1854
1855 /* The first command in the history which doesn't exist (i.e. one more
1856 than the number of the last command). Relative to history_base. */
1857 int hist_len;
1858
1859 struct _hist_entry *history_get();
1860 extern int history_base;
1861
1862 /* Print out some of the commands from the command history. */
1863 /* First determine the length of the history list. */
1864 hist_len = history_size;
1865 for (offset = 0; offset < history_size; offset++)
1866 {
1867 if (!history_get (history_base + offset))
1868 {
1869 hist_len = offset;
1870 break;
1871 }
1872 }
1873
1874 if (args)
1875 {
1876 if (args[0] == '+' && args[1] == '\0')
1877 /* "info editing +" should print from the stored position. */
1878 ;
1879 else
1880 /* "info editing <exp>" should print around command number <exp>. */
1881 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
1882 }
1883 /* "show commands" means print the last Hist_print commands. */
1884 else
1885 {
1886 num = hist_len - Hist_print;
1887 }
1888
1889 if (num < 0)
1890 num = 0;
1891
1892 /* If there are at least Hist_print commands, we want to display the last
1893 Hist_print rather than, say, the last 6. */
1894 if (hist_len - num < Hist_print)
1895 {
1896 num = hist_len - Hist_print;
1897 if (num < 0)
1898 num = 0;
1899 }
1900
1901 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
1902 {
1903 printf_filtered ("%5d %s\n", history_base + offset,
1904 (history_get (history_base + offset))->line);
1905 }
1906
1907 /* The next command we want to display is the next one that we haven't
1908 displayed yet. */
1909 num += Hist_print;
1910
1911 /* If the user repeats this command with return, it should do what
1912 "show commands +" does. This is unnecessary if arg is null,
1913 because "show commands +" is not useful after "show commands". */
1914 if (from_tty && args)
1915 {
1916 args[0] = '+';
1917 args[1] = '\0';
1918 }
1919 }
1920
1921 /* Called by do_setshow_command. */
1922 /* ARGSUSED */
1923 static void
1924 set_history_size_command (args, from_tty, c)
1925 char *args;
1926 int from_tty;
1927 struct cmd_list_element *c;
1928 {
1929 if (history_size == UINT_MAX)
1930 unstifle_history ();
1931 else
1932 stifle_history (history_size);
1933 }
1934
1935 /* ARGSUSED */
1936 static void
1937 set_history (args, from_tty)
1938 char *args;
1939 int from_tty;
1940 {
1941 printf ("\"set history\" must be followed by the name of a history subcommand.\n");
1942 help_list (sethistlist, "set history ", -1, stdout);
1943 }
1944
1945 /* ARGSUSED */
1946 static void
1947 show_history (args, from_tty)
1948 char *args;
1949 int from_tty;
1950 {
1951 cmd_show_list (showhistlist, from_tty, "");
1952 }
1953
1954 int info_verbose = 0; /* Default verbose msgs off */
1955
1956 /* Called by do_setshow_command. An elaborate joke. */
1957 /* ARGSUSED */
1958 static void
1959 set_verbose (args, from_tty, c)
1960 char *args;
1961 int from_tty;
1962 struct cmd_list_element *c;
1963 {
1964 char *cmdname = "verbose";
1965 struct cmd_list_element *showcmd;
1966
1967 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
1968
1969 if (info_verbose)
1970 {
1971 c->doc = "Set verbose printing of informational messages.";
1972 showcmd->doc = "Show verbose printing of informational messages.";
1973 }
1974 else
1975 {
1976 c->doc = "Set verbosity.";
1977 showcmd->doc = "Show verbosity.";
1978 }
1979 }
1980
1981 static void
1982 float_handler ()
1983 {
1984 /* This message is based on ANSI C, section 4.7. Note that integer
1985 divide by zero causes this, so "float" is a misnomer. */
1986 error ("Erroneous arithmetic operation.");
1987 }
1988
1989 /* Return whether we are running a batch file or from terminal. */
1990 int
1991 batch_mode ()
1992 {
1993 return !(instream == stdin && ISATTY (stdin));
1994 }
1995
1996 \f
1997 static void
1998 initialize_cmd_lists ()
1999 {
2000 cmdlist = (struct cmd_list_element *) 0;
2001 infolist = (struct cmd_list_element *) 0;
2002 enablelist = (struct cmd_list_element *) 0;
2003 disablelist = (struct cmd_list_element *) 0;
2004 deletelist = (struct cmd_list_element *) 0;
2005 enablebreaklist = (struct cmd_list_element *) 0;
2006 setlist = (struct cmd_list_element *) 0;
2007 showlist = NULL;
2008 sethistlist = (struct cmd_list_element *) 0;
2009 showhistlist = NULL;
2010 unsethistlist = (struct cmd_list_element *) 0;
2011 }
2012
2013 /* Init the history buffer. Note that we are called after the init file(s)
2014 * have been read so that the user can change the history file via his
2015 * .gdbinit file (for instance). The GDBHISTFILE environment variable
2016 * overrides all of this.
2017 */
2018
2019 static void
2020 initialize_history()
2021 {
2022 char *tmpenv;
2023
2024 if (tmpenv = getenv ("HISTSIZE"))
2025 history_size = atoi (tmpenv);
2026 else if (!history_size)
2027 history_size = 256;
2028
2029 stifle_history (history_size);
2030
2031 if (tmpenv = getenv ("GDBHISTFILE"))
2032 history_filename = savestring (tmpenv, strlen(tmpenv));
2033 else if (!history_filename) {
2034 /* We include the current directory so that if the user changes
2035 directories the file written will be the same as the one
2036 that was read. */
2037 history_filename = concat (current_directory, "/.gdb_history", NULL);
2038 }
2039 read_history (history_filename);
2040 }
2041
2042 static void
2043 initialize_main ()
2044 {
2045 struct cmd_list_element *c;
2046
2047 #ifdef DEFAULT_PROMPT
2048 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
2049 #else
2050 prompt = savestring ("(gdb) ", 6);
2051 #endif
2052
2053 /* Set the important stuff up for command editing. */
2054 command_editing_p = 1;
2055 history_expansion_p = 0;
2056 write_history_p = 0;
2057
2058 /* Setup important stuff for command line editing. */
2059 rl_completion_entry_function = (int (*)()) symbol_completion_function;
2060 rl_completer_word_break_characters = gdb_completer_word_break_characters;
2061 rl_readline_name = "gdb";
2062
2063 /* Define the classes of commands.
2064 They will appear in the help list in the reverse of this order. */
2065
2066 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
2067 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
2068 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
2069 The commands in this class are those defined by the user.\n\
2070 Use the \"define\" command to define a command.", &cmdlist);
2071 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
2072 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
2073 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
2074 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
2075 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
2076 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
2077 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2078 counting from zero for the innermost (currently executing) frame.\n\n\
2079 At any time gdb identifies one frame as the \"selected\" frame.\n\
2080 Variable lookups are done with respect to the selected frame.\n\
2081 When the program being debugged stops, gdb selects the innermost frame.\n\
2082 The commands below can be used to select other frames by number or address.",
2083 &cmdlist);
2084 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
2085
2086 add_com ("pwd", class_files, pwd_command,
2087 "Print working directory. This is used for your program as well.");
2088 add_com ("cd", class_files, cd_command,
2089 "Set working directory to DIR for debugger and program being debugged.\n\
2090 The change does not take effect for the program being debugged\n\
2091 until the next time it is started.");
2092
2093 add_show_from_set
2094 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
2095 "Set gdb's prompt",
2096 &setlist),
2097 &showlist);
2098
2099 add_com ("echo", class_support, echo_command,
2100 "Print a constant string. Give string as argument.\n\
2101 C escape sequences may be used in the argument.\n\
2102 No newline is added at the end of the argument;\n\
2103 use \"\\n\" if you want a newline to be printed.\n\
2104 Since leading and trailing whitespace are ignored in command arguments,\n\
2105 if you want to print some you must use \"\\\" before leading whitespace\n\
2106 to be printed or after trailing whitespace.");
2107 add_com ("document", class_support, document_command,
2108 "Document a user-defined command.\n\
2109 Give command name as argument. Give documentation on following lines.\n\
2110 End with a line of just \"end\".");
2111 add_com ("define", class_support, define_command,
2112 "Define a new command name. Command name is argument.\n\
2113 Definition appears on following lines, one command per line.\n\
2114 End with a line of just \"end\".\n\
2115 Use the \"document\" command to give documentation for the new command.\n\
2116 Commands defined in this way do not take arguments.");
2117
2118 #ifdef __STDC__
2119 add_com ("source", class_support, source_command,
2120 "Read commands from a file named FILE.\n\
2121 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
2122 when gdb is started.");
2123 #else
2124 /* Punt file name, we can't help it easily. */
2125 add_com ("source", class_support, source_command,
2126 "Read commands from a file named FILE.\n\
2127 Note that the file \".gdbinit\" is read automatically in this way\n\
2128 when gdb is started.");
2129 #endif
2130
2131 add_com ("quit", class_support, quit_command, "Exit gdb.");
2132 add_com ("help", class_support, help_command, "Print list of commands.");
2133 add_com_alias ("q", "quit", class_support, 1);
2134 add_com_alias ("h", "help", class_support, 1);
2135
2136
2137 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
2138 "Set ",
2139 &setlist),
2140 add_show_from_set (c, &showlist);
2141 c->function = set_verbose;
2142 set_verbose (NULL, 0, c);
2143
2144 add_com ("dump-me", class_obscure, dump_me_command,
2145 "Get fatal error; make debugger dump its core.");
2146
2147 add_show_from_set
2148 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
2149 "Set editing of command lines as they are typed.\n\
2150 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2151 Without an argument, command line editing is enabled. To edit, use\n\
2152 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
2153 &showlist);
2154
2155 add_prefix_cmd ("history", class_support, set_history,
2156 "Generic command for setting command history parameters.",
2157 &sethistlist, "set history ", 0, &setlist);
2158 add_prefix_cmd ("history", class_support, show_history,
2159 "Generic command for showing command history parameters.",
2160 &showhistlist, "show history ", 0, &showlist);
2161
2162 add_show_from_set
2163 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
2164 "Set history expansion on command input.\n\
2165 Without an argument, history expansion is enabled.", &sethistlist),
2166 &showhistlist);
2167
2168 add_show_from_set
2169 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
2170 "Set saving of the history record on exit.\n\
2171 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2172 Without an argument, saving is enabled.", &sethistlist),
2173 &showhistlist);
2174
2175 c = add_set_cmd ("size", no_class, var_uinteger, (char *)&history_size,
2176 "Set the size of the command history, \n\
2177 ie. the number of previous commands to keep a record of.", &sethistlist);
2178 add_show_from_set (c, &showhistlist);
2179 c->function = set_history_size_command;
2180
2181 add_show_from_set
2182 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
2183 "Set the filename in which to record the command history\n\
2184 (the list of previous commands of which a record is kept).", &sethistlist),
2185 &showhistlist);
2186
2187 add_show_from_set
2188 (add_set_cmd ("confirm", class_support, var_boolean,
2189 (char *)&caution,
2190 "Set whether to confirm potentially dangerous operations.",
2191 &setlist),
2192 &showlist);
2193
2194 add_prefix_cmd ("info", class_info, info_command,
2195 "Generic command for showing things about the program being debugged.",
2196 &infolist, "info ", 0, &cmdlist);
2197 add_com_alias ("i", "info", class_info, 1);
2198
2199 add_prefix_cmd ("show", class_info, show_command,
2200 "Generic command for showing things about the debugger.",
2201 &showlist, "show ", 0, &cmdlist);
2202 /* Another way to get at the same thing. */
2203 add_info ("set", show_command, "Show all GDB settings.");
2204
2205 add_cmd ("commands", no_class, show_commands,
2206 "Show the the history of commands you typed.\n\
2207 You can supply a command number to start with, or a `+' to start after\n\
2208 the previous command number shown.",
2209 &showlist);
2210
2211 add_cmd ("version", no_class, show_version,
2212 "Show what version of GDB this is.", &showlist);
2213 }
This page took 0.071644 seconds and 3 git commands to generate.