1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 1999
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,
20 Boston, MA 02111-1307, USA. */
26 #include "call-cmds.h"
30 #include <sys/types.h>
34 #include "gdb_string.h"
35 #include "event-loop.h"
37 /* If nonzero, display time usage both at startup and for each command. */
41 /* If nonzero, display space usage both at startup and for each command. */
45 /* Whether this is the async version or not. The async version is
46 invoked on the command line with the -nw --async options. In this
47 version, the usual command_loop is substituted by and event loop which
48 processes UI events asynchronously. */
51 /* Whether this is the command line version or not */
54 /* Whether xdb commands will be handled */
57 /* Whether dbx commands will be handled */
63 GDB_FILE
*gdb_stdtarg
;
65 /* Whether to enable writing into executable and core files */
66 extern int write_files
;
68 static void print_gdb_help
PARAMS ((GDB_FILE
*));
70 /* These two are used to set the external editor commands when gdb is farming
71 out files to be edited by another program. */
73 extern int enable_external_editor
;
74 extern char *external_editor_command
;
77 #include <windows.h> /* for MAX_PATH */
78 #include <sys/cygwin.h> /* for cygwin32_conv_to_posix_path */
90 /* Pointers to various arguments from command line. */
97 /* These are static so that we can take their address in an initializer. */
98 static int print_help
;
99 static int print_version
;
101 /* Pointers to all arguments of --command option. */
103 /* Allocated size of cmdarg. */
105 /* Number of elements of cmdarg used. */
108 /* Indices of all arguments of --directory option. */
110 /* Allocated size. */
112 /* Number of elements used. */
115 struct stat homebuf
, cwdbuf
;
116 char *homedir
, *homeinit
;
120 long time_at_startup
= get_run_time ();
124 START_PROGRESS (argv
[0], 0);
127 /* Do all Mac-specific setup. */
131 /* This needs to happen before the first use of malloc. */
132 init_malloc ((PTR
) NULL
);
134 #if defined (ALIGN_STACK_ON_STARTUP)
135 i
= (int) &count
& 0x3;
140 /* If error() is called from initialization code, just exit */
141 if (SET_TOP_LEVEL ())
147 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
150 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
154 line
= (char *) xmalloc (linesize
);
155 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
158 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
159 current_directory
= gdb_dirbuf
;
163 gdb_stdout
= stdio_fileopen (stdout
);
164 gdb_stderr
= stdio_fileopen (stderr
);
165 gdb_stdlog
= gdb_stderr
; /* for moment */
166 gdb_stdtarg
= gdb_stderr
; /* for moment */
168 gdb_stdout
= tui_fileopen (stdout
);
169 gdb_stderr
= tui_fileopen (stderr
);
170 gdb_stdlog
= gdb_stdout
; /* for moment */
171 gdb_stdtarg
= gdb_stderr
; /* for moment */
174 /* Parse arguments and options. */
177 /* When var field is 0, use flag field to record the equivalent
178 short option (or arbitrary numbers starting at 10 for those
179 with no equivalent). */
180 static struct option long_options
[] =
182 {"async", no_argument
, &async_p
, 1},
183 {"noasync", no_argument
, &async_p
, 0},
185 {"tui", no_argument
, &tui_version
, 1},
187 {"xdb", no_argument
, &xdb_commands
, 1},
188 {"dbx", no_argument
, &dbx_commands
, 1},
189 {"readnow", no_argument
, &readnow_symbol_files
, 1},
190 {"r", no_argument
, &readnow_symbol_files
, 1},
191 {"mapped", no_argument
, &mapped_symbol_files
, 1},
192 {"m", no_argument
, &mapped_symbol_files
, 1},
193 {"quiet", no_argument
, &quiet
, 1},
194 {"q", no_argument
, &quiet
, 1},
195 {"silent", no_argument
, &quiet
, 1},
196 {"nx", no_argument
, &inhibit_gdbinit
, 1},
197 {"n", no_argument
, &inhibit_gdbinit
, 1},
198 {"batch", no_argument
, &batch
, 1},
199 {"epoch", no_argument
, &epoch_interface
, 1},
201 /* This is a synonym for "--annotate=1". --annotate is now preferred,
202 but keep this here for a long time because people will be running
203 emacses which use --fullname. */
204 {"fullname", no_argument
, 0, 'f'},
205 {"f", no_argument
, 0, 'f'},
207 {"annotate", required_argument
, 0, 12},
208 {"help", no_argument
, &print_help
, 1},
209 {"se", required_argument
, 0, 10},
210 {"symbols", required_argument
, 0, 's'},
211 {"s", required_argument
, 0, 's'},
212 {"exec", required_argument
, 0, 'e'},
213 {"e", required_argument
, 0, 'e'},
214 {"core", required_argument
, 0, 'c'},
215 {"c", required_argument
, 0, 'c'},
216 {"command", required_argument
, 0, 'x'},
217 {"version", no_argument
, &print_version
, 1},
218 {"x", required_argument
, 0, 'x'},
219 {"directory", required_argument
, 0, 'd'},
220 {"cd", required_argument
, 0, 11},
221 {"tty", required_argument
, 0, 't'},
222 {"baud", required_argument
, 0, 'b'},
223 {"b", required_argument
, 0, 'b'},
224 {"nw", no_argument
, &use_windows
, 0},
225 {"nowindows", no_argument
, &use_windows
, 0},
226 {"w", no_argument
, &use_windows
, 1},
227 {"windows", no_argument
, &use_windows
, 1},
228 {"statistics", no_argument
, 0, 13},
229 {"write", no_argument
, &write_files
, 1},
230 /* Allow machine descriptions to add more options... */
231 #ifdef ADDITIONAL_OPTIONS
234 {0, no_argument
, 0, 0}
241 c
= getopt_long_only (argc
, argv
, "",
242 long_options
, &option_index
);
246 /* Long option that takes an argument. */
247 if (c
== 0 && long_options
[option_index
].flag
== 0)
248 c
= long_options
[option_index
].val
;
253 /* Long option that just sets a flag. */
263 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
264 annotation_level
= atoi (optarg
);
267 /* Enable the display of both time and space usage. */
272 annotation_level
= 1;
273 /* We have probably been invoked from emacs. Disable window interface. */
286 cmdarg
[ncmd
++] = optarg
;
290 cmdarg
= (char **) xrealloc ((char *) cmdarg
,
291 cmdsize
* sizeof (*cmdarg
));
295 dirarg
[ndir
++] = optarg
;
299 dirarg
= (char **) xrealloc ((char *) dirarg
,
300 dirsize
* sizeof (*dirarg
));
314 i
= strtol (optarg
, &p
, 0);
315 if (i
== 0 && p
== optarg
)
317 /* Don't use *_filtered or warning() (which relies on
318 current_target) until after initialize_all_files(). */
322 "warning: could not set baud rate to `%s'.\n", optarg
);
331 i
= strtol (optarg
, &p
, 0);
332 if (i
== 0 && p
== optarg
)
334 /* Don't use *_filtered or warning() (which relies on
335 current_target) until after initialize_all_files(). */
339 "warning: could not set timeout limit to `%s'.\n", optarg
);
345 #ifdef ADDITIONAL_OPTION_CASES
346 ADDITIONAL_OPTION_CASES
349 fprintf_unfiltered (gdb_stderr
,
350 "Use `%s --help' for a complete list of options.\n",
356 /* If --help or --version, disable window interface. */
357 if (print_help
|| print_version
)
361 /* Disable the TUI as well. */
367 /* An explicit --tui flag overrides the default UI, which is the
373 /* OK, that's all the options. The other arguments are filenames. */
375 for (; optind
< argc
; optind
++)
379 symarg
= argv
[optind
];
380 execarg
= argv
[optind
];
383 corearg
= argv
[optind
];
386 fprintf_unfiltered (gdb_stderr
,
387 "Excess command line arguments ignored. (%s%s)\n",
388 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
396 /* Should this be moved to tui-top.c:_initialize_tui()? */
398 init_ui_hook
= tuiInit
;
401 /* Initialize all files. Give the interpreter a chance to take
402 control of the console via the init_ui_hook()) */
405 /* Do these (and anything which might call wrap_here or *_filtered)
406 after initialize_all_files. */
409 print_gdb_version (gdb_stdout
);
411 printf_filtered ("\n");
417 print_gdb_help (gdb_stdout
);
418 fputs_unfiltered ("\n", gdb_stdout
);
424 /* Print all the junk at the top, with trailing "..." if we are about
425 to read a symbol file (possibly slowly). */
426 print_gdb_version (gdb_stdout
);
428 printf_filtered ("..");
430 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
433 error_pre_print
= "\n\n";
434 quit_pre_print
= error_pre_print
;
436 /* We may get more than one warning, don't double space all of them... */
437 warning_pre_print
= "\nwarning: ";
439 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
440 *before* all the command line arguments are processed; it sets
441 global parameters, which are independent of what file you are
442 debugging or what directory you are in. */
445 char *tmp
= getenv ("HOME");
449 homedir
= (char *) alloca (MAX_PATH
+ 1);
450 cygwin32_conv_to_posix_path (tmp
, homedir
);
456 homedir
= getenv ("HOME");
460 homeinit
= (char *) alloca (strlen (homedir
) +
461 strlen (gdbinit
) + 10);
462 strcpy (homeinit
, homedir
);
463 strcat (homeinit
, "/");
464 strcat (homeinit
, gdbinit
);
466 if (!inhibit_gdbinit
)
468 if (!SET_TOP_LEVEL ())
469 source_command (homeinit
, 0);
471 do_cleanups (ALL_CLEANUPS
);
473 /* Do stats; no need to do them elsewhere since we'll only
474 need them if homedir is set. Make sure that they are
475 zero in case one of them fails (this guarantees that they
476 won't match if either exists). */
478 memset (&homebuf
, 0, sizeof (struct stat
));
479 memset (&cwdbuf
, 0, sizeof (struct stat
));
481 stat (homeinit
, &homebuf
);
482 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
486 /* Now perform all the actions indicated by the arguments. */
489 if (!SET_TOP_LEVEL ())
491 cd_command (cdarg
, 0);
494 do_cleanups (ALL_CLEANUPS
);
496 for (i
= 0; i
< ndir
; i
++)
497 if (!SET_TOP_LEVEL ())
498 directory_command (dirarg
[i
], 0);
500 do_cleanups (ALL_CLEANUPS
);
504 && STREQ (execarg
, symarg
))
506 /* The exec file and the symbol-file are the same. If we can't open
507 it, better only print one error message. */
508 if (!SET_TOP_LEVEL ())
510 exec_file_command (execarg
, !batch
);
511 symbol_file_command (symarg
, 0);
517 if (!SET_TOP_LEVEL ())
518 exec_file_command (execarg
, !batch
);
520 if (!SET_TOP_LEVEL ())
521 symbol_file_command (symarg
, 0);
523 do_cleanups (ALL_CLEANUPS
);
525 /* After the symbol file has been read, print a newline to get us
526 beyond the copyright line... But errors should still set off
527 the error message with a (single) blank line. */
529 printf_filtered ("\n");
530 error_pre_print
= "\n";
531 quit_pre_print
= error_pre_print
;
532 warning_pre_print
= "\nwarning: ";
536 if (!SET_TOP_LEVEL ())
537 core_file_command (corearg
, !batch
);
538 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
539 attach_command (corearg
, !batch
);
541 do_cleanups (ALL_CLEANUPS
);
544 if (!SET_TOP_LEVEL ())
545 tty_command (ttyarg
, !batch
);
546 do_cleanups (ALL_CLEANUPS
);
548 #ifdef ADDITIONAL_OPTION_HANDLER
549 ADDITIONAL_OPTION_HANDLER
;
552 /* Error messages should no longer be distinguished with extra output. */
553 error_pre_print
= NULL
;
554 quit_pre_print
= NULL
;
555 warning_pre_print
= "warning: ";
557 /* Read the .gdbinit file in the current directory, *if* it isn't
558 the same as the $HOME/.gdbinit file (it should exist, also). */
561 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
562 if (!inhibit_gdbinit
)
564 if (!SET_TOP_LEVEL ())
565 source_command (gdbinit
, 0);
567 do_cleanups (ALL_CLEANUPS
);
569 for (i
= 0; i
< ncmd
; i
++)
571 if (!SET_TOP_LEVEL ())
573 /* NOTE: I am commenting this out, because it is not clear
574 where this feature is used. It is very old and
575 undocumented. ezannoni: 1999-05-04 */
577 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
578 read_command_file (stdin
);
581 source_command (cmdarg
[i
], !batch
);
582 do_cleanups (ALL_CLEANUPS
);
587 /* Read in the old history after all the command files have been read. */
592 /* We have hit the end of the batch file. */
596 /* Do any host- or target-specific hacks. This is used for i960 targets
597 to force the user to set a nindy target and spec its parameters. */
599 #ifdef BEFORE_MAIN_LOOP_HOOK
600 BEFORE_MAIN_LOOP_HOOK
;
603 END_PROGRESS (argv
[0]);
605 /* Show time and/or space usage. */
609 long init_time
= get_run_time () - time_at_startup
;
611 printf_unfiltered ("Startup time: %ld.%06ld\n",
612 init_time
/ 1000000, init_time
% 1000000);
618 extern char **environ
;
619 char *lim
= (char *) sbrk (0);
621 printf_unfiltered ("Startup size: data size %ld\n",
622 (long) (lim
- (char *) &environ
));
626 /* The default command loop.
627 The WIN32 Gui calls this main to set up gdb's state, and
628 has its own command loop. */
629 #if !defined _WIN32 || defined __GNUC__
632 if (!SET_TOP_LEVEL ())
634 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
635 /* GUIs generally have their own command loop, mainloop, or whatever.
636 This is a good place to gain control because many error
637 conditions will end up here via longjmp(). */
638 if (command_loop_hook
)
639 command_loop_hook ();
642 quit_command ((char *) 0, instream
== stdin
);
645 /* No exit -- exit is through quit_command. */
650 /* Don't use *_filtered for printing help. We don't want to prompt
651 for continue no matter how small the screen or how much we're going
655 print_gdb_help (stream
)
659 This is the GNU debugger. Usage:\n\n\
660 gdb [options] [executable-file [core-file or process-id]]\n\n\
664 --[no]async Enable (disable) asynchronous version of CLI\n\
667 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
668 --batch Exit after processing options.\n\
669 --cd=DIR Change current directory to DIR.\n\
670 --command=FILE Execute GDB commands from FILE.\n\
671 --core=COREFILE Analyze the core dump COREFILE.\n\
674 --dbx DBX compatibility mode.\n\
675 --directory=DIR Search for source files in DIR.\n\
676 --epoch Output information used by epoch emacs-GDB interface.\n\
677 --exec=EXECFILE Use EXECFILE as the executable.\n\
678 --fullname Output information used by emacs-GDB interface.\n\
679 --help Print this message.\n\
682 --mapped Use mapped symbol files if supported on this system.\n\
683 --nw Do not use a window interface.\n\
684 --nx Do not read ", stream
);
685 fputs_unfiltered (gdbinit
, stream
);
686 fputs_unfiltered (" file.\n\
687 --quiet Do not print version number on startup.\n\
688 --readnow Fully read symbol files on first access.\n\
691 --se=FILE Use FILE as symbol file and executable file.\n\
692 --symbols=SYMFILE Read symbols from SYMFILE.\n\
693 --tty=TTY Use TTY for input/output by the program being debugged.\n\
697 --tui Use a terminal user interface.\n\
701 --version Print version information and then exit.\n\
702 -w Use a window interface.\n\
703 --write Set writing into executable and core files.\n\
704 --xdb XDB compatibility mode.\n\
706 #ifdef ADDITIONAL_OPTION_HELP
707 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, stream
);
709 fputs_unfiltered ("\n\
710 For more information, type \"help\" from within GDB, or consult the\n\
711 GDB manual (available as on-line info or a printed manual).\n\
712 Report bugs to \"bug-gdb@prep.ai.mit.edu\".\