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