1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "call-cmds.h"
30 #include <sys/types.h>
34 #include "gdb_string.h"
36 /* Temporary variable for SET_TOP_LEVEL. */
38 static int top_level_val
;
40 /* Do a setjmp on error_return and quit_return. catch_errors is
41 generally a cleaner way to do this, but main() would look pretty
42 ugly if it had to use catch_errors each time. */
44 #define SET_TOP_LEVEL() \
45 (((top_level_val = SIGSETJMP (error_return)) \
46 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (SIGJMP_BUF))) \
49 /* If nonzero, display time usage both at startup and for each command. */
53 /* If nonzero, display space usage both at startup and for each command. */
57 /* Whether this is the command line version or not */
60 /* Whether xdb commands will be handled */
63 /* Whether dbx commands will be handled */
69 /* Whether to enable writing into executable and core files */
70 extern int write_files
;
72 static void print_gdb_help
PARAMS ((GDB_FILE
*));
73 extern void gdb_init
PARAMS ((char *));
75 /* These two are used to set the external editor commands when gdb is farming
76 out files to be edited by another program. */
78 extern int enable_external_editor
;
79 extern char * external_editor_command
;
82 #include <windows.h> /* for MAX_PATH */
83 #include <sys/cygwin.h> /* for cygwin32_conv_to_posix_path */
95 /* Pointers to various arguments from command line. */
102 /* These are static so that we can take their address in an initializer. */
103 static int print_help
;
104 static int print_version
;
106 /* Pointers to all arguments of --command option. */
108 /* Allocated size of cmdarg. */
110 /* Number of elements of cmdarg used. */
113 /* Indices of all arguments of --directory option. */
115 /* Allocated size. */
117 /* Number of elements used. */
120 struct stat homebuf
, cwdbuf
;
121 char *homedir
, *homeinit
;
125 long time_at_startup
= get_run_time ();
129 START_PROGRESS (argv
[0], 0);
132 /* Do all Mac-specific setup. */
136 /* This needs to happen before the first use of malloc. */
137 init_malloc ((PTR
) NULL
);
139 #if defined (ALIGN_STACK_ON_STARTUP)
140 i
= (int) &count
& 0x3;
145 /* If error() is called from initialization code, just exit */
146 if (SET_TOP_LEVEL ()) {
151 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
154 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
158 line
= (char *) xmalloc (linesize
);
159 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
162 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
163 current_directory
= gdb_dirbuf
;
165 gdb_file_size
= sizeof(GDB_FILE
);
167 gdb_stdout
= (GDB_FILE
*)xmalloc (gdb_file_size
);
168 gdb_stdout
->ts_streamtype
= afile
;
169 gdb_stdout
->ts_filestream
= stdout
;
170 gdb_stdout
->ts_strbuf
= NULL
;
171 gdb_stdout
->ts_buflen
= 0;
173 gdb_stderr
= (GDB_FILE
*)xmalloc (gdb_file_size
);
174 gdb_stderr
->ts_streamtype
= afile
;
175 gdb_stderr
->ts_filestream
= stderr
;
176 gdb_stderr
->ts_strbuf
= NULL
;
177 gdb_stderr
->ts_buflen
= 0;
179 /* Parse arguments and options. */
182 /* When var field is 0, use flag field to record the equivalent
183 short option (or arbitrary numbers starting at 10 for those
184 with no equivalent). */
185 static struct option long_options
[] =
188 {"tui", no_argument
, &tui_version
, 1},
190 {"xdb", no_argument
, &xdb_commands
, 1},
191 {"dbx", no_argument
, &dbx_commands
, 1},
192 {"readnow", no_argument
, &readnow_symbol_files
, 1},
193 {"r", no_argument
, &readnow_symbol_files
, 1},
194 {"mapped", no_argument
, &mapped_symbol_files
, 1},
195 {"m", no_argument
, &mapped_symbol_files
, 1},
196 {"quiet", no_argument
, &quiet
, 1},
197 {"q", no_argument
, &quiet
, 1},
198 {"silent", no_argument
, &quiet
, 1},
199 {"nx", no_argument
, &inhibit_gdbinit
, 1},
200 {"n", no_argument
, &inhibit_gdbinit
, 1},
201 {"batch", no_argument
, &batch
, 1},
202 {"epoch", no_argument
, &epoch_interface
, 1},
204 /* This is a synonym for "--annotate=1". --annotate is now preferred,
205 but keep this here for a long time because people will be running
206 emacses which use --fullname. */
207 {"fullname", no_argument
, 0, 'f'},
208 {"f", no_argument
, 0, 'f'},
210 {"annotate", required_argument
, 0, 12},
211 {"help", no_argument
, &print_help
, 1},
212 {"se", required_argument
, 0, 10},
213 {"symbols", required_argument
, 0, 's'},
214 {"s", required_argument
, 0, 's'},
215 {"exec", required_argument
, 0, 'e'},
216 {"e", required_argument
, 0, 'e'},
217 {"core", required_argument
, 0, 'c'},
218 {"c", required_argument
, 0, 'c'},
219 {"command", required_argument
, 0, 'x'},
220 {"version", no_argument
, &print_version
, 1},
221 {"x", required_argument
, 0, 'x'},
222 {"directory", required_argument
, 0, 'd'},
223 {"cd", required_argument
, 0, 11},
224 {"tty", required_argument
, 0, 't'},
225 {"baud", required_argument
, 0, 'b'},
226 {"b", required_argument
, 0, 'b'},
227 {"nw", no_argument
, &use_windows
, 0},
228 {"nowindows", no_argument
, &use_windows
, 0},
229 {"w", no_argument
, &use_windows
, 1},
230 {"windows", no_argument
, &use_windows
, 1},
231 {"statistics", no_argument
, 0, 13},
232 {"write", no_argument
, &write_files
, 1},
233 /* Allow machine descriptions to add more options... */
234 #ifdef ADDITIONAL_OPTIONS
237 {0, no_argument
, 0, 0}
244 c
= getopt_long_only (argc
, argv
, "",
245 long_options
, &option_index
);
249 /* Long option that takes an argument. */
250 if (c
== 0 && long_options
[option_index
].flag
== 0)
251 c
= long_options
[option_index
].val
;
256 /* Long option that just sets a flag. */
266 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
267 annotation_level
= atoi (optarg
);
270 /* Enable the display of both time and space usage. */
275 annotation_level
= 1;
276 /* We have probably been invoked from emacs. Disable window interface. */
289 cmdarg
[ncmd
++] = optarg
;
293 cmdarg
= (char **) xrealloc ((char *)cmdarg
,
294 cmdsize
* sizeof (*cmdarg
));
298 dirarg
[ndir
++] = optarg
;
302 dirarg
= (char **) xrealloc ((char *)dirarg
,
303 dirsize
* sizeof (*dirarg
));
317 i
= strtol (optarg
, &p
, 0);
318 if (i
== 0 && p
== optarg
)
320 /* Don't use *_filtered or warning() (which relies on
321 current_target) until after initialize_all_files(). */
325 "warning: could not set baud rate to `%s'.\n", optarg
);
334 i
= strtol (optarg
, &p
, 0);
335 if (i
== 0 && p
== optarg
)
337 /* Don't use *_filtered or warning() (which relies on
338 current_target) until after initialize_all_files(). */
342 "warning: could not set timeout limit to `%s'.\n", optarg
);
348 #ifdef ADDITIONAL_OPTION_CASES
349 ADDITIONAL_OPTION_CASES
352 fprintf_unfiltered (gdb_stderr
,
353 "Use `%s --help' for a complete list of options.\n",
359 /* If --help or --version, disable window interface. */
360 if (print_help
|| print_version
)
364 /* Disable the TUI as well. */
370 /* An explicit --tui flag overrides the default UI, which is the
376 /* OK, that's all the options. The other arguments are filenames. */
378 for (; optind
< argc
; optind
++)
382 symarg
= argv
[optind
];
383 execarg
= argv
[optind
];
386 corearg
= argv
[optind
];
389 fprintf_unfiltered (gdb_stderr
,
390 "Excess command line arguments ignored. (%s%s)\n",
391 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
400 init_ui_hook
= tuiInit
;
404 /* Do these (and anything which might call wrap_here or *_filtered)
405 after initialize_all_files. */
408 print_gdb_version (gdb_stdout
);
410 printf_filtered ("\n");
416 print_gdb_help (gdb_stdout
);
417 fputs_unfiltered ("\n", gdb_stdout
);
423 /* Print all the junk at the top, with trailing "..." if we are about
424 to read a symbol file (possibly slowly). */
425 print_gdb_version (gdb_stdout
);
427 printf_filtered ("..");
429 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
432 error_pre_print
= "\n\n";
433 quit_pre_print
= error_pre_print
;
435 /* We may get more than one warning, don't double space all of them... */
436 warning_pre_print
= "\nwarning: ";
438 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
439 *before* all the command line arguments are processed; it sets
440 global parameters, which are independent of what file you are
441 debugging or what directory you are in. */
444 char * tmp
= getenv ("HOME");
448 homedir
= (char *) alloca (MAX_PATH
+1);
449 cygwin32_conv_to_posix_path (tmp
, homedir
);
455 homedir
= getenv ("HOME");
459 homeinit
= (char *) alloca (strlen (homedir
) +
460 strlen (gdbinit
) + 10);
461 strcpy (homeinit
, homedir
);
462 strcat (homeinit
, "/");
463 strcat (homeinit
, gdbinit
);
465 if (!inhibit_gdbinit
)
467 if (!SET_TOP_LEVEL ())
468 source_command (homeinit
, 0);
470 do_cleanups (ALL_CLEANUPS
);
472 /* Do stats; no need to do them elsewhere since we'll only
473 need them if homedir is set. Make sure that they are
474 zero in case one of them fails (this guarantees that they
475 won't match if either exists). */
477 memset (&homebuf
, 0, sizeof (struct stat
));
478 memset (&cwdbuf
, 0, sizeof (struct stat
));
480 stat (homeinit
, &homebuf
);
481 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
485 /* Now perform all the actions indicated by the arguments. */
488 if (!SET_TOP_LEVEL ())
490 cd_command (cdarg
, 0);
493 do_cleanups (ALL_CLEANUPS
);
495 for (i
= 0; i
< ndir
; i
++)
496 if (!SET_TOP_LEVEL ())
497 directory_command (dirarg
[i
], 0);
499 do_cleanups (ALL_CLEANUPS
);
503 && STREQ (execarg
, symarg
))
505 /* The exec file and the symbol-file are the same. If we can't open
506 it, better only print one error message. */
507 if (!SET_TOP_LEVEL ())
509 exec_file_command (execarg
, !batch
);
510 symbol_file_command (symarg
, 0);
516 if (!SET_TOP_LEVEL ())
517 exec_file_command (execarg
, !batch
);
519 if (!SET_TOP_LEVEL ())
520 symbol_file_command (symarg
, 0);
522 do_cleanups (ALL_CLEANUPS
);
524 /* After the symbol file has been read, print a newline to get us
525 beyond the copyright line... But errors should still set off
526 the error message with a (single) blank line. */
528 printf_filtered ("\n");
529 error_pre_print
= "\n";
530 quit_pre_print
= error_pre_print
;
531 warning_pre_print
= "\nwarning: ";
535 if (!SET_TOP_LEVEL ())
536 core_file_command (corearg
, !batch
);
537 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
538 attach_command (corearg
, !batch
);
540 do_cleanups (ALL_CLEANUPS
);
543 if (!SET_TOP_LEVEL ())
544 tty_command (ttyarg
, !batch
);
545 do_cleanups (ALL_CLEANUPS
);
547 #ifdef ADDITIONAL_OPTION_HANDLER
548 ADDITIONAL_OPTION_HANDLER
;
551 /* Error messages should no longer be distinguished with extra output. */
552 error_pre_print
= NULL
;
553 quit_pre_print
= NULL
;
554 warning_pre_print
= "warning: ";
556 /* Read the .gdbinit file in the current directory, *if* it isn't
557 the same as the $HOME/.gdbinit file (it should exist, also). */
560 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
561 if (!inhibit_gdbinit
)
563 if (!SET_TOP_LEVEL ())
564 source_command (gdbinit
, 0);
566 do_cleanups (ALL_CLEANUPS
);
568 for (i
= 0; i
< ncmd
; i
++)
570 if (!SET_TOP_LEVEL ())
572 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
573 read_command_file (stdin
);
575 source_command (cmdarg
[i
], !batch
);
576 do_cleanups (ALL_CLEANUPS
);
581 /* Read in the old history after all the command files have been read. */
586 /* We have hit the end of the batch file. */
590 /* Do any host- or target-specific hacks. This is used for i960 targets
591 to force the user to set a nindy target and spec its parameters. */
593 #ifdef BEFORE_MAIN_LOOP_HOOK
594 BEFORE_MAIN_LOOP_HOOK
;
597 END_PROGRESS (argv
[0]);
599 /* Show time and/or space usage. */
603 long init_time
= get_run_time () - time_at_startup
;
605 printf_unfiltered ("Startup time: %ld.%06ld\n",
606 init_time
/ 1000000, init_time
% 1000000);
612 extern char **environ
;
613 char *lim
= (char *) sbrk (0);
615 printf_unfiltered ("Startup size: data size %ld\n",
616 (long) (lim
- (char *) &environ
));
620 /* The default command loop.
621 The WIN32 Gui calls this main to set up gdb's state, and
622 has its own command loop. */
623 #if !defined _WIN32 || defined __GNUC__
626 if (!SET_TOP_LEVEL ())
628 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
629 /* GUIs generally have their own command loop, mainloop, or whatever.
630 This is a good place to gain control because many error
631 conditions will end up here via longjmp(). */
632 if (command_loop_hook
)
633 command_loop_hook ();
636 quit_command ((char *)0, instream
== stdin
);
640 /* No exit -- exit is through quit_command. */
645 /* Don't use *_filtered for printing help. We don't want to prompt
646 for continue no matter how small the screen or how much we're going
650 print_gdb_help (stream
)
654 This is the GNU debugger. Usage:\n\n\
655 gdb [options] [executable-file [core-file or process-id]]\n\n\
659 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
660 --batch Exit after processing options.\n\
661 --cd=DIR Change current directory to DIR.\n\
662 --command=FILE Execute GDB commands from FILE.\n\
663 --core=COREFILE Analyze the core dump COREFILE.\n\
666 --dbx DBX compatibility mode.\n\
667 --directory=DIR Search for source files in DIR.\n\
668 --epoch Output information used by epoch emacs-GDB interface.\n\
669 --exec=EXECFILE Use EXECFILE as the executable.\n\
670 --fullname Output information used by emacs-GDB interface.\n\
671 --help Print this message.\n\
674 --mapped Use mapped symbol files if supported on this system.\n\
675 --nw Do not use a window interface.\n\
676 --nx Do not read .gdbinit file.\n\
677 --quiet Do not print version number on startup.\n\
678 --readnow Fully read symbol files on first access.\n\
681 --se=FILE Use FILE as symbol file and executable file.\n\
682 --symbols=SYMFILE Read symbols from SYMFILE.\n\
683 --tty=TTY Use TTY for input/output by the program being debugged.\n\
687 --tui Use a terminal user interface.\n\
691 --version Print version information and then exit.\n\
692 -w Use a window interface.\n\
693 --write Set writing into executable and core files.\n\
694 --xdb XDB compatibility mode.\n\
696 #ifdef ADDITIONAL_OPTION_HELP
697 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, stream
);
699 fputs_unfiltered ("\n\
700 For more information, type \"help\" from within GDB, or consult the\n\
701 GDB manual (available as on-line info or a printed manual).\n\
702 Report bugs to \"bug-gdb@prep.ai.mit.edu\".\
713 proc_remove_foreign (pid
)
718 /* All I/O sent to the *_filtered and *_unfiltered functions eventually ends up
719 here. The fputs_unfiltered_hook is primarily used by GUIs to collect all
720 output and send it to the GUI, instead of the controlling terminal. Only
721 output to gdb_stdout and gdb_stderr are sent to the hook. Everything else
722 is sent on to fputs to allow file I/O to be handled appropriately. */
725 fputs_unfiltered (linebuffer
, stream
)
726 const char *linebuffer
;
730 extern int tui_owns_terminal
;
732 /* If anything (GUI, TUI) wants to capture GDB output, this is
733 * the place... the way to do it is to set up
734 * fputs_unfiltered_hook.
735 * Our TUI ("gdb -tui") used to hook output, but in the
736 * new (XDB style) scheme, we do not do that anymore... - RT
738 if (fputs_unfiltered_hook
739 && (stream
== gdb_stdout
740 || stream
== gdb_stderr
))
741 fputs_unfiltered_hook (linebuffer
, stream
);
745 if (tui_version
&& tui_owns_terminal
) {
746 /* If we get here somehow while updating the TUI (from
747 * within a tuiDo(), then we need to temporarily
748 * set up the terminal for GDB output. This probably just
749 * happens on error output.
752 if (stream
->ts_streamtype
== astring
) {
753 gdb_file_adjust_strbuf(strlen(linebuffer
), stream
);
754 strcat(stream
->ts_strbuf
, linebuffer
);
756 tuiTermUnsetup(0, (tui_version
) ? cmdWin
->detail
.commandInfo
.curch
: 0);
757 fputs (linebuffer
, stream
->ts_filestream
);
759 if (linebuffer
[strlen(linebuffer
) - 1] == '\n')
760 tuiClearCommandCharCount();
762 tuiIncrCommandCharCountBy(strlen(linebuffer
));
765 /* The normal case - just do a fputs() */
766 if (stream
->ts_streamtype
== astring
) {
767 gdb_file_adjust_strbuf(strlen(linebuffer
), stream
);
768 strcat(stream
->ts_strbuf
, linebuffer
);
769 } else fputs (linebuffer
, stream
->ts_filestream
);
774 if (stream
->ts_streamtype
== astring
) {
775 gdb_file_adjust_strbuf(strlen(linebuffer
), stream
);
776 strcat(stream
->ts_strbuf
, linebuffer
);
777 } else fputs (linebuffer
, stream
->ts_filestream
);