* configure.in: Check for working mmap, ansi headers, string.h,
[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, 1995
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 "gdb_stat.h"
32 #include <ctype.h>
33
34 #include "gdb_string.h"
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #ifndef NO_SYS_FILE
39 #include <sys/file.h>
40 #endif
41
42 /* Temporary variable for SET_TOP_LEVEL. */
43
44 static int top_level_val;
45
46 /* Do a setjmp on error_return and quit_return. catch_errors is
47 generally a cleaner way to do this, but main() would look pretty
48 ugly if it had to use catch_errors each time. */
49
50 #define SET_TOP_LEVEL() \
51 (((top_level_val = setjmp (error_return)) \
52 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \
53 , top_level_val)
54
55 /* If nonzero, display time usage both at startup and for each command. */
56
57 int display_time;
58
59 /* If nonzero, display space usage both at startup and for each command. */
60
61 int display_space;
62
63 extern void gdb_init PARAMS ((void));
64
65 int
66 main (argc, argv)
67 int argc;
68 char **argv;
69 {
70 int count;
71 static int quiet = 0;
72 static int batch = 0;
73
74 /* Pointers to various arguments from command line. */
75 char *symarg = NULL;
76 char *execarg = NULL;
77 char *corearg = NULL;
78 char *cdarg = NULL;
79 char *ttyarg = NULL;
80
81 /* These are static so that we can take their address in an initializer. */
82 static int print_help;
83 static int print_version;
84
85 /* Pointers to all arguments of --command option. */
86 char **cmdarg;
87 /* Allocated size of cmdarg. */
88 int cmdsize;
89 /* Number of elements of cmdarg used. */
90 int ncmd;
91
92 /* Indices of all arguments of --directory option. */
93 char **dirarg;
94 /* Allocated size. */
95 int dirsize;
96 /* Number of elements used. */
97 int ndir;
98
99 struct stat homebuf, cwdbuf;
100 char *homedir, *homeinit;
101
102 register int i;
103
104 long time_at_startup = get_run_time ();
105
106 START_PROGRESS (argv[0], 0);
107
108 #ifdef MPW
109 /* Do all Mac-specific setup. */
110 mac_init ();
111 #endif /* MPW */
112
113 /* This needs to happen before the first use of malloc. */
114 init_malloc ((PTR) NULL);
115
116 #if defined (ALIGN_STACK_ON_STARTUP)
117 i = (int) &count & 0x3;
118 if (i != 0)
119 alloca (4 - i);
120 #endif
121
122 /* If error() is called from initialization code, just exit */
123 if (SET_TOP_LEVEL ()) {
124 exit(1);
125 }
126
127 cmdsize = 1;
128 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
129 ncmd = 0;
130 dirsize = 1;
131 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
132 ndir = 0;
133
134 quit_flag = 0;
135 line = (char *) xmalloc (linesize);
136 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
137 instream = stdin;
138
139 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
140 current_directory = gdb_dirbuf;
141
142 /* Parse arguments and options. */
143 #ifndef WIN32
144 {
145 int c;
146 /* When var field is 0, use flag field to record the equivalent
147 short option (or arbitrary numbers starting at 10 for those
148 with no equivalent). */
149 static struct option long_options[] =
150 {
151 {"readnow", no_argument, &readnow_symbol_files, 1},
152 {"r", no_argument, &readnow_symbol_files, 1},
153 {"mapped", no_argument, &mapped_symbol_files, 1},
154 {"m", no_argument, &mapped_symbol_files, 1},
155 {"quiet", no_argument, &quiet, 1},
156 {"q", no_argument, &quiet, 1},
157 {"silent", no_argument, &quiet, 1},
158 {"nx", no_argument, &inhibit_gdbinit, 1},
159 {"n", no_argument, &inhibit_gdbinit, 1},
160 {"batch", no_argument, &batch, 1},
161 {"epoch", no_argument, &epoch_interface, 1},
162
163 /* This is a synonym for "--annotate=1". --annotate is now preferred,
164 but keep this here for a long time because people will be running
165 emacses which use --fullname. */
166 {"fullname", no_argument, 0, 'f'},
167 {"f", no_argument, 0, 'f'},
168
169 {"annotate", required_argument, 0, 12},
170 {"help", no_argument, &print_help, 1},
171 {"se", required_argument, 0, 10},
172 {"symbols", required_argument, 0, 's'},
173 {"s", required_argument, 0, 's'},
174 {"exec", required_argument, 0, 'e'},
175 {"e", required_argument, 0, 'e'},
176 {"core", required_argument, 0, 'c'},
177 {"c", required_argument, 0, 'c'},
178 {"command", required_argument, 0, 'x'},
179 {"version", no_argument, &print_version, 1},
180 {"x", required_argument, 0, 'x'},
181 {"directory", required_argument, 0, 'd'},
182 {"cd", required_argument, 0, 11},
183 {"tty", required_argument, 0, 't'},
184 {"baud", required_argument, 0, 'b'},
185 {"b", required_argument, 0, 'b'},
186 {"nw", no_argument, &use_windows, 0},
187 {"nowindows", no_argument, &use_windows, 0},
188 {"w", no_argument, &use_windows, 1},
189 {"windows", no_argument, &use_windows, 1},
190 {"statistics", no_argument, 0, 13},
191 /* Allow machine descriptions to add more options... */
192 #ifdef ADDITIONAL_OPTIONS
193 ADDITIONAL_OPTIONS
194 #endif
195 {0, no_argument, 0, 0}
196 };
197
198 while (1)
199 {
200 int option_index;
201
202 c = getopt_long_only (argc, argv, "",
203 long_options, &option_index);
204 if (c == EOF)
205 break;
206
207 /* Long option that takes an argument. */
208 if (c == 0 && long_options[option_index].flag == 0)
209 c = long_options[option_index].val;
210
211 switch (c)
212 {
213 case 0:
214 /* Long option that just sets a flag. */
215 break;
216 case 10:
217 symarg = optarg;
218 execarg = optarg;
219 break;
220 case 11:
221 cdarg = optarg;
222 break;
223 case 12:
224 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
225 annotation_level = atoi (optarg);
226 break;
227 case 13:
228 /* Enable the display of both time and space usage. */
229 display_time = 1;
230 display_space = 1;
231 break;
232 case 'f':
233 annotation_level = 1;
234 /* We have probably been invoked from emacs. Disable window interface. */
235 use_windows = 0;
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 #endif
323 gdb_init ();
324
325 /* Do these (and anything which might call wrap_here or *_filtered)
326 after initialize_all_files. */
327 if (print_version)
328 {
329 print_gdb_version (gdb_stdout);
330 wrap_here ("");
331 printf_filtered ("\n");
332 exit (0);
333 }
334
335 if (print_help)
336 {
337 /* --version is intentionally not documented here, because we
338 are printing the version here, and the help is long enough
339 already. */
340
341 print_gdb_version (gdb_stdout);
342 /* Make sure the output gets printed. */
343 wrap_here ("");
344 printf_filtered ("\n");
345
346 /* But don't use *_filtered here. We don't want to prompt for continue
347 no matter how small the screen or how much we're going to print. */
348 fputs_unfiltered ("\
349 This is the GNU debugger. Usage:\n\
350 gdb [options] [executable-file [core-file or process-id]]\n\
351 Options:\n\
352 --help Print this message.\n\
353 --quiet Do not print version number on startup.\n\
354 --fullname Output information used by emacs-GDB interface.\n\
355 --epoch Output information used by epoch emacs-GDB interface.\n\
356 ", gdb_stdout);
357 fputs_unfiltered ("\
358 --batch Exit after processing options.\n\
359 --nx Do not read .gdbinit file.\n\
360 --tty=TTY Use TTY for input/output by the program being debugged.\n\
361 --cd=DIR Change current directory to DIR.\n\
362 --directory=DIR Search for source files in DIR.\n\
363 ", gdb_stdout);
364 fputs_unfiltered ("\
365 --command=FILE Execute GDB commands from FILE.\n\
366 --symbols=SYMFILE Read symbols from SYMFILE.\n\
367 --exec=EXECFILE Use EXECFILE as the executable.\n\
368 --se=FILE Use FILE as symbol file and executable file.\n\
369 ", gdb_stdout);
370 fputs_unfiltered ("\
371 --core=COREFILE Analyze the core dump COREFILE.\n\
372 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
373 --mapped Use mapped symbol files if supported on this system.\n\
374 --readnow Fully read symbol files on first access.\n\
375 --nw Do not use a window interface.\n\
376 ", gdb_stdout);
377 #ifdef ADDITIONAL_OPTION_HELP
378 fputs_unfiltered (ADDITIONAL_OPTION_HELP, gdb_stdout);
379 #endif
380 fputs_unfiltered ("\n\
381 For more information, type \"help\" from within GDB, or consult the\n\
382 GDB manual (available as on-line info or a printed manual).\n", gdb_stdout);
383 exit (0);
384 }
385
386 if (!quiet)
387 {
388 /* Print all the junk at the top, with trailing "..." if we are about
389 to read a symbol file (possibly slowly). */
390 print_gnu_advertisement ();
391 print_gdb_version (gdb_stdout);
392 if (symarg)
393 printf_filtered ("..");
394 wrap_here("");
395 gdb_flush (gdb_stdout); /* Force to screen during slow operations */
396 }
397
398 error_pre_print = "\n\n";
399 quit_pre_print = error_pre_print;
400
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 quit_pre_print = error_pre_print;
482 warning_pre_print = "\nwarning: ";
483
484 if (corearg != NULL)
485 if (!SET_TOP_LEVEL ())
486 core_file_command (corearg, !batch);
487 else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ())
488 attach_command (corearg, !batch);
489 do_cleanups (ALL_CLEANUPS);
490
491 if (ttyarg != NULL)
492 if (!SET_TOP_LEVEL ())
493 tty_command (ttyarg, !batch);
494 do_cleanups (ALL_CLEANUPS);
495
496 #ifdef ADDITIONAL_OPTION_HANDLER
497 ADDITIONAL_OPTION_HANDLER;
498 #endif
499
500 /* Error messages should no longer be distinguished with extra output. */
501 error_pre_print = NULL;
502 quit_pre_print = NULL;
503 warning_pre_print = "warning: ";
504
505 /* Read the .gdbinit file in the current directory, *if* it isn't
506 the same as the $HOME/.gdbinit file (it should exist, also). */
507
508 if (!homedir
509 || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
510 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
511 {
512 if (!SET_TOP_LEVEL ())
513 source_command (gdbinit, 0);
514 }
515 do_cleanups (ALL_CLEANUPS);
516
517 for (i = 0; i < ncmd; i++)
518 {
519 if (!SET_TOP_LEVEL ())
520 {
521 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
522 read_command_file (stdin);
523 else
524 source_command (cmdarg[i], !batch);
525 do_cleanups (ALL_CLEANUPS);
526 }
527 }
528 free ((PTR)cmdarg);
529
530 /* Read in the old history after all the command files have been read. */
531 init_history();
532
533 if (batch)
534 {
535 /* We have hit the end of the batch file. */
536 exit (0);
537 }
538
539 /* Do any host- or target-specific hacks. This is used for i960 targets
540 to force the user to set a nindy target and spec its parameters. */
541
542 #ifdef BEFORE_MAIN_LOOP_HOOK
543 BEFORE_MAIN_LOOP_HOOK;
544 #endif
545
546 END_PROGRESS (argv[0]);
547
548 /* Show time and/or space usage. */
549
550 if (display_time)
551 {
552 long init_time = get_run_time () - time_at_startup;
553
554 printf_unfiltered ("Startup time: %ld.%06ld\n",
555 init_time / 1000000, init_time % 1000000);
556 }
557
558 if (display_space)
559 {
560 extern char **environ;
561 char *lim = (char *) sbrk (0);
562
563 printf_unfiltered ("Startup size: data size %ld\n",
564 (long) (lim - (char *) &environ));
565 }
566
567 /* The default command loop.
568 The WIN32 Gui calls this main to set up gdb's state, and
569 has its own command loop. */
570 #if !defined (WIN32)
571 while (1)
572 {
573 if (!SET_TOP_LEVEL ())
574 {
575 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
576 /* GUIs generally have their own command loop, mainloop, or whatever.
577 This is a good place to gain control because many error
578 conditions will end up here via longjmp(). */
579 if (command_loop_hook)
580 command_loop_hook ();
581 else
582 command_loop ();
583 quit_command ((char *)0, instream == stdin);
584 }
585 }
586
587 /* No exit -- exit is through quit_command. */
588 #endif
589
590 }
591 \f
592 void
593 init_proc ()
594 {
595 }
596
597 int
598 proc_wait (pid, status)
599 int pid;
600 int *status;
601 {
602 #ifndef __GO32__
603 return wait (status);
604 #endif
605 }
606
607 void
608 proc_remove_foreign (pid)
609 int pid;
610 {
611 }
612
613 void
614 fputs_unfiltered (linebuffer, stream)
615 const char *linebuffer;
616 FILE *stream;
617 {
618 if (fputs_unfiltered_hook)
619 {
620 /* FIXME: I think we should only be doing this for stdout or stderr.
621 Either that or we should be passing stream to the hook so it can
622 deal with it. If that is cleaned up, this function can go back
623 into utils.c and the fputs_unfiltered_hook can replace the current
624 ability to avoid this function by not linking with main.c. */
625 fputs_unfiltered_hook (linebuffer, stream);
626 return;
627 }
628
629 fputs (linebuffer, stream);
630 }
This page took 0.041645 seconds and 5 git commands to generate.