Commit | Line | Data |
---|---|---|
e2207b9a JK |
1 | /* GDB routines for supporting auto-loaded scripts. |
2 | ||
61baf725 | 3 | Copyright (C) 2012-2017 Free Software Foundation, Inc. |
e2207b9a JK |
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" | |
9f050062 | 21 | #include <ctype.h> |
e2207b9a JK |
22 | #include "auto-load.h" |
23 | #include "progspace.h" | |
e2207b9a JK |
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 "cli/cli-script.h" |
31 | #include "gdbcmd.h" | |
5b2bf947 | 32 | #include "cli/cli-cmds.h" |
bf88dd68 JK |
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" | |
202cbf1c | 38 | #include "fnmatch.h" |
9a950c7c | 39 | #include "top.h" |
614c279d | 40 | #include "filestuff.h" |
6dddc817 | 41 | #include "extension.h" |
ed3ef339 | 42 | #include "gdb/section-scripts.h" |
bf88dd68 | 43 | |
5b2bf947 DE |
44 | /* The section to look in for auto-loaded scripts (in file formats that |
45 | support sections). | |
46 | Each entry in this section is a record that begins with a leading byte | |
47 | identifying the record type. | |
48 | At the moment we only support one record type: A leading byte of 1, | |
49 | followed by the path of a python script to load. */ | |
50 | #define AUTO_SECTION_NAME ".debug_gdb_scripts" | |
51 | ||
9f050062 DE |
52 | static void maybe_print_unsupported_script_warning |
53 | (struct auto_load_pspace_info *, struct objfile *objfile, | |
54 | const struct extension_language_defn *language, | |
55 | const char *section_name, unsigned offset); | |
6dddc817 | 56 | |
9f050062 DE |
57 | static void maybe_print_script_not_found_warning |
58 | (struct auto_load_pspace_info *, struct objfile *objfile, | |
59 | const struct extension_language_defn *language, | |
60 | const char *section_name, unsigned offset); | |
bf88dd68 | 61 | |
4dc84fd1 JK |
62 | /* Value of the 'set debug auto-load' configuration variable. */ |
63 | static int debug_auto_load = 0; | |
64 | ||
65 | /* "show" command for the debug_auto_load configuration variable. */ | |
66 | ||
67 | static void | |
68 | show_debug_auto_load (struct ui_file *file, int from_tty, | |
69 | struct cmd_list_element *c, const char *value) | |
70 | { | |
71 | fprintf_filtered (file, _("Debugging output for files " | |
72 | "of 'set auto-load ...' is %s.\n"), | |
73 | value); | |
74 | } | |
75 | ||
bf88dd68 JK |
76 | /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME |
77 | scripts: | |
78 | set auto-load gdb-scripts on|off | |
79 | This is true if we should auto-load associated scripts when an objfile | |
80 | is opened, false otherwise. */ | |
81 | static int auto_load_gdb_scripts = 1; | |
82 | ||
83 | /* "show" command for the auto_load_gdb_scripts configuration variable. */ | |
84 | ||
85 | static void | |
86 | show_auto_load_gdb_scripts (struct ui_file *file, int from_tty, | |
87 | struct cmd_list_element *c, const char *value) | |
88 | { | |
89 | fprintf_filtered (file, _("Auto-loading of canned sequences of commands " | |
90 | "scripts is %s.\n"), | |
91 | value); | |
92 | } | |
e2207b9a | 93 | |
5b2bf947 DE |
94 | /* Return non-zero if auto-loading gdb scripts is enabled. */ |
95 | ||
6dddc817 DE |
96 | int |
97 | auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang) | |
5b2bf947 DE |
98 | { |
99 | return auto_load_gdb_scripts; | |
100 | } | |
101 | ||
e2207b9a JK |
102 | /* Internal-use flag to enable/disable auto-loading. |
103 | This is true if we should auto-load python code when an objfile is opened, | |
104 | false otherwise. | |
105 | ||
bf88dd68 | 106 | Both auto_load_scripts && global_auto_load must be true to enable |
e2207b9a JK |
107 | auto-loading. |
108 | ||
109 | This flag exists to facilitate deferring auto-loading during start-up | |
110 | until after ./.gdbinit has been read; it may augment the search directories | |
111 | used to find the scripts. */ | |
bf88dd68 JK |
112 | int global_auto_load = 1; |
113 | ||
114 | /* Auto-load .gdbinit file from the current directory? */ | |
115 | int auto_load_local_gdbinit = 1; | |
116 | ||
117 | /* Absolute pathname to the current directory .gdbinit, if it exists. */ | |
118 | char *auto_load_local_gdbinit_pathname = NULL; | |
119 | ||
120 | /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */ | |
121 | int auto_load_local_gdbinit_loaded = 0; | |
122 | ||
123 | /* "show" command for the auto_load_local_gdbinit configuration variable. */ | |
124 | ||
125 | static void | |
126 | show_auto_load_local_gdbinit (struct ui_file *file, int from_tty, | |
127 | struct cmd_list_element *c, const char *value) | |
128 | { | |
129 | fprintf_filtered (file, _("Auto-loading of .gdbinit script from current " | |
130 | "directory is %s.\n"), | |
131 | value); | |
132 | } | |
133 | ||
7349ff92 JK |
134 | /* Directory list from which to load auto-loaded scripts. It is not checked |
135 | for absolute paths but they are strongly recommended. It is initialized by | |
136 | _initialize_auto_load. */ | |
137 | static char *auto_load_dir; | |
138 | ||
139 | /* "set" command for the auto_load_dir configuration variable. */ | |
140 | ||
141 | static void | |
142 | set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c) | |
143 | { | |
144 | /* Setting the variable to "" resets it to the compile time defaults. */ | |
145 | if (auto_load_dir[0] == '\0') | |
146 | { | |
147 | xfree (auto_load_dir); | |
148 | auto_load_dir = xstrdup (AUTO_LOAD_DIR); | |
149 | } | |
150 | } | |
151 | ||
152 | /* "show" command for the auto_load_dir configuration variable. */ | |
153 | ||
154 | static void | |
155 | show_auto_load_dir (struct ui_file *file, int from_tty, | |
156 | struct cmd_list_element *c, const char *value) | |
157 | { | |
158 | fprintf_filtered (file, _("List of directories from which to load " | |
159 | "auto-loaded scripts is %s.\n"), | |
160 | value); | |
161 | } | |
162 | ||
bccbefd2 JK |
163 | /* Directory list safe to hold auto-loaded files. It is not checked for |
164 | absolute paths but they are strongly recommended. It is initialized by | |
165 | _initialize_auto_load. */ | |
166 | static char *auto_load_safe_path; | |
167 | ||
168 | /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized | |
169 | by tilde_expand and possibly each entries has added its gdb_realpath | |
170 | counterpart. */ | |
171 | static VEC (char_ptr) *auto_load_safe_path_vec; | |
172 | ||
1564a261 JK |
173 | /* Expand $datadir and $debugdir in STRING according to the rules of |
174 | substitute_path_component. Return vector from dirnames_to_char_ptr_vec, | |
175 | this vector must be freed by free_char_ptr_vec by the caller. */ | |
176 | ||
177 | static VEC (char_ptr) * | |
178 | auto_load_expand_dir_vars (const char *string) | |
179 | { | |
180 | VEC (char_ptr) *dir_vec; | |
181 | char *s; | |
182 | ||
183 | s = xstrdup (string); | |
184 | substitute_path_component (&s, "$datadir", gdb_datadir); | |
185 | substitute_path_component (&s, "$debugdir", debug_file_directory); | |
186 | ||
187 | if (debug_auto_load && strcmp (s, string) != 0) | |
188 | fprintf_unfiltered (gdb_stdlog, | |
189 | _("auto-load: Expanded $-variables to \"%s\".\n"), s); | |
190 | ||
191 | dir_vec = dirnames_to_char_ptr_vec (s); | |
192 | xfree(s); | |
193 | ||
194 | return dir_vec; | |
195 | } | |
196 | ||
bccbefd2 JK |
197 | /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */ |
198 | ||
199 | static void | |
200 | auto_load_safe_path_vec_update (void) | |
201 | { | |
bccbefd2 JK |
202 | unsigned len; |
203 | int ix; | |
204 | ||
4dc84fd1 JK |
205 | if (debug_auto_load) |
206 | fprintf_unfiltered (gdb_stdlog, | |
207 | _("auto-load: Updating directories of \"%s\".\n"), | |
208 | auto_load_safe_path); | |
209 | ||
bccbefd2 JK |
210 | free_char_ptr_vec (auto_load_safe_path_vec); |
211 | ||
1564a261 | 212 | auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path); |
bccbefd2 JK |
213 | len = VEC_length (char_ptr, auto_load_safe_path_vec); |
214 | ||
215 | /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC | |
216 | element. */ | |
217 | for (ix = 0; ix < len; ix++) | |
218 | { | |
219 | char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix); | |
1564a261 | 220 | char *expanded = tilde_expand (dir); |
14278e1f | 221 | gdb::unique_xmalloc_ptr<char> real_path = gdb_realpath (expanded); |
bccbefd2 | 222 | |
1564a261 | 223 | /* Ensure the current entry is at least tilde_expand-ed. */ |
bccbefd2 JK |
224 | VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded); |
225 | ||
4dc84fd1 JK |
226 | if (debug_auto_load) |
227 | { | |
228 | if (strcmp (expanded, dir) == 0) | |
229 | fprintf_unfiltered (gdb_stdlog, | |
230 | _("auto-load: Using directory \"%s\".\n"), | |
231 | expanded); | |
232 | else | |
233 | fprintf_unfiltered (gdb_stdlog, | |
234 | _("auto-load: Resolved directory \"%s\" " | |
235 | "as \"%s\".\n"), | |
236 | dir, expanded); | |
237 | } | |
238 | xfree (dir); | |
239 | ||
bccbefd2 | 240 | /* If gdb_realpath returns a different content, append it. */ |
14278e1f | 241 | if (strcmp (real_path.get (), expanded) != 0) |
4dc84fd1 | 242 | { |
4dc84fd1 JK |
243 | if (debug_auto_load) |
244 | fprintf_unfiltered (gdb_stdlog, | |
245 | _("auto-load: And canonicalized as \"%s\".\n"), | |
14278e1f TT |
246 | real_path.get ()); |
247 | ||
248 | VEC_safe_push (char_ptr, auto_load_safe_path_vec, | |
249 | real_path.release ()); | |
4dc84fd1 | 250 | } |
bccbefd2 JK |
251 | } |
252 | } | |
253 | ||
aff139ff | 254 | /* Variable gdb_datadir has been set. Update content depending on $datadir. */ |
6dea1fbd JK |
255 | |
256 | static void | |
257 | auto_load_gdb_datadir_changed (void) | |
258 | { | |
259 | auto_load_safe_path_vec_update (); | |
260 | } | |
261 | ||
bccbefd2 JK |
262 | /* "set" command for the auto_load_safe_path configuration variable. */ |
263 | ||
264 | static void | |
265 | set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c) | |
266 | { | |
6dea1fbd | 267 | /* Setting the variable to "" resets it to the compile time defaults. */ |
af2c1515 JK |
268 | if (auto_load_safe_path[0] == '\0') |
269 | { | |
270 | xfree (auto_load_safe_path); | |
6dea1fbd | 271 | auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH); |
af2c1515 JK |
272 | } |
273 | ||
bccbefd2 JK |
274 | auto_load_safe_path_vec_update (); |
275 | } | |
276 | ||
277 | /* "show" command for the auto_load_safe_path configuration variable. */ | |
278 | ||
279 | static void | |
280 | show_auto_load_safe_path (struct ui_file *file, int from_tty, | |
281 | struct cmd_list_element *c, const char *value) | |
282 | { | |
f7bfa992 JK |
283 | const char *cs; |
284 | ||
285 | /* Check if user has entered either "/" or for example ":". | |
286 | But while more complicate content like ":/foo" would still also | |
287 | permit any location do not hide those. */ | |
288 | ||
289 | for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs)); | |
290 | cs++); | |
291 | if (*cs == 0) | |
bccbefd2 JK |
292 | fprintf_filtered (file, _("Auto-load files are safe to load from any " |
293 | "directory.\n")); | |
294 | else | |
295 | fprintf_filtered (file, _("List of directories from which it is safe to " | |
296 | "auto-load files is %s.\n"), | |
297 | value); | |
298 | } | |
299 | ||
300 | /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration | |
301 | variable. */ | |
302 | ||
303 | static void | |
304 | add_auto_load_safe_path (char *args, int from_tty) | |
305 | { | |
306 | char *s; | |
307 | ||
308 | if (args == NULL || *args == 0) | |
309 | error (_("\ | |
af2c1515 JK |
310 | Directory argument required.\n\ |
311 | Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\ | |
312 | ")); | |
bccbefd2 JK |
313 | |
314 | s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args); | |
315 | xfree (auto_load_safe_path); | |
316 | auto_load_safe_path = s; | |
317 | ||
318 | auto_load_safe_path_vec_update (); | |
319 | } | |
320 | ||
f10c5b19 JK |
321 | /* "add-auto-load-scripts-directory" command for the auto_load_dir configuration |
322 | variable. */ | |
323 | ||
324 | static void | |
325 | add_auto_load_dir (char *args, int from_tty) | |
326 | { | |
327 | char *s; | |
328 | ||
329 | if (args == NULL || *args == 0) | |
330 | error (_("Directory argument required.")); | |
331 | ||
332 | s = xstrprintf ("%s%c%s", auto_load_dir, DIRNAME_SEPARATOR, args); | |
333 | xfree (auto_load_dir); | |
334 | auto_load_dir = s; | |
335 | } | |
336 | ||
202cbf1c JK |
337 | /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME |
338 | and PATTERN. */ | |
bccbefd2 | 339 | |
202cbf1c JK |
340 | static int |
341 | filename_is_in_pattern_1 (char *filename, char *pattern) | |
bccbefd2 | 342 | { |
202cbf1c JK |
343 | size_t pattern_len = strlen (pattern); |
344 | size_t filename_len = strlen (filename); | |
345 | ||
346 | if (debug_auto_load) | |
347 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" " | |
348 | "to pattern \"%s\"\n"), | |
349 | filename, pattern); | |
bccbefd2 | 350 | |
202cbf1c JK |
351 | /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as |
352 | trailing slashes are trimmed also from FILENAME it still matches | |
353 | correctly. */ | |
354 | while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1])) | |
355 | pattern_len--; | |
356 | pattern[pattern_len] = '\0'; | |
bccbefd2 | 357 | |
1ef71717 JK |
358 | /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows |
359 | platform FILENAME even after gdb_realpath does not have to start with | |
360 | IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */ | |
202cbf1c JK |
361 | if (pattern_len == 0) |
362 | { | |
363 | if (debug_auto_load) | |
364 | fprintf_unfiltered (gdb_stdlog, | |
365 | _("auto-load: Matched - empty pattern\n")); | |
366 | return 1; | |
367 | } | |
368 | ||
369 | for (;;) | |
370 | { | |
371 | /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the | |
372 | same way so they will match. */ | |
373 | while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1])) | |
374 | filename_len--; | |
375 | filename[filename_len] = '\0'; | |
376 | if (filename_len == 0) | |
377 | { | |
378 | if (debug_auto_load) | |
379 | fprintf_unfiltered (gdb_stdlog, | |
380 | _("auto-load: Not matched - pattern \"%s\".\n"), | |
381 | pattern); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE) | |
386 | == 0) | |
387 | { | |
388 | if (debug_auto_load) | |
389 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file " | |
390 | "\"%s\" to pattern \"%s\".\n"), | |
391 | filename, pattern); | |
392 | return 1; | |
393 | } | |
394 | ||
395 | /* Trim trailing FILENAME component. */ | |
396 | while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1])) | |
397 | filename_len--; | |
398 | } | |
399 | } | |
400 | ||
401 | /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in | |
402 | a subdirectory of a directory that matches PATTERN. Return 0 otherwise. | |
403 | gdb_realpath normalization is never done here. */ | |
404 | ||
405 | static ATTRIBUTE_PURE int | |
406 | filename_is_in_pattern (const char *filename, const char *pattern) | |
407 | { | |
408 | char *filename_copy, *pattern_copy; | |
409 | ||
224c3ddb | 410 | filename_copy = (char *) alloca (strlen (filename) + 1); |
202cbf1c | 411 | strcpy (filename_copy, filename); |
224c3ddb | 412 | pattern_copy = (char *) alloca (strlen (pattern) + 1); |
202cbf1c | 413 | strcpy (pattern_copy, pattern); |
1ef71717 | 414 | |
202cbf1c | 415 | return filename_is_in_pattern_1 (filename_copy, pattern_copy); |
bccbefd2 JK |
416 | } |
417 | ||
418 | /* Return 1 if FILENAME belongs to one of directory components of | |
419 | AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise. | |
420 | auto_load_safe_path_vec_update is never called. | |
14278e1f | 421 | *FILENAME_REALP may be updated by gdb_realpath of FILENAME. */ |
bccbefd2 JK |
422 | |
423 | static int | |
424 | filename_is_in_auto_load_safe_path_vec (const char *filename, | |
14278e1f | 425 | gdb::unique_xmalloc_ptr<char> *filename_realp) |
bccbefd2 | 426 | { |
202cbf1c | 427 | char *pattern; |
bccbefd2 JK |
428 | int ix; |
429 | ||
202cbf1c JK |
430 | for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); |
431 | ++ix) | |
432 | if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern)) | |
bccbefd2 JK |
433 | break; |
434 | ||
202cbf1c | 435 | if (pattern == NULL) |
bccbefd2 JK |
436 | { |
437 | if (*filename_realp == NULL) | |
4dc84fd1 JK |
438 | { |
439 | *filename_realp = gdb_realpath (filename); | |
14278e1f | 440 | if (debug_auto_load && strcmp (filename_realp->get (), filename) != 0) |
4dc84fd1 JK |
441 | fprintf_unfiltered (gdb_stdlog, |
442 | _("auto-load: Resolved " | |
443 | "file \"%s\" as \"%s\".\n"), | |
14278e1f | 444 | filename, filename_realp->get ()); |
4dc84fd1 | 445 | } |
bccbefd2 | 446 | |
14278e1f | 447 | if (strcmp (filename_realp->get (), filename) != 0) |
202cbf1c JK |
448 | for (ix = 0; |
449 | VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix) | |
14278e1f | 450 | if (filename_is_in_pattern (filename_realp->get (), pattern)) |
4dc84fd1 | 451 | break; |
bccbefd2 JK |
452 | } |
453 | ||
202cbf1c | 454 | if (pattern != NULL) |
4dc84fd1 JK |
455 | { |
456 | if (debug_auto_load) | |
457 | fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches " | |
458 | "directory \"%s\".\n"), | |
202cbf1c | 459 | filename, pattern); |
4dc84fd1 JK |
460 | return 1; |
461 | } | |
bccbefd2 JK |
462 | |
463 | return 0; | |
464 | } | |
465 | ||
466 | /* Return 1 if FILENAME is located in one of the directories of | |
467 | AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does | |
468 | not have to be an absolute path. | |
469 | ||
470 | Existence of FILENAME is not checked. Function will still give a warning | |
471 | even if the caller would quietly skip non-existing file in unsafe | |
472 | directory. */ | |
473 | ||
474 | int | |
4dc84fd1 | 475 | file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...) |
bccbefd2 | 476 | { |
14278e1f | 477 | gdb::unique_xmalloc_ptr<char> filename_real; |
9a950c7c | 478 | static int advice_printed = 0; |
bccbefd2 | 479 | |
4dc84fd1 JK |
480 | if (debug_auto_load) |
481 | { | |
482 | va_list debug_args; | |
483 | ||
484 | va_start (debug_args, debug_fmt); | |
485 | vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args); | |
486 | va_end (debug_args); | |
487 | } | |
488 | ||
bccbefd2 | 489 | if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real)) |
14278e1f | 490 | return 1; |
bccbefd2 JK |
491 | |
492 | auto_load_safe_path_vec_update (); | |
493 | if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real)) | |
14278e1f | 494 | return 1; |
bccbefd2 JK |
495 | |
496 | warning (_("File \"%s\" auto-loading has been declined by your " | |
497 | "`auto-load safe-path' set to \"%s\"."), | |
14278e1f | 498 | filename_real.get (), auto_load_safe_path); |
bccbefd2 | 499 | |
9a950c7c JK |
500 | if (!advice_printed) |
501 | { | |
502 | const char *homedir = getenv ("HOME"); | |
9a950c7c JK |
503 | |
504 | if (homedir == NULL) | |
505 | homedir = "$HOME"; | |
14278e1f | 506 | std::string homeinit = string_printf ("%s/%s", homedir, gdbinit); |
9a950c7c JK |
507 | |
508 | printf_filtered (_("\ | |
509 | To enable execution of this file add\n\ | |
510 | \tadd-auto-load-safe-path %s\n\ | |
511 | line to your configuration file \"%s\".\n\ | |
512 | To completely disable this security protection add\n\ | |
513 | \tset auto-load safe-path /\n\ | |
514 | line to your configuration file \"%s\".\n\ | |
515 | For more information about this security protection see the\n\ | |
516 | \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\ | |
517 | \tinfo \"(gdb)Auto-loading safe path\"\n"), | |
14278e1f TT |
518 | filename_real.get (), |
519 | homeinit.c_str (), homeinit.c_str ()); | |
9a950c7c JK |
520 | advice_printed = 1; |
521 | } | |
522 | ||
bccbefd2 JK |
523 | return 0; |
524 | } | |
525 | ||
e2207b9a JK |
526 | /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load |
527 | the same script. There's no point in loading the script multiple times, | |
528 | and there can be a lot of objfiles and scripts, so we keep track of scripts | |
529 | loaded this way. */ | |
530 | ||
531 | struct auto_load_pspace_info | |
532 | { | |
9f050062 DE |
533 | /* For each program space we keep track of loaded scripts, both when |
534 | specified as file names and as scripts to be executed directly. */ | |
535 | struct htab *loaded_script_files; | |
536 | struct htab *loaded_script_texts; | |
e2207b9a | 537 | |
6dddc817 DE |
538 | /* Non-zero if we've issued the warning about an auto-load script not being |
539 | supported. We only want to issue this warning once. */ | |
540 | int unsupported_script_warning_printed; | |
541 | ||
e2207b9a JK |
542 | /* Non-zero if we've issued the warning about an auto-load script not being |
543 | found. We only want to issue this warning once. */ | |
544 | int script_not_found_warning_printed; | |
545 | }; | |
546 | ||
9f050062 | 547 | /* Objects of this type are stored in the loaded_script hash table. */ |
e2207b9a JK |
548 | |
549 | struct loaded_script | |
550 | { | |
551 | /* Name as provided by the objfile. */ | |
552 | const char *name; | |
bf88dd68 | 553 | |
e2207b9a | 554 | /* Full path name or NULL if script wasn't found (or was otherwise |
9f050062 | 555 | inaccessible), or NULL for loaded_script_texts. */ |
e2207b9a | 556 | const char *full_path; |
bf88dd68 | 557 | |
bccbefd2 JK |
558 | /* Non-zero if this script has been loaded. */ |
559 | int loaded; | |
560 | ||
6dddc817 | 561 | const struct extension_language_defn *language; |
e2207b9a JK |
562 | }; |
563 | ||
564 | /* Per-program-space data key. */ | |
565 | static const struct program_space_data *auto_load_pspace_data; | |
566 | ||
567 | static void | |
568 | auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg) | |
569 | { | |
9a3c8263 | 570 | struct auto_load_pspace_info *info = (struct auto_load_pspace_info *) arg; |
e2207b9a | 571 | |
9f050062 DE |
572 | if (info->loaded_script_files) |
573 | htab_delete (info->loaded_script_files); | |
574 | if (info->loaded_script_texts) | |
575 | htab_delete (info->loaded_script_texts); | |
487ad57c | 576 | xfree (info); |
e2207b9a JK |
577 | } |
578 | ||
579 | /* Get the current autoload data. If none is found yet, add it now. This | |
580 | function always returns a valid object. */ | |
581 | ||
582 | static struct auto_load_pspace_info * | |
583 | get_auto_load_pspace_data (struct program_space *pspace) | |
584 | { | |
585 | struct auto_load_pspace_info *info; | |
586 | ||
9a3c8263 SM |
587 | info = ((struct auto_load_pspace_info *) |
588 | program_space_data (pspace, auto_load_pspace_data)); | |
e2207b9a JK |
589 | if (info == NULL) |
590 | { | |
41bf6aca | 591 | info = XCNEW (struct auto_load_pspace_info); |
e2207b9a JK |
592 | set_program_space_data (pspace, auto_load_pspace_data, info); |
593 | } | |
594 | ||
595 | return info; | |
596 | } | |
597 | ||
598 | /* Hash function for the loaded script hash. */ | |
599 | ||
600 | static hashval_t | |
601 | hash_loaded_script_entry (const void *data) | |
602 | { | |
9a3c8263 | 603 | const struct loaded_script *e = (const struct loaded_script *) data; |
e2207b9a | 604 | |
bf88dd68 | 605 | return htab_hash_string (e->name) ^ htab_hash_pointer (e->language); |
e2207b9a JK |
606 | } |
607 | ||
608 | /* Equality function for the loaded script hash. */ | |
609 | ||
610 | static int | |
611 | eq_loaded_script_entry (const void *a, const void *b) | |
612 | { | |
9a3c8263 SM |
613 | const struct loaded_script *ea = (const struct loaded_script *) a; |
614 | const struct loaded_script *eb = (const struct loaded_script *) b; | |
e2207b9a | 615 | |
bf88dd68 | 616 | return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language; |
e2207b9a JK |
617 | } |
618 | ||
619 | /* Initialize the table to track loaded scripts. | |
620 | Each entry is hashed by the full path name. */ | |
621 | ||
622 | static void | |
623 | init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info) | |
624 | { | |
625 | /* Choose 31 as the starting size of the hash table, somewhat arbitrarily. | |
626 | Space for each entry is obtained with one malloc so we can free them | |
627 | easily. */ | |
628 | ||
9f050062 DE |
629 | pspace_info->loaded_script_files = htab_create (31, |
630 | hash_loaded_script_entry, | |
631 | eq_loaded_script_entry, | |
632 | xfree); | |
633 | pspace_info->loaded_script_texts = htab_create (31, | |
634 | hash_loaded_script_entry, | |
635 | eq_loaded_script_entry, | |
636 | xfree); | |
e2207b9a | 637 | |
6dddc817 | 638 | pspace_info->unsupported_script_warning_printed = FALSE; |
e2207b9a JK |
639 | pspace_info->script_not_found_warning_printed = FALSE; |
640 | } | |
641 | ||
642 | /* Wrapper on get_auto_load_pspace_data to also allocate the hash table | |
643 | for loading scripts. */ | |
644 | ||
645 | struct auto_load_pspace_info * | |
646 | get_auto_load_pspace_data_for_loading (struct program_space *pspace) | |
647 | { | |
648 | struct auto_load_pspace_info *info; | |
649 | ||
650 | info = get_auto_load_pspace_data (pspace); | |
9f050062 | 651 | if (info->loaded_script_files == NULL) |
e2207b9a JK |
652 | init_loaded_scripts_info (info); |
653 | ||
654 | return info; | |
655 | } | |
656 | ||
9f050062 DE |
657 | /* Add script file NAME in LANGUAGE to hash table of PSPACE_INFO. |
658 | LOADED 1 if the script has been (is going to) be loaded, 0 otherwise | |
659 | (such as if it has not been found). | |
660 | FULL_PATH is NULL if the script wasn't found. | |
661 | The result is true if the script was already in the hash table. */ | |
e2207b9a | 662 | |
6dddc817 | 663 | static int |
9f050062 DE |
664 | maybe_add_script_file (struct auto_load_pspace_info *pspace_info, int loaded, |
665 | const char *name, const char *full_path, | |
666 | const struct extension_language_defn *language) | |
e2207b9a | 667 | { |
9f050062 | 668 | struct htab *htab = pspace_info->loaded_script_files; |
e2207b9a JK |
669 | struct loaded_script **slot, entry; |
670 | int in_hash_table; | |
671 | ||
672 | entry.name = name; | |
bf88dd68 | 673 | entry.language = language; |
e2207b9a JK |
674 | slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT); |
675 | in_hash_table = *slot != NULL; | |
676 | ||
677 | /* If this script is not in the hash table, add it. */ | |
678 | ||
9f050062 | 679 | if (!in_hash_table) |
e2207b9a JK |
680 | { |
681 | char *p; | |
682 | ||
683 | /* Allocate all space in one chunk so it's easier to free. */ | |
224c3ddb SM |
684 | *slot = ((struct loaded_script *) |
685 | xmalloc (sizeof (**slot) | |
686 | + strlen (name) + 1 | |
687 | + (full_path != NULL ? (strlen (full_path) + 1) : 0))); | |
e2207b9a JK |
688 | p = ((char*) *slot) + sizeof (**slot); |
689 | strcpy (p, name); | |
690 | (*slot)->name = p; | |
691 | if (full_path != NULL) | |
692 | { | |
693 | p += strlen (p) + 1; | |
694 | strcpy (p, full_path); | |
695 | (*slot)->full_path = p; | |
696 | } | |
697 | else | |
698 | (*slot)->full_path = NULL; | |
bccbefd2 | 699 | (*slot)->loaded = loaded; |
bf88dd68 | 700 | (*slot)->language = language; |
e2207b9a JK |
701 | } |
702 | ||
703 | return in_hash_table; | |
704 | } | |
705 | ||
9f050062 DE |
706 | /* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO. |
707 | LOADED 1 if the script has been (is going to) be loaded, 0 otherwise | |
708 | (such as if it has not been found). | |
709 | The result is true if the script was already in the hash table. */ | |
710 | ||
711 | static int | |
712 | maybe_add_script_text (struct auto_load_pspace_info *pspace_info, | |
713 | int loaded, const char *name, | |
714 | const struct extension_language_defn *language) | |
715 | { | |
716 | struct htab *htab = pspace_info->loaded_script_texts; | |
717 | struct loaded_script **slot, entry; | |
718 | int in_hash_table; | |
719 | ||
720 | entry.name = name; | |
721 | entry.language = language; | |
722 | slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT); | |
723 | in_hash_table = *slot != NULL; | |
724 | ||
725 | /* If this script is not in the hash table, add it. */ | |
726 | ||
727 | if (!in_hash_table) | |
728 | { | |
729 | char *p; | |
730 | ||
731 | /* Allocate all space in one chunk so it's easier to free. */ | |
224c3ddb SM |
732 | *slot = ((struct loaded_script *) |
733 | xmalloc (sizeof (**slot) + strlen (name) + 1)); | |
9f050062 DE |
734 | p = ((char*) *slot) + sizeof (**slot); |
735 | strcpy (p, name); | |
736 | (*slot)->name = p; | |
737 | (*slot)->full_path = NULL; | |
738 | (*slot)->loaded = loaded; | |
739 | (*slot)->language = language; | |
740 | } | |
741 | ||
742 | return in_hash_table; | |
743 | } | |
744 | ||
e2207b9a JK |
745 | /* Clear the table of loaded section scripts. */ |
746 | ||
747 | static void | |
748 | clear_section_scripts (void) | |
749 | { | |
750 | struct program_space *pspace = current_program_space; | |
751 | struct auto_load_pspace_info *info; | |
752 | ||
9a3c8263 SM |
753 | info = ((struct auto_load_pspace_info *) |
754 | program_space_data (pspace, auto_load_pspace_data)); | |
9f050062 | 755 | if (info != NULL && info->loaded_script_files != NULL) |
e2207b9a | 756 | { |
9f050062 DE |
757 | htab_delete (info->loaded_script_files); |
758 | htab_delete (info->loaded_script_texts); | |
759 | info->loaded_script_files = NULL; | |
760 | info->loaded_script_texts = NULL; | |
6dddc817 | 761 | info->unsupported_script_warning_printed = FALSE; |
e2207b9a JK |
762 | info->script_not_found_warning_printed = FALSE; |
763 | } | |
764 | } | |
765 | ||
e9687799 JK |
766 | /* Look for the auto-load script in LANGUAGE associated with OBJFILE where |
767 | OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any | |
768 | matching script, return 0 otherwise. */ | |
e2207b9a | 769 | |
e9687799 JK |
770 | static int |
771 | auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, | |
6dddc817 | 772 | const struct extension_language_defn *language) |
e2207b9a | 773 | { |
e2207b9a | 774 | char *filename, *debugfile; |
e9687799 | 775 | int len, retval; |
e2207b9a | 776 | struct cleanup *cleanups; |
6dddc817 | 777 | const char *suffix = ext_lang_auto_load_suffix (language); |
e2207b9a | 778 | |
e2207b9a | 779 | len = strlen (realname); |
224c3ddb | 780 | filename = (char *) xmalloc (len + strlen (suffix) + 1); |
e2207b9a | 781 | memcpy (filename, realname, len); |
6dddc817 | 782 | strcpy (filename + len, suffix); |
e2207b9a JK |
783 | |
784 | cleanups = make_cleanup (xfree, filename); | |
e2207b9a | 785 | |
d419f42d | 786 | gdb_file_up input = gdb_fopen_cloexec (filename, "r"); |
e2207b9a | 787 | debugfile = filename; |
7349ff92 JK |
788 | if (debug_auto_load) |
789 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"), | |
790 | debugfile, input ? _("exists") : _("does not exist")); | |
e2207b9a | 791 | |
7349ff92 | 792 | if (!input) |
e2207b9a | 793 | { |
7349ff92 JK |
794 | VEC (char_ptr) *vec; |
795 | int ix; | |
796 | char *dir; | |
797 | ||
e2207b9a JK |
798 | /* Also try the same file in a subdirectory of gdb's data |
799 | directory. */ | |
7349ff92 | 800 | |
1564a261 | 801 | vec = auto_load_expand_dir_vars (auto_load_dir); |
7349ff92 JK |
802 | make_cleanup_free_char_ptr_vec (vec); |
803 | ||
804 | if (debug_auto_load) | |
805 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load " | |
806 | "scripts-directory' path \"%s\".\n"), | |
807 | auto_load_dir); | |
808 | ||
809 | for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix) | |
810 | { | |
224c3ddb | 811 | debugfile = (char *) xmalloc (strlen (dir) + strlen (filename) + 1); |
1564a261 | 812 | strcpy (debugfile, dir); |
7349ff92 JK |
813 | |
814 | /* FILENAME is absolute, so we don't need a "/" here. */ | |
815 | strcat (debugfile, filename); | |
816 | ||
817 | make_cleanup (xfree, debugfile); | |
614c279d | 818 | input = gdb_fopen_cloexec (debugfile, "r"); |
7349ff92 JK |
819 | if (debug_auto_load) |
820 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file " | |
821 | "\"%s\" %s.\n"), | |
822 | debugfile, | |
823 | input ? _("exists") : _("does not exist")); | |
824 | if (input != NULL) | |
825 | break; | |
826 | } | |
e2207b9a JK |
827 | } |
828 | ||
829 | if (input) | |
830 | { | |
5b2bf947 DE |
831 | int is_safe; |
832 | struct auto_load_pspace_info *pspace_info; | |
833 | ||
5b2bf947 | 834 | is_safe |
849c862e | 835 | = file_is_auto_load_safe (debugfile, |
5b2bf947 DE |
836 | _("auto-load: Loading %s script \"%s\"" |
837 | " by extension for objfile \"%s\".\n"), | |
6dddc817 | 838 | ext_lang_name (language), |
849c862e | 839 | debugfile, objfile_name (objfile)); |
5b2bf947 DE |
840 | |
841 | /* Add this script to the hash table too so | |
842 | "info auto-load ${lang}-scripts" can print it. */ | |
843 | pspace_info | |
844 | = get_auto_load_pspace_data_for_loading (current_program_space); | |
9f050062 DE |
845 | maybe_add_script_file (pspace_info, is_safe, debugfile, debugfile, |
846 | language); | |
5b2bf947 | 847 | |
e2207b9a JK |
848 | /* To preserve existing behaviour we don't check for whether the |
849 | script was already in the table, and always load it. | |
850 | It's highly unlikely that we'd ever load it twice, | |
851 | and these scripts are required to be idempotent under multiple | |
852 | loads anyway. */ | |
5b2bf947 | 853 | if (is_safe) |
6dddc817 DE |
854 | { |
855 | objfile_script_sourcer_func *sourcer | |
856 | = ext_lang_objfile_script_sourcer (language); | |
857 | ||
858 | /* We shouldn't get here if support for the language isn't | |
859 | compiled in. And the extension language is required to implement | |
860 | this function. */ | |
861 | gdb_assert (sourcer != NULL); | |
d419f42d | 862 | sourcer (language, objfile, input.get (), debugfile); |
6dddc817 | 863 | } |
e9687799 JK |
864 | |
865 | retval = 1; | |
866 | } | |
867 | else | |
868 | retval = 0; | |
869 | ||
870 | do_cleanups (cleanups); | |
871 | return retval; | |
872 | } | |
873 | ||
874 | /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load | |
875 | it. */ | |
876 | ||
6dddc817 | 877 | void |
e9687799 | 878 | auto_load_objfile_script (struct objfile *objfile, |
6dddc817 | 879 | const struct extension_language_defn *language) |
e9687799 | 880 | { |
14278e1f TT |
881 | gdb::unique_xmalloc_ptr<char> realname |
882 | = gdb_realpath (objfile_name (objfile)); | |
e9687799 | 883 | |
14278e1f | 884 | if (!auto_load_objfile_script_1 (objfile, realname.get (), language)) |
e9687799 JK |
885 | { |
886 | /* For Windows/DOS .exe executables, strip the .exe suffix, so that | |
887 | FOO-gdb.gdb could be used for FOO.exe, and try again. */ | |
888 | ||
14278e1f | 889 | size_t len = strlen (realname.get ()); |
e9687799 JK |
890 | const size_t lexe = sizeof (".exe") - 1; |
891 | ||
14278e1f | 892 | if (len > lexe && strcasecmp (realname.get () + len - lexe, ".exe") == 0) |
e9687799 JK |
893 | { |
894 | len -= lexe; | |
14278e1f | 895 | realname.get ()[len] = '\0'; |
e9687799 JK |
896 | if (debug_auto_load) |
897 | fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, " | |
898 | "retrying with \"%s\".\n"), | |
14278e1f TT |
899 | realname.get ()); |
900 | auto_load_objfile_script_1 (objfile, realname.get (), language); | |
e9687799 | 901 | } |
e2207b9a | 902 | } |
e2207b9a JK |
903 | } |
904 | ||
9f050062 DE |
905 | /* Subroutine of source_section_scripts to simplify it. |
906 | Load FILE as a script in extension language LANGUAGE. | |
907 | The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */ | |
908 | ||
909 | static void | |
910 | source_script_file (struct auto_load_pspace_info *pspace_info, | |
911 | struct objfile *objfile, | |
912 | const struct extension_language_defn *language, | |
913 | const char *section_name, unsigned int offset, | |
914 | const char *file) | |
915 | { | |
ed166945 | 916 | int in_hash_table; |
9f050062 DE |
917 | objfile_script_sourcer_func *sourcer; |
918 | ||
919 | /* Skip this script if support is not compiled in. */ | |
920 | sourcer = ext_lang_objfile_script_sourcer (language); | |
921 | if (sourcer == NULL) | |
922 | { | |
923 | /* We don't throw an error, the program is still debuggable. */ | |
924 | maybe_print_unsupported_script_warning (pspace_info, objfile, language, | |
925 | section_name, offset); | |
926 | /* We *could* still try to open it, but there's no point. */ | |
927 | maybe_add_script_file (pspace_info, 0, file, NULL, language); | |
928 | return; | |
929 | } | |
930 | ||
931 | /* Skip this script if auto-loading it has been disabled. */ | |
932 | if (!ext_lang_auto_load_enabled (language)) | |
933 | { | |
934 | /* No message is printed, just skip it. */ | |
935 | return; | |
936 | } | |
937 | ||
ed166945 TT |
938 | gdb::optional<open_script> opened = find_and_open_script (file, |
939 | 1 /*search_path*/); | |
9f050062 | 940 | |
9f050062 DE |
941 | if (opened) |
942 | { | |
ed166945 | 943 | if (!file_is_auto_load_safe (opened->full_path.get (), |
9f050062 DE |
944 | _("auto-load: Loading %s script " |
945 | "\"%s\" from section \"%s\" of " | |
946 | "objfile \"%s\".\n"), | |
ed166945 TT |
947 | ext_lang_name (language), |
948 | opened->full_path.get (), | |
9f050062 | 949 | section_name, objfile_name (objfile))) |
ed166945 | 950 | opened.reset (); |
9f050062 DE |
951 | } |
952 | else | |
953 | { | |
9f050062 DE |
954 | /* If one script isn't found it's not uncommon for more to not be |
955 | found either. We don't want to print a message for each script, | |
956 | too much noise. Instead, we print the warning once and tell the | |
957 | user how to find the list of scripts that weren't loaded. | |
958 | We don't throw an error, the program is still debuggable. | |
959 | ||
960 | IWBN if complaints.c were more general-purpose. */ | |
961 | ||
962 | maybe_print_script_not_found_warning (pspace_info, objfile, language, | |
963 | section_name, offset); | |
964 | } | |
965 | ||
ed166945 TT |
966 | in_hash_table = maybe_add_script_file (pspace_info, bool (opened), file, |
967 | (opened | |
968 | ? opened->full_path.get () | |
969 | : NULL), | |
9f050062 DE |
970 | language); |
971 | ||
972 | /* If this file is not currently loaded, load it. */ | |
973 | if (opened && !in_hash_table) | |
ed166945 TT |
974 | sourcer (language, objfile, opened->stream.get (), |
975 | opened->full_path.get ()); | |
9f050062 DE |
976 | } |
977 | ||
978 | /* Subroutine of source_section_scripts to simplify it. | |
979 | Execute SCRIPT as a script in extension language LANG. | |
980 | The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */ | |
981 | ||
982 | static void | |
983 | execute_script_contents (struct auto_load_pspace_info *pspace_info, | |
984 | struct objfile *objfile, | |
985 | const struct extension_language_defn *language, | |
986 | const char *section_name, unsigned int offset, | |
987 | const char *script) | |
988 | { | |
989 | objfile_script_executor_func *executor; | |
990 | const char *newline, *script_text; | |
870f88f7 | 991 | char *name; |
9f050062 DE |
992 | int is_safe, in_hash_table; |
993 | struct cleanup *cleanups; | |
994 | ||
995 | cleanups = make_cleanup (null_cleanup, NULL); | |
996 | ||
997 | /* The first line of the script is the name of the script. | |
998 | It must not contain any kind of space character. */ | |
999 | name = NULL; | |
1000 | newline = strchr (script, '\n'); | |
1001 | if (newline != NULL) | |
1002 | { | |
1003 | char *buf, *p; | |
1004 | ||
1005 | /* Put the name in a buffer and validate it. */ | |
1006 | buf = xstrndup (script, newline - script); | |
1007 | make_cleanup (xfree, buf); | |
1008 | for (p = buf; *p != '\0'; ++p) | |
1009 | { | |
1010 | if (isspace (*p)) | |
1011 | break; | |
1012 | } | |
1013 | /* We don't allow nameless scripts, they're not helpful to the user. */ | |
1014 | if (p != buf && *p == '\0') | |
1015 | name = buf; | |
1016 | } | |
1017 | if (name == NULL) | |
1018 | { | |
1019 | /* We don't throw an error, the program is still debuggable. */ | |
1020 | warning (_("\ | |
1021 | Missing/bad script name in entry at offset %u in section %s\n\ | |
1022 | of file %s."), | |
1023 | offset, section_name, objfile_name (objfile)); | |
1024 | do_cleanups (cleanups); | |
1025 | return; | |
1026 | } | |
1027 | script_text = newline + 1; | |
1028 | ||
1029 | /* Skip this script if support is not compiled in. */ | |
1030 | executor = ext_lang_objfile_script_executor (language); | |
1031 | if (executor == NULL) | |
1032 | { | |
1033 | /* We don't throw an error, the program is still debuggable. */ | |
1034 | maybe_print_unsupported_script_warning (pspace_info, objfile, language, | |
1035 | section_name, offset); | |
1036 | maybe_add_script_text (pspace_info, 0, name, language); | |
1037 | do_cleanups (cleanups); | |
1038 | return; | |
1039 | } | |
1040 | ||
1041 | /* Skip this script if auto-loading it has been disabled. */ | |
1042 | if (!ext_lang_auto_load_enabled (language)) | |
1043 | { | |
1044 | /* No message is printed, just skip it. */ | |
1045 | do_cleanups (cleanups); | |
1046 | return; | |
1047 | } | |
1048 | ||
1049 | is_safe = file_is_auto_load_safe (objfile_name (objfile), | |
1050 | _("auto-load: Loading %s script " | |
1051 | "\"%s\" from section \"%s\" of " | |
1052 | "objfile \"%s\".\n"), | |
1053 | ext_lang_name (language), name, | |
1054 | section_name, objfile_name (objfile)); | |
1055 | ||
1056 | in_hash_table = maybe_add_script_text (pspace_info, is_safe, name, language); | |
1057 | ||
1058 | /* If this file is not currently loaded, load it. */ | |
1059 | if (is_safe && !in_hash_table) | |
1060 | executor (language, objfile, name, script_text); | |
1061 | ||
1062 | do_cleanups (cleanups); | |
1063 | } | |
1064 | ||
5b2bf947 DE |
1065 | /* Load scripts specified in OBJFILE. |
1066 | START,END delimit a buffer containing a list of nul-terminated | |
1067 | file names. | |
1068 | SECTION_NAME is used in error messages. | |
1069 | ||
9f050062 DE |
1070 | Scripts specified as file names are found per normal "source -s" command |
1071 | processing. First the script is looked for in $cwd. If not found there | |
1072 | the source search path is used. | |
5b2bf947 | 1073 | |
9f050062 DE |
1074 | The section contains a list of path names of script files to load or |
1075 | actual script contents. Each entry is nul-terminated. */ | |
5b2bf947 DE |
1076 | |
1077 | static void | |
1078 | source_section_scripts (struct objfile *objfile, const char *section_name, | |
1079 | const char *start, const char *end) | |
1080 | { | |
1081 | const char *p; | |
1082 | struct auto_load_pspace_info *pspace_info; | |
1083 | ||
1084 | pspace_info = get_auto_load_pspace_data_for_loading (current_program_space); | |
1085 | ||
1086 | for (p = start; p < end; ++p) | |
1087 | { | |
9f050062 | 1088 | const char *entry; |
ed3ef339 | 1089 | const struct extension_language_defn *language; |
9f050062 DE |
1090 | unsigned int offset = p - start; |
1091 | int code = *p; | |
5b2bf947 | 1092 | |
9f050062 | 1093 | switch (code) |
5b2bf947 | 1094 | { |
ed3ef339 | 1095 | case SECTION_SCRIPT_ID_PYTHON_FILE: |
9f050062 | 1096 | case SECTION_SCRIPT_ID_PYTHON_TEXT: |
ed3ef339 DE |
1097 | language = get_ext_lang_defn (EXT_LANG_PYTHON); |
1098 | break; | |
1099 | case SECTION_SCRIPT_ID_SCHEME_FILE: | |
9f050062 | 1100 | case SECTION_SCRIPT_ID_SCHEME_TEXT: |
ed3ef339 DE |
1101 | language = get_ext_lang_defn (EXT_LANG_GUILE); |
1102 | break; | |
1103 | default: | |
5b2bf947 DE |
1104 | warning (_("Invalid entry in %s section"), section_name); |
1105 | /* We could try various heuristics to find the next valid entry, | |
1106 | but it's safer to just punt. */ | |
ed3ef339 | 1107 | return; |
5b2bf947 | 1108 | } |
9f050062 | 1109 | entry = ++p; |
5b2bf947 DE |
1110 | |
1111 | while (p < end && *p != '\0') | |
1112 | ++p; | |
1113 | if (p == end) | |
1114 | { | |
9f050062 DE |
1115 | warning (_("Non-nul-terminated entry in %s at offset %u"), |
1116 | section_name, offset); | |
1117 | /* Don't load/execute it. */ | |
5b2bf947 DE |
1118 | break; |
1119 | } | |
6dddc817 | 1120 | |
9f050062 | 1121 | switch (code) |
6dddc817 | 1122 | { |
9f050062 DE |
1123 | case SECTION_SCRIPT_ID_PYTHON_FILE: |
1124 | case SECTION_SCRIPT_ID_SCHEME_FILE: | |
1125 | if (p == entry) | |
6dddc817 | 1126 | { |
9f050062 DE |
1127 | warning (_("Empty entry in %s at offset %u"), |
1128 | section_name, offset); | |
1129 | continue; | |
6dddc817 | 1130 | } |
9f050062 DE |
1131 | source_script_file (pspace_info, objfile, language, |
1132 | section_name, offset, entry); | |
1133 | break; | |
1134 | case SECTION_SCRIPT_ID_PYTHON_TEXT: | |
1135 | case SECTION_SCRIPT_ID_SCHEME_TEXT: | |
1136 | execute_script_contents (pspace_info, objfile, language, | |
1137 | section_name, offset, entry); | |
1138 | break; | |
5b2bf947 | 1139 | } |
5b2bf947 DE |
1140 | } |
1141 | } | |
1142 | ||
1143 | /* Load scripts specified in section SECTION_NAME of OBJFILE. */ | |
1144 | ||
1145 | static void | |
1146 | auto_load_section_scripts (struct objfile *objfile, const char *section_name) | |
1147 | { | |
1148 | bfd *abfd = objfile->obfd; | |
1149 | asection *scripts_sect; | |
1150 | bfd_byte *data = NULL; | |
1151 | ||
1152 | scripts_sect = bfd_get_section_by_name (abfd, section_name); | |
ec13808e JK |
1153 | if (scripts_sect == NULL |
1154 | || (bfd_get_section_flags (abfd, scripts_sect) & SEC_HAS_CONTENTS) == 0) | |
5b2bf947 DE |
1155 | return; |
1156 | ||
1157 | if (!bfd_get_full_section_contents (abfd, scripts_sect, &data)) | |
1158 | warning (_("Couldn't read %s section of %s"), | |
1159 | section_name, bfd_get_filename (abfd)); | |
1160 | else | |
1161 | { | |
1162 | struct cleanup *cleanups; | |
1163 | char *p = (char *) data; | |
1164 | ||
1165 | cleanups = make_cleanup (xfree, p); | |
1166 | source_section_scripts (objfile, section_name, p, | |
1167 | p + bfd_get_section_size (scripts_sect)); | |
1168 | do_cleanups (cleanups); | |
1169 | } | |
1170 | } | |
1171 | ||
bf88dd68 JK |
1172 | /* Load any auto-loaded scripts for OBJFILE. */ |
1173 | ||
1174 | void | |
1175 | load_auto_scripts_for_objfile (struct objfile *objfile) | |
1176 | { | |
c47cf547 DE |
1177 | /* Return immediately if auto-loading has been globally disabled. |
1178 | This is to handle sequencing of operations during gdb startup. | |
5fbae7d1 GB |
1179 | Also return immediately if OBJFILE was not created from a file |
1180 | on the local filesystem. */ | |
1181 | if (!global_auto_load | |
1182 | || (objfile->flags & OBJF_NOT_FILENAME) != 0 | |
1183 | || is_target_filename (objfile->original_name)) | |
bf88dd68 JK |
1184 | return; |
1185 | ||
6dddc817 DE |
1186 | /* Load any extension language scripts for this objfile. |
1187 | E.g., foo-gdb.gdb, foo-gdb.py. */ | |
1188 | auto_load_ext_lang_scripts_for_objfile (objfile); | |
bf88dd68 | 1189 | |
c47cf547 | 1190 | /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */ |
5b2bf947 | 1191 | auto_load_section_scripts (objfile, AUTO_SECTION_NAME); |
bf88dd68 JK |
1192 | } |
1193 | ||
e2207b9a JK |
1194 | /* This is a new_objfile observer callback to auto-load scripts. |
1195 | ||
1196 | Two flavors of auto-loaded scripts are supported. | |
1197 | 1) based on the path to the objfile | |
1198 | 2) from .debug_gdb_scripts section */ | |
1199 | ||
1200 | static void | |
1201 | auto_load_new_objfile (struct objfile *objfile) | |
1202 | { | |
1203 | if (!objfile) | |
1204 | { | |
1205 | /* OBJFILE is NULL when loading a new "main" symbol-file. */ | |
1206 | clear_section_scripts (); | |
1207 | return; | |
1208 | } | |
1209 | ||
1210 | load_auto_scripts_for_objfile (objfile); | |
1211 | } | |
1212 | ||
1213 | /* Collect scripts to be printed in a vec. */ | |
1214 | ||
1215 | typedef struct loaded_script *loaded_script_ptr; | |
1216 | DEF_VEC_P (loaded_script_ptr); | |
1217 | ||
bf88dd68 JK |
1218 | struct collect_matching_scripts_data |
1219 | { | |
1220 | VEC (loaded_script_ptr) **scripts_p; | |
1221 | ||
6dddc817 | 1222 | const struct extension_language_defn *language; |
bf88dd68 JK |
1223 | }; |
1224 | ||
e2207b9a JK |
1225 | /* Traversal function for htab_traverse. |
1226 | Collect the entry if it matches the regexp. */ | |
1227 | ||
1228 | static int | |
1229 | collect_matching_scripts (void **slot, void *info) | |
1230 | { | |
9a3c8263 SM |
1231 | struct loaded_script *script = (struct loaded_script *) *slot; |
1232 | struct collect_matching_scripts_data *data | |
1233 | = (struct collect_matching_scripts_data *) info; | |
e2207b9a | 1234 | |
bf88dd68 JK |
1235 | if (script->language == data->language && re_exec (script->name)) |
1236 | VEC_safe_push (loaded_script_ptr, *data->scripts_p, script); | |
e2207b9a JK |
1237 | |
1238 | return 1; | |
1239 | } | |
1240 | ||
1241 | /* Print SCRIPT. */ | |
1242 | ||
1243 | static void | |
1244 | print_script (struct loaded_script *script) | |
1245 | { | |
1246 | struct ui_out *uiout = current_uiout; | |
e2207b9a | 1247 | |
2e783024 | 1248 | ui_out_emit_tuple tuple_emitter (uiout, NULL); |
e2207b9a | 1249 | |
112e8700 SM |
1250 | uiout->field_string ("loaded", script->loaded ? "Yes" : "No"); |
1251 | uiout->field_string ("script", script->name); | |
1252 | uiout->text ("\n"); | |
e2207b9a JK |
1253 | |
1254 | /* If the name isn't the full path, print it too. */ | |
1255 | if (script->full_path != NULL | |
1256 | && strcmp (script->name, script->full_path) != 0) | |
1257 | { | |
112e8700 SM |
1258 | uiout->text ("\tfull name: "); |
1259 | uiout->field_string ("full_path", script->full_path); | |
1260 | uiout->text ("\n"); | |
e2207b9a | 1261 | } |
e2207b9a JK |
1262 | } |
1263 | ||
1264 | /* Helper for info_auto_load_scripts to sort the scripts by name. */ | |
1265 | ||
1266 | static int | |
1267 | sort_scripts_by_name (const void *ap, const void *bp) | |
1268 | { | |
1269 | const struct loaded_script *a = *(const struct loaded_script **) ap; | |
1270 | const struct loaded_script *b = *(const struct loaded_script **) bp; | |
1271 | ||
1272 | return FILENAME_CMP (a->name, b->name); | |
1273 | } | |
1274 | ||
bf88dd68 JK |
1275 | /* Special internal GDB value of auto_load_info_scripts's PATTERN identify |
1276 | the "info auto-load XXX" command has been executed through the general | |
1277 | "info auto-load" invocation. Extra newline will be printed if needed. */ | |
1278 | char auto_load_info_scripts_pattern_nl[] = ""; | |
e2207b9a | 1279 | |
9f050062 DE |
1280 | /* Subroutine of auto_load_info_scripts to simplify it. |
1281 | Print SCRIPTS. */ | |
1282 | ||
1283 | static void | |
1284 | print_scripts (VEC (loaded_script_ptr) *scripts) | |
1285 | { | |
9f050062 DE |
1286 | int i; |
1287 | loaded_script_ptr script; | |
1288 | ||
1289 | qsort (VEC_address (loaded_script_ptr, scripts), | |
1290 | VEC_length (loaded_script_ptr, scripts), | |
1291 | sizeof (loaded_script_ptr), sort_scripts_by_name); | |
1292 | for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i) | |
1293 | print_script (script); | |
1294 | } | |
1295 | ||
bf88dd68 JK |
1296 | /* Implementation for "info auto-load gdb-scripts" |
1297 | (and "info auto-load python-scripts"). List scripts in LANGUAGE matching | |
1298 | PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */ | |
1299 | ||
1300 | void | |
1301 | auto_load_info_scripts (char *pattern, int from_tty, | |
6dddc817 | 1302 | const struct extension_language_defn *language) |
e2207b9a JK |
1303 | { |
1304 | struct ui_out *uiout = current_uiout; | |
1305 | struct auto_load_pspace_info *pspace_info; | |
1306 | struct cleanup *script_chain; | |
9f050062 | 1307 | VEC (loaded_script_ptr) *script_files, *script_texts; |
e2207b9a JK |
1308 | int nr_scripts; |
1309 | ||
1310 | dont_repeat (); | |
1311 | ||
1312 | pspace_info = get_auto_load_pspace_data (current_program_space); | |
1313 | ||
1314 | if (pattern && *pattern) | |
1315 | { | |
1316 | char *re_err = re_comp (pattern); | |
1317 | ||
1318 | if (re_err) | |
1319 | error (_("Invalid regexp: %s"), re_err); | |
1320 | } | |
1321 | else | |
1322 | { | |
1323 | re_comp (""); | |
1324 | } | |
1325 | ||
1326 | /* We need to know the number of rows before we build the table. | |
1327 | Plus we want to sort the scripts by name. | |
1328 | So first traverse the hash table collecting the matching scripts. */ | |
1329 | ||
9f050062 DE |
1330 | script_files = VEC_alloc (loaded_script_ptr, 10); |
1331 | script_texts = VEC_alloc (loaded_script_ptr, 10); | |
1332 | script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &script_files); | |
1333 | make_cleanup (VEC_cleanup (loaded_script_ptr), &script_texts); | |
1334 | ||
1335 | if (pspace_info != NULL && pspace_info->loaded_script_files != NULL) | |
1336 | { | |
1337 | struct collect_matching_scripts_data data = { &script_files, language }; | |
1338 | ||
1339 | /* Pass a pointer to scripts as VEC_safe_push can realloc space. */ | |
1340 | htab_traverse_noresize (pspace_info->loaded_script_files, | |
1341 | collect_matching_scripts, &data); | |
1342 | } | |
e2207b9a | 1343 | |
9f050062 | 1344 | if (pspace_info != NULL && pspace_info->loaded_script_texts != NULL) |
e2207b9a | 1345 | { |
9f050062 | 1346 | struct collect_matching_scripts_data data = { &script_texts, language }; |
bf88dd68 | 1347 | |
e2207b9a | 1348 | /* Pass a pointer to scripts as VEC_safe_push can realloc space. */ |
9f050062 | 1349 | htab_traverse_noresize (pspace_info->loaded_script_texts, |
bf88dd68 | 1350 | collect_matching_scripts, &data); |
e2207b9a JK |
1351 | } |
1352 | ||
9f050062 DE |
1353 | nr_scripts = (VEC_length (loaded_script_ptr, script_files) |
1354 | + VEC_length (loaded_script_ptr, script_texts)); | |
bf88dd68 JK |
1355 | |
1356 | /* Table header shifted right by preceding "gdb-scripts: " would not match | |
1357 | its columns. */ | |
1358 | if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl) | |
112e8700 | 1359 | uiout->text ("\n"); |
bf88dd68 | 1360 | |
4a2b031d TT |
1361 | { |
1362 | ui_out_emit_table table_emitter (uiout, 2, nr_scripts, | |
1363 | "AutoLoadedScriptsTable"); | |
e2207b9a | 1364 | |
4a2b031d TT |
1365 | uiout->table_header (7, ui_left, "loaded", "Loaded"); |
1366 | uiout->table_header (70, ui_left, "script", "Script"); | |
1367 | uiout->table_body (); | |
e2207b9a | 1368 | |
4a2b031d TT |
1369 | print_scripts (script_files); |
1370 | print_scripts (script_texts); | |
1371 | } | |
e2207b9a JK |
1372 | |
1373 | do_cleanups (script_chain); | |
1374 | ||
1375 | if (nr_scripts == 0) | |
1376 | { | |
1377 | if (pattern && *pattern) | |
112e8700 | 1378 | uiout->message ("No auto-load scripts matching %s.\n", pattern); |
e2207b9a | 1379 | else |
112e8700 | 1380 | uiout->message ("No auto-load scripts.\n"); |
e2207b9a JK |
1381 | } |
1382 | } | |
1383 | ||
bf88dd68 JK |
1384 | /* Wrapper for "info auto-load gdb-scripts". */ |
1385 | ||
1386 | static void | |
1387 | info_auto_load_gdb_scripts (char *pattern, int from_tty) | |
1388 | { | |
6dddc817 | 1389 | auto_load_info_scripts (pattern, from_tty, &extension_language_gdb); |
bf88dd68 JK |
1390 | } |
1391 | ||
1392 | /* Implement 'info auto-load local-gdbinit'. */ | |
1393 | ||
1394 | static void | |
1395 | info_auto_load_local_gdbinit (char *args, int from_tty) | |
1396 | { | |
1397 | if (auto_load_local_gdbinit_pathname == NULL) | |
1398 | printf_filtered (_("Local .gdbinit file was not found.\n")); | |
1399 | else if (auto_load_local_gdbinit_loaded) | |
1400 | printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"), | |
1401 | auto_load_local_gdbinit_pathname); | |
1402 | else | |
1403 | printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"), | |
1404 | auto_load_local_gdbinit_pathname); | |
1405 | } | |
1406 | ||
9f050062 DE |
1407 | /* Print an "unsupported script" warning if it has not already been printed. |
1408 | The script is in language LANGUAGE at offset OFFSET in section SECTION_NAME | |
1409 | of OBJFILE. */ | |
6dddc817 | 1410 | |
9f050062 DE |
1411 | static void |
1412 | maybe_print_unsupported_script_warning | |
1413 | (struct auto_load_pspace_info *pspace_info, | |
1414 | struct objfile *objfile, const struct extension_language_defn *language, | |
1415 | const char *section_name, unsigned offset) | |
6dddc817 | 1416 | { |
9f050062 DE |
1417 | if (!pspace_info->unsupported_script_warning_printed) |
1418 | { | |
1419 | warning (_("\ | |
1420 | Unsupported auto-load script at offset %u in section %s\n\ | |
1421 | of file %s.\n\ | |
1422 | Use `info auto-load %s-scripts [REGEXP]' to list them."), | |
1423 | offset, section_name, objfile_name (objfile), | |
1424 | ext_lang_name (language)); | |
1425 | pspace_info->unsupported_script_warning_printed = 1; | |
1426 | } | |
6dddc817 DE |
1427 | } |
1428 | ||
e2207b9a JK |
1429 | /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset |
1430 | before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED | |
1431 | of PSPACE_INFO. */ | |
1432 | ||
9f050062 DE |
1433 | static void |
1434 | maybe_print_script_not_found_warning | |
1435 | (struct auto_load_pspace_info *pspace_info, | |
1436 | struct objfile *objfile, const struct extension_language_defn *language, | |
1437 | const char *section_name, unsigned offset) | |
e2207b9a | 1438 | { |
9f050062 DE |
1439 | if (!pspace_info->script_not_found_warning_printed) |
1440 | { | |
1441 | warning (_("\ | |
1442 | Missing auto-load script at offset %u in section %s\n\ | |
1443 | of file %s.\n\ | |
1444 | Use `info auto-load %s-scripts [REGEXP]' to list them."), | |
1445 | offset, section_name, objfile_name (objfile), | |
1446 | ext_lang_name (language)); | |
1447 | pspace_info->script_not_found_warning_printed = 1; | |
1448 | } | |
e2207b9a JK |
1449 | } |
1450 | ||
bf88dd68 JK |
1451 | /* The only valid "set auto-load" argument is off|0|no|disable. */ |
1452 | ||
1453 | static void | |
1454 | set_auto_load_cmd (char *args, int from_tty) | |
1455 | { | |
1456 | struct cmd_list_element *list; | |
1457 | size_t length; | |
1458 | ||
1459 | /* See parse_binary_operation in use by the sub-commands. */ | |
1460 | ||
1461 | length = args ? strlen (args) : 0; | |
1462 | ||
1463 | while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t')) | |
1464 | length--; | |
1465 | ||
1466 | if (length == 0 || (strncmp (args, "off", length) != 0 | |
1467 | && strncmp (args, "0", length) != 0 | |
1468 | && strncmp (args, "no", length) != 0 | |
1469 | && strncmp (args, "disable", length) != 0)) | |
1470 | error (_("Valid is only global 'set auto-load no'; " | |
1471 | "otherwise check the auto-load sub-commands.")); | |
1472 | ||
1473 | for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next) | |
1474 | if (list->var_type == var_boolean) | |
1475 | { | |
1476 | gdb_assert (list->type == set_cmd); | |
5b9afe8a | 1477 | do_set_command (args, from_tty, list); |
bf88dd68 JK |
1478 | } |
1479 | } | |
1480 | ||
1481 | /* Initialize "set auto-load " commands prefix and return it. */ | |
1482 | ||
1483 | struct cmd_list_element ** | |
1484 | auto_load_set_cmdlist_get (void) | |
1485 | { | |
1486 | static struct cmd_list_element *retval; | |
1487 | ||
1488 | if (retval == NULL) | |
1489 | add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\ | |
1490 | Auto-loading specific settings.\n\ | |
1491 | Configure various auto-load-specific variables such as\n\ | |
1492 | automatic loading of Python scripts."), | |
1493 | &retval, "set auto-load ", | |
1494 | 1/*allow-unknown*/, &setlist); | |
1495 | ||
1496 | return &retval; | |
1497 | } | |
1498 | ||
1499 | /* Command "show auto-load" displays summary of all the current | |
1500 | "show auto-load " settings. */ | |
1501 | ||
1502 | static void | |
1503 | show_auto_load_cmd (char *args, int from_tty) | |
1504 | { | |
1505 | cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, ""); | |
1506 | } | |
1507 | ||
1508 | /* Initialize "show auto-load " commands prefix and return it. */ | |
1509 | ||
1510 | struct cmd_list_element ** | |
1511 | auto_load_show_cmdlist_get (void) | |
1512 | { | |
1513 | static struct cmd_list_element *retval; | |
1514 | ||
1515 | if (retval == NULL) | |
1516 | add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\ | |
1517 | Show auto-loading specific settings.\n\ | |
1518 | Show configuration of various auto-load-specific variables such as\n\ | |
1519 | automatic loading of Python scripts."), | |
1520 | &retval, "show auto-load ", | |
1521 | 0/*allow-unknown*/, &showlist); | |
1522 | ||
1523 | return &retval; | |
1524 | } | |
1525 | ||
1526 | /* Command "info auto-load" displays whether the various auto-load files have | |
1527 | been loaded. This is reimplementation of cmd_show_list which inserts | |
1528 | newlines at proper places. */ | |
1529 | ||
1530 | static void | |
1531 | info_auto_load_cmd (char *args, int from_tty) | |
1532 | { | |
1533 | struct cmd_list_element *list; | |
1534 | struct cleanup *infolist_chain; | |
1535 | struct ui_out *uiout = current_uiout; | |
1536 | ||
2e783024 | 1537 | ui_out_emit_tuple tuple_emitter (uiout, "infolist"); |
bf88dd68 JK |
1538 | |
1539 | for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next) | |
1540 | { | |
2e783024 | 1541 | ui_out_emit_tuple option_emitter (uiout, "option"); |
bf88dd68 JK |
1542 | |
1543 | gdb_assert (!list->prefixlist); | |
1544 | gdb_assert (list->type == not_set_cmd); | |
1545 | ||
112e8700 SM |
1546 | uiout->field_string ("name", list->name); |
1547 | uiout->text (": "); | |
bf88dd68 | 1548 | cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty); |
bf88dd68 | 1549 | } |
bf88dd68 JK |
1550 | } |
1551 | ||
1552 | /* Initialize "info auto-load " commands prefix and return it. */ | |
1553 | ||
1554 | struct cmd_list_element ** | |
1555 | auto_load_info_cmdlist_get (void) | |
1556 | { | |
1557 | static struct cmd_list_element *retval; | |
1558 | ||
1559 | if (retval == NULL) | |
1560 | add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\ | |
1561 | Print current status of auto-loaded files.\n\ | |
1562 | Print whether various files like Python scripts or .gdbinit files have been\n\ | |
1563 | found and/or loaded."), | |
1564 | &retval, "info auto-load ", | |
1565 | 0/*allow-unknown*/, &infolist); | |
1566 | ||
1567 | return &retval; | |
1568 | } | |
1569 | ||
e2207b9a JK |
1570 | void |
1571 | _initialize_auto_load (void) | |
1572 | { | |
bccbefd2 | 1573 | struct cmd_list_element *cmd; |
6dddc817 | 1574 | char *scripts_directory_help, *gdb_name_help, *python_name_help; |
ed3ef339 DE |
1575 | char *guile_name_help; |
1576 | const char *suffix; | |
bccbefd2 | 1577 | |
e2207b9a | 1578 | auto_load_pspace_data |
8e260fc0 TT |
1579 | = register_program_space_data_with_cleanup (NULL, |
1580 | auto_load_pspace_data_cleanup); | |
e2207b9a JK |
1581 | |
1582 | observer_attach_new_objfile (auto_load_new_objfile); | |
1583 | ||
bf88dd68 JK |
1584 | add_setshow_boolean_cmd ("gdb-scripts", class_support, |
1585 | &auto_load_gdb_scripts, _("\ | |
1586 | Enable or disable auto-loading of canned sequences of commands scripts."), _("\ | |
1587 | Show whether auto-loading of canned sequences of commands scripts is enabled."), | |
1588 | _("\ | |
1589 | If enabled, canned sequences of commands are loaded when the debugger reads\n\ | |
1590 | an executable or shared library.\n\ | |
1591 | This options has security implications for untrusted inferiors."), | |
1592 | NULL, show_auto_load_gdb_scripts, | |
1593 | auto_load_set_cmdlist_get (), | |
1594 | auto_load_show_cmdlist_get ()); | |
1595 | ||
1596 | add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts, | |
1597 | _("Print the list of automatically loaded sequences of commands.\n\ | |
1598 | Usage: info auto-load gdb-scripts [REGEXP]"), | |
1599 | auto_load_info_cmdlist_get ()); | |
1600 | ||
1601 | add_setshow_boolean_cmd ("local-gdbinit", class_support, | |
1602 | &auto_load_local_gdbinit, _("\ | |
1603 | Enable or disable auto-loading of .gdbinit script in current directory."), _("\ | |
1604 | Show whether auto-loading .gdbinit script in current directory is enabled."), | |
1605 | _("\ | |
1606 | If enabled, canned sequences of commands are loaded when debugger starts\n\ | |
1607 | from .gdbinit file in current directory. Such files are deprecated,\n\ | |
1608 | use a script associated with inferior executable file instead.\n\ | |
1609 | This options has security implications for untrusted inferiors."), | |
1610 | NULL, show_auto_load_local_gdbinit, | |
1611 | auto_load_set_cmdlist_get (), | |
1612 | auto_load_show_cmdlist_get ()); | |
1613 | ||
1614 | add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit, | |
1615 | _("Print whether current directory .gdbinit file has been loaded.\n\ | |
1616 | Usage: info auto-load local-gdbinit"), | |
1617 | auto_load_info_cmdlist_get ()); | |
bccbefd2 | 1618 | |
7349ff92 | 1619 | auto_load_dir = xstrdup (AUTO_LOAD_DIR); |
6dddc817 | 1620 | |
ed3ef339 | 1621 | suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB)); |
6dddc817 DE |
1622 | gdb_name_help |
1623 | = xstrprintf (_("\ | |
1624 | GDB scripts: OBJFILE%s\n"), | |
ed3ef339 | 1625 | suffix); |
6dddc817 | 1626 | python_name_help = NULL; |
9a950c7c | 1627 | #ifdef HAVE_PYTHON |
ed3ef339 | 1628 | suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON)); |
6dddc817 DE |
1629 | python_name_help |
1630 | = xstrprintf (_("\ | |
1631 | Python scripts: OBJFILE%s\n"), | |
ed3ef339 DE |
1632 | suffix); |
1633 | #endif | |
1634 | guile_name_help = NULL; | |
1635 | #ifdef HAVE_GUILE | |
1636 | suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE)); | |
1637 | guile_name_help | |
1638 | = xstrprintf (_("\ | |
1639 | Guile scripts: OBJFILE%s\n"), | |
1640 | suffix); | |
9a950c7c | 1641 | #endif |
6dddc817 DE |
1642 | scripts_directory_help |
1643 | = xstrprintf (_("\ | |
1644 | Automatically loaded scripts are located in one of the directories listed\n\ | |
1645 | by this option.\n\ | |
1646 | \n\ | |
1647 | Script names:\n\ | |
ed3ef339 | 1648 | %s%s%s\ |
6dddc817 | 1649 | \n\ |
9a950c7c JK |
1650 | This option is ignored for the kinds of scripts \ |
1651 | having 'set auto-load ... off'.\n\ | |
1652 | Directories listed here need to be present also \ | |
1653 | in the 'set auto-load safe-path'\n\ | |
6dddc817 DE |
1654 | option."), |
1655 | gdb_name_help, | |
ed3ef339 DE |
1656 | python_name_help ? python_name_help : "", |
1657 | guile_name_help ? guile_name_help : ""); | |
6dddc817 | 1658 | |
7349ff92 JK |
1659 | add_setshow_optional_filename_cmd ("scripts-directory", class_support, |
1660 | &auto_load_dir, _("\ | |
1661 | Set the list of directories from which to load auto-loaded scripts."), _("\ | |
9a950c7c JK |
1662 | Show the list of directories from which to load auto-loaded scripts."), |
1663 | scripts_directory_help, | |
7349ff92 JK |
1664 | set_auto_load_dir, show_auto_load_dir, |
1665 | auto_load_set_cmdlist_get (), | |
1666 | auto_load_show_cmdlist_get ()); | |
9a950c7c | 1667 | xfree (scripts_directory_help); |
6dddc817 DE |
1668 | xfree (python_name_help); |
1669 | xfree (gdb_name_help); | |
ed3ef339 | 1670 | xfree (guile_name_help); |
7349ff92 | 1671 | |
6dea1fbd | 1672 | auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH); |
bccbefd2 JK |
1673 | auto_load_safe_path_vec_update (); |
1674 | add_setshow_optional_filename_cmd ("safe-path", class_support, | |
1675 | &auto_load_safe_path, _("\ | |
9a950c7c JK |
1676 | Set the list of files and directories that are safe for auto-loading."), _("\ |
1677 | Show the list of files and directories that are safe for auto-loading."), _("\ | |
bccbefd2 JK |
1678 | Various files loaded automatically for the 'set auto-load ...' options must\n\ |
1679 | be located in one of the directories listed by this option. Warning will be\n\ | |
af2c1515 | 1680 | printed and file will not be used otherwise.\n\ |
9a950c7c | 1681 | You can mix both directory and filename entries.\n\ |
af2c1515 JK |
1682 | Setting this parameter to an empty list resets it to its default value.\n\ |
1683 | Setting this parameter to '/' (without the quotes) allows any file\n\ | |
9a950c7c | 1684 | for the 'set auto-load ...' options. Each path entry can be also shell\n\ |
202cbf1c | 1685 | wildcard pattern; '*' does not match directory separator.\n\ |
bccbefd2 JK |
1686 | This option is ignored for the kinds of files having 'set auto-load ... off'.\n\ |
1687 | This options has security implications for untrusted inferiors."), | |
1688 | set_auto_load_safe_path, | |
1689 | show_auto_load_safe_path, | |
1690 | auto_load_set_cmdlist_get (), | |
1691 | auto_load_show_cmdlist_get ()); | |
6dea1fbd | 1692 | observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed); |
bccbefd2 JK |
1693 | |
1694 | cmd = add_cmd ("add-auto-load-safe-path", class_support, | |
1695 | add_auto_load_safe_path, | |
1696 | _("Add entries to the list of directories from which it is safe " | |
1697 | "to auto-load files.\n\ | |
1698 | See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\ | |
1699 | access the current full list setting."), | |
1700 | &cmdlist); | |
1701 | set_cmd_completer (cmd, filename_completer); | |
4dc84fd1 | 1702 | |
f10c5b19 JK |
1703 | cmd = add_cmd ("add-auto-load-scripts-directory", class_support, |
1704 | add_auto_load_dir, | |
1705 | _("Add entries to the list of directories from which to load " | |
1706 | "auto-loaded scripts.\n\ | |
1707 | See the commands 'set auto-load scripts-directory' and\n\ | |
1708 | 'show auto-load scripts-directory' to access the current full list setting."), | |
1709 | &cmdlist); | |
1710 | set_cmd_completer (cmd, filename_completer); | |
1711 | ||
4dc84fd1 JK |
1712 | add_setshow_boolean_cmd ("auto-load", class_maintenance, |
1713 | &debug_auto_load, _("\ | |
1714 | Set auto-load verifications debugging."), _("\ | |
1715 | Show auto-load verifications debugging."), _("\ | |
1716 | When non-zero, debugging output for files of 'set auto-load ...'\n\ | |
1717 | is displayed."), | |
1718 | NULL, show_debug_auto_load, | |
1719 | &setdebuglist, &showdebuglist); | |
e2207b9a | 1720 | } |