6c4feaef2e9561bb8ec7fe8e96c793b2afff2eeb
1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
32 #include <sys/types.h>
36 #include "gdb_string.h"
37 #include "event-loop.h"
42 /* If nonzero, display time usage both at startup and for each command. */
46 /* If nonzero, display space usage both at startup and for each command. */
50 /* Whether this is the async version or not. The async version is
51 invoked on the command line with the -nw --async options. In this
52 version, the usual command_loop is substituted by and event loop which
53 processes UI events asynchronously. */
56 /* Has an interpreter been specified and if so, which. */
59 /* Whether this is the command line version or not */
62 /* Whether xdb commands will be handled */
65 /* Whether dbx commands will be handled */
68 /* System root path, used to find libraries etc. */
69 char *gdb_sysroot
= 0;
71 struct ui_file
*gdb_stdout
;
72 struct ui_file
*gdb_stderr
;
73 struct ui_file
*gdb_stdlog
;
74 struct ui_file
*gdb_stdtarg
;
76 /* Used to initialize error() - defined in utils.c */
78 extern void error_init (void);
80 /* Whether to enable writing into executable and core files */
81 extern int write_files
;
83 static void print_gdb_help (struct ui_file
*);
85 /* These two are used to set the external editor commands when gdb is farming
86 out files to be edited by another program. */
88 extern char *external_editor_command
;
90 /* Call command_loop. If it happens to return, pass that through as a
91 non-zero return status. */
94 captured_command_loop (void *data
)
96 if (command_loop_hook
== NULL
)
100 /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
101 would clean things up (restoring the cleanup chain) to the state
102 they were just prior to the call. Technically, this means that
103 the do_cleanups() below is redundant. Unfortunately, many FUNCs
104 are not that well behaved. do_cleanups should either be replaced
105 with a do_cleanups call (to cover the problem) or an assertion
106 check to detect bad FUNCs code. */
107 do_cleanups (ALL_CLEANUPS
);
108 /* If the command_loop returned, normally (rather than threw an
109 error) we try to quit. If the quit is aborted, catch_errors()
110 which called this catch the signal and restart the command
112 quit_command (NULL
, instream
== stdin
);
117 captured_main (void *data
)
119 struct captured_main_args
*context
= data
;
120 int argc
= context
->argc
;
121 char **argv
= context
->argv
;
123 static int quiet
= 0;
124 static int batch
= 0;
125 static int set_args
= 0;
127 /* Pointers to various arguments from command line. */
129 char *execarg
= NULL
;
130 char *corearg
= NULL
;
134 /* These are static so that we can take their address in an initializer. */
135 static int print_help
;
136 static int print_version
;
138 /* Pointers to all arguments of --command option. */
140 /* Allocated size of cmdarg. */
142 /* Number of elements of cmdarg used. */
145 /* Indices of all arguments of --directory option. */
147 /* Allocated size. */
149 /* Number of elements used. */
152 struct stat homebuf
, cwdbuf
;
153 char *homedir
, *homeinit
;
157 long time_at_startup
= get_run_time ();
159 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
160 setlocale (LC_MESSAGES
, "");
162 #if defined (HAVE_SETLOCALE)
163 setlocale (LC_CTYPE
, "");
165 bindtextdomain (PACKAGE
, LOCALEDIR
);
166 textdomain (PACKAGE
);
168 START_PROGRESS (argv
[0], 0);
170 /* This needs to happen before the first use of malloc. */
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 gdb_stdout
= stdio_fileopen (stdout
);
195 gdb_stderr
= stdio_fileopen (stderr
);
196 gdb_stdlog
= gdb_stderr
; /* for moment */
197 gdb_stdtarg
= gdb_stderr
; /* for moment */
199 /* initialize error() */
202 /* Set the sysroot path. */
203 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
204 gdb_sysroot
= make_relative_prefix (argv
[0], BINDIR
, TARGET_SYSTEM_ROOT
);
210 if (stat (gdb_sysroot
, &s
) == 0)
211 if (S_ISDIR (s
.st_mode
))
217 gdb_sysroot
= TARGET_SYSTEM_ROOT
;
221 gdb_sysroot
= TARGET_SYSTEM_ROOT
;
223 #if defined (TARGET_SYSTEM_ROOT)
224 gdb_sysroot
= TARGET_SYSTEM_ROOT
;
230 /* Parse arguments and options. */
233 /* When var field is 0, use flag field to record the equivalent
234 short option (or arbitrary numbers starting at 10 for those
235 with no equivalent). */
236 static struct option long_options
[] =
238 {"async", no_argument
, &event_loop_p
, 1},
239 {"noasync", no_argument
, &event_loop_p
, 0},
241 {"tui", no_argument
, &tui_version
, 1},
243 {"xdb", no_argument
, &xdb_commands
, 1},
244 {"dbx", no_argument
, &dbx_commands
, 1},
245 {"readnow", no_argument
, &readnow_symbol_files
, 1},
246 {"r", no_argument
, &readnow_symbol_files
, 1},
247 {"mapped", no_argument
, &mapped_symbol_files
, 1},
248 {"m", no_argument
, &mapped_symbol_files
, 1},
249 {"quiet", no_argument
, &quiet
, 1},
250 {"q", no_argument
, &quiet
, 1},
251 {"silent", no_argument
, &quiet
, 1},
252 {"nx", no_argument
, &inhibit_gdbinit
, 1},
253 {"n", no_argument
, &inhibit_gdbinit
, 1},
254 {"batch", no_argument
, &batch
, 1},
255 {"epoch", no_argument
, &epoch_interface
, 1},
257 /* This is a synonym for "--annotate=1". --annotate is now preferred,
258 but keep this here for a long time because people will be running
259 emacses which use --fullname. */
260 {"fullname", no_argument
, 0, 'f'},
261 {"f", no_argument
, 0, 'f'},
263 {"annotate", required_argument
, 0, 12},
264 {"help", no_argument
, &print_help
, 1},
265 {"se", required_argument
, 0, 10},
266 {"symbols", required_argument
, 0, 's'},
267 {"s", required_argument
, 0, 's'},
268 {"exec", required_argument
, 0, 'e'},
269 {"e", required_argument
, 0, 'e'},
270 {"core", required_argument
, 0, 'c'},
271 {"c", required_argument
, 0, 'c'},
272 {"pid", required_argument
, 0, 'p'},
273 {"p", required_argument
, 0, 'p'},
274 {"command", required_argument
, 0, 'x'},
275 {"version", no_argument
, &print_version
, 1},
276 {"x", required_argument
, 0, 'x'},
278 {"tclcommand", required_argument
, 0, 'z'},
279 {"enable-external-editor", no_argument
, 0, 'y'},
280 {"editor-command", required_argument
, 0, 'w'},
282 {"ui", required_argument
, 0, 'i'},
283 {"interpreter", required_argument
, 0, 'i'},
284 {"i", required_argument
, 0, 'i'},
285 {"directory", required_argument
, 0, 'd'},
286 {"d", required_argument
, 0, 'd'},
287 {"cd", required_argument
, 0, 11},
288 {"tty", required_argument
, 0, 't'},
289 {"baud", required_argument
, 0, 'b'},
290 {"b", required_argument
, 0, 'b'},
291 {"nw", no_argument
, &use_windows
, 0},
292 {"nowindows", no_argument
, &use_windows
, 0},
293 {"w", no_argument
, &use_windows
, 1},
294 {"windows", no_argument
, &use_windows
, 1},
295 {"statistics", no_argument
, 0, 13},
296 {"write", no_argument
, &write_files
, 1},
297 {"args", no_argument
, &set_args
, 1},
298 {0, no_argument
, 0, 0}
305 c
= getopt_long_only (argc
, argv
, "",
306 long_options
, &option_index
);
307 if (c
== EOF
|| set_args
)
310 /* Long option that takes an argument. */
311 if (c
== 0 && long_options
[option_index
].flag
== 0)
312 c
= long_options
[option_index
].val
;
317 /* Long option that just sets a flag. */
327 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
328 annotation_level
= atoi (optarg
);
331 /* Enable the display of both time and space usage. */
336 annotation_level
= 1;
337 /* We have probably been invoked from emacs. Disable window interface. */
350 /* "corearg" is shared by "--core" and "--pid" */
354 cmdarg
[ncmd
++] = optarg
;
358 cmdarg
= (char **) xrealloc ((char *) cmdarg
,
359 cmdsize
* sizeof (*cmdarg
));
365 extern int gdbtk_test (char *);
366 if (!gdbtk_test (optarg
))
368 fprintf_unfiltered (gdb_stderr
, _("%s: unable to load tclcommand file \"%s\""),
375 /* Backwards compatibility only. */
379 external_editor_command
= xstrdup (optarg
);
384 interpreter_p
= optarg
;
387 dirarg
[ndir
++] = optarg
;
391 dirarg
= (char **) xrealloc ((char *) dirarg
,
392 dirsize
* sizeof (*dirarg
));
406 i
= strtol (optarg
, &p
, 0);
407 if (i
== 0 && p
== optarg
)
409 /* Don't use *_filtered or warning() (which relies on
410 current_target) until after initialize_all_files(). */
414 _("warning: could not set baud rate to `%s'.\n"), optarg
);
424 i
= strtol (optarg
, &p
, 0);
425 if (i
== 0 && p
== optarg
)
427 /* Don't use *_filtered or warning() (which relies on
428 current_target) until after initialize_all_files(). */
432 _("warning: could not set timeout limit to `%s'.\n"), optarg
);
439 fprintf_unfiltered (gdb_stderr
,
440 _("Use `%s --help' for a complete list of options.\n"),
446 /* If --help or --version, disable window interface. */
447 if (print_help
|| print_version
)
451 /* Disable the TUI as well. */
457 /* An explicit --tui flag overrides the default UI, which is the
465 /* The remaining options are the command-line options for the
466 inferior. The first one is the sym/exec file, and the rest
470 fprintf_unfiltered (gdb_stderr
,
471 _("%s: `--args' specified but no program specified\n"),
475 symarg
= argv
[optind
];
476 execarg
= argv
[optind
];
478 set_inferior_args_vector (argc
- optind
, &argv
[optind
]);
482 /* OK, that's all the options. The other arguments are filenames. */
484 for (; optind
< argc
; optind
++)
488 symarg
= argv
[optind
];
489 execarg
= argv
[optind
];
492 /* The documentation says this can be a "ProcID" as well.
493 We will try it as both a corefile and a pid. */
494 corearg
= argv
[optind
];
497 fprintf_unfiltered (gdb_stderr
,
498 _("Excess command line arguments ignored. (%s%s)\n"),
499 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
507 /* Initialize all files. Give the interpreter a chance to take
508 control of the console via the init_ui_hook()) */
511 /* Do these (and anything which might call wrap_here or *_filtered)
512 after initialize_all_files. */
515 print_gdb_version (gdb_stdout
);
517 printf_filtered ("\n");
523 print_gdb_help (gdb_stdout
);
524 fputs_unfiltered ("\n", gdb_stdout
);
530 /* Print all the junk at the top, with trailing "..." if we are about
531 to read a symbol file (possibly slowly). */
532 print_gdb_version (gdb_stdout
);
534 printf_filtered ("..");
536 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
539 error_pre_print
= "\n\n";
540 quit_pre_print
= error_pre_print
;
542 /* We may get more than one warning, don't double space all of them... */
543 warning_pre_print
= _("\nwarning: ");
545 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
546 *before* all the command line arguments are processed; it sets
547 global parameters, which are independent of what file you are
548 debugging or what directory you are in. */
549 homedir
= getenv ("HOME");
552 homeinit
= (char *) alloca (strlen (homedir
) +
553 strlen (gdbinit
) + 10);
554 strcpy (homeinit
, homedir
);
555 strcat (homeinit
, "/");
556 strcat (homeinit
, gdbinit
);
558 if (!inhibit_gdbinit
)
560 catch_command_errors (source_command
, homeinit
, 0, RETURN_MASK_ALL
);
563 /* Do stats; no need to do them elsewhere since we'll only
564 need them if homedir is set. Make sure that they are
565 zero in case one of them fails (this guarantees that they
566 won't match if either exists). */
568 memset (&homebuf
, 0, sizeof (struct stat
));
569 memset (&cwdbuf
, 0, sizeof (struct stat
));
571 stat (homeinit
, &homebuf
);
572 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
576 /* Now perform all the actions indicated by the arguments. */
579 catch_command_errors (cd_command
, cdarg
, 0, RETURN_MASK_ALL
);
582 for (i
= 0; i
< ndir
; i
++)
583 catch_command_errors (directory_command
, dirarg
[i
], 0, RETURN_MASK_ALL
);
588 && STREQ (execarg
, symarg
))
590 /* The exec file and the symbol-file are the same. If we can't
591 open it, better only print one error message.
592 catch_command_errors returns non-zero on success! */
593 if (catch_command_errors (exec_file_attach
, execarg
, !batch
, RETURN_MASK_ALL
))
594 catch_command_errors (symbol_file_add_main
, symarg
, 0, RETURN_MASK_ALL
);
599 catch_command_errors (exec_file_attach
, execarg
, !batch
, RETURN_MASK_ALL
);
601 catch_command_errors (symbol_file_add_main
, symarg
, 0, RETURN_MASK_ALL
);
604 /* After the symbol file has been read, print a newline to get us
605 beyond the copyright line... But errors should still set off
606 the error message with a (single) blank line. */
608 printf_filtered ("\n");
609 error_pre_print
= "\n";
610 quit_pre_print
= error_pre_print
;
611 warning_pre_print
= _("\nwarning: ");
615 /* corearg may be either a corefile or a pid.
616 If its first character is a digit, try attach first
617 and then corefile. Otherwise try corefile first. */
619 if (isdigit (corearg
[0]))
621 if (catch_command_errors (attach_command
, corearg
,
622 !batch
, RETURN_MASK_ALL
) == 0)
623 catch_command_errors (core_file_command
, corearg
,
624 !batch
, RETURN_MASK_ALL
);
626 else /* Can't be a pid, better be a corefile. */
627 catch_command_errors (core_file_command
, corearg
,
628 !batch
, RETURN_MASK_ALL
);
632 catch_command_errors (tty_command
, ttyarg
, !batch
, RETURN_MASK_ALL
);
634 /* Error messages should no longer be distinguished with extra output. */
635 error_pre_print
= NULL
;
636 quit_pre_print
= NULL
;
637 warning_pre_print
= _("warning: ");
639 /* Read the .gdbinit file in the current directory, *if* it isn't
640 the same as the $HOME/.gdbinit file (it should exist, also). */
643 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
644 if (!inhibit_gdbinit
)
646 catch_command_errors (source_command
, gdbinit
, 0, RETURN_MASK_ALL
);
649 for (i
= 0; i
< ncmd
; i
++)
652 /* NOTE: cagney/1999-11-03: SET_TOP_LEVEL() was a macro that
653 expanded into a call to setjmp(). */
654 if (!SET_TOP_LEVEL ()) /* NB: This is #if 0'd out */
656 /* NOTE: I am commenting this out, because it is not clear
657 where this feature is used. It is very old and
658 undocumented. ezannoni: 1999-05-04 */
660 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
661 read_command_file (stdin
);
664 source_command (cmdarg
[i
], !batch
);
665 do_cleanups (ALL_CLEANUPS
);
668 catch_command_errors (source_command
, cmdarg
[i
], !batch
, RETURN_MASK_ALL
);
672 /* Read in the old history after all the command files have been read. */
677 /* We have hit the end of the batch file. */
681 /* Do any host- or target-specific hacks. This is used for i960 targets
682 to force the user to set a nindy target and spec its parameters. */
684 #ifdef BEFORE_MAIN_LOOP_HOOK
685 BEFORE_MAIN_LOOP_HOOK
;
688 END_PROGRESS (argv
[0]);
690 /* Show time and/or space usage. */
694 long init_time
= get_run_time () - time_at_startup
;
696 printf_unfiltered (_("Startup time: %ld.%06ld\n"),
697 init_time
/ 1000000, init_time
% 1000000);
703 extern char **environ
;
704 char *lim
= (char *) sbrk (0);
706 printf_unfiltered (_("Startup size: data size %ld\n"),
707 (long) (lim
- (char *) &environ
));
712 /* FIXME: cagney/1999-11-06: The original main loop was like: */
715 if (!SET_TOP_LEVEL ())
717 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
718 /* GUIs generally have their own command loop, mainloop, or whatever.
719 This is a good place to gain control because many error
720 conditions will end up here via longjmp(). */
721 if (command_loop_hook
)
722 command_loop_hook ();
725 quit_command ((char *) 0, instream
== stdin
);
728 /* NOTE: If the command_loop() returned normally, the loop would
729 attempt to exit by calling the function quit_command(). That
730 function would either call exit() or throw an error returning
731 control to SET_TOP_LEVEL. */
732 /* NOTE: The function do_cleanups() was called once each time round
733 the loop. The usefulness of the call isn't clear. If an error
734 was thrown, everything would have already been cleaned up. If
735 command_loop() returned normally and quit_command() was called,
736 either exit() or error() (again cleaning up) would be called. */
738 /* NOTE: cagney/1999-11-07: There is probably no reason for not
739 moving this loop and the code found in captured_command_loop()
740 into the command_loop() proper. The main thing holding back that
741 change - SET_TOP_LEVEL() - has been eliminated. */
744 catch_errors (captured_command_loop
, 0, "", RETURN_MASK_ALL
);
746 /* No exit -- exit is through quit_command. */
750 gdb_main (struct captured_main_args
*args
)
752 use_windows
= args
->use_windows
;
753 catch_errors (captured_main
, args
, "", RETURN_MASK_ALL
);
758 /* Don't use *_filtered for printing help. We don't want to prompt
759 for continue no matter how small the screen or how much we're going
763 print_gdb_help (struct ui_file
*stream
)
765 fputs_unfiltered (_("\
766 This is the GNU debugger. Usage:\n\n\
767 gdb [options] [executable-file [core-file or process-id]]\n\
768 gdb [options] --args executable-file [inferior-arguments ...]\n\n\
771 fputs_unfiltered (_("\
772 --args Arguments after executable-file are passed to inferior\n\
774 fputs_unfiltered (_("\
775 --[no]async Enable (disable) asynchronous version of CLI\n\
777 fputs_unfiltered (_("\
778 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
779 --batch Exit after processing options.\n\
780 --cd=DIR Change current directory to DIR.\n\
781 --command=FILE Execute GDB commands from FILE.\n\
782 --core=COREFILE Analyze the core dump COREFILE.\n\
783 --pid=PID Attach to running process PID.\n\
785 fputs_unfiltered (_("\
786 --dbx DBX compatibility mode.\n\
787 --directory=DIR Search for source files in DIR.\n\
788 --epoch Output information used by epoch emacs-GDB interface.\n\
789 --exec=EXECFILE Use EXECFILE as the executable.\n\
790 --fullname Output information used by emacs-GDB interface.\n\
791 --help Print this message.\n\
793 fputs_unfiltered (_("\
794 --interpreter=INTERP\n\
795 Select a specific interpreter / user interface\n\
797 fputs_unfiltered (_("\
798 --mapped Use mapped symbol files if supported on this system.\n\
799 --nw Do not use a window interface.\n\
800 --nx Do not read "), stream
);
801 fputs_unfiltered (gdbinit
, stream
);
802 fputs_unfiltered (_(" file.\n\
803 --quiet Do not print version number on startup.\n\
804 --readnow Fully read symbol files on first access.\n\
806 fputs_unfiltered (_("\
807 --se=FILE Use FILE as symbol file and executable file.\n\
808 --symbols=SYMFILE Read symbols from SYMFILE.\n\
809 --tty=TTY Use TTY for input/output by the program being debugged.\n\
812 fputs_unfiltered (_("\
813 --tui Use a terminal user interface.\n\
816 fputs_unfiltered (_("\
817 --version Print version information and then exit.\n\
818 -w Use a window interface.\n\
819 --write Set writing into executable and core files.\n\
820 --xdb XDB compatibility mode.\n\
822 fputs_unfiltered (_("\n\
823 For more information, type \"help\" from within GDB, or consult the\n\
824 GDB manual (available as on-line info or a printed manual).\n\
825 Report bugs to \"bug-gdb@gnu.org\".\
This page took 0.045529 seconds and 4 git commands to generate.