Passdown -S to linker.
[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, 1993, 1994
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include "gdbcmd.h"
23 #include "call-cmds.h"
24 #include "symtab.h"
25 #include "inferior.h"
26 #include "signals.h"
27 #include "target.h"
28 #include "breakpoint.h"
29 #include "gdbtypes.h"
30 #include "expression.h"
31 #include "language.h"
32 #include "terminal.h" /* For job_control. */
33 #include "annotate.h"
34
35 #include "getopt.h"
36
37 /* readline include files */
38 #include "readline.h"
39 #include "history.h"
40
41 /* readline defines this. */
42 #undef savestring
43
44 #include <sys/types.h>
45 #ifdef USG
46 /* What is this for? X_OK? */
47 #include <unistd.h>
48 #endif
49
50 #include <string.h>
51 #ifndef NO_SYS_FILE
52 #include <sys/file.h>
53 #endif
54 #include <setjmp.h>
55 #include <sys/param.h>
56 #include <sys/stat.h>
57 #include <ctype.h>
58
59 #ifdef SET_STACK_LIMIT_HUGE
60 #include <sys/time.h>
61 #include <sys/resource.h>
62
63 int original_stack_limit;
64 #endif
65
66 /* Prototypes for local functions */
67
68 static char *
69 symbol_completion_function PARAMS ((char *, int));
70
71 static void
72 command_loop PARAMS ((void));
73
74 static void
75 command_loop_marker PARAMS ((int));
76
77 static void
78 print_gdb_version PARAMS ((GDB_FILE *));
79
80 static void
81 quit_command PARAMS ((char *, int));
82
83 static void
84 init_main PARAMS ((void));
85
86 static void
87 init_history PARAMS ((void));
88
89 static void
90 init_cmd_lists PARAMS ((void));
91
92 static void
93 float_handler PARAMS ((int));
94
95 static void
96 source_command PARAMS ((char *, int));
97
98 static void cd_command PARAMS ((char *, int));
99
100 static void
101 print_gnu_advertisement PARAMS ((void));
102
103 static void
104 init_signals PARAMS ((void));
105
106 static void
107 read_command_file PARAMS ((FILE *));
108
109 static void
110 set_verbose PARAMS ((char *, int, struct cmd_list_element *));
111
112 static void
113 show_history PARAMS ((char *, int));
114
115 static void
116 set_history PARAMS ((char *, int));
117
118 static void
119 set_history_size_command PARAMS ((char *, int, struct cmd_list_element *));
120
121 static void
122 show_commands PARAMS ((char *, int));
123
124 static void
125 echo_command PARAMS ((char *, int));
126
127 static void
128 pwd_command PARAMS ((char *, int));
129
130 static void
131 show_version PARAMS ((char *, int));
132
133 static void
134 document_command PARAMS ((char *, int));
135
136 static void
137 define_command PARAMS ((char *, int));
138
139 static void
140 validate_comname PARAMS ((char *));
141
142 static void
143 help_command PARAMS ((char *, int));
144
145 static void
146 show_command PARAMS ((char *, int));
147
148 static void
149 info_command PARAMS ((char *, int));
150
151 static void
152 complete_command PARAMS ((char *, int));
153
154 static void
155 do_nothing PARAMS ((int));
156
157 static int
158 quit_cover PARAMS ((char *));
159
160 static void
161 disconnect PARAMS ((int));
162
163 static void
164 source_cleanup PARAMS ((FILE *));
165
166 /* If this definition isn't overridden by the header files, assume
167 that isatty and fileno exist on this system. */
168 #ifndef ISATTY
169 #define ISATTY(FP) (isatty (fileno (FP)))
170 #endif
171
172 /* Initialization file name for gdb. This is overridden in some configs. */
173
174 #ifndef GDBINIT_FILENAME
175 #define GDBINIT_FILENAME ".gdbinit"
176 #endif
177 static char gdbinit[] = GDBINIT_FILENAME;
178 static int inhibit_gdbinit = 0;
179
180 #define ALL_CLEANUPS ((struct cleanup *)0)
181
182 /* Version number of GDB, as a string. */
183
184 extern char *version;
185
186 /* Canonical host name as a string. */
187
188 extern char *host_name;
189
190 /* Canonical target name as a string. */
191
192 extern char *target_name;
193
194 extern char lang_frame_mismatch_warn[]; /* language.c */
195
196 /* Flag for whether we want all the "from_tty" gubbish printed. */
197
198 int caution = 1; /* Default is yes, sigh. */
199
200 /*
201 * Define all cmd_list_element's
202 */
203
204 /* Chain containing all defined commands. */
205
206 struct cmd_list_element *cmdlist;
207
208 /* Chain containing all defined info subcommands. */
209
210 struct cmd_list_element *infolist;
211
212 /* Chain containing all defined enable subcommands. */
213
214 struct cmd_list_element *enablelist;
215
216 /* Chain containing all defined disable subcommands. */
217
218 struct cmd_list_element *disablelist;
219
220 /* Chain containing all defined delete subcommands. */
221
222 struct cmd_list_element *deletelist;
223
224 /* Chain containing all defined "enable breakpoint" subcommands. */
225
226 struct cmd_list_element *enablebreaklist;
227
228 /* Chain containing all defined set subcommands */
229
230 struct cmd_list_element *setlist;
231
232 /* Chain containing all defined unset subcommands */
233
234 struct cmd_list_element *unsetlist;
235
236 /* Chain containing all defined show subcommands. */
237
238 struct cmd_list_element *showlist;
239
240 /* Chain containing all defined \"set history\". */
241
242 struct cmd_list_element *sethistlist;
243
244 /* Chain containing all defined \"show history\". */
245
246 struct cmd_list_element *showhistlist;
247
248 /* Chain containing all defined \"unset history\". */
249
250 struct cmd_list_element *unsethistlist;
251
252 /* Chain containing all defined maintenance subcommands. */
253
254 #if MAINTENANCE_CMDS
255 struct cmd_list_element *maintenancelist;
256 #endif
257
258 /* Chain containing all defined "maintenance info" subcommands. */
259
260 #if MAINTENANCE_CMDS
261 struct cmd_list_element *maintenanceinfolist;
262 #endif
263
264 /* Chain containing all defined "maintenance print" subcommands. */
265
266 #if MAINTENANCE_CMDS
267 struct cmd_list_element *maintenanceprintlist;
268 #endif
269
270 struct cmd_list_element *setprintlist;
271
272 struct cmd_list_element *showprintlist;
273
274 struct cmd_list_element *setchecklist;
275
276 struct cmd_list_element *showchecklist;
277
278 /* stdio stream that command input is being read from. Set to stdin normally.
279 Set by source_command to the file we are sourcing. Set to NULL if we are
280 executing a user-defined command. */
281
282 FILE *instream;
283
284 /* Current working directory. */
285
286 char *current_directory;
287
288 /* The directory name is actually stored here (usually). */
289 static char dirbuf[1024];
290
291 /* Function to call before reading a command, if nonzero.
292 The function receives two args: an input stream,
293 and a prompt string. */
294
295 void (*window_hook) PARAMS ((FILE *, char *));
296
297 extern int mapped_symbol_files;
298 extern int readnow_symbol_files;
299
300 int epoch_interface;
301 int xgdb_verbose;
302
303 /* gdb prints this when reading a command interactively */
304 static char *prompt;
305
306 /* Buffer used for reading command lines, and the size
307 allocated for it so far. */
308
309 char *line;
310 int linesize = 100;
311
312 /* Nonzero if the current command is modified by "server ". This
313 affects things like recording into the command history, comamnds
314 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
315 whatever) can issue its own commands and also send along commands
316 from the user, and have the user not notice that the user interface
317 is issuing commands too. */
318 int server_command;
319
320 /* Baud rate specified for talking to serial target systems. Default
321 is left as -1, so targets can choose their own defaults. */
322 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
323 or (unsigned int)-1. This is a Bad User Interface. */
324
325 int baud_rate = -1;
326
327 /* Non-zero tells remote* modules to output debugging info. */
328
329 int remote_debug = 0;
330
331 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
332
333 #ifndef STOP_SIGNAL
334 #ifdef SIGTSTP
335 #define STOP_SIGNAL SIGTSTP
336 static void stop_sig PARAMS ((int));
337 #endif
338 #endif
339
340 /* Some System V have job control but not sigsetmask(). */
341 #if !defined (HAVE_SIGSETMASK)
342 #if !defined (USG)
343 #define HAVE_SIGSETMASK 1
344 #else
345 #define HAVE_SIGSETMASK 0
346 #endif
347 #endif
348
349 #if 0 == (HAVE_SIGSETMASK)
350 #define sigsetmask(n)
351 #endif
352 \f
353 /* Where to go for return_to_top_level (RETURN_ERROR). */
354 static jmp_buf error_return;
355 /* Where to go for return_to_top_level (RETURN_QUIT). */
356 static jmp_buf quit_return;
357
358 /* Temporary variable for SET_TOP_LEVEL. */
359 static int top_level_val;
360
361 /* Do a setjmp on error_return and quit_return. catch_errors is
362 generally a cleaner way to do this, but main() would look pretty
363 ugly if it had to use catch_errors each time. */
364
365 #define SET_TOP_LEVEL() \
366 (((top_level_val = setjmp (error_return)) \
367 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \
368 , top_level_val)
369
370 /* Return for reason REASON. This generally gets back to the command
371 loop, but can be caught via catch_errors. */
372
373 NORETURN void
374 return_to_top_level (reason)
375 enum return_reason reason;
376 {
377 quit_flag = 0;
378 immediate_quit = 0;
379
380 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
381 I can think of a reason why that is vital, though). */
382 bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */
383
384 disable_current_display ();
385 do_cleanups (ALL_CLEANUPS);
386
387 if (annotation_level > 1)
388 switch (reason)
389 {
390 case RETURN_QUIT:
391 annotate_quit ();
392 break;
393 case RETURN_ERROR:
394 annotate_error ();
395 break;
396 }
397
398 (NORETURN void) longjmp
399 (reason == RETURN_ERROR ? error_return : quit_return, 1);
400 }
401
402 /* Call FUNC with arg ARGS, catching any errors. If there is no
403 error, return the value returned by FUNC. If there is an error,
404 print ERRSTRING, print the specific error message, then return
405 zero.
406
407 Must not be called with immediate_quit in effect (bad things might
408 happen, say we got a signal in the middle of a memcpy to quit_return).
409 This is an OK restriction; with very few exceptions immediate_quit can
410 be replaced by judicious use of QUIT.
411
412 MASK specifies what to catch; it is normally set to
413 RETURN_MASK_ALL, if for no other reason than that the code which
414 calls catch_errors might not be set up to deal with a quit which
415 isn't caught. But if the code can deal with it, it generally
416 should be RETURN_MASK_ERROR, unless for some reason it is more
417 useful to abort only the portion of the operation inside the
418 catch_errors. Note that quit should return to the command line
419 fairly quickly, even if some further processing is being done. */
420
421 int
422 catch_errors (func, args, errstring, mask)
423 int (*func) PARAMS ((char *));
424 PTR args;
425 char *errstring;
426 return_mask mask;
427 {
428 jmp_buf saved_error;
429 jmp_buf saved_quit;
430 jmp_buf tmp_jmp;
431 int val;
432 struct cleanup *saved_cleanup_chain;
433 char *saved_error_pre_print;
434
435 saved_cleanup_chain = save_cleanups ();
436 saved_error_pre_print = error_pre_print;
437
438 if (mask & RETURN_MASK_ERROR)
439 memcpy ((char *)saved_error, (char *)error_return, sizeof (jmp_buf));
440 if (mask & RETURN_MASK_QUIT)
441 memcpy (saved_quit, quit_return, sizeof (jmp_buf));
442 error_pre_print = errstring;
443
444 if (setjmp (tmp_jmp) == 0)
445 {
446 if (mask & RETURN_MASK_ERROR)
447 memcpy (error_return, tmp_jmp, sizeof (jmp_buf));
448 if (mask & RETURN_MASK_QUIT)
449 memcpy (quit_return, tmp_jmp, sizeof (jmp_buf));
450 val = (*func) (args);
451 }
452 else
453 val = 0;
454
455 restore_cleanups (saved_cleanup_chain);
456
457 error_pre_print = saved_error_pre_print;
458 if (mask & RETURN_MASK_ERROR)
459 memcpy (error_return, saved_error, sizeof (jmp_buf));
460 if (mask & RETURN_MASK_QUIT)
461 memcpy (quit_return, saved_quit, sizeof (jmp_buf));
462 return val;
463 }
464
465 /* Handler for SIGHUP. */
466
467 static void
468 disconnect (signo)
469 int signo;
470 {
471 catch_errors (quit_cover, NULL,
472 "Could not kill the program being debugged", RETURN_MASK_ALL);
473 signal (SIGHUP, SIG_DFL);
474 kill (getpid (), SIGHUP);
475 }
476
477 /* Just a little helper function for disconnect(). */
478
479 static int
480 quit_cover (s)
481 char *s;
482 {
483 caution = 0; /* Throw caution to the wind -- we're exiting.
484 This prevents asking the user dumb questions. */
485 quit_command((char *)0, 0);
486 return 0;
487 }
488 \f
489 /* Line number we are currently in in a file which is being sourced. */
490 static int source_line_number;
491
492 /* Name of the file we are sourcing. */
493 static char *source_file_name;
494
495 /* Buffer containing the error_pre_print used by the source stuff.
496 Malloc'd. */
497 static char *source_error;
498 static int source_error_allocated;
499
500 /* Something to glom on to the start of error_pre_print if source_file_name
501 is set. */
502 static char *source_pre_error;
503
504 /* Clean up on error during a "source" command (or execution of a
505 user-defined command). */
506
507 static void
508 source_cleanup (stream)
509 FILE *stream;
510 {
511 /* Restore the previous input stream. */
512 instream = stream;
513 }
514
515 /* Read commands from STREAM. */
516 static void
517 read_command_file (stream)
518 FILE *stream;
519 {
520 struct cleanup *cleanups;
521
522 cleanups = make_cleanup (source_cleanup, instream);
523 instream = stream;
524 command_loop ();
525 do_cleanups (cleanups);
526 }
527 \f
528 int
529 main (argc, argv)
530 int argc;
531 char **argv;
532 {
533 int count;
534 static int quiet = 0;
535 static int batch = 0;
536
537 /* Pointers to various arguments from command line. */
538 char *symarg = NULL;
539 char *execarg = NULL;
540 char *corearg = NULL;
541 char *cdarg = NULL;
542 char *ttyarg = NULL;
543
544 /* These are static so that we can take their address in an initializer. */
545 static int print_help;
546 static int print_version;
547
548 /* Pointers to all arguments of --command option. */
549 char **cmdarg;
550 /* Allocated size of cmdarg. */
551 int cmdsize;
552 /* Number of elements of cmdarg used. */
553 int ncmd;
554
555 /* Indices of all arguments of --directory option. */
556 char **dirarg;
557 /* Allocated size. */
558 int dirsize;
559 /* Number of elements used. */
560 int ndir;
561
562 struct stat homebuf, cwdbuf;
563 char *homedir, *homeinit;
564
565 register int i;
566
567 /* start-sanitize-mpw */
568 #ifdef MPW
569 /* Drop into MacsBug, but only if the executable is specially named. */
570 if (strcmp(argv[0], "DEBUGGDB") == 0)
571 DebugStr("\pat start of GDB main");
572
573 if (StandAlone)
574 mac_app = mac_init ();
575 #endif /* MPW */
576 /* end-sanitize-mpw */
577 /* This needs to happen before the first use of malloc. */
578 init_malloc ((PTR) NULL);
579
580 #if defined (ALIGN_STACK_ON_STARTUP)
581 i = (int) &count & 0x3;
582 if (i != 0)
583 alloca (4 - i);
584 #endif
585
586 /* If error() is called from initialization code, just exit */
587 if (SET_TOP_LEVEL ()) {
588 exit(1);
589 }
590
591 cmdsize = 1;
592 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
593 ncmd = 0;
594 dirsize = 1;
595 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
596 ndir = 0;
597
598 quit_flag = 0;
599 line = (char *) xmalloc (linesize);
600 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
601 instream = stdin;
602
603 getcwd (dirbuf, sizeof (dirbuf));
604 current_directory = dirbuf;
605
606 #ifdef SET_STACK_LIMIT_HUGE
607 {
608 struct rlimit rlim;
609
610 /* Set the stack limit huge so that alloca (particularly stringtab
611 * in dbxread.c) does not fail. */
612 getrlimit (RLIMIT_STACK, &rlim);
613 original_stack_limit = rlim.rlim_cur;
614 rlim.rlim_cur = rlim.rlim_max;
615 setrlimit (RLIMIT_STACK, &rlim);
616 }
617 #endif /* SET_STACK_LIMIT_HUGE */
618
619 /* Parse arguments and options. */
620 {
621 int c;
622 /* When var field is 0, use flag field to record the equivalent
623 short option (or arbitrary numbers starting at 10 for those
624 with no equivalent). */
625 static struct option long_options[] =
626 {
627 {"readnow", no_argument, &readnow_symbol_files, 1},
628 {"r", no_argument, &readnow_symbol_files, 1},
629 {"mapped", no_argument, &mapped_symbol_files, 1},
630 {"m", no_argument, &mapped_symbol_files, 1},
631 {"quiet", no_argument, &quiet, 1},
632 {"q", no_argument, &quiet, 1},
633 {"silent", no_argument, &quiet, 1},
634 {"nx", no_argument, &inhibit_gdbinit, 1},
635 {"n", no_argument, &inhibit_gdbinit, 1},
636 {"batch", no_argument, &batch, 1},
637 {"epoch", no_argument, &epoch_interface, 1},
638
639 /* This is a synonym for "--annotate=1". --annotate is now preferred,
640 but keep this here for a long time because people will be running
641 emacses which use --fullname. */
642 {"fullname", no_argument, 0, 'f'},
643 {"f", no_argument, 0, 'f'},
644
645 {"annotate", required_argument, 0, 12},
646 {"help", no_argument, &print_help, 1},
647 {"se", required_argument, 0, 10},
648 {"symbols", required_argument, 0, 's'},
649 {"s", required_argument, 0, 's'},
650 {"exec", required_argument, 0, 'e'},
651 {"e", required_argument, 0, 'e'},
652 {"core", required_argument, 0, 'c'},
653 {"c", required_argument, 0, 'c'},
654 {"command", required_argument, 0, 'x'},
655 {"version", no_argument, &print_version, 1},
656 {"x", required_argument, 0, 'x'},
657 {"directory", required_argument, 0, 'd'},
658 {"cd", required_argument, 0, 11},
659 {"tty", required_argument, 0, 't'},
660 {"baud", required_argument, 0, 'b'},
661 {"b", required_argument, 0, 'b'},
662 /* Allow machine descriptions to add more options... */
663 #ifdef ADDITIONAL_OPTIONS
664 ADDITIONAL_OPTIONS
665 #endif
666 {0, no_argument, 0, 0},
667 };
668
669 while (1)
670 {
671 int option_index;
672
673 c = getopt_long_only (argc, argv, "",
674 long_options, &option_index);
675 if (c == EOF)
676 break;
677
678 /* Long option that takes an argument. */
679 if (c == 0 && long_options[option_index].flag == 0)
680 c = long_options[option_index].val;
681
682 switch (c)
683 {
684 case 0:
685 /* Long option that just sets a flag. */
686 break;
687 case 10:
688 symarg = optarg;
689 execarg = optarg;
690 break;
691 case 11:
692 cdarg = optarg;
693 break;
694 case 12:
695 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
696 annotation_level = atoi (optarg);
697 break;
698 case 'f':
699 annotation_level = 1;
700 break;
701 case 's':
702 symarg = optarg;
703 break;
704 case 'e':
705 execarg = optarg;
706 break;
707 case 'c':
708 corearg = optarg;
709 break;
710 case 'x':
711 cmdarg[ncmd++] = optarg;
712 if (ncmd >= cmdsize)
713 {
714 cmdsize *= 2;
715 cmdarg = (char **) xrealloc ((char *)cmdarg,
716 cmdsize * sizeof (*cmdarg));
717 }
718 break;
719 case 'd':
720 dirarg[ndir++] = optarg;
721 if (ndir >= dirsize)
722 {
723 dirsize *= 2;
724 dirarg = (char **) xrealloc ((char *)dirarg,
725 dirsize * sizeof (*dirarg));
726 }
727 break;
728 case 't':
729 ttyarg = optarg;
730 break;
731 case 'q':
732 quiet = 1;
733 break;
734 case 'b':
735 {
736 int i;
737 char *p;
738
739 i = strtol (optarg, &p, 0);
740 if (i == 0 && p == optarg)
741
742 /* Don't use *_filtered or warning() (which relies on
743 current_target) until after initialize_all_files(). */
744
745 fprintf_unfiltered
746 (gdb_stderr,
747 "warning: could not set baud rate to `%s'.\n", optarg);
748 else
749 baud_rate = i;
750 }
751 break;
752
753 #ifdef ADDITIONAL_OPTION_CASES
754 ADDITIONAL_OPTION_CASES
755 #endif
756 case '?':
757 fprintf_unfiltered (gdb_stderr,
758 "Use `%s --help' for a complete list of options.\n",
759 argv[0]);
760 exit (1);
761 }
762 }
763
764 /* OK, that's all the options. The other arguments are filenames. */
765 count = 0;
766 for (; optind < argc; optind++)
767 switch (++count)
768 {
769 case 1:
770 symarg = argv[optind];
771 execarg = argv[optind];
772 break;
773 case 2:
774 corearg = argv[optind];
775 break;
776 case 3:
777 fprintf_unfiltered (gdb_stderr,
778 "Excess command line arguments ignored. (%s%s)\n",
779 argv[optind], (optind == argc - 1) ? "" : " ...");
780 break;
781 }
782 if (batch)
783 quiet = 1;
784 }
785
786 /* Run the init function of each source file */
787
788 init_cmd_lists (); /* This needs to be done first */
789 initialize_all_files ();
790 init_main (); /* But that omits this file! Do it now */
791 init_signals ();
792
793 /* Do these (and anything which might call wrap_here or *_filtered)
794 after initialize_all_files. */
795 if (print_version)
796 {
797 print_gdb_version (gdb_stdout);
798 wrap_here ("");
799 printf_filtered ("\n");
800 exit (0);
801 }
802
803 if (print_help)
804 {
805 /* --version is intentionally not documented here, because we
806 are printing the version here, and the help is long enough
807 already. */
808
809 print_gdb_version (gdb_stdout);
810 /* Make sure the output gets printed. */
811 wrap_here ("");
812 printf_filtered ("\n");
813
814 /* But don't use *_filtered here. We don't want to prompt for continue
815 no matter how small the screen or how much we're going to print. */
816 /* start-sanitize-mpw */
817 /* For reasons too ugly to describe... */
818 #ifdef MPW_C
819 fputs_unfiltered ("This is the GNU debugger.\n", gdb_stdout);
820 #else
821 /* end-sanitize-mpw */
822 fputs_unfiltered ("\
823 This is the GNU debugger. Usage:\n\
824 gdb [options] [executable-file [core-file or process-id]]\n\
825 Options:\n\
826 --help Print this message.\n\
827 --quiet Do not print version number on startup.\n\
828 --fullname Output information used by emacs-GDB interface.\n\
829 --epoch Output information used by epoch emacs-GDB interface.\n\
830 --batch Exit after processing options.\n\
831 --nx Do not read .gdbinit file.\n\
832 --tty=TTY Use TTY for input/output by the program being debugged.\n\
833 --cd=DIR Change current directory to DIR.\n\
834 --directory=DIR Search for source files in DIR.\n\
835 --command=FILE Execute GDB commands from FILE.\n\
836 --symbols=SYMFILE Read symbols from SYMFILE.\n\
837 --exec=EXECFILE Use EXECFILE as the executable.\n\
838 --se=FILE Use FILE as symbol file and executable file.\n\
839 --core=COREFILE Analyze the core dump COREFILE.\n\
840 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
841 --mapped Use mapped symbol files if supported on this system.\n\
842 --readnow Fully read symbol files on first access.\n\
843 ", gdb_stdout);
844 /* start-sanitize-mpw */
845 #endif /* MPW_C */
846 /* end-sanitize-mpw */
847 #ifdef ADDITIONAL_OPTION_HELP
848 fputs_unfiltered (ADDITIONAL_OPTION_HELP, gdb_stdout);
849 #endif
850 fputs_unfiltered ("\n\
851 For more information, type \"help\" from within GDB, or consult the\n\
852 GDB manual (available as on-line info or a printed manual).\n", gdb_stdout);
853 exit (0);
854 }
855
856 if (!quiet)
857 {
858 /* Print all the junk at the top, with trailing "..." if we are about
859 to read a symbol file (possibly slowly). */
860 print_gnu_advertisement ();
861 print_gdb_version (gdb_stdout);
862 if (symarg)
863 printf_filtered ("..");
864 wrap_here("");
865 gdb_flush (gdb_stdout); /* Force to screen during slow operations */
866 }
867
868 error_pre_print = "\n\n";
869 /* We may get more than one warning, don't double space all of them... */
870 warning_pre_print = "\nwarning: ";
871
872 /* We need a default language for parsing expressions, so simple things like
873 "set width 0" won't fail if no language is explicitly set in a config file
874 or implicitly set by reading an executable during startup. */
875 set_language (language_c);
876 expected_language = current_language; /* don't warn about the change. */
877
878 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
879 *before* all the command line arguments are processed; it sets
880 global parameters, which are independent of what file you are
881 debugging or what directory you are in. */
882 homedir = getenv ("HOME");
883 if (homedir)
884 {
885 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
886 strlen (gdbinit) + 10);
887 strcpy (homeinit, getenv ("HOME"));
888 strcat (homeinit, "/");
889 strcat (homeinit, gdbinit);
890 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
891 {
892 if (!SET_TOP_LEVEL ())
893 source_command (homeinit, 0);
894 }
895 do_cleanups (ALL_CLEANUPS);
896
897 /* Do stats; no need to do them elsewhere since we'll only
898 need them if homedir is set. Make sure that they are
899 zero in case one of them fails (this guarantees that they
900 won't match if either exists). */
901
902 memset (&homebuf, 0, sizeof (struct stat));
903 memset (&cwdbuf, 0, sizeof (struct stat));
904
905 stat (homeinit, &homebuf);
906 stat (gdbinit, &cwdbuf); /* We'll only need this if
907 homedir was set. */
908 }
909
910 /* Now perform all the actions indicated by the arguments. */
911 if (cdarg != NULL)
912 {
913 if (!SET_TOP_LEVEL ())
914 {
915 cd_command (cdarg, 0);
916 }
917 }
918 do_cleanups (ALL_CLEANUPS);
919
920 for (i = 0; i < ndir; i++)
921 if (!SET_TOP_LEVEL ())
922 directory_command (dirarg[i], 0);
923 free ((PTR)dirarg);
924 do_cleanups (ALL_CLEANUPS);
925
926 if (execarg != NULL
927 && symarg != NULL
928 && STREQ (execarg, symarg))
929 {
930 /* The exec file and the symbol-file are the same. If we can't open
931 it, better only print one error message. */
932 if (!SET_TOP_LEVEL ())
933 {
934 exec_file_command (execarg, !batch);
935 symbol_file_command (symarg, 0);
936 }
937 }
938 else
939 {
940 if (execarg != NULL)
941 if (!SET_TOP_LEVEL ())
942 exec_file_command (execarg, !batch);
943 if (symarg != NULL)
944 if (!SET_TOP_LEVEL ())
945 symbol_file_command (symarg, 0);
946 }
947 do_cleanups (ALL_CLEANUPS);
948
949 /* After the symbol file has been read, print a newline to get us
950 beyond the copyright line... But errors should still set off
951 the error message with a (single) blank line. */
952 if (!quiet)
953 printf_filtered ("\n");
954 error_pre_print = "\n";
955 warning_pre_print = "\nwarning: ";
956
957 if (corearg != NULL)
958 if (!SET_TOP_LEVEL ())
959 core_file_command (corearg, !batch);
960 else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ())
961 attach_command (corearg, !batch);
962 do_cleanups (ALL_CLEANUPS);
963
964 if (ttyarg != NULL)
965 if (!SET_TOP_LEVEL ())
966 tty_command (ttyarg, !batch);
967 do_cleanups (ALL_CLEANUPS);
968
969 #ifdef ADDITIONAL_OPTION_HANDLER
970 ADDITIONAL_OPTION_HANDLER;
971 #endif
972
973 /* Error messages should no longer be distinguished with extra output. */
974 error_pre_print = 0;
975 warning_pre_print = "warning: ";
976
977 /* Read the .gdbinit file in the current directory, *if* it isn't
978 the same as the $HOME/.gdbinit file (it should exist, also). */
979
980 if (!homedir
981 || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
982 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
983 {
984 if (!SET_TOP_LEVEL ())
985 source_command (gdbinit, 0);
986 }
987 do_cleanups (ALL_CLEANUPS);
988
989 for (i = 0; i < ncmd; i++)
990 {
991 if (!SET_TOP_LEVEL ())
992 {
993 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
994 read_command_file (stdin);
995 else
996 source_command (cmdarg[i], !batch);
997 do_cleanups (ALL_CLEANUPS);
998 }
999 }
1000 free ((PTR)cmdarg);
1001
1002 /* Read in the old history after all the command files have been read. */
1003 init_history();
1004
1005 if (batch)
1006 {
1007 /* We have hit the end of the batch file. */
1008 exit (0);
1009 }
1010
1011 /* Do any host- or target-specific hacks. This is used for i960 targets
1012 to force the user to set a nindy target and spec its parameters. */
1013
1014 #ifdef BEFORE_MAIN_LOOP_HOOK
1015 BEFORE_MAIN_LOOP_HOOK;
1016 #endif
1017
1018 /* The command loop. */
1019
1020 while (1)
1021 {
1022 if (!SET_TOP_LEVEL ())
1023 {
1024 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
1025 /* start-sanitize-mpw */
1026 #ifdef MPW
1027 /* If we're being a Mac application, go into a Mac-specific
1028 event-handling loop instead. We still want to be inside
1029 the outer loop, because that will catch longjmps resulting
1030 from some command executions. */
1031 if (mac_app)
1032 mac_command_loop ();
1033 else
1034 #endif /* MPW */
1035 /* start-sanitize-mpw */
1036 command_loop ();
1037 quit_command ((char *)0, instream == stdin);
1038 }
1039 }
1040 /* No exit -- exit is through quit_command. */
1041 }
1042
1043 void
1044 execute_user_command (c, args)
1045 struct cmd_list_element *c;
1046 char *args;
1047 {
1048 register struct command_line *cmdlines;
1049 struct cleanup *old_chain;
1050
1051 if (args)
1052 error ("User-defined commands cannot take arguments.");
1053
1054 cmdlines = c->user_commands;
1055 if (cmdlines == 0)
1056 /* Null command */
1057 return;
1058
1059 /* Set the instream to 0, indicating execution of a
1060 user-defined function. */
1061 old_chain = make_cleanup (source_cleanup, instream);
1062 instream = (FILE *) 0;
1063 while (cmdlines)
1064 {
1065 execute_command (cmdlines->line, 0);
1066 cmdlines = cmdlines->next;
1067 }
1068 do_cleanups (old_chain);
1069 }
1070
1071 /* Execute the line P as a command.
1072 Pass FROM_TTY as second argument to the defining function. */
1073
1074 void
1075 execute_command (p, from_tty)
1076 char *p;
1077 int from_tty;
1078 {
1079 register struct cmd_list_element *c;
1080 register enum language flang;
1081 static int warned = 0;
1082
1083 free_all_values ();
1084
1085 /* This can happen when command_line_input hits end of file. */
1086 if (p == NULL)
1087 return;
1088
1089 while (*p == ' ' || *p == '\t') p++;
1090 if (*p)
1091 {
1092 char *arg;
1093
1094 c = lookup_cmd (&p, cmdlist, "", 0, 1);
1095 /* Pass null arg rather than an empty one. */
1096 arg = *p ? p : 0;
1097
1098 /* If this command has been hooked, run the hook first. */
1099 if (c->hook)
1100 execute_user_command (c->hook, (char *)0);
1101
1102 if (c->class == class_user)
1103 execute_user_command (c, arg);
1104 else if (c->type == set_cmd || c->type == show_cmd)
1105 do_setshow_command (arg, from_tty & caution, c);
1106 else if (c->function.cfunc == NO_FUNCTION)
1107 error ("That is not a command, just a help topic.");
1108 else
1109 (*c->function.cfunc) (arg, from_tty & caution);
1110 }
1111
1112 /* Tell the user if the language has changed (except first time). */
1113 if (current_language != expected_language)
1114 {
1115 if (language_mode == language_mode_auto) {
1116 language_info (1); /* Print what changed. */
1117 }
1118 warned = 0;
1119 }
1120
1121 /* Warn the user if the working language does not match the
1122 language of the current frame. Only warn the user if we are
1123 actually running the program, i.e. there is a stack. */
1124 /* FIXME: This should be cacheing the frame and only running when
1125 the frame changes. */
1126 if (target_has_stack)
1127 {
1128 flang = get_frame_language ();
1129 if (!warned
1130 && flang != language_unknown
1131 && flang != current_language->la_language)
1132 {
1133 printf_filtered ("%s\n", lang_frame_mismatch_warn);
1134 warned = 1;
1135 }
1136 }
1137 }
1138
1139 /* ARGSUSED */
1140 static void
1141 command_loop_marker (foo)
1142 int foo;
1143 {
1144 }
1145
1146 /* Read commands from `instream' and execute them
1147 until end of file or error reading instream. */
1148 static void
1149 command_loop ()
1150 {
1151 struct cleanup *old_chain;
1152 char *command;
1153 int stdin_is_tty = ISATTY (stdin);
1154
1155 while (!feof (instream))
1156 {
1157 if (window_hook && instream == stdin)
1158 (*window_hook) (instream, prompt);
1159
1160 quit_flag = 0;
1161 if (instream == stdin && stdin_is_tty)
1162 reinitialize_more_filter ();
1163 old_chain = make_cleanup (command_loop_marker, 0);
1164 command = command_line_input (instream == stdin ? prompt : (char *) NULL,
1165 instream == stdin, "prompt");
1166 if (command == 0)
1167 return;
1168 execute_command (command, instream == stdin);
1169 /* Do any commands attached to breakpoint we stopped at. */
1170 bpstat_do_actions (&stop_bpstat);
1171 do_cleanups (old_chain);
1172 }
1173 }
1174 \f
1175 /* Commands call this if they do not want to be repeated by null lines. */
1176
1177 void
1178 dont_repeat ()
1179 {
1180 if (server_command)
1181 return;
1182
1183 /* If we aren't reading from standard input, we are saving the last
1184 thing read from stdin in line and don't want to delete it. Null lines
1185 won't repeat here in any case. */
1186 if (instream == stdin)
1187 *line = 0;
1188 }
1189 \f
1190 /* Read a line from the stream "instream" without command line editing.
1191
1192 It prints PRROMPT once at the start.
1193 Action is compatible with "readline", e.g. space for the result is
1194 malloc'd and should be freed by the caller.
1195
1196 A NULL return means end of file. */
1197 char *
1198 gdb_readline (prrompt)
1199 char *prrompt;
1200 {
1201 int c;
1202 char *result;
1203 int input_index = 0;
1204 int result_size = 80;
1205
1206 if (prrompt)
1207 {
1208 /* Don't use a _filtered function here. It causes the assumed
1209 character position to be off, since the newline we read from
1210 the user is not accounted for. */
1211 fputs_unfiltered (prrompt, gdb_stdout);
1212 /* start-sanitize-mpw */
1213 #ifdef MPW
1214 /* Move to a new line so the entered line doesn't have a prompt
1215 on the front of it. */
1216 fputs_unfiltered ("\n", gdb_stdout);
1217 #endif /* MPW */
1218 /* end-sanitize-mpw */
1219 gdb_flush (gdb_stdout);
1220 }
1221
1222 result = (char *) xmalloc (result_size);
1223
1224 while (1)
1225 {
1226 /* Read from stdin if we are executing a user defined command.
1227 This is the right thing for prompt_for_continue, at least. */
1228 c = fgetc (instream ? instream : stdin);
1229
1230 if (c == EOF)
1231 {
1232 if (input_index > 0)
1233 /* The last line does not end with a newline. Return it, and
1234 if we are called again fgetc will still return EOF and
1235 we'll return NULL then. */
1236 break;
1237 free (result);
1238 return NULL;
1239 }
1240
1241 if (c == '\n')
1242 break;
1243
1244 result[input_index++] = c;
1245 while (input_index >= result_size)
1246 {
1247 result_size *= 2;
1248 result = (char *) xrealloc (result, result_size);
1249 }
1250 }
1251
1252 result[input_index++] = '\0';
1253 return result;
1254 }
1255
1256 /* Variables which control command line editing and history
1257 substitution. These variables are given default values at the end
1258 of this file. */
1259 static int command_editing_p;
1260 static int history_expansion_p;
1261 static int write_history_p;
1262 static int history_size;
1263 static char *history_filename;
1264
1265 /* readline uses the word breaks for two things:
1266 (1) In figuring out where to point the TEXT parameter to the
1267 rl_completion_entry_function. Since we don't use TEXT for much,
1268 it doesn't matter a lot what the word breaks are for this purpose, but
1269 it does affect how much stuff M-? lists.
1270 (2) If one of the matches contains a word break character, readline
1271 will quote it. That's why we switch between
1272 gdb_completer_word_break_characters and
1273 gdb_completer_command_word_break_characters. I'm not sure when
1274 we need this behavior (perhaps for funky characters in C++ symbols?). */
1275
1276 /* Variables which are necessary for fancy command line editing. */
1277 char *gdb_completer_word_break_characters =
1278 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1279
1280 /* When completing on command names, we remove '-' from the list of
1281 word break characters, since we use it in command names. If the
1282 readline library sees one in any of the current completion strings,
1283 it thinks that the string needs to be quoted and automatically supplies
1284 a leading quote. */
1285 char *gdb_completer_command_word_break_characters =
1286 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1287
1288 /* Characters that can be used to quote completion strings. Note that we
1289 can't include '"' because the gdb C parser treats such quoted sequences
1290 as strings. */
1291 char *gdb_completer_quote_characters =
1292 "'";
1293
1294 /* Functions that are used as part of the fancy command line editing. */
1295
1296 /* This can be used for functions which don't want to complete on symbols
1297 but don't want to complete on anything else either. */
1298 /* ARGSUSED */
1299 char **
1300 noop_completer (text, prefix)
1301 char *text;
1302 char *prefix;
1303 {
1304 return NULL;
1305 }
1306
1307 /* Complete on filenames. */
1308 char **
1309 filename_completer (text, word)
1310 char *text;
1311 char *word;
1312 {
1313 /* From readline. */
1314 extern char *filename_completion_function ();
1315 int subsequent_name;
1316 char **return_val;
1317 int return_val_used;
1318 int return_val_alloced;
1319
1320 return_val_used = 0;
1321 /* Small for testing. */
1322 return_val_alloced = 1;
1323 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1324
1325 subsequent_name = 0;
1326 while (1)
1327 {
1328 char *p;
1329 p = filename_completion_function (text, subsequent_name);
1330 if (return_val_used >= return_val_alloced)
1331 {
1332 return_val_alloced *= 2;
1333 return_val =
1334 (char **) xrealloc (return_val,
1335 return_val_alloced * sizeof (char *));
1336 }
1337 if (p == NULL)
1338 {
1339 return_val[return_val_used++] = p;
1340 break;
1341 }
1342 /* Like emacs, don't complete on old versions. Especially useful
1343 in the "source" command. */
1344 if (p[strlen (p) - 1] == '~')
1345 continue;
1346
1347 {
1348 char *q;
1349 if (word == text)
1350 /* Return exactly p. */
1351 return_val[return_val_used++] = p;
1352 else if (word > text)
1353 {
1354 /* Return some portion of p. */
1355 q = xmalloc (strlen (p) + 5);
1356 strcpy (q, p + (word - text));
1357 return_val[return_val_used++] = q;
1358 free (p);
1359 }
1360 else
1361 {
1362 /* Return some of TEXT plus p. */
1363 q = xmalloc (strlen (p) + (text - word) + 5);
1364 strncpy (q, word, text - word);
1365 q[text - word] = '\0';
1366 strcat (q, p);
1367 return_val[return_val_used++] = q;
1368 free (p);
1369 }
1370 }
1371 subsequent_name = 1;
1372 }
1373 #if 0
1374 /* There is no way to do this just long enough to affect quote inserting
1375 without also affecting the next completion. This should be fixed in
1376 readline. FIXME. */
1377 /* Insure that readline does the right thing
1378 with respect to inserting quotes. */
1379 rl_completer_word_break_characters = "";
1380 #endif
1381 return return_val;
1382 }
1383
1384 /* Here are some useful test cases for completion. FIXME: These should
1385 be put in the test suite. They should be tested with both M-? and TAB.
1386
1387 "show output-" "radix"
1388 "show output" "-radix"
1389 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1390 "p " ambiguous (all symbols)
1391 "info t foo" no completions
1392 "info t " no completions
1393 "info t" ambiguous ("info target", "info terminal", etc.)
1394 "info ajksdlfk" no completions
1395 "info ajksdlfk " no completions
1396 "info" " "
1397 "info " ambiguous (all info commands)
1398 "p \"a" no completions (string constant)
1399 "p 'a" ambiguous (all symbols starting with a)
1400 "p b-a" ambiguous (all symbols starting with a)
1401 "p b-" ambiguous (all symbols)
1402 "file Make" "file" (word break hard to screw up here)
1403 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1404 */
1405
1406 /* Generate completions one by one for the completer. Each time we are
1407 called return another potential completion to the caller. The function
1408 is misnamed; it just completes on commands or passes the buck to the
1409 command's completer function; the stuff specific to symbol completion
1410 is in make_symbol_completion_list.
1411
1412 TEXT is readline's idea of the "word" we are looking at; we don't really
1413 like readline's ideas about word breaking so we ignore it.
1414
1415 MATCHES is the number of matches that have currently been collected from
1416 calling this completion function. When zero, then we need to initialize,
1417 otherwise the initialization has already taken place and we can just
1418 return the next potential completion string.
1419
1420 Returns NULL if there are no more completions, else a pointer to a string
1421 which is a possible completion.
1422
1423 RL_LINE_BUFFER is available to be looked at; it contains the entire text
1424 of the line. RL_POINT is the offset in that line of the cursor. You
1425 should pretend that the line ends at RL_POINT. */
1426
1427 static char *
1428 symbol_completion_function (text, matches)
1429 char *text;
1430 int matches;
1431 {
1432 static char **list = (char **)NULL; /* Cache of completions */
1433 static int index; /* Next cached completion */
1434 char *output = NULL;
1435 char *tmp_command, *p;
1436 /* Pointer within tmp_command which corresponds to text. */
1437 char *word;
1438 struct cmd_list_element *c, *result_list;
1439
1440 if (matches == 0)
1441 {
1442 /* The caller is beginning to accumulate a new set of completions, so
1443 we need to find all of them now, and cache them for returning one at
1444 a time on future calls. */
1445
1446 if (list)
1447 {
1448 /* Free the storage used by LIST, but not by the strings inside.
1449 This is because rl_complete_internal () frees the strings. */
1450 free ((PTR)list);
1451 }
1452 list = 0;
1453 index = 0;
1454
1455 /* Choose the default set of word break characters to break completions.
1456 If we later find out that we are doing completions on command strings
1457 (as opposed to strings supplied by the individual command completer
1458 functions, which can be any string) then we will switch to the
1459 special word break set for command strings, which leaves out the
1460 '-' character used in some commands. */
1461
1462 rl_completer_word_break_characters =
1463 gdb_completer_word_break_characters;
1464
1465 /* Decide whether to complete on a list of gdb commands or on symbols. */
1466 tmp_command = (char *) alloca (rl_point + 1);
1467 p = tmp_command;
1468
1469 strncpy (tmp_command, rl_line_buffer, rl_point);
1470 tmp_command[rl_point] = '\0';
1471 /* Since text always contains some number of characters leading up
1472 to rl_point, we can find the equivalent position in tmp_command
1473 by subtracting that many characters from the end of tmp_command. */
1474 word = tmp_command + rl_point - strlen (text);
1475
1476 if (rl_point == 0)
1477 {
1478 /* An empty line we want to consider ambiguous; that is, it
1479 could be any command. */
1480 c = (struct cmd_list_element *) -1;
1481 result_list = 0;
1482 }
1483 else
1484 {
1485 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
1486 }
1487
1488 /* Move p up to the next interesting thing. */
1489 while (*p == ' ' || *p == '\t')
1490 {
1491 p++;
1492 }
1493
1494 if (!c)
1495 {
1496 /* It is an unrecognized command. So there are no
1497 possible completions. */
1498 list = NULL;
1499 }
1500 else if (c == (struct cmd_list_element *) -1)
1501 {
1502 char *q;
1503
1504 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1505 doesn't advance over that thing itself. Do so now. */
1506 q = p;
1507 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1508 ++q;
1509 if (q != tmp_command + rl_point)
1510 {
1511 /* There is something beyond the ambiguous
1512 command, so there are no possible completions. For
1513 example, "info t " or "info t foo" does not complete
1514 to anything, because "info t" can be "info target" or
1515 "info terminal". */
1516 list = NULL;
1517 }
1518 else
1519 {
1520 /* We're trying to complete on the command which was ambiguous.
1521 This we can deal with. */
1522 if (result_list)
1523 {
1524 list = complete_on_cmdlist (*result_list->prefixlist, p,
1525 word);
1526 }
1527 else
1528 {
1529 list = complete_on_cmdlist (cmdlist, p, word);
1530 }
1531 /* Insure that readline does the right thing with respect to
1532 inserting quotes. */
1533 rl_completer_word_break_characters =
1534 gdb_completer_command_word_break_characters;
1535 }
1536 }
1537 else
1538 {
1539 /* We've recognized a full command. */
1540
1541 if (p == tmp_command + rl_point)
1542 {
1543 /* There is no non-whitespace in the line beyond the command. */
1544
1545 if (p[-1] == ' ' || p[-1] == '\t')
1546 {
1547 /* The command is followed by whitespace; we need to complete
1548 on whatever comes after command. */
1549 if (c->prefixlist)
1550 {
1551 /* It is a prefix command; what comes after it is
1552 a subcommand (e.g. "info "). */
1553 list = complete_on_cmdlist (*c->prefixlist, p, word);
1554
1555 /* Insure that readline does the right thing
1556 with respect to inserting quotes. */
1557 rl_completer_word_break_characters =
1558 gdb_completer_command_word_break_characters;
1559 }
1560 else
1561 {
1562 /* It is a normal command; what comes after it is
1563 completed by the command's completer function. */
1564 list = (*c->completer) (p, word);
1565 }
1566 }
1567 else
1568 {
1569 /* The command is not followed by whitespace; we need to
1570 complete on the command itself. e.g. "p" which is a
1571 command itself but also can complete to "print", "ptype"
1572 etc. */
1573 char *q;
1574
1575 /* Find the command we are completing on. */
1576 q = p;
1577 while (q > tmp_command)
1578 {
1579 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1580 --q;
1581 else
1582 break;
1583 }
1584
1585 list = complete_on_cmdlist (result_list, q, word);
1586
1587 /* Insure that readline does the right thing
1588 with respect to inserting quotes. */
1589 rl_completer_word_break_characters =
1590 gdb_completer_command_word_break_characters;
1591 }
1592 }
1593 else
1594 {
1595 /* There is non-whitespace beyond the command. */
1596
1597 if (c->prefixlist && !c->allow_unknown)
1598 {
1599 /* It is an unrecognized subcommand of a prefix command,
1600 e.g. "info adsfkdj". */
1601 list = NULL;
1602 }
1603 else
1604 {
1605 /* It is a normal command. */
1606 list = (*c->completer) (p, word);
1607 }
1608 }
1609 }
1610 }
1611
1612 /* If we found a list of potential completions during initialization then
1613 dole them out one at a time. The vector of completions is NULL
1614 terminated, so after returning the last one, return NULL (and continue
1615 to do so) each time we are called after that, until a new list is
1616 available. */
1617
1618 if (list)
1619 {
1620 output = list[index];
1621 if (output)
1622 {
1623 index++;
1624 }
1625 }
1626
1627 #if 0
1628 /* Can't do this because readline hasn't yet checked the word breaks
1629 for figuring out whether to insert a quote. */
1630 if (output == NULL)
1631 /* Make sure the word break characters are set back to normal for the
1632 next time that readline tries to complete something. */
1633 rl_completer_word_break_characters =
1634 gdb_completer_word_break_characters;
1635 #endif
1636
1637 return (output);
1638 }
1639
1640 /* Skip over a possibly quoted word (as defined by the quote characters
1641 and word break characters the completer uses). Returns pointer to the
1642 location after the "word". */
1643
1644 char *
1645 skip_quoted (str)
1646 char *str;
1647 {
1648 char quote_char = '\0';
1649 char *scan;
1650
1651 for (scan = str; *scan != '\0'; scan++)
1652 {
1653 if (quote_char != '\0')
1654 {
1655 /* Ignore everything until the matching close quote char */
1656 if (*scan == quote_char)
1657 {
1658 /* Found matching close quote. */
1659 scan++;
1660 break;
1661 }
1662 }
1663 else if (strchr (gdb_completer_quote_characters, *scan))
1664 {
1665 /* Found start of a quoted string. */
1666 quote_char = *scan;
1667 }
1668 else if (strchr (gdb_completer_word_break_characters, *scan))
1669 {
1670 break;
1671 }
1672 }
1673 return (scan);
1674 }
1675
1676 \f
1677 #ifdef STOP_SIGNAL
1678 static void
1679 stop_sig (signo)
1680 int signo;
1681 {
1682 #if STOP_SIGNAL == SIGTSTP
1683 signal (SIGTSTP, SIG_DFL);
1684 sigsetmask (0);
1685 kill (getpid (), SIGTSTP);
1686 signal (SIGTSTP, stop_sig);
1687 #else
1688 signal (STOP_SIGNAL, stop_sig);
1689 #endif
1690 printf_unfiltered ("%s", prompt);
1691 gdb_flush (gdb_stdout);
1692
1693 /* Forget about any previous command -- null line now will do nothing. */
1694 dont_repeat ();
1695 }
1696 #endif /* STOP_SIGNAL */
1697
1698 /* Initialize signal handlers. */
1699 static void
1700 do_nothing (signo)
1701 int signo;
1702 {
1703 }
1704
1705 static void
1706 init_signals ()
1707 {
1708 signal (SIGINT, request_quit);
1709
1710 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
1711 passed to the inferior, which we don't want. It would be
1712 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
1713 on BSD4.3 systems using vfork, that can affect the
1714 GDB process as well as the inferior (the signal handling tables
1715 might be in memory, shared between the two). Since we establish
1716 a handler for SIGQUIT, when we call exec it will set the signal
1717 to SIG_DFL for us. */
1718 signal (SIGQUIT, do_nothing);
1719 if (signal (SIGHUP, do_nothing) != SIG_IGN)
1720 signal (SIGHUP, disconnect);
1721 signal (SIGFPE, float_handler);
1722
1723 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1724 signal (SIGWINCH, SIGWINCH_HANDLER);
1725 #endif
1726 }
1727 \f
1728 /* Read one line from the command input stream `instream'
1729 into the local static buffer `linebuffer' (whose current length
1730 is `linelength').
1731 The buffer is made bigger as necessary.
1732 Returns the address of the start of the line.
1733
1734 NULL is returned for end of file.
1735
1736 *If* the instream == stdin & stdin is a terminal, the line read
1737 is copied into the file line saver (global var char *line,
1738 length linesize) so that it can be duplicated.
1739
1740 This routine either uses fancy command line editing or
1741 simple input as the user has requested. */
1742
1743 char *
1744 command_line_input (prrompt, repeat, annotation_suffix)
1745 char *prrompt;
1746 int repeat;
1747 char *annotation_suffix;
1748 {
1749 static char *linebuffer = 0;
1750 static unsigned linelength = 0;
1751 register char *p;
1752 char *p1;
1753 char *rl;
1754 char *local_prompt = prrompt;
1755 register int c;
1756 char *nline;
1757 char got_eof = 0;
1758
1759 if (annotation_level > 1 && instream == stdin)
1760 {
1761 local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt))
1762 + strlen (annotation_suffix) + 40);
1763 if (prrompt == NULL)
1764 local_prompt[0] = '\0';
1765 else
1766 strcpy (local_prompt, prrompt);
1767 strcat (local_prompt, "\n\032\032");
1768 strcat (local_prompt, annotation_suffix);
1769 strcat (local_prompt, "\n");
1770 }
1771
1772 if (linebuffer == 0)
1773 {
1774 linelength = 80;
1775 linebuffer = (char *) xmalloc (linelength);
1776 }
1777
1778 p = linebuffer;
1779
1780 /* Control-C quits instantly if typed while in this loop
1781 since it should not wait until the user types a newline. */
1782 immediate_quit++;
1783 #ifdef STOP_SIGNAL
1784 if (job_control)
1785 signal (STOP_SIGNAL, stop_sig);
1786 #endif
1787
1788 while (1)
1789 {
1790 /* Make sure that all output has been output. Some machines may let
1791 you get away with leaving out some of the gdb_flush, but not all. */
1792 wrap_here ("");
1793 gdb_flush (gdb_stdout);
1794 gdb_flush (gdb_stderr);
1795
1796 if (source_file_name != NULL)
1797 {
1798 ++source_line_number;
1799 sprintf (source_error,
1800 "%s%s:%d: Error in sourced command file:\n",
1801 source_pre_error,
1802 source_file_name,
1803 source_line_number);
1804 error_pre_print = source_error;
1805 }
1806
1807 if (annotation_level > 1 && instream == stdin)
1808 {
1809 printf_unfiltered ("\n\032\032pre-");
1810 printf_unfiltered (annotation_suffix);
1811 printf_unfiltered ("\n");
1812 }
1813
1814 /* Don't use fancy stuff if not talking to stdin. */
1815 if (command_editing_p && instream == stdin
1816 && ISATTY (instream))
1817 rl = readline (local_prompt);
1818 else
1819 rl = gdb_readline (local_prompt);
1820
1821 if (annotation_level > 1 && instream == stdin)
1822 {
1823 printf_unfiltered ("\n\032\032post-");
1824 printf_unfiltered (annotation_suffix);
1825 printf_unfiltered ("\n");
1826 }
1827
1828 if (!rl || rl == (char *) EOF)
1829 {
1830 got_eof = 1;
1831 break;
1832 }
1833 if (strlen(rl) + 1 + (p - linebuffer) > linelength)
1834 {
1835 linelength = strlen(rl) + 1 + (p - linebuffer);
1836 nline = (char *) xrealloc (linebuffer, linelength);
1837 p += nline - linebuffer;
1838 linebuffer = nline;
1839 }
1840 p1 = rl;
1841 /* Copy line. Don't copy null at end. (Leaves line alone
1842 if this was just a newline) */
1843 while (*p1)
1844 *p++ = *p1++;
1845
1846 free (rl); /* Allocated in readline. */
1847
1848 if (p == linebuffer || *(p - 1) != '\\')
1849 break;
1850
1851 p--; /* Put on top of '\'. */
1852 local_prompt = (char *) 0;
1853 }
1854
1855 #ifdef STOP_SIGNAL
1856 if (job_control)
1857 signal (STOP_SIGNAL, SIG_DFL);
1858 #endif
1859 immediate_quit--;
1860
1861 if (got_eof)
1862 return NULL;
1863
1864 #define SERVER_COMMAND_LENGTH 7
1865 server_command =
1866 (p - linebuffer > SERVER_COMMAND_LENGTH)
1867 && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
1868 if (server_command)
1869 {
1870 /* Note that we don't set `line'. Between this and the check in
1871 dont_repeat, this insures that repeating will still do the
1872 right thing. */
1873 *p = '\0';
1874 return linebuffer + SERVER_COMMAND_LENGTH;
1875 }
1876
1877 /* Do history expansion if that is wished. */
1878 if (history_expansion_p && instream == stdin
1879 && ISATTY (instream))
1880 {
1881 char *history_value;
1882 int expanded;
1883
1884 *p = '\0'; /* Insert null now. */
1885 expanded = history_expand (linebuffer, &history_value);
1886 if (expanded)
1887 {
1888 /* Print the changes. */
1889 printf_unfiltered ("%s\n", history_value);
1890
1891 /* If there was an error, call this function again. */
1892 if (expanded < 0)
1893 {
1894 free (history_value);
1895 return command_line_input (prrompt, repeat, annotation_suffix);
1896 }
1897 if (strlen (history_value) > linelength)
1898 {
1899 linelength = strlen (history_value) + 1;
1900 linebuffer = (char *) xrealloc (linebuffer, linelength);
1901 }
1902 strcpy (linebuffer, history_value);
1903 p = linebuffer + strlen(linebuffer);
1904 free (history_value);
1905 }
1906 }
1907
1908 /* If we just got an empty line, and that is supposed
1909 to repeat the previous command, return the value in the
1910 global buffer. */
1911 if (repeat)
1912 {
1913 if (p == linebuffer)
1914 return line;
1915 p1 = linebuffer;
1916 while (*p1 == ' ' || *p1 == '\t')
1917 p1++;
1918 if (!*p1)
1919 return line;
1920 }
1921
1922 *p = 0;
1923
1924 /* Add line to history if appropriate. */
1925 if (instream == stdin
1926 && ISATTY (stdin) && *linebuffer)
1927 add_history (linebuffer);
1928
1929 /* Note: lines consisting solely of comments are added to the command
1930 history. This is useful when you type a command, and then
1931 realize you don't want to execute it quite yet. You can comment
1932 out the command and then later fetch it from the value history
1933 and remove the '#'. The kill ring is probably better, but some
1934 people are in the habit of commenting things out. */
1935 p1 = linebuffer;
1936 while ((c = *p1++) != '\0')
1937 {
1938 if (c == '"')
1939 while ((c = *p1++) != '"')
1940 {
1941 /* Make sure an escaped '"' doesn't make us think the string
1942 is ended. */
1943 if (c == '\\')
1944 parse_escape (&p1);
1945 if (c == '\0')
1946 break;
1947 }
1948 else if (c == '\'')
1949 while ((c = *p1++) != '\'')
1950 {
1951 /* Make sure an escaped '\'' doesn't make us think the string
1952 is ended. */
1953 if (c == '\\')
1954 parse_escape (&p1);
1955 if (c == '\0')
1956 break;
1957 }
1958 else if (c == '#')
1959 {
1960 /* Found a comment. */
1961 p1[-1] = '\0';
1962 break;
1963 }
1964 }
1965
1966 /* Save into global buffer if appropriate. */
1967 if (repeat)
1968 {
1969 if (linelength > linesize)
1970 {
1971 line = xrealloc (line, linelength);
1972 linesize = linelength;
1973 }
1974 strcpy (line, linebuffer);
1975 return line;
1976 }
1977
1978 return linebuffer;
1979 }
1980 \f
1981 /* Read lines from the input stream
1982 and accumulate them in a chain of struct command_line's
1983 which is then returned. */
1984
1985 struct command_line *
1986 read_command_lines ()
1987 {
1988 struct command_line *first = 0;
1989 register struct command_line *next, *tail = 0;
1990 register char *p, *p1;
1991 struct cleanup *old_chain = 0;
1992
1993 while (1)
1994 {
1995 dont_repeat ();
1996 p = command_line_input ((char *) NULL, instream == stdin, "commands");
1997 if (p == NULL)
1998 /* Treat end of file like "end". */
1999 break;
2000
2001 /* Remove leading and trailing blanks. */
2002 while (*p == ' ' || *p == '\t') p++;
2003 p1 = p + strlen (p);
2004 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
2005
2006 /* Is this "end"? */
2007 if (p1 - p == 3 && !strncmp (p, "end", 3))
2008 break;
2009
2010 /* No => add this line to the chain of command lines. */
2011 next = (struct command_line *) xmalloc (sizeof (struct command_line));
2012 next->line = savestring (p, p1 - p);
2013 next->next = 0;
2014 if (tail)
2015 {
2016 tail->next = next;
2017 }
2018 else
2019 {
2020 /* We just read the first line.
2021 From now on, arrange to throw away the lines we have
2022 if we quit or get an error while inside this function. */
2023 first = next;
2024 old_chain = make_cleanup (free_command_lines, &first);
2025 }
2026 tail = next;
2027 }
2028
2029 dont_repeat ();
2030
2031 /* Now we are about to return the chain to our caller,
2032 so freeing it becomes his responsibility. */
2033 if (first)
2034 discard_cleanups (old_chain);
2035 return first;
2036 }
2037
2038 /* Free a chain of struct command_line's. */
2039
2040 void
2041 free_command_lines (lptr)
2042 struct command_line **lptr;
2043 {
2044 register struct command_line *l = *lptr;
2045 register struct command_line *next;
2046
2047 while (l)
2048 {
2049 next = l->next;
2050 free (l->line);
2051 free ((PTR)l);
2052 l = next;
2053 }
2054 }
2055 \f
2056 /* Add an element to the list of info subcommands. */
2057
2058 void
2059 add_info (name, fun, doc)
2060 char *name;
2061 void (*fun) PARAMS ((char *, int));
2062 char *doc;
2063 {
2064 add_cmd (name, no_class, fun, doc, &infolist);
2065 }
2066
2067 /* Add an alias to the list of info subcommands. */
2068
2069 void
2070 add_info_alias (name, oldname, abbrev_flag)
2071 char *name;
2072 char *oldname;
2073 int abbrev_flag;
2074 {
2075 add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2076 }
2077
2078 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2079 Therefore, its own definition is called only for "info" with no args. */
2080
2081 /* ARGSUSED */
2082 static void
2083 info_command (arg, from_tty)
2084 char *arg;
2085 int from_tty;
2086 {
2087 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2088 help_list (infolist, "info ", -1, gdb_stdout);
2089 }
2090
2091 /* The "complete" command is used by Emacs to implement completion. */
2092
2093 /* ARGSUSED */
2094 static void
2095 complete_command (arg, from_tty)
2096 char *arg;
2097 int from_tty;
2098 {
2099 int i;
2100 char *completion;
2101
2102 dont_repeat ();
2103
2104 if (arg == NULL)
2105 {
2106 rl_line_buffer[0] = '\0';
2107 rl_point = 0;
2108 }
2109 else
2110 {
2111 strcpy (rl_line_buffer, arg);
2112 rl_point = strlen (arg);
2113 }
2114
2115 for (completion = symbol_completion_function (rl_line_buffer, i = 0);
2116 completion;
2117 completion = symbol_completion_function (rl_line_buffer, ++i))
2118 printf_unfiltered ("%s\n", completion);
2119 }
2120
2121 /* The "show" command with no arguments shows all the settings. */
2122
2123 /* ARGSUSED */
2124 static void
2125 show_command (arg, from_tty)
2126 char *arg;
2127 int from_tty;
2128 {
2129 cmd_show_list (showlist, from_tty, "");
2130 }
2131 \f
2132 /* Add an element to the list of commands. */
2133
2134 void
2135 add_com (name, class, fun, doc)
2136 char *name;
2137 enum command_class class;
2138 void (*fun) PARAMS ((char *, int));
2139 char *doc;
2140 {
2141 add_cmd (name, class, fun, doc, &cmdlist);
2142 }
2143
2144 /* Add an alias or abbreviation command to the list of commands. */
2145
2146 void
2147 add_com_alias (name, oldname, class, abbrev_flag)
2148 char *name;
2149 char *oldname;
2150 enum command_class class;
2151 int abbrev_flag;
2152 {
2153 add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
2154 }
2155
2156 void
2157 error_no_arg (why)
2158 char *why;
2159 {
2160 error ("Argument required (%s).", why);
2161 }
2162
2163 /* ARGSUSED */
2164 static void
2165 help_command (command, from_tty)
2166 char *command;
2167 int from_tty; /* Ignored */
2168 {
2169 help_cmd (command, gdb_stdout);
2170 }
2171 \f
2172 static void
2173 validate_comname (comname)
2174 char *comname;
2175 {
2176 register char *p;
2177
2178 if (comname == 0)
2179 error_no_arg ("name of command to define");
2180
2181 p = comname;
2182 while (*p)
2183 {
2184 if (!isalnum(*p) && *p != '-')
2185 error ("Junk in argument list: \"%s\"", p);
2186 p++;
2187 }
2188 }
2189
2190 /* This is just a placeholder in the command data structures. */
2191 static void
2192 user_defined_command (ignore, from_tty)
2193 char *ignore;
2194 int from_tty;
2195 {
2196 }
2197
2198 static void
2199 define_command (comname, from_tty)
2200 char *comname;
2201 int from_tty;
2202 {
2203 register struct command_line *cmds;
2204 register struct cmd_list_element *c, *newc, *hookc = 0;
2205 char *tem = comname;
2206 #define HOOK_STRING "hook-"
2207 #define HOOK_LEN 5
2208
2209 validate_comname (comname);
2210
2211 /* Look it up, and verify that we got an exact match. */
2212 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
2213 if (c && !STREQ (comname, c->name))
2214 c = 0;
2215
2216 if (c)
2217 {
2218 if (c->class == class_user || c->class == class_alias)
2219 tem = "Redefine command \"%s\"? ";
2220 else
2221 tem = "Really redefine built-in command \"%s\"? ";
2222 if (!query (tem, c->name))
2223 error ("Command \"%s\" not redefined.", c->name);
2224 }
2225
2226 /* If this new command is a hook, then mark the command which it
2227 is hooking. Note that we allow hooking `help' commands, so that
2228 we can hook the `stop' pseudo-command. */
2229
2230 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
2231 {
2232 /* Look up cmd it hooks, and verify that we got an exact match. */
2233 tem = comname+HOOK_LEN;
2234 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
2235 if (hookc && !STREQ (comname+HOOK_LEN, hookc->name))
2236 hookc = 0;
2237 if (!hookc)
2238 {
2239 warning ("Your new `%s' command does not hook any existing command.",
2240 comname);
2241 if (!query ("Proceed? ", (char *)0))
2242 error ("Not confirmed.");
2243 }
2244 }
2245
2246 comname = savestring (comname, strlen (comname));
2247
2248 /* If the rest of the commands will be case insensitive, this one
2249 should behave in the same manner. */
2250 for (tem = comname; *tem; tem++)
2251 if (isupper(*tem)) *tem = tolower(*tem);
2252
2253 if (from_tty)
2254 {
2255 printf_unfiltered ("Type commands for definition of \"%s\".\n\
2256 End with a line saying just \"end\".\n", comname);
2257 gdb_flush (gdb_stdout);
2258 }
2259
2260 cmds = read_command_lines ();
2261
2262 if (c && c->class == class_user)
2263 free_command_lines (&c->user_commands);
2264
2265 newc = add_cmd (comname, class_user, user_defined_command,
2266 (c && c->class == class_user)
2267 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
2268 newc->user_commands = cmds;
2269
2270 /* If this new command is a hook, then mark both commands as being
2271 tied. */
2272 if (hookc)
2273 {
2274 hookc->hook = newc; /* Target gets hooked. */
2275 newc->hookee = hookc; /* We are marked as hooking target cmd. */
2276 }
2277 }
2278
2279 static void
2280 document_command (comname, from_tty)
2281 char *comname;
2282 int from_tty;
2283 {
2284 struct command_line *doclines;
2285 register struct cmd_list_element *c;
2286 char *tem = comname;
2287
2288 validate_comname (comname);
2289
2290 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
2291
2292 if (c->class != class_user)
2293 error ("Command \"%s\" is built-in.", comname);
2294
2295 if (from_tty)
2296 printf_unfiltered ("Type documentation for \"%s\".\n\
2297 End with a line saying just \"end\".\n", comname);
2298
2299 doclines = read_command_lines ();
2300
2301 if (c->doc) free (c->doc);
2302
2303 {
2304 register struct command_line *cl1;
2305 register int len = 0;
2306
2307 for (cl1 = doclines; cl1; cl1 = cl1->next)
2308 len += strlen (cl1->line) + 1;
2309
2310 c->doc = (char *) xmalloc (len + 1);
2311 *c->doc = 0;
2312
2313 for (cl1 = doclines; cl1; cl1 = cl1->next)
2314 {
2315 strcat (c->doc, cl1->line);
2316 if (cl1->next)
2317 strcat (c->doc, "\n");
2318 }
2319 }
2320
2321 free_command_lines (&doclines);
2322 }
2323 \f
2324 static void
2325 print_gnu_advertisement()
2326 {
2327 printf_unfiltered ("\
2328 GDB is free software and you are welcome to distribute copies of it\n\
2329 under certain conditions; type \"show copying\" to see the conditions.\n\
2330 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
2331 ");
2332 }
2333
2334 static void
2335 print_gdb_version (stream)
2336 GDB_FILE *stream;
2337 {
2338 fprintf_filtered (stream, "\
2339 GDB %s (%s", version, host_name);
2340
2341 if (!STREQ (host_name, target_name))
2342 fprintf_filtered (stream, " --target %s", target_name);
2343
2344 fprintf_filtered (stream, "), ");
2345 wrap_here("");
2346 fprintf_filtered (stream, "Copyright 1994 Free Software Foundation, Inc.");
2347 }
2348
2349 /* ARGSUSED */
2350 static void
2351 show_version (args, from_tty)
2352 char *args;
2353 int from_tty;
2354 {
2355 immediate_quit++;
2356 print_gnu_advertisement ();
2357 print_gdb_version (gdb_stdout);
2358 printf_filtered ("\n");
2359 immediate_quit--;
2360 }
2361 \f
2362 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2363 is obsolete. */
2364
2365 void
2366 print_prompt ()
2367 {
2368 printf_unfiltered ("%s", prompt);
2369 gdb_flush (gdb_stdout);
2370 }
2371 \f
2372 static void
2373 quit_command (args, from_tty)
2374 char *args;
2375 int from_tty;
2376 {
2377 if (inferior_pid != 0 && target_has_execution)
2378 {
2379 if (attach_flag)
2380 {
2381 if (query ("The program is running. Quit anyway (and detach it)? "))
2382 target_detach (args, from_tty);
2383 else
2384 error ("Not confirmed.");
2385 }
2386 else
2387 {
2388 if (query ("The program is running. Quit anyway (and kill it)? "))
2389 target_kill ();
2390 else
2391 error ("Not confirmed.");
2392 }
2393 }
2394 /* UDI wants this, to kill the TIP. */
2395 target_close (1);
2396
2397 /* Save the history information if it is appropriate to do so. */
2398 if (write_history_p && history_filename)
2399 write_history (history_filename);
2400
2401 exit (0);
2402 }
2403
2404 /* Returns whether GDB is running on a terminal and whether the user
2405 desires that questions be asked of them on that terminal. */
2406
2407 int
2408 input_from_terminal_p ()
2409 {
2410 return gdb_has_a_terminal () && (instream == stdin) & caution;
2411 }
2412 \f
2413 /* ARGSUSED */
2414 static void
2415 pwd_command (args, from_tty)
2416 char *args;
2417 int from_tty;
2418 {
2419 if (args) error ("The \"pwd\" command does not take an argument: %s", args);
2420 getcwd (dirbuf, sizeof (dirbuf));
2421
2422 if (!STREQ (dirbuf, current_directory))
2423 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
2424 current_directory, dirbuf);
2425 else
2426 printf_unfiltered ("Working directory %s.\n", current_directory);
2427 }
2428
2429 static void
2430 cd_command (dir, from_tty)
2431 char *dir;
2432 int from_tty;
2433 {
2434 int len;
2435 /* Found something other than leading repetitions of "/..". */
2436 int found_real_path;
2437 char *p;
2438
2439 /* If the new directory is absolute, repeat is a no-op; if relative,
2440 repeat might be useful but is more likely to be a mistake. */
2441 dont_repeat ();
2442
2443 if (dir == 0)
2444 error_no_arg ("new working directory");
2445
2446 dir = tilde_expand (dir);
2447 make_cleanup (free, dir);
2448
2449 if (chdir (dir) < 0)
2450 perror_with_name (dir);
2451
2452 len = strlen (dir);
2453 dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
2454 if (dir[0] == '/')
2455 current_directory = dir;
2456 else
2457 {
2458 if (current_directory[0] == '/' && current_directory[1] == '\0')
2459 current_directory = concat (current_directory, dir, NULL);
2460 else
2461 current_directory = concat (current_directory, "/", dir, NULL);
2462 free (dir);
2463 }
2464
2465 /* Now simplify any occurrences of `.' and `..' in the pathname. */
2466
2467 found_real_path = 0;
2468 for (p = current_directory; *p;)
2469 {
2470 if (p[0] == '/' && p[1] == '.' && (p[2] == 0 || p[2] == '/'))
2471 strcpy (p, p + 2);
2472 else if (p[0] == '/' && p[1] == '.' && p[2] == '.'
2473 && (p[3] == 0 || p[3] == '/'))
2474 {
2475 if (found_real_path)
2476 {
2477 /* Search backwards for the directory just before the "/.."
2478 and obliterate it and the "/..". */
2479 char *q = p;
2480 while (q != current_directory && q[-1] != '/')
2481 --q;
2482
2483 if (q == current_directory)
2484 /* current_directory is
2485 a relative pathname ("can't happen"--leave it alone). */
2486 ++p;
2487 else
2488 {
2489 strcpy (q - 1, p + 3);
2490 p = q - 1;
2491 }
2492 }
2493 else
2494 /* We are dealing with leading repetitions of "/..", for example
2495 "/../..", which is the Mach super-root. */
2496 p += 3;
2497 }
2498 else
2499 {
2500 found_real_path = 1;
2501 ++p;
2502 }
2503 }
2504
2505 forget_cached_source_info ();
2506
2507 if (from_tty)
2508 pwd_command ((char *) 0, 1);
2509 }
2510 \f
2511 struct source_cleanup_lines_args {
2512 int old_line;
2513 char *old_file;
2514 char *old_pre_error;
2515 char *old_error_pre_print;
2516 };
2517
2518 static void
2519 source_cleanup_lines (args)
2520 PTR args;
2521 {
2522 struct source_cleanup_lines_args *p =
2523 (struct source_cleanup_lines_args *)args;
2524 source_line_number = p->old_line;
2525 source_file_name = p->old_file;
2526 source_pre_error = p->old_pre_error;
2527 error_pre_print = p->old_error_pre_print;
2528 }
2529
2530 /* ARGSUSED */
2531 static void
2532 source_command (args, from_tty)
2533 char *args;
2534 int from_tty;
2535 {
2536 FILE *stream;
2537 struct cleanup *old_cleanups;
2538 char *file = args;
2539 struct source_cleanup_lines_args old_lines;
2540 int needed_length;
2541
2542 if (file == NULL)
2543 {
2544 error ("source command requires pathname of file to source.");
2545 }
2546
2547 file = tilde_expand (file);
2548 old_cleanups = make_cleanup (free, file);
2549
2550 stream = fopen (file, FOPEN_RT);
2551 if (stream == 0)
2552 perror_with_name (file);
2553
2554 make_cleanup (fclose, stream);
2555
2556 old_lines.old_line = source_line_number;
2557 old_lines.old_file = source_file_name;
2558 old_lines.old_pre_error = source_pre_error;
2559 old_lines.old_error_pre_print = error_pre_print;
2560 make_cleanup (source_cleanup_lines, &old_lines);
2561 source_line_number = 0;
2562 source_file_name = file;
2563 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
2564 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
2565 make_cleanup (free, source_pre_error);
2566 /* This will get set every time we read a line. So it won't stay "" for
2567 long. */
2568 error_pre_print = "";
2569
2570 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
2571 if (source_error_allocated < needed_length)
2572 {
2573 source_error_allocated *= 2;
2574 if (source_error_allocated < needed_length)
2575 source_error_allocated = needed_length;
2576 if (source_error == NULL)
2577 source_error = xmalloc (source_error_allocated);
2578 else
2579 source_error = xrealloc (source_error, source_error_allocated);
2580 }
2581
2582 read_command_file (stream);
2583
2584 do_cleanups (old_cleanups);
2585 }
2586
2587 /* ARGSUSED */
2588 static void
2589 echo_command (text, from_tty)
2590 char *text;
2591 int from_tty;
2592 {
2593 char *p = text;
2594 register int c;
2595
2596 if (text)
2597 while ((c = *p++) != '\0')
2598 {
2599 if (c == '\\')
2600 {
2601 /* \ at end of argument is used after spaces
2602 so they won't be lost. */
2603 if (*p == 0)
2604 return;
2605
2606 c = parse_escape (&p);
2607 if (c >= 0)
2608 printf_filtered ("%c", c);
2609 }
2610 else
2611 printf_filtered ("%c", c);
2612 }
2613
2614 /* Force this output to appear now. */
2615 wrap_here ("");
2616 gdb_flush (gdb_stdout);
2617 }
2618
2619 \f
2620 /* Functions to manipulate command line editing control variables. */
2621
2622 /* Number of commands to print in each call to show_commands. */
2623 #define Hist_print 10
2624 static void
2625 show_commands (args, from_tty)
2626 char *args;
2627 int from_tty;
2628 {
2629 /* Index for history commands. Relative to history_base. */
2630 int offset;
2631
2632 /* Number of the history entry which we are planning to display next.
2633 Relative to history_base. */
2634 static int num = 0;
2635
2636 /* The first command in the history which doesn't exist (i.e. one more
2637 than the number of the last command). Relative to history_base. */
2638 int hist_len;
2639
2640 extern HIST_ENTRY *history_get PARAMS ((int));
2641
2642 /* Print out some of the commands from the command history. */
2643 /* First determine the length of the history list. */
2644 hist_len = history_size;
2645 for (offset = 0; offset < history_size; offset++)
2646 {
2647 if (!history_get (history_base + offset))
2648 {
2649 hist_len = offset;
2650 break;
2651 }
2652 }
2653
2654 if (args)
2655 {
2656 if (args[0] == '+' && args[1] == '\0')
2657 /* "info editing +" should print from the stored position. */
2658 ;
2659 else
2660 /* "info editing <exp>" should print around command number <exp>. */
2661 num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
2662 }
2663 /* "show commands" means print the last Hist_print commands. */
2664 else
2665 {
2666 num = hist_len - Hist_print;
2667 }
2668
2669 if (num < 0)
2670 num = 0;
2671
2672 /* If there are at least Hist_print commands, we want to display the last
2673 Hist_print rather than, say, the last 6. */
2674 if (hist_len - num < Hist_print)
2675 {
2676 num = hist_len - Hist_print;
2677 if (num < 0)
2678 num = 0;
2679 }
2680
2681 for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
2682 {
2683 printf_filtered ("%5d %s\n", history_base + offset,
2684 (history_get (history_base + offset))->line);
2685 }
2686
2687 /* The next command we want to display is the next one that we haven't
2688 displayed yet. */
2689 num += Hist_print;
2690
2691 /* If the user repeats this command with return, it should do what
2692 "show commands +" does. This is unnecessary if arg is null,
2693 because "show commands +" is not useful after "show commands". */
2694 if (from_tty && args)
2695 {
2696 args[0] = '+';
2697 args[1] = '\0';
2698 }
2699 }
2700
2701 /* Called by do_setshow_command. */
2702 /* ARGSUSED */
2703 static void
2704 set_history_size_command (args, from_tty, c)
2705 char *args;
2706 int from_tty;
2707 struct cmd_list_element *c;
2708 {
2709 if (history_size == INT_MAX)
2710 unstifle_history ();
2711 else if (history_size >= 0)
2712 stifle_history (history_size);
2713 else
2714 {
2715 history_size = INT_MAX;
2716 error ("History size must be non-negative");
2717 }
2718 }
2719
2720 /* ARGSUSED */
2721 static void
2722 set_history (args, from_tty)
2723 char *args;
2724 int from_tty;
2725 {
2726 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
2727 help_list (sethistlist, "set history ", -1, gdb_stdout);
2728 }
2729
2730 /* ARGSUSED */
2731 static void
2732 show_history (args, from_tty)
2733 char *args;
2734 int from_tty;
2735 {
2736 cmd_show_list (showhistlist, from_tty, "");
2737 }
2738
2739 int info_verbose = 0; /* Default verbose msgs off */
2740
2741 /* Called by do_setshow_command. An elaborate joke. */
2742 /* ARGSUSED */
2743 static void
2744 set_verbose (args, from_tty, c)
2745 char *args;
2746 int from_tty;
2747 struct cmd_list_element *c;
2748 {
2749 char *cmdname = "verbose";
2750 struct cmd_list_element *showcmd;
2751
2752 showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
2753
2754 if (info_verbose)
2755 {
2756 c->doc = "Set verbose printing of informational messages.";
2757 showcmd->doc = "Show verbose printing of informational messages.";
2758 }
2759 else
2760 {
2761 c->doc = "Set verbosity.";
2762 showcmd->doc = "Show verbosity.";
2763 }
2764 }
2765
2766 static void
2767 float_handler (signo)
2768 int signo;
2769 {
2770 /* This message is based on ANSI C, section 4.7. Note that integer
2771 divide by zero causes this, so "float" is a misnomer. */
2772 signal (SIGFPE, float_handler);
2773 error ("Erroneous arithmetic operation.");
2774 }
2775
2776 /* Return whether we are running a batch file or from terminal. */
2777 int
2778 batch_mode ()
2779 {
2780 return !(instream == stdin && ISATTY (stdin));
2781 }
2782
2783 \f
2784 static void
2785 init_cmd_lists ()
2786 {
2787 cmdlist = NULL;
2788 infolist = NULL;
2789 enablelist = NULL;
2790 disablelist = NULL;
2791 deletelist = NULL;
2792 enablebreaklist = NULL;
2793 setlist = NULL;
2794 unsetlist = NULL;
2795 showlist = NULL;
2796 sethistlist = NULL;
2797 showhistlist = NULL;
2798 unsethistlist = NULL;
2799 #if MAINTENANCE_CMDS
2800 maintenancelist = NULL;
2801 maintenanceinfolist = NULL;
2802 maintenanceprintlist = NULL;
2803 #endif
2804 setprintlist = NULL;
2805 showprintlist = NULL;
2806 setchecklist = NULL;
2807 showchecklist = NULL;
2808 }
2809
2810 /* Init the history buffer. Note that we are called after the init file(s)
2811 * have been read so that the user can change the history file via his
2812 * .gdbinit file (for instance). The GDBHISTFILE environment variable
2813 * overrides all of this.
2814 */
2815
2816 static void
2817 init_history()
2818 {
2819 char *tmpenv;
2820
2821 tmpenv = getenv ("HISTSIZE");
2822 if (tmpenv)
2823 history_size = atoi (tmpenv);
2824 else if (!history_size)
2825 history_size = 256;
2826
2827 stifle_history (history_size);
2828
2829 tmpenv = getenv ("GDBHISTFILE");
2830 if (tmpenv)
2831 history_filename = savestring (tmpenv, strlen(tmpenv));
2832 else if (!history_filename) {
2833 /* We include the current directory so that if the user changes
2834 directories the file written will be the same as the one
2835 that was read. */
2836 history_filename = concat (current_directory, "/.gdb_history", NULL);
2837 }
2838 read_history (history_filename);
2839 }
2840
2841 static void
2842 init_main ()
2843 {
2844 struct cmd_list_element *c;
2845
2846 #ifdef DEFAULT_PROMPT
2847 prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
2848 #else
2849 prompt = savestring ("(gdb) ", 6);
2850 #endif
2851
2852 /* Set the important stuff up for command editing. */
2853 command_editing_p = 1;
2854 history_expansion_p = 0;
2855 write_history_p = 0;
2856
2857 /* Setup important stuff for command line editing. */
2858 rl_completion_entry_function = (int (*)()) symbol_completion_function;
2859 rl_completer_word_break_characters = gdb_completer_word_break_characters;
2860 rl_completer_quote_characters = gdb_completer_quote_characters;
2861 rl_readline_name = "gdb";
2862
2863 /* Define the classes of commands.
2864 They will appear in the help list in the reverse of this order. */
2865
2866 add_cmd ("internals", class_maintenance, NO_FUNCTION,
2867 "Maintenance commands.\n\
2868 Some gdb commands are provided just for use by gdb maintainers.\n\
2869 These commands are subject to frequent change, and may not be as\n\
2870 well documented as user commands.",
2871 &cmdlist);
2872 add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
2873 add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
2874 add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
2875 The commands in this class are those defined by the user.\n\
2876 Use the \"define\" command to define a command.", &cmdlist);
2877 add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
2878 add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
2879 add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
2880 add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
2881 add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
2882 add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
2883 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2884 counting from zero for the innermost (currently executing) frame.\n\n\
2885 At any time gdb identifies one frame as the \"selected\" frame.\n\
2886 Variable lookups are done with respect to the selected frame.\n\
2887 When the program being debugged stops, gdb selects the innermost frame.\n\
2888 The commands below can be used to select other frames by number or address.",
2889 &cmdlist);
2890 add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
2891
2892 add_com ("pwd", class_files, pwd_command,
2893 "Print working directory. This is used for your program as well.");
2894 c = add_cmd ("cd", class_files, cd_command,
2895 "Set working directory to DIR for debugger and program being debugged.\n\
2896 The change does not take effect for the program being debugged\n\
2897 until the next time it is started.", &cmdlist);
2898 c->completer = filename_completer;
2899
2900 add_show_from_set
2901 (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
2902 "Set gdb's prompt",
2903 &setlist),
2904 &showlist);
2905
2906 add_com ("echo", class_support, echo_command,
2907 "Print a constant string. Give string as argument.\n\
2908 C escape sequences may be used in the argument.\n\
2909 No newline is added at the end of the argument;\n\
2910 use \"\\n\" if you want a newline to be printed.\n\
2911 Since leading and trailing whitespace are ignored in command arguments,\n\
2912 if you want to print some you must use \"\\\" before leading whitespace\n\
2913 to be printed or after trailing whitespace.");
2914 add_com ("document", class_support, document_command,
2915 "Document a user-defined command.\n\
2916 Give command name as argument. Give documentation on following lines.\n\
2917 End with a line of just \"end\".");
2918 add_com ("define", class_support, define_command,
2919 "Define a new command name. Command name is argument.\n\
2920 Definition appears on following lines, one command per line.\n\
2921 End with a line of just \"end\".\n\
2922 Use the \"document\" command to give documentation for the new command.\n\
2923 Commands defined in this way do not take arguments.");
2924
2925 #ifdef __STDC__
2926 c = add_cmd ("source", class_support, source_command,
2927 "Read commands from a file named FILE.\n\
2928 Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
2929 when gdb is started.", &cmdlist);
2930 #else
2931 /* Punt file name, we can't help it easily. */
2932 c = add_cmd ("source", class_support, source_command,
2933 "Read commands from a file named FILE.\n\
2934 Note that the file \".gdbinit\" is read automatically in this way\n\
2935 when gdb is started.", &cmdlist);
2936 #endif
2937 c->completer = filename_completer;
2938
2939 add_com ("quit", class_support, quit_command, "Exit gdb.");
2940 add_com ("help", class_support, help_command, "Print list of commands.");
2941 add_com_alias ("q", "quit", class_support, 1);
2942 add_com_alias ("h", "help", class_support, 1);
2943
2944
2945 c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
2946 "Set ",
2947 &setlist),
2948 add_show_from_set (c, &showlist);
2949 c->function.sfunc = set_verbose;
2950 set_verbose (NULL, 0, c);
2951
2952 add_show_from_set
2953 (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
2954 "Set editing of command lines as they are typed.\n\
2955 Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
2956 Without an argument, command line editing is enabled. To edit, use\n\
2957 EMACS-like or VI-like commands like control-P or ESC.", &setlist),
2958 &showlist);
2959
2960 add_prefix_cmd ("history", class_support, set_history,
2961 "Generic command for setting command history parameters.",
2962 &sethistlist, "set history ", 0, &setlist);
2963 add_prefix_cmd ("history", class_support, show_history,
2964 "Generic command for showing command history parameters.",
2965 &showhistlist, "show history ", 0, &showlist);
2966
2967 add_show_from_set
2968 (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
2969 "Set history expansion on command input.\n\
2970 Without an argument, history expansion is enabled.", &sethistlist),
2971 &showhistlist);
2972
2973 add_show_from_set
2974 (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
2975 "Set saving of the history record on exit.\n\
2976 Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
2977 Without an argument, saving is enabled.", &sethistlist),
2978 &showhistlist);
2979
2980 c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size,
2981 "Set the size of the command history, \n\
2982 ie. the number of previous commands to keep a record of.", &sethistlist);
2983 add_show_from_set (c, &showhistlist);
2984 c->function.sfunc = set_history_size_command;
2985
2986 add_show_from_set
2987 (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
2988 "Set the filename in which to record the command history\n\
2989 (the list of previous commands of which a record is kept).", &sethistlist),
2990 &showhistlist);
2991
2992 add_show_from_set
2993 (add_set_cmd ("confirm", class_support, var_boolean,
2994 (char *)&caution,
2995 "Set whether to confirm potentially dangerous operations.",
2996 &setlist),
2997 &showlist);
2998
2999 add_prefix_cmd ("info", class_info, info_command,
3000 "Generic command for showing things about the program being debugged.",
3001 &infolist, "info ", 0, &cmdlist);
3002 add_com_alias ("i", "info", class_info, 1);
3003
3004 add_com ("complete", class_obscure, complete_command,
3005 "List the completions for the rest of the line as a command.");
3006
3007 add_prefix_cmd ("show", class_info, show_command,
3008 "Generic command for showing things about the debugger.",
3009 &showlist, "show ", 0, &cmdlist);
3010 /* Another way to get at the same thing. */
3011 add_info ("set", show_command, "Show all GDB settings.");
3012
3013 add_cmd ("commands", no_class, show_commands,
3014 "Show the the history of commands you typed.\n\
3015 You can supply a command number to start with, or a `+' to start after\n\
3016 the previous command number shown.",
3017 &showlist);
3018
3019 add_cmd ("version", no_class, show_version,
3020 "Show what version of GDB this is.", &showlist);
3021
3022 /* If target is open when baud changes, it doesn't take effect until the
3023 next open (I think, not sure). */
3024 add_show_from_set (add_set_cmd ("remotebaud", no_class,
3025 var_zinteger, (char *)&baud_rate,
3026 "Set baud rate for remote serial I/O.\n\
3027 This value is used to set the speed of the serial port when debugging\n\
3028 using remote targets.", &setlist),
3029 &showlist);
3030
3031 add_show_from_set (
3032 add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug,
3033 "Set debugging of remote protocol.\n\
3034 When enabled, each packet sent or received with the remote target\n\
3035 is displayed.", &setlist),
3036 &showlist);
3037 }
This page took 0.093681 seconds and 4 git commands to generate.