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