* inftarg.c (child_thread_alive): New function to see if a
[deliverable/binutils-gdb.git] / gdb / main.c
1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include <setjmp.h>
23 #include "top.h"
24 #include "target.h"
25 #include "inferior.h"
26 #include "call-cmds.h"
27
28 #include "getopt.h"
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <ctype.h>
33
34 #include <string.h>
35 /* R_OK lives in either unistd.h or sys/file.h. */
36 #ifdef USG
37 #include <unistd.h>
38 #endif
39 #ifndef NO_SYS_FILE
40 #include <sys/file.h>
41 #endif
42
43 /* Temporary variable for SET_TOP_LEVEL. */
44
45 static int top_level_val;
46
47 /* Do a setjmp on error_return and quit_return. catch_errors is
48 generally a cleaner way to do this, but main() would look pretty
49 ugly if it had to use catch_errors each time. */
50
51 #define SET_TOP_LEVEL() \
52 (((top_level_val = setjmp (error_return)) \
53 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \
54 , top_level_val)
55
56 /* If nonzero, display time usage both at startup and for each command. */
57
58 int display_time;
59
60 /* If nonzero, display space usage both at startup and for each command. */
61
62 int display_space;
63
64 extern void gdb_init PARAMS ((void));
65
66 int
67 main (argc, argv)
68 int argc;
69 char **argv;
70 {
71 int count;
72 static int quiet = 0;
73 static int batch = 0;
74
75 /* Pointers to various arguments from command line. */
76 char *symarg = NULL;
77 char *execarg = NULL;
78 char *corearg = NULL;
79 char *cdarg = NULL;
80 char *ttyarg = NULL;
81
82 /* These are static so that we can take their address in an initializer. */
83 static int print_help;
84 static int print_version;
85
86 /* Pointers to all arguments of --command option. */
87 char **cmdarg;
88 /* Allocated size of cmdarg. */
89 int cmdsize;
90 /* Number of elements of cmdarg used. */
91 int ncmd;
92
93 /* Indices of all arguments of --directory option. */
94 char **dirarg;
95 /* Allocated size. */
96 int dirsize;
97 /* Number of elements used. */
98 int ndir;
99
100 struct stat homebuf, cwdbuf;
101 char *homedir, *homeinit;
102
103 register int i;
104
105 long time_at_startup = get_run_time ();
106
107 /* start-sanitize-mpw */
108 #ifdef MPW
109 /* Drop into MacsBug, but only if the executable is specially named. */
110 if (strcmp(argv[0], "DEBUGGDB") == 0)
111 DebugStr("\pat start of GDB main");
112 /* Do all Mac-specific setup. */
113 mac_init ();
114 #endif /* MPW */
115 /* end-sanitize-mpw */
116 /* This needs to happen before the first use of malloc. */
117 init_malloc ((PTR) NULL);
118
119 #if defined (ALIGN_STACK_ON_STARTUP)
120 i = (int) &count & 0x3;
121 if (i != 0)
122 alloca (4 - i);
123 #endif
124
125 /* If error() is called from initialization code, just exit */
126 if (SET_TOP_LEVEL ()) {
127 exit(1);
128 }
129
130 cmdsize = 1;
131 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
132 ncmd = 0;
133 dirsize = 1;
134 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
135 ndir = 0;
136
137 quit_flag = 0;
138 line = (char *) xmalloc (linesize);
139 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
140 instream = stdin;
141
142 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
143 current_directory = gdb_dirbuf;
144
145 /* Parse arguments and options. */
146 {
147 int c;
148 /* When var field is 0, use flag field to record the equivalent
149 short option (or arbitrary numbers starting at 10 for those
150 with no equivalent). */
151 static struct option long_options[] =
152 {
153 {"readnow", no_argument, &readnow_symbol_files, 1},
154 {"r", no_argument, &readnow_symbol_files, 1},
155 {"mapped", no_argument, &mapped_symbol_files, 1},
156 {"m", no_argument, &mapped_symbol_files, 1},
157 {"quiet", no_argument, &quiet, 1},
158 {"q", no_argument, &quiet, 1},
159 {"silent", no_argument, &quiet, 1},
160 {"nx", no_argument, &inhibit_gdbinit, 1},
161 {"n", no_argument, &inhibit_gdbinit, 1},
162 {"batch", no_argument, &batch, 1},
163 {"epoch", no_argument, &epoch_interface, 1},
164
165 /* This is a synonym for "--annotate=1". --annotate is now preferred,
166 but keep this here for a long time because people will be running
167 emacses which use --fullname. */
168 {"fullname", no_argument, 0, 'f'},
169 {"f", no_argument, 0, 'f'},
170
171 {"annotate", required_argument, 0, 12},
172 {"help", no_argument, &print_help, 1},
173 {"se", required_argument, 0, 10},
174 {"symbols", required_argument, 0, 's'},
175 {"s", required_argument, 0, 's'},
176 {"exec", required_argument, 0, 'e'},
177 {"e", required_argument, 0, 'e'},
178 {"core", required_argument, 0, 'c'},
179 {"c", required_argument, 0, 'c'},
180 {"command", required_argument, 0, 'x'},
181 {"version", no_argument, &print_version, 1},
182 {"x", required_argument, 0, 'x'},
183 {"directory", required_argument, 0, 'd'},
184 {"cd", required_argument, 0, 11},
185 {"tty", required_argument, 0, 't'},
186 {"baud", required_argument, 0, 'b'},
187 {"b", required_argument, 0, 'b'},
188 {"nw", no_argument, &use_windows, 0},
189 {"nowindows", no_argument, &use_windows, 0},
190 {"w", no_argument, &use_windows, 1},
191 {"windows", no_argument, &use_windows, 1},
192 {"statistics", no_argument, 0, 13},
193 /* Allow machine descriptions to add more options... */
194 #ifdef ADDITIONAL_OPTIONS
195 ADDITIONAL_OPTIONS
196 #endif
197 {0, no_argument, 0, 0}
198 };
199
200 while (1)
201 {
202 int option_index;
203
204 c = getopt_long_only (argc, argv, "",
205 long_options, &option_index);
206 if (c == EOF)
207 break;
208
209 /* Long option that takes an argument. */
210 if (c == 0 && long_options[option_index].flag == 0)
211 c = long_options[option_index].val;
212
213 switch (c)
214 {
215 case 0:
216 /* Long option that just sets a flag. */
217 break;
218 case 10:
219 symarg = optarg;
220 execarg = optarg;
221 break;
222 case 11:
223 cdarg = optarg;
224 break;
225 case 12:
226 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
227 annotation_level = atoi (optarg);
228 break;
229 case 13:
230 /* Enable the display of both time and space usage. */
231 display_time = 1;
232 display_space = 1;
233 break;
234 case 'f':
235 annotation_level = 1;
236 break;
237 case 's':
238 symarg = optarg;
239 break;
240 case 'e':
241 execarg = optarg;
242 break;
243 case 'c':
244 corearg = optarg;
245 break;
246 case 'x':
247 cmdarg[ncmd++] = optarg;
248 if (ncmd >= cmdsize)
249 {
250 cmdsize *= 2;
251 cmdarg = (char **) xrealloc ((char *)cmdarg,
252 cmdsize * sizeof (*cmdarg));
253 }
254 break;
255 case 'd':
256 dirarg[ndir++] = optarg;
257 if (ndir >= dirsize)
258 {
259 dirsize *= 2;
260 dirarg = (char **) xrealloc ((char *)dirarg,
261 dirsize * sizeof (*dirarg));
262 }
263 break;
264 case 't':
265 ttyarg = optarg;
266 break;
267 case 'q':
268 quiet = 1;
269 break;
270 case 'b':
271 {
272 int i;
273 char *p;
274
275 i = strtol (optarg, &p, 0);
276 if (i == 0 && p == optarg)
277
278 /* Don't use *_filtered or warning() (which relies on
279 current_target) until after initialize_all_files(). */
280
281 fprintf_unfiltered
282 (gdb_stderr,
283 "warning: could not set baud rate to `%s'.\n", optarg);
284 else
285 baud_rate = i;
286 }
287 break;
288
289 #ifdef ADDITIONAL_OPTION_CASES
290 ADDITIONAL_OPTION_CASES
291 #endif
292 case '?':
293 fprintf_unfiltered (gdb_stderr,
294 "Use `%s --help' for a complete list of options.\n",
295 argv[0]);
296 exit (1);
297 }
298 }
299
300 /* OK, that's all the options. The other arguments are filenames. */
301 count = 0;
302 for (; optind < argc; optind++)
303 switch (++count)
304 {
305 case 1:
306 symarg = argv[optind];
307 execarg = argv[optind];
308 break;
309 case 2:
310 corearg = argv[optind];
311 break;
312 case 3:
313 fprintf_unfiltered (gdb_stderr,
314 "Excess command line arguments ignored. (%s%s)\n",
315 argv[optind], (optind == argc - 1) ? "" : " ...");
316 break;
317 }
318 if (batch)
319 quiet = 1;
320 }
321
322 gdb_init ();
323
324 /* Do these (and anything which might call wrap_here or *_filtered)
325 after initialize_all_files. */
326 if (print_version)
327 {
328 print_gdb_version (gdb_stdout);
329 wrap_here ("");
330 printf_filtered ("\n");
331 exit (0);
332 }
333
334 if (print_help)
335 {
336 /* --version is intentionally not documented here, because we
337 are printing the version here, and the help is long enough
338 already. */
339
340 print_gdb_version (gdb_stdout);
341 /* Make sure the output gets printed. */
342 wrap_here ("");
343 printf_filtered ("\n");
344
345 /* But don't use *_filtered here. We don't want to prompt for continue
346 no matter how small the screen or how much we're going to print. */
347 /* start-sanitize-mpw */
348 /* For reasons too ugly to describe... */
349 #ifdef MPW_C
350 fputs_unfiltered ("This is the GNU debugger.\n", gdb_stdout);
351 #else
352 /* end-sanitize-mpw */
353 fputs_unfiltered ("\
354 This is the GNU debugger. Usage:\n\
355 gdb [options] [executable-file [core-file or process-id]]\n\
356 Options:\n\
357 --help Print this message.\n\
358 --quiet Do not print version number on startup.\n\
359 --fullname Output information used by emacs-GDB interface.\n\
360 --epoch Output information used by epoch emacs-GDB interface.\n\
361 --batch Exit after processing options.\n\
362 --nx Do not read .gdbinit file.\n\
363 --tty=TTY Use TTY for input/output by the program being debugged.\n\
364 --cd=DIR Change current directory to DIR.\n\
365 --directory=DIR Search for source files in DIR.\n\
366 --command=FILE Execute GDB commands from FILE.\n\
367 --symbols=SYMFILE Read symbols from SYMFILE.\n\
368 --exec=EXECFILE Use EXECFILE as the executable.\n\
369 --se=FILE Use FILE as symbol file and executable file.\n\
370 --core=COREFILE Analyze the core dump COREFILE.\n\
371 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
372 --mapped Use mapped symbol files if supported on this system.\n\
373 --readnow Fully read symbol files on first access.\n\
374 --nw Do not use a window interface.\n\
375 ", gdb_stdout);
376 /* start-sanitize-mpw */
377 #endif /* MPW_C */
378 /* end-sanitize-mpw */
379 #ifdef ADDITIONAL_OPTION_HELP
380 fputs_unfiltered (ADDITIONAL_OPTION_HELP, gdb_stdout);
381 #endif
382 fputs_unfiltered ("\n\
383 For more information, type \"help\" from within GDB, or consult the\n\
384 GDB manual (available as on-line info or a printed manual).\n", gdb_stdout);
385 exit (0);
386 }
387
388 if (!quiet)
389 {
390 /* Print all the junk at the top, with trailing "..." if we are about
391 to read a symbol file (possibly slowly). */
392 print_gnu_advertisement ();
393 print_gdb_version (gdb_stdout);
394 if (symarg)
395 printf_filtered ("..");
396 wrap_here("");
397 gdb_flush (gdb_stdout); /* Force to screen during slow operations */
398 }
399
400 error_pre_print = "\n\n";
401 /* We may get more than one warning, don't double space all of them... */
402 warning_pre_print = "\nwarning: ";
403
404 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
405 *before* all the command line arguments are processed; it sets
406 global parameters, which are independent of what file you are
407 debugging or what directory you are in. */
408 homedir = getenv ("HOME");
409 if (homedir)
410 {
411 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
412 strlen (gdbinit) + 10);
413 strcpy (homeinit, getenv ("HOME"));
414 strcat (homeinit, "/");
415 strcat (homeinit, gdbinit);
416 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
417 {
418 if (!SET_TOP_LEVEL ())
419 source_command (homeinit, 0);
420 }
421 do_cleanups (ALL_CLEANUPS);
422
423 /* Do stats; no need to do them elsewhere since we'll only
424 need them if homedir is set. Make sure that they are
425 zero in case one of them fails (this guarantees that they
426 won't match if either exists). */
427
428 memset (&homebuf, 0, sizeof (struct stat));
429 memset (&cwdbuf, 0, sizeof (struct stat));
430
431 stat (homeinit, &homebuf);
432 stat (gdbinit, &cwdbuf); /* We'll only need this if
433 homedir was set. */
434 }
435
436 /* Now perform all the actions indicated by the arguments. */
437 if (cdarg != NULL)
438 {
439 if (!SET_TOP_LEVEL ())
440 {
441 cd_command (cdarg, 0);
442 }
443 }
444 do_cleanups (ALL_CLEANUPS);
445
446 for (i = 0; i < ndir; i++)
447 if (!SET_TOP_LEVEL ())
448 directory_command (dirarg[i], 0);
449 free ((PTR)dirarg);
450 do_cleanups (ALL_CLEANUPS);
451
452 if (execarg != NULL
453 && symarg != NULL
454 && STREQ (execarg, symarg))
455 {
456 /* The exec file and the symbol-file are the same. If we can't open
457 it, better only print one error message. */
458 if (!SET_TOP_LEVEL ())
459 {
460 exec_file_command (execarg, !batch);
461 symbol_file_command (symarg, 0);
462 }
463 }
464 else
465 {
466 if (execarg != NULL)
467 if (!SET_TOP_LEVEL ())
468 exec_file_command (execarg, !batch);
469 if (symarg != NULL)
470 if (!SET_TOP_LEVEL ())
471 symbol_file_command (symarg, 0);
472 }
473 do_cleanups (ALL_CLEANUPS);
474
475 /* After the symbol file has been read, print a newline to get us
476 beyond the copyright line... But errors should still set off
477 the error message with a (single) blank line. */
478 if (!quiet)
479 printf_filtered ("\n");
480 error_pre_print = "\n";
481 warning_pre_print = "\nwarning: ";
482
483 if (corearg != NULL)
484 if (!SET_TOP_LEVEL ())
485 core_file_command (corearg, !batch);
486 else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ())
487 attach_command (corearg, !batch);
488 do_cleanups (ALL_CLEANUPS);
489
490 if (ttyarg != NULL)
491 if (!SET_TOP_LEVEL ())
492 tty_command (ttyarg, !batch);
493 do_cleanups (ALL_CLEANUPS);
494
495 #ifdef ADDITIONAL_OPTION_HANDLER
496 ADDITIONAL_OPTION_HANDLER;
497 #endif
498
499 /* Error messages should no longer be distinguished with extra output. */
500 error_pre_print = 0;
501 warning_pre_print = "warning: ";
502
503 /* Read the .gdbinit file in the current directory, *if* it isn't
504 the same as the $HOME/.gdbinit file (it should exist, also). */
505
506 if (!homedir
507 || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
508 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
509 {
510 if (!SET_TOP_LEVEL ())
511 source_command (gdbinit, 0);
512 }
513 do_cleanups (ALL_CLEANUPS);
514
515 for (i = 0; i < ncmd; i++)
516 {
517 if (!SET_TOP_LEVEL ())
518 {
519 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
520 read_command_file (stdin);
521 else
522 source_command (cmdarg[i], !batch);
523 do_cleanups (ALL_CLEANUPS);
524 }
525 }
526 free ((PTR)cmdarg);
527
528 /* Read in the old history after all the command files have been read. */
529 init_history();
530
531 if (batch)
532 {
533 /* We have hit the end of the batch file. */
534 exit (0);
535 }
536
537 /* Do any host- or target-specific hacks. This is used for i960 targets
538 to force the user to set a nindy target and spec its parameters. */
539
540 #ifdef BEFORE_MAIN_LOOP_HOOK
541 BEFORE_MAIN_LOOP_HOOK;
542 #endif
543
544 /* Show time and/or space usage. */
545
546 if (display_time)
547 {
548 long init_time = get_run_time () - time_at_startup;
549
550 printf_unfiltered ("Startup time: %ld.%06ld\n",
551 init_time / 1000000, init_time % 1000000);
552 }
553
554 if (display_space)
555 {
556 extern char **environ;
557 char *lim = (char *) sbrk (0);
558
559 printf_unfiltered ("Startup size: data size %ld\n",
560 (long) (lim - (char *) &environ));
561 }
562
563 /* The command loop. */
564
565 while (1)
566 {
567 if (!SET_TOP_LEVEL ())
568 {
569 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
570 /* GUIs generally have their own command loop, mainloop, or whatever.
571 This is a good place to gain control because many error
572 conditions will end up here via longjmp(). */
573 if (command_loop_hook)
574 command_loop_hook ();
575 else
576 command_loop ();
577 quit_command ((char *)0, instream == stdin);
578 }
579 }
580 /* No exit -- exit is through quit_command. */
581 }
582 \f
583 void
584 init_proc ()
585 {
586 }
587
588 int
589 proc_wait (pid, status)
590 int pid;
591 int *status;
592 {
593 #ifndef __GO32__
594 return wait (status);
595 #endif
596 }
597
598 void
599 proc_remove_foreign (pid)
600 int pid;
601 {
602 }
603
604 void
605 fputs_unfiltered (linebuffer, stream)
606 const char *linebuffer;
607 FILE *stream;
608 {
609 if (fputs_unfiltered_hook)
610 {
611 /* FIXME: I think we should only be doing this for stdout or stderr.
612 Either that or we should be passing stream to the hook so it can
613 deal with it. If that is cleaned up, this function can go back
614 into utils.c and the fputs_unfiltered_hook can replace the current
615 ability to avoid this function by not linking with main.c. */
616 fputs_unfiltered_hook (linebuffer, stream);
617 return;
618 }
619
620 fputs (linebuffer, stream);
621 }
This page took 0.042144 seconds and 4 git commands to generate.