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