Commit | Line | Data |
---|---|---|
e2207b9a JK |
1 | /* GDB routines for supporting auto-loaded scripts. |
2 | ||
3 | Copyright (C) 2012 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 "auto-load.h" | |
22 | #include "progspace.h" | |
23 | #include "python/python.h" | |
24 | #include "gdb_regex.h" | |
25 | #include "ui-out.h" | |
26 | #include "filenames.h" | |
27 | #include "command.h" | |
28 | #include "observer.h" | |
29 | #include "objfiles.h" | |
bf88dd68 JK |
30 | #include "exceptions.h" |
31 | #include "cli/cli-script.h" | |
32 | #include "gdbcmd.h" | |
33 | #include "cli/cli-decode.h" | |
34 | #include "cli/cli-setshow.h" | |
bccbefd2 JK |
35 | #include "gdb_vecs.h" |
36 | #include "readline/tilde.h" | |
37 | #include "completer.h" | |
bf88dd68 JK |
38 | |
39 | /* The suffix of per-objfile scripts to auto-load as non-Python command files. | |
40 | E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */ | |
41 | #define GDB_AUTO_FILE_NAME "-gdb.gdb" | |
42 | ||
43 | static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file, | |
44 | const char *filename); | |
45 | ||
46 | /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME | |
47 | scripts: | |
48 | set auto-load gdb-scripts on|off | |
49 | This is true if we should auto-load associated scripts when an objfile | |
50 | is opened, false otherwise. */ | |
51 | static int auto_load_gdb_scripts = 1; | |
52 | ||
53 | /* "show" command for the auto_load_gdb_scripts configuration variable. */ | |
54 | ||
55 | static void | |
56 | show_auto_load_gdb_scripts (struct ui_file *file, int from_tty, | |
57 | struct cmd_list_element *c, const char *value) | |
58 | { | |
59 | fprintf_filtered (file, _("Auto-loading of canned sequences of commands " | |
60 | "scripts is %s.\n"), | |
61 | value); | |
62 | } | |
e2207b9a JK |
63 | |
64 | /* Internal-use flag to enable/disable auto-loading. | |
65 | This is true if we should auto-load python code when an objfile is opened, | |
66 | false otherwise. | |
67 | ||
bf88dd68 | 68 | Both auto_load_scripts && global_auto_load must be true to enable |
e2207b9a JK |
69 | auto-loading. |
70 | ||
71 | This flag exists to facilitate deferring auto-loading during start-up | |
72 | until after ./.gdbinit has been read; it may augment the search directories | |
73 | used to find the scripts. */ | |
bf88dd68 JK |
74 | int global_auto_load = 1; |
75 | ||
76 | /* Auto-load .gdbinit file from the current directory? */ | |
77 | int auto_load_local_gdbinit = 1; | |
78 | ||
79 | /* Absolute pathname to the current directory .gdbinit, if it exists. */ | |
80 | char *auto_load_local_gdbinit_pathname = NULL; | |
81 | ||
82 | /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */ | |
83 | int auto_load_local_gdbinit_loaded = 0; | |
84 | ||
85 | /* "show" command for the auto_load_local_gdbinit configuration variable. */ | |
86 | ||
87 | static void | |
88 | show_auto_load_local_gdbinit (struct ui_file *file, int from_tty, | |
89 | struct cmd_list_element *c, const char *value) | |
90 | { | |
91 | fprintf_filtered (file, _("Auto-loading of .gdbinit script from current " | |
92 | "directory is %s.\n"), | |
93 | value); | |
94 | } | |
95 | ||
bccbefd2 JK |
96 | /* Directory list safe to hold auto-loaded files. It is not checked for |
97 | absolute paths but they are strongly recommended. It is initialized by | |
98 | _initialize_auto_load. */ | |
99 | static char *auto_load_safe_path; | |
100 | ||
101 | /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized | |
102 | by tilde_expand and possibly each entries has added its gdb_realpath | |
103 | counterpart. */ | |
104 | static VEC (char_ptr) *auto_load_safe_path_vec; | |
105 | ||
106 | /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */ | |
107 | ||
108 | static void | |
109 | auto_load_safe_path_vec_update (void) | |
110 | { | |
111 | VEC (char_ptr) *dir_vec = NULL; | |
112 | unsigned len; | |
113 | int ix; | |
114 | ||
115 | free_char_ptr_vec (auto_load_safe_path_vec); | |
116 | ||
117 | auto_load_safe_path_vec = dirnames_to_char_ptr_vec (auto_load_safe_path); | |
118 | len = VEC_length (char_ptr, auto_load_safe_path_vec); | |
119 | ||
120 | /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC | |
121 | element. */ | |
122 | for (ix = 0; ix < len; ix++) | |
123 | { | |
124 | char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix); | |
125 | char *expanded = tilde_expand (dir); | |
126 | char *real_path = gdb_realpath (expanded); | |
127 | ||
128 | /* Ensure the current entry is at least tilde_expand-ed. */ | |
129 | xfree (dir); | |
130 | VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded); | |
131 | ||
132 | /* If gdb_realpath returns a different content, append it. */ | |
133 | if (strcmp (real_path, expanded) == 0) | |
134 | xfree (real_path); | |
135 | else | |
136 | VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path); | |
137 | } | |
138 | } | |
139 | ||
140 | /* "set" command for the auto_load_safe_path configuration variable. */ | |
141 | ||
142 | static void | |
143 | set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c) | |
144 | { | |
145 | auto_load_safe_path_vec_update (); | |
146 | } | |
147 | ||
148 | /* "show" command for the auto_load_safe_path configuration variable. */ | |
149 | ||
150 | static void | |
151 | show_auto_load_safe_path (struct ui_file *file, int from_tty, | |
152 | struct cmd_list_element *c, const char *value) | |
153 | { | |
154 | if (*value == 0) | |
155 | fprintf_filtered (file, _("Auto-load files are safe to load from any " | |
156 | "directory.\n")); | |
157 | else | |
158 | fprintf_filtered (file, _("List of directories from which it is safe to " | |
159 | "auto-load files is %s.\n"), | |
160 | value); | |
161 | } | |
162 | ||
163 | /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration | |
164 | variable. */ | |
165 | ||
166 | static void | |
167 | add_auto_load_safe_path (char *args, int from_tty) | |
168 | { | |
169 | char *s; | |
170 | ||
171 | if (args == NULL || *args == 0) | |
172 | error (_("\ | |
173 | Adding empty directory element disables the auto-load safe-path security. \ | |
174 | Use 'set auto-load safe-path' instead if you mean that.")); | |
175 | ||
176 | s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args); | |
177 | xfree (auto_load_safe_path); | |
178 | auto_load_safe_path = s; | |
179 | ||
180 | auto_load_safe_path_vec_update (); | |
181 | } | |
182 | ||
183 | /* Return 1 if FILENAME is equal to DIR or if FILENAME belongs to the | |
184 | subdirectory DIR. Return 0 otherwise. gdb_realpath normalization is never | |
185 | done here. */ | |
186 | ||
187 | static ATTRIBUTE_PURE int | |
188 | filename_is_in_dir (const char *filename, const char *dir) | |
189 | { | |
190 | size_t dir_len = strlen (dir); | |
191 | ||
192 | while (dir_len && IS_DIR_SEPARATOR (dir[dir_len - 1])) | |
193 | dir_len--; | |
194 | ||
195 | return (filename_ncmp (dir, filename, dir_len) == 0 | |
196 | && (IS_DIR_SEPARATOR (filename[dir_len]) | |
197 | || filename[dir_len] == '\0')); | |
198 | } | |
199 | ||
200 | /* Return 1 if FILENAME belongs to one of directory components of | |
201 | AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise. | |
202 | auto_load_safe_path_vec_update is never called. | |
203 | *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be | |
204 | freed by the caller. */ | |
205 | ||
206 | static int | |
207 | filename_is_in_auto_load_safe_path_vec (const char *filename, | |
208 | char **filename_realp) | |
209 | { | |
210 | char *dir; | |
211 | int ix; | |
212 | ||
213 | for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir); ++ix) | |
214 | if (*filename_realp == NULL && filename_is_in_dir (filename, dir)) | |
215 | break; | |
216 | ||
217 | if (dir == NULL) | |
218 | { | |
219 | if (*filename_realp == NULL) | |
220 | *filename_realp = gdb_realpath (filename); | |
221 | ||
222 | for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir); | |
223 | ++ix) | |
224 | if (filename_is_in_dir (*filename_realp, dir)) | |
225 | break; | |
226 | } | |
227 | ||
228 | if (dir != NULL) | |
229 | return 1; | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | /* Return 1 if FILENAME is located in one of the directories of | |
235 | AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does | |
236 | not have to be an absolute path. | |
237 | ||
238 | Existence of FILENAME is not checked. Function will still give a warning | |
239 | even if the caller would quietly skip non-existing file in unsafe | |
240 | directory. */ | |
241 | ||
242 | int | |
243 | file_is_auto_load_safe (const char *filename) | |
244 | { | |
245 | char *filename_real = NULL; | |
246 | struct cleanup *back_to; | |
247 | ||
248 | back_to = make_cleanup (free_current_contents, &filename_real); | |
249 | ||
250 | if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real)) | |
251 | { | |
252 | do_cleanups (back_to); | |
253 | return 1; | |
254 | } | |
255 | ||
256 | auto_load_safe_path_vec_update (); | |
257 | if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real)) | |
258 | { | |
259 | do_cleanups (back_to); | |
260 | return 1; | |
261 | } | |
262 | ||
263 | warning (_("File \"%s\" auto-loading has been declined by your " | |
264 | "`auto-load safe-path' set to \"%s\"."), | |
265 | filename_real, auto_load_safe_path); | |
266 | ||
267 | do_cleanups (back_to); | |
268 | return 0; | |
269 | } | |
270 | ||
bf88dd68 JK |
271 | /* Definition of script language for GDB canned sequences of commands. */ |
272 | ||
273 | static const struct script_language script_language_gdb | |
274 | = { GDB_AUTO_FILE_NAME, source_gdb_script_for_objfile }; | |
275 | ||
276 | static void | |
277 | source_gdb_script_for_objfile (struct objfile *objfile, FILE *file, | |
278 | const char *filename) | |
279 | { | |
bccbefd2 | 280 | int is_safe; |
bf88dd68 JK |
281 | struct auto_load_pspace_info *pspace_info; |
282 | volatile struct gdb_exception e; | |
283 | ||
bccbefd2 JK |
284 | is_safe = file_is_auto_load_safe (filename); |
285 | ||
bf88dd68 JK |
286 | /* Add this script to the hash table too so "info auto-load gdb-scripts" |
287 | can print it. */ | |
288 | pspace_info = get_auto_load_pspace_data_for_loading (current_program_space); | |
bccbefd2 JK |
289 | maybe_add_script (pspace_info, is_safe, filename, filename, |
290 | &script_language_gdb); | |
291 | ||
292 | if (!is_safe) | |
293 | return; | |
bf88dd68 JK |
294 | |
295 | TRY_CATCH (e, RETURN_MASK_ALL) | |
296 | { | |
297 | script_from_file (file, filename); | |
298 | } | |
299 | exception_print (gdb_stderr, e); | |
300 | } | |
e2207b9a JK |
301 | |
302 | /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load | |
303 | the same script. There's no point in loading the script multiple times, | |
304 | and there can be a lot of objfiles and scripts, so we keep track of scripts | |
305 | loaded this way. */ | |
306 | ||
307 | struct auto_load_pspace_info | |
308 | { | |
309 | /* For each program space we keep track of loaded scripts. */ | |
310 | struct htab *loaded_scripts; | |
311 | ||
312 | /* Non-zero if we've issued the warning about an auto-load script not being | |
313 | found. We only want to issue this warning once. */ | |
314 | int script_not_found_warning_printed; | |
315 | }; | |
316 | ||
317 | /* Objects of this type are stored in the loaded script hash table. */ | |
318 | ||
319 | struct loaded_script | |
320 | { | |
321 | /* Name as provided by the objfile. */ | |
322 | const char *name; | |
bf88dd68 | 323 | |
e2207b9a JK |
324 | /* Full path name or NULL if script wasn't found (or was otherwise |
325 | inaccessible). */ | |
326 | const char *full_path; | |
bf88dd68 | 327 | |
bccbefd2 JK |
328 | /* Non-zero if this script has been loaded. */ |
329 | int loaded; | |
330 | ||
bf88dd68 | 331 | const struct script_language *language; |
e2207b9a JK |
332 | }; |
333 | ||
334 | /* Per-program-space data key. */ | |
335 | static const struct program_space_data *auto_load_pspace_data; | |
336 | ||
337 | static void | |
338 | auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg) | |
339 | { | |
340 | struct auto_load_pspace_info *info; | |
341 | ||
342 | info = program_space_data (pspace, auto_load_pspace_data); | |
343 | if (info != NULL) | |
344 | { | |
345 | if (info->loaded_scripts) | |
346 | htab_delete (info->loaded_scripts); | |
347 | xfree (info); | |
348 | } | |
349 | } | |
350 | ||
351 | /* Get the current autoload data. If none is found yet, add it now. This | |
352 | function always returns a valid object. */ | |
353 | ||
354 | static struct auto_load_pspace_info * | |
355 | get_auto_load_pspace_data (struct program_space *pspace) | |
356 | { | |
357 | struct auto_load_pspace_info *info; | |
358 | ||
359 | info = program_space_data (pspace, auto_load_pspace_data); | |
360 | if (info == NULL) | |
361 | { | |
362 | info = XZALLOC (struct auto_load_pspace_info); | |
363 | set_program_space_data (pspace, auto_load_pspace_data, info); | |
364 | } | |
365 | ||
366 | return info; | |
367 | } | |
368 | ||
369 | /* Hash function for the loaded script hash. */ | |
370 | ||
371 | static hashval_t | |
372 | hash_loaded_script_entry (const void *data) | |
373 | { | |
374 | const struct loaded_script *e = data; | |
375 | ||
bf88dd68 | 376 | return htab_hash_string (e->name) ^ htab_hash_pointer (e->language); |
e2207b9a JK |
377 | } |
378 | ||
379 | /* Equality function for the loaded script hash. */ | |
380 | ||
381 | static int | |
382 | eq_loaded_script_entry (const void *a, const void *b) | |
383 | { | |
384 | const struct loaded_script *ea = a; | |
385 | const struct loaded_script *eb = b; | |
386 | ||
bf88dd68 | 387 | return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language; |
e2207b9a JK |
388 | } |
389 | ||
390 | /* Initialize the table to track loaded scripts. | |
391 | Each entry is hashed by the full path name. */ | |
392 | ||
393 | static void | |
394 | init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info) | |
395 | { | |
396 | /* Choose 31 as the starting size of the hash table, somewhat arbitrarily. | |
397 | Space for each entry is obtained with one malloc so we can free them | |
398 | easily. */ | |
399 | ||
400 | pspace_info->loaded_scripts = htab_create (31, | |
401 | hash_loaded_script_entry, | |
402 | eq_loaded_script_entry, | |
403 | xfree); | |
404 | ||
405 | pspace_info->script_not_found_warning_printed = FALSE; | |
406 | } | |
407 | ||
408 | /* Wrapper on get_auto_load_pspace_data to also allocate the hash table | |
409 | for loading scripts. */ | |
410 | ||
411 | struct auto_load_pspace_info * | |
412 | get_auto_load_pspace_data_for_loading (struct program_space *pspace) | |
413 | { | |
414 | struct auto_load_pspace_info *info; | |
415 | ||
416 | info = get_auto_load_pspace_data (pspace); | |
417 | if (info->loaded_scripts == NULL) | |
418 | init_loaded_scripts_info (info); | |
419 | ||
420 | return info; | |
421 | } | |
422 | ||
bccbefd2 JK |
423 | /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the |
424 | script has been (is going to) be loaded, 0 otherwise (such as if it has not | |
425 | been found). FULL_PATH is NULL if the script wasn't found. The result is | |
bf88dd68 | 426 | true if the script was already in the hash table. */ |
e2207b9a JK |
427 | |
428 | int | |
bccbefd2 | 429 | maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded, |
bf88dd68 JK |
430 | const char *name, const char *full_path, |
431 | const struct script_language *language) | |
e2207b9a JK |
432 | { |
433 | struct htab *htab = pspace_info->loaded_scripts; | |
434 | struct loaded_script **slot, entry; | |
435 | int in_hash_table; | |
436 | ||
437 | entry.name = name; | |
bf88dd68 | 438 | entry.language = language; |
e2207b9a JK |
439 | slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT); |
440 | in_hash_table = *slot != NULL; | |
441 | ||
442 | /* If this script is not in the hash table, add it. */ | |
443 | ||
444 | if (! in_hash_table) | |
445 | { | |
446 | char *p; | |
447 | ||
448 | /* Allocate all space in one chunk so it's easier to free. */ | |
449 | *slot = xmalloc (sizeof (**slot) | |
450 | + strlen (name) + 1 | |
451 | + (full_path != NULL ? (strlen (full_path) + 1) : 0)); | |
452 | p = ((char*) *slot) + sizeof (**slot); | |
453 | strcpy (p, name); | |
454 | (*slot)->name = p; | |
455 | if (full_path != NULL) | |
456 | { | |
457 | p += strlen (p) + 1; | |
458 | strcpy (p, full_path); | |
459 | (*slot)->full_path = p; | |
460 | } | |
461 | else | |
462 | (*slot)->full_path = NULL; | |
bccbefd2 | 463 | (*slot)->loaded = loaded; |
bf88dd68 | 464 | (*slot)->language = language; |
e2207b9a JK |
465 | } |
466 | ||
467 | return in_hash_table; | |
468 | } | |
469 | ||
470 | /* Clear the table of loaded section scripts. */ | |
471 | ||
472 | static void | |
473 | clear_section_scripts (void) | |
474 | { | |
475 | struct program_space *pspace = current_program_space; | |
476 | struct auto_load_pspace_info *info; | |
477 | ||
478 | info = program_space_data (pspace, auto_load_pspace_data); | |
479 | if (info != NULL && info->loaded_scripts != NULL) | |
480 | { | |
481 | htab_delete (info->loaded_scripts); | |
482 | info->loaded_scripts = NULL; | |
483 | info->script_not_found_warning_printed = FALSE; | |
484 | } | |
485 | } | |
486 | ||
bf88dd68 JK |
487 | /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load |
488 | it. */ | |
e2207b9a JK |
489 | |
490 | void | |
bf88dd68 JK |
491 | auto_load_objfile_script (struct objfile *objfile, |
492 | const struct script_language *language) | |
e2207b9a JK |
493 | { |
494 | char *realname; | |
495 | char *filename, *debugfile; | |
496 | int len; | |
497 | FILE *input; | |
498 | struct cleanup *cleanups; | |
499 | ||
500 | realname = gdb_realpath (objfile->name); | |
501 | len = strlen (realname); | |
bf88dd68 | 502 | filename = xmalloc (len + strlen (language->suffix) + 1); |
e2207b9a | 503 | memcpy (filename, realname, len); |
bf88dd68 | 504 | strcpy (filename + len, language->suffix); |
e2207b9a JK |
505 | |
506 | cleanups = make_cleanup (xfree, filename); | |
507 | make_cleanup (xfree, realname); | |
508 | ||
509 | input = fopen (filename, "r"); | |
510 | debugfile = filename; | |
511 | ||
512 | if (!input && debug_file_directory) | |
513 | { | |
514 | /* Also try the same file in the separate debug info directory. */ | |
515 | debugfile = xmalloc (strlen (filename) | |
516 | + strlen (debug_file_directory) + 1); | |
517 | strcpy (debugfile, debug_file_directory); | |
518 | /* FILENAME is absolute, so we don't need a "/" here. */ | |
519 | strcat (debugfile, filename); | |
520 | ||
521 | make_cleanup (xfree, debugfile); | |
522 | input = fopen (debugfile, "r"); | |
523 | } | |
524 | ||
525 | if (!input && gdb_datadir) | |
526 | { | |
527 | /* Also try the same file in a subdirectory of gdb's data | |
528 | directory. */ | |
529 | debugfile = xmalloc (strlen (gdb_datadir) + strlen (filename) | |
530 | + strlen ("/auto-load") + 1); | |
531 | strcpy (debugfile, gdb_datadir); | |
532 | strcat (debugfile, "/auto-load"); | |
533 | /* FILENAME is absolute, so we don't need a "/" here. */ | |
534 | strcat (debugfile, filename); | |
535 | ||
536 | make_cleanup (xfree, debugfile); | |
537 | input = fopen (debugfile, "r"); | |
538 | } | |
539 | ||
540 | if (input) | |
541 | { | |
e2207b9a JK |
542 | make_cleanup_fclose (input); |
543 | ||
e2207b9a JK |
544 | /* To preserve existing behaviour we don't check for whether the |
545 | script was already in the table, and always load it. | |
546 | It's highly unlikely that we'd ever load it twice, | |
547 | and these scripts are required to be idempotent under multiple | |
548 | loads anyway. */ | |
bf88dd68 | 549 | language->source_script_for_objfile (objfile, input, debugfile); |
e2207b9a JK |
550 | } |
551 | ||
552 | do_cleanups (cleanups); | |
553 | } | |
554 | ||
bf88dd68 JK |
555 | /* Load any auto-loaded scripts for OBJFILE. */ |
556 | ||
557 | void | |
558 | load_auto_scripts_for_objfile (struct objfile *objfile) | |
559 | { | |
560 | if (!global_auto_load) | |
561 | return; | |
562 | ||
563 | if (auto_load_gdb_scripts) | |
564 | auto_load_objfile_script (objfile, &script_language_gdb); | |
565 | ||
566 | gdbpy_load_auto_scripts_for_objfile (objfile); | |
567 | } | |
568 | ||
e2207b9a JK |
569 | /* This is a new_objfile observer callback to auto-load scripts. |
570 | ||
571 | Two flavors of auto-loaded scripts are supported. | |
572 | 1) based on the path to the objfile | |
573 | 2) from .debug_gdb_scripts section */ | |
574 | ||
575 | static void | |
576 | auto_load_new_objfile (struct objfile *objfile) | |
577 | { | |
578 | if (!objfile) | |
579 | { | |
580 | /* OBJFILE is NULL when loading a new "main" symbol-file. */ | |
581 | clear_section_scripts (); | |
582 | return; | |
583 | } | |
584 | ||
585 | load_auto_scripts_for_objfile (objfile); | |
586 | } | |
587 | ||
588 | /* Collect scripts to be printed in a vec. */ | |
589 | ||
590 | typedef struct loaded_script *loaded_script_ptr; | |
591 | DEF_VEC_P (loaded_script_ptr); | |
592 | ||
bf88dd68 JK |
593 | struct collect_matching_scripts_data |
594 | { | |
595 | VEC (loaded_script_ptr) **scripts_p; | |
596 | ||
597 | const struct script_language *language; | |
598 | }; | |
599 | ||
e2207b9a JK |
600 | /* Traversal function for htab_traverse. |
601 | Collect the entry if it matches the regexp. */ | |
602 | ||
603 | static int | |
604 | collect_matching_scripts (void **slot, void *info) | |
605 | { | |
606 | struct loaded_script *script = *slot; | |
bf88dd68 | 607 | struct collect_matching_scripts_data *data = info; |
e2207b9a | 608 | |
bf88dd68 JK |
609 | if (script->language == data->language && re_exec (script->name)) |
610 | VEC_safe_push (loaded_script_ptr, *data->scripts_p, script); | |
e2207b9a JK |
611 | |
612 | return 1; | |
613 | } | |
614 | ||
615 | /* Print SCRIPT. */ | |
616 | ||
617 | static void | |
618 | print_script (struct loaded_script *script) | |
619 | { | |
620 | struct ui_out *uiout = current_uiout; | |
621 | struct cleanup *chain; | |
622 | ||
623 | chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
624 | ||
bccbefd2 | 625 | ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No"); |
e2207b9a JK |
626 | ui_out_field_string (uiout, "script", script->name); |
627 | ui_out_text (uiout, "\n"); | |
628 | ||
629 | /* If the name isn't the full path, print it too. */ | |
630 | if (script->full_path != NULL | |
631 | && strcmp (script->name, script->full_path) != 0) | |
632 | { | |
633 | ui_out_text (uiout, "\tfull name: "); | |
634 | ui_out_field_string (uiout, "full_path", script->full_path); | |
635 | ui_out_text (uiout, "\n"); | |
636 | } | |
637 | ||
638 | do_cleanups (chain); | |
639 | } | |
640 | ||
641 | /* Helper for info_auto_load_scripts to sort the scripts by name. */ | |
642 | ||
643 | static int | |
644 | sort_scripts_by_name (const void *ap, const void *bp) | |
645 | { | |
646 | const struct loaded_script *a = *(const struct loaded_script **) ap; | |
647 | const struct loaded_script *b = *(const struct loaded_script **) bp; | |
648 | ||
649 | return FILENAME_CMP (a->name, b->name); | |
650 | } | |
651 | ||
bf88dd68 JK |
652 | /* Special internal GDB value of auto_load_info_scripts's PATTERN identify |
653 | the "info auto-load XXX" command has been executed through the general | |
654 | "info auto-load" invocation. Extra newline will be printed if needed. */ | |
655 | char auto_load_info_scripts_pattern_nl[] = ""; | |
e2207b9a | 656 | |
bf88dd68 JK |
657 | /* Implementation for "info auto-load gdb-scripts" |
658 | (and "info auto-load python-scripts"). List scripts in LANGUAGE matching | |
659 | PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */ | |
660 | ||
661 | void | |
662 | auto_load_info_scripts (char *pattern, int from_tty, | |
663 | const struct script_language *language) | |
e2207b9a JK |
664 | { |
665 | struct ui_out *uiout = current_uiout; | |
666 | struct auto_load_pspace_info *pspace_info; | |
667 | struct cleanup *script_chain; | |
668 | VEC (loaded_script_ptr) *scripts; | |
669 | int nr_scripts; | |
670 | ||
671 | dont_repeat (); | |
672 | ||
673 | pspace_info = get_auto_load_pspace_data (current_program_space); | |
674 | ||
675 | if (pattern && *pattern) | |
676 | { | |
677 | char *re_err = re_comp (pattern); | |
678 | ||
679 | if (re_err) | |
680 | error (_("Invalid regexp: %s"), re_err); | |
681 | } | |
682 | else | |
683 | { | |
684 | re_comp (""); | |
685 | } | |
686 | ||
687 | /* We need to know the number of rows before we build the table. | |
688 | Plus we want to sort the scripts by name. | |
689 | So first traverse the hash table collecting the matching scripts. */ | |
690 | ||
691 | scripts = VEC_alloc (loaded_script_ptr, 10); | |
692 | script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts); | |
693 | ||
694 | if (pspace_info != NULL && pspace_info->loaded_scripts != NULL) | |
695 | { | |
bf88dd68 JK |
696 | struct collect_matching_scripts_data data = { &scripts, language }; |
697 | ||
e2207b9a JK |
698 | immediate_quit++; |
699 | /* Pass a pointer to scripts as VEC_safe_push can realloc space. */ | |
700 | htab_traverse_noresize (pspace_info->loaded_scripts, | |
bf88dd68 | 701 | collect_matching_scripts, &data); |
e2207b9a JK |
702 | immediate_quit--; |
703 | } | |
704 | ||
705 | nr_scripts = VEC_length (loaded_script_ptr, scripts); | |
bf88dd68 JK |
706 | |
707 | /* Table header shifted right by preceding "gdb-scripts: " would not match | |
708 | its columns. */ | |
709 | if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl) | |
710 | ui_out_text (uiout, "\n"); | |
711 | ||
e2207b9a JK |
712 | make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts, |
713 | "AutoLoadedScriptsTable"); | |
714 | ||
715 | ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded"); | |
716 | ui_out_table_header (uiout, 70, ui_left, "script", "Script"); | |
717 | ui_out_table_body (uiout); | |
718 | ||
719 | if (nr_scripts > 0) | |
720 | { | |
721 | int i; | |
722 | loaded_script_ptr script; | |
723 | ||
724 | qsort (VEC_address (loaded_script_ptr, scripts), | |
725 | VEC_length (loaded_script_ptr, scripts), | |
726 | sizeof (loaded_script_ptr), sort_scripts_by_name); | |
727 | for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i) | |
728 | print_script (script); | |
729 | } | |
730 | ||
731 | do_cleanups (script_chain); | |
732 | ||
733 | if (nr_scripts == 0) | |
734 | { | |
735 | if (pattern && *pattern) | |
736 | ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n", | |
737 | pattern); | |
738 | else | |
739 | ui_out_message (uiout, 0, "No auto-load scripts.\n"); | |
740 | } | |
741 | } | |
742 | ||
bf88dd68 JK |
743 | /* Wrapper for "info auto-load gdb-scripts". */ |
744 | ||
745 | static void | |
746 | info_auto_load_gdb_scripts (char *pattern, int from_tty) | |
747 | { | |
748 | auto_load_info_scripts (pattern, from_tty, &script_language_gdb); | |
749 | } | |
750 | ||
751 | /* Implement 'info auto-load local-gdbinit'. */ | |
752 | ||
753 | static void | |
754 | info_auto_load_local_gdbinit (char *args, int from_tty) | |
755 | { | |
756 | if (auto_load_local_gdbinit_pathname == NULL) | |
757 | printf_filtered (_("Local .gdbinit file was not found.\n")); | |
758 | else if (auto_load_local_gdbinit_loaded) | |
759 | printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"), | |
760 | auto_load_local_gdbinit_pathname); | |
761 | else | |
762 | printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"), | |
763 | auto_load_local_gdbinit_pathname); | |
764 | } | |
765 | ||
e2207b9a JK |
766 | /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset |
767 | before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED | |
768 | of PSPACE_INFO. */ | |
769 | ||
770 | int | |
771 | script_not_found_warning_print (struct auto_load_pspace_info *pspace_info) | |
772 | { | |
773 | int retval = !pspace_info->script_not_found_warning_printed; | |
774 | ||
775 | pspace_info->script_not_found_warning_printed = 1; | |
776 | ||
777 | return retval; | |
778 | } | |
779 | ||
bf88dd68 JK |
780 | /* The only valid "set auto-load" argument is off|0|no|disable. */ |
781 | ||
782 | static void | |
783 | set_auto_load_cmd (char *args, int from_tty) | |
784 | { | |
785 | struct cmd_list_element *list; | |
786 | size_t length; | |
787 | ||
788 | /* See parse_binary_operation in use by the sub-commands. */ | |
789 | ||
790 | length = args ? strlen (args) : 0; | |
791 | ||
792 | while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t')) | |
793 | length--; | |
794 | ||
795 | if (length == 0 || (strncmp (args, "off", length) != 0 | |
796 | && strncmp (args, "0", length) != 0 | |
797 | && strncmp (args, "no", length) != 0 | |
798 | && strncmp (args, "disable", length) != 0)) | |
799 | error (_("Valid is only global 'set auto-load no'; " | |
800 | "otherwise check the auto-load sub-commands.")); | |
801 | ||
802 | for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next) | |
803 | if (list->var_type == var_boolean) | |
804 | { | |
805 | gdb_assert (list->type == set_cmd); | |
806 | do_setshow_command (args, from_tty, list); | |
807 | } | |
808 | } | |
809 | ||
810 | /* Initialize "set auto-load " commands prefix and return it. */ | |
811 | ||
812 | struct cmd_list_element ** | |
813 | auto_load_set_cmdlist_get (void) | |
814 | { | |
815 | static struct cmd_list_element *retval; | |
816 | ||
817 | if (retval == NULL) | |
818 | add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\ | |
819 | Auto-loading specific settings.\n\ | |
820 | Configure various auto-load-specific variables such as\n\ | |
821 | automatic loading of Python scripts."), | |
822 | &retval, "set auto-load ", | |
823 | 1/*allow-unknown*/, &setlist); | |
824 | ||
825 | return &retval; | |
826 | } | |
827 | ||
828 | /* Command "show auto-load" displays summary of all the current | |
829 | "show auto-load " settings. */ | |
830 | ||
831 | static void | |
832 | show_auto_load_cmd (char *args, int from_tty) | |
833 | { | |
834 | cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, ""); | |
835 | } | |
836 | ||
837 | /* Initialize "show auto-load " commands prefix and return it. */ | |
838 | ||
839 | struct cmd_list_element ** | |
840 | auto_load_show_cmdlist_get (void) | |
841 | { | |
842 | static struct cmd_list_element *retval; | |
843 | ||
844 | if (retval == NULL) | |
845 | add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\ | |
846 | Show auto-loading specific settings.\n\ | |
847 | Show configuration of various auto-load-specific variables such as\n\ | |
848 | automatic loading of Python scripts."), | |
849 | &retval, "show auto-load ", | |
850 | 0/*allow-unknown*/, &showlist); | |
851 | ||
852 | return &retval; | |
853 | } | |
854 | ||
855 | /* Command "info auto-load" displays whether the various auto-load files have | |
856 | been loaded. This is reimplementation of cmd_show_list which inserts | |
857 | newlines at proper places. */ | |
858 | ||
859 | static void | |
860 | info_auto_load_cmd (char *args, int from_tty) | |
861 | { | |
862 | struct cmd_list_element *list; | |
863 | struct cleanup *infolist_chain; | |
864 | struct ui_out *uiout = current_uiout; | |
865 | ||
866 | infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist"); | |
867 | ||
868 | for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next) | |
869 | { | |
870 | struct cleanup *option_chain | |
871 | = make_cleanup_ui_out_tuple_begin_end (uiout, "option"); | |
872 | ||
873 | gdb_assert (!list->prefixlist); | |
874 | gdb_assert (list->type == not_set_cmd); | |
875 | ||
876 | ui_out_field_string (uiout, "name", list->name); | |
877 | ui_out_text (uiout, ": "); | |
878 | cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty); | |
879 | ||
880 | /* Close the tuple. */ | |
881 | do_cleanups (option_chain); | |
882 | } | |
883 | ||
884 | /* Close the tuple. */ | |
885 | do_cleanups (infolist_chain); | |
886 | } | |
887 | ||
888 | /* Initialize "info auto-load " commands prefix and return it. */ | |
889 | ||
890 | struct cmd_list_element ** | |
891 | auto_load_info_cmdlist_get (void) | |
892 | { | |
893 | static struct cmd_list_element *retval; | |
894 | ||
895 | if (retval == NULL) | |
896 | add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\ | |
897 | Print current status of auto-loaded files.\n\ | |
898 | Print whether various files like Python scripts or .gdbinit files have been\n\ | |
899 | found and/or loaded."), | |
900 | &retval, "info auto-load ", | |
901 | 0/*allow-unknown*/, &infolist); | |
902 | ||
903 | return &retval; | |
904 | } | |
905 | ||
e2207b9a JK |
906 | void _initialize_auto_load (void); |
907 | ||
908 | void | |
909 | _initialize_auto_load (void) | |
910 | { | |
bccbefd2 JK |
911 | struct cmd_list_element *cmd; |
912 | ||
e2207b9a JK |
913 | auto_load_pspace_data |
914 | = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup); | |
915 | ||
916 | observer_attach_new_objfile (auto_load_new_objfile); | |
917 | ||
bf88dd68 JK |
918 | add_setshow_boolean_cmd ("gdb-scripts", class_support, |
919 | &auto_load_gdb_scripts, _("\ | |
920 | Enable or disable auto-loading of canned sequences of commands scripts."), _("\ | |
921 | Show whether auto-loading of canned sequences of commands scripts is enabled."), | |
922 | _("\ | |
923 | If enabled, canned sequences of commands are loaded when the debugger reads\n\ | |
924 | an executable or shared library.\n\ | |
925 | This options has security implications for untrusted inferiors."), | |
926 | NULL, show_auto_load_gdb_scripts, | |
927 | auto_load_set_cmdlist_get (), | |
928 | auto_load_show_cmdlist_get ()); | |
929 | ||
930 | add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts, | |
931 | _("Print the list of automatically loaded sequences of commands.\n\ | |
932 | Usage: info auto-load gdb-scripts [REGEXP]"), | |
933 | auto_load_info_cmdlist_get ()); | |
934 | ||
935 | add_setshow_boolean_cmd ("local-gdbinit", class_support, | |
936 | &auto_load_local_gdbinit, _("\ | |
937 | Enable or disable auto-loading of .gdbinit script in current directory."), _("\ | |
938 | Show whether auto-loading .gdbinit script in current directory is enabled."), | |
939 | _("\ | |
940 | If enabled, canned sequences of commands are loaded when debugger starts\n\ | |
941 | from .gdbinit file in current directory. Such files are deprecated,\n\ | |
942 | use a script associated with inferior executable file instead.\n\ | |
943 | This options has security implications for untrusted inferiors."), | |
944 | NULL, show_auto_load_local_gdbinit, | |
945 | auto_load_set_cmdlist_get (), | |
946 | auto_load_show_cmdlist_get ()); | |
947 | ||
948 | add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit, | |
949 | _("Print whether current directory .gdbinit file has been loaded.\n\ | |
950 | Usage: info auto-load local-gdbinit"), | |
951 | auto_load_info_cmdlist_get ()); | |
bccbefd2 JK |
952 | |
953 | auto_load_safe_path = xstrdup (DEFAULT_AUTO_LOAD_SAFE_PATH); | |
954 | auto_load_safe_path_vec_update (); | |
955 | add_setshow_optional_filename_cmd ("safe-path", class_support, | |
956 | &auto_load_safe_path, _("\ | |
957 | Set the list of directories from which it is safe to auto-load files."), _("\ | |
958 | Show the list of directories from which it is safe to auto-load files."), _("\ | |
959 | Various files loaded automatically for the 'set auto-load ...' options must\n\ | |
960 | be located in one of the directories listed by this option. Warning will be\n\ | |
961 | printed and file will not be used otherwise. Use empty string (or even\n\ | |
962 | empty directory entry) to allow any file for the 'set auto-load ...' options.\n\ | |
963 | This option is ignored for the kinds of files having 'set auto-load ... off'.\n\ | |
964 | This options has security implications for untrusted inferiors."), | |
965 | set_auto_load_safe_path, | |
966 | show_auto_load_safe_path, | |
967 | auto_load_set_cmdlist_get (), | |
968 | auto_load_show_cmdlist_get ()); | |
969 | ||
970 | cmd = add_cmd ("add-auto-load-safe-path", class_support, | |
971 | add_auto_load_safe_path, | |
972 | _("Add entries to the list of directories from which it is safe " | |
973 | "to auto-load files.\n\ | |
974 | See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\ | |
975 | access the current full list setting."), | |
976 | &cmdlist); | |
977 | set_cmd_completer (cmd, filename_completer); | |
e2207b9a | 978 | } |