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