gdb/auto-load.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* GDB routines for supporting auto-loaded scripts.

  2.    Copyright (C) 2012-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "auto-load.h"
  16. #include "progspace.h"
  17. #include "gdb_regex.h"
  18. #include "ui-out.h"
  19. #include "filenames.h"
  20. #include "command.h"
  21. #include "observer.h"
  22. #include "objfiles.h"
  23. #include "cli/cli-script.h"
  24. #include "gdbcmd.h"
  25. #include "cli/cli-cmds.h"
  26. #include "cli/cli-decode.h"
  27. #include "cli/cli-setshow.h"
  28. #include "gdb_vecs.h"
  29. #include "readline/tilde.h"
  30. #include "completer.h"
  31. #include "fnmatch.h"
  32. #include "top.h"
  33. #include "filestuff.h"
  34. #include "extension.h"
  35. #include "gdb/section-scripts.h"

  36. /* The section to look in for auto-loaded scripts (in file formats that
  37.    support sections).
  38.    Each entry in this section is a record that begins with a leading byte
  39.    identifying the record type.
  40.    At the moment we only support one record type: A leading byte of 1,
  41.    followed by the path of a python script to load.  */
  42. #define AUTO_SECTION_NAME ".debug_gdb_scripts"

  43. static int maybe_add_script (struct auto_load_pspace_info *pspace_info,
  44.                              int loaded, const char *name,
  45.                              const char *full_path,
  46.                              const struct extension_language_defn *language);

  47. static int unsupported_script_warning_print (struct auto_load_pspace_info *);

  48. static int script_not_found_warning_print (struct auto_load_pspace_info *);

  49. /* Value of the 'set debug auto-load' configuration variable.  */
  50. static int debug_auto_load = 0;

  51. /* "show" command for the debug_auto_load configuration variable.  */

  52. static void
  53. show_debug_auto_load (struct ui_file *file, int from_tty,
  54.                       struct cmd_list_element *c, const char *value)
  55. {
  56.   fprintf_filtered (file, _("Debugging output for files "
  57.                             "of 'set auto-load ...' is %s.\n"),
  58.                     value);
  59. }

  60. /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
  61.    scripts:
  62.    set auto-load gdb-scripts on|off
  63.    This is true if we should auto-load associated scripts when an objfile
  64.    is opened, false otherwise.  */
  65. static int auto_load_gdb_scripts = 1;

  66. /* "show" command for the auto_load_gdb_scripts configuration variable.  */

  67. static void
  68. show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
  69.                             struct cmd_list_element *c, const char *value)
  70. {
  71.   fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
  72.                             "scripts is %s.\n"),
  73.                     value);
  74. }

  75. /* Return non-zero if auto-loading gdb scripts is enabled.  */

  76. int
  77. auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang)
  78. {
  79.   return auto_load_gdb_scripts;
  80. }

  81. /* Internal-use flag to enable/disable auto-loading.
  82.    This is true if we should auto-load python code when an objfile is opened,
  83.    false otherwise.

  84.    Both auto_load_scripts && global_auto_load must be true to enable
  85.    auto-loading.

  86.    This flag exists to facilitate deferring auto-loading during start-up
  87.    until after ./.gdbinit has been read; it may augment the search directories
  88.    used to find the scripts.  */
  89. int global_auto_load = 1;

  90. /* Auto-load .gdbinit file from the current directory?  */
  91. int auto_load_local_gdbinit = 1;

  92. /* Absolute pathname to the current directory .gdbinit, if it exists.  */
  93. char *auto_load_local_gdbinit_pathname = NULL;

  94. /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded.  */
  95. int auto_load_local_gdbinit_loaded = 0;

  96. /* "show" command for the auto_load_local_gdbinit configuration variable.  */

  97. static void
  98. show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
  99.                               struct cmd_list_element *c, const char *value)
  100. {
  101.   fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
  102.                             "directory is %s.\n"),
  103.                     value);
  104. }

  105. /* Directory list from which to load auto-loaded scripts.  It is not checked
  106.    for absolute paths but they are strongly recommended.  It is initialized by
  107.    _initialize_auto_load.  */
  108. static char *auto_load_dir;

  109. /* "set" command for the auto_load_dir configuration variable.  */

  110. static void
  111. set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c)
  112. {
  113.   /* Setting the variable to "" resets it to the compile time defaults.  */
  114.   if (auto_load_dir[0] == '\0')
  115.     {
  116.       xfree (auto_load_dir);
  117.       auto_load_dir = xstrdup (AUTO_LOAD_DIR);
  118.     }
  119. }

  120. /* "show" command for the auto_load_dir configuration variable.  */

  121. static void
  122. show_auto_load_dir (struct ui_file *file, int from_tty,
  123.                     struct cmd_list_element *c, const char *value)
  124. {
  125.   fprintf_filtered (file, _("List of directories from which to load "
  126.                             "auto-loaded scripts is %s.\n"),
  127.                     value);
  128. }

  129. /* Directory list safe to hold auto-loaded files.  It is not checked for
  130.    absolute paths but they are strongly recommended.  It is initialized by
  131.    _initialize_auto_load.  */
  132. static char *auto_load_safe_path;

  133. /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
  134.    by tilde_expand and possibly each entries has added its gdb_realpath
  135.    counterpart.  */
  136. static VEC (char_ptr) *auto_load_safe_path_vec;

  137. /* Expand $datadir and $debugdir in STRING according to the rules of
  138.    substitute_path_component.  Return vector from dirnames_to_char_ptr_vec,
  139.    this vector must be freed by free_char_ptr_vec by the caller.  */

  140. static VEC (char_ptr) *
  141. auto_load_expand_dir_vars (const char *string)
  142. {
  143.   VEC (char_ptr) *dir_vec;
  144.   char *s;

  145.   s = xstrdup (string);
  146.   substitute_path_component (&s, "$datadir", gdb_datadir);
  147.   substitute_path_component (&s, "$debugdir", debug_file_directory);

  148.   if (debug_auto_load && strcmp (s, string) != 0)
  149.     fprintf_unfiltered (gdb_stdlog,
  150.                         _("auto-load: Expanded $-variables to \"%s\".\n"), s);

  151.   dir_vec = dirnames_to_char_ptr_vec (s);
  152.   xfree(s);

  153.   return dir_vec;
  154. }

  155. /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH.  */

  156. static void
  157. auto_load_safe_path_vec_update (void)
  158. {
  159.   unsigned len;
  160.   int ix;

  161.   if (debug_auto_load)
  162.     fprintf_unfiltered (gdb_stdlog,
  163.                         _("auto-load: Updating directories of \"%s\".\n"),
  164.                         auto_load_safe_path);

  165.   free_char_ptr_vec (auto_load_safe_path_vec);

  166.   auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
  167.   len = VEC_length (char_ptr, auto_load_safe_path_vec);

  168.   /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
  169.      element.  */
  170.   for (ix = 0; ix < len; ix++)
  171.     {
  172.       char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
  173.       char *expanded = tilde_expand (dir);
  174.       char *real_path = gdb_realpath (expanded);

  175.       /* Ensure the current entry is at least tilde_expand-ed.  */
  176.       VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);

  177.       if (debug_auto_load)
  178.         {
  179.           if (strcmp (expanded, dir) == 0)
  180.             fprintf_unfiltered (gdb_stdlog,
  181.                                 _("auto-load: Using directory \"%s\".\n"),
  182.                                 expanded);
  183.           else
  184.             fprintf_unfiltered (gdb_stdlog,
  185.                                 _("auto-load: Resolved directory \"%s\" "
  186.                                   "as \"%s\".\n"),
  187.                                 dir, expanded);
  188.         }
  189.       xfree (dir);

  190.       /* If gdb_realpath returns a different content, append it.  */
  191.       if (strcmp (real_path, expanded) == 0)
  192.         xfree (real_path);
  193.       else
  194.         {
  195.           VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);

  196.           if (debug_auto_load)
  197.             fprintf_unfiltered (gdb_stdlog,
  198.                                 _("auto-load: And canonicalized as \"%s\".\n"),
  199.                                 real_path);
  200.         }
  201.     }
  202. }

  203. /* Variable gdb_datadir has been set.  Update content depending on $datadir.  */

  204. static void
  205. auto_load_gdb_datadir_changed (void)
  206. {
  207.   auto_load_safe_path_vec_update ();
  208. }

  209. /* "set" command for the auto_load_safe_path configuration variable.  */

  210. static void
  211. set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
  212. {
  213.   /* Setting the variable to "" resets it to the compile time defaults.  */
  214.   if (auto_load_safe_path[0] == '\0')
  215.     {
  216.       xfree (auto_load_safe_path);
  217.       auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
  218.     }

  219.   auto_load_safe_path_vec_update ();
  220. }

  221. /* "show" command for the auto_load_safe_path configuration variable.  */

  222. static void
  223. show_auto_load_safe_path (struct ui_file *file, int from_tty,
  224.                           struct cmd_list_element *c, const char *value)
  225. {
  226.   const char *cs;

  227.   /* Check if user has entered either "/" or for example ":".
  228.      But while more complicate content like ":/foo" would still also
  229.      permit any location do not hide those.  */

  230.   for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
  231.        cs++);
  232.   if (*cs == 0)
  233.     fprintf_filtered (file, _("Auto-load files are safe to load from any "
  234.                               "directory.\n"));
  235.   else
  236.     fprintf_filtered (file, _("List of directories from which it is safe to "
  237.                               "auto-load files is %s.\n"),
  238.                       value);
  239. }

  240. /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
  241.    variable.  */

  242. static void
  243. add_auto_load_safe_path (char *args, int from_tty)
  244. {
  245.   char *s;

  246.   if (args == NULL || *args == 0)
  247.     error (_("\
  248. Directory argument required.\n\
  249. Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
  250. "));

  251.   s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
  252.   xfree (auto_load_safe_path);
  253.   auto_load_safe_path = s;

  254.   auto_load_safe_path_vec_update ();
  255. }

  256. /* "add-auto-load-scripts-directory" command for the auto_load_dir configuration
  257.    variable.  */

  258. static void
  259. add_auto_load_dir (char *args, int from_tty)
  260. {
  261.   char *s;

  262.   if (args == NULL || *args == 0)
  263.     error (_("Directory argument required."));

  264.   s = xstrprintf ("%s%c%s", auto_load_dir, DIRNAME_SEPARATOR, args);
  265.   xfree (auto_load_dir);
  266.   auto_load_dir = s;
  267. }

  268. /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
  269.    and PATTERN.  */

  270. static int
  271. filename_is_in_pattern_1 (char *filename, char *pattern)
  272. {
  273.   size_t pattern_len = strlen (pattern);
  274.   size_t filename_len = strlen (filename);

  275.   if (debug_auto_load)
  276.     fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" "
  277.                                       "to pattern \"%s\"\n"),
  278.                         filename, pattern);

  279.   /* Trim trailing slashes ("/") from PATTERN.  Even for "d:\" paths as
  280.      trailing slashes are trimmed also from FILENAME it still matches
  281.      correctly.  */
  282.   while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
  283.     pattern_len--;
  284.   pattern[pattern_len] = '\0';

  285.   /* Ensure auto_load_safe_path "/" matches any FILENAME.  On MS-Windows
  286.      platform FILENAME even after gdb_realpath does not have to start with
  287.      IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename.  */
  288.   if (pattern_len == 0)
  289.     {
  290.       if (debug_auto_load)
  291.         fprintf_unfiltered (gdb_stdlog,
  292.                             _("auto-load: Matched - empty pattern\n"));
  293.       return 1;
  294.     }

  295.   for (;;)
  296.     {
  297.       /* Trim trailing slashes ("/").  PATTERN also has slashes trimmed the
  298.          same way so they will match.  */
  299.       while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
  300.         filename_len--;
  301.       filename[filename_len] = '\0';
  302.       if (filename_len == 0)
  303.         {
  304.           if (debug_auto_load)
  305.             fprintf_unfiltered (gdb_stdlog,
  306.                                 _("auto-load: Not matched - pattern \"%s\".\n"),
  307.                                 pattern);
  308.           return 0;
  309.         }

  310.       if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
  311.           == 0)
  312.         {
  313.           if (debug_auto_load)
  314.             fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file "
  315.                                               "\"%s\" to pattern \"%s\".\n"),
  316.                                 filename, pattern);
  317.           return 1;
  318.         }

  319.       /* Trim trailing FILENAME component.  */
  320.       while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
  321.         filename_len--;
  322.     }
  323. }

  324. /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
  325.    a subdirectory of a directory that matches PATTERN.  Return 0 otherwise.
  326.    gdb_realpath normalization is never done here.  */

  327. static ATTRIBUTE_PURE int
  328. filename_is_in_pattern (const char *filename, const char *pattern)
  329. {
  330.   char *filename_copy, *pattern_copy;

  331.   filename_copy = alloca (strlen (filename) + 1);
  332.   strcpy (filename_copy, filename);
  333.   pattern_copy = alloca (strlen (pattern) + 1);
  334.   strcpy (pattern_copy, pattern);

  335.   return filename_is_in_pattern_1 (filename_copy, pattern_copy);
  336. }

  337. /* Return 1 if FILENAME belongs to one of directory components of
  338.    AUTO_LOAD_SAFE_PATH_VEC.  Return 0 otherwise.
  339.    auto_load_safe_path_vec_update is never called.
  340.    *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
  341.    freed by the caller.  */

  342. static int
  343. filename_is_in_auto_load_safe_path_vec (const char *filename,
  344.                                         char **filename_realp)
  345. {
  346.   char *pattern;
  347.   int ix;

  348.   for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern);
  349.        ++ix)
  350.     if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern))
  351.       break;

  352.   if (pattern == NULL)
  353.     {
  354.       if (*filename_realp == NULL)
  355.         {
  356.           *filename_realp = gdb_realpath (filename);
  357.           if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
  358.             fprintf_unfiltered (gdb_stdlog,
  359.                                 _("auto-load: Resolved "
  360.                                   "file \"%s\" as \"%s\".\n"),
  361.                                 filename, *filename_realp);
  362.         }

  363.       if (strcmp (*filename_realp, filename) != 0)
  364.         for (ix = 0;
  365.              VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix)
  366.           if (filename_is_in_pattern (*filename_realp, pattern))
  367.             break;
  368.     }

  369.   if (pattern != NULL)
  370.     {
  371.       if (debug_auto_load)
  372.         fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
  373.                                           "directory \"%s\".\n"),
  374.                             filename, pattern);
  375.       return 1;
  376.     }

  377.   return 0;
  378. }

  379. /* Return 1 if FILENAME is located in one of the directories of
  380.    AUTO_LOAD_SAFE_PATH.  Otherwise call warning and return 0.  FILENAME does
  381.    not have to be an absolute path.

  382.    Existence of FILENAME is not checked.  Function will still give a warning
  383.    even if the caller would quietly skip non-existing file in unsafe
  384.    directory.  */

  385. int
  386. file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
  387. {
  388.   char *filename_real = NULL;
  389.   struct cleanup *back_to;
  390.   static int advice_printed = 0;

  391.   if (debug_auto_load)
  392.     {
  393.       va_list debug_args;

  394.       va_start (debug_args, debug_fmt);
  395.       vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
  396.       va_end (debug_args);
  397.     }

  398.   back_to = make_cleanup (free_current_contents, &filename_real);

  399.   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
  400.     {
  401.       do_cleanups (back_to);
  402.       return 1;
  403.     }

  404.   auto_load_safe_path_vec_update ();
  405.   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
  406.     {
  407.       do_cleanups (back_to);
  408.       return 1;
  409.     }

  410.   warning (_("File \"%s\" auto-loading has been declined by your "
  411.              "`auto-load safe-path' set to \"%s\"."),
  412.            filename_real, auto_load_safe_path);

  413.   if (!advice_printed)
  414.     {
  415.       const char *homedir = getenv ("HOME");
  416.       char *homeinit;

  417.       if (homedir == NULL)
  418.         homedir = "$HOME";
  419.       homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
  420.       make_cleanup (xfree, homeinit);

  421.       printf_filtered (_("\
  422. To enable execution of this file add\n\
  423. \tadd-auto-load-safe-path %s\n\
  424. line to your configuration file \"%s\".\n\
  425. To completely disable this security protection add\n\
  426. \tset auto-load safe-path /\n\
  427. line to your configuration file \"%s\".\n\
  428. For more information about this security protection see the\n\
  429. \"Auto-loading safe path\" section in the GDB manual.  E.g., run from the shell:\n\
  430. \tinfo \"(gdb)Auto-loading safe path\"\n"),
  431.                        filename_real, homeinit, homeinit);
  432.       advice_printed = 1;
  433.     }

  434.   do_cleanups (back_to);
  435.   return 0;
  436. }

  437. /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
  438.    the same script.  There's no point in loading the script multiple times,
  439.    and there can be a lot of objfiles and scripts, so we keep track of scripts
  440.    loaded this way.  */

  441. struct auto_load_pspace_info
  442. {
  443.   /* For each program space we keep track of loaded scripts.  */
  444.   struct htab *loaded_scripts;

  445.   /* Non-zero if we've issued the warning about an auto-load script not being
  446.      supported.  We only want to issue this warning once.  */
  447.   int unsupported_script_warning_printed;

  448.   /* Non-zero if we've issued the warning about an auto-load script not being
  449.      found.  We only want to issue this warning once.  */
  450.   int script_not_found_warning_printed;
  451. };

  452. /* Objects of this type are stored in the loaded script hash table.  */

  453. struct loaded_script
  454. {
  455.   /* Name as provided by the objfile.  */
  456.   const char *name;

  457.   /* Full path name or NULL if script wasn't found (or was otherwise
  458.      inaccessible).  */
  459.   const char *full_path;

  460.   /* Non-zero if this script has been loaded.  */
  461.   int loaded;

  462.   const struct extension_language_defn *language;
  463. };

  464. /* Per-program-space data key.  */
  465. static const struct program_space_data *auto_load_pspace_data;

  466. static void
  467. auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
  468. {
  469.   struct auto_load_pspace_info *info = arg;

  470.   if (info->loaded_scripts)
  471.     htab_delete (info->loaded_scripts);
  472.   xfree (info);
  473. }

  474. /* Get the current autoload data.  If none is found yet, add it now.  This
  475.    function always returns a valid object.  */

  476. static struct auto_load_pspace_info *
  477. get_auto_load_pspace_data (struct program_space *pspace)
  478. {
  479.   struct auto_load_pspace_info *info;

  480.   info = program_space_data (pspace, auto_load_pspace_data);
  481.   if (info == NULL)
  482.     {
  483.       info = XCNEW (struct auto_load_pspace_info);
  484.       set_program_space_data (pspace, auto_load_pspace_data, info);
  485.     }

  486.   return info;
  487. }

  488. /* Hash function for the loaded script hash.  */

  489. static hashval_t
  490. hash_loaded_script_entry (const void *data)
  491. {
  492.   const struct loaded_script *e = data;

  493.   return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
  494. }

  495. /* Equality function for the loaded script hash.  */

  496. static int
  497. eq_loaded_script_entry (const void *a, const void *b)
  498. {
  499.   const struct loaded_script *ea = a;
  500.   const struct loaded_script *eb = b;

  501.   return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
  502. }

  503. /* Initialize the table to track loaded scripts.
  504.    Each entry is hashed by the full path name.  */

  505. static void
  506. init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
  507. {
  508.   /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
  509.      Space for each entry is obtained with one malloc so we can free them
  510.      easily.  */

  511.   pspace_info->loaded_scripts = htab_create (31,
  512.                                              hash_loaded_script_entry,
  513.                                              eq_loaded_script_entry,
  514.                                              xfree);

  515.   pspace_info->unsupported_script_warning_printed = FALSE;
  516.   pspace_info->script_not_found_warning_printed = FALSE;
  517. }

  518. /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
  519.    for loading scripts.  */

  520. struct auto_load_pspace_info *
  521. get_auto_load_pspace_data_for_loading (struct program_space *pspace)
  522. {
  523.   struct auto_load_pspace_info *info;

  524.   info = get_auto_load_pspace_data (pspace);
  525.   if (info->loaded_scripts == NULL)
  526.     init_loaded_scripts_info (info);

  527.   return info;
  528. }

  529. /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO.  LOADED 1 if the
  530.    script has been (is going to) be loaded, 0 otherwise (such as if it has not
  531.    been found).  FULL_PATH is NULL if the script wasn't found.  The result is
  532.    true if the script was already in the hash table.  */

  533. static int
  534. maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
  535.                   const char *name, const char *full_path,
  536.                   const struct extension_language_defn *language)
  537. {
  538.   struct htab *htab = pspace_info->loaded_scripts;
  539.   struct loaded_script **slot, entry;
  540.   int in_hash_table;

  541.   entry.name = name;
  542.   entry.language = language;
  543.   slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
  544.   in_hash_table = *slot != NULL;

  545.   /* If this script is not in the hash table, add it.  */

  546.   if (! in_hash_table)
  547.     {
  548.       char *p;

  549.       /* Allocate all space in one chunk so it's easier to free.  */
  550.       *slot = xmalloc (sizeof (**slot)
  551.                        + strlen (name) + 1
  552.                        + (full_path != NULL ? (strlen (full_path) + 1) : 0));
  553.       p = ((char*) *slot) + sizeof (**slot);
  554.       strcpy (p, name);
  555.       (*slot)->name = p;
  556.       if (full_path != NULL)
  557.         {
  558.           p += strlen (p) + 1;
  559.           strcpy (p, full_path);
  560.           (*slot)->full_path = p;
  561.         }
  562.       else
  563.         (*slot)->full_path = NULL;
  564.       (*slot)->loaded = loaded;
  565.       (*slot)->language = language;
  566.     }

  567.   return in_hash_table;
  568. }

  569. /* Clear the table of loaded section scripts.  */

  570. static void
  571. clear_section_scripts (void)
  572. {
  573.   struct program_space *pspace = current_program_space;
  574.   struct auto_load_pspace_info *info;

  575.   info = program_space_data (pspace, auto_load_pspace_data);
  576.   if (info != NULL && info->loaded_scripts != NULL)
  577.     {
  578.       htab_delete (info->loaded_scripts);
  579.       info->loaded_scripts = NULL;
  580.       info->unsupported_script_warning_printed = FALSE;
  581.       info->script_not_found_warning_printed = FALSE;
  582.     }
  583. }

  584. /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
  585.    OBJFILE's gdb_realpath is REALNAME and load it.  Return 1 if we found any
  586.    matching script, return 0 otherwise.  */

  587. static int
  588. auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
  589.                             const struct extension_language_defn *language)
  590. {
  591.   char *filename, *debugfile;
  592.   int len, retval;
  593.   FILE *input;
  594.   struct cleanup *cleanups;
  595.   const char *suffix = ext_lang_auto_load_suffix (language);

  596.   len = strlen (realname);
  597.   filename = xmalloc (len + strlen (suffix) + 1);
  598.   memcpy (filename, realname, len);
  599.   strcpy (filename + len, suffix);

  600.   cleanups = make_cleanup (xfree, filename);

  601.   input = gdb_fopen_cloexec (filename, "r");
  602.   debugfile = filename;
  603.   if (debug_auto_load)
  604.     fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
  605.                         debugfile, input ? _("exists") : _("does not exist"));

  606.   if (!input)
  607.     {
  608.       VEC (char_ptr) *vec;
  609.       int ix;
  610.       char *dir;

  611.       /* Also try the same file in a subdirectory of gdb's data
  612.          directory.  */

  613.       vec = auto_load_expand_dir_vars (auto_load_dir);
  614.       make_cleanup_free_char_ptr_vec (vec);

  615.       if (debug_auto_load)
  616.         fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
  617.                                           "scripts-directory' path \"%s\".\n"),
  618.                             auto_load_dir);

  619.       for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
  620.         {
  621.           debugfile = xmalloc (strlen (dir) + strlen (filename) + 1);
  622.           strcpy (debugfile, dir);

  623.           /* FILENAME is absolute, so we don't need a "/" here.  */
  624.           strcat (debugfile, filename);

  625.           make_cleanup (xfree, debugfile);
  626.           input = gdb_fopen_cloexec (debugfile, "r");
  627.           if (debug_auto_load)
  628.             fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
  629.                                               "\"%s\" %s.\n"),
  630.                                 debugfile,
  631.                                 input ? _("exists") : _("does not exist"));
  632.           if (input != NULL)
  633.             break;
  634.         }
  635.     }

  636.   if (input)
  637.     {
  638.       int is_safe;
  639.       struct auto_load_pspace_info *pspace_info;

  640.       make_cleanup_fclose (input);

  641.       is_safe
  642.         = file_is_auto_load_safe (debugfile,
  643.                                   _("auto-load: Loading %s script \"%s\""
  644.                                     " by extension for objfile \"%s\".\n"),
  645.                                   ext_lang_name (language),
  646.                                   debugfile, objfile_name (objfile));

  647.       /* Add this script to the hash table too so
  648.          "info auto-load ${lang}-scripts" can print it.  */
  649.       pspace_info
  650.         = get_auto_load_pspace_data_for_loading (current_program_space);
  651.       maybe_add_script (pspace_info, is_safe, debugfile, debugfile, language);

  652.       /* To preserve existing behaviour we don't check for whether the
  653.          script was already in the table, and always load it.
  654.          It's highly unlikely that we'd ever load it twice,
  655.          and these scripts are required to be idempotent under multiple
  656.          loads anyway.  */
  657.       if (is_safe)
  658.         {
  659.           objfile_script_sourcer_func *sourcer
  660.             = ext_lang_objfile_script_sourcer (language);

  661.           /* We shouldn't get here if support for the language isn't
  662.              compiled in.  And the extension language is required to implement
  663.              this function.  */
  664.           gdb_assert (sourcer != NULL);
  665.           sourcer (language, objfile, input, debugfile);
  666.         }

  667.       retval = 1;
  668.     }
  669.   else
  670.     retval = 0;

  671.   do_cleanups (cleanups);
  672.   return retval;
  673. }

  674. /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
  675.    it.  */

  676. void
  677. auto_load_objfile_script (struct objfile *objfile,
  678.                           const struct extension_language_defn *language)
  679. {
  680.   char *realname = gdb_realpath (objfile_name (objfile));
  681.   struct cleanup *cleanups = make_cleanup (xfree, realname);

  682.   if (!auto_load_objfile_script_1 (objfile, realname, language))
  683.     {
  684.       /* For Windows/DOS .exe executables, strip the .exe suffix, so that
  685.          FOO-gdb.gdb could be used for FOO.exe, and try again.  */

  686.       size_t len = strlen (realname);
  687.       const size_t lexe = sizeof (".exe") - 1;

  688.       if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0)
  689.         {
  690.           len -= lexe;
  691.           realname[len] = '\0';
  692.           if (debug_auto_load)
  693.             fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
  694.                                               "retrying with \"%s\".\n"),
  695.                                 realname);
  696.           auto_load_objfile_script_1 (objfile, realname, language);
  697.         }
  698.     }

  699.   do_cleanups (cleanups);
  700. }

  701. /* Load scripts specified in OBJFILE.
  702.    START,END delimit a buffer containing a list of nul-terminated
  703.    file names.
  704.    SECTION_NAME is used in error messages.

  705.    Scripts are found per normal "source -s" command processing.
  706.    First the script is looked for in $cwd.  If not found there the
  707.    source search path is used.

  708.    The section contains a list of path names of script files to load.
  709.    Each path is null-terminated.  */

  710. static void
  711. source_section_scripts (struct objfile *objfile, const char *section_name,
  712.                         const char *start, const char *end)
  713. {
  714.   const char *p;
  715.   struct auto_load_pspace_info *pspace_info;

  716.   pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);

  717.   for (p = start; p < end; ++p)
  718.     {
  719.       const char *file;
  720.       FILE *stream;
  721.       char *full_path;
  722.       int opened, in_hash_table;
  723.       struct cleanup *back_to;
  724.       const struct extension_language_defn *language;
  725.       objfile_script_sourcer_func *sourcer;

  726.       switch (*p)
  727.         {
  728.         case SECTION_SCRIPT_ID_PYTHON_FILE:
  729.           language = get_ext_lang_defn (EXT_LANG_PYTHON);
  730.           break;
  731.         case SECTION_SCRIPT_ID_SCHEME_FILE:
  732.           language = get_ext_lang_defn (EXT_LANG_GUILE);
  733.           break;
  734.         default:
  735.           warning (_("Invalid entry in %s section"), section_name);
  736.           /* We could try various heuristics to find the next valid entry,
  737.              but it's safer to just punt.  */
  738.           return;
  739.         }
  740.       file = ++p;

  741.       while (p < end && *p != '\0')
  742.         ++p;
  743.       if (p == end)
  744.         {
  745.           char *buf = alloca (p - file + 1);

  746.           memcpy (buf, file, p - file);
  747.           buf[p - file] = '\0';
  748.           warning (_("Non-null-terminated path in %s: %s"),
  749.                    section_name, buf);
  750.           /* Don't load it.  */
  751.           break;
  752.         }
  753.       if (p == file)
  754.         {
  755.           warning (_("Empty path in %s"), section_name);
  756.           continue;
  757.         }

  758.       /* Until we support more types of records in .debug_gdb_scripts we do
  759.          all the processing here.  The expectation is to add a new
  760.          extension_language_script_ops "method" that handles all the records
  761.          for the language.  For now we can just use
  762.          extension_language_script_ops.objfile_script_sourcer.  */

  763.       /* Skip this script if support is not compiled in.  */
  764.       sourcer = ext_lang_objfile_script_sourcer (language);
  765.       if (sourcer == NULL)
  766.         {
  767.           /* We don't throw an error, the program is still debuggable.  */
  768.           if (!unsupported_script_warning_print (pspace_info))
  769.             {
  770.               warning (_("Unsupported auto-load scripts referenced in"
  771.                          " %s section\n"
  772.                          "of file %s.\n"
  773.                          "Use `info auto-load %s-scripts [REGEXP]'"
  774.                          " to list them."),
  775.                        section_name, objfile_name (objfile),
  776.                        ext_lang_name (language));
  777.             }
  778.           /* We *could* still try to open it, but there's no point.  */
  779.           maybe_add_script (pspace_info, 0, file, NULL, language);
  780.           continue;
  781.         }

  782.       /* Skip this script if auto-loading it has been disabled.  */
  783.       if (!ext_lang_auto_load_enabled (language))
  784.         {
  785.           /* No message is printed, just skip it.  */
  786.           continue;
  787.         }

  788.       opened = find_and_open_script (file, 1 /*search_path*/,
  789.                                      &stream, &full_path);

  790.       back_to = make_cleanup (null_cleanup, NULL);
  791.       if (opened)
  792.         {
  793.           make_cleanup_fclose (stream);
  794.           make_cleanup (xfree, full_path);

  795.           if (!file_is_auto_load_safe (full_path,
  796.                                        _("auto-load: Loading %s script "
  797.                                          "\"%s\" from section \"%s\" of "
  798.                                          "objfile \"%s\".\n"),
  799.                                        ext_lang_name (language), full_path,
  800.                                        section_name, objfile_name (objfile)))
  801.             opened = 0;
  802.         }
  803.       else
  804.         {
  805.           full_path = NULL;

  806.           /* If one script isn't found it's not uncommon for more to not be
  807.              found either.  We don't want to print a message for each script,
  808.              too much noise.  Instead, we print the warning once and tell the
  809.              user how to find the list of scripts that weren't loaded.
  810.              We don't throw an error, the program is still debuggable.

  811.              IWBN if complaints.c were more general-purpose.  */

  812.           if (script_not_found_warning_print (pspace_info))
  813.             warning (_("Missing auto-load scripts referenced in section %s\n\
  814. of file %s\n\
  815. Use `info auto-load %s-scripts [REGEXP]' to list them."),
  816.                      section_name, objfile_name (objfile),
  817.                      ext_lang_name (language));
  818.         }

  819.       in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
  820.                                         language);

  821.       /* If this file is not currently loaded, load it.  */
  822.       if (opened && !in_hash_table)
  823.         sourcer (language, objfile, stream, full_path);

  824.       do_cleanups (back_to);
  825.     }
  826. }

  827. /* Load scripts specified in section SECTION_NAME of OBJFILE.  */

  828. static void
  829. auto_load_section_scripts (struct objfile *objfile, const char *section_name)
  830. {
  831.   bfd *abfd = objfile->obfd;
  832.   asection *scripts_sect;
  833.   bfd_byte *data = NULL;

  834.   scripts_sect = bfd_get_section_by_name (abfd, section_name);
  835.   if (scripts_sect == NULL)
  836.     return;

  837.   if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
  838.     warning (_("Couldn't read %s section of %s"),
  839.              section_name, bfd_get_filename (abfd));
  840.   else
  841.     {
  842.       struct cleanup *cleanups;
  843.       char *p = (char *) data;

  844.       cleanups = make_cleanup (xfree, p);
  845.       source_section_scripts (objfile, section_name, p,
  846.                               p + bfd_get_section_size (scripts_sect));
  847.       do_cleanups (cleanups);
  848.     }
  849. }

  850. /* Load any auto-loaded scripts for OBJFILE.  */

  851. void
  852. load_auto_scripts_for_objfile (struct objfile *objfile)
  853. {
  854.   /* Return immediately if auto-loading has been globally disabled.
  855.      This is to handle sequencing of operations during gdb startup.
  856.      Also return immediately if OBJFILE is not actually a file.  */
  857.   if (!global_auto_load || (objfile->flags & OBJF_NOT_FILENAME) != 0)
  858.     return;

  859.   /* Load any extension language scripts for this objfile.
  860.      E.g., foo-gdb.gdb, foo-gdb.py.  */
  861.   auto_load_ext_lang_scripts_for_objfile (objfile);

  862.   /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts).  */
  863.   auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
  864. }

  865. /* This is a new_objfile observer callback to auto-load scripts.

  866.    Two flavors of auto-loaded scripts are supported.
  867.    1) based on the path to the objfile
  868.    2) from .debug_gdb_scripts section  */

  869. static void
  870. auto_load_new_objfile (struct objfile *objfile)
  871. {
  872.   if (!objfile)
  873.     {
  874.       /* OBJFILE is NULL when loading a new "main" symbol-file.  */
  875.       clear_section_scripts ();
  876.       return;
  877.     }

  878.   load_auto_scripts_for_objfile (objfile);
  879. }

  880. /* Collect scripts to be printed in a vec.  */

  881. typedef struct loaded_script *loaded_script_ptr;
  882. DEF_VEC_P (loaded_script_ptr);

  883. struct collect_matching_scripts_data
  884. {
  885.   VEC (loaded_script_ptr) **scripts_p;

  886.   const struct extension_language_defn *language;
  887. };

  888. /* Traversal function for htab_traverse.
  889.    Collect the entry if it matches the regexp.  */

  890. static int
  891. collect_matching_scripts (void **slot, void *info)
  892. {
  893.   struct loaded_script *script = *slot;
  894.   struct collect_matching_scripts_data *data = info;

  895.   if (script->language == data->language && re_exec (script->name))
  896.     VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);

  897.   return 1;
  898. }

  899. /* Print SCRIPT.  */

  900. static void
  901. print_script (struct loaded_script *script)
  902. {
  903.   struct ui_out *uiout = current_uiout;
  904.   struct cleanup *chain;

  905.   chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  906.   ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
  907.   ui_out_field_string (uiout, "script", script->name);
  908.   ui_out_text (uiout, "\n");

  909.   /* If the name isn't the full path, print it too.  */
  910.   if (script->full_path != NULL
  911.       && strcmp (script->name, script->full_path) != 0)
  912.     {
  913.       ui_out_text (uiout, "\tfull name: ");
  914.       ui_out_field_string (uiout, "full_path", script->full_path);
  915.       ui_out_text (uiout, "\n");
  916.     }

  917.   do_cleanups (chain);
  918. }

  919. /* Helper for info_auto_load_scripts to sort the scripts by name.  */

  920. static int
  921. sort_scripts_by_name (const void *ap, const void *bp)
  922. {
  923.   const struct loaded_script *a = *(const struct loaded_script **) ap;
  924.   const struct loaded_script *b = *(const struct loaded_script **) bp;

  925.   return FILENAME_CMP (a->name, b->name);
  926. }

  927. /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
  928.    the "info auto-load XXX" command has been executed through the general
  929.    "info auto-load" invocation.  Extra newline will be printed if needed.  */
  930. char auto_load_info_scripts_pattern_nl[] = "";

  931. /* Implementation for "info auto-load gdb-scripts"
  932.    (and "info auto-load python-scripts").  List scripts in LANGUAGE matching
  933.    PATTERN.  FROM_TTY is the usual GDB boolean for user interactivity.  */

  934. void
  935. auto_load_info_scripts (char *pattern, int from_tty,
  936.                         const struct extension_language_defn *language)
  937. {
  938.   struct ui_out *uiout = current_uiout;
  939.   struct auto_load_pspace_info *pspace_info;
  940.   struct cleanup *script_chain;
  941.   VEC (loaded_script_ptr) *scripts;
  942.   int nr_scripts;

  943.   dont_repeat ();

  944.   pspace_info = get_auto_load_pspace_data (current_program_space);

  945.   if (pattern && *pattern)
  946.     {
  947.       char *re_err = re_comp (pattern);

  948.       if (re_err)
  949.         error (_("Invalid regexp: %s"), re_err);
  950.     }
  951.   else
  952.     {
  953.       re_comp ("");
  954.     }

  955.   /* We need to know the number of rows before we build the table.
  956.      Plus we want to sort the scripts by name.
  957.      So first traverse the hash table collecting the matching scripts.  */

  958.   scripts = VEC_alloc (loaded_script_ptr, 10);
  959.   script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);

  960.   if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
  961.     {
  962.       struct collect_matching_scripts_data data = { &scripts, language };

  963.       /* Pass a pointer to scripts as VEC_safe_push can realloc space.  */
  964.       htab_traverse_noresize (pspace_info->loaded_scripts,
  965.                               collect_matching_scripts, &data);
  966.     }

  967.   nr_scripts = VEC_length (loaded_script_ptr, scripts);

  968.   /* Table header shifted right by preceding "gdb-scripts:  " would not match
  969.      its columns.  */
  970.   if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
  971.     ui_out_text (uiout, "\n");

  972.   make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
  973.                                        "AutoLoadedScriptsTable");

  974.   ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
  975.   ui_out_table_header (uiout, 70, ui_left, "script", "Script");
  976.   ui_out_table_body (uiout);

  977.   if (nr_scripts > 0)
  978.     {
  979.       int i;
  980.       loaded_script_ptr script;

  981.       qsort (VEC_address (loaded_script_ptr, scripts),
  982.              VEC_length (loaded_script_ptr, scripts),
  983.              sizeof (loaded_script_ptr), sort_scripts_by_name);
  984.       for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
  985.         print_script (script);
  986.     }

  987.   do_cleanups (script_chain);

  988.   if (nr_scripts == 0)
  989.     {
  990.       if (pattern && *pattern)
  991.         ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
  992.                         pattern);
  993.       else
  994.         ui_out_message (uiout, 0, "No auto-load scripts.\n");
  995.     }
  996. }

  997. /* Wrapper for "info auto-load gdb-scripts".  */

  998. static void
  999. info_auto_load_gdb_scripts (char *pattern, int from_tty)
  1000. {
  1001.   auto_load_info_scripts (pattern, from_tty, &extension_language_gdb);
  1002. }

  1003. /* Implement 'info auto-load local-gdbinit'.  */

  1004. static void
  1005. info_auto_load_local_gdbinit (char *args, int from_tty)
  1006. {
  1007.   if (auto_load_local_gdbinit_pathname == NULL)
  1008.     printf_filtered (_("Local .gdbinit file was not found.\n"));
  1009.   else if (auto_load_local_gdbinit_loaded)
  1010.     printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
  1011.                      auto_load_local_gdbinit_pathname);
  1012.   else
  1013.     printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
  1014.                      auto_load_local_gdbinit_pathname);
  1015. }

  1016. /* Return non-zero if UNSUPPORTED_SCRIPT_WARNING_PRINTED of PSPACE_INFO was
  1017.    unset before calling this function.  Always set
  1018.    UNSUPPORTED_SCRIPT_WARNING_PRINTED of PSPACE_INFO.  */

  1019. static int
  1020. unsupported_script_warning_print (struct auto_load_pspace_info *pspace_info)
  1021. {
  1022.   int retval = !pspace_info->unsupported_script_warning_printed;

  1023.   pspace_info->unsupported_script_warning_printed = 1;

  1024.   return retval;
  1025. }

  1026. /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
  1027.    before calling this function.  Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
  1028.    of PSPACE_INFO.  */

  1029. static int
  1030. script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
  1031. {
  1032.   int retval = !pspace_info->script_not_found_warning_printed;

  1033.   pspace_info->script_not_found_warning_printed = 1;

  1034.   return retval;
  1035. }

  1036. /* The only valid "set auto-load" argument is off|0|no|disable.  */

  1037. static void
  1038. set_auto_load_cmd (char *args, int from_tty)
  1039. {
  1040.   struct cmd_list_element *list;
  1041.   size_t length;

  1042.   /* See parse_binary_operation in use by the sub-commands.  */

  1043.   length = args ? strlen (args) : 0;

  1044.   while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
  1045.     length--;

  1046.   if (length == 0 || (strncmp (args, "off", length) != 0
  1047.                       && strncmp (args, "0", length) != 0
  1048.                       && strncmp (args, "no", length) != 0
  1049.                       && strncmp (args, "disable", length) != 0))
  1050.     error (_("Valid is only global 'set auto-load no'; "
  1051.              "otherwise check the auto-load sub-commands."));

  1052.   for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
  1053.     if (list->var_type == var_boolean)
  1054.       {
  1055.         gdb_assert (list->type == set_cmd);
  1056.         do_set_command (args, from_tty, list);
  1057.       }
  1058. }

  1059. /* Initialize "set auto-load " commands prefix and return it.  */

  1060. struct cmd_list_element **
  1061. auto_load_set_cmdlist_get (void)
  1062. {
  1063.   static struct cmd_list_element *retval;

  1064.   if (retval == NULL)
  1065.     add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
  1066. Auto-loading specific settings.\n\
  1067. Configure various auto-load-specific variables such as\n\
  1068. automatic loading of Python scripts."),
  1069.                     &retval, "set auto-load ",
  1070.                     1/*allow-unknown*/, &setlist);

  1071.   return &retval;
  1072. }

  1073. /* Command "show auto-load" displays summary of all the current
  1074.    "show auto-load " settings.  */

  1075. static void
  1076. show_auto_load_cmd (char *args, int from_tty)
  1077. {
  1078.   cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
  1079. }

  1080. /* Initialize "show auto-load " commands prefix and return it.  */

  1081. struct cmd_list_element **
  1082. auto_load_show_cmdlist_get (void)
  1083. {
  1084.   static struct cmd_list_element *retval;

  1085.   if (retval == NULL)
  1086.     add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
  1087. Show auto-loading specific settings.\n\
  1088. Show configuration of various auto-load-specific variables such as\n\
  1089. automatic loading of Python scripts."),
  1090.                     &retval, "show auto-load ",
  1091.                     0/*allow-unknown*/, &showlist);

  1092.   return &retval;
  1093. }

  1094. /* Command "info auto-load" displays whether the various auto-load files have
  1095.    been loaded.  This is reimplementation of cmd_show_list which inserts
  1096.    newlines at proper places.  */

  1097. static void
  1098. info_auto_load_cmd (char *args, int from_tty)
  1099. {
  1100.   struct cmd_list_element *list;
  1101.   struct cleanup *infolist_chain;
  1102.   struct ui_out *uiout = current_uiout;

  1103.   infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");

  1104.   for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
  1105.     {
  1106.       struct cleanup *option_chain
  1107.         = make_cleanup_ui_out_tuple_begin_end (uiout, "option");

  1108.       gdb_assert (!list->prefixlist);
  1109.       gdb_assert (list->type == not_set_cmd);

  1110.       ui_out_field_string (uiout, "name", list->name);
  1111.       ui_out_text (uiout, ":  ");
  1112.       cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);

  1113.       /* Close the tuple.  */
  1114.       do_cleanups (option_chain);
  1115.     }

  1116.   /* Close the tuple.  */
  1117.   do_cleanups (infolist_chain);
  1118. }

  1119. /* Initialize "info auto-load " commands prefix and return it.  */

  1120. struct cmd_list_element **
  1121. auto_load_info_cmdlist_get (void)
  1122. {
  1123.   static struct cmd_list_element *retval;

  1124.   if (retval == NULL)
  1125.     add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
  1126. Print current status of auto-loaded files.\n\
  1127. Print whether various files like Python scripts or .gdbinit files have been\n\
  1128. found and/or loaded."),
  1129.                     &retval, "info auto-load ",
  1130.                     0/*allow-unknown*/, &infolist);

  1131.   return &retval;
  1132. }

  1133. void _initialize_auto_load (void);

  1134. void
  1135. _initialize_auto_load (void)
  1136. {
  1137.   struct cmd_list_element *cmd;
  1138.   char *scripts_directory_help, *gdb_name_help, *python_name_help;
  1139.   char *guile_name_help;
  1140.   const char *suffix;

  1141.   auto_load_pspace_data
  1142.     = register_program_space_data_with_cleanup (NULL,
  1143.                                                 auto_load_pspace_data_cleanup);

  1144.   observer_attach_new_objfile (auto_load_new_objfile);

  1145.   add_setshow_boolean_cmd ("gdb-scripts", class_support,
  1146.                            &auto_load_gdb_scripts, _("\
  1147. Enable or disable auto-loading of canned sequences of commands scripts."), _("\
  1148. Show whether auto-loading of canned sequences of commands scripts is enabled."),
  1149.                            _("\
  1150. If enabled, canned sequences of commands are loaded when the debugger reads\n\
  1151. an executable or shared library.\n\
  1152. This options has security implications for untrusted inferiors."),
  1153.                            NULL, show_auto_load_gdb_scripts,
  1154.                            auto_load_set_cmdlist_get (),
  1155.                            auto_load_show_cmdlist_get ());

  1156.   add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
  1157.            _("Print the list of automatically loaded sequences of commands.\n\
  1158. Usage: info auto-load gdb-scripts [REGEXP]"),
  1159.            auto_load_info_cmdlist_get ());

  1160.   add_setshow_boolean_cmd ("local-gdbinit", class_support,
  1161.                            &auto_load_local_gdbinit, _("\
  1162. Enable or disable auto-loading of .gdbinit script in current directory."), _("\
  1163. Show whether auto-loading .gdbinit script in current directory is enabled."),
  1164.                            _("\
  1165. If enabled, canned sequences of commands are loaded when debugger starts\n\
  1166. from .gdbinit file in current directory.  Such files are deprecated,\n\
  1167. use a script associated with inferior executable file instead.\n\
  1168. This options has security implications for untrusted inferiors."),
  1169.                            NULL, show_auto_load_local_gdbinit,
  1170.                            auto_load_set_cmdlist_get (),
  1171.                            auto_load_show_cmdlist_get ());

  1172.   add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
  1173.            _("Print whether current directory .gdbinit file has been loaded.\n\
  1174. Usage: info auto-load local-gdbinit"),
  1175.            auto_load_info_cmdlist_get ());

  1176.   auto_load_dir = xstrdup (AUTO_LOAD_DIR);

  1177.   suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB));
  1178.   gdb_name_help
  1179.     = xstrprintf (_("\
  1180. GDB scripts:    OBJFILE%s\n"),
  1181.                   suffix);
  1182.   python_name_help = NULL;
  1183. #ifdef HAVE_PYTHON
  1184.   suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON));
  1185.   python_name_help
  1186.     = xstrprintf (_("\
  1187. Python scripts: OBJFILE%s\n"),
  1188.                   suffix);
  1189. #endif
  1190.   guile_name_help = NULL;
  1191. #ifdef HAVE_GUILE
  1192.   suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE));
  1193.   guile_name_help
  1194.     = xstrprintf (_("\
  1195. Guile scripts:  OBJFILE%s\n"),
  1196.                   suffix);
  1197. #endif
  1198.   scripts_directory_help
  1199.     = xstrprintf (_("\
  1200. Automatically loaded scripts are located in one of the directories listed\n\
  1201. by this option.\n\
  1202. \n\
  1203. Script names:\n\
  1204. %s%s%s\
  1205. \n\
  1206. This option is ignored for the kinds of scripts \
  1207. having 'set auto-load ... off'.\n\
  1208. Directories listed here need to be present also \
  1209. in the 'set auto-load safe-path'\n\
  1210. option."),
  1211.                   gdb_name_help,
  1212.                   python_name_help ? python_name_help : "",
  1213.                   guile_name_help ? guile_name_help : "");

  1214.   add_setshow_optional_filename_cmd ("scripts-directory", class_support,
  1215.                                      &auto_load_dir, _("\
  1216. Set the list of directories from which to load auto-loaded scripts."), _("\
  1217. Show the list of directories from which to load auto-loaded scripts."),
  1218.                                      scripts_directory_help,
  1219.                                      set_auto_load_dir, show_auto_load_dir,
  1220.                                      auto_load_set_cmdlist_get (),
  1221.                                      auto_load_show_cmdlist_get ());
  1222.   xfree (scripts_directory_help);
  1223.   xfree (python_name_help);
  1224.   xfree (gdb_name_help);
  1225.   xfree (guile_name_help);

  1226.   auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
  1227.   auto_load_safe_path_vec_update ();
  1228.   add_setshow_optional_filename_cmd ("safe-path", class_support,
  1229.                                      &auto_load_safe_path, _("\
  1230. Set the list of files and directories that are safe for auto-loading."), _("\
  1231. Show the list of files and directories that are safe for auto-loading."), _("\
  1232. Various files loaded automatically for the 'set auto-load ...' options must\n\
  1233. be located in one of the directories listed by this option.  Warning will be\n\
  1234. printed and file will not be used otherwise.\n\
  1235. You can mix both directory and filename entries.\n\
  1236. Setting this parameter to an empty list resets it to its default value.\n\
  1237. Setting this parameter to '/' (without the quotes) allows any file\n\
  1238. for the 'set auto-load ...' options.  Each path entry can be also shell\n\
  1239. wildcard pattern; '*' does not match directory separator.\n\
  1240. This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
  1241. This options has security implications for untrusted inferiors."),
  1242.                                      set_auto_load_safe_path,
  1243.                                      show_auto_load_safe_path,
  1244.                                      auto_load_set_cmdlist_get (),
  1245.                                      auto_load_show_cmdlist_get ());
  1246.   observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);

  1247.   cmd = add_cmd ("add-auto-load-safe-path", class_support,
  1248.                  add_auto_load_safe_path,
  1249.                  _("Add entries to the list of directories from which it is safe "
  1250.                    "to auto-load files.\n\
  1251. See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
  1252. access the current full list setting."),
  1253.                  &cmdlist);
  1254.   set_cmd_completer (cmd, filename_completer);

  1255.   cmd = add_cmd ("add-auto-load-scripts-directory", class_support,
  1256.                  add_auto_load_dir,
  1257.                  _("Add entries to the list of directories from which to load "
  1258.                    "auto-loaded scripts.\n\
  1259. See the commands 'set auto-load scripts-directory' and\n\
  1260. 'show auto-load scripts-directory' to access the current full list setting."),
  1261.                  &cmdlist);
  1262.   set_cmd_completer (cmd, filename_completer);

  1263.   add_setshow_boolean_cmd ("auto-load", class_maintenance,
  1264.                            &debug_auto_load, _("\
  1265. Set auto-load verifications debugging."), _("\
  1266. Show auto-load verifications debugging."), _("\
  1267. When non-zero, debugging output for files of 'set auto-load ...'\n\
  1268. is displayed."),
  1269.                             NULL, show_debug_auto_load,
  1270.                             &setdebuglist, &showdebuglist);
  1271. }