]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/main.c
* ada-tasks (task_command_1): Call target_find_new_threads.
[thirdparty/binutils-gdb.git] / gdb / main.c
CommitLineData
c906108c 1/* Top level stuff for GDB, the GNU debugger.
4389a95a 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
0fb0cc75
JB
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
5 2009 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
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.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
c906108c
SS
23#include "top.h"
24#include "target.h"
25#include "inferior.h"
1adeb98a
FN
26#include "symfile.h"
27#include "gdbcore.h"
c906108c 28
60250e8b 29#include "exceptions.h"
c906108c
SS
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"
9e0b60a8 37#include "event-loop.h"
8b93c638 38#include "ui-out.h"
6457bd47 39
4389a95a 40#include "interps.h"
f15ab4a7
AC
41#include "main.h"
42
29b0e8a2
JM
43#include "source.h"
44
c906108c
SS
45/* If nonzero, display time usage both at startup and for each command. */
46
47int display_time;
48
49/* If nonzero, display space usage both at startup and for each command. */
50
51int display_space;
52
4389a95a
AC
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. */
fb40c209 56char *interpreter_p;
fb40c209 57
c906108c
SS
58/* Whether xdb commands will be handled */
59int xdb_commands = 0;
60
61/* Whether dbx commands will be handled */
62int dbx_commands = 0;
63
030292b7
DJ
64/* System root path, used to find libraries etc. */
65char *gdb_sysroot = 0;
66
d9fcf2fb
JM
67struct ui_file *gdb_stdout;
68struct ui_file *gdb_stderr;
69struct ui_file *gdb_stdlog;
449092f6
CV
70struct ui_file *gdb_stdin;
71/* target IO streams */
72struct ui_file *gdb_stdtargin;
22e8e3c7 73struct ui_file *gdb_stdtarg;
449092f6 74struct ui_file *gdb_stdtargerr;
c906108c 75
1a088d06
AS
76/* Support for the --batch-silent option. */
77int batch_silent = 0;
78
4b0ad762
AS
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. */
82int return_child_result = 0;
83int return_child_result_value = -1;
84
c906108c
SS
85/* Whether to enable writing into executable and core files */
86extern int write_files;
87
16e7150e
JG
88/* GDB as it has been invoked from the command line (i.e. argv[0]). */
89static char *gdb_program_name;
90
d9fcf2fb 91static void print_gdb_help (struct ui_file *);
c906108c
SS
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
c5aa993b 96extern char *external_editor_command;
c906108c 97
16e7150e
JG
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. */
103static void
104get_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
11cf8741
JM
174/* Call command_loop. If it happens to return, pass that through as a
175 non-zero return status. */
176
177static int
178captured_command_loop (void *data)
c906108c 179{
4389a95a 180 current_interp_command_loop ();
11cf8741
JM
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
e26cc349 184 the do_cleanups() below is redundant. Unfortunately, many FUNCs
11cf8741
JM
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
11cf8741
JM
197static int
198captured_main (void *data)
199{
200 struct captured_main_args *context = data;
201 int argc = context->argc;
202 char **argv = context->argv;
c906108c
SS
203 static int quiet = 0;
204 static int batch = 0;
552c04a7 205 static int set_args = 0;
c906108c
SS
206
207 /* Pointers to various arguments from command line. */
208 char *symarg = NULL;
209 char *execarg = NULL;
a4d9b460 210 char *pidarg = NULL;
c906108c 211 char *corearg = NULL;
a4d9b460 212 char *pid_or_core_arg = NULL;
c906108c
SS
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. */
8a5a3c82
AS
221 struct cmdarg {
222 enum {
223 CMDARG_FILE,
224 CMDARG_COMMAND
225 } type;
226 char *string;
227 } *cmdarg;
c906108c
SS
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;
c5aa993b 239
16e7150e
JG
240 /* gdb init files. */
241 char *system_gdbinit;
242 char *home_gdbinit;
243 char *local_gdbinit;
c906108c 244
52f0bd74 245 int i;
c906108c
SS
246
247 long time_at_startup = get_run_time ();
248
0fbb3da7
TT
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
6dd77b81
RH
258#ifdef HAVE_SBRK
259 lim_at_start = (char *) sbrk (0);
260#endif
261
c906108c 262 cmdsize = 1;
8a5a3c82 263 cmdarg = (struct cmdarg *) xmalloc (cmdsize * sizeof (*cmdarg));
c906108c
SS
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
da59e081
JM
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 */
449092f6
CV
278 gdb_stdin = stdio_fileopen (stdin);
279 gdb_stdtargerr = gdb_stderr; /* for moment */
280 gdb_stdtargin = gdb_stdin; /* for moment */
c906108c 281
16e7150e
JG
282 gdb_program_name = xstrdup (argv[0]);
283
bf1d7d9c
JB
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
030292b7
DJ
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 {
a84d24ee 307 xfree (gdb_sysroot);
aa28a74e 308 gdb_sysroot = xstrdup (TARGET_SYSTEM_ROOT);
030292b7
DJ
309 }
310 }
311 else
aa28a74e 312 gdb_sysroot = xstrdup (TARGET_SYSTEM_ROOT);
030292b7 313#else
aa28a74e 314 gdb_sysroot = xstrdup (TARGET_SYSTEM_ROOT);
030292b7 315#endif
aa28a74e
DJ
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);
030292b7
DJ
349#endif
350
aa28a74e
DJ
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
16e7150e
JG
362 get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
363
29b0e8a2
JM
364#ifdef RELOC_SRCDIR
365 add_substitute_path_rule (RELOC_SRCDIR,
366 make_relative_prefix (argv[0], BINDIR,
367 RELOC_SRCDIR));
368#endif
369
4389a95a 370 /* There will always be an interpreter. Either the one passed into
e46e5ccd
KS
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);
4389a95a 375
c906108c
SS
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). */
49c7e338
AC
382 enum {
383 OPT_SE = 10,
384 OPT_CD,
385 OPT_ANNOTATE,
386 OPT_STATISTICS,
42fa7c0f
AC
387 OPT_TUI,
388 OPT_NOWINDOWS,
389 OPT_WINDOWS
49c7e338 390 };
c906108c 391 static struct option long_options[] =
c5aa993b 392 {
49c7e338 393 {"tui", no_argument, 0, OPT_TUI},
c5aa993b
JM
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},
c5aa993b
JM
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},
1a088d06 403 {"batch-silent", no_argument, 0, 'B'},
c5aa993b
JM
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
49c7e338 413 {"annotate", required_argument, 0, OPT_ANNOTATE},
c5aa993b 414 {"help", no_argument, &print_help, 1},
49c7e338 415 {"se", required_argument, 0, OPT_SE},
c5aa993b
JM
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'},
00546b04
MS
422 {"pid", required_argument, 0, 'p'},
423 {"p", required_argument, 0, 'p'},
c5aa993b 424 {"command", required_argument, 0, 'x'},
8a5a3c82 425 {"eval-command", required_argument, 0, 'X'},
c5aa993b
JM
426 {"version", no_argument, &print_version, 1},
427 {"x", required_argument, 0, 'x'},
8a5a3c82 428 {"ex", required_argument, 0, 'X'},
3fc11d3e
JM
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
8b93c638
JM
434 {"ui", required_argument, 0, 'i'},
435 {"interpreter", required_argument, 0, 'i'},
436 {"i", required_argument, 0, 'i'},
c5aa993b 437 {"directory", required_argument, 0, 'd'},
c4093a6a 438 {"d", required_argument, 0, 'd'},
49c7e338 439 {"cd", required_argument, 0, OPT_CD},
c5aa993b
JM
440 {"tty", required_argument, 0, 't'},
441 {"baud", required_argument, 0, 'b'},
442 {"b", required_argument, 0, 'b'},
42fa7c0f
AC
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},
49c7e338 447 {"statistics", no_argument, 0, OPT_STATISTICS},
c5aa993b 448 {"write", no_argument, &write_files, 1},
552c04a7 449 {"args", no_argument, &set_args, 1},
f47b1503 450 {"l", required_argument, 0, 'l'},
4b0ad762 451 {"return-child-result", no_argument, &return_child_result, 1},
c5aa993b
JM
452 {0, no_argument, 0, 0}
453 };
c906108c
SS
454
455 while (1)
456 {
457 int option_index;
458
459 c = getopt_long_only (argc, argv, "",
460 long_options, &option_index);
552c04a7 461 if (c == EOF || set_args)
c906108c
SS
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;
49c7e338 473 case OPT_SE:
c906108c
SS
474 symarg = optarg;
475 execarg = optarg;
476 break;
49c7e338 477 case OPT_CD:
c906108c
SS
478 cdarg = optarg;
479 break;
49c7e338 480 case OPT_ANNOTATE:
c906108c
SS
481 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
482 annotation_level = atoi (optarg);
483 break;
49c7e338 484 case OPT_STATISTICS:
c906108c
SS
485 /* Enable the display of both time and space usage. */
486 display_time = 1;
487 display_space = 1;
488 break;
49c7e338 489 case OPT_TUI:
021e7609 490 /* --tui is equivalent to -i=tui. */
b0da54f1 491#ifdef TUI
021e7609 492 xfree (interpreter_p);
cc4349ed 493 interpreter_p = xstrdup (INTERP_TUI);
b0da54f1
BW
494#else
495 fprintf_unfiltered (gdb_stderr,
496 _("%s: TUI mode is not supported\n"),
497 argv[0]);
498 exit (1);
499#endif
021e7609 500 break;
42fa7c0f
AC
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. */
cc4349ed
AS
504#ifdef GDBTK
505 /* --windows is equivalent to -i=insight. */
506 xfree (interpreter_p);
507 interpreter_p = xstrdup (INTERP_INSIGHT);
508#endif
42fa7c0f
AC
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;
c906108c
SS
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;
00546b04 531 case 'p':
a4d9b460 532 pidarg = optarg;
00546b04 533 break;
c906108c 534 case 'x':
8a5a3c82
AS
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;
c906108c
SS
547 if (ncmd >= cmdsize)
548 {
549 cmdsize *= 2;
8a5a3c82
AS
550 cmdarg = xrealloc ((char *) cmdarg,
551 cmdsize * sizeof (*cmdarg));
c906108c
SS
552 }
553 break;
1a088d06
AS
554 case 'B':
555 batch = batch_silent = 1;
556 gdb_stdout = ui_file_new();
557 break;
3fc11d3e
JM
558#ifdef GDBTK
559 case 'z':
560 {
a14ed312 561extern int gdbtk_test (char *);
3fc11d3e
JM
562 if (!gdbtk_test (optarg))
563 {
defc6f8c 564 fprintf_unfiltered (gdb_stderr, _("%s: unable to load tclcommand file \"%s\""),
3fc11d3e
JM
565 argv[0], optarg);
566 exit (1);
567 }
568 break;
569 }
570 case 'y':
78f49586
TT
571 /* Backwards compatibility only. */
572 break;
3fc11d3e
JM
573 case 'w':
574 {
3fc11d3e
JM
575 external_editor_command = xstrdup (optarg);
576 break;
577 }
578#endif /* GDBTK */
fb40c209 579 case 'i':
4389a95a
AC
580 xfree (interpreter_p);
581 interpreter_p = xstrdup (optarg);
fb40c209 582 break;
c906108c
SS
583 case 'd':
584 dirarg[ndir++] = optarg;
585 if (ndir >= dirsize)
586 {
587 dirsize *= 2;
c5aa993b 588 dirarg = (char **) xrealloc ((char *) dirarg,
c906108c
SS
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
c5aa993b 607 current_target) until after initialize_all_files(). */
c906108c
SS
608
609 fprintf_unfiltered
610 (gdb_stderr,
defc6f8c 611 _("warning: could not set baud rate to `%s'.\n"), optarg);
c906108c
SS
612 else
613 baud_rate = i;
614 }
046ca86a 615 break;
c906108c
SS
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
c5aa993b 625 current_target) until after initialize_all_files(). */
c906108c
SS
626
627 fprintf_unfiltered
628 (gdb_stderr,
defc6f8c 629 _("warning: could not set timeout limit to `%s'.\n"), optarg);
c906108c
SS
630 else
631 remote_timeout = i;
632 }
633 break;
634
c906108c
SS
635 case '?':
636 fprintf_unfiltered (gdb_stderr,
defc6f8c 637 _("Use `%s --help' for a complete list of options.\n"),
c5aa993b 638 argv[0]);
c906108c
SS
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;
c906108c
SS
647 }
648
552c04a7
TT
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,
defc6f8c 657 _("%s: `--args' specified but no program specified\n"),
552c04a7
TT
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 {
a4d9b460
PA
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, _("\
693Excess command line arguments ignored. (%s%s)\n"),
694 argv[optind],
695 (optind == argc - 1) ? "" : " ...");
552c04a7 696 }
c906108c
SS
697 if (batch)
698 quiet = 1;
699 }
700
0f71a2f6 701 /* Initialize all files. Give the interpreter a chance to take
ba5e7e8d 702 control of the console via the deprecated_init_ui_hook (). */
c906108c
SS
703 gdb_init (argv[0]);
704
705 /* Do these (and anything which might call wrap_here or *_filtered)
4389a95a
AC
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
c906108c
SS
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
4389a95a
AC
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 ("");
e896d70e 737 printf_filtered ("\n");
4389a95a
AC
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)
8a3fe4f8 749 error (_("Interpreter `%s' unrecognized"), interpreter_p);
4389a95a 750 /* Install it. */
683f2885 751 if (!interp_set (interp, 1))
4389a95a
AC
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))
c906108c
SS
765 {
766 /* Print all the junk at the top, with trailing "..." if we are about
c5aa993b 767 to read a symbol file (possibly slowly). */
c906108c
SS
768 print_gdb_version (gdb_stdout);
769 if (symarg)
770 printf_filtered ("..");
c5aa993b 771 wrap_here ("");
e896d70e 772 printf_filtered ("\n");
c5aa993b 773 gdb_flush (gdb_stdout); /* Force to screen during slow operations */
c906108c
SS
774 }
775
e896d70e
DJ
776 /* Set off error and warning messages with a blank line. */
777 error_pre_print = "\n";
c906108c 778 quit_pre_print = error_pre_print;
defc6f8c 779 warning_pre_print = _("\nwarning: ");
c906108c 780
16e7150e
JG
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
c906108c
SS
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. */
c906108c 792
16e7150e
JG
793 if (home_gdbinit && !inhibit_gdbinit)
794 catch_command_errors (source_script, home_gdbinit, 0, RETURN_MASK_ALL);
c906108c
SS
795
796 /* Now perform all the actions indicated by the arguments. */
797 if (cdarg != NULL)
798 {
11cf8741 799 catch_command_errors (cd_command, cdarg, 0, RETURN_MASK_ALL);
c906108c 800 }
c906108c
SS
801
802 for (i = 0; i < ndir; i++)
13d35ae5 803 catch_command_errors (directory_switch, dirarg[i], 0, RETURN_MASK_ALL);
b8c9b27d 804 xfree (dirarg);
c906108c
SS
805
806 if (execarg != NULL
807 && symarg != NULL
5cb316ef 808 && strcmp (execarg, symarg) == 0)
c906108c 809 {
11cf8741
JM
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! */
1adeb98a
FN
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);
c906108c
SS
815 }
816 else
817 {
818 if (execarg != NULL)
1adeb98a 819 catch_command_errors (exec_file_attach, execarg, !batch, RETURN_MASK_ALL);
c906108c 820 if (symarg != NULL)
1adeb98a 821 catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL);
c906108c 822 }
c906108c 823
a4d9b460
PA
824 if (corearg && pidarg)
825 error (_("\
826Can't attach to process and specify a core file at the same time."));
827
c906108c 828 if (corearg != NULL)
a4d9b460
PA
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)
c906108c 835 {
a4d9b460
PA
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. */
00546b04 839
a4d9b460 840 if (isdigit (pid_or_core_arg[0]))
11cf8741 841 {
a4d9b460 842 if (catch_command_errors (attach_command, pid_or_core_arg,
00546b04 843 !batch, RETURN_MASK_ALL) == 0)
a4d9b460 844 catch_command_errors (core_file_command, pid_or_core_arg,
00546b04 845 !batch, RETURN_MASK_ALL);
11cf8741 846 }
a4d9b460
PA
847 else /* Can't be a pid, better be a corefile. */
848 catch_command_errors (core_file_command, pid_or_core_arg,
00546b04 849 !batch, RETURN_MASK_ALL);
c906108c 850 }
c906108c
SS
851
852 if (ttyarg != NULL)
11cf8741 853 catch_command_errors (tty_command, ttyarg, !batch, RETURN_MASK_ALL);
c906108c 854
c906108c
SS
855 /* Error messages should no longer be distinguished with extra output. */
856 error_pre_print = NULL;
857 quit_pre_print = NULL;
defc6f8c 858 warning_pre_print = _("warning: ");
c906108c
SS
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). */
16e7150e
JG
862 if (local_gdbinit && !inhibit_gdbinit)
863 catch_command_errors (source_script, local_gdbinit, 0, RETURN_MASK_ALL);
c906108c
SS
864
865 for (i = 0; i < ncmd; i++)
866 {
8a5a3c82 867 if (cmdarg[i].type == CMDARG_FILE)
16026cd7 868 catch_command_errors (source_script, cmdarg[i].string,
8a5a3c82
AS
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);
c906108c 873 }
b8c9b27d 874 xfree (cmdarg);
c906108c
SS
875
876 /* Read in the old history after all the command files have been read. */
c5aa993b 877 init_history ();
c906108c
SS
878
879 if (batch)
880 {
881 /* We have hit the end of the batch file. */
4b0ad762 882 quit_force (NULL, 0);
c906108c
SS
883 }
884
c906108c
SS
885 /* Show time and/or space usage. */
886
887 if (display_time)
888 {
889 long init_time = get_run_time () - time_at_startup;
890
defc6f8c 891 printf_unfiltered (_("Startup time: %ld.%06ld\n"),
c906108c
SS
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
defc6f8c 901 printf_unfiltered (_("Startup size: data size %ld\n"),
c906108c
SS
902 (long) (lim - (char *) &environ));
903#endif
904 }
905
11cf8741
JM
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 }
11cf8741
JM
914 /* No exit -- exit is through quit_command. */
915}
c906108c 916
11cf8741 917int
f15ab4a7 918gdb_main (struct captured_main_args *args)
11cf8741 919{
f15ab4a7
AC
920 use_windows = args->use_windows;
921 catch_errors (captured_main, args, "", RETURN_MASK_ALL);
864dbc90
AC
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;
c906108c
SS
925}
926
11cf8741 927
c906108c
SS
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
932static void
d9fcf2fb 933print_gdb_help (struct ui_file *stream)
c906108c 934{
16e7150e
JG
935 char *system_gdbinit;
936 char *home_gdbinit;
937 char *local_gdbinit;
938
939 get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
940
defc6f8c 941 fputs_unfiltered (_("\
c906108c 942This is the GNU debugger. Usage:\n\n\
552c04a7
TT
943 gdb [options] [executable-file [core-file or process-id]]\n\
944 gdb [options] --args executable-file [inferior-arguments ...]\n\n\
c906108c 945Options:\n\n\
defc6f8c
TT
946"), stream);
947 fputs_unfiltered (_("\
552c04a7 948 --args Arguments after executable-file are passed to inferior\n\
defc6f8c
TT
949"), stream);
950 fputs_unfiltered (_("\
c906108c
SS
951 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
952 --batch Exit after processing options.\n\
1a088d06 953 --batch-silent As for --batch, but suppress all gdb stdout output.\n\
4b0ad762
AS
954 --return-child-result\n\
955 GDB exit code will be the child's exit code.\n\
c906108c 956 --cd=DIR Change current directory to DIR.\n\
8a5a3c82
AS
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\
c906108c 962 --core=COREFILE Analyze the core dump COREFILE.\n\
00546b04 963 --pid=PID Attach to running process PID.\n\
defc6f8c
TT
964"), stream);
965 fputs_unfiltered (_("\
c906108c
SS
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\
defc6f8c
TT
972"), stream);
973 fputs_unfiltered (_("\
8b93c638
JM
974 --interpreter=INTERP\n\
975 Select a specific interpreter / user interface\n\
defc6f8c
TT
976"), stream);
977 fputs_unfiltered (_("\
f47b1503 978 -l TIMEOUT Set timeout in seconds for remote debugging.\n\
c906108c 979 --nw Do not use a window interface.\n\
defc6f8c 980 --nx Do not read "), stream);
96baa820 981 fputs_unfiltered (gdbinit, stream);
defc6f8c 982 fputs_unfiltered (_(" file.\n\
c906108c
SS
983 --quiet Do not print version number on startup.\n\
984 --readnow Fully read symbol files on first access.\n\
defc6f8c
TT
985"), stream);
986 fputs_unfiltered (_("\
c906108c
SS
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\
defc6f8c 990"), stream);
c906108c 991#if defined(TUI)
defc6f8c 992 fputs_unfiltered (_("\
c906108c 993 --tui Use a terminal user interface.\n\
defc6f8c 994"), stream);
c906108c 995#endif
defc6f8c 996 fputs_unfiltered (_("\
c906108c
SS
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\
defc6f8c 1001"), stream);
defc6f8c 1002 fputs_unfiltered (_("\n\
16e7150e
JG
1003At 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\
c906108c
SS
1018For more information, type \"help\" from within GDB, or consult the\n\
1019GDB manual (available as on-line info or a printed manual).\n\
defc6f8c 1020"), stream);
c16158bc
JM
1021 if (REPORT_BUGS_TO[0] && stream == gdb_stdout)
1022 fprintf_unfiltered (stream, _("\
1023Report bugs to \"%s\".\n\
1024"), REPORT_BUGS_TO);
c906108c 1025}