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