* Makefile.in (CC_FOR_TARGET, CXX_FOR_TARGET): Use newlib if it is
[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'},
bd5635a1
RP
177/* Allow machine descriptions to add more options... */
178#ifdef ADDITIONAL_OPTIONS
179 ADDITIONAL_OPTIONS
180#endif
ee0613d1 181 {0, no_argument, 0, 0},
bd5635a1
RP
182 };
183
184 while (1)
185 {
ee0613d1
JG
186 int option_index;
187
bd5635a1
RP
188 c = getopt_long_only (argc, argv, "",
189 long_options, &option_index);
190 if (c == EOF)
191 break;
192
193 /* Long option that takes an argument. */
194 if (c == 0 && long_options[option_index].flag == 0)
195 c = long_options[option_index].val;
196
197 switch (c)
198 {
199 case 0:
200 /* Long option that just sets a flag. */
201 break;
202 case 10:
203 symarg = optarg;
204 execarg = optarg;
205 break;
206 case 11:
207 cdarg = optarg;
208 break;
6c803036
JK
209 case 12:
210 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
211 annotation_level = atoi (optarg);
212 break;
213 case 'f':
214 annotation_level = 1;
215 break;
bd5635a1
RP
216 case 's':
217 symarg = optarg;
218 break;
219 case 'e':
220 execarg = optarg;
221 break;
222 case 'c':
223 corearg = optarg;
224 break;
225 case 'x':
226 cmdarg[ncmd++] = optarg;
227 if (ncmd >= cmdsize)
228 {
229 cmdsize *= 2;
230 cmdarg = (char **) xrealloc ((char *)cmdarg,
231 cmdsize * sizeof (*cmdarg));
232 }
233 break;
234 case 'd':
235 dirarg[ndir++] = optarg;
236 if (ndir >= dirsize)
237 {
238 dirsize *= 2;
239 dirarg = (char **) xrealloc ((char *)dirarg,
240 dirsize * sizeof (*dirarg));
241 }
242 break;
243 case 't':
244 ttyarg = optarg;
245 break;
246 case 'q':
247 quiet = 1;
248 break;
249 case 'b':
d0d8484a
SG
250 {
251 int i;
252 char *p;
253
254 i = strtol (optarg, &p, 0);
255 if (i == 0 && p == optarg)
8e4c7b3e
JK
256
257 /* Don't use *_filtered or warning() (which relies on
258 current_target) until after initialize_all_files(). */
259
260 fprintf_unfiltered
261 (gdb_stderr,
262 "warning: could not set baud rate to `%s'.\n", optarg);
d0d8484a
SG
263 else
264 baud_rate = i;
265 }
bd5635a1 266 break;
d0d8484a 267
bd5635a1
RP
268#ifdef ADDITIONAL_OPTION_CASES
269 ADDITIONAL_OPTION_CASES
270#endif
271 case '?':
199b2450 272 fprintf_unfiltered (gdb_stderr,
fb29d681 273 "Use `%s --help' for a complete list of options.\n",
bd5635a1
RP
274 argv[0]);
275 exit (1);
276 }
bd5635a1 277 }
fb29d681 278
bd5635a1
RP
279 /* OK, that's all the options. The other arguments are filenames. */
280 count = 0;
281 for (; optind < argc; optind++)
282 switch (++count)
283 {
284 case 1:
285 symarg = argv[optind];
286 execarg = argv[optind];
287 break;
288 case 2:
289 corearg = argv[optind];
290 break;
291 case 3:
199b2450 292 fprintf_unfiltered (gdb_stderr,
bd5635a1
RP
293 "Excess command line arguments ignored. (%s%s)\n",
294 argv[optind], (optind == argc - 1) ? "" : " ...");
295 break;
296 }
297 if (batch)
298 quiet = 1;
299 }
300
d8fc8773 301 gdb_init ();
bd5635a1 302
c307bb11
JK
303 /* Do these (and anything which might call wrap_here or *_filtered)
304 after initialize_all_files. */
305 if (print_version)
306 {
199b2450 307 print_gdb_version (gdb_stdout);
c307bb11
JK
308 wrap_here ("");
309 printf_filtered ("\n");
310 exit (0);
311 }
312
313 if (print_help)
314 {
315 /* --version is intentionally not documented here, because we
316 are printing the version here, and the help is long enough
317 already. */
318
199b2450 319 print_gdb_version (gdb_stdout);
c307bb11
JK
320 /* Make sure the output gets printed. */
321 wrap_here ("");
322 printf_filtered ("\n");
323
324 /* But don't use *_filtered here. We don't want to prompt for continue
325 no matter how small the screen or how much we're going to print. */
a6b26c44
SS
326/* start-sanitize-mpw */
327/* For reasons too ugly to describe... */
328#ifdef MPW_C
329 fputs_unfiltered ("This is the GNU debugger.\n", gdb_stdout);
330#else
331/* end-sanitize-mpw */
199b2450 332 fputs_unfiltered ("\
c307bb11
JK
333This is the GNU debugger. Usage:\n\
334 gdb [options] [executable-file [core-file or process-id]]\n\
335Options:\n\
336 --help Print this message.\n\
337 --quiet Do not print version number on startup.\n\
338 --fullname Output information used by emacs-GDB interface.\n\
339 --epoch Output information used by epoch emacs-GDB interface.\n\
340 --batch Exit after processing options.\n\
341 --nx Do not read .gdbinit file.\n\
342 --tty=TTY Use TTY for input/output by the program being debugged.\n\
343 --cd=DIR Change current directory to DIR.\n\
344 --directory=DIR Search for source files in DIR.\n\
345 --command=FILE Execute GDB commands from FILE.\n\
346 --symbols=SYMFILE Read symbols from SYMFILE.\n\
347 --exec=EXECFILE Use EXECFILE as the executable.\n\
348 --se=FILE Use FILE as symbol file and executable file.\n\
349 --core=COREFILE Analyze the core dump COREFILE.\n\
350 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
351 --mapped Use mapped symbol files if supported on this system.\n\
352 --readnow Fully read symbol files on first access.\n\
199b2450 353", gdb_stdout);
a6b26c44
SS
354/* start-sanitize-mpw */
355#endif /* MPW_C */
356/* end-sanitize-mpw */
c307bb11 357#ifdef ADDITIONAL_OPTION_HELP
199b2450 358 fputs_unfiltered (ADDITIONAL_OPTION_HELP, gdb_stdout);
c307bb11 359#endif
199b2450 360 fputs_unfiltered ("\n\
c307bb11 361For more information, type \"help\" from within GDB, or consult the\n\
199b2450 362GDB manual (available as on-line info or a printed manual).\n", gdb_stdout);
c307bb11
JK
363 exit (0);
364 }
365
bd5635a1
RP
366 if (!quiet)
367 {
81066208
JG
368 /* Print all the junk at the top, with trailing "..." if we are about
369 to read a symbol file (possibly slowly). */
370 print_gnu_advertisement ();
199b2450 371 print_gdb_version (gdb_stdout);
81066208
JG
372 if (symarg)
373 printf_filtered ("..");
bd099407 374 wrap_here("");
199b2450 375 gdb_flush (gdb_stdout); /* Force to screen during slow operations */
bd5635a1
RP
376 }
377
81066208 378 error_pre_print = "\n\n";
51b57ded
FF
379 /* We may get more than one warning, don't double space all of them... */
380 warning_pre_print = "\nwarning: ";
81066208 381
3a16d640
JG
382 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
383 *before* all the command line arguments are processed; it sets
384 global parameters, which are independent of what file you are
385 debugging or what directory you are in. */
386 homedir = getenv ("HOME");
387 if (homedir)
388 {
389 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
390 strlen (gdbinit) + 10);
391 strcpy (homeinit, getenv ("HOME"));
392 strcat (homeinit, "/");
393 strcat (homeinit, gdbinit);
394 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
395 {
9748446f 396 if (!SET_TOP_LEVEL ())
3a16d640
JG
397 source_command (homeinit, 0);
398 }
399 do_cleanups (ALL_CLEANUPS);
400
401 /* Do stats; no need to do them elsewhere since we'll only
402 need them if homedir is set. Make sure that they are
403 zero in case one of them fails (this guarantees that they
404 won't match if either exists). */
405
406 memset (&homebuf, 0, sizeof (struct stat));
407 memset (&cwdbuf, 0, sizeof (struct stat));
408
409 stat (homeinit, &homebuf);
410 stat (gdbinit, &cwdbuf); /* We'll only need this if
411 homedir was set. */
412 }
413
bd5635a1
RP
414 /* Now perform all the actions indicated by the arguments. */
415 if (cdarg != NULL)
416 {
9748446f 417 if (!SET_TOP_LEVEL ())
bd5635a1
RP
418 {
419 cd_command (cdarg, 0);
bd5635a1
RP
420 }
421 }
f266e564
JK
422 do_cleanups (ALL_CLEANUPS);
423
bd5635a1 424 for (i = 0; i < ndir; i++)
9748446f 425 if (!SET_TOP_LEVEL ())
bd5635a1 426 directory_command (dirarg[i], 0);
51b57ded 427 free ((PTR)dirarg);
f266e564
JK
428 do_cleanups (ALL_CLEANUPS);
429
bd5635a1
RP
430 if (execarg != NULL
431 && symarg != NULL
2e4964ad 432 && STREQ (execarg, symarg))
bd5635a1
RP
433 {
434 /* The exec file and the symbol-file are the same. If we can't open
435 it, better only print one error message. */
9748446f 436 if (!SET_TOP_LEVEL ())
bd5635a1
RP
437 {
438 exec_file_command (execarg, !batch);
81066208 439 symbol_file_command (symarg, 0);
bd5635a1
RP
440 }
441 }
442 else
443 {
444 if (execarg != NULL)
9748446f 445 if (!SET_TOP_LEVEL ())
bd5635a1
RP
446 exec_file_command (execarg, !batch);
447 if (symarg != NULL)
9748446f 448 if (!SET_TOP_LEVEL ())
81066208 449 symbol_file_command (symarg, 0);
bd5635a1 450 }
f266e564
JK
451 do_cleanups (ALL_CLEANUPS);
452
81066208
JG
453 /* After the symbol file has been read, print a newline to get us
454 beyond the copyright line... But errors should still set off
455 the error message with a (single) blank line. */
afe4ca15
JG
456 if (!quiet)
457 printf_filtered ("\n");
81066208 458 error_pre_print = "\n";
318bf84f 459 warning_pre_print = "\nwarning: ";
81066208 460
bd5635a1 461 if (corearg != NULL)
9748446f 462 if (!SET_TOP_LEVEL ())
bd5635a1 463 core_file_command (corearg, !batch);
9748446f 464 else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ())
bd5635a1 465 attach_command (corearg, !batch);
f266e564 466 do_cleanups (ALL_CLEANUPS);
bd5635a1
RP
467
468 if (ttyarg != NULL)
9748446f 469 if (!SET_TOP_LEVEL ())
bd5635a1 470 tty_command (ttyarg, !batch);
f266e564 471 do_cleanups (ALL_CLEANUPS);
bd5635a1
RP
472
473#ifdef ADDITIONAL_OPTION_HANDLER
474 ADDITIONAL_OPTION_HANDLER;
475#endif
476
81066208
JG
477 /* Error messages should no longer be distinguished with extra output. */
478 error_pre_print = 0;
318bf84f 479 warning_pre_print = "warning: ";
81066208 480
3a16d640
JG
481 /* Read the .gdbinit file in the current directory, *if* it isn't
482 the same as the $HOME/.gdbinit file (it should exist, also). */
483
484 if (!homedir
485 || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
486 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
bd5635a1 487 {
9748446f 488 if (!SET_TOP_LEVEL ())
d75710b0 489 source_command (gdbinit, 0);
bd5635a1 490 }
d75710b0
FF
491 do_cleanups (ALL_CLEANUPS);
492
493 for (i = 0; i < ncmd; i++)
494 {
9748446f 495 if (!SET_TOP_LEVEL ())
d75710b0
FF
496 {
497 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
498 read_command_file (stdin);
499 else
500 source_command (cmdarg[i], !batch);
501 do_cleanups (ALL_CLEANUPS);
502 }
503 }
51b57ded 504 free ((PTR)cmdarg);
bd5635a1 505
8b3c897a 506 /* Read in the old history after all the command files have been read. */
fc61e9ee 507 init_history();
8b3c897a 508
bd5635a1
RP
509 if (batch)
510 {
511 /* We have hit the end of the batch file. */
512 exit (0);
513 }
514
515 /* Do any host- or target-specific hacks. This is used for i960 targets
516 to force the user to set a nindy target and spec its parameters. */
517
518#ifdef BEFORE_MAIN_LOOP_HOOK
519 BEFORE_MAIN_LOOP_HOOK;
520#endif
521
522 /* The command loop. */
523
524 while (1)
525 {
9748446f 526 if (!SET_TOP_LEVEL ())
bd5635a1 527 {
f266e564 528 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
a6b26c44
SS
529/* start-sanitize-mpw */
530#ifdef MPW
531 /* If we're being a Mac application, go into a Mac-specific
532 event-handling loop instead. We still want to be inside
533 the outer loop, because that will catch longjmps resulting
534 from some command executions. */
535 if (mac_app)
536 mac_command_loop ();
537 else
538#endif /* MPW */
60b3e281 539/* end-sanitize-mpw */
bd5635a1
RP
540 command_loop ();
541 quit_command ((char *)0, instream == stdin);
542 }
543 }
544 /* No exit -- exit is through quit_command. */
545}
d3507982
JK
546\f
547void
548init_proc ()
549{
550}
551
552int
553proc_wait (pid, status)
554 int pid;
555 int *status;
556{
844750e3 557#ifndef __GO32__
d3507982 558 return wait (status);
844750e3 559#endif
d3507982
JK
560}
561
562void
563proc_remove_foreign (pid)
564 int pid;
565{
566}
bd5635a1
RP
567
568void
b8ec8d4a
SS
569fputs_unfiltered (linebuffer, stream)
570 const char *linebuffer;
571 FILE *stream;
bd5635a1 572{
b8ec8d4a 573 fputs (linebuffer, stream);
bd5635a1 574}
This page took 0.204121 seconds and 4 git commands to generate.