]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/py-auto-load.c
8512129c707b8c83040a34f8c4785d6b4f12cea0
[thirdparty/binutils-gdb.git] / gdb / python / py-auto-load.c
1 /* GDB routines for supporting auto-loaded scripts.
2
3 Copyright (C) 2010-2012 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "gdb_string.h"
22 #include "top.h"
23 #include "exceptions.h"
24 #include "gdbcmd.h"
25 #include "objfiles.h"
26 #include "python.h"
27 #include "cli/cli-cmds.h"
28 #include "auto-load.h"
29
30 #ifdef HAVE_PYTHON
31
32 #include "python-internal.h"
33
34 /* The suffix of per-objfile scripts to auto-load.
35 E.g. When the program loads libfoo.so, look for libfoo-gdb.py. */
36 #define GDBPY_AUTO_FILE_NAME "-gdb.py"
37
38 /* The section to look for Python auto-loaded scripts (in file formats that
39 support sections).
40 Each entry in this section is a byte of value 1, and then the nul-terminated
41 name of the script. The script name may include a directory.
42 The leading byte is to allow upward compatible extensions. */
43 #define GDBPY_AUTO_SECTION_NAME ".debug_gdb_scripts"
44
45 /* User-settable option to enable/disable auto-loading of Python scripts:
46 set auto-load python-scripts on|off
47 This is true if we should auto-load associated Python scripts when an
48 objfile is opened, false otherwise. */
49 static int auto_load_python_scripts = 1;
50
51 static void gdbpy_load_auto_script_for_objfile (struct objfile *objfile,
52 FILE *file,
53 const char *filename);
54
55 /* "show" command for the auto_load_python_scripts configuration variable. */
56
57 static void
58 show_auto_load_python_scripts (struct ui_file *file, int from_tty,
59 struct cmd_list_element *c, const char *value)
60 {
61 fprintf_filtered (file, _("Auto-loading of Python scripts is %s.\n"), value);
62 }
63
64 /* Definition of script language for Python scripts. */
65
66 static const struct script_language script_language_python
67 = { GDBPY_AUTO_FILE_NAME, gdbpy_load_auto_script_for_objfile };
68
69 /* Wrapper of source_python_script_for_objfile for script_language_python. */
70
71 static void
72 gdbpy_load_auto_script_for_objfile (struct objfile *objfile, FILE *file,
73 const char *filename)
74 {
75 int is_safe;
76 struct auto_load_pspace_info *pspace_info;
77
78 is_safe = file_is_auto_load_safe (filename,
79 _("auto-load: Loading Python script \"%s\" "
80 "by extension for objfile \"%s\".\n"),
81 filename, objfile->name);
82
83 /* Add this script to the hash table too so "info auto-load python-scripts"
84 can print it. */
85 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
86 maybe_add_script (pspace_info, is_safe, filename, filename,
87 &script_language_python);
88
89 if (is_safe)
90 source_python_script_for_objfile (objfile, file, filename);
91 }
92
93 /* Load scripts specified in OBJFILE.
94 START,END delimit a buffer containing a list of nul-terminated
95 file names.
96 SOURCE_NAME is used in error messages.
97
98 Scripts are found per normal "source -s" command processing.
99 First the script is looked for in $cwd. If not found there the
100 source search path is used.
101
102 The section contains a list of path names of files containing
103 python code to load. Each path is null-terminated. */
104
105 static void
106 source_section_scripts (struct objfile *objfile, const char *source_name,
107 const char *start, const char *end)
108 {
109 const char *p;
110 struct auto_load_pspace_info *pspace_info;
111
112 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
113
114 for (p = start; p < end; ++p)
115 {
116 const char *file;
117 FILE *stream;
118 char *full_path;
119 int opened, in_hash_table;
120 struct cleanup *back_to;
121
122 if (*p != 1)
123 {
124 warning (_("Invalid entry in %s section"), GDBPY_AUTO_SECTION_NAME);
125 /* We could try various heuristics to find the next valid entry,
126 but it's safer to just punt. */
127 break;
128 }
129 file = ++p;
130
131 while (p < end && *p != '\0')
132 ++p;
133 if (p == end)
134 {
135 char *buf = alloca (p - file + 1);
136
137 memcpy (buf, file, p - file);
138 buf[p - file] = '\0';
139 warning (_("Non-null-terminated path in %s: %s"),
140 source_name, buf);
141 /* Don't load it. */
142 break;
143 }
144 if (p == file)
145 {
146 warning (_("Empty path in %s"), source_name);
147 continue;
148 }
149
150 opened = find_and_open_script (file, 1 /*search_path*/,
151 &stream, &full_path);
152
153 back_to = make_cleanup (null_cleanup, NULL);
154 if (opened)
155 {
156 make_cleanup_fclose (stream);
157 make_cleanup (xfree, full_path);
158
159 if (!file_is_auto_load_safe (full_path,
160 _("auto-load: Loading Python script "
161 "\"%s\" from section \"%s\" of "
162 "objfile \"%s\".\n"),
163 full_path, GDBPY_AUTO_SECTION_NAME,
164 objfile->name))
165 opened = 0;
166 }
167 else
168 {
169 full_path = NULL;
170
171 /* We don't throw an error, the program is still debuggable. */
172 if (script_not_found_warning_print (pspace_info))
173 warning (_("Missing auto-load scripts referenced in section %s\n\
174 of file %s\n\
175 Use `info auto-load python [REGEXP]' to list them."),
176 GDBPY_AUTO_SECTION_NAME, objfile->name);
177 }
178
179 /* If one script isn't found it's not uncommon for more to not be
180 found either. We don't want to print an error message for each
181 script, too much noise. Instead, we print the warning once and tell
182 the user how to find the list of scripts that weren't loaded.
183
184 IWBN if complaints.c were more general-purpose. */
185
186 in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
187 &script_language_python);
188
189 /* If this file is not currently loaded, load it. */
190 if (opened && !in_hash_table)
191 source_python_script_for_objfile (objfile, stream, full_path);
192
193 do_cleanups (back_to);
194 }
195 }
196
197 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
198
199 static void
200 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
201 {
202 bfd *abfd = objfile->obfd;
203 asection *scripts_sect;
204 bfd_size_type size;
205 char *p;
206 struct cleanup *cleanups;
207
208 scripts_sect = bfd_get_section_by_name (abfd, section_name);
209 if (scripts_sect == NULL)
210 return;
211
212 size = bfd_get_section_size (scripts_sect);
213 p = xmalloc (size);
214
215 cleanups = make_cleanup (xfree, p);
216
217 if (bfd_get_section_contents (abfd, scripts_sect, p, (file_ptr) 0, size))
218 source_section_scripts (objfile, section_name, p, p + size);
219 else
220 warning (_("Couldn't read %s section of %s"),
221 section_name, bfd_get_filename (abfd));
222
223 do_cleanups (cleanups);
224 }
225
226 /* Load any Python auto-loaded scripts for OBJFILE. */
227
228 void
229 gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
230 {
231 if (auto_load_python_scripts)
232 {
233 auto_load_objfile_script (objfile, &script_language_python);
234 auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
235 }
236 }
237
238 /* Wrapper for "info auto-load python-scripts". */
239
240 static void
241 info_auto_load_python_scripts (char *pattern, int from_tty)
242 {
243 auto_load_info_scripts (pattern, from_tty, &script_language_python);
244 }
245 \f
246 void
247 gdbpy_initialize_auto_load (void)
248 {
249 struct cmd_list_element *cmd;
250 char *cmd_name;
251
252 add_setshow_boolean_cmd ("python-scripts", class_support,
253 &auto_load_python_scripts, _("\
254 Set the debugger's behaviour regarding auto-loaded Python scripts."), _("\
255 Show the debugger's behaviour regarding auto-loaded Python scripts."), _("\
256 If enabled, auto-loaded Python scripts are loaded when the debugger reads\n\
257 an executable or shared library.\n\
258 This options has security implications for untrusted inferiors."),
259 NULL, show_auto_load_python_scripts,
260 auto_load_set_cmdlist_get (),
261 auto_load_show_cmdlist_get ());
262
263 add_setshow_boolean_cmd ("auto-load-scripts", class_support,
264 &auto_load_python_scripts, _("\
265 Set the debugger's behaviour regarding auto-loaded Python scripts, "
266 "deprecated."),
267 _("\
268 Show the debugger's behaviour regarding auto-loaded Python scripts, "
269 "deprecated."),
270 NULL, NULL, show_auto_load_python_scripts,
271 &setlist, &showlist);
272 cmd_name = "auto-load-scripts";
273 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
274 deprecate_cmd (cmd, "set auto-load python-scripts");
275
276 /* It is needed because lookup_cmd updates the CMD_NAME pointer. */
277 cmd_name = "auto-load-scripts";
278 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
279 deprecate_cmd (cmd, "show auto-load python-scripts");
280
281 add_cmd ("python-scripts", class_info, info_auto_load_python_scripts,
282 _("Print the list of automatically loaded Python scripts.\n\
283 Usage: info auto-load python-scripts [REGEXP]"),
284 auto_load_info_cmdlist_get ());
285
286 cmd = add_info ("auto-load-scripts", info_auto_load_python_scripts, _("\
287 Print the list of automatically loaded Python scripts, deprecated."));
288 deprecate_cmd (cmd, "info auto-load python-scripts");
289 }
290
291 #else /* ! HAVE_PYTHON */
292
293 void
294 gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
295 {
296 }
297
298 #endif /* ! HAVE_PYTHON */