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