gdb/
[deliverable/binutils-gdb.git] / gdb / auto-load.c
CommitLineData
e2207b9a
JK
1/* GDB routines for supporting auto-loaded scripts.
2
3 Copyright (C) 2012 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "auto-load.h"
22#include "progspace.h"
23#include "python/python.h"
24#include "gdb_regex.h"
25#include "ui-out.h"
26#include "filenames.h"
27#include "command.h"
28#include "observer.h"
29#include "objfiles.h"
bf88dd68
JK
30#include "exceptions.h"
31#include "cli/cli-script.h"
32#include "gdbcmd.h"
33#include "cli/cli-decode.h"
34#include "cli/cli-setshow.h"
bccbefd2
JK
35#include "gdb_vecs.h"
36#include "readline/tilde.h"
37#include "completer.h"
6dea1fbd 38#include "observer.h"
bf88dd68
JK
39
40/* The suffix of per-objfile scripts to auto-load as non-Python command files.
41 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
42#define GDB_AUTO_FILE_NAME "-gdb.gdb"
43
44static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
45 const char *filename);
46
4dc84fd1
JK
47/* Value of the 'set debug auto-load' configuration variable. */
48static int debug_auto_load = 0;
49
50/* "show" command for the debug_auto_load configuration variable. */
51
52static void
53show_debug_auto_load (struct ui_file *file, int from_tty,
54 struct cmd_list_element *c, const char *value)
55{
56 fprintf_filtered (file, _("Debugging output for files "
57 "of 'set auto-load ...' is %s.\n"),
58 value);
59}
60
bf88dd68
JK
61/* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
62 scripts:
63 set auto-load gdb-scripts on|off
64 This is true if we should auto-load associated scripts when an objfile
65 is opened, false otherwise. */
66static int auto_load_gdb_scripts = 1;
67
68/* "show" command for the auto_load_gdb_scripts configuration variable. */
69
70static void
71show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
72 struct cmd_list_element *c, const char *value)
73{
74 fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
75 "scripts is %s.\n"),
76 value);
77}
e2207b9a
JK
78
79/* Internal-use flag to enable/disable auto-loading.
80 This is true if we should auto-load python code when an objfile is opened,
81 false otherwise.
82
bf88dd68 83 Both auto_load_scripts && global_auto_load must be true to enable
e2207b9a
JK
84 auto-loading.
85
86 This flag exists to facilitate deferring auto-loading during start-up
87 until after ./.gdbinit has been read; it may augment the search directories
88 used to find the scripts. */
bf88dd68
JK
89int global_auto_load = 1;
90
91/* Auto-load .gdbinit file from the current directory? */
92int auto_load_local_gdbinit = 1;
93
94/* Absolute pathname to the current directory .gdbinit, if it exists. */
95char *auto_load_local_gdbinit_pathname = NULL;
96
97/* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
98int auto_load_local_gdbinit_loaded = 0;
99
100/* "show" command for the auto_load_local_gdbinit configuration variable. */
101
102static void
103show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
104 struct cmd_list_element *c, const char *value)
105{
106 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
107 "directory is %s.\n"),
108 value);
109}
110
7349ff92
JK
111/* Directory list from which to load auto-loaded scripts. It is not checked
112 for absolute paths but they are strongly recommended. It is initialized by
113 _initialize_auto_load. */
114static char *auto_load_dir;
115
116/* "set" command for the auto_load_dir configuration variable. */
117
118static void
119set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c)
120{
121 /* Setting the variable to "" resets it to the compile time defaults. */
122 if (auto_load_dir[0] == '\0')
123 {
124 xfree (auto_load_dir);
125 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
126 }
127}
128
129/* "show" command for the auto_load_dir configuration variable. */
130
131static void
132show_auto_load_dir (struct ui_file *file, int from_tty,
133 struct cmd_list_element *c, const char *value)
134{
135 fprintf_filtered (file, _("List of directories from which to load "
136 "auto-loaded scripts is %s.\n"),
137 value);
138}
139
bccbefd2
JK
140/* Directory list safe to hold auto-loaded files. It is not checked for
141 absolute paths but they are strongly recommended. It is initialized by
142 _initialize_auto_load. */
143static char *auto_load_safe_path;
144
145/* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
146 by tilde_expand and possibly each entries has added its gdb_realpath
147 counterpart. */
148static VEC (char_ptr) *auto_load_safe_path_vec;
149
150/* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
151
152static void
153auto_load_safe_path_vec_update (void)
154{
155 VEC (char_ptr) *dir_vec = NULL;
156 unsigned len;
157 int ix;
158
4dc84fd1
JK
159 if (debug_auto_load)
160 fprintf_unfiltered (gdb_stdlog,
161 _("auto-load: Updating directories of \"%s\".\n"),
162 auto_load_safe_path);
163
bccbefd2
JK
164 free_char_ptr_vec (auto_load_safe_path_vec);
165
166 auto_load_safe_path_vec = dirnames_to_char_ptr_vec (auto_load_safe_path);
167 len = VEC_length (char_ptr, auto_load_safe_path_vec);
168
169 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
170 element. */
171 for (ix = 0; ix < len; ix++)
172 {
173 char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
6dea1fbd
JK
174 char *ddir_subst, *expanded, *real_path;
175
176 ddir_subst = xstrdup (dir);
aff139ff 177 substitute_path_component (&ddir_subst, "$datadir", gdb_datadir);
6dea1fbd
JK
178 expanded = tilde_expand (ddir_subst);
179 xfree (ddir_subst);
180 real_path = gdb_realpath (expanded);
bccbefd2 181
6dea1fbd 182 /* Ensure the current entry is at least a valid path (therefore
aff139ff 183 $datadir-expanded and tilde-expanded). */
bccbefd2
JK
184 VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
185
4dc84fd1
JK
186 if (debug_auto_load)
187 {
188 if (strcmp (expanded, dir) == 0)
189 fprintf_unfiltered (gdb_stdlog,
190 _("auto-load: Using directory \"%s\".\n"),
191 expanded);
192 else
193 fprintf_unfiltered (gdb_stdlog,
194 _("auto-load: Resolved directory \"%s\" "
195 "as \"%s\".\n"),
196 dir, expanded);
197 }
198 xfree (dir);
199
bccbefd2
JK
200 /* If gdb_realpath returns a different content, append it. */
201 if (strcmp (real_path, expanded) == 0)
202 xfree (real_path);
203 else
4dc84fd1
JK
204 {
205 VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
206
207 if (debug_auto_load)
208 fprintf_unfiltered (gdb_stdlog,
209 _("auto-load: And canonicalized as \"%s\".\n"),
210 real_path);
211 }
bccbefd2
JK
212 }
213}
214
aff139ff 215/* Variable gdb_datadir has been set. Update content depending on $datadir. */
6dea1fbd
JK
216
217static void
218auto_load_gdb_datadir_changed (void)
219{
220 auto_load_safe_path_vec_update ();
221}
222
bccbefd2
JK
223/* "set" command for the auto_load_safe_path configuration variable. */
224
225static void
226set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
227{
6dea1fbd 228 /* Setting the variable to "" resets it to the compile time defaults. */
af2c1515
JK
229 if (auto_load_safe_path[0] == '\0')
230 {
231 xfree (auto_load_safe_path);
6dea1fbd 232 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
af2c1515
JK
233 }
234
bccbefd2
JK
235 auto_load_safe_path_vec_update ();
236}
237
238/* "show" command for the auto_load_safe_path configuration variable. */
239
240static void
241show_auto_load_safe_path (struct ui_file *file, int from_tty,
242 struct cmd_list_element *c, const char *value)
243{
f7bfa992
JK
244 const char *cs;
245
246 /* Check if user has entered either "/" or for example ":".
247 But while more complicate content like ":/foo" would still also
248 permit any location do not hide those. */
249
250 for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
251 cs++);
252 if (*cs == 0)
bccbefd2
JK
253 fprintf_filtered (file, _("Auto-load files are safe to load from any "
254 "directory.\n"));
255 else
256 fprintf_filtered (file, _("List of directories from which it is safe to "
257 "auto-load files is %s.\n"),
258 value);
259}
260
261/* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
262 variable. */
263
264static void
265add_auto_load_safe_path (char *args, int from_tty)
266{
267 char *s;
268
269 if (args == NULL || *args == 0)
270 error (_("\
af2c1515
JK
271Directory argument required.\n\
272Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
273"));
bccbefd2
JK
274
275 s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
276 xfree (auto_load_safe_path);
277 auto_load_safe_path = s;
278
279 auto_load_safe_path_vec_update ();
280}
281
282/* Return 1 if FILENAME is equal to DIR or if FILENAME belongs to the
283 subdirectory DIR. Return 0 otherwise. gdb_realpath normalization is never
284 done here. */
285
286static ATTRIBUTE_PURE int
287filename_is_in_dir (const char *filename, const char *dir)
288{
289 size_t dir_len = strlen (dir);
290
291 while (dir_len && IS_DIR_SEPARATOR (dir[dir_len - 1]))
292 dir_len--;
293
1ef71717
JK
294 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
295 platform FILENAME even after gdb_realpath does not have to start with
296 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
297 if (dir_len == 0)
298 return 1;
299
bccbefd2
JK
300 return (filename_ncmp (dir, filename, dir_len) == 0
301 && (IS_DIR_SEPARATOR (filename[dir_len])
302 || filename[dir_len] == '\0'));
303}
304
305/* Return 1 if FILENAME belongs to one of directory components of
306 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
307 auto_load_safe_path_vec_update is never called.
308 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
309 freed by the caller. */
310
311static int
312filename_is_in_auto_load_safe_path_vec (const char *filename,
313 char **filename_realp)
314{
315 char *dir;
316 int ix;
317
318 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir); ++ix)
319 if (*filename_realp == NULL && filename_is_in_dir (filename, dir))
320 break;
321
322 if (dir == NULL)
323 {
324 if (*filename_realp == NULL)
4dc84fd1
JK
325 {
326 *filename_realp = gdb_realpath (filename);
327 if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
328 fprintf_unfiltered (gdb_stdlog,
329 _("auto-load: Resolved "
330 "file \"%s\" as \"%s\".\n"),
331 filename, *filename_realp);
332 }
bccbefd2 333
4dc84fd1
JK
334 if (strcmp (*filename_realp, filename) != 0)
335 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, dir);
336 ++ix)
337 if (filename_is_in_dir (*filename_realp, dir))
338 break;
bccbefd2
JK
339 }
340
341 if (dir != NULL)
4dc84fd1
JK
342 {
343 if (debug_auto_load)
344 fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
345 "directory \"%s\".\n"),
346 filename, dir);
347 return 1;
348 }
bccbefd2
JK
349
350 return 0;
351}
352
353/* Return 1 if FILENAME is located in one of the directories of
354 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
355 not have to be an absolute path.
356
357 Existence of FILENAME is not checked. Function will still give a warning
358 even if the caller would quietly skip non-existing file in unsafe
359 directory. */
360
361int
4dc84fd1 362file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
bccbefd2
JK
363{
364 char *filename_real = NULL;
365 struct cleanup *back_to;
366
4dc84fd1
JK
367 if (debug_auto_load)
368 {
369 va_list debug_args;
370
371 va_start (debug_args, debug_fmt);
372 vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
373 va_end (debug_args);
374 }
375
bccbefd2
JK
376 back_to = make_cleanup (free_current_contents, &filename_real);
377
378 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
379 {
380 do_cleanups (back_to);
381 return 1;
382 }
383
384 auto_load_safe_path_vec_update ();
385 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
386 {
387 do_cleanups (back_to);
388 return 1;
389 }
390
391 warning (_("File \"%s\" auto-loading has been declined by your "
392 "`auto-load safe-path' set to \"%s\"."),
393 filename_real, auto_load_safe_path);
394
395 do_cleanups (back_to);
396 return 0;
397}
398
bf88dd68
JK
399/* Definition of script language for GDB canned sequences of commands. */
400
401static const struct script_language script_language_gdb
402 = { GDB_AUTO_FILE_NAME, source_gdb_script_for_objfile };
403
404static void
405source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
406 const char *filename)
407{
bccbefd2 408 int is_safe;
bf88dd68
JK
409 struct auto_load_pspace_info *pspace_info;
410 volatile struct gdb_exception e;
411
4dc84fd1
JK
412 is_safe = file_is_auto_load_safe (filename, _("auto-load: Loading canned "
413 "sequences of commands script "
414 "\"%s\" for objfile \"%s\".\n"),
415 filename, objfile->name);
bccbefd2 416
bf88dd68
JK
417 /* Add this script to the hash table too so "info auto-load gdb-scripts"
418 can print it. */
419 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
bccbefd2
JK
420 maybe_add_script (pspace_info, is_safe, filename, filename,
421 &script_language_gdb);
422
423 if (!is_safe)
424 return;
bf88dd68
JK
425
426 TRY_CATCH (e, RETURN_MASK_ALL)
427 {
428 script_from_file (file, filename);
429 }
430 exception_print (gdb_stderr, e);
431}
e2207b9a
JK
432
433/* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
434 the same script. There's no point in loading the script multiple times,
435 and there can be a lot of objfiles and scripts, so we keep track of scripts
436 loaded this way. */
437
438struct auto_load_pspace_info
439{
440 /* For each program space we keep track of loaded scripts. */
441 struct htab *loaded_scripts;
442
443 /* Non-zero if we've issued the warning about an auto-load script not being
444 found. We only want to issue this warning once. */
445 int script_not_found_warning_printed;
446};
447
448/* Objects of this type are stored in the loaded script hash table. */
449
450struct loaded_script
451{
452 /* Name as provided by the objfile. */
453 const char *name;
bf88dd68 454
e2207b9a
JK
455 /* Full path name or NULL if script wasn't found (or was otherwise
456 inaccessible). */
457 const char *full_path;
bf88dd68 458
bccbefd2
JK
459 /* Non-zero if this script has been loaded. */
460 int loaded;
461
bf88dd68 462 const struct script_language *language;
e2207b9a
JK
463};
464
465/* Per-program-space data key. */
466static const struct program_space_data *auto_load_pspace_data;
467
468static void
469auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
470{
471 struct auto_load_pspace_info *info;
472
473 info = program_space_data (pspace, auto_load_pspace_data);
474 if (info != NULL)
475 {
476 if (info->loaded_scripts)
477 htab_delete (info->loaded_scripts);
478 xfree (info);
479 }
480}
481
482/* Get the current autoload data. If none is found yet, add it now. This
483 function always returns a valid object. */
484
485static struct auto_load_pspace_info *
486get_auto_load_pspace_data (struct program_space *pspace)
487{
488 struct auto_load_pspace_info *info;
489
490 info = program_space_data (pspace, auto_load_pspace_data);
491 if (info == NULL)
492 {
493 info = XZALLOC (struct auto_load_pspace_info);
494 set_program_space_data (pspace, auto_load_pspace_data, info);
495 }
496
497 return info;
498}
499
500/* Hash function for the loaded script hash. */
501
502static hashval_t
503hash_loaded_script_entry (const void *data)
504{
505 const struct loaded_script *e = data;
506
bf88dd68 507 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
e2207b9a
JK
508}
509
510/* Equality function for the loaded script hash. */
511
512static int
513eq_loaded_script_entry (const void *a, const void *b)
514{
515 const struct loaded_script *ea = a;
516 const struct loaded_script *eb = b;
517
bf88dd68 518 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
e2207b9a
JK
519}
520
521/* Initialize the table to track loaded scripts.
522 Each entry is hashed by the full path name. */
523
524static void
525init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
526{
527 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
528 Space for each entry is obtained with one malloc so we can free them
529 easily. */
530
531 pspace_info->loaded_scripts = htab_create (31,
532 hash_loaded_script_entry,
533 eq_loaded_script_entry,
534 xfree);
535
536 pspace_info->script_not_found_warning_printed = FALSE;
537}
538
539/* Wrapper on get_auto_load_pspace_data to also allocate the hash table
540 for loading scripts. */
541
542struct auto_load_pspace_info *
543get_auto_load_pspace_data_for_loading (struct program_space *pspace)
544{
545 struct auto_load_pspace_info *info;
546
547 info = get_auto_load_pspace_data (pspace);
548 if (info->loaded_scripts == NULL)
549 init_loaded_scripts_info (info);
550
551 return info;
552}
553
bccbefd2
JK
554/* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the
555 script has been (is going to) be loaded, 0 otherwise (such as if it has not
556 been found). FULL_PATH is NULL if the script wasn't found. The result is
bf88dd68 557 true if the script was already in the hash table. */
e2207b9a
JK
558
559int
bccbefd2 560maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
bf88dd68
JK
561 const char *name, const char *full_path,
562 const struct script_language *language)
e2207b9a
JK
563{
564 struct htab *htab = pspace_info->loaded_scripts;
565 struct loaded_script **slot, entry;
566 int in_hash_table;
567
568 entry.name = name;
bf88dd68 569 entry.language = language;
e2207b9a
JK
570 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
571 in_hash_table = *slot != NULL;
572
573 /* If this script is not in the hash table, add it. */
574
575 if (! in_hash_table)
576 {
577 char *p;
578
579 /* Allocate all space in one chunk so it's easier to free. */
580 *slot = xmalloc (sizeof (**slot)
581 + strlen (name) + 1
582 + (full_path != NULL ? (strlen (full_path) + 1) : 0));
583 p = ((char*) *slot) + sizeof (**slot);
584 strcpy (p, name);
585 (*slot)->name = p;
586 if (full_path != NULL)
587 {
588 p += strlen (p) + 1;
589 strcpy (p, full_path);
590 (*slot)->full_path = p;
591 }
592 else
593 (*slot)->full_path = NULL;
bccbefd2 594 (*slot)->loaded = loaded;
bf88dd68 595 (*slot)->language = language;
e2207b9a
JK
596 }
597
598 return in_hash_table;
599}
600
601/* Clear the table of loaded section scripts. */
602
603static void
604clear_section_scripts (void)
605{
606 struct program_space *pspace = current_program_space;
607 struct auto_load_pspace_info *info;
608
609 info = program_space_data (pspace, auto_load_pspace_data);
610 if (info != NULL && info->loaded_scripts != NULL)
611 {
612 htab_delete (info->loaded_scripts);
613 info->loaded_scripts = NULL;
614 info->script_not_found_warning_printed = FALSE;
615 }
616}
617
bf88dd68
JK
618/* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
619 it. */
e2207b9a
JK
620
621void
bf88dd68
JK
622auto_load_objfile_script (struct objfile *objfile,
623 const struct script_language *language)
e2207b9a
JK
624{
625 char *realname;
626 char *filename, *debugfile;
627 int len;
628 FILE *input;
629 struct cleanup *cleanups;
630
631 realname = gdb_realpath (objfile->name);
632 len = strlen (realname);
bf88dd68 633 filename = xmalloc (len + strlen (language->suffix) + 1);
e2207b9a 634 memcpy (filename, realname, len);
bf88dd68 635 strcpy (filename + len, language->suffix);
e2207b9a
JK
636
637 cleanups = make_cleanup (xfree, filename);
638 make_cleanup (xfree, realname);
639
640 input = fopen (filename, "r");
641 debugfile = filename;
7349ff92
JK
642 if (debug_auto_load)
643 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
644 debugfile, input ? _("exists") : _("does not exist"));
e2207b9a 645
b09aca3a 646 if (!input)
e2207b9a 647 {
b09aca3a
JK
648 char *debugdir;
649 VEC (char_ptr) *debugdir_vec;
650 int ix;
651
652 debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
653 make_cleanup_free_char_ptr_vec (debugdir_vec);
654
7349ff92
JK
655 if (debug_auto_load)
656 fprintf_unfiltered (gdb_stdlog,
657 _("auto-load: Searching 'set debug-file-directory' "
658 "path \"%s\".\n"),
659 debug_file_directory);
660
b09aca3a
JK
661 for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
662 {
663 /* Also try the same file in the separate debug info directory. */
664 debugfile = xmalloc (strlen (debugdir) + strlen (filename) + 1);
665 strcpy (debugfile, debugdir);
666
667 /* FILENAME is absolute, so we don't need a "/" here. */
668 strcat (debugfile, filename);
669
670 make_cleanup (xfree, debugfile);
671 input = fopen (debugfile, "r");
7349ff92
JK
672 if (debug_auto_load)
673 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
674 "\"%s\" %s.\n"),
675 debugfile,
676 input ? _("exists") : _("does not exist"));
b09aca3a
JK
677 if (input != NULL)
678 break;
679 }
e2207b9a
JK
680 }
681
7349ff92 682 if (!input)
e2207b9a 683 {
7349ff92
JK
684 VEC (char_ptr) *vec;
685 int ix;
686 char *dir;
687
e2207b9a
JK
688 /* Also try the same file in a subdirectory of gdb's data
689 directory. */
7349ff92
JK
690
691 vec = dirnames_to_char_ptr_vec (auto_load_dir);
692 make_cleanup_free_char_ptr_vec (vec);
693
694 if (debug_auto_load)
695 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
696 "scripts-directory' path \"%s\".\n"),
697 auto_load_dir);
698
699 for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
700 {
701 debugfile = xstrdup (dir);
aff139ff 702 substitute_path_component (&debugfile, "$datadir", gdb_datadir);
7349ff92
JK
703 debugfile = xrealloc (debugfile, (strlen (debugfile)
704 + strlen (filename) + 1));
705
706 /* FILENAME is absolute, so we don't need a "/" here. */
707 strcat (debugfile, filename);
708
709 make_cleanup (xfree, debugfile);
710 input = fopen (debugfile, "r");
711 if (debug_auto_load)
712 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
713 "\"%s\" %s.\n"),
714 debugfile,
715 input ? _("exists") : _("does not exist"));
716 if (input != NULL)
717 break;
718 }
e2207b9a
JK
719 }
720
721 if (input)
722 {
e2207b9a
JK
723 make_cleanup_fclose (input);
724
e2207b9a
JK
725 /* To preserve existing behaviour we don't check for whether the
726 script was already in the table, and always load it.
727 It's highly unlikely that we'd ever load it twice,
728 and these scripts are required to be idempotent under multiple
729 loads anyway. */
bf88dd68 730 language->source_script_for_objfile (objfile, input, debugfile);
e2207b9a
JK
731 }
732
733 do_cleanups (cleanups);
734}
735
bf88dd68
JK
736/* Load any auto-loaded scripts for OBJFILE. */
737
738void
739load_auto_scripts_for_objfile (struct objfile *objfile)
740{
741 if (!global_auto_load)
742 return;
743
744 if (auto_load_gdb_scripts)
745 auto_load_objfile_script (objfile, &script_language_gdb);
746
747 gdbpy_load_auto_scripts_for_objfile (objfile);
748}
749
e2207b9a
JK
750/* This is a new_objfile observer callback to auto-load scripts.
751
752 Two flavors of auto-loaded scripts are supported.
753 1) based on the path to the objfile
754 2) from .debug_gdb_scripts section */
755
756static void
757auto_load_new_objfile (struct objfile *objfile)
758{
759 if (!objfile)
760 {
761 /* OBJFILE is NULL when loading a new "main" symbol-file. */
762 clear_section_scripts ();
763 return;
764 }
765
766 load_auto_scripts_for_objfile (objfile);
767}
768
769/* Collect scripts to be printed in a vec. */
770
771typedef struct loaded_script *loaded_script_ptr;
772DEF_VEC_P (loaded_script_ptr);
773
bf88dd68
JK
774struct collect_matching_scripts_data
775{
776 VEC (loaded_script_ptr) **scripts_p;
777
778 const struct script_language *language;
779};
780
e2207b9a
JK
781/* Traversal function for htab_traverse.
782 Collect the entry if it matches the regexp. */
783
784static int
785collect_matching_scripts (void **slot, void *info)
786{
787 struct loaded_script *script = *slot;
bf88dd68 788 struct collect_matching_scripts_data *data = info;
e2207b9a 789
bf88dd68
JK
790 if (script->language == data->language && re_exec (script->name))
791 VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
e2207b9a
JK
792
793 return 1;
794}
795
796/* Print SCRIPT. */
797
798static void
799print_script (struct loaded_script *script)
800{
801 struct ui_out *uiout = current_uiout;
802 struct cleanup *chain;
803
804 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
805
bccbefd2 806 ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
e2207b9a
JK
807 ui_out_field_string (uiout, "script", script->name);
808 ui_out_text (uiout, "\n");
809
810 /* If the name isn't the full path, print it too. */
811 if (script->full_path != NULL
812 && strcmp (script->name, script->full_path) != 0)
813 {
814 ui_out_text (uiout, "\tfull name: ");
815 ui_out_field_string (uiout, "full_path", script->full_path);
816 ui_out_text (uiout, "\n");
817 }
818
819 do_cleanups (chain);
820}
821
822/* Helper for info_auto_load_scripts to sort the scripts by name. */
823
824static int
825sort_scripts_by_name (const void *ap, const void *bp)
826{
827 const struct loaded_script *a = *(const struct loaded_script **) ap;
828 const struct loaded_script *b = *(const struct loaded_script **) bp;
829
830 return FILENAME_CMP (a->name, b->name);
831}
832
bf88dd68
JK
833/* Special internal GDB value of auto_load_info_scripts's PATTERN identify
834 the "info auto-load XXX" command has been executed through the general
835 "info auto-load" invocation. Extra newline will be printed if needed. */
836char auto_load_info_scripts_pattern_nl[] = "";
e2207b9a 837
bf88dd68
JK
838/* Implementation for "info auto-load gdb-scripts"
839 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
840 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
841
842void
843auto_load_info_scripts (char *pattern, int from_tty,
844 const struct script_language *language)
e2207b9a
JK
845{
846 struct ui_out *uiout = current_uiout;
847 struct auto_load_pspace_info *pspace_info;
848 struct cleanup *script_chain;
849 VEC (loaded_script_ptr) *scripts;
850 int nr_scripts;
851
852 dont_repeat ();
853
854 pspace_info = get_auto_load_pspace_data (current_program_space);
855
856 if (pattern && *pattern)
857 {
858 char *re_err = re_comp (pattern);
859
860 if (re_err)
861 error (_("Invalid regexp: %s"), re_err);
862 }
863 else
864 {
865 re_comp ("");
866 }
867
868 /* We need to know the number of rows before we build the table.
869 Plus we want to sort the scripts by name.
870 So first traverse the hash table collecting the matching scripts. */
871
872 scripts = VEC_alloc (loaded_script_ptr, 10);
873 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
874
875 if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
876 {
bf88dd68
JK
877 struct collect_matching_scripts_data data = { &scripts, language };
878
e2207b9a
JK
879 immediate_quit++;
880 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
881 htab_traverse_noresize (pspace_info->loaded_scripts,
bf88dd68 882 collect_matching_scripts, &data);
e2207b9a
JK
883 immediate_quit--;
884 }
885
886 nr_scripts = VEC_length (loaded_script_ptr, scripts);
bf88dd68
JK
887
888 /* Table header shifted right by preceding "gdb-scripts: " would not match
889 its columns. */
890 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
891 ui_out_text (uiout, "\n");
892
e2207b9a
JK
893 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
894 "AutoLoadedScriptsTable");
895
896 ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
897 ui_out_table_header (uiout, 70, ui_left, "script", "Script");
898 ui_out_table_body (uiout);
899
900 if (nr_scripts > 0)
901 {
902 int i;
903 loaded_script_ptr script;
904
905 qsort (VEC_address (loaded_script_ptr, scripts),
906 VEC_length (loaded_script_ptr, scripts),
907 sizeof (loaded_script_ptr), sort_scripts_by_name);
908 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
909 print_script (script);
910 }
911
912 do_cleanups (script_chain);
913
914 if (nr_scripts == 0)
915 {
916 if (pattern && *pattern)
917 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
918 pattern);
919 else
920 ui_out_message (uiout, 0, "No auto-load scripts.\n");
921 }
922}
923
bf88dd68
JK
924/* Wrapper for "info auto-load gdb-scripts". */
925
926static void
927info_auto_load_gdb_scripts (char *pattern, int from_tty)
928{
929 auto_load_info_scripts (pattern, from_tty, &script_language_gdb);
930}
931
932/* Implement 'info auto-load local-gdbinit'. */
933
934static void
935info_auto_load_local_gdbinit (char *args, int from_tty)
936{
937 if (auto_load_local_gdbinit_pathname == NULL)
938 printf_filtered (_("Local .gdbinit file was not found.\n"));
939 else if (auto_load_local_gdbinit_loaded)
940 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
941 auto_load_local_gdbinit_pathname);
942 else
943 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
944 auto_load_local_gdbinit_pathname);
945}
946
e2207b9a
JK
947/* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
948 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
949 of PSPACE_INFO. */
950
951int
952script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
953{
954 int retval = !pspace_info->script_not_found_warning_printed;
955
956 pspace_info->script_not_found_warning_printed = 1;
957
958 return retval;
959}
960
bf88dd68
JK
961/* The only valid "set auto-load" argument is off|0|no|disable. */
962
963static void
964set_auto_load_cmd (char *args, int from_tty)
965{
966 struct cmd_list_element *list;
967 size_t length;
968
969 /* See parse_binary_operation in use by the sub-commands. */
970
971 length = args ? strlen (args) : 0;
972
973 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
974 length--;
975
976 if (length == 0 || (strncmp (args, "off", length) != 0
977 && strncmp (args, "0", length) != 0
978 && strncmp (args, "no", length) != 0
979 && strncmp (args, "disable", length) != 0))
980 error (_("Valid is only global 'set auto-load no'; "
981 "otherwise check the auto-load sub-commands."));
982
983 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
984 if (list->var_type == var_boolean)
985 {
986 gdb_assert (list->type == set_cmd);
987 do_setshow_command (args, from_tty, list);
988 }
989}
990
991/* Initialize "set auto-load " commands prefix and return it. */
992
993struct cmd_list_element **
994auto_load_set_cmdlist_get (void)
995{
996 static struct cmd_list_element *retval;
997
998 if (retval == NULL)
999 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1000Auto-loading specific settings.\n\
1001Configure various auto-load-specific variables such as\n\
1002automatic loading of Python scripts."),
1003 &retval, "set auto-load ",
1004 1/*allow-unknown*/, &setlist);
1005
1006 return &retval;
1007}
1008
1009/* Command "show auto-load" displays summary of all the current
1010 "show auto-load " settings. */
1011
1012static void
1013show_auto_load_cmd (char *args, int from_tty)
1014{
1015 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1016}
1017
1018/* Initialize "show auto-load " commands prefix and return it. */
1019
1020struct cmd_list_element **
1021auto_load_show_cmdlist_get (void)
1022{
1023 static struct cmd_list_element *retval;
1024
1025 if (retval == NULL)
1026 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1027Show auto-loading specific settings.\n\
1028Show configuration of various auto-load-specific variables such as\n\
1029automatic loading of Python scripts."),
1030 &retval, "show auto-load ",
1031 0/*allow-unknown*/, &showlist);
1032
1033 return &retval;
1034}
1035
1036/* Command "info auto-load" displays whether the various auto-load files have
1037 been loaded. This is reimplementation of cmd_show_list which inserts
1038 newlines at proper places. */
1039
1040static void
1041info_auto_load_cmd (char *args, int from_tty)
1042{
1043 struct cmd_list_element *list;
1044 struct cleanup *infolist_chain;
1045 struct ui_out *uiout = current_uiout;
1046
1047 infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");
1048
1049 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1050 {
1051 struct cleanup *option_chain
1052 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
1053
1054 gdb_assert (!list->prefixlist);
1055 gdb_assert (list->type == not_set_cmd);
1056
1057 ui_out_field_string (uiout, "name", list->name);
1058 ui_out_text (uiout, ": ");
1059 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1060
1061 /* Close the tuple. */
1062 do_cleanups (option_chain);
1063 }
1064
1065 /* Close the tuple. */
1066 do_cleanups (infolist_chain);
1067}
1068
1069/* Initialize "info auto-load " commands prefix and return it. */
1070
1071struct cmd_list_element **
1072auto_load_info_cmdlist_get (void)
1073{
1074 static struct cmd_list_element *retval;
1075
1076 if (retval == NULL)
1077 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1078Print current status of auto-loaded files.\n\
1079Print whether various files like Python scripts or .gdbinit files have been\n\
1080found and/or loaded."),
1081 &retval, "info auto-load ",
1082 0/*allow-unknown*/, &infolist);
1083
1084 return &retval;
1085}
1086
e2207b9a
JK
1087void _initialize_auto_load (void);
1088
1089void
1090_initialize_auto_load (void)
1091{
bccbefd2
JK
1092 struct cmd_list_element *cmd;
1093
e2207b9a
JK
1094 auto_load_pspace_data
1095 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup);
1096
1097 observer_attach_new_objfile (auto_load_new_objfile);
1098
bf88dd68
JK
1099 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1100 &auto_load_gdb_scripts, _("\
1101Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1102Show whether auto-loading of canned sequences of commands scripts is enabled."),
1103 _("\
1104If enabled, canned sequences of commands are loaded when the debugger reads\n\
1105an executable or shared library.\n\
1106This options has security implications for untrusted inferiors."),
1107 NULL, show_auto_load_gdb_scripts,
1108 auto_load_set_cmdlist_get (),
1109 auto_load_show_cmdlist_get ());
1110
1111 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1112 _("Print the list of automatically loaded sequences of commands.\n\
1113Usage: info auto-load gdb-scripts [REGEXP]"),
1114 auto_load_info_cmdlist_get ());
1115
1116 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1117 &auto_load_local_gdbinit, _("\
1118Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1119Show whether auto-loading .gdbinit script in current directory is enabled."),
1120 _("\
1121If enabled, canned sequences of commands are loaded when debugger starts\n\
1122from .gdbinit file in current directory. Such files are deprecated,\n\
1123use a script associated with inferior executable file instead.\n\
1124This options has security implications for untrusted inferiors."),
1125 NULL, show_auto_load_local_gdbinit,
1126 auto_load_set_cmdlist_get (),
1127 auto_load_show_cmdlist_get ());
1128
1129 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1130 _("Print whether current directory .gdbinit file has been loaded.\n\
1131Usage: info auto-load local-gdbinit"),
1132 auto_load_info_cmdlist_get ());
bccbefd2 1133
7349ff92
JK
1134 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1135 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1136 &auto_load_dir, _("\
1137Set the list of directories from which to load auto-loaded scripts."), _("\
1138Show the list of directories from which to load auto-loaded scripts."), _("\
1139Automatically loaded Python scripts and GDB scripts are located in one of the\n\
1140directories listed by this option. This option is ignored for the kinds of\n\
1141scripts having 'set auto-load ... off'. Directories listed here need to be\n\
1142present also in the 'set auto-load safe-path' option."),
1143 set_auto_load_dir, show_auto_load_dir,
1144 auto_load_set_cmdlist_get (),
1145 auto_load_show_cmdlist_get ());
1146
6dea1fbd 1147 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
bccbefd2
JK
1148 auto_load_safe_path_vec_update ();
1149 add_setshow_optional_filename_cmd ("safe-path", class_support,
1150 &auto_load_safe_path, _("\
1151Set the list of directories from which it is safe to auto-load files."), _("\
1152Show the list of directories from which it is safe to auto-load files."), _("\
1153Various files loaded automatically for the 'set auto-load ...' options must\n\
1154be located in one of the directories listed by this option. Warning will be\n\
af2c1515
JK
1155printed and file will not be used otherwise.\n\
1156Setting this parameter to an empty list resets it to its default value.\n\
1157Setting this parameter to '/' (without the quotes) allows any file\n\
1158for the 'set auto-load ...' options.\n\
bccbefd2
JK
1159This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1160This options has security implications for untrusted inferiors."),
1161 set_auto_load_safe_path,
1162 show_auto_load_safe_path,
1163 auto_load_set_cmdlist_get (),
1164 auto_load_show_cmdlist_get ());
6dea1fbd 1165 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
bccbefd2
JK
1166
1167 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1168 add_auto_load_safe_path,
1169 _("Add entries to the list of directories from which it is safe "
1170 "to auto-load files.\n\
1171See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1172access the current full list setting."),
1173 &cmdlist);
1174 set_cmd_completer (cmd, filename_completer);
4dc84fd1
JK
1175
1176 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1177 &debug_auto_load, _("\
1178Set auto-load verifications debugging."), _("\
1179Show auto-load verifications debugging."), _("\
1180When non-zero, debugging output for files of 'set auto-load ...'\n\
1181is displayed."),
1182 NULL, show_debug_auto_load,
1183 &setdebuglist, &showdebuglist);
e2207b9a 1184}
This page took 0.078172 seconds and 4 git commands to generate.