Code cleanup: Add objfile_name accessor
[deliverable/binutils-gdb.git] / gdb / python / py-auto-load.c
CommitLineData
8a1ea21f
DE
1/* GDB routines for supporting auto-loaded scripts.
2
28e7fd62 3 Copyright (C) 2010-2013 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"),
4262abfb 77 filename, objfile_name (objfile));
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,
4262abfb 160 objfile_name (objfile)))
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."),
4262abfb 172 GDBPY_AUTO_SECTION_NAME, objfile_name (objfile));
bf88dd68 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;
ea9f10bb 200 bfd_byte *data = NULL;
8a1ea21f
DE
201
202 scripts_sect = bfd_get_section_by_name (abfd, section_name);
203 if (scripts_sect == NULL)
204 return;
205
ea9f10bb 206 if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
8a1ea21f
DE
207 warning (_("Couldn't read %s section of %s"),
208 section_name, bfd_get_filename (abfd));
ea9f10bb
TT
209 else
210 {
211 struct cleanup *cleanups;
212 char *p = (char *) data;
8a1ea21f 213
ea9f10bb
TT
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 }
8a1ea21f
DE
219}
220
bf88dd68 221/* Load any Python auto-loaded scripts for OBJFILE. */
88a1906b
DE
222
223void
bf88dd68 224gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
88a1906b 225{
bf88dd68 226 if (auto_load_python_scripts)
8a1ea21f 227 {
bf88dd68 228 auto_load_objfile_script (objfile, &script_language_python);
8a1ea21f
DE
229 auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
230 }
231}
bf88dd68
JK
232
233/* Wrapper for "info auto-load python-scripts". */
234
235static void
236info_auto_load_python_scripts (char *pattern, int from_tty)
237{
238 auto_load_info_scripts (pattern, from_tty, &script_language_python);
239}
8a1ea21f 240\f
999633ed 241int
8a1ea21f
DE
242gdbpy_initialize_auto_load (void)
243{
bf88dd68 244 struct cmd_list_element *cmd;
6f937416 245 const char *cmd_name;
bf88dd68
JK
246
247 add_setshow_boolean_cmd ("python-scripts", class_support,
248 &auto_load_python_scripts, _("\
249Set the debugger's behaviour regarding auto-loaded Python scripts."), _("\
250Show the debugger's behaviour regarding auto-loaded Python scripts."), _("\
251If enabled, auto-loaded Python scripts are loaded when the debugger reads\n\
252an executable or shared library.\n\
253This 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
a86caf66 258 add_setshow_boolean_cmd ("auto-load-scripts", class_support,
bf88dd68
JK
259 &auto_load_python_scripts, _("\
260Set the debugger's behaviour regarding auto-loaded Python scripts, "
261 "deprecated."),
262 _("\
263Show 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\
278Usage: 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, _("\
282Print the list of automatically loaded Python scripts, deprecated."));
283 deprecate_cmd (cmd, "info auto-load python-scripts");
999633ed
TT
284
285 return 0;
8a1ea21f 286}
88a1906b
DE
287
288#else /* ! HAVE_PYTHON */
289
290void
bf88dd68 291gdbpy_load_auto_scripts_for_objfile (struct objfile *objfile)
88a1906b
DE
292{
293}
294
295#endif /* ! HAVE_PYTHON */
This page took 0.424069 seconds and 4 git commands to generate.