1 /* Top level stuff for GDB, the GNU debugger.
3 Copyright (C) 1986-2013 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 3 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, see <http://www.gnu.org/licenses/>. */
27 #include "exceptions.h"
30 #include <sys/types.h>
34 #include "gdb_string.h"
35 #include "event-loop.h"
41 #include "cli/cli-cmds.h"
42 #include "python/python.h"
44 #include "auto-load.h"
47 /* The selected interpreter. This will be used as a set command
48 variable, so it should always be malloc'ed - since
49 do_setshow_command will free it. */
52 /* Whether xdb commands will be handled. */
55 /* Whether dbx commands will be handled. */
58 /* System root path, used to find libraries etc. */
59 char *gdb_sysroot = 0;
61 /* GDB datadir, used to store data files. */
62 char *gdb_datadir = 0;
64 /* Non-zero if GDB_DATADIR was provided on the command line.
65 This doesn't track whether data-directory is set later from the
66 command line, but we don't reread system.gdbinit when that happens. */
67 static int gdb_datadir_provided = 0;
69 /* If gdb was configured with --with-python=/path,
70 the possibly relocated path to python's lib directory. */
71 char *python_libdir = 0;
73 struct ui_file *gdb_stdout;
74 struct ui_file *gdb_stderr;
75 struct ui_file *gdb_stdlog;
76 struct ui_file *gdb_stdin;
77 /* Target IO streams. */
78 struct ui_file *gdb_stdtargin;
79 struct ui_file *gdb_stdtarg;
80 struct ui_file *gdb_stdtargerr;
82 /* True if --batch or --batch-silent was seen. */
85 /* Support for the --batch-silent option. */
88 /* Support for --return-child-result option.
89 Set the default to -1 to return error in the case
90 that the program does not run or does not complete. */
91 int return_child_result = 0;
92 int return_child_result_value = -1;
95 /* GDB as it has been invoked from the command line (i.e. argv[0]). */
96 static char *gdb_program_name;
98 static void print_gdb_help (struct ui_file *);
100 /* Relocate a file or directory. PROGNAME is the name by which gdb
101 was invoked (i.e., argv[0]). INITIAL is the default value for the
102 file or directory. FLAG is true if the value is relocatable, false
103 otherwise. Returns a newly allocated string; this may return NULL
104 under the same conditions as make_relative_prefix. */
107 relocate_path (const char *progname, const char *initial, int flag)
110 return make_relative_prefix (progname, BINDIR, initial);
111 return xstrdup (initial);
114 /* Like relocate_path, but specifically checks for a directory.
115 INITIAL is relocated according to the rules of relocate_path. If
116 the result is a directory, it is used; otherwise, INITIAL is used.
117 The chosen directory is then canonicalized using lrealpath. This
118 function always returns a newly-allocated string. */
121 relocate_gdb_directory (const char *initial, int flag)
125 dir = relocate_path (gdb_program_name, initial, flag);
130 if (*dir == '\0' || stat (dir, &s) != 0 || !S_ISDIR (s.st_mode))
137 dir = xstrdup (initial);
139 /* Canonicalize the directory. */
142 char *canon_sysroot = lrealpath (dir);
154 /* Compute the locations of init files that GDB should source and
155 return them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT. If
156 there is no system gdbinit (resp. home gdbinit and local gdbinit)
157 to be loaded, then SYSTEM_GDBINIT (resp. HOME_GDBINIT and
158 LOCAL_GDBINIT) is set to NULL. */
160 get_init_files (char **system_gdbinit,
162 char **local_gdbinit)
164 static char *sysgdbinit = NULL;
165 static char *homeinit = NULL;
166 static char *localinit = NULL;
167 static int initialized = 0;
171 struct stat homebuf, cwdbuf, s;
174 if (SYSTEM_GDBINIT[0])
176 int datadir_len = strlen (GDB_DATADIR);
177 int sys_gdbinit_len = strlen (SYSTEM_GDBINIT);
178 char *relocated_sysgdbinit;
180 /* If SYSTEM_GDBINIT lives in data-directory, and data-directory
181 has been provided, search for SYSTEM_GDBINIT there. */
182 if (gdb_datadir_provided
183 && datadir_len < sys_gdbinit_len
184 && strncmp (SYSTEM_GDBINIT, GDB_DATADIR, datadir_len) == 0
185 && strchr (SLASH_STRING, SYSTEM_GDBINIT[datadir_len]) != NULL)
187 /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
189 char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
192 for (p = tmp_sys_gdbinit; strchr (SLASH_STRING, *p); ++p)
194 relocated_sysgdbinit = concat (gdb_datadir, SLASH_STRING, p,
196 xfree (tmp_sys_gdbinit);
200 relocated_sysgdbinit = relocate_path (gdb_program_name,
202 SYSTEM_GDBINIT_RELOCATABLE);
204 if (relocated_sysgdbinit && stat (relocated_sysgdbinit, &s) == 0)
205 sysgdbinit = relocated_sysgdbinit;
207 xfree (relocated_sysgdbinit);
210 homedir = getenv ("HOME");
212 /* If the .gdbinit file in the current directory is the same as
213 the $HOME/.gdbinit file, it should not be sourced. homebuf
214 and cwdbuf are used in that purpose. Make sure that the stats
215 are zero in case one of them fails (this guarantees that they
216 won't match if either exists). */
218 memset (&homebuf, 0, sizeof (struct stat));
219 memset (&cwdbuf, 0, sizeof (struct stat));
223 homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
224 if (stat (homeinit, &homebuf) != 0)
231 if (stat (gdbinit, &cwdbuf) == 0)
234 || memcmp ((char *) &homebuf, (char *) &cwdbuf,
235 sizeof (struct stat)))
242 *system_gdbinit = sysgdbinit;
243 *home_gdbinit = homeinit;
244 *local_gdbinit = localinit;
247 /* Call command_loop. If it happens to return, pass that through as a
248 non-zero return status. */
251 captured_command_loop (void *data)
253 /* Top-level execution commands can be run on the background from
255 interpreter_async = 1;
257 current_interp_command_loop ();
258 /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
259 would clean things up (restoring the cleanup chain) to the state
260 they were just prior to the call. Technically, this means that
261 the do_cleanups() below is redundant. Unfortunately, many FUNCs
262 are not that well behaved. do_cleanups should either be replaced
263 with a do_cleanups call (to cover the problem) or an assertion
264 check to detect bad FUNCs code. */
265 do_cleanups (all_cleanups ());
266 /* If the command_loop returned, normally (rather than threw an
267 error) we try to quit. If the quit is aborted, catch_errors()
268 which called this catch the signal and restart the command
270 quit_command (NULL, instream == stdin);
274 /* Arguments of --command option and its counterpart. */
275 typedef struct cmdarg {
276 /* Type of this option. */
278 /* Option type -x. */
281 /* Option type -ex. */
284 /* Option type -ix. */
287 /* Option type -iex. */
291 /* Value of this option - filename or the GDB command itself. String memory
292 is not owned by this structure despite it is 'const'. */
296 /* Define type VEC (cmdarg_s). */
297 DEF_VEC_O (cmdarg_s);
300 captured_main (void *data)
302 struct captured_main_args *context = data;
303 int argc = context->argc;
304 char **argv = context->argv;
305 static int quiet = 0;
306 static int set_args = 0;
307 static int inhibit_home_gdbinit = 0;
309 /* Pointers to various arguments from command line. */
311 char *execarg = NULL;
313 char *corearg = NULL;
314 char *pid_or_core_arg = NULL;
318 /* These are static so that we can take their address in an
320 static int print_help;
321 static int print_version;
323 /* Pointers to all arguments of --command option. */
324 VEC (cmdarg_s) *cmdarg_vec = NULL;
325 struct cmdarg *cmdarg_p;
327 /* Indices of all arguments of --directory option. */
329 /* Allocated size. */
331 /* Number of elements used. */
334 /* gdb init files. */
335 char *system_gdbinit;
341 struct objfile *objfile;
343 struct cleanup *pre_stat_chain;
346 /* Set this before calling make_command_stats_cleanup. */
347 lim_at_start = (char *) sbrk (0);
350 pre_stat_chain = make_command_stats_cleanup (0);
352 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
353 setlocale (LC_MESSAGES, "");
355 #if defined (HAVE_SETLOCALE)
356 setlocale (LC_CTYPE, "");
358 bindtextdomain (PACKAGE, LOCALEDIR);
359 textdomain (PACKAGE);
363 make_cleanup (VEC_cleanup (cmdarg_s), &cmdarg_vec);
365 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
369 saved_command_line = (char *) xmalloc (saved_command_line_size);
370 saved_command_line[0] = '\0';
373 gdb_stdout = stdio_fileopen (stdout);
374 gdb_stderr = stdio_fileopen (stderr);
375 gdb_stdlog = gdb_stderr; /* for moment */
376 gdb_stdtarg = gdb_stderr; /* for moment */
377 gdb_stdin = stdio_fileopen (stdin);
378 gdb_stdtargerr = gdb_stderr; /* for moment */
379 gdb_stdtargin = gdb_stdin; /* for moment */
381 gdb_program_name = xstrdup (argv[0]);
383 if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
384 /* Don't use *_filtered or warning() (which relies on
385 current_target) until after initialize_all_files(). */
386 fprintf_unfiltered (gdb_stderr,
387 _("%s: warning: error finding "
388 "working directory: %s\n"),
389 argv[0], safe_strerror (errno));
391 current_directory = gdb_dirbuf;
393 /* Set the sysroot path. */
394 gdb_sysroot = relocate_gdb_directory (TARGET_SYSTEM_ROOT,
395 TARGET_SYSTEM_ROOT_RELOCATABLE);
397 debug_file_directory = relocate_gdb_directory (DEBUGDIR,
398 DEBUGDIR_RELOCATABLE);
400 gdb_datadir = relocate_gdb_directory (GDB_DATADIR,
401 GDB_DATADIR_RELOCATABLE);
403 #ifdef WITH_PYTHON_PATH
405 /* For later use in helping Python find itself. */
406 char *tmp = concat (WITH_PYTHON_PATH, SLASH_STRING, "lib", NULL);
408 python_libdir = relocate_gdb_directory (tmp, PYTHON_PATH_RELOCATABLE);
414 add_substitute_path_rule (RELOC_SRCDIR,
415 make_relative_prefix (argv[0], BINDIR,
419 /* There will always be an interpreter. Either the one passed into
420 this captured main, or one specified by the user at start up, or
421 the console. Initialize the interpreter to the one requested by
423 interpreter_p = xstrdup (context->interpreter_p);
425 /* Parse arguments and options. */
428 /* When var field is 0, use flag field to record the equivalent
429 short option (or arbitrary numbers starting at 10 for those
430 with no equivalent). */
442 static struct option long_options[] =
444 {"tui", no_argument, 0, OPT_TUI},
445 {"xdb", no_argument, &xdb_commands, 1},
446 {"dbx", no_argument, &dbx_commands, 1},
447 {"readnow", no_argument, &readnow_symbol_files, 1},
448 {"r", no_argument, &readnow_symbol_files, 1},
449 {"quiet", no_argument, &quiet, 1},
450 {"q", no_argument, &quiet, 1},
451 {"silent", no_argument, &quiet, 1},
452 {"nh", no_argument, &inhibit_home_gdbinit, 1},
453 {"nx", no_argument, &inhibit_gdbinit, 1},
454 {"n", no_argument, &inhibit_gdbinit, 1},
455 {"batch-silent", no_argument, 0, 'B'},
456 {"batch", no_argument, &batch_flag, 1},
458 /* This is a synonym for "--annotate=1". --annotate is now
459 preferred, but keep this here for a long time because people
460 will be running emacses which use --fullname. */
461 {"fullname", no_argument, 0, 'f'},
462 {"f", no_argument, 0, 'f'},
464 {"annotate", required_argument, 0, OPT_ANNOTATE},
465 {"help", no_argument, &print_help, 1},
466 {"se", required_argument, 0, OPT_SE},
467 {"symbols", required_argument, 0, 's'},
468 {"s", required_argument, 0, 's'},
469 {"exec", required_argument, 0, 'e'},
470 {"e", required_argument, 0, 'e'},
471 {"core", required_argument, 0, 'c'},
472 {"c", required_argument, 0, 'c'},
473 {"pid", required_argument, 0, 'p'},
474 {"p", required_argument, 0, 'p'},
475 {"command", required_argument, 0, 'x'},
476 {"eval-command", required_argument, 0, 'X'},
477 {"version", no_argument, &print_version, 1},
478 {"x", required_argument, 0, 'x'},
479 {"ex", required_argument, 0, 'X'},
480 {"init-command", required_argument, 0, OPT_IX},
481 {"init-eval-command", required_argument, 0, OPT_IEX},
482 {"ix", required_argument, 0, OPT_IX},
483 {"iex", required_argument, 0, OPT_IEX},
485 {"tclcommand", required_argument, 0, 'z'},
486 {"enable-external-editor", no_argument, 0, 'y'},
487 {"editor-command", required_argument, 0, 'w'},
489 {"ui", required_argument, 0, 'i'},
490 {"interpreter", required_argument, 0, 'i'},
491 {"i", required_argument, 0, 'i'},
492 {"directory", required_argument, 0, 'd'},
493 {"d", required_argument, 0, 'd'},
494 {"data-directory", required_argument, 0, 'D'},
495 {"cd", required_argument, 0, OPT_CD},
496 {"tty", required_argument, 0, 't'},
497 {"baud", required_argument, 0, 'b'},
498 {"b", required_argument, 0, 'b'},
499 {"nw", no_argument, NULL, OPT_NOWINDOWS},
500 {"nowindows", no_argument, NULL, OPT_NOWINDOWS},
501 {"w", no_argument, NULL, OPT_WINDOWS},
502 {"windows", no_argument, NULL, OPT_WINDOWS},
503 {"statistics", no_argument, 0, OPT_STATISTICS},
504 {"write", no_argument, &write_files, 1},
505 {"args", no_argument, &set_args, 1},
506 {"l", required_argument, 0, 'l'},
507 {"return-child-result", no_argument, &return_child_result, 1},
508 {0, no_argument, 0, 0}
515 c = getopt_long_only (argc, argv, "",
516 long_options, &option_index);
517 if (c == EOF || set_args)
520 /* Long option that takes an argument. */
521 if (c == 0 && long_options[option_index].flag == 0)
522 c = long_options[option_index].val;
527 /* Long option that just sets a flag. */
537 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
538 annotation_level = atoi (optarg);
541 /* Enable the display of both time and space usage. */
542 set_per_command_time (1);
543 set_per_command_space (1);
546 /* --tui is equivalent to -i=tui. */
548 xfree (interpreter_p);
549 interpreter_p = xstrdup (INTERP_TUI);
551 fprintf_unfiltered (gdb_stderr,
552 _("%s: TUI mode is not supported\n"),
558 /* FIXME: cagney/2003-03-01: Not sure if this option is
559 actually useful, and if it is, what it should do. */
561 /* --windows is equivalent to -i=insight. */
562 xfree (interpreter_p);
563 interpreter_p = xstrdup (INTERP_INSIGHT);
568 /* -nw is equivalent to -i=console. */
569 xfree (interpreter_p);
570 interpreter_p = xstrdup (INTERP_CONSOLE);
574 annotation_level = 1;
575 /* We have probably been invoked from emacs. Disable
593 struct cmdarg cmdarg = { CMDARG_FILE, optarg };
595 VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
600 struct cmdarg cmdarg = { CMDARG_COMMAND, optarg };
602 VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
607 struct cmdarg cmdarg = { CMDARG_INIT_FILE, optarg };
609 VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
614 struct cmdarg cmdarg = { CMDARG_INIT_COMMAND, optarg };
616 VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
620 batch_flag = batch_silent = 1;
621 gdb_stdout = ui_file_new();
625 gdb_datadir = xstrdup (optarg);
626 gdb_datadir_provided = 1;
631 extern int gdbtk_test (char *);
633 if (!gdbtk_test (optarg))
635 fprintf_unfiltered (gdb_stderr,
636 _("%s: unable to load "
637 "tclcommand file \"%s\""),
644 /* Backwards compatibility only. */
648 /* Set the external editor commands when gdb is farming out files
649 to be edited by another program. */
650 extern char *external_editor_command;
652 external_editor_command = xstrdup (optarg);
657 xfree (interpreter_p);
658 interpreter_p = xstrdup (optarg);
661 dirarg[ndir++] = optarg;
665 dirarg = (char **) xrealloc ((char *) dirarg,
666 dirsize * sizeof (*dirarg));
680 i = strtol (optarg, &p, 0);
681 if (i == 0 && p == optarg)
683 /* Don't use *_filtered or warning() (which relies on
684 current_target) until after initialize_all_files(). */
688 _("warning: could not set baud rate to `%s'.\n"), optarg);
698 i = strtol (optarg, &p, 0);
699 if (i == 0 && p == optarg)
701 /* Don't use *_filtered or warning() (which relies on
702 current_target) until after initialize_all_files(). */
704 fprintf_unfiltered (gdb_stderr,
705 _("warning: could not set "
706 "timeout limit to `%s'.\n"), optarg);
713 fprintf_unfiltered (gdb_stderr,
714 _("Use `%s --help' for a "
715 "complete list of options.\n"),
721 /* If --help or --version, disable window interface. */
722 if (print_help || print_version)
731 /* Initialize all files. Give the interpreter a chance to take
732 control of the console via the deprecated_init_ui_hook (). */
735 /* Now that gdb_init has created the initial inferior, we're in
736 position to set args for that inferior. */
739 /* The remaining options are the command-line options for the
740 inferior. The first one is the sym/exec file, and the rest
744 fprintf_unfiltered (gdb_stderr,
745 _("%s: `--args' specified but "
746 "no program specified\n"),
750 symarg = argv[optind];
751 execarg = argv[optind];
753 set_inferior_args_vector (argc - optind, &argv[optind]);
757 /* OK, that's all the options. */
759 /* The first argument, if specified, is the name of the
763 symarg = argv[optind];
764 execarg = argv[optind];
768 /* If the user hasn't already specified a PID or the name of a
769 core file, then a second optional argument is allowed. If
770 present, this argument should be interpreted as either a
771 PID or a core file, whichever works. */
772 if (pidarg == NULL && corearg == NULL && optind < argc)
774 pid_or_core_arg = argv[optind];
778 /* Any argument left on the command line is unexpected and
779 will be ignored. Inform the user. */
781 fprintf_unfiltered (gdb_stderr,
782 _("Excess command line "
783 "arguments ignored. (%s%s)\n"),
785 (optind == argc - 1) ? "" : " ...");
788 /* Lookup gdbinit files. Note that the gdbinit file name may be
789 overriden during file initialization, so get_init_files should be
790 called after gdb_init. */
791 get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
793 /* Do these (and anything which might call wrap_here or *_filtered)
794 after initialize_all_files() but before the interpreter has been
795 installed. Otherwize the help/version messages will be eaten by
796 the interpreter's output handler. */
800 print_gdb_version (gdb_stdout);
802 printf_filtered ("\n");
808 print_gdb_help (gdb_stdout);
809 fputs_unfiltered ("\n", gdb_stdout);
813 /* FIXME: cagney/2003-02-03: The big hack (part 1 of 2) that lets
814 GDB retain the old MI1 interpreter startup behavior. Output the
815 copyright message before the interpreter is installed. That way
816 it isn't encapsulated in MI output. */
817 if (!quiet && strcmp (interpreter_p, INTERP_MI1) == 0)
819 /* Print all the junk at the top, with trailing "..." if we are
820 about to read a symbol file (possibly slowly). */
821 print_gdb_version (gdb_stdout);
823 printf_filtered ("..");
825 printf_filtered ("\n");
826 gdb_flush (gdb_stdout); /* Force to screen during slow
830 /* Install the default UI. All the interpreters should have had a
831 look at things by now. Initialize the default interpreter. */
835 struct interp *interp = interp_lookup (interpreter_p);
838 error (_("Interpreter `%s' unrecognized"), interpreter_p);
840 if (!interp_set (interp, 1))
842 fprintf_unfiltered (gdb_stderr,
843 "Interpreter `%s' failed to initialize.\n",
849 /* FIXME: cagney/2003-02-03: The big hack (part 2 of 2) that lets
850 GDB retain the old MI1 interpreter startup behavior. Output the
851 copyright message after the interpreter is installed when it is
852 any sane interpreter. */
853 if (!quiet && !current_interp_named_p (INTERP_MI1))
855 /* Print all the junk at the top, with trailing "..." if we are
856 about to read a symbol file (possibly slowly). */
857 print_gdb_version (gdb_stdout);
859 printf_filtered ("..");
861 printf_filtered ("\n");
862 gdb_flush (gdb_stdout); /* Force to screen during slow
866 /* Set off error and warning messages with a blank line. */
867 error_pre_print = "\n";
868 quit_pre_print = error_pre_print;
869 warning_pre_print = _("\nwarning: ");
871 /* Read and execute the system-wide gdbinit file, if it exists.
872 This is done *before* all the command line arguments are
873 processed; it sets global parameters, which are independent of
874 what file you are debugging or what directory you are in. */
875 if (system_gdbinit && !inhibit_gdbinit)
876 catch_command_errors (source_script, system_gdbinit, 0, RETURN_MASK_ALL);
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. */
883 if (home_gdbinit && !inhibit_gdbinit && !inhibit_home_gdbinit)
884 catch_command_errors (source_script, home_gdbinit, 0, RETURN_MASK_ALL);
886 /* Process '-ix' and '-iex' options early. */
887 for (i = 0; VEC_iterate (cmdarg_s, cmdarg_vec, i, cmdarg_p); i++)
888 switch (cmdarg_p->type)
890 case CMDARG_INIT_FILE:
891 catch_command_errors (source_script, cmdarg_p->string,
892 !batch_flag, RETURN_MASK_ALL);
894 case CMDARG_INIT_COMMAND:
895 catch_command_errors (execute_command, cmdarg_p->string,
896 !batch_flag, RETURN_MASK_ALL);
900 /* Now perform all the actions indicated by the arguments. */
903 catch_command_errors (cd_command, cdarg, 0, RETURN_MASK_ALL);
906 for (i = 0; i < ndir; i++)
907 catch_command_errors (directory_switch, dirarg[i], 0, RETURN_MASK_ALL);
910 /* Skip auto-loading section-specified scripts until we've sourced
911 local_gdbinit (which is often used to augment the source search
913 save_auto_load = global_auto_load;
914 global_auto_load = 0;
918 && strcmp (execarg, symarg) == 0)
920 /* The exec file and the symbol-file are the same. If we can't
921 open it, better only print one error message.
922 catch_command_errors returns non-zero on success! */
923 if (catch_command_errors (exec_file_attach, execarg,
924 !batch_flag, RETURN_MASK_ALL))
925 catch_command_errors (symbol_file_add_main, symarg,
926 !batch_flag, RETURN_MASK_ALL);
931 catch_command_errors (exec_file_attach, execarg,
932 !batch_flag, RETURN_MASK_ALL);
934 catch_command_errors (symbol_file_add_main, symarg,
935 !batch_flag, RETURN_MASK_ALL);
938 if (corearg && pidarg)
939 error (_("Can't attach to process and specify "
940 "a core file at the same time."));
943 catch_command_errors (core_file_command, corearg,
944 !batch_flag, RETURN_MASK_ALL);
945 else if (pidarg != NULL)
946 catch_command_errors (attach_command, pidarg,
947 !batch_flag, RETURN_MASK_ALL);
948 else if (pid_or_core_arg)
950 /* The user specified 'gdb program pid' or gdb program core'.
951 If pid_or_core_arg's first character is a digit, try attach
952 first and then corefile. Otherwise try just corefile. */
954 if (isdigit (pid_or_core_arg[0]))
956 if (catch_command_errors (attach_command, pid_or_core_arg,
957 !batch_flag, RETURN_MASK_ALL) == 0)
958 catch_command_errors (core_file_command, pid_or_core_arg,
959 !batch_flag, RETURN_MASK_ALL);
961 else /* Can't be a pid, better be a corefile. */
962 catch_command_errors (core_file_command, pid_or_core_arg,
963 !batch_flag, RETURN_MASK_ALL);
967 set_inferior_io_terminal (ttyarg);
969 /* Error messages should no longer be distinguished with extra output. */
970 error_pre_print = NULL;
971 quit_pre_print = NULL;
972 warning_pre_print = _("warning: ");
974 /* Read the .gdbinit file in the current directory, *if* it isn't
975 the same as the $HOME/.gdbinit file (it should exist, also). */
978 auto_load_local_gdbinit_pathname = gdb_realpath (local_gdbinit);
980 if (!inhibit_gdbinit && auto_load_local_gdbinit
981 && file_is_auto_load_safe (local_gdbinit,
982 _("auto-load: Loading .gdbinit "
986 auto_load_local_gdbinit_loaded = 1;
988 catch_command_errors (source_script, local_gdbinit, 0,
993 /* Now that all .gdbinit's have been read and all -d options have been
994 processed, we can read any scripts mentioned in SYMARG.
995 We wait until now because it is common to add to the source search
996 path in local_gdbinit. */
997 global_auto_load = save_auto_load;
998 ALL_OBJFILES (objfile)
999 load_auto_scripts_for_objfile (objfile);
1001 /* Process '-x' and '-ex' options. */
1002 for (i = 0; VEC_iterate (cmdarg_s, cmdarg_vec, i, cmdarg_p); i++)
1003 switch (cmdarg_p->type)
1006 catch_command_errors (source_script, cmdarg_p->string,
1007 !batch_flag, RETURN_MASK_ALL);
1009 case CMDARG_COMMAND:
1010 catch_command_errors (execute_command, cmdarg_p->string,
1011 !batch_flag, RETURN_MASK_ALL);
1015 /* Read in the old history after all the command files have been
1021 /* We have hit the end of the batch file. */
1022 quit_force (NULL, 0);
1025 /* Show time and/or space usage. */
1026 do_cleanups (pre_stat_chain);
1028 /* NOTE: cagney/1999-11-07: There is probably no reason for not
1029 moving this loop and the code found in captured_command_loop()
1030 into the command_loop() proper. The main thing holding back that
1031 change - SET_TOP_LEVEL() - has been eliminated. */
1034 catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
1036 /* No exit -- exit is through quit_command. */
1040 gdb_main (struct captured_main_args *args)
1042 use_windows = args->use_windows;
1043 catch_errors (captured_main, args, "", RETURN_MASK_ALL);
1044 /* The only way to end up here is by an error (normal exit is
1045 handled by quit_force()), hence always return an error status. */
1050 /* Don't use *_filtered for printing help. We don't want to prompt
1051 for continue no matter how small the screen or how much we're going
1055 print_gdb_help (struct ui_file *stream)
1057 char *system_gdbinit;
1059 char *local_gdbinit;
1061 get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
1063 fputs_unfiltered (_("\
1064 This is the GNU debugger. Usage:\n\n\
1065 gdb [options] [executable-file [core-file or process-id]]\n\
1066 gdb [options] --args executable-file [inferior-arguments ...]\n\n\
1069 fputs_unfiltered (_("\
1070 --args Arguments after executable-file are passed to inferior\n\
1072 fputs_unfiltered (_("\
1073 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
1074 --batch Exit after processing options.\n\
1075 --batch-silent As for --batch, but suppress all gdb stdout output.\n\
1076 --return-child-result\n\
1077 GDB exit code will be the child's exit code.\n\
1078 --cd=DIR Change current directory to DIR.\n\
1079 --command=FILE, -x Execute GDB commands from FILE.\n\
1080 --eval-command=COMMAND, -ex\n\
1081 Execute a single GDB command.\n\
1082 May be used multiple times and in conjunction\n\
1084 --init-command=FILE, -ix Like -x but execute it before loading inferior.\n\
1085 --init-eval-command=COMMAND, -iex Like -ex but before loading inferior.\n\
1086 --core=COREFILE Analyze the core dump COREFILE.\n\
1087 --pid=PID Attach to running process PID.\n\
1089 fputs_unfiltered (_("\
1090 --dbx DBX compatibility mode.\n\
1091 --directory=DIR Search for source files in DIR.\n\
1092 --exec=EXECFILE Use EXECFILE as the executable.\n\
1093 --fullname Output information used by emacs-GDB interface.\n\
1094 --help Print this message.\n\
1096 fputs_unfiltered (_("\
1097 --interpreter=INTERP\n\
1098 Select a specific interpreter / user interface\n\
1100 fputs_unfiltered (_("\
1101 -l TIMEOUT Set timeout in seconds for remote debugging.\n\
1102 --nw Do not use a window interface.\n\
1103 --nx Do not read any "), stream);
1104 fputs_unfiltered (gdbinit, stream);
1105 fputs_unfiltered (_(" files.\n\
1106 --nh Do not read "), stream);
1107 fputs_unfiltered (gdbinit, stream);
1108 fputs_unfiltered (_(" file from home directory.\n\
1109 --quiet Do not print version number on startup.\n\
1110 --readnow Fully read symbol files on first access.\n\
1112 fputs_unfiltered (_("\
1113 --se=FILE Use FILE as symbol file and executable file.\n\
1114 --symbols=SYMFILE Read symbols from SYMFILE.\n\
1115 --tty=TTY Use TTY for input/output by the program being debugged.\n\
1118 fputs_unfiltered (_("\
1119 --tui Use a terminal user interface.\n\
1122 fputs_unfiltered (_("\
1123 --version Print version information and then exit.\n\
1124 -w Use a window interface.\n\
1125 --write Set writing into executable and core files.\n\
1126 --xdb XDB compatibility mode.\n\
1128 fputs_unfiltered (_("\n\
1129 At startup, GDB reads the following init files and executes their commands:\n\
1132 fprintf_unfiltered (stream, _("\
1133 * system-wide init file: %s\n\
1134 "), system_gdbinit);
1136 fprintf_unfiltered (stream, _("\
1137 * user-specific init file: %s\n\
1140 fprintf_unfiltered (stream, _("\
1141 * local init file (see also 'set auto-load local-gdbinit'): ./%s\n\
1143 fputs_unfiltered (_("\n\
1144 For more information, type \"help\" from within GDB, or consult the\n\
1145 GDB manual (available as on-line info or a printed manual).\n\
1147 if (REPORT_BUGS_TO[0] && stream == gdb_stdout)
1148 fprintf_unfiltered (stream, _("\
1149 Report bugs to \"%s\".\n\
1150 "), REPORT_BUGS_TO);