* inftarg.c (child_thread_alive): New function to see if a
[deliverable/binutils-gdb.git] / gdb / main.c
CommitLineData
b8ec8d4a 1/* Top level stuff for GDB, the GNU debugger.
8b564df8 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
51b57ded 3 Free Software Foundation, Inc.
bd5635a1
RP
4
5This file is part of GDB.
6
e522fb52 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
e522fb52
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
e522fb52 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
e522fb52
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 20
bd5635a1 21#include "defs.h"
b8ec8d4a
SS
22#include <setjmp.h>
23#include "top.h"
bd5635a1 24#include "target.h"
844750e3
SS
25#include "inferior.h"
26#include "call-cmds.h"
bd5635a1 27
e522fb52
JG
28#include "getopt.h"
29
bd5635a1 30#include <sys/types.h>
b8ec8d4a 31#include <sys/stat.h>
844750e3 32#include <ctype.h>
b8ec8d4a
SS
33
34#include <string.h>
35/* R_OK lives in either unistd.h or sys/file.h. */
09973223 36#ifdef USG
bd5635a1
RP
37#include <unistd.h>
38#endif
ee0613d1 39#ifndef NO_SYS_FILE
bd5635a1 40#include <sys/file.h>
ee0613d1 41#endif
bd5635a1 42
9748446f 43/* Temporary variable for SET_TOP_LEVEL. */
86db943c 44
9748446f
JK
45static 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
86db943c
SG
56/* If nonzero, display time usage both at startup and for each command. */
57
58int display_time;
59
60/* If nonzero, display space usage both at startup and for each command. */
61
62int display_space;
63
844750e3
SS
64extern void gdb_init PARAMS ((void));
65
bd5635a1
RP
66int
67main (argc, argv)
68 int argc;
69 char **argv;
70{
71 int count;
bd5635a1
RP
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
c307bb11
JK
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. */
bd5635a1
RP
87 char **cmdarg;
88 /* Allocated size of cmdarg. */
89 int cmdsize;
90 /* Number of elements of cmdarg used. */
91 int ncmd;
92
c307bb11 93 /* Indices of all arguments of --directory option. */
bd5635a1
RP
94 char **dirarg;
95 /* Allocated size. */
96 int dirsize;
97 /* Number of elements used. */
98 int ndir;
99
3a16d640
JG
100 struct stat homebuf, cwdbuf;
101 char *homedir, *homeinit;
102
bd5635a1
RP
103 register int i;
104
86db943c
SG
105 long time_at_startup = get_run_time ();
106
a6b26c44
SS
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");
b8ec8d4a
SS
112 /* Do all Mac-specific setup. */
113 mac_init ();
a6b26c44
SS
114#endif /* MPW */
115/* end-sanitize-mpw */
bd5635a1 116 /* This needs to happen before the first use of malloc. */
318bf84f 117 init_malloc ((PTR) NULL);
bd5635a1
RP
118
119#if defined (ALIGN_STACK_ON_STARTUP)
120 i = (int) &count & 0x3;
121 if (i != 0)
122 alloca (4 - i);
123#endif
124
bd099407 125 /* If error() is called from initialization code, just exit */
9748446f 126 if (SET_TOP_LEVEL ()) {
bd099407
JG
127 exit(1);
128 }
129
bd5635a1
RP
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
b7ec5b8d
FF
142 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
143 current_directory = gdb_dirbuf;
bd5635a1 144
bd5635a1
RP
145 /* Parse arguments and options. */
146 {
147 int c;
bd5635a1
RP
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 {
318bf84f
FF
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},
ee0613d1
JG
157 {"quiet", no_argument, &quiet, 1},
158 {"q", no_argument, &quiet, 1},
fb29d681 159 {"silent", no_argument, &quiet, 1},
ee0613d1
JG
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},
6c803036
JK
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},
ee0613d1
JG
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'},
c307bb11 181 {"version", no_argument, &print_version, 1},
ee0613d1
JG
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'},
86db943c
SG
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},
bd5635a1
RP
193/* Allow machine descriptions to add more options... */
194#ifdef ADDITIONAL_OPTIONS
195 ADDITIONAL_OPTIONS
196#endif
86db943c 197 {0, no_argument, 0, 0}
bd5635a1
RP
198 };
199
200 while (1)
201 {
ee0613d1
JG
202 int option_index;
203
bd5635a1
RP
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;
6c803036
JK
225 case 12:
226 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
227 annotation_level = atoi (optarg);
228 break;
86db943c
SG
229 case 13:
230 /* Enable the display of both time and space usage. */
231 display_time = 1;
232 display_space = 1;
233 break;
6c803036
JK
234 case 'f':
235 annotation_level = 1;
236 break;
bd5635a1
RP
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':
d0d8484a
SG
271 {
272 int i;
273 char *p;
274
275 i = strtol (optarg, &p, 0);
276 if (i == 0 && p == optarg)
8e4c7b3e
JK
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);
d0d8484a
SG
284 else
285 baud_rate = i;
286 }
bd5635a1 287 break;
d0d8484a 288
bd5635a1
RP
289#ifdef ADDITIONAL_OPTION_CASES
290 ADDITIONAL_OPTION_CASES
291#endif
292 case '?':
199b2450 293 fprintf_unfiltered (gdb_stderr,
fb29d681 294 "Use `%s --help' for a complete list of options.\n",
bd5635a1
RP
295 argv[0]);
296 exit (1);
297 }
bd5635a1 298 }
fb29d681 299
bd5635a1
RP
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:
199b2450 313 fprintf_unfiltered (gdb_stderr,
bd5635a1
RP
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
d8fc8773 322 gdb_init ();
bd5635a1 323
c307bb11
JK
324 /* Do these (and anything which might call wrap_here or *_filtered)
325 after initialize_all_files. */
326 if (print_version)
327 {
199b2450 328 print_gdb_version (gdb_stdout);
c307bb11
JK
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
199b2450 340 print_gdb_version (gdb_stdout);
c307bb11
JK
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. */
a6b26c44
SS
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 */
199b2450 353 fputs_unfiltered ("\
c307bb11
JK
354This is the GNU debugger. Usage:\n\
355 gdb [options] [executable-file [core-file or process-id]]\n\
356Options:\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\
754e5da2 374 --nw Do not use a window interface.\n\
199b2450 375", gdb_stdout);
a6b26c44
SS
376/* start-sanitize-mpw */
377#endif /* MPW_C */
378/* end-sanitize-mpw */
c307bb11 379#ifdef ADDITIONAL_OPTION_HELP
199b2450 380 fputs_unfiltered (ADDITIONAL_OPTION_HELP, gdb_stdout);
c307bb11 381#endif
199b2450 382 fputs_unfiltered ("\n\
c307bb11 383For more information, type \"help\" from within GDB, or consult the\n\
199b2450 384GDB manual (available as on-line info or a printed manual).\n", gdb_stdout);
c307bb11
JK
385 exit (0);
386 }
387
bd5635a1
RP
388 if (!quiet)
389 {
81066208
JG
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 ();
199b2450 393 print_gdb_version (gdb_stdout);
81066208
JG
394 if (symarg)
395 printf_filtered ("..");
bd099407 396 wrap_here("");
199b2450 397 gdb_flush (gdb_stdout); /* Force to screen during slow operations */
bd5635a1
RP
398 }
399
81066208 400 error_pre_print = "\n\n";
51b57ded
FF
401 /* We may get more than one warning, don't double space all of them... */
402 warning_pre_print = "\nwarning: ";
81066208 403
3a16d640
JG
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 {
9748446f 418 if (!SET_TOP_LEVEL ())
3a16d640
JG
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 }
754e5da2 435
bd5635a1
RP
436 /* Now perform all the actions indicated by the arguments. */
437 if (cdarg != NULL)
438 {
9748446f 439 if (!SET_TOP_LEVEL ())
bd5635a1
RP
440 {
441 cd_command (cdarg, 0);
bd5635a1
RP
442 }
443 }
f266e564
JK
444 do_cleanups (ALL_CLEANUPS);
445
bd5635a1 446 for (i = 0; i < ndir; i++)
9748446f 447 if (!SET_TOP_LEVEL ())
bd5635a1 448 directory_command (dirarg[i], 0);
51b57ded 449 free ((PTR)dirarg);
f266e564
JK
450 do_cleanups (ALL_CLEANUPS);
451
bd5635a1
RP
452 if (execarg != NULL
453 && symarg != NULL
2e4964ad 454 && STREQ (execarg, symarg))
bd5635a1
RP
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. */
9748446f 458 if (!SET_TOP_LEVEL ())
bd5635a1
RP
459 {
460 exec_file_command (execarg, !batch);
81066208 461 symbol_file_command (symarg, 0);
bd5635a1
RP
462 }
463 }
464 else
465 {
466 if (execarg != NULL)
9748446f 467 if (!SET_TOP_LEVEL ())
bd5635a1
RP
468 exec_file_command (execarg, !batch);
469 if (symarg != NULL)
9748446f 470 if (!SET_TOP_LEVEL ())
81066208 471 symbol_file_command (symarg, 0);
bd5635a1 472 }
f266e564
JK
473 do_cleanups (ALL_CLEANUPS);
474
81066208
JG
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. */
afe4ca15
JG
478 if (!quiet)
479 printf_filtered ("\n");
81066208 480 error_pre_print = "\n";
318bf84f 481 warning_pre_print = "\nwarning: ";
81066208 482
bd5635a1 483 if (corearg != NULL)
9748446f 484 if (!SET_TOP_LEVEL ())
bd5635a1 485 core_file_command (corearg, !batch);
9748446f 486 else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ())
bd5635a1 487 attach_command (corearg, !batch);
f266e564 488 do_cleanups (ALL_CLEANUPS);
bd5635a1
RP
489
490 if (ttyarg != NULL)
9748446f 491 if (!SET_TOP_LEVEL ())
bd5635a1 492 tty_command (ttyarg, !batch);
f266e564 493 do_cleanups (ALL_CLEANUPS);
bd5635a1
RP
494
495#ifdef ADDITIONAL_OPTION_HANDLER
496 ADDITIONAL_OPTION_HANDLER;
497#endif
498
81066208
JG
499 /* Error messages should no longer be distinguished with extra output. */
500 error_pre_print = 0;
318bf84f 501 warning_pre_print = "warning: ";
81066208 502
3a16d640
JG
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)
bd5635a1 509 {
9748446f 510 if (!SET_TOP_LEVEL ())
d75710b0 511 source_command (gdbinit, 0);
bd5635a1 512 }
d75710b0
FF
513 do_cleanups (ALL_CLEANUPS);
514
515 for (i = 0; i < ncmd; i++)
516 {
9748446f 517 if (!SET_TOP_LEVEL ())
d75710b0
FF
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 }
51b57ded 526 free ((PTR)cmdarg);
bd5635a1 527
8b3c897a 528 /* Read in the old history after all the command files have been read. */
fc61e9ee 529 init_history();
8b3c897a 530
bd5635a1
RP
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
86db943c
SG
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
bd5635a1
RP
563 /* The command loop. */
564
565 while (1)
566 {
9748446f 567 if (!SET_TOP_LEVEL ())
bd5635a1 568 {
f266e564 569 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
754e5da2
SG
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 ();
a6b26c44 575 else
754e5da2 576 command_loop ();
bd5635a1
RP
577 quit_command ((char *)0, instream == stdin);
578 }
579 }
580 /* No exit -- exit is through quit_command. */
581}
d3507982
JK
582\f
583void
584init_proc ()
585{
586}
587
588int
589proc_wait (pid, status)
590 int pid;
591 int *status;
592{
844750e3 593#ifndef __GO32__
d3507982 594 return wait (status);
844750e3 595#endif
d3507982
JK
596}
597
598void
599proc_remove_foreign (pid)
600 int pid;
601{
602}
bd5635a1
RP
603
604void
b8ec8d4a
SS
605fputs_unfiltered (linebuffer, stream)
606 const char *linebuffer;
607 FILE *stream;
bd5635a1 608{
754e5da2
SG
609 if (fputs_unfiltered_hook)
610 {
86db943c
SG
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);
754e5da2
SG
617 return;
618 }
619
b8ec8d4a 620 fputs (linebuffer, stream);
bd5635a1 621}
This page took 0.246871 seconds and 4 git commands to generate.