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