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