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