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