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