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