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