* python/py-arch.c (gdbpy_initialize_arch): Return 'int'.
[platform/upstream/binutils.git] / gdb / python / py-auto-load.c
1 /* GDB routines for supporting auto-loaded scripts.
2
3    Copyright (C) 2010-2013 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 section to look for Python auto-loaded scripts (in file formats that
35    support sections).
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"
40
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;
46
47 static void gdbpy_load_auto_script_for_objfile (struct objfile *objfile,
48                                                 FILE *file,
49                                                 const char *filename);
50
51 /* "show" command for the auto_load_python_scripts configuration variable.  */
52
53 static void
54 show_auto_load_python_scripts (struct ui_file *file, int from_tty,
55                                struct cmd_list_element *c, const char *value)
56 {
57   fprintf_filtered (file, _("Auto-loading of Python scripts is %s.\n"), value);
58 }
59
60 /* Definition of script language for Python scripts.  */
61
62 static const struct script_language script_language_python
63   = { GDBPY_AUTO_FILE_NAME, gdbpy_load_auto_script_for_objfile };
64
65 /* Wrapper of source_python_script_for_objfile for script_language_python.  */
66
67 static void
68 gdbpy_load_auto_script_for_objfile (struct objfile *objfile, FILE *file,
69                                     const char *filename)
70 {
71   int is_safe;
72   struct auto_load_pspace_info *pspace_info;
73
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);
78
79   /* Add this script to the hash table too so "info auto-load python-scripts"
80      can print it.  */
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);
84
85   if (is_safe)
86     source_python_script_for_objfile (objfile, file, filename);
87 }
88
89 /* Load scripts specified in OBJFILE.
90    START,END delimit a buffer containing a list of nul-terminated
91    file names.
92    SOURCE_NAME is used in error messages.
93
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.
97
98    The section contains a list of path names of files containing
99    python code to load.  Each path is null-terminated.  */
100
101 static void
102 source_section_scripts (struct objfile *objfile, const char *source_name,
103                         const char *start, const char *end)
104 {
105   const char *p;
106   struct auto_load_pspace_info *pspace_info;
107
108   pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
109
110   for (p = start; p < end; ++p)
111     {
112       const char *file;
113       FILE *stream;
114       char *full_path;
115       int opened, in_hash_table;
116       struct cleanup *back_to;
117
118       if (*p != 1)
119         {
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.  */
123           break;
124         }
125       file = ++p;
126
127       while (p < end && *p != '\0')
128         ++p;
129       if (p == end)
130         {
131           char *buf = alloca (p - file + 1);
132
133           memcpy (buf, file, p - file);
134           buf[p - file] = '\0';
135           warning (_("Non-null-terminated path in %s: %s"),
136                    source_name, buf);
137           /* Don't load it.  */
138           break;
139         }
140       if (p == file)
141         {
142           warning (_("Empty path in %s"), source_name);
143           continue;
144         }
145
146       opened = find_and_open_script (file, 1 /*search_path*/,
147                                      &stream, &full_path);
148
149       back_to = make_cleanup (null_cleanup, NULL);
150       if (opened)
151         {
152           make_cleanup_fclose (stream);
153           make_cleanup (xfree, full_path);
154
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))
161             opened = 0;
162         }
163       else
164         {
165           full_path = NULL;
166
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\
170 of file %s\n\
171 Use `info auto-load python [REGEXP]' to list them."),
172                      GDBPY_AUTO_SECTION_NAME, objfile->name);
173         }
174
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.
179
180          IWBN if complaints.c were more general-purpose.  */
181
182       in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
183                                         &script_language_python);
184
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);
188
189       do_cleanups (back_to);
190     }
191 }
192
193 /* Load scripts specified in section SECTION_NAME of OBJFILE.  */
194
195 static void
196 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
197 {
198   bfd *abfd = objfile->obfd;
199   asection *scripts_sect;
200   bfd_byte *data = NULL;
201
202   scripts_sect = bfd_get_section_by_name (abfd, section_name);
203   if (scripts_sect == NULL)
204     return;
205
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));
209   else
210     {
211       struct cleanup *cleanups;
212       char *p = (char *) data;
213
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);
218     }
219 }
220
221 /* Load any Python auto-loaded scripts for OBJFILE.  */
222
223 void
224 gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
225 {
226   if (auto_load_python_scripts)
227     {
228       auto_load_objfile_script (objfile, &script_language_python);
229       auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
230     }
231 }
232
233 /* Wrapper for "info auto-load python-scripts".  */
234
235 static void
236 info_auto_load_python_scripts (char *pattern, int from_tty)
237 {
238   auto_load_info_scripts (pattern, from_tty, &script_language_python);
239 }
240 \f
241 int
242 gdbpy_initialize_auto_load (void)
243 {
244   struct cmd_list_element *cmd;
245   const char *cmd_name;
246
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 ());
257
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, "
261                                                                  "deprecated."),
262                            _("\
263 Show the debugger's behaviour regarding auto-loaded Python scripts, "
264                                                                  "deprecated."),
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");
270
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");
275
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 ());
280
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");
284
285   return 0;
286 }
287
288 #else /* ! HAVE_PYTHON */
289
290 void
291 gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
292 {
293 }
294
295 #endif /* ! HAVE_PYTHON */