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