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