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