1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986-1995, 1999-2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 #include "call-cmds.h"
29 #include <sys/types.h>
33 #include "gdb_string.h"
34 #include "event-loop.h"
35 #if defined (TUI) || defined (GDBTK)
36 /* FIXME: cagney/2000-01-31: This #include is to allow older code such
37 as that found in the TUI to continue to build. */
38 #include "tui/tui-file.h"
41 /* If nonzero, display time usage both at startup and for each command. */
45 /* If nonzero, display space usage both at startup and for each command. */
49 /* Whether this is the async version or not. The async version is
50 invoked on the command line with the -nw --async options. In this
51 version, the usual command_loop is substituted by and event loop which
52 processes UI events asynchronously. */
55 /* Whether this is the command line version or not */
58 /* Whether xdb commands will be handled */
61 /* Whether dbx commands will be handled */
64 struct ui_file
*gdb_stdout
;
65 struct ui_file
*gdb_stderr
;
66 struct ui_file
*gdb_stdlog
;
67 struct ui_file
*gdb_stdtarg
;
69 /* Used to initialize error() - defined in utils.c */
71 extern void error_init (void);
73 /* Whether to enable writing into executable and core files */
74 extern int write_files
;
76 static void print_gdb_help (struct ui_file
*);
78 /* These two are used to set the external editor commands when gdb is farming
79 out files to be edited by another program. */
81 extern int enable_external_editor
;
82 extern char *external_editor_command
;
85 #include <windows.h> /* for MAX_PATH */
86 #include <sys/cygwin.h> /* for cygwin32_conv_to_posix_path */
89 /* Call command_loop. If it happens to return, pass that through as a
90 non-zero return status. */
93 captured_command_loop (void *data
)
95 if (command_loop_hook
== NULL
)
99 /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
100 would clean things up (restoring the cleanup chain) to the state
101 they were just prior to the call. Technically, this means that
102 the do_cleanups() below is redundant. Unfortunatly, many FUNC's
103 are not that well behaved. do_cleanups should either be replaced
104 with a do_cleanups call (to cover the problem) or an assertion
105 check to detect bad FUNCs code. */
106 do_cleanups (ALL_CLEANUPS
);
107 /* If the command_loop returned, normally (rather than threw an
108 error) we try to quit. If the quit is aborted, catch_errors()
109 which called this catch the signal and restart the command
111 quit_command (NULL
, instream
== stdin
);
115 struct captured_main_args
122 captured_main (void *data
)
124 struct captured_main_args
*context
= data
;
125 int argc
= context
->argc
;
126 char **argv
= context
->argv
;
128 static int quiet
= 0;
129 static int batch
= 0;
131 /* Pointers to various arguments from command line. */
133 char *execarg
= NULL
;
134 char *corearg
= NULL
;
138 /* These are static so that we can take their address in an initializer. */
139 static int print_help
;
140 static int print_version
;
142 /* Pointers to all arguments of --command option. */
144 /* Allocated size of cmdarg. */
146 /* Number of elements of cmdarg used. */
149 /* Indices of all arguments of --directory option. */
151 /* Allocated size. */
153 /* Number of elements used. */
156 struct stat homebuf
, cwdbuf
;
157 char *homedir
, *homeinit
;
161 long time_at_startup
= get_run_time ();
163 START_PROGRESS (argv
[0], 0);
166 /* Do all Mac-specific setup. */
170 /* This needs to happen before the first use of malloc. */
171 init_malloc ((PTR
) NULL
);
173 #if defined (ALIGN_STACK_ON_STARTUP)
174 i
= (int) &count
& 0x3;
180 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
183 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
187 line
= (char *) xmalloc (linesize
);
188 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
191 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
192 current_directory
= gdb_dirbuf
;
194 #if defined (TUI) || defined (GDBTK)
195 /* Older code uses the tui_file and fputs_unfiltered_hook(). It
196 should be using a customized UI_FILE object and re-initializing
197 within its own _initialize function. */
198 gdb_stdout
= tui_fileopen (stdout
);
199 gdb_stderr
= tui_fileopen (stderr
);
200 gdb_stdlog
= gdb_stdout
; /* for moment */
201 gdb_stdtarg
= gdb_stderr
; /* for moment */
203 gdb_stdout
= stdio_fileopen (stdout
);
204 gdb_stderr
= stdio_fileopen (stderr
);
205 gdb_stdlog
= gdb_stderr
; /* for moment */
206 gdb_stdtarg
= gdb_stderr
; /* for moment */
209 /* initialize error() */
212 /* Parse arguments and options. */
215 /* When var field is 0, use flag field to record the equivalent
216 short option (or arbitrary numbers starting at 10 for those
217 with no equivalent). */
218 static struct option long_options
[] =
220 {"async", no_argument
, &event_loop_p
, 1},
221 {"noasync", no_argument
, &event_loop_p
, 0},
223 {"tui", no_argument
, &tui_version
, 1},
225 {"xdb", no_argument
, &xdb_commands
, 1},
226 {"dbx", no_argument
, &dbx_commands
, 1},
227 {"readnow", no_argument
, &readnow_symbol_files
, 1},
228 {"r", no_argument
, &readnow_symbol_files
, 1},
229 {"mapped", no_argument
, &mapped_symbol_files
, 1},
230 {"m", no_argument
, &mapped_symbol_files
, 1},
231 {"quiet", no_argument
, &quiet
, 1},
232 {"q", no_argument
, &quiet
, 1},
233 {"silent", no_argument
, &quiet
, 1},
234 {"nx", no_argument
, &inhibit_gdbinit
, 1},
235 {"n", no_argument
, &inhibit_gdbinit
, 1},
236 {"batch", no_argument
, &batch
, 1},
237 {"epoch", no_argument
, &epoch_interface
, 1},
239 /* This is a synonym for "--annotate=1". --annotate is now preferred,
240 but keep this here for a long time because people will be running
241 emacses which use --fullname. */
242 {"fullname", no_argument
, 0, 'f'},
243 {"f", no_argument
, 0, 'f'},
245 {"annotate", required_argument
, 0, 12},
246 {"help", no_argument
, &print_help
, 1},
247 {"se", required_argument
, 0, 10},
248 {"symbols", required_argument
, 0, 's'},
249 {"s", required_argument
, 0, 's'},
250 {"exec", required_argument
, 0, 'e'},
251 {"e", required_argument
, 0, 'e'},
252 {"core", required_argument
, 0, 'c'},
253 {"c", required_argument
, 0, 'c'},
254 {"command", required_argument
, 0, 'x'},
255 {"version", no_argument
, &print_version
, 1},
256 {"x", required_argument
, 0, 'x'},
257 {"directory", required_argument
, 0, 'd'},
258 {"d", required_argument
, 0, 'd'},
259 {"cd", required_argument
, 0, 11},
260 {"tty", required_argument
, 0, 't'},
261 {"baud", required_argument
, 0, 'b'},
262 {"b", required_argument
, 0, 'b'},
263 {"nw", no_argument
, &use_windows
, 0},
264 {"nowindows", no_argument
, &use_windows
, 0},
265 {"w", no_argument
, &use_windows
, 1},
266 {"windows", no_argument
, &use_windows
, 1},
267 {"statistics", no_argument
, 0, 13},
268 {"write", no_argument
, &write_files
, 1},
269 /* Allow machine descriptions to add more options... */
270 #ifdef ADDITIONAL_OPTIONS
273 {0, no_argument
, 0, 0}
280 c
= getopt_long_only (argc
, argv
, "",
281 long_options
, &option_index
);
285 /* Long option that takes an argument. */
286 if (c
== 0 && long_options
[option_index
].flag
== 0)
287 c
= long_options
[option_index
].val
;
292 /* Long option that just sets a flag. */
302 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
303 annotation_level
= atoi (optarg
);
306 /* Enable the display of both time and space usage. */
311 annotation_level
= 1;
312 /* We have probably been invoked from emacs. Disable window interface. */
325 cmdarg
[ncmd
++] = optarg
;
329 cmdarg
= (char **) xrealloc ((char *) cmdarg
,
330 cmdsize
* sizeof (*cmdarg
));
334 dirarg
[ndir
++] = optarg
;
338 dirarg
= (char **) xrealloc ((char *) dirarg
,
339 dirsize
* sizeof (*dirarg
));
353 i
= strtol (optarg
, &p
, 0);
354 if (i
== 0 && p
== optarg
)
356 /* Don't use *_filtered or warning() (which relies on
357 current_target) until after initialize_all_files(). */
361 "warning: could not set baud rate to `%s'.\n", optarg
);
370 i
= strtol (optarg
, &p
, 0);
371 if (i
== 0 && p
== optarg
)
373 /* Don't use *_filtered or warning() (which relies on
374 current_target) until after initialize_all_files(). */
378 "warning: could not set timeout limit to `%s'.\n", optarg
);
384 #ifdef ADDITIONAL_OPTION_CASES
385 ADDITIONAL_OPTION_CASES
388 fprintf_unfiltered (gdb_stderr
,
389 "Use `%s --help' for a complete list of options.\n",
395 /* If --help or --version, disable window interface. */
396 if (print_help
|| print_version
)
400 /* Disable the TUI as well. */
406 /* An explicit --tui flag overrides the default UI, which is the
412 /* OK, that's all the options. The other arguments are filenames. */
414 for (; optind
< argc
; optind
++)
418 symarg
= argv
[optind
];
419 execarg
= argv
[optind
];
422 corearg
= argv
[optind
];
425 fprintf_unfiltered (gdb_stderr
,
426 "Excess command line arguments ignored. (%s%s)\n",
427 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
435 /* Should this be moved to tui-top.c:_initialize_tui()? */
437 init_ui_hook
= tuiInit
;
440 /* Initialize all files. Give the interpreter a chance to take
441 control of the console via the init_ui_hook()) */
444 /* Do these (and anything which might call wrap_here or *_filtered)
445 after initialize_all_files. */
448 print_gdb_version (gdb_stdout
);
450 printf_filtered ("\n");
456 print_gdb_help (gdb_stdout
);
457 fputs_unfiltered ("\n", gdb_stdout
);
463 /* Print all the junk at the top, with trailing "..." if we are about
464 to read a symbol file (possibly slowly). */
465 print_gdb_version (gdb_stdout
);
467 printf_filtered ("..");
469 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
472 error_pre_print
= "\n\n";
473 quit_pre_print
= error_pre_print
;
475 /* We may get more than one warning, don't double space all of them... */
476 warning_pre_print
= "\nwarning: ";
478 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
479 *before* all the command line arguments are processed; it sets
480 global parameters, which are independent of what file you are
481 debugging or what directory you are in. */
484 char *tmp
= getenv ("HOME");
488 homedir
= (char *) alloca (MAX_PATH
+ 1);
489 cygwin32_conv_to_posix_path (tmp
, homedir
);
495 homedir
= getenv ("HOME");
499 homeinit
= (char *) alloca (strlen (homedir
) +
500 strlen (gdbinit
) + 10);
501 strcpy (homeinit
, homedir
);
502 strcat (homeinit
, "/");
503 strcat (homeinit
, gdbinit
);
505 if (!inhibit_gdbinit
)
507 catch_command_errors (source_command
, homeinit
, 0, RETURN_MASK_ALL
);
510 /* Do stats; no need to do them elsewhere since we'll only
511 need them if homedir is set. Make sure that they are
512 zero in case one of them fails (this guarantees that they
513 won't match if either exists). */
515 memset (&homebuf
, 0, sizeof (struct stat
));
516 memset (&cwdbuf
, 0, sizeof (struct stat
));
518 stat (homeinit
, &homebuf
);
519 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
523 /* Now perform all the actions indicated by the arguments. */
526 catch_command_errors (cd_command
, cdarg
, 0, RETURN_MASK_ALL
);
529 for (i
= 0; i
< ndir
; i
++)
530 catch_command_errors (directory_command
, dirarg
[i
], 0, RETURN_MASK_ALL
);
535 && STREQ (execarg
, symarg
))
537 /* The exec file and the symbol-file are the same. If we can't
538 open it, better only print one error message.
539 catch_command_errors returns non-zero on success! */
540 if (catch_command_errors (exec_file_command
, execarg
, !batch
, RETURN_MASK_ALL
))
541 catch_command_errors (symbol_file_command
, symarg
, 0, RETURN_MASK_ALL
);
546 catch_command_errors (exec_file_command
, execarg
, !batch
, RETURN_MASK_ALL
);
548 catch_command_errors (symbol_file_command
, symarg
, 0, RETURN_MASK_ALL
);
551 /* After the symbol file has been read, print a newline to get us
552 beyond the copyright line... But errors should still set off
553 the error message with a (single) blank line. */
555 printf_filtered ("\n");
556 error_pre_print
= "\n";
557 quit_pre_print
= error_pre_print
;
558 warning_pre_print
= "\nwarning: ";
562 if (catch_command_errors (core_file_command
, corearg
, !batch
, RETURN_MASK_ALL
) == 0)
564 /* See if the core file is really a PID. */
565 if (isdigit (corearg
[0]))
566 catch_command_errors (attach_command
, corearg
, !batch
, RETURN_MASK_ALL
);
571 catch_command_errors (tty_command
, ttyarg
, !batch
, RETURN_MASK_ALL
);
573 #ifdef ADDITIONAL_OPTION_HANDLER
574 ADDITIONAL_OPTION_HANDLER
;
577 /* Error messages should no longer be distinguished with extra output. */
578 error_pre_print
= NULL
;
579 quit_pre_print
= NULL
;
580 warning_pre_print
= "warning: ";
582 /* Read the .gdbinit file in the current directory, *if* it isn't
583 the same as the $HOME/.gdbinit file (it should exist, also). */
586 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
587 if (!inhibit_gdbinit
)
589 catch_command_errors (source_command
, gdbinit
, 0, RETURN_MASK_ALL
);
592 for (i
= 0; i
< ncmd
; i
++)
595 /* NOTE: cagney/1999-11-03: SET_TOP_LEVEL() was a macro that
596 expanded into a call to setjmp(). */
597 if (!SET_TOP_LEVEL ()) /* NB: This is #if 0'd out */
599 /* NOTE: I am commenting this out, because it is not clear
600 where this feature is used. It is very old and
601 undocumented. ezannoni: 1999-05-04 */
603 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
604 read_command_file (stdin
);
607 source_command (cmdarg
[i
], !batch
);
608 do_cleanups (ALL_CLEANUPS
);
611 catch_command_errors (source_command
, cmdarg
[i
], !batch
, RETURN_MASK_ALL
);
615 /* Read in the old history after all the command files have been read. */
620 /* We have hit the end of the batch file. */
624 /* Do any host- or target-specific hacks. This is used for i960 targets
625 to force the user to set a nindy target and spec its parameters. */
627 #ifdef BEFORE_MAIN_LOOP_HOOK
628 BEFORE_MAIN_LOOP_HOOK
;
631 END_PROGRESS (argv
[0]);
633 /* Show time and/or space usage. */
637 long init_time
= get_run_time () - time_at_startup
;
639 printf_unfiltered ("Startup time: %ld.%06ld\n",
640 init_time
/ 1000000, init_time
% 1000000);
646 extern char **environ
;
647 char *lim
= (char *) sbrk (0);
649 printf_unfiltered ("Startup size: data size %ld\n",
650 (long) (lim
- (char *) &environ
));
654 /* The default command loop.
655 The WIN32 Gui calls this main to set up gdb's state, and
656 has its own command loop. */
657 #if !defined _WIN32 || defined __GNUC__
658 /* GUIs generally have their own command loop, mainloop, or
659 whatever. This is a good place to gain control because many
660 error conditions will end up here via longjmp(). */
662 /* FIXME: cagney/1999-11-06: The original main loop was like: */
665 if (!SET_TOP_LEVEL ())
667 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
668 /* GUIs generally have their own command loop, mainloop, or whatever.
669 This is a good place to gain control because many error
670 conditions will end up here via longjmp(). */
671 if (command_loop_hook
)
672 command_loop_hook ();
675 quit_command ((char *) 0, instream
== stdin
);
678 /* NOTE: If the command_loop() returned normally, the loop would
679 attempt to exit by calling the function quit_command(). That
680 function would either call exit() or throw an error returning
681 control to SET_TOP_LEVEL. */
682 /* NOTE: The function do_cleanups() was called once each time round
683 the loop. The usefulness of the call isn't clear. If an error
684 was thrown, everything would have already been cleaned up. If
685 command_loop() returned normally and quit_command() was called,
686 either exit() or error() (again cleaning up) would be called. */
688 /* NOTE: cagney/1999-11-07: There is probably no reason for not
689 moving this loop and the code found in captured_command_loop()
690 into the command_loop() proper. The main thing holding back that
691 change - SET_TOP_LEVEL() - has been eliminated. */
694 catch_errors (captured_command_loop
, 0, "", RETURN_MASK_ALL
);
697 /* No exit -- exit is through quit_command. */
701 main (int argc
, char **argv
)
703 struct captured_main_args args
;
706 catch_errors (captured_main
, &args
, "", RETURN_MASK_ALL
);
711 /* Don't use *_filtered for printing help. We don't want to prompt
712 for continue no matter how small the screen or how much we're going
716 print_gdb_help (struct ui_file
*stream
)
719 This is the GNU debugger. Usage:\n\n\
720 gdb [options] [executable-file [core-file or process-id]]\n\n\
724 --[no]async Enable (disable) asynchronous version of CLI\n\
727 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
728 --batch Exit after processing options.\n\
729 --cd=DIR Change current directory to DIR.\n\
730 --command=FILE Execute GDB commands from FILE.\n\
731 --core=COREFILE Analyze the core dump COREFILE.\n\
734 --dbx DBX compatibility mode.\n\
735 --directory=DIR Search for source files in DIR.\n\
736 --epoch Output information used by epoch emacs-GDB interface.\n\
737 --exec=EXECFILE Use EXECFILE as the executable.\n\
738 --fullname Output information used by emacs-GDB interface.\n\
739 --help Print this message.\n\
742 --mapped Use mapped symbol files if supported on this system.\n\
743 --nw Do not use a window interface.\n\
744 --nx Do not read ", stream
);
745 fputs_unfiltered (gdbinit
, stream
);
746 fputs_unfiltered (" file.\n\
747 --quiet Do not print version number on startup.\n\
748 --readnow Fully read symbol files on first access.\n\
751 --se=FILE Use FILE as symbol file and executable file.\n\
752 --symbols=SYMFILE Read symbols from SYMFILE.\n\
753 --tty=TTY Use TTY for input/output by the program being debugged.\n\
757 --tui Use a terminal user interface.\n\
761 --version Print version information and then exit.\n\
762 -w Use a window interface.\n\
763 --write Set writing into executable and core files.\n\
764 --xdb XDB compatibility mode.\n\
766 #ifdef ADDITIONAL_OPTION_HELP
767 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, stream
);
769 fputs_unfiltered ("\n\
770 For more information, type \"help\" from within GDB, or consult the\n\
771 GDB manual (available as on-line info or a printed manual).\n\
772 Report bugs to \"bug-gdb@gnu.org\".\