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