2012-05-09 Frank Ch. Eigler <fche@redhat.com>
[deliverable/binutils-gdb.git] / gdb / python / py-auto-load.c
CommitLineData
8a1ea21f
DE
1/* GDB routines for supporting auto-loaded scripts.
2
0b302171 3 Copyright (C) 2010-2012 Free Software Foundation, Inc.
8a1ea21f
DE
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"
8a1ea21f
DE
22#include "top.h"
23#include "exceptions.h"
8a1ea21f 24#include "gdbcmd.h"
8a1ea21f
DE
25#include "objfiles.h"
26#include "python.h"
8a1ea21f 27#include "cli/cli-cmds.h"
e2207b9a 28#include "auto-load.h"
88a1906b
DE
29
30#ifdef HAVE_PYTHON
31
32#include "python-internal.h"
33
8a1ea21f
DE
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
bf88dd68
JK
38/* The section to look for Python auto-loaded scripts (in file formats that
39 support sections).
8a1ea21f
DE
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
bf88dd68
JK
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. */
49static int auto_load_python_scripts = 1;
50
51static 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
57static void
58show_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
66static 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
71static void
72gdbpy_load_auto_script_for_objfile (struct objfile *objfile, FILE *file,
73 const char *filename)
74{
bccbefd2 75 int is_safe;
bf88dd68
JK
76 struct auto_load_pspace_info *pspace_info;
77
4dc84fd1
JK
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);
bccbefd2 82
bf88dd68
JK
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);
bccbefd2
JK
86 maybe_add_script (pspace_info, is_safe, filename, filename,
87 &script_language_python);
bf88dd68 88
bccbefd2
JK
89 if (is_safe)
90 source_python_script_for_objfile (objfile, file, filename);
bf88dd68 91}
8a1ea21f 92
8a1ea21f
DE
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
105static void
106source_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;
8a1ea21f 111
dbaefcf7 112 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
8a1ea21f
DE
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;
e97a38f7 120 struct cleanup *back_to;
8a1ea21f
DE
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);
d59b6f6c 136
8a1ea21f
DE
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
e97a38f7
JK
153 back_to = make_cleanup (null_cleanup, NULL);
154 if (opened)
155 {
156 make_cleanup_fclose (stream);
157 make_cleanup (xfree, full_path);
bccbefd2 158
4dc84fd1
JK
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))
bccbefd2 165 opened = 0;
e97a38f7 166 }
bf88dd68
JK
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\
174of file %s\n\
175Use `info auto-load python [REGEXP]' to list them."),
176 GDBPY_AUTO_SECTION_NAME, objfile->name);
177 }
e97a38f7 178
dbaefcf7
DE
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.
8a1ea21f 183
dbaefcf7 184 IWBN if complaints.c were more general-purpose. */
8a1ea21f 185
bccbefd2 186 in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
bf88dd68 187 &script_language_python);
8a1ea21f 188
bf88dd68
JK
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);
e97a38f7
JK
192
193 do_cleanups (back_to);
8a1ea21f
DE
194 }
195}
196
197/* Load scripts specified in section SECTION_NAME of OBJFILE. */
198
199static void
200auto_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
bf88dd68 226/* Load any Python auto-loaded scripts for OBJFILE. */
88a1906b
DE
227
228void
bf88dd68 229gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
88a1906b 230{
bf88dd68 231 if (auto_load_python_scripts)
8a1ea21f 232 {
bf88dd68 233 auto_load_objfile_script (objfile, &script_language_python);
8a1ea21f
DE
234 auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
235 }
236}
bf88dd68
JK
237
238/* Wrapper for "info auto-load python-scripts". */
239
240static void
241info_auto_load_python_scripts (char *pattern, int from_tty)
242{
243 auto_load_info_scripts (pattern, from_tty, &script_language_python);
244}
8a1ea21f 245\f
8a1ea21f
DE
246void
247gdbpy_initialize_auto_load (void)
248{
bf88dd68
JK
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, _("\
254Set the debugger's behaviour regarding auto-loaded Python scripts."), _("\
255Show the debugger's behaviour regarding auto-loaded Python scripts."), _("\
256If enabled, auto-loaded Python scripts are loaded when the debugger reads\n\
257an executable or shared library.\n\
258This 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
a86caf66 263 add_setshow_boolean_cmd ("auto-load-scripts", class_support,
bf88dd68
JK
264 &auto_load_python_scripts, _("\
265Set the debugger's behaviour regarding auto-loaded Python scripts, "
266 "deprecated."),
267 _("\
268Show 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\
283Usage: 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, _("\
287Print the list of automatically loaded Python scripts, deprecated."));
288 deprecate_cmd (cmd, "info auto-load python-scripts");
8a1ea21f 289}
88a1906b
DE
290
291#else /* ! HAVE_PYTHON */
292
293void
bf88dd68 294gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
88a1906b
DE
295{
296}
297
298#endif /* ! HAVE_PYTHON */
This page took 0.249004 seconds and 4 git commands to generate.