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.
5 This file is part of GDB.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "call-cmds.h"
30 #include <sys/types.h>
34 #include "gdb_string.h"
36 /* Temporary variable for SET_TOP_LEVEL. */
38 static int top_level_val
;
40 /* Do a setjmp on error_return and quit_return. catch_errors is
41 generally a cleaner way to do this, but main() would look pretty
42 ugly if it had to use catch_errors each time. */
44 #define SET_TOP_LEVEL() \
45 (((top_level_val = SIGSETJMP (error_return)) \
46 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (SIGJMP_BUF))) \
49 /* If nonzero, display time usage both at startup and for each command. */
53 /* If nonzero, display space usage both at startup and for each command. */
57 /* Whether xdb commands will be handled */
60 /* Whether dbx commands will be handled */
63 static void print_gdb_help
PARAMS ((GDB_FILE
*));
64 extern void gdb_init
PARAMS ((char *));
66 #include <windows.h> /* for MAX_PATH */
67 #include <sys/cygwin.h> /* for cygwin32_conv_to_posix_path */
79 /* Pointers to various arguments from command line. */
86 /* These are static so that we can take their address in an initializer. */
87 static int print_help
;
88 static int print_version
;
90 /* Pointers to all arguments of --command option. */
92 /* Allocated size of cmdarg. */
94 /* Number of elements of cmdarg used. */
97 /* Indices of all arguments of --directory option. */
101 /* Number of elements used. */
104 struct stat homebuf
, cwdbuf
;
105 char *homedir
, *homeinit
;
109 long time_at_startup
= get_run_time ();
111 START_PROGRESS (argv
[0], 0);
114 /* Do all Mac-specific setup. */
118 /* This needs to happen before the first use of malloc. */
119 init_malloc ((PTR
) NULL
);
121 #if defined (ALIGN_STACK_ON_STARTUP)
122 i
= (int) &count
& 0x3;
127 /* If error() is called from initialization code, just exit */
128 if (SET_TOP_LEVEL ()) {
133 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
136 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
140 line
= (char *) xmalloc (linesize
);
141 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
144 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
145 current_directory
= gdb_dirbuf
;
147 /* Parse arguments and options. */
150 /* When var field is 0, use flag field to record the equivalent
151 short option (or arbitrary numbers starting at 10 for those
152 with no equivalent). */
153 static struct option long_options
[] =
155 {"readnow", no_argument
, &readnow_symbol_files
, 1},
156 {"r", no_argument
, &readnow_symbol_files
, 1},
157 {"mapped", no_argument
, &mapped_symbol_files
, 1},
158 {"m", no_argument
, &mapped_symbol_files
, 1},
159 {"quiet", no_argument
, &quiet
, 1},
160 {"q", no_argument
, &quiet
, 1},
161 {"silent", no_argument
, &quiet
, 1},
162 {"nx", no_argument
, &inhibit_gdbinit
, 1},
163 {"n", no_argument
, &inhibit_gdbinit
, 1},
164 {"batch", no_argument
, &batch
, 1},
165 {"epoch", no_argument
, &epoch_interface
, 1},
167 /* This is a synonym for "--annotate=1". --annotate is now preferred,
168 but keep this here for a long time because people will be running
169 emacses which use --fullname. */
170 {"fullname", no_argument
, 0, 'f'},
171 {"f", no_argument
, 0, 'f'},
173 {"annotate", required_argument
, 0, 12},
174 {"help", no_argument
, &print_help
, 1},
175 {"se", required_argument
, 0, 10},
176 {"symbols", required_argument
, 0, 's'},
177 {"s", required_argument
, 0, 's'},
178 {"exec", required_argument
, 0, 'e'},
179 {"e", required_argument
, 0, 'e'},
180 {"core", required_argument
, 0, 'c'},
181 {"c", required_argument
, 0, 'c'},
182 {"command", required_argument
, 0, 'x'},
183 {"version", no_argument
, &print_version
, 1},
184 {"x", required_argument
, 0, 'x'},
185 /* start-sanitize-gdbtk */
187 {"tclcommand", required_argument
, 0, 'z'},
189 /* end-sanitize-gdbtk */
190 {"directory", required_argument
, 0, 'd'},
191 {"cd", required_argument
, 0, 11},
192 {"tty", required_argument
, 0, 't'},
193 {"baud", required_argument
, 0, 'b'},
194 {"b", required_argument
, 0, 'b'},
195 {"nw", no_argument
, &use_windows
, 0},
196 {"nowindows", no_argument
, &use_windows
, 0},
197 {"w", no_argument
, &use_windows
, 1},
198 {"windows", no_argument
, &use_windows
, 1},
199 {"statistics", no_argument
, 0, 13},
200 /* Allow machine descriptions to add more options... */
201 #ifdef ADDITIONAL_OPTIONS
204 {0, no_argument
, 0, 0}
211 c
= getopt_long_only (argc
, argv
, "",
212 long_options
, &option_index
);
216 /* Long option that takes an argument. */
217 if (c
== 0 && long_options
[option_index
].flag
== 0)
218 c
= long_options
[option_index
].val
;
223 /* Long option that just sets a flag. */
233 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
234 annotation_level
= atoi (optarg
);
237 /* Enable the display of both time and space usage. */
242 annotation_level
= 1;
243 /* We have probably been invoked from emacs. Disable window interface. */
256 cmdarg
[ncmd
++] = optarg
;
260 cmdarg
= (char **) xrealloc ((char *)cmdarg
,
261 cmdsize
* sizeof (*cmdarg
));
264 /* start-sanitize-gdbtk */
268 extern int gdbtk_test
PARAMS ((char *));
269 if (!gdbtk_test (optarg
))
271 fprintf_unfiltered (gdb_stderr
, "%s: unable to load tclcommand file \"%s\"",
278 /* end-sanitize-gdbtk */
280 dirarg
[ndir
++] = optarg
;
284 dirarg
= (char **) xrealloc ((char *)dirarg
,
285 dirsize
* sizeof (*dirarg
));
299 i
= strtol (optarg
, &p
, 0);
300 if (i
== 0 && p
== optarg
)
302 /* Don't use *_filtered or warning() (which relies on
303 current_target) until after initialize_all_files(). */
307 "warning: could not set baud rate to `%s'.\n", optarg
);
316 i
= strtol (optarg
, &p
, 0);
317 if (i
== 0 && p
== optarg
)
319 /* Don't use *_filtered or warning() (which relies on
320 current_target) until after initialize_all_files(). */
324 "warning: could not set timeout limit to `%s'.\n", optarg
);
330 #ifdef ADDITIONAL_OPTION_CASES
331 ADDITIONAL_OPTION_CASES
334 fprintf_unfiltered (gdb_stderr
,
335 "Use `%s --help' for a complete list of options.\n",
341 /* If --help or --version, disable window interface. */
342 if (print_help
|| print_version
)
345 /* OK, that's all the options. The other arguments are filenames. */
347 for (; optind
< argc
; optind
++)
351 symarg
= argv
[optind
];
352 execarg
= argv
[optind
];
355 corearg
= argv
[optind
];
358 fprintf_unfiltered (gdb_stderr
,
359 "Excess command line arguments ignored. (%s%s)\n",
360 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
369 /* Do these (and anything which might call wrap_here or *_filtered)
370 after initialize_all_files. */
373 print_gdb_version (gdb_stdout
);
375 printf_filtered ("\n");
381 print_gdb_help (gdb_stdout
);
382 fputs_unfiltered ("\n", gdb_stdout
);
388 /* Print all the junk at the top, with trailing "..." if we are about
389 to read a symbol file (possibly slowly). */
390 print_gdb_version (gdb_stdout
);
392 printf_filtered ("..");
394 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
397 error_pre_print
= "\n\n";
398 quit_pre_print
= error_pre_print
;
400 /* We may get more than one warning, don't double space all of them... */
401 warning_pre_print
= "\nwarning: ";
403 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
404 *before* all the command line arguments are processed; it sets
405 global parameters, which are independent of what file you are
406 debugging or what directory you are in. */
409 char * tmp
= getenv ("HOME");
413 homedir
= (char *) alloca (MAX_PATH
+1);
414 cygwin32_conv_to_posix_path (tmp
, homedir
);
420 homedir
= getenv ("HOME");
424 homeinit
= (char *) alloca (strlen (homedir
) +
425 strlen (gdbinit
) + 10);
426 strcpy (homeinit
, homedir
);
427 strcat (homeinit
, "/");
428 strcat (homeinit
, gdbinit
);
430 if (!inhibit_gdbinit
)
432 if (!SET_TOP_LEVEL ())
433 source_command (homeinit
, 0);
435 do_cleanups (ALL_CLEANUPS
);
437 /* Do stats; no need to do them elsewhere since we'll only
438 need them if homedir is set. Make sure that they are
439 zero in case one of them fails (this guarantees that they
440 won't match if either exists). */
442 memset (&homebuf
, 0, sizeof (struct stat
));
443 memset (&cwdbuf
, 0, sizeof (struct stat
));
445 stat (homeinit
, &homebuf
);
446 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
450 /* Now perform all the actions indicated by the arguments. */
453 if (!SET_TOP_LEVEL ())
455 cd_command (cdarg
, 0);
458 do_cleanups (ALL_CLEANUPS
);
460 for (i
= 0; i
< ndir
; i
++)
461 if (!SET_TOP_LEVEL ())
462 directory_command (dirarg
[i
], 0);
464 do_cleanups (ALL_CLEANUPS
);
468 && STREQ (execarg
, symarg
))
470 /* The exec file and the symbol-file are the same. If we can't open
471 it, better only print one error message. */
472 if (!SET_TOP_LEVEL ())
474 exec_file_command (execarg
, !batch
);
475 symbol_file_command (symarg
, 0);
481 if (!SET_TOP_LEVEL ())
482 exec_file_command (execarg
, !batch
);
484 if (!SET_TOP_LEVEL ())
485 symbol_file_command (symarg
, 0);
487 do_cleanups (ALL_CLEANUPS
);
489 /* After the symbol file has been read, print a newline to get us
490 beyond the copyright line... But errors should still set off
491 the error message with a (single) blank line. */
493 printf_filtered ("\n");
494 error_pre_print
= "\n";
495 quit_pre_print
= error_pre_print
;
496 warning_pre_print
= "\nwarning: ";
499 if (!SET_TOP_LEVEL ())
500 core_file_command (corearg
, !batch
);
501 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
502 attach_command (corearg
, !batch
);
503 do_cleanups (ALL_CLEANUPS
);
506 if (!SET_TOP_LEVEL ())
507 tty_command (ttyarg
, !batch
);
508 do_cleanups (ALL_CLEANUPS
);
510 #ifdef ADDITIONAL_OPTION_HANDLER
511 ADDITIONAL_OPTION_HANDLER
;
514 /* Error messages should no longer be distinguished with extra output. */
515 error_pre_print
= NULL
;
516 quit_pre_print
= NULL
;
517 warning_pre_print
= "warning: ";
519 /* Read the .gdbinit file in the current directory, *if* it isn't
520 the same as the $HOME/.gdbinit file (it should exist, also). */
523 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
524 if (!inhibit_gdbinit
)
526 if (!SET_TOP_LEVEL ())
527 source_command (gdbinit
, 0);
529 do_cleanups (ALL_CLEANUPS
);
531 for (i
= 0; i
< ncmd
; i
++)
533 if (!SET_TOP_LEVEL ())
535 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
536 read_command_file (stdin
);
538 source_command (cmdarg
[i
], !batch
);
539 do_cleanups (ALL_CLEANUPS
);
544 /* Read in the old history after all the command files have been read. */
549 /* We have hit the end of the batch file. */
553 /* Do any host- or target-specific hacks. This is used for i960 targets
554 to force the user to set a nindy target and spec its parameters. */
556 #ifdef BEFORE_MAIN_LOOP_HOOK
557 BEFORE_MAIN_LOOP_HOOK
;
560 END_PROGRESS (argv
[0]);
562 /* Show time and/or space usage. */
566 long init_time
= get_run_time () - time_at_startup
;
568 printf_unfiltered ("Startup time: %ld.%06ld\n",
569 init_time
/ 1000000, init_time
% 1000000);
575 extern char **environ
;
576 char *lim
= (char *) sbrk (0);
578 printf_unfiltered ("Startup size: data size %ld\n",
579 (long) (lim
- (char *) &environ
));
583 /* The default command loop.
584 The WIN32 Gui calls this main to set up gdb's state, and
585 has its own command loop. */
586 #if !defined _WIN32 || defined __GNUC__
589 if (!SET_TOP_LEVEL ())
591 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
592 /* GUIs generally have their own command loop, mainloop, or whatever.
593 This is a good place to gain control because many error
594 conditions will end up here via longjmp(). */
595 if (command_loop_hook
)
596 command_loop_hook ();
599 quit_command ((char *)0, instream
== stdin
);
603 /* No exit -- exit is through quit_command. */
608 /* Don't use *_filtered for printing help. We don't want to prompt
609 for continue no matter how small the screen or how much we're going
613 print_gdb_help (stream
)
617 This is the GNU debugger. Usage:\n\n\
618 gdb [options] [executable-file [core-file or process-id]]\n\n\
622 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
623 --batch Exit after processing options.\n\
624 --cd=DIR Change current directory to DIR.\n\
625 --command=FILE Execute GDB commands from FILE.\n\
626 --core=COREFILE Analyze the core dump COREFILE.\n\
629 --directory=DIR Search for source files in DIR.\n\
630 --epoch Output information used by epoch emacs-GDB interface.\n\
631 --exec=EXECFILE Use EXECFILE as the executable.\n\
632 --fullname Output information used by emacs-GDB interface.\n\
633 --help Print this message.\n\
636 --mapped Use mapped symbol files if supported on this system.\n\
637 --nw Do not use a window interface.\n\
638 --nx Do not read .gdbinit file.\n\
639 --quiet Do not print version number on startup.\n\
640 --readnow Fully read symbol files on first access.\n\
643 --se=FILE Use FILE as symbol file and executable file.\n\
644 --symbols=SYMFILE Read symbols from SYMFILE.\n\
645 --tty=TTY Use TTY for input/output by the program being debugged.\n\
646 --version Print version information and then exit.\n\
648 #ifdef ADDITIONAL_OPTION_HELP
649 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, stream
);
651 fputs_unfiltered ("\n\
652 For more information, type \"help\" from within GDB, or consult the\n\
653 GDB manual (available as on-line info or a printed manual).\n\
654 Report bugs to \"bug-gdb@prep.ai.mit.edu\".\
665 proc_remove_foreign (pid
)
670 /* All I/O sent to the *_filtered and *_unfiltered functions eventually ends up
671 here. The fputs_unfiltered_hook is primarily used by GUIs to collect all
672 output and send it to the GUI, instead of the controlling terminal. Only
673 output to gdb_stdout and gdb_stderr are sent to the hook. Everything else
674 is sent on to fputs to allow file I/O to be handled appropriately. */
677 fputs_unfiltered (linebuffer
, stream
)
678 const char *linebuffer
;
681 if (fputs_unfiltered_hook
682 && (stream
== gdb_stdout
683 || stream
== gdb_stderr
))
684 fputs_unfiltered_hook (linebuffer
, stream
);
686 fputs (linebuffer
, stream
);