2009-01-28 Daniel Jacobowitz <dan@codesourcery.com>
[external/binutils.git] / gdb / main.c
1 /* Top level stuff for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
5    2009 Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "top.h"
24 #include "target.h"
25 #include "inferior.h"
26 #include "symfile.h"
27 #include "gdbcore.h"
28
29 #include "exceptions.h"
30 #include "getopt.h"
31
32 #include <sys/types.h>
33 #include "gdb_stat.h"
34 #include <ctype.h>
35
36 #include "gdb_string.h"
37 #include "event-loop.h"
38 #include "ui-out.h"
39
40 #include "interps.h"
41 #include "main.h"
42
43 /* If nonzero, display time usage both at startup and for each command.  */
44
45 int display_time;
46
47 /* If nonzero, display space usage both at startup and for each command.  */
48
49 int display_space;
50
51 /* The selected interpreter.  This will be used as a set command
52    variable, so it should always be malloc'ed - since
53    do_setshow_command will free it. */
54 char *interpreter_p;
55
56 /* Whether xdb commands will be handled */
57 int xdb_commands = 0;
58
59 /* Whether dbx commands will be handled */
60 int dbx_commands = 0;
61
62 /* System root path, used to find libraries etc.  */
63 char *gdb_sysroot = 0;
64
65 struct ui_file *gdb_stdout;
66 struct ui_file *gdb_stderr;
67 struct ui_file *gdb_stdlog;
68 struct ui_file *gdb_stdin;
69 /* target IO streams */
70 struct ui_file *gdb_stdtargin;
71 struct ui_file *gdb_stdtarg;
72 struct ui_file *gdb_stdtargerr;
73
74 /* Support for the --batch-silent option.  */
75 int batch_silent = 0;
76
77 /* Support for --return-child-result option.
78    Set the default to -1 to return error in the case
79    that the program does not run or does not complete.  */
80 int return_child_result = 0;
81 int return_child_result_value = -1;
82
83 /* Whether to enable writing into executable and core files */
84 extern int write_files;
85
86 /* GDB as it has been invoked from the command line (i.e. argv[0]).  */
87 static char *gdb_program_name;
88
89 static void print_gdb_help (struct ui_file *);
90
91 /* These two are used to set the external editor commands when gdb is farming
92    out files to be edited by another program. */
93
94 extern char *external_editor_command;
95
96 /* Compute the locations of init files that GDB should source and return
97    them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT.  If there is 
98    no system gdbinit (resp. home gdbinit and local gdbinit) to be loaded,
99    then SYSTEM_GDBINIT (resp. HOME_GDBINIT and LOCAL_GDBINIT) is set to
100    NULL.  */
101 static void
102 get_init_files (char **system_gdbinit,
103                 char **home_gdbinit,
104                 char **local_gdbinit)
105 {
106   static char *sysgdbinit = NULL;
107   static char *homeinit = NULL;
108   static char *localinit = NULL;
109   static int initialized = 0;
110
111   if (!initialized)
112     {
113       struct stat homebuf, cwdbuf, s;
114       char *homedir, *relocated_sysgdbinit;
115
116       sysgdbinit = SYSTEM_GDBINIT;
117       if (!sysgdbinit [0] || stat (sysgdbinit, &s) != 0)
118         sysgdbinit = NULL;
119
120 #ifdef SYSTEM_GDBINIT_RELOCATABLE
121       relocated_sysgdbinit = make_relative_prefix (gdb_program_name, BINDIR,
122                                                    SYSTEM_GDBINIT);
123       if (relocated_sysgdbinit)
124         {
125           struct stat s;
126           int res = 0;
127
128           if (stat (relocated_sysgdbinit, &s) == 0)
129             sysgdbinit = relocated_sysgdbinit;
130           else
131             xfree (relocated_sysgdbinit);
132         }
133 #endif
134
135       homedir = getenv ("HOME");
136
137       /* If the .gdbinit file in the current directory is the same as
138          the $HOME/.gdbinit file, it should not be sourced.  homebuf
139          and cwdbuf are used in that purpose. Make sure that the stats
140          are zero in case one of them fails (this guarantees that they
141          won't match if either exists).  */
142
143       memset (&homebuf, 0, sizeof (struct stat));
144       memset (&cwdbuf, 0, sizeof (struct stat));
145
146       if (homedir)
147         {
148           homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
149           if (stat (homeinit, &homebuf) != 0)
150             {
151               xfree (homeinit);
152               homeinit = NULL;
153             }
154         }
155
156       if (stat (gdbinit, &cwdbuf) == 0)
157         {
158           if (!homeinit
159               || memcmp ((char *) &homebuf, (char *) &cwdbuf,
160                          sizeof (struct stat)))
161             localinit = gdbinit;
162         }
163       
164       initialized = 1;
165     }
166
167   *system_gdbinit = sysgdbinit;
168   *home_gdbinit = homeinit;
169   *local_gdbinit = localinit;
170 }
171
172 /* Call command_loop.  If it happens to return, pass that through as a
173    non-zero return status. */
174
175 static int
176 captured_command_loop (void *data)
177 {
178   current_interp_command_loop ();
179   /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
180      would clean things up (restoring the cleanup chain) to the state
181      they were just prior to the call.  Technically, this means that
182      the do_cleanups() below is redundant.  Unfortunately, many FUNCs
183      are not that well behaved.  do_cleanups should either be replaced
184      with a do_cleanups call (to cover the problem) or an assertion
185      check to detect bad FUNCs code. */
186   do_cleanups (ALL_CLEANUPS);
187   /* If the command_loop returned, normally (rather than threw an
188      error) we try to quit. If the quit is aborted, catch_errors()
189      which called this catch the signal and restart the command
190      loop. */
191   quit_command (NULL, instream == stdin);
192   return 1;
193 }
194
195 static int
196 captured_main (void *data)
197 {
198   struct captured_main_args *context = data;
199   int argc = context->argc;
200   char **argv = context->argv;
201   static int quiet = 0;
202   static int batch = 0;
203   static int set_args = 0;
204
205   /* Pointers to various arguments from command line.  */
206   char *symarg = NULL;
207   char *execarg = NULL;
208   char *pidarg = NULL;
209   char *corearg = NULL;
210   char *pid_or_core_arg = NULL;
211   char *cdarg = NULL;
212   char *ttyarg = NULL;
213
214   /* These are static so that we can take their address in an initializer.  */
215   static int print_help;
216   static int print_version;
217
218   /* Pointers to all arguments of --command option.  */
219   struct cmdarg {
220     enum {
221       CMDARG_FILE,
222       CMDARG_COMMAND
223     } type;
224     char *string;
225   } *cmdarg;
226   /* Allocated size of cmdarg.  */
227   int cmdsize;
228   /* Number of elements of cmdarg used.  */
229   int ncmd;
230
231   /* Indices of all arguments of --directory option.  */
232   char **dirarg;
233   /* Allocated size.  */
234   int dirsize;
235   /* Number of elements used.  */
236   int ndir;
237
238   /* gdb init files.  */
239   char *system_gdbinit;
240   char *home_gdbinit;
241   char *local_gdbinit;
242
243   int i;
244
245   long time_at_startup = get_run_time ();
246
247 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
248   setlocale (LC_MESSAGES, "");
249 #endif
250 #if defined (HAVE_SETLOCALE)
251   setlocale (LC_CTYPE, "");
252 #endif
253   bindtextdomain (PACKAGE, LOCALEDIR);
254   textdomain (PACKAGE);
255
256 #ifdef HAVE_SBRK
257   lim_at_start = (char *) sbrk (0);
258 #endif
259
260   cmdsize = 1;
261   cmdarg = (struct cmdarg *) xmalloc (cmdsize * sizeof (*cmdarg));
262   ncmd = 0;
263   dirsize = 1;
264   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
265   ndir = 0;
266
267   quit_flag = 0;
268   line = (char *) xmalloc (linesize);
269   line[0] = '\0';               /* Terminate saved (now empty) cmd line */
270   instream = stdin;
271
272   gdb_stdout = stdio_fileopen (stdout);
273   gdb_stderr = stdio_fileopen (stderr);
274   gdb_stdlog = gdb_stderr;      /* for moment */
275   gdb_stdtarg = gdb_stderr;     /* for moment */
276   gdb_stdin = stdio_fileopen (stdin);
277   gdb_stdtargerr = gdb_stderr;  /* for moment */
278   gdb_stdtargin = gdb_stdin;    /* for moment */
279
280   gdb_program_name = xstrdup (argv[0]);
281
282   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
283     /* Don't use *_filtered or warning() (which relies on
284        current_target) until after initialize_all_files(). */
285     fprintf_unfiltered (gdb_stderr,
286                         _("%s: warning: error finding working directory: %s\n"),
287                         argv[0], safe_strerror (errno));
288     
289   current_directory = gdb_dirbuf;
290
291   /* Set the sysroot path.  */
292 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
293   gdb_sysroot = make_relative_prefix (argv[0], BINDIR, TARGET_SYSTEM_ROOT);
294   if (gdb_sysroot)
295     {
296       struct stat s;
297       int res = 0;
298
299       if (stat (gdb_sysroot, &s) == 0)
300         if (S_ISDIR (s.st_mode))
301           res = 1;
302
303       if (res == 0)
304         {
305           xfree (gdb_sysroot);
306           gdb_sysroot = xstrdup (TARGET_SYSTEM_ROOT);
307         }
308     }
309   else
310     gdb_sysroot = xstrdup (TARGET_SYSTEM_ROOT);
311 #else
312   gdb_sysroot = xstrdup (TARGET_SYSTEM_ROOT);
313 #endif
314
315   /* Canonicalize the sysroot path.  */
316   if (*gdb_sysroot)
317     {
318       char *canon_sysroot = lrealpath (gdb_sysroot);
319       if (canon_sysroot)
320         {
321           xfree (gdb_sysroot);
322           gdb_sysroot = canon_sysroot;
323         }
324     }
325
326 #ifdef DEBUGDIR_RELOCATABLE
327   debug_file_directory = make_relative_prefix (argv[0], BINDIR, DEBUGDIR);
328   if (debug_file_directory)
329     {
330       struct stat s;
331       int res = 0;
332
333       if (stat (debug_file_directory, &s) == 0)
334         if (S_ISDIR (s.st_mode))
335           res = 1;
336
337       if (res == 0)
338         {
339           xfree (debug_file_directory);
340           debug_file_directory = xstrdup (DEBUGDIR);
341         }
342     }
343   else
344     debug_file_directory = xstrdup (DEBUGDIR);
345 #else
346   debug_file_directory = xstrdup (DEBUGDIR);
347 #endif
348
349   /* Canonicalize the debugfile path.  */
350   if (*debug_file_directory)
351     {
352       char *canon_debug = lrealpath (debug_file_directory);
353       if (canon_debug)
354         {
355           xfree (debug_file_directory);
356           debug_file_directory = canon_debug;
357         }
358     }
359
360   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
361
362   /* There will always be an interpreter.  Either the one passed into
363      this captured main, or one specified by the user at start up, or
364      the console.  Initialize the interpreter to the one requested by 
365      the application.  */
366   interpreter_p = xstrdup (context->interpreter_p);
367
368   /* Parse arguments and options.  */
369   {
370     int c;
371     /* When var field is 0, use flag field to record the equivalent
372        short option (or arbitrary numbers starting at 10 for those
373        with no equivalent).  */
374     enum {
375       OPT_SE = 10,
376       OPT_CD,
377       OPT_ANNOTATE,
378       OPT_STATISTICS,
379       OPT_TUI,
380       OPT_NOWINDOWS,
381       OPT_WINDOWS
382     };
383     static struct option long_options[] =
384     {
385       {"tui", no_argument, 0, OPT_TUI},
386       {"xdb", no_argument, &xdb_commands, 1},
387       {"dbx", no_argument, &dbx_commands, 1},
388       {"readnow", no_argument, &readnow_symbol_files, 1},
389       {"r", no_argument, &readnow_symbol_files, 1},
390       {"quiet", no_argument, &quiet, 1},
391       {"q", no_argument, &quiet, 1},
392       {"silent", no_argument, &quiet, 1},
393       {"nx", no_argument, &inhibit_gdbinit, 1},
394       {"n", no_argument, &inhibit_gdbinit, 1},
395       {"batch-silent", no_argument, 0, 'B'},
396       {"batch", no_argument, &batch, 1},
397       {"epoch", no_argument, &epoch_interface, 1},
398
399     /* This is a synonym for "--annotate=1".  --annotate is now preferred,
400        but keep this here for a long time because people will be running
401        emacses which use --fullname.  */
402       {"fullname", no_argument, 0, 'f'},
403       {"f", no_argument, 0, 'f'},
404
405       {"annotate", required_argument, 0, OPT_ANNOTATE},
406       {"help", no_argument, &print_help, 1},
407       {"se", required_argument, 0, OPT_SE},
408       {"symbols", required_argument, 0, 's'},
409       {"s", required_argument, 0, 's'},
410       {"exec", required_argument, 0, 'e'},
411       {"e", required_argument, 0, 'e'},
412       {"core", required_argument, 0, 'c'},
413       {"c", required_argument, 0, 'c'},
414       {"pid", required_argument, 0, 'p'},
415       {"p", required_argument, 0, 'p'},
416       {"command", required_argument, 0, 'x'},
417       {"eval-command", required_argument, 0, 'X'},
418       {"version", no_argument, &print_version, 1},
419       {"x", required_argument, 0, 'x'},
420       {"ex", required_argument, 0, 'X'},
421 #ifdef GDBTK
422       {"tclcommand", required_argument, 0, 'z'},
423       {"enable-external-editor", no_argument, 0, 'y'},
424       {"editor-command", required_argument, 0, 'w'},
425 #endif
426       {"ui", required_argument, 0, 'i'},
427       {"interpreter", required_argument, 0, 'i'},
428       {"i", required_argument, 0, 'i'},
429       {"directory", required_argument, 0, 'd'},
430       {"d", required_argument, 0, 'd'},
431       {"cd", required_argument, 0, OPT_CD},
432       {"tty", required_argument, 0, 't'},
433       {"baud", required_argument, 0, 'b'},
434       {"b", required_argument, 0, 'b'},
435       {"nw", no_argument, NULL, OPT_NOWINDOWS},
436       {"nowindows", no_argument, NULL, OPT_NOWINDOWS},
437       {"w", no_argument, NULL, OPT_WINDOWS},
438       {"windows", no_argument, NULL, OPT_WINDOWS},
439       {"statistics", no_argument, 0, OPT_STATISTICS},
440       {"write", no_argument, &write_files, 1},
441       {"args", no_argument, &set_args, 1},
442      {"l", required_argument, 0, 'l'},
443       {"return-child-result", no_argument, &return_child_result, 1},
444       {0, no_argument, 0, 0}
445     };
446
447     while (1)
448       {
449         int option_index;
450
451         c = getopt_long_only (argc, argv, "",
452                               long_options, &option_index);
453         if (c == EOF || set_args)
454           break;
455
456         /* Long option that takes an argument.  */
457         if (c == 0 && long_options[option_index].flag == 0)
458           c = long_options[option_index].val;
459
460         switch (c)
461           {
462           case 0:
463             /* Long option that just sets a flag.  */
464             break;
465           case OPT_SE:
466             symarg = optarg;
467             execarg = optarg;
468             break;
469           case OPT_CD:
470             cdarg = optarg;
471             break;
472           case OPT_ANNOTATE:
473             /* FIXME: what if the syntax is wrong (e.g. not digits)?  */
474             annotation_level = atoi (optarg);
475             break;
476           case OPT_STATISTICS:
477             /* Enable the display of both time and space usage.  */
478             display_time = 1;
479             display_space = 1;
480             break;
481           case OPT_TUI:
482             /* --tui is equivalent to -i=tui.  */
483 #ifdef TUI
484             xfree (interpreter_p);
485             interpreter_p = xstrdup (INTERP_TUI);
486 #else
487             fprintf_unfiltered (gdb_stderr,
488                                 _("%s: TUI mode is not supported\n"),
489                                 argv[0]);
490             exit (1);
491 #endif
492             break;
493           case OPT_WINDOWS:
494             /* FIXME: cagney/2003-03-01: Not sure if this option is
495                actually useful, and if it is, what it should do.  */
496 #ifdef GDBTK
497             /* --windows is equivalent to -i=insight.  */
498             xfree (interpreter_p);
499             interpreter_p = xstrdup (INTERP_INSIGHT);
500 #endif
501             use_windows = 1;
502             break;
503           case OPT_NOWINDOWS:
504             /* -nw is equivalent to -i=console.  */
505             xfree (interpreter_p);
506             interpreter_p = xstrdup (INTERP_CONSOLE);
507             use_windows = 0;
508             break;
509           case 'f':
510             annotation_level = 1;
511 /* We have probably been invoked from emacs.  Disable window interface.  */
512             use_windows = 0;
513             break;
514           case 's':
515             symarg = optarg;
516             break;
517           case 'e':
518             execarg = optarg;
519             break;
520           case 'c':
521             corearg = optarg;
522             break;
523           case 'p':
524             pidarg = optarg;
525             break;
526           case 'x':
527             cmdarg[ncmd].type = CMDARG_FILE;
528             cmdarg[ncmd++].string = optarg;
529             if (ncmd >= cmdsize)
530               {
531                 cmdsize *= 2;
532                 cmdarg = xrealloc ((char *) cmdarg,
533                                    cmdsize * sizeof (*cmdarg));
534               }
535             break;
536           case 'X':
537             cmdarg[ncmd].type = CMDARG_COMMAND;
538             cmdarg[ncmd++].string = optarg;
539             if (ncmd >= cmdsize)
540               {
541                 cmdsize *= 2;
542                 cmdarg = xrealloc ((char *) cmdarg,
543                                    cmdsize * sizeof (*cmdarg));
544               }
545             break;
546           case 'B':
547             batch = batch_silent = 1;
548             gdb_stdout = ui_file_new();
549             break;
550 #ifdef GDBTK
551           case 'z':
552             {
553 extern int gdbtk_test (char *);
554               if (!gdbtk_test (optarg))
555                 {
556                   fprintf_unfiltered (gdb_stderr, _("%s: unable to load tclcommand file \"%s\""),
557                                       argv[0], optarg);
558                   exit (1);
559                 }
560               break;
561             }
562           case 'y':
563             /* Backwards compatibility only.  */
564             break;
565           case 'w':
566             {
567               external_editor_command = xstrdup (optarg);
568               break;
569             }
570 #endif /* GDBTK */
571           case 'i':
572             xfree (interpreter_p);
573             interpreter_p = xstrdup (optarg);
574             break;
575           case 'd':
576             dirarg[ndir++] = optarg;
577             if (ndir >= dirsize)
578               {
579                 dirsize *= 2;
580                 dirarg = (char **) xrealloc ((char *) dirarg,
581                                              dirsize * sizeof (*dirarg));
582               }
583             break;
584           case 't':
585             ttyarg = optarg;
586             break;
587           case 'q':
588             quiet = 1;
589             break;
590           case 'b':
591             {
592               int i;
593               char *p;
594
595               i = strtol (optarg, &p, 0);
596               if (i == 0 && p == optarg)
597
598                 /* Don't use *_filtered or warning() (which relies on
599                    current_target) until after initialize_all_files(). */
600
601                 fprintf_unfiltered
602                   (gdb_stderr,
603                    _("warning: could not set baud rate to `%s'.\n"), optarg);
604               else
605                 baud_rate = i;
606             }
607             break;
608           case 'l':
609             {
610               int i;
611               char *p;
612
613               i = strtol (optarg, &p, 0);
614               if (i == 0 && p == optarg)
615
616                 /* Don't use *_filtered or warning() (which relies on
617                    current_target) until after initialize_all_files(). */
618
619                 fprintf_unfiltered
620                   (gdb_stderr,
621                  _("warning: could not set timeout limit to `%s'.\n"), optarg);
622               else
623                 remote_timeout = i;
624             }
625             break;
626
627           case '?':
628             fprintf_unfiltered (gdb_stderr,
629                         _("Use `%s --help' for a complete list of options.\n"),
630                                 argv[0]);
631             exit (1);
632           }
633       }
634
635     /* If --help or --version, disable window interface.  */
636     if (print_help || print_version)
637       {
638         use_windows = 0;
639       }
640
641     if (set_args)
642       {
643         /* The remaining options are the command-line options for the
644            inferior.  The first one is the sym/exec file, and the rest
645            are arguments.  */
646         if (optind >= argc)
647           {
648             fprintf_unfiltered (gdb_stderr,
649                                 _("%s: `--args' specified but no program specified\n"),
650                                 argv[0]);
651             exit (1);
652           }
653         symarg = argv[optind];
654         execarg = argv[optind];
655         ++optind;
656         set_inferior_args_vector (argc - optind, &argv[optind]);
657       }
658     else
659       {
660         /* OK, that's all the options.  */
661
662         /* The first argument, if specified, is the name of the
663            executable.  */
664         if (optind < argc)
665           {
666             symarg = argv[optind];
667             execarg = argv[optind];
668             optind++;
669           }
670
671         /* If the user hasn't already specified a PID or the name of a
672            core file, then a second optional argument is allowed.  If
673            present, this argument should be interpreted as either a
674            PID or a core file, whichever works.  */
675         if (pidarg == NULL && corearg == NULL && optind < argc)
676           {
677             pid_or_core_arg = argv[optind];
678             optind++;
679           }
680
681         /* Any argument left on the command line is unexpected and
682            will be ignored.  Inform the user.  */
683         if (optind < argc)
684           fprintf_unfiltered (gdb_stderr, _("\
685 Excess command line arguments ignored. (%s%s)\n"),
686                               argv[optind],
687                               (optind == argc - 1) ? "" : " ...");
688       }
689     if (batch)
690       quiet = 1;
691   }
692
693   /* Initialize all files.  Give the interpreter a chance to take
694      control of the console via the deprecated_init_ui_hook ().  */
695   gdb_init (argv[0]);
696
697   /* Do these (and anything which might call wrap_here or *_filtered)
698      after initialize_all_files() but before the interpreter has been
699      installed.  Otherwize the help/version messages will be eaten by
700      the interpreter's output handler.  */
701
702   if (print_version)
703     {
704       print_gdb_version (gdb_stdout);
705       wrap_here ("");
706       printf_filtered ("\n");
707       exit (0);
708     }
709
710   if (print_help)
711     {
712       print_gdb_help (gdb_stdout);
713       fputs_unfiltered ("\n", gdb_stdout);
714       exit (0);
715     }
716
717   /* FIXME: cagney/2003-02-03: The big hack (part 1 of 2) that lets
718      GDB retain the old MI1 interpreter startup behavior.  Output the
719      copyright message before the interpreter is installed.  That way
720      it isn't encapsulated in MI output.  */
721   if (!quiet && strcmp (interpreter_p, INTERP_MI1) == 0)
722     {
723       /* Print all the junk at the top, with trailing "..." if we are about
724          to read a symbol file (possibly slowly).  */
725       print_gdb_version (gdb_stdout);
726       if (symarg)
727         printf_filtered ("..");
728       wrap_here ("");
729       printf_filtered ("\n");
730       gdb_flush (gdb_stdout);   /* Force to screen during slow operations */
731     }
732
733
734   /* Install the default UI.  All the interpreters should have had a
735      look at things by now.  Initialize the default interpreter. */
736
737   {
738     /* Find it.  */
739     struct interp *interp = interp_lookup (interpreter_p);
740     if (interp == NULL)
741       error (_("Interpreter `%s' unrecognized"), interpreter_p);
742     /* Install it.  */
743     if (!interp_set (interp, 1))
744       {
745         fprintf_unfiltered (gdb_stderr,
746                             "Interpreter `%s' failed to initialize.\n",
747                             interpreter_p);
748         exit (1);
749       }
750   }
751
752   /* FIXME: cagney/2003-02-03: The big hack (part 2 of 2) that lets
753      GDB retain the old MI1 interpreter startup behavior.  Output the
754      copyright message after the interpreter is installed when it is
755      any sane interpreter.  */
756   if (!quiet && !current_interp_named_p (INTERP_MI1))
757     {
758       /* Print all the junk at the top, with trailing "..." if we are about
759          to read a symbol file (possibly slowly).  */
760       print_gdb_version (gdb_stdout);
761       if (symarg)
762         printf_filtered ("..");
763       wrap_here ("");
764       printf_filtered ("\n");
765       gdb_flush (gdb_stdout);   /* Force to screen during slow operations */
766     }
767
768   /* Set off error and warning messages with a blank line.  */
769   error_pre_print = "\n";
770   quit_pre_print = error_pre_print;
771   warning_pre_print = _("\nwarning: ");
772
773   /* Read and execute the system-wide gdbinit file, if it exists.
774      This is done *before* all the command line arguments are
775      processed; it sets global parameters, which are independent of
776      what file you are debugging or what directory you are in.  */
777   if (system_gdbinit && !inhibit_gdbinit)
778     catch_command_errors (source_script, system_gdbinit, 0, RETURN_MASK_ALL);
779
780   /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
781      *before* all the command line arguments are processed; it sets
782      global parameters, which are independent of what file you are
783      debugging or what directory you are in.  */
784
785   if (home_gdbinit && !inhibit_gdbinit)
786     catch_command_errors (source_script, home_gdbinit, 0, RETURN_MASK_ALL);
787
788   /* Now perform all the actions indicated by the arguments.  */
789   if (cdarg != NULL)
790     {
791       catch_command_errors (cd_command, cdarg, 0, RETURN_MASK_ALL);
792     }
793
794   for (i = 0; i < ndir; i++)
795     catch_command_errors (directory_switch, dirarg[i], 0, RETURN_MASK_ALL);
796   xfree (dirarg);
797
798   if (execarg != NULL
799       && symarg != NULL
800       && strcmp (execarg, symarg) == 0)
801     {
802       /* The exec file and the symbol-file are the same.  If we can't
803          open it, better only print one error message.
804          catch_command_errors returns non-zero on success! */
805       if (catch_command_errors (exec_file_attach, execarg, !batch, RETURN_MASK_ALL))
806         catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL);
807     }
808   else
809     {
810       if (execarg != NULL)
811         catch_command_errors (exec_file_attach, execarg, !batch, RETURN_MASK_ALL);
812       if (symarg != NULL)
813         catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL);
814     }
815
816   if (corearg && pidarg)
817     error (_("\
818 Can't attach to process and specify a core file at the same time."));
819
820   if (corearg != NULL)
821     catch_command_errors (core_file_command, corearg,
822                           !batch, RETURN_MASK_ALL);
823   else if (pidarg != NULL)
824     catch_command_errors (attach_command, pidarg,
825                           !batch, RETURN_MASK_ALL);
826   else if (pid_or_core_arg)
827     {
828       /* The user specified 'gdb program pid' or gdb program core'.
829          If pid_or_core_arg's first character is a digit, try attach
830          first and then corefile.  Otherwise try just corefile.  */
831
832       if (isdigit (pid_or_core_arg[0]))
833         {
834           if (catch_command_errors (attach_command, pid_or_core_arg,
835                                     !batch, RETURN_MASK_ALL) == 0)
836             catch_command_errors (core_file_command, pid_or_core_arg,
837                                   !batch, RETURN_MASK_ALL);
838         }
839       else /* Can't be a pid, better be a corefile.  */
840         catch_command_errors (core_file_command, pid_or_core_arg,
841                               !batch, RETURN_MASK_ALL);
842     }
843
844   if (ttyarg != NULL)
845     catch_command_errors (tty_command, ttyarg, !batch, RETURN_MASK_ALL);
846
847   /* Error messages should no longer be distinguished with extra output. */
848   error_pre_print = NULL;
849   quit_pre_print = NULL;
850   warning_pre_print = _("warning: ");
851
852   /* Read the .gdbinit file in the current directory, *if* it isn't
853      the same as the $HOME/.gdbinit file (it should exist, also).  */
854   if (local_gdbinit && !inhibit_gdbinit)
855     catch_command_errors (source_script, local_gdbinit, 0, RETURN_MASK_ALL);
856
857   for (i = 0; i < ncmd; i++)
858     {
859       if (cmdarg[i].type == CMDARG_FILE)
860         catch_command_errors (source_script, cmdarg[i].string,
861                               !batch, RETURN_MASK_ALL);
862       else  /* cmdarg[i].type == CMDARG_COMMAND */
863         catch_command_errors (execute_command, cmdarg[i].string,
864                               !batch, RETURN_MASK_ALL);
865     }
866   xfree (cmdarg);
867
868   /* Read in the old history after all the command files have been read. */
869   init_history ();
870
871   if (batch)
872     {
873       /* We have hit the end of the batch file.  */
874       quit_force (NULL, 0);
875     }
876
877   /* Show time and/or space usage.  */
878
879   if (display_time)
880     {
881       long init_time = get_run_time () - time_at_startup;
882
883       printf_unfiltered (_("Startup time: %ld.%06ld\n"),
884                          init_time / 1000000, init_time % 1000000);
885     }
886
887   if (display_space)
888     {
889 #ifdef HAVE_SBRK
890       extern char **environ;
891       char *lim = (char *) sbrk (0);
892
893       printf_unfiltered (_("Startup size: data size %ld\n"),
894                          (long) (lim - (char *) &environ));
895 #endif
896     }
897
898   /* NOTE: cagney/1999-11-07: There is probably no reason for not
899      moving this loop and the code found in captured_command_loop()
900      into the command_loop() proper.  The main thing holding back that
901      change - SET_TOP_LEVEL() - has been eliminated. */
902   while (1)
903     {
904       catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
905     }
906   /* No exit -- exit is through quit_command.  */
907 }
908
909 int
910 gdb_main (struct captured_main_args *args)
911 {
912   use_windows = args->use_windows;
913   catch_errors (captured_main, args, "", RETURN_MASK_ALL);
914   /* The only way to end up here is by an error (normal exit is
915      handled by quit_force()), hence always return an error status.  */
916   return 1;
917 }
918
919
920 /* Don't use *_filtered for printing help.  We don't want to prompt
921    for continue no matter how small the screen or how much we're going
922    to print.  */
923
924 static void
925 print_gdb_help (struct ui_file *stream)
926 {
927   char *system_gdbinit;
928   char *home_gdbinit;
929   char *local_gdbinit;
930
931   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
932
933   fputs_unfiltered (_("\
934 This is the GNU debugger.  Usage:\n\n\
935     gdb [options] [executable-file [core-file or process-id]]\n\
936     gdb [options] --args executable-file [inferior-arguments ...]\n\n\
937 Options:\n\n\
938 "), stream);
939   fputs_unfiltered (_("\
940   --args             Arguments after executable-file are passed to inferior\n\
941 "), stream);
942   fputs_unfiltered (_("\
943   -b BAUDRATE        Set serial port baud rate used for remote debugging.\n\
944   --batch            Exit after processing options.\n\
945   --batch-silent     As for --batch, but suppress all gdb stdout output.\n\
946   --return-child-result\n\
947                      GDB exit code will be the child's exit code.\n\
948   --cd=DIR           Change current directory to DIR.\n\
949   --command=FILE, -x Execute GDB commands from FILE.\n\
950   --eval-command=COMMAND, -ex\n\
951                      Execute a single GDB command.\n\
952                      May be used multiple times and in conjunction\n\
953                      with --command.\n\
954   --core=COREFILE    Analyze the core dump COREFILE.\n\
955   --pid=PID          Attach to running process PID.\n\
956 "), stream);
957   fputs_unfiltered (_("\
958   --dbx              DBX compatibility mode.\n\
959   --directory=DIR    Search for source files in DIR.\n\
960   --epoch            Output information used by epoch emacs-GDB interface.\n\
961   --exec=EXECFILE    Use EXECFILE as the executable.\n\
962   --fullname         Output information used by emacs-GDB interface.\n\
963   --help             Print this message.\n\
964 "), stream);
965   fputs_unfiltered (_("\
966   --interpreter=INTERP\n\
967                      Select a specific interpreter / user interface\n\
968 "), stream);
969   fputs_unfiltered (_("\
970   -l TIMEOUT         Set timeout in seconds for remote debugging.\n\
971   --nw               Do not use a window interface.\n\
972   --nx               Do not read "), stream);
973   fputs_unfiltered (gdbinit, stream);
974   fputs_unfiltered (_(" file.\n\
975   --quiet            Do not print version number on startup.\n\
976   --readnow          Fully read symbol files on first access.\n\
977 "), stream);
978   fputs_unfiltered (_("\
979   --se=FILE          Use FILE as symbol file and executable file.\n\
980   --symbols=SYMFILE  Read symbols from SYMFILE.\n\
981   --tty=TTY          Use TTY for input/output by the program being debugged.\n\
982 "), stream);
983 #if defined(TUI)
984   fputs_unfiltered (_("\
985   --tui              Use a terminal user interface.\n\
986 "), stream);
987 #endif
988   fputs_unfiltered (_("\
989   --version          Print version information and then exit.\n\
990   -w                 Use a window interface.\n\
991   --write            Set writing into executable and core files.\n\
992   --xdb              XDB compatibility mode.\n\
993 "), stream);
994   fputs_unfiltered (_("\n\
995 At startup, GDB reads the following init files and executes their commands:\n\
996 "), stream);
997   if (system_gdbinit)
998     fprintf_unfiltered (stream, _("\
999    * system-wide init file: %s\n\
1000 "), system_gdbinit);
1001   if (home_gdbinit)
1002     fprintf_unfiltered (stream, _("\
1003    * user-specific init file: %s\n\
1004 "), home_gdbinit);
1005   if (local_gdbinit)
1006     fprintf_unfiltered (stream, _("\
1007    * local init file: ./%s\n\
1008 "), local_gdbinit);
1009   fputs_unfiltered (_("\n\
1010 For more information, type \"help\" from within GDB, or consult the\n\
1011 GDB manual (available as on-line info or a printed manual).\n\
1012 "), stream);
1013   if (REPORT_BUGS_TO[0] && stream == gdb_stdout)
1014     fprintf_unfiltered (stream, _("\
1015 Report bugs to \"%s\".\n\
1016 "), REPORT_BUGS_TO);
1017 }