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