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