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