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