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