From 16e7150e45f1220e9aafb85a80b967b43f93d75b Mon Sep 17 00:00:00 2001 From: Jerome Guitton Date: Wed, 28 Jan 2009 15:01:00 +0000 Subject: 2009-01-28 Daniel Jacobowitz Jerome Guitton * configure, config.in: Regenerated. * configure.ac: Add --with-system-gdbinit. * main.c (get_init_files): New. (captured_main): Use get_init_files. Load system gdbinit before $HOME/.gdbinit. (print_gdb_help): Print location of init files. --- gdb/main.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 119 insertions(+), 31 deletions(-) (limited to 'gdb/main.c') diff --git a/gdb/main.c b/gdb/main.c index 0eb9596..5d4640b 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -83,6 +83,9 @@ int return_child_result_value = -1; /* Whether to enable writing into executable and core files */ extern int write_files; +/* GDB as it has been invoked from the command line (i.e. argv[0]). */ +static char *gdb_program_name; + static void print_gdb_help (struct ui_file *); /* These two are used to set the external editor commands when gdb is farming @@ -90,6 +93,82 @@ static void print_gdb_help (struct ui_file *); extern char *external_editor_command; +/* Compute the locations of init files that GDB should source and return + them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT. If there is + no system gdbinit (resp. home gdbinit and local gdbinit) to be loaded, + then SYSTEM_GDBINIT (resp. HOME_GDBINIT and LOCAL_GDBINIT) is set to + NULL. */ +static void +get_init_files (char **system_gdbinit, + char **home_gdbinit, + char **local_gdbinit) +{ + static char *sysgdbinit = NULL; + static char *homeinit = NULL; + static char *localinit = NULL; + static int initialized = 0; + + if (!initialized) + { + struct stat homebuf, cwdbuf, s; + char *homedir, *relocated_sysgdbinit; + + sysgdbinit = SYSTEM_GDBINIT; + if (!sysgdbinit [0] || stat (sysgdbinit, &s) != 0) + sysgdbinit = NULL; + +#ifdef SYSTEM_GDBINIT_RELOCATABLE + relocated_sysgdbinit = make_relative_prefix (gdb_program_name, BINDIR, + SYSTEM_GDBINIT); + if (relocated_sysgdbinit) + { + struct stat s; + int res = 0; + + if (stat (relocated_sysgdbinit, &s) == 0) + sysgdbinit = relocated_sysgdbinit; + else + xfree (relocated_sysgdbinit); + } +#endif + + homedir = getenv ("HOME"); + + /* If the .gdbinit file in the current directory is the same as + the $HOME/.gdbinit file, it should not be sourced. homebuf + and cwdbuf are used in that purpose. Make sure that the stats + are zero in case one of them fails (this guarantees that they + won't match if either exists). */ + + memset (&homebuf, 0, sizeof (struct stat)); + memset (&cwdbuf, 0, sizeof (struct stat)); + + if (homedir) + { + homeinit = xstrprintf ("%s/%s", homedir, gdbinit); + if (stat (homeinit, &homebuf) != 0) + { + xfree (homeinit); + homeinit = NULL; + } + } + + if (stat (gdbinit, &cwdbuf) == 0) + { + if (!homeinit + || memcmp ((char *) &homebuf, (char *) &cwdbuf, + sizeof (struct stat))) + localinit = gdbinit; + } + + initialized = 1; + } + + *system_gdbinit = sysgdbinit; + *home_gdbinit = homeinit; + *local_gdbinit = localinit; +} + /* Call command_loop. If it happens to return, pass that through as a non-zero return status. */ @@ -156,8 +235,10 @@ captured_main (void *data) /* Number of elements used. */ int ndir; - struct stat homebuf, cwdbuf; - char *homedir; + /* gdb init files. */ + char *system_gdbinit; + char *home_gdbinit; + char *local_gdbinit; int i; @@ -196,6 +277,8 @@ captured_main (void *data) gdb_stdtargerr = gdb_stderr; /* for moment */ gdb_stdtargin = gdb_stdin; /* for moment */ + gdb_program_name = xstrdup (argv[0]); + if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf))) /* Don't use *_filtered or warning() (which relies on current_target) until after initialize_all_files(). */ @@ -274,6 +357,8 @@ captured_main (void *data) } } + get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit); + /* There will always be an interpreter. Either the one passed into this captured main, or one specified by the user at start up, or the console. Initialize the interpreter to the one requested by @@ -685,33 +770,20 @@ Excess command line arguments ignored. (%s%s)\n"), quit_pre_print = error_pre_print; warning_pre_print = _("\nwarning: "); + /* Read and execute the system-wide gdbinit file, if it exists. + This is done *before* all the command line arguments are + processed; it sets global parameters, which are independent of + what file you are debugging or what directory you are in. */ + if (system_gdbinit && !inhibit_gdbinit) + catch_command_errors (source_script, system_gdbinit, 0, RETURN_MASK_ALL); + /* Read and execute $HOME/.gdbinit file, if it exists. This is done *before* all the command line arguments are processed; it sets global parameters, which are independent of what file you are debugging or what directory you are in. */ - homedir = getenv ("HOME"); - if (homedir) - { - char *homeinit = xstrprintf ("%s/%s", homedir, gdbinit); - if (!inhibit_gdbinit) - { - catch_command_errors (source_script, homeinit, 0, RETURN_MASK_ALL); - } - - /* Do stats; no need to do them elsewhere since we'll only - need them if homedir is set. Make sure that they are - zero in case one of them fails (this guarantees that they - won't match if either exists). */ - - memset (&homebuf, 0, sizeof (struct stat)); - memset (&cwdbuf, 0, sizeof (struct stat)); - - stat (homeinit, &homebuf); - stat (gdbinit, &cwdbuf); /* We'll only need this if - homedir was set. */ - xfree (homeinit); - } + if (home_gdbinit && !inhibit_gdbinit) + catch_command_errors (source_script, home_gdbinit, 0, RETURN_MASK_ALL); /* Now perform all the actions indicated by the arguments. */ if (cdarg != NULL) @@ -779,13 +851,8 @@ Can't attach to process and specify a core file at the same time.")); /* Read the .gdbinit file in the current directory, *if* it isn't the same as the $HOME/.gdbinit file (it should exist, also). */ - - if (!homedir - || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat))) - if (!inhibit_gdbinit) - { - catch_command_errors (source_script, gdbinit, 0, RETURN_MASK_ALL); - } + if (local_gdbinit && !inhibit_gdbinit) + catch_command_errors (source_script, local_gdbinit, 0, RETURN_MASK_ALL); for (i = 0; i < ncmd; i++) { @@ -857,6 +924,12 @@ gdb_main (struct captured_main_args *args) static void print_gdb_help (struct ui_file *stream) { + char *system_gdbinit; + char *home_gdbinit; + char *local_gdbinit; + + get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit); + fputs_unfiltered (_("\ This is the GNU debugger. Usage:\n\n\ gdb [options] [executable-file [core-file or process-id]]\n\ @@ -919,6 +992,21 @@ Options:\n\n\ --xdb XDB compatibility mode.\n\ "), stream); fputs_unfiltered (_("\n\ +At startup, GDB reads the following init files and executes their commands:\n\ +"), stream); + if (system_gdbinit) + fprintf_unfiltered (stream, _("\ + * system-wide init file: %s\n\ +"), system_gdbinit); + if (home_gdbinit) + fprintf_unfiltered (stream, _("\ + * user-specific init file: %s\n\ +"), home_gdbinit); + if (local_gdbinit) + fprintf_unfiltered (stream, _("\ + * local init file: ./%s\n\ +"), local_gdbinit); + 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\ "), stream); -- cgit v1.1