GDB (API)
/home/stan/gdb/src/gdb/python/py-auto-load.c
Go to the documentation of this file.
00001 /* GDB routines for supporting auto-loaded scripts.
00002 
00003    Copyright (C) 2010-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdb_string.h"
00022 #include "top.h"
00023 #include "exceptions.h"
00024 #include "gdbcmd.h"
00025 #include "objfiles.h"
00026 #include "python.h"
00027 #include "cli/cli-cmds.h"
00028 #include "auto-load.h"
00029 
00030 #ifdef HAVE_PYTHON
00031 
00032 #include "python-internal.h"
00033 
00034 /* The section to look for Python auto-loaded scripts (in file formats that
00035    support sections).
00036    Each entry in this section is a byte of value 1, and then the nul-terminated
00037    name of the script.  The script name may include a directory.
00038    The leading byte is to allow upward compatible extensions.  */
00039 #define GDBPY_AUTO_SECTION_NAME ".debug_gdb_scripts"
00040 
00041 /* User-settable option to enable/disable auto-loading of Python scripts:
00042    set auto-load python-scripts on|off
00043    This is true if we should auto-load associated Python scripts when an
00044    objfile is opened, false otherwise.  */
00045 static int auto_load_python_scripts = 1;
00046 
00047 static void gdbpy_load_auto_script_for_objfile (struct objfile *objfile,
00048                                                 FILE *file,
00049                                                 const char *filename);
00050 
00051 /* "show" command for the auto_load_python_scripts configuration variable.  */
00052 
00053 static void
00054 show_auto_load_python_scripts (struct ui_file *file, int from_tty,
00055                                struct cmd_list_element *c, const char *value)
00056 {
00057   fprintf_filtered (file, _("Auto-loading of Python scripts is %s.\n"), value);
00058 }
00059 
00060 /* Definition of script language for Python scripts.  */
00061 
00062 static const struct script_language script_language_python
00063   = { GDBPY_AUTO_FILE_NAME, gdbpy_load_auto_script_for_objfile };
00064 
00065 /* Wrapper of source_python_script_for_objfile for script_language_python.  */
00066 
00067 static void
00068 gdbpy_load_auto_script_for_objfile (struct objfile *objfile, FILE *file,
00069                                     const char *filename)
00070 {
00071   int is_safe;
00072   struct auto_load_pspace_info *pspace_info;
00073 
00074   is_safe = file_is_auto_load_safe (filename,
00075                                     _("auto-load: Loading Python script \"%s\" "
00076                                       "by extension for objfile \"%s\".\n"),
00077                                     filename, objfile_name (objfile));
00078 
00079   /* Add this script to the hash table too so "info auto-load python-scripts"
00080      can print it.  */
00081   pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
00082   maybe_add_script (pspace_info, is_safe, filename, filename,
00083                     &script_language_python);
00084 
00085   if (is_safe)
00086     source_python_script_for_objfile (objfile, file, filename);
00087 }
00088 
00089 /* Load scripts specified in OBJFILE.
00090    START,END delimit a buffer containing a list of nul-terminated
00091    file names.
00092    SOURCE_NAME is used in error messages.
00093 
00094    Scripts are found per normal "source -s" command processing.
00095    First the script is looked for in $cwd.  If not found there the
00096    source search path is used.
00097 
00098    The section contains a list of path names of files containing
00099    python code to load.  Each path is null-terminated.  */
00100 
00101 static void
00102 source_section_scripts (struct objfile *objfile, const char *source_name,
00103                         const char *start, const char *end)
00104 {
00105   const char *p;
00106   struct auto_load_pspace_info *pspace_info;
00107 
00108   pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
00109 
00110   for (p = start; p < end; ++p)
00111     {
00112       const char *file;
00113       FILE *stream;
00114       char *full_path;
00115       int opened, in_hash_table;
00116       struct cleanup *back_to;
00117 
00118       if (*p != 1)
00119         {
00120           warning (_("Invalid entry in %s section"), GDBPY_AUTO_SECTION_NAME);
00121           /* We could try various heuristics to find the next valid entry,
00122              but it's safer to just punt.  */
00123           break;
00124         }
00125       file = ++p;
00126 
00127       while (p < end && *p != '\0')
00128         ++p;
00129       if (p == end)
00130         {
00131           char *buf = alloca (p - file + 1);
00132 
00133           memcpy (buf, file, p - file);
00134           buf[p - file] = '\0';
00135           warning (_("Non-null-terminated path in %s: %s"),
00136                    source_name, buf);
00137           /* Don't load it.  */
00138           break;
00139         }
00140       if (p == file)
00141         {
00142           warning (_("Empty path in %s"), source_name);
00143           continue;
00144         }
00145 
00146       opened = find_and_open_script (file, 1 /*search_path*/,
00147                                      &stream, &full_path);
00148 
00149       back_to = make_cleanup (null_cleanup, NULL);
00150       if (opened)
00151         {
00152           make_cleanup_fclose (stream);
00153           make_cleanup (xfree, full_path);
00154 
00155           if (!file_is_auto_load_safe (full_path,
00156                                        _("auto-load: Loading Python script "
00157                                          "\"%s\" from section \"%s\" of "
00158                                          "objfile \"%s\".\n"),
00159                                        full_path, GDBPY_AUTO_SECTION_NAME,
00160                                        objfile_name (objfile)))
00161             opened = 0;
00162         }
00163       else
00164         {
00165           full_path = NULL;
00166 
00167           /* We don't throw an error, the program is still debuggable.  */
00168           if (script_not_found_warning_print (pspace_info))
00169             warning (_("Missing auto-load scripts referenced in section %s\n\
00170 of file %s\n\
00171 Use `info auto-load python [REGEXP]' to list them."),
00172                      GDBPY_AUTO_SECTION_NAME, objfile_name (objfile));
00173         }
00174 
00175       /* If one script isn't found it's not uncommon for more to not be
00176          found either.  We don't want to print an error message for each
00177          script, too much noise.  Instead, we print the warning once and tell
00178          the user how to find the list of scripts that weren't loaded.
00179 
00180          IWBN if complaints.c were more general-purpose.  */
00181 
00182       in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
00183                                         &script_language_python);
00184 
00185       /* If this file is not currently loaded, load it.  */
00186       if (opened && !in_hash_table)
00187         source_python_script_for_objfile (objfile, stream, full_path);
00188 
00189       do_cleanups (back_to);
00190     }
00191 }
00192 
00193 /* Load scripts specified in section SECTION_NAME of OBJFILE.  */
00194 
00195 static void
00196 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
00197 {
00198   bfd *abfd = objfile->obfd;
00199   asection *scripts_sect;
00200   bfd_byte *data = NULL;
00201 
00202   scripts_sect = bfd_get_section_by_name (abfd, section_name);
00203   if (scripts_sect == NULL)
00204     return;
00205 
00206   if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
00207     warning (_("Couldn't read %s section of %s"),
00208              section_name, bfd_get_filename (abfd));
00209   else
00210     {
00211       struct cleanup *cleanups;
00212       char *p = (char *) data;
00213 
00214       cleanups = make_cleanup (xfree, p);
00215       source_section_scripts (objfile, section_name, p,
00216                               p + bfd_get_section_size (scripts_sect));
00217       do_cleanups (cleanups);
00218     }
00219 }
00220 
00221 /* Load any Python auto-loaded scripts for OBJFILE.  */
00222 
00223 void
00224 gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
00225 {
00226   if (auto_load_python_scripts)
00227     {
00228       auto_load_objfile_script (objfile, &script_language_python);
00229       auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
00230     }
00231 }
00232 
00233 /* Wrapper for "info auto-load python-scripts".  */
00234 
00235 static void
00236 info_auto_load_python_scripts (char *pattern, int from_tty)
00237 {
00238   auto_load_info_scripts (pattern, from_tty, &script_language_python);
00239 }
00240 
00241 int
00242 gdbpy_initialize_auto_load (void)
00243 {
00244   struct cmd_list_element *cmd;
00245   const char *cmd_name;
00246 
00247   add_setshow_boolean_cmd ("python-scripts", class_support,
00248                            &auto_load_python_scripts, _("\
00249 Set the debugger's behaviour regarding auto-loaded Python scripts."), _("\
00250 Show the debugger's behaviour regarding auto-loaded Python scripts."), _("\
00251 If enabled, auto-loaded Python scripts are loaded when the debugger reads\n\
00252 an executable or shared library.\n\
00253 This options has security implications for untrusted inferiors."),
00254                            NULL, show_auto_load_python_scripts,
00255                            auto_load_set_cmdlist_get (),
00256                            auto_load_show_cmdlist_get ());
00257 
00258   add_setshow_boolean_cmd ("auto-load-scripts", class_support,
00259                            &auto_load_python_scripts, _("\
00260 Set the debugger's behaviour regarding auto-loaded Python scripts, "
00261                                                                  "deprecated."),
00262                            _("\
00263 Show the debugger's behaviour regarding auto-loaded Python scripts, "
00264                                                                  "deprecated."),
00265                            NULL, NULL, show_auto_load_python_scripts,
00266                            &setlist, &showlist);
00267   cmd_name = "auto-load-scripts";
00268   cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
00269   deprecate_cmd (cmd, "set auto-load python-scripts");
00270 
00271   /* It is needed because lookup_cmd updates the CMD_NAME pointer.  */
00272   cmd_name = "auto-load-scripts";
00273   cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
00274   deprecate_cmd (cmd, "show auto-load python-scripts");
00275 
00276   add_cmd ("python-scripts", class_info, info_auto_load_python_scripts,
00277            _("Print the list of automatically loaded Python scripts.\n\
00278 Usage: info auto-load python-scripts [REGEXP]"),
00279            auto_load_info_cmdlist_get ());
00280 
00281   cmd = add_info ("auto-load-scripts", info_auto_load_python_scripts, _("\
00282 Print the list of automatically loaded Python scripts, deprecated."));
00283   deprecate_cmd (cmd, "info auto-load python-scripts");
00284 
00285   return 0;
00286 }
00287 
00288 #else /* ! HAVE_PYTHON */
00289 
00290 void
00291 gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
00292 {
00293 }
00294 
00295 #endif /* ! HAVE_PYTHON */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines