gdb: bool-ify ext_lang_auto_load_enabled and friends
[deliverable/binutils-gdb.git] / gdb / auto-load.c
index 115d5c10e821eb6b33521b7b365c9eb4cf03a2af..0d5532b4882e4789de39a92a2cb8f990b24e6550 100644 (file)
@@ -1,6 +1,6 @@
 /* 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.
 
@@ -41,6 +41,7 @@
 #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).
@@ -61,7 +62,7 @@ static void maybe_print_script_not_found_warning
    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.  */
 
@@ -79,7 +80,7 @@ show_debug_auto_load (struct ui_file *file, int from_tty,
    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.  */
 
@@ -92,9 +93,9 @@ show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
                    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;
@@ -110,16 +111,16 @@ auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang)
    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.  */
 
@@ -363,7 +364,7 @@ filename_is_in_pattern_1 (char *filename, char *pattern)
   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';
@@ -460,19 +461,13 @@ filename_is_in_auto_load_safe_path_vec (const char *filename,
   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)
     {
@@ -484,23 +479,39 @@ file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
     }
 
   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\
@@ -513,11 +524,11 @@ For more information about this security protection see the\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
@@ -527,13 +538,10 @@ For more information about this security protection see the\n\
 
 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.  */
@@ -565,14 +573,6 @@ struct loaded_script
 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.  */
 
@@ -619,14 +619,16 @@ init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
      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;
@@ -658,7 +660,7 @@ maybe_add_script_file (struct auto_load_pspace_info *pspace_info, int loaded,
                       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;
 
@@ -706,7 +708,7 @@ maybe_add_script_text (struct auto_load_pspace_info *pspace_info,
                       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;
 
@@ -782,6 +784,11 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
                                          "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.  */
@@ -989,8 +996,10 @@ execute_script_contents (struct auto_load_pspace_info *pspace_info,
       /* 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;
@@ -1116,19 +1125,21 @@ auto_load_section_scripts (struct objfile *objfile, const char *section_name)
 
   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));
     }
 }
 
@@ -1286,7 +1297,7 @@ auto_load_info_scripts (const char *pattern, int from_tty,
       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 (),
@@ -1298,7 +1309,7 @@ auto_load_info_scripts (const char *pattern, int from_tty,
       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 (),
@@ -1349,11 +1360,13 @@ info_auto_load_local_gdbinit (const char *args, int from_tty)
   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.
@@ -1370,9 +1383,11 @@ maybe_print_unsupported_script_warning
     {
       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;
     }
@@ -1392,9 +1407,11 @@ maybe_print_script_not_found_warning
     {
       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;
     }
@@ -1448,15 +1465,6 @@ automatic loading of Python scripts."),
   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 **
@@ -1465,12 +1473,12 @@ auto_load_show_cmdlist_get (void)
   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;
 }
@@ -1518,8 +1526,9 @@ found and/or loaded."),
   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;
This page took 0.03262 seconds and 4 git commands to generate.