Remove sp_regnum_from_eax and pc_regnum_from_eax
[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
bf88dd68
JK
34/* The section to look for Python auto-loaded scripts (in file formats that
35 support sections).
8a1ea21f
DE
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
bf88dd68
JK
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. */
45static int auto_load_python_scripts = 1;
46
47static 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
53static void
54show_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
62static 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
67static void
68gdbpy_load_auto_script_for_objfile (struct objfile *objfile, FILE *file,
69 const char *filename)
70{
bccbefd2 71 int is_safe;
bf88dd68
JK
72 struct auto_load_pspace_info *pspace_info;
73
4dc84fd1
JK
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);
bccbefd2 78
bf88dd68
JK
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);
bccbefd2
JK
82 maybe_add_script (pspace_info, is_safe, filename, filename,
83 &script_language_python);
bf88dd68 84
bccbefd2
JK
85 if (is_safe)
86 source_python_script_for_objfile (objfile, file, filename);
bf88dd68 87}
8a1ea21f 88
8a1ea21f
DE
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
101static void
102source_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;
8a1ea21f 107
dbaefcf7 108 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
8a1ea21f
DE
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;
e97a38f7 116 struct cleanup *back_to;
8a1ea21f
DE
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);
d59b6f6c 132
8a1ea21f
DE
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
e97a38f7
JK
149 back_to = make_cleanup (null_cleanup, NULL);
150 if (opened)
151 {
152 make_cleanup_fclose (stream);
153 make_cleanup (xfree, full_path);
bccbefd2 154
4dc84fd1
JK
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))
bccbefd2 161 opened = 0;
e97a38f7 162 }
bf88dd68
JK
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\
170of file %s\n\
171Use `info auto-load python [REGEXP]' to list them."),
172 GDBPY_AUTO_SECTION_NAME, objfile->name);
173 }
e97a38f7 174
dbaefcf7
DE
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.
8a1ea21f 179
dbaefcf7 180 IWBN if complaints.c were more general-purpose. */
8a1ea21f 181
bccbefd2 182 in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
bf88dd68 183 &script_language_python);
8a1ea21f 184
bf88dd68
JK
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);
e97a38f7
JK
188
189 do_cleanups (back_to);
8a1ea21f
DE
190 }
191}
192
193/* Load scripts specified in section SECTION_NAME of OBJFILE. */
194
195static void
196auto_load_section_scripts (struct objfile *objfile, const char *section_name)
197{
198 bfd *abfd = objfile->obfd;
199 asection *scripts_sect;
200 bfd_size_type size;
201 char *p;
202 struct cleanup *cleanups;
203
204 scripts_sect = bfd_get_section_by_name (abfd, section_name);
205 if (scripts_sect == NULL)
206 return;
207
208 size = bfd_get_section_size (scripts_sect);
209 p = xmalloc (size);
210
211 cleanups = make_cleanup (xfree, p);
212
213 if (bfd_get_section_contents (abfd, scripts_sect, p, (file_ptr) 0, size))
214 source_section_scripts (objfile, section_name, p, p + size);
215 else
216 warning (_("Couldn't read %s section of %s"),
217 section_name, bfd_get_filename (abfd));
218
219 do_cleanups (cleanups);
220}
221
bf88dd68 222/* Load any Python auto-loaded scripts for OBJFILE. */
88a1906b
DE
223
224void
bf88dd68 225gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
88a1906b 226{
bf88dd68 227 if (auto_load_python_scripts)
8a1ea21f 228 {
bf88dd68 229 auto_load_objfile_script (objfile, &script_language_python);
8a1ea21f
DE
230 auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
231 }
232}
bf88dd68
JK
233
234/* Wrapper for "info auto-load python-scripts". */
235
236static void
237info_auto_load_python_scripts (char *pattern, int from_tty)
238{
239 auto_load_info_scripts (pattern, from_tty, &script_language_python);
240}
8a1ea21f 241\f
8a1ea21f
DE
242void
243gdbpy_initialize_auto_load (void)
244{
bf88dd68
JK
245 struct cmd_list_element *cmd;
246 char *cmd_name;
247
248 add_setshow_boolean_cmd ("python-scripts", class_support,
249 &auto_load_python_scripts, _("\
250Set the debugger's behaviour regarding auto-loaded Python scripts."), _("\
251Show the debugger's behaviour regarding auto-loaded Python scripts."), _("\
252If enabled, auto-loaded Python scripts are loaded when the debugger reads\n\
253an executable or shared library.\n\
254This options has security implications for untrusted inferiors."),
255 NULL, show_auto_load_python_scripts,
256 auto_load_set_cmdlist_get (),
257 auto_load_show_cmdlist_get ());
258
a86caf66 259 add_setshow_boolean_cmd ("auto-load-scripts", class_support,
bf88dd68
JK
260 &auto_load_python_scripts, _("\
261Set the debugger's behaviour regarding auto-loaded Python scripts, "
262 "deprecated."),
263 _("\
264Show the debugger's behaviour regarding auto-loaded Python scripts, "
265 "deprecated."),
266 NULL, NULL, show_auto_load_python_scripts,
267 &setlist, &showlist);
268 cmd_name = "auto-load-scripts";
269 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
270 deprecate_cmd (cmd, "set auto-load python-scripts");
271
272 /* It is needed because lookup_cmd updates the CMD_NAME pointer. */
273 cmd_name = "auto-load-scripts";
274 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
275 deprecate_cmd (cmd, "show auto-load python-scripts");
276
277 add_cmd ("python-scripts", class_info, info_auto_load_python_scripts,
278 _("Print the list of automatically loaded Python scripts.\n\
279Usage: info auto-load python-scripts [REGEXP]"),
280 auto_load_info_cmdlist_get ());
281
282 cmd = add_info ("auto-load-scripts", info_auto_load_python_scripts, _("\
283Print the list of automatically loaded Python scripts, deprecated."));
284 deprecate_cmd (cmd, "info auto-load python-scripts");
8a1ea21f 285}
88a1906b
DE
286
287#else /* ! HAVE_PYTHON */
288
289void
bf88dd68 290gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
88a1906b
DE
291{
292}
293
294#endif /* ! HAVE_PYTHON */
This page took 0.27489 seconds and 4 git commands to generate.