Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Top level stuff for GDB, the GNU debugger. |
b6ba6518 | 2 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
030292b7 | 3 | 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 |
00546b04 | 4 | Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
c906108c SS |
24 | #include "top.h" |
25 | #include "target.h" | |
26 | #include "inferior.h" | |
1adeb98a FN |
27 | #include "symfile.h" |
28 | #include "gdbcore.h" | |
c906108c SS |
29 | |
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 | |
f15ab4a7 AC |
40 | #include "main.h" |
41 | ||
c906108c SS |
42 | /* If nonzero, display time usage both at startup and for each command. */ |
43 | ||
44 | int display_time; | |
45 | ||
46 | /* If nonzero, display space usage both at startup and for each command. */ | |
47 | ||
48 | int display_space; | |
49 | ||
cd0fc7c3 | 50 | /* Whether this is the async version or not. The async version is |
c5aa993b JM |
51 | invoked on the command line with the -nw --async options. In this |
52 | version, the usual command_loop is substituted by and event loop which | |
53 | processes UI events asynchronously. */ | |
6426a772 | 54 | int event_loop_p = 1; |
cd0fc7c3 | 55 | |
fb40c209 AC |
56 | /* Has an interpreter been specified and if so, which. */ |
57 | char *interpreter_p; | |
fb40c209 | 58 | |
c906108c SS |
59 | /* Whether this is the command line version or not */ |
60 | int tui_version = 0; | |
61 | ||
62 | /* Whether xdb commands will be handled */ | |
63 | int xdb_commands = 0; | |
64 | ||
65 | /* Whether dbx commands will be handled */ | |
66 | int dbx_commands = 0; | |
67 | ||
030292b7 DJ |
68 | /* System root path, used to find libraries etc. */ |
69 | char *gdb_sysroot = 0; | |
70 | ||
d9fcf2fb JM |
71 | struct ui_file *gdb_stdout; |
72 | struct ui_file *gdb_stderr; | |
73 | struct ui_file *gdb_stdlog; | |
74 | struct ui_file *gdb_stdtarg; | |
c906108c | 75 | |
2acceee2 JM |
76 | /* Used to initialize error() - defined in utils.c */ |
77 | ||
78 | extern void error_init (void); | |
79 | ||
c906108c SS |
80 | /* Whether to enable writing into executable and core files */ |
81 | extern int write_files; | |
82 | ||
d9fcf2fb | 83 | static void print_gdb_help (struct ui_file *); |
c906108c SS |
84 | |
85 | /* These two are used to set the external editor commands when gdb is farming | |
86 | out files to be edited by another program. */ | |
87 | ||
c5aa993b | 88 | extern char *external_editor_command; |
c906108c | 89 | |
11cf8741 JM |
90 | /* Call command_loop. If it happens to return, pass that through as a |
91 | non-zero return status. */ | |
92 | ||
93 | static int | |
94 | captured_command_loop (void *data) | |
c906108c | 95 | { |
11cf8741 JM |
96 | if (command_loop_hook == NULL) |
97 | command_loop (); | |
98 | else | |
99 | command_loop_hook (); | |
100 | /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton | |
101 | would clean things up (restoring the cleanup chain) to the state | |
102 | they were just prior to the call. Technically, this means that | |
e26cc349 | 103 | the do_cleanups() below is redundant. Unfortunately, many FUNCs |
11cf8741 JM |
104 | are not that well behaved. do_cleanups should either be replaced |
105 | with a do_cleanups call (to cover the problem) or an assertion | |
106 | check to detect bad FUNCs code. */ | |
107 | do_cleanups (ALL_CLEANUPS); | |
108 | /* If the command_loop returned, normally (rather than threw an | |
109 | error) we try to quit. If the quit is aborted, catch_errors() | |
110 | which called this catch the signal and restart the command | |
111 | loop. */ | |
112 | quit_command (NULL, instream == stdin); | |
113 | return 1; | |
114 | } | |
115 | ||
11cf8741 JM |
116 | static int |
117 | captured_main (void *data) | |
118 | { | |
119 | struct captured_main_args *context = data; | |
120 | int argc = context->argc; | |
121 | char **argv = context->argv; | |
c906108c SS |
122 | int count; |
123 | static int quiet = 0; | |
124 | static int batch = 0; | |
552c04a7 | 125 | static int set_args = 0; |
c906108c SS |
126 | |
127 | /* Pointers to various arguments from command line. */ | |
128 | char *symarg = NULL; | |
129 | char *execarg = NULL; | |
130 | char *corearg = NULL; | |
131 | char *cdarg = NULL; | |
132 | char *ttyarg = NULL; | |
133 | ||
134 | /* These are static so that we can take their address in an initializer. */ | |
135 | static int print_help; | |
136 | static int print_version; | |
137 | ||
138 | /* Pointers to all arguments of --command option. */ | |
139 | char **cmdarg; | |
140 | /* Allocated size of cmdarg. */ | |
141 | int cmdsize; | |
142 | /* Number of elements of cmdarg used. */ | |
143 | int ncmd; | |
144 | ||
145 | /* Indices of all arguments of --directory option. */ | |
146 | char **dirarg; | |
147 | /* Allocated size. */ | |
148 | int dirsize; | |
149 | /* Number of elements used. */ | |
150 | int ndir; | |
c5aa993b | 151 | |
c906108c SS |
152 | struct stat homebuf, cwdbuf; |
153 | char *homedir, *homeinit; | |
154 | ||
155 | register int i; | |
156 | ||
157 | long time_at_startup = get_run_time (); | |
158 | ||
0fbb3da7 TT |
159 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) |
160 | setlocale (LC_MESSAGES, ""); | |
161 | #endif | |
162 | #if defined (HAVE_SETLOCALE) | |
163 | setlocale (LC_CTYPE, ""); | |
164 | #endif | |
165 | bindtextdomain (PACKAGE, LOCALEDIR); | |
166 | textdomain (PACKAGE); | |
167 | ||
c906108c SS |
168 | START_PROGRESS (argv[0], 0); |
169 | ||
c906108c | 170 | /* This needs to happen before the first use of malloc. */ |
762f08a3 | 171 | init_malloc (NULL); |
c906108c SS |
172 | |
173 | #if defined (ALIGN_STACK_ON_STARTUP) | |
174 | i = (int) &count & 0x3; | |
175 | if (i != 0) | |
176 | alloca (4 - i); | |
177 | #endif | |
178 | ||
c906108c SS |
179 | cmdsize = 1; |
180 | cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg)); | |
181 | ncmd = 0; | |
182 | dirsize = 1; | |
183 | dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg)); | |
184 | ndir = 0; | |
185 | ||
186 | quit_flag = 0; | |
187 | line = (char *) xmalloc (linesize); | |
188 | line[0] = '\0'; /* Terminate saved (now empty) cmd line */ | |
189 | instream = stdin; | |
190 | ||
191 | getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)); | |
192 | current_directory = gdb_dirbuf; | |
193 | ||
da59e081 JM |
194 | gdb_stdout = stdio_fileopen (stdout); |
195 | gdb_stderr = stdio_fileopen (stderr); | |
196 | gdb_stdlog = gdb_stderr; /* for moment */ | |
197 | gdb_stdtarg = gdb_stderr; /* for moment */ | |
c906108c | 198 | |
2acceee2 JM |
199 | /* initialize error() */ |
200 | error_init (); | |
201 | ||
030292b7 DJ |
202 | /* Set the sysroot path. */ |
203 | #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE | |
204 | gdb_sysroot = make_relative_prefix (argv[0], BINDIR, TARGET_SYSTEM_ROOT); | |
205 | if (gdb_sysroot) | |
206 | { | |
207 | struct stat s; | |
208 | int res = 0; | |
209 | ||
210 | if (stat (gdb_sysroot, &s) == 0) | |
211 | if (S_ISDIR (s.st_mode)) | |
212 | res = 1; | |
213 | ||
214 | if (res == 0) | |
215 | { | |
a84d24ee | 216 | xfree (gdb_sysroot); |
030292b7 DJ |
217 | gdb_sysroot = TARGET_SYSTEM_ROOT; |
218 | } | |
219 | } | |
220 | else | |
221 | gdb_sysroot = TARGET_SYSTEM_ROOT; | |
222 | #else | |
223 | #if defined (TARGET_SYSTEM_ROOT) | |
224 | gdb_sysroot = TARGET_SYSTEM_ROOT; | |
225 | #else | |
226 | gdb_sysroot = ""; | |
227 | #endif | |
228 | #endif | |
229 | ||
c906108c SS |
230 | /* Parse arguments and options. */ |
231 | { | |
232 | int c; | |
233 | /* When var field is 0, use flag field to record the equivalent | |
234 | short option (or arbitrary numbers starting at 10 for those | |
235 | with no equivalent). */ | |
236 | static struct option long_options[] = | |
c5aa993b | 237 | { |
6426a772 JM |
238 | {"async", no_argument, &event_loop_p, 1}, |
239 | {"noasync", no_argument, &event_loop_p, 0}, | |
c906108c | 240 | #if defined(TUI) |
c5aa993b | 241 | {"tui", no_argument, &tui_version, 1}, |
c906108c | 242 | #endif |
c5aa993b JM |
243 | {"xdb", no_argument, &xdb_commands, 1}, |
244 | {"dbx", no_argument, &dbx_commands, 1}, | |
245 | {"readnow", no_argument, &readnow_symbol_files, 1}, | |
246 | {"r", no_argument, &readnow_symbol_files, 1}, | |
247 | {"mapped", no_argument, &mapped_symbol_files, 1}, | |
248 | {"m", no_argument, &mapped_symbol_files, 1}, | |
249 | {"quiet", no_argument, &quiet, 1}, | |
250 | {"q", no_argument, &quiet, 1}, | |
251 | {"silent", no_argument, &quiet, 1}, | |
252 | {"nx", no_argument, &inhibit_gdbinit, 1}, | |
253 | {"n", no_argument, &inhibit_gdbinit, 1}, | |
254 | {"batch", no_argument, &batch, 1}, | |
255 | {"epoch", no_argument, &epoch_interface, 1}, | |
256 | ||
257 | /* This is a synonym for "--annotate=1". --annotate is now preferred, | |
258 | but keep this here for a long time because people will be running | |
259 | emacses which use --fullname. */ | |
260 | {"fullname", no_argument, 0, 'f'}, | |
261 | {"f", no_argument, 0, 'f'}, | |
262 | ||
263 | {"annotate", required_argument, 0, 12}, | |
264 | {"help", no_argument, &print_help, 1}, | |
265 | {"se", required_argument, 0, 10}, | |
266 | {"symbols", required_argument, 0, 's'}, | |
267 | {"s", required_argument, 0, 's'}, | |
268 | {"exec", required_argument, 0, 'e'}, | |
269 | {"e", required_argument, 0, 'e'}, | |
270 | {"core", required_argument, 0, 'c'}, | |
271 | {"c", required_argument, 0, 'c'}, | |
00546b04 MS |
272 | {"pid", required_argument, 0, 'p'}, |
273 | {"p", required_argument, 0, 'p'}, | |
c5aa993b JM |
274 | {"command", required_argument, 0, 'x'}, |
275 | {"version", no_argument, &print_version, 1}, | |
276 | {"x", required_argument, 0, 'x'}, | |
3fc11d3e JM |
277 | #ifdef GDBTK |
278 | {"tclcommand", required_argument, 0, 'z'}, | |
279 | {"enable-external-editor", no_argument, 0, 'y'}, | |
280 | {"editor-command", required_argument, 0, 'w'}, | |
281 | #endif | |
8b93c638 JM |
282 | {"ui", required_argument, 0, 'i'}, |
283 | {"interpreter", required_argument, 0, 'i'}, | |
284 | {"i", required_argument, 0, 'i'}, | |
c5aa993b | 285 | {"directory", required_argument, 0, 'd'}, |
c4093a6a | 286 | {"d", required_argument, 0, 'd'}, |
c5aa993b JM |
287 | {"cd", required_argument, 0, 11}, |
288 | {"tty", required_argument, 0, 't'}, | |
289 | {"baud", required_argument, 0, 'b'}, | |
290 | {"b", required_argument, 0, 'b'}, | |
291 | {"nw", no_argument, &use_windows, 0}, | |
292 | {"nowindows", no_argument, &use_windows, 0}, | |
293 | {"w", no_argument, &use_windows, 1}, | |
294 | {"windows", no_argument, &use_windows, 1}, | |
295 | {"statistics", no_argument, 0, 13}, | |
296 | {"write", no_argument, &write_files, 1}, | |
552c04a7 | 297 | {"args", no_argument, &set_args, 1}, |
c5aa993b JM |
298 | {0, no_argument, 0, 0} |
299 | }; | |
c906108c SS |
300 | |
301 | while (1) | |
302 | { | |
303 | int option_index; | |
304 | ||
305 | c = getopt_long_only (argc, argv, "", | |
306 | long_options, &option_index); | |
552c04a7 | 307 | if (c == EOF || set_args) |
c906108c SS |
308 | break; |
309 | ||
310 | /* Long option that takes an argument. */ | |
311 | if (c == 0 && long_options[option_index].flag == 0) | |
312 | c = long_options[option_index].val; | |
313 | ||
314 | switch (c) | |
315 | { | |
316 | case 0: | |
317 | /* Long option that just sets a flag. */ | |
318 | break; | |
319 | case 10: | |
320 | symarg = optarg; | |
321 | execarg = optarg; | |
322 | break; | |
323 | case 11: | |
324 | cdarg = optarg; | |
325 | break; | |
326 | case 12: | |
327 | /* FIXME: what if the syntax is wrong (e.g. not digits)? */ | |
328 | annotation_level = atoi (optarg); | |
329 | break; | |
330 | case 13: | |
331 | /* Enable the display of both time and space usage. */ | |
332 | display_time = 1; | |
333 | display_space = 1; | |
334 | break; | |
335 | case 'f': | |
336 | annotation_level = 1; | |
337 | /* We have probably been invoked from emacs. Disable window interface. */ | |
338 | use_windows = 0; | |
339 | break; | |
340 | case 's': | |
341 | symarg = optarg; | |
342 | break; | |
343 | case 'e': | |
344 | execarg = optarg; | |
345 | break; | |
346 | case 'c': | |
347 | corearg = optarg; | |
348 | break; | |
00546b04 MS |
349 | case 'p': |
350 | /* "corearg" is shared by "--core" and "--pid" */ | |
351 | corearg = optarg; | |
352 | break; | |
c906108c SS |
353 | case 'x': |
354 | cmdarg[ncmd++] = optarg; | |
355 | if (ncmd >= cmdsize) | |
356 | { | |
357 | cmdsize *= 2; | |
c5aa993b | 358 | cmdarg = (char **) xrealloc ((char *) cmdarg, |
c906108c SS |
359 | cmdsize * sizeof (*cmdarg)); |
360 | } | |
361 | break; | |
3fc11d3e JM |
362 | #ifdef GDBTK |
363 | case 'z': | |
364 | { | |
a14ed312 | 365 | extern int gdbtk_test (char *); |
3fc11d3e JM |
366 | if (!gdbtk_test (optarg)) |
367 | { | |
defc6f8c | 368 | fprintf_unfiltered (gdb_stderr, _("%s: unable to load tclcommand file \"%s\""), |
3fc11d3e JM |
369 | argv[0], optarg); |
370 | exit (1); | |
371 | } | |
372 | break; | |
373 | } | |
374 | case 'y': | |
78f49586 TT |
375 | /* Backwards compatibility only. */ |
376 | break; | |
3fc11d3e JM |
377 | case 'w': |
378 | { | |
3fc11d3e JM |
379 | external_editor_command = xstrdup (optarg); |
380 | break; | |
381 | } | |
382 | #endif /* GDBTK */ | |
fb40c209 AC |
383 | case 'i': |
384 | interpreter_p = optarg; | |
385 | break; | |
c906108c SS |
386 | case 'd': |
387 | dirarg[ndir++] = optarg; | |
388 | if (ndir >= dirsize) | |
389 | { | |
390 | dirsize *= 2; | |
c5aa993b | 391 | dirarg = (char **) xrealloc ((char *) dirarg, |
c906108c SS |
392 | dirsize * sizeof (*dirarg)); |
393 | } | |
394 | break; | |
395 | case 't': | |
396 | ttyarg = optarg; | |
397 | break; | |
398 | case 'q': | |
399 | quiet = 1; | |
400 | break; | |
401 | case 'b': | |
402 | { | |
403 | int i; | |
404 | char *p; | |
405 | ||
406 | i = strtol (optarg, &p, 0); | |
407 | if (i == 0 && p == optarg) | |
408 | ||
409 | /* Don't use *_filtered or warning() (which relies on | |
c5aa993b | 410 | current_target) until after initialize_all_files(). */ |
c906108c SS |
411 | |
412 | fprintf_unfiltered | |
413 | (gdb_stderr, | |
defc6f8c | 414 | _("warning: could not set baud rate to `%s'.\n"), optarg); |
c906108c SS |
415 | else |
416 | baud_rate = i; | |
417 | } | |
046ca86a | 418 | break; |
c906108c SS |
419 | case 'l': |
420 | { | |
421 | int i; | |
422 | char *p; | |
423 | ||
424 | i = strtol (optarg, &p, 0); | |
425 | if (i == 0 && p == optarg) | |
426 | ||
427 | /* Don't use *_filtered or warning() (which relies on | |
c5aa993b | 428 | current_target) until after initialize_all_files(). */ |
c906108c SS |
429 | |
430 | fprintf_unfiltered | |
431 | (gdb_stderr, | |
defc6f8c | 432 | _("warning: could not set timeout limit to `%s'.\n"), optarg); |
c906108c SS |
433 | else |
434 | remote_timeout = i; | |
435 | } | |
436 | break; | |
437 | ||
c906108c SS |
438 | case '?': |
439 | fprintf_unfiltered (gdb_stderr, | |
defc6f8c | 440 | _("Use `%s --help' for a complete list of options.\n"), |
c5aa993b | 441 | argv[0]); |
c906108c SS |
442 | exit (1); |
443 | } | |
444 | } | |
445 | ||
446 | /* If --help or --version, disable window interface. */ | |
447 | if (print_help || print_version) | |
448 | { | |
449 | use_windows = 0; | |
450 | #ifdef TUI | |
451 | /* Disable the TUI as well. */ | |
452 | tui_version = 0; | |
453 | #endif | |
454 | } | |
455 | ||
456 | #ifdef TUI | |
457 | /* An explicit --tui flag overrides the default UI, which is the | |
458 | window system. */ | |
459 | if (tui_version) | |
460 | use_windows = 0; | |
c5aa993b | 461 | #endif |
c906108c | 462 | |
552c04a7 TT |
463 | if (set_args) |
464 | { | |
465 | /* The remaining options are the command-line options for the | |
466 | inferior. The first one is the sym/exec file, and the rest | |
467 | are arguments. */ | |
468 | if (optind >= argc) | |
469 | { | |
470 | fprintf_unfiltered (gdb_stderr, | |
defc6f8c | 471 | _("%s: `--args' specified but no program specified\n"), |
552c04a7 TT |
472 | argv[0]); |
473 | exit (1); | |
474 | } | |
475 | symarg = argv[optind]; | |
476 | execarg = argv[optind]; | |
477 | ++optind; | |
478 | set_inferior_args_vector (argc - optind, &argv[optind]); | |
479 | } | |
480 | else | |
481 | { | |
482 | /* OK, that's all the options. The other arguments are filenames. */ | |
483 | count = 0; | |
484 | for (; optind < argc; optind++) | |
485 | switch (++count) | |
486 | { | |
487 | case 1: | |
488 | symarg = argv[optind]; | |
489 | execarg = argv[optind]; | |
490 | break; | |
491 | case 2: | |
00546b04 MS |
492 | /* The documentation says this can be a "ProcID" as well. |
493 | We will try it as both a corefile and a pid. */ | |
552c04a7 TT |
494 | corearg = argv[optind]; |
495 | break; | |
496 | case 3: | |
497 | fprintf_unfiltered (gdb_stderr, | |
defc6f8c | 498 | _("Excess command line arguments ignored. (%s%s)\n"), |
552c04a7 TT |
499 | argv[optind], (optind == argc - 1) ? "" : " ..."); |
500 | break; | |
501 | } | |
502 | } | |
c906108c SS |
503 | if (batch) |
504 | quiet = 1; | |
505 | } | |
506 | ||
0f71a2f6 JM |
507 | /* Initialize all files. Give the interpreter a chance to take |
508 | control of the console via the init_ui_hook()) */ | |
c906108c SS |
509 | gdb_init (argv[0]); |
510 | ||
511 | /* Do these (and anything which might call wrap_here or *_filtered) | |
512 | after initialize_all_files. */ | |
513 | if (print_version) | |
514 | { | |
515 | print_gdb_version (gdb_stdout); | |
516 | wrap_here (""); | |
517 | printf_filtered ("\n"); | |
518 | exit (0); | |
519 | } | |
520 | ||
521 | if (print_help) | |
522 | { | |
523 | print_gdb_help (gdb_stdout); | |
524 | fputs_unfiltered ("\n", gdb_stdout); | |
525 | exit (0); | |
526 | } | |
527 | ||
528 | if (!quiet) | |
529 | { | |
530 | /* Print all the junk at the top, with trailing "..." if we are about | |
c5aa993b | 531 | to read a symbol file (possibly slowly). */ |
c906108c SS |
532 | print_gdb_version (gdb_stdout); |
533 | if (symarg) | |
534 | printf_filtered (".."); | |
c5aa993b JM |
535 | wrap_here (""); |
536 | gdb_flush (gdb_stdout); /* Force to screen during slow operations */ | |
c906108c SS |
537 | } |
538 | ||
539 | error_pre_print = "\n\n"; | |
540 | quit_pre_print = error_pre_print; | |
541 | ||
542 | /* We may get more than one warning, don't double space all of them... */ | |
defc6f8c | 543 | warning_pre_print = _("\nwarning: "); |
c906108c SS |
544 | |
545 | /* Read and execute $HOME/.gdbinit file, if it exists. This is done | |
546 | *before* all the command line arguments are processed; it sets | |
547 | global parameters, which are independent of what file you are | |
548 | debugging or what directory you are in. */ | |
c5aa993b | 549 | homedir = getenv ("HOME"); |
c906108c SS |
550 | if (homedir) |
551 | { | |
552 | homeinit = (char *) alloca (strlen (homedir) + | |
553 | strlen (gdbinit) + 10); | |
554 | strcpy (homeinit, homedir); | |
555 | strcat (homeinit, "/"); | |
556 | strcat (homeinit, gdbinit); | |
557 | ||
558 | if (!inhibit_gdbinit) | |
559 | { | |
11cf8741 | 560 | catch_command_errors (source_command, homeinit, 0, RETURN_MASK_ALL); |
c906108c | 561 | } |
c906108c SS |
562 | |
563 | /* Do stats; no need to do them elsewhere since we'll only | |
c5aa993b JM |
564 | need them if homedir is set. Make sure that they are |
565 | zero in case one of them fails (this guarantees that they | |
566 | won't match if either exists). */ | |
567 | ||
c906108c SS |
568 | memset (&homebuf, 0, sizeof (struct stat)); |
569 | memset (&cwdbuf, 0, sizeof (struct stat)); | |
c5aa993b | 570 | |
c906108c | 571 | stat (homeinit, &homebuf); |
c5aa993b JM |
572 | stat (gdbinit, &cwdbuf); /* We'll only need this if |
573 | homedir was set. */ | |
c906108c SS |
574 | } |
575 | ||
576 | /* Now perform all the actions indicated by the arguments. */ | |
577 | if (cdarg != NULL) | |
578 | { | |
11cf8741 | 579 | catch_command_errors (cd_command, cdarg, 0, RETURN_MASK_ALL); |
c906108c | 580 | } |
c906108c SS |
581 | |
582 | for (i = 0; i < ndir; i++) | |
11cf8741 | 583 | catch_command_errors (directory_command, dirarg[i], 0, RETURN_MASK_ALL); |
b8c9b27d | 584 | xfree (dirarg); |
c906108c SS |
585 | |
586 | if (execarg != NULL | |
587 | && symarg != NULL | |
588 | && STREQ (execarg, symarg)) | |
589 | { | |
11cf8741 JM |
590 | /* The exec file and the symbol-file are the same. If we can't |
591 | open it, better only print one error message. | |
592 | catch_command_errors returns non-zero on success! */ | |
1adeb98a FN |
593 | if (catch_command_errors (exec_file_attach, execarg, !batch, RETURN_MASK_ALL)) |
594 | catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL); | |
c906108c SS |
595 | } |
596 | else | |
597 | { | |
598 | if (execarg != NULL) | |
1adeb98a | 599 | catch_command_errors (exec_file_attach, execarg, !batch, RETURN_MASK_ALL); |
c906108c | 600 | if (symarg != NULL) |
1adeb98a | 601 | catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL); |
c906108c | 602 | } |
c906108c SS |
603 | |
604 | /* After the symbol file has been read, print a newline to get us | |
605 | beyond the copyright line... But errors should still set off | |
606 | the error message with a (single) blank line. */ | |
607 | if (!quiet) | |
608 | printf_filtered ("\n"); | |
609 | error_pre_print = "\n"; | |
610 | quit_pre_print = error_pre_print; | |
defc6f8c | 611 | warning_pre_print = _("\nwarning: "); |
c906108c SS |
612 | |
613 | if (corearg != NULL) | |
614 | { | |
00546b04 MS |
615 | /* corearg may be either a corefile or a pid. |
616 | If its first character is a digit, try attach first | |
617 | and then corefile. Otherwise try corefile first. */ | |
618 | ||
619 | if (isdigit (corearg[0])) | |
11cf8741 | 620 | { |
00546b04 MS |
621 | if (catch_command_errors (attach_command, corearg, |
622 | !batch, RETURN_MASK_ALL) == 0) | |
623 | catch_command_errors (core_file_command, corearg, | |
624 | !batch, RETURN_MASK_ALL); | |
11cf8741 | 625 | } |
00546b04 MS |
626 | else /* Can't be a pid, better be a corefile. */ |
627 | catch_command_errors (core_file_command, corearg, | |
628 | !batch, RETURN_MASK_ALL); | |
c906108c | 629 | } |
c906108c SS |
630 | |
631 | if (ttyarg != NULL) | |
11cf8741 | 632 | catch_command_errors (tty_command, ttyarg, !batch, RETURN_MASK_ALL); |
c906108c | 633 | |
c906108c SS |
634 | /* Error messages should no longer be distinguished with extra output. */ |
635 | error_pre_print = NULL; | |
636 | quit_pre_print = NULL; | |
defc6f8c | 637 | warning_pre_print = _("warning: "); |
c906108c SS |
638 | |
639 | /* Read the .gdbinit file in the current directory, *if* it isn't | |
640 | the same as the $HOME/.gdbinit file (it should exist, also). */ | |
c5aa993b | 641 | |
c906108c SS |
642 | if (!homedir |
643 | || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat))) | |
644 | if (!inhibit_gdbinit) | |
645 | { | |
11cf8741 | 646 | catch_command_errors (source_command, gdbinit, 0, RETURN_MASK_ALL); |
c906108c | 647 | } |
c906108c SS |
648 | |
649 | for (i = 0; i < ncmd; i++) | |
650 | { | |
11cf8741 JM |
651 | #if 0 |
652 | /* NOTE: cagney/1999-11-03: SET_TOP_LEVEL() was a macro that | |
653 | expanded into a call to setjmp(). */ | |
654 | if (!SET_TOP_LEVEL ()) /* NB: This is #if 0'd out */ | |
c906108c | 655 | { |
b83266a0 SS |
656 | /* NOTE: I am commenting this out, because it is not clear |
657 | where this feature is used. It is very old and | |
c5aa993b | 658 | undocumented. ezannoni: 1999-05-04 */ |
b83266a0 | 659 | #if 0 |
c906108c SS |
660 | if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0') |
661 | read_command_file (stdin); | |
662 | else | |
b83266a0 | 663 | #endif |
c906108c SS |
664 | source_command (cmdarg[i], !batch); |
665 | do_cleanups (ALL_CLEANUPS); | |
666 | } | |
11cf8741 JM |
667 | #endif |
668 | catch_command_errors (source_command, cmdarg[i], !batch, RETURN_MASK_ALL); | |
c906108c | 669 | } |
b8c9b27d | 670 | xfree (cmdarg); |
c906108c SS |
671 | |
672 | /* Read in the old history after all the command files have been read. */ | |
c5aa993b | 673 | init_history (); |
c906108c SS |
674 | |
675 | if (batch) | |
676 | { | |
677 | /* We have hit the end of the batch file. */ | |
678 | exit (0); | |
679 | } | |
680 | ||
681 | /* Do any host- or target-specific hacks. This is used for i960 targets | |
682 | to force the user to set a nindy target and spec its parameters. */ | |
683 | ||
684 | #ifdef BEFORE_MAIN_LOOP_HOOK | |
685 | BEFORE_MAIN_LOOP_HOOK; | |
686 | #endif | |
687 | ||
688 | END_PROGRESS (argv[0]); | |
689 | ||
690 | /* Show time and/or space usage. */ | |
691 | ||
692 | if (display_time) | |
693 | { | |
694 | long init_time = get_run_time () - time_at_startup; | |
695 | ||
defc6f8c | 696 | printf_unfiltered (_("Startup time: %ld.%06ld\n"), |
c906108c SS |
697 | init_time / 1000000, init_time % 1000000); |
698 | } | |
699 | ||
700 | if (display_space) | |
701 | { | |
702 | #ifdef HAVE_SBRK | |
703 | extern char **environ; | |
704 | char *lim = (char *) sbrk (0); | |
705 | ||
defc6f8c | 706 | printf_unfiltered (_("Startup size: data size %ld\n"), |
c906108c SS |
707 | (long) (lim - (char *) &environ)); |
708 | #endif | |
709 | } | |
710 | ||
11cf8741 JM |
711 | #if 0 |
712 | /* FIXME: cagney/1999-11-06: The original main loop was like: */ | |
c906108c SS |
713 | while (1) |
714 | { | |
715 | if (!SET_TOP_LEVEL ()) | |
716 | { | |
c5aa993b | 717 | do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */ |
c906108c SS |
718 | /* GUIs generally have their own command loop, mainloop, or whatever. |
719 | This is a good place to gain control because many error | |
720 | conditions will end up here via longjmp(). */ | |
721 | if (command_loop_hook) | |
722 | command_loop_hook (); | |
723 | else | |
724 | command_loop (); | |
c5aa993b | 725 | quit_command ((char *) 0, instream == stdin); |
c906108c SS |
726 | } |
727 | } | |
11cf8741 JM |
728 | /* NOTE: If the command_loop() returned normally, the loop would |
729 | attempt to exit by calling the function quit_command(). That | |
730 | function would either call exit() or throw an error returning | |
731 | control to SET_TOP_LEVEL. */ | |
732 | /* NOTE: The function do_cleanups() was called once each time round | |
733 | the loop. The usefulness of the call isn't clear. If an error | |
734 | was thrown, everything would have already been cleaned up. If | |
735 | command_loop() returned normally and quit_command() was called, | |
736 | either exit() or error() (again cleaning up) would be called. */ | |
737 | #endif | |
738 | /* NOTE: cagney/1999-11-07: There is probably no reason for not | |
739 | moving this loop and the code found in captured_command_loop() | |
740 | into the command_loop() proper. The main thing holding back that | |
741 | change - SET_TOP_LEVEL() - has been eliminated. */ | |
742 | while (1) | |
743 | { | |
744 | catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL); | |
745 | } | |
11cf8741 JM |
746 | /* No exit -- exit is through quit_command. */ |
747 | } | |
c906108c | 748 | |
11cf8741 | 749 | int |
f15ab4a7 | 750 | gdb_main (struct captured_main_args *args) |
11cf8741 | 751 | { |
f15ab4a7 AC |
752 | use_windows = args->use_windows; |
753 | catch_errors (captured_main, args, "", RETURN_MASK_ALL); | |
11cf8741 | 754 | return 0; |
c906108c SS |
755 | } |
756 | ||
11cf8741 | 757 | |
c906108c SS |
758 | /* Don't use *_filtered for printing help. We don't want to prompt |
759 | for continue no matter how small the screen or how much we're going | |
760 | to print. */ | |
761 | ||
762 | static void | |
d9fcf2fb | 763 | print_gdb_help (struct ui_file *stream) |
c906108c | 764 | { |
defc6f8c | 765 | fputs_unfiltered (_("\ |
c906108c | 766 | This is the GNU debugger. Usage:\n\n\ |
552c04a7 TT |
767 | gdb [options] [executable-file [core-file or process-id]]\n\ |
768 | gdb [options] --args executable-file [inferior-arguments ...]\n\n\ | |
c906108c | 769 | Options:\n\n\ |
defc6f8c TT |
770 | "), stream); |
771 | fputs_unfiltered (_("\ | |
552c04a7 | 772 | --args Arguments after executable-file are passed to inferior\n\ |
defc6f8c TT |
773 | "), stream); |
774 | fputs_unfiltered (_("\ | |
0f71a2f6 | 775 | --[no]async Enable (disable) asynchronous version of CLI\n\ |
defc6f8c TT |
776 | "), stream); |
777 | fputs_unfiltered (_("\ | |
c906108c SS |
778 | -b BAUDRATE Set serial port baud rate used for remote debugging.\n\ |
779 | --batch Exit after processing options.\n\ | |
780 | --cd=DIR Change current directory to DIR.\n\ | |
781 | --command=FILE Execute GDB commands from FILE.\n\ | |
782 | --core=COREFILE Analyze the core dump COREFILE.\n\ | |
00546b04 | 783 | --pid=PID Attach to running process PID.\n\ |
defc6f8c TT |
784 | "), stream); |
785 | fputs_unfiltered (_("\ | |
c906108c SS |
786 | --dbx DBX compatibility mode.\n\ |
787 | --directory=DIR Search for source files in DIR.\n\ | |
788 | --epoch Output information used by epoch emacs-GDB interface.\n\ | |
789 | --exec=EXECFILE Use EXECFILE as the executable.\n\ | |
790 | --fullname Output information used by emacs-GDB interface.\n\ | |
791 | --help Print this message.\n\ | |
defc6f8c TT |
792 | "), stream); |
793 | fputs_unfiltered (_("\ | |
8b93c638 JM |
794 | --interpreter=INTERP\n\ |
795 | Select a specific interpreter / user interface\n\ | |
defc6f8c TT |
796 | "), stream); |
797 | fputs_unfiltered (_("\ | |
c906108c SS |
798 | --mapped Use mapped symbol files if supported on this system.\n\ |
799 | --nw Do not use a window interface.\n\ | |
defc6f8c | 800 | --nx Do not read "), stream); |
96baa820 | 801 | fputs_unfiltered (gdbinit, stream); |
defc6f8c | 802 | fputs_unfiltered (_(" file.\n\ |
c906108c SS |
803 | --quiet Do not print version number on startup.\n\ |
804 | --readnow Fully read symbol files on first access.\n\ | |
defc6f8c TT |
805 | "), stream); |
806 | fputs_unfiltered (_("\ | |
c906108c SS |
807 | --se=FILE Use FILE as symbol file and executable file.\n\ |
808 | --symbols=SYMFILE Read symbols from SYMFILE.\n\ | |
809 | --tty=TTY Use TTY for input/output by the program being debugged.\n\ | |
defc6f8c | 810 | "), stream); |
c906108c | 811 | #if defined(TUI) |
defc6f8c | 812 | fputs_unfiltered (_("\ |
c906108c | 813 | --tui Use a terminal user interface.\n\ |
defc6f8c | 814 | "), stream); |
c906108c | 815 | #endif |
defc6f8c | 816 | fputs_unfiltered (_("\ |
c906108c SS |
817 | --version Print version information and then exit.\n\ |
818 | -w Use a window interface.\n\ | |
819 | --write Set writing into executable and core files.\n\ | |
820 | --xdb XDB compatibility mode.\n\ | |
defc6f8c | 821 | "), stream); |
defc6f8c | 822 | fputs_unfiltered (_("\n\ |
c906108c SS |
823 | For more information, type \"help\" from within GDB, or consult the\n\ |
824 | GDB manual (available as on-line info or a printed manual).\n\ | |
2df3850c | 825 | Report bugs to \"bug-gdb@gnu.org\".\ |
defc6f8c | 826 | "), stream); |
c906108c | 827 | } |