OLD | NEW |
1 /* GDB routines for supporting auto-loaded scripts. | 1 /* GDB routines for supporting auto-loaded scripts. |
2 | 2 |
3 Copyright (C) 2010-2012 Free Software Foundation, Inc. | 3 Copyright (C) 2010-2012 Free Software Foundation, Inc. |
4 | 4 |
5 This file is part of GDB. | 5 This file is part of GDB. |
6 | 6 |
7 This program is free software; you can redistribute it and/or modify | 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 | 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 | 9 the Free Software Foundation; either version 3 of the License, or |
10 (at your option) any later version. | 10 (at your option) any later version. |
11 | 11 |
12 This program is distributed in the hope that it will be useful, | 12 This program is distributed in the hope that it will be useful, |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 GNU General Public License for more details. | 15 GNU General Public License for more details. |
16 | 16 |
17 You should have received a copy of the GNU General Public License | 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/>. */ | 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
19 | 19 |
20 #include "defs.h" | 20 #include "defs.h" |
21 #include "filenames.h" | |
22 #include "gdb_string.h" | 21 #include "gdb_string.h" |
23 #include "gdb_regex.h" | |
24 #include "top.h" | 22 #include "top.h" |
25 #include "exceptions.h" | 23 #include "exceptions.h" |
26 #include "command.h" | |
27 #include "gdbcmd.h" | 24 #include "gdbcmd.h" |
28 #include "observer.h" | |
29 #include "progspace.h" | |
30 #include "objfiles.h" | 25 #include "objfiles.h" |
31 #include "python.h" | 26 #include "python.h" |
32 #include "cli/cli-cmds.h" | 27 #include "cli/cli-cmds.h" |
33 | 28 #include "auto-load.h" |
34 /* Internal-use flag to enable/disable auto-loading. | |
35 This is true if we should auto-load python code when an objfile is opened, | |
36 false otherwise. | |
37 | |
38 Both auto_load_scripts && gdbpy_global_auto_load must be true to enable | |
39 auto-loading. | |
40 | |
41 This flag exists to facilitate deferring auto-loading during start-up | |
42 until after ./.gdbinit has been read; it may augment the search directories | |
43 used to find the scripts. */ | |
44 int gdbpy_global_auto_load = 1; | |
45 | 29 |
46 #ifdef HAVE_PYTHON | 30 #ifdef HAVE_PYTHON |
47 | 31 |
48 #include "python-internal.h" | 32 #include "python-internal.h" |
49 | 33 |
50 /* NOTE: It's trivial to also support auto-loading normal gdb scripts. | 34 /* The section to look for Python auto-loaded scripts (in file formats that |
51 There has yet to be a need so it's not implemented. */ | 35 support sections). |
52 | |
53 /* The suffix of per-objfile scripts to auto-load. | |
54 E.g. When the program loads libfoo.so, look for libfoo-gdb.py. */ | |
55 #define GDBPY_AUTO_FILE_NAME "-gdb.py" | |
56 | |
57 /* The section to look for scripts (in file formats that support sections). | |
58 Each entry in this section is a byte of value 1, and then the nul-terminated | 36 Each entry in this section is a byte of value 1, and then the nul-terminated |
59 name of the script. The script name may include a directory. | 37 name of the script. The script name may include a directory. |
60 The leading byte is to allow upward compatible extensions. */ | 38 The leading byte is to allow upward compatible extensions. */ |
61 #define GDBPY_AUTO_SECTION_NAME ".debug_gdb_scripts" | 39 #define GDBPY_AUTO_SECTION_NAME ".debug_gdb_scripts" |
62 | 40 |
63 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load | 41 /* User-settable option to enable/disable auto-loading of Python scripts: |
64 the same script. There's no point in loading the script multiple times, | 42 set auto-load python-scripts on|off |
65 and there can be a lot of objfiles and scripts, so we keep track of scripts | 43 This is true if we should auto-load associated Python scripts when an |
66 loaded this way. */ | 44 objfile is opened, false otherwise. */ |
| 45 static int auto_load_python_scripts = 1; |
67 | 46 |
68 struct auto_load_pspace_info | 47 static void gdbpy_load_auto_script_for_objfile (struct objfile *objfile, |
69 { | 48 » » » » » » FILE *file, |
70 /* For each program space we keep track of loaded scripts. */ | 49 » » » » » » const char *filename); |
71 struct htab *loaded_scripts; | |
72 | 50 |
73 /* Non-zero if we've issued the warning about an auto-load script not being | 51 /* "show" command for the auto_load_python_scripts configuration variable. */ |
74 found. We only want to issue this warning once. */ | |
75 int script_not_found_warning_printed; | |
76 }; | |
77 | |
78 /* Objects of this type are stored in the loaded script hash table. */ | |
79 | |
80 struct loaded_script | |
81 { | |
82 /* Name as provided by the objfile. */ | |
83 const char *name; | |
84 /* Full path name or NULL if script wasn't found (or was otherwise | |
85 inaccessible). */ | |
86 const char *full_path; | |
87 }; | |
88 | |
89 /* User-settable option to enable/disable auto-loading: | |
90 set auto-load-scripts on|off | |
91 This is true if we should auto-load associated scripts when an objfile | |
92 is opened, false otherwise. | |
93 At the moment, this only affects python scripts, but there's no reason | |
94 one couldn't also have other kinds of auto-loaded scripts, and there's | |
95 no reason to have them each controlled by a separate flag. | |
96 So we elide "python" from the name here and in the option. | |
97 The fact that it lives here is just an implementation detail. */ | |
98 static int auto_load_scripts = 1; | |
99 | |
100 /* Per-program-space data key. */ | |
101 static const struct program_space_data *auto_load_pspace_data; | |
102 | 52 |
103 static void | 53 static void |
104 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg) | 54 show_auto_load_python_scripts (struct ui_file *file, int from_tty, |
| 55 » » » struct cmd_list_element *c, const char *value) |
105 { | 56 { |
106 struct auto_load_pspace_info *info; | 57 fprintf_filtered (file, _("Auto-loading of Python scripts is %s.\n"), value); |
107 | |
108 info = program_space_data (pspace, auto_load_pspace_data); | |
109 if (info != NULL) | |
110 { | |
111 if (info->loaded_scripts) | |
112 » htab_delete (info->loaded_scripts); | |
113 xfree (info); | |
114 } | |
115 } | 58 } |
116 | 59 |
117 /* Get the current autoload data. If none is found yet, add it now. This | 60 /* Definition of script language for Python scripts. */ |
118 function always returns a valid object. */ | |
119 | 61 |
120 static struct auto_load_pspace_info * | 62 static const struct script_language script_language_python |
121 get_auto_load_pspace_data (struct program_space *pspace) | 63 = { GDBPY_AUTO_FILE_NAME, gdbpy_load_auto_script_for_objfile }; |
122 { | |
123 struct auto_load_pspace_info *info; | |
124 | 64 |
125 info = program_space_data (pspace, auto_load_pspace_data); | 65 /* Wrapper of source_python_script_for_objfile for script_language_python. */ |
126 if (info == NULL) | |
127 { | |
128 info = XZALLOC (struct auto_load_pspace_info); | |
129 set_program_space_data (pspace, auto_load_pspace_data, info); | |
130 } | |
131 | |
132 return info; | |
133 } | |
134 | |
135 /* Hash function for the loaded script hash. */ | |
136 | |
137 static hashval_t | |
138 hash_loaded_script_entry (const void *data) | |
139 { | |
140 const struct loaded_script *e = data; | |
141 | |
142 return htab_hash_string (e->name); | |
143 } | |
144 | |
145 /* Equality function for the loaded script hash. */ | |
146 | |
147 static int | |
148 eq_loaded_script_entry (const void *a, const void *b) | |
149 { | |
150 const struct loaded_script *ea = a; | |
151 const struct loaded_script *eb = b; | |
152 | |
153 return strcmp (ea->name, eb->name) == 0; | |
154 } | |
155 | |
156 /* Initialize the table to track loaded scripts. | |
157 Each entry is hashed by the full path name. */ | |
158 | 66 |
159 static void | 67 static void |
160 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info) | 68 gdbpy_load_auto_script_for_objfile (struct objfile *objfile, FILE *file, |
| 69 » » » » const char *filename) |
161 { | 70 { |
162 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily. | 71 int is_safe; |
163 Space for each entry is obtained with one malloc so we can free them | 72 struct auto_load_pspace_info *pspace_info; |
164 easily. */ | |
165 | 73 |
166 pspace_info->loaded_scripts = htab_create (31, | 74 is_safe = file_is_auto_load_safe (filename, |
167 » » » » » hash_loaded_script_entry, | 75 » » » » _("auto-load: Loading Python script \"%s\" " |
168 » » » » » eq_loaded_script_entry, | 76 » » » » "by extension for objfile \"%s\".\n"), |
169 » » » » » xfree); | 77 » » » » filename, objfile->name); |
170 | 78 |
171 pspace_info->script_not_found_warning_printed = FALSE; | 79 /* Add this script to the hash table too so "info auto-load python-scripts" |
172 } | 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); |
173 | 84 |
174 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table | 85 if (is_safe) |
175 for loading scripts. */ | 86 source_python_script_for_objfile (objfile, file, filename); |
176 | |
177 static struct auto_load_pspace_info * | |
178 get_auto_load_pspace_data_for_loading (struct program_space *pspace) | |
179 { | |
180 struct auto_load_pspace_info *info; | |
181 | |
182 info = get_auto_load_pspace_data (pspace); | |
183 if (info->loaded_scripts == NULL) | |
184 init_loaded_scripts_info (info); | |
185 | |
186 return info; | |
187 } | |
188 | |
189 /* Add script NAME to hash table HTAB. | |
190 FULL_PATH is NULL if the script wasn't found. | |
191 The result is true if the script was already in the hash table. */ | |
192 | |
193 static int | |
194 maybe_add_script (struct htab *htab, const char *name, const char *full_path) | |
195 { | |
196 struct loaded_script **slot, entry; | |
197 int in_hash_table; | |
198 | |
199 entry.name = name; | |
200 entry.full_path = full_path; | |
201 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT); | |
202 in_hash_table = *slot != NULL; | |
203 | |
204 /* If this script is not in the hash table, add it. */ | |
205 | |
206 if (! in_hash_table) | |
207 { | |
208 char *p; | |
209 | |
210 /* Allocate all space in one chunk so it's easier to free. */ | |
211 *slot = xmalloc (sizeof (**slot) | |
212 » » + strlen (name) + 1 | |
213 » » + (full_path != NULL ? (strlen (full_path) + 1) : 0)); | |
214 p = ((char*) *slot) + sizeof (**slot); | |
215 strcpy (p, name); | |
216 (*slot)->name = p; | |
217 if (full_path != NULL) | |
218 » { | |
219 » p += strlen (p) + 1; | |
220 » strcpy (p, full_path); | |
221 » (*slot)->full_path = p; | |
222 » } | |
223 else | |
224 » (*slot)->full_path = NULL; | |
225 } | |
226 | |
227 return in_hash_table; | |
228 } | 87 } |
229 | 88 |
230 /* Load scripts specified in OBJFILE. | 89 /* Load scripts specified in OBJFILE. |
231 START,END delimit a buffer containing a list of nul-terminated | 90 START,END delimit a buffer containing a list of nul-terminated |
232 file names. | 91 file names. |
233 SOURCE_NAME is used in error messages. | 92 SOURCE_NAME is used in error messages. |
234 | 93 |
235 Scripts are found per normal "source -s" command processing. | 94 Scripts are found per normal "source -s" command processing. |
236 First the script is looked for in $cwd. If not found there the | 95 First the script is looked for in $cwd. If not found there the |
237 source search path is used. | 96 source search path is used. |
238 | 97 |
239 The section contains a list of path names of files containing | 98 The section contains a list of path names of files containing |
240 python code to load. Each path is null-terminated. */ | 99 python code to load. Each path is null-terminated. */ |
241 | 100 |
242 static void | 101 static void |
243 source_section_scripts (struct objfile *objfile, const char *source_name, | 102 source_section_scripts (struct objfile *objfile, const char *source_name, |
244 const char *start, const char *end) | 103 const char *start, const char *end) |
245 { | 104 { |
246 const char *p; | 105 const char *p; |
247 struct auto_load_pspace_info *pspace_info; | 106 struct auto_load_pspace_info *pspace_info; |
248 | 107 |
249 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space); | 108 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space); |
250 | 109 |
251 for (p = start; p < end; ++p) | 110 for (p = start; p < end; ++p) |
252 { | 111 { |
253 const char *file; | 112 const char *file; |
254 FILE *stream; | 113 FILE *stream; |
255 char *full_path; | 114 char *full_path; |
256 int opened, in_hash_table; | 115 int opened, in_hash_table; |
| 116 struct cleanup *back_to; |
257 | 117 |
258 if (*p != 1) | 118 if (*p != 1) |
259 { | 119 { |
260 warning (_("Invalid entry in %s section"), GDBPY_AUTO_SECTION_NAME); | 120 warning (_("Invalid entry in %s section"), GDBPY_AUTO_SECTION_NAME); |
261 /* We could try various heuristics to find the next valid entry, | 121 /* We could try various heuristics to find the next valid entry, |
262 but it's safer to just punt. */ | 122 but it's safer to just punt. */ |
263 break; | 123 break; |
264 } | 124 } |
265 file = ++p; | 125 file = ++p; |
266 | 126 |
(...skipping 12 matching lines...) Expand all Loading... |
279 } | 139 } |
280 if (p == file) | 140 if (p == file) |
281 { | 141 { |
282 warning (_("Empty path in %s"), source_name); | 142 warning (_("Empty path in %s"), source_name); |
283 continue; | 143 continue; |
284 } | 144 } |
285 | 145 |
286 opened = find_and_open_script (file, 1 /*search_path*/, | 146 opened = find_and_open_script (file, 1 /*search_path*/, |
287 &stream, &full_path); | 147 &stream, &full_path); |
288 | 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 |
289 /* If one script isn't found it's not uncommon for more to not be | 175 /* If one script isn't found it's not uncommon for more to not be |
290 found either. We don't want to print an error message for each | 176 found either. We don't want to print an error message for each |
291 script, too much noise. Instead, we print the warning once and tell | 177 script, too much noise. Instead, we print the warning once and tell |
292 the user how to find the list of scripts that weren't loaded. | 178 the user how to find the list of scripts that weren't loaded. |
293 | 179 |
294 IWBN if complaints.c were more general-purpose. */ | 180 IWBN if complaints.c were more general-purpose. */ |
295 | 181 |
296 in_hash_table = maybe_add_script (pspace_info->loaded_scripts, file, | 182 in_hash_table = maybe_add_script (pspace_info, opened, file, full_path, |
297 » » » » » opened ? full_path : NULL); | 183 » » » » » &script_language_python); |
298 | 184 |
299 if (! opened) | 185 /* If this file is not currently loaded, load it. */ |
300 » { | 186 if (opened && !in_hash_table) |
301 » /* We don't throw an error, the program is still debuggable. */ | 187 » source_python_script_for_objfile (objfile, stream, full_path); |
302 » if (! pspace_info->script_not_found_warning_printed) | 188 |
303 » { | 189 do_cleanups (back_to); |
304 » warning (_("Missing auto-load scripts referenced in section %s\n\ | |
305 of file %s\n\ | |
306 Use `info auto-load-scripts [REGEXP]' to list them."), | |
307 » » GDBPY_AUTO_SECTION_NAME, objfile->name); | |
308 » pspace_info->script_not_found_warning_printed = TRUE; | |
309 » } | |
310 » } | |
311 else | |
312 » { | |
313 » /* If this file is not currently loaded, load it. */ | |
314 » if (! in_hash_table) | |
315 » source_python_script_for_objfile (objfile, full_path); | |
316 » fclose (stream); | |
317 » xfree (full_path); | |
318 » } | |
319 } | 190 } |
320 } | 191 } |
321 | 192 |
322 /* Load scripts specified in section SECTION_NAME of OBJFILE. */ | 193 /* Load scripts specified in section SECTION_NAME of OBJFILE. */ |
323 | 194 |
324 static void | 195 static void |
325 auto_load_section_scripts (struct objfile *objfile, const char *section_name) | 196 auto_load_section_scripts (struct objfile *objfile, const char *section_name) |
326 { | 197 { |
327 bfd *abfd = objfile->obfd; | 198 bfd *abfd = objfile->obfd; |
328 asection *scripts_sect; | 199 asection *scripts_sect; |
329 bfd_size_type size; | 200 bfd_size_type size; |
330 char *p; | 201 char *p; |
331 struct cleanup *cleanups; | 202 struct cleanup *cleanups; |
332 | 203 |
333 scripts_sect = bfd_get_section_by_name (abfd, section_name); | 204 scripts_sect = bfd_get_section_by_name (abfd, section_name); |
334 if (scripts_sect == NULL) | 205 if (scripts_sect == NULL) |
335 return; | 206 return; |
336 | 207 |
337 size = bfd_get_section_size (scripts_sect); | 208 size = bfd_get_section_size (scripts_sect); |
338 p = xmalloc (size); | 209 p = xmalloc (size); |
339 | 210 |
340 cleanups = make_cleanup (xfree, p); | 211 cleanups = make_cleanup (xfree, p); |
341 | 212 |
342 if (bfd_get_section_contents (abfd, scripts_sect, p, (file_ptr) 0, size)) | 213 if (bfd_get_section_contents (abfd, scripts_sect, p, (file_ptr) 0, size)) |
343 source_section_scripts (objfile, section_name, p, p + size); | 214 source_section_scripts (objfile, section_name, p, p + size); |
344 else | 215 else |
345 warning (_("Couldn't read %s section of %s"), | 216 warning (_("Couldn't read %s section of %s"), |
346 section_name, bfd_get_filename (abfd)); | 217 section_name, bfd_get_filename (abfd)); |
347 | 218 |
error: old chunk mismatch |
None
OLD | NEW |