1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 1999
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,
20 Boston, MA 02111-1307, USA. */
26 #include "call-cmds.h"
30 #include <sys/types.h>
34 #include "gdb_string.h"
35 #include "event-loop.h"
37 /* If nonzero, display time usage both at startup and for each command. */
41 /* If nonzero, display space usage both at startup and for each command. */
45 /* Whether this is the async version or not. The async version is
46 invoked on the command line with the -nw --async options. In this
47 version, the usual command_loop is substituted by and event loop which
48 processes UI events asynchronously. */
51 /* Whether this is the command line version or not */
54 /* Whether xdb commands will be handled */
57 /* Whether dbx commands will be handled */
63 GDB_FILE
*gdb_stdtarg
;
65 /* Used to initialize error() - defined in utils.c */
67 extern void error_init (void);
69 /* Whether to enable writing into executable and core files */
70 extern int write_files
;
72 static void print_gdb_help
PARAMS ((GDB_FILE
*));
74 /* These two are used to set the external editor commands when gdb is farming
75 out files to be edited by another program. */
77 extern int enable_external_editor
;
78 extern char *external_editor_command
;
81 #include <windows.h> /* for MAX_PATH */
82 #include <sys/cygwin.h> /* for cygwin32_conv_to_posix_path */
85 /* Call command_loop. If it happens to return, pass that through as a
86 non-zero return status. */
89 captured_command_loop (void *data
)
91 if (command_loop_hook
== NULL
)
95 /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
96 would clean things up (restoring the cleanup chain) to the state
97 they were just prior to the call. Technically, this means that
98 the do_cleanups() below is redundant. Unfortunatly, many FUNC's
99 are not that well behaved. do_cleanups should either be replaced
100 with a do_cleanups call (to cover the problem) or an assertion
101 check to detect bad FUNCs code. */
102 do_cleanups (ALL_CLEANUPS
);
103 /* If the command_loop returned, normally (rather than threw an
104 error) we try to quit. If the quit is aborted, catch_errors()
105 which called this catch the signal and restart the command
107 quit_command (NULL
, instream
== stdin
);
111 struct captured_main_args
118 captured_main (void *data
)
120 struct captured_main_args
*context
= data
;
121 int argc
= context
->argc
;
122 char **argv
= context
->argv
;
124 static int quiet
= 0;
125 static int batch
= 0;
127 /* Pointers to various arguments from command line. */
129 char *execarg
= NULL
;
130 char *corearg
= NULL
;
134 /* These are static so that we can take their address in an initializer. */
135 static int print_help
;
136 static int print_version
;
138 /* Pointers to all arguments of --command option. */
140 /* Allocated size of cmdarg. */
142 /* Number of elements of cmdarg used. */
145 /* Indices of all arguments of --directory option. */
147 /* Allocated size. */
149 /* Number of elements used. */
152 struct stat homebuf
, cwdbuf
;
153 char *homedir
, *homeinit
;
157 long time_at_startup
= get_run_time ();
159 START_PROGRESS (argv
[0], 0);
162 /* Do all Mac-specific setup. */
166 /* This needs to happen before the first use of malloc. */
167 init_malloc ((PTR
) NULL
);
169 #if defined (ALIGN_STACK_ON_STARTUP)
170 i
= (int) &count
& 0x3;
176 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
179 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
183 line
= (char *) xmalloc (linesize
);
184 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
187 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
188 current_directory
= gdb_dirbuf
;
192 gdb_stdout
= stdio_fileopen (stdout
);
193 gdb_stderr
= stdio_fileopen (stderr
);
194 gdb_stdlog
= gdb_stderr
; /* for moment */
195 gdb_stdtarg
= gdb_stderr
; /* for moment */
197 gdb_stdout
= tui_fileopen (stdout
);
198 gdb_stderr
= tui_fileopen (stderr
);
199 gdb_stdlog
= gdb_stdout
; /* for moment */
200 gdb_stdtarg
= gdb_stderr
; /* for moment */
203 /* initialize error() */
206 /* Parse arguments and options. */
209 /* When var field is 0, use flag field to record the equivalent
210 short option (or arbitrary numbers starting at 10 for those
211 with no equivalent). */
212 static struct option long_options
[] =
214 {"async", no_argument
, &event_loop_p
, 1},
215 {"noasync", no_argument
, &event_loop_p
, 0},
217 {"tui", no_argument
, &tui_version
, 1},
219 {"xdb", no_argument
, &xdb_commands
, 1},
220 {"dbx", no_argument
, &dbx_commands
, 1},
221 {"readnow", no_argument
, &readnow_symbol_files
, 1},
222 {"r", no_argument
, &readnow_symbol_files
, 1},
223 {"mapped", no_argument
, &mapped_symbol_files
, 1},
224 {"m", no_argument
, &mapped_symbol_files
, 1},
225 {"quiet", no_argument
, &quiet
, 1},
226 {"q", no_argument
, &quiet
, 1},
227 {"silent", no_argument
, &quiet
, 1},
228 {"nx", no_argument
, &inhibit_gdbinit
, 1},
229 {"n", no_argument
, &inhibit_gdbinit
, 1},
230 {"batch", no_argument
, &batch
, 1},
231 {"epoch", no_argument
, &epoch_interface
, 1},
233 /* This is a synonym for "--annotate=1". --annotate is now preferred,
234 but keep this here for a long time because people will be running
235 emacses which use --fullname. */
236 {"fullname", no_argument
, 0, 'f'},
237 {"f", no_argument
, 0, 'f'},
239 {"annotate", required_argument
, 0, 12},
240 {"help", no_argument
, &print_help
, 1},
241 {"se", required_argument
, 0, 10},
242 {"symbols", required_argument
, 0, 's'},
243 {"s", required_argument
, 0, 's'},
244 {"exec", required_argument
, 0, 'e'},
245 {"e", required_argument
, 0, 'e'},
246 {"core", required_argument
, 0, 'c'},
247 {"c", required_argument
, 0, 'c'},
248 {"command", required_argument
, 0, 'x'},
249 {"version", no_argument
, &print_version
, 1},
250 {"x", required_argument
, 0, 'x'},
251 {"directory", required_argument
, 0, 'd'},
252 {"cd", required_argument
, 0, 11},
253 {"tty", required_argument
, 0, 't'},
254 {"baud", required_argument
, 0, 'b'},
255 {"b", required_argument
, 0, 'b'},
256 {"nw", no_argument
, &use_windows
, 0},
257 {"nowindows", no_argument
, &use_windows
, 0},
258 {"w", no_argument
, &use_windows
, 1},
259 {"windows", no_argument
, &use_windows
, 1},
260 {"statistics", no_argument
, 0, 13},
261 {"write", no_argument
, &write_files
, 1},
262 /* Allow machine descriptions to add more options... */
263 #ifdef ADDITIONAL_OPTIONS
266 {0, no_argument
, 0, 0}
273 c
= getopt_long_only (argc
, argv
, "",
274 long_options
, &option_index
);
278 /* Long option that takes an argument. */
279 if (c
== 0 && long_options
[option_index
].flag
== 0)
280 c
= long_options
[option_index
].val
;
285 /* Long option that just sets a flag. */
295 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
296 annotation_level
= atoi (optarg
);
299 /* Enable the display of both time and space usage. */
304 annotation_level
= 1;
305 /* We have probably been invoked from emacs. Disable window interface. */
318 cmdarg
[ncmd
++] = optarg
;
322 cmdarg
= (char **) xrealloc ((char *) cmdarg
,
323 cmdsize
* sizeof (*cmdarg
));
327 dirarg
[ndir
++] = optarg
;
331 dirarg
= (char **) xrealloc ((char *) dirarg
,
332 dirsize
* sizeof (*dirarg
));
346 i
= strtol (optarg
, &p
, 0);
347 if (i
== 0 && p
== optarg
)
349 /* Don't use *_filtered or warning() (which relies on
350 current_target) until after initialize_all_files(). */
354 "warning: could not set baud rate to `%s'.\n", optarg
);
363 i
= strtol (optarg
, &p
, 0);
364 if (i
== 0 && p
== optarg
)
366 /* Don't use *_filtered or warning() (which relies on
367 current_target) until after initialize_all_files(). */
371 "warning: could not set timeout limit to `%s'.\n", optarg
);
377 #ifdef ADDITIONAL_OPTION_CASES
378 ADDITIONAL_OPTION_CASES
381 fprintf_unfiltered (gdb_stderr
,
382 "Use `%s --help' for a complete list of options.\n",
388 /* If --help or --version, disable window interface. */
389 if (print_help
|| print_version
)
393 /* Disable the TUI as well. */
399 /* An explicit --tui flag overrides the default UI, which is the
405 /* OK, that's all the options. The other arguments are filenames. */
407 for (; optind
< argc
; optind
++)
411 symarg
= argv
[optind
];
412 execarg
= argv
[optind
];
415 corearg
= argv
[optind
];
418 fprintf_unfiltered (gdb_stderr
,
419 "Excess command line arguments ignored. (%s%s)\n",
420 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
428 /* Should this be moved to tui-top.c:_initialize_tui()? */
430 init_ui_hook
= tuiInit
;
433 /* Initialize all files. Give the interpreter a chance to take
434 control of the console via the init_ui_hook()) */
437 /* Do these (and anything which might call wrap_here or *_filtered)
438 after initialize_all_files. */
441 print_gdb_version (gdb_stdout
);
443 printf_filtered ("\n");
449 print_gdb_help (gdb_stdout
);
450 fputs_unfiltered ("\n", gdb_stdout
);
456 /* Print all the junk at the top, with trailing "..." if we are about
457 to read a symbol file (possibly slowly). */
458 print_gdb_version (gdb_stdout
);
460 printf_filtered ("..");
462 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
465 error_pre_print
= "\n\n";
466 quit_pre_print
= error_pre_print
;
468 /* We may get more than one warning, don't double space all of them... */
469 warning_pre_print
= "\nwarning: ";
471 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
472 *before* all the command line arguments are processed; it sets
473 global parameters, which are independent of what file you are
474 debugging or what directory you are in. */
477 char *tmp
= getenv ("HOME");
481 homedir
= (char *) alloca (MAX_PATH
+ 1);
482 cygwin32_conv_to_posix_path (tmp
, homedir
);
488 homedir
= getenv ("HOME");
492 homeinit
= (char *) alloca (strlen (homedir
) +
493 strlen (gdbinit
) + 10);
494 strcpy (homeinit
, homedir
);
495 strcat (homeinit
, "/");
496 strcat (homeinit
, gdbinit
);
498 if (!inhibit_gdbinit
)
500 catch_command_errors (source_command
, homeinit
, 0, RETURN_MASK_ALL
);
503 /* Do stats; no need to do them elsewhere since we'll only
504 need them if homedir is set. Make sure that they are
505 zero in case one of them fails (this guarantees that they
506 won't match if either exists). */
508 memset (&homebuf
, 0, sizeof (struct stat
));
509 memset (&cwdbuf
, 0, sizeof (struct stat
));
511 stat (homeinit
, &homebuf
);
512 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
516 /* Now perform all the actions indicated by the arguments. */
519 catch_command_errors (cd_command
, cdarg
, 0, RETURN_MASK_ALL
);
522 for (i
= 0; i
< ndir
; i
++)
523 catch_command_errors (directory_command
, dirarg
[i
], 0, RETURN_MASK_ALL
);
528 && STREQ (execarg
, symarg
))
530 /* The exec file and the symbol-file are the same. If we can't
531 open it, better only print one error message.
532 catch_command_errors returns non-zero on success! */
533 if (catch_command_errors (exec_file_command
, execarg
, !batch
, RETURN_MASK_ALL
))
534 catch_command_errors (symbol_file_command
, symarg
, 0, RETURN_MASK_ALL
);
539 catch_command_errors (exec_file_command
, execarg
, !batch
, RETURN_MASK_ALL
);
541 catch_command_errors (symbol_file_command
, symarg
, 0, RETURN_MASK_ALL
);
544 /* After the symbol file has been read, print a newline to get us
545 beyond the copyright line... But errors should still set off
546 the error message with a (single) blank line. */
548 printf_filtered ("\n");
549 error_pre_print
= "\n";
550 quit_pre_print
= error_pre_print
;
551 warning_pre_print
= "\nwarning: ";
555 if (catch_command_errors (core_file_command
, corearg
, !batch
, RETURN_MASK_ALL
) == 0)
557 /* See if the core file is really a PID. */
558 if (isdigit (corearg
[0]))
559 catch_command_errors (attach_command
, corearg
, !batch
, RETURN_MASK_ALL
);
564 catch_command_errors (tty_command
, ttyarg
, !batch
, RETURN_MASK_ALL
);
566 #ifdef ADDITIONAL_OPTION_HANDLER
567 ADDITIONAL_OPTION_HANDLER
;
570 /* Error messages should no longer be distinguished with extra output. */
571 error_pre_print
= NULL
;
572 quit_pre_print
= NULL
;
573 warning_pre_print
= "warning: ";
575 /* Read the .gdbinit file in the current directory, *if* it isn't
576 the same as the $HOME/.gdbinit file (it should exist, also). */
579 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
580 if (!inhibit_gdbinit
)
582 catch_command_errors (source_command
, gdbinit
, 0, RETURN_MASK_ALL
);
585 for (i
= 0; i
< ncmd
; i
++)
588 /* NOTE: cagney/1999-11-03: SET_TOP_LEVEL() was a macro that
589 expanded into a call to setjmp(). */
590 if (!SET_TOP_LEVEL ()) /* NB: This is #if 0'd out */
592 /* NOTE: I am commenting this out, because it is not clear
593 where this feature is used. It is very old and
594 undocumented. ezannoni: 1999-05-04 */
596 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
597 read_command_file (stdin
);
600 source_command (cmdarg
[i
], !batch
);
601 do_cleanups (ALL_CLEANUPS
);
604 catch_command_errors (source_command
, cmdarg
[i
], !batch
, RETURN_MASK_ALL
);
608 /* Read in the old history after all the command files have been read. */
613 /* We have hit the end of the batch file. */
617 /* Do any host- or target-specific hacks. This is used for i960 targets
618 to force the user to set a nindy target and spec its parameters. */
620 #ifdef BEFORE_MAIN_LOOP_HOOK
621 BEFORE_MAIN_LOOP_HOOK
;
624 END_PROGRESS (argv
[0]);
626 /* Show time and/or space usage. */
630 long init_time
= get_run_time () - time_at_startup
;
632 printf_unfiltered ("Startup time: %ld.%06ld\n",
633 init_time
/ 1000000, init_time
% 1000000);
639 extern char **environ
;
640 char *lim
= (char *) sbrk (0);
642 printf_unfiltered ("Startup size: data size %ld\n",
643 (long) (lim
- (char *) &environ
));
647 /* The default command loop.
648 The WIN32 Gui calls this main to set up gdb's state, and
649 has its own command loop. */
650 #if !defined _WIN32 || defined __GNUC__
651 /* GUIs generally have their own command loop, mainloop, or
652 whatever. This is a good place to gain control because many
653 error conditions will end up here via longjmp(). */
655 /* FIXME: cagney/1999-11-06: The original main loop was like: */
658 if (!SET_TOP_LEVEL ())
660 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
661 /* GUIs generally have their own command loop, mainloop, or whatever.
662 This is a good place to gain control because many error
663 conditions will end up here via longjmp(). */
664 if (command_loop_hook
)
665 command_loop_hook ();
668 quit_command ((char *) 0, instream
== stdin
);
671 /* NOTE: If the command_loop() returned normally, the loop would
672 attempt to exit by calling the function quit_command(). That
673 function would either call exit() or throw an error returning
674 control to SET_TOP_LEVEL. */
675 /* NOTE: The function do_cleanups() was called once each time round
676 the loop. The usefulness of the call isn't clear. If an error
677 was thrown, everything would have already been cleaned up. If
678 command_loop() returned normally and quit_command() was called,
679 either exit() or error() (again cleaning up) would be called. */
681 /* NOTE: cagney/1999-11-07: There is probably no reason for not
682 moving this loop and the code found in captured_command_loop()
683 into the command_loop() proper. The main thing holding back that
684 change - SET_TOP_LEVEL() - has been eliminated. */
687 catch_errors (captured_command_loop
, 0, "", RETURN_MASK_ALL
);
690 /* No exit -- exit is through quit_command. */
694 main (int argc
, char **argv
)
697 struct captured_main_args args
;
700 catch_errors (captured_main
, &args
, "", RETURN_MASK_ALL
);
705 /* Don't use *_filtered for printing help. We don't want to prompt
706 for continue no matter how small the screen or how much we're going
710 print_gdb_help (stream
)
714 This is the GNU debugger. Usage:\n\n\
715 gdb [options] [executable-file [core-file or process-id]]\n\n\
719 --[no]async Enable (disable) asynchronous version of CLI\n\
722 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
723 --batch Exit after processing options.\n\
724 --cd=DIR Change current directory to DIR.\n\
725 --command=FILE Execute GDB commands from FILE.\n\
726 --core=COREFILE Analyze the core dump COREFILE.\n\
729 --dbx DBX compatibility mode.\n\
730 --directory=DIR Search for source files in DIR.\n\
731 --epoch Output information used by epoch emacs-GDB interface.\n\
732 --exec=EXECFILE Use EXECFILE as the executable.\n\
733 --fullname Output information used by emacs-GDB interface.\n\
734 --help Print this message.\n\
737 --mapped Use mapped symbol files if supported on this system.\n\
738 --nw Do not use a window interface.\n\
739 --nx Do not read ", stream
);
740 fputs_unfiltered (gdbinit
, stream
);
741 fputs_unfiltered (" file.\n\
742 --quiet Do not print version number on startup.\n\
743 --readnow Fully read symbol files on first access.\n\
746 --se=FILE Use FILE as symbol file and executable file.\n\
747 --symbols=SYMFILE Read symbols from SYMFILE.\n\
748 --tty=TTY Use TTY for input/output by the program being debugged.\n\
752 --tui Use a terminal user interface.\n\
756 --version Print version information and then exit.\n\
757 -w Use a window interface.\n\
758 --write Set writing into executable and core files.\n\
759 --xdb XDB compatibility mode.\n\
761 #ifdef ADDITIONAL_OPTION_HELP
762 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, stream
);
764 fputs_unfiltered ("\n\
765 For more information, type \"help\" from within GDB, or consult the\n\
766 GDB manual (available as on-line info or a printed manual).\n\
767 Report bugs to \"bug-gdb@gnu.org\".\