#include "defs.h"
#include "top.h"
#include "target.h"
#include "inferior.h"
#include "symfile.h"
#include "gdbcore.h"
#include "exceptions.h"
#include "getopt.h"
#include <sys/types.h>
#include "gdb_stat.h"
#include <ctype.h>
#include "gdb_string.h"
#include "event-loop.h"
#include "ui-out.h"
#include "osabi.h"
#include "arch-utils.h"
#include "interps.h"
#include "main.h"
#include <pthread.h>
int display_time;
int display_space;
char *interpreter_p;
int xdb_commands = 0;
int dbx_commands = 0;
char *gdb_sysroot = 0;
struct ui_file *gdb_stdout;
struct ui_file *gdb_stderr;
struct ui_file *gdb_stdlog;
struct ui_file *gdb_stdin;
struct ui_file *gdb_null;
struct ui_file *gdb_stdtargin;
struct ui_file *gdb_stdtarg;
struct ui_file *gdb_stdtargerr;
extern int write_files;
void set_osabi_option (const char *osabi_str);
static void print_gdb_help (struct ui_file *);
extern char *external_editor_command;
static int
captured_command_loop (void *data)
{
current_interp_command_loop ();
do_cleanups (ALL_CLEANUPS);
quit_command (NULL, instream == stdin);
return 1;
}
static int
captured_main (void *data)
{
struct captured_main_args *context = data;
int argc = context->argc;
char **argv = context->argv;
int count;
static int quiet = 0;
static int batch = 0;
static int set_args = 0;
char *symarg = NULL;
char *execarg = NULL;
char *corearg = NULL;
char *cdarg = NULL;
char *ttyarg = NULL;
char *osabiarg = NULL;
static int print_help;
static int print_version;
char **cmdarg;
int cmdsize;
int ncmd;
char **dirarg;
int dirsize;
int ndir;
struct stat homebuf, cwdbuf, globalbuf;
char *homedir;
char *attach_waitfor = NULL;
char *initial_arch = NULL;
int i;
long time_at_startup = get_run_time ();
#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
setlocale (LC_MESSAGES, "");
#endif
#if defined (HAVE_SETLOCALE)
setlocale (LC_CTYPE, "");
#endif
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
#ifdef USE_MMALLOC
init_mmalloc_default_pool ((PTR) NULL);
#endif
init_malloc (NULL);
#ifdef HAVE_SBRK
lim_at_start = (char *) sbrk (0);
#endif
#if defined (ALIGN_STACK_ON_STARTUP)
i = (int) &count & 0x3;
if (i != 0)
alloca (4 - i);
#endif
cmdsize = 1;
cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
ncmd = 0;
dirsize = 1;
dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
ndir = 0;
quit_flag = 0;
line = (char *) xmalloc (linesize);
line[0] = '\0';
instream = stdin;
getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
current_directory = gdb_dirbuf;
gdb_null = ui_file_new ();
gdb_stdout = stdio_fileopen (stdout);
gdb_stderr = stdio_fileopen (stderr);
gdb_stdlog = gdb_stderr;
gdb_stdtarg = gdb_stderr;
gdb_stdin = stdio_fileopen (stdin);
gdb_stdtargerr = gdb_stderr;
gdb_stdtargin = gdb_stdin;
#ifdef HAVE_PTHREAD_SETNAME_NP
pthread_setname_np ("gdb main thread");
#endif
#ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
gdb_sysroot = make_relative_prefix (argv[0], BINDIR, TARGET_SYSTEM_ROOT);
if (gdb_sysroot)
{
struct stat s;
int res = 0;
if (stat (gdb_sysroot, &s) == 0)
if (S_ISDIR (s.st_mode))
res = 1;
if (res == 0)
{
xfree (gdb_sysroot);
gdb_sysroot = TARGET_SYSTEM_ROOT;
}
}
else
gdb_sysroot = TARGET_SYSTEM_ROOT;
#else
#if defined (TARGET_SYSTEM_ROOT)
gdb_sysroot = TARGET_SYSTEM_ROOT;
#else
gdb_sysroot = "";
#endif
#endif
interpreter_p = xstrdup (context->interpreter_p);
{
int c;
enum {
OPT_SE = 10,
OPT_CD,
OPT_ANNOTATE,
OPT_STATISTICS,
OPT_TUI,
OPT_NOWINDOWS,
OPT_WINDOWS,
OPT_WAITFOR,
OPT_ARCH,
OPT_OSABI
};
static struct option long_options[] =
{
#if defined(TUI)
{"tui", no_argument, 0, OPT_TUI},
#endif
{"xdb", no_argument, &xdb_commands, 1},
{"dbx", no_argument, &dbx_commands, 1},
{"readnow", no_argument, &readnow_symbol_files, 1},
{"r", no_argument, &readnow_symbol_files, 1},
{"quiet", no_argument, &quiet, 1},
{"q", no_argument, &quiet, 1},
{"silent", no_argument, &quiet, 1},
{"nx", no_argument, &inhibit_gdbinit, 1},
{"n", no_argument, &inhibit_gdbinit, 1},
{"batch", no_argument, &batch, 1},
{"epoch", no_argument, &epoch_interface, 1},
{"fullname", no_argument, 0, 'f'},
{"f", no_argument, 0, 'f'},
{"annotate", required_argument, 0, OPT_ANNOTATE},
{"help", no_argument, &print_help, 1},
{"se", required_argument, 0, OPT_SE},
{"symbols", required_argument, 0, 's'},
{"s", required_argument, 0, 's'},
{"exec", required_argument, 0, 'e'},
{"e", required_argument, 0, 'e'},
{"core", required_argument, 0, 'c'},
{"c", required_argument, 0, 'c'},
{"pid", required_argument, 0, 'p'},
{"p", required_argument, 0, 'p'},
{"command", required_argument, 0, 'x'},
{"version", no_argument, &print_version, 1},
{"x", required_argument, 0, 'x'},
#ifdef GDBTK
{"tclcommand", required_argument, 0, 'z'},
{"enable-external-editor", no_argument, 0, 'y'},
{"editor-command", required_argument, 0, 'w'},
#endif
{"ui", required_argument, 0, 'i'},
{"interpreter", required_argument, 0, 'i'},
{"i", required_argument, 0, 'i'},
{"directory", required_argument, 0, 'd'},
{"d", required_argument, 0, 'd'},
{"cd", required_argument, 0, OPT_CD},
{"tty", required_argument, 0, 't'},
{"baud", required_argument, 0, 'b'},
{"b", required_argument, 0, 'b'},
{"nw", no_argument, NULL, OPT_NOWINDOWS},
{"nowindows", no_argument, NULL, OPT_NOWINDOWS},
{"w", no_argument, NULL, OPT_WINDOWS},
{"windows", no_argument, NULL, OPT_WINDOWS},
{"statistics", no_argument, 0, OPT_STATISTICS},
{"write", no_argument, &write_files, 1},
{"args", no_argument, &set_args, 1},
{"waitfor", required_argument, 0, OPT_WAITFOR},
{"arch", required_argument, 0, OPT_ARCH},
{"osabi", required_argument, 0, OPT_OSABI},
{"l", required_argument, 0, 'l'},
{0, no_argument, 0, 0}
};
while (1)
{
int option_index;
c = getopt_long_only (argc, argv, "",
long_options, &option_index);
if (c == EOF || set_args)
break;
if (c == 0 && long_options[option_index].flag == 0)
c = long_options[option_index].val;
switch (c)
{
case 0:
break;
case OPT_SE:
symarg = optarg;
execarg = optarg;
break;
case OPT_CD:
cdarg = optarg;
break;
case OPT_ANNOTATE:
annotation_level = atoi (optarg);
break;
case OPT_STATISTICS:
display_time = 1;
display_space = 1;
break;
case OPT_TUI:
xfree (interpreter_p);
interpreter_p = xstrdup ("tui");
break;
case OPT_WINDOWS:
use_windows = 1;
break;
case OPT_NOWINDOWS:
xfree (interpreter_p);
interpreter_p = xstrdup (INTERP_CONSOLE);
use_windows = 0;
break;
case OPT_WAITFOR:
attach_waitfor = (char *) xmalloc (12 + strlen (optarg));
sprintf (attach_waitfor, "-waitfor \"%s\"", optarg);
break;
case OPT_ARCH:
initial_arch = xstrdup (optarg);
break;
case OPT_OSABI:
osabiarg = optarg;
break;
case 'f':
annotation_level = 1;
use_windows = 0;
break;
case 's':
symarg = optarg;
break;
case 'e':
execarg = optarg;
break;
case 'c':
corearg = optarg;
break;
case 'p':
corearg = optarg;
break;
case 'x':
cmdarg[ncmd++] = optarg;
if (ncmd >= cmdsize)
{
cmdsize *= 2;
cmdarg = (char **) xrealloc ((char *) cmdarg,
cmdsize * sizeof (*cmdarg));
}
break;
#ifdef GDBTK
case 'z':
{
extern int gdbtk_test (char *);
if (!gdbtk_test (optarg))
{
fprintf_unfiltered (gdb_stderr, _("%s: unable to load tclcommand file \"%s\""),
argv[0], optarg);
exit (1);
}
break;
}
case 'y':
break;
case 'w':
{
external_editor_command = xstrdup (optarg);
break;
}
#endif
case 'i':
xfree (interpreter_p);
interpreter_p = xstrdup (optarg);
break;
case 'd':
dirarg[ndir++] = optarg;
if (ndir >= dirsize)
{
dirsize *= 2;
dirarg = (char **) xrealloc ((char *) dirarg,
dirsize * sizeof (*dirarg));
}
break;
case 't':
ttyarg = optarg;
break;
case 'q':
quiet = 1;
break;
case 'b':
{
int i;
char *p;
i = strtol (optarg, &p, 0);
if (i == 0 && p == optarg)
fprintf_unfiltered
(gdb_stderr,
_("warning: could not set baud rate to `%s'.\n"), optarg);
else
baud_rate = i;
}
break;
case 'l':
{
int i;
char *p;
i = strtol (optarg, &p, 0);
if (i == 0 && p == optarg)
fprintf_unfiltered
(gdb_stderr,
_("warning: could not set timeout limit to `%s'.\n"), optarg);
else
remote_timeout = i;
}
break;
case '?':
fprintf_unfiltered (gdb_stderr,
_("Use `%s --help' for a complete list of options.\n"),
argv[0]);
exit (1);
}
}
if (print_help || print_version)
{
use_windows = 0;
}
if (set_args)
{
if (optind >= argc)
{
fprintf_unfiltered (gdb_stderr,
_("%s: `--args' specified but no program specified\n"),
argv[0]);
exit (1);
}
symarg = argv[optind];
execarg = argv[optind];
++optind;
set_inferior_args_vector (argc - optind, &argv[optind]);
}
else
{
count = 0;
for (; optind < argc; optind++)
switch (++count)
{
case 1:
symarg = argv[optind];
execarg = argv[optind];
break;
case 2:
corearg = argv[optind];
break;
case 3:
fprintf_unfiltered (gdb_stderr,
_("Excess command line arguments ignored. (%s%s)\n"),
argv[optind], (optind == argc - 1) ? "" : " ...");
break;
}
}
if (batch)
quiet = 1;
}
gdb_init (argv[0]);
if (print_version)
{
print_gdb_version (gdb_stdout);
wrap_here ("");
printf_filtered ("\n");
exit (0);
}
if (print_help)
{
print_gdb_help (gdb_stdout);
fputs_unfiltered ("\n", gdb_stdout);
exit (0);
}
if (!quiet && strcmp (interpreter_p, INTERP_MI1) == 0)
{
print_gdb_version (gdb_stdout);
printf_filtered ("\n");
wrap_here ("");
gdb_flush (gdb_stdout);
}
if (state_change_hook)
{
state_change_hook (STATE_ACTIVE);
}
{
struct interp *interp = interp_lookup (interpreter_p);
if (interp == NULL)
error (_("Interpreter `%s' unrecognized"), interpreter_p);
if (interp_set (interp) == NULL)
{
fprintf_unfiltered (gdb_stderr,
"Interpreter `%s' failed to initialize.\n",
interpreter_p);
exit (1);
}
}
if (!quiet && !current_interp_named_p (INTERP_MI1))
{
print_gdb_version (gdb_stdout);
if (symarg)
printf_filtered ("..");
wrap_here ("");
gdb_flush (gdb_stdout);
}
error_pre_print = "\n\n";
quit_pre_print = error_pre_print;
warning_pre_print = _("\nwarning: ");
memset (&homebuf, 0, sizeof (struct stat));
memset (&cwdbuf, 0, sizeof (struct stat));
memset (&globalbuf, 0, sizeof (struct stat));
stat (gdbinit_global, &globalbuf);
if (!inhibit_gdbinit)
{
source_file (gdbinit_global, 0);
}
do_cleanups (ALL_CLEANUPS);
set_internalvar (lookup_internalvar ("_Xcode"),
value_from_longest (builtin_type_int, (LONGEST) 0));
homedir = getenv ("HOME");
if (homedir)
{
char *homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
stat (homeinit, &homebuf);
if (!inhibit_gdbinit)
if ((globalbuf.st_dev != homebuf.st_dev) || (globalbuf.st_ino != homebuf.st_ino))
{
catch_command_errors (source_file, homeinit, 0, RETURN_MASK_ALL);
}
}
if (cdarg != NULL)
{
catch_command_errors (cd_command, cdarg, 0, RETURN_MASK_ALL);
}
for (i = 0; i < ndir; i++)
catch_command_errors (directory_command, dirarg[i], 0, RETURN_MASK_ALL);
xfree (dirarg);
#if defined (USE_POSIX_SPAWN) || defined (USE_ARCH_FOR_EXEC)
if (initial_arch != NULL)
{
char *arch_string = NULL;
char *osabi_string;
#if defined (TARGET_POWERPC)
if (strcmp (initial_arch, "ppc") == 0)
{
arch_string = "powerpc:common";
osabi_string = "Darwin";
}
else if (strcmp (initial_arch, "ppc64") == 0)
{
arch_string = "powerpc:common64";
osabi_string = "Darwin64";
}
else
warning ("invalid argument \"%s\" for \"--arch\", should be one of "
"\"ppc\" or \"ppc64\"\n", initial_arch);
#elif defined (TARGET_I386)
if (strcmp (initial_arch, "i386") == 0)
{
arch_string = "i386";
osabi_string = "Darwin";
}
else if (strcmp (initial_arch, "x86_64") == 0)
{
arch_string = "i386:x86-64";
osabi_string = "Darwin64";
}
else
warning ("invalid argument \"%s\" for \"--arch\", should be one of "
"\"i386\" or \"x86_64\"\n", initial_arch);
#elif defined (TARGET_ARM)
if (strcmp (initial_arch, "arm") == 0)
{
arch_string = "arm";
osabi_string = "Darwin";
}
else if (strcmp (initial_arch, "armv6") == 0)
{
arch_string = "armv6";
osabi_string = "DarwinV6";
}
else
warning ("invalid argument \"%s\" for \"--arch\", should be one of "
"\"armv\" or \"armv6\"\n", initial_arch);
#endif
if (arch_string != NULL)
{
set_architecture_from_string (arch_string);
set_osabi_from_string (osabi_string);
}
}
#else
warning ("--arch option not supported in this gdb.");
#endif
if (osabiarg != NULL)
set_osabi_option (osabiarg);
if (execarg != NULL
&& symarg != NULL
&& strcmp (execarg, symarg) == 0)
{
if (catch_command_errors (exec_file_attach, execarg, !batch, RETURN_MASK_ALL))
catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL);
}
else
{
if (execarg != NULL)
catch_command_errors (exec_file_attach, execarg, !batch, RETURN_MASK_ALL);
if (symarg != NULL)
catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL);
}
if (state_change_hook && symarg != NULL)
{
state_change_hook (STATE_INFERIOR_LOADED);
}
if (attach_waitfor != NULL)
{
printf_filtered ("\n");
catch_command_errors (attach_command, attach_waitfor, 0, RETURN_MASK_ALL);
}
if (!quiet)
printf_filtered ("\n");
error_pre_print = "\n";
quit_pre_print = error_pre_print;
warning_pre_print = _("\nwarning: ");
if (corearg != NULL)
{
if (catch_command_errors (core_file_attach, corearg, !batch, RETURN_MASK_ALL) == 0)
{
if (corearg[0] == '"')
{
int len = strlen (corearg);
if (corearg[len - 1] == '"')
{
corearg[len - 1] = '\0';
corearg += 1;
}
}
if (isdigit (corearg[0]))
catch_command_errors (attach_command, corearg, !batch, RETURN_MASK_ALL);
}
}
if (ttyarg != NULL)
catch_command_errors (tty_command, ttyarg, !batch, RETURN_MASK_ALL);
error_pre_print = NULL;
quit_pre_print = NULL;
warning_pre_print = _("warning: ");
stat (gdbinit, &cwdbuf);
if (!inhibit_gdbinit)
if (((globalbuf.st_dev != cwdbuf.st_dev) || (globalbuf.st_ino != cwdbuf.st_ino))
&& ((homebuf.st_dev != cwdbuf.st_dev) || (homebuf.st_ino != cwdbuf.st_ino)))
{
if (cwdbuf.st_uid == getuid ())
catch_command_errors (source_file, gdbinit, 0, RETURN_MASK_ALL);
}
set_internalvar (lookup_internalvar ("input_radix"),
value_from_longest (builtin_type_int, (LONGEST) input_radix));
set_internalvar (lookup_internalvar ("output_radix"),
value_from_longest (builtin_type_int, (LONGEST) output_radix));
for (i = 0; i < ncmd; i++)
{
#if 0
if (!SET_TOP_LEVEL ())
{
#if 0
if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
read_command_file (stdin);
else
#endif
source_file (cmdarg[i], !batch);
do_cleanups (ALL_CLEANUPS);
}
#endif
catch_command_errors (source_file, cmdarg[i], !batch, RETURN_MASK_ALL);
}
xfree (cmdarg);
init_history ();
if (batch)
{
if (attach_flag)
target_detach (NULL, 0);
exit (0);
}
#ifdef BEFORE_MAIN_LOOP_HOOK
BEFORE_MAIN_LOOP_HOOK;
#endif
if (display_time)
{
long init_time = get_run_time () - time_at_startup;
printf_unfiltered (_("Startup time: %ld.%06ld\n"),
init_time / 1000000, init_time % 1000000);
}
if (display_space)
{
#ifdef HAVE_SBRK
extern char **environ;
char *lim = (char *) sbrk (0);
printf_unfiltered (_("Startup size: data size %ld\n"),
(long) (lim - (char *) &environ));
#endif
}
#if 0
while (1)
{
if (!SET_TOP_LEVEL ())
{
do_cleanups (ALL_CLEANUPS);
if (deprecated_command_loop_hook)
deprecated_command_loop_hook ();
else
deprecated_command_loop ();
quit_command ((char *) 0, instream == stdin);
}
}
#endif
while (1)
{
catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
}
}
int
gdb_main (struct captured_main_args *args)
{
use_windows = args->use_windows;
catch_errors (captured_main, args, "", RETURN_MASK_ALL);
return 1;
}
static void
print_gdb_help (struct ui_file *stream)
{
fputs_unfiltered (_("\
This is the GNU debugger. Usage:\n\n\
gdb [options] [executable-file [core-file or process-id]]\n\
gdb [options] --args executable-file [inferior-arguments ...]\n\n\
Options:\n\n\
"), stream);
fputs_unfiltered (_("\
--args Arguments after executable-file are passed to inferior\n\
"), stream);
fputs_unfiltered (_("\
-b BAUDRATE Set serial port baud rate used for remote debugging.\n\
--batch Exit after processing options.\n\
--cd=DIR Change current directory to DIR.\n\
--command=FILE Execute GDB commands from FILE.\n\
--core=COREFILE Analyze the core dump COREFILE.\n\
--pid=PID Attach to running process PID.\n\
"), stream);
fputs_unfiltered (_("\
--dbx DBX compatibility mode.\n\
--directory=DIR Search for source files in DIR.\n\
--epoch Output information used by epoch emacs-GDB interface.\n\
--exec=EXECFILE Use EXECFILE as the executable.\n\
--fullname Output information used by emacs-GDB interface.\n\
--help Print this message.\n\
"), stream);
fputs_unfiltered (_("\
--interpreter=INTERP\n\
Select a specific interpreter / user interface\n\
"), stream);
fputs_unfiltered (_("\
-l TIMEOUT Set timeout in seconds for remote debugging.\n\
--mapped Use mapped symbol files if supported on this system.\n\
--nw Do not use a window interface.\n\
--nx Do not read "), stream);
fputs_unfiltered (gdbinit, stream);
fputs_unfiltered (_(" file.\n\
--quiet Do not print version number on startup.\n\
--readnow Fully read symbol files on first access.\n\
"), stream);
fputs_unfiltered (_("\
--se=FILE Use FILE as symbol file and executable file.\n\
--symbols=SYMFILE Read symbols from SYMFILE.\n\
--tty=TTY Use TTY for input/output by the program being debugged.\n\
"), stream);
#if defined(TUI)
fputs_unfiltered (_("\
--tui Use a terminal user interface.\n\
"), stream);
#endif
fputs_unfiltered (_("\
--version Print version information and then exit.\n\
-w Use a window interface.\n\
--write Set writing into executable and core files.\n\
--xdb XDB compatibility mode.\n\
--waitfor=PROCNAME Poll continuously for PROCNAME to launch; attach to it.\n\
--arch=ARCH Run the slice of a Universal file given by ARCH.\n\
--osabi=OSABI Set the osabi prior to loading any executables.\n\
"), stream);
fputs_unfiltered (_("\n\
For more information, type \"help\" from within GDB, or consult the\n\
GDB manual (available as on-line info or a printed manual).\n\
Report bugs to \"bug-gdb@gnu.org\".\
"), stream);
}
#if 0
#include "cli/cli-cmds.h"
#include "cli/cli-decode.h"
struct cmd_list_element *
add_set_cmd (char *name,
enum command_class class,
var_types var_type,
void *var,
char *doc,
struct cmd_list_element **list)
{
return deprecated_add_set_cmd (name, class, var_type, var, doc, list);
}
#endif