1 /* GDB routines for supporting auto-loaded scripts.
3 Copyright (C) 2010-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "exceptions.h"
27 #include "cli/cli-cmds.h"
28 #include "auto-load.h"
32 #include "python-internal.h"
34 /* The section to look for Python auto-loaded scripts (in file formats that
36 Each entry in this section is a byte of value 1, and then the nul-terminated
37 name of the script. The script name may include a directory.
38 The leading byte is to allow upward compatible extensions. */
39 #define GDBPY_AUTO_SECTION_NAME ".debug_gdb_scripts"
41 /* User-settable option to enable/disable auto-loading of Python scripts:
42 set auto-load python-scripts on|off
43 This is true if we should auto-load associated Python scripts when an
44 objfile is opened, false otherwise. */
45 static int auto_load_python_scripts = 1;
47 static void gdbpy_load_auto_script_for_objfile (struct objfile *objfile,
49 const char *filename);
51 /* "show" command for the auto_load_python_scripts configuration variable. */
54 show_auto_load_python_scripts (struct ui_file *file, int from_tty,
55 struct cmd_list_element *c, const char *value)
57 fprintf_filtered (file, _("Auto-loading of Python scripts is %s.\n"), value);
60 /* Definition of script language for Python scripts. */
62 static const struct script_language script_language_python
63 = { GDBPY_AUTO_FILE_NAME, gdbpy_load_auto_script_for_objfile };
65 /* Wrapper of source_python_script_for_objfile for script_language_python. */
68 gdbpy_load_auto_script_for_objfile (struct objfile *objfile, FILE *file,
72 struct auto_load_pspace_info *pspace_info;
74 is_safe = file_is_auto_load_safe (filename,
75 _("auto-load: Loading Python script \"%s\" "
76 "by extension for objfile \"%s\".\n"),
77 filename, objfile_name (objfile));
79 /* Add this script to the hash table too so "info auto-load python-scripts"
81 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
82 maybe_add_script (pspace_info, is_safe, filename, filename,
83 &script_language_python);
86 source_python_script_for_objfile (objfile, file, filename);
89 /* Load scripts specified in OBJFILE.
90 START,END delimit a buffer containing a list of nul-terminated
92 SOURCE_NAME is used in error messages.
94 Scripts are found per normal "source -s" command processing.
95 First the script is looked for in $cwd. If not found there the
96 source search path is used.
98 The section contains a list of path names of files containing
99 python code to load. Each path is null-terminated. */
102 source_section_scripts (struct objfile *objfile, const char *source_name,
103 const char *start, const char *end)
106 struct auto_load_pspace_info *pspace_info;
108 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
110 for (p = start; p < end; ++p)
115 int opened, in_hash_table;
116 struct cleanup *back_to;
120 warning (_("Invalid entry in %s section"), GDBPY_AUTO_SECTION_NAME);
121 /* We could try various heuristics to find the next valid entry,
122 but it's safer to just punt. */
127 while (p < end && *p != '\0')
131 char *buf = alloca (p - file + 1);
133 memcpy (buf, file, p - file);
134 buf[p - file] = '\0';
135 warning (_("Non-null-terminated path in %s: %s"),
142 warning (_("Empty path in %s"), source_name);
146 opened = find_and_open_script (file, 1 /*search_path*/,
147 &stream, &full_path);
149 back_to = make_cleanup (null_cleanup, NULL);
152 make_cleanup_fclose (stream);
153 make_cleanup (xfree, full_path);
155 if (!file_is_auto_load_safe (full_path,
156 _("auto-load: Loading Python script "
157 "\"%s\" from section \"%s\" of "
158 "objfile \"%s\".\n"),
159 full_path, GDBPY_AUTO_SECTION_NAME,
160 objfile_name (objfile)))
167 /* We don't throw an error, the program is still debuggable. */
168 if (script_not_found_warning_print (pspace_info))
169 warning (_("Missing auto-load scripts referenced in section %s\n\
171 Use `info auto-load python [REGEXP]' to list them."),
172 GDBPY_AUTO_SECTION_NAME, objfile_name (objfile));
175 /* If one script isn't found it's not uncommon for more to not be
176 found either. We don't want to print an error message for each
177 script, too much noise. Instead, we print the warning once and tell
178 the user how to find the list of scripts that weren't loaded.
180 IWBN if complaints.c were more general-purpose. */
182 in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
183 &script_language_python);
185 /* If this file is not currently loaded, load it. */
186 if (opened && !in_hash_table)
187 source_python_script_for_objfile (objfile, stream, full_path);
189 do_cleanups (back_to);
193 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
196 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
198 bfd *abfd = objfile->obfd;
199 asection *scripts_sect;
200 bfd_byte *data = NULL;
202 scripts_sect = bfd_get_section_by_name (abfd, section_name);
203 if (scripts_sect == NULL)
206 if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
207 warning (_("Couldn't read %s section of %s"),
208 section_name, bfd_get_filename (abfd));
211 struct cleanup *cleanups;
212 char *p = (char *) data;
214 cleanups = make_cleanup (xfree, p);
215 source_section_scripts (objfile, section_name, p,
216 p + bfd_get_section_size (scripts_sect));
217 do_cleanups (cleanups);
221 /* Load any Python auto-loaded scripts for OBJFILE. */
224 gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
226 if (auto_load_python_scripts)
228 auto_load_objfile_script (objfile, &script_language_python);
229 auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
233 /* Wrapper for "info auto-load python-scripts". */
236 info_auto_load_python_scripts (char *pattern, int from_tty)
238 auto_load_info_scripts (pattern, from_tty, &script_language_python);
242 gdbpy_initialize_auto_load (void)
244 struct cmd_list_element *cmd;
245 const char *cmd_name;
247 add_setshow_boolean_cmd ("python-scripts", class_support,
248 &auto_load_python_scripts, _("\
249 Set the debugger's behaviour regarding auto-loaded Python scripts."), _("\
250 Show the debugger's behaviour regarding auto-loaded Python scripts."), _("\
251 If enabled, auto-loaded Python scripts are loaded when the debugger reads\n\
252 an executable or shared library.\n\
253 This options has security implications for untrusted inferiors."),
254 NULL, show_auto_load_python_scripts,
255 auto_load_set_cmdlist_get (),
256 auto_load_show_cmdlist_get ());
258 add_setshow_boolean_cmd ("auto-load-scripts", class_support,
259 &auto_load_python_scripts, _("\
260 Set the debugger's behaviour regarding auto-loaded Python scripts, "
263 Show the debugger's behaviour regarding auto-loaded Python scripts, "
265 NULL, NULL, show_auto_load_python_scripts,
266 &setlist, &showlist);
267 cmd_name = "auto-load-scripts";
268 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
269 deprecate_cmd (cmd, "set auto-load python-scripts");
271 /* It is needed because lookup_cmd updates the CMD_NAME pointer. */
272 cmd_name = "auto-load-scripts";
273 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
274 deprecate_cmd (cmd, "show auto-load python-scripts");
276 add_cmd ("python-scripts", class_info, info_auto_load_python_scripts,
277 _("Print the list of automatically loaded Python scripts.\n\
278 Usage: info auto-load python-scripts [REGEXP]"),
279 auto_load_info_cmdlist_get ());
281 cmd = add_info ("auto-load-scripts", info_auto_load_python_scripts, _("\
282 Print the list of automatically loaded Python scripts, deprecated."));
283 deprecate_cmd (cmd, "info auto-load python-scripts");
288 #else /* ! HAVE_PYTHON */
291 gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
295 #endif /* ! HAVE_PYTHON */