/* GDB routines for supporting auto-loaded scripts.
- Copyright (C) 2012-2019 Free Software Foundation, Inc.
+ Copyright (C) 2012-2021 Free Software Foundation, Inc.
This file is part of GDB.
#include "gdb/section-scripts.h"
#include <algorithm>
#include "gdbsupport/pathstuff.h"
+#include "cli/cli-style.h"
/* The section to look in for auto-loaded scripts (in file formats that
support sections).
const char *section_name, unsigned offset);
/* Value of the 'set debug auto-load' configuration variable. */
-static int debug_auto_load = 0;
+static bool debug_auto_load = false;
/* "show" command for the debug_auto_load configuration variable. */
set auto-load gdb-scripts on|off
This is true if we should auto-load associated scripts when an objfile
is opened, false otherwise. */
-static int auto_load_gdb_scripts = 1;
+static bool auto_load_gdb_scripts = true;
/* "show" command for the auto_load_gdb_scripts configuration variable. */
value);
}
-/* Return non-zero if auto-loading gdb scripts is enabled. */
+/* See auto-load.h. */
-int
+bool
auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang)
{
return auto_load_gdb_scripts;
This flag exists to facilitate deferring auto-loading during start-up
until after ./.gdbinit has been read; it may augment the search directories
used to find the scripts. */
-int global_auto_load = 1;
+bool global_auto_load = true;
/* Auto-load .gdbinit file from the current directory? */
-int auto_load_local_gdbinit = 1;
+bool auto_load_local_gdbinit = true;
/* Absolute pathname to the current directory .gdbinit, if it exists. */
char *auto_load_local_gdbinit_pathname = NULL;
-/* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
-int auto_load_local_gdbinit_loaded = 0;
+/* if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
+bool auto_load_local_gdbinit_loaded = false;
/* "show" command for the auto_load_local_gdbinit configuration variable. */
for (;;)
{
/* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
- same way so they will match. */
+ same way so they will match. */
while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
filename_len--;
filename[filename_len] = '\0';
return 0;
}
-/* Return 1 if FILENAME is located in one of the directories of
- AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
- not have to be an absolute path.
+/* See auto-load.h. */
- Existence of FILENAME is not checked. Function will still give a warning
- even if the caller would quietly skip non-existing file in unsafe
- directory. */
-
-int
+bool
file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
{
gdb::unique_xmalloc_ptr<char> filename_real;
- static int advice_printed = 0;
+ static bool advice_printed = false;
if (debug_auto_load)
{
}
if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
- return 1;
+ return true;
auto_load_safe_path_vec_update ();
if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
- return 1;
+ return true;
- warning (_("File \"%s\" auto-loading has been declined by your "
+ warning (_("File \"%ps\" auto-loading has been declined by your "
"`auto-load safe-path' set to \"%s\"."),
- filename_real.get (), auto_load_safe_path);
+ styled_string (file_name_style.style (), filename_real.get ()),
+ auto_load_safe_path);
if (!advice_printed)
{
- const char *homedir = getenv ("HOME");
-
- if (homedir == NULL)
- homedir = "$HOME";
- std::string homeinit = string_printf ("%s/%s", homedir, GDBINIT);
+ /* Find the existing home directory config file. */
+ struct stat buf;
+ std::string home_config = find_gdb_home_config_file (GDBINIT, &buf);
+ if (home_config.empty ())
+ {
+ /* The user doesn't have an existing home directory config file,
+ so we should suggest a suitable path for them to use. */
+ std::string config_dir_file
+ = get_standard_config_filename (GDBINIT);
+ if (!config_dir_file.empty ())
+ home_config = config_dir_file;
+ else
+ {
+ const char *homedir = getenv ("HOME");
+ if (homedir == nullptr)
+ homedir = "$HOME";
+ home_config = (std::string (homedir) + SLASH_STRING
+ + std::string (GDBINIT));
+ }
+ }
printf_filtered (_("\
To enable execution of this file add\n\
\"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
\tinfo \"(gdb)Auto-loading safe path\"\n"),
filename_real.get (),
- homeinit.c_str (), homeinit.c_str ());
- advice_printed = 1;
+ home_config.c_str (), home_config.c_str ());
+ advice_printed = true;
}
- return 0;
+ return false;
}
/* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
struct auto_load_pspace_info
{
- auto_load_pspace_info () = default;
- ~auto_load_pspace_info ();
-
/* For each program space we keep track of loaded scripts, both when
specified as file names and as scripts to be executed directly. */
- struct htab *loaded_script_files = nullptr;
- struct htab *loaded_script_texts = nullptr;
+ htab_up loaded_script_files;
+ htab_up loaded_script_texts;
/* Non-zero if we've issued the warning about an auto-load script not being
supported. We only want to issue this warning once. */
static const struct program_space_key<struct auto_load_pspace_info>
auto_load_pspace_data;
-auto_load_pspace_info::~auto_load_pspace_info ()
-{
- if (loaded_script_files)
- htab_delete (loaded_script_files);
- if (loaded_script_texts)
- htab_delete (loaded_script_texts);
-}
-
/* Get the current autoload data. If none is found yet, add it now. This
function always returns a valid object. */
Space for each entry is obtained with one malloc so we can free them
easily. */
- pspace_info->loaded_script_files = htab_create (31,
- hash_loaded_script_entry,
- eq_loaded_script_entry,
- xfree);
- pspace_info->loaded_script_texts = htab_create (31,
- hash_loaded_script_entry,
- eq_loaded_script_entry,
- xfree);
+ pspace_info->loaded_script_files.reset
+ (htab_create (31,
+ hash_loaded_script_entry,
+ eq_loaded_script_entry,
+ xfree));
+ pspace_info->loaded_script_texts.reset
+ (htab_create (31,
+ hash_loaded_script_entry,
+ eq_loaded_script_entry,
+ xfree));
pspace_info->unsupported_script_warning_printed = false;
pspace_info->script_not_found_warning_printed = false;
const char *name, const char *full_path,
const struct extension_language_defn *language)
{
- struct htab *htab = pspace_info->loaded_script_files;
+ struct htab *htab = pspace_info->loaded_script_files.get ();
struct loaded_script **slot, entry;
int in_hash_table;
int loaded, const char *name,
const struct extension_language_defn *language)
{
- struct htab *htab = pspace_info->loaded_script_texts;
+ struct htab *htab = pspace_info->loaded_script_texts.get ();
struct loaded_script **slot, entry;
int in_hash_table;
"scripts-directory' path \"%s\".\n"),
auto_load_dir);
+ /* Convert Windows file name from c:/dir/file to /c/dir/file. */
+ if (HAS_DRIVE_SPEC (debugfile))
+ filename = (std::string("\\") + debugfile[0]
+ + STRIP_DRIVE_SPEC (debugfile));
+
for (const gdb::unique_xmalloc_ptr<char> &dir : vec)
{
/* FILENAME is absolute, so we don't need a "/" here. */
/* We don't throw an error, the program is still debuggable. */
warning (_("\
Missing/bad script name in entry at offset %u in section %s\n\
-of file %s."),
- offset, section_name, objfile_name (objfile));
+of file %ps."),
+ offset, section_name,
+ styled_string (file_name_style.style (),
+ objfile_name (objfile)));
return;
}
script_text = newline + 1;
scripts_sect = bfd_get_section_by_name (abfd, section_name);
if (scripts_sect == NULL
- || (bfd_get_section_flags (abfd, scripts_sect) & SEC_HAS_CONTENTS) == 0)
+ || (bfd_section_flags (scripts_sect) & SEC_HAS_CONTENTS) == 0)
return;
if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
- warning (_("Couldn't read %s section of %s"),
- section_name, bfd_get_filename (abfd));
+ warning (_("Couldn't read %s section of %ps"),
+ section_name,
+ styled_string (file_name_style.style (),
+ bfd_get_filename (abfd)));
else
{
gdb::unique_xmalloc_ptr<bfd_byte> data_holder (data);
char *p = (char *) data;
source_section_scripts (objfile, section_name, p,
- p + bfd_get_section_size (scripts_sect));
+ p + bfd_section_size (scripts_sect));
}
}
collect_matching_scripts_data data (&script_files, language);
/* Pass a pointer to scripts as VEC_safe_push can realloc space. */
- htab_traverse_noresize (pspace_info->loaded_script_files,
+ htab_traverse_noresize (pspace_info->loaded_script_files.get (),
collect_matching_scripts, &data);
std::sort (script_files.begin (), script_files.end (),
collect_matching_scripts_data data (&script_texts, language);
/* Pass a pointer to scripts as VEC_safe_push can realloc space. */
- htab_traverse_noresize (pspace_info->loaded_script_texts,
+ htab_traverse_noresize (pspace_info->loaded_script_texts.get (),
collect_matching_scripts, &data);
std::sort (script_texts.begin (), script_texts.end (),
if (auto_load_local_gdbinit_pathname == NULL)
printf_filtered (_("Local .gdbinit file was not found.\n"));
else if (auto_load_local_gdbinit_loaded)
- printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
- auto_load_local_gdbinit_pathname);
+ printf_filtered (_("Local .gdbinit file \"%ps\" has been loaded.\n"),
+ styled_string (file_name_style.style (),
+ auto_load_local_gdbinit_pathname));
else
- printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
- auto_load_local_gdbinit_pathname);
+ printf_filtered (_("Local .gdbinit file \"%ps\" has not been loaded.\n"),
+ styled_string (file_name_style.style (),
+ auto_load_local_gdbinit_pathname));
}
/* Print an "unsupported script" warning if it has not already been printed.
{
warning (_("\
Unsupported auto-load script at offset %u in section %s\n\
-of file %s.\n\
+of file %ps.\n\
Use `info auto-load %s-scripts [REGEXP]' to list them."),
- offset, section_name, objfile_name (objfile),
+ offset, section_name,
+ styled_string (file_name_style.style (),
+ objfile_name (objfile)),
ext_lang_name (language));
pspace_info->unsupported_script_warning_printed = true;
}
{
warning (_("\
Missing auto-load script at offset %u in section %s\n\
-of file %s.\n\
+of file %ps.\n\
Use `info auto-load %s-scripts [REGEXP]' to list them."),
- offset, section_name, objfile_name (objfile),
+ offset, section_name,
+ styled_string (file_name_style.style (),
+ objfile_name (objfile)),
ext_lang_name (language));
pspace_info->script_not_found_warning_printed = true;
}
return &retval;
}
-/* Command "show auto-load" displays summary of all the current
- "show auto-load " settings. */
-
-static void
-show_auto_load_cmd (const char *args, int from_tty)
-{
- cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
-}
-
/* Initialize "show auto-load " commands prefix and return it. */
struct cmd_list_element **
static struct cmd_list_element *retval;
if (retval == NULL)
- add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
+ add_show_prefix_cmd ("auto-load", class_maintenance, _("\
Show auto-loading specific settings.\n\
Show configuration of various auto-load-specific variables such as\n\
automatic loading of Python scripts."),
- &retval, "show auto-load ",
- 0/*allow-unknown*/, &showlist);
+ &retval, "show auto-load ",
+ 0/*allow-unknown*/, &showlist);
return &retval;
}
return &retval;
}
+void _initialize_auto_load ();
void
-_initialize_auto_load (void)
+_initialize_auto_load ()
{
struct cmd_list_element *cmd;
char *scripts_directory_help, *gdb_name_help, *python_name_help;